-
Notifications
You must be signed in to change notification settings - Fork 2
/
scanner.go
297 lines (277 loc) · 7.41 KB
/
scanner.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
package matroska
import (
"errors"
"fmt"
"github.com/coding-socks/ebml"
"github.com/coding-socks/ebml/schema"
"io"
)
// ErrUnexpectedClusterElement means that Cluster was encountered before
// an Info and a Tracks Element or a SeekHead Element.
//
// The first Info Element and the first Tracks Element MUST either be
// stored before the first Cluster Element or both SHALL be referenced
// by a SeekHead Element occurring before the first Cluster Element.
var ErrUnexpectedClusterElement = errors.New("unexpected Cluster")
type Scanner struct {
decoder *ebml.Decoder
header *ebml.EBML
segmentEl ebml.Element
segmentStart int64
info *Info
tracks *Tracks
seekHead *SeekHead
// fSeekHead is an attempt to recreate SeekHead in case it is missing.
fSeekHead *SeekHead
offset int64
cluster Cluster
err error
}
func NewScanner(r io.ReadSeeker) (*Scanner, error) {
d := ebml.NewDecoder(r)
h, err := d.DecodeHeader()
if err != nil {
return nil, fmt.Errorf("matroska: could not decode header: %w", err)
}
if h.DocType != DocType {
return nil, fmt.Errorf("matroska: cannot decode DocType: %v", h.DocType)
}
def, err := ebml.Definition(h.DocType)
if err != nil {
panic("matroska: document type is not registered")
}
s := Scanner{
decoder: d,
header: h,
fSeekHead: &SeekHead{},
}
if err := s.init(def); err != nil {
return nil, err
}
return &s, nil
}
// Header returns the ebml.EBML element of the matroska document
func (s *Scanner) Header() *ebml.EBML {
return s.header
}
// Info returns the Info element of the matroska document.
func (s *Scanner) Info() *Info {
return s.info
}
// Tracks returns the Tracks element of the matroska document.
func (s *Scanner) Tracks() *Tracks {
return s.tracks
}
// SeekHead returns the SeekHead element of the matroska document
// or a constructed version of it. The second return value is false
// when it cannot be trusted because it is constructed.
func (s *Scanner) SeekHead() (*SeekHead, bool) {
if s.seekHead == nil {
return s.fSeekHead, false
}
return s.seekHead, true
}
func (s *Scanner) Next() bool {
d := s.decoder
segmentEl := s.segmentEl
for {
el, n, err := d.NextOf(segmentEl, s.offset)
if segmentEl.DataSize.Known() {
s.offset += int64(n)
}
if err == ebml.ErrInvalidVINTLength {
continue
} else if err == io.EOF {
return false
} else if err != nil {
s.err = err
return false
}
if segmentEl.DataSize.Known() {
// detect element overflow early to pretend the element is smaller
if segmentEl.DataSize.Size() < s.offset+el.DataSize.Size() {
el.DataSize = ebml.NewKnownDataSize(segmentEl.DataSize.Size() - s.offset)
}
s.offset += el.DataSize.Size()
}
switch el.ID {
default:
if _, err := d.Seek(el.DataSize.Size(), io.SeekCurrent); err != nil {
s.err = fmt.Errorf("matroska: could not skip %v: %w", el.ID, err)
return false
}
case IDCluster:
var cl Cluster
if err := d.Decode(&cl); err != nil {
s.err = fmt.Errorf("matroska: could not decode %v: %w", el.ID, err)
return err == ebml.ErrElementOverflow
}
s.cluster = cl
return true
}
}
}
func (s *Scanner) Cluster() Cluster {
return s.cluster
}
func (s *Scanner) Err() error {
return s.err
}
func (s *Scanner) init(def *ebml.Def) error {
// find root element
segment:
for {
el, _, err := s.decoder.Next()
if err != nil {
return fmt.Errorf("matroska: %w", err)
}
switch el.ID {
default:
return fmt.Errorf("matroska: got %v instead of segment", el.ID)
case ebml.IDVoid:
continue
case def.Root.ID:
s.segmentEl = el
break segment // Done here
}
}
s.segmentStart, _ = s.decoder.Seek(0, io.SeekCurrent)
var offset int64
for {
el, n, err := s.decoder.NextOf(s.segmentEl, offset)
if s.segmentEl.DataSize.Known() {
offset += int64(n)
}
if err == ebml.ErrInvalidVINTLength {
continue
} else if err == io.EOF {
return io.ErrUnexpectedEOF
} else if err != nil {
return fmt.Errorf("matroska: could not decode element: %w", err)
}
if s.segmentEl.DataSize.Known() {
// detect element overflow early to pretend the element is smaller
if s.segmentEl.DataSize.Size() < s.offset+el.DataSize.Size() {
el.DataSize = ebml.NewKnownDataSize(s.segmentEl.DataSize.Size() - s.offset)
}
offset += el.DataSize.Size()
}
if err := s.updateFSeek(el); err != nil {
return err
}
switch el.ID {
default:
if _, err := s.decoder.Seek(el.DataSize.Size(), io.SeekCurrent); err != nil {
return fmt.Errorf("matroska: could not skip %v: %w", el.ID, err)
}
continue
case IDSeekHead:
sh := &SeekHead{}
if err := s.decoder.Decode(sh); err != nil {
return fmt.Errorf("matroska: could not decode %v: %w", el.ID, err)
}
// There could be a second SeekHead element according to Section 6.3.
if s.seekHead == nil {
s.seekHead = sh
if o, found := s.seekTo(IDSeekHead, 0); found {
offset = o
continue
}
} else {
s.seekHead.Seek = append(s.seekHead.Seek, sh.Seek...)
}
case IDInfo:
s.info = &Info{}
if err := s.decoder.Decode(s.info); err != nil {
return fmt.Errorf("matroska: could not decode %v: %w", el.ID, err)
}
case IDTracks:
s.tracks = &Tracks{}
if err := s.decoder.Decode(s.tracks); err != nil {
return fmt.Errorf("matroska: could not decode %v: %w", el.ID, err)
}
case IDCluster:
return ErrUnexpectedClusterElement
}
if s.seekHead != nil && s.info == nil {
offset, _ = s.seekTo(IDInfo, 0)
continue
}
if s.seekHead != nil && s.tracks == nil {
offset, _ = s.seekTo(IDTracks, 0)
continue
}
if s.info != nil && s.tracks != nil {
break
}
}
if s.seekHead != nil {
offset, _ = s.seekTo(IDCluster, 0)
} else {
// find cluster element
cluster:
for {
el, n, err := s.decoder.Next()
if s.segmentEl.DataSize.Known() {
offset += int64(n)
}
if err != nil {
return fmt.Errorf("matroska: %w", err)
}
if err := s.updateFSeek(el); err != nil {
return err
}
if s.segmentEl.DataSize.Known() {
// detect element overflow early to pretend the element is smaller
if s.segmentEl.DataSize.Size() < s.offset+el.DataSize.Size() {
el.DataSize = ebml.NewKnownDataSize(s.segmentEl.DataSize.Size() - s.offset)
}
offset += el.DataSize.Size()
}
switch el.ID {
default:
if _, err := s.decoder.Seek(el.DataSize.Size(), io.SeekCurrent); err != nil {
return fmt.Errorf("matroska: could not skip %v: %w", el.ID, err)
}
continue
case IDCluster:
if _, err := s.decoder.Seek(int64(-n), io.SeekCurrent); err != nil {
return fmt.Errorf("matroska: could not revert read: %w", err)
}
break cluster // Done here
}
}
}
s.offset = offset
return nil
}
func (s *Scanner) seekTo(seekID schema.ElementID, n int) (int64, bool) {
if s.seekHead == nil {
return 0, false
}
i := 0
for _, seek := range s.seekHead.Seek {
if seek.SeekID == seekID {
if i < n {
i++
continue
}
s.decoder.Seek(s.segmentStart+int64(seek.SeekPosition), io.SeekStart)
return int64(seek.SeekPosition), true
}
}
return 0, false
}
func (s *Scanner) updateFSeek(el ebml.Element) error {
if s.seekHead == nil && el.ID != IDSeekHead && el.ID != ebml.IDVoid && el.ID != ebml.IDCRC32 {
pos, err := s.decoder.Seek(0, io.SeekCurrent)
if err != nil {
return fmt.Errorf("matroska: could not read position: %w", err)
}
s.fSeekHead.Seek = append(s.fSeekHead.Seek, Seek{
SeekID: el.ID,
SeekPosition: uint(pos - s.segmentStart),
})
}
return nil
}