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