Coverage Report

Created: 2026-10-28 08:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opus/silk/dec_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 "API.h"
32
#include "main.h"
33
#include "stack_alloc.h"
34
#include "os_support.h"
35
36
#ifdef ENABLE_OSCE
37
#include "osce.h"
38
#include "osce_structs.h"
39
#ifdef ENABLE_OSCE_BWE
40
#include "osce_features.h"
41
#endif
42
#endif
43
44
/************************/
45
/* Decoder Super Struct */
46
/************************/
47
typedef struct {
48
    silk_decoder_state          channel_state[ DECODER_NUM_CHANNELS ];
49
    stereo_dec_state                sStereo;
50
    opus_int                         nChannelsAPI;
51
    opus_int                         nChannelsInternal;
52
    opus_int                         prev_decode_only_middle;
53
#ifdef ENABLE_OSCE
54
    OSCEModel                        osce_model;
55
#endif
56
} silk_decoder;
57
58
/*********************/
59
/* Decoder functions */
60
/*********************/
61
62
63
64
opus_int silk_LoadOSCEModels(void *decState, const unsigned char *data, int len)
65
51.7k
{
66
#ifdef ENABLE_OSCE
67
    opus_int ret = SILK_NO_ERROR;
68
69
    ret = osce_load_models(&((silk_decoder *)decState)->osce_model, data, len);
70
    ((silk_decoder *)decState)->osce_model.loaded = (ret == 0);
71
    return ret;
72
#else
73
51.7k
    (void) decState;
74
51.7k
    (void) data;
75
51.7k
    (void) len;
76
51.7k
    return SILK_NO_ERROR;
77
51.7k
#endif
78
51.7k
}
79
80
opus_int silk_Get_Decoder_Size(                         /* O    Returns error code                              */
81
    opus_int                        *decSizeBytes       /* O    Number of bytes in SILK decoder state           */
82
)
83
1.02M
{
84
1.02M
    opus_int ret = SILK_NO_ERROR;
85
86
1.02M
    *decSizeBytes = sizeof( silk_decoder );
87
88
1.02M
    return ret;
89
1.02M
}
90
91
/* Reset decoder state */
92
opus_int silk_ResetDecoder(                              /* O    Returns error code                              */
93
    void                            *decState           /* I/O  State                                           */
94
)
95
997k
{
96
997k
    opus_int n, ret = SILK_NO_ERROR;
97
997k
    silk_decoder_state *channel_state = ((silk_decoder *)decState)->channel_state;
98
99
2.99M
    for( n = 0; n < DECODER_NUM_CHANNELS; n++ ) {
100
1.99M
        ret  = silk_reset_decoder( &channel_state[ n ] );
101
1.99M
    }
102
997k
    silk_memset(&((silk_decoder *)decState)->sStereo, 0, sizeof(((silk_decoder *)decState)->sStereo));
103
    /* Not strictly needed, but it's cleaner that way */
104
997k
    ((silk_decoder *)decState)->prev_decode_only_middle = 0;
105
106
997k
    return ret;
107
997k
}
108
109
110
opus_int silk_InitDecoder(                              /* O    Returns error code                              */
111
    void                            *decState           /* I/O  State                                           */
112
)
113
51.7k
{
114
51.7k
    opus_int n, ret = SILK_NO_ERROR;
115
51.7k
    silk_decoder_state *channel_state = ((silk_decoder *)decState)->channel_state;
116
#ifdef ENABLE_OSCE
117
    ((silk_decoder *)decState)->osce_model.loaded = 0;
118
#endif
119
51.7k
#ifndef USE_WEIGHTS_FILE
120
    /* load osce models */
121
51.7k
    silk_LoadOSCEModels(decState, NULL, 0);
122
51.7k
#endif
123
124
155k
    for( n = 0; n < DECODER_NUM_CHANNELS; n++ ) {
125
103k
        ret  = silk_init_decoder( &channel_state[ n ] );
126
103k
    }
127
51.7k
    silk_memset(&((silk_decoder *)decState)->sStereo, 0, sizeof(((silk_decoder *)decState)->sStereo));
128
    /* Not strictly needed, but it's cleaner that way */
129
51.7k
    ((silk_decoder *)decState)->prev_decode_only_middle = 0;
130
131
51.7k
    return ret;
132
51.7k
}
133
134
/* Decode a frame */
135
opus_int silk_Decode(                                   /* O    Returns error code                              */
136
    void*                           decState,           /* I/O  State                                           */
137
    silk_DecControlStruct*          decControl,         /* I/O  Control Structure                               */
138
    opus_int                        lostFlag,           /* I    0: no loss, 1 loss, 2 decode fec                */
139
    opus_int                        newPacketFlag,      /* I    Indicates first decoder call for this packet    */
140
    ec_dec                          *psRangeDec,        /* I/O  Compressor data structure                       */
141
    opus_res                        *samplesOut,        /* O    Decoded output speech vector                    */
142
    opus_int32                      *nSamplesOut,       /* O    Number of samples decoded                       */
143
#ifdef ENABLE_DEEP_PLC
144
    LPCNetPLCState                  *lpcnet,
145
#endif
146
    int                             arch                /* I    Run-time architecture                           */
147
)
148
194k
{
149
194k
    opus_int   i, n, decode_only_middle = 0, ret = SILK_NO_ERROR;
150
194k
    opus_int32 nSamplesOutDec, LBRR_symbol;
151
194k
    opus_int16 *samplesOut1_tmp[ 2 ];
152
194k
    VARDECL( opus_int16, samplesOut1_tmp_storage1 );
153
194k
    VARDECL( opus_int16, samplesOut2_tmp );
154
194k
    opus_int32 MS_pred_Q13[ 2 ] = { 0 };
155
194k
    opus_int16 *resample_out_ptr;
156
194k
    silk_decoder *psDec = ( silk_decoder * )decState;
157
194k
    silk_decoder_state *channel_state = psDec->channel_state;
158
194k
    opus_int has_side;
159
194k
    opus_int stereo_to_mono;
160
#ifdef ENABLE_OSCE_BWE
161
    VARDECL( opus_int16, resamp_buffer );
162
#endif
163
194k
    SAVE_STACK;
164
165
194k
    celt_assert( decControl->nChannelsInternal == 1 || decControl->nChannelsInternal == 2 );
166
167
#ifdef ENABLE_OSCE_BWE
168
    /* Bandwidth extension needs the OSCE models. Fall back to plain
169
       resampling when they have not been loaded. */
170
    if( !psDec->osce_model.loaded ) {
171
        if( decControl->osce_extended_mode == OSCE_MODE_SILK_BBWE ) {
172
            decControl->osce_extended_mode = OSCE_MODE_SILK_ONLY;
173
        }
174
        if( decControl->prev_osce_extended_mode == OSCE_MODE_SILK_BBWE ) {
175
            decControl->prev_osce_extended_mode = OSCE_MODE_SILK_ONLY;
176
        }
177
    }
178
#endif
179
180
    /**********************************/
181
    /* Test if first frame in payload */
182
    /**********************************/
183
194k
    if( newPacketFlag ) {
184
396k
        for( n = 0; n < decControl->nChannelsInternal; n++ ) {
185
224k
            channel_state[ n ].nFramesDecoded = 0;  /* Used to count frames in packet */
186
224k
        }
187
172k
    }
188
189
    /* If Mono -> Stereo transition in bitstream: init state of second channel */
190
194k
    if( decControl->nChannelsInternal > psDec->nChannelsInternal ) {
191
5.61k
        ret += silk_init_decoder( &channel_state[ 1 ] );
192
5.61k
    }
193
194
194k
    stereo_to_mono = decControl->nChannelsInternal == 1 && psDec->nChannelsInternal == 2 &&
195
2.57k
                     ( decControl->internalSampleRate == 1000*channel_state[ 0 ].fs_kHz );
196
197
194k
    if( channel_state[ 0 ].nFramesDecoded == 0 ) {
198
396k
        for( n = 0; n < decControl->nChannelsInternal; n++ ) {
199
224k
            opus_int fs_kHz_dec;
200
224k
            if( decControl->payloadSize_ms == 0 ) {
201
                /* Assuming packet loss, use 10 ms */
202
0
                channel_state[ n ].nFramesPerPacket = 1;
203
0
                channel_state[ n ].nb_subfr = 2;
204
224k
            } else if( decControl->payloadSize_ms == 10 ) {
205
152k
                channel_state[ n ].nFramesPerPacket = 1;
206
152k
                channel_state[ n ].nb_subfr = 2;
207
152k
            } else if( decControl->payloadSize_ms == 20 ) {
208
42.4k
                channel_state[ n ].nFramesPerPacket = 1;
209
42.4k
                channel_state[ n ].nb_subfr = 4;
210
42.4k
            } else if( decControl->payloadSize_ms == 40 ) {
211
19.8k
                channel_state[ n ].nFramesPerPacket = 2;
212
19.8k
                channel_state[ n ].nb_subfr = 4;
213
19.8k
            } else if( decControl->payloadSize_ms == 60 ) {
214
9.42k
                channel_state[ n ].nFramesPerPacket = 3;
215
9.42k
                channel_state[ n ].nb_subfr = 4;
216
9.42k
            } else {
217
0
                celt_assert( 0 );
218
0
                RESTORE_STACK;
219
0
                return SILK_DEC_INVALID_FRAME_SIZE;
220
0
            }
221
224k
            fs_kHz_dec = ( decControl->internalSampleRate >> 10 ) + 1;
222
224k
            if( fs_kHz_dec != 8 && fs_kHz_dec != 12 && fs_kHz_dec != 16 ) {
223
0
                celt_assert( 0 );
224
0
                RESTORE_STACK;
225
0
                return SILK_DEC_INVALID_SAMPLING_FREQUENCY;
226
0
            }
227
224k
            ret += silk_decoder_set_fs( &channel_state[ n ], fs_kHz_dec, decControl->API_sampleRate );
228
224k
        }
229
172k
    }
230
231
194k
    if( decControl->nChannelsAPI == 2 && decControl->nChannelsInternal == 2 && ( psDec->nChannelsAPI == 1 || psDec->nChannelsInternal == 1 ) ) {
232
2.39k
        silk_memset( psDec->sStereo.pred_prev_Q13, 0, sizeof( psDec->sStereo.pred_prev_Q13 ) );
233
2.39k
        silk_memset( psDec->sStereo.sSide, 0, sizeof( psDec->sStereo.sSide ) );
234
2.39k
        silk_memcpy( &channel_state[ 1 ].resampler_state, &channel_state[ 0 ].resampler_state, sizeof( silk_resampler_state_struct ) );
235
2.39k
    }
236
194k
    psDec->nChannelsAPI      = decControl->nChannelsAPI;
237
194k
    psDec->nChannelsInternal = decControl->nChannelsInternal;
238
239
194k
    if( decControl->API_sampleRate > (opus_int32)MAX_API_FS_KHZ * 1000 || decControl->API_sampleRate < 8000 ) {
240
0
        ret = SILK_DEC_INVALID_SAMPLING_FREQUENCY;
241
0
        RESTORE_STACK;
242
0
        return( ret );
243
0
    }
244
245
194k
    if( lostFlag != FLAG_PACKET_LOST && channel_state[ 0 ].nFramesDecoded == 0 ) {
246
        /* First decoder call for this payload */
247
        /* Decode VAD flags and LBRR flag */
248
170k
        for( n = 0; n < decControl->nChannelsInternal; n++ ) {
249
237k
            for( i = 0; i < channel_state[ n ].nFramesPerPacket; i++ ) {
250
138k
                channel_state[ n ].VAD_flags[ i ] = ec_dec_bit_logp(psRangeDec, 1);
251
138k
            }
252
99.2k
            channel_state[ n ].LBRR_flag = ec_dec_bit_logp(psRangeDec, 1);
253
99.2k
        }
254
        /* Decode LBRR flags */
255
170k
        for( n = 0; n < decControl->nChannelsInternal; n++ ) {
256
99.2k
            silk_memset( channel_state[ n ].LBRR_flags, 0, sizeof( channel_state[ n ].LBRR_flags ) );
257
99.2k
            if( channel_state[ n ].LBRR_flag ) {
258
38.7k
                if( channel_state[ n ].nFramesPerPacket == 1 ) {
259
26.0k
                    channel_state[ n ].LBRR_flags[ 0 ] = 1;
260
26.0k
                } else {
261
12.7k
                    LBRR_symbol = ec_dec_icdf( psRangeDec, silk_LBRR_flags_iCDF_ptr[ channel_state[ n ].nFramesPerPacket - 2 ], 8 ) + 1;
262
44.9k
                    for( i = 0; i < channel_state[ n ].nFramesPerPacket; i++ ) {
263
32.1k
                        channel_state[ n ].LBRR_flags[ i ] = silk_RSHIFT( LBRR_symbol, i ) & 1;
264
32.1k
                    }
265
12.7k
                }
266
38.7k
            }
267
99.2k
        }
268
269
71.2k
        if( lostFlag == FLAG_DECODE_NORMAL ) {
270
            /* Regular decoding: skip all LBRR data */
271
164k
            for( i = 0; i < channel_state[ 0 ].nFramesPerPacket; i++ ) {
272
231k
                for( n = 0; n < decControl->nChannelsInternal; n++ ) {
273
138k
                    if( channel_state[ n ].LBRR_flags[ i ] ) {
274
46.4k
                        opus_int16 pulses[ MAX_FRAME_LENGTH ];
275
46.4k
                        opus_int condCoding;
276
277
46.4k
                        if( decControl->nChannelsInternal == 2 && n == 0 ) {
278
13.3k
                            silk_stereo_decode_pred( psRangeDec, MS_pred_Q13 );
279
13.3k
                            if( channel_state[ 1 ].LBRR_flags[ i ] == 0 ) {
280
2.97k
                                silk_stereo_decode_mid_only( psRangeDec, &decode_only_middle );
281
2.97k
                            }
282
13.3k
                        }
283
                        /* Use conditional coding if previous frame available */
284
46.4k
                        if( i > 0 && channel_state[ n ].LBRR_flags[ i - 1 ] ) {
285
7.63k
                            condCoding = CODE_CONDITIONALLY;
286
38.8k
                        } else {
287
38.8k
                            condCoding = CODE_INDEPENDENTLY;
288
38.8k
                        }
289
46.4k
                        silk_decode_indices( &channel_state[ n ], psRangeDec, i, 1, condCoding );
290
46.4k
                        silk_decode_pulses( psRangeDec, pulses, channel_state[ n ].indices.signalType,
291
46.4k
                            channel_state[ n ].indices.quantOffsetType, channel_state[ n ].frame_length );
292
46.4k
                    }
293
138k
                }
294
93.4k
            }
295
71.2k
        }
296
71.2k
    }
297
298
    /* Get MS predictor index */
299
194k
    if( decControl->nChannelsInternal == 2 ) {
300
68.9k
        if(   lostFlag == FLAG_DECODE_NORMAL ||
301
24.4k
            ( lostFlag == FLAG_DECODE_LBRR && channel_state[ 0 ].LBRR_flags[ channel_state[ 0 ].nFramesDecoded ] == 1 ) )
302
44.5k
        {
303
44.5k
            silk_stereo_decode_pred( psRangeDec, MS_pred_Q13 );
304
            /* For LBRR data, decode mid-only flag only if side-channel's LBRR flag is false */
305
44.5k
            if( ( lostFlag == FLAG_DECODE_NORMAL && channel_state[ 1 ].VAD_flags[ channel_state[ 0 ].nFramesDecoded ] == 0 ) ||
306
17.5k
                ( lostFlag == FLAG_DECODE_LBRR && channel_state[ 1 ].LBRR_flags[ channel_state[ 0 ].nFramesDecoded ] == 0 ) )
307
26.9k
            {
308
26.9k
                silk_stereo_decode_mid_only( psRangeDec, &decode_only_middle );
309
26.9k
            } else {
310
17.5k
                decode_only_middle = 0;
311
17.5k
            }
312
44.5k
        } else {
313
73.3k
            for( n = 0; n < 2; n++ ) {
314
48.9k
                MS_pred_Q13[ n ] = psDec->sStereo.pred_prev_Q13[ n ];
315
48.9k
            }
316
24.4k
        }
317
68.9k
    }
318
319
    /* Reset side channel decoder prediction memory for first frame with side coding */
320
194k
    if( decControl->nChannelsInternal == 2 && decode_only_middle == 0 && psDec->prev_decode_only_middle == 1 ) {
321
5.57k
        silk_memset( psDec->channel_state[ 1 ].outBuf, 0, sizeof(psDec->channel_state[ 1 ].outBuf) );
322
5.57k
        silk_memset( psDec->channel_state[ 1 ].sLPC_Q14_buf, 0, sizeof(psDec->channel_state[ 1 ].sLPC_Q14_buf) );
323
5.57k
        psDec->channel_state[ 1 ].lagPrev        = 100;
324
5.57k
        psDec->channel_state[ 1 ].LastGainIndex  = 10;
325
5.57k
        psDec->channel_state[ 1 ].prevSignalType = TYPE_NO_VOICE_ACTIVITY;
326
5.57k
        psDec->channel_state[ 1 ].first_frame_after_reset = 1;
327
5.57k
    }
328
329
    /* Check if the temp buffer fits into the output PCM buffer. If it fits,
330
       we can delay allocating the temp buffer until after the SILK peak stack
331
       usage. We need to use a < and not a <= because of the two extra samples. */
332
194k
    ALLOC( samplesOut1_tmp_storage1, decControl->nChannelsInternal*(channel_state[ 0 ].frame_length + 2 ),
333
194k
           opus_int16 );
334
194k
    samplesOut1_tmp[ 0 ] = samplesOut1_tmp_storage1;
335
194k
    samplesOut1_tmp[ 1 ] = samplesOut1_tmp_storage1 + channel_state[ 0 ].frame_length + 2;
336
337
194k
    if( lostFlag == FLAG_DECODE_NORMAL ) {
338
93.4k
        has_side = !decode_only_middle;
339
100k
    } else {
340
100k
        has_side = !psDec->prev_decode_only_middle
341
2.34k
              || (decControl->nChannelsInternal == 2 && lostFlag == FLAG_DECODE_LBRR && channel_state[1].LBRR_flags[ channel_state[1].nFramesDecoded ] == 1 );
342
100k
    }
343
194k
    channel_state[ 0 ].sPLC.enable_deep_plc = decControl->enable_deep_plc;
344
    /* Call decoder for one frame */
345
457k
    for( n = 0; n < decControl->nChannelsInternal; n++ ) {
346
263k
        if( n == 0 || has_side ) {
347
256k
            opus_int FrameIndex;
348
256k
            opus_int condCoding;
349
350
256k
            FrameIndex = channel_state[ 0 ].nFramesDecoded - n;
351
            /* Use independent coding if no previous frame available */
352
256k
            if( FrameIndex <= 0 ) {
353
219k
                condCoding = CODE_INDEPENDENTLY;
354
219k
            } else if( lostFlag == FLAG_DECODE_LBRR ) {
355
0
                condCoding = channel_state[ n ].LBRR_flags[ FrameIndex - 1 ] ? CODE_CONDITIONALLY : CODE_INDEPENDENTLY;
356
37.2k
            } else if( n > 0 && psDec->prev_decode_only_middle ) {
357
                /* If we skipped a side frame in this packet, we don't
358
                   need LTP scaling; the LTP state is well-defined. */
359
2.12k
                condCoding = CODE_INDEPENDENTLY_NO_LTP_SCALING;
360
35.1k
            } else {
361
35.1k
                condCoding = CODE_CONDITIONALLY;
362
35.1k
            }
363
#ifdef ENABLE_OSCE
364
            if ( channel_state[n].osce.method != decControl->osce_method ) {
365
                osce_reset( &channel_state[n].osce, decControl->osce_method );
366
            }
367
#endif
368
256k
            ret += silk_decode_frame( &channel_state[ n ], psRangeDec, &samplesOut1_tmp[ n ][ 2 ], &nSamplesOutDec, lostFlag, condCoding,
369
#ifdef ENABLE_DEEP_PLC
370
                n == 0 ? lpcnet : NULL,
371
#endif
372
#ifdef ENABLE_OSCE
373
                &psDec->osce_model,
374
#endif
375
256k
                arch);
376
256k
        } else {
377
6.81k
            silk_memset( &samplesOut1_tmp[ n ][ 2 ], 0, nSamplesOutDec * sizeof( opus_int16 ) );
378
6.81k
        }
379
263k
        channel_state[ n ].nFramesDecoded++;
380
263k
    }
381
382
194k
    if( decControl->nChannelsAPI == 2 && decControl->nChannelsInternal == 2 ) {
383
        /* Convert Mid/Side to Left/Right */
384
64.5k
        silk_stereo_MS_to_LR( &psDec->sStereo, samplesOut1_tmp[ 0 ], samplesOut1_tmp[ 1 ], MS_pred_Q13, channel_state[ 0 ].fs_kHz, nSamplesOutDec );
385
129k
    } else {
386
        /* Buffering */
387
129k
        silk_memcpy( samplesOut1_tmp[ 0 ], psDec->sStereo.sMid, 2 * sizeof( opus_int16 ) );
388
129k
        silk_memcpy( psDec->sStereo.sMid, &samplesOut1_tmp[ 0 ][ nSamplesOutDec ], 2 * sizeof( opus_int16 ) );
389
129k
    }
390
391
    /* Number of output samples */
392
194k
    *nSamplesOut = silk_DIV32( nSamplesOutDec * decControl->API_sampleRate, silk_SMULBB( channel_state[ 0 ].fs_kHz, 1000 ) );
393
394
    /* Set up pointers to temp buffers */
395
194k
    ALLOC( samplesOut2_tmp, *nSamplesOut, opus_int16 );
396
194k
    resample_out_ptr = samplesOut2_tmp;
397
398
#ifdef ENABLE_OSCE_BWE
399
    ALLOC(resamp_buffer, 3 * MAX_FRAME_LENGTH, opus_int16);
400
#endif
401
402
452k
    for( n = 0; n < silk_min( decControl->nChannelsAPI, decControl->nChannelsInternal ); n++ ) {
403
404
#ifdef ENABLE_OSCE_BWE
405
        /* Resample or extend decoded signal to API_sampleRate */
406
        if (decControl->osce_extended_mode == OSCE_MODE_SILK_BBWE) {
407
            silk_assert(decControl->API_sampleRate == 48000);
408
409
            if (decControl->prev_osce_extended_mode != OSCE_MODE_SILK_BBWE) {
410
                /* Reset the BWE state */
411
                osce_bwe_reset( &channel_state[ n ].osce_bwe );
412
            }
413
414
            osce_bwe(&psDec->osce_model, &channel_state[ n ].osce_bwe,
415
                resample_out_ptr, &samplesOut1_tmp[ n ][ 1 ], nSamplesOutDec, arch);
416
417
            if (decControl->prev_osce_extended_mode == OSCE_MODE_SILK_ONLY ||
418
                decControl->prev_osce_extended_mode == OSCE_MODE_HYBRID) {
419
                    /* cross-fade with upsampled signal */
420
                    silk_resampler( &channel_state[ n ].resampler_state, resamp_buffer, &samplesOut1_tmp[ n ][ 1 ], nSamplesOutDec );
421
                    osce_bwe_cross_fade_10ms(resample_out_ptr, resamp_buffer, 480);
422
            }
423
        } else {
424
            ret += silk_resampler( &channel_state[ n ].resampler_state, resample_out_ptr, &samplesOut1_tmp[ n ][ 1 ], nSamplesOutDec );
425
            if (decControl->prev_osce_extended_mode == OSCE_MODE_SILK_BBWE && decControl->internalSampleRate == 16000) {
426
                /* fade out if internal sample rate did not change */
427
                osce_bwe(&psDec->osce_model, &channel_state[ n ].osce_bwe,
428
                    resamp_buffer, &samplesOut1_tmp[ n ][ 1 ], nSamplesOutDec, arch);
429
                /* cross-fade with upsampled signal */
430
                osce_bwe_cross_fade_10ms(resample_out_ptr, resamp_buffer, 480);
431
            }
432
        }
433
#else
434
        /* Resample decoded signal to API_sampleRate */
435
258k
        ret += silk_resampler( &channel_state[ n ].resampler_state, resample_out_ptr, &samplesOut1_tmp[ n ][ 1 ], nSamplesOutDec );
436
258k
#endif
437
        /* Interleave if stereo output and stereo stream */
438
258k
        if( decControl->nChannelsAPI == 2 ) {
439
168M
            for( i = 0; i < *nSamplesOut; i++ ) {
440
168M
                samplesOut[ n + 2 * i ] = INT16TORES(resample_out_ptr[ i ]);
441
168M
            }
442
247k
        } else {
443
7.73M
            for( i = 0; i < *nSamplesOut; i++ ) {
444
7.72M
                samplesOut[ i ] = INT16TORES(resample_out_ptr[ i ]);
445
7.72M
            }
446
10.8k
        }
447
258k
    }
448
449
#ifdef ENABLE_OSCE_BWE
450
    decControl->prev_osce_extended_mode = decControl->osce_extended_mode;
451
#endif
452
453
    /* Create two channel output from mono stream */
454
194k
    if( decControl->nChannelsAPI == 2 && decControl->nChannelsInternal == 1 ) {
455
118k
        if ( stereo_to_mono ){
456
            /* Resample right channel for newly collapsed stereo just in case
457
               we weren't doing collapsing when switching to mono */
458
1.17k
            ret += silk_resampler( &channel_state[ 1 ].resampler_state, resample_out_ptr, &samplesOut1_tmp[ 0 ][ 1 ], nSamplesOutDec );
459
460
725k
            for( i = 0; i < *nSamplesOut; i++ ) {
461
724k
                samplesOut[ 1 + 2 * i ] = INT16TORES(resample_out_ptr[ i ]);
462
724k
            }
463
117k
        } else {
464
72.0M
            for( i = 0; i < *nSamplesOut; i++ ) {
465
71.9M
                samplesOut[ 1 + 2 * i ] = samplesOut[ 0 + 2 * i ];
466
71.9M
            }
467
117k
        }
468
118k
    }
469
470
    /* Export pitch lag, measured at 48 kHz sampling rate */
471
194k
    if( channel_state[ 0 ].prevSignalType == TYPE_VOICED ) {
472
30.0k
        int mult_tab[ 3 ] = { 6, 4, 3 };
473
30.0k
        decControl->prevPitchLag = channel_state[ 0 ].lagPrev * mult_tab[ ( channel_state[ 0 ].fs_kHz - 8 ) >> 2 ];
474
164k
    } else {
475
164k
        decControl->prevPitchLag = 0;
476
164k
    }
477
478
194k
    if( lostFlag == FLAG_PACKET_LOST ) {
479
       /* On packet loss, remove the gain clamping to prevent having the energy "bounce back"
480
          if we lose packets when the energy is going down */
481
225k
       for ( i = 0; i < psDec->nChannelsInternal; i++ )
482
125k
          psDec->channel_state[ i ].LastGainIndex = 10;
483
100k
    } else {
484
93.4k
       psDec->prev_decode_only_middle = decode_only_middle;
485
93.4k
    }
486
194k
    RESTORE_STACK;
487
194k
    return ret;
488
194k
}
489
490
#if 0
491
/* Getting table of contents for a packet */
492
opus_int silk_get_TOC(
493
    const opus_uint8                *payload,           /* I    Payload data                                */
494
    const opus_int                  nBytesIn,           /* I    Number of input bytes                       */
495
    const opus_int                  nFramesPerPayload,  /* I    Number of SILK frames per payload           */
496
    silk_TOC_struct                 *Silk_TOC           /* O    Type of content                             */
497
)
498
{
499
    opus_int i, flags, ret = SILK_NO_ERROR;
500
501
    if( nBytesIn < 1 ) {
502
        return -1;
503
    }
504
    if( nFramesPerPayload < 0 || nFramesPerPayload > 3 ) {
505
        return -1;
506
    }
507
508
    silk_memset( Silk_TOC, 0, sizeof( *Silk_TOC ) );
509
510
    /* For stereo, extract the flags for the mid channel */
511
    flags = silk_RSHIFT( payload[ 0 ], 7 - nFramesPerPayload ) & ( silk_LSHIFT( 1, nFramesPerPayload + 1 ) - 1 );
512
513
    Silk_TOC->inbandFECFlag = flags & 1;
514
    for( i = nFramesPerPayload - 1; i >= 0 ; i-- ) {
515
        flags = silk_RSHIFT( flags, 1 );
516
        Silk_TOC->VADFlags[ i ] = flags & 1;
517
        Silk_TOC->VADFlag |= flags & 1;
518
    }
519
520
    return ret;
521
}
522
#endif