-
Notifications
You must be signed in to change notification settings - Fork 2
/
ac.c
273 lines (214 loc) · 6.19 KB
/
ac.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
#include "ac.h"
#include <assert.h>
#include <stdlib.h>
void count_cum_freqs(struct symbol *table, size_t symbols)
{
if (symbols > 0) {
table[0].cum_freq = 0;
for (size_t i = 1; i < symbols; i++) {
table[i].cum_freq =
table[i-1].cum_freq +
table[i-1].freq;
}
}
}
size_t calc_total_freq(struct symbol *table, size_t symbols)
{
size_t total = 0;
for (size_t i = 0; i < symbols; i++) {
total += table[i].freq;
}
return total;
}
const size_t g_FirstQuarter = 0x20000000;
const size_t g_ThirdQuarter = 0x60000000;
const size_t g_Half = 0x40000000;
void ac_init(struct ac *ac)
{
ac->mLow = 0x00000000;
ac->mHigh = 0x7FFFFFFF;
ac->mScale = 0;
}
#define put_bit(bio, b) bio_write_bits((bio), (b), 1)
#define get_bit(bio) bio_read_bits((bio), 1)
void ac_encode_scale(struct ac *ac, struct bio *bio)
{
// E1/E2
while ((ac->mHigh < g_Half) || (ac->mLow >= g_Half)) {
if (ac->mHigh < g_Half) {
put_bit(bio, 0);
ac->mLow = 2 * ac->mLow;
ac->mHigh = 2 * ac->mHigh + 1;
for (; ac->mScale > 0; ac->mScale--) {
put_bit(bio, 1);
}
} else if (ac->mLow >= g_Half) {
put_bit(bio, 1);
ac->mLow = 2 * (ac->mLow - g_Half);
ac->mHigh = 2 * (ac->mHigh - g_Half) + 1;
for (; ac->mScale > 0; ac->mScale--) {
put_bit(bio, 0);
}
}
}
// E3
while ((g_FirstQuarter <= ac->mLow) && (ac->mHigh < g_ThirdQuarter)) {
ac->mScale++;
ac->mLow = 2 * (ac->mLow - g_FirstQuarter);
ac->mHigh = 2 * (ac->mHigh - g_FirstQuarter) + 1;
}
}
void ac_encode(struct ac *ac, struct bio *bio, size_t low_freq, size_t high_freq, size_t total)
{
size_t mStep = (ac->mHigh - ac->mLow + 1) / total;
ac->mHigh = ac->mLow + mStep * high_freq - 1;
ac->mLow = ac->mLow + mStep * low_freq;
ac_encode_scale(ac, bio);
}
size_t index_of_symbol(size_t symb, struct symbol *model, size_t symbols)
{
for (size_t i = 0; i < symbols; i++) {
if (symb == model[i].symb) {
return i;
}
}
abort();
}
void ac_encode_symbol(struct ac *ac, struct bio *bio, size_t symb, struct symbol *model, size_t symbols, size_t total_count)
{
size_t index = index_of_symbol(symb, model, symbols);
size_t low_freq = model[index].cum_freq;
size_t high_freq = model[index].cum_freq + model[index].freq;
ac_encode(ac, bio, low_freq, high_freq, total_count);
}
float ac_encode_symbol_query_prob(size_t symb, struct symbol *model, size_t symbols, size_t total_count)
{
size_t index = index_of_symbol(symb, model, symbols);
return (float)model[index].freq / total_count;
}
void ac_encode_flush(struct ac *ac, struct bio *bio)
{
if (ac->mLow < g_FirstQuarter) {
put_bit(bio, 0);
for (size_t i=0; i < ac->mScale + 1; i++) {
put_bit(bio, 1);
}
} else {
put_bit(bio, 1);
}
}
size_t ac_decode_target(struct ac *ac, size_t mStep)
{
return (ac->mBuffer - ac->mLow) / mStep;
}
void ac_decode_init(struct ac *ac, struct bio *bio)
{
ac->mBuffer = 0;
for (size_t i = 0; i < 31; i++) {
ac->mBuffer = (ac->mBuffer << 1) | get_bit(bio);
}
}
void ac_decode_scale(struct ac *ac, struct bio *bio)
{
// E1/E2
while ((ac->mHigh < g_Half) || (ac->mLow >= g_Half)) {
if (ac->mHigh < g_Half) {
ac->mLow = 2 * ac->mLow;
ac->mHigh = 2 * ac->mHigh + 1;
ac->mBuffer = 2 * ac->mBuffer + get_bit(bio);
} else if (ac->mLow >= g_Half) {
ac->mLow = 2 * (ac->mLow - g_Half);
ac->mHigh = 2 * (ac->mHigh - g_Half) + 1;
ac->mBuffer = 2 * (ac->mBuffer - g_Half) + get_bit(bio);
}
ac->mScale = 0;
}
// E3
while ((g_FirstQuarter <= ac->mLow) && (ac->mHigh < g_ThirdQuarter)) {
ac->mScale++;
ac->mLow = 2 * (ac->mLow - g_FirstQuarter);
ac->mHigh = 2 * (ac->mHigh - g_FirstQuarter) + 1;
ac->mBuffer = 2 * (ac->mBuffer - g_FirstQuarter) + get_bit(bio);
}
}
size_t index_of_value(size_t value, struct symbol *model, size_t symbols)
{
for (size_t i = 0; i < symbols; i++) {
size_t low_freq = model[i].cum_freq;
size_t high_freq = model[i].cum_freq + model[i].freq;
if (value >= low_freq && value < high_freq) {
return i;
}
}
abort();
}
size_t ac_decode_symbol(struct ac *ac, struct bio *bio, struct symbol *model, size_t symbols, size_t total)
{
size_t mStep = (ac->mHigh - ac->mLow + 1) / total;
size_t value = ac_decode_target(ac, mStep);
size_t index = index_of_value(value, model, symbols);
size_t low_freq = model[index].cum_freq;
size_t high_freq = model[index].cum_freq + model[index].freq;
ac->mHigh = ac->mLow + mStep * high_freq - 1;
ac->mLow = ac->mLow + mStep * low_freq;
ac_decode_scale(ac, bio);
return model[index].symb;
}
void ac_encode_symbol_model(struct ac *ac, struct bio *bio, size_t symb, struct model *model)
{
ac_encode_symbol(ac, bio, symb, model->table, model->count, model->total);
}
float ac_encode_symbol_model_query_prob(size_t symb, struct model *model)
{
return ac_encode_symbol_query_prob(symb, model->table, model->count, model->total);
}
size_t ac_decode_symbol_model(struct ac *ac, struct bio *bio, struct model *model)
{
return ac_decode_symbol(ac, bio, model->table, model->count, model->total);
}
void inc_model(struct model *model, size_t symbol)
{
#if 1
// FIXME BUG
const size_t index = symbol;
#else
size_t index = index_of_symbol(symbol, model->table, model->count);
#endif
const size_t increment = 1;
model->table[index].freq += increment;
count_cum_freqs(model->table, model->count);
model->total += increment;
}
void model_create(struct model *model, size_t size)
{
assert(model != NULL);
model->count = size;
model->table = malloc(model->count * sizeof(struct symbol));
if (model->table == NULL) {
abort();
}
for (size_t i = 0; i < model->count; ++i) {
model->table[i].symb = i;
model->table[i].freq = 1;
}
count_cum_freqs(model->table, model->count);
model->total = calc_total_freq(model->table, model->count);
}
void model_enlarge(struct model *model)
{
assert(model != NULL);
model->count++;
model->table = realloc(model->table, model->count * sizeof(struct symbol));
if (model->table == NULL) {
abort();
}
model->table[model->count - 1].symb = model->count - 1;
model->table[model->count - 1].freq = 1;
count_cum_freqs(model->table, model->count);
model->total = calc_total_freq(model->table, model->count);
}
void model_destroy(struct model *model)
{
assert(model != NULL);
free(model->table);
}