forked from mstoykov/mp4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stbl.go
150 lines (143 loc) · 2.5 KB
/
stbl.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
package mp4
import "io"
// StblBox - Sample Table Box (stbl - mandatory)
//
// Contained in : Media Information Box (minf)
//
// Status: partially decoded (anything other than stsd, stts, stsc, stss, stsz, stco, ctts is ignored)
//
// The table contains all information relevant to data samples (times, chunks, sizes, ...)
type StblBox struct {
Stsd *StsdBox
Stts *SttsBox
Stss *StssBox
Stsc *StscBox
Stsz *StszBox
Stco *StcoBox
Co64 *Co64Box
Ctts *CttsBox
}
// DecodeStbl decodes stbl
func DecodeStbl(r io.Reader, size uint64) (Box, error) {
l, err := DecodeContainer(r, size)
if err != nil {
return nil, err
}
s := &StblBox{}
for _, b := range l {
switch b.Type() {
case "stsd":
s.Stsd = b.(*StsdBox)
case "stts":
s.Stts = b.(*SttsBox)
case "stsc":
s.Stsc = b.(*StscBox)
case "stss":
s.Stss = b.(*StssBox)
case "stsz":
s.Stsz = b.(*StszBox)
case "stco":
s.Stco = b.(*StcoBox)
case "co64":
s.Co64 = b.(*Co64Box)
case "ctts":
s.Ctts = b.(*CttsBox)
}
}
return s, nil
}
// Type returns stbl
func (b *StblBox) Type() string {
return "stbl"
}
// Size returns size
func (b *StblBox) Size() uint64 {
sz := AddHeaderSize(b.Stsd.Size())
if b.Stts != nil {
sz += AddHeaderSize(b.Stts.Size())
}
if b.Stss != nil {
sz += AddHeaderSize(b.Stss.Size())
}
if b.Stsc != nil {
sz += AddHeaderSize(b.Stsc.Size())
}
if b.Stsz != nil {
sz += AddHeaderSize(b.Stsz.Size())
}
if b.Stco != nil {
sz += AddHeaderSize(b.Stco.Size())
}
if b.Co64 != nil {
sz += AddHeaderSize(b.Co64.Size())
}
if b.Ctts != nil {
sz += AddHeaderSize(b.Ctts.Size())
}
return sz
}
// Dump dumps
func (b *StblBox) Dump() {
if b.Stsc != nil {
b.Stsc.Dump()
}
if b.Stts != nil {
b.Stts.Dump()
}
if b.Stsz != nil {
b.Stsz.Dump()
}
if b.Stss != nil {
b.Stss.Dump()
}
if b.Stco != nil {
b.Stco.Dump()
}
if b.Co64 != nil {
b.Co64.Dump()
}
}
// Encode encodes
func (b *StblBox) Encode(w io.Writer) error {
err := EncodeHeader(b, w)
if err != nil {
return err
}
err = b.Stsd.Encode(w)
if err != nil {
return err
}
err = b.Stts.Encode(w)
if err != nil {
return err
}
if b.Stss != nil {
err = b.Stss.Encode(w)
if err != nil {
return err
}
}
err = b.Stsc.Encode(w)
if err != nil {
return err
}
err = b.Stsz.Encode(w)
if err != nil {
return err
}
if b.Stco != nil {
err = b.Stco.Encode(w)
if err != nil {
return err
}
} else {
err = b.Co64.Encode(w)
if err != nil {
return err
}
}
if b.Ctts != nil {
return b.Ctts.Encode(w)
}
return nil
}