| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | use ec_core::{Error, Result}; |
| 22 | |
| 23 | use crate::Decoder; |
| 24 | |
| 25 | |
| 26 | #[derive(Debug)] |
| 27 | pub struct MultistreamDecoder { |
| 28 | streams: Vec<Decoder>, |
| 29 | coupled: usize, |
| 30 | mapping: Vec<u8>, |
| 31 | channels: usize, |
| 32 | sample_rate: u32, |
| 33 | |
| 34 | buf: Vec<f32>, |
| 35 | } |
| 36 | |
| 37 | impl MultistreamDecoder { |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | pub fn with_rate( |
| 49 | sample_rate: u32, |
| 50 | streams: usize, |
| 51 | coupled: usize, |
| 52 | mapping: &[u8], |
| 53 | ) -> MultistreamDecoder { |
| 54 | match Self::try_with_rate(sample_rate, streams, coupled, mapping) { |
| 55 | Ok(d) => d, |
| 56 | Err(e) => panic!("invalid Opus multistream layout: {e}"), |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | |
| 61 | |
| 62 | pub fn try_with_rate( |
| 63 | sample_rate: u32, |
| 64 | streams: usize, |
| 65 | coupled: usize, |
| 66 | mapping: &[u8], |
| 67 | ) -> Result<MultistreamDecoder> { |
| 68 | if streams == 0 || streams > 255 || coupled > streams { |
| 69 | return Err(Error::corrupt(format!( |
| 70 | "opus multistream: {streams} streams of which {coupled} coupled" |
| 71 | ))); |
| 72 | } |
| 73 | let coded = coupled + streams; |
| 74 | if mapping.is_empty() || mapping.len() > 255 { |
| 75 | return Err(Error::corrupt(format!( |
| 76 | "opus multistream: {} output channels", |
| 77 | mapping.len() |
| 78 | ))); |
| 79 | } |
| 80 | if let Some(&bad) = mapping.iter().find(|&&m| m != 255 && m as usize >= coded) { |
| 81 | return Err(Error::corrupt(format!( |
| 82 | "opus multistream: mapping entry {bad} names a channel of {coded}" |
| 83 | ))); |
| 84 | } |
| 85 | let mut decoders = Vec::with_capacity(streams); |
| 86 | for s in 0..streams { |
| 87 | let ch = if s < coupled { 2 } else { 1 }; |
| 88 | decoders.push(Decoder::new(sample_rate, ch)?); |
| 89 | } |
| 90 | Ok(MultistreamDecoder { |
| 91 | streams: decoders, |
| 92 | coupled, |
| 93 | mapping: mapping.to_vec(), |
| 94 | channels: mapping.len(), |
| 95 | sample_rate, |
| 96 | buf: vec![0.0; 2 * 5760], |
| 97 | }) |
| 98 | } |
| 99 | |
| 100 | |
| 101 | pub fn channels(&self) -> usize { |
| 102 | self.channels |
| 103 | } |
| 104 | |
| 105 | |
| 106 | pub fn sample_rate(&self) -> u32 { |
| 107 | self.sample_rate |
| 108 | } |
| 109 | |
| 110 | |
| 111 | pub fn reset(&mut self) { |
| 112 | for s in self.streams.iter_mut() { |
| 113 | s.reset(); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | |
| 118 | |
| 119 | pub fn final_range(&self) -> u32 { |
| 120 | self.streams.iter().fold(0, |acc, s| acc ^ s.final_range()) |
| 121 | } |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | pub fn decode_packet(&mut self, data: &[u8]) -> Result<Vec<f32>> { |
| 130 | |
| 131 | let mut out = vec![0.0; 5760 * self.channels * self.sample_rate as usize / 48000]; |
| 132 | let n = self.decode_float(data, &mut out)?; |
| 133 | out.truncate(n * self.channels); |
| 134 | Ok(out) |
| 135 | } |
| 136 | |
| 137 | |
| 138 | |
| 139 | pub fn decode_float(&mut self, data: &[u8], out: &mut [f32]) -> Result<usize> { |
| 140 | |
| 141 | if data.len() < 2 * self.streams.len() - 1 { |
| 142 | return Err(Error::corrupt(format!( |
| 143 | "opus multistream: {} bytes for {} streams", |
| 144 | data.len(), |
| 145 | self.streams.len() |
| 146 | ))); |
| 147 | } |
| 148 | let mut rest = data; |
| 149 | let mut frame_size = 0usize; |
| 150 | let nb_streams = self.streams.len(); |
| 151 | for s in 0..nb_streams { |
| 152 | let last = s == nb_streams - 1; |
| 153 | let stream_channels = if s < self.coupled { 2 } else { 1 }; |
| 154 | let need = 5760 * stream_channels; |
| 155 | if self.buf.len() < need { |
| 156 | self.buf.resize(need, 0.0); |
| 157 | } |
| 158 | let (n, consumed) = if last { |
| 159 | ( |
| 160 | self.streams[s].decode_float(rest, &mut self.buf)?, |
| 161 | rest.len(), |
| 162 | ) |
| 163 | } else { |
| 164 | self.streams[s].decode_self_delimited(rest, &mut self.buf)? |
| 165 | }; |
| 166 | rest = &rest[consumed.min(rest.len())..]; |
| 167 | if s == 0 { |
| 168 | frame_size = n; |
| 169 | if out.len() < n * self.channels { |
| 170 | return Err(Error::corrupt(format!( |
| 171 | "opus multistream: output buffer holds {} samples, packet needs {}", |
| 172 | out.len(), |
| 173 | n * self.channels |
| 174 | ))); |
| 175 | } |
| 176 | } else if n != frame_size { |
| 177 | return Err(Error::corrupt(format!( |
| 178 | "opus multistream: stream {s} decoded {n} samples, stream 0 gave {frame_size}" |
| 179 | ))); |
| 180 | } |
| 181 | |
| 182 | for (c, &m) in self.mapping.iter().enumerate() { |
| 183 | if m == 255 { |
| 184 | continue; |
| 185 | } |
| 186 | let m = m as usize; |
| 187 | let src = if s < self.coupled { |
| 188 | if m == 2 * s { |
| 189 | Some(0) |
| 190 | } else if m == 2 * s + 1 { |
| 191 | Some(1) |
| 192 | } else { |
| 193 | None |
| 194 | } |
| 195 | } else if m == s + self.coupled { |
| 196 | Some(0) |
| 197 | } else { |
| 198 | None |
| 199 | }; |
| 200 | if let Some(off) = src { |
| 201 | for i in 0..frame_size { |
| 202 | out[self.channels * i + c] = self.buf[stream_channels * i + off]; |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | for (c, &m) in self.mapping.iter().enumerate() { |
| 209 | if m == 255 { |
| 210 | for i in 0..frame_size { |
| 211 | out[self.channels * i + c] = 0.0; |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | Ok(frame_size) |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | #[cfg(test)] |
| 220 | mod tests { |
| 221 | use super::*; |
| 222 | |
| 223 | #[test] |
| 224 | fn layouts_are_checked() { |
| 225 | |
| 226 | assert!(MultistreamDecoder::try_with_rate(48000, 4, 2, &[0, 4, 1, 2, 3, 5]).is_ok()); |
| 227 | |
| 228 | assert!(MultistreamDecoder::try_with_rate(48000, 2, 3, &[0, 1]).is_err()); |
| 229 | |
| 230 | |
| 231 | assert!(MultistreamDecoder::try_with_rate(48000, 4, 2, &[0, 6]).is_err()); |
| 232 | |
| 233 | assert!(MultistreamDecoder::try_with_rate(48000, 1, 0, &[0, 255]).is_ok()); |
| 234 | assert!(MultistreamDecoder::try_with_rate(44100, 1, 0, &[0]).is_err()); |
| 235 | } |
| 236 | |
| 237 | #[test] |
| 238 | fn truncated_packets_are_errors_not_panics() { |
| 239 | let mut dec = MultistreamDecoder::with_rate(48000, 4, 2, &[0, 4, 1, 2, 3, 5]); |
| 240 | for n in 0..40usize { |
| 241 | let data: Vec<u8> = (0..n).map(|i| (i * 37 + 11) as u8).collect(); |
| 242 | let _ = dec.decode_packet(&data); |
| 243 | } |
| 244 | } |
| 245 | } |