| 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::packet::Packet; |
| 24 | use crate::Decoder; |
| 25 | |
| 26 | |
| 27 | #[derive(Debug)] |
| 28 | pub struct MultistreamDecoder { |
| 29 | streams: Vec<Decoder>, |
| 30 | coupled: usize, |
| 31 | mapping: Vec<u8>, |
| 32 | channels: usize, |
| 33 | sample_rate: u32, |
| 34 | |
| 35 | buf: Vec<f32>, |
| 36 | |
| 37 | fec: Vec<u8>, |
| 38 | } |
| 39 | |
| 40 | impl MultistreamDecoder { |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | pub fn with_rate( |
| 52 | sample_rate: u32, |
| 53 | streams: usize, |
| 54 | coupled: usize, |
| 55 | mapping: &[u8], |
| 56 | ) -> MultistreamDecoder { |
| 57 | match Self::try_with_rate(sample_rate, streams, coupled, mapping) { |
| 58 | Ok(d) => d, |
| 59 | Err(e) => panic!("invalid Opus multistream layout: {e}"), |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | |
| 64 | |
| 65 | pub fn try_with_rate( |
| 66 | sample_rate: u32, |
| 67 | streams: usize, |
| 68 | coupled: usize, |
| 69 | mapping: &[u8], |
| 70 | ) -> Result<MultistreamDecoder> { |
| 71 | if streams == 0 || streams > 255 || coupled > streams { |
| 72 | return Err(Error::corrupt(format!( |
| 73 | "opus multistream: {streams} streams of which {coupled} coupled" |
| 74 | ))); |
| 75 | } |
| 76 | let coded = coupled + streams; |
| 77 | if mapping.is_empty() || mapping.len() > 255 { |
| 78 | return Err(Error::corrupt(format!( |
| 79 | "opus multistream: {} output channels", |
| 80 | mapping.len() |
| 81 | ))); |
| 82 | } |
| 83 | if let Some(&bad) = mapping.iter().find(|&&m| m != 255 && m as usize >= coded) { |
| 84 | return Err(Error::corrupt(format!( |
| 85 | "opus multistream: mapping entry {bad} names a channel of {coded}" |
| 86 | ))); |
| 87 | } |
| 88 | let mut decoders = Vec::with_capacity(streams); |
| 89 | for s in 0..streams { |
| 90 | let ch = if s < coupled { 2 } else { 1 }; |
| 91 | decoders.push(Decoder::new(sample_rate, ch)?); |
| 92 | } |
| 93 | Ok(MultistreamDecoder { |
| 94 | streams: decoders, |
| 95 | coupled, |
| 96 | mapping: mapping.to_vec(), |
| 97 | channels: mapping.len(), |
| 98 | sample_rate, |
| 99 | buf: vec![0.0; 2 * 5760], |
| 100 | fec: Vec::with_capacity(1276), |
| 101 | }) |
| 102 | } |
| 103 | |
| 104 | |
| 105 | pub fn channels(&self) -> usize { |
| 106 | self.channels |
| 107 | } |
| 108 | |
| 109 | |
| 110 | pub fn sample_rate(&self) -> u32 { |
| 111 | self.sample_rate |
| 112 | } |
| 113 | |
| 114 | |
| 115 | pub fn reset(&mut self) { |
| 116 | for s in self.streams.iter_mut() { |
| 117 | s.reset(); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | |
| 122 | |
| 123 | pub fn final_range(&self) -> u32 { |
| 124 | self.streams.iter().fold(0, |acc, s| acc ^ s.final_range()) |
| 125 | } |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | pub fn decode_packet(&mut self, data: &[u8]) -> Result<Vec<f32>> { |
| 134 | |
| 135 | let mut out = vec![0.0; 5760 * self.channels * self.sample_rate as usize / 48000]; |
| 136 | let n = self.decode_float(data, &mut out)?; |
| 137 | out.truncate(n * self.channels); |
| 138 | Ok(out) |
| 139 | } |
| 140 | |
| 141 | |
| 142 | |
| 143 | pub fn decode_float(&mut self, data: &[u8], out: &mut [f32]) -> Result<usize> { |
| 144 | |
| 145 | if data.len() < 2 * self.streams.len() - 1 { |
| 146 | return Err(Error::corrupt(format!( |
| 147 | "opus multistream: {} bytes for {} streams", |
| 148 | data.len(), |
| 149 | self.streams.len() |
| 150 | ))); |
| 151 | } |
| 152 | let mut rest = data; |
| 153 | let mut frame_size = 0usize; |
| 154 | let nb_streams = self.streams.len(); |
| 155 | for s in 0..nb_streams { |
| 156 | let last = s == nb_streams - 1; |
| 157 | let stream_channels = if s < self.coupled { 2 } else { 1 }; |
| 158 | let need = 5760 * stream_channels; |
| 159 | if self.buf.len() < need { |
| 160 | self.buf.resize(need, 0.0); |
| 161 | } |
| 162 | let (n, consumed) = if last { |
| 163 | ( |
| 164 | self.streams[s].decode_float(rest, &mut self.buf)?, |
| 165 | rest.len(), |
| 166 | ) |
| 167 | } else { |
| 168 | self.streams[s].decode_self_delimited(rest, &mut self.buf)? |
| 169 | }; |
| 170 | rest = &rest[consumed.min(rest.len())..]; |
| 171 | if s == 0 { |
| 172 | frame_size = n; |
| 173 | self.check_out(frame_size, out)?; |
| 174 | } else if n != frame_size { |
| 175 | return Err(Error::corrupt(format!( |
| 176 | "opus multistream: stream {s} decoded {n} samples, stream 0 gave {frame_size}" |
| 177 | ))); |
| 178 | } |
| 179 | self.route(s, frame_size, out); |
| 180 | } |
| 181 | self.silence(frame_size, out); |
| 182 | Ok(frame_size) |
| 183 | } |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | pub fn conceal(&mut self, frame_size: usize, out: &mut [f32]) -> Result<usize> { |
| 189 | self.check_out(frame_size, out)?; |
| 190 | for s in 0..self.streams.len() { |
| 191 | self.streams[s].conceal(frame_size, &mut self.buf)?; |
| 192 | self.route(s, frame_size, out); |
| 193 | } |
| 194 | self.silence(frame_size, out); |
| 195 | Ok(frame_size) |
| 196 | } |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | pub fn decode_fec( |
| 203 | &mut self, |
| 204 | next_packet: &[u8], |
| 205 | frame_size: usize, |
| 206 | out: &mut [f32], |
| 207 | ) -> Result<usize> { |
| 208 | self.check_out(frame_size, out)?; |
| 209 | let nb_streams = self.streams.len(); |
| 210 | let mut rest = next_packet; |
| 211 | for s in 0..nb_streams { |
| 212 | let last = s == nb_streams - 1; |
| 213 | match first_frame(rest, !last, &mut self.fec) { |
| 214 | Ok(consumed) => { |
| 215 | rest = &rest[consumed.min(rest.len())..]; |
| 216 | self.streams[s].decode_fec(&self.fec, frame_size, &mut self.buf)?; |
| 217 | } |
| 218 | Err(_) => { |
| 219 | rest = &[]; |
| 220 | self.streams[s].conceal(frame_size, &mut self.buf)?; |
| 221 | } |
| 222 | } |
| 223 | self.route(s, frame_size, out); |
| 224 | } |
| 225 | self.silence(frame_size, out); |
| 226 | Ok(frame_size) |
| 227 | } |
| 228 | |
| 229 | fn check_out(&self, frame_size: usize, out: &[f32]) -> Result<()> { |
| 230 | if out.len() < frame_size * self.channels { |
| 231 | return Err(Error::corrupt(format!( |
| 232 | "opus multistream: output buffer holds {} samples, packet needs {}", |
| 233 | out.len(), |
| 234 | frame_size * self.channels |
| 235 | ))); |
| 236 | } |
| 237 | Ok(()) |
| 238 | } |
| 239 | |
| 240 | |
| 241 | |
| 242 | fn route(&self, s: usize, frame_size: usize, out: &mut [f32]) { |
| 243 | let stream_channels = if s < self.coupled { 2 } else { 1 }; |
| 244 | for (c, &m) in self.mapping.iter().enumerate() { |
| 245 | if m == 255 { |
| 246 | continue; |
| 247 | } |
| 248 | let m = m as usize; |
| 249 | let src = if s < self.coupled { |
| 250 | if m == 2 * s { |
| 251 | Some(0) |
| 252 | } else if m == 2 * s + 1 { |
| 253 | Some(1) |
| 254 | } else { |
| 255 | None |
| 256 | } |
| 257 | } else if m == s + self.coupled { |
| 258 | Some(0) |
| 259 | } else { |
| 260 | None |
| 261 | }; |
| 262 | if let Some(off) = src { |
| 263 | for i in 0..frame_size { |
| 264 | out[self.channels * i + c] = self.buf[stream_channels * i + off]; |
| 265 | } |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | |
| 271 | fn silence(&self, frame_size: usize, out: &mut [f32]) { |
| 272 | for (c, &m) in self.mapping.iter().enumerate() { |
| 273 | if m == 255 { |
| 274 | for i in 0..frame_size { |
| 275 | out[self.channels * i + c] = 0.0; |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | |
| 283 | |
| 284 | |
| 285 | fn first_frame(data: &[u8], self_delimited: bool, scratch: &mut Vec<u8>) -> Result<usize> { |
| 286 | let packet = Packet::parse(data, self_delimited)?; |
| 287 | let first: &[u8] = packet.frames.first().copied().unwrap_or(&[]); |
| 288 | let toc = packet.toc; |
| 289 | scratch.clear(); |
| 290 | scratch.push((toc.config << 3) | (u8::from(toc.stereo) << 2)); |
| 291 | scratch.extend_from_slice(first); |
| 292 | Ok(packet.consumed) |
| 293 | } |
| 294 | |
| 295 | #[cfg(test)] |
| 296 | mod tests { |
| 297 | use super::*; |
| 298 | |
| 299 | #[test] |
| 300 | fn layouts_are_checked() { |
| 301 | |
| 302 | assert!(MultistreamDecoder::try_with_rate(48000, 4, 2, &[0, 4, 1, 2, 3, 5]).is_ok()); |
| 303 | |
| 304 | assert!(MultistreamDecoder::try_with_rate(48000, 2, 3, &[0, 1]).is_err()); |
| 305 | |
| 306 | |
| 307 | assert!(MultistreamDecoder::try_with_rate(48000, 4, 2, &[0, 6]).is_err()); |
| 308 | |
| 309 | assert!(MultistreamDecoder::try_with_rate(48000, 1, 0, &[0, 255]).is_ok()); |
| 310 | assert!(MultistreamDecoder::try_with_rate(44100, 1, 0, &[0]).is_err()); |
| 311 | } |
| 312 | |
| 313 | #[test] |
| 314 | fn truncated_packets_are_errors_not_panics() { |
| 315 | let mut dec = MultistreamDecoder::with_rate(48000, 4, 2, &[0, 4, 1, 2, 3, 5]); |
| 316 | for n in 0..40usize { |
| 317 | let data: Vec<u8> = (0..n).map(|i| (i * 37 + 11) as u8).collect(); |
| 318 | let _ = dec.decode_packet(&data); |
| 319 | } |
| 320 | } |
| 321 | } |