-
Notifications
You must be signed in to change notification settings - Fork 11
/
buffer.go
388 lines (297 loc) · 6.87 KB
/
buffer.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
package mpeg
import (
"io"
)
var (
// BufferSize is the default size for buffer.
BufferSize = 128 * 1024
)
// LoadFunc callback function.
type LoadFunc func(buffer *Buffer)
// Buffer provides the data source for all other interfaces.
type Buffer struct {
reader io.Reader
bytes []byte
bitIndex int
totalSize int
hasEnded bool
discardRead bool
available []byte
loadCallback LoadFunc
}
// NewBuffer creates a buffer instance.
func NewBuffer(r io.Reader) (*Buffer, error) {
buf := &Buffer{}
if r != nil {
seeker, ok := r.(io.Seeker)
if ok {
cur, err := seeker.Seek(0, io.SeekCurrent)
if err != nil {
return nil, err
}
off, err := seeker.Seek(0, io.SeekEnd)
if err != nil {
return nil, err
}
buf.totalSize = int(off)
_, err = seeker.Seek(cur, io.SeekStart)
if err != nil {
return nil, err
}
}
}
buf.reader = r
buf.bytes = make([]byte, 0, BufferSize)
buf.available = make([]byte, BufferSize)
buf.discardRead = true
return buf, nil
}
// Bytes returns a slice holding the unread portion of the buffer.
func (b *Buffer) Bytes() []byte {
return b.bytes
}
// Index returns byte index.
func (b *Buffer) Index() int {
return b.bitIndex >> 3
}
// Seekable returns true if reader is seekable.
func (b *Buffer) Seekable() bool {
return b.reader != nil && b.totalSize > 0
}
// Write appends the contents of p to the buffer.
func (b *Buffer) Write(p []byte) int {
if b.discardRead {
b.discardReadBytes()
}
b.bytes = append(b.bytes, p...)
b.hasEnded = false
return len(p)
}
// SignalEnd marks the current byte length as the end of this buffer and signal that no
// more data is expected to be written to it. This function should be called
// just after the last Write().
func (b *Buffer) SignalEnd() {
b.totalSize = len(b.bytes)
}
// SetLoadCallback sets a callback that is called whenever the buffer needs more data.
func (b *Buffer) SetLoadCallback(callback LoadFunc) {
b.loadCallback = callback
}
// Rewind the buffer back to the beginning. When loading from io.ReadSeeker,
// this also seeks to the beginning.
func (b *Buffer) Rewind() {
b.seek(0)
}
// Size returns the total size. For io.ReadSeeker, this returns the total size. For all other
// types it returns the number of bytes currently in the buffer.
func (b *Buffer) Size() int {
if b.totalSize > 0 {
return b.totalSize
}
return len(b.bytes)
}
// Remaining returns the number of remaining (yet unread) bytes in the buffer.
// This can be useful to throttle writing.
func (b *Buffer) Remaining() int {
return len(b.bytes) - (b.bitIndex >> 3)
}
// HasEnded checks whether the read position of the buffer is at the end and no more data is expected.
func (b *Buffer) HasEnded() bool {
return b.hasEnded
}
// LoadReaderCallback is a callback that is called whenever the buffer needs more data.
func (b *Buffer) LoadReaderCallback(buffer *Buffer) {
if b.hasEnded {
return
}
p := b.available
n, err := io.ReadFull(b.reader, p)
if err != nil {
if err == io.ErrUnexpectedEOF {
p = p[:n]
} else if err == io.EOF {
b.hasEnded = true
return
}
}
if n == 0 {
b.hasEnded = true
return
}
b.Write(p)
}
func (b *Buffer) seek(pos int) {
b.hasEnded = false
if b.reader != nil && b.totalSize > 0 {
seeker := b.reader.(io.Seeker)
_, _ = seeker.Seek(int64(pos), io.SeekStart)
b.bytes = b.bytes[:0]
b.bitIndex = 0
} else if b.reader == nil {
if pos != 0 {
return
}
b.bytes = b.bytes[:0]
b.bitIndex = 0
}
}
func (b *Buffer) tell() int {
if b.reader != nil && b.totalSize > 0 {
seeker := b.reader.(io.Seeker)
off, _ := seeker.Seek(0, io.SeekCurrent)
return int(off) + (b.bitIndex >> 3) - len(b.bytes)
}
return b.bitIndex >> 3
}
func (b *Buffer) discardReadBytes() {
bytePos := b.bitIndex >> 3
if bytePos == len(b.bytes) {
b.bytes = b.bytes[:0]
b.bitIndex = 0
} else if bytePos > 0 {
copy(b.bytes, b.bytes[bytePos:])
b.bytes = b.bytes[:len(b.bytes)-bytePos]
b.bitIndex -= bytePos << 3
}
}
func (b *Buffer) has(count int) bool {
if ((len(b.bytes) << 3) - b.bitIndex) >= count {
return true
}
if b.loadCallback != nil {
b.loadCallback(b)
if ((len(b.bytes) << 3) - b.bitIndex) >= count {
return true
}
}
if b.totalSize != 0 && len(b.bytes) == b.totalSize {
b.hasEnded = true
}
return false
}
func (b *Buffer) read(count int) int {
if !b.has(count) {
return 0
}
value := 0
for count != 0 {
currentByte := int(b.Bytes()[b.bitIndex>>3])
remaining := 8 - (b.bitIndex & 7) // Remaining bits in byte
read := count
if remaining < count { // Bits in self run
read = remaining
}
shift := remaining - read
mask := 0xff >> (8 - read)
value = (value << read) | ((currentByte & (mask << shift)) >> shift)
b.bitIndex += read
count -= read
}
return value
}
func (b *Buffer) read1() int {
if !b.has(1) {
return 0
}
currentByte := int(b.Bytes()[b.bitIndex>>3])
shift := 7 - (b.bitIndex & 7)
value := (currentByte & (1 << shift)) >> shift
b.bitIndex += 1
return value
}
func (b *Buffer) align() {
b.bitIndex = ((b.bitIndex + 7) >> 3) << 3 // Align to next byte
}
func (b *Buffer) skip(count int) {
if b.has(count) {
b.bitIndex += count
}
}
func (b *Buffer) skipBytes(v byte) int {
b.align()
skipped := 0
for b.has(8) && b.Bytes()[b.bitIndex>>3] == v {
b.bitIndex += 8
skipped++
}
return skipped
}
func (b *Buffer) nextStartCode() int {
b.align()
for b.has(5 << 3) {
data := b.Bytes()
byteIndex := b.bitIndex >> 3
if data[byteIndex] == 0x00 &&
data[byteIndex+1] == 0x00 &&
data[byteIndex+2] == 0x01 {
b.bitIndex = (byteIndex + 4) << 3
return int(data[byteIndex+3])
}
b.bitIndex += 8
}
return -1
}
func (b *Buffer) findStartCode(code int) int {
for {
current := b.nextStartCode()
if current == code || current == -1 {
return current
}
}
}
func (b *Buffer) hasStartCode(code int) int {
prevBitIndex := b.bitIndex
prevDiscardRead := b.discardRead
b.discardRead = false
current := b.findStartCode(code)
b.bitIndex = prevBitIndex
b.discardRead = prevDiscardRead
return current
}
func (b *Buffer) findFrameSync() bool {
var i int
for i = b.bitIndex >> 3; i < len(b.bytes)-1; i++ {
if b.Bytes()[i] == 0xFF && (b.Bytes()[i+1]&0xFE) == 0xFC {
b.bitIndex = ((i + 1) << 3) + 3
return true
}
}
b.bitIndex = (i + 1) << 3
return false
}
func (b *Buffer) peekNonZero(bitCount int) bool {
if !b.has(bitCount) {
return false
}
val := b.read(bitCount)
b.bitIndex -= bitCount
return val != 0
}
func (b *Buffer) readVlc(table []vlc) int {
var state vlc
for {
state = table[int(state.Index)+b.read1()]
if state.Index <= 0 {
break
}
}
return int(state.Value)
}
func (b *Buffer) readVlcUint(table []vlcUint) uint16 {
var state vlcUint
for {
state = table[int(state.Index)+b.read1()]
if state.Index <= 0 {
break
}
}
return state.Value
}
type vlc struct {
Index int16
Value int16
}
type vlcUint struct {
Index int16
Value uint16
}