Merge commit '096cc11ec102701a18951b4f0437d609081ca1dd'
[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  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  *
22  * MMX optimization by Nick Kurshev <nickols_k@mail.ru>
23  */
24
25 #include "libavutil/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 "libavcodec/videodsp.h"
33 #include "dsputil_mmx.h"
34 #include "idct_xvid.h"
35 #include "diracdsp_mmx.h"
36
37 //#undef NDEBUG
38 //#include <assert.h>
39
40 /* pixel operations */
41 DECLARE_ALIGNED(8,  const uint64_t, ff_bone) = 0x0101010101010101ULL;
42 DECLARE_ALIGNED(8,  const uint64_t, ff_wtwo) = 0x0002000200020002ULL;
43
44 DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_1)    = { 0x0001000100010001ULL, 0x0001000100010001ULL };
45 DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_2)    = { 0x0002000200020002ULL, 0x0002000200020002ULL };
46 DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_3)    = { 0x0003000300030003ULL, 0x0003000300030003ULL };
47 DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_4)    = { 0x0004000400040004ULL, 0x0004000400040004ULL };
48 DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_5)    = { 0x0005000500050005ULL, 0x0005000500050005ULL };
49 DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_8)    = { 0x0008000800080008ULL, 0x0008000800080008ULL };
50 DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_9)    = { 0x0009000900090009ULL, 0x0009000900090009ULL };
51 DECLARE_ALIGNED(8,  const uint64_t, ff_pw_15)   =   0x000F000F000F000FULL;
52 DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_16)   = { 0x0010001000100010ULL, 0x0010001000100010ULL };
53 DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_17)   = { 0x0011001100110011ULL, 0x0011001100110011ULL };
54 DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_18)   = { 0x0012001200120012ULL, 0x0012001200120012ULL };
55 DECLARE_ALIGNED(8,  const uint64_t, ff_pw_20)   =   0x0014001400140014ULL;
56 DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_27)   = { 0x001B001B001B001BULL, 0x001B001B001B001BULL };
57 DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_28)   = { 0x001C001C001C001CULL, 0x001C001C001C001CULL };
58 DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_32)   = { 0x0020002000200020ULL, 0x0020002000200020ULL };
59 DECLARE_ALIGNED(8,  const uint64_t, ff_pw_42)   =   0x002A002A002A002AULL;
60 DECLARE_ALIGNED(8,  const uint64_t, ff_pw_53)   =   0x0035003500350035ULL;
61 DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_63)   = { 0x003F003F003F003FULL, 0x003F003F003F003FULL };
62 DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_64)   = { 0x0040004000400040ULL, 0x0040004000400040ULL };
63 DECLARE_ALIGNED(8,  const uint64_t, ff_pw_96)   =   0x0060006000600060ULL;
64 DECLARE_ALIGNED(8,  const uint64_t, ff_pw_128)  =   0x0080008000800080ULL;
65 DECLARE_ALIGNED(8,  const uint64_t, ff_pw_255)  =   0x00ff00ff00ff00ffULL;
66 DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_512)  = { 0x0200020002000200ULL, 0x0200020002000200ULL };
67 DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_1019) = { 0x03FB03FB03FB03FBULL, 0x03FB03FB03FB03FBULL };
68
69 DECLARE_ALIGNED(16, const xmm_reg,  ff_pb_0)    = { 0x0000000000000000ULL, 0x0000000000000000ULL };
70 DECLARE_ALIGNED(16, const xmm_reg,  ff_pb_1)    = { 0x0101010101010101ULL, 0x0101010101010101ULL };
71 DECLARE_ALIGNED(16, const xmm_reg,  ff_pb_3)    = { 0x0303030303030303ULL, 0x0303030303030303ULL };
72 DECLARE_ALIGNED(16, const xmm_reg,  ff_pb_4)    = { 0x0404040404040404ULL, 0x0404040404040404ULL };
73 DECLARE_ALIGNED(8,  const uint64_t, ff_pb_7)    =   0x0707070707070707ULL;
74 DECLARE_ALIGNED(8,  const uint64_t, ff_pb_1F)   =   0x1F1F1F1F1F1F1F1FULL;
75 DECLARE_ALIGNED(8,  const uint64_t, ff_pb_3F)   =   0x3F3F3F3F3F3F3F3FULL;
76 DECLARE_ALIGNED(16, const xmm_reg,  ff_pb_80)   = { 0x8080808080808080ULL, 0x8080808080808080ULL };
77 DECLARE_ALIGNED(8,  const uint64_t, ff_pb_81)   =   0x8181818181818181ULL;
78 DECLARE_ALIGNED(16, const xmm_reg,  ff_pb_A1)   = { 0xA1A1A1A1A1A1A1A1ULL, 0xA1A1A1A1A1A1A1A1ULL };
79 DECLARE_ALIGNED(16, const xmm_reg,  ff_pb_F8)   = { 0xF8F8F8F8F8F8F8F8ULL, 0xF8F8F8F8F8F8F8F8ULL };
80 DECLARE_ALIGNED(8,  const uint64_t, ff_pb_FC)   =   0xFCFCFCFCFCFCFCFCULL;
81 DECLARE_ALIGNED(16, const xmm_reg,  ff_pb_FE)   = { 0xFEFEFEFEFEFEFEFEULL, 0xFEFEFEFEFEFEFEFEULL };
82
83 DECLARE_ALIGNED(16, const double, ff_pd_1)[2] = { 1.0, 1.0 };
84 DECLARE_ALIGNED(16, const double, ff_pd_2)[2] = { 2.0, 2.0 };
85
86
87 #if HAVE_YASM
88 void ff_put_pixels8_x2_mmxext(uint8_t *block, const uint8_t *pixels,
89                               ptrdiff_t line_size, int h);
90 void ff_put_pixels8_x2_3dnow(uint8_t *block, const uint8_t *pixels,
91                              ptrdiff_t line_size, int h);
92 void ff_put_pixels8_l2_mmxext(uint8_t *dst, uint8_t *src1, uint8_t *src2,
93                               int dstStride, int src1Stride, int h);
94 void ff_put_no_rnd_pixels8_l2_mmxext(uint8_t *dst, uint8_t *src1,
95                                      uint8_t *src2, int dstStride,
96                                      int src1Stride, int h);
97 void ff_avg_pixels8_l2_mmxext(uint8_t *dst, uint8_t *src1, uint8_t *src2,
98                               int dstStride, int src1Stride, int h);
99 void ff_put_pixels16_x2_mmxext(uint8_t *block, const uint8_t *pixels,
100                                ptrdiff_t line_size, int h);
101 void ff_put_pixels16_x2_3dnow(uint8_t *block, const uint8_t *pixels,
102                               ptrdiff_t line_size, int h);
103 void ff_put_pixels16_l2_mmxext(uint8_t *dst, uint8_t *src1, uint8_t *src2,
104                                int dstStride, int src1Stride, int h);
105 void ff_avg_pixels16_l2_mmxext(uint8_t *dst, uint8_t *src1, uint8_t *src2,
106                                int dstStride, int src1Stride, int h);
107 void ff_put_no_rnd_pixels16_l2_mmxext(uint8_t *dst, uint8_t *src1, uint8_t *src2,
108                                       int dstStride, int src1Stride, int h);
109 void ff_put_no_rnd_pixels8_x2_mmxext(uint8_t *block, const uint8_t *pixels,
110                                      ptrdiff_t line_size, int h);
111 void ff_put_no_rnd_pixels8_x2_3dnow(uint8_t *block, const uint8_t *pixels,
112                                     ptrdiff_t line_size, int h);
113 void ff_put_no_rnd_pixels8_x2_exact_mmxext(uint8_t *block,
114                                            const uint8_t *pixels,
115                                            ptrdiff_t line_size, int h);
116 void ff_put_no_rnd_pixels8_x2_exact_3dnow(uint8_t *block,
117                                           const uint8_t *pixels,
118                                           ptrdiff_t line_size, int h);
119 void ff_put_pixels8_y2_mmxext(uint8_t *block, const uint8_t *pixels,
120                               ptrdiff_t line_size, int h);
121 void ff_put_pixels8_y2_3dnow(uint8_t *block, const uint8_t *pixels,
122                              ptrdiff_t line_size, int h);
123 void ff_put_no_rnd_pixels8_y2_mmxext(uint8_t *block, const uint8_t *pixels,
124                                      ptrdiff_t line_size, int h);
125 void ff_put_no_rnd_pixels8_y2_3dnow(uint8_t *block, const uint8_t *pixels,
126                                     ptrdiff_t line_size, int h);
127 void ff_put_no_rnd_pixels8_y2_exact_mmxext(uint8_t *block,
128                                            const uint8_t *pixels,
129                                            ptrdiff_t line_size, int h);
130 void ff_put_no_rnd_pixels8_y2_exact_3dnow(uint8_t *block,
131                                           const uint8_t *pixels,
132                                           ptrdiff_t line_size, int h);
133 void ff_avg_pixels8_3dnow(uint8_t *block, const uint8_t *pixels,
134                           ptrdiff_t line_size, int h);
135 void ff_avg_pixels8_x2_mmxext(uint8_t *block, const uint8_t *pixels,
136                               ptrdiff_t line_size, int h);
137 void ff_avg_pixels8_x2_3dnow(uint8_t *block, const uint8_t *pixels,
138                              ptrdiff_t line_size, int h);
139 void ff_avg_pixels8_y2_mmxext(uint8_t *block, const uint8_t *pixels,
140                               ptrdiff_t line_size, int h);
141 void ff_avg_pixels8_y2_3dnow(uint8_t *block, const uint8_t *pixels,
142                              ptrdiff_t line_size, int h);
143 void ff_avg_pixels8_xy2_mmxext(uint8_t *block, const uint8_t *pixels,
144                                ptrdiff_t line_size, int h);
145 void ff_avg_pixels8_xy2_3dnow(uint8_t *block, const uint8_t *pixels,
146                               ptrdiff_t line_size, int h);
147
148 void ff_put_pixels8_mmxext(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h);
149 static void ff_put_pixels16_mmxext(uint8_t *block, const uint8_t *pixels,
150                                    ptrdiff_t line_size, int h)
151 {
152     ff_put_pixels8_mmxext(block,     pixels,     line_size, h);
153     ff_put_pixels8_mmxext(block + 8, pixels + 8, line_size, h);
154 }
155
156 void ff_put_mpeg4_qpel16_h_lowpass_mmxext(uint8_t *dst, uint8_t *src,
157                                          int dstStride, int srcStride, int h);
158 void ff_avg_mpeg4_qpel16_h_lowpass_mmxext(uint8_t *dst, uint8_t *src,
159                                          int dstStride, int srcStride, int h);
160 void ff_put_no_rnd_mpeg4_qpel16_h_lowpass_mmxext(uint8_t *dst, uint8_t *src,
161                                                  int dstStride, int srcStride,
162                                                  int h);
163 void ff_put_mpeg4_qpel8_h_lowpass_mmxext(uint8_t *dst, uint8_t *src,
164                                         int dstStride, int srcStride, int h);
165 void ff_avg_mpeg4_qpel8_h_lowpass_mmxext(uint8_t *dst, uint8_t *src,
166                                         int dstStride, int srcStride, int h);
167 void ff_put_no_rnd_mpeg4_qpel8_h_lowpass_mmxext(uint8_t *dst, uint8_t *src,
168                                                 int dstStride, int srcStride,
169                                                 int h);
170 void ff_put_mpeg4_qpel16_v_lowpass_mmxext(uint8_t *dst, uint8_t *src,
171                                          int dstStride, int srcStride);
172 void ff_avg_mpeg4_qpel16_v_lowpass_mmxext(uint8_t *dst, uint8_t *src,
173                                          int dstStride, int srcStride);
174 void ff_put_no_rnd_mpeg4_qpel16_v_lowpass_mmxext(uint8_t *dst, uint8_t *src,
175                                                  int dstStride, int srcStride);
176 void ff_put_mpeg4_qpel8_v_lowpass_mmxext(uint8_t *dst, uint8_t *src,
177                                         int dstStride, int srcStride);
178 void ff_avg_mpeg4_qpel8_v_lowpass_mmxext(uint8_t *dst, uint8_t *src,
179                                         int dstStride, int srcStride);
180 void ff_put_no_rnd_mpeg4_qpel8_v_lowpass_mmxext(uint8_t *dst, uint8_t *src,
181                                                 int dstStride, int srcStride);
182 #define ff_put_no_rnd_pixels16_mmxext ff_put_pixels16_mmxext
183 #define ff_put_no_rnd_pixels8_mmxext ff_put_pixels8_mmxext
184 #endif /* HAVE_YASM */
185
186
187 #if HAVE_INLINE_ASM
188
189 #define JUMPALIGN()     __asm__ volatile (".p2align 3"::)
190 #define MOVQ_ZERO(regd) __asm__ volatile ("pxor %%"#regd", %%"#regd ::)
191
192 #define MOVQ_BFE(regd)                                  \
193     __asm__ volatile (                                  \
194         "pcmpeqd %%"#regd", %%"#regd"   \n\t"           \
195         "paddb   %%"#regd", %%"#regd"   \n\t" ::)
196
197 #ifndef PIC
198 #define MOVQ_BONE(regd) __asm__ volatile ("movq %0, %%"#regd" \n\t" :: "m"(ff_bone))
199 #define MOVQ_WTWO(regd) __asm__ volatile ("movq %0, %%"#regd" \n\t" :: "m"(ff_wtwo))
200 #else
201 // for shared library it's better to use this way for accessing constants
202 // pcmpeqd -> -1
203 #define MOVQ_BONE(regd)                                 \
204     __asm__ volatile (                                  \
205         "pcmpeqd  %%"#regd", %%"#regd"  \n\t"           \
206         "psrlw          $15, %%"#regd"  \n\t"           \
207         "packuswb %%"#regd", %%"#regd"  \n\t" ::)
208
209 #define MOVQ_WTWO(regd)                                 \
210     __asm__ volatile (                                  \
211         "pcmpeqd %%"#regd", %%"#regd"   \n\t"           \
212         "psrlw         $15, %%"#regd"   \n\t"           \
213         "psllw          $1, %%"#regd"   \n\t"::)
214
215 #endif
216
217 // using regr as temporary and for the output result
218 // first argument is unmodifed and second is trashed
219 // regfe is supposed to contain 0xfefefefefefefefe
220 #define PAVGB_MMX_NO_RND(rega, regb, regr, regfe)                \
221     "movq   "#rega", "#regr"            \n\t"                    \
222     "pand   "#regb", "#regr"            \n\t"                    \
223     "pxor   "#rega", "#regb"            \n\t"                    \
224     "pand  "#regfe", "#regb"            \n\t"                    \
225     "psrlq       $1, "#regb"            \n\t"                    \
226     "paddb  "#regb", "#regr"            \n\t"
227
228 #define PAVGB_MMX(rega, regb, regr, regfe)                       \
229     "movq   "#rega", "#regr"            \n\t"                    \
230     "por    "#regb", "#regr"            \n\t"                    \
231     "pxor   "#rega", "#regb"            \n\t"                    \
232     "pand  "#regfe", "#regb"            \n\t"                    \
233     "psrlq       $1, "#regb"            \n\t"                    \
234     "psubb  "#regb", "#regr"            \n\t"
235
236 // mm6 is supposed to contain 0xfefefefefefefefe
237 #define PAVGBP_MMX_NO_RND(rega, regb, regr,  regc, regd, regp)   \
238     "movq  "#rega", "#regr"             \n\t"                    \
239     "movq  "#regc", "#regp"             \n\t"                    \
240     "pand  "#regb", "#regr"             \n\t"                    \
241     "pand  "#regd", "#regp"             \n\t"                    \
242     "pxor  "#rega", "#regb"             \n\t"                    \
243     "pxor  "#regc", "#regd"             \n\t"                    \
244     "pand    %%mm6, "#regb"             \n\t"                    \
245     "pand    %%mm6, "#regd"             \n\t"                    \
246     "psrlq      $1, "#regb"             \n\t"                    \
247     "psrlq      $1, "#regd"             \n\t"                    \
248     "paddb "#regb", "#regr"             \n\t"                    \
249     "paddb "#regd", "#regp"             \n\t"
250
251 #define PAVGBP_MMX(rega, regb, regr, regc, regd, regp)           \
252     "movq  "#rega", "#regr"             \n\t"                    \
253     "movq  "#regc", "#regp"             \n\t"                    \
254     "por   "#regb", "#regr"             \n\t"                    \
255     "por   "#regd", "#regp"             \n\t"                    \
256     "pxor  "#rega", "#regb"             \n\t"                    \
257     "pxor  "#regc", "#regd"             \n\t"                    \
258     "pand    %%mm6, "#regb"             \n\t"                    \
259     "pand    %%mm6, "#regd"             \n\t"                    \
260     "psrlq      $1, "#regd"             \n\t"                    \
261     "psrlq      $1, "#regb"             \n\t"                    \
262     "psubb "#regb", "#regr"             \n\t"                    \
263     "psubb "#regd", "#regp"             \n\t"
264
265 /***********************************/
266 /* MMX no rounding */
267 #define NO_RND 1
268 #define DEF(x, y) x ## _no_rnd_ ## y ## _mmx
269 #define SET_RND  MOVQ_WONE
270 #define PAVGBP(a, b, c, d, e, f)        PAVGBP_MMX_NO_RND(a, b, c, d, e, f)
271 #define PAVGB(a, b, c, e)               PAVGB_MMX_NO_RND(a, b, c, e)
272 #define OP_AVG(a, b, c, e)              PAVGB_MMX(a, b, c, e)
273
274 #include "dsputil_rnd_template.c"
275
276 #undef DEF
277 #undef SET_RND
278 #undef PAVGBP
279 #undef PAVGB
280 #undef NO_RND
281 /***********************************/
282 /* MMX rounding */
283
284 #define DEF(x, y) x ## _ ## y ## _mmx
285 #define SET_RND  MOVQ_WTWO
286 #define PAVGBP(a, b, c, d, e, f)        PAVGBP_MMX(a, b, c, d, e, f)
287 #define PAVGB(a, b, c, e)               PAVGB_MMX(a, b, c, e)
288
289 #include "dsputil_rnd_template.c"
290
291 #undef DEF
292 #undef SET_RND
293 #undef PAVGBP
294 #undef PAVGB
295 #undef OP_AVG
296
297 #endif /* HAVE_INLINE_ASM */
298
299
300 #if HAVE_YASM
301 #define ff_put_pixels8_mmx ff_put_pixels8_mmxext
302
303 /***********************************/
304 /* 3Dnow specific */
305
306 #define DEF(x) x ## _3dnow
307
308 #include "dsputil_avg_template.c"
309
310 #undef DEF
311
312 /***********************************/
313 /* MMXEXT specific */
314
315 #define DEF(x) x ## _mmxext
316
317 #include "dsputil_avg_template.c"
318
319 #undef DEF
320
321 #endif /* HAVE_YASM */
322
323
324 #if HAVE_INLINE_ASM
325 #define put_no_rnd_pixels16_mmx put_pixels16_mmx
326 #define put_no_rnd_pixels8_mmx put_pixels8_mmx
327 #define put_pixels16_mmxext put_pixels16_mmx
328 #define put_pixels8_mmxext put_pixels8_mmx
329 #define put_pixels4_mmxext put_pixels4_mmx
330 #define put_no_rnd_pixels16_mmxext put_no_rnd_pixels16_mmx
331 #define put_no_rnd_pixels8_mmxext put_no_rnd_pixels8_mmx
332
333 /***********************************/
334 /* standard MMX */
335
336 void ff_put_pixels_clamped_mmx(const int16_t *block, uint8_t *pixels,
337                                int line_size)
338 {
339     const int16_t *p;
340     uint8_t *pix;
341
342     /* read the pixels */
343     p   = block;
344     pix = pixels;
345     /* unrolled loop */
346     __asm__ volatile (
347         "movq      (%3), %%mm0          \n\t"
348         "movq     8(%3), %%mm1          \n\t"
349         "movq    16(%3), %%mm2          \n\t"
350         "movq    24(%3), %%mm3          \n\t"
351         "movq    32(%3), %%mm4          \n\t"
352         "movq    40(%3), %%mm5          \n\t"
353         "movq    48(%3), %%mm6          \n\t"
354         "movq    56(%3), %%mm7          \n\t"
355         "packuswb %%mm1, %%mm0          \n\t"
356         "packuswb %%mm3, %%mm2          \n\t"
357         "packuswb %%mm5, %%mm4          \n\t"
358         "packuswb %%mm7, %%mm6          \n\t"
359         "movq     %%mm0, (%0)           \n\t"
360         "movq     %%mm2, (%0, %1)       \n\t"
361         "movq     %%mm4, (%0, %1, 2)    \n\t"
362         "movq     %%mm6, (%0, %2)       \n\t"
363         :: "r"(pix), "r"((x86_reg)line_size), "r"((x86_reg)line_size * 3),
364            "r"(p)
365         : "memory");
366     pix += line_size * 4;
367     p   += 32;
368
369     // if here would be an exact copy of the code above
370     // compiler would generate some very strange code
371     // thus using "r"
372     __asm__ volatile (
373         "movq       (%3), %%mm0         \n\t"
374         "movq      8(%3), %%mm1         \n\t"
375         "movq     16(%3), %%mm2         \n\t"
376         "movq     24(%3), %%mm3         \n\t"
377         "movq     32(%3), %%mm4         \n\t"
378         "movq     40(%3), %%mm5         \n\t"
379         "movq     48(%3), %%mm6         \n\t"
380         "movq     56(%3), %%mm7         \n\t"
381         "packuswb  %%mm1, %%mm0         \n\t"
382         "packuswb  %%mm3, %%mm2         \n\t"
383         "packuswb  %%mm5, %%mm4         \n\t"
384         "packuswb  %%mm7, %%mm6         \n\t"
385         "movq      %%mm0, (%0)          \n\t"
386         "movq      %%mm2, (%0, %1)      \n\t"
387         "movq      %%mm4, (%0, %1, 2)   \n\t"
388         "movq      %%mm6, (%0, %2)      \n\t"
389         :: "r"(pix), "r"((x86_reg)line_size), "r"((x86_reg)line_size * 3), "r"(p)
390         : "memory");
391 }
392
393 #define put_signed_pixels_clamped_mmx_half(off)             \
394     "movq          "#off"(%2), %%mm1        \n\t"           \
395     "movq     16 + "#off"(%2), %%mm2        \n\t"           \
396     "movq     32 + "#off"(%2), %%mm3        \n\t"           \
397     "movq     48 + "#off"(%2), %%mm4        \n\t"           \
398     "packsswb  8 + "#off"(%2), %%mm1        \n\t"           \
399     "packsswb 24 + "#off"(%2), %%mm2        \n\t"           \
400     "packsswb 40 + "#off"(%2), %%mm3        \n\t"           \
401     "packsswb 56 + "#off"(%2), %%mm4        \n\t"           \
402     "paddb              %%mm0, %%mm1        \n\t"           \
403     "paddb              %%mm0, %%mm2        \n\t"           \
404     "paddb              %%mm0, %%mm3        \n\t"           \
405     "paddb              %%mm0, %%mm4        \n\t"           \
406     "movq               %%mm1, (%0)         \n\t"           \
407     "movq               %%mm2, (%0, %3)     \n\t"           \
408     "movq               %%mm3, (%0, %3, 2)  \n\t"           \
409     "movq               %%mm4, (%0, %1)     \n\t"
410
411 void ff_put_signed_pixels_clamped_mmx(const int16_t *block, uint8_t *pixels,
412                                       int line_size)
413 {
414     x86_reg line_skip = line_size;
415     x86_reg line_skip3;
416
417     __asm__ volatile (
418         "movq "MANGLE(ff_pb_80)", %%mm0     \n\t"
419         "lea         (%3, %3, 2), %1        \n\t"
420         put_signed_pixels_clamped_mmx_half(0)
421         "lea         (%0, %3, 4), %0        \n\t"
422         put_signed_pixels_clamped_mmx_half(64)
423         : "+&r"(pixels), "=&r"(line_skip3)
424         : "r"(block), "r"(line_skip)
425         : "memory");
426 }
427
428 void ff_add_pixels_clamped_mmx(const int16_t *block, uint8_t *pixels,
429                                int line_size)
430 {
431     const int16_t *p;
432     uint8_t *pix;
433     int i;
434
435     /* read the pixels */
436     p   = block;
437     pix = pixels;
438     MOVQ_ZERO(mm7);
439     i = 4;
440     do {
441         __asm__ volatile (
442             "movq        (%2), %%mm0    \n\t"
443             "movq       8(%2), %%mm1    \n\t"
444             "movq      16(%2), %%mm2    \n\t"
445             "movq      24(%2), %%mm3    \n\t"
446             "movq          %0, %%mm4    \n\t"
447             "movq          %1, %%mm6    \n\t"
448             "movq       %%mm4, %%mm5    \n\t"
449             "punpcklbw  %%mm7, %%mm4    \n\t"
450             "punpckhbw  %%mm7, %%mm5    \n\t"
451             "paddsw     %%mm4, %%mm0    \n\t"
452             "paddsw     %%mm5, %%mm1    \n\t"
453             "movq       %%mm6, %%mm5    \n\t"
454             "punpcklbw  %%mm7, %%mm6    \n\t"
455             "punpckhbw  %%mm7, %%mm5    \n\t"
456             "paddsw     %%mm6, %%mm2    \n\t"
457             "paddsw     %%mm5, %%mm3    \n\t"
458             "packuswb   %%mm1, %%mm0    \n\t"
459             "packuswb   %%mm3, %%mm2    \n\t"
460             "movq       %%mm0, %0       \n\t"
461             "movq       %%mm2, %1       \n\t"
462             : "+m"(*pix), "+m"(*(pix + line_size))
463             : "r"(p)
464             : "memory");
465         pix += line_size * 2;
466         p   += 16;
467     } while (--i);
468 }
469
470 static void put_pixels8_mmx(uint8_t *block, const uint8_t *pixels,
471                             ptrdiff_t line_size, int h)
472 {
473     __asm__ volatile (
474         "lea   (%3, %3), %%"REG_a"      \n\t"
475         ".p2align     3                 \n\t"
476         "1:                             \n\t"
477         "movq  (%1    ), %%mm0          \n\t"
478         "movq  (%1, %3), %%mm1          \n\t"
479         "movq     %%mm0, (%2)           \n\t"
480         "movq     %%mm1, (%2, %3)       \n\t"
481         "add  %%"REG_a", %1             \n\t"
482         "add  %%"REG_a", %2             \n\t"
483         "movq  (%1    ), %%mm0          \n\t"
484         "movq  (%1, %3), %%mm1          \n\t"
485         "movq     %%mm0, (%2)           \n\t"
486         "movq     %%mm1, (%2, %3)       \n\t"
487         "add  %%"REG_a", %1             \n\t"
488         "add  %%"REG_a", %2             \n\t"
489         "subl        $4, %0             \n\t"
490         "jnz         1b                 \n\t"
491         : "+g"(h), "+r"(pixels),  "+r"(block)
492         : "r"((x86_reg)line_size)
493         : "%"REG_a, "memory"
494         );
495 }
496
497 static void put_pixels16_mmx(uint8_t *block, const uint8_t *pixels,
498                              ptrdiff_t line_size, int h)
499 {
500     __asm__ volatile (
501         "lea   (%3, %3), %%"REG_a"      \n\t"
502         ".p2align     3                 \n\t"
503         "1:                             \n\t"
504         "movq  (%1    ), %%mm0          \n\t"
505         "movq 8(%1    ), %%mm4          \n\t"
506         "movq  (%1, %3), %%mm1          \n\t"
507         "movq 8(%1, %3), %%mm5          \n\t"
508         "movq     %%mm0,  (%2)          \n\t"
509         "movq     %%mm4, 8(%2)          \n\t"
510         "movq     %%mm1,  (%2, %3)      \n\t"
511         "movq     %%mm5, 8(%2, %3)      \n\t"
512         "add  %%"REG_a", %1             \n\t"
513         "add  %%"REG_a", %2             \n\t"
514         "movq  (%1    ), %%mm0          \n\t"
515         "movq 8(%1    ), %%mm4          \n\t"
516         "movq  (%1, %3), %%mm1          \n\t"
517         "movq 8(%1, %3), %%mm5          \n\t"
518         "movq     %%mm0,  (%2)          \n\t"
519         "movq     %%mm4, 8(%2)          \n\t"
520         "movq     %%mm1,  (%2, %3)      \n\t"
521         "movq     %%mm5, 8(%2, %3)      \n\t"
522         "add  %%"REG_a", %1             \n\t"
523         "add  %%"REG_a", %2             \n\t"
524         "subl        $4, %0             \n\t"
525         "jnz         1b                 \n\t"
526         : "+g"(h), "+r"(pixels),  "+r"(block)
527         : "r"((x86_reg)line_size)
528         : "%"REG_a, "memory"
529         );
530 }
531
532 #define CLEAR_BLOCKS(name, n)                           \
533 static void name(int16_t *blocks)                       \
534 {                                                       \
535     __asm__ volatile (                                  \
536         "pxor %%mm7, %%mm7              \n\t"           \
537         "mov     %1,        %%"REG_a"   \n\t"           \
538         "1:                             \n\t"           \
539         "movq %%mm7,   (%0, %%"REG_a")  \n\t"           \
540         "movq %%mm7,  8(%0, %%"REG_a")  \n\t"           \
541         "movq %%mm7, 16(%0, %%"REG_a")  \n\t"           \
542         "movq %%mm7, 24(%0, %%"REG_a")  \n\t"           \
543         "add    $32, %%"REG_a"          \n\t"           \
544         "js      1b                     \n\t"           \
545         :: "r"(((uint8_t *)blocks) + 128 * n),          \
546            "i"(-128 * n)                                \
547         : "%"REG_a                                      \
548         );                                              \
549 }
550 CLEAR_BLOCKS(clear_blocks_mmx, 6)
551 CLEAR_BLOCKS(clear_block_mmx, 1)
552
553 static void clear_block_sse(int16_t *block)
554 {
555     __asm__ volatile (
556         "xorps  %%xmm0, %%xmm0          \n"
557         "movaps %%xmm0,    (%0)         \n"
558         "movaps %%xmm0,  16(%0)         \n"
559         "movaps %%xmm0,  32(%0)         \n"
560         "movaps %%xmm0,  48(%0)         \n"
561         "movaps %%xmm0,  64(%0)         \n"
562         "movaps %%xmm0,  80(%0)         \n"
563         "movaps %%xmm0,  96(%0)         \n"
564         "movaps %%xmm0, 112(%0)         \n"
565         :: "r"(block)
566         : "memory"
567     );
568 }
569
570 static void clear_blocks_sse(int16_t *blocks)
571 {
572     __asm__ volatile (
573         "xorps  %%xmm0, %%xmm0              \n"
574         "mov        %1,         %%"REG_a"   \n"
575         "1:                                 \n"
576         "movaps %%xmm0,    (%0, %%"REG_a")  \n"
577         "movaps %%xmm0,  16(%0, %%"REG_a")  \n"
578         "movaps %%xmm0,  32(%0, %%"REG_a")  \n"
579         "movaps %%xmm0,  48(%0, %%"REG_a")  \n"
580         "movaps %%xmm0,  64(%0, %%"REG_a")  \n"
581         "movaps %%xmm0,  80(%0, %%"REG_a")  \n"
582         "movaps %%xmm0,  96(%0, %%"REG_a")  \n"
583         "movaps %%xmm0, 112(%0, %%"REG_a")  \n"
584         "add      $128,         %%"REG_a"   \n"
585         "js         1b                      \n"
586         :: "r"(((uint8_t *)blocks) + 128 * 6),
587            "i"(-128 * 6)
588         : "%"REG_a
589     );
590 }
591
592 static void add_bytes_mmx(uint8_t *dst, uint8_t *src, int w)
593 {
594     x86_reg i = 0;
595     __asm__ volatile (
596         "jmp          2f                \n\t"
597         "1:                             \n\t"
598         "movq   (%1, %0), %%mm0         \n\t"
599         "movq   (%2, %0), %%mm1         \n\t"
600         "paddb     %%mm0, %%mm1         \n\t"
601         "movq      %%mm1, (%2, %0)      \n\t"
602         "movq  8(%1, %0), %%mm0         \n\t"
603         "movq  8(%2, %0), %%mm1         \n\t"
604         "paddb     %%mm0, %%mm1         \n\t"
605         "movq      %%mm1, 8(%2, %0)     \n\t"
606         "add         $16, %0            \n\t"
607         "2:                             \n\t"
608         "cmp          %3, %0            \n\t"
609         "js           1b                \n\t"
610         : "+r"(i)
611         : "r"(src), "r"(dst), "r"((x86_reg)w - 15)
612     );
613     for ( ; i < w; i++)
614         dst[i + 0] += src[i + 0];
615 }
616
617 #if HAVE_7REGS
618 static void add_hfyu_median_prediction_cmov(uint8_t *dst, const uint8_t *top,
619                                             const uint8_t *diff, int w,
620                                             int *left, int *left_top)
621 {
622     x86_reg w2 = -w;
623     x86_reg x;
624     int l  = *left     & 0xff;
625     int tl = *left_top & 0xff;
626     int t;
627     __asm__ volatile (
628         "mov          %7, %3            \n"
629         "1:                             \n"
630         "movzbl (%3, %4), %2            \n"
631         "mov          %2, %k3           \n"
632         "sub         %b1, %b3           \n"
633         "add         %b0, %b3           \n"
634         "mov          %2, %1            \n"
635         "cmp          %0, %2            \n"
636         "cmovg        %0, %2            \n"
637         "cmovg        %1, %0            \n"
638         "cmp         %k3, %0            \n"
639         "cmovg       %k3, %0            \n"
640         "mov          %7, %3            \n"
641         "cmp          %2, %0            \n"
642         "cmovl        %2, %0            \n"
643         "add    (%6, %4), %b0           \n"
644         "mov         %b0, (%5, %4)      \n"
645         "inc          %4                \n"
646         "jl           1b                \n"
647         : "+&q"(l), "+&q"(tl), "=&r"(t), "=&q"(x), "+&r"(w2)
648         : "r"(dst + w), "r"(diff + w), "rm"(top + w)
649     );
650     *left     = l;
651     *left_top = tl;
652 }
653 #endif
654 #endif /* HAVE_INLINE_ASM */
655
656 void ff_h263_v_loop_filter_mmx(uint8_t *src, int stride, int qscale);
657 void ff_h263_h_loop_filter_mmx(uint8_t *src, int stride, int qscale);
658
659 #if HAVE_INLINE_ASM
660 /* Draw the edges of width 'w' of an image of size width, height
661  * this MMX version can only handle w == 8 || w == 16. */
662 static void draw_edges_mmx(uint8_t *buf, int wrap, int width, int height,
663                            int w, int h, int sides)
664 {
665     uint8_t *ptr, *last_line;
666     int i;
667
668     last_line = buf + (height - 1) * wrap;
669     /* left and right */
670     ptr = buf;
671     if (w == 8) {
672         __asm__ volatile (
673             "1:                             \n\t"
674             "movd            (%0), %%mm0    \n\t"
675             "punpcklbw      %%mm0, %%mm0    \n\t"
676             "punpcklwd      %%mm0, %%mm0    \n\t"
677             "punpckldq      %%mm0, %%mm0    \n\t"
678             "movq           %%mm0, -8(%0)   \n\t"
679             "movq      -8(%0, %2), %%mm1    \n\t"
680             "punpckhbw      %%mm1, %%mm1    \n\t"
681             "punpckhwd      %%mm1, %%mm1    \n\t"
682             "punpckhdq      %%mm1, %%mm1    \n\t"
683             "movq           %%mm1, (%0, %2) \n\t"
684             "add               %1, %0       \n\t"
685             "cmp               %3, %0       \n\t"
686             "jb                1b           \n\t"
687             : "+r"(ptr)
688             : "r"((x86_reg)wrap), "r"((x86_reg)width), "r"(ptr + wrap * height)
689             );
690     } else if(w==16){
691         __asm__ volatile (
692             "1:                                 \n\t"
693             "movd            (%0), %%mm0        \n\t"
694             "punpcklbw      %%mm0, %%mm0        \n\t"
695             "punpcklwd      %%mm0, %%mm0        \n\t"
696             "punpckldq      %%mm0, %%mm0        \n\t"
697             "movq           %%mm0, -8(%0)       \n\t"
698             "movq           %%mm0, -16(%0)      \n\t"
699             "movq      -8(%0, %2), %%mm1        \n\t"
700             "punpckhbw      %%mm1, %%mm1        \n\t"
701             "punpckhwd      %%mm1, %%mm1        \n\t"
702             "punpckhdq      %%mm1, %%mm1        \n\t"
703             "movq           %%mm1,  (%0, %2)    \n\t"
704             "movq           %%mm1, 8(%0, %2)    \n\t"
705             "add               %1, %0           \n\t"
706             "cmp               %3, %0           \n\t"
707             "jb                1b               \n\t"
708             : "+r"(ptr)
709             : "r"((x86_reg)wrap), "r"((x86_reg)width), "r"(ptr + wrap * height)
710             );
711     } else {
712         av_assert1(w == 4);
713         __asm__ volatile (
714             "1:                             \n\t"
715             "movd            (%0), %%mm0    \n\t"
716             "punpcklbw      %%mm0, %%mm0    \n\t"
717             "punpcklwd      %%mm0, %%mm0    \n\t"
718             "movd           %%mm0, -4(%0)   \n\t"
719             "movd      -4(%0, %2), %%mm1    \n\t"
720             "punpcklbw      %%mm1, %%mm1    \n\t"
721             "punpckhwd      %%mm1, %%mm1    \n\t"
722             "punpckhdq      %%mm1, %%mm1    \n\t"
723             "movd           %%mm1, (%0, %2) \n\t"
724             "add               %1, %0       \n\t"
725             "cmp               %3, %0       \n\t"
726             "jb                1b           \n\t"
727             : "+r"(ptr)
728             : "r"((x86_reg)wrap), "r"((x86_reg)width), "r"(ptr + wrap * height)
729             );
730     }
731
732     /* top and bottom (and hopefully also the corners) */
733     if (sides & EDGE_TOP) {
734         for (i = 0; i < h; i += 4) {
735             ptr = buf - (i + 1) * wrap - w;
736             __asm__ volatile (
737                 "1:                             \n\t"
738                 "movq (%1, %0), %%mm0           \n\t"
739                 "movq    %%mm0, (%0)            \n\t"
740                 "movq    %%mm0, (%0, %2)        \n\t"
741                 "movq    %%mm0, (%0, %2, 2)     \n\t"
742                 "movq    %%mm0, (%0, %3)        \n\t"
743                 "add        $8, %0              \n\t"
744                 "cmp        %4, %0              \n\t"
745                 "jb         1b                  \n\t"
746                 : "+r"(ptr)
747                 : "r"((x86_reg)buf - (x86_reg)ptr - w), "r"((x86_reg) -wrap),
748                   "r"((x86_reg) -wrap * 3), "r"(ptr + width + 2 * w)
749                 );
750         }
751     }
752
753     if (sides & EDGE_BOTTOM) {
754         for (i = 0; i < h; i += 4) {
755             ptr = last_line + (i + 1) * wrap - w;
756             __asm__ volatile (
757                 "1:                             \n\t"
758                 "movq (%1, %0), %%mm0           \n\t"
759                 "movq    %%mm0, (%0)            \n\t"
760                 "movq    %%mm0, (%0, %2)        \n\t"
761                 "movq    %%mm0, (%0, %2, 2)     \n\t"
762                 "movq    %%mm0, (%0, %3)        \n\t"
763                 "add        $8, %0              \n\t"
764                 "cmp        %4, %0              \n\t"
765                 "jb         1b                  \n\t"
766                 : "+r"(ptr)
767                 : "r"((x86_reg)last_line - (x86_reg)ptr - w),
768                   "r"((x86_reg)wrap), "r"((x86_reg)wrap * 3),
769                   "r"(ptr + width + 2 * w)
770                 );
771         }
772     }
773 }
774 #endif /* HAVE_INLINE_ASM */
775
776
777 #if HAVE_YASM
778 #define QPEL_OP(OPNAME, ROUNDER, RND, MMX)                              \
779 static void OPNAME ## qpel8_mc00_ ## MMX (uint8_t *dst, uint8_t *src,   \
780                                           int stride)                   \
781 {                                                                       \
782     ff_ ## OPNAME ## pixels8_ ## MMX(dst, src, stride, 8);              \
783 }                                                                       \
784                                                                         \
785 static void OPNAME ## qpel8_mc10_ ## MMX(uint8_t *dst, uint8_t *src,    \
786                                          int stride)                    \
787 {                                                                       \
788     uint64_t temp[8];                                                   \
789     uint8_t * const half = (uint8_t*)temp;                              \
790     ff_put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(half, src, 8,        \
791                                                    stride, 8);          \
792     ff_ ## OPNAME ## pixels8_l2_ ## MMX(dst, src, half,                 \
793                                         stride, stride, 8);             \
794 }                                                                       \
795                                                                         \
796 static void OPNAME ## qpel8_mc20_ ## MMX(uint8_t *dst, uint8_t *src,    \
797                                          int stride)                    \
798 {                                                                       \
799     ff_ ## OPNAME ## mpeg4_qpel8_h_lowpass_ ## MMX(dst, src, stride,    \
800                                                    stride, 8);          \
801 }                                                                       \
802                                                                         \
803 static void OPNAME ## qpel8_mc30_ ## MMX(uint8_t *dst, uint8_t *src,    \
804                                          int stride)                    \
805 {                                                                       \
806     uint64_t temp[8];                                                   \
807     uint8_t * const half = (uint8_t*)temp;                              \
808     ff_put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(half, src, 8,        \
809                                                    stride, 8);          \
810     ff_ ## OPNAME ## pixels8_l2_ ## MMX(dst, src + 1, half, stride,     \
811                                         stride, 8);                     \
812 }                                                                       \
813                                                                         \
814 static void OPNAME ## qpel8_mc01_ ## MMX(uint8_t *dst, uint8_t *src,    \
815                                          int stride)                    \
816 {                                                                       \
817     uint64_t temp[8];                                                   \
818     uint8_t * const half = (uint8_t*)temp;                              \
819     ff_put ## RND ## mpeg4_qpel8_v_lowpass_ ## MMX(half, src,           \
820                                                    8, stride);          \
821     ff_ ## OPNAME ## pixels8_l2_ ## MMX(dst, src, half,                 \
822                                         stride, stride, 8);             \
823 }                                                                       \
824                                                                         \
825 static void OPNAME ## qpel8_mc02_ ## MMX(uint8_t *dst, uint8_t *src,    \
826                                          int stride)                    \
827 {                                                                       \
828     ff_ ## OPNAME ## mpeg4_qpel8_v_lowpass_ ## MMX(dst, src,            \
829                                                    stride, stride);     \
830 }                                                                       \
831                                                                         \
832 static void OPNAME ## qpel8_mc03_ ## MMX(uint8_t *dst, uint8_t *src,    \
833                                          int stride)                    \
834 {                                                                       \
835     uint64_t temp[8];                                                   \
836     uint8_t * const half = (uint8_t*)temp;                              \
837     ff_put ## RND ## mpeg4_qpel8_v_lowpass_ ## MMX(half, src,           \
838                                                    8, stride);          \
839     ff_ ## OPNAME ## pixels8_l2_ ## MMX(dst, src + stride, half, stride,\
840                                         stride, 8);                     \
841 }                                                                       \
842                                                                         \
843 static void OPNAME ## qpel8_mc11_ ## MMX(uint8_t *dst, uint8_t *src,    \
844                                          int stride)                    \
845 {                                                                       \
846     uint64_t half[8 + 9];                                               \
847     uint8_t * const halfH  = ((uint8_t*)half) + 64;                     \
848     uint8_t * const halfHV = ((uint8_t*)half);                          \
849     ff_put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8,       \
850                                                    stride, 9);          \
851     ff_put ## RND ## pixels8_l2_ ## MMX(halfH, src, halfH, 8,           \
852                                         stride, 9);                     \
853     ff_put ## RND ## mpeg4_qpel8_v_lowpass_ ## MMX(halfHV, halfH, 8, 8);\
854     ff_ ## OPNAME ## pixels8_l2_ ## MMX(dst, halfH, halfHV,             \
855                                         stride, 8, 8);                  \
856 }                                                                       \
857                                                                         \
858 static void OPNAME ## qpel8_mc31_ ## MMX(uint8_t *dst, uint8_t *src,    \
859                                          int stride)                    \
860 {                                                                       \
861     uint64_t half[8 + 9];                                               \
862     uint8_t * const halfH  = ((uint8_t*)half) + 64;                     \
863     uint8_t * const halfHV = ((uint8_t*)half);                          \
864     ff_put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8,       \
865                                                    stride, 9);          \
866     ff_put ## RND ## pixels8_l2_ ## MMX(halfH, src + 1, halfH, 8,       \
867                                         stride, 9);                     \
868     ff_put ## RND ## mpeg4_qpel8_v_lowpass_ ## MMX(halfHV, halfH, 8, 8);\
869     ff_ ## OPNAME ## pixels8_l2_ ## MMX(dst, halfH, halfHV,             \
870                                         stride, 8, 8);                  \
871 }                                                                       \
872                                                                         \
873 static void OPNAME ## qpel8_mc13_ ## MMX(uint8_t *dst, uint8_t *src,    \
874                                          int stride)                    \
875 {                                                                       \
876     uint64_t half[8 + 9];                                               \
877     uint8_t * const halfH  = ((uint8_t*)half) + 64;                     \
878     uint8_t * const halfHV = ((uint8_t*)half);                          \
879     ff_put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8,       \
880                                                    stride, 9);          \
881     ff_put ## RND ## pixels8_l2_ ## MMX(halfH, src, halfH, 8,           \
882                                         stride, 9);                     \
883     ff_put ## RND ## mpeg4_qpel8_v_lowpass_ ## MMX(halfHV, halfH, 8, 8);\
884     ff_ ## OPNAME ## pixels8_l2_ ## MMX(dst, halfH + 8, halfHV,         \
885                                         stride, 8, 8);                  \
886 }                                                                       \
887                                                                         \
888 static void OPNAME ## qpel8_mc33_ ## MMX(uint8_t *dst, uint8_t *src,    \
889                                          int stride)                    \
890 {                                                                       \
891     uint64_t half[8 + 9];                                               \
892     uint8_t * const halfH  = ((uint8_t*)half) + 64;                     \
893     uint8_t * const halfHV = ((uint8_t*)half);                          \
894     ff_put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8,       \
895                                                    stride, 9);          \
896     ff_put ## RND ## pixels8_l2_ ## MMX(halfH, src + 1, halfH, 8,       \
897                                         stride, 9);                     \
898     ff_put ## RND ## mpeg4_qpel8_v_lowpass_ ## MMX(halfHV, halfH, 8, 8);\
899     ff_ ## OPNAME ## pixels8_l2_ ## MMX(dst, halfH + 8, halfHV,         \
900                                         stride, 8, 8);                  \
901 }                                                                       \
902                                                                         \
903 static void OPNAME ## qpel8_mc21_ ## MMX(uint8_t *dst, uint8_t *src,    \
904                                          int stride)                    \
905 {                                                                       \
906     uint64_t half[8 + 9];                                               \
907     uint8_t * const halfH  = ((uint8_t*)half) + 64;                     \
908     uint8_t * const halfHV = ((uint8_t*)half);                          \
909     ff_put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8,       \
910                                                    stride, 9);          \
911     ff_put ## RND ## mpeg4_qpel8_v_lowpass_ ## MMX(halfHV, halfH, 8, 8);\
912     ff_ ## OPNAME ## pixels8_l2_ ## MMX(dst, halfH, halfHV,             \
913                                         stride, 8, 8);                  \
914 }                                                                       \
915                                                                         \
916 static void OPNAME ## qpel8_mc23_ ## MMX(uint8_t *dst, uint8_t *src,    \
917                                          int stride)                    \
918 {                                                                       \
919     uint64_t half[8 + 9];                                               \
920     uint8_t * const halfH  = ((uint8_t*)half) + 64;                     \
921     uint8_t * const halfHV = ((uint8_t*)half);                          \
922     ff_put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8,       \
923                                                    stride, 9);          \
924     ff_put ## RND ## mpeg4_qpel8_v_lowpass_ ## MMX(halfHV, halfH, 8, 8);\
925     ff_ ## OPNAME ## pixels8_l2_ ## MMX(dst, halfH + 8, halfHV,         \
926                                         stride, 8, 8);                  \
927 }                                                                       \
928                                                                         \
929 static void OPNAME ## qpel8_mc12_ ## MMX(uint8_t *dst, uint8_t *src,    \
930                                          int stride)                    \
931 {                                                                       \
932     uint64_t half[8 + 9];                                               \
933     uint8_t * const halfH = ((uint8_t*)half);                           \
934     ff_put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8,       \
935                                                    stride, 9);          \
936     ff_put ## RND ## pixels8_l2_ ## MMX(halfH, src, halfH,              \
937                                         8, stride, 9);                  \
938     ff_ ## OPNAME ## mpeg4_qpel8_v_lowpass_ ## MMX(dst, halfH,          \
939                                                    stride, 8);          \
940 }                                                                       \
941                                                                         \
942 static void OPNAME ## qpel8_mc32_ ## MMX(uint8_t *dst, uint8_t *src,    \
943                                          int stride)                    \
944 {                                                                       \
945     uint64_t half[8 + 9];                                               \
946     uint8_t * const halfH = ((uint8_t*)half);                           \
947     ff_put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8,       \
948                                                    stride, 9);          \
949     ff_put ## RND ## pixels8_l2_ ## MMX(halfH, src + 1, halfH, 8,       \
950                                         stride, 9);                     \
951     ff_ ## OPNAME ## mpeg4_qpel8_v_lowpass_ ## MMX(dst, halfH,          \
952                                                    stride, 8);          \
953 }                                                                       \
954                                                                         \
955 static void OPNAME ## qpel8_mc22_ ## MMX(uint8_t *dst, uint8_t *src,    \
956                                          int stride)                    \
957 {                                                                       \
958     uint64_t half[9];                                                   \
959     uint8_t * const halfH = ((uint8_t*)half);                           \
960     ff_put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8,       \
961                                                    stride, 9);          \
962     ff_ ## OPNAME ## mpeg4_qpel8_v_lowpass_ ## MMX(dst, halfH,          \
963                                                    stride, 8);          \
964 }                                                                       \
965                                                                         \
966 static void OPNAME ## qpel16_mc00_ ## MMX (uint8_t *dst, uint8_t *src,  \
967                                            int stride)                  \
968 {                                                                       \
969     ff_ ## OPNAME ## pixels16_ ## MMX(dst, src, stride, 16);            \
970 }                                                                       \
971                                                                         \
972 static void OPNAME ## qpel16_mc10_ ## MMX(uint8_t *dst, uint8_t *src,   \
973                                           int stride)                   \
974 {                                                                       \
975     uint64_t temp[32];                                                  \
976     uint8_t * const half = (uint8_t*)temp;                              \
977     ff_put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(half, src, 16,      \
978                                                     stride, 16);        \
979     ff_ ## OPNAME ## pixels16_l2_ ## MMX(dst, src, half, stride,        \
980                                          stride, 16);                   \
981 }                                                                       \
982                                                                         \
983 static void OPNAME ## qpel16_mc20_ ## MMX(uint8_t *dst, uint8_t *src,   \
984                                           int stride)                   \
985 {                                                                       \
986     ff_ ## OPNAME ## mpeg4_qpel16_h_lowpass_ ## MMX(dst, src,           \
987                                                     stride, stride, 16);\
988 }                                                                       \
989                                                                         \
990 static void OPNAME ## qpel16_mc30_ ## MMX(uint8_t *dst, uint8_t *src,   \
991                                           int stride)                   \
992 {                                                                       \
993     uint64_t temp[32];                                                  \
994     uint8_t * const half = (uint8_t*)temp;                              \
995     ff_put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(half, src, 16,      \
996                                                     stride, 16);        \
997     ff_ ## OPNAME ## pixels16_l2_ ## MMX(dst, src + 1, half,            \
998                                          stride, stride, 16);           \
999 }                                                                       \
1000                                                                         \
1001 static void OPNAME ## qpel16_mc01_ ## MMX(uint8_t *dst, uint8_t *src,   \
1002                                           int stride)                   \
1003 {                                                                       \
1004     uint64_t temp[32];                                                  \
1005     uint8_t * const half = (uint8_t*)temp;                              \
1006     ff_put ## RND ## mpeg4_qpel16_v_lowpass_ ## MMX(half, src, 16,      \
1007                                                     stride);            \
1008     ff_ ## OPNAME ## pixels16_l2_ ## MMX(dst, src, half, stride,        \
1009                                          stride, 16);                   \
1010 }                                                                       \
1011                                                                         \
1012 static void OPNAME ## qpel16_mc02_ ## MMX(uint8_t *dst, uint8_t *src,   \
1013                                           int stride)                   \
1014 {                                                                       \
1015     ff_ ## OPNAME ## mpeg4_qpel16_v_lowpass_ ## MMX(dst, src,           \
1016                                                     stride, stride);    \
1017 }                                                                       \
1018                                                                         \
1019 static void OPNAME ## qpel16_mc03_ ## MMX(uint8_t *dst, uint8_t *src,   \
1020                                           int stride)                   \
1021 {                                                                       \
1022     uint64_t temp[32];                                                  \
1023     uint8_t * const half = (uint8_t*)temp;                              \
1024     ff_put ## RND ## mpeg4_qpel16_v_lowpass_ ## MMX(half, src, 16,      \
1025                                                     stride);            \
1026     ff_ ## OPNAME ## pixels16_l2_ ## MMX(dst, src+stride, half,         \
1027                                          stride, stride, 16);           \
1028 }                                                                       \
1029                                                                         \
1030 static void OPNAME ## qpel16_mc11_ ## MMX(uint8_t *dst, uint8_t *src,   \
1031                                           int stride)                   \
1032 {                                                                       \
1033     uint64_t half[16 * 2 + 17 * 2];                                     \
1034     uint8_t * const halfH  = ((uint8_t*)half) + 256;                    \
1035     uint8_t * const halfHV = ((uint8_t*)half);                          \
1036     ff_put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16,     \
1037                                                     stride, 17);        \
1038     ff_put ## RND ## pixels16_l2_ ## MMX(halfH, src, halfH, 16,         \
1039                                          stride, 17);                   \
1040     ff_put ## RND ## mpeg4_qpel16_v_lowpass_ ## MMX(halfHV, halfH,      \
1041                                                     16, 16);            \
1042     ff_ ## OPNAME ## pixels16_l2_ ## MMX(dst, halfH, halfHV,            \
1043                                          stride, 16, 16);               \
1044 }                                                                       \
1045                                                                         \
1046 static void OPNAME ## qpel16_mc31_ ## MMX(uint8_t *dst, uint8_t *src,   \
1047                                           int stride)                   \
1048 {                                                                       \
1049     uint64_t half[16 * 2 + 17 * 2];                                     \
1050     uint8_t * const halfH  = ((uint8_t*)half) + 256;                    \
1051     uint8_t * const halfHV = ((uint8_t*)half);                          \
1052     ff_put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16,     \
1053                                                     stride, 17);        \
1054     ff_put ## RND ## pixels16_l2_ ## MMX(halfH, src + 1, halfH, 16,     \
1055                                          stride, 17);                   \
1056     ff_put ## RND ## mpeg4_qpel16_v_lowpass_ ## MMX(halfHV, halfH,      \
1057                                                     16, 16);            \
1058     ff_ ## OPNAME ## pixels16_l2_ ## MMX(dst, halfH, halfHV,            \
1059                                          stride, 16, 16);               \
1060 }                                                                       \
1061                                                                         \
1062 static void OPNAME ## qpel16_mc13_ ## MMX(uint8_t *dst, uint8_t *src,   \
1063                                           int stride)                   \
1064 {                                                                       \
1065     uint64_t half[16 * 2 + 17 * 2];                                     \
1066     uint8_t * const halfH  = ((uint8_t*)half) + 256;                    \
1067     uint8_t * const halfHV = ((uint8_t*)half);                          \
1068     ff_put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16,     \
1069                                                     stride, 17);        \
1070     ff_put ## RND ## pixels16_l2_ ## MMX(halfH, src, halfH, 16,         \
1071                                          stride, 17);                   \
1072     ff_put ## RND ## mpeg4_qpel16_v_lowpass_ ## MMX(halfHV, halfH,      \
1073                                                     16, 16);            \
1074     ff_ ## OPNAME ## pixels16_l2_ ## MMX(dst, halfH + 16, halfHV,       \
1075                                          stride, 16, 16);               \
1076 }                                                                       \
1077                                                                         \
1078 static void OPNAME ## qpel16_mc33_ ## MMX(uint8_t *dst, uint8_t *src,   \
1079                                           int stride)                   \
1080 {                                                                       \
1081     uint64_t half[16 * 2 + 17 * 2];                                     \
1082     uint8_t * const halfH  = ((uint8_t*)half) + 256;                    \
1083     uint8_t * const halfHV = ((uint8_t*)half);                          \
1084     ff_put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16,     \
1085                                                     stride, 17);        \
1086     ff_put ## RND ## pixels16_l2_ ## MMX(halfH, src + 1, halfH, 16,     \
1087                                          stride, 17);                   \
1088     ff_put ## RND ## mpeg4_qpel16_v_lowpass_ ## MMX(halfHV, halfH,      \
1089                                                     16, 16);            \
1090     ff_ ## OPNAME ## pixels16_l2_ ## MMX(dst, halfH + 16, halfHV,       \
1091                                          stride, 16, 16);               \
1092 }                                                                       \
1093                                                                         \
1094 static void OPNAME ## qpel16_mc21_ ## MMX(uint8_t *dst, uint8_t *src,   \
1095                                           int stride)                   \
1096 {                                                                       \
1097     uint64_t half[16 * 2 + 17 * 2];                                     \
1098     uint8_t * const halfH  = ((uint8_t*)half) + 256;                    \
1099     uint8_t * const halfHV = ((uint8_t*)half);                          \
1100     ff_put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16,     \
1101                                                     stride, 17);        \
1102     ff_put ## RND ## mpeg4_qpel16_v_lowpass_ ## MMX(halfHV, halfH,      \
1103                                                     16, 16);            \
1104     ff_ ## OPNAME ## pixels16_l2_ ## MMX(dst, halfH, halfHV,            \
1105                                          stride, 16, 16);               \
1106 }                                                                       \
1107                                                                         \
1108 static void OPNAME ## qpel16_mc23_ ## MMX(uint8_t *dst, uint8_t *src,   \
1109                                           int stride)                   \
1110 {                                                                       \
1111     uint64_t half[16 * 2 + 17 * 2];                                     \
1112     uint8_t * const halfH  = ((uint8_t*)half) + 256;                    \
1113     uint8_t * const halfHV = ((uint8_t*)half);                          \
1114     ff_put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16,     \
1115                                                     stride, 17);        \
1116     ff_put ## RND ## mpeg4_qpel16_v_lowpass_ ## MMX(halfHV, halfH,      \
1117                                                     16, 16);            \
1118     ff_ ## OPNAME ## pixels16_l2_ ## MMX(dst, halfH + 16, halfHV,       \
1119                                          stride, 16, 16);               \
1120 }                                                                       \
1121                                                                         \
1122 static void OPNAME ## qpel16_mc12_ ## MMX(uint8_t *dst, uint8_t *src,   \
1123                                           int stride)                   \
1124 {                                                                       \
1125     uint64_t half[17 * 2];                                              \
1126     uint8_t * const halfH = ((uint8_t*)half);                           \
1127     ff_put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16,     \
1128                                                     stride, 17);        \
1129     ff_put ## RND ## pixels16_l2_ ## MMX(halfH, src, halfH, 16,         \
1130                                          stride, 17);                   \
1131     ff_ ## OPNAME ## mpeg4_qpel16_v_lowpass_ ## MMX(dst, halfH,         \
1132                                                     stride, 16);        \
1133 }                                                                       \
1134                                                                         \
1135 static void OPNAME ## qpel16_mc32_ ## MMX(uint8_t *dst, uint8_t *src,   \
1136                                           int stride)                   \
1137 {                                                                       \
1138     uint64_t half[17 * 2];                                              \
1139     uint8_t * const halfH = ((uint8_t*)half);                           \
1140     ff_put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16,     \
1141                                                     stride, 17);        \
1142     ff_put ## RND ## pixels16_l2_ ## MMX(halfH, src + 1, halfH, 16,     \
1143                                          stride, 17);                   \
1144     ff_ ## OPNAME ## mpeg4_qpel16_v_lowpass_ ## MMX(dst, halfH,         \
1145                                                     stride, 16);        \
1146 }                                                                       \
1147                                                                         \
1148 static void OPNAME ## qpel16_mc22_ ## MMX(uint8_t *dst, uint8_t *src,   \
1149                                           int stride)                   \
1150 {                                                                       \
1151     uint64_t half[17 * 2];                                              \
1152     uint8_t * const halfH = ((uint8_t*)half);                           \
1153     ff_put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16,     \
1154                                                     stride, 17);        \
1155     ff_ ## OPNAME ## mpeg4_qpel16_v_lowpass_ ## MMX(dst, halfH,         \
1156                                                     stride, 16);        \
1157 }
1158
1159 QPEL_OP(put_,          ff_pw_16, _,        mmxext)
1160 QPEL_OP(avg_,          ff_pw_16, _,        mmxext)
1161 QPEL_OP(put_no_rnd_,   ff_pw_15, _no_rnd_, mmxext)
1162 #endif /* HAVE_YASM */
1163
1164
1165 #if HAVE_INLINE_ASM
1166 void ff_put_rv40_qpel8_mc33_mmx(uint8_t *dst, uint8_t *src, int stride)
1167 {
1168   put_pixels8_xy2_mmx(dst, src, stride, 8);
1169 }
1170 void ff_put_rv40_qpel16_mc33_mmx(uint8_t *dst, uint8_t *src, int stride)
1171 {
1172   put_pixels16_xy2_mmx(dst, src, stride, 16);
1173 }
1174 void ff_avg_rv40_qpel8_mc33_mmx(uint8_t *dst, uint8_t *src, int stride)
1175 {
1176   avg_pixels8_xy2_mmx(dst, src, stride, 8);
1177 }
1178 void ff_avg_rv40_qpel16_mc33_mmx(uint8_t *dst, uint8_t *src, int stride)
1179 {
1180   avg_pixels16_xy2_mmx(dst, src, stride, 16);
1181 }
1182
1183 typedef void emulated_edge_mc_func(uint8_t *dst, const uint8_t *src,
1184                                    ptrdiff_t linesize, int block_w, int block_h,
1185                                    int src_x, int src_y, int w, int h);
1186
1187 static av_always_inline void gmc(uint8_t *dst, uint8_t *src,
1188                                  int stride, int h, int ox, int oy,
1189                                  int dxx, int dxy, int dyx, int dyy,
1190                                  int shift, int r, int width, int height,
1191                                  emulated_edge_mc_func *emu_edge_fn)
1192 {
1193     const int w    = 8;
1194     const int ix   = ox  >> (16 + shift);
1195     const int iy   = oy  >> (16 + shift);
1196     const int oxs  = ox  >> 4;
1197     const int oys  = oy  >> 4;
1198     const int dxxs = dxx >> 4;
1199     const int dxys = dxy >> 4;
1200     const int dyxs = dyx >> 4;
1201     const int dyys = dyy >> 4;
1202     const uint16_t r4[4]   = { r, r, r, r };
1203     const uint16_t dxy4[4] = { dxys, dxys, dxys, dxys };
1204     const uint16_t dyy4[4] = { dyys, dyys, dyys, dyys };
1205     const uint64_t shift2 = 2 * shift;
1206 #define MAX_STRIDE 4096U
1207 #define MAX_H 8U
1208     uint8_t edge_buf[(MAX_H + 1) * MAX_STRIDE];
1209     int x, y;
1210
1211     const int dxw = (dxx - (1 << (16 + shift))) * (w - 1);
1212     const int dyh = (dyy - (1 << (16 + shift))) * (h - 1);
1213     const int dxh = dxy * (h - 1);
1214     const int dyw = dyx * (w - 1);
1215     int need_emu =  (unsigned)ix >= width  - w ||
1216                     (unsigned)iy >= height - h;
1217
1218     if ( // non-constant fullpel offset (3% of blocks)
1219         ((ox ^ (ox + dxw)) | (ox ^ (ox + dxh)) | (ox ^ (ox + dxw + dxh)) |
1220          (oy ^ (oy + dyw)) | (oy ^ (oy + dyh)) | (oy ^ (oy + dyw + dyh))) >> (16 + shift)
1221         // uses more than 16 bits of subpel mv (only at huge resolution)
1222         || (dxx | dxy | dyx | dyy) & 15
1223         || (need_emu && (h > MAX_H || stride > MAX_STRIDE))) {
1224         // FIXME could still use mmx for some of the rows
1225         ff_gmc_c(dst, src, stride, h, ox, oy, dxx, dxy, dyx, dyy,
1226                  shift, r, width, height);
1227         return;
1228     }
1229
1230     src += ix + iy * stride;
1231     if (need_emu) {
1232         emu_edge_fn(edge_buf, src, stride, w + 1, h + 1, ix, iy, width, height);
1233         src = edge_buf;
1234     }
1235
1236     __asm__ volatile (
1237         "movd         %0, %%mm6         \n\t"
1238         "pxor      %%mm7, %%mm7         \n\t"
1239         "punpcklwd %%mm6, %%mm6         \n\t"
1240         "punpcklwd %%mm6, %%mm6         \n\t"
1241         :: "r"(1<<shift)
1242     );
1243
1244     for (x = 0; x < w; x += 4) {
1245         uint16_t dx4[4] = { oxs - dxys + dxxs * (x + 0),
1246                             oxs - dxys + dxxs * (x + 1),
1247                             oxs - dxys + dxxs * (x + 2),
1248                             oxs - dxys + dxxs * (x + 3) };
1249         uint16_t dy4[4] = { oys - dyys + dyxs * (x + 0),
1250                             oys - dyys + dyxs * (x + 1),
1251                             oys - dyys + dyxs * (x + 2),
1252                             oys - dyys + dyxs * (x + 3) };
1253
1254         for (y = 0; y < h; y++) {
1255             __asm__ volatile (
1256                 "movq      %0, %%mm4    \n\t"
1257                 "movq      %1, %%mm5    \n\t"
1258                 "paddw     %2, %%mm4    \n\t"
1259                 "paddw     %3, %%mm5    \n\t"
1260                 "movq   %%mm4, %0       \n\t"
1261                 "movq   %%mm5, %1       \n\t"
1262                 "psrlw    $12, %%mm4    \n\t"
1263                 "psrlw    $12, %%mm5    \n\t"
1264                 : "+m"(*dx4), "+m"(*dy4)
1265                 : "m"(*dxy4), "m"(*dyy4)
1266             );
1267
1268             __asm__ volatile (
1269                 "movq      %%mm6, %%mm2 \n\t"
1270                 "movq      %%mm6, %%mm1 \n\t"
1271                 "psubw     %%mm4, %%mm2 \n\t"
1272                 "psubw     %%mm5, %%mm1 \n\t"
1273                 "movq      %%mm2, %%mm0 \n\t"
1274                 "movq      %%mm4, %%mm3 \n\t"
1275                 "pmullw    %%mm1, %%mm0 \n\t" // (s - dx) * (s - dy)
1276                 "pmullw    %%mm5, %%mm3 \n\t" // dx * dy
1277                 "pmullw    %%mm5, %%mm2 \n\t" // (s - dx) * dy
1278                 "pmullw    %%mm4, %%mm1 \n\t" // dx * (s - dy)
1279
1280                 "movd         %4, %%mm5 \n\t"
1281                 "movd         %3, %%mm4 \n\t"
1282                 "punpcklbw %%mm7, %%mm5 \n\t"
1283                 "punpcklbw %%mm7, %%mm4 \n\t"
1284                 "pmullw    %%mm5, %%mm3 \n\t" // src[1, 1] * dx * dy
1285                 "pmullw    %%mm4, %%mm2 \n\t" // src[0, 1] * (s - dx) * dy
1286
1287                 "movd         %2, %%mm5 \n\t"
1288                 "movd         %1, %%mm4 \n\t"
1289                 "punpcklbw %%mm7, %%mm5 \n\t"
1290                 "punpcklbw %%mm7, %%mm4 \n\t"
1291                 "pmullw    %%mm5, %%mm1 \n\t" // src[1, 0] * dx * (s - dy)
1292                 "pmullw    %%mm4, %%mm0 \n\t" // src[0, 0] * (s - dx) * (s - dy)
1293                 "paddw        %5, %%mm1 \n\t"
1294                 "paddw     %%mm3, %%mm2 \n\t"
1295                 "paddw     %%mm1, %%mm0 \n\t"
1296                 "paddw     %%mm2, %%mm0 \n\t"
1297
1298                 "psrlw        %6, %%mm0 \n\t"
1299                 "packuswb  %%mm0, %%mm0 \n\t"
1300                 "movd      %%mm0, %0    \n\t"
1301
1302                 : "=m"(dst[x + y * stride])
1303                 : "m"(src[0]), "m"(src[1]),
1304                   "m"(src[stride]), "m"(src[stride + 1]),
1305                   "m"(*r4), "m"(shift2)
1306             );
1307             src += stride;
1308         }
1309         src += 4 - h * stride;
1310     }
1311 }
1312
1313 #if CONFIG_VIDEODSP
1314 #if HAVE_YASM
1315 #if ARCH_X86_32
1316 static void gmc_mmx(uint8_t *dst, uint8_t *src,
1317                     int stride, int h, int ox, int oy,
1318                     int dxx, int dxy, int dyx, int dyy,
1319                     int shift, int r, int width, int height)
1320 {
1321     gmc(dst, src, stride, h, ox, oy, dxx, dxy, dyx, dyy, shift, r,
1322         width, height, &ff_emulated_edge_mc_8);
1323 }
1324 #endif
1325 static void gmc_sse(uint8_t *dst, uint8_t *src,
1326                     int stride, int h, int ox, int oy,
1327                     int dxx, int dxy, int dyx, int dyy,
1328                     int shift, int r, int width, int height)
1329 {
1330     gmc(dst, src, stride, h, ox, oy, dxx, dxy, dyx, dyy, shift, r,
1331         width, height, &ff_emulated_edge_mc_8);
1332 }
1333 #else
1334 static void gmc_mmx(uint8_t *dst, uint8_t *src,
1335                     int stride, int h, int ox, int oy,
1336                     int dxx, int dxy, int dyx, int dyy,
1337                     int shift, int r, int width, int height)
1338 {
1339     gmc(dst, src, stride, h, ox, oy, dxx, dxy, dyx, dyy, shift, r,
1340         width, height, &ff_emulated_edge_mc_8);
1341 }
1342 #endif
1343 #endif
1344
1345 #endif /* HAVE_INLINE_ASM */
1346
1347 void ff_put_pixels16_sse2(uint8_t *block, const uint8_t *pixels,
1348                           ptrdiff_t line_size, int h);
1349 void ff_avg_pixels16_sse2(uint8_t *block, const uint8_t *pixels,
1350                           ptrdiff_t line_size, int h);
1351
1352 #if HAVE_INLINE_ASM
1353
1354 /* CAVS-specific */
1355 void ff_put_cavs_qpel8_mc00_mmxext(uint8_t *dst, uint8_t *src, int stride)
1356 {
1357     put_pixels8_mmx(dst, src, stride, 8);
1358 }
1359
1360 void ff_avg_cavs_qpel8_mc00_mmxext(uint8_t *dst, uint8_t *src, int stride)
1361 {
1362     avg_pixels8_mmx(dst, src, stride, 8);
1363 }
1364
1365 void ff_put_cavs_qpel16_mc00_mmxext(uint8_t *dst, uint8_t *src, int stride)
1366 {
1367     put_pixels16_mmx(dst, src, stride, 16);
1368 }
1369
1370 void ff_avg_cavs_qpel16_mc00_mmxext(uint8_t *dst, uint8_t *src, int stride)
1371 {
1372     avg_pixels16_mmx(dst, src, stride, 16);
1373 }
1374 #endif /* HAVE_INLINE_ASM */
1375
1376 #if HAVE_YASM
1377 /* VC-1-specific */
1378 void ff_put_vc1_mspel_mc00_mmx(uint8_t *dst, const uint8_t *src,
1379                                int stride, int rnd)
1380 {
1381     ff_put_pixels8_mmx(dst, src, stride, 8);
1382 }
1383 #endif /* HAVE_YASM */
1384
1385 #if CONFIG_DIRAC_DECODER
1386 #define DIRAC_PIXOP(OPNAME2, OPNAME, EXT)\
1387 void ff_ ## OPNAME2 ## _dirac_pixels8_ ## EXT(uint8_t *dst, const uint8_t *src[5], int stride, int h)\
1388 {\
1389     if (h&3)\
1390         ff_ ## OPNAME2 ## _dirac_pixels8_c(dst, src, stride, h);\
1391     else\
1392         OPNAME ## _pixels8_ ## EXT(dst, src[0], stride, h);\
1393 }\
1394 void ff_ ## OPNAME2 ## _dirac_pixels16_ ## EXT(uint8_t *dst, const uint8_t *src[5], int stride, int h)\
1395 {\
1396     if (h&3)\
1397         ff_ ## OPNAME2 ## _dirac_pixels16_c(dst, src, stride, h);\
1398     else\
1399         OPNAME ## _pixels16_ ## EXT(dst, src[0], stride, h);\
1400 }\
1401 void ff_ ## OPNAME2 ## _dirac_pixels32_ ## EXT(uint8_t *dst, const uint8_t *src[5], int stride, int h)\
1402 {\
1403     if (h&3) {\
1404         ff_ ## OPNAME2 ## _dirac_pixels32_c(dst, src, stride, h);\
1405     } else {\
1406         OPNAME ## _pixels16_ ## EXT(dst   , src[0]   , stride, h);\
1407         OPNAME ## _pixels16_ ## EXT(dst+16, src[0]+16, stride, h);\
1408     }\
1409 }
1410
1411 #if HAVE_MMX_INLINE
1412 DIRAC_PIXOP(put, put, mmx)
1413 DIRAC_PIXOP(avg, avg, mmx)
1414 #endif
1415
1416 #if HAVE_YASM
1417 DIRAC_PIXOP(avg, ff_avg, mmxext)
1418
1419 void ff_put_dirac_pixels16_sse2(uint8_t *dst, const uint8_t *src[5], int stride, int h)
1420 {
1421     if (h&3)
1422         ff_put_dirac_pixels16_c(dst, src, stride, h);
1423     else
1424     ff_put_pixels16_sse2(dst, src[0], stride, h);
1425 }
1426 void ff_avg_dirac_pixels16_sse2(uint8_t *dst, const uint8_t *src[5], int stride, int h)
1427 {
1428     if (h&3)
1429         ff_avg_dirac_pixels16_c(dst, src, stride, h);
1430     else
1431     ff_avg_pixels16_sse2(dst, src[0], stride, h);
1432 }
1433 void ff_put_dirac_pixels32_sse2(uint8_t *dst, const uint8_t *src[5], int stride, int h)
1434 {
1435     if (h&3) {
1436         ff_put_dirac_pixels32_c(dst, src, stride, h);
1437     } else {
1438     ff_put_pixels16_sse2(dst   , src[0]   , stride, h);
1439     ff_put_pixels16_sse2(dst+16, src[0]+16, stride, h);
1440     }
1441 }
1442 void ff_avg_dirac_pixels32_sse2(uint8_t *dst, const uint8_t *src[5], int stride, int h)
1443 {
1444     if (h&3) {
1445         ff_avg_dirac_pixels32_c(dst, src, stride, h);
1446     } else {
1447     ff_avg_pixels16_sse2(dst   , src[0]   , stride, h);
1448     ff_avg_pixels16_sse2(dst+16, src[0]+16, stride, h);
1449     }
1450 }
1451 #endif
1452 #endif
1453
1454 /* XXX: Those functions should be suppressed ASAP when all IDCTs are
1455  * converted. */
1456 #if CONFIG_GPL
1457 static void ff_libmpeg2mmx_idct_put(uint8_t *dest, int line_size,
1458                                     int16_t *block)
1459 {
1460     ff_mmx_idct(block);
1461     ff_put_pixels_clamped_mmx(block, dest, line_size);
1462 }
1463
1464 static void ff_libmpeg2mmx_idct_add(uint8_t *dest, int line_size,
1465                                     int16_t *block)
1466 {
1467     ff_mmx_idct(block);
1468     ff_add_pixels_clamped_mmx(block, dest, line_size);
1469 }
1470
1471 static void ff_libmpeg2mmx2_idct_put(uint8_t *dest, int line_size,
1472                                      int16_t *block)
1473 {
1474     ff_mmxext_idct(block);
1475     ff_put_pixels_clamped_mmx(block, dest, line_size);
1476 }
1477
1478 static void ff_libmpeg2mmx2_idct_add(uint8_t *dest, int line_size,
1479                                      int16_t *block)
1480 {
1481     ff_mmxext_idct(block);
1482     ff_add_pixels_clamped_mmx(block, dest, line_size);
1483 }
1484 #endif
1485
1486 #if HAVE_INLINE_ASM
1487 static void vector_clipf_sse(float *dst, const float *src,
1488                              float min, float max, int len)
1489 {
1490     x86_reg i = (len - 16) * 4;
1491     __asm__ volatile (
1492         "movss          %3, %%xmm4      \n\t"
1493         "movss          %4, %%xmm5      \n\t"
1494         "shufps $0, %%xmm4, %%xmm4      \n\t"
1495         "shufps $0, %%xmm5, %%xmm5      \n\t"
1496         "1:                             \n\t"
1497         "movaps   (%2, %0), %%xmm0      \n\t" // 3/1 on intel
1498         "movaps 16(%2, %0), %%xmm1      \n\t"
1499         "movaps 32(%2, %0), %%xmm2      \n\t"
1500         "movaps 48(%2, %0), %%xmm3      \n\t"
1501         "maxps      %%xmm4, %%xmm0      \n\t"
1502         "maxps      %%xmm4, %%xmm1      \n\t"
1503         "maxps      %%xmm4, %%xmm2      \n\t"
1504         "maxps      %%xmm4, %%xmm3      \n\t"
1505         "minps      %%xmm5, %%xmm0      \n\t"
1506         "minps      %%xmm5, %%xmm1      \n\t"
1507         "minps      %%xmm5, %%xmm2      \n\t"
1508         "minps      %%xmm5, %%xmm3      \n\t"
1509         "movaps     %%xmm0,   (%1, %0)  \n\t"
1510         "movaps     %%xmm1, 16(%1, %0)  \n\t"
1511         "movaps     %%xmm2, 32(%1, %0)  \n\t"
1512         "movaps     %%xmm3, 48(%1, %0)  \n\t"
1513         "sub           $64, %0          \n\t"
1514         "jge            1b              \n\t"
1515         : "+&r"(i)
1516         : "r"(dst), "r"(src), "m"(min), "m"(max)
1517         : "memory"
1518     );
1519 }
1520
1521 #endif /* HAVE_INLINE_ASM */
1522
1523 int32_t ff_scalarproduct_int16_mmxext(const int16_t *v1, const int16_t *v2,
1524                                       int order);
1525 int32_t ff_scalarproduct_int16_sse2(const int16_t *v1, const int16_t *v2,
1526                                     int order);
1527 int32_t ff_scalarproduct_and_madd_int16_mmxext(int16_t *v1, const int16_t *v2,
1528                                                const int16_t *v3,
1529                                                int order, int mul);
1530 int32_t ff_scalarproduct_and_madd_int16_sse2(int16_t *v1, const int16_t *v2,
1531                                              const int16_t *v3,
1532                                              int order, int mul);
1533 int32_t ff_scalarproduct_and_madd_int16_ssse3(int16_t *v1, const int16_t *v2,
1534                                               const int16_t *v3,
1535                                               int order, int mul);
1536
1537 void ff_apply_window_int16_round_mmxext(int16_t *output, const int16_t *input,
1538                                         const int16_t *window, unsigned int len);
1539 void ff_apply_window_int16_round_sse2(int16_t *output, const int16_t *input,
1540                                       const int16_t *window, unsigned int len);
1541 void ff_apply_window_int16_mmxext(int16_t *output, const int16_t *input,
1542                                   const int16_t *window, unsigned int len);
1543 void ff_apply_window_int16_sse2(int16_t *output, const int16_t *input,
1544                                 const int16_t *window, unsigned int len);
1545 void ff_apply_window_int16_ssse3(int16_t *output, const int16_t *input,
1546                                  const int16_t *window, unsigned int len);
1547 void ff_apply_window_int16_ssse3_atom(int16_t *output, const int16_t *input,
1548                                       const int16_t *window, unsigned int len);
1549
1550 void ff_bswap32_buf_ssse3(uint32_t *dst, const uint32_t *src, int w);
1551 void ff_bswap32_buf_sse2(uint32_t *dst, const uint32_t *src, int w);
1552
1553 void ff_add_hfyu_median_prediction_mmxext(uint8_t *dst, const uint8_t *top,
1554                                           const uint8_t *diff, int w,
1555                                           int *left, int *left_top);
1556 int  ff_add_hfyu_left_prediction_ssse3(uint8_t *dst, const uint8_t *src,
1557                                        int w, int left);
1558 int  ff_add_hfyu_left_prediction_sse4(uint8_t *dst, const uint8_t *src,
1559                                       int w, int left);
1560
1561 void ff_vector_clip_int32_mmx     (int32_t *dst, const int32_t *src,
1562                                    int32_t min, int32_t max, unsigned int len);
1563 void ff_vector_clip_int32_sse2    (int32_t *dst, const int32_t *src,
1564                                    int32_t min, int32_t max, unsigned int len);
1565 void ff_vector_clip_int32_int_sse2(int32_t *dst, const int32_t *src,
1566                                    int32_t min, int32_t max, unsigned int len);
1567 void ff_vector_clip_int32_sse4    (int32_t *dst, const int32_t *src,
1568                                    int32_t min, int32_t max, unsigned int len);
1569
1570 #define SET_QPEL_FUNCS(PFX, IDX, SIZE, CPU, PREFIX)                          \
1571     do {                                                                     \
1572     c->PFX ## _pixels_tab[IDX][ 0] = PREFIX ## PFX ## SIZE ## _mc00_ ## CPU; \
1573     c->PFX ## _pixels_tab[IDX][ 1] = PREFIX ## PFX ## SIZE ## _mc10_ ## CPU; \
1574     c->PFX ## _pixels_tab[IDX][ 2] = PREFIX ## PFX ## SIZE ## _mc20_ ## CPU; \
1575     c->PFX ## _pixels_tab[IDX][ 3] = PREFIX ## PFX ## SIZE ## _mc30_ ## CPU; \
1576     c->PFX ## _pixels_tab[IDX][ 4] = PREFIX ## PFX ## SIZE ## _mc01_ ## CPU; \
1577     c->PFX ## _pixels_tab[IDX][ 5] = PREFIX ## PFX ## SIZE ## _mc11_ ## CPU; \
1578     c->PFX ## _pixels_tab[IDX][ 6] = PREFIX ## PFX ## SIZE ## _mc21_ ## CPU; \
1579     c->PFX ## _pixels_tab[IDX][ 7] = PREFIX ## PFX ## SIZE ## _mc31_ ## CPU; \
1580     c->PFX ## _pixels_tab[IDX][ 8] = PREFIX ## PFX ## SIZE ## _mc02_ ## CPU; \
1581     c->PFX ## _pixels_tab[IDX][ 9] = PREFIX ## PFX ## SIZE ## _mc12_ ## CPU; \
1582     c->PFX ## _pixels_tab[IDX][10] = PREFIX ## PFX ## SIZE ## _mc22_ ## CPU; \
1583     c->PFX ## _pixels_tab[IDX][11] = PREFIX ## PFX ## SIZE ## _mc32_ ## CPU; \
1584     c->PFX ## _pixels_tab[IDX][12] = PREFIX ## PFX ## SIZE ## _mc03_ ## CPU; \
1585     c->PFX ## _pixels_tab[IDX][13] = PREFIX ## PFX ## SIZE ## _mc13_ ## CPU; \
1586     c->PFX ## _pixels_tab[IDX][14] = PREFIX ## PFX ## SIZE ## _mc23_ ## CPU; \
1587     c->PFX ## _pixels_tab[IDX][15] = PREFIX ## PFX ## SIZE ## _mc33_ ## CPU; \
1588     } while (0)
1589
1590 #define SET_HPEL_FUNCS(PFX, IDX, SIZE, CPU)                                     \
1591     do {                                                                        \
1592         c->PFX ## _pixels_tab IDX [0] = PFX ## _pixels ## SIZE ## _     ## CPU; \
1593         c->PFX ## _pixels_tab IDX [1] = PFX ## _pixels ## SIZE ## _x2_  ## CPU; \
1594         c->PFX ## _pixels_tab IDX [2] = PFX ## _pixels ## SIZE ## _y2_  ## CPU; \
1595         c->PFX ## _pixels_tab IDX [3] = PFX ## _pixels ## SIZE ## _xy2_ ## CPU; \
1596     } while (0)
1597
1598 static av_cold void dsputil_init_mmx(DSPContext *c, AVCodecContext *avctx,
1599                                      int mm_flags)
1600 {
1601     const int high_bit_depth = avctx->bits_per_raw_sample > 8;
1602
1603 #if HAVE_INLINE_ASM
1604     c->put_pixels_clamped        = ff_put_pixels_clamped_mmx;
1605     c->put_signed_pixels_clamped = ff_put_signed_pixels_clamped_mmx;
1606     c->add_pixels_clamped        = ff_add_pixels_clamped_mmx;
1607
1608     if (!high_bit_depth) {
1609         c->clear_block  = clear_block_mmx;
1610         c->clear_blocks = clear_blocks_mmx;
1611         c->draw_edges   = draw_edges_mmx;
1612
1613         SET_HPEL_FUNCS(put,        [0], 16, mmx);
1614         SET_HPEL_FUNCS(put_no_rnd, [0], 16, mmx);
1615         SET_HPEL_FUNCS(avg,        [0], 16, mmx);
1616         SET_HPEL_FUNCS(avg_no_rnd,    , 16, mmx);
1617         SET_HPEL_FUNCS(put,        [1],  8, mmx);
1618         SET_HPEL_FUNCS(put_no_rnd, [1],  8, mmx);
1619         SET_HPEL_FUNCS(avg,        [1],  8, mmx);
1620     }
1621
1622 #if CONFIG_VIDEODSP && (ARCH_X86_32 || !HAVE_YASM)
1623     c->gmc = gmc_mmx;
1624 #endif
1625
1626     c->add_bytes = add_bytes_mmx;
1627 #endif /* HAVE_INLINE_ASM */
1628
1629 #if HAVE_YASM
1630     if (CONFIG_H263_DECODER || CONFIG_H263_ENCODER) {
1631         c->h263_v_loop_filter = ff_h263_v_loop_filter_mmx;
1632         c->h263_h_loop_filter = ff_h263_h_loop_filter_mmx;
1633     }
1634
1635     c->vector_clip_int32 = ff_vector_clip_int32_mmx;
1636 #endif
1637
1638 }
1639
1640 static av_cold void dsputil_init_mmxext(DSPContext *c, AVCodecContext *avctx,
1641                                         int mm_flags)
1642 {
1643     const int bit_depth      = avctx->bits_per_raw_sample;
1644     const int high_bit_depth = bit_depth > 8;
1645
1646 #if HAVE_YASM
1647     SET_QPEL_FUNCS(avg_qpel,        0, 16, mmxext, );
1648     SET_QPEL_FUNCS(avg_qpel,        1,  8, mmxext, );
1649
1650     SET_QPEL_FUNCS(put_qpel,        0, 16, mmxext, );
1651     SET_QPEL_FUNCS(put_qpel,        1,  8, mmxext, );
1652     SET_QPEL_FUNCS(put_no_rnd_qpel, 0, 16, mmxext, );
1653     SET_QPEL_FUNCS(put_no_rnd_qpel, 1,  8, mmxext, );
1654
1655     if (!high_bit_depth) {
1656         c->put_pixels_tab[0][1] = ff_put_pixels16_x2_mmxext;
1657         c->put_pixels_tab[0][2] = ff_put_pixels16_y2_mmxext;
1658
1659         c->avg_pixels_tab[0][0] = ff_avg_pixels16_mmxext;
1660         c->avg_pixels_tab[0][1] = ff_avg_pixels16_x2_mmxext;
1661         c->avg_pixels_tab[0][2] = ff_avg_pixels16_y2_mmxext;
1662
1663         c->put_pixels_tab[1][1] = ff_put_pixels8_x2_mmxext;
1664         c->put_pixels_tab[1][2] = ff_put_pixels8_y2_mmxext;
1665
1666         c->avg_pixels_tab[1][0] = ff_avg_pixels8_mmxext;
1667         c->avg_pixels_tab[1][1] = ff_avg_pixels8_x2_mmxext;
1668         c->avg_pixels_tab[1][2] = ff_avg_pixels8_y2_mmxext;
1669     }
1670
1671     if (!(avctx->flags & CODEC_FLAG_BITEXACT)) {
1672         if (!high_bit_depth) {
1673             c->put_no_rnd_pixels_tab[0][1] = ff_put_no_rnd_pixels16_x2_mmxext;
1674             c->put_no_rnd_pixels_tab[0][2] = ff_put_no_rnd_pixels16_y2_mmxext;
1675             c->put_no_rnd_pixels_tab[1][1] = ff_put_no_rnd_pixels8_x2_mmxext;
1676             c->put_no_rnd_pixels_tab[1][2] = ff_put_no_rnd_pixels8_y2_mmxext;
1677
1678             c->avg_pixels_tab[0][3] = ff_avg_pixels16_xy2_mmxext;
1679             c->avg_pixels_tab[1][3] = ff_avg_pixels8_xy2_mmxext;
1680         }
1681     }
1682 #endif /* HAVE_YASM */
1683
1684 #if HAVE_MMXEXT_EXTERNAL
1685     if (CONFIG_VP3_DECODER && (avctx->codec_id == AV_CODEC_ID_VP3 ||
1686                                avctx->codec_id == AV_CODEC_ID_THEORA)) {
1687         c->put_no_rnd_pixels_tab[1][1] = ff_put_no_rnd_pixels8_x2_exact_mmxext;
1688         c->put_no_rnd_pixels_tab[1][2] = ff_put_no_rnd_pixels8_y2_exact_mmxext;
1689     }
1690
1691     /* slower than cmov version on AMD */
1692     if (!(mm_flags & AV_CPU_FLAG_3DNOW))
1693         c->add_hfyu_median_prediction = ff_add_hfyu_median_prediction_mmxext;
1694
1695     c->scalarproduct_int16          = ff_scalarproduct_int16_mmxext;
1696     c->scalarproduct_and_madd_int16 = ff_scalarproduct_and_madd_int16_mmxext;
1697
1698     if (avctx->flags & CODEC_FLAG_BITEXACT) {
1699         c->apply_window_int16 = ff_apply_window_int16_mmxext;
1700     } else {
1701         c->apply_window_int16 = ff_apply_window_int16_round_mmxext;
1702     }
1703 #endif /* HAVE_MMXEXT_EXTERNAL */
1704 }
1705
1706 static av_cold void dsputil_init_3dnow(DSPContext *c, AVCodecContext *avctx,
1707                                        int mm_flags)
1708 {
1709     const int high_bit_depth = avctx->bits_per_raw_sample > 8;
1710
1711 #if HAVE_YASM
1712     if (!high_bit_depth) {
1713         c->put_pixels_tab[0][1] = ff_put_pixels16_x2_3dnow;
1714         c->put_pixels_tab[0][2] = ff_put_pixels16_y2_3dnow;
1715
1716         c->avg_pixels_tab[0][0] = ff_avg_pixels16_3dnow;
1717         c->avg_pixels_tab[0][1] = ff_avg_pixels16_x2_3dnow;
1718         c->avg_pixels_tab[0][2] = ff_avg_pixels16_y2_3dnow;
1719
1720         c->put_pixels_tab[1][1] = ff_put_pixels8_x2_3dnow;
1721         c->put_pixels_tab[1][2] = ff_put_pixels8_y2_3dnow;
1722
1723         c->avg_pixels_tab[1][0] = ff_avg_pixels8_3dnow;
1724         c->avg_pixels_tab[1][1] = ff_avg_pixels8_x2_3dnow;
1725         c->avg_pixels_tab[1][2] = ff_avg_pixels8_y2_3dnow;
1726
1727         if (!(avctx->flags & CODEC_FLAG_BITEXACT)){
1728             c->put_no_rnd_pixels_tab[0][1] = ff_put_no_rnd_pixels16_x2_3dnow;
1729             c->put_no_rnd_pixels_tab[0][2] = ff_put_no_rnd_pixels16_y2_3dnow;
1730             c->put_no_rnd_pixels_tab[1][1] = ff_put_no_rnd_pixels8_x2_3dnow;
1731             c->put_no_rnd_pixels_tab[1][2] = ff_put_no_rnd_pixels8_y2_3dnow;
1732
1733             c->avg_pixels_tab[0][3] = ff_avg_pixels16_xy2_3dnow;
1734             c->avg_pixels_tab[1][3] = ff_avg_pixels8_xy2_3dnow;
1735         }
1736     }
1737
1738     if (CONFIG_VP3_DECODER && (avctx->codec_id == AV_CODEC_ID_VP3 ||
1739                                avctx->codec_id == AV_CODEC_ID_THEORA)) {
1740         c->put_no_rnd_pixels_tab[1][1] = ff_put_no_rnd_pixels8_x2_exact_3dnow;
1741         c->put_no_rnd_pixels_tab[1][2] = ff_put_no_rnd_pixels8_y2_exact_3dnow;
1742     }
1743 #endif /* HAVE_YASM */
1744 }
1745
1746 static av_cold void dsputil_init_sse(DSPContext *c, AVCodecContext *avctx,
1747                                      int mm_flags)
1748 {
1749     const int high_bit_depth = avctx->bits_per_raw_sample > 8;
1750
1751 #if HAVE_INLINE_ASM
1752     if (!high_bit_depth) {
1753         if (!(CONFIG_MPEG_XVMC_DECODER && avctx->xvmc_acceleration > 1)) {
1754             /* XvMCCreateBlocks() may not allocate 16-byte aligned blocks */
1755             c->clear_block  = clear_block_sse;
1756             c->clear_blocks = clear_blocks_sse;
1757         }
1758     }
1759
1760     c->vector_clipf = vector_clipf_sse;
1761 #endif /* HAVE_INLINE_ASM */
1762
1763 #if HAVE_YASM
1764 #if HAVE_INLINE_ASM && CONFIG_VIDEODSP
1765     c->gmc = gmc_sse;
1766 #endif
1767 #endif /* HAVE_YASM */
1768 }
1769
1770 static av_cold void dsputil_init_sse2(DSPContext *c, AVCodecContext *avctx,
1771                                       int mm_flags)
1772 {
1773     const int bit_depth      = avctx->bits_per_raw_sample;
1774     const int high_bit_depth = bit_depth > 8;
1775
1776 #if HAVE_SSE2_INLINE
1777     if (!high_bit_depth && avctx->idct_algo == FF_IDCT_XVIDMMX) {
1778         c->idct_put              = ff_idct_xvid_sse2_put;
1779         c->idct_add              = ff_idct_xvid_sse2_add;
1780         c->idct                  = ff_idct_xvid_sse2;
1781         c->idct_permutation_type = FF_SSE2_IDCT_PERM;
1782     }
1783 #endif /* HAVE_SSE2_INLINE */
1784
1785 #if HAVE_SSE2_EXTERNAL
1786     if (!(mm_flags & AV_CPU_FLAG_SSE2SLOW)) {
1787         // these functions are slower than mmx on AMD, but faster on Intel
1788         if (!high_bit_depth) {
1789             c->put_pixels_tab[0][0]        = ff_put_pixels16_sse2;
1790             c->put_no_rnd_pixels_tab[0][0] = ff_put_pixels16_sse2;
1791             c->avg_pixels_tab[0][0]        = ff_avg_pixels16_sse2;
1792         }
1793     }
1794
1795     c->scalarproduct_int16          = ff_scalarproduct_int16_sse2;
1796     c->scalarproduct_and_madd_int16 = ff_scalarproduct_and_madd_int16_sse2;
1797     if (mm_flags & AV_CPU_FLAG_ATOM) {
1798         c->vector_clip_int32 = ff_vector_clip_int32_int_sse2;
1799     } else {
1800         c->vector_clip_int32 = ff_vector_clip_int32_sse2;
1801     }
1802     if (avctx->flags & CODEC_FLAG_BITEXACT) {
1803         c->apply_window_int16 = ff_apply_window_int16_sse2;
1804     } else if (!(mm_flags & AV_CPU_FLAG_SSE2SLOW)) {
1805         c->apply_window_int16 = ff_apply_window_int16_round_sse2;
1806     }
1807     c->bswap_buf = ff_bswap32_buf_sse2;
1808 #endif /* HAVE_SSE2_EXTERNAL */
1809 }
1810
1811 static av_cold void dsputil_init_ssse3(DSPContext *c, AVCodecContext *avctx,
1812                                        int mm_flags)
1813 {
1814 #if HAVE_SSSE3_EXTERNAL
1815     c->add_hfyu_left_prediction = ff_add_hfyu_left_prediction_ssse3;
1816     if (mm_flags & AV_CPU_FLAG_SSE4) // not really sse4, just slow on Conroe
1817         c->add_hfyu_left_prediction = ff_add_hfyu_left_prediction_sse4;
1818
1819     if (mm_flags & AV_CPU_FLAG_ATOM)
1820         c->apply_window_int16 = ff_apply_window_int16_ssse3_atom;
1821     else
1822         c->apply_window_int16 = ff_apply_window_int16_ssse3;
1823     if (!(mm_flags & (AV_CPU_FLAG_SSE42|AV_CPU_FLAG_3DNOW))) // cachesplit
1824         c->scalarproduct_and_madd_int16 = ff_scalarproduct_and_madd_int16_ssse3;
1825     c->bswap_buf = ff_bswap32_buf_ssse3;
1826 #endif /* HAVE_SSSE3_EXTERNAL */
1827 }
1828
1829 static av_cold void dsputil_init_sse4(DSPContext *c, AVCodecContext *avctx,
1830                                       int mm_flags)
1831 {
1832 #if HAVE_SSE4_EXTERNAL
1833     c->vector_clip_int32 = ff_vector_clip_int32_sse4;
1834 #endif /* HAVE_SSE4_EXTERNAL */
1835 }
1836
1837 av_cold void ff_dsputil_init_mmx(DSPContext *c, AVCodecContext *avctx)
1838 {
1839     int mm_flags = av_get_cpu_flags();
1840
1841 #if HAVE_7REGS && HAVE_INLINE_ASM
1842     if (mm_flags & AV_CPU_FLAG_CMOV)
1843         c->add_hfyu_median_prediction = add_hfyu_median_prediction_cmov;
1844 #endif
1845
1846     if (mm_flags & AV_CPU_FLAG_MMX) {
1847 #if HAVE_INLINE_ASM
1848         const int idct_algo = avctx->idct_algo;
1849
1850         if (avctx->lowres == 0 && avctx->bits_per_raw_sample <= 8) {
1851             if (idct_algo == FF_IDCT_AUTO || idct_algo == FF_IDCT_SIMPLEMMX) {
1852                 c->idct_put              = ff_simple_idct_put_mmx;
1853                 c->idct_add              = ff_simple_idct_add_mmx;
1854                 c->idct                  = ff_simple_idct_mmx;
1855                 c->idct_permutation_type = FF_SIMPLE_IDCT_PERM;
1856 #if CONFIG_GPL
1857             } else if (idct_algo == FF_IDCT_LIBMPEG2MMX) {
1858                 if (mm_flags & AV_CPU_FLAG_MMX2) {
1859                     c->idct_put = ff_libmpeg2mmx2_idct_put;
1860                     c->idct_add = ff_libmpeg2mmx2_idct_add;
1861                     c->idct     = ff_mmxext_idct;
1862                 } else {
1863                     c->idct_put = ff_libmpeg2mmx_idct_put;
1864                     c->idct_add = ff_libmpeg2mmx_idct_add;
1865                     c->idct     = ff_mmx_idct;
1866                 }
1867                 c->idct_permutation_type = FF_LIBMPEG2_IDCT_PERM;
1868 #endif
1869             } else if (idct_algo == FF_IDCT_XVIDMMX) {
1870                 if (mm_flags & AV_CPU_FLAG_SSE2) {
1871                     c->idct_put              = ff_idct_xvid_sse2_put;
1872                     c->idct_add              = ff_idct_xvid_sse2_add;
1873                     c->idct                  = ff_idct_xvid_sse2;
1874                     c->idct_permutation_type = FF_SSE2_IDCT_PERM;
1875                 } else if (mm_flags & AV_CPU_FLAG_MMXEXT) {
1876                     c->idct_put              = ff_idct_xvid_mmxext_put;
1877                     c->idct_add              = ff_idct_xvid_mmxext_add;
1878                     c->idct                  = ff_idct_xvid_mmxext;
1879                 } else {
1880                     c->idct_put              = ff_idct_xvid_mmx_put;
1881                     c->idct_add              = ff_idct_xvid_mmx_add;
1882                     c->idct                  = ff_idct_xvid_mmx;
1883                 }
1884             }
1885         }
1886 #endif /* HAVE_INLINE_ASM */
1887
1888         dsputil_init_mmx(c, avctx, mm_flags);
1889     }
1890
1891     if (mm_flags & AV_CPU_FLAG_MMXEXT)
1892         dsputil_init_mmxext(c, avctx, mm_flags);
1893
1894     if (mm_flags & AV_CPU_FLAG_3DNOW)
1895         dsputil_init_3dnow(c, avctx, mm_flags);
1896
1897     if (mm_flags & AV_CPU_FLAG_SSE)
1898         dsputil_init_sse(c, avctx, mm_flags);
1899
1900     if (mm_flags & AV_CPU_FLAG_SSE2)
1901         dsputil_init_sse2(c, avctx, mm_flags);
1902
1903     if (mm_flags & AV_CPU_FLAG_SSSE3)
1904         dsputil_init_ssse3(c, avctx, mm_flags);
1905
1906     if (mm_flags & AV_CPU_FLAG_SSE4)
1907         dsputil_init_sse4(c, avctx, mm_flags);
1908
1909     if (CONFIG_ENCODERS)
1910         ff_dsputilenc_init_mmx(c, avctx);
1911 }