control_codec.c 20 KB

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