bits

package
v0.41.0 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2024 License: MIT, MIT Imports: 5 Imported by: 6

Documentation

Overview

Package bits - bit reading and writing including Golomb codes and EBSP.

Beyond plain bit reading and writing, reading of ebsp (Encapsulated Byte Sequence Packets) Golomb codes as used in the AVC/H.264 and HEVC video coding standards.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotReedSeeker = errors.New("Reader does not support Seek")
)

ESBPReader errors

View Source
var (
	ErrSliceRead = fmt.Errorf("read too far in SliceReader")
)

SliceReader errors

View Source
var SliceWriterError = errors.New("overflow in SliceWriter")

Functions

func CeilLog2 added in v0.35.0

func CeilLog2(n uint) int

CeilLog2 returns nr bits needed to represent numbers 0 - n-1.

func EBSP2rbsp

func EBSP2rbsp(ebsp []byte) []byte

EBSP2rbsp - convert from EBSP to RBSP by removing start code emulation prevention bytes

Types

type AccErrByteWriter

type AccErrByteWriter struct {
	// contains filtered or unexported fields
}

AccErrByteWriter - writer that wraps an io.Writer and accumulates error

func NewAccErrByteWriter

func NewAccErrByteWriter(w io.Writer) *AccErrByteWriter

NewAccErrByteWriter - create accumulated error writer around io.Writer

func (*AccErrByteWriter) AccError

func (a *AccErrByteWriter) AccError() error

AccError - return accumulated error

func (*AccErrByteWriter) WriteSlice

func (a *AccErrByteWriter) WriteSlice(s []byte)

WriteSlice - write a slice

func (*AccErrByteWriter) WriteUint16

func (a *AccErrByteWriter) WriteUint16(u uint16)

WriteUint16 - write uint16

func (*AccErrByteWriter) WriteUint32

func (a *AccErrByteWriter) WriteUint32(u uint32)

WriteUint32 - write uint32

func (*AccErrByteWriter) WriteUint48

func (a *AccErrByteWriter) WriteUint48(u uint64)

WriteUint48 - write uint48

func (*AccErrByteWriter) WriteUint64

func (a *AccErrByteWriter) WriteUint64(u uint64)

WriteUint64 - write uint64

func (*AccErrByteWriter) WriteUint8

func (a *AccErrByteWriter) WriteUint8(b byte)

WriteUint8 - write a byte

type AccErrEBSPReader

type AccErrEBSPReader struct {
	// contains filtered or unexported fields
}

AccErrEBSPReader - Reader that drops start code emulation 0x03 after two bytes of 0x00

func NewAccErrEBSPReader

func NewAccErrEBSPReader(rd io.Reader) *AccErrEBSPReader

NewAccErrEBSPReader - return a new reader accumulating errors.

func (*AccErrEBSPReader) AccError

func (r *AccErrEBSPReader) AccError() error

AccError - accumulated error

func (*AccErrEBSPReader) IsSeeker

func (r *AccErrEBSPReader) IsSeeker() bool

IsSeeker - does reader support Seek

func (*AccErrEBSPReader) MoreRbspData

func (r *AccErrEBSPReader) MoreRbspData() (bool, error)

MoreRbspData - false if next bit is 1 and last 1 in fullSlice. Underlying reader must support ReadSeeker interface to reset after check Return false, nil if underlying error

func (*AccErrEBSPReader) NrBitsRead added in v0.40.0

func (r *AccErrEBSPReader) NrBitsRead() int

NrBitsRead - how many bits read into parser

func (*AccErrEBSPReader) NrBitsReadInCurrentByte

func (r *AccErrEBSPReader) NrBitsReadInCurrentByte() int

NrBitsReadInCurrentByte - how many bits have been read

func (*AccErrEBSPReader) NrBytesRead

func (r *AccErrEBSPReader) NrBytesRead() int

NrBytesRead - how many bytes read into parser

func (*AccErrEBSPReader) Read

func (r *AccErrEBSPReader) Read(n int) uint

Read - read n bits and return 0 if (previous) error

func (*AccErrEBSPReader) ReadBytes

func (r *AccErrEBSPReader) ReadBytes(n int) []byte

Read - read n bytes and return nil if (previous) error or if n bytes not available

func (*AccErrEBSPReader) ReadExpGolomb

func (r *AccErrEBSPReader) ReadExpGolomb() uint

ReadExpGolomb - Read one unsigned exponential golomb code. Return 0 if error

func (*AccErrEBSPReader) ReadFlag

func (r *AccErrEBSPReader) ReadFlag() bool

ReadFlag - read 1 bit into bool. Return false if not possible

func (*AccErrEBSPReader) ReadRbspTrailingBits

func (r *AccErrEBSPReader) ReadRbspTrailingBits() error

ReadRbspTrailingBits - read rbsp_traling_bits. Return error if wrong pattern If other error, return nil and let AccError provide that error

func (*AccErrEBSPReader) ReadSignedGolomb

func (r *AccErrEBSPReader) ReadSignedGolomb() int

ReadSignedGolomb - Read one signed exponential golomb code. Return 0 if error

func (*AccErrEBSPReader) SetError

func (r *AccErrEBSPReader) SetError(err error)

SetError - set an error if not already set.

type AccErrReader

type AccErrReader struct {
	// contains filtered or unexported fields
}

AccErrReader - bit reader that accumulates error First error can be fetched as reader.AccError()

func NewAccErrReader

func NewAccErrReader(rd io.Reader) *AccErrReader

NewAccErrReader - return a new Reader

func (*AccErrReader) AccError

func (r *AccErrReader) AccError() error

AccError - accumulated error is first error that occurred

func (*AccErrReader) Read

func (r *AccErrReader) Read(n int) uint

Read - read n bits. Return 0, if error now or previously

func (*AccErrReader) ReadFlag

func (r *AccErrReader) ReadFlag() bool

ReadFlag - read 1 bit into flag. Return false if error now or previously

func (*AccErrReader) ReadRemainingBytes

func (r *AccErrReader) ReadRemainingBytes() []byte

ReadRemainingBytes - read remaining bytes if byte-aligned

func (*AccErrReader) ReadSigned added in v0.41.0

func (r *AccErrReader) ReadSigned(n int) int

ReadSigned reads a 2-complemented signed int with n bits.

func (*AccErrReader) ReadVInt

func (r *AccErrReader) ReadVInt(n int) int

ReadFlag - Read i(v) which is 2-complement of n bits

type EBSPReader

type EBSPReader struct {
	// contains filtered or unexported fields
}

EBSPReader - Reader that drops start code emulation 0x03 after two bytes of 0x00

func NewEBSPReader

func NewEBSPReader(rd io.Reader) *EBSPReader

NewEBSPReader - return a new Reader.

func (*EBSPReader) IsSeeker

func (r *EBSPReader) IsSeeker() bool

IsSeeker - does reader support Seek

func (*EBSPReader) MoreRbspData

func (r *EBSPReader) MoreRbspData() (bool, error)

MoreRbspData - false if next bit is 1 and last 1 in fullSlice Underlying reader must support ReadSeeker interface to reset after check

func (*EBSPReader) MustRead

func (r *EBSPReader) MustRead(n int) uint

MustRead - read n bits and panic if not possible

func (*EBSPReader) MustReadExpGolomb

func (r *EBSPReader) MustReadExpGolomb() uint

MustReadExpGolomb - Read one unsigned exponential golomb code. Panic if not possible

func (*EBSPReader) MustReadFlag

func (r *EBSPReader) MustReadFlag() bool

MustReadFlag - read 1 bit into flag. Panic if not possible

func (*EBSPReader) MustReadSignedGolomb

func (r *EBSPReader) MustReadSignedGolomb() int

MustReadSignedGolomb - Read one signed exponential golomb code. Panic if not possible

func (*EBSPReader) NrBitsReadInCurrentByte

func (r *EBSPReader) NrBitsReadInCurrentByte() int

NrBitsReadInCurrentByte - how many bits have been read

func (*EBSPReader) NrBytesRead

func (r *EBSPReader) NrBytesRead() int

NrBytesRead - how many bytes read into parser

func (*EBSPReader) Read

func (r *EBSPReader) Read(n int) (uint, error)

Read - read n bits and return error if not possible

func (*EBSPReader) ReadExpGolomb

func (r *EBSPReader) ReadExpGolomb() (uint, error)

ReadExpGolomb - Read one unsigned exponential golomb code

func (*EBSPReader) ReadFlag

func (r *EBSPReader) ReadFlag() (bool, error)

ReadFlag - read 1 bit into flag. Return error if not possible

func (*EBSPReader) ReadRbspTrailingBits

func (r *EBSPReader) ReadRbspTrailingBits() error

ReadRbspTrailingBits - read rbsp_traling_bits. Return false if wrong pattern

func (*EBSPReader) ReadSignedGolomb

func (r *EBSPReader) ReadSignedGolomb() (int, error)

ReadSignedGolomb - Read one signed exponential golomb code

type EBSPWriter

type EBSPWriter struct {
	// contains filtered or unexported fields
}

EBSPWriter - write bits and insert start-code emulation prevention bytes as necessary. Cease writing at first error. Errors that have occurred 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) 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 - read bits from the given io.Reader

func NewReader

func NewReader(rd io.Reader) *Reader

NewReader - return a new Reader

func (*Reader) MustRead

func (r *Reader) MustRead(n int) uint

MustRead - Read bits and panic if not possible

func (*Reader) MustReadFlag

func (r *Reader) MustReadFlag() bool

MustReadFlag - read 1 bit into flag and panic if not possible

func (*Reader) Read

func (r *Reader) Read(n int) (uint, error)

Read - read n bits

func (*Reader) ReadFlag

func (r *Reader) ReadFlag() (bool, error)

ReadFlag - read 1 bit into flag

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
	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. Errors that have occurred can later be checked with Error().

func NewWriter

func NewWriter(w io.Writer) *Writer

NewWriter - returns a new Writer

func (*Writer) Error

func (w *Writer) Error() error

Error - error that has occurred and stopped writing

func (*Writer) Flush

func (w *Writer) Flush()

Flush - write remaining bits to the underlying io.Writer. bits will be left-shifted.

func (*Writer) Write

func (w *Writer) Write(bits uint, n int)

Write - write n bits from bits and save error state

Jump to

Keyboard shortcuts

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