Coverage Report

Created: 2026-06-30 07:18

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opus/silk/control_codec.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
#ifdef FIXED_POINT
32
#include "main_FIX.h"
33
#define silk_encoder_state_Fxx      silk_encoder_state_FIX
34
#else
35
#include "main_FLP.h"
36
#define silk_encoder_state_Fxx      silk_encoder_state_FLP
37
#endif
38
#include "stack_alloc.h"
39
#include "tuning_parameters.h"
40
#include "pitch_est_defines.h"
41
42
static opus_int silk_setup_resamplers(
43
    silk_encoder_state_Fxx          *psEnc,             /* I/O                      */
44
    opus_int                        fs_kHz              /* I                        */
45
);
46
47
static opus_int silk_setup_fs(
48
    silk_encoder_state_Fxx          *psEnc,             /* I/O                      */
49
    opus_int                        fs_kHz,             /* I                        */
50
    opus_int                        PacketSize_ms       /* I                        */
51
);
52
53
static opus_int silk_setup_complexity(
54
    silk_encoder_state              *psEncC,            /* I/O                      */
55
    opus_int                        Complexity          /* I                        */
56
);
57
58
static OPUS_INLINE opus_int silk_setup_LBRR(
59
    silk_encoder_state              *psEncC,            /* I/O                      */
60
    const silk_EncControlStruct     *encControl         /* I                        */
61
);
62
63
64
/* Control encoder */
65
opus_int silk_control_encoder(
66
    silk_encoder_state_Fxx          *psEnc,                                 /* I/O  Pointer to Silk encoder state                                               */
67
    silk_EncControlStruct           *encControl,                            /* I    Control structure                                                           */
68
    const opus_int                  allow_bw_switch,                        /* I    Flag to allow switching audio bandwidth                                     */
69
    const opus_int                  channelNb,                              /* I    Channel number                                                              */
70
    const opus_int                  force_fs_kHz
71
)
72
95.6M
{
73
95.6M
    opus_int   fs_kHz, ret = 0;
74
75
95.6M
    psEnc->sCmn.useDTX                 = encControl->useDTX;
76
95.6M
    psEnc->sCmn.useCBR                 = encControl->useCBR;
77
95.6M
    psEnc->sCmn.API_fs_Hz              = encControl->API_sampleRate;
78
95.6M
    psEnc->sCmn.maxInternal_fs_Hz      = encControl->maxInternalSampleRate;
79
95.6M
    psEnc->sCmn.minInternal_fs_Hz      = encControl->minInternalSampleRate;
80
95.6M
    psEnc->sCmn.desiredInternal_fs_Hz  = encControl->desiredInternalSampleRate;
81
95.6M
    psEnc->sCmn.useInBandFEC           = encControl->useInBandFEC;
82
95.6M
    psEnc->sCmn.nChannelsAPI           = encControl->nChannelsAPI;
83
95.6M
    psEnc->sCmn.nChannelsInternal      = encControl->nChannelsInternal;
84
95.6M
    psEnc->sCmn.allow_bandwidth_switch = allow_bw_switch;
85
95.6M
    psEnc->sCmn.channelNb              = channelNb;
86
87
95.6M
    if( psEnc->sCmn.controlled_since_last_payload != 0 && psEnc->sCmn.prefillFlag == 0 ) {
88
0
        if( psEnc->sCmn.API_fs_Hz != psEnc->sCmn.prev_API_fs_Hz && psEnc->sCmn.fs_kHz > 0 ) {
89
            /* Change in API sampling rate in the middle of encoding a packet */
90
0
            ret += silk_setup_resamplers( psEnc, psEnc->sCmn.fs_kHz );
91
0
        }
92
0
        return ret;
93
0
    }
94
95
    /* Beyond this point we know that there are no previously coded frames in the payload buffer */
96
97
    /********************************************/
98
    /* Determine internal sampling rate         */
99
    /********************************************/
100
95.6M
    fs_kHz = silk_control_audio_bandwidth( &psEnc->sCmn, encControl );
101
95.6M
    if( force_fs_kHz ) {
102
15.5M
       fs_kHz = force_fs_kHz;
103
15.5M
    }
104
    /********************************************/
105
    /* Prepare resampler and buffered data      */
106
    /********************************************/
107
95.6M
    ret += silk_setup_resamplers( psEnc, fs_kHz );
108
109
    /********************************************/
110
    /* Set internal sampling frequency          */
111
    /********************************************/
112
95.6M
    ret += silk_setup_fs( psEnc, fs_kHz, encControl->payloadSize_ms );
113
114
    /********************************************/
115
    /* Set encoding complexity                  */
116
    /********************************************/
117
95.6M
    ret += silk_setup_complexity( &psEnc->sCmn, encControl->complexity  );
118
119
    /********************************************/
120
    /* Set packet loss rate measured by farend  */
121
    /********************************************/
122
95.6M
    psEnc->sCmn.PacketLoss_perc = encControl->packetLossPercentage;
123
124
    /********************************************/
125
    /* Set LBRR usage                           */
126
    /********************************************/
127
95.6M
    ret += silk_setup_LBRR( &psEnc->sCmn, encControl );
128
129
95.6M
    psEnc->sCmn.controlled_since_last_payload = 1;
130
131
95.6M
    return ret;
132
95.6M
}
133
134
static opus_int silk_setup_resamplers(
135
    silk_encoder_state_Fxx          *psEnc,             /* I/O                      */
136
    opus_int                         fs_kHz              /* I                        */
137
)
138
95.6M
{
139
95.6M
    opus_int   ret = SILK_NO_ERROR;
140
95.6M
    SAVE_STACK;
141
142
95.6M
    if( psEnc->sCmn.fs_kHz != fs_kHz || psEnc->sCmn.prev_API_fs_Hz != psEnc->sCmn.API_fs_Hz )
143
217k
    {
144
217k
        if( psEnc->sCmn.fs_kHz == 0 ) {
145
            /* Initialize the resampler for enc_API.c preparing resampling from API_fs_Hz to fs_kHz */
146
217k
            ret += silk_resampler_init( &psEnc->sCmn.resampler_state, psEnc->sCmn.API_fs_Hz, fs_kHz * 1000, 1 );
147
217k
        } else {
148
212
            VARDECL( opus_int16, x_buf_API_fs_Hz );
149
212
            VARDECL( silk_resampler_state_struct, temp_resampler_state );
150
#ifdef FIXED_POINT
151
            opus_int16 *x_bufFIX = psEnc->x_buf;
152
#else
153
            VARDECL( opus_int16, x_bufFIX );
154
            opus_int32 new_buf_samples;
155
#endif
156
212
            opus_int32 api_buf_samples;
157
212
            opus_int32 old_buf_samples;
158
212
            opus_int32 buf_length_ms;
159
160
212
            buf_length_ms = silk_LSHIFT( psEnc->sCmn.nb_subfr * 5, 1 ) + LA_SHAPE_MS;
161
212
            old_buf_samples = buf_length_ms * psEnc->sCmn.fs_kHz;
162
163
#ifndef FIXED_POINT
164
            new_buf_samples = buf_length_ms * fs_kHz;
165
152
            ALLOC( x_bufFIX, silk_max( old_buf_samples, new_buf_samples ),
166
                   opus_int16 );
167
            silk_float2short_array( x_bufFIX, psEnc->x_buf, old_buf_samples );
168
#endif
169
170
            /* Initialize resampler for temporary resampling of x_buf data to API_fs_Hz */
171
212
            ALLOC( temp_resampler_state, 1, silk_resampler_state_struct );
172
212
            ret += silk_resampler_init( temp_resampler_state, silk_SMULBB( psEnc->sCmn.fs_kHz, 1000 ), psEnc->sCmn.API_fs_Hz, 0 );
173
174
            /* Calculate number of samples to temporarily upsample */
175
212
            api_buf_samples = buf_length_ms * silk_DIV32_16( psEnc->sCmn.API_fs_Hz, 1000 );
176
177
            /* Temporary resampling of x_buf data to API_fs_Hz */
178
212
            ALLOC( x_buf_API_fs_Hz, api_buf_samples, opus_int16 );
179
212
            ret += silk_resampler( temp_resampler_state, x_buf_API_fs_Hz, x_bufFIX, old_buf_samples );
180
181
            /* Initialize the resampler for enc_API.c preparing resampling from API_fs_Hz to fs_kHz */
182
212
            ret += silk_resampler_init( &psEnc->sCmn.resampler_state, psEnc->sCmn.API_fs_Hz, silk_SMULBB( fs_kHz, 1000 ), 1 );
183
184
            /* Correct resampler state by resampling buffered data from API_fs_Hz to fs_kHz */
185
212
            ret += silk_resampler( &psEnc->sCmn.resampler_state, x_bufFIX, x_buf_API_fs_Hz, api_buf_samples );
186
187
#ifndef FIXED_POINT
188
            silk_short2float_array( psEnc->x_buf, x_bufFIX, new_buf_samples);
189
#endif
190
212
        }
191
217k
    }
192
193
95.6M
    psEnc->sCmn.prev_API_fs_Hz = psEnc->sCmn.API_fs_Hz;
194
195
95.6M
    RESTORE_STACK;
196
95.6M
    return ret;
197
95.6M
}
control_codec.c:silk_setup_resamplers
Line
Count
Source
138
36.9M
{
139
36.9M
    opus_int   ret = SILK_NO_ERROR;
140
36.9M
    SAVE_STACK;
141
142
36.9M
    if( psEnc->sCmn.fs_kHz != fs_kHz || psEnc->sCmn.prev_API_fs_Hz != psEnc->sCmn.API_fs_Hz )
143
93.0k
    {
144
93.0k
        if( psEnc->sCmn.fs_kHz == 0 ) {
145
            /* Initialize the resampler for enc_API.c preparing resampling from API_fs_Hz to fs_kHz */
146
93.0k
            ret += silk_resampler_init( &psEnc->sCmn.resampler_state, psEnc->sCmn.API_fs_Hz, fs_kHz * 1000, 1 );
147
93.0k
        } else {
148
60
            VARDECL( opus_int16, x_buf_API_fs_Hz );
149
60
            VARDECL( silk_resampler_state_struct, temp_resampler_state );
150
60
#ifdef FIXED_POINT
151
60
            opus_int16 *x_bufFIX = psEnc->x_buf;
152
#else
153
            VARDECL( opus_int16, x_bufFIX );
154
            opus_int32 new_buf_samples;
155
#endif
156
60
            opus_int32 api_buf_samples;
157
60
            opus_int32 old_buf_samples;
158
60
            opus_int32 buf_length_ms;
159
160
60
            buf_length_ms = silk_LSHIFT( psEnc->sCmn.nb_subfr * 5, 1 ) + LA_SHAPE_MS;
161
60
            old_buf_samples = buf_length_ms * psEnc->sCmn.fs_kHz;
162
163
#ifndef FIXED_POINT
164
            new_buf_samples = buf_length_ms * fs_kHz;
165
            ALLOC( x_bufFIX, silk_max( old_buf_samples, new_buf_samples ),
166
                   opus_int16 );
167
            silk_float2short_array( x_bufFIX, psEnc->x_buf, old_buf_samples );
168
#endif
169
170
            /* Initialize resampler for temporary resampling of x_buf data to API_fs_Hz */
171
60
            ALLOC( temp_resampler_state, 1, silk_resampler_state_struct );
172
60
            ret += silk_resampler_init( temp_resampler_state, silk_SMULBB( psEnc->sCmn.fs_kHz, 1000 ), psEnc->sCmn.API_fs_Hz, 0 );
173
174
            /* Calculate number of samples to temporarily upsample */
175
60
            api_buf_samples = buf_length_ms * silk_DIV32_16( psEnc->sCmn.API_fs_Hz, 1000 );
176
177
            /* Temporary resampling of x_buf data to API_fs_Hz */
178
60
            ALLOC( x_buf_API_fs_Hz, api_buf_samples, opus_int16 );
179
60
            ret += silk_resampler( temp_resampler_state, x_buf_API_fs_Hz, x_bufFIX, old_buf_samples );
180
181
            /* Initialize the resampler for enc_API.c preparing resampling from API_fs_Hz to fs_kHz */
182
60
            ret += silk_resampler_init( &psEnc->sCmn.resampler_state, psEnc->sCmn.API_fs_Hz, silk_SMULBB( fs_kHz, 1000 ), 1 );
183
184
            /* Correct resampler state by resampling buffered data from API_fs_Hz to fs_kHz */
185
60
            ret += silk_resampler( &psEnc->sCmn.resampler_state, x_bufFIX, x_buf_API_fs_Hz, api_buf_samples );
186
187
#ifndef FIXED_POINT
188
            silk_short2float_array( psEnc->x_buf, x_bufFIX, new_buf_samples);
189
#endif
190
60
        }
191
93.0k
    }
192
193
36.9M
    psEnc->sCmn.prev_API_fs_Hz = psEnc->sCmn.API_fs_Hz;
194
195
36.9M
    RESTORE_STACK;
196
36.9M
    return ret;
197
36.9M
}
control_codec.c:silk_setup_resamplers
Line
Count
Source
138
58.7M
{
139
58.7M
    opus_int   ret = SILK_NO_ERROR;
140
58.7M
    SAVE_STACK;
141
142
58.7M
    if( psEnc->sCmn.fs_kHz != fs_kHz || psEnc->sCmn.prev_API_fs_Hz != psEnc->sCmn.API_fs_Hz )
143
124k
    {
144
124k
        if( psEnc->sCmn.fs_kHz == 0 ) {
145
            /* Initialize the resampler for enc_API.c preparing resampling from API_fs_Hz to fs_kHz */
146
124k
            ret += silk_resampler_init( &psEnc->sCmn.resampler_state, psEnc->sCmn.API_fs_Hz, fs_kHz * 1000, 1 );
147
124k
        } else {
148
152
            VARDECL( opus_int16, x_buf_API_fs_Hz );
149
152
            VARDECL( silk_resampler_state_struct, temp_resampler_state );
150
#ifdef FIXED_POINT
151
            opus_int16 *x_bufFIX = psEnc->x_buf;
152
#else
153
152
            VARDECL( opus_int16, x_bufFIX );
154
152
            opus_int32 new_buf_samples;
155
152
#endif
156
152
            opus_int32 api_buf_samples;
157
152
            opus_int32 old_buf_samples;
158
152
            opus_int32 buf_length_ms;
159
160
152
            buf_length_ms = silk_LSHIFT( psEnc->sCmn.nb_subfr * 5, 1 ) + LA_SHAPE_MS;
161
152
            old_buf_samples = buf_length_ms * psEnc->sCmn.fs_kHz;
162
163
152
#ifndef FIXED_POINT
164
152
            new_buf_samples = buf_length_ms * fs_kHz;
165
152
            ALLOC( x_bufFIX, silk_max( old_buf_samples, new_buf_samples ),
166
152
                   opus_int16 );
167
152
            silk_float2short_array( x_bufFIX, psEnc->x_buf, old_buf_samples );
168
152
#endif
169
170
            /* Initialize resampler for temporary resampling of x_buf data to API_fs_Hz */
171
152
            ALLOC( temp_resampler_state, 1, silk_resampler_state_struct );
172
152
            ret += silk_resampler_init( temp_resampler_state, silk_SMULBB( psEnc->sCmn.fs_kHz, 1000 ), psEnc->sCmn.API_fs_Hz, 0 );
173
174
            /* Calculate number of samples to temporarily upsample */
175
152
            api_buf_samples = buf_length_ms * silk_DIV32_16( psEnc->sCmn.API_fs_Hz, 1000 );
176
177
            /* Temporary resampling of x_buf data to API_fs_Hz */
178
152
            ALLOC( x_buf_API_fs_Hz, api_buf_samples, opus_int16 );
179
152
            ret += silk_resampler( temp_resampler_state, x_buf_API_fs_Hz, x_bufFIX, old_buf_samples );
180
181
            /* Initialize the resampler for enc_API.c preparing resampling from API_fs_Hz to fs_kHz */
182
152
            ret += silk_resampler_init( &psEnc->sCmn.resampler_state, psEnc->sCmn.API_fs_Hz, silk_SMULBB( fs_kHz, 1000 ), 1 );
183
184
            /* Correct resampler state by resampling buffered data from API_fs_Hz to fs_kHz */
185
152
            ret += silk_resampler( &psEnc->sCmn.resampler_state, x_bufFIX, x_buf_API_fs_Hz, api_buf_samples );
186
187
152
#ifndef FIXED_POINT
188
152
            silk_short2float_array( psEnc->x_buf, x_bufFIX, new_buf_samples);
189
152
#endif
190
152
        }
191
124k
    }
192
193
58.7M
    psEnc->sCmn.prev_API_fs_Hz = psEnc->sCmn.API_fs_Hz;
194
195
58.7M
    RESTORE_STACK;
196
58.7M
    return ret;
197
58.7M
}
198
199
static opus_int silk_setup_fs(
200
    silk_encoder_state_Fxx          *psEnc,             /* I/O                      */
201
    opus_int                        fs_kHz,             /* I                        */
202
    opus_int                        PacketSize_ms       /* I                        */
203
)
204
95.6M
{
205
95.6M
    opus_int ret = SILK_NO_ERROR;
206
207
    /* Set packet size */
208
95.6M
    if( PacketSize_ms != psEnc->sCmn.PacketSize_ms ) {
209
219k
        if( ( PacketSize_ms !=  10 ) &&
210
116k
            ( PacketSize_ms !=  20 ) &&
211
62.5k
            ( PacketSize_ms !=  40 ) &&
212
24.0k
            ( PacketSize_ms !=  60 ) ) {
213
0
            ret = SILK_ENC_PACKET_SIZE_NOT_SUPPORTED;
214
0
        }
215
219k
        if( PacketSize_ms <= 10 ) {
216
102k
            psEnc->sCmn.nFramesPerPacket = 1;
217
102k
            psEnc->sCmn.nb_subfr = PacketSize_ms == 10 ? 2 : 1;
218
102k
            psEnc->sCmn.frame_length = silk_SMULBB( PacketSize_ms, fs_kHz );
219
102k
            psEnc->sCmn.pitch_LPC_win_length = silk_SMULBB( FIND_PITCH_LPC_WIN_MS_2_SF, fs_kHz );
220
102k
            if( psEnc->sCmn.fs_kHz == 8 ) {
221
0
                psEnc->sCmn.pitch_contour_iCDF = silk_pitch_contour_10_ms_NB_iCDF;
222
102k
            } else {
223
102k
                psEnc->sCmn.pitch_contour_iCDF = silk_pitch_contour_10_ms_iCDF;
224
102k
            }
225
116k
        } else {
226
116k
            psEnc->sCmn.nFramesPerPacket = silk_DIV32_16( PacketSize_ms, MAX_FRAME_LENGTH_MS );
227
116k
            psEnc->sCmn.nb_subfr = MAX_NB_SUBFR;
228
116k
            psEnc->sCmn.frame_length = silk_SMULBB( 20, fs_kHz );
229
116k
            psEnc->sCmn.pitch_LPC_win_length = silk_SMULBB( FIND_PITCH_LPC_WIN_MS, fs_kHz );
230
116k
            if( psEnc->sCmn.fs_kHz == 8 ) {
231
200
                psEnc->sCmn.pitch_contour_iCDF = silk_pitch_contour_NB_iCDF;
232
116k
            } else {
233
116k
                psEnc->sCmn.pitch_contour_iCDF = silk_pitch_contour_iCDF;
234
116k
            }
235
116k
        }
236
219k
        psEnc->sCmn.PacketSize_ms  = PacketSize_ms;
237
219k
        psEnc->sCmn.TargetRate_bps = 0;         /* trigger new SNR computation */
238
219k
    }
239
240
    /* Set internal sampling frequency */
241
95.6M
    celt_assert( fs_kHz == 8 || fs_kHz == 12 || fs_kHz == 16 );
242
95.6M
    celt_assert( psEnc->sCmn.nb_subfr == 2 || psEnc->sCmn.nb_subfr == 4 );
243
95.6M
    if( psEnc->sCmn.fs_kHz != fs_kHz ) {
244
        /* reset part of the state */
245
217k
        silk_memset( &psEnc->sShape,               0, sizeof( psEnc->sShape ) );
246
217k
        silk_memset( &psEnc->sCmn.sNSQ,            0, sizeof( psEnc->sCmn.sNSQ ) );
247
217k
        silk_memset( psEnc->sCmn.prev_NLSFq_Q15,   0, sizeof( psEnc->sCmn.prev_NLSFq_Q15 ) );
248
217k
        silk_memset( &psEnc->sCmn.sLP.In_LP_State, 0, sizeof( psEnc->sCmn.sLP.In_LP_State ) );
249
217k
        psEnc->sCmn.inputBufIx                  = 0;
250
217k
        psEnc->sCmn.nFramesEncoded              = 0;
251
217k
        psEnc->sCmn.TargetRate_bps              = 0;     /* trigger new SNR computation */
252
253
        /* Initialize non-zero parameters */
254
217k
        psEnc->sCmn.prevLag                     = 100;
255
217k
        psEnc->sCmn.first_frame_after_reset     = 1;
256
217k
        psEnc->sShape.LastGainIndex             = 10;
257
217k
        psEnc->sCmn.sNSQ.lagPrev                = 100;
258
217k
        psEnc->sCmn.sNSQ.prev_gain_Q16          = 65536;
259
217k
        psEnc->sCmn.prevSignalType              = TYPE_NO_VOICE_ACTIVITY;
260
261
217k
        psEnc->sCmn.fs_kHz = fs_kHz;
262
217k
        if( psEnc->sCmn.fs_kHz == 8 ) {
263
160k
            if( psEnc->sCmn.nb_subfr == MAX_NB_SUBFR ) {
264
81.8k
                psEnc->sCmn.pitch_contour_iCDF = silk_pitch_contour_NB_iCDF;
265
81.8k
            } else {
266
79.0k
                psEnc->sCmn.pitch_contour_iCDF = silk_pitch_contour_10_ms_NB_iCDF;
267
79.0k
            }
268
160k
        } else {
269
56.9k
            if( psEnc->sCmn.nb_subfr == MAX_NB_SUBFR ) {
270
33.3k
                psEnc->sCmn.pitch_contour_iCDF = silk_pitch_contour_iCDF;
271
33.3k
            } else {
272
23.6k
                psEnc->sCmn.pitch_contour_iCDF = silk_pitch_contour_10_ms_iCDF;
273
23.6k
            }
274
56.9k
        }
275
217k
        if( psEnc->sCmn.fs_kHz == 8 || psEnc->sCmn.fs_kHz == 12 ) {
276
181k
            psEnc->sCmn.predictLPCOrder = MIN_LPC_ORDER;
277
181k
            psEnc->sCmn.psNLSF_CB  = &silk_NLSF_CB_NB_MB;
278
181k
        } else {
279
36.5k
            psEnc->sCmn.predictLPCOrder = MAX_LPC_ORDER;
280
36.5k
            psEnc->sCmn.psNLSF_CB  = &silk_NLSF_CB_WB;
281
36.5k
        }
282
217k
        psEnc->sCmn.subfr_length   = SUB_FRAME_LENGTH_MS * fs_kHz;
283
217k
        psEnc->sCmn.frame_length   = silk_SMULBB( psEnc->sCmn.subfr_length, psEnc->sCmn.nb_subfr );
284
217k
        psEnc->sCmn.ltp_mem_length = silk_SMULBB( LTP_MEM_LENGTH_MS, fs_kHz );
285
217k
        psEnc->sCmn.la_pitch       = silk_SMULBB( LA_PITCH_MS, fs_kHz );
286
217k
        psEnc->sCmn.max_pitch_lag  = silk_SMULBB( 18, fs_kHz );
287
217k
        if( psEnc->sCmn.nb_subfr == MAX_NB_SUBFR ) {
288
115k
            psEnc->sCmn.pitch_LPC_win_length = silk_SMULBB( FIND_PITCH_LPC_WIN_MS, fs_kHz );
289
115k
        } else {
290
102k
            psEnc->sCmn.pitch_LPC_win_length = silk_SMULBB( FIND_PITCH_LPC_WIN_MS_2_SF, fs_kHz );
291
102k
        }
292
217k
        if( psEnc->sCmn.fs_kHz == 16 ) {
293
36.5k
            psEnc->sCmn.pitch_lag_low_bits_iCDF = silk_uniform8_iCDF;
294
181k
        } else if( psEnc->sCmn.fs_kHz == 12 ) {
295
20.3k
            psEnc->sCmn.pitch_lag_low_bits_iCDF = silk_uniform6_iCDF;
296
160k
        } else {
297
160k
            psEnc->sCmn.pitch_lag_low_bits_iCDF = silk_uniform4_iCDF;
298
160k
        }
299
217k
    }
300
301
    /* Check that settings are valid */
302
95.6M
    celt_assert( ( psEnc->sCmn.subfr_length * psEnc->sCmn.nb_subfr ) == psEnc->sCmn.frame_length );
303
304
95.6M
    return ret;
305
95.6M
}
306
307
static opus_int silk_setup_complexity(
308
    silk_encoder_state              *psEncC,            /* I/O                      */
309
    opus_int                        Complexity          /* I                        */
310
)
311
95.6M
{
312
95.6M
    opus_int ret = 0;
313
314
    /* Set encoding complexity */
315
95.6M
    celt_assert( Complexity >= 0 && Complexity <= 10 );
316
95.6M
    if( Complexity < 1 ) {
317
31.9M
        psEncC->pitchEstimationComplexity       = SILK_PE_MIN_COMPLEX;
318
31.9M
        psEncC->pitchEstimationThreshold_Q16    = SILK_FIX_CONST( 0.8, 16 );
319
31.9M
        psEncC->pitchEstimationLPCOrder         = 6;
320
31.9M
        psEncC->shapingLPCOrder                 = 12;
321
31.9M
        psEncC->la_shape                        = 3 * psEncC->fs_kHz;
322
31.9M
        psEncC->nStatesDelayedDecision          = 1;
323
31.9M
        psEncC->useInterpolatedNLSFs            = 0;
324
31.9M
        psEncC->NLSF_MSVQ_Survivors             = 2;
325
31.9M
        psEncC->warping_Q16                     = 0;
326
63.7M
    } else if( Complexity < 2 ) {
327
12.9M
        psEncC->pitchEstimationComplexity       = SILK_PE_MID_COMPLEX;
328
12.9M
        psEncC->pitchEstimationThreshold_Q16    = SILK_FIX_CONST( 0.76, 16 );
329
12.9M
        psEncC->pitchEstimationLPCOrder         = 8;
330
12.9M
        psEncC->shapingLPCOrder                 = 14;
331
12.9M
        psEncC->la_shape                        = 5 * psEncC->fs_kHz;
332
12.9M
        psEncC->nStatesDelayedDecision          = 1;
333
12.9M
        psEncC->useInterpolatedNLSFs            = 0;
334
12.9M
        psEncC->NLSF_MSVQ_Survivors             = 3;
335
12.9M
        psEncC->warping_Q16                     = 0;
336
50.8M
    } else if( Complexity < 3 ) {
337
16.6M
        psEncC->pitchEstimationComplexity       = SILK_PE_MIN_COMPLEX;
338
16.6M
        psEncC->pitchEstimationThreshold_Q16    = SILK_FIX_CONST( 0.8, 16 );
339
16.6M
        psEncC->pitchEstimationLPCOrder         = 6;
340
16.6M
        psEncC->shapingLPCOrder                 = 12;
341
16.6M
        psEncC->la_shape                        = 3 * psEncC->fs_kHz;
342
16.6M
        psEncC->nStatesDelayedDecision          = 2;
343
16.6M
        psEncC->useInterpolatedNLSFs            = 0;
344
16.6M
        psEncC->NLSF_MSVQ_Survivors             = 2;
345
16.6M
        psEncC->warping_Q16                     = 0;
346
34.1M
    } else if( Complexity < 4 ) {
347
5.01M
        psEncC->pitchEstimationComplexity       = SILK_PE_MID_COMPLEX;
348
5.01M
        psEncC->pitchEstimationThreshold_Q16    = SILK_FIX_CONST( 0.76, 16 );
349
5.01M
        psEncC->pitchEstimationLPCOrder         = 8;
350
5.01M
        psEncC->shapingLPCOrder                 = 14;
351
5.01M
        psEncC->la_shape                        = 5 * psEncC->fs_kHz;
352
5.01M
        psEncC->nStatesDelayedDecision          = 2;
353
5.01M
        psEncC->useInterpolatedNLSFs            = 0;
354
5.01M
        psEncC->NLSF_MSVQ_Survivors             = 4;
355
5.01M
        psEncC->warping_Q16                     = 0;
356
29.1M
    } else if( Complexity < 6 ) {
357
10.0M
        psEncC->pitchEstimationComplexity       = SILK_PE_MID_COMPLEX;
358
10.0M
        psEncC->pitchEstimationThreshold_Q16    = SILK_FIX_CONST( 0.74, 16 );
359
10.0M
        psEncC->pitchEstimationLPCOrder         = 10;
360
10.0M
        psEncC->shapingLPCOrder                 = 16;
361
10.0M
        psEncC->la_shape                        = 5 * psEncC->fs_kHz;
362
10.0M
        psEncC->nStatesDelayedDecision          = 2;
363
10.0M
        psEncC->useInterpolatedNLSFs            = 1;
364
10.0M
        psEncC->NLSF_MSVQ_Survivors             = 6;
365
10.0M
        psEncC->warping_Q16                     = psEncC->fs_kHz * SILK_FIX_CONST( WARPING_MULTIPLIER, 16 );
366
19.1M
    } else if( Complexity < 8 ) {
367
10.2M
        psEncC->pitchEstimationComplexity       = SILK_PE_MID_COMPLEX;
368
10.2M
        psEncC->pitchEstimationThreshold_Q16    = SILK_FIX_CONST( 0.72, 16 );
369
10.2M
        psEncC->pitchEstimationLPCOrder         = 12;
370
10.2M
        psEncC->shapingLPCOrder                 = 20;
371
10.2M
        psEncC->la_shape                        = 5 * psEncC->fs_kHz;
372
10.2M
        psEncC->nStatesDelayedDecision          = 3;
373
10.2M
        psEncC->useInterpolatedNLSFs            = 1;
374
10.2M
        psEncC->NLSF_MSVQ_Survivors             = 8;
375
10.2M
        psEncC->warping_Q16                     = psEncC->fs_kHz * SILK_FIX_CONST( WARPING_MULTIPLIER, 16 );
376
10.2M
    } else {
377
8.90M
        psEncC->pitchEstimationComplexity       = SILK_PE_MAX_COMPLEX;
378
8.90M
        psEncC->pitchEstimationThreshold_Q16    = SILK_FIX_CONST( 0.7, 16 );
379
8.90M
        psEncC->pitchEstimationLPCOrder         = 16;
380
8.90M
        psEncC->shapingLPCOrder                 = 24;
381
8.90M
        psEncC->la_shape                        = 5 * psEncC->fs_kHz;
382
8.90M
        psEncC->nStatesDelayedDecision          = MAX_DEL_DEC_STATES;
383
8.90M
        psEncC->useInterpolatedNLSFs            = 1;
384
8.90M
        psEncC->NLSF_MSVQ_Survivors             = 16;
385
8.90M
        psEncC->warping_Q16                     = psEncC->fs_kHz * SILK_FIX_CONST( WARPING_MULTIPLIER, 16 );
386
8.90M
    }
387
388
    /* Do not allow higher pitch estimation LPC order than predict LPC order */
389
95.6M
    psEncC->pitchEstimationLPCOrder = silk_min_int( psEncC->pitchEstimationLPCOrder, psEncC->predictLPCOrder );
390
95.6M
    psEncC->shapeWinLength          = SUB_FRAME_LENGTH_MS * psEncC->fs_kHz + 2 * psEncC->la_shape;
391
95.6M
    psEncC->Complexity              = Complexity;
392
393
95.6M
    celt_assert( psEncC->pitchEstimationLPCOrder <= MAX_FIND_PITCH_LPC_ORDER );
394
95.6M
    celt_assert( psEncC->shapingLPCOrder         <= MAX_SHAPE_LPC_ORDER      );
395
95.6M
    celt_assert( psEncC->nStatesDelayedDecision  <= MAX_DEL_DEC_STATES       );
396
95.6M
    celt_assert( psEncC->warping_Q16             <= 32767                    );
397
95.6M
    celt_assert( psEncC->la_shape                <= LA_SHAPE_MAX             );
398
95.6M
    celt_assert( psEncC->shapeWinLength          <= SHAPE_LPC_WIN_MAX        );
399
400
95.6M
    return ret;
401
95.6M
}
402
403
static OPUS_INLINE opus_int silk_setup_LBRR(
404
    silk_encoder_state          *psEncC,            /* I/O                      */
405
    const silk_EncControlStruct *encControl         /* I                        */
406
)
407
95.6M
{
408
95.6M
    opus_int   LBRR_in_previous_packet, ret = SILK_NO_ERROR;
409
410
95.6M
    LBRR_in_previous_packet = psEncC->LBRR_enabled;
411
95.6M
    psEncC->LBRR_enabled = encControl->LBRR_coded;
412
95.6M
    if( psEncC->LBRR_enabled ) {
413
        /* Set gain increase for coding LBRR excitation */
414
33.0M
        if( LBRR_in_previous_packet == 0 ) {
415
            /* Previous packet did not have LBRR, and was therefore coded at a higher bitrate */
416
117k
            psEncC->LBRR_GainIncreases = 7;
417
32.8M
        } else {
418
32.8M
            psEncC->LBRR_GainIncreases = silk_max_int( 7 - silk_SMULWB( (opus_int32)psEncC->PacketLoss_perc, SILK_FIX_CONST( 0.2, 16 ) ), 3 );
419
32.8M
        }
420
33.0M
    }
421
422
95.6M
    return ret;
423
95.6M
}