-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathebspwriter.go
124 lines (111 loc) · 2.58 KB
/
ebspwriter.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
package bits
import (
"io"
)
// EBSPWriter write bits and insert start-code emulation prevention bytes as necessary.
// Ceases writing at first error.
// The first error can later be checked with AccError().
type EBSPWriter struct {
wr io.Writer // underlying writer
err error // The first error caused by any write operation
out []byte // Slice of length 1 to avoid allocation at output
n int // current number of bits
v uint // current accumulated value
nr0 int // Number preceding zero bytes
}
// NewEBSPWriter - 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 // start code emulation prevention
_, 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++
} else {
w.nr0 = 0
}
w.n -= 8
}
w.v &= Mask(8)
}
// WriteExpGolomb - write an exponential Golomb code
func (w *EBSPWriter) WriteExpGolomb(nr uint) {
offset := uint(0)
prefixLen := uint(0)
delta := uint(0)
max := uint(0)
for {
if nr <= max {
delta = nr - offset
break
}
offset += 1 << prefixLen
prefixLen++
max = offset + (1 << prefixLen) - 1
}
w.Write(1, int(prefixLen+1))
if prefixLen > 0 {
w.Write(delta, int(prefixLen))
}
}
// WriteSEIValue insert 0xFF until value is less than 255. Used in SEI payload type and size.
func (w *EBSPWriter) WriteSEIValue(val uint) {
for {
if val >= 255 {
w.Write(0xff, 8)
val -= 255
continue
}
w.Write(val, 8)
break
}
}
// WriteRbspTrailingBits - 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)
}
}
// AccError - return accumulated error
func (w *EBSPWriter) AccError() error {
return w.err
}
// NrBitsInBuffer - number bits written in buffer byte
func (w *EBSPWriter) NrBitsInBuffer() uint {
return uint(w.n)
}
// BitsInBuffer - n bits written in buffer byte, not written to underlying writer
func (w *EBSPWriter) BitsInBuffer() (bits, n uint) {
return w.v, uint(w.n)
}