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
39 untested special converters
40 YV12/I420/IYUV -> BGR15/BGR24/BGR32 (its the yuv2rgb stuff, so it should be ok)
41 YV12/I420/IYUV -> YV12/I420/IYUV
42 YUY2/BGR15/BGR16/BGR24/BGR32/RGB24/RGB32 -> same format
43 BGR24 -> BGR32 & RGB24 -> RGB32
44 BGR32 -> BGR24 & RGB32 -> RGB24
51 #include "../config.h"
52 #include "../mangle.h"
57 #include "../cpudetect.h"
59 #include "../libvo/img_format.h"
61 #include "../libvo/fastmemcpy.h"
71 #define RET 0xC3 //near return opcode for X86
74 #define ASSERT(x) if(!(x)) { printf("ASSERT " #x " failed\n"); *((int*)0)=0; }
82 #define PI 3.14159265358979323846
85 //FIXME replace this with something faster
86 #define isPlanarYUV(x) ((x)==IMGFMT_YV12 || (x)==IMGFMT_I420)
87 #define isYUV(x) ((x)==IMGFMT_YUY2 || isPlanarYUV(x))
88 #define isHalfChrV(x) ((x)==IMGFMT_YV12 || (x)==IMGFMT_I420)
89 #define isHalfChrH(x) ((x)==IMGFMT_YUY2 || (x)==IMGFMT_YV12 || (x)==IMGFMT_I420)
90 #define isPacked(x) ((x)==IMGFMT_YUY2 || ((x)&IMGFMT_BGR_MASK)==IMGFMT_BGR || ((x)&IMGFMT_RGB_MASK)==IMGFMT_RGB)
91 #define isGray(x) ((x)==IMGFMT_Y800)
92 #define isSupportedIn(x) ((x)==IMGFMT_YV12 || (x)==IMGFMT_I420 || (x)==IMGFMT_YUY2 \
93 || (x)==IMGFMT_BGR32|| (x)==IMGFMT_BGR24|| (x)==IMGFMT_BGR16|| (x)==IMGFMT_BGR15\
94 || (x)==IMGFMT_RGB32|| (x)==IMGFMT_RGB24\
96 #define isSupportedOut(x) ((x)==IMGFMT_YV12 || (x)==IMGFMT_I420 \
97 || (x)==IMGFMT_BGR32|| (x)==IMGFMT_BGR24|| (x)==IMGFMT_BGR16|| (x)==IMGFMT_BGR15)
98 #define isBGR(x) ((x)==IMGFMT_BGR32|| (x)==IMGFMT_BGR24|| (x)==IMGFMT_BGR16|| (x)==IMGFMT_BGR15)
100 #define RGB2YUV_SHIFT 16
101 #define BY ((int)( 0.098*(1<<RGB2YUV_SHIFT)+0.5))
102 #define BV ((int)(-0.071*(1<<RGB2YUV_SHIFT)+0.5))
103 #define BU ((int)( 0.439*(1<<RGB2YUV_SHIFT)+0.5))
104 #define GY ((int)( 0.504*(1<<RGB2YUV_SHIFT)+0.5))
105 #define GV ((int)(-0.368*(1<<RGB2YUV_SHIFT)+0.5))
106 #define GU ((int)(-0.291*(1<<RGB2YUV_SHIFT)+0.5))
107 #define RY ((int)( 0.257*(1<<RGB2YUV_SHIFT)+0.5))
108 #define RV ((int)( 0.439*(1<<RGB2YUV_SHIFT)+0.5))
109 #define RU ((int)(-0.148*(1<<RGB2YUV_SHIFT)+0.5))
111 extern int verbose; // defined in mplayer.c
115 known BUGS with known cause (no bugreports please!, but patches are welcome :) )
116 horizontal fast_bilinear MMX2 scaler reads 1-7 samples too much (might cause a sig11)
118 Special versions: fast Y 1:1 scaling (no interpolation in y direction)
121 more intelligent missalignment avoidance for the horizontal scaler
122 write special vertical cubic upscale version
123 Optimize C code (yv12 / minmax)
124 add support for packed pixel yuv input & output
125 add support for Y8 output
126 optimize bgr24 & bgr32
127 add BGR4 output support
128 write special BGR->BGR scaler
129 deglobalize yuv2rgb*.c
132 #define ABS(a) ((a) > 0 ? (a) : (-(a)))
133 #define MIN(a,b) ((a) > (b) ? (b) : (a))
134 #define MAX(a,b) ((a) < (b) ? (b) : (a))
137 #define CAN_COMPILE_X86_ASM
140 #ifdef CAN_COMPILE_X86_ASM
141 static uint64_t __attribute__((aligned(8))) yCoeff= 0x2568256825682568LL;
142 static uint64_t __attribute__((aligned(8))) vrCoeff= 0x3343334333433343LL;
143 static uint64_t __attribute__((aligned(8))) ubCoeff= 0x40cf40cf40cf40cfLL;
144 static uint64_t __attribute__((aligned(8))) vgCoeff= 0xE5E2E5E2E5E2E5E2LL;
145 static uint64_t __attribute__((aligned(8))) ugCoeff= 0xF36EF36EF36EF36ELL;
146 static uint64_t __attribute__((aligned(8))) bF8= 0xF8F8F8F8F8F8F8F8LL;
147 static uint64_t __attribute__((aligned(8))) bFC= 0xFCFCFCFCFCFCFCFCLL;
148 static uint64_t __attribute__((aligned(8))) w400= 0x0400040004000400LL;
149 static uint64_t __attribute__((aligned(8))) w80= 0x0080008000800080LL;
150 static uint64_t __attribute__((aligned(8))) w10= 0x0010001000100010LL;
151 static uint64_t __attribute__((aligned(8))) w02= 0x0002000200020002LL;
152 static uint64_t __attribute__((aligned(8))) bm00001111=0x00000000FFFFFFFFLL;
153 static uint64_t __attribute__((aligned(8))) bm00000111=0x0000000000FFFFFFLL;
154 static uint64_t __attribute__((aligned(8))) bm11111000=0xFFFFFFFFFF000000LL;
155 static uint64_t __attribute__((aligned(8))) bm01010101=0x00FF00FF00FF00FFLL;
157 static volatile uint64_t __attribute__((aligned(8))) b5Dither;
158 static volatile uint64_t __attribute__((aligned(8))) g5Dither;
159 static volatile uint64_t __attribute__((aligned(8))) g6Dither;
160 static volatile uint64_t __attribute__((aligned(8))) r5Dither;
162 static uint64_t __attribute__((aligned(8))) dither4[2]={
163 0x0103010301030103LL,
164 0x0200020002000200LL,};
166 static uint64_t __attribute__((aligned(8))) dither8[2]={
167 0x0602060206020602LL,
168 0x0004000400040004LL,};
170 static uint64_t __attribute__((aligned(8))) b16Mask= 0x001F001F001F001FLL;
171 static uint64_t __attribute__((aligned(8))) g16Mask= 0x07E007E007E007E0LL;
172 static uint64_t __attribute__((aligned(8))) r16Mask= 0xF800F800F800F800LL;
173 static uint64_t __attribute__((aligned(8))) b15Mask= 0x001F001F001F001FLL;
174 static uint64_t __attribute__((aligned(8))) g15Mask= 0x03E003E003E003E0LL;
175 static uint64_t __attribute__((aligned(8))) r15Mask= 0x7C007C007C007C00LL;
177 static uint64_t __attribute__((aligned(8))) M24A= 0x00FF0000FF0000FFLL;
178 static uint64_t __attribute__((aligned(8))) M24B= 0xFF0000FF0000FF00LL;
179 static uint64_t __attribute__((aligned(8))) M24C= 0x0000FF0000FF0000LL;
182 static uint64_t __attribute__((aligned(8))) asm_yalpha1;
183 static uint64_t __attribute__((aligned(8))) asm_uvalpha1;
186 // clipping helper table for C implementations:
187 static unsigned char clip_table[768];
189 static unsigned short clip_table16b[768];
190 static unsigned short clip_table16g[768];
191 static unsigned short clip_table16r[768];
192 static unsigned short clip_table15b[768];
193 static unsigned short clip_table15g[768];
194 static unsigned short clip_table15r[768];
196 // yuv->rgb conversion tables:
197 static int yuvtab_2568[256];
198 static int yuvtab_3343[256];
199 static int yuvtab_0c92[256];
200 static int yuvtab_1a1e[256];
201 static int yuvtab_40cf[256];
202 // Needed for cubic scaler to catch overflows
203 static int clip_yuvtab_2568[768];
204 static int clip_yuvtab_3343[768];
205 static int clip_yuvtab_0c92[768];
206 static int clip_yuvtab_1a1e[768];
207 static int clip_yuvtab_40cf[768];
209 //global sws_flags from the command line
213 SwsFilter src_filter= {NULL, NULL, NULL, NULL};
215 float sws_lum_gblur= 0.0;
216 float sws_chr_gblur= 0.0;
217 int sws_chr_vshift= 0;
218 int sws_chr_hshift= 0;
219 float sws_chr_sharpen= 0.0;
220 float sws_lum_sharpen= 0.0;
222 /* cpuCaps combined from cpudetect and whats actually compiled in
223 (if there is no support for something compiled in it wont appear here) */
224 static CpuCaps cpuCaps;
226 void (*swScale)(SwsContext *context, uint8_t* src[], int srcStride[], int srcSliceY,
227 int srcSliceH, uint8_t* dst[], int dstStride[])=NULL;
229 static SwsVector *getConvVec(SwsVector *a, SwsVector *b);
231 #ifdef CAN_COMPILE_X86_ASM
232 void in_asm_used_var_warning_killer()
234 volatile int i= yCoeff+vrCoeff+ubCoeff+vgCoeff+ugCoeff+bF8+bFC+w400+w80+w10+
235 bm00001111+bm00000111+bm11111000+b16Mask+g16Mask+r16Mask+b15Mask+g15Mask+r15Mask+asm_yalpha1+ asm_uvalpha1+
236 M24A+M24B+M24C+w02 + b5Dither+g5Dither+r5Dither+g6Dither+dither4[0]+dither8[0]+bm01010101;
241 static inline void yuv2yuvXinC(int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
242 int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
243 uint8_t *dest, uint8_t *uDest, uint8_t *vDest, int dstW)
245 //FIXME Optimize (just quickly writen not opti..)
247 for(i=0; i<dstW; i++)
251 for(j=0; j<lumFilterSize; j++)
252 val += lumSrc[j][i] * lumFilter[j];
254 dest[i]= MIN(MAX(val>>19, 0), 255);
258 for(i=0; i<(dstW>>1); i++)
263 for(j=0; j<chrFilterSize; j++)
265 u += chrSrc[j][i] * chrFilter[j];
266 v += chrSrc[j][i + 2048] * chrFilter[j];
269 uDest[i]= MIN(MAX(u>>19, 0), 255);
270 vDest[i]= MIN(MAX(v>>19, 0), 255);
274 static inline void yuv2rgbXinC(int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
275 int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
276 uint8_t *dest, int dstW, int dstFormat)
278 if(dstFormat==IMGFMT_BGR32)
281 for(i=0; i<(dstW>>1); i++){
288 for(j=0; j<lumFilterSize; j++)
290 Y1 += lumSrc[j][2*i] * lumFilter[j];
291 Y2 += lumSrc[j][2*i+1] * lumFilter[j];
293 for(j=0; j<chrFilterSize; j++)
295 U += chrSrc[j][i] * chrFilter[j];
296 V += chrSrc[j][i+2048] * chrFilter[j];
298 Y1= clip_yuvtab_2568[ (Y1>>19) + 256 ];
299 Y2= clip_yuvtab_2568[ (Y2>>19) + 256 ];
303 Cb= clip_yuvtab_40cf[U+ 256];
304 Cg= clip_yuvtab_1a1e[V+ 256] + yuvtab_0c92[U+ 256];
305 Cr= clip_yuvtab_3343[V+ 256];
307 dest[8*i+0]=clip_table[((Y1 + Cb) >>13)];
308 dest[8*i+1]=clip_table[((Y1 + Cg) >>13)];
309 dest[8*i+2]=clip_table[((Y1 + Cr) >>13)];
311 dest[8*i+4]=clip_table[((Y2 + Cb) >>13)];
312 dest[8*i+5]=clip_table[((Y2 + Cg) >>13)];
313 dest[8*i+6]=clip_table[((Y2 + Cr) >>13)];
316 else if(dstFormat==IMGFMT_BGR24)
319 for(i=0; i<(dstW>>1); i++){
326 for(j=0; j<lumFilterSize; j++)
328 Y1 += lumSrc[j][2*i] * lumFilter[j];
329 Y2 += lumSrc[j][2*i+1] * lumFilter[j];
331 for(j=0; j<chrFilterSize; j++)
333 U += chrSrc[j][i] * chrFilter[j];
334 V += chrSrc[j][i+2048] * chrFilter[j];
336 Y1= clip_yuvtab_2568[ (Y1>>19) + 256 ];
337 Y2= clip_yuvtab_2568[ (Y2>>19) + 256 ];
341 Cb= clip_yuvtab_40cf[U+ 256];
342 Cg= clip_yuvtab_1a1e[V+ 256] + yuvtab_0c92[U+ 256];
343 Cr= clip_yuvtab_3343[V+ 256];
345 dest[0]=clip_table[((Y1 + Cb) >>13)];
346 dest[1]=clip_table[((Y1 + Cg) >>13)];
347 dest[2]=clip_table[((Y1 + Cr) >>13)];
349 dest[3]=clip_table[((Y2 + Cb) >>13)];
350 dest[4]=clip_table[((Y2 + Cg) >>13)];
351 dest[5]=clip_table[((Y2 + Cr) >>13)];
355 else if(dstFormat==IMGFMT_BGR16)
359 static int ditherb1=1<<14;
360 static int ditherg1=1<<13;
361 static int ditherr1=2<<14;
362 static int ditherb2=3<<14;
363 static int ditherg2=3<<13;
364 static int ditherr2=0<<14;
366 ditherb1 ^= (1^2)<<14;
367 ditherg1 ^= (1^2)<<13;
368 ditherr1 ^= (1^2)<<14;
369 ditherb2 ^= (3^0)<<14;
370 ditherg2 ^= (3^0)<<13;
371 ditherr2 ^= (3^0)<<14;
373 const int ditherb1=0;
374 const int ditherg1=0;
375 const int ditherr1=0;
376 const int ditherb2=0;
377 const int ditherg2=0;
378 const int ditherr2=0;
380 for(i=0; i<(dstW>>1); i++){
387 for(j=0; j<lumFilterSize; j++)
389 Y1 += lumSrc[j][2*i] * lumFilter[j];
390 Y2 += lumSrc[j][2*i+1] * lumFilter[j];
392 for(j=0; j<chrFilterSize; j++)
394 U += chrSrc[j][i] * chrFilter[j];
395 V += chrSrc[j][i+2048] * chrFilter[j];
397 Y1= clip_yuvtab_2568[ (Y1>>19) + 256 ];
398 Y2= clip_yuvtab_2568[ (Y2>>19) + 256 ];
402 Cb= clip_yuvtab_40cf[U+ 256];
403 Cg= clip_yuvtab_1a1e[V+ 256] + yuvtab_0c92[U+ 256];
404 Cr= clip_yuvtab_3343[V+ 256];
406 ((uint16_t*)dest)[2*i] =
407 clip_table16b[(Y1 + Cb + ditherb1) >>13] |
408 clip_table16g[(Y1 + Cg + ditherg1) >>13] |
409 clip_table16r[(Y1 + Cr + ditherr1) >>13];
411 ((uint16_t*)dest)[2*i+1] =
412 clip_table16b[(Y2 + Cb + ditherb2) >>13] |
413 clip_table16g[(Y2 + Cg + ditherg2) >>13] |
414 clip_table16r[(Y2 + Cr + ditherr2) >>13];
417 else if(dstFormat==IMGFMT_BGR15)
421 static int ditherb1=1<<14;
422 static int ditherg1=1<<14;
423 static int ditherr1=2<<14;
424 static int ditherb2=3<<14;
425 static int ditherg2=3<<14;
426 static int ditherr2=0<<14;
428 ditherb1 ^= (1^2)<<14;
429 ditherg1 ^= (1^2)<<14;
430 ditherr1 ^= (1^2)<<14;
431 ditherb2 ^= (3^0)<<14;
432 ditherg2 ^= (3^0)<<14;
433 ditherr2 ^= (3^0)<<14;
435 const int ditherb1=0;
436 const int ditherg1=0;
437 const int ditherr1=0;
438 const int ditherb2=0;
439 const int ditherg2=0;
440 const int ditherr2=0;
442 for(i=0; i<(dstW>>1); i++){
449 for(j=0; j<lumFilterSize; j++)
451 Y1 += lumSrc[j][2*i] * lumFilter[j];
452 Y2 += lumSrc[j][2*i+1] * lumFilter[j];
454 for(j=0; j<chrFilterSize; j++)
456 U += chrSrc[j][i] * chrFilter[j];
457 V += chrSrc[j][i+2048] * chrFilter[j];
459 Y1= clip_yuvtab_2568[ (Y1>>19) + 256 ];
460 Y2= clip_yuvtab_2568[ (Y2>>19) + 256 ];
464 Cb= clip_yuvtab_40cf[U+ 256];
465 Cg= clip_yuvtab_1a1e[V+ 256] + yuvtab_0c92[U+ 256];
466 Cr= clip_yuvtab_3343[V+ 256];
468 ((uint16_t*)dest)[2*i] =
469 clip_table15b[(Y1 + Cb + ditherb1) >>13] |
470 clip_table15g[(Y1 + Cg + ditherg1) >>13] |
471 clip_table15r[(Y1 + Cr + ditherr1) >>13];
473 ((uint16_t*)dest)[2*i+1] =
474 clip_table15b[(Y2 + Cb + ditherb2) >>13] |
475 clip_table15g[(Y2 + Cg + ditherg2) >>13] |
476 clip_table15r[(Y2 + Cr + ditherr2) >>13];
482 //Note: we have C, X86, MMX, MMX2, 3DNOW version therse no 3DNOW+MMX2 one
484 #if !defined (HAVE_MMX) || defined (RUNTIME_CPUDETECT)
488 #ifdef CAN_COMPILE_X86_ASM
490 #if (defined (HAVE_MMX) && !defined (HAVE_3DNOW) && !defined (HAVE_MMX2)) || defined (RUNTIME_CPUDETECT)
494 #if defined (HAVE_MMX2) || defined (RUNTIME_CPUDETECT)
498 #if (defined (HAVE_3DNOW) && !defined (HAVE_MMX2)) || defined (RUNTIME_CPUDETECT)
499 #define COMPILE_3DNOW
501 #endif //CAN_COMPILE_X86_ASM
511 #define RENAME(a) a ## _C
512 #include "swscale_template.c"
515 #ifdef CAN_COMPILE_X86_ASM
524 #define RENAME(a) a ## _X86
525 #include "swscale_template.c"
533 #define RENAME(a) a ## _MMX
534 #include "swscale_template.c"
543 #define RENAME(a) a ## _MMX2
544 #include "swscale_template.c"
553 #define RENAME(a) a ## _3DNow
554 #include "swscale_template.c"
557 #endif //CAN_COMPILE_X86_ASM
559 // minor note: the HAVE_xyz is messed up after that line so dont use it
562 // old global scaler, dont use for new code
563 // will use sws_flags from the command line
564 void SwScale_YV12slice(unsigned char* src[], int srcStride[], int srcSliceY ,
565 int srcSliceH, uint8_t* dst[], int dstStride, int dstbpp,
566 int srcW, int srcH, int dstW, int dstH){
568 static SwsContext *context=NULL;
570 int dstStride3[3]= {dstStride, dstStride>>1, dstStride>>1};
574 case 8 : dstFormat= IMGFMT_Y8; break;
575 case 12: dstFormat= IMGFMT_YV12; break;
576 case 15: dstFormat= IMGFMT_BGR15; break;
577 case 16: dstFormat= IMGFMT_BGR16; break;
578 case 24: dstFormat= IMGFMT_BGR24; break;
579 case 32: dstFormat= IMGFMT_BGR32; break;
583 if(!context) context=getSwsContextFromCmdLine(srcW, srcH, IMGFMT_YV12, dstW, dstH, dstFormat);
585 context->swScale(context, src, srcStride, srcSliceY, srcSliceH, dst, dstStride3);
588 // will use sws_flags & src_filter (from cmd line)
589 SwsContext *getSwsContextFromCmdLine(int srcW, int srcH, int srcFormat, int dstW, int dstH, int dstFormat)
592 static int firstTime=1;
596 asm volatile("emms\n\t"::: "memory"); //FIXME this shouldnt be required but it IS (even for non mmx versions)
601 flags= SWS_PRINT_INFO;
603 else if(verbose>1) flags= SWS_PRINT_INFO;
605 if(src_filter.lumH) freeVec(src_filter.lumH);
606 if(src_filter.lumV) freeVec(src_filter.lumV);
607 if(src_filter.chrH) freeVec(src_filter.chrH);
608 if(src_filter.chrV) freeVec(src_filter.chrV);
610 if(sws_lum_gblur!=0.0){
611 src_filter.lumH= getGaussianVec(sws_lum_gblur, 3.0);
612 src_filter.lumV= getGaussianVec(sws_lum_gblur, 3.0);
614 src_filter.lumH= getIdentityVec();
615 src_filter.lumV= getIdentityVec();
618 if(sws_chr_gblur!=0.0){
619 src_filter.chrH= getGaussianVec(sws_chr_gblur, 3.0);
620 src_filter.chrV= getGaussianVec(sws_chr_gblur, 3.0);
622 src_filter.chrH= getIdentityVec();
623 src_filter.chrV= getIdentityVec();
626 if(sws_chr_sharpen!=0.0){
627 SwsVector *g= getConstVec(-1.0, 3);
628 SwsVector *id= getConstVec(10.0/sws_chr_sharpen, 1);
631 convVec(src_filter.chrH, id);
632 convVec(src_filter.chrV, id);
637 if(sws_lum_sharpen!=0.0){
638 SwsVector *g= getConstVec(-1.0, 3);
639 SwsVector *id= getConstVec(10.0/sws_lum_sharpen, 1);
642 convVec(src_filter.lumH, id);
643 convVec(src_filter.lumV, id);
649 shiftVec(src_filter.chrH, sws_chr_hshift);
652 shiftVec(src_filter.chrV, sws_chr_vshift);
654 normalizeVec(src_filter.chrH, 1.0);
655 normalizeVec(src_filter.chrV, 1.0);
656 normalizeVec(src_filter.lumH, 1.0);
657 normalizeVec(src_filter.lumV, 1.0);
659 if(verbose > 1) printVec(src_filter.chrH);
660 if(verbose > 1) printVec(src_filter.lumH);
664 case 0: flags|= SWS_FAST_BILINEAR; break;
665 case 1: flags|= SWS_BILINEAR; break;
666 case 2: flags|= SWS_BICUBIC; break;
667 case 3: flags|= SWS_X; break;
668 case 4: flags|= SWS_POINT; break;
669 case 5: flags|= SWS_AREA; break;
670 default:flags|= SWS_BILINEAR; break;
673 return getSwsContext(srcW, srcH, srcFormat, dstW, dstH, dstFormat, flags, &src_filter, NULL);
677 static inline void initFilter(int16_t **outFilter, int16_t **filterPos, int *outFilterSize, int xInc,
678 int srcW, int dstW, int filterAlign, int one, int flags,
679 SwsVector *srcFilter, SwsVector *dstFilter)
686 double *filter2=NULL;
689 asm volatile("emms\n\t"::: "memory"); //FIXME this shouldnt be required but it IS (even for non mmx versions)
692 // Note the +1 is for the MMXscaler which reads over the end
693 *filterPos = (int16_t*)memalign(8, (dstW+1)*sizeof(int16_t));
695 if(ABS(xInc - 0x10000) <10) // unscaled
699 filter= (double*)memalign(8, dstW*sizeof(double)*filterSize);
700 for(i=0; i<dstW*filterSize; i++) filter[i]=0;
702 for(i=0; i<dstW; i++)
704 filter[i*filterSize]=1;
709 else if(flags&SWS_POINT) // lame looking point sampling mode
714 filter= (double*)memalign(8, dstW*sizeof(double)*filterSize);
716 xDstInSrc= xInc/2 - 0x8000;
717 for(i=0; i<dstW; i++)
719 int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
726 else if(xInc <= (1<<16) || (flags&SWS_FAST_BILINEAR)) // upscale
730 if (flags&SWS_BICUBIC) filterSize= 4;
731 else if(flags&SWS_X ) filterSize= 4;
732 else filterSize= 2; // SWS_BILINEAR / SWS_AREA
733 // printf("%d %d %d\n", filterSize, srcW, dstW);
734 filter= (double*)memalign(8, dstW*sizeof(double)*filterSize);
736 xDstInSrc= xInc/2 - 0x8000;
737 for(i=0; i<dstW; i++)
739 int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
743 if((flags & SWS_BICUBIC) || (flags & SWS_X))
745 double d= ABS(((xx+1)<<16) - xDstInSrc)/(double)(1<<16);
748 if(flags & SWS_BICUBIC){
749 // Equation is from VirtualDub
750 y1 = ( + A*d - 2.0*A*d*d + A*d*d*d);
751 y2 = (+ 1.0 - (A+3.0)*d*d + (A+2.0)*d*d*d);
752 y3 = ( - A*d + (2.0*A+3.0)*d*d - (A+2.0)*d*d*d);
753 y4 = ( + A*d*d - A*d*d*d);
755 // cubic interpolation (derived it myself)
756 y1 = ( -2.0*d + 3.0*d*d - 1.0*d*d*d)/6.0;
757 y2 = (6.0 -3.0*d - 6.0*d*d + 3.0*d*d*d)/6.0;
758 y3 = ( +6.0*d + 3.0*d*d - 3.0*d*d*d)/6.0;
759 y4 = ( -1.0*d + 1.0*d*d*d)/6.0;
762 // printf("%d %d %d \n", coeff, (int)d, xDstInSrc);
763 filter[i*filterSize + 0]= y1;
764 filter[i*filterSize + 1]= y2;
765 filter[i*filterSize + 2]= y3;
766 filter[i*filterSize + 3]= y4;
767 // printf("%1.3f %1.3f %1.3f %1.3f %1.3f\n",d , y1, y2, y3, y4);
771 //Bilinear upscale / linear interpolate / Area averaging
772 for(j=0; j<filterSize; j++)
774 double d= ABS((xx<<16) - xDstInSrc)/(double)(1<<16);
775 double coeff= 1.0 - d;
777 // printf("%d %d %d \n", coeff, (int)d, xDstInSrc);
778 filter[i*filterSize + j]= coeff;
788 if(flags&SWS_BICUBIC) filterSize= (int)ceil(1 + 4.0*srcW / (double)dstW);
789 else if(flags&SWS_X) filterSize= (int)ceil(1 + 4.0*srcW / (double)dstW);
790 else if(flags&SWS_AREA) filterSize= (int)ceil(1 + 1.0*srcW / (double)dstW);
791 else /* BILINEAR */ filterSize= (int)ceil(1 + 2.0*srcW / (double)dstW);
792 // printf("%d %d %d\n", *filterSize, srcW, dstW);
793 filter= (double*)memalign(8, dstW*sizeof(double)*filterSize);
795 xDstInSrc= xInc/2 - 0x8000;
796 for(i=0; i<dstW; i++)
798 int xx= (int)((double)xDstInSrc/(double)(1<<16) - (filterSize-1)*0.5 + 0.5);
801 for(j=0; j<filterSize; j++)
803 double d= ABS((xx<<16) - xDstInSrc)/(double)xInc;
805 if((flags & SWS_BICUBIC) || (flags & SWS_X))
809 // Equation is from VirtualDub
811 coeff = (1.0 - (A+3.0)*d*d + (A+2.0)*d*d*d);
813 coeff = (-4.0*A + 8.0*A*d - 5.0*A*d*d + A*d*d*d);
817 else if(flags & SWS_AREA)
819 double srcPixelSize= (1<<16)/(double)xInc;
820 if(d + srcPixelSize/2 < 0.5) coeff= 1.0;
821 else if(d - srcPixelSize/2 < 0.5) coeff= (0.5-d)/srcPixelSize + 0.5;
829 // printf("%1.3f %2.3f %d \n", coeff, d, xDstInSrc);
830 filter[i*filterSize + j]= coeff;
837 /* apply src & dst Filter to filter -> filter2
840 filter2Size= filterSize;
841 if(srcFilter) filter2Size+= srcFilter->length - 1;
842 if(dstFilter) filter2Size+= dstFilter->length - 1;
843 filter2= (double*)memalign(8, filter2Size*dstW*sizeof(double));
845 for(i=0; i<dstW; i++)
848 SwsVector scaleFilter;
851 scaleFilter.coeff= filter + i*filterSize;
852 scaleFilter.length= filterSize;
854 if(srcFilter) outVec= getConvVec(srcFilter, &scaleFilter);
855 else outVec= &scaleFilter;
857 ASSERT(outVec->length == filter2Size)
860 for(j=0; j<outVec->length; j++)
862 filter2[i*filter2Size + j]= outVec->coeff[j];
865 (*filterPos)[i]+= (filterSize-1)/2 - (filter2Size-1)/2;
867 if(outVec != &scaleFilter) freeVec(outVec);
869 free(filter); filter=NULL;
871 /* try to reduce the filter-size (step1 find size and shift left) */
872 // Assume its near normalized (*0.5 or *2.0 is ok but * 0.001 is not)
874 for(i=dstW-1; i>=0; i--)
876 int min= filter2Size;
880 /* get rid off near zero elements on the left by shifting left */
881 for(j=0; j<filter2Size; j++)
884 cutOff += ABS(filter2[i*filter2Size]);
886 if(cutOff > SWS_MAX_REDUCE_CUTOFF) break;
888 /* preserve Monotonicity because the core cant handle the filter otherwise */
889 if(i<dstW-1 && (*filterPos)[i] >= (*filterPos)[i+1]) break;
891 // Move filter coeffs left
892 for(k=1; k<filter2Size; k++)
893 filter2[i*filter2Size + k - 1]= filter2[i*filter2Size + k];
894 filter2[i*filter2Size + k - 1]= 0.0;
899 /* count near zeros on the right */
900 for(j=filter2Size-1; j>0; j--)
902 cutOff += ABS(filter2[i*filter2Size + j]);
904 if(cutOff > SWS_MAX_REDUCE_CUTOFF) break;
908 if(min>minFilterSize) minFilterSize= min;
911 filterSize= (minFilterSize +(filterAlign-1)) & (~(filterAlign-1));
912 filter= (double*)memalign(8, filterSize*dstW*sizeof(double));
913 *outFilterSize= filterSize;
915 if((flags&SWS_PRINT_INFO) && verbose)
916 printf("SwScaler: reducing / aligning filtersize %d -> %d\n", filter2Size, filterSize);
917 /* try to reduce the filter-size (step2 reduce it) */
918 for(i=0; i<dstW; i++)
922 for(j=0; j<filterSize; j++)
924 if(j>=filter2Size) filter[i*filterSize + j]= 0.0;
925 else filter[i*filterSize + j]= filter2[i*filter2Size + j];
928 free(filter2); filter2=NULL;
930 ASSERT(filterSize > 0)
932 //FIXME try to align filterpos if possible
935 for(i=0; i<dstW; i++)
938 if((*filterPos)[i] < 0)
940 // Move filter coeffs left to compensate for filterPos
941 for(j=1; j<filterSize; j++)
943 int left= MAX(j + (*filterPos)[i], 0);
944 filter[i*filterSize + left] += filter[i*filterSize + j];
945 filter[i*filterSize + j]=0;
950 if((*filterPos)[i] + filterSize > srcW)
952 int shift= (*filterPos)[i] + filterSize - srcW;
953 // Move filter coeffs right to compensate for filterPos
954 for(j=filterSize-2; j>=0; j--)
956 int right= MIN(j + shift, filterSize-1);
957 filter[i*filterSize +right] += filter[i*filterSize +j];
958 filter[i*filterSize +j]=0;
960 (*filterPos)[i]= srcW - filterSize;
964 // Note the +1 is for the MMXscaler which reads over the end
965 *outFilter= (int16_t*)memalign(8, *outFilterSize*(dstW+1)*sizeof(int16_t));
966 memset(*outFilter, 0, *outFilterSize*(dstW+1)*sizeof(int16_t));
968 /* Normalize & Store in outFilter */
969 for(i=0; i<dstW; i++)
974 for(j=0; j<filterSize; j++)
976 sum+= filter[i*filterSize + j];
979 for(j=0; j<filterSize; j++)
981 (*outFilter)[i*(*outFilterSize) + j]= (int)(filter[i*filterSize + j]*scale);
985 (*filterPos)[dstW]= (*filterPos)[dstW-1]; // the MMX scaler will read over the end
986 for(i=0; i<*outFilterSize; i++)
988 int j= dstW*(*outFilterSize);
989 (*outFilter)[j + i]= (*outFilter)[j + i - (*outFilterSize)];
996 static void initMMX2HScaler(int dstW, int xInc, uint8_t *funnyCode)
1005 // create an optimized horizontal scaling routine
1013 "movq (%%esi), %%mm0 \n\t" //FIXME Alignment
1014 "movq %%mm0, %%mm1 \n\t"
1015 "psrlq $8, %%mm0 \n\t"
1016 "punpcklbw %%mm7, %%mm1 \n\t"
1017 "movq %%mm2, %%mm3 \n\t"
1018 "punpcklbw %%mm7, %%mm0 \n\t"
1019 "addw %%bx, %%cx \n\t" //2*xalpha += (4*lumXInc)&0xFFFF
1020 "pshufw $0xFF, %%mm1, %%mm1 \n\t"
1022 "adcl %%edx, %%esi \n\t" //xx+= (4*lumXInc)>>16 + carry
1023 "pshufw $0xFF, %%mm0, %%mm0 \n\t"
1025 "psrlw $9, %%mm3 \n\t"
1026 "psubw %%mm1, %%mm0 \n\t"
1027 "pmullw %%mm3, %%mm0 \n\t"
1028 "paddw %%mm6, %%mm2 \n\t" // 2*alpha += xpos&0xFFFF
1029 "psllw $7, %%mm1 \n\t"
1030 "paddw %%mm1, %%mm0 \n\t"
1032 "movq %%mm0, (%%edi, %%eax) \n\t"
1034 "addl $8, %%eax \n\t"
1047 :"=r" (fragment), "=r" (imm8OfPShufW1), "=r" (imm8OfPShufW2),
1048 "=r" (fragmentLength)
1051 xpos= 0; //lumXInc/2 - 0x8000; // difference between pixel centers
1053 for(i=0; i<dstW/8; i++)
1060 int b=((xpos+xInc)>>16) - xx;
1061 int c=((xpos+xInc*2)>>16) - xx;
1062 int d=((xpos+xInc*3)>>16) - xx;
1064 memcpy(funnyCode + fragmentLength*i/4, fragment, fragmentLength);
1066 funnyCode[fragmentLength*i/4 + imm8OfPShufW1]=
1067 funnyCode[fragmentLength*i/4 + imm8OfPShufW2]=
1068 a | (b<<2) | (c<<4) | (d<<6);
1070 // if we dont need to read 8 bytes than dont :), reduces the chance of
1071 // crossing a cache line
1072 if(d<3) funnyCode[fragmentLength*i/4 + 1]= 0x6E;
1074 funnyCode[fragmentLength*(i+4)/4]= RET;
1082 void SwScale_Init(){
1085 static void globalInit(){
1086 // generating tables:
1088 for(i=0; i<768; i++){
1089 int c= MIN(MAX(i-256, 0), 255);
1091 yuvtab_2568[c]= clip_yuvtab_2568[i]=(0x2568*(c-16))+(256<<13);
1092 yuvtab_3343[c]= clip_yuvtab_3343[i]=0x3343*(c-128);
1093 yuvtab_0c92[c]= clip_yuvtab_0c92[i]=-0x0c92*(c-128);
1094 yuvtab_1a1e[c]= clip_yuvtab_1a1e[i]=-0x1a1e*(c-128);
1095 yuvtab_40cf[c]= clip_yuvtab_40cf[i]=0x40cf*(c-128);
1098 for(i=0; i<768; i++)
1100 int v= clip_table[i];
1101 clip_table16b[i]= le2me_16( v>>3);
1102 clip_table16g[i]= le2me_16((v<<3)&0x07E0);
1103 clip_table16r[i]= le2me_16((v<<8)&0xF800);
1104 clip_table15b[i]= le2me_16( v>>3);
1105 clip_table15g[i]= le2me_16((v<<2)&0x03E0);
1106 clip_table15r[i]= le2me_16((v<<7)&0x7C00);
1111 #ifdef RUNTIME_CPUDETECT
1112 #ifdef CAN_COMPILE_X86_ASM
1113 // ordered per speed fasterst first
1114 if(gCpuCaps.hasMMX2)
1115 swScale= swScale_MMX2;
1116 else if(gCpuCaps.has3DNow)
1117 swScale= swScale_3DNow;
1118 else if(gCpuCaps.hasMMX)
1119 swScale= swScale_MMX;
1125 cpuCaps.hasMMX2 = cpuCaps.hasMMX = cpuCaps.has3DNow = 0;
1127 #else //RUNTIME_CPUDETECT
1129 swScale= swScale_MMX2;
1130 cpuCaps.has3DNow = 0;
1131 #elif defined (HAVE_3DNOW)
1132 swScale= swScale_3DNow;
1133 cpuCaps.hasMMX2 = 0;
1134 #elif defined (HAVE_MMX)
1135 swScale= swScale_MMX;
1136 cpuCaps.hasMMX2 = cpuCaps.has3DNow = 0;
1139 cpuCaps.hasMMX2 = cpuCaps.hasMMX = cpuCaps.has3DNow = 0;
1141 #endif //!RUNTIME_CPUDETECT
1144 /* Warper functions for yuv2bgr */
1145 static void planarYuvToBgr(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1146 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1147 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1149 if(c->srcFormat==IMGFMT_YV12)
1150 yuv2rgb( dst,src[0],src[1],src[2],c->srcW,srcSliceH,dstStride[0],srcStride[0],srcStride[1] );
1151 else /* I420 & IYUV */
1152 yuv2rgb( dst,src[0],src[2],src[1],c->srcW,srcSliceH,dstStride[0],srcStride[0],srcStride[1] );
1155 static void bgr24to32Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1156 int srcSliceH, uint8_t* dst[], int dstStride[]){
1158 if(dstStride[0]*3==srcStride[0]*4)
1159 rgb24to32(src[0], dst[0] + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
1163 uint8_t *srcPtr= src[0];
1164 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1166 for(i=0; i<srcSliceH; i++)
1168 rgb24to32(srcPtr, dstPtr, c->srcW*3);
1169 srcPtr+= srcStride[0];
1170 dstPtr+= dstStride[0];
1175 static void bgr32to24Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1176 int srcSliceH, uint8_t* dst[], int dstStride[]){
1178 if(dstStride[0]*4==srcStride[0]*3)
1179 rgb32to24(src[0], dst[0] + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
1183 uint8_t *srcPtr= src[0];
1184 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1186 for(i=0; i<srcSliceH; i++)
1188 rgb32to24(srcPtr, dstPtr, c->srcW<<2);
1189 srcPtr+= srcStride[0];
1190 dstPtr+= dstStride[0];
1195 static void bgr15to16Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1196 int srcSliceH, uint8_t* dst[], int dstStride[]){
1198 if(dstStride[0]==srcStride[0])
1199 rgb15to16(src[0], dst[0] + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
1203 uint8_t *srcPtr= src[0];
1204 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1206 for(i=0; i<srcSliceH; i++)
1208 rgb15to16(srcPtr, dstPtr, c->srcW<<1);
1209 srcPtr+= srcStride[0];
1210 dstPtr+= dstStride[0];
1216 /* unscaled copy like stuff (assumes nearly identical formats) */
1217 static void simpleCopy(SwsContext *c, uint8_t* srcParam[], int srcStrideParam[], int srcSliceY,
1218 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1224 if(c->srcFormat == IMGFMT_I420){
1225 src[0]= srcParam[0];
1226 src[1]= srcParam[2];
1227 src[2]= srcParam[1];
1228 srcStride[0]= srcStrideParam[0];
1229 srcStride[1]= srcStrideParam[2];
1230 srcStride[2]= srcStrideParam[1];
1232 else if(c->srcFormat==IMGFMT_YV12){
1233 src[0]= srcParam[0];
1234 src[1]= srcParam[1];
1235 src[2]= srcParam[2];
1236 srcStride[0]= srcStrideParam[0];
1237 srcStride[1]= srcStrideParam[1];
1238 srcStride[2]= srcStrideParam[2];
1240 else if(isPacked(c->srcFormat) || isGray(c->srcFormat)){
1241 src[0]= srcParam[0];
1244 srcStride[0]= srcStrideParam[0];
1249 if(c->dstFormat == IMGFMT_I420){
1250 dst[0]= dstParam[0];
1251 dst[1]= dstParam[2];
1252 dst[2]= dstParam[1];
1255 dst[0]= dstParam[0];
1256 dst[1]= dstParam[1];
1257 dst[2]= dstParam[2];
1260 if(isPacked(c->srcFormat))
1262 if(dstStride[0]==srcStride[0])
1263 memcpy(dst[0] + dstStride[0]*srcSliceY, src[0], srcSliceH*dstStride[0]);
1267 uint8_t *srcPtr= src[0];
1268 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1271 /* universal length finder */
1272 while(length+c->srcW <= dstStride[0]
1273 && length+c->srcW <= srcStride[0]) length+= c->srcW;
1276 for(i=0; i<srcSliceH; i++)
1278 memcpy(dstPtr, srcPtr, length);
1279 srcPtr+= srcStride[0];
1280 dstPtr+= dstStride[0];
1287 for(plane=0; plane<3; plane++)
1289 int length= plane==0 ? c->srcW : ((c->srcW+1)>>1);
1290 int y= plane==0 ? srcSliceY: ((srcSliceY+1)>>1);
1291 int height= plane==0 ? srcSliceH: ((srcSliceH+1)>>1);
1293 if(dstStride[plane]==srcStride[plane])
1294 memcpy(dst[plane] + dstStride[plane]*y, src[plane], height*dstStride[plane]);
1298 uint8_t *srcPtr= src[plane];
1299 uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
1300 for(i=0; i<height; i++)
1302 memcpy(dstPtr, srcPtr, length);
1303 srcPtr+= srcStride[plane];
1304 dstPtr+= dstStride[plane];
1311 SwsContext *getSwsContext(int srcW, int srcH, int srcFormat, int dstW, int dstH, int dstFormat, int flags,
1312 SwsFilter *srcFilter, SwsFilter *dstFilter){
1317 SwsFilter dummyFilter= {NULL, NULL, NULL, NULL};
1321 asm volatile("emms\n\t"::: "memory");
1324 if(swScale==NULL) globalInit();
1326 /* avoid dupplicate Formats, so we dont need to check to much */
1327 if(srcFormat==IMGFMT_IYUV) srcFormat=IMGFMT_I420;
1328 if(srcFormat==IMGFMT_Y8) srcFormat=IMGFMT_Y800;
1329 if(dstFormat==IMGFMT_Y8) dstFormat=IMGFMT_Y800;
1331 if(!isSupportedIn(srcFormat))
1333 fprintf(stderr, "swScaler: %s is not supported as input format\n", vo_format_name(srcFormat));
1336 if(!isSupportedOut(dstFormat))
1338 fprintf(stderr, "swScaler: %s is not supported as output format\n", vo_format_name(dstFormat));
1343 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
1345 fprintf(stderr, "swScaler: %dx%d -> %dx%d is invalid scaling dimension\n",
1346 srcW, srcH, dstW, dstH);
1350 if(!dstFilter) dstFilter= &dummyFilter;
1351 if(!srcFilter) srcFilter= &dummyFilter;
1353 c= memalign(64, sizeof(SwsContext));
1354 memset(c, 0, sizeof(SwsContext));
1360 c->lumXInc= ((srcW<<16) + (dstW>>1))/dstW;
1361 c->lumYInc= ((srcH<<16) + (dstH>>1))/dstH;
1363 c->dstFormat= dstFormat;
1364 c->srcFormat= srcFormat;
1367 if(dstFilter->lumV!=NULL && dstFilter->lumV->length>1) usesFilter=1;
1368 if(dstFilter->lumH!=NULL && dstFilter->lumH->length>1) usesFilter=1;
1369 if(dstFilter->chrV!=NULL && dstFilter->chrV->length>1) usesFilter=1;
1370 if(dstFilter->chrH!=NULL && dstFilter->chrH->length>1) usesFilter=1;
1371 if(srcFilter->lumV!=NULL && srcFilter->lumV->length>1) usesFilter=1;
1372 if(srcFilter->lumH!=NULL && srcFilter->lumH->length>1) usesFilter=1;
1373 if(srcFilter->chrV!=NULL && srcFilter->chrV->length>1) usesFilter=1;
1374 if(srcFilter->chrH!=NULL && srcFilter->chrH->length>1) usesFilter=1;
1376 /* unscaled special Cases */
1377 if(srcW==dstW && srcH==dstH && !usesFilter)
1380 if(isPlanarYUV(srcFormat) && isBGR(dstFormat))
1382 // FIXME multiple yuv2rgb converters wont work that way cuz that thing is full of globals&statics
1383 yuv2rgb_init( dstFormat&0xFF /* =bpp */, MODE_RGB);
1384 c->swScale= planarYuvToBgr;
1386 if(flags&SWS_PRINT_INFO)
1387 printf("SwScaler: using unscaled %s -> %s special converter\n",
1388 vo_format_name(srcFormat), vo_format_name(dstFormat));
1393 if(srcFormat == dstFormat || (isPlanarYUV(srcFormat) && isPlanarYUV(dstFormat)))
1395 c->swScale= simpleCopy;
1397 if(flags&SWS_PRINT_INFO)
1398 printf("SwScaler: using unscaled %s -> %s special converter\n",
1399 vo_format_name(srcFormat), vo_format_name(dstFormat));
1403 /* bgr32to24 & rgb32to24*/
1404 if((srcFormat==IMGFMT_BGR32 && dstFormat==IMGFMT_BGR24)
1405 ||(srcFormat==IMGFMT_RGB32 && dstFormat==IMGFMT_RGB24))
1407 c->swScale= bgr32to24Wrapper;
1409 if(flags&SWS_PRINT_INFO)
1410 printf("SwScaler: using unscaled %s -> %s special converter\n",
1411 vo_format_name(srcFormat), vo_format_name(dstFormat));
1415 /* bgr24to32 & rgb24to32*/
1416 if((srcFormat==IMGFMT_BGR24 && dstFormat==IMGFMT_BGR32)
1417 ||(srcFormat==IMGFMT_RGB24 && dstFormat==IMGFMT_RGB32))
1419 c->swScale= bgr24to32Wrapper;
1421 if(flags&SWS_PRINT_INFO)
1422 printf("SwScaler: using unscaled %s -> %s special converter\n",
1423 vo_format_name(srcFormat), vo_format_name(dstFormat));
1428 if(srcFormat==IMGFMT_BGR15 && dstFormat==IMGFMT_BGR16)
1430 c->swScale= bgr15to16Wrapper;
1432 if(flags&SWS_PRINT_INFO)
1433 printf("SwScaler: using unscaled %s -> %s special converter\n",
1434 vo_format_name(srcFormat), vo_format_name(dstFormat));
1441 c->canMMX2BeUsed= (dstW >=srcW && (dstW&31)==0 && (srcW&15)==0) ? 1 : 0;
1442 if(!c->canMMX2BeUsed && dstW >=srcW && (srcW&15)==0 && (flags&SWS_FAST_BILINEAR))
1444 if(flags&SWS_PRINT_INFO)
1445 fprintf(stderr, "SwScaler: output Width is not a multiple of 32 -> no MMX2 scaler\n");
1452 /* dont use full vertical UV input/internaly if the source doesnt even have it */
1453 if(isHalfChrV(srcFormat)) c->flags= flags= flags&(~SWS_FULL_CHR_V);
1454 /* dont use full horizontal UV input if the source doesnt even have it */
1455 if(isHalfChrH(srcFormat)) c->flags= flags= flags&(~SWS_FULL_CHR_H_INP);
1456 /* dont use full horizontal UV internally if the destination doesnt even have it */
1457 if(isHalfChrH(dstFormat)) c->flags= flags= flags&(~SWS_FULL_CHR_H_INT);
1459 if(flags&SWS_FULL_CHR_H_INP) c->chrSrcW= srcW;
1460 else c->chrSrcW= (srcW+1)>>1;
1462 if(flags&SWS_FULL_CHR_H_INT) c->chrDstW= dstW;
1463 else c->chrDstW= (dstW+1)>>1;
1465 if(flags&SWS_FULL_CHR_V) c->chrSrcH= srcH;
1466 else c->chrSrcH= (srcH+1)>>1;
1468 if(isHalfChrV(dstFormat)) c->chrDstH= (dstH+1)>>1;
1469 else c->chrDstH= dstH;
1471 c->chrXInc= ((c->chrSrcW<<16) + (c->chrDstW>>1))/c->chrDstW;
1472 c->chrYInc= ((c->chrSrcH<<16) + (c->chrDstH>>1))/c->chrDstH;
1475 // match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src to pixel n-2 of dst
1476 // but only for the FAST_BILINEAR mode otherwise do correct scaling
1477 // n-2 is the last chrominance sample available
1478 // this is not perfect, but noone shuld notice the difference, the more correct variant
1479 // would be like the vertical one, but that would require some special code for the
1480 // first and last pixel
1481 if(flags&SWS_FAST_BILINEAR)
1483 if(c->canMMX2BeUsed)
1488 //we dont use the x86asm scaler if mmx is available
1489 else if(cpuCaps.hasMMX)
1491 c->lumXInc = ((srcW-2)<<16)/(dstW-2) - 20;
1492 c->chrXInc = ((c->chrSrcW-2)<<16)/(c->chrDstW-2) - 20;
1496 /* precalculate horizontal scaler filter coefficients */
1498 const int filterAlign= cpuCaps.hasMMX ? 4 : 1;
1500 initFilter(&c->hLumFilter, &c->hLumFilterPos, &c->hLumFilterSize, c->lumXInc,
1501 srcW , dstW, filterAlign, 1<<14, flags,
1502 srcFilter->lumH, dstFilter->lumH);
1503 initFilter(&c->hChrFilter, &c->hChrFilterPos, &c->hChrFilterSize, c->chrXInc,
1504 (srcW+1)>>1, c->chrDstW, filterAlign, 1<<14, flags,
1505 srcFilter->chrH, dstFilter->chrH);
1508 // cant downscale !!!
1509 if(c->canMMX2BeUsed && (flags & SWS_FAST_BILINEAR))
1511 initMMX2HScaler( dstW, c->lumXInc, c->funnyYCode);
1512 initMMX2HScaler(c->chrDstW, c->chrXInc, c->funnyUVCode);
1515 } // Init Horizontal stuff
1519 /* precalculate vertical scaler filter coefficients */
1520 initFilter(&c->vLumFilter, &c->vLumFilterPos, &c->vLumFilterSize, c->lumYInc,
1521 srcH , dstH, 1, (1<<12)-4, flags,
1522 srcFilter->lumV, dstFilter->lumV);
1523 initFilter(&c->vChrFilter, &c->vChrFilterPos, &c->vChrFilterSize, c->chrYInc,
1524 (srcH+1)>>1, c->chrDstH, 1, (1<<12)-4, flags,
1525 srcFilter->chrV, dstFilter->chrV);
1527 // Calculate Buffer Sizes so that they wont run out while handling these damn slices
1528 c->vLumBufSize= c->vLumFilterSize;
1529 c->vChrBufSize= c->vChrFilterSize;
1530 for(i=0; i<dstH; i++)
1532 int chrI= i*c->chrDstH / dstH;
1533 int nextSlice= MAX(c->vLumFilterPos[i ] + c->vLumFilterSize - 1,
1534 ((c->vChrFilterPos[chrI] + c->vChrFilterSize - 1)<<1));
1535 nextSlice&= ~1; // Slices start at even boundaries
1536 if(c->vLumFilterPos[i ] + c->vLumBufSize < nextSlice)
1537 c->vLumBufSize= nextSlice - c->vLumFilterPos[i ];
1538 if(c->vChrFilterPos[chrI] + c->vChrBufSize < (nextSlice>>1))
1539 c->vChrBufSize= (nextSlice>>1) - c->vChrFilterPos[chrI];
1542 // allocate pixbufs (we use dynamic allocation because otherwise we would need to
1543 c->lumPixBuf= (int16_t**)memalign(4, c->vLumBufSize*2*sizeof(int16_t*));
1544 c->chrPixBuf= (int16_t**)memalign(4, c->vChrBufSize*2*sizeof(int16_t*));
1545 //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)
1546 for(i=0; i<c->vLumBufSize; i++)
1547 c->lumPixBuf[i]= c->lumPixBuf[i+c->vLumBufSize]= (uint16_t*)memalign(8, 4000);
1548 for(i=0; i<c->vChrBufSize; i++)
1549 c->chrPixBuf[i]= c->chrPixBuf[i+c->vChrBufSize]= (uint16_t*)memalign(8, 8000);
1551 //try to avoid drawing green stuff between the right end and the stride end
1552 for(i=0; i<c->vLumBufSize; i++) memset(c->lumPixBuf[i], 0, 4000);
1553 for(i=0; i<c->vChrBufSize; i++) memset(c->chrPixBuf[i], 64, 8000);
1555 ASSERT(c->chrDstH <= dstH)
1557 // pack filter data for mmx code
1560 c->lumMmxFilter= (int16_t*)memalign(8, c->vLumFilterSize* dstH*4*sizeof(int16_t));
1561 c->chrMmxFilter= (int16_t*)memalign(8, c->vChrFilterSize*c->chrDstH*4*sizeof(int16_t));
1562 for(i=0; i<c->vLumFilterSize*dstH; i++)
1563 c->lumMmxFilter[4*i]=c->lumMmxFilter[4*i+1]=c->lumMmxFilter[4*i+2]=c->lumMmxFilter[4*i+3]=
1565 for(i=0; i<c->vChrFilterSize*c->chrDstH; i++)
1566 c->chrMmxFilter[4*i]=c->chrMmxFilter[4*i+1]=c->chrMmxFilter[4*i+2]=c->chrMmxFilter[4*i+3]=
1570 if(flags&SWS_PRINT_INFO)
1573 char *dither= " dithered";
1577 if(flags&SWS_FAST_BILINEAR)
1578 fprintf(stderr, "\nSwScaler: FAST_BILINEAR scaler, ");
1579 else if(flags&SWS_BILINEAR)
1580 fprintf(stderr, "\nSwScaler: BILINEAR scaler, ");
1581 else if(flags&SWS_BICUBIC)
1582 fprintf(stderr, "\nSwScaler: BICUBIC scaler, ");
1583 else if(flags&SWS_X)
1584 fprintf(stderr, "\nSwScaler: Experimental scaler, ");
1585 else if(flags&SWS_POINT)
1586 fprintf(stderr, "\nSwScaler: Nearest Neighbor / POINT scaler, ");
1587 else if(flags&SWS_AREA)
1588 fprintf(stderr, "\nSwScaler: Area Averageing scaler, ");
1590 fprintf(stderr, "\nSwScaler: ehh flags invalid?! ");
1592 if(dstFormat==IMGFMT_BGR15 || dstFormat==IMGFMT_BGR16)
1593 fprintf(stderr, "from %s to%s %s ",
1594 vo_format_name(srcFormat), dither, vo_format_name(dstFormat));
1596 fprintf(stderr, "from %s to %s ",
1597 vo_format_name(srcFormat), vo_format_name(dstFormat));
1600 fprintf(stderr, "using MMX2\n");
1601 else if(cpuCaps.has3DNow)
1602 fprintf(stderr, "using 3DNOW\n");
1603 else if(cpuCaps.hasMMX)
1604 fprintf(stderr, "using MMX\n");
1606 fprintf(stderr, "using C\n");
1609 if((flags & SWS_PRINT_INFO) && verbose)
1613 if(c->canMMX2BeUsed && (flags&SWS_FAST_BILINEAR))
1614 printf("SwScaler: using FAST_BILINEAR MMX2 scaler for horizontal scaling\n");
1617 if(c->hLumFilterSize==4)
1618 printf("SwScaler: using 4-tap MMX scaler for horizontal luminance scaling\n");
1619 else if(c->hLumFilterSize==8)
1620 printf("SwScaler: using 8-tap MMX scaler for horizontal luminance scaling\n");
1622 printf("SwScaler: using n-tap MMX scaler for horizontal luminance scaling\n");
1624 if(c->hChrFilterSize==4)
1625 printf("SwScaler: using 4-tap MMX scaler for horizontal chrominance scaling\n");
1626 else if(c->hChrFilterSize==8)
1627 printf("SwScaler: using 8-tap MMX scaler for horizontal chrominance scaling\n");
1629 printf("SwScaler: using n-tap MMX scaler for horizontal chrominance scaling\n");
1635 printf("SwScaler: using X86-Asm scaler for horizontal scaling\n");
1637 if(flags & SWS_FAST_BILINEAR)
1638 printf("SwScaler: using FAST_BILINEAR C scaler for horizontal scaling\n");
1640 printf("SwScaler: using C scaler for horizontal scaling\n");
1643 if(isPlanarYUV(dstFormat))
1645 if(c->vLumFilterSize==1)
1646 printf("SwScaler: using 1-tap %s \"scaler\" for vertical scaling (YV12 like)\n", cpuCaps.hasMMX ? "MMX" : "C");
1648 printf("SwScaler: using n-tap %s scaler for vertical scaling (YV12 like)\n", cpuCaps.hasMMX ? "MMX" : "C");
1652 if(c->vLumFilterSize==1 && c->vChrFilterSize==2)
1653 printf("SwScaler: using 1-tap %s \"scaler\" for vertical luminance scaling (BGR)\n"
1654 "SwScaler: 2-tap scaler for vertical chrominance scaling (BGR)\n",cpuCaps.hasMMX ? "MMX" : "C");
1655 else if(c->vLumFilterSize==2 && c->vChrFilterSize==2)
1656 printf("SwScaler: using 2-tap linear %s scaler for vertical scaling (BGR)\n", cpuCaps.hasMMX ? "MMX" : "C");
1658 printf("SwScaler: using n-tap %s scaler for vertical scaling (BGR)\n", cpuCaps.hasMMX ? "MMX" : "C");
1661 if(dstFormat==IMGFMT_BGR24)
1662 printf("SwScaler: using %s YV12->BGR24 Converter\n",
1663 cpuCaps.hasMMX2 ? "MMX2" : (cpuCaps.hasMMX ? "MMX" : "C"));
1664 else if(dstFormat==IMGFMT_BGR32)
1665 printf("SwScaler: using %s YV12->BGR32 Converter\n", cpuCaps.hasMMX ? "MMX" : "C");
1666 else if(dstFormat==IMGFMT_BGR16)
1667 printf("SwScaler: using %s YV12->BGR16 Converter\n", cpuCaps.hasMMX ? "MMX" : "C");
1668 else if(dstFormat==IMGFMT_BGR15)
1669 printf("SwScaler: using %s YV12->BGR15 Converter\n", cpuCaps.hasMMX ? "MMX" : "C");
1671 printf("SwScaler: %dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
1673 if((flags & SWS_PRINT_INFO) && verbose>1)
1675 printf("SwScaler:Lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
1676 c->srcW, c->srcH, c->dstW, c->dstH, c->lumXInc, c->lumYInc);
1677 printf("SwScaler:Chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
1678 c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH, c->chrXInc, c->chrYInc);
1681 c->swScale= swScale;
1686 * returns a normalized gaussian curve used to filter stuff
1687 * quality=3 is high quality, lowwer is lowwer quality
1690 SwsVector *getGaussianVec(double variance, double quality){
1691 const int length= (int)(variance*quality + 0.5) | 1;
1693 double *coeff= memalign(sizeof(double), length*sizeof(double));
1694 double middle= (length-1)*0.5;
1695 SwsVector *vec= malloc(sizeof(SwsVector));
1698 vec->length= length;
1700 for(i=0; i<length; i++)
1702 double dist= i-middle;
1703 coeff[i]= exp( -dist*dist/(2*variance*variance) ) / sqrt(2*variance*PI);
1706 normalizeVec(vec, 1.0);
1711 SwsVector *getConstVec(double c, int length){
1713 double *coeff= memalign(sizeof(double), length*sizeof(double));
1714 SwsVector *vec= malloc(sizeof(SwsVector));
1717 vec->length= length;
1719 for(i=0; i<length; i++)
1726 SwsVector *getIdentityVec(void){
1727 double *coeff= memalign(sizeof(double), sizeof(double));
1728 SwsVector *vec= malloc(sizeof(SwsVector));
1737 void normalizeVec(SwsVector *a, double height){
1742 for(i=0; i<a->length; i++)
1747 for(i=0; i<a->length; i++)
1748 a->coeff[i]*= height;
1751 void scaleVec(SwsVector *a, double scalar){
1754 for(i=0; i<a->length; i++)
1755 a->coeff[i]*= scalar;
1758 static SwsVector *getConvVec(SwsVector *a, SwsVector *b){
1759 int length= a->length + b->length - 1;
1760 double *coeff= memalign(sizeof(double), length*sizeof(double));
1762 SwsVector *vec= malloc(sizeof(SwsVector));
1765 vec->length= length;
1767 for(i=0; i<length; i++) coeff[i]= 0.0;
1769 for(i=0; i<a->length; i++)
1771 for(j=0; j<b->length; j++)
1773 coeff[i+j]+= a->coeff[i]*b->coeff[j];
1780 static SwsVector *sumVec(SwsVector *a, SwsVector *b){
1781 int length= MAX(a->length, b->length);
1782 double *coeff= memalign(sizeof(double), length*sizeof(double));
1784 SwsVector *vec= malloc(sizeof(SwsVector));
1787 vec->length= length;
1789 for(i=0; i<length; i++) coeff[i]= 0.0;
1791 for(i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
1792 for(i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]+= b->coeff[i];
1797 static SwsVector *diffVec(SwsVector *a, SwsVector *b){
1798 int length= MAX(a->length, b->length);
1799 double *coeff= memalign(sizeof(double), length*sizeof(double));
1801 SwsVector *vec= malloc(sizeof(SwsVector));
1804 vec->length= length;
1806 for(i=0; i<length; i++) coeff[i]= 0.0;
1808 for(i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
1809 for(i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]-= b->coeff[i];
1814 /* shift left / or right if "shift" is negative */
1815 static SwsVector *getShiftedVec(SwsVector *a, int shift){
1816 int length= a->length + ABS(shift)*2;
1817 double *coeff= memalign(sizeof(double), length*sizeof(double));
1819 SwsVector *vec= malloc(sizeof(SwsVector));
1822 vec->length= length;
1824 for(i=0; i<length; i++) coeff[i]= 0.0;
1826 for(i=0; i<a->length; i++)
1828 coeff[i + (length-1)/2 - (a->length-1)/2 - shift]= a->coeff[i];
1834 void shiftVec(SwsVector *a, int shift){
1835 SwsVector *shifted= getShiftedVec(a, shift);
1837 a->coeff= shifted->coeff;
1838 a->length= shifted->length;
1842 void addVec(SwsVector *a, SwsVector *b){
1843 SwsVector *sum= sumVec(a, b);
1845 a->coeff= sum->coeff;
1846 a->length= sum->length;
1850 void subVec(SwsVector *a, SwsVector *b){
1851 SwsVector *diff= diffVec(a, b);
1853 a->coeff= diff->coeff;
1854 a->length= diff->length;
1858 void convVec(SwsVector *a, SwsVector *b){
1859 SwsVector *conv= getConvVec(a, b);
1861 a->coeff= conv->coeff;
1862 a->length= conv->length;
1866 SwsVector *cloneVec(SwsVector *a){
1867 double *coeff= memalign(sizeof(double), a->length*sizeof(double));
1869 SwsVector *vec= malloc(sizeof(SwsVector));
1872 vec->length= a->length;
1874 for(i=0; i<a->length; i++) coeff[i]= a->coeff[i];
1879 void printVec(SwsVector *a){
1885 for(i=0; i<a->length; i++)
1886 if(a->coeff[i]>max) max= a->coeff[i];
1888 for(i=0; i<a->length; i++)
1889 if(a->coeff[i]<min) min= a->coeff[i];
1893 for(i=0; i<a->length; i++)
1895 int x= (int)((a->coeff[i]-min)*60.0/range +0.5);
1896 printf("%1.3f ", a->coeff[i]);
1897 for(;x>0; x--) printf(" ");
1902 void freeVec(SwsVector *a){
1904 if(a->coeff) free(a->coeff);
1910 void freeSwsContext(SwsContext *c){
1917 for(i=0; i<c->vLumBufSize; i++)
1919 if(c->lumPixBuf[i]) free(c->lumPixBuf[i]);
1920 c->lumPixBuf[i]=NULL;
1928 for(i=0; i<c->vChrBufSize; i++)
1930 if(c->chrPixBuf[i]) free(c->chrPixBuf[i]);
1931 c->chrPixBuf[i]=NULL;
1937 if(c->vLumFilter) free(c->vLumFilter);
1938 c->vLumFilter = NULL;
1939 if(c->vChrFilter) free(c->vChrFilter);
1940 c->vChrFilter = NULL;
1941 if(c->hLumFilter) free(c->hLumFilter);
1942 c->hLumFilter = NULL;
1943 if(c->hChrFilter) free(c->hChrFilter);
1944 c->hChrFilter = NULL;
1946 if(c->vLumFilterPos) free(c->vLumFilterPos);
1947 c->vLumFilterPos = NULL;
1948 if(c->vChrFilterPos) free(c->vChrFilterPos);
1949 c->vChrFilterPos = NULL;
1950 if(c->hLumFilterPos) free(c->hLumFilterPos);
1951 c->hLumFilterPos = NULL;
1952 if(c->hChrFilterPos) free(c->hChrFilterPos);
1953 c->hChrFilterPos = NULL;
1955 if(c->lumMmxFilter) free(c->lumMmxFilter);
1956 c->lumMmxFilter = NULL;
1957 if(c->chrMmxFilter) free(c->chrMmxFilter);
1958 c->chrMmxFilter = NULL;