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