annotate src/opus-1.3/silk/NLSF_del_dec_quant.c @ 81:7029a4916348

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