control_codec.c 20 KB

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