Fixing silk fixed point
[opus.git] / silk / silk_control_codec.c
1 /***********************************************************************\r
2 Copyright (c) 2006-2011, Skype Limited. All rights reserved. \r
3 Redistribution and use in source and binary forms, with or without \r
4 modification, (subject to the limitations in the disclaimer below) \r
5 are permitted provided that the following conditions are met:\r
6 - Redistributions of source code must retain the above copyright notice,\r
7 this list of conditions and the following disclaimer.\r
8 - Redistributions in binary form must reproduce the above copyright \r
9 notice, this list of conditions and the following disclaimer in the \r
10 documentation and/or other materials provided with the distribution.\r
11 - Neither the name of Skype Limited, nor the names of specific \r
12 contributors, may be used to endorse or promote products derived from \r
13 this software without specific prior written permission.\r
14 NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED \r
15 BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND \r
16 CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,\r
17 BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND \r
18 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE \r
19 COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, \r
20 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\r
21 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF \r
22 USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON \r
23 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT \r
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE \r
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
26 ***********************************************************************/\r
27 \r
28 #ifdef HAVE_CONFIG_H\r
29 #include "config.h"\r
30 #endif\r
31 \r
32 #include "silk_tuning_parameters.h"\r
33 #ifdef FIXED_POINT\r
34 #include "silk_main_FIX.h"\r
35 #define silk_encoder_state_Fxx      silk_encoder_state_FIX\r
36 #else\r
37 #include "silk_main_FLP.h"\r
38 #define silk_encoder_state_Fxx      silk_encoder_state_FLP\r
39 #endif\r
40 \r
41 SKP_int silk_setup_resamplers(\r
42     silk_encoder_state_Fxx          *psEnc,             /* I/O                      */\r
43     SKP_int                         fs_kHz              /* I                        */\r
44 );\r
45 \r
46 SKP_int silk_setup_fs(\r
47     silk_encoder_state_Fxx          *psEnc,             /* I/O                      */\r
48     SKP_int                         fs_kHz,             /* I                        */\r
49     SKP_int                         PacketSize_ms       /* I                        */\r
50 );\r
51 \r
52 SKP_int silk_setup_complexity(\r
53     silk_encoder_state              *psEncC,            /* I/O                      */\r
54     SKP_int                         Complexity          /* I                        */\r
55 );\r
56 \r
57 SKP_INLINE SKP_int silk_setup_LBRR(\r
58     silk_encoder_state                  *psEncC,            /* I/O                      */\r
59     const SKP_int32                 TargetRate_bps      /* I                        */\r
60 );\r
61 \r
62 \r
63 /* Control encoder */\r
64 SKP_int silk_control_encoder( \r
65     silk_encoder_state_Fxx          *psEnc,             /* I/O  Pointer to Silk encoder state           */\r
66     silk_EncControlStruct           *encControl,        /* I:   Control structure                       */\r
67     const SKP_int32                 TargetRate_bps,     /* I    Target max bitrate (bps)                */\r
68     const SKP_int                   allow_bw_switch,    /* I    Flag to allow switching audio bandwidth */\r
69     const SKP_int                   channelNb           /* I    Channel number                          */\r
70 )\r
71 {\r
72     SKP_int   fs_kHz, ret = 0;\r
73 \r
74     psEnc->sCmn.useDTX                 = encControl->useDTX;\r
75     psEnc->sCmn.useCBR                 = encControl->useCBR;\r
76     psEnc->sCmn.API_fs_Hz              = encControl->API_sampleRate;\r
77     psEnc->sCmn.maxInternal_fs_Hz      = encControl->maxInternalSampleRate;\r
78     psEnc->sCmn.minInternal_fs_Hz      = encControl->minInternalSampleRate;\r
79     psEnc->sCmn.desiredInternal_fs_Hz  = encControl->desiredInternalSampleRate;\r
80     psEnc->sCmn.useInBandFEC           = encControl->useInBandFEC;\r
81     psEnc->sCmn.nChannelsAPI           = encControl->nChannelsAPI;\r
82     psEnc->sCmn.nChannelsInternal      = encControl->nChannelsInternal;\r
83     psEnc->sCmn.allow_bandwidth_switch = allow_bw_switch;\r
84     psEnc->sCmn.channelNb              = channelNb;\r
85 \r
86     if( psEnc->sCmn.controlled_since_last_payload != 0 && psEnc->sCmn.prefillFlag == 0 ) {\r
87         if( psEnc->sCmn.API_fs_Hz != psEnc->sCmn.prev_API_fs_Hz && psEnc->sCmn.fs_kHz > 0 ) {\r
88             /* Change in API sampling rate in the middle of encoding a packet */\r
89             ret += silk_setup_resamplers( psEnc, psEnc->sCmn.fs_kHz );\r
90         }\r
91         return ret;\r
92     }\r
93 \r
94     /* Beyond this point we know that there are no previously coded frames in the payload buffer */\r
95 \r
96     /********************************************/\r
97     /* Determine internal sampling rate         */\r
98     /********************************************/\r
99     fs_kHz = silk_control_audio_bandwidth( &psEnc->sCmn );\r
100 \r
101     /********************************************/\r
102     /* Prepare resampler and buffered data      */\r
103     /********************************************/\r
104     ret += silk_setup_resamplers( psEnc, fs_kHz );\r
105 \r
106     /********************************************/\r
107     /* Set internal sampling frequency          */\r
108     /********************************************/\r
109     ret += silk_setup_fs( psEnc, fs_kHz, encControl->payloadSize_ms );\r
110 \r
111     /********************************************/\r
112     /* Set encoding complexity                  */\r
113     /********************************************/\r
114     ret += silk_setup_complexity( &psEnc->sCmn, encControl->complexity  );\r
115 \r
116     /********************************************/\r
117     /* Set packet loss rate measured by farend  */\r
118     /********************************************/\r
119     psEnc->sCmn.PacketLoss_perc = encControl->packetLossPercentage;\r
120 \r
121     /********************************************/\r
122     /* Set LBRR usage                           */\r
123     /********************************************/\r
124     ret += silk_setup_LBRR( &psEnc->sCmn, TargetRate_bps );\r
125 \r
126     psEnc->sCmn.controlled_since_last_payload = 1;\r
127 \r
128     return ret;\r
129 }\r
130 \r
131 SKP_int silk_setup_resamplers(\r
132     silk_encoder_state_Fxx          *psEnc,             /* I/O                      */\r
133     SKP_int                         fs_kHz              /* I                        */\r
134 )\r
135 {\r
136     SKP_int   ret = SILK_NO_ERROR;\r
137     SKP_int32 nSamples_temp;\r
138     \r
139     if( psEnc->sCmn.fs_kHz != fs_kHz || psEnc->sCmn.prev_API_fs_Hz != psEnc->sCmn.API_fs_Hz ) \r
140     {\r
141         if( psEnc->sCmn.fs_kHz == 0 ) {\r
142             /* Initialize the resampler for enc_API.c preparing resampling from API_fs_Hz to fs_kHz */\r
143             ret += silk_resampler_init( &psEnc->sCmn.resampler_state, psEnc->sCmn.API_fs_Hz, fs_kHz * 1000 );\r
144         } else {\r
145             /* Allocate worst case space for temporary upsampling, 8 to 48 kHz, so a factor 6 */\r
146             SKP_int16 x_buf_API_fs_Hz[ ( 2 * MAX_FRAME_LENGTH_MS + LA_SHAPE_MS ) * MAX_API_FS_KHZ ];\r
147 #ifdef FIXED_POINT\r
148             SKP_int16 *x_bufFIX = psEnc->x_buf;\r
149 #else\r
150             SKP_int16 x_bufFIX[ 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ]; \r
151 #endif\r
152 \r
153             nSamples_temp = SKP_LSHIFT( psEnc->sCmn.frame_length, 1 ) + LA_SHAPE_MS * psEnc->sCmn.fs_kHz;\r
154 \r
155 #ifndef FIXED_POINT\r
156             SKP_float2short_array( x_bufFIX, psEnc->x_buf, nSamples_temp );\r
157 #endif\r
158 \r
159             if( SKP_SMULBB( fs_kHz, 1000 ) < psEnc->sCmn.API_fs_Hz && psEnc->sCmn.fs_kHz != 0 ) {\r
160                 /* Resample buffered data in x_buf to API_fs_Hz */\r
161 \r
162                 silk_resampler_state_struct  temp_resampler_state;\r
163 \r
164                 /* Initialize resampler for temporary resampling of x_buf data to API_fs_Hz */\r
165                 ret += silk_resampler_init( &temp_resampler_state, SKP_SMULBB( psEnc->sCmn.fs_kHz, 1000 ), psEnc->sCmn.API_fs_Hz );\r
166 \r
167                 /* Temporary resampling of x_buf data to API_fs_Hz */\r
168                 ret += silk_resampler( &temp_resampler_state, x_buf_API_fs_Hz, x_bufFIX, nSamples_temp );\r
169 \r
170                 /* Calculate number of samples that has been temporarily upsampled */\r
171                 nSamples_temp = SKP_DIV32_16( nSamples_temp * psEnc->sCmn.API_fs_Hz, SKP_SMULBB( psEnc->sCmn.fs_kHz, 1000 ) );\r
172 \r
173                 /* Initialize the resampler for enc_API.c preparing resampling from API_fs_Hz to fs_kHz */\r
174                 ret += silk_resampler_init( &psEnc->sCmn.resampler_state, psEnc->sCmn.API_fs_Hz, SKP_SMULBB( fs_kHz, 1000 ) );\r
175 \r
176             } else {\r
177                 /* Copy data */\r
178                 SKP_memcpy( x_buf_API_fs_Hz, x_bufFIX, nSamples_temp * sizeof( SKP_int16 ) );\r
179             }\r
180 \r
181             if( 1000 * fs_kHz != psEnc->sCmn.API_fs_Hz ) {\r
182                 /* Correct resampler state (unless resampling by a factor 1) by resampling buffered data from API_fs_Hz to fs_kHz */\r
183                 ret += silk_resampler( &psEnc->sCmn.resampler_state, x_bufFIX, x_buf_API_fs_Hz, nSamples_temp );\r
184             }\r
185 #ifndef FIXED_POINT\r
186             SKP_short2float_array( psEnc->x_buf, x_bufFIX, ( 2 * MAX_FRAME_LENGTH_MS + LA_SHAPE_MS ) * fs_kHz );\r
187 #endif\r
188         }\r
189     }\r
190 \r
191     psEnc->sCmn.prev_API_fs_Hz = psEnc->sCmn.API_fs_Hz;\r
192 \r
193     return ret;\r
194 }\r
195 \r
196 SKP_int silk_setup_fs(\r
197     silk_encoder_state_Fxx          *psEnc,             /* I/O                      */\r
198     SKP_int                         fs_kHz,             /* I                        */\r
199     SKP_int                         PacketSize_ms       /* I                        */\r
200 )\r
201 {\r
202     SKP_int ret = SILK_NO_ERROR;\r
203 \r
204     /* Set packet size */\r
205     if( PacketSize_ms != psEnc->sCmn.PacketSize_ms ) {\r
206         if( ( PacketSize_ms !=  10 ) &&\r
207             ( PacketSize_ms !=  20 ) &&\r
208             ( PacketSize_ms !=  40 ) && \r
209             ( PacketSize_ms !=  60 ) ) {\r
210             ret = SILK_ENC_PACKET_SIZE_NOT_SUPPORTED;\r
211         }\r
212         if( PacketSize_ms <= 10 ) {\r
213             psEnc->sCmn.nFramesPerPacket = 1;\r
214             psEnc->sCmn.nb_subfr = PacketSize_ms == 10 ? 2 : 1;\r
215             psEnc->sCmn.frame_length = SKP_SMULBB( PacketSize_ms, fs_kHz );\r
216             psEnc->sCmn.pitch_LPC_win_length = SKP_SMULBB( FIND_PITCH_LPC_WIN_MS_2_SF, fs_kHz );\r
217             if( psEnc->sCmn.fs_kHz == 8 ) {\r
218                 psEnc->sCmn.pitch_contour_iCDF = silk_pitch_contour_10_ms_NB_iCDF;\r
219             } else {\r
220                 psEnc->sCmn.pitch_contour_iCDF = silk_pitch_contour_10_ms_iCDF;\r
221             }\r
222         } else {\r
223             psEnc->sCmn.nFramesPerPacket = SKP_DIV32_16( PacketSize_ms, MAX_FRAME_LENGTH_MS );\r
224             psEnc->sCmn.nb_subfr = MAX_NB_SUBFR;\r
225             psEnc->sCmn.frame_length = SKP_SMULBB( 20, fs_kHz );\r
226             psEnc->sCmn.pitch_LPC_win_length = SKP_SMULBB( FIND_PITCH_LPC_WIN_MS, fs_kHz );\r
227             if( psEnc->sCmn.fs_kHz == 8 ) {\r
228                 psEnc->sCmn.pitch_contour_iCDF = silk_pitch_contour_NB_iCDF;\r
229             } else {\r
230                 psEnc->sCmn.pitch_contour_iCDF = silk_pitch_contour_iCDF; \r
231             }\r
232         }\r
233         psEnc->sCmn.PacketSize_ms  = PacketSize_ms;\r
234         psEnc->sCmn.TargetRate_bps = 0;         /* trigger new SNR computation */\r
235     }\r
236 \r
237     /* Set internal sampling frequency */\r
238     SKP_assert( fs_kHz == 8 || fs_kHz == 12 || fs_kHz == 16 );\r
239     SKP_assert( psEnc->sCmn.nb_subfr == 2 || psEnc->sCmn.nb_subfr == 4 );\r
240     if( psEnc->sCmn.fs_kHz != fs_kHz ) {\r
241         /* reset part of the state */\r
242 #ifdef FIXED_POINT\r
243         SKP_memset( &psEnc->sShape,               0, sizeof( silk_shape_state_FIX ) );\r
244         SKP_memset( &psEnc->sPrefilt,             0, sizeof( silk_prefilter_state_FIX ) );\r
245 #else\r
246         SKP_memset( &psEnc->sShape,               0, sizeof( silk_shape_state_FLP ) );\r
247         SKP_memset( &psEnc->sPrefilt,             0, sizeof( silk_prefilter_state_FLP ) );\r
248 #endif\r
249         SKP_memset( &psEnc->sCmn.sNSQ,            0, sizeof( silk_nsq_state ) );\r
250         SKP_memset( psEnc->sCmn.prev_NLSFq_Q15,   0, sizeof( psEnc->sCmn.prev_NLSFq_Q15 ) );\r
251         SKP_memset( &psEnc->sCmn.sLP.In_LP_State, 0, sizeof( psEnc->sCmn.sLP.In_LP_State ) );\r
252         psEnc->sCmn.inputBufIx                  = 0;\r
253         psEnc->sCmn.nFramesEncoded              = 0;\r
254         psEnc->sCmn.TargetRate_bps              = 0;     /* trigger new SNR computation */\r
255 \r
256         /* Initialize non-zero parameters */\r
257         psEnc->sCmn.prevLag                     = 100;\r
258         psEnc->sCmn.first_frame_after_reset     = 1;\r
259         psEnc->sPrefilt.lagPrev                 = 100;\r
260         psEnc->sShape.LastGainIndex             = 10;\r
261         psEnc->sCmn.sNSQ.lagPrev                = 100;\r
262         psEnc->sCmn.sNSQ.prev_inv_gain_Q16      = 65536;\r
263 \r
264         psEnc->sCmn.fs_kHz = fs_kHz;\r
265         if( psEnc->sCmn.fs_kHz == 8 ) {\r
266             if( psEnc->sCmn.nb_subfr == MAX_NB_SUBFR ) {\r
267                 psEnc->sCmn.pitch_contour_iCDF = silk_pitch_contour_NB_iCDF; \r
268             } else {\r
269                 psEnc->sCmn.pitch_contour_iCDF = silk_pitch_contour_10_ms_NB_iCDF;\r
270             }\r
271         } else {\r
272             if( psEnc->sCmn.nb_subfr == MAX_NB_SUBFR ) {\r
273                 psEnc->sCmn.pitch_contour_iCDF = silk_pitch_contour_iCDF; \r
274             } else {\r
275                 psEnc->sCmn.pitch_contour_iCDF = silk_pitch_contour_10_ms_iCDF;\r
276             }\r
277         }\r
278         if( psEnc->sCmn.fs_kHz == 8 || psEnc->sCmn.fs_kHz == 12 ) {\r
279             psEnc->sCmn.predictLPCOrder = MIN_LPC_ORDER;\r
280             psEnc->sCmn.psNLSF_CB  = &silk_NLSF_CB_NB_MB;\r
281         } else {\r
282             psEnc->sCmn.predictLPCOrder = MAX_LPC_ORDER;\r
283             psEnc->sCmn.psNLSF_CB  = &silk_NLSF_CB_WB;\r
284         }\r
285         psEnc->sCmn.subfr_length   = SUB_FRAME_LENGTH_MS * fs_kHz;\r
286         psEnc->sCmn.frame_length   = SKP_SMULBB( psEnc->sCmn.subfr_length, psEnc->sCmn.nb_subfr );\r
287         psEnc->sCmn.ltp_mem_length = SKP_SMULBB( LTP_MEM_LENGTH_MS, fs_kHz ); \r
288         psEnc->sCmn.la_pitch       = SKP_SMULBB( LA_PITCH_MS, fs_kHz );\r
289         psEnc->sCmn.max_pitch_lag  = SKP_SMULBB( 18, fs_kHz );\r
290         if( psEnc->sCmn.nb_subfr == MAX_NB_SUBFR ) {\r
291             psEnc->sCmn.pitch_LPC_win_length = SKP_SMULBB( FIND_PITCH_LPC_WIN_MS, fs_kHz );\r
292         } else {\r
293             psEnc->sCmn.pitch_LPC_win_length = SKP_SMULBB( FIND_PITCH_LPC_WIN_MS_2_SF, fs_kHz );\r
294         }\r
295         if( psEnc->sCmn.fs_kHz == 16 ) {\r
296             psEnc->sCmn.mu_LTP_Q9 = SILK_FIX_CONST( MU_LTP_QUANT_WB, 9 );\r
297             psEnc->sCmn.pitch_lag_low_bits_iCDF = silk_uniform8_iCDF;\r
298         } else if( psEnc->sCmn.fs_kHz == 12 ) {\r
299             psEnc->sCmn.mu_LTP_Q9 = SILK_FIX_CONST( MU_LTP_QUANT_MB, 9 );\r
300             psEnc->sCmn.pitch_lag_low_bits_iCDF = silk_uniform6_iCDF;\r
301         } else {\r
302             psEnc->sCmn.mu_LTP_Q9 = SILK_FIX_CONST( MU_LTP_QUANT_NB, 9 );\r
303             psEnc->sCmn.pitch_lag_low_bits_iCDF = silk_uniform4_iCDF;\r
304         }\r
305     }\r
306 \r
307     /* Check that settings are valid */\r
308     SKP_assert( ( psEnc->sCmn.subfr_length * psEnc->sCmn.nb_subfr ) == psEnc->sCmn.frame_length );\r
309  \r
310     return ret;\r
311 }\r
312 \r
313 SKP_int silk_setup_complexity(\r
314     silk_encoder_state              *psEncC,            /* I/O                      */\r
315     SKP_int                         Complexity          /* I                        */\r
316 )\r
317 {\r
318     SKP_int ret = 0;\r
319 \r
320     /* Set encoding complexity */\r
321     SKP_assert( Complexity >= 0 && Complexity <= 10 );\r
322     if( Complexity < 2 ) {\r
323         psEncC->pitchEstimationComplexity       = SILK_PE_MIN_COMPLEX;\r
324         psEncC->pitchEstimationThreshold_Q16    = SILK_FIX_CONST( 0.8, 16 );\r
325         psEncC->pitchEstimationLPCOrder         = 6;\r
326         psEncC->shapingLPCOrder                 = 8;\r
327         psEncC->la_shape                        = 3 * psEncC->fs_kHz;\r
328         psEncC->nStatesDelayedDecision          = 1;\r
329         psEncC->useInterpolatedNLSFs            = 0;\r
330         psEncC->LTPQuantLowComplexity           = 1;\r
331         psEncC->NLSF_MSVQ_Survivors             = 2;\r
332         psEncC->warping_Q16                     = 0;\r
333     } else if( Complexity < 4 ) {\r
334         psEncC->pitchEstimationComplexity       = SILK_PE_MID_COMPLEX;\r
335         psEncC->pitchEstimationThreshold_Q16    = SILK_FIX_CONST( 0.76, 16 );\r
336         psEncC->pitchEstimationLPCOrder         = 8;\r
337         psEncC->shapingLPCOrder                 = 10;\r
338         psEncC->la_shape                        = 5 * psEncC->fs_kHz;\r
339         psEncC->nStatesDelayedDecision          = 1;\r
340         psEncC->useInterpolatedNLSFs            = 1;\r
341         psEncC->LTPQuantLowComplexity           = 0;\r
342         psEncC->NLSF_MSVQ_Survivors             = 4;\r
343         psEncC->warping_Q16                     = 0;\r
344     } else if( Complexity < 6 ) {\r
345         psEncC->pitchEstimationComplexity       = SILK_PE_MID_COMPLEX;\r
346         psEncC->pitchEstimationThreshold_Q16    = SILK_FIX_CONST( 0.74, 16 );\r
347         psEncC->pitchEstimationLPCOrder         = 10;\r
348         psEncC->shapingLPCOrder                 = 12;\r
349         psEncC->la_shape                        = 5 * psEncC->fs_kHz;\r
350         psEncC->nStatesDelayedDecision          = 2;\r
351         psEncC->useInterpolatedNLSFs            = 0;\r
352         psEncC->LTPQuantLowComplexity           = 0;\r
353         psEncC->NLSF_MSVQ_Survivors             = 6;\r
354         psEncC->warping_Q16                     = psEncC->fs_kHz * SILK_FIX_CONST( WARPING_MULTIPLIER, 16 );\r
355     } else if( Complexity < 8 ) {\r
356         psEncC->pitchEstimationComplexity       = SILK_PE_MID_COMPLEX;\r
357         psEncC->pitchEstimationThreshold_Q16    = SILK_FIX_CONST( 0.72, 16 );\r
358         psEncC->pitchEstimationLPCOrder         = 12;\r
359         psEncC->shapingLPCOrder                 = 14;\r
360         psEncC->la_shape                        = 5 * psEncC->fs_kHz;\r
361         psEncC->nStatesDelayedDecision          = 3;\r
362         psEncC->useInterpolatedNLSFs            = 0;\r
363         psEncC->LTPQuantLowComplexity           = 0;\r
364         psEncC->NLSF_MSVQ_Survivors             = 8;\r
365         psEncC->warping_Q16                     = psEncC->fs_kHz * SILK_FIX_CONST( WARPING_MULTIPLIER, 16 );\r
366     } else {\r
367         psEncC->pitchEstimationComplexity       = SILK_PE_MAX_COMPLEX;\r
368         psEncC->pitchEstimationThreshold_Q16    = SILK_FIX_CONST( 0.7, 16 );\r
369         psEncC->pitchEstimationLPCOrder         = 16;\r
370         psEncC->shapingLPCOrder                 = 16;\r
371         psEncC->la_shape                        = 5 * psEncC->fs_kHz;\r
372         psEncC->nStatesDelayedDecision          = MAX_DEL_DEC_STATES;\r
373         psEncC->useInterpolatedNLSFs            = 1;\r
374         psEncC->LTPQuantLowComplexity           = 0;\r
375         psEncC->NLSF_MSVQ_Survivors             = 16;\r
376         psEncC->warping_Q16                     = psEncC->fs_kHz * SILK_FIX_CONST( WARPING_MULTIPLIER, 16 );\r
377     }\r
378 \r
379     /* Do not allow higher pitch estimation LPC order than predict LPC order */\r
380     psEncC->pitchEstimationLPCOrder = SKP_min_int( psEncC->pitchEstimationLPCOrder, psEncC->predictLPCOrder );\r
381     psEncC->shapeWinLength          = SUB_FRAME_LENGTH_MS * psEncC->fs_kHz + 2 * psEncC->la_shape;\r
382     psEncC->Complexity              = Complexity;\r
383 \r
384     SKP_assert( psEncC->pitchEstimationLPCOrder <= MAX_FIND_PITCH_LPC_ORDER );\r
385     SKP_assert( psEncC->shapingLPCOrder         <= MAX_SHAPE_LPC_ORDER      );\r
386     SKP_assert( psEncC->nStatesDelayedDecision  <= MAX_DEL_DEC_STATES       );\r
387     SKP_assert( psEncC->warping_Q16             <= 32767                    );\r
388     SKP_assert( psEncC->la_shape                <= LA_SHAPE_MAX             );\r
389     SKP_assert( psEncC->shapeWinLength          <= SHAPE_LPC_WIN_MAX        );\r
390     SKP_assert( psEncC->NLSF_MSVQ_Survivors     <= NLSF_VQ_MAX_SURVIVORS    );\r
391 \r
392     return ret;\r
393 }\r
394 \r
395 SKP_INLINE SKP_int silk_setup_LBRR(\r
396     silk_encoder_state          *psEncC,            /* I/O                      */\r
397     const SKP_int32                 TargetRate_bps      /* I                        */\r
398 )\r
399 {\r
400     SKP_int   ret = SILK_NO_ERROR;\r
401     SKP_int32 LBRR_rate_thres_bps;\r
402 \r
403     psEncC->LBRR_enabled = 0;\r
404     if( psEncC->useInBandFEC && psEncC->PacketLoss_perc > 0 ) {\r
405         if( psEncC->fs_kHz == 8 ) {\r
406             LBRR_rate_thres_bps = LBRR_NB_MIN_RATE_BPS;\r
407         } else if( psEncC->fs_kHz == 12 ) {\r
408             LBRR_rate_thres_bps = LBRR_MB_MIN_RATE_BPS;\r
409         } else {\r
410             LBRR_rate_thres_bps = LBRR_WB_MIN_RATE_BPS;\r
411         }\r
412         LBRR_rate_thres_bps = SKP_SMULWB( SKP_MUL( LBRR_rate_thres_bps, 125 - SKP_min( psEncC->PacketLoss_perc, 25 ) ), SILK_FIX_CONST( 0.01, 16 ) );\r
413 \r
414         if( TargetRate_bps > LBRR_rate_thres_bps ) {\r
415             /* Set gain increase for coding LBRR excitation */\r
416             psEncC->LBRR_enabled = 1;\r
417             psEncC->LBRR_GainIncreases = SKP_max_int( 7 - SKP_SMULWB( psEncC->PacketLoss_perc, SILK_FIX_CONST( 0.4, 16 ) ), 2 );\r
418         }\r
419     }\r
420 \r
421     return ret;\r
422 }\r