fse_encoder.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  1. // Copyright 2019+ Klaus Post. All rights reserved.
  2. // License information can be found in the LICENSE file.
  3. // Based on work by Yann Collet, released under BSD License.
  4. package zstd
  5. import (
  6. "errors"
  7. "fmt"
  8. "math"
  9. )
  10. const (
  11. // For encoding we only support up to
  12. maxEncTableLog = 8
  13. maxEncTablesize = 1 << maxTableLog
  14. maxEncTableMask = (1 << maxTableLog) - 1
  15. minEncTablelog = 5
  16. maxEncSymbolValue = maxMatchLengthSymbol
  17. )
  18. // Scratch provides temporary storage for compression and decompression.
  19. type fseEncoder struct {
  20. symbolLen uint16 // Length of active part of the symbol table.
  21. actualTableLog uint8 // Selected tablelog.
  22. ct cTable // Compression tables.
  23. maxCount int // count of the most probable symbol
  24. zeroBits bool // no bits has prob > 50%.
  25. clearCount bool // clear count
  26. useRLE bool // This encoder is for RLE
  27. preDefined bool // This encoder is predefined.
  28. reUsed bool // Set to know when the encoder has been reused.
  29. rleVal uint8 // RLE Symbol
  30. maxBits uint8 // Maximum output bits after transform.
  31. // TODO: Technically zstd should be fine with 64 bytes.
  32. count [256]uint32
  33. norm [256]int16
  34. }
  35. // cTable contains tables used for compression.
  36. type cTable struct {
  37. tableSymbol []byte
  38. stateTable []uint16
  39. symbolTT []symbolTransform
  40. }
  41. // symbolTransform contains the state transform for a symbol.
  42. type symbolTransform struct {
  43. deltaNbBits uint32
  44. deltaFindState int16
  45. outBits uint8
  46. }
  47. // String prints values as a human readable string.
  48. func (s symbolTransform) String() string {
  49. return fmt.Sprintf("{deltabits: %08x, findstate:%d outbits:%d}", s.deltaNbBits, s.deltaFindState, s.outBits)
  50. }
  51. // Histogram allows to populate the histogram and skip that step in the compression,
  52. // It otherwise allows to inspect the histogram when compression is done.
  53. // To indicate that you have populated the histogram call HistogramFinished
  54. // with the value of the highest populated symbol, as well as the number of entries
  55. // in the most populated entry. These are accepted at face value.
  56. func (s *fseEncoder) Histogram() *[256]uint32 {
  57. return &s.count
  58. }
  59. // HistogramFinished can be called to indicate that the histogram has been populated.
  60. // maxSymbol is the index of the highest set symbol of the next data segment.
  61. // maxCount is the number of entries in the most populated entry.
  62. // These are accepted at face value.
  63. func (s *fseEncoder) HistogramFinished(maxSymbol uint8, maxCount int) {
  64. s.maxCount = maxCount
  65. s.symbolLen = uint16(maxSymbol) + 1
  66. s.clearCount = maxCount != 0
  67. }
  68. // allocCtable will allocate tables needed for compression.
  69. // If existing tables a re big enough, they are simply re-used.
  70. func (s *fseEncoder) allocCtable() {
  71. tableSize := 1 << s.actualTableLog
  72. // get tableSymbol that is big enough.
  73. if cap(s.ct.tableSymbol) < tableSize {
  74. s.ct.tableSymbol = make([]byte, tableSize)
  75. }
  76. s.ct.tableSymbol = s.ct.tableSymbol[:tableSize]
  77. ctSize := tableSize
  78. if cap(s.ct.stateTable) < ctSize {
  79. s.ct.stateTable = make([]uint16, ctSize)
  80. }
  81. s.ct.stateTable = s.ct.stateTable[:ctSize]
  82. if cap(s.ct.symbolTT) < 256 {
  83. s.ct.symbolTT = make([]symbolTransform, 256)
  84. }
  85. s.ct.symbolTT = s.ct.symbolTT[:256]
  86. }
  87. // buildCTable will populate the compression table so it is ready to be used.
  88. func (s *fseEncoder) buildCTable() error {
  89. tableSize := uint32(1 << s.actualTableLog)
  90. highThreshold := tableSize - 1
  91. var cumul [256]int16
  92. s.allocCtable()
  93. tableSymbol := s.ct.tableSymbol[:tableSize]
  94. // symbol start positions
  95. {
  96. cumul[0] = 0
  97. for ui, v := range s.norm[:s.symbolLen-1] {
  98. u := byte(ui) // one less than reference
  99. if v == -1 {
  100. // Low proba symbol
  101. cumul[u+1] = cumul[u] + 1
  102. tableSymbol[highThreshold] = u
  103. highThreshold--
  104. } else {
  105. cumul[u+1] = cumul[u] + v
  106. }
  107. }
  108. // Encode last symbol separately to avoid overflowing u
  109. u := int(s.symbolLen - 1)
  110. v := s.norm[s.symbolLen-1]
  111. if v == -1 {
  112. // Low proba symbol
  113. cumul[u+1] = cumul[u] + 1
  114. tableSymbol[highThreshold] = byte(u)
  115. highThreshold--
  116. } else {
  117. cumul[u+1] = cumul[u] + v
  118. }
  119. if uint32(cumul[s.symbolLen]) != tableSize {
  120. return fmt.Errorf("internal error: expected cumul[s.symbolLen] (%d) == tableSize (%d)", cumul[s.symbolLen], tableSize)
  121. }
  122. cumul[s.symbolLen] = int16(tableSize) + 1
  123. }
  124. // Spread symbols
  125. s.zeroBits = false
  126. {
  127. step := tableStep(tableSize)
  128. tableMask := tableSize - 1
  129. var position uint32
  130. // if any symbol > largeLimit, we may have 0 bits output.
  131. largeLimit := int16(1 << (s.actualTableLog - 1))
  132. for ui, v := range s.norm[:s.symbolLen] {
  133. symbol := byte(ui)
  134. if v > largeLimit {
  135. s.zeroBits = true
  136. }
  137. for nbOccurrences := int16(0); nbOccurrences < v; nbOccurrences++ {
  138. tableSymbol[position] = symbol
  139. position = (position + step) & tableMask
  140. for position > highThreshold {
  141. position = (position + step) & tableMask
  142. } /* Low proba area */
  143. }
  144. }
  145. // Check if we have gone through all positions
  146. if position != 0 {
  147. return errors.New("position!=0")
  148. }
  149. }
  150. // Build table
  151. table := s.ct.stateTable
  152. {
  153. tsi := int(tableSize)
  154. for u, v := range tableSymbol {
  155. // TableU16 : sorted by symbol order; gives next state value
  156. table[cumul[v]] = uint16(tsi + u)
  157. cumul[v]++
  158. }
  159. }
  160. // Build Symbol Transformation Table
  161. {
  162. total := int16(0)
  163. symbolTT := s.ct.symbolTT[:s.symbolLen]
  164. tableLog := s.actualTableLog
  165. tl := (uint32(tableLog) << 16) - (1 << tableLog)
  166. for i, v := range s.norm[:s.symbolLen] {
  167. switch v {
  168. case 0:
  169. case -1, 1:
  170. symbolTT[i].deltaNbBits = tl
  171. symbolTT[i].deltaFindState = total - 1
  172. total++
  173. default:
  174. maxBitsOut := uint32(tableLog) - highBit(uint32(v-1))
  175. minStatePlus := uint32(v) << maxBitsOut
  176. symbolTT[i].deltaNbBits = (maxBitsOut << 16) - minStatePlus
  177. symbolTT[i].deltaFindState = total - v
  178. total += v
  179. }
  180. }
  181. if total != int16(tableSize) {
  182. return fmt.Errorf("total mismatch %d (got) != %d (want)", total, tableSize)
  183. }
  184. }
  185. return nil
  186. }
  187. var rtbTable = [...]uint32{0, 473195, 504333, 520860, 550000, 700000, 750000, 830000}
  188. func (s *fseEncoder) setRLE(val byte) {
  189. s.allocCtable()
  190. s.actualTableLog = 0
  191. s.ct.stateTable = s.ct.stateTable[:1]
  192. s.ct.symbolTT[val] = symbolTransform{
  193. deltaFindState: 0,
  194. deltaNbBits: 0,
  195. }
  196. if debugEncoder {
  197. println("setRLE: val", val, "symbolTT", s.ct.symbolTT[val])
  198. }
  199. s.rleVal = val
  200. s.useRLE = true
  201. }
  202. // setBits will set output bits for the transform.
  203. // if nil is provided, the number of bits is equal to the index.
  204. func (s *fseEncoder) setBits(transform []byte) {
  205. if s.reUsed || s.preDefined {
  206. return
  207. }
  208. if s.useRLE {
  209. if transform == nil {
  210. s.ct.symbolTT[s.rleVal].outBits = s.rleVal
  211. s.maxBits = s.rleVal
  212. return
  213. }
  214. s.maxBits = transform[s.rleVal]
  215. s.ct.symbolTT[s.rleVal].outBits = s.maxBits
  216. return
  217. }
  218. if transform == nil {
  219. for i := range s.ct.symbolTT[:s.symbolLen] {
  220. s.ct.symbolTT[i].outBits = uint8(i)
  221. }
  222. s.maxBits = uint8(s.symbolLen - 1)
  223. return
  224. }
  225. s.maxBits = 0
  226. for i, v := range transform[:s.symbolLen] {
  227. s.ct.symbolTT[i].outBits = v
  228. if v > s.maxBits {
  229. // We could assume bits always going up, but we play safe.
  230. s.maxBits = v
  231. }
  232. }
  233. }
  234. // normalizeCount will normalize the count of the symbols so
  235. // the total is equal to the table size.
  236. // If successful, compression tables will also be made ready.
  237. func (s *fseEncoder) normalizeCount(length int) error {
  238. if s.reUsed {
  239. return nil
  240. }
  241. s.optimalTableLog(length)
  242. var (
  243. tableLog = s.actualTableLog
  244. scale = 62 - uint64(tableLog)
  245. step = (1 << 62) / uint64(length)
  246. vStep = uint64(1) << (scale - 20)
  247. stillToDistribute = int16(1 << tableLog)
  248. largest int
  249. largestP int16
  250. lowThreshold = (uint32)(length >> tableLog)
  251. )
  252. if s.maxCount == length {
  253. s.useRLE = true
  254. return nil
  255. }
  256. s.useRLE = false
  257. for i, cnt := range s.count[:s.symbolLen] {
  258. // already handled
  259. // if (count[s] == s.length) return 0; /* rle special case */
  260. if cnt == 0 {
  261. s.norm[i] = 0
  262. continue
  263. }
  264. if cnt <= lowThreshold {
  265. s.norm[i] = -1
  266. stillToDistribute--
  267. } else {
  268. proba := (int16)((uint64(cnt) * step) >> scale)
  269. if proba < 8 {
  270. restToBeat := vStep * uint64(rtbTable[proba])
  271. v := uint64(cnt)*step - (uint64(proba) << scale)
  272. if v > restToBeat {
  273. proba++
  274. }
  275. }
  276. if proba > largestP {
  277. largestP = proba
  278. largest = i
  279. }
  280. s.norm[i] = proba
  281. stillToDistribute -= proba
  282. }
  283. }
  284. if -stillToDistribute >= (s.norm[largest] >> 1) {
  285. // corner case, need another normalization method
  286. err := s.normalizeCount2(length)
  287. if err != nil {
  288. return err
  289. }
  290. if debugAsserts {
  291. err = s.validateNorm()
  292. if err != nil {
  293. return err
  294. }
  295. }
  296. return s.buildCTable()
  297. }
  298. s.norm[largest] += stillToDistribute
  299. if debugAsserts {
  300. err := s.validateNorm()
  301. if err != nil {
  302. return err
  303. }
  304. }
  305. return s.buildCTable()
  306. }
  307. // Secondary normalization method.
  308. // To be used when primary method fails.
  309. func (s *fseEncoder) normalizeCount2(length int) error {
  310. const notYetAssigned = -2
  311. var (
  312. distributed uint32
  313. total = uint32(length)
  314. tableLog = s.actualTableLog
  315. lowThreshold = total >> tableLog
  316. lowOne = (total * 3) >> (tableLog + 1)
  317. )
  318. for i, cnt := range s.count[:s.symbolLen] {
  319. if cnt == 0 {
  320. s.norm[i] = 0
  321. continue
  322. }
  323. if cnt <= lowThreshold {
  324. s.norm[i] = -1
  325. distributed++
  326. total -= cnt
  327. continue
  328. }
  329. if cnt <= lowOne {
  330. s.norm[i] = 1
  331. distributed++
  332. total -= cnt
  333. continue
  334. }
  335. s.norm[i] = notYetAssigned
  336. }
  337. toDistribute := (1 << tableLog) - distributed
  338. if (total / toDistribute) > lowOne {
  339. // risk of rounding to zero
  340. lowOne = (total * 3) / (toDistribute * 2)
  341. for i, cnt := range s.count[:s.symbolLen] {
  342. if (s.norm[i] == notYetAssigned) && (cnt <= lowOne) {
  343. s.norm[i] = 1
  344. distributed++
  345. total -= cnt
  346. continue
  347. }
  348. }
  349. toDistribute = (1 << tableLog) - distributed
  350. }
  351. if distributed == uint32(s.symbolLen)+1 {
  352. // all values are pretty poor;
  353. // probably incompressible data (should have already been detected);
  354. // find max, then give all remaining points to max
  355. var maxV int
  356. var maxC uint32
  357. for i, cnt := range s.count[:s.symbolLen] {
  358. if cnt > maxC {
  359. maxV = i
  360. maxC = cnt
  361. }
  362. }
  363. s.norm[maxV] += int16(toDistribute)
  364. return nil
  365. }
  366. if total == 0 {
  367. // all of the symbols were low enough for the lowOne or lowThreshold
  368. for i := uint32(0); toDistribute > 0; i = (i + 1) % (uint32(s.symbolLen)) {
  369. if s.norm[i] > 0 {
  370. toDistribute--
  371. s.norm[i]++
  372. }
  373. }
  374. return nil
  375. }
  376. var (
  377. vStepLog = 62 - uint64(tableLog)
  378. mid = uint64((1 << (vStepLog - 1)) - 1)
  379. rStep = (((1 << vStepLog) * uint64(toDistribute)) + mid) / uint64(total) // scale on remaining
  380. tmpTotal = mid
  381. )
  382. for i, cnt := range s.count[:s.symbolLen] {
  383. if s.norm[i] == notYetAssigned {
  384. var (
  385. end = tmpTotal + uint64(cnt)*rStep
  386. sStart = uint32(tmpTotal >> vStepLog)
  387. sEnd = uint32(end >> vStepLog)
  388. weight = sEnd - sStart
  389. )
  390. if weight < 1 {
  391. return errors.New("weight < 1")
  392. }
  393. s.norm[i] = int16(weight)
  394. tmpTotal = end
  395. }
  396. }
  397. return nil
  398. }
  399. // optimalTableLog calculates and sets the optimal tableLog in s.actualTableLog
  400. func (s *fseEncoder) optimalTableLog(length int) {
  401. tableLog := uint8(maxEncTableLog)
  402. minBitsSrc := highBit(uint32(length)) + 1
  403. minBitsSymbols := highBit(uint32(s.symbolLen-1)) + 2
  404. minBits := uint8(minBitsSymbols)
  405. if minBitsSrc < minBitsSymbols {
  406. minBits = uint8(minBitsSrc)
  407. }
  408. maxBitsSrc := uint8(highBit(uint32(length-1))) - 2
  409. if maxBitsSrc < tableLog {
  410. // Accuracy can be reduced
  411. tableLog = maxBitsSrc
  412. }
  413. if minBits > tableLog {
  414. tableLog = minBits
  415. }
  416. // Need a minimum to safely represent all symbol values
  417. if tableLog < minEncTablelog {
  418. tableLog = minEncTablelog
  419. }
  420. if tableLog > maxEncTableLog {
  421. tableLog = maxEncTableLog
  422. }
  423. s.actualTableLog = tableLog
  424. }
  425. // validateNorm validates the normalized histogram table.
  426. func (s *fseEncoder) validateNorm() (err error) {
  427. var total int
  428. for _, v := range s.norm[:s.symbolLen] {
  429. if v >= 0 {
  430. total += int(v)
  431. } else {
  432. total -= int(v)
  433. }
  434. }
  435. defer func() {
  436. if err == nil {
  437. return
  438. }
  439. fmt.Printf("selected TableLog: %d, Symbol length: %d\n", s.actualTableLog, s.symbolLen)
  440. for i, v := range s.norm[:s.symbolLen] {
  441. fmt.Printf("%3d: %5d -> %4d \n", i, s.count[i], v)
  442. }
  443. }()
  444. if total != (1 << s.actualTableLog) {
  445. return fmt.Errorf("warning: Total == %d != %d", total, 1<<s.actualTableLog)
  446. }
  447. for i, v := range s.count[s.symbolLen:] {
  448. if v != 0 {
  449. return fmt.Errorf("warning: Found symbol out of range, %d after cut", i)
  450. }
  451. }
  452. return nil
  453. }
  454. // writeCount will write the normalized histogram count to header.
  455. // This is read back by readNCount.
  456. func (s *fseEncoder) writeCount(out []byte) ([]byte, error) {
  457. if s.useRLE {
  458. return append(out, s.rleVal), nil
  459. }
  460. if s.preDefined || s.reUsed {
  461. // Never write predefined.
  462. return out, nil
  463. }
  464. var (
  465. tableLog = s.actualTableLog
  466. tableSize = 1 << tableLog
  467. previous0 bool
  468. charnum uint16
  469. // maximum header size plus 2 extra bytes for final output if bitCount == 0.
  470. maxHeaderSize = ((int(s.symbolLen) * int(tableLog)) >> 3) + 3 + 2
  471. // Write Table Size
  472. bitStream = uint32(tableLog - minEncTablelog)
  473. bitCount = uint(4)
  474. remaining = int16(tableSize + 1) /* +1 for extra accuracy */
  475. threshold = int16(tableSize)
  476. nbBits = uint(tableLog + 1)
  477. outP = len(out)
  478. )
  479. if cap(out) < outP+maxHeaderSize {
  480. out = append(out, make([]byte, maxHeaderSize*3)...)
  481. out = out[:len(out)-maxHeaderSize*3]
  482. }
  483. out = out[:outP+maxHeaderSize]
  484. // stops at 1
  485. for remaining > 1 {
  486. if previous0 {
  487. start := charnum
  488. for s.norm[charnum] == 0 {
  489. charnum++
  490. }
  491. for charnum >= start+24 {
  492. start += 24
  493. bitStream += uint32(0xFFFF) << bitCount
  494. out[outP] = byte(bitStream)
  495. out[outP+1] = byte(bitStream >> 8)
  496. outP += 2
  497. bitStream >>= 16
  498. }
  499. for charnum >= start+3 {
  500. start += 3
  501. bitStream += 3 << bitCount
  502. bitCount += 2
  503. }
  504. bitStream += uint32(charnum-start) << bitCount
  505. bitCount += 2
  506. if bitCount > 16 {
  507. out[outP] = byte(bitStream)
  508. out[outP+1] = byte(bitStream >> 8)
  509. outP += 2
  510. bitStream >>= 16
  511. bitCount -= 16
  512. }
  513. }
  514. count := s.norm[charnum]
  515. charnum++
  516. max := (2*threshold - 1) - remaining
  517. if count < 0 {
  518. remaining += count
  519. } else {
  520. remaining -= count
  521. }
  522. count++ // +1 for extra accuracy
  523. if count >= threshold {
  524. count += max // [0..max[ [max..threshold[ (...) [threshold+max 2*threshold[
  525. }
  526. bitStream += uint32(count) << bitCount
  527. bitCount += nbBits
  528. if count < max {
  529. bitCount--
  530. }
  531. previous0 = count == 1
  532. if remaining < 1 {
  533. return nil, errors.New("internal error: remaining < 1")
  534. }
  535. for remaining < threshold {
  536. nbBits--
  537. threshold >>= 1
  538. }
  539. if bitCount > 16 {
  540. out[outP] = byte(bitStream)
  541. out[outP+1] = byte(bitStream >> 8)
  542. outP += 2
  543. bitStream >>= 16
  544. bitCount -= 16
  545. }
  546. }
  547. if outP+2 > len(out) {
  548. return nil, fmt.Errorf("internal error: %d > %d, maxheader: %d, sl: %d, tl: %d, normcount: %v", outP+2, len(out), maxHeaderSize, s.symbolLen, int(tableLog), s.norm[:s.symbolLen])
  549. }
  550. out[outP] = byte(bitStream)
  551. out[outP+1] = byte(bitStream >> 8)
  552. outP += int((bitCount + 7) / 8)
  553. if charnum > s.symbolLen {
  554. return nil, errors.New("internal error: charnum > s.symbolLen")
  555. }
  556. return out[:outP], nil
  557. }
  558. // Approximate symbol cost, as fractional value, using fixed-point format (accuracyLog fractional bits)
  559. // note 1 : assume symbolValue is valid (<= maxSymbolValue)
  560. // note 2 : if freq[symbolValue]==0, @return a fake cost of tableLog+1 bits *
  561. func (s *fseEncoder) bitCost(symbolValue uint8, accuracyLog uint32) uint32 {
  562. minNbBits := s.ct.symbolTT[symbolValue].deltaNbBits >> 16
  563. threshold := (minNbBits + 1) << 16
  564. if debugAsserts {
  565. if !(s.actualTableLog < 16) {
  566. panic("!s.actualTableLog < 16")
  567. }
  568. // ensure enough room for renormalization double shift
  569. if !(uint8(accuracyLog) < 31-s.actualTableLog) {
  570. panic("!uint8(accuracyLog) < 31-s.actualTableLog")
  571. }
  572. }
  573. tableSize := uint32(1) << s.actualTableLog
  574. deltaFromThreshold := threshold - (s.ct.symbolTT[symbolValue].deltaNbBits + tableSize)
  575. // linear interpolation (very approximate)
  576. normalizedDeltaFromThreshold := (deltaFromThreshold << accuracyLog) >> s.actualTableLog
  577. bitMultiplier := uint32(1) << accuracyLog
  578. if debugAsserts {
  579. if s.ct.symbolTT[symbolValue].deltaNbBits+tableSize > threshold {
  580. panic("s.ct.symbolTT[symbolValue].deltaNbBits+tableSize > threshold")
  581. }
  582. if normalizedDeltaFromThreshold > bitMultiplier {
  583. panic("normalizedDeltaFromThreshold > bitMultiplier")
  584. }
  585. }
  586. return (minNbBits+1)*bitMultiplier - normalizedDeltaFromThreshold
  587. }
  588. // Returns the cost in bits of encoding the distribution in count using ctable.
  589. // Histogram should only be up to the last non-zero symbol.
  590. // Returns an -1 if ctable cannot represent all the symbols in count.
  591. func (s *fseEncoder) approxSize(hist []uint32) uint32 {
  592. if int(s.symbolLen) < len(hist) {
  593. // More symbols than we have.
  594. return math.MaxUint32
  595. }
  596. if s.useRLE {
  597. // We will never reuse RLE encoders.
  598. return math.MaxUint32
  599. }
  600. const kAccuracyLog = 8
  601. badCost := (uint32(s.actualTableLog) + 1) << kAccuracyLog
  602. var cost uint32
  603. for i, v := range hist {
  604. if v == 0 {
  605. continue
  606. }
  607. if s.norm[i] == 0 {
  608. return math.MaxUint32
  609. }
  610. bitCost := s.bitCost(uint8(i), kAccuracyLog)
  611. if bitCost > badCost {
  612. return math.MaxUint32
  613. }
  614. cost += v * bitCost
  615. }
  616. return cost >> kAccuracyLog
  617. }
  618. // maxHeaderSize returns the maximum header size in bits.
  619. // This is not exact size, but we want a penalty for new tables anyway.
  620. func (s *fseEncoder) maxHeaderSize() uint32 {
  621. if s.preDefined {
  622. return 0
  623. }
  624. if s.useRLE {
  625. return 8
  626. }
  627. return (((uint32(s.symbolLen) * uint32(s.actualTableLog)) >> 3) + 3) * 8
  628. }
  629. // cState contains the compression state of a stream.
  630. type cState struct {
  631. bw *bitWriter
  632. stateTable []uint16
  633. state uint16
  634. }
  635. // init will initialize the compression state to the first symbol of the stream.
  636. func (c *cState) init(bw *bitWriter, ct *cTable, first symbolTransform) {
  637. c.bw = bw
  638. c.stateTable = ct.stateTable
  639. if len(c.stateTable) == 1 {
  640. // RLE
  641. c.stateTable[0] = uint16(0)
  642. c.state = 0
  643. return
  644. }
  645. nbBitsOut := (first.deltaNbBits + (1 << 15)) >> 16
  646. im := int32((nbBitsOut << 16) - first.deltaNbBits)
  647. lu := (im >> nbBitsOut) + int32(first.deltaFindState)
  648. c.state = c.stateTable[lu]
  649. }
  650. // flush will write the tablelog to the output and flush the remaining full bytes.
  651. func (c *cState) flush(tableLog uint8) {
  652. c.bw.flush32()
  653. c.bw.addBits16NC(c.state, tableLog)
  654. }