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