-
Notifications
You must be signed in to change notification settings - Fork 6
/
common.c
225 lines (164 loc) · 4.3 KB
/
common.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
#include <stddef.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include "common.h"
#include "mjpeg.h"
#include "huffman.h"
#include "coeffs.h"
int init_qtable(struct qtable *qtable)
{
assert(qtable != NULL);
qtable->Pq = 0;
for (int i = 0; i < 64; ++i) {
qtable->Q[i] = 1;
}
return RET_SUCCESS;
}
int init_component(struct component *component)
{
assert(component != NULL);
component->H = 0;
component->V = 0;
component->Tq = 0;
component->Td = 0;
component->Ta = 0;
component->b_x = 0;
component->b_y = 0;
component->int_buffer = NULL;
component->flt_buffer = NULL;
component->frame_buffer = NULL;
return RET_SUCCESS;
}
int init_htable(struct htable *htable)
{
assert(htable != NULL);
for (int i = 0; i < 16; ++i) {
htable->L[i] = 0;
}
for (int i = 0; i < 16; ++i) {
for (int j = 0; j < 255; ++j) {
htable->V[i][j] = 0;
}
}
return RET_SUCCESS;
}
int init_context(struct context *context)
{
assert(context != NULL);
for (int i = 0; i < 4; ++i) {
init_qtable(&context->qtable[i]);
}
context->P = 0;
context->Y = 0;
context->X = 0;
context->Nf = 0;
for (int i = 0; i < 256; ++i) {
init_component(&context->component[i]);
}
for (int j = 0; j < 2; ++j) {
for (int i = 0; i < 4; ++i) {
init_htable(&context->htable[j][i]);
init_huffenc(&context->huffenc[j][i]);
}
}
/* implicit MJPEG tables */
context->htable[0][0] = mjpg_htable_0_0;
context->htable[0][1] = mjpg_htable_0_1;
context->htable[1][0] = mjpg_htable_1_0;
context->htable[1][1] = mjpg_htable_1_1;
conv_htable_to_hcode(&context->htable[0][0], &context->hcode[0][0]);
conv_htable_to_hcode(&context->htable[0][1], &context->hcode[0][1]);
conv_htable_to_hcode(&context->htable[1][0], &context->hcode[1][0]);
conv_htable_to_hcode(&context->htable[1][1], &context->hcode[1][1]);
context->Ri = 0;
context->m_x = 0;
context->m_y = 0;
context->mblocks = 0;
return RET_SUCCESS;
}
size_t ceil_div(size_t n, size_t d)
{
return (n + (d - 1)) / d;
}
int alloc_buffers(struct component *component, size_t size)
{
// redefine component (multiple definitions of the same component inside SOF marker)
free(component->int_buffer);
free(component->flt_buffer);
free(component->frame_buffer);
component->int_buffer = malloc(sizeof(struct int_block) * size);
if (component->int_buffer == NULL) {
return RET_FAILURE_MEMORY_ALLOCATION;
}
memset(component->int_buffer, 0, sizeof(struct int_block) * size);
component->flt_buffer = malloc(sizeof(struct flt_block) * size);
if (component->flt_buffer == NULL) {
return RET_FAILURE_MEMORY_ALLOCATION;
}
component->frame_buffer = malloc(sizeof(float) * 64 * size);
if (component->frame_buffer == NULL) {
return RET_FAILURE_MEMORY_ALLOCATION;
}
return RET_SUCCESS;
}
void free_buffers(struct context *context)
{
for (int i = 0; i < 256; ++i) {
free(context->component[i].int_buffer);
free(context->component[i].flt_buffer);
free(context->component[i].frame_buffer);
}
}
int compute_no_blocks_and_alloc_buffers(struct context *context)
{
assert(context != NULL);
int err;
uint16_t Y, X;
uint8_t max_H, max_V;
Y = context->Y;
X = context->X;
max_H = context->max_H;
max_V = context->max_V;
context->m_x = ceil_div(X, 8 * max_H);
context->m_y = ceil_div(Y, 8 * max_V);
printf("Expecting %zu macroblocks\n", context->m_x * context->m_y);
for (int i = 0; i < 256; ++i) {
uint8_t H, V;
H = context->component[i].H;
V = context->component[i].V;
if (H != 0) {
size_t b_x = ceil_div(X, 8 * max_H) * H;
size_t b_y = ceil_div(Y, 8 * max_V) * V;
context->component[i].b_x = b_x;
context->component[i].b_y = b_y;
printf("C = %i: %zu blocks (x=%zu y=%zu)\n", i, b_x * b_y, b_x, b_y);
err = alloc_buffers(&context->component[i], b_x * b_y);
RETURN_IF(err);
}
}
return RET_SUCCESS;
}
int clamp(int min, int val, int max)
{
if (val < min) {
return min;
}
if (val > max) {
return max;
}
return val;
}
/* Before starting the procedure, the values of FREQ are collected for V = 0 to 255 and the FREQ value for V = 256 is set to 1 to reserve one code point. */
void init_huffenc(struct huffenc *huffenc)
{
assert(huffenc != NULL);
for (int i = 0; i < 256; ++i) {
huffenc->freq[i] = 0;
}
huffenc->freq[256] = 1;
for (int i = 0; i < 257; ++i) {
huffenc->codesize[i] = 0;
huffenc->others[i] = -1;
}
}