]> git.sesse.net Git - ffmpeg/blob - postproc/swscale.c
right green line bugfix for width not %8==0 (untested -vo vesa doesnt work)
[ffmpeg] / postproc / swscale.c
1
2 // Software scaling and colorspace conversion routines for MPlayer
3
4 // Orginal C implementation by A'rpi/ESP-team <arpi@thot.banki.hu>
5 // current version mostly by Michael Niedermayer (michaelni@gmx.at)
6 // the parts written by michael are under GNU GPL
7
8 #include <inttypes.h>
9 #include <string.h>
10 #include "../config.h"
11 #include "swscale.h"
12 #include "../mmx_defs.h"
13 #undef MOVNTQ
14
15 //#undef HAVE_MMX2
16 //#undef HAVE_MMX
17 //#undef ARCH_X86
18 #define DITHER1XBPP
19 int fullUVIpol=0;
20 //disables the unscaled height version
21 int allwaysIpol=0;
22
23 #define RET 0xC3 //near return opcode
24 /*
25 NOTES
26
27 known BUGS with known cause (no bugreports please!, but patches are welcome :) )
28 horizontal MMX2 scaler reads 1-7 samples too much (might cause a sig11)
29
30 Supported output formats BGR15 BGR16 BGR24 BGR32
31 BGR15 & BGR16 MMX verions support dithering
32 Special versions: fast Y 1:1 scaling (no interpolation in y direction)
33
34 TODO
35 more intelligent missalignment avoidance for the horizontal scaler
36 bicubic scaler
37 dither in C
38 change the distance of the u & v buffer
39 */
40
41 #define ABS(a) ((a) > 0 ? (a) : (-(a)))
42 #define MIN(a,b) ((a) > (b) ? (b) : (a))
43 #define MAX(a,b) ((a) < (b) ? (b) : (a))
44
45 #ifdef HAVE_MMX2
46 #define PAVGB(a,b) "pavgb " #a ", " #b " \n\t"
47 #elif defined (HAVE_3DNOW)
48 #define PAVGB(a,b) "pavgusb " #a ", " #b " \n\t"
49 #endif
50
51 #ifdef HAVE_MMX2
52 #define MOVNTQ(a,b) "movntq " #a ", " #b " \n\t"
53 #else
54 #define MOVNTQ(a,b) "movq " #a ", " #b " \n\t"
55 #endif
56
57
58 #ifdef HAVE_MMX
59 static uint64_t __attribute__((aligned(8))) yCoeff=    0x2568256825682568LL;
60 static uint64_t __attribute__((aligned(8))) vrCoeff=   0x3343334333433343LL;
61 static uint64_t __attribute__((aligned(8))) ubCoeff=   0x40cf40cf40cf40cfLL;
62 static uint64_t __attribute__((aligned(8))) vgCoeff=   0xE5E2E5E2E5E2E5E2LL;
63 static uint64_t __attribute__((aligned(8))) ugCoeff=   0xF36EF36EF36EF36ELL;
64 static uint64_t __attribute__((aligned(8))) w400=      0x0400040004000400LL;
65 static uint64_t __attribute__((aligned(8))) w80=       0x0080008000800080LL;
66 static uint64_t __attribute__((aligned(8))) w10=       0x0010001000100010LL;
67 static uint64_t __attribute__((aligned(8))) bm00001111=0x00000000FFFFFFFFLL;
68 static uint64_t __attribute__((aligned(8))) bm00000111=0x0000000000FFFFFFLL;
69 static uint64_t __attribute__((aligned(8))) bm11111000=0xFFFFFFFFFF000000LL;
70
71 static uint64_t __attribute__((aligned(8))) b16Dither= 0x0004000400040004LL;
72 static uint64_t __attribute__((aligned(8))) b16Dither1=0x0004000400040004LL;
73 static uint64_t __attribute__((aligned(8))) b16Dither2=0x0602060206020602LL;
74 static uint64_t __attribute__((aligned(8))) g16Dither= 0x0002000200020002LL;
75 static uint64_t __attribute__((aligned(8))) g16Dither1=0x0002000200020002LL;
76 static uint64_t __attribute__((aligned(8))) g16Dither2=0x0301030103010301LL;
77
78 static uint64_t __attribute__((aligned(8))) b16Mask=   0x001F001F001F001FLL;
79 static uint64_t __attribute__((aligned(8))) g16Mask=   0x07E007E007E007E0LL;
80 static uint64_t __attribute__((aligned(8))) r16Mask=   0xF800F800F800F800LL;
81 static uint64_t __attribute__((aligned(8))) b15Mask=   0x001F001F001F001FLL;
82 static uint64_t __attribute__((aligned(8))) g15Mask=   0x03E003E003E003E0LL;
83 static uint64_t __attribute__((aligned(8))) r15Mask=   0x7C007C007C007C00LL;
84
85 static uint64_t __attribute__((aligned(8))) temp0;
86 static uint64_t __attribute__((aligned(8))) asm_yalpha1;
87 static uint64_t __attribute__((aligned(8))) asm_uvalpha1;
88 #endif
89
90 // temporary storage for 4 yuv lines:
91 // 16bit for now (mmx likes it more compact)
92 #ifdef HAVE_MMX
93 static uint16_t __attribute__((aligned(8))) pix_buf_y[4][2048];
94 static uint16_t __attribute__((aligned(8))) pix_buf_uv[2][2048*2];
95 #else
96 static uint16_t pix_buf_y[4][2048];
97 static uint16_t pix_buf_uv[2][2048*2];
98 #endif
99
100 // clipping helper table for C implementations:
101 static unsigned char clip_table[768];
102
103 static unsigned short clip_table16b[768];
104 static unsigned short clip_table16g[768];
105 static unsigned short clip_table16r[768];
106 static unsigned short clip_table15b[768];
107 static unsigned short clip_table15g[768];
108 static unsigned short clip_table15r[768];
109
110 // yuv->rgb conversion tables:
111 static    int yuvtab_2568[256];
112 static    int yuvtab_3343[256];
113 static    int yuvtab_0c92[256];
114 static    int yuvtab_1a1e[256];
115 static    int yuvtab_40cf[256];
116
117
118 static uint8_t funnyYCode[10000];
119 static uint8_t funnyUVCode[10000];
120
121 static int canMMX2BeUsed=0;
122
123 #define FULL_YSCALEYUV2RGB \
124                 "pxor %%mm7, %%mm7              \n\t"\
125                 "movd %6, %%mm6                 \n\t" /*yalpha1*/\
126                 "punpcklwd %%mm6, %%mm6         \n\t"\
127                 "punpcklwd %%mm6, %%mm6         \n\t"\
128                 "movd %7, %%mm5                 \n\t" /*uvalpha1*/\
129                 "punpcklwd %%mm5, %%mm5         \n\t"\
130                 "punpcklwd %%mm5, %%mm5         \n\t"\
131                 "xorl %%eax, %%eax              \n\t"\
132                 "1:                             \n\t"\
133                 "movq (%0, %%eax, 2), %%mm0     \n\t" /*buf0[eax]*/\
134                 "movq (%1, %%eax, 2), %%mm1     \n\t" /*buf1[eax]*/\
135                 "movq (%2, %%eax,2), %%mm2      \n\t" /* uvbuf0[eax]*/\
136                 "movq (%3, %%eax,2), %%mm3      \n\t" /* uvbuf1[eax]*/\
137                 "psubw %%mm1, %%mm0             \n\t" /* buf0[eax] - buf1[eax]*/\
138                 "psubw %%mm3, %%mm2             \n\t" /* uvbuf0[eax] - uvbuf1[eax]*/\
139                 "pmulhw %%mm6, %%mm0            \n\t" /* (buf0[eax] - buf1[eax])yalpha1>>16*/\
140                 "pmulhw %%mm5, %%mm2            \n\t" /* (uvbuf0[eax] - uvbuf1[eax])uvalpha1>>16*/\
141                 "psraw $4, %%mm1                \n\t" /* buf0[eax] - buf1[eax] >>4*/\
142                 "movq 4096(%2, %%eax,2), %%mm4  \n\t" /* uvbuf0[eax+2048]*/\
143                 "psraw $4, %%mm3                \n\t" /* uvbuf0[eax] - uvbuf1[eax] >>4*/\
144                 "paddw %%mm0, %%mm1             \n\t" /* buf0[eax]yalpha1 + buf1[eax](1-yalpha1) >>16*/\
145                 "movq 4096(%3, %%eax,2), %%mm0  \n\t" /* uvbuf1[eax+2048]*/\
146                 "paddw %%mm2, %%mm3             \n\t" /* uvbuf0[eax]uvalpha1 - uvbuf1[eax](1-uvalpha1)*/\
147                 "psubw %%mm0, %%mm4             \n\t" /* uvbuf0[eax+2048] - uvbuf1[eax+2048]*/\
148                 "psubw w80, %%mm1               \n\t" /* 8(Y-16)*/\
149                 "psubw w400, %%mm3              \n\t" /* 8(U-128)*/\
150                 "pmulhw yCoeff, %%mm1           \n\t"\
151 \
152 \
153                 "pmulhw %%mm5, %%mm4            \n\t" /* (uvbuf0[eax+2048] - uvbuf1[eax+2048])uvalpha1>>16*/\
154                 "movq %%mm3, %%mm2              \n\t" /* (U-128)8*/\
155                 "pmulhw ubCoeff, %%mm3          \n\t"\
156                 "psraw $4, %%mm0                \n\t" /* uvbuf0[eax+2048] - uvbuf1[eax+2048] >>4*/\
157                 "pmulhw ugCoeff, %%mm2          \n\t"\
158                 "paddw %%mm4, %%mm0             \n\t" /* uvbuf0[eax+2048]uvalpha1 - uvbuf1[eax+2048](1-uvalpha1)*/\
159                 "psubw w400, %%mm0              \n\t" /* (V-128)8*/\
160 \
161 \
162                 "movq %%mm0, %%mm4              \n\t" /* (V-128)8*/\
163                 "pmulhw vrCoeff, %%mm0          \n\t"\
164                 "pmulhw vgCoeff, %%mm4          \n\t"\
165                 "paddw %%mm1, %%mm3             \n\t" /* B*/\
166                 "paddw %%mm1, %%mm0             \n\t" /* R*/\
167                 "packuswb %%mm3, %%mm3          \n\t"\
168 \
169                 "packuswb %%mm0, %%mm0          \n\t"\
170                 "paddw %%mm4, %%mm2             \n\t"\
171                 "paddw %%mm2, %%mm1             \n\t" /* G*/\
172 \
173                 "packuswb %%mm1, %%mm1          \n\t"
174
175 #define YSCALEYUV2RGB \
176                 "movd %6, %%mm6                 \n\t" /*yalpha1*/\
177                 "punpcklwd %%mm6, %%mm6         \n\t"\
178                 "punpcklwd %%mm6, %%mm6         \n\t"\
179                 "movq %%mm6, asm_yalpha1        \n\t"\
180                 "movd %7, %%mm5                 \n\t" /*uvalpha1*/\
181                 "punpcklwd %%mm5, %%mm5         \n\t"\
182                 "punpcklwd %%mm5, %%mm5         \n\t"\
183                 "movq %%mm5, asm_uvalpha1       \n\t"\
184                 "xorl %%eax, %%eax              \n\t"\
185                 "1:                             \n\t"\
186                 "movq (%2, %%eax), %%mm2        \n\t" /* uvbuf0[eax]*/\
187                 "movq (%3, %%eax), %%mm3        \n\t" /* uvbuf1[eax]*/\
188                 "movq 4096(%2, %%eax), %%mm5    \n\t" /* uvbuf0[eax+2048]*/\
189                 "movq 4096(%3, %%eax), %%mm4    \n\t" /* uvbuf1[eax+2048]*/\
190                 "psubw %%mm3, %%mm2             \n\t" /* uvbuf0[eax] - uvbuf1[eax]*/\
191                 "psubw %%mm4, %%mm5             \n\t" /* uvbuf0[eax+2048] - uvbuf1[eax+2048]*/\
192                 "movq asm_uvalpha1, %%mm0       \n\t"\
193                 "pmulhw %%mm0, %%mm2            \n\t" /* (uvbuf0[eax] - uvbuf1[eax])uvalpha1>>16*/\
194                 "pmulhw %%mm0, %%mm5            \n\t" /* (uvbuf0[eax+2048] - uvbuf1[eax+2048])uvalpha1>>16*/\
195                 "psraw $4, %%mm3                \n\t" /* uvbuf0[eax] - uvbuf1[eax] >>4*/\
196                 "psraw $4, %%mm4                \n\t" /* uvbuf0[eax+2048] - uvbuf1[eax+2048] >>4*/\
197                 "paddw %%mm2, %%mm3             \n\t" /* uvbuf0[eax]uvalpha1 - uvbuf1[eax](1-uvalpha1)*/\
198                 "paddw %%mm5, %%mm4             \n\t" /* uvbuf0[eax+2048]uvalpha1 - uvbuf1[eax+2048](1-uvalpha1)*/\
199                 "psubw w400, %%mm3              \n\t" /* (U-128)8*/\
200                 "psubw w400, %%mm4              \n\t" /* (V-128)8*/\
201                 "movq %%mm3, %%mm2              \n\t" /* (U-128)8*/\
202                 "movq %%mm4, %%mm5              \n\t" /* (V-128)8*/\
203                 "pmulhw ugCoeff, %%mm3          \n\t"\
204                 "pmulhw vgCoeff, %%mm4          \n\t"\
205         /* mm2=(U-128)8, mm3=ug, mm4=vg mm5=(V-128)8 */\
206                 "movq (%0, %%eax, 2), %%mm0     \n\t" /*buf0[eax]*/\
207                 "movq (%1, %%eax, 2), %%mm1     \n\t" /*buf1[eax]*/\
208                 "movq 8(%0, %%eax, 2), %%mm6    \n\t" /*buf0[eax]*/\
209                 "movq 8(%1, %%eax, 2), %%mm7    \n\t" /*buf1[eax]*/\
210                 "psubw %%mm1, %%mm0             \n\t" /* buf0[eax] - buf1[eax]*/\
211                 "psubw %%mm7, %%mm6             \n\t" /* buf0[eax] - buf1[eax]*/\
212                 "pmulhw asm_yalpha1, %%mm0      \n\t" /* (buf0[eax] - buf1[eax])yalpha1>>16*/\
213                 "pmulhw asm_yalpha1, %%mm6      \n\t" /* (buf0[eax] - buf1[eax])yalpha1>>16*/\
214                 "psraw $4, %%mm1                \n\t" /* buf0[eax] - buf1[eax] >>4*/\
215                 "psraw $4, %%mm7                \n\t" /* buf0[eax] - buf1[eax] >>4*/\
216                 "paddw %%mm0, %%mm1             \n\t" /* buf0[eax]yalpha1 + buf1[eax](1-yalpha1) >>16*/\
217                 "paddw %%mm6, %%mm7             \n\t" /* buf0[eax]yalpha1 + buf1[eax](1-yalpha1) >>16*/\
218                 "pmulhw ubCoeff, %%mm2          \n\t"\
219                 "pmulhw vrCoeff, %%mm5          \n\t"\
220                 "psubw w80, %%mm1               \n\t" /* 8(Y-16)*/\
221                 "psubw w80, %%mm7               \n\t" /* 8(Y-16)*/\
222                 "pmulhw yCoeff, %%mm1           \n\t"\
223                 "pmulhw yCoeff, %%mm7           \n\t"\
224         /* mm1= Y1, mm2=ub, mm3=ug, mm4=vg mm5=vr, mm7=Y2 */\
225                 "paddw %%mm3, %%mm4             \n\t"\
226                 "movq %%mm2, %%mm0              \n\t"\
227                 "movq %%mm5, %%mm6              \n\t"\
228                 "movq %%mm4, %%mm3              \n\t"\
229                 "punpcklwd %%mm2, %%mm2         \n\t"\
230                 "punpcklwd %%mm5, %%mm5         \n\t"\
231                 "punpcklwd %%mm4, %%mm4         \n\t"\
232                 "paddw %%mm1, %%mm2             \n\t"\
233                 "paddw %%mm1, %%mm5             \n\t"\
234                 "paddw %%mm1, %%mm4             \n\t"\
235                 "punpckhwd %%mm0, %%mm0         \n\t"\
236                 "punpckhwd %%mm6, %%mm6         \n\t"\
237                 "punpckhwd %%mm3, %%mm3         \n\t"\
238                 "paddw %%mm7, %%mm0             \n\t"\
239                 "paddw %%mm7, %%mm6             \n\t"\
240                 "paddw %%mm7, %%mm3             \n\t"\
241                 /* mm0=B1, mm2=B2, mm3=G2, mm4=G1, mm5=R1, mm6=R2 */\
242                 "packuswb %%mm0, %%mm2          \n\t"\
243                 "packuswb %%mm6, %%mm5          \n\t"\
244                 "packuswb %%mm3, %%mm4          \n\t"\
245                 "pxor %%mm7, %%mm7              \n\t"
246
247 #define YSCALEYUV2RGB1 \
248                 "xorl %%eax, %%eax              \n\t"\
249                 "1:                             \n\t"\
250                 "movq (%2, %%eax), %%mm3        \n\t" /* uvbuf0[eax]*/\
251                 "movq 4096(%2, %%eax), %%mm4    \n\t" /* uvbuf0[eax+2048]*/\
252                 "psraw $4, %%mm3                \n\t" /* uvbuf0[eax] - uvbuf1[eax] >>4*/\
253                 "psraw $4, %%mm4                \n\t" /* uvbuf0[eax+2048] - uvbuf1[eax+2048] >>4*/\
254                 "psubw w400, %%mm3              \n\t" /* (U-128)8*/\
255                 "psubw w400, %%mm4              \n\t" /* (V-128)8*/\
256                 "movq %%mm3, %%mm2              \n\t" /* (U-128)8*/\
257                 "movq %%mm4, %%mm5              \n\t" /* (V-128)8*/\
258                 "pmulhw ugCoeff, %%mm3          \n\t"\
259                 "pmulhw vgCoeff, %%mm4          \n\t"\
260         /* mm2=(U-128)8, mm3=ug, mm4=vg mm5=(V-128)8 */\
261                 "movq (%0, %%eax, 2), %%mm1     \n\t" /*buf0[eax]*/\
262                 "movq 8(%0, %%eax, 2), %%mm7    \n\t" /*buf0[eax]*/\
263                 "psraw $4, %%mm1                \n\t" /* buf0[eax] - buf1[eax] >>4*/\
264                 "psraw $4, %%mm7                \n\t" /* buf0[eax] - buf1[eax] >>4*/\
265                 "pmulhw ubCoeff, %%mm2          \n\t"\
266                 "pmulhw vrCoeff, %%mm5          \n\t"\
267                 "psubw w80, %%mm1               \n\t" /* 8(Y-16)*/\
268                 "psubw w80, %%mm7               \n\t" /* 8(Y-16)*/\
269                 "pmulhw yCoeff, %%mm1           \n\t"\
270                 "pmulhw yCoeff, %%mm7           \n\t"\
271         /* mm1= Y1, mm2=ub, mm3=ug, mm4=vg mm5=vr, mm7=Y2 */\
272                 "paddw %%mm3, %%mm4             \n\t"\
273                 "movq %%mm2, %%mm0              \n\t"\
274                 "movq %%mm5, %%mm6              \n\t"\
275                 "movq %%mm4, %%mm3              \n\t"\
276                 "punpcklwd %%mm2, %%mm2         \n\t"\
277                 "punpcklwd %%mm5, %%mm5         \n\t"\
278                 "punpcklwd %%mm4, %%mm4         \n\t"\
279                 "paddw %%mm1, %%mm2             \n\t"\
280                 "paddw %%mm1, %%mm5             \n\t"\
281                 "paddw %%mm1, %%mm4             \n\t"\
282                 "punpckhwd %%mm0, %%mm0         \n\t"\
283                 "punpckhwd %%mm6, %%mm6         \n\t"\
284                 "punpckhwd %%mm3, %%mm3         \n\t"\
285                 "paddw %%mm7, %%mm0             \n\t"\
286                 "paddw %%mm7, %%mm6             \n\t"\
287                 "paddw %%mm7, %%mm3             \n\t"\
288                 /* mm0=B1, mm2=B2, mm3=G2, mm4=G1, mm5=R1, mm6=R2 */\
289                 "packuswb %%mm0, %%mm2          \n\t"\
290                 "packuswb %%mm6, %%mm5          \n\t"\
291                 "packuswb %%mm3, %%mm4          \n\t"\
292                 "pxor %%mm7, %%mm7              \n\t"
293
294 // do vertical chrominance interpolation
295 #define YSCALEYUV2RGB1b \
296                 "xorl %%eax, %%eax              \n\t"\
297                 "1:                             \n\t"\
298                 "movq (%2, %%eax), %%mm2        \n\t" /* uvbuf0[eax]*/\
299                 "movq (%3, %%eax), %%mm3        \n\t" /* uvbuf1[eax]*/\
300                 "movq 4096(%2, %%eax), %%mm5    \n\t" /* uvbuf0[eax+2048]*/\
301                 "movq 4096(%3, %%eax), %%mm4    \n\t" /* uvbuf1[eax+2048]*/\
302                 "paddw %%mm2, %%mm3             \n\t" /* uvbuf0[eax] + uvbuf1[eax]*/\
303                 "paddw %%mm5, %%mm4             \n\t" /* uvbuf0[eax+2048] + uvbuf1[eax+2048]*/\
304                 "psrlw $5, %%mm3                \n\t"\
305                 "psrlw $5, %%mm4                \n\t"\
306                 "psubw w400, %%mm3              \n\t" /* (U-128)8*/\
307                 "psubw w400, %%mm4              \n\t" /* (V-128)8*/\
308                 "movq %%mm3, %%mm2              \n\t" /* (U-128)8*/\
309                 "movq %%mm4, %%mm5              \n\t" /* (V-128)8*/\
310                 "pmulhw ugCoeff, %%mm3          \n\t"\
311                 "pmulhw vgCoeff, %%mm4          \n\t"\
312         /* mm2=(U-128)8, mm3=ug, mm4=vg mm5=(V-128)8 */\
313                 "movq (%0, %%eax, 2), %%mm1     \n\t" /*buf0[eax]*/\
314                 "movq 8(%0, %%eax, 2), %%mm7    \n\t" /*buf0[eax]*/\
315                 "psraw $4, %%mm1                \n\t" /* buf0[eax] - buf1[eax] >>4*/\
316                 "psraw $4, %%mm7                \n\t" /* buf0[eax] - buf1[eax] >>4*/\
317                 "pmulhw ubCoeff, %%mm2          \n\t"\
318                 "pmulhw vrCoeff, %%mm5          \n\t"\
319                 "psubw w80, %%mm1               \n\t" /* 8(Y-16)*/\
320                 "psubw w80, %%mm7               \n\t" /* 8(Y-16)*/\
321                 "pmulhw yCoeff, %%mm1           \n\t"\
322                 "pmulhw yCoeff, %%mm7           \n\t"\
323         /* mm1= Y1, mm2=ub, mm3=ug, mm4=vg mm5=vr, mm7=Y2 */\
324                 "paddw %%mm3, %%mm4             \n\t"\
325                 "movq %%mm2, %%mm0              \n\t"\
326                 "movq %%mm5, %%mm6              \n\t"\
327                 "movq %%mm4, %%mm3              \n\t"\
328                 "punpcklwd %%mm2, %%mm2         \n\t"\
329                 "punpcklwd %%mm5, %%mm5         \n\t"\
330                 "punpcklwd %%mm4, %%mm4         \n\t"\
331                 "paddw %%mm1, %%mm2             \n\t"\
332                 "paddw %%mm1, %%mm5             \n\t"\
333                 "paddw %%mm1, %%mm4             \n\t"\
334                 "punpckhwd %%mm0, %%mm0         \n\t"\
335                 "punpckhwd %%mm6, %%mm6         \n\t"\
336                 "punpckhwd %%mm3, %%mm3         \n\t"\
337                 "paddw %%mm7, %%mm0             \n\t"\
338                 "paddw %%mm7, %%mm6             \n\t"\
339                 "paddw %%mm7, %%mm3             \n\t"\
340                 /* mm0=B1, mm2=B2, mm3=G2, mm4=G1, mm5=R1, mm6=R2 */\
341                 "packuswb %%mm0, %%mm2          \n\t"\
342                 "packuswb %%mm6, %%mm5          \n\t"\
343                 "packuswb %%mm3, %%mm4          \n\t"\
344                 "pxor %%mm7, %%mm7              \n\t"
345
346 #define WRITEBGR32 \
347                 /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */\
348                         "movq %%mm2, %%mm1              \n\t" /* B */\
349                         "movq %%mm5, %%mm6              \n\t" /* R */\
350                         "punpcklbw %%mm4, %%mm2         \n\t" /* GBGBGBGB 0 */\
351                         "punpcklbw %%mm7, %%mm5         \n\t" /* 0R0R0R0R 0 */\
352                         "punpckhbw %%mm4, %%mm1         \n\t" /* GBGBGBGB 2 */\
353                         "punpckhbw %%mm7, %%mm6         \n\t" /* 0R0R0R0R 2 */\
354                         "movq %%mm2, %%mm0              \n\t" /* GBGBGBGB 0 */\
355                         "movq %%mm1, %%mm3              \n\t" /* GBGBGBGB 2 */\
356                         "punpcklwd %%mm5, %%mm0         \n\t" /* 0RGB0RGB 0 */\
357                         "punpckhwd %%mm5, %%mm2         \n\t" /* 0RGB0RGB 1 */\
358                         "punpcklwd %%mm6, %%mm1         \n\t" /* 0RGB0RGB 2 */\
359                         "punpckhwd %%mm6, %%mm3         \n\t" /* 0RGB0RGB 3 */\
360 \
361                         MOVNTQ(%%mm0, (%4, %%eax, 4))\
362                         MOVNTQ(%%mm2, 8(%4, %%eax, 4))\
363                         MOVNTQ(%%mm1, 16(%4, %%eax, 4))\
364                         MOVNTQ(%%mm3, 24(%4, %%eax, 4))\
365 \
366                         "addl $8, %%eax                 \n\t"\
367                         "cmpl %5, %%eax                 \n\t"\
368                         " jb 1b                         \n\t"
369
370 #define WRITEBGR16 \
371                         "movq %%mm2, %%mm1              \n\t" /* B */\
372                         "movq %%mm4, %%mm3              \n\t" /* G */\
373                         "movq %%mm5, %%mm6              \n\t" /* R */\
374 \
375                         "punpcklbw %%mm7, %%mm3         \n\t" /* 0G0G0G0G */\
376                         "punpcklbw %%mm7, %%mm2         \n\t" /* 0B0B0B0B */\
377                         "punpcklbw %%mm7, %%mm5         \n\t" /* 0R0R0R0R */\
378 \
379                         "psrlw $3, %%mm2                \n\t"\
380                         "psllw $3, %%mm3                \n\t"\
381                         "psllw $8, %%mm5                \n\t"\
382 \
383                         "pand g16Mask, %%mm3            \n\t"\
384                         "pand r16Mask, %%mm5            \n\t"\
385 \
386                         "por %%mm3, %%mm2               \n\t"\
387                         "por %%mm5, %%mm2               \n\t"\
388 \
389                         "punpckhbw %%mm7, %%mm4         \n\t" /* 0G0G0G0G */\
390                         "punpckhbw %%mm7, %%mm1         \n\t" /* 0B0B0B0B */\
391                         "punpckhbw %%mm7, %%mm6         \n\t" /* 0R0R0R0R */\
392 \
393                         "psrlw $3, %%mm1                \n\t"\
394                         "psllw $3, %%mm4                \n\t"\
395                         "psllw $8, %%mm6                \n\t"\
396 \
397                         "pand g16Mask, %%mm4            \n\t"\
398                         "pand r16Mask, %%mm6            \n\t"\
399 \
400                         "por %%mm4, %%mm1               \n\t"\
401                         "por %%mm6, %%mm1               \n\t"\
402 \
403                         MOVNTQ(%%mm2, (%4, %%eax, 2))\
404                         MOVNTQ(%%mm1, 8(%4, %%eax, 2))\
405 \
406                         "addl $8, %%eax                 \n\t"\
407                         "cmpl %5, %%eax                 \n\t"\
408                         " jb 1b                         \n\t"
409
410 #define WRITEBGR15 \
411                         "movq %%mm2, %%mm1              \n\t" /* B */\
412                         "movq %%mm4, %%mm3              \n\t" /* G */\
413                         "movq %%mm5, %%mm6              \n\t" /* R */\
414 \
415                         "punpcklbw %%mm7, %%mm3         \n\t" /* 0G0G0G0G */\
416                         "punpcklbw %%mm7, %%mm2         \n\t" /* 0B0B0B0B */\
417                         "punpcklbw %%mm7, %%mm5         \n\t" /* 0R0R0R0R */\
418 \
419                         "psrlw $3, %%mm2                \n\t"\
420                         "psllw $2, %%mm3                \n\t"\
421                         "psllw $7, %%mm5                \n\t"\
422 \
423                         "pand g15Mask, %%mm3            \n\t"\
424                         "pand r15Mask, %%mm5            \n\t"\
425 \
426                         "por %%mm3, %%mm2               \n\t"\
427                         "por %%mm5, %%mm2               \n\t"\
428 \
429                         "punpckhbw %%mm7, %%mm4         \n\t" /* 0G0G0G0G */\
430                         "punpckhbw %%mm7, %%mm1         \n\t" /* 0B0B0B0B */\
431                         "punpckhbw %%mm7, %%mm6         \n\t" /* 0R0R0R0R */\
432 \
433                         "psrlw $3, %%mm1                \n\t"\
434                         "psllw $2, %%mm4                \n\t"\
435                         "psllw $7, %%mm6                \n\t"\
436 \
437                         "pand g15Mask, %%mm4            \n\t"\
438                         "pand r15Mask, %%mm6            \n\t"\
439 \
440                         "por %%mm4, %%mm1               \n\t"\
441                         "por %%mm6, %%mm1               \n\t"\
442 \
443                         MOVNTQ(%%mm2, (%4, %%eax, 2))\
444                         MOVNTQ(%%mm1, 8(%4, %%eax, 2))\
445 \
446                         "addl $8, %%eax                 \n\t"\
447                         "cmpl %5, %%eax                 \n\t"\
448                         " jb 1b                         \n\t"
449 // FIXME find a faster way to shuffle it to BGR24
450 #define WRITEBGR24 \
451                 /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */\
452                         "movq %%mm2, %%mm1              \n\t" /* B */\
453                         "movq %%mm5, %%mm6              \n\t" /* R */\
454                         "punpcklbw %%mm4, %%mm2         \n\t" /* GBGBGBGB 0 */\
455                         "punpcklbw %%mm7, %%mm5         \n\t" /* 0R0R0R0R 0 */\
456                         "punpckhbw %%mm4, %%mm1         \n\t" /* GBGBGBGB 2 */\
457                         "punpckhbw %%mm7, %%mm6         \n\t" /* 0R0R0R0R 2 */\
458                         "movq %%mm2, %%mm0              \n\t" /* GBGBGBGB 0 */\
459                         "movq %%mm1, %%mm3              \n\t" /* GBGBGBGB 2 */\
460                         "punpcklwd %%mm5, %%mm0         \n\t" /* 0RGB0RGB 0 */\
461                         "punpckhwd %%mm5, %%mm2         \n\t" /* 0RGB0RGB 1 */\
462                         "punpcklwd %%mm6, %%mm1         \n\t" /* 0RGB0RGB 2 */\
463                         "punpckhwd %%mm6, %%mm3         \n\t" /* 0RGB0RGB 3 */\
464 \
465                         "movq %%mm0, %%mm4              \n\t" /* 0RGB0RGB 0 */\
466                         "psrlq $8, %%mm0                \n\t" /* 00RGB0RG 0 */\
467                         "pand bm00000111, %%mm4         \n\t" /* 00000RGB 0 */\
468                         "pand bm11111000, %%mm0         \n\t" /* 00RGB000 0.5 */\
469                         "por %%mm4, %%mm0               \n\t" /* 00RGBRGB 0 */\
470                         "movq %%mm2, %%mm4              \n\t" /* 0RGB0RGB 1 */\
471                         "psllq $48, %%mm2               \n\t" /* GB000000 1 */\
472                         "por %%mm2, %%mm0               \n\t" /* GBRGBRGB 0 */\
473 \
474                         "movq %%mm4, %%mm2              \n\t" /* 0RGB0RGB 1 */\
475                         "psrld $16, %%mm4               \n\t" /* 000R000R 1 */\
476                         "psrlq $24, %%mm2               \n\t" /* 0000RGB0 1.5 */\
477                         "por %%mm4, %%mm2               \n\t" /* 000RRGBR 1 */\
478                         "pand bm00001111, %%mm2         \n\t" /* 0000RGBR 1 */\
479                         "movq %%mm1, %%mm4              \n\t" /* 0RGB0RGB 2 */\
480                         "psrlq $8, %%mm1                \n\t" /* 00RGB0RG 2 */\
481                         "pand bm00000111, %%mm4         \n\t" /* 00000RGB 2 */\
482                         "pand bm11111000, %%mm1         \n\t" /* 00RGB000 2.5 */\
483                         "por %%mm4, %%mm1               \n\t" /* 00RGBRGB 2 */\
484                         "movq %%mm1, %%mm4              \n\t" /* 00RGBRGB 2 */\
485                         "psllq $32, %%mm1               \n\t" /* BRGB0000 2 */\
486                         "por %%mm1, %%mm2               \n\t" /* BRGBRGBR 1 */\
487 \
488                         "psrlq $32, %%mm4               \n\t" /* 000000RG 2.5 */\
489                         "movq %%mm3, %%mm5              \n\t" /* 0RGB0RGB 3 */\
490                         "psrlq $8, %%mm3                \n\t" /* 00RGB0RG 3 */\
491                         "pand bm00000111, %%mm5         \n\t" /* 00000RGB 3 */\
492                         "pand bm11111000, %%mm3         \n\t" /* 00RGB000 3.5 */\
493                         "por %%mm5, %%mm3               \n\t" /* 00RGBRGB 3 */\
494                         "psllq $16, %%mm3               \n\t" /* RGBRGB00 3 */\
495                         "por %%mm4, %%mm3               \n\t" /* RGBRGBRG 2.5 */\
496 \
497                         "leal (%%eax, %%eax, 2), %%ebx  \n\t"\
498                         MOVNTQ(%%mm0, (%4, %%ebx))\
499                         MOVNTQ(%%mm2, 8(%4, %%ebx))\
500                         MOVNTQ(%%mm3, 16(%4, %%ebx))\
501 \
502                         "addl $8, %%eax                 \n\t"\
503                         "cmpl %5, %%eax                 \n\t"\
504                         " jb 1b                         \n\t"
505
506
507 static inline void yuv2yuv(uint16_t *buf0, uint16_t *buf1, uint16_t *uvbuf0, uint16_t *uvbuf1,
508                            uint8_t *dest, uint8_t *uDest, uint8_t *vDest, int dstw, int yalpha, int uvalpha)
509 {
510         int yalpha1=yalpha^4095;
511         int uvalpha1=uvalpha^4095;
512         int i;
513
514         asm volatile ("\n\t"::: "memory");
515
516         for(i=0;i<dstw;i++)
517         {
518                 ((uint8_t*)dest)[i] = (buf0[i]*yalpha1+buf1[i]*yalpha)>>19;
519         }
520
521         if(uvalpha != -1)
522         {
523                 for(i=0; i<(dstw>>1); i++)
524                 {
525                         ((uint8_t*)uDest)[i] = (uvbuf0[i]*uvalpha1+uvbuf1[i]*uvalpha)>>19;
526                         ((uint8_t*)vDest)[i] = (uvbuf0[i+2048]*uvalpha1+uvbuf1[i+2048]*uvalpha)>>19;
527                 }
528         }
529 }
530
531 /**
532  * vertical scale YV12 to RGB
533  */
534 static inline void yuv2rgbX(uint16_t *buf0, uint16_t *buf1, uint16_t *uvbuf0, uint16_t *uvbuf1,
535                             uint8_t *dest, int dstw, int yalpha, int uvalpha, int dstbpp)
536 {
537         int yalpha1=yalpha^4095;
538         int uvalpha1=uvalpha^4095;
539         int i;
540
541         if(fullUVIpol)
542         {
543
544 #ifdef HAVE_MMX
545                 if(dstbpp == 32)
546                 {
547                         asm volatile(
548
549
550 FULL_YSCALEYUV2RGB
551                         "punpcklbw %%mm1, %%mm3         \n\t" // BGBGBGBG
552                         "punpcklbw %%mm7, %%mm0         \n\t" // R0R0R0R0
553
554                         "movq %%mm3, %%mm1              \n\t"
555                         "punpcklwd %%mm0, %%mm3         \n\t" // BGR0BGR0
556                         "punpckhwd %%mm0, %%mm1         \n\t" // BGR0BGR0
557
558                         MOVNTQ(%%mm3, (%4, %%eax, 4))
559                         MOVNTQ(%%mm1, 8(%4, %%eax, 4))
560
561                         "addl $4, %%eax                 \n\t"
562                         "cmpl %5, %%eax                 \n\t"
563                         " jb 1b                         \n\t"
564
565
566                         :: "r" (buf0), "r" (buf1), "r" (uvbuf0), "r" (uvbuf1), "r" (dest), "m" (dstw),
567                         "m" (yalpha1), "m" (uvalpha1)
568                         : "%eax"
569                         );
570                 }
571                 else if(dstbpp==24)
572                 {
573                         asm volatile(
574
575 FULL_YSCALEYUV2RGB
576
577                                                                 // lsb ... msb
578                         "punpcklbw %%mm1, %%mm3         \n\t" // BGBGBGBG
579                         "punpcklbw %%mm7, %%mm0         \n\t" // R0R0R0R0
580
581                         "movq %%mm3, %%mm1              \n\t"
582                         "punpcklwd %%mm0, %%mm3         \n\t" // BGR0BGR0
583                         "punpckhwd %%mm0, %%mm1         \n\t" // BGR0BGR0
584
585                         "movq %%mm3, %%mm2              \n\t" // BGR0BGR0
586                         "psrlq $8, %%mm3                \n\t" // GR0BGR00
587                         "pand bm00000111, %%mm2         \n\t" // BGR00000
588                         "pand bm11111000, %%mm3         \n\t" // 000BGR00
589                         "por %%mm2, %%mm3               \n\t" // BGRBGR00
590                         "movq %%mm1, %%mm2              \n\t"
591                         "psllq $48, %%mm1               \n\t" // 000000BG
592                         "por %%mm1, %%mm3               \n\t" // BGRBGRBG
593
594                         "movq %%mm2, %%mm1              \n\t" // BGR0BGR0
595                         "psrld $16, %%mm2               \n\t" // R000R000
596                         "psrlq $24, %%mm1               \n\t" // 0BGR0000
597                         "por %%mm2, %%mm1               \n\t" // RBGRR000
598
599                         "movl %4, %%ebx                 \n\t"
600                         "addl %%eax, %%ebx              \n\t"
601
602 #ifdef HAVE_MMX2
603                         //FIXME Alignment
604                         "movntq %%mm3, (%%ebx, %%eax, 2)\n\t"
605                         "movntq %%mm1, 8(%%ebx, %%eax, 2)\n\t"
606 #else
607                         "movd %%mm3, (%%ebx, %%eax, 2)  \n\t"
608                         "psrlq $32, %%mm3               \n\t"
609                         "movd %%mm3, 4(%%ebx, %%eax, 2) \n\t"
610                         "movd %%mm1, 8(%%ebx, %%eax, 2) \n\t"
611 #endif
612                         "addl $4, %%eax                 \n\t"
613                         "cmpl %5, %%eax                 \n\t"
614                         " jb 1b                         \n\t"
615
616                         :: "r" (buf0), "r" (buf1), "r" (uvbuf0), "r" (uvbuf1), "m" (dest), "m" (dstw),
617                         "m" (yalpha1), "m" (uvalpha1)
618                         : "%eax", "%ebx"
619                         );
620                 }
621                 else if(dstbpp==15)
622                 {
623                         asm volatile(
624
625 FULL_YSCALEYUV2RGB
626 #ifdef DITHER1XBPP
627                         "paddusb b16Dither, %%mm1       \n\t"
628                         "paddusb b16Dither, %%mm0       \n\t"
629                         "paddusb b16Dither, %%mm3       \n\t"
630 #endif
631                         "punpcklbw %%mm7, %%mm1         \n\t" // 0G0G0G0G
632                         "punpcklbw %%mm7, %%mm3         \n\t" // 0B0B0B0B
633                         "punpcklbw %%mm7, %%mm0         \n\t" // 0R0R0R0R
634
635                         "psrlw $3, %%mm3                \n\t"
636                         "psllw $2, %%mm1                \n\t"
637                         "psllw $7, %%mm0                \n\t"
638                         "pand g15Mask, %%mm1            \n\t"
639                         "pand r15Mask, %%mm0            \n\t"
640
641                         "por %%mm3, %%mm1               \n\t"
642                         "por %%mm1, %%mm0               \n\t"
643
644                         MOVNTQ(%%mm0, (%4, %%eax, 2))
645
646                         "addl $4, %%eax                 \n\t"
647                         "cmpl %5, %%eax                 \n\t"
648                         " jb 1b                         \n\t"
649
650                         :: "r" (buf0), "r" (buf1), "r" (uvbuf0), "r" (uvbuf1), "r" (dest), "m" (dstw),
651                         "m" (yalpha1), "m" (uvalpha1)
652                         : "%eax"
653                         );
654                 }
655                 else if(dstbpp==16)
656                 {
657                         asm volatile(
658
659 FULL_YSCALEYUV2RGB
660 #ifdef DITHER1XBPP
661                         "paddusb g16Dither, %%mm1       \n\t"
662                         "paddusb b16Dither, %%mm0       \n\t"
663                         "paddusb b16Dither, %%mm3       \n\t"
664 #endif
665                         "punpcklbw %%mm7, %%mm1         \n\t" // 0G0G0G0G
666                         "punpcklbw %%mm7, %%mm3         \n\t" // 0B0B0B0B
667                         "punpcklbw %%mm7, %%mm0         \n\t" // 0R0R0R0R
668
669                         "psrlw $3, %%mm3                \n\t"
670                         "psllw $3, %%mm1                \n\t"
671                         "psllw $8, %%mm0                \n\t"
672                         "pand g16Mask, %%mm1            \n\t"
673                         "pand r16Mask, %%mm0            \n\t"
674
675                         "por %%mm3, %%mm1               \n\t"
676                         "por %%mm1, %%mm0               \n\t"
677
678                         MOVNTQ(%%mm0, (%4, %%eax, 2))
679
680                         "addl $4, %%eax                 \n\t"
681                         "cmpl %5, %%eax                 \n\t"
682                         " jb 1b                         \n\t"
683
684                         :: "r" (buf0), "r" (buf1), "r" (uvbuf0), "r" (uvbuf1), "r" (dest), "m" (dstw),
685                         "m" (yalpha1), "m" (uvalpha1)
686                         : "%eax"
687                         );
688                 }
689 #else
690                 asm volatile ("\n\t"::: "memory");
691
692                 if(dstbpp==32 || dstbpp==24)
693                 {
694                         for(i=0;i<dstw;i++){
695                                 // vertical linear interpolation && yuv2rgb in a single step:
696                                 int Y=yuvtab_2568[((buf0[i]*yalpha1+buf1[i]*yalpha)>>19)];
697                                 int U=((uvbuf0[i]*uvalpha1+uvbuf1[i]*uvalpha)>>19);
698                                 int V=((uvbuf0[i+2048]*uvalpha1+uvbuf1[i+2048]*uvalpha)>>19);
699                                 dest[0]=clip_table[((Y + yuvtab_40cf[U]) >>13)];
700                                 dest[1]=clip_table[((Y + yuvtab_1a1e[V] + yuvtab_0c92[U]) >>13)];
701                                 dest[2]=clip_table[((Y + yuvtab_3343[V]) >>13)];
702                                 dest+=dstbpp>>3;
703                         }
704                 }
705                 else if(dstbpp==16)
706                 {
707                         for(i=0;i<dstw;i++){
708                                 // vertical linear interpolation && yuv2rgb in a single step:
709                                 int Y=yuvtab_2568[((buf0[i]*yalpha1+buf1[i]*yalpha)>>19)];
710                                 int U=((uvbuf0[i]*uvalpha1+uvbuf1[i]*uvalpha)>>19);
711                                 int V=((uvbuf0[i+2048]*uvalpha1+uvbuf1[i+2048]*uvalpha)>>19);
712
713                                 ((uint16_t*)dest)[i] =
714                                         clip_table16b[(Y + yuvtab_40cf[U]) >>13] |
715                                         clip_table16g[(Y + yuvtab_1a1e[V] + yuvtab_0c92[U]) >>13] |
716                                         clip_table16r[(Y + yuvtab_3343[V]) >>13];
717                         }
718                 }
719                 else if(dstbpp==15)
720                 {
721                         for(i=0;i<dstw;i++){
722                                 // vertical linear interpolation && yuv2rgb in a single step:
723                                 int Y=yuvtab_2568[((buf0[i]*yalpha1+buf1[i]*yalpha)>>19)];
724                                 int U=((uvbuf0[i]*uvalpha1+uvbuf1[i]*uvalpha)>>19);
725                                 int V=((uvbuf0[i+2048]*uvalpha1+uvbuf1[i+2048]*uvalpha)>>19);
726
727                                 ((uint16_t*)dest)[i] =
728                                         clip_table15b[(Y + yuvtab_40cf[U]) >>13] |
729                                         clip_table15g[(Y + yuvtab_1a1e[V] + yuvtab_0c92[U]) >>13] |
730                                         clip_table15r[(Y + yuvtab_3343[V]) >>13];
731                         }
732                 }
733 #endif
734         }//FULL_UV_IPOL
735         else
736         {
737 #ifdef HAVE_MMX
738                 if(dstbpp == 32)
739                 {
740                         asm volatile(
741                                 YSCALEYUV2RGB
742                                 WRITEBGR32
743
744                         :: "r" (buf0), "r" (buf1), "r" (uvbuf0), "r" (uvbuf1), "r" (dest), "m" (dstw),
745                         "m" (yalpha1), "m" (uvalpha1)
746                         : "%eax"
747                         );
748                 }
749                 else if(dstbpp==24)
750                 {
751                         asm volatile(
752                                 YSCALEYUV2RGB
753                                 WRITEBGR24
754
755                         :: "r" (buf0), "r" (buf1), "r" (uvbuf0), "r" (uvbuf1), "r" (dest), "m" (dstw),
756                         "m" (yalpha1), "m" (uvalpha1)
757                         : "%eax", "%ebx"
758                         );
759                 }
760                 else if(dstbpp==15)
761                 {
762                         asm volatile(
763                                 YSCALEYUV2RGB
764                 /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */
765 #ifdef DITHER1XBPP
766                                 "paddusb b16Dither, %%mm2       \n\t"
767                                 "paddusb b16Dither, %%mm4       \n\t"
768                                 "paddusb b16Dither, %%mm5       \n\t"
769 #endif
770
771                                 WRITEBGR15
772
773                         :: "r" (buf0), "r" (buf1), "r" (uvbuf0), "r" (uvbuf1), "r" (dest), "m" (dstw),
774                         "m" (yalpha1), "m" (uvalpha1)
775                         : "%eax"
776                         );
777                 }
778                 else if(dstbpp==16)
779                 {
780                         asm volatile(
781                                 YSCALEYUV2RGB
782                 /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */
783 #ifdef DITHER1XBPP
784                                 "paddusb g16Dither, %%mm2       \n\t"
785                                 "paddusb b16Dither, %%mm4       \n\t"
786                                 "paddusb b16Dither, %%mm5       \n\t"
787 #endif
788
789                                 WRITEBGR16
790
791                         :: "r" (buf0), "r" (buf1), "r" (uvbuf0), "r" (uvbuf1), "r" (dest), "m" (dstw),
792                         "m" (yalpha1), "m" (uvalpha1)
793                         : "%eax"
794                         );
795                 }
796 #else
797                 asm volatile ("\n\t"::: "memory");
798
799                 if(dstbpp==32)
800                 {
801                         for(i=0; i<dstw-1; i+=2){
802                                 // vertical linear interpolation && yuv2rgb in a single step:
803                                 int Y1=yuvtab_2568[((buf0[i]*yalpha1+buf1[i]*yalpha)>>19)];
804                                 int Y2=yuvtab_2568[((buf0[i+1]*yalpha1+buf1[i+1]*yalpha)>>19)];
805                                 int U=((uvbuf0[i>>1]*uvalpha1+uvbuf1[i>>1]*uvalpha)>>19);
806                                 int V=((uvbuf0[(i>>1)+2048]*uvalpha1+uvbuf1[(i>>1)+2048]*uvalpha)>>19);
807
808                                 int Cb= yuvtab_40cf[U];
809                                 int Cg= yuvtab_1a1e[V] + yuvtab_0c92[U];
810                                 int Cr= yuvtab_3343[V];
811
812                                 dest[4*i+0]=clip_table[((Y1 + Cb) >>13)];
813                                 dest[4*i+1]=clip_table[((Y1 + Cg) >>13)];
814                                 dest[4*i+2]=clip_table[((Y1 + Cr) >>13)];
815
816                                 dest[4*i+4]=clip_table[((Y2 + Cb) >>13)];
817                                 dest[4*i+5]=clip_table[((Y2 + Cg) >>13)];
818                                 dest[4*i+6]=clip_table[((Y2 + Cr) >>13)];
819                         }
820                 }
821                 if(dstbpp==24)
822                 {
823                         for(i=0; i<dstw-1; i+=2){
824                                 // vertical linear interpolation && yuv2rgb in a single step:
825                                 int Y1=yuvtab_2568[((buf0[i]*yalpha1+buf1[i]*yalpha)>>19)];
826                                 int Y2=yuvtab_2568[((buf0[i+1]*yalpha1+buf1[i+1]*yalpha)>>19)];
827                                 int U=((uvbuf0[i>>1]*uvalpha1+uvbuf1[i>>1]*uvalpha)>>19);
828                                 int V=((uvbuf0[(i>>1)+2048]*uvalpha1+uvbuf1[(i>>1)+2048]*uvalpha)>>19);
829
830                                 int Cb= yuvtab_40cf[U];
831                                 int Cg= yuvtab_1a1e[V] + yuvtab_0c92[U];
832                                 int Cr= yuvtab_3343[V];
833
834                                 dest[0]=clip_table[((Y1 + Cb) >>13)];
835                                 dest[1]=clip_table[((Y1 + Cg) >>13)];
836                                 dest[2]=clip_table[((Y1 + Cr) >>13)];
837
838                                 dest[3]=clip_table[((Y2 + Cb) >>13)];
839                                 dest[4]=clip_table[((Y2 + Cg) >>13)];
840                                 dest[5]=clip_table[((Y2 + Cr) >>13)];
841                                 dest+=6;
842                         }
843                 }
844                 else if(dstbpp==16)
845                 {
846                         for(i=0; i<dstw-1; i+=2){
847                                 // vertical linear interpolation && yuv2rgb in a single step:
848                                 int Y1=yuvtab_2568[((buf0[i]*yalpha1+buf1[i]*yalpha)>>19)];
849                                 int Y2=yuvtab_2568[((buf0[i+1]*yalpha1+buf1[i+1]*yalpha)>>19)];
850                                 int U=((uvbuf0[i>>1]*uvalpha1+uvbuf1[i>>1]*uvalpha)>>19);
851                                 int V=((uvbuf0[(i>>1)+2048]*uvalpha1+uvbuf1[(i>>1)+2048]*uvalpha)>>19);
852
853                                 int Cb= yuvtab_40cf[U];
854                                 int Cg= yuvtab_1a1e[V] + yuvtab_0c92[U];
855                                 int Cr= yuvtab_3343[V];
856
857                                 ((uint16_t*)dest)[i] =
858                                         clip_table16b[(Y1 + Cb) >>13] |
859                                         clip_table16g[(Y1 + Cg) >>13] |
860                                         clip_table16r[(Y1 + Cr) >>13];
861
862                                 ((uint16_t*)dest)[i+1] =
863                                         clip_table16b[(Y2 + Cb) >>13] |
864                                         clip_table16g[(Y2 + Cg) >>13] |
865                                         clip_table16r[(Y2 + Cr) >>13];
866                         }
867                 }
868                 else if(dstbpp==15)
869                 {
870                         for(i=0; i<dstw-1; i+=2){
871                                 // vertical linear interpolation && yuv2rgb in a single step:
872                                 int Y1=yuvtab_2568[((buf0[i]*yalpha1+buf1[i]*yalpha)>>19)];
873                                 int Y2=yuvtab_2568[((buf0[i+1]*yalpha1+buf1[i+1]*yalpha)>>19)];
874                                 int U=((uvbuf0[i>>1]*uvalpha1+uvbuf1[i>>1]*uvalpha)>>19);
875                                 int V=((uvbuf0[(i>>1)+2048]*uvalpha1+uvbuf1[(i>>1)+2048]*uvalpha)>>19);
876
877                                 int Cb= yuvtab_40cf[U];
878                                 int Cg= yuvtab_1a1e[V] + yuvtab_0c92[U];
879                                 int Cr= yuvtab_3343[V];
880
881                                 ((uint16_t*)dest)[i] =
882                                         clip_table15b[(Y1 + Cb) >>13] |
883                                         clip_table15g[(Y1 + Cg) >>13] |
884                                         clip_table15r[(Y1 + Cr) >>13];
885
886                                 ((uint16_t*)dest)[i+1] =
887                                         clip_table15b[(Y2 + Cb) >>13] |
888                                         clip_table15g[(Y2 + Cg) >>13] |
889                                         clip_table15r[(Y2 + Cr) >>13];
890                         }
891                 }
892 #endif
893         } //!FULL_UV_IPOL
894 }
895
896 /**
897  * YV12 to RGB without scaling or interpolating
898  */
899 static inline void yuv2rgb1(uint16_t *buf0, uint16_t *buf1, uint16_t *uvbuf0, uint16_t *uvbuf1,
900                             uint8_t *dest, int dstw, int yalpha, int uvalpha, int dstbpp)
901 {
902         int yalpha1=yalpha^4095;
903         int uvalpha1=uvalpha^4095;
904         int i;
905         if(fullUVIpol || allwaysIpol)
906         {
907                 yuv2rgbX(buf0, buf1, uvbuf0, uvbuf1, dest, dstw, yalpha, uvalpha, dstbpp);
908                 return;
909         }
910         if( yalpha > 2048 ) buf0 = buf1;
911
912 #ifdef HAVE_MMX
913         if( uvalpha < 2048 ) // note this is not correct (shifts chrominance by 0.5 pixels) but its a bit faster
914         {
915                 if(dstbpp == 32)
916                 {
917                         asm volatile(
918                                 YSCALEYUV2RGB1
919                                 WRITEBGR32
920                         :: "r" (buf0), "r" (buf1), "r" (uvbuf0), "r" (uvbuf1), "r" (dest), "m" (dstw),
921                         "m" (yalpha1), "m" (uvalpha1)
922                         : "%eax"
923                         );
924                 }
925                 else if(dstbpp==24)
926                 {
927                         asm volatile(
928                                 YSCALEYUV2RGB1
929                                 WRITEBGR24
930                         :: "r" (buf0), "r" (buf1), "r" (uvbuf0), "r" (uvbuf1), "r" (dest), "m" (dstw),
931                         "m" (yalpha1), "m" (uvalpha1)
932                         : "%eax", "%ebx"
933                         );
934                 }
935                 else if(dstbpp==15)
936                 {
937                         asm volatile(
938                                 YSCALEYUV2RGB1
939                 /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */
940 #ifdef DITHER1XBPP
941                                 "paddusb b16Dither, %%mm2       \n\t"
942                                 "paddusb b16Dither, %%mm4       \n\t"
943                                 "paddusb b16Dither, %%mm5       \n\t"
944 #endif
945                                 WRITEBGR15
946                         :: "r" (buf0), "r" (buf1), "r" (uvbuf0), "r" (uvbuf1), "r" (dest), "m" (dstw),
947                         "m" (yalpha1), "m" (uvalpha1)
948                         : "%eax"
949                         );
950                 }
951                 else if(dstbpp==16)
952                 {
953                         asm volatile(
954                                 YSCALEYUV2RGB1
955                 /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */
956 #ifdef DITHER1XBPP
957                                 "paddusb g16Dither, %%mm2       \n\t"
958                                 "paddusb b16Dither, %%mm4       \n\t"
959                                 "paddusb b16Dither, %%mm5       \n\t"
960 #endif
961
962                                 WRITEBGR16
963                         :: "r" (buf0), "r" (buf1), "r" (uvbuf0), "r" (uvbuf1), "r" (dest), "m" (dstw),
964                         "m" (yalpha1), "m" (uvalpha1)
965                         : "%eax"
966                         );
967                 }
968         }
969         else
970         {
971                 if(dstbpp == 32)
972                 {
973                         asm volatile(
974                                 YSCALEYUV2RGB1b
975                                 WRITEBGR32
976                         :: "r" (buf0), "r" (buf1), "r" (uvbuf0), "r" (uvbuf1), "r" (dest), "m" (dstw),
977                         "m" (yalpha1), "m" (uvalpha1)
978                         : "%eax"
979                         );
980                 }
981                 else if(dstbpp==24)
982                 {
983                         asm volatile(
984                                 YSCALEYUV2RGB1b
985                                 WRITEBGR24
986                         :: "r" (buf0), "r" (buf1), "r" (uvbuf0), "r" (uvbuf1), "r" (dest), "m" (dstw),
987                         "m" (yalpha1), "m" (uvalpha1)
988                         : "%eax", "%ebx"
989                         );
990                 }
991                 else if(dstbpp==15)
992                 {
993                         asm volatile(
994                                 YSCALEYUV2RGB1b
995                 /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */
996 #ifdef DITHER1XBPP
997                                 "paddusb b16Dither, %%mm2       \n\t"
998                                 "paddusb b16Dither, %%mm4       \n\t"
999                                 "paddusb b16Dither, %%mm5       \n\t"
1000 #endif
1001                                 WRITEBGR15
1002                         :: "r" (buf0), "r" (buf1), "r" (uvbuf0), "r" (uvbuf1), "r" (dest), "m" (dstw),
1003                         "m" (yalpha1), "m" (uvalpha1)
1004                         : "%eax"
1005                         );
1006                 }
1007                 else if(dstbpp==16)
1008                 {
1009                         asm volatile(
1010                                 YSCALEYUV2RGB1b
1011                 /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */
1012 #ifdef DITHER1XBPP
1013                                 "paddusb g16Dither, %%mm2       \n\t"
1014                                 "paddusb b16Dither, %%mm4       \n\t"
1015                                 "paddusb b16Dither, %%mm5       \n\t"
1016 #endif
1017
1018                                 WRITEBGR16
1019                         :: "r" (buf0), "r" (buf1), "r" (uvbuf0), "r" (uvbuf1), "r" (dest), "m" (dstw),
1020                         "m" (yalpha1), "m" (uvalpha1)
1021                         : "%eax"
1022                         );
1023                 }
1024         }
1025 #else
1026 //FIXME write 2 versions (for even & odd lines)
1027         asm volatile ("\n\t"::: "memory");
1028
1029         if(dstbpp==32)
1030         {
1031                 for(i=0; i<dstw-1; i+=2){
1032                         // vertical linear interpolation && yuv2rgb in a single step:
1033                         int Y1=yuvtab_2568[buf0[i]>>7];
1034                         int Y2=yuvtab_2568[buf0[i+1]>>7];
1035                         int U=((uvbuf0[i>>1]*uvalpha1+uvbuf1[i>>1]*uvalpha)>>19);
1036                         int V=((uvbuf0[(i>>1)+2048]*uvalpha1+uvbuf1[(i>>1)+2048]*uvalpha)>>19);
1037
1038                         int Cb= yuvtab_40cf[U];
1039                         int Cg= yuvtab_1a1e[V] + yuvtab_0c92[U];
1040                         int Cr= yuvtab_3343[V];
1041
1042                         dest[4*i+0]=clip_table[((Y1 + Cb) >>13)];
1043                         dest[4*i+1]=clip_table[((Y1 + Cg) >>13)];
1044                         dest[4*i+2]=clip_table[((Y1 + Cr) >>13)];
1045
1046                         dest[4*i+4]=clip_table[((Y2 + Cb) >>13)];
1047                         dest[4*i+5]=clip_table[((Y2 + Cg) >>13)];
1048                         dest[4*i+6]=clip_table[((Y2 + Cr) >>13)];
1049                 }
1050         }
1051         if(dstbpp==24)
1052         {
1053                 for(i=0; i<dstw-1; i+=2){
1054                         // vertical linear interpolation && yuv2rgb in a single step:
1055                         int Y1=yuvtab_2568[buf0[i]>>7];
1056                         int Y2=yuvtab_2568[buf0[i+1]>>7];
1057                         int U=((uvbuf0[i>>1]*uvalpha1+uvbuf1[i>>1]*uvalpha)>>19);
1058                         int V=((uvbuf0[(i>>1)+2048]*uvalpha1+uvbuf1[(i>>1)+2048]*uvalpha)>>19);
1059
1060                         int Cb= yuvtab_40cf[U];
1061                         int Cg= yuvtab_1a1e[V] + yuvtab_0c92[U];
1062                         int Cr= yuvtab_3343[V];
1063
1064                         dest[0]=clip_table[((Y1 + Cb) >>13)];
1065                         dest[1]=clip_table[((Y1 + Cg) >>13)];
1066                         dest[2]=clip_table[((Y1 + Cr) >>13)];
1067
1068                         dest[3]=clip_table[((Y2 + Cb) >>13)];
1069                         dest[4]=clip_table[((Y2 + Cg) >>13)];
1070                         dest[5]=clip_table[((Y2 + Cr) >>13)];
1071                         dest+=6;
1072                 }
1073         }
1074         else if(dstbpp==16)
1075         {
1076                 for(i=0; i<dstw-1; i+=2){
1077                         // vertical linear interpolation && yuv2rgb in a single step:
1078                         int Y1=yuvtab_2568[buf0[i]>>7];
1079                         int Y2=yuvtab_2568[buf0[i+1]>>7];
1080                         int U=((uvbuf0[i>>1]*uvalpha1+uvbuf1[i>>1]*uvalpha)>>19);
1081                         int V=((uvbuf0[(i>>1)+2048]*uvalpha1+uvbuf1[(i>>1)+2048]*uvalpha)>>19);
1082
1083                         int Cb= yuvtab_40cf[U];
1084                         int Cg= yuvtab_1a1e[V] + yuvtab_0c92[U];
1085                         int Cr= yuvtab_3343[V];
1086
1087                         ((uint16_t*)dest)[i] =
1088                                 clip_table16b[(Y1 + Cb) >>13] |
1089                                 clip_table16g[(Y1 + Cg) >>13] |
1090                                 clip_table16r[(Y1 + Cr) >>13];
1091
1092                         ((uint16_t*)dest)[i+1] =
1093                                 clip_table16b[(Y2 + Cb) >>13] |
1094                                 clip_table16g[(Y2 + Cg) >>13] |
1095                                 clip_table16r[(Y2 + Cr) >>13];
1096                 }
1097         }
1098         else if(dstbpp==15)
1099         {
1100                 for(i=0; i<dstw-1; i+=2){
1101                         // vertical linear interpolation && yuv2rgb in a single step:
1102                         int Y1=yuvtab_2568[buf0[i]>>7];
1103                         int Y2=yuvtab_2568[buf0[i+1]>>7];
1104                         int U=((uvbuf0[i>>1]*uvalpha1+uvbuf1[i>>1]*uvalpha)>>19);
1105                         int V=((uvbuf0[(i>>1)+2048]*uvalpha1+uvbuf1[(i>>1)+2048]*uvalpha)>>19);
1106
1107                         int Cb= yuvtab_40cf[U];
1108                         int Cg= yuvtab_1a1e[V] + yuvtab_0c92[U];
1109                         int Cr= yuvtab_3343[V];
1110
1111                         ((uint16_t*)dest)[i] =
1112                                 clip_table15b[(Y1 + Cb) >>13] |
1113                                 clip_table15g[(Y1 + Cg) >>13] |
1114                                 clip_table15r[(Y1 + Cr) >>13];
1115
1116                         ((uint16_t*)dest)[i+1] =
1117                                 clip_table15b[(Y2 + Cb) >>13] |
1118                                 clip_table15g[(Y2 + Cg) >>13] |
1119                                 clip_table15r[(Y2 + Cr) >>13];
1120                 }
1121         }
1122 #endif
1123 }
1124
1125
1126 static inline void hyscale(uint16_t *dst, int dstWidth, uint8_t *src, int srcWidth, int xInc)
1127 {
1128         int i;
1129       unsigned int xpos=0;
1130       // *** horizontal scale Y line to temp buffer
1131 #ifdef ARCH_X86
1132 #ifdef HAVE_MMX2
1133         if(canMMX2BeUsed)
1134         {
1135                 asm volatile(
1136                         "pxor %%mm7, %%mm7              \n\t"
1137                         "pxor %%mm2, %%mm2              \n\t" // 2*xalpha
1138                         "movd %5, %%mm6                 \n\t" // xInc&0xFFFF
1139                         "punpcklwd %%mm6, %%mm6         \n\t"
1140                         "punpcklwd %%mm6, %%mm6         \n\t"
1141                         "movq %%mm6, %%mm2              \n\t"
1142                         "psllq $16, %%mm2               \n\t"
1143                         "paddw %%mm6, %%mm2             \n\t"
1144                         "psllq $16, %%mm2               \n\t"
1145                         "paddw %%mm6, %%mm2             \n\t"
1146                         "psllq $16, %%mm2               \n\t" //0,t,2t,3t               t=xInc&0xFF
1147                         "movq %%mm2, temp0              \n\t"
1148                         "movd %4, %%mm6                 \n\t" //(xInc*4)&0xFFFF
1149                         "punpcklwd %%mm6, %%mm6         \n\t"
1150                         "punpcklwd %%mm6, %%mm6         \n\t"
1151                         "xorl %%eax, %%eax              \n\t" // i
1152                         "movl %0, %%esi                 \n\t" // src
1153                         "movl %1, %%edi                 \n\t" // buf1
1154                         "movl %3, %%edx                 \n\t" // (xInc*4)>>16
1155                         "xorl %%ecx, %%ecx              \n\t"
1156                         "xorl %%ebx, %%ebx              \n\t"
1157                         "movw %4, %%bx                  \n\t" // (xInc*4)&0xFFFF
1158
1159 #define FUNNY_Y_CODE \
1160                         PREFETCH" 1024(%%esi)           \n\t"\
1161                         PREFETCH" 1056(%%esi)           \n\t"\
1162                         PREFETCH" 1088(%%esi)           \n\t"\
1163                         "call funnyYCode                \n\t"\
1164                         "movq temp0, %%mm2              \n\t"\
1165                         "xorl %%ecx, %%ecx              \n\t"
1166
1167 FUNNY_Y_CODE
1168 FUNNY_Y_CODE
1169 FUNNY_Y_CODE
1170 FUNNY_Y_CODE
1171 FUNNY_Y_CODE
1172 FUNNY_Y_CODE
1173 FUNNY_Y_CODE
1174 FUNNY_Y_CODE
1175
1176                         :: "m" (src), "m" (dst), "m" (dstWidth), "m" ((xInc*4)>>16),
1177                         "m" ((xInc*4)&0xFFFF), "m" (xInc&0xFFFF)
1178                         : "%eax", "%ebx", "%ecx", "%edx", "%esi", "%edi"
1179                 );
1180                 for(i=dstWidth-1; (i*xInc)>>16 >=srcWidth-1; i--) dst[i] = src[srcWidth-1]*128;
1181         }
1182         else
1183         {
1184 #endif
1185         //NO MMX just normal asm ...
1186         asm volatile(
1187                 "xorl %%eax, %%eax              \n\t" // i
1188                 "xorl %%ebx, %%ebx              \n\t" // xx
1189                 "xorl %%ecx, %%ecx              \n\t" // 2*xalpha
1190                 "1:                             \n\t"
1191                 "movzbl  (%0, %%ebx), %%edi     \n\t" //src[xx]
1192                 "movzbl 1(%0, %%ebx), %%esi     \n\t" //src[xx+1]
1193                 "subl %%edi, %%esi              \n\t" //src[xx+1] - src[xx]
1194                 "imull %%ecx, %%esi             \n\t" //(src[xx+1] - src[xx])*2*xalpha
1195                 "shll $16, %%edi                \n\t"
1196                 "addl %%edi, %%esi              \n\t" //src[xx+1]*2*xalpha + src[xx]*(1-2*xalpha)
1197                 "movl %1, %%edi                 \n\t"
1198                 "shrl $9, %%esi                 \n\t"
1199                 "movw %%si, (%%edi, %%eax, 2)   \n\t"
1200                 "addw %4, %%cx                  \n\t" //2*xalpha += xInc&0xFF
1201                 "adcl %3, %%ebx                 \n\t" //xx+= xInc>>8 + carry
1202
1203                 "movzbl (%0, %%ebx), %%edi      \n\t" //src[xx]
1204                 "movzbl 1(%0, %%ebx), %%esi     \n\t" //src[xx+1]
1205                 "subl %%edi, %%esi              \n\t" //src[xx+1] - src[xx]
1206                 "imull %%ecx, %%esi             \n\t" //(src[xx+1] - src[xx])*2*xalpha
1207                 "shll $16, %%edi                \n\t"
1208                 "addl %%edi, %%esi              \n\t" //src[xx+1]*2*xalpha + src[xx]*(1-2*xalpha)
1209                 "movl %1, %%edi                 \n\t"
1210                 "shrl $9, %%esi                 \n\t"
1211                 "movw %%si, 2(%%edi, %%eax, 2)  \n\t"
1212                 "addw %4, %%cx                  \n\t" //2*xalpha += xInc&0xFF
1213                 "adcl %3, %%ebx                 \n\t" //xx+= xInc>>8 + carry
1214
1215
1216                 "addl $2, %%eax                 \n\t"
1217                 "cmpl %2, %%eax                 \n\t"
1218                 " jb 1b                         \n\t"
1219
1220
1221                 :: "r" (src), "m" (dst), "m" (dstWidth), "m" (xInc>>16), "m" (xInc&0xFFFF)
1222                 : "%eax", "%ebx", "%ecx", "%edi", "%esi"
1223                 );
1224 #ifdef HAVE_MMX2
1225         } //if MMX2 cant be used
1226 #endif
1227 #else
1228       for(i=0;i<dstWidth;i++){
1229         register unsigned int xx=xpos>>16;
1230         register unsigned int xalpha=(xpos&0xFFFF)>>9;
1231         dst[i]= (src[xx]<<7) + (src[xx+1] - src[xx])*xalpha;
1232         xpos+=xInc;
1233       }
1234 #endif
1235 }
1236
1237 inline static void hcscale(uint16_t *dst, int dstWidth,
1238                                 uint8_t *src1, uint8_t *src2, int srcWidth, int xInc)
1239 {
1240         int xpos=0;
1241         int i;
1242 #ifdef ARCH_X86
1243 #ifdef HAVE_MMX2
1244         if(canMMX2BeUsed)
1245         {
1246                 asm volatile(
1247                 "pxor %%mm7, %%mm7              \n\t"
1248                 "pxor %%mm2, %%mm2              \n\t" // 2*xalpha
1249                 "movd %5, %%mm6                 \n\t" // xInc&0xFFFF
1250                 "punpcklwd %%mm6, %%mm6         \n\t"
1251                 "punpcklwd %%mm6, %%mm6         \n\t"
1252                 "movq %%mm6, %%mm2              \n\t"
1253                 "psllq $16, %%mm2               \n\t"
1254                 "paddw %%mm6, %%mm2             \n\t"
1255                 "psllq $16, %%mm2               \n\t"
1256                 "paddw %%mm6, %%mm2             \n\t"
1257                 "psllq $16, %%mm2               \n\t" //0,t,2t,3t               t=xInc&0xFFFF
1258                 "movq %%mm2, temp0              \n\t"
1259                 "movd %4, %%mm6                 \n\t" //(xInc*4)&0xFFFF
1260                 "punpcklwd %%mm6, %%mm6         \n\t"
1261                 "punpcklwd %%mm6, %%mm6         \n\t"
1262                 "xorl %%eax, %%eax              \n\t" // i
1263                 "movl %0, %%esi                 \n\t" // src
1264                 "movl %1, %%edi                 \n\t" // buf1
1265                 "movl %3, %%edx                 \n\t" // (xInc*4)>>16
1266                 "xorl %%ecx, %%ecx              \n\t"
1267                 "xorl %%ebx, %%ebx              \n\t"
1268                 "movw %4, %%bx                  \n\t" // (xInc*4)&0xFFFF
1269
1270 #define FUNNYUVCODE \
1271                         PREFETCH" 1024(%%esi)           \n\t"\
1272                         PREFETCH" 1056(%%esi)           \n\t"\
1273                         PREFETCH" 1088(%%esi)           \n\t"\
1274                         "call funnyUVCode               \n\t"\
1275                         "movq temp0, %%mm2              \n\t"\
1276                         "xorl %%ecx, %%ecx              \n\t"
1277
1278 FUNNYUVCODE
1279 FUNNYUVCODE
1280 FUNNYUVCODE
1281 FUNNYUVCODE
1282
1283 FUNNYUVCODE
1284 FUNNYUVCODE
1285 FUNNYUVCODE
1286 FUNNYUVCODE
1287                 "xorl %%eax, %%eax              \n\t" // i
1288                 "movl %6, %%esi                 \n\t" // src
1289                 "movl %1, %%edi                 \n\t" // buf1
1290                 "addl $4096, %%edi              \n\t"
1291
1292 FUNNYUVCODE
1293 FUNNYUVCODE
1294 FUNNYUVCODE
1295 FUNNYUVCODE
1296
1297 FUNNYUVCODE
1298 FUNNYUVCODE
1299 FUNNYUVCODE
1300 FUNNYUVCODE
1301
1302                 :: "m" (src1), "m" (dst), "m" (dstWidth), "m" ((xInc*4)>>16),
1303                   "m" ((xInc*4)&0xFFFF), "m" (xInc&0xFFFF), "m" (src2)
1304                 : "%eax", "%ebx", "%ecx", "%edx", "%esi", "%edi"
1305         );
1306                 for(i=dstWidth-1; (i*xInc)>>16 >=srcWidth/2-1; i--)
1307                 {
1308                         dst[i] = src1[srcWidth/2-1]*128;
1309                         dst[i+2048] = src2[srcWidth/2-1]*128;
1310                 }
1311         }
1312         else
1313         {
1314 #endif
1315         asm volatile(
1316                 "xorl %%eax, %%eax              \n\t" // i
1317                 "xorl %%ebx, %%ebx              \n\t" // xx
1318                 "xorl %%ecx, %%ecx              \n\t" // 2*xalpha
1319                 "1:                             \n\t"
1320                 "movl %0, %%esi                 \n\t"
1321                 "movzbl  (%%esi, %%ebx), %%edi  \n\t" //src[xx]
1322                 "movzbl 1(%%esi, %%ebx), %%esi  \n\t" //src[xx+1]
1323                 "subl %%edi, %%esi              \n\t" //src[xx+1] - src[xx]
1324                 "imull %%ecx, %%esi             \n\t" //(src[xx+1] - src[xx])*2*xalpha
1325                 "shll $16, %%edi                \n\t"
1326                 "addl %%edi, %%esi              \n\t" //src[xx+1]*2*xalpha + src[xx]*(1-2*xalpha)
1327                 "movl %1, %%edi                 \n\t"
1328                 "shrl $9, %%esi                 \n\t"
1329                 "movw %%si, (%%edi, %%eax, 2)   \n\t"
1330
1331                 "movzbl  (%5, %%ebx), %%edi     \n\t" //src[xx]
1332                 "movzbl 1(%5, %%ebx), %%esi     \n\t" //src[xx+1]
1333                 "subl %%edi, %%esi              \n\t" //src[xx+1] - src[xx]
1334                 "imull %%ecx, %%esi             \n\t" //(src[xx+1] - src[xx])*2*xalpha
1335                 "shll $16, %%edi                \n\t"
1336                 "addl %%edi, %%esi              \n\t" //src[xx+1]*2*xalpha + src[xx]*(1-2*xalpha)
1337                 "movl %1, %%edi                 \n\t"
1338                 "shrl $9, %%esi                 \n\t"
1339                 "movw %%si, 4096(%%edi, %%eax, 2)\n\t"
1340
1341                 "addw %4, %%cx                  \n\t" //2*xalpha += xInc&0xFF
1342                 "adcl %3, %%ebx                 \n\t" //xx+= xInc>>8 + carry
1343                 "addl $1, %%eax                 \n\t"
1344                 "cmpl %2, %%eax                 \n\t"
1345                 " jb 1b                         \n\t"
1346
1347                 :: "m" (src1), "m" (dst), "m" (dstWidth), "m" (xInc>>16), "m" (xInc&0xFFFF),
1348                 "r" (src2)
1349                 : "%eax", "%ebx", "%ecx", "%edi", "%esi"
1350                 );
1351 #ifdef HAVE_MMX2
1352         } //if MMX2 cant be used
1353 #endif
1354 #else
1355       for(i=0;i<dstWidth;i++){
1356           register unsigned int xx=xpos>>16;
1357           register unsigned int xalpha=(xpos&0xFFFF)>>9;
1358           dst[i]=(src1[xx]*(xalpha^127)+src1[xx+1]*xalpha);
1359           dst[i+2048]=(src2[xx]*(xalpha^127)+src2[xx+1]*xalpha);
1360 /* slower
1361           dst[i]= (src1[xx]<<7) + (src1[xx+1] - src1[xx])*xalpha;
1362           dst[i+2048]=(src2[xx]<<7) + (src2[xx+1] - src2[xx])*xalpha;
1363 */
1364           xpos+=xInc;
1365       }
1366 #endif
1367 }
1368
1369
1370 // *** bilinear scaling and yuv->rgb or yuv->yuv conversion of yv12 slices:
1371 // *** Note: it's called multiple times while decoding a frame, first time y==0
1372 // *** Designed to upscale, but may work for downscale too.
1373 // s_xinc = (src_width << 16) / dst_width
1374 // s_yinc = (src_height << 16) / dst_height
1375 void SwScale_YV12slice(unsigned char* srcptr[],int stride[], int y, int h,
1376                              uint8_t* dstptr[], int dststride, int dstw, int dstbpp,
1377                              unsigned int s_xinc,unsigned int s_yinc){
1378
1379 // scaling factors:
1380 //static int s_yinc=(vo_dga_src_height<<16)/vo_dga_vp_height;
1381 //static int s_xinc=(vo_dga_src_width<<8)/vo_dga_vp_width;
1382
1383 unsigned int s_xinc2;
1384
1385 static int s_srcypos; // points to the dst Pixels center in the source (0 is the center of pixel 0,0 in src)
1386 static int s_ypos;
1387
1388 // last horzontally interpolated lines, used to avoid unnecessary calculations
1389 static int s_last_ypos;
1390 static int s_last_y1pos;
1391
1392 static int static_dstw;
1393
1394 #ifdef HAVE_MMX2
1395 // used to detect a horizontal size change
1396 static int old_dstw= -1;
1397 static int old_s_xinc= -1;
1398 #endif
1399
1400 int srcWidth= (dstw*s_xinc + 0x8000)>>16;
1401 int dstUVw= fullUVIpol ? dstw : dstw/2;
1402 int i;
1403
1404 #ifdef HAVE_MMX2
1405 canMMX2BeUsed= (s_xinc <= 0x10000 && (dstw&31)==0 && (srcWidth&15)==0) ? 1 : 0;
1406 #endif
1407
1408 // match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src to pixel n-2 of dst
1409 // n-2 is the last chrominance sample available
1410 // FIXME this is not perfect, but noone shuld notice the difference, the more correct variant
1411 // would be like the vertical one, but that would require some special code for the
1412 // first and last pixel
1413 if(canMMX2BeUsed)       s_xinc+= 20;
1414 else                    s_xinc = ((srcWidth-2)<<16)/(dstw-2) - 20;
1415
1416 if(fullUVIpol && !(dstbpp==12))         s_xinc2= s_xinc>>1;
1417 else                                    s_xinc2= s_xinc;
1418   // force calculation of the horizontal interpolation of the first line
1419
1420   if(y==0){
1421         s_last_ypos=-99;
1422         s_last_y1pos=-99;
1423         s_srcypos= s_yinc/2 - 0x8000;
1424         s_ypos=0;
1425
1426         // clean the buffers so that no green stuff is drawen if the width is not sane (%8=0)
1427         for(i=dstw-2; i<dstw+20; i++)
1428         {
1429                 pix_buf_uv[0][i] = pix_buf_uv[1][i]
1430                 = pix_buf_uv[0][2048+i] = pix_buf_uv[1][2048+i] = 128;
1431                 pix_buf_uv[0][i/2] = pix_buf_uv[1][i/2]
1432                 = pix_buf_uv[0][2048+i/2] = pix_buf_uv[1][2048+i/2] = 128;
1433                 pix_buf_y[0][i]= pix_buf_y[1][i]= 0;
1434         }
1435
1436 #ifdef HAVE_MMX2
1437 // cant downscale !!!
1438         if((old_s_xinc != s_xinc || old_dstw!=dstw) && canMMX2BeUsed)
1439         {
1440                 uint8_t *fragment;
1441                 int imm8OfPShufW1;
1442                 int imm8OfPShufW2;
1443                 int fragmentLength;
1444
1445                 int xpos, xx, xalpha, i;
1446
1447                 old_s_xinc= s_xinc;
1448                 old_dstw= dstw;
1449
1450                 static_dstw= dstw;
1451
1452                 // create an optimized horizontal scaling routine
1453
1454                 //code fragment
1455
1456                 asm volatile(
1457                         "jmp 9f                         \n\t"
1458                 // Begin
1459                         "0:                             \n\t"
1460                         "movq (%%esi), %%mm0            \n\t" //FIXME Alignment
1461                         "movq %%mm0, %%mm1              \n\t"
1462                         "psrlq $8, %%mm0                \n\t"
1463                         "punpcklbw %%mm7, %%mm1 \n\t"
1464                         "movq %%mm2, %%mm3              \n\t"
1465                         "punpcklbw %%mm7, %%mm0 \n\t"
1466                         "addw %%bx, %%cx                \n\t" //2*xalpha += (4*s_xinc)&0xFFFF
1467                         "pshufw $0xFF, %%mm1, %%mm1     \n\t"
1468                         "1:                             \n\t"
1469                         "adcl %%edx, %%esi              \n\t" //xx+= (4*s_xinc)>>16 + carry
1470                         "pshufw $0xFF, %%mm0, %%mm0     \n\t"
1471                         "2:                             \n\t"
1472                         "psrlw $9, %%mm3                \n\t"
1473                         "psubw %%mm1, %%mm0             \n\t"
1474                         "pmullw %%mm3, %%mm0            \n\t"
1475                         "paddw %%mm6, %%mm2             \n\t" // 2*alpha += xpos&0xFFFF
1476                         "psllw $7, %%mm1                \n\t"
1477                         "paddw %%mm1, %%mm0             \n\t"
1478
1479                         "movq %%mm0, (%%edi, %%eax)     \n\t"
1480
1481                         "addl $8, %%eax                 \n\t"
1482                 // End
1483                         "9:                             \n\t"
1484 //              "int $3\n\t"
1485                         "leal 0b, %0                    \n\t"
1486                         "leal 1b, %1                    \n\t"
1487                         "leal 2b, %2                    \n\t"
1488                         "decl %1                        \n\t"
1489                         "decl %2                        \n\t"
1490                         "subl %0, %1                    \n\t"
1491                         "subl %0, %2                    \n\t"
1492                         "leal 9b, %3                    \n\t"
1493                         "subl %0, %3                    \n\t"
1494                         :"=r" (fragment), "=r" (imm8OfPShufW1), "=r" (imm8OfPShufW2),
1495                          "=r" (fragmentLength)
1496                 );
1497
1498                 xpos= 0; //s_xinc/2 - 0x8000; // difference between pixel centers
1499
1500                 /* choose xinc so that all 8 parts fit exactly
1501                    Note: we cannot use just 1 part because it would not fit in the code cache */
1502 //              s_xinc2_diff= -((((s_xinc2*(dstw/8))&0xFFFF))/(dstw/8))-10;
1503 //              s_xinc_diff= -((((s_xinc*(dstw/8))&0xFFFF))/(dstw/8));
1504 #ifdef ALT_ERROR
1505 //              s_xinc2_diff+= ((0x10000/(dstw/8)));
1506 #endif
1507 //              s_xinc_diff= s_xinc2_diff*2;
1508
1509 //              s_xinc2+= s_xinc2_diff;
1510 //              s_xinc+= s_xinc_diff;
1511
1512 //              old_s_xinc= s_xinc;
1513
1514                 for(i=0; i<dstw/8; i++)
1515                 {
1516                         int xx=xpos>>16;
1517
1518                         if((i&3) == 0)
1519                         {
1520                                 int a=0;
1521                                 int b=((xpos+s_xinc)>>16) - xx;
1522                                 int c=((xpos+s_xinc*2)>>16) - xx;
1523                                 int d=((xpos+s_xinc*3)>>16) - xx;
1524
1525                                 memcpy(funnyYCode + fragmentLength*i/4, fragment, fragmentLength);
1526
1527                                 funnyYCode[fragmentLength*i/4 + imm8OfPShufW1]=
1528                                 funnyYCode[fragmentLength*i/4 + imm8OfPShufW2]=
1529                                         a | (b<<2) | (c<<4) | (d<<6);
1530
1531                                 // if we dont need to read 8 bytes than dont :), reduces the chance of
1532                                 // crossing a cache line
1533                                 if(d<3) funnyYCode[fragmentLength*i/4 + 1]= 0x6E;
1534
1535                                 funnyYCode[fragmentLength*(i+4)/4]= RET;
1536                         }
1537                         xpos+=s_xinc;
1538                 }
1539
1540                 xpos= 0; //s_xinc2/2 - 0x10000; // difference between centers of chrom samples
1541                 for(i=0; i<dstUVw/8; i++)
1542                 {
1543                         int xx=xpos>>16;
1544
1545                         if((i&3) == 0)
1546                         {
1547                                 int a=0;
1548                                 int b=((xpos+s_xinc2)>>16) - xx;
1549                                 int c=((xpos+s_xinc2*2)>>16) - xx;
1550                                 int d=((xpos+s_xinc2*3)>>16) - xx;
1551
1552                                 memcpy(funnyUVCode + fragmentLength*i/4, fragment, fragmentLength);
1553
1554                                 funnyUVCode[fragmentLength*i/4 + imm8OfPShufW1]=
1555                                 funnyUVCode[fragmentLength*i/4 + imm8OfPShufW2]=
1556                                         a | (b<<2) | (c<<4) | (d<<6);
1557
1558                                 // if we dont need to read 8 bytes than dont :), reduces the chance of
1559                                 // crossing a cache line
1560                                 if(d<3) funnyUVCode[fragmentLength*i/4 + 1]= 0x6E;
1561
1562                                 funnyUVCode[fragmentLength*(i+4)/4]= RET;
1563                         }
1564                         xpos+=s_xinc2;
1565                 }
1566 //              funnyCode[0]= RET;
1567         }
1568
1569 #endif // HAVE_MMX2
1570   } // reset counters
1571
1572   while(1){
1573     unsigned char *dest =dstptr[0]+dststride*s_ypos;
1574     unsigned char *uDest=dstptr[1]+(dststride>>1)*(s_ypos>>1);
1575     unsigned char *vDest=dstptr[2]+(dststride>>1)*(s_ypos>>1);
1576
1577     int y0=(s_srcypos + 0xFFFF)>>16;  // first luminance source line number below the dst line
1578         // points to the dst Pixels center in the source (0 is the center of pixel 0,0 in src)
1579     int srcuvpos= dstbpp==12 ?  s_srcypos + s_yinc/2 - 0x8000 :
1580                                 s_srcypos - 0x8000;
1581     int y1=(srcuvpos + 0x1FFFF)>>17; // first chrominance source line number below the dst line
1582     int yalpha=((s_srcypos-1)&0xFFFF)>>4;
1583     int uvalpha=((srcuvpos-1)&0x1FFFF)>>5;
1584     uint16_t *buf0=pix_buf_y[y0&1];             // top line of the interpolated slice
1585     uint16_t *buf1=pix_buf_y[((y0+1)&1)];       // bottom line of the interpolated slice
1586     uint16_t *uvbuf0=pix_buf_uv[y1&1];          // top line of the interpolated slice
1587     uint16_t *uvbuf1=pix_buf_uv[(y1+1)&1];      // bottom line of the interpolated slice
1588     int i;
1589
1590     if(y0>=y+h) break; // FIXME wrong, skips last lines, but they are dupliactes anyway
1591
1592     if((y0&1) && dstbpp==12) uvalpha=-1; // there is no alpha if there is no line
1593
1594     s_ypos++; s_srcypos+=s_yinc;
1595
1596     //only interpolate the src line horizontally if we didnt do it allready
1597         if(s_last_ypos!=y0)
1598         {
1599                 unsigned char *src;
1600                 // skip if first line has been horiz scaled alleady
1601                 if(s_last_ypos != y0-1)
1602                 {
1603                         // check if first line is before any available src lines
1604                         if(y0-1 < y)    src=srcptr[0]+(0     )*stride[0];
1605                         else            src=srcptr[0]+(y0-y-1)*stride[0];
1606
1607                         hyscale(buf0, dstw, src, srcWidth, s_xinc);
1608                 }
1609                 // check if second line is after any available src lines
1610                 if(y0-y >= h)   src=srcptr[0]+(h-1)*stride[0];
1611                 else            src=srcptr[0]+(y0-y)*stride[0];
1612
1613                 // the min() is required to avoid reuseing lines which where not available
1614                 s_last_ypos= MIN(y0, y+h-1);
1615                 hyscale(buf1, dstw, src, srcWidth, s_xinc);
1616         }
1617 //      printf("%d %d %d %d\n", y, y1, s_last_y1pos, h);
1618       // *** horizontal scale U and V lines to temp buffer
1619         if(s_last_y1pos!=y1)
1620         {
1621                 uint8_t *src1, *src2;
1622                 // skip if first line has been horiz scaled alleady
1623                 if(s_last_y1pos != y1-1)
1624                 {
1625                         // check if first line is before any available src lines
1626                         if(y1-y/2-1 < 0)
1627                         {
1628                                 src1= srcptr[1]+(0)*stride[1];
1629                                 src2= srcptr[2]+(0)*stride[2];
1630                         }else{
1631                                 src1= srcptr[1]+(y1-y/2-1)*stride[1];
1632                                 src2= srcptr[2]+(y1-y/2-1)*stride[2];
1633                         }
1634                         hcscale(uvbuf0, dstUVw, src1, src2, srcWidth, s_xinc2);
1635                 }
1636
1637                 // check if second line is after any available src lines
1638                 if(y1 - y/2 >= h/2)
1639                 {
1640                         src1= srcptr[1]+(h/2-1)*stride[1];
1641                         src2= srcptr[2]+(h/2-1)*stride[2];
1642                 }else{
1643                         src1= srcptr[1]+(y1-y/2)*stride[1];
1644                         src2= srcptr[2]+(y1-y/2)*stride[2];
1645                 }
1646                 hcscale(uvbuf1, dstUVw, src1, src2, srcWidth, s_xinc2);
1647
1648                 // the min() is required to avoid reuseing lines which where not available
1649                 s_last_y1pos= MIN(y1, y/2+h/2-1);
1650         }
1651
1652         if(dstbpp==12) //YV12
1653                 yuv2yuv(buf0, buf1, uvbuf0, uvbuf1, dest, uDest, vDest, dstw, yalpha, uvalpha);
1654         else if(ABS(s_yinc - 0x10000) < 10)
1655                 yuv2rgb1(buf0, buf1, uvbuf0, uvbuf1, dest, dstw, yalpha, uvalpha, dstbpp);
1656         else
1657                 yuv2rgbX(buf0, buf1, uvbuf0, uvbuf1, dest, dstw, yalpha, uvalpha, dstbpp);
1658
1659 #ifdef HAVE_MMX
1660         b16Dither= b16Dither1;
1661         b16Dither1= b16Dither2;
1662         b16Dither2= b16Dither;
1663
1664         g16Dither= g16Dither1;
1665         g16Dither1= g16Dither2;
1666         g16Dither2= g16Dither;
1667 #endif
1668   }
1669
1670 #ifdef HAVE_MMX
1671         __asm __volatile(SFENCE:::"memory");
1672         __asm __volatile(EMMS:::"memory");
1673 #endif
1674 }
1675
1676
1677 void SwScale_Init(){
1678     // generating tables:
1679     int i;
1680     for(i=0;i<256;i++){
1681         clip_table[i]=0;
1682         clip_table[i+256]=i;
1683         clip_table[i+512]=255;
1684         yuvtab_2568[i]=(0x2568*(i-16))+(256<<13);
1685         yuvtab_3343[i]=0x3343*(i-128);
1686         yuvtab_0c92[i]=-0x0c92*(i-128);
1687         yuvtab_1a1e[i]=-0x1a1e*(i-128);
1688         yuvtab_40cf[i]=0x40cf*(i-128);
1689     }
1690
1691     for(i=0; i<768; i++)
1692     {
1693         int v= clip_table[i];
1694         clip_table16b[i]= v>>3;
1695         clip_table16g[i]= (v<<3)&0x07E0;
1696         clip_table16r[i]= (v<<8)&0xF800;
1697         clip_table15b[i]= v>>3;
1698         clip_table15g[i]= (v<<2)&0x03E0;
1699         clip_table15r[i]= (v<<7)&0x7C00;
1700     }
1701
1702 }