-
Notifications
You must be signed in to change notification settings - Fork 0
/
ebml.go
235 lines (204 loc) · 5.17 KB
/
ebml.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
package gomkv
import (
"fmt"
"io"
"os"
)
// Range is the byte-address range of the memory [Start, End).
type Range struct {
Start int64
End int64 // not inclusive
}
func (r Range) Size() int64 {
return r.End - r.Start
}
// Element ...
// [ID...Size...Data)
// | |
// At Offset
// [-- Size -->)
type Element struct {
ID uint32 // predefined hex
At int64 // offset to file start
Size int64 // total size including "header"
Offset int64 // data start relative to At
Level uint // a.k.a., depth
Children Elements
}
// DataRange ...
func (e *Element) DataRange() Range {
return Range{e.At + e.Offset, e.At + e.Size}
}
// Elements ...
type Elements []*Element
// Iter loops thru the element-tree in a depth-first manner.
func (es Elements) Iter(f func(*Element) error) error {
for _, e := range es {
if err := f(e); err != nil {
return err
}
if len(e.Children) > 0 {
if err := e.Children.Iter(f); err != nil {
return err
}
}
}
return nil
}
// GetVarN ...
/*
bits, big-endian
1xxx xxxx - value 0 to 2^7-2
01xx xxxx xxxx xxxx - value 0 to 2^14-2
001x xxxx xxxx xxxx xxxx xxxx - value 0 to 2^21-2
0001 xxxx xxxx xxxx xxxx xxxx xxxx xxxx - value 0 to 2^28-2
0000 1xxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx - value 0 to 2^35-2
0000 01xx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx - value 0 to 2^42-2
0000 001x xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx - value 0 to 2^49-2
0000 0001 xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx - value 0 to 2^56-2
*/
func GetVarN(b byte) int64 {
for i := 7; i >= 0; i-- {
if b&(0x1<<i) > 0 {
return int64(8 - i)
}
}
// should never happen
return 0
}
// ParseVarBits parses the variable bits for
// class ID (unmasked, as spec providing raw ID)
// or data size (masked, convert to number for calculation).
func ParseVarBits(bs []byte, masked bool) uint64 {
// fmt.Printf("%08b\n", bs)
n := len(bs)
if n < 1 || n > 8 {
return 0
}
acc := uint64(0)
for i := 0; i < n; i++ {
if i == 0 && masked {
acc += ((0x1 << (8 - n)) - 1) & uint64(bs[0]) // only the fisrt byte has mask
} else {
acc = acc<<8 + uint64(bs[i])
}
}
return acc
}
// ReadByte ...
func ReadByte(r io.ReaderAt, offset int64) (byte, error) {
bs := make([]byte, 1)
if _, err := r.ReadAt(bs, offset); err != nil {
return 0, nil
}
return bs[0], nil
}
// ReadVarBits ...
func ReadVarBits(m io.ReaderAt, offset int64, masked bool) (n int64, v uint64, err error) {
first, err := ReadByte(m, offset)
if err != nil {
return
}
n = GetVarN(first)
// fmt.Printf("%08b %d\n", first, n)
bs := make([]byte, n)
if _, err = m.ReadAt(bs, offset); err != nil {
return
}
v = ParseVarBits(bs, masked)
return
}
// ReadElement ...
func ReadElement(m io.ReaderAt, offset int64) (e *Element, err error) {
nC, id, err := ReadVarBits(m, offset, false)
if err != nil {
err = fmt.Errorf("read class ID: %w", err)
return
}
nS, size, err := ReadVarBits(m, offset+nC, true)
if err != nil {
err = fmt.Errorf("read data size: %w", err)
return
}
return &Element{
ID: uint32(id),
At: offset,
Offset: nC + nS,
Size: int64(size) + nC + nS,
}, nil
}
func getElements(m io.ReaderAt, r Range, level uint, nested map[uint32]bool) (es Elements, err error) {
var (
e *Element
cur = r.Start
)
for {
e, err = ReadElement(m, cur)
if err != nil {
return
}
e.Level = level
if isNested, ok := nested[e.ID]; ok && isNested {
e.Children, err = getElements(m, e.DataRange(), level+1, nested)
}
es = append(es, e)
// update
cur += e.Size
if cur >= r.End {
break
}
}
return
}
// EBMLReader reads the elements from an EBML file.
type EBMLReader interface {
// GetElements return the element-tree recursively w.r.t. class-id map nested.
GetElements() (Elements, error)
// ReadData reads the data bytes pointed by the element.
ReadData(e *Element) ([]byte, error)
}
type mkvReader struct {
r io.ReaderAt
s int64
nested map[uint32]bool
}
func (er *mkvReader) GetElements() (Elements, error) {
return getElements(er.r, Range{0, er.s}, 0, er.nested)
}
func (er *mkvReader) ReadData(e *Element) (bs []byte, err error) {
r := e.DataRange()
s := r.Size()
if s > 0 {
bs = make([]byte, s)
_, err = er.r.ReadAt(bs, r.Start)
}
return
}
// DefaultNested ...
func DefaultNested() map[uint32]bool {
return map[uint32]bool{
ElementEMBL: true,
ElementSegment: true,
ElementInfo: true,
ElementTracks: true,
ElementTags: true,
TrackEntry: true,
Tag: true,
}
}
// Open opens a MKV file and return the reader.
func Open(path string) (r EBMLReader, err error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
info, err := file.Stat()
if err != nil {
return nil, err
}
return &mkvReader{
r: file,
s: info.Size(),
nested: DefaultNested(),
}, nil
}