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