forked from Eyevinn/mp4ff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ebsp_writer.go
73 lines (66 loc) · 1.51 KB
/
ebsp_writer.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
package bits
import (
"io"
)
// EbspWriter writes bits into underlying io.Writer and inserts
// start-code emulation prevention bytes as necessary
// Stops writing at first error.
// Errors that have occured can later be checked with Error().
type EBSPWriter struct {
n int // current number of bits
v uint // current accumulated value
err error // The first error caused by any write operation
nr0 int // Number preceeding zero bytes
out []byte // Slice of length 1 to avoid allocation at output
wr io.Writer
}
// NewWriter - returns a new Writer
func NewEBSPWriter(w io.Writer) *EBSPWriter {
return &EBSPWriter{
wr: w,
out: make([]byte, 1),
}
}
// Write - write n bits from bits and save error state
func (w *EBSPWriter) Write(bits uint, n int) {
if w.err != nil {
return
}
w.v <<= uint(n)
w.v |= bits & mask(n)
w.n += n
for w.n >= 8 {
b := (w.v >> (uint(w.n) - 8)) & mask(8)
if w.nr0 == 2 && b <= 3 {
w.out[0] = 0x3
_, err := w.wr.Write(w.out)
if err != nil {
w.err = err
return
}
w.nr0 = 0
}
w.out[0] = uint8(b)
_, err := w.wr.Write(w.out)
if err != nil {
w.err = err
return
}
if b == 0 {
w.nr0++
}
w.n -= 8
}
w.v &= mask(8)
}
// Write - write rbsp trailing bits (a 1 followed by zeros to a byte boundary)
func (w *EBSPWriter) WriteRbspTrailingBits() {
w.Write(1, 1)
w.StuffByteWithZeros()
}
// StuffByteWithZeros - write zero bits until byte boundary (0-7bits)
func (w *EBSPWriter) StuffByteWithZeros() {
if w.n > 0 {
w.Write(0, 8-w.n)
}
}