Coverage Report

Created: 2026-04-12 08:03

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