Alexandre Lision | 744f742 | 2013-09-25 11:39:37 -0400 | [diff] [blame] | 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. |
Alexandre Lision | 8538238 | 2014-01-27 15:54:16 -0500 | [diff] [blame] | 11 | - Neither the name of Internet Society, IETF or IETF Trust, nor the |
Alexandre Lision | 744f742 | 2013-09-25 11:39:37 -0400 | [diff] [blame] | 12 | names of specific contributors, may be used to endorse or promote |
| 13 | products derived from this software without specific prior written |
| 14 | permission. |
Alexandre Lision | 8538238 | 2014-01-27 15:54:16 -0500 | [diff] [blame] | 15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
Alexandre Lision | 744f742 | 2013-09-25 11:39:37 -0400 | [diff] [blame] | 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" |
Alexandre Lision | 8538238 | 2014-01-27 15:54:16 -0500 | [diff] [blame] | 33 | #include "stack_alloc.h" |
Alexandre Lision | 744f742 | 2013-09-25 11:39:37 -0400 | [diff] [blame] | 34 | |
| 35 | /*********************************************/ |
| 36 | /* Encode quantization indices of excitation */ |
| 37 | /*********************************************/ |
| 38 | |
Alexandre Lision | 8538238 | 2014-01-27 15:54:16 -0500 | [diff] [blame] | 39 | static OPUS_INLINE opus_int combine_and_check( /* return ok */ |
Alexandre Lision | 744f742 | 2013-09-25 11:39:37 -0400 | [diff] [blame] | 40 | opus_int *pulses_comb, /* O */ |
| 41 | const opus_int *pulses_in, /* I */ |
| 42 | opus_int max_pulses, /* I max value for sum of pulses */ |
| 43 | opus_int len /* I number of output values */ |
| 44 | ) |
| 45 | { |
| 46 | opus_int k, sum; |
| 47 | |
| 48 | for( k = 0; k < len; k++ ) { |
| 49 | sum = pulses_in[ 2 * k ] + pulses_in[ 2 * k + 1 ]; |
| 50 | if( sum > max_pulses ) { |
| 51 | return 1; |
| 52 | } |
| 53 | pulses_comb[ k ] = sum; |
| 54 | } |
| 55 | |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | /* Encode quantization indices of excitation */ |
| 60 | void silk_encode_pulses( |
| 61 | ec_enc *psRangeEnc, /* I/O compressor data structure */ |
| 62 | const opus_int signalType, /* I Signal type */ |
| 63 | const opus_int quantOffsetType, /* I quantOffsetType */ |
| 64 | opus_int8 pulses[], /* I quantization indices */ |
| 65 | const opus_int frame_length /* I Frame length */ |
| 66 | ) |
| 67 | { |
| 68 | opus_int i, k, j, iter, bit, nLS, scale_down, RateLevelIndex = 0; |
| 69 | opus_int32 abs_q, minSumBits_Q5, sumBits_Q5; |
Alexandre Lision | 8538238 | 2014-01-27 15:54:16 -0500 | [diff] [blame] | 70 | VARDECL( opus_int, abs_pulses ); |
| 71 | VARDECL( opus_int, sum_pulses ); |
| 72 | VARDECL( opus_int, nRshifts ); |
Alexandre Lision | 744f742 | 2013-09-25 11:39:37 -0400 | [diff] [blame] | 73 | opus_int pulses_comb[ 8 ]; |
| 74 | opus_int *abs_pulses_ptr; |
| 75 | const opus_int8 *pulses_ptr; |
| 76 | const opus_uint8 *cdf_ptr; |
| 77 | const opus_uint8 *nBits_ptr; |
Alexandre Lision | 8538238 | 2014-01-27 15:54:16 -0500 | [diff] [blame] | 78 | SAVE_STACK; |
Alexandre Lision | 744f742 | 2013-09-25 11:39:37 -0400 | [diff] [blame] | 79 | |
| 80 | silk_memset( pulses_comb, 0, 8 * sizeof( opus_int ) ); /* Fixing Valgrind reported problem*/ |
| 81 | |
| 82 | /****************************/ |
| 83 | /* Prepare for shell coding */ |
| 84 | /****************************/ |
| 85 | /* Calculate number of shell blocks */ |
| 86 | silk_assert( 1 << LOG2_SHELL_CODEC_FRAME_LENGTH == SHELL_CODEC_FRAME_LENGTH ); |
| 87 | iter = silk_RSHIFT( frame_length, LOG2_SHELL_CODEC_FRAME_LENGTH ); |
| 88 | if( iter * SHELL_CODEC_FRAME_LENGTH < frame_length ) { |
| 89 | silk_assert( frame_length == 12 * 10 ); /* Make sure only happens for 10 ms @ 12 kHz */ |
| 90 | iter++; |
| 91 | silk_memset( &pulses[ frame_length ], 0, SHELL_CODEC_FRAME_LENGTH * sizeof(opus_int8)); |
| 92 | } |
| 93 | |
| 94 | /* Take the absolute value of the pulses */ |
Alexandre Lision | 8538238 | 2014-01-27 15:54:16 -0500 | [diff] [blame] | 95 | ALLOC( abs_pulses, iter * SHELL_CODEC_FRAME_LENGTH, opus_int ); |
| 96 | silk_assert( !( SHELL_CODEC_FRAME_LENGTH & 3 ) ); |
Alexandre Lision | 744f742 | 2013-09-25 11:39:37 -0400 | [diff] [blame] | 97 | for( i = 0; i < iter * SHELL_CODEC_FRAME_LENGTH; i+=4 ) { |
| 98 | abs_pulses[i+0] = ( opus_int )silk_abs( pulses[ i + 0 ] ); |
| 99 | abs_pulses[i+1] = ( opus_int )silk_abs( pulses[ i + 1 ] ); |
| 100 | abs_pulses[i+2] = ( opus_int )silk_abs( pulses[ i + 2 ] ); |
| 101 | abs_pulses[i+3] = ( opus_int )silk_abs( pulses[ i + 3 ] ); |
| 102 | } |
| 103 | |
| 104 | /* Calc sum pulses per shell code frame */ |
Alexandre Lision | 8538238 | 2014-01-27 15:54:16 -0500 | [diff] [blame] | 105 | ALLOC( sum_pulses, iter, opus_int ); |
| 106 | ALLOC( nRshifts, iter, opus_int ); |
Alexandre Lision | 744f742 | 2013-09-25 11:39:37 -0400 | [diff] [blame] | 107 | abs_pulses_ptr = abs_pulses; |
| 108 | for( i = 0; i < iter; i++ ) { |
| 109 | nRshifts[ i ] = 0; |
| 110 | |
| 111 | while( 1 ) { |
| 112 | /* 1+1 -> 2 */ |
| 113 | scale_down = combine_and_check( pulses_comb, abs_pulses_ptr, silk_max_pulses_table[ 0 ], 8 ); |
| 114 | /* 2+2 -> 4 */ |
| 115 | scale_down += combine_and_check( pulses_comb, pulses_comb, silk_max_pulses_table[ 1 ], 4 ); |
| 116 | /* 4+4 -> 8 */ |
| 117 | scale_down += combine_and_check( pulses_comb, pulses_comb, silk_max_pulses_table[ 2 ], 2 ); |
| 118 | /* 8+8 -> 16 */ |
| 119 | scale_down += combine_and_check( &sum_pulses[ i ], pulses_comb, silk_max_pulses_table[ 3 ], 1 ); |
| 120 | |
| 121 | if( scale_down ) { |
| 122 | /* We need to downscale the quantization signal */ |
| 123 | nRshifts[ i ]++; |
| 124 | for( k = 0; k < SHELL_CODEC_FRAME_LENGTH; k++ ) { |
| 125 | abs_pulses_ptr[ k ] = silk_RSHIFT( abs_pulses_ptr[ k ], 1 ); |
| 126 | } |
| 127 | } else { |
| 128 | /* Jump out of while(1) loop and go to next shell coding frame */ |
| 129 | break; |
| 130 | } |
| 131 | } |
| 132 | abs_pulses_ptr += SHELL_CODEC_FRAME_LENGTH; |
| 133 | } |
| 134 | |
| 135 | /**************/ |
| 136 | /* Rate level */ |
| 137 | /**************/ |
| 138 | /* find rate level that leads to fewest bits for coding of pulses per block info */ |
| 139 | minSumBits_Q5 = silk_int32_MAX; |
| 140 | for( k = 0; k < N_RATE_LEVELS - 1; k++ ) { |
| 141 | nBits_ptr = silk_pulses_per_block_BITS_Q5[ k ]; |
| 142 | sumBits_Q5 = silk_rate_levels_BITS_Q5[ signalType >> 1 ][ k ]; |
| 143 | for( i = 0; i < iter; i++ ) { |
| 144 | if( nRshifts[ i ] > 0 ) { |
| 145 | sumBits_Q5 += nBits_ptr[ MAX_PULSES + 1 ]; |
| 146 | } else { |
| 147 | sumBits_Q5 += nBits_ptr[ sum_pulses[ i ] ]; |
| 148 | } |
| 149 | } |
| 150 | if( sumBits_Q5 < minSumBits_Q5 ) { |
| 151 | minSumBits_Q5 = sumBits_Q5; |
| 152 | RateLevelIndex = k; |
| 153 | } |
| 154 | } |
| 155 | ec_enc_icdf( psRangeEnc, RateLevelIndex, silk_rate_levels_iCDF[ signalType >> 1 ], 8 ); |
| 156 | |
| 157 | /***************************************************/ |
| 158 | /* Sum-Weighted-Pulses Encoding */ |
| 159 | /***************************************************/ |
| 160 | cdf_ptr = silk_pulses_per_block_iCDF[ RateLevelIndex ]; |
| 161 | for( i = 0; i < iter; i++ ) { |
| 162 | if( nRshifts[ i ] == 0 ) { |
| 163 | ec_enc_icdf( psRangeEnc, sum_pulses[ i ], cdf_ptr, 8 ); |
| 164 | } else { |
| 165 | ec_enc_icdf( psRangeEnc, MAX_PULSES + 1, cdf_ptr, 8 ); |
| 166 | for( k = 0; k < nRshifts[ i ] - 1; k++ ) { |
| 167 | ec_enc_icdf( psRangeEnc, MAX_PULSES + 1, silk_pulses_per_block_iCDF[ N_RATE_LEVELS - 1 ], 8 ); |
| 168 | } |
| 169 | ec_enc_icdf( psRangeEnc, sum_pulses[ i ], silk_pulses_per_block_iCDF[ N_RATE_LEVELS - 1 ], 8 ); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | /******************/ |
| 174 | /* Shell Encoding */ |
| 175 | /******************/ |
| 176 | for( i = 0; i < iter; i++ ) { |
| 177 | if( sum_pulses[ i ] > 0 ) { |
| 178 | silk_shell_encoder( psRangeEnc, &abs_pulses[ i * SHELL_CODEC_FRAME_LENGTH ] ); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | /****************/ |
| 183 | /* LSB Encoding */ |
| 184 | /****************/ |
| 185 | for( i = 0; i < iter; i++ ) { |
| 186 | if( nRshifts[ i ] > 0 ) { |
| 187 | pulses_ptr = &pulses[ i * SHELL_CODEC_FRAME_LENGTH ]; |
| 188 | nLS = nRshifts[ i ] - 1; |
| 189 | for( k = 0; k < SHELL_CODEC_FRAME_LENGTH; k++ ) { |
| 190 | abs_q = (opus_int8)silk_abs( pulses_ptr[ k ] ); |
| 191 | for( j = nLS; j > 0; j-- ) { |
| 192 | bit = silk_RSHIFT( abs_q, j ) & 1; |
| 193 | ec_enc_icdf( psRangeEnc, bit, silk_lsb_iCDF, 8 ); |
| 194 | } |
| 195 | bit = abs_q & 1; |
| 196 | ec_enc_icdf( psRangeEnc, bit, silk_lsb_iCDF, 8 ); |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | /****************/ |
| 202 | /* Encode signs */ |
| 203 | /****************/ |
| 204 | silk_encode_signs( psRangeEnc, pulses, frame_length, signalType, quantOffsetType, sum_pulses ); |
Alexandre Lision | 8538238 | 2014-01-27 15:54:16 -0500 | [diff] [blame] | 205 | RESTORE_STACK; |
Alexandre Lision | 744f742 | 2013-09-25 11:39:37 -0400 | [diff] [blame] | 206 | } |