-
Notifications
You must be signed in to change notification settings - Fork 84
/
boxsr.go
268 lines (250 loc) · 7.36 KB
/
boxsr.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
package mp4
import (
"fmt"
"github.com/Eyevinn/mp4ff/bits"
)
var decodersSR map[string]BoxDecoderSR
func init() {
decodersSR = map[string]BoxDecoderSR{
"ac-3": DecodeAudioSampleEntrySR,
"av01": DecodeVisualSampleEntrySR,
"avc1": DecodeVisualSampleEntrySR,
"avc3": DecodeVisualSampleEntrySR,
"alou": DecodeAlouBoxSR,
"avcC": DecodeAvcCSR,
"av1C": DecodeAv1CSR,
"btrt": DecodeBtrtSR,
"cdat": DecodeCdatSR,
"cdsc": DecodeTrefTypeSR,
"clap": DecodeClapSR,
"cslg": DecodeCslgSR,
"co64": DecodeCo64SR,
"colr": DecodeColrSR,
"ctim": DecodeCtimSR,
"ctts": DecodeCttsSR,
"dac3": DecodeDac3SR,
"data": DecodeDataSR,
"dec3": DecodeDec3SR,
"desc": DecodeGenericContainerBoxSR,
"dinf": DecodeDinfSR,
"dpnd": DecodeTrefTypeSR,
"dref": DecodeDrefSR,
"ec-3": DecodeAudioSampleEntrySR,
"elng": DecodeElngSR,
"emeb": DecodeEmebSR,
"emib": DecodeEmibSR,
"esds": DecodeEsdsSR,
"edts": DecodeEdtsSR,
"elst": DecodeElstSR,
"enca": DecodeAudioSampleEntrySR,
"encv": DecodeVisualSampleEntrySR,
"emsg": DecodeEmsgSR,
"evte": DecodeEvteSR,
"font": DecodeTrefTypeSR,
"free": DecodeFreeSR,
"frma": DecodeFrmaSR,
"ftyp": DecodeFtypSR,
"hdlr": DecodeHdlrSR,
"hev1": DecodeVisualSampleEntrySR,
"hind": DecodeTrefTypeSR,
"hint": DecodeTrefTypeSR,
"hvcC": DecodeHvcCSR,
"hvc1": DecodeVisualSampleEntrySR,
"iden": DecodeIdenSR,
"ilst": DecodeIlstSR,
"iods": DecodeUnknownSR,
"ipir": DecodeTrefTypeSR,
"kind": DecodeKindSR,
"leva": DecodeLevaSR,
"ludt": DecodeLudtSR,
"mdat": DecodeMdatSR,
"mehd": DecodeMehdSR,
"mdhd": DecodeMdhdSR,
"mdia": DecodeMdiaSR,
"meta": DecodeMetaSR,
"mfhd": DecodeMfhdSR,
"mfra": DecodeMfraSR,
"mfro": DecodeMfroSR,
"mime": DecodeMimeSR,
"minf": DecodeMinfSR,
"moof": DecodeMoofSR,
"moov": DecodeMoovSR,
"mpod": DecodeTrefTypeSR,
"mvex": DecodeMvexSR,
"mvhd": DecodeMvhdSR,
"mp4a": DecodeAudioSampleEntrySR,
"nmhd": DecodeNmhdSR,
"pasp": DecodePaspSR,
"payl": DecodePaylSR,
"prft": DecodePrftSR,
"pssh": DecodePsshSR,
"saio": DecodeSaioSR,
"saiz": DecodeSaizSR,
"sbgp": DecodeSbgpSR,
"schi": DecodeSchiSR,
"schm": DecodeSchmSR,
"sdtp": DecodeSdtpSR,
"senc": DecodeSencSR,
"sgpd": DecodeSgpdSR,
"sidx": DecodeSidxSR,
"silb": DecodeSilbSR,
"sinf": DecodeSinfSR,
"skip": DecodeFreeSR,
"smhd": DecodeSmhdSR,
"ssix": DecodeSsixSR,
"sthd": DecodeSthdSR,
"stbl": DecodeStblSR,
"stco": DecodeStcoSR,
"stpp": DecodeStppSR,
"stsc": DecodeStscSR,
"stsd": DecodeStsdSR,
"stss": DecodeStssSR,
"stsz": DecodeStszSR,
"sttg": DecodeSttgSR,
"stts": DecodeSttsSR,
"styp": DecodeStypSR,
"subs": DecodeSubsSR,
"subt": DecodeTrefTypeSR,
"sync": DecodeTrefTypeSR,
"tenc": DecodeTencSR,
"tfdt": DecodeTfdtSR,
"tfhd": DecodeTfhdSR,
"tfra": DecodeTfraSR,
"tkhd": DecodeTkhdSR,
"tlou": DecodeTlouSR,
"traf": DecodeTrafSR,
"trak": DecodeTrakSR,
"tref": DecodeTrefSR,
"trep": DecodeTrepSR,
"trex": DecodeTrexSR,
"trun": DecodeTrunSR,
"udta": DecodeUdtaSR,
"url ": DecodeURLBoxSR,
"uuid": DecodeUUIDBoxSR,
"vdep": DecodeTrefTypeSR,
"vlab": DecodeVlabSR,
"vmhd": DecodeVmhdSR,
"vplx": DecodeTrefTypeSR,
"vsid": DecodeVsidSR,
"vtta": DecodeVttaSR,
"vttc": DecodeVttcSR,
"vttC": DecodeVttCSR,
"vtte": DecodeVtteSR,
"wvtt": DecodeWvttSR,
"\xa9cpy": DecodeGenericContainerBoxSR,
"\xa9nam": DecodeGenericContainerBoxSR,
"\xa9too": DecodeGenericContainerBoxSR,
"\xa9ART": DecodeGenericContainerBoxSR,
}
}
// BoxDecoderSR is function signature of the Box DecodeSR method
type BoxDecoderSR func(hdr BoxHeader, startPos uint64, sw bits.SliceReader) (Box, error)
// DecodeBoxSR - decode a box from SliceReader
func DecodeBoxSR(startPos uint64, sr bits.SliceReader) (Box, error) {
var err error
var b Box
h, err := DecodeHeaderSR(sr)
if err != nil {
return nil, err
}
maxSize := uint64(sr.NrRemainingBytes()) + uint64(h.Hdrlen)
// In the following, we do not block mdat to allow for the case
// that the first kiloBytes of a file are fetched and parsed to
// get the init part of a file. In the future, a new decode option that
// stops before the mdat starts is a better alternative.
if h.Size > maxSize && h.Name != "mdat" {
return nil, fmt.Errorf("decode box %q, size %d too big (max %d)", h.Name, h.Size, maxSize)
}
d, ok := decodersSR[h.Name]
if !ok {
b, err = DecodeUnknownSR(h, startPos, sr)
} else {
b, err = d(h, startPos, sr)
}
if err != nil {
return nil, fmt.Errorf("decode %s pos %d: %w", h.Name, startPos, err)
}
return b, nil
}
// DecodeHeaderSR - decode a box header (size + box type + possible largeSize) from sr
func DecodeHeaderSR(sr bits.SliceReader) (BoxHeader, error) {
size := uint64(sr.ReadUint32())
boxType := sr.ReadFixedLengthString(4)
headerLen := boxHeaderSize
if size == 1 {
size = sr.ReadUint64()
headerLen += largeSizeLen
} else if size == 0 {
return BoxHeader{}, fmt.Errorf("Size 0, meaning to end of file, not supported")
}
return BoxHeader{boxType, size, headerLen}, nil
}
// DecodeFile - parse and decode a file from reader r with optional file options.
// For example, the file options overwrite the default decode or encode mode.
func DecodeFileSR(sr bits.SliceReader, options ...Option) (*File, error) {
f := NewFile()
// apply options to change the default decode or encode mode
f.ApplyOptions(options...)
var boxStartPos uint64 = 0
lastBoxType := ""
if f.fileDecMode == DecModeLazyMdat {
return nil, fmt.Errorf("no support for lazy mdat in DecodeFileSR")
}
LoopBoxes:
for {
var box Box
var err error
if sr.NrRemainingBytes() == 0 {
break LoopBoxes
}
box, err = DecodeBoxSR(boxStartPos, sr)
if err != nil {
return nil, err
}
boxType, boxSize := box.Type(), box.Size()
switch boxType {
case "mdat":
if f.isFragmented {
if lastBoxType != "moof" {
return nil, fmt.Errorf("does not support %v between moof and mdat", lastBoxType)
}
} else {
if f.Mdat != nil {
oldPayloadSize := f.Mdat.Size() - f.Mdat.HeaderSize()
newMdat := box.(*MdatBox)
newPayloadSize := newMdat.Size() - newMdat.HeaderSize()
if oldPayloadSize > 0 && newPayloadSize > 0 {
return nil, fmt.Errorf("only one non-empty mdat box supported (payload sizes %d and %d)",
oldPayloadSize, newPayloadSize)
}
}
}
case "moof":
moof := box.(*MoofBox)
for _, traf := range moof.Trafs {
if ok, parsed := traf.ContainsSencBox(); ok && !parsed {
isEncrypted := true
defaultIVSize := byte(0) // Should get this from tenc in sinf
if f.Moov != nil {
trackID := traf.Tfhd.TrackID
isEncrypted = f.Moov.IsEncrypted(trackID)
sinf := f.Moov.GetSinf(trackID)
if sinf != nil && sinf.Schi != nil && sinf.Schi.Tenc != nil {
defaultIVSize = sinf.Schi.Tenc.DefaultPerSampleIVSize
}
}
if isEncrypted {
err = traf.ParseReadSenc(defaultIVSize, moof.StartPos)
if err != nil {
return nil, err
}
}
}
}
}
f.AddChild(box, boxStartPos)
lastBoxType = boxType
boxStartPos += boxSize
}
return f, nil
}