mirror of
https://github.com/AlexxIT/go2rtc.git
synced 2026-04-22 23:57:20 +08:00
2b7682cdb3
- replace traditional for loop with range-based for loop for clarity
refactor(ffmpeg): simplify cut function loop
- utilize range-based for loop instead of traditional for loop
refactor(ring): update API response mapping type
- change map type from `interface{}` to `any` for better type safety
refactor(stream): handle nil source in NewStream
- add nil check for source elements before processing
refactor(webrtc): unify payload handling in GetToken
- change map type from `interface{}` to `any` for consistency
refactor(ascii): optimize nested loops in Write function
- replace traditional for loops with range-based for loops for readability
refactor(bits): enhance readability in Reader methods
- replace traditional for loops with range-based for loops in Read functions
refactor(h264): modernize loop structures in DecodeConfig
- switch to range-based for loops for cleaner code
refactor(h265): streamline profile_tier_level loops
- utilize range-based for loops instead of traditional for loops
chore(core): remove commented-out test function for clarity
refactor(core): simplify RandString function loop
- change traditional for loop to range-based for loop
refactor(flvt): optimize timestamp handling in TestTimeToRTP
- switch to range-based for loop for iterating frames
refactor(nest): improve error handling in ExchangeSDP
- format error message with printf-style formatting for clarity
refactor(tapo): enhance securityEncode function
- change traditional for loop to range-based for loop for readability
fix(tcp): correct masking in websocket Write method
- replace traditional for loop with range-based for loop
refactor(tutk): modernize encoding loops in crypto functions
- utilize range-based for loops for better readability
refactor(tuya): unify data types in MQTT message struct
- change map type from `interface{}` to `any` for consistency
refactor(webrtc): standardize codec registration
- change map type from `interface{}` to `any` for type safety
refactor(yaml): simplify Unmarshal function signature
- update parameter type from `interface{}` to `any` for better clarity
144 lines
2.6 KiB
Go
144 lines
2.6 KiB
Go
package bits
|
|
|
|
type Reader struct {
|
|
EOF bool // if end of buffer raised during reading
|
|
|
|
buf []byte // total buf
|
|
byte byte // current byte
|
|
bits byte // bits left in byte
|
|
pos int // current pos in buf
|
|
}
|
|
|
|
func NewReader(b []byte) *Reader {
|
|
return &Reader{buf: b}
|
|
}
|
|
|
|
//goland:noinspection GoStandardMethods
|
|
func (r *Reader) ReadUint8() byte {
|
|
if r.bits != 0 {
|
|
return r.ReadBits8(8)
|
|
}
|
|
|
|
if r.pos >= len(r.buf) {
|
|
r.EOF = true
|
|
return 0
|
|
}
|
|
|
|
b := r.buf[r.pos]
|
|
r.pos++
|
|
return b
|
|
}
|
|
|
|
func (r *Reader) ReadUint16() uint16 {
|
|
if r.bits != 0 {
|
|
return r.ReadBits16(16)
|
|
}
|
|
return uint16(r.ReadUint8())<<8 | uint16(r.ReadUint8())
|
|
}
|
|
|
|
func (r *Reader) ReadUint24() uint32 {
|
|
if r.bits != 0 {
|
|
return r.ReadBits(24)
|
|
}
|
|
return uint32(r.ReadUint8())<<16 | uint32(r.ReadUint8())<<8 | uint32(r.ReadUint8())
|
|
}
|
|
|
|
func (r *Reader) ReadUint32() uint32 {
|
|
if r.bits != 0 {
|
|
return r.ReadBits(32)
|
|
}
|
|
return uint32(r.ReadUint8())<<24 | uint32(r.ReadUint8())<<16 | uint32(r.ReadUint8())<<8 | uint32(r.ReadUint8())
|
|
}
|
|
|
|
func (r *Reader) ReadBit() byte {
|
|
if r.bits == 0 {
|
|
r.byte = r.ReadUint8()
|
|
r.bits = 7
|
|
} else {
|
|
r.bits--
|
|
}
|
|
|
|
return (r.byte >> r.bits) & 0b1
|
|
}
|
|
|
|
func (r *Reader) ReadBits(n byte) (res uint32) {
|
|
for i := n - 1; i != 255; i-- {
|
|
res |= uint32(r.ReadBit()) << i
|
|
}
|
|
return
|
|
}
|
|
|
|
func (r *Reader) ReadBits8(n byte) (res uint8) {
|
|
for i := n - 1; i != 255; i-- {
|
|
res |= r.ReadBit() << i
|
|
}
|
|
return
|
|
}
|
|
|
|
func (r *Reader) ReadBits16(n byte) (res uint16) {
|
|
for i := n - 1; i != 255; i-- {
|
|
res |= uint16(r.ReadBit()) << i
|
|
}
|
|
return
|
|
}
|
|
|
|
func (r *Reader) ReadBits64(n byte) (res uint64) {
|
|
for i := n - 1; i != 255; i-- {
|
|
res |= uint64(r.ReadBit()) << i
|
|
}
|
|
return
|
|
}
|
|
|
|
func (r *Reader) ReadFloat32() float64 {
|
|
i := r.ReadUint16()
|
|
f := r.ReadUint16()
|
|
return float64(i) + float64(f)/65536
|
|
}
|
|
|
|
func (r *Reader) ReadBytes(n int) (b []byte) {
|
|
if r.bits == 0 {
|
|
if r.pos+n > len(r.buf) {
|
|
r.EOF = true
|
|
return nil
|
|
}
|
|
|
|
b = r.buf[r.pos : r.pos+n]
|
|
r.pos += n
|
|
} else {
|
|
b = make([]byte, n)
|
|
for i := range n {
|
|
b[i] = r.ReadUint8()
|
|
}
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// ReadUEGolomb - ReadExponentialGolomb (unsigned)
|
|
func (r *Reader) ReadUEGolomb() uint32 {
|
|
var size byte
|
|
for size = 0; size < 32; size++ {
|
|
if b := r.ReadBit(); b != 0 || r.EOF {
|
|
break
|
|
}
|
|
}
|
|
return r.ReadBits(size) + (1 << size) - 1
|
|
}
|
|
|
|
// ReadSEGolomb - ReadSignedExponentialGolomb
|
|
func (r *Reader) ReadSEGolomb() int32 {
|
|
if b := r.ReadUEGolomb(); b%2 == 0 {
|
|
return -int32(b / 2)
|
|
} else {
|
|
return int32((b + 1) / 2)
|
|
}
|
|
}
|
|
|
|
func (r *Reader) Left() []byte {
|
|
return r.buf[r.pos:]
|
|
}
|
|
|
|
func (r *Reader) Pos() (int, byte) {
|
|
return r.pos - 1, r.bits
|
|
}
|