fn read_granule_channel_side_info<B: BitStream>(
    bs: &mut B,
    channel: &mut GranuleChannel,
    header: &FrameHeader,
) -> Result<()> {

    channel.part2_3_length = bs.read_bits_leq32(12)? as u16;
    channel.big_values = bs.read_bits_leq32(9)? as u16;

    // The maximum number of samples in a granule is 576. One big_value decodes to 2 samples,
    // therefore there can be no more than 288 (576/2) big_values.
    if channel.big_values > 288 {
        return decode_error("granule big_values > 288");
    }

    channel.global_gain = bs.read_bits_leq32(8)? as u8;

    channel.scalefac_compress = if header.is_mpeg1() {
        bs.read_bits_leq32(4)
    }
    else {
        bs.read_bits_leq32(9)
    }? as u16;

    let window_switching = bs.read_bit()?;

    if window_switching {
        let block_type_enc = bs.read_bits_leq32(2)?;

        let is_mixed = bs.read_bit()?;

        channel.block_type = match block_type_enc {
            // Long block types are not allowed with window switching.
            0b00 => return decode_error("invalid block_type"),
            0b01 => BlockType::Start,
            0b10 => BlockType::Short { is_mixed },
            0b11 => BlockType::End,
            _    => unreachable!(),
        };

        // When window switching is used, there are only two regions, therefore there are only
        // two table selectors.
        for i in 0..2 {
            channel.table_select[i] = bs.read_bits_leq32(5)? as u8;
        }

        for i in 0..3 {
            channel.subblock_gain[i] = bs.read_bits_leq32(3)? as u8;
        }

        // When using window switching, the boundaries of region[0..3] are set implicitly according
        // to the MPEG version and block type. Below, the boundaries to set as per the applicable
        // standard.
        //
        // If MPEG version 2.5 specifically...
        if header.is_mpeg2p5() {
            // For MPEG2.5, the number of scale-factor bands in region0 depends on the block type.
            // The standard indicates these values as 1 less than the actual value, therefore 1 is
            // added here to both values.
            let region0_count = match channel.block_type {
                BlockType::Short { is_mixed: false } => 5 + 1,
                _                                    => 7 + 1,
            };

            channel.region1_start = SFB_LONG_BANDS[header.sample_rate_idx][region0_count];
        }
        // If MPEG version 1, OR the block type is Short...
        else if header.is_mpeg1() || block_type_enc == 0b11 {
            // For MPEG1 with LONG blocks, the first 8 LONG scale-factor bands are used for region0.
            // These bands are always [4, 4, 4, 4, 4, 4, 6, 6, ...] regardless of sample rate. These
            // bands sum to 36 samples.
            //
            // For MPEG1 with SHORT blocks, the first 9 SHORT scale-factor bands are used for
            // region0. These band are always [4, 4, 4, 4, 4, 4, 4, 4, 4, ...] regardless of sample
            // rate. These bands also sum to 36 samples.
            //
            // Finally, for MPEG2 with SHORT blocks, the first 9 short scale-factor bands are used
            // for region0. These bands are also always  [4, 4, 4, 4, 4, 4, 4, 4, 4, ...] regardless
            // of sample and thus sum to 36 samples.
            //
            // In all cases, the region0_count is 36.
            channel.region1_start = 36;
        }
        // If MPEG version 2 AND the block type is not Short...
        else {
            // For MPEG2 and LONG blocks, the first 8 LONG scale-factor bands are used for region0.
            // These bands are always [6, 6, 6, 6, 6, 6, 8, 10, ...] regardless of sample rate.
            // These bands sum to 54.
            channel.region1_start = 54;
        }

        // The second region, region1, spans the remaining samples. Therefore the third region,
        // region2, isn't used.
        channel.region2_start = 576;
    }
    else {
        // If window switching is not used, the block type is always Long.
        channel.block_type = BlockType::Long;

        for i in 0..3 {
            channel.table_select[i] = bs.read_bits_leq32(5)? as u8;
        }

        // When window switching is not used, only LONG scale-factor bands are used for each region.
        // The number of bands in region0 and region1 are defined in side_info. The stored value is
        // 1 less than the actual value.
        let region0_count   = bs.read_bits_leq32(4)? as usize + 1;
        let region0_1_count = bs.read_bits_leq32(3)? as usize + region0_count + 1;

        channel.region1_start = SFB_LONG_BANDS[header.sample_rate_idx][region0_count];

        // The count in region0_1_count may exceed the last band (22) in the LONG bands table.
        // Protect against this.
        channel.region2_start = match region0_1_count {
            0..=22 => SFB_LONG_BANDS[header.sample_rate_idx][region0_1_count],
            _      => 576,
        };
    }

    channel.preflag = if header.is_mpeg1() {
        bs.read_bit()?
    }
    else {
        // Pre-flag is determined implicitly for MPEG2: ISO/IEC 13818-3 section 2.4.3.4.
        channel.scalefac_compress >= 500
    };

    channel.scalefac_scale = bs.read_bit()?;
    channel.count1table_select = bs.read_bit()?;

    Ok(())
}
/// Reads the side_info for a single channel in a granule from a `BitStream`.
Reads the side_info for a single channel in a granule from a `BitStream`.
Reads the side_info for a single channel in a granule from a ` BitStream ` .
fn read_granule_channel_side_info<B: BitStream>(
    bs: &mut B,
    channel: &mut GranuleChannel,
    header: &FrameHeader,
) -> Result<()> {
    channel.part2_3_length = bs.read_bits_leq32(12)? as u16;
    channel.big_values = bs.read_bits_leq32(9)? as u16;
    // The maximum number of samples in a granule is 576. One big_value decodes to 2 samples,
    // therefore there can be no more than 288 (576/2) big_values.
    if channel.big_values > 288 {
        return decode_error("granule big_values > 288");
    }
    channel.global_gain = bs.read_bits_leq32(8)? as u8;
    channel.scalefac_compress = if header.is_mpeg1() {
        bs.read_bits_leq32(4)
    }
    else {
        bs.read_bits_leq32(9)
    }? as u16;
    let window_switching = bs.read_bit()?;
    if window_switching {
        let block_type_enc = bs.read_bits_leq32(2)?;
        let is_mixed = bs.read_bit()?;
        channel.block_type = match block_type_enc {
            // Long block types are not allowed with window switching.
            0b00 => return decode_error("invalid block_type"),
            0b01 => BlockType::Start,
            0b10 => BlockType::Short { is_mixed },
            0b11 => BlockType::End,
            _    => unreachable!(),
        };
        // When window switching is used, there are only two regions, therefore there are only
        // two table selectors.
        for i in 0..2 {
            channel.table_select[i] = bs.read_bits_leq32(5)? as u8;
        }
        for i in 0..3 {
            channel.subblock_gain[i] = bs.read_bits_leq32(3)? as u8;
        }
        // When using window switching, the boundaries of region[0..3] are set implicitly according
        // to the MPEG version and block type. Below, the boundaries to set as per the applicable
        // standard.
        //
        // If MPEG version 2.5 specifically...
        if header.is_mpeg2p5() {
            // For MPEG2.5, the number of scale-factor bands in region0 depends on the block type.
            // The standard indicates these values as 1 less than the actual value, therefore 1 is
            // added here to both values.
            let region0_count = match channel.block_type {
                BlockType::Short { is_mixed: false } => 5 + 1,
                _                                    => 7 + 1,
            };
            channel.region1_start = SFB_LONG_BANDS[header.sample_rate_idx][region0_count];
        }
        // If MPEG version 1, OR the block type is Short...
        else if header.is_mpeg1() || block_type_enc == 0b11 {
            // For MPEG1 with LONG blocks, the first 8 LONG scale-factor bands are used for region0.
            // These bands are always [4, 4, 4, 4, 4, 4, 6, 6, ...] regardless of sample rate. These
            // bands sum to 36 samples.
            //
            // For MPEG1 with SHORT blocks, the first 9 SHORT scale-factor bands are used for
            // region0. These band are always [4, 4, 4, 4, 4, 4, 4, 4, 4, ...] regardless of sample
            // rate. These bands also sum to 36 samples.
            //
            // Finally, for MPEG2 with SHORT blocks, the first 9 short scale-factor bands are used
            // for region0. These bands are also always  [4, 4, 4, 4, 4, 4, 4, 4, 4, ...] regardless
            // of sample and thus sum to 36 samples.
            //
            // In all cases, the region0_count is 36.
            channel.region1_start = 36;
        }
        // If MPEG version 2 AND the block type is not Short...
        else {
            // For MPEG2 and LONG blocks, the first 8 LONG scale-factor bands are used for region0.
            // These bands are always [6, 6, 6, 6, 6, 6, 8, 10, ...] regardless of sample rate.
            // These bands sum to 54.
            channel.region1_start = 54;
        }
        // The second region, region1, spans the remaining samples. Therefore the third region,
        // region2, isn't used.
        channel.region2_start = 576;
    }
    else {
        // If window switching is not used, the block type is always Long.
        channel.block_type = BlockType::Long;
        for i in 0..3 {
            channel.table_select[i] = bs.read_bits_leq32(5)? as u8;
        }
        // When window switching is not used, only LONG scale-factor bands are used for each region.
        // The number of bands in region0 and region1 are defined in side_info. The stored value is
        // 1 less than the actual value.
        let region0_count   = bs.read_bits_leq32(4)? as usize + 1;
        let region0_1_count = bs.read_bits_leq32(3)? as usize + region0_count + 1;
        channel.region1_start = SFB_LONG_BANDS[header.sample_rate_idx][region0_count];
        // The count in region0_1_count may exceed the last band (22) in the LONG bands table.
        // Protect against this.
        channel.region2_start = match region0_1_count {
            0..=22 => SFB_LONG_BANDS[header.sample_rate_idx][region0_1_count],
            _      => 576,
        };
    }
    channel.preflag = if header.is_mpeg1() {
        bs.read_bit()?
    }
    else {
        // Pre-flag is determined implicitly for MPEG2: ISO/IEC 13818-3 section 2.4.3.4.
        channel.scalefac_compress >= 500
    };
    channel.scalefac_scale = bs.read_bit()?;
    channel.count1table_select = bs.read_bit()?;
    Ok(())
}
fn read_granule_channel_side_info < B : BitStream > ( bs : & mut B , channel : & mut GranuleChannel , header : & FrameHeader , ) -> Result < ( ) > { channel . part2_3_length = bs . read_bits_leq32 ( 12 ) ? as u16 ; channel . big_values = bs . read_bits_leq32 ( 9 ) ? as u16 ; // The maximum number of samples in a granule is 576. One big_value decodes to 2 samples, // therefore there can be no more than 288 (576/2) big_values. if channel . big_values > 288 { return decode_error ( " " ) ; } channel . global_gain = bs . read_bits_leq32 ( 8 ) ? as u8 ; channel . scalefac_compress = if header . is_mpeg1 ( ) { bs . read_bits_leq32 ( 4 ) } else { bs . read_bits_leq32 ( 9 ) } ? as u16 ; let window_switching = bs . read_bit ( ) ? ; if window_switching { let block_type_enc = bs . read_bits_leq32 ( 2 ) ? ; let is_mixed = bs . read_bit ( ) ? ; channel . block_type = match block_type_enc { // Long block types are not allowed with window switching. 0b00 => return decode_error ( " " ) , 0b01 => BlockType :: Start , 0b10 => BlockType :: Short { is_mixed } , 0b11 => BlockType :: End , _ => unreachable ! ( ) , } ; // When window switching is used, there are only two regions, therefore there are only // two table selectors. for i in 0 .. 2 { channel . table_select [ i ] = bs . read_bits_leq32 ( 5 ) ? as u8 ; } for i in 0 .. 3 { channel . subblock_gain [ i ] = bs . read_bits_leq32 ( 3 ) ? as u8 ; } // When using window switching, the boundaries of region[0..3] are set implicitly according // to the MPEG version and block type. Below, the boundaries to set as per the applicable // standard. // // If MPEG version 2.5 specifically... if header . is_mpeg2p5 ( ) { // For MPEG2.5, the number of scale-factor bands in region0 depends on the block type. // The standard indicates these values as 1 less than the actual value, therefore 1 is // added here to both values. let region0_count = match channel . block_type { BlockType :: Short { is_mixed : false } => 5 + 1 , _ => 7 + 1 , } ; channel . region1_start = SFB_LONG_BANDS [ header . sample_rate_idx ] [ region0_count ] ; } // If MPEG version 1, OR the block type is Short... else if header . is_mpeg1 ( ) || block_type_enc == 0b11 { // For MPEG1 with LONG blocks, the first 8 LONG scale-factor bands are used for region0. // These bands are always [4, 4, 4, 4, 4, 4, 6, 6, ...] regardless of sample rate. These // bands sum to 36 samples. // // For MPEG1 with SHORT blocks, the first 9 SHORT scale-factor bands are used for // region0. These band are always [4, 4, 4, 4, 4, 4, 4, 4, 4, ...] regardless of sample // rate. These bands also sum to 36 samples. // // Finally, for MPEG2 with SHORT blocks, the first 9 short scale-factor bands are used // for region0. These bands are also always  [4, 4, 4, 4, 4, 4, 4, 4, 4, ...] regardless // of sample and thus sum to 36 samples. // // In all cases, the region0_count is 36. channel . region1_start = 36 ; } // If MPEG version 2 AND the block type is not Short... else { // For MPEG2 and LONG blocks, the first 8 LONG scale-factor bands are used for region0. // These bands are always [6, 6, 6, 6, 6, 6, 8, 10, ...] regardless of sample rate. // These bands sum to 54. channel . region1_start = 54 ; } // The second region, region1, spans the remaining samples. Therefore the third region, // region2, isn't used. channel . region2_start = 576 ; } else { // If window switching is not used, the block type is always Long. channel . block_type = BlockType :: Long ; for i in 0 .. 3 { channel . table_select [ i ] = bs . read_bits_leq32 ( 5 ) ? as u8 ; } // When window switching is not used, only LONG scale-factor bands are used for each region. // The number of bands in region0 and region1 are defined in side_info. The stored value is // 1 less than the actual value. let region0_count = bs . read_bits_leq32 ( 4 ) ? as usize + 1 ; let region0_1_count = bs . read_bits_leq32 ( 3 ) ? as usize + region0_count + 1 ; channel . region1_start = SFB_LONG_BANDS [ header . sample_rate_idx ] [ region0_count ] ; // The count in region0_1_count may exceed the last band (22) in the LONG bands table. // Protect against this. channel . region2_start = match region0_1_count { 0 ..= 22 => SFB_LONG_BANDS [ header . sample_rate_idx ] [ region0_1_count ] , _ => 576 , } ; } channel . preflag = if header . is_mpeg1 ( ) { bs . read_bit ( ) ? } else { // Pre-flag is determined implicitly for MPEG2: ISO/IEC 13818-3 section 2.4.3.4. channel . scalefac_compress >= 500 } ; channel . scalefac_scale = bs . read_bit ( ) ? ; channel . count1table_select = bs . read_bit ( ) ? ; Ok ( ( ) ) }
Reads the side_info for a single channel in a granule from a `BitStream`.
Reads the side_info for a single channel in a granule from a ` BitStream ` .
bs
&mut B
channel
&mut GranuleChannel
header
&FrameHeader
bs
&mut B
channel
&mut GranuleChannel
header
&FrameHeader