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