]> git.sesse.net Git - ffmpeg/blob - libavcodec/x86/dsputilenc_mmx.c
Drop DCTELEM typedef
[ffmpeg] / libavcodec / x86 / dsputilenc_mmx.c
1 /*
2  * MMX optimized DSP utils
3  * Copyright (c) 2000, 2001 Fabrice Bellard
4  * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
5  *
6  * This file is part of Libav.
7  *
8  * Libav 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.
12  *
13  * Libav 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.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with Libav; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  *
22  * MMX optimization by Nick Kurshev <nickols_k@mail.ru>
23  */
24
25 #include "libavutil/cpu.h"
26 #include "libavutil/x86/asm.h"
27 #include "libavutil/x86/cpu.h"
28 #include "libavcodec/dsputil.h"
29 #include "libavcodec/mpegvideo.h"
30 #include "libavcodec/mathops.h"
31 #include "dsputil_mmx.h"
32
33 void ff_get_pixels_mmx(int16_t *block, const uint8_t *pixels, int line_size);
34 void ff_get_pixels_sse2(int16_t *block, const uint8_t *pixels, int line_size);
35 void ff_diff_pixels_mmx(int16_t *block, const uint8_t *s1, const uint8_t *s2, int stride);
36 int ff_pix_sum16_mmx(uint8_t * pix, int line_size);
37 int ff_pix_norm1_mmx(uint8_t *pix, int line_size);
38
39 #if HAVE_INLINE_ASM
40
41 static int sse8_mmx(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int h) {
42     int tmp;
43   __asm__ volatile (
44       "movl %4,%%ecx\n"
45       "shr $1,%%ecx\n"
46       "pxor %%mm0,%%mm0\n"      /* mm0 = 0 */
47       "pxor %%mm7,%%mm7\n"      /* mm7 holds the sum */
48       "1:\n"
49       "movq (%0),%%mm1\n"       /* mm1 = pix1[0][0-7] */
50       "movq (%1),%%mm2\n"       /* mm2 = pix2[0][0-7] */
51       "movq (%0,%3),%%mm3\n"    /* mm3 = pix1[1][0-7] */
52       "movq (%1,%3),%%mm4\n"    /* mm4 = pix2[1][0-7] */
53
54       /* todo: mm1-mm2, mm3-mm4 */
55       /* algo: subtract mm1 from mm2 with saturation and vice versa */
56       /*       OR the results to get absolute difference */
57       "movq %%mm1,%%mm5\n"
58       "movq %%mm3,%%mm6\n"
59       "psubusb %%mm2,%%mm1\n"
60       "psubusb %%mm4,%%mm3\n"
61       "psubusb %%mm5,%%mm2\n"
62       "psubusb %%mm6,%%mm4\n"
63
64       "por %%mm1,%%mm2\n"
65       "por %%mm3,%%mm4\n"
66
67       /* now convert to 16-bit vectors so we can square them */
68       "movq %%mm2,%%mm1\n"
69       "movq %%mm4,%%mm3\n"
70
71       "punpckhbw %%mm0,%%mm2\n"
72       "punpckhbw %%mm0,%%mm4\n"
73       "punpcklbw %%mm0,%%mm1\n" /* mm1 now spread over (mm1,mm2) */
74       "punpcklbw %%mm0,%%mm3\n" /* mm4 now spread over (mm3,mm4) */
75
76       "pmaddwd %%mm2,%%mm2\n"
77       "pmaddwd %%mm4,%%mm4\n"
78       "pmaddwd %%mm1,%%mm1\n"
79       "pmaddwd %%mm3,%%mm3\n"
80
81       "lea (%0,%3,2), %0\n"     /* pix1 += 2*line_size */
82       "lea (%1,%3,2), %1\n"     /* pix2 += 2*line_size */
83
84       "paddd %%mm2,%%mm1\n"
85       "paddd %%mm4,%%mm3\n"
86       "paddd %%mm1,%%mm7\n"
87       "paddd %%mm3,%%mm7\n"
88
89       "decl %%ecx\n"
90       "jnz 1b\n"
91
92       "movq %%mm7,%%mm1\n"
93       "psrlq $32, %%mm7\n"      /* shift hi dword to lo */
94       "paddd %%mm7,%%mm1\n"
95       "movd %%mm1,%2\n"
96       : "+r" (pix1), "+r" (pix2), "=r"(tmp)
97       : "r" ((x86_reg)line_size) , "m" (h)
98       : "%ecx");
99     return tmp;
100 }
101
102 static int sse16_mmx(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int h) {
103     int tmp;
104   __asm__ volatile (
105       "movl %4,%%ecx\n"
106       "pxor %%mm0,%%mm0\n"      /* mm0 = 0 */
107       "pxor %%mm7,%%mm7\n"      /* mm7 holds the sum */
108       "1:\n"
109       "movq (%0),%%mm1\n"       /* mm1 = pix1[0-7] */
110       "movq (%1),%%mm2\n"       /* mm2 = pix2[0-7] */
111       "movq 8(%0),%%mm3\n"      /* mm3 = pix1[8-15] */
112       "movq 8(%1),%%mm4\n"      /* mm4 = pix2[8-15] */
113
114       /* todo: mm1-mm2, mm3-mm4 */
115       /* algo: subtract mm1 from mm2 with saturation and vice versa */
116       /*       OR the results to get absolute difference */
117       "movq %%mm1,%%mm5\n"
118       "movq %%mm3,%%mm6\n"
119       "psubusb %%mm2,%%mm1\n"
120       "psubusb %%mm4,%%mm3\n"
121       "psubusb %%mm5,%%mm2\n"
122       "psubusb %%mm6,%%mm4\n"
123
124       "por %%mm1,%%mm2\n"
125       "por %%mm3,%%mm4\n"
126
127       /* now convert to 16-bit vectors so we can square them */
128       "movq %%mm2,%%mm1\n"
129       "movq %%mm4,%%mm3\n"
130
131       "punpckhbw %%mm0,%%mm2\n"
132       "punpckhbw %%mm0,%%mm4\n"
133       "punpcklbw %%mm0,%%mm1\n" /* mm1 now spread over (mm1,mm2) */
134       "punpcklbw %%mm0,%%mm3\n" /* mm4 now spread over (mm3,mm4) */
135
136       "pmaddwd %%mm2,%%mm2\n"
137       "pmaddwd %%mm4,%%mm4\n"
138       "pmaddwd %%mm1,%%mm1\n"
139       "pmaddwd %%mm3,%%mm3\n"
140
141       "add %3,%0\n"
142       "add %3,%1\n"
143
144       "paddd %%mm2,%%mm1\n"
145       "paddd %%mm4,%%mm3\n"
146       "paddd %%mm1,%%mm7\n"
147       "paddd %%mm3,%%mm7\n"
148
149       "decl %%ecx\n"
150       "jnz 1b\n"
151
152       "movq %%mm7,%%mm1\n"
153       "psrlq $32, %%mm7\n"      /* shift hi dword to lo */
154       "paddd %%mm7,%%mm1\n"
155       "movd %%mm1,%2\n"
156       : "+r" (pix1), "+r" (pix2), "=r"(tmp)
157       : "r" ((x86_reg)line_size) , "m" (h)
158       : "%ecx");
159     return tmp;
160 }
161
162 static int hf_noise8_mmx(uint8_t * pix1, int line_size, int h) {
163     int tmp;
164   __asm__ volatile (
165       "movl %3,%%ecx\n"
166       "pxor %%mm7,%%mm7\n"
167       "pxor %%mm6,%%mm6\n"
168
169       "movq (%0),%%mm0\n"
170       "movq %%mm0, %%mm1\n"
171       "psllq $8, %%mm0\n"
172       "psrlq $8, %%mm1\n"
173       "psrlq $8, %%mm0\n"
174       "movq %%mm0, %%mm2\n"
175       "movq %%mm1, %%mm3\n"
176       "punpcklbw %%mm7,%%mm0\n"
177       "punpcklbw %%mm7,%%mm1\n"
178       "punpckhbw %%mm7,%%mm2\n"
179       "punpckhbw %%mm7,%%mm3\n"
180       "psubw %%mm1, %%mm0\n"
181       "psubw %%mm3, %%mm2\n"
182
183       "add %2,%0\n"
184
185       "movq (%0),%%mm4\n"
186       "movq %%mm4, %%mm1\n"
187       "psllq $8, %%mm4\n"
188       "psrlq $8, %%mm1\n"
189       "psrlq $8, %%mm4\n"
190       "movq %%mm4, %%mm5\n"
191       "movq %%mm1, %%mm3\n"
192       "punpcklbw %%mm7,%%mm4\n"
193       "punpcklbw %%mm7,%%mm1\n"
194       "punpckhbw %%mm7,%%mm5\n"
195       "punpckhbw %%mm7,%%mm3\n"
196       "psubw %%mm1, %%mm4\n"
197       "psubw %%mm3, %%mm5\n"
198       "psubw %%mm4, %%mm0\n"
199       "psubw %%mm5, %%mm2\n"
200       "pxor %%mm3, %%mm3\n"
201       "pxor %%mm1, %%mm1\n"
202       "pcmpgtw %%mm0, %%mm3\n\t"
203       "pcmpgtw %%mm2, %%mm1\n\t"
204       "pxor %%mm3, %%mm0\n"
205       "pxor %%mm1, %%mm2\n"
206       "psubw %%mm3, %%mm0\n"
207       "psubw %%mm1, %%mm2\n"
208       "paddw %%mm0, %%mm2\n"
209       "paddw %%mm2, %%mm6\n"
210
211       "add %2,%0\n"
212       "1:\n"
213
214       "movq (%0),%%mm0\n"
215       "movq %%mm0, %%mm1\n"
216       "psllq $8, %%mm0\n"
217       "psrlq $8, %%mm1\n"
218       "psrlq $8, %%mm0\n"
219       "movq %%mm0, %%mm2\n"
220       "movq %%mm1, %%mm3\n"
221       "punpcklbw %%mm7,%%mm0\n"
222       "punpcklbw %%mm7,%%mm1\n"
223       "punpckhbw %%mm7,%%mm2\n"
224       "punpckhbw %%mm7,%%mm3\n"
225       "psubw %%mm1, %%mm0\n"
226       "psubw %%mm3, %%mm2\n"
227       "psubw %%mm0, %%mm4\n"
228       "psubw %%mm2, %%mm5\n"
229       "pxor %%mm3, %%mm3\n"
230       "pxor %%mm1, %%mm1\n"
231       "pcmpgtw %%mm4, %%mm3\n\t"
232       "pcmpgtw %%mm5, %%mm1\n\t"
233       "pxor %%mm3, %%mm4\n"
234       "pxor %%mm1, %%mm5\n"
235       "psubw %%mm3, %%mm4\n"
236       "psubw %%mm1, %%mm5\n"
237       "paddw %%mm4, %%mm5\n"
238       "paddw %%mm5, %%mm6\n"
239
240       "add %2,%0\n"
241
242       "movq (%0),%%mm4\n"
243       "movq %%mm4, %%mm1\n"
244       "psllq $8, %%mm4\n"
245       "psrlq $8, %%mm1\n"
246       "psrlq $8, %%mm4\n"
247       "movq %%mm4, %%mm5\n"
248       "movq %%mm1, %%mm3\n"
249       "punpcklbw %%mm7,%%mm4\n"
250       "punpcklbw %%mm7,%%mm1\n"
251       "punpckhbw %%mm7,%%mm5\n"
252       "punpckhbw %%mm7,%%mm3\n"
253       "psubw %%mm1, %%mm4\n"
254       "psubw %%mm3, %%mm5\n"
255       "psubw %%mm4, %%mm0\n"
256       "psubw %%mm5, %%mm2\n"
257       "pxor %%mm3, %%mm3\n"
258       "pxor %%mm1, %%mm1\n"
259       "pcmpgtw %%mm0, %%mm3\n\t"
260       "pcmpgtw %%mm2, %%mm1\n\t"
261       "pxor %%mm3, %%mm0\n"
262       "pxor %%mm1, %%mm2\n"
263       "psubw %%mm3, %%mm0\n"
264       "psubw %%mm1, %%mm2\n"
265       "paddw %%mm0, %%mm2\n"
266       "paddw %%mm2, %%mm6\n"
267
268       "add %2,%0\n"
269       "subl $2, %%ecx\n"
270       " jnz 1b\n"
271
272       "movq %%mm6, %%mm0\n"
273       "punpcklwd %%mm7,%%mm0\n"
274       "punpckhwd %%mm7,%%mm6\n"
275       "paddd %%mm0, %%mm6\n"
276
277       "movq %%mm6,%%mm0\n"
278       "psrlq $32, %%mm6\n"
279       "paddd %%mm6,%%mm0\n"
280       "movd %%mm0,%1\n"
281       : "+r" (pix1), "=r"(tmp)
282       : "r" ((x86_reg)line_size) , "g" (h-2)
283       : "%ecx");
284       return tmp;
285 }
286
287 static int hf_noise16_mmx(uint8_t * pix1, int line_size, int h) {
288     int tmp;
289     uint8_t * pix= pix1;
290   __asm__ volatile (
291       "movl %3,%%ecx\n"
292       "pxor %%mm7,%%mm7\n"
293       "pxor %%mm6,%%mm6\n"
294
295       "movq (%0),%%mm0\n"
296       "movq 1(%0),%%mm1\n"
297       "movq %%mm0, %%mm2\n"
298       "movq %%mm1, %%mm3\n"
299       "punpcklbw %%mm7,%%mm0\n"
300       "punpcklbw %%mm7,%%mm1\n"
301       "punpckhbw %%mm7,%%mm2\n"
302       "punpckhbw %%mm7,%%mm3\n"
303       "psubw %%mm1, %%mm0\n"
304       "psubw %%mm3, %%mm2\n"
305
306       "add %2,%0\n"
307
308       "movq (%0),%%mm4\n"
309       "movq 1(%0),%%mm1\n"
310       "movq %%mm4, %%mm5\n"
311       "movq %%mm1, %%mm3\n"
312       "punpcklbw %%mm7,%%mm4\n"
313       "punpcklbw %%mm7,%%mm1\n"
314       "punpckhbw %%mm7,%%mm5\n"
315       "punpckhbw %%mm7,%%mm3\n"
316       "psubw %%mm1, %%mm4\n"
317       "psubw %%mm3, %%mm5\n"
318       "psubw %%mm4, %%mm0\n"
319       "psubw %%mm5, %%mm2\n"
320       "pxor %%mm3, %%mm3\n"
321       "pxor %%mm1, %%mm1\n"
322       "pcmpgtw %%mm0, %%mm3\n\t"
323       "pcmpgtw %%mm2, %%mm1\n\t"
324       "pxor %%mm3, %%mm0\n"
325       "pxor %%mm1, %%mm2\n"
326       "psubw %%mm3, %%mm0\n"
327       "psubw %%mm1, %%mm2\n"
328       "paddw %%mm0, %%mm2\n"
329       "paddw %%mm2, %%mm6\n"
330
331       "add %2,%0\n"
332       "1:\n"
333
334       "movq (%0),%%mm0\n"
335       "movq 1(%0),%%mm1\n"
336       "movq %%mm0, %%mm2\n"
337       "movq %%mm1, %%mm3\n"
338       "punpcklbw %%mm7,%%mm0\n"
339       "punpcklbw %%mm7,%%mm1\n"
340       "punpckhbw %%mm7,%%mm2\n"
341       "punpckhbw %%mm7,%%mm3\n"
342       "psubw %%mm1, %%mm0\n"
343       "psubw %%mm3, %%mm2\n"
344       "psubw %%mm0, %%mm4\n"
345       "psubw %%mm2, %%mm5\n"
346       "pxor %%mm3, %%mm3\n"
347       "pxor %%mm1, %%mm1\n"
348       "pcmpgtw %%mm4, %%mm3\n\t"
349       "pcmpgtw %%mm5, %%mm1\n\t"
350       "pxor %%mm3, %%mm4\n"
351       "pxor %%mm1, %%mm5\n"
352       "psubw %%mm3, %%mm4\n"
353       "psubw %%mm1, %%mm5\n"
354       "paddw %%mm4, %%mm5\n"
355       "paddw %%mm5, %%mm6\n"
356
357       "add %2,%0\n"
358
359       "movq (%0),%%mm4\n"
360       "movq 1(%0),%%mm1\n"
361       "movq %%mm4, %%mm5\n"
362       "movq %%mm1, %%mm3\n"
363       "punpcklbw %%mm7,%%mm4\n"
364       "punpcklbw %%mm7,%%mm1\n"
365       "punpckhbw %%mm7,%%mm5\n"
366       "punpckhbw %%mm7,%%mm3\n"
367       "psubw %%mm1, %%mm4\n"
368       "psubw %%mm3, %%mm5\n"
369       "psubw %%mm4, %%mm0\n"
370       "psubw %%mm5, %%mm2\n"
371       "pxor %%mm3, %%mm3\n"
372       "pxor %%mm1, %%mm1\n"
373       "pcmpgtw %%mm0, %%mm3\n\t"
374       "pcmpgtw %%mm2, %%mm1\n\t"
375       "pxor %%mm3, %%mm0\n"
376       "pxor %%mm1, %%mm2\n"
377       "psubw %%mm3, %%mm0\n"
378       "psubw %%mm1, %%mm2\n"
379       "paddw %%mm0, %%mm2\n"
380       "paddw %%mm2, %%mm6\n"
381
382       "add %2,%0\n"
383       "subl $2, %%ecx\n"
384       " jnz 1b\n"
385
386       "movq %%mm6, %%mm0\n"
387       "punpcklwd %%mm7,%%mm0\n"
388       "punpckhwd %%mm7,%%mm6\n"
389       "paddd %%mm0, %%mm6\n"
390
391       "movq %%mm6,%%mm0\n"
392       "psrlq $32, %%mm6\n"
393       "paddd %%mm6,%%mm0\n"
394       "movd %%mm0,%1\n"
395       : "+r" (pix1), "=r"(tmp)
396       : "r" ((x86_reg)line_size) , "g" (h-2)
397       : "%ecx");
398       return tmp + hf_noise8_mmx(pix+8, line_size, h);
399 }
400
401 static int nsse16_mmx(void *p, uint8_t * pix1, uint8_t * pix2, int line_size, int h) {
402     MpegEncContext *c = p;
403     int score1, score2;
404
405     if(c) score1 = c->dsp.sse[0](c, pix1, pix2, line_size, h);
406     else  score1 = sse16_mmx(c, pix1, pix2, line_size, h);
407     score2= hf_noise16_mmx(pix1, line_size, h) - hf_noise16_mmx(pix2, line_size, h);
408
409     if(c) return score1 + FFABS(score2)*c->avctx->nsse_weight;
410     else  return score1 + FFABS(score2)*8;
411 }
412
413 static int nsse8_mmx(void *p, uint8_t * pix1, uint8_t * pix2, int line_size, int h) {
414     MpegEncContext *c = p;
415     int score1= sse8_mmx(c, pix1, pix2, line_size, h);
416     int score2= hf_noise8_mmx(pix1, line_size, h) - hf_noise8_mmx(pix2, line_size, h);
417
418     if(c) return score1 + FFABS(score2)*c->avctx->nsse_weight;
419     else  return score1 + FFABS(score2)*8;
420 }
421
422 static int vsad_intra16_mmx(void *v, uint8_t * pix, uint8_t * dummy, int line_size, int h) {
423     int tmp;
424
425     assert( (((int)pix) & 7) == 0);
426     assert((line_size &7) ==0);
427
428 #define SUM(in0, in1, out0, out1) \
429       "movq (%0), %%mm2\n"\
430       "movq 8(%0), %%mm3\n"\
431       "add %2,%0\n"\
432       "movq %%mm2, " #out0 "\n"\
433       "movq %%mm3, " #out1 "\n"\
434       "psubusb " #in0 ", %%mm2\n"\
435       "psubusb " #in1 ", %%mm3\n"\
436       "psubusb " #out0 ", " #in0 "\n"\
437       "psubusb " #out1 ", " #in1 "\n"\
438       "por %%mm2, " #in0 "\n"\
439       "por %%mm3, " #in1 "\n"\
440       "movq " #in0 ", %%mm2\n"\
441       "movq " #in1 ", %%mm3\n"\
442       "punpcklbw %%mm7, " #in0 "\n"\
443       "punpcklbw %%mm7, " #in1 "\n"\
444       "punpckhbw %%mm7, %%mm2\n"\
445       "punpckhbw %%mm7, %%mm3\n"\
446       "paddw " #in1 ", " #in0 "\n"\
447       "paddw %%mm3, %%mm2\n"\
448       "paddw %%mm2, " #in0 "\n"\
449       "paddw " #in0 ", %%mm6\n"
450
451
452   __asm__ volatile (
453       "movl %3,%%ecx\n"
454       "pxor %%mm6,%%mm6\n"
455       "pxor %%mm7,%%mm7\n"
456       "movq (%0),%%mm0\n"
457       "movq 8(%0),%%mm1\n"
458       "add %2,%0\n"
459       "jmp 2f\n"
460       "1:\n"
461
462       SUM(%%mm4, %%mm5, %%mm0, %%mm1)
463       "2:\n"
464       SUM(%%mm0, %%mm1, %%mm4, %%mm5)
465
466       "subl $2, %%ecx\n"
467       "jnz 1b\n"
468
469       "movq %%mm6,%%mm0\n"
470       "psrlq $32, %%mm6\n"
471       "paddw %%mm6,%%mm0\n"
472       "movq %%mm0,%%mm6\n"
473       "psrlq $16, %%mm0\n"
474       "paddw %%mm6,%%mm0\n"
475       "movd %%mm0,%1\n"
476       : "+r" (pix), "=r"(tmp)
477       : "r" ((x86_reg)line_size) , "m" (h)
478       : "%ecx");
479     return tmp & 0xFFFF;
480 }
481 #undef SUM
482
483 static int vsad_intra16_mmxext(void *v, uint8_t *pix, uint8_t *dummy,
484                                int line_size, int h)
485 {
486     int tmp;
487
488     assert( (((int)pix) & 7) == 0);
489     assert((line_size &7) ==0);
490
491 #define SUM(in0, in1, out0, out1) \
492       "movq (%0), " #out0 "\n"\
493       "movq 8(%0), " #out1 "\n"\
494       "add %2,%0\n"\
495       "psadbw " #out0 ", " #in0 "\n"\
496       "psadbw " #out1 ", " #in1 "\n"\
497       "paddw " #in1 ", " #in0 "\n"\
498       "paddw " #in0 ", %%mm6\n"
499
500   __asm__ volatile (
501       "movl %3,%%ecx\n"
502       "pxor %%mm6,%%mm6\n"
503       "pxor %%mm7,%%mm7\n"
504       "movq (%0),%%mm0\n"
505       "movq 8(%0),%%mm1\n"
506       "add %2,%0\n"
507       "jmp 2f\n"
508       "1:\n"
509
510       SUM(%%mm4, %%mm5, %%mm0, %%mm1)
511       "2:\n"
512       SUM(%%mm0, %%mm1, %%mm4, %%mm5)
513
514       "subl $2, %%ecx\n"
515       "jnz 1b\n"
516
517       "movd %%mm6,%1\n"
518       : "+r" (pix), "=r"(tmp)
519       : "r" ((x86_reg)line_size) , "m" (h)
520       : "%ecx");
521     return tmp;
522 }
523 #undef SUM
524
525 static int vsad16_mmx(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int h) {
526     int tmp;
527
528     assert( (((int)pix1) & 7) == 0);
529     assert( (((int)pix2) & 7) == 0);
530     assert((line_size &7) ==0);
531
532 #define SUM(in0, in1, out0, out1) \
533       "movq (%0),%%mm2\n"\
534       "movq (%1)," #out0 "\n"\
535       "movq 8(%0),%%mm3\n"\
536       "movq 8(%1)," #out1 "\n"\
537       "add %3,%0\n"\
538       "add %3,%1\n"\
539       "psubb " #out0 ", %%mm2\n"\
540       "psubb " #out1 ", %%mm3\n"\
541       "pxor %%mm7, %%mm2\n"\
542       "pxor %%mm7, %%mm3\n"\
543       "movq %%mm2, " #out0 "\n"\
544       "movq %%mm3, " #out1 "\n"\
545       "psubusb " #in0 ", %%mm2\n"\
546       "psubusb " #in1 ", %%mm3\n"\
547       "psubusb " #out0 ", " #in0 "\n"\
548       "psubusb " #out1 ", " #in1 "\n"\
549       "por %%mm2, " #in0 "\n"\
550       "por %%mm3, " #in1 "\n"\
551       "movq " #in0 ", %%mm2\n"\
552       "movq " #in1 ", %%mm3\n"\
553       "punpcklbw %%mm7, " #in0 "\n"\
554       "punpcklbw %%mm7, " #in1 "\n"\
555       "punpckhbw %%mm7, %%mm2\n"\
556       "punpckhbw %%mm7, %%mm3\n"\
557       "paddw " #in1 ", " #in0 "\n"\
558       "paddw %%mm3, %%mm2\n"\
559       "paddw %%mm2, " #in0 "\n"\
560       "paddw " #in0 ", %%mm6\n"
561
562
563   __asm__ volatile (
564       "movl %4,%%ecx\n"
565       "pxor %%mm6,%%mm6\n"
566       "pcmpeqw %%mm7,%%mm7\n"
567       "psllw $15, %%mm7\n"
568       "packsswb %%mm7, %%mm7\n"
569       "movq (%0),%%mm0\n"
570       "movq (%1),%%mm2\n"
571       "movq 8(%0),%%mm1\n"
572       "movq 8(%1),%%mm3\n"
573       "add %3,%0\n"
574       "add %3,%1\n"
575       "psubb %%mm2, %%mm0\n"
576       "psubb %%mm3, %%mm1\n"
577       "pxor %%mm7, %%mm0\n"
578       "pxor %%mm7, %%mm1\n"
579       "jmp 2f\n"
580       "1:\n"
581
582       SUM(%%mm4, %%mm5, %%mm0, %%mm1)
583       "2:\n"
584       SUM(%%mm0, %%mm1, %%mm4, %%mm5)
585
586       "subl $2, %%ecx\n"
587       "jnz 1b\n"
588
589       "movq %%mm6,%%mm0\n"
590       "psrlq $32, %%mm6\n"
591       "paddw %%mm6,%%mm0\n"
592       "movq %%mm0,%%mm6\n"
593       "psrlq $16, %%mm0\n"
594       "paddw %%mm6,%%mm0\n"
595       "movd %%mm0,%2\n"
596       : "+r" (pix1), "+r" (pix2), "=r"(tmp)
597       : "r" ((x86_reg)line_size) , "m" (h)
598       : "%ecx");
599     return tmp & 0x7FFF;
600 }
601 #undef SUM
602
603 static int vsad16_mmxext(void *v, uint8_t *pix1, uint8_t *pix2,
604                          int line_size, int h)
605 {
606     int tmp;
607
608     assert( (((int)pix1) & 7) == 0);
609     assert( (((int)pix2) & 7) == 0);
610     assert((line_size &7) ==0);
611
612 #define SUM(in0, in1, out0, out1) \
613       "movq (%0)," #out0 "\n"\
614       "movq (%1),%%mm2\n"\
615       "movq 8(%0)," #out1 "\n"\
616       "movq 8(%1),%%mm3\n"\
617       "add %3,%0\n"\
618       "add %3,%1\n"\
619       "psubb %%mm2, " #out0 "\n"\
620       "psubb %%mm3, " #out1 "\n"\
621       "pxor %%mm7, " #out0 "\n"\
622       "pxor %%mm7, " #out1 "\n"\
623       "psadbw " #out0 ", " #in0 "\n"\
624       "psadbw " #out1 ", " #in1 "\n"\
625       "paddw " #in1 ", " #in0 "\n"\
626       "paddw " #in0 ", %%mm6\n"
627
628   __asm__ volatile (
629       "movl %4,%%ecx\n"
630       "pxor %%mm6,%%mm6\n"
631       "pcmpeqw %%mm7,%%mm7\n"
632       "psllw $15, %%mm7\n"
633       "packsswb %%mm7, %%mm7\n"
634       "movq (%0),%%mm0\n"
635       "movq (%1),%%mm2\n"
636       "movq 8(%0),%%mm1\n"
637       "movq 8(%1),%%mm3\n"
638       "add %3,%0\n"
639       "add %3,%1\n"
640       "psubb %%mm2, %%mm0\n"
641       "psubb %%mm3, %%mm1\n"
642       "pxor %%mm7, %%mm0\n"
643       "pxor %%mm7, %%mm1\n"
644       "jmp 2f\n"
645       "1:\n"
646
647       SUM(%%mm4, %%mm5, %%mm0, %%mm1)
648       "2:\n"
649       SUM(%%mm0, %%mm1, %%mm4, %%mm5)
650
651       "subl $2, %%ecx\n"
652       "jnz 1b\n"
653
654       "movd %%mm6,%2\n"
655       : "+r" (pix1), "+r" (pix2), "=r"(tmp)
656       : "r" ((x86_reg)line_size) , "m" (h)
657       : "%ecx");
658     return tmp;
659 }
660 #undef SUM
661
662 static void diff_bytes_mmx(uint8_t *dst, uint8_t *src1, uint8_t *src2, int w){
663     x86_reg i=0;
664     __asm__ volatile(
665         "1:                             \n\t"
666         "movq  (%2, %0), %%mm0          \n\t"
667         "movq  (%1, %0), %%mm1          \n\t"
668         "psubb %%mm0, %%mm1             \n\t"
669         "movq %%mm1, (%3, %0)           \n\t"
670         "movq 8(%2, %0), %%mm0          \n\t"
671         "movq 8(%1, %0), %%mm1          \n\t"
672         "psubb %%mm0, %%mm1             \n\t"
673         "movq %%mm1, 8(%3, %0)          \n\t"
674         "add $16, %0                    \n\t"
675         "cmp %4, %0                     \n\t"
676         " jb 1b                         \n\t"
677         : "+r" (i)
678         : "r"(src1), "r"(src2), "r"(dst), "r"((x86_reg)w-15)
679     );
680     for(; i<w; i++)
681         dst[i+0] = src1[i+0]-src2[i+0];
682 }
683
684 static void sub_hfyu_median_prediction_mmxext(uint8_t *dst, const uint8_t *src1,
685                                               const uint8_t *src2, int w,
686                                               int *left, int *left_top)
687 {
688     x86_reg i=0;
689     uint8_t l, lt;
690
691     __asm__ volatile(
692         "movq  (%1, %0), %%mm0          \n\t" // LT
693         "psllq $8, %%mm0                \n\t"
694         "1:                             \n\t"
695         "movq  (%1, %0), %%mm1          \n\t" // T
696         "movq  -1(%2, %0), %%mm2        \n\t" // L
697         "movq  (%2, %0), %%mm3          \n\t" // X
698         "movq %%mm2, %%mm4              \n\t" // L
699         "psubb %%mm0, %%mm2             \n\t"
700         "paddb %%mm1, %%mm2             \n\t" // L + T - LT
701         "movq %%mm4, %%mm5              \n\t" // L
702         "pmaxub %%mm1, %%mm4            \n\t" // max(T, L)
703         "pminub %%mm5, %%mm1            \n\t" // min(T, L)
704         "pminub %%mm2, %%mm4            \n\t"
705         "pmaxub %%mm1, %%mm4            \n\t"
706         "psubb %%mm4, %%mm3             \n\t" // dst - pred
707         "movq %%mm3, (%3, %0)           \n\t"
708         "add $8, %0                     \n\t"
709         "movq -1(%1, %0), %%mm0         \n\t" // LT
710         "cmp %4, %0                     \n\t"
711         " jb 1b                         \n\t"
712         : "+r" (i)
713         : "r"(src1), "r"(src2), "r"(dst), "r"((x86_reg)w)
714     );
715
716     l= *left;
717     lt= *left_top;
718
719     dst[0]= src2[0] - mid_pred(l, src1[0], (l + src1[0] - lt)&0xFF);
720
721     *left_top= src1[w-1];
722     *left    = src2[w-1];
723 }
724
725 #define MMABS_MMX(a,z)\
726     "pxor " #z ", " #z "              \n\t"\
727     "pcmpgtw " #a ", " #z "           \n\t"\
728     "pxor " #z ", " #a "              \n\t"\
729     "psubw " #z ", " #a "             \n\t"
730
731 #define MMABS_MMXEXT(a, z)                 \
732     "pxor " #z ", " #z "              \n\t"\
733     "psubw " #a ", " #z "             \n\t"\
734     "pmaxsw " #z ", " #a "            \n\t"
735
736 #define MMABS_SSSE3(a,z)\
737     "pabsw " #a ", " #a "             \n\t"
738
739 #define MMABS_SUM(a,z, sum)\
740     MMABS(a,z)\
741     "paddusw " #a ", " #sum "         \n\t"
742
743 /* FIXME: HSUM_* saturates at 64k, while an 8x8 hadamard or dct block can get up to
744  * about 100k on extreme inputs. But that's very unlikely to occur in natural video,
745  * and it's even more unlikely to not have any alternative mvs/modes with lower cost. */
746 #define HSUM_MMX(a, t, dst)\
747     "movq "#a", "#t"                  \n\t"\
748     "psrlq $32, "#a"                  \n\t"\
749     "paddusw "#t", "#a"               \n\t"\
750     "movq "#a", "#t"                  \n\t"\
751     "psrlq $16, "#a"                  \n\t"\
752     "paddusw "#t", "#a"               \n\t"\
753     "movd "#a", "#dst"                \n\t"\
754
755 #define HSUM_MMXEXT(a, t, dst)             \
756     "pshufw $0x0E, "#a", "#t"         \n\t"\
757     "paddusw "#t", "#a"               \n\t"\
758     "pshufw $0x01, "#a", "#t"         \n\t"\
759     "paddusw "#t", "#a"               \n\t"\
760     "movd "#a", "#dst"                \n\t"\
761
762 #define HSUM_SSE2(a, t, dst)\
763     "movhlps "#a", "#t"               \n\t"\
764     "paddusw "#t", "#a"               \n\t"\
765     "pshuflw $0x0E, "#a", "#t"        \n\t"\
766     "paddusw "#t", "#a"               \n\t"\
767     "pshuflw $0x01, "#a", "#t"        \n\t"\
768     "paddusw "#t", "#a"               \n\t"\
769     "movd "#a", "#dst"                \n\t"\
770
771 #define DCT_SAD4(m,mm,o)\
772     "mov"#m" "#o"+ 0(%1), "#mm"2      \n\t"\
773     "mov"#m" "#o"+16(%1), "#mm"3      \n\t"\
774     "mov"#m" "#o"+32(%1), "#mm"4      \n\t"\
775     "mov"#m" "#o"+48(%1), "#mm"5      \n\t"\
776     MMABS_SUM(mm##2, mm##6, mm##0)\
777     MMABS_SUM(mm##3, mm##7, mm##1)\
778     MMABS_SUM(mm##4, mm##6, mm##0)\
779     MMABS_SUM(mm##5, mm##7, mm##1)\
780
781 #define DCT_SAD_MMX\
782     "pxor %%mm0, %%mm0                \n\t"\
783     "pxor %%mm1, %%mm1                \n\t"\
784     DCT_SAD4(q, %%mm, 0)\
785     DCT_SAD4(q, %%mm, 8)\
786     DCT_SAD4(q, %%mm, 64)\
787     DCT_SAD4(q, %%mm, 72)\
788     "paddusw %%mm1, %%mm0             \n\t"\
789     HSUM(%%mm0, %%mm1, %0)
790
791 #define DCT_SAD_SSE2\
792     "pxor %%xmm0, %%xmm0              \n\t"\
793     "pxor %%xmm1, %%xmm1              \n\t"\
794     DCT_SAD4(dqa, %%xmm, 0)\
795     DCT_SAD4(dqa, %%xmm, 64)\
796     "paddusw %%xmm1, %%xmm0           \n\t"\
797     HSUM(%%xmm0, %%xmm1, %0)
798
799 #define DCT_SAD_FUNC(cpu) \
800 static int sum_abs_dctelem_##cpu(int16_t *block){\
801     int sum;\
802     __asm__ volatile(\
803         DCT_SAD\
804         :"=r"(sum)\
805         :"r"(block)\
806     );\
807     return sum&0xFFFF;\
808 }
809
810 #define DCT_SAD       DCT_SAD_MMX
811 #define HSUM(a,t,dst) HSUM_MMX(a,t,dst)
812 #define MMABS(a,z)    MMABS_MMX(a,z)
813 DCT_SAD_FUNC(mmx)
814 #undef MMABS
815 #undef HSUM
816
817 #define HSUM(a,t,dst) HSUM_MMXEXT(a,t,dst)
818 #define MMABS(a,z)    MMABS_MMXEXT(a,z)
819 DCT_SAD_FUNC(mmxext)
820 #undef HSUM
821 #undef DCT_SAD
822
823 #define DCT_SAD       DCT_SAD_SSE2
824 #define HSUM(a,t,dst) HSUM_SSE2(a,t,dst)
825 DCT_SAD_FUNC(sse2)
826 #undef MMABS
827
828 #if HAVE_SSSE3_INLINE
829 #define MMABS(a,z)    MMABS_SSSE3(a,z)
830 DCT_SAD_FUNC(ssse3)
831 #undef MMABS
832 #endif
833 #undef HSUM
834 #undef DCT_SAD
835
836 static int ssd_int8_vs_int16_mmx(const int8_t *pix1, const int16_t *pix2, int size){
837     int sum;
838     x86_reg i=size;
839     __asm__ volatile(
840         "pxor %%mm4, %%mm4 \n"
841         "1: \n"
842         "sub $8, %0 \n"
843         "movq (%2,%0), %%mm2 \n"
844         "movq (%3,%0,2), %%mm0 \n"
845         "movq 8(%3,%0,2), %%mm1 \n"
846         "punpckhbw %%mm2, %%mm3 \n"
847         "punpcklbw %%mm2, %%mm2 \n"
848         "psraw $8, %%mm3 \n"
849         "psraw $8, %%mm2 \n"
850         "psubw %%mm3, %%mm1 \n"
851         "psubw %%mm2, %%mm0 \n"
852         "pmaddwd %%mm1, %%mm1 \n"
853         "pmaddwd %%mm0, %%mm0 \n"
854         "paddd %%mm1, %%mm4 \n"
855         "paddd %%mm0, %%mm4 \n"
856         "jg 1b \n"
857         "movq %%mm4, %%mm3 \n"
858         "psrlq $32, %%mm3 \n"
859         "paddd %%mm3, %%mm4 \n"
860         "movd %%mm4, %1 \n"
861         :"+r"(i), "=r"(sum)
862         :"r"(pix1), "r"(pix2)
863     );
864     return sum;
865 }
866
867 #define PHADDD(a, t)\
868     "movq "#a", "#t"                  \n\t"\
869     "psrlq $32, "#a"                  \n\t"\
870     "paddd "#t", "#a"                 \n\t"
871 /*
872    pmulhw: dst[0-15]=(src[0-15]*dst[0-15])[16-31]
873    pmulhrw: dst[0-15]=(src[0-15]*dst[0-15] + 0x8000)[16-31]
874    pmulhrsw: dst[0-15]=(src[0-15]*dst[0-15] + 0x4000)[15-30]
875  */
876 #define PMULHRW(x, y, s, o)\
877     "pmulhw " #s ", "#x "            \n\t"\
878     "pmulhw " #s ", "#y "            \n\t"\
879     "paddw " #o ", "#x "             \n\t"\
880     "paddw " #o ", "#y "             \n\t"\
881     "psraw $1, "#x "                 \n\t"\
882     "psraw $1, "#y "                 \n\t"
883 #define DEF(x) x ## _mmx
884 #define SET_RND MOVQ_WONE
885 #define SCALE_OFFSET 1
886
887 #include "dsputil_qns_template.c"
888
889 #undef DEF
890 #undef SET_RND
891 #undef SCALE_OFFSET
892 #undef PMULHRW
893
894 #define DEF(x) x ## _3dnow
895 #define SET_RND(x)
896 #define SCALE_OFFSET 0
897 #define PMULHRW(x, y, s, o)\
898     "pmulhrw " #s ", "#x "           \n\t"\
899     "pmulhrw " #s ", "#y "           \n\t"
900
901 #include "dsputil_qns_template.c"
902
903 #undef DEF
904 #undef SET_RND
905 #undef SCALE_OFFSET
906 #undef PMULHRW
907
908 #if HAVE_SSSE3_INLINE
909 #undef PHADDD
910 #define DEF(x) x ## _ssse3
911 #define SET_RND(x)
912 #define SCALE_OFFSET -1
913 #define PHADDD(a, t)\
914     "pshufw $0x0E, "#a", "#t"         \n\t"\
915     "paddd "#t", "#a"                 \n\t" /* faster than phaddd on core2 */
916 #define PMULHRW(x, y, s, o)\
917     "pmulhrsw " #s ", "#x "          \n\t"\
918     "pmulhrsw " #s ", "#y "          \n\t"
919
920 #include "dsputil_qns_template.c"
921
922 #undef DEF
923 #undef SET_RND
924 #undef SCALE_OFFSET
925 #undef PMULHRW
926 #undef PHADDD
927 #endif /* HAVE_SSSE3_INLINE */
928
929 #endif /* HAVE_INLINE_ASM */
930
931 int ff_sse16_sse2(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int h);
932
933 #define hadamard_func(cpu) \
934 int ff_hadamard8_diff_##cpu  (void *s, uint8_t *src1, uint8_t *src2, \
935                               int stride, int h); \
936 int ff_hadamard8_diff16_##cpu(void *s, uint8_t *src1, uint8_t *src2, \
937                               int stride, int h);
938
939 hadamard_func(mmx)
940 hadamard_func(mmxext)
941 hadamard_func(sse2)
942 hadamard_func(ssse3)
943
944 void ff_dsputilenc_init_mmx(DSPContext* c, AVCodecContext *avctx)
945 {
946     int mm_flags = av_get_cpu_flags();
947     int bit_depth = avctx->bits_per_raw_sample;
948
949 #if HAVE_YASM
950     if (EXTERNAL_MMX(mm_flags)) {
951         if (bit_depth <= 8)
952             c->get_pixels = ff_get_pixels_mmx;
953         c->diff_pixels = ff_diff_pixels_mmx;
954         c->pix_sum = ff_pix_sum16_mmx;
955
956         c->pix_norm1 = ff_pix_norm1_mmx;
957     }
958     if (EXTERNAL_SSE2(mm_flags))
959         if (bit_depth <= 8)
960             c->get_pixels = ff_get_pixels_sse2;
961 #endif /* HAVE_YASM */
962
963 #if HAVE_INLINE_ASM
964     if (mm_flags & AV_CPU_FLAG_MMX) {
965         const int dct_algo = avctx->dct_algo;
966         if (avctx->bits_per_raw_sample <= 8 &&
967             (dct_algo==FF_DCT_AUTO || dct_algo==FF_DCT_MMX)) {
968             if(mm_flags & AV_CPU_FLAG_SSE2){
969                 c->fdct = ff_fdct_sse2;
970             } else if (mm_flags & AV_CPU_FLAG_MMXEXT) {
971                 c->fdct = ff_fdct_mmxext;
972             }else{
973                 c->fdct = ff_fdct_mmx;
974             }
975         }
976
977
978         c->diff_bytes= diff_bytes_mmx;
979         c->sum_abs_dctelem= sum_abs_dctelem_mmx;
980
981         c->sse[0] = sse16_mmx;
982         c->sse[1] = sse8_mmx;
983         c->vsad[4]= vsad_intra16_mmx;
984
985         c->nsse[0] = nsse16_mmx;
986         c->nsse[1] = nsse8_mmx;
987         if(!(avctx->flags & CODEC_FLAG_BITEXACT)){
988             c->vsad[0] = vsad16_mmx;
989         }
990
991         if(!(avctx->flags & CODEC_FLAG_BITEXACT)){
992             c->try_8x8basis= try_8x8basis_mmx;
993         }
994         c->add_8x8basis= add_8x8basis_mmx;
995
996         c->ssd_int8_vs_int16 = ssd_int8_vs_int16_mmx;
997
998         if (mm_flags & AV_CPU_FLAG_MMXEXT) {
999             c->sum_abs_dctelem = sum_abs_dctelem_mmxext;
1000             c->vsad[4]         = vsad_intra16_mmxext;
1001
1002             if(!(avctx->flags & CODEC_FLAG_BITEXACT)){
1003                 c->vsad[0] = vsad16_mmxext;
1004             }
1005
1006             c->sub_hfyu_median_prediction = sub_hfyu_median_prediction_mmxext;
1007         }
1008
1009         if(mm_flags & AV_CPU_FLAG_SSE2){
1010             c->sum_abs_dctelem= sum_abs_dctelem_sse2;
1011         }
1012
1013 #if HAVE_SSSE3_INLINE
1014         if(mm_flags & AV_CPU_FLAG_SSSE3){
1015             if(!(avctx->flags & CODEC_FLAG_BITEXACT)){
1016                 c->try_8x8basis= try_8x8basis_ssse3;
1017             }
1018             c->add_8x8basis= add_8x8basis_ssse3;
1019             c->sum_abs_dctelem= sum_abs_dctelem_ssse3;
1020         }
1021 #endif
1022
1023         if(mm_flags & AV_CPU_FLAG_3DNOW){
1024             if(!(avctx->flags & CODEC_FLAG_BITEXACT)){
1025                 c->try_8x8basis= try_8x8basis_3dnow;
1026             }
1027             c->add_8x8basis= add_8x8basis_3dnow;
1028         }
1029     }
1030 #endif /* HAVE_INLINE_ASM */
1031
1032     if (EXTERNAL_MMX(mm_flags)) {
1033         c->hadamard8_diff[0] = ff_hadamard8_diff16_mmx;
1034         c->hadamard8_diff[1] = ff_hadamard8_diff_mmx;
1035
1036         if (EXTERNAL_MMXEXT(mm_flags)) {
1037             c->hadamard8_diff[0] = ff_hadamard8_diff16_mmxext;
1038             c->hadamard8_diff[1] = ff_hadamard8_diff_mmxext;
1039         }
1040
1041         if (EXTERNAL_SSE2(mm_flags)) {
1042             c->sse[0] = ff_sse16_sse2;
1043
1044 #if HAVE_ALIGNED_STACK
1045             c->hadamard8_diff[0] = ff_hadamard8_diff16_sse2;
1046             c->hadamard8_diff[1] = ff_hadamard8_diff_sse2;
1047 #endif
1048         }
1049
1050         if (EXTERNAL_SSSE3(mm_flags) && HAVE_ALIGNED_STACK) {
1051             c->hadamard8_diff[0] = ff_hadamard8_diff16_ssse3;
1052             c->hadamard8_diff[1] = ff_hadamard8_diff_ssse3;
1053         }
1054     }
1055
1056     ff_dsputil_init_pix_mmx(c, avctx);
1057 }