/// Reads the scale factors for a single channel in a granule in a MPEG version 1 audio frame.
Reads the scale factors for a single channel in a granule in a MPEG version 1 audio frame.
Reads the scale factors for a single channel in a granule in a MPEG version 1 audio frame .
pub(super) fn read_scale_factors_mpeg1<B: BitStream>(
    bs: &mut B,
    gr: usize,
    ch: usize,
    frame_data: &mut FrameData,
) -> Result<u32> {
    let mut bits_read = 0;
    let channel = &mut frame_data.granules[gr].channels[ch];
    // For MPEG1, scalefac_compress is a 4-bit index into a scale factor bit length lookup table.
    let (slen1, slen2) = SCALE_FACTOR_SLEN[channel.scalefac_compress as usize];
    // Short or Mixed windows...
    if let BlockType::Short { is_mixed } = channel.block_type {
        // If the block is mixed, there are three total scale factor partitions. The first is a long
        // scale factor partition for bands 0..8 (scalefacs[0..8] with each scale factor being slen1
        // bits long. Following this is a short scale factor partition covering bands 8..11 with a
        // window of 3 (scalefacs[8..17]) and each scale factoring being slen1 bits long.
        //
        // If a block is not mixed, then there are a total of two scale factor partitions. The first
        // is a short scale factor partition for bands 0..6 with a window length of 3
        // (scalefacs[0..18]) and each scale factor being slen1 bits long.
        let n_sfb = if is_mixed { 8 + 3 * 3 } else { 6 * 3 };
        if slen1 > 0 {
            for sfb in 0..n_sfb {
                channel.scalefacs[sfb] = bs.read_bits_leq32(slen1)? as u8;
            }
            bits_read += n_sfb * slen1 as usize;
        }
        // The final scale factor partition is always a a short scale factor window. It covers bands
        // 11..17 (scalefacs[17..35]) if the block is mixed, or bands 6..12 (scalefacs[18..36]) if
        // not. Each band has a window of 3 with each scale factor being slen2 bits long.
        if slen2 > 0 {
            for sfb in n_sfb..(n_sfb + (6 * 3)) {
                channel.scalefacs[sfb] = bs.read_bits_leq32(slen2)? as u8;
            }
            bits_read += 6 * 3 * slen2 as usize;
        }
    }
    // Normal (long, start, end) windows...
    else {
        // For normal windows there are 21 scale factor bands. These bands are divivided into four
        // band ranges. Scale factors in the first two band ranges: [0..6], [6..11], have scale
        // factors that are slen1 bits long, while the last two band ranges: [11..16], [16..21] have
        // scale factors that are slen2 bits long.
        const SCALE_FACTOR_BANDS: [(usize, usize); 4] = [(0, 6), (6, 11), (11, 16), (16, 21)];
        for (i, (start, end)) in SCALE_FACTOR_BANDS.iter().enumerate() {
            let slen = if i < 2 { slen1 } else { slen2 };
            // If this is the second granule, and the scale factor selection information for this
            // channel indicates that the scale factors should be copied from the first granule,
            // do so.
            if gr > 0 && frame_data.scfsi[ch][i] {
                let (granule0, granule1) = frame_data.granules.split_first_mut().unwrap();
                granule1[0].channels[ch].scalefacs[*start..*end]
                    .copy_from_slice(&granule0.channels[ch].scalefacs[*start..*end]);
            }
            // Otherwise, read the scale factors from the bitstream. Since scale factors are already
            // zeroed out by default, don't do anything if slen is 0.
            else if slen > 0 {
                for sfb in *start..*end {
                    frame_data.granules[gr].channels[ch].scalefacs[sfb] =
                        bs.read_bits_leq32(slen)? as u8;
                }
                bits_read += slen as usize * (end - start);
            }
        }
    }
    Ok(bits_read as u32)
}
pub ( super ) fn read_scale_factors_mpeg1 < B : BitStream > ( bs : & mut B , gr : usize , ch : usize , frame_data : & mut FrameData , ) -> Result < u32 > { let mut bits_read = 0 ; let channel = & mut frame_data . granules [ gr ] . channels [ ch ] ; // For MPEG1, scalefac_compress is a 4-bit index into a scale factor bit length lookup table. let ( slen1 , slen2 ) = SCALE_FACTOR_SLEN [ channel . scalefac_compress as usize ] ; // Short or Mixed windows... if let BlockType :: Short { is_mixed } = channel . block_type { // If the block is mixed, there are three total scale factor partitions. The first is a long // scale factor partition for bands 0..8 (scalefacs[0..8] with each scale factor being slen1 // bits long. Following this is a short scale factor partition covering bands 8..11 with a // window of 3 (scalefacs[8..17]) and each scale factoring being slen1 bits long. // // If a block is not mixed, then there are a total of two scale factor partitions. The first // is a short scale factor partition for bands 0..6 with a window length of 3 // (scalefacs[0..18]) and each scale factor being slen1 bits long. let n_sfb = if is_mixed { 8 + 3 * 3 } else { 6 * 3 } ; if slen1 > 0 { for sfb in 0 .. n_sfb { channel . scalefacs [ sfb ] = bs . read_bits_leq32 ( slen1 ) ? as u8 ; } bits_read += n_sfb * slen1 as usize ; } // The final scale factor partition is always a a short scale factor window. It covers bands // 11..17 (scalefacs[17..35]) if the block is mixed, or bands 6..12 (scalefacs[18..36]) if // not. Each band has a window of 3 with each scale factor being slen2 bits long. if slen2 > 0 { for sfb in n_sfb .. ( n_sfb + ( 6 * 3 ) ) { channel . scalefacs [ sfb ] = bs . read_bits_leq32 ( slen2 ) ? as u8 ; } bits_read += 6 * 3 * slen2 as usize ; } } // Normal (long, start, end) windows... else { // For normal windows there are 21 scale factor bands. These bands are divivided into four // band ranges. Scale factors in the first two band ranges: [0..6], [6..11], have scale // factors that are slen1 bits long, while the last two band ranges: [11..16], [16..21] have // scale factors that are slen2 bits long. const SCALE_FACTOR_BANDS : [ ( usize , usize ) ; 4 ] = [ ( 0 , 6 ) , ( 6 , 11 ) , ( 11 , 16 ) , ( 16 , 21 ) ] ; for ( i , ( start , end ) ) in SCALE_FACTOR_BANDS . iter ( ) . enumerate ( ) { let slen = if i < 2 { slen1 } else { slen2 } ; // If this is the second granule, and the scale factor selection information for this // channel indicates that the scale factors should be copied from the first granule, // do so. if gr > 0 && frame_data . scfsi [ ch ] [ i ] { let ( granule0 , granule1 ) = frame_data . granules . split_first_mut ( ) . unwrap ( ) ; granule1 [ 0 ] . channels [ ch ] . scalefacs [ * start .. * end ] . copy_from_slice ( & granule0 . channels [ ch ] . scalefacs [ * start .. * end ] ) ; } // Otherwise, read the scale factors from the bitstream. Since scale factors are already // zeroed out by default, don't do anything if slen is 0. else if slen > 0 { for sfb in * start .. * end { frame_data . granules [ gr ] . channels [ ch ] . scalefacs [ sfb ] = bs . read_bits_leq32 ( slen ) ? as u8 ; } bits_read += slen as usize * ( end - start ) ; } } } Ok ( bits_read as u32 ) }
Reads the scale factors for a single channel in a granule in a MPEG version 1 audio frame.
Reads the scale factors for a single channel in a granule in a MPEG version 1 audio frame .
bs
&mut B
gr
usize
ch
usize
frame_data
&mut FrameData