-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArithmeticEncoder.cpp
45 lines (39 loc) · 1.29 KB
/
ArithmeticEncoder.cpp
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
#include "ArithmeticEncoder.hpp"
#include <cassert>
#include <cstdint>
#include <cstdio>
ArithmeticEncoder::ArithmeticEncoder(File* f) : x1(0), x2(0xffffffff), x(0), archive(f) {};
void ArithmeticEncoder::prefetch() {
for (int i = 0; i < 4; ++i) {
x = (x << 8) + (archive->getchar() & 255);
}
}
void ArithmeticEncoder::flush() {
archive->putChar(x1 >> 24); // flush first unequal byte of range
}
void ArithmeticEncoder::encodeBit(int p, int bit) {
if (p == 0) p++;
assert(p > 0 && p < 65536);
uint32_t xMid = x1 + ((x2 - x1) >> 16) * p + (((x2 - x1) & 0xffff) * p >> 16);
assert(xMid >= x1 && xMid < x2);
bit != 0 ? (x2 = xMid) : (x1 = xMid + 1);
while (((x1 ^ x2) & 0xFF000000) == 0) { // pass equal leading bytes of range
archive->putChar(x2 >> 24);
x1 <<= 8;
x2 = (x2 << 8) + 255;
}
}
int ArithmeticEncoder::decodeBit(int p) {
if (p == 0) p++;
assert(p > 0 && p < 65536);
uint32_t xMid = x1 + ((x2 - x1) >> 16) * p +(((x2 - x1) & 0xffff) * p >> 16);
assert(xMid >= x1 && xMid < x2);
int bit = static_cast<int>(x <= xMid);
bit != 0 ? (x2 = xMid) : (x1 = xMid + 1);
while (((x1 ^ x2) & 0xFF000000) == 0) { // pass equal leading bytes of range
x1 <<= 8;
x2 = (x2 << 8) + 255;
x = (x << 8) + (archive->getchar() & 255); // EOF is OK
}
return bit;
}