-
Notifications
You must be signed in to change notification settings - Fork 0
/
dsp.c
162 lines (125 loc) · 3.14 KB
/
dsp.c
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
#include <inttypes.h>
#include <math.h>
#include <stdlib.h>
#include "dsp.h"
#include "tables.h"
static void transpose_block(float *in_data, float *out_data)
{
int i, j;
for (i = 0; i < 8; ++i)
{
for (j = 0; j < 8; ++j)
{
out_data[i*8+j] = in_data[j*8+i];
}
}
}
static void dct_1d(float *in_data, float *out_data)
{
int i, j;
for (i = 0; i < 8; ++i)
{
float dct = 0;
for (j = 0; j < 8; ++j)
{
dct += in_data[j] * dctlookup[j][i];
}
out_data[i] = dct;
}
}
static void idct_1d(float *in_data, float *out_data)
{
int i, j;
for (i = 0; i < 8; ++i)
{
float idct = 0;
for (j = 0; j < 8; ++j)
{
idct += in_data[j] * dctlookup[i][j];
}
out_data[i] = idct;
}
}
static void scale_block(float *in_data, float *out_data)
{
int u, v;
for (v = 0; v < 8; ++v)
{
for (u = 0; u < 8; ++u)
{
float a1 = !u ? ISQRT2 : 1.0f;
float a2 = !v ? ISQRT2 : 1.0f;
/* Scale according to normalizing function */
out_data[v*8+u] = in_data[v*8+u] * a1 * a2;
}
}
}
static void quantize_block(float *in_data, float *out_data, uint8_t *quant_tbl)
{
int zigzag;
for (zigzag = 0; zigzag < 64; ++zigzag)
{
uint8_t u = zigzag_U[zigzag];
uint8_t v = zigzag_V[zigzag];
float dct = in_data[v*8+u];
/* Zig-zag and quantize */
out_data[zigzag] = (float) round((dct / 4.0) / quant_tbl[zigzag]);
}
}
static void dequantize_block(float *in_data, float *out_data,
uint8_t *quant_tbl)
{
int zigzag;
for (zigzag = 0; zigzag < 64; ++zigzag)
{
uint8_t u = zigzag_U[zigzag];
uint8_t v = zigzag_V[zigzag];
float dct = in_data[zigzag];
/* Zig-zag and de-quantize */
out_data[v*8+u] = (float) round((dct * quant_tbl[zigzag]) / 4.0);
}
}
void dct_quant_block_8x8(int16_t *in_data, int16_t *out_data,
uint8_t *quant_tbl)
{
float mb[8*8] __attribute((aligned(16)));
float mb2[8*8] __attribute((aligned(16)));
int i, v;
for (i = 0; i < 64; ++i) { mb2[i] = in_data[i]; }
/* Two 1D DCT operations with transpose */
for (v = 0; v < 8; ++v) { dct_1d(mb2+v*8, mb+v*8); }
transpose_block(mb, mb2);
for (v = 0; v < 8; ++v) { dct_1d(mb2+v*8, mb+v*8); }
transpose_block(mb, mb2);
scale_block(mb2, mb);
quantize_block(mb, mb2, quant_tbl);
for (i = 0; i < 64; ++i) { out_data[i] = mb2[i]; }
}
void dequant_idct_block_8x8(int16_t *in_data, int16_t *out_data,
uint8_t *quant_tbl)
{
float mb[8*8] __attribute((aligned(16)));
float mb2[8*8] __attribute((aligned(16)));
int i, v;
for (i = 0; i < 64; ++i) { mb[i] = in_data[i]; }
dequantize_block(mb, mb2, quant_tbl);
scale_block(mb2, mb);
/* Two 1D inverse DCT operations with transpose */
for (v = 0; v < 8; ++v) { idct_1d(mb+v*8, mb2+v*8); }
transpose_block(mb2, mb);
for (v = 0; v < 8; ++v) { idct_1d(mb+v*8, mb2+v*8); }
transpose_block(mb2, mb);
for (i = 0; i < 64; ++i) { out_data[i] = mb[i]; }
}
void sad_block_8x8(uint8_t *block1, uint8_t *block2, int stride, int *result)
{
int u, v;
*result = 0;
for (v = 0; v < 8; ++v)
{
for (u = 0; u < 8; ++u)
{
*result += abs(block2[v*stride+u] - block1[v*stride+u]);
}
}
}