Statistics
| Branch: | Revision:

ffmpeg / libavcodec / x86 / mpegvideo_mmx_template.c @ 2912e87a

History | View | Annotate | Download (17.4 KB)

1
/*
2
 * MPEG video MMX templates
3
 *
4
 * Copyright (c) 2002 Michael Niedermayer <michaelni@gmx.at>
5
 *
6
 * This file is part of Libav.
7
 *
8
 * Libav is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public
10
 * License as published by the Free Software Foundation; either
11
 * version 2.1 of the License, or (at your option) any later version.
12
 *
13
 * Libav is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with Libav; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
 */
22

    
23
#undef MMREG_WIDTH
24
#undef MM
25
#undef MOVQ
26
#undef SPREADW
27
#undef PMAXW
28
#undef PMAX
29
#undef SAVE_SIGN
30
#undef RESTORE_SIGN
31

    
32
#if HAVE_SSE2
33
#define MMREG_WIDTH "16"
34
#define MM "%%xmm"
35
#define MOVQ "movdqa"
36
#define SPREADW(a) \
37
            "pshuflw $0, "a", "a"       \n\t"\
38
            "punpcklwd "a", "a"         \n\t"
39
#define PMAXW(a,b) "pmaxsw "a", "b"     \n\t"
40
#define PMAX(a,b) \
41
            "movhlps "a", "b"           \n\t"\
42
            PMAXW(b, a)\
43
            "pshuflw $0x0E, "a", "b"    \n\t"\
44
            PMAXW(b, a)\
45
            "pshuflw $0x01, "a", "b"    \n\t"\
46
            PMAXW(b, a)
47
#else
48
#define MMREG_WIDTH "8"
49
#define MM "%%mm"
50
#define MOVQ "movq"
51
#if HAVE_MMX2
52
#define SPREADW(a) "pshufw $0, "a", "a" \n\t"
53
#define PMAXW(a,b) "pmaxsw "a", "b"     \n\t"
54
#define PMAX(a,b) \
55
            "pshufw $0x0E, "a", "b"     \n\t"\
56
            PMAXW(b, a)\
57
            "pshufw $0x01, "a", "b"     \n\t"\
58
            PMAXW(b, a)
59
#else
60
#define SPREADW(a) \
61
            "punpcklwd "a", "a"         \n\t"\
62
            "punpcklwd "a", "a"         \n\t"
63
#define PMAXW(a,b) \
64
            "psubusw "a", "b"           \n\t"\
65
            "paddw "a", "b"             \n\t"
66
#define PMAX(a,b)  \
67
            "movq "a", "b"              \n\t"\
68
            "psrlq $32, "a"             \n\t"\
69
            PMAXW(b, a)\
70
            "movq "a", "b"              \n\t"\
71
            "psrlq $16, "a"             \n\t"\
72
            PMAXW(b, a)
73

    
74
#endif
75
#endif
76

    
77
#if HAVE_SSSE3
78
#define SAVE_SIGN(a,b) \
79
            "movdqa "b", "a"            \n\t"\
80
            "pabsw  "b", "b"            \n\t"
81
#define RESTORE_SIGN(a,b) \
82
            "psignw "a", "b"            \n\t"
83
#else
84
#define SAVE_SIGN(a,b) \
85
            "pxor "a", "a"              \n\t"\
86
            "pcmpgtw "b", "a"           \n\t" /* block[i] <= 0 ? 0xFF : 0x00 */\
87
            "pxor "a", "b"              \n\t"\
88
            "psubw "a", "b"             \n\t" /* ABS(block[i]) */
89
#define RESTORE_SIGN(a,b) \
90
            "pxor "a", "b"              \n\t"\
91
            "psubw "a", "b"             \n\t" // out=((ABS(block[i])*qmat[0] - bias[0]*qmat[0])>>16)*sign(block[i])
92
#endif
93

    
94
static int RENAME(dct_quantize)(MpegEncContext *s,
95
                            DCTELEM *block, int n,
96
                            int qscale, int *overflow)
97
{
98
    x86_reg last_non_zero_p1;
99
    int level=0, q; //=0 is because gcc says uninitialized ...
100
    const uint16_t *qmat, *bias;
101
    DECLARE_ALIGNED(16, int16_t, temp_block)[64];
102

    
103
    assert((7&(int)(&temp_block[0])) == 0); //did gcc align it correctly?
104

    
105
    //s->fdct (block);
106
    RENAMEl(ff_fdct) (block); //cannot be anything else ...
107

    
108
    if(s->dct_error_sum)
109
        s->denoise_dct(s, block);
110

    
111
    if (s->mb_intra) {
112
        int dummy;
113
        if (n < 4)
114
            q = s->y_dc_scale;
115
        else
116
            q = s->c_dc_scale;
117
        /* note: block[0] is assumed to be positive */
118
        if (!s->h263_aic) {
119
#if 1
120
        __asm__ volatile (
121
                "mul %%ecx                \n\t"
122
                : "=d" (level), "=a"(dummy)
123
                : "a" ((block[0]>>2) + q), "c" (ff_inverse[q<<1])
124
        );
125
#else
126
        __asm__ volatile (
127
                "xorl %%edx, %%edx        \n\t"
128
                "divw %%cx                \n\t"
129
                "movzwl %%ax, %%eax       \n\t"
130
                : "=a" (level)
131
                : "a" ((block[0]>>2) + q), "c" (q<<1)
132
                : "%edx"
133
        );
134
#endif
135
        } else
136
            /* For AIC we skip quant/dequant of INTRADC */
137
            level = (block[0] + 4)>>3;
138

    
139
        block[0]=0; //avoid fake overflow
140
//        temp_block[0] = (block[0] + (q >> 1)) / q;
141
        last_non_zero_p1 = 1;
142
        bias = s->q_intra_matrix16[qscale][1];
143
        qmat = s->q_intra_matrix16[qscale][0];
144
    } else {
145
        last_non_zero_p1 = 0;
146
        bias = s->q_inter_matrix16[qscale][1];
147
        qmat = s->q_inter_matrix16[qscale][0];
148
    }
149

    
150
    if((s->out_format == FMT_H263 || s->out_format == FMT_H261) && s->mpeg_quant==0){
151

    
152
        __asm__ volatile(
153
            "movd %%"REG_a", "MM"3              \n\t" // last_non_zero_p1
154
            SPREADW(MM"3")
155
            "pxor "MM"7, "MM"7                  \n\t" // 0
156
            "pxor "MM"4, "MM"4                  \n\t" // 0
157
            MOVQ" (%2), "MM"5                   \n\t" // qmat[0]
158
            "pxor "MM"6, "MM"6                  \n\t"
159
            "psubw (%3), "MM"6                  \n\t" // -bias[0]
160
            "mov $-128, %%"REG_a"               \n\t"
161
            ".p2align 4                         \n\t"
162
            "1:                                 \n\t"
163
            MOVQ" (%1, %%"REG_a"), "MM"0        \n\t" // block[i]
164
            SAVE_SIGN(MM"1", MM"0")                   // ABS(block[i])
165
            "psubusw "MM"6, "MM"0               \n\t" // ABS(block[i]) + bias[0]
166
            "pmulhw "MM"5, "MM"0                \n\t" // (ABS(block[i])*qmat[0] - bias[0]*qmat[0])>>16
167
            "por "MM"0, "MM"4                   \n\t"
168
            RESTORE_SIGN(MM"1", MM"0")                // out=((ABS(block[i])*qmat[0] - bias[0]*qmat[0])>>16)*sign(block[i])
169
            MOVQ" "MM"0, (%5, %%"REG_a")        \n\t"
170
            "pcmpeqw "MM"7, "MM"0               \n\t" // out==0 ? 0xFF : 0x00
171
            MOVQ" (%4, %%"REG_a"), "MM"1        \n\t"
172
            MOVQ" "MM"7, (%1, %%"REG_a")        \n\t" // 0
173
            "pandn "MM"1, "MM"0                 \n\t"
174
            PMAXW(MM"0", MM"3")
175
            "add $"MMREG_WIDTH", %%"REG_a"      \n\t"
176
            " js 1b                             \n\t"
177
            PMAX(MM"3", MM"0")
178
            "movd "MM"3, %%"REG_a"              \n\t"
179
            "movzb %%al, %%"REG_a"              \n\t" // last_non_zero_p1
180
            : "+a" (last_non_zero_p1)
181
            : "r" (block+64), "r" (qmat), "r" (bias),
182
              "r" (inv_zigzag_direct16+64), "r" (temp_block+64)
183
              XMM_CLOBBERS_ONLY("%xmm0", "%xmm1", "%xmm2", "%xmm3",
184
                                "%xmm4", "%xmm5", "%xmm6", "%xmm7")
185
        );
186
    }else{ // FMT_H263
187
        __asm__ volatile(
188
            "movd %%"REG_a", "MM"3              \n\t" // last_non_zero_p1
189
            SPREADW(MM"3")
190
            "pxor "MM"7, "MM"7                  \n\t" // 0
191
            "pxor "MM"4, "MM"4                  \n\t" // 0
192
            "mov $-128, %%"REG_a"               \n\t"
193
            ".p2align 4                         \n\t"
194
            "1:                                 \n\t"
195
            MOVQ" (%1, %%"REG_a"), "MM"0        \n\t" // block[i]
196
            SAVE_SIGN(MM"1", MM"0")                   // ABS(block[i])
197
            MOVQ" (%3, %%"REG_a"), "MM"6        \n\t" // bias[0]
198
            "paddusw "MM"6, "MM"0               \n\t" // ABS(block[i]) + bias[0]
199
            MOVQ" (%2, %%"REG_a"), "MM"5        \n\t" // qmat[i]
200
            "pmulhw "MM"5, "MM"0                \n\t" // (ABS(block[i])*qmat[0] + bias[0]*qmat[0])>>16
201
            "por "MM"0, "MM"4                   \n\t"
202
            RESTORE_SIGN(MM"1", MM"0")                // out=((ABS(block[i])*qmat[0] - bias[0]*qmat[0])>>16)*sign(block[i])
203
            MOVQ" "MM"0, (%5, %%"REG_a")        \n\t"
204
            "pcmpeqw "MM"7, "MM"0               \n\t" // out==0 ? 0xFF : 0x00
205
            MOVQ" (%4, %%"REG_a"), "MM"1        \n\t"
206
            MOVQ" "MM"7, (%1, %%"REG_a")        \n\t" // 0
207
            "pandn "MM"1, "MM"0                 \n\t"
208
            PMAXW(MM"0", MM"3")
209
            "add $"MMREG_WIDTH", %%"REG_a"      \n\t"
210
            " js 1b                             \n\t"
211
            PMAX(MM"3", MM"0")
212
            "movd "MM"3, %%"REG_a"              \n\t"
213
            "movzb %%al, %%"REG_a"              \n\t" // last_non_zero_p1
214
            : "+a" (last_non_zero_p1)
215
            : "r" (block+64), "r" (qmat+64), "r" (bias+64),
216
              "r" (inv_zigzag_direct16+64), "r" (temp_block+64)
217
              XMM_CLOBBERS_ONLY("%xmm0", "%xmm1", "%xmm2", "%xmm3",
218
                                "%xmm4", "%xmm5", "%xmm6", "%xmm7")
219
        );
220
    }
221
    __asm__ volatile(
222
        "movd %1, "MM"1                     \n\t" // max_qcoeff
223
        SPREADW(MM"1")
224
        "psubusw "MM"1, "MM"4               \n\t"
225
        "packuswb "MM"4, "MM"4              \n\t"
226
#if HAVE_SSE2
227
        "packuswb "MM"4, "MM"4              \n\t"
228
#endif
229
        "movd "MM"4, %0                     \n\t" // *overflow
230
        : "=g" (*overflow)
231
        : "g" (s->max_qcoeff)
232
    );
233

    
234
    if(s->mb_intra) block[0]= level;
235
    else            block[0]= temp_block[0];
236

    
237
    if(s->dsp.idct_permutation_type == FF_SIMPLE_IDCT_PERM){
238
        if(last_non_zero_p1 <= 1) goto end;
239
        block[0x08] = temp_block[0x01]; block[0x10] = temp_block[0x08];
240
        block[0x20] = temp_block[0x10];
241
        if(last_non_zero_p1 <= 4) goto end;
242
        block[0x18] = temp_block[0x09]; block[0x04] = temp_block[0x02];
243
        block[0x09] = temp_block[0x03];
244
        if(last_non_zero_p1 <= 7) goto end;
245
        block[0x14] = temp_block[0x0A]; block[0x28] = temp_block[0x11];
246
        block[0x12] = temp_block[0x18]; block[0x02] = temp_block[0x20];
247
        if(last_non_zero_p1 <= 11) goto end;
248
        block[0x1A] = temp_block[0x19]; block[0x24] = temp_block[0x12];
249
        block[0x19] = temp_block[0x0B]; block[0x01] = temp_block[0x04];
250
        block[0x0C] = temp_block[0x05];
251
        if(last_non_zero_p1 <= 16) goto end;
252
        block[0x11] = temp_block[0x0C]; block[0x29] = temp_block[0x13];
253
        block[0x16] = temp_block[0x1A]; block[0x0A] = temp_block[0x21];
254
        block[0x30] = temp_block[0x28]; block[0x22] = temp_block[0x30];
255
        block[0x38] = temp_block[0x29]; block[0x06] = temp_block[0x22];
256
        if(last_non_zero_p1 <= 24) goto end;
257
        block[0x1B] = temp_block[0x1B]; block[0x21] = temp_block[0x14];
258
        block[0x1C] = temp_block[0x0D]; block[0x05] = temp_block[0x06];
259
        block[0x0D] = temp_block[0x07]; block[0x15] = temp_block[0x0E];
260
        block[0x2C] = temp_block[0x15]; block[0x13] = temp_block[0x1C];
261
        if(last_non_zero_p1 <= 32) goto end;
262
        block[0x0B] = temp_block[0x23]; block[0x34] = temp_block[0x2A];
263
        block[0x2A] = temp_block[0x31]; block[0x32] = temp_block[0x38];
264
        block[0x3A] = temp_block[0x39]; block[0x26] = temp_block[0x32];
265
        block[0x39] = temp_block[0x2B]; block[0x03] = temp_block[0x24];
266
        if(last_non_zero_p1 <= 40) goto end;
267
        block[0x1E] = temp_block[0x1D]; block[0x25] = temp_block[0x16];
268
        block[0x1D] = temp_block[0x0F]; block[0x2D] = temp_block[0x17];
269
        block[0x17] = temp_block[0x1E]; block[0x0E] = temp_block[0x25];
270
        block[0x31] = temp_block[0x2C]; block[0x2B] = temp_block[0x33];
271
        if(last_non_zero_p1 <= 48) goto end;
272
        block[0x36] = temp_block[0x3A]; block[0x3B] = temp_block[0x3B];
273
        block[0x23] = temp_block[0x34]; block[0x3C] = temp_block[0x2D];
274
        block[0x07] = temp_block[0x26]; block[0x1F] = temp_block[0x1F];
275
        block[0x0F] = temp_block[0x27]; block[0x35] = temp_block[0x2E];
276
        if(last_non_zero_p1 <= 56) goto end;
277
        block[0x2E] = temp_block[0x35]; block[0x33] = temp_block[0x3C];
278
        block[0x3E] = temp_block[0x3D]; block[0x27] = temp_block[0x36];
279
        block[0x3D] = temp_block[0x2F]; block[0x2F] = temp_block[0x37];
280
        block[0x37] = temp_block[0x3E]; block[0x3F] = temp_block[0x3F];
281
    }else if(s->dsp.idct_permutation_type == FF_LIBMPEG2_IDCT_PERM){
282
        if(last_non_zero_p1 <= 1) goto end;
283
        block[0x04] = temp_block[0x01];
284
        block[0x08] = temp_block[0x08]; block[0x10] = temp_block[0x10];
285
        if(last_non_zero_p1 <= 4) goto end;
286
        block[0x0C] = temp_block[0x09]; block[0x01] = temp_block[0x02];
287
        block[0x05] = temp_block[0x03];
288
        if(last_non_zero_p1 <= 7) goto end;
289
        block[0x09] = temp_block[0x0A]; block[0x14] = temp_block[0x11];
290
        block[0x18] = temp_block[0x18]; block[0x20] = temp_block[0x20];
291
        if(last_non_zero_p1 <= 11) goto end;
292
        block[0x1C] = temp_block[0x19];
293
        block[0x11] = temp_block[0x12]; block[0x0D] = temp_block[0x0B];
294
        block[0x02] = temp_block[0x04]; block[0x06] = temp_block[0x05];
295
        if(last_non_zero_p1 <= 16) goto end;
296
        block[0x0A] = temp_block[0x0C]; block[0x15] = temp_block[0x13];
297
        block[0x19] = temp_block[0x1A]; block[0x24] = temp_block[0x21];
298
        block[0x28] = temp_block[0x28]; block[0x30] = temp_block[0x30];
299
        block[0x2C] = temp_block[0x29]; block[0x21] = temp_block[0x22];
300
        if(last_non_zero_p1 <= 24) goto end;
301
        block[0x1D] = temp_block[0x1B]; block[0x12] = temp_block[0x14];
302
        block[0x0E] = temp_block[0x0D]; block[0x03] = temp_block[0x06];
303
        block[0x07] = temp_block[0x07]; block[0x0B] = temp_block[0x0E];
304
        block[0x16] = temp_block[0x15]; block[0x1A] = temp_block[0x1C];
305
        if(last_non_zero_p1 <= 32) goto end;
306
        block[0x25] = temp_block[0x23]; block[0x29] = temp_block[0x2A];
307
        block[0x34] = temp_block[0x31]; block[0x38] = temp_block[0x38];
308
        block[0x3C] = temp_block[0x39]; block[0x31] = temp_block[0x32];
309
        block[0x2D] = temp_block[0x2B]; block[0x22] = temp_block[0x24];
310
        if(last_non_zero_p1 <= 40) goto end;
311
        block[0x1E] = temp_block[0x1D]; block[0x13] = temp_block[0x16];
312
        block[0x0F] = temp_block[0x0F]; block[0x17] = temp_block[0x17];
313
        block[0x1B] = temp_block[0x1E]; block[0x26] = temp_block[0x25];
314
        block[0x2A] = temp_block[0x2C]; block[0x35] = temp_block[0x33];
315
        if(last_non_zero_p1 <= 48) goto end;
316
        block[0x39] = temp_block[0x3A]; block[0x3D] = temp_block[0x3B];
317
        block[0x32] = temp_block[0x34]; block[0x2E] = temp_block[0x2D];
318
            block[0x23] = temp_block[0x26]; block[0x1F] = temp_block[0x1F];
319
        block[0x27] = temp_block[0x27]; block[0x2B] = temp_block[0x2E];
320
        if(last_non_zero_p1 <= 56) goto end;
321
        block[0x36] = temp_block[0x35]; block[0x3A] = temp_block[0x3C];
322
        block[0x3E] = temp_block[0x3D]; block[0x33] = temp_block[0x36];
323
        block[0x2F] = temp_block[0x2F]; block[0x37] = temp_block[0x37];
324
        block[0x3B] = temp_block[0x3E]; block[0x3F] = temp_block[0x3F];
325
    }else{
326
        if(last_non_zero_p1 <= 1) goto end;
327
        block[0x01] = temp_block[0x01];
328
        block[0x08] = temp_block[0x08]; block[0x10] = temp_block[0x10];
329
        if(last_non_zero_p1 <= 4) goto end;
330
        block[0x09] = temp_block[0x09]; block[0x02] = temp_block[0x02];
331
        block[0x03] = temp_block[0x03];
332
        if(last_non_zero_p1 <= 7) goto end;
333
        block[0x0A] = temp_block[0x0A]; block[0x11] = temp_block[0x11];
334
        block[0x18] = temp_block[0x18]; block[0x20] = temp_block[0x20];
335
        if(last_non_zero_p1 <= 11) goto end;
336
        block[0x19] = temp_block[0x19];
337
        block[0x12] = temp_block[0x12]; block[0x0B] = temp_block[0x0B];
338
        block[0x04] = temp_block[0x04]; block[0x05] = temp_block[0x05];
339
        if(last_non_zero_p1 <= 16) goto end;
340
        block[0x0C] = temp_block[0x0C]; block[0x13] = temp_block[0x13];
341
        block[0x1A] = temp_block[0x1A]; block[0x21] = temp_block[0x21];
342
        block[0x28] = temp_block[0x28]; block[0x30] = temp_block[0x30];
343
        block[0x29] = temp_block[0x29]; block[0x22] = temp_block[0x22];
344
        if(last_non_zero_p1 <= 24) goto end;
345
        block[0x1B] = temp_block[0x1B]; block[0x14] = temp_block[0x14];
346
        block[0x0D] = temp_block[0x0D]; block[0x06] = temp_block[0x06];
347
        block[0x07] = temp_block[0x07]; block[0x0E] = temp_block[0x0E];
348
        block[0x15] = temp_block[0x15]; block[0x1C] = temp_block[0x1C];
349
        if(last_non_zero_p1 <= 32) goto end;
350
        block[0x23] = temp_block[0x23]; block[0x2A] = temp_block[0x2A];
351
        block[0x31] = temp_block[0x31]; block[0x38] = temp_block[0x38];
352
        block[0x39] = temp_block[0x39]; block[0x32] = temp_block[0x32];
353
        block[0x2B] = temp_block[0x2B]; block[0x24] = temp_block[0x24];
354
        if(last_non_zero_p1 <= 40) goto end;
355
        block[0x1D] = temp_block[0x1D]; block[0x16] = temp_block[0x16];
356
        block[0x0F] = temp_block[0x0F]; block[0x17] = temp_block[0x17];
357
        block[0x1E] = temp_block[0x1E]; block[0x25] = temp_block[0x25];
358
        block[0x2C] = temp_block[0x2C]; block[0x33] = temp_block[0x33];
359
        if(last_non_zero_p1 <= 48) goto end;
360
        block[0x3A] = temp_block[0x3A]; block[0x3B] = temp_block[0x3B];
361
        block[0x34] = temp_block[0x34]; block[0x2D] = temp_block[0x2D];
362
        block[0x26] = temp_block[0x26]; block[0x1F] = temp_block[0x1F];
363
        block[0x27] = temp_block[0x27]; block[0x2E] = temp_block[0x2E];
364
        if(last_non_zero_p1 <= 56) goto end;
365
        block[0x35] = temp_block[0x35]; block[0x3C] = temp_block[0x3C];
366
        block[0x3D] = temp_block[0x3D]; block[0x36] = temp_block[0x36];
367
        block[0x2F] = temp_block[0x2F]; block[0x37] = temp_block[0x37];
368
        block[0x3E] = temp_block[0x3E]; block[0x3F] = temp_block[0x3F];
369
    }
370
    end:
371
/*
372
    for(i=0; i<last_non_zero_p1; i++)
373
    {
374
       int j= zigzag_direct_noperm[i];
375
       block[block_permute_op(j)]= temp_block[j];
376
    }
377
*/
378

    
379
    return last_non_zero_p1 - 1;
380
}