Fix bustage in a16cef62.
[opus.git] / silk / enc_API.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, 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
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31 #include "define.h"
32 #include "API.h"
33 #include "control.h"
34 #include "typedef.h"
35 #include "stack_alloc.h"
36 #include "structs.h"
37 #include "tuning_parameters.h"
38 #ifdef FIXED_POINT
39 #include "main_FIX.h"
40 #else
41 #include "main_FLP.h"
42 #endif
43
44 /***************************************/
45 /* Read control structure from encoder */
46 /***************************************/
47 static opus_int silk_QueryEncoder(                      /* O    Returns error code                              */
48     const void                      *encState,          /* I    State                                           */
49     silk_EncControlStruct           *encStatus          /* O    Encoder Status                                  */
50 );
51
52 /****************************************/
53 /* Encoder functions                    */
54 /****************************************/
55
56 opus_int silk_Get_Encoder_Size(                         /* O    Returns error code                              */
57     opus_int                        *encSizeBytes       /* O    Number of bytes in SILK encoder state           */
58 )
59 {
60     opus_int ret = SILK_NO_ERROR;
61
62     *encSizeBytes = sizeof( silk_encoder );
63
64     return ret;
65 }
66
67 /*************************/
68 /* Init or Reset encoder */
69 /*************************/
70 opus_int silk_InitEncoder(                              /* O    Returns error code                              */
71     void                            *encState,          /* I/O  State                                           */
72     silk_EncControlStruct           *encStatus          /* O    Encoder Status                                  */
73 )
74 {
75     silk_encoder *psEnc;
76     opus_int n, ret = SILK_NO_ERROR;
77
78     psEnc = (silk_encoder *)encState;
79
80     /* Reset encoder */
81     silk_memset( psEnc, 0, sizeof( silk_encoder ) );
82     for( n = 0; n < ENCODER_NUM_CHANNELS; n++ ) {
83         if( ret += silk_init_encoder( &psEnc->state_Fxx[ n ] ) ) {
84             silk_assert( 0 );
85         }
86     }
87
88     psEnc->nChannelsAPI = 1;
89     psEnc->nChannelsInternal = 1;
90
91     /* Read control structure */
92     if( ret += silk_QueryEncoder( encState, encStatus ) ) {
93         silk_assert( 0 );
94     }
95
96     return ret;
97 }
98
99 /***************************************/
100 /* Read control structure from encoder */
101 /***************************************/
102 static opus_int silk_QueryEncoder(                      /* O    Returns error code                              */
103     const void                      *encState,          /* I    State                                           */
104     silk_EncControlStruct           *encStatus          /* O    Encoder Status                                  */
105 )
106 {
107     opus_int ret = SILK_NO_ERROR;
108     silk_encoder_state_Fxx *state_Fxx;
109     silk_encoder *psEnc = (silk_encoder *)encState;
110
111     state_Fxx = psEnc->state_Fxx;
112
113     encStatus->nChannelsAPI              = psEnc->nChannelsAPI;
114     encStatus->nChannelsInternal         = psEnc->nChannelsInternal;
115     encStatus->API_sampleRate            = state_Fxx[ 0 ].sCmn.API_fs_Hz;
116     encStatus->maxInternalSampleRate     = state_Fxx[ 0 ].sCmn.maxInternal_fs_Hz;
117     encStatus->minInternalSampleRate     = state_Fxx[ 0 ].sCmn.minInternal_fs_Hz;
118     encStatus->desiredInternalSampleRate = state_Fxx[ 0 ].sCmn.desiredInternal_fs_Hz;
119     encStatus->payloadSize_ms            = state_Fxx[ 0 ].sCmn.PacketSize_ms;
120     encStatus->bitRate                   = state_Fxx[ 0 ].sCmn.TargetRate_bps;
121     encStatus->packetLossPercentage      = state_Fxx[ 0 ].sCmn.PacketLoss_perc;
122     encStatus->complexity                = state_Fxx[ 0 ].sCmn.Complexity;
123     encStatus->useInBandFEC              = state_Fxx[ 0 ].sCmn.useInBandFEC;
124     encStatus->useDTX                    = state_Fxx[ 0 ].sCmn.useDTX;
125     encStatus->useCBR                    = state_Fxx[ 0 ].sCmn.useCBR;
126     encStatus->internalSampleRate        = silk_SMULBB( state_Fxx[ 0 ].sCmn.fs_kHz, 1000 );
127     encStatus->allowBandwidthSwitch      = state_Fxx[ 0 ].sCmn.allow_bandwidth_switch;
128     encStatus->inWBmodeWithoutVariableLP = state_Fxx[ 0 ].sCmn.fs_kHz == 16 && state_Fxx[ 0 ].sCmn.sLP.mode == 0;
129
130     return ret;
131 }
132
133
134 /**************************/
135 /* Encode frame with Silk */
136 /**************************/
137 /* Note: if prefillFlag is set, the input must contain 10 ms of audio, irrespective of what                     */
138 /* encControl->payloadSize_ms is set to                                                                         */
139 opus_int silk_Encode(                                   /* O    Returns error code                              */
140     void                            *encState,          /* I/O  State                                           */
141     silk_EncControlStruct           *encControl,        /* I    Control status                                  */
142     const opus_int16                *samplesIn,         /* I    Speech sample input vector                      */
143     opus_int                        nSamplesIn,         /* I    Number of samples in input vector               */
144     ec_enc                          *psRangeEnc,        /* I/O  Compressor data structure                       */
145     opus_int32                      *nBytesOut,         /* I/O  Number of bytes in payload (input: Max bytes)   */
146     const opus_int                  prefillFlag         /* I    Flag to indicate prefilling buffers no coding   */
147 )
148 {
149     opus_int   n, i, nBits, flags, tmp_payloadSize_ms = 0, tmp_complexity = 0, ret = 0;
150     opus_int   nSamplesToBuffer, nSamplesToBufferMax, nBlocksOf10ms;
151     opus_int   nSamplesFromInput = 0, nSamplesFromInputMax;
152     opus_int   speech_act_thr_for_switch_Q8;
153     opus_int32 TargetRate_bps, MStargetRates_bps[ 2 ], channelRate_bps, LBRR_symbol, sum;
154     silk_encoder *psEnc = ( silk_encoder * )encState;
155     VARDECL( opus_int16, buf );
156     opus_int transition, curr_block, tot_blocks;
157     SAVE_STACK;
158
159     psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded = psEnc->state_Fxx[ 1 ].sCmn.nFramesEncoded = 0;
160
161     /* Check values in encoder control structure */
162     if( ( ret = check_control_input( encControl ) != 0 ) ) {
163         silk_assert( 0 );
164         RESTORE_STACK;
165         return ret;
166     }
167
168     encControl->switchReady = 0;
169
170     if( encControl->nChannelsInternal > psEnc->nChannelsInternal ) {
171         /* Mono -> Stereo transition: init state of second channel and stereo state */
172         ret += silk_init_encoder( &psEnc->state_Fxx[ 1 ] );
173         silk_memset( psEnc->sStereo.pred_prev_Q13, 0, sizeof( psEnc->sStereo.pred_prev_Q13 ) );
174         silk_memset( psEnc->sStereo.sSide, 0, sizeof( psEnc->sStereo.sSide ) );
175         psEnc->sStereo.mid_side_amp_Q0[ 0 ] = 0;
176         psEnc->sStereo.mid_side_amp_Q0[ 1 ] = 1;
177         psEnc->sStereo.mid_side_amp_Q0[ 2 ] = 0;
178         psEnc->sStereo.mid_side_amp_Q0[ 3 ] = 1;
179         psEnc->sStereo.width_prev_Q14 = 0;
180         psEnc->sStereo.smth_width_Q14 = SILK_FIX_CONST( 1, 14 );
181         if( psEnc->nChannelsAPI == 2 ) {
182             silk_memcpy( &psEnc->state_Fxx[ 1 ].sCmn.resampler_state, &psEnc->state_Fxx[ 0 ].sCmn.resampler_state, sizeof( silk_resampler_state_struct ) );
183             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 ) );
184         }
185     }
186
187     transition = (encControl->payloadSize_ms != psEnc->state_Fxx[ 0 ].sCmn.PacketSize_ms) || (psEnc->nChannelsInternal != encControl->nChannelsInternal);
188
189     psEnc->nChannelsAPI = encControl->nChannelsAPI;
190     psEnc->nChannelsInternal = encControl->nChannelsInternal;
191
192     nBlocksOf10ms = silk_DIV32( 100 * nSamplesIn, encControl->API_sampleRate );
193     tot_blocks = ( nBlocksOf10ms > 1 ) ? nBlocksOf10ms >> 1 : 1;
194     curr_block = 0;
195     if( prefillFlag ) {
196         /* Only accept input length of 10 ms */
197         if( nBlocksOf10ms != 1 ) {
198             silk_assert( 0 );
199             RESTORE_STACK;
200             return SILK_ENC_INPUT_INVALID_NO_OF_SAMPLES;
201         }
202         /* Reset Encoder */
203         for( n = 0; n < encControl->nChannelsInternal; n++ ) {
204             if( (ret = silk_init_encoder( &psEnc->state_Fxx[ n ] ) ) != 0 ) {
205                 silk_assert( 0 );
206             }
207         }
208         tmp_payloadSize_ms = encControl->payloadSize_ms;
209         encControl->payloadSize_ms = 10;
210         tmp_complexity = encControl->complexity;
211         encControl->complexity = 0;
212         for( n = 0; n < encControl->nChannelsInternal; n++ ) {
213             psEnc->state_Fxx[ n ].sCmn.controlled_since_last_payload = 0;
214             psEnc->state_Fxx[ n ].sCmn.prefillFlag = 1;
215         }
216     } else {
217         /* Only accept input lengths that are a multiple of 10 ms */
218         if( nBlocksOf10ms * encControl->API_sampleRate != 100 * nSamplesIn || nSamplesIn < 0 ) {
219             silk_assert( 0 );
220             RESTORE_STACK;
221             return SILK_ENC_INPUT_INVALID_NO_OF_SAMPLES;
222         }
223         /* Make sure no more than one packet can be produced */
224         if( 1000 * (opus_int32)nSamplesIn > encControl->payloadSize_ms * encControl->API_sampleRate ) {
225             silk_assert( 0 );
226             RESTORE_STACK;
227             return SILK_ENC_INPUT_INVALID_NO_OF_SAMPLES;
228         }
229     }
230
231     TargetRate_bps = silk_RSHIFT32( encControl->bitRate, encControl->nChannelsInternal - 1 );
232     for( n = 0; n < encControl->nChannelsInternal; n++ ) {
233         /* Force the side channel to the same rate as the mid */
234         opus_int force_fs_kHz = (n==1) ? psEnc->state_Fxx[0].sCmn.fs_kHz : 0;
235         if( ( ret = silk_control_encoder( &psEnc->state_Fxx[ n ], encControl, TargetRate_bps, psEnc->allowBandwidthSwitch, n, force_fs_kHz ) ) != 0 ) {
236             silk_assert( 0 );
237             RESTORE_STACK;
238             return ret;
239         }
240         if( psEnc->state_Fxx[n].sCmn.first_frame_after_reset || transition ) {
241             for( i = 0; i < psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket; i++ ) {
242                 psEnc->state_Fxx[ n ].sCmn.LBRR_flags[ i ] = 0;
243             }
244         }
245         psEnc->state_Fxx[ n ].sCmn.inDTX = psEnc->state_Fxx[ n ].sCmn.useDTX;
246     }
247     silk_assert( encControl->nChannelsInternal == 1 || psEnc->state_Fxx[ 0 ].sCmn.fs_kHz == psEnc->state_Fxx[ 1 ].sCmn.fs_kHz );
248
249     /* Input buffering/resampling and encoding */
250     nSamplesToBufferMax =
251         10 * nBlocksOf10ms * psEnc->state_Fxx[ 0 ].sCmn.fs_kHz;
252     nSamplesFromInputMax =
253         silk_DIV32_16( nSamplesToBufferMax *
254                            psEnc->state_Fxx[ 0 ].sCmn.API_fs_Hz,
255                        psEnc->state_Fxx[ 0 ].sCmn.fs_kHz * 1000 );
256     ALLOC( buf, nSamplesFromInputMax, opus_int16 );
257     while( 1 ) {
258         nSamplesToBuffer  = psEnc->state_Fxx[ 0 ].sCmn.frame_length - psEnc->state_Fxx[ 0 ].sCmn.inputBufIx;
259         nSamplesToBuffer  = silk_min( nSamplesToBuffer, nSamplesToBufferMax );
260         nSamplesFromInput = silk_DIV32_16( nSamplesToBuffer * psEnc->state_Fxx[ 0 ].sCmn.API_fs_Hz, psEnc->state_Fxx[ 0 ].sCmn.fs_kHz * 1000 );
261         /* Resample and write to buffer */
262         if( encControl->nChannelsAPI == 2 && encControl->nChannelsInternal == 2 ) {
263             opus_int id = psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded;
264             for( n = 0; n < nSamplesFromInput; n++ ) {
265                 buf[ n ] = samplesIn[ 2 * n ];
266             }
267             /* Making sure to start both resamplers from the same state when switching from mono to stereo */
268             if( psEnc->nPrevChannelsInternal == 1 && id==0 ) {
269                silk_memcpy( &psEnc->state_Fxx[ 1 ].sCmn.resampler_state, &psEnc->state_Fxx[ 0 ].sCmn.resampler_state, sizeof(psEnc->state_Fxx[ 1 ].sCmn.resampler_state));
270             }
271
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;
275
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             for( n = 0; n < nSamplesFromInput; n++ ) {
279                 buf[ n ] = samplesIn[ 2 * n + 1 ];
280             }
281             ret += silk_resampler( &psEnc->state_Fxx[ 1 ].sCmn.resampler_state,
282                 &psEnc->state_Fxx[ 1 ].sCmn.inputBuf[ psEnc->state_Fxx[ 1 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput );
283
284             psEnc->state_Fxx[ 1 ].sCmn.inputBufIx += nSamplesToBuffer;
285         } else if( encControl->nChannelsAPI == 2 && encControl->nChannelsInternal == 1 ) {
286             /* Combine left and right channels before resampling */
287             for( n = 0; n < nSamplesFromInput; n++ ) {
288                 sum = samplesIn[ 2 * n ] + samplesIn[ 2 * n + 1 ];
289                 buf[ n ] = (opus_int16)silk_RSHIFT_ROUND( sum,  1 );
290             }
291             ret += silk_resampler( &psEnc->state_Fxx[ 0 ].sCmn.resampler_state,
292                 &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput );
293             /* On the first mono frame, average the results for the two resampler states  */
294             if( psEnc->nPrevChannelsInternal == 2 && psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded == 0 ) {
295                ret += silk_resampler( &psEnc->state_Fxx[ 1 ].sCmn.resampler_state,
296                    &psEnc->state_Fxx[ 1 ].sCmn.inputBuf[ psEnc->state_Fxx[ 1 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput );
297                for( n = 0; n < psEnc->state_Fxx[ 0 ].sCmn.frame_length; n++ ) {
298                   psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx+n+2 ] =
299                         silk_RSHIFT(psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx+n+2 ]
300                                   + psEnc->state_Fxx[ 1 ].sCmn.inputBuf[ psEnc->state_Fxx[ 1 ].sCmn.inputBufIx+n+2 ], 1);
301                }
302             }
303             psEnc->state_Fxx[ 0 ].sCmn.inputBufIx += nSamplesToBuffer;
304         } else {
305             silk_assert( encControl->nChannelsAPI == 1 && encControl->nChannelsInternal == 1 );
306             silk_memcpy(buf, samplesIn, nSamplesFromInput*sizeof(opus_int16));
307             ret += silk_resampler( &psEnc->state_Fxx[ 0 ].sCmn.resampler_state,
308                 &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput );
309             psEnc->state_Fxx[ 0 ].sCmn.inputBufIx += nSamplesToBuffer;
310         }
311
312         samplesIn  += nSamplesFromInput * encControl->nChannelsAPI;
313         nSamplesIn -= nSamplesFromInput;
314
315         /* Default */
316         psEnc->allowBandwidthSwitch = 0;
317
318         /* Silk encoder */
319         if( psEnc->state_Fxx[ 0 ].sCmn.inputBufIx >= psEnc->state_Fxx[ 0 ].sCmn.frame_length ) {
320             /* Enough data in input buffer, so encode */
321             silk_assert( psEnc->state_Fxx[ 0 ].sCmn.inputBufIx == psEnc->state_Fxx[ 0 ].sCmn.frame_length );
322             silk_assert( encControl->nChannelsInternal == 1 || psEnc->state_Fxx[ 1 ].sCmn.inputBufIx == psEnc->state_Fxx[ 1 ].sCmn.frame_length );
323
324             /* Deal with LBRR data */
325             if( psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded == 0 && !prefillFlag ) {
326                 /* Create space at start of payload for VAD and FEC flags */
327                 opus_uint8 iCDF[ 2 ] = { 0, 0 };
328                 iCDF[ 0 ] = 256 - silk_RSHIFT( 256, ( psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket + 1 ) * encControl->nChannelsInternal );
329                 ec_enc_icdf( psRangeEnc, 0, iCDF, 8 );
330
331                 /* Encode any LBRR data from previous packet */
332                 /* Encode LBRR flags */
333                 for( n = 0; n < encControl->nChannelsInternal; n++ ) {
334                     LBRR_symbol = 0;
335                     for( i = 0; i < psEnc->state_Fxx[ n ].sCmn.nFramesPerPacket; i++ ) {
336                         LBRR_symbol |= silk_LSHIFT( psEnc->state_Fxx[ n ].sCmn.LBRR_flags[ i ], i );
337                     }
338                     psEnc->state_Fxx[ n ].sCmn.LBRR_flag = LBRR_symbol > 0 ? 1 : 0;
339                     if( LBRR_symbol && psEnc->state_Fxx[ n ].sCmn.nFramesPerPacket > 1 ) {
340                         ec_enc_icdf( psRangeEnc, LBRR_symbol - 1, silk_LBRR_flags_iCDF_ptr[ psEnc->state_Fxx[ n ].sCmn.nFramesPerPacket - 2 ], 8 );
341                     }
342                 }
343
344                 /* Code LBRR indices and excitation signals */
345                 for( i = 0; i < psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket; i++ ) {
346                     for( n = 0; n < encControl->nChannelsInternal; n++ ) {
347                         if( psEnc->state_Fxx[ n ].sCmn.LBRR_flags[ i ] ) {
348                             opus_int condCoding;
349
350                             if( encControl->nChannelsInternal == 2 && n == 0 ) {
351                                 silk_stereo_encode_pred( psRangeEnc, psEnc->sStereo.predIx[ i ] );
352                                 /* For LBRR data there's no need to code the mid-only flag if the side-channel LBRR flag is set */
353                                 if( psEnc->state_Fxx[ 1 ].sCmn.LBRR_flags[ i ] == 0 ) {
354                                     silk_stereo_encode_mid_only( psRangeEnc, psEnc->sStereo.mid_only_flags[ i ] );
355                                 }
356                             }
357                             /* Use conditional coding if previous frame available */
358                             if( i > 0 && psEnc->state_Fxx[ n ].sCmn.LBRR_flags[ i - 1 ] ) {
359                                 condCoding = CODE_CONDITIONALLY;
360                             } else {
361                                 condCoding = CODE_INDEPENDENTLY;
362                             }
363                             silk_encode_indices( &psEnc->state_Fxx[ n ].sCmn, psRangeEnc, i, 1, condCoding );
364                             silk_encode_pulses( psRangeEnc, psEnc->state_Fxx[ n ].sCmn.indices_LBRR[i].signalType, psEnc->state_Fxx[ n ].sCmn.indices_LBRR[i].quantOffsetType,
365                                 psEnc->state_Fxx[ n ].sCmn.pulses_LBRR[ i ], psEnc->state_Fxx[ n ].sCmn.frame_length );
366                         }
367                     }
368                 }
369
370                 /* Reset LBRR flags */
371                 for( n = 0; n < encControl->nChannelsInternal; n++ ) {
372                     silk_memset( psEnc->state_Fxx[ n ].sCmn.LBRR_flags, 0, sizeof( psEnc->state_Fxx[ n ].sCmn.LBRR_flags ) );
373                 }
374             }
375
376             silk_HP_variable_cutoff( psEnc->state_Fxx );
377
378             /* Total target bits for packet */
379             nBits = silk_DIV32_16( silk_MUL( encControl->bitRate, encControl->payloadSize_ms ), 1000 );
380             /* Subtract half of the bits already used */
381             if( !prefillFlag ) {
382                 nBits -= ec_tell( psRangeEnc ) >> 1;
383             }
384             /* Divide by number of uncoded frames left in packet */
385             nBits = silk_DIV32_16( nBits, psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket - psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded );
386             /* Convert to bits/second */
387             if( encControl->payloadSize_ms == 10 ) {
388                 TargetRate_bps = silk_SMULBB( nBits, 100 );
389             } else {
390                 TargetRate_bps = silk_SMULBB( nBits, 50 );
391             }
392             /* Subtract fraction of bits in excess of target in previous packets */
393             TargetRate_bps -= silk_DIV32_16( silk_MUL( psEnc->nBitsExceeded, 1000 ), BITRESERVOIR_DECAY_TIME_MS );
394             /* Never exceed input bitrate */
395             TargetRate_bps = silk_LIMIT( TargetRate_bps, encControl->bitRate, 5000 );
396
397             /* Convert Left/Right to Mid/Side */
398             if( encControl->nChannelsInternal == 2 ) {
399                 silk_stereo_LR_to_MS( &psEnc->sStereo, &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ 2 ], &psEnc->state_Fxx[ 1 ].sCmn.inputBuf[ 2 ],
400                     psEnc->sStereo.predIx[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ], &psEnc->sStereo.mid_only_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ],
401                     MStargetRates_bps, TargetRate_bps, psEnc->state_Fxx[ 0 ].sCmn.speech_activity_Q8, encControl->toMono,
402                     psEnc->state_Fxx[ 0 ].sCmn.fs_kHz, psEnc->state_Fxx[ 0 ].sCmn.frame_length );
403                 if( psEnc->sStereo.mid_only_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] == 0 ) {
404                     /* Reset side channel encoder memory for first frame with side coding */
405                     if( psEnc->prev_decode_only_middle == 1 ) {
406                         silk_memset( &psEnc->state_Fxx[ 1 ].sShape,               0, sizeof( psEnc->state_Fxx[ 1 ].sShape ) );
407                         silk_memset( &psEnc->state_Fxx[ 1 ].sPrefilt,             0, sizeof( psEnc->state_Fxx[ 1 ].sPrefilt ) );
408                         silk_memset( &psEnc->state_Fxx[ 1 ].sCmn.sNSQ,            0, sizeof( psEnc->state_Fxx[ 1 ].sCmn.sNSQ ) );
409                         silk_memset( psEnc->state_Fxx[ 1 ].sCmn.prev_NLSFq_Q15,   0, sizeof( psEnc->state_Fxx[ 1 ].sCmn.prev_NLSFq_Q15 ) );
410                         silk_memset( &psEnc->state_Fxx[ 1 ].sCmn.sLP.In_LP_State, 0, sizeof( psEnc->state_Fxx[ 1 ].sCmn.sLP.In_LP_State ) );
411                         psEnc->state_Fxx[ 1 ].sCmn.prevLag                 = 100;
412                         psEnc->state_Fxx[ 1 ].sCmn.sNSQ.lagPrev            = 100;
413                         psEnc->state_Fxx[ 1 ].sShape.LastGainIndex         = 10;
414                         psEnc->state_Fxx[ 1 ].sCmn.prevSignalType          = TYPE_NO_VOICE_ACTIVITY;
415                         psEnc->state_Fxx[ 1 ].sCmn.sNSQ.prev_gain_Q16      = 65536;
416                         psEnc->state_Fxx[ 1 ].sCmn.first_frame_after_reset = 1;
417                     }
418                     silk_encode_do_VAD_Fxx( &psEnc->state_Fxx[ 1 ] );
419                 } else {
420                     psEnc->state_Fxx[ 1 ].sCmn.VAD_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] = 0;
421                 }
422                 if( !prefillFlag ) {
423                     silk_stereo_encode_pred( psRangeEnc, psEnc->sStereo.predIx[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] );
424                     if( psEnc->state_Fxx[ 1 ].sCmn.VAD_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] == 0 ) {
425                         silk_stereo_encode_mid_only( psRangeEnc, psEnc->sStereo.mid_only_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] );
426                     }
427                 }
428             } else {
429                 /* Buffering */
430                 silk_memcpy( psEnc->state_Fxx[ 0 ].sCmn.inputBuf, psEnc->sStereo.sMid, 2 * sizeof( opus_int16 ) );
431                 silk_memcpy( psEnc->sStereo.sMid, &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.frame_length ], 2 * sizeof( opus_int16 ) );
432             }
433             silk_encode_do_VAD_Fxx( &psEnc->state_Fxx[ 0 ] );
434
435             /* Encode */
436             for( n = 0; n < encControl->nChannelsInternal; n++ ) {
437                 opus_int maxBits, useCBR;
438
439                 /* Handling rate constraints */
440                 maxBits = encControl->maxBits;
441                 if( tot_blocks == 2 && curr_block == 0 ) {
442                     maxBits = maxBits * 3 / 5;
443                 } else if( tot_blocks == 3 ) {
444                     if( curr_block == 0 ) {
445                         maxBits = maxBits * 2 / 5;
446                     } else if( curr_block == 1 ) {
447                         maxBits = maxBits * 3 / 4;
448                     }
449                 }
450                 useCBR = encControl->useCBR && curr_block == tot_blocks - 1;
451
452                 if( encControl->nChannelsInternal == 1 ) {
453                     channelRate_bps = TargetRate_bps;
454                 } else {
455                     channelRate_bps = MStargetRates_bps[ n ];
456                     if( n == 0 && MStargetRates_bps[ 1 ] > 0 ) {
457                         useCBR = 0;
458                         /* Give mid up to 1/2 of the max bits for that frame */
459                         maxBits -= encControl->maxBits / ( tot_blocks * 2 );
460                     }
461                 }
462
463                 if( channelRate_bps > 0 ) {
464                     opus_int condCoding;
465
466                     silk_control_SNR( &psEnc->state_Fxx[ n ].sCmn, channelRate_bps );
467
468                     /* Use independent coding if no previous frame available */
469                     if( psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded - n <= 0 ) {
470                         condCoding = CODE_INDEPENDENTLY;
471                     } else if( n > 0 && psEnc->prev_decode_only_middle ) {
472                         /* If we skipped a side frame in this packet, we don't
473                            need LTP scaling; the LTP state is well-defined. */
474                         condCoding = CODE_INDEPENDENTLY_NO_LTP_SCALING;
475                     } else {
476                         condCoding = CODE_CONDITIONALLY;
477                     }
478                     if( ( ret = silk_encode_frame_Fxx( &psEnc->state_Fxx[ n ], nBytesOut, psRangeEnc, condCoding, maxBits, useCBR ) ) != 0 ) {
479                         silk_assert( 0 );
480                     }
481                 }
482                 psEnc->state_Fxx[ n ].sCmn.controlled_since_last_payload = 0;
483                 psEnc->state_Fxx[ n ].sCmn.inputBufIx = 0;
484                 psEnc->state_Fxx[ n ].sCmn.nFramesEncoded++;
485             }
486             psEnc->prev_decode_only_middle = psEnc->sStereo.mid_only_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded - 1 ];
487
488             /* Insert VAD and FEC flags at beginning of bitstream */
489             if( *nBytesOut > 0 && psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded == psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket) {
490                 flags = 0;
491                 for( n = 0; n < encControl->nChannelsInternal; n++ ) {
492                     for( i = 0; i < psEnc->state_Fxx[ n ].sCmn.nFramesPerPacket; i++ ) {
493                         flags  = silk_LSHIFT( flags, 1 );
494                         flags |= psEnc->state_Fxx[ n ].sCmn.VAD_flags[ i ];
495                     }
496                     flags  = silk_LSHIFT( flags, 1 );
497                     flags |= psEnc->state_Fxx[ n ].sCmn.LBRR_flag;
498                 }
499                 if( !prefillFlag ) {
500                     ec_enc_patch_initial_bits( psRangeEnc, flags, ( psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket + 1 ) * encControl->nChannelsInternal );
501                 }
502
503                 /* Return zero bytes if all channels DTXed */
504                 if( psEnc->state_Fxx[ 0 ].sCmn.inDTX && ( encControl->nChannelsInternal == 1 || psEnc->state_Fxx[ 1 ].sCmn.inDTX ) ) {
505                     *nBytesOut = 0;
506                 }
507
508                 psEnc->nBitsExceeded += *nBytesOut * 8;
509                 psEnc->nBitsExceeded -= silk_DIV32_16( silk_MUL( encControl->bitRate, encControl->payloadSize_ms ), 1000 );
510                 psEnc->nBitsExceeded  = silk_LIMIT( psEnc->nBitsExceeded, 0, 10000 );
511
512                 /* Update flag indicating if bandwidth switching is allowed */
513                 speech_act_thr_for_switch_Q8 = silk_SMLAWB( SILK_FIX_CONST( SPEECH_ACTIVITY_DTX_THRES, 8 ),
514                     SILK_FIX_CONST( ( 1 - SPEECH_ACTIVITY_DTX_THRES ) / MAX_BANDWIDTH_SWITCH_DELAY_MS, 16 + 8 ), psEnc->timeSinceSwitchAllowed_ms );
515                 if( psEnc->state_Fxx[ 0 ].sCmn.speech_activity_Q8 < speech_act_thr_for_switch_Q8 ) {
516                     psEnc->allowBandwidthSwitch = 1;
517                     psEnc->timeSinceSwitchAllowed_ms = 0;
518                 } else {
519                     psEnc->allowBandwidthSwitch = 0;
520                     psEnc->timeSinceSwitchAllowed_ms += encControl->payloadSize_ms;
521                 }
522             }
523
524             if( nSamplesIn == 0 ) {
525                 break;
526             }
527         } else {
528             break;
529         }
530         curr_block++;
531     }
532
533     psEnc->nPrevChannelsInternal = encControl->nChannelsInternal;
534
535     encControl->allowBandwidthSwitch = psEnc->allowBandwidthSwitch;
536     encControl->inWBmodeWithoutVariableLP = psEnc->state_Fxx[ 0 ].sCmn.fs_kHz == 16 && psEnc->state_Fxx[ 0 ].sCmn.sLP.mode == 0;
537     encControl->internalSampleRate = silk_SMULBB( psEnc->state_Fxx[ 0 ].sCmn.fs_kHz, 1000 );
538     encControl->stereoWidth_Q14 = encControl->toMono ? 0 : psEnc->sStereo.smth_width_Q14;
539     if( prefillFlag ) {
540         encControl->payloadSize_ms = tmp_payloadSize_ms;
541         encControl->complexity = tmp_complexity;
542         for( n = 0; n < encControl->nChannelsInternal; n++ ) {
543             psEnc->state_Fxx[ n ].sCmn.controlled_since_last_payload = 0;
544             psEnc->state_Fxx[ n ].sCmn.prefillFlag = 0;
545         }
546     }
547
548     RESTORE_STACK;
549     return ret;
550 }
551