Use MANGLE in cavsdsp.c to save two registers using gcc.
[ffmpeg.git] / libswscale / x86 / swscale_template.c
1 /*
2  * Copyright (C) 2001-2011 Michael Niedermayer <michaelni@gmx.at>
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg 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  * FFmpeg 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 FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 #include <stdint.h>
22
23 #include "libavutil/x86/asm.h"
24 #include "libswscale/swscale_internal.h"
25
26 #undef REAL_MOVNTQ
27 #undef MOVNTQ
28 #undef MOVNTQ2
29 #undef PREFETCH
30
31 #if COMPILE_TEMPLATE_MMXEXT
32 #define PREFETCH "prefetchnta"
33 #else
34 #define PREFETCH  " # nop"
35 #endif
36
37 #if COMPILE_TEMPLATE_MMXEXT
38 #define REAL_MOVNTQ(a,b) "movntq " #a ", " #b " \n\t"
39 #define MOVNTQ2 "movntq "
40 #else
41 #define REAL_MOVNTQ(a,b) "movq " #a ", " #b " \n\t"
42 #define MOVNTQ2 "movq "
43 #endif
44 #define MOVNTQ(a,b)  REAL_MOVNTQ(a,b)
45
46 #if !COMPILE_TEMPLATE_MMXEXT
47 static av_always_inline void
48 dither_8to16(const uint8_t *srcDither, int rot)
49 {
50     if (rot) {
51         __asm__ volatile("pxor      %%mm0, %%mm0\n\t"
52                          "movq       (%0), %%mm3\n\t"
53                          "movq      %%mm3, %%mm4\n\t"
54                          "psrlq       $24, %%mm3\n\t"
55                          "psllq       $40, %%mm4\n\t"
56                          "por       %%mm4, %%mm3\n\t"
57                          "movq      %%mm3, %%mm4\n\t"
58                          "punpcklbw %%mm0, %%mm3\n\t"
59                          "punpckhbw %%mm0, %%mm4\n\t"
60                          :: "r"(srcDither)
61                          );
62     } else {
63         __asm__ volatile("pxor      %%mm0, %%mm0\n\t"
64                          "movq       (%0), %%mm3\n\t"
65                          "movq      %%mm3, %%mm4\n\t"
66                          "punpcklbw %%mm0, %%mm3\n\t"
67                          "punpckhbw %%mm0, %%mm4\n\t"
68                          :: "r"(srcDither)
69                          );
70     }
71 }
72 #endif
73
74 static void RENAME(yuv2yuvX)(const int16_t *filter, int filterSize,
75                            const int16_t **src, uint8_t *dest, int dstW,
76                            const uint8_t *dither, int offset)
77 {
78     dither_8to16(dither, offset);
79     filterSize--;
80     __asm__ volatile(
81         "movd %0, %%mm1\n\t"
82         "punpcklwd %%mm1, %%mm1\n\t"
83         "punpckldq %%mm1, %%mm1\n\t"
84         "psllw        $3, %%mm1\n\t"
85         "paddw     %%mm1, %%mm3\n\t"
86         "paddw     %%mm1, %%mm4\n\t"
87         "psraw        $4, %%mm3\n\t"
88         "psraw        $4, %%mm4\n\t"
89         ::"m"(filterSize)
90      );
91
92     __asm__ volatile(\
93         "movq    %%mm3, %%mm6\n\t"
94         "movq    %%mm4, %%mm7\n\t"
95         "movl %3, %%ecx\n\t"
96         "mov                                 %0, %%"REG_d"  \n\t"\
97         "mov                        (%%"REG_d"), %%"REG_S"  \n\t"\
98         ".p2align                             4             \n\t" /* FIXME Unroll? */\
99         "1:                                                 \n\t"\
100         "movq                      8(%%"REG_d"), %%mm0      \n\t" /* filterCoeff */\
101         "movq                (%%"REG_S", %%"REG_c", 2), %%mm2      \n\t" /* srcData */\
102         "movq               8(%%"REG_S", %%"REG_c", 2), %%mm5      \n\t" /* srcData */\
103         "add                                $16, %%"REG_d"  \n\t"\
104         "mov                        (%%"REG_d"), %%"REG_S"  \n\t"\
105         "test                         %%"REG_S", %%"REG_S"  \n\t"\
106         "pmulhw                           %%mm0, %%mm2      \n\t"\
107         "pmulhw                           %%mm0, %%mm5      \n\t"\
108         "paddw                            %%mm2, %%mm3      \n\t"\
109         "paddw                            %%mm5, %%mm4      \n\t"\
110         " jnz                                1b             \n\t"\
111         "psraw                               $3, %%mm3      \n\t"\
112         "psraw                               $3, %%mm4      \n\t"\
113         "packuswb                         %%mm4, %%mm3      \n\t"
114         MOVNTQ2 "                         %%mm3, (%1, %%"REG_c")\n\t"
115         "add                          $8, %%"REG_c"         \n\t"\
116         "cmp                          %2, %%"REG_c"         \n\t"\
117         "movq    %%mm6, %%mm3\n\t"
118         "movq    %%mm7, %%mm4\n\t"
119         "mov                                 %0, %%"REG_d"  \n\t"\
120         "mov                        (%%"REG_d"), %%"REG_S"  \n\t"\
121         "jb                                  1b             \n\t"\
122         :: "g" (filter),
123            "r" (dest-offset), "g" ((x86_reg)(dstW+offset)), "m" (offset)
124         : "%"REG_d, "%"REG_S, "%"REG_c
125     );
126 }
127
128 #define YSCALEYUV2PACKEDX_UV \
129     __asm__ volatile(\
130         "xor                   %%"REG_a", %%"REG_a"     \n\t"\
131         ".p2align                      4                \n\t"\
132         "nop                                            \n\t"\
133         "1:                                             \n\t"\
134         "lea "CHR_MMX_FILTER_OFFSET"(%0), %%"REG_d"     \n\t"\
135         "mov                 (%%"REG_d"), %%"REG_S"     \n\t"\
136         "movq      "VROUNDER_OFFSET"(%0), %%mm3         \n\t"\
137         "movq                      %%mm3, %%mm4         \n\t"\
138         ".p2align                      4                \n\t"\
139         "2:                                             \n\t"\
140         "movq               8(%%"REG_d"), %%mm0         \n\t" /* filterCoeff */\
141         "movq     (%%"REG_S", %%"REG_a"), %%mm2         \n\t" /* UsrcData */\
142         "add                          %6, %%"REG_S"     \n\t" \
143         "movq     (%%"REG_S", %%"REG_a"), %%mm5         \n\t" /* VsrcData */\
144         "add                         $16, %%"REG_d"     \n\t"\
145         "mov                 (%%"REG_d"), %%"REG_S"     \n\t"\
146         "pmulhw                    %%mm0, %%mm2         \n\t"\
147         "pmulhw                    %%mm0, %%mm5         \n\t"\
148         "paddw                     %%mm2, %%mm3         \n\t"\
149         "paddw                     %%mm5, %%mm4         \n\t"\
150         "test                  %%"REG_S", %%"REG_S"     \n\t"\
151         " jnz                         2b                \n\t"\
152
153 #define YSCALEYUV2PACKEDX_YA(offset,coeff,src1,src2,dst1,dst2) \
154     "lea                "offset"(%0), %%"REG_d"     \n\t"\
155     "mov                 (%%"REG_d"), %%"REG_S"     \n\t"\
156     "movq      "VROUNDER_OFFSET"(%0), "#dst1"       \n\t"\
157     "movq                    "#dst1", "#dst2"       \n\t"\
158     ".p2align                      4                \n\t"\
159     "2:                                             \n\t"\
160     "movq               8(%%"REG_d"), "#coeff"      \n\t" /* filterCoeff */\
161     "movq  (%%"REG_S", %%"REG_a", 2), "#src1"       \n\t" /* Y1srcData */\
162     "movq 8(%%"REG_S", %%"REG_a", 2), "#src2"       \n\t" /* Y2srcData */\
163     "add                         $16, %%"REG_d"            \n\t"\
164     "mov                 (%%"REG_d"), %%"REG_S"     \n\t"\
165     "pmulhw                 "#coeff", "#src1"       \n\t"\
166     "pmulhw                 "#coeff", "#src2"       \n\t"\
167     "paddw                   "#src1", "#dst1"       \n\t"\
168     "paddw                   "#src2", "#dst2"       \n\t"\
169     "test                  %%"REG_S", %%"REG_S"     \n\t"\
170     " jnz                         2b                \n\t"\
171
172 #define YSCALEYUV2PACKEDX \
173     YSCALEYUV2PACKEDX_UV \
174     YSCALEYUV2PACKEDX_YA(LUM_MMX_FILTER_OFFSET,%%mm0,%%mm2,%%mm5,%%mm1,%%mm7) \
175
176 #define YSCALEYUV2PACKEDX_END                     \
177         :: "r" (&c->redDither),                   \
178             "m" (dummy), "m" (dummy), "m" (dummy),\
179             "r" (dest), "m" (dstW_reg), "m"(uv_off) \
180             NAMED_CONSTRAINTS_ADD(bF8,bFC) \
181         : "%"REG_a, "%"REG_d, "%"REG_S            \
182     );
183
184 #define YSCALEYUV2PACKEDX_ACCURATE_UV \
185     __asm__ volatile(\
186         "xor %%"REG_a", %%"REG_a"                       \n\t"\
187         ".p2align                      4                \n\t"\
188         "nop                                            \n\t"\
189         "1:                                             \n\t"\
190         "lea "CHR_MMX_FILTER_OFFSET"(%0), %%"REG_d"     \n\t"\
191         "mov                 (%%"REG_d"), %%"REG_S"     \n\t"\
192         "pxor                      %%mm4, %%mm4         \n\t"\
193         "pxor                      %%mm5, %%mm5         \n\t"\
194         "pxor                      %%mm6, %%mm6         \n\t"\
195         "pxor                      %%mm7, %%mm7         \n\t"\
196         ".p2align                      4                \n\t"\
197         "2:                                             \n\t"\
198         "movq     (%%"REG_S", %%"REG_a"), %%mm0         \n\t" /* UsrcData */\
199         "add                          %6, %%"REG_S"      \n\t" \
200         "movq     (%%"REG_S", %%"REG_a"), %%mm2         \n\t" /* VsrcData */\
201         "mov "STR(APCK_PTR2)"(%%"REG_d"), %%"REG_S"     \n\t"\
202         "movq     (%%"REG_S", %%"REG_a"), %%mm1         \n\t" /* UsrcData */\
203         "movq                      %%mm0, %%mm3         \n\t"\
204         "punpcklwd                 %%mm1, %%mm0         \n\t"\
205         "punpckhwd                 %%mm1, %%mm3         \n\t"\
206         "movq "STR(APCK_COEF)"(%%"REG_d"),%%mm1         \n\t" /* filterCoeff */\
207         "pmaddwd                   %%mm1, %%mm0         \n\t"\
208         "pmaddwd                   %%mm1, %%mm3         \n\t"\
209         "paddd                     %%mm0, %%mm4         \n\t"\
210         "paddd                     %%mm3, %%mm5         \n\t"\
211         "add                          %6, %%"REG_S"      \n\t" \
212         "movq     (%%"REG_S", %%"REG_a"), %%mm3         \n\t" /* VsrcData */\
213         "mov "STR(APCK_SIZE)"(%%"REG_d"), %%"REG_S"     \n\t"\
214         "add           $"STR(APCK_SIZE)", %%"REG_d"     \n\t"\
215         "test                  %%"REG_S", %%"REG_S"     \n\t"\
216         "movq                      %%mm2, %%mm0         \n\t"\
217         "punpcklwd                 %%mm3, %%mm2         \n\t"\
218         "punpckhwd                 %%mm3, %%mm0         \n\t"\
219         "pmaddwd                   %%mm1, %%mm2         \n\t"\
220         "pmaddwd                   %%mm1, %%mm0         \n\t"\
221         "paddd                     %%mm2, %%mm6         \n\t"\
222         "paddd                     %%mm0, %%mm7         \n\t"\
223         " jnz                         2b                \n\t"\
224         "psrad                       $16, %%mm4         \n\t"\
225         "psrad                       $16, %%mm5         \n\t"\
226         "psrad                       $16, %%mm6         \n\t"\
227         "psrad                       $16, %%mm7         \n\t"\
228         "movq      "VROUNDER_OFFSET"(%0), %%mm0         \n\t"\
229         "packssdw                  %%mm5, %%mm4         \n\t"\
230         "packssdw                  %%mm7, %%mm6         \n\t"\
231         "paddw                     %%mm0, %%mm4         \n\t"\
232         "paddw                     %%mm0, %%mm6         \n\t"\
233         "movq                      %%mm4, "U_TEMP"(%0)  \n\t"\
234         "movq                      %%mm6, "V_TEMP"(%0)  \n\t"\
235
236 #define YSCALEYUV2PACKEDX_ACCURATE_YA(offset) \
237     "lea                "offset"(%0), %%"REG_d"     \n\t"\
238     "mov                 (%%"REG_d"), %%"REG_S"     \n\t"\
239     "pxor                      %%mm1, %%mm1         \n\t"\
240     "pxor                      %%mm5, %%mm5         \n\t"\
241     "pxor                      %%mm7, %%mm7         \n\t"\
242     "pxor                      %%mm6, %%mm6         \n\t"\
243     ".p2align                      4                \n\t"\
244     "2:                                             \n\t"\
245     "movq  (%%"REG_S", %%"REG_a", 2), %%mm0         \n\t" /* Y1srcData */\
246     "movq 8(%%"REG_S", %%"REG_a", 2), %%mm2         \n\t" /* Y2srcData */\
247     "mov "STR(APCK_PTR2)"(%%"REG_d"), %%"REG_S"     \n\t"\
248     "movq  (%%"REG_S", %%"REG_a", 2), %%mm4         \n\t" /* Y1srcData */\
249     "movq                      %%mm0, %%mm3         \n\t"\
250     "punpcklwd                 %%mm4, %%mm0         \n\t"\
251     "punpckhwd                 %%mm4, %%mm3         \n\t"\
252     "movq "STR(APCK_COEF)"(%%"REG_d"), %%mm4         \n\t" /* filterCoeff */\
253     "pmaddwd                   %%mm4, %%mm0         \n\t"\
254     "pmaddwd                   %%mm4, %%mm3         \n\t"\
255     "paddd                     %%mm0, %%mm1         \n\t"\
256     "paddd                     %%mm3, %%mm5         \n\t"\
257     "movq 8(%%"REG_S", %%"REG_a", 2), %%mm3         \n\t" /* Y2srcData */\
258     "mov "STR(APCK_SIZE)"(%%"REG_d"), %%"REG_S"     \n\t"\
259     "add           $"STR(APCK_SIZE)", %%"REG_d"     \n\t"\
260     "test                  %%"REG_S", %%"REG_S"     \n\t"\
261     "movq                      %%mm2, %%mm0         \n\t"\
262     "punpcklwd                 %%mm3, %%mm2         \n\t"\
263     "punpckhwd                 %%mm3, %%mm0         \n\t"\
264     "pmaddwd                   %%mm4, %%mm2         \n\t"\
265     "pmaddwd                   %%mm4, %%mm0         \n\t"\
266     "paddd                     %%mm2, %%mm7         \n\t"\
267     "paddd                     %%mm0, %%mm6         \n\t"\
268     " jnz                         2b                \n\t"\
269     "psrad                       $16, %%mm1         \n\t"\
270     "psrad                       $16, %%mm5         \n\t"\
271     "psrad                       $16, %%mm7         \n\t"\
272     "psrad                       $16, %%mm6         \n\t"\
273     "movq      "VROUNDER_OFFSET"(%0), %%mm0         \n\t"\
274     "packssdw                  %%mm5, %%mm1         \n\t"\
275     "packssdw                  %%mm6, %%mm7         \n\t"\
276     "paddw                     %%mm0, %%mm1         \n\t"\
277     "paddw                     %%mm0, %%mm7         \n\t"\
278     "movq               "U_TEMP"(%0), %%mm3         \n\t"\
279     "movq               "V_TEMP"(%0), %%mm4         \n\t"\
280
281 #define YSCALEYUV2PACKEDX_ACCURATE \
282     YSCALEYUV2PACKEDX_ACCURATE_UV \
283     YSCALEYUV2PACKEDX_ACCURATE_YA(LUM_MMX_FILTER_OFFSET)
284
285 #define YSCALEYUV2RGBX \
286     "psubw  "U_OFFSET"(%0), %%mm3       \n\t" /* (U-128)8*/\
287     "psubw  "V_OFFSET"(%0), %%mm4       \n\t" /* (V-128)8*/\
288     "movq            %%mm3, %%mm2       \n\t" /* (U-128)8*/\
289     "movq            %%mm4, %%mm5       \n\t" /* (V-128)8*/\
290     "pmulhw "UG_COEFF"(%0), %%mm3       \n\t"\
291     "pmulhw "VG_COEFF"(%0), %%mm4       \n\t"\
292     /* mm2=(U-128)8, mm3=ug, mm4=vg mm5=(V-128)8 */\
293     "pmulhw "UB_COEFF"(%0), %%mm2       \n\t"\
294     "pmulhw "VR_COEFF"(%0), %%mm5       \n\t"\
295     "psubw  "Y_OFFSET"(%0), %%mm1       \n\t" /* 8(Y-16)*/\
296     "psubw  "Y_OFFSET"(%0), %%mm7       \n\t" /* 8(Y-16)*/\
297     "pmulhw  "Y_COEFF"(%0), %%mm1       \n\t"\
298     "pmulhw  "Y_COEFF"(%0), %%mm7       \n\t"\
299     /* mm1= Y1, mm2=ub, mm3=ug, mm4=vg mm5=vr, mm7=Y2 */\
300     "paddw           %%mm3, %%mm4       \n\t"\
301     "movq            %%mm2, %%mm0       \n\t"\
302     "movq            %%mm5, %%mm6       \n\t"\
303     "movq            %%mm4, %%mm3       \n\t"\
304     "punpcklwd       %%mm2, %%mm2       \n\t"\
305     "punpcklwd       %%mm5, %%mm5       \n\t"\
306     "punpcklwd       %%mm4, %%mm4       \n\t"\
307     "paddw           %%mm1, %%mm2       \n\t"\
308     "paddw           %%mm1, %%mm5       \n\t"\
309     "paddw           %%mm1, %%mm4       \n\t"\
310     "punpckhwd       %%mm0, %%mm0       \n\t"\
311     "punpckhwd       %%mm6, %%mm6       \n\t"\
312     "punpckhwd       %%mm3, %%mm3       \n\t"\
313     "paddw           %%mm7, %%mm0       \n\t"\
314     "paddw           %%mm7, %%mm6       \n\t"\
315     "paddw           %%mm7, %%mm3       \n\t"\
316     /* mm0=B1, mm2=B2, mm3=G2, mm4=G1, mm5=R1, mm6=R2 */\
317     "packuswb        %%mm0, %%mm2       \n\t"\
318     "packuswb        %%mm6, %%mm5       \n\t"\
319     "packuswb        %%mm3, %%mm4       \n\t"\
320
321 #define REAL_WRITEBGR32(dst, dstw, index, b, g, r, a, q0, q2, q3, t) \
322     "movq       "#b", "#q2"     \n\t" /* B */\
323     "movq       "#r", "#t"      \n\t" /* R */\
324     "punpcklbw  "#g", "#b"      \n\t" /* GBGBGBGB 0 */\
325     "punpcklbw  "#a", "#r"      \n\t" /* ARARARAR 0 */\
326     "punpckhbw  "#g", "#q2"     \n\t" /* GBGBGBGB 2 */\
327     "punpckhbw  "#a", "#t"      \n\t" /* ARARARAR 2 */\
328     "movq       "#b", "#q0"     \n\t" /* GBGBGBGB 0 */\
329     "movq      "#q2", "#q3"     \n\t" /* GBGBGBGB 2 */\
330     "punpcklwd  "#r", "#q0"     \n\t" /* ARGBARGB 0 */\
331     "punpckhwd  "#r", "#b"      \n\t" /* ARGBARGB 1 */\
332     "punpcklwd  "#t", "#q2"     \n\t" /* ARGBARGB 2 */\
333     "punpckhwd  "#t", "#q3"     \n\t" /* ARGBARGB 3 */\
334 \
335     MOVNTQ(   q0,   (dst, index, 4))\
336     MOVNTQ(    b,  8(dst, index, 4))\
337     MOVNTQ(   q2, 16(dst, index, 4))\
338     MOVNTQ(   q3, 24(dst, index, 4))\
339 \
340     "add      $8, "#index"      \n\t"\
341     "cmp  "dstw", "#index"      \n\t"\
342     " jb      1b                \n\t"
343 #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)
344
345 static void RENAME(yuv2rgb32_X_ar)(SwsContext *c, const int16_t *lumFilter,
346                                    const int16_t **lumSrc, int lumFilterSize,
347                                    const int16_t *chrFilter, const int16_t **chrUSrc,
348                                    const int16_t **chrVSrc,
349                                    int chrFilterSize, const int16_t **alpSrc,
350                                    uint8_t *dest, int dstW, int dstY)
351 {
352     x86_reg dummy=0;
353     x86_reg dstW_reg = dstW;
354     x86_reg uv_off = c->uv_offx2;
355
356     if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
357         YSCALEYUV2PACKEDX_ACCURATE
358         YSCALEYUV2RGBX
359         "movq                      %%mm2, "U_TEMP"(%0)  \n\t"
360         "movq                      %%mm4, "V_TEMP"(%0)  \n\t"
361         "movq                      %%mm5, "Y_TEMP"(%0)  \n\t"
362         YSCALEYUV2PACKEDX_ACCURATE_YA(ALP_MMX_FILTER_OFFSET)
363         "movq               "Y_TEMP"(%0), %%mm5         \n\t"
364         "psraw                        $3, %%mm1         \n\t"
365         "psraw                        $3, %%mm7         \n\t"
366         "packuswb                  %%mm7, %%mm1         \n\t"
367         WRITEBGR32(%4, "%5", %%REGa, %%mm3, %%mm4, %%mm5, %%mm1, %%mm0, %%mm7, %%mm2, %%mm6)
368         YSCALEYUV2PACKEDX_END
369     } else {
370         YSCALEYUV2PACKEDX_ACCURATE
371         YSCALEYUV2RGBX
372         "pcmpeqd %%mm7, %%mm7 \n\t"
373         WRITEBGR32(%4, "%5", %%REGa, %%mm2, %%mm4, %%mm5, %%mm7, %%mm0, %%mm1, %%mm3, %%mm6)
374         YSCALEYUV2PACKEDX_END
375     }
376 }
377
378 static void RENAME(yuv2rgb32_X)(SwsContext *c, const int16_t *lumFilter,
379                                 const int16_t **lumSrc, int lumFilterSize,
380                                 const int16_t *chrFilter, const int16_t **chrUSrc,
381                                 const int16_t **chrVSrc,
382                                 int chrFilterSize, const int16_t **alpSrc,
383                                 uint8_t *dest, int dstW, int dstY)
384 {
385     x86_reg dummy=0;
386     x86_reg dstW_reg = dstW;
387     x86_reg uv_off = c->uv_offx2;
388
389     if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
390         YSCALEYUV2PACKEDX
391         YSCALEYUV2RGBX
392         YSCALEYUV2PACKEDX_YA(ALP_MMX_FILTER_OFFSET, %%mm0, %%mm3, %%mm6, %%mm1, %%mm7)
393         "psraw                        $3, %%mm1         \n\t"
394         "psraw                        $3, %%mm7         \n\t"
395         "packuswb                  %%mm7, %%mm1         \n\t"
396         WRITEBGR32(%4, "%5", %%REGa, %%mm2, %%mm4, %%mm5, %%mm1, %%mm0, %%mm7, %%mm3, %%mm6)
397         YSCALEYUV2PACKEDX_END
398     } else {
399         YSCALEYUV2PACKEDX
400         YSCALEYUV2RGBX
401         "pcmpeqd %%mm7, %%mm7 \n\t"
402         WRITEBGR32(%4, "%5", %%REGa, %%mm2, %%mm4, %%mm5, %%mm7, %%mm0, %%mm1, %%mm3, %%mm6)
403         YSCALEYUV2PACKEDX_END
404     }
405 }
406
407 #define REAL_WRITERGB16(dst, dstw, index) \
408     "pand "MANGLE(bF8)", %%mm2  \n\t" /* B */\
409     "pand "MANGLE(bFC)", %%mm4  \n\t" /* G */\
410     "pand "MANGLE(bF8)", %%mm5  \n\t" /* R */\
411     "psrlq           $3, %%mm2  \n\t"\
412 \
413     "movq         %%mm2, %%mm1  \n\t"\
414     "movq         %%mm4, %%mm3  \n\t"\
415 \
416     "punpcklbw    %%mm7, %%mm3  \n\t"\
417     "punpcklbw    %%mm5, %%mm2  \n\t"\
418     "punpckhbw    %%mm7, %%mm4  \n\t"\
419     "punpckhbw    %%mm5, %%mm1  \n\t"\
420 \
421     "psllq           $3, %%mm3  \n\t"\
422     "psllq           $3, %%mm4  \n\t"\
423 \
424     "por          %%mm3, %%mm2  \n\t"\
425     "por          %%mm4, %%mm1  \n\t"\
426 \
427     MOVNTQ(%%mm2,  (dst, index, 2))\
428     MOVNTQ(%%mm1, 8(dst, index, 2))\
429 \
430     "add             $8, "#index"   \n\t"\
431     "cmp         "dstw", "#index"   \n\t"\
432     " jb             1b             \n\t"
433 #define WRITERGB16(dst, dstw, index)  REAL_WRITERGB16(dst, dstw, index)
434
435 static void RENAME(yuv2rgb565_X_ar)(SwsContext *c, const int16_t *lumFilter,
436                                     const int16_t **lumSrc, int lumFilterSize,
437                                     const int16_t *chrFilter, const int16_t **chrUSrc,
438                                     const int16_t **chrVSrc,
439                                     int chrFilterSize, const int16_t **alpSrc,
440                                     uint8_t *dest, int dstW, int dstY)
441 {
442     x86_reg dummy=0;
443     x86_reg dstW_reg = dstW;
444     x86_reg uv_off = c->uv_offx2;
445
446     YSCALEYUV2PACKEDX_ACCURATE
447     YSCALEYUV2RGBX
448     "pxor %%mm7, %%mm7 \n\t"
449     /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */
450 #ifdef DITHER1XBPP
451     "paddusb "BLUE_DITHER"(%0), %%mm2\n\t"
452     "paddusb "GREEN_DITHER"(%0), %%mm4\n\t"
453     "paddusb "RED_DITHER"(%0), %%mm5\n\t"
454 #endif
455     WRITERGB16(%4, "%5", %%REGa)
456     YSCALEYUV2PACKEDX_END
457 }
458
459 static void RENAME(yuv2rgb565_X)(SwsContext *c, const int16_t *lumFilter,
460                                  const int16_t **lumSrc, int lumFilterSize,
461                                  const int16_t *chrFilter, const int16_t **chrUSrc,
462                                  const int16_t **chrVSrc,
463                                  int chrFilterSize, const int16_t **alpSrc,
464                                  uint8_t *dest, int dstW, int dstY)
465 {
466     x86_reg dummy=0;
467     x86_reg dstW_reg = dstW;
468     x86_reg uv_off = c->uv_offx2;
469
470     YSCALEYUV2PACKEDX
471     YSCALEYUV2RGBX
472     "pxor %%mm7, %%mm7 \n\t"
473     /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */
474 #ifdef DITHER1XBPP
475     "paddusb "BLUE_DITHER"(%0), %%mm2  \n\t"
476     "paddusb "GREEN_DITHER"(%0), %%mm4  \n\t"
477     "paddusb "RED_DITHER"(%0), %%mm5  \n\t"
478 #endif
479     WRITERGB16(%4, "%5", %%REGa)
480     YSCALEYUV2PACKEDX_END
481 }
482
483 #define REAL_WRITERGB15(dst, dstw, index) \
484     "pand "MANGLE(bF8)", %%mm2  \n\t" /* B */\
485     "pand "MANGLE(bF8)", %%mm4  \n\t" /* G */\
486     "pand "MANGLE(bF8)", %%mm5  \n\t" /* R */\
487     "psrlq           $3, %%mm2  \n\t"\
488     "psrlq           $1, %%mm5  \n\t"\
489 \
490     "movq         %%mm2, %%mm1  \n\t"\
491     "movq         %%mm4, %%mm3  \n\t"\
492 \
493     "punpcklbw    %%mm7, %%mm3  \n\t"\
494     "punpcklbw    %%mm5, %%mm2  \n\t"\
495     "punpckhbw    %%mm7, %%mm4  \n\t"\
496     "punpckhbw    %%mm5, %%mm1  \n\t"\
497 \
498     "psllq           $2, %%mm3  \n\t"\
499     "psllq           $2, %%mm4  \n\t"\
500 \
501     "por          %%mm3, %%mm2  \n\t"\
502     "por          %%mm4, %%mm1  \n\t"\
503 \
504     MOVNTQ(%%mm2,  (dst, index, 2))\
505     MOVNTQ(%%mm1, 8(dst, index, 2))\
506 \
507     "add             $8, "#index"   \n\t"\
508     "cmp         "dstw", "#index"   \n\t"\
509     " jb             1b             \n\t"
510 #define WRITERGB15(dst, dstw, index)  REAL_WRITERGB15(dst, dstw, index)
511
512 static void RENAME(yuv2rgb555_X_ar)(SwsContext *c, const int16_t *lumFilter,
513                                     const int16_t **lumSrc, int lumFilterSize,
514                                     const int16_t *chrFilter, const int16_t **chrUSrc,
515                                     const int16_t **chrVSrc,
516                                     int chrFilterSize, const int16_t **alpSrc,
517                                     uint8_t *dest, int dstW, int dstY)
518 {
519     x86_reg dummy=0;
520     x86_reg dstW_reg = dstW;
521     x86_reg uv_off = c->uv_offx2;
522
523     YSCALEYUV2PACKEDX_ACCURATE
524     YSCALEYUV2RGBX
525     "pxor %%mm7, %%mm7 \n\t"
526     /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */
527 #ifdef DITHER1XBPP
528     "paddusb "BLUE_DITHER"(%0), %%mm2\n\t"
529     "paddusb "GREEN_DITHER"(%0), %%mm4\n\t"
530     "paddusb "RED_DITHER"(%0), %%mm5\n\t"
531 #endif
532     WRITERGB15(%4, "%5", %%REGa)
533     YSCALEYUV2PACKEDX_END
534 }
535
536 static void RENAME(yuv2rgb555_X)(SwsContext *c, const int16_t *lumFilter,
537                                  const int16_t **lumSrc, int lumFilterSize,
538                                  const int16_t *chrFilter, const int16_t **chrUSrc,
539                                  const int16_t **chrVSrc,
540                                  int chrFilterSize, const int16_t **alpSrc,
541                                  uint8_t *dest, int dstW, int dstY)
542 {
543     x86_reg dummy=0;
544     x86_reg dstW_reg = dstW;
545     x86_reg uv_off = c->uv_offx2;
546
547     YSCALEYUV2PACKEDX
548     YSCALEYUV2RGBX
549     "pxor %%mm7, %%mm7 \n\t"
550     /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */
551 #ifdef DITHER1XBPP
552     "paddusb "BLUE_DITHER"(%0), %%mm2  \n\t"
553     "paddusb "GREEN_DITHER"(%0), %%mm4  \n\t"
554     "paddusb "RED_DITHER"(%0), %%mm5  \n\t"
555 #endif
556     WRITERGB15(%4, "%5", %%REGa)
557     YSCALEYUV2PACKEDX_END
558 }
559
560 #define WRITEBGR24MMX(dst, dstw, index) \
561     /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */\
562     "movq      %%mm2, %%mm1     \n\t" /* B */\
563     "movq      %%mm5, %%mm6     \n\t" /* R */\
564     "punpcklbw %%mm4, %%mm2     \n\t" /* GBGBGBGB 0 */\
565     "punpcklbw %%mm7, %%mm5     \n\t" /* 0R0R0R0R 0 */\
566     "punpckhbw %%mm4, %%mm1     \n\t" /* GBGBGBGB 2 */\
567     "punpckhbw %%mm7, %%mm6     \n\t" /* 0R0R0R0R 2 */\
568     "movq      %%mm2, %%mm0     \n\t" /* GBGBGBGB 0 */\
569     "movq      %%mm1, %%mm3     \n\t" /* GBGBGBGB 2 */\
570     "punpcklwd %%mm5, %%mm0     \n\t" /* 0RGB0RGB 0 */\
571     "punpckhwd %%mm5, %%mm2     \n\t" /* 0RGB0RGB 1 */\
572     "punpcklwd %%mm6, %%mm1     \n\t" /* 0RGB0RGB 2 */\
573     "punpckhwd %%mm6, %%mm3     \n\t" /* 0RGB0RGB 3 */\
574 \
575     "movq      %%mm0, %%mm4     \n\t" /* 0RGB0RGB 0 */\
576     "movq      %%mm2, %%mm6     \n\t" /* 0RGB0RGB 1 */\
577     "movq      %%mm1, %%mm5     \n\t" /* 0RGB0RGB 2 */\
578     "movq      %%mm3, %%mm7     \n\t" /* 0RGB0RGB 3 */\
579 \
580     "psllq       $40, %%mm0     \n\t" /* RGB00000 0 */\
581     "psllq       $40, %%mm2     \n\t" /* RGB00000 1 */\
582     "psllq       $40, %%mm1     \n\t" /* RGB00000 2 */\
583     "psllq       $40, %%mm3     \n\t" /* RGB00000 3 */\
584 \
585     "punpckhdq %%mm4, %%mm0     \n\t" /* 0RGBRGB0 0 */\
586     "punpckhdq %%mm6, %%mm2     \n\t" /* 0RGBRGB0 1 */\
587     "punpckhdq %%mm5, %%mm1     \n\t" /* 0RGBRGB0 2 */\
588     "punpckhdq %%mm7, %%mm3     \n\t" /* 0RGBRGB0 3 */\
589 \
590     "psrlq        $8, %%mm0     \n\t" /* 00RGBRGB 0 */\
591     "movq      %%mm2, %%mm6     \n\t" /* 0RGBRGB0 1 */\
592     "psllq       $40, %%mm2     \n\t" /* GB000000 1 */\
593     "por       %%mm2, %%mm0     \n\t" /* GBRGBRGB 0 */\
594     MOVNTQ(%%mm0, (dst))\
595 \
596     "psrlq       $24, %%mm6     \n\t" /* 0000RGBR 1 */\
597     "movq      %%mm1, %%mm5     \n\t" /* 0RGBRGB0 2 */\
598     "psllq       $24, %%mm1     \n\t" /* BRGB0000 2 */\
599     "por       %%mm1, %%mm6     \n\t" /* BRGBRGBR 1 */\
600     MOVNTQ(%%mm6, 8(dst))\
601 \
602     "psrlq       $40, %%mm5     \n\t" /* 000000RG 2 */\
603     "psllq        $8, %%mm3     \n\t" /* RGBRGB00 3 */\
604     "por       %%mm3, %%mm5     \n\t" /* RGBRGBRG 2 */\
605     MOVNTQ(%%mm5, 16(dst))\
606 \
607     "add         $24, "#dst"    \n\t"\
608 \
609     "add          $8, "#index"  \n\t"\
610     "cmp      "dstw", "#index"  \n\t"\
611     " jb          1b            \n\t"
612
613 #define WRITEBGR24MMXEXT(dst, dstw, index) \
614     /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */\
615     "movq "MANGLE(ff_M24A)", %%mm0 \n\t"\
616     "movq "MANGLE(ff_M24C)", %%mm7 \n\t"\
617     "pshufw $0x50, %%mm2, %%mm1 \n\t" /* B3 B2 B3 B2  B1 B0 B1 B0 */\
618     "pshufw $0x50, %%mm4, %%mm3 \n\t" /* G3 G2 G3 G2  G1 G0 G1 G0 */\
619     "pshufw $0x00, %%mm5, %%mm6 \n\t" /* R1 R0 R1 R0  R1 R0 R1 R0 */\
620 \
621     "pand   %%mm0, %%mm1        \n\t" /*    B2        B1       B0 */\
622     "pand   %%mm0, %%mm3        \n\t" /*    G2        G1       G0 */\
623     "pand   %%mm7, %%mm6        \n\t" /*       R1        R0       */\
624 \
625     "psllq     $8, %%mm3        \n\t" /* G2        G1       G0    */\
626     "por    %%mm1, %%mm6        \n\t"\
627     "por    %%mm3, %%mm6        \n\t"\
628     MOVNTQ(%%mm6, (dst))\
629 \
630     "psrlq     $8, %%mm4        \n\t" /* 00 G7 G6 G5  G4 G3 G2 G1 */\
631     "pshufw $0xA5, %%mm2, %%mm1 \n\t" /* B5 B4 B5 B4  B3 B2 B3 B2 */\
632     "pshufw $0x55, %%mm4, %%mm3 \n\t" /* G4 G3 G4 G3  G4 G3 G4 G3 */\
633     "pshufw $0xA5, %%mm5, %%mm6 \n\t" /* R5 R4 R5 R4  R3 R2 R3 R2 */\
634 \
635     "pand "MANGLE(ff_M24B)", %%mm1 \n\t" /* B5       B4        B3    */\
636     "pand   %%mm7, %%mm3        \n\t" /*       G4        G3       */\
637     "pand   %%mm0, %%mm6        \n\t" /*    R4        R3       R2 */\
638 \
639     "por    %%mm1, %%mm3        \n\t" /* B5    G4 B4     G3 B3    */\
640     "por    %%mm3, %%mm6        \n\t"\
641     MOVNTQ(%%mm6, 8(dst))\
642 \
643     "pshufw $0xFF, %%mm2, %%mm1 \n\t" /* B7 B6 B7 B6  B7 B6 B6 B7 */\
644     "pshufw $0xFA, %%mm4, %%mm3 \n\t" /* 00 G7 00 G7  G6 G5 G6 G5 */\
645     "pshufw $0xFA, %%mm5, %%mm6 \n\t" /* R7 R6 R7 R6  R5 R4 R5 R4 */\
646 \
647     "pand   %%mm7, %%mm1        \n\t" /*       B7        B6       */\
648     "pand   %%mm0, %%mm3        \n\t" /*    G7        G6       G5 */\
649     "pand "MANGLE(ff_M24B)", %%mm6 \n\t" /* R7       R6        R5    */\
650 \
651     "por    %%mm1, %%mm3        \n\t"\
652     "por    %%mm3, %%mm6        \n\t"\
653     MOVNTQ(%%mm6, 16(dst))\
654 \
655     "add      $24, "#dst"       \n\t"\
656 \
657     "add       $8, "#index"     \n\t"\
658     "cmp   "dstw", "#index"     \n\t"\
659     " jb       1b               \n\t"
660
661 #if COMPILE_TEMPLATE_MMXEXT
662 #undef WRITEBGR24
663 #define WRITEBGR24(dst, dstw, index)  WRITEBGR24MMXEXT(dst, dstw, index)
664 #else
665 #undef WRITEBGR24
666 #define WRITEBGR24(dst, dstw, index)  WRITEBGR24MMX(dst, dstw, index)
667 #endif
668
669 static void RENAME(yuv2bgr24_X_ar)(SwsContext *c, const int16_t *lumFilter,
670                                    const int16_t **lumSrc, int lumFilterSize,
671                                    const int16_t *chrFilter, const int16_t **chrUSrc,
672                                    const int16_t **chrVSrc,
673                                    int chrFilterSize, const int16_t **alpSrc,
674                                    uint8_t *dest, int dstW, int dstY)
675 {
676     x86_reg dummy=0;
677     x86_reg dstW_reg = dstW;
678     x86_reg uv_off = c->uv_offx2;
679
680     YSCALEYUV2PACKEDX_ACCURATE
681     YSCALEYUV2RGBX
682     "pxor %%mm7, %%mm7 \n\t"
683     "lea (%%"REG_a", %%"REG_a", 2), %%"REG_c"\n\t" //FIXME optimize
684     "add %4, %%"REG_c"                        \n\t"
685     WRITEBGR24(%%REGc, "%5", %%REGa)
686     :: "r" (&c->redDither),
687        "m" (dummy), "m" (dummy), "m" (dummy),
688        "r" (dest), "m" (dstW_reg), "m"(uv_off)
689        NAMED_CONSTRAINTS_ADD(ff_M24A,ff_M24C,ff_M24B)
690     : "%"REG_a, "%"REG_c, "%"REG_d, "%"REG_S
691     );
692 }
693
694 static void RENAME(yuv2bgr24_X)(SwsContext *c, const int16_t *lumFilter,
695                                 const int16_t **lumSrc, int lumFilterSize,
696                                 const int16_t *chrFilter, const int16_t **chrUSrc,
697                                 const int16_t **chrVSrc,
698                                 int chrFilterSize, const int16_t **alpSrc,
699                                 uint8_t *dest, int dstW, int dstY)
700 {
701     x86_reg dummy=0;
702     x86_reg dstW_reg = dstW;
703     x86_reg uv_off = c->uv_offx2;
704
705     YSCALEYUV2PACKEDX
706     YSCALEYUV2RGBX
707     "pxor                    %%mm7, %%mm7       \n\t"
708     "lea (%%"REG_a", %%"REG_a", 2), %%"REG_c"   \n\t" //FIXME optimize
709     "add                        %4, %%"REG_c"   \n\t"
710     WRITEBGR24(%%REGc, "%5", %%REGa)
711     :: "r" (&c->redDither),
712        "m" (dummy), "m" (dummy), "m" (dummy),
713        "r" (dest),  "m" (dstW_reg), "m"(uv_off)
714        NAMED_CONSTRAINTS_ADD(ff_M24A,ff_M24C,ff_M24B)
715     : "%"REG_a, "%"REG_c, "%"REG_d, "%"REG_S
716     );
717 }
718
719 #define REAL_WRITEYUY2(dst, dstw, index) \
720     "packuswb  %%mm3, %%mm3     \n\t"\
721     "packuswb  %%mm4, %%mm4     \n\t"\
722     "packuswb  %%mm7, %%mm1     \n\t"\
723     "punpcklbw %%mm4, %%mm3     \n\t"\
724     "movq      %%mm1, %%mm7     \n\t"\
725     "punpcklbw %%mm3, %%mm1     \n\t"\
726     "punpckhbw %%mm3, %%mm7     \n\t"\
727 \
728     MOVNTQ(%%mm1, (dst, index, 2))\
729     MOVNTQ(%%mm7, 8(dst, index, 2))\
730 \
731     "add          $8, "#index"  \n\t"\
732     "cmp      "dstw", "#index"  \n\t"\
733     " jb          1b            \n\t"
734 #define WRITEYUY2(dst, dstw, index)  REAL_WRITEYUY2(dst, dstw, index)
735
736 static void RENAME(yuv2yuyv422_X_ar)(SwsContext *c, const int16_t *lumFilter,
737                                      const int16_t **lumSrc, int lumFilterSize,
738                                      const int16_t *chrFilter, const int16_t **chrUSrc,
739                                      const int16_t **chrVSrc,
740                                      int chrFilterSize, const int16_t **alpSrc,
741                                      uint8_t *dest, int dstW, int dstY)
742 {
743     x86_reg dummy=0;
744     x86_reg dstW_reg = dstW;
745     x86_reg uv_off = c->uv_offx2;
746
747     YSCALEYUV2PACKEDX_ACCURATE
748     /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */
749     "psraw $3, %%mm3    \n\t"
750     "psraw $3, %%mm4    \n\t"
751     "psraw $3, %%mm1    \n\t"
752     "psraw $3, %%mm7    \n\t"
753     WRITEYUY2(%4, "%5", %%REGa)
754     YSCALEYUV2PACKEDX_END
755 }
756
757 static void RENAME(yuv2yuyv422_X)(SwsContext *c, const int16_t *lumFilter,
758                                   const int16_t **lumSrc, int lumFilterSize,
759                                   const int16_t *chrFilter, const int16_t **chrUSrc,
760                                   const int16_t **chrVSrc,
761                                   int chrFilterSize, const int16_t **alpSrc,
762                                   uint8_t *dest, int dstW, int dstY)
763 {
764     x86_reg dummy=0;
765     x86_reg dstW_reg = dstW;
766     x86_reg uv_off = c->uv_offx2;
767
768     YSCALEYUV2PACKEDX
769     /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */
770     "psraw $3, %%mm3    \n\t"
771     "psraw $3, %%mm4    \n\t"
772     "psraw $3, %%mm1    \n\t"
773     "psraw $3, %%mm7    \n\t"
774     WRITEYUY2(%4, "%5", %%REGa)
775     YSCALEYUV2PACKEDX_END
776 }
777
778 #define REAL_YSCALEYUV2RGB_UV(index, c) \
779     "xor            "#index", "#index"  \n\t"\
780     ".p2align              4            \n\t"\
781     "1:                                 \n\t"\
782     "movq     (%2, "#index"), %%mm2     \n\t" /* uvbuf0[eax]*/\
783     "movq     (%3, "#index"), %%mm3     \n\t" /* uvbuf1[eax]*/\
784     "add "UV_OFF_BYTE"("#c"), "#index"  \n\t" \
785     "movq     (%2, "#index"), %%mm5     \n\t" /* uvbuf0[eax+2048]*/\
786     "movq     (%3, "#index"), %%mm4     \n\t" /* uvbuf1[eax+2048]*/\
787     "sub "UV_OFF_BYTE"("#c"), "#index"  \n\t" \
788     "psubw             %%mm3, %%mm2     \n\t" /* uvbuf0[eax] - uvbuf1[eax]*/\
789     "psubw             %%mm4, %%mm5     \n\t" /* uvbuf0[eax+2048] - uvbuf1[eax+2048]*/\
790     "movq "CHR_MMX_FILTER_OFFSET"+8("#c"), %%mm0    \n\t"\
791     "pmulhw            %%mm0, %%mm2     \n\t" /* (uvbuf0[eax] - uvbuf1[eax])uvalpha1>>16*/\
792     "pmulhw            %%mm0, %%mm5     \n\t" /* (uvbuf0[eax+2048] - uvbuf1[eax+2048])uvalpha1>>16*/\
793     "psraw                $4, %%mm3     \n\t" /* uvbuf0[eax] - uvbuf1[eax] >>4*/\
794     "psraw                $4, %%mm4     \n\t" /* uvbuf0[eax+2048] - uvbuf1[eax+2048] >>4*/\
795     "paddw             %%mm2, %%mm3     \n\t" /* uvbuf0[eax]uvalpha1 - uvbuf1[eax](1-uvalpha1)*/\
796     "paddw             %%mm5, %%mm4     \n\t" /* uvbuf0[eax+2048]uvalpha1 - uvbuf1[eax+2048](1-uvalpha1)*/\
797     "psubw  "U_OFFSET"("#c"), %%mm3     \n\t" /* (U-128)8*/\
798     "psubw  "V_OFFSET"("#c"), %%mm4     \n\t" /* (V-128)8*/\
799     "movq              %%mm3, %%mm2     \n\t" /* (U-128)8*/\
800     "movq              %%mm4, %%mm5     \n\t" /* (V-128)8*/\
801     "pmulhw "UG_COEFF"("#c"), %%mm3     \n\t"\
802     "pmulhw "VG_COEFF"("#c"), %%mm4     \n\t"\
803     /* mm2=(U-128)8, mm3=ug, mm4=vg mm5=(V-128)8 */\
804
805 #define REAL_YSCALEYUV2RGB_YA(index, c, b1, b2) \
806     "movq  ("#b1", "#index", 2), %%mm0     \n\t" /*buf0[eax]*/\
807     "movq  ("#b2", "#index", 2), %%mm1     \n\t" /*buf1[eax]*/\
808     "movq 8("#b1", "#index", 2), %%mm6     \n\t" /*buf0[eax]*/\
809     "movq 8("#b2", "#index", 2), %%mm7     \n\t" /*buf1[eax]*/\
810     "psubw             %%mm1, %%mm0     \n\t" /* buf0[eax] - buf1[eax]*/\
811     "psubw             %%mm7, %%mm6     \n\t" /* buf0[eax] - buf1[eax]*/\
812     "pmulhw "LUM_MMX_FILTER_OFFSET"+8("#c"), %%mm0  \n\t" /* (buf0[eax] - buf1[eax])yalpha1>>16*/\
813     "pmulhw "LUM_MMX_FILTER_OFFSET"+8("#c"), %%mm6  \n\t" /* (buf0[eax] - buf1[eax])yalpha1>>16*/\
814     "psraw                $4, %%mm1     \n\t" /* buf0[eax] - buf1[eax] >>4*/\
815     "psraw                $4, %%mm7     \n\t" /* buf0[eax] - buf1[eax] >>4*/\
816     "paddw             %%mm0, %%mm1     \n\t" /* buf0[eax]yalpha1 + buf1[eax](1-yalpha1) >>16*/\
817     "paddw             %%mm6, %%mm7     \n\t" /* buf0[eax]yalpha1 + buf1[eax](1-yalpha1) >>16*/\
818
819 #define REAL_YSCALEYUV2RGB_COEFF(c) \
820     "pmulhw "UB_COEFF"("#c"), %%mm2     \n\t"\
821     "pmulhw "VR_COEFF"("#c"), %%mm5     \n\t"\
822     "psubw  "Y_OFFSET"("#c"), %%mm1     \n\t" /* 8(Y-16)*/\
823     "psubw  "Y_OFFSET"("#c"), %%mm7     \n\t" /* 8(Y-16)*/\
824     "pmulhw  "Y_COEFF"("#c"), %%mm1     \n\t"\
825     "pmulhw  "Y_COEFF"("#c"), %%mm7     \n\t"\
826     /* mm1= Y1, mm2=ub, mm3=ug, mm4=vg mm5=vr, mm7=Y2 */\
827     "paddw             %%mm3, %%mm4     \n\t"\
828     "movq              %%mm2, %%mm0     \n\t"\
829     "movq              %%mm5, %%mm6     \n\t"\
830     "movq              %%mm4, %%mm3     \n\t"\
831     "punpcklwd         %%mm2, %%mm2     \n\t"\
832     "punpcklwd         %%mm5, %%mm5     \n\t"\
833     "punpcklwd         %%mm4, %%mm4     \n\t"\
834     "paddw             %%mm1, %%mm2     \n\t"\
835     "paddw             %%mm1, %%mm5     \n\t"\
836     "paddw             %%mm1, %%mm4     \n\t"\
837     "punpckhwd         %%mm0, %%mm0     \n\t"\
838     "punpckhwd         %%mm6, %%mm6     \n\t"\
839     "punpckhwd         %%mm3, %%mm3     \n\t"\
840     "paddw             %%mm7, %%mm0     \n\t"\
841     "paddw             %%mm7, %%mm6     \n\t"\
842     "paddw             %%mm7, %%mm3     \n\t"\
843     /* mm0=B1, mm2=B2, mm3=G2, mm4=G1, mm5=R1, mm6=R2 */\
844     "packuswb          %%mm0, %%mm2     \n\t"\
845     "packuswb          %%mm6, %%mm5     \n\t"\
846     "packuswb          %%mm3, %%mm4     \n\t"\
847
848 #define YSCALEYUV2RGB_YA(index, c, b1, b2) REAL_YSCALEYUV2RGB_YA(index, c, b1, b2)
849
850 #define YSCALEYUV2RGB(index, c) \
851     REAL_YSCALEYUV2RGB_UV(index, c) \
852     REAL_YSCALEYUV2RGB_YA(index, c, %0, %1) \
853     REAL_YSCALEYUV2RGB_COEFF(c)
854
855 /**
856  * vertical bilinear scale YV12 to RGB
857  */
858 static void RENAME(yuv2rgb32_2)(SwsContext *c, const int16_t *buf[2],
859                                 const int16_t *ubuf[2], const int16_t *vbuf[2],
860                                 const int16_t *abuf[2], uint8_t *dest,
861                                 int dstW, int yalpha, int uvalpha, int y)
862 {
863     const int16_t *buf0  = buf[0],  *buf1  = buf[1],
864                   *ubuf0 = ubuf[0], *ubuf1 = ubuf[1];
865
866     if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
867         const int16_t *abuf0 = abuf[0], *abuf1 = abuf[1];
868 #if ARCH_X86_64
869         __asm__ volatile(
870             YSCALEYUV2RGB(%%r8, %5)
871             YSCALEYUV2RGB_YA(%%r8, %5, %6, %7)
872             "psraw                  $3, %%mm1       \n\t" /* abuf0[eax] - abuf1[eax] >>7*/
873             "psraw                  $3, %%mm7       \n\t" /* abuf0[eax] - abuf1[eax] >>7*/
874             "packuswb            %%mm7, %%mm1       \n\t"
875             WRITEBGR32(%4, DSTW_OFFSET"(%5)", %%r8, %%mm2, %%mm4, %%mm5, %%mm1, %%mm0, %%mm7, %%mm3, %%mm6)
876             :: "c" (buf0), "d" (buf1), "S" (ubuf0), "D" (ubuf1), "r" (dest),
877                "a" (&c->redDither),
878                "r" (abuf0), "r" (abuf1)
879             : "%r8"
880         );
881 #else
882         c->u_temp=(intptr_t)abuf0;
883         c->v_temp=(intptr_t)abuf1;
884         __asm__ volatile(
885             "mov %%"REG_b", "ESP_OFFSET"(%5)        \n\t"
886             "mov        %4, %%"REG_b"               \n\t"
887             "push %%"REG_BP"                        \n\t"
888             YSCALEYUV2RGB(%%REGBP, %5)
889             "push                   %0              \n\t"
890             "push                   %1              \n\t"
891             "mov          "U_TEMP"(%5), %0          \n\t"
892             "mov          "V_TEMP"(%5), %1          \n\t"
893             YSCALEYUV2RGB_YA(%%REGBP, %5, %0, %1)
894             "psraw                  $3, %%mm1       \n\t" /* abuf0[eax] - abuf1[eax] >>7*/
895             "psraw                  $3, %%mm7       \n\t" /* abuf0[eax] - abuf1[eax] >>7*/
896             "packuswb            %%mm7, %%mm1       \n\t"
897             "pop                    %1              \n\t"
898             "pop                    %0              \n\t"
899             WRITEBGR32(%%REGb, DSTW_OFFSET"(%5)", %%REGBP, %%mm2, %%mm4, %%mm5, %%mm1, %%mm0, %%mm7, %%mm3, %%mm6)
900             "pop %%"REG_BP"                         \n\t"
901             "mov "ESP_OFFSET"(%5), %%"REG_b"        \n\t"
902             :: "c" (buf0), "d" (buf1), "S" (ubuf0), "D" (ubuf1), "m" (dest),
903                "a" (&c->redDither)
904         );
905 #endif
906     } else {
907         __asm__ volatile(
908             "mov %%"REG_b", "ESP_OFFSET"(%5)        \n\t"
909             "mov        %4, %%"REG_b"               \n\t"
910             "push %%"REG_BP"                        \n\t"
911             YSCALEYUV2RGB(%%REGBP, %5)
912             "pcmpeqd %%mm7, %%mm7                   \n\t"
913             WRITEBGR32(%%REGb, DSTW_OFFSET"(%5)", %%REGBP, %%mm2, %%mm4, %%mm5, %%mm7, %%mm0, %%mm1, %%mm3, %%mm6)
914             "pop %%"REG_BP"                         \n\t"
915             "mov "ESP_OFFSET"(%5), %%"REG_b"        \n\t"
916             :: "c" (buf0), "d" (buf1), "S" (ubuf0), "D" (ubuf1), "m" (dest),
917                "a" (&c->redDither)
918         );
919     }
920 }
921
922 static void RENAME(yuv2bgr24_2)(SwsContext *c, const int16_t *buf[2],
923                                 const int16_t *ubuf[2], const int16_t *vbuf[2],
924                                 const int16_t *abuf[2], uint8_t *dest,
925                                 int dstW, int yalpha, int uvalpha, int y)
926 {
927     const int16_t *buf0  = buf[0],  *buf1  = buf[1],
928                   *ubuf0 = ubuf[0], *ubuf1 = ubuf[1];
929
930     __asm__ volatile(
931         "mov %%"REG_b", "ESP_OFFSET"(%5)        \n\t"
932         "mov        %4, %%"REG_b"               \n\t"
933         "push %%"REG_BP"                        \n\t"
934         YSCALEYUV2RGB(%%REGBP, %5)
935         "pxor    %%mm7, %%mm7                   \n\t"
936         WRITEBGR24(%%REGb, DSTW_OFFSET"(%5)", %%REGBP)
937         "pop %%"REG_BP"                         \n\t"
938         "mov "ESP_OFFSET"(%5), %%"REG_b"        \n\t"
939         :: "c" (buf0), "d" (buf1), "S" (ubuf0), "D" (ubuf1), "m" (dest),
940            "a" (&c->redDither)
941            NAMED_CONSTRAINTS_ADD(ff_M24A,ff_M24C,ff_M24B)
942     );
943 }
944
945 static void RENAME(yuv2rgb555_2)(SwsContext *c, const int16_t *buf[2],
946                                  const int16_t *ubuf[2], const int16_t *vbuf[2],
947                                  const int16_t *abuf[2], uint8_t *dest,
948                                  int dstW, int yalpha, int uvalpha, int y)
949 {
950     const int16_t *buf0  = buf[0],  *buf1  = buf[1],
951                   *ubuf0 = ubuf[0], *ubuf1 = ubuf[1];
952
953     __asm__ volatile(
954         "mov %%"REG_b", "ESP_OFFSET"(%5)        \n\t"
955         "mov        %4, %%"REG_b"               \n\t"
956         "push %%"REG_BP"                        \n\t"
957         YSCALEYUV2RGB(%%REGBP, %5)
958         "pxor    %%mm7, %%mm7                   \n\t"
959         /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */
960 #ifdef DITHER1XBPP
961         "paddusb "BLUE_DITHER"(%5), %%mm2      \n\t"
962         "paddusb "GREEN_DITHER"(%5), %%mm4      \n\t"
963         "paddusb "RED_DITHER"(%5), %%mm5      \n\t"
964 #endif
965         WRITERGB15(%%REGb, DSTW_OFFSET"(%5)", %%REGBP)
966         "pop %%"REG_BP"                         \n\t"
967         "mov "ESP_OFFSET"(%5), %%"REG_b"        \n\t"
968         :: "c" (buf0), "d" (buf1), "S" (ubuf0), "D" (ubuf1), "m" (dest),
969            "a" (&c->redDither)
970            NAMED_CONSTRAINTS_ADD(bF8)
971     );
972 }
973
974 static void RENAME(yuv2rgb565_2)(SwsContext *c, const int16_t *buf[2],
975                                  const int16_t *ubuf[2], const int16_t *vbuf[2],
976                                  const int16_t *abuf[2], uint8_t *dest,
977                                  int dstW, int yalpha, int uvalpha, int y)
978 {
979     const int16_t *buf0  = buf[0],  *buf1  = buf[1],
980                   *ubuf0 = ubuf[0], *ubuf1 = ubuf[1];
981
982     __asm__ volatile(
983         "mov %%"REG_b", "ESP_OFFSET"(%5)        \n\t"
984         "mov        %4, %%"REG_b"               \n\t"
985         "push %%"REG_BP"                        \n\t"
986         YSCALEYUV2RGB(%%REGBP, %5)
987         "pxor    %%mm7, %%mm7                   \n\t"
988         /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */
989 #ifdef DITHER1XBPP
990         "paddusb "BLUE_DITHER"(%5), %%mm2      \n\t"
991         "paddusb "GREEN_DITHER"(%5), %%mm4      \n\t"
992         "paddusb "RED_DITHER"(%5), %%mm5      \n\t"
993 #endif
994         WRITERGB16(%%REGb, DSTW_OFFSET"(%5)", %%REGBP)
995         "pop %%"REG_BP"                         \n\t"
996         "mov "ESP_OFFSET"(%5), %%"REG_b"        \n\t"
997         :: "c" (buf0), "d" (buf1), "S" (ubuf0), "D" (ubuf1), "m" (dest),
998            "a" (&c->redDither)
999            NAMED_CONSTRAINTS_ADD(bF8,bFC)
1000     );
1001 }
1002
1003 #define REAL_YSCALEYUV2PACKED(index, c) \
1004     "movq "CHR_MMX_FILTER_OFFSET"+8("#c"), %%mm0              \n\t"\
1005     "movq "LUM_MMX_FILTER_OFFSET"+8("#c"), %%mm1              \n\t"\
1006     "psraw                $3, %%mm0                           \n\t"\
1007     "psraw                $3, %%mm1                           \n\t"\
1008     "movq              %%mm0, "CHR_MMX_FILTER_OFFSET"+8("#c") \n\t"\
1009     "movq              %%mm1, "LUM_MMX_FILTER_OFFSET"+8("#c") \n\t"\
1010     "xor            "#index", "#index"                        \n\t"\
1011     ".p2align              4            \n\t"\
1012     "1:                                 \n\t"\
1013     "movq     (%2, "#index"), %%mm2     \n\t" /* uvbuf0[eax]*/\
1014     "movq     (%3, "#index"), %%mm3     \n\t" /* uvbuf1[eax]*/\
1015     "add "UV_OFF_BYTE"("#c"), "#index"  \n\t" \
1016     "movq     (%2, "#index"), %%mm5     \n\t" /* uvbuf0[eax+2048]*/\
1017     "movq     (%3, "#index"), %%mm4     \n\t" /* uvbuf1[eax+2048]*/\
1018     "sub "UV_OFF_BYTE"("#c"), "#index"  \n\t" \
1019     "psubw             %%mm3, %%mm2     \n\t" /* uvbuf0[eax] - uvbuf1[eax]*/\
1020     "psubw             %%mm4, %%mm5     \n\t" /* uvbuf0[eax+2048] - uvbuf1[eax+2048]*/\
1021     "movq "CHR_MMX_FILTER_OFFSET"+8("#c"), %%mm0    \n\t"\
1022     "pmulhw            %%mm0, %%mm2     \n\t" /* (uvbuf0[eax] - uvbuf1[eax])uvalpha1>>16*/\
1023     "pmulhw            %%mm0, %%mm5     \n\t" /* (uvbuf0[eax+2048] - uvbuf1[eax+2048])uvalpha1>>16*/\
1024     "psraw                $7, %%mm3     \n\t" /* uvbuf0[eax] - uvbuf1[eax] >>4*/\
1025     "psraw                $7, %%mm4     \n\t" /* uvbuf0[eax+2048] - uvbuf1[eax+2048] >>4*/\
1026     "paddw             %%mm2, %%mm3     \n\t" /* uvbuf0[eax]uvalpha1 - uvbuf1[eax](1-uvalpha1)*/\
1027     "paddw             %%mm5, %%mm4     \n\t" /* uvbuf0[eax+2048]uvalpha1 - uvbuf1[eax+2048](1-uvalpha1)*/\
1028     "movq  (%0, "#index", 2), %%mm0     \n\t" /*buf0[eax]*/\
1029     "movq  (%1, "#index", 2), %%mm1     \n\t" /*buf1[eax]*/\
1030     "movq 8(%0, "#index", 2), %%mm6     \n\t" /*buf0[eax]*/\
1031     "movq 8(%1, "#index", 2), %%mm7     \n\t" /*buf1[eax]*/\
1032     "psubw             %%mm1, %%mm0     \n\t" /* buf0[eax] - buf1[eax]*/\
1033     "psubw             %%mm7, %%mm6     \n\t" /* buf0[eax] - buf1[eax]*/\
1034     "pmulhw "LUM_MMX_FILTER_OFFSET"+8("#c"), %%mm0  \n\t" /* (buf0[eax] - buf1[eax])yalpha1>>16*/\
1035     "pmulhw "LUM_MMX_FILTER_OFFSET"+8("#c"), %%mm6  \n\t" /* (buf0[eax] - buf1[eax])yalpha1>>16*/\
1036     "psraw                $7, %%mm1     \n\t" /* buf0[eax] - buf1[eax] >>4*/\
1037     "psraw                $7, %%mm7     \n\t" /* buf0[eax] - buf1[eax] >>4*/\
1038     "paddw             %%mm0, %%mm1     \n\t" /* buf0[eax]yalpha1 + buf1[eax](1-yalpha1) >>16*/\
1039     "paddw             %%mm6, %%mm7     \n\t" /* buf0[eax]yalpha1 + buf1[eax](1-yalpha1) >>16*/\
1040
1041 #define YSCALEYUV2PACKED(index, c)  REAL_YSCALEYUV2PACKED(index, c)
1042
1043 static void RENAME(yuv2yuyv422_2)(SwsContext *c, const int16_t *buf[2],
1044                                   const int16_t *ubuf[2], const int16_t *vbuf[2],
1045                                   const int16_t *abuf[2], uint8_t *dest,
1046                                   int dstW, int yalpha, int uvalpha, int y)
1047 {
1048     const int16_t *buf0  = buf[0],  *buf1  = buf[1],
1049                   *ubuf0 = ubuf[0], *ubuf1 = ubuf[1];
1050
1051     __asm__ volatile(
1052         "mov %%"REG_b", "ESP_OFFSET"(%5)        \n\t"
1053         "mov %4, %%"REG_b"                        \n\t"
1054         "push %%"REG_BP"                        \n\t"
1055         YSCALEYUV2PACKED(%%REGBP, %5)
1056         WRITEYUY2(%%REGb, DSTW_OFFSET"(%5)", %%REGBP)
1057         "pop %%"REG_BP"                         \n\t"
1058         "mov "ESP_OFFSET"(%5), %%"REG_b"        \n\t"
1059         :: "c" (buf0), "d" (buf1), "S" (ubuf0), "D" (ubuf1), "m" (dest),
1060            "a" (&c->redDither)
1061     );
1062 }
1063
1064 #define REAL_YSCALEYUV2RGB1(index, c) \
1065     "xor            "#index", "#index"  \n\t"\
1066     ".p2align              4            \n\t"\
1067     "1:                                 \n\t"\
1068     "movq     (%2, "#index"), %%mm3     \n\t" /* uvbuf0[eax]*/\
1069     "add "UV_OFF_BYTE"("#c"), "#index"  \n\t" \
1070     "movq     (%2, "#index"), %%mm4     \n\t" /* uvbuf0[eax+2048]*/\
1071     "sub "UV_OFF_BYTE"("#c"), "#index"  \n\t" \
1072     "psraw                $4, %%mm3     \n\t" /* uvbuf0[eax] - uvbuf1[eax] >>4*/\
1073     "psraw                $4, %%mm4     \n\t" /* uvbuf0[eax+2048] - uvbuf1[eax+2048] >>4*/\
1074     "psubw  "U_OFFSET"("#c"), %%mm3     \n\t" /* (U-128)8*/\
1075     "psubw  "V_OFFSET"("#c"), %%mm4     \n\t" /* (V-128)8*/\
1076     "movq              %%mm3, %%mm2     \n\t" /* (U-128)8*/\
1077     "movq              %%mm4, %%mm5     \n\t" /* (V-128)8*/\
1078     "pmulhw "UG_COEFF"("#c"), %%mm3     \n\t"\
1079     "pmulhw "VG_COEFF"("#c"), %%mm4     \n\t"\
1080     /* mm2=(U-128)8, mm3=ug, mm4=vg mm5=(V-128)8 */\
1081     "movq  (%0, "#index", 2), %%mm1     \n\t" /*buf0[eax]*/\
1082     "movq 8(%0, "#index", 2), %%mm7     \n\t" /*buf0[eax]*/\
1083     "psraw                $4, %%mm1     \n\t" /* buf0[eax] - buf1[eax] >>4*/\
1084     "psraw                $4, %%mm7     \n\t" /* buf0[eax] - buf1[eax] >>4*/\
1085     "pmulhw "UB_COEFF"("#c"), %%mm2     \n\t"\
1086     "pmulhw "VR_COEFF"("#c"), %%mm5     \n\t"\
1087     "psubw  "Y_OFFSET"("#c"), %%mm1     \n\t" /* 8(Y-16)*/\
1088     "psubw  "Y_OFFSET"("#c"), %%mm7     \n\t" /* 8(Y-16)*/\
1089     "pmulhw  "Y_COEFF"("#c"), %%mm1     \n\t"\
1090     "pmulhw  "Y_COEFF"("#c"), %%mm7     \n\t"\
1091     /* mm1= Y1, mm2=ub, mm3=ug, mm4=vg mm5=vr, mm7=Y2 */\
1092     "paddw             %%mm3, %%mm4     \n\t"\
1093     "movq              %%mm2, %%mm0     \n\t"\
1094     "movq              %%mm5, %%mm6     \n\t"\
1095     "movq              %%mm4, %%mm3     \n\t"\
1096     "punpcklwd         %%mm2, %%mm2     \n\t"\
1097     "punpcklwd         %%mm5, %%mm5     \n\t"\
1098     "punpcklwd         %%mm4, %%mm4     \n\t"\
1099     "paddw             %%mm1, %%mm2     \n\t"\
1100     "paddw             %%mm1, %%mm5     \n\t"\
1101     "paddw             %%mm1, %%mm4     \n\t"\
1102     "punpckhwd         %%mm0, %%mm0     \n\t"\
1103     "punpckhwd         %%mm6, %%mm6     \n\t"\
1104     "punpckhwd         %%mm3, %%mm3     \n\t"\
1105     "paddw             %%mm7, %%mm0     \n\t"\
1106     "paddw             %%mm7, %%mm6     \n\t"\
1107     "paddw             %%mm7, %%mm3     \n\t"\
1108     /* mm0=B1, mm2=B2, mm3=G2, mm4=G1, mm5=R1, mm6=R2 */\
1109     "packuswb          %%mm0, %%mm2     \n\t"\
1110     "packuswb          %%mm6, %%mm5     \n\t"\
1111     "packuswb          %%mm3, %%mm4     \n\t"\
1112
1113 #define YSCALEYUV2RGB1(index, c)  REAL_YSCALEYUV2RGB1(index, c)
1114
1115 // do vertical chrominance interpolation
1116 #define REAL_YSCALEYUV2RGB1b(index, c) \
1117     "xor            "#index", "#index"  \n\t"\
1118     ".p2align              4            \n\t"\
1119     "1:                                 \n\t"\
1120     "movq     (%2, "#index"), %%mm2     \n\t" /* uvbuf0[eax]*/\
1121     "movq     (%3, "#index"), %%mm3     \n\t" /* uvbuf1[eax]*/\
1122     "add "UV_OFF_BYTE"("#c"), "#index"  \n\t" \
1123     "movq     (%2, "#index"), %%mm5     \n\t" /* uvbuf0[eax+2048]*/\
1124     "movq     (%3, "#index"), %%mm4     \n\t" /* uvbuf1[eax+2048]*/\
1125     "sub "UV_OFF_BYTE"("#c"), "#index"  \n\t" \
1126     "paddw             %%mm2, %%mm3     \n\t" /* uvbuf0[eax] + uvbuf1[eax]*/\
1127     "paddw             %%mm5, %%mm4     \n\t" /* uvbuf0[eax+2048] + uvbuf1[eax+2048]*/\
1128     "psrlw                $5, %%mm3     \n\t" /*FIXME might overflow*/\
1129     "psrlw                $5, %%mm4     \n\t" /*FIXME might overflow*/\
1130     "psubw  "U_OFFSET"("#c"), %%mm3     \n\t" /* (U-128)8*/\
1131     "psubw  "V_OFFSET"("#c"), %%mm4     \n\t" /* (V-128)8*/\
1132     "movq              %%mm3, %%mm2     \n\t" /* (U-128)8*/\
1133     "movq              %%mm4, %%mm5     \n\t" /* (V-128)8*/\
1134     "pmulhw "UG_COEFF"("#c"), %%mm3     \n\t"\
1135     "pmulhw "VG_COEFF"("#c"), %%mm4     \n\t"\
1136     /* mm2=(U-128)8, mm3=ug, mm4=vg mm5=(V-128)8 */\
1137     "movq  (%0, "#index", 2), %%mm1     \n\t" /*buf0[eax]*/\
1138     "movq 8(%0, "#index", 2), %%mm7     \n\t" /*buf0[eax]*/\
1139     "psraw                $4, %%mm1     \n\t" /* buf0[eax] - buf1[eax] >>4*/\
1140     "psraw                $4, %%mm7     \n\t" /* buf0[eax] - buf1[eax] >>4*/\
1141     "pmulhw "UB_COEFF"("#c"), %%mm2     \n\t"\
1142     "pmulhw "VR_COEFF"("#c"), %%mm5     \n\t"\
1143     "psubw  "Y_OFFSET"("#c"), %%mm1     \n\t" /* 8(Y-16)*/\
1144     "psubw  "Y_OFFSET"("#c"), %%mm7     \n\t" /* 8(Y-16)*/\
1145     "pmulhw  "Y_COEFF"("#c"), %%mm1     \n\t"\
1146     "pmulhw  "Y_COEFF"("#c"), %%mm7     \n\t"\
1147     /* mm1= Y1, mm2=ub, mm3=ug, mm4=vg mm5=vr, mm7=Y2 */\
1148     "paddw             %%mm3, %%mm4     \n\t"\
1149     "movq              %%mm2, %%mm0     \n\t"\
1150     "movq              %%mm5, %%mm6     \n\t"\
1151     "movq              %%mm4, %%mm3     \n\t"\
1152     "punpcklwd         %%mm2, %%mm2     \n\t"\
1153     "punpcklwd         %%mm5, %%mm5     \n\t"\
1154     "punpcklwd         %%mm4, %%mm4     \n\t"\
1155     "paddw             %%mm1, %%mm2     \n\t"\
1156     "paddw             %%mm1, %%mm5     \n\t"\
1157     "paddw             %%mm1, %%mm4     \n\t"\
1158     "punpckhwd         %%mm0, %%mm0     \n\t"\
1159     "punpckhwd         %%mm6, %%mm6     \n\t"\
1160     "punpckhwd         %%mm3, %%mm3     \n\t"\
1161     "paddw             %%mm7, %%mm0     \n\t"\
1162     "paddw             %%mm7, %%mm6     \n\t"\
1163     "paddw             %%mm7, %%mm3     \n\t"\
1164     /* mm0=B1, mm2=B2, mm3=G2, mm4=G1, mm5=R1, mm6=R2 */\
1165     "packuswb          %%mm0, %%mm2     \n\t"\
1166     "packuswb          %%mm6, %%mm5     \n\t"\
1167     "packuswb          %%mm3, %%mm4     \n\t"\
1168
1169 #define YSCALEYUV2RGB1b(index, c)  REAL_YSCALEYUV2RGB1b(index, c)
1170
1171 #define REAL_YSCALEYUV2RGB1_ALPHA(index) \
1172     "movq  (%1, "#index", 2), %%mm7     \n\t" /* abuf0[index  ]     */\
1173     "movq 8(%1, "#index", 2), %%mm1     \n\t" /* abuf0[index+4]     */\
1174     "psraw                $7, %%mm7     \n\t" /* abuf0[index  ] >>7 */\
1175     "psraw                $7, %%mm1     \n\t" /* abuf0[index+4] >>7 */\
1176     "packuswb          %%mm1, %%mm7     \n\t"
1177 #define YSCALEYUV2RGB1_ALPHA(index) REAL_YSCALEYUV2RGB1_ALPHA(index)
1178
1179 /**
1180  * YV12 to RGB without scaling or interpolating
1181  */
1182 static void RENAME(yuv2rgb32_1)(SwsContext *c, const int16_t *buf0,
1183                                 const int16_t *ubuf[2], const int16_t *vbuf[2],
1184                                 const int16_t *abuf0, uint8_t *dest,
1185                                 int dstW, int uvalpha, int y)
1186 {
1187     const int16_t *ubuf0 = ubuf[0];
1188     const int16_t *buf1= buf0; //FIXME needed for RGB1/BGR1
1189
1190     if (uvalpha < 2048) { // note this is not correct (shifts chrominance by 0.5 pixels) but it is a bit faster
1191         const int16_t *ubuf1 = ubuf[0];
1192         if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
1193             __asm__ volatile(
1194                 "mov %%"REG_b", "ESP_OFFSET"(%5)        \n\t"
1195                 "mov        %4, %%"REG_b"               \n\t"
1196                 "push %%"REG_BP"                        \n\t"
1197                 YSCALEYUV2RGB1(%%REGBP, %5)
1198                 YSCALEYUV2RGB1_ALPHA(%%REGBP)
1199                 WRITEBGR32(%%REGb, DSTW_OFFSET"(%5)", %%REGBP, %%mm2, %%mm4, %%mm5, %%mm7, %%mm0, %%mm1, %%mm3, %%mm6)
1200                 "pop %%"REG_BP"                         \n\t"
1201                 "mov "ESP_OFFSET"(%5), %%"REG_b"        \n\t"
1202                 :: "c" (buf0), "d" (abuf0), "S" (ubuf0), "D" (ubuf1), "m" (dest),
1203                    "a" (&c->redDither)
1204             );
1205         } else {
1206             __asm__ volatile(
1207                 "mov %%"REG_b", "ESP_OFFSET"(%5)        \n\t"
1208                 "mov        %4, %%"REG_b"               \n\t"
1209                 "push %%"REG_BP"                        \n\t"
1210                 YSCALEYUV2RGB1(%%REGBP, %5)
1211                 "pcmpeqd %%mm7, %%mm7                   \n\t"
1212                 WRITEBGR32(%%REGb, DSTW_OFFSET"(%5)", %%REGBP, %%mm2, %%mm4, %%mm5, %%mm7, %%mm0, %%mm1, %%mm3, %%mm6)
1213                 "pop %%"REG_BP"                         \n\t"
1214                 "mov "ESP_OFFSET"(%5), %%"REG_b"        \n\t"
1215                 :: "c" (buf0), "d" (buf1), "S" (ubuf0), "D" (ubuf1), "m" (dest),
1216                    "a" (&c->redDither)
1217             );
1218         }
1219     } else {
1220         const int16_t *ubuf1 = ubuf[1];
1221         if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
1222             __asm__ volatile(
1223                 "mov %%"REG_b", "ESP_OFFSET"(%5)        \n\t"
1224                 "mov        %4, %%"REG_b"               \n\t"
1225                 "push %%"REG_BP"                        \n\t"
1226                 YSCALEYUV2RGB1b(%%REGBP, %5)
1227                 YSCALEYUV2RGB1_ALPHA(%%REGBP)
1228                 WRITEBGR32(%%REGb, DSTW_OFFSET"(%5)", %%REGBP, %%mm2, %%mm4, %%mm5, %%mm7, %%mm0, %%mm1, %%mm3, %%mm6)
1229                 "pop %%"REG_BP"                         \n\t"
1230                 "mov "ESP_OFFSET"(%5), %%"REG_b"        \n\t"
1231                 :: "c" (buf0), "d" (abuf0), "S" (ubuf0), "D" (ubuf1), "m" (dest),
1232                    "a" (&c->redDither)
1233             );
1234         } else {
1235             __asm__ volatile(
1236                 "mov %%"REG_b", "ESP_OFFSET"(%5)        \n\t"
1237                 "mov        %4, %%"REG_b"               \n\t"
1238                 "push %%"REG_BP"                        \n\t"
1239                 YSCALEYUV2RGB1b(%%REGBP, %5)
1240                 "pcmpeqd %%mm7, %%mm7                   \n\t"
1241                 WRITEBGR32(%%REGb, DSTW_OFFSET"(%5)", %%REGBP, %%mm2, %%mm4, %%mm5, %%mm7, %%mm0, %%mm1, %%mm3, %%mm6)
1242                 "pop %%"REG_BP"                         \n\t"
1243                 "mov "ESP_OFFSET"(%5), %%"REG_b"        \n\t"
1244                 :: "c" (buf0), "d" (buf1), "S" (ubuf0), "D" (ubuf1), "m" (dest),
1245                    "a" (&c->redDither)
1246             );
1247         }
1248     }
1249 }
1250
1251 static void RENAME(yuv2bgr24_1)(SwsContext *c, const int16_t *buf0,
1252                                 const int16_t *ubuf[2], const int16_t *vbuf[2],
1253                                 const int16_t *abuf0, uint8_t *dest,
1254                                 int dstW, int uvalpha, int y)
1255 {
1256     const int16_t *ubuf0 = ubuf[0];
1257     const int16_t *buf1= buf0; //FIXME needed for RGB1/BGR1
1258
1259     if (uvalpha < 2048) { // note this is not correct (shifts chrominance by 0.5 pixels) but it is a bit faster
1260         const int16_t *ubuf1 = ubuf[0];
1261         __asm__ volatile(
1262             "mov %%"REG_b", "ESP_OFFSET"(%5)        \n\t"
1263             "mov        %4, %%"REG_b"               \n\t"
1264             "push %%"REG_BP"                        \n\t"
1265             YSCALEYUV2RGB1(%%REGBP, %5)
1266             "pxor    %%mm7, %%mm7                   \n\t"
1267             WRITEBGR24(%%REGb, DSTW_OFFSET"(%5)", %%REGBP)
1268             "pop %%"REG_BP"                         \n\t"
1269             "mov "ESP_OFFSET"(%5), %%"REG_b"        \n\t"
1270             :: "c" (buf0), "d" (buf1), "S" (ubuf0), "D" (ubuf1), "m" (dest),
1271                "a" (&c->redDither)
1272                NAMED_CONSTRAINTS_ADD(ff_M24A,ff_M24C,ff_M24B)
1273         );
1274     } else {
1275         const int16_t *ubuf1 = ubuf[1];
1276         __asm__ volatile(
1277             "mov %%"REG_b", "ESP_OFFSET"(%5)        \n\t"
1278             "mov        %4, %%"REG_b"               \n\t"
1279             "push %%"REG_BP"                        \n\t"
1280             YSCALEYUV2RGB1b(%%REGBP, %5)
1281             "pxor    %%mm7, %%mm7                   \n\t"
1282             WRITEBGR24(%%REGb, DSTW_OFFSET"(%5)", %%REGBP)
1283             "pop %%"REG_BP"                         \n\t"
1284             "mov "ESP_OFFSET"(%5), %%"REG_b"        \n\t"
1285             :: "c" (buf0), "d" (buf1), "S" (ubuf0), "D" (ubuf1), "m" (dest),
1286                "a" (&c->redDither)
1287                NAMED_CONSTRAINTS_ADD(ff_M24A,ff_M24C,ff_M24B)
1288         );
1289     }
1290 }
1291
1292 static void RENAME(yuv2rgb555_1)(SwsContext *c, const int16_t *buf0,
1293                                  const int16_t *ubuf[2], const int16_t *vbuf[2],
1294                                  const int16_t *abuf0, uint8_t *dest,
1295                                  int dstW, int uvalpha, int y)
1296 {
1297     const int16_t *ubuf0 = ubuf[0];
1298     const int16_t *buf1= buf0; //FIXME needed for RGB1/BGR1
1299
1300     if (uvalpha < 2048) { // note this is not correct (shifts chrominance by 0.5 pixels) but it is a bit faster
1301         const int16_t *ubuf1 = ubuf[0];
1302         __asm__ volatile(
1303             "mov %%"REG_b", "ESP_OFFSET"(%5)        \n\t"
1304             "mov        %4, %%"REG_b"               \n\t"
1305             "push %%"REG_BP"                        \n\t"
1306             YSCALEYUV2RGB1(%%REGBP, %5)
1307             "pxor    %%mm7, %%mm7                   \n\t"
1308             /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */
1309 #ifdef DITHER1XBPP
1310             "paddusb "BLUE_DITHER"(%5), %%mm2      \n\t"
1311             "paddusb "GREEN_DITHER"(%5), %%mm4      \n\t"
1312             "paddusb "RED_DITHER"(%5), %%mm5      \n\t"
1313 #endif
1314             WRITERGB15(%%REGb, DSTW_OFFSET"(%5)", %%REGBP)
1315             "pop %%"REG_BP"                         \n\t"
1316             "mov "ESP_OFFSET"(%5), %%"REG_b"        \n\t"
1317             :: "c" (buf0), "d" (buf1), "S" (ubuf0), "D" (ubuf1), "m" (dest),
1318                "a" (&c->redDither)
1319                NAMED_CONSTRAINTS_ADD(bF8)
1320         );
1321     } else {
1322         const int16_t *ubuf1 = ubuf[1];
1323         __asm__ volatile(
1324             "mov %%"REG_b", "ESP_OFFSET"(%5)        \n\t"
1325             "mov        %4, %%"REG_b"               \n\t"
1326             "push %%"REG_BP"                        \n\t"
1327             YSCALEYUV2RGB1b(%%REGBP, %5)
1328             "pxor    %%mm7, %%mm7                   \n\t"
1329             /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */
1330 #ifdef DITHER1XBPP
1331             "paddusb "BLUE_DITHER"(%5), %%mm2      \n\t"
1332             "paddusb "GREEN_DITHER"(%5), %%mm4      \n\t"
1333             "paddusb "RED_DITHER"(%5), %%mm5      \n\t"
1334 #endif
1335             WRITERGB15(%%REGb, DSTW_OFFSET"(%5)", %%REGBP)
1336             "pop %%"REG_BP"                         \n\t"
1337             "mov "ESP_OFFSET"(%5), %%"REG_b"        \n\t"
1338             :: "c" (buf0), "d" (buf1), "S" (ubuf0), "D" (ubuf1), "m" (dest),
1339                "a" (&c->redDither)
1340                NAMED_CONSTRAINTS_ADD(bF8)
1341         );
1342     }
1343 }
1344
1345 static void RENAME(yuv2rgb565_1)(SwsContext *c, const int16_t *buf0,
1346                                  const int16_t *ubuf[2], const int16_t *vbuf[2],
1347                                  const int16_t *abuf0, uint8_t *dest,
1348                                  int dstW, int uvalpha, int y)
1349 {
1350     const int16_t *ubuf0 = ubuf[0];
1351     const int16_t *buf1= buf0; //FIXME needed for RGB1/BGR1
1352
1353     if (uvalpha < 2048) { // note this is not correct (shifts chrominance by 0.5 pixels) but it is a bit faster
1354         const int16_t *ubuf1 = ubuf[0];
1355         __asm__ volatile(
1356             "mov %%"REG_b", "ESP_OFFSET"(%5)        \n\t"
1357             "mov        %4, %%"REG_b"               \n\t"
1358             "push %%"REG_BP"                        \n\t"
1359             YSCALEYUV2RGB1(%%REGBP, %5)
1360             "pxor    %%mm7, %%mm7                   \n\t"
1361             /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */
1362 #ifdef DITHER1XBPP
1363             "paddusb "BLUE_DITHER"(%5), %%mm2      \n\t"
1364             "paddusb "GREEN_DITHER"(%5), %%mm4      \n\t"
1365             "paddusb "RED_DITHER"(%5), %%mm5      \n\t"
1366 #endif
1367             WRITERGB16(%%REGb, DSTW_OFFSET"(%5)", %%REGBP)
1368             "pop %%"REG_BP"                         \n\t"
1369             "mov "ESP_OFFSET"(%5), %%"REG_b"        \n\t"
1370             :: "c" (buf0), "d" (buf1), "S" (ubuf0), "D" (ubuf1), "m" (dest),
1371                "a" (&c->redDither)
1372                NAMED_CONSTRAINTS_ADD(bF8,bFC)
1373         );
1374     } else {
1375         const int16_t *ubuf1 = ubuf[1];
1376         __asm__ volatile(
1377             "mov %%"REG_b", "ESP_OFFSET"(%5)        \n\t"
1378             "mov        %4, %%"REG_b"               \n\t"
1379             "push %%"REG_BP"                        \n\t"
1380             YSCALEYUV2RGB1b(%%REGBP, %5)
1381             "pxor    %%mm7, %%mm7                   \n\t"
1382             /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */
1383 #ifdef DITHER1XBPP
1384             "paddusb "BLUE_DITHER"(%5), %%mm2      \n\t"
1385             "paddusb "GREEN_DITHER"(%5), %%mm4      \n\t"
1386             "paddusb "RED_DITHER"(%5), %%mm5      \n\t"
1387 #endif
1388             WRITERGB16(%%REGb, DSTW_OFFSET"(%5)", %%REGBP)
1389             "pop %%"REG_BP"                         \n\t"
1390             "mov "ESP_OFFSET"(%5), %%"REG_b"        \n\t"
1391             :: "c" (buf0), "d" (buf1), "S" (ubuf0), "D" (ubuf1), "m" (dest),
1392                "a" (&c->redDither)
1393                NAMED_CONSTRAINTS_ADD(bF8,bFC)
1394         );
1395     }
1396 }
1397
1398 #define REAL_YSCALEYUV2PACKED1(index, c) \
1399     "xor            "#index", "#index"  \n\t"\
1400     ".p2align              4            \n\t"\
1401     "1:                                 \n\t"\
1402     "movq     (%2, "#index"), %%mm3     \n\t" /* uvbuf0[eax]*/\
1403     "add "UV_OFF_BYTE"("#c"), "#index"  \n\t" \
1404     "movq     (%2, "#index"), %%mm4     \n\t" /* uvbuf0[eax+2048]*/\
1405     "sub "UV_OFF_BYTE"("#c"), "#index"  \n\t" \
1406     "psraw                $7, %%mm3     \n\t" \
1407     "psraw                $7, %%mm4     \n\t" \
1408     "movq  (%0, "#index", 2), %%mm1     \n\t" /*buf0[eax]*/\
1409     "movq 8(%0, "#index", 2), %%mm7     \n\t" /*buf0[eax]*/\
1410     "psraw                $7, %%mm1     \n\t" \
1411     "psraw                $7, %%mm7     \n\t" \
1412
1413 #define YSCALEYUV2PACKED1(index, c)  REAL_YSCALEYUV2PACKED1(index, c)
1414
1415 #define REAL_YSCALEYUV2PACKED1b(index, c) \
1416     "xor "#index", "#index"             \n\t"\
1417     ".p2align              4            \n\t"\
1418     "1:                                 \n\t"\
1419     "movq     (%2, "#index"), %%mm2     \n\t" /* uvbuf0[eax]*/\
1420     "movq     (%3, "#index"), %%mm3     \n\t" /* uvbuf1[eax]*/\
1421     "add "UV_OFF_BYTE"("#c"), "#index"  \n\t" \
1422     "movq     (%2, "#index"), %%mm5     \n\t" /* uvbuf0[eax+2048]*/\
1423     "movq     (%3, "#index"), %%mm4     \n\t" /* uvbuf1[eax+2048]*/\
1424     "sub "UV_OFF_BYTE"("#c"), "#index"  \n\t" \
1425     "paddw             %%mm2, %%mm3     \n\t" /* uvbuf0[eax] + uvbuf1[eax]*/\
1426     "paddw             %%mm5, %%mm4     \n\t" /* uvbuf0[eax+2048] + uvbuf1[eax+2048]*/\
1427     "psrlw                $8, %%mm3     \n\t" \
1428     "psrlw                $8, %%mm4     \n\t" \
1429     "movq  (%0, "#index", 2), %%mm1     \n\t" /*buf0[eax]*/\
1430     "movq 8(%0, "#index", 2), %%mm7     \n\t" /*buf0[eax]*/\
1431     "psraw                $7, %%mm1     \n\t" \
1432     "psraw                $7, %%mm7     \n\t"
1433 #define YSCALEYUV2PACKED1b(index, c)  REAL_YSCALEYUV2PACKED1b(index, c)
1434
1435 static void RENAME(yuv2yuyv422_1)(SwsContext *c, const int16_t *buf0,
1436                                   const int16_t *ubuf[2], const int16_t *vbuf[2],
1437                                   const int16_t *abuf0, uint8_t *dest,
1438                                   int dstW, int uvalpha, int y)
1439 {
1440     const int16_t *ubuf0 = ubuf[0];
1441     const int16_t *buf1= buf0; //FIXME needed for RGB1/BGR1
1442
1443     if (uvalpha < 2048) { // note this is not correct (shifts chrominance by 0.5 pixels) but it is a bit faster
1444         const int16_t *ubuf1 = ubuf[0];
1445         __asm__ volatile(
1446             "mov %%"REG_b", "ESP_OFFSET"(%5)        \n\t"
1447             "mov        %4, %%"REG_b"               \n\t"
1448             "push %%"REG_BP"                        \n\t"
1449             YSCALEYUV2PACKED1(%%REGBP, %5)
1450             WRITEYUY2(%%REGb, DSTW_OFFSET"(%5)", %%REGBP)
1451             "pop %%"REG_BP"                         \n\t"
1452             "mov "ESP_OFFSET"(%5), %%"REG_b"        \n\t"
1453             :: "c" (buf0), "d" (buf1), "S" (ubuf0), "D" (ubuf1), "m" (dest),
1454                "a" (&c->redDither)
1455         );
1456     } else {
1457         const int16_t *ubuf1 = ubuf[1];
1458         __asm__ volatile(
1459             "mov %%"REG_b", "ESP_OFFSET"(%5)        \n\t"
1460             "mov        %4, %%"REG_b"               \n\t"
1461             "push %%"REG_BP"                        \n\t"
1462             YSCALEYUV2PACKED1b(%%REGBP, %5)
1463             WRITEYUY2(%%REGb, DSTW_OFFSET"(%5)", %%REGBP)
1464             "pop %%"REG_BP"                         \n\t"
1465             "mov "ESP_OFFSET"(%5), %%"REG_b"        \n\t"
1466             :: "c" (buf0), "d" (buf1), "S" (ubuf0), "D" (ubuf1), "m" (dest),
1467                "a" (&c->redDither)
1468         );
1469     }
1470 }
1471
1472 #if COMPILE_TEMPLATE_MMXEXT
1473 static void RENAME(hyscale_fast)(SwsContext *c, int16_t *dst,
1474                                  int dstWidth, const uint8_t *src,
1475                                  int srcW, int xInc)
1476 {
1477     int32_t *filterPos = c->hLumFilterPos;
1478     int16_t *filter    = c->hLumFilter;
1479     void    *mmxextFilterCode = c->lumMmxextFilterCode;
1480     int i;
1481 #if defined(PIC)
1482     uint64_t ebxsave;
1483 #endif
1484 #if ARCH_X86_64
1485     uint64_t retsave;
1486 #endif
1487
1488     __asm__ volatile(
1489 #if defined(PIC)
1490         "mov               %%"REG_b", %5        \n\t"
1491 #if ARCH_X86_64
1492         "mov               -8(%%rsp), %%"REG_a" \n\t"
1493         "mov               %%"REG_a", %6        \n\t"
1494 #endif
1495 #else
1496 #if ARCH_X86_64
1497         "mov               -8(%%rsp), %%"REG_a" \n\t"
1498         "mov               %%"REG_a", %5        \n\t"
1499 #endif
1500 #endif
1501         "pxor                  %%mm7, %%mm7     \n\t"
1502         "mov                      %0, %%"REG_c" \n\t"
1503         "mov                      %1, %%"REG_D" \n\t"
1504         "mov                      %2, %%"REG_d" \n\t"
1505         "mov                      %3, %%"REG_b" \n\t"
1506         "xor               %%"REG_a", %%"REG_a" \n\t" // i
1507         PREFETCH"        (%%"REG_c")            \n\t"
1508         PREFETCH"      32(%%"REG_c")            \n\t"
1509         PREFETCH"      64(%%"REG_c")            \n\t"
1510
1511 #if ARCH_X86_64
1512 #define CALL_MMXEXT_FILTER_CODE \
1513         "movl            (%%"REG_b"), %%esi     \n\t"\
1514         "call                    *%4            \n\t"\
1515         "movl (%%"REG_b", %%"REG_a"), %%esi     \n\t"\
1516         "add               %%"REG_S", %%"REG_c" \n\t"\
1517         "add               %%"REG_a", %%"REG_D" \n\t"\
1518         "xor               %%"REG_a", %%"REG_a" \n\t"\
1519
1520 #else
1521 #define CALL_MMXEXT_FILTER_CODE \
1522         "movl (%%"REG_b"), %%esi        \n\t"\
1523         "call         *%4                       \n\t"\
1524         "addl (%%"REG_b", %%"REG_a"), %%"REG_c" \n\t"\
1525         "add               %%"REG_a", %%"REG_D" \n\t"\
1526         "xor               %%"REG_a", %%"REG_a" \n\t"\
1527
1528 #endif /* ARCH_X86_64 */
1529
1530         CALL_MMXEXT_FILTER_CODE
1531         CALL_MMXEXT_FILTER_CODE
1532         CALL_MMXEXT_FILTER_CODE
1533         CALL_MMXEXT_FILTER_CODE
1534         CALL_MMXEXT_FILTER_CODE
1535         CALL_MMXEXT_FILTER_CODE
1536         CALL_MMXEXT_FILTER_CODE
1537         CALL_MMXEXT_FILTER_CODE
1538
1539 #if defined(PIC)
1540         "mov                      %5, %%"REG_b" \n\t"
1541 #if ARCH_X86_64
1542         "mov                      %6, %%"REG_a" \n\t"
1543         "mov               %%"REG_a", -8(%%rsp) \n\t"
1544 #endif
1545 #else
1546 #if ARCH_X86_64
1547         "mov                      %5, %%"REG_a" \n\t"
1548         "mov               %%"REG_a", -8(%%rsp) \n\t"
1549 #endif
1550 #endif
1551         :: "m" (src), "m" (dst), "m" (filter), "m" (filterPos),
1552            "m" (mmxextFilterCode)
1553 #if defined(PIC)
1554           ,"m" (ebxsave)
1555 #endif
1556 #if ARCH_X86_64
1557           ,"m"(retsave)
1558 #endif
1559         : "%"REG_a, "%"REG_c, "%"REG_d, "%"REG_S, "%"REG_D
1560 #if !defined(PIC)
1561          ,"%"REG_b
1562 #endif
1563     );
1564
1565     for (i=dstWidth-1; (i*xInc)>>16 >=srcW-1; i--)
1566         dst[i] = src[srcW-1]*128;
1567 }
1568
1569 static void RENAME(hcscale_fast)(SwsContext *c, int16_t *dst1, int16_t *dst2,
1570                                  int dstWidth, const uint8_t *src1,
1571                                  const uint8_t *src2, int srcW, int xInc)
1572 {
1573     int32_t *filterPos = c->hChrFilterPos;
1574     int16_t *filter    = c->hChrFilter;
1575     void    *mmxextFilterCode = c->chrMmxextFilterCode;
1576     int i;
1577 #if defined(PIC)
1578     DECLARE_ALIGNED(8, uint64_t, ebxsave);
1579 #endif
1580 #if ARCH_X86_64
1581     DECLARE_ALIGNED(8, uint64_t, retsave);
1582 #endif
1583
1584     __asm__ volatile(
1585 #if defined(PIC)
1586         "mov          %%"REG_b", %7         \n\t"
1587 #if ARCH_X86_64
1588         "mov          -8(%%rsp), %%"REG_a"  \n\t"
1589         "mov          %%"REG_a", %8         \n\t"
1590 #endif
1591 #else
1592 #if ARCH_X86_64
1593         "mov          -8(%%rsp), %%"REG_a"  \n\t"
1594         "mov          %%"REG_a", %7         \n\t"
1595 #endif
1596 #endif
1597         "pxor             %%mm7, %%mm7      \n\t"
1598         "mov                 %0, %%"REG_c"  \n\t"
1599         "mov                 %1, %%"REG_D"  \n\t"
1600         "mov                 %2, %%"REG_d"  \n\t"
1601         "mov                 %3, %%"REG_b"  \n\t"
1602         "xor          %%"REG_a", %%"REG_a"  \n\t" // i
1603         PREFETCH"   (%%"REG_c")             \n\t"
1604         PREFETCH" 32(%%"REG_c")             \n\t"
1605         PREFETCH" 64(%%"REG_c")             \n\t"
1606
1607         CALL_MMXEXT_FILTER_CODE
1608         CALL_MMXEXT_FILTER_CODE
1609         CALL_MMXEXT_FILTER_CODE
1610         CALL_MMXEXT_FILTER_CODE
1611         "xor          %%"REG_a", %%"REG_a"  \n\t" // i
1612         "mov                 %5, %%"REG_c"  \n\t" // src
1613         "mov                 %6, %%"REG_D"  \n\t" // buf2
1614         PREFETCH"   (%%"REG_c")             \n\t"
1615         PREFETCH" 32(%%"REG_c")             \n\t"
1616         PREFETCH" 64(%%"REG_c")             \n\t"
1617
1618         CALL_MMXEXT_FILTER_CODE
1619         CALL_MMXEXT_FILTER_CODE
1620         CALL_MMXEXT_FILTER_CODE
1621         CALL_MMXEXT_FILTER_CODE
1622
1623 #if defined(PIC)
1624         "mov %7, %%"REG_b"    \n\t"
1625 #if ARCH_X86_64
1626         "mov                 %8, %%"REG_a"  \n\t"
1627         "mov          %%"REG_a", -8(%%rsp)  \n\t"
1628 #endif
1629 #else
1630 #if ARCH_X86_64
1631         "mov                 %7, %%"REG_a"  \n\t"
1632         "mov          %%"REG_a", -8(%%rsp)  \n\t"
1633 #endif
1634 #endif
1635         :: "m" (src1), "m" (dst1), "m" (filter), "m" (filterPos),
1636            "m" (mmxextFilterCode), "m" (src2), "m"(dst2)
1637 #if defined(PIC)
1638           ,"m" (ebxsave)
1639 #endif
1640 #if ARCH_X86_64
1641           ,"m"(retsave)
1642 #endif
1643         : "%"REG_a, "%"REG_c, "%"REG_d, "%"REG_S, "%"REG_D
1644 #if !defined(PIC)
1645          ,"%"REG_b
1646 #endif
1647     );
1648
1649     for (i=dstWidth-1; (i*xInc)>>16 >=srcW-1; i--) {
1650         dst1[i] = src1[srcW-1]*128;
1651         dst2[i] = src2[srcW-1]*128;
1652     }
1653 }
1654 #endif /* COMPILE_TEMPLATE_MMXEXT */
1655
1656 static av_cold void RENAME(sws_init_swscale)(SwsContext *c)
1657 {
1658     enum AVPixelFormat dstFormat = c->dstFormat;
1659
1660     c->use_mmx_vfilter= 0;
1661     if (!is16BPS(dstFormat) && !is9_OR_10BPS(dstFormat) && dstFormat != AV_PIX_FMT_NV12
1662         && dstFormat != AV_PIX_FMT_NV21 && !(c->flags & SWS_BITEXACT)) {
1663             if (c->flags & SWS_ACCURATE_RND) {
1664                 if (!(c->flags & SWS_FULL_CHR_H_INT)) {
1665                     switch (c->dstFormat) {
1666                     case AV_PIX_FMT_RGB32:   c->yuv2packedX = RENAME(yuv2rgb32_X_ar);   break;
1667                     case AV_PIX_FMT_BGR24:   c->yuv2packedX = RENAME(yuv2bgr24_X_ar);   break;
1668                     case AV_PIX_FMT_RGB555:  c->yuv2packedX = RENAME(yuv2rgb555_X_ar);  break;
1669                     case AV_PIX_FMT_RGB565:  c->yuv2packedX = RENAME(yuv2rgb565_X_ar);  break;
1670                     case AV_PIX_FMT_YUYV422: c->yuv2packedX = RENAME(yuv2yuyv422_X_ar); break;
1671                     default: break;
1672                     }
1673                 }
1674             } else {
1675                 c->use_mmx_vfilter= 1;
1676                 c->yuv2planeX = RENAME(yuv2yuvX    );
1677                 if (!(c->flags & SWS_FULL_CHR_H_INT)) {
1678                     switch (c->dstFormat) {
1679                     case AV_PIX_FMT_RGB32:   c->yuv2packedX = RENAME(yuv2rgb32_X);   break;
1680                     case AV_PIX_FMT_BGR24:   c->yuv2packedX = RENAME(yuv2bgr24_X);   break;
1681                     case AV_PIX_FMT_RGB555:  c->yuv2packedX = RENAME(yuv2rgb555_X);  break;
1682                     case AV_PIX_FMT_RGB565:  c->yuv2packedX = RENAME(yuv2rgb565_X);  break;
1683                     case AV_PIX_FMT_YUYV422: c->yuv2packedX = RENAME(yuv2yuyv422_X); break;
1684                     default: break;
1685                     }
1686                 }
1687             }
1688         if (!(c->flags & SWS_FULL_CHR_H_INT)) {
1689             switch (c->dstFormat) {
1690             case AV_PIX_FMT_RGB32:
1691                 c->yuv2packed1 = RENAME(yuv2rgb32_1);
1692                 c->yuv2packed2 = RENAME(yuv2rgb32_2);
1693                 break;
1694             case AV_PIX_FMT_BGR24:
1695                 c->yuv2packed1 = RENAME(yuv2bgr24_1);
1696                 c->yuv2packed2 = RENAME(yuv2bgr24_2);
1697                 break;
1698             case AV_PIX_FMT_RGB555:
1699                 c->yuv2packed1 = RENAME(yuv2rgb555_1);
1700                 c->yuv2packed2 = RENAME(yuv2rgb555_2);
1701                 break;
1702             case AV_PIX_FMT_RGB565:
1703                 c->yuv2packed1 = RENAME(yuv2rgb565_1);
1704                 c->yuv2packed2 = RENAME(yuv2rgb565_2);
1705                 break;
1706             case AV_PIX_FMT_YUYV422:
1707                 c->yuv2packed1 = RENAME(yuv2yuyv422_1);
1708                 c->yuv2packed2 = RENAME(yuv2yuyv422_2);
1709                 break;
1710             default:
1711                 break;
1712             }
1713         }
1714     }
1715
1716     if (c->srcBpc == 8 && c->dstBpc <= 14) {
1717     // Use the new MMX scaler if the MMXEXT one can't be used (it is faster than the x86 ASM one).
1718 #if COMPILE_TEMPLATE_MMXEXT
1719     if (c->flags & SWS_FAST_BILINEAR && c->canMMXEXTBeUsed) {
1720         c->hyscale_fast = RENAME(hyscale_fast);
1721         c->hcscale_fast = RENAME(hcscale_fast);
1722     } else {
1723 #endif /* COMPILE_TEMPLATE_MMXEXT */
1724         c->hyscale_fast = NULL;
1725         c->hcscale_fast = NULL;
1726 #if COMPILE_TEMPLATE_MMXEXT
1727     }
1728 #endif /* COMPILE_TEMPLATE_MMXEXT */
1729     }
1730 }