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 (grayscale soon too)
21 supported output formats: YV12, BGR15, BGR16, BGR24, BGR32 (grayscale soon too)
28 #include "../config.h"
29 #include "../mangle.h"
34 #include "../cpudetect.h"
35 #include "../libvo/img_format.h"
45 #define RET 0xC3 //near return opcode
48 #define ASSERT(x) if(!(x)) { printf("ASSERT " #x " failed\n"); *((int*)0)=0; }
56 #define PI 3.14159265358979323846
59 extern int verbose; // defined in mplayer.c
63 known BUGS with known cause (no bugreports please!, but patches are welcome :) )
64 horizontal fast_bilinear MMX2 scaler reads 1-7 samples too much (might cause a sig11)
66 Supported output formats BGR15 BGR16 BGR24 BGR32 YV12
67 BGR15 & BGR16 MMX verions support dithering
68 Special versions: fast Y 1:1 scaling (no interpolation in y direction)
71 more intelligent missalignment avoidance for the horizontal scaler
72 change the distance of the u & v buffer
73 write special vertical cubic upscale version
74 Optimize C code (yv12 / minmax)
75 add support for packed pixel yuv input & output
76 add support for Y8 input & output
77 add BGR4 output support
78 add BGR32 / BGR24 input support
81 #define ABS(a) ((a) > 0 ? (a) : (-(a)))
82 #define MIN(a,b) ((a) > (b) ? (b) : (a))
83 #define MAX(a,b) ((a) < (b) ? (b) : (a))
86 #define CAN_COMPILE_X86_ASM
89 #ifdef CAN_COMPILE_X86_ASM
90 static uint64_t __attribute__((aligned(8))) yCoeff= 0x2568256825682568LL;
91 static uint64_t __attribute__((aligned(8))) vrCoeff= 0x3343334333433343LL;
92 static uint64_t __attribute__((aligned(8))) ubCoeff= 0x40cf40cf40cf40cfLL;
93 static uint64_t __attribute__((aligned(8))) vgCoeff= 0xE5E2E5E2E5E2E5E2LL;
94 static uint64_t __attribute__((aligned(8))) ugCoeff= 0xF36EF36EF36EF36ELL;
95 static uint64_t __attribute__((aligned(8))) bF8= 0xF8F8F8F8F8F8F8F8LL;
96 static uint64_t __attribute__((aligned(8))) bFC= 0xFCFCFCFCFCFCFCFCLL;
97 static uint64_t __attribute__((aligned(8))) w400= 0x0400040004000400LL;
98 static uint64_t __attribute__((aligned(8))) w80= 0x0080008000800080LL;
99 static uint64_t __attribute__((aligned(8))) w10= 0x0010001000100010LL;
100 static uint64_t __attribute__((aligned(8))) w02= 0x0002000200020002LL;
101 static uint64_t __attribute__((aligned(8))) bm00001111=0x00000000FFFFFFFFLL;
102 static uint64_t __attribute__((aligned(8))) bm00000111=0x0000000000FFFFFFLL;
103 static uint64_t __attribute__((aligned(8))) bm11111000=0xFFFFFFFFFF000000LL;
105 static volatile uint64_t __attribute__((aligned(8))) b5Dither;
106 static volatile uint64_t __attribute__((aligned(8))) g5Dither;
107 static volatile uint64_t __attribute__((aligned(8))) g6Dither;
108 static volatile uint64_t __attribute__((aligned(8))) r5Dither;
110 static uint64_t __attribute__((aligned(8))) dither4[2]={
111 0x0103010301030103LL,
112 0x0200020002000200LL,};
114 static uint64_t __attribute__((aligned(8))) dither8[2]={
115 0x0602060206020602LL,
116 0x0004000400040004LL,};
118 static uint64_t __attribute__((aligned(8))) b16Mask= 0x001F001F001F001FLL;
119 static uint64_t __attribute__((aligned(8))) g16Mask= 0x07E007E007E007E0LL;
120 static uint64_t __attribute__((aligned(8))) r16Mask= 0xF800F800F800F800LL;
121 static uint64_t __attribute__((aligned(8))) b15Mask= 0x001F001F001F001FLL;
122 static uint64_t __attribute__((aligned(8))) g15Mask= 0x03E003E003E003E0LL;
123 static uint64_t __attribute__((aligned(8))) r15Mask= 0x7C007C007C007C00LL;
125 static uint64_t __attribute__((aligned(8))) M24A= 0x00FF0000FF0000FFLL;
126 static uint64_t __attribute__((aligned(8))) M24B= 0xFF0000FF0000FF00LL;
127 static uint64_t __attribute__((aligned(8))) M24C= 0x0000FF0000FF0000LL;
130 static uint64_t __attribute__((aligned(8))) asm_yalpha1;
131 static uint64_t __attribute__((aligned(8))) asm_uvalpha1;
134 // clipping helper table for C implementations:
135 static unsigned char clip_table[768];
137 static unsigned short clip_table16b[768];
138 static unsigned short clip_table16g[768];
139 static unsigned short clip_table16r[768];
140 static unsigned short clip_table15b[768];
141 static unsigned short clip_table15g[768];
142 static unsigned short clip_table15r[768];
144 // yuv->rgb conversion tables:
145 static int yuvtab_2568[256];
146 static int yuvtab_3343[256];
147 static int yuvtab_0c92[256];
148 static int yuvtab_1a1e[256];
149 static int yuvtab_40cf[256];
150 // Needed for cubic scaler to catch overflows
151 static int clip_yuvtab_2568[768];
152 static int clip_yuvtab_3343[768];
153 static int clip_yuvtab_0c92[768];
154 static int clip_yuvtab_1a1e[768];
155 static int clip_yuvtab_40cf[768];
157 //global sws_flags from the command line
161 SwsFilter src_filter= {NULL, NULL, NULL, NULL};
163 float sws_lum_gblur= 0.0;
164 float sws_chr_gblur= 0.0;
165 int sws_chr_vshift= 0;
166 int sws_chr_hshift= 0;
167 float sws_chr_sharpen= 0.0;
168 float sws_lum_sharpen= 0.0;
170 /* cpuCaps combined from cpudetect and whats actually compiled in
171 (if there is no support for something compiled in it wont appear here) */
172 static CpuCaps cpuCaps;
174 void (*swScale)(SwsContext *context, uint8_t* src[], int srcStride[], int srcSliceY,
175 int srcSliceH, uint8_t* dst[], int dstStride[])=NULL;
177 static SwsVector *getConvVec(SwsVector *a, SwsVector *b);
179 #ifdef CAN_COMPILE_X86_ASM
180 void in_asm_used_var_warning_killer()
182 volatile int i= yCoeff+vrCoeff+ubCoeff+vgCoeff+ugCoeff+bF8+bFC+w400+w80+w10+
183 bm00001111+bm00000111+bm11111000+b16Mask+g16Mask+r16Mask+b15Mask+g15Mask+r15Mask+asm_yalpha1+ asm_uvalpha1+
184 M24A+M24B+M24C+w02 + b5Dither+g5Dither+r5Dither+g6Dither+dither4[0]+dither8[0];
189 static inline void yuv2yuvXinC(int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
190 int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
191 uint8_t *dest, uint8_t *uDest, uint8_t *vDest, int dstW)
193 //FIXME Optimize (just quickly writen not opti..)
195 for(i=0; i<dstW; i++)
199 for(j=0; j<lumFilterSize; j++)
200 val += lumSrc[j][i] * lumFilter[j];
202 dest[i]= MIN(MAX(val>>19, 0), 255);
206 for(i=0; i<(dstW>>1); i++)
211 for(j=0; j<chrFilterSize; j++)
213 u += chrSrc[j][i] * chrFilter[j];
214 v += chrSrc[j][i + 2048] * chrFilter[j];
217 uDest[i]= MIN(MAX(u>>19, 0), 255);
218 vDest[i]= MIN(MAX(v>>19, 0), 255);
222 static inline void yuv2rgbXinC(int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
223 int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
224 uint8_t *dest, int dstW, int dstFormat)
226 if(dstFormat==IMGFMT_BGR32)
229 for(i=0; i<(dstW>>1); i++){
236 for(j=0; j<lumFilterSize; j++)
238 Y1 += lumSrc[j][2*i] * lumFilter[j];
239 Y2 += lumSrc[j][2*i+1] * lumFilter[j];
241 for(j=0; j<chrFilterSize; j++)
243 U += chrSrc[j][i] * chrFilter[j];
244 V += chrSrc[j][i+2048] * chrFilter[j];
246 Y1= clip_yuvtab_2568[ (Y1>>19) + 256 ];
247 Y2= clip_yuvtab_2568[ (Y2>>19) + 256 ];
251 Cb= clip_yuvtab_40cf[U+ 256];
252 Cg= clip_yuvtab_1a1e[V+ 256] + yuvtab_0c92[U+ 256];
253 Cr= clip_yuvtab_3343[V+ 256];
255 dest[8*i+0]=clip_table[((Y1 + Cb) >>13)];
256 dest[8*i+1]=clip_table[((Y1 + Cg) >>13)];
257 dest[8*i+2]=clip_table[((Y1 + Cr) >>13)];
259 dest[8*i+4]=clip_table[((Y2 + Cb) >>13)];
260 dest[8*i+5]=clip_table[((Y2 + Cg) >>13)];
261 dest[8*i+6]=clip_table[((Y2 + Cr) >>13)];
264 else if(dstFormat==IMGFMT_BGR24)
267 for(i=0; i<(dstW>>1); i++){
274 for(j=0; j<lumFilterSize; j++)
276 Y1 += lumSrc[j][2*i] * lumFilter[j];
277 Y2 += lumSrc[j][2*i+1] * lumFilter[j];
279 for(j=0; j<chrFilterSize; j++)
281 U += chrSrc[j][i] * chrFilter[j];
282 V += chrSrc[j][i+2048] * chrFilter[j];
284 Y1= clip_yuvtab_2568[ (Y1>>19) + 256 ];
285 Y2= clip_yuvtab_2568[ (Y2>>19) + 256 ];
289 Cb= clip_yuvtab_40cf[U+ 256];
290 Cg= clip_yuvtab_1a1e[V+ 256] + yuvtab_0c92[U+ 256];
291 Cr= clip_yuvtab_3343[V+ 256];
293 dest[0]=clip_table[((Y1 + Cb) >>13)];
294 dest[1]=clip_table[((Y1 + Cg) >>13)];
295 dest[2]=clip_table[((Y1 + Cr) >>13)];
297 dest[3]=clip_table[((Y2 + Cb) >>13)];
298 dest[4]=clip_table[((Y2 + Cg) >>13)];
299 dest[5]=clip_table[((Y2 + Cr) >>13)];
303 else if(dstFormat==IMGFMT_BGR16)
307 static int ditherb1=1<<14;
308 static int ditherg1=1<<13;
309 static int ditherr1=2<<14;
310 static int ditherb2=3<<14;
311 static int ditherg2=3<<13;
312 static int ditherr2=0<<14;
314 ditherb1 ^= (1^2)<<14;
315 ditherg1 ^= (1^2)<<13;
316 ditherr1 ^= (1^2)<<14;
317 ditherb2 ^= (3^0)<<14;
318 ditherg2 ^= (3^0)<<13;
319 ditherr2 ^= (3^0)<<14;
321 const int ditherb1=0;
322 const int ditherg1=0;
323 const int ditherr1=0;
324 const int ditherb2=0;
325 const int ditherg2=0;
326 const int ditherr2=0;
328 for(i=0; i<(dstW>>1); i++){
335 for(j=0; j<lumFilterSize; j++)
337 Y1 += lumSrc[j][2*i] * lumFilter[j];
338 Y2 += lumSrc[j][2*i+1] * lumFilter[j];
340 for(j=0; j<chrFilterSize; j++)
342 U += chrSrc[j][i] * chrFilter[j];
343 V += chrSrc[j][i+2048] * chrFilter[j];
345 Y1= clip_yuvtab_2568[ (Y1>>19) + 256 ];
346 Y2= clip_yuvtab_2568[ (Y2>>19) + 256 ];
350 Cb= clip_yuvtab_40cf[U+ 256];
351 Cg= clip_yuvtab_1a1e[V+ 256] + yuvtab_0c92[U+ 256];
352 Cr= clip_yuvtab_3343[V+ 256];
354 ((uint16_t*)dest)[2*i] =
355 clip_table16b[(Y1 + Cb + ditherb1) >>13] |
356 clip_table16g[(Y1 + Cg + ditherg1) >>13] |
357 clip_table16r[(Y1 + Cr + ditherr1) >>13];
359 ((uint16_t*)dest)[2*i+1] =
360 clip_table16b[(Y2 + Cb + ditherb2) >>13] |
361 clip_table16g[(Y2 + Cg + ditherg2) >>13] |
362 clip_table16r[(Y2 + Cr + ditherr2) >>13];
365 else if(dstFormat==IMGFMT_BGR15)
369 static int ditherb1=1<<14;
370 static int ditherg1=1<<14;
371 static int ditherr1=2<<14;
372 static int ditherb2=3<<14;
373 static int ditherg2=3<<14;
374 static int ditherr2=0<<14;
376 ditherb1 ^= (1^2)<<14;
377 ditherg1 ^= (1^2)<<14;
378 ditherr1 ^= (1^2)<<14;
379 ditherb2 ^= (3^0)<<14;
380 ditherg2 ^= (3^0)<<14;
381 ditherr2 ^= (3^0)<<14;
383 const int ditherb1=0;
384 const int ditherg1=0;
385 const int ditherr1=0;
386 const int ditherb2=0;
387 const int ditherg2=0;
388 const int ditherr2=0;
390 for(i=0; i<(dstW>>1); i++){
397 for(j=0; j<lumFilterSize; j++)
399 Y1 += lumSrc[j][2*i] * lumFilter[j];
400 Y2 += lumSrc[j][2*i+1] * lumFilter[j];
402 for(j=0; j<chrFilterSize; j++)
404 U += chrSrc[j][i] * chrFilter[j];
405 V += chrSrc[j][i+2048] * chrFilter[j];
407 Y1= clip_yuvtab_2568[ (Y1>>19) + 256 ];
408 Y2= clip_yuvtab_2568[ (Y2>>19) + 256 ];
412 Cb= clip_yuvtab_40cf[U+ 256];
413 Cg= clip_yuvtab_1a1e[V+ 256] + yuvtab_0c92[U+ 256];
414 Cr= clip_yuvtab_3343[V+ 256];
416 ((uint16_t*)dest)[2*i] =
417 clip_table15b[(Y1 + Cb + ditherb1) >>13] |
418 clip_table15g[(Y1 + Cg + ditherg1) >>13] |
419 clip_table15r[(Y1 + Cr + ditherr1) >>13];
421 ((uint16_t*)dest)[2*i+1] =
422 clip_table15b[(Y2 + Cb + ditherb2) >>13] |
423 clip_table15g[(Y2 + Cg + ditherg2) >>13] |
424 clip_table15r[(Y2 + Cr + ditherr2) >>13];
430 //Note: we have C, X86, MMX, MMX2, 3DNOW version therse no 3DNOW+MMX2 one
432 #if !defined (HAVE_MMX) || defined (RUNTIME_CPUDETECT)
436 #ifdef CAN_COMPILE_X86_ASM
438 #if (defined (HAVE_MMX) && !defined (HAVE_3DNOW) && !defined (HAVE_MMX2)) || defined (RUNTIME_CPUDETECT)
442 #if defined (HAVE_MMX2) || defined (RUNTIME_CPUDETECT)
446 #if (defined (HAVE_3DNOW) && !defined (HAVE_MMX2)) || defined (RUNTIME_CPUDETECT)
447 #define COMPILE_3DNOW
449 #endif //CAN_COMPILE_X86_ASM
459 #define RENAME(a) a ## _C
460 #include "swscale_template.c"
463 #ifdef CAN_COMPILE_X86_ASM
472 #define RENAME(a) a ## _X86
473 #include "swscale_template.c"
481 #define RENAME(a) a ## _MMX
482 #include "swscale_template.c"
491 #define RENAME(a) a ## _MMX2
492 #include "swscale_template.c"
501 #define RENAME(a) a ## _3DNow
502 #include "swscale_template.c"
505 #endif //CAN_COMPILE_X86_ASM
507 // minor note: the HAVE_xyz is messed up after that line so dont use it
510 // old global scaler, dont use for new code, unless it uses only the stuff from the command line
511 // will use sws_flags from the command line
512 void SwScale_YV12slice(unsigned char* src[], int srcStride[], int srcSliceY ,
513 int srcSliceH, uint8_t* dst[], int dstStride, int dstbpp,
514 int srcW, int srcH, int dstW, int dstH){
516 static SwsContext *context=NULL;
519 static int firstTime=1;
520 int dstStride3[3]= {dstStride, dstStride>>1, dstStride>>1};
526 asm volatile("emms\n\t"::: "memory"); //FIXME this shouldnt be required but it IS (even for non mmx versions)
528 flags= SWS_PRINT_INFO;
531 if(src_filter.lumH) freeVec(src_filter.lumH);
532 if(src_filter.lumV) freeVec(src_filter.lumV);
533 if(src_filter.chrH) freeVec(src_filter.chrH);
534 if(src_filter.chrV) freeVec(src_filter.chrV);
536 if(sws_lum_gblur!=0.0){
537 src_filter.lumH= getGaussianVec(sws_lum_gblur, 3.0);
538 src_filter.lumV= getGaussianVec(sws_lum_gblur, 3.0);
540 src_filter.lumH= getIdentityVec();
541 src_filter.lumV= getIdentityVec();
544 if(sws_chr_gblur!=0.0){
545 src_filter.chrH= getGaussianVec(sws_chr_gblur, 3.0);
546 src_filter.chrV= getGaussianVec(sws_chr_gblur, 3.0);
548 src_filter.chrH= getIdentityVec();
549 src_filter.chrV= getIdentityVec();
552 if(sws_chr_sharpen!=0.0){
553 SwsVector *g= getConstVec(-1.0, 3);
554 SwsVector *id= getConstVec(10.0/sws_chr_sharpen, 1);
557 convVec(src_filter.chrH, id);
558 convVec(src_filter.chrV, id);
563 if(sws_lum_sharpen!=0.0){
564 SwsVector *g= getConstVec(-1.0, 3);
565 SwsVector *id= getConstVec(10.0/sws_lum_sharpen, 1);
568 convVec(src_filter.lumH, id);
569 convVec(src_filter.lumV, id);
575 shiftVec(src_filter.chrH, sws_chr_hshift);
578 shiftVec(src_filter.chrV, sws_chr_vshift);
580 normalizeVec(src_filter.chrH, 1.0);
581 normalizeVec(src_filter.chrV, 1.0);
582 normalizeVec(src_filter.lumH, 1.0);
583 normalizeVec(src_filter.lumV, 1.0);
585 if(verbose > 1) printVec(src_filter.chrH);
586 if(verbose > 1) printVec(src_filter.lumH);
591 case 8 : dstFormat= IMGFMT_Y8; break;
592 case 12: dstFormat= IMGFMT_YV12; break;
593 case 15: dstFormat= IMGFMT_BGR15; break;
594 case 16: dstFormat= IMGFMT_BGR16; break;
595 case 24: dstFormat= IMGFMT_BGR24; break;
596 case 32: dstFormat= IMGFMT_BGR32; break;
602 case 0: flags|= SWS_FAST_BILINEAR; break;
603 case 1: flags|= SWS_BILINEAR; break;
604 case 2: flags|= SWS_BICUBIC; break;
605 case 3: flags|= SWS_X; break;
606 case 4: flags|= SWS_POINT; break;
607 case 5: flags|= SWS_AREA; break;
608 default:flags|= SWS_BILINEAR; break;
611 if(!context) context=getSwsContext(srcW, srcH, IMGFMT_YV12, dstW, dstH, dstFormat, flags, &src_filter, NULL);
614 swScale(context, src, srcStride, srcSliceY, srcSliceH, dst, dstStride3);
617 static inline void initFilter(int16_t **outFilter, int16_t **filterPos, int *outFilterSize, int xInc,
618 int srcW, int dstW, int filterAlign, int one, int flags,
619 SwsVector *srcFilter, SwsVector *dstFilter)
626 double *filter2=NULL;
629 asm volatile("emms\n\t"::: "memory"); //FIXME this shouldnt be required but it IS (even for non mmx versions)
632 *filterPos = (int16_t*)memalign(8, dstW*sizeof(int16_t));
633 if(ABS(xInc - 0x10000) <10) // unscaled
637 filter= (double*)memalign(8, dstW*sizeof(double)*filterSize);
638 for(i=0; i<dstW*filterSize; i++) filter[i]=0;
640 for(i=0; i<dstW; i++)
642 filter[i*filterSize]=1;
647 else if(flags&SWS_POINT) // lame looking point sampling mode
652 filter= (double*)memalign(8, dstW*sizeof(double)*filterSize);
654 xDstInSrc= xInc/2 - 0x8000;
655 for(i=0; i<dstW; i++)
657 int xx= (xDstInSrc>>16) - (filterSize>>1) + 1;
664 else if(xInc <= (1<<16) || (flags&SWS_FAST_BILINEAR)) // upscale
668 if (flags&SWS_BICUBIC) filterSize= 4;
669 else if(flags&SWS_X ) filterSize= 4;
670 else filterSize= 2; // SWS_BILINEAR / SWS_AREA
671 // printf("%d %d %d\n", filterSize, srcW, dstW);
672 filter= (double*)memalign(8, dstW*sizeof(double)*filterSize);
674 xDstInSrc= xInc/2 - 0x8000;
675 for(i=0; i<dstW; i++)
677 int xx= (xDstInSrc>>16) - (filterSize>>1) + 1;
681 if((flags & SWS_BICUBIC) || (flags & SWS_X))
683 double d= ABS(((xx+1)<<16) - xDstInSrc)/(double)(1<<16);
686 if(flags & SWS_BICUBIC){
687 // Equation is from VirtualDub
688 y1 = ( + A*d - 2.0*A*d*d + A*d*d*d);
689 y2 = (+ 1.0 - (A+3.0)*d*d + (A+2.0)*d*d*d);
690 y3 = ( - A*d + (2.0*A+3.0)*d*d - (A+2.0)*d*d*d);
691 y4 = ( + A*d*d - A*d*d*d);
693 // cubic interpolation (derived it myself)
694 y1 = ( -2.0*d + 3.0*d*d - 1.0*d*d*d)/6.0;
695 y2 = (6.0 -3.0*d - 6.0*d*d + 3.0*d*d*d)/6.0;
696 y3 = ( +6.0*d + 3.0*d*d - 3.0*d*d*d)/6.0;
697 y4 = ( -1.0*d + 1.0*d*d*d)/6.0;
700 // printf("%d %d %d \n", coeff, (int)d, xDstInSrc);
701 filter[i*filterSize + 0]= y1;
702 filter[i*filterSize + 1]= y2;
703 filter[i*filterSize + 2]= y3;
704 filter[i*filterSize + 3]= y4;
705 // printf("%1.3f %1.3f %1.3f %1.3f %1.3f\n",d , y1, y2, y3, y4);
709 //Bilinear upscale / linear interpolate / Area averaging
710 for(j=0; j<filterSize; j++)
712 double d= ABS((xx<<16) - xDstInSrc)/(double)(1<<16);
713 double coeff= 1.0 - d;
715 // printf("%d %d %d \n", coeff, (int)d, xDstInSrc);
716 filter[i*filterSize + j]= coeff;
726 if(flags&SWS_BICUBIC) filterSize= (int)ceil(1 + 4.0*srcW / (double)dstW);
727 else if(flags&SWS_X) filterSize= (int)ceil(1 + 4.0*srcW / (double)dstW);
728 else if(flags&SWS_AREA) filterSize= (int)ceil(1 + 1.0*srcW / (double)dstW);
729 else /* BILINEAR */ filterSize= (int)ceil(1 + 2.0*srcW / (double)dstW);
730 // printf("%d %d %d\n", *filterSize, srcW, dstW);
731 filter= (double*)memalign(8, dstW*sizeof(double)*filterSize);
733 xDstInSrc= xInc/2 - 0x8000;
734 for(i=0; i<dstW; i++)
736 int xx= (int)((double)xDstInSrc/(double)(1<<16) - (filterSize-1)*0.5 + 0.5);
739 for(j=0; j<filterSize; j++)
741 double d= ABS((xx<<16) - xDstInSrc)/(double)xInc;
743 if((flags & SWS_BICUBIC) || (flags & SWS_X))
747 // Equation is from VirtualDub
749 coeff = (1.0 - (A+3.0)*d*d + (A+2.0)*d*d*d);
751 coeff = (-4.0*A + 8.0*A*d - 5.0*A*d*d + A*d*d*d);
755 else if(flags & SWS_AREA)
757 double srcPixelSize= (1<<16)/(double)xInc;
758 if(d + srcPixelSize/2 < 0.5) coeff= 1.0;
759 else if(d - srcPixelSize/2 < 0.5) coeff= (0.5-d)/srcPixelSize + 0.5;
767 // printf("%1.3f %2.3f %d \n", coeff, d, xDstInSrc);
768 filter[i*filterSize + j]= coeff;
775 /* apply src & dst Filter to filter -> filter2
778 filter2Size= filterSize;
779 if(srcFilter) filter2Size+= srcFilter->length - 1;
780 if(dstFilter) filter2Size+= dstFilter->length - 1;
781 filter2= (double*)memalign(8, filter2Size*dstW*sizeof(double));
783 for(i=0; i<dstW; i++)
786 SwsVector scaleFilter;
789 scaleFilter.coeff= filter + i*filterSize;
790 scaleFilter.length= filterSize;
792 if(srcFilter) outVec= getConvVec(srcFilter, &scaleFilter);
793 else outVec= &scaleFilter;
795 ASSERT(outVec->length == filter2Size)
798 for(j=0; j<outVec->length; j++)
800 filter2[i*filter2Size + j]= outVec->coeff[j];
803 (*filterPos)[i]+= (filterSize-1)/2 - (filter2Size-1)/2;
805 if(outVec != &scaleFilter) freeVec(outVec);
807 free(filter); filter=NULL;
809 /* try to reduce the filter-size (step1 find size and shift left) */
810 // Assume its near normalized (*0.5 or *2.0 is ok but * 0.001 is not)
812 for(i=dstW-1; i>=0; i--)
814 int min= filter2Size;
818 /* get rid off near zero elements on the left by shifting left */
819 for(j=0; j<filter2Size; j++)
822 cutOff += ABS(filter2[i*filter2Size]);
824 if(cutOff > SWS_MAX_REDUCE_CUTOFF) break;
826 /* preserve Monotonicity because the core cant handle the filter otherwise */
827 if(i<dstW-1 && (*filterPos)[i] >= (*filterPos)[i+1]) break;
829 // Move filter coeffs left
830 for(k=1; k<filter2Size; k++)
831 filter2[i*filter2Size + k - 1]= filter2[i*filter2Size + k];
832 filter2[i*filter2Size + k - 1]= 0.0;
837 /* count near zeros on the right */
838 for(j=filter2Size-1; j>0; j--)
840 cutOff += ABS(filter2[i*filter2Size + j]);
842 if(cutOff > SWS_MAX_REDUCE_CUTOFF) break;
846 if(min>minFilterSize) minFilterSize= min;
849 /* try to reduce the filter-size (step2 reduce it) */
850 for(i=0; i<dstW; i++)
854 for(j=0; j<minFilterSize; j++)
855 filter2[i*minFilterSize + j]= filter2[i*filter2Size + j];
857 if((flags&SWS_PRINT_INFO) && verbose)
858 printf("SwScaler: reducing filtersize %d -> %d\n", filter2Size, minFilterSize);
859 filter2Size= minFilterSize;
860 ASSERT(filter2Size > 0)
862 //FIXME try to align filterpos if possible
865 for(i=0; i<dstW; i++)
868 if((*filterPos)[i] < 0)
870 // Move filter coeffs left to compensate for filterPos
871 for(j=1; j<filter2Size; j++)
873 int left= MAX(j + (*filterPos)[i], 0);
874 filter2[i*filter2Size + left] += filter2[i*filter2Size + j];
875 filter2[i*filter2Size + j]=0;
880 if((*filterPos)[i] + filter2Size > srcW)
882 int shift= (*filterPos)[i] + filter2Size - srcW;
883 // Move filter coeffs right to compensate for filterPos
884 for(j=filter2Size-2; j>=0; j--)
886 int right= MIN(j + shift, filter2Size-1);
887 filter2[i*filter2Size +right] += filter2[i*filter2Size +j];
888 filter2[i*filter2Size +j]=0;
890 (*filterPos)[i]= srcW - filter2Size;
895 *outFilterSize= (filter2Size +(filterAlign-1)) & (~(filterAlign-1));
896 *outFilter= (int16_t*)memalign(8, *outFilterSize*dstW*sizeof(int16_t));
897 memset(*outFilter, 0, *outFilterSize*dstW*sizeof(int16_t));
899 /* Normalize & Store in outFilter */
900 for(i=0; i<dstW; i++)
905 for(j=0; j<filter2Size; j++)
907 sum+= filter2[i*filter2Size + j];
910 for(j=0; j<filter2Size; j++)
912 (*outFilter)[i*(*outFilterSize) + j]= (int)(filter2[i*filter2Size + j]*scale);
920 static void initMMX2HScaler(int dstW, int xInc, uint8_t *funnyCode)
929 // create an optimized horizontal scaling routine
937 "movq (%%esi), %%mm0 \n\t" //FIXME Alignment
938 "movq %%mm0, %%mm1 \n\t"
939 "psrlq $8, %%mm0 \n\t"
940 "punpcklbw %%mm7, %%mm1 \n\t"
941 "movq %%mm2, %%mm3 \n\t"
942 "punpcklbw %%mm7, %%mm0 \n\t"
943 "addw %%bx, %%cx \n\t" //2*xalpha += (4*lumXInc)&0xFFFF
944 "pshufw $0xFF, %%mm1, %%mm1 \n\t"
946 "adcl %%edx, %%esi \n\t" //xx+= (4*lumXInc)>>16 + carry
947 "pshufw $0xFF, %%mm0, %%mm0 \n\t"
949 "psrlw $9, %%mm3 \n\t"
950 "psubw %%mm1, %%mm0 \n\t"
951 "pmullw %%mm3, %%mm0 \n\t"
952 "paddw %%mm6, %%mm2 \n\t" // 2*alpha += xpos&0xFFFF
953 "psllw $7, %%mm1 \n\t"
954 "paddw %%mm1, %%mm0 \n\t"
956 "movq %%mm0, (%%edi, %%eax) \n\t"
958 "addl $8, %%eax \n\t"
971 :"=r" (fragment), "=r" (imm8OfPShufW1), "=r" (imm8OfPShufW2),
972 "=r" (fragmentLength)
975 xpos= 0; //lumXInc/2 - 0x8000; // difference between pixel centers
977 for(i=0; i<dstW/8; i++)
984 int b=((xpos+xInc)>>16) - xx;
985 int c=((xpos+xInc*2)>>16) - xx;
986 int d=((xpos+xInc*3)>>16) - xx;
988 memcpy(funnyCode + fragmentLength*i/4, fragment, fragmentLength);
990 funnyCode[fragmentLength*i/4 + imm8OfPShufW1]=
991 funnyCode[fragmentLength*i/4 + imm8OfPShufW2]=
992 a | (b<<2) | (c<<4) | (d<<6);
994 // if we dont need to read 8 bytes than dont :), reduces the chance of
995 // crossing a cache line
996 if(d<3) funnyCode[fragmentLength*i/4 + 1]= 0x6E;
998 funnyCode[fragmentLength*(i+4)/4]= RET;
1006 void SwScale_Init(){
1009 static void globalInit(){
1010 // generating tables:
1012 for(i=0; i<768; i++){
1013 int c= MIN(MAX(i-256, 0), 255);
1015 yuvtab_2568[c]= clip_yuvtab_2568[i]=(0x2568*(c-16))+(256<<13);
1016 yuvtab_3343[c]= clip_yuvtab_3343[i]=0x3343*(c-128);
1017 yuvtab_0c92[c]= clip_yuvtab_0c92[i]=-0x0c92*(c-128);
1018 yuvtab_1a1e[c]= clip_yuvtab_1a1e[i]=-0x1a1e*(c-128);
1019 yuvtab_40cf[c]= clip_yuvtab_40cf[i]=0x40cf*(c-128);
1022 for(i=0; i<768; i++)
1024 int v= clip_table[i];
1025 clip_table16b[i]= v>>3;
1026 clip_table16g[i]= (v<<3)&0x07E0;
1027 clip_table16r[i]= (v<<8)&0xF800;
1028 clip_table15b[i]= v>>3;
1029 clip_table15g[i]= (v<<2)&0x03E0;
1030 clip_table15r[i]= (v<<7)&0x7C00;
1035 #ifdef RUNTIME_CPUDETECT
1036 #ifdef CAN_COMPILE_X86_ASM
1037 // ordered per speed fasterst first
1038 if(gCpuCaps.hasMMX2)
1039 swScale= swScale_MMX2;
1040 else if(gCpuCaps.has3DNow)
1041 swScale= swScale_3DNow;
1042 else if(gCpuCaps.hasMMX)
1043 swScale= swScale_MMX;
1049 cpuCaps.hasMMX2 = cpuCaps.hasMMX = cpuCaps.has3DNow = 0;
1051 #else //RUNTIME_CPUDETECT
1053 swScale= swScale_MMX2;
1054 cpuCaps.has3DNow = 0;
1055 #elif defined (HAVE_3DNOW)
1056 swScale= swScale_3DNow;
1057 cpuCaps.hasMMX2 = 0;
1058 #elif defined (HAVE_MMX)
1059 swScale= swScale_MMX;
1060 cpuCaps.hasMMX2 = cpuCaps.has3DNow = 0;
1063 cpuCaps.hasMMX2 = cpuCaps.hasMMX = cpuCaps.has3DNow = 0;
1065 #endif //!RUNTIME_CPUDETECT
1069 SwsContext *getSwsContext(int srcW, int srcH, int srcFormat, int dstW, int dstH, int dstFormat, int flags,
1070 SwsFilter *srcFilter, SwsFilter *dstFilter){
1072 const int widthAlign= dstFormat==IMGFMT_YV12 ? 16 : 8;
1075 SwsFilter dummyFilter= {NULL, NULL, NULL, NULL};
1079 asm volatile("emms\n\t"::: "memory");
1082 if(swScale==NULL) globalInit();
1085 if(srcW<1 || srcH<1 || dstW<1 || dstH<1) return NULL;
1088 if(dstStride[0]%widthAlign !=0 )
1090 if(flags & SWS_PRINT_INFO)
1091 fprintf(stderr, "SwScaler: Warning: dstStride is not a multiple of %d!\n"
1092 "SwScaler: ->cannot do aligned memory acesses anymore\n",
1096 if(!dstFilter) dstFilter= &dummyFilter;
1097 if(!srcFilter) srcFilter= &dummyFilter;
1099 c= memalign(64, sizeof(SwsContext));
1100 memset(c, 0, sizeof(SwsContext));
1106 c->lumXInc= ((srcW<<16) + (dstW>>1))/dstW;
1107 c->lumYInc= ((srcH<<16) + (dstH>>1))/dstH;
1109 c->dstFormat= dstFormat;
1110 c->srcFormat= srcFormat;
1114 c->canMMX2BeUsed= (dstW >=srcW && (dstW&31)==0 && (srcW&15)==0) ? 1 : 0;
1115 if(!c->canMMX2BeUsed && dstW >=srcW && (srcW&15)==0 && (flags&SWS_FAST_BILINEAR))
1117 if(flags&SWS_PRINT_INFO)
1118 fprintf(stderr, "SwScaler: output Width is not a multiple of 32 -> no MMX2 scaler\n");
1124 // match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src to pixel n-2 of dst
1125 // but only for the FAST_BILINEAR mode otherwise do correct scaling
1126 // n-2 is the last chrominance sample available
1127 // this is not perfect, but noone shuld notice the difference, the more correct variant
1128 // would be like the vertical one, but that would require some special code for the
1129 // first and last pixel
1130 if(flags&SWS_FAST_BILINEAR)
1132 if(c->canMMX2BeUsed) c->lumXInc+= 20;
1133 //we dont use the x86asm scaler if mmx is available
1134 else if(cpuCaps.hasMMX) c->lumXInc = ((srcW-2)<<16)/(dstW-2) - 20;
1137 /* set chrXInc & chrDstW */
1138 if((flags&SWS_FULL_UV_IPOL) && dstFormat!=IMGFMT_YV12)
1139 c->chrXInc= c->lumXInc>>1, c->chrDstW= dstW;
1141 c->chrXInc= c->lumXInc, c->chrDstW= (dstW+1)>>1;
1143 /* set chrYInc & chrDstH */
1144 if(dstFormat==IMGFMT_YV12) c->chrYInc= c->lumYInc, c->chrDstH= (dstH+1)>>1;
1145 else c->chrYInc= c->lumYInc>>1, c->chrDstH= dstH;
1147 /* precalculate horizontal scaler filter coefficients */
1149 const int filterAlign= cpuCaps.hasMMX ? 4 : 1;
1151 initFilter(&c->hLumFilter, &c->hLumFilterPos, &c->hLumFilterSize, c->lumXInc,
1152 srcW , dstW, filterAlign, 1<<14, flags,
1153 srcFilter->lumH, dstFilter->lumH);
1154 initFilter(&c->hChrFilter, &c->hChrFilterPos, &c->hChrFilterSize, c->chrXInc,
1155 (srcW+1)>>1, c->chrDstW, filterAlign, 1<<14, flags,
1156 srcFilter->chrH, dstFilter->chrH);
1159 // cant downscale !!!
1160 if(c->canMMX2BeUsed && (flags & SWS_FAST_BILINEAR))
1162 initMMX2HScaler( dstW, c->lumXInc, c->funnyYCode);
1163 initMMX2HScaler(c->chrDstW, c->chrXInc, c->funnyUVCode);
1166 } // Init Horizontal stuff
1170 /* precalculate vertical scaler filter coefficients */
1171 initFilter(&c->vLumFilter, &c->vLumFilterPos, &c->vLumFilterSize, c->lumYInc,
1172 srcH , dstH, 1, (1<<12)-4, flags,
1173 srcFilter->lumV, dstFilter->lumV);
1174 initFilter(&c->vChrFilter, &c->vChrFilterPos, &c->vChrFilterSize, c->chrYInc,
1175 (srcH+1)>>1, c->chrDstH, 1, (1<<12)-4, flags,
1176 srcFilter->chrV, dstFilter->chrV);
1178 // Calculate Buffer Sizes so that they wont run out while handling these damn slices
1179 c->vLumBufSize= c->vLumFilterSize;
1180 c->vChrBufSize= c->vChrFilterSize;
1181 for(i=0; i<dstH; i++)
1183 int chrI= i*c->chrDstH / dstH;
1184 int nextSlice= MAX(c->vLumFilterPos[i ] + c->vLumFilterSize - 1,
1185 ((c->vChrFilterPos[chrI] + c->vChrFilterSize - 1)<<1));
1186 nextSlice&= ~1; // Slices start at even boundaries
1187 if(c->vLumFilterPos[i ] + c->vLumBufSize < nextSlice)
1188 c->vLumBufSize= nextSlice - c->vLumFilterPos[i ];
1189 if(c->vChrFilterPos[chrI] + c->vChrBufSize < (nextSlice>>1))
1190 c->vChrBufSize= (nextSlice>>1) - c->vChrFilterPos[chrI];
1193 // allocate pixbufs (we use dynamic allocation because otherwise we would need to
1194 // allocate several megabytes to handle all possible cases)
1195 c->lumPixBuf= (int16_t**)memalign(4, c->vLumBufSize*2*sizeof(int16_t*));
1196 c->chrPixBuf= (int16_t**)memalign(4, c->vChrBufSize*2*sizeof(int16_t*));
1197 for(i=0; i<c->vLumBufSize; i++)
1198 c->lumPixBuf[i]= c->lumPixBuf[i+c->vLumBufSize]= (uint16_t*)memalign(8, 4000);
1199 for(i=0; i<c->vChrBufSize; i++)
1200 c->chrPixBuf[i]= c->chrPixBuf[i+c->vChrBufSize]= (uint16_t*)memalign(8, 8000);
1202 //try to avoid drawing green stuff between the right end and the stride end
1203 for(i=0; i<c->vLumBufSize; i++) memset(c->lumPixBuf[i], 0, 4000);
1204 for(i=0; i<c->vChrBufSize; i++) memset(c->chrPixBuf[i], 64, 8000);
1206 ASSERT(c->chrDstH <= dstH)
1208 // pack filter data for mmx code
1211 c->lumMmxFilter= (int16_t*)memalign(8, c->vLumFilterSize* dstH*4*sizeof(int16_t));
1212 c->chrMmxFilter= (int16_t*)memalign(8, c->vChrFilterSize*c->chrDstH*4*sizeof(int16_t));
1213 for(i=0; i<c->vLumFilterSize*dstH; i++)
1214 c->lumMmxFilter[4*i]=c->lumMmxFilter[4*i+1]=c->lumMmxFilter[4*i+2]=c->lumMmxFilter[4*i+3]=
1216 for(i=0; i<c->vChrFilterSize*c->chrDstH; i++)
1217 c->chrMmxFilter[4*i]=c->chrMmxFilter[4*i+1]=c->chrMmxFilter[4*i+2]=c->chrMmxFilter[4*i+3]=
1221 if(flags&SWS_PRINT_INFO)
1224 char *dither= " dithered";
1228 if(flags&SWS_FAST_BILINEAR)
1229 fprintf(stderr, "\nSwScaler: FAST_BILINEAR scaler ");
1230 else if(flags&SWS_BILINEAR)
1231 fprintf(stderr, "\nSwScaler: BILINEAR scaler ");
1232 else if(flags&SWS_BICUBIC)
1233 fprintf(stderr, "\nSwScaler: BICUBIC scaler ");
1234 else if(flags&SWS_POINT)
1235 fprintf(stderr, "\nSwScaler: Nearest Neighbor / POINT scaler ");
1236 else if(flags&SWS_AREA)
1237 fprintf(stderr, "\nSwScaler: Area Averageing scaler ");
1239 fprintf(stderr, "\nSwScaler: ehh flags invalid?! ");
1241 if(dstFormat==IMGFMT_BGR15)
1242 fprintf(stderr, "with%s BGR15 output ", dither);
1243 else if(dstFormat==IMGFMT_BGR16)
1244 fprintf(stderr, "with%s BGR16 output ", dither);
1245 else if(dstFormat==IMGFMT_BGR24)
1246 fprintf(stderr, "with BGR24 output ");
1247 else if(dstFormat==IMGFMT_BGR32)
1248 fprintf(stderr, "with BGR32 output ");
1249 else if(dstFormat==IMGFMT_YV12)
1250 fprintf(stderr, "with YV12 output ");
1252 fprintf(stderr, "without output ");
1255 fprintf(stderr, "using MMX2\n");
1256 else if(cpuCaps.has3DNow)
1257 fprintf(stderr, "using 3DNOW\n");
1258 else if(cpuCaps.hasMMX)
1259 fprintf(stderr, "using MMX\n");
1261 fprintf(stderr, "using C\n");
1264 if((flags & SWS_PRINT_INFO) && verbose)
1268 if(c->canMMX2BeUsed && (flags&SWS_FAST_BILINEAR))
1269 printf("SwScaler: using FAST_BILINEAR MMX2 scaler for horizontal scaling\n");
1272 if(c->hLumFilterSize==4)
1273 printf("SwScaler: using 4-tap MMX scaler for horizontal luminance scaling\n");
1274 else if(c->hLumFilterSize==8)
1275 printf("SwScaler: using 8-tap MMX scaler for horizontal luminance scaling\n");
1277 printf("SwScaler: using n-tap MMX scaler for horizontal luminance scaling\n");
1279 if(c->hChrFilterSize==4)
1280 printf("SwScaler: using 4-tap MMX scaler for horizontal chrominance scaling\n");
1281 else if(c->hChrFilterSize==8)
1282 printf("SwScaler: using 8-tap MMX scaler for horizontal chrominance scaling\n");
1284 printf("SwScaler: using n-tap MMX scaler for horizontal chrominance scaling\n");
1290 printf("SwScaler: using X86-Asm scaler for horizontal scaling\n");
1292 if(flags & SWS_FAST_BILINEAR)
1293 printf("SwScaler: using FAST_BILINEAR C scaler for horizontal scaling\n");
1295 printf("SwScaler: using C scaler for horizontal scaling\n");
1298 if(dstFormat==IMGFMT_YV12)
1300 if(c->vLumFilterSize==1)
1301 printf("SwScaler: using 1-tap %s \"scaler\" for vertical scaling (YV12)\n", cpuCaps.hasMMX ? "MMX" : "C");
1303 printf("SwScaler: using n-tap %s scaler for vertical scaling (YV12)\n", cpuCaps.hasMMX ? "MMX" : "C");
1307 if(c->vLumFilterSize==1 && c->vChrFilterSize==2)
1308 printf("SwScaler: using 1-tap %s \"scaler\" for vertical luminance scaling (BGR)\n"
1309 "SwScaler: 2-tap scaler for vertical chrominance scaling (BGR)\n",cpuCaps.hasMMX ? "MMX" : "C");
1310 else if(c->vLumFilterSize==2 && c->vChrFilterSize==2)
1311 printf("SwScaler: using 2-tap linear %s scaler for vertical scaling (BGR)\n", cpuCaps.hasMMX ? "MMX" : "C");
1313 printf("SwScaler: using n-tap %s scaler for vertical scaling (BGR)\n", cpuCaps.hasMMX ? "MMX" : "C");
1316 if(dstFormat==IMGFMT_BGR24)
1317 printf("SwScaler: using %s YV12->BGR24 Converter\n",
1318 cpuCaps.hasMMX2 ? "MMX2" : (cpuCaps.hasMMX ? "MMX" : "C"));
1319 else if(dstFormat==IMGFMT_BGR32)
1320 printf("SwScaler: using %s YV12->BGR32 Converter\n", cpuCaps.hasMMX ? "MMX" : "C");
1321 else if(dstFormat==IMGFMT_BGR16)
1322 printf("SwScaler: using %s YV12->BGR16 Converter\n", cpuCaps.hasMMX ? "MMX" : "C");
1323 else if(dstFormat==IMGFMT_BGR15)
1324 printf("SwScaler: using %s YV12->BGR15 Converter\n", cpuCaps.hasMMX ? "MMX" : "C");
1326 printf("SwScaler: %dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
1333 * returns a normalized gaussian curve used to filter stuff
1334 * quality=3 is high quality, lowwer is lowwer quality
1337 SwsVector *getGaussianVec(double variance, double quality){
1338 const int length= (int)(variance*quality + 0.5) | 1;
1340 double *coeff= memalign(sizeof(double), length*sizeof(double));
1341 double middle= (length-1)*0.5;
1342 SwsVector *vec= malloc(sizeof(SwsVector));
1345 vec->length= length;
1347 for(i=0; i<length; i++)
1349 double dist= i-middle;
1350 coeff[i]= exp( -dist*dist/(2*variance*variance) ) / sqrt(2*variance*PI);
1353 normalizeVec(vec, 1.0);
1358 SwsVector *getConstVec(double c, int length){
1360 double *coeff= memalign(sizeof(double), length*sizeof(double));
1361 SwsVector *vec= malloc(sizeof(SwsVector));
1364 vec->length= length;
1366 for(i=0; i<length; i++)
1373 SwsVector *getIdentityVec(void){
1374 double *coeff= memalign(sizeof(double), sizeof(double));
1375 SwsVector *vec= malloc(sizeof(SwsVector));
1384 void normalizeVec(SwsVector *a, double height){
1389 for(i=0; i<a->length; i++)
1394 for(i=0; i<a->length; i++)
1395 a->coeff[i]*= height;
1398 void scaleVec(SwsVector *a, double scalar){
1401 for(i=0; i<a->length; i++)
1402 a->coeff[i]*= scalar;
1405 static SwsVector *getConvVec(SwsVector *a, SwsVector *b){
1406 int length= a->length + b->length - 1;
1407 double *coeff= memalign(sizeof(double), length*sizeof(double));
1409 SwsVector *vec= malloc(sizeof(SwsVector));
1412 vec->length= length;
1414 for(i=0; i<length; i++) coeff[i]= 0.0;
1416 for(i=0; i<a->length; i++)
1418 for(j=0; j<b->length; j++)
1420 coeff[i+j]+= a->coeff[i]*b->coeff[j];
1427 static SwsVector *sumVec(SwsVector *a, SwsVector *b){
1428 int length= MAX(a->length, b->length);
1429 double *coeff= memalign(sizeof(double), length*sizeof(double));
1431 SwsVector *vec= malloc(sizeof(SwsVector));
1434 vec->length= length;
1436 for(i=0; i<length; i++) coeff[i]= 0.0;
1438 for(i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
1439 for(i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]+= b->coeff[i];
1444 static SwsVector *diffVec(SwsVector *a, SwsVector *b){
1445 int length= MAX(a->length, b->length);
1446 double *coeff= memalign(sizeof(double), length*sizeof(double));
1448 SwsVector *vec= malloc(sizeof(SwsVector));
1451 vec->length= length;
1453 for(i=0; i<length; i++) coeff[i]= 0.0;
1455 for(i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
1456 for(i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]-= b->coeff[i];
1461 /* shift left / or right if "shift" is negative */
1462 static SwsVector *getShiftedVec(SwsVector *a, int shift){
1463 int length= a->length + ABS(shift)*2;
1464 double *coeff= memalign(sizeof(double), length*sizeof(double));
1466 SwsVector *vec= malloc(sizeof(SwsVector));
1469 vec->length= length;
1471 for(i=0; i<length; i++) coeff[i]= 0.0;
1473 for(i=0; i<a->length; i++)
1475 coeff[i + (length-1)/2 - (a->length-1)/2 - shift]= a->coeff[i];
1481 void shiftVec(SwsVector *a, int shift){
1482 SwsVector *shifted= getShiftedVec(a, shift);
1484 a->coeff= shifted->coeff;
1485 a->length= shifted->length;
1489 void addVec(SwsVector *a, SwsVector *b){
1490 SwsVector *sum= sumVec(a, b);
1492 a->coeff= sum->coeff;
1493 a->length= sum->length;
1497 void subVec(SwsVector *a, SwsVector *b){
1498 SwsVector *diff= diffVec(a, b);
1500 a->coeff= diff->coeff;
1501 a->length= diff->length;
1505 void convVec(SwsVector *a, SwsVector *b){
1506 SwsVector *conv= getConvVec(a, b);
1508 a->coeff= conv->coeff;
1509 a->length= conv->length;
1513 SwsVector *cloneVec(SwsVector *a){
1514 double *coeff= memalign(sizeof(double), a->length*sizeof(double));
1516 SwsVector *vec= malloc(sizeof(SwsVector));
1519 vec->length= a->length;
1521 for(i=0; i<a->length; i++) coeff[i]= a->coeff[i];
1526 void printVec(SwsVector *a){
1532 for(i=0; i<a->length; i++)
1533 if(a->coeff[i]>max) max= a->coeff[i];
1535 for(i=0; i<a->length; i++)
1536 if(a->coeff[i]<min) min= a->coeff[i];
1540 for(i=0; i<a->length; i++)
1542 int x= (int)((a->coeff[i]-min)*60.0/range +0.5);
1543 printf("%1.3f ", a->coeff[i]);
1544 for(;x>0; x--) printf(" ");
1549 void freeVec(SwsVector *a){
1551 if(a->coeff) free(a->coeff);
1557 void freeSwsContext(SwsContext *c){
1564 for(i=0; i<c->vLumBufSize*2; i++)
1566 if(c->lumPixBuf[i]) free(c->lumPixBuf[i]);
1567 c->lumPixBuf[i]=NULL;
1575 for(i=0; i<c->vChrBufSize*2; i++)
1577 if(c->chrPixBuf[i]) free(c->chrPixBuf[i]);
1578 c->chrPixBuf[i]=NULL;
1584 if(c->vLumFilter) free(c->vLumFilter);
1585 c->vLumFilter = NULL;
1586 if(c->vChrFilter) free(c->vChrFilter);
1587 c->vChrFilter = NULL;
1588 if(c->hLumFilter) free(c->hLumFilter);
1589 c->hLumFilter = NULL;
1590 if(c->hChrFilter) free(c->hChrFilter);
1591 c->hChrFilter = NULL;
1593 if(c->vLumFilterPos) free(c->vLumFilterPos);
1594 c->vLumFilterPos = NULL;
1595 if(c->vChrFilterPos) free(c->vChrFilterPos);
1596 c->vChrFilterPos = NULL;
1597 if(c->hLumFilterPos) free(c->hLumFilterPos);
1598 c->hLumFilterPos = NULL;
1599 if(c->hChrFilterPos) free(c->hChrFilterPos);
1600 c->hChrFilterPos = NULL;
1602 if(c->lumMmxFilter) free(c->lumMmxFilter);
1603 c->lumMmxFilter = NULL;
1604 if(c->chrMmxFilter) free(c->chrMmxFilter);
1605 c->chrMmxFilter = NULL;