michael@0: /*********************************************************************** michael@0: Copyright (c) 2006-2011, Skype Limited. All rights reserved. michael@0: Redistribution and use in source and binary forms, with or without michael@0: modification, are permitted provided that the following conditions michael@0: are met: michael@0: - Redistributions of source code must retain the above copyright notice, michael@0: this list of conditions and the following disclaimer. michael@0: - Redistributions in binary form must reproduce the above copyright michael@0: notice, this list of conditions and the following disclaimer in the michael@0: documentation and/or other materials provided with the distribution. michael@0: - Neither the name of Internet Society, IETF or IETF Trust, nor the michael@0: names of specific contributors, may be used to endorse or promote michael@0: products derived from this software without specific prior written michael@0: permission. michael@0: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" michael@0: AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE michael@0: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE michael@0: ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE michael@0: LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR michael@0: CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF michael@0: SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS michael@0: INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN michael@0: CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) michael@0: ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE michael@0: POSSIBILITY OF SUCH DAMAGE. michael@0: ***********************************************************************/ michael@0: michael@0: #ifdef HAVE_CONFIG_H michael@0: #include "config.h" michael@0: #endif michael@0: michael@0: #include "main.h" michael@0: #include "stack_alloc.h" michael@0: michael@0: /*********************************************/ michael@0: /* Encode quantization indices of excitation */ michael@0: /*********************************************/ michael@0: michael@0: static OPUS_INLINE opus_int combine_and_check( /* return ok */ michael@0: opus_int *pulses_comb, /* O */ michael@0: const opus_int *pulses_in, /* I */ michael@0: opus_int max_pulses, /* I max value for sum of pulses */ michael@0: opus_int len /* I number of output values */ michael@0: ) michael@0: { michael@0: opus_int k, sum; michael@0: michael@0: for( k = 0; k < len; k++ ) { michael@0: sum = pulses_in[ 2 * k ] + pulses_in[ 2 * k + 1 ]; michael@0: if( sum > max_pulses ) { michael@0: return 1; michael@0: } michael@0: pulses_comb[ k ] = sum; michael@0: } michael@0: michael@0: return 0; michael@0: } michael@0: michael@0: /* Encode quantization indices of excitation */ michael@0: void silk_encode_pulses( michael@0: ec_enc *psRangeEnc, /* I/O compressor data structure */ michael@0: const opus_int signalType, /* I Signal type */ michael@0: const opus_int quantOffsetType, /* I quantOffsetType */ michael@0: opus_int8 pulses[], /* I quantization indices */ michael@0: const opus_int frame_length /* I Frame length */ michael@0: ) michael@0: { michael@0: opus_int i, k, j, iter, bit, nLS, scale_down, RateLevelIndex = 0; michael@0: opus_int32 abs_q, minSumBits_Q5, sumBits_Q5; michael@0: VARDECL( opus_int, abs_pulses ); michael@0: VARDECL( opus_int, sum_pulses ); michael@0: VARDECL( opus_int, nRshifts ); michael@0: opus_int pulses_comb[ 8 ]; michael@0: opus_int *abs_pulses_ptr; michael@0: const opus_int8 *pulses_ptr; michael@0: const opus_uint8 *cdf_ptr; michael@0: const opus_uint8 *nBits_ptr; michael@0: SAVE_STACK; michael@0: michael@0: silk_memset( pulses_comb, 0, 8 * sizeof( opus_int ) ); /* Fixing Valgrind reported problem*/ michael@0: michael@0: /****************************/ michael@0: /* Prepare for shell coding */ michael@0: /****************************/ michael@0: /* Calculate number of shell blocks */ michael@0: silk_assert( 1 << LOG2_SHELL_CODEC_FRAME_LENGTH == SHELL_CODEC_FRAME_LENGTH ); michael@0: iter = silk_RSHIFT( frame_length, LOG2_SHELL_CODEC_FRAME_LENGTH ); michael@0: if( iter * SHELL_CODEC_FRAME_LENGTH < frame_length ) { michael@0: silk_assert( frame_length == 12 * 10 ); /* Make sure only happens for 10 ms @ 12 kHz */ michael@0: iter++; michael@0: silk_memset( &pulses[ frame_length ], 0, SHELL_CODEC_FRAME_LENGTH * sizeof(opus_int8)); michael@0: } michael@0: michael@0: /* Take the absolute value of the pulses */ michael@0: ALLOC( abs_pulses, iter * SHELL_CODEC_FRAME_LENGTH, opus_int ); michael@0: silk_assert( !( SHELL_CODEC_FRAME_LENGTH & 3 ) ); michael@0: for( i = 0; i < iter * SHELL_CODEC_FRAME_LENGTH; i+=4 ) { michael@0: abs_pulses[i+0] = ( opus_int )silk_abs( pulses[ i + 0 ] ); michael@0: abs_pulses[i+1] = ( opus_int )silk_abs( pulses[ i + 1 ] ); michael@0: abs_pulses[i+2] = ( opus_int )silk_abs( pulses[ i + 2 ] ); michael@0: abs_pulses[i+3] = ( opus_int )silk_abs( pulses[ i + 3 ] ); michael@0: } michael@0: michael@0: /* Calc sum pulses per shell code frame */ michael@0: ALLOC( sum_pulses, iter, opus_int ); michael@0: ALLOC( nRshifts, iter, opus_int ); michael@0: abs_pulses_ptr = abs_pulses; michael@0: for( i = 0; i < iter; i++ ) { michael@0: nRshifts[ i ] = 0; michael@0: michael@0: while( 1 ) { michael@0: /* 1+1 -> 2 */ michael@0: scale_down = combine_and_check( pulses_comb, abs_pulses_ptr, silk_max_pulses_table[ 0 ], 8 ); michael@0: /* 2+2 -> 4 */ michael@0: scale_down += combine_and_check( pulses_comb, pulses_comb, silk_max_pulses_table[ 1 ], 4 ); michael@0: /* 4+4 -> 8 */ michael@0: scale_down += combine_and_check( pulses_comb, pulses_comb, silk_max_pulses_table[ 2 ], 2 ); michael@0: /* 8+8 -> 16 */ michael@0: scale_down += combine_and_check( &sum_pulses[ i ], pulses_comb, silk_max_pulses_table[ 3 ], 1 ); michael@0: michael@0: if( scale_down ) { michael@0: /* We need to downscale the quantization signal */ michael@0: nRshifts[ i ]++; michael@0: for( k = 0; k < SHELL_CODEC_FRAME_LENGTH; k++ ) { michael@0: abs_pulses_ptr[ k ] = silk_RSHIFT( abs_pulses_ptr[ k ], 1 ); michael@0: } michael@0: } else { michael@0: /* Jump out of while(1) loop and go to next shell coding frame */ michael@0: break; michael@0: } michael@0: } michael@0: abs_pulses_ptr += SHELL_CODEC_FRAME_LENGTH; michael@0: } michael@0: michael@0: /**************/ michael@0: /* Rate level */ michael@0: /**************/ michael@0: /* find rate level that leads to fewest bits for coding of pulses per block info */ michael@0: minSumBits_Q5 = silk_int32_MAX; michael@0: for( k = 0; k < N_RATE_LEVELS - 1; k++ ) { michael@0: nBits_ptr = silk_pulses_per_block_BITS_Q5[ k ]; michael@0: sumBits_Q5 = silk_rate_levels_BITS_Q5[ signalType >> 1 ][ k ]; michael@0: for( i = 0; i < iter; i++ ) { michael@0: if( nRshifts[ i ] > 0 ) { michael@0: sumBits_Q5 += nBits_ptr[ MAX_PULSES + 1 ]; michael@0: } else { michael@0: sumBits_Q5 += nBits_ptr[ sum_pulses[ i ] ]; michael@0: } michael@0: } michael@0: if( sumBits_Q5 < minSumBits_Q5 ) { michael@0: minSumBits_Q5 = sumBits_Q5; michael@0: RateLevelIndex = k; michael@0: } michael@0: } michael@0: ec_enc_icdf( psRangeEnc, RateLevelIndex, silk_rate_levels_iCDF[ signalType >> 1 ], 8 ); michael@0: michael@0: /***************************************************/ michael@0: /* Sum-Weighted-Pulses Encoding */ michael@0: /***************************************************/ michael@0: cdf_ptr = silk_pulses_per_block_iCDF[ RateLevelIndex ]; michael@0: for( i = 0; i < iter; i++ ) { michael@0: if( nRshifts[ i ] == 0 ) { michael@0: ec_enc_icdf( psRangeEnc, sum_pulses[ i ], cdf_ptr, 8 ); michael@0: } else { michael@0: ec_enc_icdf( psRangeEnc, MAX_PULSES + 1, cdf_ptr, 8 ); michael@0: for( k = 0; k < nRshifts[ i ] - 1; k++ ) { michael@0: ec_enc_icdf( psRangeEnc, MAX_PULSES + 1, silk_pulses_per_block_iCDF[ N_RATE_LEVELS - 1 ], 8 ); michael@0: } michael@0: ec_enc_icdf( psRangeEnc, sum_pulses[ i ], silk_pulses_per_block_iCDF[ N_RATE_LEVELS - 1 ], 8 ); michael@0: } michael@0: } michael@0: michael@0: /******************/ michael@0: /* Shell Encoding */ michael@0: /******************/ michael@0: for( i = 0; i < iter; i++ ) { michael@0: if( sum_pulses[ i ] > 0 ) { michael@0: silk_shell_encoder( psRangeEnc, &abs_pulses[ i * SHELL_CODEC_FRAME_LENGTH ] ); michael@0: } michael@0: } michael@0: michael@0: /****************/ michael@0: /* LSB Encoding */ michael@0: /****************/ michael@0: for( i = 0; i < iter; i++ ) { michael@0: if( nRshifts[ i ] > 0 ) { michael@0: pulses_ptr = &pulses[ i * SHELL_CODEC_FRAME_LENGTH ]; michael@0: nLS = nRshifts[ i ] - 1; michael@0: for( k = 0; k < SHELL_CODEC_FRAME_LENGTH; k++ ) { michael@0: abs_q = (opus_int8)silk_abs( pulses_ptr[ k ] ); michael@0: for( j = nLS; j > 0; j-- ) { michael@0: bit = silk_RSHIFT( abs_q, j ) & 1; michael@0: ec_enc_icdf( psRangeEnc, bit, silk_lsb_iCDF, 8 ); michael@0: } michael@0: bit = abs_q & 1; michael@0: ec_enc_icdf( psRangeEnc, bit, silk_lsb_iCDF, 8 ); michael@0: } michael@0: } michael@0: } michael@0: michael@0: /****************/ michael@0: /* Encode signs */ michael@0: /****************/ michael@0: silk_encode_signs( psRangeEnc, pulses, frame_length, signalType, quantOffsetType, sum_pulses ); michael@0: RESTORE_STACK; michael@0: }