2 * MMX optimized motion estimation
3 * Copyright (c) 2001 Fabrice Bellard.
4 * Copyright (c) 2002-2004 Michael Niedermayer
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 * mostly by Michael Niedermayer <michaelni@gmx.at>
24 #include "../dsputil.h"
27 static const __attribute__ ((aligned(8))) uint64_t round_tab[3]={
28 0x0000000000000000ULL,
29 0x0001000100010001ULL,
30 0x0002000200020002ULL,
33 static attribute_used __attribute__ ((aligned(8))) uint64_t bone= 0x0101010101010101LL;
35 static inline void sad8_1_mmx(uint8_t *blk1, uint8_t *blk2, int stride, int h)
37 long len= -(stride*h);
41 "movq (%1, %%"REG_a"), %%mm0 \n\t"
42 "movq (%2, %%"REG_a"), %%mm2 \n\t"
43 "movq (%2, %%"REG_a"), %%mm4 \n\t"
44 "add %3, %%"REG_a" \n\t"
45 "psubusb %%mm0, %%mm2 \n\t"
46 "psubusb %%mm4, %%mm0 \n\t"
47 "movq (%1, %%"REG_a"), %%mm1 \n\t"
48 "movq (%2, %%"REG_a"), %%mm3 \n\t"
49 "movq (%2, %%"REG_a"), %%mm5 \n\t"
50 "psubusb %%mm1, %%mm3 \n\t"
51 "psubusb %%mm5, %%mm1 \n\t"
52 "por %%mm2, %%mm0 \n\t"
53 "por %%mm1, %%mm3 \n\t"
54 "movq %%mm0, %%mm1 \n\t"
55 "movq %%mm3, %%mm2 \n\t"
56 "punpcklbw %%mm7, %%mm0 \n\t"
57 "punpckhbw %%mm7, %%mm1 \n\t"
58 "punpcklbw %%mm7, %%mm3 \n\t"
59 "punpckhbw %%mm7, %%mm2 \n\t"
60 "paddw %%mm1, %%mm0 \n\t"
61 "paddw %%mm3, %%mm2 \n\t"
62 "paddw %%mm2, %%mm0 \n\t"
63 "paddw %%mm0, %%mm6 \n\t"
64 "add %3, %%"REG_a" \n\t"
67 : "r" (blk1 - len), "r" (blk2 - len), "r" ((long)stride)
71 static inline void sad8_1_mmx2(uint8_t *blk1, uint8_t *blk2, int stride, int h)
73 long len= -(stride*h);
77 "movq (%1, %%"REG_a"), %%mm0 \n\t"
78 "movq (%2, %%"REG_a"), %%mm2 \n\t"
79 "psadbw %%mm2, %%mm0 \n\t"
80 "add %3, %%"REG_a" \n\t"
81 "movq (%1, %%"REG_a"), %%mm1 \n\t"
82 "movq (%2, %%"REG_a"), %%mm3 \n\t"
83 "psadbw %%mm1, %%mm3 \n\t"
84 "paddw %%mm3, %%mm0 \n\t"
85 "paddw %%mm0, %%mm6 \n\t"
86 "add %3, %%"REG_a" \n\t"
89 : "r" (blk1 - len), "r" (blk2 - len), "r" ((long)stride)
93 static inline void sad8_2_mmx2(uint8_t *blk1a, uint8_t *blk1b, uint8_t *blk2, int stride, int h)
95 long len= -(stride*h);
99 "movq (%1, %%"REG_a"), %%mm0 \n\t"
100 "movq (%2, %%"REG_a"), %%mm2 \n\t"
101 "pavgb %%mm2, %%mm0 \n\t"
102 "movq (%3, %%"REG_a"), %%mm2 \n\t"
103 "psadbw %%mm2, %%mm0 \n\t"
104 "add %4, %%"REG_a" \n\t"
105 "movq (%1, %%"REG_a"), %%mm1 \n\t"
106 "movq (%2, %%"REG_a"), %%mm3 \n\t"
107 "pavgb %%mm1, %%mm3 \n\t"
108 "movq (%3, %%"REG_a"), %%mm1 \n\t"
109 "psadbw %%mm1, %%mm3 \n\t"
110 "paddw %%mm3, %%mm0 \n\t"
111 "paddw %%mm0, %%mm6 \n\t"
112 "add %4, %%"REG_a" \n\t"
115 : "r" (blk1a - len), "r" (blk1b -len), "r" (blk2 - len), "r" ((long)stride)
119 static inline void sad8_4_mmx2(uint8_t *blk1, uint8_t *blk2, int stride, int h)
121 long len= -(stride*h);
124 "movq "MANGLE(bone)", %%mm5 \n\t"
126 "movq (%1, %%"REG_a"), %%mm0 \n\t"
127 "movq (%2, %%"REG_a"), %%mm2 \n\t"
128 "movq 1(%1, %%"REG_a"), %%mm1 \n\t"
129 "movq 1(%2, %%"REG_a"), %%mm3 \n\t"
130 "pavgb %%mm2, %%mm0 \n\t"
131 "pavgb %%mm1, %%mm3 \n\t"
132 "psubusb %%mm5, %%mm3 \n\t"
133 "pavgb %%mm3, %%mm0 \n\t"
134 "movq (%3, %%"REG_a"), %%mm2 \n\t"
135 "psadbw %%mm2, %%mm0 \n\t"
136 "add %4, %%"REG_a" \n\t"
137 "movq (%1, %%"REG_a"), %%mm1 \n\t"
138 "movq (%2, %%"REG_a"), %%mm3 \n\t"
139 "movq 1(%1, %%"REG_a"), %%mm2 \n\t"
140 "movq 1(%2, %%"REG_a"), %%mm4 \n\t"
141 "pavgb %%mm3, %%mm1 \n\t"
142 "pavgb %%mm4, %%mm2 \n\t"
143 "psubusb %%mm5, %%mm2 \n\t"
144 "pavgb %%mm1, %%mm2 \n\t"
145 "movq (%3, %%"REG_a"), %%mm1 \n\t"
146 "psadbw %%mm1, %%mm2 \n\t"
147 "paddw %%mm2, %%mm0 \n\t"
148 "paddw %%mm0, %%mm6 \n\t"
149 "add %4, %%"REG_a" \n\t"
152 : "r" (blk1 - len), "r" (blk1 - len + stride), "r" (blk2 - len), "r" ((long)stride)
156 static inline void sad8_2_mmx(uint8_t *blk1a, uint8_t *blk1b, uint8_t *blk2, int stride, int h)
158 long len= -(stride*h);
162 "movq (%1, %%"REG_a"), %%mm0 \n\t"
163 "movq (%2, %%"REG_a"), %%mm1 \n\t"
164 "movq (%1, %%"REG_a"), %%mm2 \n\t"
165 "movq (%2, %%"REG_a"), %%mm3 \n\t"
166 "punpcklbw %%mm7, %%mm0 \n\t"
167 "punpcklbw %%mm7, %%mm1 \n\t"
168 "punpckhbw %%mm7, %%mm2 \n\t"
169 "punpckhbw %%mm7, %%mm3 \n\t"
170 "paddw %%mm0, %%mm1 \n\t"
171 "paddw %%mm2, %%mm3 \n\t"
172 "movq (%3, %%"REG_a"), %%mm4 \n\t"
173 "movq (%3, %%"REG_a"), %%mm2 \n\t"
174 "paddw %%mm5, %%mm1 \n\t"
175 "paddw %%mm5, %%mm3 \n\t"
176 "psrlw $1, %%mm1 \n\t"
177 "psrlw $1, %%mm3 \n\t"
178 "packuswb %%mm3, %%mm1 \n\t"
179 "psubusb %%mm1, %%mm4 \n\t"
180 "psubusb %%mm2, %%mm1 \n\t"
181 "por %%mm4, %%mm1 \n\t"
182 "movq %%mm1, %%mm0 \n\t"
183 "punpcklbw %%mm7, %%mm0 \n\t"
184 "punpckhbw %%mm7, %%mm1 \n\t"
185 "paddw %%mm1, %%mm0 \n\t"
186 "paddw %%mm0, %%mm6 \n\t"
187 "add %4, %%"REG_a" \n\t"
190 : "r" (blk1a - len), "r" (blk1b -len), "r" (blk2 - len), "r" ((long)stride)
194 static inline void sad8_4_mmx(uint8_t *blk1, uint8_t *blk2, int stride, int h)
196 long len= -(stride*h);
200 "movq (%1, %%"REG_a"), %%mm0 \n\t"
201 "movq (%2, %%"REG_a"), %%mm1 \n\t"
202 "movq %%mm0, %%mm4 \n\t"
203 "movq %%mm1, %%mm2 \n\t"
204 "punpcklbw %%mm7, %%mm0 \n\t"
205 "punpcklbw %%mm7, %%mm1 \n\t"
206 "punpckhbw %%mm7, %%mm4 \n\t"
207 "punpckhbw %%mm7, %%mm2 \n\t"
208 "paddw %%mm1, %%mm0 \n\t"
209 "paddw %%mm2, %%mm4 \n\t"
210 "movq 1(%1, %%"REG_a"), %%mm2 \n\t"
211 "movq 1(%2, %%"REG_a"), %%mm3 \n\t"
212 "movq %%mm2, %%mm1 \n\t"
213 "punpcklbw %%mm7, %%mm2 \n\t"
214 "punpckhbw %%mm7, %%mm1 \n\t"
215 "paddw %%mm0, %%mm2 \n\t"
216 "paddw %%mm4, %%mm1 \n\t"
217 "movq %%mm3, %%mm4 \n\t"
218 "punpcklbw %%mm7, %%mm3 \n\t"
219 "punpckhbw %%mm7, %%mm4 \n\t"
220 "paddw %%mm3, %%mm2 \n\t"
221 "paddw %%mm4, %%mm1 \n\t"
222 "movq (%3, %%"REG_a"), %%mm3 \n\t"
223 "movq (%3, %%"REG_a"), %%mm4 \n\t"
224 "paddw %%mm5, %%mm2 \n\t"
225 "paddw %%mm5, %%mm1 \n\t"
226 "psrlw $2, %%mm2 \n\t"
227 "psrlw $2, %%mm1 \n\t"
228 "packuswb %%mm1, %%mm2 \n\t"
229 "psubusb %%mm2, %%mm3 \n\t"
230 "psubusb %%mm4, %%mm2 \n\t"
231 "por %%mm3, %%mm2 \n\t"
232 "movq %%mm2, %%mm0 \n\t"
233 "punpcklbw %%mm7, %%mm0 \n\t"
234 "punpckhbw %%mm7, %%mm2 \n\t"
235 "paddw %%mm2, %%mm0 \n\t"
236 "paddw %%mm0, %%mm6 \n\t"
237 "add %4, %%"REG_a" \n\t"
240 : "r" (blk1 - len), "r" (blk1 -len + stride), "r" (blk2 - len), "r" ((long)stride)
244 static inline int sum_mmx(void)
248 "movq %%mm6, %%mm0 \n\t"
249 "psrlq $32, %%mm6 \n\t"
250 "paddw %%mm0, %%mm6 \n\t"
251 "movq %%mm6, %%mm0 \n\t"
252 "psrlq $16, %%mm6 \n\t"
253 "paddw %%mm0, %%mm6 \n\t"
254 "movd %%mm6, %0 \n\t"
260 static inline int sum_mmx2(void)
264 "movd %%mm6, %0 \n\t"
271 #define PIX_SAD(suf)\
272 static int sad8_ ## suf(void *v, uint8_t *blk2, uint8_t *blk1, int stride, int h)\
275 asm volatile("pxor %%mm7, %%mm7 \n\t"\
276 "pxor %%mm6, %%mm6 \n\t":);\
278 sad8_1_ ## suf(blk1, blk2, stride, 8);\
280 return sum_ ## suf();\
282 static int sad8_x2_ ## suf(void *v, uint8_t *blk2, uint8_t *blk1, int stride, int h)\
285 asm volatile("pxor %%mm7, %%mm7 \n\t"\
286 "pxor %%mm6, %%mm6 \n\t"\
287 "movq %0, %%mm5 \n\t"\
288 :: "m"(round_tab[1]) \
291 sad8_2_ ## suf(blk1, blk1+1, blk2, stride, 8);\
293 return sum_ ## suf();\
296 static int sad8_y2_ ## suf(void *v, uint8_t *blk2, uint8_t *blk1, int stride, int h)\
299 asm volatile("pxor %%mm7, %%mm7 \n\t"\
300 "pxor %%mm6, %%mm6 \n\t"\
301 "movq %0, %%mm5 \n\t"\
302 :: "m"(round_tab[1]) \
305 sad8_2_ ## suf(blk1, blk1+stride, blk2, stride, 8);\
307 return sum_ ## suf();\
310 static int sad8_xy2_ ## suf(void *v, uint8_t *blk2, uint8_t *blk1, int stride, int h)\
313 asm volatile("pxor %%mm7, %%mm7 \n\t"\
314 "pxor %%mm6, %%mm6 \n\t"\
315 "movq %0, %%mm5 \n\t"\
316 :: "m"(round_tab[2]) \
319 sad8_4_ ## suf(blk1, blk2, stride, 8);\
321 return sum_ ## suf();\
324 static int sad16_ ## suf(void *v, uint8_t *blk2, uint8_t *blk1, int stride, int h)\
326 asm volatile("pxor %%mm7, %%mm7 \n\t"\
327 "pxor %%mm6, %%mm6 \n\t":);\
329 sad8_1_ ## suf(blk1 , blk2 , stride, h);\
330 sad8_1_ ## suf(blk1+8, blk2+8, stride, h);\
332 return sum_ ## suf();\
334 static int sad16_x2_ ## suf(void *v, uint8_t *blk2, uint8_t *blk1, int stride, int h)\
336 asm volatile("pxor %%mm7, %%mm7 \n\t"\
337 "pxor %%mm6, %%mm6 \n\t"\
338 "movq %0, %%mm5 \n\t"\
339 :: "m"(round_tab[1]) \
342 sad8_2_ ## suf(blk1 , blk1+1, blk2 , stride, h);\
343 sad8_2_ ## suf(blk1+8, blk1+9, blk2+8, stride, h);\
345 return sum_ ## suf();\
347 static int sad16_y2_ ## suf(void *v, uint8_t *blk2, uint8_t *blk1, int stride, int h)\
349 asm volatile("pxor %%mm7, %%mm7 \n\t"\
350 "pxor %%mm6, %%mm6 \n\t"\
351 "movq %0, %%mm5 \n\t"\
352 :: "m"(round_tab[1]) \
355 sad8_2_ ## suf(blk1 , blk1+stride, blk2 , stride, h);\
356 sad8_2_ ## suf(blk1+8, blk1+stride+8,blk2+8, stride, h);\
358 return sum_ ## suf();\
360 static int sad16_xy2_ ## suf(void *v, uint8_t *blk2, uint8_t *blk1, int stride, int h)\
362 asm volatile("pxor %%mm7, %%mm7 \n\t"\
363 "pxor %%mm6, %%mm6 \n\t"\
364 "movq %0, %%mm5 \n\t"\
365 :: "m"(round_tab[2]) \
368 sad8_4_ ## suf(blk1 , blk2 , stride, h);\
369 sad8_4_ ## suf(blk1+8, blk2+8, stride, h);\
371 return sum_ ## suf();\
377 void dsputil_init_pix_mmx(DSPContext* c, AVCodecContext *avctx)
379 if (mm_flags & MM_MMX) {
380 c->pix_abs[0][0] = sad16_mmx;
381 c->pix_abs[0][1] = sad16_x2_mmx;
382 c->pix_abs[0][2] = sad16_y2_mmx;
383 c->pix_abs[0][3] = sad16_xy2_mmx;
384 c->pix_abs[1][0] = sad8_mmx;
385 c->pix_abs[1][1] = sad8_x2_mmx;
386 c->pix_abs[1][2] = sad8_y2_mmx;
387 c->pix_abs[1][3] = sad8_xy2_mmx;
389 c->sad[0]= sad16_mmx;
392 if (mm_flags & MM_MMXEXT) {
393 c->pix_abs[0][0] = sad16_mmx2;
394 c->pix_abs[1][0] = sad8_mmx2;
396 c->sad[0]= sad16_mmx2;
397 c->sad[1]= sad8_mmx2;
399 if(!(avctx->flags & CODEC_FLAG_BITEXACT)){
400 c->pix_abs[0][1] = sad16_x2_mmx2;
401 c->pix_abs[0][2] = sad16_y2_mmx2;
402 c->pix_abs[0][3] = sad16_xy2_mmx2;
403 c->pix_abs[1][1] = sad8_x2_mmx2;
404 c->pix_abs[1][2] = sad8_y2_mmx2;
405 c->pix_abs[1][3] = sad8_xy2_mmx2;