bits

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2026 License: MIT Imports: 0 Imported by: 0 modules, 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 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

func NewReader(buf []byte) Reader

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

func NewReaderBits(buf []byte, posBits, limitBits int) Reader

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

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

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.

func (*Reader) Overrun

func (r *Reader) Overrun() bool

Overrun reports whether any read has ever advanced the position past the limit.

func (*Reader) Pos

func (r *Reader) Pos() int

Pos returns the current bit position.

func (*Reader) SetPos

func (r *Reader) SetPos(bits int)

SetPos sets the bit position directly, e.g. to rewind or seek within the buffer.

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

func NewWriter(buf []byte) Writer

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

func (w *Writer) BitsWritten() int

BitsWritten returns the total number of bits written since NewWriter.

func (*Writer) Flush

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

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

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

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.

Jump to

Keyboard shortcuts

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