]> git.sesse.net Git - ffmpeg/blob - libavcodec/x86/dsputil_mmx.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavcodec / x86 / dsputil_mmx.c
1 /*
2  * MMX optimized DSP utils
3  * Copyright (c) 2000, 2001 Fabrice Bellard
4  * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  *
22  * MMX optimization by Nick Kurshev <nickols_k@mail.ru>
23  */
24
25 #include "libavutil/cpu.h"
26 #include "libavutil/x86_cpu.h"
27 #include "libavcodec/dsputil.h"
28 #include "libavcodec/h264dsp.h"
29 #include "libavcodec/mpegvideo.h"
30 #include "libavcodec/simple_idct.h"
31 #include "libavcodec/ac3dec.h"
32 #include "dsputil_mmx.h"
33 #include "idct_xvid.h"
34
35 //#undef NDEBUG
36 //#include <assert.h>
37
38 /* pixel operations */
39 DECLARE_ALIGNED(8,  const uint64_t, ff_bone) = 0x0101010101010101ULL;
40 DECLARE_ALIGNED(8,  const uint64_t, ff_wtwo) = 0x0002000200020002ULL;
41
42 DECLARE_ALIGNED(16, const uint64_t, ff_pdw_80000000)[2] =
43 {0x8000000080000000ULL, 0x8000000080000000ULL};
44
45 DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_1  ) = {0x0001000100010001ULL, 0x0001000100010001ULL};
46 DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_2  ) = {0x0002000200020002ULL, 0x0002000200020002ULL};
47 DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_3  ) = {0x0003000300030003ULL, 0x0003000300030003ULL};
48 DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_4  ) = {0x0004000400040004ULL, 0x0004000400040004ULL};
49 DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_5  ) = {0x0005000500050005ULL, 0x0005000500050005ULL};
50 DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_8  ) = {0x0008000800080008ULL, 0x0008000800080008ULL};
51 DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_9  ) = {0x0009000900090009ULL, 0x0009000900090009ULL};
52 DECLARE_ALIGNED(8,  const uint64_t, ff_pw_15 ) = 0x000F000F000F000FULL;
53 DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_16 ) = {0x0010001000100010ULL, 0x0010001000100010ULL};
54 DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_17 ) = {0x0011001100110011ULL, 0x0011001100110011ULL};
55 DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_18 ) = {0x0012001200120012ULL, 0x0012001200120012ULL};
56 DECLARE_ALIGNED(8,  const uint64_t, ff_pw_20 ) = 0x0014001400140014ULL;
57 DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_27 ) = {0x001B001B001B001BULL, 0x001B001B001B001BULL};
58 DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_28 ) = {0x001C001C001C001CULL, 0x001C001C001C001CULL};
59 DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_32 ) = {0x0020002000200020ULL, 0x0020002000200020ULL};
60 DECLARE_ALIGNED(8,  const uint64_t, ff_pw_42 ) = 0x002A002A002A002AULL;
61 DECLARE_ALIGNED(8,  const uint64_t, ff_pw_53 ) = 0x0035003500350035ULL;
62 DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_63 ) = {0x003F003F003F003FULL, 0x003F003F003F003FULL};
63 DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_64 ) = {0x0040004000400040ULL, 0x0040004000400040ULL};
64 DECLARE_ALIGNED(8,  const uint64_t, ff_pw_96 ) = 0x0060006000600060ULL;
65 DECLARE_ALIGNED(8,  const uint64_t, ff_pw_128) = 0x0080008000800080ULL;
66 DECLARE_ALIGNED(8,  const uint64_t, ff_pw_255) = 0x00ff00ff00ff00ffULL;
67
68 DECLARE_ALIGNED(16, const xmm_reg,  ff_pb_0  ) = {0x0000000000000000ULL, 0x0000000000000000ULL};
69 DECLARE_ALIGNED(16, const xmm_reg,  ff_pb_1  ) = {0x0101010101010101ULL, 0x0101010101010101ULL};
70 DECLARE_ALIGNED(16, const xmm_reg,  ff_pb_3  ) = {0x0303030303030303ULL, 0x0303030303030303ULL};
71 DECLARE_ALIGNED(16, const xmm_reg,  ff_pb_4  ) = {0x0404040404040404ULL, 0x0404040404040404ULL};
72 DECLARE_ALIGNED(8,  const uint64_t, ff_pb_7  ) = 0x0707070707070707ULL;
73 DECLARE_ALIGNED(8,  const uint64_t, ff_pb_1F ) = 0x1F1F1F1F1F1F1F1FULL;
74 DECLARE_ALIGNED(8,  const uint64_t, ff_pb_3F ) = 0x3F3F3F3F3F3F3F3FULL;
75 DECLARE_ALIGNED(16, const xmm_reg,  ff_pb_80 ) = {0x8080808080808080ULL, 0x8080808080808080ULL};
76 DECLARE_ALIGNED(8,  const uint64_t, ff_pb_81 ) = 0x8181818181818181ULL;
77 DECLARE_ALIGNED(16, const xmm_reg,  ff_pb_A1 ) = {0xA1A1A1A1A1A1A1A1ULL, 0xA1A1A1A1A1A1A1A1ULL};
78 DECLARE_ALIGNED(16, const xmm_reg,  ff_pb_F8 ) = {0xF8F8F8F8F8F8F8F8ULL, 0xF8F8F8F8F8F8F8F8ULL};
79 DECLARE_ALIGNED(8,  const uint64_t, ff_pb_FC ) = 0xFCFCFCFCFCFCFCFCULL;
80 DECLARE_ALIGNED(16, const xmm_reg,  ff_pb_FE ) = {0xFEFEFEFEFEFEFEFEULL, 0xFEFEFEFEFEFEFEFEULL};
81
82 DECLARE_ALIGNED(16, const double, ff_pd_1)[2] = { 1.0, 1.0 };
83 DECLARE_ALIGNED(16, const double, ff_pd_2)[2] = { 2.0, 2.0 };
84
85 #define JUMPALIGN() __asm__ volatile (".p2align 3"::)
86 #define MOVQ_ZERO(regd)  __asm__ volatile ("pxor %%" #regd ", %%" #regd ::)
87
88 #define MOVQ_BFE(regd) \
89     __asm__ volatile ( \
90     "pcmpeqd %%" #regd ", %%" #regd " \n\t"\
91     "paddb %%" #regd ", %%" #regd " \n\t" ::)
92
93 #ifndef PIC
94 #define MOVQ_BONE(regd)  __asm__ volatile ("movq %0, %%" #regd " \n\t" ::"m"(ff_bone))
95 #define MOVQ_WTWO(regd)  __asm__ volatile ("movq %0, %%" #regd " \n\t" ::"m"(ff_wtwo))
96 #else
97 // for shared library it's better to use this way for accessing constants
98 // pcmpeqd -> -1
99 #define MOVQ_BONE(regd) \
100     __asm__ volatile ( \
101     "pcmpeqd %%" #regd ", %%" #regd " \n\t" \
102     "psrlw $15, %%" #regd " \n\t" \
103     "packuswb %%" #regd ", %%" #regd " \n\t" ::)
104
105 #define MOVQ_WTWO(regd) \
106     __asm__ volatile ( \
107     "pcmpeqd %%" #regd ", %%" #regd " \n\t" \
108     "psrlw $15, %%" #regd " \n\t" \
109     "psllw $1, %%" #regd " \n\t"::)
110
111 #endif
112
113 // using regr as temporary and for the output result
114 // first argument is unmodifed and second is trashed
115 // regfe is supposed to contain 0xfefefefefefefefe
116 #define PAVGB_MMX_NO_RND(rega, regb, regr, regfe) \
117     "movq " #rega ", " #regr "  \n\t"\
118     "pand " #regb ", " #regr "  \n\t"\
119     "pxor " #rega ", " #regb "  \n\t"\
120     "pand " #regfe "," #regb "  \n\t"\
121     "psrlq $1, " #regb "        \n\t"\
122     "paddb " #regb ", " #regr " \n\t"
123
124 #define PAVGB_MMX(rega, regb, regr, regfe) \
125     "movq " #rega ", " #regr "  \n\t"\
126     "por  " #regb ", " #regr "  \n\t"\
127     "pxor " #rega ", " #regb "  \n\t"\
128     "pand " #regfe "," #regb "  \n\t"\
129     "psrlq $1, " #regb "        \n\t"\
130     "psubb " #regb ", " #regr " \n\t"
131
132 // mm6 is supposed to contain 0xfefefefefefefefe
133 #define PAVGBP_MMX_NO_RND(rega, regb, regr,  regc, regd, regp) \
134     "movq " #rega ", " #regr "  \n\t"\
135     "movq " #regc ", " #regp "  \n\t"\
136     "pand " #regb ", " #regr "  \n\t"\
137     "pand " #regd ", " #regp "  \n\t"\
138     "pxor " #rega ", " #regb "  \n\t"\
139     "pxor " #regc ", " #regd "  \n\t"\
140     "pand %%mm6, " #regb "      \n\t"\
141     "pand %%mm6, " #regd "      \n\t"\
142     "psrlq $1, " #regb "        \n\t"\
143     "psrlq $1, " #regd "        \n\t"\
144     "paddb " #regb ", " #regr " \n\t"\
145     "paddb " #regd ", " #regp " \n\t"
146
147 #define PAVGBP_MMX(rega, regb, regr, regc, regd, regp) \
148     "movq " #rega ", " #regr "  \n\t"\
149     "movq " #regc ", " #regp "  \n\t"\
150     "por  " #regb ", " #regr "  \n\t"\
151     "por  " #regd ", " #regp "  \n\t"\
152     "pxor " #rega ", " #regb "  \n\t"\
153     "pxor " #regc ", " #regd "  \n\t"\
154     "pand %%mm6, " #regb "      \n\t"\
155     "pand %%mm6, " #regd "      \n\t"\
156     "psrlq $1, " #regd "        \n\t"\
157     "psrlq $1, " #regb "        \n\t"\
158     "psubb " #regb ", " #regr " \n\t"\
159     "psubb " #regd ", " #regp " \n\t"
160
161 /***********************************/
162 /* MMX no rounding */
163 #define DEF(x, y) x ## _no_rnd_ ## y ##_mmx
164 #define SET_RND  MOVQ_WONE
165 #define PAVGBP(a, b, c, d, e, f)        PAVGBP_MMX_NO_RND(a, b, c, d, e, f)
166 #define PAVGB(a, b, c, e)               PAVGB_MMX_NO_RND(a, b, c, e)
167 #define OP_AVG(a, b, c, e)              PAVGB_MMX(a, b, c, e)
168
169 #include "dsputil_mmx_rnd_template.c"
170
171 #undef DEF
172 #undef SET_RND
173 #undef PAVGBP
174 #undef PAVGB
175 /***********************************/
176 /* MMX rounding */
177
178 #define DEF(x, y) x ## _ ## y ##_mmx
179 #define SET_RND  MOVQ_WTWO
180 #define PAVGBP(a, b, c, d, e, f)        PAVGBP_MMX(a, b, c, d, e, f)
181 #define PAVGB(a, b, c, e)               PAVGB_MMX(a, b, c, e)
182
183 #include "dsputil_mmx_rnd_template.c"
184
185 #undef DEF
186 #undef SET_RND
187 #undef PAVGBP
188 #undef PAVGB
189 #undef OP_AVG
190
191 /***********************************/
192 /* 3Dnow specific */
193
194 #define DEF(x) x ## _3dnow
195 #define PAVGB "pavgusb"
196 #define OP_AVG PAVGB
197
198 #include "dsputil_mmx_avg_template.c"
199
200 #undef DEF
201 #undef PAVGB
202 #undef OP_AVG
203
204 /***********************************/
205 /* MMX2 specific */
206
207 #define DEF(x) x ## _mmx2
208
209 /* Introduced only in MMX2 set */
210 #define PAVGB "pavgb"
211 #define OP_AVG PAVGB
212
213 #include "dsputil_mmx_avg_template.c"
214
215 #undef DEF
216 #undef PAVGB
217 #undef OP_AVG
218
219 #define put_no_rnd_pixels16_mmx put_pixels16_mmx
220 #define put_no_rnd_pixels8_mmx put_pixels8_mmx
221 #define put_pixels16_mmx2 put_pixels16_mmx
222 #define put_pixels8_mmx2 put_pixels8_mmx
223 #define put_pixels4_mmx2 put_pixels4_mmx
224 #define put_no_rnd_pixels16_mmx2 put_no_rnd_pixels16_mmx
225 #define put_no_rnd_pixels8_mmx2 put_no_rnd_pixels8_mmx
226 #define put_pixels16_3dnow put_pixels16_mmx
227 #define put_pixels8_3dnow put_pixels8_mmx
228 #define put_pixels4_3dnow put_pixels4_mmx
229 #define put_no_rnd_pixels16_3dnow put_no_rnd_pixels16_mmx
230 #define put_no_rnd_pixels8_3dnow put_no_rnd_pixels8_mmx
231
232 /***********************************/
233 /* standard MMX */
234
235 void ff_put_pixels_clamped_mmx(const DCTELEM *block, uint8_t *pixels, int line_size)
236 {
237     const DCTELEM *p;
238     uint8_t *pix;
239
240     /* read the pixels */
241     p = block;
242     pix = pixels;
243     /* unrolled loop */
244         __asm__ volatile(
245                 "movq   %3, %%mm0               \n\t"
246                 "movq   8%3, %%mm1              \n\t"
247                 "movq   16%3, %%mm2             \n\t"
248                 "movq   24%3, %%mm3             \n\t"
249                 "movq   32%3, %%mm4             \n\t"
250                 "movq   40%3, %%mm5             \n\t"
251                 "movq   48%3, %%mm6             \n\t"
252                 "movq   56%3, %%mm7             \n\t"
253                 "packuswb %%mm1, %%mm0          \n\t"
254                 "packuswb %%mm3, %%mm2          \n\t"
255                 "packuswb %%mm5, %%mm4          \n\t"
256                 "packuswb %%mm7, %%mm6          \n\t"
257                 "movq   %%mm0, (%0)             \n\t"
258                 "movq   %%mm2, (%0, %1)         \n\t"
259                 "movq   %%mm4, (%0, %1, 2)      \n\t"
260                 "movq   %%mm6, (%0, %2)         \n\t"
261                 ::"r" (pix), "r" ((x86_reg)line_size), "r" ((x86_reg)line_size*3), "m"(*p)
262                 :"memory");
263         pix += line_size*4;
264         p += 32;
265
266     // if here would be an exact copy of the code above
267     // compiler would generate some very strange code
268     // thus using "r"
269     __asm__ volatile(
270             "movq       (%3), %%mm0             \n\t"
271             "movq       8(%3), %%mm1            \n\t"
272             "movq       16(%3), %%mm2           \n\t"
273             "movq       24(%3), %%mm3           \n\t"
274             "movq       32(%3), %%mm4           \n\t"
275             "movq       40(%3), %%mm5           \n\t"
276             "movq       48(%3), %%mm6           \n\t"
277             "movq       56(%3), %%mm7           \n\t"
278             "packuswb %%mm1, %%mm0              \n\t"
279             "packuswb %%mm3, %%mm2              \n\t"
280             "packuswb %%mm5, %%mm4              \n\t"
281             "packuswb %%mm7, %%mm6              \n\t"
282             "movq       %%mm0, (%0)             \n\t"
283             "movq       %%mm2, (%0, %1)         \n\t"
284             "movq       %%mm4, (%0, %1, 2)      \n\t"
285             "movq       %%mm6, (%0, %2)         \n\t"
286             ::"r" (pix), "r" ((x86_reg)line_size), "r" ((x86_reg)line_size*3), "r"(p)
287             :"memory");
288 }
289
290 #define put_signed_pixels_clamped_mmx_half(off) \
291             "movq    "#off"(%2), %%mm1          \n\t"\
292             "movq 16+"#off"(%2), %%mm2          \n\t"\
293             "movq 32+"#off"(%2), %%mm3          \n\t"\
294             "movq 48+"#off"(%2), %%mm4          \n\t"\
295             "packsswb  8+"#off"(%2), %%mm1      \n\t"\
296             "packsswb 24+"#off"(%2), %%mm2      \n\t"\
297             "packsswb 40+"#off"(%2), %%mm3      \n\t"\
298             "packsswb 56+"#off"(%2), %%mm4      \n\t"\
299             "paddb %%mm0, %%mm1                 \n\t"\
300             "paddb %%mm0, %%mm2                 \n\t"\
301             "paddb %%mm0, %%mm3                 \n\t"\
302             "paddb %%mm0, %%mm4                 \n\t"\
303             "movq %%mm1, (%0)                   \n\t"\
304             "movq %%mm2, (%0, %3)               \n\t"\
305             "movq %%mm3, (%0, %3, 2)            \n\t"\
306             "movq %%mm4, (%0, %1)               \n\t"
307
308 void ff_put_signed_pixels_clamped_mmx(const DCTELEM *block, uint8_t *pixels, int line_size)
309 {
310     x86_reg line_skip = line_size;
311     x86_reg line_skip3;
312
313     __asm__ volatile (
314             "movq "MANGLE(ff_pb_80)", %%mm0 \n\t"
315             "lea (%3, %3, 2), %1                \n\t"
316             put_signed_pixels_clamped_mmx_half(0)
317             "lea (%0, %3, 4), %0                \n\t"
318             put_signed_pixels_clamped_mmx_half(64)
319             :"+&r" (pixels), "=&r" (line_skip3)
320             :"r" (block), "r"(line_skip)
321             :"memory");
322 }
323
324 void ff_add_pixels_clamped_mmx(const DCTELEM *block, uint8_t *pixels, int line_size)
325 {
326     const DCTELEM *p;
327     uint8_t *pix;
328     int i;
329
330     /* read the pixels */
331     p = block;
332     pix = pixels;
333     MOVQ_ZERO(mm7);
334     i = 4;
335     do {
336         __asm__ volatile(
337                 "movq   (%2), %%mm0     \n\t"
338                 "movq   8(%2), %%mm1    \n\t"
339                 "movq   16(%2), %%mm2   \n\t"
340                 "movq   24(%2), %%mm3   \n\t"
341                 "movq   %0, %%mm4       \n\t"
342                 "movq   %1, %%mm6       \n\t"
343                 "movq   %%mm4, %%mm5    \n\t"
344                 "punpcklbw %%mm7, %%mm4 \n\t"
345                 "punpckhbw %%mm7, %%mm5 \n\t"
346                 "paddsw %%mm4, %%mm0    \n\t"
347                 "paddsw %%mm5, %%mm1    \n\t"
348                 "movq   %%mm6, %%mm5    \n\t"
349                 "punpcklbw %%mm7, %%mm6 \n\t"
350                 "punpckhbw %%mm7, %%mm5 \n\t"
351                 "paddsw %%mm6, %%mm2    \n\t"
352                 "paddsw %%mm5, %%mm3    \n\t"
353                 "packuswb %%mm1, %%mm0  \n\t"
354                 "packuswb %%mm3, %%mm2  \n\t"
355                 "movq   %%mm0, %0       \n\t"
356                 "movq   %%mm2, %1       \n\t"
357                 :"+m"(*pix), "+m"(*(pix+line_size))
358                 :"r"(p)
359                 :"memory");
360         pix += line_size*2;
361         p += 16;
362     } while (--i);
363 }
364
365 static void put_pixels4_mmx(uint8_t *block, const uint8_t *pixels, int line_size, int h)
366 {
367     __asm__ volatile(
368          "lea (%3, %3), %%"REG_a"       \n\t"
369          ".p2align 3                    \n\t"
370          "1:                            \n\t"
371          "movd (%1), %%mm0              \n\t"
372          "movd (%1, %3), %%mm1          \n\t"
373          "movd %%mm0, (%2)              \n\t"
374          "movd %%mm1, (%2, %3)          \n\t"
375          "add %%"REG_a", %1             \n\t"
376          "add %%"REG_a", %2             \n\t"
377          "movd (%1), %%mm0              \n\t"
378          "movd (%1, %3), %%mm1          \n\t"
379          "movd %%mm0, (%2)              \n\t"
380          "movd %%mm1, (%2, %3)          \n\t"
381          "add %%"REG_a", %1             \n\t"
382          "add %%"REG_a", %2             \n\t"
383          "subl $4, %0                   \n\t"
384          "jnz 1b                        \n\t"
385          : "+g"(h), "+r" (pixels),  "+r" (block)
386          : "r"((x86_reg)line_size)
387          : "%"REG_a, "memory"
388         );
389 }
390
391 static void put_pixels8_mmx(uint8_t *block, const uint8_t *pixels, int line_size, int h)
392 {
393     __asm__ volatile(
394          "lea (%3, %3), %%"REG_a"       \n\t"
395          ".p2align 3                    \n\t"
396          "1:                            \n\t"
397          "movq (%1), %%mm0              \n\t"
398          "movq (%1, %3), %%mm1          \n\t"
399          "movq %%mm0, (%2)              \n\t"
400          "movq %%mm1, (%2, %3)          \n\t"
401          "add %%"REG_a", %1             \n\t"
402          "add %%"REG_a", %2             \n\t"
403          "movq (%1), %%mm0              \n\t"
404          "movq (%1, %3), %%mm1          \n\t"
405          "movq %%mm0, (%2)              \n\t"
406          "movq %%mm1, (%2, %3)          \n\t"
407          "add %%"REG_a", %1             \n\t"
408          "add %%"REG_a", %2             \n\t"
409          "subl $4, %0                   \n\t"
410          "jnz 1b                        \n\t"
411          : "+g"(h), "+r" (pixels),  "+r" (block)
412          : "r"((x86_reg)line_size)
413          : "%"REG_a, "memory"
414         );
415 }
416
417 static void put_pixels16_mmx(uint8_t *block, const uint8_t *pixels, int line_size, int h)
418 {
419     __asm__ volatile(
420          "lea (%3, %3), %%"REG_a"       \n\t"
421          ".p2align 3                    \n\t"
422          "1:                            \n\t"
423          "movq (%1), %%mm0              \n\t"
424          "movq 8(%1), %%mm4             \n\t"
425          "movq (%1, %3), %%mm1          \n\t"
426          "movq 8(%1, %3), %%mm5         \n\t"
427          "movq %%mm0, (%2)              \n\t"
428          "movq %%mm4, 8(%2)             \n\t"
429          "movq %%mm1, (%2, %3)          \n\t"
430          "movq %%mm5, 8(%2, %3)         \n\t"
431          "add %%"REG_a", %1             \n\t"
432          "add %%"REG_a", %2             \n\t"
433          "movq (%1), %%mm0              \n\t"
434          "movq 8(%1), %%mm4             \n\t"
435          "movq (%1, %3), %%mm1          \n\t"
436          "movq 8(%1, %3), %%mm5         \n\t"
437          "movq %%mm0, (%2)              \n\t"
438          "movq %%mm4, 8(%2)             \n\t"
439          "movq %%mm1, (%2, %3)          \n\t"
440          "movq %%mm5, 8(%2, %3)         \n\t"
441          "add %%"REG_a", %1             \n\t"
442          "add %%"REG_a", %2             \n\t"
443          "subl $4, %0                   \n\t"
444          "jnz 1b                        \n\t"
445          : "+g"(h), "+r" (pixels),  "+r" (block)
446          : "r"((x86_reg)line_size)
447          : "%"REG_a, "memory"
448         );
449 }
450
451 static void put_pixels16_sse2(uint8_t *block, const uint8_t *pixels, int line_size, int h)
452 {
453     __asm__ volatile(
454          "1:                            \n\t"
455          "movdqu (%1), %%xmm0           \n\t"
456          "movdqu (%1,%3), %%xmm1        \n\t"
457          "movdqu (%1,%3,2), %%xmm2      \n\t"
458          "movdqu (%1,%4), %%xmm3        \n\t"
459          "movdqa %%xmm0, (%2)           \n\t"
460          "movdqa %%xmm1, (%2,%3)        \n\t"
461          "movdqa %%xmm2, (%2,%3,2)      \n\t"
462          "movdqa %%xmm3, (%2,%4)        \n\t"
463          "subl $4, %0                   \n\t"
464          "lea (%1,%3,4), %1             \n\t"
465          "lea (%2,%3,4), %2             \n\t"
466          "jnz 1b                        \n\t"
467          : "+g"(h), "+r" (pixels),  "+r" (block)
468          : "r"((x86_reg)line_size), "r"((x86_reg)3L*line_size)
469          : "memory"
470         );
471 }
472
473 static void avg_pixels16_sse2(uint8_t *block, const uint8_t *pixels, int line_size, int h)
474 {
475     __asm__ volatile(
476          "1:                            \n\t"
477          "movdqu (%1), %%xmm0           \n\t"
478          "movdqu (%1,%3), %%xmm1        \n\t"
479          "movdqu (%1,%3,2), %%xmm2      \n\t"
480          "movdqu (%1,%4), %%xmm3        \n\t"
481          "pavgb  (%2), %%xmm0           \n\t"
482          "pavgb  (%2,%3), %%xmm1        \n\t"
483          "pavgb  (%2,%3,2), %%xmm2      \n\t"
484          "pavgb  (%2,%4), %%xmm3        \n\t"
485          "movdqa %%xmm0, (%2)           \n\t"
486          "movdqa %%xmm1, (%2,%3)        \n\t"
487          "movdqa %%xmm2, (%2,%3,2)      \n\t"
488          "movdqa %%xmm3, (%2,%4)        \n\t"
489          "subl $4, %0                   \n\t"
490          "lea (%1,%3,4), %1             \n\t"
491          "lea (%2,%3,4), %2             \n\t"
492          "jnz 1b                        \n\t"
493          : "+g"(h), "+r" (pixels),  "+r" (block)
494          : "r"((x86_reg)line_size), "r"((x86_reg)3L*line_size)
495          : "memory"
496         );
497 }
498
499 #define CLEAR_BLOCKS(name,n) \
500 static void name(DCTELEM *blocks)\
501 {\
502     __asm__ volatile(\
503                 "pxor %%mm7, %%mm7              \n\t"\
504                 "mov     %1, %%"REG_a"          \n\t"\
505                 "1:                             \n\t"\
506                 "movq %%mm7, (%0, %%"REG_a")    \n\t"\
507                 "movq %%mm7, 8(%0, %%"REG_a")   \n\t"\
508                 "movq %%mm7, 16(%0, %%"REG_a")  \n\t"\
509                 "movq %%mm7, 24(%0, %%"REG_a")  \n\t"\
510                 "add $32, %%"REG_a"             \n\t"\
511                 " js 1b                         \n\t"\
512                 : : "r" (((uint8_t *)blocks)+128*n),\
513                     "i" (-128*n)\
514                 : "%"REG_a\
515         );\
516 }
517 CLEAR_BLOCKS(clear_blocks_mmx, 6)
518 CLEAR_BLOCKS(clear_block_mmx, 1)
519
520 static void clear_block_sse(DCTELEM *block)
521 {
522     __asm__ volatile(
523         "xorps  %%xmm0, %%xmm0  \n"
524         "movaps %%xmm0,    (%0) \n"
525         "movaps %%xmm0,  16(%0) \n"
526         "movaps %%xmm0,  32(%0) \n"
527         "movaps %%xmm0,  48(%0) \n"
528         "movaps %%xmm0,  64(%0) \n"
529         "movaps %%xmm0,  80(%0) \n"
530         "movaps %%xmm0,  96(%0) \n"
531         "movaps %%xmm0, 112(%0) \n"
532         :: "r"(block)
533         : "memory"
534     );
535 }
536
537 static void clear_blocks_sse(DCTELEM *blocks)
538 {\
539     __asm__ volatile(
540         "xorps  %%xmm0, %%xmm0  \n"
541         "mov     %1, %%"REG_a"  \n"
542         "1:                     \n"
543         "movaps %%xmm0,    (%0, %%"REG_a") \n"
544         "movaps %%xmm0,  16(%0, %%"REG_a") \n"
545         "movaps %%xmm0,  32(%0, %%"REG_a") \n"
546         "movaps %%xmm0,  48(%0, %%"REG_a") \n"
547         "movaps %%xmm0,  64(%0, %%"REG_a") \n"
548         "movaps %%xmm0,  80(%0, %%"REG_a") \n"
549         "movaps %%xmm0,  96(%0, %%"REG_a") \n"
550         "movaps %%xmm0, 112(%0, %%"REG_a") \n"
551         "add $128, %%"REG_a"    \n"
552         " js 1b                 \n"
553         : : "r" (((uint8_t *)blocks)+128*6),
554             "i" (-128*6)
555         : "%"REG_a
556     );
557 }
558
559 static void add_bytes_mmx(uint8_t *dst, uint8_t *src, int w){
560     x86_reg i=0;
561     __asm__ volatile(
562         "jmp 2f                         \n\t"
563         "1:                             \n\t"
564         "movq  (%1, %0), %%mm0          \n\t"
565         "movq  (%2, %0), %%mm1          \n\t"
566         "paddb %%mm0, %%mm1             \n\t"
567         "movq %%mm1, (%2, %0)           \n\t"
568         "movq 8(%1, %0), %%mm0          \n\t"
569         "movq 8(%2, %0), %%mm1          \n\t"
570         "paddb %%mm0, %%mm1             \n\t"
571         "movq %%mm1, 8(%2, %0)          \n\t"
572         "add $16, %0                    \n\t"
573         "2:                             \n\t"
574         "cmp %3, %0                     \n\t"
575         " js 1b                         \n\t"
576         : "+r" (i)
577         : "r"(src), "r"(dst), "r"((x86_reg)w-15)
578     );
579     for(; i<w; i++)
580         dst[i+0] += src[i+0];
581 }
582
583 #if HAVE_7REGS
584 static void add_hfyu_median_prediction_cmov(uint8_t *dst, const uint8_t *top, const uint8_t *diff, int w, int *left, int *left_top) {
585     x86_reg w2 = -w;
586     x86_reg x;
587     int l = *left & 0xff;
588     int tl = *left_top & 0xff;
589     int t;
590     __asm__ volatile(
591         "mov    %7, %3 \n"
592         "1: \n"
593         "movzbl (%3,%4), %2 \n"
594         "mov    %2, %k3 \n"
595         "sub   %b1, %b3 \n"
596         "add   %b0, %b3 \n"
597         "mov    %2, %1 \n"
598         "cmp    %0, %2 \n"
599         "cmovg  %0, %2 \n"
600         "cmovg  %1, %0 \n"
601         "cmp   %k3, %0 \n"
602         "cmovg %k3, %0 \n"
603         "mov    %7, %3 \n"
604         "cmp    %2, %0 \n"
605         "cmovl  %2, %0 \n"
606         "add (%6,%4), %b0 \n"
607         "mov   %b0, (%5,%4) \n"
608         "inc    %4 \n"
609         "jl 1b \n"
610         :"+&q"(l), "+&q"(tl), "=&r"(t), "=&q"(x), "+&r"(w2)
611         :"r"(dst+w), "r"(diff+w), "rm"(top+w)
612     );
613     *left = l;
614     *left_top = tl;
615 }
616 #endif
617
618 #define H263_LOOP_FILTER \
619         "pxor %%mm7, %%mm7              \n\t"\
620         "movq  %0, %%mm0                \n\t"\
621         "movq  %0, %%mm1                \n\t"\
622         "movq  %3, %%mm2                \n\t"\
623         "movq  %3, %%mm3                \n\t"\
624         "punpcklbw %%mm7, %%mm0         \n\t"\
625         "punpckhbw %%mm7, %%mm1         \n\t"\
626         "punpcklbw %%mm7, %%mm2         \n\t"\
627         "punpckhbw %%mm7, %%mm3         \n\t"\
628         "psubw %%mm2, %%mm0             \n\t"\
629         "psubw %%mm3, %%mm1             \n\t"\
630         "movq  %1, %%mm2                \n\t"\
631         "movq  %1, %%mm3                \n\t"\
632         "movq  %2, %%mm4                \n\t"\
633         "movq  %2, %%mm5                \n\t"\
634         "punpcklbw %%mm7, %%mm2         \n\t"\
635         "punpckhbw %%mm7, %%mm3         \n\t"\
636         "punpcklbw %%mm7, %%mm4         \n\t"\
637         "punpckhbw %%mm7, %%mm5         \n\t"\
638         "psubw %%mm2, %%mm4             \n\t"\
639         "psubw %%mm3, %%mm5             \n\t"\
640         "psllw $2, %%mm4                \n\t"\
641         "psllw $2, %%mm5                \n\t"\
642         "paddw %%mm0, %%mm4             \n\t"\
643         "paddw %%mm1, %%mm5             \n\t"\
644         "pxor %%mm6, %%mm6              \n\t"\
645         "pcmpgtw %%mm4, %%mm6           \n\t"\
646         "pcmpgtw %%mm5, %%mm7           \n\t"\
647         "pxor %%mm6, %%mm4              \n\t"\
648         "pxor %%mm7, %%mm5              \n\t"\
649         "psubw %%mm6, %%mm4             \n\t"\
650         "psubw %%mm7, %%mm5             \n\t"\
651         "psrlw $3, %%mm4                \n\t"\
652         "psrlw $3, %%mm5                \n\t"\
653         "packuswb %%mm5, %%mm4          \n\t"\
654         "packsswb %%mm7, %%mm6          \n\t"\
655         "pxor %%mm7, %%mm7              \n\t"\
656         "movd %4, %%mm2                 \n\t"\
657         "punpcklbw %%mm2, %%mm2         \n\t"\
658         "punpcklbw %%mm2, %%mm2         \n\t"\
659         "punpcklbw %%mm2, %%mm2         \n\t"\
660         "psubusb %%mm4, %%mm2           \n\t"\
661         "movq %%mm2, %%mm3              \n\t"\
662         "psubusb %%mm4, %%mm3           \n\t"\
663         "psubb %%mm3, %%mm2             \n\t"\
664         "movq %1, %%mm3                 \n\t"\
665         "movq %2, %%mm4                 \n\t"\
666         "pxor %%mm6, %%mm3              \n\t"\
667         "pxor %%mm6, %%mm4              \n\t"\
668         "paddusb %%mm2, %%mm3           \n\t"\
669         "psubusb %%mm2, %%mm4           \n\t"\
670         "pxor %%mm6, %%mm3              \n\t"\
671         "pxor %%mm6, %%mm4              \n\t"\
672         "paddusb %%mm2, %%mm2           \n\t"\
673         "packsswb %%mm1, %%mm0          \n\t"\
674         "pcmpgtb %%mm0, %%mm7           \n\t"\
675         "pxor %%mm7, %%mm0              \n\t"\
676         "psubb %%mm7, %%mm0             \n\t"\
677         "movq %%mm0, %%mm1              \n\t"\
678         "psubusb %%mm2, %%mm0           \n\t"\
679         "psubb %%mm0, %%mm1             \n\t"\
680         "pand %5, %%mm1                 \n\t"\
681         "psrlw $2, %%mm1                \n\t"\
682         "pxor %%mm7, %%mm1              \n\t"\
683         "psubb %%mm7, %%mm1             \n\t"\
684         "movq %0, %%mm5                 \n\t"\
685         "movq %3, %%mm6                 \n\t"\
686         "psubb %%mm1, %%mm5             \n\t"\
687         "paddb %%mm1, %%mm6             \n\t"
688
689 static void h263_v_loop_filter_mmx(uint8_t *src, int stride, int qscale){
690     if(CONFIG_H263_DECODER || CONFIG_H263_ENCODER) {
691     const int strength= ff_h263_loop_filter_strength[qscale];
692
693     __asm__ volatile(
694
695         H263_LOOP_FILTER
696
697         "movq %%mm3, %1                 \n\t"
698         "movq %%mm4, %2                 \n\t"
699         "movq %%mm5, %0                 \n\t"
700         "movq %%mm6, %3                 \n\t"
701         : "+m" (*(uint64_t*)(src - 2*stride)),
702           "+m" (*(uint64_t*)(src - 1*stride)),
703           "+m" (*(uint64_t*)(src + 0*stride)),
704           "+m" (*(uint64_t*)(src + 1*stride))
705         : "g" (2*strength), "m"(ff_pb_FC)
706     );
707     }
708 }
709
710 static void h263_h_loop_filter_mmx(uint8_t *src, int stride, int qscale){
711     if(CONFIG_H263_DECODER || CONFIG_H263_ENCODER) {
712     const int strength= ff_h263_loop_filter_strength[qscale];
713     DECLARE_ALIGNED(8, uint64_t, temp)[4];
714     uint8_t *btemp= (uint8_t*)temp;
715
716     src -= 2;
717
718     transpose4x4(btemp  , src           , 8, stride);
719     transpose4x4(btemp+4, src + 4*stride, 8, stride);
720     __asm__ volatile(
721         H263_LOOP_FILTER // 5 3 4 6
722
723         : "+m" (temp[0]),
724           "+m" (temp[1]),
725           "+m" (temp[2]),
726           "+m" (temp[3])
727         : "g" (2*strength), "m"(ff_pb_FC)
728     );
729
730     __asm__ volatile(
731         "movq %%mm5, %%mm1              \n\t"
732         "movq %%mm4, %%mm0              \n\t"
733         "punpcklbw %%mm3, %%mm5         \n\t"
734         "punpcklbw %%mm6, %%mm4         \n\t"
735         "punpckhbw %%mm3, %%mm1         \n\t"
736         "punpckhbw %%mm6, %%mm0         \n\t"
737         "movq %%mm5, %%mm3              \n\t"
738         "movq %%mm1, %%mm6              \n\t"
739         "punpcklwd %%mm4, %%mm5         \n\t"
740         "punpcklwd %%mm0, %%mm1         \n\t"
741         "punpckhwd %%mm4, %%mm3         \n\t"
742         "punpckhwd %%mm0, %%mm6         \n\t"
743         "movd %%mm5, (%0)               \n\t"
744         "punpckhdq %%mm5, %%mm5         \n\t"
745         "movd %%mm5, (%0,%2)            \n\t"
746         "movd %%mm3, (%0,%2,2)          \n\t"
747         "punpckhdq %%mm3, %%mm3         \n\t"
748         "movd %%mm3, (%0,%3)            \n\t"
749         "movd %%mm1, (%1)               \n\t"
750         "punpckhdq %%mm1, %%mm1         \n\t"
751         "movd %%mm1, (%1,%2)            \n\t"
752         "movd %%mm6, (%1,%2,2)          \n\t"
753         "punpckhdq %%mm6, %%mm6         \n\t"
754         "movd %%mm6, (%1,%3)            \n\t"
755         :: "r" (src),
756            "r" (src + 4*stride),
757            "r" ((x86_reg)   stride ),
758            "r" ((x86_reg)(3*stride))
759     );
760     }
761 }
762
763 /* draw the edges of width 'w' of an image of size width, height
764    this mmx version can only handle w==8 || w==16 */
765 static void draw_edges_mmx(uint8_t *buf, int wrap, int width, int height, int w, int h, int sides)
766 {
767     uint8_t *ptr, *last_line;
768     int i;
769
770     last_line = buf + (height - 1) * wrap;
771     /* left and right */
772     ptr = buf;
773     if(w==8)
774     {
775         __asm__ volatile(
776                 "1:                             \n\t"
777                 "movd (%0), %%mm0               \n\t"
778                 "punpcklbw %%mm0, %%mm0         \n\t"
779                 "punpcklwd %%mm0, %%mm0         \n\t"
780                 "punpckldq %%mm0, %%mm0         \n\t"
781                 "movq %%mm0, -8(%0)             \n\t"
782                 "movq -8(%0, %2), %%mm1         \n\t"
783                 "punpckhbw %%mm1, %%mm1         \n\t"
784                 "punpckhwd %%mm1, %%mm1         \n\t"
785                 "punpckhdq %%mm1, %%mm1         \n\t"
786                 "movq %%mm1, (%0, %2)           \n\t"
787                 "add %1, %0                     \n\t"
788                 "cmp %3, %0                     \n\t"
789                 " jb 1b                         \n\t"
790                 : "+r" (ptr)
791                 : "r" ((x86_reg)wrap), "r" ((x86_reg)width), "r" (ptr + wrap*height)
792         );
793     }
794     else
795     {
796         __asm__ volatile(
797                 "1:                             \n\t"
798                 "movd (%0), %%mm0               \n\t"
799                 "punpcklbw %%mm0, %%mm0         \n\t"
800                 "punpcklwd %%mm0, %%mm0         \n\t"
801                 "punpckldq %%mm0, %%mm0         \n\t"
802                 "movq %%mm0, -8(%0)             \n\t"
803                 "movq %%mm0, -16(%0)            \n\t"
804                 "movq -8(%0, %2), %%mm1         \n\t"
805                 "punpckhbw %%mm1, %%mm1         \n\t"
806                 "punpckhwd %%mm1, %%mm1         \n\t"
807                 "punpckhdq %%mm1, %%mm1         \n\t"
808                 "movq %%mm1, (%0, %2)           \n\t"
809                 "movq %%mm1, 8(%0, %2)          \n\t"
810                 "add %1, %0                     \n\t"
811                 "cmp %3, %0                     \n\t"
812                 " jb 1b                         \n\t"
813                 : "+r" (ptr)
814                 : "r" ((x86_reg)wrap), "r" ((x86_reg)width), "r" (ptr + wrap*height)
815         );
816     }
817
818     /* top and bottom (and hopefully also the corners) */
819     if (sides&EDGE_TOP) {
820         for(i = 0; i < h; i += 4) {
821             ptr= buf - (i + 1) * wrap - w;
822             __asm__ volatile(
823                     "1:                             \n\t"
824                     "movq (%1, %0), %%mm0           \n\t"
825                     "movq %%mm0, (%0)               \n\t"
826                     "movq %%mm0, (%0, %2)           \n\t"
827                     "movq %%mm0, (%0, %2, 2)        \n\t"
828                     "movq %%mm0, (%0, %3)           \n\t"
829                     "add $8, %0                     \n\t"
830                     "cmp %4, %0                     \n\t"
831                     " jb 1b                         \n\t"
832                     : "+r" (ptr)
833                     : "r" ((x86_reg)buf - (x86_reg)ptr - w), "r" ((x86_reg)-wrap), "r" ((x86_reg)-wrap*3), "r" (ptr+width+2*w)
834             );
835         }
836     }
837
838     if (sides&EDGE_BOTTOM) {
839         for(i = 0; i < w; i += 4) {
840             ptr= last_line + (i + 1) * wrap - w;
841             __asm__ volatile(
842                     "1:                             \n\t"
843                     "movq (%1, %0), %%mm0           \n\t"
844                     "movq %%mm0, (%0)               \n\t"
845                     "movq %%mm0, (%0, %2)           \n\t"
846                     "movq %%mm0, (%0, %2, 2)        \n\t"
847                     "movq %%mm0, (%0, %3)           \n\t"
848                     "add $8, %0                     \n\t"
849                     "cmp %4, %0                     \n\t"
850                     " jb 1b                         \n\t"
851                     : "+r" (ptr)
852                     : "r" ((x86_reg)last_line - (x86_reg)ptr - w), "r" ((x86_reg)wrap), "r" ((x86_reg)wrap*3), "r" (ptr+width+2*w)
853             );
854         }
855     }
856 }
857
858 #define QPEL_V_LOW(m3,m4,m5,m6, pw_20, pw_3, rnd, in0, in1, in2, in7, out, OP)\
859         "paddw " #m4 ", " #m3 "           \n\t" /* x1 */\
860         "movq "MANGLE(ff_pw_20)", %%mm4   \n\t" /* 20 */\
861         "pmullw " #m3 ", %%mm4            \n\t" /* 20x1 */\
862         "movq "#in7", " #m3 "             \n\t" /* d */\
863         "movq "#in0", %%mm5               \n\t" /* D */\
864         "paddw " #m3 ", %%mm5             \n\t" /* x4 */\
865         "psubw %%mm5, %%mm4               \n\t" /* 20x1 - x4 */\
866         "movq "#in1", %%mm5               \n\t" /* C */\
867         "movq "#in2", %%mm6               \n\t" /* B */\
868         "paddw " #m6 ", %%mm5             \n\t" /* x3 */\
869         "paddw " #m5 ", %%mm6             \n\t" /* x2 */\
870         "paddw %%mm6, %%mm6               \n\t" /* 2x2 */\
871         "psubw %%mm6, %%mm5               \n\t" /* -2x2 + x3 */\
872         "pmullw "MANGLE(ff_pw_3)", %%mm5  \n\t" /* -6x2 + 3x3 */\
873         "paddw " #rnd ", %%mm4            \n\t" /* x2 */\
874         "paddw %%mm4, %%mm5               \n\t" /* 20x1 - 6x2 + 3x3 - x4 */\
875         "psraw $5, %%mm5                  \n\t"\
876         "packuswb %%mm5, %%mm5            \n\t"\
877         OP(%%mm5, out, %%mm7, d)
878
879 #define QPEL_BASE(OPNAME, ROUNDER, RND, OP_MMX2, OP_3DNOW)\
880 static void OPNAME ## mpeg4_qpel16_h_lowpass_mmx2(uint8_t *dst, uint8_t *src, int dstStride, int srcStride, int h){\
881     uint64_t temp;\
882 \
883     __asm__ volatile(\
884         "pxor %%mm7, %%mm7                \n\t"\
885         "1:                               \n\t"\
886         "movq  (%0), %%mm0                \n\t" /* ABCDEFGH */\
887         "movq %%mm0, %%mm1                \n\t" /* ABCDEFGH */\
888         "movq %%mm0, %%mm2                \n\t" /* ABCDEFGH */\
889         "punpcklbw %%mm7, %%mm0           \n\t" /* 0A0B0C0D */\
890         "punpckhbw %%mm7, %%mm1           \n\t" /* 0E0F0G0H */\
891         "pshufw $0x90, %%mm0, %%mm5       \n\t" /* 0A0A0B0C */\
892         "pshufw $0x41, %%mm0, %%mm6       \n\t" /* 0B0A0A0B */\
893         "movq %%mm2, %%mm3                \n\t" /* ABCDEFGH */\
894         "movq %%mm2, %%mm4                \n\t" /* ABCDEFGH */\
895         "psllq $8, %%mm2                  \n\t" /* 0ABCDEFG */\
896         "psllq $16, %%mm3                 \n\t" /* 00ABCDEF */\
897         "psllq $24, %%mm4                 \n\t" /* 000ABCDE */\
898         "punpckhbw %%mm7, %%mm2           \n\t" /* 0D0E0F0G */\
899         "punpckhbw %%mm7, %%mm3           \n\t" /* 0C0D0E0F */\
900         "punpckhbw %%mm7, %%mm4           \n\t" /* 0B0C0D0E */\
901         "paddw %%mm3, %%mm5               \n\t" /* b */\
902         "paddw %%mm2, %%mm6               \n\t" /* c */\
903         "paddw %%mm5, %%mm5               \n\t" /* 2b */\
904         "psubw %%mm5, %%mm6               \n\t" /* c - 2b */\
905         "pshufw $0x06, %%mm0, %%mm5       \n\t" /* 0C0B0A0A */\
906         "pmullw "MANGLE(ff_pw_3)", %%mm6  \n\t" /* 3c - 6b */\
907         "paddw %%mm4, %%mm0               \n\t" /* a */\
908         "paddw %%mm1, %%mm5               \n\t" /* d */\
909         "pmullw "MANGLE(ff_pw_20)", %%mm0 \n\t" /* 20a */\
910         "psubw %%mm5, %%mm0               \n\t" /* 20a - d */\
911         "paddw %6, %%mm6                  \n\t"\
912         "paddw %%mm6, %%mm0               \n\t" /* 20a - 6b + 3c - d */\
913         "psraw $5, %%mm0                  \n\t"\
914         "movq %%mm0, %5                   \n\t"\
915         /* mm1=EFGH, mm2=DEFG, mm3=CDEF, mm4=BCDE, mm7=0 */\
916         \
917         "movq 5(%0), %%mm0                \n\t" /* FGHIJKLM */\
918         "movq %%mm0, %%mm5                \n\t" /* FGHIJKLM */\
919         "movq %%mm0, %%mm6                \n\t" /* FGHIJKLM */\
920         "psrlq $8, %%mm0                  \n\t" /* GHIJKLM0 */\
921         "psrlq $16, %%mm5                 \n\t" /* HIJKLM00 */\
922         "punpcklbw %%mm7, %%mm0           \n\t" /* 0G0H0I0J */\
923         "punpcklbw %%mm7, %%mm5           \n\t" /* 0H0I0J0K */\
924         "paddw %%mm0, %%mm2               \n\t" /* b */\
925         "paddw %%mm5, %%mm3               \n\t" /* c */\
926         "paddw %%mm2, %%mm2               \n\t" /* 2b */\
927         "psubw %%mm2, %%mm3               \n\t" /* c - 2b */\
928         "movq %%mm6, %%mm2                \n\t" /* FGHIJKLM */\
929         "psrlq $24, %%mm6                 \n\t" /* IJKLM000 */\
930         "punpcklbw %%mm7, %%mm2           \n\t" /* 0F0G0H0I */\
931         "punpcklbw %%mm7, %%mm6           \n\t" /* 0I0J0K0L */\
932         "pmullw "MANGLE(ff_pw_3)", %%mm3  \n\t" /* 3c - 6b */\
933         "paddw %%mm2, %%mm1               \n\t" /* a */\
934         "paddw %%mm6, %%mm4               \n\t" /* d */\
935         "pmullw "MANGLE(ff_pw_20)", %%mm1 \n\t" /* 20a */\
936         "psubw %%mm4, %%mm3               \n\t" /* - 6b +3c - d */\
937         "paddw %6, %%mm1                  \n\t"\
938         "paddw %%mm1, %%mm3               \n\t" /* 20a - 6b +3c - d */\
939         "psraw $5, %%mm3                  \n\t"\
940         "movq %5, %%mm1                   \n\t"\
941         "packuswb %%mm3, %%mm1            \n\t"\
942         OP_MMX2(%%mm1, (%1),%%mm4, q)\
943         /* mm0= GHIJ, mm2=FGHI, mm5=HIJK, mm6=IJKL, mm7=0 */\
944         \
945         "movq 9(%0), %%mm1                \n\t" /* JKLMNOPQ */\
946         "movq %%mm1, %%mm4                \n\t" /* JKLMNOPQ */\
947         "movq %%mm1, %%mm3                \n\t" /* JKLMNOPQ */\
948         "psrlq $8, %%mm1                  \n\t" /* KLMNOPQ0 */\
949         "psrlq $16, %%mm4                 \n\t" /* LMNOPQ00 */\
950         "punpcklbw %%mm7, %%mm1           \n\t" /* 0K0L0M0N */\
951         "punpcklbw %%mm7, %%mm4           \n\t" /* 0L0M0N0O */\
952         "paddw %%mm1, %%mm5               \n\t" /* b */\
953         "paddw %%mm4, %%mm0               \n\t" /* c */\
954         "paddw %%mm5, %%mm5               \n\t" /* 2b */\
955         "psubw %%mm5, %%mm0               \n\t" /* c - 2b */\
956         "movq %%mm3, %%mm5                \n\t" /* JKLMNOPQ */\
957         "psrlq $24, %%mm3                 \n\t" /* MNOPQ000 */\
958         "pmullw "MANGLE(ff_pw_3)", %%mm0  \n\t" /* 3c - 6b */\
959         "punpcklbw %%mm7, %%mm3           \n\t" /* 0M0N0O0P */\
960         "paddw %%mm3, %%mm2               \n\t" /* d */\
961         "psubw %%mm2, %%mm0               \n\t" /* -6b + 3c - d */\
962         "movq %%mm5, %%mm2                \n\t" /* JKLMNOPQ */\
963         "punpcklbw %%mm7, %%mm2           \n\t" /* 0J0K0L0M */\
964         "punpckhbw %%mm7, %%mm5           \n\t" /* 0N0O0P0Q */\
965         "paddw %%mm2, %%mm6               \n\t" /* a */\
966         "pmullw "MANGLE(ff_pw_20)", %%mm6 \n\t" /* 20a */\
967         "paddw %6, %%mm0                  \n\t"\
968         "paddw %%mm6, %%mm0               \n\t" /* 20a - 6b + 3c - d */\
969         "psraw $5, %%mm0                  \n\t"\
970         /* mm1=KLMN, mm2=JKLM, mm3=MNOP, mm4=LMNO, mm5=NOPQ mm7=0 */\
971         \
972         "paddw %%mm5, %%mm3               \n\t" /* a */\
973         "pshufw $0xF9, %%mm5, %%mm6       \n\t" /* 0O0P0Q0Q */\
974         "paddw %%mm4, %%mm6               \n\t" /* b */\
975         "pshufw $0xBE, %%mm5, %%mm4       \n\t" /* 0P0Q0Q0P */\
976         "pshufw $0x6F, %%mm5, %%mm5       \n\t" /* 0Q0Q0P0O */\
977         "paddw %%mm1, %%mm4               \n\t" /* c */\
978         "paddw %%mm2, %%mm5               \n\t" /* d */\
979         "paddw %%mm6, %%mm6               \n\t" /* 2b */\
980         "psubw %%mm6, %%mm4               \n\t" /* c - 2b */\
981         "pmullw "MANGLE(ff_pw_20)", %%mm3 \n\t" /* 20a */\
982         "pmullw "MANGLE(ff_pw_3)", %%mm4  \n\t" /* 3c - 6b */\
983         "psubw %%mm5, %%mm3               \n\t" /* -6b + 3c - d */\
984         "paddw %6, %%mm4                  \n\t"\
985         "paddw %%mm3, %%mm4               \n\t" /* 20a - 6b + 3c - d */\
986         "psraw $5, %%mm4                  \n\t"\
987         "packuswb %%mm4, %%mm0            \n\t"\
988         OP_MMX2(%%mm0, 8(%1), %%mm4, q)\
989         \
990         "add %3, %0                       \n\t"\
991         "add %4, %1                       \n\t"\
992         "decl %2                          \n\t"\
993         " jnz 1b                          \n\t"\
994         : "+a"(src), "+c"(dst), "+D"(h)\
995         : "d"((x86_reg)srcStride), "S"((x86_reg)dstStride), /*"m"(ff_pw_20), "m"(ff_pw_3),*/ "m"(temp), "m"(ROUNDER)\
996         : "memory"\
997     );\
998 }\
999 \
1000 static void OPNAME ## mpeg4_qpel16_h_lowpass_3dnow(uint8_t *dst, uint8_t *src, int dstStride, int srcStride, int h){\
1001     int i;\
1002     int16_t temp[16];\
1003     /* quick HACK, XXX FIXME MUST be optimized */\
1004     for(i=0; i<h; i++)\
1005     {\
1006         temp[ 0]= (src[ 0]+src[ 1])*20 - (src[ 0]+src[ 2])*6 + (src[ 1]+src[ 3])*3 - (src[ 2]+src[ 4]);\
1007         temp[ 1]= (src[ 1]+src[ 2])*20 - (src[ 0]+src[ 3])*6 + (src[ 0]+src[ 4])*3 - (src[ 1]+src[ 5]);\
1008         temp[ 2]= (src[ 2]+src[ 3])*20 - (src[ 1]+src[ 4])*6 + (src[ 0]+src[ 5])*3 - (src[ 0]+src[ 6]);\
1009         temp[ 3]= (src[ 3]+src[ 4])*20 - (src[ 2]+src[ 5])*6 + (src[ 1]+src[ 6])*3 - (src[ 0]+src[ 7]);\
1010         temp[ 4]= (src[ 4]+src[ 5])*20 - (src[ 3]+src[ 6])*6 + (src[ 2]+src[ 7])*3 - (src[ 1]+src[ 8]);\
1011         temp[ 5]= (src[ 5]+src[ 6])*20 - (src[ 4]+src[ 7])*6 + (src[ 3]+src[ 8])*3 - (src[ 2]+src[ 9]);\
1012         temp[ 6]= (src[ 6]+src[ 7])*20 - (src[ 5]+src[ 8])*6 + (src[ 4]+src[ 9])*3 - (src[ 3]+src[10]);\
1013         temp[ 7]= (src[ 7]+src[ 8])*20 - (src[ 6]+src[ 9])*6 + (src[ 5]+src[10])*3 - (src[ 4]+src[11]);\
1014         temp[ 8]= (src[ 8]+src[ 9])*20 - (src[ 7]+src[10])*6 + (src[ 6]+src[11])*3 - (src[ 5]+src[12]);\
1015         temp[ 9]= (src[ 9]+src[10])*20 - (src[ 8]+src[11])*6 + (src[ 7]+src[12])*3 - (src[ 6]+src[13]);\
1016         temp[10]= (src[10]+src[11])*20 - (src[ 9]+src[12])*6 + (src[ 8]+src[13])*3 - (src[ 7]+src[14]);\
1017         temp[11]= (src[11]+src[12])*20 - (src[10]+src[13])*6 + (src[ 9]+src[14])*3 - (src[ 8]+src[15]);\
1018         temp[12]= (src[12]+src[13])*20 - (src[11]+src[14])*6 + (src[10]+src[15])*3 - (src[ 9]+src[16]);\
1019         temp[13]= (src[13]+src[14])*20 - (src[12]+src[15])*6 + (src[11]+src[16])*3 - (src[10]+src[16]);\
1020         temp[14]= (src[14]+src[15])*20 - (src[13]+src[16])*6 + (src[12]+src[16])*3 - (src[11]+src[15]);\
1021         temp[15]= (src[15]+src[16])*20 - (src[14]+src[16])*6 + (src[13]+src[15])*3 - (src[12]+src[14]);\
1022         __asm__ volatile(\
1023             "movq (%0), %%mm0               \n\t"\
1024             "movq 8(%0), %%mm1              \n\t"\
1025             "paddw %2, %%mm0                \n\t"\
1026             "paddw %2, %%mm1                \n\t"\
1027             "psraw $5, %%mm0                \n\t"\
1028             "psraw $5, %%mm1                \n\t"\
1029             "packuswb %%mm1, %%mm0          \n\t"\
1030             OP_3DNOW(%%mm0, (%1), %%mm1, q)\
1031             "movq 16(%0), %%mm0             \n\t"\
1032             "movq 24(%0), %%mm1             \n\t"\
1033             "paddw %2, %%mm0                \n\t"\
1034             "paddw %2, %%mm1                \n\t"\
1035             "psraw $5, %%mm0                \n\t"\
1036             "psraw $5, %%mm1                \n\t"\
1037             "packuswb %%mm1, %%mm0          \n\t"\
1038             OP_3DNOW(%%mm0, 8(%1), %%mm1, q)\
1039             :: "r"(temp), "r"(dst), "m"(ROUNDER)\
1040             : "memory"\
1041         );\
1042         dst+=dstStride;\
1043         src+=srcStride;\
1044     }\
1045 }\
1046 \
1047 static void OPNAME ## mpeg4_qpel8_h_lowpass_mmx2(uint8_t *dst, uint8_t *src, int dstStride, int srcStride, int h){\
1048     __asm__ volatile(\
1049         "pxor %%mm7, %%mm7                \n\t"\
1050         "1:                               \n\t"\
1051         "movq  (%0), %%mm0                \n\t" /* ABCDEFGH */\
1052         "movq %%mm0, %%mm1                \n\t" /* ABCDEFGH */\
1053         "movq %%mm0, %%mm2                \n\t" /* ABCDEFGH */\
1054         "punpcklbw %%mm7, %%mm0           \n\t" /* 0A0B0C0D */\
1055         "punpckhbw %%mm7, %%mm1           \n\t" /* 0E0F0G0H */\
1056         "pshufw $0x90, %%mm0, %%mm5       \n\t" /* 0A0A0B0C */\
1057         "pshufw $0x41, %%mm0, %%mm6       \n\t" /* 0B0A0A0B */\
1058         "movq %%mm2, %%mm3                \n\t" /* ABCDEFGH */\
1059         "movq %%mm2, %%mm4                \n\t" /* ABCDEFGH */\
1060         "psllq $8, %%mm2                  \n\t" /* 0ABCDEFG */\
1061         "psllq $16, %%mm3                 \n\t" /* 00ABCDEF */\
1062         "psllq $24, %%mm4                 \n\t" /* 000ABCDE */\
1063         "punpckhbw %%mm7, %%mm2           \n\t" /* 0D0E0F0G */\
1064         "punpckhbw %%mm7, %%mm3           \n\t" /* 0C0D0E0F */\
1065         "punpckhbw %%mm7, %%mm4           \n\t" /* 0B0C0D0E */\
1066         "paddw %%mm3, %%mm5               \n\t" /* b */\
1067         "paddw %%mm2, %%mm6               \n\t" /* c */\
1068         "paddw %%mm5, %%mm5               \n\t" /* 2b */\
1069         "psubw %%mm5, %%mm6               \n\t" /* c - 2b */\
1070         "pshufw $0x06, %%mm0, %%mm5       \n\t" /* 0C0B0A0A */\
1071         "pmullw "MANGLE(ff_pw_3)", %%mm6  \n\t" /* 3c - 6b */\
1072         "paddw %%mm4, %%mm0               \n\t" /* a */\
1073         "paddw %%mm1, %%mm5               \n\t" /* d */\
1074         "pmullw "MANGLE(ff_pw_20)", %%mm0 \n\t" /* 20a */\
1075         "psubw %%mm5, %%mm0               \n\t" /* 20a - d */\
1076         "paddw %5, %%mm6                  \n\t"\
1077         "paddw %%mm6, %%mm0               \n\t" /* 20a - 6b + 3c - d */\
1078         "psraw $5, %%mm0                  \n\t"\
1079         /* mm1=EFGH, mm2=DEFG, mm3=CDEF, mm4=BCDE, mm7=0 */\
1080         \
1081         "movd 5(%0), %%mm5                \n\t" /* FGHI */\
1082         "punpcklbw %%mm7, %%mm5           \n\t" /* 0F0G0H0I */\
1083         "pshufw $0xF9, %%mm5, %%mm6       \n\t" /* 0G0H0I0I */\
1084         "paddw %%mm5, %%mm1               \n\t" /* a */\
1085         "paddw %%mm6, %%mm2               \n\t" /* b */\
1086         "pshufw $0xBE, %%mm5, %%mm6       \n\t" /* 0H0I0I0H */\
1087         "pshufw $0x6F, %%mm5, %%mm5       \n\t" /* 0I0I0H0G */\
1088         "paddw %%mm6, %%mm3               \n\t" /* c */\
1089         "paddw %%mm5, %%mm4               \n\t" /* d */\
1090         "paddw %%mm2, %%mm2               \n\t" /* 2b */\
1091         "psubw %%mm2, %%mm3               \n\t" /* c - 2b */\
1092         "pmullw "MANGLE(ff_pw_20)", %%mm1 \n\t" /* 20a */\
1093         "pmullw "MANGLE(ff_pw_3)", %%mm3  \n\t" /* 3c - 6b */\
1094         "psubw %%mm4, %%mm3               \n\t" /* -6b + 3c - d */\
1095         "paddw %5, %%mm1                  \n\t"\
1096         "paddw %%mm1, %%mm3               \n\t" /* 20a - 6b + 3c - d */\
1097         "psraw $5, %%mm3                  \n\t"\
1098         "packuswb %%mm3, %%mm0            \n\t"\
1099         OP_MMX2(%%mm0, (%1), %%mm4, q)\
1100         \
1101         "add %3, %0                       \n\t"\
1102         "add %4, %1                       \n\t"\
1103         "decl %2                          \n\t"\
1104         " jnz 1b                          \n\t"\
1105         : "+a"(src), "+c"(dst), "+d"(h)\
1106         : "S"((x86_reg)srcStride), "D"((x86_reg)dstStride), /*"m"(ff_pw_20), "m"(ff_pw_3),*/ "m"(ROUNDER)\
1107         : "memory"\
1108     );\
1109 }\
1110 \
1111 static void OPNAME ## mpeg4_qpel8_h_lowpass_3dnow(uint8_t *dst, uint8_t *src, int dstStride, int srcStride, int h){\
1112     int i;\
1113     int16_t temp[8];\
1114     /* quick HACK, XXX FIXME MUST be optimized */\
1115     for(i=0; i<h; i++)\
1116     {\
1117         temp[ 0]= (src[ 0]+src[ 1])*20 - (src[ 0]+src[ 2])*6 + (src[ 1]+src[ 3])*3 - (src[ 2]+src[ 4]);\
1118         temp[ 1]= (src[ 1]+src[ 2])*20 - (src[ 0]+src[ 3])*6 + (src[ 0]+src[ 4])*3 - (src[ 1]+src[ 5]);\
1119         temp[ 2]= (src[ 2]+src[ 3])*20 - (src[ 1]+src[ 4])*6 + (src[ 0]+src[ 5])*3 - (src[ 0]+src[ 6]);\
1120         temp[ 3]= (src[ 3]+src[ 4])*20 - (src[ 2]+src[ 5])*6 + (src[ 1]+src[ 6])*3 - (src[ 0]+src[ 7]);\
1121         temp[ 4]= (src[ 4]+src[ 5])*20 - (src[ 3]+src[ 6])*6 + (src[ 2]+src[ 7])*3 - (src[ 1]+src[ 8]);\
1122         temp[ 5]= (src[ 5]+src[ 6])*20 - (src[ 4]+src[ 7])*6 + (src[ 3]+src[ 8])*3 - (src[ 2]+src[ 8]);\
1123         temp[ 6]= (src[ 6]+src[ 7])*20 - (src[ 5]+src[ 8])*6 + (src[ 4]+src[ 8])*3 - (src[ 3]+src[ 7]);\
1124         temp[ 7]= (src[ 7]+src[ 8])*20 - (src[ 6]+src[ 8])*6 + (src[ 5]+src[ 7])*3 - (src[ 4]+src[ 6]);\
1125         __asm__ volatile(\
1126             "movq (%0), %%mm0           \n\t"\
1127             "movq 8(%0), %%mm1          \n\t"\
1128             "paddw %2, %%mm0            \n\t"\
1129             "paddw %2, %%mm1            \n\t"\
1130             "psraw $5, %%mm0            \n\t"\
1131             "psraw $5, %%mm1            \n\t"\
1132             "packuswb %%mm1, %%mm0      \n\t"\
1133             OP_3DNOW(%%mm0, (%1), %%mm1, q)\
1134             :: "r"(temp), "r"(dst), "m"(ROUNDER)\
1135             :"memory"\
1136         );\
1137         dst+=dstStride;\
1138         src+=srcStride;\
1139     }\
1140 }
1141
1142 #define QPEL_OP(OPNAME, ROUNDER, RND, OP, MMX)\
1143 \
1144 static void OPNAME ## mpeg4_qpel16_v_lowpass_ ## MMX(uint8_t *dst, uint8_t *src, int dstStride, int srcStride){\
1145     uint64_t temp[17*4];\
1146     uint64_t *temp_ptr= temp;\
1147     int count= 17;\
1148 \
1149     /*FIXME unroll */\
1150     __asm__ volatile(\
1151         "pxor %%mm7, %%mm7              \n\t"\
1152         "1:                             \n\t"\
1153         "movq (%0), %%mm0               \n\t"\
1154         "movq (%0), %%mm1               \n\t"\
1155         "movq 8(%0), %%mm2              \n\t"\
1156         "movq 8(%0), %%mm3              \n\t"\
1157         "punpcklbw %%mm7, %%mm0         \n\t"\
1158         "punpckhbw %%mm7, %%mm1         \n\t"\
1159         "punpcklbw %%mm7, %%mm2         \n\t"\
1160         "punpckhbw %%mm7, %%mm3         \n\t"\
1161         "movq %%mm0, (%1)               \n\t"\
1162         "movq %%mm1, 17*8(%1)           \n\t"\
1163         "movq %%mm2, 2*17*8(%1)         \n\t"\
1164         "movq %%mm3, 3*17*8(%1)         \n\t"\
1165         "add $8, %1                     \n\t"\
1166         "add %3, %0                     \n\t"\
1167         "decl %2                        \n\t"\
1168         " jnz 1b                        \n\t"\
1169         : "+r" (src), "+r" (temp_ptr), "+r"(count)\
1170         : "r" ((x86_reg)srcStride)\
1171         : "memory"\
1172     );\
1173     \
1174     temp_ptr= temp;\
1175     count=4;\
1176     \
1177 /*FIXME reorder for speed */\
1178     __asm__ volatile(\
1179         /*"pxor %%mm7, %%mm7              \n\t"*/\
1180         "1:                             \n\t"\
1181         "movq (%0), %%mm0               \n\t"\
1182         "movq 8(%0), %%mm1              \n\t"\
1183         "movq 16(%0), %%mm2             \n\t"\
1184         "movq 24(%0), %%mm3             \n\t"\
1185         QPEL_V_LOW(%%mm0, %%mm1, %%mm2, %%mm3, %5, %6, %5, 16(%0),  8(%0),   (%0), 32(%0), (%1), OP)\
1186         QPEL_V_LOW(%%mm1, %%mm2, %%mm3, %%mm0, %5, %6, %5,  8(%0),   (%0),   (%0), 40(%0), (%1, %3), OP)\
1187         "add %4, %1                     \n\t"\
1188         QPEL_V_LOW(%%mm2, %%mm3, %%mm0, %%mm1, %5, %6, %5,   (%0),   (%0),  8(%0), 48(%0), (%1), OP)\
1189         \
1190         QPEL_V_LOW(%%mm3, %%mm0, %%mm1, %%mm2, %5, %6, %5,   (%0),  8(%0), 16(%0), 56(%0), (%1, %3), OP)\
1191         "add %4, %1                     \n\t"\
1192         QPEL_V_LOW(%%mm0, %%mm1, %%mm2, %%mm3, %5, %6, %5,  8(%0), 16(%0), 24(%0), 64(%0), (%1), OP)\
1193         QPEL_V_LOW(%%mm1, %%mm2, %%mm3, %%mm0, %5, %6, %5, 16(%0), 24(%0), 32(%0), 72(%0), (%1, %3), OP)\
1194         "add %4, %1                     \n\t"\
1195         QPEL_V_LOW(%%mm2, %%mm3, %%mm0, %%mm1, %5, %6, %5, 24(%0), 32(%0), 40(%0), 80(%0), (%1), OP)\
1196         QPEL_V_LOW(%%mm3, %%mm0, %%mm1, %%mm2, %5, %6, %5, 32(%0), 40(%0), 48(%0), 88(%0), (%1, %3), OP)\
1197         "add %4, %1                     \n\t"\
1198         QPEL_V_LOW(%%mm0, %%mm1, %%mm2, %%mm3, %5, %6, %5, 40(%0), 48(%0), 56(%0), 96(%0), (%1), OP)\
1199         QPEL_V_LOW(%%mm1, %%mm2, %%mm3, %%mm0, %5, %6, %5, 48(%0), 56(%0), 64(%0),104(%0), (%1, %3), OP)\
1200         "add %4, %1                     \n\t"\
1201         QPEL_V_LOW(%%mm2, %%mm3, %%mm0, %%mm1, %5, %6, %5, 56(%0), 64(%0), 72(%0),112(%0), (%1), OP)\
1202         QPEL_V_LOW(%%mm3, %%mm0, %%mm1, %%mm2, %5, %6, %5, 64(%0), 72(%0), 80(%0),120(%0), (%1, %3), OP)\
1203         "add %4, %1                     \n\t"\
1204         QPEL_V_LOW(%%mm0, %%mm1, %%mm2, %%mm3, %5, %6, %5, 72(%0), 80(%0), 88(%0),128(%0), (%1), OP)\
1205         \
1206         QPEL_V_LOW(%%mm1, %%mm2, %%mm3, %%mm0, %5, %6, %5, 80(%0), 88(%0), 96(%0),128(%0), (%1, %3), OP)\
1207         "add %4, %1                     \n\t"  \
1208         QPEL_V_LOW(%%mm2, %%mm3, %%mm0, %%mm1, %5, %6, %5, 88(%0), 96(%0),104(%0),120(%0), (%1), OP)\
1209         QPEL_V_LOW(%%mm3, %%mm0, %%mm1, %%mm2, %5, %6, %5, 96(%0),104(%0),112(%0),112(%0), (%1, %3), OP)\
1210         \
1211         "add $136, %0                   \n\t"\
1212         "add %6, %1                     \n\t"\
1213         "decl %2                        \n\t"\
1214         " jnz 1b                        \n\t"\
1215         \
1216         : "+r"(temp_ptr), "+r"(dst), "+g"(count)\
1217         : "r"((x86_reg)dstStride), "r"(2*(x86_reg)dstStride), /*"m"(ff_pw_20), "m"(ff_pw_3),*/ "m"(ROUNDER), "g"(4-14*(x86_reg)dstStride)\
1218         :"memory"\
1219     );\
1220 }\
1221 \
1222 static void OPNAME ## mpeg4_qpel8_v_lowpass_ ## MMX(uint8_t *dst, uint8_t *src, int dstStride, int srcStride){\
1223     uint64_t temp[9*2];\
1224     uint64_t *temp_ptr= temp;\
1225     int count= 9;\
1226 \
1227     /*FIXME unroll */\
1228     __asm__ volatile(\
1229         "pxor %%mm7, %%mm7              \n\t"\
1230         "1:                             \n\t"\
1231         "movq (%0), %%mm0               \n\t"\
1232         "movq (%0), %%mm1               \n\t"\
1233         "punpcklbw %%mm7, %%mm0         \n\t"\
1234         "punpckhbw %%mm7, %%mm1         \n\t"\
1235         "movq %%mm0, (%1)               \n\t"\
1236         "movq %%mm1, 9*8(%1)            \n\t"\
1237         "add $8, %1                     \n\t"\
1238         "add %3, %0                     \n\t"\
1239         "decl %2                        \n\t"\
1240         " jnz 1b                        \n\t"\
1241         : "+r" (src), "+r" (temp_ptr), "+r"(count)\
1242         : "r" ((x86_reg)srcStride)\
1243         : "memory"\
1244     );\
1245     \
1246     temp_ptr= temp;\
1247     count=2;\
1248     \
1249 /*FIXME reorder for speed */\
1250     __asm__ volatile(\
1251         /*"pxor %%mm7, %%mm7              \n\t"*/\
1252         "1:                             \n\t"\
1253         "movq (%0), %%mm0               \n\t"\
1254         "movq 8(%0), %%mm1              \n\t"\
1255         "movq 16(%0), %%mm2             \n\t"\
1256         "movq 24(%0), %%mm3             \n\t"\
1257         QPEL_V_LOW(%%mm0, %%mm1, %%mm2, %%mm3, %5, %6, %5, 16(%0),  8(%0),   (%0), 32(%0), (%1), OP)\
1258         QPEL_V_LOW(%%mm1, %%mm2, %%mm3, %%mm0, %5, %6, %5,  8(%0),   (%0),   (%0), 40(%0), (%1, %3), OP)\
1259         "add %4, %1                     \n\t"\
1260         QPEL_V_LOW(%%mm2, %%mm3, %%mm0, %%mm1, %5, %6, %5,   (%0),   (%0),  8(%0), 48(%0), (%1), OP)\
1261         \
1262         QPEL_V_LOW(%%mm3, %%mm0, %%mm1, %%mm2, %5, %6, %5,   (%0),  8(%0), 16(%0), 56(%0), (%1, %3), OP)\
1263         "add %4, %1                     \n\t"\
1264         QPEL_V_LOW(%%mm0, %%mm1, %%mm2, %%mm3, %5, %6, %5,  8(%0), 16(%0), 24(%0), 64(%0), (%1), OP)\
1265         \
1266         QPEL_V_LOW(%%mm1, %%mm2, %%mm3, %%mm0, %5, %6, %5, 16(%0), 24(%0), 32(%0), 64(%0), (%1, %3), OP)\
1267         "add %4, %1                     \n\t"\
1268         QPEL_V_LOW(%%mm2, %%mm3, %%mm0, %%mm1, %5, %6, %5, 24(%0), 32(%0), 40(%0), 56(%0), (%1), OP)\
1269         QPEL_V_LOW(%%mm3, %%mm0, %%mm1, %%mm2, %5, %6, %5, 32(%0), 40(%0), 48(%0), 48(%0), (%1, %3), OP)\
1270                 \
1271         "add $72, %0                    \n\t"\
1272         "add %6, %1                     \n\t"\
1273         "decl %2                        \n\t"\
1274         " jnz 1b                        \n\t"\
1275          \
1276         : "+r"(temp_ptr), "+r"(dst), "+g"(count)\
1277         : "r"((x86_reg)dstStride), "r"(2*(x86_reg)dstStride), /*"m"(ff_pw_20), "m"(ff_pw_3),*/ "m"(ROUNDER), "g"(4-6*(x86_reg)dstStride)\
1278         : "memory"\
1279    );\
1280 }\
1281 \
1282 static void OPNAME ## qpel8_mc00_ ## MMX (uint8_t *dst, uint8_t *src, int stride){\
1283     OPNAME ## pixels8_ ## MMX(dst, src, stride, 8);\
1284 }\
1285 \
1286 static void OPNAME ## qpel8_mc10_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
1287     uint64_t temp[8];\
1288     uint8_t * const half= (uint8_t*)temp;\
1289     put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(half, src, 8, stride, 8);\
1290     OPNAME ## pixels8_l2_ ## MMX(dst, src, half, stride, stride, 8);\
1291 }\
1292 \
1293 static void OPNAME ## qpel8_mc20_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
1294     OPNAME ## mpeg4_qpel8_h_lowpass_ ## MMX(dst, src, stride, stride, 8);\
1295 }\
1296 \
1297 static void OPNAME ## qpel8_mc30_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
1298     uint64_t temp[8];\
1299     uint8_t * const half= (uint8_t*)temp;\
1300     put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(half, src, 8, stride, 8);\
1301     OPNAME ## pixels8_l2_ ## MMX(dst, src+1, half, stride, stride, 8);\
1302 }\
1303 \
1304 static void OPNAME ## qpel8_mc01_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
1305     uint64_t temp[8];\
1306     uint8_t * const half= (uint8_t*)temp;\
1307     put ## RND ## mpeg4_qpel8_v_lowpass_ ## MMX(half, src, 8, stride);\
1308     OPNAME ## pixels8_l2_ ## MMX(dst, src, half, stride, stride, 8);\
1309 }\
1310 \
1311 static void OPNAME ## qpel8_mc02_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
1312     OPNAME ## mpeg4_qpel8_v_lowpass_ ## MMX(dst, src, stride, stride);\
1313 }\
1314 \
1315 static void OPNAME ## qpel8_mc03_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
1316     uint64_t temp[8];\
1317     uint8_t * const half= (uint8_t*)temp;\
1318     put ## RND ## mpeg4_qpel8_v_lowpass_ ## MMX(half, src, 8, stride);\
1319     OPNAME ## pixels8_l2_ ## MMX(dst, src+stride, half, stride, stride, 8);\
1320 }\
1321 static void OPNAME ## qpel8_mc11_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
1322     uint64_t half[8 + 9];\
1323     uint8_t * const halfH= ((uint8_t*)half) + 64;\
1324     uint8_t * const halfHV= ((uint8_t*)half);\
1325     put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8, stride, 9);\
1326     put ## RND ## pixels8_l2_ ## MMX(halfH, src, halfH, 8, stride, 9);\
1327     put ## RND ## mpeg4_qpel8_v_lowpass_ ## MMX(halfHV, halfH, 8, 8);\
1328     OPNAME ## pixels8_l2_ ## MMX(dst, halfH, halfHV, stride, 8, 8);\
1329 }\
1330 static void OPNAME ## qpel8_mc31_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
1331     uint64_t half[8 + 9];\
1332     uint8_t * const halfH= ((uint8_t*)half) + 64;\
1333     uint8_t * const halfHV= ((uint8_t*)half);\
1334     put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8, stride, 9);\
1335     put ## RND ## pixels8_l2_ ## MMX(halfH, src+1, halfH, 8, stride, 9);\
1336     put ## RND ## mpeg4_qpel8_v_lowpass_ ## MMX(halfHV, halfH, 8, 8);\
1337     OPNAME ## pixels8_l2_ ## MMX(dst, halfH, halfHV, stride, 8, 8);\
1338 }\
1339 static void OPNAME ## qpel8_mc13_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
1340     uint64_t half[8 + 9];\
1341     uint8_t * const halfH= ((uint8_t*)half) + 64;\
1342     uint8_t * const halfHV= ((uint8_t*)half);\
1343     put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8, stride, 9);\
1344     put ## RND ## pixels8_l2_ ## MMX(halfH, src, halfH, 8, stride, 9);\
1345     put ## RND ## mpeg4_qpel8_v_lowpass_ ## MMX(halfHV, halfH, 8, 8);\
1346     OPNAME ## pixels8_l2_ ## MMX(dst, halfH+8, halfHV, stride, 8, 8);\
1347 }\
1348 static void OPNAME ## qpel8_mc33_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
1349     uint64_t half[8 + 9];\
1350     uint8_t * const halfH= ((uint8_t*)half) + 64;\
1351     uint8_t * const halfHV= ((uint8_t*)half);\
1352     put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8, stride, 9);\
1353     put ## RND ## pixels8_l2_ ## MMX(halfH, src+1, halfH, 8, stride, 9);\
1354     put ## RND ## mpeg4_qpel8_v_lowpass_ ## MMX(halfHV, halfH, 8, 8);\
1355     OPNAME ## pixels8_l2_ ## MMX(dst, halfH+8, halfHV, stride, 8, 8);\
1356 }\
1357 static void OPNAME ## qpel8_mc21_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
1358     uint64_t half[8 + 9];\
1359     uint8_t * const halfH= ((uint8_t*)half) + 64;\
1360     uint8_t * const halfHV= ((uint8_t*)half);\
1361     put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8, stride, 9);\
1362     put ## RND ## mpeg4_qpel8_v_lowpass_ ## MMX(halfHV, halfH, 8, 8);\
1363     OPNAME ## pixels8_l2_ ## MMX(dst, halfH, halfHV, stride, 8, 8);\
1364 }\
1365 static void OPNAME ## qpel8_mc23_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
1366     uint64_t half[8 + 9];\
1367     uint8_t * const halfH= ((uint8_t*)half) + 64;\
1368     uint8_t * const halfHV= ((uint8_t*)half);\
1369     put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8, stride, 9);\
1370     put ## RND ## mpeg4_qpel8_v_lowpass_ ## MMX(halfHV, halfH, 8, 8);\
1371     OPNAME ## pixels8_l2_ ## MMX(dst, halfH+8, halfHV, stride, 8, 8);\
1372 }\
1373 static void OPNAME ## qpel8_mc12_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
1374     uint64_t half[8 + 9];\
1375     uint8_t * const halfH= ((uint8_t*)half);\
1376     put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8, stride, 9);\
1377     put ## RND ## pixels8_l2_ ## MMX(halfH, src, halfH, 8, stride, 9);\
1378     OPNAME ## mpeg4_qpel8_v_lowpass_ ## MMX(dst, halfH, stride, 8);\
1379 }\
1380 static void OPNAME ## qpel8_mc32_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
1381     uint64_t half[8 + 9];\
1382     uint8_t * const halfH= ((uint8_t*)half);\
1383     put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8, stride, 9);\
1384     put ## RND ## pixels8_l2_ ## MMX(halfH, src+1, halfH, 8, stride, 9);\
1385     OPNAME ## mpeg4_qpel8_v_lowpass_ ## MMX(dst, halfH, stride, 8);\
1386 }\
1387 static void OPNAME ## qpel8_mc22_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
1388     uint64_t half[9];\
1389     uint8_t * const halfH= ((uint8_t*)half);\
1390     put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8, stride, 9);\
1391     OPNAME ## mpeg4_qpel8_v_lowpass_ ## MMX(dst, halfH, stride, 8);\
1392 }\
1393 static void OPNAME ## qpel16_mc00_ ## MMX (uint8_t *dst, uint8_t *src, int stride){\
1394     OPNAME ## pixels16_ ## MMX(dst, src, stride, 16);\
1395 }\
1396 \
1397 static void OPNAME ## qpel16_mc10_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
1398     uint64_t temp[32];\
1399     uint8_t * const half= (uint8_t*)temp;\
1400     put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(half, src, 16, stride, 16);\
1401     OPNAME ## pixels16_l2_ ## MMX(dst, src, half, stride, stride, 16);\
1402 }\
1403 \
1404 static void OPNAME ## qpel16_mc20_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
1405     OPNAME ## mpeg4_qpel16_h_lowpass_ ## MMX(dst, src, stride, stride, 16);\
1406 }\
1407 \
1408 static void OPNAME ## qpel16_mc30_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
1409     uint64_t temp[32];\
1410     uint8_t * const half= (uint8_t*)temp;\
1411     put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(half, src, 16, stride, 16);\
1412     OPNAME ## pixels16_l2_ ## MMX(dst, src+1, half, stride, stride, 16);\
1413 }\
1414 \
1415 static void OPNAME ## qpel16_mc01_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
1416     uint64_t temp[32];\
1417     uint8_t * const half= (uint8_t*)temp;\
1418     put ## RND ## mpeg4_qpel16_v_lowpass_ ## MMX(half, src, 16, stride);\
1419     OPNAME ## pixels16_l2_ ## MMX(dst, src, half, stride, stride, 16);\
1420 }\
1421 \
1422 static void OPNAME ## qpel16_mc02_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
1423     OPNAME ## mpeg4_qpel16_v_lowpass_ ## MMX(dst, src, stride, stride);\
1424 }\
1425 \
1426 static void OPNAME ## qpel16_mc03_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
1427     uint64_t temp[32];\
1428     uint8_t * const half= (uint8_t*)temp;\
1429     put ## RND ## mpeg4_qpel16_v_lowpass_ ## MMX(half, src, 16, stride);\
1430     OPNAME ## pixels16_l2_ ## MMX(dst, src+stride, half, stride, stride, 16);\
1431 }\
1432 static void OPNAME ## qpel16_mc11_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
1433     uint64_t half[16*2 + 17*2];\
1434     uint8_t * const halfH= ((uint8_t*)half) + 256;\
1435     uint8_t * const halfHV= ((uint8_t*)half);\
1436     put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16, stride, 17);\
1437     put ## RND ## pixels16_l2_ ## MMX(halfH, src, halfH, 16, stride, 17);\
1438     put ## RND ## mpeg4_qpel16_v_lowpass_ ## MMX(halfHV, halfH, 16, 16);\
1439     OPNAME ## pixels16_l2_ ## MMX(dst, halfH, halfHV, stride, 16, 16);\
1440 }\
1441 static void OPNAME ## qpel16_mc31_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
1442     uint64_t half[16*2 + 17*2];\
1443     uint8_t * const halfH= ((uint8_t*)half) + 256;\
1444     uint8_t * const halfHV= ((uint8_t*)half);\
1445     put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16, stride, 17);\
1446     put ## RND ## pixels16_l2_ ## MMX(halfH, src+1, halfH, 16, stride, 17);\
1447     put ## RND ## mpeg4_qpel16_v_lowpass_ ## MMX(halfHV, halfH, 16, 16);\
1448     OPNAME ## pixels16_l2_ ## MMX(dst, halfH, halfHV, stride, 16, 16);\
1449 }\
1450 static void OPNAME ## qpel16_mc13_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
1451     uint64_t half[16*2 + 17*2];\
1452     uint8_t * const halfH= ((uint8_t*)half) + 256;\
1453     uint8_t * const halfHV= ((uint8_t*)half);\
1454     put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16, stride, 17);\
1455     put ## RND ## pixels16_l2_ ## MMX(halfH, src, halfH, 16, stride, 17);\
1456     put ## RND ## mpeg4_qpel16_v_lowpass_ ## MMX(halfHV, halfH, 16, 16);\
1457     OPNAME ## pixels16_l2_ ## MMX(dst, halfH+16, halfHV, stride, 16, 16);\
1458 }\
1459 static void OPNAME ## qpel16_mc33_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
1460     uint64_t half[16*2 + 17*2];\
1461     uint8_t * const halfH= ((uint8_t*)half) + 256;\
1462     uint8_t * const halfHV= ((uint8_t*)half);\
1463     put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16, stride, 17);\
1464     put ## RND ## pixels16_l2_ ## MMX(halfH, src+1, halfH, 16, stride, 17);\
1465     put ## RND ## mpeg4_qpel16_v_lowpass_ ## MMX(halfHV, halfH, 16, 16);\
1466     OPNAME ## pixels16_l2_ ## MMX(dst, halfH+16, halfHV, stride, 16, 16);\
1467 }\
1468 static void OPNAME ## qpel16_mc21_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
1469     uint64_t half[16*2 + 17*2];\
1470     uint8_t * const halfH= ((uint8_t*)half) + 256;\
1471     uint8_t * const halfHV= ((uint8_t*)half);\
1472     put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16, stride, 17);\
1473     put ## RND ## mpeg4_qpel16_v_lowpass_ ## MMX(halfHV, halfH, 16, 16);\
1474     OPNAME ## pixels16_l2_ ## MMX(dst, halfH, halfHV, stride, 16, 16);\
1475 }\
1476 static void OPNAME ## qpel16_mc23_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
1477     uint64_t half[16*2 + 17*2];\
1478     uint8_t * const halfH= ((uint8_t*)half) + 256;\
1479     uint8_t * const halfHV= ((uint8_t*)half);\
1480     put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16, stride, 17);\
1481     put ## RND ## mpeg4_qpel16_v_lowpass_ ## MMX(halfHV, halfH, 16, 16);\
1482     OPNAME ## pixels16_l2_ ## MMX(dst, halfH+16, halfHV, stride, 16, 16);\
1483 }\
1484 static void OPNAME ## qpel16_mc12_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
1485     uint64_t half[17*2];\
1486     uint8_t * const halfH= ((uint8_t*)half);\
1487     put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16, stride, 17);\
1488     put ## RND ## pixels16_l2_ ## MMX(halfH, src, halfH, 16, stride, 17);\
1489     OPNAME ## mpeg4_qpel16_v_lowpass_ ## MMX(dst, halfH, stride, 16);\
1490 }\
1491 static void OPNAME ## qpel16_mc32_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
1492     uint64_t half[17*2];\
1493     uint8_t * const halfH= ((uint8_t*)half);\
1494     put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16, stride, 17);\
1495     put ## RND ## pixels16_l2_ ## MMX(halfH, src+1, halfH, 16, stride, 17);\
1496     OPNAME ## mpeg4_qpel16_v_lowpass_ ## MMX(dst, halfH, stride, 16);\
1497 }\
1498 static void OPNAME ## qpel16_mc22_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
1499     uint64_t half[17*2];\
1500     uint8_t * const halfH= ((uint8_t*)half);\
1501     put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16, stride, 17);\
1502     OPNAME ## mpeg4_qpel16_v_lowpass_ ## MMX(dst, halfH, stride, 16);\
1503 }
1504
1505 #define PUT_OP(a,b,temp, size) "mov" #size " " #a ", " #b "        \n\t"
1506 #define AVG_3DNOW_OP(a,b,temp, size) \
1507 "mov" #size " " #b ", " #temp "   \n\t"\
1508 "pavgusb " #temp ", " #a "        \n\t"\
1509 "mov" #size " " #a ", " #b "      \n\t"
1510 #define AVG_MMX2_OP(a,b,temp, size) \
1511 "mov" #size " " #b ", " #temp "   \n\t"\
1512 "pavgb " #temp ", " #a "          \n\t"\
1513 "mov" #size " " #a ", " #b "      \n\t"
1514
1515 QPEL_BASE(put_       , ff_pw_16, _       , PUT_OP, PUT_OP)
1516 QPEL_BASE(avg_       , ff_pw_16, _       , AVG_MMX2_OP, AVG_3DNOW_OP)
1517 QPEL_BASE(put_no_rnd_, ff_pw_15, _no_rnd_, PUT_OP, PUT_OP)
1518 QPEL_OP(put_       , ff_pw_16, _       , PUT_OP, 3dnow)
1519 QPEL_OP(avg_       , ff_pw_16, _       , AVG_3DNOW_OP, 3dnow)
1520 QPEL_OP(put_no_rnd_, ff_pw_15, _no_rnd_, PUT_OP, 3dnow)
1521 QPEL_OP(put_       , ff_pw_16, _       , PUT_OP, mmx2)
1522 QPEL_OP(avg_       , ff_pw_16, _       , AVG_MMX2_OP, mmx2)
1523 QPEL_OP(put_no_rnd_, ff_pw_15, _no_rnd_, PUT_OP, mmx2)
1524
1525 /***********************************/
1526 /* bilinear qpel: not compliant to any spec, only for -lavdopts fast */
1527
1528 #define QPEL_2TAP_XY(OPNAME, SIZE, MMX, XY, HPEL)\
1529 static void OPNAME ## 2tap_qpel ## SIZE ## _mc ## XY ## _ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
1530     OPNAME ## pixels ## SIZE ## HPEL(dst, src, stride, SIZE);\
1531 }
1532 #define QPEL_2TAP_L3(OPNAME, SIZE, MMX, XY, S0, S1, S2)\
1533 static void OPNAME ## 2tap_qpel ## SIZE ## _mc ## XY ## _ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
1534     OPNAME ## 2tap_qpel ## SIZE ## _l3_ ## MMX(dst, src+S0, stride, SIZE, S1, S2);\
1535 }
1536
1537 #define QPEL_2TAP(OPNAME, SIZE, MMX)\
1538 QPEL_2TAP_XY(OPNAME, SIZE, MMX, 20, _x2_ ## MMX)\
1539 QPEL_2TAP_XY(OPNAME, SIZE, MMX, 02, _y2_ ## MMX)\
1540 QPEL_2TAP_XY(OPNAME, SIZE, MMX, 22, _xy2_mmx)\
1541 static const qpel_mc_func OPNAME ## 2tap_qpel ## SIZE ## _mc00_ ## MMX =\
1542                           OPNAME ## qpel ## SIZE ## _mc00_ ## MMX;\
1543 static const qpel_mc_func OPNAME ## 2tap_qpel ## SIZE ## _mc21_ ## MMX =\
1544                           OPNAME ## 2tap_qpel ## SIZE ## _mc20_ ## MMX;\
1545 static const qpel_mc_func OPNAME ## 2tap_qpel ## SIZE ## _mc12_ ## MMX =\
1546                           OPNAME ## 2tap_qpel ## SIZE ## _mc02_ ## MMX;\
1547 static void OPNAME ## 2tap_qpel ## SIZE ## _mc32_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
1548     OPNAME ## pixels ## SIZE ## _y2_ ## MMX(dst, src+1, stride, SIZE);\
1549 }\
1550 static void OPNAME ## 2tap_qpel ## SIZE ## _mc23_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
1551     OPNAME ## pixels ## SIZE ## _x2_ ## MMX(dst, src+stride, stride, SIZE);\
1552 }\
1553 QPEL_2TAP_L3(OPNAME, SIZE, MMX, 10, 0,         1,       0)\
1554 QPEL_2TAP_L3(OPNAME, SIZE, MMX, 30, 1,        -1,       0)\
1555 QPEL_2TAP_L3(OPNAME, SIZE, MMX, 01, 0,         stride,  0)\
1556 QPEL_2TAP_L3(OPNAME, SIZE, MMX, 03, stride,   -stride,  0)\
1557 QPEL_2TAP_L3(OPNAME, SIZE, MMX, 11, 0,         stride,  1)\
1558 QPEL_2TAP_L3(OPNAME, SIZE, MMX, 31, 1,         stride, -1)\
1559 QPEL_2TAP_L3(OPNAME, SIZE, MMX, 13, stride,   -stride,  1)\
1560 QPEL_2TAP_L3(OPNAME, SIZE, MMX, 33, stride+1, -stride, -1)\
1561
1562 QPEL_2TAP(put_, 16, mmx2)
1563 QPEL_2TAP(avg_, 16, mmx2)
1564 QPEL_2TAP(put_,  8, mmx2)
1565 QPEL_2TAP(avg_,  8, mmx2)
1566 QPEL_2TAP(put_, 16, 3dnow)
1567 QPEL_2TAP(avg_, 16, 3dnow)
1568 QPEL_2TAP(put_,  8, 3dnow)
1569 QPEL_2TAP(avg_,  8, 3dnow)
1570
1571
1572 #if HAVE_YASM
1573 typedef void emu_edge_core_func (uint8_t *buf, const uint8_t *src,
1574                                  x86_reg linesize, x86_reg start_y,
1575                                  x86_reg end_y, x86_reg block_h,
1576                                  x86_reg start_x, x86_reg end_x,
1577                                  x86_reg block_w);
1578 extern emu_edge_core_func ff_emu_edge_core_mmx;
1579 extern emu_edge_core_func ff_emu_edge_core_sse;
1580
1581 static av_always_inline
1582 void emulated_edge_mc(uint8_t *buf, const uint8_t *src, int linesize,
1583                       int block_w, int block_h,
1584                       int src_x, int src_y, int w, int h,
1585                       emu_edge_core_func *core_fn)
1586 {
1587     int start_y, start_x, end_y, end_x, src_y_add=0;
1588
1589     if(src_y>= h){
1590         src_y_add = h-1-src_y;
1591         src_y=h-1;
1592     }else if(src_y<=-block_h){
1593         src_y_add = 1-block_h-src_y;
1594         src_y=1-block_h;
1595     }
1596     if(src_x>= w){
1597         src+= (w-1-src_x);
1598         src_x=w-1;
1599     }else if(src_x<=-block_w){
1600         src+= (1-block_w-src_x);
1601         src_x=1-block_w;
1602     }
1603
1604     start_y= FFMAX(0, -src_y);
1605     start_x= FFMAX(0, -src_x);
1606     end_y= FFMIN(block_h, h-src_y);
1607     end_x= FFMIN(block_w, w-src_x);
1608     assert(start_x < end_x && block_w > 0);
1609     assert(start_y < end_y && block_h > 0);
1610
1611     // fill in the to-be-copied part plus all above/below
1612     src += (src_y_add+start_y)*linesize + start_x;
1613     buf += start_x;
1614     core_fn(buf, src, linesize, start_y, end_y, block_h, start_x, end_x, block_w);
1615 }
1616
1617 #if ARCH_X86_32
1618 static av_noinline
1619 void emulated_edge_mc_mmx(uint8_t *buf, const uint8_t *src, int linesize,
1620                           int block_w, int block_h,
1621                           int src_x, int src_y, int w, int h)
1622 {
1623     emulated_edge_mc(buf, src, linesize, block_w, block_h, src_x, src_y,
1624                      w, h, &ff_emu_edge_core_mmx);
1625 }
1626 #endif
1627 static av_noinline
1628 void emulated_edge_mc_sse(uint8_t *buf, const uint8_t *src, int linesize,
1629                           int block_w, int block_h,
1630                           int src_x, int src_y, int w, int h)
1631 {
1632     emulated_edge_mc(buf, src, linesize, block_w, block_h, src_x, src_y,
1633                      w, h, &ff_emu_edge_core_sse);
1634 }
1635 #endif /* HAVE_YASM */
1636
1637 typedef void emulated_edge_mc_func (uint8_t *dst, const uint8_t *src,
1638                                     int linesize, int block_w, int block_h,
1639                                     int src_x, int src_y, int w, int h);
1640
1641 static av_always_inline
1642 void gmc(uint8_t *dst, uint8_t *src, int stride, int h, int ox, int oy,
1643          int dxx, int dxy, int dyx, int dyy, int shift, int r, int width, int height,
1644          emulated_edge_mc_func *emu_edge_fn)
1645 {
1646     const int w = 8;
1647     const int ix = ox>>(16+shift);
1648     const int iy = oy>>(16+shift);
1649     const int oxs = ox>>4;
1650     const int oys = oy>>4;
1651     const int dxxs = dxx>>4;
1652     const int dxys = dxy>>4;
1653     const int dyxs = dyx>>4;
1654     const int dyys = dyy>>4;
1655     const uint16_t r4[4] = {r,r,r,r};
1656     const uint16_t dxy4[4] = {dxys,dxys,dxys,dxys};
1657     const uint16_t dyy4[4] = {dyys,dyys,dyys,dyys};
1658     const uint64_t shift2 = 2*shift;
1659     uint8_t edge_buf[(h+1)*stride];
1660     int x, y;
1661
1662     const int dxw = (dxx-(1<<(16+shift)))*(w-1);
1663     const int dyh = (dyy-(1<<(16+shift)))*(h-1);
1664     const int dxh = dxy*(h-1);
1665     const int dyw = dyx*(w-1);
1666     if( // non-constant fullpel offset (3% of blocks)
1667         ((ox^(ox+dxw)) | (ox^(ox+dxh)) | (ox^(ox+dxw+dxh)) |
1668          (oy^(oy+dyw)) | (oy^(oy+dyh)) | (oy^(oy+dyw+dyh))) >> (16+shift)
1669         // uses more than 16 bits of subpel mv (only at huge resolution)
1670         || (dxx|dxy|dyx|dyy)&15 )
1671     {
1672         //FIXME could still use mmx for some of the rows
1673         ff_gmc_c(dst, src, stride, h, ox, oy, dxx, dxy, dyx, dyy, shift, r, width, height);
1674         return;
1675     }
1676
1677     src += ix + iy*stride;
1678     if( (unsigned)ix >= width-w ||
1679         (unsigned)iy >= height-h )
1680     {
1681         emu_edge_fn(edge_buf, src, stride, w+1, h+1, ix, iy, width, height);
1682         src = edge_buf;
1683     }
1684
1685     __asm__ volatile(
1686         "movd         %0, %%mm6 \n\t"
1687         "pxor      %%mm7, %%mm7 \n\t"
1688         "punpcklwd %%mm6, %%mm6 \n\t"
1689         "punpcklwd %%mm6, %%mm6 \n\t"
1690         :: "r"(1<<shift)
1691     );
1692
1693     for(x=0; x<w; x+=4){
1694         uint16_t dx4[4] = { oxs - dxys + dxxs*(x+0),
1695                             oxs - dxys + dxxs*(x+1),
1696                             oxs - dxys + dxxs*(x+2),
1697                             oxs - dxys + dxxs*(x+3) };
1698         uint16_t dy4[4] = { oys - dyys + dyxs*(x+0),
1699                             oys - dyys + dyxs*(x+1),
1700                             oys - dyys + dyxs*(x+2),
1701                             oys - dyys + dyxs*(x+3) };
1702
1703         for(y=0; y<h; y++){
1704             __asm__ volatile(
1705                 "movq   %0,  %%mm4 \n\t"
1706                 "movq   %1,  %%mm5 \n\t"
1707                 "paddw  %2,  %%mm4 \n\t"
1708                 "paddw  %3,  %%mm5 \n\t"
1709                 "movq   %%mm4, %0  \n\t"
1710                 "movq   %%mm5, %1  \n\t"
1711                 "psrlw  $12, %%mm4 \n\t"
1712                 "psrlw  $12, %%mm5 \n\t"
1713                 : "+m"(*dx4), "+m"(*dy4)
1714                 : "m"(*dxy4), "m"(*dyy4)
1715             );
1716
1717             __asm__ volatile(
1718                 "movq   %%mm6, %%mm2 \n\t"
1719                 "movq   %%mm6, %%mm1 \n\t"
1720                 "psubw  %%mm4, %%mm2 \n\t"
1721                 "psubw  %%mm5, %%mm1 \n\t"
1722                 "movq   %%mm2, %%mm0 \n\t"
1723                 "movq   %%mm4, %%mm3 \n\t"
1724                 "pmullw %%mm1, %%mm0 \n\t" // (s-dx)*(s-dy)
1725                 "pmullw %%mm5, %%mm3 \n\t" // dx*dy
1726                 "pmullw %%mm5, %%mm2 \n\t" // (s-dx)*dy
1727                 "pmullw %%mm4, %%mm1 \n\t" // dx*(s-dy)
1728
1729                 "movd   %4,    %%mm5 \n\t"
1730                 "movd   %3,    %%mm4 \n\t"
1731                 "punpcklbw %%mm7, %%mm5 \n\t"
1732                 "punpcklbw %%mm7, %%mm4 \n\t"
1733                 "pmullw %%mm5, %%mm3 \n\t" // src[1,1] * dx*dy
1734                 "pmullw %%mm4, %%mm2 \n\t" // src[0,1] * (s-dx)*dy
1735
1736                 "movd   %2,    %%mm5 \n\t"
1737                 "movd   %1,    %%mm4 \n\t"
1738                 "punpcklbw %%mm7, %%mm5 \n\t"
1739                 "punpcklbw %%mm7, %%mm4 \n\t"
1740                 "pmullw %%mm5, %%mm1 \n\t" // src[1,0] * dx*(s-dy)
1741                 "pmullw %%mm4, %%mm0 \n\t" // src[0,0] * (s-dx)*(s-dy)
1742                 "paddw  %5,    %%mm1 \n\t"
1743                 "paddw  %%mm3, %%mm2 \n\t"
1744                 "paddw  %%mm1, %%mm0 \n\t"
1745                 "paddw  %%mm2, %%mm0 \n\t"
1746
1747                 "psrlw    %6,    %%mm0 \n\t"
1748                 "packuswb %%mm0, %%mm0 \n\t"
1749                 "movd     %%mm0, %0    \n\t"
1750
1751                 : "=m"(dst[x+y*stride])
1752                 : "m"(src[0]), "m"(src[1]),
1753                   "m"(src[stride]), "m"(src[stride+1]),
1754                   "m"(*r4), "m"(shift2)
1755             );
1756             src += stride;
1757         }
1758         src += 4-h*stride;
1759     }
1760 }
1761
1762 #if HAVE_YASM
1763 #if ARCH_X86_32
1764 static void gmc_mmx(uint8_t *dst, uint8_t *src, int stride, int h, int ox, int oy,
1765                     int dxx, int dxy, int dyx, int dyy, int shift, int r, int width, int height)
1766 {
1767     gmc(dst, src, stride, h, ox, oy, dxx, dxy, dyx, dyy, shift, r,
1768         width, height, &emulated_edge_mc_mmx);
1769 }
1770 #endif
1771 static void gmc_sse(uint8_t *dst, uint8_t *src, int stride, int h, int ox, int oy,
1772                     int dxx, int dxy, int dyx, int dyy, int shift, int r, int width, int height)
1773 {
1774     gmc(dst, src, stride, h, ox, oy, dxx, dxy, dyx, dyy, shift, r,
1775         width, height, &emulated_edge_mc_sse);
1776 }
1777 #else
1778 static void gmc_mmx(uint8_t *dst, uint8_t *src, int stride, int h, int ox, int oy,
1779                     int dxx, int dxy, int dyx, int dyy, int shift, int r, int width, int height)
1780 {
1781     gmc(dst, src, stride, h, ox, oy, dxx, dxy, dyx, dyy, shift, r,
1782         width, height, &ff_emulated_edge_mc_8);
1783 }
1784 #endif
1785
1786 #define PREFETCH(name, op) \
1787 static void name(void *mem, int stride, int h){\
1788     const uint8_t *p= mem;\
1789     do{\
1790         __asm__ volatile(#op" %0" :: "m"(*p));\
1791         p+= stride;\
1792     }while(--h);\
1793 }
1794 PREFETCH(prefetch_mmx2,  prefetcht0)
1795 PREFETCH(prefetch_3dnow, prefetch)
1796 #undef PREFETCH
1797
1798 #include "h264_qpel_mmx.c"
1799
1800 void ff_put_h264_chroma_mc8_mmx_rnd   (uint8_t *dst, uint8_t *src,
1801                                        int stride, int h, int x, int y);
1802 void ff_put_rv40_chroma_mc8_mmx       (uint8_t *dst, uint8_t *src,
1803                                        int stride, int h, int x, int y);
1804 void ff_avg_h264_chroma_mc8_mmx2_rnd  (uint8_t *dst, uint8_t *src,
1805                                        int stride, int h, int x, int y);
1806 void ff_avg_rv40_chroma_mc8_mmx2      (uint8_t *dst, uint8_t *src,
1807                                        int stride, int h, int x, int y);
1808 void ff_avg_h264_chroma_mc8_3dnow_rnd (uint8_t *dst, uint8_t *src,
1809                                        int stride, int h, int x, int y);
1810 void ff_avg_rv40_chroma_mc8_3dnow     (uint8_t *dst, uint8_t *src,
1811                                        int stride, int h, int x, int y);
1812
1813 void ff_put_h264_chroma_mc4_mmx       (uint8_t *dst, uint8_t *src,
1814                                        int stride, int h, int x, int y);
1815 void ff_put_rv40_chroma_mc4_mmx       (uint8_t *dst, uint8_t *src,
1816                                        int stride, int h, int x, int y);
1817 void ff_avg_h264_chroma_mc4_mmx2      (uint8_t *dst, uint8_t *src,
1818                                        int stride, int h, int x, int y);
1819 void ff_avg_rv40_chroma_mc4_mmx2      (uint8_t *dst, uint8_t *src,
1820                                        int stride, int h, int x, int y);
1821 void ff_avg_h264_chroma_mc4_3dnow     (uint8_t *dst, uint8_t *src,
1822                                        int stride, int h, int x, int y);
1823 void ff_avg_rv40_chroma_mc4_3dnow     (uint8_t *dst, uint8_t *src,
1824                                        int stride, int h, int x, int y);
1825
1826 void ff_put_h264_chroma_mc2_mmx2      (uint8_t *dst, uint8_t *src,
1827                                        int stride, int h, int x, int y);
1828 void ff_avg_h264_chroma_mc2_mmx2      (uint8_t *dst, uint8_t *src,
1829                                        int stride, int h, int x, int y);
1830
1831 void ff_put_h264_chroma_mc8_ssse3_rnd (uint8_t *dst, uint8_t *src,
1832                                        int stride, int h, int x, int y);
1833 void ff_put_h264_chroma_mc4_ssse3     (uint8_t *dst, uint8_t *src,
1834                                        int stride, int h, int x, int y);
1835
1836 void ff_avg_h264_chroma_mc8_ssse3_rnd (uint8_t *dst, uint8_t *src,
1837                                        int stride, int h, int x, int y);
1838 void ff_avg_h264_chroma_mc4_ssse3     (uint8_t *dst, uint8_t *src,
1839                                        int stride, int h, int x, int y);
1840
1841 #define CHROMA_MC(OP, NUM, DEPTH, OPT) \
1842 void ff_ ## OP ## _h264_chroma_mc ## NUM ## _ ## DEPTH ## _ ## OPT \
1843                                       (uint8_t *dst, uint8_t *src,\
1844                                        int stride, int h, int x, int y);
1845
1846 CHROMA_MC(put, 2, 10, mmxext)
1847 CHROMA_MC(avg, 2, 10, mmxext)
1848 CHROMA_MC(put, 4, 10, mmxext)
1849 CHROMA_MC(avg, 4, 10, mmxext)
1850 CHROMA_MC(put, 8, 10, sse2)
1851 CHROMA_MC(avg, 8, 10, sse2)
1852 CHROMA_MC(put, 8, 10, avx)
1853 CHROMA_MC(avg, 8, 10, avx)
1854
1855 /* CAVS specific */
1856 void ff_put_cavs_qpel8_mc00_mmx2(uint8_t *dst, uint8_t *src, int stride) {
1857     put_pixels8_mmx(dst, src, stride, 8);
1858 }
1859 void ff_avg_cavs_qpel8_mc00_mmx2(uint8_t *dst, uint8_t *src, int stride) {
1860     avg_pixels8_mmx(dst, src, stride, 8);
1861 }
1862 void ff_put_cavs_qpel16_mc00_mmx2(uint8_t *dst, uint8_t *src, int stride) {
1863     put_pixels16_mmx(dst, src, stride, 16);
1864 }
1865 void ff_avg_cavs_qpel16_mc00_mmx2(uint8_t *dst, uint8_t *src, int stride) {
1866     avg_pixels16_mmx(dst, src, stride, 16);
1867 }
1868
1869 /* VC1 specific */
1870 void ff_put_vc1_mspel_mc00_mmx(uint8_t *dst, const uint8_t *src, int stride, int rnd) {
1871     put_pixels8_mmx(dst, src, stride, 8);
1872 }
1873 void ff_avg_vc1_mspel_mc00_mmx2(uint8_t *dst, const uint8_t *src, int stride, int rnd) {
1874     avg_pixels8_mmx2(dst, src, stride, 8);
1875 }
1876
1877 /* XXX: those functions should be suppressed ASAP when all IDCTs are
1878    converted */
1879 #if CONFIG_GPL
1880 static void ff_libmpeg2mmx_idct_put(uint8_t *dest, int line_size, DCTELEM *block)
1881 {
1882     ff_mmx_idct (block);
1883     ff_put_pixels_clamped_mmx(block, dest, line_size);
1884 }
1885 static void ff_libmpeg2mmx_idct_add(uint8_t *dest, int line_size, DCTELEM *block)
1886 {
1887     ff_mmx_idct (block);
1888     ff_add_pixels_clamped_mmx(block, dest, line_size);
1889 }
1890 static void ff_libmpeg2mmx2_idct_put(uint8_t *dest, int line_size, DCTELEM *block)
1891 {
1892     ff_mmxext_idct (block);
1893     ff_put_pixels_clamped_mmx(block, dest, line_size);
1894 }
1895 static void ff_libmpeg2mmx2_idct_add(uint8_t *dest, int line_size, DCTELEM *block)
1896 {
1897     ff_mmxext_idct (block);
1898     ff_add_pixels_clamped_mmx(block, dest, line_size);
1899 }
1900 #endif
1901 static void ff_idct_xvid_mmx_put(uint8_t *dest, int line_size, DCTELEM *block)
1902 {
1903     ff_idct_xvid_mmx (block);
1904     ff_put_pixels_clamped_mmx(block, dest, line_size);
1905 }
1906 static void ff_idct_xvid_mmx_add(uint8_t *dest, int line_size, DCTELEM *block)
1907 {
1908     ff_idct_xvid_mmx (block);
1909     ff_add_pixels_clamped_mmx(block, dest, line_size);
1910 }
1911 static void ff_idct_xvid_mmx2_put(uint8_t *dest, int line_size, DCTELEM *block)
1912 {
1913     ff_idct_xvid_mmx2 (block);
1914     ff_put_pixels_clamped_mmx(block, dest, line_size);
1915 }
1916 static void ff_idct_xvid_mmx2_add(uint8_t *dest, int line_size, DCTELEM *block)
1917 {
1918     ff_idct_xvid_mmx2 (block);
1919     ff_add_pixels_clamped_mmx(block, dest, line_size);
1920 }
1921
1922 static void vorbis_inverse_coupling_3dnow(float *mag, float *ang, int blocksize)
1923 {
1924     int i;
1925     __asm__ volatile("pxor %%mm7, %%mm7":);
1926     for(i=0; i<blocksize; i+=2) {
1927         __asm__ volatile(
1928             "movq    %0,    %%mm0 \n\t"
1929             "movq    %1,    %%mm1 \n\t"
1930             "movq    %%mm0, %%mm2 \n\t"
1931             "movq    %%mm1, %%mm3 \n\t"
1932             "pfcmpge %%mm7, %%mm2 \n\t" // m <= 0.0
1933             "pfcmpge %%mm7, %%mm3 \n\t" // a <= 0.0
1934             "pslld   $31,   %%mm2 \n\t" // keep only the sign bit
1935             "pxor    %%mm2, %%mm1 \n\t"
1936             "movq    %%mm3, %%mm4 \n\t"
1937             "pand    %%mm1, %%mm3 \n\t"
1938             "pandn   %%mm1, %%mm4 \n\t"
1939             "pfadd   %%mm0, %%mm3 \n\t" // a = m + ((a<0) & (a ^ sign(m)))
1940             "pfsub   %%mm4, %%mm0 \n\t" // m = m + ((a>0) & (a ^ sign(m)))
1941             "movq    %%mm3, %1    \n\t"
1942             "movq    %%mm0, %0    \n\t"
1943             :"+m"(mag[i]), "+m"(ang[i])
1944             ::"memory"
1945         );
1946     }
1947     __asm__ volatile("femms");
1948 }
1949 static void vorbis_inverse_coupling_sse(float *mag, float *ang, int blocksize)
1950 {
1951     int i;
1952
1953     __asm__ volatile(
1954             "movaps  %0,     %%xmm5 \n\t"
1955         ::"m"(ff_pdw_80000000[0])
1956     );
1957     for(i=0; i<blocksize; i+=4) {
1958         __asm__ volatile(
1959             "movaps  %0,     %%xmm0 \n\t"
1960             "movaps  %1,     %%xmm1 \n\t"
1961             "xorps   %%xmm2, %%xmm2 \n\t"
1962             "xorps   %%xmm3, %%xmm3 \n\t"
1963             "cmpleps %%xmm0, %%xmm2 \n\t" // m <= 0.0
1964             "cmpleps %%xmm1, %%xmm3 \n\t" // a <= 0.0
1965             "andps   %%xmm5, %%xmm2 \n\t" // keep only the sign bit
1966             "xorps   %%xmm2, %%xmm1 \n\t"
1967             "movaps  %%xmm3, %%xmm4 \n\t"
1968             "andps   %%xmm1, %%xmm3 \n\t"
1969             "andnps  %%xmm1, %%xmm4 \n\t"
1970             "addps   %%xmm0, %%xmm3 \n\t" // a = m + ((a<0) & (a ^ sign(m)))
1971             "subps   %%xmm4, %%xmm0 \n\t" // m = m + ((a>0) & (a ^ sign(m)))
1972             "movaps  %%xmm3, %1     \n\t"
1973             "movaps  %%xmm0, %0     \n\t"
1974             :"+m"(mag[i]), "+m"(ang[i])
1975             ::"memory"
1976         );
1977     }
1978 }
1979
1980 #define IF1(x) x
1981 #define IF0(x)
1982
1983 #define MIX5(mono,stereo)\
1984     __asm__ volatile(\
1985         "movss          0(%2), %%xmm5 \n"\
1986         "movss          8(%2), %%xmm6 \n"\
1987         "movss         24(%2), %%xmm7 \n"\
1988         "shufps    $0, %%xmm5, %%xmm5 \n"\
1989         "shufps    $0, %%xmm6, %%xmm6 \n"\
1990         "shufps    $0, %%xmm7, %%xmm7 \n"\
1991         "1: \n"\
1992         "movaps       (%0,%1), %%xmm0 \n"\
1993         "movaps  0x400(%0,%1), %%xmm1 \n"\
1994         "movaps  0x800(%0,%1), %%xmm2 \n"\
1995         "movaps  0xc00(%0,%1), %%xmm3 \n"\
1996         "movaps 0x1000(%0,%1), %%xmm4 \n"\
1997         "mulps         %%xmm5, %%xmm0 \n"\
1998         "mulps         %%xmm6, %%xmm1 \n"\
1999         "mulps         %%xmm5, %%xmm2 \n"\
2000         "mulps         %%xmm7, %%xmm3 \n"\
2001         "mulps         %%xmm7, %%xmm4 \n"\
2002  stereo("addps         %%xmm1, %%xmm0 \n")\
2003         "addps         %%xmm1, %%xmm2 \n"\
2004         "addps         %%xmm3, %%xmm0 \n"\
2005         "addps         %%xmm4, %%xmm2 \n"\
2006    mono("addps         %%xmm2, %%xmm0 \n")\
2007         "movaps  %%xmm0,      (%0,%1) \n"\
2008  stereo("movaps  %%xmm2, 0x400(%0,%1) \n")\
2009         "add $16, %0 \n"\
2010         "jl 1b \n"\
2011         :"+&r"(i)\
2012         :"r"(samples[0]+len), "r"(matrix)\
2013         :XMM_CLOBBERS("%xmm0", "%xmm1", "%xmm2", "%xmm3", \
2014                       "%xmm4", "%xmm5", "%xmm6", "%xmm7",)\
2015          "memory"\
2016     );
2017
2018 #define MIX_MISC(stereo)\
2019     __asm__ volatile(\
2020         "1: \n"\
2021         "movaps  (%3,%0), %%xmm0 \n"\
2022  stereo("movaps   %%xmm0, %%xmm1 \n")\
2023         "mulps    %%xmm4, %%xmm0 \n"\
2024  stereo("mulps    %%xmm5, %%xmm1 \n")\
2025         "lea 1024(%3,%0), %1 \n"\
2026         "mov %5, %2 \n"\
2027         "2: \n"\
2028         "movaps   (%1),   %%xmm2 \n"\
2029  stereo("movaps   %%xmm2, %%xmm3 \n")\
2030         "mulps   (%4,%2), %%xmm2 \n"\
2031  stereo("mulps 16(%4,%2), %%xmm3 \n")\
2032         "addps    %%xmm2, %%xmm0 \n"\
2033  stereo("addps    %%xmm3, %%xmm1 \n")\
2034         "add $1024, %1 \n"\
2035         "add $32, %2 \n"\
2036         "jl 2b \n"\
2037         "movaps   %%xmm0,     (%3,%0) \n"\
2038  stereo("movaps   %%xmm1, 1024(%3,%0) \n")\
2039         "add $16, %0 \n"\
2040         "jl 1b \n"\
2041         :"+&r"(i), "=&r"(j), "=&r"(k)\
2042         :"r"(samples[0]+len), "r"(matrix_simd+in_ch), "g"((intptr_t)-32*(in_ch-1))\
2043         :"memory"\
2044     );
2045
2046 static void ac3_downmix_sse(float (*samples)[256], float (*matrix)[2], int out_ch, int in_ch, int len)
2047 {
2048     int (*matrix_cmp)[2] = (int(*)[2])matrix;
2049     intptr_t i,j,k;
2050
2051     i = -len*sizeof(float);
2052     if(in_ch == 5 && out_ch == 2 && !(matrix_cmp[0][1]|matrix_cmp[2][0]|matrix_cmp[3][1]|matrix_cmp[4][0]|(matrix_cmp[1][0]^matrix_cmp[1][1])|(matrix_cmp[0][0]^matrix_cmp[2][1]))) {
2053         MIX5(IF0,IF1);
2054     } else if(in_ch == 5 && out_ch == 1 && matrix_cmp[0][0]==matrix_cmp[2][0] && matrix_cmp[3][0]==matrix_cmp[4][0]) {
2055         MIX5(IF1,IF0);
2056     } else {
2057         DECLARE_ALIGNED(16, float, matrix_simd)[AC3_MAX_CHANNELS][2][4];
2058         j = 2*in_ch*sizeof(float);
2059         __asm__ volatile(
2060             "1: \n"
2061             "sub $8, %0 \n"
2062             "movss     (%2,%0), %%xmm4 \n"
2063             "movss    4(%2,%0), %%xmm5 \n"
2064             "shufps $0, %%xmm4, %%xmm4 \n"
2065             "shufps $0, %%xmm5, %%xmm5 \n"
2066             "movaps %%xmm4,   (%1,%0,4) \n"
2067             "movaps %%xmm5, 16(%1,%0,4) \n"
2068             "jg 1b \n"
2069             :"+&r"(j)
2070             :"r"(matrix_simd), "r"(matrix)
2071             :"memory"
2072         );
2073         if(out_ch == 2) {
2074             MIX_MISC(IF1);
2075         } else {
2076             MIX_MISC(IF0);
2077         }
2078     }
2079 }
2080
2081 static void vector_fmul_3dnow(float *dst, const float *src0, const float *src1, int len){
2082     x86_reg i = (len-4)*4;
2083     __asm__ volatile(
2084         "1: \n\t"
2085         "movq    (%2,%0), %%mm0 \n\t"
2086         "movq   8(%2,%0), %%mm1 \n\t"
2087         "pfmul   (%3,%0), %%mm0 \n\t"
2088         "pfmul  8(%3,%0), %%mm1 \n\t"
2089         "movq   %%mm0,  (%1,%0) \n\t"
2090         "movq   %%mm1, 8(%1,%0) \n\t"
2091         "sub  $16, %0 \n\t"
2092         "jge 1b \n\t"
2093         "femms  \n\t"
2094         :"+r"(i)
2095         :"r"(dst), "r"(src0), "r"(src1)
2096         :"memory"
2097     );
2098 }
2099 static void vector_fmul_sse(float *dst, const float *src0, const float *src1, int len){
2100     x86_reg i = (len-8)*4;
2101     __asm__ volatile(
2102         "1: \n\t"
2103         "movaps    (%2,%0), %%xmm0 \n\t"
2104         "movaps  16(%2,%0), %%xmm1 \n\t"
2105         "mulps     (%3,%0), %%xmm0 \n\t"
2106         "mulps   16(%3,%0), %%xmm1 \n\t"
2107         "movaps  %%xmm0,   (%1,%0) \n\t"
2108         "movaps  %%xmm1, 16(%1,%0) \n\t"
2109         "sub  $32, %0 \n\t"
2110         "jge 1b \n\t"
2111         :"+r"(i)
2112         :"r"(dst), "r"(src0), "r"(src1)
2113         :"memory"
2114     );
2115 }
2116
2117 static void vector_fmul_reverse_3dnow2(float *dst, const float *src0, const float *src1, int len){
2118     x86_reg i = len*4-16;
2119     __asm__ volatile(
2120         "1: \n\t"
2121         "pswapd   8(%1), %%mm0 \n\t"
2122         "pswapd    (%1), %%mm1 \n\t"
2123         "pfmul  (%3,%0), %%mm0 \n\t"
2124         "pfmul 8(%3,%0), %%mm1 \n\t"
2125         "movq  %%mm0,  (%2,%0) \n\t"
2126         "movq  %%mm1, 8(%2,%0) \n\t"
2127         "add   $16, %1 \n\t"
2128         "sub   $16, %0 \n\t"
2129         "jge   1b \n\t"
2130         :"+r"(i), "+r"(src1)
2131         :"r"(dst), "r"(src0)
2132     );
2133     __asm__ volatile("femms");
2134 }
2135 static void vector_fmul_reverse_sse(float *dst, const float *src0, const float *src1, int len){
2136     x86_reg i = len*4-32;
2137     __asm__ volatile(
2138         "1: \n\t"
2139         "movaps        16(%1), %%xmm0 \n\t"
2140         "movaps          (%1), %%xmm1 \n\t"
2141         "shufps $0x1b, %%xmm0, %%xmm0 \n\t"
2142         "shufps $0x1b, %%xmm1, %%xmm1 \n\t"
2143         "mulps        (%3,%0), %%xmm0 \n\t"
2144         "mulps      16(%3,%0), %%xmm1 \n\t"
2145         "movaps     %%xmm0,   (%2,%0) \n\t"
2146         "movaps     %%xmm1, 16(%2,%0) \n\t"
2147         "add    $32, %1 \n\t"
2148         "sub    $32, %0 \n\t"
2149         "jge    1b \n\t"
2150         :"+r"(i), "+r"(src1)
2151         :"r"(dst), "r"(src0)
2152     );
2153 }
2154
2155 static void vector_fmul_add_3dnow(float *dst, const float *src0, const float *src1,
2156                                   const float *src2, int len){
2157     x86_reg i = (len-4)*4;
2158     __asm__ volatile(
2159         "1: \n\t"
2160         "movq    (%2,%0), %%mm0 \n\t"
2161         "movq   8(%2,%0), %%mm1 \n\t"
2162         "pfmul   (%3,%0), %%mm0 \n\t"
2163         "pfmul  8(%3,%0), %%mm1 \n\t"
2164         "pfadd   (%4,%0), %%mm0 \n\t"
2165         "pfadd  8(%4,%0), %%mm1 \n\t"
2166         "movq  %%mm0,   (%1,%0) \n\t"
2167         "movq  %%mm1,  8(%1,%0) \n\t"
2168         "sub  $16, %0 \n\t"
2169         "jge  1b \n\t"
2170         :"+r"(i)
2171         :"r"(dst), "r"(src0), "r"(src1), "r"(src2)
2172         :"memory"
2173     );
2174     __asm__ volatile("femms");
2175 }
2176 static void vector_fmul_add_sse(float *dst, const float *src0, const float *src1,
2177                                 const float *src2, int len){
2178     x86_reg i = (len-8)*4;
2179     __asm__ volatile(
2180         "1: \n\t"
2181         "movaps   (%2,%0), %%xmm0 \n\t"
2182         "movaps 16(%2,%0), %%xmm1 \n\t"
2183         "mulps    (%3,%0), %%xmm0 \n\t"
2184         "mulps  16(%3,%0), %%xmm1 \n\t"
2185         "addps    (%4,%0), %%xmm0 \n\t"
2186         "addps  16(%4,%0), %%xmm1 \n\t"
2187         "movaps %%xmm0,   (%1,%0) \n\t"
2188         "movaps %%xmm1, 16(%1,%0) \n\t"
2189         "sub  $32, %0 \n\t"
2190         "jge  1b \n\t"
2191         :"+r"(i)
2192         :"r"(dst), "r"(src0), "r"(src1), "r"(src2)
2193         :"memory"
2194     );
2195 }
2196
2197 #if HAVE_6REGS
2198 static void vector_fmul_window_3dnow2(float *dst, const float *src0, const float *src1,
2199                                       const float *win, int len){
2200     x86_reg i = -len*4;
2201     x86_reg j = len*4-8;
2202     __asm__ volatile(
2203         "1: \n"
2204         "pswapd  (%5,%1), %%mm1 \n"
2205         "movq    (%5,%0), %%mm0 \n"
2206         "pswapd  (%4,%1), %%mm5 \n"
2207         "movq    (%3,%0), %%mm4 \n"
2208         "movq      %%mm0, %%mm2 \n"
2209         "movq      %%mm1, %%mm3 \n"
2210         "pfmul     %%mm4, %%mm2 \n" // src0[len+i]*win[len+i]
2211         "pfmul     %%mm5, %%mm3 \n" // src1[    j]*win[len+j]
2212         "pfmul     %%mm4, %%mm1 \n" // src0[len+i]*win[len+j]
2213         "pfmul     %%mm5, %%mm0 \n" // src1[    j]*win[len+i]
2214         "pfadd     %%mm3, %%mm2 \n"
2215         "pfsub     %%mm0, %%mm1 \n"
2216         "pswapd    %%mm2, %%mm2 \n"
2217         "movq      %%mm1, (%2,%0) \n"
2218         "movq      %%mm2, (%2,%1) \n"
2219         "sub $8, %1 \n"
2220         "add $8, %0 \n"
2221         "jl 1b \n"
2222         "femms \n"
2223         :"+r"(i), "+r"(j)
2224         :"r"(dst+len), "r"(src0+len), "r"(src1), "r"(win+len)
2225     );
2226 }
2227
2228 static void vector_fmul_window_sse(float *dst, const float *src0, const float *src1,
2229                                    const float *win, int len){
2230     x86_reg i = -len*4;
2231     x86_reg j = len*4-16;
2232     __asm__ volatile(
2233         "1: \n"
2234         "movaps       (%5,%1), %%xmm1 \n"
2235         "movaps       (%5,%0), %%xmm0 \n"
2236         "movaps       (%4,%1), %%xmm5 \n"
2237         "movaps       (%3,%0), %%xmm4 \n"
2238         "shufps $0x1b, %%xmm1, %%xmm1 \n"
2239         "shufps $0x1b, %%xmm5, %%xmm5 \n"
2240         "movaps        %%xmm0, %%xmm2 \n"
2241         "movaps        %%xmm1, %%xmm3 \n"
2242         "mulps         %%xmm4, %%xmm2 \n" // src0[len+i]*win[len+i]
2243         "mulps         %%xmm5, %%xmm3 \n" // src1[    j]*win[len+j]
2244         "mulps         %%xmm4, %%xmm1 \n" // src0[len+i]*win[len+j]
2245         "mulps         %%xmm5, %%xmm0 \n" // src1[    j]*win[len+i]
2246         "addps         %%xmm3, %%xmm2 \n"
2247         "subps         %%xmm0, %%xmm1 \n"
2248         "shufps $0x1b, %%xmm2, %%xmm2 \n"
2249         "movaps        %%xmm1, (%2,%0) \n"
2250         "movaps        %%xmm2, (%2,%1) \n"
2251         "sub $16, %1 \n"
2252         "add $16, %0 \n"
2253         "jl 1b \n"
2254         :"+r"(i), "+r"(j)
2255         :"r"(dst+len), "r"(src0+len), "r"(src1), "r"(win+len)
2256     );
2257 }
2258 #endif /* HAVE_6REGS */
2259
2260 static void vector_clipf_sse(float *dst, const float *src, float min, float max,
2261                              int len)
2262 {
2263     x86_reg i = (len-16)*4;
2264     __asm__ volatile(
2265         "movss  %3, %%xmm4 \n"
2266         "movss  %4, %%xmm5 \n"
2267         "shufps $0, %%xmm4, %%xmm4 \n"
2268         "shufps $0, %%xmm5, %%xmm5 \n"
2269         "1: \n\t"
2270         "movaps    (%2,%0), %%xmm0 \n\t" // 3/1 on intel
2271         "movaps  16(%2,%0), %%xmm1 \n\t"
2272         "movaps  32(%2,%0), %%xmm2 \n\t"
2273         "movaps  48(%2,%0), %%xmm3 \n\t"
2274         "maxps      %%xmm4, %%xmm0 \n\t"
2275         "maxps      %%xmm4, %%xmm1 \n\t"
2276         "maxps      %%xmm4, %%xmm2 \n\t"
2277         "maxps      %%xmm4, %%xmm3 \n\t"
2278         "minps      %%xmm5, %%xmm0 \n\t"
2279         "minps      %%xmm5, %%xmm1 \n\t"
2280         "minps      %%xmm5, %%xmm2 \n\t"
2281         "minps      %%xmm5, %%xmm3 \n\t"
2282         "movaps  %%xmm0,   (%1,%0) \n\t"
2283         "movaps  %%xmm1, 16(%1,%0) \n\t"
2284         "movaps  %%xmm2, 32(%1,%0) \n\t"
2285         "movaps  %%xmm3, 48(%1,%0) \n\t"
2286         "sub  $64, %0 \n\t"
2287         "jge 1b \n\t"
2288         :"+&r"(i)
2289         :"r"(dst), "r"(src), "m"(min), "m"(max)
2290         :"memory"
2291     );
2292 }
2293
2294 void ff_vp3_idct_mmx(int16_t *input_data);
2295 void ff_vp3_idct_put_mmx(uint8_t *dest, int line_size, DCTELEM *block);
2296 void ff_vp3_idct_add_mmx(uint8_t *dest, int line_size, DCTELEM *block);
2297
2298 void ff_vp3_idct_dc_add_mmx2(uint8_t *dest, int line_size, const DCTELEM *block);
2299
2300 void ff_vp3_v_loop_filter_mmx2(uint8_t *src, int stride, int *bounding_values);
2301 void ff_vp3_h_loop_filter_mmx2(uint8_t *src, int stride, int *bounding_values);
2302
2303 void ff_vp3_idct_sse2(int16_t *input_data);
2304 void ff_vp3_idct_put_sse2(uint8_t *dest, int line_size, DCTELEM *block);
2305 void ff_vp3_idct_add_sse2(uint8_t *dest, int line_size, DCTELEM *block);
2306
2307 int32_t ff_scalarproduct_int16_mmx2(const int16_t *v1, const int16_t *v2, int order, int shift);
2308 int32_t ff_scalarproduct_int16_sse2(const int16_t *v1, const int16_t *v2, int order, int shift);
2309 int32_t ff_scalarproduct_and_madd_int16_mmx2(int16_t *v1, const int16_t *v2, const int16_t *v3, int order, int mul);
2310 int32_t ff_scalarproduct_and_madd_int16_sse2(int16_t *v1, const int16_t *v2, const int16_t *v3, int order, int mul);
2311 int32_t ff_scalarproduct_and_madd_int16_ssse3(int16_t *v1, const int16_t *v2, const int16_t *v3, int order, int mul);
2312
2313 void ff_apply_window_int16_mmxext    (int16_t *output, const int16_t *input,
2314                                       const int16_t *window, unsigned int len);
2315 void ff_apply_window_int16_mmxext_ba (int16_t *output, const int16_t *input,
2316                                       const int16_t *window, unsigned int len);
2317 void ff_apply_window_int16_sse2      (int16_t *output, const int16_t *input,
2318                                       const int16_t *window, unsigned int len);
2319 void ff_apply_window_int16_sse2_ba   (int16_t *output, const int16_t *input,
2320                                       const int16_t *window, unsigned int len);
2321 void ff_apply_window_int16_ssse3     (int16_t *output, const int16_t *input,
2322                                       const int16_t *window, unsigned int len);
2323 void ff_apply_window_int16_ssse3_atom(int16_t *output, const int16_t *input,
2324                                       const int16_t *window, unsigned int len);
2325
2326 void ff_add_hfyu_median_prediction_mmx2(uint8_t *dst, const uint8_t *top, const uint8_t *diff, int w, int *left, int *left_top);
2327 int  ff_add_hfyu_left_prediction_ssse3(uint8_t *dst, const uint8_t *src, int w, int left);
2328 int  ff_add_hfyu_left_prediction_sse4(uint8_t *dst, const uint8_t *src, int w, int left);
2329
2330 float ff_scalarproduct_float_sse(const float *v1, const float *v2, int order);
2331
2332 void ff_vector_clip_int32_mmx     (int32_t *dst, const int32_t *src, int32_t min,
2333                                    int32_t max, unsigned int len);
2334 void ff_vector_clip_int32_sse2    (int32_t *dst, const int32_t *src, int32_t min,
2335                                    int32_t max, unsigned int len);
2336 void ff_vector_clip_int32_sse2_int(int32_t *dst, const int32_t *src, int32_t min,
2337                                    int32_t max, unsigned int len);
2338 void ff_vector_clip_int32_sse41   (int32_t *dst, const int32_t *src, int32_t min,
2339                                    int32_t max, unsigned int len);
2340
2341 void dsputil_init_mmx(DSPContext* c, AVCodecContext *avctx)
2342 {
2343     int mm_flags = av_get_cpu_flags();
2344     const int high_bit_depth = avctx->codec_id == CODEC_ID_H264 && avctx->bits_per_raw_sample > 8;
2345     const int bit_depth = avctx->bits_per_raw_sample;
2346
2347     if (avctx->dsp_mask) {
2348         if (avctx->dsp_mask & AV_CPU_FLAG_FORCE)
2349             mm_flags |= (avctx->dsp_mask & 0xffff);
2350         else
2351             mm_flags &= ~(avctx->dsp_mask & 0xffff);
2352     }
2353
2354 #if 0
2355     av_log(avctx, AV_LOG_INFO, "libavcodec: CPU flags:");
2356     if (mm_flags & AV_CPU_FLAG_MMX)
2357         av_log(avctx, AV_LOG_INFO, " mmx");
2358     if (mm_flags & AV_CPU_FLAG_MMX2)
2359         av_log(avctx, AV_LOG_INFO, " mmx2");
2360     if (mm_flags & AV_CPU_FLAG_3DNOW)
2361         av_log(avctx, AV_LOG_INFO, " 3dnow");
2362     if (mm_flags & AV_CPU_FLAG_SSE)
2363         av_log(avctx, AV_LOG_INFO, " sse");
2364     if (mm_flags & AV_CPU_FLAG_SSE2)
2365         av_log(avctx, AV_LOG_INFO, " sse2");
2366     av_log(avctx, AV_LOG_INFO, "\n");
2367 #endif
2368
2369     if (mm_flags & AV_CPU_FLAG_MMX) {
2370         const int idct_algo= avctx->idct_algo;
2371
2372         if(avctx->lowres==0){
2373             if(idct_algo==FF_IDCT_AUTO || idct_algo==FF_IDCT_SIMPLEMMX){
2374                 c->idct_put= ff_simple_idct_put_mmx;
2375                 c->idct_add= ff_simple_idct_add_mmx;
2376                 c->idct    = ff_simple_idct_mmx;
2377                 c->idct_permutation_type= FF_SIMPLE_IDCT_PERM;
2378 #if CONFIG_GPL
2379             }else if(idct_algo==FF_IDCT_LIBMPEG2MMX){
2380                 if(mm_flags & AV_CPU_FLAG_MMX2){
2381                     c->idct_put= ff_libmpeg2mmx2_idct_put;
2382                     c->idct_add= ff_libmpeg2mmx2_idct_add;
2383                     c->idct    = ff_mmxext_idct;
2384                 }else{
2385                     c->idct_put= ff_libmpeg2mmx_idct_put;
2386                     c->idct_add= ff_libmpeg2mmx_idct_add;
2387                     c->idct    = ff_mmx_idct;
2388                 }
2389                 c->idct_permutation_type= FF_LIBMPEG2_IDCT_PERM;
2390 #endif
2391             }else if((CONFIG_VP3_DECODER || CONFIG_VP5_DECODER || CONFIG_VP6_DECODER) &&
2392                      idct_algo==FF_IDCT_VP3 && HAVE_YASM){
2393                 if(mm_flags & AV_CPU_FLAG_SSE2){
2394                     c->idct_put= ff_vp3_idct_put_sse2;
2395                     c->idct_add= ff_vp3_idct_add_sse2;
2396                     c->idct    = ff_vp3_idct_sse2;
2397                     c->idct_permutation_type= FF_TRANSPOSE_IDCT_PERM;
2398                 }else{
2399                     c->idct_put= ff_vp3_idct_put_mmx;
2400                     c->idct_add= ff_vp3_idct_add_mmx;
2401                     c->idct    = ff_vp3_idct_mmx;
2402                     c->idct_permutation_type= FF_PARTTRANS_IDCT_PERM;
2403                 }
2404             }else if(idct_algo==FF_IDCT_CAVS){
2405                     c->idct_permutation_type= FF_TRANSPOSE_IDCT_PERM;
2406             }else if(idct_algo==FF_IDCT_XVIDMMX){
2407                 if(mm_flags & AV_CPU_FLAG_SSE2){
2408                     c->idct_put= ff_idct_xvid_sse2_put;
2409                     c->idct_add= ff_idct_xvid_sse2_add;
2410                     c->idct    = ff_idct_xvid_sse2;
2411                     c->idct_permutation_type= FF_SSE2_IDCT_PERM;
2412                 }else if(mm_flags & AV_CPU_FLAG_MMX2){
2413                     c->idct_put= ff_idct_xvid_mmx2_put;
2414                     c->idct_add= ff_idct_xvid_mmx2_add;
2415                     c->idct    = ff_idct_xvid_mmx2;
2416                 }else{
2417                     c->idct_put= ff_idct_xvid_mmx_put;
2418                     c->idct_add= ff_idct_xvid_mmx_add;
2419                     c->idct    = ff_idct_xvid_mmx;
2420                 }
2421             }
2422         }
2423
2424         c->put_pixels_clamped = ff_put_pixels_clamped_mmx;
2425         c->put_signed_pixels_clamped = ff_put_signed_pixels_clamped_mmx;
2426         c->add_pixels_clamped = ff_add_pixels_clamped_mmx;
2427         if (!high_bit_depth) {
2428         c->clear_block  = clear_block_mmx;
2429         c->clear_blocks = clear_blocks_mmx;
2430         if ((mm_flags & AV_CPU_FLAG_SSE) &&
2431             !(CONFIG_MPEG_XVMC_DECODER && avctx->xvmc_acceleration > 1)){
2432             /* XvMCCreateBlocks() may not allocate 16-byte aligned blocks */
2433             c->clear_block  = clear_block_sse;
2434             c->clear_blocks = clear_blocks_sse;
2435         }
2436         }
2437
2438 #define SET_HPEL_FUNCS(PFX, IDX, SIZE, CPU) \
2439         c->PFX ## _pixels_tab[IDX][0] = PFX ## _pixels ## SIZE ## _ ## CPU; \
2440         c->PFX ## _pixels_tab[IDX][1] = PFX ## _pixels ## SIZE ## _x2_ ## CPU; \
2441         c->PFX ## _pixels_tab[IDX][2] = PFX ## _pixels ## SIZE ## _y2_ ## CPU; \
2442         c->PFX ## _pixels_tab[IDX][3] = PFX ## _pixels ## SIZE ## _xy2_ ## CPU
2443
2444         if (!high_bit_depth) {
2445         SET_HPEL_FUNCS(put, 0, 16, mmx);
2446         SET_HPEL_FUNCS(put_no_rnd, 0, 16, mmx);
2447         SET_HPEL_FUNCS(avg, 0, 16, mmx);
2448         SET_HPEL_FUNCS(avg_no_rnd, 0, 16, mmx);
2449         SET_HPEL_FUNCS(put, 1, 8, mmx);
2450         SET_HPEL_FUNCS(put_no_rnd, 1, 8, mmx);
2451         SET_HPEL_FUNCS(avg, 1, 8, mmx);
2452         SET_HPEL_FUNCS(avg_no_rnd, 1, 8, mmx);
2453         }
2454
2455 #if ARCH_X86_32 || !HAVE_YASM
2456         c->gmc= gmc_mmx;
2457 #endif
2458 #if ARCH_X86_32 && HAVE_YASM
2459         if (!high_bit_depth)
2460         c->emulated_edge_mc = emulated_edge_mc_mmx;
2461 #endif
2462
2463         c->add_bytes= add_bytes_mmx;
2464
2465         if (!high_bit_depth)
2466         c->draw_edges = draw_edges_mmx;
2467
2468         if (CONFIG_H263_DECODER || CONFIG_H263_ENCODER) {
2469             c->h263_v_loop_filter= h263_v_loop_filter_mmx;
2470             c->h263_h_loop_filter= h263_h_loop_filter_mmx;
2471         }
2472
2473 #if HAVE_YASM
2474         if (!high_bit_depth) {
2475         c->put_h264_chroma_pixels_tab[0]= ff_put_h264_chroma_mc8_mmx_rnd;
2476         c->put_h264_chroma_pixels_tab[1]= ff_put_h264_chroma_mc4_mmx;
2477         }
2478
2479         c->put_rv40_chroma_pixels_tab[0]= ff_put_rv40_chroma_mc8_mmx;
2480         c->put_rv40_chroma_pixels_tab[1]= ff_put_rv40_chroma_mc4_mmx;
2481
2482         c->vector_clip_int32 = ff_vector_clip_int32_mmx;
2483 #endif
2484
2485         if (mm_flags & AV_CPU_FLAG_MMX2) {
2486             c->prefetch = prefetch_mmx2;
2487
2488             if (!high_bit_depth) {
2489             c->put_pixels_tab[0][1] = put_pixels16_x2_mmx2;
2490             c->put_pixels_tab[0][2] = put_pixels16_y2_mmx2;
2491
2492             c->avg_pixels_tab[0][0] = avg_pixels16_mmx2;
2493             c->avg_pixels_tab[0][1] = avg_pixels16_x2_mmx2;
2494             c->avg_pixels_tab[0][2] = avg_pixels16_y2_mmx2;
2495
2496             c->put_pixels_tab[1][1] = put_pixels8_x2_mmx2;
2497             c->put_pixels_tab[1][2] = put_pixels8_y2_mmx2;
2498
2499             c->avg_pixels_tab[1][0] = avg_pixels8_mmx2;
2500             c->avg_pixels_tab[1][1] = avg_pixels8_x2_mmx2;
2501             c->avg_pixels_tab[1][2] = avg_pixels8_y2_mmx2;
2502             }
2503
2504             if(!(avctx->flags & CODEC_FLAG_BITEXACT)){
2505                 if (!high_bit_depth) {
2506                 c->put_no_rnd_pixels_tab[0][1] = put_no_rnd_pixels16_x2_mmx2;
2507                 c->put_no_rnd_pixels_tab[0][2] = put_no_rnd_pixels16_y2_mmx2;
2508                 c->put_no_rnd_pixels_tab[1][1] = put_no_rnd_pixels8_x2_mmx2;
2509                 c->put_no_rnd_pixels_tab[1][2] = put_no_rnd_pixels8_y2_mmx2;
2510                 c->avg_pixels_tab[0][3] = avg_pixels16_xy2_mmx2;
2511                 c->avg_pixels_tab[1][3] = avg_pixels8_xy2_mmx2;
2512                 }
2513
2514                 if (CONFIG_VP3_DECODER && HAVE_YASM) {
2515                     c->vp3_v_loop_filter= ff_vp3_v_loop_filter_mmx2;
2516                     c->vp3_h_loop_filter= ff_vp3_h_loop_filter_mmx2;
2517                 }
2518             }
2519             if (CONFIG_VP3_DECODER && HAVE_YASM) {
2520                 c->vp3_idct_dc_add = ff_vp3_idct_dc_add_mmx2;
2521             }
2522
2523             if (CONFIG_VP3_DECODER
2524                 && (avctx->codec_id == CODEC_ID_VP3 || avctx->codec_id == CODEC_ID_THEORA)) {
2525                 c->put_no_rnd_pixels_tab[1][1] = put_no_rnd_pixels8_x2_exact_mmx2;
2526                 c->put_no_rnd_pixels_tab[1][2] = put_no_rnd_pixels8_y2_exact_mmx2;
2527             }
2528
2529 #define SET_QPEL_FUNCS(PFX, IDX, SIZE, CPU, PREFIX) \
2530             c->PFX ## _pixels_tab[IDX][ 0] = PREFIX ## PFX ## SIZE ## _mc00_ ## CPU; \
2531             c->PFX ## _pixels_tab[IDX][ 1] = PREFIX ## PFX ## SIZE ## _mc10_ ## CPU; \
2532             c->PFX ## _pixels_tab[IDX][ 2] = PREFIX ## PFX ## SIZE ## _mc20_ ## CPU; \
2533             c->PFX ## _pixels_tab[IDX][ 3] = PREFIX ## PFX ## SIZE ## _mc30_ ## CPU; \
2534             c->PFX ## _pixels_tab[IDX][ 4] = PREFIX ## PFX ## SIZE ## _mc01_ ## CPU; \
2535             c->PFX ## _pixels_tab[IDX][ 5] = PREFIX ## PFX ## SIZE ## _mc11_ ## CPU; \
2536             c->PFX ## _pixels_tab[IDX][ 6] = PREFIX ## PFX ## SIZE ## _mc21_ ## CPU; \
2537             c->PFX ## _pixels_tab[IDX][ 7] = PREFIX ## PFX ## SIZE ## _mc31_ ## CPU; \
2538             c->PFX ## _pixels_tab[IDX][ 8] = PREFIX ## PFX ## SIZE ## _mc02_ ## CPU; \
2539             c->PFX ## _pixels_tab[IDX][ 9] = PREFIX ## PFX ## SIZE ## _mc12_ ## CPU; \
2540             c->PFX ## _pixels_tab[IDX][10] = PREFIX ## PFX ## SIZE ## _mc22_ ## CPU; \
2541             c->PFX ## _pixels_tab[IDX][11] = PREFIX ## PFX ## SIZE ## _mc32_ ## CPU; \
2542             c->PFX ## _pixels_tab[IDX][12] = PREFIX ## PFX ## SIZE ## _mc03_ ## CPU; \
2543             c->PFX ## _pixels_tab[IDX][13] = PREFIX ## PFX ## SIZE ## _mc13_ ## CPU; \
2544             c->PFX ## _pixels_tab[IDX][14] = PREFIX ## PFX ## SIZE ## _mc23_ ## CPU; \
2545             c->PFX ## _pixels_tab[IDX][15] = PREFIX ## PFX ## SIZE ## _mc33_ ## CPU
2546
2547             SET_QPEL_FUNCS(put_qpel, 0, 16, mmx2, );
2548             SET_QPEL_FUNCS(put_qpel, 1, 8, mmx2, );
2549             SET_QPEL_FUNCS(put_no_rnd_qpel, 0, 16, mmx2, );
2550             SET_QPEL_FUNCS(put_no_rnd_qpel, 1, 8, mmx2, );
2551             SET_QPEL_FUNCS(avg_qpel, 0, 16, mmx2, );
2552             SET_QPEL_FUNCS(avg_qpel, 1, 8, mmx2, );
2553
2554             if (!high_bit_depth) {
2555             SET_QPEL_FUNCS(put_h264_qpel, 0, 16, mmx2, );
2556             SET_QPEL_FUNCS(put_h264_qpel, 1, 8, mmx2, );
2557             SET_QPEL_FUNCS(put_h264_qpel, 2, 4, mmx2, );
2558             SET_QPEL_FUNCS(avg_h264_qpel, 0, 16, mmx2, );
2559             SET_QPEL_FUNCS(avg_h264_qpel, 1, 8, mmx2, );
2560             SET_QPEL_FUNCS(avg_h264_qpel, 2, 4, mmx2, );
2561             }
2562             else if (bit_depth == 10) {
2563 #if HAVE_YASM
2564 #if !ARCH_X86_64
2565                 SET_QPEL_FUNCS(avg_h264_qpel, 0, 16, 10_mmxext, ff_);
2566                 SET_QPEL_FUNCS(put_h264_qpel, 0, 16, 10_mmxext, ff_);
2567                 SET_QPEL_FUNCS(put_h264_qpel, 1, 8,  10_mmxext, ff_);
2568                 SET_QPEL_FUNCS(avg_h264_qpel, 1, 8,  10_mmxext, ff_);
2569 #endif
2570                 SET_QPEL_FUNCS(put_h264_qpel, 2, 4,  10_mmxext, ff_);
2571                 SET_QPEL_FUNCS(avg_h264_qpel, 2, 4,  10_mmxext, ff_);
2572 #endif
2573             }
2574
2575             SET_QPEL_FUNCS(put_2tap_qpel, 0, 16, mmx2, );
2576             SET_QPEL_FUNCS(put_2tap_qpel, 1, 8, mmx2, );
2577             SET_QPEL_FUNCS(avg_2tap_qpel, 0, 16, mmx2, );
2578             SET_QPEL_FUNCS(avg_2tap_qpel, 1, 8, mmx2, );
2579
2580 #if HAVE_YASM
2581             c->avg_rv40_chroma_pixels_tab[0]= ff_avg_rv40_chroma_mc8_mmx2;
2582             c->avg_rv40_chroma_pixels_tab[1]= ff_avg_rv40_chroma_mc4_mmx2;
2583
2584             if (!high_bit_depth) {
2585             c->avg_h264_chroma_pixels_tab[0]= ff_avg_h264_chroma_mc8_mmx2_rnd;
2586             c->avg_h264_chroma_pixels_tab[1]= ff_avg_h264_chroma_mc4_mmx2;
2587             c->avg_h264_chroma_pixels_tab[2]= ff_avg_h264_chroma_mc2_mmx2;
2588             c->put_h264_chroma_pixels_tab[2]= ff_put_h264_chroma_mc2_mmx2;
2589             }
2590             if (bit_depth == 10) {
2591                 c->put_h264_chroma_pixels_tab[2]= ff_put_h264_chroma_mc2_10_mmxext;
2592                 c->avg_h264_chroma_pixels_tab[2]= ff_avg_h264_chroma_mc2_10_mmxext;
2593                 c->put_h264_chroma_pixels_tab[1]= ff_put_h264_chroma_mc4_10_mmxext;
2594                 c->avg_h264_chroma_pixels_tab[1]= ff_avg_h264_chroma_mc4_10_mmxext;
2595             }
2596
2597             c->add_hfyu_median_prediction = ff_add_hfyu_median_prediction_mmx2;
2598 #endif
2599 #if HAVE_7REGS
2600             if( mm_flags&AV_CPU_FLAG_3DNOW )
2601                 c->add_hfyu_median_prediction = add_hfyu_median_prediction_cmov;
2602 #endif
2603
2604         } else if (mm_flags & AV_CPU_FLAG_3DNOW) {
2605             c->prefetch = prefetch_3dnow;
2606
2607             if (!high_bit_depth) {
2608             c->put_pixels_tab[0][1] = put_pixels16_x2_3dnow;
2609             c->put_pixels_tab[0][2] = put_pixels16_y2_3dnow;
2610
2611             c->avg_pixels_tab[0][0] = avg_pixels16_3dnow;
2612             c->avg_pixels_tab[0][1] = avg_pixels16_x2_3dnow;
2613             c->avg_pixels_tab[0][2] = avg_pixels16_y2_3dnow;
2614
2615             c->put_pixels_tab[1][1] = put_pixels8_x2_3dnow;
2616             c->put_pixels_tab[1][2] = put_pixels8_y2_3dnow;
2617
2618             c->avg_pixels_tab[1][0] = avg_pixels8_3dnow;
2619             c->avg_pixels_tab[1][1] = avg_pixels8_x2_3dnow;
2620             c->avg_pixels_tab[1][2] = avg_pixels8_y2_3dnow;
2621
2622             if(!(avctx->flags & CODEC_FLAG_BITEXACT)){
2623                 c->put_no_rnd_pixels_tab[0][1] = put_no_rnd_pixels16_x2_3dnow;
2624                 c->put_no_rnd_pixels_tab[0][2] = put_no_rnd_pixels16_y2_3dnow;
2625                 c->put_no_rnd_pixels_tab[1][1] = put_no_rnd_pixels8_x2_3dnow;
2626                 c->put_no_rnd_pixels_tab[1][2] = put_no_rnd_pixels8_y2_3dnow;
2627                 c->avg_pixels_tab[0][3] = avg_pixels16_xy2_3dnow;
2628                 c->avg_pixels_tab[1][3] = avg_pixels8_xy2_3dnow;
2629             }
2630             }
2631
2632             if (CONFIG_VP3_DECODER
2633                 && (avctx->codec_id == CODEC_ID_VP3 || avctx->codec_id == CODEC_ID_THEORA)) {
2634                 c->put_no_rnd_pixels_tab[1][1] = put_no_rnd_pixels8_x2_exact_3dnow;
2635                 c->put_no_rnd_pixels_tab[1][2] = put_no_rnd_pixels8_y2_exact_3dnow;
2636             }
2637
2638             SET_QPEL_FUNCS(put_qpel, 0, 16, 3dnow, );
2639             SET_QPEL_FUNCS(put_qpel, 1, 8, 3dnow, );
2640             SET_QPEL_FUNCS(put_no_rnd_qpel, 0, 16, 3dnow, );
2641             SET_QPEL_FUNCS(put_no_rnd_qpel, 1, 8, 3dnow, );
2642             SET_QPEL_FUNCS(avg_qpel, 0, 16, 3dnow, );
2643             SET_QPEL_FUNCS(avg_qpel, 1, 8, 3dnow, );
2644
2645             if (!high_bit_depth) {
2646             SET_QPEL_FUNCS(put_h264_qpel, 0, 16, 3dnow, );
2647             SET_QPEL_FUNCS(put_h264_qpel, 1, 8, 3dnow, );
2648             SET_QPEL_FUNCS(put_h264_qpel, 2, 4, 3dnow, );
2649             SET_QPEL_FUNCS(avg_h264_qpel, 0, 16, 3dnow, );
2650             SET_QPEL_FUNCS(avg_h264_qpel, 1, 8, 3dnow, );
2651             SET_QPEL_FUNCS(avg_h264_qpel, 2, 4, 3dnow, );
2652             }
2653
2654             SET_QPEL_FUNCS(put_2tap_qpel, 0, 16, 3dnow, );
2655             SET_QPEL_FUNCS(put_2tap_qpel, 1, 8, 3dnow, );
2656             SET_QPEL_FUNCS(avg_2tap_qpel, 0, 16, 3dnow, );
2657             SET_QPEL_FUNCS(avg_2tap_qpel, 1, 8, 3dnow, );
2658
2659 #if HAVE_YASM
2660             if (!high_bit_depth) {
2661             c->avg_h264_chroma_pixels_tab[0]= ff_avg_h264_chroma_mc8_3dnow_rnd;
2662             c->avg_h264_chroma_pixels_tab[1]= ff_avg_h264_chroma_mc4_3dnow;
2663             }
2664
2665             c->avg_rv40_chroma_pixels_tab[0]= ff_avg_rv40_chroma_mc8_3dnow;
2666             c->avg_rv40_chroma_pixels_tab[1]= ff_avg_rv40_chroma_mc4_3dnow;
2667 #endif
2668         }
2669
2670
2671 #define H264_QPEL_FUNCS(x, y, CPU)\
2672             c->put_h264_qpel_pixels_tab[0][x+y*4] = put_h264_qpel16_mc##x##y##_##CPU;\
2673             c->put_h264_qpel_pixels_tab[1][x+y*4] = put_h264_qpel8_mc##x##y##_##CPU;\
2674             c->avg_h264_qpel_pixels_tab[0][x+y*4] = avg_h264_qpel16_mc##x##y##_##CPU;\
2675             c->avg_h264_qpel_pixels_tab[1][x+y*4] = avg_h264_qpel8_mc##x##y##_##CPU;
2676         if((mm_flags & AV_CPU_FLAG_SSE2) && !(mm_flags & AV_CPU_FLAG_3DNOW)){
2677             // these functions are slower than mmx on AMD, but faster on Intel
2678             if (!high_bit_depth) {
2679             c->put_pixels_tab[0][0] = put_pixels16_sse2;
2680             c->put_no_rnd_pixels_tab[0][0] = put_pixels16_sse2;
2681             c->avg_pixels_tab[0][0] = avg_pixels16_sse2;
2682             H264_QPEL_FUNCS(0, 0, sse2);
2683             }
2684         }
2685         if(mm_flags & AV_CPU_FLAG_SSE2){
2686             if (!high_bit_depth) {
2687             H264_QPEL_FUNCS(0, 1, sse2);
2688             H264_QPEL_FUNCS(0, 2, sse2);
2689             H264_QPEL_FUNCS(0, 3, sse2);
2690             H264_QPEL_FUNCS(1, 1, sse2);
2691             H264_QPEL_FUNCS(1, 2, sse2);
2692             H264_QPEL_FUNCS(1, 3, sse2);
2693             H264_QPEL_FUNCS(2, 1, sse2);
2694             H264_QPEL_FUNCS(2, 2, sse2);
2695             H264_QPEL_FUNCS(2, 3, sse2);
2696             H264_QPEL_FUNCS(3, 1, sse2);
2697             H264_QPEL_FUNCS(3, 2, sse2);
2698             H264_QPEL_FUNCS(3, 3, sse2);
2699             }
2700 #if HAVE_YASM
2701 #define H264_QPEL_FUNCS_10(x, y, CPU)\
2702             c->put_h264_qpel_pixels_tab[0][x+y*4] = ff_put_h264_qpel16_mc##x##y##_10_##CPU;\
2703             c->put_h264_qpel_pixels_tab[1][x+y*4] = ff_put_h264_qpel8_mc##x##y##_10_##CPU;\
2704             c->avg_h264_qpel_pixels_tab[0][x+y*4] = ff_avg_h264_qpel16_mc##x##y##_10_##CPU;\
2705             c->avg_h264_qpel_pixels_tab[1][x+y*4] = ff_avg_h264_qpel8_mc##x##y##_10_##CPU;
2706             if (bit_depth == 10) {
2707                 SET_QPEL_FUNCS(put_h264_qpel, 0, 16, 10_sse2, ff_);
2708                 SET_QPEL_FUNCS(put_h264_qpel, 1, 8,  10_sse2, ff_);
2709                 SET_QPEL_FUNCS(avg_h264_qpel, 0, 16, 10_sse2, ff_);
2710                 SET_QPEL_FUNCS(avg_h264_qpel, 1, 8,  10_sse2, ff_);
2711                 H264_QPEL_FUNCS_10(1, 0, sse2_cache64)
2712                 H264_QPEL_FUNCS_10(2, 0, sse2_cache64)
2713                 H264_QPEL_FUNCS_10(3, 0, sse2_cache64)
2714
2715                 c->put_h264_chroma_pixels_tab[0]= ff_put_h264_chroma_mc8_10_sse2;
2716                 c->avg_h264_chroma_pixels_tab[0]= ff_avg_h264_chroma_mc8_10_sse2;
2717             }
2718 #endif
2719         }
2720 #if HAVE_SSSE3
2721         if(mm_flags & AV_CPU_FLAG_SSSE3){
2722             if (!high_bit_depth) {
2723             H264_QPEL_FUNCS(1, 0, ssse3);
2724             H264_QPEL_FUNCS(1, 1, ssse3);
2725             H264_QPEL_FUNCS(1, 2, ssse3);
2726             H264_QPEL_FUNCS(1, 3, ssse3);
2727             H264_QPEL_FUNCS(2, 0, ssse3);
2728             H264_QPEL_FUNCS(2, 1, ssse3);
2729             H264_QPEL_FUNCS(2, 2, ssse3);
2730             H264_QPEL_FUNCS(2, 3, ssse3);
2731             H264_QPEL_FUNCS(3, 0, ssse3);
2732             H264_QPEL_FUNCS(3, 1, ssse3);
2733             H264_QPEL_FUNCS(3, 2, ssse3);
2734             H264_QPEL_FUNCS(3, 3, ssse3);
2735             }
2736 #if HAVE_YASM
2737             else if (bit_depth == 10) {
2738                 H264_QPEL_FUNCS_10(1, 0, ssse3_cache64)
2739                 H264_QPEL_FUNCS_10(2, 0, ssse3_cache64)
2740                 H264_QPEL_FUNCS_10(3, 0, ssse3_cache64)
2741             }
2742             if (!high_bit_depth) {
2743             c->put_h264_chroma_pixels_tab[0]= ff_put_h264_chroma_mc8_ssse3_rnd;
2744             c->avg_h264_chroma_pixels_tab[0]= ff_avg_h264_chroma_mc8_ssse3_rnd;
2745             c->put_h264_chroma_pixels_tab[1]= ff_put_h264_chroma_mc4_ssse3;
2746             c->avg_h264_chroma_pixels_tab[1]= ff_avg_h264_chroma_mc4_ssse3;
2747             }
2748             c->add_hfyu_left_prediction = ff_add_hfyu_left_prediction_ssse3;
2749             if (mm_flags & AV_CPU_FLAG_SSE4) // not really sse4, just slow on Conroe
2750                 c->add_hfyu_left_prediction = ff_add_hfyu_left_prediction_sse4;
2751 #endif
2752         }
2753 #endif
2754
2755         if(mm_flags & AV_CPU_FLAG_3DNOW){
2756             c->vorbis_inverse_coupling = vorbis_inverse_coupling_3dnow;
2757             c->vector_fmul = vector_fmul_3dnow;
2758         }
2759         if(mm_flags & AV_CPU_FLAG_3DNOWEXT){
2760             c->vector_fmul_reverse = vector_fmul_reverse_3dnow2;
2761 #if HAVE_6REGS
2762             c->vector_fmul_window = vector_fmul_window_3dnow2;
2763 #endif
2764         }
2765         if(mm_flags & AV_CPU_FLAG_MMX2){
2766 #if HAVE_YASM
2767             c->scalarproduct_int16 = ff_scalarproduct_int16_mmx2;
2768             c->scalarproduct_and_madd_int16 = ff_scalarproduct_and_madd_int16_mmx2;
2769             if (avctx->flags & CODEC_FLAG_BITEXACT) {
2770                 c->apply_window_int16 = ff_apply_window_int16_mmxext_ba;
2771             } else {
2772                 c->apply_window_int16 = ff_apply_window_int16_mmxext;
2773             }
2774 #endif
2775         }
2776         if(mm_flags & AV_CPU_FLAG_SSE){
2777             c->vorbis_inverse_coupling = vorbis_inverse_coupling_sse;
2778             c->ac3_downmix = ac3_downmix_sse;
2779             c->vector_fmul = vector_fmul_sse;
2780             c->vector_fmul_reverse = vector_fmul_reverse_sse;
2781             c->vector_fmul_add = vector_fmul_add_sse;
2782 #if HAVE_6REGS
2783             c->vector_fmul_window = vector_fmul_window_sse;
2784 #endif
2785             c->vector_clipf = vector_clipf_sse;
2786 #if HAVE_YASM
2787             c->scalarproduct_float = ff_scalarproduct_float_sse;
2788 #endif
2789         }
2790         if(mm_flags & AV_CPU_FLAG_3DNOW)
2791             c->vector_fmul_add = vector_fmul_add_3dnow; // faster than sse
2792         if(mm_flags & AV_CPU_FLAG_SSE2){
2793 #if HAVE_YASM
2794             c->scalarproduct_int16 = ff_scalarproduct_int16_sse2;
2795             c->scalarproduct_and_madd_int16 = ff_scalarproduct_and_madd_int16_sse2;
2796             if (mm_flags & AV_CPU_FLAG_ATOM) {
2797                 c->vector_clip_int32 = ff_vector_clip_int32_sse2_int;
2798             } else {
2799                 c->vector_clip_int32 = ff_vector_clip_int32_sse2;
2800             }
2801             if (avctx->flags & CODEC_FLAG_BITEXACT) {
2802                 c->apply_window_int16 = ff_apply_window_int16_sse2_ba;
2803             } else {
2804                 if (!(mm_flags & AV_CPU_FLAG_SSE2SLOW)) {
2805                     c->apply_window_int16 = ff_apply_window_int16_sse2;
2806                 }
2807             }
2808
2809             if (!high_bit_depth)
2810             c->emulated_edge_mc = emulated_edge_mc_sse;
2811             c->gmc= gmc_sse;
2812 #endif
2813         }
2814         if (mm_flags & AV_CPU_FLAG_SSSE3) {
2815 #if HAVE_YASM
2816             if (mm_flags & AV_CPU_FLAG_ATOM) {
2817                 c->apply_window_int16 = ff_apply_window_int16_ssse3_atom;
2818             } else {
2819                 c->apply_window_int16 = ff_apply_window_int16_ssse3;
2820             }
2821             if (!(mm_flags & (AV_CPU_FLAG_SSE42|AV_CPU_FLAG_3DNOW))) { // cachesplit
2822                 c->scalarproduct_and_madd_int16 = ff_scalarproduct_and_madd_int16_ssse3;
2823             }
2824 #endif
2825         }
2826
2827         if (mm_flags & AV_CPU_FLAG_SSE4 && HAVE_SSE) {
2828 #if HAVE_YASM
2829             c->vector_clip_int32 = ff_vector_clip_int32_sse41;
2830 #endif
2831         }
2832
2833 #if HAVE_AVX && HAVE_YASM
2834         if (mm_flags & AV_CPU_FLAG_AVX) {
2835             if (bit_depth == 10) {
2836                 //AVX implies !cache64.
2837                 //TODO: Port cache(32|64) detection from x264.
2838                 H264_QPEL_FUNCS_10(1, 0, sse2)
2839                 H264_QPEL_FUNCS_10(2, 0, sse2)
2840                 H264_QPEL_FUNCS_10(3, 0, sse2)
2841
2842                 c->put_h264_chroma_pixels_tab[0]= ff_put_h264_chroma_mc8_10_avx;
2843                 c->avg_h264_chroma_pixels_tab[0]= ff_avg_h264_chroma_mc8_10_avx;
2844             }
2845         }
2846 #endif
2847     }
2848
2849     if (CONFIG_ENCODERS)
2850         dsputilenc_init_mmx(c, avctx);
2851 }