Coverage Report

Created: 2025-11-11 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.62M
{
65
2.62M
    opus_int ret = SILK_NO_ERROR;
66
67
2.62M
    *encSizeBytes = sizeof( silk_encoder );
68
    /* Skip second encoder state for mono. */
69
2.62M
    if ( channels == 1 ) {
70
1.40M
        *encSizeBytes -= sizeof( silk_encoder_state_Fxx );
71
1.40M
    }
72
73
2.62M
    return ret;
74
2.62M
}
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
514k
{
86
514k
    silk_encoder *psEnc;
87
514k
    opus_int n, ret = SILK_NO_ERROR;
88
89
514k
    psEnc = (silk_encoder *)encState;
90
91
    /* Reset encoder. Skip second encoder state for mono. */
92
514k
    silk_memset( psEnc, 0, sizeof( silk_encoder ) - (channels==1)*sizeof( silk_encoder_state_Fxx ) );
93
1.19M
    for( n = 0; n < channels; n++ ) {
94
681k
        if( ret += silk_init_encoder( &psEnc->state_Fxx[ n ], arch ) ) {
95
0
            celt_assert( 0 );
96
0
        }
97
681k
    }
98
99
514k
    psEnc->nChannelsAPI = 1;
100
514k
    psEnc->nChannelsInternal = 1;
101
102
    /* Read control structure */
103
514k
    if( ret += silk_QueryEncoder( encState, encStatus ) ) {
104
0
        celt_assert( 0 );
105
0
    }
106
107
514k
    return ret;
108
514k
}
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
1.02M
{
118
1.02M
    opus_int ret = SILK_NO_ERROR;
119
1.02M
    silk_encoder_state_Fxx *state_Fxx;
120
1.02M
    silk_encoder *psEnc = (silk_encoder *)encState;
121
122
1.02M
    state_Fxx = psEnc->state_Fxx;
123
124
1.02M
    encStatus->nChannelsAPI              = psEnc->nChannelsAPI;
125
1.02M
    encStatus->nChannelsInternal         = psEnc->nChannelsInternal;
126
1.02M
    encStatus->API_sampleRate            = state_Fxx[ 0 ].sCmn.API_fs_Hz;
127
1.02M
    encStatus->maxInternalSampleRate     = state_Fxx[ 0 ].sCmn.maxInternal_fs_Hz;
128
1.02M
    encStatus->minInternalSampleRate     = state_Fxx[ 0 ].sCmn.minInternal_fs_Hz;
129
1.02M
    encStatus->desiredInternalSampleRate = state_Fxx[ 0 ].sCmn.desiredInternal_fs_Hz;
130
1.02M
    encStatus->payloadSize_ms            = state_Fxx[ 0 ].sCmn.PacketSize_ms;
131
1.02M
    encStatus->bitRate                   = state_Fxx[ 0 ].sCmn.TargetRate_bps;
132
1.02M
    encStatus->packetLossPercentage      = state_Fxx[ 0 ].sCmn.PacketLoss_perc;
133
1.02M
    encStatus->complexity                = state_Fxx[ 0 ].sCmn.Complexity;
134
1.02M
    encStatus->useInBandFEC              = state_Fxx[ 0 ].sCmn.useInBandFEC;
135
1.02M
    encStatus->useDTX                    = state_Fxx[ 0 ].sCmn.useDTX;
136
1.02M
    encStatus->useCBR                    = state_Fxx[ 0 ].sCmn.useCBR;
137
1.02M
    encStatus->internalSampleRate        = silk_SMULBB( state_Fxx[ 0 ].sCmn.fs_kHz, 1000 );
138
1.02M
    encStatus->allowBandwidthSwitch      = state_Fxx[ 0 ].sCmn.allow_bandwidth_switch;
139
1.02M
    encStatus->inWBmodeWithoutVariableLP = state_Fxx[ 0 ].sCmn.fs_kHz == 16 && state_Fxx[ 0 ].sCmn.sLP.mode == 0;
140
141
1.02M
    return ret;
142
1.02M
}
enc_API.c:silk_QueryEncoder
Line
Count
Source
117
514k
{
118
514k
    opus_int ret = SILK_NO_ERROR;
119
514k
    silk_encoder_state_Fxx *state_Fxx;
120
514k
    silk_encoder *psEnc = (silk_encoder *)encState;
121
122
514k
    state_Fxx = psEnc->state_Fxx;
123
124
514k
    encStatus->nChannelsAPI              = psEnc->nChannelsAPI;
125
514k
    encStatus->nChannelsInternal         = psEnc->nChannelsInternal;
126
514k
    encStatus->API_sampleRate            = state_Fxx[ 0 ].sCmn.API_fs_Hz;
127
514k
    encStatus->maxInternalSampleRate     = state_Fxx[ 0 ].sCmn.maxInternal_fs_Hz;
128
514k
    encStatus->minInternalSampleRate     = state_Fxx[ 0 ].sCmn.minInternal_fs_Hz;
129
514k
    encStatus->desiredInternalSampleRate = state_Fxx[ 0 ].sCmn.desiredInternal_fs_Hz;
130
514k
    encStatus->payloadSize_ms            = state_Fxx[ 0 ].sCmn.PacketSize_ms;
131
514k
    encStatus->bitRate                   = state_Fxx[ 0 ].sCmn.TargetRate_bps;
132
514k
    encStatus->packetLossPercentage      = state_Fxx[ 0 ].sCmn.PacketLoss_perc;
133
514k
    encStatus->complexity                = state_Fxx[ 0 ].sCmn.Complexity;
134
514k
    encStatus->useInBandFEC              = state_Fxx[ 0 ].sCmn.useInBandFEC;
135
514k
    encStatus->useDTX                    = state_Fxx[ 0 ].sCmn.useDTX;
136
514k
    encStatus->useCBR                    = state_Fxx[ 0 ].sCmn.useCBR;
137
514k
    encStatus->internalSampleRate        = silk_SMULBB( state_Fxx[ 0 ].sCmn.fs_kHz, 1000 );
138
514k
    encStatus->allowBandwidthSwitch      = state_Fxx[ 0 ].sCmn.allow_bandwidth_switch;
139
514k
    encStatus->inWBmodeWithoutVariableLP = state_Fxx[ 0 ].sCmn.fs_kHz == 16 && state_Fxx[ 0 ].sCmn.sLP.mode == 0;
140
141
514k
    return ret;
142
514k
}
enc_API.c:silk_QueryEncoder
Line
Count
Source
117
514k
{
118
514k
    opus_int ret = SILK_NO_ERROR;
119
514k
    silk_encoder_state_Fxx *state_Fxx;
120
514k
    silk_encoder *psEnc = (silk_encoder *)encState;
121
122
514k
    state_Fxx = psEnc->state_Fxx;
123
124
514k
    encStatus->nChannelsAPI              = psEnc->nChannelsAPI;
125
514k
    encStatus->nChannelsInternal         = psEnc->nChannelsInternal;
126
514k
    encStatus->API_sampleRate            = state_Fxx[ 0 ].sCmn.API_fs_Hz;
127
514k
    encStatus->maxInternalSampleRate     = state_Fxx[ 0 ].sCmn.maxInternal_fs_Hz;
128
514k
    encStatus->minInternalSampleRate     = state_Fxx[ 0 ].sCmn.minInternal_fs_Hz;
129
514k
    encStatus->desiredInternalSampleRate = state_Fxx[ 0 ].sCmn.desiredInternal_fs_Hz;
130
514k
    encStatus->payloadSize_ms            = state_Fxx[ 0 ].sCmn.PacketSize_ms;
131
514k
    encStatus->bitRate                   = state_Fxx[ 0 ].sCmn.TargetRate_bps;
132
514k
    encStatus->packetLossPercentage      = state_Fxx[ 0 ].sCmn.PacketLoss_perc;
133
514k
    encStatus->complexity                = state_Fxx[ 0 ].sCmn.Complexity;
134
514k
    encStatus->useInBandFEC              = state_Fxx[ 0 ].sCmn.useInBandFEC;
135
514k
    encStatus->useDTX                    = state_Fxx[ 0 ].sCmn.useDTX;
136
514k
    encStatus->useCBR                    = state_Fxx[ 0 ].sCmn.useCBR;
137
514k
    encStatus->internalSampleRate        = silk_SMULBB( state_Fxx[ 0 ].sCmn.fs_kHz, 1000 );
138
514k
    encStatus->allowBandwidthSwitch      = state_Fxx[ 0 ].sCmn.allow_bandwidth_switch;
139
514k
    encStatus->inWBmodeWithoutVariableLP = state_Fxx[ 0 ].sCmn.fs_kHz == 16 && state_Fxx[ 0 ].sCmn.sLP.mode == 0;
140
141
514k
    return ret;
142
514k
}
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
84.7M
{
161
84.7M
    opus_int   n, i, nBits, flags, tmp_payloadSize_ms = 0, tmp_complexity = 0, ret = 0;
162
84.7M
    opus_int   nSamplesToBuffer, nSamplesToBufferMax, nBlocksOf10ms;
163
84.7M
    opus_int   nSamplesFromInput = 0, nSamplesFromInputMax;
164
84.7M
    opus_int   speech_act_thr_for_switch_Q8;
165
84.7M
    opus_int32 TargetRate_bps, MStargetRates_bps[ 2 ], channelRate_bps, LBRR_symbol, sum;
166
84.7M
    silk_encoder *psEnc = ( silk_encoder * )encState;
167
84.7M
    VARDECL( opus_int16, buf );
168
84.7M
    opus_int transition, curr_block, tot_blocks;
169
84.7M
    SAVE_STACK;
170
171
84.7M
    celt_assert( encControl->nChannelsAPI >= encControl->nChannelsInternal && encControl->nChannelsAPI >= psEnc->nChannelsInternal );
172
84.7M
    if (encControl->reducedDependency)
173
25.6M
    {
174
62.1M
       for( n = 0; n < encControl->nChannelsAPI; n++ ) {
175
36.4M
           psEnc->state_Fxx[ n ].sCmn.first_frame_after_reset = 1;
176
36.4M
       }
177
25.6M
    }
178
199M
    for( n = 0; n < encControl->nChannelsAPI; n++ ) {
179
114M
        psEnc->state_Fxx[ n ].sCmn.nFramesEncoded = 0;
180
114M
    }
181
    /* Check values in encoder control structure */
182
84.7M
    if( ( ret = check_control_input( encControl ) ) != 0 ) {
183
0
        celt_assert( 0 );
184
0
        RESTORE_STACK;
185
0
        return ret;
186
0
    }
187
188
84.7M
    encControl->switchReady = 0;
189
190
84.7M
    if( encControl->nChannelsInternal > psEnc->nChannelsInternal ) {
191
        /* Mono -> Stereo transition: init state of second channel and stereo state */
192
115k
        ret += silk_init_encoder( &psEnc->state_Fxx[ 1 ], psEnc->state_Fxx[ 0 ].sCmn.arch );
193
115k
        silk_memset( psEnc->sStereo.pred_prev_Q13, 0, sizeof( psEnc->sStereo.pred_prev_Q13 ) );
194
115k
        silk_memset( psEnc->sStereo.sSide, 0, sizeof( psEnc->sStereo.sSide ) );
195
115k
        psEnc->sStereo.mid_side_amp_Q0[ 0 ] = 0;
196
115k
        psEnc->sStereo.mid_side_amp_Q0[ 1 ] = 1;
197
115k
        psEnc->sStereo.mid_side_amp_Q0[ 2 ] = 0;
198
115k
        psEnc->sStereo.mid_side_amp_Q0[ 3 ] = 1;
199
115k
        psEnc->sStereo.width_prev_Q14 = 0;
200
115k
        psEnc->sStereo.smth_width_Q14 = SILK_FIX_CONST( 1, 14 );
201
115k
        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
115k
    }
206
207
84.7M
    transition = (encControl->payloadSize_ms != psEnc->state_Fxx[ 0 ].sCmn.PacketSize_ms) || (psEnc->nChannelsInternal != encControl->nChannelsInternal);
208
209
84.7M
    psEnc->nChannelsAPI = encControl->nChannelsAPI;
210
84.7M
    psEnc->nChannelsInternal = encControl->nChannelsInternal;
211
212
84.7M
    nBlocksOf10ms = silk_DIV32( 100 * nSamplesIn, encControl->API_sampleRate );
213
84.7M
    tot_blocks = ( nBlocksOf10ms > 1 ) ? nBlocksOf10ms >> 1 : 1;
214
84.7M
    curr_block = 0;
215
84.7M
    if( prefillFlag ) {
216
1.99k
        silk_LP_state save_LP;
217
        /* Only accept input length of 10 ms */
218
1.99k
        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.99k
        if ( prefillFlag == 2 ) {
224
734
            save_LP = psEnc->state_Fxx[ 0 ].sCmn.sLP;
225
            /* Save the sampling rate so the bandwidth switching code can keep handling transitions. */
226
734
            save_LP.saved_fs_kHz = psEnc->state_Fxx[ 0 ].sCmn.fs_kHz;
227
734
        }
228
        /* Reset Encoder */
229
4.44k
        for( n = 0; n < encControl->nChannelsInternal; n++ ) {
230
2.44k
            ret = silk_init_encoder( &psEnc->state_Fxx[ n ], psEnc->state_Fxx[ n ].sCmn.arch );
231
            /* Restore the variable LP state. */
232
2.44k
            if ( prefillFlag == 2 ) {
233
906
                psEnc->state_Fxx[ n ].sCmn.sLP = save_LP;
234
906
            }
235
2.44k
            celt_assert( !ret );
236
2.44k
        }
237
1.99k
        tmp_payloadSize_ms = encControl->payloadSize_ms;
238
1.99k
        encControl->payloadSize_ms = 10;
239
1.99k
        tmp_complexity = encControl->complexity;
240
1.99k
        encControl->complexity = 0;
241
4.44k
        for( n = 0; n < encControl->nChannelsInternal; n++ ) {
242
2.44k
            psEnc->state_Fxx[ n ].sCmn.controlled_since_last_payload = 0;
243
2.44k
            psEnc->state_Fxx[ n ].sCmn.prefillFlag = 1;
244
2.44k
        }
245
84.7M
    } else {
246
        /* Only accept input lengths that are a multiple of 10 ms */
247
84.7M
        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
84.7M
        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
84.7M
    }
259
260
192M
    for( n = 0; n < encControl->nChannelsInternal; n++ ) {
261
        /* Force the side channel to the same rate as the mid */
262
107M
        opus_int force_fs_kHz = (n==1) ? psEnc->state_Fxx[0].sCmn.fs_kHz : 0;
263
107M
        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
107M
        if( psEnc->state_Fxx[n].sCmn.first_frame_after_reset || transition ) {
269
75.8M
            for( i = 0; i < psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket; i++ ) {
270
41.0M
                psEnc->state_Fxx[ n ].sCmn.LBRR_flags[ i ] = 0;
271
41.0M
            }
272
34.7M
        }
273
107M
        psEnc->state_Fxx[ n ].sCmn.inDTX = psEnc->state_Fxx[ n ].sCmn.useDTX;
274
107M
    }
275
84.7M
    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
84.7M
    nSamplesToBufferMax =
279
84.7M
        10 * nBlocksOf10ms * psEnc->state_Fxx[ 0 ].sCmn.fs_kHz;
280
84.7M
    nSamplesFromInputMax =
281
84.7M
        silk_DIV32_16( nSamplesToBufferMax *
282
84.7M
                           psEnc->state_Fxx[ 0 ].sCmn.API_fs_Hz,
283
84.7M
                       psEnc->state_Fxx[ 0 ].sCmn.fs_kHz * 1000 );
284
84.7M
    ALLOC( buf, nSamplesFromInputMax, opus_int16 );
285
98.6M
    while( 1 ) {
286
98.6M
        int curr_nBitsUsedLBRR = 0;
287
98.6M
        nSamplesToBuffer  = psEnc->state_Fxx[ 0 ].sCmn.frame_length - psEnc->state_Fxx[ 0 ].sCmn.inputBufIx;
288
98.6M
        nSamplesToBuffer  = silk_min( nSamplesToBuffer, nSamplesToBufferMax );
289
98.6M
        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
98.6M
        if( encControl->nChannelsAPI == 2 && encControl->nChannelsInternal == 2 ) {
292
26.1M
            opus_int id = psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded;
293
7.87G
            for( n = 0; n < nSamplesFromInput; n++ ) {
294
7.85G
                buf[ n ] = RES2INT16(samplesIn[ 2 * n ]);
295
7.85G
            }
296
            /* Making sure to start both resamplers from the same state when switching from mono to stereo */
297
26.1M
            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.1M
            ret += silk_resampler( &psEnc->state_Fxx[ 0 ].sCmn.resampler_state,
302
26.1M
                &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput );
303
26.1M
            psEnc->state_Fxx[ 0 ].sCmn.inputBufIx += nSamplesToBuffer;
304
305
26.1M
            nSamplesToBuffer  = psEnc->state_Fxx[ 1 ].sCmn.frame_length - psEnc->state_Fxx[ 1 ].sCmn.inputBufIx;
306
26.1M
            nSamplesToBuffer  = silk_min( nSamplesToBuffer, 10 * nBlocksOf10ms * psEnc->state_Fxx[ 1 ].sCmn.fs_kHz );
307
7.87G
            for( n = 0; n < nSamplesFromInput; n++ ) {
308
7.85G
                buf[ n ] = RES2INT16(samplesIn[ 2 * n + 1 ]);
309
7.85G
            }
310
26.1M
            ret += silk_resampler( &psEnc->state_Fxx[ 1 ].sCmn.resampler_state,
311
26.1M
                &psEnc->state_Fxx[ 1 ].sCmn.inputBuf[ psEnc->state_Fxx[ 1 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput );
312
313
26.1M
            psEnc->state_Fxx[ 1 ].sCmn.inputBufIx += nSamplesToBuffer;
314
72.4M
        } else if( encControl->nChannelsAPI == 2 && encControl->nChannelsInternal == 1 ) {
315
            /* Combine left and right channels before resampling */
316
2.55G
            for( n = 0; n < nSamplesFromInput; n++ ) {
317
2.54G
                sum = RES2INT16(samplesIn[ 2 * n ] + samplesIn[ 2 * n + 1 ]);
318
2.54G
                buf[ n ] = (opus_int16)silk_RSHIFT_ROUND( sum,  1 );
319
2.54G
            }
320
8.43M
            ret += silk_resampler( &psEnc->state_Fxx[ 0 ].sCmn.resampler_state,
321
8.43M
                &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
8.43M
            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
8.43M
            psEnc->state_Fxx[ 0 ].sCmn.inputBufIx += nSamplesToBuffer;
333
64.0M
        } else {
334
64.0M
            celt_assert( encControl->nChannelsAPI == 1 && encControl->nChannelsInternal == 1 );
335
19.1G
            for( n = 0; n < nSamplesFromInput; n++ ) {
336
19.0G
                buf[n] = RES2INT16(samplesIn[n]);
337
19.0G
            }
338
64.0M
            ret += silk_resampler( &psEnc->state_Fxx[ 0 ].sCmn.resampler_state,
339
64.0M
                &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput );
340
64.0M
            psEnc->state_Fxx[ 0 ].sCmn.inputBufIx += nSamplesToBuffer;
341
64.0M
        }
342
343
98.6M
        samplesIn  += nSamplesFromInput * encControl->nChannelsAPI;
344
98.6M
        nSamplesIn -= nSamplesFromInput;
345
346
        /* Default */
347
98.6M
        psEnc->allowBandwidthSwitch = 0;
348
349
        /* Silk encoder */
350
98.6M
        if( psEnc->state_Fxx[ 0 ].sCmn.inputBufIx >= psEnc->state_Fxx[ 0 ].sCmn.frame_length ) {
351
            /* Enough data in input buffer, so encode */
352
98.6M
            celt_assert( psEnc->state_Fxx[ 0 ].sCmn.inputBufIx == psEnc->state_Fxx[ 0 ].sCmn.frame_length );
353
98.6M
            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
98.6M
            if( psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded == 0 && !prefillFlag ) {
357
                /* Create space at start of payload for VAD and FEC flags */
358
84.7M
                opus_uint8 iCDF[ 2 ] = { 0, 0 };
359
84.7M
                iCDF[ 0 ] = 256 - silk_RSHIFT( 256, ( psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket + 1 ) * encControl->nChannelsInternal );
360
84.7M
                ec_enc_icdf( psRangeEnc, 0, iCDF, 8 );
361
84.7M
                curr_nBitsUsedLBRR = ec_tell( psRangeEnc );
362
363
                /* Encode any LBRR data from previous packet */
364
                /* Encode LBRR flags */
365
192M
                for( n = 0; n < encControl->nChannelsInternal; n++ ) {
366
107M
                    LBRR_symbol = 0;
367
232M
                    for( i = 0; i < psEnc->state_Fxx[ n ].sCmn.nFramesPerPacket; i++ ) {
368
124M
                        LBRR_symbol |= silk_LSHIFT( psEnc->state_Fxx[ n ].sCmn.LBRR_flags[ i ], i );
369
124M
                    }
370
107M
                    psEnc->state_Fxx[ n ].sCmn.LBRR_flag = LBRR_symbol > 0 ? 1 : 0;
371
107M
                    if( LBRR_symbol && psEnc->state_Fxx[ n ].sCmn.nFramesPerPacket > 1 ) {
372
134k
                        ec_enc_icdf( psRangeEnc, LBRR_symbol - 1, silk_LBRR_flags_iCDF_ptr[ psEnc->state_Fxx[ n ].sCmn.nFramesPerPacket - 2 ], 8 );
373
134k
                    }
374
107M
                }
375
376
                /* Code LBRR indices and excitation signals */
377
183M
                for( i = 0; i < psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket; i++ ) {
378
223M
                    for( n = 0; n < encControl->nChannelsInternal; n++ ) {
379
124M
                        if( psEnc->state_Fxx[ n ].sCmn.LBRR_flags[ i ] ) {
380
735k
                            opus_int condCoding;
381
382
735k
                            if( encControl->nChannelsInternal == 2 && n == 0 ) {
383
251k
                                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
251k
                                if( psEnc->state_Fxx[ 1 ].sCmn.LBRR_flags[ i ] == 0 ) {
386
124k
                                    silk_stereo_encode_mid_only( psRangeEnc, psEnc->sStereo.mid_only_flags[ i ] );
387
124k
                                }
388
251k
                            }
389
                            /* Use conditional coding if previous frame available */
390
735k
                            if( i > 0 && psEnc->state_Fxx[ n ].sCmn.LBRR_flags[ i - 1 ] ) {
391
152k
                                condCoding = CODE_CONDITIONALLY;
392
582k
                            } else {
393
582k
                                condCoding = CODE_INDEPENDENTLY;
394
582k
                            }
395
735k
                            silk_encode_indices( &psEnc->state_Fxx[ n ].sCmn, psRangeEnc, i, 1, condCoding );
396
735k
                            silk_encode_pulses( psRangeEnc, psEnc->state_Fxx[ n ].sCmn.indices_LBRR[i].signalType, psEnc->state_Fxx[ n ].sCmn.indices_LBRR[i].quantOffsetType,
397
735k
                                psEnc->state_Fxx[ n ].sCmn.pulses_LBRR[ i ], psEnc->state_Fxx[ n ].sCmn.frame_length );
398
735k
                        }
399
124M
                    }
400
98.6M
                }
401
402
                /* Reset LBRR flags */
403
192M
                for( n = 0; n < encControl->nChannelsInternal; n++ ) {
404
107M
                    silk_memset( psEnc->state_Fxx[ n ].sCmn.LBRR_flags, 0, sizeof( psEnc->state_Fxx[ n ].sCmn.LBRR_flags ) );
405
107M
                }
406
84.7M
                curr_nBitsUsedLBRR = ec_tell( psRangeEnc ) - curr_nBitsUsedLBRR;
407
84.7M
            }
408
409
98.6M
            silk_HP_variable_cutoff( psEnc->state_Fxx );
410
411
            /* Total target bits for packet */
412
98.6M
            nBits = silk_DIV32_16( silk_MUL( encControl->bitRate, encControl->payloadSize_ms ), 1000 );
413
            /* Subtract bits used for LBRR */
414
98.6M
            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
98.6M
                if ( curr_nBitsUsedLBRR < 10 ) {
419
98.1M
                    psEnc->nBitsUsedLBRR = 0;
420
98.1M
                } else if ( psEnc->nBitsUsedLBRR < 10) {
421
139k
                    psEnc->nBitsUsedLBRR = curr_nBitsUsedLBRR;
422
338k
                } else {
423
338k
                    psEnc->nBitsUsedLBRR = ( psEnc->nBitsUsedLBRR + curr_nBitsUsedLBRR ) / 2;
424
338k
                }
425
98.6M
                nBits -= psEnc->nBitsUsedLBRR;
426
98.6M
            }
427
            /* Divide by number of uncoded frames left in packet */
428
98.6M
            nBits = silk_DIV32_16( nBits, psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket );
429
            /* Convert to bits/second */
430
98.6M
            if( encControl->payloadSize_ms == 10 ) {
431
40.3M
                TargetRate_bps = silk_SMULBB( nBits, 100 );
432
58.2M
            } else {
433
58.2M
                TargetRate_bps = silk_SMULBB( nBits, 50 );
434
58.2M
            }
435
            /* Subtract fraction of bits in excess of target in previous frames and packets */
436
98.6M
            TargetRate_bps -= silk_DIV32_16( silk_MUL( psEnc->nBitsExceeded, 1000 ), BITRESERVOIR_DECAY_TIME_MS );
437
98.6M
            if( !prefillFlag && psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded > 0 ) {
438
                /* Compare actual vs target bits so far in this packet */
439
13.8M
                opus_int32 bitsBalance = ec_tell( psRangeEnc ) - psEnc->nBitsUsedLBRR - nBits * psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded;
440
13.8M
                TargetRate_bps -= silk_DIV32_16( silk_MUL( bitsBalance, 1000 ), BITRESERVOIR_DECAY_TIME_MS );
441
13.8M
            }
442
            /* Never exceed input bitrate */
443
98.6M
            TargetRate_bps = silk_LIMIT( TargetRate_bps, encControl->bitRate, 5000 );
444
445
            /* Convert Left/Right to Mid/Side */
446
98.6M
            if( encControl->nChannelsInternal == 2 ) {
447
26.1M
                silk_stereo_LR_to_MS( &psEnc->sStereo, &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ 2 ], &psEnc->state_Fxx[ 1 ].sCmn.inputBuf[ 2 ],
448
26.1M
                    psEnc->sStereo.predIx[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ], &psEnc->sStereo.mid_only_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ],
449
26.1M
                    MStargetRates_bps, TargetRate_bps, psEnc->state_Fxx[ 0 ].sCmn.speech_activity_Q8, encControl->toMono,
450
26.1M
                    psEnc->state_Fxx[ 0 ].sCmn.fs_kHz, psEnc->state_Fxx[ 0 ].sCmn.frame_length );
451
26.1M
                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
20.3M
                    if( psEnc->prev_decode_only_middle == 1 ) {
454
7.75k
                        silk_memset( &psEnc->state_Fxx[ 1 ].sShape,               0, sizeof( psEnc->state_Fxx[ 1 ].sShape ) );
455
7.75k
                        silk_memset( &psEnc->state_Fxx[ 1 ].sCmn.sNSQ,            0, sizeof( psEnc->state_Fxx[ 1 ].sCmn.sNSQ ) );
456
7.75k
                        silk_memset( psEnc->state_Fxx[ 1 ].sCmn.prev_NLSFq_Q15,   0, sizeof( psEnc->state_Fxx[ 1 ].sCmn.prev_NLSFq_Q15 ) );
457
7.75k
                        silk_memset( &psEnc->state_Fxx[ 1 ].sCmn.sLP.In_LP_State, 0, sizeof( psEnc->state_Fxx[ 1 ].sCmn.sLP.In_LP_State ) );
458
7.75k
                        psEnc->state_Fxx[ 1 ].sCmn.prevLag                 = 100;
459
7.75k
                        psEnc->state_Fxx[ 1 ].sCmn.sNSQ.lagPrev            = 100;
460
7.75k
                        psEnc->state_Fxx[ 1 ].sShape.LastGainIndex         = 10;
461
7.75k
                        psEnc->state_Fxx[ 1 ].sCmn.prevSignalType          = TYPE_NO_VOICE_ACTIVITY;
462
7.75k
                        psEnc->state_Fxx[ 1 ].sCmn.sNSQ.prev_gain_Q16      = 65536;
463
7.75k
                        psEnc->state_Fxx[ 1 ].sCmn.first_frame_after_reset = 1;
464
7.75k
                    }
465
20.3M
                    silk_encode_do_VAD_Fxx( &psEnc->state_Fxx[ 1 ], activity );
466
20.3M
                } else {
467
5.80M
                    psEnc->state_Fxx[ 1 ].sCmn.VAD_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] = 0;
468
5.80M
                }
469
26.1M
                if( !prefillFlag ) {
470
26.1M
                    silk_stereo_encode_pred( psRangeEnc, psEnc->sStereo.predIx[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] );
471
26.1M
                    if( psEnc->state_Fxx[ 1 ].sCmn.VAD_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] == 0 ) {
472
25.5M
                        silk_stereo_encode_mid_only( psRangeEnc, psEnc->sStereo.mid_only_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] );
473
25.5M
                    }
474
26.1M
                }
475
72.4M
            } else {
476
                /* Buffering */
477
72.4M
                silk_memcpy( psEnc->state_Fxx[ 0 ].sCmn.inputBuf, psEnc->sStereo.sMid, 2 * sizeof( opus_int16 ) );
478
72.4M
                silk_memcpy( psEnc->sStereo.sMid, &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.frame_length ], 2 * sizeof( opus_int16 ) );
479
72.4M
            }
480
98.6M
            silk_encode_do_VAD_Fxx( &psEnc->state_Fxx[ 0 ], activity );
481
482
            /* Encode */
483
223M
            for( n = 0; n < encControl->nChannelsInternal; n++ ) {
484
124M
                opus_int maxBits, useCBR;
485
486
                /* Handling rate constraints */
487
124M
                maxBits = encControl->maxBits;
488
124M
                if( tot_blocks == 2 && curr_block == 0 ) {
489
8.72M
                    maxBits = maxBits * 3 / 5;
490
116M
                } else if( tot_blocks == 3 ) {
491
13.0M
                    if( curr_block == 0 ) {
492
4.34M
                        maxBits = maxBits * 2 / 5;
493
8.69M
                    } else if( curr_block == 1 ) {
494
4.34M
                        maxBits = maxBits * 3 / 4;
495
4.34M
                    }
496
13.0M
                }
497
124M
                useCBR = encControl->useCBR && curr_block == tot_blocks - 1;
498
499
124M
                if( encControl->nChannelsInternal == 1 ) {
500
72.4M
                    channelRate_bps = TargetRate_bps;
501
72.4M
                } else {
502
52.3M
                    channelRate_bps = MStargetRates_bps[ n ];
503
52.3M
                    if( n == 0 && MStargetRates_bps[ 1 ] > 0 ) {
504
20.3M
                        useCBR = 0;
505
                        /* Give mid up to 1/2 of the max bits for that frame */
506
20.3M
                        maxBits -= encControl->maxBits / ( tot_blocks * 2 );
507
20.3M
                    }
508
52.3M
                }
509
510
124M
                if( channelRate_bps > 0 ) {
511
119M
                    opus_int condCoding;
512
513
119M
                    silk_control_SNR( &psEnc->state_Fxx[ n ].sCmn, channelRate_bps );
514
515
                    /* Use independent coding if no previous frame available */
516
119M
                    if( psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded - n <= 0 ) {
517
102M
                        condCoding = CODE_INDEPENDENTLY;
518
102M
                    } 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
3.96k
                        condCoding = CODE_INDEPENDENTLY_NO_LTP_SCALING;
522
16.6M
                    } else {
523
16.6M
                        condCoding = CODE_CONDITIONALLY;
524
16.6M
                    }
525
119M
                    if( ( ret = silk_encode_frame_Fxx( &psEnc->state_Fxx[ n ], nBytesOut, psRangeEnc, condCoding, maxBits, useCBR ) ) != 0 ) {
526
0
                        silk_assert( 0 );
527
0
                    }
528
119M
                }
529
124M
                psEnc->state_Fxx[ n ].sCmn.controlled_since_last_payload = 0;
530
124M
                psEnc->state_Fxx[ n ].sCmn.inputBufIx = 0;
531
124M
                psEnc->state_Fxx[ n ].sCmn.nFramesEncoded++;
532
124M
            }
533
98.6M
            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
98.6M
            if( *nBytesOut > 0 && psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded == psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket) {
537
84.7M
                flags = 0;
538
192M
                for( n = 0; n < encControl->nChannelsInternal; n++ ) {
539
232M
                    for( i = 0; i < psEnc->state_Fxx[ n ].sCmn.nFramesPerPacket; i++ ) {
540
124M
                        flags  = silk_LSHIFT( flags, 1 );
541
124M
                        flags |= psEnc->state_Fxx[ n ].sCmn.VAD_flags[ i ];
542
124M
                    }
543
107M
                    flags  = silk_LSHIFT( flags, 1 );
544
107M
                    flags |= psEnc->state_Fxx[ n ].sCmn.LBRR_flag;
545
107M
                }
546
84.7M
                if( !prefillFlag ) {
547
84.7M
                    ec_enc_patch_initial_bits( psRangeEnc, flags, ( psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket + 1 ) * encControl->nChannelsInternal );
548
84.7M
                }
549
550
                /* Return zero bytes if all channels DTXed */
551
84.7M
                if( psEnc->state_Fxx[ 0 ].sCmn.inDTX && ( encControl->nChannelsInternal == 1 || psEnc->state_Fxx[ 1 ].sCmn.inDTX ) ) {
552
30.2M
                    *nBytesOut = 0;
553
30.2M
                }
554
555
84.7M
                psEnc->nBitsExceeded += *nBytesOut * 8;
556
84.7M
                psEnc->nBitsExceeded -= silk_DIV32_16( silk_MUL( encControl->bitRate, encControl->payloadSize_ms ), 1000 );
557
84.7M
                psEnc->nBitsExceeded  = silk_LIMIT( psEnc->nBitsExceeded, 0, 10000 );
558
559
                /* Update flag indicating if bandwidth switching is allowed */
560
84.7M
                speech_act_thr_for_switch_Q8 = silk_SMLAWB( SILK_FIX_CONST( SPEECH_ACTIVITY_DTX_THRES, 8 ),
561
84.7M
                    SILK_FIX_CONST( ( 1 - SPEECH_ACTIVITY_DTX_THRES ) / MAX_BANDWIDTH_SWITCH_DELAY_MS, 16 + 8 ), psEnc->timeSinceSwitchAllowed_ms );
562
84.7M
                if( psEnc->state_Fxx[ 0 ].sCmn.speech_activity_Q8 < speech_act_thr_for_switch_Q8 ) {
563
82.3M
                    psEnc->allowBandwidthSwitch = 1;
564
82.3M
                    psEnc->timeSinceSwitchAllowed_ms = 0;
565
82.3M
                } else {
566
2.43M
                    psEnc->allowBandwidthSwitch = 0;
567
2.43M
                    psEnc->timeSinceSwitchAllowed_ms += encControl->payloadSize_ms;
568
2.43M
                }
569
84.7M
            }
570
571
98.6M
            if( nSamplesIn == 0 ) {
572
84.7M
                break;
573
84.7M
            }
574
98.6M
        } else {
575
0
            break;
576
0
        }
577
13.8M
        curr_block++;
578
13.8M
    }
579
580
84.7M
    psEnc->nPrevChannelsInternal = encControl->nChannelsInternal;
581
582
84.7M
    encControl->allowBandwidthSwitch = psEnc->allowBandwidthSwitch;
583
84.7M
    encControl->inWBmodeWithoutVariableLP = psEnc->state_Fxx[ 0 ].sCmn.fs_kHz == 16 && psEnc->state_Fxx[ 0 ].sCmn.sLP.mode == 0;
584
84.7M
    encControl->internalSampleRate = silk_SMULBB( psEnc->state_Fxx[ 0 ].sCmn.fs_kHz, 1000 );
585
84.7M
    encControl->stereoWidth_Q14 = encControl->toMono ? 0 : psEnc->sStereo.smth_width_Q14;
586
84.7M
    if( prefillFlag ) {
587
1.99k
        encControl->payloadSize_ms = tmp_payloadSize_ms;
588
1.99k
        encControl->complexity = tmp_complexity;
589
4.44k
        for( n = 0; n < encControl->nChannelsInternal; n++ ) {
590
2.44k
            psEnc->state_Fxx[ n ].sCmn.controlled_since_last_payload = 0;
591
2.44k
            psEnc->state_Fxx[ n ].sCmn.prefillFlag = 0;
592
2.44k
        }
593
1.99k
    }
594
595
84.7M
    encControl->signalType = psEnc->state_Fxx[0].sCmn.indices.signalType;
596
84.7M
    encControl->offset = silk_Quantization_Offsets_Q10
597
84.7M
                         [ psEnc->state_Fxx[0].sCmn.indices.signalType >> 1 ]
598
84.7M
                         [ psEnc->state_Fxx[0].sCmn.indices.quantOffsetType ];
599
84.7M
    RESTORE_STACK;
600
84.7M
    return ret;
601
84.7M
}
silk_Encode
Line
Count
Source
160
42.3M
{
161
42.3M
    opus_int   n, i, nBits, flags, tmp_payloadSize_ms = 0, tmp_complexity = 0, ret = 0;
162
42.3M
    opus_int   nSamplesToBuffer, nSamplesToBufferMax, nBlocksOf10ms;
163
42.3M
    opus_int   nSamplesFromInput = 0, nSamplesFromInputMax;
164
42.3M
    opus_int   speech_act_thr_for_switch_Q8;
165
42.3M
    opus_int32 TargetRate_bps, MStargetRates_bps[ 2 ], channelRate_bps, LBRR_symbol, sum;
166
42.3M
    silk_encoder *psEnc = ( silk_encoder * )encState;
167
42.3M
    VARDECL( opus_int16, buf );
168
42.3M
    opus_int transition, curr_block, tot_blocks;
169
42.3M
    SAVE_STACK;
170
171
42.3M
    celt_assert( encControl->nChannelsAPI >= encControl->nChannelsInternal && encControl->nChannelsAPI >= psEnc->nChannelsInternal );
172
42.3M
    if (encControl->reducedDependency)
173
12.8M
    {
174
31.0M
       for( n = 0; n < encControl->nChannelsAPI; n++ ) {
175
18.2M
           psEnc->state_Fxx[ n ].sCmn.first_frame_after_reset = 1;
176
18.2M
       }
177
12.8M
    }
178
99.6M
    for( n = 0; n < encControl->nChannelsAPI; n++ ) {
179
57.3M
        psEnc->state_Fxx[ n ].sCmn.nFramesEncoded = 0;
180
57.3M
    }
181
    /* Check values in encoder control structure */
182
42.3M
    if( ( ret = check_control_input( encControl ) ) != 0 ) {
183
0
        celt_assert( 0 );
184
0
        RESTORE_STACK;
185
0
        return ret;
186
0
    }
187
188
42.3M
    encControl->switchReady = 0;
189
190
42.3M
    if( encControl->nChannelsInternal > psEnc->nChannelsInternal ) {
191
        /* Mono -> Stereo transition: init state of second channel and stereo state */
192
57.7k
        ret += silk_init_encoder( &psEnc->state_Fxx[ 1 ], psEnc->state_Fxx[ 0 ].sCmn.arch );
193
57.7k
        silk_memset( psEnc->sStereo.pred_prev_Q13, 0, sizeof( psEnc->sStereo.pred_prev_Q13 ) );
194
57.7k
        silk_memset( psEnc->sStereo.sSide, 0, sizeof( psEnc->sStereo.sSide ) );
195
57.7k
        psEnc->sStereo.mid_side_amp_Q0[ 0 ] = 0;
196
57.7k
        psEnc->sStereo.mid_side_amp_Q0[ 1 ] = 1;
197
57.7k
        psEnc->sStereo.mid_side_amp_Q0[ 2 ] = 0;
198
57.7k
        psEnc->sStereo.mid_side_amp_Q0[ 3 ] = 1;
199
57.7k
        psEnc->sStereo.width_prev_Q14 = 0;
200
57.7k
        psEnc->sStereo.smth_width_Q14 = SILK_FIX_CONST( 1, 14 );
201
57.7k
        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
57.7k
    }
206
207
42.3M
    transition = (encControl->payloadSize_ms != psEnc->state_Fxx[ 0 ].sCmn.PacketSize_ms) || (psEnc->nChannelsInternal != encControl->nChannelsInternal);
208
209
42.3M
    psEnc->nChannelsAPI = encControl->nChannelsAPI;
210
42.3M
    psEnc->nChannelsInternal = encControl->nChannelsInternal;
211
212
42.3M
    nBlocksOf10ms = silk_DIV32( 100 * nSamplesIn, encControl->API_sampleRate );
213
42.3M
    tot_blocks = ( nBlocksOf10ms > 1 ) ? nBlocksOf10ms >> 1 : 1;
214
42.3M
    curr_block = 0;
215
42.3M
    if( prefillFlag ) {
216
997
        silk_LP_state save_LP;
217
        /* Only accept input length of 10 ms */
218
997
        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
997
        if ( prefillFlag == 2 ) {
224
367
            save_LP = psEnc->state_Fxx[ 0 ].sCmn.sLP;
225
            /* Save the sampling rate so the bandwidth switching code can keep handling transitions. */
226
367
            save_LP.saved_fs_kHz = psEnc->state_Fxx[ 0 ].sCmn.fs_kHz;
227
367
        }
228
        /* Reset Encoder */
229
2.22k
        for( n = 0; n < encControl->nChannelsInternal; n++ ) {
230
1.22k
            ret = silk_init_encoder( &psEnc->state_Fxx[ n ], psEnc->state_Fxx[ n ].sCmn.arch );
231
            /* Restore the variable LP state. */
232
1.22k
            if ( prefillFlag == 2 ) {
233
453
                psEnc->state_Fxx[ n ].sCmn.sLP = save_LP;
234
453
            }
235
1.22k
            celt_assert( !ret );
236
1.22k
        }
237
997
        tmp_payloadSize_ms = encControl->payloadSize_ms;
238
997
        encControl->payloadSize_ms = 10;
239
997
        tmp_complexity = encControl->complexity;
240
997
        encControl->complexity = 0;
241
2.22k
        for( n = 0; n < encControl->nChannelsInternal; n++ ) {
242
1.22k
            psEnc->state_Fxx[ n ].sCmn.controlled_since_last_payload = 0;
243
1.22k
            psEnc->state_Fxx[ n ].sCmn.prefillFlag = 1;
244
1.22k
        }
245
42.3M
    } else {
246
        /* Only accept input lengths that are a multiple of 10 ms */
247
42.3M
        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
42.3M
        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
42.3M
    }
259
260
96.0M
    for( n = 0; n < encControl->nChannelsInternal; n++ ) {
261
        /* Force the side channel to the same rate as the mid */
262
53.7M
        opus_int force_fs_kHz = (n==1) ? psEnc->state_Fxx[0].sCmn.fs_kHz : 0;
263
53.7M
        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
53.7M
        if( psEnc->state_Fxx[n].sCmn.first_frame_after_reset || transition ) {
269
37.9M
            for( i = 0; i < psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket; i++ ) {
270
20.5M
                psEnc->state_Fxx[ n ].sCmn.LBRR_flags[ i ] = 0;
271
20.5M
            }
272
17.3M
        }
273
53.7M
        psEnc->state_Fxx[ n ].sCmn.inDTX = psEnc->state_Fxx[ n ].sCmn.useDTX;
274
53.7M
    }
275
42.3M
    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
42.3M
    nSamplesToBufferMax =
279
42.3M
        10 * nBlocksOf10ms * psEnc->state_Fxx[ 0 ].sCmn.fs_kHz;
280
42.3M
    nSamplesFromInputMax =
281
42.3M
        silk_DIV32_16( nSamplesToBufferMax *
282
42.3M
                           psEnc->state_Fxx[ 0 ].sCmn.API_fs_Hz,
283
42.3M
                       psEnc->state_Fxx[ 0 ].sCmn.fs_kHz * 1000 );
284
42.3M
    ALLOC( buf, nSamplesFromInputMax, opus_int16 );
285
49.3M
    while( 1 ) {
286
49.3M
        int curr_nBitsUsedLBRR = 0;
287
49.3M
        nSamplesToBuffer  = psEnc->state_Fxx[ 0 ].sCmn.frame_length - psEnc->state_Fxx[ 0 ].sCmn.inputBufIx;
288
49.3M
        nSamplesToBuffer  = silk_min( nSamplesToBuffer, nSamplesToBufferMax );
289
49.3M
        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
49.3M
        if( encControl->nChannelsAPI == 2 && encControl->nChannelsInternal == 2 ) {
292
13.0M
            opus_int id = psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded;
293
3.93G
            for( n = 0; n < nSamplesFromInput; n++ ) {
294
3.92G
                buf[ n ] = RES2INT16(samplesIn[ 2 * n ]);
295
3.92G
            }
296
            /* Making sure to start both resamplers from the same state when switching from mono to stereo */
297
13.0M
            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.0M
            ret += silk_resampler( &psEnc->state_Fxx[ 0 ].sCmn.resampler_state,
302
13.0M
                &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput );
303
13.0M
            psEnc->state_Fxx[ 0 ].sCmn.inputBufIx += nSamplesToBuffer;
304
305
13.0M
            nSamplesToBuffer  = psEnc->state_Fxx[ 1 ].sCmn.frame_length - psEnc->state_Fxx[ 1 ].sCmn.inputBufIx;
306
13.0M
            nSamplesToBuffer  = silk_min( nSamplesToBuffer, 10 * nBlocksOf10ms * psEnc->state_Fxx[ 1 ].sCmn.fs_kHz );
307
3.93G
            for( n = 0; n < nSamplesFromInput; n++ ) {
308
3.92G
                buf[ n ] = RES2INT16(samplesIn[ 2 * n + 1 ]);
309
3.92G
            }
310
13.0M
            ret += silk_resampler( &psEnc->state_Fxx[ 1 ].sCmn.resampler_state,
311
13.0M
                &psEnc->state_Fxx[ 1 ].sCmn.inputBuf[ psEnc->state_Fxx[ 1 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput );
312
313
13.0M
            psEnc->state_Fxx[ 1 ].sCmn.inputBufIx += nSamplesToBuffer;
314
36.2M
        } else if( encControl->nChannelsAPI == 2 && encControl->nChannelsInternal == 1 ) {
315
            /* Combine left and right channels before resampling */
316
1.27G
            for( n = 0; n < nSamplesFromInput; n++ ) {
317
1.27G
                sum = RES2INT16(samplesIn[ 2 * n ] + samplesIn[ 2 * n + 1 ]);
318
1.27G
                buf[ n ] = (opus_int16)silk_RSHIFT_ROUND( sum,  1 );
319
1.27G
            }
320
4.21M
            ret += silk_resampler( &psEnc->state_Fxx[ 0 ].sCmn.resampler_state,
321
4.21M
                &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
4.21M
            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
4.21M
            psEnc->state_Fxx[ 0 ].sCmn.inputBufIx += nSamplesToBuffer;
333
32.0M
        } else {
334
32.0M
            celt_assert( encControl->nChannelsAPI == 1 && encControl->nChannelsInternal == 1 );
335
9.55G
            for( n = 0; n < nSamplesFromInput; n++ ) {
336
9.52G
                buf[n] = RES2INT16(samplesIn[n]);
337
9.52G
            }
338
32.0M
            ret += silk_resampler( &psEnc->state_Fxx[ 0 ].sCmn.resampler_state,
339
32.0M
                &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput );
340
32.0M
            psEnc->state_Fxx[ 0 ].sCmn.inputBufIx += nSamplesToBuffer;
341
32.0M
        }
342
343
49.3M
        samplesIn  += nSamplesFromInput * encControl->nChannelsAPI;
344
49.3M
        nSamplesIn -= nSamplesFromInput;
345
346
        /* Default */
347
49.3M
        psEnc->allowBandwidthSwitch = 0;
348
349
        /* Silk encoder */
350
49.3M
        if( psEnc->state_Fxx[ 0 ].sCmn.inputBufIx >= psEnc->state_Fxx[ 0 ].sCmn.frame_length ) {
351
            /* Enough data in input buffer, so encode */
352
49.3M
            celt_assert( psEnc->state_Fxx[ 0 ].sCmn.inputBufIx == psEnc->state_Fxx[ 0 ].sCmn.frame_length );
353
49.3M
            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
49.3M
            if( psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded == 0 && !prefillFlag ) {
357
                /* Create space at start of payload for VAD and FEC flags */
358
42.3M
                opus_uint8 iCDF[ 2 ] = { 0, 0 };
359
42.3M
                iCDF[ 0 ] = 256 - silk_RSHIFT( 256, ( psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket + 1 ) * encControl->nChannelsInternal );
360
42.3M
                ec_enc_icdf( psRangeEnc, 0, iCDF, 8 );
361
42.3M
                curr_nBitsUsedLBRR = ec_tell( psRangeEnc );
362
363
                /* Encode any LBRR data from previous packet */
364
                /* Encode LBRR flags */
365
96.0M
                for( n = 0; n < encControl->nChannelsInternal; n++ ) {
366
53.7M
                    LBRR_symbol = 0;
367
116M
                    for( i = 0; i < psEnc->state_Fxx[ n ].sCmn.nFramesPerPacket; i++ ) {
368
62.4M
                        LBRR_symbol |= silk_LSHIFT( psEnc->state_Fxx[ n ].sCmn.LBRR_flags[ i ], i );
369
62.4M
                    }
370
53.7M
                    psEnc->state_Fxx[ n ].sCmn.LBRR_flag = LBRR_symbol > 0 ? 1 : 0;
371
53.7M
                    if( LBRR_symbol && psEnc->state_Fxx[ n ].sCmn.nFramesPerPacket > 1 ) {
372
67.2k
                        ec_enc_icdf( psRangeEnc, LBRR_symbol - 1, silk_LBRR_flags_iCDF_ptr[ psEnc->state_Fxx[ n ].sCmn.nFramesPerPacket - 2 ], 8 );
373
67.2k
                    }
374
53.7M
                }
375
376
                /* Code LBRR indices and excitation signals */
377
91.7M
                for( i = 0; i < psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket; i++ ) {
378
111M
                    for( n = 0; n < encControl->nChannelsInternal; n++ ) {
379
62.4M
                        if( psEnc->state_Fxx[ n ].sCmn.LBRR_flags[ i ] ) {
380
367k
                            opus_int condCoding;
381
382
367k
                            if( encControl->nChannelsInternal == 2 && n == 0 ) {
383
125k
                                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
125k
                                if( psEnc->state_Fxx[ 1 ].sCmn.LBRR_flags[ i ] == 0 ) {
386
62.4k
                                    silk_stereo_encode_mid_only( psRangeEnc, psEnc->sStereo.mid_only_flags[ i ] );
387
62.4k
                                }
388
125k
                            }
389
                            /* Use conditional coding if previous frame available */
390
367k
                            if( i > 0 && psEnc->state_Fxx[ n ].sCmn.LBRR_flags[ i - 1 ] ) {
391
76.1k
                                condCoding = CODE_CONDITIONALLY;
392
291k
                            } else {
393
291k
                                condCoding = CODE_INDEPENDENTLY;
394
291k
                            }
395
367k
                            silk_encode_indices( &psEnc->state_Fxx[ n ].sCmn, psRangeEnc, i, 1, condCoding );
396
367k
                            silk_encode_pulses( psRangeEnc, psEnc->state_Fxx[ n ].sCmn.indices_LBRR[i].signalType, psEnc->state_Fxx[ n ].sCmn.indices_LBRR[i].quantOffsetType,
397
367k
                                psEnc->state_Fxx[ n ].sCmn.pulses_LBRR[ i ], psEnc->state_Fxx[ n ].sCmn.frame_length );
398
367k
                        }
399
62.4M
                    }
400
49.3M
                }
401
402
                /* Reset LBRR flags */
403
96.0M
                for( n = 0; n < encControl->nChannelsInternal; n++ ) {
404
53.7M
                    silk_memset( psEnc->state_Fxx[ n ].sCmn.LBRR_flags, 0, sizeof( psEnc->state_Fxx[ n ].sCmn.LBRR_flags ) );
405
53.7M
                }
406
42.3M
                curr_nBitsUsedLBRR = ec_tell( psRangeEnc ) - curr_nBitsUsedLBRR;
407
42.3M
            }
408
409
49.3M
            silk_HP_variable_cutoff( psEnc->state_Fxx );
410
411
            /* Total target bits for packet */
412
49.3M
            nBits = silk_DIV32_16( silk_MUL( encControl->bitRate, encControl->payloadSize_ms ), 1000 );
413
            /* Subtract bits used for LBRR */
414
49.3M
            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
49.3M
                if ( curr_nBitsUsedLBRR < 10 ) {
419
49.0M
                    psEnc->nBitsUsedLBRR = 0;
420
49.0M
                } else if ( psEnc->nBitsUsedLBRR < 10) {
421
69.9k
                    psEnc->nBitsUsedLBRR = curr_nBitsUsedLBRR;
422
169k
                } else {
423
169k
                    psEnc->nBitsUsedLBRR = ( psEnc->nBitsUsedLBRR + curr_nBitsUsedLBRR ) / 2;
424
169k
                }
425
49.3M
                nBits -= psEnc->nBitsUsedLBRR;
426
49.3M
            }
427
            /* Divide by number of uncoded frames left in packet */
428
49.3M
            nBits = silk_DIV32_16( nBits, psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket );
429
            /* Convert to bits/second */
430
49.3M
            if( encControl->payloadSize_ms == 10 ) {
431
20.1M
                TargetRate_bps = silk_SMULBB( nBits, 100 );
432
29.1M
            } else {
433
29.1M
                TargetRate_bps = silk_SMULBB( nBits, 50 );
434
29.1M
            }
435
            /* Subtract fraction of bits in excess of target in previous frames and packets */
436
49.3M
            TargetRate_bps -= silk_DIV32_16( silk_MUL( psEnc->nBitsExceeded, 1000 ), BITRESERVOIR_DECAY_TIME_MS );
437
49.3M
            if( !prefillFlag && psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded > 0 ) {
438
                /* Compare actual vs target bits so far in this packet */
439
6.92M
                opus_int32 bitsBalance = ec_tell( psRangeEnc ) - psEnc->nBitsUsedLBRR - nBits * psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded;
440
6.92M
                TargetRate_bps -= silk_DIV32_16( silk_MUL( bitsBalance, 1000 ), BITRESERVOIR_DECAY_TIME_MS );
441
6.92M
            }
442
            /* Never exceed input bitrate */
443
49.3M
            TargetRate_bps = silk_LIMIT( TargetRate_bps, encControl->bitRate, 5000 );
444
445
            /* Convert Left/Right to Mid/Side */
446
49.3M
            if( encControl->nChannelsInternal == 2 ) {
447
13.0M
                silk_stereo_LR_to_MS( &psEnc->sStereo, &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ 2 ], &psEnc->state_Fxx[ 1 ].sCmn.inputBuf[ 2 ],
448
13.0M
                    psEnc->sStereo.predIx[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ], &psEnc->sStereo.mid_only_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ],
449
13.0M
                    MStargetRates_bps, TargetRate_bps, psEnc->state_Fxx[ 0 ].sCmn.speech_activity_Q8, encControl->toMono,
450
13.0M
                    psEnc->state_Fxx[ 0 ].sCmn.fs_kHz, psEnc->state_Fxx[ 0 ].sCmn.frame_length );
451
13.0M
                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
10.1M
                    if( psEnc->prev_decode_only_middle == 1 ) {
454
3.87k
                        silk_memset( &psEnc->state_Fxx[ 1 ].sShape,               0, sizeof( psEnc->state_Fxx[ 1 ].sShape ) );
455
3.87k
                        silk_memset( &psEnc->state_Fxx[ 1 ].sCmn.sNSQ,            0, sizeof( psEnc->state_Fxx[ 1 ].sCmn.sNSQ ) );
456
3.87k
                        silk_memset( psEnc->state_Fxx[ 1 ].sCmn.prev_NLSFq_Q15,   0, sizeof( psEnc->state_Fxx[ 1 ].sCmn.prev_NLSFq_Q15 ) );
457
3.87k
                        silk_memset( &psEnc->state_Fxx[ 1 ].sCmn.sLP.In_LP_State, 0, sizeof( psEnc->state_Fxx[ 1 ].sCmn.sLP.In_LP_State ) );
458
3.87k
                        psEnc->state_Fxx[ 1 ].sCmn.prevLag                 = 100;
459
3.87k
                        psEnc->state_Fxx[ 1 ].sCmn.sNSQ.lagPrev            = 100;
460
3.87k
                        psEnc->state_Fxx[ 1 ].sShape.LastGainIndex         = 10;
461
3.87k
                        psEnc->state_Fxx[ 1 ].sCmn.prevSignalType          = TYPE_NO_VOICE_ACTIVITY;
462
3.87k
                        psEnc->state_Fxx[ 1 ].sCmn.sNSQ.prev_gain_Q16      = 65536;
463
3.87k
                        psEnc->state_Fxx[ 1 ].sCmn.first_frame_after_reset = 1;
464
3.87k
                    }
465
10.1M
                    silk_encode_do_VAD_Fxx( &psEnc->state_Fxx[ 1 ], activity );
466
10.1M
                } else {
467
2.90M
                    psEnc->state_Fxx[ 1 ].sCmn.VAD_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] = 0;
468
2.90M
                }
469
13.0M
                if( !prefillFlag ) {
470
13.0M
                    silk_stereo_encode_pred( psRangeEnc, psEnc->sStereo.predIx[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] );
471
13.0M
                    if( psEnc->state_Fxx[ 1 ].sCmn.VAD_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] == 0 ) {
472
12.7M
                        silk_stereo_encode_mid_only( psRangeEnc, psEnc->sStereo.mid_only_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] );
473
12.7M
                    }
474
13.0M
                }
475
36.2M
            } else {
476
                /* Buffering */
477
36.2M
                silk_memcpy( psEnc->state_Fxx[ 0 ].sCmn.inputBuf, psEnc->sStereo.sMid, 2 * sizeof( opus_int16 ) );
478
36.2M
                silk_memcpy( psEnc->sStereo.sMid, &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.frame_length ], 2 * sizeof( opus_int16 ) );
479
36.2M
            }
480
49.3M
            silk_encode_do_VAD_Fxx( &psEnc->state_Fxx[ 0 ], activity );
481
482
            /* Encode */
483
111M
            for( n = 0; n < encControl->nChannelsInternal; n++ ) {
484
62.4M
                opus_int maxBits, useCBR;
485
486
                /* Handling rate constraints */
487
62.4M
                maxBits = encControl->maxBits;
488
62.4M
                if( tot_blocks == 2 && curr_block == 0 ) {
489
4.36M
                    maxBits = maxBits * 3 / 5;
490
58.0M
                } else if( tot_blocks == 3 ) {
491
6.52M
                    if( curr_block == 0 ) {
492
2.17M
                        maxBits = maxBits * 2 / 5;
493
4.34M
                    } else if( curr_block == 1 ) {
494
2.17M
                        maxBits = maxBits * 3 / 4;
495
2.17M
                    }
496
6.52M
                }
497
62.4M
                useCBR = encControl->useCBR && curr_block == tot_blocks - 1;
498
499
62.4M
                if( encControl->nChannelsInternal == 1 ) {
500
36.2M
                    channelRate_bps = TargetRate_bps;
501
36.2M
                } else {
502
26.1M
                    channelRate_bps = MStargetRates_bps[ n ];
503
26.1M
                    if( n == 0 && MStargetRates_bps[ 1 ] > 0 ) {
504
10.1M
                        useCBR = 0;
505
                        /* Give mid up to 1/2 of the max bits for that frame */
506
10.1M
                        maxBits -= encControl->maxBits / ( tot_blocks * 2 );
507
10.1M
                    }
508
26.1M
                }
509
510
62.4M
                if( channelRate_bps > 0 ) {
511
59.5M
                    opus_int condCoding;
512
513
59.5M
                    silk_control_SNR( &psEnc->state_Fxx[ n ].sCmn, channelRate_bps );
514
515
                    /* Use independent coding if no previous frame available */
516
59.5M
                    if( psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded - n <= 0 ) {
517
51.1M
                        condCoding = CODE_INDEPENDENTLY;
518
51.1M
                    } 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.98k
                        condCoding = CODE_INDEPENDENTLY_NO_LTP_SCALING;
522
8.32M
                    } else {
523
8.32M
                        condCoding = CODE_CONDITIONALLY;
524
8.32M
                    }
525
59.5M
                    if( ( ret = silk_encode_frame_Fxx( &psEnc->state_Fxx[ n ], nBytesOut, psRangeEnc, condCoding, maxBits, useCBR ) ) != 0 ) {
526
0
                        silk_assert( 0 );
527
0
                    }
528
59.5M
                }
529
62.4M
                psEnc->state_Fxx[ n ].sCmn.controlled_since_last_payload = 0;
530
62.4M
                psEnc->state_Fxx[ n ].sCmn.inputBufIx = 0;
531
62.4M
                psEnc->state_Fxx[ n ].sCmn.nFramesEncoded++;
532
62.4M
            }
533
49.3M
            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
49.3M
            if( *nBytesOut > 0 && psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded == psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket) {
537
42.3M
                flags = 0;
538
96.0M
                for( n = 0; n < encControl->nChannelsInternal; n++ ) {
539
116M
                    for( i = 0; i < psEnc->state_Fxx[ n ].sCmn.nFramesPerPacket; i++ ) {
540
62.4M
                        flags  = silk_LSHIFT( flags, 1 );
541
62.4M
                        flags |= psEnc->state_Fxx[ n ].sCmn.VAD_flags[ i ];
542
62.4M
                    }
543
53.7M
                    flags  = silk_LSHIFT( flags, 1 );
544
53.7M
                    flags |= psEnc->state_Fxx[ n ].sCmn.LBRR_flag;
545
53.7M
                }
546
42.3M
                if( !prefillFlag ) {
547
42.3M
                    ec_enc_patch_initial_bits( psRangeEnc, flags, ( psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket + 1 ) * encControl->nChannelsInternal );
548
42.3M
                }
549
550
                /* Return zero bytes if all channels DTXed */
551
42.3M
                if( psEnc->state_Fxx[ 0 ].sCmn.inDTX && ( encControl->nChannelsInternal == 1 || psEnc->state_Fxx[ 1 ].sCmn.inDTX ) ) {
552
15.1M
                    *nBytesOut = 0;
553
15.1M
                }
554
555
42.3M
                psEnc->nBitsExceeded += *nBytesOut * 8;
556
42.3M
                psEnc->nBitsExceeded -= silk_DIV32_16( silk_MUL( encControl->bitRate, encControl->payloadSize_ms ), 1000 );
557
42.3M
                psEnc->nBitsExceeded  = silk_LIMIT( psEnc->nBitsExceeded, 0, 10000 );
558
559
                /* Update flag indicating if bandwidth switching is allowed */
560
42.3M
                speech_act_thr_for_switch_Q8 = silk_SMLAWB( SILK_FIX_CONST( SPEECH_ACTIVITY_DTX_THRES, 8 ),
561
42.3M
                    SILK_FIX_CONST( ( 1 - SPEECH_ACTIVITY_DTX_THRES ) / MAX_BANDWIDTH_SWITCH_DELAY_MS, 16 + 8 ), psEnc->timeSinceSwitchAllowed_ms );
562
42.3M
                if( psEnc->state_Fxx[ 0 ].sCmn.speech_activity_Q8 < speech_act_thr_for_switch_Q8 ) {
563
41.1M
                    psEnc->allowBandwidthSwitch = 1;
564
41.1M
                    psEnc->timeSinceSwitchAllowed_ms = 0;
565
41.1M
                } else {
566
1.21M
                    psEnc->allowBandwidthSwitch = 0;
567
1.21M
                    psEnc->timeSinceSwitchAllowed_ms += encControl->payloadSize_ms;
568
1.21M
                }
569
42.3M
            }
570
571
49.3M
            if( nSamplesIn == 0 ) {
572
42.3M
                break;
573
42.3M
            }
574
49.3M
        } else {
575
0
            break;
576
0
        }
577
6.92M
        curr_block++;
578
6.92M
    }
579
580
42.3M
    psEnc->nPrevChannelsInternal = encControl->nChannelsInternal;
581
582
42.3M
    encControl->allowBandwidthSwitch = psEnc->allowBandwidthSwitch;
583
42.3M
    encControl->inWBmodeWithoutVariableLP = psEnc->state_Fxx[ 0 ].sCmn.fs_kHz == 16 && psEnc->state_Fxx[ 0 ].sCmn.sLP.mode == 0;
584
42.3M
    encControl->internalSampleRate = silk_SMULBB( psEnc->state_Fxx[ 0 ].sCmn.fs_kHz, 1000 );
585
42.3M
    encControl->stereoWidth_Q14 = encControl->toMono ? 0 : psEnc->sStereo.smth_width_Q14;
586
42.3M
    if( prefillFlag ) {
587
997
        encControl->payloadSize_ms = tmp_payloadSize_ms;
588
997
        encControl->complexity = tmp_complexity;
589
2.22k
        for( n = 0; n < encControl->nChannelsInternal; n++ ) {
590
1.22k
            psEnc->state_Fxx[ n ].sCmn.controlled_since_last_payload = 0;
591
1.22k
            psEnc->state_Fxx[ n ].sCmn.prefillFlag = 0;
592
1.22k
        }
593
997
    }
594
595
42.3M
    encControl->signalType = psEnc->state_Fxx[0].sCmn.indices.signalType;
596
42.3M
    encControl->offset = silk_Quantization_Offsets_Q10
597
42.3M
                         [ psEnc->state_Fxx[0].sCmn.indices.signalType >> 1 ]
598
42.3M
                         [ psEnc->state_Fxx[0].sCmn.indices.quantOffsetType ];
599
42.3M
    RESTORE_STACK;
600
42.3M
    return ret;
601
42.3M
}
silk_Encode
Line
Count
Source
160
42.3M
{
161
42.3M
    opus_int   n, i, nBits, flags, tmp_payloadSize_ms = 0, tmp_complexity = 0, ret = 0;
162
42.3M
    opus_int   nSamplesToBuffer, nSamplesToBufferMax, nBlocksOf10ms;
163
42.3M
    opus_int   nSamplesFromInput = 0, nSamplesFromInputMax;
164
42.3M
    opus_int   speech_act_thr_for_switch_Q8;
165
42.3M
    opus_int32 TargetRate_bps, MStargetRates_bps[ 2 ], channelRate_bps, LBRR_symbol, sum;
166
42.3M
    silk_encoder *psEnc = ( silk_encoder * )encState;
167
42.3M
    VARDECL( opus_int16, buf );
168
42.3M
    opus_int transition, curr_block, tot_blocks;
169
42.3M
    SAVE_STACK;
170
171
42.3M
    celt_assert( encControl->nChannelsAPI >= encControl->nChannelsInternal && encControl->nChannelsAPI >= psEnc->nChannelsInternal );
172
42.3M
    if (encControl->reducedDependency)
173
12.8M
    {
174
31.0M
       for( n = 0; n < encControl->nChannelsAPI; n++ ) {
175
18.2M
           psEnc->state_Fxx[ n ].sCmn.first_frame_after_reset = 1;
176
18.2M
       }
177
12.8M
    }
178
99.6M
    for( n = 0; n < encControl->nChannelsAPI; n++ ) {
179
57.3M
        psEnc->state_Fxx[ n ].sCmn.nFramesEncoded = 0;
180
57.3M
    }
181
    /* Check values in encoder control structure */
182
42.3M
    if( ( ret = check_control_input( encControl ) ) != 0 ) {
183
0
        celt_assert( 0 );
184
0
        RESTORE_STACK;
185
0
        return ret;
186
0
    }
187
188
42.3M
    encControl->switchReady = 0;
189
190
42.3M
    if( encControl->nChannelsInternal > psEnc->nChannelsInternal ) {
191
        /* Mono -> Stereo transition: init state of second channel and stereo state */
192
57.7k
        ret += silk_init_encoder( &psEnc->state_Fxx[ 1 ], psEnc->state_Fxx[ 0 ].sCmn.arch );
193
57.7k
        silk_memset( psEnc->sStereo.pred_prev_Q13, 0, sizeof( psEnc->sStereo.pred_prev_Q13 ) );
194
57.7k
        silk_memset( psEnc->sStereo.sSide, 0, sizeof( psEnc->sStereo.sSide ) );
195
57.7k
        psEnc->sStereo.mid_side_amp_Q0[ 0 ] = 0;
196
57.7k
        psEnc->sStereo.mid_side_amp_Q0[ 1 ] = 1;
197
57.7k
        psEnc->sStereo.mid_side_amp_Q0[ 2 ] = 0;
198
57.7k
        psEnc->sStereo.mid_side_amp_Q0[ 3 ] = 1;
199
57.7k
        psEnc->sStereo.width_prev_Q14 = 0;
200
57.7k
        psEnc->sStereo.smth_width_Q14 = SILK_FIX_CONST( 1, 14 );
201
57.7k
        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
57.7k
    }
206
207
42.3M
    transition = (encControl->payloadSize_ms != psEnc->state_Fxx[ 0 ].sCmn.PacketSize_ms) || (psEnc->nChannelsInternal != encControl->nChannelsInternal);
208
209
42.3M
    psEnc->nChannelsAPI = encControl->nChannelsAPI;
210
42.3M
    psEnc->nChannelsInternal = encControl->nChannelsInternal;
211
212
42.3M
    nBlocksOf10ms = silk_DIV32( 100 * nSamplesIn, encControl->API_sampleRate );
213
42.3M
    tot_blocks = ( nBlocksOf10ms > 1 ) ? nBlocksOf10ms >> 1 : 1;
214
42.3M
    curr_block = 0;
215
42.3M
    if( prefillFlag ) {
216
997
        silk_LP_state save_LP;
217
        /* Only accept input length of 10 ms */
218
997
        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
997
        if ( prefillFlag == 2 ) {
224
367
            save_LP = psEnc->state_Fxx[ 0 ].sCmn.sLP;
225
            /* Save the sampling rate so the bandwidth switching code can keep handling transitions. */
226
367
            save_LP.saved_fs_kHz = psEnc->state_Fxx[ 0 ].sCmn.fs_kHz;
227
367
        }
228
        /* Reset Encoder */
229
2.22k
        for( n = 0; n < encControl->nChannelsInternal; n++ ) {
230
1.22k
            ret = silk_init_encoder( &psEnc->state_Fxx[ n ], psEnc->state_Fxx[ n ].sCmn.arch );
231
            /* Restore the variable LP state. */
232
1.22k
            if ( prefillFlag == 2 ) {
233
453
                psEnc->state_Fxx[ n ].sCmn.sLP = save_LP;
234
453
            }
235
1.22k
            celt_assert( !ret );
236
1.22k
        }
237
997
        tmp_payloadSize_ms = encControl->payloadSize_ms;
238
997
        encControl->payloadSize_ms = 10;
239
997
        tmp_complexity = encControl->complexity;
240
997
        encControl->complexity = 0;
241
2.22k
        for( n = 0; n < encControl->nChannelsInternal; n++ ) {
242
1.22k
            psEnc->state_Fxx[ n ].sCmn.controlled_since_last_payload = 0;
243
1.22k
            psEnc->state_Fxx[ n ].sCmn.prefillFlag = 1;
244
1.22k
        }
245
42.3M
    } else {
246
        /* Only accept input lengths that are a multiple of 10 ms */
247
42.3M
        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
42.3M
        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
42.3M
    }
259
260
96.0M
    for( n = 0; n < encControl->nChannelsInternal; n++ ) {
261
        /* Force the side channel to the same rate as the mid */
262
53.7M
        opus_int force_fs_kHz = (n==1) ? psEnc->state_Fxx[0].sCmn.fs_kHz : 0;
263
53.7M
        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
53.7M
        if( psEnc->state_Fxx[n].sCmn.first_frame_after_reset || transition ) {
269
37.9M
            for( i = 0; i < psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket; i++ ) {
270
20.5M
                psEnc->state_Fxx[ n ].sCmn.LBRR_flags[ i ] = 0;
271
20.5M
            }
272
17.3M
        }
273
53.7M
        psEnc->state_Fxx[ n ].sCmn.inDTX = psEnc->state_Fxx[ n ].sCmn.useDTX;
274
53.7M
    }
275
42.3M
    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
42.3M
    nSamplesToBufferMax =
279
42.3M
        10 * nBlocksOf10ms * psEnc->state_Fxx[ 0 ].sCmn.fs_kHz;
280
42.3M
    nSamplesFromInputMax =
281
42.3M
        silk_DIV32_16( nSamplesToBufferMax *
282
42.3M
                           psEnc->state_Fxx[ 0 ].sCmn.API_fs_Hz,
283
42.3M
                       psEnc->state_Fxx[ 0 ].sCmn.fs_kHz * 1000 );
284
42.3M
    ALLOC( buf, nSamplesFromInputMax, opus_int16 );
285
49.3M
    while( 1 ) {
286
49.3M
        int curr_nBitsUsedLBRR = 0;
287
49.3M
        nSamplesToBuffer  = psEnc->state_Fxx[ 0 ].sCmn.frame_length - psEnc->state_Fxx[ 0 ].sCmn.inputBufIx;
288
49.3M
        nSamplesToBuffer  = silk_min( nSamplesToBuffer, nSamplesToBufferMax );
289
49.3M
        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
49.3M
        if( encControl->nChannelsAPI == 2 && encControl->nChannelsInternal == 2 ) {
292
13.0M
            opus_int id = psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded;
293
3.93G
            for( n = 0; n < nSamplesFromInput; n++ ) {
294
3.92G
                buf[ n ] = RES2INT16(samplesIn[ 2 * n ]);
295
3.92G
            }
296
            /* Making sure to start both resamplers from the same state when switching from mono to stereo */
297
13.0M
            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.0M
            ret += silk_resampler( &psEnc->state_Fxx[ 0 ].sCmn.resampler_state,
302
13.0M
                &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput );
303
13.0M
            psEnc->state_Fxx[ 0 ].sCmn.inputBufIx += nSamplesToBuffer;
304
305
13.0M
            nSamplesToBuffer  = psEnc->state_Fxx[ 1 ].sCmn.frame_length - psEnc->state_Fxx[ 1 ].sCmn.inputBufIx;
306
13.0M
            nSamplesToBuffer  = silk_min( nSamplesToBuffer, 10 * nBlocksOf10ms * psEnc->state_Fxx[ 1 ].sCmn.fs_kHz );
307
3.93G
            for( n = 0; n < nSamplesFromInput; n++ ) {
308
3.92G
                buf[ n ] = RES2INT16(samplesIn[ 2 * n + 1 ]);
309
3.92G
            }
310
13.0M
            ret += silk_resampler( &psEnc->state_Fxx[ 1 ].sCmn.resampler_state,
311
13.0M
                &psEnc->state_Fxx[ 1 ].sCmn.inputBuf[ psEnc->state_Fxx[ 1 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput );
312
313
13.0M
            psEnc->state_Fxx[ 1 ].sCmn.inputBufIx += nSamplesToBuffer;
314
36.2M
        } else if( encControl->nChannelsAPI == 2 && encControl->nChannelsInternal == 1 ) {
315
            /* Combine left and right channels before resampling */
316
1.27G
            for( n = 0; n < nSamplesFromInput; n++ ) {
317
1.27G
                sum = RES2INT16(samplesIn[ 2 * n ] + samplesIn[ 2 * n + 1 ]);
318
1.27G
                buf[ n ] = (opus_int16)silk_RSHIFT_ROUND( sum,  1 );
319
1.27G
            }
320
4.21M
            ret += silk_resampler( &psEnc->state_Fxx[ 0 ].sCmn.resampler_state,
321
4.21M
                &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
4.21M
            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
4.21M
            psEnc->state_Fxx[ 0 ].sCmn.inputBufIx += nSamplesToBuffer;
333
32.0M
        } else {
334
32.0M
            celt_assert( encControl->nChannelsAPI == 1 && encControl->nChannelsInternal == 1 );
335
9.55G
            for( n = 0; n < nSamplesFromInput; n++ ) {
336
9.52G
                buf[n] = RES2INT16(samplesIn[n]);
337
9.52G
            }
338
32.0M
            ret += silk_resampler( &psEnc->state_Fxx[ 0 ].sCmn.resampler_state,
339
32.0M
                &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput );
340
32.0M
            psEnc->state_Fxx[ 0 ].sCmn.inputBufIx += nSamplesToBuffer;
341
32.0M
        }
342
343
49.3M
        samplesIn  += nSamplesFromInput * encControl->nChannelsAPI;
344
49.3M
        nSamplesIn -= nSamplesFromInput;
345
346
        /* Default */
347
49.3M
        psEnc->allowBandwidthSwitch = 0;
348
349
        /* Silk encoder */
350
49.3M
        if( psEnc->state_Fxx[ 0 ].sCmn.inputBufIx >= psEnc->state_Fxx[ 0 ].sCmn.frame_length ) {
351
            /* Enough data in input buffer, so encode */
352
49.3M
            celt_assert( psEnc->state_Fxx[ 0 ].sCmn.inputBufIx == psEnc->state_Fxx[ 0 ].sCmn.frame_length );
353
49.3M
            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
49.3M
            if( psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded == 0 && !prefillFlag ) {
357
                /* Create space at start of payload for VAD and FEC flags */
358
42.3M
                opus_uint8 iCDF[ 2 ] = { 0, 0 };
359
42.3M
                iCDF[ 0 ] = 256 - silk_RSHIFT( 256, ( psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket + 1 ) * encControl->nChannelsInternal );
360
42.3M
                ec_enc_icdf( psRangeEnc, 0, iCDF, 8 );
361
42.3M
                curr_nBitsUsedLBRR = ec_tell( psRangeEnc );
362
363
                /* Encode any LBRR data from previous packet */
364
                /* Encode LBRR flags */
365
96.0M
                for( n = 0; n < encControl->nChannelsInternal; n++ ) {
366
53.7M
                    LBRR_symbol = 0;
367
116M
                    for( i = 0; i < psEnc->state_Fxx[ n ].sCmn.nFramesPerPacket; i++ ) {
368
62.4M
                        LBRR_symbol |= silk_LSHIFT( psEnc->state_Fxx[ n ].sCmn.LBRR_flags[ i ], i );
369
62.4M
                    }
370
53.7M
                    psEnc->state_Fxx[ n ].sCmn.LBRR_flag = LBRR_symbol > 0 ? 1 : 0;
371
53.7M
                    if( LBRR_symbol && psEnc->state_Fxx[ n ].sCmn.nFramesPerPacket > 1 ) {
372
67.2k
                        ec_enc_icdf( psRangeEnc, LBRR_symbol - 1, silk_LBRR_flags_iCDF_ptr[ psEnc->state_Fxx[ n ].sCmn.nFramesPerPacket - 2 ], 8 );
373
67.2k
                    }
374
53.7M
                }
375
376
                /* Code LBRR indices and excitation signals */
377
91.7M
                for( i = 0; i < psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket; i++ ) {
378
111M
                    for( n = 0; n < encControl->nChannelsInternal; n++ ) {
379
62.4M
                        if( psEnc->state_Fxx[ n ].sCmn.LBRR_flags[ i ] ) {
380
367k
                            opus_int condCoding;
381
382
367k
                            if( encControl->nChannelsInternal == 2 && n == 0 ) {
383
125k
                                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
125k
                                if( psEnc->state_Fxx[ 1 ].sCmn.LBRR_flags[ i ] == 0 ) {
386
62.4k
                                    silk_stereo_encode_mid_only( psRangeEnc, psEnc->sStereo.mid_only_flags[ i ] );
387
62.4k
                                }
388
125k
                            }
389
                            /* Use conditional coding if previous frame available */
390
367k
                            if( i > 0 && psEnc->state_Fxx[ n ].sCmn.LBRR_flags[ i - 1 ] ) {
391
76.1k
                                condCoding = CODE_CONDITIONALLY;
392
291k
                            } else {
393
291k
                                condCoding = CODE_INDEPENDENTLY;
394
291k
                            }
395
367k
                            silk_encode_indices( &psEnc->state_Fxx[ n ].sCmn, psRangeEnc, i, 1, condCoding );
396
367k
                            silk_encode_pulses( psRangeEnc, psEnc->state_Fxx[ n ].sCmn.indices_LBRR[i].signalType, psEnc->state_Fxx[ n ].sCmn.indices_LBRR[i].quantOffsetType,
397
367k
                                psEnc->state_Fxx[ n ].sCmn.pulses_LBRR[ i ], psEnc->state_Fxx[ n ].sCmn.frame_length );
398
367k
                        }
399
62.4M
                    }
400
49.3M
                }
401
402
                /* Reset LBRR flags */
403
96.0M
                for( n = 0; n < encControl->nChannelsInternal; n++ ) {
404
53.7M
                    silk_memset( psEnc->state_Fxx[ n ].sCmn.LBRR_flags, 0, sizeof( psEnc->state_Fxx[ n ].sCmn.LBRR_flags ) );
405
53.7M
                }
406
42.3M
                curr_nBitsUsedLBRR = ec_tell( psRangeEnc ) - curr_nBitsUsedLBRR;
407
42.3M
            }
408
409
49.3M
            silk_HP_variable_cutoff( psEnc->state_Fxx );
410
411
            /* Total target bits for packet */
412
49.3M
            nBits = silk_DIV32_16( silk_MUL( encControl->bitRate, encControl->payloadSize_ms ), 1000 );
413
            /* Subtract bits used for LBRR */
414
49.3M
            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
49.3M
                if ( curr_nBitsUsedLBRR < 10 ) {
419
49.0M
                    psEnc->nBitsUsedLBRR = 0;
420
49.0M
                } else if ( psEnc->nBitsUsedLBRR < 10) {
421
69.9k
                    psEnc->nBitsUsedLBRR = curr_nBitsUsedLBRR;
422
169k
                } else {
423
169k
                    psEnc->nBitsUsedLBRR = ( psEnc->nBitsUsedLBRR + curr_nBitsUsedLBRR ) / 2;
424
169k
                }
425
49.3M
                nBits -= psEnc->nBitsUsedLBRR;
426
49.3M
            }
427
            /* Divide by number of uncoded frames left in packet */
428
49.3M
            nBits = silk_DIV32_16( nBits, psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket );
429
            /* Convert to bits/second */
430
49.3M
            if( encControl->payloadSize_ms == 10 ) {
431
20.1M
                TargetRate_bps = silk_SMULBB( nBits, 100 );
432
29.1M
            } else {
433
29.1M
                TargetRate_bps = silk_SMULBB( nBits, 50 );
434
29.1M
            }
435
            /* Subtract fraction of bits in excess of target in previous frames and packets */
436
49.3M
            TargetRate_bps -= silk_DIV32_16( silk_MUL( psEnc->nBitsExceeded, 1000 ), BITRESERVOIR_DECAY_TIME_MS );
437
49.3M
            if( !prefillFlag && psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded > 0 ) {
438
                /* Compare actual vs target bits so far in this packet */
439
6.92M
                opus_int32 bitsBalance = ec_tell( psRangeEnc ) - psEnc->nBitsUsedLBRR - nBits * psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded;
440
6.92M
                TargetRate_bps -= silk_DIV32_16( silk_MUL( bitsBalance, 1000 ), BITRESERVOIR_DECAY_TIME_MS );
441
6.92M
            }
442
            /* Never exceed input bitrate */
443
49.3M
            TargetRate_bps = silk_LIMIT( TargetRate_bps, encControl->bitRate, 5000 );
444
445
            /* Convert Left/Right to Mid/Side */
446
49.3M
            if( encControl->nChannelsInternal == 2 ) {
447
13.0M
                silk_stereo_LR_to_MS( &psEnc->sStereo, &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ 2 ], &psEnc->state_Fxx[ 1 ].sCmn.inputBuf[ 2 ],
448
13.0M
                    psEnc->sStereo.predIx[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ], &psEnc->sStereo.mid_only_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ],
449
13.0M
                    MStargetRates_bps, TargetRate_bps, psEnc->state_Fxx[ 0 ].sCmn.speech_activity_Q8, encControl->toMono,
450
13.0M
                    psEnc->state_Fxx[ 0 ].sCmn.fs_kHz, psEnc->state_Fxx[ 0 ].sCmn.frame_length );
451
13.0M
                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
10.1M
                    if( psEnc->prev_decode_only_middle == 1 ) {
454
3.87k
                        silk_memset( &psEnc->state_Fxx[ 1 ].sShape,               0, sizeof( psEnc->state_Fxx[ 1 ].sShape ) );
455
3.87k
                        silk_memset( &psEnc->state_Fxx[ 1 ].sCmn.sNSQ,            0, sizeof( psEnc->state_Fxx[ 1 ].sCmn.sNSQ ) );
456
3.87k
                        silk_memset( psEnc->state_Fxx[ 1 ].sCmn.prev_NLSFq_Q15,   0, sizeof( psEnc->state_Fxx[ 1 ].sCmn.prev_NLSFq_Q15 ) );
457
3.87k
                        silk_memset( &psEnc->state_Fxx[ 1 ].sCmn.sLP.In_LP_State, 0, sizeof( psEnc->state_Fxx[ 1 ].sCmn.sLP.In_LP_State ) );
458
3.87k
                        psEnc->state_Fxx[ 1 ].sCmn.prevLag                 = 100;
459
3.87k
                        psEnc->state_Fxx[ 1 ].sCmn.sNSQ.lagPrev            = 100;
460
3.87k
                        psEnc->state_Fxx[ 1 ].sShape.LastGainIndex         = 10;
461
3.87k
                        psEnc->state_Fxx[ 1 ].sCmn.prevSignalType          = TYPE_NO_VOICE_ACTIVITY;
462
3.87k
                        psEnc->state_Fxx[ 1 ].sCmn.sNSQ.prev_gain_Q16      = 65536;
463
3.87k
                        psEnc->state_Fxx[ 1 ].sCmn.first_frame_after_reset = 1;
464
3.87k
                    }
465
10.1M
                    silk_encode_do_VAD_Fxx( &psEnc->state_Fxx[ 1 ], activity );
466
10.1M
                } else {
467
2.90M
                    psEnc->state_Fxx[ 1 ].sCmn.VAD_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] = 0;
468
2.90M
                }
469
13.0M
                if( !prefillFlag ) {
470
13.0M
                    silk_stereo_encode_pred( psRangeEnc, psEnc->sStereo.predIx[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] );
471
13.0M
                    if( psEnc->state_Fxx[ 1 ].sCmn.VAD_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] == 0 ) {
472
12.7M
                        silk_stereo_encode_mid_only( psRangeEnc, psEnc->sStereo.mid_only_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] );
473
12.7M
                    }
474
13.0M
                }
475
36.2M
            } else {
476
                /* Buffering */
477
36.2M
                silk_memcpy( psEnc->state_Fxx[ 0 ].sCmn.inputBuf, psEnc->sStereo.sMid, 2 * sizeof( opus_int16 ) );
478
36.2M
                silk_memcpy( psEnc->sStereo.sMid, &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.frame_length ], 2 * sizeof( opus_int16 ) );
479
36.2M
            }
480
49.3M
            silk_encode_do_VAD_Fxx( &psEnc->state_Fxx[ 0 ], activity );
481
482
            /* Encode */
483
111M
            for( n = 0; n < encControl->nChannelsInternal; n++ ) {
484
62.4M
                opus_int maxBits, useCBR;
485
486
                /* Handling rate constraints */
487
62.4M
                maxBits = encControl->maxBits;
488
62.4M
                if( tot_blocks == 2 && curr_block == 0 ) {
489
4.36M
                    maxBits = maxBits * 3 / 5;
490
58.0M
                } else if( tot_blocks == 3 ) {
491
6.52M
                    if( curr_block == 0 ) {
492
2.17M
                        maxBits = maxBits * 2 / 5;
493
4.34M
                    } else if( curr_block == 1 ) {
494
2.17M
                        maxBits = maxBits * 3 / 4;
495
2.17M
                    }
496
6.52M
                }
497
62.4M
                useCBR = encControl->useCBR && curr_block == tot_blocks - 1;
498
499
62.4M
                if( encControl->nChannelsInternal == 1 ) {
500
36.2M
                    channelRate_bps = TargetRate_bps;
501
36.2M
                } else {
502
26.1M
                    channelRate_bps = MStargetRates_bps[ n ];
503
26.1M
                    if( n == 0 && MStargetRates_bps[ 1 ] > 0 ) {
504
10.1M
                        useCBR = 0;
505
                        /* Give mid up to 1/2 of the max bits for that frame */
506
10.1M
                        maxBits -= encControl->maxBits / ( tot_blocks * 2 );
507
10.1M
                    }
508
26.1M
                }
509
510
62.4M
                if( channelRate_bps > 0 ) {
511
59.5M
                    opus_int condCoding;
512
513
59.5M
                    silk_control_SNR( &psEnc->state_Fxx[ n ].sCmn, channelRate_bps );
514
515
                    /* Use independent coding if no previous frame available */
516
59.5M
                    if( psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded - n <= 0 ) {
517
51.1M
                        condCoding = CODE_INDEPENDENTLY;
518
51.1M
                    } 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.98k
                        condCoding = CODE_INDEPENDENTLY_NO_LTP_SCALING;
522
8.32M
                    } else {
523
8.32M
                        condCoding = CODE_CONDITIONALLY;
524
8.32M
                    }
525
59.5M
                    if( ( ret = silk_encode_frame_Fxx( &psEnc->state_Fxx[ n ], nBytesOut, psRangeEnc, condCoding, maxBits, useCBR ) ) != 0 ) {
526
0
                        silk_assert( 0 );
527
0
                    }
528
59.5M
                }
529
62.4M
                psEnc->state_Fxx[ n ].sCmn.controlled_since_last_payload = 0;
530
62.4M
                psEnc->state_Fxx[ n ].sCmn.inputBufIx = 0;
531
62.4M
                psEnc->state_Fxx[ n ].sCmn.nFramesEncoded++;
532
62.4M
            }
533
49.3M
            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
49.3M
            if( *nBytesOut > 0 && psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded == psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket) {
537
42.3M
                flags = 0;
538
96.0M
                for( n = 0; n < encControl->nChannelsInternal; n++ ) {
539
116M
                    for( i = 0; i < psEnc->state_Fxx[ n ].sCmn.nFramesPerPacket; i++ ) {
540
62.4M
                        flags  = silk_LSHIFT( flags, 1 );
541
62.4M
                        flags |= psEnc->state_Fxx[ n ].sCmn.VAD_flags[ i ];
542
62.4M
                    }
543
53.7M
                    flags  = silk_LSHIFT( flags, 1 );
544
53.7M
                    flags |= psEnc->state_Fxx[ n ].sCmn.LBRR_flag;
545
53.7M
                }
546
42.3M
                if( !prefillFlag ) {
547
42.3M
                    ec_enc_patch_initial_bits( psRangeEnc, flags, ( psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket + 1 ) * encControl->nChannelsInternal );
548
42.3M
                }
549
550
                /* Return zero bytes if all channels DTXed */
551
42.3M
                if( psEnc->state_Fxx[ 0 ].sCmn.inDTX && ( encControl->nChannelsInternal == 1 || psEnc->state_Fxx[ 1 ].sCmn.inDTX ) ) {
552
15.1M
                    *nBytesOut = 0;
553
15.1M
                }
554
555
42.3M
                psEnc->nBitsExceeded += *nBytesOut * 8;
556
42.3M
                psEnc->nBitsExceeded -= silk_DIV32_16( silk_MUL( encControl->bitRate, encControl->payloadSize_ms ), 1000 );
557
42.3M
                psEnc->nBitsExceeded  = silk_LIMIT( psEnc->nBitsExceeded, 0, 10000 );
558
559
                /* Update flag indicating if bandwidth switching is allowed */
560
42.3M
                speech_act_thr_for_switch_Q8 = silk_SMLAWB( SILK_FIX_CONST( SPEECH_ACTIVITY_DTX_THRES, 8 ),
561
42.3M
                    SILK_FIX_CONST( ( 1 - SPEECH_ACTIVITY_DTX_THRES ) / MAX_BANDWIDTH_SWITCH_DELAY_MS, 16 + 8 ), psEnc->timeSinceSwitchAllowed_ms );
562
42.3M
                if( psEnc->state_Fxx[ 0 ].sCmn.speech_activity_Q8 < speech_act_thr_for_switch_Q8 ) {
563
41.1M
                    psEnc->allowBandwidthSwitch = 1;
564
41.1M
                    psEnc->timeSinceSwitchAllowed_ms = 0;
565
41.1M
                } else {
566
1.21M
                    psEnc->allowBandwidthSwitch = 0;
567
1.21M
                    psEnc->timeSinceSwitchAllowed_ms += encControl->payloadSize_ms;
568
1.21M
                }
569
42.3M
            }
570
571
49.3M
            if( nSamplesIn == 0 ) {
572
42.3M
                break;
573
42.3M
            }
574
49.3M
        } else {
575
0
            break;
576
0
        }
577
6.92M
        curr_block++;
578
6.92M
    }
579
580
42.3M
    psEnc->nPrevChannelsInternal = encControl->nChannelsInternal;
581
582
42.3M
    encControl->allowBandwidthSwitch = psEnc->allowBandwidthSwitch;
583
42.3M
    encControl->inWBmodeWithoutVariableLP = psEnc->state_Fxx[ 0 ].sCmn.fs_kHz == 16 && psEnc->state_Fxx[ 0 ].sCmn.sLP.mode == 0;
584
42.3M
    encControl->internalSampleRate = silk_SMULBB( psEnc->state_Fxx[ 0 ].sCmn.fs_kHz, 1000 );
585
42.3M
    encControl->stereoWidth_Q14 = encControl->toMono ? 0 : psEnc->sStereo.smth_width_Q14;
586
42.3M
    if( prefillFlag ) {
587
997
        encControl->payloadSize_ms = tmp_payloadSize_ms;
588
997
        encControl->complexity = tmp_complexity;
589
2.22k
        for( n = 0; n < encControl->nChannelsInternal; n++ ) {
590
1.22k
            psEnc->state_Fxx[ n ].sCmn.controlled_since_last_payload = 0;
591
1.22k
            psEnc->state_Fxx[ n ].sCmn.prefillFlag = 0;
592
1.22k
        }
593
997
    }
594
595
42.3M
    encControl->signalType = psEnc->state_Fxx[0].sCmn.indices.signalType;
596
42.3M
    encControl->offset = silk_Quantization_Offsets_Q10
597
42.3M
                         [ psEnc->state_Fxx[0].sCmn.indices.signalType >> 1 ]
598
42.3M
                         [ psEnc->state_Fxx[0].sCmn.indices.quantOffsetType ];
599
42.3M
    RESTORE_STACK;
600
42.3M
    return ret;
601
42.3M
}