-
Notifications
You must be signed in to change notification settings - Fork 6
/
imgproc.c
293 lines (225 loc) · 6.1 KB
/
imgproc.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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#include <stddef.h>
#include <assert.h>
#include <stdint.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include "imgproc.h"
#include "coeffs.h"
void dequantize_block(struct int_block *int_block, struct flt_block *flt_block, struct qtable *qtable)
{
assert(int_block != NULL);
assert(flt_block != NULL);
assert(qtable != NULL);
for (int j = 0; j < 64; ++j) {
flt_block->c[j] = (float)(int_block->c[j] * (int32_t)qtable->Q[j]);
}
}
void quantize_block(struct int_block *int_block, struct flt_block *flt_block, struct qtable *qtable)
{
assert(int_block != NULL);
assert(flt_block != NULL);
assert(qtable != NULL);
for (int j = 0; j < 64; ++j) {
int_block->c[j] = (int32_t)roundf(flt_block->c[j] / (float)qtable->Q[j]);
}
}
int dequantize(struct context *context)
{
assert(context != NULL);
for (int i = 0; i < 256; ++i) {
if (context->component[i].int_buffer != NULL) {
printf("Dequantizing component %i...\n", i);
size_t blocks = context->component[i].b_x * context->component[i].b_y;
uint8_t Tq = context->component[i].Tq;
struct qtable *qtable = &context->qtable[Tq];
// for each block, for each coefficient, c[] *= Q[]
for (size_t b = 0; b < blocks; ++b) {
struct int_block *int_block = &context->component[i].int_buffer[b];
struct flt_block *flt_block = &context->component[i].flt_buffer[b];
dequantize_block(int_block, flt_block, qtable);
}
}
}
return RET_SUCCESS;
}
int quantize(struct context *context)
{
assert(context != NULL);
for (int i = 0; i < 256; ++i) {
if (context->component[i].int_buffer != NULL) {
printf("Quantizing component %i...\n", i);
size_t blocks = context->component[i].b_x * context->component[i].b_y;
uint8_t Tq = context->component[i].Tq;
struct qtable *qtable = &context->qtable[Tq];
// for each block, for each coefficient, c[] *= Q[]
for (size_t b = 0; b < blocks; ++b) {
struct int_block *int_block = &context->component[i].int_buffer[b];
struct flt_block *flt_block = &context->component[i].flt_buffer[b];
quantize_block(int_block, flt_block, qtable);
}
}
}
return RET_SUCCESS;
}
static float C(int u)
{
if (u == 0) {
return 1.f / sqrtf(2.f);
}
return 1.f;
}
float lut[8][8];
void init_lut()
{
for (int x = 0; x < 8; ++x) {
for (int u = 0; u < 8; ++u) {
lut[x][u] = 0.5f * C(u) * cosf((2 * x + 1) * u * M_PI / 16);
}
}
}
void idct1(const float in[8], float out[8], size_t stride)
{
for (int x = 0; x < 8; ++x) {
float s = 0.f;
for (int u = 0; u < 8; ++u) {
s += in[u * stride] * lut[x][u];
}
out[x * stride] = s;
}
}
void fdct1(const float in[8], float out[8], size_t stride)
{
for (int u = 0; u < 8; ++u) {
float s = 0.f;
for (int x = 0; x < 8; ++x) {
s += in[x * stride] * lut[x][u];
}
out[u * stride] = s;
}
}
void idct(struct flt_block *flt_block)
{
static int init = 0;
// init look-up table
if (init == 0) {
init_lut();
init = 1;
}
struct flt_block b;
for (int y = 0; y < 8; ++y) {
idct1(&flt_block->c[y * 8], &b.c[y * 8], 1);
}
for (int x = 0; x < 8; ++x) {
idct1(&b.c[x], &flt_block->c[x], 8);
}
}
void fdct(struct flt_block *flt_block)
{
static int init = 0;
// init look-up table
if (init == 0) {
init_lut();
init = 1;
}
struct flt_block b;
for (int y = 0; y < 8; ++y) {
fdct1(&flt_block->c[y * 8], &b.c[y * 8], 1);
}
for (int x = 0; x < 8; ++x) {
fdct1(&b.c[x], &flt_block->c[x], 8);
}
}
int inverse_dct(struct context *context)
{
assert(context != NULL);
/* precision */
uint8_t P = context->P;
int shift = 1 << (P - 1);
for (int i = 0; i < 256; ++i) {
if (context->component[i].int_buffer != NULL) {
printf("IDCT on component %i...\n", i);
size_t blocks = context->component[i].b_x * context->component[i].b_y;
for (size_t b = 0; b < blocks; ++b) {
struct flt_block *flt_block = &context->component[i].flt_buffer[b];
idct(flt_block);
// level shift
for (int j = 0; j < 64; ++j) {
flt_block->c[j] += shift;
}
}
}
}
return RET_SUCCESS;
}
int forward_dct(struct context *context)
{
assert(context != NULL);
/* precision */
uint8_t P = context->P;
int shift = 1 << (P - 1);
for (int i = 0; i < 256; ++i) {
if (context->component[i].int_buffer != NULL) {
printf("FDCT on component %i...\n", i);
size_t blocks = context->component[i].b_x * context->component[i].b_y;
for (size_t b = 0; b < blocks; ++b) {
struct flt_block *flt_block = &context->component[i].flt_buffer[b];
// level shift
for (int j = 0; j < 64; ++j) {
flt_block->c[j] -= shift;
}
fdct(flt_block);
}
}
}
return RET_SUCCESS;
}
/* convert floating-point blocks to frame buffers (for each component) */
int conv_blocks_to_frame(struct context *context)
{
assert(context != NULL);
for (int i = 0; i < 256; ++i) {
if (context->component[i].frame_buffer != NULL) {
printf("converting component %i...\n", i);
float *buffer = context->component[i].frame_buffer;
size_t b_x = context->component[i].b_x;
size_t b_y = context->component[i].b_y;
for (size_t y = 0; y < b_y; ++y) {
for (size_t x = 0; x < b_x; ++x) {
/* copy from... */
struct flt_block *flt_block = &context->component[i].flt_buffer[y * b_x + x];
for (int v = 0; v < 8; ++v) {
for (int u = 0; u < 8; ++u) {
buffer[y * b_x * 8 * 8 + v * b_x * 8 + x * 8 + u] = flt_block->c[v * 8 + u];
}
}
}
}
}
}
return RET_SUCCESS;
}
int conv_frame_to_blocks(struct context *context)
{
assert(context != NULL);
for (int i = 0; i < 256; ++i) {
if (context->component[i].frame_buffer != NULL) {
printf("converting component %i...\n", i);
float *buffer = context->component[i].frame_buffer;
size_t b_x = context->component[i].b_x;
size_t b_y = context->component[i].b_y;
for (size_t y = 0; y < b_y; ++y) {
for (size_t x = 0; x < b_x; ++x) {
/* copy to... */
struct flt_block *flt_block = &context->component[i].flt_buffer[y * b_x + x];
for (int v = 0; v < 8; ++v) {
for (int u = 0; u < 8; ++u) {
flt_block->c[v * 8 + u] = buffer[y * b_x * 8 * 8 + v * b_x * 8 + x * 8 + u];
}
}
}
}
}
}
return RET_SUCCESS;
}