forked from godeep/mp4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stbl.go
137 lines (130 loc) · 2.25 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
package mp4
import (
"encoding/binary"
"io"
)
// Soample 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 {
Stts *SttsBox
Stss *StssBox
Stsc *StscBox
Stsz *StszBox
Stco *StcoBox
Ctts *CttsBox
boxes []Box
header [8]byte
}
func DecodeStbl(r io.Reader) (Box, error) {
l, err := DecodeContainer(r)
if err != nil {
return nil, err
}
s := &StblBox{
boxes: make([]Box, 0, len(l)),
}
for _, b := range l {
switch b.Type() {
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)
default:
s.boxes = append(s.boxes, b)
}
}
return s, nil
}
func (b *StblBox) Type() string {
return "stbl"
}
func (b *StblBox) Size() (sz int) {
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()
}
for _, box := range b.boxes {
sz += box.Size()
}
return sz + BoxHeaderSize
}
func (b *StblBox) Dump() {
if b.Stsc != nil {
b.Stsc.Dump()
}
if b.Stts != nil {
b.Stts.Dump()
}
if b.Stss != nil {
b.Stss.Dump()
}
if b.Stco != nil {
b.Stco.Dump()
}
}
func (b *StblBox) Encode(w io.Writer) error {
binary.BigEndian.PutUint32(b.header[:4], uint32(b.Size()))
copy(b.header[4:], b.Type())
_, err := w.Write(b.header[:])
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
}
for _, b := range b.boxes {
if err = b.Encode(w); err != nil {
return err
}
}
if b.Ctts != nil {
return b.Ctts.Encode(w)
}
return nil
}