-
Notifications
You must be signed in to change notification settings - Fork 84
/
audiosamplentry.go
262 lines (231 loc) · 6.47 KB
/
audiosamplentry.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package mp4
import (
"bytes"
"fmt"
"io"
"github.com/Eyevinn/mp4ff/bits"
)
// AudioSampleEntryBox according to ISO/IEC 14496-12
type AudioSampleEntryBox struct {
name string
DataReferenceIndex uint16
ChannelCount uint16
SampleSize uint16
SampleRate uint16 // Integer part
Esds *EsdsBox
Dac3 *Dac3Box
Dec3 *Dec3Box
Btrt *BtrtBox
Sinf *SinfBox
Children []Box
}
// NewAudioSampleEntryBox - Create new empty mp4a box
func NewAudioSampleEntryBox(name string) *AudioSampleEntryBox {
return &AudioSampleEntryBox{name: name, DataReferenceIndex: 1}
}
func makeFixed32Uint(nr uint16) uint32 {
return uint32(nr) << 16
}
func makeUint16FromFixed32(nr uint32) uint16 {
return uint16(nr >> 16)
}
// CreateAudioSampleEntryBox - Create new AudioSampleEntry such as mp4
func CreateAudioSampleEntryBox(name string, nrChannels, sampleSize, sampleRate uint16, child Box) *AudioSampleEntryBox {
a := &AudioSampleEntryBox{
name: name,
DataReferenceIndex: 1,
ChannelCount: nrChannels,
SampleSize: sampleSize,
SampleRate: sampleRate,
Children: nil,
}
if child != nil {
a.AddChild(child)
}
return a
}
// AddChild - add a child box (avcC normally, but clap and pasp could be part of visual entry)
func (a *AudioSampleEntryBox) AddChild(child Box) {
switch child.Type() {
case "esds":
a.Esds = child.(*EsdsBox)
case "dac3":
a.Dac3 = child.(*Dac3Box)
case "dec3":
a.Dec3 = child.(*Dec3Box)
case "btrt":
a.Btrt = child.(*BtrtBox)
case "sinf":
a.Sinf = child.(*SinfBox)
}
a.Children = append(a.Children, child)
}
const nrAudioSampleBytesBeforeChildren = 36
// DecodeAudioSampleEntry - decode mp4a... box
func DecodeAudioSampleEntry(hdr BoxHeader, startPos uint64, r io.Reader) (Box, error) {
data, err := readBoxBody(r, hdr)
if err != nil {
return nil, err
}
sr := bits.NewFixedSliceReader(data)
a := NewAudioSampleEntryBox(hdr.Name)
// 14496-12 8.5.2.2 Sample entry (8 bytes)
sr.SkipBytes(6) // Skip 6 reserved bytes
a.DataReferenceIndex = sr.ReadUint16()
// 14496-12 12.2.3.2 Audio Sample entry (20 bytes)
sr.SkipBytes(8) // reserved == 0
a.ChannelCount = sr.ReadUint16()
a.SampleSize = sr.ReadUint16()
sr.SkipBytes(4) // Predefined + reserved
a.SampleRate = makeUint16FromFixed32(sr.ReadUint32())
remaining := sr.RemainingBytes()
restReader := bytes.NewReader(remaining)
pos := startPos + nrAudioSampleBytesBeforeChildren // Size of all previous data
for {
box, err := DecodeBox(pos, restReader)
if err == io.EOF {
break
} else if err != nil {
return nil, err
}
if box != nil {
a.AddChild(box)
pos += box.Size()
}
if pos == startPos+hdr.Size {
break
} else if pos > startPos+hdr.Size {
return nil, fmt.Errorf("bad size when decoding %s", hdr.Name)
}
}
return a, nil
}
// DecodeAudioSampleEntry - decode mp4a... box
func DecodeAudioSampleEntrySR(hdr BoxHeader, startPos uint64, sr bits.SliceReader) (Box, error) {
a := NewAudioSampleEntryBox(hdr.Name)
// 14496-12 8.5.2.2 Sample entry (8 bytes)
sr.SkipBytes(6) // Skip 6 reserved bytes
a.DataReferenceIndex = sr.ReadUint16()
// 14496-12 12.2.3.2 Audio Sample entry (20 bytes)
sr.SkipBytes(8) // reserved == 0
a.ChannelCount = sr.ReadUint16()
a.SampleSize = sr.ReadUint16()
sr.SkipBytes(4) // Predefined + reserved
a.SampleRate = makeUint16FromFixed32(sr.ReadUint32())
pos := startPos + nrAudioSampleBytesBeforeChildren // Size of all previous data
lastPos := startPos + hdr.Size
for {
if pos >= lastPos {
break
}
box, err := DecodeBoxSR(pos, sr)
if err != nil {
return nil, err
}
if box != nil {
a.AddChild(box)
pos += box.Size()
}
}
return a, sr.AccError()
}
// Type - return box type
func (a *AudioSampleEntryBox) Type() string {
return a.name
}
// SetType sets the type (name) of the box
func (a *AudioSampleEntryBox) SetType(name string) {
a.name = name
}
// Size - return calculated size
func (a *AudioSampleEntryBox) Size() uint64 {
totalSize := uint64(nrAudioSampleBytesBeforeChildren)
for _, child := range a.Children {
totalSize += child.Size()
}
return totalSize
}
// Encode - write box to w
func (a *AudioSampleEntryBox) Encode(w io.Writer) error {
err := EncodeHeader(a, w)
if err != nil {
return err
}
buf := makebuf(a)
sw := bits.NewFixedSliceWriterFromSlice(buf)
sw.WriteZeroBytes(6)
sw.WriteUint16(a.DataReferenceIndex)
sw.WriteZeroBytes(8) // pre_defined and reserved
sw.WriteUint16(a.ChannelCount)
sw.WriteUint16(a.SampleSize)
sw.WriteZeroBytes(4) // Pre-defined and reserved
sw.WriteUint32(makeFixed32Uint(a.SampleRate)) // nrAudioSampleBytesBeforeChildren bytes this far
_, err = w.Write(buf[:sw.Offset()]) // Only write written bytes
if err != nil {
return err
}
// Next output child boxes in order
for _, child := range a.Children {
err = child.Encode(w)
if err != nil {
return err
}
}
return err
}
// Encode - write box to sw
func (a *AudioSampleEntryBox) EncodeSW(sw bits.SliceWriter) error {
err := EncodeHeaderSW(a, sw)
if err != nil {
return err
}
sw.WriteZeroBytes(6)
sw.WriteUint16(a.DataReferenceIndex)
sw.WriteZeroBytes(8) // pre_defined and reserved
sw.WriteUint16(a.ChannelCount)
sw.WriteUint16(a.SampleSize)
sw.WriteZeroBytes(4) // Pre-defined and reserved
sw.WriteUint32(makeFixed32Uint(a.SampleRate)) // nrAudioSampleBytesBeforeChildren bytes this far
// Next output child boxes in order
for _, child := range a.Children {
err = child.EncodeSW(sw)
if err != nil {
return err
}
}
return err
}
// Info - write box info to w
func (a *AudioSampleEntryBox) Info(w io.Writer, specificBoxLevels, indent, indentStep string) error {
bd := newInfoDumper(w, indent, a, -1, 0)
if bd.err != nil {
return bd.err
}
var err error
for _, child := range a.Children {
err = child.Info(w, specificBoxLevels, indent+indentStep, indentStep)
if err != nil {
return err
}
}
return nil
}
// RemoveEncryption - remove sinf box and set type to unencrypted type
func (a *AudioSampleEntryBox) RemoveEncryption() (*SinfBox, error) {
if a.name != "enca" {
return nil, fmt.Errorf("is not encrypted: %s", a.name)
}
sinf := a.Sinf
if sinf == nil {
return nil, fmt.Errorf("does not have sinf box")
}
for i := range a.Children {
if a.Children[i].Type() == "sinf" {
a.Children = append(a.Children[:i], a.Children[i+1:]...)
a.Sinf = nil
break
}
}
a.name = sinf.Frma.DataFormat
return sinf, nil
}