Documentation
¶
Overview ¶
Package bits provides an MSB-first bit reader matching the bit-position and overrun semantics of minimp3's bs_t/get_bits (see tools/oracle/minimp3.h, CC0-1.0), which every decode unit in this package reads its bitstream through.
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 is an MSB-first bit reader. Position and limit are tracked in bits, not bytes, mirroring upstream bs_t.
func NewReader ¶
NewReader creates a Reader over buf with the limit set to the full buffer length in bits (8*len(buf)), mirroring upstream bs_init.
func NewReaderBits ¶
NewReaderBits creates a Reader over buf starting at posBits with an explicit bit limit, for callers that seek into a shared buffer (e.g. the bit reservoir) rather than starting at byte 0.
func (*Reader) Bits ¶
Bits reads the next n bits (n in [0,24]) MSB-first and advances the position, porting upstream get_bits exactly: the position always advances by n first, and a read that pushes it past the limit returns 0 and latches Overrun. Since pos never moves backward on its own, every read after that also lands past the limit and returns 0 too.
type Writer ¶
type Writer struct {
// contains filtered or unexported fields
}
Writer is an MSB-first bit writer, the mirror of Reader: bits written by Writer read back identically through Reader. It appends to a caller-owned byte slice, accumulating pending bits MSB-aligned and draining full bytes as they complete, the same bit convention Reader.Bits consumes.
func NewWriter ¶
NewWriter creates a Writer that appends to buf, starting at len(buf). Any existing bytes in buf are preserved as a prefix.
func (*Writer) BitsWritten ¶
BitsWritten returns the total number of bits written since NewWriter.
func (*Writer) Flush ¶
Flush zero-pads any pending partial byte and returns the extended buf. Frame stuffing relies on the padding bits being zero. After Flush, the Writer is spent; create a new one per frame.
func (*Writer) WriteBits ¶
WriteBits writes the low n bits of v, MSB-first, masking v to its low n bits (callers may pass wider values; the mask is part of the contract, matching how side-info fields are packed). n must be in [0, 32]; there are no error paths for valid input, but WriteBits panics if n is outside [0, 32]. A negative n would convert to a huge uint in w.nacc += uint(n), and the byte-drain loop would then append essentially without bound (a hang or an out-of-memory condition, not a clean error), so a caller bug here must fail fast instead.