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