S1: PcmFormat enum replaces the is_pcm predicate and the wildcard arm in pcm()
Browse files at this commit93365f34a7b5aa31c5e948a297f9b02e82c25f2fParent: 4fd4ed41 changed file +58 -43
- crates/ec-probe/src/decoder.rs +58 -43
crates/ec-probe/src/decoder.rsmodified+58 -43
| @@ -26,7 +26,36 @@ enum Inner { | |||
| 26 | AmrWb(Box<ec_amr::wb::AmrWbDecoder>), | 26 | AmrWb(Box<ec_amr::wb::AmrWbDecoder>), |
| 27 | Opus(Box<ec_opus::MultistreamDecoder>), | 27 | Opus(Box<ec_opus::MultistreamDecoder>), |
| 28 | /// PCM needs no decoder, only a reading of the bytes. | 28 | /// PCM needs no decoder, only a reading of the bytes. |
| 29 | - Pcm(CodecId), | 29 | + Pcm(PcmFormat), |
| 30 | +} | ||
| 31 | + | ||
| 32 | +/// The PCM layouts read straight from the packet bytes. | ||
| 33 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
| 34 | +enum PcmFormat { | ||
| 35 | + U8, | ||
| 36 | + S16Le, | ||
| 37 | + S16Be, | ||
| 38 | + S24Le, | ||
| 39 | + S24Be, | ||
| 40 | + S32Le, | ||
| 41 | + S32Be, | ||
| 42 | + F32Le, | ||
| 43 | +} | ||
| 44 | + | ||
| 45 | +impl PcmFormat { | ||
| 46 | + fn of(codec: CodecId) -> Option<PcmFormat> { | ||
| 47 | + Some(match codec { | ||
| 48 | + CodecId::PcmU8 => PcmFormat::U8, | ||
| 49 | + CodecId::PcmS16Le => PcmFormat::S16Le, | ||
| 50 | + CodecId::PcmS16Be => PcmFormat::S16Be, | ||
| 51 | + CodecId::PcmS24Le => PcmFormat::S24Le, | ||
| 52 | + CodecId::PcmS24Be => PcmFormat::S24Be, | ||
| 53 | + CodecId::PcmS32Le => PcmFormat::S32Le, | ||
| 54 | + CodecId::PcmS32Be => PcmFormat::S32Be, | ||
| 55 | + CodecId::PcmF32Le => PcmFormat::F32Le, | ||
| 56 | + _ => return None, | ||
| 57 | + }) | ||
| 58 | + } | ||
| 30 | } | 59 | } |
| 31 | 60 | ||
| 32 | /// A decoder for one audio stream. | 61 | /// A decoder for one audio stream. |
| @@ -129,18 +158,20 @@ impl AudioDecoder { | |||
| 129 | 48_000, streams, coupled, &mapping, | 158 | 48_000, streams, coupled, &mapping, |
| 130 | )?)) | 159 | )?)) |
| 131 | } | 160 | } |
| 132 | - codec if is_pcm(codec) => Inner::Pcm(codec), | 161 | + codec => match PcmFormat::of(codec) { |
| 133 | - codec => { | 162 | + Some(format) => Inner::Pcm(format), |
| 134 | - return Err(Error::unsupported( | 163 | + None => { |
| 135 | - format!("the {} track", codec.name()), | 164 | + return Err(Error::unsupported( |
| 136 | - match codec.media_type() { | 165 | + format!("the {} track", codec.name()), |
| 137 | - ec_core::MediaType::Audio => { | 166 | + match codec.media_type() { |
| 138 | - "no decoder for it exists in this family (yet)" | 167 | + ec_core::MediaType::Audio => { |
| 139 | - } | 168 | + "no decoder for it exists in this family (yet)" |
| 140 | - _ => "this is an audio probe; picture and subtitles decode elsewhere", | 169 | + } |
| 141 | - }, | 170 | + _ => "this is an audio probe; picture and subtitles decode elsewhere", |
| 142 | - )); | 171 | + }, |
| 143 | - } | 172 | + )); |
| 173 | + } | ||
| 174 | + }, | ||
| 144 | }; | 175 | }; |
| 145 | Ok(AudioDecoder { | 176 | Ok(AudioDecoder { |
| 146 | inner, | 177 | inner, |
| @@ -213,7 +244,7 @@ impl AudioDecoder { | |||
| 213 | let samples = d.decode_packet(&packet.data)?; | 244 | let samples = d.decode_packet(&packet.data)?; |
| 214 | out.extend_from_slice(&samples); | 245 | out.extend_from_slice(&samples); |
| 215 | } | 246 | } |
| 216 | - Inner::Pcm(codec) => pcm(*codec, &packet.data, out), | 247 | + Inner::Pcm(format) => pcm(*format, &packet.data, out), |
| 217 | } | 248 | } |
| 218 | // Vorbis trims to its own page granules inside `ec-vorbis`, so its | 249 | // Vorbis trims to its own page granules inside `ec-vorbis`, so its |
| 219 | // output is final as it arrives. Every other codec here is trimmed | 250 | // output is final as it arrives. Every other codec here is trimmed |
| @@ -366,21 +397,6 @@ fn flac_stream_info(data: &[u8]) -> Option<&[u8]> { | |||
| 366 | None | 397 | None |
| 367 | } | 398 | } |
| 368 | 399 | ||
| 369 | -/// True for the PCM codec ids, which are read rather than decoded. | ||
| 370 | -fn is_pcm(codec: CodecId) -> bool { | ||
| 371 | - matches!( | ||
| 372 | - codec, | ||
| 373 | - CodecId::PcmU8 | ||
| 374 | - | CodecId::PcmS16Le | ||
| 375 | - | CodecId::PcmS16Be | ||
| 376 | - | CodecId::PcmS24Le | ||
| 377 | - | CodecId::PcmS24Be | ||
| 378 | - | CodecId::PcmS32Le | ||
| 379 | - | CodecId::PcmS32Be | ||
| 380 | - | CodecId::PcmF32Le | ||
| 381 | - ) | ||
| 382 | -} | ||
| 383 | - | ||
| 384 | /// Push one packet through an [`ec_core::Decoder`] and take every frame it | 400 | /// Push one packet through an [`ec_core::Decoder`] and take every frame it |
| 385 | /// yields, converted to interleaved `f32`. | 401 | /// yields, converted to interleaved `f32`. |
| 386 | fn drain(decoder: &mut dyn Decoder, packet: &Packet, out: &mut Vec<f32>) -> Result<()> { | 402 | fn drain(decoder: &mut dyn Decoder, packet: &Packet, out: &mut Vec<f32>) -> Result<()> { |
| @@ -449,39 +465,38 @@ fn interleave(frame: &ec_core::AudioFrame, out: &mut Vec<f32>) { | |||
| 449 | } | 465 | } |
| 450 | 466 | ||
| 451 | /// PCM bytes as normalised `f32`. | 467 | /// PCM bytes as normalised `f32`. |
| 452 | -fn pcm(codec: CodecId, data: &[u8], out: &mut Vec<f32>) { | 468 | +fn pcm(format: PcmFormat, data: &[u8], out: &mut Vec<f32>) { |
| 453 | - match codec { | 469 | + match format { |
| 454 | - CodecId::PcmU8 => out.extend(data.iter().map(|&b| (f32::from(b) - 128.0) / 128.0)), | 470 | + PcmFormat::U8 => out.extend(data.iter().map(|&b| (f32::from(b) - 128.0) / 128.0)), |
| 455 | - CodecId::PcmS16Le => out.extend( | 471 | + PcmFormat::S16Le => out.extend( |
| 456 | data.as_chunks::<2>().0.iter() | 472 | data.as_chunks::<2>().0.iter() |
| 457 | .map(|c| f32::from(i16::from_le_bytes([c[0], c[1]])) / 32768.0), | 473 | .map(|c| f32::from(i16::from_le_bytes([c[0], c[1]])) / 32768.0), |
| 458 | ), | 474 | ), |
| 459 | - CodecId::PcmS16Be => out.extend( | 475 | + PcmFormat::S16Be => out.extend( |
| 460 | data.as_chunks::<2>().0.iter() | 476 | data.as_chunks::<2>().0.iter() |
| 461 | .map(|c| f32::from(i16::from_be_bytes([c[0], c[1]])) / 32768.0), | 477 | .map(|c| f32::from(i16::from_be_bytes([c[0], c[1]])) / 32768.0), |
| 462 | ), | 478 | ), |
| 463 | // 24-bit is stored packed, little-endian, and sign-extends from bit 23. | 479 | // 24-bit is stored packed, little-endian, and sign-extends from bit 23. |
| 464 | - CodecId::PcmS24Le => out.extend(data.as_chunks::<3>().0.iter().map(|c| { | 480 | + PcmFormat::S24Le => out.extend(data.as_chunks::<3>().0.iter().map(|c| { |
| 465 | let v = i32::from_le_bytes([0, c[0], c[1], c[2]]); | 481 | let v = i32::from_le_bytes([0, c[0], c[1], c[2]]); |
| 466 | v as f32 / 2147483648.0 | 482 | v as f32 / 2147483648.0 |
| 467 | })), | 483 | })), |
| 468 | - CodecId::PcmS24Be => out.extend(data.as_chunks::<3>().0.iter().map(|c| { | 484 | + PcmFormat::S24Be => out.extend(data.as_chunks::<3>().0.iter().map(|c| { |
| 469 | let v = i32::from_be_bytes([c[0], c[1], c[2], 0]); | 485 | let v = i32::from_be_bytes([c[0], c[1], c[2], 0]); |
| 470 | v as f32 / 2147483648.0 | 486 | v as f32 / 2147483648.0 |
| 471 | })), | 487 | })), |
| 472 | - CodecId::PcmS32Le => out.extend( | 488 | + PcmFormat::S32Le => out.extend( |
| 473 | data.as_chunks::<4>().0.iter() | 489 | data.as_chunks::<4>().0.iter() |
| 474 | .map(|c| i32::from_le_bytes([c[0], c[1], c[2], c[3]]) as f32 / 2147483648.0), | 490 | .map(|c| i32::from_le_bytes([c[0], c[1], c[2], c[3]]) as f32 / 2147483648.0), |
| 475 | ), | 491 | ), |
| 476 | - CodecId::PcmS32Be => out.extend( | 492 | + PcmFormat::S32Be => out.extend( |
| 477 | data.as_chunks::<4>().0.iter() | 493 | data.as_chunks::<4>().0.iter() |
| 478 | .map(|c| i32::from_be_bytes([c[0], c[1], c[2], c[3]]) as f32 / 2147483648.0), | 494 | .map(|c| i32::from_be_bytes([c[0], c[1], c[2], c[3]]) as f32 / 2147483648.0), |
| 479 | ), | 495 | ), |
| 480 | - CodecId::PcmF32Le => out.extend( | 496 | + PcmFormat::F32Le => out.extend( |
| 481 | data.as_chunks::<4>().0.iter() | 497 | data.as_chunks::<4>().0.iter() |
| 482 | .map(|c| f32::from_le_bytes([c[0], c[1], c[2], c[3]])), | 498 | .map(|c| f32::from_le_bytes([c[0], c[1], c[2], c[3]])), |
| 483 | ), | 499 | ), |
| 484 | - _ => {} | ||
| 485 | } | 500 | } |
| 486 | } | 501 | } |
| 487 | 502 | ||
| @@ -579,15 +594,15 @@ mod tests { | |||
| 579 | #[test] | 594 | #[test] |
| 580 | fn pcm_reads_every_width_at_full_scale() { | 595 | fn pcm_reads_every_width_at_full_scale() { |
| 581 | let mut out = Vec::new(); | 596 | let mut out = Vec::new(); |
| 582 | - pcm(CodecId::PcmS16Le, &[0x00, 0x80, 0xff, 0x7f], &mut out); | 597 | + pcm(PcmFormat::S16Le, &[0x00, 0x80, 0xff, 0x7f], &mut out); |
| 583 | assert_eq!(out, vec![-1.0, 32767.0 / 32768.0]); | 598 | assert_eq!(out, vec![-1.0, 32767.0 / 32768.0]); |
| 584 | out.clear(); | 599 | out.clear(); |
| 585 | - pcm(CodecId::PcmU8, &[0, 128, 255], &mut out); | 600 | + pcm(PcmFormat::U8, &[0, 128, 255], &mut out); |
| 586 | assert_eq!(out[0], -1.0); | 601 | assert_eq!(out[0], -1.0); |
| 587 | assert_eq!(out[1], 0.0); | 602 | assert_eq!(out[1], 0.0); |
| 588 | out.clear(); | 603 | out.clear(); |
| 589 | // 24-bit sign-extends: 0x800000 is full-scale negative. | 604 | // 24-bit sign-extends: 0x800000 is full-scale negative. |
| 590 | - pcm(CodecId::PcmS24Le, &[0x00, 0x00, 0x80], &mut out); | 605 | + pcm(PcmFormat::S24Le, &[0x00, 0x00, 0x80], &mut out); |
| 591 | assert_eq!(out, vec![-1.0]); | 606 | assert_eq!(out, vec![-1.0]); |
| 592 | } | 607 | } |
| 593 | } | 608 | } |