Fixing DTX for fixed-point too, updating project files
[opus.git] / src_common / SKP_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 #include "SKP_Silk_tuning_parameters.h"\r
29 #if FIXED_POINT\r
30 #include "SKP_Silk_main_FIX.h"\r
31 #define SKP_Silk_encoder_state_Fxx      SKP_Silk_encoder_state_FIX\r
32 #else\r
33 #include "SKP_Silk_main_FLP.h"\r
34 #define SKP_Silk_encoder_state_Fxx      SKP_Silk_encoder_state_FLP\r
35 #endif\r
36 \r
37 SKP_int SKP_Silk_setup_resamplers(\r
38     SKP_Silk_encoder_state_Fxx      *psEnc,             /* I/O                      */\r
39     SKP_int                         fs_kHz              /* I                        */\r
40 );\r
41 \r
42 SKP_int SKP_Silk_setup_fs(\r
43     SKP_Silk_encoder_state_Fxx      *psEnc,             /* I/O                      */\r
44     SKP_int                         fs_kHz,             /* I                        */\r
45     SKP_int                         PacketSize_ms       /* I                        */\r
46 );\r
47 \r
48 SKP_int SKP_Silk_setup_complexity(\r
49     SKP_Silk_encoder_state          *psEncC,            /* I/O                      */\r
50     SKP_int                         Complexity          /* I                        */\r
51 );\r
52 \r
53 static SKP_int SKP_Silk_setup_LBRR(\r
54     SKP_Silk_encoder_state          *psEncC,            /* I/O                      */\r
55     const SKP_int32                 TargetRate_bps      /* I                        */\r
56 );\r
57 \r
58 \r
59 /* Control encoder */\r
60 SKP_int SKP_Silk_control_encoder( \r
61     SKP_Silk_encoder_state_Fxx      *psEnc,             /* I/O  Pointer to Silk encoder state           */\r
62     SKP_SILK_SDK_EncControlStruct   *encControl,        /* I:   Control structure                       */\r
63     const SKP_int32                 TargetRate_bps      /* I    Target max bitrate (bps)                */\r
64 )\r
65 {\r
66     SKP_int   fs_kHz, ret = 0;\r
67 \r
68     psEnc->sCmn.useDTX            = encControl->useDTX;\r
69     psEnc->sCmn.useCBR            = encControl->useCBR;\r
70     psEnc->sCmn.API_fs_Hz         = encControl->API_sampleRate;\r
71     psEnc->sCmn.maxInternal_fs_Hz = encControl->maxInternalSampleRate;\r
72     psEnc->sCmn.minInternal_fs_Hz = encControl->minInternalSampleRate;\r
73     psEnc->sCmn.useInBandFEC      = encControl->useInBandFEC;\r
74 \r
75     if( psEnc->sCmn.controlled_since_last_payload != 0 && psEnc->sCmn.prefillFlag == 0 ) {\r
76         if( psEnc->sCmn.API_fs_Hz != psEnc->sCmn.prev_API_fs_Hz && psEnc->sCmn.fs_kHz > 0 ) {\r
77             /* Change in API sampling rate in the middle of encoding a packet */\r
78             ret += SKP_Silk_setup_resamplers( psEnc, psEnc->sCmn.fs_kHz );\r
79         }\r
80         return ret;\r
81     }\r
82 \r
83     /* Beyond this point we know that there are no previously coded frames in the payload buffer */\r
84 \r
85     /********************************************/\r
86     /* Determine internal sampling rate         */\r
87     /********************************************/\r
88     fs_kHz = SKP_Silk_control_audio_bandwidth( &psEnc->sCmn, TargetRate_bps );\r
89 \r
90     /********************************************/\r
91     /* Prepare resampler and buffered data      */\r
92     /********************************************/\r
93     ret += SKP_Silk_setup_resamplers( psEnc, fs_kHz );\r
94 \r
95     /********************************************/\r
96     /* Set internal sampling frequency          */\r
97     /********************************************/\r
98     ret += SKP_Silk_setup_fs( psEnc, fs_kHz, encControl->payloadSize_ms );\r
99 \r
100     /********************************************/\r
101     /* Set encoding complexity                  */\r
102     /********************************************/\r
103     ret += SKP_Silk_setup_complexity( &psEnc->sCmn, encControl->complexity  );\r
104 \r
105     /********************************************/\r
106     /* Set packet loss rate measured by farend  */\r
107     /********************************************/\r
108     psEnc->sCmn.PacketLoss_perc = encControl->packetLossPercentage;\r
109 \r
110     /********************************************/\r
111     /* Set LBRR usage                           */\r
112     /********************************************/\r
113     ret += SKP_Silk_setup_LBRR( &psEnc->sCmn, TargetRate_bps );\r
114 \r
115     psEnc->sCmn.controlled_since_last_payload = 1;\r
116 \r
117     return ret;\r
118 }\r
119 \r
120 SKP_int SKP_Silk_setup_resamplers(\r
121     SKP_Silk_encoder_state_Fxx      *psEnc,             /* I/O                      */\r
122     SKP_int                         fs_kHz              /* I                        */\r
123 )\r
124 {\r
125     SKP_int   ret = SKP_SILK_NO_ERROR;\r
126     SKP_int32 nSamples_temp;\r
127     \r
128     if( psEnc->sCmn.fs_kHz != fs_kHz || psEnc->sCmn.prev_API_fs_Hz != psEnc->sCmn.API_fs_Hz ) \r
129     {\r
130         if( psEnc->sCmn.fs_kHz == 0 ) {\r
131             /* Initialize the resampler for enc_API.c preparing resampling from API_fs_Hz to fs_kHz */\r
132             ret += SKP_Silk_resampler_init( &psEnc->sCmn.resampler_state, psEnc->sCmn.API_fs_Hz, fs_kHz * 1000 );\r
133         } else {\r
134             /* Allocate space for worst case temporary upsampling, 8 to 48 kHz, so a factor 6 */\r
135             SKP_int16 x_buf_API_fs_Hz[ ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) * ( MAX_API_FS_KHZ / 8 ) ];\r
136 #if FIXED_POINT\r
137             SKP_int16 *x_bufFIX = psEnc->x_buf;\r
138 #else\r
139             SKP_int16 x_bufFIX[ 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ]; \r
140 #endif\r
141 \r
142             nSamples_temp = SKP_LSHIFT( psEnc->sCmn.frame_length, 1 ) + LA_SHAPE_MS * psEnc->sCmn.fs_kHz;\r
143 \r
144 #if !FIXED_POINT\r
145             SKP_float2short_array( x_bufFIX, psEnc->x_buf, nSamples_temp );\r
146 #endif\r
147 \r
148             if( SKP_SMULBB( fs_kHz, 1000 ) < psEnc->sCmn.API_fs_Hz && psEnc->sCmn.fs_kHz != 0 ) {\r
149                 /* Resample buffered data in x_buf to API_fs_Hz */\r
150 \r
151                 SKP_Silk_resampler_state_struct  temp_resampler_state;\r
152 \r
153                 /* Initialize resampler for temporary resampling of x_buf data to API_fs_Hz */\r
154                 ret += SKP_Silk_resampler_init( &temp_resampler_state, SKP_SMULBB( psEnc->sCmn.fs_kHz, 1000 ), psEnc->sCmn.API_fs_Hz );\r
155 \r
156                 /* Temporary resampling of x_buf data to API_fs_Hz */\r
157                 ret += SKP_Silk_resampler( &temp_resampler_state, x_buf_API_fs_Hz, x_bufFIX, nSamples_temp );\r
158 \r
159                 /* Calculate number of samples that has been temporarily upsampled */\r
160                 nSamples_temp = SKP_DIV32_16( nSamples_temp * psEnc->sCmn.API_fs_Hz, SKP_SMULBB( psEnc->sCmn.fs_kHz, 1000 ) );\r
161 \r
162                 /* Initialize the resampler for enc_API.c preparing resampling from API_fs_Hz to fs_kHz */\r
163                 ret += SKP_Silk_resampler_init( &psEnc->sCmn.resampler_state, psEnc->sCmn.API_fs_Hz, SKP_SMULBB( fs_kHz, 1000 ) );\r
164 \r
165             } else {\r
166                 /* Copy data */\r
167                 SKP_memcpy( x_buf_API_fs_Hz, x_bufFIX, nSamples_temp * sizeof( SKP_int16 ) );\r
168             }\r
169 \r
170             if( 1000 * fs_kHz != psEnc->sCmn.API_fs_Hz ) {\r
171                 /* Correct resampler state (unless resampling by a factor 1) by resampling buffered data from API_fs_Hz to fs_kHz */\r
172                 ret += SKP_Silk_resampler( &psEnc->sCmn.resampler_state, x_bufFIX, x_buf_API_fs_Hz, nSamples_temp );\r
173             }\r
174 #if !FIXED_POINT\r
175             SKP_short2float_array( psEnc->x_buf, x_bufFIX, ( 2 * MAX_FRAME_LENGTH_MS + LA_SHAPE_MS ) * fs_kHz );\r
176 #endif\r
177         }\r
178     }\r
179 \r
180     psEnc->sCmn.prev_API_fs_Hz = psEnc->sCmn.API_fs_Hz;\r
181 \r
182     return ret;\r
183 }\r
184 \r
185 SKP_int SKP_Silk_setup_fs(\r
186     SKP_Silk_encoder_state_Fxx      *psEnc,             /* I/O                      */\r
187     SKP_int                         fs_kHz,             /* I                        */\r
188     SKP_int                         PacketSize_ms       /* I                        */\r
189 )\r
190 {\r
191     SKP_int ret = SKP_SILK_NO_ERROR;\r
192 \r
193     /* Set packet size */\r
194     if( PacketSize_ms != psEnc->sCmn.PacketSize_ms ) {\r
195         if( ( PacketSize_ms !=  10 ) &&\r
196             ( PacketSize_ms !=  20 ) &&\r
197             ( PacketSize_ms !=  40 ) && \r
198             ( PacketSize_ms !=  60 ) ) {\r
199             ret = SKP_SILK_ENC_PACKET_SIZE_NOT_SUPPORTED;\r
200         }\r
201         if( PacketSize_ms <= 10 ) {\r
202             psEnc->sCmn.nFramesPerPacket = 1;\r
203             psEnc->sCmn.nb_subfr = PacketSize_ms == 10 ? 2 : 1;\r
204             psEnc->sCmn.frame_length = SKP_SMULBB( PacketSize_ms, fs_kHz );\r
205             psEnc->sCmn.pitch_LPC_win_length = SKP_SMULBB( FIND_PITCH_LPC_WIN_MS_2_SF, fs_kHz );\r
206             if( psEnc->sCmn.fs_kHz == 8 ) {\r
207                 psEnc->sCmn.pitch_contour_iCDF = SKP_Silk_pitch_contour_10_ms_NB_iCDF;\r
208             } else {\r
209                 psEnc->sCmn.pitch_contour_iCDF = SKP_Silk_pitch_contour_10_ms_iCDF;\r
210             }\r
211         } else {\r
212             psEnc->sCmn.nFramesPerPacket = SKP_DIV32_16( PacketSize_ms, MAX_FRAME_LENGTH_MS );\r
213             psEnc->sCmn.nb_subfr = MAX_NB_SUBFR;\r
214             psEnc->sCmn.frame_length = SKP_SMULBB( 20, fs_kHz );\r
215             psEnc->sCmn.pitch_LPC_win_length = SKP_SMULBB( FIND_PITCH_LPC_WIN_MS, fs_kHz );\r
216             if( psEnc->sCmn.fs_kHz == 8 ) {\r
217                 psEnc->sCmn.pitch_contour_iCDF = SKP_Silk_pitch_contour_NB_iCDF;\r
218             } else {\r
219                 psEnc->sCmn.pitch_contour_iCDF = SKP_Silk_pitch_contour_iCDF; \r
220             }\r
221         }\r
222         psEnc->sCmn.PacketSize_ms  = PacketSize_ms;\r
223         psEnc->sCmn.TargetRate_bps = 0;         /* trigger new SNR computation */\r
224     }\r
225 \r
226     /* Set internal sampling frequency */\r
227     SKP_assert( fs_kHz == 8 || fs_kHz == 12 || fs_kHz == 16 );\r
228     SKP_assert( psEnc->sCmn.nb_subfr == 2 || psEnc->sCmn.nb_subfr == 4 );\r
229     if( psEnc->sCmn.fs_kHz != fs_kHz ) {\r
230         /* reset part of the state */\r
231 #if FIXED_POINT\r
232         SKP_memset( &psEnc->sShape,               0, sizeof( SKP_Silk_shape_state_FIX ) );\r
233         SKP_memset( &psEnc->sPrefilt,             0, sizeof( SKP_Silk_prefilter_state_FIX ) );\r
234 #else\r
235         SKP_memset( &psEnc->sShape,               0, sizeof( SKP_Silk_shape_state_FLP ) );\r
236         SKP_memset( &psEnc->sPrefilt,             0, sizeof( SKP_Silk_prefilter_state_FLP ) );\r
237 #endif\r
238         SKP_memset( &psEnc->sCmn.sNSQ,            0, sizeof( SKP_Silk_nsq_state ) );\r
239         SKP_memset( psEnc->sCmn.prev_NLSFq_Q15,   0, sizeof( psEnc->sCmn.prev_NLSFq_Q15 ) );\r
240         SKP_memset( &psEnc->sCmn.sLP.In_LP_State, 0, sizeof( psEnc->sCmn.sLP.In_LP_State ) );\r
241         psEnc->sCmn.inputBufIx                  = 0;\r
242         psEnc->sCmn.nFramesAnalyzed             = 0;\r
243         psEnc->sCmn.TargetRate_bps              = 0;     /* trigger new SNR computation */\r
244 \r
245         /* Initialize non-zero parameters */\r
246         psEnc->sCmn.prevLag                     = 100;\r
247         psEnc->sCmn.first_frame_after_reset     = 1;\r
248         psEnc->sPrefilt.lagPrev                 = 100;\r
249         psEnc->sShape.LastGainIndex             = 10;\r
250         psEnc->sCmn.sNSQ.lagPrev                = 100;\r
251         psEnc->sCmn.sNSQ.prev_inv_gain_Q16      = 65536;\r
252 \r
253         psEnc->sCmn.fs_kHz = fs_kHz;\r
254         if( psEnc->sCmn.fs_kHz == 8 ) {\r
255             if( psEnc->sCmn.nb_subfr == MAX_NB_SUBFR ) {\r
256                 psEnc->sCmn.pitch_contour_iCDF = SKP_Silk_pitch_contour_NB_iCDF; \r
257             } else {\r
258                 psEnc->sCmn.pitch_contour_iCDF = SKP_Silk_pitch_contour_10_ms_NB_iCDF;\r
259             }\r
260         } else {\r
261             if( psEnc->sCmn.nb_subfr == MAX_NB_SUBFR ) {\r
262                 psEnc->sCmn.pitch_contour_iCDF = SKP_Silk_pitch_contour_iCDF; \r
263             } else {\r
264                 psEnc->sCmn.pitch_contour_iCDF = SKP_Silk_pitch_contour_10_ms_iCDF;\r
265             }\r
266         }\r
267         if( psEnc->sCmn.fs_kHz == 8 || psEnc->sCmn.fs_kHz == 12 ) {\r
268             psEnc->sCmn.predictLPCOrder = MIN_LPC_ORDER;\r
269             psEnc->sCmn.psNLSF_CB  = &SKP_Silk_NLSF_CB_NB_MB;\r
270         } else {\r
271             psEnc->sCmn.predictLPCOrder = MAX_LPC_ORDER;\r
272             psEnc->sCmn.psNLSF_CB  = &SKP_Silk_NLSF_CB_WB;\r
273         }\r
274         psEnc->sCmn.subfr_length   = SUB_FRAME_LENGTH_MS * fs_kHz;\r
275         psEnc->sCmn.frame_length   = SKP_SMULBB( psEnc->sCmn.subfr_length, psEnc->sCmn.nb_subfr );\r
276         psEnc->sCmn.ltp_mem_length = SKP_SMULBB( LTP_MEM_LENGTH_MS, fs_kHz ); \r
277         psEnc->sCmn.la_pitch       = SKP_SMULBB( LA_PITCH_MS, fs_kHz );\r
278         psEnc->sCmn.max_pitch_lag  = SKP_SMULBB( 18, fs_kHz );\r
279         if( psEnc->sCmn.nb_subfr == MAX_NB_SUBFR ) {\r
280             psEnc->sCmn.pitch_LPC_win_length = SKP_SMULBB( FIND_PITCH_LPC_WIN_MS, fs_kHz );\r
281         } else {\r
282             psEnc->sCmn.pitch_LPC_win_length = SKP_SMULBB( FIND_PITCH_LPC_WIN_MS_2_SF, fs_kHz );\r
283         }\r
284         if( psEnc->sCmn.fs_kHz == 16 ) {\r
285             psEnc->sCmn.mu_LTP_Q9 = SKP_FIX_CONST( MU_LTP_QUANT_WB, 9 );\r
286             psEnc->sCmn.pitch_lag_low_bits_iCDF = SKP_Silk_uniform8_iCDF;\r
287         } else if( psEnc->sCmn.fs_kHz == 12 ) {\r
288             psEnc->sCmn.mu_LTP_Q9 = SKP_FIX_CONST( MU_LTP_QUANT_MB, 9 );\r
289             psEnc->sCmn.pitch_lag_low_bits_iCDF = SKP_Silk_uniform6_iCDF;\r
290         } else {\r
291             psEnc->sCmn.mu_LTP_Q9 = SKP_FIX_CONST( MU_LTP_QUANT_NB, 9 );\r
292             psEnc->sCmn.pitch_lag_low_bits_iCDF = SKP_Silk_uniform4_iCDF;\r
293         }\r
294     }\r
295 \r
296     /* Check that settings are valid */\r
297     SKP_assert( ( psEnc->sCmn.subfr_length * psEnc->sCmn.nb_subfr ) == psEnc->sCmn.frame_length );\r
298  \r
299     return ret;\r
300 }\r
301 \r
302 SKP_int SKP_Silk_setup_complexity(\r
303     SKP_Silk_encoder_state          *psEncC,            /* I/O                      */\r
304     SKP_int                         Complexity          /* I                        */\r
305 )\r
306 {\r
307     SKP_int ret = 0;\r
308 \r
309     /* Set encoding complexity */\r
310     SKP_assert( Complexity >= 0 && Complexity <= 10 );\r
311     if( Complexity < 2 ) {\r
312         psEncC->pitchEstimationComplexity       = SKP_Silk_PE_MIN_COMPLEX;\r
313         psEncC->pitchEstimationThreshold_Q16    = SKP_FIX_CONST( 0.8, 16 );\r
314         psEncC->pitchEstimationLPCOrder         = 6;\r
315         psEncC->shapingLPCOrder                 = 8;\r
316         psEncC->la_shape                        = 3 * psEncC->fs_kHz;\r
317         psEncC->nStatesDelayedDecision          = 1;\r
318         psEncC->useInterpolatedNLSFs            = 0;\r
319         psEncC->LTPQuantLowComplexity           = 1;\r
320         psEncC->NLSF_MSVQ_Survivors             = 2;\r
321         psEncC->warping_Q16                     = 0;\r
322     } else if( Complexity < 4 ) {\r
323         psEncC->pitchEstimationComplexity       = SKP_Silk_PE_MID_COMPLEX;\r
324         psEncC->pitchEstimationThreshold_Q16    = SKP_FIX_CONST( 0.76, 16 );\r
325         psEncC->pitchEstimationLPCOrder         = 8;\r
326         psEncC->shapingLPCOrder                 = 10;\r
327         psEncC->la_shape                        = 5 * psEncC->fs_kHz;\r
328         psEncC->nStatesDelayedDecision          = 1;\r
329         psEncC->useInterpolatedNLSFs            = 1;\r
330         psEncC->LTPQuantLowComplexity           = 0;\r
331         psEncC->NLSF_MSVQ_Survivors             = 4;\r
332         psEncC->warping_Q16                     = 0;\r
333     } else if( Complexity < 6 ) {\r
334         psEncC->pitchEstimationComplexity       = SKP_Silk_PE_MID_COMPLEX;\r
335         psEncC->pitchEstimationThreshold_Q16    = SKP_FIX_CONST( 0.74, 16 );\r
336         psEncC->pitchEstimationLPCOrder         = 10;\r
337         psEncC->shapingLPCOrder                 = 12;\r
338         psEncC->la_shape                        = 5 * psEncC->fs_kHz;\r
339         psEncC->nStatesDelayedDecision          = 2;\r
340         psEncC->useInterpolatedNLSFs            = 0;\r
341         psEncC->LTPQuantLowComplexity           = 0;\r
342         psEncC->NLSF_MSVQ_Survivors             = 6;\r
343         psEncC->warping_Q16                     = psEncC->fs_kHz * SKP_FIX_CONST( WARPING_MULTIPLIER, 16 );\r
344     } else if( Complexity < 8 ) {\r
345         psEncC->pitchEstimationComplexity       = SKP_Silk_PE_MID_COMPLEX;\r
346         psEncC->pitchEstimationThreshold_Q16    = SKP_FIX_CONST( 0.72, 16 );\r
347         psEncC->pitchEstimationLPCOrder         = 12;\r
348         psEncC->shapingLPCOrder                 = 14;\r
349         psEncC->la_shape                        = 5 * psEncC->fs_kHz;\r
350         psEncC->nStatesDelayedDecision          = 3;\r
351         psEncC->useInterpolatedNLSFs            = 0;\r
352         psEncC->LTPQuantLowComplexity           = 0;\r
353         psEncC->NLSF_MSVQ_Survivors             = 8;\r
354         psEncC->warping_Q16                     = psEncC->fs_kHz * SKP_FIX_CONST( WARPING_MULTIPLIER, 16 );\r
355     } else {\r
356         psEncC->pitchEstimationComplexity       = SKP_Silk_PE_MAX_COMPLEX;\r
357         psEncC->pitchEstimationThreshold_Q16    = SKP_FIX_CONST( 0.7, 16 );\r
358         psEncC->pitchEstimationLPCOrder         = 16;\r
359         psEncC->shapingLPCOrder                 = 16;\r
360         psEncC->la_shape                        = 5 * psEncC->fs_kHz;\r
361         psEncC->nStatesDelayedDecision          = MAX_DEL_DEC_STATES;\r
362         psEncC->useInterpolatedNLSFs            = 1;\r
363         psEncC->LTPQuantLowComplexity           = 0;\r
364         psEncC->NLSF_MSVQ_Survivors             = 16;\r
365         psEncC->warping_Q16                     = psEncC->fs_kHz * SKP_FIX_CONST( WARPING_MULTIPLIER, 16 );\r
366     }\r
367 \r
368     /* Do not allow higher pitch estimation LPC order than predict LPC order */\r
369     psEncC->pitchEstimationLPCOrder = SKP_min_int( psEncC->pitchEstimationLPCOrder, psEncC->predictLPCOrder );\r
370     psEncC->shapeWinLength          = SUB_FRAME_LENGTH_MS * psEncC->fs_kHz + 2 * psEncC->la_shape;\r
371 \r
372     SKP_assert( psEncC->pitchEstimationLPCOrder <= MAX_FIND_PITCH_LPC_ORDER );\r
373     SKP_assert( psEncC->shapingLPCOrder         <= MAX_SHAPE_LPC_ORDER      );\r
374     SKP_assert( psEncC->nStatesDelayedDecision  <= MAX_DEL_DEC_STATES       );\r
375     SKP_assert( psEncC->warping_Q16             <= 32767                    );\r
376     SKP_assert( psEncC->la_shape                <= LA_SHAPE_MAX             );\r
377     SKP_assert( psEncC->shapeWinLength          <= SHAPE_LPC_WIN_MAX        );\r
378     SKP_assert( psEncC->NLSF_MSVQ_Survivors     <= NLSF_VQ_MAX_SURVIVORS    );\r
379 \r
380     return ret;\r
381 }\r
382 \r
383 SKP_INLINE SKP_int SKP_Silk_setup_LBRR(\r
384     SKP_Silk_encoder_state          *psEncC,            /* I/O                      */\r
385     const SKP_int32                 TargetRate_bps      /* I                        */\r
386 )\r
387 {\r
388     SKP_int   ret = SKP_SILK_NO_ERROR;\r
389     SKP_int32 LBRRRate_thres_bps;\r
390 \r
391     SKP_assert( psEncC->fs_kHz == 8 || psEncC->fs_kHz == 12 || psEncC->fs_kHz == 16 );\r
392     if( psEncC->fs_kHz == 8 ) {\r
393         LBRRRate_thres_bps = LBRR_NB_MIN_RATE_BPS;\r
394     } else if( psEncC->fs_kHz == 12 ) {\r
395         LBRRRate_thres_bps = LBRR_MB_MIN_RATE_BPS;\r
396     } else {\r
397         LBRRRate_thres_bps = LBRR_WB_MIN_RATE_BPS;\r
398     }\r
399 \r
400     LBRRRate_thres_bps = SKP_RSHIFT( SKP_SMULBB( LBRRRate_thres_bps, 7 - psEncC->PacketLoss_perc ), 2 );\r
401     if( psEncC->useInBandFEC && TargetRate_bps >= LBRRRate_thres_bps && psEncC->PacketLoss_perc > 0 ) {\r
402         /* Set gain increase / rate reduction for LBRR usage */\r
403         psEncC->LBRR_GainIncreases = SKP_max_int( 7 - SKP_SMULWB( psEncC->PacketLoss_perc, SKP_FIX_CONST( 0.4, 16 ) ), 2 );\r
404         psEncC->LBRR_enabled = 1;\r
405     } else {\r
406         psEncC->LBRR_enabled = 0;\r
407     }\r
408 \r
409     return ret;\r
410 }\r