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