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