Start un-exporting names.
This commit is contained in:
parent
1ce1c68433
commit
5001b3d0d7
|
@ -19,7 +19,7 @@ package brotli
|
|||
initially the total amount of commands output by previous
|
||||
CreateBackwardReferences calls, and must be incremented by the amount written
|
||||
by this call. */
|
||||
func ComputeDistanceCode(distance uint, max_distance uint, dist_cache []int) uint {
|
||||
func computeDistanceCode(distance uint, max_distance uint, dist_cache []int) uint {
|
||||
if distance <= max_distance {
|
||||
var distance_plus_3 uint = distance + 3
|
||||
var offset0 uint = distance_plus_3 - uint(dist_cache[0])
|
||||
|
@ -42,7 +42,7 @@ func ComputeDistanceCode(distance uint, max_distance uint, dist_cache []int) uin
|
|||
return distance + BROTLI_NUM_DISTANCE_SHORT_CODES - 1
|
||||
}
|
||||
|
||||
func BrotliCreateBackwardReferences(num_bytes uint, position uint, ringbuffer []byte, ringbuffer_mask uint, params *BrotliEncoderParams, hasher HasherHandle, dist_cache []int, last_insert_len *uint, commands []Command, num_commands *uint, num_literals *uint) {
|
||||
func createBackwardReferences(num_bytes uint, position uint, ringbuffer []byte, ringbuffer_mask uint, params *BrotliEncoderParams, hasher HasherHandle, dist_cache []int, last_insert_len *uint, commands []Command, num_commands *uint, num_literals *uint) {
|
||||
var max_backward_limit uint = BROTLI_MAX_BACKWARD_LIMIT(params.lgwin)
|
||||
var orig_commands []Command = commands
|
||||
var insert_length uint = *last_insert_len
|
||||
|
@ -112,7 +112,7 @@ func BrotliCreateBackwardReferences(num_bytes uint, position uint, ringbuffer []
|
|||
{
|
||||
/* The first 16 codes are special short-codes,
|
||||
and the minimum offset is 1. */
|
||||
var distance_code uint = ComputeDistanceCode(sr.distance, max_distance+gap, dist_cache)
|
||||
var distance_code uint = computeDistanceCode(sr.distance, max_distance+gap, dist_cache)
|
||||
if (sr.distance <= (max_distance + gap)) && distance_code > 0 {
|
||||
dist_cache[3] = dist_cache[2]
|
||||
dist_cache[2] = dist_cache[1]
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package brotli
|
||||
|
||||
type ZopfliNode struct {
|
||||
type zopfliNode struct {
|
||||
length uint32
|
||||
distance uint32
|
||||
dcode_insert_length uint32
|
||||
|
@ -24,7 +24,7 @@ type ZopfliNode struct {
|
|||
(1) nodes[i].copy_length() >= 2
|
||||
(2) nodes[i].command_length() <= i and
|
||||
(3) nodes[i - nodes[i].command_length()].cost < kInfinity */
|
||||
const BROTLI_MAX_EFFECTIVE_DISTANCE_ALPHABET_SIZE = 544
|
||||
const maxEffectiveDistanceAlphabetSize = 544
|
||||
|
||||
var kInfinity float32 = 1.7e38 /* ~= 2 ^ 127 */
|
||||
|
||||
|
@ -32,8 +32,8 @@ var kDistanceCacheIndex = []uint32{0, 1, 2, 3, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
|
|||
|
||||
var kDistanceCacheOffset = []int{0, 0, 0, 0, -1, 1, -2, 2, -3, 3, -1, 1, -2, 2, -3, 3}
|
||||
|
||||
func BrotliInitZopfliNodes(array []ZopfliNode, length uint) {
|
||||
var stub ZopfliNode
|
||||
func initZopfliNodes(array []zopfliNode, length uint) {
|
||||
var stub zopfliNode
|
||||
var i uint
|
||||
stub.length = 1
|
||||
stub.distance = 0
|
||||
|
@ -44,34 +44,34 @@ func BrotliInitZopfliNodes(array []ZopfliNode, length uint) {
|
|||
}
|
||||
}
|
||||
|
||||
func ZopfliNodeCopyLength(self *ZopfliNode) uint32 {
|
||||
func zopfliNodeCopyLength(self *zopfliNode) uint32 {
|
||||
return self.length & 0x1FFFFFF
|
||||
}
|
||||
|
||||
func ZopfliNodeLengthCode(self *ZopfliNode) uint32 {
|
||||
func zopfliNodeLengthCode(self *zopfliNode) uint32 {
|
||||
var modifier uint32 = self.length >> 25
|
||||
return ZopfliNodeCopyLength(self) + 9 - modifier
|
||||
return zopfliNodeCopyLength(self) + 9 - modifier
|
||||
}
|
||||
|
||||
func ZopfliNodeCopyDistance(self *ZopfliNode) uint32 {
|
||||
func zopfliNodeCopyDistance(self *zopfliNode) uint32 {
|
||||
return self.distance
|
||||
}
|
||||
|
||||
func ZopfliNodeDistanceCode(self *ZopfliNode) uint32 {
|
||||
func zopfliNodeDistanceCode(self *zopfliNode) uint32 {
|
||||
var short_code uint32 = self.dcode_insert_length >> 27
|
||||
if short_code == 0 {
|
||||
return ZopfliNodeCopyDistance(self) + BROTLI_NUM_DISTANCE_SHORT_CODES - 1
|
||||
return zopfliNodeCopyDistance(self) + BROTLI_NUM_DISTANCE_SHORT_CODES - 1
|
||||
} else {
|
||||
return short_code - 1
|
||||
}
|
||||
}
|
||||
|
||||
func ZopfliNodeCommandLength(self *ZopfliNode) uint32 {
|
||||
return ZopfliNodeCopyLength(self) + (self.dcode_insert_length & 0x7FFFFFF)
|
||||
func zopfliNodeCommandLength(self *zopfliNode) uint32 {
|
||||
return zopfliNodeCopyLength(self) + (self.dcode_insert_length & 0x7FFFFFF)
|
||||
}
|
||||
|
||||
/* Histogram based cost model for zopflification. */
|
||||
type ZopfliCostModel struct {
|
||||
type zopfliCostModel struct {
|
||||
cost_cmd_ [BROTLI_NUM_COMMAND_SYMBOLS]float32
|
||||
cost_dist_ []float32
|
||||
distance_histogram_size uint32
|
||||
|
@ -80,10 +80,10 @@ type ZopfliCostModel struct {
|
|||
num_bytes_ uint
|
||||
}
|
||||
|
||||
func InitZopfliCostModel(self *ZopfliCostModel, dist *BrotliDistanceParams, num_bytes uint) {
|
||||
func initZopfliCostModel(self *zopfliCostModel, dist *BrotliDistanceParams, num_bytes uint) {
|
||||
var distance_histogram_size uint32 = dist.alphabet_size
|
||||
if distance_histogram_size > BROTLI_MAX_EFFECTIVE_DISTANCE_ALPHABET_SIZE {
|
||||
distance_histogram_size = BROTLI_MAX_EFFECTIVE_DISTANCE_ALPHABET_SIZE
|
||||
if distance_histogram_size > maxEffectiveDistanceAlphabetSize {
|
||||
distance_histogram_size = maxEffectiveDistanceAlphabetSize
|
||||
}
|
||||
|
||||
self.num_bytes_ = num_bytes
|
||||
|
@ -92,12 +92,12 @@ func InitZopfliCostModel(self *ZopfliCostModel, dist *BrotliDistanceParams, num_
|
|||
self.distance_histogram_size = distance_histogram_size
|
||||
}
|
||||
|
||||
func CleanupZopfliCostModel(self *ZopfliCostModel) {
|
||||
func cleanupZopfliCostModel(self *zopfliCostModel) {
|
||||
self.literal_costs_ = nil
|
||||
self.cost_dist_ = nil
|
||||
}
|
||||
|
||||
func SetCost(histogram []uint32, histogram_size uint, literal_histogram bool, cost []float32) {
|
||||
func setCost(histogram []uint32, histogram_size uint, literal_histogram bool, cost []float32) {
|
||||
var sum uint = 0
|
||||
var missing_symbol_sum uint
|
||||
var log2sum float32
|
||||
|
@ -134,10 +134,10 @@ func SetCost(histogram []uint32, histogram_size uint, literal_histogram bool, co
|
|||
}
|
||||
}
|
||||
|
||||
func ZopfliCostModelSetFromCommands(self *ZopfliCostModel, position uint, ringbuffer []byte, ringbuffer_mask uint, commands []Command, num_commands uint, last_insert_len uint) {
|
||||
func zopfliCostModelSetFromCommands(self *zopfliCostModel, position uint, ringbuffer []byte, ringbuffer_mask uint, commands []Command, num_commands uint, last_insert_len uint) {
|
||||
var histogram_literal [BROTLI_NUM_LITERAL_SYMBOLS]uint32
|
||||
var histogram_cmd [BROTLI_NUM_COMMAND_SYMBOLS]uint32
|
||||
var histogram_dist [BROTLI_MAX_EFFECTIVE_DISTANCE_ALPHABET_SIZE]uint32
|
||||
var histogram_dist [maxEffectiveDistanceAlphabetSize]uint32
|
||||
var cost_literal [BROTLI_NUM_LITERAL_SYMBOLS]float32
|
||||
var pos uint = position - last_insert_len
|
||||
var min_cost_cmd float32 = kInfinity
|
||||
|
@ -147,7 +147,7 @@ func ZopfliCostModelSetFromCommands(self *ZopfliCostModel, position uint, ringbu
|
|||
|
||||
histogram_literal = [BROTLI_NUM_LITERAL_SYMBOLS]uint32{}
|
||||
histogram_cmd = [BROTLI_NUM_COMMAND_SYMBOLS]uint32{}
|
||||
histogram_dist = [BROTLI_MAX_EFFECTIVE_DISTANCE_ALPHABET_SIZE]uint32{}
|
||||
histogram_dist = [maxEffectiveDistanceAlphabetSize]uint32{}
|
||||
|
||||
for i = 0; i < num_commands; i++ {
|
||||
var inslength uint = uint(commands[i].insert_len_)
|
||||
|
@ -168,9 +168,9 @@ func ZopfliCostModelSetFromCommands(self *ZopfliCostModel, position uint, ringbu
|
|||
pos += inslength + copylength
|
||||
}
|
||||
|
||||
SetCost(histogram_literal[:], BROTLI_NUM_LITERAL_SYMBOLS, true, cost_literal[:])
|
||||
SetCost(histogram_cmd[:], BROTLI_NUM_COMMAND_SYMBOLS, false, cost_cmd)
|
||||
SetCost(histogram_dist[:], uint(self.distance_histogram_size), false, self.cost_dist_)
|
||||
setCost(histogram_literal[:], BROTLI_NUM_LITERAL_SYMBOLS, true, cost_literal[:])
|
||||
setCost(histogram_cmd[:], BROTLI_NUM_COMMAND_SYMBOLS, false, cost_cmd)
|
||||
setCost(histogram_dist[:], uint(self.distance_histogram_size), false, self.cost_dist_)
|
||||
|
||||
for i = 0; i < BROTLI_NUM_COMMAND_SYMBOLS; i++ {
|
||||
min_cost_cmd = brotli_min_float(min_cost_cmd, cost_cmd[i])
|
||||
|
@ -190,7 +190,7 @@ func ZopfliCostModelSetFromCommands(self *ZopfliCostModel, position uint, ringbu
|
|||
}
|
||||
}
|
||||
|
||||
func ZopfliCostModelSetFromLiteralCosts(self *ZopfliCostModel, position uint, ringbuffer []byte, ringbuffer_mask uint) {
|
||||
func zopfliCostModelSetFromLiteralCosts(self *zopfliCostModel, position uint, ringbuffer []byte, ringbuffer_mask uint) {
|
||||
var literal_costs []float32 = self.literal_costs_
|
||||
var literal_carry float32 = 0.0
|
||||
var cost_dist []float32 = self.cost_dist_
|
||||
|
@ -216,34 +216,34 @@ func ZopfliCostModelSetFromLiteralCosts(self *ZopfliCostModel, position uint, ri
|
|||
self.min_cost_cmd_ = float32(FastLog2(11))
|
||||
}
|
||||
|
||||
func ZopfliCostModelGetCommandCost(self *ZopfliCostModel, cmdcode uint16) float32 {
|
||||
func zopfliCostModelGetCommandCost(self *zopfliCostModel, cmdcode uint16) float32 {
|
||||
return self.cost_cmd_[cmdcode]
|
||||
}
|
||||
|
||||
func ZopfliCostModelGetDistanceCost(self *ZopfliCostModel, distcode uint) float32 {
|
||||
func zopfliCostModelGetDistanceCost(self *zopfliCostModel, distcode uint) float32 {
|
||||
return self.cost_dist_[distcode]
|
||||
}
|
||||
|
||||
func ZopfliCostModelGetLiteralCosts(self *ZopfliCostModel, from uint, to uint) float32 {
|
||||
func zopfliCostModelGetLiteralCosts(self *zopfliCostModel, from uint, to uint) float32 {
|
||||
return self.literal_costs_[to] - self.literal_costs_[from]
|
||||
}
|
||||
|
||||
func ZopfliCostModelGetMinCostCmd(self *ZopfliCostModel) float32 {
|
||||
func zopfliCostModelGetMinCostCmd(self *zopfliCostModel) float32 {
|
||||
return self.min_cost_cmd_
|
||||
}
|
||||
|
||||
/* REQUIRES: len >= 2, start_pos <= pos */
|
||||
/* REQUIRES: cost < kInfinity, nodes[start_pos].cost < kInfinity */
|
||||
/* Maintains the "ZopfliNode array invariant". */
|
||||
func UpdateZopfliNode(nodes []ZopfliNode, pos uint, start_pos uint, len uint, len_code uint, dist uint, short_code uint, cost float32) {
|
||||
var next *ZopfliNode = &nodes[pos+len]
|
||||
func updateZopfliNode(nodes []zopfliNode, pos uint, start_pos uint, len uint, len_code uint, dist uint, short_code uint, cost float32) {
|
||||
var next *zopfliNode = &nodes[pos+len]
|
||||
next.length = uint32(len | (len+9-len_code)<<25)
|
||||
next.distance = uint32(dist)
|
||||
next.dcode_insert_length = uint32(short_code<<27 | (pos - start_pos))
|
||||
next.u.cost = cost
|
||||
}
|
||||
|
||||
type PosData struct {
|
||||
type posData struct {
|
||||
pos uint
|
||||
distance_cache [4]int
|
||||
costdiff float32
|
||||
|
@ -251,32 +251,32 @@ type PosData struct {
|
|||
}
|
||||
|
||||
/* Maintains the smallest 8 cost difference together with their positions */
|
||||
type StartPosQueue struct {
|
||||
q_ [8]PosData
|
||||
type startPosQueue struct {
|
||||
q_ [8]posData
|
||||
idx_ uint
|
||||
}
|
||||
|
||||
func InitStartPosQueue(self *StartPosQueue) {
|
||||
func initStartPosQueue(self *startPosQueue) {
|
||||
self.idx_ = 0
|
||||
}
|
||||
|
||||
func StartPosQueueSize(self *StartPosQueue) uint {
|
||||
func startPosQueueSize(self *startPosQueue) uint {
|
||||
return brotli_min_size_t(self.idx_, 8)
|
||||
}
|
||||
|
||||
func StartPosQueuePush(self *StartPosQueue, posdata *PosData) {
|
||||
func startPosQueuePush(self *startPosQueue, posdata *posData) {
|
||||
var offset uint = ^(self.idx_) & 7
|
||||
self.idx_++
|
||||
var len uint = StartPosQueueSize(self)
|
||||
var len uint = startPosQueueSize(self)
|
||||
var i uint
|
||||
var q []PosData = self.q_[:]
|
||||
var q []posData = self.q_[:]
|
||||
q[offset] = *posdata
|
||||
|
||||
/* Restore the sorted order. In the list of |len| items at most |len - 1|
|
||||
adjacent element comparisons / swaps are required. */
|
||||
for i = 1; i < len; i++ {
|
||||
if q[offset&7].costdiff > q[(offset+1)&7].costdiff {
|
||||
var tmp PosData = q[offset&7]
|
||||
var tmp posData = q[offset&7]
|
||||
q[offset&7] = q[(offset+1)&7]
|
||||
q[(offset+1)&7] = tmp
|
||||
}
|
||||
|
@ -285,13 +285,13 @@ func StartPosQueuePush(self *StartPosQueue, posdata *PosData) {
|
|||
}
|
||||
}
|
||||
|
||||
func StartPosQueueAt(self *StartPosQueue, k uint) *PosData {
|
||||
func startPosQueueAt(self *startPosQueue, k uint) *posData {
|
||||
return &self.q_[(k-self.idx_)&7]
|
||||
}
|
||||
|
||||
/* Returns the minimum possible copy length that can improve the cost of any */
|
||||
/* future position. */
|
||||
func ComputeMinimumCopyLength(start_cost float32, nodes []ZopfliNode, num_bytes uint, pos uint) uint {
|
||||
func computeMinimumCopyLength(start_cost float32, nodes []zopfliNode, num_bytes uint, pos uint) uint {
|
||||
var min_cost float32 = start_cost
|
||||
var len uint = 2
|
||||
var next_len_bucket uint = 4
|
||||
|
@ -319,10 +319,10 @@ func ComputeMinimumCopyLength(start_cost float32, nodes []ZopfliNode, num_bytes
|
|||
|
||||
/* REQUIRES: nodes[pos].cost < kInfinity
|
||||
REQUIRES: nodes[0..pos] satisfies that "ZopfliNode array invariant". */
|
||||
func ComputeDistanceShortcut(block_start uint, pos uint, max_backward_limit uint, gap uint, nodes []ZopfliNode) uint32 {
|
||||
var clen uint = uint(ZopfliNodeCopyLength(&nodes[pos]))
|
||||
func computeDistanceShortcut(block_start uint, pos uint, max_backward_limit uint, gap uint, nodes []zopfliNode) uint32 {
|
||||
var clen uint = uint(zopfliNodeCopyLength(&nodes[pos]))
|
||||
var ilen uint = uint(nodes[pos].dcode_insert_length & 0x7FFFFFF)
|
||||
var dist uint = uint(ZopfliNodeCopyDistance(&nodes[pos]))
|
||||
var dist uint = uint(zopfliNodeCopyDistance(&nodes[pos]))
|
||||
|
||||
/* Since |block_start + pos| is the end position of the command, the copy part
|
||||
starts from |block_start + pos - clen|. Distances that are greater than
|
||||
|
@ -331,7 +331,7 @@ func ComputeDistanceShortcut(block_start uint, pos uint, max_backward_limit uint
|
|||
Also distance code 0 (last distance) does not update the last distances. */
|
||||
if pos == 0 {
|
||||
return 0
|
||||
} else if dist+clen <= block_start+pos+gap && dist <= max_backward_limit+gap && ZopfliNodeDistanceCode(&nodes[pos]) > 0 {
|
||||
} else if dist+clen <= block_start+pos+gap && dist <= max_backward_limit+gap && zopfliNodeDistanceCode(&nodes[pos]) > 0 {
|
||||
return uint32(pos)
|
||||
} else {
|
||||
return nodes[pos-clen-ilen].u.shortcut
|
||||
|
@ -345,13 +345,13 @@ func ComputeDistanceShortcut(block_start uint, pos uint, max_backward_limit uint
|
|||
starting_dist_cache[0..3].
|
||||
REQUIRES: nodes[pos].cost < kInfinity
|
||||
REQUIRES: nodes[0..pos] satisfies that "ZopfliNode array invariant". */
|
||||
func ComputeDistanceCache(pos uint, starting_dist_cache []int, nodes []ZopfliNode, dist_cache []int) {
|
||||
func computeDistanceCache(pos uint, starting_dist_cache []int, nodes []zopfliNode, dist_cache []int) {
|
||||
var idx int = 0
|
||||
var p uint = uint(nodes[pos].u.shortcut)
|
||||
for idx < 4 && p > 0 {
|
||||
var ilen uint = uint(nodes[p].dcode_insert_length & 0x7FFFFFF)
|
||||
var clen uint = uint(ZopfliNodeCopyLength(&nodes[p]))
|
||||
var dist uint = uint(ZopfliNodeCopyDistance(&nodes[p]))
|
||||
var clen uint = uint(zopfliNodeCopyLength(&nodes[p]))
|
||||
var dist uint = uint(zopfliNodeCopyDistance(&nodes[p]))
|
||||
dist_cache[idx] = int(dist)
|
||||
idx++
|
||||
|
||||
|
@ -367,22 +367,22 @@ func ComputeDistanceCache(pos uint, starting_dist_cache []int, nodes []ZopfliNod
|
|||
|
||||
/* Maintains "ZopfliNode array invariant" and pushes node to the queue, if it
|
||||
is eligible. */
|
||||
func EvaluateNode(block_start uint, pos uint, max_backward_limit uint, gap uint, starting_dist_cache []int, model *ZopfliCostModel, queue *StartPosQueue, nodes []ZopfliNode) {
|
||||
func evaluateNode(block_start uint, pos uint, max_backward_limit uint, gap uint, starting_dist_cache []int, model *zopfliCostModel, queue *startPosQueue, nodes []zopfliNode) {
|
||||
/* Save cost, because ComputeDistanceCache invalidates it. */
|
||||
var node_cost float32 = nodes[pos].u.cost
|
||||
nodes[pos].u.shortcut = ComputeDistanceShortcut(block_start, pos, max_backward_limit, gap, nodes)
|
||||
if node_cost <= ZopfliCostModelGetLiteralCosts(model, 0, pos) {
|
||||
var posdata PosData
|
||||
nodes[pos].u.shortcut = computeDistanceShortcut(block_start, pos, max_backward_limit, gap, nodes)
|
||||
if node_cost <= zopfliCostModelGetLiteralCosts(model, 0, pos) {
|
||||
var posdata posData
|
||||
posdata.pos = pos
|
||||
posdata.cost = node_cost
|
||||
posdata.costdiff = node_cost - ZopfliCostModelGetLiteralCosts(model, 0, pos)
|
||||
ComputeDistanceCache(pos, starting_dist_cache, nodes, posdata.distance_cache[:])
|
||||
StartPosQueuePush(queue, &posdata)
|
||||
posdata.costdiff = node_cost - zopfliCostModelGetLiteralCosts(model, 0, pos)
|
||||
computeDistanceCache(pos, starting_dist_cache, nodes, posdata.distance_cache[:])
|
||||
startPosQueuePush(queue, &posdata)
|
||||
}
|
||||
}
|
||||
|
||||
/* Returns longest copy length. */
|
||||
func UpdateNodes(num_bytes uint, block_start uint, pos uint, ringbuffer []byte, ringbuffer_mask uint, params *BrotliEncoderParams, max_backward_limit uint, starting_dist_cache []int, num_matches uint, matches []BackwardMatch, model *ZopfliCostModel, queue *StartPosQueue, nodes []ZopfliNode) uint {
|
||||
func updateNodes(num_bytes uint, block_start uint, pos uint, ringbuffer []byte, ringbuffer_mask uint, params *BrotliEncoderParams, max_backward_limit uint, starting_dist_cache []int, num_matches uint, matches []BackwardMatch, model *zopfliCostModel, queue *startPosQueue, nodes []zopfliNode) uint {
|
||||
var cur_ix uint = block_start + pos
|
||||
var cur_ix_masked uint = cur_ix & ringbuffer_mask
|
||||
var max_distance uint = brotli_min_size_t(cur_ix, max_backward_limit)
|
||||
|
@ -394,21 +394,21 @@ func UpdateNodes(num_bytes uint, block_start uint, pos uint, ringbuffer []byte,
|
|||
var k uint
|
||||
var gap uint = 0
|
||||
|
||||
EvaluateNode(block_start, pos, max_backward_limit, gap, starting_dist_cache, model, queue, nodes)
|
||||
evaluateNode(block_start, pos, max_backward_limit, gap, starting_dist_cache, model, queue, nodes)
|
||||
{
|
||||
var posdata *PosData = StartPosQueueAt(queue, 0)
|
||||
var min_cost float32 = (posdata.cost + ZopfliCostModelGetMinCostCmd(model) + ZopfliCostModelGetLiteralCosts(model, posdata.pos, pos))
|
||||
min_len = ComputeMinimumCopyLength(min_cost, nodes, num_bytes, pos)
|
||||
var posdata *posData = startPosQueueAt(queue, 0)
|
||||
var min_cost float32 = (posdata.cost + zopfliCostModelGetMinCostCmd(model) + zopfliCostModelGetLiteralCosts(model, posdata.pos, pos))
|
||||
min_len = computeMinimumCopyLength(min_cost, nodes, num_bytes, pos)
|
||||
}
|
||||
|
||||
/* Go over the command starting positions in order of increasing cost
|
||||
difference. */
|
||||
for k = 0; k < max_iters && k < StartPosQueueSize(queue); k++ {
|
||||
var posdata *PosData = StartPosQueueAt(queue, k)
|
||||
for k = 0; k < max_iters && k < startPosQueueSize(queue); k++ {
|
||||
var posdata *posData = startPosQueueAt(queue, k)
|
||||
var start uint = posdata.pos
|
||||
var inscode uint16 = GetInsertLengthCode(pos - start)
|
||||
var start_costdiff float32 = posdata.costdiff
|
||||
var base_cost float32 = start_costdiff + float32(GetInsertExtra(inscode)) + ZopfliCostModelGetLiteralCosts(model, 0, pos)
|
||||
var base_cost float32 = start_costdiff + float32(GetInsertExtra(inscode)) + zopfliCostModelGetLiteralCosts(model, 0, pos)
|
||||
var best_len uint = min_len - 1
|
||||
var j uint = 0
|
||||
/* Look for last distance matches using the distance cache from this
|
||||
|
@ -444,7 +444,7 @@ func UpdateNodes(num_bytes uint, block_start uint, pos uint, ringbuffer []byte,
|
|||
continue
|
||||
}
|
||||
{
|
||||
var dist_cost float32 = base_cost + ZopfliCostModelGetDistanceCost(model, j)
|
||||
var dist_cost float32 = base_cost + zopfliCostModelGetDistanceCost(model, j)
|
||||
var l uint
|
||||
for l = best_len + 1; l <= len; l++ {
|
||||
var copycode uint16 = GetCopyLengthCode(l)
|
||||
|
@ -455,9 +455,9 @@ func UpdateNodes(num_bytes uint, block_start uint, pos uint, ringbuffer []byte,
|
|||
} else {
|
||||
tmp = dist_cost
|
||||
}
|
||||
var cost float32 = tmp + float32(GetCopyExtra(copycode)) + ZopfliCostModelGetCommandCost(model, cmdcode)
|
||||
var cost float32 = tmp + float32(GetCopyExtra(copycode)) + zopfliCostModelGetCommandCost(model, cmdcode)
|
||||
if cost < nodes[pos+l].u.cost {
|
||||
UpdateZopfliNode(nodes, pos, start, l, l, backward, j+1, cost)
|
||||
updateZopfliNode(nodes, pos, start, l, l, backward, j+1, cost)
|
||||
result = brotli_max_size_t(result, l)
|
||||
}
|
||||
|
||||
|
@ -490,7 +490,7 @@ func UpdateNodes(num_bytes uint, block_start uint, pos uint, ringbuffer []byte,
|
|||
PrefixEncodeCopyDistance(dist_code, uint(params.dist.num_direct_distance_codes), uint(params.dist.distance_postfix_bits), &dist_symbol, &distextra)
|
||||
|
||||
distnumextra = uint32(dist_symbol) >> 10
|
||||
dist_cost = base_cost + float32(distnumextra) + ZopfliCostModelGetDistanceCost(model, uint(dist_symbol)&0x3FF)
|
||||
dist_cost = base_cost + float32(distnumextra) + zopfliCostModelGetDistanceCost(model, uint(dist_symbol)&0x3FF)
|
||||
|
||||
/* Try all copy lengths up until the maximum copy length corresponding
|
||||
to this distance. If the distance refers to the static dictionary, or
|
||||
|
@ -510,9 +510,9 @@ func UpdateNodes(num_bytes uint, block_start uint, pos uint, ringbuffer []byte,
|
|||
}
|
||||
var copycode uint16 = GetCopyLengthCode(len_code)
|
||||
var cmdcode uint16 = CombineLengthCodes(inscode, copycode, false)
|
||||
var cost float32 = dist_cost + float32(GetCopyExtra(copycode)) + ZopfliCostModelGetCommandCost(model, cmdcode)
|
||||
var cost float32 = dist_cost + float32(GetCopyExtra(copycode)) + zopfliCostModelGetCommandCost(model, cmdcode)
|
||||
if cost < nodes[pos+len].u.cost {
|
||||
UpdateZopfliNode(nodes, pos, start, uint(len), len_code, dist, 0, cost)
|
||||
updateZopfliNode(nodes, pos, start, uint(len), len_code, dist, 0, cost)
|
||||
result = brotli_max_size_t(result, uint(len))
|
||||
}
|
||||
}
|
||||
|
@ -523,7 +523,7 @@ func UpdateNodes(num_bytes uint, block_start uint, pos uint, ringbuffer []byte,
|
|||
return result
|
||||
}
|
||||
|
||||
func ComputeShortestPathFromNodes(num_bytes uint, nodes []ZopfliNode) uint {
|
||||
func computeShortestPathFromNodes(num_bytes uint, nodes []zopfliNode) uint {
|
||||
var index uint = num_bytes
|
||||
var num_commands uint = 0
|
||||
for nodes[index].dcode_insert_length&0x7FFFFFF == 0 && nodes[index].length == 1 {
|
||||
|
@ -531,7 +531,7 @@ func ComputeShortestPathFromNodes(num_bytes uint, nodes []ZopfliNode) uint {
|
|||
}
|
||||
nodes[index].u.next = BROTLI_UINT32_MAX
|
||||
for index != 0 {
|
||||
var len uint = uint(ZopfliNodeCommandLength(&nodes[index]))
|
||||
var len uint = uint(zopfliNodeCommandLength(&nodes[index]))
|
||||
index -= uint(len)
|
||||
nodes[index].u.next = uint32(len)
|
||||
num_commands++
|
||||
|
@ -541,15 +541,15 @@ func ComputeShortestPathFromNodes(num_bytes uint, nodes []ZopfliNode) uint {
|
|||
}
|
||||
|
||||
/* REQUIRES: nodes != NULL and len(nodes) >= num_bytes + 1 */
|
||||
func BrotliZopfliCreateCommands(num_bytes uint, block_start uint, nodes []ZopfliNode, dist_cache []int, last_insert_len *uint, params *BrotliEncoderParams, commands []Command, num_literals *uint) {
|
||||
func zopfliCreateCommands(num_bytes uint, block_start uint, nodes []zopfliNode, dist_cache []int, last_insert_len *uint, params *BrotliEncoderParams, commands []Command, num_literals *uint) {
|
||||
var max_backward_limit uint = BROTLI_MAX_BACKWARD_LIMIT(params.lgwin)
|
||||
var pos uint = 0
|
||||
var offset uint32 = nodes[0].u.next
|
||||
var i uint
|
||||
var gap uint = 0
|
||||
for i = 0; offset != BROTLI_UINT32_MAX; i++ {
|
||||
var next *ZopfliNode = &nodes[uint32(pos)+offset]
|
||||
var copy_length uint = uint(ZopfliNodeCopyLength(next))
|
||||
var next *zopfliNode = &nodes[uint32(pos)+offset]
|
||||
var copy_length uint = uint(zopfliNodeCopyLength(next))
|
||||
var insert_length uint = uint(next.dcode_insert_length & 0x7FFFFFF)
|
||||
pos += insert_length
|
||||
offset = next.u.next
|
||||
|
@ -558,11 +558,11 @@ func BrotliZopfliCreateCommands(num_bytes uint, block_start uint, nodes []Zopfli
|
|||
*last_insert_len = 0
|
||||
}
|
||||
{
|
||||
var distance uint = uint(ZopfliNodeCopyDistance(next))
|
||||
var len_code uint = uint(ZopfliNodeLengthCode(next))
|
||||
var distance uint = uint(zopfliNodeCopyDistance(next))
|
||||
var len_code uint = uint(zopfliNodeLengthCode(next))
|
||||
var max_distance uint = brotli_min_size_t(block_start+pos, max_backward_limit)
|
||||
var is_dictionary bool = (distance > max_distance+gap)
|
||||
var dist_code uint = uint(ZopfliNodeDistanceCode(next))
|
||||
var dist_code uint = uint(zopfliNodeDistanceCode(next))
|
||||
InitCommand(&commands[i], ¶ms.dist, insert_length, copy_length, int(len_code)-int(copy_length), dist_code)
|
||||
|
||||
if !is_dictionary && dist_code > 0 {
|
||||
|
@ -580,17 +580,17 @@ func BrotliZopfliCreateCommands(num_bytes uint, block_start uint, nodes []Zopfli
|
|||
*last_insert_len += num_bytes - pos
|
||||
}
|
||||
|
||||
func ZopfliIterate(num_bytes uint, position uint, ringbuffer []byte, ringbuffer_mask uint, params *BrotliEncoderParams, gap uint, dist_cache []int, model *ZopfliCostModel, num_matches []uint32, matches []BackwardMatch, nodes []ZopfliNode) uint {
|
||||
func zopfliIterate(num_bytes uint, position uint, ringbuffer []byte, ringbuffer_mask uint, params *BrotliEncoderParams, gap uint, dist_cache []int, model *zopfliCostModel, num_matches []uint32, matches []BackwardMatch, nodes []zopfliNode) uint {
|
||||
var max_backward_limit uint = BROTLI_MAX_BACKWARD_LIMIT(params.lgwin)
|
||||
var max_zopfli_len uint = MaxZopfliLen(params)
|
||||
var queue StartPosQueue
|
||||
var queue startPosQueue
|
||||
var cur_match_pos uint = 0
|
||||
var i uint
|
||||
nodes[0].length = 0
|
||||
nodes[0].u.cost = 0
|
||||
InitStartPosQueue(&queue)
|
||||
initStartPosQueue(&queue)
|
||||
for i = 0; i+3 < num_bytes; i++ {
|
||||
var skip uint = UpdateNodes(num_bytes, position, i, ringbuffer, ringbuffer_mask, params, max_backward_limit, dist_cache, uint(num_matches[i]), matches[cur_match_pos:], model, &queue, nodes)
|
||||
var skip uint = updateNodes(num_bytes, position, i, ringbuffer, ringbuffer_mask, params, max_backward_limit, dist_cache, uint(num_matches[i]), matches[cur_match_pos:], model, &queue, nodes)
|
||||
if skip < BROTLI_LONG_COPY_QUICK_STEP {
|
||||
skip = 0
|
||||
}
|
||||
|
@ -606,22 +606,22 @@ func ZopfliIterate(num_bytes uint, position uint, ringbuffer []byte, ringbuffer_
|
|||
if i+3 >= num_bytes {
|
||||
break
|
||||
}
|
||||
EvaluateNode(position, i, max_backward_limit, gap, dist_cache, model, &queue, nodes)
|
||||
evaluateNode(position, i, max_backward_limit, gap, dist_cache, model, &queue, nodes)
|
||||
cur_match_pos += uint(num_matches[i])
|
||||
skip--
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ComputeShortestPathFromNodes(num_bytes, nodes)
|
||||
return computeShortestPathFromNodes(num_bytes, nodes)
|
||||
}
|
||||
|
||||
/* REQUIRES: nodes != NULL and len(nodes) >= num_bytes + 1 */
|
||||
func BrotliZopfliComputeShortestPath(num_bytes uint, position uint, ringbuffer []byte, ringbuffer_mask uint, params *BrotliEncoderParams, dist_cache []int, hasher *H10, nodes []ZopfliNode) uint {
|
||||
func zopfliComputeShortestPath(num_bytes uint, position uint, ringbuffer []byte, ringbuffer_mask uint, params *BrotliEncoderParams, dist_cache []int, hasher *H10, nodes []zopfliNode) uint {
|
||||
var max_backward_limit uint = BROTLI_MAX_BACKWARD_LIMIT(params.lgwin)
|
||||
var max_zopfli_len uint = MaxZopfliLen(params)
|
||||
var model ZopfliCostModel
|
||||
var queue StartPosQueue
|
||||
var model zopfliCostModel
|
||||
var queue startPosQueue
|
||||
var matches [2 * (MAX_NUM_MATCHES_H10 + 64)]BackwardMatch
|
||||
var store_end uint
|
||||
if num_bytes >= hasher.StoreLookahead() {
|
||||
|
@ -634,9 +634,9 @@ func BrotliZopfliComputeShortestPath(num_bytes uint, position uint, ringbuffer [
|
|||
var lz_matches_offset uint = 0
|
||||
nodes[0].length = 0
|
||||
nodes[0].u.cost = 0
|
||||
InitZopfliCostModel(&model, ¶ms.dist, num_bytes)
|
||||
ZopfliCostModelSetFromLiteralCosts(&model, position, ringbuffer, ringbuffer_mask)
|
||||
InitStartPosQueue(&queue)
|
||||
initZopfliCostModel(&model, ¶ms.dist, num_bytes)
|
||||
zopfliCostModelSetFromLiteralCosts(&model, position, ringbuffer, ringbuffer_mask)
|
||||
initStartPosQueue(&queue)
|
||||
for i = 0; i+hasher.HashTypeLength()-1 < num_bytes; i++ {
|
||||
var pos uint = position + i
|
||||
var max_distance uint = brotli_min_size_t(pos, max_backward_limit)
|
||||
|
@ -648,7 +648,7 @@ func BrotliZopfliComputeShortestPath(num_bytes uint, position uint, ringbuffer [
|
|||
num_matches = 1
|
||||
}
|
||||
|
||||
skip = UpdateNodes(num_bytes, position, i, ringbuffer, ringbuffer_mask, params, max_backward_limit, dist_cache, num_matches, matches[:], &model, &queue, nodes)
|
||||
skip = updateNodes(num_bytes, position, i, ringbuffer, ringbuffer_mask, params, max_backward_limit, dist_cache, num_matches, matches[:], &model, &queue, nodes)
|
||||
if skip < BROTLI_LONG_COPY_QUICK_STEP {
|
||||
skip = 0
|
||||
}
|
||||
|
@ -666,26 +666,26 @@ func BrotliZopfliComputeShortestPath(num_bytes uint, position uint, ringbuffer [
|
|||
if i+hasher.HashTypeLength()-1 >= num_bytes {
|
||||
break
|
||||
}
|
||||
EvaluateNode(position, i, max_backward_limit, gap, dist_cache, &model, &queue, nodes)
|
||||
evaluateNode(position, i, max_backward_limit, gap, dist_cache, &model, &queue, nodes)
|
||||
skip--
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
CleanupZopfliCostModel(&model)
|
||||
return ComputeShortestPathFromNodes(num_bytes, nodes)
|
||||
cleanupZopfliCostModel(&model)
|
||||
return computeShortestPathFromNodes(num_bytes, nodes)
|
||||
}
|
||||
|
||||
func BrotliCreateZopfliBackwardReferences(num_bytes uint, position uint, ringbuffer []byte, ringbuffer_mask uint, params *BrotliEncoderParams, hasher *H10, dist_cache []int, last_insert_len *uint, commands []Command, num_commands *uint, num_literals *uint) {
|
||||
var nodes []ZopfliNode
|
||||
nodes = make([]ZopfliNode, (num_bytes + 1))
|
||||
BrotliInitZopfliNodes(nodes, num_bytes+1)
|
||||
*num_commands += BrotliZopfliComputeShortestPath(num_bytes, position, ringbuffer, ringbuffer_mask, params, dist_cache, hasher, nodes)
|
||||
BrotliZopfliCreateCommands(num_bytes, position, nodes, dist_cache, last_insert_len, params, commands, num_literals)
|
||||
func createZopfliBackwardReferences(num_bytes uint, position uint, ringbuffer []byte, ringbuffer_mask uint, params *BrotliEncoderParams, hasher *H10, dist_cache []int, last_insert_len *uint, commands []Command, num_commands *uint, num_literals *uint) {
|
||||
var nodes []zopfliNode
|
||||
nodes = make([]zopfliNode, (num_bytes + 1))
|
||||
initZopfliNodes(nodes, num_bytes+1)
|
||||
*num_commands += zopfliComputeShortestPath(num_bytes, position, ringbuffer, ringbuffer_mask, params, dist_cache, hasher, nodes)
|
||||
zopfliCreateCommands(num_bytes, position, nodes, dist_cache, last_insert_len, params, commands, num_literals)
|
||||
nodes = nil
|
||||
}
|
||||
|
||||
func BrotliCreateHqZopfliBackwardReferences(num_bytes uint, position uint, ringbuffer []byte, ringbuffer_mask uint, params *BrotliEncoderParams, hasher HasherHandle, dist_cache []int, last_insert_len *uint, commands []Command, num_commands *uint, num_literals *uint) {
|
||||
func createHqZopfliBackwardReferences(num_bytes uint, position uint, ringbuffer []byte, ringbuffer_mask uint, params *BrotliEncoderParams, hasher HasherHandle, dist_cache []int, last_insert_len *uint, commands []Command, num_commands *uint, num_literals *uint) {
|
||||
var max_backward_limit uint = BROTLI_MAX_BACKWARD_LIMIT(params.lgwin)
|
||||
var num_matches []uint32 = make([]uint32, num_bytes)
|
||||
var matches_size uint = 4 * num_bytes
|
||||
|
@ -701,8 +701,8 @@ func BrotliCreateHqZopfliBackwardReferences(num_bytes uint, position uint, ringb
|
|||
var orig_last_insert_len uint
|
||||
var orig_dist_cache [4]int
|
||||
var orig_num_commands uint
|
||||
var model ZopfliCostModel
|
||||
var nodes []ZopfliNode
|
||||
var model zopfliCostModel
|
||||
var nodes []zopfliNode
|
||||
var matches []BackwardMatch = make([]BackwardMatch, matches_size)
|
||||
var gap uint = 0
|
||||
var shadow_matches uint = 0
|
||||
|
@ -767,25 +767,25 @@ func BrotliCreateHqZopfliBackwardReferences(num_bytes uint, position uint, ringb
|
|||
orig_last_insert_len = *last_insert_len
|
||||
copy(orig_dist_cache[:], dist_cache[:4])
|
||||
orig_num_commands = *num_commands
|
||||
nodes = make([]ZopfliNode, (num_bytes + 1))
|
||||
InitZopfliCostModel(&model, ¶ms.dist, num_bytes)
|
||||
nodes = make([]zopfliNode, (num_bytes + 1))
|
||||
initZopfliCostModel(&model, ¶ms.dist, num_bytes)
|
||||
for i = 0; i < 2; i++ {
|
||||
BrotliInitZopfliNodes(nodes, num_bytes+1)
|
||||
initZopfliNodes(nodes, num_bytes+1)
|
||||
if i == 0 {
|
||||
ZopfliCostModelSetFromLiteralCosts(&model, position, ringbuffer, ringbuffer_mask)
|
||||
zopfliCostModelSetFromLiteralCosts(&model, position, ringbuffer, ringbuffer_mask)
|
||||
} else {
|
||||
ZopfliCostModelSetFromCommands(&model, position, ringbuffer, ringbuffer_mask, commands, *num_commands-orig_num_commands, orig_last_insert_len)
|
||||
zopfliCostModelSetFromCommands(&model, position, ringbuffer, ringbuffer_mask, commands, *num_commands-orig_num_commands, orig_last_insert_len)
|
||||
}
|
||||
|
||||
*num_commands = orig_num_commands
|
||||
*num_literals = orig_num_literals
|
||||
*last_insert_len = orig_last_insert_len
|
||||
copy(dist_cache, orig_dist_cache[:4])
|
||||
*num_commands += ZopfliIterate(num_bytes, position, ringbuffer, ringbuffer_mask, params, gap, dist_cache, &model, num_matches, matches, nodes)
|
||||
BrotliZopfliCreateCommands(num_bytes, position, nodes, dist_cache, last_insert_len, params, commands, num_literals)
|
||||
*num_commands += zopfliIterate(num_bytes, position, ringbuffer, ringbuffer_mask, params, gap, dist_cache, &model, num_matches, matches, nodes)
|
||||
zopfliCreateCommands(num_bytes, position, nodes, dist_cache, last_insert_len, params, commands, num_literals)
|
||||
}
|
||||
|
||||
CleanupZopfliCostModel(&model)
|
||||
cleanupZopfliCostModel(&model)
|
||||
nodes = nil
|
||||
matches = nil
|
||||
num_matches = nil
|
||||
|
|
66
bit_cost.go
66
bit_cost.go
|
@ -14,7 +14,7 @@ package brotli
|
|||
*/
|
||||
|
||||
/* Functions to estimate the bit cost of Huffman trees. */
|
||||
func ShannonEntropy(population []uint32, size uint, total *uint) float64 {
|
||||
func shannonEntropy(population []uint32, size uint, total *uint) float64 {
|
||||
var sum uint = 0
|
||||
var retval float64 = 0
|
||||
var population_end []uint32 = population[size:]
|
||||
|
@ -33,9 +33,9 @@ func ShannonEntropy(population []uint32, size uint, total *uint) float64 {
|
|||
return retval
|
||||
}
|
||||
|
||||
func BitsEntropy(population []uint32, size uint) float64 {
|
||||
func bitsEntropy(population []uint32, size uint) float64 {
|
||||
var sum uint
|
||||
var retval float64 = ShannonEntropy(population, size, &sum)
|
||||
var retval float64 = shannonEntropy(population, size, &sum)
|
||||
if retval < float64(sum) {
|
||||
/* At least one bit per literal is needed. */
|
||||
retval = float64(sum)
|
||||
|
@ -44,19 +44,19 @@ func BitsEntropy(population []uint32, size uint) float64 {
|
|||
return retval
|
||||
}
|
||||
|
||||
var BrotliPopulationCostLiteral_kOneSymbolHistogramCost float64 = 12
|
||||
var BrotliPopulationCostLiteral_kTwoSymbolHistogramCost float64 = 20
|
||||
var BrotliPopulationCostLiteral_kThreeSymbolHistogramCost float64 = 28
|
||||
var BrotliPopulationCostLiteral_kFourSymbolHistogramCost float64 = 37
|
||||
const kOneSymbolHistogramCost float64 = 12
|
||||
const kTwoSymbolHistogramCost float64 = 20
|
||||
const kThreeSymbolHistogramCost float64 = 28
|
||||
const kFourSymbolHistogramCost float64 = 37
|
||||
|
||||
func BrotliPopulationCostLiteral(histogram *HistogramLiteral) float64 {
|
||||
func populationCostLiteral(histogram *HistogramLiteral) float64 {
|
||||
var data_size uint = HistogramDataSizeLiteral()
|
||||
var count int = 0
|
||||
var s [5]uint
|
||||
var bits float64 = 0.0
|
||||
var i uint
|
||||
if histogram.total_count_ == 0 {
|
||||
return BrotliPopulationCostLiteral_kOneSymbolHistogramCost
|
||||
return kOneSymbolHistogramCost
|
||||
}
|
||||
|
||||
for i = 0; i < data_size; i++ {
|
||||
|
@ -70,11 +70,11 @@ func BrotliPopulationCostLiteral(histogram *HistogramLiteral) float64 {
|
|||
}
|
||||
|
||||
if count == 1 {
|
||||
return BrotliPopulationCostLiteral_kOneSymbolHistogramCost
|
||||
return kOneSymbolHistogramCost
|
||||
}
|
||||
|
||||
if count == 2 {
|
||||
return BrotliPopulationCostLiteral_kTwoSymbolHistogramCost + float64(histogram.total_count_)
|
||||
return kTwoSymbolHistogramCost + float64(histogram.total_count_)
|
||||
}
|
||||
|
||||
if count == 3 {
|
||||
|
@ -82,7 +82,7 @@ func BrotliPopulationCostLiteral(histogram *HistogramLiteral) float64 {
|
|||
var histo1 uint32 = histogram.data_[s[1]]
|
||||
var histo2 uint32 = histogram.data_[s[2]]
|
||||
var histomax uint32 = brotli_max_uint32_t(histo0, brotli_max_uint32_t(histo1, histo2))
|
||||
return BrotliPopulationCostLiteral_kThreeSymbolHistogramCost + 2*(float64(histo0)+float64(histo1)+float64(histo2)) - float64(histomax)
|
||||
return kThreeSymbolHistogramCost + 2*(float64(histo0)+float64(histo1)+float64(histo2)) - float64(histomax)
|
||||
}
|
||||
|
||||
if count == 4 {
|
||||
|
@ -107,7 +107,7 @@ func BrotliPopulationCostLiteral(histogram *HistogramLiteral) float64 {
|
|||
|
||||
h23 = histo[2] + histo[3]
|
||||
histomax = brotli_max_uint32_t(h23, histo[0])
|
||||
return BrotliPopulationCostLiteral_kFourSymbolHistogramCost + 3*float64(h23) + 2*(float64(histo[0])+float64(histo[1])) - float64(histomax)
|
||||
return kFourSymbolHistogramCost + 3*float64(h23) + 2*(float64(histo[0])+float64(histo[1])) - float64(histomax)
|
||||
}
|
||||
{
|
||||
var max_depth uint = 1
|
||||
|
@ -174,25 +174,20 @@ func BrotliPopulationCostLiteral(histogram *HistogramLiteral) float64 {
|
|||
bits += float64(18 + 2*max_depth)
|
||||
|
||||
/* Add the entropy of the code length code histogram. */
|
||||
bits += BitsEntropy(depth_histo[:], BROTLI_CODE_LENGTH_CODES)
|
||||
bits += bitsEntropy(depth_histo[:], BROTLI_CODE_LENGTH_CODES)
|
||||
}
|
||||
|
||||
return bits
|
||||
}
|
||||
|
||||
var BrotliPopulationCostCommand_kOneSymbolHistogramCost float64 = 12
|
||||
var BrotliPopulationCostCommand_kTwoSymbolHistogramCost float64 = 20
|
||||
var BrotliPopulationCostCommand_kThreeSymbolHistogramCost float64 = 28
|
||||
var BrotliPopulationCostCommand_kFourSymbolHistogramCost float64 = 37
|
||||
|
||||
func BrotliPopulationCostCommand(histogram *HistogramCommand) float64 {
|
||||
func populationCostCommand(histogram *HistogramCommand) float64 {
|
||||
var data_size uint = HistogramDataSizeCommand()
|
||||
var count int = 0
|
||||
var s [5]uint
|
||||
var bits float64 = 0.0
|
||||
var i uint
|
||||
if histogram.total_count_ == 0 {
|
||||
return BrotliPopulationCostCommand_kOneSymbolHistogramCost
|
||||
return kOneSymbolHistogramCost
|
||||
}
|
||||
|
||||
for i = 0; i < data_size; i++ {
|
||||
|
@ -206,11 +201,11 @@ func BrotliPopulationCostCommand(histogram *HistogramCommand) float64 {
|
|||
}
|
||||
|
||||
if count == 1 {
|
||||
return BrotliPopulationCostCommand_kOneSymbolHistogramCost
|
||||
return kOneSymbolHistogramCost
|
||||
}
|
||||
|
||||
if count == 2 {
|
||||
return BrotliPopulationCostCommand_kTwoSymbolHistogramCost + float64(histogram.total_count_)
|
||||
return kTwoSymbolHistogramCost + float64(histogram.total_count_)
|
||||
}
|
||||
|
||||
if count == 3 {
|
||||
|
@ -218,7 +213,7 @@ func BrotliPopulationCostCommand(histogram *HistogramCommand) float64 {
|
|||
var histo1 uint32 = histogram.data_[s[1]]
|
||||
var histo2 uint32 = histogram.data_[s[2]]
|
||||
var histomax uint32 = brotli_max_uint32_t(histo0, brotli_max_uint32_t(histo1, histo2))
|
||||
return BrotliPopulationCostCommand_kThreeSymbolHistogramCost + 2*(float64(histo0)+float64(histo1)+float64(histo2)) - float64(histomax)
|
||||
return kThreeSymbolHistogramCost + 2*(float64(histo0)+float64(histo1)+float64(histo2)) - float64(histomax)
|
||||
}
|
||||
|
||||
if count == 4 {
|
||||
|
@ -243,7 +238,7 @@ func BrotliPopulationCostCommand(histogram *HistogramCommand) float64 {
|
|||
|
||||
h23 = histo[2] + histo[3]
|
||||
histomax = brotli_max_uint32_t(h23, histo[0])
|
||||
return BrotliPopulationCostCommand_kFourSymbolHistogramCost + 3*float64(h23) + 2*(float64(histo[0])+float64(histo[1])) - float64(histomax)
|
||||
return kFourSymbolHistogramCost + 3*float64(h23) + 2*(float64(histo[0])+float64(histo[1])) - float64(histomax)
|
||||
}
|
||||
{
|
||||
var max_depth uint = 1
|
||||
|
@ -310,25 +305,20 @@ func BrotliPopulationCostCommand(histogram *HistogramCommand) float64 {
|
|||
bits += float64(18 + 2*max_depth)
|
||||
|
||||
/* Add the entropy of the code length code histogram. */
|
||||
bits += BitsEntropy(depth_histo[:], BROTLI_CODE_LENGTH_CODES)
|
||||
bits += bitsEntropy(depth_histo[:], BROTLI_CODE_LENGTH_CODES)
|
||||
}
|
||||
|
||||
return bits
|
||||
}
|
||||
|
||||
var BrotliPopulationCostDistance_kOneSymbolHistogramCost float64 = 12
|
||||
var BrotliPopulationCostDistance_kTwoSymbolHistogramCost float64 = 20
|
||||
var BrotliPopulationCostDistance_kThreeSymbolHistogramCost float64 = 28
|
||||
var BrotliPopulationCostDistance_kFourSymbolHistogramCost float64 = 37
|
||||
|
||||
func BrotliPopulationCostDistance(histogram *HistogramDistance) float64 {
|
||||
func populationCostDistance(histogram *HistogramDistance) float64 {
|
||||
var data_size uint = HistogramDataSizeDistance()
|
||||
var count int = 0
|
||||
var s [5]uint
|
||||
var bits float64 = 0.0
|
||||
var i uint
|
||||
if histogram.total_count_ == 0 {
|
||||
return BrotliPopulationCostDistance_kOneSymbolHistogramCost
|
||||
return kOneSymbolHistogramCost
|
||||
}
|
||||
|
||||
for i = 0; i < data_size; i++ {
|
||||
|
@ -342,11 +332,11 @@ func BrotliPopulationCostDistance(histogram *HistogramDistance) float64 {
|
|||
}
|
||||
|
||||
if count == 1 {
|
||||
return BrotliPopulationCostDistance_kOneSymbolHistogramCost
|
||||
return kOneSymbolHistogramCost
|
||||
}
|
||||
|
||||
if count == 2 {
|
||||
return BrotliPopulationCostDistance_kTwoSymbolHistogramCost + float64(histogram.total_count_)
|
||||
return kTwoSymbolHistogramCost + float64(histogram.total_count_)
|
||||
}
|
||||
|
||||
if count == 3 {
|
||||
|
@ -354,7 +344,7 @@ func BrotliPopulationCostDistance(histogram *HistogramDistance) float64 {
|
|||
var histo1 uint32 = histogram.data_[s[1]]
|
||||
var histo2 uint32 = histogram.data_[s[2]]
|
||||
var histomax uint32 = brotli_max_uint32_t(histo0, brotli_max_uint32_t(histo1, histo2))
|
||||
return BrotliPopulationCostDistance_kThreeSymbolHistogramCost + 2*(float64(histo0)+float64(histo1)+float64(histo2)) - float64(histomax)
|
||||
return kThreeSymbolHistogramCost + 2*(float64(histo0)+float64(histo1)+float64(histo2)) - float64(histomax)
|
||||
}
|
||||
|
||||
if count == 4 {
|
||||
|
@ -379,7 +369,7 @@ func BrotliPopulationCostDistance(histogram *HistogramDistance) float64 {
|
|||
|
||||
h23 = histo[2] + histo[3]
|
||||
histomax = brotli_max_uint32_t(h23, histo[0])
|
||||
return BrotliPopulationCostDistance_kFourSymbolHistogramCost + 3*float64(h23) + 2*(float64(histo[0])+float64(histo[1])) - float64(histomax)
|
||||
return kFourSymbolHistogramCost + 3*float64(h23) + 2*(float64(histo[0])+float64(histo[1])) - float64(histomax)
|
||||
}
|
||||
{
|
||||
var max_depth uint = 1
|
||||
|
@ -446,7 +436,7 @@ func BrotliPopulationCostDistance(histogram *HistogramDistance) float64 {
|
|||
bits += float64(18 + 2*max_depth)
|
||||
|
||||
/* Add the entropy of the code length code histogram. */
|
||||
bits += BitsEntropy(depth_histo[:], BROTLI_CODE_LENGTH_CODES)
|
||||
bits += bitsEntropy(depth_histo[:], BROTLI_CODE_LENGTH_CODES)
|
||||
}
|
||||
|
||||
return bits
|
||||
|
|
|
@ -9,7 +9,7 @@ import "encoding/binary"
|
|||
*/
|
||||
|
||||
/* Bit reading helpers */
|
||||
const BROTLI_SHORT_FILL_BIT_WINDOW_READ = (8 >> 1)
|
||||
const shortFillBitWindowRead = (8 >> 1)
|
||||
|
||||
var kBitMask = [33]uint32{
|
||||
0x00000000,
|
||||
|
@ -47,11 +47,11 @@ var kBitMask = [33]uint32{
|
|||
0xFFFFFFFF,
|
||||
}
|
||||
|
||||
func BitMask(n uint32) uint32 {
|
||||
func bitMask(n uint32) uint32 {
|
||||
return kBitMask[n]
|
||||
}
|
||||
|
||||
type BrotliBitReader struct {
|
||||
type bitReader struct {
|
||||
val_ uint64
|
||||
bit_pos_ uint32
|
||||
input []byte
|
||||
|
@ -59,7 +59,7 @@ type BrotliBitReader struct {
|
|||
byte_pos uint
|
||||
}
|
||||
|
||||
type BrotliBitReaderState struct {
|
||||
type bitReaderState struct {
|
||||
val_ uint64
|
||||
bit_pos_ uint32
|
||||
input []byte
|
||||
|
@ -74,7 +74,7 @@ type BrotliBitReaderState struct {
|
|||
Returns false if data is required but there is no input available.
|
||||
For BROTLI_ALIGNED_READ this function also prepares bit reader for aligned
|
||||
reading. */
|
||||
func BrotliBitReaderSaveState(from *BrotliBitReader, to *BrotliBitReaderState) {
|
||||
func bitReaderSaveState(from *bitReader, to *bitReaderState) {
|
||||
to.val_ = from.val_
|
||||
to.bit_pos_ = from.bit_pos_
|
||||
to.input = from.input
|
||||
|
@ -82,7 +82,7 @@ func BrotliBitReaderSaveState(from *BrotliBitReader, to *BrotliBitReaderState) {
|
|||
to.byte_pos = from.byte_pos
|
||||
}
|
||||
|
||||
func BrotliBitReaderRestoreState(to *BrotliBitReader, from *BrotliBitReaderState) {
|
||||
func bitReaderRestoreState(to *bitReader, from *bitReaderState) {
|
||||
to.val_ = from.val_
|
||||
to.bit_pos_ = from.bit_pos_
|
||||
to.input = from.input
|
||||
|
@ -90,19 +90,19 @@ func BrotliBitReaderRestoreState(to *BrotliBitReader, from *BrotliBitReaderState
|
|||
to.byte_pos = from.byte_pos
|
||||
}
|
||||
|
||||
func BrotliGetAvailableBits(br *BrotliBitReader) uint32 {
|
||||
func getAvailableBits(br *bitReader) uint32 {
|
||||
return 64 - br.bit_pos_
|
||||
}
|
||||
|
||||
/* Returns amount of unread bytes the bit reader still has buffered from the
|
||||
BrotliInput, including whole bytes in br->val_. */
|
||||
func BrotliGetRemainingBytes(br *BrotliBitReader) uint {
|
||||
return uint(uint32(br.input_len-br.byte_pos) + (BrotliGetAvailableBits(br) >> 3))
|
||||
func getRemainingBytes(br *bitReader) uint {
|
||||
return uint(uint32(br.input_len-br.byte_pos) + (getAvailableBits(br) >> 3))
|
||||
}
|
||||
|
||||
/* Checks if there is at least |num| bytes left in the input ring-buffer
|
||||
(excluding the bits remaining in br->val_). */
|
||||
func BrotliCheckInputAmount(br *BrotliBitReader, num uint) bool {
|
||||
func checkInputAmount(br *bitReader, num uint) bool {
|
||||
return br.input_len-br.byte_pos >= num
|
||||
}
|
||||
|
||||
|
@ -110,7 +110,7 @@ func BrotliCheckInputAmount(br *BrotliBitReader, num uint) bool {
|
|||
Precondition: accumulator contains at least 1 bit.
|
||||
|n_bits| should be in the range [1..24] for regular build. For portable
|
||||
non-64-bit little-endian build only 16 bits are safe to request. */
|
||||
func BrotliFillBitWindow(br *BrotliBitReader, n_bits uint32) {
|
||||
func fillBitWindow(br *bitReader, n_bits uint32) {
|
||||
if br.bit_pos_ >= 32 {
|
||||
br.val_ >>= 32
|
||||
br.bit_pos_ ^= 32 /* here same as -= 32 because of the if condition */
|
||||
|
@ -121,13 +121,13 @@ func BrotliFillBitWindow(br *BrotliBitReader, n_bits uint32) {
|
|||
|
||||
/* Mostly like BrotliFillBitWindow, but guarantees only 16 bits and reads no
|
||||
more than BROTLI_SHORT_FILL_BIT_WINDOW_READ bytes of input. */
|
||||
func BrotliFillBitWindow16(br *BrotliBitReader) {
|
||||
BrotliFillBitWindow(br, 17)
|
||||
func fillBitWindow16(br *bitReader) {
|
||||
fillBitWindow(br, 17)
|
||||
}
|
||||
|
||||
/* Tries to pull one byte of input to accumulator.
|
||||
Returns false if there is no input available. */
|
||||
func BrotliPullByte(br *BrotliBitReader) bool {
|
||||
func pullByte(br *bitReader) bool {
|
||||
if br.byte_pos == br.input_len {
|
||||
return false
|
||||
}
|
||||
|
@ -141,44 +141,44 @@ func BrotliPullByte(br *BrotliBitReader) bool {
|
|||
|
||||
/* Returns currently available bits.
|
||||
The number of valid bits could be calculated by BrotliGetAvailableBits. */
|
||||
func BrotliGetBitsUnmasked(br *BrotliBitReader) uint64 {
|
||||
func getBitsUnmasked(br *bitReader) uint64 {
|
||||
return br.val_ >> br.bit_pos_
|
||||
}
|
||||
|
||||
/* Like BrotliGetBits, but does not mask the result.
|
||||
The result contains at least 16 valid bits. */
|
||||
func BrotliGet16BitsUnmasked(br *BrotliBitReader) uint32 {
|
||||
BrotliFillBitWindow(br, 16)
|
||||
return uint32(BrotliGetBitsUnmasked(br))
|
||||
func get16BitsUnmasked(br *bitReader) uint32 {
|
||||
fillBitWindow(br, 16)
|
||||
return uint32(getBitsUnmasked(br))
|
||||
}
|
||||
|
||||
/* Returns the specified number of bits from |br| without advancing bit
|
||||
position. */
|
||||
func BrotliGetBits(br *BrotliBitReader, n_bits uint32) uint32 {
|
||||
BrotliFillBitWindow(br, n_bits)
|
||||
return uint32(BrotliGetBitsUnmasked(br)) & BitMask(n_bits)
|
||||
func BrotliGetBits(br *bitReader, n_bits uint32) uint32 {
|
||||
fillBitWindow(br, n_bits)
|
||||
return uint32(getBitsUnmasked(br)) & bitMask(n_bits)
|
||||
}
|
||||
|
||||
/* Tries to peek the specified amount of bits. Returns false, if there
|
||||
is not enough input. */
|
||||
func BrotliSafeGetBits(br *BrotliBitReader, n_bits uint32, val *uint32) bool {
|
||||
for BrotliGetAvailableBits(br) < n_bits {
|
||||
if !BrotliPullByte(br) {
|
||||
func safeGetBits(br *bitReader, n_bits uint32, val *uint32) bool {
|
||||
for getAvailableBits(br) < n_bits {
|
||||
if !pullByte(br) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
*val = uint32(BrotliGetBitsUnmasked(br)) & BitMask(n_bits)
|
||||
*val = uint32(getBitsUnmasked(br)) & bitMask(n_bits)
|
||||
return true
|
||||
}
|
||||
|
||||
/* Advances the bit pos by |n_bits|. */
|
||||
func BrotliDropBits(br *BrotliBitReader, n_bits uint32) {
|
||||
func dropBits(br *bitReader, n_bits uint32) {
|
||||
br.bit_pos_ += n_bits
|
||||
}
|
||||
|
||||
func BrotliBitReaderUnload(br *BrotliBitReader) {
|
||||
var unused_bytes uint32 = BrotliGetAvailableBits(br) >> 3
|
||||
func bitReaderUnload(br *bitReader) {
|
||||
var unused_bytes uint32 = getAvailableBits(br) >> 3
|
||||
var unused_bits uint32 = unused_bytes << 3
|
||||
br.byte_pos -= uint(unused_bytes)
|
||||
if unused_bits == 64 {
|
||||
|
@ -192,40 +192,40 @@ func BrotliBitReaderUnload(br *BrotliBitReader) {
|
|||
|
||||
/* Reads the specified number of bits from |br| and advances the bit pos.
|
||||
Precondition: accumulator MUST contain at least |n_bits|. */
|
||||
func BrotliTakeBits(br *BrotliBitReader, n_bits uint32, val *uint32) {
|
||||
*val = uint32(BrotliGetBitsUnmasked(br)) & BitMask(n_bits)
|
||||
BrotliDropBits(br, n_bits)
|
||||
func takeBits(br *bitReader, n_bits uint32, val *uint32) {
|
||||
*val = uint32(getBitsUnmasked(br)) & bitMask(n_bits)
|
||||
dropBits(br, n_bits)
|
||||
}
|
||||
|
||||
/* Reads the specified number of bits from |br| and advances the bit pos.
|
||||
Assumes that there is enough input to perform BrotliFillBitWindow. */
|
||||
func BrotliReadBits(br *BrotliBitReader, n_bits uint32) uint32 {
|
||||
func readBits(br *bitReader, n_bits uint32) uint32 {
|
||||
var val uint32
|
||||
BrotliFillBitWindow(br, n_bits)
|
||||
BrotliTakeBits(br, n_bits, &val)
|
||||
fillBitWindow(br, n_bits)
|
||||
takeBits(br, n_bits, &val)
|
||||
return val
|
||||
}
|
||||
|
||||
/* Tries to read the specified amount of bits. Returns false, if there
|
||||
is not enough input. |n_bits| MUST be positive. */
|
||||
func BrotliSafeReadBits(br *BrotliBitReader, n_bits uint32, val *uint32) bool {
|
||||
for BrotliGetAvailableBits(br) < n_bits {
|
||||
if !BrotliPullByte(br) {
|
||||
func safeReadBits(br *bitReader, n_bits uint32, val *uint32) bool {
|
||||
for getAvailableBits(br) < n_bits {
|
||||
if !pullByte(br) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
BrotliTakeBits(br, n_bits, val)
|
||||
takeBits(br, n_bits, val)
|
||||
return true
|
||||
}
|
||||
|
||||
/* Advances the bit reader position to the next byte boundary and verifies
|
||||
that any skipped bits are set to zero. */
|
||||
func BrotliJumpToByteBoundary(br *BrotliBitReader) bool {
|
||||
var pad_bits_count uint32 = BrotliGetAvailableBits(br) & 0x7
|
||||
func jumpToByteBoundary(br *bitReader) bool {
|
||||
var pad_bits_count uint32 = getAvailableBits(br) & 0x7
|
||||
var pad_bits uint32 = 0
|
||||
if pad_bits_count != 0 {
|
||||
BrotliTakeBits(br, pad_bits_count, &pad_bits)
|
||||
takeBits(br, pad_bits_count, &pad_bits)
|
||||
}
|
||||
|
||||
return pad_bits == 0
|
||||
|
@ -234,10 +234,10 @@ func BrotliJumpToByteBoundary(br *BrotliBitReader) bool {
|
|||
/* Copies remaining input bytes stored in the bit reader to the output. Value
|
||||
|num| may not be larger than BrotliGetRemainingBytes. The bit reader must be
|
||||
warmed up again after this. */
|
||||
func BrotliCopyBytes(dest []byte, br *BrotliBitReader, num uint) {
|
||||
for BrotliGetAvailableBits(br) >= 8 && num > 0 {
|
||||
dest[0] = byte(BrotliGetBitsUnmasked(br))
|
||||
BrotliDropBits(br, 8)
|
||||
func copyBytes(dest []byte, br *bitReader, num uint) {
|
||||
for getAvailableBits(br) >= 8 && num > 0 {
|
||||
dest[0] = byte(getBitsUnmasked(br))
|
||||
dropBits(br, 8)
|
||||
dest = dest[1:]
|
||||
num--
|
||||
}
|
||||
|
@ -246,17 +246,17 @@ func BrotliCopyBytes(dest []byte, br *BrotliBitReader, num uint) {
|
|||
br.byte_pos += num
|
||||
}
|
||||
|
||||
func BrotliInitBitReader(br *BrotliBitReader) {
|
||||
func initBitReader(br *bitReader) {
|
||||
br.val_ = 0
|
||||
br.bit_pos_ = 64
|
||||
}
|
||||
|
||||
func BrotliWarmupBitReader(br *BrotliBitReader) bool {
|
||||
func warmupBitReader(br *bitReader) bool {
|
||||
/* Fixing alignment after unaligned BrotliFillWindow would result accumulator
|
||||
overflow. If unalignment is caused by BrotliSafeReadBits, then there is
|
||||
enough space in accumulator to fix alignment. */
|
||||
if BrotliGetAvailableBits(br) == 0 {
|
||||
if !BrotliPullByte(br) {
|
||||
if getAvailableBits(br) == 0 {
|
||||
if !pullByte(br) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
|
|
@ -239,7 +239,7 @@ func ClusterBlocksCommand(data []uint16, length uint, num_blocks uint, block_ids
|
|||
pos++
|
||||
}
|
||||
|
||||
histograms[j].bit_cost_ = BrotliPopulationCostCommand(&histograms[j])
|
||||
histograms[j].bit_cost_ = populationCostCommand(&histograms[j])
|
||||
new_clusters[j] = uint32(j)
|
||||
symbols[j] = uint32(j)
|
||||
sizes[j] = 1
|
||||
|
|
|
@ -239,7 +239,7 @@ func ClusterBlocksDistance(data []uint16, length uint, num_blocks uint, block_id
|
|||
pos++
|
||||
}
|
||||
|
||||
histograms[j].bit_cost_ = BrotliPopulationCostDistance(&histograms[j])
|
||||
histograms[j].bit_cost_ = populationCostDistance(&histograms[j])
|
||||
new_clusters[j] = uint32(j)
|
||||
symbols[j] = uint32(j)
|
||||
sizes[j] = 1
|
||||
|
|
|
@ -239,7 +239,7 @@ func ClusterBlocksLiteral(data []byte, length uint, num_blocks uint, block_ids [
|
|||
pos++
|
||||
}
|
||||
|
||||
histograms[j].bit_cost_ = BrotliPopulationCostLiteral(&histograms[j])
|
||||
histograms[j].bit_cost_ = populationCostLiteral(&histograms[j])
|
||||
new_clusters[j] = uint32(j)
|
||||
symbols[j] = uint32(j)
|
||||
sizes[j] = 1
|
||||
|
|
|
@ -48,7 +48,7 @@ func BrotliCompareAndPushToQueueCommand(out []HistogramCommand, cluster_size []u
|
|||
var combo HistogramCommand = out[idx1]
|
||||
var cost_combo float64
|
||||
HistogramAddHistogramCommand(&combo, &out[idx2])
|
||||
cost_combo = BrotliPopulationCostCommand(&combo)
|
||||
cost_combo = populationCostCommand(&combo)
|
||||
if cost_combo < threshold-p.cost_diff {
|
||||
p.cost_combo = cost_combo
|
||||
is_good_pair = true
|
||||
|
@ -160,7 +160,7 @@ func BrotliHistogramBitCostDistanceCommand(histogram *HistogramCommand, candidat
|
|||
} else {
|
||||
var tmp HistogramCommand = *histogram
|
||||
HistogramAddHistogramCommand(&tmp, candidate)
|
||||
return BrotliPopulationCostCommand(&tmp) - candidate.bit_cost_
|
||||
return populationCostCommand(&tmp) - candidate.bit_cost_
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -269,7 +269,7 @@ func BrotliClusterHistogramsCommand(in []HistogramCommand, in_size uint, max_his
|
|||
|
||||
for i = 0; i < in_size; i++ {
|
||||
out[i] = in[i]
|
||||
out[i].bit_cost_ = BrotliPopulationCostCommand(&in[i])
|
||||
out[i].bit_cost_ = populationCostCommand(&in[i])
|
||||
histogram_symbols[i] = uint32(i)
|
||||
}
|
||||
|
||||
|
|
|
@ -48,7 +48,7 @@ func BrotliCompareAndPushToQueueDistance(out []HistogramDistance, cluster_size [
|
|||
var combo HistogramDistance = out[idx1]
|
||||
var cost_combo float64
|
||||
HistogramAddHistogramDistance(&combo, &out[idx2])
|
||||
cost_combo = BrotliPopulationCostDistance(&combo)
|
||||
cost_combo = populationCostDistance(&combo)
|
||||
if cost_combo < threshold-p.cost_diff {
|
||||
p.cost_combo = cost_combo
|
||||
is_good_pair = true
|
||||
|
@ -160,7 +160,7 @@ func BrotliHistogramBitCostDistanceDistance(histogram *HistogramDistance, candid
|
|||
} else {
|
||||
var tmp HistogramDistance = *histogram
|
||||
HistogramAddHistogramDistance(&tmp, candidate)
|
||||
return BrotliPopulationCostDistance(&tmp) - candidate.bit_cost_
|
||||
return populationCostDistance(&tmp) - candidate.bit_cost_
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -269,7 +269,7 @@ func BrotliClusterHistogramsDistance(in []HistogramDistance, in_size uint, max_h
|
|||
|
||||
for i = 0; i < in_size; i++ {
|
||||
out[i] = in[i]
|
||||
out[i].bit_cost_ = BrotliPopulationCostDistance(&in[i])
|
||||
out[i].bit_cost_ = populationCostDistance(&in[i])
|
||||
histogram_symbols[i] = uint32(i)
|
||||
}
|
||||
|
||||
|
|
|
@ -48,7 +48,7 @@ func BrotliCompareAndPushToQueueLiteral(out []HistogramLiteral, cluster_size []u
|
|||
var combo HistogramLiteral = out[idx1]
|
||||
var cost_combo float64
|
||||
HistogramAddHistogramLiteral(&combo, &out[idx2])
|
||||
cost_combo = BrotliPopulationCostLiteral(&combo)
|
||||
cost_combo = populationCostLiteral(&combo)
|
||||
if cost_combo < threshold-p.cost_diff {
|
||||
p.cost_combo = cost_combo
|
||||
is_good_pair = true
|
||||
|
@ -160,7 +160,7 @@ func BrotliHistogramBitCostDistanceLiteral(histogram *HistogramLiteral, candidat
|
|||
} else {
|
||||
var tmp HistogramLiteral = *histogram
|
||||
HistogramAddHistogramLiteral(&tmp, candidate)
|
||||
return BrotliPopulationCostLiteral(&tmp) - candidate.bit_cost_
|
||||
return populationCostLiteral(&tmp) - candidate.bit_cost_
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -269,7 +269,7 @@ func BrotliClusterHistogramsLiteral(in []HistogramLiteral, in_size uint, max_his
|
|||
|
||||
for i = 0; i < in_size; i++ {
|
||||
out[i] = in[i]
|
||||
out[i].bit_cost_ = BrotliPopulationCostLiteral(&in[i])
|
||||
out[i].bit_cost_ = populationCostLiteral(&in[i])
|
||||
histogram_symbols[i] = uint32(i)
|
||||
}
|
||||
|
||||
|
|
|
@ -670,7 +670,7 @@ func ShouldCompress(input []byte, input_size uint, num_literals uint) bool {
|
|||
literal_histo[input[i]]++
|
||||
}
|
||||
|
||||
return BitsEntropy(literal_histo[:], 256) < max_total_bit_cost
|
||||
return bitsEntropy(literal_histo[:], 256) < max_total_bit_cost
|
||||
}
|
||||
}
|
||||
|
||||
|
|
238
decode.go
238
decode.go
|
@ -156,26 +156,26 @@ func SaveErrorCode(s *Reader, e int) int {
|
|||
|
||||
/* Decodes WBITS by reading 1 - 7 bits, or 0x11 for "Large Window Brotli".
|
||||
Precondition: bit-reader accumulator has at least 8 bits. */
|
||||
func DecodeWindowBits(s *Reader, br *BrotliBitReader) int {
|
||||
func DecodeWindowBits(s *Reader, br *bitReader) int {
|
||||
var n uint32
|
||||
var large_window bool = s.large_window
|
||||
s.large_window = false
|
||||
BrotliTakeBits(br, 1, &n)
|
||||
takeBits(br, 1, &n)
|
||||
if n == 0 {
|
||||
s.window_bits = 16
|
||||
return BROTLI_DECODER_SUCCESS
|
||||
}
|
||||
|
||||
BrotliTakeBits(br, 3, &n)
|
||||
takeBits(br, 3, &n)
|
||||
if n != 0 {
|
||||
s.window_bits = 17 + n
|
||||
return BROTLI_DECODER_SUCCESS
|
||||
}
|
||||
|
||||
BrotliTakeBits(br, 3, &n)
|
||||
takeBits(br, 3, &n)
|
||||
if n == 1 {
|
||||
if large_window {
|
||||
BrotliTakeBits(br, 1, &n)
|
||||
takeBits(br, 1, &n)
|
||||
if n == 1 {
|
||||
return BROTLI_DECODER_ERROR_FORMAT_WINDOW_BITS
|
||||
}
|
||||
|
@ -197,11 +197,11 @@ func DecodeWindowBits(s *Reader, br *BrotliBitReader) int {
|
|||
}
|
||||
|
||||
/* Decodes a number in the range [0..255], by reading 1 - 11 bits. */
|
||||
func DecodeVarLenUint8(s *Reader, br *BrotliBitReader, value *uint32) int {
|
||||
func DecodeVarLenUint8(s *Reader, br *bitReader, value *uint32) int {
|
||||
var bits uint32
|
||||
switch s.substate_decode_uint8 {
|
||||
case BROTLI_STATE_DECODE_UINT8_NONE:
|
||||
if !BrotliSafeReadBits(br, 1, &bits) {
|
||||
if !safeReadBits(br, 1, &bits) {
|
||||
return BROTLI_DECODER_NEEDS_MORE_INPUT
|
||||
}
|
||||
|
||||
|
@ -213,7 +213,7 @@ func DecodeVarLenUint8(s *Reader, br *BrotliBitReader, value *uint32) int {
|
|||
|
||||
/* Fall through. */
|
||||
case BROTLI_STATE_DECODE_UINT8_SHORT:
|
||||
if !BrotliSafeReadBits(br, 3, &bits) {
|
||||
if !safeReadBits(br, 3, &bits) {
|
||||
s.substate_decode_uint8 = BROTLI_STATE_DECODE_UINT8_SHORT
|
||||
return BROTLI_DECODER_NEEDS_MORE_INPUT
|
||||
}
|
||||
|
@ -230,7 +230,7 @@ func DecodeVarLenUint8(s *Reader, br *BrotliBitReader, value *uint32) int {
|
|||
|
||||
/* Fall through. */
|
||||
case BROTLI_STATE_DECODE_UINT8_LONG:
|
||||
if !BrotliSafeReadBits(br, *value, &bits) {
|
||||
if !safeReadBits(br, *value, &bits) {
|
||||
s.substate_decode_uint8 = BROTLI_STATE_DECODE_UINT8_LONG
|
||||
return BROTLI_DECODER_NEEDS_MORE_INPUT
|
||||
}
|
||||
|
@ -245,13 +245,13 @@ func DecodeVarLenUint8(s *Reader, br *BrotliBitReader, value *uint32) int {
|
|||
}
|
||||
|
||||
/* Decodes a metablock length and flags by reading 2 - 31 bits. */
|
||||
func DecodeMetaBlockLength(s *Reader, br *BrotliBitReader) int {
|
||||
func DecodeMetaBlockLength(s *Reader, br *bitReader) int {
|
||||
var bits uint32
|
||||
var i int
|
||||
for {
|
||||
switch s.substate_metablock_header {
|
||||
case BROTLI_STATE_METABLOCK_HEADER_NONE:
|
||||
if !BrotliSafeReadBits(br, 1, &bits) {
|
||||
if !safeReadBits(br, 1, &bits) {
|
||||
return BROTLI_DECODER_NEEDS_MORE_INPUT
|
||||
}
|
||||
|
||||
|
@ -273,7 +273,7 @@ func DecodeMetaBlockLength(s *Reader, br *BrotliBitReader) int {
|
|||
|
||||
/* Fall through. */
|
||||
case BROTLI_STATE_METABLOCK_HEADER_EMPTY:
|
||||
if !BrotliSafeReadBits(br, 1, &bits) {
|
||||
if !safeReadBits(br, 1, &bits) {
|
||||
return BROTLI_DECODER_NEEDS_MORE_INPUT
|
||||
}
|
||||
|
||||
|
@ -287,7 +287,7 @@ func DecodeMetaBlockLength(s *Reader, br *BrotliBitReader) int {
|
|||
|
||||
/* Fall through. */
|
||||
case BROTLI_STATE_METABLOCK_HEADER_NIBBLES:
|
||||
if !BrotliSafeReadBits(br, 2, &bits) {
|
||||
if !safeReadBits(br, 2, &bits) {
|
||||
return BROTLI_DECODER_NEEDS_MORE_INPUT
|
||||
}
|
||||
|
||||
|
@ -307,7 +307,7 @@ func DecodeMetaBlockLength(s *Reader, br *BrotliBitReader) int {
|
|||
i = s.loop_counter
|
||||
|
||||
for ; i < int(s.size_nibbles); i++ {
|
||||
if !BrotliSafeReadBits(br, 4, &bits) {
|
||||
if !safeReadBits(br, 4, &bits) {
|
||||
s.loop_counter = i
|
||||
return BROTLI_DECODER_NEEDS_MORE_INPUT
|
||||
}
|
||||
|
@ -325,7 +325,7 @@ func DecodeMetaBlockLength(s *Reader, br *BrotliBitReader) int {
|
|||
/* Fall through. */
|
||||
case BROTLI_STATE_METABLOCK_HEADER_UNCOMPRESSED:
|
||||
if s.is_last_metablock == 0 {
|
||||
if !BrotliSafeReadBits(br, 1, &bits) {
|
||||
if !safeReadBits(br, 1, &bits) {
|
||||
return BROTLI_DECODER_NEEDS_MORE_INPUT
|
||||
}
|
||||
|
||||
|
@ -341,7 +341,7 @@ func DecodeMetaBlockLength(s *Reader, br *BrotliBitReader) int {
|
|||
return BROTLI_DECODER_SUCCESS
|
||||
|
||||
case BROTLI_STATE_METABLOCK_HEADER_RESERVED:
|
||||
if !BrotliSafeReadBits(br, 1, &bits) {
|
||||
if !safeReadBits(br, 1, &bits) {
|
||||
return BROTLI_DECODER_NEEDS_MORE_INPUT
|
||||
}
|
||||
|
||||
|
@ -354,7 +354,7 @@ func DecodeMetaBlockLength(s *Reader, br *BrotliBitReader) int {
|
|||
|
||||
/* Fall through. */
|
||||
case BROTLI_STATE_METABLOCK_HEADER_BYTES:
|
||||
if !BrotliSafeReadBits(br, 2, &bits) {
|
||||
if !safeReadBits(br, 2, &bits) {
|
||||
return BROTLI_DECODER_NEEDS_MORE_INPUT
|
||||
}
|
||||
|
||||
|
@ -372,7 +372,7 @@ func DecodeMetaBlockLength(s *Reader, br *BrotliBitReader) int {
|
|||
i = s.loop_counter
|
||||
|
||||
for ; i < int(s.size_nibbles); i++ {
|
||||
if !BrotliSafeReadBits(br, 8, &bits) {
|
||||
if !safeReadBits(br, 8, &bits) {
|
||||
s.loop_counter = i
|
||||
return BROTLI_DECODER_NEEDS_MORE_INPUT
|
||||
}
|
||||
|
@ -398,29 +398,29 @@ func DecodeMetaBlockLength(s *Reader, br *BrotliBitReader) int {
|
|||
This method doesn't read data from the bit reader, BUT drops the amount of
|
||||
bits that correspond to the decoded symbol.
|
||||
bits MUST contain at least 15 (BROTLI_HUFFMAN_MAX_CODE_LENGTH) valid bits. */
|
||||
func DecodeSymbol(bits uint32, table []HuffmanCode, br *BrotliBitReader) uint32 {
|
||||
func DecodeSymbol(bits uint32, table []HuffmanCode, br *bitReader) uint32 {
|
||||
table = table[bits&HUFFMAN_TABLE_MASK:]
|
||||
if table[0].bits > HUFFMAN_TABLE_BITS {
|
||||
var nbits uint32 = uint32(table[0].bits) - HUFFMAN_TABLE_BITS
|
||||
BrotliDropBits(br, HUFFMAN_TABLE_BITS)
|
||||
table = table[uint32(table[0].value)+((bits>>HUFFMAN_TABLE_BITS)&BitMask(nbits)):]
|
||||
dropBits(br, HUFFMAN_TABLE_BITS)
|
||||
table = table[uint32(table[0].value)+((bits>>HUFFMAN_TABLE_BITS)&bitMask(nbits)):]
|
||||
}
|
||||
|
||||
BrotliDropBits(br, uint32(table[0].bits))
|
||||
dropBits(br, uint32(table[0].bits))
|
||||
return uint32(table[0].value)
|
||||
}
|
||||
|
||||
/* Reads and decodes the next Huffman code from bit-stream.
|
||||
This method peeks 16 bits of input and drops 0 - 15 of them. */
|
||||
func ReadSymbol(table []HuffmanCode, br *BrotliBitReader) uint32 {
|
||||
return DecodeSymbol(BrotliGet16BitsUnmasked(br), table, br)
|
||||
func ReadSymbol(table []HuffmanCode, br *bitReader) uint32 {
|
||||
return DecodeSymbol(get16BitsUnmasked(br), table, br)
|
||||
}
|
||||
|
||||
/* Same as DecodeSymbol, but it is known that there is less than 15 bits of
|
||||
input are currently available. */
|
||||
func SafeDecodeSymbol(table []HuffmanCode, br *BrotliBitReader, result *uint32) bool {
|
||||
func SafeDecodeSymbol(table []HuffmanCode, br *bitReader, result *uint32) bool {
|
||||
var val uint32
|
||||
var available_bits uint32 = BrotliGetAvailableBits(br)
|
||||
var available_bits uint32 = getAvailableBits(br)
|
||||
if available_bits == 0 {
|
||||
if table[0].bits == 0 {
|
||||
*result = uint32(table[0].value)
|
||||
|
@ -430,11 +430,11 @@ func SafeDecodeSymbol(table []HuffmanCode, br *BrotliBitReader, result *uint32)
|
|||
return false /* No valid bits at all. */
|
||||
}
|
||||
|
||||
val = uint32(BrotliGetBitsUnmasked(br))
|
||||
val = uint32(getBitsUnmasked(br))
|
||||
table = table[val&HUFFMAN_TABLE_MASK:]
|
||||
if table[0].bits <= HUFFMAN_TABLE_BITS {
|
||||
if uint32(table[0].bits) <= available_bits {
|
||||
BrotliDropBits(br, uint32(table[0].bits))
|
||||
dropBits(br, uint32(table[0].bits))
|
||||
*result = uint32(table[0].value)
|
||||
return true
|
||||
} else {
|
||||
|
@ -447,7 +447,7 @@ func SafeDecodeSymbol(table []HuffmanCode, br *BrotliBitReader, result *uint32)
|
|||
}
|
||||
|
||||
/* Speculatively drop HUFFMAN_TABLE_BITS. */
|
||||
val = (val & BitMask(uint32(table[0].bits))) >> HUFFMAN_TABLE_BITS
|
||||
val = (val & bitMask(uint32(table[0].bits))) >> HUFFMAN_TABLE_BITS
|
||||
|
||||
available_bits -= HUFFMAN_TABLE_BITS
|
||||
table = table[uint32(table[0].value)+val:]
|
||||
|
@ -455,14 +455,14 @@ func SafeDecodeSymbol(table []HuffmanCode, br *BrotliBitReader, result *uint32)
|
|||
return false /* Not enough bits for the second level. */
|
||||
}
|
||||
|
||||
BrotliDropBits(br, HUFFMAN_TABLE_BITS+uint32(table[0].bits))
|
||||
dropBits(br, HUFFMAN_TABLE_BITS+uint32(table[0].bits))
|
||||
*result = uint32(table[0].value)
|
||||
return true
|
||||
}
|
||||
|
||||
func SafeReadSymbol(table []HuffmanCode, br *BrotliBitReader, result *uint32) bool {
|
||||
func SafeReadSymbol(table []HuffmanCode, br *bitReader, result *uint32) bool {
|
||||
var val uint32
|
||||
if BrotliSafeGetBits(br, 15, &val) {
|
||||
if safeGetBits(br, 15, &val) {
|
||||
*result = DecodeSymbol(val, table, br)
|
||||
return true
|
||||
}
|
||||
|
@ -471,7 +471,7 @@ func SafeReadSymbol(table []HuffmanCode, br *BrotliBitReader, result *uint32) bo
|
|||
}
|
||||
|
||||
/* Makes a look-up in first level Huffman table. Peeks 8 bits. */
|
||||
func PreloadSymbol(safe int, table []HuffmanCode, br *BrotliBitReader, bits *uint32, value *uint32) {
|
||||
func PreloadSymbol(safe int, table []HuffmanCode, br *bitReader, bits *uint32, value *uint32) {
|
||||
if safe != 0 {
|
||||
return
|
||||
}
|
||||
|
@ -483,19 +483,19 @@ func PreloadSymbol(safe int, table []HuffmanCode, br *BrotliBitReader, bits *uin
|
|||
|
||||
/* Decodes the next Huffman code using data prepared by PreloadSymbol.
|
||||
Reads 0 - 15 bits. Also peeks 8 following bits. */
|
||||
func ReadPreloadedSymbol(table []HuffmanCode, br *BrotliBitReader, bits *uint32, value *uint32) uint32 {
|
||||
func ReadPreloadedSymbol(table []HuffmanCode, br *bitReader, bits *uint32, value *uint32) uint32 {
|
||||
var result uint32 = *value
|
||||
var ext []HuffmanCode
|
||||
if *bits > HUFFMAN_TABLE_BITS {
|
||||
var val uint32 = BrotliGet16BitsUnmasked(br)
|
||||
var val uint32 = get16BitsUnmasked(br)
|
||||
ext = table[val&HUFFMAN_TABLE_MASK:][*value:]
|
||||
var mask uint32 = BitMask((*bits - HUFFMAN_TABLE_BITS))
|
||||
BrotliDropBits(br, HUFFMAN_TABLE_BITS)
|
||||
var mask uint32 = bitMask((*bits - HUFFMAN_TABLE_BITS))
|
||||
dropBits(br, HUFFMAN_TABLE_BITS)
|
||||
ext = ext[(val>>HUFFMAN_TABLE_BITS)&mask:]
|
||||
BrotliDropBits(br, uint32(ext[0].bits))
|
||||
dropBits(br, uint32(ext[0].bits))
|
||||
result = uint32(ext[0].value)
|
||||
} else {
|
||||
BrotliDropBits(br, *bits)
|
||||
dropBits(br, *bits)
|
||||
}
|
||||
|
||||
PreloadSymbol(0, table, br, bits, value)
|
||||
|
@ -516,7 +516,7 @@ func Log2Floor(x uint32) uint32 {
|
|||
Totally 1..4 symbols are read, 1..11 bits each.
|
||||
The list of symbols MUST NOT contain duplicates. */
|
||||
func ReadSimpleHuffmanSymbols(alphabet_size uint32, max_symbol uint32, s *Reader) int {
|
||||
var br *BrotliBitReader = &s.br
|
||||
var br *bitReader = &s.br
|
||||
var max_bits uint32 = Log2Floor(alphabet_size - 1)
|
||||
var i uint32 = s.sub_loop_counter
|
||||
/* max_bits == 1..11; symbol == 0..3; 1..44 bits will be read. */
|
||||
|
@ -524,7 +524,7 @@ func ReadSimpleHuffmanSymbols(alphabet_size uint32, max_symbol uint32, s *Reader
|
|||
var num_symbols uint32 = s.symbol
|
||||
for i <= num_symbols {
|
||||
var v uint32
|
||||
if !BrotliSafeReadBits(br, max_bits, &v) {
|
||||
if !safeReadBits(br, max_bits, &v) {
|
||||
s.sub_loop_counter = i
|
||||
s.substate_huffman = BROTLI_STATE_HUFFMAN_SIMPLE_READ
|
||||
return BROTLI_DECODER_NEEDS_MORE_INPUT
|
||||
|
@ -629,7 +629,7 @@ func ProcessRepeatedCodeLength(code_len uint32, repeat_delta uint32, alphabet_si
|
|||
|
||||
/* Reads and decodes symbol codelengths. */
|
||||
func ReadSymbolCodeLengths(alphabet_size uint32, s *Reader) int {
|
||||
var br *BrotliBitReader = &s.br
|
||||
var br *bitReader = &s.br
|
||||
var symbol uint32 = s.symbol
|
||||
var repeat uint32 = s.repeat
|
||||
var space uint32 = s.space
|
||||
|
@ -638,14 +638,14 @@ func ReadSymbolCodeLengths(alphabet_size uint32, s *Reader) int {
|
|||
var symbol_lists SymbolList = s.symbol_lists
|
||||
var code_length_histo []uint16 = s.code_length_histo[:]
|
||||
var next_symbol []int = s.next_symbol[:]
|
||||
if !BrotliWarmupBitReader(br) {
|
||||
if !warmupBitReader(br) {
|
||||
return BROTLI_DECODER_NEEDS_MORE_INPUT
|
||||
}
|
||||
var p []HuffmanCode
|
||||
for symbol < alphabet_size && space > 0 {
|
||||
p = s.table[:]
|
||||
var code_len uint32
|
||||
if !BrotliCheckInputAmount(br, BROTLI_SHORT_FILL_BIT_WINDOW_READ) {
|
||||
if !checkInputAmount(br, shortFillBitWindowRead) {
|
||||
s.symbol = symbol
|
||||
s.repeat = repeat
|
||||
s.prev_code_len = prev_code_len
|
||||
|
@ -654,9 +654,9 @@ func ReadSymbolCodeLengths(alphabet_size uint32, s *Reader) int {
|
|||
return BROTLI_DECODER_NEEDS_MORE_INPUT
|
||||
}
|
||||
|
||||
BrotliFillBitWindow16(br)
|
||||
p = p[BrotliGetBitsUnmasked(br)&uint64(BitMask(BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH)):]
|
||||
BrotliDropBits(br, uint32(p[0].bits)) /* Use 1..5 bits. */
|
||||
fillBitWindow16(br)
|
||||
p = p[getBitsUnmasked(br)&uint64(bitMask(BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH)):]
|
||||
dropBits(br, uint32(p[0].bits)) /* Use 1..5 bits. */
|
||||
code_len = uint32(p[0].value) /* code_len == 0..17 */
|
||||
if code_len < BROTLI_REPEAT_PREVIOUS_CODE_LENGTH {
|
||||
ProcessSingleCodeLength(code_len, &symbol, &repeat, &space, &prev_code_len, symbol_lists, code_length_histo, next_symbol) /* code_len == 16..17, extra_bits == 2..3 */
|
||||
|
@ -667,8 +667,8 @@ func ReadSymbolCodeLengths(alphabet_size uint32, s *Reader) int {
|
|||
} else {
|
||||
extra_bits = 3
|
||||
}
|
||||
var repeat_delta uint32 = uint32(BrotliGetBitsUnmasked(br)) & BitMask(extra_bits)
|
||||
BrotliDropBits(br, extra_bits)
|
||||
var repeat_delta uint32 = uint32(getBitsUnmasked(br)) & bitMask(extra_bits)
|
||||
dropBits(br, extra_bits)
|
||||
ProcessRepeatedCodeLength(code_len, repeat_delta, alphabet_size, &symbol, &repeat, &space, &prev_code_len, &repeat_code_len, symbol_lists, code_length_histo, next_symbol)
|
||||
}
|
||||
}
|
||||
|
@ -678,7 +678,7 @@ func ReadSymbolCodeLengths(alphabet_size uint32, s *Reader) int {
|
|||
}
|
||||
|
||||
func SafeReadSymbolCodeLengths(alphabet_size uint32, s *Reader) int {
|
||||
var br *BrotliBitReader = &s.br
|
||||
var br *bitReader = &s.br
|
||||
var get_byte bool = false
|
||||
var p []HuffmanCode
|
||||
for s.symbol < alphabet_size && s.space > 0 {
|
||||
|
@ -686,16 +686,16 @@ func SafeReadSymbolCodeLengths(alphabet_size uint32, s *Reader) int {
|
|||
var code_len uint32
|
||||
var available_bits uint32
|
||||
var bits uint32 = 0
|
||||
if get_byte && !BrotliPullByte(br) {
|
||||
if get_byte && !pullByte(br) {
|
||||
return BROTLI_DECODER_NEEDS_MORE_INPUT
|
||||
}
|
||||
get_byte = false
|
||||
available_bits = BrotliGetAvailableBits(br)
|
||||
available_bits = getAvailableBits(br)
|
||||
if available_bits != 0 {
|
||||
bits = uint32(BrotliGetBitsUnmasked(br))
|
||||
bits = uint32(getBitsUnmasked(br))
|
||||
}
|
||||
|
||||
p = p[bits&BitMask(BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH):]
|
||||
p = p[bits&bitMask(BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH):]
|
||||
if uint32(p[0].bits) > available_bits {
|
||||
get_byte = true
|
||||
continue
|
||||
|
@ -703,17 +703,17 @@ func SafeReadSymbolCodeLengths(alphabet_size uint32, s *Reader) int {
|
|||
|
||||
code_len = uint32(p[0].value) /* code_len == 0..17 */
|
||||
if code_len < BROTLI_REPEAT_PREVIOUS_CODE_LENGTH {
|
||||
BrotliDropBits(br, uint32(p[0].bits))
|
||||
dropBits(br, uint32(p[0].bits))
|
||||
ProcessSingleCodeLength(code_len, &s.symbol, &s.repeat, &s.space, &s.prev_code_len, s.symbol_lists, s.code_length_histo[:], s.next_symbol[:]) /* code_len == 16..17, extra_bits == 2..3 */
|
||||
} else {
|
||||
var extra_bits uint32 = code_len - 14
|
||||
var repeat_delta uint32 = (bits >> p[0].bits) & BitMask(extra_bits)
|
||||
var repeat_delta uint32 = (bits >> p[0].bits) & bitMask(extra_bits)
|
||||
if available_bits < uint32(p[0].bits)+extra_bits {
|
||||
get_byte = true
|
||||
continue
|
||||
}
|
||||
|
||||
BrotliDropBits(br, uint32(p[0].bits)+extra_bits)
|
||||
dropBits(br, uint32(p[0].bits)+extra_bits)
|
||||
ProcessRepeatedCodeLength(code_len, repeat_delta, alphabet_size, &s.symbol, &s.repeat, &s.space, &s.prev_code_len, &s.repeat_code_len, s.symbol_lists, s.code_length_histo[:], s.next_symbol[:])
|
||||
}
|
||||
}
|
||||
|
@ -724,7 +724,7 @@ func SafeReadSymbolCodeLengths(alphabet_size uint32, s *Reader) int {
|
|||
/* Reads and decodes 15..18 codes using static prefix code.
|
||||
Each code is 2..4 bits long. In total 30..72 bits are used. */
|
||||
func ReadCodeLengthCodeLengths(s *Reader) int {
|
||||
var br *BrotliBitReader = &s.br
|
||||
var br *bitReader = &s.br
|
||||
var num_codes uint32 = s.repeat
|
||||
var space uint32 = s.space
|
||||
var i uint32 = s.sub_loop_counter
|
||||
|
@ -732,10 +732,10 @@ func ReadCodeLengthCodeLengths(s *Reader) int {
|
|||
var code_len_idx byte = kCodeLengthCodeOrder[i]
|
||||
var ix uint32
|
||||
var v uint32
|
||||
if !BrotliSafeGetBits(br, 4, &ix) {
|
||||
var available_bits uint32 = BrotliGetAvailableBits(br)
|
||||
if !safeGetBits(br, 4, &ix) {
|
||||
var available_bits uint32 = getAvailableBits(br)
|
||||
if available_bits != 0 {
|
||||
ix = uint32(BrotliGetBitsUnmasked(br) & 0xF)
|
||||
ix = uint32(getBitsUnmasked(br) & 0xF)
|
||||
} else {
|
||||
ix = 0
|
||||
}
|
||||
|
@ -750,7 +750,7 @@ func ReadCodeLengthCodeLengths(s *Reader) int {
|
|||
}
|
||||
|
||||
v = uint32(kCodeLengthPrefixValue[ix])
|
||||
BrotliDropBits(br, uint32(kCodeLengthPrefixLength[ix]))
|
||||
dropBits(br, uint32(kCodeLengthPrefixLength[ix]))
|
||||
s.code_length_code_lengths[code_len_idx] = byte(v)
|
||||
if v != 0 {
|
||||
space = space - (32 >> v)
|
||||
|
@ -782,7 +782,7 @@ func ReadCodeLengthCodeLengths(s *Reader) int {
|
|||
B.2) Decoded table is used to decode code lengths of symbols in resulting
|
||||
Huffman table. In worst case 3520 bits are read. */
|
||||
func ReadHuffmanCode(alphabet_size uint32, max_symbol uint32, table []HuffmanCode, opt_table_size *uint32, s *Reader) int {
|
||||
var br *BrotliBitReader = &s.br
|
||||
var br *bitReader = &s.br
|
||||
|
||||
/* Unnecessary masking, but might be good for safety. */
|
||||
alphabet_size &= 0x7FF
|
||||
|
@ -791,7 +791,7 @@ func ReadHuffmanCode(alphabet_size uint32, max_symbol uint32, table []HuffmanCod
|
|||
for {
|
||||
switch s.substate_huffman {
|
||||
case BROTLI_STATE_HUFFMAN_NONE:
|
||||
if !BrotliSafeReadBits(br, 2, &s.sub_loop_counter) {
|
||||
if !safeReadBits(br, 2, &s.sub_loop_counter) {
|
||||
return BROTLI_DECODER_NEEDS_MORE_INPUT
|
||||
}
|
||||
|
||||
|
@ -818,7 +818,7 @@ func ReadHuffmanCode(alphabet_size uint32, max_symbol uint32, table []HuffmanCod
|
|||
/* Read symbols, codes & code lengths directly. */
|
||||
/* Fall through. */
|
||||
case BROTLI_STATE_HUFFMAN_SIMPLE_SIZE:
|
||||
if !BrotliSafeReadBits(br, 2, &s.symbol) { /* num_symbols */
|
||||
if !safeReadBits(br, 2, &s.symbol) { /* num_symbols */
|
||||
s.substate_huffman = BROTLI_STATE_HUFFMAN_SIMPLE_SIZE
|
||||
return BROTLI_DECODER_NEEDS_MORE_INPUT
|
||||
}
|
||||
|
@ -841,7 +841,7 @@ func ReadHuffmanCode(alphabet_size uint32, max_symbol uint32, table []HuffmanCod
|
|||
var table_size uint32
|
||||
if s.symbol == 3 {
|
||||
var bits uint32
|
||||
if !BrotliSafeReadBits(br, 1, &bits) {
|
||||
if !safeReadBits(br, 1, &bits) {
|
||||
s.substate_huffman = BROTLI_STATE_HUFFMAN_SIMPLE_BUILD
|
||||
return BROTLI_DECODER_NEEDS_MORE_INPUT
|
||||
}
|
||||
|
@ -921,17 +921,17 @@ func ReadHuffmanCode(alphabet_size uint32, max_symbol uint32, table []HuffmanCod
|
|||
}
|
||||
|
||||
/* Decodes a block length by reading 3..39 bits. */
|
||||
func ReadBlockLength(table []HuffmanCode, br *BrotliBitReader) uint32 {
|
||||
func ReadBlockLength(table []HuffmanCode, br *bitReader) uint32 {
|
||||
var code uint32
|
||||
var nbits uint32
|
||||
code = ReadSymbol(table, br)
|
||||
nbits = uint32(kBlockLengthPrefixCode1[code].nbits) /* nbits == 2..24 */
|
||||
return uint32(kBlockLengthPrefixCode1[code].offset) + BrotliReadBits(br, nbits)
|
||||
return uint32(kBlockLengthPrefixCode1[code].offset) + readBits(br, nbits)
|
||||
}
|
||||
|
||||
/* WARNING: if state is not BROTLI_STATE_READ_BLOCK_LENGTH_NONE, then
|
||||
reading can't be continued with ReadBlockLength. */
|
||||
func SafeReadBlockLength(s *Reader, result *uint32, table []HuffmanCode, br *BrotliBitReader) bool {
|
||||
func SafeReadBlockLength(s *Reader, result *uint32, table []HuffmanCode, br *bitReader) bool {
|
||||
var index uint32
|
||||
if s.substate_read_block_length == BROTLI_STATE_READ_BLOCK_LENGTH_NONE {
|
||||
if !SafeReadSymbol(table, br, &index) {
|
||||
|
@ -943,7 +943,7 @@ func SafeReadBlockLength(s *Reader, result *uint32, table []HuffmanCode, br *Bro
|
|||
{
|
||||
var bits uint32 /* nbits == 2..24 */
|
||||
var nbits uint32 = uint32(kBlockLengthPrefixCode1[index].nbits)
|
||||
if !BrotliSafeReadBits(br, nbits, &bits) {
|
||||
if !safeReadBits(br, nbits, &bits) {
|
||||
s.block_length_index = index
|
||||
s.substate_read_block_length = BROTLI_STATE_READ_BLOCK_LENGTH_SUFFIX
|
||||
return false
|
||||
|
@ -1024,7 +1024,7 @@ func HuffmanTreeGroupDecode(group *HuffmanTreeGroup, s *Reader) int {
|
|||
3) Read context map items; "0" values could be run-length encoded.
|
||||
4) Optionally, apply InverseMoveToFront transform to the resulting map. */
|
||||
func DecodeContextMap(context_map_size uint32, num_htrees *uint32, context_map_arg *[]byte, s *Reader) int {
|
||||
var br *BrotliBitReader = &s.br
|
||||
var br *bitReader = &s.br
|
||||
var result int = BROTLI_DECODER_SUCCESS
|
||||
|
||||
switch int(s.substate_context_map) {
|
||||
|
@ -1057,16 +1057,16 @@ func DecodeContextMap(context_map_size uint32, num_htrees *uint32, context_map_a
|
|||
|
||||
/* In next stage ReadHuffmanCode uses at least 4 bits, so it is safe
|
||||
to peek 4 bits ahead. */
|
||||
if !BrotliSafeGetBits(br, 5, &bits) {
|
||||
if !safeGetBits(br, 5, &bits) {
|
||||
return BROTLI_DECODER_NEEDS_MORE_INPUT
|
||||
}
|
||||
|
||||
if bits&1 != 0 { /* Use RLE for zeros. */
|
||||
s.max_run_length_prefix = (bits >> 1) + 1
|
||||
BrotliDropBits(br, 5)
|
||||
dropBits(br, 5)
|
||||
} else {
|
||||
s.max_run_length_prefix = 0
|
||||
BrotliDropBits(br, 1)
|
||||
dropBits(br, 1)
|
||||
}
|
||||
|
||||
s.substate_context_map = BROTLI_STATE_CONTEXT_MAP_HUFFMAN
|
||||
|
@ -1120,7 +1120,7 @@ func DecodeContextMap(context_map_size uint32, num_htrees *uint32, context_map_a
|
|||
/* RLE sub-stage. */
|
||||
{
|
||||
var reps uint32
|
||||
if !BrotliSafeReadBits(br, code, &reps) {
|
||||
if !safeReadBits(br, code, &reps) {
|
||||
s.code = code
|
||||
s.context_index = context_index
|
||||
return BROTLI_DECODER_NEEDS_MORE_INPUT
|
||||
|
@ -1148,7 +1148,7 @@ func DecodeContextMap(context_map_size uint32, num_htrees *uint32, context_map_a
|
|||
case BROTLI_STATE_CONTEXT_MAP_TRANSFORM:
|
||||
{
|
||||
var bits uint32
|
||||
if !BrotliSafeReadBits(br, 1, &bits) {
|
||||
if !safeReadBits(br, 1, &bits) {
|
||||
s.substate_context_map = BROTLI_STATE_CONTEXT_MAP_TRANSFORM
|
||||
return BROTLI_DECODER_NEEDS_MORE_INPUT
|
||||
}
|
||||
|
@ -1175,7 +1175,7 @@ func DecodeBlockTypeAndLength(safe int, s *Reader, tree_type int) bool {
|
|||
type_tree = s.block_type_trees[tree_type*BROTLI_HUFFMAN_MAX_SIZE_258:]
|
||||
var len_tree []HuffmanCode
|
||||
len_tree = s.block_len_trees[tree_type*BROTLI_HUFFMAN_MAX_SIZE_26:]
|
||||
var br *BrotliBitReader = &s.br
|
||||
var br *bitReader = &s.br
|
||||
var ringbuffer []uint32 = s.block_type_rb[tree_type*2:]
|
||||
var block_type uint32
|
||||
if max_block_type <= 1 {
|
||||
|
@ -1187,14 +1187,14 @@ func DecodeBlockTypeAndLength(safe int, s *Reader, tree_type int) bool {
|
|||
block_type = ReadSymbol(type_tree, br)
|
||||
s.block_length[tree_type] = ReadBlockLength(len_tree, br)
|
||||
} else {
|
||||
var memento BrotliBitReaderState
|
||||
BrotliBitReaderSaveState(br, &memento)
|
||||
var memento bitReaderState
|
||||
bitReaderSaveState(br, &memento)
|
||||
if !SafeReadSymbol(type_tree, br, &block_type) {
|
||||
return false
|
||||
}
|
||||
if !SafeReadBlockLength(s, &s.block_length[tree_type], len_tree, br) {
|
||||
s.substate_read_block_length = BROTLI_STATE_READ_BLOCK_LENGTH_NONE
|
||||
BrotliBitReaderRestoreState(br, &memento)
|
||||
bitReaderRestoreState(br, &memento)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
@ -1430,7 +1430,7 @@ func CopyUncompressedBlockToOutput(available_out *uint, next_out *[]byte, total_
|
|||
switch s.substate_uncompressed {
|
||||
case BROTLI_STATE_UNCOMPRESSED_NONE:
|
||||
{
|
||||
var nbytes int = int(BrotliGetRemainingBytes(&s.br))
|
||||
var nbytes int = int(getRemainingBytes(&s.br))
|
||||
if nbytes > s.meta_block_remaining_len {
|
||||
nbytes = s.meta_block_remaining_len
|
||||
}
|
||||
|
@ -1440,7 +1440,7 @@ func CopyUncompressedBlockToOutput(available_out *uint, next_out *[]byte, total_
|
|||
}
|
||||
|
||||
/* Copy remaining bytes from s->br.buf_ to ring-buffer. */
|
||||
BrotliCopyBytes(s.ringbuffer[s.pos:], &s.br, uint(nbytes))
|
||||
copyBytes(s.ringbuffer[s.pos:], &s.br, uint(nbytes))
|
||||
|
||||
s.pos += nbytes
|
||||
s.meta_block_remaining_len -= nbytes
|
||||
|
@ -1535,12 +1535,12 @@ func BrotliCalculateRingBufferSize(s *Reader) {
|
|||
|
||||
/* Reads 1..256 2-bit context modes. */
|
||||
func ReadContextModes(s *Reader) int {
|
||||
var br *BrotliBitReader = &s.br
|
||||
var br *bitReader = &s.br
|
||||
var i int = s.loop_counter
|
||||
|
||||
for i < int(s.num_block_types[0]) {
|
||||
var bits uint32
|
||||
if !BrotliSafeReadBits(br, 2, &bits) {
|
||||
if !safeReadBits(br, 2, &bits) {
|
||||
s.loop_counter = i
|
||||
return BROTLI_DECODER_NEEDS_MORE_INPUT
|
||||
}
|
||||
|
@ -1585,9 +1585,9 @@ func TakeDistanceFromRingBuffer(s *Reader) {
|
|||
}
|
||||
}
|
||||
|
||||
func SafeReadBits(br *BrotliBitReader, n_bits uint32, val *uint32) bool {
|
||||
func SafeReadBits(br *bitReader, n_bits uint32, val *uint32) bool {
|
||||
if n_bits != 0 {
|
||||
return BrotliSafeReadBits(br, n_bits, val)
|
||||
return safeReadBits(br, n_bits, val)
|
||||
} else {
|
||||
*val = 0
|
||||
return true
|
||||
|
@ -1595,15 +1595,15 @@ func SafeReadBits(br *BrotliBitReader, n_bits uint32, val *uint32) bool {
|
|||
}
|
||||
|
||||
/* Precondition: s->distance_code < 0. */
|
||||
func ReadDistanceInternal(safe int, s *Reader, br *BrotliBitReader) bool {
|
||||
func ReadDistanceInternal(safe int, s *Reader, br *bitReader) bool {
|
||||
var distval int
|
||||
var memento BrotliBitReaderState
|
||||
var memento bitReaderState
|
||||
var distance_tree []HuffmanCode = []HuffmanCode(s.distance_hgroup.htrees[s.dist_htree_index])
|
||||
if safe == 0 {
|
||||
s.distance_code = int(ReadSymbol(distance_tree, br))
|
||||
} else {
|
||||
var code uint32
|
||||
BrotliBitReaderSaveState(br, &memento)
|
||||
bitReaderSaveState(br, &memento)
|
||||
if !SafeReadSymbol(distance_tree, br, &code) {
|
||||
return false
|
||||
}
|
||||
|
@ -1629,7 +1629,7 @@ func ReadDistanceInternal(safe int, s *Reader, br *BrotliBitReader) bool {
|
|||
if safe == 0 && (s.distance_postfix_bits == 0) {
|
||||
nbits = (uint32(distval) >> 1) + 1
|
||||
offset = ((2 + (distval & 1)) << nbits) - 4
|
||||
s.distance_code = int(s.num_direct_distance_codes) + offset + int(BrotliReadBits(br, nbits))
|
||||
s.distance_code = int(s.num_direct_distance_codes) + offset + int(readBits(br, nbits))
|
||||
} else {
|
||||
/* This branch also works well when s->distance_postfix_bits == 0. */
|
||||
var bits uint32
|
||||
|
@ -1639,11 +1639,11 @@ func ReadDistanceInternal(safe int, s *Reader, br *BrotliBitReader) bool {
|
|||
if safe != 0 {
|
||||
if !SafeReadBits(br, nbits, &bits) {
|
||||
s.distance_code = -1 /* Restore precondition. */
|
||||
BrotliBitReaderRestoreState(br, &memento)
|
||||
bitReaderRestoreState(br, &memento)
|
||||
return false
|
||||
}
|
||||
} else {
|
||||
bits = BrotliReadBits(br, nbits)
|
||||
bits = readBits(br, nbits)
|
||||
}
|
||||
|
||||
offset = ((2 + (distval & 1)) << nbits) - 4
|
||||
|
@ -1656,24 +1656,24 @@ func ReadDistanceInternal(safe int, s *Reader, br *BrotliBitReader) bool {
|
|||
return true
|
||||
}
|
||||
|
||||
func ReadDistance(s *Reader, br *BrotliBitReader) {
|
||||
func ReadDistance(s *Reader, br *bitReader) {
|
||||
ReadDistanceInternal(0, s, br)
|
||||
}
|
||||
|
||||
func SafeReadDistance(s *Reader, br *BrotliBitReader) bool {
|
||||
func SafeReadDistance(s *Reader, br *bitReader) bool {
|
||||
return ReadDistanceInternal(1, s, br)
|
||||
}
|
||||
|
||||
func ReadCommandInternal(safe int, s *Reader, br *BrotliBitReader, insert_length *int) bool {
|
||||
func ReadCommandInternal(safe int, s *Reader, br *bitReader, insert_length *int) bool {
|
||||
var cmd_code uint32
|
||||
var insert_len_extra uint32 = 0
|
||||
var copy_length uint32
|
||||
var v CmdLutElement
|
||||
var memento BrotliBitReaderState
|
||||
var memento bitReaderState
|
||||
if safe == 0 {
|
||||
cmd_code = ReadSymbol(s.htree_command, br)
|
||||
} else {
|
||||
BrotliBitReaderSaveState(br, &memento)
|
||||
bitReaderSaveState(br, &memento)
|
||||
if !SafeReadSymbol(s.htree_command, br, &cmd_code) {
|
||||
return false
|
||||
}
|
||||
|
@ -1686,13 +1686,13 @@ func ReadCommandInternal(safe int, s *Reader, br *BrotliBitReader, insert_length
|
|||
*insert_length = int(v.insert_len_offset)
|
||||
if safe == 0 {
|
||||
if v.insert_len_extra_bits != 0 {
|
||||
insert_len_extra = BrotliReadBits(br, uint32(v.insert_len_extra_bits))
|
||||
insert_len_extra = readBits(br, uint32(v.insert_len_extra_bits))
|
||||
}
|
||||
|
||||
copy_length = BrotliReadBits(br, uint32(v.copy_len_extra_bits))
|
||||
copy_length = readBits(br, uint32(v.copy_len_extra_bits))
|
||||
} else {
|
||||
if !SafeReadBits(br, uint32(v.insert_len_extra_bits), &insert_len_extra) || !SafeReadBits(br, uint32(v.copy_len_extra_bits), ©_length) {
|
||||
BrotliBitReaderRestoreState(br, &memento)
|
||||
bitReaderRestoreState(br, &memento)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
@ -1703,27 +1703,27 @@ func ReadCommandInternal(safe int, s *Reader, br *BrotliBitReader, insert_length
|
|||
return true
|
||||
}
|
||||
|
||||
func ReadCommand(s *Reader, br *BrotliBitReader, insert_length *int) {
|
||||
func ReadCommand(s *Reader, br *bitReader, insert_length *int) {
|
||||
ReadCommandInternal(0, s, br, insert_length)
|
||||
}
|
||||
|
||||
func SafeReadCommand(s *Reader, br *BrotliBitReader, insert_length *int) bool {
|
||||
func SafeReadCommand(s *Reader, br *bitReader, insert_length *int) bool {
|
||||
return ReadCommandInternal(1, s, br, insert_length)
|
||||
}
|
||||
|
||||
func CheckInputAmount(safe int, br *BrotliBitReader, num uint) bool {
|
||||
func CheckInputAmount(safe int, br *bitReader, num uint) bool {
|
||||
if safe != 0 {
|
||||
return true
|
||||
}
|
||||
|
||||
return BrotliCheckInputAmount(br, num)
|
||||
return checkInputAmount(br, num)
|
||||
}
|
||||
|
||||
func ProcessCommandsInternal(safe int, s *Reader) int {
|
||||
var pos int = s.pos
|
||||
var i int = s.loop_counter
|
||||
var result int = BROTLI_DECODER_SUCCESS
|
||||
var br *BrotliBitReader = &s.br
|
||||
var br *bitReader = &s.br
|
||||
var hc []HuffmanCode
|
||||
|
||||
if !CheckInputAmount(safe, br, 28) {
|
||||
|
@ -1732,7 +1732,7 @@ func ProcessCommandsInternal(safe int, s *Reader) int {
|
|||
}
|
||||
|
||||
if safe == 0 {
|
||||
BrotliWarmupBitReader(br)
|
||||
warmupBitReader(br)
|
||||
}
|
||||
|
||||
/* Jump into state machine. */
|
||||
|
@ -1970,7 +1970,7 @@ CommandPostDecodeLiterals:
|
|||
var transforms *BrotliTransforms = s.transforms
|
||||
var offset int = int(s.dictionary.offsets_by_length[i])
|
||||
var shift uint32 = uint32(s.dictionary.size_bits_by_length[i])
|
||||
var mask int = int(BitMask(shift))
|
||||
var mask int = int(bitMask(shift))
|
||||
var word_idx int = address & mask
|
||||
var transform_idx int = address >> shift
|
||||
|
||||
|
@ -2125,7 +2125,7 @@ func BrotliMaxDistanceSymbol(ndirect uint32, npostfix uint32) uint32 {
|
|||
buffer; this is possible because the invariant is held on enter */
|
||||
func BrotliDecoderDecompressStream(s *Reader, available_in *uint, next_in *[]byte, available_out *uint, next_out *[]byte) int {
|
||||
var result int = BROTLI_DECODER_SUCCESS
|
||||
var br *BrotliBitReader = &s.br
|
||||
var br *bitReader = &s.br
|
||||
|
||||
/* Do not try to process further in a case of unrecoverable error. */
|
||||
if int(s.error_code) < 0 {
|
||||
|
@ -2227,7 +2227,7 @@ func BrotliDecoderDecompressStream(s *Reader, available_in *uint, next_in *[]byt
|
|||
/* Using input stream in last iteration. When decoder switches to input
|
||||
stream it has less than 8 bits in accumulator, so it is safe to
|
||||
return unused accumulator bits there. */
|
||||
BrotliBitReaderUnload(br)
|
||||
bitReaderUnload(br)
|
||||
|
||||
*available_in = br.input_len - br.byte_pos
|
||||
*next_in = br.input[br.byte_pos:]
|
||||
|
@ -2239,7 +2239,7 @@ func BrotliDecoderDecompressStream(s *Reader, available_in *uint, next_in *[]byt
|
|||
switch s.state {
|
||||
/* Prepare to the first read. */
|
||||
case BROTLI_STATE_UNINITED:
|
||||
if !BrotliWarmupBitReader(br) {
|
||||
if !warmupBitReader(br) {
|
||||
result = BROTLI_DECODER_NEEDS_MORE_INPUT
|
||||
break
|
||||
}
|
||||
|
@ -2258,7 +2258,7 @@ func BrotliDecoderDecompressStream(s *Reader, available_in *uint, next_in *[]byt
|
|||
s.state = BROTLI_STATE_INITIALIZE
|
||||
|
||||
case BROTLI_STATE_LARGE_WINDOW_BITS:
|
||||
if !BrotliSafeReadBits(br, 6, &s.window_bits) {
|
||||
if !safeReadBits(br, 6, &s.window_bits) {
|
||||
result = BROTLI_DECODER_NEEDS_MORE_INPUT
|
||||
break
|
||||
}
|
||||
|
@ -2305,7 +2305,7 @@ func BrotliDecoderDecompressStream(s *Reader, available_in *uint, next_in *[]byt
|
|||
}
|
||||
|
||||
if s.is_metadata != 0 || s.is_uncompressed != 0 {
|
||||
if !BrotliJumpToByteBoundary(br) {
|
||||
if !jumpToByteBoundary(br) {
|
||||
result = BROTLI_DECODER_ERROR_FORMAT_PADDING_1
|
||||
break
|
||||
}
|
||||
|
@ -2346,7 +2346,7 @@ func BrotliDecoderDecompressStream(s *Reader, available_in *uint, next_in *[]byt
|
|||
var bits uint32
|
||||
|
||||
/* Read one byte and ignore it. */
|
||||
if !BrotliSafeReadBits(br, 8, &bits) {
|
||||
if !safeReadBits(br, 8, &bits) {
|
||||
result = BROTLI_DECODER_NEEDS_MORE_INPUT
|
||||
break
|
||||
}
|
||||
|
@ -2420,15 +2420,15 @@ func BrotliDecoderDecompressStream(s *Reader, available_in *uint, next_in *[]byt
|
|||
case BROTLI_STATE_METABLOCK_HEADER_2:
|
||||
{
|
||||
var bits uint32
|
||||
if !BrotliSafeReadBits(br, 6, &bits) {
|
||||
if !safeReadBits(br, 6, &bits) {
|
||||
result = BROTLI_DECODER_NEEDS_MORE_INPUT
|
||||
break
|
||||
}
|
||||
|
||||
s.distance_postfix_bits = bits & BitMask(2)
|
||||
s.distance_postfix_bits = bits & bitMask(2)
|
||||
bits >>= 2
|
||||
s.num_direct_distance_codes = BROTLI_NUM_DISTANCE_SHORT_CODES + (bits << s.distance_postfix_bits)
|
||||
s.distance_postfix_mask = int(BitMask(s.distance_postfix_bits))
|
||||
s.distance_postfix_mask = int(bitMask(s.distance_postfix_bits))
|
||||
s.context_modes = make([]byte, uint(s.num_block_types[0]))
|
||||
if s.context_modes == nil {
|
||||
result = BROTLI_DECODER_ERROR_ALLOC_CONTEXT_MODES
|
||||
|
@ -2607,13 +2607,13 @@ func BrotliDecoderDecompressStream(s *Reader, available_in *uint, next_in *[]byt
|
|||
break
|
||||
}
|
||||
|
||||
if !BrotliJumpToByteBoundary(br) {
|
||||
if !jumpToByteBoundary(br) {
|
||||
result = BROTLI_DECODER_ERROR_FORMAT_PADDING_2
|
||||
break
|
||||
}
|
||||
|
||||
if s.buffer_length == 0 {
|
||||
BrotliBitReaderUnload(br)
|
||||
bitReaderUnload(br)
|
||||
*available_in = br.input_len - br.byte_pos
|
||||
*next_in = br.input[br.byte_pos:]
|
||||
}
|
||||
|
@ -2681,7 +2681,7 @@ func BrotliDecoderTakeOutput(s *Reader, size *uint) []byte {
|
|||
}
|
||||
|
||||
func BrotliDecoderIsUsed(s *Reader) bool {
|
||||
return s.state != BROTLI_STATE_UNINITED || BrotliGetAvailableBits(&s.br) != 0
|
||||
return s.state != BROTLI_STATE_UNINITED || getAvailableBits(&s.br) != 0
|
||||
}
|
||||
|
||||
func BrotliDecoderIsFinished(s *Reader) bool {
|
||||
|
|
26
encode.go
26
encode.go
|
@ -710,11 +710,11 @@ func ChooseContextMap(quality int, bigram_histo []uint32, num_literal_contexts *
|
|||
two_prefix_histo[i%6] += bigram_histo[i]
|
||||
}
|
||||
|
||||
entropy[1] = ShannonEntropy(monogram_histo[:], 3, &dummy)
|
||||
entropy[2] = (ShannonEntropy(two_prefix_histo[:], 3, &dummy) + ShannonEntropy(two_prefix_histo[3:], 3, &dummy))
|
||||
entropy[1] = shannonEntropy(monogram_histo[:], 3, &dummy)
|
||||
entropy[2] = (shannonEntropy(two_prefix_histo[:], 3, &dummy) + shannonEntropy(two_prefix_histo[3:], 3, &dummy))
|
||||
entropy[3] = 0
|
||||
for i = 0; i < 3; i++ {
|
||||
entropy[3] += ShannonEntropy(bigram_histo[3*i:], 3, &dummy)
|
||||
entropy[3] += shannonEntropy(bigram_histo[3*i:], 3, &dummy)
|
||||
}
|
||||
|
||||
total = uint(monogram_histo[0] + monogram_histo[1] + monogram_histo[2])
|
||||
|
@ -848,10 +848,10 @@ func ShouldUseComplexStaticContextMap(input []byte, start_pos uint, length uint,
|
|||
}
|
||||
}
|
||||
|
||||
entropy[1] = ShannonEntropy(combined_histo[:], 32, &dummy)
|
||||
entropy[1] = shannonEntropy(combined_histo[:], 32, &dummy)
|
||||
entropy[2] = 0
|
||||
for i = 0; i < 13; i++ {
|
||||
entropy[2] += ShannonEntropy(context_histo[i][0:], 32, &dummy)
|
||||
entropy[2] += shannonEntropy(context_histo[i][0:], 32, &dummy)
|
||||
}
|
||||
|
||||
entropy[0] = 1.0 / float64(total)
|
||||
|
@ -922,7 +922,7 @@ func ShouldCompress_encode(data []byte, mask uint, last_flush_pos uint64, bytes
|
|||
pos += kSampleRate
|
||||
}
|
||||
|
||||
if BitsEntropy(literal_histo[:], 256) > bit_cost_threshold {
|
||||
if bitsEntropy(literal_histo[:], 256) > bit_cost_threshold {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
@ -1348,12 +1348,12 @@ func EncodeData(s *Writer, is_last bool, force_flush bool, out_size *uint, outpu
|
|||
|
||||
if s.params.quality == ZOPFLIFICATION_QUALITY {
|
||||
assert(s.params.hasher.type_ == 10)
|
||||
BrotliCreateZopfliBackwardReferences(uint(bytes), uint(wrapped_last_processed_pos), data, uint(mask), &s.params, s.hasher_.(*H10), s.dist_cache_[:], &s.last_insert_len_, s.commands_[s.num_commands_:], &s.num_commands_, &s.num_literals_)
|
||||
createZopfliBackwardReferences(uint(bytes), uint(wrapped_last_processed_pos), data, uint(mask), &s.params, s.hasher_.(*H10), s.dist_cache_[:], &s.last_insert_len_, s.commands_[s.num_commands_:], &s.num_commands_, &s.num_literals_)
|
||||
} else if s.params.quality == HQ_ZOPFLIFICATION_QUALITY {
|
||||
assert(s.params.hasher.type_ == 10)
|
||||
BrotliCreateHqZopfliBackwardReferences(uint(bytes), uint(wrapped_last_processed_pos), data, uint(mask), &s.params, s.hasher_, s.dist_cache_[:], &s.last_insert_len_, s.commands_[s.num_commands_:], &s.num_commands_, &s.num_literals_)
|
||||
createHqZopfliBackwardReferences(uint(bytes), uint(wrapped_last_processed_pos), data, uint(mask), &s.params, s.hasher_, s.dist_cache_[:], &s.last_insert_len_, s.commands_[s.num_commands_:], &s.num_commands_, &s.num_literals_)
|
||||
} else {
|
||||
BrotliCreateBackwardReferences(uint(bytes), uint(wrapped_last_processed_pos), data, uint(mask), &s.params, s.hasher_, s.dist_cache_[:], &s.last_insert_len_, s.commands_[s.num_commands_:], &s.num_commands_, &s.num_literals_)
|
||||
createBackwardReferences(uint(bytes), uint(wrapped_last_processed_pos), data, uint(mask), &s.params, s.hasher_, s.dist_cache_[:], &s.last_insert_len_, s.commands_[s.num_commands_:], &s.num_commands_, &s.num_literals_)
|
||||
}
|
||||
{
|
||||
var max_length uint = MaxMetablockSize(&s.params)
|
||||
|
@ -1517,12 +1517,12 @@ func BrotliCompressBufferQuality10(lgwin int, input_size uint, input_buffer []by
|
|||
var block_start uint
|
||||
for block_start = metablock_start; block_start < metablock_end; {
|
||||
var block_size uint = brotli_min_size_t(metablock_end-block_start, max_block_size)
|
||||
var nodes []ZopfliNode = make([]ZopfliNode, (block_size + 1))
|
||||
var nodes []zopfliNode = make([]zopfliNode, (block_size + 1))
|
||||
var path_size uint
|
||||
var new_cmd_alloc_size uint
|
||||
BrotliInitZopfliNodes(nodes, block_size+1)
|
||||
initZopfliNodes(nodes, block_size+1)
|
||||
hasher.StitchToPreviousBlock(block_size, block_start, input_buffer, mask)
|
||||
path_size = BrotliZopfliComputeShortestPath(block_size, block_start, input_buffer, mask, ¶ms, dist_cache[:], hasher.(*H10), nodes)
|
||||
path_size = zopfliComputeShortestPath(block_size, block_start, input_buffer, mask, ¶ms, dist_cache[:], hasher.(*H10), nodes)
|
||||
|
||||
/* We allocate a command buffer in the first iteration of this loop that
|
||||
will be likely big enough for the whole metablock, so that for most
|
||||
|
@ -1545,7 +1545,7 @@ func BrotliCompressBufferQuality10(lgwin int, input_size uint, input_buffer []by
|
|||
commands = new_commands
|
||||
}
|
||||
|
||||
BrotliZopfliCreateCommands(block_size, block_start, nodes[0:], dist_cache[:], &last_insert_len, ¶ms, commands[num_commands:], &num_literals)
|
||||
zopfliCreateCommands(block_size, block_start, nodes[0:], dist_cache[:], &last_insert_len, ¶ms, commands[num_commands:], &num_literals)
|
||||
num_commands += path_size
|
||||
block_start += block_size
|
||||
metablock_size += block_size
|
||||
|
|
|
@ -161,7 +161,7 @@ func ComputeDistanceCost(cmds []Command, num_commands uint, orig_params *BrotliD
|
|||
}
|
||||
}
|
||||
|
||||
*cost = BrotliPopulationCostDistance(&histo) + extra_bits
|
||||
*cost = populationCostDistance(&histo) + extra_bits
|
||||
return true
|
||||
}
|
||||
|
||||
|
@ -363,7 +363,7 @@ func ContextBlockSplitterFinishBlock(self *ContextBlockSplitter, is_final bool)
|
|||
split.types[0] = 0
|
||||
|
||||
for i = 0; i < num_contexts; i++ {
|
||||
last_entropy[i] = BitsEntropy(histograms[i].data_[:], self.alphabet_size_)
|
||||
last_entropy[i] = bitsEntropy(histograms[i].data_[:], self.alphabet_size_)
|
||||
last_entropy[num_contexts+i] = last_entropy[i]
|
||||
}
|
||||
|
||||
|
@ -389,13 +389,13 @@ func ContextBlockSplitterFinishBlock(self *ContextBlockSplitter, is_final bool)
|
|||
for i = 0; i < num_contexts; i++ {
|
||||
var curr_histo_ix uint = self.curr_histogram_ix_ + i
|
||||
var j uint
|
||||
entropy[i] = BitsEntropy(histograms[curr_histo_ix].data_[:], self.alphabet_size_)
|
||||
entropy[i] = bitsEntropy(histograms[curr_histo_ix].data_[:], self.alphabet_size_)
|
||||
for j = 0; j < 2; j++ {
|
||||
var jx uint = j*num_contexts + i
|
||||
var last_histogram_ix uint = self.last_histogram_ix_[j] + i
|
||||
combined_histo[jx] = histograms[curr_histo_ix]
|
||||
HistogramAddHistogramLiteral(&combined_histo[jx], &histograms[last_histogram_ix])
|
||||
combined_entropy[jx] = BitsEntropy(combined_histo[jx].data_[0:], self.alphabet_size_)
|
||||
combined_entropy[jx] = bitsEntropy(combined_histo[jx].data_[0:], self.alphabet_size_)
|
||||
diff[j] += combined_entropy[jx] - entropy[i] - last_entropy[jx]
|
||||
}
|
||||
}
|
||||
|
|
|
@ -70,7 +70,7 @@ func BlockSplitterFinishBlockCommand(self *BlockSplitterCommand, is_final bool)
|
|||
split.lengths[0] = uint32(self.block_size_)
|
||||
|
||||
split.types[0] = 0
|
||||
last_entropy[0] = BitsEntropy(histograms[0].data_[:], self.alphabet_size_)
|
||||
last_entropy[0] = bitsEntropy(histograms[0].data_[:], self.alphabet_size_)
|
||||
last_entropy[1] = last_entropy[0]
|
||||
self.num_blocks_++
|
||||
split.num_types++
|
||||
|
@ -80,7 +80,7 @@ func BlockSplitterFinishBlockCommand(self *BlockSplitterCommand, is_final bool)
|
|||
}
|
||||
self.block_size_ = 0
|
||||
} else if self.block_size_ > 0 {
|
||||
var entropy float64 = BitsEntropy(histograms[self.curr_histogram_ix_].data_[:], self.alphabet_size_)
|
||||
var entropy float64 = bitsEntropy(histograms[self.curr_histogram_ix_].data_[:], self.alphabet_size_)
|
||||
var combined_histo [2]HistogramCommand
|
||||
var combined_entropy [2]float64
|
||||
var diff [2]float64
|
||||
|
@ -89,7 +89,7 @@ func BlockSplitterFinishBlockCommand(self *BlockSplitterCommand, is_final bool)
|
|||
var last_histogram_ix uint = self.last_histogram_ix_[j]
|
||||
combined_histo[j] = histograms[self.curr_histogram_ix_]
|
||||
HistogramAddHistogramCommand(&combined_histo[j], &histograms[last_histogram_ix])
|
||||
combined_entropy[j] = BitsEntropy(combined_histo[j].data_[0:], self.alphabet_size_)
|
||||
combined_entropy[j] = bitsEntropy(combined_histo[j].data_[0:], self.alphabet_size_)
|
||||
diff[j] = combined_entropy[j] - entropy - last_entropy[j]
|
||||
}
|
||||
|
||||
|
|
|
@ -70,7 +70,7 @@ func BlockSplitterFinishBlockDistance(self *BlockSplitterDistance, is_final bool
|
|||
split.lengths[0] = uint32(self.block_size_)
|
||||
|
||||
split.types[0] = 0
|
||||
last_entropy[0] = BitsEntropy(histograms[0].data_[:], self.alphabet_size_)
|
||||
last_entropy[0] = bitsEntropy(histograms[0].data_[:], self.alphabet_size_)
|
||||
last_entropy[1] = last_entropy[0]
|
||||
self.num_blocks_++
|
||||
split.num_types++
|
||||
|
@ -80,7 +80,7 @@ func BlockSplitterFinishBlockDistance(self *BlockSplitterDistance, is_final bool
|
|||
}
|
||||
self.block_size_ = 0
|
||||
} else if self.block_size_ > 0 {
|
||||
var entropy float64 = BitsEntropy(histograms[self.curr_histogram_ix_].data_[:], self.alphabet_size_)
|
||||
var entropy float64 = bitsEntropy(histograms[self.curr_histogram_ix_].data_[:], self.alphabet_size_)
|
||||
var combined_histo [2]HistogramDistance
|
||||
var combined_entropy [2]float64
|
||||
var diff [2]float64
|
||||
|
@ -89,7 +89,7 @@ func BlockSplitterFinishBlockDistance(self *BlockSplitterDistance, is_final bool
|
|||
var last_histogram_ix uint = self.last_histogram_ix_[j]
|
||||
combined_histo[j] = histograms[self.curr_histogram_ix_]
|
||||
HistogramAddHistogramDistance(&combined_histo[j], &histograms[last_histogram_ix])
|
||||
combined_entropy[j] = BitsEntropy(combined_histo[j].data_[0:], self.alphabet_size_)
|
||||
combined_entropy[j] = bitsEntropy(combined_histo[j].data_[0:], self.alphabet_size_)
|
||||
diff[j] = combined_entropy[j] - entropy - last_entropy[j]
|
||||
}
|
||||
|
||||
|
|
|
@ -70,7 +70,7 @@ func BlockSplitterFinishBlockLiteral(self *BlockSplitterLiteral, is_final bool)
|
|||
split.lengths[0] = uint32(self.block_size_)
|
||||
|
||||
split.types[0] = 0
|
||||
last_entropy[0] = BitsEntropy(histograms[0].data_[:], self.alphabet_size_)
|
||||
last_entropy[0] = bitsEntropy(histograms[0].data_[:], self.alphabet_size_)
|
||||
last_entropy[1] = last_entropy[0]
|
||||
self.num_blocks_++
|
||||
split.num_types++
|
||||
|
@ -80,7 +80,7 @@ func BlockSplitterFinishBlockLiteral(self *BlockSplitterLiteral, is_final bool)
|
|||
}
|
||||
self.block_size_ = 0
|
||||
} else if self.block_size_ > 0 {
|
||||
var entropy float64 = BitsEntropy(histograms[self.curr_histogram_ix_].data_[:], self.alphabet_size_)
|
||||
var entropy float64 = bitsEntropy(histograms[self.curr_histogram_ix_].data_[:], self.alphabet_size_)
|
||||
var combined_histo [2]HistogramLiteral
|
||||
var combined_entropy [2]float64
|
||||
var diff [2]float64
|
||||
|
@ -89,7 +89,7 @@ func BlockSplitterFinishBlockLiteral(self *BlockSplitterLiteral, is_final bool)
|
|||
var last_histogram_ix uint = self.last_histogram_ix_[j]
|
||||
combined_histo[j] = histograms[self.curr_histogram_ix_]
|
||||
HistogramAddHistogramLiteral(&combined_histo[j], &histograms[last_histogram_ix])
|
||||
combined_entropy[j] = BitsEntropy(combined_histo[j].data_[0:], self.alphabet_size_)
|
||||
combined_entropy[j] = bitsEntropy(combined_histo[j].data_[0:], self.alphabet_size_)
|
||||
diff[j] = combined_entropy[j] - entropy - last_entropy[j]
|
||||
}
|
||||
|
||||
|
|
4
state.go
4
state.go
|
@ -98,7 +98,7 @@ type Reader struct {
|
|||
|
||||
state int
|
||||
loop_counter int
|
||||
br BrotliBitReader
|
||||
br bitReader
|
||||
buffer struct {
|
||||
u64 uint64
|
||||
u8 [8]byte
|
||||
|
@ -186,7 +186,7 @@ type Reader struct {
|
|||
func BrotliDecoderStateInit(s *Reader) bool {
|
||||
s.error_code = 0 /* BROTLI_DECODER_NO_ERROR */
|
||||
|
||||
BrotliInitBitReader(&s.br)
|
||||
initBitReader(&s.br)
|
||||
s.state = BROTLI_STATE_UNINITED
|
||||
s.large_window = false
|
||||
s.substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NONE
|
||||
|
|
Loading…
Reference in New Issue