Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / synfig_0_61_03 / synfig-core / src / modules / mod_libavcodec / 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
27 extern uint8_t zigzag_direct_noperm[64];
28 extern uint16_t inv_zigzag_direct16[64];
29
30 static const unsigned long long int mm_wabs __attribute__ ((aligned(8))) = 0xffffffffffffffffULL;
31 static const unsigned long long int mm_wone __attribute__ ((aligned(8))) = 0x0001000100010001ULL;
32
33
34 static void dct_unquantize_h263_mmx(MpegEncContext *s,
35                                   DCTELEM *block, int n, int qscale)
36 {
37     int level, qmul, qadd, nCoeffs;
38
39     qmul = qscale << 1;
40     qadd = (qscale - 1) | 1;
41
42     assert(s->block_last_index[n]>=0);
43         
44     if (s->mb_intra) {
45         if (!s->h263_aic) {
46             if (n < 4)
47                 level = block[0] * s->y_dc_scale;
48             else
49                 level = block[0] * s->c_dc_scale;
50         }else{
51             qadd = 0;
52             level= block[0];
53         }
54         nCoeffs=63;
55     } else {
56         nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
57         level = 0;/* keep gcc quiet */
58     }
59 //printf("%d %d  ", qmul, qadd);
60 asm volatile(
61                 "movd %1, %%mm6                 \n\t" //qmul
62                 "packssdw %%mm6, %%mm6          \n\t"
63                 "packssdw %%mm6, %%mm6          \n\t"
64                 "movd %2, %%mm5                 \n\t" //qadd
65                 "pxor %%mm7, %%mm7              \n\t"
66                 "packssdw %%mm5, %%mm5          \n\t"
67                 "packssdw %%mm5, %%mm5          \n\t"
68                 "psubw %%mm5, %%mm7             \n\t"
69                 "pxor %%mm4, %%mm4              \n\t"
70                 ".balign 16\n\t"
71                 "1:                             \n\t"
72                 "movq (%0, %3), %%mm0           \n\t"
73                 "movq 8(%0, %3), %%mm1          \n\t"
74
75                 "pmullw %%mm6, %%mm0            \n\t"
76                 "pmullw %%mm6, %%mm1            \n\t"
77
78                 "movq (%0, %3), %%mm2           \n\t"
79                 "movq 8(%0, %3), %%mm3          \n\t"
80
81                 "pcmpgtw %%mm4, %%mm2           \n\t" // block[i] < 0 ? -1 : 0
82                 "pcmpgtw %%mm4, %%mm3           \n\t" // block[i] < 0 ? -1 : 0
83
84                 "pxor %%mm2, %%mm0              \n\t"
85                 "pxor %%mm3, %%mm1              \n\t"
86
87                 "paddw %%mm7, %%mm0             \n\t"
88                 "paddw %%mm7, %%mm1             \n\t"
89
90                 "pxor %%mm0, %%mm2              \n\t"
91                 "pxor %%mm1, %%mm3              \n\t"
92
93                 "pcmpeqw %%mm7, %%mm0           \n\t" // block[i] == 0 ? -1 : 0
94                 "pcmpeqw %%mm7, %%mm1           \n\t" // block[i] == 0 ? -1 : 0
95
96                 "pandn %%mm2, %%mm0             \n\t"
97                 "pandn %%mm3, %%mm1             \n\t"
98
99                 "movq %%mm0, (%0, %3)           \n\t"
100                 "movq %%mm1, 8(%0, %3)          \n\t"
101
102                 "addl $16, %3                   \n\t"
103                 "jng 1b                         \n\t"
104                 ::"r" (block+nCoeffs), "g"(qmul), "g" (qadd), "r" (2*(-nCoeffs))
105                 : "memory"
106         );
107         if(s->mb_intra)
108             block[0]= level;
109 }
110
111
112 /*
113   NK:
114   Note: looking at PARANOID:
115   "enable all paranoid tests for rounding, overflows, etc..."
116
117 #ifdef PARANOID
118                 if (level < -2048 || level > 2047)
119                     fprintf(stderr, "unquant error %d %d\n", i, level);
120 #endif
121   We can suppose that result of two multiplications can't be greate of 0xFFFF
122   i.e. is 16-bit, so we use here only PMULLW instruction and can avoid
123   a complex multiplication.
124 =====================================================
125  Full formula for multiplication of 2 integer numbers
126  which are represent as high:low words:
127  input: value1 = high1:low1
128         value2 = high2:low2
129  output: value3 = value1*value2
130  value3=high3:low3 (on overflow: modulus 2^32 wrap-around)
131  this mean that for 0x123456 * 0x123456 correct result is 0x766cb0ce4
132  but this algorithm will compute only 0x66cb0ce4
133  this limited by 16-bit size of operands
134  ---------------------------------
135  tlow1 = high1*low2
136  tlow2 = high2*low1
137  tlow1 = tlow1 + tlow2
138  high3:low3 = low1*low2
139  high3 += tlow1
140 */
141 static void dct_unquantize_mpeg1_mmx(MpegEncContext *s,
142                                      DCTELEM *block, int n, int qscale)
143 {
144     int nCoeffs;
145     const uint16_t *quant_matrix;
146
147     assert(s->block_last_index[n]>=0);
148
149     nCoeffs= s->intra_scantable.raster_end[ s->block_last_index[n] ]+1;
150
151     if (s->mb_intra) {
152         int block0;
153         if (n < 4) 
154             block0 = block[0] * s->y_dc_scale;
155         else
156             block0 = block[0] * s->c_dc_scale;
157         /* XXX: only mpeg1 */
158         quant_matrix = s->intra_matrix;
159 asm volatile(
160                 "pcmpeqw %%mm7, %%mm7           \n\t"
161                 "psrlw $15, %%mm7               \n\t"
162                 "movd %2, %%mm6                 \n\t"
163                 "packssdw %%mm6, %%mm6          \n\t"
164                 "packssdw %%mm6, %%mm6          \n\t"
165                 "movl %3, %%eax                 \n\t"
166                 ".balign 16\n\t"
167                 "1:                             \n\t"
168                 "movq (%0, %%eax), %%mm0        \n\t"
169                 "movq 8(%0, %%eax), %%mm1       \n\t"
170                 "movq (%1, %%eax), %%mm4        \n\t"
171                 "movq 8(%1, %%eax), %%mm5       \n\t"
172                 "pmullw %%mm6, %%mm4            \n\t" // q=qscale*quant_matrix[i]
173                 "pmullw %%mm6, %%mm5            \n\t" // q=qscale*quant_matrix[i]
174                 "pxor %%mm2, %%mm2              \n\t"
175                 "pxor %%mm3, %%mm3              \n\t"
176                 "pcmpgtw %%mm0, %%mm2           \n\t" // block[i] < 0 ? -1 : 0
177                 "pcmpgtw %%mm1, %%mm3           \n\t" // block[i] < 0 ? -1 : 0
178                 "pxor %%mm2, %%mm0              \n\t"
179                 "pxor %%mm3, %%mm1              \n\t"
180                 "psubw %%mm2, %%mm0             \n\t" // abs(block[i])
181                 "psubw %%mm3, %%mm1             \n\t" // abs(block[i])
182                 "pmullw %%mm4, %%mm0            \n\t" // abs(block[i])*q
183                 "pmullw %%mm5, %%mm1            \n\t" // abs(block[i])*q
184                 "pxor %%mm4, %%mm4              \n\t"
185                 "pxor %%mm5, %%mm5              \n\t" // FIXME slow
186                 "pcmpeqw (%0, %%eax), %%mm4     \n\t" // block[i] == 0 ? -1 : 0
187                 "pcmpeqw 8(%0, %%eax), %%mm5    \n\t" // block[i] == 0 ? -1 : 0
188                 "psraw $3, %%mm0                \n\t"
189                 "psraw $3, %%mm1                \n\t"
190                 "psubw %%mm7, %%mm0             \n\t"
191                 "psubw %%mm7, %%mm1             \n\t"
192                 "por %%mm7, %%mm0               \n\t"
193                 "por %%mm7, %%mm1               \n\t"
194                 "pxor %%mm2, %%mm0              \n\t"
195                 "pxor %%mm3, %%mm1              \n\t"
196                 "psubw %%mm2, %%mm0             \n\t"
197                 "psubw %%mm3, %%mm1             \n\t"
198                 "pandn %%mm0, %%mm4             \n\t"
199                 "pandn %%mm1, %%mm5             \n\t"
200                 "movq %%mm4, (%0, %%eax)        \n\t"
201                 "movq %%mm5, 8(%0, %%eax)       \n\t"
202
203                 "addl $16, %%eax                \n\t"
204                 "js 1b                          \n\t"
205                 ::"r" (block+nCoeffs), "r"(quant_matrix+nCoeffs), "g" (qscale), "g" (-2*nCoeffs)
206                 : "%eax", "memory"
207         );    
208         block[0]= block0;
209
210         } else {
211         quant_matrix = s->inter_matrix;
212 asm volatile(
213                 "pcmpeqw %%mm7, %%mm7           \n\t"
214                 "psrlw $15, %%mm7               \n\t"
215                 "movd %2, %%mm6                 \n\t"
216                 "packssdw %%mm6, %%mm6          \n\t"
217                 "packssdw %%mm6, %%mm6          \n\t"
218                 "movl %3, %%eax                 \n\t"
219                 ".balign 16\n\t"
220                 "1:                             \n\t"
221                 "movq (%0, %%eax), %%mm0        \n\t"
222                 "movq 8(%0, %%eax), %%mm1       \n\t"
223                 "movq (%1, %%eax), %%mm4        \n\t"
224                 "movq 8(%1, %%eax), %%mm5       \n\t"
225                 "pmullw %%mm6, %%mm4            \n\t" // q=qscale*quant_matrix[i]
226                 "pmullw %%mm6, %%mm5            \n\t" // q=qscale*quant_matrix[i]
227                 "pxor %%mm2, %%mm2              \n\t"
228                 "pxor %%mm3, %%mm3              \n\t"
229                 "pcmpgtw %%mm0, %%mm2           \n\t" // block[i] < 0 ? -1 : 0
230                 "pcmpgtw %%mm1, %%mm3           \n\t" // block[i] < 0 ? -1 : 0
231                 "pxor %%mm2, %%mm0              \n\t"
232                 "pxor %%mm3, %%mm1              \n\t"
233                 "psubw %%mm2, %%mm0             \n\t" // abs(block[i])
234                 "psubw %%mm3, %%mm1             \n\t" // abs(block[i])
235                 "paddw %%mm0, %%mm0             \n\t" // abs(block[i])*2
236                 "paddw %%mm1, %%mm1             \n\t" // abs(block[i])*2
237                 "paddw %%mm7, %%mm0             \n\t" // abs(block[i])*2 + 1
238                 "paddw %%mm7, %%mm1             \n\t" // abs(block[i])*2 + 1
239                 "pmullw %%mm4, %%mm0            \n\t" // (abs(block[i])*2 + 1)*q
240                 "pmullw %%mm5, %%mm1            \n\t" // (abs(block[i])*2 + 1)*q
241                 "pxor %%mm4, %%mm4              \n\t"
242                 "pxor %%mm5, %%mm5              \n\t" // FIXME slow
243                 "pcmpeqw (%0, %%eax), %%mm4     \n\t" // block[i] == 0 ? -1 : 0
244                 "pcmpeqw 8(%0, %%eax), %%mm5    \n\t" // block[i] == 0 ? -1 : 0
245                 "psraw $4, %%mm0                \n\t"
246                 "psraw $4, %%mm1                \n\t"
247                 "psubw %%mm7, %%mm0             \n\t"
248                 "psubw %%mm7, %%mm1             \n\t"
249                 "por %%mm7, %%mm0               \n\t"
250                 "por %%mm7, %%mm1               \n\t"
251                 "pxor %%mm2, %%mm0              \n\t"
252                 "pxor %%mm3, %%mm1              \n\t"
253                 "psubw %%mm2, %%mm0             \n\t"
254                 "psubw %%mm3, %%mm1             \n\t"
255                 "pandn %%mm0, %%mm4             \n\t"
256                 "pandn %%mm1, %%mm5             \n\t"
257                 "movq %%mm4, (%0, %%eax)        \n\t"
258                 "movq %%mm5, 8(%0, %%eax)       \n\t"
259
260                 "addl $16, %%eax                \n\t"
261                 "js 1b                          \n\t"
262                 ::"r" (block+nCoeffs), "r"(quant_matrix+nCoeffs), "g" (qscale), "g" (-2*nCoeffs)
263                 : "%eax", "memory"
264         );
265     }
266
267 }
268
269 static void dct_unquantize_mpeg2_mmx(MpegEncContext *s,
270                                      DCTELEM *block, int n, int qscale)
271 {
272     int nCoeffs;
273     const uint16_t *quant_matrix;
274     
275     assert(s->block_last_index[n]>=0);
276
277     if(s->alternate_scan) nCoeffs= 63; //FIXME
278     else nCoeffs= s->intra_scantable.raster_end[ s->block_last_index[n] ];
279
280     if (s->mb_intra) {
281         int block0;
282         if (n < 4) 
283             block0 = block[0] * s->y_dc_scale;
284         else
285             block0 = block[0] * s->c_dc_scale;
286         quant_matrix = s->intra_matrix;
287 asm volatile(
288                 "pcmpeqw %%mm7, %%mm7           \n\t"
289                 "psrlw $15, %%mm7               \n\t"
290                 "movd %2, %%mm6                 \n\t"
291                 "packssdw %%mm6, %%mm6          \n\t"
292                 "packssdw %%mm6, %%mm6          \n\t"
293                 "movl %3, %%eax                 \n\t"
294                 ".balign 16\n\t"
295                 "1:                             \n\t"
296                 "movq (%0, %%eax), %%mm0        \n\t"
297                 "movq 8(%0, %%eax), %%mm1       \n\t"
298                 "movq (%1, %%eax), %%mm4        \n\t"
299                 "movq 8(%1, %%eax), %%mm5       \n\t"
300                 "pmullw %%mm6, %%mm4            \n\t" // q=qscale*quant_matrix[i]
301                 "pmullw %%mm6, %%mm5            \n\t" // q=qscale*quant_matrix[i]
302                 "pxor %%mm2, %%mm2              \n\t"
303                 "pxor %%mm3, %%mm3              \n\t"
304                 "pcmpgtw %%mm0, %%mm2           \n\t" // block[i] < 0 ? -1 : 0
305                 "pcmpgtw %%mm1, %%mm3           \n\t" // block[i] < 0 ? -1 : 0
306                 "pxor %%mm2, %%mm0              \n\t"
307                 "pxor %%mm3, %%mm1              \n\t"
308                 "psubw %%mm2, %%mm0             \n\t" // abs(block[i])
309                 "psubw %%mm3, %%mm1             \n\t" // abs(block[i])
310                 "pmullw %%mm4, %%mm0            \n\t" // abs(block[i])*q
311                 "pmullw %%mm5, %%mm1            \n\t" // abs(block[i])*q
312                 "pxor %%mm4, %%mm4              \n\t"
313                 "pxor %%mm5, %%mm5              \n\t" // FIXME slow
314                 "pcmpeqw (%0, %%eax), %%mm4     \n\t" // block[i] == 0 ? -1 : 0
315                 "pcmpeqw 8(%0, %%eax), %%mm5    \n\t" // block[i] == 0 ? -1 : 0
316                 "psraw $3, %%mm0                \n\t"
317                 "psraw $3, %%mm1                \n\t"
318                 "pxor %%mm2, %%mm0              \n\t"
319                 "pxor %%mm3, %%mm1              \n\t"
320                 "psubw %%mm2, %%mm0             \n\t"
321                 "psubw %%mm3, %%mm1             \n\t"
322                 "pandn %%mm0, %%mm4             \n\t"
323                 "pandn %%mm1, %%mm5             \n\t"
324                 "movq %%mm4, (%0, %%eax)        \n\t"
325                 "movq %%mm5, 8(%0, %%eax)       \n\t"
326
327                 "addl $16, %%eax                \n\t"
328                 "jng 1b                         \n\t"
329                 ::"r" (block+nCoeffs), "r"(quant_matrix+nCoeffs), "g" (qscale), "g" (-2*nCoeffs)
330                 : "%eax", "memory"
331         );    
332         block[0]= block0;
333         //Note, we dont do mismatch control for intra as errors cannot accumulate
334
335     } else {
336         quant_matrix = s->inter_matrix;
337 asm volatile(
338                 "pcmpeqw %%mm7, %%mm7           \n\t"
339                 "psrlq $48, %%mm7               \n\t"
340                 "movd %2, %%mm6                 \n\t"
341                 "packssdw %%mm6, %%mm6          \n\t"
342                 "packssdw %%mm6, %%mm6          \n\t"
343                 "movl %3, %%eax                 \n\t"
344                 ".balign 16\n\t"
345                 "1:                             \n\t"
346                 "movq (%0, %%eax), %%mm0        \n\t"
347                 "movq 8(%0, %%eax), %%mm1       \n\t"
348                 "movq (%1, %%eax), %%mm4        \n\t"
349                 "movq 8(%1, %%eax), %%mm5       \n\t"
350                 "pmullw %%mm6, %%mm4            \n\t" // q=qscale*quant_matrix[i]
351                 "pmullw %%mm6, %%mm5            \n\t" // q=qscale*quant_matrix[i]
352                 "pxor %%mm2, %%mm2              \n\t"
353                 "pxor %%mm3, %%mm3              \n\t"
354                 "pcmpgtw %%mm0, %%mm2           \n\t" // block[i] < 0 ? -1 : 0
355                 "pcmpgtw %%mm1, %%mm3           \n\t" // block[i] < 0 ? -1 : 0
356                 "pxor %%mm2, %%mm0              \n\t"
357                 "pxor %%mm3, %%mm1              \n\t"
358                 "psubw %%mm2, %%mm0             \n\t" // abs(block[i])
359                 "psubw %%mm3, %%mm1             \n\t" // abs(block[i])
360                 "paddw %%mm0, %%mm0             \n\t" // abs(block[i])*2
361                 "paddw %%mm1, %%mm1             \n\t" // abs(block[i])*2
362                 "pmullw %%mm4, %%mm0            \n\t" // abs(block[i])*2*q
363                 "pmullw %%mm5, %%mm1            \n\t" // abs(block[i])*2*q
364                 "paddw %%mm4, %%mm0             \n\t" // (abs(block[i])*2 + 1)*q
365                 "paddw %%mm5, %%mm1             \n\t" // (abs(block[i])*2 + 1)*q
366                 "pxor %%mm4, %%mm4              \n\t"
367                 "pxor %%mm5, %%mm5              \n\t" // FIXME slow
368                 "pcmpeqw (%0, %%eax), %%mm4     \n\t" // block[i] == 0 ? -1 : 0
369                 "pcmpeqw 8(%0, %%eax), %%mm5    \n\t" // block[i] == 0 ? -1 : 0
370                 "psrlw $4, %%mm0                \n\t"
371                 "psrlw $4, %%mm1                \n\t"
372                 "pxor %%mm2, %%mm0              \n\t"
373                 "pxor %%mm3, %%mm1              \n\t"
374                 "psubw %%mm2, %%mm0             \n\t"
375                 "psubw %%mm3, %%mm1             \n\t"
376                 "pandn %%mm0, %%mm4             \n\t"
377                 "pandn %%mm1, %%mm5             \n\t"
378                 "pxor %%mm4, %%mm7              \n\t"
379                 "pxor %%mm5, %%mm7              \n\t"
380                 "movq %%mm4, (%0, %%eax)        \n\t"
381                 "movq %%mm5, 8(%0, %%eax)       \n\t"
382
383                 "addl $16, %%eax                \n\t"
384                 "jng 1b                         \n\t"
385                 "movd 124(%0, %3), %%mm0        \n\t"
386                 "movq %%mm7, %%mm6              \n\t"
387                 "psrlq $32, %%mm7               \n\t"
388                 "pxor %%mm6, %%mm7              \n\t"
389                 "movq %%mm7, %%mm6              \n\t"
390                 "psrlq $16, %%mm7               \n\t"
391                 "pxor %%mm6, %%mm7              \n\t"
392                 "pslld $31, %%mm7               \n\t"
393                 "psrlq $15, %%mm7               \n\t"
394                 "pxor %%mm7, %%mm0              \n\t"
395                 "movd %%mm0, 124(%0, %3)        \n\t"
396                 
397                 ::"r" (block+nCoeffs), "r"(quant_matrix+nCoeffs), "g" (qscale), "r" (-2*nCoeffs)
398                 : "%eax", "memory"
399         );
400     }
401 }
402
403 /* draw the edges of width 'w' of an image of size width, height 
404    this mmx version can only handle w==8 || w==16 */
405 static void draw_edges_mmx(uint8_t *buf, int wrap, int width, int height, int w)
406 {
407     uint8_t *ptr, *last_line;
408     int i;
409
410     last_line = buf + (height - 1) * wrap;
411     /* left and right */
412     ptr = buf;
413     if(w==8)
414     {
415         asm volatile(
416                 "1:                             \n\t"
417                 "movd (%0), %%mm0               \n\t"
418                 "punpcklbw %%mm0, %%mm0         \n\t" 
419                 "punpcklwd %%mm0, %%mm0         \n\t"
420                 "punpckldq %%mm0, %%mm0         \n\t"
421                 "movq %%mm0, -8(%0)             \n\t"
422                 "movq -8(%0, %2), %%mm1         \n\t"
423                 "punpckhbw %%mm1, %%mm1         \n\t"
424                 "punpckhwd %%mm1, %%mm1         \n\t"
425                 "punpckhdq %%mm1, %%mm1         \n\t"
426                 "movq %%mm1, (%0, %2)           \n\t"
427                 "addl %1, %0                    \n\t"
428                 "cmpl %3, %0                    \n\t"
429                 " jb 1b                         \n\t"
430                 : "+r" (ptr)
431                 : "r" (wrap), "r" (width), "r" (ptr + wrap*height)
432         );
433     }
434     else
435     {
436         asm volatile(
437                 "1:                             \n\t"
438                 "movd (%0), %%mm0               \n\t"
439                 "punpcklbw %%mm0, %%mm0         \n\t" 
440                 "punpcklwd %%mm0, %%mm0         \n\t"
441                 "punpckldq %%mm0, %%mm0         \n\t"
442                 "movq %%mm0, -8(%0)             \n\t"
443                 "movq %%mm0, -16(%0)            \n\t"
444                 "movq -8(%0, %2), %%mm1         \n\t"
445                 "punpckhbw %%mm1, %%mm1         \n\t"
446                 "punpckhwd %%mm1, %%mm1         \n\t"
447                 "punpckhdq %%mm1, %%mm1         \n\t"
448                 "movq %%mm1, (%0, %2)           \n\t"
449                 "movq %%mm1, 8(%0, %2)          \n\t"
450                 "addl %1, %0                    \n\t"
451                 "cmpl %3, %0                    \n\t"
452                 " jb 1b                         \n\t"           
453                 : "+r" (ptr)
454                 : "r" (wrap), "r" (width), "r" (ptr + wrap*height)
455         );
456     }
457     
458     for(i=0;i<w;i+=4) {
459         /* top and bottom (and hopefully also the corners) */
460         ptr= buf - (i + 1) * wrap - w;
461         asm volatile(
462                 "1:                             \n\t"
463                 "movq (%1, %0), %%mm0           \n\t"
464                 "movq %%mm0, (%0)               \n\t"
465                 "movq %%mm0, (%0, %2)           \n\t"
466                 "movq %%mm0, (%0, %2, 2)        \n\t"
467                 "movq %%mm0, (%0, %3)           \n\t"
468                 "addl $8, %0                    \n\t"
469                 "cmpl %4, %0                    \n\t"
470                 " jb 1b                         \n\t"
471                 : "+r" (ptr)
472                 : "r" ((int)buf - (int)ptr - w), "r" (-wrap), "r" (-wrap*3), "r" (ptr+width+2*w)
473         );
474         ptr= last_line + (i + 1) * wrap - w;
475         asm volatile(
476                 "1:                             \n\t"
477                 "movq (%1, %0), %%mm0           \n\t"
478                 "movq %%mm0, (%0)               \n\t"
479                 "movq %%mm0, (%0, %2)           \n\t"
480                 "movq %%mm0, (%0, %2, 2)        \n\t"
481                 "movq %%mm0, (%0, %3)           \n\t"
482                 "addl $8, %0                    \n\t"
483                 "cmpl %4, %0                    \n\t"
484                 " jb 1b                         \n\t"
485                 : "+r" (ptr)
486                 : "r" ((int)last_line - (int)ptr - w), "r" (wrap), "r" (wrap*3), "r" (ptr+width+2*w)
487         );
488     }
489 }
490
491 #undef HAVE_MMX2
492 #define RENAME(a) a ## _MMX
493 #include "mpegvideo_mmx_template.c"
494
495 #define HAVE_MMX2
496 #undef RENAME
497 #define RENAME(a) a ## _MMX2
498 #include "mpegvideo_mmx_template.c"
499
500 void MPV_common_init_mmx(MpegEncContext *s)
501 {
502     if (mm_flags & MM_MMX) {
503         const int dct_algo = s->avctx->dct_algo;
504         
505         s->dct_unquantize_h263 = dct_unquantize_h263_mmx;
506         s->dct_unquantize_mpeg1 = dct_unquantize_mpeg1_mmx;
507         s->dct_unquantize_mpeg2 = dct_unquantize_mpeg2_mmx;
508
509         draw_edges = draw_edges_mmx;
510
511         if(dct_algo==FF_DCT_AUTO || dct_algo==FF_DCT_MMX){
512             if(mm_flags & MM_MMXEXT){
513                 s->dct_quantize= dct_quantize_MMX2;
514             } else {
515                 s->dct_quantize= dct_quantize_MMX;
516             }
517         }
518     }
519 }