forked from Eyevinn/mp4ff
/
samplegroupentries.go
306 lines (267 loc) · 8.61 KB
/
samplegroupentries.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
298
299
300
301
302
303
304
305
306
package mp4
import (
"encoding/hex"
"fmt"
"io"
)
// SampleGroupEntry - like a box, but size and type are not in a header
type SampleGroupEntry interface {
// GroupingType SampleGroupEntry (uint32 according to spec)
Type() string // actually
// Size of SampleGroup Entry
Size() uint64
// Encode SampleGroupEntry to SliceWriter
Encode(sw *SliceWriter)
// Info - description of content.
Info(w io.Writer, specificBoxLevels, indent, indentStep string) (err error)
}
// SampleGroupEntryDecoder is function signature of the SampleGroupEntry Decode method
type SampleGroupEntryDecoder func(name string, length uint32, sr *SliceReader) (SampleGroupEntry, error)
var sgeDecoders map[string]SampleGroupEntryDecoder
func init() {
sgeDecoders = map[string]SampleGroupEntryDecoder{
"seig": DecodeSeigSampleGroupEntry,
"roll": DecodeRollSampleGroupEntry,
"rap ": DecodeRapSampleGroupEntry,
"alst": DecodeAlstSampleGroupEntry,
}
}
func decodeSampleGroupEntry(name string, length uint32, sr *SliceReader) (SampleGroupEntry, error) {
decode, ok := sgeDecoders[name]
if ok {
return decode(name, length, sr)
}
return DecodeUnknownSampleGroupEntry(name, length, sr)
}
// SeigSampleGroupEntry - CencSampleEncryptionInformationGroupEntry as defined in
// CEF ISO/IEC 23001-7 3rd edition 2016
type SeigSampleGroupEntry struct {
CryptByteBlock byte
SkipByteBlock byte
IsProtected byte
PerSampleIVSize byte
KID UUID
// ConstantIVSize byte given by len(ConstantIV)
ConstantIV []byte
}
// DecodeSeigSampleGroupEntry - decode Commone Encryption Sample Group Entry
func DecodeSeigSampleGroupEntry(name string, length uint32, sr *SliceReader) (SampleGroupEntry, error) {
s := &SeigSampleGroupEntry{}
_ = sr.ReadUint8() // Reserved
byteTwo := sr.ReadUint8()
s.CryptByteBlock = byteTwo >> 4
s.SkipByteBlock = byteTwo % 0xf
s.IsProtected = sr.ReadUint8()
s.PerSampleIVSize = sr.ReadUint8()
s.KID = UUID(sr.ReadBytes(16))
if s.IsProtected == 1 && s.PerSampleIVSize == 0 {
constantIVSize := int(sr.ReadUint8())
s.ConstantIV = sr.ReadBytes(constantIVSize)
}
if length != uint32(s.Size()) {
return nil, fmt.Errorf("seig: given length %d different from calculated size %d", length, s.Size())
}
return s, nil
}
func (s *SeigSampleGroupEntry) Type() string {
return "seig"
}
func (s *SeigSampleGroupEntry) Size() uint64 {
// reserved: 1
// cryptByteBlock + SkipByteBlock : 1
// isProtected: 1
// perSampleIVSize: 1
// KID: 16
size := 20
if s.IsProtected == 1 && s.PerSampleIVSize == 0 {
size += 1 + len(s.ConstantIV)
}
return uint64(size)
}
func (s *SeigSampleGroupEntry) Encode(sw *SliceWriter) {
sw.WriteUint8(0) // Reserved
byteTwo := s.CryptByteBlock<<4 | s.SkipByteBlock
sw.WriteUint8(byteTwo)
sw.WriteUint8(s.IsProtected)
sw.WriteUint8(s.PerSampleIVSize)
sw.WriteBytes(s.KID)
if s.IsProtected == 1 && s.PerSampleIVSize == 0 {
sw.WriteUint8(byte(len(s.ConstantIV)))
sw.WriteBytes(s.ConstantIV)
}
}
// Info - write box info to w
func (s *SeigSampleGroupEntry) Info(w io.Writer, specificBoxLevels, indent, indentStep string) (err error) {
bd := newInfoDumper(w, indent, s, -2, 0)
bd.write(" * cryptByteBlock: %d", s.CryptByteBlock)
bd.write(" * skipByteBlock: %d", s.SkipByteBlock)
bd.write(" * isProtected: %d", s.IsProtected)
bd.write(" * perSampleIVSize: %d", s.PerSampleIVSize)
bd.write(" * KID: %s", s.KID)
if s.IsProtected == 1 && s.PerSampleIVSize == 0 {
bd.write(" * constantIV: %s", hex.EncodeToString(s.ConstantIV))
}
return bd.err
}
// Unknown or not implemented SampleGroupEntry
type UnknownSampleGroupEntry struct {
Name string
Length uint32
Data []byte
}
func DecodeUnknownSampleGroupEntry(name string, length uint32, sr *SliceReader) (SampleGroupEntry, error) {
return &UnknownSampleGroupEntry{
Name: name,
Data: sr.ReadBytes(int(length)),
}, nil
}
func (s *UnknownSampleGroupEntry) Type() string {
return s.Name
}
func (s *UnknownSampleGroupEntry) Size() uint64 {
return uint64(len(s.Data))
}
func (s *UnknownSampleGroupEntry) Encode(sw *SliceWriter) {
sw.WriteBytes(s.Data)
}
// Info - write box info to w
func (s *UnknownSampleGroupEntry) Info(w io.Writer, specificBoxLevels, indent, indentStep string) (err error) {
bd := newInfoDumper(w, indent, s, -2, 0)
bd.write(" * Unknown data of length: %d", len(s.Data))
level := getInfoLevel(s, specificBoxLevels)
if level > 0 {
bd.write(" * data: %s", hex.EncodeToString(s.Data))
}
return bd.err
}
// RollSampleGroupEntry - Gradual Decoding Refresh "roll"
//
// ISO/IEC 14496-12 Ed. 6 2020 Section 10.1
//
// VisualRollRecoveryEntry / AudioRollRecoveryEntry / AudioPreRollEntry
type RollSampleGroupEntry struct {
RollDistance int16
}
func DecodeRollSampleGroupEntry(name string, length uint32, sr *SliceReader) (SampleGroupEntry, error) {
entry := &RollSampleGroupEntry{}
entry.RollDistance = sr.ReadInt16()
return entry, nil
}
func (s *RollSampleGroupEntry) Type() string {
return "roll"
}
func (s *RollSampleGroupEntry) Size() uint64 {
return 2
}
func (s *RollSampleGroupEntry) Encode(sw *SliceWriter) {
sw.WriteInt16(s.RollDistance)
}
// Info - write box info to w
func (s *RollSampleGroupEntry) Info(w io.Writer, specificBoxLevels, indent, indentStep string) (err error) {
bd := newInfoDumper(w, indent, s, -2, 0)
bd.write(" * rollDistance: %d", s.RollDistance)
return bd.err
}
// RapSampleGroupEntry - Random Access Point "rap "
//
// ISO/IEC 14496-12 Ed. 6 2020 Section 10.4 - VisualRandomAccessEntry
type RapSampleGroupEntry struct {
NumLeadingSamplesKnown uint8
NumLeadingSamples uint8
}
func DecodeRapSampleGroupEntry(name string, length uint32, sr *SliceReader) (SampleGroupEntry, error) {
entry := &RapSampleGroupEntry{}
byt := sr.ReadUint8()
entry.NumLeadingSamplesKnown = byt >> 7
entry.NumLeadingSamples = byt & 0x7F
return entry, nil
}
func (s *RapSampleGroupEntry) Type() string {
return "rap "
}
func (s *RapSampleGroupEntry) Size() uint64 {
return 1
}
func (s *RapSampleGroupEntry) Encode(sw *SliceWriter) {
var byt uint8
byt |= (s.NumLeadingSamplesKnown << 7)
byt |= (s.NumLeadingSamples)
sw.WriteUint8(byt)
}
// Info - write box info to w
func (s *RapSampleGroupEntry) Info(w io.Writer, specificBoxLevels, indent, indentStep string) (err error) {
bd := newInfoDumper(w, indent, s, -2, 0)
bd.write(" * numLeadingSamplesKnown: %d", s.NumLeadingSamplesKnown)
bd.write(" * numLeadingSamples: %d", s.NumLeadingSamples)
return bd.err
}
// AlstSampleGroupEntry - Alternative Startup Entry "alst"
//
// ISO/IEC 14496-12 Ed. 6 2020 Section 10.3 - AlternativeStartupEntry
type AlstSampleGroupEntry struct {
RollCount uint16
FirstOutputSample uint16
SampleOffset []uint32
NumOutputSamples []uint16
NumTotalSamples []uint16
}
func (s *AlstSampleGroupEntry) Type() string {
return "alst "
}
func (s *AlstSampleGroupEntry) Size() uint64 {
// RollCount: 2
// FirstOutputSample: 2
// SampleOffset: 4 * count
// NumOutputSamples: 2 * count
// NumTotalSamples: 2 * count
return uint64(4 + 4*len(s.SampleOffset) + 2*len(s.NumOutputSamples) + 2*len(s.NumTotalSamples))
}
func DecodeAlstSampleGroupEntry(name string, length uint32, sr *SliceReader) (SampleGroupEntry, error) {
entry := &AlstSampleGroupEntry{}
entry.RollCount = sr.ReadUint16()
entry.FirstOutputSample = sr.ReadUint16()
entry.SampleOffset = make([]uint32, int(entry.RollCount))
for i := 0; i < int(entry.RollCount); i++ {
entry.SampleOffset[i] = sr.ReadUint32()
}
remaining := int(length-uint32(entry.Size())) / 4
if remaining <= 0 {
return entry, nil
}
// Optional
entry.NumOutputSamples = make([]uint16, remaining)
entry.NumTotalSamples = make([]uint16, remaining)
for i := 0; i < remaining; i++ {
entry.NumOutputSamples[i] = sr.ReadUint16()
entry.NumTotalSamples[i] = sr.ReadUint16()
}
return entry, nil
}
func (s *AlstSampleGroupEntry) Encode(sw *SliceWriter) {
sw.WriteUint16(s.RollCount)
sw.WriteUint16(s.FirstOutputSample)
for _, offset := range s.SampleOffset {
sw.WriteUint32(offset)
}
for i := range s.NumOutputSamples {
sw.WriteUint16(s.NumOutputSamples[i])
sw.WriteUint16(s.NumTotalSamples[i])
}
}
// Info - write box info to w
func (s *AlstSampleGroupEntry) Info(w io.Writer, specificBoxLevels, indent, indentStep string) (err error) {
bd := newInfoDumper(w, indent, s, -2, 0)
bd.write(" * rollDistance: %d", s.RollCount)
bd.write(" * firstOutputSample: %d", s.FirstOutputSample)
level := getInfoLevel(s, specificBoxLevels)
if level > 0 {
for i, offset := range s.SampleOffset {
bd.write(" * sampleOffset[%d]: %d", i+1, offset)
}
for i := range s.NumOutputSamples {
bd.write(" * numOutputSamples[%d]: %d", i+1, s.NumOutputSamples[i])
bd.write(" * numTotalSamples[%d]: %d", i+1, s.NumTotalSamples[i])
}
}
return bd.err
}