-
Notifications
You must be signed in to change notification settings - Fork 84
/
dref.go
162 lines (144 loc) · 3.67 KB
/
dref.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
package mp4
import (
"encoding/binary"
"fmt"
"io"
"github.com/Eyevinn/mp4ff/bits"
)
// DrefBox - Data Reference Box (dref - mandatory)
//
// Contained id: Data Information Box (dinf)
//
// Defines the location of the media data. If the data for the track is located in the same file
// it contains nothing useful.
type DrefBox struct {
Version byte
Flags uint32
EntryCount uint32
Children []Box
}
// CreateDref - Create an DataReferenceBox for selfcontained content
func CreateDref() *DrefBox {
url := CreateURLBox()
dref := &DrefBox{}
dref.AddChild(url)
return dref
}
// AddChild - Add a child box and update EntryCount
func (d *DrefBox) AddChild(box Box) {
d.Children = append(d.Children, box)
d.EntryCount++
}
// DecodeDref - box-specific decode
func DecodeDref(hdr BoxHeader, startPos uint64, r io.Reader) (Box, error) {
var versionAndFlags, entryCount uint32
err := binary.Read(r, binary.BigEndian, &versionAndFlags)
if err != nil {
return nil, err
}
err = binary.Read(r, binary.BigEndian, &entryCount)
if err != nil {
return nil, err
}
// Note higher startPos for children since not simple container.
children, err := DecodeContainerChildren(hdr, startPos+16, startPos+hdr.Size, r)
if err != nil {
return nil, err
}
dref := &DrefBox{
Version: byte(versionAndFlags >> 24),
Flags: versionAndFlags & flagsMask,
}
for _, c := range children {
dref.AddChild(c)
}
if entryCount != dref.EntryCount {
return nil, fmt.Errorf("inconsistent entry count in Dref")
}
return dref, nil
}
// DecodeDrefSR - box-specific decode
func DecodeDrefSR(hdr BoxHeader, startPos uint64, sr bits.SliceReader) (Box, error) {
versionAndFlags := sr.ReadUint32()
entryCount := sr.ReadUint32()
// Note higher startPos for children since not simple container.
children, err := DecodeContainerChildrenSR(hdr, startPos+16, startPos+hdr.Size, sr)
if err != nil {
return nil, err
}
dref := &DrefBox{
Version: byte(versionAndFlags >> 24),
Flags: versionAndFlags & flagsMask,
EntryCount: 0, // incremented by AddChild
}
for _, c := range children {
dref.AddChild(c)
}
if entryCount != dref.EntryCount {
return nil, fmt.Errorf("inconsistent entry count in Dref")
}
return dref, sr.AccError()
}
// Type - box type
func (d *DrefBox) Type() string {
return "dref"
}
// Size - calculated size of box
func (d *DrefBox) Size() uint64 {
return containerSize(d.Children) + 8
}
// Encode - write dref box to w including children
func (d *DrefBox) Encode(w io.Writer) error {
err := EncodeHeader(d, w)
if err != nil {
return err
}
versionAndFlags := (uint32(d.Version) << 24) + d.Flags
err = binary.Write(w, binary.BigEndian, versionAndFlags)
if err != nil {
return err
}
err = binary.Write(w, binary.BigEndian, uint32(d.EntryCount))
if err != nil {
return err
}
for _, b := range d.Children {
err = b.Encode(w)
if err != nil {
return err
}
}
return err
}
// EncodeSW - write dref box to w including children
func (d *DrefBox) EncodeSW(sw bits.SliceWriter) error {
err := EncodeHeaderSW(d, sw)
if err != nil {
return err
}
versionAndFlags := (uint32(d.Version) << 24) + d.Flags
sw.WriteUint32(versionAndFlags)
sw.WriteUint32(uint32(d.EntryCount))
for _, b := range d.Children {
err = b.EncodeSW(sw)
if err != nil {
return err
}
}
return err
}
// Info - write box-specific information
func (d *DrefBox) Info(w io.Writer, specificBoxLevels, indent, indentStep string) error {
bd := newInfoDumper(w, indent, d, int(d.Version), d.Flags)
if bd.err != nil {
return bd.err
}
var err error
for _, c := range d.Children {
err = c.Info(w, specificBoxLevels, indent+indentStep, indentStep)
if err != nil {
return err
}
}
return err
}