]> git.sesse.net Git - ffmpeg/blob - libavcodec/x86/dsputil_init.c
Merge commit '4cb4680c1087a2cd13d4b0c9167a2eb3147f99d8'
[ffmpeg] / 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 #endif /* HAVE_YASM */
501
502 #define SET_QPEL_FUNCS(PFX, IDX, SIZE, CPU, PREFIX)                          \
503     do {                                                                     \
504     c->PFX ## _pixels_tab[IDX][ 0] = PREFIX ## PFX ## SIZE ## _mc00_ ## CPU; \
505     c->PFX ## _pixels_tab[IDX][ 1] = PREFIX ## PFX ## SIZE ## _mc10_ ## CPU; \
506     c->PFX ## _pixels_tab[IDX][ 2] = PREFIX ## PFX ## SIZE ## _mc20_ ## CPU; \
507     c->PFX ## _pixels_tab[IDX][ 3] = PREFIX ## PFX ## SIZE ## _mc30_ ## CPU; \
508     c->PFX ## _pixels_tab[IDX][ 4] = PREFIX ## PFX ## SIZE ## _mc01_ ## CPU; \
509     c->PFX ## _pixels_tab[IDX][ 5] = PREFIX ## PFX ## SIZE ## _mc11_ ## CPU; \
510     c->PFX ## _pixels_tab[IDX][ 6] = PREFIX ## PFX ## SIZE ## _mc21_ ## CPU; \
511     c->PFX ## _pixels_tab[IDX][ 7] = PREFIX ## PFX ## SIZE ## _mc31_ ## CPU; \
512     c->PFX ## _pixels_tab[IDX][ 8] = PREFIX ## PFX ## SIZE ## _mc02_ ## CPU; \
513     c->PFX ## _pixels_tab[IDX][ 9] = PREFIX ## PFX ## SIZE ## _mc12_ ## CPU; \
514     c->PFX ## _pixels_tab[IDX][10] = PREFIX ## PFX ## SIZE ## _mc22_ ## CPU; \
515     c->PFX ## _pixels_tab[IDX][11] = PREFIX ## PFX ## SIZE ## _mc32_ ## CPU; \
516     c->PFX ## _pixels_tab[IDX][12] = PREFIX ## PFX ## SIZE ## _mc03_ ## CPU; \
517     c->PFX ## _pixels_tab[IDX][13] = PREFIX ## PFX ## SIZE ## _mc13_ ## CPU; \
518     c->PFX ## _pixels_tab[IDX][14] = PREFIX ## PFX ## SIZE ## _mc23_ ## CPU; \
519     c->PFX ## _pixels_tab[IDX][15] = PREFIX ## PFX ## SIZE ## _mc33_ ## CPU; \
520     } while (0)
521
522 static av_cold void dsputil_init_mmx(DSPContext *c, AVCodecContext *avctx,
523                                      int cpu_flags)
524 {
525 #if HAVE_MMX_INLINE
526     const int high_bit_depth = avctx->bits_per_raw_sample > 8;
527
528     c->put_pixels_clamped        = ff_put_pixels_clamped_mmx;
529     c->put_signed_pixels_clamped = ff_put_signed_pixels_clamped_mmx;
530     c->add_pixels_clamped        = ff_add_pixels_clamped_mmx;
531
532     if (!high_bit_depth) {
533         c->clear_block  = ff_clear_block_mmx;
534         c->clear_blocks = ff_clear_blocks_mmx;
535         c->draw_edges   = ff_draw_edges_mmx;
536     }
537
538 #if CONFIG_VIDEODSP && (ARCH_X86_32 || !HAVE_YASM)
539     c->gmc = ff_gmc_mmx;
540 #endif
541
542     c->add_bytes = ff_add_bytes_mmx;
543 #endif /* HAVE_MMX_INLINE */
544
545 #if HAVE_MMX_EXTERNAL
546     c->vector_clip_int32 = ff_vector_clip_int32_mmx;
547 #endif /* HAVE_MMX_EXTERNAL */
548 }
549
550 static av_cold void dsputil_init_mmxext(DSPContext *c, AVCodecContext *avctx,
551                                         int cpu_flags)
552 {
553 #if HAVE_MMXEXT_INLINE
554     const int high_bit_depth = avctx->bits_per_raw_sample > 8;
555
556     if (!high_bit_depth && avctx->idct_algo == FF_IDCT_XVIDMMX && avctx->lowres == 0) {
557         c->idct_put = ff_idct_xvid_mmxext_put;
558         c->idct_add = ff_idct_xvid_mmxext_add;
559         c->idct     = ff_idct_xvid_mmxext;
560     }
561 #endif /* HAVE_MMXEXT_INLINE */
562
563 #if HAVE_MMXEXT_EXTERNAL
564     SET_QPEL_FUNCS(avg_qpel,        0, 16, mmxext, );
565     SET_QPEL_FUNCS(avg_qpel,        1,  8, mmxext, );
566
567     SET_QPEL_FUNCS(put_qpel,        0, 16, mmxext, );
568     SET_QPEL_FUNCS(put_qpel,        1,  8, mmxext, );
569     SET_QPEL_FUNCS(put_no_rnd_qpel, 0, 16, mmxext, );
570     SET_QPEL_FUNCS(put_no_rnd_qpel, 1,  8, mmxext, );
571
572     /* slower than cmov version on AMD */
573     if (!(cpu_flags & AV_CPU_FLAG_3DNOW))
574         c->add_hfyu_median_prediction = ff_add_hfyu_median_prediction_mmxext;
575
576     c->scalarproduct_int16          = ff_scalarproduct_int16_mmxext;
577     c->scalarproduct_and_madd_int16 = ff_scalarproduct_and_madd_int16_mmxext;
578 #endif /* HAVE_MMXEXT_EXTERNAL */
579 }
580
581 static av_cold void dsputil_init_sse(DSPContext *c, AVCodecContext *avctx,
582                                      int cpu_flags)
583 {
584 #if HAVE_SSE_INLINE
585     const int high_bit_depth = avctx->bits_per_raw_sample > 8;
586
587     c->vector_clipf = ff_vector_clipf_sse;
588
589     /* XvMCCreateBlocks() may not allocate 16-byte aligned blocks */
590     if (CONFIG_XVMC && avctx->hwaccel && avctx->hwaccel->decode_mb)
591         return;
592
593     if (!high_bit_depth) {
594         c->clear_block  = ff_clear_block_sse;
595         c->clear_blocks = ff_clear_blocks_sse;
596     }
597 #endif /* HAVE_SSE_INLINE */
598
599 #if HAVE_YASM
600 #if HAVE_INLINE_ASM && CONFIG_VIDEODSP
601     c->gmc = ff_gmc_sse;
602 #endif
603 #endif /* HAVE_YASM */
604 }
605
606 static av_cold void dsputil_init_sse2(DSPContext *c, AVCodecContext *avctx,
607                                       int cpu_flags)
608 {
609 #if HAVE_SSE2_INLINE
610     const int high_bit_depth = avctx->bits_per_raw_sample > 8;
611
612     if (!high_bit_depth && avctx->idct_algo == FF_IDCT_XVIDMMX && avctx->lowres == 0) {
613         c->idct_put              = ff_idct_xvid_sse2_put;
614         c->idct_add              = ff_idct_xvid_sse2_add;
615         c->idct                  = ff_idct_xvid_sse2;
616         c->idct_permutation_type = FF_SSE2_IDCT_PERM;
617     }
618 #endif /* HAVE_SSE2_INLINE */
619
620 #if HAVE_SSE2_EXTERNAL
621     c->scalarproduct_int16          = ff_scalarproduct_int16_sse2;
622     c->scalarproduct_and_madd_int16 = ff_scalarproduct_and_madd_int16_sse2;
623     if (cpu_flags & AV_CPU_FLAG_ATOM) {
624         c->vector_clip_int32 = ff_vector_clip_int32_int_sse2;
625     } else {
626         c->vector_clip_int32 = ff_vector_clip_int32_sse2;
627     }
628     c->bswap_buf = ff_bswap32_buf_sse2;
629 #endif /* HAVE_SSE2_EXTERNAL */
630 }
631
632 static av_cold void dsputil_init_ssse3(DSPContext *c, AVCodecContext *avctx,
633                                        int cpu_flags)
634 {
635 #if HAVE_SSSE3_EXTERNAL
636     c->add_hfyu_left_prediction = ff_add_hfyu_left_prediction_ssse3;
637     if (cpu_flags & AV_CPU_FLAG_SSE4) // not really SSE4, just slow on Conroe
638         c->add_hfyu_left_prediction = ff_add_hfyu_left_prediction_sse4;
639
640     if (!(cpu_flags & (AV_CPU_FLAG_SSE42 | AV_CPU_FLAG_3DNOW))) // cachesplit
641         c->scalarproduct_and_madd_int16 = ff_scalarproduct_and_madd_int16_ssse3;
642     c->bswap_buf = ff_bswap32_buf_ssse3;
643 #endif /* HAVE_SSSE3_EXTERNAL */
644 }
645
646 static av_cold void dsputil_init_sse4(DSPContext *c, AVCodecContext *avctx,
647                                       int cpu_flags)
648 {
649 #if HAVE_SSE4_EXTERNAL
650     c->vector_clip_int32 = ff_vector_clip_int32_sse4;
651 #endif /* HAVE_SSE4_EXTERNAL */
652 }
653
654 av_cold void ff_dsputil_init_x86(DSPContext *c, AVCodecContext *avctx)
655 {
656     int cpu_flags = av_get_cpu_flags();
657
658 #if HAVE_7REGS && HAVE_INLINE_ASM
659     if (HAVE_MMX && cpu_flags & AV_CPU_FLAG_CMOV)
660         c->add_hfyu_median_prediction = ff_add_hfyu_median_prediction_cmov;
661 #endif
662
663     if (X86_MMX(cpu_flags)) {
664 #if HAVE_INLINE_ASM
665         const int idct_algo = avctx->idct_algo;
666
667         if (avctx->lowres == 0 && avctx->bits_per_raw_sample <= 8) {
668             if (idct_algo == FF_IDCT_AUTO || idct_algo == FF_IDCT_SIMPLEMMX) {
669                 c->idct_put              = ff_simple_idct_put_mmx;
670                 c->idct_add              = ff_simple_idct_add_mmx;
671                 c->idct                  = ff_simple_idct_mmx;
672                 c->idct_permutation_type = FF_SIMPLE_IDCT_PERM;
673             } else if (idct_algo == FF_IDCT_XVIDMMX) {
674                 c->idct_put              = ff_idct_xvid_mmx_put;
675                 c->idct_add              = ff_idct_xvid_mmx_add;
676                 c->idct                  = ff_idct_xvid_mmx;
677             }
678         }
679 #endif /* HAVE_INLINE_ASM */
680
681         dsputil_init_mmx(c, avctx, cpu_flags);
682     }
683
684     if (X86_MMXEXT(cpu_flags))
685         dsputil_init_mmxext(c, avctx, cpu_flags);
686
687     if (X86_SSE(cpu_flags))
688         dsputil_init_sse(c, avctx, cpu_flags);
689
690     if (X86_SSE2(cpu_flags))
691         dsputil_init_sse2(c, avctx, cpu_flags);
692
693     if (EXTERNAL_SSSE3(cpu_flags))
694         dsputil_init_ssse3(c, avctx, cpu_flags);
695
696     if (EXTERNAL_SSE4(cpu_flags))
697         dsputil_init_sse4(c, avctx, cpu_flags);
698
699     if (CONFIG_ENCODERS)
700         ff_dsputilenc_init_mmx(c, avctx);
701 }