forked from deepch/vdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
opus.go
202 lines (187 loc) · 3.73 KB
/
opus.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
package fmp4io
import (
"github.com/galindocode/vdk/utils/bits/pio"
)
const (
OPUS = Tag(0x4f707573)
DOPS = Tag(0x644f7073)
)
type OpusSampleEntry struct {
DataRefIdx uint16
NumberOfChannels uint16
SampleSize uint16
CompressionID uint16
SampleRate float64
Conf *OpusSpecificConfiguration
AtomPos
}
func (a OpusSampleEntry) Tag() Tag { return OPUS }
func (a OpusSampleEntry) Marshal(b []byte) (n int) {
pio.PutU32BE(b[4:], uint32(OPUS))
n += a.marshal(b[8:]) + 8
pio.PutU32BE(b[0:], uint32(n))
return
}
func (a OpusSampleEntry) marshal(b []byte) (n int) {
n += 6
pio.PutU16BE(b[n:], a.DataRefIdx)
n += 2
n += 8
pio.PutU16BE(b[n:], a.NumberOfChannels)
n += 2
pio.PutU16BE(b[n:], a.SampleSize)
n += 2
n += 4
PutFixed32(b[n:], a.SampleRate)
n += 4
if a.Conf != nil {
n += a.Conf.Marshal(b[n:])
}
return
}
func (a OpusSampleEntry) Len() (n int) {
n += 8
n += 6
n += 2
n += 8
n += 2
n += 2
n += 4
n += 4
if a.Conf != nil {
n += a.Conf.Len()
}
return
}
func (a *OpusSampleEntry) Unmarshal(b []byte, offset int) (n int, err error) {
(&a.AtomPos).setPos(offset, len(b))
n += 8
n += 6
if len(b) < n+2 {
err = parseErr("DataRefIdx", n+offset, err)
return
}
a.DataRefIdx = pio.U16BE(b[n:])
n += 2
n += 2
n += 2
n += 4
if len(b) < n+2 {
err = parseErr("NumberOfChannels", n+offset, err)
return
}
a.NumberOfChannels = pio.U16BE(b[n:])
n += 2
if len(b) < n+2 {
err = parseErr("SampleSize", n+offset, err)
return
}
a.SampleSize = pio.U16BE(b[n:])
n += 2
n += 2
n += 2
if len(b) < n+4 {
err = parseErr("SampleRate", n+offset, err)
return
}
a.SampleRate = GetFixed32(b[n:])
n += 4
for n+8 < len(b) {
tag := Tag(pio.U32BE(b[n+4:]))
size := int(pio.U32BE(b[n:]))
if len(b) < n+size {
err = parseErr("TagSizeInvalid", n+offset, err)
return
}
switch tag {
case DOPS:
{
atom := &OpusSpecificConfiguration{}
if _, err = atom.Unmarshal(b[n:n+size], offset+n); err != nil {
err = parseErr("esds", n+offset, err)
return
}
a.Conf = atom
}
}
n += size
}
return
}
func (a OpusSampleEntry) Children() (r []Atom) {
if a.Conf != nil {
r = append(r, a.Conf)
}
return
}
type OpusSpecificConfiguration struct {
Version uint8
OutputChannelCount uint8
PreSkip uint16
InputSampleRate uint32
OutputGain int16
ChannelMappingFamily uint8
AtomPos
}
func (a OpusSpecificConfiguration) Tag() Tag { return DOPS }
func (a OpusSpecificConfiguration) Children() []Atom { return nil }
func (a OpusSpecificConfiguration) Len() (n int) {
n += 8
n++
n++
n += 2
n += 4
n += 2
n++
return
}
func (a OpusSpecificConfiguration) Marshal(b []byte) (n int) {
pio.PutU32BE(b[4:], uint32(DOPS))
n += a.marshal(b[8:]) + 8
pio.PutU32BE(b[0:], uint32(n))
return
}
func (a OpusSpecificConfiguration) marshal(b []byte) (n int) {
pio.PutU8(b[n:], a.Version)
n++
pio.PutU8(b[n:], a.OutputChannelCount)
n++
pio.PutU16BE(b[n:], a.PreSkip)
n += 2
pio.PutU32BE(b[n:], a.InputSampleRate)
n += 4
pio.PutI16BE(b[n:], a.OutputGain)
n += 2
pio.PutU8(b[n:], a.ChannelMappingFamily)
n++
return
}
func (a *OpusSpecificConfiguration) Unmarshal(b []byte, offset int) (n int, err error) {
a.setPos(offset, len(b))
n += 8
if len(b) < 8+11 {
err = parseErr("OpusSpecificConfiguration", offset, nil)
return
}
a.Version = b[n]
if a.Version != 0 {
err = parseErr("unknown version", offset, nil)
return
}
n++
a.OutputChannelCount = b[n]
n++
a.PreSkip = pio.U16BE(b[n:])
n += 2
a.InputSampleRate = pio.U32BE(b[n:])
n += 4
a.OutputGain = pio.I16BE(b[n:])
n += 2
a.ChannelMappingFamily = b[n]
if a.ChannelMappingFamily != 0 {
err = parseErr("ChannelMappingFamily", offset+n, nil)
return
}
n++
return
}