huffman.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. package brotli
  2. /* Copyright 2013 Google Inc. All Rights Reserved.
  3. Distributed under MIT license.
  4. See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
  5. */
  6. /* Utilities for building Huffman decoding tables. */
  7. const huffmanMaxCodeLength = 15
  8. /* Maximum possible Huffman table size for an alphabet size of (index * 32),
  9. max code length 15 and root table bits 8. */
  10. var kMaxHuffmanTableSize = []uint16{
  11. 256,
  12. 402,
  13. 436,
  14. 468,
  15. 500,
  16. 534,
  17. 566,
  18. 598,
  19. 630,
  20. 662,
  21. 694,
  22. 726,
  23. 758,
  24. 790,
  25. 822,
  26. 854,
  27. 886,
  28. 920,
  29. 952,
  30. 984,
  31. 1016,
  32. 1048,
  33. 1080,
  34. 1112,
  35. 1144,
  36. 1176,
  37. 1208,
  38. 1240,
  39. 1272,
  40. 1304,
  41. 1336,
  42. 1368,
  43. 1400,
  44. 1432,
  45. 1464,
  46. 1496,
  47. 1528,
  48. }
  49. /* BROTLI_NUM_BLOCK_LEN_SYMBOLS == 26 */
  50. const huffmanMaxSize26 = 396
  51. /* BROTLI_MAX_BLOCK_TYPE_SYMBOLS == 258 */
  52. const huffmanMaxSize258 = 632
  53. /* BROTLI_MAX_CONTEXT_MAP_SYMBOLS == 272 */
  54. const huffmanMaxSize272 = 646
  55. const huffmanMaxCodeLengthCodeLength = 5
  56. /* Do not create this struct directly - use the ConstructHuffmanCode
  57. * constructor below! */
  58. type huffmanCode struct {
  59. bits byte
  60. value uint16
  61. }
  62. func constructHuffmanCode(bits byte, value uint16) huffmanCode {
  63. var h huffmanCode
  64. h.bits = bits
  65. h.value = value
  66. return h
  67. }
  68. /* Builds Huffman lookup table assuming code lengths are in symbol order. */
  69. /* Builds Huffman lookup table assuming code lengths are in symbol order.
  70. Returns size of resulting table. */
  71. /* Builds a simple Huffman table. The |num_symbols| parameter is to be
  72. interpreted as follows: 0 means 1 symbol, 1 means 2 symbols,
  73. 2 means 3 symbols, 3 means 4 symbols with lengths [2, 2, 2, 2],
  74. 4 means 4 symbols with lengths [1, 2, 3, 3]. */
  75. /* Contains a collection of Huffman trees with the same alphabet size. */
  76. /* max_symbol is needed due to simple codes since log2(alphabet_size) could be
  77. greater than log2(max_symbol). */
  78. type huffmanTreeGroup struct {
  79. htrees [][]huffmanCode
  80. codes []huffmanCode
  81. alphabet_size uint16
  82. max_symbol uint16
  83. num_htrees uint16
  84. }
  85. const reverseBitsMax = 8
  86. const reverseBitsBase = 0
  87. var kReverseBits = [1 << reverseBitsMax]byte{
  88. 0x00,
  89. 0x80,
  90. 0x40,
  91. 0xC0,
  92. 0x20,
  93. 0xA0,
  94. 0x60,
  95. 0xE0,
  96. 0x10,
  97. 0x90,
  98. 0x50,
  99. 0xD0,
  100. 0x30,
  101. 0xB0,
  102. 0x70,
  103. 0xF0,
  104. 0x08,
  105. 0x88,
  106. 0x48,
  107. 0xC8,
  108. 0x28,
  109. 0xA8,
  110. 0x68,
  111. 0xE8,
  112. 0x18,
  113. 0x98,
  114. 0x58,
  115. 0xD8,
  116. 0x38,
  117. 0xB8,
  118. 0x78,
  119. 0xF8,
  120. 0x04,
  121. 0x84,
  122. 0x44,
  123. 0xC4,
  124. 0x24,
  125. 0xA4,
  126. 0x64,
  127. 0xE4,
  128. 0x14,
  129. 0x94,
  130. 0x54,
  131. 0xD4,
  132. 0x34,
  133. 0xB4,
  134. 0x74,
  135. 0xF4,
  136. 0x0C,
  137. 0x8C,
  138. 0x4C,
  139. 0xCC,
  140. 0x2C,
  141. 0xAC,
  142. 0x6C,
  143. 0xEC,
  144. 0x1C,
  145. 0x9C,
  146. 0x5C,
  147. 0xDC,
  148. 0x3C,
  149. 0xBC,
  150. 0x7C,
  151. 0xFC,
  152. 0x02,
  153. 0x82,
  154. 0x42,
  155. 0xC2,
  156. 0x22,
  157. 0xA2,
  158. 0x62,
  159. 0xE2,
  160. 0x12,
  161. 0x92,
  162. 0x52,
  163. 0xD2,
  164. 0x32,
  165. 0xB2,
  166. 0x72,
  167. 0xF2,
  168. 0x0A,
  169. 0x8A,
  170. 0x4A,
  171. 0xCA,
  172. 0x2A,
  173. 0xAA,
  174. 0x6A,
  175. 0xEA,
  176. 0x1A,
  177. 0x9A,
  178. 0x5A,
  179. 0xDA,
  180. 0x3A,
  181. 0xBA,
  182. 0x7A,
  183. 0xFA,
  184. 0x06,
  185. 0x86,
  186. 0x46,
  187. 0xC6,
  188. 0x26,
  189. 0xA6,
  190. 0x66,
  191. 0xE6,
  192. 0x16,
  193. 0x96,
  194. 0x56,
  195. 0xD6,
  196. 0x36,
  197. 0xB6,
  198. 0x76,
  199. 0xF6,
  200. 0x0E,
  201. 0x8E,
  202. 0x4E,
  203. 0xCE,
  204. 0x2E,
  205. 0xAE,
  206. 0x6E,
  207. 0xEE,
  208. 0x1E,
  209. 0x9E,
  210. 0x5E,
  211. 0xDE,
  212. 0x3E,
  213. 0xBE,
  214. 0x7E,
  215. 0xFE,
  216. 0x01,
  217. 0x81,
  218. 0x41,
  219. 0xC1,
  220. 0x21,
  221. 0xA1,
  222. 0x61,
  223. 0xE1,
  224. 0x11,
  225. 0x91,
  226. 0x51,
  227. 0xD1,
  228. 0x31,
  229. 0xB1,
  230. 0x71,
  231. 0xF1,
  232. 0x09,
  233. 0x89,
  234. 0x49,
  235. 0xC9,
  236. 0x29,
  237. 0xA9,
  238. 0x69,
  239. 0xE9,
  240. 0x19,
  241. 0x99,
  242. 0x59,
  243. 0xD9,
  244. 0x39,
  245. 0xB9,
  246. 0x79,
  247. 0xF9,
  248. 0x05,
  249. 0x85,
  250. 0x45,
  251. 0xC5,
  252. 0x25,
  253. 0xA5,
  254. 0x65,
  255. 0xE5,
  256. 0x15,
  257. 0x95,
  258. 0x55,
  259. 0xD5,
  260. 0x35,
  261. 0xB5,
  262. 0x75,
  263. 0xF5,
  264. 0x0D,
  265. 0x8D,
  266. 0x4D,
  267. 0xCD,
  268. 0x2D,
  269. 0xAD,
  270. 0x6D,
  271. 0xED,
  272. 0x1D,
  273. 0x9D,
  274. 0x5D,
  275. 0xDD,
  276. 0x3D,
  277. 0xBD,
  278. 0x7D,
  279. 0xFD,
  280. 0x03,
  281. 0x83,
  282. 0x43,
  283. 0xC3,
  284. 0x23,
  285. 0xA3,
  286. 0x63,
  287. 0xE3,
  288. 0x13,
  289. 0x93,
  290. 0x53,
  291. 0xD3,
  292. 0x33,
  293. 0xB3,
  294. 0x73,
  295. 0xF3,
  296. 0x0B,
  297. 0x8B,
  298. 0x4B,
  299. 0xCB,
  300. 0x2B,
  301. 0xAB,
  302. 0x6B,
  303. 0xEB,
  304. 0x1B,
  305. 0x9B,
  306. 0x5B,
  307. 0xDB,
  308. 0x3B,
  309. 0xBB,
  310. 0x7B,
  311. 0xFB,
  312. 0x07,
  313. 0x87,
  314. 0x47,
  315. 0xC7,
  316. 0x27,
  317. 0xA7,
  318. 0x67,
  319. 0xE7,
  320. 0x17,
  321. 0x97,
  322. 0x57,
  323. 0xD7,
  324. 0x37,
  325. 0xB7,
  326. 0x77,
  327. 0xF7,
  328. 0x0F,
  329. 0x8F,
  330. 0x4F,
  331. 0xCF,
  332. 0x2F,
  333. 0xAF,
  334. 0x6F,
  335. 0xEF,
  336. 0x1F,
  337. 0x9F,
  338. 0x5F,
  339. 0xDF,
  340. 0x3F,
  341. 0xBF,
  342. 0x7F,
  343. 0xFF,
  344. }
  345. const reverseBitsLowest = (uint64(1) << (reverseBitsMax - 1 + reverseBitsBase))
  346. /* Returns reverse(num >> BROTLI_REVERSE_BITS_BASE, BROTLI_REVERSE_BITS_MAX),
  347. where reverse(value, len) is the bit-wise reversal of the len least
  348. significant bits of value. */
  349. func reverseBits8(num uint64) uint64 {
  350. return uint64(kReverseBits[num])
  351. }
  352. /* Stores code in table[0], table[step], table[2*step], ..., table[end] */
  353. /* Assumes that end is an integer multiple of step */
  354. func replicateValue(table []huffmanCode, step int, end int, code huffmanCode) {
  355. for {
  356. end -= step
  357. table[end] = code
  358. if end <= 0 {
  359. break
  360. }
  361. }
  362. }
  363. /* Returns the table width of the next 2nd level table. |count| is the histogram
  364. of bit lengths for the remaining symbols, |len| is the code length of the
  365. next processed symbol. */
  366. func nextTableBitSize(count []uint16, len int, root_bits int) int {
  367. var left int = 1 << uint(len-root_bits)
  368. for len < huffmanMaxCodeLength {
  369. left -= int(count[len])
  370. if left <= 0 {
  371. break
  372. }
  373. len++
  374. left <<= 1
  375. }
  376. return len - root_bits
  377. }
  378. func buildCodeLengthsHuffmanTable(table []huffmanCode, code_lengths []byte, count []uint16) {
  379. var code huffmanCode /* current table entry */ /* symbol index in original or sorted table */ /* prefix code */ /* prefix code addend */ /* step size to replicate values in current table */ /* size of current table */ /* symbols sorted by code length */
  380. var symbol int
  381. var key uint64
  382. var key_step uint64
  383. var step int
  384. var table_size int
  385. var sorted [codeLengthCodes]int
  386. var offset [huffmanMaxCodeLengthCodeLength + 1]int
  387. var bits int
  388. var bits_count int
  389. /* offsets in sorted table for each length */
  390. assert(huffmanMaxCodeLengthCodeLength <= reverseBitsMax)
  391. /* Generate offsets into sorted symbol table by code length. */
  392. symbol = -1
  393. bits = 1
  394. var i int
  395. for i = 0; i < huffmanMaxCodeLengthCodeLength; i++ {
  396. symbol += int(count[bits])
  397. offset[bits] = symbol
  398. bits++
  399. }
  400. /* Symbols with code length 0 are placed after all other symbols. */
  401. offset[0] = codeLengthCodes - 1
  402. /* Sort symbols by length, by symbol order within each length. */
  403. symbol = codeLengthCodes
  404. for {
  405. var i int
  406. for i = 0; i < 6; i++ {
  407. symbol--
  408. sorted[offset[code_lengths[symbol]]] = symbol
  409. offset[code_lengths[symbol]]--
  410. }
  411. if symbol == 0 {
  412. break
  413. }
  414. }
  415. table_size = 1 << huffmanMaxCodeLengthCodeLength
  416. /* Special case: all symbols but one have 0 code length. */
  417. if offset[0] == 0 {
  418. code = constructHuffmanCode(0, uint16(sorted[0]))
  419. for key = 0; key < uint64(table_size); key++ {
  420. table[key] = code
  421. }
  422. return
  423. }
  424. /* Fill in table. */
  425. key = 0
  426. key_step = reverseBitsLowest
  427. symbol = 0
  428. bits = 1
  429. step = 2
  430. for {
  431. for bits_count = int(count[bits]); bits_count != 0; bits_count-- {
  432. code = constructHuffmanCode(byte(bits), uint16(sorted[symbol]))
  433. symbol++
  434. replicateValue(table[reverseBits8(key):], step, table_size, code)
  435. key += key_step
  436. }
  437. step <<= 1
  438. key_step >>= 1
  439. bits++
  440. if bits > huffmanMaxCodeLengthCodeLength {
  441. break
  442. }
  443. }
  444. }
  445. func buildHuffmanTable(root_table []huffmanCode, root_bits int, symbol_lists symbolList, count []uint16) uint32 {
  446. var code huffmanCode /* current table entry */ /* next available space in table */ /* current code length */ /* symbol index in original or sorted table */ /* prefix code */ /* prefix code addend */ /* 2nd level table prefix code */ /* 2nd level table prefix code addend */ /* step size to replicate values in current table */ /* key length of current table */ /* size of current table */ /* sum of root table size and 2nd level table sizes */
  447. var table []huffmanCode
  448. var len int
  449. var symbol int
  450. var key uint64
  451. var key_step uint64
  452. var sub_key uint64
  453. var sub_key_step uint64
  454. var step int
  455. var table_bits int
  456. var table_size int
  457. var total_size int
  458. var max_length int = -1
  459. var bits int
  460. var bits_count int
  461. assert(root_bits <= reverseBitsMax)
  462. assert(huffmanMaxCodeLength-root_bits <= reverseBitsMax)
  463. for symbolListGet(symbol_lists, max_length) == 0xFFFF {
  464. max_length--
  465. }
  466. max_length += huffmanMaxCodeLength + 1
  467. table = root_table
  468. table_bits = root_bits
  469. table_size = 1 << uint(table_bits)
  470. total_size = table_size
  471. /* Fill in the root table. Reduce the table size to if possible,
  472. and create the repetitions by memcpy. */
  473. if table_bits > max_length {
  474. table_bits = max_length
  475. table_size = 1 << uint(table_bits)
  476. }
  477. key = 0
  478. key_step = reverseBitsLowest
  479. bits = 1
  480. step = 2
  481. for {
  482. symbol = bits - (huffmanMaxCodeLength + 1)
  483. for bits_count = int(count[bits]); bits_count != 0; bits_count-- {
  484. symbol = int(symbolListGet(symbol_lists, symbol))
  485. code = constructHuffmanCode(byte(bits), uint16(symbol))
  486. replicateValue(table[reverseBits8(key):], step, table_size, code)
  487. key += key_step
  488. }
  489. step <<= 1
  490. key_step >>= 1
  491. bits++
  492. if bits > table_bits {
  493. break
  494. }
  495. }
  496. /* If root_bits != table_bits then replicate to fill the remaining slots. */
  497. for total_size != table_size {
  498. copy(table[table_size:], table[:uint(table_size)])
  499. table_size <<= 1
  500. }
  501. /* Fill in 2nd level tables and add pointers to root table. */
  502. key_step = reverseBitsLowest >> uint(root_bits-1)
  503. sub_key = reverseBitsLowest << 1
  504. sub_key_step = reverseBitsLowest
  505. len = root_bits + 1
  506. step = 2
  507. for ; len <= max_length; len++ {
  508. symbol = len - (huffmanMaxCodeLength + 1)
  509. for ; count[len] != 0; count[len]-- {
  510. if sub_key == reverseBitsLowest<<1 {
  511. table = table[table_size:]
  512. table_bits = nextTableBitSize(count, int(len), root_bits)
  513. table_size = 1 << uint(table_bits)
  514. total_size += table_size
  515. sub_key = reverseBits8(key)
  516. key += key_step
  517. root_table[sub_key] = constructHuffmanCode(byte(table_bits+root_bits), uint16(uint64(uint(-cap(table)+cap(root_table)))-sub_key))
  518. sub_key = 0
  519. }
  520. symbol = int(symbolListGet(symbol_lists, symbol))
  521. code = constructHuffmanCode(byte(len-root_bits), uint16(symbol))
  522. replicateValue(table[reverseBits8(sub_key):], step, table_size, code)
  523. sub_key += sub_key_step
  524. }
  525. step <<= 1
  526. sub_key_step >>= 1
  527. }
  528. return uint32(total_size)
  529. }
  530. func buildSimpleHuffmanTable(table []huffmanCode, root_bits int, val []uint16, num_symbols uint32) uint32 {
  531. var table_size uint32 = 1
  532. var goal_size uint32 = 1 << uint(root_bits)
  533. switch num_symbols {
  534. case 0:
  535. table[0] = constructHuffmanCode(0, val[0])
  536. case 1:
  537. if val[1] > val[0] {
  538. table[0] = constructHuffmanCode(1, val[0])
  539. table[1] = constructHuffmanCode(1, val[1])
  540. } else {
  541. table[0] = constructHuffmanCode(1, val[1])
  542. table[1] = constructHuffmanCode(1, val[0])
  543. }
  544. table_size = 2
  545. case 2:
  546. table[0] = constructHuffmanCode(1, val[0])
  547. table[2] = constructHuffmanCode(1, val[0])
  548. if val[2] > val[1] {
  549. table[1] = constructHuffmanCode(2, val[1])
  550. table[3] = constructHuffmanCode(2, val[2])
  551. } else {
  552. table[1] = constructHuffmanCode(2, val[2])
  553. table[3] = constructHuffmanCode(2, val[1])
  554. }
  555. table_size = 4
  556. case 3:
  557. var i int
  558. var k int
  559. for i = 0; i < 3; i++ {
  560. for k = i + 1; k < 4; k++ {
  561. if val[k] < val[i] {
  562. var t uint16 = val[k]
  563. val[k] = val[i]
  564. val[i] = t
  565. }
  566. }
  567. }
  568. table[0] = constructHuffmanCode(2, val[0])
  569. table[2] = constructHuffmanCode(2, val[1])
  570. table[1] = constructHuffmanCode(2, val[2])
  571. table[3] = constructHuffmanCode(2, val[3])
  572. table_size = 4
  573. case 4:
  574. if val[3] < val[2] {
  575. var t uint16 = val[3]
  576. val[3] = val[2]
  577. val[2] = t
  578. }
  579. table[0] = constructHuffmanCode(1, val[0])
  580. table[1] = constructHuffmanCode(2, val[1])
  581. table[2] = constructHuffmanCode(1, val[0])
  582. table[3] = constructHuffmanCode(3, val[2])
  583. table[4] = constructHuffmanCode(1, val[0])
  584. table[5] = constructHuffmanCode(2, val[1])
  585. table[6] = constructHuffmanCode(1, val[0])
  586. table[7] = constructHuffmanCode(3, val[3])
  587. table_size = 8
  588. }
  589. for table_size != goal_size {
  590. copy(table[table_size:], table[:uint(table_size)])
  591. table_size <<= 1
  592. }
  593. return goal_size
  594. }