1
1
mirror of https://github.com/wader/fq.git synced 2024-11-27 14:14:58 +03:00
fq/pkg/decode/io.go
2021-11-18 01:17:41 +01:00

34 lines
573 B
Go

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
}