michael@0: /*********************************************************************** michael@0: Copyright (c) 2006-2011, Skype Limited. All rights reserved. michael@0: Redistribution and use in source and binary forms, with or without michael@0: modification, are permitted provided that the following conditions michael@0: are met: michael@0: - Redistributions of source code must retain the above copyright notice, michael@0: this list of conditions and the following disclaimer. michael@0: - Redistributions in binary form must reproduce the above copyright michael@0: notice, this list of conditions and the following disclaimer in the michael@0: documentation and/or other materials provided with the distribution. michael@0: - Neither the name of Internet Society, IETF or IETF Trust, nor the michael@0: names of specific contributors, may be used to endorse or promote michael@0: products derived from this software without specific prior written michael@0: permission. michael@0: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" michael@0: AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE michael@0: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE michael@0: ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE michael@0: LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR michael@0: CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF michael@0: SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS michael@0: INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN michael@0: CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) michael@0: ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE michael@0: POSSIBILITY OF SUCH DAMAGE. michael@0: ***********************************************************************/ michael@0: michael@0: #ifdef HAVE_CONFIG_H michael@0: #include "config.h" michael@0: #endif michael@0: michael@0: #include "main_FLP.h" michael@0: michael@0: /* Wrappers. Calls flp / fix code */ michael@0: michael@0: /* Convert AR filter coefficients to NLSF parameters */ michael@0: void silk_A2NLSF_FLP( michael@0: opus_int16 *NLSF_Q15, /* O NLSF vector [ LPC_order ] */ michael@0: const silk_float *pAR, /* I LPC coefficients [ LPC_order ] */ michael@0: const opus_int LPC_order /* I LPC order */ michael@0: ) michael@0: { michael@0: opus_int i; michael@0: opus_int32 a_fix_Q16[ MAX_LPC_ORDER ]; michael@0: michael@0: for( i = 0; i < LPC_order; i++ ) { michael@0: a_fix_Q16[ i ] = silk_float2int( pAR[ i ] * 65536.0f ); michael@0: } michael@0: michael@0: silk_A2NLSF( NLSF_Q15, a_fix_Q16, LPC_order ); michael@0: } michael@0: michael@0: /* Convert LSF parameters to AR prediction filter coefficients */ michael@0: void silk_NLSF2A_FLP( michael@0: silk_float *pAR, /* O LPC coefficients [ LPC_order ] */ michael@0: const opus_int16 *NLSF_Q15, /* I NLSF vector [ LPC_order ] */ michael@0: const opus_int LPC_order /* I LPC order */ michael@0: ) michael@0: { michael@0: opus_int i; michael@0: opus_int16 a_fix_Q12[ MAX_LPC_ORDER ]; michael@0: michael@0: silk_NLSF2A( a_fix_Q12, NLSF_Q15, LPC_order ); michael@0: michael@0: for( i = 0; i < LPC_order; i++ ) { michael@0: pAR[ i ] = ( silk_float )a_fix_Q12[ i ] * ( 1.0f / 4096.0f ); michael@0: } michael@0: } michael@0: michael@0: /******************************************/ michael@0: /* Floating-point NLSF processing wrapper */ michael@0: /******************************************/ michael@0: void silk_process_NLSFs_FLP( michael@0: silk_encoder_state *psEncC, /* I/O Encoder state */ michael@0: silk_float PredCoef[ 2 ][ MAX_LPC_ORDER ], /* O Prediction coefficients */ michael@0: opus_int16 NLSF_Q15[ MAX_LPC_ORDER ], /* I/O Normalized LSFs (quant out) (0 - (2^15-1)) */ michael@0: const opus_int16 prev_NLSF_Q15[ MAX_LPC_ORDER ] /* I Previous Normalized LSFs (0 - (2^15-1)) */ michael@0: ) michael@0: { michael@0: opus_int i, j; michael@0: opus_int16 PredCoef_Q12[ 2 ][ MAX_LPC_ORDER ]; michael@0: michael@0: silk_process_NLSFs( psEncC, PredCoef_Q12, NLSF_Q15, prev_NLSF_Q15); michael@0: michael@0: for( j = 0; j < 2; j++ ) { michael@0: for( i = 0; i < psEncC->predictLPCOrder; i++ ) { michael@0: PredCoef[ j ][ i ] = ( silk_float )PredCoef_Q12[ j ][ i ] * ( 1.0f / 4096.0f ); michael@0: } michael@0: } michael@0: } michael@0: michael@0: /****************************************/ michael@0: /* Floating-point Silk NSQ wrapper */ michael@0: /****************************************/ michael@0: void silk_NSQ_wrapper_FLP( michael@0: silk_encoder_state_FLP *psEnc, /* I/O Encoder state FLP */ michael@0: silk_encoder_control_FLP *psEncCtrl, /* I/O Encoder control FLP */ michael@0: SideInfoIndices *psIndices, /* I/O Quantization indices */ michael@0: silk_nsq_state *psNSQ, /* I/O Noise Shaping Quantzation state */ michael@0: opus_int8 pulses[], /* O Quantized pulse signal */ michael@0: const silk_float x[] /* I Prefiltered input signal */ michael@0: ) michael@0: { michael@0: opus_int i, j; michael@0: opus_int32 x_Q3[ MAX_FRAME_LENGTH ]; michael@0: opus_int32 Gains_Q16[ MAX_NB_SUBFR ]; michael@0: silk_DWORD_ALIGN opus_int16 PredCoef_Q12[ 2 ][ MAX_LPC_ORDER ]; michael@0: opus_int16 LTPCoef_Q14[ LTP_ORDER * MAX_NB_SUBFR ]; michael@0: opus_int LTP_scale_Q14; michael@0: michael@0: /* Noise shaping parameters */ michael@0: opus_int16 AR2_Q13[ MAX_NB_SUBFR * MAX_SHAPE_LPC_ORDER ]; michael@0: opus_int32 LF_shp_Q14[ MAX_NB_SUBFR ]; /* Packs two int16 coefficients per int32 value */ michael@0: opus_int Lambda_Q10; michael@0: opus_int Tilt_Q14[ MAX_NB_SUBFR ]; michael@0: opus_int HarmShapeGain_Q14[ MAX_NB_SUBFR ]; michael@0: michael@0: /* Convert control struct to fix control struct */ michael@0: /* Noise shape parameters */ michael@0: for( i = 0; i < psEnc->sCmn.nb_subfr; i++ ) { michael@0: for( j = 0; j < psEnc->sCmn.shapingLPCOrder; j++ ) { michael@0: AR2_Q13[ i * MAX_SHAPE_LPC_ORDER + j ] = silk_float2int( psEncCtrl->AR2[ i * MAX_SHAPE_LPC_ORDER + j ] * 8192.0f ); michael@0: } michael@0: } michael@0: michael@0: for( i = 0; i < psEnc->sCmn.nb_subfr; i++ ) { michael@0: LF_shp_Q14[ i ] = silk_LSHIFT32( silk_float2int( psEncCtrl->LF_AR_shp[ i ] * 16384.0f ), 16 ) | michael@0: (opus_uint16)silk_float2int( psEncCtrl->LF_MA_shp[ i ] * 16384.0f ); michael@0: Tilt_Q14[ i ] = (opus_int)silk_float2int( psEncCtrl->Tilt[ i ] * 16384.0f ); michael@0: HarmShapeGain_Q14[ i ] = (opus_int)silk_float2int( psEncCtrl->HarmShapeGain[ i ] * 16384.0f ); michael@0: } michael@0: Lambda_Q10 = ( opus_int )silk_float2int( psEncCtrl->Lambda * 1024.0f ); michael@0: michael@0: /* prediction and coding parameters */ michael@0: for( i = 0; i < psEnc->sCmn.nb_subfr * LTP_ORDER; i++ ) { michael@0: LTPCoef_Q14[ i ] = (opus_int16)silk_float2int( psEncCtrl->LTPCoef[ i ] * 16384.0f ); michael@0: } michael@0: michael@0: for( j = 0; j < 2; j++ ) { michael@0: for( i = 0; i < psEnc->sCmn.predictLPCOrder; i++ ) { michael@0: PredCoef_Q12[ j ][ i ] = (opus_int16)silk_float2int( psEncCtrl->PredCoef[ j ][ i ] * 4096.0f ); michael@0: } michael@0: } michael@0: michael@0: for( i = 0; i < psEnc->sCmn.nb_subfr; i++ ) { michael@0: Gains_Q16[ i ] = silk_float2int( psEncCtrl->Gains[ i ] * 65536.0f ); michael@0: silk_assert( Gains_Q16[ i ] > 0 ); michael@0: } michael@0: michael@0: if( psIndices->signalType == TYPE_VOICED ) { michael@0: LTP_scale_Q14 = silk_LTPScales_table_Q14[ psIndices->LTP_scaleIndex ]; michael@0: } else { michael@0: LTP_scale_Q14 = 0; michael@0: } michael@0: michael@0: /* Convert input to fix */ michael@0: for( i = 0; i < psEnc->sCmn.frame_length; i++ ) { michael@0: x_Q3[ i ] = silk_float2int( 8.0f * x[ i ] ); michael@0: } michael@0: michael@0: /* Call NSQ */ michael@0: if( psEnc->sCmn.nStatesDelayedDecision > 1 || psEnc->sCmn.warping_Q16 > 0 ) { michael@0: silk_NSQ_del_dec( &psEnc->sCmn, psNSQ, psIndices, x_Q3, pulses, PredCoef_Q12[ 0 ], LTPCoef_Q14, michael@0: AR2_Q13, HarmShapeGain_Q14, Tilt_Q14, LF_shp_Q14, Gains_Q16, psEncCtrl->pitchL, Lambda_Q10, LTP_scale_Q14 ); michael@0: } else { michael@0: silk_NSQ( &psEnc->sCmn, psNSQ, psIndices, x_Q3, pulses, PredCoef_Q12[ 0 ], LTPCoef_Q14, michael@0: AR2_Q13, HarmShapeGain_Q14, Tilt_Q14, LF_shp_Q14, Gains_Q16, psEncCtrl->pitchL, Lambda_Q10, LTP_scale_Q14 ); michael@0: } michael@0: } michael@0: michael@0: /***********************************************/ michael@0: /* Floating-point Silk LTP quantiation wrapper */ michael@0: /***********************************************/ michael@0: void silk_quant_LTP_gains_FLP( michael@0: silk_float B[ MAX_NB_SUBFR * LTP_ORDER ], /* I/O (Un-)quantized LTP gains */ michael@0: opus_int8 cbk_index[ MAX_NB_SUBFR ], /* O Codebook index */ michael@0: opus_int8 *periodicity_index, /* O Periodicity index */ michael@0: opus_int32 *sum_log_gain_Q7, /* I/O Cumulative max prediction gain */ michael@0: const silk_float W[ MAX_NB_SUBFR * LTP_ORDER * LTP_ORDER ], /* I Error weights */ michael@0: const opus_int mu_Q10, /* I Mu value (R/D tradeoff) */ michael@0: const opus_int lowComplexity, /* I Flag for low complexity */ michael@0: const opus_int nb_subfr /* I number of subframes */ michael@0: ) michael@0: { michael@0: opus_int i; michael@0: opus_int16 B_Q14[ MAX_NB_SUBFR * LTP_ORDER ]; michael@0: opus_int32 W_Q18[ MAX_NB_SUBFR*LTP_ORDER*LTP_ORDER ]; michael@0: michael@0: for( i = 0; i < nb_subfr * LTP_ORDER; i++ ) { michael@0: B_Q14[ i ] = (opus_int16)silk_float2int( B[ i ] * 16384.0f ); michael@0: } michael@0: for( i = 0; i < nb_subfr * LTP_ORDER * LTP_ORDER; i++ ) { michael@0: W_Q18[ i ] = (opus_int32)silk_float2int( W[ i ] * 262144.0f ); michael@0: } michael@0: michael@0: silk_quant_LTP_gains( B_Q14, cbk_index, periodicity_index, sum_log_gain_Q7, W_Q18, mu_Q10, lowComplexity, nb_subfr ); michael@0: michael@0: for( i = 0; i < nb_subfr * LTP_ORDER; i++ ) { michael@0: B[ i ] = (silk_float)B_Q14[ i ] * ( 1.0f / 16384.0f ); michael@0: } michael@0: }