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

23 24 25 26 27 28
#include <stdint.h>

#include "libavutil/internal.h"
#include "libavutil/x86/asm.h"
#include "libavcodec/mpegvideo.h"

29 30 31
#undef MMREG_WIDTH
#undef MM
#undef MOVQ
32 33
#undef SPREADW
#undef PMAXW
Måns Rullgård's avatar
Måns Rullgård committed
34
#undef PMAX
35 36 37
#undef SAVE_SIGN
#undef RESTORE_SIGN

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

80
#endif
81 82
#endif

83
#if COMPILE_TEMPLATE_SSSE3
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
#define SAVE_SIGN(a,b) \
            "movdqa "b", "a"            \n\t"\
            "pabsw  "b", "b"            \n\t"
#define RESTORE_SIGN(a,b) \
            "psignw "a", "b"            \n\t"
#else
#define SAVE_SIGN(a,b) \
            "pxor "a", "a"              \n\t"\
            "pcmpgtw "b", "a"           \n\t" /* block[i] <= 0 ? 0xFF : 0x00 */\
            "pxor "a", "b"              \n\t"\
            "psubw "a", "b"             \n\t" /* ABS(block[i]) */
#define RESTORE_SIGN(a,b) \
            "pxor "a", "b"              \n\t"\
            "psubw "a", "b"             \n\t" // out=((ABS(block[i])*qmat[0] - bias[0]*qmat[0])>>16)*sign(block[i])
#endif
99 100

static int RENAME(dct_quantize)(MpegEncContext *s,
Diego Biurrun's avatar
Diego Biurrun committed
101
                            int16_t *block, int n,
102
                            int qscale, int *overflow)
103
{
104
    x86_reg last_non_zero_p1;
Diego Biurrun's avatar
Diego Biurrun committed
105
    int level=0, q; //=0 is because gcc says uninitialized ...
106
    const uint16_t *qmat, *bias;
107
    LOCAL_ALIGNED_16(int16_t, temp_block, [64]);
108

109
    av_assert2((7&(int)(&temp_block[0])) == 0); //did gcc align it correctly?
110

111
    //s->fdct (block);
Diego Biurrun's avatar
Diego Biurrun committed
112
    RENAMEl(ff_fdct) (block); //cannot be anything else ...
113

114
    if(s->dct_error_sum)
Michael Niedermayer's avatar
Michael Niedermayer committed
115
        s->denoise_dct(s, block);
116

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

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

148
    if((s->out_format == FMT_H263 || s->out_format == FMT_H261) && s->mpeg_quant==0){
149

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

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

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