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 ***********************************************************************/
36 #include "tuning_parameters.h"
43 /****************************************/
44 /* Encoder functions */
45 /****************************************/
47 opus_int silk_Get_Encoder_Size( int *encSizeBytes )
49 opus_int ret = SILK_NO_ERROR;
51 *encSizeBytes = sizeof( silk_encoder );
56 /*************************/
57 /* Init or Reset encoder */
58 /*************************/
59 opus_int silk_InitEncoder(
60 void *encState, /* I/O: State */
61 silk_EncControlStruct *encStatus /* O: Control structure */
65 opus_int n, ret = SILK_NO_ERROR;
67 psEnc = (silk_encoder *)encState;
70 silk_memset( psEnc, 0, sizeof( silk_encoder ) );
71 for( n = 0; n < ENCODER_NUM_CHANNELS; n++ ) {
72 if( ret += silk_init_encoder( &psEnc->state_Fxx[ n ] ) ) {
77 psEnc->nChannelsAPI = 1;
78 psEnc->nChannelsInternal = 1;
80 /* Read control structure */
81 if( ret += silk_QueryEncoder( encState, encStatus ) ) {
88 /***************************************/
89 /* Read control structure from encoder */
90 /***************************************/
91 opus_int silk_QueryEncoder(
92 const void *encState, /* I: State Vector */
93 silk_EncControlStruct *encStatus /* O: Control Structure */
96 opus_int ret = SILK_NO_ERROR;
97 silk_encoder_state_Fxx *state_Fxx;
98 silk_encoder *psEnc = (silk_encoder *)encState;
100 state_Fxx = psEnc->state_Fxx;
102 encStatus->nChannelsAPI = psEnc->nChannelsAPI;
103 encStatus->nChannelsInternal = psEnc->nChannelsInternal;
104 encStatus->API_sampleRate = state_Fxx[ 0 ].sCmn.API_fs_Hz;
105 encStatus->maxInternalSampleRate = state_Fxx[ 0 ].sCmn.maxInternal_fs_Hz;
106 encStatus->minInternalSampleRate = state_Fxx[ 0 ].sCmn.minInternal_fs_Hz;
107 encStatus->desiredInternalSampleRate = state_Fxx[ 0 ].sCmn.desiredInternal_fs_Hz;
108 encStatus->payloadSize_ms = state_Fxx[ 0 ].sCmn.PacketSize_ms;
109 encStatus->bitRate = state_Fxx[ 0 ].sCmn.TargetRate_bps;
110 encStatus->packetLossPercentage = state_Fxx[ 0 ].sCmn.PacketLoss_perc;
111 encStatus->complexity = state_Fxx[ 0 ].sCmn.Complexity;
112 encStatus->useInBandFEC = state_Fxx[ 0 ].sCmn.useInBandFEC;
113 encStatus->useDTX = state_Fxx[ 0 ].sCmn.useDTX;
114 encStatus->useCBR = state_Fxx[ 0 ].sCmn.useCBR;
115 encStatus->internalSampleRate = silk_SMULBB( state_Fxx[ 0 ].sCmn.fs_kHz, 1000 );
116 encStatus->allowBandwidthSwitch = state_Fxx[ 0 ].sCmn.allow_bandwidth_switch;
117 encStatus->inWBmodeWithoutVariableLP = state_Fxx[ 0 ].sCmn.fs_kHz == 16 && state_Fxx[ 0 ].sCmn.sLP.mode == 0;
122 static void stereo_crossmix(const opus_int16 *in, opus_int16 *out, int channel, int len, int to_mono, int id)
125 opus_int16 delta, g1, g2;
126 const opus_int16 *x1, *x2;
130 g1 = to_mono ? 16384: 8192;
131 g2 = to_mono ? 0 : 8192;
133 /* We want to finish at 0.5 */
134 delta = (16384+(len>>1))/(len);
141 for ( ; i < len>>1; i++ ) {
142 out[ i ] = silk_RSHIFT_ROUND( silk_SMLABB( silk_SMULBB( x1[ 2*i ], g1 ), x2[ 2*i ], g2 ), 14 );
148 for ( ; i < len; i++ ) {
149 out[ i ] = silk_RSHIFT( (opus_int32)x1[ 2*i ] + (opus_int32)x2[ 2*i ], 1 );
152 for ( ; i < len; i++ ) {
153 out[ i ] = x1[ 2*i ];
156 /*fprintf(stderr, "%d %d %d\n", g1, g2, to_mono);*/
159 /**************************/
160 /* Encode frame with Silk */
161 /**************************/
162 opus_int silk_Encode(
163 void *encState, /* I/O: State */
164 silk_EncControlStruct *encControl, /* I: Control structure */
165 const opus_int16 *samplesIn, /* I: Speech sample input vector */
166 opus_int nSamplesIn, /* I: Number of samples in input vector */
167 ec_enc *psRangeEnc, /* I/O Compressor data structure */
168 opus_int *nBytesOut, /* I/O: Number of bytes in payload (input: Max bytes) */
169 const opus_int prefillFlag /* I: Flag to indicate prefilling buffers; no coding */
172 opus_int n, i, nBits, flags, tmp_payloadSize_ms = 0, tmp_complexity = 0, ret = 0;
173 opus_int nSamplesToBuffer, nBlocksOf10ms, nSamplesFromInput = 0;
174 opus_int speech_act_thr_for_switch_Q8;
175 opus_int32 TargetRate_bps, MStargetRates_bps[ 2 ], channelRate_bps, LBRR_symbol;
176 silk_encoder *psEnc = ( silk_encoder * )encState;
177 opus_int16 buf[ MAX_FRAME_LENGTH_MS * MAX_API_FS_KHZ ];
179 psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded = psEnc->state_Fxx[ 1 ].sCmn.nFramesEncoded = 0;
181 /* Check values in encoder control structure */
182 if( ( ret = check_control_input( encControl ) != 0 ) ) {
187 if( encControl->nChannelsInternal > psEnc->nChannelsInternal ) {
188 /* Mono -> Stereo transition: init state of second channel and stereo state */
189 ret += silk_init_encoder( &psEnc->state_Fxx[ 1 ] );
190 silk_memset( psEnc->sStereo.pred_prev_Q13, 0, sizeof( psEnc->sStereo.pred_prev_Q13 ) );
191 silk_memset( psEnc->sStereo.sSide, 0, sizeof( psEnc->sStereo.sSide ) );
192 silk_memset( psEnc->sStereo.mid_side_amp_Q0, 0, sizeof( psEnc->sStereo.mid_side_amp_Q0 ) );
193 psEnc->sStereo.width_prev_Q14 = 0;
194 psEnc->sStereo.smth_width_Q14 = SILK_FIX_CONST( 1, 14 );
195 if( psEnc->nChannelsAPI == 2 ) {
196 silk_memcpy( &psEnc->state_Fxx[ 1 ].sCmn.resampler_state, &psEnc->state_Fxx[ 0 ].sCmn.resampler_state, sizeof( silk_resampler_state_struct ) );
197 silk_memcpy( &psEnc->state_Fxx[ 1 ].sCmn.In_HP_State, &psEnc->state_Fxx[ 0 ].sCmn.In_HP_State, sizeof( psEnc->state_Fxx[ 1 ].sCmn.In_HP_State ) );
200 psEnc->nChannelsAPI = encControl->nChannelsAPI;
201 psEnc->nChannelsInternal = encControl->nChannelsInternal;
203 nBlocksOf10ms = silk_DIV32( 100 * nSamplesIn, encControl->API_sampleRate );
205 /* Only accept input length of 10 ms */
206 if( nBlocksOf10ms != 1 ) {
207 ret = SILK_ENC_INPUT_INVALID_NO_OF_SAMPLES;
212 for( n = 0; n < encControl->nChannelsInternal; n++ ) {
213 if( (ret = silk_init_encoder( &psEnc->state_Fxx[ n ] ) ) != 0 ) {
217 tmp_payloadSize_ms = encControl->payloadSize_ms;
218 encControl->payloadSize_ms = 10;
219 tmp_complexity = encControl->complexity;
220 encControl->complexity = 0;
221 for( n = 0; n < encControl->nChannelsInternal; n++ ) {
222 psEnc->state_Fxx[ n ].sCmn.controlled_since_last_payload = 0;
223 psEnc->state_Fxx[ n ].sCmn.prefillFlag = 1;
226 /* Only accept input lengths that are a multiple of 10 ms */
227 if( nBlocksOf10ms * encControl->API_sampleRate != 100 * nSamplesIn || nSamplesIn < 0 ) {
228 ret = SILK_ENC_INPUT_INVALID_NO_OF_SAMPLES;
232 /* Make sure no more than one packet can be produced */
233 if( 1000 * (opus_int32)nSamplesIn > encControl->payloadSize_ms * encControl->API_sampleRate ) {
234 ret = SILK_ENC_INPUT_INVALID_NO_OF_SAMPLES;
240 TargetRate_bps = silk_RSHIFT32( encControl->bitRate, encControl->nChannelsInternal - 1 );
241 for( n = 0; n < encControl->nChannelsInternal; n++ ) {
242 /* JMV: Force the side channel to the same rate as the mid. Is this the right way? */
243 int force_fs_kHz = (n==1) ? psEnc->state_Fxx[0].sCmn.fs_kHz : 0;
244 if( ( ret = silk_control_encoder( &psEnc->state_Fxx[ n ], encControl, TargetRate_bps, psEnc->allowBandwidthSwitch, n, force_fs_kHz ) ) != 0 ) {
249 silk_assert( encControl->nChannelsInternal == 1 || psEnc->state_Fxx[ 0 ].sCmn.fs_kHz == psEnc->state_Fxx[ 1 ].sCmn.fs_kHz );
251 /* Input buffering/resampling and encoding */
253 nSamplesToBuffer = psEnc->state_Fxx[ 0 ].sCmn.frame_length - psEnc->state_Fxx[ 0 ].sCmn.inputBufIx;
254 nSamplesToBuffer = silk_min( nSamplesToBuffer, 10 * nBlocksOf10ms * psEnc->state_Fxx[ 0 ].sCmn.fs_kHz );
255 nSamplesFromInput = silk_DIV32_16( nSamplesToBuffer * psEnc->state_Fxx[ 0 ].sCmn.API_fs_Hz, psEnc->state_Fxx[ 0 ].sCmn.fs_kHz * 1000 );
256 /* Resample and write to buffer */
257 if( encControl->nChannelsAPI == 2 && encControl->nChannelsInternal == 2 ) {
258 int id = psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded;
259 if ( encControl->toMono ) {
260 stereo_crossmix( samplesIn, buf, 0, nSamplesFromInput, 1, id );
261 } else if( psEnc->nPrevChannelsInternal == 1 || encControl->toMono == -1 ) {
262 stereo_crossmix( samplesIn, buf, 0, nSamplesFromInput, 0, id );
264 for( n = 0; n < nSamplesFromInput; n++ ) {
265 buf[ n ] = samplesIn[ 2 * n ];
268 /* Making sure to start both resamplers from the same state when switching from mono to stereo */
269 if(psEnc->nPrevChannelsInternal == 1 && id==0)
270 silk_memcpy(&psEnc->state_Fxx[ 1 ].sCmn.resampler_state, &psEnc->state_Fxx[ 0 ].sCmn.resampler_state, sizeof(psEnc->state_Fxx[ 1 ].sCmn.resampler_state));
272 ret += silk_resampler( &psEnc->state_Fxx[ 0 ].sCmn.resampler_state,
273 &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput );
274 psEnc->state_Fxx[ 0 ].sCmn.inputBufIx += nSamplesToBuffer;
276 nSamplesToBuffer = psEnc->state_Fxx[ 1 ].sCmn.frame_length - psEnc->state_Fxx[ 1 ].sCmn.inputBufIx;
277 nSamplesToBuffer = silk_min( nSamplesToBuffer, 10 * nBlocksOf10ms * psEnc->state_Fxx[ 1 ].sCmn.fs_kHz );
278 if ( encControl->toMono ) {
279 stereo_crossmix( samplesIn, buf, 1, nSamplesFromInput, 1, id );
280 } else if( psEnc->nPrevChannelsInternal == 1 ) {
281 stereo_crossmix( samplesIn, buf, 1, nSamplesFromInput, 0, id );
283 for( n = 0; n < nSamplesFromInput; n++ ) {
284 buf[ n ] = samplesIn[ 2 * n + 1 ];
287 ret += silk_resampler( &psEnc->state_Fxx[ 1 ].sCmn.resampler_state,
288 &psEnc->state_Fxx[ 1 ].sCmn.inputBuf[ psEnc->state_Fxx[ 1 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput );
289 psEnc->state_Fxx[ 1 ].sCmn.inputBufIx += nSamplesToBuffer;
290 } else if( encControl->nChannelsAPI == 2 && encControl->nChannelsInternal == 1 ) {
291 /* Combine left and right channels before resampling */
292 for( n = 0; n < nSamplesFromInput; n++ ) {
293 buf[ n ] = (opus_int16)silk_RSHIFT_ROUND( samplesIn[ 2 * n ] + samplesIn[ 2 * n + 1 ], 1 );
295 ret += silk_resampler( &psEnc->state_Fxx[ 0 ].sCmn.resampler_state,
296 &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput );
297 psEnc->state_Fxx[ 0 ].sCmn.inputBufIx += nSamplesToBuffer;
299 silk_assert( encControl->nChannelsAPI == 1 && encControl->nChannelsInternal == 1 );
300 ret += silk_resampler( &psEnc->state_Fxx[ 0 ].sCmn.resampler_state,
301 &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx + 2 ], samplesIn, nSamplesFromInput );
302 psEnc->state_Fxx[ 0 ].sCmn.inputBufIx += nSamplesToBuffer;
305 samplesIn += nSamplesFromInput * encControl->nChannelsAPI;
306 nSamplesIn -= nSamplesFromInput;
309 psEnc->allowBandwidthSwitch = 0;
312 if( psEnc->state_Fxx[ 0 ].sCmn.inputBufIx >= psEnc->state_Fxx[ 0 ].sCmn.frame_length ) {
313 /* Enough data in input buffer, so encode */
314 silk_assert( psEnc->state_Fxx[ 0 ].sCmn.inputBufIx == psEnc->state_Fxx[ 0 ].sCmn.frame_length );
315 silk_assert( encControl->nChannelsInternal == 1 || psEnc->state_Fxx[ 1 ].sCmn.inputBufIx == psEnc->state_Fxx[ 1 ].sCmn.frame_length );
317 /* Deal with LBRR data */
318 if( psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded == 0 && !prefillFlag ) {
319 /* Create space at start of payload for VAD and FEC flags */
320 opus_uint8 iCDF[ 2 ] = { 0, 0 };
321 iCDF[ 0 ] = 256 - silk_RSHIFT( 256, ( psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket + 1 ) * encControl->nChannelsInternal );
322 ec_enc_icdf( psRangeEnc, 0, iCDF, 8 );
324 /* Encode any LBRR data from previous packet */
325 /* Encode LBRR flags */
326 for( n = 0; n < encControl->nChannelsInternal; n++ ) {
328 for( i = 0; i < psEnc->state_Fxx[ n ].sCmn.nFramesPerPacket; i++ ) {
329 LBRR_symbol |= silk_LSHIFT( psEnc->state_Fxx[ n ].sCmn.LBRR_flags[ i ], i );
331 psEnc->state_Fxx[ n ].sCmn.LBRR_flag = LBRR_symbol > 0 ? 1 : 0;
332 if( LBRR_symbol && psEnc->state_Fxx[ n ].sCmn.nFramesPerPacket > 1 ) {
333 ec_enc_icdf( psRangeEnc, LBRR_symbol - 1, silk_LBRR_flags_iCDF_ptr[ psEnc->state_Fxx[ n ].sCmn.nFramesPerPacket - 2 ], 8 );
337 /* Code LBRR indices and excitation signals */
338 for( i = 0; i < psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket; i++ ) {
339 for( n = 0; n < encControl->nChannelsInternal; n++ ) {
340 if( psEnc->state_Fxx[ n ].sCmn.LBRR_flags[ i ] ) {
341 if( encControl->nChannelsInternal == 2 && n == 0 ) {
342 silk_stereo_encode_pred( psRangeEnc, psEnc->sStereo.predIx[ i ] );
343 /* For LBRR data there's no need to code the mid-only flag if the side-channel LBRR flag is set */
344 if( psEnc->state_Fxx[ 1 ].sCmn.LBRR_flags[ i ] == 0 ) {
345 silk_stereo_encode_mid_only( psRangeEnc, psEnc->sStereo.mid_only_flags[ i ] );
348 silk_encode_indices( &psEnc->state_Fxx[ n ].sCmn, psRangeEnc, i, 1 );
349 silk_encode_pulses( psRangeEnc, psEnc->state_Fxx[ n ].sCmn.indices_LBRR[i].signalType, psEnc->state_Fxx[ n ].sCmn.indices_LBRR[i].quantOffsetType,
350 psEnc->state_Fxx[ n ].sCmn.pulses_LBRR[ i ], psEnc->state_Fxx[ n ].sCmn.frame_length );
355 /* Reset LBRR flags */
356 for( n = 0; n < encControl->nChannelsInternal; n++ ) {
357 silk_memset( psEnc->state_Fxx[ n ].sCmn.LBRR_flags, 0, sizeof( psEnc->state_Fxx[ n ].sCmn.LBRR_flags ) );
361 silk_HP_variable_cutoff( psEnc->state_Fxx );
363 /* Total target bits for packet */
364 nBits = silk_DIV32_16( silk_MUL( encControl->bitRate, encControl->payloadSize_ms ), 1000 );
365 /* Subtract half of the bits already used */
367 nBits -= ec_tell( psRangeEnc ) >> 1;
368 /* Divide by number of uncoded frames left in packet */
369 nBits = silk_DIV32_16( nBits, psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket - psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded );
370 /* Convert to bits/second */
371 if( encControl->payloadSize_ms == 10 ) {
372 TargetRate_bps = silk_SMULBB( nBits, 100 );
374 TargetRate_bps = silk_SMULBB( nBits, 50 );
376 /* Subtract fraction of bits in excess of target in previous packets */
377 TargetRate_bps -= silk_DIV32_16( silk_MUL( psEnc->nBitsExceeded, 1000 ), BITRESERVOIR_DECAY_TIME_MS );
378 /* Never exceed input bitrate */
379 TargetRate_bps = silk_LIMIT( TargetRate_bps, encControl->bitRate, 5000 );
381 /* Convert Left/Right to Mid/Side */
382 if( encControl->nChannelsInternal == 2 ) {
383 silk_stereo_LR_to_MS( &psEnc->sStereo, &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ 2 ], &psEnc->state_Fxx[ 1 ].sCmn.inputBuf[ 2 ],
384 psEnc->sStereo.predIx[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ], &psEnc->sStereo.mid_only_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ],
385 MStargetRates_bps, TargetRate_bps, psEnc->state_Fxx[ 0 ].sCmn.speech_activity_Q8,
386 psEnc->state_Fxx[ 0 ].sCmn.fs_kHz, psEnc->state_Fxx[ 0 ].sCmn.frame_length );
388 silk_stereo_encode_pred( psRangeEnc, psEnc->sStereo.predIx[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] );
389 silk_stereo_encode_mid_only( psRangeEnc, psEnc->sStereo.mid_only_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] );
393 silk_memcpy( psEnc->state_Fxx[ 0 ].sCmn.inputBuf, psEnc->sStereo.sMid, 2 * sizeof( opus_int16 ) );
394 silk_memcpy( psEnc->sStereo.sMid, &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.frame_length ], 2 * sizeof( opus_int16 ) );
398 for( n = 0; n < encControl->nChannelsInternal; n++ ) {
399 if( encControl->nChannelsInternal == 1 ) {
400 channelRate_bps = TargetRate_bps;
402 channelRate_bps = MStargetRates_bps[ n ];
405 if( channelRate_bps > 0 ) {
406 silk_control_SNR( &psEnc->state_Fxx[ n ].sCmn, channelRate_bps );
408 if( ( ret = silk_encode_frame_Fxx( &psEnc->state_Fxx[ n ], nBytesOut, psRangeEnc ) ) != 0 ) {
411 psEnc->state_Fxx[ n ].sCmn.nFramesEncoded++;
413 psEnc->state_Fxx[ n ].sCmn.controlled_since_last_payload = 0;
414 psEnc->state_Fxx[ n ].sCmn.inputBufIx = 0;
417 /* Insert VAD and FEC flags at beginning of bitstream */
418 if( *nBytesOut > 0 && psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded == psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket) {
420 for( n = 0; n < encControl->nChannelsInternal; n++ ) {
421 for( i = 0; i < psEnc->state_Fxx[ n ].sCmn.nFramesPerPacket; i++ ) {
422 flags = silk_LSHIFT( flags, 1 );
423 flags |= psEnc->state_Fxx[ n ].sCmn.VAD_flags[ i ];
425 flags = silk_LSHIFT( flags, 1 );
426 flags |= psEnc->state_Fxx[ n ].sCmn.LBRR_flag;
429 ec_enc_patch_initial_bits( psRangeEnc, flags, ( psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket + 1 ) * encControl->nChannelsInternal );
431 /* Return zero bytes if all channels DTXed */
432 if( psEnc->state_Fxx[ 0 ].sCmn.inDTX && ( encControl->nChannelsInternal == 1 || psEnc->state_Fxx[ 1 ].sCmn.inDTX ) ) {
436 psEnc->nBitsExceeded += *nBytesOut * 8;
437 psEnc->nBitsExceeded -= silk_DIV32_16( silk_MUL( encControl->bitRate, encControl->payloadSize_ms ), 1000 );
438 psEnc->nBitsExceeded = silk_LIMIT( psEnc->nBitsExceeded, 0, 10000 );
440 /* Update flag indicating if bandwidth switching is allowed */
441 speech_act_thr_for_switch_Q8 = silk_SMLAWB( SILK_FIX_CONST( SPEECH_ACTIVITY_DTX_THRES, 8 ),
442 SILK_FIX_CONST( ( 1 - SPEECH_ACTIVITY_DTX_THRES ) / MAX_BANDWIDTH_SWITCH_DELAY_MS, 16 + 8 ), psEnc->timeSinceSwitchAllowed_ms );
443 if( psEnc->state_Fxx[ 0 ].sCmn.speech_activity_Q8 < speech_act_thr_for_switch_Q8 ) {
444 psEnc->allowBandwidthSwitch = 1;
445 psEnc->timeSinceSwitchAllowed_ms = 0;
447 psEnc->allowBandwidthSwitch = 0;
448 psEnc->timeSinceSwitchAllowed_ms += encControl->payloadSize_ms;
452 if( nSamplesIn == 0 ) {
459 psEnc->nPrevChannelsInternal = encControl->nChannelsInternal;
461 encControl->allowBandwidthSwitch = psEnc->allowBandwidthSwitch;
462 encControl->inWBmodeWithoutVariableLP = psEnc->state_Fxx[ 0 ].sCmn.fs_kHz == 16 && psEnc->state_Fxx[ 0 ].sCmn.sLP.mode == 0;
463 encControl->internalSampleRate = silk_SMULBB( psEnc->state_Fxx[ 0 ].sCmn.fs_kHz, 1000 );
464 encControl->stereoWidth_Q14 = psEnc->sStereo.width_prev_Q14;
466 encControl->payloadSize_ms = tmp_payloadSize_ms;
467 encControl->complexity = tmp_complexity;
468 for( n = 0; n < encControl->nChannelsInternal; n++ ) {
469 psEnc->state_Fxx[ n ].sCmn.controlled_since_last_payload = 0;
470 psEnc->state_Fxx[ n ].sCmn.prefillFlag = 0;