bits

package
v0.7.3 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2026 License: LGPL-2.1 Imports: 1 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 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

View Source
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 NewReader

func NewReader(buf []byte) *Reader

NewReader returns a Reader over buf. The caller must not mutate buf while reading.

func (*Reader) Align

func (r *Reader) Align()

Align advances to the next byte boundary. Mirrors align_get_bits.

func (*Reader) Err

func (r *Reader) Err() error

Err returns ErrOverread once any read or skip has consumed bits past the end of the buffer, nil otherwise.

func (*Reader) Left

func (r *Reader) Left() int

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

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

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) Pos

func (r *Reader) Pos() int

Pos returns the number of bits consumed. Mirrors get_bits_count.

func (*Reader) Read

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

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).

func (*Reader) ReadBit

func (r *Reader) ReadBit() uint32

ReadBit consumes and returns one bit. Mirrors get_bits1.

func (*Reader) Reset

func (r *Reader) Reset(buf []byte)

Reset re-arms the Reader over buf, clearing position and error state.

func (*Reader) Skip

func (r *Reader) Skip(n int)

Skip consumes n bits (n >= 0). Consuming past the end clamps at the end and latches ErrOverread. Mirrors skip_bits / skip_bits_long.

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 NewWriter

func NewWriter(buf []byte) *Writer

NewWriter returns a Writer that appends to buf, which may be nil.

func (*Writer) Count

func (w *Writer) Count() int

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

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

Flush zero-pads to a byte boundary and returns the written bytes. Mirrors flush_put_bits.

func (*Writer) Put

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

Put writes the low n bits of v, MSB first. n must be in 0..32.

func (*Writer) Reset

func (w *Writer) Reset(buf []byte)

Reset re-arms the writer over buf (which may be nil), discarding state.

Jump to

Keyboard shortcuts

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