x86: avcodec: Use convenience macros to check for CPU flags
[ffmpeg.git] / libavcodec / x86 / dsputil_init.c
1 /*
2  * This file is part of Libav.
3  *
4  * Libav is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * Libav is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with Libav; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 #include "config.h"
20 #include "libavutil/attributes.h"
21 #include "libavutil/cpu.h"
22 #include "libavutil/x86/asm.h"
23 #include "libavutil/x86/cpu.h"
24 #include "libavcodec/dsputil.h"
25 #include "libavcodec/simple_idct.h"
26 #include "dsputil_x86.h"
27 #include "idct_xvid.h"
28
29 void ff_put_pixels8_l2_mmxext(uint8_t *dst, uint8_t *src1, uint8_t *src2,
30                               int dstStride, int src1Stride, int h);
31 void ff_put_no_rnd_pixels8_l2_mmxext(uint8_t *dst, uint8_t *src1,
32                                      uint8_t *src2, int dstStride,
33                                      int src1Stride, int h);
34 void ff_avg_pixels8_l2_mmxext(uint8_t *dst, uint8_t *src1, uint8_t *src2,
35                               int dstStride, int src1Stride, int h);
36 void ff_put_pixels16_l2_mmxext(uint8_t *dst, uint8_t *src1, uint8_t *src2,
37                                int dstStride, int src1Stride, int h);
38 void ff_avg_pixels16_l2_mmxext(uint8_t *dst, uint8_t *src1, uint8_t *src2,
39                                int dstStride, int src1Stride, int h);
40 void ff_put_no_rnd_pixels16_l2_mmxext(uint8_t *dst, uint8_t *src1, uint8_t *src2,
41                                       int dstStride, int src1Stride, int h);
42 void ff_put_mpeg4_qpel16_h_lowpass_mmxext(uint8_t *dst, uint8_t *src,
43                                          int dstStride, int srcStride, int h);
44 void ff_avg_mpeg4_qpel16_h_lowpass_mmxext(uint8_t *dst, uint8_t *src,
45                                          int dstStride, int srcStride, int h);
46 void ff_put_no_rnd_mpeg4_qpel16_h_lowpass_mmxext(uint8_t *dst, uint8_t *src,
47                                                  int dstStride, int srcStride,
48                                                  int h);
49 void ff_put_mpeg4_qpel8_h_lowpass_mmxext(uint8_t *dst, uint8_t *src,
50                                         int dstStride, int srcStride, int h);
51 void ff_avg_mpeg4_qpel8_h_lowpass_mmxext(uint8_t *dst, uint8_t *src,
52                                         int dstStride, int srcStride, int h);
53 void ff_put_no_rnd_mpeg4_qpel8_h_lowpass_mmxext(uint8_t *dst, uint8_t *src,
54                                                 int dstStride, int srcStride,
55                                                 int h);
56 void ff_put_mpeg4_qpel16_v_lowpass_mmxext(uint8_t *dst, uint8_t *src,
57                                          int dstStride, int srcStride);
58 void ff_avg_mpeg4_qpel16_v_lowpass_mmxext(uint8_t *dst, uint8_t *src,
59                                          int dstStride, int srcStride);
60 void ff_put_no_rnd_mpeg4_qpel16_v_lowpass_mmxext(uint8_t *dst, uint8_t *src,
61                                                  int dstStride, int srcStride);
62 void ff_put_mpeg4_qpel8_v_lowpass_mmxext(uint8_t *dst, uint8_t *src,
63                                         int dstStride, int srcStride);
64 void ff_avg_mpeg4_qpel8_v_lowpass_mmxext(uint8_t *dst, uint8_t *src,
65                                         int dstStride, int srcStride);
66 void ff_put_no_rnd_mpeg4_qpel8_v_lowpass_mmxext(uint8_t *dst, uint8_t *src,
67                                                 int dstStride, int srcStride);
68 #define ff_put_no_rnd_pixels16_mmxext ff_put_pixels16_mmxext
69 #define ff_put_no_rnd_pixels8_mmxext ff_put_pixels8_mmxext
70
71 void ff_h263_v_loop_filter_mmx(uint8_t *src, int stride, int qscale);
72 void ff_h263_h_loop_filter_mmx(uint8_t *src, int stride, int qscale);
73
74 int32_t ff_scalarproduct_int16_mmxext(const int16_t *v1, const int16_t *v2,
75                                       int order);
76 int32_t ff_scalarproduct_int16_sse2(const int16_t *v1, const int16_t *v2,
77                                     int order);
78 int32_t ff_scalarproduct_and_madd_int16_mmxext(int16_t *v1, const int16_t *v2,
79                                                const int16_t *v3,
80                                                int order, int mul);
81 int32_t ff_scalarproduct_and_madd_int16_sse2(int16_t *v1, const int16_t *v2,
82                                              const int16_t *v3,
83                                              int order, int mul);
84 int32_t ff_scalarproduct_and_madd_int16_ssse3(int16_t *v1, const int16_t *v2,
85                                               const int16_t *v3,
86                                               int order, int mul);
87
88 void ff_apply_window_int16_round_mmxext(int16_t *output, const int16_t *input,
89                                         const int16_t *window, unsigned int len);
90 void ff_apply_window_int16_round_sse2(int16_t *output, const int16_t *input,
91                                       const int16_t *window, unsigned int len);
92 void ff_apply_window_int16_mmxext(int16_t *output, const int16_t *input,
93                                   const int16_t *window, unsigned int len);
94 void ff_apply_window_int16_sse2(int16_t *output, const int16_t *input,
95                                 const int16_t *window, unsigned int len);
96 void ff_apply_window_int16_ssse3(int16_t *output, const int16_t *input,
97                                  const int16_t *window, unsigned int len);
98 void ff_apply_window_int16_ssse3_atom(int16_t *output, const int16_t *input,
99                                       const int16_t *window, unsigned int len);
100
101 void ff_bswap32_buf_ssse3(uint32_t *dst, const uint32_t *src, int w);
102 void ff_bswap32_buf_sse2(uint32_t *dst, const uint32_t *src, int w);
103
104 void ff_add_hfyu_median_prediction_mmxext(uint8_t *dst, const uint8_t *top,
105                                           const uint8_t *diff, int w,
106                                           int *left, int *left_top);
107 int  ff_add_hfyu_left_prediction_ssse3(uint8_t *dst, const uint8_t *src,
108                                        int w, int left);
109 int  ff_add_hfyu_left_prediction_sse4(uint8_t *dst, const uint8_t *src,
110                                       int w, int left);
111
112 void ff_vector_clip_int32_mmx     (int32_t *dst, const int32_t *src,
113                                    int32_t min, int32_t max, unsigned int len);
114 void ff_vector_clip_int32_sse2    (int32_t *dst, const int32_t *src,
115                                    int32_t min, int32_t max, unsigned int len);
116 void ff_vector_clip_int32_int_sse2(int32_t *dst, const int32_t *src,
117                                    int32_t min, int32_t max, unsigned int len);
118 void ff_vector_clip_int32_sse4    (int32_t *dst, const int32_t *src,
119                                    int32_t min, int32_t max, unsigned int len);
120
121 #if HAVE_YASM
122
123 PIXELS16(static, ff_avg, , , _mmxext)
124 PIXELS16(static, ff_put, , , _mmxext)
125
126 #define QPEL_OP(OPNAME, RND, MMX)                                       \
127 static void OPNAME ## qpel8_mc00_ ## MMX (uint8_t *dst, uint8_t *src,   \
128                                           ptrdiff_t stride)             \
129 {                                                                       \
130     ff_ ## OPNAME ## pixels8_ ## MMX(dst, src, stride, 8);              \
131 }                                                                       \
132                                                                         \
133 static void OPNAME ## qpel8_mc10_ ## MMX(uint8_t *dst, uint8_t *src,    \
134                                          ptrdiff_t stride)              \
135 {                                                                       \
136     uint64_t temp[8];                                                   \
137     uint8_t * const half = (uint8_t*)temp;                              \
138     ff_put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(half, src, 8,        \
139                                                    stride, 8);          \
140     ff_ ## OPNAME ## pixels8_l2_ ## MMX(dst, src, half,                 \
141                                         stride, stride, 8);             \
142 }                                                                       \
143                                                                         \
144 static void OPNAME ## qpel8_mc20_ ## MMX(uint8_t *dst, uint8_t *src,    \
145                                          ptrdiff_t stride)              \
146 {                                                                       \
147     ff_ ## OPNAME ## mpeg4_qpel8_h_lowpass_ ## MMX(dst, src, stride,    \
148                                                    stride, 8);          \
149 }                                                                       \
150                                                                         \
151 static void OPNAME ## qpel8_mc30_ ## MMX(uint8_t *dst, uint8_t *src,    \
152                                          ptrdiff_t stride)              \
153 {                                                                       \
154     uint64_t temp[8];                                                   \
155     uint8_t * const half = (uint8_t*)temp;                              \
156     ff_put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(half, src, 8,        \
157                                                    stride, 8);          \
158     ff_ ## OPNAME ## pixels8_l2_ ## MMX(dst, src + 1, half, stride,     \
159                                         stride, 8);                     \
160 }                                                                       \
161                                                                         \
162 static void OPNAME ## qpel8_mc01_ ## MMX(uint8_t *dst, uint8_t *src,    \
163                                          ptrdiff_t stride)              \
164 {                                                                       \
165     uint64_t temp[8];                                                   \
166     uint8_t * const half = (uint8_t*)temp;                              \
167     ff_put ## RND ## mpeg4_qpel8_v_lowpass_ ## MMX(half, src,           \
168                                                    8, stride);          \
169     ff_ ## OPNAME ## pixels8_l2_ ## MMX(dst, src, half,                 \
170                                         stride, stride, 8);             \
171 }                                                                       \
172                                                                         \
173 static void OPNAME ## qpel8_mc02_ ## MMX(uint8_t *dst, uint8_t *src,    \
174                                          ptrdiff_t stride)              \
175 {                                                                       \
176     ff_ ## OPNAME ## mpeg4_qpel8_v_lowpass_ ## MMX(dst, src,            \
177                                                    stride, stride);     \
178 }                                                                       \
179                                                                         \
180 static void OPNAME ## qpel8_mc03_ ## MMX(uint8_t *dst, uint8_t *src,    \
181                                          ptrdiff_t stride)              \
182 {                                                                       \
183     uint64_t temp[8];                                                   \
184     uint8_t * const half = (uint8_t*)temp;                              \
185     ff_put ## RND ## mpeg4_qpel8_v_lowpass_ ## MMX(half, src,           \
186                                                    8, stride);          \
187     ff_ ## OPNAME ## pixels8_l2_ ## MMX(dst, src + stride, half, stride,\
188                                         stride, 8);                     \
189 }                                                                       \
190                                                                         \
191 static void OPNAME ## qpel8_mc11_ ## MMX(uint8_t *dst, uint8_t *src,    \
192                                          ptrdiff_t stride)              \
193 {                                                                       \
194     uint64_t half[8 + 9];                                               \
195     uint8_t * const halfH  = ((uint8_t*)half) + 64;                     \
196     uint8_t * const halfHV = ((uint8_t*)half);                          \
197     ff_put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8,       \
198                                                    stride, 9);          \
199     ff_put ## RND ## pixels8_l2_ ## MMX(halfH, src, halfH, 8,           \
200                                         stride, 9);                     \
201     ff_put ## RND ## mpeg4_qpel8_v_lowpass_ ## MMX(halfHV, halfH, 8, 8);\
202     ff_ ## OPNAME ## pixels8_l2_ ## MMX(dst, halfH, halfHV,             \
203                                         stride, 8, 8);                  \
204 }                                                                       \
205                                                                         \
206 static void OPNAME ## qpel8_mc31_ ## MMX(uint8_t *dst, uint8_t *src,    \
207                                          ptrdiff_t stride)              \
208 {                                                                       \
209     uint64_t half[8 + 9];                                               \
210     uint8_t * const halfH  = ((uint8_t*)half) + 64;                     \
211     uint8_t * const halfHV = ((uint8_t*)half);                          \
212     ff_put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8,       \
213                                                    stride, 9);          \
214     ff_put ## RND ## pixels8_l2_ ## MMX(halfH, src + 1, halfH, 8,       \
215                                         stride, 9);                     \
216     ff_put ## RND ## mpeg4_qpel8_v_lowpass_ ## MMX(halfHV, halfH, 8, 8);\
217     ff_ ## OPNAME ## pixels8_l2_ ## MMX(dst, halfH, halfHV,             \
218                                         stride, 8, 8);                  \
219 }                                                                       \
220                                                                         \
221 static void OPNAME ## qpel8_mc13_ ## MMX(uint8_t *dst, uint8_t *src,    \
222                                          ptrdiff_t stride)              \
223 {                                                                       \
224     uint64_t half[8 + 9];                                               \
225     uint8_t * const halfH  = ((uint8_t*)half) + 64;                     \
226     uint8_t * const halfHV = ((uint8_t*)half);                          \
227     ff_put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8,       \
228                                                    stride, 9);          \
229     ff_put ## RND ## pixels8_l2_ ## MMX(halfH, src, halfH, 8,           \
230                                         stride, 9);                     \
231     ff_put ## RND ## mpeg4_qpel8_v_lowpass_ ## MMX(halfHV, halfH, 8, 8);\
232     ff_ ## OPNAME ## pixels8_l2_ ## MMX(dst, halfH + 8, halfHV,         \
233                                         stride, 8, 8);                  \
234 }                                                                       \
235                                                                         \
236 static void OPNAME ## qpel8_mc33_ ## MMX(uint8_t *dst, uint8_t *src,    \
237                                          ptrdiff_t stride)              \
238 {                                                                       \
239     uint64_t half[8 + 9];                                               \
240     uint8_t * const halfH  = ((uint8_t*)half) + 64;                     \
241     uint8_t * const halfHV = ((uint8_t*)half);                          \
242     ff_put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8,       \
243                                                    stride, 9);          \
244     ff_put ## RND ## pixels8_l2_ ## MMX(halfH, src + 1, halfH, 8,       \
245                                         stride, 9);                     \
246     ff_put ## RND ## mpeg4_qpel8_v_lowpass_ ## MMX(halfHV, halfH, 8, 8);\
247     ff_ ## OPNAME ## pixels8_l2_ ## MMX(dst, halfH + 8, halfHV,         \
248                                         stride, 8, 8);                  \
249 }                                                                       \
250                                                                         \
251 static void OPNAME ## qpel8_mc21_ ## MMX(uint8_t *dst, uint8_t *src,    \
252                                          ptrdiff_t stride)              \
253 {                                                                       \
254     uint64_t half[8 + 9];                                               \
255     uint8_t * const halfH  = ((uint8_t*)half) + 64;                     \
256     uint8_t * const halfHV = ((uint8_t*)half);                          \
257     ff_put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8,       \
258                                                    stride, 9);          \
259     ff_put ## RND ## mpeg4_qpel8_v_lowpass_ ## MMX(halfHV, halfH, 8, 8);\
260     ff_ ## OPNAME ## pixels8_l2_ ## MMX(dst, halfH, halfHV,             \
261                                         stride, 8, 8);                  \
262 }                                                                       \
263                                                                         \
264 static void OPNAME ## qpel8_mc23_ ## MMX(uint8_t *dst, uint8_t *src,    \
265                                          ptrdiff_t stride)              \
266 {                                                                       \
267     uint64_t half[8 + 9];                                               \
268     uint8_t * const halfH  = ((uint8_t*)half) + 64;                     \
269     uint8_t * const halfHV = ((uint8_t*)half);                          \
270     ff_put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8,       \
271                                                    stride, 9);          \
272     ff_put ## RND ## mpeg4_qpel8_v_lowpass_ ## MMX(halfHV, halfH, 8, 8);\
273     ff_ ## OPNAME ## pixels8_l2_ ## MMX(dst, halfH + 8, halfHV,         \
274                                         stride, 8, 8);                  \
275 }                                                                       \
276                                                                         \
277 static void OPNAME ## qpel8_mc12_ ## MMX(uint8_t *dst, uint8_t *src,    \
278                                          ptrdiff_t stride)              \
279 {                                                                       \
280     uint64_t half[8 + 9];                                               \
281     uint8_t * const halfH = ((uint8_t*)half);                           \
282     ff_put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8,       \
283                                                    stride, 9);          \
284     ff_put ## RND ## pixels8_l2_ ## MMX(halfH, src, halfH,              \
285                                         8, stride, 9);                  \
286     ff_ ## OPNAME ## mpeg4_qpel8_v_lowpass_ ## MMX(dst, halfH,          \
287                                                    stride, 8);          \
288 }                                                                       \
289                                                                         \
290 static void OPNAME ## qpel8_mc32_ ## MMX(uint8_t *dst, uint8_t *src,    \
291                                          ptrdiff_t stride)              \
292 {                                                                       \
293     uint64_t half[8 + 9];                                               \
294     uint8_t * const halfH = ((uint8_t*)half);                           \
295     ff_put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8,       \
296                                                    stride, 9);          \
297     ff_put ## RND ## pixels8_l2_ ## MMX(halfH, src + 1, halfH, 8,       \
298                                         stride, 9);                     \
299     ff_ ## OPNAME ## mpeg4_qpel8_v_lowpass_ ## MMX(dst, halfH,          \
300                                                    stride, 8);          \
301 }                                                                       \
302                                                                         \
303 static void OPNAME ## qpel8_mc22_ ## MMX(uint8_t *dst, uint8_t *src,    \
304                                          ptrdiff_t stride)              \
305 {                                                                       \
306     uint64_t half[9];                                                   \
307     uint8_t * const halfH = ((uint8_t*)half);                           \
308     ff_put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8,       \
309                                                    stride, 9);          \
310     ff_ ## OPNAME ## mpeg4_qpel8_v_lowpass_ ## MMX(dst, halfH,          \
311                                                    stride, 8);          \
312 }                                                                       \
313                                                                         \
314 static void OPNAME ## qpel16_mc00_ ## MMX (uint8_t *dst, uint8_t *src,  \
315                                            ptrdiff_t stride)            \
316 {                                                                       \
317     ff_ ## OPNAME ## pixels16_ ## MMX(dst, src, stride, 16);            \
318 }                                                                       \
319                                                                         \
320 static void OPNAME ## qpel16_mc10_ ## MMX(uint8_t *dst, uint8_t *src,   \
321                                           ptrdiff_t stride)             \
322 {                                                                       \
323     uint64_t temp[32];                                                  \
324     uint8_t * const half = (uint8_t*)temp;                              \
325     ff_put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(half, src, 16,      \
326                                                     stride, 16);        \
327     ff_ ## OPNAME ## pixels16_l2_ ## MMX(dst, src, half, stride,        \
328                                          stride, 16);                   \
329 }                                                                       \
330                                                                         \
331 static void OPNAME ## qpel16_mc20_ ## MMX(uint8_t *dst, uint8_t *src,   \
332                                           ptrdiff_t stride)             \
333 {                                                                       \
334     ff_ ## OPNAME ## mpeg4_qpel16_h_lowpass_ ## MMX(dst, src,           \
335                                                     stride, stride, 16);\
336 }                                                                       \
337                                                                         \
338 static void OPNAME ## qpel16_mc30_ ## MMX(uint8_t *dst, uint8_t *src,   \
339                                           ptrdiff_t stride)             \
340 {                                                                       \
341     uint64_t temp[32];                                                  \
342     uint8_t * const half = (uint8_t*)temp;                              \
343     ff_put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(half, src, 16,      \
344                                                     stride, 16);        \
345     ff_ ## OPNAME ## pixels16_l2_ ## MMX(dst, src + 1, half,            \
346                                          stride, stride, 16);           \
347 }                                                                       \
348                                                                         \
349 static void OPNAME ## qpel16_mc01_ ## MMX(uint8_t *dst, uint8_t *src,   \
350                                           ptrdiff_t stride)             \
351 {                                                                       \
352     uint64_t temp[32];                                                  \
353     uint8_t * const half = (uint8_t*)temp;                              \
354     ff_put ## RND ## mpeg4_qpel16_v_lowpass_ ## MMX(half, src, 16,      \
355                                                     stride);            \
356     ff_ ## OPNAME ## pixels16_l2_ ## MMX(dst, src, half, stride,        \
357                                          stride, 16);                   \
358 }                                                                       \
359                                                                         \
360 static void OPNAME ## qpel16_mc02_ ## MMX(uint8_t *dst, uint8_t *src,   \
361                                           ptrdiff_t stride)             \
362 {                                                                       \
363     ff_ ## OPNAME ## mpeg4_qpel16_v_lowpass_ ## MMX(dst, src,           \
364                                                     stride, stride);    \
365 }                                                                       \
366                                                                         \
367 static void OPNAME ## qpel16_mc03_ ## MMX(uint8_t *dst, uint8_t *src,   \
368                                           ptrdiff_t stride)             \
369 {                                                                       \
370     uint64_t temp[32];                                                  \
371     uint8_t * const half = (uint8_t*)temp;                              \
372     ff_put ## RND ## mpeg4_qpel16_v_lowpass_ ## MMX(half, src, 16,      \
373                                                     stride);            \
374     ff_ ## OPNAME ## pixels16_l2_ ## MMX(dst, src+stride, half,         \
375                                          stride, stride, 16);           \
376 }                                                                       \
377                                                                         \
378 static void OPNAME ## qpel16_mc11_ ## MMX(uint8_t *dst, uint8_t *src,   \
379                                           ptrdiff_t stride)             \
380 {                                                                       \
381     uint64_t half[16 * 2 + 17 * 2];                                     \
382     uint8_t * const halfH  = ((uint8_t*)half) + 256;                    \
383     uint8_t * const halfHV = ((uint8_t*)half);                          \
384     ff_put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16,     \
385                                                     stride, 17);        \
386     ff_put ## RND ## pixels16_l2_ ## MMX(halfH, src, halfH, 16,         \
387                                          stride, 17);                   \
388     ff_put ## RND ## mpeg4_qpel16_v_lowpass_ ## MMX(halfHV, halfH,      \
389                                                     16, 16);            \
390     ff_ ## OPNAME ## pixels16_l2_ ## MMX(dst, halfH, halfHV,            \
391                                          stride, 16, 16);               \
392 }                                                                       \
393                                                                         \
394 static void OPNAME ## qpel16_mc31_ ## MMX(uint8_t *dst, uint8_t *src,   \
395                                           ptrdiff_t stride)             \
396 {                                                                       \
397     uint64_t half[16 * 2 + 17 * 2];                                     \
398     uint8_t * const halfH  = ((uint8_t*)half) + 256;                    \
399     uint8_t * const halfHV = ((uint8_t*)half);                          \
400     ff_put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16,     \
401                                                     stride, 17);        \
402     ff_put ## RND ## pixels16_l2_ ## MMX(halfH, src + 1, halfH, 16,     \
403                                          stride, 17);                   \
404     ff_put ## RND ## mpeg4_qpel16_v_lowpass_ ## MMX(halfHV, halfH,      \
405                                                     16, 16);            \
406     ff_ ## OPNAME ## pixels16_l2_ ## MMX(dst, halfH, halfHV,            \
407                                          stride, 16, 16);               \
408 }                                                                       \
409                                                                         \
410 static void OPNAME ## qpel16_mc13_ ## MMX(uint8_t *dst, uint8_t *src,   \
411                                           ptrdiff_t stride)             \
412 {                                                                       \
413     uint64_t half[16 * 2 + 17 * 2];                                     \
414     uint8_t * const halfH  = ((uint8_t*)half) + 256;                    \
415     uint8_t * const halfHV = ((uint8_t*)half);                          \
416     ff_put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16,     \
417                                                     stride, 17);        \
418     ff_put ## RND ## pixels16_l2_ ## MMX(halfH, src, halfH, 16,         \
419                                          stride, 17);                   \
420     ff_put ## RND ## mpeg4_qpel16_v_lowpass_ ## MMX(halfHV, halfH,      \
421                                                     16, 16);            \
422     ff_ ## OPNAME ## pixels16_l2_ ## MMX(dst, halfH + 16, halfHV,       \
423                                          stride, 16, 16);               \
424 }                                                                       \
425                                                                         \
426 static void OPNAME ## qpel16_mc33_ ## MMX(uint8_t *dst, uint8_t *src,   \
427                                           ptrdiff_t stride)             \
428 {                                                                       \
429     uint64_t half[16 * 2 + 17 * 2];                                     \
430     uint8_t * const halfH  = ((uint8_t*)half) + 256;                    \
431     uint8_t * const halfHV = ((uint8_t*)half);                          \
432     ff_put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16,     \
433                                                     stride, 17);        \
434     ff_put ## RND ## pixels16_l2_ ## MMX(halfH, src + 1, halfH, 16,     \
435                                          stride, 17);                   \
436     ff_put ## RND ## mpeg4_qpel16_v_lowpass_ ## MMX(halfHV, halfH,      \
437                                                     16, 16);            \
438     ff_ ## OPNAME ## pixels16_l2_ ## MMX(dst, halfH + 16, halfHV,       \
439                                          stride, 16, 16);               \
440 }                                                                       \
441                                                                         \
442 static void OPNAME ## qpel16_mc21_ ## MMX(uint8_t *dst, uint8_t *src,   \
443                                           ptrdiff_t stride)             \
444 {                                                                       \
445     uint64_t half[16 * 2 + 17 * 2];                                     \
446     uint8_t * const halfH  = ((uint8_t*)half) + 256;                    \
447     uint8_t * const halfHV = ((uint8_t*)half);                          \
448     ff_put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16,     \
449                                                     stride, 17);        \
450     ff_put ## RND ## mpeg4_qpel16_v_lowpass_ ## MMX(halfHV, halfH,      \
451                                                     16, 16);            \
452     ff_ ## OPNAME ## pixels16_l2_ ## MMX(dst, halfH, halfHV,            \
453                                          stride, 16, 16);               \
454 }                                                                       \
455                                                                         \
456 static void OPNAME ## qpel16_mc23_ ## MMX(uint8_t *dst, uint8_t *src,   \
457                                           ptrdiff_t stride)             \
458 {                                                                       \
459     uint64_t half[16 * 2 + 17 * 2];                                     \
460     uint8_t * const halfH  = ((uint8_t*)half) + 256;                    \
461     uint8_t * const halfHV = ((uint8_t*)half);                          \
462     ff_put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16,     \
463                                                     stride, 17);        \
464     ff_put ## RND ## mpeg4_qpel16_v_lowpass_ ## MMX(halfHV, halfH,      \
465                                                     16, 16);            \
466     ff_ ## OPNAME ## pixels16_l2_ ## MMX(dst, halfH + 16, halfHV,       \
467                                          stride, 16, 16);               \
468 }                                                                       \
469                                                                         \
470 static void OPNAME ## qpel16_mc12_ ## MMX(uint8_t *dst, uint8_t *src,   \
471                                           ptrdiff_t stride)             \
472 {                                                                       \
473     uint64_t half[17 * 2];                                              \
474     uint8_t * const halfH = ((uint8_t*)half);                           \
475     ff_put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16,     \
476                                                     stride, 17);        \
477     ff_put ## RND ## pixels16_l2_ ## MMX(halfH, src, halfH, 16,         \
478                                          stride, 17);                   \
479     ff_ ## OPNAME ## mpeg4_qpel16_v_lowpass_ ## MMX(dst, halfH,         \
480                                                     stride, 16);        \
481 }                                                                       \
482                                                                         \
483 static void OPNAME ## qpel16_mc32_ ## MMX(uint8_t *dst, uint8_t *src,   \
484                                           ptrdiff_t stride)             \
485 {                                                                       \
486     uint64_t half[17 * 2];                                              \
487     uint8_t * const halfH = ((uint8_t*)half);                           \
488     ff_put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16,     \
489                                                     stride, 17);        \
490     ff_put ## RND ## pixels16_l2_ ## MMX(halfH, src + 1, halfH, 16,     \
491                                          stride, 17);                   \
492     ff_ ## OPNAME ## mpeg4_qpel16_v_lowpass_ ## MMX(dst, halfH,         \
493                                                     stride, 16);        \
494 }                                                                       \
495                                                                         \
496 static void OPNAME ## qpel16_mc22_ ## MMX(uint8_t *dst, uint8_t *src,   \
497                                           ptrdiff_t stride)             \
498 {                                                                       \
499     uint64_t half[17 * 2];                                              \
500     uint8_t * const halfH = ((uint8_t*)half);                           \
501     ff_put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16,     \
502                                                     stride, 17);        \
503     ff_ ## OPNAME ## mpeg4_qpel16_v_lowpass_ ## MMX(dst, halfH,         \
504                                                     stride, 16);        \
505 }
506
507 QPEL_OP(put_,        _,        mmxext)
508 QPEL_OP(avg_,        _,        mmxext)
509 QPEL_OP(put_no_rnd_, _no_rnd_, mmxext)
510 #endif /* HAVE_YASM */
511
512 #define SET_QPEL_FUNCS(PFX, IDX, SIZE, CPU, PREFIX)                          \
513     do {                                                                     \
514     c->PFX ## _pixels_tab[IDX][ 0] = PREFIX ## PFX ## SIZE ## _mc00_ ## CPU; \
515     c->PFX ## _pixels_tab[IDX][ 1] = PREFIX ## PFX ## SIZE ## _mc10_ ## CPU; \
516     c->PFX ## _pixels_tab[IDX][ 2] = PREFIX ## PFX ## SIZE ## _mc20_ ## CPU; \
517     c->PFX ## _pixels_tab[IDX][ 3] = PREFIX ## PFX ## SIZE ## _mc30_ ## CPU; \
518     c->PFX ## _pixels_tab[IDX][ 4] = PREFIX ## PFX ## SIZE ## _mc01_ ## CPU; \
519     c->PFX ## _pixels_tab[IDX][ 5] = PREFIX ## PFX ## SIZE ## _mc11_ ## CPU; \
520     c->PFX ## _pixels_tab[IDX][ 6] = PREFIX ## PFX ## SIZE ## _mc21_ ## CPU; \
521     c->PFX ## _pixels_tab[IDX][ 7] = PREFIX ## PFX ## SIZE ## _mc31_ ## CPU; \
522     c->PFX ## _pixels_tab[IDX][ 8] = PREFIX ## PFX ## SIZE ## _mc02_ ## CPU; \
523     c->PFX ## _pixels_tab[IDX][ 9] = PREFIX ## PFX ## SIZE ## _mc12_ ## CPU; \
524     c->PFX ## _pixels_tab[IDX][10] = PREFIX ## PFX ## SIZE ## _mc22_ ## CPU; \
525     c->PFX ## _pixels_tab[IDX][11] = PREFIX ## PFX ## SIZE ## _mc32_ ## CPU; \
526     c->PFX ## _pixels_tab[IDX][12] = PREFIX ## PFX ## SIZE ## _mc03_ ## CPU; \
527     c->PFX ## _pixels_tab[IDX][13] = PREFIX ## PFX ## SIZE ## _mc13_ ## CPU; \
528     c->PFX ## _pixels_tab[IDX][14] = PREFIX ## PFX ## SIZE ## _mc23_ ## CPU; \
529     c->PFX ## _pixels_tab[IDX][15] = PREFIX ## PFX ## SIZE ## _mc33_ ## CPU; \
530     } while (0)
531
532 static av_cold void dsputil_init_mmx(DSPContext *c, AVCodecContext *avctx,
533                                      int cpu_flags)
534 {
535 #if HAVE_MMX_INLINE
536     const int high_bit_depth = avctx->bits_per_raw_sample > 8;
537
538     c->put_pixels_clamped        = ff_put_pixels_clamped_mmx;
539     c->put_signed_pixels_clamped = ff_put_signed_pixels_clamped_mmx;
540     c->add_pixels_clamped        = ff_add_pixels_clamped_mmx;
541
542     if (!high_bit_depth) {
543         c->clear_block  = ff_clear_block_mmx;
544         c->clear_blocks = ff_clear_blocks_mmx;
545         c->draw_edges   = ff_draw_edges_mmx;
546
547         switch (avctx->idct_algo) {
548         case FF_IDCT_AUTO:
549         case FF_IDCT_SIMPLEMMX:
550             c->idct_put              = ff_simple_idct_put_mmx;
551             c->idct_add              = ff_simple_idct_add_mmx;
552             c->idct                  = ff_simple_idct_mmx;
553             c->idct_permutation_type = FF_SIMPLE_IDCT_PERM;
554             break;
555         case FF_IDCT_XVIDMMX:
556             c->idct_put              = ff_idct_xvid_mmx_put;
557             c->idct_add              = ff_idct_xvid_mmx_add;
558             c->idct                  = ff_idct_xvid_mmx;
559             break;
560         }
561     }
562
563     c->gmc = ff_gmc_mmx;
564
565     c->add_bytes = ff_add_bytes_mmx;
566 #endif /* HAVE_MMX_INLINE */
567
568 #if HAVE_MMX_EXTERNAL
569     if (CONFIG_H263_DECODER || CONFIG_H263_ENCODER) {
570         c->h263_v_loop_filter = ff_h263_v_loop_filter_mmx;
571         c->h263_h_loop_filter = ff_h263_h_loop_filter_mmx;
572     }
573
574     c->vector_clip_int32 = ff_vector_clip_int32_mmx;
575 #endif /* HAVE_MMX_EXTERNAL */
576 }
577
578 static av_cold void dsputil_init_mmxext(DSPContext *c, AVCodecContext *avctx,
579                                         int cpu_flags)
580 {
581 #if HAVE_MMXEXT_INLINE
582     const int high_bit_depth = avctx->bits_per_raw_sample > 8;
583
584     if (!high_bit_depth && avctx->idct_algo == FF_IDCT_XVIDMMX) {
585         c->idct_put = ff_idct_xvid_mmxext_put;
586         c->idct_add = ff_idct_xvid_mmxext_add;
587         c->idct     = ff_idct_xvid_mmxext;
588     }
589 #endif /* HAVE_MMXEXT_INLINE */
590
591 #if HAVE_MMXEXT_EXTERNAL
592     SET_QPEL_FUNCS(avg_qpel,        0, 16, mmxext, );
593     SET_QPEL_FUNCS(avg_qpel,        1,  8, mmxext, );
594
595     SET_QPEL_FUNCS(put_qpel,        0, 16, mmxext, );
596     SET_QPEL_FUNCS(put_qpel,        1,  8, mmxext, );
597     SET_QPEL_FUNCS(put_no_rnd_qpel, 0, 16, mmxext, );
598     SET_QPEL_FUNCS(put_no_rnd_qpel, 1,  8, mmxext, );
599
600     /* slower than cmov version on AMD */
601     if (!(cpu_flags & AV_CPU_FLAG_3DNOW))
602         c->add_hfyu_median_prediction = ff_add_hfyu_median_prediction_mmxext;
603
604     c->scalarproduct_int16          = ff_scalarproduct_int16_mmxext;
605     c->scalarproduct_and_madd_int16 = ff_scalarproduct_and_madd_int16_mmxext;
606
607     if (avctx->flags & CODEC_FLAG_BITEXACT) {
608         c->apply_window_int16 = ff_apply_window_int16_mmxext;
609     } else {
610         c->apply_window_int16 = ff_apply_window_int16_round_mmxext;
611     }
612 #endif /* HAVE_MMXEXT_EXTERNAL */
613 }
614
615 static av_cold void dsputil_init_sse(DSPContext *c, AVCodecContext *avctx,
616                                      int cpu_flags)
617 {
618 #if HAVE_SSE_INLINE
619     const int high_bit_depth = avctx->bits_per_raw_sample > 8;
620
621     if (!high_bit_depth) {
622         if (!(CONFIG_MPEG_XVMC_DECODER && avctx->xvmc_acceleration > 1)) {
623             /* XvMCCreateBlocks() may not allocate 16-byte aligned blocks */
624             c->clear_block  = ff_clear_block_sse;
625             c->clear_blocks = ff_clear_blocks_sse;
626         }
627     }
628
629     c->vector_clipf = ff_vector_clipf_sse;
630 #endif /* HAVE_SSE_INLINE */
631 }
632
633 static av_cold void dsputil_init_sse2(DSPContext *c, AVCodecContext *avctx,
634                                       int cpu_flags)
635 {
636 #if HAVE_SSE2_INLINE
637     const int high_bit_depth = avctx->bits_per_raw_sample > 8;
638
639     if (!high_bit_depth && avctx->idct_algo == FF_IDCT_XVIDMMX) {
640         c->idct_put              = ff_idct_xvid_sse2_put;
641         c->idct_add              = ff_idct_xvid_sse2_add;
642         c->idct                  = ff_idct_xvid_sse2;
643         c->idct_permutation_type = FF_SSE2_IDCT_PERM;
644     }
645 #endif /* HAVE_SSE2_INLINE */
646
647 #if HAVE_SSE2_EXTERNAL
648     c->scalarproduct_int16          = ff_scalarproduct_int16_sse2;
649     c->scalarproduct_and_madd_int16 = ff_scalarproduct_and_madd_int16_sse2;
650     if (cpu_flags & AV_CPU_FLAG_ATOM) {
651         c->vector_clip_int32 = ff_vector_clip_int32_int_sse2;
652     } else {
653         c->vector_clip_int32 = ff_vector_clip_int32_sse2;
654     }
655     if (avctx->flags & CODEC_FLAG_BITEXACT) {
656         c->apply_window_int16 = ff_apply_window_int16_sse2;
657     } else if (!(cpu_flags & AV_CPU_FLAG_SSE2SLOW)) {
658         c->apply_window_int16 = ff_apply_window_int16_round_sse2;
659     }
660     c->bswap_buf = ff_bswap32_buf_sse2;
661 #endif /* HAVE_SSE2_EXTERNAL */
662 }
663
664 static av_cold void dsputil_init_ssse3(DSPContext *c, AVCodecContext *avctx,
665                                        int cpu_flags)
666 {
667 #if HAVE_SSSE3_EXTERNAL
668     c->add_hfyu_left_prediction = ff_add_hfyu_left_prediction_ssse3;
669     if (cpu_flags & AV_CPU_FLAG_SSE4) // not really SSE4, just slow on Conroe
670         c->add_hfyu_left_prediction = ff_add_hfyu_left_prediction_sse4;
671
672     if (cpu_flags & AV_CPU_FLAG_ATOM)
673         c->apply_window_int16 = ff_apply_window_int16_ssse3_atom;
674     else
675         c->apply_window_int16 = ff_apply_window_int16_ssse3;
676     if (!(cpu_flags & (AV_CPU_FLAG_SSE42 | AV_CPU_FLAG_3DNOW))) // cachesplit
677         c->scalarproduct_and_madd_int16 = ff_scalarproduct_and_madd_int16_ssse3;
678     c->bswap_buf = ff_bswap32_buf_ssse3;
679 #endif /* HAVE_SSSE3_EXTERNAL */
680 }
681
682 static av_cold void dsputil_init_sse4(DSPContext *c, AVCodecContext *avctx,
683                                       int cpu_flags)
684 {
685 #if HAVE_SSE4_EXTERNAL
686     c->vector_clip_int32 = ff_vector_clip_int32_sse4;
687 #endif /* HAVE_SSE4_EXTERNAL */
688 }
689
690 av_cold void ff_dsputil_init_x86(DSPContext *c, AVCodecContext *avctx)
691 {
692     int cpu_flags = av_get_cpu_flags();
693
694 #if HAVE_7REGS && HAVE_INLINE_ASM
695     if (cpu_flags & AV_CPU_FLAG_CMOV)
696         c->add_hfyu_median_prediction = ff_add_hfyu_median_prediction_cmov;
697 #endif
698
699     if (X86_MMX(cpu_flags))
700         dsputil_init_mmx(c, avctx, cpu_flags);
701
702     if (X86_MMXEXT(cpu_flags))
703         dsputil_init_mmxext(c, avctx, cpu_flags);
704
705     if (X86_SSE(cpu_flags))
706         dsputil_init_sse(c, avctx, cpu_flags);
707
708     if (X86_SSE2(cpu_flags))
709         dsputil_init_sse2(c, avctx, cpu_flags);
710
711     if (EXTERNAL_SSSE3(cpu_flags))
712         dsputil_init_ssse3(c, avctx, cpu_flags);
713
714     if (EXTERNAL_SSE4(cpu_flags))
715         dsputil_init_sse4(c, avctx, cpu_flags);
716
717     if (CONFIG_ENCODERS)
718         ff_dsputilenc_init_mmx(c, avctx);
719 }