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