46ed30a76de90cc363b1cb2dbbb11fb0a63e0dc8
[ffmpeg.git] / libavcodec / x86 / mpegvideo_mmx.c
1 /*
2  * The simplest mpeg encoder (well, it was the simplest!)
3  * Copyright (c) 2000,2001 Fabrice Bellard
4  *
5  * Optimized for ia32 CPUs by Nick Kurshev <nickols_k@mail.ru>
6  * h263, mpeg1, mpeg2 dequantizer & draw_edges by Michael Niedermayer <michaelni@gmx.at>
7  *
8  * This file is part of Libav.
9  *
10  * Libav 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  * Libav 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 Libav; 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/cpu.h"
26 #include "libavutil/x86/asm.h"
27 #include "libavcodec/avcodec.h"
28 #include "libavcodec/dsputil.h"
29 #include "libavcodec/mpegvideo.h"
30 #include "dsputil_mmx.h"
31
32 #if HAVE_INLINE_ASM
33
34 extern uint16_t ff_inv_zigzag_direct16[64];
35
36
37 static void dct_unquantize_h263_intra_mmx(MpegEncContext *s,
38                                   DCTELEM *block, int n, int qscale)
39 {
40     x86_reg level, qmul, qadd, nCoeffs;
41
42     qmul = qscale << 1;
43
44     assert(s->block_last_index[n]>=0 || s->h263_aic);
45
46     if (!s->h263_aic) {
47         if (n < 4)
48             level = block[0] * s->y_dc_scale;
49         else
50             level = block[0] * s->c_dc_scale;
51         qadd = (qscale - 1) | 1;
52     }else{
53         qadd = 0;
54         level= block[0];
55     }
56     if(s->ac_pred)
57         nCoeffs=63;
58     else
59         nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
60 //printf("%d %d  ", qmul, qadd);
61 __asm__ volatile(
62                 "movd %1, %%mm6                 \n\t" //qmul
63                 "packssdw %%mm6, %%mm6          \n\t"
64                 "packssdw %%mm6, %%mm6          \n\t"
65                 "movd %2, %%mm5                 \n\t" //qadd
66                 "pxor %%mm7, %%mm7              \n\t"
67                 "packssdw %%mm5, %%mm5          \n\t"
68                 "packssdw %%mm5, %%mm5          \n\t"
69                 "psubw %%mm5, %%mm7             \n\t"
70                 "pxor %%mm4, %%mm4              \n\t"
71                 ".p2align 4                     \n\t"
72                 "1:                             \n\t"
73                 "movq (%0, %3), %%mm0           \n\t"
74                 "movq 8(%0, %3), %%mm1          \n\t"
75
76                 "pmullw %%mm6, %%mm0            \n\t"
77                 "pmullw %%mm6, %%mm1            \n\t"
78
79                 "movq (%0, %3), %%mm2           \n\t"
80                 "movq 8(%0, %3), %%mm3          \n\t"
81
82                 "pcmpgtw %%mm4, %%mm2           \n\t" // block[i] < 0 ? -1 : 0
83                 "pcmpgtw %%mm4, %%mm3           \n\t" // block[i] < 0 ? -1 : 0
84
85                 "pxor %%mm2, %%mm0              \n\t"
86                 "pxor %%mm3, %%mm1              \n\t"
87
88                 "paddw %%mm7, %%mm0             \n\t"
89                 "paddw %%mm7, %%mm1             \n\t"
90
91                 "pxor %%mm0, %%mm2              \n\t"
92                 "pxor %%mm1, %%mm3              \n\t"
93
94                 "pcmpeqw %%mm7, %%mm0           \n\t" // block[i] == 0 ? -1 : 0
95                 "pcmpeqw %%mm7, %%mm1           \n\t" // block[i] == 0 ? -1 : 0
96
97                 "pandn %%mm2, %%mm0             \n\t"
98                 "pandn %%mm3, %%mm1             \n\t"
99
100                 "movq %%mm0, (%0, %3)           \n\t"
101                 "movq %%mm1, 8(%0, %3)          \n\t"
102
103                 "add $16, %3                    \n\t"
104                 "jng 1b                         \n\t"
105                 ::"r" (block+nCoeffs), "rm"(qmul), "rm" (qadd), "r" (2*(-nCoeffs))
106                 : "memory"
107         );
108         block[0]= level;
109 }
110
111
112 static void dct_unquantize_h263_inter_mmx(MpegEncContext *s,
113                                   DCTELEM *block, int n, int qscale)
114 {
115     x86_reg qmul, qadd, nCoeffs;
116
117     qmul = qscale << 1;
118     qadd = (qscale - 1) | 1;
119
120     assert(s->block_last_index[n]>=0 || s->h263_aic);
121
122     nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
123 //printf("%d %d  ", qmul, qadd);
124 __asm__ volatile(
125                 "movd %1, %%mm6                 \n\t" //qmul
126                 "packssdw %%mm6, %%mm6          \n\t"
127                 "packssdw %%mm6, %%mm6          \n\t"
128                 "movd %2, %%mm5                 \n\t" //qadd
129                 "pxor %%mm7, %%mm7              \n\t"
130                 "packssdw %%mm5, %%mm5          \n\t"
131                 "packssdw %%mm5, %%mm5          \n\t"
132                 "psubw %%mm5, %%mm7             \n\t"
133                 "pxor %%mm4, %%mm4              \n\t"
134                 ".p2align 4                     \n\t"
135                 "1:                             \n\t"
136                 "movq (%0, %3), %%mm0           \n\t"
137                 "movq 8(%0, %3), %%mm1          \n\t"
138
139                 "pmullw %%mm6, %%mm0            \n\t"
140                 "pmullw %%mm6, %%mm1            \n\t"
141
142                 "movq (%0, %3), %%mm2           \n\t"
143                 "movq 8(%0, %3), %%mm3          \n\t"
144
145                 "pcmpgtw %%mm4, %%mm2           \n\t" // block[i] < 0 ? -1 : 0
146                 "pcmpgtw %%mm4, %%mm3           \n\t" // block[i] < 0 ? -1 : 0
147
148                 "pxor %%mm2, %%mm0              \n\t"
149                 "pxor %%mm3, %%mm1              \n\t"
150
151                 "paddw %%mm7, %%mm0             \n\t"
152                 "paddw %%mm7, %%mm1             \n\t"
153
154                 "pxor %%mm0, %%mm2              \n\t"
155                 "pxor %%mm1, %%mm3              \n\t"
156
157                 "pcmpeqw %%mm7, %%mm0           \n\t" // block[i] == 0 ? -1 : 0
158                 "pcmpeqw %%mm7, %%mm1           \n\t" // block[i] == 0 ? -1 : 0
159
160                 "pandn %%mm2, %%mm0             \n\t"
161                 "pandn %%mm3, %%mm1             \n\t"
162
163                 "movq %%mm0, (%0, %3)           \n\t"
164                 "movq %%mm1, 8(%0, %3)          \n\t"
165
166                 "add $16, %3                    \n\t"
167                 "jng 1b                         \n\t"
168                 ::"r" (block+nCoeffs), "rm"(qmul), "rm" (qadd), "r" (2*(-nCoeffs))
169                 : "memory"
170         );
171 }
172
173
174 /*
175   NK:
176   Note: looking at PARANOID:
177   "enable all paranoid tests for rounding, overflows, etc..."
178
179 #ifdef PARANOID
180                 if (level < -2048 || level > 2047)
181                     fprintf(stderr, "unquant error %d %d\n", i, level);
182 #endif
183   We can suppose that result of two multiplications can't be greater than 0xFFFF
184   i.e. is 16-bit, so we use here only PMULLW instruction and can avoid
185   a complex multiplication.
186 =====================================================
187  Full formula for multiplication of 2 integer numbers
188  which are represent as high:low words:
189  input: value1 = high1:low1
190         value2 = high2:low2
191  output: value3 = value1*value2
192  value3=high3:low3 (on overflow: modulus 2^32 wrap-around)
193  this mean that for 0x123456 * 0x123456 correct result is 0x766cb0ce4
194  but this algorithm will compute only 0x66cb0ce4
195  this limited by 16-bit size of operands
196  ---------------------------------
197  tlow1 = high1*low2
198  tlow2 = high2*low1
199  tlow1 = tlow1 + tlow2
200  high3:low3 = low1*low2
201  high3 += tlow1
202 */
203 static void dct_unquantize_mpeg1_intra_mmx(MpegEncContext *s,
204                                      DCTELEM *block, int n, int qscale)
205 {
206     x86_reg nCoeffs;
207     const uint16_t *quant_matrix;
208     int block0;
209
210     assert(s->block_last_index[n]>=0);
211
212     nCoeffs= s->intra_scantable.raster_end[ s->block_last_index[n] ]+1;
213
214     if (n < 4)
215         block0 = block[0] * s->y_dc_scale;
216     else
217         block0 = block[0] * s->c_dc_scale;
218     /* XXX: only mpeg1 */
219     quant_matrix = s->intra_matrix;
220 __asm__ volatile(
221                 "pcmpeqw %%mm7, %%mm7           \n\t"
222                 "psrlw $15, %%mm7               \n\t"
223                 "movd %2, %%mm6                 \n\t"
224                 "packssdw %%mm6, %%mm6          \n\t"
225                 "packssdw %%mm6, %%mm6          \n\t"
226                 "mov %3, %%"REG_a"              \n\t"
227                 ".p2align 4                     \n\t"
228                 "1:                             \n\t"
229                 "movq (%0, %%"REG_a"), %%mm0    \n\t"
230                 "movq 8(%0, %%"REG_a"), %%mm1   \n\t"
231                 "movq (%1, %%"REG_a"), %%mm4    \n\t"
232                 "movq 8(%1, %%"REG_a"), %%mm5   \n\t"
233                 "pmullw %%mm6, %%mm4            \n\t" // q=qscale*quant_matrix[i]
234                 "pmullw %%mm6, %%mm5            \n\t" // q=qscale*quant_matrix[i]
235                 "pxor %%mm2, %%mm2              \n\t"
236                 "pxor %%mm3, %%mm3              \n\t"
237                 "pcmpgtw %%mm0, %%mm2           \n\t" // block[i] < 0 ? -1 : 0
238                 "pcmpgtw %%mm1, %%mm3           \n\t" // block[i] < 0 ? -1 : 0
239                 "pxor %%mm2, %%mm0              \n\t"
240                 "pxor %%mm3, %%mm1              \n\t"
241                 "psubw %%mm2, %%mm0             \n\t" // abs(block[i])
242                 "psubw %%mm3, %%mm1             \n\t" // abs(block[i])
243                 "pmullw %%mm4, %%mm0            \n\t" // abs(block[i])*q
244                 "pmullw %%mm5, %%mm1            \n\t" // abs(block[i])*q
245                 "pxor %%mm4, %%mm4              \n\t"
246                 "pxor %%mm5, %%mm5              \n\t" // FIXME slow
247                 "pcmpeqw (%0, %%"REG_a"), %%mm4 \n\t" // block[i] == 0 ? -1 : 0
248                 "pcmpeqw 8(%0, %%"REG_a"), %%mm5\n\t" // block[i] == 0 ? -1 : 0
249                 "psraw $3, %%mm0                \n\t"
250                 "psraw $3, %%mm1                \n\t"
251                 "psubw %%mm7, %%mm0             \n\t"
252                 "psubw %%mm7, %%mm1             \n\t"
253                 "por %%mm7, %%mm0               \n\t"
254                 "por %%mm7, %%mm1               \n\t"
255                 "pxor %%mm2, %%mm0              \n\t"
256                 "pxor %%mm3, %%mm1              \n\t"
257                 "psubw %%mm2, %%mm0             \n\t"
258                 "psubw %%mm3, %%mm1             \n\t"
259                 "pandn %%mm0, %%mm4             \n\t"
260                 "pandn %%mm1, %%mm5             \n\t"
261                 "movq %%mm4, (%0, %%"REG_a")    \n\t"
262                 "movq %%mm5, 8(%0, %%"REG_a")   \n\t"
263
264                 "add $16, %%"REG_a"             \n\t"
265                 "js 1b                          \n\t"
266                 ::"r" (block+nCoeffs), "r"(quant_matrix+nCoeffs), "rm" (qscale), "g" (-2*nCoeffs)
267                 : "%"REG_a, "memory"
268         );
269     block[0]= block0;
270 }
271
272 static void dct_unquantize_mpeg1_inter_mmx(MpegEncContext *s,
273                                      DCTELEM *block, int n, int qscale)
274 {
275     x86_reg nCoeffs;
276     const uint16_t *quant_matrix;
277
278     assert(s->block_last_index[n]>=0);
279
280     nCoeffs= s->intra_scantable.raster_end[ s->block_last_index[n] ]+1;
281
282         quant_matrix = s->inter_matrix;
283 __asm__ volatile(
284                 "pcmpeqw %%mm7, %%mm7           \n\t"
285                 "psrlw $15, %%mm7               \n\t"
286                 "movd %2, %%mm6                 \n\t"
287                 "packssdw %%mm6, %%mm6          \n\t"
288                 "packssdw %%mm6, %%mm6          \n\t"
289                 "mov %3, %%"REG_a"              \n\t"
290                 ".p2align 4                     \n\t"
291                 "1:                             \n\t"
292                 "movq (%0, %%"REG_a"), %%mm0    \n\t"
293                 "movq 8(%0, %%"REG_a"), %%mm1   \n\t"
294                 "movq (%1, %%"REG_a"), %%mm4    \n\t"
295                 "movq 8(%1, %%"REG_a"), %%mm5   \n\t"
296                 "pmullw %%mm6, %%mm4            \n\t" // q=qscale*quant_matrix[i]
297                 "pmullw %%mm6, %%mm5            \n\t" // q=qscale*quant_matrix[i]
298                 "pxor %%mm2, %%mm2              \n\t"
299                 "pxor %%mm3, %%mm3              \n\t"
300                 "pcmpgtw %%mm0, %%mm2           \n\t" // block[i] < 0 ? -1 : 0
301                 "pcmpgtw %%mm1, %%mm3           \n\t" // block[i] < 0 ? -1 : 0
302                 "pxor %%mm2, %%mm0              \n\t"
303                 "pxor %%mm3, %%mm1              \n\t"
304                 "psubw %%mm2, %%mm0             \n\t" // abs(block[i])
305                 "psubw %%mm3, %%mm1             \n\t" // abs(block[i])
306                 "paddw %%mm0, %%mm0             \n\t" // abs(block[i])*2
307                 "paddw %%mm1, %%mm1             \n\t" // abs(block[i])*2
308                 "paddw %%mm7, %%mm0             \n\t" // abs(block[i])*2 + 1
309                 "paddw %%mm7, %%mm1             \n\t" // abs(block[i])*2 + 1
310                 "pmullw %%mm4, %%mm0            \n\t" // (abs(block[i])*2 + 1)*q
311                 "pmullw %%mm5, %%mm1            \n\t" // (abs(block[i])*2 + 1)*q
312                 "pxor %%mm4, %%mm4              \n\t"
313                 "pxor %%mm5, %%mm5              \n\t" // FIXME slow
314                 "pcmpeqw (%0, %%"REG_a"), %%mm4 \n\t" // block[i] == 0 ? -1 : 0
315                 "pcmpeqw 8(%0, %%"REG_a"), %%mm5\n\t" // block[i] == 0 ? -1 : 0
316                 "psraw $4, %%mm0                \n\t"
317                 "psraw $4, %%mm1                \n\t"
318                 "psubw %%mm7, %%mm0             \n\t"
319                 "psubw %%mm7, %%mm1             \n\t"
320                 "por %%mm7, %%mm0               \n\t"
321                 "por %%mm7, %%mm1               \n\t"
322                 "pxor %%mm2, %%mm0              \n\t"
323                 "pxor %%mm3, %%mm1              \n\t"
324                 "psubw %%mm2, %%mm0             \n\t"
325                 "psubw %%mm3, %%mm1             \n\t"
326                 "pandn %%mm0, %%mm4             \n\t"
327                 "pandn %%mm1, %%mm5             \n\t"
328                 "movq %%mm4, (%0, %%"REG_a")    \n\t"
329                 "movq %%mm5, 8(%0, %%"REG_a")   \n\t"
330
331                 "add $16, %%"REG_a"             \n\t"
332                 "js 1b                          \n\t"
333                 ::"r" (block+nCoeffs), "r"(quant_matrix+nCoeffs), "rm" (qscale), "g" (-2*nCoeffs)
334                 : "%"REG_a, "memory"
335         );
336 }
337
338 static void dct_unquantize_mpeg2_intra_mmx(MpegEncContext *s,
339                                      DCTELEM *block, int n, int qscale)
340 {
341     x86_reg nCoeffs;
342     const uint16_t *quant_matrix;
343     int block0;
344
345     assert(s->block_last_index[n]>=0);
346
347     if(s->alternate_scan) nCoeffs= 63; //FIXME
348     else nCoeffs= s->intra_scantable.raster_end[ s->block_last_index[n] ];
349
350     if (n < 4)
351         block0 = block[0] * s->y_dc_scale;
352     else
353         block0 = block[0] * s->c_dc_scale;
354     quant_matrix = s->intra_matrix;
355 __asm__ volatile(
356                 "pcmpeqw %%mm7, %%mm7           \n\t"
357                 "psrlw $15, %%mm7               \n\t"
358                 "movd %2, %%mm6                 \n\t"
359                 "packssdw %%mm6, %%mm6          \n\t"
360                 "packssdw %%mm6, %%mm6          \n\t"
361                 "mov %3, %%"REG_a"              \n\t"
362                 ".p2align 4                     \n\t"
363                 "1:                             \n\t"
364                 "movq (%0, %%"REG_a"), %%mm0    \n\t"
365                 "movq 8(%0, %%"REG_a"), %%mm1   \n\t"
366                 "movq (%1, %%"REG_a"), %%mm4    \n\t"
367                 "movq 8(%1, %%"REG_a"), %%mm5   \n\t"
368                 "pmullw %%mm6, %%mm4            \n\t" // q=qscale*quant_matrix[i]
369                 "pmullw %%mm6, %%mm5            \n\t" // q=qscale*quant_matrix[i]
370                 "pxor %%mm2, %%mm2              \n\t"
371                 "pxor %%mm3, %%mm3              \n\t"
372                 "pcmpgtw %%mm0, %%mm2           \n\t" // block[i] < 0 ? -1 : 0
373                 "pcmpgtw %%mm1, %%mm3           \n\t" // block[i] < 0 ? -1 : 0
374                 "pxor %%mm2, %%mm0              \n\t"
375                 "pxor %%mm3, %%mm1              \n\t"
376                 "psubw %%mm2, %%mm0             \n\t" // abs(block[i])
377                 "psubw %%mm3, %%mm1             \n\t" // abs(block[i])
378                 "pmullw %%mm4, %%mm0            \n\t" // abs(block[i])*q
379                 "pmullw %%mm5, %%mm1            \n\t" // abs(block[i])*q
380                 "pxor %%mm4, %%mm4              \n\t"
381                 "pxor %%mm5, %%mm5              \n\t" // FIXME slow
382                 "pcmpeqw (%0, %%"REG_a"), %%mm4 \n\t" // block[i] == 0 ? -1 : 0
383                 "pcmpeqw 8(%0, %%"REG_a"), %%mm5\n\t" // block[i] == 0 ? -1 : 0
384                 "psraw $3, %%mm0                \n\t"
385                 "psraw $3, %%mm1                \n\t"
386                 "pxor %%mm2, %%mm0              \n\t"
387                 "pxor %%mm3, %%mm1              \n\t"
388                 "psubw %%mm2, %%mm0             \n\t"
389                 "psubw %%mm3, %%mm1             \n\t"
390                 "pandn %%mm0, %%mm4             \n\t"
391                 "pandn %%mm1, %%mm5             \n\t"
392                 "movq %%mm4, (%0, %%"REG_a")    \n\t"
393                 "movq %%mm5, 8(%0, %%"REG_a")   \n\t"
394
395                 "add $16, %%"REG_a"             \n\t"
396                 "jng 1b                         \n\t"
397                 ::"r" (block+nCoeffs), "r"(quant_matrix+nCoeffs), "rm" (qscale), "g" (-2*nCoeffs)
398                 : "%"REG_a, "memory"
399         );
400     block[0]= block0;
401         //Note, we do not do mismatch control for intra as errors cannot accumulate
402 }
403
404 static void dct_unquantize_mpeg2_inter_mmx(MpegEncContext *s,
405                                      DCTELEM *block, int n, int qscale)
406 {
407     x86_reg nCoeffs;
408     const uint16_t *quant_matrix;
409
410     assert(s->block_last_index[n]>=0);
411
412     if(s->alternate_scan) nCoeffs= 63; //FIXME
413     else nCoeffs= s->intra_scantable.raster_end[ s->block_last_index[n] ];
414
415         quant_matrix = s->inter_matrix;
416 __asm__ volatile(
417                 "pcmpeqw %%mm7, %%mm7           \n\t"
418                 "psrlq $48, %%mm7               \n\t"
419                 "movd %2, %%mm6                 \n\t"
420                 "packssdw %%mm6, %%mm6          \n\t"
421                 "packssdw %%mm6, %%mm6          \n\t"
422                 "mov %3, %%"REG_a"              \n\t"
423                 ".p2align 4                     \n\t"
424                 "1:                             \n\t"
425                 "movq (%0, %%"REG_a"), %%mm0    \n\t"
426                 "movq 8(%0, %%"REG_a"), %%mm1   \n\t"
427                 "movq (%1, %%"REG_a"), %%mm4    \n\t"
428                 "movq 8(%1, %%"REG_a"), %%mm5   \n\t"
429                 "pmullw %%mm6, %%mm4            \n\t" // q=qscale*quant_matrix[i]
430                 "pmullw %%mm6, %%mm5            \n\t" // q=qscale*quant_matrix[i]
431                 "pxor %%mm2, %%mm2              \n\t"
432                 "pxor %%mm3, %%mm3              \n\t"
433                 "pcmpgtw %%mm0, %%mm2           \n\t" // block[i] < 0 ? -1 : 0
434                 "pcmpgtw %%mm1, %%mm3           \n\t" // block[i] < 0 ? -1 : 0
435                 "pxor %%mm2, %%mm0              \n\t"
436                 "pxor %%mm3, %%mm1              \n\t"
437                 "psubw %%mm2, %%mm0             \n\t" // abs(block[i])
438                 "psubw %%mm3, %%mm1             \n\t" // abs(block[i])
439                 "paddw %%mm0, %%mm0             \n\t" // abs(block[i])*2
440                 "paddw %%mm1, %%mm1             \n\t" // abs(block[i])*2
441                 "pmullw %%mm4, %%mm0            \n\t" // abs(block[i])*2*q
442                 "pmullw %%mm5, %%mm1            \n\t" // abs(block[i])*2*q
443                 "paddw %%mm4, %%mm0             \n\t" // (abs(block[i])*2 + 1)*q
444                 "paddw %%mm5, %%mm1             \n\t" // (abs(block[i])*2 + 1)*q
445                 "pxor %%mm4, %%mm4              \n\t"
446                 "pxor %%mm5, %%mm5              \n\t" // FIXME slow
447                 "pcmpeqw (%0, %%"REG_a"), %%mm4 \n\t" // block[i] == 0 ? -1 : 0
448                 "pcmpeqw 8(%0, %%"REG_a"), %%mm5\n\t" // block[i] == 0 ? -1 : 0
449                 "psrlw $4, %%mm0                \n\t"
450                 "psrlw $4, %%mm1                \n\t"
451                 "pxor %%mm2, %%mm0              \n\t"
452                 "pxor %%mm3, %%mm1              \n\t"
453                 "psubw %%mm2, %%mm0             \n\t"
454                 "psubw %%mm3, %%mm1             \n\t"
455                 "pandn %%mm0, %%mm4             \n\t"
456                 "pandn %%mm1, %%mm5             \n\t"
457                 "pxor %%mm4, %%mm7              \n\t"
458                 "pxor %%mm5, %%mm7              \n\t"
459                 "movq %%mm4, (%0, %%"REG_a")    \n\t"
460                 "movq %%mm5, 8(%0, %%"REG_a")   \n\t"
461
462                 "add $16, %%"REG_a"             \n\t"
463                 "jng 1b                         \n\t"
464                 "movd 124(%0, %3), %%mm0        \n\t"
465                 "movq %%mm7, %%mm6              \n\t"
466                 "psrlq $32, %%mm7               \n\t"
467                 "pxor %%mm6, %%mm7              \n\t"
468                 "movq %%mm7, %%mm6              \n\t"
469                 "psrlq $16, %%mm7               \n\t"
470                 "pxor %%mm6, %%mm7              \n\t"
471                 "pslld $31, %%mm7               \n\t"
472                 "psrlq $15, %%mm7               \n\t"
473                 "pxor %%mm7, %%mm0              \n\t"
474                 "movd %%mm0, 124(%0, %3)        \n\t"
475
476                 ::"r" (block+nCoeffs), "r"(quant_matrix+nCoeffs), "rm" (qscale), "r" (-2*nCoeffs)
477                 : "%"REG_a, "memory"
478         );
479 }
480
481 static void  denoise_dct_mmx(MpegEncContext *s, DCTELEM *block){
482     const int intra= s->mb_intra;
483     int *sum= s->dct_error_sum[intra];
484     uint16_t *offset= s->dct_offset[intra];
485
486     s->dct_count[intra]++;
487
488     __asm__ volatile(
489         "pxor %%mm7, %%mm7                      \n\t"
490         "1:                                     \n\t"
491         "pxor %%mm0, %%mm0                      \n\t"
492         "pxor %%mm1, %%mm1                      \n\t"
493         "movq (%0), %%mm2                       \n\t"
494         "movq 8(%0), %%mm3                      \n\t"
495         "pcmpgtw %%mm2, %%mm0                   \n\t"
496         "pcmpgtw %%mm3, %%mm1                   \n\t"
497         "pxor %%mm0, %%mm2                      \n\t"
498         "pxor %%mm1, %%mm3                      \n\t"
499         "psubw %%mm0, %%mm2                     \n\t"
500         "psubw %%mm1, %%mm3                     \n\t"
501         "movq %%mm2, %%mm4                      \n\t"
502         "movq %%mm3, %%mm5                      \n\t"
503         "psubusw (%2), %%mm2                    \n\t"
504         "psubusw 8(%2), %%mm3                   \n\t"
505         "pxor %%mm0, %%mm2                      \n\t"
506         "pxor %%mm1, %%mm3                      \n\t"
507         "psubw %%mm0, %%mm2                     \n\t"
508         "psubw %%mm1, %%mm3                     \n\t"
509         "movq %%mm2, (%0)                       \n\t"
510         "movq %%mm3, 8(%0)                      \n\t"
511         "movq %%mm4, %%mm2                      \n\t"
512         "movq %%mm5, %%mm3                      \n\t"
513         "punpcklwd %%mm7, %%mm4                 \n\t"
514         "punpckhwd %%mm7, %%mm2                 \n\t"
515         "punpcklwd %%mm7, %%mm5                 \n\t"
516         "punpckhwd %%mm7, %%mm3                 \n\t"
517         "paddd (%1), %%mm4                      \n\t"
518         "paddd 8(%1), %%mm2                     \n\t"
519         "paddd 16(%1), %%mm5                    \n\t"
520         "paddd 24(%1), %%mm3                    \n\t"
521         "movq %%mm4, (%1)                       \n\t"
522         "movq %%mm2, 8(%1)                      \n\t"
523         "movq %%mm5, 16(%1)                     \n\t"
524         "movq %%mm3, 24(%1)                     \n\t"
525         "add $16, %0                            \n\t"
526         "add $32, %1                            \n\t"
527         "add $16, %2                            \n\t"
528         "cmp %3, %0                             \n\t"
529             " jb 1b                             \n\t"
530         : "+r" (block), "+r" (sum), "+r" (offset)
531         : "r"(block+64)
532     );
533 }
534
535 static void  denoise_dct_sse2(MpegEncContext *s, DCTELEM *block){
536     const int intra= s->mb_intra;
537     int *sum= s->dct_error_sum[intra];
538     uint16_t *offset= s->dct_offset[intra];
539
540     s->dct_count[intra]++;
541
542     __asm__ volatile(
543         "pxor %%xmm7, %%xmm7                    \n\t"
544         "1:                                     \n\t"
545         "pxor %%xmm0, %%xmm0                    \n\t"
546         "pxor %%xmm1, %%xmm1                    \n\t"
547         "movdqa (%0), %%xmm2                    \n\t"
548         "movdqa 16(%0), %%xmm3                  \n\t"
549         "pcmpgtw %%xmm2, %%xmm0                 \n\t"
550         "pcmpgtw %%xmm3, %%xmm1                 \n\t"
551         "pxor %%xmm0, %%xmm2                    \n\t"
552         "pxor %%xmm1, %%xmm3                    \n\t"
553         "psubw %%xmm0, %%xmm2                   \n\t"
554         "psubw %%xmm1, %%xmm3                   \n\t"
555         "movdqa %%xmm2, %%xmm4                  \n\t"
556         "movdqa %%xmm3, %%xmm5                  \n\t"
557         "psubusw (%2), %%xmm2                   \n\t"
558         "psubusw 16(%2), %%xmm3                 \n\t"
559         "pxor %%xmm0, %%xmm2                    \n\t"
560         "pxor %%xmm1, %%xmm3                    \n\t"
561         "psubw %%xmm0, %%xmm2                   \n\t"
562         "psubw %%xmm1, %%xmm3                   \n\t"
563         "movdqa %%xmm2, (%0)                    \n\t"
564         "movdqa %%xmm3, 16(%0)                  \n\t"
565         "movdqa %%xmm4, %%xmm6                  \n\t"
566         "movdqa %%xmm5, %%xmm0                  \n\t"
567         "punpcklwd %%xmm7, %%xmm4               \n\t"
568         "punpckhwd %%xmm7, %%xmm6               \n\t"
569         "punpcklwd %%xmm7, %%xmm5               \n\t"
570         "punpckhwd %%xmm7, %%xmm0               \n\t"
571         "paddd (%1), %%xmm4                     \n\t"
572         "paddd 16(%1), %%xmm6                   \n\t"
573         "paddd 32(%1), %%xmm5                   \n\t"
574         "paddd 48(%1), %%xmm0                   \n\t"
575         "movdqa %%xmm4, (%1)                    \n\t"
576         "movdqa %%xmm6, 16(%1)                  \n\t"
577         "movdqa %%xmm5, 32(%1)                  \n\t"
578         "movdqa %%xmm0, 48(%1)                  \n\t"
579         "add $32, %0                            \n\t"
580         "add $64, %1                            \n\t"
581         "add $32, %2                            \n\t"
582         "cmp %3, %0                             \n\t"
583             " jb 1b                             \n\t"
584         : "+r" (block), "+r" (sum), "+r" (offset)
585         : "r"(block+64)
586           XMM_CLOBBERS_ONLY("%xmm0", "%xmm1", "%xmm2", "%xmm3",
587                             "%xmm4", "%xmm5", "%xmm6", "%xmm7")
588     );
589 }
590
591 #if HAVE_SSSE3
592 #define HAVE_SSSE3_BAK
593 #endif
594 #undef HAVE_SSSE3
595 #define HAVE_SSSE3 0
596
597 #undef HAVE_SSE2
598 #undef HAVE_MMXEXT
599 #define HAVE_SSE2 0
600 #define HAVE_MMXEXT 0
601 #define RENAME(a) a ## _MMX
602 #define RENAMEl(a) a ## _mmx
603 #include "mpegvideo_mmx_template.c"
604
605 #undef HAVE_MMXEXT
606 #define HAVE_MMXEXT 1
607 #undef RENAME
608 #undef RENAMEl
609 #define RENAME(a) a ## _MMX2
610 #define RENAMEl(a) a ## _mmx2
611 #include "mpegvideo_mmx_template.c"
612
613 #undef HAVE_SSE2
614 #define HAVE_SSE2 1
615 #undef RENAME
616 #undef RENAMEl
617 #define RENAME(a) a ## _SSE2
618 #define RENAMEl(a) a ## _sse2
619 #include "mpegvideo_mmx_template.c"
620
621 #ifdef HAVE_SSSE3_BAK
622 #undef HAVE_SSSE3
623 #define HAVE_SSSE3 1
624 #undef RENAME
625 #undef RENAMEl
626 #define RENAME(a) a ## _SSSE3
627 #define RENAMEl(a) a ## _sse2
628 #include "mpegvideo_mmx_template.c"
629 #endif
630
631 #endif /* HAVE_INLINE_ASM */
632
633 void ff_MPV_common_init_mmx(MpegEncContext *s)
634 {
635 #if HAVE_INLINE_ASM
636     int mm_flags = av_get_cpu_flags();
637
638     if (mm_flags & AV_CPU_FLAG_MMX) {
639         const int dct_algo = s->avctx->dct_algo;
640
641         s->dct_unquantize_h263_intra = dct_unquantize_h263_intra_mmx;
642         s->dct_unquantize_h263_inter = dct_unquantize_h263_inter_mmx;
643         s->dct_unquantize_mpeg1_intra = dct_unquantize_mpeg1_intra_mmx;
644         s->dct_unquantize_mpeg1_inter = dct_unquantize_mpeg1_inter_mmx;
645         if(!(s->flags & CODEC_FLAG_BITEXACT))
646             s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_mmx;
647         s->dct_unquantize_mpeg2_inter = dct_unquantize_mpeg2_inter_mmx;
648
649         if (mm_flags & AV_CPU_FLAG_SSE2) {
650             s->denoise_dct= denoise_dct_sse2;
651         } else {
652                 s->denoise_dct= denoise_dct_mmx;
653         }
654
655         if(dct_algo==FF_DCT_AUTO || dct_algo==FF_DCT_MMX){
656 #if HAVE_SSSE3
657             if(mm_flags & AV_CPU_FLAG_SSSE3){
658                 s->dct_quantize= dct_quantize_SSSE3;
659             } else
660 #endif
661             if(mm_flags & AV_CPU_FLAG_SSE2){
662                 s->dct_quantize= dct_quantize_SSE2;
663             } else if (mm_flags & AV_CPU_FLAG_MMXEXT) {
664                 s->dct_quantize= dct_quantize_MMX2;
665             } else {
666                 s->dct_quantize= dct_quantize_MMX;
667             }
668         }
669     }
670 #endif /* HAVE_INLINE_ASM */
671 }