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