forked from godeep/mp4
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstbl.go
129 lines (122 loc) · 2.04 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
package mp4
import "io"
// 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
Ctts *CttsBox
}
func DecodeStbl(r io.Reader) (Box, error) {
l, err := DecodeContainer(r)
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 "ctts":
s.Ctts = b.(*CttsBox)
}
}
return s, nil
}
func (b *StblBox) Type() string {
return "stbl"
}
func (b *StblBox) Size() int {
sz := b.Stsd.Size()
if b.Stts != nil {
sz += b.Stts.Size()
}
if b.Stss != nil {
sz += b.Stss.Size()
}
if b.Stsc != nil {
sz += b.Stsc.Size()
}
if b.Stsz != nil {
sz += b.Stsz.Size()
}
if b.Stco != nil {
sz += b.Stco.Size()
}
if b.Ctts != nil {
sz += b.Ctts.Size()
}
return sz + BoxHeaderSize
}
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()
}
}
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
}
err = b.Stco.Encode(w)
if err != nil {
return err
}
if b.Ctts != nil {
return b.Ctts.Encode(w)
}
return nil
}