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_FLP.h" |
michael@0 | 33 | |
michael@0 | 34 | /* Wrappers. Calls flp / fix code */ |
michael@0 | 35 | |
michael@0 | 36 | /* Convert AR filter coefficients to NLSF parameters */ |
michael@0 | 37 | void silk_A2NLSF_FLP( |
michael@0 | 38 | opus_int16 *NLSF_Q15, /* O NLSF vector [ LPC_order ] */ |
michael@0 | 39 | const silk_float *pAR, /* I LPC coefficients [ LPC_order ] */ |
michael@0 | 40 | const opus_int LPC_order /* I LPC order */ |
michael@0 | 41 | ) |
michael@0 | 42 | { |
michael@0 | 43 | opus_int i; |
michael@0 | 44 | opus_int32 a_fix_Q16[ MAX_LPC_ORDER ]; |
michael@0 | 45 | |
michael@0 | 46 | for( i = 0; i < LPC_order; i++ ) { |
michael@0 | 47 | a_fix_Q16[ i ] = silk_float2int( pAR[ i ] * 65536.0f ); |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | silk_A2NLSF( NLSF_Q15, a_fix_Q16, LPC_order ); |
michael@0 | 51 | } |
michael@0 | 52 | |
michael@0 | 53 | /* Convert LSF parameters to AR prediction filter coefficients */ |
michael@0 | 54 | void silk_NLSF2A_FLP( |
michael@0 | 55 | silk_float *pAR, /* O LPC coefficients [ LPC_order ] */ |
michael@0 | 56 | const opus_int16 *NLSF_Q15, /* I NLSF vector [ LPC_order ] */ |
michael@0 | 57 | const opus_int LPC_order /* I LPC order */ |
michael@0 | 58 | ) |
michael@0 | 59 | { |
michael@0 | 60 | opus_int i; |
michael@0 | 61 | opus_int16 a_fix_Q12[ MAX_LPC_ORDER ]; |
michael@0 | 62 | |
michael@0 | 63 | silk_NLSF2A( a_fix_Q12, NLSF_Q15, LPC_order ); |
michael@0 | 64 | |
michael@0 | 65 | for( i = 0; i < LPC_order; i++ ) { |
michael@0 | 66 | pAR[ i ] = ( silk_float )a_fix_Q12[ i ] * ( 1.0f / 4096.0f ); |
michael@0 | 67 | } |
michael@0 | 68 | } |
michael@0 | 69 | |
michael@0 | 70 | /******************************************/ |
michael@0 | 71 | /* Floating-point NLSF processing wrapper */ |
michael@0 | 72 | /******************************************/ |
michael@0 | 73 | void silk_process_NLSFs_FLP( |
michael@0 | 74 | silk_encoder_state *psEncC, /* I/O Encoder state */ |
michael@0 | 75 | silk_float PredCoef[ 2 ][ MAX_LPC_ORDER ], /* O Prediction coefficients */ |
michael@0 | 76 | opus_int16 NLSF_Q15[ MAX_LPC_ORDER ], /* I/O Normalized LSFs (quant out) (0 - (2^15-1)) */ |
michael@0 | 77 | const opus_int16 prev_NLSF_Q15[ MAX_LPC_ORDER ] /* I Previous Normalized LSFs (0 - (2^15-1)) */ |
michael@0 | 78 | ) |
michael@0 | 79 | { |
michael@0 | 80 | opus_int i, j; |
michael@0 | 81 | opus_int16 PredCoef_Q12[ 2 ][ MAX_LPC_ORDER ]; |
michael@0 | 82 | |
michael@0 | 83 | silk_process_NLSFs( psEncC, PredCoef_Q12, NLSF_Q15, prev_NLSF_Q15); |
michael@0 | 84 | |
michael@0 | 85 | for( j = 0; j < 2; j++ ) { |
michael@0 | 86 | for( i = 0; i < psEncC->predictLPCOrder; i++ ) { |
michael@0 | 87 | PredCoef[ j ][ i ] = ( silk_float )PredCoef_Q12[ j ][ i ] * ( 1.0f / 4096.0f ); |
michael@0 | 88 | } |
michael@0 | 89 | } |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | /****************************************/ |
michael@0 | 93 | /* Floating-point Silk NSQ wrapper */ |
michael@0 | 94 | /****************************************/ |
michael@0 | 95 | void silk_NSQ_wrapper_FLP( |
michael@0 | 96 | silk_encoder_state_FLP *psEnc, /* I/O Encoder state FLP */ |
michael@0 | 97 | silk_encoder_control_FLP *psEncCtrl, /* I/O Encoder control FLP */ |
michael@0 | 98 | SideInfoIndices *psIndices, /* I/O Quantization indices */ |
michael@0 | 99 | silk_nsq_state *psNSQ, /* I/O Noise Shaping Quantzation state */ |
michael@0 | 100 | opus_int8 pulses[], /* O Quantized pulse signal */ |
michael@0 | 101 | const silk_float x[] /* I Prefiltered input signal */ |
michael@0 | 102 | ) |
michael@0 | 103 | { |
michael@0 | 104 | opus_int i, j; |
michael@0 | 105 | opus_int32 x_Q3[ MAX_FRAME_LENGTH ]; |
michael@0 | 106 | opus_int32 Gains_Q16[ MAX_NB_SUBFR ]; |
michael@0 | 107 | silk_DWORD_ALIGN opus_int16 PredCoef_Q12[ 2 ][ MAX_LPC_ORDER ]; |
michael@0 | 108 | opus_int16 LTPCoef_Q14[ LTP_ORDER * MAX_NB_SUBFR ]; |
michael@0 | 109 | opus_int LTP_scale_Q14; |
michael@0 | 110 | |
michael@0 | 111 | /* Noise shaping parameters */ |
michael@0 | 112 | opus_int16 AR2_Q13[ MAX_NB_SUBFR * MAX_SHAPE_LPC_ORDER ]; |
michael@0 | 113 | opus_int32 LF_shp_Q14[ MAX_NB_SUBFR ]; /* Packs two int16 coefficients per int32 value */ |
michael@0 | 114 | opus_int Lambda_Q10; |
michael@0 | 115 | opus_int Tilt_Q14[ MAX_NB_SUBFR ]; |
michael@0 | 116 | opus_int HarmShapeGain_Q14[ MAX_NB_SUBFR ]; |
michael@0 | 117 | |
michael@0 | 118 | /* Convert control struct to fix control struct */ |
michael@0 | 119 | /* Noise shape parameters */ |
michael@0 | 120 | for( i = 0; i < psEnc->sCmn.nb_subfr; i++ ) { |
michael@0 | 121 | for( j = 0; j < psEnc->sCmn.shapingLPCOrder; j++ ) { |
michael@0 | 122 | AR2_Q13[ i * MAX_SHAPE_LPC_ORDER + j ] = silk_float2int( psEncCtrl->AR2[ i * MAX_SHAPE_LPC_ORDER + j ] * 8192.0f ); |
michael@0 | 123 | } |
michael@0 | 124 | } |
michael@0 | 125 | |
michael@0 | 126 | for( i = 0; i < psEnc->sCmn.nb_subfr; i++ ) { |
michael@0 | 127 | LF_shp_Q14[ i ] = silk_LSHIFT32( silk_float2int( psEncCtrl->LF_AR_shp[ i ] * 16384.0f ), 16 ) | |
michael@0 | 128 | (opus_uint16)silk_float2int( psEncCtrl->LF_MA_shp[ i ] * 16384.0f ); |
michael@0 | 129 | Tilt_Q14[ i ] = (opus_int)silk_float2int( psEncCtrl->Tilt[ i ] * 16384.0f ); |
michael@0 | 130 | HarmShapeGain_Q14[ i ] = (opus_int)silk_float2int( psEncCtrl->HarmShapeGain[ i ] * 16384.0f ); |
michael@0 | 131 | } |
michael@0 | 132 | Lambda_Q10 = ( opus_int )silk_float2int( psEncCtrl->Lambda * 1024.0f ); |
michael@0 | 133 | |
michael@0 | 134 | /* prediction and coding parameters */ |
michael@0 | 135 | for( i = 0; i < psEnc->sCmn.nb_subfr * LTP_ORDER; i++ ) { |
michael@0 | 136 | LTPCoef_Q14[ i ] = (opus_int16)silk_float2int( psEncCtrl->LTPCoef[ i ] * 16384.0f ); |
michael@0 | 137 | } |
michael@0 | 138 | |
michael@0 | 139 | for( j = 0; j < 2; j++ ) { |
michael@0 | 140 | for( i = 0; i < psEnc->sCmn.predictLPCOrder; i++ ) { |
michael@0 | 141 | PredCoef_Q12[ j ][ i ] = (opus_int16)silk_float2int( psEncCtrl->PredCoef[ j ][ i ] * 4096.0f ); |
michael@0 | 142 | } |
michael@0 | 143 | } |
michael@0 | 144 | |
michael@0 | 145 | for( i = 0; i < psEnc->sCmn.nb_subfr; i++ ) { |
michael@0 | 146 | Gains_Q16[ i ] = silk_float2int( psEncCtrl->Gains[ i ] * 65536.0f ); |
michael@0 | 147 | silk_assert( Gains_Q16[ i ] > 0 ); |
michael@0 | 148 | } |
michael@0 | 149 | |
michael@0 | 150 | if( psIndices->signalType == TYPE_VOICED ) { |
michael@0 | 151 | LTP_scale_Q14 = silk_LTPScales_table_Q14[ psIndices->LTP_scaleIndex ]; |
michael@0 | 152 | } else { |
michael@0 | 153 | LTP_scale_Q14 = 0; |
michael@0 | 154 | } |
michael@0 | 155 | |
michael@0 | 156 | /* Convert input to fix */ |
michael@0 | 157 | for( i = 0; i < psEnc->sCmn.frame_length; i++ ) { |
michael@0 | 158 | x_Q3[ i ] = silk_float2int( 8.0f * x[ i ] ); |
michael@0 | 159 | } |
michael@0 | 160 | |
michael@0 | 161 | /* Call NSQ */ |
michael@0 | 162 | if( psEnc->sCmn.nStatesDelayedDecision > 1 || psEnc->sCmn.warping_Q16 > 0 ) { |
michael@0 | 163 | silk_NSQ_del_dec( &psEnc->sCmn, psNSQ, psIndices, x_Q3, pulses, PredCoef_Q12[ 0 ], LTPCoef_Q14, |
michael@0 | 164 | AR2_Q13, HarmShapeGain_Q14, Tilt_Q14, LF_shp_Q14, Gains_Q16, psEncCtrl->pitchL, Lambda_Q10, LTP_scale_Q14 ); |
michael@0 | 165 | } else { |
michael@0 | 166 | silk_NSQ( &psEnc->sCmn, psNSQ, psIndices, x_Q3, pulses, PredCoef_Q12[ 0 ], LTPCoef_Q14, |
michael@0 | 167 | AR2_Q13, HarmShapeGain_Q14, Tilt_Q14, LF_shp_Q14, Gains_Q16, psEncCtrl->pitchL, Lambda_Q10, LTP_scale_Q14 ); |
michael@0 | 168 | } |
michael@0 | 169 | } |
michael@0 | 170 | |
michael@0 | 171 | /***********************************************/ |
michael@0 | 172 | /* Floating-point Silk LTP quantiation wrapper */ |
michael@0 | 173 | /***********************************************/ |
michael@0 | 174 | void silk_quant_LTP_gains_FLP( |
michael@0 | 175 | silk_float B[ MAX_NB_SUBFR * LTP_ORDER ], /* I/O (Un-)quantized LTP gains */ |
michael@0 | 176 | opus_int8 cbk_index[ MAX_NB_SUBFR ], /* O Codebook index */ |
michael@0 | 177 | opus_int8 *periodicity_index, /* O Periodicity index */ |
michael@0 | 178 | opus_int32 *sum_log_gain_Q7, /* I/O Cumulative max prediction gain */ |
michael@0 | 179 | const silk_float W[ MAX_NB_SUBFR * LTP_ORDER * LTP_ORDER ], /* I Error weights */ |
michael@0 | 180 | const opus_int mu_Q10, /* I Mu value (R/D tradeoff) */ |
michael@0 | 181 | const opus_int lowComplexity, /* I Flag for low complexity */ |
michael@0 | 182 | const opus_int nb_subfr /* I number of subframes */ |
michael@0 | 183 | ) |
michael@0 | 184 | { |
michael@0 | 185 | opus_int i; |
michael@0 | 186 | opus_int16 B_Q14[ MAX_NB_SUBFR * LTP_ORDER ]; |
michael@0 | 187 | opus_int32 W_Q18[ MAX_NB_SUBFR*LTP_ORDER*LTP_ORDER ]; |
michael@0 | 188 | |
michael@0 | 189 | for( i = 0; i < nb_subfr * LTP_ORDER; i++ ) { |
michael@0 | 190 | B_Q14[ i ] = (opus_int16)silk_float2int( B[ i ] * 16384.0f ); |
michael@0 | 191 | } |
michael@0 | 192 | for( i = 0; i < nb_subfr * LTP_ORDER * LTP_ORDER; i++ ) { |
michael@0 | 193 | W_Q18[ i ] = (opus_int32)silk_float2int( W[ i ] * 262144.0f ); |
michael@0 | 194 | } |
michael@0 | 195 | |
michael@0 | 196 | silk_quant_LTP_gains( B_Q14, cbk_index, periodicity_index, sum_log_gain_Q7, W_Q18, mu_Q10, lowComplexity, nb_subfr ); |
michael@0 | 197 | |
michael@0 | 198 | for( i = 0; i < nb_subfr * LTP_ORDER; i++ ) { |
michael@0 | 199 | B[ i ] = (silk_float)B_Q14[ i ] * ( 1.0f / 16384.0f ); |
michael@0 | 200 | } |
michael@0 | 201 | } |