forge
Explore
Log in

Don't you dare go Hollow

S10: mp3 granule block type parsed once into a BlockType enum; the window_switching && block_type==2 checks collapse to one comparison (non-switching granules already hold Normal)

Browse files at this commit

8b1df89965ac8b0804d675fb1ebe68709a6709cfAhmet Kaan GümüşParent: 9279b9f
crates/ec-mp3/src/decode.rsmodified+35 -12
@@ -22,6 +22,29 @@ pub struct DecodedFrame {
2222 pub samples: Vec<f32>,
2323 }
2424
25+/// `block_type` of a granule with window switching. Normal when the granule
26+/// does not window-switch. The discriminant indexes the IMDCT window table.
27+#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
28+#[repr(u8)]
29+enum BlockType {
30+ #[default]
31+ Normal = 0,
32+ Start = 1,
33+ Short = 2,
34+ Stop = 3,
35+}
36+
37+impl BlockType {
38+ fn from_bits(v: u32) -> BlockType {
39+ match v {
40+ 0 => BlockType::Normal,
41+ 1 => BlockType::Start,
42+ 2 => BlockType::Short,
43+ _ => BlockType::Stop,
44+ }
45+ }
46+}
47+
2548 #[derive(Clone, Copy, Debug, Default)]
2649 struct GranuleInfo {
2750 part2_3_length: u32,
@@ -29,7 +52,7 @@ struct GranuleInfo {
2952 global_gain: u32,
3053 scalefac_compress: u32,
3154 window_switching: bool,
32- block_type: u8,
55+ block_type: BlockType,
3356 mixed_block: bool,
3457 table_select: [u8; 3],
3558 subblock_gain: [u32; 3],
@@ -86,7 +109,7 @@ fn parse_side_info(header: &FrameHeader, bytes: &[u8]) -> Result<SideInfo> {
86109 )));
87110 }
88111 if g.window_switching {
89- g.block_type = r.read_bits(2)? as u8;
112+ g.block_type = BlockType::from_bits(r.read_bits(2)?);
90113 g.mixed_block = r.read_bit()?;
91114 for slot in &mut g.table_select[..2] {
92115 *slot = r.read_bits(5)? as u8;
@@ -94,13 +117,13 @@ fn parse_side_info(header: &FrameHeader, bytes: &[u8]) -> Result<SideInfo> {
94117 for slot in &mut g.subblock_gain {
95118 *slot = r.read_bits(3)?;
96119 }
97- g.region0_count = if g.block_type == 2 && !g.mixed_block {
120+ g.region0_count = if g.block_type == BlockType::Short && !g.mixed_block {
98121 8
99122 } else {
100123 7
101124 };
102125 g.region1_count = 20 - g.region0_count;
103- if g.block_type == 0 {
126+ if g.block_type == BlockType::Normal {
104127 return Err(Error::corrupt(
105128 "mp3: window switching with normal block type",
106129 ));
@@ -309,7 +332,7 @@ impl Mp3Decode {
309332 ) -> Result<()> {
310333 let granule = side.granules[gr][ch];
311334 let (slen1, slen2) = SLEN[(granule.scalefac_compress & 15) as usize];
312- if granule.window_switching && granule.block_type == 2 {
335+ if granule.block_type == BlockType::Short {
313336 if granule.mixed_block {
314337 for sfb in 0..8 {
315338 self.scalefac_l[ch][sfb] = reader.read_bits(slen1)? as u8;
@@ -386,7 +409,7 @@ impl Mp3Decode {
386409 slen = [sfc / 3, sfc % 3, 0, 0];
387410 block_number = 2;
388411 }
389- let short = granule.window_switching && granule.block_type == 2;
412+ let short = granule.block_type == BlockType::Short;
390413 let kind = if !short {
391414 0
392415 } else if granule.mixed_block {
@@ -427,7 +450,7 @@ impl Mp3Decode {
427450 let is = &mut self.is[ch];
428451 is.fill(0);
429452 let starts = long_starts(header.sample_rate);
430- let short = granule.window_switching && granule.block_type == 2;
453+ let short = granule.block_type == BlockType::Short;
431454 let (region1, region2) = if granule.window_switching {
432455 if short {
433456 // Three short bands, all three windows: 36 lines at every rate
@@ -493,7 +516,7 @@ impl Mp3Decode {
493516 let power = power43();
494517 let xr = &mut self.xr[ch];
495518 let is = &self.is[ch];
496- let short = granule.window_switching && granule.block_type == 2;
519+ let short = granule.block_type == BlockType::Short;
497520 let multiplier = if granule.scalefac_scale { 1.0 } else { 0.5 };
498521 let base = (granule.global_gain as f32 - 210.0) * 0.25;
499522 let value = |v: i32| -> f32 {
@@ -566,7 +589,7 @@ impl Mp3Decode {
566589 let ms = joint && header.mode_ext & 2 != 0;
567590 let intensity = joint && header.mode_ext & 1 != 0;
568591 let granule = side.granules[gr][0];
569- let short = granule.window_switching && granule.block_type == 2;
592+ let short = granule.block_type == BlockType::Short;
570593 // Intensity coding starts at the first band above the right channel's
571594 // last non-zero line.
572595 let bound = if intensity {
@@ -624,7 +647,7 @@ impl Mp3Decode {
624647 channels: usize,
625648 out: &mut [f32],
626649 ) {
627- let short = granule.window_switching && granule.block_type == 2;
650+ let short = granule.block_type == BlockType::Short;
628651 let alias_bands = if !short {
629652 32
630653 } else if granule.mixed_block {
@@ -643,8 +666,8 @@ impl Mp3Decode {
643666 if short && !(granule.mixed_block && sb < 2) {
644667 self.imdct.short(coefficients, &mut block);
645668 } else {
646- let block_type = if short { 0 } else { granule.block_type };
647- self.imdct.long(coefficients, block_type, &mut block);
669+ let block_type = if short { BlockType::Normal } else { granule.block_type };
670+ self.imdct.long(coefficients, block_type as u8, &mut block);
648671 }
649672 let overlap = &mut self.overlap[ch][sb];
650673 for t in 0..18 {