-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathcontainer.go
197 lines (177 loc) · 4.86 KB
/
container.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
package mp4
import (
"fmt"
"io"
"github.com/Eyevinn/mp4ff/bits"
)
// ContainerBox is interface for ContainerBoxes
type ContainerBox interface {
Type() string
Size() uint64
Encode(w io.Writer) error
EncodeSW(w bits.SliceWriter) error
GetChildren() []Box
Info(w io.Writer, specificBoxLevels, indent, indentStep string) error
}
// GenericContainerBox is a generic container box with no special child pointers
type GenericContainerBox struct {
name string
Children []Box
}
func NewGenericContainerBox(name string) *GenericContainerBox {
return &GenericContainerBox{name: name}
}
func (b *GenericContainerBox) Type() string {
return b.name
}
func (b *GenericContainerBox) Size() uint64 {
return containerSize(b.Children)
}
// Encode - write GenericContainerBox to w
func (b *GenericContainerBox) Encode(w io.Writer) error {
return EncodeContainer(b, w)
}
// Encode - write minf container to sw
func (b *GenericContainerBox) EncodeSW(sw bits.SliceWriter) error {
return EncodeContainerSW(b, sw)
}
// Info - write box-specific information
func (b *GenericContainerBox) Info(w io.Writer, specificBoxLevels, indent, indentStep string) error {
return ContainerInfo(b, w, specificBoxLevels, indent, indentStep)
}
// GetChildren - list of child boxes
func (b *GenericContainerBox) GetChildren() []Box {
return b.Children
}
// DecodeGenericContainerBox - box-specific decode
func DecodeGenericContainerBox(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
}
m := NewGenericContainerBox(hdr.Name)
for _, c := range children {
m.AddChild(c)
}
return m, nil
}
// DecodeGenericContainerBoxSR - box-specific decode
func DecodeGenericContainerBoxSR(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
}
m := NewGenericContainerBox(hdr.Name)
for _, c := range children {
m.AddChild(c)
}
return m, nil
}
// AddChild - Add a child box
func (b *GenericContainerBox) AddChild(child Box) {
b.Children = append(b.Children, child)
}
func containerSize(children []Box) uint64 {
var contentSize uint64 = 0
for _, child := range children {
contentSize += child.Size()
}
return boxHeaderSize + contentSize
}
// DecodeContainerChildren decodes a container box
func DecodeContainerChildren(hdr BoxHeader, startPos, endPos uint64, r io.Reader) ([]Box, error) {
children := make([]Box, 0, 8)
pos := startPos
for {
child, err := DecodeBox(pos, r)
if err == io.EOF {
return children, nil
}
if err != nil {
return children, err
}
children = append(children, child)
pos += child.Size()
if pos == endPos {
return children, nil
} else if pos > endPos {
msg := ""
for _, c := range children {
msg += fmt.Sprintf("%s:%d ", c.Type(), c.Size())
}
return nil, fmt.Errorf("non-matching children box sizes, parentSize=%d, %s", endPos-startPos, msg)
}
}
}
// DecodeContainerChildren decodes a container box
func DecodeContainerChildrenSR(hdr BoxHeader, startPos, endPos uint64, sr bits.SliceReader) ([]Box, error) {
children := make([]Box, 0, 8) // Good initial size
pos := startPos
initPos := sr.GetPos()
for {
if pos > endPos {
msg := ""
for _, c := range children {
msg += fmt.Sprintf("%s:%d ", c.Type(), c.Size())
}
return nil, fmt.Errorf("non-matching children box sizes, parentSize=%d, %s", endPos-startPos, msg)
}
if pos == endPos {
break
}
child, err := DecodeBoxSR(pos, sr)
if err != nil {
return children, err
}
children = append(children, child)
pos += child.Size()
relPosFromSize := sr.GetPos() - initPos
if int(pos-startPos) != relPosFromSize {
return nil, fmt.Errorf("child %s size mismatch in %s: %d - %d", child.Type(), hdr.Name, pos-startPos, relPosFromSize)
}
}
return children, nil
}
// EncodeContainer - marshal container c to w
func EncodeContainer(c ContainerBox, w io.Writer) error {
err := EncodeHeader(c, w)
if err != nil {
return err
}
for _, child := range c.GetChildren() {
err = child.Encode(w)
if err != nil {
return err
}
}
return nil
}
// EncodeContainerSW - marshal container c to sw
func EncodeContainerSW(c ContainerBox, sw bits.SliceWriter) error {
err := EncodeHeaderSW(c, sw)
if err != nil {
return err
}
for _, child := range c.GetChildren() {
err = child.EncodeSW(sw)
if err != nil {
return err
}
}
return nil
}
// ContainerInfo - write container-box information
func ContainerInfo(c ContainerBox, w io.Writer, specificBoxLevels, indent, indentStep string) error {
bd := newInfoDumper(w, indent, c, -1, 0)
if bd.err != nil {
return bd.err
}
var err error
for _, child := range c.GetChildren() {
err := child.Info(w, specificBoxLevels, indent+indentStep, indentStep)
if err != nil {
return err
}
}
return err
}