1 /*********************************************************************** 2 Copyright (c) 2006-2011, Skype Limited. All rights reserved. 3 Redistribution and use in source and binary forms, with or without 4 modification, are permitted provided that the following conditions 5 are met: 6 - Redistributions of source code must retain the above copyright notice, 7 this list of conditions and the following disclaimer. 8 - Redistributions in binary form must reproduce the above copyright 9 notice, this list of conditions and the following disclaimer in the 10 documentation and/or other materials provided with the distribution. 11 - Neither the name of Internet Society, IETF or IETF Trust, nor the 12 names of specific contributors, may be used to endorse or promote 13 products derived from this software without specific prior written 14 permission. 15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS 16 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 19 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 POSSIBILITY OF SUCH DAMAGE. 26 ***********************************************************************/ 27 28 #ifdef HAVE_CONFIG_H 29 #include "config.h" 30 #endif 31 32 #include "main.h" 33 34 /*********************************************/ 35 /* Encode quantization indices of excitation */ 36 /*********************************************/ 37 38 static inline opus_int combine_and_check( /* return ok */ 39 opus_int *pulses_comb, /* O */ 40 const opus_int *pulses_in, /* I */ 41 opus_int max_pulses, /* I max value for sum of pulses */ 42 opus_int len /* I number of output values */ 43 ) 44 { 45 opus_int k, sum; 46 47 for( k = 0; k < len; k++ ) { 48 sum = pulses_in[ 2 * k ] + pulses_in[ 2 * k + 1 ]; 49 if( sum > max_pulses ) { 50 return 1; 51 } 52 pulses_comb[ k ] = sum; 53 } 54 55 return 0; 56 } 57 58 /* Encode quantization indices of excitation */ 59 void silk_encode_pulses( 60 ec_enc *psRangeEnc, /* I/O compressor data structure */ 61 const opus_int signalType, /* I Signal type */ 62 const opus_int quantOffsetType, /* I quantOffsetType */ 63 opus_int8 pulses[], /* I quantization indices */ 64 const opus_int frame_length /* I Frame length */ 65 ) 66 { 67 opus_int i, k, j, iter, bit, nLS, scale_down, RateLevelIndex = 0; 68 opus_int32 abs_q, minSumBits_Q5, sumBits_Q5; 69 opus_int abs_pulses[ MAX_FRAME_LENGTH ]; 70 opus_int sum_pulses[ MAX_NB_SHELL_BLOCKS ]; 71 opus_int nRshifts[ MAX_NB_SHELL_BLOCKS ]; 72 opus_int pulses_comb[ 8 ]; 73 opus_int *abs_pulses_ptr; 74 const opus_int8 *pulses_ptr; 75 const opus_uint8 *cdf_ptr; 76 const opus_uint8 *nBits_ptr; 77 78 silk_memset( pulses_comb, 0, 8 * sizeof( opus_int ) ); /* Fixing Valgrind reported problem*/ 79 80 /****************************/ 81 /* Prepare for shell coding */ 82 /****************************/ 83 /* Calculate number of shell blocks */ 84 silk_assert( 1 << LOG2_SHELL_CODEC_FRAME_LENGTH == SHELL_CODEC_FRAME_LENGTH ); 85 iter = silk_RSHIFT( frame_length, LOG2_SHELL_CODEC_FRAME_LENGTH ); 86 if( iter * SHELL_CODEC_FRAME_LENGTH < frame_length ) { 87 silk_assert( frame_length == 12 * 10 ); /* Make sure only happens for 10 ms @ 12 kHz */ 88 iter++; 89 silk_memset( &pulses[ frame_length ], 0, SHELL_CODEC_FRAME_LENGTH * sizeof(opus_int8)); 90 } 91 92 /* Take the absolute value of the pulses */ 93 for( i = 0; i < iter * SHELL_CODEC_FRAME_LENGTH; i+=4 ) { 94 abs_pulses[i+0] = ( opus_int )silk_abs( pulses[ i + 0 ] ); 95 abs_pulses[i+1] = ( opus_int )silk_abs( pulses[ i + 1 ] ); 96 abs_pulses[i+2] = ( opus_int )silk_abs( pulses[ i + 2 ] ); 97 abs_pulses[i+3] = ( opus_int )silk_abs( pulses[ i + 3 ] ); 98 } 99 100 /* Calc sum pulses per shell code frame */ 101 abs_pulses_ptr = abs_pulses; 102 for( i = 0; i < iter; i++ ) { 103 nRshifts[ i ] = 0; 104 105 while( 1 ) { 106 /* 1+1 -> 2 */ 107 scale_down = combine_and_check( pulses_comb, abs_pulses_ptr, silk_max_pulses_table[ 0 ], 8 ); 108 /* 2+2 -> 4 */ 109 scale_down += combine_and_check( pulses_comb, pulses_comb, silk_max_pulses_table[ 1 ], 4 ); 110 /* 4+4 -> 8 */ 111 scale_down += combine_and_check( pulses_comb, pulses_comb, silk_max_pulses_table[ 2 ], 2 ); 112 /* 8+8 -> 16 */ 113 scale_down += combine_and_check( &sum_pulses[ i ], pulses_comb, silk_max_pulses_table[ 3 ], 1 ); 114 115 if( scale_down ) { 116 /* We need to downscale the quantization signal */ 117 nRshifts[ i ]++; 118 for( k = 0; k < SHELL_CODEC_FRAME_LENGTH; k++ ) { 119 abs_pulses_ptr[ k ] = silk_RSHIFT( abs_pulses_ptr[ k ], 1 ); 120 } 121 } else { 122 /* Jump out of while(1) loop and go to next shell coding frame */ 123 break; 124 } 125 } 126 abs_pulses_ptr += SHELL_CODEC_FRAME_LENGTH; 127 } 128 129 /**************/ 130 /* Rate level */ 131 /**************/ 132 /* find rate level that leads to fewest bits for coding of pulses per block info */ 133 minSumBits_Q5 = silk_int32_MAX; 134 for( k = 0; k < N_RATE_LEVELS - 1; k++ ) { 135 nBits_ptr = silk_pulses_per_block_BITS_Q5[ k ]; 136 sumBits_Q5 = silk_rate_levels_BITS_Q5[ signalType >> 1 ][ k ]; 137 for( i = 0; i < iter; i++ ) { 138 if( nRshifts[ i ] > 0 ) { 139 sumBits_Q5 += nBits_ptr[ MAX_PULSES + 1 ]; 140 } else { 141 sumBits_Q5 += nBits_ptr[ sum_pulses[ i ] ]; 142 } 143 } 144 if( sumBits_Q5 < minSumBits_Q5 ) { 145 minSumBits_Q5 = sumBits_Q5; 146 RateLevelIndex = k; 147 } 148 } 149 ec_enc_icdf( psRangeEnc, RateLevelIndex, silk_rate_levels_iCDF[ signalType >> 1 ], 8 ); 150 151 /***************************************************/ 152 /* Sum-Weighted-Pulses Encoding */ 153 /***************************************************/ 154 cdf_ptr = silk_pulses_per_block_iCDF[ RateLevelIndex ]; 155 for( i = 0; i < iter; i++ ) { 156 if( nRshifts[ i ] == 0 ) { 157 ec_enc_icdf( psRangeEnc, sum_pulses[ i ], cdf_ptr, 8 ); 158 } else { 159 ec_enc_icdf( psRangeEnc, MAX_PULSES + 1, cdf_ptr, 8 ); 160 for( k = 0; k < nRshifts[ i ] - 1; k++ ) { 161 ec_enc_icdf( psRangeEnc, MAX_PULSES + 1, silk_pulses_per_block_iCDF[ N_RATE_LEVELS - 1 ], 8 ); 162 } 163 ec_enc_icdf( psRangeEnc, sum_pulses[ i ], silk_pulses_per_block_iCDF[ N_RATE_LEVELS - 1 ], 8 ); 164 } 165 } 166 167 /******************/ 168 /* Shell Encoding */ 169 /******************/ 170 for( i = 0; i < iter; i++ ) { 171 if( sum_pulses[ i ] > 0 ) { 172 silk_shell_encoder( psRangeEnc, &abs_pulses[ i * SHELL_CODEC_FRAME_LENGTH ] ); 173 } 174 } 175 176 /****************/ 177 /* LSB Encoding */ 178 /****************/ 179 for( i = 0; i < iter; i++ ) { 180 if( nRshifts[ i ] > 0 ) { 181 pulses_ptr = &pulses[ i * SHELL_CODEC_FRAME_LENGTH ]; 182 nLS = nRshifts[ i ] - 1; 183 for( k = 0; k < SHELL_CODEC_FRAME_LENGTH; k++ ) { 184 abs_q = (opus_int8)silk_abs( pulses_ptr[ k ] ); 185 for( j = nLS; j > 0; j-- ) { 186 bit = silk_RSHIFT( abs_q, j ) & 1; 187 ec_enc_icdf( psRangeEnc, bit, silk_lsb_iCDF, 8 ); 188 } 189 bit = abs_q & 1; 190 ec_enc_icdf( psRangeEnc, bit, silk_lsb_iCDF, 8 ); 191 } 192 } 193 } 194 195 /****************/ 196 /* Encode signs */ 197 /****************/ 198 silk_encode_signs( psRangeEnc, pulses, frame_length, signalType, quantOffsetType, sum_pulses ); 199 } 200