Statistics
| Branch: | Revision:

ffmpeg / libavcodec / i386 / mpegvideo_mmx.c @ 3bf43d42

History | View | Annotate | Download (12.8 KB)

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 dequantizer 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
extern void (*draw_edges)(UINT8 *buf, int wrap, int width, int height, int w);
30
extern int (*dct_quantize)(MpegEncContext *s, DCTELEM *block, int n, int qscale);
31

    
32
extern UINT8 zigzag_direct_noperm[64];
33
extern UINT16 inv_zigzag_direct16[64];
34
extern UINT32 inverse[256];
35

    
36
#if 0
37

38
/* XXX: GL: I don't understand why this function needs optimization
39
   (it is called only once per frame!), so I disabled it */
40

41
void MPV_frame_start(MpegEncContext *s)
42
{
43
    if (s->pict_type == B_TYPE) {
44
        __asm __volatile(
45
            "movl        (%1), %%eax\n\t"
46
            "movl        4(%1), %%edx\n\t"
47
            "movl        8(%1), %%ecx\n\t"
48
            "movl        %%eax, (%0)\n\t"
49
            "movl        %%edx, 4(%0)\n\t"
50
            "movl        %%ecx, 8(%0)\n\t"
51
            :
52
            :"r"(s->current_picture), "r"(s->aux_picture)
53
            :"eax","edx","ecx","memory");
54
    } else {
55
            /* swap next and last */
56
        __asm __volatile(
57
            "movl        (%1), %%eax\n\t"
58
            "movl        4(%1), %%edx\n\t"
59
            "movl        8(%1), %%ecx\n\t"
60
            "xchgl        (%0), %%eax\n\t"
61
            "xchgl        4(%0), %%edx\n\t"
62
            "xchgl        8(%0), %%ecx\n\t"
63
            "movl        %%eax, (%1)\n\t"
64
            "movl        %%edx, 4(%1)\n\t"
65
            "movl        %%ecx, 8(%1)\n\t"
66
            "movl        %%eax, (%2)\n\t"
67
            "movl        %%edx, 4(%2)\n\t"
68
            "movl        %%ecx, 8(%2)\n\t"
69
            :
70
            :"r"(s->last_picture), "r"(s->next_picture), "r"(s->current_picture)
71
            :"eax","edx","ecx","memory");
72
    }
73
}
74
#endif
75

    
76
static const unsigned long long int mm_wabs __attribute__ ((aligned(8))) = 0xffffffffffffffffULL;
77
static const unsigned long long int mm_wone __attribute__ ((aligned(8))) = 0x0001000100010001ULL;
78

    
79

    
80
static void dct_unquantize_h263_mmx(MpegEncContext *s,
81
                                  DCTELEM *block, int n, int qscale)
82
{
83
    int i, level, qmul, qadd, nCoeffs;
84
    
85
    qmul = s->qscale << 1;
86
    if (s->h263_aic && s->mb_intra)
87
        qadd = 0;
88
    else
89
        qadd = (s->qscale - 1) | 1;
90

    
91
    if (s->mb_intra) {
92
        if (!s->h263_aic) {
93
            if (n < 4)
94
                block[0] = block[0] * s->y_dc_scale;
95
            else
96
                block[0] = block[0] * s->c_dc_scale;
97
        }
98
        for(i=1; i<8; i++) {
99
            level = block[i];
100
            if (level) {
101
                    if (level < 0) {
102
                    level = level * qmul - qadd;
103
                } else {
104
                    level = level * qmul + qadd;
105
                }
106
                block[i] = level;
107
            }
108
        }
109
        nCoeffs=64;
110
    } else {
111
        i = 0;
112
        nCoeffs= zigzag_end[ s->block_last_index[n] ];
113
    }
114
//printf("%d %d  ", qmul, qadd);
115
asm volatile(
116
                "movd %1, %%mm6                        \n\t" //qmul
117
                "packssdw %%mm6, %%mm6                \n\t"
118
                "packssdw %%mm6, %%mm6                \n\t"
119
                "movd %2, %%mm5                        \n\t" //qadd
120
                "pxor %%mm7, %%mm7                \n\t"
121
                "packssdw %%mm5, %%mm5                \n\t"
122
                "packssdw %%mm5, %%mm5                \n\t"
123
                "psubw %%mm5, %%mm7                \n\t"
124
                "pxor %%mm4, %%mm4                \n\t"
125
                ".balign 16\n\t"
126
                "1:                                \n\t"
127
                "movq (%0, %3), %%mm0                \n\t"
128
                "movq 8(%0, %3), %%mm1                \n\t"
129

    
130
                "pmullw %%mm6, %%mm0                \n\t"
131
                "pmullw %%mm6, %%mm1                \n\t"
132

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

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

    
139
                "pxor %%mm2, %%mm0                \n\t"
140
                "pxor %%mm3, %%mm1                \n\t"
141

    
142
                "paddw %%mm7, %%mm0                \n\t"
143
                "paddw %%mm7, %%mm1                \n\t"
144

    
145
                "pxor %%mm0, %%mm2                \n\t"
146
                "pxor %%mm1, %%mm3                \n\t"
147

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

    
151
                "pandn %%mm2, %%mm0                \n\t"
152
                "pandn %%mm3, %%mm1                \n\t"
153

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

    
157
                "addl $16, %3                        \n\t"
158
                "js 1b                                \n\t"
159
                ::"r" (block+nCoeffs), "g"(qmul), "g" (qadd), "r" (2*(i-nCoeffs))
160
                : "memory"
161
        );
162
}
163

    
164

    
165
/*
166
  NK:
167
  Note: looking at PARANOID:
168
  "enable all paranoid tests for rounding, overflows, etc..."
169

170
#ifdef PARANOID
171
                if (level < -2048 || level > 2047)
172
                    fprintf(stderr, "unquant error %d %d\n", i, level);
173
#endif
174
  We can suppose that result of two multiplications can't be greate of 0xFFFF
175
  i.e. is 16-bit, so we use here only PMULLW instruction and can avoid
176
  a complex multiplication.
177
=====================================================
178
 Full formula for multiplication of 2 integer numbers
179
 which are represent as high:low words:
180
 input: value1 = high1:low1
181
        value2 = high2:low2
182
 output: value3 = value1*value2
183
 value3=high3:low3 (on overflow: modulus 2^32 wrap-around)
184
 this mean that for 0x123456 * 0x123456 correct result is 0x766cb0ce4
185
 but this algorithm will compute only 0x66cb0ce4
186
 this limited by 16-bit size of operands
187
 ---------------------------------
188
 tlow1 = high1*low2
189
 tlow2 = high2*low1
190
 tlow1 = tlow1 + tlow2
191
 high3:low3 = low1*low2
192
 high3 += tlow1
193
*/
194
static void dct_unquantize_mpeg1_mmx(MpegEncContext *s,
195
                                     DCTELEM *block, int n, int qscale)
196
{
197
    int i, level, nCoeffs;
198
    const UINT16 *quant_matrix;
199
    
200
    if(s->alternate_scan) nCoeffs= 64;
201
    else nCoeffs= nCoeffs= zigzag_end[ s->block_last_index[n] ];
202

    
203
    if (s->mb_intra) {
204
        if (n < 4) 
205
            block[0] = block[0] * s->y_dc_scale;
206
        else
207
            block[0] = block[0] * s->c_dc_scale;
208
        /* isnt used anymore (we have a h263 unquantizer since some time)
209
        if (s->out_format == FMT_H263) {
210
            i = 1;
211
            goto unquant_even;
212
        }*/
213
        /* XXX: only mpeg1 */
214
        quant_matrix = s->intra_matrix;
215
        i=1;
216
        /* Align on 4 elements boundary */
217
        while(i&3)
218
        {
219
            level = block[i];
220
            if (level) {
221
                if (level < 0) level = -level;
222
                    level = (int)(level * qscale * quant_matrix[i]) >> 3;
223
                    level = (level - 1) | 1;
224
                if (block[i] < 0) level = -level;
225
                block[i] = level;
226
            }
227
            i++;
228
        }
229
        __asm __volatile(
230
        "movd        %0, %%mm6\n\t"       /* mm6 = qscale | 0  */
231
        "punpckldq %%mm6, %%mm6\n\t" /* mm6 = qscale | qscale */
232
        "movq        %2, %%mm4\n\t"
233
        "movq        %%mm6, %%mm7\n\t"
234
        "movq        %1, %%mm5\n\t"
235
        "packssdw %%mm6, %%mm7\n\t" /* mm7 = qscale | qscale | qscale | qscale */
236
        "pxor        %%mm6, %%mm6\n\t"
237
        ::"g"(qscale),"m"(mm_wone),"m"(mm_wabs):"memory");
238
        for(;i<nCoeffs;i+=4) {
239
                __asm __volatile(
240
                        "movq        %1, %%mm0\n\t"
241
                        "movq        %%mm7, %%mm1\n\t"
242
                        "movq        %%mm0, %%mm2\n\t"
243
                        "movq        %%mm0, %%mm3\n\t"
244
                        "pcmpgtw %%mm6, %%mm2\n\t"
245
                        "pmullw        %2, %%mm1\n\t"
246
                        "pandn        %%mm4, %%mm2\n\t"
247
                        "por        %%mm5, %%mm2\n\t"
248
                        "pmullw        %%mm2, %%mm0\n\t" /* mm0 = abs(block[i]). */
249

    
250
                        "pcmpeqw %%mm6, %%mm3\n\t"
251
                        "pmullw        %%mm0, %%mm1\n\t"
252
                        "psraw        $3, %%mm1\n\t"
253
                        "psubw        %%mm5, %%mm1\n\t"   /* block[i] --; */
254
                        "pandn        %%mm4, %%mm3\n\t"  /* fake of pcmpneqw : mm0 != 0 then mm1 = -1 */
255
                        "por        %%mm5, %%mm1\n\t"   /* block[i] |= 1 */
256
                        "pmullw %%mm2, %%mm1\n\t"   /* change signs again */
257

    
258
                        "pand        %%mm3, %%mm1\n\t" /* nullify if was zero */
259
                        "movq        %%mm1, %0"
260
                        :"=m"(block[i])
261
                        :"m"(block[i]), "m"(quant_matrix[i])
262
                        :"memory");
263
        }
264
    } else {
265
        i = 0;
266
//    unquant_even:
267
        quant_matrix = s->non_intra_matrix;
268
        /* Align on 4 elements boundary */
269
        while(i&7)
270
        {
271
            level = block[i];
272
            if (level) {
273
                if (level < 0) level = -level;
274
                    level = (((level << 1) + 1) * qscale *
275
                             ((int) quant_matrix[i])) >> 4;
276
                    level = (level - 1) | 1;
277
                if(block[i] < 0) level = -level;
278
                block[i] = level;
279
            }
280
            i++;
281
        }
282
asm volatile(
283
                "pcmpeqw %%mm7, %%mm7                \n\t"
284
                "psrlw $15, %%mm7                \n\t"
285
                "movd %2, %%mm6                        \n\t"
286
                "packssdw %%mm6, %%mm6                \n\t"
287
                "packssdw %%mm6, %%mm6                \n\t"
288
                ".balign 16\n\t"
289
                "1:                                \n\t"
290
                "movq (%0, %3), %%mm0                \n\t"
291
                "movq 8(%0, %3), %%mm1                \n\t"
292
                "movq (%1, %3), %%mm4                \n\t"
293
                "movq 8(%1, %3), %%mm5                \n\t"
294
                "pmullw %%mm6, %%mm4                \n\t" // q=qscale*quant_matrix[i]
295
                "pmullw %%mm6, %%mm5                \n\t" // q=qscale*quant_matrix[i]
296
                "pxor %%mm2, %%mm2                \n\t"
297
                "pxor %%mm3, %%mm3                \n\t"
298
                "pcmpgtw %%mm0, %%mm2                \n\t" // block[i] < 0 ? -1 : 0
299
                "pcmpgtw %%mm1, %%mm3                \n\t" // block[i] < 0 ? -1 : 0
300
                "pxor %%mm2, %%mm0                \n\t"
301
                "pxor %%mm3, %%mm1                \n\t"
302
                "psubw %%mm2, %%mm0                \n\t" // abs(block[i])
303
                "psubw %%mm3, %%mm1                \n\t" // abs(block[i])
304
                "paddw %%mm0, %%mm0                \n\t" // abs(block[i])*2
305
                "paddw %%mm1, %%mm1                \n\t" // abs(block[i])*2
306
                "paddw %%mm7, %%mm0                \n\t" // abs(block[i])*2 + 1
307
                "paddw %%mm7, %%mm1                \n\t" // abs(block[i])*2 + 1
308
                "pmullw %%mm4, %%mm0                \n\t" // (abs(block[i])*2 + 1)*q
309
                "pmullw %%mm5, %%mm1                \n\t" // (abs(block[i])*2 + 1)*q
310
                "pxor %%mm4, %%mm4                \n\t"
311
                "pxor %%mm5, %%mm5                \n\t" // FIXME slow
312
                "pcmpeqw (%0, %3), %%mm4        \n\t" // block[i] == 0 ? -1 : 0
313
                "pcmpeqw 8(%0, %3), %%mm5        \n\t" // block[i] == 0 ? -1 : 0
314
                "psraw $4, %%mm0                \n\t"
315
                "psraw $4, %%mm1                \n\t"
316
                "psubw %%mm7, %%mm0                \n\t"
317
                "psubw %%mm7, %%mm1                \n\t"
318
                "por %%mm7, %%mm0                \n\t"
319
                "por %%mm7, %%mm1                \n\t"
320
                "pxor %%mm2, %%mm0                \n\t"
321
                "pxor %%mm3, %%mm1                \n\t"
322
                "psubw %%mm2, %%mm0                \n\t"
323
                "psubw %%mm3, %%mm1                \n\t"
324
                "pandn %%mm0, %%mm4                \n\t"
325
                "pandn %%mm1, %%mm5                \n\t"
326
                "movq %%mm4, (%0, %3)                \n\t"
327
                "movq %%mm5, 8(%0, %3)                \n\t"
328

    
329
                "addl $16, %3                        \n\t"
330
                "js 1b                                \n\t"
331
                ::"r" (block+nCoeffs), "r"(quant_matrix+nCoeffs), "g" (qscale), "r" (2*(i-nCoeffs))
332
                : "memory"
333
        );
334
    }
335
}
336

    
337
/* draw the edges of width 'w' of an image of size width, height 
338
   this mmx version can only handle w==8 || w==16 */
339
static void draw_edges_mmx(UINT8 *buf, int wrap, int width, int height, int w)
340
{
341
    UINT8 *ptr, *last_line;
342
    int i;
343

    
344
    last_line = buf + (height - 1) * wrap;
345
    /* left and right */
346
    ptr = buf;
347
    if(w==8)
348
    {
349
        asm volatile(
350
                "1:                                \n\t"
351
                "movd (%0), %%mm0                \n\t"
352
                "punpcklbw %%mm0, %%mm0                \n\t" 
353
                "punpcklwd %%mm0, %%mm0                \n\t"
354
                "punpckldq %%mm0, %%mm0                \n\t"
355
                "movq %%mm0, -8(%0)                \n\t"
356
                "movq -8(%0, %2), %%mm1                \n\t"
357
                "punpckhbw %%mm1, %%mm1                \n\t"
358
                "punpckhwd %%mm1, %%mm1                \n\t"
359
                "punpckhdq %%mm1, %%mm1                \n\t"
360
                "movq %%mm1, (%0, %2)                \n\t"
361
                "addl %1, %0                        \n\t"
362
                "cmpl %3, %0                        \n\t"
363
                " jb 1b                                \n\t"
364
                : "+r" (ptr)
365
                : "r" (wrap), "r" (width), "r" (ptr + wrap*height)
366
        );
367
    }
368
    else
369
    {
370
        asm volatile(
371
                "1:                                \n\t"
372
                "movd (%0), %%mm0                \n\t"
373
                "punpcklbw %%mm0, %%mm0                \n\t" 
374
                "punpcklwd %%mm0, %%mm0                \n\t"
375
                "punpckldq %%mm0, %%mm0                \n\t"
376
                "movq %%mm0, -8(%0)                \n\t"
377
                "movq %%mm0, -16(%0)                \n\t"
378
                "movq -8(%0, %2), %%mm1                \n\t"
379
                "punpckhbw %%mm1, %%mm1                \n\t"
380
                "punpckhwd %%mm1, %%mm1                \n\t"
381
                "punpckhdq %%mm1, %%mm1                \n\t"
382
                "movq %%mm1, (%0, %2)                \n\t"
383
                "movq %%mm1, 8(%0, %2)                \n\t"
384
                "addl %1, %0                        \n\t"
385
                "cmpl %3, %0                        \n\t"
386
                " jb 1b                                \n\t"                
387
                : "+r" (ptr)
388
                : "r" (wrap), "r" (width), "r" (ptr + wrap*height)
389
        );
390
    }
391
    
392
    for(i=0;i<w;i+=4) {
393
        /* top and bottom (and hopefully also the corners) */
394
        ptr= buf - (i + 1) * wrap - w;
395
        asm volatile(
396
                "1:                                \n\t"
397
                "movq (%1, %0), %%mm0                \n\t"
398
                "movq %%mm0, (%0)                \n\t"
399
                "movq %%mm0, (%0, %2)                \n\t"
400
                "movq %%mm0, (%0, %2, 2)        \n\t"
401
                "movq %%mm0, (%0, %3)                \n\t"
402
                "addl $8, %0                        \n\t"
403
                "cmpl %4, %0                        \n\t"
404
                " jb 1b                                \n\t"
405
                : "+r" (ptr)
406
                : "r" ((int)buf - (int)ptr - w), "r" (-wrap), "r" (-wrap*3), "r" (ptr+width+2*w)
407
        );
408
        ptr= last_line + (i + 1) * wrap - w;
409
        asm volatile(
410
                "1:                                \n\t"
411
                "movq (%1, %0), %%mm0                \n\t"
412
                "movq %%mm0, (%0)                \n\t"
413
                "movq %%mm0, (%0, %2)                \n\t"
414
                "movq %%mm0, (%0, %2, 2)        \n\t"
415
                "movq %%mm0, (%0, %3)                \n\t"
416
                "addl $8, %0                        \n\t"
417
                "cmpl %4, %0                        \n\t"
418
                " jb 1b                                \n\t"
419
                : "+r" (ptr)
420
                : "r" ((int)last_line - (int)ptr - w), "r" (wrap), "r" (wrap*3), "r" (ptr+width+2*w)
421
        );
422
    }
423
}
424

    
425
static volatile int esp_temp;
426

    
427
void unused_var_warning_killer(){
428
        esp_temp++;
429
}
430

    
431
#undef HAVE_MMX2
432
#define RENAME(a) a ## _MMX
433
#include "mpegvideo_mmx_template.c"
434

    
435
#define HAVE_MMX2
436
#undef RENAME
437
#define RENAME(a) a ## _MMX2
438
#include "mpegvideo_mmx_template.c"
439

    
440
void MPV_common_init_mmx(MpegEncContext *s)
441
{
442
    if (mm_flags & MM_MMX) {
443
               s->dct_unquantize_h263 = dct_unquantize_h263_mmx;
444
               s->dct_unquantize_mpeg = dct_unquantize_mpeg1_mmx;
445

    
446
        draw_edges = draw_edges_mmx;
447

    
448
        if(mm_flags & MM_MMXEXT){
449
                dct_quantize= dct_quantize_MMX2;
450
        }else{
451
                dct_quantize= dct_quantize_MMX;
452
        }
453
    }
454
}