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 commit8b1df89965ac8b0804d675fb1ebe68709a6709cfParent: 9279b9f1 changed file +35 -12
- crates/ec-mp3/src/decode.rs +35 -12
crates/ec-mp3/src/decode.rsmodified+35 -12
| @@ -22,6 +22,29 @@ pub struct DecodedFrame { | ||
| 22 | 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 | 48 | #[derive(Clone, Copy, Debug, Default)] |
| 26 | 49 | struct GranuleInfo { |
| 27 | 50 | part2_3_length: u32, |
| @@ -29,7 +52,7 @@ struct GranuleInfo { | ||
| 29 | 52 | global_gain: u32, |
| 30 | 53 | scalefac_compress: u32, |
| 31 | 54 | window_switching: bool, |
| 32 | - block_type: u8, | |
| 55 | + block_type: BlockType, | |
| 33 | 56 | mixed_block: bool, |
| 34 | 57 | table_select: [u8; 3], |
| 35 | 58 | subblock_gain: [u32; 3], |
| @@ -86,7 +109,7 @@ fn parse_side_info(header: &FrameHeader, bytes: &[u8]) -> Result<SideInfo> { | ||
| 86 | 109 | ))); |
| 87 | 110 | } |
| 88 | 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 | 113 | g.mixed_block = r.read_bit()?; |
| 91 | 114 | for slot in &mut g.table_select[..2] { |
| 92 | 115 | *slot = r.read_bits(5)? as u8; |
| @@ -94,13 +117,13 @@ fn parse_side_info(header: &FrameHeader, bytes: &[u8]) -> Result<SideInfo> { | ||
| 94 | 117 | for slot in &mut g.subblock_gain { |
| 95 | 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 | 121 | 8 |
| 99 | 122 | } else { |
| 100 | 123 | 7 |
| 101 | 124 | }; |
| 102 | 125 | g.region1_count = 20 - g.region0_count; |
| 103 | - if g.block_type == 0 { | |
| 126 | + if g.block_type == BlockType::Normal { | |
| 104 | 127 | return Err(Error::corrupt( |
| 105 | 128 | "mp3: window switching with normal block type", |
| 106 | 129 | )); |
| @@ -309,7 +332,7 @@ impl Mp3Decode { | ||
| 309 | 332 | ) -> Result<()> { |
| 310 | 333 | let granule = side.granules[gr][ch]; |
| 311 | 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 | 336 | if granule.mixed_block { |
| 314 | 337 | for sfb in 0..8 { |
| 315 | 338 | self.scalefac_l[ch][sfb] = reader.read_bits(slen1)? as u8; |
| @@ -386,7 +409,7 @@ impl Mp3Decode { | ||
| 386 | 409 | slen = [sfc / 3, sfc % 3, 0, 0]; |
| 387 | 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 | 413 | let kind = if !short { |
| 391 | 414 | 0 |
| 392 | 415 | } else if granule.mixed_block { |
| @@ -427,7 +450,7 @@ impl Mp3Decode { | ||
| 427 | 450 | let is = &mut self.is[ch]; |
| 428 | 451 | is.fill(0); |
| 429 | 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 | 454 | let (region1, region2) = if granule.window_switching { |
| 432 | 455 | if short { |
| 433 | 456 | // Three short bands, all three windows: 36 lines at every rate |
| @@ -493,7 +516,7 @@ impl Mp3Decode { | ||
| 493 | 516 | let power = power43(); |
| 494 | 517 | let xr = &mut self.xr[ch]; |
| 495 | 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 | 520 | let multiplier = if granule.scalefac_scale { 1.0 } else { 0.5 }; |
| 498 | 521 | let base = (granule.global_gain as f32 - 210.0) * 0.25; |
| 499 | 522 | let value = |v: i32| -> f32 { |
| @@ -566,7 +589,7 @@ impl Mp3Decode { | ||
| 566 | 589 | let ms = joint && header.mode_ext & 2 != 0; |
| 567 | 590 | let intensity = joint && header.mode_ext & 1 != 0; |
| 568 | 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 | 593 | // Intensity coding starts at the first band above the right channel's |
| 571 | 594 | // last non-zero line. |
| 572 | 595 | let bound = if intensity { |
| @@ -624,7 +647,7 @@ impl Mp3Decode { | ||
| 624 | 647 | channels: usize, |
| 625 | 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 | 651 | let alias_bands = if !short { |
| 629 | 652 | 32 |
| 630 | 653 | } else if granule.mixed_block { |
| @@ -643,8 +666,8 @@ impl Mp3Decode { | ||
| 643 | 666 | if short && !(granule.mixed_block && sb < 2) { |
| 644 | 667 | self.imdct.short(coefficients, &mut block); |
| 645 | 668 | } 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); | |
| 648 | 671 | } |
| 649 | 672 | let overlap = &mut self.overlap[ch][sb]; |
| 650 | 673 | for t in 0..18 { |