]> git.sesse.net Git - ffmpeg/blob - libavcodec/x86/me_cmp_init.c
Merge commit '443502aed8b814d883825e52e91e4f018955aa66'
[ffmpeg] / libavcodec / x86 / me_cmp_init.c
1 /*
2  * SIMD-optimized motion estimation
3  * Copyright (c) 2000, 2001 Fabrice Bellard
4  * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
5  *
6  * MMX optimization by Nick Kurshev <nickols_k@mail.ru>
7  *
8  * This file is part of FFmpeg.
9  *
10  * FFmpeg is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * FFmpeg is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with FFmpeg; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  */
24
25 #include "libavutil/attributes.h"
26 #include "libavutil/cpu.h"
27 #include "libavutil/x86/asm.h"
28 #include "libavutil/x86/cpu.h"
29 #include "libavcodec/me_cmp.h"
30 #include "libavcodec/mpegvideo.h"
31
32 int ff_sum_abs_dctelem_mmx(int16_t *block);
33 int ff_sum_abs_dctelem_mmxext(int16_t *block);
34 int ff_sum_abs_dctelem_sse2(int16_t *block);
35 int ff_sum_abs_dctelem_ssse3(int16_t *block);
36 int ff_sse8_mmx(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
37                 int line_size, int h);
38 int ff_sse16_mmx(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
39                  int line_size, int h);
40 int ff_sse16_sse2(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
41                   int line_size, int h);
42 int ff_hf_noise8_mmx(uint8_t *pix1, int lsize, int h);
43 int ff_hf_noise16_mmx(uint8_t *pix1, int lsize, int h);
44 int ff_sad8_mmxext(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
45                    int stride, int h);
46 int ff_sad16_mmxext(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
47                     int stride, int h);
48 int ff_sad16_sse2(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
49                   int stride, int h);
50 int ff_sad8_x2_mmxext(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
51                       int stride, int h);
52 int ff_sad16_x2_mmxext(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
53                        int stride, int h);
54 int ff_sad16_x2_sse2(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
55                      int stride, int h);
56 int ff_sad8_y2_mmxext(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
57                       int stride, int h);
58 int ff_sad16_y2_mmxext(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
59                        int stride, int h);
60 int ff_sad16_y2_sse2(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
61                      int stride, int h);
62 int ff_sad8_approx_xy2_mmxext(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
63                               int stride, int h);
64 int ff_sad16_approx_xy2_mmxext(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
65                                int stride, int h);
66 int ff_sad16_approx_xy2_sse2(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
67                              int stride, int h);
68 int ff_vsad_intra8_mmxext(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
69                           int line_size, int h);
70 int ff_vsad_intra16_mmxext(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
71                            int line_size, int h);
72 int ff_vsad_intra16_sse2(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
73                          int line_size, int h);
74 int ff_vsad8_approx_mmxext(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
75                     int line_size, int h);
76 int ff_vsad16_approx_mmxext(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
77                      int line_size, int h);
78 int ff_vsad16_approx_sse2(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
79                    int line_size, int h);
80
81 #define hadamard_func(cpu)                                              \
82     int ff_hadamard8_diff_ ## cpu(MpegEncContext *s, uint8_t *src1,     \
83                                   uint8_t *src2, int stride, int h);    \
84     int ff_hadamard8_diff16_ ## cpu(MpegEncContext *s, uint8_t *src1,   \
85                                     uint8_t *src2, int stride, int h);
86
87 hadamard_func(mmx)
88 hadamard_func(mmxext)
89 hadamard_func(sse2)
90 hadamard_func(ssse3)
91
92 #if HAVE_YASM
93 static int nsse16_mmx(MpegEncContext *c, uint8_t *pix1, uint8_t *pix2,
94                       int line_size, int h)
95 {
96     int score1, score2;
97
98     if (c)
99         score1 = c->mecc.sse[0](c, pix1, pix2, line_size, h);
100     else
101         score1 = ff_sse16_mmx(c, pix1, pix2, line_size, h);
102     score2 = ff_hf_noise16_mmx(pix1, line_size, h) + ff_hf_noise8_mmx(pix1+8, line_size, h)
103            - ff_hf_noise16_mmx(pix2, line_size, h) - ff_hf_noise8_mmx(pix2+8, line_size, h);
104
105     if (c)
106         return score1 + FFABS(score2) * c->avctx->nsse_weight;
107     else
108         return score1 + FFABS(score2) * 8;
109 }
110
111 static int nsse8_mmx(MpegEncContext *c, uint8_t *pix1, uint8_t *pix2,
112                      int line_size, int h)
113 {
114     int score1 = ff_sse8_mmx(c, pix1, pix2, line_size, h);
115     int score2 = ff_hf_noise8_mmx(pix1, line_size, h) -
116                  ff_hf_noise8_mmx(pix2, line_size, h);
117
118     if (c)
119         return score1 + FFABS(score2) * c->avctx->nsse_weight;
120     else
121         return score1 + FFABS(score2) * 8;
122 }
123
124 #endif /* HAVE_YASM */
125
126 #if HAVE_INLINE_ASM
127
128 static int vsad_intra16_mmx(MpegEncContext *v, uint8_t *pix, uint8_t *dummy,
129                             int line_size, int h)
130 {
131     int tmp;
132
133     av_assert2((((int) pix) & 7) == 0);
134     av_assert2((line_size & 7) == 0);
135
136 #define SUM(in0, in1, out0, out1)               \
137     "movq (%0), %%mm2\n"                        \
138     "movq 8(%0), %%mm3\n"                       \
139     "add %2,%0\n"                               \
140     "movq %%mm2, " #out0 "\n"                   \
141     "movq %%mm3, " #out1 "\n"                   \
142     "psubusb " #in0 ", %%mm2\n"                 \
143     "psubusb " #in1 ", %%mm3\n"                 \
144     "psubusb " #out0 ", " #in0 "\n"             \
145     "psubusb " #out1 ", " #in1 "\n"             \
146     "por %%mm2, " #in0 "\n"                     \
147     "por %%mm3, " #in1 "\n"                     \
148     "movq " #in0 ", %%mm2\n"                    \
149     "movq " #in1 ", %%mm3\n"                    \
150     "punpcklbw %%mm7, " #in0 "\n"               \
151     "punpcklbw %%mm7, " #in1 "\n"               \
152     "punpckhbw %%mm7, %%mm2\n"                  \
153     "punpckhbw %%mm7, %%mm3\n"                  \
154     "paddw " #in1 ", " #in0 "\n"                \
155     "paddw %%mm3, %%mm2\n"                      \
156     "paddw %%mm2, " #in0 "\n"                   \
157     "paddw " #in0 ", %%mm6\n"
158
159
160     __asm__ volatile (
161         "movl    %3, %%ecx\n"
162         "pxor %%mm6, %%mm6\n"
163         "pxor %%mm7, %%mm7\n"
164         "movq  (%0), %%mm0\n"
165         "movq 8(%0), %%mm1\n"
166         "add %2, %0\n"
167         "jmp 2f\n"
168         "1:\n"
169
170         SUM(%%mm4, %%mm5, %%mm0, %%mm1)
171         "2:\n"
172         SUM(%%mm0, %%mm1, %%mm4, %%mm5)
173
174         "subl $2, %%ecx\n"
175         "jnz 1b\n"
176
177         "movq  %%mm6, %%mm0\n"
178         "psrlq $32,   %%mm6\n"
179         "paddw %%mm6, %%mm0\n"
180         "movq  %%mm0, %%mm6\n"
181         "psrlq $16,   %%mm0\n"
182         "paddw %%mm6, %%mm0\n"
183         "movd  %%mm0, %1\n"
184         : "+r" (pix), "=r" (tmp)
185         : "r" ((x86_reg) line_size), "m" (h)
186         : "%ecx");
187
188     return tmp & 0xFFFF;
189 }
190 #undef SUM
191
192 static int vsad16_mmx(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
193                       int line_size, int h)
194 {
195     int tmp;
196
197     av_assert2((((int) pix1) & 7) == 0);
198     av_assert2((((int) pix2) & 7) == 0);
199     av_assert2((line_size & 7) == 0);
200
201 #define SUM(in0, in1, out0, out1)       \
202     "movq (%0), %%mm2\n"                \
203     "movq (%1), " #out0 "\n"            \
204     "movq 8(%0), %%mm3\n"               \
205     "movq 8(%1), " #out1 "\n"           \
206     "add %3, %0\n"                      \
207     "add %3, %1\n"                      \
208     "psubb " #out0 ", %%mm2\n"          \
209     "psubb " #out1 ", %%mm3\n"          \
210     "pxor %%mm7, %%mm2\n"               \
211     "pxor %%mm7, %%mm3\n"               \
212     "movq %%mm2, " #out0 "\n"           \
213     "movq %%mm3, " #out1 "\n"           \
214     "psubusb " #in0 ", %%mm2\n"         \
215     "psubusb " #in1 ", %%mm3\n"         \
216     "psubusb " #out0 ", " #in0 "\n"     \
217     "psubusb " #out1 ", " #in1 "\n"     \
218     "por %%mm2, " #in0 "\n"             \
219     "por %%mm3, " #in1 "\n"             \
220     "movq " #in0 ", %%mm2\n"            \
221     "movq " #in1 ", %%mm3\n"            \
222     "punpcklbw %%mm7, " #in0 "\n"       \
223     "punpcklbw %%mm7, " #in1 "\n"       \
224     "punpckhbw %%mm7, %%mm2\n"          \
225     "punpckhbw %%mm7, %%mm3\n"          \
226     "paddw " #in1 ", " #in0 "\n"        \
227     "paddw %%mm3, %%mm2\n"              \
228     "paddw %%mm2, " #in0 "\n"           \
229     "paddw " #in0 ", %%mm6\n"
230
231
232     __asm__ volatile (
233         "movl %4, %%ecx\n"
234         "pxor %%mm6, %%mm6\n"
235         "pcmpeqw %%mm7, %%mm7\n"
236         "psllw $15, %%mm7\n"
237         "packsswb %%mm7, %%mm7\n"
238         "movq (%0), %%mm0\n"
239         "movq (%1), %%mm2\n"
240         "movq 8(%0), %%mm1\n"
241         "movq 8(%1), %%mm3\n"
242         "add %3, %0\n"
243         "add %3, %1\n"
244         "psubb %%mm2, %%mm0\n"
245         "psubb %%mm3, %%mm1\n"
246         "pxor %%mm7, %%mm0\n"
247         "pxor %%mm7, %%mm1\n"
248         "jmp 2f\n"
249         "1:\n"
250
251         SUM(%%mm4, %%mm5, %%mm0, %%mm1)
252         "2:\n"
253         SUM(%%mm0, %%mm1, %%mm4, %%mm5)
254
255         "subl $2, %%ecx\n"
256         "jnz 1b\n"
257
258         "movq %%mm6, %%mm0\n"
259         "psrlq $32, %%mm6\n"
260         "paddw %%mm6, %%mm0\n"
261         "movq %%mm0, %%mm6\n"
262         "psrlq $16, %%mm0\n"
263         "paddw %%mm6, %%mm0\n"
264         "movd %%mm0, %2\n"
265         : "+r" (pix1), "+r" (pix2), "=r" (tmp)
266         : "r" ((x86_reg) line_size), "m" (h)
267         : "%ecx");
268
269     return tmp & 0x7FFF;
270 }
271 #undef SUM
272
273 DECLARE_ASM_CONST(8, uint64_t, round_tab)[3] = {
274     0x0000000000000000ULL,
275     0x0001000100010001ULL,
276     0x0002000200020002ULL,
277 };
278
279 static inline void sad8_1_mmx(uint8_t *blk1, uint8_t *blk2, int stride, int h)
280 {
281     x86_reg len = -(x86_reg)stride * h;
282     __asm__ volatile (
283         ".p2align 4                     \n\t"
284         "1:                             \n\t"
285         "movq (%1, %%"REG_a"), %%mm0    \n\t"
286         "movq (%2, %%"REG_a"), %%mm2    \n\t"
287         "movq (%2, %%"REG_a"), %%mm4    \n\t"
288         "add %3, %%"REG_a"              \n\t"
289         "psubusb %%mm0, %%mm2           \n\t"
290         "psubusb %%mm4, %%mm0           \n\t"
291         "movq (%1, %%"REG_a"), %%mm1    \n\t"
292         "movq (%2, %%"REG_a"), %%mm3    \n\t"
293         "movq (%2, %%"REG_a"), %%mm5    \n\t"
294         "psubusb %%mm1, %%mm3           \n\t"
295         "psubusb %%mm5, %%mm1           \n\t"
296         "por %%mm2, %%mm0               \n\t"
297         "por %%mm1, %%mm3               \n\t"
298         "movq %%mm0, %%mm1              \n\t"
299         "movq %%mm3, %%mm2              \n\t"
300         "punpcklbw %%mm7, %%mm0         \n\t"
301         "punpckhbw %%mm7, %%mm1         \n\t"
302         "punpcklbw %%mm7, %%mm3         \n\t"
303         "punpckhbw %%mm7, %%mm2         \n\t"
304         "paddw %%mm1, %%mm0             \n\t"
305         "paddw %%mm3, %%mm2             \n\t"
306         "paddw %%mm2, %%mm0             \n\t"
307         "paddw %%mm0, %%mm6             \n\t"
308         "add %3, %%"REG_a"              \n\t"
309         " js 1b                         \n\t"
310         : "+a" (len)
311         : "r" (blk1 - len), "r" (blk2 - len), "r" ((x86_reg) stride));
312 }
313
314 static inline void sad8_2_mmx(uint8_t *blk1a, uint8_t *blk1b, uint8_t *blk2,
315                               int stride, int h)
316 {
317     x86_reg len = -(x86_reg)stride * h;
318     __asm__ volatile (
319         ".p2align 4                     \n\t"
320         "1:                             \n\t"
321         "movq (%1, %%"REG_a"), %%mm0    \n\t"
322         "movq (%2, %%"REG_a"), %%mm1    \n\t"
323         "movq (%1, %%"REG_a"), %%mm2    \n\t"
324         "movq (%2, %%"REG_a"), %%mm3    \n\t"
325         "punpcklbw %%mm7, %%mm0         \n\t"
326         "punpcklbw %%mm7, %%mm1         \n\t"
327         "punpckhbw %%mm7, %%mm2         \n\t"
328         "punpckhbw %%mm7, %%mm3         \n\t"
329         "paddw %%mm0, %%mm1             \n\t"
330         "paddw %%mm2, %%mm3             \n\t"
331         "movq (%3, %%"REG_a"), %%mm4    \n\t"
332         "movq (%3, %%"REG_a"), %%mm2    \n\t"
333         "paddw %%mm5, %%mm1             \n\t"
334         "paddw %%mm5, %%mm3             \n\t"
335         "psrlw $1, %%mm1                \n\t"
336         "psrlw $1, %%mm3                \n\t"
337         "packuswb %%mm3, %%mm1          \n\t"
338         "psubusb %%mm1, %%mm4           \n\t"
339         "psubusb %%mm2, %%mm1           \n\t"
340         "por %%mm4, %%mm1               \n\t"
341         "movq %%mm1, %%mm0              \n\t"
342         "punpcklbw %%mm7, %%mm0         \n\t"
343         "punpckhbw %%mm7, %%mm1         \n\t"
344         "paddw %%mm1, %%mm0             \n\t"
345         "paddw %%mm0, %%mm6             \n\t"
346         "add %4, %%"REG_a"              \n\t"
347         " js 1b                         \n\t"
348         : "+a" (len)
349         : "r" (blk1a - len), "r" (blk1b - len), "r" (blk2 - len),
350           "r" ((x86_reg) stride));
351 }
352
353 static inline void sad8_4_mmx(uint8_t *blk1, uint8_t *blk2, int stride, int h)
354 {
355     x86_reg len = -(x86_reg)stride * h;
356     __asm__ volatile (
357         "movq  (%1, %%"REG_a"), %%mm0   \n\t"
358         "movq 1(%1, %%"REG_a"), %%mm2   \n\t"
359         "movq %%mm0, %%mm1              \n\t"
360         "movq %%mm2, %%mm3              \n\t"
361         "punpcklbw %%mm7, %%mm0         \n\t"
362         "punpckhbw %%mm7, %%mm1         \n\t"
363         "punpcklbw %%mm7, %%mm2         \n\t"
364         "punpckhbw %%mm7, %%mm3         \n\t"
365         "paddw %%mm2, %%mm0             \n\t"
366         "paddw %%mm3, %%mm1             \n\t"
367         ".p2align 4                     \n\t"
368         "1:                             \n\t"
369         "movq  (%2, %%"REG_a"), %%mm2   \n\t"
370         "movq 1(%2, %%"REG_a"), %%mm4   \n\t"
371         "movq %%mm2, %%mm3              \n\t"
372         "movq %%mm4, %%mm5              \n\t"
373         "punpcklbw %%mm7, %%mm2         \n\t"
374         "punpckhbw %%mm7, %%mm3         \n\t"
375         "punpcklbw %%mm7, %%mm4         \n\t"
376         "punpckhbw %%mm7, %%mm5         \n\t"
377         "paddw %%mm4, %%mm2             \n\t"
378         "paddw %%mm5, %%mm3             \n\t"
379         "movq %5, %%mm5                 \n\t"
380         "paddw %%mm2, %%mm0             \n\t"
381         "paddw %%mm3, %%mm1             \n\t"
382         "paddw %%mm5, %%mm0             \n\t"
383         "paddw %%mm5, %%mm1             \n\t"
384         "movq (%3, %%"REG_a"), %%mm4    \n\t"
385         "movq (%3, %%"REG_a"), %%mm5    \n\t"
386         "psrlw $2, %%mm0                \n\t"
387         "psrlw $2, %%mm1                \n\t"
388         "packuswb %%mm1, %%mm0          \n\t"
389         "psubusb %%mm0, %%mm4           \n\t"
390         "psubusb %%mm5, %%mm0           \n\t"
391         "por %%mm4, %%mm0               \n\t"
392         "movq %%mm0, %%mm4              \n\t"
393         "punpcklbw %%mm7, %%mm0         \n\t"
394         "punpckhbw %%mm7, %%mm4         \n\t"
395         "paddw %%mm0, %%mm6             \n\t"
396         "paddw %%mm4, %%mm6             \n\t"
397         "movq  %%mm2, %%mm0             \n\t"
398         "movq  %%mm3, %%mm1             \n\t"
399         "add %4, %%"REG_a"              \n\t"
400         " js 1b                         \n\t"
401         : "+a" (len)
402         : "r" (blk1 - len), "r" (blk1 - len + stride), "r" (blk2 - len),
403           "r" ((x86_reg) stride), "m" (round_tab[2]));
404 }
405
406 static inline int sum_mmx(void)
407 {
408     int ret;
409     __asm__ volatile (
410         "movq %%mm6, %%mm0              \n\t"
411         "psrlq $32, %%mm6               \n\t"
412         "paddw %%mm0, %%mm6             \n\t"
413         "movq %%mm6, %%mm0              \n\t"
414         "psrlq $16, %%mm6               \n\t"
415         "paddw %%mm0, %%mm6             \n\t"
416         "movd %%mm6, %0                 \n\t"
417         : "=r" (ret));
418     return ret & 0xFFFF;
419 }
420
421 static inline void sad8_x2a_mmx(uint8_t *blk1, uint8_t *blk2, int stride, int h)
422 {
423     sad8_2_mmx(blk1, blk1 + 1, blk2, stride, h);
424 }
425
426 static inline void sad8_y2a_mmx(uint8_t *blk1, uint8_t *blk2, int stride, int h)
427 {
428     sad8_2_mmx(blk1, blk1 + stride, blk2, stride, h);
429 }
430
431 #define PIX_SAD(suf)                                                    \
432 static int sad8_ ## suf(MpegEncContext *v, uint8_t *blk2,               \
433                         uint8_t *blk1, int stride, int h)               \
434 {                                                                       \
435     av_assert2(h == 8);                                                     \
436     __asm__ volatile (                                                  \
437         "pxor %%mm7, %%mm7     \n\t"                                    \
438         "pxor %%mm6, %%mm6     \n\t"                                    \
439         :);                                                             \
440                                                                         \
441     sad8_1_ ## suf(blk1, blk2, stride, 8);                              \
442                                                                         \
443     return sum_ ## suf();                                               \
444 }                                                                       \
445                                                                         \
446 static int sad8_x2_ ## suf(MpegEncContext *v, uint8_t *blk2,            \
447                            uint8_t *blk1, int stride, int h)            \
448 {                                                                       \
449     av_assert2(h == 8);                                                     \
450     __asm__ volatile (                                                  \
451         "pxor %%mm7, %%mm7     \n\t"                                    \
452         "pxor %%mm6, %%mm6     \n\t"                                    \
453         "movq %0, %%mm5        \n\t"                                    \
454         :: "m" (round_tab[1]));                                         \
455                                                                         \
456     sad8_x2a_ ## suf(blk1, blk2, stride, 8);                            \
457                                                                         \
458     return sum_ ## suf();                                               \
459 }                                                                       \
460                                                                         \
461 static int sad8_y2_ ## suf(MpegEncContext *v, uint8_t *blk2,            \
462                            uint8_t *blk1, int stride, int h)            \
463 {                                                                       \
464     av_assert2(h == 8);                                                     \
465     __asm__ volatile (                                                  \
466         "pxor %%mm7, %%mm7     \n\t"                                    \
467         "pxor %%mm6, %%mm6     \n\t"                                    \
468         "movq %0, %%mm5        \n\t"                                    \
469         :: "m" (round_tab[1]));                                         \
470                                                                         \
471     sad8_y2a_ ## suf(blk1, blk2, stride, 8);                            \
472                                                                         \
473     return sum_ ## suf();                                               \
474 }                                                                       \
475                                                                         \
476 static int sad8_xy2_ ## suf(MpegEncContext *v, uint8_t *blk2,           \
477                             uint8_t *blk1, int stride, int h)           \
478 {                                                                       \
479     av_assert2(h == 8);                                                     \
480     __asm__ volatile (                                                  \
481         "pxor %%mm7, %%mm7     \n\t"                                    \
482         "pxor %%mm6, %%mm6     \n\t"                                    \
483         ::);                                                            \
484                                                                         \
485     sad8_4_ ## suf(blk1, blk2, stride, 8);                              \
486                                                                         \
487     return sum_ ## suf();                                               \
488 }                                                                       \
489                                                                         \
490 static int sad16_ ## suf(MpegEncContext *v, uint8_t *blk2,              \
491                          uint8_t *blk1, int stride, int h)              \
492 {                                                                       \
493     __asm__ volatile (                                                  \
494         "pxor %%mm7, %%mm7     \n\t"                                    \
495         "pxor %%mm6, %%mm6     \n\t"                                    \
496         :);                                                             \
497                                                                         \
498     sad8_1_ ## suf(blk1,     blk2,     stride, h);                      \
499     sad8_1_ ## suf(blk1 + 8, blk2 + 8, stride, h);                      \
500                                                                         \
501     return sum_ ## suf();                                               \
502 }                                                                       \
503                                                                         \
504 static int sad16_x2_ ## suf(MpegEncContext *v, uint8_t *blk2,           \
505                             uint8_t *blk1, int stride, int h)           \
506 {                                                                       \
507     __asm__ volatile (                                                  \
508         "pxor %%mm7, %%mm7     \n\t"                                    \
509         "pxor %%mm6, %%mm6     \n\t"                                    \
510         "movq %0, %%mm5        \n\t"                                    \
511         :: "m" (round_tab[1]));                                         \
512                                                                         \
513     sad8_x2a_ ## suf(blk1,     blk2,     stride, h);                    \
514     sad8_x2a_ ## suf(blk1 + 8, blk2 + 8, stride, h);                    \
515                                                                         \
516     return sum_ ## suf();                                               \
517 }                                                                       \
518                                                                         \
519 static int sad16_y2_ ## suf(MpegEncContext *v, uint8_t *blk2,           \
520                             uint8_t *blk1, int stride, int h)           \
521 {                                                                       \
522     __asm__ volatile (                                                  \
523         "pxor %%mm7, %%mm7     \n\t"                                    \
524         "pxor %%mm6, %%mm6     \n\t"                                    \
525         "movq %0, %%mm5        \n\t"                                    \
526         :: "m" (round_tab[1]));                                         \
527                                                                         \
528     sad8_y2a_ ## suf(blk1,     blk2,     stride, h);                    \
529     sad8_y2a_ ## suf(blk1 + 8, blk2 + 8, stride, h);                    \
530                                                                         \
531     return sum_ ## suf();                                               \
532 }                                                                       \
533                                                                         \
534 static int sad16_xy2_ ## suf(MpegEncContext *v, uint8_t *blk2,          \
535                              uint8_t *blk1, int stride, int h)          \
536 {                                                                       \
537     __asm__ volatile (                                                  \
538         "pxor %%mm7, %%mm7     \n\t"                                    \
539         "pxor %%mm6, %%mm6     \n\t"                                    \
540         ::);                                                            \
541                                                                         \
542     sad8_4_ ## suf(blk1,     blk2,     stride, h);                      \
543     sad8_4_ ## suf(blk1 + 8, blk2 + 8, stride, h);                      \
544                                                                         \
545     return sum_ ## suf();                                               \
546 }                                                                       \
547
548 PIX_SAD(mmx)
549
550 #endif /* HAVE_INLINE_ASM */
551
552 av_cold void ff_me_cmp_init_x86(MECmpContext *c, AVCodecContext *avctx)
553 {
554     int cpu_flags = av_get_cpu_flags();
555
556 #if HAVE_INLINE_ASM
557     if (INLINE_MMX(cpu_flags)) {
558         c->pix_abs[0][0] = sad16_mmx;
559         c->pix_abs[0][1] = sad16_x2_mmx;
560         c->pix_abs[0][2] = sad16_y2_mmx;
561         c->pix_abs[0][3] = sad16_xy2_mmx;
562         c->pix_abs[1][0] = sad8_mmx;
563         c->pix_abs[1][1] = sad8_x2_mmx;
564         c->pix_abs[1][2] = sad8_y2_mmx;
565         c->pix_abs[1][3] = sad8_xy2_mmx;
566
567         c->sad[0] = sad16_mmx;
568         c->sad[1] = sad8_mmx;
569
570         c->vsad[4] = vsad_intra16_mmx;
571
572         if (!(avctx->flags & CODEC_FLAG_BITEXACT)) {
573             c->vsad[0] = vsad16_mmx;
574         }
575     }
576
577 #endif /* HAVE_INLINE_ASM */
578
579     if (EXTERNAL_MMX(cpu_flags)) {
580         c->hadamard8_diff[0] = ff_hadamard8_diff16_mmx;
581         c->hadamard8_diff[1] = ff_hadamard8_diff_mmx;
582         c->sum_abs_dctelem   = ff_sum_abs_dctelem_mmx;
583         c->sse[0]            = ff_sse16_mmx;
584         c->sse[1]            = ff_sse8_mmx;
585 #if HAVE_YASM
586         c->nsse[0]           = nsse16_mmx;
587         c->nsse[1]           = nsse8_mmx;
588 #endif
589     }
590
591     if (EXTERNAL_MMXEXT(cpu_flags)) {
592         c->hadamard8_diff[0] = ff_hadamard8_diff16_mmxext;
593         c->hadamard8_diff[1] = ff_hadamard8_diff_mmxext;
594         c->sum_abs_dctelem   = ff_sum_abs_dctelem_mmxext;
595
596         c->sad[0] = ff_sad16_mmxext;
597         c->sad[1] = ff_sad8_mmxext;
598
599         c->pix_abs[0][0] = ff_sad16_mmxext;
600         c->pix_abs[0][1] = ff_sad16_x2_mmxext;
601         c->pix_abs[0][2] = ff_sad16_y2_mmxext;
602         c->pix_abs[1][0] = ff_sad8_mmxext;
603         c->pix_abs[1][1] = ff_sad8_x2_mmxext;
604         c->pix_abs[1][2] = ff_sad8_y2_mmxext;
605
606         c->vsad[4] = ff_vsad_intra16_mmxext;
607         c->vsad[5] = ff_vsad_intra8_mmxext;
608
609         if (!(avctx->flags & CODEC_FLAG_BITEXACT)) {
610             c->pix_abs[0][3] = ff_sad16_approx_xy2_mmxext;
611             c->pix_abs[1][3] = ff_sad8_approx_xy2_mmxext;
612
613             c->vsad[0] = ff_vsad16_approx_mmxext;
614             c->vsad[1] = ff_vsad8_approx_mmxext;
615         }
616     }
617
618     if (EXTERNAL_SSE2(cpu_flags)) {
619         c->sse[0] = ff_sse16_sse2;
620         c->sum_abs_dctelem   = ff_sum_abs_dctelem_sse2;
621
622 #if HAVE_ALIGNED_STACK
623         c->hadamard8_diff[0] = ff_hadamard8_diff16_sse2;
624         c->hadamard8_diff[1] = ff_hadamard8_diff_sse2;
625 #endif
626         if (!(cpu_flags & AV_CPU_FLAG_SSE2SLOW) && avctx->codec_id != AV_CODEC_ID_SNOW) {
627             c->sad[0]        = ff_sad16_sse2;
628             c->pix_abs[0][0] = ff_sad16_sse2;
629             c->pix_abs[0][1] = ff_sad16_x2_sse2;
630             c->pix_abs[0][2] = ff_sad16_y2_sse2;
631
632             c->vsad[4]       = ff_vsad_intra16_sse2;
633             if (!(avctx->flags & CODEC_FLAG_BITEXACT)) {
634                 c->pix_abs[0][3] = ff_sad16_approx_xy2_sse2;
635                 c->vsad[0]       = ff_vsad16_approx_sse2;
636             }
637         }
638     }
639
640     if (EXTERNAL_SSSE3(cpu_flags)) {
641         c->sum_abs_dctelem   = ff_sum_abs_dctelem_ssse3;
642 #if HAVE_ALIGNED_STACK
643         c->hadamard8_diff[0] = ff_hadamard8_diff16_ssse3;
644         c->hadamard8_diff[1] = ff_hadamard8_diff_ssse3;
645 #endif
646     }
647 }