2 Copyright (C) 2001-2002 Michael Niedermayer <michaelni@gmx.at>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 supported Input formats: YV12, I420, IYUV, YUY2, BGR32, BGR24, BGR16, BGR15, RGB32, RGB24, Y8, Y800
21 supported output formats: YV12, I420, IYUV, BGR15, BGR16, BGR24, BGR32 (grayscale soon too)
22 BGR15/16 support dithering
24 unscaled special converters
25 YV12/I420/IYUV -> BGR15/BGR16/BGR24/BGR32
26 YV12/I420/IYUV -> YV12/I420/IYUV
27 YUY2/BGR15/BGR16/BGR24/BGR32/RGB24/RGB32 -> same format
28 BGR24 -> BGR32 & RGB24 -> RGB32
29 BGR32 -> BGR24 & RGB32 -> RGB24
34 tested special converters
40 untested special converters
41 YV12/I420 -> BGR15/BGR24/BGR32 (its the yuv2rgb stuff, so it should be ok)
42 YV12/I420 -> YV12/I420
43 YUY2/BGR15/BGR24/BGR32/RGB24/RGB32 -> same format
44 BGR24 -> BGR32 & RGB24 -> RGB32
45 BGR32 -> BGR24 & RGB32 -> RGB24
53 #include "../config.h"
54 #include "../mangle.h"
62 #include "../cpudetect.h"
64 #include "../libvo/img_format.h"
66 #include "../libvo/fastmemcpy.h"
67 #include "../mp_msg.h"
69 #define MSG_WARN(args...) mp_msg(MSGT_SWS,MSGL_WARN, ##args )
70 #define MSG_FATAL(args...) mp_msg(MSGT_SWS,MSGL_FATAL, ##args )
71 #define MSG_ERR(args...) mp_msg(MSGT_SWS,MSGL_ERR, ##args )
72 #define MSG_V(args...) mp_msg(MSGT_SWS,MSGL_V, ##args )
73 #define MSG_DBG2(args...) mp_msg(MSGT_SWS,MSGL_DBG2, ##args )
74 #define MSG_INFO(args...) mp_msg(MSGT_SWS,MSGL_INFO, ##args )
83 //#define WORDS_BIGENDIAN
86 #define FAST_BGR2YV12 // use 7 bit coeffs instead of 15bit
88 #define RET 0xC3 //near return opcode for X86
91 #define ASSERT(x) assert(x);
99 #define PI 3.14159265358979323846
102 //FIXME replace this with something faster
103 #define isBGR(x) ((x)==IMGFMT_BGR32|| (x)==IMGFMT_BGR24|| (x)==IMGFMT_BGR16|| (x)==IMGFMT_BGR15)
104 #define isRGB(x) ((x)==IMGFMT_RGB32|| (x)==IMGFMT_RGB24|| (x)==IMGFMT_RGB16|| (x)==IMGFMT_RGB15)
105 #define isPlanarYUV(x) ((x)==IMGFMT_YV12 || (x)==IMGFMT_I420 || (x)==IMGFMT_IYUV|| (x)==IMGFMT_YVU9 || (x)==IMGFMT_IF09)
106 #define isYUV(x) (!(isBGR(x) || isRGB(x)))
107 #define isHalfChrV(x) ((x)==IMGFMT_YV12 || (x)==IMGFMT_I420 || (x)==IMGFMT_IYUV)
108 #define isHalfChrH(x) ((x)==IMGFMT_YUY2 || (x)==IMGFMT_YV12 || (x)==IMGFMT_I420)
109 #define isPacked(x) (isYUV(x) && !isPlanarYUV(x))
110 #define isGray(x) ((x)==IMGFMT_Y800) /* Behaviour the same as PACKED but it's PLANAR */
111 #define isSupportedIn(x) ((x)==IMGFMT_YV12 || (x)==IMGFMT_I420 || (x)==IMGFMT_YUY2 \
112 || (x)==IMGFMT_BGR32|| (x)==IMGFMT_BGR24|| (x)==IMGFMT_BGR16|| (x)==IMGFMT_BGR15\
113 || (x)==IMGFMT_RGB32|| (x)==IMGFMT_RGB24\
115 #define isSupportedOut(x) ((x)==IMGFMT_YV12 || (x)==IMGFMT_I420 \
116 || (x)==IMGFMT_BGR32|| (x)==IMGFMT_BGR24|| (x)==IMGFMT_BGR16|| (x)==IMGFMT_BGR15)
117 #define isSupportedUnscaledIn(x) ((x)==IMGFMT_YV12 || (x)==IMGFMT_I420 || (x)==IMGFMT_YUY2 || (x)==IMGFMT_NV12 \
118 || (x)==IMGFMT_BGR32|| (x)==IMGFMT_BGR24|| (x)==IMGFMT_BGR16|| (x)==IMGFMT_BGR15\
119 || (x)==IMGFMT_RGB32|| (x)==IMGFMT_RGB24\
121 #define isSupportedUnscaledOut(x) ((x)==IMGFMT_YV12 || (x)==IMGFMT_I420 || (x) == IMGFMT_YUY2 \
122 || (x)==IMGFMT_BGR32|| (x)==IMGFMT_BGR24|| (x)==IMGFMT_BGR16|| (x)==IMGFMT_BGR15)
124 #define RGB2YUV_SHIFT 16
125 #define BY ((int)( 0.098*(1<<RGB2YUV_SHIFT)+0.5))
126 #define BV ((int)(-0.071*(1<<RGB2YUV_SHIFT)+0.5))
127 #define BU ((int)( 0.439*(1<<RGB2YUV_SHIFT)+0.5))
128 #define GY ((int)( 0.504*(1<<RGB2YUV_SHIFT)+0.5))
129 #define GV ((int)(-0.368*(1<<RGB2YUV_SHIFT)+0.5))
130 #define GU ((int)(-0.291*(1<<RGB2YUV_SHIFT)+0.5))
131 #define RY ((int)( 0.257*(1<<RGB2YUV_SHIFT)+0.5))
132 #define RV ((int)( 0.439*(1<<RGB2YUV_SHIFT)+0.5))
133 #define RU ((int)(-0.148*(1<<RGB2YUV_SHIFT)+0.5))
135 extern int verbose; // defined in mplayer.c
138 Special versions: fast Y 1:1 scaling (no interpolation in y direction)
141 more intelligent missalignment avoidance for the horizontal scaler
142 write special vertical cubic upscale version
143 Optimize C code (yv12 / minmax)
144 add support for packed pixel yuv input & output
145 add support for Y8 output
146 optimize bgr24 & bgr32
147 add BGR4 output support
148 write special BGR->BGR scaler
149 deglobalize yuv2rgb*.c
152 #define ABS(a) ((a) > 0 ? (a) : (-(a)))
153 #define MIN(a,b) ((a) > (b) ? (b) : (a))
154 #define MAX(a,b) ((a) < (b) ? (b) : (a))
157 #define CAN_COMPILE_X86_ASM
160 #ifdef CAN_COMPILE_X86_ASM
161 static uint64_t __attribute__((aligned(8))) yCoeff= 0x2568256825682568LL;
162 static uint64_t __attribute__((aligned(8))) vrCoeff= 0x3343334333433343LL;
163 static uint64_t __attribute__((aligned(8))) ubCoeff= 0x40cf40cf40cf40cfLL;
164 static uint64_t __attribute__((aligned(8))) vgCoeff= 0xE5E2E5E2E5E2E5E2LL;
165 static uint64_t __attribute__((aligned(8))) ugCoeff= 0xF36EF36EF36EF36ELL;
166 static uint64_t __attribute__((aligned(8))) bF8= 0xF8F8F8F8F8F8F8F8LL;
167 static uint64_t __attribute__((aligned(8))) bFC= 0xFCFCFCFCFCFCFCFCLL;
168 static uint64_t __attribute__((aligned(8))) w400= 0x0400040004000400LL;
169 static uint64_t __attribute__((aligned(8))) w80= 0x0080008000800080LL;
170 static uint64_t __attribute__((aligned(8))) w10= 0x0010001000100010LL;
171 static uint64_t __attribute__((aligned(8))) w02= 0x0002000200020002LL;
172 static uint64_t __attribute__((aligned(8))) bm00001111=0x00000000FFFFFFFFLL;
173 static uint64_t __attribute__((aligned(8))) bm00000111=0x0000000000FFFFFFLL;
174 static uint64_t __attribute__((aligned(8))) bm11111000=0xFFFFFFFFFF000000LL;
175 static uint64_t __attribute__((aligned(8))) bm01010101=0x00FF00FF00FF00FFLL;
177 static volatile uint64_t __attribute__((aligned(8))) b5Dither;
178 static volatile uint64_t __attribute__((aligned(8))) g5Dither;
179 static volatile uint64_t __attribute__((aligned(8))) g6Dither;
180 static volatile uint64_t __attribute__((aligned(8))) r5Dither;
182 static uint64_t __attribute__((aligned(8))) dither4[2]={
183 0x0103010301030103LL,
184 0x0200020002000200LL,};
186 static uint64_t __attribute__((aligned(8))) dither8[2]={
187 0x0602060206020602LL,
188 0x0004000400040004LL,};
190 static uint64_t __attribute__((aligned(8))) b16Mask= 0x001F001F001F001FLL;
191 static uint64_t __attribute__((aligned(8))) g16Mask= 0x07E007E007E007E0LL;
192 static uint64_t __attribute__((aligned(8))) r16Mask= 0xF800F800F800F800LL;
193 static uint64_t __attribute__((aligned(8))) b15Mask= 0x001F001F001F001FLL;
194 static uint64_t __attribute__((aligned(8))) g15Mask= 0x03E003E003E003E0LL;
195 static uint64_t __attribute__((aligned(8))) r15Mask= 0x7C007C007C007C00LL;
197 static uint64_t __attribute__((aligned(8))) M24A= 0x00FF0000FF0000FFLL;
198 static uint64_t __attribute__((aligned(8))) M24B= 0xFF0000FF0000FF00LL;
199 static uint64_t __attribute__((aligned(8))) M24C= 0x0000FF0000FF0000LL;
202 static const uint64_t bgr2YCoeff __attribute__((aligned(8))) = 0x000000210041000DULL;
203 static const uint64_t bgr2UCoeff __attribute__((aligned(8))) = 0x0000FFEEFFDC0038ULL;
204 static const uint64_t bgr2VCoeff __attribute__((aligned(8))) = 0x00000038FFD2FFF8ULL;
206 static const uint64_t bgr2YCoeff __attribute__((aligned(8))) = 0x000020E540830C8BULL;
207 static const uint64_t bgr2UCoeff __attribute__((aligned(8))) = 0x0000ED0FDAC23831ULL;
208 static const uint64_t bgr2VCoeff __attribute__((aligned(8))) = 0x00003831D0E6F6EAULL;
210 static const uint64_t bgr2YOffset __attribute__((aligned(8))) = 0x1010101010101010ULL;
211 static const uint64_t bgr2UVOffset __attribute__((aligned(8)))= 0x8080808080808080ULL;
212 static const uint64_t w1111 __attribute__((aligned(8))) = 0x0001000100010001ULL;
215 static uint64_t __attribute__((aligned(8))) asm_yalpha1;
216 static uint64_t __attribute__((aligned(8))) asm_uvalpha1;
219 // clipping helper table for C implementations:
220 static unsigned char clip_table[768];
222 static unsigned short clip_table16b[768];
223 static unsigned short clip_table16g[768];
224 static unsigned short clip_table16r[768];
225 static unsigned short clip_table15b[768];
226 static unsigned short clip_table15g[768];
227 static unsigned short clip_table15r[768];
229 // yuv->rgb conversion tables:
230 static int yuvtab_2568[256];
231 static int yuvtab_3343[256];
232 static int yuvtab_0c92[256];
233 static int yuvtab_1a1e[256];
234 static int yuvtab_40cf[256];
235 // Needed for cubic scaler to catch overflows
236 static int clip_yuvtab_2568[768];
237 static int clip_yuvtab_3343[768];
238 static int clip_yuvtab_0c92[768];
239 static int clip_yuvtab_1a1e[768];
240 static int clip_yuvtab_40cf[768];
242 //global sws_flags from the command line
246 SwsFilter src_filter= {NULL, NULL, NULL, NULL};
248 float sws_lum_gblur= 0.0;
249 float sws_chr_gblur= 0.0;
250 int sws_chr_vshift= 0;
251 int sws_chr_hshift= 0;
252 float sws_chr_sharpen= 0.0;
253 float sws_lum_sharpen= 0.0;
255 /* cpuCaps combined from cpudetect and whats actually compiled in
256 (if there is no support for something compiled in it wont appear here) */
257 static CpuCaps cpuCaps;
259 void (*swScale)(SwsContext *context, uint8_t* src[], int srcStride[], int srcSliceY,
260 int srcSliceH, uint8_t* dst[], int dstStride[])=NULL;
262 static SwsVector *getConvVec(SwsVector *a, SwsVector *b);
264 #ifdef CAN_COMPILE_X86_ASM
265 void in_asm_used_var_warning_killer()
267 volatile int i= yCoeff+vrCoeff+ubCoeff+vgCoeff+ugCoeff+bF8+bFC+w400+w80+w10+
268 bm00001111+bm00000111+bm11111000+b16Mask+g16Mask+r16Mask+b15Mask+g15Mask+r15Mask+asm_yalpha1+ asm_uvalpha1+
269 M24A+M24B+M24C+w02 + b5Dither+g5Dither+r5Dither+g6Dither+dither4[0]+dither8[0]+bm01010101;
274 static inline void yuv2yuvXinC(int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
275 int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
276 uint8_t *dest, uint8_t *uDest, uint8_t *vDest, int dstW)
278 //FIXME Optimize (just quickly writen not opti..)
280 for(i=0; i<dstW; i++)
284 for(j=0; j<lumFilterSize; j++)
285 val += lumSrc[j][i] * lumFilter[j];
287 dest[i]= MIN(MAX(val>>19, 0), 255);
291 for(i=0; i<(dstW>>1); i++)
296 for(j=0; j<chrFilterSize; j++)
298 u += chrSrc[j][i] * chrFilter[j];
299 v += chrSrc[j][i + 2048] * chrFilter[j];
302 uDest[i]= MIN(MAX(u>>19, 0), 255);
303 vDest[i]= MIN(MAX(v>>19, 0), 255);
307 static inline void yuv2rgbXinC(int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
308 int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
309 uint8_t *dest, int dstW, int dstFormat)
311 if(dstFormat==IMGFMT_BGR32)
314 #ifdef WORDS_BIGENDIAN
317 for(i=0; i<(dstW>>1); i++){
324 for(j=0; j<lumFilterSize; j++)
326 Y1 += lumSrc[j][2*i] * lumFilter[j];
327 Y2 += lumSrc[j][2*i+1] * lumFilter[j];
329 for(j=0; j<chrFilterSize; j++)
331 U += chrSrc[j][i] * chrFilter[j];
332 V += chrSrc[j][i+2048] * chrFilter[j];
334 Y1= clip_yuvtab_2568[ (Y1>>19) + 256 ];
335 Y2= clip_yuvtab_2568[ (Y2>>19) + 256 ];
339 Cb= clip_yuvtab_40cf[U+ 256];
340 Cg= clip_yuvtab_1a1e[V+ 256] + yuvtab_0c92[U+ 256];
341 Cr= clip_yuvtab_3343[V+ 256];
343 dest[8*i+0]=clip_table[((Y1 + Cb) >>13)];
344 dest[8*i+1]=clip_table[((Y1 + Cg) >>13)];
345 dest[8*i+2]=clip_table[((Y1 + Cr) >>13)];
347 dest[8*i+4]=clip_table[((Y2 + Cb) >>13)];
348 dest[8*i+5]=clip_table[((Y2 + Cg) >>13)];
349 dest[8*i+6]=clip_table[((Y2 + Cr) >>13)];
352 else if(dstFormat==IMGFMT_BGR24)
355 for(i=0; i<(dstW>>1); i++){
362 for(j=0; j<lumFilterSize; j++)
364 Y1 += lumSrc[j][2*i] * lumFilter[j];
365 Y2 += lumSrc[j][2*i+1] * lumFilter[j];
367 for(j=0; j<chrFilterSize; j++)
369 U += chrSrc[j][i] * chrFilter[j];
370 V += chrSrc[j][i+2048] * chrFilter[j];
372 Y1= clip_yuvtab_2568[ (Y1>>19) + 256 ];
373 Y2= clip_yuvtab_2568[ (Y2>>19) + 256 ];
377 Cb= clip_yuvtab_40cf[U+ 256];
378 Cg= clip_yuvtab_1a1e[V+ 256] + yuvtab_0c92[U+ 256];
379 Cr= clip_yuvtab_3343[V+ 256];
381 dest[0]=clip_table[((Y1 + Cb) >>13)];
382 dest[1]=clip_table[((Y1 + Cg) >>13)];
383 dest[2]=clip_table[((Y1 + Cr) >>13)];
385 dest[3]=clip_table[((Y2 + Cb) >>13)];
386 dest[4]=clip_table[((Y2 + Cg) >>13)];
387 dest[5]=clip_table[((Y2 + Cr) >>13)];
391 else if(dstFormat==IMGFMT_BGR16)
395 static int ditherb1=1<<14;
396 static int ditherg1=1<<13;
397 static int ditherr1=2<<14;
398 static int ditherb2=3<<14;
399 static int ditherg2=3<<13;
400 static int ditherr2=0<<14;
402 ditherb1 ^= (1^2)<<14;
403 ditherg1 ^= (1^2)<<13;
404 ditherr1 ^= (1^2)<<14;
405 ditherb2 ^= (3^0)<<14;
406 ditherg2 ^= (3^0)<<13;
407 ditherr2 ^= (3^0)<<14;
409 const int ditherb1=0;
410 const int ditherg1=0;
411 const int ditherr1=0;
412 const int ditherb2=0;
413 const int ditherg2=0;
414 const int ditherr2=0;
416 for(i=0; i<(dstW>>1); i++){
423 for(j=0; j<lumFilterSize; j++)
425 Y1 += lumSrc[j][2*i] * lumFilter[j];
426 Y2 += lumSrc[j][2*i+1] * lumFilter[j];
428 for(j=0; j<chrFilterSize; j++)
430 U += chrSrc[j][i] * chrFilter[j];
431 V += chrSrc[j][i+2048] * chrFilter[j];
433 Y1= clip_yuvtab_2568[ (Y1>>19) + 256 ];
434 Y2= clip_yuvtab_2568[ (Y2>>19) + 256 ];
438 Cb= clip_yuvtab_40cf[U+ 256];
439 Cg= clip_yuvtab_1a1e[V+ 256] + yuvtab_0c92[U+ 256];
440 Cr= clip_yuvtab_3343[V+ 256];
442 ((uint16_t*)dest)[2*i] =
443 clip_table16b[(Y1 + Cb + ditherb1) >>13] |
444 clip_table16g[(Y1 + Cg + ditherg1) >>13] |
445 clip_table16r[(Y1 + Cr + ditherr1) >>13];
447 ((uint16_t*)dest)[2*i+1] =
448 clip_table16b[(Y2 + Cb + ditherb2) >>13] |
449 clip_table16g[(Y2 + Cg + ditherg2) >>13] |
450 clip_table16r[(Y2 + Cr + ditherr2) >>13];
453 else if(dstFormat==IMGFMT_BGR15)
457 static int ditherb1=1<<14;
458 static int ditherg1=1<<14;
459 static int ditherr1=2<<14;
460 static int ditherb2=3<<14;
461 static int ditherg2=3<<14;
462 static int ditherr2=0<<14;
464 ditherb1 ^= (1^2)<<14;
465 ditherg1 ^= (1^2)<<14;
466 ditherr1 ^= (1^2)<<14;
467 ditherb2 ^= (3^0)<<14;
468 ditherg2 ^= (3^0)<<14;
469 ditherr2 ^= (3^0)<<14;
471 const int ditherb1=0;
472 const int ditherg1=0;
473 const int ditherr1=0;
474 const int ditherb2=0;
475 const int ditherg2=0;
476 const int ditherr2=0;
478 for(i=0; i<(dstW>>1); i++){
485 for(j=0; j<lumFilterSize; j++)
487 Y1 += lumSrc[j][2*i] * lumFilter[j];
488 Y2 += lumSrc[j][2*i+1] * lumFilter[j];
490 for(j=0; j<chrFilterSize; j++)
492 U += chrSrc[j][i] * chrFilter[j];
493 V += chrSrc[j][i+2048] * chrFilter[j];
495 Y1= clip_yuvtab_2568[ (Y1>>19) + 256 ];
496 Y2= clip_yuvtab_2568[ (Y2>>19) + 256 ];
500 Cb= clip_yuvtab_40cf[U+ 256];
501 Cg= clip_yuvtab_1a1e[V+ 256] + yuvtab_0c92[U+ 256];
502 Cr= clip_yuvtab_3343[V+ 256];
504 ((uint16_t*)dest)[2*i] =
505 clip_table15b[(Y1 + Cb + ditherb1) >>13] |
506 clip_table15g[(Y1 + Cg + ditherg1) >>13] |
507 clip_table15r[(Y1 + Cr + ditherr1) >>13];
509 ((uint16_t*)dest)[2*i+1] =
510 clip_table15b[(Y2 + Cb + ditherb2) >>13] |
511 clip_table15g[(Y2 + Cg + ditherg2) >>13] |
512 clip_table15r[(Y2 + Cr + ditherr2) >>13];
518 //Note: we have C, X86, MMX, MMX2, 3DNOW version therse no 3DNOW+MMX2 one
520 #if !defined (HAVE_MMX) || defined (RUNTIME_CPUDETECT)
524 #ifdef CAN_COMPILE_X86_ASM
526 #if (defined (HAVE_MMX) && !defined (HAVE_3DNOW) && !defined (HAVE_MMX2)) || defined (RUNTIME_CPUDETECT)
530 #if defined (HAVE_MMX2) || defined (RUNTIME_CPUDETECT)
534 #if (defined (HAVE_3DNOW) && !defined (HAVE_MMX2)) || defined (RUNTIME_CPUDETECT)
535 #define COMPILE_3DNOW
537 #endif //CAN_COMPILE_X86_ASM
547 #define RENAME(a) a ## _C
548 #include "swscale_template.c"
551 #ifdef CAN_COMPILE_X86_ASM
560 #define RENAME(a) a ## _X86
561 #include "swscale_template.c"
569 #define RENAME(a) a ## _MMX
570 #include "swscale_template.c"
579 #define RENAME(a) a ## _MMX2
580 #include "swscale_template.c"
589 #define RENAME(a) a ## _3DNow
590 #include "swscale_template.c"
593 #endif //CAN_COMPILE_X86_ASM
595 // minor note: the HAVE_xyz is messed up after that line so dont use it
598 // old global scaler, dont use for new code
599 // will use sws_flags from the command line
600 void SwScale_YV12slice(unsigned char* src[], int srcStride[], int srcSliceY ,
601 int srcSliceH, uint8_t* dst[], int dstStride, int dstbpp,
602 int srcW, int srcH, int dstW, int dstH){
604 static SwsContext *context=NULL;
606 int dstStride3[3]= {dstStride, dstStride>>1, dstStride>>1};
610 case 8 : dstFormat= IMGFMT_Y8; break;
611 case 12: dstFormat= IMGFMT_YV12; break;
612 case 15: dstFormat= IMGFMT_BGR15; break;
613 case 16: dstFormat= IMGFMT_BGR16; break;
614 case 24: dstFormat= IMGFMT_BGR24; break;
615 case 32: dstFormat= IMGFMT_BGR32; break;
619 if(!context) context=getSwsContextFromCmdLine(srcW, srcH, IMGFMT_YV12, dstW, dstH, dstFormat);
621 context->swScale(context, src, srcStride, srcSliceY, srcSliceH, dst, dstStride3);
624 // will use sws_flags & src_filter (from cmd line)
625 SwsContext *getSwsContextFromCmdLine(int srcW, int srcH, int srcFormat, int dstW, int dstH, int dstFormat)
628 static int firstTime=1;
632 asm volatile("emms\n\t"::: "memory"); //FIXME this shouldnt be required but it IS (even for non mmx versions)
637 flags= SWS_PRINT_INFO;
639 else if(verbose>1) flags= SWS_PRINT_INFO;
641 if(src_filter.lumH) freeVec(src_filter.lumH);
642 if(src_filter.lumV) freeVec(src_filter.lumV);
643 if(src_filter.chrH) freeVec(src_filter.chrH);
644 if(src_filter.chrV) freeVec(src_filter.chrV);
646 if(sws_lum_gblur!=0.0){
647 src_filter.lumH= getGaussianVec(sws_lum_gblur, 3.0);
648 src_filter.lumV= getGaussianVec(sws_lum_gblur, 3.0);
650 src_filter.lumH= getIdentityVec();
651 src_filter.lumV= getIdentityVec();
654 if(sws_chr_gblur!=0.0){
655 src_filter.chrH= getGaussianVec(sws_chr_gblur, 3.0);
656 src_filter.chrV= getGaussianVec(sws_chr_gblur, 3.0);
658 src_filter.chrH= getIdentityVec();
659 src_filter.chrV= getIdentityVec();
662 if(sws_chr_sharpen!=0.0){
663 SwsVector *g= getConstVec(-1.0, 3);
664 SwsVector *id= getConstVec(10.0/sws_chr_sharpen, 1);
667 convVec(src_filter.chrH, id);
668 convVec(src_filter.chrV, id);
673 if(sws_lum_sharpen!=0.0){
674 SwsVector *g= getConstVec(-1.0, 3);
675 SwsVector *id= getConstVec(10.0/sws_lum_sharpen, 1);
678 convVec(src_filter.lumH, id);
679 convVec(src_filter.lumV, id);
685 shiftVec(src_filter.chrH, sws_chr_hshift);
688 shiftVec(src_filter.chrV, sws_chr_vshift);
690 normalizeVec(src_filter.chrH, 1.0);
691 normalizeVec(src_filter.chrV, 1.0);
692 normalizeVec(src_filter.lumH, 1.0);
693 normalizeVec(src_filter.lumV, 1.0);
695 if(verbose > 1) printVec(src_filter.chrH);
696 if(verbose > 1) printVec(src_filter.lumH);
700 case 0: flags|= SWS_FAST_BILINEAR; break;
701 case 1: flags|= SWS_BILINEAR; break;
702 case 2: flags|= SWS_BICUBIC; break;
703 case 3: flags|= SWS_X; break;
704 case 4: flags|= SWS_POINT; break;
705 case 5: flags|= SWS_AREA; break;
706 default:flags|= SWS_BILINEAR; break;
709 return getSwsContext(srcW, srcH, srcFormat, dstW, dstH, dstFormat, flags, &src_filter, NULL);
713 static inline void initFilter(int16_t **outFilter, int16_t **filterPos, int *outFilterSize, int xInc,
714 int srcW, int dstW, int filterAlign, int one, int flags,
715 SwsVector *srcFilter, SwsVector *dstFilter)
722 double *filter2=NULL;
725 asm volatile("emms\n\t"::: "memory"); //FIXME this shouldnt be required but it IS (even for non mmx versions)
728 // Note the +1 is for the MMXscaler which reads over the end
729 *filterPos = (int16_t*)memalign(8, (dstW+1)*sizeof(int16_t));
731 if(ABS(xInc - 0x10000) <10) // unscaled
735 filter= (double*)memalign(8, dstW*sizeof(double)*filterSize);
736 for(i=0; i<dstW*filterSize; i++) filter[i]=0;
738 for(i=0; i<dstW; i++)
740 filter[i*filterSize]=1;
745 else if(flags&SWS_POINT) // lame looking point sampling mode
750 filter= (double*)memalign(8, dstW*sizeof(double)*filterSize);
752 xDstInSrc= xInc/2 - 0x8000;
753 for(i=0; i<dstW; i++)
755 int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
762 else if(xInc <= (1<<16) || (flags&SWS_FAST_BILINEAR)) // upscale
766 if (flags&SWS_BICUBIC) filterSize= 4;
767 else if(flags&SWS_X ) filterSize= 4;
768 else filterSize= 2; // SWS_BILINEAR / SWS_AREA
769 filter= (double*)memalign(8, dstW*sizeof(double)*filterSize);
771 xDstInSrc= xInc/2 - 0x8000;
772 for(i=0; i<dstW; i++)
774 int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
778 if((flags & SWS_BICUBIC) || (flags & SWS_X))
780 double d= ABS(((xx+1)<<16) - xDstInSrc)/(double)(1<<16);
783 if(flags & SWS_BICUBIC){
784 // Equation is from VirtualDub
785 y1 = ( + A*d - 2.0*A*d*d + A*d*d*d);
786 y2 = (+ 1.0 - (A+3.0)*d*d + (A+2.0)*d*d*d);
787 y3 = ( - A*d + (2.0*A+3.0)*d*d - (A+2.0)*d*d*d);
788 y4 = ( + A*d*d - A*d*d*d);
790 // cubic interpolation (derived it myself)
791 y1 = ( -2.0*d + 3.0*d*d - 1.0*d*d*d)/6.0;
792 y2 = (6.0 -3.0*d - 6.0*d*d + 3.0*d*d*d)/6.0;
793 y3 = ( +6.0*d + 3.0*d*d - 3.0*d*d*d)/6.0;
794 y4 = ( -1.0*d + 1.0*d*d*d)/6.0;
797 filter[i*filterSize + 0]= y1;
798 filter[i*filterSize + 1]= y2;
799 filter[i*filterSize + 2]= y3;
800 filter[i*filterSize + 3]= y4;
804 //Bilinear upscale / linear interpolate / Area averaging
805 for(j=0; j<filterSize; j++)
807 double d= ABS((xx<<16) - xDstInSrc)/(double)(1<<16);
808 double coeff= 1.0 - d;
810 filter[i*filterSize + j]= coeff;
822 if(flags&SWS_BICUBIC) filterSize= (int)ceil(1 + 4.0*srcW / (double)dstW);
823 else if(flags&SWS_X) filterSize= (int)ceil(1 + 4.0*srcW / (double)dstW);
824 else if(flags&SWS_AREA) filterSize= (int)ceil(1 + 1.0*srcW / (double)dstW);
825 else /* BILINEAR */ filterSize= (int)ceil(1 + 2.0*srcW / (double)dstW);
826 filter= (double*)memalign(8, dstW*sizeof(double)*filterSize);
828 xDstInSrc= xInc/2 - 0x8000;
829 for(i=0; i<dstW; i++)
831 int xx= (int)((double)xDstInSrc/(double)(1<<16) - (filterSize-1)*0.5 + 0.5);
834 for(j=0; j<filterSize; j++)
836 double d= ABS((xx<<16) - xDstInSrc)/(double)xInc;
838 if((flags & SWS_BICUBIC) || (flags & SWS_X))
842 // Equation is from VirtualDub
844 coeff = (1.0 - (A+3.0)*d*d + (A+2.0)*d*d*d);
846 coeff = (-4.0*A + 8.0*A*d - 5.0*A*d*d + A*d*d*d);
850 else if(flags & SWS_AREA)
852 double srcPixelSize= (1<<16)/(double)xInc;
853 if(d + srcPixelSize/2 < 0.5) coeff= 1.0;
854 else if(d - srcPixelSize/2 < 0.5) coeff= (0.5-d)/srcPixelSize + 0.5;
862 filter[i*filterSize + j]= coeff;
869 /* apply src & dst Filter to filter -> filter2
873 filter2Size= filterSize;
874 if(srcFilter) filter2Size+= srcFilter->length - 1;
875 if(dstFilter) filter2Size+= dstFilter->length - 1;
876 ASSERT(filter2Size>0)
877 filter2= (double*)memalign(8, filter2Size*dstW*sizeof(double));
879 for(i=0; i<dstW; i++)
882 SwsVector scaleFilter;
885 scaleFilter.coeff= filter + i*filterSize;
886 scaleFilter.length= filterSize;
888 if(srcFilter) outVec= getConvVec(srcFilter, &scaleFilter);
889 else outVec= &scaleFilter;
891 ASSERT(outVec->length == filter2Size)
894 for(j=0; j<outVec->length; j++)
896 filter2[i*filter2Size + j]= outVec->coeff[j];
899 (*filterPos)[i]+= (filterSize-1)/2 - (filter2Size-1)/2;
901 if(outVec != &scaleFilter) freeVec(outVec);
903 free(filter); filter=NULL;
905 /* try to reduce the filter-size (step1 find size and shift left) */
906 // Assume its near normalized (*0.5 or *2.0 is ok but * 0.001 is not)
908 for(i=dstW-1; i>=0; i--)
910 int min= filter2Size;
914 /* get rid off near zero elements on the left by shifting left */
915 for(j=0; j<filter2Size; j++)
918 cutOff += ABS(filter2[i*filter2Size]);
920 if(cutOff > SWS_MAX_REDUCE_CUTOFF) break;
922 /* preserve Monotonicity because the core cant handle the filter otherwise */
923 if(i<dstW-1 && (*filterPos)[i] >= (*filterPos)[i+1]) break;
925 // Move filter coeffs left
926 for(k=1; k<filter2Size; k++)
927 filter2[i*filter2Size + k - 1]= filter2[i*filter2Size + k];
928 filter2[i*filter2Size + k - 1]= 0.0;
933 /* count near zeros on the right */
934 for(j=filter2Size-1; j>0; j--)
936 cutOff += ABS(filter2[i*filter2Size + j]);
938 if(cutOff > SWS_MAX_REDUCE_CUTOFF) break;
942 if(min>minFilterSize) minFilterSize= min;
945 ASSERT(minFilterSize > 0)
946 filterSize= (minFilterSize +(filterAlign-1)) & (~(filterAlign-1));
947 ASSERT(filterSize > 0)
948 filter= (double*)memalign(8, filterSize*dstW*sizeof(double));
949 *outFilterSize= filterSize;
951 if(flags&SWS_PRINT_INFO)
952 MSG_INFO("SwScaler: reducing / aligning filtersize %d -> %d\n", filter2Size, filterSize);
953 /* try to reduce the filter-size (step2 reduce it) */
954 for(i=0; i<dstW; i++)
958 for(j=0; j<filterSize; j++)
960 if(j>=filter2Size) filter[i*filterSize + j]= 0.0;
961 else filter[i*filterSize + j]= filter2[i*filter2Size + j];
964 free(filter2); filter2=NULL;
967 //FIXME try to align filterpos if possible
970 for(i=0; i<dstW; i++)
973 if((*filterPos)[i] < 0)
975 // Move filter coeffs left to compensate for filterPos
976 for(j=1; j<filterSize; j++)
978 int left= MAX(j + (*filterPos)[i], 0);
979 filter[i*filterSize + left] += filter[i*filterSize + j];
980 filter[i*filterSize + j]=0;
985 if((*filterPos)[i] + filterSize > srcW)
987 int shift= (*filterPos)[i] + filterSize - srcW;
988 // Move filter coeffs right to compensate for filterPos
989 for(j=filterSize-2; j>=0; j--)
991 int right= MIN(j + shift, filterSize-1);
992 filter[i*filterSize +right] += filter[i*filterSize +j];
993 filter[i*filterSize +j]=0;
995 (*filterPos)[i]= srcW - filterSize;
999 // Note the +1 is for the MMXscaler which reads over the end
1000 *outFilter= (int16_t*)memalign(8, *outFilterSize*(dstW+1)*sizeof(int16_t));
1001 memset(*outFilter, 0, *outFilterSize*(dstW+1)*sizeof(int16_t));
1003 /* Normalize & Store in outFilter */
1004 for(i=0; i<dstW; i++)
1009 for(j=0; j<filterSize; j++)
1011 sum+= filter[i*filterSize + j];
1014 for(j=0; j<filterSize; j++)
1016 (*outFilter)[i*(*outFilterSize) + j]= (int)(filter[i*filterSize + j]*scale);
1020 (*filterPos)[dstW]= (*filterPos)[dstW-1]; // the MMX scaler will read over the end
1021 for(i=0; i<*outFilterSize; i++)
1023 int j= dstW*(*outFilterSize);
1024 (*outFilter)[j + i]= (*outFilter)[j + i - (*outFilterSize)];
1031 static void initMMX2HScaler(int dstW, int xInc, uint8_t *funnyCode, int16_t *filter, int32_t *filterPos, int numSplits)
1036 int fragmentLengthA;
1040 int fragmentLengthB;
1045 // create an optimized horizontal scaling routine
1053 "movq (%%edx, %%eax), %%mm3 \n\t"
1054 "movd (%%ecx, %%esi), %%mm0 \n\t"
1055 "movd 1(%%ecx, %%esi), %%mm1 \n\t"
1056 "punpcklbw %%mm7, %%mm1 \n\t"
1057 "punpcklbw %%mm7, %%mm0 \n\t"
1058 "pshufw $0xFF, %%mm1, %%mm1 \n\t"
1060 "pshufw $0xFF, %%mm0, %%mm0 \n\t"
1062 "psubw %%mm1, %%mm0 \n\t"
1063 "movl 8(%%ebx, %%eax), %%esi \n\t"
1064 "pmullw %%mm3, %%mm0 \n\t"
1065 "psllw $7, %%mm1 \n\t"
1066 "paddw %%mm1, %%mm0 \n\t"
1068 "movq %%mm0, (%%edi, %%eax) \n\t"
1070 "addl $8, %%eax \n\t"
1085 :"=r" (fragmentA), "=r" (imm8OfPShufW1A), "=r" (imm8OfPShufW2A),
1086 "=r" (fragmentLengthA)
1093 "movq (%%edx, %%eax), %%mm3 \n\t"
1094 "movd (%%ecx, %%esi), %%mm0 \n\t"
1095 "punpcklbw %%mm7, %%mm0 \n\t"
1096 "pshufw $0xFF, %%mm0, %%mm1 \n\t"
1098 "pshufw $0xFF, %%mm0, %%mm0 \n\t"
1100 "psubw %%mm1, %%mm0 \n\t"
1101 "movl 8(%%ebx, %%eax), %%esi \n\t"
1102 "pmullw %%mm3, %%mm0 \n\t"
1103 "psllw $7, %%mm1 \n\t"
1104 "paddw %%mm1, %%mm0 \n\t"
1106 "movq %%mm0, (%%edi, %%eax) \n\t"
1108 "addl $8, %%eax \n\t"
1123 :"=r" (fragmentB), "=r" (imm8OfPShufW1B), "=r" (imm8OfPShufW2B),
1124 "=r" (fragmentLengthB)
1127 xpos= 0; //lumXInc/2 - 0x8000; // difference between pixel centers
1130 for(i=0; i<dstW/numSplits; i++)
1137 int b=((xpos+xInc)>>16) - xx;
1138 int c=((xpos+xInc*2)>>16) - xx;
1139 int d=((xpos+xInc*3)>>16) - xx;
1141 filter[i ] = (( xpos & 0xFFFF) ^ 0xFFFF)>>9;
1142 filter[i+1] = (((xpos+xInc ) & 0xFFFF) ^ 0xFFFF)>>9;
1143 filter[i+2] = (((xpos+xInc*2) & 0xFFFF) ^ 0xFFFF)>>9;
1144 filter[i+3] = (((xpos+xInc*3) & 0xFFFF) ^ 0xFFFF)>>9;
1149 int maxShift= 3-(d+1);
1152 memcpy(funnyCode + fragmentPos, fragmentB, fragmentLengthB);
1154 funnyCode[fragmentPos + imm8OfPShufW1B]=
1155 (a+1) | ((b+1)<<2) | ((c+1)<<4) | ((d+1)<<6);
1156 funnyCode[fragmentPos + imm8OfPShufW2B]=
1157 a | (b<<2) | (c<<4) | (d<<6);
1159 if(i+3>=dstW) shift=maxShift; //avoid overread
1160 else if((filterPos[i/2]&3) <= maxShift) shift=filterPos[i/2]&3; //Align
1162 if(shift && i>=shift)
1164 funnyCode[fragmentPos + imm8OfPShufW1B]+= 0x55*shift;
1165 funnyCode[fragmentPos + imm8OfPShufW2B]+= 0x55*shift;
1166 filterPos[i/2]-=shift;
1169 fragmentPos+= fragmentLengthB;
1176 memcpy(funnyCode + fragmentPos, fragmentA, fragmentLengthA);
1178 funnyCode[fragmentPos + imm8OfPShufW1A]=
1179 funnyCode[fragmentPos + imm8OfPShufW2A]=
1180 a | (b<<2) | (c<<4) | (d<<6);
1182 if(i+4>=dstW) shift=maxShift; //avoid overread
1183 else if((filterPos[i/2]&3) <= maxShift) shift=filterPos[i/2]&3; //partial align
1185 if(shift && i>=shift)
1187 funnyCode[fragmentPos + imm8OfPShufW1A]+= 0x55*shift;
1188 funnyCode[fragmentPos + imm8OfPShufW2A]+= 0x55*shift;
1189 filterPos[i/2]-=shift;
1192 fragmentPos+= fragmentLengthA;
1195 funnyCode[fragmentPos]= RET;
1199 filterPos[i/2]= xpos>>16; // needed to jump to the next part
1204 void SwScale_Init(){
1207 static void globalInit(){
1208 // generating tables:
1210 for(i=0; i<768; i++){
1211 int c= MIN(MAX(i-256, 0), 255);
1213 yuvtab_2568[c]= clip_yuvtab_2568[i]=(0x2568*(c-16))+(256<<13);
1214 yuvtab_3343[c]= clip_yuvtab_3343[i]=0x3343*(c-128);
1215 yuvtab_0c92[c]= clip_yuvtab_0c92[i]=-0x0c92*(c-128);
1216 yuvtab_1a1e[c]= clip_yuvtab_1a1e[i]=-0x1a1e*(c-128);
1217 yuvtab_40cf[c]= clip_yuvtab_40cf[i]=0x40cf*(c-128);
1220 for(i=0; i<768; i++)
1222 int v= clip_table[i];
1223 clip_table16b[i]= v>>3;
1224 clip_table16g[i]= (v<<3)&0x07E0;
1225 clip_table16r[i]= (v<<8)&0xF800;
1226 clip_table15b[i]= v>>3;
1227 clip_table15g[i]= (v<<2)&0x03E0;
1228 clip_table15r[i]= (v<<7)&0x7C00;
1233 #ifdef RUNTIME_CPUDETECT
1234 #ifdef CAN_COMPILE_X86_ASM
1235 // ordered per speed fasterst first
1236 if(gCpuCaps.hasMMX2)
1237 swScale= swScale_MMX2;
1238 else if(gCpuCaps.has3DNow)
1239 swScale= swScale_3DNow;
1240 else if(gCpuCaps.hasMMX)
1241 swScale= swScale_MMX;
1247 cpuCaps.hasMMX2 = cpuCaps.hasMMX = cpuCaps.has3DNow = 0;
1249 #else //RUNTIME_CPUDETECT
1251 swScale= swScale_MMX2;
1252 cpuCaps.has3DNow = 0;
1253 #elif defined (HAVE_3DNOW)
1254 swScale= swScale_3DNow;
1255 cpuCaps.hasMMX2 = 0;
1256 #elif defined (HAVE_MMX)
1257 swScale= swScale_MMX;
1258 cpuCaps.hasMMX2 = cpuCaps.has3DNow = 0;
1261 cpuCaps.hasMMX2 = cpuCaps.hasMMX = cpuCaps.has3DNow = 0;
1263 #endif //!RUNTIME_CPUDETECT
1266 static void PlanarToNV12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1267 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1268 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1270 if(dstStride[0]==srcStride[0])
1271 memcpy(dst, src[0], srcSliceH*dstStride[0]);
1275 uint8_t *srcPtr= src[0];
1276 uint8_t *dstPtr= dst;
1277 for(i=0; i<srcSliceH; i++)
1279 memcpy(dstPtr, srcPtr, srcStride[0]);
1280 srcPtr+= srcStride[0];
1281 dstPtr+= dstStride[0];
1284 dst = dstParam[1] + dstStride[1]*srcSliceY;
1285 if(c->srcFormat==IMGFMT_YV12)
1286 interleaveBytes( src[1],src[2],dst,c->srcW,srcSliceH,srcStride[1],srcStride[2],dstStride[0] );
1287 else /* I420 & IYUV */
1288 interleaveBytes( src[2],src[1],dst,c->srcW,srcSliceH,srcStride[2],srcStride[1],dstStride[0] );
1292 /* Warper functions for yuv2bgr */
1293 static void planarYuvToBgr(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1294 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1295 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1297 if(c->srcFormat==IMGFMT_YV12)
1298 yuv2rgb( dst,src[0],src[1],src[2],c->srcW,srcSliceH,dstStride[0],srcStride[0],srcStride[1] );
1299 else /* I420 & IYUV */
1300 yuv2rgb( dst,src[0],src[2],src[1],c->srcW,srcSliceH,dstStride[0],srcStride[0],srcStride[1] );
1303 static void Planar2PackedWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1304 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1305 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1307 if(c->srcFormat==IMGFMT_YV12)
1308 yv12toyuy2( src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0] );
1309 else /* I420 & IYUV */
1310 yv12toyuy2( src[0],src[2],src[1],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0] );
1313 static void bgr24to32Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1314 int srcSliceH, uint8_t* dst[], int dstStride[]){
1316 if(dstStride[0]*3==srcStride[0]*4)
1317 rgb24to32(src[0], dst[0] + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
1321 uint8_t *srcPtr= src[0];
1322 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1324 for(i=0; i<srcSliceH; i++)
1326 rgb24to32(srcPtr, dstPtr, c->srcW*3);
1327 srcPtr+= srcStride[0];
1328 dstPtr+= dstStride[0];
1333 static void bgr24to16Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1334 int srcSliceH, uint8_t* dst[], int dstStride[]){
1336 if(dstStride[0]*3==srcStride[0]*2)
1337 rgb24to16(src[0], dst[0] + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
1341 uint8_t *srcPtr= src[0];
1342 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1344 for(i=0; i<srcSliceH; i++)
1346 rgb24to16(srcPtr, dstPtr, c->srcW*3);
1347 srcPtr+= srcStride[0];
1348 dstPtr+= dstStride[0];
1353 static void bgr24to15Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1354 int srcSliceH, uint8_t* dst[], int dstStride[]){
1356 if(dstStride[0]*3==srcStride[0]*2)
1357 rgb24to15(src[0], dst[0] + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
1361 uint8_t *srcPtr= src[0];
1362 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1364 for(i=0; i<srcSliceH; i++)
1366 rgb24to15(srcPtr, dstPtr, c->srcW*3);
1367 srcPtr+= srcStride[0];
1368 dstPtr+= dstStride[0];
1373 static void bgr32to24Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1374 int srcSliceH, uint8_t* dst[], int dstStride[]){
1376 if(dstStride[0]*4==srcStride[0]*3)
1377 rgb32to24(src[0], dst[0] + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
1381 uint8_t *srcPtr= src[0];
1382 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1384 for(i=0; i<srcSliceH; i++)
1386 rgb32to24(srcPtr, dstPtr, c->srcW<<2);
1387 srcPtr+= srcStride[0];
1388 dstPtr+= dstStride[0];
1393 static void bgr32to16Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1394 int srcSliceH, uint8_t* dst[], int dstStride[]){
1396 if(dstStride[0]*4==srcStride[0]*2)
1397 rgb32to16(src[0], dst[0] + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
1401 uint8_t *srcPtr= src[0];
1402 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1404 for(i=0; i<srcSliceH; i++)
1406 rgb32to16(srcPtr, dstPtr, c->srcW<<2);
1407 srcPtr+= srcStride[0];
1408 dstPtr+= dstStride[0];
1413 static void bgr32to15Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1414 int srcSliceH, uint8_t* dst[], int dstStride[]){
1416 if(dstStride[0]*4==srcStride[0]*2)
1417 rgb32to15(src[0], dst[0] + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
1421 uint8_t *srcPtr= src[0];
1422 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1424 for(i=0; i<srcSliceH; i++)
1426 rgb32to15(srcPtr, dstPtr, c->srcW<<2);
1427 srcPtr+= srcStride[0];
1428 dstPtr+= dstStride[0];
1433 static void bgr15to16Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1434 int srcSliceH, uint8_t* dst[], int dstStride[]){
1436 if(dstStride[0]==srcStride[0])
1437 rgb15to16(src[0], dst[0] + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
1441 uint8_t *srcPtr= src[0];
1442 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1444 for(i=0; i<srcSliceH; i++)
1446 rgb15to16(srcPtr, dstPtr, c->srcW<<1);
1447 srcPtr+= srcStride[0];
1448 dstPtr+= dstStride[0];
1453 static void bgr15to24Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1454 int srcSliceH, uint8_t* dst[], int dstStride[]){
1456 if(dstStride[0]*2==srcStride[0]*3)
1457 rgb15to24(src[0], dst[0] + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
1461 uint8_t *srcPtr= src[0];
1462 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1464 for(i=0; i<srcSliceH; i++)
1466 rgb15to24(srcPtr, dstPtr, c->srcW<<1);
1467 srcPtr+= srcStride[0];
1468 dstPtr+= dstStride[0];
1473 static void bgr15to32Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1474 int srcSliceH, uint8_t* dst[], int dstStride[]){
1476 if(dstStride[0]*2==srcStride[0]*4)
1477 rgb15to32(src[0], dst[0] + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
1481 uint8_t *srcPtr= src[0];
1482 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1484 for(i=0; i<srcSliceH; i++)
1486 rgb15to32(srcPtr, dstPtr, c->srcW<<1);
1487 srcPtr+= srcStride[0];
1488 dstPtr+= dstStride[0];
1493 static void bgr16to24Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1494 int srcSliceH, uint8_t* dst[], int dstStride[]){
1496 if(dstStride[0]*2==srcStride[0]*3)
1497 rgb16to24(src[0], dst[0] + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
1501 uint8_t *srcPtr= src[0];
1502 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1504 for(i=0; i<srcSliceH; i++)
1506 rgb16to24(srcPtr, dstPtr, c->srcW<<1);
1507 srcPtr+= srcStride[0];
1508 dstPtr+= dstStride[0];
1513 static void bgr16to32Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1514 int srcSliceH, uint8_t* dst[], int dstStride[]){
1516 if(dstStride[0]*2==srcStride[0]*4)
1517 rgb16to32(src[0], dst[0] + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
1521 uint8_t *srcPtr= src[0];
1522 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1524 for(i=0; i<srcSliceH; i++)
1526 rgb16to32(srcPtr, dstPtr, c->srcW<<1);
1527 srcPtr+= srcStride[0];
1528 dstPtr+= dstStride[0];
1533 static void bgr24toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1534 int srcSliceH, uint8_t* dst[], int dstStride[]){
1538 dst[0]+ srcSliceY *dstStride[0],
1539 dst[1]+(srcSliceY>>1)*dstStride[1],
1540 dst[2]+(srcSliceY>>1)*dstStride[2],
1542 dstStride[0], dstStride[1], srcStride[0]);
1546 /* unscaled copy like stuff (assumes nearly identical formats) */
1547 static void simpleCopy(SwsContext *c, uint8_t* srcParam[], int srcStrideParam[], int srcSliceY,
1548 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1554 if(isPlanarYUV(c->srcFormat))
1556 if(c->srcFormat == IMGFMT_I420 || c->srcFormat == IMGFMT_IYUV){
1557 src[0]= srcParam[0];
1558 src[1]= srcParam[2];
1559 src[2]= srcParam[1];
1560 srcStride[0]= srcStrideParam[0];
1561 srcStride[1]= srcStrideParam[2];
1562 srcStride[2]= srcStrideParam[1];
1566 src[0]= srcParam[0];
1567 src[1]= srcParam[1];
1568 src[2]= srcParam[2];
1569 srcStride[0]= srcStrideParam[0];
1570 srcStride[1]= srcStrideParam[1];
1571 srcStride[2]= srcStrideParam[2];
1574 else if(isPacked(c->srcFormat) || isGray(c->srcFormat)){
1575 src[0]= srcParam[0];
1578 srcStride[0]= srcStrideParam[0];
1583 if(c->dstFormat == IMGFMT_I420 || c->dstFormat == IMGFMT_IYUV){
1584 dst[0]= dstParam[0];
1585 dst[1]= dstParam[2];
1586 dst[2]= dstParam[1];
1589 dst[0]= dstParam[0];
1590 dst[1]= dstParam[1];
1591 dst[2]= dstParam[2];
1594 if(isPacked(c->srcFormat))
1596 if(dstStride[0]==srcStride[0])
1597 memcpy(dst[0] + dstStride[0]*srcSliceY, src[0], srcSliceH*dstStride[0]);
1601 uint8_t *srcPtr= src[0];
1602 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1605 /* universal length finder */
1606 while(length+c->srcW <= ABS(dstStride[0])
1607 && length+c->srcW <= ABS(srcStride[0])) length+= c->srcW;
1610 for(i=0; i<srcSliceH; i++)
1612 memcpy(dstPtr, srcPtr, length);
1613 srcPtr+= srcStride[0];
1614 dstPtr+= dstStride[0];
1621 for(plane=0; plane<3; plane++)
1626 if(c->srcFormat == IMGFMT_YVU9 || c->srcFormat == IMGFMT_IF09)
1628 length= plane==0 ? c->srcW : ((c->srcW+1)>>2);
1629 y= plane==0 ? srcSliceY: ((srcSliceY+1)>>2);
1630 height= plane==0 ? srcSliceH: ((srcSliceH+1)>>2);
1634 length= plane==0 ? c->srcW : ((c->srcW+1)>>1);
1635 y= plane==0 ? srcSliceY: ((srcSliceY+1)>>1);
1636 height= plane==0 ? srcSliceH: ((srcSliceH+1)>>1);
1639 if(dstStride[plane]==srcStride[plane])
1640 memcpy(dst[plane] + dstStride[plane]*y, src[plane], height*dstStride[plane]);
1644 uint8_t *srcPtr= src[plane];
1645 uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
1646 for(i=0; i<height; i++)
1648 memcpy(dstPtr, srcPtr, length);
1649 srcPtr+= srcStride[plane];
1650 dstPtr+= dstStride[plane];
1657 static uint32_t remove_dup_fourcc(uint32_t fourcc)
1661 case IMGFMT_IYUV: return IMGFMT_I420;
1662 case IMGFMT_Y8 : return IMGFMT_Y800;
1663 default: return fourcc;
1667 SwsContext *getSwsContext(int srcW, int srcH, int srcFormat, int dstW, int dstH, int dstFormat, int flags,
1668 SwsFilter *srcFilter, SwsFilter *dstFilter){
1673 int simple_copy, unscaled_copy;
1674 SwsFilter dummyFilter= {NULL, NULL, NULL, NULL};
1678 asm volatile("emms\n\t"::: "memory");
1681 if(swScale==NULL) globalInit();
1683 /* avoid dupplicate Formats, so we dont need to check to much */
1684 srcFormat = remove_dup_fourcc(srcFormat);
1685 dstFormat = remove_dup_fourcc(dstFormat);
1686 /* don't refuse this beauty */
1687 unscaled_copy = (srcW == dstW && srcH == dstH);
1688 simple_copy = (srcW == dstW && srcH == dstH && srcFormat == dstFormat);
1693 if(!isSupportedUnscaledIn(srcFormat))
1695 MSG_ERR("swScaler: %s is not supported as input format\n", vo_format_name(srcFormat));
1698 if(!isSupportedUnscaledOut(dstFormat))
1700 MSG_ERR("swScaler: %s is not supported as output format\n", vo_format_name(dstFormat));
1706 if(!isSupportedIn(srcFormat))
1708 MSG_ERR("swScaler: %s is not supported as input format\n", vo_format_name(srcFormat));
1711 if(!isSupportedOut(dstFormat))
1713 MSG_ERR("swScaler: %s is not supported as output format\n", vo_format_name(dstFormat));
1719 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
1721 MSG_ERR("swScaler: %dx%d -> %dx%d is invalid scaling dimension\n",
1722 srcW, srcH, dstW, dstH);
1726 if(!dstFilter) dstFilter= &dummyFilter;
1727 if(!srcFilter) srcFilter= &dummyFilter;
1729 c= memalign(64, sizeof(SwsContext));
1730 memset(c, 0, sizeof(SwsContext));
1736 c->lumXInc= ((srcW<<16) + (dstW>>1))/dstW;
1737 c->lumYInc= ((srcH<<16) + (dstH>>1))/dstH;
1739 c->dstFormat= dstFormat;
1740 c->srcFormat= srcFormat;
1743 if(dstFilter->lumV!=NULL && dstFilter->lumV->length>1) usesFilter=1;
1744 if(dstFilter->lumH!=NULL && dstFilter->lumH->length>1) usesFilter=1;
1745 if(dstFilter->chrV!=NULL && dstFilter->chrV->length>1) usesFilter=1;
1746 if(dstFilter->chrH!=NULL && dstFilter->chrH->length>1) usesFilter=1;
1747 if(srcFilter->lumV!=NULL && srcFilter->lumV->length>1) usesFilter=1;
1748 if(srcFilter->lumH!=NULL && srcFilter->lumH->length>1) usesFilter=1;
1749 if(srcFilter->chrV!=NULL && srcFilter->chrV->length>1) usesFilter=1;
1750 if(srcFilter->chrH!=NULL && srcFilter->chrH->length>1) usesFilter=1;
1752 /* unscaled special Cases */
1753 if(srcW==dstW && srcH==dstH && !usesFilter)
1756 if((srcFormat == IMGFMT_YV12||srcFormat==IMGFMT_I420)&&dstFormat == IMGFMT_NV12)
1758 c->swScale= PlanarToNV12Wrapper;
1760 if(flags&SWS_PRINT_INFO)
1761 MSG_INFO("SwScaler: using unscaled %s -> %s special converter\n",
1762 vo_format_name(srcFormat), vo_format_name(dstFormat));
1766 if((srcFormat == IMGFMT_YV12||srcFormat==IMGFMT_I420)&&dstFormat == IMGFMT_YUY2)
1768 c->swScale= Planar2PackedWrapper;
1770 if(flags&SWS_PRINT_INFO)
1771 MSG_INFO("SwScaler: using unscaled %s -> %s special converter\n",
1772 vo_format_name(srcFormat), vo_format_name(dstFormat));
1776 if(isPlanarYUV(srcFormat) && isBGR(dstFormat))
1778 // FIXME multiple yuv2rgb converters wont work that way cuz that thing is full of globals&statics
1779 #ifdef WORDS_BIGENDIAN
1780 if(dstFormat==IMGFMT_BGR32)
1781 yuv2rgb_init( dstFormat&0xFF /* =bpp */, MODE_BGR);
1783 yuv2rgb_init( dstFormat&0xFF /* =bpp */, MODE_RGB);
1785 yuv2rgb_init( dstFormat&0xFF /* =bpp */, MODE_RGB);
1787 c->swScale= planarYuvToBgr;
1789 if(flags&SWS_PRINT_INFO)
1790 MSG_INFO("SwScaler: using unscaled %s -> %s special converter\n",
1791 vo_format_name(srcFormat), vo_format_name(dstFormat));
1796 if(srcFormat == dstFormat || (isPlanarYUV(srcFormat) && isPlanarYUV(dstFormat)))
1798 c->swScale= simpleCopy;
1800 if(flags&SWS_PRINT_INFO)
1801 MSG_INFO("SwScaler: using unscaled %s -> %s special converter\n",
1802 vo_format_name(srcFormat), vo_format_name(dstFormat));
1806 /* bgr32to24 & rgb32to24*/
1807 if((srcFormat==IMGFMT_BGR32 && dstFormat==IMGFMT_BGR24)
1808 ||(srcFormat==IMGFMT_RGB32 && dstFormat==IMGFMT_RGB24))
1810 c->swScale= bgr32to24Wrapper;
1812 if(flags&SWS_PRINT_INFO)
1813 MSG_INFO("SwScaler: using unscaled %s -> %s special converter\n",
1814 vo_format_name(srcFormat), vo_format_name(dstFormat));
1818 /* bgr32to16 & rgb32to16*/
1819 if((srcFormat==IMGFMT_BGR32 && dstFormat==IMGFMT_BGR16)
1820 ||(srcFormat==IMGFMT_RGB32 && dstFormat==IMGFMT_RGB16))
1822 c->swScale= bgr32to16Wrapper;
1824 if(flags&SWS_PRINT_INFO)
1825 MSG_INFO("SwScaler: using unscaled %s -> %s special converter\n",
1826 vo_format_name(srcFormat), vo_format_name(dstFormat));
1830 /* bgr32to15 & rgb32to15*/
1831 if((srcFormat==IMGFMT_BGR32 && dstFormat==IMGFMT_BGR15)
1832 ||(srcFormat==IMGFMT_RGB32 && dstFormat==IMGFMT_RGB15))
1834 c->swScale= bgr32to15Wrapper;
1836 if(flags&SWS_PRINT_INFO)
1837 MSG_INFO("SwScaler: using unscaled %s -> %s special converter\n",
1838 vo_format_name(srcFormat), vo_format_name(dstFormat));
1842 /* bgr24to32 & rgb24to32*/
1843 if((srcFormat==IMGFMT_BGR24 && dstFormat==IMGFMT_BGR32)
1844 ||(srcFormat==IMGFMT_RGB24 && dstFormat==IMGFMT_RGB32))
1846 c->swScale= bgr24to32Wrapper;
1848 if(flags&SWS_PRINT_INFO)
1849 MSG_INFO("SwScaler: using unscaled %s -> %s special converter\n",
1850 vo_format_name(srcFormat), vo_format_name(dstFormat));
1854 /* bgr24to16 & rgb24to16*/
1855 if((srcFormat==IMGFMT_BGR24 && dstFormat==IMGFMT_BGR16)
1856 ||(srcFormat==IMGFMT_RGB24 && dstFormat==IMGFMT_RGB16))
1858 c->swScale= bgr24to16Wrapper;
1860 if(flags&SWS_PRINT_INFO)
1861 MSG_INFO("SwScaler: using unscaled %s -> %s special converter\n",
1862 vo_format_name(srcFormat), vo_format_name(dstFormat));
1866 /* bgr24to15 & rgb24to15*/
1867 if((srcFormat==IMGFMT_BGR24 && dstFormat==IMGFMT_BGR15)
1868 ||(srcFormat==IMGFMT_RGB24 && dstFormat==IMGFMT_RGB15))
1870 c->swScale= bgr24to15Wrapper;
1872 if(flags&SWS_PRINT_INFO)
1873 MSG_INFO("SwScaler: using unscaled %s -> %s special converter\n",
1874 vo_format_name(srcFormat), vo_format_name(dstFormat));
1879 if(srcFormat==IMGFMT_BGR15 && dstFormat==IMGFMT_BGR16)
1881 c->swScale= bgr15to16Wrapper;
1883 if(flags&SWS_PRINT_INFO)
1884 MSG_INFO("SwScaler: using unscaled %s -> %s special converter\n",
1885 vo_format_name(srcFormat), vo_format_name(dstFormat));
1890 if((srcFormat==IMGFMT_BGR15 && dstFormat==IMGFMT_BGR24)
1891 ||(srcFormat==IMGFMT_RGB15 && dstFormat==IMGFMT_RGB24))
1893 c->swScale= bgr15to24Wrapper;
1895 if(flags&SWS_PRINT_INFO)
1896 MSG_INFO("SwScaler: using unscaled %s -> %s special converter\n",
1897 vo_format_name(srcFormat), vo_format_name(dstFormat));
1902 if((srcFormat==IMGFMT_BGR15 && dstFormat==IMGFMT_BGR32)
1903 ||(srcFormat==IMGFMT_RGB15 && dstFormat==IMGFMT_RGB32))
1905 c->swScale= bgr15to32Wrapper;
1907 if(flags&SWS_PRINT_INFO)
1908 MSG_INFO("SwScaler: using unscaled %s -> %s special converter\n",
1909 vo_format_name(srcFormat), vo_format_name(dstFormat));
1914 if((srcFormat==IMGFMT_BGR16 && dstFormat==IMGFMT_BGR24)
1915 ||(srcFormat==IMGFMT_RGB16 && dstFormat==IMGFMT_RGB24))
1917 c->swScale= bgr16to24Wrapper;
1919 if(flags&SWS_PRINT_INFO)
1920 MSG_INFO("SwScaler: using unscaled %s -> %s special converter\n",
1921 vo_format_name(srcFormat), vo_format_name(dstFormat));
1926 if((srcFormat==IMGFMT_BGR16 && dstFormat==IMGFMT_BGR32)
1927 ||(srcFormat==IMGFMT_RGB16 && dstFormat==IMGFMT_RGB32))
1929 c->swScale= bgr16to32Wrapper;
1931 if(flags&SWS_PRINT_INFO)
1932 MSG_INFO("SwScaler: using unscaled %s -> %s special converter\n",
1933 vo_format_name(srcFormat), vo_format_name(dstFormat));
1938 if(srcFormat==IMGFMT_BGR24 && dstFormat==IMGFMT_YV12)
1940 c->swScale= bgr24toyv12Wrapper;
1942 if(flags&SWS_PRINT_INFO)
1943 MSG_INFO("SwScaler: using unscaled %s -> %s special converter\n",
1944 vo_format_name(srcFormat), vo_format_name(dstFormat));
1951 c->canMMX2BeUsed= (dstW >=srcW && (dstW&31)==0 && (srcW&15)==0) ? 1 : 0;
1952 if(!c->canMMX2BeUsed && dstW >=srcW && (srcW&15)==0 && (flags&SWS_FAST_BILINEAR))
1954 if(flags&SWS_PRINT_INFO)
1955 MSG_INFO("SwScaler: output Width is not a multiple of 32 -> no MMX2 scaler\n");
1962 /* dont use full vertical UV input/internaly if the source doesnt even have it */
1963 if(isHalfChrV(srcFormat)) c->flags= flags= flags&(~SWS_FULL_CHR_V);
1964 /* dont use full horizontal UV input if the source doesnt even have it */
1965 if(isHalfChrH(srcFormat)) c->flags= flags= flags&(~SWS_FULL_CHR_H_INP);
1966 /* dont use full horizontal UV internally if the destination doesnt even have it */
1967 if(isHalfChrH(dstFormat)) c->flags= flags= flags&(~SWS_FULL_CHR_H_INT);
1969 if(flags&SWS_FULL_CHR_H_INP) c->chrSrcW= srcW;
1970 else c->chrSrcW= (srcW+1)>>1;
1972 if(flags&SWS_FULL_CHR_H_INT) c->chrDstW= dstW;
1973 else c->chrDstW= (dstW+1)>>1;
1975 if(flags&SWS_FULL_CHR_V) c->chrSrcH= srcH;
1976 else c->chrSrcH= (srcH+1)>>1;
1978 if(isHalfChrV(dstFormat)) c->chrDstH= (dstH+1)>>1;
1979 else c->chrDstH= dstH;
1981 c->chrXInc= ((c->chrSrcW<<16) + (c->chrDstW>>1))/c->chrDstW;
1982 c->chrYInc= ((c->chrSrcH<<16) + (c->chrDstH>>1))/c->chrDstH;
1985 // match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src to pixel n-2 of dst
1986 // but only for the FAST_BILINEAR mode otherwise do correct scaling
1987 // n-2 is the last chrominance sample available
1988 // this is not perfect, but noone shuld notice the difference, the more correct variant
1989 // would be like the vertical one, but that would require some special code for the
1990 // first and last pixel
1991 if(flags&SWS_FAST_BILINEAR)
1993 if(c->canMMX2BeUsed)
1998 //we dont use the x86asm scaler if mmx is available
1999 else if(cpuCaps.hasMMX)
2001 c->lumXInc = ((srcW-2)<<16)/(dstW-2) - 20;
2002 c->chrXInc = ((c->chrSrcW-2)<<16)/(c->chrDstW-2) - 20;
2006 /* precalculate horizontal scaler filter coefficients */
2008 const int filterAlign= cpuCaps.hasMMX ? 4 : 1;
2010 initFilter(&c->hLumFilter, &c->hLumFilterPos, &c->hLumFilterSize, c->lumXInc,
2011 srcW , dstW, filterAlign, 1<<14, flags,
2012 srcFilter->lumH, dstFilter->lumH);
2013 initFilter(&c->hChrFilter, &c->hChrFilterPos, &c->hChrFilterSize, c->chrXInc,
2014 (srcW+1)>>1, c->chrDstW, filterAlign, 1<<14, flags,
2015 srcFilter->chrH, dstFilter->chrH);
2018 // cant downscale !!!
2019 if(c->canMMX2BeUsed && (flags & SWS_FAST_BILINEAR))
2021 c->lumMmx2Filter = (int16_t*)memalign(8, (dstW /8+8)*sizeof(int16_t));
2022 c->chrMmx2Filter = (int16_t*)memalign(8, (c->chrDstW /4+8)*sizeof(int16_t));
2023 c->lumMmx2FilterPos= (int32_t*)memalign(8, (dstW /2/8+8)*sizeof(int32_t));
2024 c->chrMmx2FilterPos= (int32_t*)memalign(8, (c->chrDstW/2/4+8)*sizeof(int32_t));
2026 initMMX2HScaler( dstW, c->lumXInc, c->funnyYCode , c->lumMmx2Filter, c->lumMmx2FilterPos, 8);
2027 initMMX2HScaler(c->chrDstW, c->chrXInc, c->funnyUVCode, c->chrMmx2Filter, c->chrMmx2FilterPos, 4);
2030 } // Init Horizontal stuff
2034 /* precalculate vertical scaler filter coefficients */
2035 initFilter(&c->vLumFilter, &c->vLumFilterPos, &c->vLumFilterSize, c->lumYInc,
2036 srcH , dstH, 1, (1<<12)-4, flags,
2037 srcFilter->lumV, dstFilter->lumV);
2038 initFilter(&c->vChrFilter, &c->vChrFilterPos, &c->vChrFilterSize, c->chrYInc,
2039 (srcH+1)>>1, c->chrDstH, 1, (1<<12)-4, flags,
2040 srcFilter->chrV, dstFilter->chrV);
2042 // Calculate Buffer Sizes so that they wont run out while handling these damn slices
2043 c->vLumBufSize= c->vLumFilterSize;
2044 c->vChrBufSize= c->vChrFilterSize;
2045 for(i=0; i<dstH; i++)
2047 int chrI= i*c->chrDstH / dstH;
2048 int nextSlice= MAX(c->vLumFilterPos[i ] + c->vLumFilterSize - 1,
2049 ((c->vChrFilterPos[chrI] + c->vChrFilterSize - 1)<<1));
2050 nextSlice&= ~1; // Slices start at even boundaries
2051 if(c->vLumFilterPos[i ] + c->vLumBufSize < nextSlice)
2052 c->vLumBufSize= nextSlice - c->vLumFilterPos[i ];
2053 if(c->vChrFilterPos[chrI] + c->vChrBufSize < (nextSlice>>1))
2054 c->vChrBufSize= (nextSlice>>1) - c->vChrFilterPos[chrI];
2057 // allocate pixbufs (we use dynamic allocation because otherwise we would need to
2058 c->lumPixBuf= (int16_t**)memalign(4, c->vLumBufSize*2*sizeof(int16_t*));
2059 c->chrPixBuf= (int16_t**)memalign(4, c->vChrBufSize*2*sizeof(int16_t*));
2060 //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)
2061 for(i=0; i<c->vLumBufSize; i++)
2062 c->lumPixBuf[i]= c->lumPixBuf[i+c->vLumBufSize]= (uint16_t*)memalign(8, 4000);
2063 for(i=0; i<c->vChrBufSize; i++)
2064 c->chrPixBuf[i]= c->chrPixBuf[i+c->vChrBufSize]= (uint16_t*)memalign(8, 8000);
2066 //try to avoid drawing green stuff between the right end and the stride end
2067 for(i=0; i<c->vLumBufSize; i++) memset(c->lumPixBuf[i], 0, 4000);
2068 for(i=0; i<c->vChrBufSize; i++) memset(c->chrPixBuf[i], 64, 8000);
2070 ASSERT(c->chrDstH <= dstH)
2072 // pack filter data for mmx code
2075 c->lumMmxFilter= (int16_t*)memalign(8, c->vLumFilterSize* dstH*4*sizeof(int16_t));
2076 c->chrMmxFilter= (int16_t*)memalign(8, c->vChrFilterSize*c->chrDstH*4*sizeof(int16_t));
2077 for(i=0; i<c->vLumFilterSize*dstH; i++)
2078 c->lumMmxFilter[4*i]=c->lumMmxFilter[4*i+1]=c->lumMmxFilter[4*i+2]=c->lumMmxFilter[4*i+3]=
2080 for(i=0; i<c->vChrFilterSize*c->chrDstH; i++)
2081 c->chrMmxFilter[4*i]=c->chrMmxFilter[4*i+1]=c->chrMmxFilter[4*i+2]=c->chrMmxFilter[4*i+3]=
2085 if(flags&SWS_PRINT_INFO)
2088 char *dither= " dithered";
2092 if(flags&SWS_FAST_BILINEAR)
2093 MSG_INFO("\nSwScaler: FAST_BILINEAR scaler, ");
2094 else if(flags&SWS_BILINEAR)
2095 MSG_INFO("\nSwScaler: BILINEAR scaler, ");
2096 else if(flags&SWS_BICUBIC)
2097 MSG_INFO("\nSwScaler: BICUBIC scaler, ");
2098 else if(flags&SWS_X)
2099 MSG_INFO("\nSwScaler: Experimental scaler, ");
2100 else if(flags&SWS_POINT)
2101 MSG_INFO("\nSwScaler: Nearest Neighbor / POINT scaler, ");
2102 else if(flags&SWS_AREA)
2103 MSG_INFO("\nSwScaler: Area Averageing scaler, ");
2105 MSG_INFO("\nSwScaler: ehh flags invalid?! ");
2107 if(dstFormat==IMGFMT_BGR15 || dstFormat==IMGFMT_BGR16)
2108 MSG_INFO("from %s to%s %s ",
2109 vo_format_name(srcFormat), dither, vo_format_name(dstFormat));
2111 MSG_INFO("from %s to %s ",
2112 vo_format_name(srcFormat), vo_format_name(dstFormat));
2115 MSG_INFO("using MMX2\n");
2116 else if(cpuCaps.has3DNow)
2117 MSG_INFO("using 3DNOW\n");
2118 else if(cpuCaps.hasMMX)
2119 MSG_INFO("using MMX\n");
2121 MSG_INFO("using C\n");
2124 if((flags & SWS_PRINT_INFO) && verbose)
2128 if(c->canMMX2BeUsed && (flags&SWS_FAST_BILINEAR))
2129 MSG_V("SwScaler: using FAST_BILINEAR MMX2 scaler for horizontal scaling\n");
2132 if(c->hLumFilterSize==4)
2133 MSG_V("SwScaler: using 4-tap MMX scaler for horizontal luminance scaling\n");
2134 else if(c->hLumFilterSize==8)
2135 MSG_V("SwScaler: using 8-tap MMX scaler for horizontal luminance scaling\n");
2137 MSG_V("SwScaler: using n-tap MMX scaler for horizontal luminance scaling\n");
2139 if(c->hChrFilterSize==4)
2140 MSG_V("SwScaler: using 4-tap MMX scaler for horizontal chrominance scaling\n");
2141 else if(c->hChrFilterSize==8)
2142 MSG_V("SwScaler: using 8-tap MMX scaler for horizontal chrominance scaling\n");
2144 MSG_V("SwScaler: using n-tap MMX scaler for horizontal chrominance scaling\n");
2150 MSG_V("SwScaler: using X86-Asm scaler for horizontal scaling\n");
2152 if(flags & SWS_FAST_BILINEAR)
2153 MSG_V("SwScaler: using FAST_BILINEAR C scaler for horizontal scaling\n");
2155 MSG_V("SwScaler: using C scaler for horizontal scaling\n");
2158 if(isPlanarYUV(dstFormat))
2160 if(c->vLumFilterSize==1)
2161 MSG_V("SwScaler: using 1-tap %s \"scaler\" for vertical scaling (YV12 like)\n", cpuCaps.hasMMX ? "MMX" : "C");
2163 MSG_V("SwScaler: using n-tap %s scaler for vertical scaling (YV12 like)\n", cpuCaps.hasMMX ? "MMX" : "C");
2167 if(c->vLumFilterSize==1 && c->vChrFilterSize==2)
2168 MSG_V("SwScaler: using 1-tap %s \"scaler\" for vertical luminance scaling (BGR)\n"
2169 "SwScaler: 2-tap scaler for vertical chrominance scaling (BGR)\n",cpuCaps.hasMMX ? "MMX" : "C");
2170 else if(c->vLumFilterSize==2 && c->vChrFilterSize==2)
2171 MSG_V("SwScaler: using 2-tap linear %s scaler for vertical scaling (BGR)\n", cpuCaps.hasMMX ? "MMX" : "C");
2173 MSG_V("SwScaler: using n-tap %s scaler for vertical scaling (BGR)\n", cpuCaps.hasMMX ? "MMX" : "C");
2176 if(dstFormat==IMGFMT_BGR24)
2177 MSG_V("SwScaler: using %s YV12->BGR24 Converter\n",
2178 cpuCaps.hasMMX2 ? "MMX2" : (cpuCaps.hasMMX ? "MMX" : "C"));
2179 else if(dstFormat==IMGFMT_BGR32)
2180 MSG_V("SwScaler: using %s YV12->BGR32 Converter\n", cpuCaps.hasMMX ? "MMX" : "C");
2181 else if(dstFormat==IMGFMT_BGR16)
2182 MSG_V("SwScaler: using %s YV12->BGR16 Converter\n", cpuCaps.hasMMX ? "MMX" : "C");
2183 else if(dstFormat==IMGFMT_BGR15)
2184 MSG_V("SwScaler: using %s YV12->BGR15 Converter\n", cpuCaps.hasMMX ? "MMX" : "C");
2186 MSG_V("SwScaler: %dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
2188 if((flags & SWS_PRINT_INFO) && verbose>1)
2190 MSG_DBG2("SwScaler:Lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
2191 c->srcW, c->srcH, c->dstW, c->dstH, c->lumXInc, c->lumYInc);
2192 MSG_DBG2("SwScaler:Chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
2193 c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH, c->chrXInc, c->chrYInc);
2196 c->swScale= swScale;
2201 * returns a normalized gaussian curve used to filter stuff
2202 * quality=3 is high quality, lowwer is lowwer quality
2205 SwsVector *getGaussianVec(double variance, double quality){
2206 const int length= (int)(variance*quality + 0.5) | 1;
2208 double *coeff= memalign(sizeof(double), length*sizeof(double));
2209 double middle= (length-1)*0.5;
2210 SwsVector *vec= malloc(sizeof(SwsVector));
2213 vec->length= length;
2215 for(i=0; i<length; i++)
2217 double dist= i-middle;
2218 coeff[i]= exp( -dist*dist/(2*variance*variance) ) / sqrt(2*variance*PI);
2221 normalizeVec(vec, 1.0);
2226 SwsVector *getConstVec(double c, int length){
2228 double *coeff= memalign(sizeof(double), length*sizeof(double));
2229 SwsVector *vec= malloc(sizeof(SwsVector));
2232 vec->length= length;
2234 for(i=0; i<length; i++)
2241 SwsVector *getIdentityVec(void){
2242 double *coeff= memalign(sizeof(double), sizeof(double));
2243 SwsVector *vec= malloc(sizeof(SwsVector));
2252 void normalizeVec(SwsVector *a, double height){
2257 for(i=0; i<a->length; i++)
2262 for(i=0; i<a->length; i++)
2263 a->coeff[i]*= height;
2266 void scaleVec(SwsVector *a, double scalar){
2269 for(i=0; i<a->length; i++)
2270 a->coeff[i]*= scalar;
2273 static SwsVector *getConvVec(SwsVector *a, SwsVector *b){
2274 int length= a->length + b->length - 1;
2275 double *coeff= memalign(sizeof(double), length*sizeof(double));
2277 SwsVector *vec= malloc(sizeof(SwsVector));
2280 vec->length= length;
2282 for(i=0; i<length; i++) coeff[i]= 0.0;
2284 for(i=0; i<a->length; i++)
2286 for(j=0; j<b->length; j++)
2288 coeff[i+j]+= a->coeff[i]*b->coeff[j];
2295 static SwsVector *sumVec(SwsVector *a, SwsVector *b){
2296 int length= MAX(a->length, b->length);
2297 double *coeff= memalign(sizeof(double), length*sizeof(double));
2299 SwsVector *vec= malloc(sizeof(SwsVector));
2302 vec->length= length;
2304 for(i=0; i<length; i++) coeff[i]= 0.0;
2306 for(i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
2307 for(i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]+= b->coeff[i];
2312 static SwsVector *diffVec(SwsVector *a, SwsVector *b){
2313 int length= MAX(a->length, b->length);
2314 double *coeff= memalign(sizeof(double), length*sizeof(double));
2316 SwsVector *vec= malloc(sizeof(SwsVector));
2319 vec->length= length;
2321 for(i=0; i<length; i++) coeff[i]= 0.0;
2323 for(i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
2324 for(i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]-= b->coeff[i];
2329 /* shift left / or right if "shift" is negative */
2330 static SwsVector *getShiftedVec(SwsVector *a, int shift){
2331 int length= a->length + ABS(shift)*2;
2332 double *coeff= memalign(sizeof(double), length*sizeof(double));
2334 SwsVector *vec= malloc(sizeof(SwsVector));
2337 vec->length= length;
2339 for(i=0; i<length; i++) coeff[i]= 0.0;
2341 for(i=0; i<a->length; i++)
2343 coeff[i + (length-1)/2 - (a->length-1)/2 - shift]= a->coeff[i];
2349 void shiftVec(SwsVector *a, int shift){
2350 SwsVector *shifted= getShiftedVec(a, shift);
2352 a->coeff= shifted->coeff;
2353 a->length= shifted->length;
2357 void addVec(SwsVector *a, SwsVector *b){
2358 SwsVector *sum= sumVec(a, b);
2360 a->coeff= sum->coeff;
2361 a->length= sum->length;
2365 void subVec(SwsVector *a, SwsVector *b){
2366 SwsVector *diff= diffVec(a, b);
2368 a->coeff= diff->coeff;
2369 a->length= diff->length;
2373 void convVec(SwsVector *a, SwsVector *b){
2374 SwsVector *conv= getConvVec(a, b);
2376 a->coeff= conv->coeff;
2377 a->length= conv->length;
2381 SwsVector *cloneVec(SwsVector *a){
2382 double *coeff= memalign(sizeof(double), a->length*sizeof(double));
2384 SwsVector *vec= malloc(sizeof(SwsVector));
2387 vec->length= a->length;
2389 for(i=0; i<a->length; i++) coeff[i]= a->coeff[i];
2394 void printVec(SwsVector *a){
2400 for(i=0; i<a->length; i++)
2401 if(a->coeff[i]>max) max= a->coeff[i];
2403 for(i=0; i<a->length; i++)
2404 if(a->coeff[i]<min) min= a->coeff[i];
2408 for(i=0; i<a->length; i++)
2410 int x= (int)((a->coeff[i]-min)*60.0/range +0.5);
2411 MSG_DBG2("%1.3f ", a->coeff[i]);
2412 for(;x>0; x--) MSG_DBG2(" ");
2417 void freeVec(SwsVector *a){
2419 if(a->coeff) free(a->coeff);
2425 void freeSwsContext(SwsContext *c){
2432 for(i=0; i<c->vLumBufSize; i++)
2434 if(c->lumPixBuf[i]) free(c->lumPixBuf[i]);
2435 c->lumPixBuf[i]=NULL;
2443 for(i=0; i<c->vChrBufSize; i++)
2445 if(c->chrPixBuf[i]) free(c->chrPixBuf[i]);
2446 c->chrPixBuf[i]=NULL;
2452 if(c->vLumFilter) free(c->vLumFilter);
2453 c->vLumFilter = NULL;
2454 if(c->vChrFilter) free(c->vChrFilter);
2455 c->vChrFilter = NULL;
2456 if(c->hLumFilter) free(c->hLumFilter);
2457 c->hLumFilter = NULL;
2458 if(c->hChrFilter) free(c->hChrFilter);
2459 c->hChrFilter = NULL;
2461 if(c->vLumFilterPos) free(c->vLumFilterPos);
2462 c->vLumFilterPos = NULL;
2463 if(c->vChrFilterPos) free(c->vChrFilterPos);
2464 c->vChrFilterPos = NULL;
2465 if(c->hLumFilterPos) free(c->hLumFilterPos);
2466 c->hLumFilterPos = NULL;
2467 if(c->hChrFilterPos) free(c->hChrFilterPos);
2468 c->hChrFilterPos = NULL;
2470 if(c->lumMmxFilter) free(c->lumMmxFilter);
2471 c->lumMmxFilter = NULL;
2472 if(c->chrMmxFilter) free(c->chrMmxFilter);
2473 c->chrMmxFilter = NULL;
2475 if(c->lumMmx2Filter) free(c->lumMmx2Filter);
2476 c->lumMmx2Filter=NULL;
2477 if(c->chrMmx2Filter) free(c->chrMmx2Filter);
2478 c->chrMmx2Filter=NULL;
2479 if(c->lumMmx2FilterPos) free(c->lumMmx2FilterPos);
2480 c->lumMmx2FilterPos=NULL;
2481 if(c->chrMmx2FilterPos) free(c->chrMmx2FilterPos);
2482 c->chrMmx2FilterPos=NULL;