mpegvideo_mmx.c 15.8 KB
Newer Older
Nick Kurshev's avatar
Nick Kurshev committed
1 2
/*
 * The simplest mpeg encoder (well, it was the simplest!)
3
 * Copyright (c) 2000,2001 Fabrice Bellard.
Nick Kurshev's avatar
Nick Kurshev committed
4
 *
5 6 7 8
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
Nick Kurshev's avatar
Nick Kurshev committed
9
 *
10
 * This library is distributed in the hope that it will be useful,
Nick Kurshev's avatar
Nick Kurshev committed
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
Nick Kurshev's avatar
Nick Kurshev committed
14
 *
15 16 17
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
Nick Kurshev's avatar
Nick Kurshev committed
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>
Nick Kurshev's avatar
Nick Kurshev committed
21 22
 */

23 24
#include "../dsputil.h"
#include "../mpegvideo.h"
25
#include "../avcodec.h"
26

27 28
extern uint8_t zigzag_direct_noperm[64];
extern uint16_t inv_zigzag_direct16[64];
29

Nick Kurshev's avatar
Nick Kurshev committed
30 31 32
static const unsigned long long int mm_wabs __attribute__ ((aligned(8))) = 0xffffffffffffffffULL;
static const unsigned long long int mm_wone __attribute__ ((aligned(8))) = 0x0001000100010001ULL;

33 34 35 36

static void dct_unquantize_h263_mmx(MpegEncContext *s,
                                  DCTELEM *block, int n, int qscale)
{
37 38 39 40
    int level, qmul, qadd, nCoeffs;

    qmul = qscale << 1;
    qadd = (qscale - 1) | 1;
41

42 43
    assert(s->block_last_index[n]>=0);
        
44
    if (s->mb_intra) {
45 46
        if (!s->h263_aic) {
            if (n < 4)
47
                level = block[0] * s->y_dc_scale;
48
            else
49 50 51 52
                level = block[0] * s->c_dc_scale;
        }else{
            qadd = 0;
            level= block[0];
53
        }
54
        nCoeffs=63;
55
    } else {
56 57
	nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
        level = 0;/* keep gcc quiet */
58
    }
59
//printf("%d %d  ", qmul, qadd);
60 61 62 63 64 65 66 67 68 69
asm volatile(
		"movd %1, %%mm6			\n\t" //qmul
		"packssdw %%mm6, %%mm6		\n\t"
		"packssdw %%mm6, %%mm6		\n\t"
		"movd %2, %%mm5			\n\t" //qadd
		"pxor %%mm7, %%mm7		\n\t"
		"packssdw %%mm5, %%mm5		\n\t"
		"packssdw %%mm5, %%mm5		\n\t"
		"psubw %%mm5, %%mm7		\n\t"
		"pxor %%mm4, %%mm4		\n\t"
Nick Kurshev's avatar
Nick Kurshev committed
70
		".balign 16\n\t"
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
		"1:				\n\t"
		"movq (%0, %3), %%mm0		\n\t"
		"movq 8(%0, %3), %%mm1		\n\t"

		"pmullw %%mm6, %%mm0		\n\t"
		"pmullw %%mm6, %%mm1		\n\t"

		"movq (%0, %3), %%mm2		\n\t"
		"movq 8(%0, %3), %%mm3		\n\t"

		"pcmpgtw %%mm4, %%mm2		\n\t" // block[i] < 0 ? -1 : 0
		"pcmpgtw %%mm4, %%mm3		\n\t" // block[i] < 0 ? -1 : 0

		"pxor %%mm2, %%mm0		\n\t"
		"pxor %%mm3, %%mm1		\n\t"

		"paddw %%mm7, %%mm0		\n\t"
		"paddw %%mm7, %%mm1		\n\t"

		"pxor %%mm0, %%mm2		\n\t"
		"pxor %%mm1, %%mm3		\n\t"

		"pcmpeqw %%mm7, %%mm0		\n\t" // block[i] == 0 ? -1 : 0
		"pcmpeqw %%mm7, %%mm1		\n\t" // block[i] == 0 ? -1 : 0

		"pandn %%mm2, %%mm0		\n\t"
		"pandn %%mm3, %%mm1		\n\t"

		"movq %%mm0, (%0, %3)		\n\t"
		"movq %%mm1, 8(%0, %3)		\n\t"

		"addl $16, %3			\n\t"
103 104
		"jng 1b				\n\t"
		::"r" (block+nCoeffs), "g"(qmul), "g" (qadd), "r" (2*(-nCoeffs))
105 106
		: "memory"
	);
107 108
        if(s->mb_intra)
            block[0]= level;
109 110 111
}


Nick Kurshev's avatar
Nick Kurshev committed
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
/*
  NK:
  Note: looking at PARANOID:
  "enable all paranoid tests for rounding, overflows, etc..."

#ifdef PARANOID
                if (level < -2048 || level > 2047)
                    fprintf(stderr, "unquant error %d %d\n", i, level);
#endif
  We can suppose that result of two multiplications can't be greate of 0xFFFF
  i.e. is 16-bit, so we use here only PMULLW instruction and can avoid
  a complex multiplication.
=====================================================
 Full formula for multiplication of 2 integer numbers
 which are represent as high:low words:
 input: value1 = high1:low1
        value2 = high2:low2
 output: value3 = value1*value2
 value3=high3:low3 (on overflow: modulus 2^32 wrap-around)
 this mean that for 0x123456 * 0x123456 correct result is 0x766cb0ce4
 but this algorithm will compute only 0x66cb0ce4
 this limited by 16-bit size of operands
 ---------------------------------
 tlow1 = high1*low2
 tlow2 = high2*low1
 tlow1 = tlow1 + tlow2
 high3:low3 = low1*low2
 high3 += tlow1
*/
141 142
static void dct_unquantize_mpeg1_mmx(MpegEncContext *s,
                                     DCTELEM *block, int n, int qscale)
Nick Kurshev's avatar
Nick Kurshev committed
143
{
144
    int nCoeffs;
145
    const uint16_t *quant_matrix;
146 147 148 149

    assert(s->block_last_index[n]>=0);

    nCoeffs= s->intra_scantable.raster_end[ s->block_last_index[n] ]+1;
150

Nick Kurshev's avatar
Nick Kurshev committed
151
    if (s->mb_intra) {
152
        int block0;
Nick Kurshev's avatar
Nick Kurshev committed
153
        if (n < 4) 
154
            block0 = block[0] * s->y_dc_scale;
Nick Kurshev's avatar
Nick Kurshev committed
155
        else
156
            block0 = block[0] * s->c_dc_scale;
Nick Kurshev's avatar
Nick Kurshev committed
157 158
        /* XXX: only mpeg1 */
        quant_matrix = s->intra_matrix;
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
asm volatile(
		"pcmpeqw %%mm7, %%mm7		\n\t"
		"psrlw $15, %%mm7		\n\t"
		"movd %2, %%mm6			\n\t"
		"packssdw %%mm6, %%mm6		\n\t"
		"packssdw %%mm6, %%mm6		\n\t"
                "movl %3, %%eax			\n\t"
		".balign 16\n\t"
		"1:				\n\t"
		"movq (%0, %%eax), %%mm0	\n\t"
		"movq 8(%0, %%eax), %%mm1	\n\t"
		"movq (%1, %%eax), %%mm4	\n\t"
		"movq 8(%1, %%eax), %%mm5	\n\t"
		"pmullw %%mm6, %%mm4		\n\t" // q=qscale*quant_matrix[i]
		"pmullw %%mm6, %%mm5		\n\t" // q=qscale*quant_matrix[i]
		"pxor %%mm2, %%mm2		\n\t"
		"pxor %%mm3, %%mm3		\n\t"
		"pcmpgtw %%mm0, %%mm2		\n\t" // block[i] < 0 ? -1 : 0
		"pcmpgtw %%mm1, %%mm3		\n\t" // block[i] < 0 ? -1 : 0
		"pxor %%mm2, %%mm0		\n\t"
		"pxor %%mm3, %%mm1		\n\t"
		"psubw %%mm2, %%mm0		\n\t" // abs(block[i])
		"psubw %%mm3, %%mm1		\n\t" // abs(block[i])
		"pmullw %%mm4, %%mm0		\n\t" // abs(block[i])*q
		"pmullw %%mm5, %%mm1		\n\t" // abs(block[i])*q
		"pxor %%mm4, %%mm4		\n\t"
		"pxor %%mm5, %%mm5		\n\t" // FIXME slow
		"pcmpeqw (%0, %%eax), %%mm4	\n\t" // block[i] == 0 ? -1 : 0
		"pcmpeqw 8(%0, %%eax), %%mm5	\n\t" // block[i] == 0 ? -1 : 0
		"psraw $3, %%mm0		\n\t"
		"psraw $3, %%mm1		\n\t"
		"psubw %%mm7, %%mm0		\n\t"
		"psubw %%mm7, %%mm1		\n\t"
		"por %%mm7, %%mm0		\n\t"
		"por %%mm7, %%mm1		\n\t"
		"pxor %%mm2, %%mm0		\n\t"
		"pxor %%mm3, %%mm1		\n\t"
		"psubw %%mm2, %%mm0		\n\t"
		"psubw %%mm3, %%mm1		\n\t"
		"pandn %%mm0, %%mm4		\n\t"
		"pandn %%mm1, %%mm5		\n\t"
		"movq %%mm4, (%0, %%eax)	\n\t"
		"movq %%mm5, 8(%0, %%eax)	\n\t"

		"addl $16, %%eax		\n\t"
		"js 1b				\n\t"
		::"r" (block+nCoeffs), "r"(quant_matrix+nCoeffs), "g" (qscale), "g" (-2*nCoeffs)
		: "%eax", "memory"
	);    
        block[0]= block0;

        } else {
211
        quant_matrix = s->inter_matrix;
212 213 214 215 216 217
asm volatile(
		"pcmpeqw %%mm7, %%mm7		\n\t"
		"psrlw $15, %%mm7		\n\t"
		"movd %2, %%mm6			\n\t"
		"packssdw %%mm6, %%mm6		\n\t"
		"packssdw %%mm6, %%mm6		\n\t"
218
                "movl %3, %%eax			\n\t"
Nick Kurshev's avatar
Nick Kurshev committed
219
		".balign 16\n\t"
220
		"1:				\n\t"
221 222 223 224
		"movq (%0, %%eax), %%mm0	\n\t"
		"movq 8(%0, %%eax), %%mm1	\n\t"
		"movq (%1, %%eax), %%mm4	\n\t"
		"movq 8(%1, %%eax), %%mm5	\n\t"
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
		"pmullw %%mm6, %%mm4		\n\t" // q=qscale*quant_matrix[i]
		"pmullw %%mm6, %%mm5		\n\t" // q=qscale*quant_matrix[i]
		"pxor %%mm2, %%mm2		\n\t"
		"pxor %%mm3, %%mm3		\n\t"
		"pcmpgtw %%mm0, %%mm2		\n\t" // block[i] < 0 ? -1 : 0
		"pcmpgtw %%mm1, %%mm3		\n\t" // block[i] < 0 ? -1 : 0
		"pxor %%mm2, %%mm0		\n\t"
		"pxor %%mm3, %%mm1		\n\t"
		"psubw %%mm2, %%mm0		\n\t" // abs(block[i])
		"psubw %%mm3, %%mm1		\n\t" // abs(block[i])
		"paddw %%mm0, %%mm0		\n\t" // abs(block[i])*2
		"paddw %%mm1, %%mm1		\n\t" // abs(block[i])*2
		"paddw %%mm7, %%mm0		\n\t" // abs(block[i])*2 + 1
		"paddw %%mm7, %%mm1		\n\t" // abs(block[i])*2 + 1
		"pmullw %%mm4, %%mm0		\n\t" // (abs(block[i])*2 + 1)*q
		"pmullw %%mm5, %%mm1		\n\t" // (abs(block[i])*2 + 1)*q
		"pxor %%mm4, %%mm4		\n\t"
		"pxor %%mm5, %%mm5		\n\t" // FIXME slow
243 244
		"pcmpeqw (%0, %%eax), %%mm4	\n\t" // block[i] == 0 ? -1 : 0
		"pcmpeqw 8(%0, %%eax), %%mm5	\n\t" // block[i] == 0 ? -1 : 0
245 246 247 248 249 250 251 252 253 254 255 256
		"psraw $4, %%mm0		\n\t"
		"psraw $4, %%mm1		\n\t"
		"psubw %%mm7, %%mm0		\n\t"
		"psubw %%mm7, %%mm1		\n\t"
		"por %%mm7, %%mm0		\n\t"
		"por %%mm7, %%mm1		\n\t"
		"pxor %%mm2, %%mm0		\n\t"
		"pxor %%mm3, %%mm1		\n\t"
		"psubw %%mm2, %%mm0		\n\t"
		"psubw %%mm3, %%mm1		\n\t"
		"pandn %%mm0, %%mm4		\n\t"
		"pandn %%mm1, %%mm5		\n\t"
257 258
		"movq %%mm4, (%0, %%eax)	\n\t"
		"movq %%mm5, 8(%0, %%eax)	\n\t"
259

260
		"addl $16, %%eax		\n\t"
261
		"js 1b				\n\t"
262 263 264 265
		::"r" (block+nCoeffs), "r"(quant_matrix+nCoeffs), "g" (qscale), "g" (-2*nCoeffs)
		: "%eax", "memory"
	);
    }
266

267 268 269 270 271 272
}

static void dct_unquantize_mpeg2_mmx(MpegEncContext *s,
                                     DCTELEM *block, int n, int qscale)
{
    int nCoeffs;
273
    const uint16_t *quant_matrix;
274
    
275 276 277 278
    assert(s->block_last_index[n]>=0);

    if(s->alternate_scan) nCoeffs= 63; //FIXME
    else nCoeffs= s->intra_scantable.raster_end[ s->block_last_index[n] ];
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327

    if (s->mb_intra) {
        int block0;
        if (n < 4) 
            block0 = block[0] * s->y_dc_scale;
        else
            block0 = block[0] * s->c_dc_scale;
        quant_matrix = s->intra_matrix;
asm volatile(
		"pcmpeqw %%mm7, %%mm7		\n\t"
		"psrlw $15, %%mm7		\n\t"
		"movd %2, %%mm6			\n\t"
		"packssdw %%mm6, %%mm6		\n\t"
		"packssdw %%mm6, %%mm6		\n\t"
                "movl %3, %%eax			\n\t"
		".balign 16\n\t"
		"1:				\n\t"
		"movq (%0, %%eax), %%mm0	\n\t"
		"movq 8(%0, %%eax), %%mm1	\n\t"
		"movq (%1, %%eax), %%mm4	\n\t"
		"movq 8(%1, %%eax), %%mm5	\n\t"
		"pmullw %%mm6, %%mm4		\n\t" // q=qscale*quant_matrix[i]
		"pmullw %%mm6, %%mm5		\n\t" // q=qscale*quant_matrix[i]
		"pxor %%mm2, %%mm2		\n\t"
		"pxor %%mm3, %%mm3		\n\t"
		"pcmpgtw %%mm0, %%mm2		\n\t" // block[i] < 0 ? -1 : 0
		"pcmpgtw %%mm1, %%mm3		\n\t" // block[i] < 0 ? -1 : 0
		"pxor %%mm2, %%mm0		\n\t"
		"pxor %%mm3, %%mm1		\n\t"
		"psubw %%mm2, %%mm0		\n\t" // abs(block[i])
		"psubw %%mm3, %%mm1		\n\t" // abs(block[i])
		"pmullw %%mm4, %%mm0		\n\t" // abs(block[i])*q
		"pmullw %%mm5, %%mm1		\n\t" // abs(block[i])*q
		"pxor %%mm4, %%mm4		\n\t"
		"pxor %%mm5, %%mm5		\n\t" // FIXME slow
		"pcmpeqw (%0, %%eax), %%mm4	\n\t" // block[i] == 0 ? -1 : 0
		"pcmpeqw 8(%0, %%eax), %%mm5	\n\t" // block[i] == 0 ? -1 : 0
		"psraw $3, %%mm0		\n\t"
		"psraw $3, %%mm1		\n\t"
		"pxor %%mm2, %%mm0		\n\t"
		"pxor %%mm3, %%mm1		\n\t"
		"psubw %%mm2, %%mm0		\n\t"
		"psubw %%mm3, %%mm1		\n\t"
		"pandn %%mm0, %%mm4		\n\t"
		"pandn %%mm1, %%mm5		\n\t"
		"movq %%mm4, (%0, %%eax)	\n\t"
		"movq %%mm5, 8(%0, %%eax)	\n\t"

		"addl $16, %%eax		\n\t"
328
		"jng 1b				\n\t"
329 330 331 332 333 334 335
		::"r" (block+nCoeffs), "r"(quant_matrix+nCoeffs), "g" (qscale), "g" (-2*nCoeffs)
		: "%eax", "memory"
	);    
        block[0]= block0;
        //Note, we dont do mismatch control for intra as errors cannot accumulate

    } else {
336
        quant_matrix = s->inter_matrix;
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
asm volatile(
		"pcmpeqw %%mm7, %%mm7		\n\t"
                "psrlq $48, %%mm7		\n\t"
		"movd %2, %%mm6			\n\t"
		"packssdw %%mm6, %%mm6		\n\t"
		"packssdw %%mm6, %%mm6		\n\t"
                "movl %3, %%eax			\n\t"
		".balign 16\n\t"
		"1:				\n\t"
		"movq (%0, %%eax), %%mm0	\n\t"
		"movq 8(%0, %%eax), %%mm1	\n\t"
		"movq (%1, %%eax), %%mm4	\n\t"
		"movq 8(%1, %%eax), %%mm5	\n\t"
		"pmullw %%mm6, %%mm4		\n\t" // q=qscale*quant_matrix[i]
		"pmullw %%mm6, %%mm5		\n\t" // q=qscale*quant_matrix[i]
		"pxor %%mm2, %%mm2		\n\t"
		"pxor %%mm3, %%mm3		\n\t"
		"pcmpgtw %%mm0, %%mm2		\n\t" // block[i] < 0 ? -1 : 0
		"pcmpgtw %%mm1, %%mm3		\n\t" // block[i] < 0 ? -1 : 0
		"pxor %%mm2, %%mm0		\n\t"
		"pxor %%mm3, %%mm1		\n\t"
		"psubw %%mm2, %%mm0		\n\t" // abs(block[i])
		"psubw %%mm3, %%mm1		\n\t" // abs(block[i])
		"paddw %%mm0, %%mm0		\n\t" // abs(block[i])*2
		"paddw %%mm1, %%mm1		\n\t" // abs(block[i])*2
		"pmullw %%mm4, %%mm0		\n\t" // abs(block[i])*2*q
		"pmullw %%mm5, %%mm1		\n\t" // abs(block[i])*2*q
		"paddw %%mm4, %%mm0		\n\t" // (abs(block[i])*2 + 1)*q
		"paddw %%mm5, %%mm1		\n\t" // (abs(block[i])*2 + 1)*q
		"pxor %%mm4, %%mm4		\n\t"
		"pxor %%mm5, %%mm5		\n\t" // FIXME slow
		"pcmpeqw (%0, %%eax), %%mm4	\n\t" // block[i] == 0 ? -1 : 0
		"pcmpeqw 8(%0, %%eax), %%mm5	\n\t" // block[i] == 0 ? -1 : 0
		"psrlw $4, %%mm0		\n\t"
		"psrlw $4, %%mm1		\n\t"
		"pxor %%mm2, %%mm0		\n\t"
		"pxor %%mm3, %%mm1		\n\t"
		"psubw %%mm2, %%mm0		\n\t"
		"psubw %%mm3, %%mm1		\n\t"
		"pandn %%mm0, %%mm4		\n\t"
		"pandn %%mm1, %%mm5		\n\t"
                "pxor %%mm4, %%mm7		\n\t"
                "pxor %%mm5, %%mm7		\n\t"
		"movq %%mm4, (%0, %%eax)	\n\t"
		"movq %%mm5, 8(%0, %%eax)	\n\t"

		"addl $16, %%eax		\n\t"
384
		"jng 1b				\n\t"
385 386 387 388 389 390 391 392 393 394 395 396 397 398
                "movd 124(%0, %3), %%mm0	\n\t"
                "movq %%mm7, %%mm6		\n\t"
                "psrlq $32, %%mm7		\n\t"
                "pxor %%mm6, %%mm7		\n\t"
                "movq %%mm7, %%mm6		\n\t"
                "psrlq $16, %%mm7		\n\t"
                "pxor %%mm6, %%mm7		\n\t"
                "pslld $31, %%mm7		\n\t"
                "psrlq $15, %%mm7		\n\t"
                "pxor %%mm7, %%mm0		\n\t"
                "movd %%mm0, 124(%0, %3)	\n\t"
                
		::"r" (block+nCoeffs), "r"(quant_matrix+nCoeffs), "g" (qscale), "r" (-2*nCoeffs)
		: "%eax", "memory"
399
	);
Nick Kurshev's avatar
Nick Kurshev committed
400 401 402
    }
}

403 404
/* draw the edges of width 'w' of an image of size width, height 
   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 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
    int i;

    last_line = buf + (height - 1) * wrap;
    /* left and right */
    ptr = buf;
    if(w==8)
    {
	asm volatile(
		"1:				\n\t"
		"movd (%0), %%mm0		\n\t"
		"punpcklbw %%mm0, %%mm0		\n\t" 
		"punpcklwd %%mm0, %%mm0		\n\t"
		"punpckldq %%mm0, %%mm0		\n\t"
		"movq %%mm0, -8(%0)		\n\t"
		"movq -8(%0, %2), %%mm1		\n\t"
		"punpckhbw %%mm1, %%mm1		\n\t"
		"punpckhwd %%mm1, %%mm1		\n\t"
		"punpckhdq %%mm1, %%mm1		\n\t"
		"movq %%mm1, (%0, %2)		\n\t"
		"addl %1, %0			\n\t"
		"cmpl %3, %0			\n\t"
		" jb 1b				\n\t"
		: "+r" (ptr)
		: "r" (wrap), "r" (width), "r" (ptr + wrap*height)
	);
    }
    else
    {
	asm volatile(
		"1:				\n\t"
		"movd (%0), %%mm0		\n\t"
		"punpcklbw %%mm0, %%mm0		\n\t" 
		"punpcklwd %%mm0, %%mm0		\n\t"
		"punpckldq %%mm0, %%mm0		\n\t"
		"movq %%mm0, -8(%0)		\n\t"
		"movq %%mm0, -16(%0)		\n\t"
		"movq -8(%0, %2), %%mm1		\n\t"
		"punpckhbw %%mm1, %%mm1		\n\t"
		"punpckhwd %%mm1, %%mm1		\n\t"
		"punpckhdq %%mm1, %%mm1		\n\t"
		"movq %%mm1, (%0, %2)		\n\t"
		"movq %%mm1, 8(%0, %2)		\n\t"
		"addl %1, %0			\n\t"
		"cmpl %3, %0			\n\t"
		" jb 1b				\n\t"		
		: "+r" (ptr)
		: "r" (wrap), "r" (width), "r" (ptr + wrap*height)
	);
    }
    
    for(i=0;i<w;i+=4) {
        /* top and bottom (and hopefully also the corners) */
	ptr= buf - (i + 1) * wrap - w;
	asm volatile(
		"1:				\n\t"
		"movq (%1, %0), %%mm0		\n\t"
		"movq %%mm0, (%0)		\n\t"
		"movq %%mm0, (%0, %2)		\n\t"
		"movq %%mm0, (%0, %2, 2)	\n\t"
		"movq %%mm0, (%0, %3)		\n\t"
		"addl $8, %0			\n\t"
		"cmpl %4, %0			\n\t"
		" jb 1b				\n\t"
		: "+r" (ptr)
		: "r" ((int)buf - (int)ptr - w), "r" (-wrap), "r" (-wrap*3), "r" (ptr+width+2*w)
	);
	ptr= last_line + (i + 1) * wrap - w;
	asm volatile(
		"1:				\n\t"
		"movq (%1, %0), %%mm0		\n\t"
		"movq %%mm0, (%0)		\n\t"
		"movq %%mm0, (%0, %2)		\n\t"
		"movq %%mm0, (%0, %2, 2)	\n\t"
		"movq %%mm0, (%0, %3)		\n\t"
		"addl $8, %0			\n\t"
		"cmpl %4, %0			\n\t"
		" jb 1b				\n\t"
		: "+r" (ptr)
		: "r" ((int)last_line - (int)ptr - w), "r" (wrap), "r" (wrap*3), "r" (ptr+width+2*w)
	);
    }
}

491 492
#undef HAVE_MMX2
#define RENAME(a) a ## _MMX
493
#define RENAMEl(a) a ## _mmx
494 495 496 497 498
#include "mpegvideo_mmx_template.c"

#define HAVE_MMX2
#undef RENAME
#define RENAME(a) a ## _MMX2
499
#define RENAMEl(a) a ## _mmx2
500
#include "mpegvideo_mmx_template.c"
501

502
void MPV_common_init_mmx(MpegEncContext *s)
Nick Kurshev's avatar
Nick Kurshev committed
503
{
504
    if (mm_flags & MM_MMX) {
505 506
        const int dct_algo = s->avctx->dct_algo;
        
507 508 509
        s->dct_unquantize_h263 = dct_unquantize_h263_mmx;
        s->dct_unquantize_mpeg1 = dct_unquantize_mpeg1_mmx;
        s->dct_unquantize_mpeg2 = dct_unquantize_mpeg2_mmx;
510

511
        draw_edges = draw_edges_mmx;
512

513 514 515 516 517 518
        if(dct_algo==FF_DCT_AUTO || dct_algo==FF_DCT_MMX){
            if(mm_flags & MM_MMXEXT){
                s->dct_quantize= dct_quantize_MMX2;
            } else {
                s->dct_quantize= dct_quantize_MMX;
            }
519
        }
520
    }
Nick Kurshev's avatar
Nick Kurshev committed
521
}