/
traf.go
301 lines (279 loc) · 7.52 KB
/
traf.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
package mp4
import (
"errors"
"fmt"
"io"
"github.com/Eyevinn/mp4ff/bits"
)
// TrafBox - Track Fragment Box (traf)
//
// Contained in : Movie Fragment Box (moof)
type TrafBox struct {
Tfhd *TfhdBox
Tfdt *TfdtBox
Saiz *SaizBox
Saio *SaioBox
Sbgp *SbgpBox
Sgpd *SgpdBox
Senc *SencBox
UUIDSenc *UUIDBox // A PIFF box of subtype senc
Trun *TrunBox // The first TrunBox
Truns []*TrunBox
Children []Box
}
// DecodeTraf - box-specific decode
func DecodeTraf(hdr BoxHeader, startPos uint64, r io.Reader) (Box, error) {
children, err := DecodeContainerChildren(hdr, startPos+8, startPos+hdr.Size, r)
if err != nil {
return nil, err
}
t := &TrafBox{Children: make([]Box, 0, len(children))}
for _, child := range children {
err := t.AddChild(child)
if err != nil {
return nil, err
}
}
return t, nil
}
// DecodeTrafSR - box-specific decode
func DecodeTrafSR(hdr BoxHeader, startPos uint64, sr bits.SliceReader) (Box, error) {
children, err := DecodeContainerChildrenSR(hdr, startPos+8, startPos+hdr.Size, sr)
if err != nil {
return nil, err
}
t := &TrafBox{Children: make([]Box, 0, len(children))}
for _, child := range children {
err := t.AddChild(child)
if err != nil {
return nil, err
}
}
return t, nil
}
// ContainsSencBox - is there a senc box in traf and is it parsed
// If not parsed, call ParseReadSenc to parse it
func (t *TrafBox) ContainsSencBox() (ok, parsed bool) {
for _, c := range t.Children {
switch box := c.(type) {
case *SencBox:
return true, !box.readButNotParsed
case *UUIDBox: // PIFF
if box.SubType() == "senc" {
return true, !box.Senc.readButNotParsed
}
}
}
return false, false
}
// ParseReadSenc makes a second round to parse a senc box previously read
func (t *TrafBox) ParseReadSenc(defaultIVSize byte, moofStartPos uint64) error {
if t.Senc == nil && t.UUIDSenc == nil {
return fmt.Errorf("no senc box or uuid senc box")
}
var senc *SencBox
if t.Senc != nil {
senc = t.Senc
} else {
senc = t.UUIDSenc.Senc
}
if t.Saio != nil {
// saio should be present, but we try without it, if it doesn't exist
posFromSaio := t.Saio.Offset[0] + int64(moofStartPos)
if uint64(posFromSaio) != senc.StartPos+16 {
//TODO. Reenable
return fmt.Errorf("offset from saio (%d) relative moof start differs from senc data start %d", posFromSaio, senc.StartPos+16)
//fmt.Printf("offset from saio (%d) and moof differs from senc data start %d\n", posFromSaio, senc.StartPos+16)
}
}
perSampleIVSize := defaultIVSize
if t.Sbgp != nil && t.Sgpd != nil {
sbgp, sgpd := t.Sbgp, t.Sgpd
if sbgp.GroupingType != "seig" {
return fmt.Errorf("sbgp grouping type %s not supported", sbgp.GroupingType)
}
nrSbgpEntries := len(sbgp.SampleCounts)
if nrSbgpEntries != 1 {
return fmt.Errorf("sbgp entries = %d, only 1 supported for now", nrSbgpEntries)
}
sgpdEntryNr := sbgp.GroupDescriptionIndices[0]
if sgpdEntryNr != sbgpInsideOffset+1 {
return fmt.Errorf("sgpd entry number must be first inside = 65536 + 1")
}
if sgpd.GroupingType != "seig" {
return fmt.Errorf("sgpd grouping type %s not supported", sgpd.GroupingType)
}
sgpdEntry := sgpd.SampleGroupEntries[sgpdEntryNr-sbgpInsideOffset-1]
if sgpdEntry.Type() != "seig" {
return fmt.Errorf("expected sgpd entry type seig but found %q", sgpdEntry.Type())
}
seigEntry := sgpdEntry.(*SeigSampleGroupEntry)
perSampleIVSize = seigEntry.PerSampleIVSize
}
err := senc.ParseReadBox(perSampleIVSize, t.Saiz)
if err != nil {
return err
}
return nil
}
// AddChild - add child box
func (t *TrafBox) AddChild(child Box) error {
switch box := child.(type) {
case *TfhdBox:
t.Tfhd = box
case *TfdtBox:
t.Tfdt = box
case *SaizBox:
t.Saiz = box
case *SaioBox:
t.Saio = box
case *SbgpBox:
t.Sbgp = box
case *SgpdBox:
t.Sgpd = box
case *SencBox:
t.Senc = box
case *TrunBox:
if t.Trun == nil {
t.Trun = box
}
t.Truns = append(t.Truns, box)
case *UUIDBox:
if box.SubType() == "senc" {
t.UUIDSenc = box
}
default:
}
t.Children = append(t.Children, child)
return nil
}
// Type - return box type
func (t *TrafBox) Type() string {
return "traf"
}
// Size - return calculated size
func (t *TrafBox) Size() uint64 {
return containerSize(t.Children)
}
// GetChildren - list of child boxes
func (t *TrafBox) GetChildren() []Box {
return t.Children
}
// Encode - write box to w
func (t *TrafBox) Encode(w io.Writer) error {
return EncodeContainer(t, w)
}
// Encode - write minf container to sw
func (b *TrafBox) EncodeSW(sw bits.SliceWriter) error {
return EncodeContainerSW(b, sw)
}
// Info - write box-specific information
func (t *TrafBox) Info(w io.Writer, specificBoxLevels, indent, indentStep string) error {
return ContainerInfo(t, w, specificBoxLevels, indent, indentStep)
}
// OptimizeTfhdTrun - optimize trun by default values in tfhd box
// Only look at first trun, even if there is more than one
// Don't optimize again, if already done so that no data is present
func (t *TrafBox) OptimizeTfhdTrun() error {
tfhd := t.Tfhd
trun := t.Trun
if len(trun.Samples) == 0 {
return errors.New("no samples in trun")
}
if len(trun.Samples) == 1 {
return nil // No need to optimize
}
if trun.HasSampleDuration() {
hasCommonDur := true
commonDur := trun.Samples[0].Dur
for _, s := range trun.Samples {
if s.Dur != commonDur {
hasCommonDur = false
break
}
}
if hasCommonDur {
// Set defaultSampleDuration in tfhd and remove from trun
tfhd.Flags = tfhd.Flags | defaultSampleDurationPresent
tfhd.DefaultSampleDuration = commonDur
trun.Flags = trun.Flags & ^TrunSampleDurationPresentFlag
}
}
if trun.HasSampleSize() {
hasCommonSize := true
commonSize := trun.Samples[0].Size
for _, s := range trun.Samples {
if s.Size != commonSize {
hasCommonSize = false
break
}
}
if hasCommonSize {
// Set defaultSampleSize in tfhd and remove from trun
tfhd.Flags = tfhd.Flags | defaultSampleSizePresent
tfhd.DefaultSampleSize = commonSize
trun.Flags = trun.Flags & ^TrunSampleSizePresentFlag
}
}
if trun.HasSampleFlags() {
firstSampleFlags := trun.Samples[0].Flags
hasCommonFlags := true
commonSampleFlags := trun.Samples[1].Flags
for i, s := range trun.Samples {
if i >= 1 {
if s.Flags != commonSampleFlags {
hasCommonFlags = false
break
}
}
}
if hasCommonFlags {
if firstSampleFlags != commonSampleFlags {
trun.SetFirstSampleFlags(firstSampleFlags)
}
tfhd.Flags = tfhd.Flags | defaultSampleFlagsPresent
tfhd.DefaultSampleFlags = commonSampleFlags
trun.Flags = trun.Flags & ^TrunSampleFlagsPresentFlag
}
}
if trun.HasSampleCompositionTimeOffset() {
allZeroCTO := true
for _, s := range trun.Samples {
if s.CompositionTimeOffset != 0 {
allZeroCTO = false
break
}
}
if allZeroCTO {
trun.Flags = trun.Flags & ^TrunSampleCompositionTimeOffsetPresentFlag
}
}
return nil
}
// RemoveEncryptionBoxes - remove encryption boxes and return number of bytes removed
func (t *TrafBox) RemoveEncryptionBoxes() uint64 {
remainingChildren := make([]Box, 0, len(t.Children))
var nrBytesRemoved uint64 = 0
for _, ch := range t.Children {
switch box := ch.(type) {
case *SaizBox:
nrBytesRemoved += ch.Size()
t.Saiz = nil
case *SaioBox:
nrBytesRemoved += ch.Size()
t.Saio = nil
case *SencBox:
nrBytesRemoved += ch.Size()
t.Senc = nil
case *UUIDBox:
if box.SubType() == "senc" {
nrBytesRemoved += ch.Size()
t.UUIDSenc = nil
}
default:
remainingChildren = append(remainingChildren, ch)
}
}
t.Children = remainingChildren
return nrBytesRemoved
}