forked from Eyevinn/mp4ff
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bits.go
146 lines (123 loc) · 2.61 KB
/
bits.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
package bits
import (
"encoding/binary"
"io"
)
// Writer writes bits into underlying io.Writer. Stops writing at first error.
// Errors that have occured can later be checked with Error().
type Writer struct {
n int // current number of bits
v uint // current accumulated value
err error // The first error caused by any write operation
out []byte // Slice of length 1 to avoid allocation at output
wr io.Writer
}
// NewWriter - returns a new Writer
func NewWriter(w io.Writer) *Writer {
return &Writer{
wr: w,
out: make([]byte, 1),
}
}
// Write - write n bits from bits and save error state
func (w *Writer) 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)
w.out[0] = uint8(b)
_, err := w.wr.Write(w.out)
if err != nil {
w.err = err
return
}
w.n -= 8
}
w.v &= mask(8)
}
// Flush - write remaining bits to the underlying io.Writer.
// bits will be left-shifted.
func (w *Writer) Flush() {
if w.err != nil {
return
}
if w.n != 0 {
b := (w.v << (8 - uint(w.n))) & mask(8)
if err := binary.Write(w.wr, binary.BigEndian, uint8(b)); err != nil {
w.err = err
return
}
}
}
// Error - error that has occured and stopped writing
func (w *Writer) Error() error {
return w.err
}
// Reader - read bits from the given io.Reader
type Reader struct {
n int // current number of bits
v uint // current accumulated value
rd io.Reader
}
// NewReader - return a new Reader
func NewReader(rd io.Reader) *Reader {
return &Reader{
rd: rd,
}
}
// Read - read n bits
func (r *Reader) Read(n int) (uint, error) {
var err error
for r.n < n {
r.v <<= 8
var b uint8
err = binary.Read(r.rd, binary.BigEndian, &b)
if err != nil && err != io.EOF {
return 0, err
}
r.v |= uint(b)
r.n += 8
}
v := r.v >> uint(r.n-n)
r.n -= n
r.v &= mask(r.n)
return v, err
}
// ReadFlag - read 1 bit into flag
func (r *Reader) ReadFlag() (bool, error) {
bit, err := r.Read(1)
if err != nil {
return false, err
}
return bit == 1, nil
}
// MustRead - Read bits and panic if not possible
func (r *Reader) MustRead(n int) uint {
var err error
for r.n < n {
r.v <<= 8
var b uint8
err = binary.Read(r.rd, binary.BigEndian, &b)
if err != nil {
panic("Reading error")
}
r.v |= uint(b)
r.n += 8
}
v := r.v >> uint(r.n-n)
r.n -= n
r.v &= mask(r.n)
return v
}
// MustReadFlag - read 1 bit into flag and panic if not possible
func (r *Reader) MustReadFlag() bool {
return r.MustRead(1) == 1
}
// mask - n-bit binary mask
func mask(n int) uint {
return (1 << uint(n)) - 1
}