]> git.sesse.net Git - ffmpeg/blob - postproc/swscale_template.c
fixed a bug which caused horizontal lines
[ffmpeg] / postproc / swscale_template.c
1
2 // Software scaling and colorspace conversion routines for MPlayer
3
4 // Orginal C implementation by ?
5 // current version mostly by Michael Niedermayer (michaelni@gmx.at)
6
7 #include <inttypes.h>
8 #include "../config.h"
9
10 //#undef HAVE_MMX2
11 //#undef HAVE_MMX
12 //#undef ARCH_X86
13 #define DITHER16BPP
14 #define ALT_ERROR
15
16 #define RET 0xC3 //near return opcode
17 /*
18 NOTES
19
20 known BUGS with known cause (no bugreports please!)
21 line at the right (c,asm and mmx2)
22 code reads 1 sample too much (might cause a sig11)
23
24 TODO
25 check alignment off everything
26 */
27
28 static uint64_t yCoeff=    0x2568256825682568LL;
29 static uint64_t ubCoeff=   0x3343334333433343LL;
30 static uint64_t vrCoeff=   0x40cf40cf40cf40cfLL;
31 static uint64_t ugCoeff=   0xE5E2E5E2E5E2E5E2LL;
32 static uint64_t vgCoeff=   0xF36EF36EF36EF36ELL;
33 static uint64_t w80=       0x0080008000800080LL;
34 static uint64_t w10=       0x0010001000100010LL;
35 static uint64_t bm00000111=0x0000000000FFFFFFLL;
36 static uint64_t bm11111000=0xFFFFFFFFFF000000LL;
37
38 static uint64_t b16Dither= 0x0004000400040004LL;
39 static uint64_t b16Dither1=0x0004000400040004LL;
40 static uint64_t b16Dither2=0x0602060206020602LL;
41 static uint64_t g16Dither= 0x0002000200020002LL;
42 static uint64_t g16Dither1=0x0002000200020002LL;
43 static uint64_t g16Dither2=0x0301030103010301LL;
44
45 static uint64_t b16Mask=   0x001F001F001F001FLL;
46 static uint64_t g16Mask=   0x07E007E007E007E0LL;
47 static uint64_t r16Mask=   0xF800F800F800F800LL;
48 static uint64_t temp0;
49
50
51 // temporary storage for 4 yuv lines:
52 // 16bit for now (mmx likes it more compact)
53 static uint16_t pix_buf_y[4][2048];
54 static uint16_t pix_buf_uv[2][2048*2];
55
56 // clipping helper table for C implementations:
57 static unsigned char clip_table[768];
58
59 // yuv->rgb conversion tables:
60 static    int yuvtab_2568[256];
61 static    int yuvtab_3343[256];
62 static    int yuvtab_0c92[256];
63 static    int yuvtab_1a1e[256];
64 static    int yuvtab_40cf[256];
65
66
67 static uint8_t funnyYCode[10000];
68 static uint8_t funnyUVCode[10000];
69
70
71 // *** bilinear scaling and yuv->rgb conversion of yv12 slices:
72 // *** Note: it's called multiple times while decoding a frame, first time y==0
73 // *** Designed to upscale, but may work for downscale too.
74 // s_xinc = (src_width << 8) / dst_width
75 // s_yinc = (src_height << 16) / dst_height
76 void SwScale_YV12slice_brg24(unsigned char* srcptr[],int stride[], int y, int h,
77                              unsigned char* dstptr, int dststride, int dstw, int dstbpp,
78                              unsigned int s_xinc,unsigned int s_yinc){
79
80 // scaling factors:
81 //static int s_yinc=(vo_dga_src_height<<16)/vo_dga_vp_height;
82 //static int s_xinc=(vo_dga_src_width<<8)/vo_dga_vp_width;
83
84 unsigned int s_xinc2;
85
86 static int s_srcypos; // points to the dst Pixels center in the source (0 is the center of pixel 0,0 in src)
87 static int s_ypos;
88
89 // last horzontally interpolated lines, used to avoid unnecessary calculations
90 static int s_last_ypos;
91 static int s_last_y1pos;
92
93 static int static_dstw;
94
95 #ifdef HAVE_MMX2
96 // used to detect a horizontal size change
97 static int old_dstw= -1;
98 static int old_s_xinc= -1;
99
100 // difference between the requested xinc and the required one for the mmx2 routine
101 static int s_xinc_diff=0;
102 static int s_xinc2_diff=0;
103 #endif
104 int canMMX2BeUsed;
105
106 // we need that precission at least for the mmx2 code
107 s_xinc*= 256;
108 s_xinc2=s_xinc>>1;
109 canMMX2BeUsed= (s_xinc <= 0x10000 && (dstw&31)==0) ? 1 : 0;
110
111   if(y==0){
112       s_srcypos= s_yinc/2 - 0x8000;
113       s_ypos=0;
114
115       // force calculation of the horizontal interpolation of the first line
116       s_last_ypos=-99;
117       s_last_y1pos=-99;
118 #ifdef HAVE_MMX2
119 // cant downscale !!!
120         if((old_s_xinc != s_xinc || old_dstw!=dstw) && canMMX2BeUsed)
121         {
122                 uint8_t *fragment;
123                 int imm8OfPShufW1;
124                 int imm8OfPShufW2;
125                 int fragmentLength;
126
127                 int xpos, xx, xalpha, i;
128
129                 old_s_xinc= s_xinc;
130                 old_dstw= dstw;
131
132                 static_dstw= dstw;
133
134                 // create an optimized horizontal scaling routine
135
136                 //code fragment
137
138                 asm volatile(
139                         "jmp 9f                         \n\t"
140                 // Begin
141                         "0:                             \n\t"
142                         "movq (%%esi), %%mm0            \n\t" //FIXME Alignment
143                         "movq %%mm0, %%mm1              \n\t"
144                         "psrlq $8, %%mm0                \n\t"
145                         "punpcklbw %%mm7, %%mm1 \n\t"
146                         "movq %%mm2, %%mm3              \n\t"
147                         "punpcklbw %%mm7, %%mm0 \n\t"
148                         "addw %%bx, %%cx                \n\t" //2*xalpha += (4*s_xinc)&0xFFFF
149                         "pshufw $0xFF, %%mm1, %%mm1     \n\t"
150                         "1:                             \n\t"
151                         "adcl %%edx, %%esi              \n\t" //xx+= (4*s_xinc)>>16 + carry
152                         "pshufw $0xFF, %%mm0, %%mm0     \n\t"
153                         "2:                             \n\t"
154                         "psrlw $9, %%mm3                \n\t"
155                         "psubw %%mm1, %%mm0             \n\t"
156                         "pmullw %%mm3, %%mm0            \n\t"
157                         "paddw %%mm6, %%mm2             \n\t" // 2*alpha += xpos&0xFFFF
158                         "psllw $7, %%mm1                \n\t"
159                         "paddw %%mm1, %%mm0             \n\t"
160
161                         "movq %%mm0, (%%edi, %%eax)     \n\t"
162
163                         "addl $8, %%eax                 \n\t"
164                 // End
165                         "9:                             \n\t"
166 //              "int $3\n\t"
167                         "leal 0b, %0                    \n\t"
168                         "leal 1b, %1                    \n\t"
169                         "leal 2b, %2                    \n\t"
170                         "decl %1                        \n\t"
171                         "decl %2                        \n\t"
172                         "subl %0, %1                    \n\t"
173                         "subl %0, %2                    \n\t"
174                         "leal 9b, %3                    \n\t"
175                         "subl %0, %3                    \n\t"
176                         :"=r" (fragment), "=r" (imm8OfPShufW1), "=r" (imm8OfPShufW2),
177                          "=r" (fragmentLength)
178                 );
179
180                 xpos= xx=xalpha= 0;
181
182                 /* choose xinc so that all 8 parts fit exactly
183                    Note: we cannot use just 1 part because it would not fit in the code cache */
184                 s_xinc2_diff= -((((s_xinc2*(dstw/8))&0xFFFF))/(dstw/8))+10;
185 //              s_xinc_diff= -((((s_xinc*(dstw/8))&0xFFFF))/(dstw/8));
186 #ifdef ALT_ERROR
187                 s_xinc2_diff+= ((0x10000/(dstw/8)));
188 #endif
189                 s_xinc_diff= s_xinc2_diff*2;
190
191                 s_xinc2+= s_xinc2_diff;
192                 s_xinc+= s_xinc_diff;
193                 for(i=0; i<dstw/8; i++)
194                 {
195                         int xx=xpos>>16;
196
197                         if((i&3) == 0)
198                         {
199                                 int a=0;
200                                 int b=((xpos+s_xinc)>>16) - xx;
201                                 int c=((xpos+s_xinc*2)>>16) - xx;
202                                 int d=((xpos+s_xinc*3)>>16) - xx;
203
204                                 memcpy(funnyYCode + fragmentLength*i/4, fragment, fragmentLength);
205
206                                 funnyYCode[fragmentLength*i/4 + imm8OfPShufW1]=
207                                 funnyYCode[fragmentLength*i/4 + imm8OfPShufW2]=
208                                         a | (b<<2) | (c<<4) | (d<<6);
209
210                                 funnyYCode[fragmentLength*(i+4)/4]= RET;
211                         }
212                         xpos+=s_xinc;
213                 }
214
215                 xpos= xx=xalpha= 0;
216                 //FIXME choose size and or xinc so that they fit exactly
217                 for(i=0; i<dstw/8; i++)
218                 {
219                         int xx=xpos>>16;
220
221                         if((i&3) == 0)
222                         {
223                                 int a=0;
224                                 int b=((xpos+s_xinc2)>>16) - xx;
225                                 int c=((xpos+s_xinc2*2)>>16) - xx;
226                                 int d=((xpos+s_xinc2*3)>>16) - xx;
227
228                                 memcpy(funnyUVCode + fragmentLength*i/4, fragment, fragmentLength);
229
230                                 funnyUVCode[fragmentLength*i/4 + imm8OfPShufW1]=
231                                 funnyUVCode[fragmentLength*i/4 + imm8OfPShufW2]=
232                                         a | (b<<2) | (c<<4) | (d<<6);
233
234                                 funnyUVCode[fragmentLength*(i+4)/4]= RET;
235                         }
236                         xpos+=s_xinc2;
237                 }
238 //              funnyCode[0]= RET;
239
240
241         }
242
243         if(canMMX2BeUsed)
244         {
245                 s_xinc+= s_xinc_diff;
246                 s_xinc2+= s_xinc2_diff;
247         }
248 #endif // HAVE_MMX2
249   } // reset counters
250
251   while(1){
252     unsigned char *dest=dstptr+dststride*s_ypos;
253     int y0=(s_srcypos + 0xFFFF)>>16;  // first luminance source line number below the dst line
254         // points to the dst Pixels center in the source (0 is the center of pixel 0,0 in src)
255     int srcuvpos= s_srcypos + s_yinc/2 - 0x8000;
256     int y1=(srcuvpos + 0x1FFFF)>>17; // first chrominance source line number below the dst line
257     int yalpha=((s_srcypos-1)&0xFFFF)>>7;
258     int yalpha1=yalpha^511;
259     int uvalpha=((srcuvpos-1)&0x1FFFF)>>8;
260     int uvalpha1=uvalpha^511;
261     uint16_t *buf0=pix_buf_y[y0&1];             // top line of the interpolated slice
262     uint16_t *buf1=pix_buf_y[((y0+1)&1)];       // bottom line of the interpolated slice
263     uint16_t *uvbuf0=pix_buf_uv[y1&1];          // top line of the interpolated slice
264     uint16_t *uvbuf1=pix_buf_uv[(y1+1)&1];      // bottom line of the interpolated slice
265     int i;
266
267     // if this is before the first line than use only the first src line
268     if(y0==0) buf0= buf1;
269     if(y1==0) uvbuf0= uvbuf1; // yes we do have to check this, its not the same as y0==0
270
271     if(y0>=y+h) break; // FIXME wrong, skips last lines, but they are dupliactes anyway
272
273     // if this is after the last line than use only the last src line
274     if(y0>=y+h)
275     {
276         buf1= buf0;
277         s_last_ypos=y0;
278     }
279     if(y1>=(y+h)/2)
280     {
281         uvbuf1= uvbuf0;
282         s_last_y1pos=y1;
283     }
284
285
286     s_ypos++; s_srcypos+=s_yinc;
287
288     //only interpolate the src line horizontally if we didnt do it allready
289     if(s_last_ypos!=y0){
290       unsigned char *src=srcptr[0]+(y0-y)*stride[0];
291       unsigned int xpos=0;
292       s_last_ypos=y0;
293       // *** horizontal scale Y line to temp buffer
294 #ifdef ARCH_X86
295
296 #ifdef HAVE_MMX2
297         if(canMMX2BeUsed)
298         {
299                 asm volatile(
300                         "pxor %%mm7, %%mm7              \n\t"
301                         "pxor %%mm2, %%mm2              \n\t" // 2*xalpha
302                         "movd %5, %%mm6                 \n\t" // s_xinc&0xFFFF
303                         "punpcklwd %%mm6, %%mm6         \n\t"
304                         "punpcklwd %%mm6, %%mm6         \n\t"
305                         "movq %%mm6, %%mm2              \n\t"
306                         "psllq $16, %%mm2               \n\t"
307                         "paddw %%mm6, %%mm2             \n\t"
308                         "psllq $16, %%mm2               \n\t"
309                         "paddw %%mm6, %%mm2             \n\t"
310                         "psllq $16, %%mm2               \n\t" //0,t,2t,3t               t=s_xinc&0xFF
311                         "movq %%mm2, temp0              \n\t"
312                         "movd %4, %%mm6                 \n\t" //(s_xinc*4)&0xFFFF
313                         "punpcklwd %%mm6, %%mm6         \n\t"
314                         "punpcklwd %%mm6, %%mm6         \n\t"
315                         "xorl %%eax, %%eax              \n\t" // i
316                         "movl %0, %%esi                 \n\t" // src
317                         "movl %1, %%edi                 \n\t" // buf1
318                         "movl %3, %%edx                 \n\t" // (s_xinc*4)>>16
319                         "xorl %%ecx, %%ecx              \n\t"
320                         "xorl %%ebx, %%ebx              \n\t"
321                         "movw %4, %%bx                  \n\t" // (s_xinc*4)&0xFFFF
322         //      "int $3\n\t"
323                         "call funnyYCode                        \n\t"
324                         "movq temp0, %%mm2              \n\t"
325                         "xorl %%ecx, %%ecx              \n\t"
326                         "call funnyYCode                        \n\t"
327                         "movq temp0, %%mm2              \n\t"
328                         "xorl %%ecx, %%ecx              \n\t"
329                         "call funnyYCode                        \n\t"
330                         "movq temp0, %%mm2              \n\t"
331                         "xorl %%ecx, %%ecx              \n\t"
332                         "call funnyYCode                        \n\t"
333                         "movq temp0, %%mm2              \n\t"
334                         "xorl %%ecx, %%ecx              \n\t"
335                         "call funnyYCode                        \n\t"
336                         "movq temp0, %%mm2              \n\t"
337                         "xorl %%ecx, %%ecx              \n\t"
338                         "call funnyYCode                        \n\t"
339                         "movq temp0, %%mm2              \n\t"
340                         "xorl %%ecx, %%ecx              \n\t"
341                         "call funnyYCode                        \n\t"
342                         "movq temp0, %%mm2              \n\t"
343                         "xorl %%ecx, %%ecx              \n\t"
344                         "call funnyYCode                        \n\t"
345                         :: "m" (src), "m" (buf1), "m" (dstw), "m" ((s_xinc*4)>>16),
346                         "m" ((s_xinc*4)&0xFFFF), "m" (s_xinc&0xFFFF)
347                         : "%eax", "%ebx", "%ecx", "%edx", "%esi", "%edi"
348                 );
349         }
350         else
351         {
352 #endif
353         //NO MMX just normal asm ... FIXME try/write funny MMX2 variant
354         //FIXME add prefetch
355         asm volatile(
356                 "xorl %%eax, %%eax              \n\t" // i
357                 "xorl %%ebx, %%ebx              \n\t" // xx
358                 "xorl %%ecx, %%ecx              \n\t" // 2*xalpha
359                 "1:                             \n\t"
360                 "movzbl  (%0, %%ebx), %%edi     \n\t" //src[xx]
361                 "movzbl 1(%0, %%ebx), %%esi     \n\t" //src[xx+1]
362                 "subl %%edi, %%esi              \n\t" //src[xx+1] - src[xx]
363                 "imull %%ecx, %%esi             \n\t" //(src[xx+1] - src[xx])*2*xalpha
364                 "shll $16, %%edi                \n\t"
365                 "addl %%edi, %%esi              \n\t" //src[xx+1]*2*xalpha + src[xx]*(1-2*xalpha)
366                 "movl %1, %%edi                 \n\t"
367                 "shrl $9, %%esi                 \n\t"
368                 "movw %%si, (%%edi, %%eax, 2)   \n\t"
369                 "addw %4, %%cx                  \n\t" //2*xalpha += s_xinc&0xFF
370                 "adcl %3, %%ebx                 \n\t" //xx+= s_xinc>>8 + carry
371
372                 "movzbl (%0, %%ebx), %%edi      \n\t" //src[xx]
373                 "movzbl 1(%0, %%ebx), %%esi     \n\t" //src[xx+1]
374                 "subl %%edi, %%esi              \n\t" //src[xx+1] - src[xx]
375                 "imull %%ecx, %%esi             \n\t" //(src[xx+1] - src[xx])*2*xalpha
376                 "shll $16, %%edi                \n\t"
377                 "addl %%edi, %%esi              \n\t" //src[xx+1]*2*xalpha + src[xx]*(1-2*xalpha)
378                 "movl %1, %%edi                 \n\t"
379                 "shrl $9, %%esi                 \n\t"
380                 "movw %%si, 2(%%edi, %%eax, 2)  \n\t"
381                 "addw %4, %%cx                  \n\t" //2*xalpha += s_xinc&0xFF
382                 "adcl %3, %%ebx                 \n\t" //xx+= s_xinc>>8 + carry
383
384
385                 "addl $2, %%eax                 \n\t"
386                 "cmpl %2, %%eax                 \n\t"
387                 " jb 1b                         \n\t"
388
389
390                 :: "r" (src), "m" (buf1), "m" (dstw), "m" (s_xinc>>16), "m" (s_xinc&0xFFFF)
391                 : "%eax", "%ebx", "%ecx", "%edi", "%esi"
392                 );
393 #ifdef HAVE_MMX2
394         } //if MMX2 cant be used
395 #endif
396 #else
397       for(i=0;i<dstw;i++){
398         register unsigned int xx=xpos>>16;
399         register unsigned int xalpha=(xpos&0xFFFF)>>9;
400         buf1[i]=(src[xx]*(xalpha^127)+src[xx+1]*xalpha);
401         xpos+=s_xinc;
402       }
403 #endif
404     }
405       // *** horizontal scale U and V lines to temp buffer
406     if(s_last_y1pos!=y1){
407         unsigned char *src1=srcptr[1]+(y1-y/2)*stride[1];
408         unsigned char *src2=srcptr[2]+(y1-y/2)*stride[2];
409         int xpos=0;
410         s_last_y1pos= y1;
411 #ifdef ARCH_X86
412 #ifdef HAVE_MMX2
413         if(canMMX2BeUsed)
414         {
415                 asm volatile(
416                 "pxor %%mm7, %%mm7              \n\t"
417                 "pxor %%mm2, %%mm2              \n\t" // 2*xalpha
418                 "movd %5, %%mm6                 \n\t" // s_xinc&0xFFFF
419                 "punpcklwd %%mm6, %%mm6         \n\t"
420                 "punpcklwd %%mm6, %%mm6         \n\t"
421                 "movq %%mm6, %%mm2              \n\t"
422                 "psllq $16, %%mm2               \n\t"
423                 "paddw %%mm6, %%mm2             \n\t"
424                 "psllq $16, %%mm2               \n\t"
425                 "paddw %%mm6, %%mm2             \n\t"
426                 "psllq $16, %%mm2               \n\t" //0,t,2t,3t               t=s_xinc&0xFFFF
427                 "movq %%mm2, temp0              \n\t"
428                 "movd %4, %%mm6                 \n\t" //(s_xinc*4)&0xFFFF
429                 "punpcklwd %%mm6, %%mm6         \n\t"
430                 "punpcklwd %%mm6, %%mm6         \n\t"
431                 "xorl %%eax, %%eax              \n\t" // i
432                 "movl %0, %%esi                 \n\t" // src
433                 "movl %1, %%edi                 \n\t" // buf1
434                 "movl %3, %%edx                 \n\t" // (s_xinc*4)>>16
435                 "xorl %%ecx, %%ecx              \n\t"
436                 "xorl %%ebx, %%ebx              \n\t"
437                 "movw %4, %%bx                  \n\t" // (s_xinc*4)&0xFFFF
438
439 //      "int $3\n\t"
440 #define FUNNYUVCODE \
441                 "call funnyUVCode               \n\t"\
442                 "movq temp0, %%mm2              \n\t"\
443                 "xorl %%ecx, %%ecx              \n\t"
444
445 FUNNYUVCODE
446 FUNNYUVCODE
447 FUNNYUVCODE
448 FUNNYUVCODE
449
450 FUNNYUVCODE
451 FUNNYUVCODE
452 FUNNYUVCODE
453 FUNNYUVCODE
454
455
456
457                 "xorl %%eax, %%eax              \n\t" // i
458                 "movl %6, %%esi                 \n\t" // src
459                 "movl %1, %%edi                 \n\t" // buf1
460                 "addl $4096, %%edi              \n\t"
461
462 FUNNYUVCODE
463 FUNNYUVCODE
464 FUNNYUVCODE
465 FUNNYUVCODE
466
467 FUNNYUVCODE
468 FUNNYUVCODE
469 FUNNYUVCODE
470 FUNNYUVCODE
471
472                 :: "m" (src1), "m" (uvbuf1), "m" (dstw), "m" ((s_xinc2*4)>>16),
473                   "m" ((s_xinc2*4)&0xFFFF), "m" (s_xinc2&0xFFFF), "m" (src2)
474                 : "%eax", "%ebx", "%ecx", "%edx", "%esi", "%edi"
475         );
476         }
477         else
478         {
479 #endif
480         asm volatile(
481                 "xorl %%eax, %%eax              \n\t" // i
482                 "xorl %%ebx, %%ebx              \n\t" // xx
483                 "xorl %%ecx, %%ecx              \n\t" // 2*xalpha
484                 "1:                             \n\t"
485                 "movl %0, %%esi                 \n\t"
486                 "movzbl  (%%esi, %%ebx), %%edi  \n\t" //src[xx]
487                 "movzbl 1(%%esi, %%ebx), %%esi  \n\t" //src[xx+1]
488                 "subl %%edi, %%esi              \n\t" //src[xx+1] - src[xx]
489                 "imull %%ecx, %%esi             \n\t" //(src[xx+1] - src[xx])*2*xalpha
490                 "shll $16, %%edi                \n\t"
491                 "addl %%edi, %%esi              \n\t" //src[xx+1]*2*xalpha + src[xx]*(1-2*xalpha)
492                 "movl %1, %%edi                 \n\t"
493                 "shrl $9, %%esi                 \n\t"
494                 "movw %%si, (%%edi, %%eax, 2)   \n\t"
495
496                 "movzbl  (%5, %%ebx), %%edi     \n\t" //src[xx]
497                 "movzbl 1(%5, %%ebx), %%esi     \n\t" //src[xx+1]
498                 "subl %%edi, %%esi              \n\t" //src[xx+1] - src[xx]
499                 "imull %%ecx, %%esi             \n\t" //(src[xx+1] - src[xx])*2*xalpha
500                 "shll $16, %%edi                \n\t"
501                 "addl %%edi, %%esi              \n\t" //src[xx+1]*2*xalpha + src[xx]*(1-2*xalpha)
502                 "movl %1, %%edi                 \n\t"
503                 "shrl $9, %%esi                 \n\t"
504                 "movw %%si, 4096(%%edi, %%eax, 2)\n\t"
505
506                 "addw %4, %%cx                  \n\t" //2*xalpha += s_xinc&0xFF
507                 "adcl %3, %%ebx                 \n\t" //xx+= s_xinc>>8 + carry
508                 "addl $1, %%eax                 \n\t"
509                 "cmpl %2, %%eax                 \n\t"
510                 " jb 1b                         \n\t"
511
512
513                 :: "m" (src1), "m" (uvbuf1), "m" (dstw), "m" (s_xinc2>>16), "m" (s_xinc2&0xFFFF),
514                 "r" (src2)
515                 : "%eax", "%ebx", "%ecx", "%edi", "%esi"
516                 );
517 #ifdef HAVE_MMX2
518         } //if MMX2 cant be used
519 #endif
520 #else
521       for(i=0;i<dstw;i++){
522           register unsigned int xx=xpos>>16;
523           register unsigned int xalpha=(xpos&0xFFFF)>>9;
524           uvbuf1[i]=(src1[xx]*(xalpha^127)+src1[xx+1]*xalpha);
525           uvbuf1[i+2048]=(src2[xx]*(xalpha^127)+src2[xx+1]*xalpha);
526           xpos+=s_xinc2;
527       }
528 #endif
529     }
530
531
532     // Note1: this code can be resticted to n*8 (or n*16) width lines to simplify optimization...
533     // Re: Note1: ok n*4 for now
534     // Note2: instead of using lookup tabs, mmx version could do the multiply...
535     // Re: Note2: yep
536     // Note3: maybe we should make separated 15/16, 24 and 32bpp version of this:
537     // Re: done (32 & 16) and 16 has dithering :) but 16 is untested
538 #ifdef HAVE_MMX
539         //FIXME write lq version with less uv ...
540         //FIXME reorder / optimize
541         if(dstbpp == 32)
542         {
543                 asm volatile(
544
545 #define YSCALEYUV2RGB \
546                 "pxor %%mm7, %%mm7              \n\t"\
547                 "movd %6, %%mm6                 \n\t" /*yalpha1*/\
548                 "punpcklwd %%mm6, %%mm6         \n\t"\
549                 "punpcklwd %%mm6, %%mm6         \n\t"\
550                 "movd %7, %%mm5                 \n\t" /*uvalpha1*/\
551                 "punpcklwd %%mm5, %%mm5         \n\t"\
552                 "punpcklwd %%mm5, %%mm5         \n\t"\
553                 "xorl %%eax, %%eax              \n\t"\
554                 "1:                             \n\t"\
555                 "movq (%0, %%eax, 2), %%mm0     \n\t" /*buf0[eax]*/\
556                 "movq (%1, %%eax, 2), %%mm1     \n\t" /*buf1[eax]*/\
557                 "movq (%2, %%eax,2), %%mm2      \n\t" /* uvbuf0[eax]*/\
558                 "movq (%3, %%eax,2), %%mm3      \n\t" /* uvbuf1[eax]*/\
559                 "psubw %%mm1, %%mm0             \n\t" /* buf0[eax] - buf1[eax]*/\
560                 "psubw %%mm3, %%mm2             \n\t" /* uvbuf0[eax] - uvbuf1[eax]*/\
561                 "pmulhw %%mm6, %%mm0            \n\t" /* (buf0[eax] - buf1[eax])yalpha1>>16*/\
562                 "pmulhw %%mm5, %%mm2            \n\t" /* (uvbuf0[eax] - uvbuf1[eax])uvalpha1>>16*/\
563                 "psraw $7, %%mm1                \n\t" /* buf0[eax] - buf1[eax] >>7*/\
564                 "movq 4096(%2, %%eax,2), %%mm4  \n\t" /* uvbuf0[eax+2048]*/\
565                 "psraw $7, %%mm3                \n\t" /* uvbuf0[eax] - uvbuf1[eax] >>7*/\
566                 "paddw %%mm0, %%mm1             \n\t" /* buf0[eax]yalpha1 + buf1[eax](1-yalpha1) >>16*/\
567                 "movq 4096(%3, %%eax,2), %%mm0  \n\t" /* uvbuf1[eax+2048]*/\
568                 "paddw %%mm2, %%mm3             \n\t" /* uvbuf0[eax]uvalpha1 - uvbuf1[eax](1-uvalpha1)*/\
569                 "psubw %%mm0, %%mm4             \n\t" /* uvbuf0[eax+2048] - uvbuf1[eax+2048]*/\
570                 "psubw w10, %%mm1               \n\t" /* Y-16*/\
571                 "psubw w80, %%mm3               \n\t" /* (U-128)*/\
572                 "psllw $3, %%mm1                \n\t" /* (y-16)*8*/\
573                 "psllw $3, %%mm3                \n\t" /*(U-128)8*/\
574                 "pmulhw yCoeff, %%mm1           \n\t"\
575 \
576 \
577                 "pmulhw %%mm5, %%mm4            \n\t" /* (uvbuf0[eax+2048] - uvbuf1[eax+2048])uvalpha1>>16*/\
578                 "movq %%mm3, %%mm2              \n\t" /* (U-128)8*/\
579                 "pmulhw ubCoeff, %%mm3          \n\t"\
580                 "psraw $7, %%mm0                \n\t" /* uvbuf0[eax+2048] - uvbuf1[eax+2048] >>7*/\
581                 "pmulhw ugCoeff, %%mm2          \n\t"\
582                 "paddw %%mm4, %%mm0             \n\t" /* uvbuf0[eax+2048]uvalpha1 - uvbuf1[eax+2048](1-uvalpha1)*/\
583                 "psubw w80, %%mm0               \n\t" /* (V-128)*/\
584                 "psllw $3, %%mm0                \n\t" /* (V-128)8*/\
585 \
586 \
587                 "movq %%mm0, %%mm4              \n\t" /* (V-128)8*/\
588                 "pmulhw vrCoeff, %%mm0          \n\t"\
589                 "pmulhw vgCoeff, %%mm4          \n\t"\
590                 "paddw %%mm1, %%mm3             \n\t" /* B*/\
591                 "paddw %%mm1, %%mm0             \n\t" /* R*/\
592                 "packuswb %%mm3, %%mm3          \n\t"\
593 \
594                 "packuswb %%mm0, %%mm0          \n\t"\
595                 "paddw %%mm4, %%mm2             \n\t"\
596                 "paddw %%mm2, %%mm1             \n\t" /* G*/\
597 \
598                 "packuswb %%mm1, %%mm1          \n\t"
599
600 YSCALEYUV2RGB
601                 "punpcklbw %%mm1, %%mm3         \n\t" // BGBGBGBG
602                 "punpcklbw %%mm7, %%mm0         \n\t" // R0R0R0R0
603
604                 "movq %%mm3, %%mm1              \n\t"
605                 "punpcklwd %%mm0, %%mm3         \n\t" // BGR0BGR0
606                 "punpckhwd %%mm0, %%mm1         \n\t" // BGR0BGR0
607 #ifdef HAVE_MMX2
608                 "movntq %%mm3, (%4, %%eax, 4)   \n\t"
609                 "movntq %%mm1, 8(%4, %%eax, 4)  \n\t"
610 #else
611                 "movq %%mm3, (%4, %%eax, 4)     \n\t"
612                 "movq %%mm1, 8(%4, %%eax, 4)    \n\t"
613 #endif
614                 "addl $4, %%eax                 \n\t"
615                 "cmpl %5, %%eax                 \n\t"
616                 " jb 1b                         \n\t"
617
618
619                 :: "r" (buf0), "r" (buf1), "r" (uvbuf0), "r" (uvbuf1), "r" (dest), "m" (dstw),
620                 "m" (yalpha1), "m" (uvalpha1)
621                 : "%eax"
622                 );
623         }
624         else if(dstbpp==24)
625         {
626                 asm volatile(
627
628 YSCALEYUV2RGB
629
630                                                         // lsb ... msb
631                 "punpcklbw %%mm1, %%mm3         \n\t" // BGBGBGBG
632                 "punpcklbw %%mm7, %%mm0         \n\t" // R0R0R0R0
633
634                 "movq %%mm3, %%mm1              \n\t"
635                 "punpcklwd %%mm0, %%mm3         \n\t" // BGR0BGR0
636                 "punpckhwd %%mm0, %%mm1         \n\t" // BGR0BGR0
637
638                 "movq %%mm3, %%mm2              \n\t" // BGR0BGR0
639                 "psrlq $8, %%mm3                \n\t" // GR0BGR00
640                 "pand bm00000111, %%mm2         \n\t" // BGR00000
641                 "pand bm11111000, %%mm3         \n\t" // 000BGR00
642                 "por %%mm2, %%mm3               \n\t" // BGRBGR00
643                 "movq %%mm1, %%mm2              \n\t"
644                 "psllq $48, %%mm1               \n\t" // 000000BG
645                 "por %%mm1, %%mm3               \n\t" // BGRBGRBG
646
647                 "movq %%mm2, %%mm1              \n\t" // BGR0BGR0
648                 "psrld $16, %%mm2               \n\t" // R000R000
649                 "psrlq $24, %%mm1               \n\t" // 0BGR0000
650                 "por %%mm2, %%mm1               \n\t" // RBGRR000
651
652                 "movl %4, %%ebx                 \n\t"
653                 "addl %%eax, %%ebx              \n\t"
654 #ifdef HAVE_MMX2
655                 //FIXME Alignment
656                 "movntq %%mm3, (%%ebx, %%eax, 2)\n\t"
657                 "movntq %%mm1, 8(%%ebx, %%eax, 2)\n\t"
658 #else
659                 "movd %%mm3, (%%ebx, %%eax, 2)  \n\t"
660                 "psrlq $32, %%mm3               \n\t"
661                 "movd %%mm3, 4(%%ebx, %%eax, 2) \n\t"
662                 "movd %%mm1, 8(%%ebx, %%eax, 2) \n\t"
663 #endif
664                 "addl $4, %%eax                 \n\t"
665                 "cmpl %5, %%eax                 \n\t"
666                 " jb 1b                         \n\t"
667
668                 :: "r" (buf0), "r" (buf1), "r" (uvbuf0), "r" (uvbuf1), "m" (dest), "m" (dstw),
669                 "m" (yalpha1), "m" (uvalpha1)
670                 : "%eax", "%ebx"
671                 );
672         }
673         else if(dstbpp==16)
674         {
675                 asm volatile(
676
677 YSCALEYUV2RGB
678 #ifdef DITHER16BPP
679                 "paddusb g16Dither, %%mm1       \n\t"
680                 "paddusb b16Dither, %%mm0       \n\t"
681                 "paddusb b16Dither, %%mm3       \n\t"
682 #endif
683                 "punpcklbw %%mm7, %%mm1         \n\t" // 0G0G0G0G
684                 "punpcklbw %%mm7, %%mm3         \n\t" // 0B0B0B0B
685                 "punpcklbw %%mm7, %%mm0         \n\t" // 0R0R0R0R
686
687                 "psrlw $3, %%mm3                \n\t"
688                 "psllw $3, %%mm1                \n\t"
689                 "psllw $8, %%mm0                \n\t"
690                 "pand g16Mask, %%mm1            \n\t"
691                 "pand r16Mask, %%mm0            \n\t"
692
693                 "por %%mm3, %%mm1               \n\t"
694                 "por %%mm1, %%mm0               \n\t"
695 #ifdef HAVE_MMX2
696                 "movntq %%mm0, (%4, %%eax, 2)   \n\t"
697 #else
698                 "movq %%mm0, (%4, %%eax, 2)     \n\t"
699 #endif
700                 "addl $4, %%eax                 \n\t"
701                 "cmpl %5, %%eax                 \n\t"
702                 " jb 1b                         \n\t"
703
704                 :: "r" (buf0), "r" (buf1), "r" (uvbuf0), "r" (uvbuf1), "r" (dest), "m" (dstw),
705                 "m" (yalpha1), "m" (uvalpha1)
706                 : "%eax"
707                 );
708         }
709 #else
710         if(dstbpp==32 || dstbpp==24)
711         {
712                 for(i=0;i<dstw;i++){
713                         // vertical linear interpolation && yuv2rgb in a single step:
714                         int Y=yuvtab_2568[((buf0[i]*yalpha1+buf1[i]*yalpha)>>16)];
715                         int U=((uvbuf0[i]*uvalpha1+uvbuf1[i]*uvalpha)>>16);
716                         int V=((uvbuf0[i+2048]*uvalpha1+uvbuf1[i+2048]*uvalpha)>>16);
717                         dest[0]=clip_table[((Y + yuvtab_3343[U]) >>13)];
718                         dest[1]=clip_table[((Y + yuvtab_0c92[V] + yuvtab_1a1e[U]) >>13)];
719                         dest[2]=clip_table[((Y + yuvtab_40cf[V]) >>13)];
720                         dest+=dstbpp>>3;
721                 }
722         }
723         else if(dstbpp==16)
724         {
725                 for(i=0;i<dstw;i++){
726                         // vertical linear interpolation && yuv2rgb in a single step:
727                         int Y=yuvtab_2568[((buf0[i]*yalpha1+buf1[i]*yalpha)>>16)];
728                         int U=((uvbuf0[i]*uvalpha1+uvbuf1[i]*uvalpha)>>16);
729                         int V=((uvbuf0[i+2048]*uvalpha1+uvbuf1[i+2048]*uvalpha)>>16);
730
731                         ((uint16_t*)dest)[0] =
732                                 (clip_table[((Y + yuvtab_3343[U]) >>13)]>>3) |
733                                 (clip_table[((Y + yuvtab_0c92[V] + yuvtab_1a1e[U]) >>13)]<<3)&0x07E0 |
734                                 (clip_table[((Y + yuvtab_40cf[V]) >>13)]<<8)&0xF800;
735                         dest+=2;
736                 }
737         }
738         else if(dstbpp==15) //15bit FIXME how do i figure out if its 15 or 16?
739         {
740                 for(i=0;i<dstw;i++){
741                         // vertical linear interpolation && yuv2rgb in a single step:
742                         int Y=yuvtab_2568[((buf0[i]*yalpha1+buf1[i]*yalpha)>>16)];
743                         int U=((uvbuf0[i]*uvalpha1+uvbuf1[i]*uvalpha)>>16);
744                         int V=((uvbuf0[i+2048]*uvalpha1+uvbuf1[i+2048]*uvalpha)>>16);
745
746                         ((uint16_t*)dest)[0] =
747                                 (clip_table[((Y + yuvtab_3343[U]) >>13)]>>3) |
748                                 (clip_table[((Y + yuvtab_0c92[V] + yuvtab_1a1e[U]) >>13)]<<2)&0x03E0 |
749                                 (clip_table[((Y + yuvtab_40cf[V]) >>13)]<<7)&0x7C00;
750                         dest+=2;
751                 }
752         }
753 #endif
754
755         b16Dither= b16Dither1;
756         b16Dither1= b16Dither2;
757         b16Dither2= b16Dither;
758
759         g16Dither= g16Dither1;
760         g16Dither1= g16Dither2;
761         g16Dither2= g16Dither;
762   }
763
764 #ifdef HAVE_3DNOW
765         asm volatile("femms");
766 #elif defined (HAVE_MMX)
767         asm volatile("emms");
768 #endif
769 }
770
771
772 void SwScale_Init(){
773     // generating tables:
774     int i;
775     for(i=0;i<256;i++){
776         clip_table[i]=0;
777         clip_table[i+256]=i;
778         clip_table[i+512]=255;
779         yuvtab_2568[i]=(0x2568*(i-16))+(256<<13);
780         yuvtab_3343[i]=0x3343*(i-128);
781         yuvtab_0c92[i]=-0x0c92*(i-128);
782         yuvtab_1a1e[i]=-0x1a1e*(i-128);
783         yuvtab_40cf[i]=0x40cf*(i-128);
784     }
785
786 }