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 {
22 pub samples: Vec<f32>,22 pub samples: Vec<f32>,
23 }23 }
24 24
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+
25 #[derive(Clone, Copy, Debug, Default)]48 #[derive(Clone, Copy, Debug, Default)]
26 struct GranuleInfo {49 struct GranuleInfo {
27 part2_3_length: u32,50 part2_3_length: u32,
@@ -29,7 +52,7 @@ struct GranuleInfo {
29 global_gain: u32,52 global_gain: u32,
30 scalefac_compress: u32,53 scalefac_compress: u32,
31 window_switching: bool,54 window_switching: bool,
32- block_type: u8,55+ block_type: BlockType,
33 mixed_block: bool,56 mixed_block: bool,
34 table_select: [u8; 3],57 table_select: [u8; 3],
35 subblock_gain: [u32; 3],58 subblock_gain: [u32; 3],
@@ -86,7 +109,7 @@ fn parse_side_info(header: &FrameHeader, bytes: &[u8]) -> Result<SideInfo> {
86 )));109 )));
87 }110 }
88 if g.window_switching {111 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)?);
90 g.mixed_block = r.read_bit()?;113 g.mixed_block = r.read_bit()?;
91 for slot in &mut g.table_select[..2] {114 for slot in &mut g.table_select[..2] {
92 *slot = r.read_bits(5)? as u8;115 *slot = r.read_bits(5)? as u8;
@@ -94,13 +117,13 @@ fn parse_side_info(header: &FrameHeader, bytes: &[u8]) -> Result<SideInfo> {
94 for slot in &mut g.subblock_gain {117 for slot in &mut g.subblock_gain {
95 *slot = r.read_bits(3)?;118 *slot = r.read_bits(3)?;
96 }119 }
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 {
98 8121 8
99 } else {122 } else {
100 7123 7
101 };124 };
102 g.region1_count = 20 - g.region0_count;125 g.region1_count = 20 - g.region0_count;
103- if g.block_type == 0 {126+ if g.block_type == BlockType::Normal {
104 return Err(Error::corrupt(127 return Err(Error::corrupt(
105 "mp3: window switching with normal block type",128 "mp3: window switching with normal block type",
106 ));129 ));
@@ -309,7 +332,7 @@ impl Mp3Decode {
309 ) -> Result<()> {332 ) -> Result<()> {
310 let granule = side.granules[gr][ch];333 let granule = side.granules[gr][ch];
311 let (slen1, slen2) = SLEN[(granule.scalefac_compress & 15) as usize];334 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 {
313 if granule.mixed_block {336 if granule.mixed_block {
314 for sfb in 0..8 {337 for sfb in 0..8 {
315 self.scalefac_l[ch][sfb] = reader.read_bits(slen1)? as u8;338 self.scalefac_l[ch][sfb] = reader.read_bits(slen1)? as u8;
@@ -386,7 +409,7 @@ impl Mp3Decode {
386 slen = [sfc / 3, sfc % 3, 0, 0];409 slen = [sfc / 3, sfc % 3, 0, 0];
387 block_number = 2;410 block_number = 2;
388 }411 }
389- let short = granule.window_switching && granule.block_type == 2;412+ let short = granule.block_type == BlockType::Short;
390 let kind = if !short {413 let kind = if !short {
391 0414 0
392 } else if granule.mixed_block {415 } else if granule.mixed_block {
@@ -427,7 +450,7 @@ impl Mp3Decode {
427 let is = &mut self.is[ch];450 let is = &mut self.is[ch];
428 is.fill(0);451 is.fill(0);
429 let starts = long_starts(header.sample_rate);452 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;
431 let (region1, region2) = if granule.window_switching {454 let (region1, region2) = if granule.window_switching {
432 if short {455 if short {
433 // Three short bands, all three windows: 36 lines at every rate456 // Three short bands, all three windows: 36 lines at every rate
@@ -493,7 +516,7 @@ impl Mp3Decode {
493 let power = power43();516 let power = power43();
494 let xr = &mut self.xr[ch];517 let xr = &mut self.xr[ch];
495 let is = &self.is[ch];518 let is = &self.is[ch];
496- let short = granule.window_switching && granule.block_type == 2;519+ let short = granule.block_type == BlockType::Short;
497 let multiplier = if granule.scalefac_scale { 1.0 } else { 0.5 };520 let multiplier = if granule.scalefac_scale { 1.0 } else { 0.5 };
498 let base = (granule.global_gain as f32 - 210.0) * 0.25;521 let base = (granule.global_gain as f32 - 210.0) * 0.25;
499 let value = |v: i32| -> f32 {522 let value = |v: i32| -> f32 {
@@ -566,7 +589,7 @@ impl Mp3Decode {
566 let ms = joint && header.mode_ext & 2 != 0;589 let ms = joint && header.mode_ext & 2 != 0;
567 let intensity = joint && header.mode_ext & 1 != 0;590 let intensity = joint && header.mode_ext & 1 != 0;
568 let granule = side.granules[gr][0];591 let granule = side.granules[gr][0];
569- let short = granule.window_switching && granule.block_type == 2;592+ let short = granule.block_type == BlockType::Short;
570 // Intensity coding starts at the first band above the right channel's593 // Intensity coding starts at the first band above the right channel's
571 // last non-zero line.594 // last non-zero line.
572 let bound = if intensity {595 let bound = if intensity {
@@ -624,7 +647,7 @@ impl Mp3Decode {
624 channels: usize,647 channels: usize,
625 out: &mut [f32],648 out: &mut [f32],
626 ) {649 ) {
627- let short = granule.window_switching && granule.block_type == 2;650+ let short = granule.block_type == BlockType::Short;
628 let alias_bands = if !short {651 let alias_bands = if !short {
629 32652 32
630 } else if granule.mixed_block {653 } else if granule.mixed_block {
@@ -643,8 +666,8 @@ impl Mp3Decode {
643 if short && !(granule.mixed_block && sb < 2) {666 if short && !(granule.mixed_block && sb < 2) {
644 self.imdct.short(coefficients, &mut block);667 self.imdct.short(coefficients, &mut block);
645 } else {668 } else {
646- let block_type = if short { 0 } else { granule.block_type };669+ let block_type = if short { BlockType::Normal } else { granule.block_type };
647- self.imdct.long(coefficients, block_type, &mut block);670+ self.imdct.long(coefficients, block_type as u8, &mut block);
648 }671 }
649 let overlap = &mut self.overlap[ch][sb];672 let overlap = &mut self.overlap[ch][sb];
650 for t in 0..18 {673 for t in 0..18 {