Documentation
¶
Overview ¶
Package bits implements an MSB-first bit writer with the semantics of FFmpeg's PutBitContext (libavcodec/put_bits.h @ d09d5afc3a).
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrOverread = errors.New("bits: read past end of input")
ErrOverread reports that a Reader consumed bits past the end of its buffer. Mirrors the condition get_bits_left(gb) < 0 of FFmpeg's safe bitstream reader (libavcodec/get_bits.h @ d09d5afc3a, with CONFIG_SAFE_BITSTREAM_READER, the default), which clamps the position and returns zero bits; the Reader does the same but also latches this sticky error so a parser cannot mistake padding zeros for payload.
Functions ¶
This section is empty.
Types ¶
type Reader ¶
type Reader struct {
// contains filtered or unexported fields
}
Reader reads bit fields MSB-first from a byte slice, mirroring the semantics of FFmpeg's GetBitContext (libavcodec/get_bits.h @ d09d5afc3a). Peeks past the end return zero bits without error, exactly like the C reader running inside its zero-initialized padding; consuming past the end clamps the position at the end and latches ErrOverread. Callers check Err at the same points the C checks get_bits_left, and once more when a frame is complete.
func (*Reader) Align ¶
func (r *Reader) Align()
Align advances to the next byte boundary. Mirrors align_get_bits.
func (*Reader) Err ¶
Err returns ErrOverread once any read or skip has consumed bits past the end of the buffer, nil otherwise.
func (*Reader) Left ¶
Left returns the number of bits remaining. It never goes negative; the overread condition C code detects via get_bits_left(gb) < 0 is reported by Err instead.
func (*Reader) Peek ¶
Peek returns the next n bits (0 <= n <= 32) without consuming them, zero-padded past the end of the buffer. Mirrors show_bits.
func (*Reader) Read ¶
Read consumes and returns the next n bits (0 <= n <= 32). Past the end it returns zero bits and latches ErrOverread. Mirrors get_bits (and get_bits_long: n = 0 is legal and returns 0).
type Writer ¶
type Writer struct {
// contains filtered or unexported fields
}
Writer writes bit fields MSB-first into a byte slice. It appends, growing the slice if needed; callers that preallocate capacity (the encoder bounds every AAC frame at 6144 bits per channel) get zero allocations.
func (*Writer) Count ¶
Count returns the number of bits written so far, including unflushed bits and, once Flush has run, the zero padding it added. Mirrors put_bits_count (libavcodec/put_bits.h @ d09d5afc3a), which computes (buf_ptr - buf) * 8 + BUF_BITS - bit_left: after a flush the write pointer has advanced past the padded byte, so the padding is counted. The encoder relies on this: aacenc.c:1392 reads put_bits_count immediately after flush_put_bits to feed the bit reservoir and the NMR side-bits EMA.
func (*Writer) Flush ¶
Flush zero-pads to a byte boundary and returns the written bytes. Mirrors flush_put_bits.