SILK stereo fix
[opus.git] / 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 "main_FIX.h"
33 #define silk_encoder_state_Fxx      silk_encoder_state_FIX
34 #else
35 #include "main_FLP.h"
36 #define silk_encoder_state_Fxx      silk_encoder_state_FLP
37 #endif
38 #include "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 static 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     const opus_int                   force_fs_kHz
70 )
71 {
72     opus_int   fs_kHz, ret = 0;
73
74     psEnc->sCmn.useDTX                 = encControl->useDTX;
75     psEnc->sCmn.useCBR                 = encControl->useCBR;
76     psEnc->sCmn.API_fs_Hz              = encControl->API_sampleRate;
77     psEnc->sCmn.maxInternal_fs_Hz      = encControl->maxInternalSampleRate;
78     psEnc->sCmn.minInternal_fs_Hz      = encControl->minInternalSampleRate;
79     psEnc->sCmn.desiredInternal_fs_Hz  = encControl->desiredInternalSampleRate;
80     psEnc->sCmn.useInBandFEC           = encControl->useInBandFEC;
81     psEnc->sCmn.nChannelsAPI           = encControl->nChannelsAPI;
82     psEnc->sCmn.nChannelsInternal      = encControl->nChannelsInternal;
83     psEnc->sCmn.allow_bandwidth_switch = allow_bw_switch;
84     psEnc->sCmn.channelNb              = channelNb;
85
86     if( psEnc->sCmn.controlled_since_last_payload != 0 && psEnc->sCmn.prefillFlag == 0 ) {
87         if( psEnc->sCmn.API_fs_Hz != psEnc->sCmn.prev_API_fs_Hz && psEnc->sCmn.fs_kHz > 0 ) {
88             /* Change in API sampling rate in the middle of encoding a packet */
89             ret += silk_setup_resamplers( psEnc, psEnc->sCmn.fs_kHz );
90         }
91         return ret;
92     }
93
94     /* Beyond this point we know that there are no previously coded frames in the payload buffer */
95
96     /********************************************/
97     /* Determine internal sampling rate         */
98     /********************************************/
99     fs_kHz = silk_control_audio_bandwidth( &psEnc->sCmn );
100     if (force_fs_kHz)
101        fs_kHz = force_fs_kHz;
102     /********************************************/
103     /* Prepare resampler and buffered data      */
104     /********************************************/
105     ret += silk_setup_resamplers( psEnc, fs_kHz );
106
107     /********************************************/
108     /* Set internal sampling frequency          */
109     /********************************************/
110     ret += silk_setup_fs( psEnc, fs_kHz, encControl->payloadSize_ms );
111
112     /********************************************/
113     /* Set encoding complexity                  */
114     /********************************************/
115     ret += silk_setup_complexity( &psEnc->sCmn, encControl->complexity  );
116
117     /********************************************/
118     /* Set packet loss rate measured by farend  */
119     /********************************************/
120     psEnc->sCmn.PacketLoss_perc = encControl->packetLossPercentage;
121
122     /********************************************/
123     /* Set LBRR usage                           */
124     /********************************************/
125     ret += silk_setup_LBRR( &psEnc->sCmn, TargetRate_bps );
126
127     psEnc->sCmn.controlled_since_last_payload = 1;
128
129     return ret;
130 }
131
132 opus_int silk_setup_resamplers(
133     silk_encoder_state_Fxx          *psEnc,             /* I/O                      */
134     opus_int                         fs_kHz              /* I                        */
135 )
136 {
137     opus_int   ret = SILK_NO_ERROR;
138     opus_int32 nSamples_temp;
139
140     if( psEnc->sCmn.fs_kHz != fs_kHz || psEnc->sCmn.prev_API_fs_Hz != psEnc->sCmn.API_fs_Hz )
141     {
142         if( psEnc->sCmn.fs_kHz == 0 ) {
143             /* Initialize the resampler for enc_API.c preparing resampling from API_fs_Hz to fs_kHz */
144             ret += silk_resampler_init( &psEnc->sCmn.resampler_state, psEnc->sCmn.API_fs_Hz, fs_kHz * 1000 );
145         } else {
146             /* Allocate worst case space for temporary upsampling, 8 to 48 kHz, so a factor 6 */
147             opus_int16 x_buf_API_fs_Hz[ ( 2 * MAX_FRAME_LENGTH_MS + LA_SHAPE_MS ) * MAX_API_FS_KHZ ];
148 #ifdef FIXED_POINT
149             opus_int16 *x_bufFIX = psEnc->x_buf;
150 #else
151             opus_int16 x_bufFIX[ 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ];
152 #endif
153
154             nSamples_temp = silk_LSHIFT( psEnc->sCmn.frame_length, 1 ) + LA_SHAPE_MS * psEnc->sCmn.fs_kHz;
155
156 #ifndef FIXED_POINT
157             silk_float2short_array( x_bufFIX, psEnc->x_buf, nSamples_temp );
158 #endif
159
160             if( silk_SMULBB( fs_kHz, 1000 ) < psEnc->sCmn.API_fs_Hz && psEnc->sCmn.fs_kHz != 0 ) {
161                 /* Resample buffered data in x_buf to API_fs_Hz */
162
163                 silk_resampler_state_struct  temp_resampler_state;
164
165                 /* Initialize resampler for temporary resampling of x_buf data to API_fs_Hz */
166                 ret += silk_resampler_init( &temp_resampler_state, silk_SMULBB( psEnc->sCmn.fs_kHz, 1000 ), psEnc->sCmn.API_fs_Hz );
167
168                 /* Temporary resampling of x_buf data to API_fs_Hz */
169                 ret += silk_resampler( &temp_resampler_state, x_buf_API_fs_Hz, x_bufFIX, nSamples_temp );
170
171                 /* Calculate number of samples that has been temporarily upsampled */
172                 nSamples_temp = silk_DIV32_16( nSamples_temp * psEnc->sCmn.API_fs_Hz, silk_SMULBB( psEnc->sCmn.fs_kHz, 1000 ) );
173
174                 /* Initialize the resampler for enc_API.c preparing resampling from API_fs_Hz to fs_kHz */
175                 ret += silk_resampler_init( &psEnc->sCmn.resampler_state, psEnc->sCmn.API_fs_Hz, silk_SMULBB( fs_kHz, 1000 ) );
176
177             } else {
178                 /* Copy data */
179                 silk_memcpy( x_buf_API_fs_Hz, x_bufFIX, nSamples_temp * sizeof( opus_int16 ) );
180             }
181
182             if( 1000 * fs_kHz != psEnc->sCmn.API_fs_Hz ) {
183                 /* Correct resampler state (unless resampling by a factor 1) by resampling buffered data from API_fs_Hz to fs_kHz */
184                 ret += silk_resampler( &psEnc->sCmn.resampler_state, x_bufFIX, x_buf_API_fs_Hz, nSamples_temp );
185             }
186 #ifndef FIXED_POINT
187             silk_short2float_array( psEnc->x_buf, x_bufFIX, ( 2 * MAX_FRAME_LENGTH_MS + LA_SHAPE_MS ) * fs_kHz );
188 #endif
189         }
190     }
191
192     psEnc->sCmn.prev_API_fs_Hz = psEnc->sCmn.API_fs_Hz;
193
194     return ret;
195 }
196
197 opus_int silk_setup_fs(
198     silk_encoder_state_Fxx          *psEnc,             /* I/O                      */
199     opus_int                         fs_kHz,             /* I                        */
200     opus_int                         PacketSize_ms       /* I                        */
201 )
202 {
203     opus_int ret = SILK_NO_ERROR;
204
205     /* Set packet size */
206     if( PacketSize_ms != psEnc->sCmn.PacketSize_ms ) {
207         if( ( PacketSize_ms !=  10 ) &&
208             ( PacketSize_ms !=  20 ) &&
209             ( PacketSize_ms !=  40 ) &&
210             ( PacketSize_ms !=  60 ) ) {
211             ret = SILK_ENC_PACKET_SIZE_NOT_SUPPORTED;
212         }
213         if( PacketSize_ms <= 10 ) {
214             psEnc->sCmn.nFramesPerPacket = 1;
215             psEnc->sCmn.nb_subfr = PacketSize_ms == 10 ? 2 : 1;
216             psEnc->sCmn.frame_length = silk_SMULBB( PacketSize_ms, fs_kHz );
217             psEnc->sCmn.pitch_LPC_win_length = silk_SMULBB( FIND_PITCH_LPC_WIN_MS_2_SF, fs_kHz );
218             if( psEnc->sCmn.fs_kHz == 8 ) {
219                 psEnc->sCmn.pitch_contour_iCDF = silk_pitch_contour_10_ms_NB_iCDF;
220             } else {
221                 psEnc->sCmn.pitch_contour_iCDF = silk_pitch_contour_10_ms_iCDF;
222             }
223         } else {
224             psEnc->sCmn.nFramesPerPacket = silk_DIV32_16( PacketSize_ms, MAX_FRAME_LENGTH_MS );
225             psEnc->sCmn.nb_subfr = MAX_NB_SUBFR;
226             psEnc->sCmn.frame_length = silk_SMULBB( 20, fs_kHz );
227             psEnc->sCmn.pitch_LPC_win_length = silk_SMULBB( FIND_PITCH_LPC_WIN_MS, fs_kHz );
228             if( psEnc->sCmn.fs_kHz == 8 ) {
229                 psEnc->sCmn.pitch_contour_iCDF = silk_pitch_contour_NB_iCDF;
230             } else {
231                 psEnc->sCmn.pitch_contour_iCDF = silk_pitch_contour_iCDF;
232             }
233         }
234         psEnc->sCmn.PacketSize_ms  = PacketSize_ms;
235         psEnc->sCmn.TargetRate_bps = 0;         /* trigger new SNR computation */
236     }
237
238     /* Set internal sampling frequency */
239     silk_assert( fs_kHz == 8 || fs_kHz == 12 || fs_kHz == 16 );
240     silk_assert( psEnc->sCmn.nb_subfr == 2 || psEnc->sCmn.nb_subfr == 4 );
241     if( psEnc->sCmn.fs_kHz != fs_kHz ) {
242         /* reset part of the state */
243 #ifdef FIXED_POINT
244         silk_memset( &psEnc->sShape,               0, sizeof( silk_shape_state_FIX ) );
245         silk_memset( &psEnc->sPrefilt,             0, sizeof( silk_prefilter_state_FIX ) );
246 #else
247         silk_memset( &psEnc->sShape,               0, sizeof( silk_shape_state_FLP ) );
248         silk_memset( &psEnc->sPrefilt,             0, sizeof( silk_prefilter_state_FLP ) );
249 #endif
250         silk_memset( &psEnc->sCmn.sNSQ,            0, sizeof( silk_nsq_state ) );
251         silk_memset( psEnc->sCmn.prev_NLSFq_Q15,   0, sizeof( psEnc->sCmn.prev_NLSFq_Q15 ) );
252         silk_memset( &psEnc->sCmn.sLP.In_LP_State, 0, sizeof( psEnc->sCmn.sLP.In_LP_State ) );
253         psEnc->sCmn.inputBufIx                  = 0;
254         psEnc->sCmn.nFramesEncoded              = 0;
255         psEnc->sCmn.TargetRate_bps              = 0;     /* trigger new SNR computation */
256
257         /* Initialize non-zero parameters */
258         psEnc->sCmn.prevLag                     = 100;
259         psEnc->sCmn.first_frame_after_reset     = 1;
260         psEnc->sPrefilt.lagPrev                 = 100;
261         psEnc->sShape.LastGainIndex             = 10;
262         psEnc->sCmn.sNSQ.lagPrev                = 100;
263         psEnc->sCmn.sNSQ.prev_inv_gain_Q16      = 65536;
264
265         psEnc->sCmn.fs_kHz = fs_kHz;
266         if( psEnc->sCmn.fs_kHz == 8 ) {
267             if( psEnc->sCmn.nb_subfr == MAX_NB_SUBFR ) {
268                 psEnc->sCmn.pitch_contour_iCDF = silk_pitch_contour_NB_iCDF;
269             } else {
270                 psEnc->sCmn.pitch_contour_iCDF = silk_pitch_contour_10_ms_NB_iCDF;
271             }
272         } else {
273             if( psEnc->sCmn.nb_subfr == MAX_NB_SUBFR ) {
274                 psEnc->sCmn.pitch_contour_iCDF = silk_pitch_contour_iCDF;
275             } else {
276                 psEnc->sCmn.pitch_contour_iCDF = silk_pitch_contour_10_ms_iCDF;
277             }
278         }
279         if( psEnc->sCmn.fs_kHz == 8 || psEnc->sCmn.fs_kHz == 12 ) {
280             psEnc->sCmn.predictLPCOrder = MIN_LPC_ORDER;
281             psEnc->sCmn.psNLSF_CB  = &silk_NLSF_CB_NB_MB;
282         } else {
283             psEnc->sCmn.predictLPCOrder = MAX_LPC_ORDER;
284             psEnc->sCmn.psNLSF_CB  = &silk_NLSF_CB_WB;
285         }
286         psEnc->sCmn.subfr_length   = SUB_FRAME_LENGTH_MS * fs_kHz;
287         psEnc->sCmn.frame_length   = silk_SMULBB( psEnc->sCmn.subfr_length, psEnc->sCmn.nb_subfr );
288         psEnc->sCmn.ltp_mem_length = silk_SMULBB( LTP_MEM_LENGTH_MS, fs_kHz );
289         psEnc->sCmn.la_pitch       = silk_SMULBB( LA_PITCH_MS, fs_kHz );
290         psEnc->sCmn.max_pitch_lag  = silk_SMULBB( 18, fs_kHz );
291         if( psEnc->sCmn.nb_subfr == MAX_NB_SUBFR ) {
292             psEnc->sCmn.pitch_LPC_win_length = silk_SMULBB( FIND_PITCH_LPC_WIN_MS, fs_kHz );
293         } else {
294             psEnc->sCmn.pitch_LPC_win_length = silk_SMULBB( FIND_PITCH_LPC_WIN_MS_2_SF, fs_kHz );
295         }
296         if( psEnc->sCmn.fs_kHz == 16 ) {
297             psEnc->sCmn.mu_LTP_Q9 = SILK_FIX_CONST( MU_LTP_QUANT_WB, 9 );
298             psEnc->sCmn.pitch_lag_low_bits_iCDF = silk_uniform8_iCDF;
299         } else if( psEnc->sCmn.fs_kHz == 12 ) {
300             psEnc->sCmn.mu_LTP_Q9 = SILK_FIX_CONST( MU_LTP_QUANT_MB, 9 );
301             psEnc->sCmn.pitch_lag_low_bits_iCDF = silk_uniform6_iCDF;
302         } else {
303             psEnc->sCmn.mu_LTP_Q9 = SILK_FIX_CONST( MU_LTP_QUANT_NB, 9 );
304             psEnc->sCmn.pitch_lag_low_bits_iCDF = silk_uniform4_iCDF;
305         }
306     }
307
308     /* Check that settings are valid */
309     silk_assert( ( psEnc->sCmn.subfr_length * psEnc->sCmn.nb_subfr ) == psEnc->sCmn.frame_length );
310
311     return ret;
312 }
313
314 opus_int silk_setup_complexity(
315     silk_encoder_state              *psEncC,            /* I/O                      */
316     opus_int                         Complexity          /* I                        */
317 )
318 {
319     opus_int ret = 0;
320
321     /* Set encoding complexity */
322     silk_assert( Complexity >= 0 && Complexity <= 10 );
323     if( Complexity < 2 ) {
324         psEncC->pitchEstimationComplexity       = SILK_PE_MIN_COMPLEX;
325         psEncC->pitchEstimationThreshold_Q16    = SILK_FIX_CONST( 0.8, 16 );
326         psEncC->pitchEstimationLPCOrder         = 6;
327         psEncC->shapingLPCOrder                 = 8;
328         psEncC->la_shape                        = 3 * psEncC->fs_kHz;
329         psEncC->nStatesDelayedDecision          = 1;
330         psEncC->useInterpolatedNLSFs            = 0;
331         psEncC->LTPQuantLowComplexity           = 1;
332         psEncC->NLSF_MSVQ_Survivors             = 2;
333         psEncC->warping_Q16                     = 0;
334     } else if( Complexity < 4 ) {
335         psEncC->pitchEstimationComplexity       = SILK_PE_MID_COMPLEX;
336         psEncC->pitchEstimationThreshold_Q16    = SILK_FIX_CONST( 0.76, 16 );
337         psEncC->pitchEstimationLPCOrder         = 8;
338         psEncC->shapingLPCOrder                 = 10;
339         psEncC->la_shape                        = 5 * psEncC->fs_kHz;
340         psEncC->nStatesDelayedDecision          = 1;
341         psEncC->useInterpolatedNLSFs            = 1;
342         psEncC->LTPQuantLowComplexity           = 0;
343         psEncC->NLSF_MSVQ_Survivors             = 4;
344         psEncC->warping_Q16                     = 0;
345     } else if( Complexity < 6 ) {
346         psEncC->pitchEstimationComplexity       = SILK_PE_MID_COMPLEX;
347         psEncC->pitchEstimationThreshold_Q16    = SILK_FIX_CONST( 0.74, 16 );
348         psEncC->pitchEstimationLPCOrder         = 10;
349         psEncC->shapingLPCOrder                 = 12;
350         psEncC->la_shape                        = 5 * psEncC->fs_kHz;
351         psEncC->nStatesDelayedDecision          = 2;
352         psEncC->useInterpolatedNLSFs            = 0;
353         psEncC->LTPQuantLowComplexity           = 0;
354         psEncC->NLSF_MSVQ_Survivors             = 8;
355         psEncC->warping_Q16                     = psEncC->fs_kHz * SILK_FIX_CONST( WARPING_MULTIPLIER, 16 );
356     } else if( Complexity < 8 ) {
357         psEncC->pitchEstimationComplexity       = SILK_PE_MID_COMPLEX;
358         psEncC->pitchEstimationThreshold_Q16    = SILK_FIX_CONST( 0.72, 16 );
359         psEncC->pitchEstimationLPCOrder         = 12;
360         psEncC->shapingLPCOrder                 = 14;
361         psEncC->la_shape                        = 5 * psEncC->fs_kHz;
362         psEncC->nStatesDelayedDecision          = 3;
363         psEncC->useInterpolatedNLSFs            = 0;
364         psEncC->LTPQuantLowComplexity           = 0;
365         psEncC->NLSF_MSVQ_Survivors             = 16;
366         psEncC->warping_Q16                     = psEncC->fs_kHz * SILK_FIX_CONST( WARPING_MULTIPLIER, 16 );
367     } else {
368         psEncC->pitchEstimationComplexity       = SILK_PE_MAX_COMPLEX;
369         psEncC->pitchEstimationThreshold_Q16    = SILK_FIX_CONST( 0.7, 16 );
370         psEncC->pitchEstimationLPCOrder         = 16;
371         psEncC->shapingLPCOrder                 = 16;
372         psEncC->la_shape                        = 5 * psEncC->fs_kHz;
373         psEncC->nStatesDelayedDecision          = MAX_DEL_DEC_STATES;
374         psEncC->useInterpolatedNLSFs            = 1;
375         psEncC->LTPQuantLowComplexity           = 0;
376         psEncC->NLSF_MSVQ_Survivors             = 32;
377         psEncC->warping_Q16                     = psEncC->fs_kHz * SILK_FIX_CONST( WARPING_MULTIPLIER, 16 );
378     }
379
380     /* Do not allow higher pitch estimation LPC order than predict LPC order */
381     psEncC->pitchEstimationLPCOrder = silk_min_int( psEncC->pitchEstimationLPCOrder, psEncC->predictLPCOrder );
382     psEncC->shapeWinLength          = SUB_FRAME_LENGTH_MS * psEncC->fs_kHz + 2 * psEncC->la_shape;
383     psEncC->Complexity              = Complexity;
384
385     silk_assert( psEncC->pitchEstimationLPCOrder <= MAX_FIND_PITCH_LPC_ORDER );
386     silk_assert( psEncC->shapingLPCOrder         <= MAX_SHAPE_LPC_ORDER      );
387     silk_assert( psEncC->nStatesDelayedDecision  <= MAX_DEL_DEC_STATES       );
388     silk_assert( psEncC->warping_Q16             <= 32767                    );
389     silk_assert( psEncC->la_shape                <= LA_SHAPE_MAX             );
390     silk_assert( psEncC->shapeWinLength          <= SHAPE_LPC_WIN_MAX        );
391     silk_assert( psEncC->NLSF_MSVQ_Survivors     <= NLSF_VQ_MAX_SURVIVORS    );
392
393     return ret;
394 }
395
396 static inline opus_int silk_setup_LBRR(
397     silk_encoder_state          *psEncC,            /* I/O                      */
398     const opus_int32                 TargetRate_bps      /* I                        */
399 )
400 {
401     opus_int   ret = SILK_NO_ERROR;
402     opus_int32 LBRR_rate_thres_bps;
403
404     psEncC->LBRR_enabled = 0;
405     if( psEncC->useInBandFEC && psEncC->PacketLoss_perc > 0 ) {
406         if( psEncC->fs_kHz == 8 ) {
407             LBRR_rate_thres_bps = LBRR_NB_MIN_RATE_BPS;
408         } else if( psEncC->fs_kHz == 12 ) {
409             LBRR_rate_thres_bps = LBRR_MB_MIN_RATE_BPS;
410         } else {
411             LBRR_rate_thres_bps = LBRR_WB_MIN_RATE_BPS;
412         }
413         LBRR_rate_thres_bps = silk_SMULWB( silk_MUL( LBRR_rate_thres_bps, 125 - silk_min( psEncC->PacketLoss_perc, 25 ) ), SILK_FIX_CONST( 0.01, 16 ) );
414
415         if( TargetRate_bps > LBRR_rate_thres_bps ) {
416             /* Set gain increase for coding LBRR excitation */
417             psEncC->LBRR_enabled = 1;
418             psEncC->LBRR_GainIncreases = silk_max_int( 7 - silk_SMULWB( psEncC->PacketLoss_perc, SILK_FIX_CONST( 0.4, 16 ) ), 2 );
419         }
420     }
421
422     return ret;
423 }