| 1 |
package huff0 |
| 2 |
|
| 3 |
import "errors" |
| 4 |
|
| 5 |
// BuildCTable builds a Huffman compression table from a precomputed symbol |
| 6 |
// histogram and installs it as the previous (reuse) table on s. |
| 7 |
// |
| 8 |
// After this call: |
| 9 |
// - EstimateSize/CanUseTable can probe the table against other histograms. |
| 10 |
// - Compress1X/Compress4X with Reuse = ReusePolicyMust will encode without |
| 11 |
// emitting a new table header. |
| 12 |
// - TransferCTable can hand the table to a sibling Scratch. |
| 13 |
// |
| 14 |
// count[i] is the number of occurrences of symbol i. The histogram must have |
| 15 |
// at least 2 distinct non-zero symbols; ErrUseRLE is returned for a single |
| 16 |
// symbol and an error is returned for an empty histogram. |
| 17 |
func (s *Scratch) BuildCTable(count *[256]uint32) error { |
| 18 |
if s == nil { |
| 19 |
return errors.New("huff0: BuildCTable on nil Scratch") |
| 20 |
} |
| 21 |
if count == nil { |
| 22 |
return errors.New("huff0: nil count passed to BuildCTable") |
| 23 |
} |
| 24 |
var err error |
| 25 |
s, err = s.prepare(nil) |
| 26 |
if err != nil { |
| 27 |
return err |
| 28 |
} |
| 29 |
s.count = *count |
| 30 |
var total, maxCount int |
| 31 |
var symLen uint16 |
| 32 |
for i, v := range s.count { |
| 33 |
total += int(v) |
| 34 |
if int(v) > maxCount { |
| 35 |
maxCount = int(v) |
| 36 |
} |
| 37 |
if v != 0 { |
| 38 |
symLen = uint16(i) + 1 |
| 39 |
} |
| 40 |
} |
| 41 |
if total == 0 { |
| 42 |
return errors.New("huff0: empty histogram") |
| 43 |
} |
| 44 |
if symLen < 2 || maxCount == total { |
| 45 |
return ErrUseRLE |
| 46 |
} |
| 47 |
// huff0's internal rank table assumes total ≤ BlockSizeMax (it uses |
| 48 |
// highBit32(count+1) + 1 as a rank index into a fixed-size array). |
| 49 |
// Histograms summed across multiple blocks can exceed that; scale the |
| 50 |
// counts down preserving the distribution. Non-zero entries round up so |
| 51 |
// rare symbols stay representable. |
| 52 |
if total > BlockSizeMax { |
| 53 |
shift := uint(0) |
| 54 |
for total>>shift > BlockSizeMax { |
| 55 |
shift++ |
| 56 |
} |
| 57 |
round := uint32(1<<shift) - 1 |
| 58 |
var newTotal, newMax int |
| 59 |
for i, v := range s.count { |
| 60 |
if v == 0 { |
| 61 |
continue |
| 62 |
} |
| 63 |
scaled := (v + round) >> shift |
| 64 |
if scaled == 0 { |
| 65 |
scaled = 1 |
| 66 |
} |
| 67 |
s.count[i] = scaled |
| 68 |
newTotal += int(scaled) |
| 69 |
if int(scaled) > newMax { |
| 70 |
newMax = int(scaled) |
| 71 |
} |
| 72 |
} |
| 73 |
total = newTotal |
| 74 |
maxCount = newMax |
| 75 |
if maxCount == total { |
| 76 |
return ErrUseRLE |
| 77 |
} |
| 78 |
} |
| 79 |
s.symbolLen = symLen |
| 80 |
s.maxCount = maxCount |
| 81 |
s.srcLen = total |
| 82 |
if err := s.buildCTable(); err != nil { |
| 83 |
return err |
| 84 |
} |
| 85 |
if cap(s.prevTable) < len(s.cTable) { |
| 86 |
s.prevTable = make(cTable, 0, maxSymbolValue+1) |
| 87 |
} |
| 88 |
s.prevTable = s.prevTable[:len(s.cTable)] |
| 89 |
copy(s.prevTable, s.cTable) |
| 90 |
s.prevTableLog = s.actualTableLog |
| 91 |
// Force the next Compress* to recount from real input. |
| 92 |
s.clearCount = true |
| 93 |
s.maxCount = 0 |
| 94 |
return nil |
| 95 |
} |
| 96 |
|
| 97 |
// EstimateSize returns an estimated compressed payload size in bytes for the |
| 98 |
// supplied histogram using the table currently stored in prevTable. It returns |
| 99 |
// -1 when the table cannot encode every non-zero symbol of hist (i.e. when |
| 100 |
// CanUseTable would return false). The estimate excludes the table header. |
| 101 |
func (s *Scratch) EstimateSize(hist *[256]uint32) int { |
| 102 |
if s == nil || hist == nil || len(s.prevTable) == 0 { |
| 103 |
return -1 |
| 104 |
} |
| 105 |
pt := s.prevTable |
| 106 |
nbBits := uint32(7) |
| 107 |
for i, v := range hist { |
| 108 |
if v == 0 { |
| 109 |
continue |
| 110 |
} |
| 111 |
if i >= len(pt) || pt[i].nBits == 0 { |
| 112 |
return -1 |
| 113 |
} |
| 114 |
nbBits += uint32(pt[i].nBits) * v |
| 115 |
} |
| 116 |
return int(nbBits >> 3) |
| 117 |
} |
| 118 |
|
| 119 |
// CanUseTable reports whether the table in prevTable can encode every |
| 120 |
// non-zero symbol present in hist. |
| 121 |
func (s *Scratch) CanUseTable(hist *[256]uint32) bool { |
| 122 |
if s == nil || hist == nil || len(s.prevTable) == 0 { |
| 123 |
return false |
| 124 |
} |
| 125 |
pt := s.prevTable |
| 126 |
for i, v := range hist { |
| 127 |
if v == 0 { |
| 128 |
continue |
| 129 |
} |
| 130 |
if i >= len(pt) || pt[i].nBits == 0 { |
| 131 |
return false |
| 132 |
} |
| 133 |
} |
| 134 |
return true |
| 135 |
} |
| 136 |
|
| 137 |
// AppendTable serializes the table currently stored in prevTable (e.g. as |
| 138 |
// installed by BuildCTable or carried over from a previous Compress call) |
| 139 |
// into a self-delimiting zstd-style header and appends it to dst. The |
| 140 |
// returned slice can be parsed back by ReadTable. |
| 141 |
func (s *Scratch) AppendTable(dst []byte) ([]byte, error) { |
| 142 |
if s == nil || len(s.prevTable) == 0 { |
| 143 |
return dst, errors.New("huff0: AppendTable with empty table") |
| 144 |
} |
| 145 |
// cTable.write reads s.actualTableLog, s.symbolLen, s.huffWeight, s.fse |
| 146 |
// and writes into s.Out. Save/restore Out so we don't disturb in-flight |
| 147 |
// compression buffers. |
| 148 |
saveOut := s.Out |
| 149 |
saveTL := s.actualTableLog |
| 150 |
saveSL := s.symbolLen |
| 151 |
if s.fse == nil { |
| 152 |
// Lazily init in case AppendTable is called on a fresh Scratch. |
| 153 |
if _, err := s.prepare(nil); err != nil { |
| 154 |
return dst, err |
| 155 |
} |
| 156 |
saveOut = s.Out |
| 157 |
} |
| 158 |
s.Out = s.Out[:0] |
| 159 |
s.actualTableLog = s.prevTableLog |
| 160 |
s.symbolLen = uint16(len(s.prevTable)) |
| 161 |
if err := s.prevTable.write(s); err != nil { |
| 162 |
s.Out, s.actualTableLog, s.symbolLen = saveOut, saveTL, saveSL |
| 163 |
return dst, err |
| 164 |
} |
| 165 |
dst = append(dst, s.Out...) |
| 166 |
s.Out, s.actualTableLog, s.symbolLen = saveOut, saveTL, saveSL |
| 167 |
return dst, nil |
| 168 |
} |