-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmultistream.go
More file actions
285 lines (260 loc) · 10.4 KB
/
multistream.go
File metadata and controls
285 lines (260 loc) · 10.4 KB
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// multistream.go implements the public Multistream API for Opus surround sound encoding and decoding.
package gopus
import (
"fmt"
"github.com/thesyncim/gopus/internal/dnnblob"
"github.com/thesyncim/gopus/multistream"
)
func multistreamExplicitChannelsError(kind string, channels int) error {
return fmt.Errorf("%w: %s supports 1-255 channels (got %d)", ErrInvalidChannels, kind, channels)
}
func multistreamDefaultChannelsError(kind, explicitCtor string, channels int) error {
if channels > 8 {
return fmt.Errorf("%w: %s supports 1-8 channels (got %d); use %s with an explicit mapping for >8 channels", ErrInvalidChannels, kind, channels, explicitCtor)
}
return fmt.Errorf("%w: %s supports 1-8 channels (got %d)", ErrInvalidChannels, kind, channels)
}
func multistreamStreamBudgetError(kind string, streams, coupledStreams int) error {
total := streams + coupledStreams
return fmt.Errorf("%w: %s requires streams + coupledStreams <= 255 (got %d + %d = %d)", ErrInvalidStreams, kind, streams, coupledStreams, total)
}
func multistreamMappingLengthError(channels, got int) error {
return fmt.Errorf("%w: expected %d mapping entries for %d channels, got %d", ErrInvalidMapping, channels, channels, got)
}
// MultistreamEncoder encodes multi-channel PCM audio into Opus multistream packets.
//
// A MultistreamEncoder instance maintains internal state and is NOT safe for concurrent use.
// Each goroutine should create its own MultistreamEncoder instance.
//
// Multistream encoding is used for surround sound configurations (5.1, 7.1, etc.)
// where multiple coupled (stereo) and uncoupled (mono) streams are combined.
//
// Reference: RFC 6716 Appendix B, RFC 7845 Section 5.1.1
type MultistreamEncoder struct {
enc *multistream.Encoder
sampleRate int
channels int
frameSize int
expertFrameDuration ExpertFrameDuration
application Application
encodedOnce bool
scratchPCM64 []float64
scratchPCM32 []float32
dnnBlob *dnnblob.Blob
}
// NewMultistreamEncoder creates a new multistream encoder with explicit configuration.
//
// Parameters:
// - sampleRate: input sample rate (8000, 12000, 16000, 24000, or 48000 Hz)
// - channels: total input channels (1-255)
// - streams: total elementary streams (N, 1-255)
// - coupledStreams: number of coupled stereo streams (M, 0 to streams)
// with streams + coupledStreams <= 255
// - mapping: channel mapping table (length must equal channels)
// - application: application hint for encoder optimization
//
// Returns ErrInvalidChannels when channels is outside the explicit multistream
// range [1, 255].
//
// The mapping table determines how input audio is routed to stream encoders:
// - Values 0 to 2*M-1: to coupled streams (even=left, odd=right of stereo pair)
// - Values 2*M to N+M-1: to uncoupled (mono) streams
// - Value 255: silent channel (input ignored)
//
// Example for 5.1 surround (6 channels, 4 streams, 2 coupled):
//
// mapping = [0, 4, 1, 2, 3, 5]
// Input 0 (FL): mapping[0]=0 -> coupled stream 0, left
// Input 1 (C): mapping[1]=4 -> uncoupled stream 2 (2*2+0)
// Input 2 (FR): mapping[2]=1 -> coupled stream 0, right
// Input 3 (RL): mapping[3]=2 -> coupled stream 1, left
// Input 4 (RR): mapping[4]=3 -> coupled stream 1, right
// Input 5 (LFE): mapping[5]=5 -> uncoupled stream 3 (2*2+1)
func NewMultistreamEncoder(sampleRate, channels, streams, coupledStreams int, mapping []byte, application Application) (*MultistreamEncoder, error) {
if !validSampleRate(sampleRate) {
return nil, ErrInvalidSampleRate
}
if channels < 1 || channels > 255 {
return nil, multistreamExplicitChannelsError("multistream encoder", channels)
}
if streams < 1 || streams > 255 {
return nil, ErrInvalidStreams
}
if coupledStreams < 0 || coupledStreams > streams {
return nil, ErrInvalidCoupledStreams
}
if streams+coupledStreams > 255 {
return nil, multistreamStreamBudgetError("multistream encoder", streams, coupledStreams)
}
if len(mapping) != channels {
return nil, multistreamMappingLengthError(channels, len(mapping))
}
if !validApplication(application) {
return nil, ErrInvalidApplication
}
enc, err := multistream.NewEncoder(sampleRate, channels, streams, coupledStreams, mapping)
if err != nil {
return nil, err
}
mse := &MultistreamEncoder{
enc: enc,
sampleRate: sampleRate,
channels: channels,
frameSize: 960, // Default 20ms at 48kHz
expertFrameDuration: ExpertFrameDurationArg,
application: application,
scratchPCM64: make([]float64, 5760*channels),
scratchPCM32: make([]float32, 5760*channels),
}
// Apply application hint
if err := mse.applyApplication(application); err != nil {
return nil, err
}
return mse, nil
}
// NewMultistreamEncoderDefault creates a multistream encoder with default Vorbis-style mapping
// for standard channel configurations (1-8 channels).
//
// This is a convenience function that calls the internal DefaultMapping() to get the appropriate
// streams, coupledStreams, and mapping for the given channel count.
// Returns ErrInvalidChannels when channels is outside the default-mapping range [1, 8].
// Use NewMultistreamEncoder with an explicit mapping for layouts above 8 channels.
//
// Supported channel counts:
// - 1: mono (1 stream, 0 coupled)
// - 2: stereo (1 stream, 1 coupled)
// - 3: 3.0 (2 streams, 1 coupled)
// - 4: quad (2 streams, 2 coupled)
// - 5: 5.0 (3 streams, 2 coupled)
// - 6: 5.1 surround (4 streams, 2 coupled)
// - 7: 6.1 surround (5 streams, 2 coupled)
// - 8: 7.1 surround (5 streams, 3 coupled)
func NewMultistreamEncoderDefault(sampleRate, channels int, application Application) (*MultistreamEncoder, error) {
if !validSampleRate(sampleRate) {
return nil, ErrInvalidSampleRate
}
if channels < 1 || channels > 8 {
return nil, multistreamDefaultChannelsError("default multistream encoder", "NewMultistreamEncoder", channels)
}
if !validApplication(application) {
return nil, ErrInvalidApplication
}
enc, err := multistream.NewEncoderDefault(sampleRate, channels)
if err != nil {
return nil, err
}
mse := &MultistreamEncoder{
enc: enc,
sampleRate: sampleRate,
channels: channels,
frameSize: 960, // Default 20ms at 48kHz
expertFrameDuration: ExpertFrameDurationArg,
application: application,
scratchPCM64: make([]float64, 5760*channels),
scratchPCM32: make([]float32, 5760*channels),
}
// Apply application hints
if err := mse.applyApplication(application); err != nil {
return nil, err
}
return mse, nil
}
// MultistreamDecoder decodes Opus multistream packets into multi-channel PCM audio.
//
// A MultistreamDecoder instance maintains internal state and is NOT safe for concurrent use.
// Each goroutine should create its own MultistreamDecoder instance.
//
// Multistream decoding is used for surround sound configurations (5.1, 7.1, etc.)
// where multiple coupled (stereo) and uncoupled (mono) streams are combined.
//
// Reference: RFC 6716 Appendix B, RFC 7845 Section 5.1.1
type MultistreamDecoder struct {
dec *multistream.Decoder
sampleRate int
channels int
lastFrameSize int
ignoreExtensions bool
dnnBlob *dnnblob.Blob
}
// NewMultistreamDecoder creates a new multistream decoder with explicit configuration.
//
// Parameters:
// - sampleRate: output sample rate (8000, 12000, 16000, 24000, or 48000 Hz)
// - channels: total output channels (1-255)
// - streams: total elementary streams (N, 1-255)
// - coupledStreams: number of coupled stereo streams (M, 0 to streams)
// with streams + coupledStreams <= 255
// - mapping: channel mapping table (length must equal channels)
//
// Returns ErrInvalidChannels when channels is outside the explicit multistream
// range [1, 255].
//
// The mapping table determines how decoded audio is routed to output channels:
// - Values 0 to 2*M-1: from coupled streams (even=left, odd=right of stereo pair)
// - Values 2*M to N+M-1: from uncoupled (mono) streams
// - Value 255: silent channel (output zeros)
func NewMultistreamDecoder(sampleRate, channels, streams, coupledStreams int, mapping []byte) (*MultistreamDecoder, error) {
if !validSampleRate(sampleRate) {
return nil, ErrInvalidSampleRate
}
if channels < 1 || channels > 255 {
return nil, multistreamExplicitChannelsError("multistream decoder", channels)
}
if streams < 1 || streams > 255 {
return nil, ErrInvalidStreams
}
if coupledStreams < 0 || coupledStreams > streams {
return nil, ErrInvalidCoupledStreams
}
if streams+coupledStreams > 255 {
return nil, multistreamStreamBudgetError("multistream decoder", streams, coupledStreams)
}
if len(mapping) != channels {
return nil, multistreamMappingLengthError(channels, len(mapping))
}
dec, err := multistream.NewDecoder(sampleRate, channels, streams, coupledStreams, mapping)
if err != nil {
return nil, err
}
return &MultistreamDecoder{
dec: dec,
sampleRate: sampleRate,
channels: channels,
lastFrameSize: 960, // Default 20ms at 48kHz
}, nil
}
// NewMultistreamDecoderDefault creates a multistream decoder with default Vorbis-style mapping
// for standard channel configurations (1-8 channels).
//
// This is a convenience function that calls the internal DefaultMapping() to get the appropriate
// streams, coupledStreams, and mapping for the given channel count.
// Returns ErrInvalidChannels when channels is outside the default-mapping range [1, 8].
// Use NewMultistreamDecoder with an explicit mapping for layouts above 8 channels.
//
// Supported channel counts:
// - 1: mono (1 stream, 0 coupled)
// - 2: stereo (1 stream, 1 coupled)
// - 3: 3.0 (2 streams, 1 coupled)
// - 4: quad (2 streams, 2 coupled)
// - 5: 5.0 (3 streams, 2 coupled)
// - 6: 5.1 surround (4 streams, 2 coupled)
// - 7: 6.1 surround (5 streams, 2 coupled)
// - 8: 7.1 surround (5 streams, 3 coupled)
func NewMultistreamDecoderDefault(sampleRate, channels int) (*MultistreamDecoder, error) {
if !validSampleRate(sampleRate) {
return nil, ErrInvalidSampleRate
}
if channels < 1 || channels > 8 {
return nil, multistreamDefaultChannelsError("default multistream decoder", "NewMultistreamDecoder", channels)
}
dec, err := multistream.NewDecoderDefault(sampleRate, channels)
if err != nil {
return nil, err
}
return &MultistreamDecoder{
dec: dec,
sampleRate: sampleRate,
channels: channels,
lastFrameSize: 960, // Default 20ms at 48kHz
}, nil
}