-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrangecoder.h
472 lines (413 loc) · 14.8 KB
/
rangecoder.h
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
#pragma once
#ifndef RANGECODER_H_
#define RANGECODER_H_
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <queue>
#include <sstream>
#include <stdint.h>
#include <string>
#include <vector>
namespace rangecoder
{
using range_t = uint64_t;
using byte_t = uint8_t;
class PModel
{
public:
// Accumulated frequency of index, i.e. sum of frequency of range [min_index, index).
virtual range_t cum_freq(int index) const = 0;
// Frequency of index
virtual range_t c_freq(int index) const = 0;
range_t total_freq() const
{
return cum_freq(max_index()) + c_freq(max_index());
};
// Returns min index, the first valid index.
// All index 'i', that satisfy 'pmodel.min_index() <= i <= pmodel.max_index()' must be valid index.
virtual int min_index() const = 0;
// Returns max index, the last valid index.
// All index 'i', that satisfy 'pmodel.min_index() <= i <= pmodel.max_index()' must be valid index.
virtual int max_index() const = 0;
bool index_is_valid(int index)
{
return min_index() <= index && index <= max_index();
}
};
enum RangeCoderVerbose {
SILENT = false,
VERBOSE = true,
};
namespace local
{
constexpr auto TOP8 = range_t(1) << (64 - 8);
constexpr auto TOP16 = range_t(1) << (64 - 16);
auto hex_zero_filled(range_t bytes) -> std::string
{
std::stringstream sformatter;
sformatter << std::setfill('0') << std::setw(sizeof(range_t) * 2) << std::hex << bytes;
return sformatter.str();
}
auto hex_zero_filled(byte_t byte) -> std::string
{
std::stringstream sformatter;
sformatter << std::setfill('0') << std::setw(2) << std::hex << static_cast<int>(byte);
return sformatter.str();
}
class RangeCoder
{
public:
RangeCoder()
{
m_lower_bound = 0;
m_range = std::numeric_limits<range_t>::max();
};
template<RangeCoderVerbose RANGECODER_VERBOSE>
auto update_param(
const PModel &pmodel, const int index, const std::function<void(byte_t)> &f = [](byte_t) {}) -> int
{
const auto c_freq = pmodel.c_freq(index);
const auto cum_freq = pmodel.cum_freq(index);
const auto total_freq = pmodel.total_freq();
auto num_bytes = 0;
const auto range_per_total = m_range / total_freq;
m_range = range_per_total * c_freq;
m_lower_bound += range_per_total * cum_freq;
if constexpr (RANGECODER_VERBOSE)
{
std::cout << " range, lower bound updated" << std::endl;
print_status();
}
while (is_no_carry_expansion_needed())
{
f(do_no_carry_expansion<RANGECODER_VERBOSE>());
num_bytes++;
}
while (is_range_reduction_expansion_needed())
{
f(do_range_reduction_expansion<RANGECODER_VERBOSE>());
num_bytes++;
}
if constexpr (RANGECODER_VERBOSE)
{
std::cout << " total: " << num_bytes << " byte shifted" << std::endl;
}
return num_bytes;
};
template<RangeCoderVerbose RANGECODER_VERBOSE>
auto shift_byte() -> byte_t
{
auto tmp = static_cast<byte_t>(m_lower_bound >> (64 - 8));
m_range <<= 8;
m_lower_bound <<= 8;
if constexpr (RANGECODER_VERBOSE)
{
std::cout << " shifted out byte: "
<< "0x"
<< local::hex_zero_filled(tmp)
<< std::endl;
}
return tmp;
};
void print_status() const
{
std::cout << " range: 0x" << local::hex_zero_filled(range()) << std::endl;
std::cout << " lower bound: 0x" << local::hex_zero_filled(lower_bound()) << std::endl;
std::cout << " upper bound: 0x" << local::hex_zero_filled(upper_bound()) << std::endl;
}
protected:
void lower_bound(const range_t lower_bound)
{
m_lower_bound = lower_bound;
};
void range(const range_t range)
{
m_range = range;
};
auto lower_bound() const -> range_t
{
return m_lower_bound;
};
auto range() const -> range_t
{
return m_range;
};
auto upper_bound() const -> uint64_t
{
return m_lower_bound + m_range;
};
private:
auto is_no_carry_expansion_needed() const -> bool
{
return (m_lower_bound ^ upper_bound()) < local::TOP8;
};
template<RangeCoderVerbose RANGECODER_VERBOSE>
auto do_no_carry_expansion() -> byte_t
{
if constexpr (RANGECODER_VERBOSE)
{
std::cout << " no carry expansion" << std::endl;
}
return shift_byte<RANGECODER_VERBOSE>();
};
auto is_range_reduction_expansion_needed() const -> bool
{
return m_range < local::TOP16;
};
template<RangeCoderVerbose RANGECODER_VERBOSE>
auto do_range_reduction_expansion() -> byte_t
{
if constexpr (RANGECODER_VERBOSE)
{
std::cout << " range reduction expansion" << std::endl;
}
m_range = (~m_lower_bound) & (local::TOP16 - 1);
return shift_byte<RANGECODER_VERBOSE>();
};
uint64_t m_lower_bound;
uint64_t m_range;
};
}// namespace local
class RangeEncoder : local::RangeCoder
{
public:
// Returns number of bytes stabled.
template<RangeCoderVerbose RANGECODER_VERBOSE = SILENT>
auto encode(const PModel &pmodel, const int index) -> int
{
if constexpr (RANGECODER_VERBOSE)
{
std::cout << " encode: " << index << std::endl;
print_status();
}
const auto n = update_param<RANGECODER_VERBOSE>(pmodel, index, [this](auto byte) { m_bytes.push_back(byte); });
if constexpr (RANGECODER_VERBOSE)
{
std::cout << " encode: " << index << " done" << std::endl
<< std::endl;
}
return n;
};
template<RangeCoderVerbose RANGECODER_VERBOSE = SILENT>
auto finish() -> std::vector<byte_t>
{
for (auto i = 0; i < 8; i++)
{
m_bytes.push_back(shift_byte<RANGECODER_VERBOSE>());
}
return m_bytes;
}
friend std::ostream &operator<<(std::ostream &os, RangeEncoder &re)
{
const auto data = re.finish();
// write all bytes in data to ostream
for (const auto byte : data)
{
os << byte;
}
return os;
}
void print_status() const
{
std::cout << " range: 0x" << local::hex_zero_filled(range()) << std::endl;
std::cout << " lower bound: 0x" << local::hex_zero_filled(lower_bound()) << std::endl;
std::cout << " upper bound: 0x" << local::hex_zero_filled(upper_bound()) << std::endl;
if (m_bytes.empty())
{
std::cout << " bytes: NULL" << std::endl;
}
else
{
std::cout << " bytes: 0x";
for (const auto byte : m_bytes)
{
std::cout << local::hex_zero_filled(byte);
}
std::cout << std::endl;
}
}
private:
std::vector<uint8_t> m_bytes;
};
class RangeDecoder : local::RangeCoder
{
public:
void start(std::istream &is)
{
std::queue<byte_t> empty;
m_bytes.swap(empty);
// read bytes from isteram into m_bytes
// until reach to eof
while (is.good())
{
m_bytes.push(is.get());
}
lower_bound(0);
range(std::numeric_limits<range_t>::max());
for (auto i = 0; i < 8; i++)
{
shift_byte_buffer();
}
}
void start(std::queue<byte_t> bytes)
{
m_bytes = std::move(bytes);
lower_bound(0);
range(std::numeric_limits<range_t>::max());
for (auto i = 0; i < 8; i++)
{
shift_byte_buffer();
}
};
void start(const std::vector<byte_t> &bytes)
{
// Convert vector to queue.
for (auto byte : bytes)
{
m_bytes.push(byte);
}
lower_bound(0);
range(std::numeric_limits<range_t>::max());
for (auto i = 0; i < 8; i++)
{
shift_byte_buffer();
}
};
// Returns index of pmodel used to encode.
// pmodel **must** be same as used to encode.
template<RangeCoderVerbose RANGECODER_VERBOSE = SILENT>
auto decode(const PModel &pmodel) -> int
{
if constexpr (RANGECODER_VERBOSE)
{
std::cout << " decode: unknown " << std::endl;
print_status();
}
const auto index = binary_search_encoded_index<RANGECODER_VERBOSE>(pmodel);
const auto n = update_param<RANGECODER_VERBOSE>(pmodel, index);
for (int i = 0; i < n; i++)
{
shift_byte_buffer();
}
if constexpr (RANGECODER_VERBOSE)
{
std::cout << " decode: " << index << " done" << std::endl;
std::cout << std::endl;
}
return static_cast<int>(index);
};
void print_status() const
{
std::cout << " range: 0x" << local::hex_zero_filled(range()) << std::endl;
std::cout << " lower bound: 0x" << local::hex_zero_filled(lower_bound()) << std::endl;
std::cout << " upper bound: 0x" << local::hex_zero_filled(upper_bound()) << std::endl;
std::cout << " data: 0x" << local::hex_zero_filled(m_data) << std::endl;
}
friend std::istream &operator>>(std::istream &is, RangeDecoder &rd)
{
rd.start(is);
return is;
}
private:
// binary search encoded index
template<RangeCoderVerbose RANGECODER_VERBOSE>
auto binary_search_encoded_index(const PModel &pmodel) const -> int
{
auto left = pmodel.min_index();
auto right = pmodel.max_index();
const auto range_per_total = range() / pmodel.total_freq();
const auto f = (m_data - lower_bound()) / range_per_total;
if constexpr (RANGECODER_VERBOSE)
{
std::cout << " --------- BINARY SEARCH ---------" << std::endl;
std::cout << " find cum: (data: 0x"
<< local::hex_zero_filled(m_data)
<< " - lower bound: 0x"
<< local::hex_zero_filled(lower_bound())
<< ")"
<< std::endl
<< " / range_per_total: 0x"
<< local::hex_zero_filled(range_per_total)
<< " = "
<< f
<< std::endl;
std::cout << " binary search encoded index: " << left << " " << right << std::endl;
}
while (left < right)
{
const auto mid = (left + right) / 2;
const auto mid_cum = pmodel.cum_freq(mid + 1);
if constexpr (RANGECODER_VERBOSE)
{
std::cout << " middle index: (left: " << left << " + right: " << right << " ) / 2 = " << mid
<< ", cum at middle: " << mid_cum << std::endl;
}
if (mid_cum <= f)
{
if constexpr (RANGECODER_VERBOSE)
{
std::cout << " target contains between left:" << left << " middle: " << mid << std::endl;
}
left = mid + 1;
}
else
{
if constexpr (RANGECODER_VERBOSE)
{
std::cout << " target contains between middle:" << mid << " right: " << right << std::endl;
}
right = mid;
}
}
if constexpr (RANGECODER_VERBOSE)
{
std::cout << " find! left: " << left << "= right: " << right << std::endl;
std::cout << " --------- BINARY SEARCH FINISH ---------" << std::endl;
}
return left;
};
void shift_byte_buffer()
{
const auto front_byte = m_bytes.front();
m_data = (m_data << 8) | static_cast<range_t>(front_byte);
m_bytes.pop();
};
std::queue<byte_t> m_bytes;
range_t m_data;
};
template<int N = 256>
class UniformDistribution : public PModel
{
public:
UniformDistribution() = default;
range_t c_freq(const int index) const override
{
return 1;
}
range_t cum_freq(const int index) const override
{
return index;
}
int min_index() const override
{
return 0;
}
int max_index() const override
{
return N - 1;
}
void print() const
{
std::cout << std::endl;
std::cout << "UNIFORM DIST" << std::endl;
for (auto i = min_index(); i <= max_index(); i++)
{
std::cout << "idx: " << i << ", c: " << c_freq(i) << ", cum: " << cum_freq(i) << std::endl;
}
std::cout << std::endl;
}
};
}// namespace rangecoder
#endif