tinygo.org/x/drivers@v0.27.1-0.20240509133757-7dbca2a54349/image/internal/compress/flate/huffman_code.go (about) 1 // Copyright 2009 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package flate 6 7 import ( 8 "math" 9 "math/bits" 10 "sort" 11 ) 12 13 // hcode is a huffman code with a bit code and bit length. 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 // stored to avoid repeated allocation in generate 23 lfs byFreq // stored to avoid repeated allocation in generate 24 } 25 26 type literalNode struct { 27 literal uint16 28 freq int32 29 } 30 31 // A levelInfo describes the state of the constructed tree for a given depth. 32 type levelInfo struct { 33 // Our level. for better printing 34 level int32 35 36 // The frequency of the last node at this level 37 lastFreq int32 38 39 // The frequency of the next character to add to this level 40 nextCharFreq int32 41 42 // The frequency of the next pair (from level below) to add to this level. 43 // Only valid if the "needed" value of the next lower level is 0. 44 nextPairFreq int32 45 46 // The number of chains remaining to generate for this level before moving 47 // up to the next level 48 needed int32 49 } 50 51 // set sets the code and length of an hcode. 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 // Generates a HuffmanCode corresponding to the fixed literal table 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 // size 8, 000110000 .. 10111111 74 bits = ch + 48 75 size = 8 76 break 77 case ch < 256: 78 // size 9, 110010000 .. 111111111 79 bits = ch + 400 - 144 80 size = 9 81 break 82 case ch < 280: 83 // size 7, 0000000 .. 0010111 84 bits = ch - 256 85 size = 7 86 break 87 default: 88 // size 8, 11000000 .. 11000111 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 // Return the number of literals assigned to each bit size in the Huffman encoding 122 // 123 // This method is only called when list.length >= 3 124 // The cases of 0, 1, and 2 literals are handled by special case code. 125 // 126 // list An array of the literals with non-zero frequencies 127 // 128 // and their associated frequencies. The array is in order of increasing 129 // frequency, and has as its last element a special element with frequency 130 // MaxInt32 131 // 132 // maxBits The maximum number of bits that should be used to encode any literal. 133 // 134 // Must be less than 16. 135 // 136 // return An integer array in which array[i] indicates the number of literals 137 // 138 // that should be encoded in i bits. 139 func (h *huffmanEncoder) bitCounts(list []literalNode, maxBits int32) []int32 { 140 if maxBits >= maxBitsLimit { 141 panic("flate: maxBits too large") 142 } 143 n := int32(len(list)) 144 list = list[0 : n+1] 145 list[n] = maxNode() 146 147 // The tree can't have greater depth than n - 1, no matter what. This 148 // saves a little bit of work in some small cases 149 if maxBits > n-1 { 150 maxBits = n - 1 151 } 152 153 // Create information about each of the levels. 154 // A bogus "Level 0" whose sole purpose is so that 155 // level1.prev.needed==0. This makes level1.nextPairFreq 156 // be a legitimate value that never gets chosen. 157 var levels [maxBitsLimit]levelInfo 158 // leafCounts[i] counts the number of literals at the left 159 // of ancestors of the rightmost node at level i. 160 // leafCounts[i][j] is the number of literals at the left 161 // of the level j ancestor. 162 var leafCounts [maxBitsLimit][maxBitsLimit]int32 163 164 for level := int32(1); level <= maxBits; level++ { 165 // For every level, the first two items are the first two characters. 166 // We initialize the levels as if we had already figured this out. 167 levels[level] = levelInfo{ 168 level: level, 169 lastFreq: list[1].freq, 170 nextCharFreq: list[2].freq, 171 nextPairFreq: list[0].freq + list[1].freq, 172 } 173 leafCounts[level][level] = 2 174 if level == 1 { 175 levels[level].nextPairFreq = math.MaxInt32 176 } 177 } 178 179 // We need a total of 2*n - 2 items at top level and have already generated 2. 180 levels[maxBits].needed = 2*n - 4 181 182 level := maxBits 183 for { 184 l := &levels[level] 185 if l.nextPairFreq == math.MaxInt32 && l.nextCharFreq == math.MaxInt32 { 186 // We've run out of both leafs and pairs. 187 // End all calculations for this level. 188 // To make sure we never come back to this level or any lower level, 189 // set nextPairFreq impossibly large. 190 l.needed = 0 191 levels[level+1].nextPairFreq = math.MaxInt32 192 level++ 193 continue 194 } 195 196 prevFreq := l.lastFreq 197 if l.nextCharFreq < l.nextPairFreq { 198 // The next item on this row is a leaf node. 199 n := leafCounts[level][level] + 1 200 l.lastFreq = l.nextCharFreq 201 // Lower leafCounts are the same of the previous node. 202 leafCounts[level][level] = n 203 l.nextCharFreq = list[n].freq 204 } else { 205 // The next item on this row is a pair from the previous row. 206 // nextPairFreq isn't valid until we generate two 207 // more values in the level below 208 l.lastFreq = l.nextPairFreq 209 // Take leaf counts from the lower level, except counts[level] remains the same. 210 copy(leafCounts[level][:level], leafCounts[level-1][:level]) 211 levels[l.level-1].needed = 2 212 } 213 214 if l.needed--; l.needed == 0 { 215 // We've done everything we need to do for this level. 216 // Continue calculating one level up. Fill in nextPairFreq 217 // of that level with the sum of the two nodes we've just calculated on 218 // this level. 219 if l.level == maxBits { 220 // All done! 221 break 222 } 223 levels[l.level+1].nextPairFreq = prevFreq + l.lastFreq 224 level++ 225 } else { 226 // If we stole from below, move down temporarily to replenish it. 227 for levels[level-1].needed > 0 { 228 level-- 229 } 230 } 231 } 232 233 // Somethings is wrong if at the end, the top level is null or hasn't used 234 // all of the leaves. 235 if leafCounts[maxBits][maxBits] != n { 236 panic("leafCounts[maxBits][maxBits] != n") 237 } 238 239 bitCount := h.bitCount[:maxBits+1] 240 bits := 1 241 counts := &leafCounts[maxBits] 242 for level := maxBits; level > 0; level-- { 243 // chain.leafCount gives the number of literals requiring at least "bits" 244 // bits to encode. 245 bitCount[bits] = counts[level] - counts[level-1] 246 bits++ 247 } 248 return bitCount 249 } 250 251 // Look at the leaves and assign them a bit count and an encoding as specified 252 // in RFC 1951 3.2.2 253 func (h *huffmanEncoder) assignEncodingAndSize(bitCount []int32, list []literalNode) { 254 code := uint16(0) 255 for n, bits := range bitCount { 256 code <<= 1 257 if n == 0 || bits == 0 { 258 continue 259 } 260 // The literals list[len(list)-bits] .. list[len(list)-bits] 261 // are encoded using "bits" bits, and get the values 262 // code, code + 1, .... The code values are 263 // assigned in literal order (not frequency order). 264 chunk := list[len(list)-int(bits):] 265 266 h.lns.sort(chunk) 267 for _, node := range chunk { 268 h.codes[node.literal] = hcode{code: reverseBits(code, uint8(n)), len: uint16(n)} 269 code++ 270 } 271 list = list[0 : len(list)-int(bits)] 272 } 273 } 274 275 // Update this Huffman Code object to be the minimum code for the specified frequency count. 276 // 277 // freq An array of frequencies, in which frequency[i] gives the frequency of literal i. 278 // maxBits The maximum number of bits to use for any literal. 279 func (h *huffmanEncoder) generate(freq []int32, maxBits int32) { 280 if h.freqcache == nil { 281 // Allocate a reusable buffer with the longest possible frequency table. 282 // Possible lengths are codegenCodeCount, offsetCodeCount and maxNumLit. 283 // The largest of these is maxNumLit, so we allocate for that case. 284 h.freqcache = make([]literalNode, maxNumLit+1) 285 } 286 list := h.freqcache[:len(freq)+1] 287 // Number of non-zero literals 288 count := 0 289 // Set list to be the set of all non-zero literals and their frequencies 290 for i, f := range freq { 291 if f != 0 { 292 list[count] = literalNode{uint16(i), f} 293 count++ 294 } else { 295 list[count] = literalNode{} 296 h.codes[i].len = 0 297 } 298 } 299 list[len(freq)] = literalNode{} 300 301 list = list[:count] 302 if count <= 2 { 303 // Handle the small cases here, because they are awkward for the general case code. With 304 // two or fewer literals, everything has bit length 1. 305 for i, node := range list { 306 // "list" is in order of increasing literal value. 307 h.codes[node.literal].set(uint16(i), 1) 308 } 309 return 310 } 311 h.lfs.sort(list) 312 313 // Get the number of literals for each bit count 314 bitCount := h.bitCounts(list, maxBits) 315 // And do the assignment 316 h.assignEncodingAndSize(bitCount, list) 317 } 318 319 type byLiteral []literalNode 320 321 func (s *byLiteral) sort(a []literalNode) { 322 *s = byLiteral(a) 323 sort.Sort(s) 324 } 325 326 func (s byLiteral) Len() int { return len(s) } 327 328 func (s byLiteral) Less(i, j int) bool { 329 return s[i].literal < s[j].literal 330 } 331 332 func (s byLiteral) Swap(i, j int) { s[i], s[j] = s[j], s[i] } 333 334 type byFreq []literalNode 335 336 func (s *byFreq) sort(a []literalNode) { 337 *s = byFreq(a) 338 sort.Sort(s) 339 } 340 341 func (s byFreq) Len() int { return len(s) } 342 343 func (s byFreq) Less(i, j int) bool { 344 if s[i].freq == s[j].freq { 345 return s[i].literal < s[j].literal 346 } 347 return s[i].freq < s[j].freq 348 } 349 350 func (s byFreq) Swap(i, j int) { s[i], s[j] = s[j], s[i] } 351 352 func reverseBits(number uint16, bitLength byte) uint16 { 353 return bits.Reverse16(number << (16 - bitLength)) 354 }