Statistics
| Branch: | Revision:

ffmpeg / libavcodec / i386 / mpegvideo_mmx.c @ badaf88e

History | View | Annotate | Download (9.7 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

    
26
extern UINT8 zigzag_end[64];
27

    
28
#if 0
29

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

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

    
68
static const unsigned long long int mm_wabs __attribute__ ((aligned(8))) = 0xffffffffffffffffULL;
69
static const unsigned long long int mm_wone __attribute__ ((aligned(8))) = 0x0001000100010001ULL;
70

    
71

    
72
static void dct_unquantize_h263_mmx(MpegEncContext *s,
73
                                  DCTELEM *block, int n, int qscale)
74
{
75
    int i, level, qmul, qadd, nCoeffs;
76
    
77
    qmul = s->qscale << 1;
78
    qadd = (s->qscale - 1) | 1;
79

    
80
    if (s->mb_intra) {
81
        if (n < 4)
82
            block[0] = block[0] * s->y_dc_scale;
83
        else
84
            block[0] = block[0] * s->c_dc_scale;
85

    
86
        for(i=1; i<8; i++) {
87
                level = block[i];
88
                if (level) {
89
                        if (level < 0) {
90
                                level = level * qmul - qadd;
91
                        } else {
92
                                level = level * qmul + qadd;
93
                        }
94
                        block[i] = level;
95
                }
96
        }
97
        nCoeffs=64;
98
    } else {
99
        i = 0;
100
        nCoeffs= zigzag_end[ s->block_last_index[n] ];
101
    }
102
//printf("%d %d  ", qmul, qadd);
103
asm volatile(
104
                "movd %1, %%mm6                        \n\t" //qmul
105
                "packssdw %%mm6, %%mm6                \n\t"
106
                "packssdw %%mm6, %%mm6                \n\t"
107
                "movd %2, %%mm5                        \n\t" //qadd
108
                "pxor %%mm7, %%mm7                \n\t"
109
                "packssdw %%mm5, %%mm5                \n\t"
110
                "packssdw %%mm5, %%mm5                \n\t"
111
                "psubw %%mm5, %%mm7                \n\t"
112
                "pxor %%mm4, %%mm4                \n\t"
113
                ".balign 16\n\t"
114
                "1:                                \n\t"
115
                "movq (%0, %3), %%mm0                \n\t"
116
                "movq 8(%0, %3), %%mm1                \n\t"
117

    
118
                "pmullw %%mm6, %%mm0                \n\t"
119
                "pmullw %%mm6, %%mm1                \n\t"
120

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

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

    
127
                "pxor %%mm2, %%mm0                \n\t"
128
                "pxor %%mm3, %%mm1                \n\t"
129

    
130
                "paddw %%mm7, %%mm0                \n\t"
131
                "paddw %%mm7, %%mm1                \n\t"
132

    
133
                "pxor %%mm0, %%mm2                \n\t"
134
                "pxor %%mm1, %%mm3                \n\t"
135

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

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

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

    
145
                "addl $16, %3                        \n\t"
146
                "js 1b                                \n\t"
147
                ::"r" (block+nCoeffs), "g"(qmul), "g" (qadd), "r" (2*(i-nCoeffs))
148
                : "memory"
149
        );
150
}
151

    
152

    
153
/*
154
  NK:
155
  Note: looking at PARANOID:
156
  "enable all paranoid tests for rounding, overflows, etc..."
157

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

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

    
238
                        "pcmpeqw %%mm6, %%mm3\n\t"
239
                        "pmullw        %%mm0, %%mm1\n\t"
240
                        "psraw        $3, %%mm1\n\t"
241
                        "psubw        %%mm5, %%mm1\n\t"   /* block[i] --; */
242
                        "pandn        %%mm4, %%mm3\n\t"  /* fake of pcmpneqw : mm0 != 0 then mm1 = -1 */
243
                        "por        %%mm5, %%mm1\n\t"   /* block[i] |= 1 */
244
                        "pmullw %%mm2, %%mm1\n\t"   /* change signs again */
245

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

    
317
                "addl $16, %3                        \n\t"
318
                "js 1b                                \n\t"
319
                ::"r" (block+nCoeffs), "r"(quant_matrix+nCoeffs), "g" (qscale), "r" (2*(i-nCoeffs))
320
                : "memory"
321
        );
322
    }
323
}
324

    
325
void MPV_common_init_mmx(MpegEncContext *s)
326
{
327
    if (mm_flags & MM_MMX) {
328
        if (s->out_format == FMT_H263)
329
                s->dct_unquantize = dct_unquantize_h263_mmx;
330
        else
331
                s->dct_unquantize = dct_unquantize_mpeg1_mmx;
332
    }
333
}