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