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