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