Coverage Report

Created: 2025-11-24 07:40

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
403k
{
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
403k
    (void) decState;
74
403k
    (void) data;
75
403k
    (void) len;
76
403k
    return SILK_NO_ERROR;
77
403k
#endif
78
403k
}
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.22M
{
84
1.22M
    opus_int ret = SILK_NO_ERROR;
85
86
1.22M
    *decSizeBytes = sizeof( silk_decoder );
87
88
1.22M
    return ret;
89
1.22M
}
90
91
/* Reset decoder state */
92
opus_int silk_ResetDecoder(                              /* O    Returns error code                              */
93
    void                            *decState           /* I/O  State                                           */
94
)
95
408
{
96
408
    opus_int n, ret = SILK_NO_ERROR;
97
408
    silk_decoder_state *channel_state = ((silk_decoder *)decState)->channel_state;
98
99
1.22k
    for( n = 0; n < DECODER_NUM_CHANNELS; n++ ) {
100
816
        ret  = silk_reset_decoder( &channel_state[ n ] );
101
816
    }
102
408
    silk_memset(&((silk_decoder *)decState)->sStereo, 0, sizeof(((silk_decoder *)decState)->sStereo));
103
    /* Not strictly needed, but it's cleaner that way */
104
408
    ((silk_decoder *)decState)->prev_decode_only_middle = 0;
105
106
408
    return ret;
107
408
}
108
109
110
opus_int silk_InitDecoder(                              /* O    Returns error code                              */
111
    void                            *decState           /* I/O  State                                           */
112
)
113
403k
{
114
403k
    opus_int n, ret = SILK_NO_ERROR;
115
403k
    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
403k
#ifndef USE_WEIGHTS_FILE
120
    /* load osce models */
121
403k
    silk_LoadOSCEModels(decState, NULL, 0);
122
403k
#endif
123
124
1.20M
    for( n = 0; n < DECODER_NUM_CHANNELS; n++ ) {
125
806k
        ret  = silk_init_decoder( &channel_state[ n ] );
126
806k
    }
127
403k
    silk_memset(&((silk_decoder *)decState)->sStereo, 0, sizeof(((silk_decoder *)decState)->sStereo));
128
    /* Not strictly needed, but it's cleaner that way */
129
403k
    ((silk_decoder *)decState)->prev_decode_only_middle = 0;
130
131
403k
    return ret;
132
403k
}
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
796k
{
149
796k
    opus_int   i, n, decode_only_middle = 0, ret = SILK_NO_ERROR;
150
796k
    opus_int32 nSamplesOutDec, LBRR_symbol;
151
796k
    opus_int16 *samplesOut1_tmp[ 2 ];
152
796k
    VARDECL( opus_int16, samplesOut1_tmp_storage1 );
153
796k
    VARDECL( opus_int16, samplesOut2_tmp );
154
796k
    opus_int32 MS_pred_Q13[ 2 ] = { 0 };
155
796k
    opus_int16 *resample_out_ptr;
156
796k
    silk_decoder *psDec = ( silk_decoder * )decState;
157
796k
    silk_decoder_state *channel_state = psDec->channel_state;
158
796k
    opus_int has_side;
159
796k
    opus_int stereo_to_mono;
160
#ifdef ENABLE_OSCE_BWE
161
    ALLOC(resamp_buffer, 3 * MAX_FRAME_LENGTH, opus_int16);
162
#endif
163
796k
    SAVE_STACK;
164
165
796k
    celt_assert( decControl->nChannelsInternal == 1 || decControl->nChannelsInternal == 2 );
166
167
    /**********************************/
168
    /* Test if first frame in payload */
169
    /**********************************/
170
796k
    if( newPacketFlag ) {
171
1.37M
        for( n = 0; n < decControl->nChannelsInternal; n++ ) {
172
856k
            channel_state[ n ].nFramesDecoded = 0;  /* Used to count frames in packet */
173
856k
        }
174
519k
    }
175
176
    /* If Mono -> Stereo transition in bitstream: init state of second channel */
177
796k
    if( decControl->nChannelsInternal > psDec->nChannelsInternal ) {
178
270k
        ret += silk_init_decoder( &channel_state[ 1 ] );
179
270k
    }
180
181
796k
    stereo_to_mono = decControl->nChannelsInternal == 1 && psDec->nChannelsInternal == 2 &&
182
0
                     ( decControl->internalSampleRate == 1000*channel_state[ 0 ].fs_kHz );
183
184
796k
    if( channel_state[ 0 ].nFramesDecoded == 0 ) {
185
1.37M
        for( n = 0; n < decControl->nChannelsInternal; n++ ) {
186
856k
            opus_int fs_kHz_dec;
187
856k
            if( decControl->payloadSize_ms == 0 ) {
188
                /* Assuming packet loss, use 10 ms */
189
0
                channel_state[ n ].nFramesPerPacket = 1;
190
0
                channel_state[ n ].nb_subfr = 2;
191
856k
            } else if( decControl->payloadSize_ms == 10 ) {
192
419k
                channel_state[ n ].nFramesPerPacket = 1;
193
419k
                channel_state[ n ].nb_subfr = 2;
194
437k
            } else if( decControl->payloadSize_ms == 20 ) {
195
176k
                channel_state[ n ].nFramesPerPacket = 1;
196
176k
                channel_state[ n ].nb_subfr = 4;
197
260k
            } else if( decControl->payloadSize_ms == 40 ) {
198
32.4k
                channel_state[ n ].nFramesPerPacket = 2;
199
32.4k
                channel_state[ n ].nb_subfr = 4;
200
228k
            } else if( decControl->payloadSize_ms == 60 ) {
201
228k
                channel_state[ n ].nFramesPerPacket = 3;
202
228k
                channel_state[ n ].nb_subfr = 4;
203
228k
            } else {
204
0
                celt_assert( 0 );
205
0
                RESTORE_STACK;
206
0
                return SILK_DEC_INVALID_FRAME_SIZE;
207
0
            }
208
856k
            fs_kHz_dec = ( decControl->internalSampleRate >> 10 ) + 1;
209
856k
            if( fs_kHz_dec != 8 && fs_kHz_dec != 12 && fs_kHz_dec != 16 ) {
210
0
                celt_assert( 0 );
211
0
                RESTORE_STACK;
212
0
                return SILK_DEC_INVALID_SAMPLING_FREQUENCY;
213
0
            }
214
856k
            ret += silk_decoder_set_fs( &channel_state[ n ], fs_kHz_dec, decControl->API_sampleRate );
215
856k
        }
216
519k
    }
217
218
796k
    if( decControl->nChannelsAPI == 2 && decControl->nChannelsInternal == 2 && ( psDec->nChannelsAPI == 1 || psDec->nChannelsInternal == 1 ) ) {
219
0
        silk_memset( psDec->sStereo.pred_prev_Q13, 0, sizeof( psDec->sStereo.pred_prev_Q13 ) );
220
0
        silk_memset( psDec->sStereo.sSide, 0, sizeof( psDec->sStereo.sSide ) );
221
0
        silk_memcpy( &channel_state[ 1 ].resampler_state, &channel_state[ 0 ].resampler_state, sizeof( silk_resampler_state_struct ) );
222
0
    }
223
796k
    psDec->nChannelsAPI      = decControl->nChannelsAPI;
224
796k
    psDec->nChannelsInternal = decControl->nChannelsInternal;
225
226
796k
    if( decControl->API_sampleRate > (opus_int32)MAX_API_FS_KHZ * 1000 || decControl->API_sampleRate < 8000 ) {
227
0
        ret = SILK_DEC_INVALID_SAMPLING_FREQUENCY;
228
0
        RESTORE_STACK;
229
0
        return( ret );
230
0
    }
231
232
796k
    if( lostFlag != FLAG_PACKET_LOST && channel_state[ 0 ].nFramesDecoded == 0 ) {
233
        /* First decoder call for this payload */
234
        /* Decode VAD flags and LBRR flag */
235
989k
        for( n = 0; n < decControl->nChannelsInternal; n++ ) {
236
1.70M
            for( i = 0; i < channel_state[ n ].nFramesPerPacket; i++ ) {
237
1.09M
                channel_state[ n ].VAD_flags[ i ] = ec_dec_bit_logp(psRangeDec, 1);
238
1.09M
            }
239
607k
            channel_state[ n ].LBRR_flag = ec_dec_bit_logp(psRangeDec, 1);
240
607k
        }
241
        /* Decode LBRR flags */
242
989k
        for( n = 0; n < decControl->nChannelsInternal; n++ ) {
243
607k
            silk_memset( channel_state[ n ].LBRR_flags, 0, sizeof( channel_state[ n ].LBRR_flags ) );
244
607k
            if( channel_state[ n ].LBRR_flag ) {
245
262k
                if( channel_state[ n ].nFramesPerPacket == 1 ) {
246
99.5k
                    channel_state[ n ].LBRR_flags[ 0 ] = 1;
247
163k
                } else {
248
163k
                    LBRR_symbol = ec_dec_icdf( psRangeDec, silk_LBRR_flags_iCDF_ptr[ channel_state[ n ].nFramesPerPacket - 2 ], 8 ) + 1;
249
636k
                    for( i = 0; i < channel_state[ n ].nFramesPerPacket; i++ ) {
250
472k
                        channel_state[ n ].LBRR_flags[ i ] = silk_RSHIFT( LBRR_symbol, i ) & 1;
251
472k
                    }
252
163k
                }
253
262k
            }
254
607k
        }
255
256
381k
        if( lostFlag == FLAG_DECODE_NORMAL ) {
257
            /* Regular decoding: skip all LBRR data */
258
807k
            for( i = 0; i < channel_state[ 0 ].nFramesPerPacket; i++ ) {
259
1.33M
                for( n = 0; n < decControl->nChannelsInternal; n++ ) {
260
835k
                    if( channel_state[ n ].LBRR_flags[ i ] ) {
261
288k
                        opus_int16 pulses[ MAX_FRAME_LENGTH ];
262
288k
                        opus_int condCoding;
263
264
288k
                        if( decControl->nChannelsInternal == 2 && n == 0 ) {
265
124k
                            silk_stereo_decode_pred( psRangeDec, MS_pred_Q13 );
266
124k
                            if( channel_state[ 1 ].LBRR_flags[ i ] == 0 ) {
267
59.1k
                                silk_stereo_decode_mid_only( psRangeDec, &decode_only_middle );
268
59.1k
                            }
269
124k
                        }
270
                        /* Use conditional coding if previous frame available */
271
288k
                        if( i > 0 && channel_state[ n ].LBRR_flags[ i - 1 ] ) {
272
93.5k
                            condCoding = CODE_CONDITIONALLY;
273
194k
                        } else {
274
194k
                            condCoding = CODE_INDEPENDENTLY;
275
194k
                        }
276
288k
                        silk_decode_indices( &channel_state[ n ], psRangeDec, i, 1, condCoding );
277
288k
                        silk_decode_pulses( psRangeDec, pulses, channel_state[ n ].indices.signalType,
278
288k
                            channel_state[ n ].indices.quantOffsetType, channel_state[ n ].frame_length );
279
288k
                    }
280
835k
                }
281
499k
            }
282
308k
        }
283
381k
    }
284
285
    /* Get MS predictor index */
286
796k
    if( decControl->nChannelsInternal == 2 ) {
287
549k
        if(   lostFlag == FLAG_DECODE_NORMAL ||
288
213k
            ( lostFlag == FLAG_DECODE_LBRR && channel_state[ 0 ].LBRR_flags[ channel_state[ 0 ].nFramesDecoded ] == 1 ) )
289
378k
        {
290
378k
            silk_stereo_decode_pred( psRangeDec, MS_pred_Q13 );
291
            /* For LBRR data, decode mid-only flag only if side-channel's LBRR flag is false */
292
378k
            if( ( lostFlag == FLAG_DECODE_NORMAL && channel_state[ 1 ].VAD_flags[ channel_state[ 0 ].nFramesDecoded ] == 0 ) ||
293
214k
                ( lostFlag == FLAG_DECODE_LBRR && channel_state[ 1 ].LBRR_flags[ channel_state[ 0 ].nFramesDecoded ] == 0 ) )
294
189k
            {
295
189k
                silk_stereo_decode_mid_only( psRangeDec, &decode_only_middle );
296
189k
            } else {
297
189k
                decode_only_middle = 0;
298
189k
            }
299
378k
        } else {
300
512k
            for( n = 0; n < 2; n++ ) {
301
341k
                MS_pred_Q13[ n ] = psDec->sStereo.pred_prev_Q13[ n ];
302
341k
            }
303
170k
        }
304
549k
    }
305
306
    /* Reset side channel decoder prediction memory for first frame with side coding */
307
796k
    if( decControl->nChannelsInternal == 2 && decode_only_middle == 0 && psDec->prev_decode_only_middle == 1 ) {
308
40.2k
        silk_memset( psDec->channel_state[ 1 ].outBuf, 0, sizeof(psDec->channel_state[ 1 ].outBuf) );
309
40.2k
        silk_memset( psDec->channel_state[ 1 ].sLPC_Q14_buf, 0, sizeof(psDec->channel_state[ 1 ].sLPC_Q14_buf) );
310
40.2k
        psDec->channel_state[ 1 ].lagPrev        = 100;
311
40.2k
        psDec->channel_state[ 1 ].LastGainIndex  = 10;
312
40.2k
        psDec->channel_state[ 1 ].prevSignalType = TYPE_NO_VOICE_ACTIVITY;
313
40.2k
        psDec->channel_state[ 1 ].first_frame_after_reset = 1;
314
40.2k
    }
315
316
    /* Check if the temp buffer fits into the output PCM buffer. If it fits,
317
       we can delay allocating the temp buffer until after the SILK peak stack
318
       usage. We need to use a < and not a <= because of the two extra samples. */
319
796k
    ALLOC( samplesOut1_tmp_storage1, decControl->nChannelsInternal*(channel_state[ 0 ].frame_length + 2 ),
320
796k
           opus_int16 );
321
796k
    samplesOut1_tmp[ 0 ] = samplesOut1_tmp_storage1;
322
796k
    samplesOut1_tmp[ 1 ] = samplesOut1_tmp_storage1 + channel_state[ 0 ].frame_length + 2;
323
324
796k
    if( lostFlag == FLAG_DECODE_NORMAL ) {
325
499k
        has_side = !decode_only_middle;
326
499k
    } else {
327
297k
        has_side = !psDec->prev_decode_only_middle
328
23.4k
              || (decControl->nChannelsInternal == 2 && lostFlag == FLAG_DECODE_LBRR && channel_state[1].LBRR_flags[ channel_state[1].nFramesDecoded ] == 1 );
329
297k
    }
330
796k
    channel_state[ 0 ].sPLC.enable_deep_plc = decControl->enable_deep_plc;
331
    /* Call decoder for one frame */
332
2.14M
    for( n = 0; n < decControl->nChannelsInternal; n++ ) {
333
1.34M
        if( n == 0 || has_side ) {
334
1.29M
            opus_int FrameIndex;
335
1.29M
            opus_int condCoding;
336
337
1.29M
            FrameIndex = channel_state[ 0 ].nFramesDecoded - n;
338
            /* Use independent coding if no previous frame available */
339
1.29M
            if( FrameIndex <= 0 ) {
340
829k
                condCoding = CODE_INDEPENDENTLY;
341
829k
            } else if( lostFlag == FLAG_DECODE_LBRR ) {
342
129k
                condCoding = channel_state[ n ].LBRR_flags[ FrameIndex - 1 ] ? CODE_CONDITIONALLY : CODE_INDEPENDENTLY;
343
334k
            } else if( n > 0 && psDec->prev_decode_only_middle ) {
344
                /* If we skipped a side frame in this packet, we don't
345
                   need LTP scaling; the LTP state is well-defined. */
346
11.0k
                condCoding = CODE_INDEPENDENTLY_NO_LTP_SCALING;
347
323k
            } else {
348
323k
                condCoding = CODE_CONDITIONALLY;
349
323k
            }
350
#ifdef ENABLE_OSCE
351
            if ( channel_state[n].osce.method != decControl->osce_method ) {
352
                osce_reset( &channel_state[n].osce, decControl->osce_method );
353
            }
354
#endif
355
1.29M
            ret += silk_decode_frame( &channel_state[ n ], psRangeDec, &samplesOut1_tmp[ n ][ 2 ], &nSamplesOutDec, lostFlag, condCoding,
356
#ifdef ENABLE_DEEP_PLC
357
                n == 0 ? lpcnet : NULL,
358
#endif
359
#ifdef ENABLE_OSCE
360
                &psDec->osce_model,
361
#endif
362
1.29M
                arch);
363
1.29M
        } else {
364
52.6k
            silk_memset( &samplesOut1_tmp[ n ][ 2 ], 0, nSamplesOutDec * sizeof( opus_int16 ) );
365
52.6k
        }
366
1.34M
        channel_state[ n ].nFramesDecoded++;
367
1.34M
    }
368
369
796k
    if( decControl->nChannelsAPI == 2 && decControl->nChannelsInternal == 2 ) {
370
        /* Convert Mid/Side to Left/Right */
371
205k
        silk_stereo_MS_to_LR( &psDec->sStereo, samplesOut1_tmp[ 0 ], samplesOut1_tmp[ 1 ], MS_pred_Q13, channel_state[ 0 ].fs_kHz, nSamplesOutDec );
372
591k
    } else {
373
        /* Buffering */
374
591k
        silk_memcpy( samplesOut1_tmp[ 0 ], psDec->sStereo.sMid, 2 * sizeof( opus_int16 ) );
375
591k
        silk_memcpy( psDec->sStereo.sMid, &samplesOut1_tmp[ 0 ][ nSamplesOutDec ], 2 * sizeof( opus_int16 ) );
376
591k
    }
377
378
    /* Number of output samples */
379
796k
    *nSamplesOut = silk_DIV32( nSamplesOutDec * decControl->API_sampleRate, silk_SMULBB( channel_state[ 0 ].fs_kHz, 1000 ) );
380
381
    /* Set up pointers to temp buffers */
382
796k
    ALLOC( samplesOut2_tmp, *nSamplesOut, opus_int16 );
383
796k
    resample_out_ptr = samplesOut2_tmp;
384
385
1.79M
    for( n = 0; n < silk_min( decControl->nChannelsAPI, decControl->nChannelsInternal ); n++ ) {
386
387
#ifdef ENABLE_OSCE_BWE
388
        /* Resample or extend decoded signal to API_sampleRate */
389
        if (decControl->osce_extended_mode == OSCE_MODE_SILK_BBWE) {
390
            silk_assert(decControl->API_sampleRate == 48000);
391
392
            if (decControl->prev_osce_extended_mode != OSCE_MODE_SILK_BBWE) {
393
                /* Reset the BWE state */
394
                osce_bwe_reset( &channel_state[ n ].osce_bwe );
395
            }
396
397
            osce_bwe(&psDec->osce_model, &channel_state[ n ].osce_bwe,
398
                resample_out_ptr, &samplesOut1_tmp[ n ][ 1 ], nSamplesOutDec, arch);
399
400
            if (decControl->prev_osce_extended_mode == OSCE_MODE_SILK_ONLY ||
401
                decControl->prev_osce_extended_mode == OSCE_MODE_HYBRID) {
402
                    /* cross-fade with upsampled signal */
403
                    silk_resampler( &channel_state[ n ].resampler_state, resamp_buffer, &samplesOut1_tmp[ n ][ 1 ], nSamplesOutDec );
404
                    osce_bwe_cross_fade_10ms(resample_out_ptr, resamp_buffer, 480);
405
            }
406
        } else {
407
            ret += silk_resampler( &channel_state[ n ].resampler_state, resample_out_ptr, &samplesOut1_tmp[ n ][ 1 ], nSamplesOutDec );
408
            if (decControl->prev_osce_extended_mode == OSCE_MODE_SILK_BBWE) {
409
                osce_bwe(&psDec->osce_model, &channel_state[ n ].osce_bwe,
410
                    resamp_buffer, &samplesOut1_tmp[ n ][ 1 ], nSamplesOutDec, arch);
411
                /* cross-fade with upsampled signal */
412
                osce_bwe_cross_fade_10ms(resample_out_ptr, resamp_buffer, 480);
413
            }
414
        }
415
#else
416
        /* Resample decoded signal to API_sampleRate */
417
1.00M
        ret += silk_resampler( &channel_state[ n ].resampler_state, resample_out_ptr, &samplesOut1_tmp[ n ][ 1 ], nSamplesOutDec );
418
1.00M
#endif
419
        /* Interleave if stereo output and stereo stream */
420
1.00M
        if( decControl->nChannelsAPI == 2 ) {
421
217M
            for( i = 0; i < *nSamplesOut; i++ ) {
422
217M
                samplesOut[ n + 2 * i ] = INT16TORES(resample_out_ptr[ i ]);
423
217M
            }
424
527k
        } else {
425
184M
            for( i = 0; i < *nSamplesOut; i++ ) {
426
184M
                samplesOut[ i ] = INT16TORES(resample_out_ptr[ i ]);
427
184M
            }
428
475k
        }
429
1.00M
    }
430
431
#ifdef ENABLE_OSCE_BWE
432
    decControl->prev_osce_extended_mode = decControl->osce_extended_mode;
433
#endif
434
435
    /* Create two channel output from mono stream */
436
796k
    if( decControl->nChannelsAPI == 2 && decControl->nChannelsInternal == 1 ) {
437
115k
        if ( stereo_to_mono ){
438
            /* Resample right channel for newly collapsed stereo just in case
439
               we weren't doing collapsing when switching to mono */
440
0
            ret += silk_resampler( &channel_state[ 1 ].resampler_state, resample_out_ptr, &samplesOut1_tmp[ 0 ][ 1 ], nSamplesOutDec );
441
442
0
            for( i = 0; i < *nSamplesOut; i++ ) {
443
0
                samplesOut[ 1 + 2 * i ] = INT16TORES(resample_out_ptr[ i ]);
444
0
            }
445
115k
        } else {
446
48.4M
            for( i = 0; i < *nSamplesOut; i++ ) {
447
48.3M
                samplesOut[ 1 + 2 * i ] = samplesOut[ 0 + 2 * i ];
448
48.3M
            }
449
115k
        }
450
115k
    }
451
452
    /* Export pitch lag, measured at 48 kHz sampling rate */
453
796k
    if( channel_state[ 0 ].prevSignalType == TYPE_VOICED ) {
454
178k
        int mult_tab[ 3 ] = { 6, 4, 3 };
455
178k
        decControl->prevPitchLag = channel_state[ 0 ].lagPrev * mult_tab[ ( channel_state[ 0 ].fs_kHz - 8 ) >> 2 ];
456
618k
    } else {
457
618k
        decControl->prevPitchLag = 0;
458
618k
    }
459
460
796k
    if( lostFlag == FLAG_PACKET_LOST ) {
461
       /* On packet loss, remove the gain clamping to prevent having the energy "bounce back"
462
          if we lose packets when the energy is going down */
463
387k
       for ( i = 0; i < psDec->nChannelsInternal; i++ )
464
249k
          psDec->channel_state[ i ].LastGainIndex = 10;
465
658k
    } else {
466
658k
       psDec->prev_decode_only_middle = decode_only_middle;
467
658k
    }
468
796k
    RESTORE_STACK;
469
796k
    return ret;
470
796k
}
silk_Decode
Line
Count
Source
148
398k
{
149
398k
    opus_int   i, n, decode_only_middle = 0, ret = SILK_NO_ERROR;
150
398k
    opus_int32 nSamplesOutDec, LBRR_symbol;
151
398k
    opus_int16 *samplesOut1_tmp[ 2 ];
152
398k
    VARDECL( opus_int16, samplesOut1_tmp_storage1 );
153
398k
    VARDECL( opus_int16, samplesOut2_tmp );
154
398k
    opus_int32 MS_pred_Q13[ 2 ] = { 0 };
155
398k
    opus_int16 *resample_out_ptr;
156
398k
    silk_decoder *psDec = ( silk_decoder * )decState;
157
398k
    silk_decoder_state *channel_state = psDec->channel_state;
158
398k
    opus_int has_side;
159
398k
    opus_int stereo_to_mono;
160
#ifdef ENABLE_OSCE_BWE
161
    ALLOC(resamp_buffer, 3 * MAX_FRAME_LENGTH, opus_int16);
162
#endif
163
398k
    SAVE_STACK;
164
165
398k
    celt_assert( decControl->nChannelsInternal == 1 || decControl->nChannelsInternal == 2 );
166
167
    /**********************************/
168
    /* Test if first frame in payload */
169
    /**********************************/
170
398k
    if( newPacketFlag ) {
171
688k
        for( n = 0; n < decControl->nChannelsInternal; n++ ) {
172
428k
            channel_state[ n ].nFramesDecoded = 0;  /* Used to count frames in packet */
173
428k
        }
174
259k
    }
175
176
    /* If Mono -> Stereo transition in bitstream: init state of second channel */
177
398k
    if( decControl->nChannelsInternal > psDec->nChannelsInternal ) {
178
135k
        ret += silk_init_decoder( &channel_state[ 1 ] );
179
135k
    }
180
181
398k
    stereo_to_mono = decControl->nChannelsInternal == 1 && psDec->nChannelsInternal == 2 &&
182
0
                     ( decControl->internalSampleRate == 1000*channel_state[ 0 ].fs_kHz );
183
184
398k
    if( channel_state[ 0 ].nFramesDecoded == 0 ) {
185
688k
        for( n = 0; n < decControl->nChannelsInternal; n++ ) {
186
428k
            opus_int fs_kHz_dec;
187
428k
            if( decControl->payloadSize_ms == 0 ) {
188
                /* Assuming packet loss, use 10 ms */
189
0
                channel_state[ n ].nFramesPerPacket = 1;
190
0
                channel_state[ n ].nb_subfr = 2;
191
428k
            } else if( decControl->payloadSize_ms == 10 ) {
192
209k
                channel_state[ n ].nFramesPerPacket = 1;
193
209k
                channel_state[ n ].nb_subfr = 2;
194
218k
            } else if( decControl->payloadSize_ms == 20 ) {
195
88.1k
                channel_state[ n ].nFramesPerPacket = 1;
196
88.1k
                channel_state[ n ].nb_subfr = 4;
197
130k
            } else if( decControl->payloadSize_ms == 40 ) {
198
16.2k
                channel_state[ n ].nFramesPerPacket = 2;
199
16.2k
                channel_state[ n ].nb_subfr = 4;
200
114k
            } else if( decControl->payloadSize_ms == 60 ) {
201
114k
                channel_state[ n ].nFramesPerPacket = 3;
202
114k
                channel_state[ n ].nb_subfr = 4;
203
114k
            } else {
204
0
                celt_assert( 0 );
205
0
                RESTORE_STACK;
206
0
                return SILK_DEC_INVALID_FRAME_SIZE;
207
0
            }
208
428k
            fs_kHz_dec = ( decControl->internalSampleRate >> 10 ) + 1;
209
428k
            if( fs_kHz_dec != 8 && fs_kHz_dec != 12 && fs_kHz_dec != 16 ) {
210
0
                celt_assert( 0 );
211
0
                RESTORE_STACK;
212
0
                return SILK_DEC_INVALID_SAMPLING_FREQUENCY;
213
0
            }
214
428k
            ret += silk_decoder_set_fs( &channel_state[ n ], fs_kHz_dec, decControl->API_sampleRate );
215
428k
        }
216
259k
    }
217
218
398k
    if( decControl->nChannelsAPI == 2 && decControl->nChannelsInternal == 2 && ( psDec->nChannelsAPI == 1 || psDec->nChannelsInternal == 1 ) ) {
219
0
        silk_memset( psDec->sStereo.pred_prev_Q13, 0, sizeof( psDec->sStereo.pred_prev_Q13 ) );
220
0
        silk_memset( psDec->sStereo.sSide, 0, sizeof( psDec->sStereo.sSide ) );
221
0
        silk_memcpy( &channel_state[ 1 ].resampler_state, &channel_state[ 0 ].resampler_state, sizeof( silk_resampler_state_struct ) );
222
0
    }
223
398k
    psDec->nChannelsAPI      = decControl->nChannelsAPI;
224
398k
    psDec->nChannelsInternal = decControl->nChannelsInternal;
225
226
398k
    if( decControl->API_sampleRate > (opus_int32)MAX_API_FS_KHZ * 1000 || decControl->API_sampleRate < 8000 ) {
227
0
        ret = SILK_DEC_INVALID_SAMPLING_FREQUENCY;
228
0
        RESTORE_STACK;
229
0
        return( ret );
230
0
    }
231
232
398k
    if( lostFlag != FLAG_PACKET_LOST && channel_state[ 0 ].nFramesDecoded == 0 ) {
233
        /* First decoder call for this payload */
234
        /* Decode VAD flags and LBRR flag */
235
494k
        for( n = 0; n < decControl->nChannelsInternal; n++ ) {
236
851k
            for( i = 0; i < channel_state[ n ].nFramesPerPacket; i++ ) {
237
548k
                channel_state[ n ].VAD_flags[ i ] = ec_dec_bit_logp(psRangeDec, 1);
238
548k
            }
239
303k
            channel_state[ n ].LBRR_flag = ec_dec_bit_logp(psRangeDec, 1);
240
303k
        }
241
        /* Decode LBRR flags */
242
494k
        for( n = 0; n < decControl->nChannelsInternal; n++ ) {
243
303k
            silk_memset( channel_state[ n ].LBRR_flags, 0, sizeof( channel_state[ n ].LBRR_flags ) );
244
303k
            if( channel_state[ n ].LBRR_flag ) {
245
131k
                if( channel_state[ n ].nFramesPerPacket == 1 ) {
246
49.7k
                    channel_state[ n ].LBRR_flags[ 0 ] = 1;
247
81.7k
                } else {
248
81.7k
                    LBRR_symbol = ec_dec_icdf( psRangeDec, silk_LBRR_flags_iCDF_ptr[ channel_state[ n ].nFramesPerPacket - 2 ], 8 ) + 1;
249
318k
                    for( i = 0; i < channel_state[ n ].nFramesPerPacket; i++ ) {
250
236k
                        channel_state[ n ].LBRR_flags[ i ] = silk_RSHIFT( LBRR_symbol, i ) & 1;
251
236k
                    }
252
81.7k
                }
253
131k
            }
254
303k
        }
255
256
190k
        if( lostFlag == FLAG_DECODE_NORMAL ) {
257
            /* Regular decoding: skip all LBRR data */
258
403k
            for( i = 0; i < channel_state[ 0 ].nFramesPerPacket; i++ ) {
259
667k
                for( n = 0; n < decControl->nChannelsInternal; n++ ) {
260
417k
                    if( channel_state[ n ].LBRR_flags[ i ] ) {
261
144k
                        opus_int16 pulses[ MAX_FRAME_LENGTH ];
262
144k
                        opus_int condCoding;
263
264
144k
                        if( decControl->nChannelsInternal == 2 && n == 0 ) {
265
62.4k
                            silk_stereo_decode_pred( psRangeDec, MS_pred_Q13 );
266
62.4k
                            if( channel_state[ 1 ].LBRR_flags[ i ] == 0 ) {
267
29.5k
                                silk_stereo_decode_mid_only( psRangeDec, &decode_only_middle );
268
29.5k
                            }
269
62.4k
                        }
270
                        /* Use conditional coding if previous frame available */
271
144k
                        if( i > 0 && channel_state[ n ].LBRR_flags[ i - 1 ] ) {
272
46.7k
                            condCoding = CODE_CONDITIONALLY;
273
97.4k
                        } else {
274
97.4k
                            condCoding = CODE_INDEPENDENTLY;
275
97.4k
                        }
276
144k
                        silk_decode_indices( &channel_state[ n ], psRangeDec, i, 1, condCoding );
277
144k
                        silk_decode_pulses( psRangeDec, pulses, channel_state[ n ].indices.signalType,
278
144k
                            channel_state[ n ].indices.quantOffsetType, channel_state[ n ].frame_length );
279
144k
                    }
280
417k
                }
281
249k
            }
282
154k
        }
283
190k
    }
284
285
    /* Get MS predictor index */
286
398k
    if( decControl->nChannelsInternal == 2 ) {
287
274k
        if(   lostFlag == FLAG_DECODE_NORMAL ||
288
106k
            ( lostFlag == FLAG_DECODE_LBRR && channel_state[ 0 ].LBRR_flags[ channel_state[ 0 ].nFramesDecoded ] == 1 ) )
289
189k
        {
290
189k
            silk_stereo_decode_pred( psRangeDec, MS_pred_Q13 );
291
            /* For LBRR data, decode mid-only flag only if side-channel's LBRR flag is false */
292
189k
            if( ( lostFlag == FLAG_DECODE_NORMAL && channel_state[ 1 ].VAD_flags[ channel_state[ 0 ].nFramesDecoded ] == 0 ) ||
293
107k
                ( lostFlag == FLAG_DECODE_LBRR && channel_state[ 1 ].LBRR_flags[ channel_state[ 0 ].nFramesDecoded ] == 0 ) )
294
94.6k
            {
295
94.6k
                silk_stereo_decode_mid_only( psRangeDec, &decode_only_middle );
296
94.6k
            } else {
297
94.5k
                decode_only_middle = 0;
298
94.5k
            }
299
189k
        } else {
300
256k
            for( n = 0; n < 2; n++ ) {
301
170k
                MS_pred_Q13[ n ] = psDec->sStereo.pred_prev_Q13[ n ];
302
170k
            }
303
85.4k
        }
304
274k
    }
305
306
    /* Reset side channel decoder prediction memory for first frame with side coding */
307
398k
    if( decControl->nChannelsInternal == 2 && decode_only_middle == 0 && psDec->prev_decode_only_middle == 1 ) {
308
20.1k
        silk_memset( psDec->channel_state[ 1 ].outBuf, 0, sizeof(psDec->channel_state[ 1 ].outBuf) );
309
20.1k
        silk_memset( psDec->channel_state[ 1 ].sLPC_Q14_buf, 0, sizeof(psDec->channel_state[ 1 ].sLPC_Q14_buf) );
310
20.1k
        psDec->channel_state[ 1 ].lagPrev        = 100;
311
20.1k
        psDec->channel_state[ 1 ].LastGainIndex  = 10;
312
20.1k
        psDec->channel_state[ 1 ].prevSignalType = TYPE_NO_VOICE_ACTIVITY;
313
20.1k
        psDec->channel_state[ 1 ].first_frame_after_reset = 1;
314
20.1k
    }
315
316
    /* Check if the temp buffer fits into the output PCM buffer. If it fits,
317
       we can delay allocating the temp buffer until after the SILK peak stack
318
       usage. We need to use a < and not a <= because of the two extra samples. */
319
398k
    ALLOC( samplesOut1_tmp_storage1, decControl->nChannelsInternal*(channel_state[ 0 ].frame_length + 2 ),
320
398k
           opus_int16 );
321
398k
    samplesOut1_tmp[ 0 ] = samplesOut1_tmp_storage1;
322
398k
    samplesOut1_tmp[ 1 ] = samplesOut1_tmp_storage1 + channel_state[ 0 ].frame_length + 2;
323
324
398k
    if( lostFlag == FLAG_DECODE_NORMAL ) {
325
249k
        has_side = !decode_only_middle;
326
249k
    } else {
327
148k
        has_side = !psDec->prev_decode_only_middle
328
11.7k
              || (decControl->nChannelsInternal == 2 && lostFlag == FLAG_DECODE_LBRR && channel_state[1].LBRR_flags[ channel_state[1].nFramesDecoded ] == 1 );
329
148k
    }
330
398k
    channel_state[ 0 ].sPLC.enable_deep_plc = decControl->enable_deep_plc;
331
    /* Call decoder for one frame */
332
1.07M
    for( n = 0; n < decControl->nChannelsInternal; n++ ) {
333
673k
        if( n == 0 || has_side ) {
334
646k
            opus_int FrameIndex;
335
646k
            opus_int condCoding;
336
337
646k
            FrameIndex = channel_state[ 0 ].nFramesDecoded - n;
338
            /* Use independent coding if no previous frame available */
339
646k
            if( FrameIndex <= 0 ) {
340
414k
                condCoding = CODE_INDEPENDENTLY;
341
414k
            } else if( lostFlag == FLAG_DECODE_LBRR ) {
342
64.5k
                condCoding = channel_state[ n ].LBRR_flags[ FrameIndex - 1 ] ? CODE_CONDITIONALLY : CODE_INDEPENDENTLY;
343
167k
            } else if( n > 0 && psDec->prev_decode_only_middle ) {
344
                /* If we skipped a side frame in this packet, we don't
345
                   need LTP scaling; the LTP state is well-defined. */
346
5.50k
                condCoding = CODE_INDEPENDENTLY_NO_LTP_SCALING;
347
161k
            } else {
348
161k
                condCoding = CODE_CONDITIONALLY;
349
161k
            }
350
#ifdef ENABLE_OSCE
351
            if ( channel_state[n].osce.method != decControl->osce_method ) {
352
                osce_reset( &channel_state[n].osce, decControl->osce_method );
353
            }
354
#endif
355
646k
            ret += silk_decode_frame( &channel_state[ n ], psRangeDec, &samplesOut1_tmp[ n ][ 2 ], &nSamplesOutDec, lostFlag, condCoding,
356
#ifdef ENABLE_DEEP_PLC
357
                n == 0 ? lpcnet : NULL,
358
#endif
359
#ifdef ENABLE_OSCE
360
                &psDec->osce_model,
361
#endif
362
646k
                arch);
363
646k
        } else {
364
26.3k
            silk_memset( &samplesOut1_tmp[ n ][ 2 ], 0, nSamplesOutDec * sizeof( opus_int16 ) );
365
26.3k
        }
366
673k
        channel_state[ n ].nFramesDecoded++;
367
673k
    }
368
369
398k
    if( decControl->nChannelsAPI == 2 && decControl->nChannelsInternal == 2 ) {
370
        /* Convert Mid/Side to Left/Right */
371
102k
        silk_stereo_MS_to_LR( &psDec->sStereo, samplesOut1_tmp[ 0 ], samplesOut1_tmp[ 1 ], MS_pred_Q13, channel_state[ 0 ].fs_kHz, nSamplesOutDec );
372
295k
    } else {
373
        /* Buffering */
374
295k
        silk_memcpy( samplesOut1_tmp[ 0 ], psDec->sStereo.sMid, 2 * sizeof( opus_int16 ) );
375
295k
        silk_memcpy( psDec->sStereo.sMid, &samplesOut1_tmp[ 0 ][ nSamplesOutDec ], 2 * sizeof( opus_int16 ) );
376
295k
    }
377
378
    /* Number of output samples */
379
398k
    *nSamplesOut = silk_DIV32( nSamplesOutDec * decControl->API_sampleRate, silk_SMULBB( channel_state[ 0 ].fs_kHz, 1000 ) );
380
381
    /* Set up pointers to temp buffers */
382
398k
    ALLOC( samplesOut2_tmp, *nSamplesOut, opus_int16 );
383
398k
    resample_out_ptr = samplesOut2_tmp;
384
385
899k
    for( n = 0; n < silk_min( decControl->nChannelsAPI, decControl->nChannelsInternal ); n++ ) {
386
387
#ifdef ENABLE_OSCE_BWE
388
        /* Resample or extend decoded signal to API_sampleRate */
389
        if (decControl->osce_extended_mode == OSCE_MODE_SILK_BBWE) {
390
            silk_assert(decControl->API_sampleRate == 48000);
391
392
            if (decControl->prev_osce_extended_mode != OSCE_MODE_SILK_BBWE) {
393
                /* Reset the BWE state */
394
                osce_bwe_reset( &channel_state[ n ].osce_bwe );
395
            }
396
397
            osce_bwe(&psDec->osce_model, &channel_state[ n ].osce_bwe,
398
                resample_out_ptr, &samplesOut1_tmp[ n ][ 1 ], nSamplesOutDec, arch);
399
400
            if (decControl->prev_osce_extended_mode == OSCE_MODE_SILK_ONLY ||
401
                decControl->prev_osce_extended_mode == OSCE_MODE_HYBRID) {
402
                    /* cross-fade with upsampled signal */
403
                    silk_resampler( &channel_state[ n ].resampler_state, resamp_buffer, &samplesOut1_tmp[ n ][ 1 ], nSamplesOutDec );
404
                    osce_bwe_cross_fade_10ms(resample_out_ptr, resamp_buffer, 480);
405
            }
406
        } else {
407
            ret += silk_resampler( &channel_state[ n ].resampler_state, resample_out_ptr, &samplesOut1_tmp[ n ][ 1 ], nSamplesOutDec );
408
            if (decControl->prev_osce_extended_mode == OSCE_MODE_SILK_BBWE) {
409
                osce_bwe(&psDec->osce_model, &channel_state[ n ].osce_bwe,
410
                    resamp_buffer, &samplesOut1_tmp[ n ][ 1 ], nSamplesOutDec, arch);
411
                /* cross-fade with upsampled signal */
412
                osce_bwe_cross_fade_10ms(resample_out_ptr, resamp_buffer, 480);
413
            }
414
        }
415
#else
416
        /* Resample decoded signal to API_sampleRate */
417
501k
        ret += silk_resampler( &channel_state[ n ].resampler_state, resample_out_ptr, &samplesOut1_tmp[ n ][ 1 ], nSamplesOutDec );
418
501k
#endif
419
        /* Interleave if stereo output and stereo stream */
420
501k
        if( decControl->nChannelsAPI == 2 ) {
421
108M
            for( i = 0; i < *nSamplesOut; i++ ) {
422
108M
                samplesOut[ n + 2 * i ] = INT16TORES(resample_out_ptr[ i ]);
423
108M
            }
424
263k
        } else {
425
92.2M
            for( i = 0; i < *nSamplesOut; i++ ) {
426
92.0M
                samplesOut[ i ] = INT16TORES(resample_out_ptr[ i ]);
427
92.0M
            }
428
237k
        }
429
501k
    }
430
431
#ifdef ENABLE_OSCE_BWE
432
    decControl->prev_osce_extended_mode = decControl->osce_extended_mode;
433
#endif
434
435
    /* Create two channel output from mono stream */
436
398k
    if( decControl->nChannelsAPI == 2 && decControl->nChannelsInternal == 1 ) {
437
57.7k
        if ( stereo_to_mono ){
438
            /* Resample right channel for newly collapsed stereo just in case
439
               we weren't doing collapsing when switching to mono */
440
0
            ret += silk_resampler( &channel_state[ 1 ].resampler_state, resample_out_ptr, &samplesOut1_tmp[ 0 ][ 1 ], nSamplesOutDec );
441
442
0
            for( i = 0; i < *nSamplesOut; i++ ) {
443
0
                samplesOut[ 1 + 2 * i ] = INT16TORES(resample_out_ptr[ i ]);
444
0
            }
445
57.7k
        } else {
446
24.2M
            for( i = 0; i < *nSamplesOut; i++ ) {
447
24.1M
                samplesOut[ 1 + 2 * i ] = samplesOut[ 0 + 2 * i ];
448
24.1M
            }
449
57.7k
        }
450
57.7k
    }
451
452
    /* Export pitch lag, measured at 48 kHz sampling rate */
453
398k
    if( channel_state[ 0 ].prevSignalType == TYPE_VOICED ) {
454
89.2k
        int mult_tab[ 3 ] = { 6, 4, 3 };
455
89.2k
        decControl->prevPitchLag = channel_state[ 0 ].lagPrev * mult_tab[ ( channel_state[ 0 ].fs_kHz - 8 ) >> 2 ];
456
309k
    } else {
457
309k
        decControl->prevPitchLag = 0;
458
309k
    }
459
460
398k
    if( lostFlag == FLAG_PACKET_LOST ) {
461
       /* On packet loss, remove the gain clamping to prevent having the energy "bounce back"
462
          if we lose packets when the energy is going down */
463
193k
       for ( i = 0; i < psDec->nChannelsInternal; i++ )
464
124k
          psDec->channel_state[ i ].LastGainIndex = 10;
465
329k
    } else {
466
329k
       psDec->prev_decode_only_middle = decode_only_middle;
467
329k
    }
468
398k
    RESTORE_STACK;
469
398k
    return ret;
470
398k
}
silk_Decode
Line
Count
Source
148
398k
{
149
398k
    opus_int   i, n, decode_only_middle = 0, ret = SILK_NO_ERROR;
150
398k
    opus_int32 nSamplesOutDec, LBRR_symbol;
151
398k
    opus_int16 *samplesOut1_tmp[ 2 ];
152
398k
    VARDECL( opus_int16, samplesOut1_tmp_storage1 );
153
398k
    VARDECL( opus_int16, samplesOut2_tmp );
154
398k
    opus_int32 MS_pred_Q13[ 2 ] = { 0 };
155
398k
    opus_int16 *resample_out_ptr;
156
398k
    silk_decoder *psDec = ( silk_decoder * )decState;
157
398k
    silk_decoder_state *channel_state = psDec->channel_state;
158
398k
    opus_int has_side;
159
398k
    opus_int stereo_to_mono;
160
#ifdef ENABLE_OSCE_BWE
161
    ALLOC(resamp_buffer, 3 * MAX_FRAME_LENGTH, opus_int16);
162
#endif
163
398k
    SAVE_STACK;
164
165
398k
    celt_assert( decControl->nChannelsInternal == 1 || decControl->nChannelsInternal == 2 );
166
167
    /**********************************/
168
    /* Test if first frame in payload */
169
    /**********************************/
170
398k
    if( newPacketFlag ) {
171
688k
        for( n = 0; n < decControl->nChannelsInternal; n++ ) {
172
428k
            channel_state[ n ].nFramesDecoded = 0;  /* Used to count frames in packet */
173
428k
        }
174
259k
    }
175
176
    /* If Mono -> Stereo transition in bitstream: init state of second channel */
177
398k
    if( decControl->nChannelsInternal > psDec->nChannelsInternal ) {
178
135k
        ret += silk_init_decoder( &channel_state[ 1 ] );
179
135k
    }
180
181
398k
    stereo_to_mono = decControl->nChannelsInternal == 1 && psDec->nChannelsInternal == 2 &&
182
0
                     ( decControl->internalSampleRate == 1000*channel_state[ 0 ].fs_kHz );
183
184
398k
    if( channel_state[ 0 ].nFramesDecoded == 0 ) {
185
688k
        for( n = 0; n < decControl->nChannelsInternal; n++ ) {
186
428k
            opus_int fs_kHz_dec;
187
428k
            if( decControl->payloadSize_ms == 0 ) {
188
                /* Assuming packet loss, use 10 ms */
189
0
                channel_state[ n ].nFramesPerPacket = 1;
190
0
                channel_state[ n ].nb_subfr = 2;
191
428k
            } else if( decControl->payloadSize_ms == 10 ) {
192
209k
                channel_state[ n ].nFramesPerPacket = 1;
193
209k
                channel_state[ n ].nb_subfr = 2;
194
218k
            } else if( decControl->payloadSize_ms == 20 ) {
195
88.1k
                channel_state[ n ].nFramesPerPacket = 1;
196
88.1k
                channel_state[ n ].nb_subfr = 4;
197
130k
            } else if( decControl->payloadSize_ms == 40 ) {
198
16.2k
                channel_state[ n ].nFramesPerPacket = 2;
199
16.2k
                channel_state[ n ].nb_subfr = 4;
200
114k
            } else if( decControl->payloadSize_ms == 60 ) {
201
114k
                channel_state[ n ].nFramesPerPacket = 3;
202
114k
                channel_state[ n ].nb_subfr = 4;
203
114k
            } else {
204
0
                celt_assert( 0 );
205
0
                RESTORE_STACK;
206
0
                return SILK_DEC_INVALID_FRAME_SIZE;
207
0
            }
208
428k
            fs_kHz_dec = ( decControl->internalSampleRate >> 10 ) + 1;
209
428k
            if( fs_kHz_dec != 8 && fs_kHz_dec != 12 && fs_kHz_dec != 16 ) {
210
0
                celt_assert( 0 );
211
0
                RESTORE_STACK;
212
0
                return SILK_DEC_INVALID_SAMPLING_FREQUENCY;
213
0
            }
214
428k
            ret += silk_decoder_set_fs( &channel_state[ n ], fs_kHz_dec, decControl->API_sampleRate );
215
428k
        }
216
259k
    }
217
218
398k
    if( decControl->nChannelsAPI == 2 && decControl->nChannelsInternal == 2 && ( psDec->nChannelsAPI == 1 || psDec->nChannelsInternal == 1 ) ) {
219
0
        silk_memset( psDec->sStereo.pred_prev_Q13, 0, sizeof( psDec->sStereo.pred_prev_Q13 ) );
220
0
        silk_memset( psDec->sStereo.sSide, 0, sizeof( psDec->sStereo.sSide ) );
221
0
        silk_memcpy( &channel_state[ 1 ].resampler_state, &channel_state[ 0 ].resampler_state, sizeof( silk_resampler_state_struct ) );
222
0
    }
223
398k
    psDec->nChannelsAPI      = decControl->nChannelsAPI;
224
398k
    psDec->nChannelsInternal = decControl->nChannelsInternal;
225
226
398k
    if( decControl->API_sampleRate > (opus_int32)MAX_API_FS_KHZ * 1000 || decControl->API_sampleRate < 8000 ) {
227
0
        ret = SILK_DEC_INVALID_SAMPLING_FREQUENCY;
228
0
        RESTORE_STACK;
229
0
        return( ret );
230
0
    }
231
232
398k
    if( lostFlag != FLAG_PACKET_LOST && channel_state[ 0 ].nFramesDecoded == 0 ) {
233
        /* First decoder call for this payload */
234
        /* Decode VAD flags and LBRR flag */
235
494k
        for( n = 0; n < decControl->nChannelsInternal; n++ ) {
236
851k
            for( i = 0; i < channel_state[ n ].nFramesPerPacket; i++ ) {
237
548k
                channel_state[ n ].VAD_flags[ i ] = ec_dec_bit_logp(psRangeDec, 1);
238
548k
            }
239
303k
            channel_state[ n ].LBRR_flag = ec_dec_bit_logp(psRangeDec, 1);
240
303k
        }
241
        /* Decode LBRR flags */
242
494k
        for( n = 0; n < decControl->nChannelsInternal; n++ ) {
243
303k
            silk_memset( channel_state[ n ].LBRR_flags, 0, sizeof( channel_state[ n ].LBRR_flags ) );
244
303k
            if( channel_state[ n ].LBRR_flag ) {
245
131k
                if( channel_state[ n ].nFramesPerPacket == 1 ) {
246
49.7k
                    channel_state[ n ].LBRR_flags[ 0 ] = 1;
247
81.7k
                } else {
248
81.7k
                    LBRR_symbol = ec_dec_icdf( psRangeDec, silk_LBRR_flags_iCDF_ptr[ channel_state[ n ].nFramesPerPacket - 2 ], 8 ) + 1;
249
318k
                    for( i = 0; i < channel_state[ n ].nFramesPerPacket; i++ ) {
250
236k
                        channel_state[ n ].LBRR_flags[ i ] = silk_RSHIFT( LBRR_symbol, i ) & 1;
251
236k
                    }
252
81.7k
                }
253
131k
            }
254
303k
        }
255
256
190k
        if( lostFlag == FLAG_DECODE_NORMAL ) {
257
            /* Regular decoding: skip all LBRR data */
258
403k
            for( i = 0; i < channel_state[ 0 ].nFramesPerPacket; i++ ) {
259
667k
                for( n = 0; n < decControl->nChannelsInternal; n++ ) {
260
417k
                    if( channel_state[ n ].LBRR_flags[ i ] ) {
261
144k
                        opus_int16 pulses[ MAX_FRAME_LENGTH ];
262
144k
                        opus_int condCoding;
263
264
144k
                        if( decControl->nChannelsInternal == 2 && n == 0 ) {
265
62.4k
                            silk_stereo_decode_pred( psRangeDec, MS_pred_Q13 );
266
62.4k
                            if( channel_state[ 1 ].LBRR_flags[ i ] == 0 ) {
267
29.5k
                                silk_stereo_decode_mid_only( psRangeDec, &decode_only_middle );
268
29.5k
                            }
269
62.4k
                        }
270
                        /* Use conditional coding if previous frame available */
271
144k
                        if( i > 0 && channel_state[ n ].LBRR_flags[ i - 1 ] ) {
272
46.7k
                            condCoding = CODE_CONDITIONALLY;
273
97.4k
                        } else {
274
97.4k
                            condCoding = CODE_INDEPENDENTLY;
275
97.4k
                        }
276
144k
                        silk_decode_indices( &channel_state[ n ], psRangeDec, i, 1, condCoding );
277
144k
                        silk_decode_pulses( psRangeDec, pulses, channel_state[ n ].indices.signalType,
278
144k
                            channel_state[ n ].indices.quantOffsetType, channel_state[ n ].frame_length );
279
144k
                    }
280
417k
                }
281
249k
            }
282
154k
        }
283
190k
    }
284
285
    /* Get MS predictor index */
286
398k
    if( decControl->nChannelsInternal == 2 ) {
287
274k
        if(   lostFlag == FLAG_DECODE_NORMAL ||
288
106k
            ( lostFlag == FLAG_DECODE_LBRR && channel_state[ 0 ].LBRR_flags[ channel_state[ 0 ].nFramesDecoded ] == 1 ) )
289
189k
        {
290
189k
            silk_stereo_decode_pred( psRangeDec, MS_pred_Q13 );
291
            /* For LBRR data, decode mid-only flag only if side-channel's LBRR flag is false */
292
189k
            if( ( lostFlag == FLAG_DECODE_NORMAL && channel_state[ 1 ].VAD_flags[ channel_state[ 0 ].nFramesDecoded ] == 0 ) ||
293
107k
                ( lostFlag == FLAG_DECODE_LBRR && channel_state[ 1 ].LBRR_flags[ channel_state[ 0 ].nFramesDecoded ] == 0 ) )
294
94.6k
            {
295
94.6k
                silk_stereo_decode_mid_only( psRangeDec, &decode_only_middle );
296
94.6k
            } else {
297
94.5k
                decode_only_middle = 0;
298
94.5k
            }
299
189k
        } else {
300
256k
            for( n = 0; n < 2; n++ ) {
301
170k
                MS_pred_Q13[ n ] = psDec->sStereo.pred_prev_Q13[ n ];
302
170k
            }
303
85.4k
        }
304
274k
    }
305
306
    /* Reset side channel decoder prediction memory for first frame with side coding */
307
398k
    if( decControl->nChannelsInternal == 2 && decode_only_middle == 0 && psDec->prev_decode_only_middle == 1 ) {
308
20.1k
        silk_memset( psDec->channel_state[ 1 ].outBuf, 0, sizeof(psDec->channel_state[ 1 ].outBuf) );
309
20.1k
        silk_memset( psDec->channel_state[ 1 ].sLPC_Q14_buf, 0, sizeof(psDec->channel_state[ 1 ].sLPC_Q14_buf) );
310
20.1k
        psDec->channel_state[ 1 ].lagPrev        = 100;
311
20.1k
        psDec->channel_state[ 1 ].LastGainIndex  = 10;
312
20.1k
        psDec->channel_state[ 1 ].prevSignalType = TYPE_NO_VOICE_ACTIVITY;
313
20.1k
        psDec->channel_state[ 1 ].first_frame_after_reset = 1;
314
20.1k
    }
315
316
    /* Check if the temp buffer fits into the output PCM buffer. If it fits,
317
       we can delay allocating the temp buffer until after the SILK peak stack
318
       usage. We need to use a < and not a <= because of the two extra samples. */
319
398k
    ALLOC( samplesOut1_tmp_storage1, decControl->nChannelsInternal*(channel_state[ 0 ].frame_length + 2 ),
320
398k
           opus_int16 );
321
398k
    samplesOut1_tmp[ 0 ] = samplesOut1_tmp_storage1;
322
398k
    samplesOut1_tmp[ 1 ] = samplesOut1_tmp_storage1 + channel_state[ 0 ].frame_length + 2;
323
324
398k
    if( lostFlag == FLAG_DECODE_NORMAL ) {
325
249k
        has_side = !decode_only_middle;
326
249k
    } else {
327
148k
        has_side = !psDec->prev_decode_only_middle
328
11.7k
              || (decControl->nChannelsInternal == 2 && lostFlag == FLAG_DECODE_LBRR && channel_state[1].LBRR_flags[ channel_state[1].nFramesDecoded ] == 1 );
329
148k
    }
330
398k
    channel_state[ 0 ].sPLC.enable_deep_plc = decControl->enable_deep_plc;
331
    /* Call decoder for one frame */
332
1.07M
    for( n = 0; n < decControl->nChannelsInternal; n++ ) {
333
673k
        if( n == 0 || has_side ) {
334
646k
            opus_int FrameIndex;
335
646k
            opus_int condCoding;
336
337
646k
            FrameIndex = channel_state[ 0 ].nFramesDecoded - n;
338
            /* Use independent coding if no previous frame available */
339
646k
            if( FrameIndex <= 0 ) {
340
414k
                condCoding = CODE_INDEPENDENTLY;
341
414k
            } else if( lostFlag == FLAG_DECODE_LBRR ) {
342
64.5k
                condCoding = channel_state[ n ].LBRR_flags[ FrameIndex - 1 ] ? CODE_CONDITIONALLY : CODE_INDEPENDENTLY;
343
167k
            } else if( n > 0 && psDec->prev_decode_only_middle ) {
344
                /* If we skipped a side frame in this packet, we don't
345
                   need LTP scaling; the LTP state is well-defined. */
346
5.50k
                condCoding = CODE_INDEPENDENTLY_NO_LTP_SCALING;
347
161k
            } else {
348
161k
                condCoding = CODE_CONDITIONALLY;
349
161k
            }
350
#ifdef ENABLE_OSCE
351
            if ( channel_state[n].osce.method != decControl->osce_method ) {
352
                osce_reset( &channel_state[n].osce, decControl->osce_method );
353
            }
354
#endif
355
646k
            ret += silk_decode_frame( &channel_state[ n ], psRangeDec, &samplesOut1_tmp[ n ][ 2 ], &nSamplesOutDec, lostFlag, condCoding,
356
#ifdef ENABLE_DEEP_PLC
357
                n == 0 ? lpcnet : NULL,
358
#endif
359
#ifdef ENABLE_OSCE
360
                &psDec->osce_model,
361
#endif
362
646k
                arch);
363
646k
        } else {
364
26.3k
            silk_memset( &samplesOut1_tmp[ n ][ 2 ], 0, nSamplesOutDec * sizeof( opus_int16 ) );
365
26.3k
        }
366
673k
        channel_state[ n ].nFramesDecoded++;
367
673k
    }
368
369
398k
    if( decControl->nChannelsAPI == 2 && decControl->nChannelsInternal == 2 ) {
370
        /* Convert Mid/Side to Left/Right */
371
102k
        silk_stereo_MS_to_LR( &psDec->sStereo, samplesOut1_tmp[ 0 ], samplesOut1_tmp[ 1 ], MS_pred_Q13, channel_state[ 0 ].fs_kHz, nSamplesOutDec );
372
295k
    } else {
373
        /* Buffering */
374
295k
        silk_memcpy( samplesOut1_tmp[ 0 ], psDec->sStereo.sMid, 2 * sizeof( opus_int16 ) );
375
295k
        silk_memcpy( psDec->sStereo.sMid, &samplesOut1_tmp[ 0 ][ nSamplesOutDec ], 2 * sizeof( opus_int16 ) );
376
295k
    }
377
378
    /* Number of output samples */
379
398k
    *nSamplesOut = silk_DIV32( nSamplesOutDec * decControl->API_sampleRate, silk_SMULBB( channel_state[ 0 ].fs_kHz, 1000 ) );
380
381
    /* Set up pointers to temp buffers */
382
398k
    ALLOC( samplesOut2_tmp, *nSamplesOut, opus_int16 );
383
398k
    resample_out_ptr = samplesOut2_tmp;
384
385
899k
    for( n = 0; n < silk_min( decControl->nChannelsAPI, decControl->nChannelsInternal ); n++ ) {
386
387
#ifdef ENABLE_OSCE_BWE
388
        /* Resample or extend decoded signal to API_sampleRate */
389
        if (decControl->osce_extended_mode == OSCE_MODE_SILK_BBWE) {
390
            silk_assert(decControl->API_sampleRate == 48000);
391
392
            if (decControl->prev_osce_extended_mode != OSCE_MODE_SILK_BBWE) {
393
                /* Reset the BWE state */
394
                osce_bwe_reset( &channel_state[ n ].osce_bwe );
395
            }
396
397
            osce_bwe(&psDec->osce_model, &channel_state[ n ].osce_bwe,
398
                resample_out_ptr, &samplesOut1_tmp[ n ][ 1 ], nSamplesOutDec, arch);
399
400
            if (decControl->prev_osce_extended_mode == OSCE_MODE_SILK_ONLY ||
401
                decControl->prev_osce_extended_mode == OSCE_MODE_HYBRID) {
402
                    /* cross-fade with upsampled signal */
403
                    silk_resampler( &channel_state[ n ].resampler_state, resamp_buffer, &samplesOut1_tmp[ n ][ 1 ], nSamplesOutDec );
404
                    osce_bwe_cross_fade_10ms(resample_out_ptr, resamp_buffer, 480);
405
            }
406
        } else {
407
            ret += silk_resampler( &channel_state[ n ].resampler_state, resample_out_ptr, &samplesOut1_tmp[ n ][ 1 ], nSamplesOutDec );
408
            if (decControl->prev_osce_extended_mode == OSCE_MODE_SILK_BBWE) {
409
                osce_bwe(&psDec->osce_model, &channel_state[ n ].osce_bwe,
410
                    resamp_buffer, &samplesOut1_tmp[ n ][ 1 ], nSamplesOutDec, arch);
411
                /* cross-fade with upsampled signal */
412
                osce_bwe_cross_fade_10ms(resample_out_ptr, resamp_buffer, 480);
413
            }
414
        }
415
#else
416
        /* Resample decoded signal to API_sampleRate */
417
501k
        ret += silk_resampler( &channel_state[ n ].resampler_state, resample_out_ptr, &samplesOut1_tmp[ n ][ 1 ], nSamplesOutDec );
418
501k
#endif
419
        /* Interleave if stereo output and stereo stream */
420
501k
        if( decControl->nChannelsAPI == 2 ) {
421
108M
            for( i = 0; i < *nSamplesOut; i++ ) {
422
108M
                samplesOut[ n + 2 * i ] = INT16TORES(resample_out_ptr[ i ]);
423
108M
            }
424
263k
        } else {
425
92.2M
            for( i = 0; i < *nSamplesOut; i++ ) {
426
92.0M
                samplesOut[ i ] = INT16TORES(resample_out_ptr[ i ]);
427
92.0M
            }
428
237k
        }
429
501k
    }
430
431
#ifdef ENABLE_OSCE_BWE
432
    decControl->prev_osce_extended_mode = decControl->osce_extended_mode;
433
#endif
434
435
    /* Create two channel output from mono stream */
436
398k
    if( decControl->nChannelsAPI == 2 && decControl->nChannelsInternal == 1 ) {
437
57.7k
        if ( stereo_to_mono ){
438
            /* Resample right channel for newly collapsed stereo just in case
439
               we weren't doing collapsing when switching to mono */
440
0
            ret += silk_resampler( &channel_state[ 1 ].resampler_state, resample_out_ptr, &samplesOut1_tmp[ 0 ][ 1 ], nSamplesOutDec );
441
442
0
            for( i = 0; i < *nSamplesOut; i++ ) {
443
0
                samplesOut[ 1 + 2 * i ] = INT16TORES(resample_out_ptr[ i ]);
444
0
            }
445
57.7k
        } else {
446
24.2M
            for( i = 0; i < *nSamplesOut; i++ ) {
447
24.1M
                samplesOut[ 1 + 2 * i ] = samplesOut[ 0 + 2 * i ];
448
24.1M
            }
449
57.7k
        }
450
57.7k
    }
451
452
    /* Export pitch lag, measured at 48 kHz sampling rate */
453
398k
    if( channel_state[ 0 ].prevSignalType == TYPE_VOICED ) {
454
89.2k
        int mult_tab[ 3 ] = { 6, 4, 3 };
455
89.2k
        decControl->prevPitchLag = channel_state[ 0 ].lagPrev * mult_tab[ ( channel_state[ 0 ].fs_kHz - 8 ) >> 2 ];
456
309k
    } else {
457
309k
        decControl->prevPitchLag = 0;
458
309k
    }
459
460
398k
    if( lostFlag == FLAG_PACKET_LOST ) {
461
       /* On packet loss, remove the gain clamping to prevent having the energy "bounce back"
462
          if we lose packets when the energy is going down */
463
193k
       for ( i = 0; i < psDec->nChannelsInternal; i++ )
464
124k
          psDec->channel_state[ i ].LastGainIndex = 10;
465
329k
    } else {
466
329k
       psDec->prev_decode_only_middle = decode_only_middle;
467
329k
    }
468
398k
    RESTORE_STACK;
469
398k
    return ret;
470
398k
}
471
472
#if 0
473
/* Getting table of contents for a packet */
474
opus_int silk_get_TOC(
475
    const opus_uint8                *payload,           /* I    Payload data                                */
476
    const opus_int                  nBytesIn,           /* I    Number of input bytes                       */
477
    const opus_int                  nFramesPerPayload,  /* I    Number of SILK frames per payload           */
478
    silk_TOC_struct                 *Silk_TOC           /* O    Type of content                             */
479
)
480
{
481
    opus_int i, flags, ret = SILK_NO_ERROR;
482
483
    if( nBytesIn < 1 ) {
484
        return -1;
485
    }
486
    if( nFramesPerPayload < 0 || nFramesPerPayload > 3 ) {
487
        return -1;
488
    }
489
490
    silk_memset( Silk_TOC, 0, sizeof( *Silk_TOC ) );
491
492
    /* For stereo, extract the flags for the mid channel */
493
    flags = silk_RSHIFT( payload[ 0 ], 7 - nFramesPerPayload ) & ( silk_LSHIFT( 1, nFramesPerPayload + 1 ) - 1 );
494
495
    Silk_TOC->inbandFECFlag = flags & 1;
496
    for( i = nFramesPerPayload - 1; i >= 0 ; i-- ) {
497
        flags = silk_RSHIFT( flags, 1 );
498
        Silk_TOC->VADFlags[ i ] = flags & 1;
499
        Silk_TOC->VADFlag |= flags & 1;
500
    }
501
502
    return ret;
503
}
504
#endif