1
2
3
4
5 package flate
6
7 import (
8 "math"
9 "math/bits"
10 "sort"
11 )
12
13
14 type hcode struct {
15 code, len uint16
16 }
17
18 type huffmanEncoder struct {
19 codes []hcode
20 freqcache []literalNode
21 bitCount [17]int32
22 lns byLiteral
23 lfs byFreq
24 }
25
26 type literalNode struct {
27 literal uint16
28 freq int32
29 }
30
31
32 type levelInfo struct {
33
34 level int32
35
36
37 lastFreq int32
38
39
40 nextCharFreq int32
41
42
43
44 nextPairFreq int32
45
46
47
48 needed int32
49 }
50
51
52 func (h *hcode) set(code uint16, length uint16) {
53 h.len = length
54 h.code = code
55 }
56
57 func maxNode() literalNode { return literalNode{math.MaxUint16, math.MaxInt32} }
58
59 func newHuffmanEncoder(size int) *huffmanEncoder {
60 return &huffmanEncoder{codes: make([]hcode, size)}
61 }
62
63
64 func generateFixedLiteralEncoding() *huffmanEncoder {
65 h := newHuffmanEncoder(maxNumLit)
66 codes := h.codes
67 var ch uint16
68 for ch = 0; ch < maxNumLit; ch++ {
69 var bits uint16
70 var size uint16
71 switch {
72 case ch < 144:
73
74 bits = ch + 48
75 size = 8
76 break
77 case ch < 256:
78
79 bits = ch + 400 - 144
80 size = 9
81 break
82 case ch < 280:
83
84 bits = ch - 256
85 size = 7
86 break
87 default:
88
89 bits = ch + 192 - 280
90 size = 8
91 }
92 codes[ch] = hcode{code: reverseBits(bits, byte(size)), len: size}
93 }
94 return h
95 }
96
97 func generateFixedOffsetEncoding() *huffmanEncoder {
98 h := newHuffmanEncoder(30)
99 codes := h.codes
100 for ch := range codes {
101 codes[ch] = hcode{code: reverseBits(uint16(ch), 5), len: 5}
102 }
103 return h
104 }
105
106 var fixedLiteralEncoding *huffmanEncoder = generateFixedLiteralEncoding()
107 var fixedOffsetEncoding *huffmanEncoder = generateFixedOffsetEncoding()
108
109 func (h *huffmanEncoder) bitLength(freq []int32) int {
110 var total int
111 for i, f := range freq {
112 if f != 0 {
113 total += int(f) * int(h.codes[i].len)
114 }
115 }
116 return total
117 }
118
119 const maxBitsLimit = 16
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134 func (h *huffmanEncoder) bitCounts(list []literalNode, maxBits int32) []int32 {
135 if maxBits >= maxBitsLimit {
136 panic("flate: maxBits too large")
137 }
138 n := int32(len(list))
139 list = list[0 : n+1]
140 list[n] = maxNode()
141
142
143
144 if maxBits > n-1 {
145 maxBits = n - 1
146 }
147
148
149
150
151
152 var levels [maxBitsLimit]levelInfo
153
154
155
156
157 var leafCounts [maxBitsLimit][maxBitsLimit]int32
158
159 for level := int32(1); level <= maxBits; level++ {
160
161
162 levels[level] = levelInfo{
163 level: level,
164 lastFreq: list[1].freq,
165 nextCharFreq: list[2].freq,
166 nextPairFreq: list[0].freq + list[1].freq,
167 }
168 leafCounts[level][level] = 2
169 if level == 1 {
170 levels[level].nextPairFreq = math.MaxInt32
171 }
172 }
173
174
175 levels[maxBits].needed = 2*n - 4
176
177 level := maxBits
178 for {
179 l := &levels[level]
180 if l.nextPairFreq == math.MaxInt32 && l.nextCharFreq == math.MaxInt32 {
181
182
183
184
185 l.needed = 0
186 levels[level+1].nextPairFreq = math.MaxInt32
187 level++
188 continue
189 }
190
191 prevFreq := l.lastFreq
192 if l.nextCharFreq < l.nextPairFreq {
193
194 n := leafCounts[level][level] + 1
195 l.lastFreq = l.nextCharFreq
196
197 leafCounts[level][level] = n
198 l.nextCharFreq = list[n].freq
199 } else {
200
201
202
203 l.lastFreq = l.nextPairFreq
204
205 copy(leafCounts[level][:level], leafCounts[level-1][:level])
206 levels[l.level-1].needed = 2
207 }
208
209 if l.needed--; l.needed == 0 {
210
211
212
213
214 if l.level == maxBits {
215
216 break
217 }
218 levels[l.level+1].nextPairFreq = prevFreq + l.lastFreq
219 level++
220 } else {
221
222 for levels[level-1].needed > 0 {
223 level--
224 }
225 }
226 }
227
228
229
230 if leafCounts[maxBits][maxBits] != n {
231 panic("leafCounts[maxBits][maxBits] != n")
232 }
233
234 bitCount := h.bitCount[:maxBits+1]
235 bits := 1
236 counts := &leafCounts[maxBits]
237 for level := maxBits; level > 0; level-- {
238
239
240 bitCount[bits] = counts[level] - counts[level-1]
241 bits++
242 }
243 return bitCount
244 }
245
246
247
248 func (h *huffmanEncoder) assignEncodingAndSize(bitCount []int32, list []literalNode) {
249 code := uint16(0)
250 for n, bits := range bitCount {
251 code <<= 1
252 if n == 0 || bits == 0 {
253 continue
254 }
255
256
257
258
259 chunk := list[len(list)-int(bits):]
260
261 h.lns.sort(chunk)
262 for _, node := range chunk {
263 h.codes[node.literal] = hcode{code: reverseBits(code, uint8(n)), len: uint16(n)}
264 code++
265 }
266 list = list[0 : len(list)-int(bits)]
267 }
268 }
269
270
271
272
273
274 func (h *huffmanEncoder) generate(freq []int32, maxBits int32) {
275 if h.freqcache == nil {
276
277
278
279 h.freqcache = make([]literalNode, maxNumLit+1)
280 }
281 list := h.freqcache[:len(freq)+1]
282
283 count := 0
284
285 for i, f := range freq {
286 if f != 0 {
287 list[count] = literalNode{uint16(i), f}
288 count++
289 } else {
290 list[count] = literalNode{}
291 h.codes[i].len = 0
292 }
293 }
294 list[len(freq)] = literalNode{}
295
296 list = list[:count]
297 if count <= 2 {
298
299
300 for i, node := range list {
301
302 h.codes[node.literal].set(uint16(i), 1)
303 }
304 return
305 }
306 h.lfs.sort(list)
307
308
309 bitCount := h.bitCounts(list, maxBits)
310
311 h.assignEncodingAndSize(bitCount, list)
312 }
313
314 type byLiteral []literalNode
315
316 func (s *byLiteral) sort(a []literalNode) {
317 *s = byLiteral(a)
318 sort.Sort(s)
319 }
320
321 func (s byLiteral) Len() int { return len(s) }
322
323 func (s byLiteral) Less(i, j int) bool {
324 return s[i].literal < s[j].literal
325 }
326
327 func (s byLiteral) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
328
329 type byFreq []literalNode
330
331 func (s *byFreq) sort(a []literalNode) {
332 *s = byFreq(a)
333 sort.Sort(s)
334 }
335
336 func (s byFreq) Len() int { return len(s) }
337
338 func (s byFreq) Less(i, j int) bool {
339 if s[i].freq == s[j].freq {
340 return s[i].literal < s[j].literal
341 }
342 return s[i].freq < s[j].freq
343 }
344
345 func (s byFreq) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
346
347 func reverseBits(number uint16, bitLength byte) uint16 {
348 return bits.Reverse16(number << (16 - bitLength))
349 }
350
View as plain text