-
Notifications
You must be signed in to change notification settings - Fork 0
/
huffman.go
374 lines (306 loc) · 6.9 KB
/
huffman.go
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
package ncrlite
import (
"container/heap"
"errors"
"fmt"
"io"
"math/bits"
"slices"
)
// Prefix table used to represent Huffman tree during decompression:
// eight layers are combined into one for fast decompression.
type htLut []htLutEntry
type htLutEntry struct {
value byte // If a leaf, the value: bitlength of the delta (minus one)
// If a leaf, the number of bits to skip in the last byte. Zero indicates
// this is a node.
skip byte
// If a node, offset into the table to find children.
next int
}
// Node in a Huffman tree when constructed from frequences
type htNode struct {
value byte // If a leaf, the value: bitlength of the delta (minus one)
count int // Cumulative count
children [2]*htNode
depth int
}
// Codebook for Huffman code
type htCode []htCodeEntry
type htCodeEntry struct {
code uint64
length byte
}
func (h htLut) Print(w io.Writer) {
for i := 0; i < len(h); i += 256 {
fmt.Fprintf(w, "offset %d:", i)
for code := 0; code < 256; code++ {
for j := 0; j < int(8); j++ {
fmt.Fprintf(w, "%d", (code>>j)&1)
}
fmt.Fprintf(w, " value=%d skip=%d next=%d\n", h[i+code].value, h[i+code].skip, h[i+code].next)
}
}
}
func (h htCode) Print(w io.Writer) {
for i, entry := range h {
fmt.Fprintf(w, "%2d ", i)
code := entry.code
for j := 0; j < int(entry.length); j++ {
fmt.Fprintf(w, "%d", code&1)
code >>= 1
}
fmt.Fprintf(w, "\n")
}
}
// Pack codebook
func (h htCode) Pack(bw *bitWriter) {
bw.WriteBits(uint64(len(h)-1), 6)
bw.WriteBits(uint64(h[0].length), 6)
prev := h[0].length
for i := 1; i < len(h); i++ {
l := h[i].length
absDiff := l - prev
sign := 1
if l < prev {
sign = 0
absDiff = -absDiff
}
for j := 0; j < int(absDiff); j++ {
bw.WriteBits(0, 1)
bw.WriteBits(uint64(sign), 1)
}
bw.WriteBits(1, 1)
prev = l
}
}
func unpackCodeLengths(br *bitReader, l io.Writer) ([]byte, error) {
size := 12
n := br.ReadBits(6) + 1
h := make([]byte, n)
h[0] = byte(br.ReadBits(6))
if l != nil {
fmt.Fprintf(l, "max bitlength %d\n", n-1)
fmt.Fprintf(l, "codelength h[0] %d\n", h[0])
defer func() {
fmt.Fprintf(l, "dictionary size %db\n", size)
}()
}
if n == 1 {
return h, br.Err()
}
change := int8(0)
i := 1
waitingFor := 0
for {
size++
next := br.ReadBit()
if next == 1 {
h[i] = byte(int8(h[i-1]) + change)
i++
if i == int(n) {
break
}
waitingFor = 0
change = 0
continue
}
waitingFor++
size++
up := br.ReadBit()
if up == 1 {
change++
} else {
change--
}
if waitingFor > int(n) {
return nil, errors.New("invalid codelength in Huffman table")
}
}
return h, br.Err()
}
// Priority queue to find nodes with lowest count
type htHeap []*htNode
func (h htHeap) Len() int { return len(h) }
func (h htHeap) Less(i, j int) bool {
if h[i].count == h[j].count {
return h[i].depth < h[j].depth
}
return h[i].count < h[j].count
}
func (h htHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *htHeap) Push(x any) {
node := x.(*htNode)
*h = append(*h, node)
}
func (h *htHeap) Pop() any {
old := *h
n := len(old)
item := old[n-1]
old[n-1] = nil
*h = old[0 : n-1]
return item
}
// Create a Huffman code for the given frequency table
func buildHuffmanCode(freq []int) htCode {
h := make(htHeap, len(freq))
for i := 0; i < len(freq); i++ {
h[i] = &htNode{
value: byte(i),
count: freq[i],
depth: 0,
}
}
heap.Init(&h)
// Build the tree: combine the two subtrees with the shortest count
// repetitively.
for len(h) > 1 {
n1 := heap.Pop(&h).(*htNode)
n2 := heap.Pop(&h).(*htNode)
heap.Push(&h, &htNode{
count: n1.count + n2.count,
children: [2]*htNode{n1, n2},
depth: max(n1.depth, n2.depth) + 1,
})
}
// There are many equivalent trees; what matters is the length
// of the code for each value. Find those and find the canonical
// code for that.
codeLengths := make([]byte, len(freq))
type nodeDepth struct {
n *htNode
depth byte
}
stack := []nodeDepth{{n: h[0], depth: 0}}
h = nil
for len(stack) > 0 {
nd := stack[len(stack)-1]
stack = stack[:len(stack)-1]
if nd.n.children[0] != nil {
stack = append(
stack,
nodeDepth{nd.n.children[0], nd.depth + 1},
nodeDepth{nd.n.children[1], nd.depth + 1},
)
continue
}
codeLengths[nd.n.value] = nd.depth
}
codebook := canonicalHuffmanCode(codeLengths)
return codebook
}
func unpackHuffmanTree(br *bitReader, l io.Writer) (htLut, error) {
codeLengths, err := unpackCodeLengths(br, l)
if err != nil {
return nil, err
}
// Special case: if there
if len(codeLengths) == 1 {
if l != nil {
fmt.Fprintf(l, "\nTrivial codebook: only zero bitlength deltas\n\n")
}
return nil, nil
}
codebook := canonicalHuffmanCode(codeLengths)
if l != nil {
fmt.Fprintf(l, "\nCodebook bitlengths:\n")
codebook.Print(l)
fmt.Fprintf(l, "\n")
}
// Build the binary tree before building the prefix table
root := &htNode{}
for bn, entry := range codebook {
code := entry.code
// Walk down the existing tree
node := root
d := 0
for {
next := node.children[code&1]
if next == nil {
break
}
d++
code >>= 1
node = next
}
// Now create the new nodes
for j := d; j < int(entry.length); j++ {
node.children[code&1] = &htNode{}
node = node.children[code&1]
code >>= 1
}
node.value = byte(bn)
}
// Build the prefix table
lut := make(htLut, 256)
type todoEntry struct {
node *htNode
offset int // in htLut
}
todo := []todoEntry{{root, 0}}
for len(todo) > 0 {
cur := todo[len(todo)-1]
todo = todo[:len(todo)-1]
for code := 0; code < 256; code++ {
node := cur.node
skip := 0
for ; skip < 8; skip++ {
next := node.children[(code>>skip)&1]
if next == nil {
break
}
node = next
}
if node.children[0] == nil {
lut[cur.offset+code].skip = byte(skip)
lut[cur.offset+code].value = node.value
continue
}
lut[cur.offset+code].skip = 0
lut[cur.offset+code].next = len(lut)
todo = append(todo, todoEntry{
node: node,
offset: len(lut),
})
for i := 0; i < 256; i++ {
lut = append(lut, htLutEntry{})
}
}
}
return lut, nil
}
func canonicalHuffmanCode(codeLengths []byte) htCode {
type valueLength struct {
value byte
length byte
code uint64
}
vls := make([]valueLength, len(codeLengths))
for i := 0; i < len(codeLengths); i++ {
vls[i].value = byte(i)
vls[i].length = codeLengths[i]
}
slices.SortFunc(vls, func(a, b valueLength) int {
if a.length != b.length {
return int(a.length) - int(b.length)
}
return int(a.value) - int(b.value)
})
prevLength := byte(0)
code := uint64(0)
ret := make(htCode, len(codeLengths))
for i := 0; i < len(vls); i++ {
l := vls[i].length
if l != prevLength {
code <<= vls[i].length - prevLength
}
vls[i].code = code
ret[vls[i].value] = htCodeEntry{
code: bits.Reverse64(code) >> (64 - l),
length: l,
}
prevLength = l
code++
}
return ret
}