-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathebspreader.go
264 lines (237 loc) · 5.19 KB
/
ebspreader.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package bits
import (
"encoding/binary"
"errors"
"fmt"
"io"
)
// ESBPReader errors
var (
ErrNotReadSeeker = errors.New("reader does not support Seek")
)
const (
startCodeEmulationPreventionByte = 0x03
)
// EBSPReader reads an EBSP bitstream dropping start-code emulation bytes.
// It also supports checking for more rbsp data and reading rbsp_trailing_bits.
type EBSPReader struct {
rd io.Reader
err error
n int // current number of bits
v uint // current accumulated value
pos int
zeroCount int // Count number of zero bytes read
}
// NewEBSPReader return a new EBSP reader stopping reading at first error.
func NewEBSPReader(rd io.Reader) *EBSPReader {
return &EBSPReader{
rd: rd,
pos: -1,
}
}
// AccError returns the accumulated error. If no error, returns nil.
func (r *EBSPReader) AccError() error {
return r.err
}
// NrBytesRead returns how many bytes read into parser.
func (r *EBSPReader) NrBytesRead() int {
return r.pos + 1 // Starts at -1
}
// NrBitsRead returns total number of bits read into parser.
func (r *EBSPReader) NrBitsRead() int {
nrBits := r.NrBytesRead() * 8
if r.NrBitsReadInCurrentByte() != 8 {
nrBits += r.NrBitsReadInCurrentByte() - 8
}
return nrBits
}
// NrBitsReadInCurrentByte returns number of bits read in current byte.
func (r *EBSPReader) NrBitsReadInCurrentByte() int {
return 8 - r.n
}
// Read reads n bits and respects and accumulates errors. If error, returns 0.
func (r *EBSPReader) Read(n int) uint {
if r.err != nil {
return 0
}
var err error
for r.n < n {
r.v <<= 8
var b uint8
err = binary.Read(r.rd, binary.BigEndian, &b)
if err != nil {
r.err = err
return 0
}
r.pos++
if r.zeroCount == 2 && b == startCodeEmulationPreventionByte {
err = binary.Read(r.rd, binary.BigEndian, &b)
if err != nil {
r.err = err
return 0
}
r.pos++
r.zeroCount = 0
}
if b != 0 {
r.zeroCount = 0
} else {
r.zeroCount++
}
r.v |= uint(b)
r.n += 8
}
v := r.v >> uint(r.n-n)
r.n -= n
r.v &= Mask(r.n)
return v
}
// ReadBytes read n bytes and return nil if new or accumulated error.
func (r *EBSPReader) ReadBytes(n int) []byte {
if r.err != nil {
return nil
}
payload := make([]byte, n)
for i := 0; i < n; i++ {
b := byte(r.Read(8))
payload[i] = b
}
if r.err != nil {
return nil
}
return payload
}
// ReadFlag reads 1 bit and translates a bool.
func (r *EBSPReader) ReadFlag() bool {
return r.Read(1) == 1
}
// ReadExpGolomb reads one unsigned exponential Golomb code.
func (r *EBSPReader) ReadExpGolomb() uint {
if r.err != nil {
return 0
}
leadingZeroBits := 0
for {
b := r.Read(1)
if r.err != nil {
return 0
}
if b == 1 {
break
}
leadingZeroBits++
}
var res uint = (1 << leadingZeroBits) - 1
endBits := r.Read(leadingZeroBits)
if r.err != nil {
return 0
}
return res + endBits
}
// ReadSignedGolomb reads one signed exponential Golomb code.
func (r *EBSPReader) ReadSignedGolomb() int {
if r.err != nil {
return 0
}
unsignedGolomb := r.ReadExpGolomb()
if r.err != nil {
return 0
}
if unsignedGolomb%2 == 1 {
return int((unsignedGolomb + 1) / 2)
}
return -int(unsignedGolomb / 2)
}
// IsSeeker returns tru if underluing reader supports Seek interface.
func (r *EBSPReader) IsSeeker() bool {
_, ok := r.rd.(io.ReadSeeker)
return ok
}
// MoreRbspData returns false if next bit is 1 and last 1-bit in fullSlice.
// Underlying reader must support ReadSeeker interface to reset after check.
// Return false, nil if underlying error.
func (r *EBSPReader) MoreRbspData() (bool, error) {
if !r.IsSeeker() {
return false, ErrNotReadSeeker
}
// Find out if next position is the last 1
stateCopy := *r
firstBit := r.Read(1)
if r.err != nil {
return false, nil
}
if firstBit != 1 {
err := r.reset(stateCopy)
if err != nil {
return false, err
}
return true, nil
}
// If all remainging bits are zero, there is no more rbsp data
more := false
for {
b := r.Read(1)
if r.err == io.EOF {
r.err = nil // Reset
break
}
if r.err != nil {
return false, nil
}
if b == 1 {
more = true
break
}
}
err := r.reset(stateCopy)
if err != nil {
return false, err
}
return more, nil
}
// reset resets EBSPReader based on copy of previous state.
func (r *EBSPReader) reset(prevState EBSPReader) error {
rdSeek, _ := r.rd.(io.ReadSeeker)
_, err := rdSeek.Seek(int64(prevState.pos+1), 0)
if err != nil {
return err
}
r.n = prevState.n
r.v = prevState.v
r.pos = prevState.pos
r.zeroCount = prevState.zeroCount
return nil
}
// ReadRbspTrailingBits reads rbsp_traling_bits. Returns error if wrong pattern.
// If other error, returns nil and let AccError() provide that error.
func (r *EBSPReader) ReadRbspTrailingBits() error {
if r.err != nil {
return nil
}
firstBit := r.Read(1)
if r.err != nil {
return nil
}
if firstBit != 1 {
return fmt.Errorf("rbspTrailingBits don't start with 1")
}
for {
b := r.Read(1)
if r.err == io.EOF {
r.err = nil // Reset
return nil
}
if r.err != nil {
return nil
}
if b == 1 {
return fmt.Errorf("another 1 in RbspTrailingBits")
}
}
}
// SetError sets an error if not already set.
func (r *EBSPReader) SetError(err error) {
if r.err == nil {
r.err = err
}
}