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