x86: dsputil: Move rv40-specific functions where they belong
[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 static void gmc_mmx(uint8_t *dst, uint8_t *src,
864                     int stride, int h, int ox, int oy,
865                     int dxx, int dxy, int dyx, int dyy,
866                     int shift, int r, int width, int height)
867 {
868     const int w    = 8;
869     const int ix   = ox  >> (16 + shift);
870     const int iy   = oy  >> (16 + shift);
871     const int oxs  = ox  >> 4;
872     const int oys  = oy  >> 4;
873     const int dxxs = dxx >> 4;
874     const int dxys = dxy >> 4;
875     const int dyxs = dyx >> 4;
876     const int dyys = dyy >> 4;
877     const uint16_t r4[4]   = { r, r, r, r };
878     const uint16_t dxy4[4] = { dxys, dxys, dxys, dxys };
879     const uint16_t dyy4[4] = { dyys, dyys, dyys, dyys };
880     const uint64_t shift2 = 2 * shift;
881     int x, y;
882
883     const int dxw = (dxx - (1 << (16 + shift))) * (w - 1);
884     const int dyh = (dyy - (1 << (16 + shift))) * (h - 1);
885     const int dxh = dxy * (h - 1);
886     const int dyw = dyx * (w - 1);
887     if ( // non-constant fullpel offset (3% of blocks)
888         ((ox ^ (ox + dxw)) | (ox ^ (ox + dxh)) | (ox ^ (ox + dxw + dxh)) |
889          (oy ^ (oy + dyw)) | (oy ^ (oy + dyh)) | (oy ^ (oy + dyw + dyh))) >> (16 + shift)
890         // uses more than 16 bits of subpel mv (only at huge resolution)
891         || (dxx | dxy | dyx | dyy) & 15 ||
892         (unsigned)ix >= width  - w ||
893         (unsigned)iy >= height - h) {
894         // FIXME could still use mmx for some of the rows
895         ff_gmc_c(dst, src, stride, h, ox, oy, dxx, dxy, dyx, dyy,
896                  shift, r, width, height);
897         return;
898     }
899
900     src += ix + iy * stride;
901
902     __asm__ volatile (
903         "movd         %0, %%mm6         \n\t"
904         "pxor      %%mm7, %%mm7         \n\t"
905         "punpcklwd %%mm6, %%mm6         \n\t"
906         "punpcklwd %%mm6, %%mm6         \n\t"
907         :: "r"(1<<shift)
908     );
909
910     for (x = 0; x < w; x += 4) {
911         uint16_t dx4[4] = { oxs - dxys + dxxs * (x + 0),
912                             oxs - dxys + dxxs * (x + 1),
913                             oxs - dxys + dxxs * (x + 2),
914                             oxs - dxys + dxxs * (x + 3) };
915         uint16_t dy4[4] = { oys - dyys + dyxs * (x + 0),
916                             oys - dyys + dyxs * (x + 1),
917                             oys - dyys + dyxs * (x + 2),
918                             oys - dyys + dyxs * (x + 3) };
919
920         for (y = 0; y < h; y++) {
921             __asm__ volatile (
922                 "movq      %0, %%mm4    \n\t"
923                 "movq      %1, %%mm5    \n\t"
924                 "paddw     %2, %%mm4    \n\t"
925                 "paddw     %3, %%mm5    \n\t"
926                 "movq   %%mm4, %0       \n\t"
927                 "movq   %%mm5, %1       \n\t"
928                 "psrlw    $12, %%mm4    \n\t"
929                 "psrlw    $12, %%mm5    \n\t"
930                 : "+m"(*dx4), "+m"(*dy4)
931                 : "m"(*dxy4), "m"(*dyy4)
932             );
933
934             __asm__ volatile (
935                 "movq      %%mm6, %%mm2 \n\t"
936                 "movq      %%mm6, %%mm1 \n\t"
937                 "psubw     %%mm4, %%mm2 \n\t"
938                 "psubw     %%mm5, %%mm1 \n\t"
939                 "movq      %%mm2, %%mm0 \n\t"
940                 "movq      %%mm4, %%mm3 \n\t"
941                 "pmullw    %%mm1, %%mm0 \n\t" // (s - dx) * (s - dy)
942                 "pmullw    %%mm5, %%mm3 \n\t" // dx * dy
943                 "pmullw    %%mm5, %%mm2 \n\t" // (s - dx) * dy
944                 "pmullw    %%mm4, %%mm1 \n\t" // dx * (s - dy)
945
946                 "movd         %4, %%mm5 \n\t"
947                 "movd         %3, %%mm4 \n\t"
948                 "punpcklbw %%mm7, %%mm5 \n\t"
949                 "punpcklbw %%mm7, %%mm4 \n\t"
950                 "pmullw    %%mm5, %%mm3 \n\t" // src[1, 1] * dx * dy
951                 "pmullw    %%mm4, %%mm2 \n\t" // src[0, 1] * (s - dx) * dy
952
953                 "movd         %2, %%mm5 \n\t"
954                 "movd         %1, %%mm4 \n\t"
955                 "punpcklbw %%mm7, %%mm5 \n\t"
956                 "punpcklbw %%mm7, %%mm4 \n\t"
957                 "pmullw    %%mm5, %%mm1 \n\t" // src[1, 0] * dx * (s - dy)
958                 "pmullw    %%mm4, %%mm0 \n\t" // src[0, 0] * (s - dx) * (s - dy)
959                 "paddw        %5, %%mm1 \n\t"
960                 "paddw     %%mm3, %%mm2 \n\t"
961                 "paddw     %%mm1, %%mm0 \n\t"
962                 "paddw     %%mm2, %%mm0 \n\t"
963
964                 "psrlw        %6, %%mm0 \n\t"
965                 "packuswb  %%mm0, %%mm0 \n\t"
966                 "movd      %%mm0, %0    \n\t"
967
968                 : "=m"(dst[x + y * stride])
969                 : "m"(src[0]), "m"(src[1]),
970                   "m"(src[stride]), "m"(src[stride + 1]),
971                   "m"(*r4), "m"(shift2)
972             );
973             src += stride;
974         }
975         src += 4 - h * stride;
976     }
977 }
978
979 static void vector_clipf_sse(float *dst, const float *src,
980                              float min, float max, int len)
981 {
982     x86_reg i = (len - 16) * 4;
983     __asm__ volatile (
984         "movss          %3, %%xmm4      \n\t"
985         "movss          %4, %%xmm5      \n\t"
986         "shufps $0, %%xmm4, %%xmm4      \n\t"
987         "shufps $0, %%xmm5, %%xmm5      \n\t"
988         "1:                             \n\t"
989         "movaps   (%2, %0), %%xmm0      \n\t" // 3/1 on intel
990         "movaps 16(%2, %0), %%xmm1      \n\t"
991         "movaps 32(%2, %0), %%xmm2      \n\t"
992         "movaps 48(%2, %0), %%xmm3      \n\t"
993         "maxps      %%xmm4, %%xmm0      \n\t"
994         "maxps      %%xmm4, %%xmm1      \n\t"
995         "maxps      %%xmm4, %%xmm2      \n\t"
996         "maxps      %%xmm4, %%xmm3      \n\t"
997         "minps      %%xmm5, %%xmm0      \n\t"
998         "minps      %%xmm5, %%xmm1      \n\t"
999         "minps      %%xmm5, %%xmm2      \n\t"
1000         "minps      %%xmm5, %%xmm3      \n\t"
1001         "movaps     %%xmm0,   (%1, %0)  \n\t"
1002         "movaps     %%xmm1, 16(%1, %0)  \n\t"
1003         "movaps     %%xmm2, 32(%1, %0)  \n\t"
1004         "movaps     %%xmm3, 48(%1, %0)  \n\t"
1005         "sub           $64, %0          \n\t"
1006         "jge            1b              \n\t"
1007         : "+&r"(i)
1008         : "r"(dst), "r"(src), "m"(min), "m"(max)
1009         : "memory"
1010     );
1011 }
1012
1013 #endif /* HAVE_INLINE_ASM */
1014
1015 void ff_h263_v_loop_filter_mmx(uint8_t *src, int stride, int qscale);
1016 void ff_h263_h_loop_filter_mmx(uint8_t *src, int stride, int qscale);
1017
1018 int32_t ff_scalarproduct_int16_mmxext(const int16_t *v1, const int16_t *v2,
1019                                       int order);
1020 int32_t ff_scalarproduct_int16_sse2(const int16_t *v1, const int16_t *v2,
1021                                     int order);
1022 int32_t ff_scalarproduct_and_madd_int16_mmxext(int16_t *v1, const int16_t *v2,
1023                                                const int16_t *v3,
1024                                                int order, int mul);
1025 int32_t ff_scalarproduct_and_madd_int16_sse2(int16_t *v1, const int16_t *v2,
1026                                              const int16_t *v3,
1027                                              int order, int mul);
1028 int32_t ff_scalarproduct_and_madd_int16_ssse3(int16_t *v1, const int16_t *v2,
1029                                               const int16_t *v3,
1030                                               int order, int mul);
1031
1032 void ff_apply_window_int16_round_mmxext(int16_t *output, const int16_t *input,
1033                                         const int16_t *window, unsigned int len);
1034 void ff_apply_window_int16_round_sse2(int16_t *output, const int16_t *input,
1035                                       const int16_t *window, unsigned int len);
1036 void ff_apply_window_int16_mmxext(int16_t *output, const int16_t *input,
1037                                   const int16_t *window, unsigned int len);
1038 void ff_apply_window_int16_sse2(int16_t *output, const int16_t *input,
1039                                 const int16_t *window, unsigned int len);
1040 void ff_apply_window_int16_ssse3(int16_t *output, const int16_t *input,
1041                                  const int16_t *window, unsigned int len);
1042 void ff_apply_window_int16_ssse3_atom(int16_t *output, const int16_t *input,
1043                                       const int16_t *window, unsigned int len);
1044
1045 void ff_bswap32_buf_ssse3(uint32_t *dst, const uint32_t *src, int w);
1046 void ff_bswap32_buf_sse2(uint32_t *dst, const uint32_t *src, int w);
1047
1048 void ff_add_hfyu_median_prediction_mmxext(uint8_t *dst, const uint8_t *top,
1049                                           const uint8_t *diff, int w,
1050                                           int *left, int *left_top);
1051 int  ff_add_hfyu_left_prediction_ssse3(uint8_t *dst, const uint8_t *src,
1052                                        int w, int left);
1053 int  ff_add_hfyu_left_prediction_sse4(uint8_t *dst, const uint8_t *src,
1054                                       int w, int left);
1055
1056 void ff_vector_clip_int32_mmx     (int32_t *dst, const int32_t *src,
1057                                    int32_t min, int32_t max, unsigned int len);
1058 void ff_vector_clip_int32_sse2    (int32_t *dst, const int32_t *src,
1059                                    int32_t min, int32_t max, unsigned int len);
1060 void ff_vector_clip_int32_int_sse2(int32_t *dst, const int32_t *src,
1061                                    int32_t min, int32_t max, unsigned int len);
1062 void ff_vector_clip_int32_sse4    (int32_t *dst, const int32_t *src,
1063                                    int32_t min, int32_t max, unsigned int len);
1064
1065 #define SET_QPEL_FUNCS(PFX, IDX, SIZE, CPU, PREFIX)                          \
1066     do {                                                                     \
1067     c->PFX ## _pixels_tab[IDX][ 0] = PREFIX ## PFX ## SIZE ## _mc00_ ## CPU; \
1068     c->PFX ## _pixels_tab[IDX][ 1] = PREFIX ## PFX ## SIZE ## _mc10_ ## CPU; \
1069     c->PFX ## _pixels_tab[IDX][ 2] = PREFIX ## PFX ## SIZE ## _mc20_ ## CPU; \
1070     c->PFX ## _pixels_tab[IDX][ 3] = PREFIX ## PFX ## SIZE ## _mc30_ ## CPU; \
1071     c->PFX ## _pixels_tab[IDX][ 4] = PREFIX ## PFX ## SIZE ## _mc01_ ## CPU; \
1072     c->PFX ## _pixels_tab[IDX][ 5] = PREFIX ## PFX ## SIZE ## _mc11_ ## CPU; \
1073     c->PFX ## _pixels_tab[IDX][ 6] = PREFIX ## PFX ## SIZE ## _mc21_ ## CPU; \
1074     c->PFX ## _pixels_tab[IDX][ 7] = PREFIX ## PFX ## SIZE ## _mc31_ ## CPU; \
1075     c->PFX ## _pixels_tab[IDX][ 8] = PREFIX ## PFX ## SIZE ## _mc02_ ## CPU; \
1076     c->PFX ## _pixels_tab[IDX][ 9] = PREFIX ## PFX ## SIZE ## _mc12_ ## CPU; \
1077     c->PFX ## _pixels_tab[IDX][10] = PREFIX ## PFX ## SIZE ## _mc22_ ## CPU; \
1078     c->PFX ## _pixels_tab[IDX][11] = PREFIX ## PFX ## SIZE ## _mc32_ ## CPU; \
1079     c->PFX ## _pixels_tab[IDX][12] = PREFIX ## PFX ## SIZE ## _mc03_ ## CPU; \
1080     c->PFX ## _pixels_tab[IDX][13] = PREFIX ## PFX ## SIZE ## _mc13_ ## CPU; \
1081     c->PFX ## _pixels_tab[IDX][14] = PREFIX ## PFX ## SIZE ## _mc23_ ## CPU; \
1082     c->PFX ## _pixels_tab[IDX][15] = PREFIX ## PFX ## SIZE ## _mc33_ ## CPU; \
1083     } while (0)
1084
1085 static av_cold void dsputil_init_mmx(DSPContext *c, AVCodecContext *avctx,
1086                                      int mm_flags)
1087 {
1088 #if HAVE_MMX_INLINE
1089     const int high_bit_depth = avctx->bits_per_raw_sample > 8;
1090
1091     c->put_pixels_clamped        = ff_put_pixels_clamped_mmx;
1092     c->put_signed_pixels_clamped = ff_put_signed_pixels_clamped_mmx;
1093     c->add_pixels_clamped        = ff_add_pixels_clamped_mmx;
1094
1095     if (!high_bit_depth) {
1096         c->clear_block  = clear_block_mmx;
1097         c->clear_blocks = clear_blocks_mmx;
1098         c->draw_edges   = draw_edges_mmx;
1099
1100         switch (avctx->idct_algo) {
1101         case FF_IDCT_AUTO:
1102         case FF_IDCT_SIMPLEMMX:
1103             c->idct_put              = ff_simple_idct_put_mmx;
1104             c->idct_add              = ff_simple_idct_add_mmx;
1105             c->idct                  = ff_simple_idct_mmx;
1106             c->idct_permutation_type = FF_SIMPLE_IDCT_PERM;
1107             break;
1108         case FF_IDCT_XVIDMMX:
1109             c->idct_put              = ff_idct_xvid_mmx_put;
1110             c->idct_add              = ff_idct_xvid_mmx_add;
1111             c->idct                  = ff_idct_xvid_mmx;
1112             break;
1113         }
1114     }
1115
1116     c->gmc = gmc_mmx;
1117
1118     c->add_bytes = add_bytes_mmx;
1119 #endif /* HAVE_MMX_INLINE */
1120
1121 #if HAVE_MMX_EXTERNAL
1122     if (CONFIG_H263_DECODER || CONFIG_H263_ENCODER) {
1123         c->h263_v_loop_filter = ff_h263_v_loop_filter_mmx;
1124         c->h263_h_loop_filter = ff_h263_h_loop_filter_mmx;
1125     }
1126
1127     c->vector_clip_int32 = ff_vector_clip_int32_mmx;
1128 #endif /* HAVE_MMX_EXTERNAL */
1129 }
1130
1131 static av_cold void dsputil_init_mmxext(DSPContext *c, AVCodecContext *avctx,
1132                                         int mm_flags)
1133 {
1134 #if HAVE_MMXEXT_INLINE
1135     const int high_bit_depth = avctx->bits_per_raw_sample > 8;
1136
1137     if (!high_bit_depth && avctx->idct_algo == FF_IDCT_XVIDMMX) {
1138         c->idct_put = ff_idct_xvid_mmxext_put;
1139         c->idct_add = ff_idct_xvid_mmxext_add;
1140         c->idct     = ff_idct_xvid_mmxext;
1141     }
1142 #endif /* HAVE_MMXEXT_INLINE */
1143
1144 #if HAVE_MMXEXT_EXTERNAL
1145     SET_QPEL_FUNCS(avg_qpel,        0, 16, mmxext, );
1146     SET_QPEL_FUNCS(avg_qpel,        1,  8, mmxext, );
1147
1148     SET_QPEL_FUNCS(put_qpel,        0, 16, mmxext, );
1149     SET_QPEL_FUNCS(put_qpel,        1,  8, mmxext, );
1150     SET_QPEL_FUNCS(put_no_rnd_qpel, 0, 16, mmxext, );
1151     SET_QPEL_FUNCS(put_no_rnd_qpel, 1,  8, mmxext, );
1152
1153     /* slower than cmov version on AMD */
1154     if (!(mm_flags & AV_CPU_FLAG_3DNOW))
1155         c->add_hfyu_median_prediction = ff_add_hfyu_median_prediction_mmxext;
1156
1157     c->scalarproduct_int16          = ff_scalarproduct_int16_mmxext;
1158     c->scalarproduct_and_madd_int16 = ff_scalarproduct_and_madd_int16_mmxext;
1159
1160     if (avctx->flags & CODEC_FLAG_BITEXACT) {
1161         c->apply_window_int16 = ff_apply_window_int16_mmxext;
1162     } else {
1163         c->apply_window_int16 = ff_apply_window_int16_round_mmxext;
1164     }
1165 #endif /* HAVE_MMXEXT_EXTERNAL */
1166 }
1167
1168 static av_cold void dsputil_init_sse(DSPContext *c, AVCodecContext *avctx,
1169                                      int mm_flags)
1170 {
1171 #if HAVE_SSE_INLINE
1172     const int high_bit_depth = avctx->bits_per_raw_sample > 8;
1173
1174     if (!high_bit_depth) {
1175         if (!(CONFIG_MPEG_XVMC_DECODER && avctx->xvmc_acceleration > 1)) {
1176             /* XvMCCreateBlocks() may not allocate 16-byte aligned blocks */
1177             c->clear_block  = clear_block_sse;
1178             c->clear_blocks = clear_blocks_sse;
1179         }
1180     }
1181
1182     c->vector_clipf = vector_clipf_sse;
1183 #endif /* HAVE_SSE_INLINE */
1184 }
1185
1186 static av_cold void dsputil_init_sse2(DSPContext *c, AVCodecContext *avctx,
1187                                       int mm_flags)
1188 {
1189 #if HAVE_SSE2_INLINE
1190     const int high_bit_depth = avctx->bits_per_raw_sample > 8;
1191
1192     if (!high_bit_depth && avctx->idct_algo == FF_IDCT_XVIDMMX) {
1193         c->idct_put              = ff_idct_xvid_sse2_put;
1194         c->idct_add              = ff_idct_xvid_sse2_add;
1195         c->idct                  = ff_idct_xvid_sse2;
1196         c->idct_permutation_type = FF_SSE2_IDCT_PERM;
1197     }
1198 #endif /* HAVE_SSE2_INLINE */
1199
1200 #if HAVE_SSE2_EXTERNAL
1201     c->scalarproduct_int16          = ff_scalarproduct_int16_sse2;
1202     c->scalarproduct_and_madd_int16 = ff_scalarproduct_and_madd_int16_sse2;
1203     if (mm_flags & AV_CPU_FLAG_ATOM) {
1204         c->vector_clip_int32 = ff_vector_clip_int32_int_sse2;
1205     } else {
1206         c->vector_clip_int32 = ff_vector_clip_int32_sse2;
1207     }
1208     if (avctx->flags & CODEC_FLAG_BITEXACT) {
1209         c->apply_window_int16 = ff_apply_window_int16_sse2;
1210     } else if (!(mm_flags & AV_CPU_FLAG_SSE2SLOW)) {
1211         c->apply_window_int16 = ff_apply_window_int16_round_sse2;
1212     }
1213     c->bswap_buf = ff_bswap32_buf_sse2;
1214 #endif /* HAVE_SSE2_EXTERNAL */
1215 }
1216
1217 static av_cold void dsputil_init_ssse3(DSPContext *c, AVCodecContext *avctx,
1218                                        int mm_flags)
1219 {
1220 #if HAVE_SSSE3_EXTERNAL
1221     c->add_hfyu_left_prediction = ff_add_hfyu_left_prediction_ssse3;
1222     if (mm_flags & AV_CPU_FLAG_SSE4) // not really sse4, just slow on Conroe
1223         c->add_hfyu_left_prediction = ff_add_hfyu_left_prediction_sse4;
1224
1225     if (mm_flags & AV_CPU_FLAG_ATOM)
1226         c->apply_window_int16 = ff_apply_window_int16_ssse3_atom;
1227     else
1228         c->apply_window_int16 = ff_apply_window_int16_ssse3;
1229     if (!(mm_flags & (AV_CPU_FLAG_SSE42|AV_CPU_FLAG_3DNOW))) // cachesplit
1230         c->scalarproduct_and_madd_int16 = ff_scalarproduct_and_madd_int16_ssse3;
1231     c->bswap_buf = ff_bswap32_buf_ssse3;
1232 #endif /* HAVE_SSSE3_EXTERNAL */
1233 }
1234
1235 static av_cold void dsputil_init_sse4(DSPContext *c, AVCodecContext *avctx,
1236                                       int mm_flags)
1237 {
1238 #if HAVE_SSE4_EXTERNAL
1239     c->vector_clip_int32 = ff_vector_clip_int32_sse4;
1240 #endif /* HAVE_SSE4_EXTERNAL */
1241 }
1242
1243 av_cold void ff_dsputil_init_mmx(DSPContext *c, AVCodecContext *avctx)
1244 {
1245     int mm_flags = av_get_cpu_flags();
1246
1247 #if HAVE_7REGS && HAVE_INLINE_ASM
1248     if (mm_flags & AV_CPU_FLAG_CMOV)
1249         c->add_hfyu_median_prediction = add_hfyu_median_prediction_cmov;
1250 #endif
1251
1252     if (mm_flags & AV_CPU_FLAG_MMX)
1253         dsputil_init_mmx(c, avctx, mm_flags);
1254
1255     if (mm_flags & AV_CPU_FLAG_MMXEXT)
1256         dsputil_init_mmxext(c, avctx, mm_flags);
1257
1258     if (mm_flags & AV_CPU_FLAG_SSE)
1259         dsputil_init_sse(c, avctx, mm_flags);
1260
1261     if (mm_flags & AV_CPU_FLAG_SSE2)
1262         dsputil_init_sse2(c, avctx, mm_flags);
1263
1264     if (mm_flags & AV_CPU_FLAG_SSSE3)
1265         dsputil_init_ssse3(c, avctx, mm_flags);
1266
1267     if (mm_flags & AV_CPU_FLAG_SSE4)
1268         dsputil_init_sse4(c, avctx, mm_flags);
1269
1270     if (CONFIG_ENCODERS)
1271         ff_dsputilenc_init_mmx(c, avctx);
1272 }