]> git.sesse.net Git - ffmpeg/blob - libavcodec/i386/mpegvideo_mmx.c
idct permutation cleanup, idct can be selected per context now
[ffmpeg] / libavcodec / i386 / mpegvideo_mmx.c
1 /*
2  * The simplest mpeg encoder (well, it was the simplest!)
3  * Copyright (c) 2000,2001 Fabrice Bellard.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  * Optimized for ia32 cpus by Nick Kurshev <nickols_k@mail.ru>
20  * h263, mpeg1, mpeg2 dequantizer & draw_edges by Michael Niedermayer <michaelni@gmx.at>
21  */
22
23 #include "../dsputil.h"
24 #include "../mpegvideo.h"
25 #include "../avcodec.h"
26 #include "../simple_idct.h"
27
28 /* Input permutation for the simple_idct_mmx */
29 static UINT8 simple_mmx_permutation[64]={
30         0x00, 0x08, 0x04, 0x09, 0x01, 0x0C, 0x05, 0x0D, 
31         0x10, 0x18, 0x14, 0x19, 0x11, 0x1C, 0x15, 0x1D, 
32         0x20, 0x28, 0x24, 0x29, 0x21, 0x2C, 0x25, 0x2D, 
33         0x12, 0x1A, 0x16, 0x1B, 0x13, 0x1E, 0x17, 0x1F, 
34         0x02, 0x0A, 0x06, 0x0B, 0x03, 0x0E, 0x07, 0x0F, 
35         0x30, 0x38, 0x34, 0x39, 0x31, 0x3C, 0x35, 0x3D, 
36         0x22, 0x2A, 0x26, 0x2B, 0x23, 0x2E, 0x27, 0x2F, 
37         0x32, 0x3A, 0x36, 0x3B, 0x33, 0x3E, 0x37, 0x3F,
38 };
39
40 extern UINT8 zigzag_direct_noperm[64];
41 extern UINT16 inv_zigzag_direct16[64];
42 extern UINT32 inverse[256];
43
44 static const unsigned long long int mm_wabs __attribute__ ((aligned(8))) = 0xffffffffffffffffULL;
45 static const unsigned long long int mm_wone __attribute__ ((aligned(8))) = 0x0001000100010001ULL;
46
47
48 static void dct_unquantize_h263_mmx(MpegEncContext *s,
49                                   DCTELEM *block, int n, int qscale)
50 {
51     int level, qmul, qadd, nCoeffs;
52
53     qmul = qscale << 1;
54     qadd = (qscale - 1) | 1;
55
56     assert(s->block_last_index[n]>=0);
57         
58     if (s->mb_intra) {
59         if (!s->h263_aic) {
60             if (n < 4)
61                 level = block[0] * s->y_dc_scale;
62             else
63                 level = block[0] * s->c_dc_scale;
64         }else{
65             qadd = 0;
66             level= block[0];
67         }
68         nCoeffs=63;
69     } else {
70         nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
71     }
72 //printf("%d %d  ", qmul, qadd);
73 asm volatile(
74                 "movd %1, %%mm6                 \n\t" //qmul
75                 "packssdw %%mm6, %%mm6          \n\t"
76                 "packssdw %%mm6, %%mm6          \n\t"
77                 "movd %2, %%mm5                 \n\t" //qadd
78                 "pxor %%mm7, %%mm7              \n\t"
79                 "packssdw %%mm5, %%mm5          \n\t"
80                 "packssdw %%mm5, %%mm5          \n\t"
81                 "psubw %%mm5, %%mm7             \n\t"
82                 "pxor %%mm4, %%mm4              \n\t"
83                 ".balign 16\n\t"
84                 "1:                             \n\t"
85                 "movq (%0, %3), %%mm0           \n\t"
86                 "movq 8(%0, %3), %%mm1          \n\t"
87
88                 "pmullw %%mm6, %%mm0            \n\t"
89                 "pmullw %%mm6, %%mm1            \n\t"
90
91                 "movq (%0, %3), %%mm2           \n\t"
92                 "movq 8(%0, %3), %%mm3          \n\t"
93
94                 "pcmpgtw %%mm4, %%mm2           \n\t" // block[i] < 0 ? -1 : 0
95                 "pcmpgtw %%mm4, %%mm3           \n\t" // block[i] < 0 ? -1 : 0
96
97                 "pxor %%mm2, %%mm0              \n\t"
98                 "pxor %%mm3, %%mm1              \n\t"
99
100                 "paddw %%mm7, %%mm0             \n\t"
101                 "paddw %%mm7, %%mm1             \n\t"
102
103                 "pxor %%mm0, %%mm2              \n\t"
104                 "pxor %%mm1, %%mm3              \n\t"
105
106                 "pcmpeqw %%mm7, %%mm0           \n\t" // block[i] == 0 ? -1 : 0
107                 "pcmpeqw %%mm7, %%mm1           \n\t" // block[i] == 0 ? -1 : 0
108
109                 "pandn %%mm2, %%mm0             \n\t"
110                 "pandn %%mm3, %%mm1             \n\t"
111
112                 "movq %%mm0, (%0, %3)           \n\t"
113                 "movq %%mm1, 8(%0, %3)          \n\t"
114
115                 "addl $16, %3                   \n\t"
116                 "jng 1b                         \n\t"
117                 ::"r" (block+nCoeffs), "g"(qmul), "g" (qadd), "r" (2*(-nCoeffs))
118                 : "memory"
119         );
120         if(s->mb_intra)
121             block[0]= level;
122 }
123
124
125 /*
126   NK:
127   Note: looking at PARANOID:
128   "enable all paranoid tests for rounding, overflows, etc..."
129
130 #ifdef PARANOID
131                 if (level < -2048 || level > 2047)
132                     fprintf(stderr, "unquant error %d %d\n", i, level);
133 #endif
134   We can suppose that result of two multiplications can't be greate of 0xFFFF
135   i.e. is 16-bit, so we use here only PMULLW instruction and can avoid
136   a complex multiplication.
137 =====================================================
138  Full formula for multiplication of 2 integer numbers
139  which are represent as high:low words:
140  input: value1 = high1:low1
141         value2 = high2:low2
142  output: value3 = value1*value2
143  value3=high3:low3 (on overflow: modulus 2^32 wrap-around)
144  this mean that for 0x123456 * 0x123456 correct result is 0x766cb0ce4
145  but this algorithm will compute only 0x66cb0ce4
146  this limited by 16-bit size of operands
147  ---------------------------------
148  tlow1 = high1*low2
149  tlow2 = high2*low1
150  tlow1 = tlow1 + tlow2
151  high3:low3 = low1*low2
152  high3 += tlow1
153 */
154 static void dct_unquantize_mpeg1_mmx(MpegEncContext *s,
155                                      DCTELEM *block, int n, int qscale)
156 {
157     int nCoeffs;
158     const UINT16 *quant_matrix;
159
160     assert(s->block_last_index[n]>=0);
161
162     nCoeffs= s->intra_scantable.raster_end[ s->block_last_index[n] ]+1;
163
164     if (s->mb_intra) {
165         int block0;
166         if (n < 4) 
167             block0 = block[0] * s->y_dc_scale;
168         else
169             block0 = block[0] * s->c_dc_scale;
170         /* XXX: only mpeg1 */
171         quant_matrix = s->intra_matrix;
172 asm volatile(
173                 "pcmpeqw %%mm7, %%mm7           \n\t"
174                 "psrlw $15, %%mm7               \n\t"
175                 "movd %2, %%mm6                 \n\t"
176                 "packssdw %%mm6, %%mm6          \n\t"
177                 "packssdw %%mm6, %%mm6          \n\t"
178                 "movl %3, %%eax                 \n\t"
179                 ".balign 16\n\t"
180                 "1:                             \n\t"
181                 "movq (%0, %%eax), %%mm0        \n\t"
182                 "movq 8(%0, %%eax), %%mm1       \n\t"
183                 "movq (%1, %%eax), %%mm4        \n\t"
184                 "movq 8(%1, %%eax), %%mm5       \n\t"
185                 "pmullw %%mm6, %%mm4            \n\t" // q=qscale*quant_matrix[i]
186                 "pmullw %%mm6, %%mm5            \n\t" // q=qscale*quant_matrix[i]
187                 "pxor %%mm2, %%mm2              \n\t"
188                 "pxor %%mm3, %%mm3              \n\t"
189                 "pcmpgtw %%mm0, %%mm2           \n\t" // block[i] < 0 ? -1 : 0
190                 "pcmpgtw %%mm1, %%mm3           \n\t" // block[i] < 0 ? -1 : 0
191                 "pxor %%mm2, %%mm0              \n\t"
192                 "pxor %%mm3, %%mm1              \n\t"
193                 "psubw %%mm2, %%mm0             \n\t" // abs(block[i])
194                 "psubw %%mm3, %%mm1             \n\t" // abs(block[i])
195                 "pmullw %%mm4, %%mm0            \n\t" // abs(block[i])*q
196                 "pmullw %%mm5, %%mm1            \n\t" // abs(block[i])*q
197                 "pxor %%mm4, %%mm4              \n\t"
198                 "pxor %%mm5, %%mm5              \n\t" // FIXME slow
199                 "pcmpeqw (%0, %%eax), %%mm4     \n\t" // block[i] == 0 ? -1 : 0
200                 "pcmpeqw 8(%0, %%eax), %%mm5    \n\t" // block[i] == 0 ? -1 : 0
201                 "psraw $3, %%mm0                \n\t"
202                 "psraw $3, %%mm1                \n\t"
203                 "psubw %%mm7, %%mm0             \n\t"
204                 "psubw %%mm7, %%mm1             \n\t"
205                 "por %%mm7, %%mm0               \n\t"
206                 "por %%mm7, %%mm1               \n\t"
207                 "pxor %%mm2, %%mm0              \n\t"
208                 "pxor %%mm3, %%mm1              \n\t"
209                 "psubw %%mm2, %%mm0             \n\t"
210                 "psubw %%mm3, %%mm1             \n\t"
211                 "pandn %%mm0, %%mm4             \n\t"
212                 "pandn %%mm1, %%mm5             \n\t"
213                 "movq %%mm4, (%0, %%eax)        \n\t"
214                 "movq %%mm5, 8(%0, %%eax)       \n\t"
215
216                 "addl $16, %%eax                \n\t"
217                 "js 1b                          \n\t"
218                 ::"r" (block+nCoeffs), "r"(quant_matrix+nCoeffs), "g" (qscale), "g" (-2*nCoeffs)
219                 : "%eax", "memory"
220         );    
221         block[0]= block0;
222
223         } else {
224         quant_matrix = s->inter_matrix;
225 asm volatile(
226                 "pcmpeqw %%mm7, %%mm7           \n\t"
227                 "psrlw $15, %%mm7               \n\t"
228                 "movd %2, %%mm6                 \n\t"
229                 "packssdw %%mm6, %%mm6          \n\t"
230                 "packssdw %%mm6, %%mm6          \n\t"
231                 "movl %3, %%eax                 \n\t"
232                 ".balign 16\n\t"
233                 "1:                             \n\t"
234                 "movq (%0, %%eax), %%mm0        \n\t"
235                 "movq 8(%0, %%eax), %%mm1       \n\t"
236                 "movq (%1, %%eax), %%mm4        \n\t"
237                 "movq 8(%1, %%eax), %%mm5       \n\t"
238                 "pmullw %%mm6, %%mm4            \n\t" // q=qscale*quant_matrix[i]
239                 "pmullw %%mm6, %%mm5            \n\t" // q=qscale*quant_matrix[i]
240                 "pxor %%mm2, %%mm2              \n\t"
241                 "pxor %%mm3, %%mm3              \n\t"
242                 "pcmpgtw %%mm0, %%mm2           \n\t" // block[i] < 0 ? -1 : 0
243                 "pcmpgtw %%mm1, %%mm3           \n\t" // block[i] < 0 ? -1 : 0
244                 "pxor %%mm2, %%mm0              \n\t"
245                 "pxor %%mm3, %%mm1              \n\t"
246                 "psubw %%mm2, %%mm0             \n\t" // abs(block[i])
247                 "psubw %%mm3, %%mm1             \n\t" // abs(block[i])
248                 "paddw %%mm0, %%mm0             \n\t" // abs(block[i])*2
249                 "paddw %%mm1, %%mm1             \n\t" // abs(block[i])*2
250                 "paddw %%mm7, %%mm0             \n\t" // abs(block[i])*2 + 1
251                 "paddw %%mm7, %%mm1             \n\t" // abs(block[i])*2 + 1
252                 "pmullw %%mm4, %%mm0            \n\t" // (abs(block[i])*2 + 1)*q
253                 "pmullw %%mm5, %%mm1            \n\t" // (abs(block[i])*2 + 1)*q
254                 "pxor %%mm4, %%mm4              \n\t"
255                 "pxor %%mm5, %%mm5              \n\t" // FIXME slow
256                 "pcmpeqw (%0, %%eax), %%mm4     \n\t" // block[i] == 0 ? -1 : 0
257                 "pcmpeqw 8(%0, %%eax), %%mm5    \n\t" // block[i] == 0 ? -1 : 0
258                 "psraw $4, %%mm0                \n\t"
259                 "psraw $4, %%mm1                \n\t"
260                 "psubw %%mm7, %%mm0             \n\t"
261                 "psubw %%mm7, %%mm1             \n\t"
262                 "por %%mm7, %%mm0               \n\t"
263                 "por %%mm7, %%mm1               \n\t"
264                 "pxor %%mm2, %%mm0              \n\t"
265                 "pxor %%mm3, %%mm1              \n\t"
266                 "psubw %%mm2, %%mm0             \n\t"
267                 "psubw %%mm3, %%mm1             \n\t"
268                 "pandn %%mm0, %%mm4             \n\t"
269                 "pandn %%mm1, %%mm5             \n\t"
270                 "movq %%mm4, (%0, %%eax)        \n\t"
271                 "movq %%mm5, 8(%0, %%eax)       \n\t"
272
273                 "addl $16, %%eax                \n\t"
274                 "js 1b                          \n\t"
275                 ::"r" (block+nCoeffs), "r"(quant_matrix+nCoeffs), "g" (qscale), "g" (-2*nCoeffs)
276                 : "%eax", "memory"
277         );
278     }
279
280 }
281
282 static void dct_unquantize_mpeg2_mmx(MpegEncContext *s,
283                                      DCTELEM *block, int n, int qscale)
284 {
285     int nCoeffs;
286     const UINT16 *quant_matrix;
287     
288     assert(s->block_last_index[n]>=0);
289
290     if(s->alternate_scan) nCoeffs= 63; //FIXME
291     else nCoeffs= s->intra_scantable.raster_end[ s->block_last_index[n] ];
292
293     if (s->mb_intra) {
294         int block0;
295         if (n < 4) 
296             block0 = block[0] * s->y_dc_scale;
297         else
298             block0 = block[0] * s->c_dc_scale;
299         quant_matrix = s->intra_matrix;
300 asm volatile(
301                 "pcmpeqw %%mm7, %%mm7           \n\t"
302                 "psrlw $15, %%mm7               \n\t"
303                 "movd %2, %%mm6                 \n\t"
304                 "packssdw %%mm6, %%mm6          \n\t"
305                 "packssdw %%mm6, %%mm6          \n\t"
306                 "movl %3, %%eax                 \n\t"
307                 ".balign 16\n\t"
308                 "1:                             \n\t"
309                 "movq (%0, %%eax), %%mm0        \n\t"
310                 "movq 8(%0, %%eax), %%mm1       \n\t"
311                 "movq (%1, %%eax), %%mm4        \n\t"
312                 "movq 8(%1, %%eax), %%mm5       \n\t"
313                 "pmullw %%mm6, %%mm4            \n\t" // q=qscale*quant_matrix[i]
314                 "pmullw %%mm6, %%mm5            \n\t" // q=qscale*quant_matrix[i]
315                 "pxor %%mm2, %%mm2              \n\t"
316                 "pxor %%mm3, %%mm3              \n\t"
317                 "pcmpgtw %%mm0, %%mm2           \n\t" // block[i] < 0 ? -1 : 0
318                 "pcmpgtw %%mm1, %%mm3           \n\t" // block[i] < 0 ? -1 : 0
319                 "pxor %%mm2, %%mm0              \n\t"
320                 "pxor %%mm3, %%mm1              \n\t"
321                 "psubw %%mm2, %%mm0             \n\t" // abs(block[i])
322                 "psubw %%mm3, %%mm1             \n\t" // abs(block[i])
323                 "pmullw %%mm4, %%mm0            \n\t" // abs(block[i])*q
324                 "pmullw %%mm5, %%mm1            \n\t" // abs(block[i])*q
325                 "pxor %%mm4, %%mm4              \n\t"
326                 "pxor %%mm5, %%mm5              \n\t" // FIXME slow
327                 "pcmpeqw (%0, %%eax), %%mm4     \n\t" // block[i] == 0 ? -1 : 0
328                 "pcmpeqw 8(%0, %%eax), %%mm5    \n\t" // block[i] == 0 ? -1 : 0
329                 "psraw $3, %%mm0                \n\t"
330                 "psraw $3, %%mm1                \n\t"
331                 "pxor %%mm2, %%mm0              \n\t"
332                 "pxor %%mm3, %%mm1              \n\t"
333                 "psubw %%mm2, %%mm0             \n\t"
334                 "psubw %%mm3, %%mm1             \n\t"
335                 "pandn %%mm0, %%mm4             \n\t"
336                 "pandn %%mm1, %%mm5             \n\t"
337                 "movq %%mm4, (%0, %%eax)        \n\t"
338                 "movq %%mm5, 8(%0, %%eax)       \n\t"
339
340                 "addl $16, %%eax                \n\t"
341                 "jng 1b                         \n\t"
342                 ::"r" (block+nCoeffs), "r"(quant_matrix+nCoeffs), "g" (qscale), "g" (-2*nCoeffs)
343                 : "%eax", "memory"
344         );    
345         block[0]= block0;
346         //Note, we dont do mismatch control for intra as errors cannot accumulate
347
348     } else {
349         quant_matrix = s->inter_matrix;
350 asm volatile(
351                 "pcmpeqw %%mm7, %%mm7           \n\t"
352                 "psrlq $48, %%mm7               \n\t"
353                 "movd %2, %%mm6                 \n\t"
354                 "packssdw %%mm6, %%mm6          \n\t"
355                 "packssdw %%mm6, %%mm6          \n\t"
356                 "movl %3, %%eax                 \n\t"
357                 ".balign 16\n\t"
358                 "1:                             \n\t"
359                 "movq (%0, %%eax), %%mm0        \n\t"
360                 "movq 8(%0, %%eax), %%mm1       \n\t"
361                 "movq (%1, %%eax), %%mm4        \n\t"
362                 "movq 8(%1, %%eax), %%mm5       \n\t"
363                 "pmullw %%mm6, %%mm4            \n\t" // q=qscale*quant_matrix[i]
364                 "pmullw %%mm6, %%mm5            \n\t" // q=qscale*quant_matrix[i]
365                 "pxor %%mm2, %%mm2              \n\t"
366                 "pxor %%mm3, %%mm3              \n\t"
367                 "pcmpgtw %%mm0, %%mm2           \n\t" // block[i] < 0 ? -1 : 0
368                 "pcmpgtw %%mm1, %%mm3           \n\t" // block[i] < 0 ? -1 : 0
369                 "pxor %%mm2, %%mm0              \n\t"
370                 "pxor %%mm3, %%mm1              \n\t"
371                 "psubw %%mm2, %%mm0             \n\t" // abs(block[i])
372                 "psubw %%mm3, %%mm1             \n\t" // abs(block[i])
373                 "paddw %%mm0, %%mm0             \n\t" // abs(block[i])*2
374                 "paddw %%mm1, %%mm1             \n\t" // abs(block[i])*2
375                 "pmullw %%mm4, %%mm0            \n\t" // abs(block[i])*2*q
376                 "pmullw %%mm5, %%mm1            \n\t" // abs(block[i])*2*q
377                 "paddw %%mm4, %%mm0             \n\t" // (abs(block[i])*2 + 1)*q
378                 "paddw %%mm5, %%mm1             \n\t" // (abs(block[i])*2 + 1)*q
379                 "pxor %%mm4, %%mm4              \n\t"
380                 "pxor %%mm5, %%mm5              \n\t" // FIXME slow
381                 "pcmpeqw (%0, %%eax), %%mm4     \n\t" // block[i] == 0 ? -1 : 0
382                 "pcmpeqw 8(%0, %%eax), %%mm5    \n\t" // block[i] == 0 ? -1 : 0
383                 "psrlw $4, %%mm0                \n\t"
384                 "psrlw $4, %%mm1                \n\t"
385                 "pxor %%mm2, %%mm0              \n\t"
386                 "pxor %%mm3, %%mm1              \n\t"
387                 "psubw %%mm2, %%mm0             \n\t"
388                 "psubw %%mm3, %%mm1             \n\t"
389                 "pandn %%mm0, %%mm4             \n\t"
390                 "pandn %%mm1, %%mm5             \n\t"
391                 "pxor %%mm4, %%mm7              \n\t"
392                 "pxor %%mm5, %%mm7              \n\t"
393                 "movq %%mm4, (%0, %%eax)        \n\t"
394                 "movq %%mm5, 8(%0, %%eax)       \n\t"
395
396                 "addl $16, %%eax                \n\t"
397                 "jng 1b                         \n\t"
398                 "movd 124(%0, %3), %%mm0        \n\t"
399                 "movq %%mm7, %%mm6              \n\t"
400                 "psrlq $32, %%mm7               \n\t"
401                 "pxor %%mm6, %%mm7              \n\t"
402                 "movq %%mm7, %%mm6              \n\t"
403                 "psrlq $16, %%mm7               \n\t"
404                 "pxor %%mm6, %%mm7              \n\t"
405                 "pslld $31, %%mm7               \n\t"
406                 "psrlq $15, %%mm7               \n\t"
407                 "pxor %%mm7, %%mm0              \n\t"
408                 "movd %%mm0, 124(%0, %3)        \n\t"
409                 
410                 ::"r" (block+nCoeffs), "r"(quant_matrix+nCoeffs), "g" (qscale), "r" (-2*nCoeffs)
411                 : "%eax", "memory"
412         );
413     }
414 }
415
416 /* draw the edges of width 'w' of an image of size width, height 
417    this mmx version can only handle w==8 || w==16 */
418 static void draw_edges_mmx(UINT8 *buf, int wrap, int width, int height, int w)
419 {
420     UINT8 *ptr, *last_line;
421     int i;
422
423     last_line = buf + (height - 1) * wrap;
424     /* left and right */
425     ptr = buf;
426     if(w==8)
427     {
428         asm volatile(
429                 "1:                             \n\t"
430                 "movd (%0), %%mm0               \n\t"
431                 "punpcklbw %%mm0, %%mm0         \n\t" 
432                 "punpcklwd %%mm0, %%mm0         \n\t"
433                 "punpckldq %%mm0, %%mm0         \n\t"
434                 "movq %%mm0, -8(%0)             \n\t"
435                 "movq -8(%0, %2), %%mm1         \n\t"
436                 "punpckhbw %%mm1, %%mm1         \n\t"
437                 "punpckhwd %%mm1, %%mm1         \n\t"
438                 "punpckhdq %%mm1, %%mm1         \n\t"
439                 "movq %%mm1, (%0, %2)           \n\t"
440                 "addl %1, %0                    \n\t"
441                 "cmpl %3, %0                    \n\t"
442                 " jb 1b                         \n\t"
443                 : "+r" (ptr)
444                 : "r" (wrap), "r" (width), "r" (ptr + wrap*height)
445         );
446     }
447     else
448     {
449         asm volatile(
450                 "1:                             \n\t"
451                 "movd (%0), %%mm0               \n\t"
452                 "punpcklbw %%mm0, %%mm0         \n\t" 
453                 "punpcklwd %%mm0, %%mm0         \n\t"
454                 "punpckldq %%mm0, %%mm0         \n\t"
455                 "movq %%mm0, -8(%0)             \n\t"
456                 "movq %%mm0, -16(%0)            \n\t"
457                 "movq -8(%0, %2), %%mm1         \n\t"
458                 "punpckhbw %%mm1, %%mm1         \n\t"
459                 "punpckhwd %%mm1, %%mm1         \n\t"
460                 "punpckhdq %%mm1, %%mm1         \n\t"
461                 "movq %%mm1, (%0, %2)           \n\t"
462                 "movq %%mm1, 8(%0, %2)          \n\t"
463                 "addl %1, %0                    \n\t"
464                 "cmpl %3, %0                    \n\t"
465                 " jb 1b                         \n\t"           
466                 : "+r" (ptr)
467                 : "r" (wrap), "r" (width), "r" (ptr + wrap*height)
468         );
469     }
470     
471     for(i=0;i<w;i+=4) {
472         /* top and bottom (and hopefully also the corners) */
473         ptr= buf - (i + 1) * wrap - w;
474         asm volatile(
475                 "1:                             \n\t"
476                 "movq (%1, %0), %%mm0           \n\t"
477                 "movq %%mm0, (%0)               \n\t"
478                 "movq %%mm0, (%0, %2)           \n\t"
479                 "movq %%mm0, (%0, %2, 2)        \n\t"
480                 "movq %%mm0, (%0, %3)           \n\t"
481                 "addl $8, %0                    \n\t"
482                 "cmpl %4, %0                    \n\t"
483                 " jb 1b                         \n\t"
484                 : "+r" (ptr)
485                 : "r" ((int)buf - (int)ptr - w), "r" (-wrap), "r" (-wrap*3), "r" (ptr+width+2*w)
486         );
487         ptr= last_line + (i + 1) * wrap - w;
488         asm volatile(
489                 "1:                             \n\t"
490                 "movq (%1, %0), %%mm0           \n\t"
491                 "movq %%mm0, (%0)               \n\t"
492                 "movq %%mm0, (%0, %2)           \n\t"
493                 "movq %%mm0, (%0, %2, 2)        \n\t"
494                 "movq %%mm0, (%0, %3)           \n\t"
495                 "addl $8, %0                    \n\t"
496                 "cmpl %4, %0                    \n\t"
497                 " jb 1b                         \n\t"
498                 : "+r" (ptr)
499                 : "r" ((int)last_line - (int)ptr - w), "r" (wrap), "r" (wrap*3), "r" (ptr+width+2*w)
500         );
501     }
502 }
503
504 #undef HAVE_MMX2
505 #define RENAME(a) a ## _MMX
506 #include "mpegvideo_mmx_template.c"
507
508 #define HAVE_MMX2
509 #undef RENAME
510 #define RENAME(a) a ## _MMX2
511 #include "mpegvideo_mmx_template.c"
512
513 /* external functions, from idct_mmx.c */
514 void ff_mmx_idct(DCTELEM *block);
515 void ff_mmxext_idct(DCTELEM *block);
516
517 /* XXX: those functions should be suppressed ASAP when all IDCTs are
518    converted */
519 static void ff_libmpeg2mmx_idct_put(UINT8 *dest, int line_size, DCTELEM *block)
520 {
521     ff_mmx_idct (block);
522     put_pixels_clamped(block, dest, line_size);
523 }
524 static void ff_libmpeg2mmx_idct_add(UINT8 *dest, int line_size, DCTELEM *block)
525 {
526     ff_mmx_idct (block);
527     add_pixels_clamped(block, dest, line_size);
528 }
529 static void ff_libmpeg2mmx2_idct_put(UINT8 *dest, int line_size, DCTELEM *block)
530 {
531     ff_mmxext_idct (block);
532     put_pixels_clamped(block, dest, line_size);
533 }
534 static void ff_libmpeg2mmx2_idct_add(UINT8 *dest, int line_size, DCTELEM *block)
535 {
536     ff_mmxext_idct (block);
537     add_pixels_clamped(block, dest, line_size);
538 }
539
540 void MPV_common_init_mmx(MpegEncContext *s)
541 {
542     if (mm_flags & MM_MMX) {
543         int i;
544         const int dct_algo = s->avctx->dct_algo;
545         const int idct_algo= s->avctx->idct_algo;
546         
547         s->dct_unquantize_h263 = dct_unquantize_h263_mmx;
548         s->dct_unquantize_mpeg1 = dct_unquantize_mpeg1_mmx;
549         s->dct_unquantize_mpeg2 = dct_unquantize_mpeg2_mmx;
550
551         draw_edges = draw_edges_mmx;
552
553         if(dct_algo==FF_DCT_AUTO || dct_algo==FF_DCT_MMX){
554             s->fdct = ff_fdct_mmx;
555
556             if(mm_flags & MM_MMXEXT){
557                 s->dct_quantize= dct_quantize_MMX2;
558             } else {
559                 s->dct_quantize= dct_quantize_MMX;
560             }
561         }
562         
563         if(idct_algo==FF_IDCT_AUTO || idct_algo==FF_IDCT_SIMPLEMMX){
564             s->idct_put= ff_simple_idct_put_mmx;
565             s->idct_add= ff_simple_idct_add_mmx;
566             for(i=0; i<64; i++)
567                 s->idct_permutation[i]= simple_mmx_permutation[i];
568         }else if(idct_algo==FF_IDCT_LIBMPEG2MMX){
569             if(mm_flags & MM_MMXEXT){
570                 s->idct_put= ff_libmpeg2mmx2_idct_put;
571                 s->idct_add= ff_libmpeg2mmx2_idct_add;
572             }else{
573                 s->idct_put= ff_libmpeg2mmx_idct_put;
574                 s->idct_add= ff_libmpeg2mmx_idct_add;
575             }
576             for(i=0; i<64; i++)
577                 s->idct_permutation[i]= (i & 0x38) | ((i & 6) >> 1) | ((i & 1) << 2);
578         }
579     }
580 }