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