bd1994700d9219e2085cf0c0a20e66387f5a8981
[ffmpeg.git] / 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  * MMX optimization by Nick Kurshev <nickols_k@mail.ru>
7  *
8  * This file is part of Libav.
9  *
10  * Libav is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * Libav is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with Libav; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  */
24
25 #include "libavutil/attributes.h"
26 #include "libavutil/cpu.h"
27 #include "libavutil/x86/asm.h"
28 #include "libavcodec/dsputil.h"
29 #include "libavcodec/h264dsp.h"
30 #include "libavcodec/mpegvideo.h"
31 #include "libavcodec/simple_idct.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_pw_15)   =   0x000F000F000F000FULL;
40 DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_17)   = { 0x0011001100110011ULL, 0x0011001100110011ULL };
41 DECLARE_ALIGNED(8,  const uint64_t, ff_pw_20)   =   0x0014001400140014ULL;
42 DECLARE_ALIGNED(8,  const uint64_t, ff_pw_42)   =   0x002A002A002A002AULL;
43 DECLARE_ALIGNED(8,  const uint64_t, ff_pw_53)   =   0x0035003500350035ULL;
44 DECLARE_ALIGNED(8,  const uint64_t, ff_pw_96)   =   0x0060006000600060ULL;
45 DECLARE_ALIGNED(8,  const uint64_t, ff_pw_128)  =   0x0080008000800080ULL;
46 DECLARE_ALIGNED(8,  const uint64_t, ff_pw_255)  =   0x00ff00ff00ff00ffULL;
47 DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_512)  = { 0x0200020002000200ULL, 0x0200020002000200ULL };
48 DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_1019) = { 0x03FB03FB03FB03FBULL, 0x03FB03FB03FB03FBULL };
49
50 DECLARE_ALIGNED(8,  const uint64_t, ff_pb_3F)   =   0x3F3F3F3F3F3F3F3FULL;
51 DECLARE_ALIGNED(8,  const uint64_t, ff_pb_FC)   =   0xFCFCFCFCFCFCFCFCULL;
52
53 DECLARE_ALIGNED(16, const double, ff_pd_1)[2] = { 1.0, 1.0 };
54 DECLARE_ALIGNED(16, const double, ff_pd_2)[2] = { 2.0, 2.0 };
55
56
57 void ff_put_pixels8_l2_mmxext(uint8_t *dst, uint8_t *src1, uint8_t *src2,
58                               int dstStride, int src1Stride, int h);
59 void ff_put_no_rnd_pixels8_l2_mmxext(uint8_t *dst, uint8_t *src1,
60                                      uint8_t *src2, int dstStride,
61                                      int src1Stride, int h);
62 void ff_avg_pixels8_l2_mmxext(uint8_t *dst, uint8_t *src1, uint8_t *src2,
63                               int dstStride, int src1Stride, int h);
64 void ff_put_pixels16_l2_mmxext(uint8_t *dst, uint8_t *src1, uint8_t *src2,
65                                int dstStride, int src1Stride, int h);
66 void ff_avg_pixels16_l2_mmxext(uint8_t *dst, uint8_t *src1, uint8_t *src2,
67                                int dstStride, int src1Stride, int h);
68 void ff_put_no_rnd_pixels16_l2_mmxext(uint8_t *dst, uint8_t *src1, uint8_t *src2,
69                                       int dstStride, int src1Stride, int h);
70 void ff_put_mpeg4_qpel16_h_lowpass_mmxext(uint8_t *dst, uint8_t *src,
71                                          int dstStride, int srcStride, int h);
72 void ff_avg_mpeg4_qpel16_h_lowpass_mmxext(uint8_t *dst, uint8_t *src,
73                                          int dstStride, int srcStride, int h);
74 void ff_put_no_rnd_mpeg4_qpel16_h_lowpass_mmxext(uint8_t *dst, uint8_t *src,
75                                                  int dstStride, int srcStride,
76                                                  int h);
77 void ff_put_mpeg4_qpel8_h_lowpass_mmxext(uint8_t *dst, uint8_t *src,
78                                         int dstStride, int srcStride, int h);
79 void ff_avg_mpeg4_qpel8_h_lowpass_mmxext(uint8_t *dst, uint8_t *src,
80                                         int dstStride, int srcStride, int h);
81 void ff_put_no_rnd_mpeg4_qpel8_h_lowpass_mmxext(uint8_t *dst, uint8_t *src,
82                                                 int dstStride, int srcStride,
83                                                 int h);
84 void ff_put_mpeg4_qpel16_v_lowpass_mmxext(uint8_t *dst, uint8_t *src,
85                                          int dstStride, int srcStride);
86 void ff_avg_mpeg4_qpel16_v_lowpass_mmxext(uint8_t *dst, uint8_t *src,
87                                          int dstStride, int srcStride);
88 void ff_put_no_rnd_mpeg4_qpel16_v_lowpass_mmxext(uint8_t *dst, uint8_t *src,
89                                                  int dstStride, int srcStride);
90 void ff_put_mpeg4_qpel8_v_lowpass_mmxext(uint8_t *dst, uint8_t *src,
91                                         int dstStride, int srcStride);
92 void ff_avg_mpeg4_qpel8_v_lowpass_mmxext(uint8_t *dst, uint8_t *src,
93                                         int dstStride, int srcStride);
94 void ff_put_no_rnd_mpeg4_qpel8_v_lowpass_mmxext(uint8_t *dst, uint8_t *src,
95                                                 int dstStride, int srcStride);
96 #define ff_put_no_rnd_pixels16_mmxext ff_put_pixels16_mmxext
97 #define ff_put_no_rnd_pixels8_mmxext ff_put_pixels8_mmxext
98
99
100 #if HAVE_INLINE_ASM
101
102 /***********************************/
103 /* standard MMX */
104
105 void ff_put_pixels_clamped_mmx(const int16_t *block, uint8_t *pixels,
106                                int line_size)
107 {
108     const int16_t *p;
109     uint8_t *pix;
110
111     /* read the pixels */
112     p   = block;
113     pix = pixels;
114     /* unrolled loop */
115     __asm__ volatile (
116         "movq      (%3), %%mm0          \n\t"
117         "movq     8(%3), %%mm1          \n\t"
118         "movq    16(%3), %%mm2          \n\t"
119         "movq    24(%3), %%mm3          \n\t"
120         "movq    32(%3), %%mm4          \n\t"
121         "movq    40(%3), %%mm5          \n\t"
122         "movq    48(%3), %%mm6          \n\t"
123         "movq    56(%3), %%mm7          \n\t"
124         "packuswb %%mm1, %%mm0          \n\t"
125         "packuswb %%mm3, %%mm2          \n\t"
126         "packuswb %%mm5, %%mm4          \n\t"
127         "packuswb %%mm7, %%mm6          \n\t"
128         "movq     %%mm0, (%0)           \n\t"
129         "movq     %%mm2, (%0, %1)       \n\t"
130         "movq     %%mm4, (%0, %1, 2)    \n\t"
131         "movq     %%mm6, (%0, %2)       \n\t"
132         :: "r"(pix), "r"((x86_reg)line_size), "r"((x86_reg)line_size * 3),
133            "r"(p)
134         : "memory");
135     pix += line_size * 4;
136     p   += 32;
137
138     // if here would be an exact copy of the code above
139     // compiler would generate some very strange code
140     // thus using "r"
141     __asm__ volatile (
142         "movq       (%3), %%mm0         \n\t"
143         "movq      8(%3), %%mm1         \n\t"
144         "movq     16(%3), %%mm2         \n\t"
145         "movq     24(%3), %%mm3         \n\t"
146         "movq     32(%3), %%mm4         \n\t"
147         "movq     40(%3), %%mm5         \n\t"
148         "movq     48(%3), %%mm6         \n\t"
149         "movq     56(%3), %%mm7         \n\t"
150         "packuswb  %%mm1, %%mm0         \n\t"
151         "packuswb  %%mm3, %%mm2         \n\t"
152         "packuswb  %%mm5, %%mm4         \n\t"
153         "packuswb  %%mm7, %%mm6         \n\t"
154         "movq      %%mm0, (%0)          \n\t"
155         "movq      %%mm2, (%0, %1)      \n\t"
156         "movq      %%mm4, (%0, %1, 2)   \n\t"
157         "movq      %%mm6, (%0, %2)      \n\t"
158         :: "r"(pix), "r"((x86_reg)line_size), "r"((x86_reg)line_size * 3), "r"(p)
159         : "memory");
160 }
161
162 #define put_signed_pixels_clamped_mmx_half(off)             \
163     "movq          "#off"(%2), %%mm1        \n\t"           \
164     "movq     16 + "#off"(%2), %%mm2        \n\t"           \
165     "movq     32 + "#off"(%2), %%mm3        \n\t"           \
166     "movq     48 + "#off"(%2), %%mm4        \n\t"           \
167     "packsswb  8 + "#off"(%2), %%mm1        \n\t"           \
168     "packsswb 24 + "#off"(%2), %%mm2        \n\t"           \
169     "packsswb 40 + "#off"(%2), %%mm3        \n\t"           \
170     "packsswb 56 + "#off"(%2), %%mm4        \n\t"           \
171     "paddb              %%mm0, %%mm1        \n\t"           \
172     "paddb              %%mm0, %%mm2        \n\t"           \
173     "paddb              %%mm0, %%mm3        \n\t"           \
174     "paddb              %%mm0, %%mm4        \n\t"           \
175     "movq               %%mm1, (%0)         \n\t"           \
176     "movq               %%mm2, (%0, %3)     \n\t"           \
177     "movq               %%mm3, (%0, %3, 2)  \n\t"           \
178     "movq               %%mm4, (%0, %1)     \n\t"
179
180 void ff_put_signed_pixels_clamped_mmx(const int16_t *block, uint8_t *pixels,
181                                       int line_size)
182 {
183     x86_reg line_skip = line_size;
184     x86_reg line_skip3;
185
186     __asm__ volatile (
187         "movq "MANGLE(ff_pb_80)", %%mm0     \n\t"
188         "lea         (%3, %3, 2), %1        \n\t"
189         put_signed_pixels_clamped_mmx_half(0)
190         "lea         (%0, %3, 4), %0        \n\t"
191         put_signed_pixels_clamped_mmx_half(64)
192         : "+&r"(pixels), "=&r"(line_skip3)
193         : "r"(block), "r"(line_skip)
194         : "memory");
195 }
196
197 void ff_add_pixels_clamped_mmx(const int16_t *block, uint8_t *pixels,
198                                int line_size)
199 {
200     const int16_t *p;
201     uint8_t *pix;
202     int i;
203
204     /* read the pixels */
205     p   = block;
206     pix = pixels;
207     MOVQ_ZERO(mm7);
208     i = 4;
209     do {
210         __asm__ volatile (
211             "movq        (%2), %%mm0    \n\t"
212             "movq       8(%2), %%mm1    \n\t"
213             "movq      16(%2), %%mm2    \n\t"
214             "movq      24(%2), %%mm3    \n\t"
215             "movq          %0, %%mm4    \n\t"
216             "movq          %1, %%mm6    \n\t"
217             "movq       %%mm4, %%mm5    \n\t"
218             "punpcklbw  %%mm7, %%mm4    \n\t"
219             "punpckhbw  %%mm7, %%mm5    \n\t"
220             "paddsw     %%mm4, %%mm0    \n\t"
221             "paddsw     %%mm5, %%mm1    \n\t"
222             "movq       %%mm6, %%mm5    \n\t"
223             "punpcklbw  %%mm7, %%mm6    \n\t"
224             "punpckhbw  %%mm7, %%mm5    \n\t"
225             "paddsw     %%mm6, %%mm2    \n\t"
226             "paddsw     %%mm5, %%mm3    \n\t"
227             "packuswb   %%mm1, %%mm0    \n\t"
228             "packuswb   %%mm3, %%mm2    \n\t"
229             "movq       %%mm0, %0       \n\t"
230             "movq       %%mm2, %1       \n\t"
231             : "+m"(*pix), "+m"(*(pix + line_size))
232             : "r"(p)
233             : "memory");
234         pix += line_size * 2;
235         p   += 16;
236     } while (--i);
237 }
238
239 #define CLEAR_BLOCKS(name, n)                           \
240 static void name(int16_t *blocks)                       \
241 {                                                       \
242     __asm__ volatile (                                  \
243         "pxor %%mm7, %%mm7              \n\t"           \
244         "mov     %1,        %%"REG_a"   \n\t"           \
245         "1:                             \n\t"           \
246         "movq %%mm7,   (%0, %%"REG_a")  \n\t"           \
247         "movq %%mm7,  8(%0, %%"REG_a")  \n\t"           \
248         "movq %%mm7, 16(%0, %%"REG_a")  \n\t"           \
249         "movq %%mm7, 24(%0, %%"REG_a")  \n\t"           \
250         "add    $32, %%"REG_a"          \n\t"           \
251         "js      1b                     \n\t"           \
252         :: "r"(((uint8_t *)blocks) + 128 * n),          \
253            "i"(-128 * n)                                \
254         : "%"REG_a                                      \
255         );                                              \
256 }
257 CLEAR_BLOCKS(clear_blocks_mmx, 6)
258 CLEAR_BLOCKS(clear_block_mmx, 1)
259
260 static void clear_block_sse(int16_t *block)
261 {
262     __asm__ volatile (
263         "xorps  %%xmm0, %%xmm0          \n"
264         "movaps %%xmm0,    (%0)         \n"
265         "movaps %%xmm0,  16(%0)         \n"
266         "movaps %%xmm0,  32(%0)         \n"
267         "movaps %%xmm0,  48(%0)         \n"
268         "movaps %%xmm0,  64(%0)         \n"
269         "movaps %%xmm0,  80(%0)         \n"
270         "movaps %%xmm0,  96(%0)         \n"
271         "movaps %%xmm0, 112(%0)         \n"
272         :: "r"(block)
273         : "memory"
274     );
275 }
276
277 static void clear_blocks_sse(int16_t *blocks)
278 {
279     __asm__ volatile (
280         "xorps  %%xmm0, %%xmm0              \n"
281         "mov        %1,         %%"REG_a"   \n"
282         "1:                                 \n"
283         "movaps %%xmm0,    (%0, %%"REG_a")  \n"
284         "movaps %%xmm0,  16(%0, %%"REG_a")  \n"
285         "movaps %%xmm0,  32(%0, %%"REG_a")  \n"
286         "movaps %%xmm0,  48(%0, %%"REG_a")  \n"
287         "movaps %%xmm0,  64(%0, %%"REG_a")  \n"
288         "movaps %%xmm0,  80(%0, %%"REG_a")  \n"
289         "movaps %%xmm0,  96(%0, %%"REG_a")  \n"
290         "movaps %%xmm0, 112(%0, %%"REG_a")  \n"
291         "add      $128,         %%"REG_a"   \n"
292         "js         1b                      \n"
293         :: "r"(((uint8_t *)blocks) + 128 * 6),
294            "i"(-128 * 6)
295         : "%"REG_a
296     );
297 }
298
299 static void add_bytes_mmx(uint8_t *dst, uint8_t *src, int w)
300 {
301     x86_reg i = 0;
302     __asm__ volatile (
303         "jmp          2f                \n\t"
304         "1:                             \n\t"
305         "movq   (%1, %0), %%mm0         \n\t"
306         "movq   (%2, %0), %%mm1         \n\t"
307         "paddb     %%mm0, %%mm1         \n\t"
308         "movq      %%mm1, (%2, %0)      \n\t"
309         "movq  8(%1, %0), %%mm0         \n\t"
310         "movq  8(%2, %0), %%mm1         \n\t"
311         "paddb     %%mm0, %%mm1         \n\t"
312         "movq      %%mm1, 8(%2, %0)     \n\t"
313         "add         $16, %0            \n\t"
314         "2:                             \n\t"
315         "cmp          %3, %0            \n\t"
316         "js           1b                \n\t"
317         : "+r"(i)
318         : "r"(src), "r"(dst), "r"((x86_reg)w - 15)
319     );
320     for ( ; i < w; i++)
321         dst[i + 0] += src[i + 0];
322 }
323
324 #if HAVE_7REGS
325 static void add_hfyu_median_prediction_cmov(uint8_t *dst, const uint8_t *top,
326                                             const uint8_t *diff, int w,
327                                             int *left, int *left_top)
328 {
329     x86_reg w2 = -w;
330     x86_reg x;
331     int l  = *left     & 0xff;
332     int tl = *left_top & 0xff;
333     int t;
334     __asm__ volatile (
335         "mov          %7, %3            \n"
336         "1:                             \n"
337         "movzbl (%3, %4), %2            \n"
338         "mov          %2, %k3           \n"
339         "sub         %b1, %b3           \n"
340         "add         %b0, %b3           \n"
341         "mov          %2, %1            \n"
342         "cmp          %0, %2            \n"
343         "cmovg        %0, %2            \n"
344         "cmovg        %1, %0            \n"
345         "cmp         %k3, %0            \n"
346         "cmovg       %k3, %0            \n"
347         "mov          %7, %3            \n"
348         "cmp          %2, %0            \n"
349         "cmovl        %2, %0            \n"
350         "add    (%6, %4), %b0           \n"
351         "mov         %b0, (%5, %4)      \n"
352         "inc          %4                \n"
353         "jl           1b                \n"
354         : "+&q"(l), "+&q"(tl), "=&r"(t), "=&q"(x), "+&r"(w2)
355         : "r"(dst + w), "r"(diff + w), "rm"(top + w)
356     );
357     *left     = l;
358     *left_top = tl;
359 }
360 #endif
361
362 /* Draw the edges of width 'w' of an image of size width, height
363  * this MMX version can only handle w == 8 || w == 16. */
364 static void draw_edges_mmx(uint8_t *buf, int wrap, int width, int height,
365                            int w, int h, int sides)
366 {
367     uint8_t *ptr, *last_line;
368     int i;
369
370     last_line = buf + (height - 1) * wrap;
371     /* left and right */
372     ptr = buf;
373     if (w == 8) {
374         __asm__ volatile (
375             "1:                             \n\t"
376             "movd            (%0), %%mm0    \n\t"
377             "punpcklbw      %%mm0, %%mm0    \n\t"
378             "punpcklwd      %%mm0, %%mm0    \n\t"
379             "punpckldq      %%mm0, %%mm0    \n\t"
380             "movq           %%mm0, -8(%0)   \n\t"
381             "movq      -8(%0, %2), %%mm1    \n\t"
382             "punpckhbw      %%mm1, %%mm1    \n\t"
383             "punpckhwd      %%mm1, %%mm1    \n\t"
384             "punpckhdq      %%mm1, %%mm1    \n\t"
385             "movq           %%mm1, (%0, %2) \n\t"
386             "add               %1, %0       \n\t"
387             "cmp               %3, %0       \n\t"
388             "jb                1b           \n\t"
389             : "+r"(ptr)
390             : "r"((x86_reg)wrap), "r"((x86_reg)width), "r"(ptr + wrap * height)
391             );
392     } else {
393         __asm__ volatile (
394             "1:                                 \n\t"
395             "movd            (%0), %%mm0        \n\t"
396             "punpcklbw      %%mm0, %%mm0        \n\t"
397             "punpcklwd      %%mm0, %%mm0        \n\t"
398             "punpckldq      %%mm0, %%mm0        \n\t"
399             "movq           %%mm0, -8(%0)       \n\t"
400             "movq           %%mm0, -16(%0)      \n\t"
401             "movq      -8(%0, %2), %%mm1        \n\t"
402             "punpckhbw      %%mm1, %%mm1        \n\t"
403             "punpckhwd      %%mm1, %%mm1        \n\t"
404             "punpckhdq      %%mm1, %%mm1        \n\t"
405             "movq           %%mm1,  (%0, %2)    \n\t"
406             "movq           %%mm1, 8(%0, %2)    \n\t"
407             "add               %1, %0           \n\t"
408             "cmp               %3, %0           \n\t"
409             "jb                1b               \n\t"
410             : "+r"(ptr)
411             : "r"((x86_reg)wrap), "r"((x86_reg)width), "r"(ptr + wrap * height)
412             );
413     }
414
415     /* top and bottom (and hopefully also the corners) */
416     if (sides & EDGE_TOP) {
417         for (i = 0; i < h; i += 4) {
418             ptr = buf - (i + 1) * wrap - w;
419             __asm__ volatile (
420                 "1:                             \n\t"
421                 "movq (%1, %0), %%mm0           \n\t"
422                 "movq    %%mm0, (%0)            \n\t"
423                 "movq    %%mm0, (%0, %2)        \n\t"
424                 "movq    %%mm0, (%0, %2, 2)     \n\t"
425                 "movq    %%mm0, (%0, %3)        \n\t"
426                 "add        $8, %0              \n\t"
427                 "cmp        %4, %0              \n\t"
428                 "jb         1b                  \n\t"
429                 : "+r"(ptr)
430                 : "r"((x86_reg)buf - (x86_reg)ptr - w), "r"((x86_reg) -wrap),
431                   "r"((x86_reg) -wrap * 3), "r"(ptr + width + 2 * w)
432                 );
433         }
434     }
435
436     if (sides & EDGE_BOTTOM) {
437         for (i = 0; i < h; i += 4) {
438             ptr = last_line + (i + 1) * wrap - w;
439             __asm__ volatile (
440                 "1:                             \n\t"
441                 "movq (%1, %0), %%mm0           \n\t"
442                 "movq    %%mm0, (%0)            \n\t"
443                 "movq    %%mm0, (%0, %2)        \n\t"
444                 "movq    %%mm0, (%0, %2, 2)     \n\t"
445                 "movq    %%mm0, (%0, %3)        \n\t"
446                 "add        $8, %0              \n\t"
447                 "cmp        %4, %0              \n\t"
448                 "jb         1b                  \n\t"
449                 : "+r"(ptr)
450                 : "r"((x86_reg)last_line - (x86_reg)ptr - w),
451                   "r"((x86_reg)wrap), "r"((x86_reg)wrap * 3),
452                   "r"(ptr + width + 2 * w)
453                 );
454         }
455     }
456 }
457 #endif /* HAVE_INLINE_ASM */
458
459
460 #if HAVE_YASM
461 static void ff_avg_pixels16_mmxext(uint8_t *block, const uint8_t *pixels,
462                                    int line_size, int h)
463 {
464     ff_avg_pixels8_mmxext(block,     pixels,     line_size, h);
465     ff_avg_pixels8_mmxext(block + 8, pixels + 8, line_size, h);
466 }
467
468 static void ff_put_pixels16_mmxext(uint8_t *block, const uint8_t *pixels,
469                                    ptrdiff_t line_size, int h)
470 {
471     ff_put_pixels8_mmxext(block,     pixels,     line_size, h);
472     ff_put_pixels8_mmxext(block + 8, pixels + 8, line_size, h);
473 }
474
475 #define QPEL_OP(OPNAME, ROUNDER, RND, MMX)                              \
476 static void OPNAME ## qpel8_mc00_ ## MMX (uint8_t *dst, uint8_t *src,   \
477                                           ptrdiff_t stride)             \
478 {                                                                       \
479     ff_ ## OPNAME ## pixels8_ ## MMX(dst, src, stride, 8);              \
480 }                                                                       \
481                                                                         \
482 static void OPNAME ## qpel8_mc10_ ## MMX(uint8_t *dst, uint8_t *src,    \
483                                          ptrdiff_t stride)              \
484 {                                                                       \
485     uint64_t temp[8];                                                   \
486     uint8_t * const half = (uint8_t*)temp;                              \
487     ff_put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(half, src, 8,        \
488                                                    stride, 8);          \
489     ff_ ## OPNAME ## pixels8_l2_ ## MMX(dst, src, half,                 \
490                                         stride, stride, 8);             \
491 }                                                                       \
492                                                                         \
493 static void OPNAME ## qpel8_mc20_ ## MMX(uint8_t *dst, uint8_t *src,    \
494                                          ptrdiff_t stride)              \
495 {                                                                       \
496     ff_ ## OPNAME ## mpeg4_qpel8_h_lowpass_ ## MMX(dst, src, stride,    \
497                                                    stride, 8);          \
498 }                                                                       \
499                                                                         \
500 static void OPNAME ## qpel8_mc30_ ## MMX(uint8_t *dst, uint8_t *src,    \
501                                          ptrdiff_t stride)              \
502 {                                                                       \
503     uint64_t temp[8];                                                   \
504     uint8_t * const half = (uint8_t*)temp;                              \
505     ff_put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(half, src, 8,        \
506                                                    stride, 8);          \
507     ff_ ## OPNAME ## pixels8_l2_ ## MMX(dst, src + 1, half, stride,     \
508                                         stride, 8);                     \
509 }                                                                       \
510                                                                         \
511 static void OPNAME ## qpel8_mc01_ ## MMX(uint8_t *dst, uint8_t *src,    \
512                                          ptrdiff_t stride)              \
513 {                                                                       \
514     uint64_t temp[8];                                                   \
515     uint8_t * const half = (uint8_t*)temp;                              \
516     ff_put ## RND ## mpeg4_qpel8_v_lowpass_ ## MMX(half, src,           \
517                                                    8, stride);          \
518     ff_ ## OPNAME ## pixels8_l2_ ## MMX(dst, src, half,                 \
519                                         stride, stride, 8);             \
520 }                                                                       \
521                                                                         \
522 static void OPNAME ## qpel8_mc02_ ## MMX(uint8_t *dst, uint8_t *src,    \
523                                          ptrdiff_t stride)              \
524 {                                                                       \
525     ff_ ## OPNAME ## mpeg4_qpel8_v_lowpass_ ## MMX(dst, src,            \
526                                                    stride, stride);     \
527 }                                                                       \
528                                                                         \
529 static void OPNAME ## qpel8_mc03_ ## MMX(uint8_t *dst, uint8_t *src,    \
530                                          ptrdiff_t stride)              \
531 {                                                                       \
532     uint64_t temp[8];                                                   \
533     uint8_t * const half = (uint8_t*)temp;                              \
534     ff_put ## RND ## mpeg4_qpel8_v_lowpass_ ## MMX(half, src,           \
535                                                    8, stride);          \
536     ff_ ## OPNAME ## pixels8_l2_ ## MMX(dst, src + stride, half, stride,\
537                                         stride, 8);                     \
538 }                                                                       \
539                                                                         \
540 static void OPNAME ## qpel8_mc11_ ## MMX(uint8_t *dst, uint8_t *src,    \
541                                          ptrdiff_t stride)              \
542 {                                                                       \
543     uint64_t half[8 + 9];                                               \
544     uint8_t * const halfH  = ((uint8_t*)half) + 64;                     \
545     uint8_t * const halfHV = ((uint8_t*)half);                          \
546     ff_put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8,       \
547                                                    stride, 9);          \
548     ff_put ## RND ## pixels8_l2_ ## MMX(halfH, src, halfH, 8,           \
549                                         stride, 9);                     \
550     ff_put ## RND ## mpeg4_qpel8_v_lowpass_ ## MMX(halfHV, halfH, 8, 8);\
551     ff_ ## OPNAME ## pixels8_l2_ ## MMX(dst, halfH, halfHV,             \
552                                         stride, 8, 8);                  \
553 }                                                                       \
554                                                                         \
555 static void OPNAME ## qpel8_mc31_ ## MMX(uint8_t *dst, uint8_t *src,    \
556                                          ptrdiff_t stride)              \
557 {                                                                       \
558     uint64_t half[8 + 9];                                               \
559     uint8_t * const halfH  = ((uint8_t*)half) + 64;                     \
560     uint8_t * const halfHV = ((uint8_t*)half);                          \
561     ff_put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8,       \
562                                                    stride, 9);          \
563     ff_put ## RND ## pixels8_l2_ ## MMX(halfH, src + 1, halfH, 8,       \
564                                         stride, 9);                     \
565     ff_put ## RND ## mpeg4_qpel8_v_lowpass_ ## MMX(halfHV, halfH, 8, 8);\
566     ff_ ## OPNAME ## pixels8_l2_ ## MMX(dst, halfH, halfHV,             \
567                                         stride, 8, 8);                  \
568 }                                                                       \
569                                                                         \
570 static void OPNAME ## qpel8_mc13_ ## MMX(uint8_t *dst, uint8_t *src,    \
571                                          ptrdiff_t stride)              \
572 {                                                                       \
573     uint64_t half[8 + 9];                                               \
574     uint8_t * const halfH  = ((uint8_t*)half) + 64;                     \
575     uint8_t * const halfHV = ((uint8_t*)half);                          \
576     ff_put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8,       \
577                                                    stride, 9);          \
578     ff_put ## RND ## pixels8_l2_ ## MMX(halfH, src, halfH, 8,           \
579                                         stride, 9);                     \
580     ff_put ## RND ## mpeg4_qpel8_v_lowpass_ ## MMX(halfHV, halfH, 8, 8);\
581     ff_ ## OPNAME ## pixels8_l2_ ## MMX(dst, halfH + 8, halfHV,         \
582                                         stride, 8, 8);                  \
583 }                                                                       \
584                                                                         \
585 static void OPNAME ## qpel8_mc33_ ## MMX(uint8_t *dst, uint8_t *src,    \
586                                          ptrdiff_t stride)              \
587 {                                                                       \
588     uint64_t half[8 + 9];                                               \
589     uint8_t * const halfH  = ((uint8_t*)half) + 64;                     \
590     uint8_t * const halfHV = ((uint8_t*)half);                          \
591     ff_put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8,       \
592                                                    stride, 9);          \
593     ff_put ## RND ## pixels8_l2_ ## MMX(halfH, src + 1, halfH, 8,       \
594                                         stride, 9);                     \
595     ff_put ## RND ## mpeg4_qpel8_v_lowpass_ ## MMX(halfHV, halfH, 8, 8);\
596     ff_ ## OPNAME ## pixels8_l2_ ## MMX(dst, halfH + 8, halfHV,         \
597                                         stride, 8, 8);                  \
598 }                                                                       \
599                                                                         \
600 static void OPNAME ## qpel8_mc21_ ## MMX(uint8_t *dst, uint8_t *src,    \
601                                          ptrdiff_t stride)              \
602 {                                                                       \
603     uint64_t half[8 + 9];                                               \
604     uint8_t * const halfH  = ((uint8_t*)half) + 64;                     \
605     uint8_t * const halfHV = ((uint8_t*)half);                          \
606     ff_put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8,       \
607                                                    stride, 9);          \
608     ff_put ## RND ## mpeg4_qpel8_v_lowpass_ ## MMX(halfHV, halfH, 8, 8);\
609     ff_ ## OPNAME ## pixels8_l2_ ## MMX(dst, halfH, halfHV,             \
610                                         stride, 8, 8);                  \
611 }                                                                       \
612                                                                         \
613 static void OPNAME ## qpel8_mc23_ ## MMX(uint8_t *dst, uint8_t *src,    \
614                                          ptrdiff_t stride)              \
615 {                                                                       \
616     uint64_t half[8 + 9];                                               \
617     uint8_t * const halfH  = ((uint8_t*)half) + 64;                     \
618     uint8_t * const halfHV = ((uint8_t*)half);                          \
619     ff_put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8,       \
620                                                    stride, 9);          \
621     ff_put ## RND ## mpeg4_qpel8_v_lowpass_ ## MMX(halfHV, halfH, 8, 8);\
622     ff_ ## OPNAME ## pixels8_l2_ ## MMX(dst, halfH + 8, halfHV,         \
623                                         stride, 8, 8);                  \
624 }                                                                       \
625                                                                         \
626 static void OPNAME ## qpel8_mc12_ ## MMX(uint8_t *dst, uint8_t *src,    \
627                                          ptrdiff_t stride)              \
628 {                                                                       \
629     uint64_t half[8 + 9];                                               \
630     uint8_t * const halfH = ((uint8_t*)half);                           \
631     ff_put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8,       \
632                                                    stride, 9);          \
633     ff_put ## RND ## pixels8_l2_ ## MMX(halfH, src, halfH,              \
634                                         8, stride, 9);                  \
635     ff_ ## OPNAME ## mpeg4_qpel8_v_lowpass_ ## MMX(dst, halfH,          \
636                                                    stride, 8);          \
637 }                                                                       \
638                                                                         \
639 static void OPNAME ## qpel8_mc32_ ## MMX(uint8_t *dst, uint8_t *src,    \
640                                          ptrdiff_t stride)              \
641 {                                                                       \
642     uint64_t half[8 + 9];                                               \
643     uint8_t * const halfH = ((uint8_t*)half);                           \
644     ff_put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8,       \
645                                                    stride, 9);          \
646     ff_put ## RND ## pixels8_l2_ ## MMX(halfH, src + 1, halfH, 8,       \
647                                         stride, 9);                     \
648     ff_ ## OPNAME ## mpeg4_qpel8_v_lowpass_ ## MMX(dst, halfH,          \
649                                                    stride, 8);          \
650 }                                                                       \
651                                                                         \
652 static void OPNAME ## qpel8_mc22_ ## MMX(uint8_t *dst, uint8_t *src,    \
653                                          ptrdiff_t stride)              \
654 {                                                                       \
655     uint64_t half[9];                                                   \
656     uint8_t * const halfH = ((uint8_t*)half);                           \
657     ff_put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8,       \
658                                                    stride, 9);          \
659     ff_ ## OPNAME ## mpeg4_qpel8_v_lowpass_ ## MMX(dst, halfH,          \
660                                                    stride, 8);          \
661 }                                                                       \
662                                                                         \
663 static void OPNAME ## qpel16_mc00_ ## MMX (uint8_t *dst, uint8_t *src,  \
664                                            ptrdiff_t stride)            \
665 {                                                                       \
666     ff_ ## OPNAME ## pixels16_ ## MMX(dst, src, stride, 16);            \
667 }                                                                       \
668                                                                         \
669 static void OPNAME ## qpel16_mc10_ ## MMX(uint8_t *dst, uint8_t *src,   \
670                                           ptrdiff_t stride)             \
671 {                                                                       \
672     uint64_t temp[32];                                                  \
673     uint8_t * const half = (uint8_t*)temp;                              \
674     ff_put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(half, src, 16,      \
675                                                     stride, 16);        \
676     ff_ ## OPNAME ## pixels16_l2_ ## MMX(dst, src, half, stride,        \
677                                          stride, 16);                   \
678 }                                                                       \
679                                                                         \
680 static void OPNAME ## qpel16_mc20_ ## MMX(uint8_t *dst, uint8_t *src,   \
681                                           ptrdiff_t stride)             \
682 {                                                                       \
683     ff_ ## OPNAME ## mpeg4_qpel16_h_lowpass_ ## MMX(dst, src,           \
684                                                     stride, stride, 16);\
685 }                                                                       \
686                                                                         \
687 static void OPNAME ## qpel16_mc30_ ## MMX(uint8_t *dst, uint8_t *src,   \
688                                           ptrdiff_t stride)             \
689 {                                                                       \
690     uint64_t temp[32];                                                  \
691     uint8_t * const half = (uint8_t*)temp;                              \
692     ff_put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(half, src, 16,      \
693                                                     stride, 16);        \
694     ff_ ## OPNAME ## pixels16_l2_ ## MMX(dst, src + 1, half,            \
695                                          stride, stride, 16);           \
696 }                                                                       \
697                                                                         \
698 static void OPNAME ## qpel16_mc01_ ## MMX(uint8_t *dst, uint8_t *src,   \
699                                           ptrdiff_t stride)             \
700 {                                                                       \
701     uint64_t temp[32];                                                  \
702     uint8_t * const half = (uint8_t*)temp;                              \
703     ff_put ## RND ## mpeg4_qpel16_v_lowpass_ ## MMX(half, src, 16,      \
704                                                     stride);            \
705     ff_ ## OPNAME ## pixels16_l2_ ## MMX(dst, src, half, stride,        \
706                                          stride, 16);                   \
707 }                                                                       \
708                                                                         \
709 static void OPNAME ## qpel16_mc02_ ## MMX(uint8_t *dst, uint8_t *src,   \
710                                           ptrdiff_t stride)             \
711 {                                                                       \
712     ff_ ## OPNAME ## mpeg4_qpel16_v_lowpass_ ## MMX(dst, src,           \
713                                                     stride, stride);    \
714 }                                                                       \
715                                                                         \
716 static void OPNAME ## qpel16_mc03_ ## MMX(uint8_t *dst, uint8_t *src,   \
717                                           ptrdiff_t stride)             \
718 {                                                                       \
719     uint64_t temp[32];                                                  \
720     uint8_t * const half = (uint8_t*)temp;                              \
721     ff_put ## RND ## mpeg4_qpel16_v_lowpass_ ## MMX(half, src, 16,      \
722                                                     stride);            \
723     ff_ ## OPNAME ## pixels16_l2_ ## MMX(dst, src+stride, half,         \
724                                          stride, stride, 16);           \
725 }                                                                       \
726                                                                         \
727 static void OPNAME ## qpel16_mc11_ ## MMX(uint8_t *dst, uint8_t *src,   \
728                                           ptrdiff_t stride)             \
729 {                                                                       \
730     uint64_t half[16 * 2 + 17 * 2];                                     \
731     uint8_t * const halfH  = ((uint8_t*)half) + 256;                    \
732     uint8_t * const halfHV = ((uint8_t*)half);                          \
733     ff_put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16,     \
734                                                     stride, 17);        \
735     ff_put ## RND ## pixels16_l2_ ## MMX(halfH, src, halfH, 16,         \
736                                          stride, 17);                   \
737     ff_put ## RND ## mpeg4_qpel16_v_lowpass_ ## MMX(halfHV, halfH,      \
738                                                     16, 16);            \
739     ff_ ## OPNAME ## pixels16_l2_ ## MMX(dst, halfH, halfHV,            \
740                                          stride, 16, 16);               \
741 }                                                                       \
742                                                                         \
743 static void OPNAME ## qpel16_mc31_ ## MMX(uint8_t *dst, uint8_t *src,   \
744                                           ptrdiff_t stride)             \
745 {                                                                       \
746     uint64_t half[16 * 2 + 17 * 2];                                     \
747     uint8_t * const halfH  = ((uint8_t*)half) + 256;                    \
748     uint8_t * const halfHV = ((uint8_t*)half);                          \
749     ff_put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16,     \
750                                                     stride, 17);        \
751     ff_put ## RND ## pixels16_l2_ ## MMX(halfH, src + 1, halfH, 16,     \
752                                          stride, 17);                   \
753     ff_put ## RND ## mpeg4_qpel16_v_lowpass_ ## MMX(halfHV, halfH,      \
754                                                     16, 16);            \
755     ff_ ## OPNAME ## pixels16_l2_ ## MMX(dst, halfH, halfHV,            \
756                                          stride, 16, 16);               \
757 }                                                                       \
758                                                                         \
759 static void OPNAME ## qpel16_mc13_ ## MMX(uint8_t *dst, uint8_t *src,   \
760                                           ptrdiff_t stride)             \
761 {                                                                       \
762     uint64_t half[16 * 2 + 17 * 2];                                     \
763     uint8_t * const halfH  = ((uint8_t*)half) + 256;                    \
764     uint8_t * const halfHV = ((uint8_t*)half);                          \
765     ff_put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16,     \
766                                                     stride, 17);        \
767     ff_put ## RND ## pixels16_l2_ ## MMX(halfH, src, halfH, 16,         \
768                                          stride, 17);                   \
769     ff_put ## RND ## mpeg4_qpel16_v_lowpass_ ## MMX(halfHV, halfH,      \
770                                                     16, 16);            \
771     ff_ ## OPNAME ## pixels16_l2_ ## MMX(dst, halfH + 16, halfHV,       \
772                                          stride, 16, 16);               \
773 }                                                                       \
774                                                                         \
775 static void OPNAME ## qpel16_mc33_ ## MMX(uint8_t *dst, uint8_t *src,   \
776                                           ptrdiff_t stride)             \
777 {                                                                       \
778     uint64_t half[16 * 2 + 17 * 2];                                     \
779     uint8_t * const halfH  = ((uint8_t*)half) + 256;                    \
780     uint8_t * const halfHV = ((uint8_t*)half);                          \
781     ff_put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16,     \
782                                                     stride, 17);        \
783     ff_put ## RND ## pixels16_l2_ ## MMX(halfH, src + 1, halfH, 16,     \
784                                          stride, 17);                   \
785     ff_put ## RND ## mpeg4_qpel16_v_lowpass_ ## MMX(halfHV, halfH,      \
786                                                     16, 16);            \
787     ff_ ## OPNAME ## pixels16_l2_ ## MMX(dst, halfH + 16, halfHV,       \
788                                          stride, 16, 16);               \
789 }                                                                       \
790                                                                         \
791 static void OPNAME ## qpel16_mc21_ ## MMX(uint8_t *dst, uint8_t *src,   \
792                                           ptrdiff_t stride)             \
793 {                                                                       \
794     uint64_t half[16 * 2 + 17 * 2];                                     \
795     uint8_t * const halfH  = ((uint8_t*)half) + 256;                    \
796     uint8_t * const halfHV = ((uint8_t*)half);                          \
797     ff_put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16,     \
798                                                     stride, 17);        \
799     ff_put ## RND ## mpeg4_qpel16_v_lowpass_ ## MMX(halfHV, halfH,      \
800                                                     16, 16);            \
801     ff_ ## OPNAME ## pixels16_l2_ ## MMX(dst, halfH, halfHV,            \
802                                          stride, 16, 16);               \
803 }                                                                       \
804                                                                         \
805 static void OPNAME ## qpel16_mc23_ ## MMX(uint8_t *dst, uint8_t *src,   \
806                                           ptrdiff_t stride)             \
807 {                                                                       \
808     uint64_t half[16 * 2 + 17 * 2];                                     \
809     uint8_t * const halfH  = ((uint8_t*)half) + 256;                    \
810     uint8_t * const halfHV = ((uint8_t*)half);                          \
811     ff_put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16,     \
812                                                     stride, 17);        \
813     ff_put ## RND ## mpeg4_qpel16_v_lowpass_ ## MMX(halfHV, halfH,      \
814                                                     16, 16);            \
815     ff_ ## OPNAME ## pixels16_l2_ ## MMX(dst, halfH + 16, halfHV,       \
816                                          stride, 16, 16);               \
817 }                                                                       \
818                                                                         \
819 static void OPNAME ## qpel16_mc12_ ## MMX(uint8_t *dst, uint8_t *src,   \
820                                           ptrdiff_t stride)             \
821 {                                                                       \
822     uint64_t half[17 * 2];                                              \
823     uint8_t * const halfH = ((uint8_t*)half);                           \
824     ff_put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16,     \
825                                                     stride, 17);        \
826     ff_put ## RND ## pixels16_l2_ ## MMX(halfH, src, halfH, 16,         \
827                                          stride, 17);                   \
828     ff_ ## OPNAME ## mpeg4_qpel16_v_lowpass_ ## MMX(dst, halfH,         \
829                                                     stride, 16);        \
830 }                                                                       \
831                                                                         \
832 static void OPNAME ## qpel16_mc32_ ## MMX(uint8_t *dst, uint8_t *src,   \
833                                           ptrdiff_t stride)             \
834 {                                                                       \
835     uint64_t half[17 * 2];                                              \
836     uint8_t * const halfH = ((uint8_t*)half);                           \
837     ff_put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16,     \
838                                                     stride, 17);        \
839     ff_put ## RND ## pixels16_l2_ ## MMX(halfH, src + 1, halfH, 16,     \
840                                          stride, 17);                   \
841     ff_ ## OPNAME ## mpeg4_qpel16_v_lowpass_ ## MMX(dst, halfH,         \
842                                                     stride, 16);        \
843 }                                                                       \
844                                                                         \
845 static void OPNAME ## qpel16_mc22_ ## MMX(uint8_t *dst, uint8_t *src,   \
846                                           ptrdiff_t stride)             \
847 {                                                                       \
848     uint64_t half[17 * 2];                                              \
849     uint8_t * const halfH = ((uint8_t*)half);                           \
850     ff_put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16,     \
851                                                     stride, 17);        \
852     ff_ ## OPNAME ## mpeg4_qpel16_v_lowpass_ ## MMX(dst, halfH,         \
853                                                     stride, 16);        \
854 }
855
856 QPEL_OP(put_,          ff_pw_16, _,        mmxext)
857 QPEL_OP(avg_,          ff_pw_16, _,        mmxext)
858 QPEL_OP(put_no_rnd_,   ff_pw_15, _no_rnd_, mmxext)
859 #endif /* HAVE_YASM */
860
861
862 #if HAVE_INLINE_ASM
863 void ff_put_rv40_qpel8_mc33_mmx(uint8_t *dst, uint8_t *src, ptrdiff_t stride)
864 {
865     ff_put_pixels8_xy2_mmx(dst, src, stride, 8);
866 }
867 void ff_put_rv40_qpel16_mc33_mmx(uint8_t *dst, uint8_t *src, ptrdiff_t stride)
868 {
869     ff_put_pixels16_xy2_mmx(dst, src, stride, 16);
870 }
871 void ff_avg_rv40_qpel8_mc33_mmx(uint8_t *dst, uint8_t *src, ptrdiff_t stride)
872 {
873     ff_avg_pixels8_xy2_mmx(dst, src, stride, 8);
874 }
875 void ff_avg_rv40_qpel16_mc33_mmx(uint8_t *dst, uint8_t *src, ptrdiff_t stride)
876 {
877     ff_avg_pixels16_xy2_mmx(dst, src, stride, 16);
878 }
879
880 static void gmc_mmx(uint8_t *dst, uint8_t *src,
881                     int stride, int h, int ox, int oy,
882                     int dxx, int dxy, int dyx, int dyy,
883                     int shift, int r, int width, int height)
884 {
885     const int w    = 8;
886     const int ix   = ox  >> (16 + shift);
887     const int iy   = oy  >> (16 + shift);
888     const int oxs  = ox  >> 4;
889     const int oys  = oy  >> 4;
890     const int dxxs = dxx >> 4;
891     const int dxys = dxy >> 4;
892     const int dyxs = dyx >> 4;
893     const int dyys = dyy >> 4;
894     const uint16_t r4[4]   = { r, r, r, r };
895     const uint16_t dxy4[4] = { dxys, dxys, dxys, dxys };
896     const uint16_t dyy4[4] = { dyys, dyys, dyys, dyys };
897     const uint64_t shift2 = 2 * shift;
898     int x, y;
899
900     const int dxw = (dxx - (1 << (16 + shift))) * (w - 1);
901     const int dyh = (dyy - (1 << (16 + shift))) * (h - 1);
902     const int dxh = dxy * (h - 1);
903     const int dyw = dyx * (w - 1);
904     if ( // non-constant fullpel offset (3% of blocks)
905         ((ox ^ (ox + dxw)) | (ox ^ (ox + dxh)) | (ox ^ (ox + dxw + dxh)) |
906          (oy ^ (oy + dyw)) | (oy ^ (oy + dyh)) | (oy ^ (oy + dyw + dyh))) >> (16 + shift)
907         // uses more than 16 bits of subpel mv (only at huge resolution)
908         || (dxx | dxy | dyx | dyy) & 15 ||
909         (unsigned)ix >= width  - w ||
910         (unsigned)iy >= height - h) {
911         // FIXME could still use mmx for some of the rows
912         ff_gmc_c(dst, src, stride, h, ox, oy, dxx, dxy, dyx, dyy,
913                  shift, r, width, height);
914         return;
915     }
916
917     src += ix + iy * stride;
918
919     __asm__ volatile (
920         "movd         %0, %%mm6         \n\t"
921         "pxor      %%mm7, %%mm7         \n\t"
922         "punpcklwd %%mm6, %%mm6         \n\t"
923         "punpcklwd %%mm6, %%mm6         \n\t"
924         :: "r"(1<<shift)
925     );
926
927     for (x = 0; x < w; x += 4) {
928         uint16_t dx4[4] = { oxs - dxys + dxxs * (x + 0),
929                             oxs - dxys + dxxs * (x + 1),
930                             oxs - dxys + dxxs * (x + 2),
931                             oxs - dxys + dxxs * (x + 3) };
932         uint16_t dy4[4] = { oys - dyys + dyxs * (x + 0),
933                             oys - dyys + dyxs * (x + 1),
934                             oys - dyys + dyxs * (x + 2),
935                             oys - dyys + dyxs * (x + 3) };
936
937         for (y = 0; y < h; y++) {
938             __asm__ volatile (
939                 "movq      %0, %%mm4    \n\t"
940                 "movq      %1, %%mm5    \n\t"
941                 "paddw     %2, %%mm4    \n\t"
942                 "paddw     %3, %%mm5    \n\t"
943                 "movq   %%mm4, %0       \n\t"
944                 "movq   %%mm5, %1       \n\t"
945                 "psrlw    $12, %%mm4    \n\t"
946                 "psrlw    $12, %%mm5    \n\t"
947                 : "+m"(*dx4), "+m"(*dy4)
948                 : "m"(*dxy4), "m"(*dyy4)
949             );
950
951             __asm__ volatile (
952                 "movq      %%mm6, %%mm2 \n\t"
953                 "movq      %%mm6, %%mm1 \n\t"
954                 "psubw     %%mm4, %%mm2 \n\t"
955                 "psubw     %%mm5, %%mm1 \n\t"
956                 "movq      %%mm2, %%mm0 \n\t"
957                 "movq      %%mm4, %%mm3 \n\t"
958                 "pmullw    %%mm1, %%mm0 \n\t" // (s - dx) * (s - dy)
959                 "pmullw    %%mm5, %%mm3 \n\t" // dx * dy
960                 "pmullw    %%mm5, %%mm2 \n\t" // (s - dx) * dy
961                 "pmullw    %%mm4, %%mm1 \n\t" // dx * (s - dy)
962
963                 "movd         %4, %%mm5 \n\t"
964                 "movd         %3, %%mm4 \n\t"
965                 "punpcklbw %%mm7, %%mm5 \n\t"
966                 "punpcklbw %%mm7, %%mm4 \n\t"
967                 "pmullw    %%mm5, %%mm3 \n\t" // src[1, 1] * dx * dy
968                 "pmullw    %%mm4, %%mm2 \n\t" // src[0, 1] * (s - dx) * dy
969
970                 "movd         %2, %%mm5 \n\t"
971                 "movd         %1, %%mm4 \n\t"
972                 "punpcklbw %%mm7, %%mm5 \n\t"
973                 "punpcklbw %%mm7, %%mm4 \n\t"
974                 "pmullw    %%mm5, %%mm1 \n\t" // src[1, 0] * dx * (s - dy)
975                 "pmullw    %%mm4, %%mm0 \n\t" // src[0, 0] * (s - dx) * (s - dy)
976                 "paddw        %5, %%mm1 \n\t"
977                 "paddw     %%mm3, %%mm2 \n\t"
978                 "paddw     %%mm1, %%mm0 \n\t"
979                 "paddw     %%mm2, %%mm0 \n\t"
980
981                 "psrlw        %6, %%mm0 \n\t"
982                 "packuswb  %%mm0, %%mm0 \n\t"
983                 "movd      %%mm0, %0    \n\t"
984
985                 : "=m"(dst[x + y * stride])
986                 : "m"(src[0]), "m"(src[1]),
987                   "m"(src[stride]), "m"(src[stride + 1]),
988                   "m"(*r4), "m"(shift2)
989             );
990             src += stride;
991         }
992         src += 4 - h * stride;
993     }
994 }
995
996 static void vector_clipf_sse(float *dst, const float *src,
997                              float min, float max, int len)
998 {
999     x86_reg i = (len - 16) * 4;
1000     __asm__ volatile (
1001         "movss          %3, %%xmm4      \n\t"
1002         "movss          %4, %%xmm5      \n\t"
1003         "shufps $0, %%xmm4, %%xmm4      \n\t"
1004         "shufps $0, %%xmm5, %%xmm5      \n\t"
1005         "1:                             \n\t"
1006         "movaps   (%2, %0), %%xmm0      \n\t" // 3/1 on intel
1007         "movaps 16(%2, %0), %%xmm1      \n\t"
1008         "movaps 32(%2, %0), %%xmm2      \n\t"
1009         "movaps 48(%2, %0), %%xmm3      \n\t"
1010         "maxps      %%xmm4, %%xmm0      \n\t"
1011         "maxps      %%xmm4, %%xmm1      \n\t"
1012         "maxps      %%xmm4, %%xmm2      \n\t"
1013         "maxps      %%xmm4, %%xmm3      \n\t"
1014         "minps      %%xmm5, %%xmm0      \n\t"
1015         "minps      %%xmm5, %%xmm1      \n\t"
1016         "minps      %%xmm5, %%xmm2      \n\t"
1017         "minps      %%xmm5, %%xmm3      \n\t"
1018         "movaps     %%xmm0,   (%1, %0)  \n\t"
1019         "movaps     %%xmm1, 16(%1, %0)  \n\t"
1020         "movaps     %%xmm2, 32(%1, %0)  \n\t"
1021         "movaps     %%xmm3, 48(%1, %0)  \n\t"
1022         "sub           $64, %0          \n\t"
1023         "jge            1b              \n\t"
1024         : "+&r"(i)
1025         : "r"(dst), "r"(src), "m"(min), "m"(max)
1026         : "memory"
1027     );
1028 }
1029
1030 #endif /* HAVE_INLINE_ASM */
1031
1032 void ff_h263_v_loop_filter_mmx(uint8_t *src, int stride, int qscale);
1033 void ff_h263_h_loop_filter_mmx(uint8_t *src, int stride, int qscale);
1034
1035 int32_t ff_scalarproduct_int16_mmxext(const int16_t *v1, const int16_t *v2,
1036                                       int order);
1037 int32_t ff_scalarproduct_int16_sse2(const int16_t *v1, const int16_t *v2,
1038                                     int order);
1039 int32_t ff_scalarproduct_and_madd_int16_mmxext(int16_t *v1, const int16_t *v2,
1040                                                const int16_t *v3,
1041                                                int order, int mul);
1042 int32_t ff_scalarproduct_and_madd_int16_sse2(int16_t *v1, const int16_t *v2,
1043                                              const int16_t *v3,
1044                                              int order, int mul);
1045 int32_t ff_scalarproduct_and_madd_int16_ssse3(int16_t *v1, const int16_t *v2,
1046                                               const int16_t *v3,
1047                                               int order, int mul);
1048
1049 void ff_apply_window_int16_round_mmxext(int16_t *output, const int16_t *input,
1050                                         const int16_t *window, unsigned int len);
1051 void ff_apply_window_int16_round_sse2(int16_t *output, const int16_t *input,
1052                                       const int16_t *window, unsigned int len);
1053 void ff_apply_window_int16_mmxext(int16_t *output, const int16_t *input,
1054                                   const int16_t *window, unsigned int len);
1055 void ff_apply_window_int16_sse2(int16_t *output, const int16_t *input,
1056                                 const int16_t *window, unsigned int len);
1057 void ff_apply_window_int16_ssse3(int16_t *output, const int16_t *input,
1058                                  const int16_t *window, unsigned int len);
1059 void ff_apply_window_int16_ssse3_atom(int16_t *output, const int16_t *input,
1060                                       const int16_t *window, unsigned int len);
1061
1062 void ff_bswap32_buf_ssse3(uint32_t *dst, const uint32_t *src, int w);
1063 void ff_bswap32_buf_sse2(uint32_t *dst, const uint32_t *src, int w);
1064
1065 void ff_add_hfyu_median_prediction_mmxext(uint8_t *dst, const uint8_t *top,
1066                                           const uint8_t *diff, int w,
1067                                           int *left, int *left_top);
1068 int  ff_add_hfyu_left_prediction_ssse3(uint8_t *dst, const uint8_t *src,
1069                                        int w, int left);
1070 int  ff_add_hfyu_left_prediction_sse4(uint8_t *dst, const uint8_t *src,
1071                                       int w, int left);
1072
1073 void ff_vector_clip_int32_mmx     (int32_t *dst, const int32_t *src,
1074                                    int32_t min, int32_t max, unsigned int len);
1075 void ff_vector_clip_int32_sse2    (int32_t *dst, const int32_t *src,
1076                                    int32_t min, int32_t max, unsigned int len);
1077 void ff_vector_clip_int32_int_sse2(int32_t *dst, const int32_t *src,
1078                                    int32_t min, int32_t max, unsigned int len);
1079 void ff_vector_clip_int32_sse4    (int32_t *dst, const int32_t *src,
1080                                    int32_t min, int32_t max, unsigned int len);
1081
1082 #define SET_QPEL_FUNCS(PFX, IDX, SIZE, CPU, PREFIX)                          \
1083     do {                                                                     \
1084     c->PFX ## _pixels_tab[IDX][ 0] = PREFIX ## PFX ## SIZE ## _mc00_ ## CPU; \
1085     c->PFX ## _pixels_tab[IDX][ 1] = PREFIX ## PFX ## SIZE ## _mc10_ ## CPU; \
1086     c->PFX ## _pixels_tab[IDX][ 2] = PREFIX ## PFX ## SIZE ## _mc20_ ## CPU; \
1087     c->PFX ## _pixels_tab[IDX][ 3] = PREFIX ## PFX ## SIZE ## _mc30_ ## CPU; \
1088     c->PFX ## _pixels_tab[IDX][ 4] = PREFIX ## PFX ## SIZE ## _mc01_ ## CPU; \
1089     c->PFX ## _pixels_tab[IDX][ 5] = PREFIX ## PFX ## SIZE ## _mc11_ ## CPU; \
1090     c->PFX ## _pixels_tab[IDX][ 6] = PREFIX ## PFX ## SIZE ## _mc21_ ## CPU; \
1091     c->PFX ## _pixels_tab[IDX][ 7] = PREFIX ## PFX ## SIZE ## _mc31_ ## CPU; \
1092     c->PFX ## _pixels_tab[IDX][ 8] = PREFIX ## PFX ## SIZE ## _mc02_ ## CPU; \
1093     c->PFX ## _pixels_tab[IDX][ 9] = PREFIX ## PFX ## SIZE ## _mc12_ ## CPU; \
1094     c->PFX ## _pixels_tab[IDX][10] = PREFIX ## PFX ## SIZE ## _mc22_ ## CPU; \
1095     c->PFX ## _pixels_tab[IDX][11] = PREFIX ## PFX ## SIZE ## _mc32_ ## CPU; \
1096     c->PFX ## _pixels_tab[IDX][12] = PREFIX ## PFX ## SIZE ## _mc03_ ## CPU; \
1097     c->PFX ## _pixels_tab[IDX][13] = PREFIX ## PFX ## SIZE ## _mc13_ ## CPU; \
1098     c->PFX ## _pixels_tab[IDX][14] = PREFIX ## PFX ## SIZE ## _mc23_ ## CPU; \
1099     c->PFX ## _pixels_tab[IDX][15] = PREFIX ## PFX ## SIZE ## _mc33_ ## CPU; \
1100     } while (0)
1101
1102 static av_cold void dsputil_init_mmx(DSPContext *c, AVCodecContext *avctx,
1103                                      int mm_flags)
1104 {
1105 #if HAVE_MMX_INLINE
1106     const int high_bit_depth = avctx->bits_per_raw_sample > 8;
1107
1108     c->put_pixels_clamped        = ff_put_pixels_clamped_mmx;
1109     c->put_signed_pixels_clamped = ff_put_signed_pixels_clamped_mmx;
1110     c->add_pixels_clamped        = ff_add_pixels_clamped_mmx;
1111
1112     if (!high_bit_depth) {
1113         c->clear_block  = clear_block_mmx;
1114         c->clear_blocks = clear_blocks_mmx;
1115         c->draw_edges   = draw_edges_mmx;
1116
1117         switch (avctx->idct_algo) {
1118         case FF_IDCT_AUTO:
1119         case FF_IDCT_SIMPLEMMX:
1120             c->idct_put              = ff_simple_idct_put_mmx;
1121             c->idct_add              = ff_simple_idct_add_mmx;
1122             c->idct                  = ff_simple_idct_mmx;
1123             c->idct_permutation_type = FF_SIMPLE_IDCT_PERM;
1124             break;
1125         case FF_IDCT_XVIDMMX:
1126             c->idct_put              = ff_idct_xvid_mmx_put;
1127             c->idct_add              = ff_idct_xvid_mmx_add;
1128             c->idct                  = ff_idct_xvid_mmx;
1129             break;
1130         }
1131     }
1132
1133     c->gmc = gmc_mmx;
1134
1135     c->add_bytes = add_bytes_mmx;
1136 #endif /* HAVE_MMX_INLINE */
1137
1138 #if HAVE_MMX_EXTERNAL
1139     if (CONFIG_H263_DECODER || CONFIG_H263_ENCODER) {
1140         c->h263_v_loop_filter = ff_h263_v_loop_filter_mmx;
1141         c->h263_h_loop_filter = ff_h263_h_loop_filter_mmx;
1142     }
1143
1144     c->vector_clip_int32 = ff_vector_clip_int32_mmx;
1145 #endif /* HAVE_MMX_EXTERNAL */
1146 }
1147
1148 static av_cold void dsputil_init_mmxext(DSPContext *c, AVCodecContext *avctx,
1149                                         int mm_flags)
1150 {
1151 #if HAVE_MMXEXT_INLINE
1152     const int high_bit_depth = avctx->bits_per_raw_sample > 8;
1153
1154     if (!high_bit_depth && avctx->idct_algo == FF_IDCT_XVIDMMX) {
1155         c->idct_put = ff_idct_xvid_mmxext_put;
1156         c->idct_add = ff_idct_xvid_mmxext_add;
1157         c->idct     = ff_idct_xvid_mmxext;
1158     }
1159 #endif /* HAVE_MMXEXT_INLINE */
1160
1161 #if HAVE_MMXEXT_EXTERNAL
1162     SET_QPEL_FUNCS(avg_qpel,        0, 16, mmxext, );
1163     SET_QPEL_FUNCS(avg_qpel,        1,  8, mmxext, );
1164
1165     SET_QPEL_FUNCS(put_qpel,        0, 16, mmxext, );
1166     SET_QPEL_FUNCS(put_qpel,        1,  8, mmxext, );
1167     SET_QPEL_FUNCS(put_no_rnd_qpel, 0, 16, mmxext, );
1168     SET_QPEL_FUNCS(put_no_rnd_qpel, 1,  8, mmxext, );
1169
1170     /* slower than cmov version on AMD */
1171     if (!(mm_flags & AV_CPU_FLAG_3DNOW))
1172         c->add_hfyu_median_prediction = ff_add_hfyu_median_prediction_mmxext;
1173
1174     c->scalarproduct_int16          = ff_scalarproduct_int16_mmxext;
1175     c->scalarproduct_and_madd_int16 = ff_scalarproduct_and_madd_int16_mmxext;
1176
1177     if (avctx->flags & CODEC_FLAG_BITEXACT) {
1178         c->apply_window_int16 = ff_apply_window_int16_mmxext;
1179     } else {
1180         c->apply_window_int16 = ff_apply_window_int16_round_mmxext;
1181     }
1182 #endif /* HAVE_MMXEXT_EXTERNAL */
1183 }
1184
1185 static av_cold void dsputil_init_sse(DSPContext *c, AVCodecContext *avctx,
1186                                      int mm_flags)
1187 {
1188 #if HAVE_SSE_INLINE
1189     const int high_bit_depth = avctx->bits_per_raw_sample > 8;
1190
1191     if (!high_bit_depth) {
1192         if (!(CONFIG_MPEG_XVMC_DECODER && avctx->xvmc_acceleration > 1)) {
1193             /* XvMCCreateBlocks() may not allocate 16-byte aligned blocks */
1194             c->clear_block  = clear_block_sse;
1195             c->clear_blocks = clear_blocks_sse;
1196         }
1197     }
1198
1199     c->vector_clipf = vector_clipf_sse;
1200 #endif /* HAVE_SSE_INLINE */
1201 }
1202
1203 static av_cold void dsputil_init_sse2(DSPContext *c, AVCodecContext *avctx,
1204                                       int mm_flags)
1205 {
1206 #if HAVE_SSE2_INLINE
1207     const int high_bit_depth = avctx->bits_per_raw_sample > 8;
1208
1209     if (!high_bit_depth && avctx->idct_algo == FF_IDCT_XVIDMMX) {
1210         c->idct_put              = ff_idct_xvid_sse2_put;
1211         c->idct_add              = ff_idct_xvid_sse2_add;
1212         c->idct                  = ff_idct_xvid_sse2;
1213         c->idct_permutation_type = FF_SSE2_IDCT_PERM;
1214     }
1215 #endif /* HAVE_SSE2_INLINE */
1216
1217 #if HAVE_SSE2_EXTERNAL
1218     c->scalarproduct_int16          = ff_scalarproduct_int16_sse2;
1219     c->scalarproduct_and_madd_int16 = ff_scalarproduct_and_madd_int16_sse2;
1220     if (mm_flags & AV_CPU_FLAG_ATOM) {
1221         c->vector_clip_int32 = ff_vector_clip_int32_int_sse2;
1222     } else {
1223         c->vector_clip_int32 = ff_vector_clip_int32_sse2;
1224     }
1225     if (avctx->flags & CODEC_FLAG_BITEXACT) {
1226         c->apply_window_int16 = ff_apply_window_int16_sse2;
1227     } else if (!(mm_flags & AV_CPU_FLAG_SSE2SLOW)) {
1228         c->apply_window_int16 = ff_apply_window_int16_round_sse2;
1229     }
1230     c->bswap_buf = ff_bswap32_buf_sse2;
1231 #endif /* HAVE_SSE2_EXTERNAL */
1232 }
1233
1234 static av_cold void dsputil_init_ssse3(DSPContext *c, AVCodecContext *avctx,
1235                                        int mm_flags)
1236 {
1237 #if HAVE_SSSE3_EXTERNAL
1238     c->add_hfyu_left_prediction = ff_add_hfyu_left_prediction_ssse3;
1239     if (mm_flags & AV_CPU_FLAG_SSE4) // not really sse4, just slow on Conroe
1240         c->add_hfyu_left_prediction = ff_add_hfyu_left_prediction_sse4;
1241
1242     if (mm_flags & AV_CPU_FLAG_ATOM)
1243         c->apply_window_int16 = ff_apply_window_int16_ssse3_atom;
1244     else
1245         c->apply_window_int16 = ff_apply_window_int16_ssse3;
1246     if (!(mm_flags & (AV_CPU_FLAG_SSE42|AV_CPU_FLAG_3DNOW))) // cachesplit
1247         c->scalarproduct_and_madd_int16 = ff_scalarproduct_and_madd_int16_ssse3;
1248     c->bswap_buf = ff_bswap32_buf_ssse3;
1249 #endif /* HAVE_SSSE3_EXTERNAL */
1250 }
1251
1252 static av_cold void dsputil_init_sse4(DSPContext *c, AVCodecContext *avctx,
1253                                       int mm_flags)
1254 {
1255 #if HAVE_SSE4_EXTERNAL
1256     c->vector_clip_int32 = ff_vector_clip_int32_sse4;
1257 #endif /* HAVE_SSE4_EXTERNAL */
1258 }
1259
1260 av_cold void ff_dsputil_init_mmx(DSPContext *c, AVCodecContext *avctx)
1261 {
1262     int mm_flags = av_get_cpu_flags();
1263
1264 #if HAVE_7REGS && HAVE_INLINE_ASM
1265     if (mm_flags & AV_CPU_FLAG_CMOV)
1266         c->add_hfyu_median_prediction = add_hfyu_median_prediction_cmov;
1267 #endif
1268
1269     if (mm_flags & AV_CPU_FLAG_MMX)
1270         dsputil_init_mmx(c, avctx, mm_flags);
1271
1272     if (mm_flags & AV_CPU_FLAG_MMXEXT)
1273         dsputil_init_mmxext(c, avctx, mm_flags);
1274
1275     if (mm_flags & AV_CPU_FLAG_SSE)
1276         dsputil_init_sse(c, avctx, mm_flags);
1277
1278     if (mm_flags & AV_CPU_FLAG_SSE2)
1279         dsputil_init_sse2(c, avctx, mm_flags);
1280
1281     if (mm_flags & AV_CPU_FLAG_SSSE3)
1282         dsputil_init_ssse3(c, avctx, mm_flags);
1283
1284     if (mm_flags & AV_CPU_FLAG_SSE4)
1285         dsputil_init_sse4(c, avctx, mm_flags);
1286
1287     if (CONFIG_ENCODERS)
1288         ff_dsputilenc_init_mmx(c, avctx);
1289 }