boolcoder

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 26, 2026 License: BSD-2-Clause Imports: 0 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package boolcoder implements the VP9 boolean arithmetic coder — the entropy layer underneath every VP9 syntax element. This is the decoder (Reader) plus a matching encoder (Writer, see encoder.go) so the two can be roundtrip-tested bit-exactly without an external reference.

Probabilities are 8-bit: prob = P(bit == 0) scaled to 256 (so 128 ≈ 50/50). Original implementation of the standard range/probability arithmetic decoding described in the public VP9 specification (§9.2).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Reader

type Reader struct {
	// contains filtered or unexported fields
}

Reader decodes booleans from a VP9-coded byte slice.

func NewReader

func NewReader(buf []byte) *Reader

NewReader initialises a boolean decoder over buf.

func NewReaderVP9 added in v0.2.0

func NewReaderVP9(buf []byte) *Reader

NewReaderVP9 initialises a boolean decoder over a real VP9 bitstream partition (the compressed header or a tile). VP9's init_bool consumes one leading marker bit after loading the value window (libvpx vpx_reader_init ends with vpx_read_bit) — this constructor reads and discards it so subsequent reads align with the stream. Use NewReader (no marker) for the encoder roundtrip.

func (*Reader) BitPos added in v0.2.0

func (r *Reader) BitPos() int

BitPos returns the number of bits consumed from the buffer so far.

func (*Reader) Overread added in v0.2.0

func (r *Reader) Overread() bool

Overread reports whether the decoder has consumed more bits than the buffer contained — i.e. the bool window has drained past the end of the partition. In a valid stream the trailing padding keeps bitsAvl >= 0 through the final symbol, so a negative value means an entropy desync (or a truncated tile). Callers use this after a tile/partition to reject a corrupt frame instead of reconstructing garbage pixels.

func (*Reader) ReadBit

func (r *Reader) ReadBit() int

ReadBit decodes one equiprobable bit (prob 128).

func (*Reader) ReadBool

func (r *Reader) ReadBool(prob uint8) int

ReadBool decodes one boolean with probability prob (1..255 = P(0)*256).

func (*Reader) ReadBoolT added in v0.2.0

func (r *Reader) ReadBoolT(prob uint8, label string) int

ReadBoolT is ReadBool with mandatory tracing (for explicit call sites).

func (*Reader) ReadLiteral

func (r *Reader) ReadLiteral(n int) uint32

ReadLiteral decodes an n-bit unsigned value MSB-first (equiprobable bits).

func (*Reader) ReadSigned

func (r *Reader) ReadSigned(n int) int32

ReadSigned decodes an n-bit magnitude followed by a sign bit (1 = negative).

func (*Reader) ReadTree

func (r *Reader) ReadTree(tree []int8, probs []uint8) int

ReadTree walks a VP9 token tree: tree is the flattened left/right index array, probs are the per-node probabilities. Returns the decoded (negated-leaf) token.

func (*Reader) ReadTreeCount

func (r *Reader) ReadTreeCount(tree []int8, probs []uint8, counts [][2]uint32) int

ReadTreeCount is ReadTree with per-node binary count accumulation. counts[nodeIdx][bit]++ for each tree node visited. Pass nil counts to skip counting (identical to ReadTree).

func (*Reader) SetLabel added in v0.2.0

func (r *Reader) SetLabel(s string)

traceLabel is the next-op label (set by SetLabel, consumed on ReadBool).

func (*Reader) State added in v0.2.0

func (r *Reader) State() (rng uint32, topByte uint8)

State returns the current range and top byte of value for debugging.

func (*Reader) TraceN added in v0.2.0

func (r *Reader) TraceN(n int, fn TraceFunc)

TraceN sets the number of subsequent ReadBool calls to trace via fn. Set fn=nil to stop tracing.

type TraceFunc added in v0.2.0

type TraceFunc func(label string, prob uint8, rng uint32, top uint8, split uint32, bit int)

TraceFunc is called for each ReadBool when tracing is active. label: caller-supplied context; prob: boolean prob; rng: range before read; top: value[63:56] before read; split: threshold; bit: decoded bit (0 or 1).

type Writer

type Writer struct {
	// contains filtered or unexported fields
}

Writer is a VP9 boolean arithmetic encoder. It exists so the decoder can be roundtrip-tested bit-exactly (encode a sequence, decode it back, compare) with no external dependency. It implements the same split/renormalise arithmetic as the decoder, with standard carry-propagating byte emission.

func NewWriter

func NewWriter() *Writer

NewWriter returns an empty encoder.

func (*Writer) Bytes

func (w *Writer) Bytes() []byte

Bytes flushes the coder and returns the encoded buffer.

func (*Writer) WriteBit

func (w *Writer) WriteBit(bit int)

WriteBit encodes one equiprobable bit.

func (*Writer) WriteBool

func (w *Writer) WriteBool(prob uint8, bit int)

WriteBool encodes one boolean with probability prob (1..255 = P(0)*256).

func (*Writer) WriteLiteral

func (w *Writer) WriteLiteral(v uint32, n int)

WriteLiteral encodes an n-bit value MSB-first (equiprobable bits).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL