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