1
1
mirror of https://github.com/wader/fq.git synced 2024-12-25 14:23:18 +03:00
fq/pkg/decode/io.go

34 lines
573 B
Go
Raw Normal View History

2020-06-08 03:29:51 +03:00
package decode
import (
"io"
)
// TODO: move?
// TODO: make generic replace reader? share with id3v2 unsync?
type NALUnescapeReader struct {
io.Reader
lastTwoZeros [2]bool
}
func (r NALUnescapeReader) Read(p []byte) (n int, err error) {
n, err = r.Reader.Read(p)
ni := 0
for i, b := range p[0:n] {
if r.lastTwoZeros[0] && r.lastTwoZeros[1] && b == 0x03 {
n--
r.lastTwoZeros[0] = false
r.lastTwoZeros[1] = false
continue
} else {
r.lastTwoZeros[1] = r.lastTwoZeros[0]
r.lastTwoZeros[0] = b == 0
}
p[ni] = p[i]
ni++
}
return n, err
}