/// Reads the scale factors for a single channel in a granule in a MPEG version 2 audio frame.
Reads the scale factors for a single channel in a granule in a MPEG version 2 audio frame.
Reads the scale factors for a single channel in a granule in a MPEG version 2 audio frame .
pub(super) fn read_scale_factors_mpeg2<B: BitStream>(
    bs: &mut B,
    is_intensity_stereo: bool,
    channel: &mut GranuleChannel,
) -> Result<u32> {
    let mut bits_read = 0;
    let block_index = match channel.block_type {
        BlockType::Short{ is_mixed: true  } => 2,
        BlockType::Short{ is_mixed: false } => 1,
        _                                   => 0,
    };
    let (slen_table, nsfb_table) = if is_intensity_stereo {
        // The actual value of scalefac_compress is a 9-bit unsigned integer (0..512) for MPEG2. A
        // left shift reduces it to an 8-bit value (0..255).
        let sfc = u32::from(channel.scalefac_compress) >> 1;
        match sfc {
            0..=179   => ([
                (sfc / 36),
                (sfc % 36) / 6,
                (sfc % 36) % 6,
                0,
            ],
            &SCALE_FACTOR_MPEG2_NSFB[0][block_index]),
            180..=243 => ([
                ((sfc - 180) % 64) >> 4,
                ((sfc - 180) % 16) >> 2,
                ((sfc - 180) %  4),
                0,
            ],
            &SCALE_FACTOR_MPEG2_NSFB[1][block_index]),
            244..=255 => ([
                (sfc - 244) / 3,
                (sfc - 244) % 3,
                0,
                0,
            ],
            &SCALE_FACTOR_MPEG2_NSFB[2][block_index]),
            _ => unreachable!(),
        }
    }
    else {
        // The actual value of scalefac_compress is a 9-bit unsigned integer (0..512) for MPEG2.
        let sfc = u32::from(channel.scalefac_compress);
        match sfc {
            0..=399   => ([
                (sfc >> 4) / 5,
                (sfc >> 4) % 5,
                (sfc % 16) >> 2,
                (sfc %  4),
            ],
            &SCALE_FACTOR_MPEG2_NSFB[3][block_index]),
            400..=499 => ([
                ((sfc - 400) >> 2) / 5,
                ((sfc - 400) >> 2) % 5,
                (sfc - 400) % 4,
                0,
            ],
            &SCALE_FACTOR_MPEG2_NSFB[4][block_index]),
            500..=512 => ([
                (sfc - 500) / 3,
                (sfc - 500) % 3,
                0,
                0,
            ],
            &SCALE_FACTOR_MPEG2_NSFB[5][block_index]),
            _ => unreachable!(),
        }
    };
    let mut start = 0;
    for (&slen, &n_sfb) in slen_table.iter().zip(nsfb_table.iter()) {
        // If slen > 0, read n_sfb scale factors with each scale factor being slen bits long. If
        // slen == 0, but n_sfb > 0, then the those scale factors should be set to 0. Since all
        // scalefacs are preinitialized to 0, this process may be skipped.
        if slen > 0 {
            for sfb in start..(start + n_sfb) {
                channel.scalefacs[sfb] = bs.read_bits_leq32(slen)? as u8;
            }
            bits_read += slen * n_sfb as u32;
        }
        start += n_sfb;
    }
    Ok(bits_read)
}
pub ( super ) fn read_scale_factors_mpeg2 < B : BitStream > ( bs : & mut B , is_intensity_stereo : bool , channel : & mut GranuleChannel , ) -> Result < u32 > { let mut bits_read = 0 ; let block_index = match channel . block_type { BlockType :: Short { is_mixed : true } => 2 , BlockType :: Short { is_mixed : false } => 1 , _ => 0 , } ; let ( slen_table , nsfb_table ) = if is_intensity_stereo { // The actual value of scalefac_compress is a 9-bit unsigned integer (0..512) for MPEG2. A // left shift reduces it to an 8-bit value (0..255). let sfc = u32 :: from ( channel . scalefac_compress ) >> 1 ; match sfc { 0 ..= 179 => ( [ ( sfc / 36 ) , ( sfc % 36 ) / 6 , ( sfc % 36 ) % 6 , 0 , ] , & SCALE_FACTOR_MPEG2_NSFB [ 0 ] [ block_index ] ) , 180 ..= 243 => ( [ ( ( sfc - 180 ) % 64 ) >> 4 , ( ( sfc - 180 ) % 16 ) >> 2 , ( ( sfc - 180 ) % 4 ) , 0 , ] , & SCALE_FACTOR_MPEG2_NSFB [ 1 ] [ block_index ] ) , 244 ..= 255 => ( [ ( sfc - 244 ) / 3 , ( sfc - 244 ) % 3 , 0 , 0 , ] , & SCALE_FACTOR_MPEG2_NSFB [ 2 ] [ block_index ] ) , _ => unreachable ! ( ) , } } else { // The actual value of scalefac_compress is a 9-bit unsigned integer (0..512) for MPEG2. let sfc = u32 :: from ( channel . scalefac_compress ) ; match sfc { 0 ..= 399 => ( [ ( sfc >> 4 ) / 5 , ( sfc >> 4 ) % 5 , ( sfc % 16 ) >> 2 , ( sfc % 4 ) , ] , & SCALE_FACTOR_MPEG2_NSFB [ 3 ] [ block_index ] ) , 400 ..= 499 => ( [ ( ( sfc - 400 ) >> 2 ) / 5 , ( ( sfc - 400 ) >> 2 ) % 5 , ( sfc - 400 ) % 4 , 0 , ] , & SCALE_FACTOR_MPEG2_NSFB [ 4 ] [ block_index ] ) , 500 ..= 512 => ( [ ( sfc - 500 ) / 3 , ( sfc - 500 ) % 3 , 0 , 0 , ] , & SCALE_FACTOR_MPEG2_NSFB [ 5 ] [ block_index ] ) , _ => unreachable ! ( ) , } } ; let mut start = 0 ; for ( & slen , & n_sfb ) in slen_table . iter ( ) . zip ( nsfb_table . iter ( ) ) { // If slen > 0, read n_sfb scale factors with each scale factor being slen bits long. If // slen == 0, but n_sfb > 0, then the those scale factors should be set to 0. Since all // scalefacs are preinitialized to 0, this process may be skipped. if slen > 0 { for sfb in start .. ( start + n_sfb ) { channel . scalefacs [ sfb ] = bs . read_bits_leq32 ( slen ) ? as u8 ; } bits_read += slen * n_sfb as u32 ; } start += n_sfb ; } Ok ( bits_read ) }
Reads the scale factors for a single channel in a granule in a MPEG version 2 audio frame.
Reads the scale factors for a single channel in a granule in a MPEG version 2 audio frame .
bs
&mut B
is_intensity_stereo
bool
channel
&mut GranuleChannel