Coverage Report

Created: 2025-08-29 07:57

/src/opus/silk/float/encode_frame_FLP.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
32
#include <stdlib.h>
33
#include "main_FLP.h"
34
#include "tuning_parameters.h"
35
36
/* Low Bitrate Redundancy (LBRR) encoding. Reuse all parameters but encode with lower bitrate */
37
static OPUS_INLINE void silk_LBRR_encode_FLP(
38
    silk_encoder_state_FLP          *psEnc,                             /* I/O  Encoder state FLP                           */
39
    silk_encoder_control_FLP        *psEncCtrl,                         /* I/O  Encoder control FLP                         */
40
    const silk_float                xfw[],                              /* I    Input signal                                */
41
    opus_int                        condCoding                          /* I    The type of conditional coding used so far for this frame */
42
);
43
44
void silk_encode_do_VAD_FLP(
45
    silk_encoder_state_FLP          *psEnc,                             /* I/O  Encoder state FLP                           */
46
    opus_int                        activity                            /* I    Decision of Opus voice activity detector    */
47
)
48
34.8M
{
49
34.8M
    const opus_int activity_threshold = SILK_FIX_CONST( SPEECH_ACTIVITY_DTX_THRES, 8 );
50
51
    /****************************/
52
    /* Voice Activity Detection */
53
    /****************************/
54
34.8M
    silk_VAD_GetSA_Q8( &psEnc->sCmn, psEnc->sCmn.inputBuf + 1, psEnc->sCmn.arch );
55
    /* If Opus VAD is inactive and Silk VAD is active: lower Silk VAD to just under the threshold */
56
34.8M
    if( activity == VAD_NO_ACTIVITY && psEnc->sCmn.speech_activity_Q8 >= activity_threshold ) {
57
2.20k
        psEnc->sCmn.speech_activity_Q8 = activity_threshold - 1;
58
2.20k
    }
59
60
    /**************************************************/
61
    /* Convert speech activity into VAD and DTX flags */
62
    /**************************************************/
63
34.8M
    if( psEnc->sCmn.speech_activity_Q8 < activity_threshold ) {
64
33.6M
        psEnc->sCmn.indices.signalType = TYPE_NO_VOICE_ACTIVITY;
65
33.6M
        psEnc->sCmn.noSpeechCounter++;
66
33.6M
        if( psEnc->sCmn.noSpeechCounter <= NB_SPEECH_FRAMES_BEFORE_DTX ) {
67
257k
            psEnc->sCmn.inDTX = 0;
68
33.4M
        } else if( psEnc->sCmn.noSpeechCounter > MAX_CONSECUTIVE_DTX + NB_SPEECH_FRAMES_BEFORE_DTX ) {
69
1.58M
            psEnc->sCmn.noSpeechCounter = NB_SPEECH_FRAMES_BEFORE_DTX;
70
1.58M
            psEnc->sCmn.inDTX           = 0;
71
1.58M
        }
72
33.6M
        psEnc->sCmn.VAD_flags[ psEnc->sCmn.nFramesEncoded ] = 0;
73
33.6M
    } else {
74
1.15M
        psEnc->sCmn.noSpeechCounter    = 0;
75
1.15M
        psEnc->sCmn.inDTX              = 0;
76
1.15M
        psEnc->sCmn.indices.signalType = TYPE_UNVOICED;
77
1.15M
        psEnc->sCmn.VAD_flags[ psEnc->sCmn.nFramesEncoded ] = 1;
78
1.15M
    }
79
34.8M
}
80
81
/****************/
82
/* Encode frame */
83
/****************/
84
opus_int silk_encode_frame_FLP(
85
    silk_encoder_state_FLP          *psEnc,                             /* I/O  Encoder state FLP                           */
86
    opus_int32                      *pnBytesOut,                        /* O    Number of payload bytes;                    */
87
    ec_enc                          *psRangeEnc,                        /* I/O  compressor data structure                   */
88
    opus_int                        condCoding,                         /* I    The type of conditional coding to use       */
89
    opus_int                        maxBits,                            /* I    If > 0: maximum number of output bits       */
90
    opus_int                        useCBR                              /* I    Flag to force constant-bitrate operation    */
91
)
92
34.8M
{
93
34.8M
    silk_encoder_control_FLP sEncCtrl;
94
34.8M
    opus_int     i, iter, maxIter, found_upper, found_lower, ret = 0;
95
34.8M
    silk_float   *x_frame, *res_pitch_frame;
96
34.8M
    silk_float   res_pitch[ 2 * MAX_FRAME_LENGTH + LA_PITCH_MAX ];
97
34.8M
    ec_enc       sRangeEnc_copy, sRangeEnc_copy2;
98
34.8M
    silk_nsq_state sNSQ_copy, sNSQ_copy2;
99
34.8M
    opus_int32   seed_copy, nBits, nBits_lower, nBits_upper, gainMult_lower, gainMult_upper;
100
34.8M
    opus_int32   gainsID, gainsID_lower, gainsID_upper;
101
34.8M
    opus_int16   gainMult_Q8;
102
34.8M
    opus_int16   ec_prevLagIndex_copy;
103
34.8M
    opus_int     ec_prevSignalType_copy;
104
34.8M
    opus_int8    LastGainIndex_copy2;
105
34.8M
    opus_int32   pGains_Q16[ MAX_NB_SUBFR ];
106
34.8M
    opus_uint8   ec_buf_copy[ 1275 ];
107
34.8M
    opus_int     gain_lock[ MAX_NB_SUBFR ] = {0};
108
34.8M
    opus_int16   best_gain_mult[ MAX_NB_SUBFR ];
109
34.8M
    opus_int     best_sum[ MAX_NB_SUBFR ];
110
34.8M
    opus_int     bits_margin;
111
112
    /* For CBR, 5 bits below budget is close enough. For VBR, allow up to 25% below the cap if we initially busted the budget. */
113
34.8M
    bits_margin = useCBR ? 5 : maxBits/4;
114
    /* This is totally unnecessary but many compilers (including gcc) are too dumb to realise it */
115
34.8M
    LastGainIndex_copy2 = nBits_lower = nBits_upper = gainMult_lower = gainMult_upper = 0;
116
117
34.8M
    psEnc->sCmn.indices.Seed = psEnc->sCmn.frameCounter++ & 3;
118
119
    /**************************************************************/
120
    /* Set up Input Pointers, and insert frame in input buffer    */
121
    /**************************************************************/
122
    /* pointers aligned with start of frame to encode */
123
34.8M
    x_frame         = psEnc->x_buf + psEnc->sCmn.ltp_mem_length;    /* start of frame to encode */
124
34.8M
    res_pitch_frame = res_pitch    + psEnc->sCmn.ltp_mem_length;    /* start of pitch LPC residual frame */
125
126
    /***************************************/
127
    /* Ensure smooth bandwidth transitions */
128
    /***************************************/
129
34.8M
    silk_LP_variable_cutoff( &psEnc->sCmn.sLP, psEnc->sCmn.inputBuf + 1, psEnc->sCmn.frame_length );
130
131
    /*******************************************/
132
    /* Copy new frame to front of input buffer */
133
    /*******************************************/
134
34.8M
    silk_short2float_array( x_frame + LA_SHAPE_MS * psEnc->sCmn.fs_kHz, psEnc->sCmn.inputBuf + 1, psEnc->sCmn.frame_length );
135
136
    /* Add tiny signal to avoid high CPU load from denormalized floating point numbers */
137
313M
    for( i = 0; i < 8; i++ ) {
138
278M
        x_frame[ LA_SHAPE_MS * psEnc->sCmn.fs_kHz + i * ( psEnc->sCmn.frame_length >> 3 ) ] += ( 1 - ( i & 2 ) ) * 1e-6f;
139
278M
    }
140
141
34.8M
    if( !psEnc->sCmn.prefillFlag ) {
142
        /*****************************************/
143
        /* Find pitch lags, initial LPC analysis */
144
        /*****************************************/
145
34.8M
        silk_find_pitch_lags_FLP( psEnc, &sEncCtrl, res_pitch, x_frame, psEnc->sCmn.arch );
146
147
        /************************/
148
        /* Noise shape analysis */
149
        /************************/
150
34.8M
        silk_noise_shape_analysis_FLP( psEnc, &sEncCtrl, res_pitch_frame, x_frame );
151
152
        /***************************************************/
153
        /* Find linear prediction coefficients (LPC + LTP) */
154
        /***************************************************/
155
34.8M
        silk_find_pred_coefs_FLP( psEnc, &sEncCtrl, res_pitch_frame, x_frame, condCoding );
156
157
        /****************************************/
158
        /* Process gains                        */
159
        /****************************************/
160
34.8M
        silk_process_gains_FLP( psEnc, &sEncCtrl, condCoding );
161
162
        /****************************************/
163
        /* Low Bitrate Redundant Encoding       */
164
        /****************************************/
165
34.8M
        silk_LBRR_encode_FLP( psEnc, &sEncCtrl, x_frame, condCoding );
166
167
        /* Loop over quantizer and entroy coding to control bitrate */
168
34.8M
        maxIter = 6;
169
34.8M
        gainMult_Q8 = SILK_FIX_CONST( 1, 8 );
170
34.8M
        found_lower = 0;
171
34.8M
        found_upper = 0;
172
34.8M
        gainsID = silk_gains_ID( psEnc->sCmn.indices.GainsIndices, psEnc->sCmn.nb_subfr );
173
34.8M
        gainsID_lower = -1;
174
34.8M
        gainsID_upper = -1;
175
        /* Copy part of the input state */
176
34.8M
        silk_memcpy( &sRangeEnc_copy, psRangeEnc, sizeof( ec_enc ) );
177
34.8M
        silk_memcpy( &sNSQ_copy, &psEnc->sCmn.sNSQ, sizeof( silk_nsq_state ) );
178
34.8M
        seed_copy = psEnc->sCmn.indices.Seed;
179
34.8M
        ec_prevLagIndex_copy = psEnc->sCmn.ec_prevLagIndex;
180
34.8M
        ec_prevSignalType_copy = psEnc->sCmn.ec_prevSignalType;
181
111M
        for( iter = 0; ; iter++ ) {
182
111M
            if( gainsID == gainsID_lower ) {
183
74.5M
                nBits = nBits_lower;
184
74.5M
            } else if( gainsID == gainsID_upper ) {
185
274k
                nBits = nBits_upper;
186
36.9M
            } else {
187
                /* Restore part of the input state */
188
36.9M
                if( iter > 0 ) {
189
2.06M
                    silk_memcpy( psRangeEnc, &sRangeEnc_copy, sizeof( ec_enc ) );
190
2.06M
                    silk_memcpy( &psEnc->sCmn.sNSQ, &sNSQ_copy, sizeof( silk_nsq_state ) );
191
2.06M
                    psEnc->sCmn.indices.Seed = seed_copy;
192
2.06M
                    psEnc->sCmn.ec_prevLagIndex = ec_prevLagIndex_copy;
193
2.06M
                    psEnc->sCmn.ec_prevSignalType = ec_prevSignalType_copy;
194
2.06M
                }
195
196
                /*****************************************/
197
                /* Noise shaping quantization            */
198
                /*****************************************/
199
36.9M
                silk_NSQ_wrapper_FLP( psEnc, &sEncCtrl, &psEnc->sCmn.indices, &psEnc->sCmn.sNSQ, psEnc->sCmn.pulses, x_frame );
200
201
36.9M
                if ( iter == maxIter && !found_lower ) {
202
59.4k
                    silk_memcpy( &sRangeEnc_copy2, psRangeEnc, sizeof( ec_enc ) );
203
59.4k
                }
204
205
                /****************************************/
206
                /* Encode Parameters                    */
207
                /****************************************/
208
36.9M
                silk_encode_indices( &psEnc->sCmn, psRangeEnc, psEnc->sCmn.nFramesEncoded, 0, condCoding );
209
210
                /****************************************/
211
                /* Encode Excitation Signal             */
212
                /****************************************/
213
36.9M
                silk_encode_pulses( psRangeEnc, psEnc->sCmn.indices.signalType, psEnc->sCmn.indices.quantOffsetType,
214
36.9M
                      psEnc->sCmn.pulses, psEnc->sCmn.frame_length );
215
216
36.9M
                nBits = ec_tell( psRangeEnc );
217
218
                /* If we still bust after the last iteration, do some damage control. */
219
36.9M
                if ( iter == maxIter && !found_lower && nBits > maxBits ) {
220
55.5k
                    silk_memcpy( psRangeEnc, &sRangeEnc_copy2, sizeof( ec_enc ) );
221
222
                    /* Keep gains the same as the last frame. */
223
55.5k
                    psEnc->sShape.LastGainIndex = sEncCtrl.lastGainIndexPrev;
224
258k
                    for ( i = 0; i < psEnc->sCmn.nb_subfr; i++ ) {
225
202k
                        psEnc->sCmn.indices.GainsIndices[ i ] = 4;
226
202k
                    }
227
55.5k
                    if (condCoding != CODE_CONDITIONALLY) {
228
42.9k
                       psEnc->sCmn.indices.GainsIndices[ 0 ] = sEncCtrl.lastGainIndexPrev;
229
42.9k
                    }
230
55.5k
                    psEnc->sCmn.ec_prevLagIndex = ec_prevLagIndex_copy;
231
55.5k
                    psEnc->sCmn.ec_prevSignalType = ec_prevSignalType_copy;
232
                    /* Clear all pulses. */
233
10.5M
                    for ( i = 0; i < psEnc->sCmn.frame_length; i++ ) {
234
10.4M
                        psEnc->sCmn.pulses[ i ] = 0;
235
10.4M
                    }
236
237
55.5k
                    silk_encode_indices( &psEnc->sCmn, psRangeEnc, psEnc->sCmn.nFramesEncoded, 0, condCoding );
238
239
55.5k
                    silk_encode_pulses( psRangeEnc, psEnc->sCmn.indices.signalType, psEnc->sCmn.indices.quantOffsetType,
240
55.5k
                        psEnc->sCmn.pulses, psEnc->sCmn.frame_length );
241
242
55.5k
                    nBits = ec_tell( psRangeEnc );
243
55.5k
                }
244
245
36.9M
                if( useCBR == 0 && iter == 0 && nBits <= maxBits ) {
246
21.9M
                    break;
247
21.9M
                }
248
36.9M
            }
249
250
89.7M
            if( iter == maxIter ) {
251
12.7M
                if( found_lower && ( gainsID == gainsID_lower || nBits > maxBits ) ) {
252
                    /* Restore output state from earlier iteration that did meet the bitrate budget */
253
12.5M
                    silk_memcpy( psRangeEnc, &sRangeEnc_copy2, sizeof( ec_enc ) );
254
12.5M
                    celt_assert( sRangeEnc_copy2.offs <= 1275 );
255
12.5M
                    silk_memcpy( psRangeEnc->buf, ec_buf_copy, sRangeEnc_copy2.offs );
256
12.5M
                    silk_memcpy( &psEnc->sCmn.sNSQ, &sNSQ_copy2, sizeof( silk_nsq_state ) );
257
12.5M
                    psEnc->sShape.LastGainIndex = LastGainIndex_copy2;
258
12.5M
                }
259
12.7M
                break;
260
12.7M
            }
261
262
77.0M
            if( nBits > maxBits ) {
263
1.07M
                if( found_lower == 0 && iter >= 2 ) {
264
                    /* Adjust the quantizer's rate/distortion tradeoff and discard previous "upper" results */
265
333k
                    sEncCtrl.Lambda = silk_max_float(sEncCtrl.Lambda*1.5f, 1.5f);
266
                    /* Reducing dithering can help us hit the target. */
267
333k
                    psEnc->sCmn.indices.quantOffsetType = 0;
268
333k
                    found_upper = 0;
269
333k
                    gainsID_upper = -1;
270
745k
                } else {
271
745k
                    found_upper = 1;
272
745k
                    nBits_upper = nBits;
273
745k
                    gainMult_upper = gainMult_Q8;
274
745k
                    gainsID_upper = gainsID;
275
745k
                }
276
75.9M
            } else if( nBits < maxBits - bits_margin ) {
277
75.7M
                found_lower = 1;
278
75.7M
                nBits_lower = nBits;
279
75.7M
                gainMult_lower = gainMult_Q8;
280
75.7M
                if( gainsID != gainsID_lower ) {
281
13.7M
                    gainsID_lower = gainsID;
282
                    /* Copy part of the output state */
283
13.7M
                    silk_memcpy( &sRangeEnc_copy2, psRangeEnc, sizeof( ec_enc ) );
284
13.7M
                    celt_assert( psRangeEnc->offs <= 1275 );
285
13.7M
                    silk_memcpy( ec_buf_copy, psRangeEnc->buf, psRangeEnc->offs );
286
13.7M
                    silk_memcpy( &sNSQ_copy2, &psEnc->sCmn.sNSQ, sizeof( silk_nsq_state ) );
287
13.7M
                    LastGainIndex_copy2 = psEnc->sShape.LastGainIndex;
288
13.7M
                }
289
75.7M
            } else {
290
                /* Close enough */
291
172k
                break;
292
172k
            }
293
294
76.8M
            if ( !found_lower && nBits > maxBits ) {
295
749k
                int j;
296
3.47M
                for ( i = 0; i < psEnc->sCmn.nb_subfr; i++ ) {
297
2.72M
                    int sum=0;
298
147M
                    for ( j = i*psEnc->sCmn.subfr_length; j < (i+1)*psEnc->sCmn.subfr_length; j++ ) {
299
144M
                        sum += abs( psEnc->sCmn.pulses[j] );
300
144M
                    }
301
2.72M
                    if ( iter == 0 || (sum < best_sum[i] && !gain_lock[i]) ) {
302
1.43M
                        best_sum[i] = sum;
303
1.43M
                        best_gain_mult[i] = gainMult_Q8;
304
1.43M
                    } else {
305
1.29M
                        gain_lock[i] = 1;
306
1.29M
                    }
307
2.72M
                }
308
749k
            }
309
76.8M
            if( ( found_lower & found_upper ) == 0 ) {
310
                /* Adjust gain according to high-rate rate/distortion curve */
311
76.2M
                if( nBits > maxBits ) {
312
749k
                    gainMult_Q8 = silk_min_32( 1024, gainMult_Q8*3/2 );
313
75.4M
                } else {
314
75.4M
                    gainMult_Q8 = silk_max_32( 64, gainMult_Q8*4/5 );
315
75.4M
                }
316
76.2M
            } else {
317
                /* Adjust gain by interpolating */
318
622k
                gainMult_Q8 = gainMult_lower + ( ( gainMult_upper - gainMult_lower ) * ( maxBits - nBits_lower ) ) / ( nBits_upper - nBits_lower );
319
                /* New gain multiplier must be between 25% and 75% of old range (note that gainMult_upper < gainMult_lower) */
320
622k
                if( gainMult_Q8 > silk_ADD_RSHIFT32( gainMult_lower, gainMult_upper - gainMult_lower, 2 ) ) {
321
98.7k
                    gainMult_Q8 = silk_ADD_RSHIFT32( gainMult_lower, gainMult_upper - gainMult_lower, 2 );
322
98.7k
                } else
323
523k
                if( gainMult_Q8 < silk_SUB_RSHIFT32( gainMult_upper, gainMult_upper - gainMult_lower, 2 ) ) {
324
148k
                    gainMult_Q8 = silk_SUB_RSHIFT32( gainMult_upper, gainMult_upper - gainMult_lower, 2 );
325
148k
                }
326
622k
            }
327
328
303M
            for( i = 0; i < psEnc->sCmn.nb_subfr; i++ ) {
329
226M
                opus_int16 tmp;
330
226M
                if ( gain_lock[i] ) {
331
1.50M
                    tmp = best_gain_mult[i];
332
225M
                } else {
333
225M
                    tmp = gainMult_Q8;
334
225M
                }
335
226M
                pGains_Q16[ i ] = silk_LSHIFT_SAT32( silk_SMULWB( sEncCtrl.GainsUnq_Q16[ i ], tmp ), 8 );
336
226M
            }
337
338
            /* Quantize gains */
339
76.8M
            psEnc->sShape.LastGainIndex = sEncCtrl.lastGainIndexPrev;
340
76.8M
            silk_gains_quant( psEnc->sCmn.indices.GainsIndices, pGains_Q16,
341
76.8M
                  &psEnc->sShape.LastGainIndex, condCoding == CODE_CONDITIONALLY, psEnc->sCmn.nb_subfr );
342
343
            /* Unique identifier of gains vector */
344
76.8M
            gainsID = silk_gains_ID( psEnc->sCmn.indices.GainsIndices, psEnc->sCmn.nb_subfr );
345
346
            /* Overwrite unquantized gains with quantized gains and convert back to Q0 from Q16 */
347
303M
            for( i = 0; i < psEnc->sCmn.nb_subfr; i++ ) {
348
226M
                sEncCtrl.Gains[ i ] = pGains_Q16[ i ] / 65536.0f;
349
226M
            }
350
76.8M
        }
351
34.8M
    }
352
353
    /* Update input buffer */
354
34.8M
    silk_memmove( psEnc->x_buf, &psEnc->x_buf[ psEnc->sCmn.frame_length ],
355
34.8M
        ( psEnc->sCmn.ltp_mem_length + LA_SHAPE_MS * psEnc->sCmn.fs_kHz ) * sizeof( silk_float ) );
356
357
    /* Exit without entropy coding */
358
34.8M
    if( psEnc->sCmn.prefillFlag ) {
359
        /* No payload */
360
846
        *pnBytesOut = 0;
361
846
        return ret;
362
846
    }
363
364
    /* Parameters needed for next frame */
365
34.8M
    psEnc->sCmn.prevLag        = sEncCtrl.pitchL[ psEnc->sCmn.nb_subfr - 1 ];
366
34.8M
    psEnc->sCmn.prevSignalType = psEnc->sCmn.indices.signalType;
367
368
    /****************************************/
369
    /* Finalize payload                     */
370
    /****************************************/
371
34.8M
    psEnc->sCmn.first_frame_after_reset = 0;
372
    /* Payload size */
373
34.8M
    *pnBytesOut = silk_RSHIFT( ec_tell( psRangeEnc ) + 7, 3 );
374
375
34.8M
    return ret;
376
34.8M
}
377
378
/* Low-Bitrate Redundancy (LBRR) encoding. Reuse all parameters but encode excitation at lower bitrate  */
379
static OPUS_INLINE void silk_LBRR_encode_FLP(
380
    silk_encoder_state_FLP          *psEnc,                             /* I/O  Encoder state FLP                           */
381
    silk_encoder_control_FLP        *psEncCtrl,                         /* I/O  Encoder control FLP                         */
382
    const silk_float                xfw[],                              /* I    Input signal                                */
383
    opus_int                        condCoding                          /* I    The type of conditional coding used so far for this frame */
384
)
385
34.8M
{
386
34.8M
    opus_int     k;
387
34.8M
    opus_int32   Gains_Q16[ MAX_NB_SUBFR ];
388
34.8M
    silk_float   TempGains[ MAX_NB_SUBFR ];
389
34.8M
    SideInfoIndices *psIndices_LBRR = &psEnc->sCmn.indices_LBRR[ psEnc->sCmn.nFramesEncoded ];
390
34.8M
    silk_nsq_state sNSQ_LBRR;
391
392
    /*******************************************/
393
    /* Control use of inband LBRR              */
394
    /*******************************************/
395
34.8M
    if( psEnc->sCmn.LBRR_enabled && psEnc->sCmn.speech_activity_Q8 > SILK_FIX_CONST( LBRR_SPEECH_ACTIVITY_THRES, 8 ) ) {
396
492k
        psEnc->sCmn.LBRR_flags[ psEnc->sCmn.nFramesEncoded ] = 1;
397
398
        /* Copy noise shaping quantizer state and quantization indices from regular encoding */
399
492k
        silk_memcpy( &sNSQ_LBRR, &psEnc->sCmn.sNSQ, sizeof( silk_nsq_state ) );
400
492k
        silk_memcpy( psIndices_LBRR, &psEnc->sCmn.indices, sizeof( SideInfoIndices ) );
401
402
        /* Save original gains */
403
492k
        silk_memcpy( TempGains, psEncCtrl->Gains, psEnc->sCmn.nb_subfr * sizeof( silk_float ) );
404
405
492k
        if( psEnc->sCmn.nFramesEncoded == 0 || psEnc->sCmn.LBRR_flags[ psEnc->sCmn.nFramesEncoded - 1 ] == 0 ) {
406
            /* First frame in packet or previous frame not LBRR coded */
407
387k
            psEnc->sCmn.LBRRprevLastGainIndex = psEnc->sShape.LastGainIndex;
408
409
            /* Increase Gains to get target LBRR rate */
410
387k
            psIndices_LBRR->GainsIndices[ 0 ] += psEnc->sCmn.LBRR_GainIncreases;
411
387k
            psIndices_LBRR->GainsIndices[ 0 ] = silk_min_int( psIndices_LBRR->GainsIndices[ 0 ], N_LEVELS_QGAIN - 1 );
412
387k
        }
413
414
        /* Decode to get gains in sync with decoder */
415
492k
        silk_gains_dequant( Gains_Q16, psIndices_LBRR->GainsIndices,
416
492k
            &psEnc->sCmn.LBRRprevLastGainIndex, condCoding == CODE_CONDITIONALLY, psEnc->sCmn.nb_subfr );
417
418
        /* Overwrite unquantized gains with quantized gains and convert back to Q0 from Q16 */
419
2.24M
        for( k = 0; k <  psEnc->sCmn.nb_subfr; k++ ) {
420
1.74M
            psEncCtrl->Gains[ k ] = Gains_Q16[ k ] * ( 1.0f / 65536.0f );
421
1.74M
        }
422
423
        /*****************************************/
424
        /* Noise shaping quantization            */
425
        /*****************************************/
426
492k
        silk_NSQ_wrapper_FLP( psEnc, psEncCtrl, psIndices_LBRR, &sNSQ_LBRR,
427
492k
            psEnc->sCmn.pulses_LBRR[ psEnc->sCmn.nFramesEncoded ], xfw );
428
429
        /* Restore original gains */
430
492k
        silk_memcpy( psEncCtrl->Gains, TempGains, psEnc->sCmn.nb_subfr * sizeof( silk_float ) );
431
492k
    }
432
34.8M
}