mpegvideo_altivec.c 4.17 KB
Newer Older
1 2 3
/*
 * Copyright (c) 2002 Dieter Shirley
 *
4 5 6
 * dct_unquantize_h263_altivec:
 * Copyright (c) 2003 Romain Dolbeau <romain@dolbeau.org>
 *
7 8 9
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
10 11
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
12
 * version 2.1 of the License, or (at your option) any later version.
13
 *
14
 * FFmpeg is distributed in the hope that it will be useful,
15 16 17 18 19
 * 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
20
 * License along with FFmpeg; if not, write to the Free Software
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22
 */
23

24 25
#include <stdlib.h>
#include <stdio.h>
26

27
#include "config.h"
28

29
#include "libavutil/attributes.h"
30
#include "libavutil/cpu.h"
31
#include "libavutil/ppc/cpu.h"
32
#include "libavutil/ppc/util_altivec.h"
33

34
#include "libavcodec/mpegvideo.h"
35

36 37
#if HAVE_ALTIVEC

38 39
/* AltiVec version of dct_unquantize_h263
   this code assumes `block' is 16 bytes-aligned */
40
static void dct_unquantize_h263_altivec(MpegEncContext *s,
Diego Biurrun's avatar
Diego Biurrun committed
41
                                 int16_t *block, int n, int qscale)
42 43 44
{
    int i, level, qmul, qadd;
    int nCoeffs;
45

46 47
    qadd = (qscale - 1) | 1;
    qmul = qscale << 1;
48

49 50
    if (s->mb_intra) {
        if (!s->h263_aic) {
51
            if (n < 4)
52 53 54 55 56 57
                block[0] = block[0] * s->y_dc_scale;
            else
                block[0] = block[0] * s->c_dc_scale;
        }else
            qadd = 0;
        i = 1;
Diego Biurrun's avatar
Diego Biurrun committed
58
        nCoeffs= 63; //does not always use zigzag table
59 60
    } else {
        i = 0;
61
        av_assert2(s->block_last_index[n]>=0);
62 63 64 65
        nCoeffs= s->intra_scantable.raster_end[ s->block_last_index[n] ];
    }

    {
66
        register const vector signed short vczero = (const vector signed short)vec_splat_s16(0);
67 68
        DECLARE_ALIGNED(16, short, qmul8) = qmul;
        DECLARE_ALIGNED(16, short, qadd8) = qadd;
69 70 71 72 73
        register vector signed short blockv, qmulv, qaddv, nqaddv, temp1;
        register vector bool short blockv_null, blockv_neg;
        register short backup_0 = block[0];
        register int j = 0;

74 75 76
        qmulv = vec_splat((vec_s16)vec_lde(0, &qmul8), 0);
        qaddv = vec_splat((vec_s16)vec_lde(0, &qadd8), 0);
        nqaddv = vec_sub(vczero, qaddv);
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105

        // vectorize all the 16 bytes-aligned blocks
        // of 8 elements
        for(; (j + 7) <= nCoeffs ; j+=8) {
            blockv = vec_ld(j << 1, block);
            blockv_neg = vec_cmplt(blockv, vczero);
            blockv_null = vec_cmpeq(blockv, vczero);
            // choose between +qadd or -qadd as the third operand
            temp1 = vec_sel(qaddv, nqaddv, blockv_neg);
            // multiply & add (block{i,i+7} * qmul [+-] qadd)
            temp1 = vec_mladd(blockv, qmulv, temp1);
            // put 0 where block[{i,i+7} used to have 0
            blockv = vec_sel(temp1, blockv, blockv_null);
            vec_st(blockv, j << 1, block);
        }

        // if nCoeffs isn't a multiple of 8, finish the job
        // using good old scalar units.
        // (we could do it using a truncated vector,
        // but I'm not sure it's worth the hassle)
        for(; j <= nCoeffs ; j++) {
            level = block[j];
            if (level) {
                if (level < 0) {
                    level = level * qmul - qadd;
                } else {
                    level = level * qmul + qadd;
                }
                block[j] = level;
106 107
            }
        }
108

109 110 111 112
        if (i == 1) {
            // cheat. this avoid special-casing the first iteration
            block[0] = backup_0;
        }
113 114
    }
}
115

116
#endif /* HAVE_ALTIVEC */
117

118
av_cold void ff_mpv_common_init_ppc(MpegEncContext *s)
119
{
120
#if HAVE_ALTIVEC
121
    if (!PPC_ALTIVEC(av_get_cpu_flags()))
122
        return;
123

124
    if ((s->avctx->dct_algo == FF_DCT_AUTO) ||
125
        (s->avctx->dct_algo == FF_DCT_ALTIVEC)) {
126 127 128
        s->dct_unquantize_h263_intra = dct_unquantize_h263_altivec;
        s->dct_unquantize_h263_inter = dct_unquantize_h263_altivec;
    }
129
#endif /* HAVE_ALTIVEC */
130
}