Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /*********************************************************************** |
michael@0 | 2 | Copyright (c) 2006-2011, Skype Limited. All rights reserved. |
michael@0 | 3 | Redistribution and use in source and binary forms, with or without |
michael@0 | 4 | modification, are permitted provided that the following conditions |
michael@0 | 5 | are met: |
michael@0 | 6 | - Redistributions of source code must retain the above copyright notice, |
michael@0 | 7 | this list of conditions and the following disclaimer. |
michael@0 | 8 | - Redistributions in binary form must reproduce the above copyright |
michael@0 | 9 | notice, this list of conditions and the following disclaimer in the |
michael@0 | 10 | documentation and/or other materials provided with the distribution. |
michael@0 | 11 | - Neither the name of Internet Society, IETF or IETF Trust, nor the |
michael@0 | 12 | names of specific contributors, may be used to endorse or promote |
michael@0 | 13 | products derived from this software without specific prior written |
michael@0 | 14 | permission. |
michael@0 | 15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
michael@0 | 16 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
michael@0 | 17 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
michael@0 | 18 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
michael@0 | 19 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
michael@0 | 20 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
michael@0 | 21 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
michael@0 | 22 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
michael@0 | 23 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
michael@0 | 24 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
michael@0 | 25 | POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 26 | ***********************************************************************/ |
michael@0 | 27 | |
michael@0 | 28 | #ifdef HAVE_CONFIG_H |
michael@0 | 29 | #include "config.h" |
michael@0 | 30 | #endif |
michael@0 | 31 | |
michael@0 | 32 | #include "main.h" |
michael@0 | 33 | |
michael@0 | 34 | /* Delayed-decision quantizer for NLSF residuals */ |
michael@0 | 35 | opus_int32 silk_NLSF_del_dec_quant( /* O Returns RD value in Q25 */ |
michael@0 | 36 | opus_int8 indices[], /* O Quantization indices [ order ] */ |
michael@0 | 37 | const opus_int16 x_Q10[], /* I Input [ order ] */ |
michael@0 | 38 | const opus_int16 w_Q5[], /* I Weights [ order ] */ |
michael@0 | 39 | const opus_uint8 pred_coef_Q8[], /* I Backward predictor coefs [ order ] */ |
michael@0 | 40 | const opus_int16 ec_ix[], /* I Indices to entropy coding tables [ order ] */ |
michael@0 | 41 | const opus_uint8 ec_rates_Q5[], /* I Rates [] */ |
michael@0 | 42 | const opus_int quant_step_size_Q16, /* I Quantization step size */ |
michael@0 | 43 | const opus_int16 inv_quant_step_size_Q6, /* I Inverse quantization step size */ |
michael@0 | 44 | const opus_int32 mu_Q20, /* I R/D tradeoff */ |
michael@0 | 45 | const opus_int16 order /* I Number of input values */ |
michael@0 | 46 | ) |
michael@0 | 47 | { |
michael@0 | 48 | opus_int i, j, nStates, ind_tmp, ind_min_max, ind_max_min, in_Q10, res_Q10; |
michael@0 | 49 | opus_int pred_Q10, diff_Q10, out0_Q10, out1_Q10, rate0_Q5, rate1_Q5; |
michael@0 | 50 | opus_int32 RD_tmp_Q25, min_Q25, min_max_Q25, max_min_Q25, pred_coef_Q16; |
michael@0 | 51 | opus_int ind_sort[ NLSF_QUANT_DEL_DEC_STATES ]; |
michael@0 | 52 | opus_int8 ind[ NLSF_QUANT_DEL_DEC_STATES ][ MAX_LPC_ORDER ]; |
michael@0 | 53 | opus_int16 prev_out_Q10[ 2 * NLSF_QUANT_DEL_DEC_STATES ]; |
michael@0 | 54 | opus_int32 RD_Q25[ 2 * NLSF_QUANT_DEL_DEC_STATES ]; |
michael@0 | 55 | opus_int32 RD_min_Q25[ NLSF_QUANT_DEL_DEC_STATES ]; |
michael@0 | 56 | opus_int32 RD_max_Q25[ NLSF_QUANT_DEL_DEC_STATES ]; |
michael@0 | 57 | const opus_uint8 *rates_Q5; |
michael@0 | 58 | |
michael@0 | 59 | silk_assert( (NLSF_QUANT_DEL_DEC_STATES & (NLSF_QUANT_DEL_DEC_STATES-1)) == 0 ); /* must be power of two */ |
michael@0 | 60 | |
michael@0 | 61 | nStates = 1; |
michael@0 | 62 | RD_Q25[ 0 ] = 0; |
michael@0 | 63 | prev_out_Q10[ 0 ] = 0; |
michael@0 | 64 | for( i = order - 1; ; i-- ) { |
michael@0 | 65 | rates_Q5 = &ec_rates_Q5[ ec_ix[ i ] ]; |
michael@0 | 66 | pred_coef_Q16 = silk_LSHIFT( (opus_int32)pred_coef_Q8[ i ], 8 ); |
michael@0 | 67 | in_Q10 = x_Q10[ i ]; |
michael@0 | 68 | for( j = 0; j < nStates; j++ ) { |
michael@0 | 69 | pred_Q10 = silk_SMULWB( pred_coef_Q16, prev_out_Q10[ j ] ); |
michael@0 | 70 | res_Q10 = silk_SUB16( in_Q10, pred_Q10 ); |
michael@0 | 71 | ind_tmp = silk_SMULWB( (opus_int32)inv_quant_step_size_Q6, res_Q10 ); |
michael@0 | 72 | ind_tmp = silk_LIMIT( ind_tmp, -NLSF_QUANT_MAX_AMPLITUDE_EXT, NLSF_QUANT_MAX_AMPLITUDE_EXT-1 ); |
michael@0 | 73 | ind[ j ][ i ] = (opus_int8)ind_tmp; |
michael@0 | 74 | |
michael@0 | 75 | /* compute outputs for ind_tmp and ind_tmp + 1 */ |
michael@0 | 76 | out0_Q10 = silk_LSHIFT( ind_tmp, 10 ); |
michael@0 | 77 | out1_Q10 = silk_ADD16( out0_Q10, 1024 ); |
michael@0 | 78 | if( ind_tmp > 0 ) { |
michael@0 | 79 | out0_Q10 = silk_SUB16( out0_Q10, SILK_FIX_CONST( NLSF_QUANT_LEVEL_ADJ, 10 ) ); |
michael@0 | 80 | out1_Q10 = silk_SUB16( out1_Q10, SILK_FIX_CONST( NLSF_QUANT_LEVEL_ADJ, 10 ) ); |
michael@0 | 81 | } else if( ind_tmp == 0 ) { |
michael@0 | 82 | out1_Q10 = silk_SUB16( out1_Q10, SILK_FIX_CONST( NLSF_QUANT_LEVEL_ADJ, 10 ) ); |
michael@0 | 83 | } else if( ind_tmp == -1 ) { |
michael@0 | 84 | out0_Q10 = silk_ADD16( out0_Q10, SILK_FIX_CONST( NLSF_QUANT_LEVEL_ADJ, 10 ) ); |
michael@0 | 85 | } else { |
michael@0 | 86 | out0_Q10 = silk_ADD16( out0_Q10, SILK_FIX_CONST( NLSF_QUANT_LEVEL_ADJ, 10 ) ); |
michael@0 | 87 | out1_Q10 = silk_ADD16( out1_Q10, SILK_FIX_CONST( NLSF_QUANT_LEVEL_ADJ, 10 ) ); |
michael@0 | 88 | } |
michael@0 | 89 | out0_Q10 = silk_SMULWB( (opus_int32)out0_Q10, quant_step_size_Q16 ); |
michael@0 | 90 | out1_Q10 = silk_SMULWB( (opus_int32)out1_Q10, quant_step_size_Q16 ); |
michael@0 | 91 | out0_Q10 = silk_ADD16( out0_Q10, pred_Q10 ); |
michael@0 | 92 | out1_Q10 = silk_ADD16( out1_Q10, pred_Q10 ); |
michael@0 | 93 | prev_out_Q10[ j ] = out0_Q10; |
michael@0 | 94 | prev_out_Q10[ j + nStates ] = out1_Q10; |
michael@0 | 95 | |
michael@0 | 96 | /* compute RD for ind_tmp and ind_tmp + 1 */ |
michael@0 | 97 | if( ind_tmp + 1 >= NLSF_QUANT_MAX_AMPLITUDE ) { |
michael@0 | 98 | if( ind_tmp + 1 == NLSF_QUANT_MAX_AMPLITUDE ) { |
michael@0 | 99 | rate0_Q5 = rates_Q5[ ind_tmp + NLSF_QUANT_MAX_AMPLITUDE ]; |
michael@0 | 100 | rate1_Q5 = 280; |
michael@0 | 101 | } else { |
michael@0 | 102 | rate0_Q5 = silk_SMLABB( 280 - 43 * NLSF_QUANT_MAX_AMPLITUDE, 43, ind_tmp ); |
michael@0 | 103 | rate1_Q5 = silk_ADD16( rate0_Q5, 43 ); |
michael@0 | 104 | } |
michael@0 | 105 | } else if( ind_tmp <= -NLSF_QUANT_MAX_AMPLITUDE ) { |
michael@0 | 106 | if( ind_tmp == -NLSF_QUANT_MAX_AMPLITUDE ) { |
michael@0 | 107 | rate0_Q5 = 280; |
michael@0 | 108 | rate1_Q5 = rates_Q5[ ind_tmp + 1 + NLSF_QUANT_MAX_AMPLITUDE ]; |
michael@0 | 109 | } else { |
michael@0 | 110 | rate0_Q5 = silk_SMLABB( 280 - 43 * NLSF_QUANT_MAX_AMPLITUDE, -43, ind_tmp ); |
michael@0 | 111 | rate1_Q5 = silk_SUB16( rate0_Q5, 43 ); |
michael@0 | 112 | } |
michael@0 | 113 | } else { |
michael@0 | 114 | rate0_Q5 = rates_Q5[ ind_tmp + NLSF_QUANT_MAX_AMPLITUDE ]; |
michael@0 | 115 | rate1_Q5 = rates_Q5[ ind_tmp + 1 + NLSF_QUANT_MAX_AMPLITUDE ]; |
michael@0 | 116 | } |
michael@0 | 117 | RD_tmp_Q25 = RD_Q25[ j ]; |
michael@0 | 118 | diff_Q10 = silk_SUB16( in_Q10, out0_Q10 ); |
michael@0 | 119 | RD_Q25[ j ] = silk_SMLABB( silk_MLA( RD_tmp_Q25, silk_SMULBB( diff_Q10, diff_Q10 ), w_Q5[ i ] ), mu_Q20, rate0_Q5 ); |
michael@0 | 120 | diff_Q10 = silk_SUB16( in_Q10, out1_Q10 ); |
michael@0 | 121 | RD_Q25[ j + nStates ] = silk_SMLABB( silk_MLA( RD_tmp_Q25, silk_SMULBB( diff_Q10, diff_Q10 ), w_Q5[ i ] ), mu_Q20, rate1_Q5 ); |
michael@0 | 122 | } |
michael@0 | 123 | |
michael@0 | 124 | if( nStates <= ( NLSF_QUANT_DEL_DEC_STATES >> 1 ) ) { |
michael@0 | 125 | /* double number of states and copy */ |
michael@0 | 126 | for( j = 0; j < nStates; j++ ) { |
michael@0 | 127 | ind[ j + nStates ][ i ] = ind[ j ][ i ] + 1; |
michael@0 | 128 | } |
michael@0 | 129 | nStates = silk_LSHIFT( nStates, 1 ); |
michael@0 | 130 | for( j = nStates; j < NLSF_QUANT_DEL_DEC_STATES; j++ ) { |
michael@0 | 131 | ind[ j ][ i ] = ind[ j - nStates ][ i ]; |
michael@0 | 132 | } |
michael@0 | 133 | } else if( i > 0 ) { |
michael@0 | 134 | /* sort lower and upper half of RD_Q25, pairwise */ |
michael@0 | 135 | for( j = 0; j < NLSF_QUANT_DEL_DEC_STATES; j++ ) { |
michael@0 | 136 | if( RD_Q25[ j ] > RD_Q25[ j + NLSF_QUANT_DEL_DEC_STATES ] ) { |
michael@0 | 137 | RD_max_Q25[ j ] = RD_Q25[ j ]; |
michael@0 | 138 | RD_min_Q25[ j ] = RD_Q25[ j + NLSF_QUANT_DEL_DEC_STATES ]; |
michael@0 | 139 | RD_Q25[ j ] = RD_min_Q25[ j ]; |
michael@0 | 140 | RD_Q25[ j + NLSF_QUANT_DEL_DEC_STATES ] = RD_max_Q25[ j ]; |
michael@0 | 141 | /* swap prev_out values */ |
michael@0 | 142 | out0_Q10 = prev_out_Q10[ j ]; |
michael@0 | 143 | prev_out_Q10[ j ] = prev_out_Q10[ j + NLSF_QUANT_DEL_DEC_STATES ]; |
michael@0 | 144 | prev_out_Q10[ j + NLSF_QUANT_DEL_DEC_STATES ] = out0_Q10; |
michael@0 | 145 | ind_sort[ j ] = j + NLSF_QUANT_DEL_DEC_STATES; |
michael@0 | 146 | } else { |
michael@0 | 147 | RD_min_Q25[ j ] = RD_Q25[ j ]; |
michael@0 | 148 | RD_max_Q25[ j ] = RD_Q25[ j + NLSF_QUANT_DEL_DEC_STATES ]; |
michael@0 | 149 | ind_sort[ j ] = j; |
michael@0 | 150 | } |
michael@0 | 151 | } |
michael@0 | 152 | /* compare the highest RD values of the winning half with the lowest one in the losing half, and copy if necessary */ |
michael@0 | 153 | /* afterwards ind_sort[] will contain the indices of the NLSF_QUANT_DEL_DEC_STATES winning RD values */ |
michael@0 | 154 | while( 1 ) { |
michael@0 | 155 | min_max_Q25 = silk_int32_MAX; |
michael@0 | 156 | max_min_Q25 = 0; |
michael@0 | 157 | ind_min_max = 0; |
michael@0 | 158 | ind_max_min = 0; |
michael@0 | 159 | for( j = 0; j < NLSF_QUANT_DEL_DEC_STATES; j++ ) { |
michael@0 | 160 | if( min_max_Q25 > RD_max_Q25[ j ] ) { |
michael@0 | 161 | min_max_Q25 = RD_max_Q25[ j ]; |
michael@0 | 162 | ind_min_max = j; |
michael@0 | 163 | } |
michael@0 | 164 | if( max_min_Q25 < RD_min_Q25[ j ] ) { |
michael@0 | 165 | max_min_Q25 = RD_min_Q25[ j ]; |
michael@0 | 166 | ind_max_min = j; |
michael@0 | 167 | } |
michael@0 | 168 | } |
michael@0 | 169 | if( min_max_Q25 >= max_min_Q25 ) { |
michael@0 | 170 | break; |
michael@0 | 171 | } |
michael@0 | 172 | /* copy ind_min_max to ind_max_min */ |
michael@0 | 173 | ind_sort[ ind_max_min ] = ind_sort[ ind_min_max ] ^ NLSF_QUANT_DEL_DEC_STATES; |
michael@0 | 174 | RD_Q25[ ind_max_min ] = RD_Q25[ ind_min_max + NLSF_QUANT_DEL_DEC_STATES ]; |
michael@0 | 175 | prev_out_Q10[ ind_max_min ] = prev_out_Q10[ ind_min_max + NLSF_QUANT_DEL_DEC_STATES ]; |
michael@0 | 176 | RD_min_Q25[ ind_max_min ] = 0; |
michael@0 | 177 | RD_max_Q25[ ind_min_max ] = silk_int32_MAX; |
michael@0 | 178 | silk_memcpy( ind[ ind_max_min ], ind[ ind_min_max ], MAX_LPC_ORDER * sizeof( opus_int8 ) ); |
michael@0 | 179 | } |
michael@0 | 180 | /* increment index if it comes from the upper half */ |
michael@0 | 181 | for( j = 0; j < NLSF_QUANT_DEL_DEC_STATES; j++ ) { |
michael@0 | 182 | ind[ j ][ i ] += silk_RSHIFT( ind_sort[ j ], NLSF_QUANT_DEL_DEC_STATES_LOG2 ); |
michael@0 | 183 | } |
michael@0 | 184 | } else { /* i == 0 */ |
michael@0 | 185 | break; |
michael@0 | 186 | } |
michael@0 | 187 | } |
michael@0 | 188 | |
michael@0 | 189 | /* last sample: find winner, copy indices and return RD value */ |
michael@0 | 190 | ind_tmp = 0; |
michael@0 | 191 | min_Q25 = silk_int32_MAX; |
michael@0 | 192 | for( j = 0; j < 2 * NLSF_QUANT_DEL_DEC_STATES; j++ ) { |
michael@0 | 193 | if( min_Q25 > RD_Q25[ j ] ) { |
michael@0 | 194 | min_Q25 = RD_Q25[ j ]; |
michael@0 | 195 | ind_tmp = j; |
michael@0 | 196 | } |
michael@0 | 197 | } |
michael@0 | 198 | for( j = 0; j < order; j++ ) { |
michael@0 | 199 | indices[ j ] = ind[ ind_tmp & ( NLSF_QUANT_DEL_DEC_STATES - 1 ) ][ j ]; |
michael@0 | 200 | silk_assert( indices[ j ] >= -NLSF_QUANT_MAX_AMPLITUDE_EXT ); |
michael@0 | 201 | silk_assert( indices[ j ] <= NLSF_QUANT_MAX_AMPLITUDE_EXT ); |
michael@0 | 202 | } |
michael@0 | 203 | indices[ 0 ] += silk_RSHIFT( ind_tmp, NLSF_QUANT_DEL_DEC_STATES_LOG2 ); |
michael@0 | 204 | silk_assert( indices[ 0 ] <= NLSF_QUANT_MAX_AMPLITUDE_EXT ); |
michael@0 | 205 | silk_assert( min_Q25 >= 0 ); |
michael@0 | 206 | return min_Q25; |
michael@0 | 207 | } |