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