swscale: reindent x86 init code.
[ffmpeg.git] / libswscale / x86 / swscale_template.c
1 /*
2  * Copyright (C) 2001-2003 Michael Niedermayer <michaelni@gmx.at>
3  *
4  * This file is part of Libav.
5  *
6  * Libav is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * Libav is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with Libav; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 #undef REAL_MOVNTQ
22 #undef MOVNTQ
23 #undef PREFETCH
24
25 #if COMPILE_TEMPLATE_MMX2
26 #define PREFETCH "prefetchnta"
27 #else
28 #define PREFETCH  " # nop"
29 #endif
30
31 #if COMPILE_TEMPLATE_MMX2
32 #define REAL_MOVNTQ(a,b) "movntq " #a ", " #b " \n\t"
33 #else
34 #define REAL_MOVNTQ(a,b) "movq " #a ", " #b " \n\t"
35 #endif
36 #define MOVNTQ(a,b)  REAL_MOVNTQ(a,b)
37
38 #define YSCALEYUV2YV12X(offset, dest, end, pos) \
39     __asm__ volatile(\
40         "movq             "VROUNDER_OFFSET"(%0), %%mm3      \n\t"\
41         "movq                             %%mm3, %%mm4      \n\t"\
42         "lea                     " offset "(%0), %%"REG_d"  \n\t"\
43         "mov                        (%%"REG_d"), %%"REG_S"  \n\t"\
44         ".p2align                             4             \n\t" /* FIXME Unroll? */\
45         "1:                                                 \n\t"\
46         "movq                      8(%%"REG_d"), %%mm0      \n\t" /* filterCoeff */\
47         "movq                (%%"REG_S", %3, 2), %%mm2      \n\t" /* srcData */\
48         "movq               8(%%"REG_S", %3, 2), %%mm5      \n\t" /* srcData */\
49         "add                                $16, %%"REG_d"  \n\t"\
50         "mov                        (%%"REG_d"), %%"REG_S"  \n\t"\
51         "test                         %%"REG_S", %%"REG_S"  \n\t"\
52         "pmulhw                           %%mm0, %%mm2      \n\t"\
53         "pmulhw                           %%mm0, %%mm5      \n\t"\
54         "paddw                            %%mm2, %%mm3      \n\t"\
55         "paddw                            %%mm5, %%mm4      \n\t"\
56         " jnz                                1b             \n\t"\
57         "psraw                               $3, %%mm3      \n\t"\
58         "psraw                               $3, %%mm4      \n\t"\
59         "packuswb                         %%mm4, %%mm3      \n\t"\
60         MOVNTQ(%%mm3, (%1, %3))\
61         "add                                 $8, %3         \n\t"\
62         "cmp                                 %2, %3         \n\t"\
63         "movq             "VROUNDER_OFFSET"(%0), %%mm3      \n\t"\
64         "movq                             %%mm3, %%mm4      \n\t"\
65         "lea                     " offset "(%0), %%"REG_d"  \n\t"\
66         "mov                        (%%"REG_d"), %%"REG_S"  \n\t"\
67         "jb                                  1b             \n\t"\
68         :: "r" (&c->redDither),\
69            "r" (dest), "g" ((x86_reg)(end)), "r"((x86_reg)(pos))\
70         : "%"REG_d, "%"REG_S\
71     );
72
73 static inline void RENAME(yuv2yuvX)(SwsContext *c, const int16_t *lumFilter,
74                                     const int16_t **lumSrc, int lumFilterSize,
75                                     const int16_t *chrFilter, const int16_t **chrUSrc,
76                                     const int16_t **chrVSrc,
77                                     int chrFilterSize, const int16_t **alpSrc,
78                                     uint8_t *dest, uint8_t *uDest, uint8_t *vDest,
79                                     uint8_t *aDest, int dstW, int chrDstW)
80 {
81     if (uDest) {
82         x86_reg uv_off = c->uv_off;
83         YSCALEYUV2YV12X(CHR_MMX_FILTER_OFFSET, uDest, chrDstW, 0)
84         YSCALEYUV2YV12X(CHR_MMX_FILTER_OFFSET, vDest - uv_off, chrDstW + uv_off, uv_off)
85     }
86     if (CONFIG_SWSCALE_ALPHA && aDest) {
87         YSCALEYUV2YV12X(ALP_MMX_FILTER_OFFSET, aDest, dstW, 0)
88     }
89
90     YSCALEYUV2YV12X(LUM_MMX_FILTER_OFFSET, dest, dstW, 0)
91 }
92
93 #define YSCALEYUV2YV12X_ACCURATE(offset, dest, end, pos) \
94     __asm__ volatile(\
95         "lea                     " offset "(%0), %%"REG_d"  \n\t"\
96         "pxor                             %%mm4, %%mm4      \n\t"\
97         "pxor                             %%mm5, %%mm5      \n\t"\
98         "pxor                             %%mm6, %%mm6      \n\t"\
99         "pxor                             %%mm7, %%mm7      \n\t"\
100         "mov                        (%%"REG_d"), %%"REG_S"  \n\t"\
101         ".p2align                             4             \n\t"\
102         "1:                                                 \n\t"\
103         "movq                (%%"REG_S", %3, 2), %%mm0      \n\t" /* srcData */\
104         "movq               8(%%"REG_S", %3, 2), %%mm2      \n\t" /* srcData */\
105         "mov        "STR(APCK_PTR2)"(%%"REG_d"), %%"REG_S"  \n\t"\
106         "movq                (%%"REG_S", %3, 2), %%mm1      \n\t" /* srcData */\
107         "movq                             %%mm0, %%mm3      \n\t"\
108         "punpcklwd                        %%mm1, %%mm0      \n\t"\
109         "punpckhwd                        %%mm1, %%mm3      \n\t"\
110         "movq       "STR(APCK_COEF)"(%%"REG_d"), %%mm1      \n\t" /* filterCoeff */\
111         "pmaddwd                          %%mm1, %%mm0      \n\t"\
112         "pmaddwd                          %%mm1, %%mm3      \n\t"\
113         "paddd                            %%mm0, %%mm4      \n\t"\
114         "paddd                            %%mm3, %%mm5      \n\t"\
115         "movq               8(%%"REG_S", %3, 2), %%mm3      \n\t" /* srcData */\
116         "mov        "STR(APCK_SIZE)"(%%"REG_d"), %%"REG_S"  \n\t"\
117         "add                  $"STR(APCK_SIZE)", %%"REG_d"  \n\t"\
118         "test                         %%"REG_S", %%"REG_S"  \n\t"\
119         "movq                             %%mm2, %%mm0      \n\t"\
120         "punpcklwd                        %%mm3, %%mm2      \n\t"\
121         "punpckhwd                        %%mm3, %%mm0      \n\t"\
122         "pmaddwd                          %%mm1, %%mm2      \n\t"\
123         "pmaddwd                          %%mm1, %%mm0      \n\t"\
124         "paddd                            %%mm2, %%mm6      \n\t"\
125         "paddd                            %%mm0, %%mm7      \n\t"\
126         " jnz                                1b             \n\t"\
127         "psrad                              $16, %%mm4      \n\t"\
128         "psrad                              $16, %%mm5      \n\t"\
129         "psrad                              $16, %%mm6      \n\t"\
130         "psrad                              $16, %%mm7      \n\t"\
131         "movq             "VROUNDER_OFFSET"(%0), %%mm0      \n\t"\
132         "packssdw                         %%mm5, %%mm4      \n\t"\
133         "packssdw                         %%mm7, %%mm6      \n\t"\
134         "paddw                            %%mm0, %%mm4      \n\t"\
135         "paddw                            %%mm0, %%mm6      \n\t"\
136         "psraw                               $3, %%mm4      \n\t"\
137         "psraw                               $3, %%mm6      \n\t"\
138         "packuswb                         %%mm6, %%mm4      \n\t"\
139         MOVNTQ(%%mm4, (%1, %3))\
140         "add                                 $8, %3         \n\t"\
141         "cmp                                 %2, %3         \n\t"\
142         "lea                     " offset "(%0), %%"REG_d"  \n\t"\
143         "pxor                             %%mm4, %%mm4      \n\t"\
144         "pxor                             %%mm5, %%mm5      \n\t"\
145         "pxor                             %%mm6, %%mm6      \n\t"\
146         "pxor                             %%mm7, %%mm7      \n\t"\
147         "mov                        (%%"REG_d"), %%"REG_S"  \n\t"\
148         "jb                                  1b             \n\t"\
149         :: "r" (&c->redDither),\
150         "r" (dest), "g" ((x86_reg)(end)), "r"((x86_reg)(pos))\
151         : "%"REG_a, "%"REG_d, "%"REG_S\
152     );
153
154 static inline void RENAME(yuv2yuvX_ar)(SwsContext *c, const int16_t *lumFilter,
155                                        const int16_t **lumSrc, int lumFilterSize,
156                                        const int16_t *chrFilter, const int16_t **chrUSrc,
157                                        const int16_t **chrVSrc,
158                                        int chrFilterSize, const int16_t **alpSrc,
159                                        uint8_t *dest, uint8_t *uDest, uint8_t *vDest,
160                                        uint8_t *aDest, int dstW, int chrDstW)
161 {
162     if (uDest) {
163         x86_reg uv_off = c->uv_off;
164         YSCALEYUV2YV12X_ACCURATE(CHR_MMX_FILTER_OFFSET, uDest, chrDstW, 0)
165         YSCALEYUV2YV12X_ACCURATE(CHR_MMX_FILTER_OFFSET, vDest - uv_off, chrDstW + uv_off, uv_off)
166     }
167     if (CONFIG_SWSCALE_ALPHA && aDest) {
168         YSCALEYUV2YV12X_ACCURATE(ALP_MMX_FILTER_OFFSET, aDest, dstW, 0)
169     }
170
171     YSCALEYUV2YV12X_ACCURATE(LUM_MMX_FILTER_OFFSET, dest, dstW, 0)
172 }
173
174 #define YSCALEYUV2YV121 \
175     "mov %2, %%"REG_a"                    \n\t"\
176     ".p2align               4             \n\t" /* FIXME Unroll? */\
177     "1:                                   \n\t"\
178     "movq  (%0, %%"REG_a", 2), %%mm0      \n\t"\
179     "movq 8(%0, %%"REG_a", 2), %%mm1      \n\t"\
180     "psraw                 $7, %%mm0      \n\t"\
181     "psraw                 $7, %%mm1      \n\t"\
182     "packuswb           %%mm1, %%mm0      \n\t"\
183     MOVNTQ(%%mm0, (%1, %%REGa))\
184     "add                   $8, %%"REG_a"  \n\t"\
185     "jnc                   1b             \n\t"
186
187 static inline void RENAME(yuv2yuv1)(SwsContext *c, const int16_t *lumSrc,
188                                     const int16_t *chrUSrc, const int16_t *chrVSrc,
189                                     const int16_t *alpSrc,
190                                     uint8_t *dest, uint8_t *uDest, uint8_t *vDest,
191                                     uint8_t *aDest, int dstW, int chrDstW)
192 {
193     int p= 4;
194     const uint8_t *src[4]= { alpSrc + dstW, lumSrc + dstW, chrUSrc + chrDstW, chrVSrc + chrDstW };
195     uint8_t *dst[4]= { aDest, dest, uDest, vDest };
196     x86_reg counter[4]= { dstW, dstW, chrDstW, chrDstW };
197
198     while (p--) {
199         if (dst[p]) {
200             __asm__ volatile(
201                YSCALEYUV2YV121
202                :: "r" (src[p]), "r" (dst[p] + counter[p]),
203                   "g" (-counter[p])
204                : "%"REG_a
205             );
206         }
207     }
208 }
209
210 #define YSCALEYUV2YV121_ACCURATE \
211     "mov %2, %%"REG_a"                    \n\t"\
212     "pcmpeqw %%mm7, %%mm7                 \n\t"\
213     "psrlw                 $15, %%mm7     \n\t"\
214     "psllw                  $6, %%mm7     \n\t"\
215     ".p2align                4            \n\t" /* FIXME Unroll? */\
216     "1:                                   \n\t"\
217     "movq  (%0, %%"REG_a", 2), %%mm0      \n\t"\
218     "movq 8(%0, %%"REG_a", 2), %%mm1      \n\t"\
219     "paddsw             %%mm7, %%mm0      \n\t"\
220     "paddsw             %%mm7, %%mm1      \n\t"\
221     "psraw                 $7, %%mm0      \n\t"\
222     "psraw                 $7, %%mm1      \n\t"\
223     "packuswb           %%mm1, %%mm0      \n\t"\
224     MOVNTQ(%%mm0, (%1, %%REGa))\
225     "add                   $8, %%"REG_a"  \n\t"\
226     "jnc                   1b             \n\t"
227
228 static inline void RENAME(yuv2yuv1_ar)(SwsContext *c, const int16_t *lumSrc,
229                                        const int16_t *chrUSrc, const int16_t *chrVSrc,
230                                        const int16_t *alpSrc,
231                                        uint8_t *dest, uint8_t *uDest, uint8_t *vDest,
232                                        uint8_t *aDest, int dstW, int chrDstW)
233 {
234     int p= 4;
235     const uint8_t *src[4]= { alpSrc + dstW, lumSrc + dstW, chrUSrc + chrDstW, chrVSrc + chrDstW };
236     uint8_t *dst[4]= { aDest, dest, uDest, vDest };
237     x86_reg counter[4]= { dstW, dstW, chrDstW, chrDstW };
238
239     while (p--) {
240         if (dst[p]) {
241             __asm__ volatile(
242                 YSCALEYUV2YV121_ACCURATE
243                 :: "r" (src[p]), "r" (dst[p] + counter[p]),
244                    "g" (-counter[p])
245                 : "%"REG_a
246             );
247         }
248     }
249 }
250
251 #define YSCALEYUV2PACKEDX_UV \
252     __asm__ volatile(\
253         "xor                   %%"REG_a", %%"REG_a"     \n\t"\
254         ".p2align                      4                \n\t"\
255         "nop                                            \n\t"\
256         "1:                                             \n\t"\
257         "lea "CHR_MMX_FILTER_OFFSET"(%0), %%"REG_d"     \n\t"\
258         "mov                 (%%"REG_d"), %%"REG_S"     \n\t"\
259         "movq      "VROUNDER_OFFSET"(%0), %%mm3         \n\t"\
260         "movq                      %%mm3, %%mm4         \n\t"\
261         ".p2align                      4                \n\t"\
262         "2:                                             \n\t"\
263         "movq               8(%%"REG_d"), %%mm0         \n\t" /* filterCoeff */\
264         "movq     (%%"REG_S", %%"REG_a"), %%mm2         \n\t" /* UsrcData */\
265         "add                          %6, %%"REG_S"     \n\t" \
266         "movq     (%%"REG_S", %%"REG_a"), %%mm5         \n\t" /* VsrcData */\
267         "add                         $16, %%"REG_d"     \n\t"\
268         "mov                 (%%"REG_d"), %%"REG_S"     \n\t"\
269         "pmulhw                    %%mm0, %%mm2         \n\t"\
270         "pmulhw                    %%mm0, %%mm5         \n\t"\
271         "paddw                     %%mm2, %%mm3         \n\t"\
272         "paddw                     %%mm5, %%mm4         \n\t"\
273         "test                  %%"REG_S", %%"REG_S"     \n\t"\
274         " jnz                         2b                \n\t"\
275
276 #define YSCALEYUV2PACKEDX_YA(offset,coeff,src1,src2,dst1,dst2) \
277     "lea                "offset"(%0), %%"REG_d"     \n\t"\
278     "mov                 (%%"REG_d"), %%"REG_S"     \n\t"\
279     "movq      "VROUNDER_OFFSET"(%0), "#dst1"       \n\t"\
280     "movq                    "#dst1", "#dst2"       \n\t"\
281     ".p2align                      4                \n\t"\
282     "2:                                             \n\t"\
283     "movq               8(%%"REG_d"), "#coeff"      \n\t" /* filterCoeff */\
284     "movq  (%%"REG_S", %%"REG_a", 2), "#src1"       \n\t" /* Y1srcData */\
285     "movq 8(%%"REG_S", %%"REG_a", 2), "#src2"       \n\t" /* Y2srcData */\
286     "add                         $16, %%"REG_d"            \n\t"\
287     "mov                 (%%"REG_d"), %%"REG_S"     \n\t"\
288     "pmulhw                 "#coeff", "#src1"       \n\t"\
289     "pmulhw                 "#coeff", "#src2"       \n\t"\
290     "paddw                   "#src1", "#dst1"       \n\t"\
291     "paddw                   "#src2", "#dst2"       \n\t"\
292     "test                  %%"REG_S", %%"REG_S"     \n\t"\
293     " jnz                         2b                \n\t"\
294
295 #define YSCALEYUV2PACKEDX \
296     YSCALEYUV2PACKEDX_UV \
297     YSCALEYUV2PACKEDX_YA(LUM_MMX_FILTER_OFFSET,%%mm0,%%mm2,%%mm5,%%mm1,%%mm7) \
298
299 #define YSCALEYUV2PACKEDX_END                     \
300         :: "r" (&c->redDither),                   \
301             "m" (dummy), "m" (dummy), "m" (dummy),\
302             "r" (dest), "m" (dstW_reg), "m"(uv_off) \
303         : "%"REG_a, "%"REG_d, "%"REG_S            \
304     );
305
306 #define YSCALEYUV2PACKEDX_ACCURATE_UV \
307     __asm__ volatile(\
308         "xor %%"REG_a", %%"REG_a"                       \n\t"\
309         ".p2align                      4                \n\t"\
310         "nop                                            \n\t"\
311         "1:                                             \n\t"\
312         "lea "CHR_MMX_FILTER_OFFSET"(%0), %%"REG_d"     \n\t"\
313         "mov                 (%%"REG_d"), %%"REG_S"     \n\t"\
314         "pxor                      %%mm4, %%mm4         \n\t"\
315         "pxor                      %%mm5, %%mm5         \n\t"\
316         "pxor                      %%mm6, %%mm6         \n\t"\
317         "pxor                      %%mm7, %%mm7         \n\t"\
318         ".p2align                      4                \n\t"\
319         "2:                                             \n\t"\
320         "movq     (%%"REG_S", %%"REG_a"), %%mm0         \n\t" /* UsrcData */\
321         "add                          %6, %%"REG_S"      \n\t" \
322         "movq     (%%"REG_S", %%"REG_a"), %%mm2         \n\t" /* VsrcData */\
323         "mov "STR(APCK_PTR2)"(%%"REG_d"), %%"REG_S"     \n\t"\
324         "movq     (%%"REG_S", %%"REG_a"), %%mm1         \n\t" /* UsrcData */\
325         "movq                      %%mm0, %%mm3         \n\t"\
326         "punpcklwd                 %%mm1, %%mm0         \n\t"\
327         "punpckhwd                 %%mm1, %%mm3         \n\t"\
328         "movq "STR(APCK_COEF)"(%%"REG_d"),%%mm1         \n\t" /* filterCoeff */\
329         "pmaddwd                   %%mm1, %%mm0         \n\t"\
330         "pmaddwd                   %%mm1, %%mm3         \n\t"\
331         "paddd                     %%mm0, %%mm4         \n\t"\
332         "paddd                     %%mm3, %%mm5         \n\t"\
333         "add                          %6, %%"REG_S"      \n\t" \
334         "movq     (%%"REG_S", %%"REG_a"), %%mm3         \n\t" /* VsrcData */\
335         "mov "STR(APCK_SIZE)"(%%"REG_d"), %%"REG_S"     \n\t"\
336         "add           $"STR(APCK_SIZE)", %%"REG_d"     \n\t"\
337         "test                  %%"REG_S", %%"REG_S"     \n\t"\
338         "movq                      %%mm2, %%mm0         \n\t"\
339         "punpcklwd                 %%mm3, %%mm2         \n\t"\
340         "punpckhwd                 %%mm3, %%mm0         \n\t"\
341         "pmaddwd                   %%mm1, %%mm2         \n\t"\
342         "pmaddwd                   %%mm1, %%mm0         \n\t"\
343         "paddd                     %%mm2, %%mm6         \n\t"\
344         "paddd                     %%mm0, %%mm7         \n\t"\
345         " jnz                         2b                \n\t"\
346         "psrad                       $16, %%mm4         \n\t"\
347         "psrad                       $16, %%mm5         \n\t"\
348         "psrad                       $16, %%mm6         \n\t"\
349         "psrad                       $16, %%mm7         \n\t"\
350         "movq      "VROUNDER_OFFSET"(%0), %%mm0         \n\t"\
351         "packssdw                  %%mm5, %%mm4         \n\t"\
352         "packssdw                  %%mm7, %%mm6         \n\t"\
353         "paddw                     %%mm0, %%mm4         \n\t"\
354         "paddw                     %%mm0, %%mm6         \n\t"\
355         "movq                      %%mm4, "U_TEMP"(%0)  \n\t"\
356         "movq                      %%mm6, "V_TEMP"(%0)  \n\t"\
357
358 #define YSCALEYUV2PACKEDX_ACCURATE_YA(offset) \
359     "lea                "offset"(%0), %%"REG_d"     \n\t"\
360     "mov                 (%%"REG_d"), %%"REG_S"     \n\t"\
361     "pxor                      %%mm1, %%mm1         \n\t"\
362     "pxor                      %%mm5, %%mm5         \n\t"\
363     "pxor                      %%mm7, %%mm7         \n\t"\
364     "pxor                      %%mm6, %%mm6         \n\t"\
365     ".p2align                      4                \n\t"\
366     "2:                                             \n\t"\
367     "movq  (%%"REG_S", %%"REG_a", 2), %%mm0         \n\t" /* Y1srcData */\
368     "movq 8(%%"REG_S", %%"REG_a", 2), %%mm2         \n\t" /* Y2srcData */\
369     "mov "STR(APCK_PTR2)"(%%"REG_d"), %%"REG_S"     \n\t"\
370     "movq  (%%"REG_S", %%"REG_a", 2), %%mm4         \n\t" /* Y1srcData */\
371     "movq                      %%mm0, %%mm3         \n\t"\
372     "punpcklwd                 %%mm4, %%mm0         \n\t"\
373     "punpckhwd                 %%mm4, %%mm3         \n\t"\
374     "movq "STR(APCK_COEF)"(%%"REG_d"), %%mm4         \n\t" /* filterCoeff */\
375     "pmaddwd                   %%mm4, %%mm0         \n\t"\
376     "pmaddwd                   %%mm4, %%mm3         \n\t"\
377     "paddd                     %%mm0, %%mm1         \n\t"\
378     "paddd                     %%mm3, %%mm5         \n\t"\
379     "movq 8(%%"REG_S", %%"REG_a", 2), %%mm3         \n\t" /* Y2srcData */\
380     "mov "STR(APCK_SIZE)"(%%"REG_d"), %%"REG_S"     \n\t"\
381     "add           $"STR(APCK_SIZE)", %%"REG_d"     \n\t"\
382     "test                  %%"REG_S", %%"REG_S"     \n\t"\
383     "movq                      %%mm2, %%mm0         \n\t"\
384     "punpcklwd                 %%mm3, %%mm2         \n\t"\
385     "punpckhwd                 %%mm3, %%mm0         \n\t"\
386     "pmaddwd                   %%mm4, %%mm2         \n\t"\
387     "pmaddwd                   %%mm4, %%mm0         \n\t"\
388     "paddd                     %%mm2, %%mm7         \n\t"\
389     "paddd                     %%mm0, %%mm6         \n\t"\
390     " jnz                         2b                \n\t"\
391     "psrad                       $16, %%mm1         \n\t"\
392     "psrad                       $16, %%mm5         \n\t"\
393     "psrad                       $16, %%mm7         \n\t"\
394     "psrad                       $16, %%mm6         \n\t"\
395     "movq      "VROUNDER_OFFSET"(%0), %%mm0         \n\t"\
396     "packssdw                  %%mm5, %%mm1         \n\t"\
397     "packssdw                  %%mm6, %%mm7         \n\t"\
398     "paddw                     %%mm0, %%mm1         \n\t"\
399     "paddw                     %%mm0, %%mm7         \n\t"\
400     "movq               "U_TEMP"(%0), %%mm3         \n\t"\
401     "movq               "V_TEMP"(%0), %%mm4         \n\t"\
402
403 #define YSCALEYUV2PACKEDX_ACCURATE \
404     YSCALEYUV2PACKEDX_ACCURATE_UV \
405     YSCALEYUV2PACKEDX_ACCURATE_YA(LUM_MMX_FILTER_OFFSET)
406
407 #define YSCALEYUV2RGBX \
408     "psubw  "U_OFFSET"(%0), %%mm3       \n\t" /* (U-128)8*/\
409     "psubw  "V_OFFSET"(%0), %%mm4       \n\t" /* (V-128)8*/\
410     "movq            %%mm3, %%mm2       \n\t" /* (U-128)8*/\
411     "movq            %%mm4, %%mm5       \n\t" /* (V-128)8*/\
412     "pmulhw "UG_COEFF"(%0), %%mm3       \n\t"\
413     "pmulhw "VG_COEFF"(%0), %%mm4       \n\t"\
414     /* mm2=(U-128)8, mm3=ug, mm4=vg mm5=(V-128)8 */\
415     "pmulhw "UB_COEFF"(%0), %%mm2       \n\t"\
416     "pmulhw "VR_COEFF"(%0), %%mm5       \n\t"\
417     "psubw  "Y_OFFSET"(%0), %%mm1       \n\t" /* 8(Y-16)*/\
418     "psubw  "Y_OFFSET"(%0), %%mm7       \n\t" /* 8(Y-16)*/\
419     "pmulhw  "Y_COEFF"(%0), %%mm1       \n\t"\
420     "pmulhw  "Y_COEFF"(%0), %%mm7       \n\t"\
421     /* mm1= Y1, mm2=ub, mm3=ug, mm4=vg mm5=vr, mm7=Y2 */\
422     "paddw           %%mm3, %%mm4       \n\t"\
423     "movq            %%mm2, %%mm0       \n\t"\
424     "movq            %%mm5, %%mm6       \n\t"\
425     "movq            %%mm4, %%mm3       \n\t"\
426     "punpcklwd       %%mm2, %%mm2       \n\t"\
427     "punpcklwd       %%mm5, %%mm5       \n\t"\
428     "punpcklwd       %%mm4, %%mm4       \n\t"\
429     "paddw           %%mm1, %%mm2       \n\t"\
430     "paddw           %%mm1, %%mm5       \n\t"\
431     "paddw           %%mm1, %%mm4       \n\t"\
432     "punpckhwd       %%mm0, %%mm0       \n\t"\
433     "punpckhwd       %%mm6, %%mm6       \n\t"\
434     "punpckhwd       %%mm3, %%mm3       \n\t"\
435     "paddw           %%mm7, %%mm0       \n\t"\
436     "paddw           %%mm7, %%mm6       \n\t"\
437     "paddw           %%mm7, %%mm3       \n\t"\
438     /* mm0=B1, mm2=B2, mm3=G2, mm4=G1, mm5=R1, mm6=R2 */\
439     "packuswb        %%mm0, %%mm2       \n\t"\
440     "packuswb        %%mm6, %%mm5       \n\t"\
441     "packuswb        %%mm3, %%mm4       \n\t"\
442
443 #define REAL_WRITEBGR32(dst, dstw, index, b, g, r, a, q0, q2, q3, t) \
444     "movq       "#b", "#q2"     \n\t" /* B */\
445     "movq       "#r", "#t"      \n\t" /* R */\
446     "punpcklbw  "#g", "#b"      \n\t" /* GBGBGBGB 0 */\
447     "punpcklbw  "#a", "#r"      \n\t" /* ARARARAR 0 */\
448     "punpckhbw  "#g", "#q2"     \n\t" /* GBGBGBGB 2 */\
449     "punpckhbw  "#a", "#t"      \n\t" /* ARARARAR 2 */\
450     "movq       "#b", "#q0"     \n\t" /* GBGBGBGB 0 */\
451     "movq      "#q2", "#q3"     \n\t" /* GBGBGBGB 2 */\
452     "punpcklwd  "#r", "#q0"     \n\t" /* ARGBARGB 0 */\
453     "punpckhwd  "#r", "#b"      \n\t" /* ARGBARGB 1 */\
454     "punpcklwd  "#t", "#q2"     \n\t" /* ARGBARGB 2 */\
455     "punpckhwd  "#t", "#q3"     \n\t" /* ARGBARGB 3 */\
456 \
457     MOVNTQ(   q0,   (dst, index, 4))\
458     MOVNTQ(    b,  8(dst, index, 4))\
459     MOVNTQ(   q2, 16(dst, index, 4))\
460     MOVNTQ(   q3, 24(dst, index, 4))\
461 \
462     "add      $8, "#index"      \n\t"\
463     "cmp "#dstw", "#index"      \n\t"\
464     " jb      1b                \n\t"
465 #define WRITEBGR32(dst, dstw, index, b, g, r, a, q0, q2, q3, t)  REAL_WRITEBGR32(dst, dstw, index, b, g, r, a, q0, q2, q3, t)
466
467 static inline void RENAME(yuv2rgb32_X_ar)(SwsContext *c, const int16_t *lumFilter,
468                                           const int16_t **lumSrc, int lumFilterSize,
469                                           const int16_t *chrFilter, const int16_t **chrUSrc,
470                                           const int16_t **chrVSrc,
471                                           int chrFilterSize, const int16_t **alpSrc,
472                                           uint8_t *dest, int dstW, int dstY)
473 {
474     x86_reg dummy=0;
475     x86_reg dstW_reg = dstW;
476     x86_reg uv_off = c->uv_off << 1;
477
478     if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
479         YSCALEYUV2PACKEDX_ACCURATE
480         YSCALEYUV2RGBX
481         "movq                      %%mm2, "U_TEMP"(%0)  \n\t"
482         "movq                      %%mm4, "V_TEMP"(%0)  \n\t"
483         "movq                      %%mm5, "Y_TEMP"(%0)  \n\t"
484         YSCALEYUV2PACKEDX_ACCURATE_YA(ALP_MMX_FILTER_OFFSET)
485         "movq               "Y_TEMP"(%0), %%mm5         \n\t"
486         "psraw                        $3, %%mm1         \n\t"
487         "psraw                        $3, %%mm7         \n\t"
488         "packuswb                  %%mm7, %%mm1         \n\t"
489         WRITEBGR32(%4, %5, %%REGa, %%mm3, %%mm4, %%mm5, %%mm1, %%mm0, %%mm7, %%mm2, %%mm6)
490         YSCALEYUV2PACKEDX_END
491     } else {
492         YSCALEYUV2PACKEDX_ACCURATE
493         YSCALEYUV2RGBX
494         "pcmpeqd %%mm7, %%mm7 \n\t"
495         WRITEBGR32(%4, %5, %%REGa, %%mm2, %%mm4, %%mm5, %%mm7, %%mm0, %%mm1, %%mm3, %%mm6)
496         YSCALEYUV2PACKEDX_END
497     }
498 }
499
500 static inline void RENAME(yuv2rgb32_X)(SwsContext *c, const int16_t *lumFilter,
501                                        const int16_t **lumSrc, int lumFilterSize,
502                                        const int16_t *chrFilter, const int16_t **chrUSrc,
503                                        const int16_t **chrVSrc,
504                                        int chrFilterSize, const int16_t **alpSrc,
505                                        uint8_t *dest, int dstW, int dstY)
506 {
507     x86_reg dummy=0;
508     x86_reg dstW_reg = dstW;
509     x86_reg uv_off = c->uv_off << 1;
510
511     if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
512         YSCALEYUV2PACKEDX
513         YSCALEYUV2RGBX
514         YSCALEYUV2PACKEDX_YA(ALP_MMX_FILTER_OFFSET, %%mm0, %%mm3, %%mm6, %%mm1, %%mm7)
515         "psraw                        $3, %%mm1         \n\t"
516         "psraw                        $3, %%mm7         \n\t"
517         "packuswb                  %%mm7, %%mm1         \n\t"
518         WRITEBGR32(%4, %5, %%REGa, %%mm2, %%mm4, %%mm5, %%mm1, %%mm0, %%mm7, %%mm3, %%mm6)
519         YSCALEYUV2PACKEDX_END
520     } else {
521         YSCALEYUV2PACKEDX
522         YSCALEYUV2RGBX
523         "pcmpeqd %%mm7, %%mm7 \n\t"
524         WRITEBGR32(%4, %5, %%REGa, %%mm2, %%mm4, %%mm5, %%mm7, %%mm0, %%mm1, %%mm3, %%mm6)
525         YSCALEYUV2PACKEDX_END
526     }
527 }
528
529 #define REAL_WRITERGB16(dst, dstw, index) \
530     "pand "MANGLE(bF8)", %%mm2  \n\t" /* B */\
531     "pand "MANGLE(bFC)", %%mm4  \n\t" /* G */\
532     "pand "MANGLE(bF8)", %%mm5  \n\t" /* R */\
533     "psrlq           $3, %%mm2  \n\t"\
534 \
535     "movq         %%mm2, %%mm1  \n\t"\
536     "movq         %%mm4, %%mm3  \n\t"\
537 \
538     "punpcklbw    %%mm7, %%mm3  \n\t"\
539     "punpcklbw    %%mm5, %%mm2  \n\t"\
540     "punpckhbw    %%mm7, %%mm4  \n\t"\
541     "punpckhbw    %%mm5, %%mm1  \n\t"\
542 \
543     "psllq           $3, %%mm3  \n\t"\
544     "psllq           $3, %%mm4  \n\t"\
545 \
546     "por          %%mm3, %%mm2  \n\t"\
547     "por          %%mm4, %%mm1  \n\t"\
548 \
549     MOVNTQ(%%mm2,  (dst, index, 2))\
550     MOVNTQ(%%mm1, 8(dst, index, 2))\
551 \
552     "add             $8, "#index"   \n\t"\
553     "cmp        "#dstw", "#index"   \n\t"\
554     " jb             1b             \n\t"
555 #define WRITERGB16(dst, dstw, index)  REAL_WRITERGB16(dst, dstw, index)
556
557 static inline void RENAME(yuv2rgb565_X_ar)(SwsContext *c, const int16_t *lumFilter,
558                                            const int16_t **lumSrc, int lumFilterSize,
559                                            const int16_t *chrFilter, const int16_t **chrUSrc,
560                                            const int16_t **chrVSrc,
561                                            int chrFilterSize, const int16_t **alpSrc,
562                                            uint8_t *dest, int dstW, int dstY)
563 {
564     x86_reg dummy=0;
565     x86_reg dstW_reg = dstW;
566     x86_reg uv_off = c->uv_off << 1;
567
568     YSCALEYUV2PACKEDX_ACCURATE
569     YSCALEYUV2RGBX
570     "pxor %%mm7, %%mm7 \n\t"
571     /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */
572 #ifdef DITHER1XBPP
573     "paddusb "BLUE_DITHER"(%0), %%mm2\n\t"
574     "paddusb "GREEN_DITHER"(%0), %%mm4\n\t"
575     "paddusb "RED_DITHER"(%0), %%mm5\n\t"
576 #endif
577     WRITERGB16(%4, %5, %%REGa)
578     YSCALEYUV2PACKEDX_END
579 }
580
581 static inline void RENAME(yuv2rgb565_X)(SwsContext *c, const int16_t *lumFilter,
582                                         const int16_t **lumSrc, int lumFilterSize,
583                                         const int16_t *chrFilter, const int16_t **chrUSrc,
584                                         const int16_t **chrVSrc,
585                                         int chrFilterSize, const int16_t **alpSrc,
586                                         uint8_t *dest, int dstW, int dstY)
587 {
588     x86_reg dummy=0;
589     x86_reg dstW_reg = dstW;
590     x86_reg uv_off = c->uv_off << 1;
591
592     YSCALEYUV2PACKEDX
593     YSCALEYUV2RGBX
594     "pxor %%mm7, %%mm7 \n\t"
595     /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */
596 #ifdef DITHER1XBPP
597     "paddusb "BLUE_DITHER"(%0), %%mm2  \n\t"
598     "paddusb "GREEN_DITHER"(%0), %%mm4  \n\t"
599     "paddusb "RED_DITHER"(%0), %%mm5  \n\t"
600 #endif
601     WRITERGB16(%4, %5, %%REGa)
602     YSCALEYUV2PACKEDX_END
603 }
604
605 #define REAL_WRITERGB15(dst, dstw, index) \
606     "pand "MANGLE(bF8)", %%mm2  \n\t" /* B */\
607     "pand "MANGLE(bF8)", %%mm4  \n\t" /* G */\
608     "pand "MANGLE(bF8)", %%mm5  \n\t" /* R */\
609     "psrlq           $3, %%mm2  \n\t"\
610     "psrlq           $1, %%mm5  \n\t"\
611 \
612     "movq         %%mm2, %%mm1  \n\t"\
613     "movq         %%mm4, %%mm3  \n\t"\
614 \
615     "punpcklbw    %%mm7, %%mm3  \n\t"\
616     "punpcklbw    %%mm5, %%mm2  \n\t"\
617     "punpckhbw    %%mm7, %%mm4  \n\t"\
618     "punpckhbw    %%mm5, %%mm1  \n\t"\
619 \
620     "psllq           $2, %%mm3  \n\t"\
621     "psllq           $2, %%mm4  \n\t"\
622 \
623     "por          %%mm3, %%mm2  \n\t"\
624     "por          %%mm4, %%mm1  \n\t"\
625 \
626     MOVNTQ(%%mm2,  (dst, index, 2))\
627     MOVNTQ(%%mm1, 8(dst, index, 2))\
628 \
629     "add             $8, "#index"   \n\t"\
630     "cmp        "#dstw", "#index"   \n\t"\
631     " jb             1b             \n\t"
632 #define WRITERGB15(dst, dstw, index)  REAL_WRITERGB15(dst, dstw, index)
633
634 static inline void RENAME(yuv2rgb555_X_ar)(SwsContext *c, const int16_t *lumFilter,
635                                            const int16_t **lumSrc, int lumFilterSize,
636                                            const int16_t *chrFilter, const int16_t **chrUSrc,
637                                            const int16_t **chrVSrc,
638                                            int chrFilterSize, const int16_t **alpSrc,
639                                            uint8_t *dest, int dstW, int dstY)
640 {
641     x86_reg dummy=0;
642     x86_reg dstW_reg = dstW;
643     x86_reg uv_off = c->uv_off << 1;
644
645     YSCALEYUV2PACKEDX_ACCURATE
646     YSCALEYUV2RGBX
647     "pxor %%mm7, %%mm7 \n\t"
648     /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */
649 #ifdef DITHER1XBPP
650     "paddusb "BLUE_DITHER"(%0), %%mm2\n\t"
651     "paddusb "GREEN_DITHER"(%0), %%mm4\n\t"
652     "paddusb "RED_DITHER"(%0), %%mm5\n\t"
653 #endif
654     WRITERGB15(%4, %5, %%REGa)
655     YSCALEYUV2PACKEDX_END
656 }
657
658 static inline void RENAME(yuv2rgb555_X)(SwsContext *c, const int16_t *lumFilter,
659                                         const int16_t **lumSrc, int lumFilterSize,
660                                         const int16_t *chrFilter, const int16_t **chrUSrc,
661                                         const int16_t **chrVSrc,
662                                         int chrFilterSize, const int16_t **alpSrc,
663                                         uint8_t *dest, int dstW, int dstY)
664 {
665     x86_reg dummy=0;
666     x86_reg dstW_reg = dstW;
667     x86_reg uv_off = c->uv_off << 1;
668
669     YSCALEYUV2PACKEDX
670     YSCALEYUV2RGBX
671     "pxor %%mm7, %%mm7 \n\t"
672     /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */
673 #ifdef DITHER1XBPP
674     "paddusb "BLUE_DITHER"(%0), %%mm2  \n\t"
675     "paddusb "GREEN_DITHER"(%0), %%mm4  \n\t"
676     "paddusb "RED_DITHER"(%0), %%mm5  \n\t"
677 #endif
678     WRITERGB15(%4, %5, %%REGa)
679     YSCALEYUV2PACKEDX_END
680 }
681
682 #define WRITEBGR24MMX(dst, dstw, index) \
683     /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */\
684     "movq      %%mm2, %%mm1     \n\t" /* B */\
685     "movq      %%mm5, %%mm6     \n\t" /* R */\
686     "punpcklbw %%mm4, %%mm2     \n\t" /* GBGBGBGB 0 */\
687     "punpcklbw %%mm7, %%mm5     \n\t" /* 0R0R0R0R 0 */\
688     "punpckhbw %%mm4, %%mm1     \n\t" /* GBGBGBGB 2 */\
689     "punpckhbw %%mm7, %%mm6     \n\t" /* 0R0R0R0R 2 */\
690     "movq      %%mm2, %%mm0     \n\t" /* GBGBGBGB 0 */\
691     "movq      %%mm1, %%mm3     \n\t" /* GBGBGBGB 2 */\
692     "punpcklwd %%mm5, %%mm0     \n\t" /* 0RGB0RGB 0 */\
693     "punpckhwd %%mm5, %%mm2     \n\t" /* 0RGB0RGB 1 */\
694     "punpcklwd %%mm6, %%mm1     \n\t" /* 0RGB0RGB 2 */\
695     "punpckhwd %%mm6, %%mm3     \n\t" /* 0RGB0RGB 3 */\
696 \
697     "movq      %%mm0, %%mm4     \n\t" /* 0RGB0RGB 0 */\
698     "movq      %%mm2, %%mm6     \n\t" /* 0RGB0RGB 1 */\
699     "movq      %%mm1, %%mm5     \n\t" /* 0RGB0RGB 2 */\
700     "movq      %%mm3, %%mm7     \n\t" /* 0RGB0RGB 3 */\
701 \
702     "psllq       $40, %%mm0     \n\t" /* RGB00000 0 */\
703     "psllq       $40, %%mm2     \n\t" /* RGB00000 1 */\
704     "psllq       $40, %%mm1     \n\t" /* RGB00000 2 */\
705     "psllq       $40, %%mm3     \n\t" /* RGB00000 3 */\
706 \
707     "punpckhdq %%mm4, %%mm0     \n\t" /* 0RGBRGB0 0 */\
708     "punpckhdq %%mm6, %%mm2     \n\t" /* 0RGBRGB0 1 */\
709     "punpckhdq %%mm5, %%mm1     \n\t" /* 0RGBRGB0 2 */\
710     "punpckhdq %%mm7, %%mm3     \n\t" /* 0RGBRGB0 3 */\
711 \
712     "psrlq        $8, %%mm0     \n\t" /* 00RGBRGB 0 */\
713     "movq      %%mm2, %%mm6     \n\t" /* 0RGBRGB0 1 */\
714     "psllq       $40, %%mm2     \n\t" /* GB000000 1 */\
715     "por       %%mm2, %%mm0     \n\t" /* GBRGBRGB 0 */\
716     MOVNTQ(%%mm0, (dst))\
717 \
718     "psrlq       $24, %%mm6     \n\t" /* 0000RGBR 1 */\
719     "movq      %%mm1, %%mm5     \n\t" /* 0RGBRGB0 2 */\
720     "psllq       $24, %%mm1     \n\t" /* BRGB0000 2 */\
721     "por       %%mm1, %%mm6     \n\t" /* BRGBRGBR 1 */\
722     MOVNTQ(%%mm6, 8(dst))\
723 \
724     "psrlq       $40, %%mm5     \n\t" /* 000000RG 2 */\
725     "psllq        $8, %%mm3     \n\t" /* RGBRGB00 3 */\
726     "por       %%mm3, %%mm5     \n\t" /* RGBRGBRG 2 */\
727     MOVNTQ(%%mm5, 16(dst))\
728 \
729     "add         $24, "#dst"    \n\t"\
730 \
731     "add          $8, "#index"  \n\t"\
732     "cmp     "#dstw", "#index"  \n\t"\
733     " jb          1b            \n\t"
734
735 #define WRITEBGR24MMX2(dst, dstw, index) \
736     /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */\
737     "movq "MANGLE(ff_M24A)", %%mm0 \n\t"\
738     "movq "MANGLE(ff_M24C)", %%mm7 \n\t"\
739     "pshufw $0x50, %%mm2, %%mm1 \n\t" /* B3 B2 B3 B2  B1 B0 B1 B0 */\
740     "pshufw $0x50, %%mm4, %%mm3 \n\t" /* G3 G2 G3 G2  G1 G0 G1 G0 */\
741     "pshufw $0x00, %%mm5, %%mm6 \n\t" /* R1 R0 R1 R0  R1 R0 R1 R0 */\
742 \
743     "pand   %%mm0, %%mm1        \n\t" /*    B2        B1       B0 */\
744     "pand   %%mm0, %%mm3        \n\t" /*    G2        G1       G0 */\
745     "pand   %%mm7, %%mm6        \n\t" /*       R1        R0       */\
746 \
747     "psllq     $8, %%mm3        \n\t" /* G2        G1       G0    */\
748     "por    %%mm1, %%mm6        \n\t"\
749     "por    %%mm3, %%mm6        \n\t"\
750     MOVNTQ(%%mm6, (dst))\
751 \
752     "psrlq     $8, %%mm4        \n\t" /* 00 G7 G6 G5  G4 G3 G2 G1 */\
753     "pshufw $0xA5, %%mm2, %%mm1 \n\t" /* B5 B4 B5 B4  B3 B2 B3 B2 */\
754     "pshufw $0x55, %%mm4, %%mm3 \n\t" /* G4 G3 G4 G3  G4 G3 G4 G3 */\
755     "pshufw $0xA5, %%mm5, %%mm6 \n\t" /* R5 R4 R5 R4  R3 R2 R3 R2 */\
756 \
757     "pand "MANGLE(ff_M24B)", %%mm1 \n\t" /* B5       B4        B3    */\
758     "pand   %%mm7, %%mm3        \n\t" /*       G4        G3       */\
759     "pand   %%mm0, %%mm6        \n\t" /*    R4        R3       R2 */\
760 \
761     "por    %%mm1, %%mm3        \n\t" /* B5    G4 B4     G3 B3    */\
762     "por    %%mm3, %%mm6        \n\t"\
763     MOVNTQ(%%mm6, 8(dst))\
764 \
765     "pshufw $0xFF, %%mm2, %%mm1 \n\t" /* B7 B6 B7 B6  B7 B6 B6 B7 */\
766     "pshufw $0xFA, %%mm4, %%mm3 \n\t" /* 00 G7 00 G7  G6 G5 G6 G5 */\
767     "pshufw $0xFA, %%mm5, %%mm6 \n\t" /* R7 R6 R7 R6  R5 R4 R5 R4 */\
768 \
769     "pand   %%mm7, %%mm1        \n\t" /*       B7        B6       */\
770     "pand   %%mm0, %%mm3        \n\t" /*    G7        G6       G5 */\
771     "pand "MANGLE(ff_M24B)", %%mm6 \n\t" /* R7       R6        R5    */\
772 \
773     "por    %%mm1, %%mm3        \n\t"\
774     "por    %%mm3, %%mm6        \n\t"\
775     MOVNTQ(%%mm6, 16(dst))\
776 \
777     "add      $24, "#dst"       \n\t"\
778 \
779     "add       $8, "#index"     \n\t"\
780     "cmp  "#dstw", "#index"     \n\t"\
781     " jb       1b               \n\t"
782
783 #if COMPILE_TEMPLATE_MMX2
784 #undef WRITEBGR24
785 #define WRITEBGR24(dst, dstw, index)  WRITEBGR24MMX2(dst, dstw, index)
786 #else
787 #undef WRITEBGR24
788 #define WRITEBGR24(dst, dstw, index)  WRITEBGR24MMX(dst, dstw, index)
789 #endif
790
791 static inline void RENAME(yuv2bgr24_X_ar)(SwsContext *c, const int16_t *lumFilter,
792                                           const int16_t **lumSrc, int lumFilterSize,
793                                           const int16_t *chrFilter, const int16_t **chrUSrc,
794                                           const int16_t **chrVSrc,
795                                           int chrFilterSize, const int16_t **alpSrc,
796                                           uint8_t *dest, int dstW, int dstY)
797 {
798     x86_reg dummy=0;
799     x86_reg dstW_reg = dstW;
800     x86_reg uv_off = c->uv_off << 1;
801
802     YSCALEYUV2PACKEDX_ACCURATE
803     YSCALEYUV2RGBX
804     "pxor %%mm7, %%mm7 \n\t"
805     "lea (%%"REG_a", %%"REG_a", 2), %%"REG_c"\n\t" //FIXME optimize
806     "add %4, %%"REG_c"                        \n\t"
807     WRITEBGR24(%%REGc, %5, %%REGa)
808     :: "r" (&c->redDither),
809        "m" (dummy), "m" (dummy), "m" (dummy),
810        "r" (dest), "m" (dstW_reg), "m"(uv_off)
811     : "%"REG_a, "%"REG_c, "%"REG_d, "%"REG_S
812     );
813 }
814
815 static inline void RENAME(yuv2bgr24_X)(SwsContext *c, const int16_t *lumFilter,
816                                        const int16_t **lumSrc, int lumFilterSize,
817                                        const int16_t *chrFilter, const int16_t **chrUSrc,
818                                        const int16_t **chrVSrc,
819                                        int chrFilterSize, const int16_t **alpSrc,
820                                        uint8_t *dest, int dstW, int dstY)
821 {
822     x86_reg dummy=0;
823     x86_reg dstW_reg = dstW;
824     x86_reg uv_off = c->uv_off << 1;
825
826     YSCALEYUV2PACKEDX
827     YSCALEYUV2RGBX
828     "pxor                    %%mm7, %%mm7       \n\t"
829     "lea (%%"REG_a", %%"REG_a", 2), %%"REG_c"   \n\t" //FIXME optimize
830     "add                        %4, %%"REG_c"   \n\t"
831     WRITEBGR24(%%REGc, %5, %%REGa)
832     :: "r" (&c->redDither),
833        "m" (dummy), "m" (dummy), "m" (dummy),
834        "r" (dest),  "m" (dstW_reg), "m"(uv_off)
835     : "%"REG_a, "%"REG_c, "%"REG_d, "%"REG_S
836     );
837 }
838
839 #define REAL_WRITEYUY2(dst, dstw, index) \
840     "packuswb  %%mm3, %%mm3     \n\t"\
841     "packuswb  %%mm4, %%mm4     \n\t"\
842     "packuswb  %%mm7, %%mm1     \n\t"\
843     "punpcklbw %%mm4, %%mm3     \n\t"\
844     "movq      %%mm1, %%mm7     \n\t"\
845     "punpcklbw %%mm3, %%mm1     \n\t"\
846     "punpckhbw %%mm3, %%mm7     \n\t"\
847 \
848     MOVNTQ(%%mm1, (dst, index, 2))\
849     MOVNTQ(%%mm7, 8(dst, index, 2))\
850 \
851     "add          $8, "#index"  \n\t"\
852     "cmp     "#dstw", "#index"  \n\t"\
853     " jb          1b            \n\t"
854 #define WRITEYUY2(dst, dstw, index)  REAL_WRITEYUY2(dst, dstw, index)
855
856 static inline void RENAME(yuv2yuyv422_X_ar)(SwsContext *c, const int16_t *lumFilter,
857                                             const int16_t **lumSrc, int lumFilterSize,
858                                             const int16_t *chrFilter, const int16_t **chrUSrc,
859                                             const int16_t **chrVSrc,
860                                             int chrFilterSize, const int16_t **alpSrc,
861                                             uint8_t *dest, int dstW, int dstY)
862 {
863     x86_reg dummy=0;
864     x86_reg dstW_reg = dstW;
865     x86_reg uv_off = c->uv_off << 1;
866
867     YSCALEYUV2PACKEDX_ACCURATE
868     /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */
869     "psraw $3, %%mm3    \n\t"
870     "psraw $3, %%mm4    \n\t"
871     "psraw $3, %%mm1    \n\t"
872     "psraw $3, %%mm7    \n\t"
873     WRITEYUY2(%4, %5, %%REGa)
874     YSCALEYUV2PACKEDX_END
875 }
876
877 static inline void RENAME(yuv2yuyv422_X)(SwsContext *c, const int16_t *lumFilter,
878                                          const int16_t **lumSrc, int lumFilterSize,
879                                          const int16_t *chrFilter, const int16_t **chrUSrc,
880                                          const int16_t **chrVSrc,
881                                          int chrFilterSize, const int16_t **alpSrc,
882                                          uint8_t *dest, int dstW, int dstY)
883 {
884     x86_reg dummy=0;
885     x86_reg dstW_reg = dstW;
886     x86_reg uv_off = c->uv_off << 1;
887
888     YSCALEYUV2PACKEDX
889     /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */
890     "psraw $3, %%mm3    \n\t"
891     "psraw $3, %%mm4    \n\t"
892     "psraw $3, %%mm1    \n\t"
893     "psraw $3, %%mm7    \n\t"
894     WRITEYUY2(%4, %5, %%REGa)
895     YSCALEYUV2PACKEDX_END
896 }
897
898 #define REAL_YSCALEYUV2RGB_UV(index, c) \
899     "xor            "#index", "#index"  \n\t"\
900     ".p2align              4            \n\t"\
901     "1:                                 \n\t"\
902     "movq     (%2, "#index"), %%mm2     \n\t" /* uvbuf0[eax]*/\
903     "movq     (%3, "#index"), %%mm3     \n\t" /* uvbuf1[eax]*/\
904     "add           "UV_OFFx2"("#c"), "#index"  \n\t" \
905     "movq     (%2, "#index"), %%mm5     \n\t" /* uvbuf0[eax+2048]*/\
906     "movq     (%3, "#index"), %%mm4     \n\t" /* uvbuf1[eax+2048]*/\
907     "sub           "UV_OFFx2"("#c"), "#index"  \n\t" \
908     "psubw             %%mm3, %%mm2     \n\t" /* uvbuf0[eax] - uvbuf1[eax]*/\
909     "psubw             %%mm4, %%mm5     \n\t" /* uvbuf0[eax+2048] - uvbuf1[eax+2048]*/\
910     "movq "CHR_MMX_FILTER_OFFSET"+8("#c"), %%mm0    \n\t"\
911     "pmulhw            %%mm0, %%mm2     \n\t" /* (uvbuf0[eax] - uvbuf1[eax])uvalpha1>>16*/\
912     "pmulhw            %%mm0, %%mm5     \n\t" /* (uvbuf0[eax+2048] - uvbuf1[eax+2048])uvalpha1>>16*/\
913     "psraw                $4, %%mm3     \n\t" /* uvbuf0[eax] - uvbuf1[eax] >>4*/\
914     "psraw                $4, %%mm4     \n\t" /* uvbuf0[eax+2048] - uvbuf1[eax+2048] >>4*/\
915     "paddw             %%mm2, %%mm3     \n\t" /* uvbuf0[eax]uvalpha1 - uvbuf1[eax](1-uvalpha1)*/\
916     "paddw             %%mm5, %%mm4     \n\t" /* uvbuf0[eax+2048]uvalpha1 - uvbuf1[eax+2048](1-uvalpha1)*/\
917     "psubw  "U_OFFSET"("#c"), %%mm3     \n\t" /* (U-128)8*/\
918     "psubw  "V_OFFSET"("#c"), %%mm4     \n\t" /* (V-128)8*/\
919     "movq              %%mm3, %%mm2     \n\t" /* (U-128)8*/\
920     "movq              %%mm4, %%mm5     \n\t" /* (V-128)8*/\
921     "pmulhw "UG_COEFF"("#c"), %%mm3     \n\t"\
922     "pmulhw "VG_COEFF"("#c"), %%mm4     \n\t"\
923     /* mm2=(U-128)8, mm3=ug, mm4=vg mm5=(V-128)8 */\
924
925 #define REAL_YSCALEYUV2RGB_YA(index, c, b1, b2) \
926     "movq  ("#b1", "#index", 2), %%mm0     \n\t" /*buf0[eax]*/\
927     "movq  ("#b2", "#index", 2), %%mm1     \n\t" /*buf1[eax]*/\
928     "movq 8("#b1", "#index", 2), %%mm6     \n\t" /*buf0[eax]*/\
929     "movq 8("#b2", "#index", 2), %%mm7     \n\t" /*buf1[eax]*/\
930     "psubw             %%mm1, %%mm0     \n\t" /* buf0[eax] - buf1[eax]*/\
931     "psubw             %%mm7, %%mm6     \n\t" /* buf0[eax] - buf1[eax]*/\
932     "pmulhw "LUM_MMX_FILTER_OFFSET"+8("#c"), %%mm0  \n\t" /* (buf0[eax] - buf1[eax])yalpha1>>16*/\
933     "pmulhw "LUM_MMX_FILTER_OFFSET"+8("#c"), %%mm6  \n\t" /* (buf0[eax] - buf1[eax])yalpha1>>16*/\
934     "psraw                $4, %%mm1     \n\t" /* buf0[eax] - buf1[eax] >>4*/\
935     "psraw                $4, %%mm7     \n\t" /* buf0[eax] - buf1[eax] >>4*/\
936     "paddw             %%mm0, %%mm1     \n\t" /* buf0[eax]yalpha1 + buf1[eax](1-yalpha1) >>16*/\
937     "paddw             %%mm6, %%mm7     \n\t" /* buf0[eax]yalpha1 + buf1[eax](1-yalpha1) >>16*/\
938
939 #define REAL_YSCALEYUV2RGB_COEFF(c) \
940     "pmulhw "UB_COEFF"("#c"), %%mm2     \n\t"\
941     "pmulhw "VR_COEFF"("#c"), %%mm5     \n\t"\
942     "psubw  "Y_OFFSET"("#c"), %%mm1     \n\t" /* 8(Y-16)*/\
943     "psubw  "Y_OFFSET"("#c"), %%mm7     \n\t" /* 8(Y-16)*/\
944     "pmulhw  "Y_COEFF"("#c"), %%mm1     \n\t"\
945     "pmulhw  "Y_COEFF"("#c"), %%mm7     \n\t"\
946     /* mm1= Y1, mm2=ub, mm3=ug, mm4=vg mm5=vr, mm7=Y2 */\
947     "paddw             %%mm3, %%mm4     \n\t"\
948     "movq              %%mm2, %%mm0     \n\t"\
949     "movq              %%mm5, %%mm6     \n\t"\
950     "movq              %%mm4, %%mm3     \n\t"\
951     "punpcklwd         %%mm2, %%mm2     \n\t"\
952     "punpcklwd         %%mm5, %%mm5     \n\t"\
953     "punpcklwd         %%mm4, %%mm4     \n\t"\
954     "paddw             %%mm1, %%mm2     \n\t"\
955     "paddw             %%mm1, %%mm5     \n\t"\
956     "paddw             %%mm1, %%mm4     \n\t"\
957     "punpckhwd         %%mm0, %%mm0     \n\t"\
958     "punpckhwd         %%mm6, %%mm6     \n\t"\
959     "punpckhwd         %%mm3, %%mm3     \n\t"\
960     "paddw             %%mm7, %%mm0     \n\t"\
961     "paddw             %%mm7, %%mm6     \n\t"\
962     "paddw             %%mm7, %%mm3     \n\t"\
963     /* mm0=B1, mm2=B2, mm3=G2, mm4=G1, mm5=R1, mm6=R2 */\
964     "packuswb          %%mm0, %%mm2     \n\t"\
965     "packuswb          %%mm6, %%mm5     \n\t"\
966     "packuswb          %%mm3, %%mm4     \n\t"\
967
968 #define YSCALEYUV2RGB_YA(index, c, b1, b2) REAL_YSCALEYUV2RGB_YA(index, c, b1, b2)
969
970 #define YSCALEYUV2RGB(index, c) \
971     REAL_YSCALEYUV2RGB_UV(index, c) \
972     REAL_YSCALEYUV2RGB_YA(index, c, %0, %1) \
973     REAL_YSCALEYUV2RGB_COEFF(c)
974
975 /**
976  * vertical bilinear scale YV12 to RGB
977  */
978 static inline void RENAME(yuv2rgb32_2)(SwsContext *c, const uint16_t *buf0,
979                                        const uint16_t *buf1, const uint16_t *ubuf0,
980                                        const uint16_t *ubuf1, const uint16_t *vbuf0,
981                                        const uint16_t *vbuf1, const uint16_t *abuf0,
982                                        const uint16_t *abuf1, uint8_t *dest,
983                                        int dstW, int yalpha, int uvalpha, int y)
984 {
985     if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
986 #if ARCH_X86_64
987         __asm__ volatile(
988             YSCALEYUV2RGB(%%r8, %5)
989             YSCALEYUV2RGB_YA(%%r8, %5, %6, %7)
990             "psraw                  $3, %%mm1       \n\t" /* abuf0[eax] - abuf1[eax] >>7*/
991             "psraw                  $3, %%mm7       \n\t" /* abuf0[eax] - abuf1[eax] >>7*/
992             "packuswb            %%mm7, %%mm1       \n\t"
993             WRITEBGR32(%4, 8280(%5), %%r8, %%mm2, %%mm4, %%mm5, %%mm1, %%mm0, %%mm7, %%mm3, %%mm6)
994             :: "c" (buf0), "d" (buf1), "S" (ubuf0), "D" (ubuf1), "r" (dest),
995                "a" (&c->redDither),
996                "r" (abuf0), "r" (abuf1)
997             : "%r8"
998         );
999 #else
1000         *(const uint16_t **)(&c->u_temp)=abuf0;
1001         *(const uint16_t **)(&c->v_temp)=abuf1;
1002         __asm__ volatile(
1003             "mov %%"REG_b", "ESP_OFFSET"(%5)        \n\t"
1004             "mov        %4, %%"REG_b"               \n\t"
1005             "push %%"REG_BP"                        \n\t"
1006             YSCALEYUV2RGB(%%REGBP, %5)
1007             "push                   %0              \n\t"
1008             "push                   %1              \n\t"
1009             "mov          "U_TEMP"(%5), %0          \n\t"
1010             "mov          "V_TEMP"(%5), %1          \n\t"
1011             YSCALEYUV2RGB_YA(%%REGBP, %5, %0, %1)
1012             "psraw                  $3, %%mm1       \n\t" /* abuf0[eax] - abuf1[eax] >>7*/
1013             "psraw                  $3, %%mm7       \n\t" /* abuf0[eax] - abuf1[eax] >>7*/
1014             "packuswb            %%mm7, %%mm1       \n\t"
1015             "pop                    %1              \n\t"
1016             "pop                    %0              \n\t"
1017             WRITEBGR32(%%REGb, 8280(%5), %%REGBP, %%mm2, %%mm4, %%mm5, %%mm1, %%mm0, %%mm7, %%mm3, %%mm6)
1018             "pop %%"REG_BP"                         \n\t"
1019             "mov "ESP_OFFSET"(%5), %%"REG_b"        \n\t"
1020             :: "c" (buf0), "d" (buf1), "S" (ubuf0), "D" (ubuf1), "m" (dest),
1021                "a" (&c->redDither)
1022         );
1023 #endif
1024     } else {
1025         __asm__ volatile(
1026             "mov %%"REG_b", "ESP_OFFSET"(%5)        \n\t"
1027             "mov        %4, %%"REG_b"               \n\t"
1028             "push %%"REG_BP"                        \n\t"
1029             YSCALEYUV2RGB(%%REGBP, %5)
1030             "pcmpeqd %%mm7, %%mm7                   \n\t"
1031             WRITEBGR32(%%REGb, 8280(%5), %%REGBP, %%mm2, %%mm4, %%mm5, %%mm7, %%mm0, %%mm1, %%mm3, %%mm6)
1032             "pop %%"REG_BP"                         \n\t"
1033             "mov "ESP_OFFSET"(%5), %%"REG_b"        \n\t"
1034             :: "c" (buf0), "d" (buf1), "S" (ubuf0), "D" (ubuf1), "m" (dest),
1035                "a" (&c->redDither)
1036         );
1037     }
1038 }
1039
1040 static inline void RENAME(yuv2bgr24_2)(SwsContext *c, const uint16_t *buf0,
1041                                        const uint16_t *buf1, const uint16_t *ubuf0,
1042                                        const uint16_t *ubuf1, const uint16_t *vbuf0,
1043                                        const uint16_t *vbuf1, const uint16_t *abuf0,
1044                                        const uint16_t *abuf1, uint8_t *dest,
1045                                        int dstW, int yalpha, int uvalpha, int y)
1046 {
1047     //Note 8280 == DSTW_OFFSET but the preprocessor can't handle that there :(
1048     __asm__ volatile(
1049         "mov %%"REG_b", "ESP_OFFSET"(%5)        \n\t"
1050         "mov        %4, %%"REG_b"               \n\t"
1051         "push %%"REG_BP"                        \n\t"
1052         YSCALEYUV2RGB(%%REGBP, %5)
1053         "pxor    %%mm7, %%mm7                   \n\t"
1054         WRITEBGR24(%%REGb, 8280(%5), %%REGBP)
1055         "pop %%"REG_BP"                         \n\t"
1056         "mov "ESP_OFFSET"(%5), %%"REG_b"        \n\t"
1057         :: "c" (buf0), "d" (buf1), "S" (ubuf0), "D" (ubuf1), "m" (dest),
1058            "a" (&c->redDither)
1059     );
1060 }
1061
1062 static inline void RENAME(yuv2rgb555_2)(SwsContext *c, const uint16_t *buf0,
1063                                         const uint16_t *buf1, const uint16_t *ubuf0,
1064                                         const uint16_t *ubuf1, const uint16_t *vbuf0,
1065                                         const uint16_t *vbuf1, const uint16_t *abuf0,
1066                                         const uint16_t *abuf1, uint8_t *dest,
1067                                         int dstW, int yalpha, int uvalpha, int y)
1068 {
1069     //Note 8280 == DSTW_OFFSET but the preprocessor can't handle that there :(
1070     __asm__ volatile(
1071         "mov %%"REG_b", "ESP_OFFSET"(%5)        \n\t"
1072         "mov        %4, %%"REG_b"               \n\t"
1073         "push %%"REG_BP"                        \n\t"
1074         YSCALEYUV2RGB(%%REGBP, %5)
1075         "pxor    %%mm7, %%mm7                   \n\t"
1076         /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */
1077 #ifdef DITHER1XBPP
1078         "paddusb "BLUE_DITHER"(%5), %%mm2      \n\t"
1079         "paddusb "GREEN_DITHER"(%5), %%mm4      \n\t"
1080         "paddusb "RED_DITHER"(%5), %%mm5      \n\t"
1081 #endif
1082         WRITERGB15(%%REGb, 8280(%5), %%REGBP)
1083         "pop %%"REG_BP"                         \n\t"
1084         "mov "ESP_OFFSET"(%5), %%"REG_b"        \n\t"
1085         :: "c" (buf0), "d" (buf1), "S" (ubuf0), "D" (ubuf1), "m" (dest),
1086            "a" (&c->redDither)
1087     );
1088 }
1089
1090 static inline void RENAME(yuv2rgb565_2)(SwsContext *c, const uint16_t *buf0,
1091                                         const uint16_t *buf1, const uint16_t *ubuf0,
1092                                         const uint16_t *ubuf1, const uint16_t *vbuf0,
1093                                         const uint16_t *vbuf1, const uint16_t *abuf0,
1094                                         const uint16_t *abuf1, uint8_t *dest,
1095                                         int dstW, int yalpha, int uvalpha, int y)
1096 {
1097     //Note 8280 == DSTW_OFFSET but the preprocessor can't handle that there :(
1098     __asm__ volatile(
1099         "mov %%"REG_b", "ESP_OFFSET"(%5)        \n\t"
1100         "mov        %4, %%"REG_b"               \n\t"
1101         "push %%"REG_BP"                        \n\t"
1102         YSCALEYUV2RGB(%%REGBP, %5)
1103         "pxor    %%mm7, %%mm7                   \n\t"
1104         /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */
1105 #ifdef DITHER1XBPP
1106         "paddusb "BLUE_DITHER"(%5), %%mm2      \n\t"
1107         "paddusb "GREEN_DITHER"(%5), %%mm4      \n\t"
1108         "paddusb "RED_DITHER"(%5), %%mm5      \n\t"
1109 #endif
1110         WRITERGB16(%%REGb, 8280(%5), %%REGBP)
1111         "pop %%"REG_BP"                         \n\t"
1112         "mov "ESP_OFFSET"(%5), %%"REG_b"        \n\t"
1113         :: "c" (buf0), "d" (buf1), "S" (ubuf0), "D" (ubuf1), "m" (dest),
1114            "a" (&c->redDither)
1115     );
1116 }
1117
1118 #define REAL_YSCALEYUV2PACKED(index, c) \
1119     "movq "CHR_MMX_FILTER_OFFSET"+8("#c"), %%mm0              \n\t"\
1120     "movq "LUM_MMX_FILTER_OFFSET"+8("#c"), %%mm1              \n\t"\
1121     "psraw                $3, %%mm0                           \n\t"\
1122     "psraw                $3, %%mm1                           \n\t"\
1123     "movq              %%mm0, "CHR_MMX_FILTER_OFFSET"+8("#c") \n\t"\
1124     "movq              %%mm1, "LUM_MMX_FILTER_OFFSET"+8("#c") \n\t"\
1125     "xor            "#index", "#index"                        \n\t"\
1126     ".p2align              4            \n\t"\
1127     "1:                                 \n\t"\
1128     "movq     (%2, "#index"), %%mm2     \n\t" /* uvbuf0[eax]*/\
1129     "movq     (%3, "#index"), %%mm3     \n\t" /* uvbuf1[eax]*/\
1130     "add           "UV_OFFx2"("#c"), "#index"  \n\t" \
1131     "movq     (%2, "#index"), %%mm5     \n\t" /* uvbuf0[eax+2048]*/\
1132     "movq     (%3, "#index"), %%mm4     \n\t" /* uvbuf1[eax+2048]*/\
1133     "sub           "UV_OFFx2"("#c"), "#index"  \n\t" \
1134     "psubw             %%mm3, %%mm2     \n\t" /* uvbuf0[eax] - uvbuf1[eax]*/\
1135     "psubw             %%mm4, %%mm5     \n\t" /* uvbuf0[eax+2048] - uvbuf1[eax+2048]*/\
1136     "movq "CHR_MMX_FILTER_OFFSET"+8("#c"), %%mm0    \n\t"\
1137     "pmulhw            %%mm0, %%mm2     \n\t" /* (uvbuf0[eax] - uvbuf1[eax])uvalpha1>>16*/\
1138     "pmulhw            %%mm0, %%mm5     \n\t" /* (uvbuf0[eax+2048] - uvbuf1[eax+2048])uvalpha1>>16*/\
1139     "psraw                $7, %%mm3     \n\t" /* uvbuf0[eax] - uvbuf1[eax] >>4*/\
1140     "psraw                $7, %%mm4     \n\t" /* uvbuf0[eax+2048] - uvbuf1[eax+2048] >>4*/\
1141     "paddw             %%mm2, %%mm3     \n\t" /* uvbuf0[eax]uvalpha1 - uvbuf1[eax](1-uvalpha1)*/\
1142     "paddw             %%mm5, %%mm4     \n\t" /* uvbuf0[eax+2048]uvalpha1 - uvbuf1[eax+2048](1-uvalpha1)*/\
1143     "movq  (%0, "#index", 2), %%mm0     \n\t" /*buf0[eax]*/\
1144     "movq  (%1, "#index", 2), %%mm1     \n\t" /*buf1[eax]*/\
1145     "movq 8(%0, "#index", 2), %%mm6     \n\t" /*buf0[eax]*/\
1146     "movq 8(%1, "#index", 2), %%mm7     \n\t" /*buf1[eax]*/\
1147     "psubw             %%mm1, %%mm0     \n\t" /* buf0[eax] - buf1[eax]*/\
1148     "psubw             %%mm7, %%mm6     \n\t" /* buf0[eax] - buf1[eax]*/\
1149     "pmulhw "LUM_MMX_FILTER_OFFSET"+8("#c"), %%mm0  \n\t" /* (buf0[eax] - buf1[eax])yalpha1>>16*/\
1150     "pmulhw "LUM_MMX_FILTER_OFFSET"+8("#c"), %%mm6  \n\t" /* (buf0[eax] - buf1[eax])yalpha1>>16*/\
1151     "psraw                $7, %%mm1     \n\t" /* buf0[eax] - buf1[eax] >>4*/\
1152     "psraw                $7, %%mm7     \n\t" /* buf0[eax] - buf1[eax] >>4*/\
1153     "paddw             %%mm0, %%mm1     \n\t" /* buf0[eax]yalpha1 + buf1[eax](1-yalpha1) >>16*/\
1154     "paddw             %%mm6, %%mm7     \n\t" /* buf0[eax]yalpha1 + buf1[eax](1-yalpha1) >>16*/\
1155
1156 #define YSCALEYUV2PACKED(index, c)  REAL_YSCALEYUV2PACKED(index, c)
1157
1158 static inline void RENAME(yuv2yuyv422_2)(SwsContext *c, const uint16_t *buf0,
1159                                          const uint16_t *buf1, const uint16_t *ubuf0,
1160                                          const uint16_t *ubuf1, const uint16_t *vbuf0,
1161                                          const uint16_t *vbuf1, const uint16_t *abuf0,
1162                                          const uint16_t *abuf1, uint8_t *dest,
1163                                          int dstW, int yalpha, int uvalpha, int y)
1164 {
1165     //Note 8280 == DSTW_OFFSET but the preprocessor can't handle that there :(
1166     __asm__ volatile(
1167         "mov %%"REG_b", "ESP_OFFSET"(%5)        \n\t"
1168         "mov %4, %%"REG_b"                        \n\t"
1169         "push %%"REG_BP"                        \n\t"
1170         YSCALEYUV2PACKED(%%REGBP, %5)
1171         WRITEYUY2(%%REGb, 8280(%5), %%REGBP)
1172         "pop %%"REG_BP"                         \n\t"
1173         "mov "ESP_OFFSET"(%5), %%"REG_b"        \n\t"
1174         :: "c" (buf0), "d" (buf1), "S" (ubuf0), "D" (ubuf1), "m" (dest),
1175            "a" (&c->redDither)
1176     );
1177 }
1178
1179 #define REAL_YSCALEYUV2RGB1(index, c) \
1180     "xor            "#index", "#index"  \n\t"\
1181     ".p2align              4            \n\t"\
1182     "1:                                 \n\t"\
1183     "movq     (%2, "#index"), %%mm3     \n\t" /* uvbuf0[eax]*/\
1184     "add           "UV_OFFx2"("#c"), "#index"  \n\t" \
1185     "movq     (%2, "#index"), %%mm4     \n\t" /* uvbuf0[eax+2048]*/\
1186     "sub           "UV_OFFx2"("#c"), "#index"  \n\t" \
1187     "psraw                $4, %%mm3     \n\t" /* uvbuf0[eax] - uvbuf1[eax] >>4*/\
1188     "psraw                $4, %%mm4     \n\t" /* uvbuf0[eax+2048] - uvbuf1[eax+2048] >>4*/\
1189     "psubw  "U_OFFSET"("#c"), %%mm3     \n\t" /* (U-128)8*/\
1190     "psubw  "V_OFFSET"("#c"), %%mm4     \n\t" /* (V-128)8*/\
1191     "movq              %%mm3, %%mm2     \n\t" /* (U-128)8*/\
1192     "movq              %%mm4, %%mm5     \n\t" /* (V-128)8*/\
1193     "pmulhw "UG_COEFF"("#c"), %%mm3     \n\t"\
1194     "pmulhw "VG_COEFF"("#c"), %%mm4     \n\t"\
1195     /* mm2=(U-128)8, mm3=ug, mm4=vg mm5=(V-128)8 */\
1196     "movq  (%0, "#index", 2), %%mm1     \n\t" /*buf0[eax]*/\
1197     "movq 8(%0, "#index", 2), %%mm7     \n\t" /*buf0[eax]*/\
1198     "psraw                $4, %%mm1     \n\t" /* buf0[eax] - buf1[eax] >>4*/\
1199     "psraw                $4, %%mm7     \n\t" /* buf0[eax] - buf1[eax] >>4*/\
1200     "pmulhw "UB_COEFF"("#c"), %%mm2     \n\t"\
1201     "pmulhw "VR_COEFF"("#c"), %%mm5     \n\t"\
1202     "psubw  "Y_OFFSET"("#c"), %%mm1     \n\t" /* 8(Y-16)*/\
1203     "psubw  "Y_OFFSET"("#c"), %%mm7     \n\t" /* 8(Y-16)*/\
1204     "pmulhw  "Y_COEFF"("#c"), %%mm1     \n\t"\
1205     "pmulhw  "Y_COEFF"("#c"), %%mm7     \n\t"\
1206     /* mm1= Y1, mm2=ub, mm3=ug, mm4=vg mm5=vr, mm7=Y2 */\
1207     "paddw             %%mm3, %%mm4     \n\t"\
1208     "movq              %%mm2, %%mm0     \n\t"\
1209     "movq              %%mm5, %%mm6     \n\t"\
1210     "movq              %%mm4, %%mm3     \n\t"\
1211     "punpcklwd         %%mm2, %%mm2     \n\t"\
1212     "punpcklwd         %%mm5, %%mm5     \n\t"\
1213     "punpcklwd         %%mm4, %%mm4     \n\t"\
1214     "paddw             %%mm1, %%mm2     \n\t"\
1215     "paddw             %%mm1, %%mm5     \n\t"\
1216     "paddw             %%mm1, %%mm4     \n\t"\
1217     "punpckhwd         %%mm0, %%mm0     \n\t"\
1218     "punpckhwd         %%mm6, %%mm6     \n\t"\
1219     "punpckhwd         %%mm3, %%mm3     \n\t"\
1220     "paddw             %%mm7, %%mm0     \n\t"\
1221     "paddw             %%mm7, %%mm6     \n\t"\
1222     "paddw             %%mm7, %%mm3     \n\t"\
1223     /* mm0=B1, mm2=B2, mm3=G2, mm4=G1, mm5=R1, mm6=R2 */\
1224     "packuswb          %%mm0, %%mm2     \n\t"\
1225     "packuswb          %%mm6, %%mm5     \n\t"\
1226     "packuswb          %%mm3, %%mm4     \n\t"\
1227
1228 #define YSCALEYUV2RGB1(index, c)  REAL_YSCALEYUV2RGB1(index, c)
1229
1230 // do vertical chrominance interpolation
1231 #define REAL_YSCALEYUV2RGB1b(index, c) \
1232     "xor            "#index", "#index"  \n\t"\
1233     ".p2align              4            \n\t"\
1234     "1:                                 \n\t"\
1235     "movq     (%2, "#index"), %%mm2     \n\t" /* uvbuf0[eax]*/\
1236     "movq     (%3, "#index"), %%mm3     \n\t" /* uvbuf1[eax]*/\
1237     "add           "UV_OFFx2"("#c"), "#index"  \n\t" \
1238     "movq     (%2, "#index"), %%mm5     \n\t" /* uvbuf0[eax+2048]*/\
1239     "movq     (%3, "#index"), %%mm4     \n\t" /* uvbuf1[eax+2048]*/\
1240     "sub           "UV_OFFx2"("#c"), "#index"  \n\t" \
1241     "paddw             %%mm2, %%mm3     \n\t" /* uvbuf0[eax] + uvbuf1[eax]*/\
1242     "paddw             %%mm5, %%mm4     \n\t" /* uvbuf0[eax+2048] + uvbuf1[eax+2048]*/\
1243     "psrlw                $5, %%mm3     \n\t" /*FIXME might overflow*/\
1244     "psrlw                $5, %%mm4     \n\t" /*FIXME might overflow*/\
1245     "psubw  "U_OFFSET"("#c"), %%mm3     \n\t" /* (U-128)8*/\
1246     "psubw  "V_OFFSET"("#c"), %%mm4     \n\t" /* (V-128)8*/\
1247     "movq              %%mm3, %%mm2     \n\t" /* (U-128)8*/\
1248     "movq              %%mm4, %%mm5     \n\t" /* (V-128)8*/\
1249     "pmulhw "UG_COEFF"("#c"), %%mm3     \n\t"\
1250     "pmulhw "VG_COEFF"("#c"), %%mm4     \n\t"\
1251     /* mm2=(U-128)8, mm3=ug, mm4=vg mm5=(V-128)8 */\
1252     "movq  (%0, "#index", 2), %%mm1     \n\t" /*buf0[eax]*/\
1253     "movq 8(%0, "#index", 2), %%mm7     \n\t" /*buf0[eax]*/\
1254     "psraw                $4, %%mm1     \n\t" /* buf0[eax] - buf1[eax] >>4*/\
1255     "psraw                $4, %%mm7     \n\t" /* buf0[eax] - buf1[eax] >>4*/\
1256     "pmulhw "UB_COEFF"("#c"), %%mm2     \n\t"\
1257     "pmulhw "VR_COEFF"("#c"), %%mm5     \n\t"\
1258     "psubw  "Y_OFFSET"("#c"), %%mm1     \n\t" /* 8(Y-16)*/\
1259     "psubw  "Y_OFFSET"("#c"), %%mm7     \n\t" /* 8(Y-16)*/\
1260     "pmulhw  "Y_COEFF"("#c"), %%mm1     \n\t"\
1261     "pmulhw  "Y_COEFF"("#c"), %%mm7     \n\t"\
1262     /* mm1= Y1, mm2=ub, mm3=ug, mm4=vg mm5=vr, mm7=Y2 */\
1263     "paddw             %%mm3, %%mm4     \n\t"\
1264     "movq              %%mm2, %%mm0     \n\t"\
1265     "movq              %%mm5, %%mm6     \n\t"\
1266     "movq              %%mm4, %%mm3     \n\t"\
1267     "punpcklwd         %%mm2, %%mm2     \n\t"\
1268     "punpcklwd         %%mm5, %%mm5     \n\t"\
1269     "punpcklwd         %%mm4, %%mm4     \n\t"\
1270     "paddw             %%mm1, %%mm2     \n\t"\
1271     "paddw             %%mm1, %%mm5     \n\t"\
1272     "paddw             %%mm1, %%mm4     \n\t"\
1273     "punpckhwd         %%mm0, %%mm0     \n\t"\
1274     "punpckhwd         %%mm6, %%mm6     \n\t"\
1275     "punpckhwd         %%mm3, %%mm3     \n\t"\
1276     "paddw             %%mm7, %%mm0     \n\t"\
1277     "paddw             %%mm7, %%mm6     \n\t"\
1278     "paddw             %%mm7, %%mm3     \n\t"\
1279     /* mm0=B1, mm2=B2, mm3=G2, mm4=G1, mm5=R1, mm6=R2 */\
1280     "packuswb          %%mm0, %%mm2     \n\t"\
1281     "packuswb          %%mm6, %%mm5     \n\t"\
1282     "packuswb          %%mm3, %%mm4     \n\t"\
1283
1284 #define YSCALEYUV2RGB1b(index, c)  REAL_YSCALEYUV2RGB1b(index, c)
1285
1286 #define REAL_YSCALEYUV2RGB1_ALPHA(index) \
1287     "movq  (%1, "#index", 2), %%mm7     \n\t" /* abuf0[index  ]     */\
1288     "movq 8(%1, "#index", 2), %%mm1     \n\t" /* abuf0[index+4]     */\
1289     "psraw                $7, %%mm7     \n\t" /* abuf0[index  ] >>7 */\
1290     "psraw                $7, %%mm1     \n\t" /* abuf0[index+4] >>7 */\
1291     "packuswb          %%mm1, %%mm7     \n\t"
1292 #define YSCALEYUV2RGB1_ALPHA(index) REAL_YSCALEYUV2RGB1_ALPHA(index)
1293
1294 /**
1295  * YV12 to RGB without scaling or interpolating
1296  */
1297 static inline void RENAME(yuv2rgb32_1)(SwsContext *c, const uint16_t *buf0,
1298                                        const uint16_t *ubuf0, const uint16_t *ubuf1,
1299                                        const uint16_t *vbuf0, const uint16_t *vbuf1,
1300                                        const uint16_t *abuf0, uint8_t *dest,
1301                                        int dstW, int uvalpha, enum PixelFormat dstFormat,
1302                                        int flags, int y)
1303 {
1304     const uint16_t *buf1= buf0; //FIXME needed for RGB1/BGR1
1305
1306     if (uvalpha < 2048) { // note this is not correct (shifts chrominance by 0.5 pixels) but it is a bit faster
1307         if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
1308             __asm__ volatile(
1309                 "mov %%"REG_b", "ESP_OFFSET"(%5)        \n\t"
1310                 "mov        %4, %%"REG_b"               \n\t"
1311                 "push %%"REG_BP"                        \n\t"
1312                 YSCALEYUV2RGB1(%%REGBP, %5)
1313                 YSCALEYUV2RGB1_ALPHA(%%REGBP)
1314                 WRITEBGR32(%%REGb, 8280(%5), %%REGBP, %%mm2, %%mm4, %%mm5, %%mm7, %%mm0, %%mm1, %%mm3, %%mm6)
1315                 "pop %%"REG_BP"                         \n\t"
1316                 "mov "ESP_OFFSET"(%5), %%"REG_b"        \n\t"
1317                 :: "c" (buf0), "d" (abuf0), "S" (ubuf0), "D" (ubuf1), "m" (dest),
1318                    "a" (&c->redDither)
1319             );
1320         } else {
1321             __asm__ volatile(
1322                 "mov %%"REG_b", "ESP_OFFSET"(%5)        \n\t"
1323                 "mov        %4, %%"REG_b"               \n\t"
1324                 "push %%"REG_BP"                        \n\t"
1325                 YSCALEYUV2RGB1(%%REGBP, %5)
1326                 "pcmpeqd %%mm7, %%mm7                   \n\t"
1327                 WRITEBGR32(%%REGb, 8280(%5), %%REGBP, %%mm2, %%mm4, %%mm5, %%mm7, %%mm0, %%mm1, %%mm3, %%mm6)
1328                 "pop %%"REG_BP"                         \n\t"
1329                 "mov "ESP_OFFSET"(%5), %%"REG_b"        \n\t"
1330                 :: "c" (buf0), "d" (buf1), "S" (ubuf0), "D" (ubuf1), "m" (dest),
1331                    "a" (&c->redDither)
1332             );
1333         }
1334     } else {
1335         if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
1336             __asm__ volatile(
1337                 "mov %%"REG_b", "ESP_OFFSET"(%5)        \n\t"
1338                 "mov        %4, %%"REG_b"               \n\t"
1339                 "push %%"REG_BP"                        \n\t"
1340                 YSCALEYUV2RGB1b(%%REGBP, %5)
1341                 YSCALEYUV2RGB1_ALPHA(%%REGBP)
1342                 WRITEBGR32(%%REGb, 8280(%5), %%REGBP, %%mm2, %%mm4, %%mm5, %%mm7, %%mm0, %%mm1, %%mm3, %%mm6)
1343                 "pop %%"REG_BP"                         \n\t"
1344                 "mov "ESP_OFFSET"(%5), %%"REG_b"        \n\t"
1345                 :: "c" (buf0), "d" (abuf0), "S" (ubuf0), "D" (ubuf1), "m" (dest),
1346                    "a" (&c->redDither)
1347             );
1348         } else {
1349             __asm__ volatile(
1350                 "mov %%"REG_b", "ESP_OFFSET"(%5)        \n\t"
1351                 "mov        %4, %%"REG_b"               \n\t"
1352                 "push %%"REG_BP"                        \n\t"
1353                 YSCALEYUV2RGB1b(%%REGBP, %5)
1354                 "pcmpeqd %%mm7, %%mm7                   \n\t"
1355                 WRITEBGR32(%%REGb, 8280(%5), %%REGBP, %%mm2, %%mm4, %%mm5, %%mm7, %%mm0, %%mm1, %%mm3, %%mm6)
1356                 "pop %%"REG_BP"                         \n\t"
1357                 "mov "ESP_OFFSET"(%5), %%"REG_b"        \n\t"
1358                 :: "c" (buf0), "d" (buf1), "S" (ubuf0), "D" (ubuf1), "m" (dest),
1359                    "a" (&c->redDither)
1360             );
1361         }
1362     }
1363 }
1364
1365 static inline void RENAME(yuv2bgr24_1)(SwsContext *c, const uint16_t *buf0,
1366                                        const uint16_t *ubuf0, const uint16_t *ubuf1,
1367                                        const uint16_t *vbuf0, const uint16_t *vbuf1,
1368                                        const uint16_t *abuf0, uint8_t *dest,
1369                                        int dstW, int uvalpha, enum PixelFormat dstFormat,
1370                                        int flags, int y)
1371 {
1372     const uint16_t *buf1= buf0; //FIXME needed for RGB1/BGR1
1373
1374     if (uvalpha < 2048) { // note this is not correct (shifts chrominance by 0.5 pixels) but it is a bit faster
1375         __asm__ volatile(
1376             "mov %%"REG_b", "ESP_OFFSET"(%5)        \n\t"
1377             "mov        %4, %%"REG_b"               \n\t"
1378             "push %%"REG_BP"                        \n\t"
1379             YSCALEYUV2RGB1(%%REGBP, %5)
1380             "pxor    %%mm7, %%mm7                   \n\t"
1381             WRITEBGR24(%%REGb, 8280(%5), %%REGBP)
1382             "pop %%"REG_BP"                         \n\t"
1383             "mov "ESP_OFFSET"(%5), %%"REG_b"        \n\t"
1384             :: "c" (buf0), "d" (buf1), "S" (ubuf0), "D" (ubuf1), "m" (dest),
1385                "a" (&c->redDither)
1386         );
1387     } else {
1388         __asm__ volatile(
1389             "mov %%"REG_b", "ESP_OFFSET"(%5)        \n\t"
1390             "mov        %4, %%"REG_b"               \n\t"
1391             "push %%"REG_BP"                        \n\t"
1392             YSCALEYUV2RGB1b(%%REGBP, %5)
1393             "pxor    %%mm7, %%mm7                   \n\t"
1394             WRITEBGR24(%%REGb, 8280(%5), %%REGBP)
1395             "pop %%"REG_BP"                         \n\t"
1396             "mov "ESP_OFFSET"(%5), %%"REG_b"        \n\t"
1397             :: "c" (buf0), "d" (buf1), "S" (ubuf0), "D" (ubuf1), "m" (dest),
1398                "a" (&c->redDither)
1399         );
1400     }
1401 }
1402
1403 static inline void RENAME(yuv2rgb555_1)(SwsContext *c, const uint16_t *buf0,
1404                                         const uint16_t *ubuf0, const uint16_t *ubuf1,
1405                                         const uint16_t *vbuf0, const uint16_t *vbuf1,
1406                                         const uint16_t *abuf0, uint8_t *dest,
1407                                         int dstW, int uvalpha, enum PixelFormat dstFormat,
1408                                         int flags, int y)
1409 {
1410     const uint16_t *buf1= buf0; //FIXME needed for RGB1/BGR1
1411
1412     if (uvalpha < 2048) { // note this is not correct (shifts chrominance by 0.5 pixels) but it is a bit faster
1413         __asm__ volatile(
1414             "mov %%"REG_b", "ESP_OFFSET"(%5)        \n\t"
1415             "mov        %4, %%"REG_b"               \n\t"
1416             "push %%"REG_BP"                        \n\t"
1417             YSCALEYUV2RGB1(%%REGBP, %5)
1418             "pxor    %%mm7, %%mm7                   \n\t"
1419             /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */
1420 #ifdef DITHER1XBPP
1421             "paddusb "BLUE_DITHER"(%5), %%mm2      \n\t"
1422             "paddusb "GREEN_DITHER"(%5), %%mm4      \n\t"
1423             "paddusb "RED_DITHER"(%5), %%mm5      \n\t"
1424 #endif
1425             WRITERGB15(%%REGb, 8280(%5), %%REGBP)
1426             "pop %%"REG_BP"                         \n\t"
1427             "mov "ESP_OFFSET"(%5), %%"REG_b"        \n\t"
1428             :: "c" (buf0), "d" (buf1), "S" (ubuf0), "D" (ubuf1), "m" (dest),
1429                "a" (&c->redDither)
1430         );
1431     } else {
1432         __asm__ volatile(
1433             "mov %%"REG_b", "ESP_OFFSET"(%5)        \n\t"
1434             "mov        %4, %%"REG_b"               \n\t"
1435             "push %%"REG_BP"                        \n\t"
1436             YSCALEYUV2RGB1b(%%REGBP, %5)
1437             "pxor    %%mm7, %%mm7                   \n\t"
1438             /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */
1439 #ifdef DITHER1XBPP
1440             "paddusb "BLUE_DITHER"(%5), %%mm2      \n\t"
1441             "paddusb "GREEN_DITHER"(%5), %%mm4      \n\t"
1442             "paddusb "RED_DITHER"(%5), %%mm5      \n\t"
1443 #endif
1444             WRITERGB15(%%REGb, 8280(%5), %%REGBP)
1445             "pop %%"REG_BP"                         \n\t"
1446             "mov "ESP_OFFSET"(%5), %%"REG_b"        \n\t"
1447             :: "c" (buf0), "d" (buf1), "S" (ubuf0), "D" (ubuf1), "m" (dest),
1448                "a" (&c->redDither)
1449         );
1450     }
1451 }
1452
1453 static inline void RENAME(yuv2rgb565_1)(SwsContext *c, const uint16_t *buf0,
1454                                         const uint16_t *ubuf0, const uint16_t *ubuf1,
1455                                         const uint16_t *vbuf0, const uint16_t *vbuf1,
1456                                         const uint16_t *abuf0, uint8_t *dest,
1457                                         int dstW, int uvalpha, enum PixelFormat dstFormat,
1458                                         int flags, int y)
1459 {
1460     const uint16_t *buf1= buf0; //FIXME needed for RGB1/BGR1
1461
1462     if (uvalpha < 2048) { // note this is not correct (shifts chrominance by 0.5 pixels) but it is a bit faster
1463         __asm__ volatile(
1464             "mov %%"REG_b", "ESP_OFFSET"(%5)        \n\t"
1465             "mov        %4, %%"REG_b"               \n\t"
1466             "push %%"REG_BP"                        \n\t"
1467             YSCALEYUV2RGB1(%%REGBP, %5)
1468             "pxor    %%mm7, %%mm7                   \n\t"
1469             /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */
1470 #ifdef DITHER1XBPP
1471             "paddusb "BLUE_DITHER"(%5), %%mm2      \n\t"
1472             "paddusb "GREEN_DITHER"(%5), %%mm4      \n\t"
1473             "paddusb "RED_DITHER"(%5), %%mm5      \n\t"
1474 #endif
1475             WRITERGB16(%%REGb, 8280(%5), %%REGBP)
1476             "pop %%"REG_BP"                         \n\t"
1477             "mov "ESP_OFFSET"(%5), %%"REG_b"        \n\t"
1478             :: "c" (buf0), "d" (buf1), "S" (ubuf0), "D" (ubuf1), "m" (dest),
1479                "a" (&c->redDither)
1480         );
1481     } else {
1482         __asm__ volatile(
1483             "mov %%"REG_b", "ESP_OFFSET"(%5)        \n\t"
1484             "mov        %4, %%"REG_b"               \n\t"
1485             "push %%"REG_BP"                        \n\t"
1486             YSCALEYUV2RGB1b(%%REGBP, %5)
1487             "pxor    %%mm7, %%mm7                   \n\t"
1488             /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */
1489 #ifdef DITHER1XBPP
1490             "paddusb "BLUE_DITHER"(%5), %%mm2      \n\t"
1491             "paddusb "GREEN_DITHER"(%5), %%mm4      \n\t"
1492             "paddusb "RED_DITHER"(%5), %%mm5      \n\t"
1493 #endif
1494             WRITERGB16(%%REGb, 8280(%5), %%REGBP)
1495             "pop %%"REG_BP"                         \n\t"
1496             "mov "ESP_OFFSET"(%5), %%"REG_b"        \n\t"
1497             :: "c" (buf0), "d" (buf1), "S" (ubuf0), "D" (ubuf1), "m" (dest),
1498                "a" (&c->redDither)
1499         );
1500     }
1501 }
1502
1503 #define REAL_YSCALEYUV2PACKED1(index, c) \
1504     "xor            "#index", "#index"  \n\t"\
1505     ".p2align              4            \n\t"\
1506     "1:                                 \n\t"\
1507     "movq     (%2, "#index"), %%mm3     \n\t" /* uvbuf0[eax]*/\
1508     "add           "UV_OFFx2"("#c"), "#index"  \n\t" \
1509     "movq     (%2, "#index"), %%mm4     \n\t" /* uvbuf0[eax+2048]*/\
1510     "sub           "UV_OFFx2"("#c"), "#index"  \n\t" \
1511     "psraw                $7, %%mm3     \n\t" \
1512     "psraw                $7, %%mm4     \n\t" \
1513     "movq  (%0, "#index", 2), %%mm1     \n\t" /*buf0[eax]*/\
1514     "movq 8(%0, "#index", 2), %%mm7     \n\t" /*buf0[eax]*/\
1515     "psraw                $7, %%mm1     \n\t" \
1516     "psraw                $7, %%mm7     \n\t" \
1517
1518 #define YSCALEYUV2PACKED1(index, c)  REAL_YSCALEYUV2PACKED1(index, c)
1519
1520 #define REAL_YSCALEYUV2PACKED1b(index, c) \
1521     "xor "#index", "#index"             \n\t"\
1522     ".p2align              4            \n\t"\
1523     "1:                                 \n\t"\
1524     "movq     (%2, "#index"), %%mm2     \n\t" /* uvbuf0[eax]*/\
1525     "movq     (%3, "#index"), %%mm3     \n\t" /* uvbuf1[eax]*/\
1526     "add           "UV_OFFx2"("#c"), "#index"  \n\t" \
1527     "movq     (%2, "#index"), %%mm5     \n\t" /* uvbuf0[eax+2048]*/\
1528     "movq     (%3, "#index"), %%mm4     \n\t" /* uvbuf1[eax+2048]*/\
1529     "sub           "UV_OFFx2"("#c"), "#index"  \n\t" \
1530     "paddw             %%mm2, %%mm3     \n\t" /* uvbuf0[eax] + uvbuf1[eax]*/\
1531     "paddw             %%mm5, %%mm4     \n\t" /* uvbuf0[eax+2048] + uvbuf1[eax+2048]*/\
1532     "psrlw                $8, %%mm3     \n\t" \
1533     "psrlw                $8, %%mm4     \n\t" \
1534     "movq  (%0, "#index", 2), %%mm1     \n\t" /*buf0[eax]*/\
1535     "movq 8(%0, "#index", 2), %%mm7     \n\t" /*buf0[eax]*/\
1536     "psraw                $7, %%mm1     \n\t" \
1537     "psraw                $7, %%mm7     \n\t"
1538 #define YSCALEYUV2PACKED1b(index, c)  REAL_YSCALEYUV2PACKED1b(index, c)
1539
1540 static inline void RENAME(yuv2yuyv422_1)(SwsContext *c, const uint16_t *buf0,
1541                                          const uint16_t *ubuf0, const uint16_t *ubuf1,
1542                                          const uint16_t *vbuf0, const uint16_t *vbuf1,
1543                                          const uint16_t *abuf0, uint8_t *dest,
1544                                          int dstW, int uvalpha, enum PixelFormat dstFormat,
1545                                          int flags, int y)
1546 {
1547     const uint16_t *buf1= buf0; //FIXME needed for RGB1/BGR1
1548
1549     if (uvalpha < 2048) { // note this is not correct (shifts chrominance by 0.5 pixels) but it is a bit faster
1550         __asm__ volatile(
1551             "mov %%"REG_b", "ESP_OFFSET"(%5)        \n\t"
1552             "mov        %4, %%"REG_b"               \n\t"
1553             "push %%"REG_BP"                        \n\t"
1554             YSCALEYUV2PACKED1(%%REGBP, %5)
1555             WRITEYUY2(%%REGb, 8280(%5), %%REGBP)
1556             "pop %%"REG_BP"                         \n\t"
1557             "mov "ESP_OFFSET"(%5), %%"REG_b"        \n\t"
1558             :: "c" (buf0), "d" (buf1), "S" (ubuf0), "D" (ubuf1), "m" (dest),
1559                "a" (&c->redDither)
1560         );
1561     } else {
1562         __asm__ volatile(
1563             "mov %%"REG_b", "ESP_OFFSET"(%5)        \n\t"
1564             "mov        %4, %%"REG_b"               \n\t"
1565             "push %%"REG_BP"                        \n\t"
1566             YSCALEYUV2PACKED1b(%%REGBP, %5)
1567             WRITEYUY2(%%REGb, 8280(%5), %%REGBP)
1568             "pop %%"REG_BP"                         \n\t"
1569             "mov "ESP_OFFSET"(%5), %%"REG_b"        \n\t"
1570             :: "c" (buf0), "d" (buf1), "S" (ubuf0), "D" (ubuf1), "m" (dest),
1571                "a" (&c->redDither)
1572         );
1573     }
1574 }
1575
1576 #if !COMPILE_TEMPLATE_MMX2
1577 //FIXME yuy2* can read up to 7 samples too much
1578
1579 static inline void RENAME(yuy2ToY)(uint8_t *dst, const uint8_t *src, int width, uint32_t *unused)
1580 {
1581     __asm__ volatile(
1582         "movq "MANGLE(bm01010101)", %%mm2           \n\t"
1583         "mov                    %0, %%"REG_a"       \n\t"
1584         "1:                                         \n\t"
1585         "movq    (%1, %%"REG_a",2), %%mm0           \n\t"
1586         "movq   8(%1, %%"REG_a",2), %%mm1           \n\t"
1587         "pand                %%mm2, %%mm0           \n\t"
1588         "pand                %%mm2, %%mm1           \n\t"
1589         "packuswb            %%mm1, %%mm0           \n\t"
1590         "movq                %%mm0, (%2, %%"REG_a") \n\t"
1591         "add                    $8, %%"REG_a"       \n\t"
1592         " js                    1b                  \n\t"
1593         : : "g" ((x86_reg)-width), "r" (src+width*2), "r" (dst+width)
1594         : "%"REG_a
1595     );
1596 }
1597
1598 static inline void RENAME(yuy2ToUV)(uint8_t *dstU, uint8_t *dstV, const uint8_t *src1, const uint8_t *src2, int width, uint32_t *unused)
1599 {
1600     __asm__ volatile(
1601         "movq "MANGLE(bm01010101)", %%mm4           \n\t"
1602         "mov                    %0, %%"REG_a"       \n\t"
1603         "1:                                         \n\t"
1604         "movq    (%1, %%"REG_a",4), %%mm0           \n\t"
1605         "movq   8(%1, %%"REG_a",4), %%mm1           \n\t"
1606         "psrlw                  $8, %%mm0           \n\t"
1607         "psrlw                  $8, %%mm1           \n\t"
1608         "packuswb            %%mm1, %%mm0           \n\t"
1609         "movq                %%mm0, %%mm1           \n\t"
1610         "psrlw                  $8, %%mm0           \n\t"
1611         "pand                %%mm4, %%mm1           \n\t"
1612         "packuswb            %%mm0, %%mm0           \n\t"
1613         "packuswb            %%mm1, %%mm1           \n\t"
1614         "movd                %%mm0, (%3, %%"REG_a") \n\t"
1615         "movd                %%mm1, (%2, %%"REG_a") \n\t"
1616         "add                    $4, %%"REG_a"       \n\t"
1617         " js                    1b                  \n\t"
1618         : : "g" ((x86_reg)-width), "r" (src1+width*4), "r" (dstU+width), "r" (dstV+width)
1619         : "%"REG_a
1620     );
1621     assert(src1 == src2);
1622 }
1623
1624 static inline void RENAME(LEToUV)(uint8_t *dstU, uint8_t *dstV, const uint8_t *src1, const uint8_t *src2, int width, uint32_t *unused)
1625 {
1626     __asm__ volatile(
1627         "mov                    %0, %%"REG_a"       \n\t"
1628         "1:                                         \n\t"
1629         "movq    (%1, %%"REG_a",2), %%mm0           \n\t"
1630         "movq   8(%1, %%"REG_a",2), %%mm1           \n\t"
1631         "movq    (%2, %%"REG_a",2), %%mm2           \n\t"
1632         "movq   8(%2, %%"REG_a",2), %%mm3           \n\t"
1633         "psrlw                  $8, %%mm0           \n\t"
1634         "psrlw                  $8, %%mm1           \n\t"
1635         "psrlw                  $8, %%mm2           \n\t"
1636         "psrlw                  $8, %%mm3           \n\t"
1637         "packuswb            %%mm1, %%mm0           \n\t"
1638         "packuswb            %%mm3, %%mm2           \n\t"
1639         "movq                %%mm0, (%3, %%"REG_a") \n\t"
1640         "movq                %%mm2, (%4, %%"REG_a") \n\t"
1641         "add                    $8, %%"REG_a"       \n\t"
1642         " js                    1b                  \n\t"
1643         : : "g" ((x86_reg)-width), "r" (src1+width*2), "r" (src2+width*2), "r" (dstU+width), "r" (dstV+width)
1644         : "%"REG_a
1645     );
1646 }
1647
1648 /* This is almost identical to the previous, end exists only because
1649  * yuy2ToY/UV)(dst, src+1, ...) would have 100% unaligned accesses. */
1650 static inline void RENAME(uyvyToY)(uint8_t *dst, const uint8_t *src, int width, uint32_t *unused)
1651 {
1652     __asm__ volatile(
1653         "mov                  %0, %%"REG_a"         \n\t"
1654         "1:                                         \n\t"
1655         "movq  (%1, %%"REG_a",2), %%mm0             \n\t"
1656         "movq 8(%1, %%"REG_a",2), %%mm1             \n\t"
1657         "psrlw                $8, %%mm0             \n\t"
1658         "psrlw                $8, %%mm1             \n\t"
1659         "packuswb          %%mm1, %%mm0             \n\t"
1660         "movq              %%mm0, (%2, %%"REG_a")   \n\t"
1661         "add                  $8, %%"REG_a"         \n\t"
1662         " js                  1b                    \n\t"
1663         : : "g" ((x86_reg)-width), "r" (src+width*2), "r" (dst+width)
1664         : "%"REG_a
1665     );
1666 }
1667
1668 static inline void RENAME(uyvyToUV)(uint8_t *dstU, uint8_t *dstV, const uint8_t *src1, const uint8_t *src2, int width, uint32_t *unused)
1669 {
1670     __asm__ volatile(
1671         "movq "MANGLE(bm01010101)", %%mm4           \n\t"
1672         "mov                    %0, %%"REG_a"       \n\t"
1673         "1:                                         \n\t"
1674         "movq    (%1, %%"REG_a",4), %%mm0           \n\t"
1675         "movq   8(%1, %%"REG_a",4), %%mm1           \n\t"
1676         "pand                %%mm4, %%mm0           \n\t"
1677         "pand                %%mm4, %%mm1           \n\t"
1678         "packuswb            %%mm1, %%mm0           \n\t"
1679         "movq                %%mm0, %%mm1           \n\t"
1680         "psrlw                  $8, %%mm0           \n\t"
1681         "pand                %%mm4, %%mm1           \n\t"
1682         "packuswb            %%mm0, %%mm0           \n\t"
1683         "packuswb            %%mm1, %%mm1           \n\t"
1684         "movd                %%mm0, (%3, %%"REG_a") \n\t"
1685         "movd                %%mm1, (%2, %%"REG_a") \n\t"
1686         "add                    $4, %%"REG_a"       \n\t"
1687         " js                    1b                  \n\t"
1688         : : "g" ((x86_reg)-width), "r" (src1+width*4), "r" (dstU+width), "r" (dstV+width)
1689         : "%"REG_a
1690     );
1691     assert(src1 == src2);
1692 }
1693
1694 static inline void RENAME(BEToUV)(uint8_t *dstU, uint8_t *dstV, const uint8_t *src1, const uint8_t *src2, int width, uint32_t *unused)
1695 {
1696     __asm__ volatile(
1697         "movq "MANGLE(bm01010101)", %%mm4           \n\t"
1698         "mov                    %0, %%"REG_a"       \n\t"
1699         "1:                                         \n\t"
1700         "movq    (%1, %%"REG_a",2), %%mm0           \n\t"
1701         "movq   8(%1, %%"REG_a",2), %%mm1           \n\t"
1702         "movq    (%2, %%"REG_a",2), %%mm2           \n\t"
1703         "movq   8(%2, %%"REG_a",2), %%mm3           \n\t"
1704         "pand                %%mm4, %%mm0           \n\t"
1705         "pand                %%mm4, %%mm1           \n\t"
1706         "pand                %%mm4, %%mm2           \n\t"
1707         "pand                %%mm4, %%mm3           \n\t"
1708         "packuswb            %%mm1, %%mm0           \n\t"
1709         "packuswb            %%mm3, %%mm2           \n\t"
1710         "movq                %%mm0, (%3, %%"REG_a") \n\t"
1711         "movq                %%mm2, (%4, %%"REG_a") \n\t"
1712         "add                    $8, %%"REG_a"       \n\t"
1713         " js                    1b                  \n\t"
1714         : : "g" ((x86_reg)-width), "r" (src1+width*2), "r" (src2+width*2), "r" (dstU+width), "r" (dstV+width)
1715         : "%"REG_a
1716     );
1717 }
1718
1719 static inline void RENAME(nvXXtoUV)(uint8_t *dst1, uint8_t *dst2,
1720                                     const uint8_t *src, int width)
1721 {
1722     __asm__ volatile(
1723         "movq "MANGLE(bm01010101)", %%mm4           \n\t"
1724         "mov                    %0, %%"REG_a"       \n\t"
1725         "1:                                         \n\t"
1726         "movq    (%1, %%"REG_a",2), %%mm0           \n\t"
1727         "movq   8(%1, %%"REG_a",2), %%mm1           \n\t"
1728         "movq                %%mm0, %%mm2           \n\t"
1729         "movq                %%mm1, %%mm3           \n\t"
1730         "pand                %%mm4, %%mm0           \n\t"
1731         "pand                %%mm4, %%mm1           \n\t"
1732         "psrlw                  $8, %%mm2           \n\t"
1733         "psrlw                  $8, %%mm3           \n\t"
1734         "packuswb            %%mm1, %%mm0           \n\t"
1735         "packuswb            %%mm3, %%mm2           \n\t"
1736         "movq                %%mm0, (%2, %%"REG_a") \n\t"
1737         "movq                %%mm2, (%3, %%"REG_a") \n\t"
1738         "add                    $8, %%"REG_a"       \n\t"
1739         " js                    1b                  \n\t"
1740         : : "g" ((x86_reg)-width), "r" (src+width*2), "r" (dst1+width), "r" (dst2+width)
1741         : "%"REG_a
1742     );
1743 }
1744
1745 static inline void RENAME(nv12ToUV)(uint8_t *dstU, uint8_t *dstV,
1746                                     const uint8_t *src1, const uint8_t *src2,
1747                                     int width, uint32_t *unused)
1748 {
1749     RENAME(nvXXtoUV)(dstU, dstV, src1, width);
1750 }
1751
1752 static inline void RENAME(nv21ToUV)(uint8_t *dstU, uint8_t *dstV,
1753                                     const uint8_t *src1, const uint8_t *src2,
1754                                     int width, uint32_t *unused)
1755 {
1756     RENAME(nvXXtoUV)(dstV, dstU, src1, width);
1757 }
1758 #endif /* !COMPILE_TEMPLATE_MMX2 */
1759
1760 static inline void RENAME(bgr24ToY_mmx)(uint8_t *dst, const uint8_t *src, int width, enum PixelFormat srcFormat)
1761 {
1762
1763     if(srcFormat == PIX_FMT_BGR24) {
1764         __asm__ volatile(
1765             "movq  "MANGLE(ff_bgr24toY1Coeff)", %%mm5       \n\t"
1766             "movq  "MANGLE(ff_bgr24toY2Coeff)", %%mm6       \n\t"
1767             :
1768         );
1769     } else {
1770         __asm__ volatile(
1771             "movq  "MANGLE(ff_rgb24toY1Coeff)", %%mm5       \n\t"
1772             "movq  "MANGLE(ff_rgb24toY2Coeff)", %%mm6       \n\t"
1773             :
1774         );
1775     }
1776
1777     __asm__ volatile(
1778         "movq  "MANGLE(ff_bgr24toYOffset)", %%mm4   \n\t"
1779         "mov                        %2, %%"REG_a"   \n\t"
1780         "pxor                    %%mm7, %%mm7       \n\t"
1781         "1:                                         \n\t"
1782         PREFETCH"               64(%0)              \n\t"
1783         "movd                     (%0), %%mm0       \n\t"
1784         "movd                    2(%0), %%mm1       \n\t"
1785         "movd                    6(%0), %%mm2       \n\t"
1786         "movd                    8(%0), %%mm3       \n\t"
1787         "add                       $12, %0          \n\t"
1788         "punpcklbw               %%mm7, %%mm0       \n\t"
1789         "punpcklbw               %%mm7, %%mm1       \n\t"
1790         "punpcklbw               %%mm7, %%mm2       \n\t"
1791         "punpcklbw               %%mm7, %%mm3       \n\t"
1792         "pmaddwd                 %%mm5, %%mm0       \n\t"
1793         "pmaddwd                 %%mm6, %%mm1       \n\t"
1794         "pmaddwd                 %%mm5, %%mm2       \n\t"
1795         "pmaddwd                 %%mm6, %%mm3       \n\t"
1796         "paddd                   %%mm1, %%mm0       \n\t"
1797         "paddd                   %%mm3, %%mm2       \n\t"
1798         "paddd                   %%mm4, %%mm0       \n\t"
1799         "paddd                   %%mm4, %%mm2       \n\t"
1800         "psrad                     $15, %%mm0       \n\t"
1801         "psrad                     $15, %%mm2       \n\t"
1802         "packssdw                %%mm2, %%mm0       \n\t"
1803         "packuswb                %%mm0, %%mm0       \n\t"
1804         "movd                %%mm0, (%1, %%"REG_a") \n\t"
1805         "add                        $4, %%"REG_a"   \n\t"
1806         " js                        1b              \n\t"
1807     : "+r" (src)
1808     : "r" (dst+width), "g" ((x86_reg)-width)
1809     : "%"REG_a
1810     );
1811 }
1812
1813 static inline void RENAME(bgr24ToUV_mmx)(uint8_t *dstU, uint8_t *dstV, const uint8_t *src, int width, enum PixelFormat srcFormat)
1814 {
1815     __asm__ volatile(
1816         "movq                    24(%4), %%mm6       \n\t"
1817         "mov                        %3, %%"REG_a"   \n\t"
1818         "pxor                    %%mm7, %%mm7       \n\t"
1819         "1:                                         \n\t"
1820         PREFETCH"               64(%0)              \n\t"
1821         "movd                     (%0), %%mm0       \n\t"
1822         "movd                    2(%0), %%mm1       \n\t"
1823         "punpcklbw               %%mm7, %%mm0       \n\t"
1824         "punpcklbw               %%mm7, %%mm1       \n\t"
1825         "movq                    %%mm0, %%mm2       \n\t"
1826         "movq                    %%mm1, %%mm3       \n\t"
1827         "pmaddwd                  (%4), %%mm0       \n\t"
1828         "pmaddwd                 8(%4), %%mm1       \n\t"
1829         "pmaddwd                16(%4), %%mm2       \n\t"
1830         "pmaddwd                 %%mm6, %%mm3       \n\t"
1831         "paddd                   %%mm1, %%mm0       \n\t"
1832         "paddd                   %%mm3, %%mm2       \n\t"
1833
1834         "movd                    6(%0), %%mm1       \n\t"
1835         "movd                    8(%0), %%mm3       \n\t"
1836         "add                       $12, %0          \n\t"
1837         "punpcklbw               %%mm7, %%mm1       \n\t"
1838         "punpcklbw               %%mm7, %%mm3       \n\t"
1839         "movq                    %%mm1, %%mm4       \n\t"
1840         "movq                    %%mm3, %%mm5       \n\t"
1841         "pmaddwd                  (%4), %%mm1       \n\t"
1842         "pmaddwd                 8(%4), %%mm3       \n\t"
1843         "pmaddwd                16(%4), %%mm4       \n\t"
1844         "pmaddwd                 %%mm6, %%mm5       \n\t"
1845         "paddd                   %%mm3, %%mm1       \n\t"
1846         "paddd                   %%mm5, %%mm4       \n\t"
1847
1848         "movq "MANGLE(ff_bgr24toUVOffset)", %%mm3       \n\t"
1849         "paddd                   %%mm3, %%mm0       \n\t"
1850         "paddd                   %%mm3, %%mm2       \n\t"
1851         "paddd                   %%mm3, %%mm1       \n\t"
1852         "paddd                   %%mm3, %%mm4       \n\t"
1853         "psrad                     $15, %%mm0       \n\t"
1854         "psrad                     $15, %%mm2       \n\t"
1855         "psrad                     $15, %%mm1       \n\t"
1856         "psrad                     $15, %%mm4       \n\t"
1857         "packssdw                %%mm1, %%mm0       \n\t"
1858         "packssdw                %%mm4, %%mm2       \n\t"
1859         "packuswb                %%mm0, %%mm0       \n\t"
1860         "packuswb                %%mm2, %%mm2       \n\t"
1861         "movd                %%mm0, (%1, %%"REG_a") \n\t"
1862         "movd                %%mm2, (%2, %%"REG_a") \n\t"
1863         "add                        $4, %%"REG_a"   \n\t"
1864         " js                        1b              \n\t"
1865     : "+r" (src)
1866     : "r" (dstU+width), "r" (dstV+width), "g" ((x86_reg)-width), "r"(ff_bgr24toUV[srcFormat == PIX_FMT_RGB24])
1867     : "%"REG_a
1868     );
1869 }
1870
1871 static inline void RENAME(bgr24ToY)(uint8_t *dst, const uint8_t *src, int width, uint32_t *unused)
1872 {
1873     RENAME(bgr24ToY_mmx)(dst, src, width, PIX_FMT_BGR24);
1874 }
1875
1876 static inline void RENAME(bgr24ToUV)(uint8_t *dstU, uint8_t *dstV, const uint8_t *src1, const uint8_t *src2, int width, uint32_t *unused)
1877 {
1878     RENAME(bgr24ToUV_mmx)(dstU, dstV, src1, width, PIX_FMT_BGR24);
1879     assert(src1 == src2);
1880 }
1881
1882 static inline void RENAME(rgb24ToY)(uint8_t *dst, const uint8_t *src, int width, uint32_t *unused)
1883 {
1884     RENAME(bgr24ToY_mmx)(dst, src, width, PIX_FMT_RGB24);
1885 }
1886
1887 static inline void RENAME(rgb24ToUV)(uint8_t *dstU, uint8_t *dstV, const uint8_t *src1, const uint8_t *src2, int width, uint32_t *unused)
1888 {
1889     assert(src1==src2);
1890     RENAME(bgr24ToUV_mmx)(dstU, dstV, src1, width, PIX_FMT_RGB24);
1891 }
1892
1893 #if !COMPILE_TEMPLATE_MMX2
1894 // bilinear / bicubic scaling
1895 static inline void RENAME(hScale)(int16_t *dst, int dstW, const uint8_t *src, int srcW, int xInc,
1896                                   const int16_t *filter, const int16_t *filterPos, int filterSize)
1897 {
1898     assert(filterSize % 4 == 0 && filterSize>0);
1899     if (filterSize==4) { // Always true for upscaling, sometimes for down, too.
1900         x86_reg counter= -2*dstW;
1901         filter-= counter*2;
1902         filterPos-= counter/2;
1903         dst-= counter/2;
1904         __asm__ volatile(
1905 #if defined(PIC)
1906             "push            %%"REG_b"              \n\t"
1907 #endif
1908             "pxor                %%mm7, %%mm7       \n\t"
1909             "push           %%"REG_BP"              \n\t" // we use 7 regs here ...
1910             "mov             %%"REG_a", %%"REG_BP"  \n\t"
1911             ".p2align                4              \n\t"
1912             "1:                                     \n\t"
1913             "movzwl   (%2, %%"REG_BP"), %%eax       \n\t"
1914             "movzwl  2(%2, %%"REG_BP"), %%ebx       \n\t"
1915             "movq  (%1, %%"REG_BP", 4), %%mm1       \n\t"
1916             "movq 8(%1, %%"REG_BP", 4), %%mm3       \n\t"
1917             "movd      (%3, %%"REG_a"), %%mm0       \n\t"
1918             "movd      (%3, %%"REG_b"), %%mm2       \n\t"
1919             "punpcklbw           %%mm7, %%mm0       \n\t"
1920             "punpcklbw           %%mm7, %%mm2       \n\t"
1921             "pmaddwd             %%mm1, %%mm0       \n\t"
1922             "pmaddwd             %%mm2, %%mm3       \n\t"
1923             "movq                %%mm0, %%mm4       \n\t"
1924             "punpckldq           %%mm3, %%mm0       \n\t"
1925             "punpckhdq           %%mm3, %%mm4       \n\t"
1926             "paddd               %%mm4, %%mm0       \n\t"
1927             "psrad                  $7, %%mm0       \n\t"
1928             "packssdw            %%mm0, %%mm0       \n\t"
1929             "movd                %%mm0, (%4, %%"REG_BP")    \n\t"
1930             "add                    $4, %%"REG_BP"  \n\t"
1931             " jnc                   1b              \n\t"
1932
1933             "pop            %%"REG_BP"              \n\t"
1934 #if defined(PIC)
1935             "pop             %%"REG_b"              \n\t"
1936 #endif
1937             : "+a" (counter)
1938             : "c" (filter), "d" (filterPos), "S" (src), "D" (dst)
1939 #if !defined(PIC)
1940             : "%"REG_b
1941 #endif
1942         );
1943     } else if (filterSize==8) {
1944         x86_reg counter= -2*dstW;
1945         filter-= counter*4;
1946         filterPos-= counter/2;
1947         dst-= counter/2;
1948         __asm__ volatile(
1949 #if defined(PIC)
1950             "push             %%"REG_b"             \n\t"
1951 #endif
1952             "pxor                 %%mm7, %%mm7      \n\t"
1953             "push            %%"REG_BP"             \n\t" // we use 7 regs here ...
1954             "mov              %%"REG_a", %%"REG_BP" \n\t"
1955             ".p2align                 4             \n\t"
1956             "1:                                     \n\t"
1957             "movzwl    (%2, %%"REG_BP"), %%eax      \n\t"
1958             "movzwl   2(%2, %%"REG_BP"), %%ebx      \n\t"
1959             "movq   (%1, %%"REG_BP", 8), %%mm1      \n\t"
1960             "movq 16(%1, %%"REG_BP", 8), %%mm3      \n\t"
1961             "movd       (%3, %%"REG_a"), %%mm0      \n\t"
1962             "movd       (%3, %%"REG_b"), %%mm2      \n\t"
1963             "punpcklbw            %%mm7, %%mm0      \n\t"
1964             "punpcklbw            %%mm7, %%mm2      \n\t"
1965             "pmaddwd              %%mm1, %%mm0      \n\t"
1966             "pmaddwd              %%mm2, %%mm3      \n\t"
1967
1968             "movq  8(%1, %%"REG_BP", 8), %%mm1      \n\t"
1969             "movq 24(%1, %%"REG_BP", 8), %%mm5      \n\t"
1970             "movd      4(%3, %%"REG_a"), %%mm4      \n\t"
1971             "movd      4(%3, %%"REG_b"), %%mm2      \n\t"
1972             "punpcklbw            %%mm7, %%mm4      \n\t"
1973             "punpcklbw            %%mm7, %%mm2      \n\t"
1974             "pmaddwd              %%mm1, %%mm4      \n\t"
1975             "pmaddwd              %%mm2, %%mm5      \n\t"
1976             "paddd                %%mm4, %%mm0      \n\t"
1977             "paddd                %%mm5, %%mm3      \n\t"
1978             "movq                 %%mm0, %%mm4      \n\t"
1979             "punpckldq            %%mm3, %%mm0      \n\t"
1980             "punpckhdq            %%mm3, %%mm4      \n\t"
1981             "paddd                %%mm4, %%mm0      \n\t"
1982             "psrad                   $7, %%mm0      \n\t"
1983             "packssdw             %%mm0, %%mm0      \n\t"
1984             "movd                 %%mm0, (%4, %%"REG_BP")   \n\t"
1985             "add                     $4, %%"REG_BP" \n\t"
1986             " jnc                    1b             \n\t"
1987
1988             "pop             %%"REG_BP"             \n\t"
1989 #if defined(PIC)
1990             "pop              %%"REG_b"             \n\t"
1991 #endif
1992             : "+a" (counter)
1993             : "c" (filter), "d" (filterPos), "S" (src), "D" (dst)
1994 #if !defined(PIC)
1995             : "%"REG_b
1996 #endif
1997         );
1998     } else {
1999         const uint8_t *offset = src+filterSize;
2000         x86_reg counter= -2*dstW;
2001         //filter-= counter*filterSize/2;
2002         filterPos-= counter/2;
2003         dst-= counter/2;
2004         __asm__ volatile(
2005             "pxor                  %%mm7, %%mm7     \n\t"
2006             ".p2align                  4            \n\t"
2007             "1:                                     \n\t"
2008             "mov                      %2, %%"REG_c" \n\t"
2009             "movzwl      (%%"REG_c", %0), %%eax     \n\t"
2010             "movzwl     2(%%"REG_c", %0), %%edx     \n\t"
2011             "mov                      %5, %%"REG_c" \n\t"
2012             "pxor                  %%mm4, %%mm4     \n\t"
2013             "pxor                  %%mm5, %%mm5     \n\t"
2014             "2:                                     \n\t"
2015             "movq                   (%1), %%mm1     \n\t"
2016             "movq               (%1, %6), %%mm3     \n\t"
2017             "movd (%%"REG_c", %%"REG_a"), %%mm0     \n\t"
2018             "movd (%%"REG_c", %%"REG_d"), %%mm2     \n\t"
2019             "punpcklbw             %%mm7, %%mm0     \n\t"
2020             "punpcklbw             %%mm7, %%mm2     \n\t"
2021             "pmaddwd               %%mm1, %%mm0     \n\t"
2022             "pmaddwd               %%mm2, %%mm3     \n\t"
2023             "paddd                 %%mm3, %%mm5     \n\t"
2024             "paddd                 %%mm0, %%mm4     \n\t"
2025             "add                      $8, %1        \n\t"
2026             "add                      $4, %%"REG_c" \n\t"
2027             "cmp                      %4, %%"REG_c" \n\t"
2028             " jb                      2b            \n\t"
2029             "add                      %6, %1        \n\t"
2030             "movq                  %%mm4, %%mm0     \n\t"
2031             "punpckldq             %%mm5, %%mm4     \n\t"
2032             "punpckhdq             %%mm5, %%mm0     \n\t"
2033             "paddd                 %%mm0, %%mm4     \n\t"
2034             "psrad                    $7, %%mm4     \n\t"
2035             "packssdw              %%mm4, %%mm4     \n\t"
2036             "mov                      %3, %%"REG_a" \n\t"
2037             "movd                  %%mm4, (%%"REG_a", %0)   \n\t"
2038             "add                      $4, %0        \n\t"
2039             " jnc                     1b            \n\t"
2040
2041             : "+r" (counter), "+r" (filter)
2042             : "m" (filterPos), "m" (dst), "m"(offset),
2043             "m" (src), "r" ((x86_reg)filterSize*2)
2044             : "%"REG_a, "%"REG_c, "%"REG_d
2045         );
2046     }
2047 }
2048 #endif /* !COMPILE_TEMPLATE_MMX2 */
2049
2050 #if COMPILE_TEMPLATE_MMX2
2051 static inline void RENAME(hyscale_fast)(SwsContext *c, int16_t *dst,
2052                                         int dstWidth, const uint8_t *src, int srcW,
2053                                         int xInc)
2054 {
2055     int32_t *filterPos = c->hLumFilterPos;
2056     int16_t *filter    = c->hLumFilter;
2057     void    *mmx2FilterCode= c->lumMmx2FilterCode;
2058     int i;
2059 #if defined(PIC)
2060     DECLARE_ALIGNED(8, uint64_t, ebxsave);
2061 #endif
2062
2063     __asm__ volatile(
2064 #if defined(PIC)
2065         "mov               %%"REG_b", %5        \n\t"
2066 #endif
2067         "pxor                  %%mm7, %%mm7     \n\t"
2068         "mov                      %0, %%"REG_c" \n\t"
2069         "mov                      %1, %%"REG_D" \n\t"
2070         "mov                      %2, %%"REG_d" \n\t"
2071         "mov                      %3, %%"REG_b" \n\t"
2072         "xor               %%"REG_a", %%"REG_a" \n\t" // i
2073         PREFETCH"        (%%"REG_c")            \n\t"
2074         PREFETCH"      32(%%"REG_c")            \n\t"
2075         PREFETCH"      64(%%"REG_c")            \n\t"
2076
2077 #if ARCH_X86_64
2078 #define CALL_MMX2_FILTER_CODE \
2079         "movl            (%%"REG_b"), %%esi     \n\t"\
2080         "call                    *%4            \n\t"\
2081         "movl (%%"REG_b", %%"REG_a"), %%esi     \n\t"\
2082         "add               %%"REG_S", %%"REG_c" \n\t"\
2083         "add               %%"REG_a", %%"REG_D" \n\t"\
2084         "xor               %%"REG_a", %%"REG_a" \n\t"\
2085
2086 #else
2087 #define CALL_MMX2_FILTER_CODE \
2088         "movl (%%"REG_b"), %%esi        \n\t"\
2089         "call         *%4                       \n\t"\
2090         "addl (%%"REG_b", %%"REG_a"), %%"REG_c" \n\t"\
2091         "add               %%"REG_a", %%"REG_D" \n\t"\
2092         "xor               %%"REG_a", %%"REG_a" \n\t"\
2093
2094 #endif /* ARCH_X86_64 */
2095
2096         CALL_MMX2_FILTER_CODE
2097         CALL_MMX2_FILTER_CODE
2098         CALL_MMX2_FILTER_CODE
2099         CALL_MMX2_FILTER_CODE
2100         CALL_MMX2_FILTER_CODE
2101         CALL_MMX2_FILTER_CODE
2102         CALL_MMX2_FILTER_CODE
2103         CALL_MMX2_FILTER_CODE
2104
2105 #if defined(PIC)
2106         "mov                      %5, %%"REG_b" \n\t"
2107 #endif
2108         :: "m" (src), "m" (dst), "m" (filter), "m" (filterPos),
2109            "m" (mmx2FilterCode)
2110 #if defined(PIC)
2111           ,"m" (ebxsave)
2112 #endif
2113         : "%"REG_a, "%"REG_c, "%"REG_d, "%"REG_S, "%"REG_D
2114 #if !defined(PIC)
2115          ,"%"REG_b
2116 #endif
2117     );
2118
2119     for (i=dstWidth-1; (i*xInc)>>16 >=srcW-1; i--)
2120         dst[i] = src[srcW-1]*128;
2121 }
2122
2123 static inline void RENAME(hcscale_fast)(SwsContext *c, int16_t *dst1, int16_t *dst2,
2124                                         int dstWidth, const uint8_t *src1,
2125                                         const uint8_t *src2, int srcW, int xInc)
2126 {
2127     int32_t *filterPos = c->hChrFilterPos;
2128     int16_t *filter    = c->hChrFilter;
2129     void    *mmx2FilterCode= c->chrMmx2FilterCode;
2130     int i;
2131 #if defined(PIC)
2132     DECLARE_ALIGNED(8, uint64_t, ebxsave);
2133 #endif
2134
2135     __asm__ volatile(
2136 #if defined(PIC)
2137         "mov          %%"REG_b", %7         \n\t"
2138 #endif
2139         "pxor             %%mm7, %%mm7      \n\t"
2140         "mov                 %0, %%"REG_c"  \n\t"
2141         "mov                 %1, %%"REG_D"  \n\t"
2142         "mov                 %2, %%"REG_d"  \n\t"
2143         "mov                 %3, %%"REG_b"  \n\t"
2144         "xor          %%"REG_a", %%"REG_a"  \n\t" // i
2145         PREFETCH"   (%%"REG_c")             \n\t"
2146         PREFETCH" 32(%%"REG_c")             \n\t"
2147         PREFETCH" 64(%%"REG_c")             \n\t"
2148
2149         CALL_MMX2_FILTER_CODE
2150         CALL_MMX2_FILTER_CODE
2151         CALL_MMX2_FILTER_CODE
2152         CALL_MMX2_FILTER_CODE
2153         "xor          %%"REG_a", %%"REG_a"  \n\t" // i
2154         "mov                 %5, %%"REG_c"  \n\t" // src
2155         "mov                 %6, %%"REG_D"  \n\t" // buf2
2156         PREFETCH"   (%%"REG_c")             \n\t"
2157         PREFETCH" 32(%%"REG_c")             \n\t"
2158         PREFETCH" 64(%%"REG_c")             \n\t"
2159
2160         CALL_MMX2_FILTER_CODE
2161         CALL_MMX2_FILTER_CODE
2162         CALL_MMX2_FILTER_CODE
2163         CALL_MMX2_FILTER_CODE
2164
2165 #if defined(PIC)
2166         "mov %7, %%"REG_b"    \n\t"
2167 #endif
2168         :: "m" (src1), "m" (dst1), "m" (filter), "m" (filterPos),
2169            "m" (mmx2FilterCode), "m" (src2), "m"(dst2)
2170 #if defined(PIC)
2171           ,"m" (ebxsave)
2172 #endif
2173         : "%"REG_a, "%"REG_c, "%"REG_d, "%"REG_S, "%"REG_D
2174 #if !defined(PIC)
2175          ,"%"REG_b
2176 #endif
2177     );
2178
2179     for (i=dstWidth-1; (i*xInc)>>16 >=srcW-1; i--) {
2180         dst1[i] = src1[srcW-1]*128;
2181         dst2[i] = src2[srcW-1]*128;
2182     }
2183 }
2184 #endif /* COMPILE_TEMPLATE_MMX2 */
2185
2186 static void RENAME(sws_init_swScale)(SwsContext *c)
2187 {
2188     enum PixelFormat srcFormat = c->srcFormat,
2189                      dstFormat = c->dstFormat;
2190
2191     if (!is16BPS(dstFormat) && !is9_OR_10BPS(dstFormat)) {
2192         if (!(c->flags & SWS_BITEXACT)) {
2193             if (c->flags & SWS_ACCURATE_RND) {
2194                 c->yuv2yuv1 = RENAME(yuv2yuv1_ar    );
2195                 c->yuv2yuvX = RENAME(yuv2yuvX_ar    );
2196                 if (!(c->flags & SWS_FULL_CHR_H_INT)) {
2197                     switch (c->dstFormat) {
2198                     case PIX_FMT_RGB32:   c->yuv2packedX = RENAME(yuv2rgb32_X_ar);   break;
2199                     case PIX_FMT_BGR24:   c->yuv2packedX = RENAME(yuv2bgr24_X_ar);   break;
2200                     case PIX_FMT_RGB555:  c->yuv2packedX = RENAME(yuv2rgb555_X_ar);  break;
2201                     case PIX_FMT_RGB565:  c->yuv2packedX = RENAME(yuv2rgb565_X_ar);  break;
2202                     case PIX_FMT_YUYV422: c->yuv2packedX = RENAME(yuv2yuyv422_X_ar); break;
2203                     default: break;
2204                     }
2205                 }
2206             } else {
2207                 c->yuv2yuv1 = RENAME(yuv2yuv1    );
2208                 c->yuv2yuvX = RENAME(yuv2yuvX    );
2209                 if (!(c->flags & SWS_FULL_CHR_H_INT)) {
2210                     switch (c->dstFormat) {
2211                     case PIX_FMT_RGB32:   c->yuv2packedX = RENAME(yuv2rgb32_X);   break;
2212                     case PIX_FMT_BGR24:   c->yuv2packedX = RENAME(yuv2bgr24_X);   break;
2213                     case PIX_FMT_RGB555:  c->yuv2packedX = RENAME(yuv2rgb555_X);  break;
2214                     case PIX_FMT_RGB565:  c->yuv2packedX = RENAME(yuv2rgb565_X);  break;
2215                     case PIX_FMT_YUYV422: c->yuv2packedX = RENAME(yuv2yuyv422_X); break;
2216                     default: break;
2217                     }
2218                 }
2219             }
2220         }
2221         if (!(c->flags & SWS_FULL_CHR_H_INT)) {
2222             switch (c->dstFormat) {
2223             case PIX_FMT_RGB32:
2224                 c->yuv2packed1 = RENAME(yuv2rgb32_1);
2225                 c->yuv2packed2 = RENAME(yuv2rgb32_2);
2226                 break;
2227             case PIX_FMT_BGR24:
2228                 c->yuv2packed1 = RENAME(yuv2bgr24_1);
2229                 c->yuv2packed2 = RENAME(yuv2bgr24_2);
2230                 break;
2231             case PIX_FMT_RGB555:
2232                 c->yuv2packed1 = RENAME(yuv2rgb555_1);
2233                 c->yuv2packed2 = RENAME(yuv2rgb555_2);
2234                 break;
2235             case PIX_FMT_RGB565:
2236                 c->yuv2packed1 = RENAME(yuv2rgb565_1);
2237                 c->yuv2packed2 = RENAME(yuv2rgb565_2);
2238                 break;
2239             case PIX_FMT_YUYV422:
2240                 c->yuv2packed1 = RENAME(yuv2yuyv422_1);
2241                 c->yuv2packed2 = RENAME(yuv2yuyv422_2);
2242                 break;
2243             default:
2244                 break;
2245             }
2246         }
2247     }
2248
2249 #if !COMPILE_TEMPLATE_MMX2
2250     c->hScale       = RENAME(hScale      );
2251 #endif /* !COMPILE_TEMPLATE_MMX2 */
2252
2253     // Use the new MMX scaler if the MMX2 one can't be used (it is faster than the x86 ASM one).
2254 #if COMPILE_TEMPLATE_MMX2
2255     if (c->flags & SWS_FAST_BILINEAR && c->canMMX2BeUsed)
2256     {
2257         c->hyscale_fast = RENAME(hyscale_fast);
2258         c->hcscale_fast = RENAME(hcscale_fast);
2259     } else {
2260 #endif /* COMPILE_TEMPLATE_MMX2 */
2261         c->hyscale_fast = NULL;
2262         c->hcscale_fast = NULL;
2263 #if COMPILE_TEMPLATE_MMX2
2264     }
2265 #endif /* COMPILE_TEMPLATE_MMX2 */
2266
2267 #if !COMPILE_TEMPLATE_MMX2
2268     switch(srcFormat) {
2269         case PIX_FMT_YUYV422  : c->chrToYV12 = RENAME(yuy2ToUV); break;
2270         case PIX_FMT_UYVY422  : c->chrToYV12 = RENAME(uyvyToUV); break;
2271         case PIX_FMT_NV12     : c->chrToYV12 = RENAME(nv12ToUV); break;
2272         case PIX_FMT_NV21     : c->chrToYV12 = RENAME(nv21ToUV); break;
2273         case PIX_FMT_YUV420P16BE:
2274         case PIX_FMT_YUV422P16BE:
2275         case PIX_FMT_YUV444P16BE: c->chrToYV12 = RENAME(BEToUV); break;
2276         case PIX_FMT_YUV420P16LE:
2277         case PIX_FMT_YUV422P16LE:
2278         case PIX_FMT_YUV444P16LE: c->chrToYV12 = RENAME(LEToUV); break;
2279         default: break;
2280     }
2281 #endif /* !COMPILE_TEMPLATE_MMX2 */
2282     if (!c->chrSrcHSubSample) {
2283         switch(srcFormat) {
2284         case PIX_FMT_BGR24  : c->chrToYV12 = RENAME(bgr24ToUV); break;
2285         case PIX_FMT_RGB24  : c->chrToYV12 = RENAME(rgb24ToUV); break;
2286         default: break;
2287         }
2288     }
2289
2290     switch (srcFormat) {
2291 #if !COMPILE_TEMPLATE_MMX2
2292     case PIX_FMT_YUYV422  :
2293     case PIX_FMT_YUV420P16BE:
2294     case PIX_FMT_YUV422P16BE:
2295     case PIX_FMT_YUV444P16BE:
2296     case PIX_FMT_Y400A    :
2297     case PIX_FMT_GRAY16BE : c->lumToYV12 = RENAME(yuy2ToY); break;
2298     case PIX_FMT_UYVY422  :
2299     case PIX_FMT_YUV420P16LE:
2300     case PIX_FMT_YUV422P16LE:
2301     case PIX_FMT_YUV444P16LE:
2302     case PIX_FMT_GRAY16LE : c->lumToYV12 = RENAME(uyvyToY); break;
2303 #endif /* !COMPILE_TEMPLATE_MMX2 */
2304     case PIX_FMT_BGR24    : c->lumToYV12 = RENAME(bgr24ToY); break;
2305     case PIX_FMT_RGB24    : c->lumToYV12 = RENAME(rgb24ToY); break;
2306     default: break;
2307     }
2308 #if !COMPILE_TEMPLATE_MMX2
2309     if (c->alpPixBuf) {
2310         switch (srcFormat) {
2311         case PIX_FMT_Y400A  : c->alpToYV12 = RENAME(yuy2ToY); break;
2312         default: break;
2313         }
2314     }
2315 #endif /* !COMPILE_TEMPLATE_MMX2 */
2316 }