Use dynamic stack allocation in the SILK encoder.
[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, 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 #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 "stack_alloc.h"
39 #include "tuning_parameters.h"
40 #include "pitch_est_defines.h"
41
42 static opus_int silk_setup_resamplers(
43     silk_encoder_state_Fxx          *psEnc,             /* I/O                      */
44     opus_int                        fs_kHz              /* I                        */
45 );
46
47 static opus_int silk_setup_fs(
48     silk_encoder_state_Fxx          *psEnc,             /* I/O                      */
49     opus_int                        fs_kHz,             /* I                        */
50     opus_int                        PacketSize_ms       /* I                        */
51 );
52
53 static opus_int silk_setup_complexity(
54     silk_encoder_state              *psEncC,            /* I/O                      */
55     opus_int                        Complexity          /* I                        */
56 );
57
58 static inline opus_int silk_setup_LBRR(
59     silk_encoder_state              *psEncC,            /* I/O                      */
60     const opus_int32                TargetRate_bps      /* I                        */
61 );
62
63
64 /* Control encoder */
65 opus_int silk_control_encoder(
66     silk_encoder_state_Fxx          *psEnc,                                 /* I/O  Pointer to Silk encoder state                                               */
67     silk_EncControlStruct           *encControl,                            /* I    Control structure                                                           */
68     const opus_int32                TargetRate_bps,                         /* I    Target max bitrate (bps)                                                    */
69     const opus_int                  allow_bw_switch,                        /* I    Flag to allow switching audio bandwidth                                     */
70     const opus_int                  channelNb,                              /* I    Channel number                                                              */
71     const opus_int                  force_fs_kHz
72 )
73 {
74     opus_int   fs_kHz, ret = 0;
75
76     psEnc->sCmn.useDTX                 = encControl->useDTX;
77     psEnc->sCmn.useCBR                 = encControl->useCBR;
78     psEnc->sCmn.API_fs_Hz              = encControl->API_sampleRate;
79     psEnc->sCmn.maxInternal_fs_Hz      = encControl->maxInternalSampleRate;
80     psEnc->sCmn.minInternal_fs_Hz      = encControl->minInternalSampleRate;
81     psEnc->sCmn.desiredInternal_fs_Hz  = encControl->desiredInternalSampleRate;
82     psEnc->sCmn.useInBandFEC           = encControl->useInBandFEC;
83     psEnc->sCmn.nChannelsAPI           = encControl->nChannelsAPI;
84     psEnc->sCmn.nChannelsInternal      = encControl->nChannelsInternal;
85     psEnc->sCmn.allow_bandwidth_switch = allow_bw_switch;
86     psEnc->sCmn.channelNb              = channelNb;
87
88     if( psEnc->sCmn.controlled_since_last_payload != 0 && psEnc->sCmn.prefillFlag == 0 ) {
89         if( psEnc->sCmn.API_fs_Hz != psEnc->sCmn.prev_API_fs_Hz && psEnc->sCmn.fs_kHz > 0 ) {
90             /* Change in API sampling rate in the middle of encoding a packet */
91             ret += silk_setup_resamplers( psEnc, psEnc->sCmn.fs_kHz );
92         }
93         return ret;
94     }
95
96     /* Beyond this point we know that there are no previously coded frames in the payload buffer */
97
98     /********************************************/
99     /* Determine internal sampling rate         */
100     /********************************************/
101     fs_kHz = silk_control_audio_bandwidth( &psEnc->sCmn, encControl );
102     if( force_fs_kHz ) {
103        fs_kHz = force_fs_kHz;
104     }
105     /********************************************/
106     /* Prepare resampler and buffered data      */
107     /********************************************/
108     ret += silk_setup_resamplers( psEnc, fs_kHz );
109
110     /********************************************/
111     /* Set internal sampling frequency          */
112     /********************************************/
113     ret += silk_setup_fs( psEnc, fs_kHz, encControl->payloadSize_ms );
114
115     /********************************************/
116     /* Set encoding complexity                  */
117     /********************************************/
118     ret += silk_setup_complexity( &psEnc->sCmn, encControl->complexity  );
119
120     /********************************************/
121     /* Set packet loss rate measured by farend  */
122     /********************************************/
123     psEnc->sCmn.PacketLoss_perc = encControl->packetLossPercentage;
124
125     /********************************************/
126     /* Set LBRR usage                           */
127     /********************************************/
128     ret += silk_setup_LBRR( &psEnc->sCmn, TargetRate_bps );
129
130     psEnc->sCmn.controlled_since_last_payload = 1;
131
132     return ret;
133 }
134
135 static opus_int silk_setup_resamplers(
136     silk_encoder_state_Fxx          *psEnc,             /* I/O                      */
137     opus_int                         fs_kHz              /* I                        */
138 )
139 {
140     opus_int   ret = SILK_NO_ERROR;
141     opus_int32 nSamples_temp;
142     SAVE_STACK;
143
144     if( psEnc->sCmn.fs_kHz != fs_kHz || psEnc->sCmn.prev_API_fs_Hz != psEnc->sCmn.API_fs_Hz )
145     {
146         if( psEnc->sCmn.fs_kHz == 0 ) {
147             /* Initialize the resampler for enc_API.c preparing resampling from API_fs_Hz to fs_kHz */
148             ret += silk_resampler_init( &psEnc->sCmn.resampler_state, psEnc->sCmn.API_fs_Hz, fs_kHz * 1000, 1 );
149         } else {
150             VARDECL( opus_int16, x_buf_API_fs_Hz );
151             VARDECL( silk_resampler_state_struct, temp_resampler_state );
152 #ifdef FIXED_POINT
153             opus_int16 *x_bufFIX = psEnc->x_buf;
154 #else
155             VARDECL( opus_int16, x_bufFIX );
156 #endif
157             opus_int32 nAPI_Samples_temp;
158
159             nSamples_temp = silk_LSHIFT( psEnc->sCmn.frame_length, 1 ) + LA_SHAPE_MS * psEnc->sCmn.fs_kHz;
160
161 #ifndef FIXED_POINT
162             ALLOC( x_bufFIX, nSamples_temp, opus_int16 );
163             silk_float2short_array( x_bufFIX, psEnc->x_buf, nSamples_temp );
164 #endif
165
166             /* Initialize resampler for temporary resampling of x_buf data to API_fs_Hz */
167             ALLOC( temp_resampler_state, 1, silk_resampler_state_struct );
168             ret += silk_resampler_init( temp_resampler_state, silk_SMULBB( psEnc->sCmn.fs_kHz, 1000 ), psEnc->sCmn.API_fs_Hz, 0 );
169
170             /* Calculate number of samples to temporarily upsample */
171             nAPI_Samples_temp = silk_DIV32_16( nSamples_temp * psEnc->sCmn.API_fs_Hz, silk_SMULBB( psEnc->sCmn.fs_kHz, 1000 ) );
172
173             /* Temporary resampling of x_buf data to API_fs_Hz */
174             ALLOC( x_buf_API_fs_Hz, nAPI_Samples_temp, opus_int16 );
175             ret += silk_resampler( temp_resampler_state, x_buf_API_fs_Hz, x_bufFIX, nSamples_temp );
176
177             /* Initialize the resampler for enc_API.c preparing resampling from API_fs_Hz to fs_kHz */
178             ret += silk_resampler_init( &psEnc->sCmn.resampler_state, psEnc->sCmn.API_fs_Hz, silk_SMULBB( fs_kHz, 1000 ), 1 );
179
180             /* Correct resampler state by resampling buffered data from API_fs_Hz to fs_kHz */
181             ret += silk_resampler( &psEnc->sCmn.resampler_state, x_bufFIX, x_buf_API_fs_Hz, nAPI_Samples_temp );
182
183 #ifndef FIXED_POINT
184             silk_short2float_array( psEnc->x_buf, x_bufFIX, ( 2 * MAX_FRAME_LENGTH_MS + LA_SHAPE_MS ) * fs_kHz );
185 #endif
186         }
187     }
188
189     psEnc->sCmn.prev_API_fs_Hz = psEnc->sCmn.API_fs_Hz;
190
191     RESTORE_STACK;
192     return ret;
193 }
194
195 static 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 = silk_SMULBB( PacketSize_ms, fs_kHz );
215             psEnc->sCmn.pitch_LPC_win_length = silk_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 = silk_DIV32_16( PacketSize_ms, MAX_FRAME_LENGTH_MS );
223             psEnc->sCmn.nb_subfr = MAX_NB_SUBFR;
224             psEnc->sCmn.frame_length = silk_SMULBB( 20, fs_kHz );
225             psEnc->sCmn.pitch_LPC_win_length = silk_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     silk_assert( fs_kHz == 8 || fs_kHz == 12 || fs_kHz == 16 );
238     silk_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         silk_memset( &psEnc->sShape,               0, sizeof( psEnc->sShape ) );
242         silk_memset( &psEnc->sPrefilt,             0, sizeof( psEnc->sPrefilt ) );
243         silk_memset( &psEnc->sCmn.sNSQ,            0, sizeof( psEnc->sCmn.sNSQ ) );
244         silk_memset( psEnc->sCmn.prev_NLSFq_Q15,   0, sizeof( psEnc->sCmn.prev_NLSFq_Q15 ) );
245         silk_memset( &psEnc->sCmn.sLP.In_LP_State, 0, sizeof( psEnc->sCmn.sLP.In_LP_State ) );
246         psEnc->sCmn.inputBufIx                  = 0;
247         psEnc->sCmn.nFramesEncoded              = 0;
248         psEnc->sCmn.TargetRate_bps              = 0;     /* trigger new SNR computation */
249
250         /* Initialize non-zero parameters */
251         psEnc->sCmn.prevLag                     = 100;
252         psEnc->sCmn.first_frame_after_reset     = 1;
253         psEnc->sPrefilt.lagPrev                 = 100;
254         psEnc->sShape.LastGainIndex             = 10;
255         psEnc->sCmn.sNSQ.lagPrev                = 100;
256         psEnc->sCmn.sNSQ.prev_gain_Q16          = 65536;
257         psEnc->sCmn.prevSignalType              = TYPE_NO_VOICE_ACTIVITY;
258
259         psEnc->sCmn.fs_kHz = fs_kHz;
260         if( psEnc->sCmn.fs_kHz == 8 ) {
261             if( psEnc->sCmn.nb_subfr == MAX_NB_SUBFR ) {
262                 psEnc->sCmn.pitch_contour_iCDF = silk_pitch_contour_NB_iCDF;
263             } else {
264                 psEnc->sCmn.pitch_contour_iCDF = silk_pitch_contour_10_ms_NB_iCDF;
265             }
266         } else {
267             if( psEnc->sCmn.nb_subfr == MAX_NB_SUBFR ) {
268                 psEnc->sCmn.pitch_contour_iCDF = silk_pitch_contour_iCDF;
269             } else {
270                 psEnc->sCmn.pitch_contour_iCDF = silk_pitch_contour_10_ms_iCDF;
271             }
272         }
273         if( psEnc->sCmn.fs_kHz == 8 || psEnc->sCmn.fs_kHz == 12 ) {
274             psEnc->sCmn.predictLPCOrder = MIN_LPC_ORDER;
275             psEnc->sCmn.psNLSF_CB  = &silk_NLSF_CB_NB_MB;
276         } else {
277             psEnc->sCmn.predictLPCOrder = MAX_LPC_ORDER;
278             psEnc->sCmn.psNLSF_CB  = &silk_NLSF_CB_WB;
279         }
280         psEnc->sCmn.subfr_length   = SUB_FRAME_LENGTH_MS * fs_kHz;
281         psEnc->sCmn.frame_length   = silk_SMULBB( psEnc->sCmn.subfr_length, psEnc->sCmn.nb_subfr );
282         psEnc->sCmn.ltp_mem_length = silk_SMULBB( LTP_MEM_LENGTH_MS, fs_kHz );
283         psEnc->sCmn.la_pitch       = silk_SMULBB( LA_PITCH_MS, fs_kHz );
284         psEnc->sCmn.max_pitch_lag  = silk_SMULBB( 18, fs_kHz );
285         if( psEnc->sCmn.nb_subfr == MAX_NB_SUBFR ) {
286             psEnc->sCmn.pitch_LPC_win_length = silk_SMULBB( FIND_PITCH_LPC_WIN_MS, fs_kHz );
287         } else {
288             psEnc->sCmn.pitch_LPC_win_length = silk_SMULBB( FIND_PITCH_LPC_WIN_MS_2_SF, fs_kHz );
289         }
290         if( psEnc->sCmn.fs_kHz == 16 ) {
291             psEnc->sCmn.mu_LTP_Q9 = SILK_FIX_CONST( MU_LTP_QUANT_WB, 9 );
292             psEnc->sCmn.pitch_lag_low_bits_iCDF = silk_uniform8_iCDF;
293         } else if( psEnc->sCmn.fs_kHz == 12 ) {
294             psEnc->sCmn.mu_LTP_Q9 = SILK_FIX_CONST( MU_LTP_QUANT_MB, 9 );
295             psEnc->sCmn.pitch_lag_low_bits_iCDF = silk_uniform6_iCDF;
296         } else {
297             psEnc->sCmn.mu_LTP_Q9 = SILK_FIX_CONST( MU_LTP_QUANT_NB, 9 );
298             psEnc->sCmn.pitch_lag_low_bits_iCDF = silk_uniform4_iCDF;
299         }
300     }
301
302     /* Check that settings are valid */
303     silk_assert( ( psEnc->sCmn.subfr_length * psEnc->sCmn.nb_subfr ) == psEnc->sCmn.frame_length );
304
305     return ret;
306 }
307
308 static opus_int silk_setup_complexity(
309     silk_encoder_state              *psEncC,            /* I/O                      */
310     opus_int                        Complexity          /* I                        */
311 )
312 {
313     opus_int ret = 0;
314
315     /* Set encoding complexity */
316     silk_assert( Complexity >= 0 && Complexity <= 10 );
317     if( Complexity < 2 ) {
318         psEncC->pitchEstimationComplexity       = SILK_PE_MIN_COMPLEX;
319         psEncC->pitchEstimationThreshold_Q16    = SILK_FIX_CONST( 0.8, 16 );
320         psEncC->pitchEstimationLPCOrder         = 6;
321         psEncC->shapingLPCOrder                 = 8;
322         psEncC->la_shape                        = 3 * psEncC->fs_kHz;
323         psEncC->nStatesDelayedDecision          = 1;
324         psEncC->useInterpolatedNLSFs            = 0;
325         psEncC->LTPQuantLowComplexity           = 1;
326         psEncC->NLSF_MSVQ_Survivors             = 2;
327         psEncC->warping_Q16                     = 0;
328     } else if( Complexity < 4 ) {
329         psEncC->pitchEstimationComplexity       = SILK_PE_MID_COMPLEX;
330         psEncC->pitchEstimationThreshold_Q16    = SILK_FIX_CONST( 0.76, 16 );
331         psEncC->pitchEstimationLPCOrder         = 8;
332         psEncC->shapingLPCOrder                 = 10;
333         psEncC->la_shape                        = 5 * psEncC->fs_kHz;
334         psEncC->nStatesDelayedDecision          = 1;
335         psEncC->useInterpolatedNLSFs            = 0;
336         psEncC->LTPQuantLowComplexity           = 0;
337         psEncC->NLSF_MSVQ_Survivors             = 4;
338         psEncC->warping_Q16                     = 0;
339     } else if( Complexity < 6 ) {
340         psEncC->pitchEstimationComplexity       = SILK_PE_MID_COMPLEX;
341         psEncC->pitchEstimationThreshold_Q16    = SILK_FIX_CONST( 0.74, 16 );
342         psEncC->pitchEstimationLPCOrder         = 10;
343         psEncC->shapingLPCOrder                 = 12;
344         psEncC->la_shape                        = 5 * psEncC->fs_kHz;
345         psEncC->nStatesDelayedDecision          = 2;
346         psEncC->useInterpolatedNLSFs            = 1;
347         psEncC->LTPQuantLowComplexity           = 0;
348         psEncC->NLSF_MSVQ_Survivors             = 8;
349         psEncC->warping_Q16                     = psEncC->fs_kHz * SILK_FIX_CONST( WARPING_MULTIPLIER, 16 );
350     } else if( Complexity < 8 ) {
351         psEncC->pitchEstimationComplexity       = SILK_PE_MID_COMPLEX;
352         psEncC->pitchEstimationThreshold_Q16    = SILK_FIX_CONST( 0.72, 16 );
353         psEncC->pitchEstimationLPCOrder         = 12;
354         psEncC->shapingLPCOrder                 = 14;
355         psEncC->la_shape                        = 5 * psEncC->fs_kHz;
356         psEncC->nStatesDelayedDecision          = 3;
357         psEncC->useInterpolatedNLSFs            = 1;
358         psEncC->LTPQuantLowComplexity           = 0;
359         psEncC->NLSF_MSVQ_Survivors             = 16;
360         psEncC->warping_Q16                     = psEncC->fs_kHz * SILK_FIX_CONST( WARPING_MULTIPLIER, 16 );
361     } else {
362         psEncC->pitchEstimationComplexity       = SILK_PE_MAX_COMPLEX;
363         psEncC->pitchEstimationThreshold_Q16    = SILK_FIX_CONST( 0.7, 16 );
364         psEncC->pitchEstimationLPCOrder         = 16;
365         psEncC->shapingLPCOrder                 = 16;
366         psEncC->la_shape                        = 5 * psEncC->fs_kHz;
367         psEncC->nStatesDelayedDecision          = MAX_DEL_DEC_STATES;
368         psEncC->useInterpolatedNLSFs            = 1;
369         psEncC->LTPQuantLowComplexity           = 0;
370         psEncC->NLSF_MSVQ_Survivors             = 32;
371         psEncC->warping_Q16                     = psEncC->fs_kHz * SILK_FIX_CONST( WARPING_MULTIPLIER, 16 );
372     }
373
374     /* Do not allow higher pitch estimation LPC order than predict LPC order */
375     psEncC->pitchEstimationLPCOrder = silk_min_int( psEncC->pitchEstimationLPCOrder, psEncC->predictLPCOrder );
376     psEncC->shapeWinLength          = SUB_FRAME_LENGTH_MS * psEncC->fs_kHz + 2 * psEncC->la_shape;
377     psEncC->Complexity              = Complexity;
378
379     silk_assert( psEncC->pitchEstimationLPCOrder <= MAX_FIND_PITCH_LPC_ORDER );
380     silk_assert( psEncC->shapingLPCOrder         <= MAX_SHAPE_LPC_ORDER      );
381     silk_assert( psEncC->nStatesDelayedDecision  <= MAX_DEL_DEC_STATES       );
382     silk_assert( psEncC->warping_Q16             <= 32767                    );
383     silk_assert( psEncC->la_shape                <= LA_SHAPE_MAX             );
384     silk_assert( psEncC->shapeWinLength          <= SHAPE_LPC_WIN_MAX        );
385     silk_assert( psEncC->NLSF_MSVQ_Survivors     <= NLSF_VQ_MAX_SURVIVORS    );
386
387     return ret;
388 }
389
390 static inline opus_int silk_setup_LBRR(
391     silk_encoder_state          *psEncC,            /* I/O                      */
392     const opus_int32            TargetRate_bps      /* I                        */
393 )
394 {
395     opus_int   ret = SILK_NO_ERROR;
396     opus_int32 LBRR_rate_thres_bps;
397
398     psEncC->LBRR_enabled = 0;
399     if( psEncC->useInBandFEC && psEncC->PacketLoss_perc > 0 ) {
400         if( psEncC->fs_kHz == 8 ) {
401             LBRR_rate_thres_bps = LBRR_NB_MIN_RATE_BPS;
402         } else if( psEncC->fs_kHz == 12 ) {
403             LBRR_rate_thres_bps = LBRR_MB_MIN_RATE_BPS;
404         } else {
405             LBRR_rate_thres_bps = LBRR_WB_MIN_RATE_BPS;
406         }
407         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 ) );
408
409         if( TargetRate_bps > LBRR_rate_thres_bps ) {
410             /* Set gain increase for coding LBRR excitation */
411             psEncC->LBRR_enabled = 1;
412             psEncC->LBRR_GainIncreases = silk_max_int( 7 - silk_SMULWB( (opus_int32)psEncC->PacketLoss_perc, SILK_FIX_CONST( 0.4, 16 ) ), 2 );
413         }
414     }
415
416     return ret;
417 }