diff --git a/crates/ec-mp3/src/decode.rs b/crates/ec-mp3/src/decode.rs index 37dd3618..2703c43f 100644 --- a/crates/ec-mp3/src/decode.rs +++ b/crates/ec-mp3/src/decode.rs @@ -22,6 +22,29 @@ pub struct DecodedFrame { pub samples: Vec, } +/// `block_type` of a granule with window switching. Normal when the granule +/// does not window-switch. The discriminant indexes the IMDCT window table. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +#[repr(u8)] +enum BlockType { + #[default] + Normal = 0, + Start = 1, + Short = 2, + Stop = 3, +} + +impl BlockType { + fn from_bits(v: u32) -> BlockType { + match v { + 0 => BlockType::Normal, + 1 => BlockType::Start, + 2 => BlockType::Short, + _ => BlockType::Stop, + } + } +} + #[derive(Clone, Copy, Debug, Default)] struct GranuleInfo { part2_3_length: u32, @@ -29,7 +52,7 @@ struct GranuleInfo { global_gain: u32, scalefac_compress: u32, window_switching: bool, - block_type: u8, + block_type: BlockType, mixed_block: bool, table_select: [u8; 3], subblock_gain: [u32; 3], @@ -86,7 +109,7 @@ fn parse_side_info(header: &FrameHeader, bytes: &[u8]) -> Result { ))); } if g.window_switching { - g.block_type = r.read_bits(2)? as u8; + g.block_type = BlockType::from_bits(r.read_bits(2)?); g.mixed_block = r.read_bit()?; for slot in &mut g.table_select[..2] { *slot = r.read_bits(5)? as u8; @@ -94,13 +117,13 @@ fn parse_side_info(header: &FrameHeader, bytes: &[u8]) -> Result { for slot in &mut g.subblock_gain { *slot = r.read_bits(3)?; } - g.region0_count = if g.block_type == 2 && !g.mixed_block { + g.region0_count = if g.block_type == BlockType::Short && !g.mixed_block { 8 } else { 7 }; g.region1_count = 20 - g.region0_count; - if g.block_type == 0 { + if g.block_type == BlockType::Normal { return Err(Error::corrupt( "mp3: window switching with normal block type", )); @@ -309,7 +332,7 @@ impl Mp3Decode { ) -> Result<()> { let granule = side.granules[gr][ch]; let (slen1, slen2) = SLEN[(granule.scalefac_compress & 15) as usize]; - if granule.window_switching && granule.block_type == 2 { + if granule.block_type == BlockType::Short { if granule.mixed_block { for sfb in 0..8 { self.scalefac_l[ch][sfb] = reader.read_bits(slen1)? as u8; @@ -386,7 +409,7 @@ impl Mp3Decode { slen = [sfc / 3, sfc % 3, 0, 0]; block_number = 2; } - let short = granule.window_switching && granule.block_type == 2; + let short = granule.block_type == BlockType::Short; let kind = if !short { 0 } else if granule.mixed_block { @@ -427,7 +450,7 @@ impl Mp3Decode { let is = &mut self.is[ch]; is.fill(0); let starts = long_starts(header.sample_rate); - let short = granule.window_switching && granule.block_type == 2; + let short = granule.block_type == BlockType::Short; let (region1, region2) = if granule.window_switching { if short { // Three short bands, all three windows: 36 lines at every rate @@ -493,7 +516,7 @@ impl Mp3Decode { let power = power43(); let xr = &mut self.xr[ch]; let is = &self.is[ch]; - let short = granule.window_switching && granule.block_type == 2; + let short = granule.block_type == BlockType::Short; let multiplier = if granule.scalefac_scale { 1.0 } else { 0.5 }; let base = (granule.global_gain as f32 - 210.0) * 0.25; let value = |v: i32| -> f32 { @@ -566,7 +589,7 @@ impl Mp3Decode { let ms = joint && header.mode_ext & 2 != 0; let intensity = joint && header.mode_ext & 1 != 0; let granule = side.granules[gr][0]; - let short = granule.window_switching && granule.block_type == 2; + let short = granule.block_type == BlockType::Short; // Intensity coding starts at the first band above the right channel's // last non-zero line. let bound = if intensity { @@ -624,7 +647,7 @@ impl Mp3Decode { channels: usize, out: &mut [f32], ) { - let short = granule.window_switching && granule.block_type == 2; + let short = granule.block_type == BlockType::Short; let alias_bands = if !short { 32 } else if granule.mixed_block { @@ -643,8 +666,8 @@ impl Mp3Decode { if short && !(granule.mixed_block && sb < 2) { self.imdct.short(coefficients, &mut block); } else { - let block_type = if short { 0 } else { granule.block_type }; - self.imdct.long(coefficients, block_type, &mut block); + let block_type = if short { BlockType::Normal } else { granule.block_type }; + self.imdct.long(coefficients, block_type as u8, &mut block); } let overlap = &mut self.overlap[ch][sb]; for t in 0..18 {