1/***********************************************************************
2Copyright (c) 2006-2011, Skype Limited. All rights reserved.
3Redistribution and use in source and binary forms, with or without
4modification, are permitted provided that the following conditions
5are met:
6- Redistributions of source code must retain the above copyright notice,
7this list of conditions and the following disclaimer.
8- Redistributions in binary form must reproduce the above copyright
9notice, this list of conditions and the following disclaimer in the
10documentation and/or other materials provided with the distribution.
11- Neither the name of Internet Society, IETF or IETF Trust, nor the
12names of specific contributors, may be used to endorse or promote
13products derived from this software without specific prior written
14permission.
15THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS”
16AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25POSSIBILITY 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/***************************************/
46static 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
55opus_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/*************************/
69opus_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/***************************************/
101static 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                                                                         */
138opus_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            ret = SILK_ENC_INPUT_INVALID_NO_OF_SAMPLES;
195            silk_assert( 0 );
196            return ret;
197        }
198        /* Reset Encoder */
199        for( n = 0; n < encControl->nChannelsInternal; n++ ) {
200            if( (ret = silk_init_encoder( &psEnc->state_Fxx[ n ] ) ) != 0 ) {
201                silk_assert( 0 );
202            }
203        }
204        tmp_payloadSize_ms = encControl->payloadSize_ms;
205        encControl->payloadSize_ms = 10;
206        tmp_complexity = encControl->complexity;
207        encControl->complexity = 0;
208        for( n = 0; n < encControl->nChannelsInternal; n++ ) {
209            psEnc->state_Fxx[ n ].sCmn.controlled_since_last_payload = 0;
210            psEnc->state_Fxx[ n ].sCmn.prefillFlag = 1;
211        }
212    } else {
213        /* Only accept input lengths that are a multiple of 10 ms */
214        if( nBlocksOf10ms * encControl->API_sampleRate != 100 * nSamplesIn || nSamplesIn < 0 ) {
215            ret = SILK_ENC_INPUT_INVALID_NO_OF_SAMPLES;
216            silk_assert( 0 );
217            return ret;
218        }
219        /* Make sure no more than one packet can be produced */
220        if( 1000 * (opus_int32)nSamplesIn > encControl->payloadSize_ms * encControl->API_sampleRate ) {
221            ret = SILK_ENC_INPUT_INVALID_NO_OF_SAMPLES;
222            silk_assert( 0 );
223            return ret;
224        }
225    }
226
227    TargetRate_bps = silk_RSHIFT32( encControl->bitRate, encControl->nChannelsInternal - 1 );
228    for( n = 0; n < encControl->nChannelsInternal; n++ ) {
229        /* Force the side channel to the same rate as the mid */
230        opus_int force_fs_kHz = (n==1) ? psEnc->state_Fxx[0].sCmn.fs_kHz : 0;
231        if( ( ret = silk_control_encoder( &psEnc->state_Fxx[ n ], encControl, TargetRate_bps, psEnc->allowBandwidthSwitch, n, force_fs_kHz ) ) != 0 ) {
232            silk_assert( 0 );
233            return ret;
234        }
235        if( psEnc->state_Fxx[n].sCmn.first_frame_after_reset || transition ) {
236            for( i = 0; i < psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket; i++ ) {
237                psEnc->state_Fxx[ n ].sCmn.LBRR_flags[ i ] = 0;
238            }
239        }
240        psEnc->state_Fxx[ n ].sCmn.inDTX = psEnc->state_Fxx[ n ].sCmn.useDTX;
241    }
242    silk_assert( encControl->nChannelsInternal == 1 || psEnc->state_Fxx[ 0 ].sCmn.fs_kHz == psEnc->state_Fxx[ 1 ].sCmn.fs_kHz );
243
244    /* Input buffering/resampling and encoding */
245    while( 1 ) {
246        nSamplesToBuffer  = psEnc->state_Fxx[ 0 ].sCmn.frame_length - psEnc->state_Fxx[ 0 ].sCmn.inputBufIx;
247        nSamplesToBuffer  = silk_min( nSamplesToBuffer, 10 * nBlocksOf10ms * psEnc->state_Fxx[ 0 ].sCmn.fs_kHz );
248        nSamplesFromInput = silk_DIV32_16( nSamplesToBuffer * psEnc->state_Fxx[ 0 ].sCmn.API_fs_Hz, psEnc->state_Fxx[ 0 ].sCmn.fs_kHz * 1000 );
249        /* Resample and write to buffer */
250        if( encControl->nChannelsAPI == 2 && encControl->nChannelsInternal == 2 ) {
251            opus_int id = psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded;
252            for( n = 0; n < nSamplesFromInput; n++ ) {
253                buf[ n ] = samplesIn[ 2 * n ];
254            }
255            /* Making sure to start both resamplers from the same state when switching from mono to stereo */
256            if( psEnc->nPrevChannelsInternal == 1 && id==0 ) {
257               silk_memcpy( &psEnc->state_Fxx[ 1 ].sCmn.resampler_state, &psEnc->state_Fxx[ 0 ].sCmn.resampler_state, sizeof(psEnc->state_Fxx[ 1 ].sCmn.resampler_state));
258            }
259
260            ret += silk_resampler( &psEnc->state_Fxx[ 0 ].sCmn.resampler_state,
261                &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput );
262            psEnc->state_Fxx[ 0 ].sCmn.inputBufIx += nSamplesToBuffer;
263
264            nSamplesToBuffer  = psEnc->state_Fxx[ 1 ].sCmn.frame_length - psEnc->state_Fxx[ 1 ].sCmn.inputBufIx;
265            nSamplesToBuffer  = silk_min( nSamplesToBuffer, 10 * nBlocksOf10ms * psEnc->state_Fxx[ 1 ].sCmn.fs_kHz );
266            for( n = 0; n < nSamplesFromInput; n++ ) {
267                buf[ n ] = samplesIn[ 2 * n + 1 ];
268            }
269            ret += silk_resampler( &psEnc->state_Fxx[ 1 ].sCmn.resampler_state,
270                &psEnc->state_Fxx[ 1 ].sCmn.inputBuf[ psEnc->state_Fxx[ 1 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput );
271
272            psEnc->state_Fxx[ 1 ].sCmn.inputBufIx += nSamplesToBuffer;
273        } else if( encControl->nChannelsAPI == 2 && encControl->nChannelsInternal == 1 ) {
274            /* Combine left and right channels before resampling */
275            for( n = 0; n < nSamplesFromInput; n++ ) {
276                sum = samplesIn[ 2 * n ] + samplesIn[ 2 * n + 1 ];
277                buf[ n ] = (opus_int16)silk_RSHIFT_ROUND( sum,  1 );
278            }
279            ret += silk_resampler( &psEnc->state_Fxx[ 0 ].sCmn.resampler_state,
280                &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput );
281            /* On the first mono frame, average the results for the two resampler states  */
282            if( psEnc->nPrevChannelsInternal == 2 && psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded == 0 ) {
283               ret += silk_resampler( &psEnc->state_Fxx[ 1 ].sCmn.resampler_state,
284                   &psEnc->state_Fxx[ 1 ].sCmn.inputBuf[ psEnc->state_Fxx[ 1 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput );
285               for( n = 0; n < psEnc->state_Fxx[ 0 ].sCmn.frame_length; n++ ) {
286                  psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx+n+2 ] =
287                        silk_RSHIFT(psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx+n+2 ]
288                                  + psEnc->state_Fxx[ 1 ].sCmn.inputBuf[ psEnc->state_Fxx[ 1 ].sCmn.inputBufIx+n+2 ], 1);
289               }
290            }
291            psEnc->state_Fxx[ 0 ].sCmn.inputBufIx += nSamplesToBuffer;
292        } else {
293            silk_assert( encControl->nChannelsAPI == 1 && encControl->nChannelsInternal == 1 );
294            silk_memcpy(buf, samplesIn, nSamplesFromInput*sizeof(opus_int16));
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;
298        }
299
300        samplesIn  += nSamplesFromInput * encControl->nChannelsAPI;
301        nSamplesIn -= nSamplesFromInput;
302
303        /* Default */
304        psEnc->allowBandwidthSwitch = 0;
305
306        /* Silk encoder */
307        if( psEnc->state_Fxx[ 0 ].sCmn.inputBufIx >= psEnc->state_Fxx[ 0 ].sCmn.frame_length ) {
308            /* Enough data in input buffer, so encode */
309            silk_assert( psEnc->state_Fxx[ 0 ].sCmn.inputBufIx == psEnc->state_Fxx[ 0 ].sCmn.frame_length );
310            silk_assert( encControl->nChannelsInternal == 1 || psEnc->state_Fxx[ 1 ].sCmn.inputBufIx == psEnc->state_Fxx[ 1 ].sCmn.frame_length );
311
312            /* Deal with LBRR data */
313            if( psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded == 0 && !prefillFlag ) {
314                /* Create space at start of payload for VAD and FEC flags */
315                opus_uint8 iCDF[ 2 ] = { 0, 0 };
316                iCDF[ 0 ] = 256 - silk_RSHIFT( 256, ( psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket + 1 ) * encControl->nChannelsInternal );
317                ec_enc_icdf( psRangeEnc, 0, iCDF, 8 );
318
319                /* Encode any LBRR data from previous packet */
320                /* Encode LBRR flags */
321                for( n = 0; n < encControl->nChannelsInternal; n++ ) {
322                    LBRR_symbol = 0;
323                    for( i = 0; i < psEnc->state_Fxx[ n ].sCmn.nFramesPerPacket; i++ ) {
324                        LBRR_symbol |= silk_LSHIFT( psEnc->state_Fxx[ n ].sCmn.LBRR_flags[ i ], i );
325                    }
326                    psEnc->state_Fxx[ n ].sCmn.LBRR_flag = LBRR_symbol > 0 ? 1 : 0;
327                    if( LBRR_symbol && psEnc->state_Fxx[ n ].sCmn.nFramesPerPacket > 1 ) {
328                        ec_enc_icdf( psRangeEnc, LBRR_symbol - 1, silk_LBRR_flags_iCDF_ptr[ psEnc->state_Fxx[ n ].sCmn.nFramesPerPacket - 2 ], 8 );
329                    }
330                }
331
332                /* Code LBRR indices and excitation signals */
333                for( i = 0; i < psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket; i++ ) {
334                    for( n = 0; n < encControl->nChannelsInternal; n++ ) {
335                        if( psEnc->state_Fxx[ n ].sCmn.LBRR_flags[ i ] ) {
336                            opus_int condCoding;
337
338                            if( encControl->nChannelsInternal == 2 && n == 0 ) {
339                                silk_stereo_encode_pred( psRangeEnc, psEnc->sStereo.predIx[ i ] );
340                                /* For LBRR data there's no need to code the mid-only flag if the side-channel LBRR flag is set */
341                                if( psEnc->state_Fxx[ 1 ].sCmn.LBRR_flags[ i ] == 0 ) {
342                                    silk_stereo_encode_mid_only( psRangeEnc, psEnc->sStereo.mid_only_flags[ i ] );
343                                }
344                            }
345                            /* Use conditional coding if previous frame available */
346                            if( i > 0 && psEnc->state_Fxx[ n ].sCmn.LBRR_flags[ i - 1 ] ) {
347                                condCoding = CODE_CONDITIONALLY;
348                            } else {
349                                condCoding = CODE_INDEPENDENTLY;
350                            }
351                            silk_encode_indices( &psEnc->state_Fxx[ n ].sCmn, psRangeEnc, i, 1, condCoding );
352                            silk_encode_pulses( psRangeEnc, psEnc->state_Fxx[ n ].sCmn.indices_LBRR[i].signalType, psEnc->state_Fxx[ n ].sCmn.indices_LBRR[i].quantOffsetType,
353                                psEnc->state_Fxx[ n ].sCmn.pulses_LBRR[ i ], psEnc->state_Fxx[ n ].sCmn.frame_length );
354                        }
355                    }
356                }
357
358                /* Reset LBRR flags */
359                for( n = 0; n < encControl->nChannelsInternal; n++ ) {
360                    silk_memset( psEnc->state_Fxx[ n ].sCmn.LBRR_flags, 0, sizeof( psEnc->state_Fxx[ n ].sCmn.LBRR_flags ) );
361                }
362            }
363
364            silk_HP_variable_cutoff( psEnc->state_Fxx );
365
366            /* Total target bits for packet */
367            nBits = silk_DIV32_16( silk_MUL( encControl->bitRate, encControl->payloadSize_ms ), 1000 );
368            /* Subtract half of the bits already used */
369            if( !prefillFlag ) {
370                nBits -= ec_tell( psRangeEnc ) >> 1;
371            }
372            /* Divide by number of uncoded frames left in packet */
373            nBits = silk_DIV32_16( nBits, psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket - psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded );
374            /* Convert to bits/second */
375            if( encControl->payloadSize_ms == 10 ) {
376                TargetRate_bps = silk_SMULBB( nBits, 100 );
377            } else {
378                TargetRate_bps = silk_SMULBB( nBits, 50 );
379            }
380            /* Subtract fraction of bits in excess of target in previous packets */
381            TargetRate_bps -= silk_DIV32_16( silk_MUL( psEnc->nBitsExceeded, 1000 ), BITRESERVOIR_DECAY_TIME_MS );
382            /* Never exceed input bitrate */
383            TargetRate_bps = silk_LIMIT( TargetRate_bps, encControl->bitRate, 5000 );
384
385            /* Convert Left/Right to Mid/Side */
386            if( encControl->nChannelsInternal == 2 ) {
387                silk_stereo_LR_to_MS( &psEnc->sStereo, &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ 2 ], &psEnc->state_Fxx[ 1 ].sCmn.inputBuf[ 2 ],
388                    psEnc->sStereo.predIx[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ], &psEnc->sStereo.mid_only_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ],
389                    MStargetRates_bps, TargetRate_bps, psEnc->state_Fxx[ 0 ].sCmn.speech_activity_Q8, encControl->toMono,
390                    psEnc->state_Fxx[ 0 ].sCmn.fs_kHz, psEnc->state_Fxx[ 0 ].sCmn.frame_length );
391                if( psEnc->sStereo.mid_only_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] == 0 ) {
392                    /* Reset side channel encoder memory for first frame with side coding */
393                    if( psEnc->prev_decode_only_middle == 1 ) {
394                        silk_memset( &psEnc->state_Fxx[ 1 ].sShape,               0, sizeof( psEnc->state_Fxx[ 1 ].sShape ) );
395                        silk_memset( &psEnc->state_Fxx[ 1 ].sPrefilt,             0, sizeof( psEnc->state_Fxx[ 1 ].sPrefilt ) );
396                        silk_memset( &psEnc->state_Fxx[ 1 ].sCmn.sNSQ,            0, sizeof( psEnc->state_Fxx[ 1 ].sCmn.sNSQ ) );
397                        silk_memset( psEnc->state_Fxx[ 1 ].sCmn.prev_NLSFq_Q15,   0, sizeof( psEnc->state_Fxx[ 1 ].sCmn.prev_NLSFq_Q15 ) );
398                        silk_memset( &psEnc->state_Fxx[ 1 ].sCmn.sLP.In_LP_State, 0, sizeof( psEnc->state_Fxx[ 1 ].sCmn.sLP.In_LP_State ) );
399                        psEnc->state_Fxx[ 1 ].sCmn.prevLag                 = 100;
400                        psEnc->state_Fxx[ 1 ].sCmn.sNSQ.lagPrev            = 100;
401                        psEnc->state_Fxx[ 1 ].sShape.LastGainIndex         = 10;
402                        psEnc->state_Fxx[ 1 ].sCmn.prevSignalType          = TYPE_NO_VOICE_ACTIVITY;
403                        psEnc->state_Fxx[ 1 ].sCmn.sNSQ.prev_gain_Q16      = 65536;
404                        psEnc->state_Fxx[ 1 ].sCmn.first_frame_after_reset = 1;
405                    }
406                    silk_encode_do_VAD_Fxx( &psEnc->state_Fxx[ 1 ] );
407                } else {
408                    psEnc->state_Fxx[ 1 ].sCmn.VAD_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] = 0;
409                }
410                if( !prefillFlag ) {
411                    silk_stereo_encode_pred( psRangeEnc, psEnc->sStereo.predIx[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] );
412                    if( psEnc->state_Fxx[ 1 ].sCmn.VAD_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] == 0 ) {
413                        silk_stereo_encode_mid_only( psRangeEnc, psEnc->sStereo.mid_only_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] );
414                    }
415                }
416            } else {
417                /* Buffering */
418                silk_memcpy( psEnc->state_Fxx[ 0 ].sCmn.inputBuf, psEnc->sStereo.sMid, 2 * sizeof( opus_int16 ) );
419                silk_memcpy( psEnc->sStereo.sMid, &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.frame_length ], 2 * sizeof( opus_int16 ) );
420            }
421            silk_encode_do_VAD_Fxx( &psEnc->state_Fxx[ 0 ] );
422
423            /* Encode */
424            for( n = 0; n < encControl->nChannelsInternal; n++ ) {
425                opus_int maxBits, useCBR;
426
427                /* Handling rate constraints */
428                maxBits = encControl->maxBits;
429                if( tot_blocks == 2 && curr_block == 0 ) {
430                    maxBits = maxBits * 3 / 5;
431                } else if( tot_blocks == 3 ) {
432                    if( curr_block == 0 ) {
433                        maxBits = maxBits * 2 / 5;
434                    } else if( curr_block == 1 ) {
435                        maxBits = maxBits * 3 / 4;
436                    }
437                }
438                useCBR = encControl->useCBR && curr_block == tot_blocks - 1;
439
440                if( encControl->nChannelsInternal == 1 ) {
441                    channelRate_bps = TargetRate_bps;
442                } else {
443                    channelRate_bps = MStargetRates_bps[ n ];
444                    if( n == 0 && MStargetRates_bps[ 1 ] > 0 ) {
445                        useCBR = 0;
446                        /* Give mid up to 1/2 of the max bits for that frame */
447                        maxBits -= encControl->maxBits / ( tot_blocks * 2 );
448                    }
449                }
450
451                if( channelRate_bps > 0 ) {
452                    opus_int condCoding;
453
454                    silk_control_SNR( &psEnc->state_Fxx[ n ].sCmn, channelRate_bps );
455
456                    /* Use independent coding if no previous frame available */
457                    if( psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded - n <= 0 ) {
458                        condCoding = CODE_INDEPENDENTLY;
459                    } else if( n > 0 && psEnc->prev_decode_only_middle ) {
460                        /* If we skipped a side frame in this packet, we don't
461                           need LTP scaling; the LTP state is well-defined. */
462                        condCoding = CODE_INDEPENDENTLY_NO_LTP_SCALING;
463                    } else {
464                        condCoding = CODE_CONDITIONALLY;
465                    }
466                    if( ( ret = silk_encode_frame_Fxx( &psEnc->state_Fxx[ n ], nBytesOut, psRangeEnc, condCoding, maxBits, useCBR ) ) != 0 ) {
467                        silk_assert( 0 );
468                    }
469                }
470                psEnc->state_Fxx[ n ].sCmn.controlled_since_last_payload = 0;
471                psEnc->state_Fxx[ n ].sCmn.inputBufIx = 0;
472                psEnc->state_Fxx[ n ].sCmn.nFramesEncoded++;
473            }
474            psEnc->prev_decode_only_middle = psEnc->sStereo.mid_only_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded - 1 ];
475
476            /* Insert VAD and FEC flags at beginning of bitstream */
477            if( *nBytesOut > 0 && psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded == psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket) {
478                flags = 0;
479                for( n = 0; n < encControl->nChannelsInternal; n++ ) {
480                    for( i = 0; i < psEnc->state_Fxx[ n ].sCmn.nFramesPerPacket; i++ ) {
481                        flags  = silk_LSHIFT( flags, 1 );
482                        flags |= psEnc->state_Fxx[ n ].sCmn.VAD_flags[ i ];
483                    }
484                    flags  = silk_LSHIFT( flags, 1 );
485                    flags |= psEnc->state_Fxx[ n ].sCmn.LBRR_flag;
486                }
487                if( !prefillFlag ) {
488                    ec_enc_patch_initial_bits( psRangeEnc, flags, ( psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket + 1 ) * encControl->nChannelsInternal );
489                }
490
491                /* Return zero bytes if all channels DTXed */
492                if( psEnc->state_Fxx[ 0 ].sCmn.inDTX && ( encControl->nChannelsInternal == 1 || psEnc->state_Fxx[ 1 ].sCmn.inDTX ) ) {
493                    *nBytesOut = 0;
494                }
495
496                psEnc->nBitsExceeded += *nBytesOut * 8;
497                psEnc->nBitsExceeded -= silk_DIV32_16( silk_MUL( encControl->bitRate, encControl->payloadSize_ms ), 1000 );
498                psEnc->nBitsExceeded  = silk_LIMIT( psEnc->nBitsExceeded, 0, 10000 );
499
500                /* Update flag indicating if bandwidth switching is allowed */
501                speech_act_thr_for_switch_Q8 = silk_SMLAWB( SILK_FIX_CONST( SPEECH_ACTIVITY_DTX_THRES, 8 ),
502                    SILK_FIX_CONST( ( 1 - SPEECH_ACTIVITY_DTX_THRES ) / MAX_BANDWIDTH_SWITCH_DELAY_MS, 16 + 8 ), psEnc->timeSinceSwitchAllowed_ms );
503                if( psEnc->state_Fxx[ 0 ].sCmn.speech_activity_Q8 < speech_act_thr_for_switch_Q8 ) {
504                    psEnc->allowBandwidthSwitch = 1;
505                    psEnc->timeSinceSwitchAllowed_ms = 0;
506                } else {
507                    psEnc->allowBandwidthSwitch = 0;
508                    psEnc->timeSinceSwitchAllowed_ms += encControl->payloadSize_ms;
509                }
510            }
511
512            if( nSamplesIn == 0 ) {
513                break;
514            }
515        } else {
516            break;
517        }
518        curr_block++;
519    }
520
521    psEnc->nPrevChannelsInternal = encControl->nChannelsInternal;
522
523    encControl->allowBandwidthSwitch = psEnc->allowBandwidthSwitch;
524    encControl->inWBmodeWithoutVariableLP = psEnc->state_Fxx[ 0 ].sCmn.fs_kHz == 16 && psEnc->state_Fxx[ 0 ].sCmn.sLP.mode == 0;
525    encControl->internalSampleRate = silk_SMULBB( psEnc->state_Fxx[ 0 ].sCmn.fs_kHz, 1000 );
526    encControl->stereoWidth_Q14 = encControl->toMono ? 0 : psEnc->sStereo.smth_width_Q14;
527    if( prefillFlag ) {
528        encControl->payloadSize_ms = tmp_payloadSize_ms;
529        encControl->complexity = tmp_complexity;
530        for( n = 0; n < encControl->nChannelsInternal; n++ ) {
531            psEnc->state_Fxx[ n ].sCmn.controlled_since_last_payload = 0;
532            psEnc->state_Fxx[ n ].sCmn.prefillFlag = 0;
533        }
534    }
535
536    return ret;
537}
538
539