-
Notifications
You must be signed in to change notification settings - Fork 31
/
read.go
199 lines (168 loc) · 5 KB
/
read.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
package mp4
import (
"errors"
"fmt"
"io"
)
type BoxPath []BoxType
func (lhs BoxPath) compareWith(rhs BoxPath) (forwardMatch bool, match bool) {
if len(lhs) > len(rhs) {
return false, false
}
for i := 0; i < len(lhs); i++ {
if !lhs[i].MatchWith(rhs[i]) {
return false, false
}
}
if len(lhs) < len(rhs) {
return true, false
}
return false, true
}
type ReadHandle struct {
Params []interface{}
BoxInfo BoxInfo
Path BoxPath
ReadPayload func() (box IBox, n uint64, err error)
ReadData func(io.Writer) (n uint64, err error)
Expand func(params ...interface{}) (vals []interface{}, err error)
}
type ReadHandler func(handle *ReadHandle) (val interface{}, err error)
func ReadBoxStructure(r io.ReadSeeker, handler ReadHandler, params ...interface{}) ([]interface{}, error) {
if _, err := r.Seek(0, io.SeekStart); err != nil {
return nil, err
}
return readBoxStructure(r, 0, true, nil, Context{}, handler, params)
}
func ReadBoxStructureFromInternal(r io.ReadSeeker, bi *BoxInfo, handler ReadHandler, params ...interface{}) (interface{}, error) {
return readBoxStructureFromInternal(r, bi, nil, handler, params)
}
func readBoxStructureFromInternal(r io.ReadSeeker, bi *BoxInfo, path BoxPath, handler ReadHandler, params []interface{}) (interface{}, error) {
if _, err := bi.SeekToPayload(r); err != nil {
return nil, err
}
// check comatible-brands
if len(path) == 0 && bi.Type == BoxTypeFtyp() {
var ftyp Ftyp
if _, err := Unmarshal(r, bi.Size-bi.HeaderSize, &ftyp, bi.Context); err != nil {
return nil, err
}
if ftyp.HasCompatibleBrand(BrandQT()) {
bi.IsQuickTimeCompatible = true
}
if _, err := bi.SeekToPayload(r); err != nil {
return nil, err
}
}
// parse numbered ilst items after keys box by saving EntryCount field to context
if bi.Type == BoxTypeKeys() {
var keys Keys
if _, err := Unmarshal(r, bi.Size-bi.HeaderSize, &keys, bi.Context); err != nil {
return nil, err
}
bi.QuickTimeKeysMetaEntryCount = int(keys.EntryCount)
if _, err := bi.SeekToPayload(r); err != nil {
return nil, err
}
}
ctx := bi.Context
if bi.Type == BoxTypeWave() {
ctx.UnderWave = true
} else if bi.Type == BoxTypeIlst() {
ctx.UnderIlst = true
} else if bi.UnderIlst && !bi.UnderIlstMeta && IsIlstMetaBoxType(bi.Type) {
ctx.UnderIlstMeta = true
if bi.Type == StrToBoxType("----") {
ctx.UnderIlstFreeMeta = true
}
} else if bi.Type == BoxTypeUdta() {
ctx.UnderUdta = true
}
newPath := make(BoxPath, len(path)+1)
copy(newPath, path)
newPath[len(path)] = bi.Type
h := &ReadHandle{
Params: params,
BoxInfo: *bi,
Path: newPath,
}
var childrenOffset uint64
h.ReadPayload = func() (IBox, uint64, error) {
if _, err := bi.SeekToPayload(r); err != nil {
return nil, 0, err
}
box, n, err := UnmarshalAny(r, bi.Type, bi.Size-bi.HeaderSize, bi.Context)
if err != nil {
return nil, 0, err
}
childrenOffset = bi.Offset + bi.HeaderSize + n
return box, n, nil
}
h.ReadData = func(w io.Writer) (uint64, error) {
if _, err := bi.SeekToPayload(r); err != nil {
return 0, err
}
size := bi.Size - bi.HeaderSize
if _, err := io.CopyN(w, r, int64(size)); err != nil {
return 0, err
}
return size, nil
}
h.Expand = func(params ...interface{}) ([]interface{}, error) {
if childrenOffset == 0 {
if _, err := bi.SeekToPayload(r); err != nil {
return nil, err
}
_, n, err := UnmarshalAny(r, bi.Type, bi.Size-bi.HeaderSize, bi.Context)
if err != nil {
return nil, err
}
childrenOffset = bi.Offset + bi.HeaderSize + n
} else {
if _, err := r.Seek(int64(childrenOffset), io.SeekStart); err != nil {
return nil, err
}
}
childrenSize := bi.Offset + bi.Size - childrenOffset
return readBoxStructure(r, childrenSize, false, newPath, ctx, handler, params)
}
if val, err := handler(h); err != nil {
return nil, err
} else if _, err := bi.SeekToEnd(r); err != nil {
return nil, err
} else {
return val, nil
}
}
func readBoxStructure(r io.ReadSeeker, totalSize uint64, isRoot bool, path BoxPath, ctx Context, handler ReadHandler, params []interface{}) ([]interface{}, error) {
vals := make([]interface{}, 0, 8)
for isRoot || totalSize >= SmallHeaderSize {
bi, err := ReadBoxInfo(r)
if isRoot && err == io.EOF {
return vals, nil
} else if err != nil {
return nil, err
}
if !isRoot && bi.Size > totalSize {
return nil, fmt.Errorf("too large box size: type=%s, size=%d, actualBufSize=%d", bi.Type.String(), bi.Size, totalSize)
}
totalSize -= bi.Size
bi.Context = ctx
val, err := readBoxStructureFromInternal(r, bi, path, handler, params)
if err != nil {
return nil, err
}
vals = append(vals, val)
if bi.IsQuickTimeCompatible {
ctx.IsQuickTimeCompatible = true
}
// preserve keys entry count on context for subsequent ilst number item box
if bi.Type == BoxTypeKeys() {
ctx.QuickTimeKeysMetaEntryCount = bi.QuickTimeKeysMetaEntryCount
}
}
if totalSize != 0 && !ctx.IsQuickTimeCompatible {
return nil, errors.New("unexpected EOF")
}
return vals, nil
}