mpegvideoenc_template.c 17.3 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
#include <stdint.h>

#include "libavutil/internal.h"
#include "libavutil/x86/asm.h"
27
#include "libavcodec/mpegutils.h"
28
#include "libavcodec/mpegvideo.h"
29
#include "fdct.h"
30

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

40
#if COMPILE_TEMPLATE_SSE2
41 42 43 44 45 46 47
#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"
48
#define PMAX(a,b) \
49
            "movhlps "a", "b"           \n\t"\
50
            PMAXW(b, a)\
51 52 53 54 55 56 57 58
            "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"
59
#if COMPILE_TEMPLATE_MMXEXT
60 61 62 63 64 65
#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"\
66
            PMAXW(b, a)
67 68
#else
#define SPREADW(a) \
69 70
            "punpcklwd "a", "a"         \n\t"\
            "punpcklwd "a", "a"         \n\t"
71
#define PMAXW(a,b) \
72 73
            "psubusw "a", "b"           \n\t"\
            "paddw "a", "b"             \n\t"
74
#define PMAX(a,b)  \
75 76
            "movq "a", "b"              \n\t"\
            "psrlq $32, "a"             \n\t"\
77
            PMAXW(b, a)\
78 79
            "movq "a", "b"              \n\t"\
            "psrlq $16, "a"             \n\t"\
80
            PMAXW(b, a)
81

82
#endif
83 84
#endif

85
#if COMPILE_TEMPLATE_SSSE3
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
#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
101 102

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

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

113
    //s->fdct (block);
114
    RENAME_FDCT(ff_fdct)(block); // cannot be anything else ...
115

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

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

141
        block[0]=0; //avoid fake overflow
142 143 144 145
//        temp_block[0] = (block[0] + (q >> 1)) / q;
        last_non_zero_p1 = 1;
    } else {
        last_non_zero_p1 = 0;
146 147
        bias = s->q_inter_matrix16[qscale][1];
        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 154 155 156 157 158 159
            "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]
160
            "mov $-128, %%"REG_a"               \n\t"
161
            ".p2align 4                         \n\t"
162
            "1:                                 \n\t"
163 164 165 166 167 168 169 170 171 172 173 174 175
            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"
176
            " js 1b                             \n\t"
177 178
            PMAX(MM"3", MM"0")
            "movd "MM"3, %%"REG_a"              \n\t"
179
            "movzbl %%al, %%eax                 \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 184
              XMM_CLOBBERS_ONLY("%xmm0", "%xmm1", "%xmm2", "%xmm3",
                                "%xmm4", "%xmm5", "%xmm6", "%xmm7")
185
        );
186
    }else{ // FMT_H263
187
        __asm__ volatile(
188 189 190 191
            "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
192
            "mov $-128, %%"REG_a"               \n\t"
193
            ".p2align 4                         \n\t"
194
            "1:                                 \n\t"
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
            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"
210
            " js 1b                             \n\t"
211 212
            PMAX(MM"3", MM"0")
            "movd "MM"3, %%"REG_a"              \n\t"
213
            "movzbl %%al, %%eax                 \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 218
              XMM_CLOBBERS_ONLY("%xmm0", "%xmm1", "%xmm2", "%xmm3",
                                "%xmm4", "%xmm5", "%xmm6", "%xmm7")
219
        );
220
    }
221
    __asm__ volatile(
222 223 224 225
        "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"
226
#if COMPILE_TEMPLATE_SSE2
227
        "packsswb "MM"4, "MM"4              \n\t"
228 229
#endif
        "movd "MM"4, %0                     \n\t" // *overflow
230 231
        : "=g" (*overflow)
        : "g" (s->max_qcoeff)
232
    );
233

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

237
    if (s->idsp.perm_type == FF_IDCT_PERM_SIMPLE) {
238
        if(last_non_zero_p1 <= 1) goto end;
239 240
        block[0x08] = temp_block[0x01]; block[0x10] = temp_block[0x08];
        block[0x20] = temp_block[0x10];
241
        if(last_non_zero_p1 <= 4) goto end;
242 243
        block[0x18] = temp_block[0x09]; block[0x04] = temp_block[0x02];
        block[0x09] = temp_block[0x03];
244
        if(last_non_zero_p1 <= 7) goto end;
245 246
        block[0x14] = temp_block[0x0A]; block[0x28] = temp_block[0x11];
        block[0x12] = temp_block[0x18]; block[0x02] = temp_block[0x20];
247
        if(last_non_zero_p1 <= 11) goto end;
248 249 250
        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];
251
        if(last_non_zero_p1 <= 16) goto end;
252 253 254 255
        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];
256
        if(last_non_zero_p1 <= 24) goto end;
257 258 259 260
        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];
261
        if(last_non_zero_p1 <= 32) goto end;
262 263 264 265
        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];
266
        if(last_non_zero_p1 <= 40) goto end;
267 268 269 270
        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];
271
        if(last_non_zero_p1 <= 48) goto end;
272 273 274 275
        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];
276
        if(last_non_zero_p1 <= 56) goto end;
277 278 279
        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];
280
        block[0x37] = temp_block[0x3E]; block[0x3F] = temp_block[0x3F];
281
    }else if(s->idsp.perm_type == FF_IDCT_PERM_LIBMPEG2){
282
        if(last_non_zero_p1 <= 1) goto end;
283 284
        block[0x04] = temp_block[0x01];
        block[0x08] = temp_block[0x08]; block[0x10] = temp_block[0x10];
285
        if(last_non_zero_p1 <= 4) goto end;
286 287
        block[0x0C] = temp_block[0x09]; block[0x01] = temp_block[0x02];
        block[0x05] = temp_block[0x03];
288
        if(last_non_zero_p1 <= 7) goto end;
289 290
        block[0x09] = temp_block[0x0A]; block[0x14] = temp_block[0x11];
        block[0x18] = temp_block[0x18]; block[0x20] = temp_block[0x20];
291
        if(last_non_zero_p1 <= 11) goto end;
292 293 294
        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];
295
        if(last_non_zero_p1 <= 16) goto end;
296 297 298 299
        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];
300
        if(last_non_zero_p1 <= 24) goto end;
301 302 303 304
        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];
305
        if(last_non_zero_p1 <= 32) goto end;
306 307 308 309
        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];
310
        if(last_non_zero_p1 <= 40) goto end;
311 312 313 314
        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];
315
        if(last_non_zero_p1 <= 48) goto end;
316 317 318 319
        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];
320
        if(last_non_zero_p1 <= 56) goto end;
321 322 323
        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];
324 325 326
        block[0x3B] = temp_block[0x3E]; block[0x3F] = temp_block[0x3F];
    }else{
        if(last_non_zero_p1 <= 1) goto end;
327 328
        block[0x01] = temp_block[0x01];
        block[0x08] = temp_block[0x08]; block[0x10] = temp_block[0x10];
329
        if(last_non_zero_p1 <= 4) goto end;
330 331
        block[0x09] = temp_block[0x09]; block[0x02] = temp_block[0x02];
        block[0x03] = temp_block[0x03];
332
        if(last_non_zero_p1 <= 7) goto end;
333 334
        block[0x0A] = temp_block[0x0A]; block[0x11] = temp_block[0x11];
        block[0x18] = temp_block[0x18]; block[0x20] = temp_block[0x20];
335
        if(last_non_zero_p1 <= 11) goto end;
336 337 338
        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];
339
        if(last_non_zero_p1 <= 16) goto end;
340 341 342 343
        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];
344
        if(last_non_zero_p1 <= 24) goto end;
345 346 347 348
        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];
349
        if(last_non_zero_p1 <= 32) goto end;
350 351 352 353
        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];
354
        if(last_non_zero_p1 <= 40) goto end;
355 356 357 358
        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];
359
        if(last_non_zero_p1 <= 48) goto end;
360 361 362 363
        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];
364
        if(last_non_zero_p1 <= 56) goto end;
365 366 367
        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];
368 369 370
        block[0x3E] = temp_block[0x3E]; block[0x3F] = temp_block[0x3F];
    }
    end:
371 372
    return last_non_zero_p1 - 1;
}