opus-decoder 0.1.0

Pure-Rust Opus decoder — RFC 8251 conformant, no unsafe, no FFI
Documentation
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
//! Opus multistream decoder — wraps multiple `OpusDecoder` instances
//! and demultiplexes channel mapping per RFC 7845 §5.1.
//!
//! Mirrors the wrapper logic from libopus `opus_multistream_decoder.c`.

use crate::packet;
use crate::{OpusDecoder, OpusError};

/// Opus multistream decoder.
///
/// Holds one `OpusDecoder` per stream. Coupled streams use stereo decoders and
/// remaining streams use mono decoders.
pub struct OpusMultistreamDecoder {
    /// One decoder per stream. Coupled streams are stereo (2ch), rest mono.
    decoders: Vec<OpusDecoder>,
    /// Total output channels (1–255).
    nb_channels: usize,
    /// Total streams in packet (coupled + mono).
    nb_streams: usize,
    /// Streams that are stereo-coupled (always the first `nb_coupled_streams`).
    nb_coupled_streams: usize,
    /// `mapping[output_channel] = index into interleaved decoder outputs`.
    /// `255` means "silence this output channel".
    mapping: Vec<u8>,
    /// Output sample rate (same for all streams).
    sample_rate: u32,
}

impl OpusMultistreamDecoder {
    /// Create a new multistream decoder.
    ///
    /// Parameters: `sample_rate`, total output `nb_channels`, `nb_streams`,
    /// `nb_coupled_streams`, and per-output-channel `mapping`.
    /// Returns: initialized multistream decoder on success.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use opus_decoder::OpusMultistreamDecoder;
    ///
    /// let decoder = OpusMultistreamDecoder::new(48_000, 2, 2, 0, &[0, 1])?;
    /// # let _ = decoder;
    /// # Ok::<(), opus_decoder::OpusError>(())
    /// ```
    pub fn new(
        sample_rate: u32,
        nb_channels: usize,
        nb_streams: usize,
        nb_coupled_streams: usize,
        mapping: &[u8],
    ) -> Result<Self, OpusError> {
        if nb_channels == 0 || nb_channels > 255 {
            return Err(OpusError::InvalidArgument("nb_channels"));
        }
        if nb_streams == 0 {
            return Err(OpusError::InvalidArgument("nb_streams"));
        }
        if nb_coupled_streams > nb_streams {
            return Err(OpusError::InvalidArgument("nb_coupled_streams"));
        }
        if nb_streams > 255usize.saturating_sub(nb_coupled_streams) {
            return Err(OpusError::InvalidArgument("nb_streams"));
        }
        if mapping.len() != nb_channels {
            return Err(OpusError::InvalidArgument("mapping"));
        }

        let total_decoded_channels = (2 * nb_coupled_streams) + (nb_streams - nb_coupled_streams);
        for &slot in mapping {
            if slot != 255 && usize::from(slot) >= total_decoded_channels {
                return Err(OpusError::InvalidArgument("mapping"));
            }
        }

        let mut decoders = Vec::with_capacity(nb_streams);
        for stream_idx in 0..nb_streams {
            let channels = if stream_idx < nb_coupled_streams {
                2
            } else {
                1
            };
            decoders.push(OpusDecoder::new(sample_rate, channels)?);
        }

        Ok(Self {
            decoders,
            nb_channels,
            nb_streams,
            nb_coupled_streams,
            mapping: mapping.to_vec(),
            sample_rate,
        })
    }

    /// Decode a multistream packet into interleaved i16 PCM.
    ///
    /// Parameters: multistream `packet`, writable interleaved `pcm`, and `fec`.
    /// - `fec`: reserved for future in-band FEC support. Currently treated as
    ///   packet loss concealment (PLC) when `true`. Pass `false` for normal decode.
    ///
    /// Returns: decoded samples per output channel.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use opus_decoder::OpusMultistreamDecoder;
    ///
    /// let mut decoder = OpusMultistreamDecoder::new(48_000, 2, 2, 0, &[0, 1])?;
    /// let packet = std::fs::read("multistream-frame.opus")?;
    /// let mut pcm = vec![0i16; 960 * 2];
    /// let samples = decoder.decode(&packet, &mut pcm, false)?;
    /// # let _ = samples;
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub fn decode(
        &mut self,
        packet: &[u8],
        pcm: &mut [i16],
        fec: bool,
    ) -> Result<usize, OpusError> {
        let sub_packets = if packet.is_empty() || fec {
            vec![&[][..]; self.nb_streams]
        } else {
            split_multistream_packet(packet, self.nb_streams)?
        };
        let frame_size = self.expected_frame_size(&sub_packets, fec)?;
        let needed = frame_size * self.nb_channels;
        if pcm.len() < needed {
            return Err(OpusError::BufferTooSmall);
        }
        if frame_size == 0 {
            return Ok(0);
        }

        let mut stream_pcm = Vec::with_capacity(self.nb_streams);
        for (stream_idx, sub_packet) in sub_packets.iter().enumerate() {
            let channels = self.stream_channels(stream_idx);
            let mut buf = vec![0i16; frame_size * channels];
            let written = self.decoders[stream_idx].decode(sub_packet, &mut buf, fec)?;
            if written != frame_size {
                return Err(OpusError::InvalidPacket);
            }
            stream_pcm.push(buf);
        }

        for (channel_idx, &slot) in self.mapping.iter().enumerate() {
            if slot == 255 {
                zero_output_channel_i16(pcm, self.nb_channels, channel_idx, frame_size);
                continue;
            }

            let (stream_idx, source_channel) = self.slot_to_stream_channel(usize::from(slot));
            copy_output_channel_i16(
                pcm,
                self.nb_channels,
                channel_idx,
                &stream_pcm[stream_idx],
                self.stream_channels(stream_idx),
                source_channel,
                frame_size,
            );
        }

        Ok(frame_size)
    }

    /// Decode a multistream packet into interleaved f32 PCM.
    ///
    /// Parameters: multistream `packet`, writable interleaved `pcm`, and `fec`.
    /// - `fec`: reserved for future in-band FEC support. Currently treated as
    ///   packet loss concealment (PLC) when `true`. Pass `false` for normal decode.
    ///
    /// Returns: decoded samples per output channel.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use opus_decoder::OpusMultistreamDecoder;
    ///
    /// let mut decoder = OpusMultistreamDecoder::new(48_000, 2, 2, 0, &[0, 1])?;
    /// let packet = std::fs::read("multistream-frame.opus")?;
    /// let mut pcm = vec![0.0f32; 960 * 2];
    /// let samples = decoder.decode_float(&packet, &mut pcm, false)?;
    /// # let _ = samples;
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub fn decode_float(
        &mut self,
        packet: &[u8],
        pcm: &mut [f32],
        fec: bool,
    ) -> Result<usize, OpusError> {
        let sub_packets = if packet.is_empty() || fec {
            vec![&[][..]; self.nb_streams]
        } else {
            split_multistream_packet(packet, self.nb_streams)?
        };
        let frame_size = self.expected_frame_size(&sub_packets, fec)?;
        let needed = frame_size * self.nb_channels;
        if pcm.len() < needed {
            return Err(OpusError::BufferTooSmall);
        }
        if frame_size == 0 {
            return Ok(0);
        }

        let mut stream_pcm = Vec::with_capacity(self.nb_streams);
        for (stream_idx, sub_packet) in sub_packets.iter().enumerate() {
            let channels = self.stream_channels(stream_idx);
            let mut buf = vec![0.0f32; frame_size * channels];
            let written = self.decoders[stream_idx].decode_float(sub_packet, &mut buf, fec)?;
            if written != frame_size {
                return Err(OpusError::InvalidPacket);
            }
            stream_pcm.push(buf);
        }

        for (channel_idx, &slot) in self.mapping.iter().enumerate() {
            if slot == 255 {
                zero_output_channel_f32(pcm, self.nb_channels, channel_idx, frame_size);
                continue;
            }

            let (stream_idx, source_channel) = self.slot_to_stream_channel(usize::from(slot));
            copy_output_channel_f32(
                pcm,
                self.nb_channels,
                channel_idx,
                &stream_pcm[stream_idx],
                self.stream_channels(stream_idx),
                source_channel,
                frame_size,
            );
        }

        Ok(frame_size)
    }

    /// Reset all internal decoders.
    ///
    /// Parameters: none.
    /// Returns: nothing.
    pub fn reset(&mut self) {
        for decoder in &mut self.decoders {
            decoder.reset();
        }
    }

    /// Compute the expected frame size for one multistream decode call.
    ///
    /// Parameters: per-stream `sub_packets` and FEC flag `fec`.
    /// Returns: decoded samples per output channel.
    fn expected_frame_size(&self, sub_packets: &[&[u8]], fec: bool) -> Result<usize, OpusError> {
        if fec || sub_packets.iter().all(|packet| packet.is_empty()) {
            return Ok(self
                .decoders
                .first()
                .map(|decoder| decoder.last_packet_duration)
                .unwrap_or(0));
        }

        let mut frame_size = None;
        for sub_packet in sub_packets.iter().filter(|packet| !packet.is_empty()) {
            let samples = packet::parse_packet(sub_packet)
                .map_err(OpusError::from)?
                .samples_per_channel(self.sample_rate);
            if let Some(expected) = frame_size {
                if expected != samples {
                    return Err(OpusError::InvalidPacket);
                }
            } else {
                frame_size = Some(samples);
            }
        }

        Ok(frame_size.unwrap_or(0))
    }

    /// Return the decoded channel count for one elementary stream.
    ///
    /// Parameters: `stream_idx` elementary stream index.
    /// Returns: `2` for coupled streams and `1` for mono streams.
    fn stream_channels(&self, stream_idx: usize) -> usize {
        if stream_idx < self.nb_coupled_streams {
            2
        } else {
            1
        }
    }

    /// Map one decoded channel slot to `(stream_idx, channel_idx)`.
    ///
    /// Parameters: flattened decoded `slot`.
    /// Returns: elementary stream index and channel index within that stream.
    fn slot_to_stream_channel(&self, slot: usize) -> (usize, usize) {
        let coupled_slots = 2 * self.nb_coupled_streams;
        if slot < coupled_slots {
            (slot / 2, slot % 2)
        } else {
            (self.nb_coupled_streams + (slot - coupled_slots), 0)
        }
    }
}

/// Split a multistream packet into per-stream sub-packets.
///
/// Parameters: full multistream `packet` and total `nb_streams`.
/// Returns: borrowed sub-packet slices in stream order.
fn split_multistream_packet(packet: &[u8], nb_streams: usize) -> Result<Vec<&[u8]>, OpusError> {
    if nb_streams == 0 {
        return Err(OpusError::InvalidArgument("nb_streams"));
    }
    if packet.is_empty() {
        return Ok(vec![packet; nb_streams]);
    }

    let mut out = Vec::with_capacity(nb_streams);
    let mut offset = 0usize;
    for _stream_idx in 0..nb_streams.saturating_sub(1) {
        let (packet_len, used) = parse_self_delimited_size(&packet[offset..])?;
        offset += used;
        if offset + packet_len > packet.len() {
            return Err(OpusError::InvalidPacket);
        }
        out.push(&packet[offset..offset + packet_len]);
        offset += packet_len;
    }

    if offset > packet.len() {
        return Err(OpusError::InvalidPacket);
    }
    out.push(&packet[offset..]);
    Ok(out)
}

/// Parse one self-delimited multistream sub-packet length.
///
/// Parameters: encoded length `data`.
/// Returns: tuple `(packet_len, bytes_used)`.
fn parse_self_delimited_size(data: &[u8]) -> Result<(usize, usize), OpusError> {
    if data.is_empty() {
        return Err(OpusError::InvalidPacket);
    }

    let first = usize::from(data[0]);
    if first < 252 {
        Ok((first, 1))
    } else {
        if data.len() < 2 {
            return Err(OpusError::InvalidPacket);
        }
        Ok((first + (4 * usize::from(data[1])), 2))
    }
}

/// Copy one decoded i16 channel into the interleaved multistream output buffer.
///
/// Parameters: mutable destination `pcm`, destination `dst_stride`, output
/// channel index `dst_channel`, source `src`, source `src_stride`, source
/// channel index `src_channel`, and `frame_size`.
/// Returns: nothing; destination channel is overwritten.
fn copy_output_channel_i16(
    pcm: &mut [i16],
    dst_stride: usize,
    dst_channel: usize,
    src: &[i16],
    src_stride: usize,
    src_channel: usize,
    frame_size: usize,
) {
    for sample_idx in 0..frame_size {
        pcm[sample_idx * dst_stride + dst_channel] = src[sample_idx * src_stride + src_channel];
    }
}

/// Zero one interleaved i16 output channel.
///
/// Parameters: mutable destination `pcm`, destination `dst_stride`, output
/// channel index `dst_channel`, and `frame_size`.
/// Returns: nothing; destination channel is zero-filled.
fn zero_output_channel_i16(
    pcm: &mut [i16],
    dst_stride: usize,
    dst_channel: usize,
    frame_size: usize,
) {
    for sample_idx in 0..frame_size {
        pcm[sample_idx * dst_stride + dst_channel] = 0;
    }
}

/// Copy one decoded f32 channel into the interleaved multistream output buffer.
///
/// Parameters: mutable destination `pcm`, destination `dst_stride`, output
/// channel index `dst_channel`, source `src`, source `src_stride`, source
/// channel index `src_channel`, and `frame_size`.
/// Returns: nothing; destination channel is overwritten.
fn copy_output_channel_f32(
    pcm: &mut [f32],
    dst_stride: usize,
    dst_channel: usize,
    src: &[f32],
    src_stride: usize,
    src_channel: usize,
    frame_size: usize,
) {
    for sample_idx in 0..frame_size {
        pcm[sample_idx * dst_stride + dst_channel] = src[sample_idx * src_stride + src_channel];
    }
}

/// Zero one interleaved f32 output channel.
///
/// Parameters: mutable destination `pcm`, destination `dst_stride`, output
/// channel index `dst_channel`, and `frame_size`.
/// Returns: nothing; destination channel is zero-filled.
fn zero_output_channel_f32(
    pcm: &mut [f32],
    dst_stride: usize,
    dst_channel: usize,
    frame_size: usize,
) {
    for sample_idx in 0..frame_size {
        pcm[sample_idx * dst_stride + dst_channel] = 0.0;
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    /// Split one two-stream packet with a one-byte length prefix.
    ///
    /// Parameters: none.
    /// Returns: nothing; panics on mismatch.
    #[test]
    fn split_two_stream_packet_with_short_prefix() {
        let packet = [3u8, 10, 11, 12, 20, 21];
        let sub_packets = split_multistream_packet(&packet, 2).unwrap();
        assert_eq!(sub_packets, vec![&[10, 11, 12][..], &[20, 21][..]]);
    }

    /// Split one empty packet into PLC sub-packets for all streams.
    ///
    /// Parameters: none.
    /// Returns: nothing; panics on mismatch.
    #[test]
    fn split_empty_packet_for_plc() {
        let sub_packets = split_multistream_packet(&[], 3).unwrap();
        assert_eq!(sub_packets, vec![&[][..], &[][..], &[][..]]);
    }
}