16
maxEncTablesize = 1 << maxTableLog
17
maxEncTableMask = (1 << maxTableLog) - 1
19
maxEncSymbolValue = maxMatchLengthSymbol
23
type fseEncoder struct {
45
symbolTT []symbolTransform
49
type symbolTransform struct {
56
func (s symbolTransform) String() string {
57
return fmt.Sprintf("{deltabits: %08x, findstate:%d outbits:%d}", s.deltaNbBits, s.deltaFindState, s.outBits)
65
func (s *fseEncoder) Histogram() *[256]uint32 {
73
func (s *fseEncoder) HistogramFinished(maxSymbol uint8, maxCount int) {
75
s.symbolLen = uint16(maxSymbol) + 1
76
s.clearCount = maxCount != 0
80
func (s *fseEncoder) prepare() (*fseEncoder, error) {
85
if s.clearCount && s.maxCount == 0 {
86
for i := range s.count {
96
func (s *fseEncoder) allocCtable() {
97
tableSize := 1 << s.actualTableLog
99
if cap(s.ct.tableSymbol) < tableSize {
100
s.ct.tableSymbol = make([]byte, tableSize)
102
s.ct.tableSymbol = s.ct.tableSymbol[:tableSize]
105
if cap(s.ct.stateTable) < ctSize {
106
s.ct.stateTable = make([]uint16, ctSize)
108
s.ct.stateTable = s.ct.stateTable[:ctSize]
110
if cap(s.ct.symbolTT) < 256 {
111
s.ct.symbolTT = make([]symbolTransform, 256)
113
s.ct.symbolTT = s.ct.symbolTT[:256]
117
func (s *fseEncoder) buildCTable() error {
118
tableSize := uint32(1 << s.actualTableLog)
119
highThreshold := tableSize - 1
123
tableSymbol := s.ct.tableSymbol[:tableSize]
127
for ui, v := range s.norm[:s.symbolLen-1] {
131
cumul[u+1] = cumul[u] + 1
132
tableSymbol[highThreshold] = u
135
cumul[u+1] = cumul[u] + v
139
u := int(s.symbolLen - 1)
140
v := s.norm[s.symbolLen-1]
143
cumul[u+1] = cumul[u] + 1
144
tableSymbol[highThreshold] = byte(u)
147
cumul[u+1] = cumul[u] + v
149
if uint32(cumul[s.symbolLen]) != tableSize {
150
return fmt.Errorf("internal error: expected cumul[s.symbolLen] (%d) == tableSize (%d)", cumul[s.symbolLen], tableSize)
152
cumul[s.symbolLen] = int16(tableSize) + 1
157
step := tableStep(tableSize)
158
tableMask := tableSize - 1
161
largeLimit := int16(1 << (s.actualTableLog - 1))
162
for ui, v := range s.norm[:s.symbolLen] {
167
for nbOccurrences := int16(0); nbOccurrences < v; nbOccurrences++ {
168
tableSymbol[position] = symbol
169
position = (position + step) & tableMask
170
for position > highThreshold {
171
position = (position + step) & tableMask
178
return errors.New("position!=0")
183
table := s.ct.stateTable
185
tsi := int(tableSize)
186
for u, v := range tableSymbol {
188
table[cumul[v]] = uint16(tsi + u)
196
symbolTT := s.ct.symbolTT[:s.symbolLen]
197
tableLog := s.actualTableLog
198
tl := (uint32(tableLog) << 16) - (1 << tableLog)
199
for i, v := range s.norm[:s.symbolLen] {
203
symbolTT[i].deltaNbBits = tl
204
symbolTT[i].deltaFindState = total - 1
207
maxBitsOut := uint32(tableLog) - highBit(uint32(v-1))
208
minStatePlus := uint32(v) << maxBitsOut
209
symbolTT[i].deltaNbBits = (maxBitsOut << 16) - minStatePlus
210
symbolTT[i].deltaFindState = total - v
214
if total != int16(tableSize) {
215
return fmt.Errorf("total mismatch %d (got) != %d (want)", total, tableSize)
221
var rtbTable = [...]uint32{0, 473195, 504333, 520860, 550000, 700000, 750000, 830000}
223
func (s *fseEncoder) setRLE(val byte) {
226
s.ct.stateTable = s.ct.stateTable[:1]
227
s.ct.symbolTT[val] = symbolTransform{
232
println("setRLE: val", val, "symbolTT", s.ct.symbolTT[val])
240
func (s *fseEncoder) setBits(transform []byte) {
241
if s.reUsed || s.preDefined {
245
if transform == nil {
246
s.ct.symbolTT[s.rleVal].outBits = s.rleVal
250
s.maxBits = transform[s.rleVal]
251
s.ct.symbolTT[s.rleVal].outBits = s.maxBits
254
if transform == nil {
255
for i := range s.ct.symbolTT[:s.symbolLen] {
256
s.ct.symbolTT[i].outBits = uint8(i)
258
s.maxBits = uint8(s.symbolLen - 1)
262
for i, v := range transform[:s.symbolLen] {
263
s.ct.symbolTT[i].outBits = v
274
func (s *fseEncoder) normalizeCount(length int) error {
278
s.optimalTableLog(length)
280
tableLog = s.actualTableLog
281
scale = 62 - uint64(tableLog)
282
step = (1 << 62) / uint64(length)
283
vStep = uint64(1) << (scale - 20)
284
stillToDistribute = int16(1 << tableLog)
287
lowThreshold = (uint32)(length >> tableLog)
289
if s.maxCount == length {
294
for i, cnt := range s.count[:s.symbolLen] {
302
if cnt <= lowThreshold {
306
proba := (int16)((uint64(cnt) * step) >> scale)
308
restToBeat := vStep * uint64(rtbTable[proba])
309
v := uint64(cnt)*step - (uint64(proba) << scale)
314
if proba > largestP {
319
stillToDistribute -= proba
323
if -stillToDistribute >= (s.norm[largest] >> 1) {
325
err := s.normalizeCount2(length)
330
err = s.validateNorm()
335
return s.buildCTable()
337
s.norm[largest] += stillToDistribute
339
err := s.validateNorm()
344
return s.buildCTable()
349
func (s *fseEncoder) normalizeCount2(length int) error {
350
const notYetAssigned = -2
353
total = uint32(length)
354
tableLog = s.actualTableLog
355
lowThreshold = total >> tableLog
356
lowOne = (total * 3) >> (tableLog + 1)
358
for i, cnt := range s.count[:s.symbolLen] {
363
if cnt <= lowThreshold {
375
s.norm[i] = notYetAssigned
377
toDistribute := (1 << tableLog) - distributed
379
if (total / toDistribute) > lowOne {
381
lowOne = (total * 3) / (toDistribute * 2)
382
for i, cnt := range s.count[:s.symbolLen] {
383
if (s.norm[i] == notYetAssigned) && (cnt <= lowOne) {
390
toDistribute = (1 << tableLog) - distributed
392
if distributed == uint32(s.symbolLen)+1 {
398
for i, cnt := range s.count[:s.symbolLen] {
404
s.norm[maxV] += int16(toDistribute)
410
for i := uint32(0); toDistribute > 0; i = (i + 1) % (uint32(s.symbolLen)) {
420
vStepLog = 62 - uint64(tableLog)
421
mid = uint64((1 << (vStepLog - 1)) - 1)
422
rStep = (((1 << vStepLog) * uint64(toDistribute)) + mid) / uint64(total)
425
for i, cnt := range s.count[:s.symbolLen] {
426
if s.norm[i] == notYetAssigned {
428
end = tmpTotal + uint64(cnt)*rStep
429
sStart = uint32(tmpTotal >> vStepLog)
430
sEnd = uint32(end >> vStepLog)
431
weight = sEnd - sStart
434
return errors.New("weight < 1")
436
s.norm[i] = int16(weight)
444
func (s *fseEncoder) optimalTableLog(length int) {
445
tableLog := uint8(maxEncTableLog)
446
minBitsSrc := highBit(uint32(length)) + 1
447
minBitsSymbols := highBit(uint32(s.symbolLen-1)) + 2
448
minBits := uint8(minBitsSymbols)
449
if minBitsSrc < minBitsSymbols {
450
minBits = uint8(minBitsSrc)
453
maxBitsSrc := uint8(highBit(uint32(length-1))) - 2
454
if maxBitsSrc < tableLog {
456
tableLog = maxBitsSrc
458
if minBits > tableLog {
462
if tableLog < minEncTablelog {
463
tableLog = minEncTablelog
465
if tableLog > maxEncTableLog {
466
tableLog = maxEncTableLog
468
s.actualTableLog = tableLog
472
func (s *fseEncoder) validateNorm() (err error) {
474
for _, v := range s.norm[:s.symbolLen] {
485
fmt.Printf("selected TableLog: %d, Symbol length: %d\n", s.actualTableLog, s.symbolLen)
486
for i, v := range s.norm[:s.symbolLen] {
487
fmt.Printf("%3d: %5d -> %4d \n", i, s.count[i], v)
490
if total != (1 << s.actualTableLog) {
491
return fmt.Errorf("warning: Total == %d != %d", total, 1<<s.actualTableLog)
493
for i, v := range s.count[s.symbolLen:] {
495
return fmt.Errorf("warning: Found symbol out of range, %d after cut", i)
503
func (s *fseEncoder) writeCount(out []byte) ([]byte, error) {
505
return append(out, s.rleVal), nil
507
if s.preDefined || s.reUsed {
513
tableLog = s.actualTableLog
514
tableSize = 1 << tableLog
519
maxHeaderSize = ((int(s.symbolLen) * int(tableLog)) >> 3) + 3 + 2
522
bitStream = uint32(tableLog - minEncTablelog)
524
remaining = int16(tableSize + 1)
525
threshold = int16(tableSize)
526
nbBits = uint(tableLog + 1)
529
if cap(out) < outP+maxHeaderSize {
530
out = append(out, make([]byte, maxHeaderSize*3)...)
531
out = out[:len(out)-maxHeaderSize*3]
533
out = out[:outP+maxHeaderSize]
539
for s.norm[charnum] == 0 {
542
for charnum >= start+24 {
544
bitStream += uint32(0xFFFF) << bitCount
545
out[outP] = byte(bitStream)
546
out[outP+1] = byte(bitStream >> 8)
550
for charnum >= start+3 {
552
bitStream += 3 << bitCount
555
bitStream += uint32(charnum-start) << bitCount
558
out[outP] = byte(bitStream)
559
out[outP+1] = byte(bitStream >> 8)
566
count := s.norm[charnum]
568
max := (2*threshold - 1) - remaining
575
if count >= threshold {
578
bitStream += uint32(count) << bitCount
584
previous0 = count == 1
586
return nil, errors.New("internal error: remaining < 1")
588
for remaining < threshold {
594
out[outP] = byte(bitStream)
595
out[outP+1] = byte(bitStream >> 8)
602
if outP+2 > len(out) {
603
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])
605
out[outP] = byte(bitStream)
606
out[outP+1] = byte(bitStream >> 8)
607
outP += int((bitCount + 7) / 8)
609
if charnum > s.symbolLen {
610
return nil, errors.New("internal error: charnum > s.symbolLen")
612
return out[:outP], nil
618
func (s *fseEncoder) bitCost(symbolValue uint8, accuracyLog uint32) uint32 {
619
minNbBits := s.ct.symbolTT[symbolValue].deltaNbBits >> 16
620
threshold := (minNbBits + 1) << 16
622
if !(s.actualTableLog < 16) {
623
panic("!s.actualTableLog < 16")
626
if !(uint8(accuracyLog) < 31-s.actualTableLog) {
627
panic("!uint8(accuracyLog) < 31-s.actualTableLog")
630
tableSize := uint32(1) << s.actualTableLog
631
deltaFromThreshold := threshold - (s.ct.symbolTT[symbolValue].deltaNbBits + tableSize)
633
normalizedDeltaFromThreshold := (deltaFromThreshold << accuracyLog) >> s.actualTableLog
634
bitMultiplier := uint32(1) << accuracyLog
636
if s.ct.symbolTT[symbolValue].deltaNbBits+tableSize > threshold {
637
panic("s.ct.symbolTT[symbolValue].deltaNbBits+tableSize > threshold")
639
if normalizedDeltaFromThreshold > bitMultiplier {
640
panic("normalizedDeltaFromThreshold > bitMultiplier")
643
return (minNbBits+1)*bitMultiplier - normalizedDeltaFromThreshold
649
func (s *fseEncoder) approxSize(hist []uint32) uint32 {
650
if int(s.symbolLen) < len(hist) {
652
return math.MaxUint32
656
return math.MaxUint32
658
const kAccuracyLog = 8
659
badCost := (uint32(s.actualTableLog) + 1) << kAccuracyLog
661
for i, v := range hist {
666
return math.MaxUint32
668
bitCost := s.bitCost(uint8(i), kAccuracyLog)
669
if bitCost > badCost {
670
return math.MaxUint32
674
return cost >> kAccuracyLog
679
func (s *fseEncoder) maxHeaderSize() uint32 {
686
return (((uint32(s.symbolLen) * uint32(s.actualTableLog)) >> 3) + 3) * 8
697
func (c *cState) init(bw *bitWriter, ct *cTable, first symbolTransform) {
699
c.stateTable = ct.stateTable
700
if len(c.stateTable) == 1 {
702
c.stateTable[0] = uint16(0)
706
nbBitsOut := (first.deltaNbBits + (1 << 15)) >> 16
707
im := int32((nbBitsOut << 16) - first.deltaNbBits)
708
lu := (im >> nbBitsOut) + int32(first.deltaFindState)
709
c.state = c.stateTable[lu]
713
func (c *cState) encode(symbolTT symbolTransform) {
714
nbBitsOut := (uint32(c.state) + symbolTT.deltaNbBits) >> 16
715
dstState := int32(c.state>>(nbBitsOut&15)) + int32(symbolTT.deltaFindState)
716
c.bw.addBits16NC(c.state, uint8(nbBitsOut))
717
c.state = c.stateTable[dstState]
721
func (c *cState) flush(tableLog uint8) {
723
c.bw.addBits16NC(c.state, tableLog)