]> git.sesse.net Git - ffmpeg/blob - postproc/swscale_template.c
e92364fd704c5f95a4948a6694ce21a50405c327
[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                         "leal (%%eax, %%eax, 2), %%ebx  \n\t"\
478                         MOVNTQ(%%mm0, (%4, %%ebx))\
479                         MOVNTQ(%%mm2, 8(%4, %%ebx))\
480                         MOVNTQ(%%mm3, 16(%4, %%ebx))\
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                                 YSCALEYUV2RGB
744                                 WRITEBGR24
745
746                         :: "r" (buf0), "r" (buf1), "r" (uvbuf0), "r" (uvbuf1), "r" (dest), "m" (dstw),
747                         "m" (yalpha1), "m" (uvalpha1)
748                         : "%eax", "%ebx"
749                         );
750                 }
751                 else if(dstbpp==15)
752                 {
753                         asm volatile(
754                                 YSCALEYUV2RGB
755                 /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */
756 #ifdef DITHER1XBPP
757                                 "paddusb b16Dither, %%mm2       \n\t"
758                                 "paddusb b16Dither, %%mm4       \n\t"
759                                 "paddusb b16Dither, %%mm5       \n\t"
760 #endif
761
762                                 WRITEBGR15
763
764                         :: "r" (buf0), "r" (buf1), "r" (uvbuf0), "r" (uvbuf1), "r" (dest), "m" (dstw),
765                         "m" (yalpha1), "m" (uvalpha1)
766                         : "%eax"
767                         );
768                 }
769                 else if(dstbpp==16)
770                 {
771                         asm volatile(
772                                 YSCALEYUV2RGB
773                 /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */
774 #ifdef DITHER1XBPP
775                                 "paddusb g16Dither, %%mm2       \n\t"
776                                 "paddusb b16Dither, %%mm4       \n\t"
777                                 "paddusb b16Dither, %%mm5       \n\t"
778 #endif
779
780                                 WRITEBGR16
781
782                         :: "r" (buf0), "r" (buf1), "r" (uvbuf0), "r" (uvbuf1), "r" (dest), "m" (dstw),
783                         "m" (yalpha1), "m" (uvalpha1)
784                         : "%eax"
785                         );
786                 }
787 #else
788                 asm volatile ("\n\t"::: "memory");
789
790                 if(dstbpp==32)
791                 {
792                         int i;
793                         for(i=0; i<dstw-1; i+=2){
794                                 // vertical linear interpolation && yuv2rgb in a single step:
795                                 int Y1=yuvtab_2568[((buf0[i]*yalpha1+buf1[i]*yalpha)>>19)];
796                                 int Y2=yuvtab_2568[((buf0[i+1]*yalpha1+buf1[i+1]*yalpha)>>19)];
797                                 int U=((uvbuf0[i>>1]*uvalpha1+uvbuf1[i>>1]*uvalpha)>>19);
798                                 int V=((uvbuf0[(i>>1)+2048]*uvalpha1+uvbuf1[(i>>1)+2048]*uvalpha)>>19);
799
800                                 int Cb= yuvtab_40cf[U];
801                                 int Cg= yuvtab_1a1e[V] + yuvtab_0c92[U];
802                                 int Cr= yuvtab_3343[V];
803
804                                 dest[4*i+0]=clip_table[((Y1 + Cb) >>13)];
805                                 dest[4*i+1]=clip_table[((Y1 + Cg) >>13)];
806                                 dest[4*i+2]=clip_table[((Y1 + Cr) >>13)];
807
808                                 dest[4*i+4]=clip_table[((Y2 + Cb) >>13)];
809                                 dest[4*i+5]=clip_table[((Y2 + Cg) >>13)];
810                                 dest[4*i+6]=clip_table[((Y2 + Cr) >>13)];
811                         }
812                 }
813                 if(dstbpp==24)
814                 {
815                         int i;
816                         for(i=0; i<dstw-1; i+=2){
817                                 // vertical linear interpolation && yuv2rgb in a single step:
818                                 int Y1=yuvtab_2568[((buf0[i]*yalpha1+buf1[i]*yalpha)>>19)];
819                                 int Y2=yuvtab_2568[((buf0[i+1]*yalpha1+buf1[i+1]*yalpha)>>19)];
820                                 int U=((uvbuf0[i>>1]*uvalpha1+uvbuf1[i>>1]*uvalpha)>>19);
821                                 int V=((uvbuf0[(i>>1)+2048]*uvalpha1+uvbuf1[(i>>1)+2048]*uvalpha)>>19);
822
823                                 int Cb= yuvtab_40cf[U];
824                                 int Cg= yuvtab_1a1e[V] + yuvtab_0c92[U];
825                                 int Cr= yuvtab_3343[V];
826
827                                 dest[0]=clip_table[((Y1 + Cb) >>13)];
828                                 dest[1]=clip_table[((Y1 + Cg) >>13)];
829                                 dest[2]=clip_table[((Y1 + Cr) >>13)];
830
831                                 dest[3]=clip_table[((Y2 + Cb) >>13)];
832                                 dest[4]=clip_table[((Y2 + Cg) >>13)];
833                                 dest[5]=clip_table[((Y2 + Cr) >>13)];
834                                 dest+=6;
835                         }
836                 }
837                 else if(dstbpp==16)
838                 {
839                         int i;
840                         for(i=0; i<dstw-1; i+=2){
841                                 // vertical linear interpolation && yuv2rgb in a single step:
842                                 int Y1=yuvtab_2568[((buf0[i]*yalpha1+buf1[i]*yalpha)>>19)];
843                                 int Y2=yuvtab_2568[((buf0[i+1]*yalpha1+buf1[i+1]*yalpha)>>19)];
844                                 int U=((uvbuf0[i>>1]*uvalpha1+uvbuf1[i>>1]*uvalpha)>>19);
845                                 int V=((uvbuf0[(i>>1)+2048]*uvalpha1+uvbuf1[(i>>1)+2048]*uvalpha)>>19);
846
847                                 int Cb= yuvtab_40cf[U];
848                                 int Cg= yuvtab_1a1e[V] + yuvtab_0c92[U];
849                                 int Cr= yuvtab_3343[V];
850
851                                 ((uint16_t*)dest)[i] =
852                                         clip_table16b[(Y1 + Cb) >>13] |
853                                         clip_table16g[(Y1 + Cg) >>13] |
854                                         clip_table16r[(Y1 + Cr) >>13];
855
856                                 ((uint16_t*)dest)[i+1] =
857                                         clip_table16b[(Y2 + Cb) >>13] |
858                                         clip_table16g[(Y2 + Cg) >>13] |
859                                         clip_table16r[(Y2 + Cr) >>13];
860                         }
861                 }
862                 else if(dstbpp==15)
863                 {
864                         int i;
865                         for(i=0; i<dstw-1; i+=2){
866                                 // vertical linear interpolation && yuv2rgb in a single step:
867                                 int Y1=yuvtab_2568[((buf0[i]*yalpha1+buf1[i]*yalpha)>>19)];
868                                 int Y2=yuvtab_2568[((buf0[i+1]*yalpha1+buf1[i+1]*yalpha)>>19)];
869                                 int U=((uvbuf0[i>>1]*uvalpha1+uvbuf1[i>>1]*uvalpha)>>19);
870                                 int V=((uvbuf0[(i>>1)+2048]*uvalpha1+uvbuf1[(i>>1)+2048]*uvalpha)>>19);
871
872                                 int Cb= yuvtab_40cf[U];
873                                 int Cg= yuvtab_1a1e[V] + yuvtab_0c92[U];
874                                 int Cr= yuvtab_3343[V];
875
876                                 ((uint16_t*)dest)[i] =
877                                         clip_table15b[(Y1 + Cb) >>13] |
878                                         clip_table15g[(Y1 + Cg) >>13] |
879                                         clip_table15r[(Y1 + Cr) >>13];
880
881                                 ((uint16_t*)dest)[i+1] =
882                                         clip_table15b[(Y2 + Cb) >>13] |
883                                         clip_table15g[(Y2 + Cg) >>13] |
884                                         clip_table15r[(Y2 + Cr) >>13];
885                         }
886                 }
887 #endif
888         } //!FULL_UV_IPOL
889 }
890
891 /**
892  * YV12 to RGB without scaling or interpolating
893  */
894 static inline void yuv2rgb1(uint16_t *buf0, uint16_t *buf1, uint16_t *uvbuf0, uint16_t *uvbuf1,
895                             uint8_t *dest, int dstw, int yalpha, int uvalpha, int dstbpp)
896 {
897         int uvalpha1=uvalpha^4095;
898 #ifdef HAVE_MMX
899         int yalpha1=yalpha^4095;
900 #endif
901
902         if(fullUVIpol || allwaysIpol)
903         {
904                 yuv2rgbX(buf0, buf1, uvbuf0, uvbuf1, dest, dstw, yalpha, uvalpha, dstbpp);
905                 return;
906         }
907         if( yalpha > 2048 ) buf0 = buf1;
908
909 #ifdef HAVE_MMX
910         if( uvalpha < 2048 ) // note this is not correct (shifts chrominance by 0.5 pixels) but its a bit faster
911         {
912                 if(dstbpp == 32)
913                 {
914                         asm volatile(
915                                 YSCALEYUV2RGB1
916                                 WRITEBGR32
917                         :: "r" (buf0), "r" (buf1), "r" (uvbuf0), "r" (uvbuf1), "r" (dest), "m" (dstw),
918                         "m" (yalpha1), "m" (uvalpha1)
919                         : "%eax"
920                         );
921                 }
922                 else if(dstbpp==24)
923                 {
924                         asm volatile(
925                                 YSCALEYUV2RGB1
926                                 WRITEBGR24
927                         :: "r" (buf0), "r" (buf1), "r" (uvbuf0), "r" (uvbuf1), "r" (dest), "m" (dstw),
928                         "m" (yalpha1), "m" (uvalpha1)
929                         : "%eax", "%ebx"
930                         );
931                 }
932                 else if(dstbpp==15)
933                 {
934                         asm volatile(
935                                 YSCALEYUV2RGB1
936                 /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */
937 #ifdef DITHER1XBPP
938                                 "paddusb b16Dither, %%mm2       \n\t"
939                                 "paddusb b16Dither, %%mm4       \n\t"
940                                 "paddusb b16Dither, %%mm5       \n\t"
941 #endif
942                                 WRITEBGR15
943                         :: "r" (buf0), "r" (buf1), "r" (uvbuf0), "r" (uvbuf1), "r" (dest), "m" (dstw),
944                         "m" (yalpha1), "m" (uvalpha1)
945                         : "%eax"
946                         );
947                 }
948                 else if(dstbpp==16)
949                 {
950                         asm volatile(
951                                 YSCALEYUV2RGB1
952                 /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */
953 #ifdef DITHER1XBPP
954                                 "paddusb g16Dither, %%mm2       \n\t"
955                                 "paddusb b16Dither, %%mm4       \n\t"
956                                 "paddusb b16Dither, %%mm5       \n\t"
957 #endif
958
959                                 WRITEBGR16
960                         :: "r" (buf0), "r" (buf1), "r" (uvbuf0), "r" (uvbuf1), "r" (dest), "m" (dstw),
961                         "m" (yalpha1), "m" (uvalpha1)
962                         : "%eax"
963                         );
964                 }
965         }
966         else
967         {
968                 if(dstbpp == 32)
969                 {
970                         asm volatile(
971                                 YSCALEYUV2RGB1b
972                                 WRITEBGR32
973                         :: "r" (buf0), "r" (buf1), "r" (uvbuf0), "r" (uvbuf1), "r" (dest), "m" (dstw),
974                         "m" (yalpha1), "m" (uvalpha1)
975                         : "%eax"
976                         );
977                 }
978                 else if(dstbpp==24)
979                 {
980                         asm volatile(
981                                 YSCALEYUV2RGB1b
982                                 WRITEBGR24
983                         :: "r" (buf0), "r" (buf1), "r" (uvbuf0), "r" (uvbuf1), "r" (dest), "m" (dstw),
984                         "m" (yalpha1), "m" (uvalpha1)
985                         : "%eax", "%ebx"
986                         );
987                 }
988                 else if(dstbpp==15)
989                 {
990                         asm volatile(
991                                 YSCALEYUV2RGB1b
992                 /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */
993 #ifdef DITHER1XBPP
994                                 "paddusb b16Dither, %%mm2       \n\t"
995                                 "paddusb b16Dither, %%mm4       \n\t"
996                                 "paddusb b16Dither, %%mm5       \n\t"
997 #endif
998                                 WRITEBGR15
999                         :: "r" (buf0), "r" (buf1), "r" (uvbuf0), "r" (uvbuf1), "r" (dest), "m" (dstw),
1000                         "m" (yalpha1), "m" (uvalpha1)
1001                         : "%eax"
1002                         );
1003                 }
1004                 else if(dstbpp==16)
1005                 {
1006                         asm volatile(
1007                                 YSCALEYUV2RGB1b
1008                 /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */
1009 #ifdef DITHER1XBPP
1010                                 "paddusb g16Dither, %%mm2       \n\t"
1011                                 "paddusb b16Dither, %%mm4       \n\t"
1012                                 "paddusb b16Dither, %%mm5       \n\t"
1013 #endif
1014
1015                                 WRITEBGR16
1016                         :: "r" (buf0), "r" (buf1), "r" (uvbuf0), "r" (uvbuf1), "r" (dest), "m" (dstw),
1017                         "m" (yalpha1), "m" (uvalpha1)
1018                         : "%eax"
1019                         );
1020                 }
1021         }
1022 #else
1023 //FIXME write 2 versions (for even & odd lines)
1024         asm volatile ("\n\t"::: "memory");
1025
1026         if(dstbpp==32)
1027         {
1028                 int i;
1029                 for(i=0; i<dstw-1; i+=2){
1030                         // vertical linear interpolation && yuv2rgb in a single step:
1031                         int Y1=yuvtab_2568[buf0[i]>>7];
1032                         int Y2=yuvtab_2568[buf0[i+1]>>7];
1033                         int U=((uvbuf0[i>>1]*uvalpha1+uvbuf1[i>>1]*uvalpha)>>19);
1034                         int V=((uvbuf0[(i>>1)+2048]*uvalpha1+uvbuf1[(i>>1)+2048]*uvalpha)>>19);
1035
1036                         int Cb= yuvtab_40cf[U];
1037                         int Cg= yuvtab_1a1e[V] + yuvtab_0c92[U];
1038                         int Cr= yuvtab_3343[V];
1039
1040                         dest[4*i+0]=clip_table[((Y1 + Cb) >>13)];
1041                         dest[4*i+1]=clip_table[((Y1 + Cg) >>13)];
1042                         dest[4*i+2]=clip_table[((Y1 + Cr) >>13)];
1043
1044                         dest[4*i+4]=clip_table[((Y2 + Cb) >>13)];
1045                         dest[4*i+5]=clip_table[((Y2 + Cg) >>13)];
1046                         dest[4*i+6]=clip_table[((Y2 + Cr) >>13)];
1047                 }
1048         }
1049         if(dstbpp==24)
1050         {
1051                 int i;
1052                 for(i=0; i<dstw-1; i+=2){
1053                         // vertical linear interpolation && yuv2rgb in a single step:
1054                         int Y1=yuvtab_2568[buf0[i]>>7];
1055                         int Y2=yuvtab_2568[buf0[i+1]>>7];
1056                         int U=((uvbuf0[i>>1]*uvalpha1+uvbuf1[i>>1]*uvalpha)>>19);
1057                         int V=((uvbuf0[(i>>1)+2048]*uvalpha1+uvbuf1[(i>>1)+2048]*uvalpha)>>19);
1058
1059                         int Cb= yuvtab_40cf[U];
1060                         int Cg= yuvtab_1a1e[V] + yuvtab_0c92[U];
1061                         int Cr= yuvtab_3343[V];
1062
1063                         dest[0]=clip_table[((Y1 + Cb) >>13)];
1064                         dest[1]=clip_table[((Y1 + Cg) >>13)];
1065                         dest[2]=clip_table[((Y1 + Cr) >>13)];
1066
1067                         dest[3]=clip_table[((Y2 + Cb) >>13)];
1068                         dest[4]=clip_table[((Y2 + Cg) >>13)];
1069                         dest[5]=clip_table[((Y2 + Cr) >>13)];
1070                         dest+=6;
1071                 }
1072         }
1073         else if(dstbpp==16)
1074         {
1075                 int i;
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                 int i;
1101                 for(i=0; i<dstw-1; i+=2){
1102                         // vertical linear interpolation && yuv2rgb in a single step:
1103                         int Y1=yuvtab_2568[buf0[i]>>7];
1104                         int Y2=yuvtab_2568[buf0[i+1]>>7];
1105                         int U=((uvbuf0[i>>1]*uvalpha1+uvbuf1[i>>1]*uvalpha)>>19);
1106                         int V=((uvbuf0[(i>>1)+2048]*uvalpha1+uvbuf1[(i>>1)+2048]*uvalpha)>>19);
1107
1108                         int Cb= yuvtab_40cf[U];
1109                         int Cg= yuvtab_1a1e[V] + yuvtab_0c92[U];
1110                         int Cr= yuvtab_3343[V];
1111
1112                         ((uint16_t*)dest)[i] =
1113                                 clip_table15b[(Y1 + Cb) >>13] |
1114                                 clip_table15g[(Y1 + Cg) >>13] |
1115                                 clip_table15r[(Y1 + Cr) >>13];
1116
1117                         ((uint16_t*)dest)[i+1] =
1118                                 clip_table15b[(Y2 + Cb) >>13] |
1119                                 clip_table15g[(Y2 + Cg) >>13] |
1120                                 clip_table15r[(Y2 + Cr) >>13];
1121                 }
1122         }
1123 #endif
1124 }
1125
1126
1127 static inline void hyscale(uint16_t *dst, int dstWidth, uint8_t *src, int srcWidth, int xInc)
1128 {
1129       // *** horizontal scale Y line to temp buffer
1130 #ifdef ARCH_X86
1131 #ifdef HAVE_MMX2
1132         int i;
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         int i;
1229         unsigned int xpos=0;
1230         for(i=0;i<dstWidth;i++)
1231         {
1232                 register unsigned int xx=xpos>>16;
1233                 register unsigned int xalpha=(xpos&0xFFFF)>>9;
1234                 dst[i]= (src[xx]<<7) + (src[xx+1] - src[xx])*xalpha;
1235                 xpos+=xInc;
1236         }
1237 #endif
1238 }
1239
1240 inline static void hcscale(uint16_t *dst, int dstWidth,
1241                                 uint8_t *src1, uint8_t *src2, int srcWidth, int xInc)
1242 {
1243 #ifdef ARCH_X86
1244 #ifdef HAVE_MMX2
1245         int i;
1246         if(canMMX2BeUsed)
1247         {
1248                 asm volatile(
1249                 "pxor %%mm7, %%mm7              \n\t"
1250                 "pxor %%mm2, %%mm2              \n\t" // 2*xalpha
1251                 "movd %5, %%mm6                 \n\t" // xInc&0xFFFF
1252                 "punpcklwd %%mm6, %%mm6         \n\t"
1253                 "punpcklwd %%mm6, %%mm6         \n\t"
1254                 "movq %%mm6, %%mm2              \n\t"
1255                 "psllq $16, %%mm2               \n\t"
1256                 "paddw %%mm6, %%mm2             \n\t"
1257                 "psllq $16, %%mm2               \n\t"
1258                 "paddw %%mm6, %%mm2             \n\t"
1259                 "psllq $16, %%mm2               \n\t" //0,t,2t,3t               t=xInc&0xFFFF
1260                 "movq %%mm2, temp0              \n\t"
1261                 "movd %4, %%mm6                 \n\t" //(xInc*4)&0xFFFF
1262                 "punpcklwd %%mm6, %%mm6         \n\t"
1263                 "punpcklwd %%mm6, %%mm6         \n\t"
1264                 "xorl %%eax, %%eax              \n\t" // i
1265                 "movl %0, %%esi                 \n\t" // src
1266                 "movl %1, %%edi                 \n\t" // buf1
1267                 "movl %3, %%edx                 \n\t" // (xInc*4)>>16
1268                 "xorl %%ecx, %%ecx              \n\t"
1269                 "xorl %%ebx, %%ebx              \n\t"
1270                 "movw %4, %%bx                  \n\t" // (xInc*4)&0xFFFF
1271
1272 #define FUNNYUVCODE \
1273                         PREFETCH" 1024(%%esi)           \n\t"\
1274                         PREFETCH" 1056(%%esi)           \n\t"\
1275                         PREFETCH" 1088(%%esi)           \n\t"\
1276                         "call funnyUVCode               \n\t"\
1277                         "movq temp0, %%mm2              \n\t"\
1278                         "xorl %%ecx, %%ecx              \n\t"
1279
1280 FUNNYUVCODE
1281 FUNNYUVCODE
1282 FUNNYUVCODE
1283 FUNNYUVCODE
1284
1285 FUNNYUVCODE
1286 FUNNYUVCODE
1287 FUNNYUVCODE
1288 FUNNYUVCODE
1289                 "xorl %%eax, %%eax              \n\t" // i
1290                 "movl %6, %%esi                 \n\t" // src
1291                 "movl %1, %%edi                 \n\t" // buf1
1292                 "addl $4096, %%edi              \n\t"
1293
1294 FUNNYUVCODE
1295 FUNNYUVCODE
1296 FUNNYUVCODE
1297 FUNNYUVCODE
1298
1299 FUNNYUVCODE
1300 FUNNYUVCODE
1301 FUNNYUVCODE
1302 FUNNYUVCODE
1303
1304                 :: "m" (src1), "m" (dst), "m" (dstWidth), "m" ((xInc*4)>>16),
1305                   "m" ((xInc*4)&0xFFFF), "m" (xInc&0xFFFF), "m" (src2)
1306                 : "%eax", "%ebx", "%ecx", "%edx", "%esi", "%edi"
1307         );
1308                 for(i=dstWidth-1; (i*xInc)>>16 >=srcWidth/2-1; i--)
1309                 {
1310                         dst[i] = src1[srcWidth/2-1]*128;
1311                         dst[i+2048] = src2[srcWidth/2-1]*128;
1312                 }
1313         }
1314         else
1315         {
1316 #endif
1317         asm volatile(
1318                 "xorl %%eax, %%eax              \n\t" // i
1319                 "xorl %%ebx, %%ebx              \n\t" // xx
1320                 "xorl %%ecx, %%ecx              \n\t" // 2*xalpha
1321                 "1:                             \n\t"
1322                 "movl %0, %%esi                 \n\t"
1323                 "movzbl  (%%esi, %%ebx), %%edi  \n\t" //src[xx]
1324                 "movzbl 1(%%esi, %%ebx), %%esi  \n\t" //src[xx+1]
1325                 "subl %%edi, %%esi              \n\t" //src[xx+1] - src[xx]
1326                 "imull %%ecx, %%esi             \n\t" //(src[xx+1] - src[xx])*2*xalpha
1327                 "shll $16, %%edi                \n\t"
1328                 "addl %%edi, %%esi              \n\t" //src[xx+1]*2*xalpha + src[xx]*(1-2*xalpha)
1329                 "movl %1, %%edi                 \n\t"
1330                 "shrl $9, %%esi                 \n\t"
1331                 "movw %%si, (%%edi, %%eax, 2)   \n\t"
1332
1333                 "movzbl  (%5, %%ebx), %%edi     \n\t" //src[xx]
1334                 "movzbl 1(%5, %%ebx), %%esi     \n\t" //src[xx+1]
1335                 "subl %%edi, %%esi              \n\t" //src[xx+1] - src[xx]
1336                 "imull %%ecx, %%esi             \n\t" //(src[xx+1] - src[xx])*2*xalpha
1337                 "shll $16, %%edi                \n\t"
1338                 "addl %%edi, %%esi              \n\t" //src[xx+1]*2*xalpha + src[xx]*(1-2*xalpha)
1339                 "movl %1, %%edi                 \n\t"
1340                 "shrl $9, %%esi                 \n\t"
1341                 "movw %%si, 4096(%%edi, %%eax, 2)\n\t"
1342
1343                 "addw %4, %%cx                  \n\t" //2*xalpha += xInc&0xFF
1344                 "adcl %3, %%ebx                 \n\t" //xx+= xInc>>8 + carry
1345                 "addl $1, %%eax                 \n\t"
1346                 "cmpl %2, %%eax                 \n\t"
1347                 " jb 1b                         \n\t"
1348
1349                 :: "m" (src1), "m" (dst), "m" (dstWidth), "m" (xInc>>16), "m" (xInc&0xFFFF),
1350                 "r" (src2)
1351                 : "%eax", "%ebx", "%ecx", "%edi", "%esi"
1352                 );
1353 #ifdef HAVE_MMX2
1354         } //if MMX2 cant be used
1355 #endif
1356 #else
1357         int i;
1358         unsigned int xpos=0;
1359         for(i=0;i<dstWidth;i++)
1360         {
1361                 register unsigned int xx=xpos>>16;
1362                 register unsigned int xalpha=(xpos&0xFFFF)>>9;
1363                 dst[i]=(src1[xx]*(xalpha^127)+src1[xx+1]*xalpha);
1364                 dst[i+2048]=(src2[xx]*(xalpha^127)+src2[xx+1]*xalpha);
1365 /* slower
1366           dst[i]= (src1[xx]<<7) + (src1[xx+1] - src1[xx])*xalpha;
1367           dst[i+2048]=(src2[xx]<<7) + (src2[xx+1] - src2[xx])*xalpha;
1368 */
1369                 xpos+=xInc;
1370         }
1371 #endif
1372 }
1373
1374
1375 // *** bilinear scaling and yuv->rgb or yuv->yuv conversion of yv12 slices:
1376 // *** Note: it's called multiple times while decoding a frame, first time y==0
1377 // *** Designed to upscale, but may work for downscale too.
1378 // s_xinc = (src_width << 16) / dst_width
1379 // s_yinc = (src_height << 16) / dst_height
1380 void SwScale_YV12slice(unsigned char* srcptr[],int stride[], int y, int h,
1381                              uint8_t* dstptr[], int dststride, int dstw, int dstbpp,
1382                              unsigned int s_xinc,unsigned int s_yinc){
1383
1384 // scaling factors:
1385 //static int s_yinc=(vo_dga_src_height<<16)/vo_dga_vp_height;
1386 //static int s_xinc=(vo_dga_src_width<<8)/vo_dga_vp_width;
1387
1388 unsigned int s_xinc2;
1389
1390 static int s_srcypos; // points to the dst Pixels center in the source (0 is the center of pixel 0,0 in src)
1391 static int s_ypos;
1392
1393 // last horzontally interpolated lines, used to avoid unnecessary calculations
1394 static int s_last_ypos;
1395 static int s_last_y1pos;
1396
1397 #ifdef HAVE_MMX2
1398 // used to detect a horizontal size change
1399 static int old_dstw= -1;
1400 static int old_s_xinc= -1;
1401 #endif
1402
1403 int srcWidth;
1404 int dstUVw;
1405 int i;
1406
1407 if(((dstw + 7)&(~7)) >= dststride) dstw&= ~7;
1408
1409 srcWidth= (dstw*s_xinc + 0x8000)>>16;
1410 dstUVw= fullUVIpol ? dstw : dstw/2;
1411
1412 #ifdef HAVE_MMX2
1413 canMMX2BeUsed= (s_xinc <= 0x10000 && (dstw&31)==0 && (srcWidth&15)==0) ? 1 : 0;
1414 #endif
1415
1416 // match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src to pixel n-2 of dst
1417 // n-2 is the last chrominance sample available
1418 // FIXME this is not perfect, but noone shuld notice the difference, the more correct variant
1419 // would be like the vertical one, but that would require some special code for the
1420 // first and last pixel
1421 if(canMMX2BeUsed)       s_xinc+= 20;
1422 else                    s_xinc = ((srcWidth-2)<<16)/(dstw-2) - 20;
1423
1424 if(fullUVIpol && !(dstbpp==12))         s_xinc2= s_xinc>>1;
1425 else                                    s_xinc2= s_xinc;
1426   // force calculation of the horizontal interpolation of the first line
1427
1428   if(y==0){
1429 //      printf("dstw %d, srcw %d, mmx2 %d\n", dstw, srcWidth, canMMX2BeUsed);
1430         s_last_ypos=-99;
1431         s_last_y1pos=-99;
1432         s_srcypos= s_yinc/2 - 0x8000;
1433         s_ypos=0;
1434
1435         // clean the buffers so that no green stuff is drawen if the width is not sane (%8=0)
1436         for(i=dstw-2; i<dstw+20; i++)
1437         {
1438                 pix_buf_uv[0][i] = pix_buf_uv[1][i]
1439                 = pix_buf_uv[0][2048+i] = pix_buf_uv[1][2048+i] = 128*128;
1440                 pix_buf_uv[0][i/2] = pix_buf_uv[1][i/2]
1441                 = pix_buf_uv[0][2048+i/2] = pix_buf_uv[1][2048+i/2] = 128*128;
1442                 pix_buf_y[0][i]= pix_buf_y[1][i]= 0;
1443         }
1444
1445 #ifdef HAVE_MMX2
1446 // cant downscale !!!
1447         if((old_s_xinc != s_xinc || old_dstw!=dstw) && canMMX2BeUsed)
1448         {
1449                 uint8_t *fragment;
1450                 int imm8OfPShufW1;
1451                 int imm8OfPShufW2;
1452                 int fragmentLength;
1453
1454                 int xpos, i;
1455
1456                 old_s_xinc= s_xinc;
1457                 old_dstw= dstw;
1458
1459                 // create an optimized horizontal scaling routine
1460
1461                 //code fragment
1462
1463                 asm volatile(
1464                         "jmp 9f                         \n\t"
1465                 // Begin
1466                         "0:                             \n\t"
1467                         "movq (%%esi), %%mm0            \n\t" //FIXME Alignment
1468                         "movq %%mm0, %%mm1              \n\t"
1469                         "psrlq $8, %%mm0                \n\t"
1470                         "punpcklbw %%mm7, %%mm1 \n\t"
1471                         "movq %%mm2, %%mm3              \n\t"
1472                         "punpcklbw %%mm7, %%mm0 \n\t"
1473                         "addw %%bx, %%cx                \n\t" //2*xalpha += (4*s_xinc)&0xFFFF
1474                         "pshufw $0xFF, %%mm1, %%mm1     \n\t"
1475                         "1:                             \n\t"
1476                         "adcl %%edx, %%esi              \n\t" //xx+= (4*s_xinc)>>16 + carry
1477                         "pshufw $0xFF, %%mm0, %%mm0     \n\t"
1478                         "2:                             \n\t"
1479                         "psrlw $9, %%mm3                \n\t"
1480                         "psubw %%mm1, %%mm0             \n\t"
1481                         "pmullw %%mm3, %%mm0            \n\t"
1482                         "paddw %%mm6, %%mm2             \n\t" // 2*alpha += xpos&0xFFFF
1483                         "psllw $7, %%mm1                \n\t"
1484                         "paddw %%mm1, %%mm0             \n\t"
1485
1486                         "movq %%mm0, (%%edi, %%eax)     \n\t"
1487
1488                         "addl $8, %%eax                 \n\t"
1489                 // End
1490                         "9:                             \n\t"
1491 //              "int $3\n\t"
1492                         "leal 0b, %0                    \n\t"
1493                         "leal 1b, %1                    \n\t"
1494                         "leal 2b, %2                    \n\t"
1495                         "decl %1                        \n\t"
1496                         "decl %2                        \n\t"
1497                         "subl %0, %1                    \n\t"
1498                         "subl %0, %2                    \n\t"
1499                         "leal 9b, %3                    \n\t"
1500                         "subl %0, %3                    \n\t"
1501                         :"=r" (fragment), "=r" (imm8OfPShufW1), "=r" (imm8OfPShufW2),
1502                          "=r" (fragmentLength)
1503                 );
1504
1505                 xpos= 0; //s_xinc/2 - 0x8000; // difference between pixel centers
1506
1507                 /* choose xinc so that all 8 parts fit exactly
1508                    Note: we cannot use just 1 part because it would not fit in the code cache */
1509 //              s_xinc2_diff= -((((s_xinc2*(dstw/8))&0xFFFF))/(dstw/8))-10;
1510 //              s_xinc_diff= -((((s_xinc*(dstw/8))&0xFFFF))/(dstw/8));
1511 #ifdef ALT_ERROR
1512 //              s_xinc2_diff+= ((0x10000/(dstw/8)));
1513 #endif
1514 //              s_xinc_diff= s_xinc2_diff*2;
1515
1516 //              s_xinc2+= s_xinc2_diff;
1517 //              s_xinc+= s_xinc_diff;
1518
1519 //              old_s_xinc= s_xinc;
1520
1521                 for(i=0; i<dstw/8; i++)
1522                 {
1523                         int xx=xpos>>16;
1524
1525                         if((i&3) == 0)
1526                         {
1527                                 int a=0;
1528                                 int b=((xpos+s_xinc)>>16) - xx;
1529                                 int c=((xpos+s_xinc*2)>>16) - xx;
1530                                 int d=((xpos+s_xinc*3)>>16) - xx;
1531
1532                                 memcpy(funnyYCode + fragmentLength*i/4, fragment, fragmentLength);
1533
1534                                 funnyYCode[fragmentLength*i/4 + imm8OfPShufW1]=
1535                                 funnyYCode[fragmentLength*i/4 + imm8OfPShufW2]=
1536                                         a | (b<<2) | (c<<4) | (d<<6);
1537
1538                                 // if we dont need to read 8 bytes than dont :), reduces the chance of
1539                                 // crossing a cache line
1540                                 if(d<3) funnyYCode[fragmentLength*i/4 + 1]= 0x6E;
1541
1542                                 funnyYCode[fragmentLength*(i+4)/4]= RET;
1543                         }
1544                         xpos+=s_xinc;
1545                 }
1546
1547                 xpos= 0; //s_xinc2/2 - 0x10000; // difference between centers of chrom samples
1548                 for(i=0; i<dstUVw/8; i++)
1549                 {
1550                         int xx=xpos>>16;
1551
1552                         if((i&3) == 0)
1553                         {
1554                                 int a=0;
1555                                 int b=((xpos+s_xinc2)>>16) - xx;
1556                                 int c=((xpos+s_xinc2*2)>>16) - xx;
1557                                 int d=((xpos+s_xinc2*3)>>16) - xx;
1558
1559                                 memcpy(funnyUVCode + fragmentLength*i/4, fragment, fragmentLength);
1560
1561                                 funnyUVCode[fragmentLength*i/4 + imm8OfPShufW1]=
1562                                 funnyUVCode[fragmentLength*i/4 + imm8OfPShufW2]=
1563                                         a | (b<<2) | (c<<4) | (d<<6);
1564
1565                                 // if we dont need to read 8 bytes than dont :), reduces the chance of
1566                                 // crossing a cache line
1567                                 if(d<3) funnyUVCode[fragmentLength*i/4 + 1]= 0x6E;
1568
1569                                 funnyUVCode[fragmentLength*(i+4)/4]= RET;
1570                         }
1571                         xpos+=s_xinc2;
1572                 }
1573 //              funnyCode[0]= RET;
1574         }
1575
1576 #endif // HAVE_MMX2
1577   } // reset counters
1578
1579   while(1){
1580     unsigned char *dest =dstptr[0]+dststride*s_ypos;
1581     unsigned char *uDest=dstptr[1]+(dststride>>1)*(s_ypos>>1);
1582     unsigned char *vDest=dstptr[2]+(dststride>>1)*(s_ypos>>1);
1583
1584     int y0=(s_srcypos + 0xFFFF)>>16;  // first luminance source line number below the dst line
1585         // points to the dst Pixels center in the source (0 is the center of pixel 0,0 in src)
1586     int srcuvpos= dstbpp==12 ?  s_srcypos + s_yinc/2 - 0x8000 :
1587                                 s_srcypos - 0x8000;
1588     int y1=(srcuvpos + 0x1FFFF)>>17; // first chrominance source line number below the dst line
1589     int yalpha=((s_srcypos-1)&0xFFFF)>>4;
1590     int uvalpha=((srcuvpos-1)&0x1FFFF)>>5;
1591     uint16_t *buf0=pix_buf_y[y0&1];             // top line of the interpolated slice
1592     uint16_t *buf1=pix_buf_y[((y0+1)&1)];       // bottom line of the interpolated slice
1593     uint16_t *uvbuf0=pix_buf_uv[y1&1];          // top line of the interpolated slice
1594     uint16_t *uvbuf1=pix_buf_uv[(y1+1)&1];      // bottom line of the interpolated slice
1595
1596     if(y0>=y+h) break; // FIXME wrong, skips last lines, but they are dupliactes anyway
1597
1598     if((y0&1) && dstbpp==12) uvalpha=-1; // there is no alpha if there is no line
1599
1600     s_ypos++; s_srcypos+=s_yinc;
1601
1602     //only interpolate the src line horizontally if we didnt do it allready
1603         if(s_last_ypos!=y0)
1604         {
1605                 unsigned char *src;
1606                 // skip if first line has been horiz scaled alleady
1607                 if(s_last_ypos != y0-1)
1608                 {
1609                         // check if first line is before any available src lines
1610                         if(y0-1 < y)    src=srcptr[0]+(0     )*stride[0];
1611                         else            src=srcptr[0]+(y0-y-1)*stride[0];
1612
1613                         hyscale(buf0, dstw, src, srcWidth, s_xinc);
1614                 }
1615                 // check if second line is after any available src lines
1616                 if(y0-y >= h)   src=srcptr[0]+(h-1)*stride[0];
1617                 else            src=srcptr[0]+(y0-y)*stride[0];
1618
1619                 // the min() is required to avoid reuseing lines which where not available
1620                 s_last_ypos= MIN(y0, y+h-1);
1621                 hyscale(buf1, dstw, src, srcWidth, s_xinc);
1622         }
1623 //      printf("%d %d %d %d\n", y, y1, s_last_y1pos, h);
1624       // *** horizontal scale U and V lines to temp buffer
1625         if(s_last_y1pos!=y1)
1626         {
1627                 uint8_t *src1, *src2;
1628                 // skip if first line has been horiz scaled alleady
1629                 if(s_last_y1pos != y1-1)
1630                 {
1631                         // check if first line is before any available src lines
1632                         if(y1-y/2-1 < 0)
1633                         {
1634                                 src1= srcptr[1]+(0)*stride[1];
1635                                 src2= srcptr[2]+(0)*stride[2];
1636                         }else{
1637                                 src1= srcptr[1]+(y1-y/2-1)*stride[1];
1638                                 src2= srcptr[2]+(y1-y/2-1)*stride[2];
1639                         }
1640                         hcscale(uvbuf0, dstUVw, src1, src2, srcWidth, s_xinc2);
1641                 }
1642
1643                 // check if second line is after any available src lines
1644                 if(y1 - y/2 >= h/2)
1645                 {
1646                         src1= srcptr[1]+(h/2-1)*stride[1];
1647                         src2= srcptr[2]+(h/2-1)*stride[2];
1648                 }else{
1649                         src1= srcptr[1]+(y1-y/2)*stride[1];
1650                         src2= srcptr[2]+(y1-y/2)*stride[2];
1651                 }
1652                 hcscale(uvbuf1, dstUVw, src1, src2, srcWidth, s_xinc2);
1653
1654                 // the min() is required to avoid reuseing lines which where not available
1655                 s_last_y1pos= MIN(y1, y/2+h/2-1);
1656         }
1657
1658         if(dstbpp==12) //YV12
1659                 yuv2yuv(buf0, buf1, uvbuf0, uvbuf1, dest, uDest, vDest, dstw, yalpha, uvalpha);
1660         else if(ABS(s_yinc - 0x10000) < 10)
1661                 yuv2rgb1(buf0, buf1, uvbuf0, uvbuf1, dest, dstw, yalpha, uvalpha, dstbpp);
1662         else
1663                 yuv2rgbX(buf0, buf1, uvbuf0, uvbuf1, dest, dstw, yalpha, uvalpha, dstbpp);
1664
1665 #ifdef HAVE_MMX
1666         b16Dither= b16Dither1;
1667         b16Dither1= b16Dither2;
1668         b16Dither2= b16Dither;
1669
1670         g16Dither= g16Dither1;
1671         g16Dither1= g16Dither2;
1672         g16Dither2= g16Dither;
1673 #endif
1674   }
1675
1676 #ifdef HAVE_MMX
1677         __asm __volatile(SFENCE:::"memory");
1678         __asm __volatile(EMMS:::"memory");
1679 #endif
1680 }
1681
1682
1683 void SwScale_Init(){
1684     // generating tables:
1685     int i;
1686     for(i=0;i<256;i++){
1687         clip_table[i]=0;
1688         clip_table[i+256]=i;
1689         clip_table[i+512]=255;
1690         yuvtab_2568[i]=(0x2568*(i-16))+(256<<13);
1691         yuvtab_3343[i]=0x3343*(i-128);
1692         yuvtab_0c92[i]=-0x0c92*(i-128);
1693         yuvtab_1a1e[i]=-0x1a1e*(i-128);
1694         yuvtab_40cf[i]=0x40cf*(i-128);
1695     }
1696
1697     for(i=0; i<768; i++)
1698     {
1699         int v= clip_table[i];
1700         clip_table16b[i]= v>>3;
1701         clip_table16g[i]= (v<<3)&0x07E0;
1702         clip_table16r[i]= (v<<8)&0xF800;
1703         clip_table15b[i]= v>>3;
1704         clip_table15g[i]= (v<<2)&0x03E0;
1705         clip_table15r[i]= (v<<7)&0x7C00;
1706     }
1707
1708 }