-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencoding.py
More file actions
121 lines (106 loc) · 3.31 KB
/
encoding.py
File metadata and controls
121 lines (106 loc) · 3.31 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
from collections import deque
MAX= 0xffff
Q1 = 0x4000
HALF= 0x8000
Q3= 0xc000
class Encoder:
def __init__(self, alph_len = 256):
self.low = 0
self.high = MAX
self.n_bits = 0
self.alph_len = alph_len
self.out_buffer = deque()
def __len__(self):
return self.out_buffer.__len__()
def encode_char(self, prob, cum_prob):
d = self.high - self.low + 1
self.low += int(d * cum_prob)
self.high = self.low + int(d * prob)
while True:
assert self.high <= MAX and self.low >= 0
if self.high < HALF:
self.write_bits(0)
self.n_bits = 0
elif self.low >= HALF:
self.write_bits(1)
self.n_bits = 0
self.low -= HALF
self.high -= HALF
elif self.low >= Q1 and self.high < Q3:
self.n_bits += 1
self.low -=Q1
self.high -= Q1
else:
break
self.low *= 2
self.high = 2*self.high + 1
def write_bits(self, bit):
self.out_buffer.append(bit)
while self.n_bits > 0:
self.out_buffer.append(not bit)
self.n_bits -= 1
def flush_buffer(self):
dat = bytearray()
while len(self.out_buffer) >= 8:
c = 0
for _ in range(8):
c = 2*c + self.out_buffer.popleft()
dat.append(c)
return dat
class Decoder:
def __init__(self, alph_len):
self.low = 0
self.high = MAX
self.code = 0
self.alph_len = alph_len
self.buffer = deque()
def __len__(self):
return self.buffer.__len__()
def update(self, prob, cum_prob):
if len(self.buffer) == 0:
raise ValueError("Bit buffer is empty")
d = self.high - self.low +1
self.low += int(cum_prob*d)
self.high = self.low + int(prob*d)
while True:
if self.high < HALF:
pass
elif self.low >= HALF:
self.low -= HALF
self.high -= HALF
self.code -= HALF
elif self.low >= Q1 and self.high < Q3:
self.code -= Q1
self.low -= Q1
self.high -= Q1
else:
break
self.low *= 2
self.high = 2*self.high +1
self.code = 2*self.code + self.buffer.popleft()
def decode(self, probs):
i = 0
cum_prob = 0
d = self.high - self.low +1
cp = (self.code - self.low +1)/d
while cum_prob + probs[i].item() < cp:
cum_prob += probs[i].item()
i += 1
prob = probs[i].item()
return i, prob, cum_prob
def init_code(self):
self.code = 0
for _ in range(16):
self.code = 2*self.code +self.buffer.popleft()
def fill_buffer(bytestream, n):
for _ in range(n):
try:
byte = bytestream.read(1)
assert byte != b''
except:
byte = b'\x00'
self.insert_byte(byte)
def insert_byte(self, byte):
c = int.from_bytes(byte, byteorder="big")
for j in reversed(range(8)):
self.buffer.append( (c>>j)&1)