mirror of
https://github.com/aler9/gortsplib
synced 2025-10-14 19:26:20 +08:00
improve performance of H264/H265 SPS parsers
This commit is contained in:
@@ -5,19 +5,33 @@ import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// HasSpace checks whether buffer has space for N bits.
|
||||
func HasSpace(buf []byte, pos int, n int) error {
|
||||
if n > ((len(buf) * 8) - pos) {
|
||||
return fmt.Errorf("not enough bits")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ReadBits reads N bits.
|
||||
func ReadBits(buf []byte, pos *int, n int) (uint64, error) {
|
||||
if n > ((len(buf) * 8) - *pos) {
|
||||
return 0, fmt.Errorf("not enough bits")
|
||||
err := HasSpace(buf, *pos, n)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return ReadBitsUnsafe(buf, pos, n), nil
|
||||
}
|
||||
|
||||
// ReadBitsUnsafe reads N bits.
|
||||
func ReadBitsUnsafe(buf []byte, pos *int, n int) uint64 {
|
||||
v := uint64(0)
|
||||
|
||||
res := 8 - (*pos & 0x07)
|
||||
if n < res {
|
||||
v := uint64((buf[*pos>>0x03] >> (res - n)) & (1<<n - 1))
|
||||
*pos += n
|
||||
return v, nil
|
||||
return v
|
||||
}
|
||||
|
||||
v = (v << res) | uint64(buf[*pos>>0x03]&(1<<res-1))
|
||||
@@ -35,7 +49,7 @@ func ReadBits(buf []byte, pos *int, n int) (uint64, error) {
|
||||
*pos += n
|
||||
}
|
||||
|
||||
return v, nil
|
||||
return v
|
||||
}
|
||||
|
||||
// ReadGolombUnsigned reads an unsigned golomb-encoded value.
|
||||
@@ -93,29 +107,17 @@ func ReadGolombSigned(buf []byte, pos *int) (int32, error) {
|
||||
|
||||
// ReadFlag reads a boolean flag.
|
||||
func ReadFlag(buf []byte, pos *int) (bool, error) {
|
||||
if (len(buf)*8 - *pos) == 0 {
|
||||
return false, fmt.Errorf("not enough bits")
|
||||
err := HasSpace(buf, *pos, 1)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return ReadFlagUnsafe(buf, pos), nil
|
||||
}
|
||||
|
||||
// ReadFlagUnsafe reads a boolean flag.
|
||||
func ReadFlagUnsafe(buf []byte, pos *int) bool {
|
||||
b := (buf[*pos>>0x03] >> (7 - (*pos & 0x07))) & 0x01
|
||||
*pos++
|
||||
return b == 1, nil
|
||||
}
|
||||
|
||||
// ReadUint8 reads a uint8.
|
||||
func ReadUint8(buf []byte, pos *int) (uint8, error) {
|
||||
v, err := ReadBits(buf, pos, 8)
|
||||
return uint8(v), err
|
||||
}
|
||||
|
||||
// ReadUint16 reads a uint16.
|
||||
func ReadUint16(buf []byte, pos *int) (uint16, error) {
|
||||
v, err := ReadBits(buf, pos, 16)
|
||||
return uint16(v), err
|
||||
}
|
||||
|
||||
// ReadUint32 reads a uint32.
|
||||
func ReadUint32(buf []byte, pos *int) (uint32, error) {
|
||||
v, err := ReadBits(buf, pos, 32)
|
||||
return uint32(v), err
|
||||
return b == 1
|
||||
}
|
||||
|
Reference in New Issue
Block a user