Coverage Report

Created: 2026-09-01 07:47

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opus/silk/enc_API.c
Line
Count
Source
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
#ifdef ENABLE_DRED
45
#include "dred_encoder.h"
46
#endif
47
48
/***************************************/
49
/* Read control structure from encoder */
50
/***************************************/
51
static opus_int silk_QueryEncoder(                      /* O    Returns error code                              */
52
    const void                      *encState,          /* I    State                                           */
53
    silk_EncControlStruct           *encStatus          /* O    Encoder Status                                  */
54
);
55
56
/****************************************/
57
/* Encoder functions                    */
58
/****************************************/
59
60
opus_int silk_Get_Encoder_Size(                         /* O    Returns error code                              */
61
    opus_int                        *encSizeBytes,      /* O    Number of bytes in SILK encoder state           */
62
    opus_int                         channels           /* I    Number of channels                              */
63
)
64
2.27M
{
65
2.27M
    opus_int ret = SILK_NO_ERROR;
66
67
2.27M
    *encSizeBytes = sizeof( silk_encoder );
68
    /* Skip second encoder state for mono. */
69
2.27M
    if ( channels == 1 ) {
70
1.19M
        *encSizeBytes -= sizeof( silk_encoder_state_Fxx );
71
1.19M
    }
72
73
2.27M
    return ret;
74
2.27M
}
75
76
/*************************/
77
/* Init or Reset encoder */
78
/*************************/
79
opus_int silk_InitEncoder(                              /* O    Returns error code                              */
80
    void                            *encState,          /* I/O  State                                           */
81
    int                              channels,          /* I    Number of channels                              */
82
    int                              arch,              /* I    Run-time architecture                           */
83
    silk_EncControlStruct           *encStatus          /* O    Encoder Status                                  */
84
)
85
441k
{
86
441k
    silk_encoder *psEnc;
87
441k
    opus_int n, ret = SILK_NO_ERROR;
88
89
441k
    psEnc = (silk_encoder *)encState;
90
91
    /* Reset encoder. Skip second encoder state for mono. */
92
441k
    silk_memset( psEnc, 0, sizeof( silk_encoder ) - (channels==1)*sizeof( silk_encoder_state_Fxx ) );
93
1.04M
    for( n = 0; n < channels; n++ ) {
94
607k
        if( ret += silk_init_encoder( &psEnc->state_Fxx[ n ], arch ) ) {
95
0
            celt_assert( 0 );
96
0
        }
97
607k
    }
98
99
441k
    psEnc->nChannelsAPI = 1;
100
441k
    psEnc->nChannelsInternal = 1;
101
102
    /* Read control structure */
103
441k
    if( ret += silk_QueryEncoder( encState, encStatus ) ) {
104
0
        celt_assert( 0 );
105
0
    }
106
107
441k
    return ret;
108
441k
}
109
110
/***************************************/
111
/* Read control structure from encoder */
112
/***************************************/
113
static opus_int silk_QueryEncoder(                      /* O    Returns error code                              */
114
    const void                      *encState,          /* I    State                                           */
115
    silk_EncControlStruct           *encStatus          /* O    Encoder Status                                  */
116
)
117
883k
{
118
883k
    opus_int ret = SILK_NO_ERROR;
119
883k
    silk_encoder_state_Fxx *state_Fxx;
120
883k
    silk_encoder *psEnc = (silk_encoder *)encState;
121
122
883k
    state_Fxx = psEnc->state_Fxx;
123
124
883k
    encStatus->nChannelsAPI              = psEnc->nChannelsAPI;
125
883k
    encStatus->nChannelsInternal         = psEnc->nChannelsInternal;
126
883k
    encStatus->API_sampleRate            = state_Fxx[ 0 ].sCmn.API_fs_Hz;
127
883k
    encStatus->maxInternalSampleRate     = state_Fxx[ 0 ].sCmn.maxInternal_fs_Hz;
128
883k
    encStatus->minInternalSampleRate     = state_Fxx[ 0 ].sCmn.minInternal_fs_Hz;
129
883k
    encStatus->desiredInternalSampleRate = state_Fxx[ 0 ].sCmn.desiredInternal_fs_Hz;
130
883k
    encStatus->payloadSize_ms            = state_Fxx[ 0 ].sCmn.PacketSize_ms;
131
883k
    encStatus->bitRate                   = state_Fxx[ 0 ].sCmn.TargetRate_bps;
132
883k
    encStatus->packetLossPercentage      = state_Fxx[ 0 ].sCmn.PacketLoss_perc;
133
883k
    encStatus->complexity                = state_Fxx[ 0 ].sCmn.Complexity;
134
883k
    encStatus->useInBandFEC              = state_Fxx[ 0 ].sCmn.useInBandFEC;
135
883k
    encStatus->useDTX                    = state_Fxx[ 0 ].sCmn.useDTX;
136
883k
    encStatus->useCBR                    = state_Fxx[ 0 ].sCmn.useCBR;
137
883k
    encStatus->internalSampleRate        = silk_SMULBB( state_Fxx[ 0 ].sCmn.fs_kHz, 1000 );
138
883k
    encStatus->allowBandwidthSwitch      = state_Fxx[ 0 ].sCmn.allow_bandwidth_switch;
139
883k
    encStatus->inWBmodeWithoutVariableLP = state_Fxx[ 0 ].sCmn.fs_kHz == 16 && state_Fxx[ 0 ].sCmn.sLP.mode == 0;
140
141
883k
    return ret;
142
883k
}
enc_API.c:silk_QueryEncoder
Line
Count
Source
117
441k
{
118
441k
    opus_int ret = SILK_NO_ERROR;
119
441k
    silk_encoder_state_Fxx *state_Fxx;
120
441k
    silk_encoder *psEnc = (silk_encoder *)encState;
121
122
441k
    state_Fxx = psEnc->state_Fxx;
123
124
441k
    encStatus->nChannelsAPI              = psEnc->nChannelsAPI;
125
441k
    encStatus->nChannelsInternal         = psEnc->nChannelsInternal;
126
441k
    encStatus->API_sampleRate            = state_Fxx[ 0 ].sCmn.API_fs_Hz;
127
441k
    encStatus->maxInternalSampleRate     = state_Fxx[ 0 ].sCmn.maxInternal_fs_Hz;
128
441k
    encStatus->minInternalSampleRate     = state_Fxx[ 0 ].sCmn.minInternal_fs_Hz;
129
441k
    encStatus->desiredInternalSampleRate = state_Fxx[ 0 ].sCmn.desiredInternal_fs_Hz;
130
441k
    encStatus->payloadSize_ms            = state_Fxx[ 0 ].sCmn.PacketSize_ms;
131
441k
    encStatus->bitRate                   = state_Fxx[ 0 ].sCmn.TargetRate_bps;
132
441k
    encStatus->packetLossPercentage      = state_Fxx[ 0 ].sCmn.PacketLoss_perc;
133
441k
    encStatus->complexity                = state_Fxx[ 0 ].sCmn.Complexity;
134
441k
    encStatus->useInBandFEC              = state_Fxx[ 0 ].sCmn.useInBandFEC;
135
441k
    encStatus->useDTX                    = state_Fxx[ 0 ].sCmn.useDTX;
136
441k
    encStatus->useCBR                    = state_Fxx[ 0 ].sCmn.useCBR;
137
441k
    encStatus->internalSampleRate        = silk_SMULBB( state_Fxx[ 0 ].sCmn.fs_kHz, 1000 );
138
441k
    encStatus->allowBandwidthSwitch      = state_Fxx[ 0 ].sCmn.allow_bandwidth_switch;
139
441k
    encStatus->inWBmodeWithoutVariableLP = state_Fxx[ 0 ].sCmn.fs_kHz == 16 && state_Fxx[ 0 ].sCmn.sLP.mode == 0;
140
141
441k
    return ret;
142
441k
}
enc_API.c:silk_QueryEncoder
Line
Count
Source
117
441k
{
118
441k
    opus_int ret = SILK_NO_ERROR;
119
441k
    silk_encoder_state_Fxx *state_Fxx;
120
441k
    silk_encoder *psEnc = (silk_encoder *)encState;
121
122
441k
    state_Fxx = psEnc->state_Fxx;
123
124
441k
    encStatus->nChannelsAPI              = psEnc->nChannelsAPI;
125
441k
    encStatus->nChannelsInternal         = psEnc->nChannelsInternal;
126
441k
    encStatus->API_sampleRate            = state_Fxx[ 0 ].sCmn.API_fs_Hz;
127
441k
    encStatus->maxInternalSampleRate     = state_Fxx[ 0 ].sCmn.maxInternal_fs_Hz;
128
441k
    encStatus->minInternalSampleRate     = state_Fxx[ 0 ].sCmn.minInternal_fs_Hz;
129
441k
    encStatus->desiredInternalSampleRate = state_Fxx[ 0 ].sCmn.desiredInternal_fs_Hz;
130
441k
    encStatus->payloadSize_ms            = state_Fxx[ 0 ].sCmn.PacketSize_ms;
131
441k
    encStatus->bitRate                   = state_Fxx[ 0 ].sCmn.TargetRate_bps;
132
441k
    encStatus->packetLossPercentage      = state_Fxx[ 0 ].sCmn.PacketLoss_perc;
133
441k
    encStatus->complexity                = state_Fxx[ 0 ].sCmn.Complexity;
134
441k
    encStatus->useInBandFEC              = state_Fxx[ 0 ].sCmn.useInBandFEC;
135
441k
    encStatus->useDTX                    = state_Fxx[ 0 ].sCmn.useDTX;
136
441k
    encStatus->useCBR                    = state_Fxx[ 0 ].sCmn.useCBR;
137
441k
    encStatus->internalSampleRate        = silk_SMULBB( state_Fxx[ 0 ].sCmn.fs_kHz, 1000 );
138
441k
    encStatus->allowBandwidthSwitch      = state_Fxx[ 0 ].sCmn.allow_bandwidth_switch;
139
441k
    encStatus->inWBmodeWithoutVariableLP = state_Fxx[ 0 ].sCmn.fs_kHz == 16 && state_Fxx[ 0 ].sCmn.sLP.mode == 0;
140
141
441k
    return ret;
142
441k
}
143
144
145
/**************************/
146
/* Encode frame with Silk */
147
/**************************/
148
/* Note: if prefillFlag is set, the input must contain 10 ms of audio, irrespective of what                     */
149
/* encControl->payloadSize_ms is set to                                                                         */
150
opus_int silk_Encode(                                   /* O    Returns error code                              */
151
    void                            *encState,          /* I/O  State                                           */
152
    silk_EncControlStruct           *encControl,        /* I    Control status                                  */
153
    const opus_res                  *samplesIn,         /* I    Speech sample input vector                      */
154
    opus_int                        nSamplesIn,         /* I    Number of samples in input vector               */
155
    ec_enc                          *psRangeEnc,        /* I/O  Compressor data structure                       */
156
    opus_int32                      *nBytesOut,         /* I/O  Number of bytes in payload (input: Max bytes)   */
157
    const opus_int                  prefillFlag,        /* I    Flag to indicate prefilling buffers no coding   */
158
    opus_int                        activity            /* I    Decision of Opus voice activity detector        */
159
)
160
95.8M
{
161
95.8M
    opus_int   n, i, nBits, flags, tmp_payloadSize_ms = 0, tmp_complexity = 0, ret = 0;
162
95.8M
    opus_int   nSamplesToBuffer, nSamplesToBufferMax, nBlocksOf10ms;
163
95.8M
    opus_int   nSamplesFromInput = 0, nSamplesFromInputMax;
164
95.8M
    opus_int   speech_act_thr_for_switch_Q8;
165
95.8M
    opus_int32 TargetRate_bps, MStargetRates_bps[ 2 ], channelRate_bps, LBRR_symbol, sum;
166
95.8M
    silk_encoder *psEnc = ( silk_encoder * )encState;
167
95.8M
    VARDECL( opus_int16, buf );
168
95.8M
    opus_int transition, curr_block, tot_blocks;
169
95.8M
    SAVE_STACK;
170
171
95.8M
    celt_assert( encControl->nChannelsAPI >= encControl->nChannelsInternal && encControl->nChannelsAPI >= psEnc->nChannelsInternal );
172
95.8M
    if (encControl->reducedDependency)
173
29.5M
    {
174
72.2M
       for( n = 0; n < encControl->nChannelsAPI; n++ ) {
175
42.6M
           psEnc->state_Fxx[ n ].sCmn.first_frame_after_reset = 1;
176
42.6M
       }
177
29.5M
    }
178
224M
    for( n = 0; n < encControl->nChannelsAPI; n++ ) {
179
128M
        psEnc->state_Fxx[ n ].sCmn.nFramesEncoded = 0;
180
128M
    }
181
    /* Check values in encoder control structure */
182
95.8M
    if( ( ret = check_control_input( encControl ) ) != 0 ) {
183
0
        celt_assert( 0 );
184
0
        RESTORE_STACK;
185
0
        return ret;
186
0
    }
187
188
95.8M
    encControl->switchReady = 0;
189
190
95.8M
    if( encControl->nChannelsInternal > psEnc->nChannelsInternal ) {
191
        /* Mono -> Stereo transition: init state of second channel and stereo state */
192
107k
        ret += silk_init_encoder( &psEnc->state_Fxx[ 1 ], psEnc->state_Fxx[ 0 ].sCmn.arch );
193
107k
        silk_memset( psEnc->sStereo.pred_prev_Q13, 0, sizeof( psEnc->sStereo.pred_prev_Q13 ) );
194
107k
        silk_memset( psEnc->sStereo.sSide, 0, sizeof( psEnc->sStereo.sSide ) );
195
107k
        psEnc->sStereo.mid_side_amp_Q0[ 0 ] = 0;
196
107k
        psEnc->sStereo.mid_side_amp_Q0[ 1 ] = 1;
197
107k
        psEnc->sStereo.mid_side_amp_Q0[ 2 ] = 0;
198
107k
        psEnc->sStereo.mid_side_amp_Q0[ 3 ] = 1;
199
107k
        psEnc->sStereo.width_prev_Q14 = 0;
200
107k
        psEnc->sStereo.smth_width_Q14 = SILK_FIX_CONST( 1, 14 );
201
107k
        if( psEnc->nChannelsAPI == 2 ) {
202
0
            silk_memcpy( &psEnc->state_Fxx[ 1 ].sCmn.resampler_state, &psEnc->state_Fxx[ 0 ].sCmn.resampler_state, sizeof( silk_resampler_state_struct ) );
203
0
            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 ) );
204
0
        }
205
107k
    }
206
207
95.8M
    transition = (encControl->payloadSize_ms != psEnc->state_Fxx[ 0 ].sCmn.PacketSize_ms) || (psEnc->nChannelsInternal != encControl->nChannelsInternal);
208
209
95.8M
    psEnc->nChannelsAPI = encControl->nChannelsAPI;
210
95.8M
    psEnc->nChannelsInternal = encControl->nChannelsInternal;
211
212
95.8M
    nBlocksOf10ms = silk_DIV32( 100 * nSamplesIn, encControl->API_sampleRate );
213
95.8M
    tot_blocks = ( nBlocksOf10ms > 1 ) ? nBlocksOf10ms >> 1 : 1;
214
95.8M
    curr_block = 0;
215
95.8M
    if( prefillFlag ) {
216
2.21k
        silk_LP_state save_LP;
217
        /* Only accept input length of 10 ms */
218
2.21k
        if( nBlocksOf10ms != 1 ) {
219
0
            celt_assert( 0 );
220
0
            RESTORE_STACK;
221
0
            return SILK_ENC_INPUT_INVALID_NO_OF_SAMPLES;
222
0
        }
223
2.21k
        if ( prefillFlag == 2 ) {
224
696
            save_LP = psEnc->state_Fxx[ 0 ].sCmn.sLP;
225
            /* Save the sampling rate so the bandwidth switching code can keep handling transitions. */
226
696
            save_LP.saved_fs_kHz = psEnc->state_Fxx[ 0 ].sCmn.fs_kHz;
227
696
        }
228
        /* Reset Encoder */
229
4.87k
        for( n = 0; n < encControl->nChannelsInternal; n++ ) {
230
2.66k
            ret = silk_init_encoder( &psEnc->state_Fxx[ n ], psEnc->state_Fxx[ n ].sCmn.arch );
231
            /* Restore the variable LP state. */
232
2.66k
            if ( prefillFlag == 2 ) {
233
822
                psEnc->state_Fxx[ n ].sCmn.sLP = save_LP;
234
822
            }
235
2.66k
            celt_assert( !ret );
236
2.66k
        }
237
2.21k
        tmp_payloadSize_ms = encControl->payloadSize_ms;
238
2.21k
        encControl->payloadSize_ms = 10;
239
2.21k
        tmp_complexity = encControl->complexity;
240
2.21k
        encControl->complexity = 0;
241
4.87k
        for( n = 0; n < encControl->nChannelsInternal; n++ ) {
242
2.66k
            psEnc->state_Fxx[ n ].sCmn.controlled_since_last_payload = 0;
243
2.66k
            psEnc->state_Fxx[ n ].sCmn.prefillFlag = 1;
244
2.66k
        }
245
95.8M
    } else {
246
        /* Only accept input lengths that are a multiple of 10 ms */
247
95.8M
        if( nBlocksOf10ms * encControl->API_sampleRate != 100 * nSamplesIn || nSamplesIn < 0 ) {
248
0
            celt_assert( 0 );
249
0
            RESTORE_STACK;
250
0
            return SILK_ENC_INPUT_INVALID_NO_OF_SAMPLES;
251
0
        }
252
        /* Make sure no more than one packet can be produced */
253
95.8M
        if( 1000 * (opus_int32)nSamplesIn > encControl->payloadSize_ms * encControl->API_sampleRate ) {
254
0
            celt_assert( 0 );
255
0
            RESTORE_STACK;
256
0
            return SILK_ENC_INPUT_INVALID_NO_OF_SAMPLES;
257
0
        }
258
95.8M
    }
259
260
213M
    for( n = 0; n < encControl->nChannelsInternal; n++ ) {
261
        /* Force the side channel to the same rate as the mid */
262
118M
        opus_int force_fs_kHz = (n==1) ? psEnc->state_Fxx[0].sCmn.fs_kHz : 0;
263
118M
        if( ( ret = silk_control_encoder( &psEnc->state_Fxx[ n ], encControl, psEnc->allowBandwidthSwitch, n, force_fs_kHz ) ) != 0 ) {
264
0
            silk_assert( 0 );
265
0
            RESTORE_STACK;
266
0
            return ret;
267
0
        }
268
118M
        if( psEnc->state_Fxx[n].sCmn.first_frame_after_reset || transition ) {
269
89.7M
            for( i = 0; i < psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket; i++ ) {
270
49.3M
                psEnc->state_Fxx[ n ].sCmn.LBRR_flags[ i ] = 0;
271
49.3M
            }
272
40.3M
        }
273
118M
        psEnc->state_Fxx[ n ].sCmn.inDTX = psEnc->state_Fxx[ n ].sCmn.useDTX;
274
118M
    }
275
95.8M
    celt_assert( encControl->nChannelsInternal == 1 || psEnc->state_Fxx[ 0 ].sCmn.fs_kHz == psEnc->state_Fxx[ 1 ].sCmn.fs_kHz );
276
277
    /* Input buffering/resampling and encoding */
278
95.8M
    nSamplesToBufferMax =
279
95.8M
        10 * nBlocksOf10ms * psEnc->state_Fxx[ 0 ].sCmn.fs_kHz;
280
95.8M
    nSamplesFromInputMax =
281
95.8M
        silk_DIV32_16( nSamplesToBufferMax *
282
95.8M
                           psEnc->state_Fxx[ 0 ].sCmn.API_fs_Hz,
283
95.8M
                       psEnc->state_Fxx[ 0 ].sCmn.fs_kHz * 1000 );
284
95.8M
    ALLOC( buf, nSamplesFromInputMax, opus_int16 );
285
115M
    while( 1 ) {
286
115M
        int curr_nBitsUsedLBRR = 0;
287
115M
        nSamplesToBuffer  = psEnc->state_Fxx[ 0 ].sCmn.frame_length - psEnc->state_Fxx[ 0 ].sCmn.inputBufIx;
288
115M
        nSamplesToBuffer  = silk_min( nSamplesToBuffer, nSamplesToBufferMax );
289
115M
        nSamplesFromInput = silk_DIV32_16( nSamplesToBuffer * psEnc->state_Fxx[ 0 ].sCmn.API_fs_Hz, psEnc->state_Fxx[ 0 ].sCmn.fs_kHz * 1000 );
290
        /* Resample and write to buffer */
291
115M
        if( encControl->nChannelsAPI == 2 && encControl->nChannelsInternal == 2 ) {
292
26.6M
            opus_int id = psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded;
293
8.45G
            for( n = 0; n < nSamplesFromInput; n++ ) {
294
8.43G
                buf[ n ] = RES2INT16(samplesIn[ 2 * n ]);
295
8.43G
            }
296
            /* Making sure to start both resamplers from the same state when switching from mono to stereo */
297
26.6M
            if( psEnc->nPrevChannelsInternal == 1 && id==0 ) {
298
0
               silk_memcpy( &psEnc->state_Fxx[ 1 ].sCmn.resampler_state, &psEnc->state_Fxx[ 0 ].sCmn.resampler_state, sizeof(psEnc->state_Fxx[ 1 ].sCmn.resampler_state));
299
0
            }
300
301
26.6M
            ret += silk_resampler( &psEnc->state_Fxx[ 0 ].sCmn.resampler_state,
302
26.6M
                &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput );
303
26.6M
            psEnc->state_Fxx[ 0 ].sCmn.inputBufIx += nSamplesToBuffer;
304
305
26.6M
            nSamplesToBuffer  = psEnc->state_Fxx[ 1 ].sCmn.frame_length - psEnc->state_Fxx[ 1 ].sCmn.inputBufIx;
306
26.6M
            nSamplesToBuffer  = silk_min( nSamplesToBuffer, 10 * nBlocksOf10ms * psEnc->state_Fxx[ 1 ].sCmn.fs_kHz );
307
8.45G
            for( n = 0; n < nSamplesFromInput; n++ ) {
308
8.43G
                buf[ n ] = RES2INT16(samplesIn[ 2 * n + 1 ]);
309
8.43G
            }
310
26.6M
            ret += silk_resampler( &psEnc->state_Fxx[ 1 ].sCmn.resampler_state,
311
26.6M
                &psEnc->state_Fxx[ 1 ].sCmn.inputBuf[ psEnc->state_Fxx[ 1 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput );
312
313
26.6M
            psEnc->state_Fxx[ 1 ].sCmn.inputBufIx += nSamplesToBuffer;
314
89.0M
        } else if( encControl->nChannelsAPI == 2 && encControl->nChannelsInternal == 1 ) {
315
            /* Combine left and right channels before resampling */
316
3.42G
            for( n = 0; n < nSamplesFromInput; n++ ) {
317
3.41G
                sum = RES2INT16(samplesIn[ 2 * n ] + samplesIn[ 2 * n + 1 ]);
318
3.41G
                buf[ n ] = (opus_int16)silk_RSHIFT_ROUND( sum,  1 );
319
3.41G
            }
320
12.2M
            ret += silk_resampler( &psEnc->state_Fxx[ 0 ].sCmn.resampler_state,
321
12.2M
                &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput );
322
            /* On the first mono frame, average the results for the two resampler states  */
323
12.2M
            if( psEnc->nPrevChannelsInternal == 2 && psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded == 0 ) {
324
0
               ret += silk_resampler( &psEnc->state_Fxx[ 1 ].sCmn.resampler_state,
325
0
                   &psEnc->state_Fxx[ 1 ].sCmn.inputBuf[ psEnc->state_Fxx[ 1 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput );
326
0
               for( n = 0; n < psEnc->state_Fxx[ 0 ].sCmn.frame_length; n++ ) {
327
0
                  psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx+n+2 ] =
328
0
                        silk_RSHIFT(psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx+n+2 ]
329
0
                                  + psEnc->state_Fxx[ 1 ].sCmn.inputBuf[ psEnc->state_Fxx[ 1 ].sCmn.inputBufIx+n+2 ], 1);
330
0
               }
331
0
            }
332
12.2M
            psEnc->state_Fxx[ 0 ].sCmn.inputBufIx += nSamplesToBuffer;
333
76.7M
        } else {
334
76.7M
            celt_assert( encControl->nChannelsAPI == 1 && encControl->nChannelsInternal == 1 );
335
23.4G
            for( n = 0; n < nSamplesFromInput; n++ ) {
336
23.3G
                buf[n] = RES2INT16(samplesIn[n]);
337
23.3G
            }
338
76.7M
            ret += silk_resampler( &psEnc->state_Fxx[ 0 ].sCmn.resampler_state,
339
76.7M
                &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput );
340
76.7M
            psEnc->state_Fxx[ 0 ].sCmn.inputBufIx += nSamplesToBuffer;
341
76.7M
        }
342
343
115M
        samplesIn  += nSamplesFromInput * encControl->nChannelsAPI;
344
115M
        nSamplesIn -= nSamplesFromInput;
345
346
        /* Default */
347
115M
        psEnc->allowBandwidthSwitch = 0;
348
349
        /* Silk encoder */
350
115M
        if( psEnc->state_Fxx[ 0 ].sCmn.inputBufIx >= psEnc->state_Fxx[ 0 ].sCmn.frame_length ) {
351
            /* Enough data in input buffer, so encode */
352
115M
            celt_assert( psEnc->state_Fxx[ 0 ].sCmn.inputBufIx == psEnc->state_Fxx[ 0 ].sCmn.frame_length );
353
115M
            celt_assert( encControl->nChannelsInternal == 1 || psEnc->state_Fxx[ 1 ].sCmn.inputBufIx == psEnc->state_Fxx[ 1 ].sCmn.frame_length );
354
355
            /* Deal with LBRR data */
356
115M
            if( psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded == 0 && !prefillFlag ) {
357
                /* Create space at start of payload for VAD and FEC flags */
358
95.8M
                opus_uint8 iCDF[ 2 ] = { 0, 0 };
359
95.8M
                iCDF[ 0 ] = 256 - silk_RSHIFT( 256, ( psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket + 1 ) * encControl->nChannelsInternal );
360
95.8M
                ec_enc_icdf( psRangeEnc, 0, iCDF, 8 );
361
95.8M
                curr_nBitsUsedLBRR = ec_tell( psRangeEnc );
362
363
                /* Encode any LBRR data from previous packet */
364
                /* Encode LBRR flags */
365
213M
                for( n = 0; n < encControl->nChannelsInternal; n++ ) {
366
117M
                    LBRR_symbol = 0;
367
260M
                    for( i = 0; i < psEnc->state_Fxx[ n ].sCmn.nFramesPerPacket; i++ ) {
368
142M
                        LBRR_symbol |= silk_LSHIFT( psEnc->state_Fxx[ n ].sCmn.LBRR_flags[ i ], i );
369
142M
                    }
370
117M
                    psEnc->state_Fxx[ n ].sCmn.LBRR_flag = LBRR_symbol > 0 ? 1 : 0;
371
117M
                    if( LBRR_symbol && psEnc->state_Fxx[ n ].sCmn.nFramesPerPacket > 1 ) {
372
103k
                        ec_enc_icdf( psRangeEnc, LBRR_symbol - 1, silk_LBRR_flags_iCDF_ptr[ psEnc->state_Fxx[ n ].sCmn.nFramesPerPacket - 2 ], 8 );
373
103k
                    }
374
117M
                }
375
376
                /* Code LBRR indices and excitation signals */
377
211M
                for( i = 0; i < psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket; i++ ) {
378
257M
                    for( n = 0; n < encControl->nChannelsInternal; n++ ) {
379
142M
                        if( psEnc->state_Fxx[ n ].sCmn.LBRR_flags[ i ] ) {
380
645k
                            opus_int condCoding;
381
382
645k
                            if( encControl->nChannelsInternal == 2 && n == 0 ) {
383
184k
                                silk_stereo_encode_pred( psRangeEnc, psEnc->sStereo.predIx[ i ] );
384
                                /* For LBRR data there's no need to code the mid-only flag if the side-channel LBRR flag is set */
385
184k
                                if( psEnc->state_Fxx[ 1 ].sCmn.LBRR_flags[ i ] == 0 ) {
386
72.7k
                                    silk_stereo_encode_mid_only( psRangeEnc, psEnc->sStereo.mid_only_flags[ i ] );
387
72.7k
                                }
388
184k
                            }
389
                            /* Use conditional coding if previous frame available */
390
645k
                            if( i > 0 && psEnc->state_Fxx[ n ].sCmn.LBRR_flags[ i - 1 ] ) {
391
125k
                                condCoding = CODE_CONDITIONALLY;
392
519k
                            } else {
393
519k
                                condCoding = CODE_INDEPENDENTLY;
394
519k
                            }
395
645k
                            silk_encode_indices( &psEnc->state_Fxx[ n ].sCmn, psRangeEnc, i, 1, condCoding );
396
645k
                            silk_encode_pulses( psRangeEnc, psEnc->state_Fxx[ n ].sCmn.indices_LBRR[i].signalType, psEnc->state_Fxx[ n ].sCmn.indices_LBRR[i].quantOffsetType,
397
645k
                                psEnc->state_Fxx[ n ].sCmn.pulses_LBRR[ i ], psEnc->state_Fxx[ n ].sCmn.frame_length );
398
645k
                        }
399
142M
                    }
400
115M
                }
401
402
                /* Reset LBRR flags */
403
213M
                for( n = 0; n < encControl->nChannelsInternal; n++ ) {
404
117M
                    silk_memset( psEnc->state_Fxx[ n ].sCmn.LBRR_flags, 0, sizeof( psEnc->state_Fxx[ n ].sCmn.LBRR_flags ) );
405
117M
                }
406
95.8M
                curr_nBitsUsedLBRR = ec_tell( psRangeEnc ) - curr_nBitsUsedLBRR;
407
95.8M
            }
408
409
115M
            silk_HP_variable_cutoff( psEnc->state_Fxx );
410
411
            /* Total target bits for packet */
412
115M
            nBits = silk_DIV32_16( silk_MUL( encControl->bitRate, encControl->payloadSize_ms ), 1000 );
413
            /* Subtract bits used for LBRR */
414
115M
            if( !prefillFlag ) {
415
                /* psEnc->nBitsUsedLBRR is an exponential moving average of the LBRR usage,
416
                   except that for the first LBRR frame it does no averaging and for the first
417
                   frame after after LBRR, it goes back to zero immediately. */
418
115M
                if ( curr_nBitsUsedLBRR < 10 ) {
419
115M
                    psEnc->nBitsUsedLBRR = 0;
420
115M
                } else if ( psEnc->nBitsUsedLBRR < 10) {
421
109k
                    psEnc->nBitsUsedLBRR = curr_nBitsUsedLBRR;
422
315k
                } else {
423
315k
                    psEnc->nBitsUsedLBRR = ( psEnc->nBitsUsedLBRR + curr_nBitsUsedLBRR ) / 2;
424
315k
                }
425
115M
                nBits -= psEnc->nBitsUsedLBRR;
426
115M
            }
427
            /* Divide by number of uncoded frames left in packet */
428
115M
            nBits = silk_DIV32_16( nBits, psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket );
429
            /* Convert to bits/second */
430
115M
            if( encControl->payloadSize_ms == 10 ) {
431
39.2M
                TargetRate_bps = silk_SMULBB( nBits, 100 );
432
76.4M
            } else {
433
76.4M
                TargetRate_bps = silk_SMULBB( nBits, 50 );
434
76.4M
            }
435
            /* Subtract fraction of bits in excess of target in previous frames and packets */
436
115M
            TargetRate_bps -= silk_DIV32_16( silk_MUL( psEnc->nBitsExceeded, 1000 ), BITRESERVOIR_DECAY_TIME_MS );
437
115M
            if( !prefillFlag && psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded > 0 ) {
438
                /* Compare actual vs target bits so far in this packet */
439
19.8M
                opus_int32 bitsBalance = ec_tell( psRangeEnc ) - psEnc->nBitsUsedLBRR - nBits * psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded;
440
19.8M
                TargetRate_bps -= silk_DIV32_16( silk_MUL( bitsBalance, 1000 ), BITRESERVOIR_DECAY_TIME_MS );
441
19.8M
            }
442
            /* Never exceed input bitrate */
443
115M
            TargetRate_bps = silk_LIMIT( TargetRate_bps, encControl->bitRate, 5000 );
444
445
            /* Convert Left/Right to Mid/Side */
446
115M
            if( encControl->nChannelsInternal == 2 ) {
447
26.6M
                silk_stereo_LR_to_MS( &psEnc->sStereo, &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ 2 ], &psEnc->state_Fxx[ 1 ].sCmn.inputBuf[ 2 ],
448
26.6M
                    psEnc->sStereo.predIx[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ], &psEnc->sStereo.mid_only_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ],
449
26.6M
                    MStargetRates_bps, TargetRate_bps, psEnc->state_Fxx[ 0 ].sCmn.speech_activity_Q8, encControl->toMono,
450
26.6M
                    psEnc->state_Fxx[ 0 ].sCmn.fs_kHz, psEnc->state_Fxx[ 0 ].sCmn.frame_length );
451
26.6M
                if( psEnc->sStereo.mid_only_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] == 0 ) {
452
                    /* Reset side channel encoder memory for first frame with side coding */
453
18.8M
                    if( psEnc->prev_decode_only_middle == 1 ) {
454
5.21k
                        silk_memset( &psEnc->state_Fxx[ 1 ].sShape,               0, sizeof( psEnc->state_Fxx[ 1 ].sShape ) );
455
5.21k
                        silk_memset( &psEnc->state_Fxx[ 1 ].sCmn.sNSQ,            0, sizeof( psEnc->state_Fxx[ 1 ].sCmn.sNSQ ) );
456
5.21k
                        silk_memset( psEnc->state_Fxx[ 1 ].sCmn.prev_NLSFq_Q15,   0, sizeof( psEnc->state_Fxx[ 1 ].sCmn.prev_NLSFq_Q15 ) );
457
5.21k
                        silk_memset( &psEnc->state_Fxx[ 1 ].sCmn.sLP.In_LP_State, 0, sizeof( psEnc->state_Fxx[ 1 ].sCmn.sLP.In_LP_State ) );
458
5.21k
                        psEnc->state_Fxx[ 1 ].sCmn.prevLag                 = 100;
459
5.21k
                        psEnc->state_Fxx[ 1 ].sCmn.sNSQ.lagPrev            = 100;
460
5.21k
                        psEnc->state_Fxx[ 1 ].sShape.LastGainIndex         = 10;
461
5.21k
                        psEnc->state_Fxx[ 1 ].sCmn.prevSignalType          = TYPE_NO_VOICE_ACTIVITY;
462
5.21k
                        psEnc->state_Fxx[ 1 ].sCmn.sNSQ.prev_gain_Q16      = 65536;
463
5.21k
                        psEnc->state_Fxx[ 1 ].sCmn.first_frame_after_reset = 1;
464
5.21k
                    }
465
18.8M
                    silk_encode_do_VAD_Fxx( &psEnc->state_Fxx[ 1 ], activity );
466
18.8M
                } else {
467
7.77M
                    psEnc->state_Fxx[ 1 ].sCmn.VAD_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] = 0;
468
7.77M
                }
469
26.6M
                if( !prefillFlag ) {
470
26.6M
                    silk_stereo_encode_pred( psRangeEnc, psEnc->sStereo.predIx[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] );
471
26.6M
                    if( psEnc->state_Fxx[ 1 ].sCmn.VAD_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] == 0 ) {
472
26.0M
                        silk_stereo_encode_mid_only( psRangeEnc, psEnc->sStereo.mid_only_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] );
473
26.0M
                    }
474
26.6M
                }
475
89.0M
            } else {
476
                /* Buffering */
477
89.0M
                silk_memcpy( psEnc->state_Fxx[ 0 ].sCmn.inputBuf, psEnc->sStereo.sMid, 2 * sizeof( opus_int16 ) );
478
89.0M
                silk_memcpy( psEnc->sStereo.sMid, &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.frame_length ], 2 * sizeof( opus_int16 ) );
479
89.0M
            }
480
115M
            silk_encode_do_VAD_Fxx( &psEnc->state_Fxx[ 0 ], activity );
481
482
            /* Encode */
483
257M
            for( n = 0; n < encControl->nChannelsInternal; n++ ) {
484
142M
                opus_int maxBits, useCBR;
485
486
                /* Handling rate constraints */
487
142M
                maxBits = encControl->maxBits;
488
142M
                if( tot_blocks == 2 && curr_block == 0 ) {
489
12.2M
                    maxBits = maxBits * 3 / 5;
490
130M
                } else if( tot_blocks == 3 ) {
491
18.1M
                    if( curr_block == 0 ) {
492
6.04M
                        maxBits = maxBits * 2 / 5;
493
12.0M
                    } else if( curr_block == 1 ) {
494
6.04M
                        maxBits = maxBits * 3 / 4;
495
6.04M
                    }
496
18.1M
                }
497
142M
                useCBR = encControl->useCBR && curr_block == tot_blocks - 1;
498
499
142M
                if( encControl->nChannelsInternal == 1 ) {
500
89.0M
                    channelRate_bps = TargetRate_bps;
501
89.0M
                } else {
502
53.2M
                    channelRate_bps = MStargetRates_bps[ n ];
503
53.2M
                    if( n == 0 && MStargetRates_bps[ 1 ] > 0 ) {
504
18.8M
                        useCBR = 0;
505
                        /* Give mid up to 1/2 of the max bits for that frame */
506
18.8M
                        maxBits -= encControl->maxBits / ( tot_blocks * 2 );
507
18.8M
                    }
508
53.2M
                }
509
510
142M
                if( channelRate_bps > 0 ) {
511
134M
                    opus_int condCoding;
512
513
134M
                    silk_control_SNR( &psEnc->state_Fxx[ n ].sCmn, channelRate_bps );
514
515
                    /* Use independent coding if no previous frame available */
516
134M
                    if( psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded - n <= 0 ) {
517
111M
                        condCoding = CODE_INDEPENDENTLY;
518
111M
                    } else if( n > 0 && psEnc->prev_decode_only_middle ) {
519
                        /* If we skipped a side frame in this packet, we don't
520
                           need LTP scaling; the LTP state is well-defined. */
521
2.82k
                        condCoding = CODE_INDEPENDENTLY_NO_LTP_SCALING;
522
23.0M
                    } else {
523
23.0M
                        condCoding = CODE_CONDITIONALLY;
524
23.0M
                    }
525
134M
                    if( ( ret = silk_encode_frame_Fxx( &psEnc->state_Fxx[ n ], nBytesOut, psRangeEnc, condCoding, maxBits, useCBR ) ) != 0 ) {
526
0
                        silk_assert( 0 );
527
0
                    }
528
134M
                }
529
142M
                psEnc->state_Fxx[ n ].sCmn.controlled_since_last_payload = 0;
530
142M
                psEnc->state_Fxx[ n ].sCmn.inputBufIx = 0;
531
142M
                psEnc->state_Fxx[ n ].sCmn.nFramesEncoded++;
532
142M
            }
533
115M
            psEnc->prev_decode_only_middle = psEnc->sStereo.mid_only_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded - 1 ];
534
535
            /* Insert VAD and FEC flags at beginning of bitstream */
536
115M
            if( *nBytesOut > 0 && psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded == psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket) {
537
95.8M
                flags = 0;
538
213M
                for( n = 0; n < encControl->nChannelsInternal; n++ ) {
539
260M
                    for( i = 0; i < psEnc->state_Fxx[ n ].sCmn.nFramesPerPacket; i++ ) {
540
142M
                        flags  = silk_LSHIFT( flags, 1 );
541
142M
                        flags |= psEnc->state_Fxx[ n ].sCmn.VAD_flags[ i ];
542
142M
                    }
543
117M
                    flags  = silk_LSHIFT( flags, 1 );
544
117M
                    flags |= psEnc->state_Fxx[ n ].sCmn.LBRR_flag;
545
117M
                }
546
95.8M
                if( !prefillFlag ) {
547
95.8M
                    ec_enc_patch_initial_bits( psRangeEnc, flags, ( psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket + 1 ) * encControl->nChannelsInternal );
548
95.8M
                }
549
550
                /* Return zero bytes if all channels DTXed */
551
95.8M
                if( psEnc->state_Fxx[ 0 ].sCmn.inDTX && ( encControl->nChannelsInternal == 1 || psEnc->state_Fxx[ 1 ].sCmn.inDTX ) ) {
552
158k
                    *nBytesOut = 0;
553
158k
                }
554
555
95.8M
                psEnc->nBitsExceeded += *nBytesOut * 8;
556
95.8M
                psEnc->nBitsExceeded -= silk_DIV32_16( silk_MUL( encControl->bitRate, encControl->payloadSize_ms ), 1000 );
557
95.8M
                psEnc->nBitsExceeded  = silk_LIMIT( psEnc->nBitsExceeded, 0, 10000 );
558
559
                /* Update flag indicating if bandwidth switching is allowed */
560
95.8M
                speech_act_thr_for_switch_Q8 = silk_SMLAWB( SILK_FIX_CONST( SPEECH_ACTIVITY_DTX_THRES, 8 ),
561
95.8M
                    SILK_FIX_CONST( ( 1 - SPEECH_ACTIVITY_DTX_THRES ) / MAX_BANDWIDTH_SWITCH_DELAY_MS, 16 + 8 ), psEnc->timeSinceSwitchAllowed_ms );
562
95.8M
                if( psEnc->state_Fxx[ 0 ].sCmn.speech_activity_Q8 < speech_act_thr_for_switch_Q8 ) {
563
93.5M
                    psEnc->allowBandwidthSwitch = 1;
564
93.5M
                    psEnc->timeSinceSwitchAllowed_ms = 0;
565
93.5M
                } else {
566
2.34M
                    psEnc->allowBandwidthSwitch = 0;
567
2.34M
                    psEnc->timeSinceSwitchAllowed_ms += encControl->payloadSize_ms;
568
2.34M
                }
569
95.8M
            }
570
571
115M
            if( nSamplesIn == 0 ) {
572
95.8M
                break;
573
95.8M
            }
574
115M
        } else {
575
0
            break;
576
0
        }
577
19.8M
        curr_block++;
578
19.8M
    }
579
580
95.8M
    psEnc->nPrevChannelsInternal = encControl->nChannelsInternal;
581
582
95.8M
    encControl->allowBandwidthSwitch = psEnc->allowBandwidthSwitch;
583
95.8M
    encControl->inWBmodeWithoutVariableLP = psEnc->state_Fxx[ 0 ].sCmn.fs_kHz == 16 && psEnc->state_Fxx[ 0 ].sCmn.sLP.mode == 0;
584
95.8M
    encControl->internalSampleRate = silk_SMULBB( psEnc->state_Fxx[ 0 ].sCmn.fs_kHz, 1000 );
585
95.8M
    encControl->stereoWidth_Q14 = encControl->toMono ? 0 : psEnc->sStereo.smth_width_Q14;
586
95.8M
    if( prefillFlag ) {
587
2.21k
        encControl->payloadSize_ms = tmp_payloadSize_ms;
588
2.21k
        encControl->complexity = tmp_complexity;
589
4.87k
        for( n = 0; n < encControl->nChannelsInternal; n++ ) {
590
2.66k
            psEnc->state_Fxx[ n ].sCmn.controlled_since_last_payload = 0;
591
2.66k
            psEnc->state_Fxx[ n ].sCmn.prefillFlag = 0;
592
2.66k
        }
593
2.21k
    }
594
595
95.8M
    encControl->signalType = psEnc->state_Fxx[0].sCmn.indices.signalType;
596
95.8M
    encControl->offset = silk_Quantization_Offsets_Q10
597
95.8M
                         [ psEnc->state_Fxx[0].sCmn.indices.signalType >> 1 ]
598
95.8M
                         [ psEnc->state_Fxx[0].sCmn.indices.quantOffsetType ];
599
95.8M
    RESTORE_STACK;
600
95.8M
    return ret;
601
95.8M
}
silk_Encode
Line
Count
Source
160
47.9M
{
161
47.9M
    opus_int   n, i, nBits, flags, tmp_payloadSize_ms = 0, tmp_complexity = 0, ret = 0;
162
47.9M
    opus_int   nSamplesToBuffer, nSamplesToBufferMax, nBlocksOf10ms;
163
47.9M
    opus_int   nSamplesFromInput = 0, nSamplesFromInputMax;
164
47.9M
    opus_int   speech_act_thr_for_switch_Q8;
165
47.9M
    opus_int32 TargetRate_bps, MStargetRates_bps[ 2 ], channelRate_bps, LBRR_symbol, sum;
166
47.9M
    silk_encoder *psEnc = ( silk_encoder * )encState;
167
47.9M
    VARDECL( opus_int16, buf );
168
47.9M
    opus_int transition, curr_block, tot_blocks;
169
47.9M
    SAVE_STACK;
170
171
47.9M
    celt_assert( encControl->nChannelsAPI >= encControl->nChannelsInternal && encControl->nChannelsAPI >= psEnc->nChannelsInternal );
172
47.9M
    if (encControl->reducedDependency)
173
14.7M
    {
174
36.1M
       for( n = 0; n < encControl->nChannelsAPI; n++ ) {
175
21.3M
           psEnc->state_Fxx[ n ].sCmn.first_frame_after_reset = 1;
176
21.3M
       }
177
14.7M
    }
178
112M
    for( n = 0; n < encControl->nChannelsAPI; n++ ) {
179
64.1M
        psEnc->state_Fxx[ n ].sCmn.nFramesEncoded = 0;
180
64.1M
    }
181
    /* Check values in encoder control structure */
182
47.9M
    if( ( ret = check_control_input( encControl ) ) != 0 ) {
183
0
        celt_assert( 0 );
184
0
        RESTORE_STACK;
185
0
        return ret;
186
0
    }
187
188
47.9M
    encControl->switchReady = 0;
189
190
47.9M
    if( encControl->nChannelsInternal > psEnc->nChannelsInternal ) {
191
        /* Mono -> Stereo transition: init state of second channel and stereo state */
192
53.9k
        ret += silk_init_encoder( &psEnc->state_Fxx[ 1 ], psEnc->state_Fxx[ 0 ].sCmn.arch );
193
53.9k
        silk_memset( psEnc->sStereo.pred_prev_Q13, 0, sizeof( psEnc->sStereo.pred_prev_Q13 ) );
194
53.9k
        silk_memset( psEnc->sStereo.sSide, 0, sizeof( psEnc->sStereo.sSide ) );
195
53.9k
        psEnc->sStereo.mid_side_amp_Q0[ 0 ] = 0;
196
53.9k
        psEnc->sStereo.mid_side_amp_Q0[ 1 ] = 1;
197
53.9k
        psEnc->sStereo.mid_side_amp_Q0[ 2 ] = 0;
198
53.9k
        psEnc->sStereo.mid_side_amp_Q0[ 3 ] = 1;
199
53.9k
        psEnc->sStereo.width_prev_Q14 = 0;
200
53.9k
        psEnc->sStereo.smth_width_Q14 = SILK_FIX_CONST( 1, 14 );
201
53.9k
        if( psEnc->nChannelsAPI == 2 ) {
202
0
            silk_memcpy( &psEnc->state_Fxx[ 1 ].sCmn.resampler_state, &psEnc->state_Fxx[ 0 ].sCmn.resampler_state, sizeof( silk_resampler_state_struct ) );
203
0
            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 ) );
204
0
        }
205
53.9k
    }
206
207
47.9M
    transition = (encControl->payloadSize_ms != psEnc->state_Fxx[ 0 ].sCmn.PacketSize_ms) || (psEnc->nChannelsInternal != encControl->nChannelsInternal);
208
209
47.9M
    psEnc->nChannelsAPI = encControl->nChannelsAPI;
210
47.9M
    psEnc->nChannelsInternal = encControl->nChannelsInternal;
211
212
47.9M
    nBlocksOf10ms = silk_DIV32( 100 * nSamplesIn, encControl->API_sampleRate );
213
47.9M
    tot_blocks = ( nBlocksOf10ms > 1 ) ? nBlocksOf10ms >> 1 : 1;
214
47.9M
    curr_block = 0;
215
47.9M
    if( prefillFlag ) {
216
1.10k
        silk_LP_state save_LP;
217
        /* Only accept input length of 10 ms */
218
1.10k
        if( nBlocksOf10ms != 1 ) {
219
0
            celt_assert( 0 );
220
0
            RESTORE_STACK;
221
0
            return SILK_ENC_INPUT_INVALID_NO_OF_SAMPLES;
222
0
        }
223
1.10k
        if ( prefillFlag == 2 ) {
224
348
            save_LP = psEnc->state_Fxx[ 0 ].sCmn.sLP;
225
            /* Save the sampling rate so the bandwidth switching code can keep handling transitions. */
226
348
            save_LP.saved_fs_kHz = psEnc->state_Fxx[ 0 ].sCmn.fs_kHz;
227
348
        }
228
        /* Reset Encoder */
229
2.43k
        for( n = 0; n < encControl->nChannelsInternal; n++ ) {
230
1.33k
            ret = silk_init_encoder( &psEnc->state_Fxx[ n ], psEnc->state_Fxx[ n ].sCmn.arch );
231
            /* Restore the variable LP state. */
232
1.33k
            if ( prefillFlag == 2 ) {
233
411
                psEnc->state_Fxx[ n ].sCmn.sLP = save_LP;
234
411
            }
235
1.33k
            celt_assert( !ret );
236
1.33k
        }
237
1.10k
        tmp_payloadSize_ms = encControl->payloadSize_ms;
238
1.10k
        encControl->payloadSize_ms = 10;
239
1.10k
        tmp_complexity = encControl->complexity;
240
1.10k
        encControl->complexity = 0;
241
2.43k
        for( n = 0; n < encControl->nChannelsInternal; n++ ) {
242
1.33k
            psEnc->state_Fxx[ n ].sCmn.controlled_since_last_payload = 0;
243
1.33k
            psEnc->state_Fxx[ n ].sCmn.prefillFlag = 1;
244
1.33k
        }
245
47.9M
    } else {
246
        /* Only accept input lengths that are a multiple of 10 ms */
247
47.9M
        if( nBlocksOf10ms * encControl->API_sampleRate != 100 * nSamplesIn || nSamplesIn < 0 ) {
248
0
            celt_assert( 0 );
249
0
            RESTORE_STACK;
250
0
            return SILK_ENC_INPUT_INVALID_NO_OF_SAMPLES;
251
0
        }
252
        /* Make sure no more than one packet can be produced */
253
47.9M
        if( 1000 * (opus_int32)nSamplesIn > encControl->payloadSize_ms * encControl->API_sampleRate ) {
254
0
            celt_assert( 0 );
255
0
            RESTORE_STACK;
256
0
            return SILK_ENC_INPUT_INVALID_NO_OF_SAMPLES;
257
0
        }
258
47.9M
    }
259
260
106M
    for( n = 0; n < encControl->nChannelsInternal; n++ ) {
261
        /* Force the side channel to the same rate as the mid */
262
59.0M
        opus_int force_fs_kHz = (n==1) ? psEnc->state_Fxx[0].sCmn.fs_kHz : 0;
263
59.0M
        if( ( ret = silk_control_encoder( &psEnc->state_Fxx[ n ], encControl, psEnc->allowBandwidthSwitch, n, force_fs_kHz ) ) != 0 ) {
264
0
            silk_assert( 0 );
265
0
            RESTORE_STACK;
266
0
            return ret;
267
0
        }
268
59.0M
        if( psEnc->state_Fxx[n].sCmn.first_frame_after_reset || transition ) {
269
44.8M
            for( i = 0; i < psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket; i++ ) {
270
24.6M
                psEnc->state_Fxx[ n ].sCmn.LBRR_flags[ i ] = 0;
271
24.6M
            }
272
20.1M
        }
273
59.0M
        psEnc->state_Fxx[ n ].sCmn.inDTX = psEnc->state_Fxx[ n ].sCmn.useDTX;
274
59.0M
    }
275
47.9M
    celt_assert( encControl->nChannelsInternal == 1 || psEnc->state_Fxx[ 0 ].sCmn.fs_kHz == psEnc->state_Fxx[ 1 ].sCmn.fs_kHz );
276
277
    /* Input buffering/resampling and encoding */
278
47.9M
    nSamplesToBufferMax =
279
47.9M
        10 * nBlocksOf10ms * psEnc->state_Fxx[ 0 ].sCmn.fs_kHz;
280
47.9M
    nSamplesFromInputMax =
281
47.9M
        silk_DIV32_16( nSamplesToBufferMax *
282
47.9M
                           psEnc->state_Fxx[ 0 ].sCmn.API_fs_Hz,
283
47.9M
                       psEnc->state_Fxx[ 0 ].sCmn.fs_kHz * 1000 );
284
47.9M
    ALLOC( buf, nSamplesFromInputMax, opus_int16 );
285
57.8M
    while( 1 ) {
286
57.8M
        int curr_nBitsUsedLBRR = 0;
287
57.8M
        nSamplesToBuffer  = psEnc->state_Fxx[ 0 ].sCmn.frame_length - psEnc->state_Fxx[ 0 ].sCmn.inputBufIx;
288
57.8M
        nSamplesToBuffer  = silk_min( nSamplesToBuffer, nSamplesToBufferMax );
289
57.8M
        nSamplesFromInput = silk_DIV32_16( nSamplesToBuffer * psEnc->state_Fxx[ 0 ].sCmn.API_fs_Hz, psEnc->state_Fxx[ 0 ].sCmn.fs_kHz * 1000 );
290
        /* Resample and write to buffer */
291
57.8M
        if( encControl->nChannelsAPI == 2 && encControl->nChannelsInternal == 2 ) {
292
13.3M
            opus_int id = psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded;
293
4.22G
            for( n = 0; n < nSamplesFromInput; n++ ) {
294
4.21G
                buf[ n ] = RES2INT16(samplesIn[ 2 * n ]);
295
4.21G
            }
296
            /* Making sure to start both resamplers from the same state when switching from mono to stereo */
297
13.3M
            if( psEnc->nPrevChannelsInternal == 1 && id==0 ) {
298
0
               silk_memcpy( &psEnc->state_Fxx[ 1 ].sCmn.resampler_state, &psEnc->state_Fxx[ 0 ].sCmn.resampler_state, sizeof(psEnc->state_Fxx[ 1 ].sCmn.resampler_state));
299
0
            }
300
301
13.3M
            ret += silk_resampler( &psEnc->state_Fxx[ 0 ].sCmn.resampler_state,
302
13.3M
                &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput );
303
13.3M
            psEnc->state_Fxx[ 0 ].sCmn.inputBufIx += nSamplesToBuffer;
304
305
13.3M
            nSamplesToBuffer  = psEnc->state_Fxx[ 1 ].sCmn.frame_length - psEnc->state_Fxx[ 1 ].sCmn.inputBufIx;
306
13.3M
            nSamplesToBuffer  = silk_min( nSamplesToBuffer, 10 * nBlocksOf10ms * psEnc->state_Fxx[ 1 ].sCmn.fs_kHz );
307
4.22G
            for( n = 0; n < nSamplesFromInput; n++ ) {
308
4.21G
                buf[ n ] = RES2INT16(samplesIn[ 2 * n + 1 ]);
309
4.21G
            }
310
13.3M
            ret += silk_resampler( &psEnc->state_Fxx[ 1 ].sCmn.resampler_state,
311
13.3M
                &psEnc->state_Fxx[ 1 ].sCmn.inputBuf[ psEnc->state_Fxx[ 1 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput );
312
313
13.3M
            psEnc->state_Fxx[ 1 ].sCmn.inputBufIx += nSamplesToBuffer;
314
44.5M
        } else if( encControl->nChannelsAPI == 2 && encControl->nChannelsInternal == 1 ) {
315
            /* Combine left and right channels before resampling */
316
1.71G
            for( n = 0; n < nSamplesFromInput; n++ ) {
317
1.70G
                sum = RES2INT16(samplesIn[ 2 * n ] + samplesIn[ 2 * n + 1 ]);
318
1.70G
                buf[ n ] = (opus_int16)silk_RSHIFT_ROUND( sum,  1 );
319
1.70G
            }
320
6.14M
            ret += silk_resampler( &psEnc->state_Fxx[ 0 ].sCmn.resampler_state,
321
6.14M
                &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput );
322
            /* On the first mono frame, average the results for the two resampler states  */
323
6.14M
            if( psEnc->nPrevChannelsInternal == 2 && psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded == 0 ) {
324
0
               ret += silk_resampler( &psEnc->state_Fxx[ 1 ].sCmn.resampler_state,
325
0
                   &psEnc->state_Fxx[ 1 ].sCmn.inputBuf[ psEnc->state_Fxx[ 1 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput );
326
0
               for( n = 0; n < psEnc->state_Fxx[ 0 ].sCmn.frame_length; n++ ) {
327
0
                  psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx+n+2 ] =
328
0
                        silk_RSHIFT(psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx+n+2 ]
329
0
                                  + psEnc->state_Fxx[ 1 ].sCmn.inputBuf[ psEnc->state_Fxx[ 1 ].sCmn.inputBufIx+n+2 ], 1);
330
0
               }
331
0
            }
332
6.14M
            psEnc->state_Fxx[ 0 ].sCmn.inputBufIx += nSamplesToBuffer;
333
38.3M
        } else {
334
38.3M
            celt_assert( encControl->nChannelsAPI == 1 && encControl->nChannelsInternal == 1 );
335
11.7G
            for( n = 0; n < nSamplesFromInput; n++ ) {
336
11.6G
                buf[n] = RES2INT16(samplesIn[n]);
337
11.6G
            }
338
38.3M
            ret += silk_resampler( &psEnc->state_Fxx[ 0 ].sCmn.resampler_state,
339
38.3M
                &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput );
340
38.3M
            psEnc->state_Fxx[ 0 ].sCmn.inputBufIx += nSamplesToBuffer;
341
38.3M
        }
342
343
57.8M
        samplesIn  += nSamplesFromInput * encControl->nChannelsAPI;
344
57.8M
        nSamplesIn -= nSamplesFromInput;
345
346
        /* Default */
347
57.8M
        psEnc->allowBandwidthSwitch = 0;
348
349
        /* Silk encoder */
350
57.8M
        if( psEnc->state_Fxx[ 0 ].sCmn.inputBufIx >= psEnc->state_Fxx[ 0 ].sCmn.frame_length ) {
351
            /* Enough data in input buffer, so encode */
352
57.8M
            celt_assert( psEnc->state_Fxx[ 0 ].sCmn.inputBufIx == psEnc->state_Fxx[ 0 ].sCmn.frame_length );
353
57.8M
            celt_assert( encControl->nChannelsInternal == 1 || psEnc->state_Fxx[ 1 ].sCmn.inputBufIx == psEnc->state_Fxx[ 1 ].sCmn.frame_length );
354
355
            /* Deal with LBRR data */
356
57.8M
            if( psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded == 0 && !prefillFlag ) {
357
                /* Create space at start of payload for VAD and FEC flags */
358
47.9M
                opus_uint8 iCDF[ 2 ] = { 0, 0 };
359
47.9M
                iCDF[ 0 ] = 256 - silk_RSHIFT( 256, ( psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket + 1 ) * encControl->nChannelsInternal );
360
47.9M
                ec_enc_icdf( psRangeEnc, 0, iCDF, 8 );
361
47.9M
                curr_nBitsUsedLBRR = ec_tell( psRangeEnc );
362
363
                /* Encode any LBRR data from previous packet */
364
                /* Encode LBRR flags */
365
106M
                for( n = 0; n < encControl->nChannelsInternal; n++ ) {
366
58.9M
                    LBRR_symbol = 0;
367
130M
                    for( i = 0; i < psEnc->state_Fxx[ n ].sCmn.nFramesPerPacket; i++ ) {
368
71.1M
                        LBRR_symbol |= silk_LSHIFT( psEnc->state_Fxx[ n ].sCmn.LBRR_flags[ i ], i );
369
71.1M
                    }
370
58.9M
                    psEnc->state_Fxx[ n ].sCmn.LBRR_flag = LBRR_symbol > 0 ? 1 : 0;
371
58.9M
                    if( LBRR_symbol && psEnc->state_Fxx[ n ].sCmn.nFramesPerPacket > 1 ) {
372
51.9k
                        ec_enc_icdf( psRangeEnc, LBRR_symbol - 1, silk_LBRR_flags_iCDF_ptr[ psEnc->state_Fxx[ n ].sCmn.nFramesPerPacket - 2 ], 8 );
373
51.9k
                    }
374
58.9M
                }
375
376
                /* Code LBRR indices and excitation signals */
377
105M
                for( i = 0; i < psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket; i++ ) {
378
128M
                    for( n = 0; n < encControl->nChannelsInternal; n++ ) {
379
71.1M
                        if( psEnc->state_Fxx[ n ].sCmn.LBRR_flags[ i ] ) {
380
322k
                            opus_int condCoding;
381
382
322k
                            if( encControl->nChannelsInternal == 2 && n == 0 ) {
383
92.4k
                                silk_stereo_encode_pred( psRangeEnc, psEnc->sStereo.predIx[ i ] );
384
                                /* For LBRR data there's no need to code the mid-only flag if the side-channel LBRR flag is set */
385
92.4k
                                if( psEnc->state_Fxx[ 1 ].sCmn.LBRR_flags[ i ] == 0 ) {
386
36.3k
                                    silk_stereo_encode_mid_only( psRangeEnc, psEnc->sStereo.mid_only_flags[ i ] );
387
36.3k
                                }
388
92.4k
                            }
389
                            /* Use conditional coding if previous frame available */
390
322k
                            if( i > 0 && psEnc->state_Fxx[ n ].sCmn.LBRR_flags[ i - 1 ] ) {
391
62.8k
                                condCoding = CODE_CONDITIONALLY;
392
259k
                            } else {
393
259k
                                condCoding = CODE_INDEPENDENTLY;
394
259k
                            }
395
322k
                            silk_encode_indices( &psEnc->state_Fxx[ n ].sCmn, psRangeEnc, i, 1, condCoding );
396
322k
                            silk_encode_pulses( psRangeEnc, psEnc->state_Fxx[ n ].sCmn.indices_LBRR[i].signalType, psEnc->state_Fxx[ n ].sCmn.indices_LBRR[i].quantOffsetType,
397
322k
                                psEnc->state_Fxx[ n ].sCmn.pulses_LBRR[ i ], psEnc->state_Fxx[ n ].sCmn.frame_length );
398
322k
                        }
399
71.1M
                    }
400
57.8M
                }
401
402
                /* Reset LBRR flags */
403
106M
                for( n = 0; n < encControl->nChannelsInternal; n++ ) {
404
58.9M
                    silk_memset( psEnc->state_Fxx[ n ].sCmn.LBRR_flags, 0, sizeof( psEnc->state_Fxx[ n ].sCmn.LBRR_flags ) );
405
58.9M
                }
406
47.9M
                curr_nBitsUsedLBRR = ec_tell( psRangeEnc ) - curr_nBitsUsedLBRR;
407
47.9M
            }
408
409
57.8M
            silk_HP_variable_cutoff( psEnc->state_Fxx );
410
411
            /* Total target bits for packet */
412
57.8M
            nBits = silk_DIV32_16( silk_MUL( encControl->bitRate, encControl->payloadSize_ms ), 1000 );
413
            /* Subtract bits used for LBRR */
414
57.8M
            if( !prefillFlag ) {
415
                /* psEnc->nBitsUsedLBRR is an exponential moving average of the LBRR usage,
416
                   except that for the first LBRR frame it does no averaging and for the first
417
                   frame after after LBRR, it goes back to zero immediately. */
418
57.8M
                if ( curr_nBitsUsedLBRR < 10 ) {
419
57.6M
                    psEnc->nBitsUsedLBRR = 0;
420
57.6M
                } else if ( psEnc->nBitsUsedLBRR < 10) {
421
54.8k
                    psEnc->nBitsUsedLBRR = curr_nBitsUsedLBRR;
422
157k
                } else {
423
157k
                    psEnc->nBitsUsedLBRR = ( psEnc->nBitsUsedLBRR + curr_nBitsUsedLBRR ) / 2;
424
157k
                }
425
57.8M
                nBits -= psEnc->nBitsUsedLBRR;
426
57.8M
            }
427
            /* Divide by number of uncoded frames left in packet */
428
57.8M
            nBits = silk_DIV32_16( nBits, psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket );
429
            /* Convert to bits/second */
430
57.8M
            if( encControl->payloadSize_ms == 10 ) {
431
19.6M
                TargetRate_bps = silk_SMULBB( nBits, 100 );
432
38.2M
            } else {
433
38.2M
                TargetRate_bps = silk_SMULBB( nBits, 50 );
434
38.2M
            }
435
            /* Subtract fraction of bits in excess of target in previous frames and packets */
436
57.8M
            TargetRate_bps -= silk_DIV32_16( silk_MUL( psEnc->nBitsExceeded, 1000 ), BITRESERVOIR_DECAY_TIME_MS );
437
57.8M
            if( !prefillFlag && psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded > 0 ) {
438
                /* Compare actual vs target bits so far in this packet */
439
9.91M
                opus_int32 bitsBalance = ec_tell( psRangeEnc ) - psEnc->nBitsUsedLBRR - nBits * psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded;
440
9.91M
                TargetRate_bps -= silk_DIV32_16( silk_MUL( bitsBalance, 1000 ), BITRESERVOIR_DECAY_TIME_MS );
441
9.91M
            }
442
            /* Never exceed input bitrate */
443
57.8M
            TargetRate_bps = silk_LIMIT( TargetRate_bps, encControl->bitRate, 5000 );
444
445
            /* Convert Left/Right to Mid/Side */
446
57.8M
            if( encControl->nChannelsInternal == 2 ) {
447
13.3M
                silk_stereo_LR_to_MS( &psEnc->sStereo, &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ 2 ], &psEnc->state_Fxx[ 1 ].sCmn.inputBuf[ 2 ],
448
13.3M
                    psEnc->sStereo.predIx[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ], &psEnc->sStereo.mid_only_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ],
449
13.3M
                    MStargetRates_bps, TargetRate_bps, psEnc->state_Fxx[ 0 ].sCmn.speech_activity_Q8, encControl->toMono,
450
13.3M
                    psEnc->state_Fxx[ 0 ].sCmn.fs_kHz, psEnc->state_Fxx[ 0 ].sCmn.frame_length );
451
13.3M
                if( psEnc->sStereo.mid_only_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] == 0 ) {
452
                    /* Reset side channel encoder memory for first frame with side coding */
453
9.42M
                    if( psEnc->prev_decode_only_middle == 1 ) {
454
2.60k
                        silk_memset( &psEnc->state_Fxx[ 1 ].sShape,               0, sizeof( psEnc->state_Fxx[ 1 ].sShape ) );
455
2.60k
                        silk_memset( &psEnc->state_Fxx[ 1 ].sCmn.sNSQ,            0, sizeof( psEnc->state_Fxx[ 1 ].sCmn.sNSQ ) );
456
2.60k
                        silk_memset( psEnc->state_Fxx[ 1 ].sCmn.prev_NLSFq_Q15,   0, sizeof( psEnc->state_Fxx[ 1 ].sCmn.prev_NLSFq_Q15 ) );
457
2.60k
                        silk_memset( &psEnc->state_Fxx[ 1 ].sCmn.sLP.In_LP_State, 0, sizeof( psEnc->state_Fxx[ 1 ].sCmn.sLP.In_LP_State ) );
458
2.60k
                        psEnc->state_Fxx[ 1 ].sCmn.prevLag                 = 100;
459
2.60k
                        psEnc->state_Fxx[ 1 ].sCmn.sNSQ.lagPrev            = 100;
460
2.60k
                        psEnc->state_Fxx[ 1 ].sShape.LastGainIndex         = 10;
461
2.60k
                        psEnc->state_Fxx[ 1 ].sCmn.prevSignalType          = TYPE_NO_VOICE_ACTIVITY;
462
2.60k
                        psEnc->state_Fxx[ 1 ].sCmn.sNSQ.prev_gain_Q16      = 65536;
463
2.60k
                        psEnc->state_Fxx[ 1 ].sCmn.first_frame_after_reset = 1;
464
2.60k
                    }
465
9.42M
                    silk_encode_do_VAD_Fxx( &psEnc->state_Fxx[ 1 ], activity );
466
9.42M
                } else {
467
3.88M
                    psEnc->state_Fxx[ 1 ].sCmn.VAD_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] = 0;
468
3.88M
                }
469
13.3M
                if( !prefillFlag ) {
470
13.3M
                    silk_stereo_encode_pred( psRangeEnc, psEnc->sStereo.predIx[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] );
471
13.3M
                    if( psEnc->state_Fxx[ 1 ].sCmn.VAD_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] == 0 ) {
472
13.0M
                        silk_stereo_encode_mid_only( psRangeEnc, psEnc->sStereo.mid_only_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] );
473
13.0M
                    }
474
13.3M
                }
475
44.5M
            } else {
476
                /* Buffering */
477
44.5M
                silk_memcpy( psEnc->state_Fxx[ 0 ].sCmn.inputBuf, psEnc->sStereo.sMid, 2 * sizeof( opus_int16 ) );
478
44.5M
                silk_memcpy( psEnc->sStereo.sMid, &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.frame_length ], 2 * sizeof( opus_int16 ) );
479
44.5M
            }
480
57.8M
            silk_encode_do_VAD_Fxx( &psEnc->state_Fxx[ 0 ], activity );
481
482
            /* Encode */
483
128M
            for( n = 0; n < encControl->nChannelsInternal; n++ ) {
484
71.1M
                opus_int maxBits, useCBR;
485
486
                /* Handling rate constraints */
487
71.1M
                maxBits = encControl->maxBits;
488
71.1M
                if( tot_blocks == 2 && curr_block == 0 ) {
489
6.10M
                    maxBits = maxBits * 3 / 5;
490
65.0M
                } else if( tot_blocks == 3 ) {
491
9.06M
                    if( curr_block == 0 ) {
492
3.02M
                        maxBits = maxBits * 2 / 5;
493
6.04M
                    } else if( curr_block == 1 ) {
494
3.02M
                        maxBits = maxBits * 3 / 4;
495
3.02M
                    }
496
9.06M
                }
497
71.1M
                useCBR = encControl->useCBR && curr_block == tot_blocks - 1;
498
499
71.1M
                if( encControl->nChannelsInternal == 1 ) {
500
44.5M
                    channelRate_bps = TargetRate_bps;
501
44.5M
                } else {
502
26.6M
                    channelRate_bps = MStargetRates_bps[ n ];
503
26.6M
                    if( n == 0 && MStargetRates_bps[ 1 ] > 0 ) {
504
9.42M
                        useCBR = 0;
505
                        /* Give mid up to 1/2 of the max bits for that frame */
506
9.42M
                        maxBits -= encControl->maxBits / ( tot_blocks * 2 );
507
9.42M
                    }
508
26.6M
                }
509
510
71.1M
                if( channelRate_bps > 0 ) {
511
67.2M
                    opus_int condCoding;
512
513
67.2M
                    silk_control_SNR( &psEnc->state_Fxx[ n ].sCmn, channelRate_bps );
514
515
                    /* Use independent coding if no previous frame available */
516
67.2M
                    if( psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded - n <= 0 ) {
517
55.7M
                        condCoding = CODE_INDEPENDENTLY;
518
55.7M
                    } else if( n > 0 && psEnc->prev_decode_only_middle ) {
519
                        /* If we skipped a side frame in this packet, we don't
520
                           need LTP scaling; the LTP state is well-defined. */
521
1.41k
                        condCoding = CODE_INDEPENDENTLY_NO_LTP_SCALING;
522
11.5M
                    } else {
523
11.5M
                        condCoding = CODE_CONDITIONALLY;
524
11.5M
                    }
525
67.2M
                    if( ( ret = silk_encode_frame_Fxx( &psEnc->state_Fxx[ n ], nBytesOut, psRangeEnc, condCoding, maxBits, useCBR ) ) != 0 ) {
526
0
                        silk_assert( 0 );
527
0
                    }
528
67.2M
                }
529
71.1M
                psEnc->state_Fxx[ n ].sCmn.controlled_since_last_payload = 0;
530
71.1M
                psEnc->state_Fxx[ n ].sCmn.inputBufIx = 0;
531
71.1M
                psEnc->state_Fxx[ n ].sCmn.nFramesEncoded++;
532
71.1M
            }
533
57.8M
            psEnc->prev_decode_only_middle = psEnc->sStereo.mid_only_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded - 1 ];
534
535
            /* Insert VAD and FEC flags at beginning of bitstream */
536
57.8M
            if( *nBytesOut > 0 && psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded == psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket) {
537
47.9M
                flags = 0;
538
106M
                for( n = 0; n < encControl->nChannelsInternal; n++ ) {
539
130M
                    for( i = 0; i < psEnc->state_Fxx[ n ].sCmn.nFramesPerPacket; i++ ) {
540
71.1M
                        flags  = silk_LSHIFT( flags, 1 );
541
71.1M
                        flags |= psEnc->state_Fxx[ n ].sCmn.VAD_flags[ i ];
542
71.1M
                    }
543
58.9M
                    flags  = silk_LSHIFT( flags, 1 );
544
58.9M
                    flags |= psEnc->state_Fxx[ n ].sCmn.LBRR_flag;
545
58.9M
                }
546
47.9M
                if( !prefillFlag ) {
547
47.9M
                    ec_enc_patch_initial_bits( psRangeEnc, flags, ( psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket + 1 ) * encControl->nChannelsInternal );
548
47.9M
                }
549
550
                /* Return zero bytes if all channels DTXed */
551
47.9M
                if( psEnc->state_Fxx[ 0 ].sCmn.inDTX && ( encControl->nChannelsInternal == 1 || psEnc->state_Fxx[ 1 ].sCmn.inDTX ) ) {
552
79.3k
                    *nBytesOut = 0;
553
79.3k
                }
554
555
47.9M
                psEnc->nBitsExceeded += *nBytesOut * 8;
556
47.9M
                psEnc->nBitsExceeded -= silk_DIV32_16( silk_MUL( encControl->bitRate, encControl->payloadSize_ms ), 1000 );
557
47.9M
                psEnc->nBitsExceeded  = silk_LIMIT( psEnc->nBitsExceeded, 0, 10000 );
558
559
                /* Update flag indicating if bandwidth switching is allowed */
560
47.9M
                speech_act_thr_for_switch_Q8 = silk_SMLAWB( SILK_FIX_CONST( SPEECH_ACTIVITY_DTX_THRES, 8 ),
561
47.9M
                    SILK_FIX_CONST( ( 1 - SPEECH_ACTIVITY_DTX_THRES ) / MAX_BANDWIDTH_SWITCH_DELAY_MS, 16 + 8 ), psEnc->timeSinceSwitchAllowed_ms );
562
47.9M
                if( psEnc->state_Fxx[ 0 ].sCmn.speech_activity_Q8 < speech_act_thr_for_switch_Q8 ) {
563
46.7M
                    psEnc->allowBandwidthSwitch = 1;
564
46.7M
                    psEnc->timeSinceSwitchAllowed_ms = 0;
565
46.7M
                } else {
566
1.17M
                    psEnc->allowBandwidthSwitch = 0;
567
1.17M
                    psEnc->timeSinceSwitchAllowed_ms += encControl->payloadSize_ms;
568
1.17M
                }
569
47.9M
            }
570
571
57.8M
            if( nSamplesIn == 0 ) {
572
47.9M
                break;
573
47.9M
            }
574
57.8M
        } else {
575
0
            break;
576
0
        }
577
9.91M
        curr_block++;
578
9.91M
    }
579
580
47.9M
    psEnc->nPrevChannelsInternal = encControl->nChannelsInternal;
581
582
47.9M
    encControl->allowBandwidthSwitch = psEnc->allowBandwidthSwitch;
583
47.9M
    encControl->inWBmodeWithoutVariableLP = psEnc->state_Fxx[ 0 ].sCmn.fs_kHz == 16 && psEnc->state_Fxx[ 0 ].sCmn.sLP.mode == 0;
584
47.9M
    encControl->internalSampleRate = silk_SMULBB( psEnc->state_Fxx[ 0 ].sCmn.fs_kHz, 1000 );
585
47.9M
    encControl->stereoWidth_Q14 = encControl->toMono ? 0 : psEnc->sStereo.smth_width_Q14;
586
47.9M
    if( prefillFlag ) {
587
1.10k
        encControl->payloadSize_ms = tmp_payloadSize_ms;
588
1.10k
        encControl->complexity = tmp_complexity;
589
2.43k
        for( n = 0; n < encControl->nChannelsInternal; n++ ) {
590
1.33k
            psEnc->state_Fxx[ n ].sCmn.controlled_since_last_payload = 0;
591
1.33k
            psEnc->state_Fxx[ n ].sCmn.prefillFlag = 0;
592
1.33k
        }
593
1.10k
    }
594
595
47.9M
    encControl->signalType = psEnc->state_Fxx[0].sCmn.indices.signalType;
596
47.9M
    encControl->offset = silk_Quantization_Offsets_Q10
597
47.9M
                         [ psEnc->state_Fxx[0].sCmn.indices.signalType >> 1 ]
598
47.9M
                         [ psEnc->state_Fxx[0].sCmn.indices.quantOffsetType ];
599
47.9M
    RESTORE_STACK;
600
47.9M
    return ret;
601
47.9M
}
silk_Encode
Line
Count
Source
160
47.9M
{
161
47.9M
    opus_int   n, i, nBits, flags, tmp_payloadSize_ms = 0, tmp_complexity = 0, ret = 0;
162
47.9M
    opus_int   nSamplesToBuffer, nSamplesToBufferMax, nBlocksOf10ms;
163
47.9M
    opus_int   nSamplesFromInput = 0, nSamplesFromInputMax;
164
47.9M
    opus_int   speech_act_thr_for_switch_Q8;
165
47.9M
    opus_int32 TargetRate_bps, MStargetRates_bps[ 2 ], channelRate_bps, LBRR_symbol, sum;
166
47.9M
    silk_encoder *psEnc = ( silk_encoder * )encState;
167
47.9M
    VARDECL( opus_int16, buf );
168
47.9M
    opus_int transition, curr_block, tot_blocks;
169
47.9M
    SAVE_STACK;
170
171
47.9M
    celt_assert( encControl->nChannelsAPI >= encControl->nChannelsInternal && encControl->nChannelsAPI >= psEnc->nChannelsInternal );
172
47.9M
    if (encControl->reducedDependency)
173
14.7M
    {
174
36.1M
       for( n = 0; n < encControl->nChannelsAPI; n++ ) {
175
21.3M
           psEnc->state_Fxx[ n ].sCmn.first_frame_after_reset = 1;
176
21.3M
       }
177
14.7M
    }
178
112M
    for( n = 0; n < encControl->nChannelsAPI; n++ ) {
179
64.1M
        psEnc->state_Fxx[ n ].sCmn.nFramesEncoded = 0;
180
64.1M
    }
181
    /* Check values in encoder control structure */
182
47.9M
    if( ( ret = check_control_input( encControl ) ) != 0 ) {
183
0
        celt_assert( 0 );
184
0
        RESTORE_STACK;
185
0
        return ret;
186
0
    }
187
188
47.9M
    encControl->switchReady = 0;
189
190
47.9M
    if( encControl->nChannelsInternal > psEnc->nChannelsInternal ) {
191
        /* Mono -> Stereo transition: init state of second channel and stereo state */
192
53.9k
        ret += silk_init_encoder( &psEnc->state_Fxx[ 1 ], psEnc->state_Fxx[ 0 ].sCmn.arch );
193
53.9k
        silk_memset( psEnc->sStereo.pred_prev_Q13, 0, sizeof( psEnc->sStereo.pred_prev_Q13 ) );
194
53.9k
        silk_memset( psEnc->sStereo.sSide, 0, sizeof( psEnc->sStereo.sSide ) );
195
53.9k
        psEnc->sStereo.mid_side_amp_Q0[ 0 ] = 0;
196
53.9k
        psEnc->sStereo.mid_side_amp_Q0[ 1 ] = 1;
197
53.9k
        psEnc->sStereo.mid_side_amp_Q0[ 2 ] = 0;
198
53.9k
        psEnc->sStereo.mid_side_amp_Q0[ 3 ] = 1;
199
53.9k
        psEnc->sStereo.width_prev_Q14 = 0;
200
53.9k
        psEnc->sStereo.smth_width_Q14 = SILK_FIX_CONST( 1, 14 );
201
53.9k
        if( psEnc->nChannelsAPI == 2 ) {
202
0
            silk_memcpy( &psEnc->state_Fxx[ 1 ].sCmn.resampler_state, &psEnc->state_Fxx[ 0 ].sCmn.resampler_state, sizeof( silk_resampler_state_struct ) );
203
0
            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 ) );
204
0
        }
205
53.9k
    }
206
207
47.9M
    transition = (encControl->payloadSize_ms != psEnc->state_Fxx[ 0 ].sCmn.PacketSize_ms) || (psEnc->nChannelsInternal != encControl->nChannelsInternal);
208
209
47.9M
    psEnc->nChannelsAPI = encControl->nChannelsAPI;
210
47.9M
    psEnc->nChannelsInternal = encControl->nChannelsInternal;
211
212
47.9M
    nBlocksOf10ms = silk_DIV32( 100 * nSamplesIn, encControl->API_sampleRate );
213
47.9M
    tot_blocks = ( nBlocksOf10ms > 1 ) ? nBlocksOf10ms >> 1 : 1;
214
47.9M
    curr_block = 0;
215
47.9M
    if( prefillFlag ) {
216
1.10k
        silk_LP_state save_LP;
217
        /* Only accept input length of 10 ms */
218
1.10k
        if( nBlocksOf10ms != 1 ) {
219
0
            celt_assert( 0 );
220
0
            RESTORE_STACK;
221
0
            return SILK_ENC_INPUT_INVALID_NO_OF_SAMPLES;
222
0
        }
223
1.10k
        if ( prefillFlag == 2 ) {
224
348
            save_LP = psEnc->state_Fxx[ 0 ].sCmn.sLP;
225
            /* Save the sampling rate so the bandwidth switching code can keep handling transitions. */
226
348
            save_LP.saved_fs_kHz = psEnc->state_Fxx[ 0 ].sCmn.fs_kHz;
227
348
        }
228
        /* Reset Encoder */
229
2.43k
        for( n = 0; n < encControl->nChannelsInternal; n++ ) {
230
1.33k
            ret = silk_init_encoder( &psEnc->state_Fxx[ n ], psEnc->state_Fxx[ n ].sCmn.arch );
231
            /* Restore the variable LP state. */
232
1.33k
            if ( prefillFlag == 2 ) {
233
411
                psEnc->state_Fxx[ n ].sCmn.sLP = save_LP;
234
411
            }
235
1.33k
            celt_assert( !ret );
236
1.33k
        }
237
1.10k
        tmp_payloadSize_ms = encControl->payloadSize_ms;
238
1.10k
        encControl->payloadSize_ms = 10;
239
1.10k
        tmp_complexity = encControl->complexity;
240
1.10k
        encControl->complexity = 0;
241
2.43k
        for( n = 0; n < encControl->nChannelsInternal; n++ ) {
242
1.33k
            psEnc->state_Fxx[ n ].sCmn.controlled_since_last_payload = 0;
243
1.33k
            psEnc->state_Fxx[ n ].sCmn.prefillFlag = 1;
244
1.33k
        }
245
47.9M
    } else {
246
        /* Only accept input lengths that are a multiple of 10 ms */
247
47.9M
        if( nBlocksOf10ms * encControl->API_sampleRate != 100 * nSamplesIn || nSamplesIn < 0 ) {
248
0
            celt_assert( 0 );
249
0
            RESTORE_STACK;
250
0
            return SILK_ENC_INPUT_INVALID_NO_OF_SAMPLES;
251
0
        }
252
        /* Make sure no more than one packet can be produced */
253
47.9M
        if( 1000 * (opus_int32)nSamplesIn > encControl->payloadSize_ms * encControl->API_sampleRate ) {
254
0
            celt_assert( 0 );
255
0
            RESTORE_STACK;
256
0
            return SILK_ENC_INPUT_INVALID_NO_OF_SAMPLES;
257
0
        }
258
47.9M
    }
259
260
106M
    for( n = 0; n < encControl->nChannelsInternal; n++ ) {
261
        /* Force the side channel to the same rate as the mid */
262
59.0M
        opus_int force_fs_kHz = (n==1) ? psEnc->state_Fxx[0].sCmn.fs_kHz : 0;
263
59.0M
        if( ( ret = silk_control_encoder( &psEnc->state_Fxx[ n ], encControl, psEnc->allowBandwidthSwitch, n, force_fs_kHz ) ) != 0 ) {
264
0
            silk_assert( 0 );
265
0
            RESTORE_STACK;
266
0
            return ret;
267
0
        }
268
59.0M
        if( psEnc->state_Fxx[n].sCmn.first_frame_after_reset || transition ) {
269
44.8M
            for( i = 0; i < psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket; i++ ) {
270
24.6M
                psEnc->state_Fxx[ n ].sCmn.LBRR_flags[ i ] = 0;
271
24.6M
            }
272
20.1M
        }
273
59.0M
        psEnc->state_Fxx[ n ].sCmn.inDTX = psEnc->state_Fxx[ n ].sCmn.useDTX;
274
59.0M
    }
275
47.9M
    celt_assert( encControl->nChannelsInternal == 1 || psEnc->state_Fxx[ 0 ].sCmn.fs_kHz == psEnc->state_Fxx[ 1 ].sCmn.fs_kHz );
276
277
    /* Input buffering/resampling and encoding */
278
47.9M
    nSamplesToBufferMax =
279
47.9M
        10 * nBlocksOf10ms * psEnc->state_Fxx[ 0 ].sCmn.fs_kHz;
280
47.9M
    nSamplesFromInputMax =
281
47.9M
        silk_DIV32_16( nSamplesToBufferMax *
282
47.9M
                           psEnc->state_Fxx[ 0 ].sCmn.API_fs_Hz,
283
47.9M
                       psEnc->state_Fxx[ 0 ].sCmn.fs_kHz * 1000 );
284
47.9M
    ALLOC( buf, nSamplesFromInputMax, opus_int16 );
285
57.8M
    while( 1 ) {
286
57.8M
        int curr_nBitsUsedLBRR = 0;
287
57.8M
        nSamplesToBuffer  = psEnc->state_Fxx[ 0 ].sCmn.frame_length - psEnc->state_Fxx[ 0 ].sCmn.inputBufIx;
288
57.8M
        nSamplesToBuffer  = silk_min( nSamplesToBuffer, nSamplesToBufferMax );
289
57.8M
        nSamplesFromInput = silk_DIV32_16( nSamplesToBuffer * psEnc->state_Fxx[ 0 ].sCmn.API_fs_Hz, psEnc->state_Fxx[ 0 ].sCmn.fs_kHz * 1000 );
290
        /* Resample and write to buffer */
291
57.8M
        if( encControl->nChannelsAPI == 2 && encControl->nChannelsInternal == 2 ) {
292
13.3M
            opus_int id = psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded;
293
4.22G
            for( n = 0; n < nSamplesFromInput; n++ ) {
294
4.21G
                buf[ n ] = RES2INT16(samplesIn[ 2 * n ]);
295
4.21G
            }
296
            /* Making sure to start both resamplers from the same state when switching from mono to stereo */
297
13.3M
            if( psEnc->nPrevChannelsInternal == 1 && id==0 ) {
298
0
               silk_memcpy( &psEnc->state_Fxx[ 1 ].sCmn.resampler_state, &psEnc->state_Fxx[ 0 ].sCmn.resampler_state, sizeof(psEnc->state_Fxx[ 1 ].sCmn.resampler_state));
299
0
            }
300
301
13.3M
            ret += silk_resampler( &psEnc->state_Fxx[ 0 ].sCmn.resampler_state,
302
13.3M
                &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput );
303
13.3M
            psEnc->state_Fxx[ 0 ].sCmn.inputBufIx += nSamplesToBuffer;
304
305
13.3M
            nSamplesToBuffer  = psEnc->state_Fxx[ 1 ].sCmn.frame_length - psEnc->state_Fxx[ 1 ].sCmn.inputBufIx;
306
13.3M
            nSamplesToBuffer  = silk_min( nSamplesToBuffer, 10 * nBlocksOf10ms * psEnc->state_Fxx[ 1 ].sCmn.fs_kHz );
307
4.22G
            for( n = 0; n < nSamplesFromInput; n++ ) {
308
4.21G
                buf[ n ] = RES2INT16(samplesIn[ 2 * n + 1 ]);
309
4.21G
            }
310
13.3M
            ret += silk_resampler( &psEnc->state_Fxx[ 1 ].sCmn.resampler_state,
311
13.3M
                &psEnc->state_Fxx[ 1 ].sCmn.inputBuf[ psEnc->state_Fxx[ 1 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput );
312
313
13.3M
            psEnc->state_Fxx[ 1 ].sCmn.inputBufIx += nSamplesToBuffer;
314
44.5M
        } else if( encControl->nChannelsAPI == 2 && encControl->nChannelsInternal == 1 ) {
315
            /* Combine left and right channels before resampling */
316
1.71G
            for( n = 0; n < nSamplesFromInput; n++ ) {
317
1.70G
                sum = RES2INT16(samplesIn[ 2 * n ] + samplesIn[ 2 * n + 1 ]);
318
1.70G
                buf[ n ] = (opus_int16)silk_RSHIFT_ROUND( sum,  1 );
319
1.70G
            }
320
6.14M
            ret += silk_resampler( &psEnc->state_Fxx[ 0 ].sCmn.resampler_state,
321
6.14M
                &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput );
322
            /* On the first mono frame, average the results for the two resampler states  */
323
6.14M
            if( psEnc->nPrevChannelsInternal == 2 && psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded == 0 ) {
324
0
               ret += silk_resampler( &psEnc->state_Fxx[ 1 ].sCmn.resampler_state,
325
0
                   &psEnc->state_Fxx[ 1 ].sCmn.inputBuf[ psEnc->state_Fxx[ 1 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput );
326
0
               for( n = 0; n < psEnc->state_Fxx[ 0 ].sCmn.frame_length; n++ ) {
327
0
                  psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx+n+2 ] =
328
0
                        silk_RSHIFT(psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx+n+2 ]
329
0
                                  + psEnc->state_Fxx[ 1 ].sCmn.inputBuf[ psEnc->state_Fxx[ 1 ].sCmn.inputBufIx+n+2 ], 1);
330
0
               }
331
0
            }
332
6.14M
            psEnc->state_Fxx[ 0 ].sCmn.inputBufIx += nSamplesToBuffer;
333
38.3M
        } else {
334
38.3M
            celt_assert( encControl->nChannelsAPI == 1 && encControl->nChannelsInternal == 1 );
335
11.7G
            for( n = 0; n < nSamplesFromInput; n++ ) {
336
11.6G
                buf[n] = RES2INT16(samplesIn[n]);
337
11.6G
            }
338
38.3M
            ret += silk_resampler( &psEnc->state_Fxx[ 0 ].sCmn.resampler_state,
339
38.3M
                &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput );
340
38.3M
            psEnc->state_Fxx[ 0 ].sCmn.inputBufIx += nSamplesToBuffer;
341
38.3M
        }
342
343
57.8M
        samplesIn  += nSamplesFromInput * encControl->nChannelsAPI;
344
57.8M
        nSamplesIn -= nSamplesFromInput;
345
346
        /* Default */
347
57.8M
        psEnc->allowBandwidthSwitch = 0;
348
349
        /* Silk encoder */
350
57.8M
        if( psEnc->state_Fxx[ 0 ].sCmn.inputBufIx >= psEnc->state_Fxx[ 0 ].sCmn.frame_length ) {
351
            /* Enough data in input buffer, so encode */
352
57.8M
            celt_assert( psEnc->state_Fxx[ 0 ].sCmn.inputBufIx == psEnc->state_Fxx[ 0 ].sCmn.frame_length );
353
57.8M
            celt_assert( encControl->nChannelsInternal == 1 || psEnc->state_Fxx[ 1 ].sCmn.inputBufIx == psEnc->state_Fxx[ 1 ].sCmn.frame_length );
354
355
            /* Deal with LBRR data */
356
57.8M
            if( psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded == 0 && !prefillFlag ) {
357
                /* Create space at start of payload for VAD and FEC flags */
358
47.9M
                opus_uint8 iCDF[ 2 ] = { 0, 0 };
359
47.9M
                iCDF[ 0 ] = 256 - silk_RSHIFT( 256, ( psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket + 1 ) * encControl->nChannelsInternal );
360
47.9M
                ec_enc_icdf( psRangeEnc, 0, iCDF, 8 );
361
47.9M
                curr_nBitsUsedLBRR = ec_tell( psRangeEnc );
362
363
                /* Encode any LBRR data from previous packet */
364
                /* Encode LBRR flags */
365
106M
                for( n = 0; n < encControl->nChannelsInternal; n++ ) {
366
58.9M
                    LBRR_symbol = 0;
367
130M
                    for( i = 0; i < psEnc->state_Fxx[ n ].sCmn.nFramesPerPacket; i++ ) {
368
71.1M
                        LBRR_symbol |= silk_LSHIFT( psEnc->state_Fxx[ n ].sCmn.LBRR_flags[ i ], i );
369
71.1M
                    }
370
58.9M
                    psEnc->state_Fxx[ n ].sCmn.LBRR_flag = LBRR_symbol > 0 ? 1 : 0;
371
58.9M
                    if( LBRR_symbol && psEnc->state_Fxx[ n ].sCmn.nFramesPerPacket > 1 ) {
372
51.9k
                        ec_enc_icdf( psRangeEnc, LBRR_symbol - 1, silk_LBRR_flags_iCDF_ptr[ psEnc->state_Fxx[ n ].sCmn.nFramesPerPacket - 2 ], 8 );
373
51.9k
                    }
374
58.9M
                }
375
376
                /* Code LBRR indices and excitation signals */
377
105M
                for( i = 0; i < psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket; i++ ) {
378
128M
                    for( n = 0; n < encControl->nChannelsInternal; n++ ) {
379
71.1M
                        if( psEnc->state_Fxx[ n ].sCmn.LBRR_flags[ i ] ) {
380
322k
                            opus_int condCoding;
381
382
322k
                            if( encControl->nChannelsInternal == 2 && n == 0 ) {
383
92.4k
                                silk_stereo_encode_pred( psRangeEnc, psEnc->sStereo.predIx[ i ] );
384
                                /* For LBRR data there's no need to code the mid-only flag if the side-channel LBRR flag is set */
385
92.4k
                                if( psEnc->state_Fxx[ 1 ].sCmn.LBRR_flags[ i ] == 0 ) {
386
36.3k
                                    silk_stereo_encode_mid_only( psRangeEnc, psEnc->sStereo.mid_only_flags[ i ] );
387
36.3k
                                }
388
92.4k
                            }
389
                            /* Use conditional coding if previous frame available */
390
322k
                            if( i > 0 && psEnc->state_Fxx[ n ].sCmn.LBRR_flags[ i - 1 ] ) {
391
62.8k
                                condCoding = CODE_CONDITIONALLY;
392
259k
                            } else {
393
259k
                                condCoding = CODE_INDEPENDENTLY;
394
259k
                            }
395
322k
                            silk_encode_indices( &psEnc->state_Fxx[ n ].sCmn, psRangeEnc, i, 1, condCoding );
396
322k
                            silk_encode_pulses( psRangeEnc, psEnc->state_Fxx[ n ].sCmn.indices_LBRR[i].signalType, psEnc->state_Fxx[ n ].sCmn.indices_LBRR[i].quantOffsetType,
397
322k
                                psEnc->state_Fxx[ n ].sCmn.pulses_LBRR[ i ], psEnc->state_Fxx[ n ].sCmn.frame_length );
398
322k
                        }
399
71.1M
                    }
400
57.8M
                }
401
402
                /* Reset LBRR flags */
403
106M
                for( n = 0; n < encControl->nChannelsInternal; n++ ) {
404
58.9M
                    silk_memset( psEnc->state_Fxx[ n ].sCmn.LBRR_flags, 0, sizeof( psEnc->state_Fxx[ n ].sCmn.LBRR_flags ) );
405
58.9M
                }
406
47.9M
                curr_nBitsUsedLBRR = ec_tell( psRangeEnc ) - curr_nBitsUsedLBRR;
407
47.9M
            }
408
409
57.8M
            silk_HP_variable_cutoff( psEnc->state_Fxx );
410
411
            /* Total target bits for packet */
412
57.8M
            nBits = silk_DIV32_16( silk_MUL( encControl->bitRate, encControl->payloadSize_ms ), 1000 );
413
            /* Subtract bits used for LBRR */
414
57.8M
            if( !prefillFlag ) {
415
                /* psEnc->nBitsUsedLBRR is an exponential moving average of the LBRR usage,
416
                   except that for the first LBRR frame it does no averaging and for the first
417
                   frame after after LBRR, it goes back to zero immediately. */
418
57.8M
                if ( curr_nBitsUsedLBRR < 10 ) {
419
57.6M
                    psEnc->nBitsUsedLBRR = 0;
420
57.6M
                } else if ( psEnc->nBitsUsedLBRR < 10) {
421
54.8k
                    psEnc->nBitsUsedLBRR = curr_nBitsUsedLBRR;
422
157k
                } else {
423
157k
                    psEnc->nBitsUsedLBRR = ( psEnc->nBitsUsedLBRR + curr_nBitsUsedLBRR ) / 2;
424
157k
                }
425
57.8M
                nBits -= psEnc->nBitsUsedLBRR;
426
57.8M
            }
427
            /* Divide by number of uncoded frames left in packet */
428
57.8M
            nBits = silk_DIV32_16( nBits, psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket );
429
            /* Convert to bits/second */
430
57.8M
            if( encControl->payloadSize_ms == 10 ) {
431
19.6M
                TargetRate_bps = silk_SMULBB( nBits, 100 );
432
38.2M
            } else {
433
38.2M
                TargetRate_bps = silk_SMULBB( nBits, 50 );
434
38.2M
            }
435
            /* Subtract fraction of bits in excess of target in previous frames and packets */
436
57.8M
            TargetRate_bps -= silk_DIV32_16( silk_MUL( psEnc->nBitsExceeded, 1000 ), BITRESERVOIR_DECAY_TIME_MS );
437
57.8M
            if( !prefillFlag && psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded > 0 ) {
438
                /* Compare actual vs target bits so far in this packet */
439
9.91M
                opus_int32 bitsBalance = ec_tell( psRangeEnc ) - psEnc->nBitsUsedLBRR - nBits * psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded;
440
9.91M
                TargetRate_bps -= silk_DIV32_16( silk_MUL( bitsBalance, 1000 ), BITRESERVOIR_DECAY_TIME_MS );
441
9.91M
            }
442
            /* Never exceed input bitrate */
443
57.8M
            TargetRate_bps = silk_LIMIT( TargetRate_bps, encControl->bitRate, 5000 );
444
445
            /* Convert Left/Right to Mid/Side */
446
57.8M
            if( encControl->nChannelsInternal == 2 ) {
447
13.3M
                silk_stereo_LR_to_MS( &psEnc->sStereo, &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ 2 ], &psEnc->state_Fxx[ 1 ].sCmn.inputBuf[ 2 ],
448
13.3M
                    psEnc->sStereo.predIx[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ], &psEnc->sStereo.mid_only_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ],
449
13.3M
                    MStargetRates_bps, TargetRate_bps, psEnc->state_Fxx[ 0 ].sCmn.speech_activity_Q8, encControl->toMono,
450
13.3M
                    psEnc->state_Fxx[ 0 ].sCmn.fs_kHz, psEnc->state_Fxx[ 0 ].sCmn.frame_length );
451
13.3M
                if( psEnc->sStereo.mid_only_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] == 0 ) {
452
                    /* Reset side channel encoder memory for first frame with side coding */
453
9.42M
                    if( psEnc->prev_decode_only_middle == 1 ) {
454
2.60k
                        silk_memset( &psEnc->state_Fxx[ 1 ].sShape,               0, sizeof( psEnc->state_Fxx[ 1 ].sShape ) );
455
2.60k
                        silk_memset( &psEnc->state_Fxx[ 1 ].sCmn.sNSQ,            0, sizeof( psEnc->state_Fxx[ 1 ].sCmn.sNSQ ) );
456
2.60k
                        silk_memset( psEnc->state_Fxx[ 1 ].sCmn.prev_NLSFq_Q15,   0, sizeof( psEnc->state_Fxx[ 1 ].sCmn.prev_NLSFq_Q15 ) );
457
2.60k
                        silk_memset( &psEnc->state_Fxx[ 1 ].sCmn.sLP.In_LP_State, 0, sizeof( psEnc->state_Fxx[ 1 ].sCmn.sLP.In_LP_State ) );
458
2.60k
                        psEnc->state_Fxx[ 1 ].sCmn.prevLag                 = 100;
459
2.60k
                        psEnc->state_Fxx[ 1 ].sCmn.sNSQ.lagPrev            = 100;
460
2.60k
                        psEnc->state_Fxx[ 1 ].sShape.LastGainIndex         = 10;
461
2.60k
                        psEnc->state_Fxx[ 1 ].sCmn.prevSignalType          = TYPE_NO_VOICE_ACTIVITY;
462
2.60k
                        psEnc->state_Fxx[ 1 ].sCmn.sNSQ.prev_gain_Q16      = 65536;
463
2.60k
                        psEnc->state_Fxx[ 1 ].sCmn.first_frame_after_reset = 1;
464
2.60k
                    }
465
9.42M
                    silk_encode_do_VAD_Fxx( &psEnc->state_Fxx[ 1 ], activity );
466
9.42M
                } else {
467
3.88M
                    psEnc->state_Fxx[ 1 ].sCmn.VAD_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] = 0;
468
3.88M
                }
469
13.3M
                if( !prefillFlag ) {
470
13.3M
                    silk_stereo_encode_pred( psRangeEnc, psEnc->sStereo.predIx[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] );
471
13.3M
                    if( psEnc->state_Fxx[ 1 ].sCmn.VAD_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] == 0 ) {
472
13.0M
                        silk_stereo_encode_mid_only( psRangeEnc, psEnc->sStereo.mid_only_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] );
473
13.0M
                    }
474
13.3M
                }
475
44.5M
            } else {
476
                /* Buffering */
477
44.5M
                silk_memcpy( psEnc->state_Fxx[ 0 ].sCmn.inputBuf, psEnc->sStereo.sMid, 2 * sizeof( opus_int16 ) );
478
44.5M
                silk_memcpy( psEnc->sStereo.sMid, &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.frame_length ], 2 * sizeof( opus_int16 ) );
479
44.5M
            }
480
57.8M
            silk_encode_do_VAD_Fxx( &psEnc->state_Fxx[ 0 ], activity );
481
482
            /* Encode */
483
128M
            for( n = 0; n < encControl->nChannelsInternal; n++ ) {
484
71.1M
                opus_int maxBits, useCBR;
485
486
                /* Handling rate constraints */
487
71.1M
                maxBits = encControl->maxBits;
488
71.1M
                if( tot_blocks == 2 && curr_block == 0 ) {
489
6.10M
                    maxBits = maxBits * 3 / 5;
490
65.0M
                } else if( tot_blocks == 3 ) {
491
9.06M
                    if( curr_block == 0 ) {
492
3.02M
                        maxBits = maxBits * 2 / 5;
493
6.04M
                    } else if( curr_block == 1 ) {
494
3.02M
                        maxBits = maxBits * 3 / 4;
495
3.02M
                    }
496
9.06M
                }
497
71.1M
                useCBR = encControl->useCBR && curr_block == tot_blocks - 1;
498
499
71.1M
                if( encControl->nChannelsInternal == 1 ) {
500
44.5M
                    channelRate_bps = TargetRate_bps;
501
44.5M
                } else {
502
26.6M
                    channelRate_bps = MStargetRates_bps[ n ];
503
26.6M
                    if( n == 0 && MStargetRates_bps[ 1 ] > 0 ) {
504
9.42M
                        useCBR = 0;
505
                        /* Give mid up to 1/2 of the max bits for that frame */
506
9.42M
                        maxBits -= encControl->maxBits / ( tot_blocks * 2 );
507
9.42M
                    }
508
26.6M
                }
509
510
71.1M
                if( channelRate_bps > 0 ) {
511
67.2M
                    opus_int condCoding;
512
513
67.2M
                    silk_control_SNR( &psEnc->state_Fxx[ n ].sCmn, channelRate_bps );
514
515
                    /* Use independent coding if no previous frame available */
516
67.2M
                    if( psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded - n <= 0 ) {
517
55.7M
                        condCoding = CODE_INDEPENDENTLY;
518
55.7M
                    } else if( n > 0 && psEnc->prev_decode_only_middle ) {
519
                        /* If we skipped a side frame in this packet, we don't
520
                           need LTP scaling; the LTP state is well-defined. */
521
1.41k
                        condCoding = CODE_INDEPENDENTLY_NO_LTP_SCALING;
522
11.5M
                    } else {
523
11.5M
                        condCoding = CODE_CONDITIONALLY;
524
11.5M
                    }
525
67.2M
                    if( ( ret = silk_encode_frame_Fxx( &psEnc->state_Fxx[ n ], nBytesOut, psRangeEnc, condCoding, maxBits, useCBR ) ) != 0 ) {
526
0
                        silk_assert( 0 );
527
0
                    }
528
67.2M
                }
529
71.1M
                psEnc->state_Fxx[ n ].sCmn.controlled_since_last_payload = 0;
530
71.1M
                psEnc->state_Fxx[ n ].sCmn.inputBufIx = 0;
531
71.1M
                psEnc->state_Fxx[ n ].sCmn.nFramesEncoded++;
532
71.1M
            }
533
57.8M
            psEnc->prev_decode_only_middle = psEnc->sStereo.mid_only_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded - 1 ];
534
535
            /* Insert VAD and FEC flags at beginning of bitstream */
536
57.8M
            if( *nBytesOut > 0 && psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded == psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket) {
537
47.9M
                flags = 0;
538
106M
                for( n = 0; n < encControl->nChannelsInternal; n++ ) {
539
130M
                    for( i = 0; i < psEnc->state_Fxx[ n ].sCmn.nFramesPerPacket; i++ ) {
540
71.1M
                        flags  = silk_LSHIFT( flags, 1 );
541
71.1M
                        flags |= psEnc->state_Fxx[ n ].sCmn.VAD_flags[ i ];
542
71.1M
                    }
543
58.9M
                    flags  = silk_LSHIFT( flags, 1 );
544
58.9M
                    flags |= psEnc->state_Fxx[ n ].sCmn.LBRR_flag;
545
58.9M
                }
546
47.9M
                if( !prefillFlag ) {
547
47.9M
                    ec_enc_patch_initial_bits( psRangeEnc, flags, ( psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket + 1 ) * encControl->nChannelsInternal );
548
47.9M
                }
549
550
                /* Return zero bytes if all channels DTXed */
551
47.9M
                if( psEnc->state_Fxx[ 0 ].sCmn.inDTX && ( encControl->nChannelsInternal == 1 || psEnc->state_Fxx[ 1 ].sCmn.inDTX ) ) {
552
79.3k
                    *nBytesOut = 0;
553
79.3k
                }
554
555
47.9M
                psEnc->nBitsExceeded += *nBytesOut * 8;
556
47.9M
                psEnc->nBitsExceeded -= silk_DIV32_16( silk_MUL( encControl->bitRate, encControl->payloadSize_ms ), 1000 );
557
47.9M
                psEnc->nBitsExceeded  = silk_LIMIT( psEnc->nBitsExceeded, 0, 10000 );
558
559
                /* Update flag indicating if bandwidth switching is allowed */
560
47.9M
                speech_act_thr_for_switch_Q8 = silk_SMLAWB( SILK_FIX_CONST( SPEECH_ACTIVITY_DTX_THRES, 8 ),
561
47.9M
                    SILK_FIX_CONST( ( 1 - SPEECH_ACTIVITY_DTX_THRES ) / MAX_BANDWIDTH_SWITCH_DELAY_MS, 16 + 8 ), psEnc->timeSinceSwitchAllowed_ms );
562
47.9M
                if( psEnc->state_Fxx[ 0 ].sCmn.speech_activity_Q8 < speech_act_thr_for_switch_Q8 ) {
563
46.7M
                    psEnc->allowBandwidthSwitch = 1;
564
46.7M
                    psEnc->timeSinceSwitchAllowed_ms = 0;
565
46.7M
                } else {
566
1.17M
                    psEnc->allowBandwidthSwitch = 0;
567
1.17M
                    psEnc->timeSinceSwitchAllowed_ms += encControl->payloadSize_ms;
568
1.17M
                }
569
47.9M
            }
570
571
57.8M
            if( nSamplesIn == 0 ) {
572
47.9M
                break;
573
47.9M
            }
574
57.8M
        } else {
575
0
            break;
576
0
        }
577
9.91M
        curr_block++;
578
9.91M
    }
579
580
47.9M
    psEnc->nPrevChannelsInternal = encControl->nChannelsInternal;
581
582
47.9M
    encControl->allowBandwidthSwitch = psEnc->allowBandwidthSwitch;
583
47.9M
    encControl->inWBmodeWithoutVariableLP = psEnc->state_Fxx[ 0 ].sCmn.fs_kHz == 16 && psEnc->state_Fxx[ 0 ].sCmn.sLP.mode == 0;
584
47.9M
    encControl->internalSampleRate = silk_SMULBB( psEnc->state_Fxx[ 0 ].sCmn.fs_kHz, 1000 );
585
47.9M
    encControl->stereoWidth_Q14 = encControl->toMono ? 0 : psEnc->sStereo.smth_width_Q14;
586
47.9M
    if( prefillFlag ) {
587
1.10k
        encControl->payloadSize_ms = tmp_payloadSize_ms;
588
1.10k
        encControl->complexity = tmp_complexity;
589
2.43k
        for( n = 0; n < encControl->nChannelsInternal; n++ ) {
590
1.33k
            psEnc->state_Fxx[ n ].sCmn.controlled_since_last_payload = 0;
591
1.33k
            psEnc->state_Fxx[ n ].sCmn.prefillFlag = 0;
592
1.33k
        }
593
1.10k
    }
594
595
47.9M
    encControl->signalType = psEnc->state_Fxx[0].sCmn.indices.signalType;
596
47.9M
    encControl->offset = silk_Quantization_Offsets_Q10
597
47.9M
                         [ psEnc->state_Fxx[0].sCmn.indices.signalType >> 1 ]
598
47.9M
                         [ psEnc->state_Fxx[0].sCmn.indices.quantOffsetType ];
599
47.9M
    RESTORE_STACK;
600
47.9M
    return ret;
601
47.9M
}