-
Notifications
You must be signed in to change notification settings - Fork 177
/
config.go
235 lines (191 loc) · 4.69 KB
/
config.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
package mpeg4audio
import (
"fmt"
"github.com/aler9/gortsplib/v2/pkg/bits"
)
// Config is a MPEG-4 Audio configuration.
type Config struct {
Type ObjectType
SampleRate int
ChannelCount int
// SBR / PS specific
ExtensionType ObjectType
ExtensionSampleRate int
// AAC-LC specific
FrameLengthFlag bool
DependsOnCoreCoder bool
CoreCoderDelay uint16
}
// Unmarshal decodes a Config.
func (c *Config) Unmarshal(buf []byte) error {
pos := 0
tmp, err := bits.ReadBits(buf, &pos, 5)
if err != nil {
return err
}
c.Type = ObjectType(tmp)
switch c.Type {
case ObjectTypeAACLC:
case ObjectTypeSBR:
case ObjectTypePS:
default:
return fmt.Errorf("unsupported object type: %d", c.Type)
}
sampleRateIndex, err := bits.ReadBits(buf, &pos, 4)
if err != nil {
return err
}
switch {
case sampleRateIndex <= 12:
c.SampleRate = sampleRates[sampleRateIndex]
case sampleRateIndex == 0x0F:
tmp, err := bits.ReadBits(buf, &pos, 24)
if err != nil {
return err
}
c.SampleRate = int(tmp)
default:
return fmt.Errorf("invalid sample rate index (%d)", sampleRateIndex)
}
channelConfig, err := bits.ReadBits(buf, &pos, 4)
if err != nil {
return err
}
switch {
case channelConfig == 0:
return fmt.Errorf("not yet supported")
case channelConfig >= 1 && channelConfig <= 6:
c.ChannelCount = int(channelConfig)
case channelConfig == 7:
c.ChannelCount = 8
default:
return fmt.Errorf("invalid channel configuration (%d)", channelConfig)
}
if c.Type == ObjectTypeSBR || c.Type == ObjectTypePS {
c.ExtensionType = c.Type
extensionSamplingFrequencyIndex, err := bits.ReadBits(buf, &pos, 4)
if err != nil {
return err
}
switch {
case extensionSamplingFrequencyIndex <= 12:
c.ExtensionSampleRate = sampleRates[extensionSamplingFrequencyIndex]
case extensionSamplingFrequencyIndex == 0x0F:
tmp, err := bits.ReadBits(buf, &pos, 24)
if err != nil {
return err
}
c.ExtensionSampleRate = int(tmp)
default:
return fmt.Errorf("invalid extension sample rate index (%d)", extensionSamplingFrequencyIndex)
}
tmp, err = bits.ReadBits(buf, &pos, 5)
if err != nil {
return err
}
c.Type = ObjectType(tmp)
if c.Type != ObjectTypeAACLC {
return fmt.Errorf("unsupported object type: %d", c.Type)
}
}
c.FrameLengthFlag, err = bits.ReadFlag(buf, &pos)
if err != nil {
return err
}
c.DependsOnCoreCoder, err = bits.ReadFlag(buf, &pos)
if err != nil {
return err
}
if c.DependsOnCoreCoder {
tmp, err := bits.ReadBits(buf, &pos, 14)
if err != nil {
return err
}
c.CoreCoderDelay = uint16(tmp)
}
extensionFlag, err := bits.ReadFlag(buf, &pos)
if err != nil {
return err
}
if extensionFlag {
return fmt.Errorf("unsupported")
}
return nil
}
func (c Config) marshalSize() int {
n := 5 + 4 + 3
_, ok := reverseSampleRates[c.SampleRate]
if !ok {
n += 28
} else {
n += 4
}
if c.ExtensionType == ObjectTypeSBR || c.ExtensionType == ObjectTypePS {
_, ok := reverseSampleRates[c.ExtensionSampleRate]
if !ok {
n += 28
} else {
n += 4
}
n += 5
} else if c.DependsOnCoreCoder {
n += 14
}
ret := n / 8
if (n % 8) != 0 {
ret++
}
return ret
}
// Marshal encodes a Config.
func (c Config) Marshal() ([]byte, error) {
buf := make([]byte, c.marshalSize())
pos := 0
if c.ExtensionType == ObjectTypeSBR || c.ExtensionType == ObjectTypePS {
bits.WriteBits(buf, &pos, uint64(c.ExtensionType), 5)
} else {
bits.WriteBits(buf, &pos, uint64(c.Type), 5)
}
sampleRateIndex, ok := reverseSampleRates[c.SampleRate]
if !ok {
bits.WriteBits(buf, &pos, uint64(15), 4)
bits.WriteBits(buf, &pos, uint64(c.SampleRate), 24)
} else {
bits.WriteBits(buf, &pos, uint64(sampleRateIndex), 4)
}
var channelConfig int
switch {
case c.ChannelCount >= 1 && c.ChannelCount <= 6:
channelConfig = c.ChannelCount
case c.ChannelCount == 8:
channelConfig = 7
default:
return nil, fmt.Errorf("invalid channel count (%d)", c.ChannelCount)
}
bits.WriteBits(buf, &pos, uint64(channelConfig), 4)
if c.ExtensionType == ObjectTypeSBR || c.ExtensionType == ObjectTypePS {
sampleRateIndex, ok := reverseSampleRates[c.ExtensionSampleRate]
if !ok {
bits.WriteBits(buf, &pos, uint64(0x0F), 4)
bits.WriteBits(buf, &pos, uint64(c.ExtensionSampleRate), 24)
} else {
bits.WriteBits(buf, &pos, uint64(sampleRateIndex), 4)
}
bits.WriteBits(buf, &pos, uint64(c.Type), 5)
} else {
if c.FrameLengthFlag {
bits.WriteBits(buf, &pos, 1, 1)
} else {
bits.WriteBits(buf, &pos, 0, 1)
}
if c.DependsOnCoreCoder {
bits.WriteBits(buf, &pos, 1, 1)
} else {
bits.WriteBits(buf, &pos, 0, 1)
}
if c.DependsOnCoreCoder {
bits.WriteBits(buf, &pos, uint64(c.CoreCoderDelay), 14)
}
}
return buf, nil
}