]> git.sesse.net Git - ffmpeg/blob - libavcodec/i386/mpegvideo_mmx.c
- Advanced Intra Coding (AIC) support for H.263+ encoder, just DC by now.
[ffmpeg] / libavcodec / i386 / mpegvideo_mmx.c
1 /*
2  * The simplest mpeg encoder (well, it was the simplest!)
3  * Copyright (c) 2000,2001 Gerard Lantau.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program 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
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, 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 "../mangle.h"
27
28 extern UINT8 zigzag_end[64];
29
30 extern UINT8 zigzag_direct_noperm[64];
31 extern UINT16 inv_zigzag_direct16[64];
32 extern UINT32 inverse[256];
33
34 #if 0
35
36 /* XXX: GL: I don't understand why this function needs optimization
37    (it is called only once per frame!), so I disabled it */
38
39 void MPV_frame_start(MpegEncContext *s)
40 {
41     if (s->pict_type == B_TYPE) {
42         __asm __volatile(
43             "movl       (%1), %%eax\n\t"
44             "movl       4(%1), %%edx\n\t"
45             "movl       8(%1), %%ecx\n\t"
46             "movl       %%eax, (%0)\n\t"
47             "movl       %%edx, 4(%0)\n\t"
48             "movl       %%ecx, 8(%0)\n\t"
49             :
50             :"r"(s->current_picture), "r"(s->aux_picture)
51             :"eax","edx","ecx","memory");
52     } else {
53             /* swap next and last */
54         __asm __volatile(
55             "movl       (%1), %%eax\n\t"
56             "movl       4(%1), %%edx\n\t"
57             "movl       8(%1), %%ecx\n\t"
58             "xchgl      (%0), %%eax\n\t"
59             "xchgl      4(%0), %%edx\n\t"
60             "xchgl      8(%0), %%ecx\n\t"
61             "movl       %%eax, (%1)\n\t"
62             "movl       %%edx, 4(%1)\n\t"
63             "movl       %%ecx, 8(%1)\n\t"
64             "movl       %%eax, (%2)\n\t"
65             "movl       %%edx, 4(%2)\n\t"
66             "movl       %%ecx, 8(%2)\n\t"
67             :
68             :"r"(s->last_picture), "r"(s->next_picture), "r"(s->current_picture)
69             :"eax","edx","ecx","memory");
70     }
71 }
72 #endif
73
74 static const unsigned long long int mm_wabs __attribute__ ((aligned(8))) = 0xffffffffffffffffULL;
75 static const unsigned long long int mm_wone __attribute__ ((aligned(8))) = 0x0001000100010001ULL;
76
77
78 static void dct_unquantize_h263_mmx(MpegEncContext *s,
79                                   DCTELEM *block, int n, int qscale)
80 {
81     int i, level, qmul, qadd, nCoeffs;
82     
83     qmul = s->qscale << 1;
84     if (s->h263_aic && s->mb_intra)
85         qadd = 0;
86     else
87         qadd = (s->qscale - 1) | 1;
88
89     if (s->mb_intra) {
90         if (!s->h263_aic) {
91             if (n < 4)
92                 block[0] = block[0] * s->y_dc_scale;
93             else
94                 block[0] = block[0] * s->c_dc_scale;
95         }
96         for(i=1; i<8; i++) {
97             level = block[i];
98             if (level) {
99                 if (level < 0) {
100                     level = level * qmul - qadd;
101                 } else {
102                     level = level * qmul + qadd;
103                 }
104                 block[i] = level;
105             }
106         }
107         nCoeffs=64;
108     } else {
109         i = 0;
110         nCoeffs= zigzag_end[ s->block_last_index[n] ];
111     }
112 //printf("%d %d  ", qmul, qadd);
113 asm volatile(
114                 "movd %1, %%mm6                 \n\t" //qmul
115                 "packssdw %%mm6, %%mm6          \n\t"
116                 "packssdw %%mm6, %%mm6          \n\t"
117                 "movd %2, %%mm5                 \n\t" //qadd
118                 "pxor %%mm7, %%mm7              \n\t"
119                 "packssdw %%mm5, %%mm5          \n\t"
120                 "packssdw %%mm5, %%mm5          \n\t"
121                 "psubw %%mm5, %%mm7             \n\t"
122                 "pxor %%mm4, %%mm4              \n\t"
123                 ".balign 16\n\t"
124                 "1:                             \n\t"
125                 "movq (%0, %3), %%mm0           \n\t"
126                 "movq 8(%0, %3), %%mm1          \n\t"
127
128                 "pmullw %%mm6, %%mm0            \n\t"
129                 "pmullw %%mm6, %%mm1            \n\t"
130
131                 "movq (%0, %3), %%mm2           \n\t"
132                 "movq 8(%0, %3), %%mm3          \n\t"
133
134                 "pcmpgtw %%mm4, %%mm2           \n\t" // block[i] < 0 ? -1 : 0
135                 "pcmpgtw %%mm4, %%mm3           \n\t" // block[i] < 0 ? -1 : 0
136
137                 "pxor %%mm2, %%mm0              \n\t"
138                 "pxor %%mm3, %%mm1              \n\t"
139
140                 "paddw %%mm7, %%mm0             \n\t"
141                 "paddw %%mm7, %%mm1             \n\t"
142
143                 "pxor %%mm0, %%mm2              \n\t"
144                 "pxor %%mm1, %%mm3              \n\t"
145
146                 "pcmpeqw %%mm7, %%mm0           \n\t" // block[i] == 0 ? -1 : 0
147                 "pcmpeqw %%mm7, %%mm1           \n\t" // block[i] == 0 ? -1 : 0
148
149                 "pandn %%mm2, %%mm0             \n\t"
150                 "pandn %%mm3, %%mm1             \n\t"
151
152                 "movq %%mm0, (%0, %3)           \n\t"
153                 "movq %%mm1, 8(%0, %3)          \n\t"
154
155                 "addl $16, %3                   \n\t"
156                 "js 1b                          \n\t"
157                 ::"r" (block+nCoeffs), "g"(qmul), "g" (qadd), "r" (2*(i-nCoeffs))
158                 : "memory"
159         );
160 }
161
162
163 /*
164   NK:
165   Note: looking at PARANOID:
166   "enable all paranoid tests for rounding, overflows, etc..."
167
168 #ifdef PARANOID
169                 if (level < -2048 || level > 2047)
170                     fprintf(stderr, "unquant error %d %d\n", i, level);
171 #endif
172   We can suppose that result of two multiplications can't be greate of 0xFFFF
173   i.e. is 16-bit, so we use here only PMULLW instruction and can avoid
174   a complex multiplication.
175 =====================================================
176  Full formula for multiplication of 2 integer numbers
177  which are represent as high:low words:
178  input: value1 = high1:low1
179         value2 = high2:low2
180  output: value3 = value1*value2
181  value3=high3:low3 (on overflow: modulus 2^32 wrap-around)
182  this mean that for 0x123456 * 0x123456 correct result is 0x766cb0ce4
183  but this algorithm will compute only 0x66cb0ce4
184  this limited by 16-bit size of operands
185  ---------------------------------
186  tlow1 = high1*low2
187  tlow2 = high2*low1
188  tlow1 = tlow1 + tlow2
189  high3:low3 = low1*low2
190  high3 += tlow1
191 */
192 static void dct_unquantize_mpeg1_mmx(MpegEncContext *s,
193                                      DCTELEM *block, int n, int qscale)
194 {
195     int nCoeffs;
196     const UINT16 *quant_matrix;
197     
198     if(s->alternate_scan) nCoeffs= 64;
199     else nCoeffs= nCoeffs= zigzag_end[ s->block_last_index[n] ];
200
201     if (s->mb_intra) {
202         int block0;
203         if (n < 4) 
204             block0 = block[0] * s->y_dc_scale;
205         else
206             block0 = block[0] * s->c_dc_scale;
207         /* XXX: only mpeg1 */
208         quant_matrix = s->intra_matrix;
209 asm volatile(
210                 "pcmpeqw %%mm7, %%mm7           \n\t"
211                 "psrlw $15, %%mm7               \n\t"
212                 "movd %2, %%mm6                 \n\t"
213                 "packssdw %%mm6, %%mm6          \n\t"
214                 "packssdw %%mm6, %%mm6          \n\t"
215                 "movl %3, %%eax                 \n\t"
216                 ".balign 16\n\t"
217                 "1:                             \n\t"
218                 "movq (%0, %%eax), %%mm0        \n\t"
219                 "movq 8(%0, %%eax), %%mm1       \n\t"
220                 "movq (%1, %%eax), %%mm4        \n\t"
221                 "movq 8(%1, %%eax), %%mm5       \n\t"
222                 "pmullw %%mm6, %%mm4            \n\t" // q=qscale*quant_matrix[i]
223                 "pmullw %%mm6, %%mm5            \n\t" // q=qscale*quant_matrix[i]
224                 "pxor %%mm2, %%mm2              \n\t"
225                 "pxor %%mm3, %%mm3              \n\t"
226                 "pcmpgtw %%mm0, %%mm2           \n\t" // block[i] < 0 ? -1 : 0
227                 "pcmpgtw %%mm1, %%mm3           \n\t" // block[i] < 0 ? -1 : 0
228                 "pxor %%mm2, %%mm0              \n\t"
229                 "pxor %%mm3, %%mm1              \n\t"
230                 "psubw %%mm2, %%mm0             \n\t" // abs(block[i])
231                 "psubw %%mm3, %%mm1             \n\t" // abs(block[i])
232                 "pmullw %%mm4, %%mm0            \n\t" // abs(block[i])*q
233                 "pmullw %%mm5, %%mm1            \n\t" // abs(block[i])*q
234                 "pxor %%mm4, %%mm4              \n\t"
235                 "pxor %%mm5, %%mm5              \n\t" // FIXME slow
236                 "pcmpeqw (%0, %%eax), %%mm4     \n\t" // block[i] == 0 ? -1 : 0
237                 "pcmpeqw 8(%0, %%eax), %%mm5    \n\t" // block[i] == 0 ? -1 : 0
238                 "psraw $3, %%mm0                \n\t"
239                 "psraw $3, %%mm1                \n\t"
240                 "psubw %%mm7, %%mm0             \n\t"
241                 "psubw %%mm7, %%mm1             \n\t"
242                 "por %%mm7, %%mm0               \n\t"
243                 "por %%mm7, %%mm1               \n\t"
244                 "pxor %%mm2, %%mm0              \n\t"
245                 "pxor %%mm3, %%mm1              \n\t"
246                 "psubw %%mm2, %%mm0             \n\t"
247                 "psubw %%mm3, %%mm1             \n\t"
248                 "pandn %%mm0, %%mm4             \n\t"
249                 "pandn %%mm1, %%mm5             \n\t"
250                 "movq %%mm4, (%0, %%eax)        \n\t"
251                 "movq %%mm5, 8(%0, %%eax)       \n\t"
252
253                 "addl $16, %%eax                \n\t"
254                 "js 1b                          \n\t"
255                 ::"r" (block+nCoeffs), "r"(quant_matrix+nCoeffs), "g" (qscale), "g" (-2*nCoeffs)
256                 : "%eax", "memory"
257         );    
258         block[0]= block0;
259
260         } else {
261         quant_matrix = s->inter_matrix;
262 asm volatile(
263                 "pcmpeqw %%mm7, %%mm7           \n\t"
264                 "psrlw $15, %%mm7               \n\t"
265                 "movd %2, %%mm6                 \n\t"
266                 "packssdw %%mm6, %%mm6          \n\t"
267                 "packssdw %%mm6, %%mm6          \n\t"
268                 "movl %3, %%eax                 \n\t"
269                 ".balign 16\n\t"
270                 "1:                             \n\t"
271                 "movq (%0, %%eax), %%mm0        \n\t"
272                 "movq 8(%0, %%eax), %%mm1       \n\t"
273                 "movq (%1, %%eax), %%mm4        \n\t"
274                 "movq 8(%1, %%eax), %%mm5       \n\t"
275                 "pmullw %%mm6, %%mm4            \n\t" // q=qscale*quant_matrix[i]
276                 "pmullw %%mm6, %%mm5            \n\t" // q=qscale*quant_matrix[i]
277                 "pxor %%mm2, %%mm2              \n\t"
278                 "pxor %%mm3, %%mm3              \n\t"
279                 "pcmpgtw %%mm0, %%mm2           \n\t" // block[i] < 0 ? -1 : 0
280                 "pcmpgtw %%mm1, %%mm3           \n\t" // block[i] < 0 ? -1 : 0
281                 "pxor %%mm2, %%mm0              \n\t"
282                 "pxor %%mm3, %%mm1              \n\t"
283                 "psubw %%mm2, %%mm0             \n\t" // abs(block[i])
284                 "psubw %%mm3, %%mm1             \n\t" // abs(block[i])
285                 "paddw %%mm0, %%mm0             \n\t" // abs(block[i])*2
286                 "paddw %%mm1, %%mm1             \n\t" // abs(block[i])*2
287                 "paddw %%mm7, %%mm0             \n\t" // abs(block[i])*2 + 1
288                 "paddw %%mm7, %%mm1             \n\t" // abs(block[i])*2 + 1
289                 "pmullw %%mm4, %%mm0            \n\t" // (abs(block[i])*2 + 1)*q
290                 "pmullw %%mm5, %%mm1            \n\t" // (abs(block[i])*2 + 1)*q
291                 "pxor %%mm4, %%mm4              \n\t"
292                 "pxor %%mm5, %%mm5              \n\t" // FIXME slow
293                 "pcmpeqw (%0, %%eax), %%mm4     \n\t" // block[i] == 0 ? -1 : 0
294                 "pcmpeqw 8(%0, %%eax), %%mm5    \n\t" // block[i] == 0 ? -1 : 0
295                 "psraw $4, %%mm0                \n\t"
296                 "psraw $4, %%mm1                \n\t"
297                 "psubw %%mm7, %%mm0             \n\t"
298                 "psubw %%mm7, %%mm1             \n\t"
299                 "por %%mm7, %%mm0               \n\t"
300                 "por %%mm7, %%mm1               \n\t"
301                 "pxor %%mm2, %%mm0              \n\t"
302                 "pxor %%mm3, %%mm1              \n\t"
303                 "psubw %%mm2, %%mm0             \n\t"
304                 "psubw %%mm3, %%mm1             \n\t"
305                 "pandn %%mm0, %%mm4             \n\t"
306                 "pandn %%mm1, %%mm5             \n\t"
307                 "movq %%mm4, (%0, %%eax)        \n\t"
308                 "movq %%mm5, 8(%0, %%eax)       \n\t"
309
310                 "addl $16, %%eax                \n\t"
311                 "js 1b                          \n\t"
312                 ::"r" (block+nCoeffs), "r"(quant_matrix+nCoeffs), "g" (qscale), "g" (-2*nCoeffs)
313                 : "%eax", "memory"
314         );
315     }
316 }
317
318 static void dct_unquantize_mpeg2_mmx(MpegEncContext *s,
319                                      DCTELEM *block, int n, int qscale)
320 {
321     int nCoeffs;
322     const UINT16 *quant_matrix;
323     
324     if(s->alternate_scan) nCoeffs= 64;
325     else nCoeffs= nCoeffs= zigzag_end[ s->block_last_index[n] ];
326
327     if (s->mb_intra) {
328         int block0;
329         if (n < 4) 
330             block0 = block[0] * s->y_dc_scale;
331         else
332             block0 = block[0] * s->c_dc_scale;
333         quant_matrix = s->intra_matrix;
334 asm volatile(
335                 "pcmpeqw %%mm7, %%mm7           \n\t"
336                 "psrlw $15, %%mm7               \n\t"
337                 "movd %2, %%mm6                 \n\t"
338                 "packssdw %%mm6, %%mm6          \n\t"
339                 "packssdw %%mm6, %%mm6          \n\t"
340                 "movl %3, %%eax                 \n\t"
341                 ".balign 16\n\t"
342                 "1:                             \n\t"
343                 "movq (%0, %%eax), %%mm0        \n\t"
344                 "movq 8(%0, %%eax), %%mm1       \n\t"
345                 "movq (%1, %%eax), %%mm4        \n\t"
346                 "movq 8(%1, %%eax), %%mm5       \n\t"
347                 "pmullw %%mm6, %%mm4            \n\t" // q=qscale*quant_matrix[i]
348                 "pmullw %%mm6, %%mm5            \n\t" // q=qscale*quant_matrix[i]
349                 "pxor %%mm2, %%mm2              \n\t"
350                 "pxor %%mm3, %%mm3              \n\t"
351                 "pcmpgtw %%mm0, %%mm2           \n\t" // block[i] < 0 ? -1 : 0
352                 "pcmpgtw %%mm1, %%mm3           \n\t" // block[i] < 0 ? -1 : 0
353                 "pxor %%mm2, %%mm0              \n\t"
354                 "pxor %%mm3, %%mm1              \n\t"
355                 "psubw %%mm2, %%mm0             \n\t" // abs(block[i])
356                 "psubw %%mm3, %%mm1             \n\t" // abs(block[i])
357                 "pmullw %%mm4, %%mm0            \n\t" // abs(block[i])*q
358                 "pmullw %%mm5, %%mm1            \n\t" // abs(block[i])*q
359                 "pxor %%mm4, %%mm4              \n\t"
360                 "pxor %%mm5, %%mm5              \n\t" // FIXME slow
361                 "pcmpeqw (%0, %%eax), %%mm4     \n\t" // block[i] == 0 ? -1 : 0
362                 "pcmpeqw 8(%0, %%eax), %%mm5    \n\t" // block[i] == 0 ? -1 : 0
363                 "psraw $3, %%mm0                \n\t"
364                 "psraw $3, %%mm1                \n\t"
365                 "pxor %%mm2, %%mm0              \n\t"
366                 "pxor %%mm3, %%mm1              \n\t"
367                 "psubw %%mm2, %%mm0             \n\t"
368                 "psubw %%mm3, %%mm1             \n\t"
369                 "pandn %%mm0, %%mm4             \n\t"
370                 "pandn %%mm1, %%mm5             \n\t"
371                 "movq %%mm4, (%0, %%eax)        \n\t"
372                 "movq %%mm5, 8(%0, %%eax)       \n\t"
373
374                 "addl $16, %%eax                \n\t"
375                 "js 1b                          \n\t"
376                 ::"r" (block+nCoeffs), "r"(quant_matrix+nCoeffs), "g" (qscale), "g" (-2*nCoeffs)
377                 : "%eax", "memory"
378         );    
379         block[0]= block0;
380         //Note, we dont do mismatch control for intra as errors cannot accumulate
381
382     } else {
383         quant_matrix = s->inter_matrix;
384 asm volatile(
385                 "pcmpeqw %%mm7, %%mm7           \n\t"
386                 "psrlq $48, %%mm7               \n\t"
387                 "movd %2, %%mm6                 \n\t"
388                 "packssdw %%mm6, %%mm6          \n\t"
389                 "packssdw %%mm6, %%mm6          \n\t"
390                 "movl %3, %%eax                 \n\t"
391                 ".balign 16\n\t"
392                 "1:                             \n\t"
393                 "movq (%0, %%eax), %%mm0        \n\t"
394                 "movq 8(%0, %%eax), %%mm1       \n\t"
395                 "movq (%1, %%eax), %%mm4        \n\t"
396                 "movq 8(%1, %%eax), %%mm5       \n\t"
397                 "pmullw %%mm6, %%mm4            \n\t" // q=qscale*quant_matrix[i]
398                 "pmullw %%mm6, %%mm5            \n\t" // q=qscale*quant_matrix[i]
399                 "pxor %%mm2, %%mm2              \n\t"
400                 "pxor %%mm3, %%mm3              \n\t"
401                 "pcmpgtw %%mm0, %%mm2           \n\t" // block[i] < 0 ? -1 : 0
402                 "pcmpgtw %%mm1, %%mm3           \n\t" // block[i] < 0 ? -1 : 0
403                 "pxor %%mm2, %%mm0              \n\t"
404                 "pxor %%mm3, %%mm1              \n\t"
405                 "psubw %%mm2, %%mm0             \n\t" // abs(block[i])
406                 "psubw %%mm3, %%mm1             \n\t" // abs(block[i])
407                 "paddw %%mm0, %%mm0             \n\t" // abs(block[i])*2
408                 "paddw %%mm1, %%mm1             \n\t" // abs(block[i])*2
409                 "pmullw %%mm4, %%mm0            \n\t" // abs(block[i])*2*q
410                 "pmullw %%mm5, %%mm1            \n\t" // abs(block[i])*2*q
411                 "paddw %%mm4, %%mm0             \n\t" // (abs(block[i])*2 + 1)*q
412                 "paddw %%mm5, %%mm1             \n\t" // (abs(block[i])*2 + 1)*q
413                 "pxor %%mm4, %%mm4              \n\t"
414                 "pxor %%mm5, %%mm5              \n\t" // FIXME slow
415                 "pcmpeqw (%0, %%eax), %%mm4     \n\t" // block[i] == 0 ? -1 : 0
416                 "pcmpeqw 8(%0, %%eax), %%mm5    \n\t" // block[i] == 0 ? -1 : 0
417                 "psrlw $4, %%mm0                \n\t"
418                 "psrlw $4, %%mm1                \n\t"
419                 "pxor %%mm2, %%mm0              \n\t"
420                 "pxor %%mm3, %%mm1              \n\t"
421                 "psubw %%mm2, %%mm0             \n\t"
422                 "psubw %%mm3, %%mm1             \n\t"
423                 "pandn %%mm0, %%mm4             \n\t"
424                 "pandn %%mm1, %%mm5             \n\t"
425                 "pxor %%mm4, %%mm7              \n\t"
426                 "pxor %%mm5, %%mm7              \n\t"
427                 "movq %%mm4, (%0, %%eax)        \n\t"
428                 "movq %%mm5, 8(%0, %%eax)       \n\t"
429
430                 "addl $16, %%eax                \n\t"
431                 "js 1b                          \n\t"
432                 "movd 124(%0, %3), %%mm0        \n\t"
433                 "movq %%mm7, %%mm6              \n\t"
434                 "psrlq $32, %%mm7               \n\t"
435                 "pxor %%mm6, %%mm7              \n\t"
436                 "movq %%mm7, %%mm6              \n\t"
437                 "psrlq $16, %%mm7               \n\t"
438                 "pxor %%mm6, %%mm7              \n\t"
439                 "pslld $31, %%mm7               \n\t"
440                 "psrlq $15, %%mm7               \n\t"
441                 "pxor %%mm7, %%mm0              \n\t"
442                 "movd %%mm0, 124(%0, %3)        \n\t"
443                 
444                 ::"r" (block+nCoeffs), "r"(quant_matrix+nCoeffs), "g" (qscale), "r" (-2*nCoeffs)
445                 : "%eax", "memory"
446         );
447     }
448 }
449
450 /* draw the edges of width 'w' of an image of size width, height 
451    this mmx version can only handle w==8 || w==16 */
452 static void draw_edges_mmx(UINT8 *buf, int wrap, int width, int height, int w)
453 {
454     UINT8 *ptr, *last_line;
455     int i;
456
457     last_line = buf + (height - 1) * wrap;
458     /* left and right */
459     ptr = buf;
460     if(w==8)
461     {
462         asm volatile(
463                 "1:                             \n\t"
464                 "movd (%0), %%mm0               \n\t"
465                 "punpcklbw %%mm0, %%mm0         \n\t" 
466                 "punpcklwd %%mm0, %%mm0         \n\t"
467                 "punpckldq %%mm0, %%mm0         \n\t"
468                 "movq %%mm0, -8(%0)             \n\t"
469                 "movq -8(%0, %2), %%mm1         \n\t"
470                 "punpckhbw %%mm1, %%mm1         \n\t"
471                 "punpckhwd %%mm1, %%mm1         \n\t"
472                 "punpckhdq %%mm1, %%mm1         \n\t"
473                 "movq %%mm1, (%0, %2)           \n\t"
474                 "addl %1, %0                    \n\t"
475                 "cmpl %3, %0                    \n\t"
476                 " jb 1b                         \n\t"
477                 : "+r" (ptr)
478                 : "r" (wrap), "r" (width), "r" (ptr + wrap*height)
479         );
480     }
481     else
482     {
483         asm volatile(
484                 "1:                             \n\t"
485                 "movd (%0), %%mm0               \n\t"
486                 "punpcklbw %%mm0, %%mm0         \n\t" 
487                 "punpcklwd %%mm0, %%mm0         \n\t"
488                 "punpckldq %%mm0, %%mm0         \n\t"
489                 "movq %%mm0, -8(%0)             \n\t"
490                 "movq %%mm0, -16(%0)            \n\t"
491                 "movq -8(%0, %2), %%mm1         \n\t"
492                 "punpckhbw %%mm1, %%mm1         \n\t"
493                 "punpckhwd %%mm1, %%mm1         \n\t"
494                 "punpckhdq %%mm1, %%mm1         \n\t"
495                 "movq %%mm1, (%0, %2)           \n\t"
496                 "movq %%mm1, 8(%0, %2)          \n\t"
497                 "addl %1, %0                    \n\t"
498                 "cmpl %3, %0                    \n\t"
499                 " jb 1b                         \n\t"           
500                 : "+r" (ptr)
501                 : "r" (wrap), "r" (width), "r" (ptr + wrap*height)
502         );
503     }
504     
505     for(i=0;i<w;i+=4) {
506         /* top and bottom (and hopefully also the corners) */
507         ptr= buf - (i + 1) * wrap - w;
508         asm volatile(
509                 "1:                             \n\t"
510                 "movq (%1, %0), %%mm0           \n\t"
511                 "movq %%mm0, (%0)               \n\t"
512                 "movq %%mm0, (%0, %2)           \n\t"
513                 "movq %%mm0, (%0, %2, 2)        \n\t"
514                 "movq %%mm0, (%0, %3)           \n\t"
515                 "addl $8, %0                    \n\t"
516                 "cmpl %4, %0                    \n\t"
517                 " jb 1b                         \n\t"
518                 : "+r" (ptr)
519                 : "r" ((int)buf - (int)ptr - w), "r" (-wrap), "r" (-wrap*3), "r" (ptr+width+2*w)
520         );
521         ptr= last_line + (i + 1) * wrap - w;
522         asm volatile(
523                 "1:                             \n\t"
524                 "movq (%1, %0), %%mm0           \n\t"
525                 "movq %%mm0, (%0)               \n\t"
526                 "movq %%mm0, (%0, %2)           \n\t"
527                 "movq %%mm0, (%0, %2, 2)        \n\t"
528                 "movq %%mm0, (%0, %3)           \n\t"
529                 "addl $8, %0                    \n\t"
530                 "cmpl %4, %0                    \n\t"
531                 " jb 1b                         \n\t"
532                 : "+r" (ptr)
533                 : "r" ((int)last_line - (int)ptr - w), "r" (wrap), "r" (wrap*3), "r" (ptr+width+2*w)
534         );
535     }
536 }
537
538 static volatile int esp_temp;
539
540 void unused_var_warning_killer(){
541         esp_temp++;
542 }
543
544 #undef HAVE_MMX2
545 #define RENAME(a) a ## _MMX
546 #include "mpegvideo_mmx_template.c"
547
548 #define HAVE_MMX2
549 #undef RENAME
550 #define RENAME(a) a ## _MMX2
551 #include "mpegvideo_mmx_template.c"
552
553 void MPV_common_init_mmx(MpegEncContext *s)
554 {
555     if (mm_flags & MM_MMX) {
556         s->dct_unquantize_h263 = dct_unquantize_h263_mmx;
557         s->dct_unquantize_mpeg1 = dct_unquantize_mpeg1_mmx;
558         s->dct_unquantize_mpeg2 = dct_unquantize_mpeg2_mmx;
559
560         draw_edges = draw_edges_mmx;
561
562         if(mm_flags & MM_MMXEXT){
563             dct_quantize= dct_quantize_MMX2;
564         } else {
565             dct_quantize= dct_quantize_MMX;
566         }
567     }
568 }