summaryrefslogtreecommitdiff
path: root/vp8/encoder/dct.c
blob: 5207e39c4b5bfde449be356626ba6d51430ad3b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
 *  Copyright (c) 2010 The VP8 project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license and patent
 *  grant that can be found in the LICENSE file in the root of the source
 *  tree. All contributing project authors may be found in the AUTHORS
 *  file in the root of the source tree.
 */


#include <math.h>


static const short dct_matrix2[4][4] =
{
    { 23170,  30274,  23170, 12540 },
    { 23170,  12540, -23170, -30274 },
    { 23170, -12540, -23170, 30274 },
    { 23170, -30274,  23170, -12540 }
};

static const short dct_matrix1[4][4] =
{
    { 23170,  23170,  23170,  23170 },
    { 30274,  12540, -12540, -30274 },
    { 23170, -23170, -23170,  23170 },
    { 12540, -30274,  30274, -12540 }
};


#define _1STSTAGESHIFT           14
#define _1STSTAGEROUNDING        (1<<( _1STSTAGESHIFT-1))
#define _2NDSTAGESHIFT           16
#define _2NDSTAGEROUNDING        (1<<( _2NDSTAGESHIFT-1))

// using matrix multiply
void vp8_short_fdct4x4_c(short *input, short *output, int pitch)
{
    int i, j, k;
    short temp[4][4];
    int sumtemp;
    pitch >>= 1;

    for (i = 0; i < 4; i++)
    {
        for (j = 0; j < 4; j++)
        {
            sumtemp = 0;

            for (k = 0; k < 4; k++)
            {
                sumtemp += input[i*pitch+k] * dct_matrix2[k][j];

            }

            temp[i][j] = (short)((sumtemp + _1STSTAGEROUNDING) >> _1STSTAGESHIFT);
        }
    }


    for (i = 0; i < 4; i++)
    {
        for (j = 0; j < 4; j++)
        {
            sumtemp = 0;

            for (k = 0; k < 4; k++)
            {
                sumtemp += dct_matrix1[i][ k] * temp[k][ j];
            }

            output[i*4+j] = (short)((sumtemp + _2NDSTAGEROUNDING) >> _2NDSTAGESHIFT);
        }
    }

}


void vp8_short_fdct8x4_c(short *input, short *output, int pitch)
{
    vp8_short_fdct4x4_c(input,   output,    pitch);
    vp8_short_fdct4x4_c(input + 4, output + 16, pitch);
}


static const signed short x_c1 = 60547;
static const signed short x_c2 = 46341;
static const signed short x_c3 = 25080;

void vp8_fast_fdct4x4_c(short *input, short *output, int pitch)
{
    int i;
    int a1, b1, c1, d1;
    int a2, b2, c2, d2;
    short *ip = input;

    short *op = output;
    int temp1, temp2;

    for (i = 0; i < 4; i++)
    {
        a1 = (ip[0] + ip[3]) * 2;
        b1 = (ip[1] + ip[2]) * 2;
        c1 = (ip[1] - ip[2]) * 2;
        d1 = (ip[0] - ip[3]) * 2;

        temp1 = a1 + b1;
        temp2 = a1 - b1;

        op[0] = ((temp1 * x_c2) >> 16) + temp1;
        op[2] = ((temp2 * x_c2) >> 16) + temp2;

        temp1 = (c1 * x_c3) >> 16;
        temp2 = ((d1 * x_c1) >> 16) + d1;

        op[1] = temp1 + temp2;

        temp1 = (d1 * x_c3) >> 16;
        temp2 = ((c1 * x_c1) >> 16) + c1;

        op[3] = temp1 - temp2;

        ip += pitch / 2;
        op += 4;
    }

    ip = output;
    op = output;

    for (i = 0; i < 4; i++)
    {

        a1 = ip[0] + ip[12];
        b1 = ip[4] + ip[8];
        c1 = ip[4] - ip[8];
        d1 = ip[0] - ip[12];


        temp1 = a1 + b1;
        temp2 = a1 - b1;

        a2 = ((temp1 * x_c2) >> 16) + temp1;
        c2 = ((temp2 * x_c2) >> 16) + temp2;

        temp1 = (c1 * x_c3) >> 16;
        temp2 = ((d1 * x_c1) >> 16) + d1;

        b2 = temp1 + temp2;

        temp1 = (d1 * x_c3) >> 16;
        temp2 = ((c1 * x_c1) >> 16) + c1;

        d2 = temp1 - temp2;


        op[0]   = (a2 + 1) >> 1;
        op[4]   = (b2 + 1) >> 1;
        op[8]   = (c2 + 1) >> 1;
        op[12]  = (d2 + 1) >> 1;

        ip++;
        op++;
    }
}

void vp8_fast_fdct8x4_c(short *input, short *output, int pitch)
{
    vp8_fast_fdct4x4_c(input,   output,    pitch);
    vp8_fast_fdct4x4_c(input + 4, output + 16, pitch);
}

void vp8_short_walsh4x4_c(short *input, short *output, int pitch)
{
    int i;
    int a1, b1, c1, d1;
    int a2, b2, c2, d2;
    short *ip = input;
    short *op = output;

    for (i = 0; i < 4; i++)
    {
        a1 = ip[0] + ip[3];
        b1 = ip[1] + ip[2];
        c1 = ip[1] - ip[2];
        d1 = ip[0] - ip[3];

        op[0] = a1 + b1;
        op[1] = c1 + d1;
        op[2] = a1 - b1;
        op[3] = d1 - c1;
        ip += pitch / 2;
        op += 4;
    }

    ip = output;
    op = output;

    for (i = 0; i < 4; i++)
    {
        a1 = ip[0] + ip[12];
        b1 = ip[4] + ip[8];
        c1 = ip[4] - ip[8];
        d1 = ip[0] - ip[12];

        a2 = a1 + b1;
        b2 = c1 + d1;
        c2 = a1 - b1;
        d2 = d1 - c1;

        a2 += (a2 > 0);
        b2 += (b2 > 0);
        c2 += (c2 > 0);
        d2 += (d2 > 0);

        op[0] = (a2) >> 1;
        op[4] = (b2) >> 1;
        op[8] = (c2) >> 1;
        op[12] = (d2) >> 1;

        ip++;
        op++;
    }
}