-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathArithmeticEncoder.cpp
More file actions
105 lines (88 loc) · 2.96 KB
/
ArithmeticEncoder.cpp
File metadata and controls
105 lines (88 loc) · 2.96 KB
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
#include "ArithmeticEncoder.hpp"
#include <cassert>
#include <cstdint>
ArithmeticEncoder::ArithmeticEncoder(File* f)
: x1(0), x2(UINT32_MAX), x(0), pending_bits(0), bits_in_B(0), B(0), archive(f) {
}
// Read one bit from the archive, buffering a full byte at a time.
int ArithmeticEncoder::bit_read() {
if (bits_in_B == 0) {
B = archive->getchar(); // EOF is handled gracefully by the caller
bits_in_B = 8;
}
bits_in_B--; //7..0
return (B >> bits_in_B) & 1;
}
// Write one bit to the archive, flushing a full byte when the buffer is full.
void ArithmeticEncoder::bit_write(const int bit) {
B = (B << 1) | bit;
bits_in_B++;
if (bits_in_B == 8) {
archive->putChar(B);
B = 0;
bits_in_B = 0;
}
}
// Write one bit, then emit all pending bits with the opposite value (E3 carry resolution).
void ArithmeticEncoder::bit_write_with_pending(const int bit) {
bit_write(bit);
for (; pending_bits > 0; pending_bits--)
bit_write(bit ^ 1);
}
// Flush the encoder: emit enough bits from x1 to complete the final byte.
void ArithmeticEncoder::flush() {
do {
bit_write_with_pending(x1 >> 31);
x1 <<= 1;
} while (bits_in_B != 0);
}
// Prime the decode window with the first 32 bits of compressed data.
void ArithmeticEncoder::prefetch() {
for (int i = 0; i < 32; ++i)
x = (x << 1) | bit_read();
}
// Encode one bit. p is the probability (scaled by 2^PRECISION) that bit == 1.
void ArithmeticEncoder::encodeBit(uint32_t p, const int bit) {
if (p == 0) p++;
assert(p > 0 && p < (1u << PRECISION));
// Split [x1, x2] at xmid proportional to p; round to nearest for accuracy.
const uint32_t xmid = x1 + uint32_t((uint64_t(x2 - x1) * p) >> PRECISION);
assert(xmid >= x1 && xmid < x2);
bit != 0 ? (x2 = xmid) : (x1 = xmid + 1);
// Emit leading bits that are identical in x1 and x2.
while (((x1 ^ x2) >> 31) == 0) {
bit_write_with_pending(x2 >> 31);
x1 <<= 1;
x2 = (x2 << 1) | 1;
}
// Rescale when the interval straddles the midpoint; defer the carry bit.
while (x1 >= 0x40000000 && x2 < 0xC0000000) {
pending_bits++;
x1 = (x1 << 1) & 0x7FFFFFFF;
x2 = (x2 << 1) | 0x80000001;
}
}
// Decode one bit. p is the probability (scaled by 2^PRECISION) that the bit is 1.
int ArithmeticEncoder::decodeBit(uint32_t p) {
if (p == 0) p++;
assert(p > 0 && p < (1u << PRECISION));
// Mirror the encoder's midpoint split.
const uint32_t xmid = x1 + uint32_t((uint64_t(x2 - x1) * p) >> PRECISION);
assert(xmid >= x1 && xmid < x2);
const int bit = (x <= xmid) ? 1 : 0;
bit != 0 ? (x2 = xmid) : (x1 = xmid + 1);
// Consume leading bits and refill x from the bitstream.
while (((x1 ^ x2) >> 31) == 0) {
x1 <<= 1;
x2 = (x2 << 1) | 1;
x = (x << 1) | bit_read();
}
// Undo midpoint rescaling and keep x in sync with the interval.
while (x1 >= 0x40000000 && x2 < 0xC0000000) {
x1 = (x1 << 1) & 0x7FFFFFFF;
x2 = (x2 << 1) | 0x80000001;
x = (x << 1) ^ 0x80000000;
x += bit_read();
}
return bit;
}