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 /* Delayed-decision quantizer for NLSF residuals */ 35 opus_int32 silk_NLSF_del_dec_quant( /* O Returns RD value in Q25 */ 36 opus_int8 indices[], /* O Quantization indices [ order ] */ 37 const opus_int16 x_Q10[], /* I Input [ order ] */ 38 const opus_int16 w_Q5[], /* I Weights [ order ] */ 39 const opus_uint8 pred_coef_Q8[], /* I Backward predictor coefs [ order ] */ 40 const opus_int16 ec_ix[], /* I Indices to entropy coding tables [ order ] */ 41 const opus_uint8 ec_rates_Q5[], /* I Rates [] */ 42 const opus_int quant_step_size_Q16, /* I Quantization step size */ 43 const opus_int16 inv_quant_step_size_Q6, /* I Inverse quantization step size */ 44 const opus_int32 mu_Q20, /* I R/D tradeoff */ 45 const opus_int16 order /* I Number of input values */ 46 ) 47 { 48 opus_int i, j, nStates, ind_tmp, ind_min_max, ind_max_min, in_Q10, res_Q10; 49 opus_int pred_Q10, diff_Q10, out0_Q10, out1_Q10, rate0_Q5, rate1_Q5; 50 opus_int32 RD_tmp_Q25, min_Q25, min_max_Q25, max_min_Q25, pred_coef_Q16; 51 opus_int ind_sort[ NLSF_QUANT_DEL_DEC_STATES ]; 52 opus_int8 ind[ NLSF_QUANT_DEL_DEC_STATES ][ MAX_LPC_ORDER ]; 53 opus_int16 prev_out_Q10[ 2 * NLSF_QUANT_DEL_DEC_STATES ]; 54 opus_int32 RD_Q25[ 2 * NLSF_QUANT_DEL_DEC_STATES ]; 55 opus_int32 RD_min_Q25[ NLSF_QUANT_DEL_DEC_STATES ]; 56 opus_int32 RD_max_Q25[ NLSF_QUANT_DEL_DEC_STATES ]; 57 const opus_uint8 *rates_Q5; 58 59 silk_assert( (NLSF_QUANT_DEL_DEC_STATES & (NLSF_QUANT_DEL_DEC_STATES-1)) == 0 ); /* must be power of two */ 60 61 nStates = 1; 62 RD_Q25[ 0 ] = 0; 63 prev_out_Q10[ 0 ] = 0; 64 for( i = order - 1; ; i-- ) { 65 rates_Q5 = &ec_rates_Q5[ ec_ix[ i ] ]; 66 pred_coef_Q16 = silk_LSHIFT( (opus_int32)pred_coef_Q8[ i ], 8 ); 67 in_Q10 = x_Q10[ i ]; 68 for( j = 0; j < nStates; j++ ) { 69 pred_Q10 = silk_SMULWB( pred_coef_Q16, prev_out_Q10[ j ] ); 70 res_Q10 = silk_SUB16( in_Q10, pred_Q10 ); 71 ind_tmp = silk_SMULWB( (opus_int32)inv_quant_step_size_Q6, res_Q10 ); 72 ind_tmp = silk_LIMIT( ind_tmp, -NLSF_QUANT_MAX_AMPLITUDE_EXT, NLSF_QUANT_MAX_AMPLITUDE_EXT-1 ); 73 ind[ j ][ i ] = (opus_int8)ind_tmp; 74 75 /* compute outputs for ind_tmp and ind_tmp + 1 */ 76 out0_Q10 = silk_LSHIFT( ind_tmp, 10 ); 77 out1_Q10 = silk_ADD16( out0_Q10, 1024 ); 78 if( ind_tmp > 0 ) { 79 out0_Q10 = silk_SUB16( out0_Q10, SILK_FIX_CONST( NLSF_QUANT_LEVEL_ADJ, 10 ) ); 80 out1_Q10 = silk_SUB16( out1_Q10, SILK_FIX_CONST( NLSF_QUANT_LEVEL_ADJ, 10 ) ); 81 } else if( ind_tmp == 0 ) { 82 out1_Q10 = silk_SUB16( out1_Q10, SILK_FIX_CONST( NLSF_QUANT_LEVEL_ADJ, 10 ) ); 83 } else if( ind_tmp == -1 ) { 84 out0_Q10 = silk_ADD16( out0_Q10, SILK_FIX_CONST( NLSF_QUANT_LEVEL_ADJ, 10 ) ); 85 } else { 86 out0_Q10 = silk_ADD16( out0_Q10, SILK_FIX_CONST( NLSF_QUANT_LEVEL_ADJ, 10 ) ); 87 out1_Q10 = silk_ADD16( out1_Q10, SILK_FIX_CONST( NLSF_QUANT_LEVEL_ADJ, 10 ) ); 88 } 89 out0_Q10 = silk_SMULWB( (opus_int32)out0_Q10, quant_step_size_Q16 ); 90 out1_Q10 = silk_SMULWB( (opus_int32)out1_Q10, quant_step_size_Q16 ); 91 out0_Q10 = silk_ADD16( out0_Q10, pred_Q10 ); 92 out1_Q10 = silk_ADD16( out1_Q10, pred_Q10 ); 93 prev_out_Q10[ j ] = out0_Q10; 94 prev_out_Q10[ j + nStates ] = out1_Q10; 95 96 /* compute RD for ind_tmp and ind_tmp + 1 */ 97 if( ind_tmp + 1 >= NLSF_QUANT_MAX_AMPLITUDE ) { 98 if( ind_tmp + 1 == NLSF_QUANT_MAX_AMPLITUDE ) { 99 rate0_Q5 = rates_Q5[ ind_tmp + NLSF_QUANT_MAX_AMPLITUDE ]; 100 rate1_Q5 = 280; 101 } else { 102 rate0_Q5 = silk_SMLABB( 280 - 43 * NLSF_QUANT_MAX_AMPLITUDE, 43, ind_tmp ); 103 rate1_Q5 = silk_ADD16( rate0_Q5, 43 ); 104 } 105 } else if( ind_tmp <= -NLSF_QUANT_MAX_AMPLITUDE ) { 106 if( ind_tmp == -NLSF_QUANT_MAX_AMPLITUDE ) { 107 rate0_Q5 = 280; 108 rate1_Q5 = rates_Q5[ ind_tmp + 1 + NLSF_QUANT_MAX_AMPLITUDE ]; 109 } else { 110 rate0_Q5 = silk_SMLABB( 280 - 43 * NLSF_QUANT_MAX_AMPLITUDE, -43, ind_tmp ); 111 rate1_Q5 = silk_SUB16( rate0_Q5, 43 ); 112 } 113 } else { 114 rate0_Q5 = rates_Q5[ ind_tmp + NLSF_QUANT_MAX_AMPLITUDE ]; 115 rate1_Q5 = rates_Q5[ ind_tmp + 1 + NLSF_QUANT_MAX_AMPLITUDE ]; 116 } 117 RD_tmp_Q25 = RD_Q25[ j ]; 118 diff_Q10 = silk_SUB16( in_Q10, out0_Q10 ); 119 RD_Q25[ j ] = silk_SMLABB( silk_MLA( RD_tmp_Q25, silk_SMULBB( diff_Q10, diff_Q10 ), w_Q5[ i ] ), mu_Q20, rate0_Q5 ); 120 diff_Q10 = silk_SUB16( in_Q10, out1_Q10 ); 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 ); 122 } 123 124 if( nStates <= ( NLSF_QUANT_DEL_DEC_STATES >> 1 ) ) { 125 /* double number of states and copy */ 126 for( j = 0; j < nStates; j++ ) { 127 ind[ j + nStates ][ i ] = ind[ j ][ i ] + 1; 128 } 129 nStates = silk_LSHIFT( nStates, 1 ); 130 for( j = nStates; j < NLSF_QUANT_DEL_DEC_STATES; j++ ) { 131 ind[ j ][ i ] = ind[ j - nStates ][ i ]; 132 } 133 } else if( i > 0 ) { 134 /* sort lower and upper half of RD_Q25, pairwise */ 135 for( j = 0; j < NLSF_QUANT_DEL_DEC_STATES; j++ ) { 136 if( RD_Q25[ j ] > RD_Q25[ j + NLSF_QUANT_DEL_DEC_STATES ] ) { 137 RD_max_Q25[ j ] = RD_Q25[ j ]; 138 RD_min_Q25[ j ] = RD_Q25[ j + NLSF_QUANT_DEL_DEC_STATES ]; 139 RD_Q25[ j ] = RD_min_Q25[ j ]; 140 RD_Q25[ j + NLSF_QUANT_DEL_DEC_STATES ] = RD_max_Q25[ j ]; 141 /* swap prev_out values */ 142 out0_Q10 = prev_out_Q10[ j ]; 143 prev_out_Q10[ j ] = prev_out_Q10[ j + NLSF_QUANT_DEL_DEC_STATES ]; 144 prev_out_Q10[ j + NLSF_QUANT_DEL_DEC_STATES ] = out0_Q10; 145 ind_sort[ j ] = j + NLSF_QUANT_DEL_DEC_STATES; 146 } else { 147 RD_min_Q25[ j ] = RD_Q25[ j ]; 148 RD_max_Q25[ j ] = RD_Q25[ j + NLSF_QUANT_DEL_DEC_STATES ]; 149 ind_sort[ j ] = j; 150 } 151 } 152 /* compare the highest RD values of the winning half with the lowest one in the losing half, and copy if necessary */ 153 /* afterwards ind_sort[] will contain the indices of the NLSF_QUANT_DEL_DEC_STATES winning RD values */ 154 while( 1 ) { 155 min_max_Q25 = silk_int32_MAX; 156 max_min_Q25 = 0; 157 ind_min_max = 0; 158 ind_max_min = 0; 159 for( j = 0; j < NLSF_QUANT_DEL_DEC_STATES; j++ ) { 160 if( min_max_Q25 > RD_max_Q25[ j ] ) { 161 min_max_Q25 = RD_max_Q25[ j ]; 162 ind_min_max = j; 163 } 164 if( max_min_Q25 < RD_min_Q25[ j ] ) { 165 max_min_Q25 = RD_min_Q25[ j ]; 166 ind_max_min = j; 167 } 168 } 169 if( min_max_Q25 >= max_min_Q25 ) { 170 break; 171 } 172 /* copy ind_min_max to ind_max_min */ 173 ind_sort[ ind_max_min ] = ind_sort[ ind_min_max ] ^ NLSF_QUANT_DEL_DEC_STATES; 174 RD_Q25[ ind_max_min ] = RD_Q25[ ind_min_max + NLSF_QUANT_DEL_DEC_STATES ]; 175 prev_out_Q10[ ind_max_min ] = prev_out_Q10[ ind_min_max + NLSF_QUANT_DEL_DEC_STATES ]; 176 RD_min_Q25[ ind_max_min ] = 0; 177 RD_max_Q25[ ind_min_max ] = silk_int32_MAX; 178 silk_memcpy( ind[ ind_max_min ], ind[ ind_min_max ], MAX_LPC_ORDER * sizeof( opus_int8 ) ); 179 } 180 /* increment index if it comes from the upper half */ 181 for( j = 0; j < NLSF_QUANT_DEL_DEC_STATES; j++ ) { 182 ind[ j ][ i ] += silk_RSHIFT( ind_sort[ j ], NLSF_QUANT_DEL_DEC_STATES_LOG2 ); 183 } 184 } else { /* i == 0 */ 185 break; 186 } 187 } 188 189 /* last sample: find winner, copy indices and return RD value */ 190 ind_tmp = 0; 191 min_Q25 = silk_int32_MAX; 192 for( j = 0; j < 2 * NLSF_QUANT_DEL_DEC_STATES; j++ ) { 193 if( min_Q25 > RD_Q25[ j ] ) { 194 min_Q25 = RD_Q25[ j ]; 195 ind_tmp = j; 196 } 197 } 198 for( j = 0; j < order; j++ ) { 199 indices[ j ] = ind[ ind_tmp & ( NLSF_QUANT_DEL_DEC_STATES - 1 ) ][ j ]; 200 silk_assert( indices[ j ] >= -NLSF_QUANT_MAX_AMPLITUDE_EXT ); 201 silk_assert( indices[ j ] <= NLSF_QUANT_MAX_AMPLITUDE_EXT ); 202 } 203 indices[ 0 ] += silk_RSHIFT( ind_tmp, NLSF_QUANT_DEL_DEC_STATES_LOG2 ); 204 silk_assert( indices[ 0 ] <= NLSF_QUANT_MAX_AMPLITUDE_EXT ); 205 silk_assert( min_Q25 >= 0 ); 206 return min_Q25; 207 } 208