Documentation
¶
Overview ¶
Package bits provides bit and bytes reading and writing including Golomb codes and EBSP as used by MPEG video standards. All readers and writers accumulate errors in the sense that they will stop reading or writing at the first error. The first error, if any, can be retrieved with an AccError() method.
EBSP (Encapsulated Byte Sequence Packets) uses insertion of start-code emulation prevention bytes 0x03 and is used in MPEG video standards from AVC (H.264) and forward. The main types are:
- Reader reads bits and bytes from an underlying io.Reader with accumulated error
- Writer writes bits and bytes to an underlying io.Writer with accumulated error
- EBSPReader reads EBSP from an underlying io.Reader with accumulated error
- EBSPWriter writes EBSP to an underlying io.Writer with accumulated error
- ByteWriter writes byte-based structures to an underlying io.Writer with accumulated error
- FixedSliceReader reads various byte-based structures from a fixed slice with accumulated error
- FixedSliceWriter writes various byte-based structures to a fixed slice with accumulated error
Index ¶
- Variables
- func CeilLog2(n uint) int
- func Mask(n int) uint
- type ByteWriter
- type EBSPReader
- func (r *EBSPReader) AccError() error
- func (r *EBSPReader) IsSeeker() bool
- func (r *EBSPReader) MoreRbspData() (bool, error)
- func (r *EBSPReader) NrBitsRead() int
- func (r *EBSPReader) NrBitsReadInCurrentByte() int
- func (r *EBSPReader) NrBytesRead() int
- func (r *EBSPReader) Read(n int) uint
- func (r *EBSPReader) ReadBytes(n int) []byte
- func (r *EBSPReader) ReadExpGolomb() uint
- func (r *EBSPReader) ReadFlag() bool
- func (r *EBSPReader) ReadRbspTrailingBits() error
- func (r *EBSPReader) ReadSignedGolomb() int
- func (r *EBSPReader) SetError(err error)
- type EBSPWriter
- func (w *EBSPWriter) AccError() error
- func (w *EBSPWriter) BitsInBuffer() (bits, n uint)
- func (w *EBSPWriter) NrBitsInBuffer() uint
- func (w *EBSPWriter) StuffByteWithZeros()
- func (w *EBSPWriter) Write(bits uint, n int)
- func (w *EBSPWriter) WriteExpGolomb(nr uint)
- func (w *EBSPWriter) WriteRbspTrailingBits()
- func (w *EBSPWriter) WriteSEIValue(val uint)
- type FixedSliceReader
- func (s *FixedSliceReader) AccError() error
- func (s *FixedSliceReader) GetPos() int
- func (s *FixedSliceReader) Length() int
- func (s *FixedSliceReader) LookAhead(offset int, data []byte) error
- func (s *FixedSliceReader) NrRemainingBytes() int
- func (s *FixedSliceReader) ReadBytes(n int) []byte
- func (s *FixedSliceReader) ReadFixedLengthString(n int) string
- func (s *FixedSliceReader) ReadInt16() int16
- func (s *FixedSliceReader) ReadInt32() int32
- func (s *FixedSliceReader) ReadInt64() int64
- func (s *FixedSliceReader) ReadPossiblyZeroTerminatedString(maxLen int) (str string, ok bool)
- func (s *FixedSliceReader) ReadUint16() uint16
- func (s *FixedSliceReader) ReadUint24() uint32
- func (s *FixedSliceReader) ReadUint32() uint32
- func (s *FixedSliceReader) ReadUint64() uint64
- func (s *FixedSliceReader) ReadUint8() byte
- func (s *FixedSliceReader) ReadZeroTerminatedString(maxLen int) string
- func (s *FixedSliceReader) RemainingBytes() []byte
- func (s *FixedSliceReader) SetPos(pos int)
- func (s *FixedSliceReader) SkipBytes(n int)
- type FixedSliceWriter
- func (sw *FixedSliceWriter) AccError() error
- func (sw *FixedSliceWriter) Bytes() []byte
- func (sw *FixedSliceWriter) Capacity() int
- func (sw *FixedSliceWriter) FlushBits()
- func (sw *FixedSliceWriter) Len() int
- func (sw *FixedSliceWriter) Offset() int
- func (sw *FixedSliceWriter) WriteBits(bits uint, n int)
- func (sw *FixedSliceWriter) WriteBytes(byteSlice []byte)
- func (sw *FixedSliceWriter) WriteFlag(f bool)
- func (sw *FixedSliceWriter) WriteInt16(n int16)
- func (sw *FixedSliceWriter) WriteInt32(n int32)
- func (sw *FixedSliceWriter) WriteInt64(n int64)
- func (sw *FixedSliceWriter) WriteString(s string, addZeroEnd bool)
- func (sw *FixedSliceWriter) WriteUint16(n uint16)
- func (sw *FixedSliceWriter) WriteUint24(n uint32)
- func (sw *FixedSliceWriter) WriteUint32(n uint32)
- func (sw *FixedSliceWriter) WriteUint48(u uint64)
- func (sw *FixedSliceWriter) WriteUint64(n uint64)
- func (sw *FixedSliceWriter) WriteUint8(n byte)
- func (sw *FixedSliceWriter) WriteUnityMatrix()
- func (sw *FixedSliceWriter) WriteZeroBytes(n int)
- type Reader
- func (r *Reader) AccError() error
- func (r *Reader) NrBitsRead() int
- func (r *Reader) NrBitsReadInCurrentByte() int
- func (r *Reader) NrBytesRead() int
- func (r *Reader) Read(n int) uint
- func (r *Reader) ReadFlag() bool
- func (r *Reader) ReadRemainingBytes() []byte
- func (r *Reader) ReadSigned(n int) int
- type SliceReader
- type SliceWriter
- type Writer
Constants ¶
This section is empty.
Variables ¶
var (
ErrNotReadSeeker = errors.New("reader does not support Seek")
)
ESBPReader errors
var (
ErrSliceRead = fmt.Errorf("read too far in SliceReader")
)
SliceReader errors
var ErrSliceWrite = errors.New("overflow in SliceWriter")
Functions ¶
Types ¶
type ByteWriter ¶ added in v0.43.0
type ByteWriter struct {
// contains filtered or unexported fields
}
ByteWriter - writer that wraps an io.Writer and accumulates error. Only the first error is saved, but any later calls will not panic.
func NewByteWriter ¶ added in v0.43.0
func NewByteWriter(w io.Writer) *ByteWriter
NewByteWriter creates accumulated error writer around io.Writer.
func (*ByteWriter) AccError ¶ added in v0.43.0
func (a *ByteWriter) AccError() error
AccError - return accumulated error
func (*ByteWriter) WriteSlice ¶ added in v0.43.0
func (a *ByteWriter) WriteSlice(s []byte)
WriteSlice - write a slice
func (*ByteWriter) WriteUint16 ¶ added in v0.43.0
func (a *ByteWriter) WriteUint16(u uint16)
WriteUint16 - write uint16
func (*ByteWriter) WriteUint32 ¶ added in v0.43.0
func (a *ByteWriter) WriteUint32(u uint32)
WriteUint32 - write uint32
func (*ByteWriter) WriteUint48 ¶ added in v0.43.0
func (a *ByteWriter) WriteUint48(u uint64)
WriteUint48 - write uint48
func (*ByteWriter) WriteUint64 ¶ added in v0.43.0
func (a *ByteWriter) WriteUint64(u uint64)
WriteUint64 - write uint64
func (*ByteWriter) WriteUint8 ¶ added in v0.43.0
func (a *ByteWriter) WriteUint8(b byte)
WriteUint8 - write a byte
type EBSPReader ¶
type EBSPReader struct {
// contains filtered or unexported fields
}
EBSPReader reads an EBSP bitstream dropping start-code emulation bytes. It also supports checking for more rbsp data and reading rbsp_trailing_bits.
func NewEBSPReader ¶
func NewEBSPReader(rd io.Reader) *EBSPReader
NewEBSPReader return a new EBSP reader stopping reading at first error.
func (*EBSPReader) AccError ¶ added in v0.43.0
func (r *EBSPReader) AccError() error
AccError returns the accumulated error. If no error, returns nil.
func (*EBSPReader) IsSeeker ¶
func (r *EBSPReader) IsSeeker() bool
IsSeeker returns tru if underluing reader supports Seek interface.
func (*EBSPReader) MoreRbspData ¶
func (r *EBSPReader) MoreRbspData() (bool, error)
MoreRbspData returns false if next bit is 1 and last 1-bit in fullSlice. Underlying reader must support ReadSeeker interface to reset after check. Return false, nil if underlying error.
func (*EBSPReader) NrBitsRead ¶ added in v0.43.0
func (r *EBSPReader) NrBitsRead() int
NrBitsRead returns total number of bits read into parser.
func (*EBSPReader) NrBitsReadInCurrentByte ¶
func (r *EBSPReader) NrBitsReadInCurrentByte() int
NrBitsReadInCurrentByte returns number of bits read in current byte.
func (*EBSPReader) NrBytesRead ¶
func (r *EBSPReader) NrBytesRead() int
NrBytesRead returns how many bytes read into parser.
func (*EBSPReader) Read ¶
func (r *EBSPReader) Read(n int) uint
Read reads n bits and respects and accumulates errors. If error, returns 0.
func (*EBSPReader) ReadBytes ¶ added in v0.43.0
func (r *EBSPReader) ReadBytes(n int) []byte
ReadBytes read n bytes and return nil if new or accumulated error.
func (*EBSPReader) ReadExpGolomb ¶
func (r *EBSPReader) ReadExpGolomb() uint
ReadExpGolomb reads one unsigned exponential Golomb code.
func (*EBSPReader) ReadFlag ¶
func (r *EBSPReader) ReadFlag() bool
ReadFlag reads 1 bit and translates a bool.
func (*EBSPReader) ReadRbspTrailingBits ¶
func (r *EBSPReader) ReadRbspTrailingBits() error
ReadRbspTrailingBits reads rbsp_traling_bits. Returns error if wrong pattern. If other error, returns nil and let AccError() provide that error.
func (*EBSPReader) ReadSignedGolomb ¶
func (r *EBSPReader) ReadSignedGolomb() int
ReadSignedGolomb reads one signed exponential Golomb code.
func (*EBSPReader) SetError ¶ added in v0.43.0
func (r *EBSPReader) SetError(err error)
SetError sets an error if not already set.
type EBSPWriter ¶
type EBSPWriter struct {
// contains filtered or unexported fields
}
EBSPWriter write bits and insert start-code emulation prevention bytes as necessary. Ceases writing at first error. The first error can later be checked with AccError().
func NewEBSPWriter ¶
func NewEBSPWriter(w io.Writer) *EBSPWriter
NewEBSPWriter - returns a new Writer
func (*EBSPWriter) AccError ¶
func (w *EBSPWriter) AccError() error
AccError - return accumulated error
func (*EBSPWriter) BitsInBuffer ¶
func (w *EBSPWriter) BitsInBuffer() (bits, n uint)
BitsInBuffer - n bits written in buffer byte, not written to underlying writer
func (*EBSPWriter) NrBitsInBuffer ¶
func (w *EBSPWriter) NrBitsInBuffer() uint
NrBitsInBuffer - number bits written in buffer byte
func (*EBSPWriter) StuffByteWithZeros ¶
func (w *EBSPWriter) StuffByteWithZeros()
StuffByteWithZeros - write zero bits until byte boundary (0-7bits)
func (*EBSPWriter) Write ¶
func (w *EBSPWriter) Write(bits uint, n int)
Write - write n bits from bits and save error state
func (*EBSPWriter) WriteExpGolomb ¶
func (w *EBSPWriter) WriteExpGolomb(nr uint)
WriteExpGolomb - write an exponential Golomb code
func (*EBSPWriter) WriteRbspTrailingBits ¶
func (w *EBSPWriter) WriteRbspTrailingBits()
WriteRbspTrailingBits - write rbsp trailing bits (a 1 followed by zeros to a byte boundary)
func (*EBSPWriter) WriteSEIValue ¶
func (w *EBSPWriter) WriteSEIValue(val uint)
WriteSEIValue insert 0xFF until value is less than 255. Used in SEI payload type and size.
type FixedSliceReader ¶
type FixedSliceReader struct {
// contains filtered or unexported fields
}
FixedSliceReader - read integers and other data from a fixed slice. Accumulates error, and the first error can be retrieved. If err != nil, 0 or empty string is returned
func NewFixedSliceReader ¶
func NewFixedSliceReader(data []byte) *FixedSliceReader
bits.NewFixedSliceReader creates a new slice reader reading from data
func (*FixedSliceReader) AccError ¶
func (s *FixedSliceReader) AccError() error
AccError - get accumulated error after read operations
func (*FixedSliceReader) GetPos ¶
func (s *FixedSliceReader) GetPos() int
GetPos - get read position is slice
func (*FixedSliceReader) Length ¶
func (s *FixedSliceReader) Length() int
Length - get length of slice
func (*FixedSliceReader) LookAhead ¶ added in v0.33.0
func (s *FixedSliceReader) LookAhead(offset int, data []byte) error
LookAhead returns data ahead of current pos if within bounds.
func (*FixedSliceReader) NrRemainingBytes ¶
func (s *FixedSliceReader) NrRemainingBytes() int
NrRemainingBytes - return number of bytes remaining
func (*FixedSliceReader) ReadBytes ¶
func (s *FixedSliceReader) ReadBytes(n int) []byte
ReadBytes - read a slice of n bytes Return empty slice if n bytes not available
func (*FixedSliceReader) ReadFixedLengthString ¶
func (s *FixedSliceReader) ReadFixedLengthString(n int) string
ReadFixedLengthString - read string of specified length n. Sets err and returns empty string if full length not available
func (*FixedSliceReader) ReadInt16 ¶
func (s *FixedSliceReader) ReadInt16() int16
ReadInt16 - read int16 from slice
func (*FixedSliceReader) ReadInt32 ¶
func (s *FixedSliceReader) ReadInt32() int32
ReadInt32 - read int32 from slice
func (*FixedSliceReader) ReadInt64 ¶
func (s *FixedSliceReader) ReadInt64() int64
ReadInt64 - read int64 from slice
func (*FixedSliceReader) ReadPossiblyZeroTerminatedString ¶ added in v0.45.0
func (s *FixedSliceReader) ReadPossiblyZeroTerminatedString(maxLen int) (str string, ok bool)
ReadPossiblyZeroTerminatedString - read string until zero byte but at most maxLen. If maxLen is reached and no zero-byte, return string and ok = false
func (*FixedSliceReader) ReadUint16 ¶
func (s *FixedSliceReader) ReadUint16() uint16
ReadUint16 - read uint16 from slice
func (*FixedSliceReader) ReadUint24 ¶
func (s *FixedSliceReader) ReadUint24() uint32
ReadUint24 - read uint24 from slice
func (*FixedSliceReader) ReadUint32 ¶
func (s *FixedSliceReader) ReadUint32() uint32
ReadUint32 - read uint32 from slice
func (*FixedSliceReader) ReadUint64 ¶
func (s *FixedSliceReader) ReadUint64() uint64
ReadUint64 - read uint64 from slice
func (*FixedSliceReader) ReadUint8 ¶
func (s *FixedSliceReader) ReadUint8() byte
ReadUint8 - read uint8 from slice
func (*FixedSliceReader) ReadZeroTerminatedString ¶
func (s *FixedSliceReader) ReadZeroTerminatedString(maxLen int) string
ReadZeroTerminatedString - read string until zero byte but at most maxLen Set err and return empty string if no zero byte found.
func (*FixedSliceReader) RemainingBytes ¶
func (s *FixedSliceReader) RemainingBytes() []byte
RemainingBytes - return remaining bytes of this slice
func (*FixedSliceReader) SetPos ¶
func (s *FixedSliceReader) SetPos(pos int)
SetPos - set read position is slice
func (*FixedSliceReader) SkipBytes ¶
func (s *FixedSliceReader) SkipBytes(n int)
SkipBytes - skip passed n bytes
type FixedSliceWriter ¶
type FixedSliceWriter struct {
// contains filtered or unexported fields
}
FixedSliceWriter - write numbers to a fixed []byte slice
func NewFixedSliceWriter ¶
func NewFixedSliceWriter(size int) *FixedSliceWriter
NewSliceWriter - create slice writer with fixed size.
func NewFixedSliceWriterFromSlice ¶
func NewFixedSliceWriterFromSlice(data []byte) *FixedSliceWriter
NewFixedSliceWriter - create writer around slice. The slice will not grow, but stay the same size. If too much data is written, there will be an accumuluated error. Can be retrieved with AccError()
func (*FixedSliceWriter) AccError ¶
func (sw *FixedSliceWriter) AccError() error
AccError - return accumulated erro
func (*FixedSliceWriter) Bytes ¶
func (sw *FixedSliceWriter) Bytes() []byte
Bytes - return buf up to what's written
func (*FixedSliceWriter) Capacity ¶
func (sw *FixedSliceWriter) Capacity() int
Capacity - max length of FixedSliceWriter buffer
func (*FixedSliceWriter) FlushBits ¶
func (sw *FixedSliceWriter) FlushBits()
FlushBits - write remaining bits to the underlying .Writer. bits will be left-shifted and zeros appended to fill up a byte.
func (*FixedSliceWriter) Len ¶
func (sw *FixedSliceWriter) Len() int
Len - length of FixedSliceWriter buffer written. Same as Offset()
func (*FixedSliceWriter) Offset ¶
func (sw *FixedSliceWriter) Offset() int
Offset - offset for writing in FixedSliceWriter buffer
func (*FixedSliceWriter) WriteBits ¶
func (sw *FixedSliceWriter) WriteBits(bits uint, n int)
func (*FixedSliceWriter) WriteBytes ¶
func (sw *FixedSliceWriter) WriteBytes(byteSlice []byte)
WriteBytes - write []byte
func (*FixedSliceWriter) WriteFlag ¶
func (sw *FixedSliceWriter) WriteFlag(f bool)
WriteFlag writes a flag as 1 bit.
func (*FixedSliceWriter) WriteInt16 ¶
func (sw *FixedSliceWriter) WriteInt16(n int16)
WriteInt16 - write int16 to slice
func (*FixedSliceWriter) WriteInt32 ¶
func (sw *FixedSliceWriter) WriteInt32(n int32)
WriteInt32 - write int32 to slice
func (*FixedSliceWriter) WriteInt64 ¶
func (sw *FixedSliceWriter) WriteInt64(n int64)
WriteInt64 - write int64 to slice
func (*FixedSliceWriter) WriteString ¶
func (sw *FixedSliceWriter) WriteString(s string, addZeroEnd bool)
WriteString - write string to slice with or without zero end
func (*FixedSliceWriter) WriteUint16 ¶
func (sw *FixedSliceWriter) WriteUint16(n uint16)
WriteUint16 - write uint16 to slice
func (*FixedSliceWriter) WriteUint24 ¶
func (sw *FixedSliceWriter) WriteUint24(n uint32)
WriteUint24 - write uint24 to slice
func (*FixedSliceWriter) WriteUint32 ¶
func (sw *FixedSliceWriter) WriteUint32(n uint32)
WriteUint32 - write uint32 to slice
func (*FixedSliceWriter) WriteUint48 ¶
func (sw *FixedSliceWriter) WriteUint48(u uint64)
WriteUint48 - write uint48
func (*FixedSliceWriter) WriteUint64 ¶
func (sw *FixedSliceWriter) WriteUint64(n uint64)
WriteUint64 - write uint64 to slice
func (*FixedSliceWriter) WriteUint8 ¶
func (sw *FixedSliceWriter) WriteUint8(n byte)
WriteUint8 - write byte to slice
func (*FixedSliceWriter) WriteUnityMatrix ¶
func (sw *FixedSliceWriter) WriteUnityMatrix()
WriteUnityMatrix - write a unity matrix for mvhd or tkhd
func (*FixedSliceWriter) WriteZeroBytes ¶
func (sw *FixedSliceWriter) WriteZeroBytes(n int)
WriteZeroBytes - write n byte of zeroes
type Reader ¶
type Reader struct {
// contains filtered or unexported fields
}
Reader is a bit reader that stops reading at first error and stores it. First error can be fetched usiin AccError().
func (*Reader) AccError ¶ added in v0.43.0
AccError - accumulated error is first error that occurred
func (*Reader) NrBitsRead ¶ added in v0.43.0
NrBitsRead returns total number of bits read into parser.
func (*Reader) NrBitsReadInCurrentByte ¶ added in v0.43.0
NrBitsReadInCurrentByte returns number of bits read in current byte.
func (*Reader) NrBytesRead ¶ added in v0.43.0
NrBytesRead returns how many bytes read into parser.
func (*Reader) ReadFlag ¶
ReadFlag reads 1 bit and interprets as a boolean flag. Returns false if error now or previously.
func (*Reader) ReadRemainingBytes ¶ added in v0.43.0
ReadRemainingBytes reads remaining bytes if byte-aligned. Returns nil if error now or previously.
func (*Reader) ReadSigned ¶ added in v0.43.0
ReadSigned reads a 2-complemented signed int with n bits.
type SliceReader ¶
type SliceReader interface { AccError() error ReadUint8() byte ReadUint16() uint16 ReadInt16() int16 ReadUint24() uint32 ReadUint32() uint32 ReadInt32() int32 ReadUint64() uint64 ReadInt64() int64 ReadFixedLengthString(n int) string ReadZeroTerminatedString(maxLen int) string ReadPossiblyZeroTerminatedString(maxLen int) (str string, ok bool) ReadBytes(n int) []byte RemainingBytes() []byte NrRemainingBytes() int SkipBytes(n int) SetPos(pos int) GetPos() int Length() int LookAhead(offset int, data []byte) error }
type SliceWriter ¶
type SliceWriter interface { Len() int Capacity() int Offset() int Bytes() []byte AccError() error WriteUint8(n byte) WriteUint16(n uint16) WriteInt16(n int16) WriteUint24(n uint32) WriteUint32(n uint32) WriteInt32(n int32) WriteUint48(u uint64) WriteUint64(n uint64) WriteInt64(n int64) WriteString(s string, addZeroEnd bool) WriteZeroBytes(n int) WriteBytes(byteSlice []byte) WriteUnityMatrix() WriteBits(bits uint, n int) WriteFlag(f bool) FlushBits() }
type Writer ¶
type Writer struct {
// contains filtered or unexported fields
}
Writer writes bits into underlying io.Writer. Stops writing at first error. That first error is stored and can later be checked with AccError().
func (*Writer) AccError ¶ added in v0.43.0
AccError returns the first error that occurred and stopped writing.