Coverage Report

Created: 2026-08-13 07:15

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
414k
{
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
414k
    (void) decState;
74
414k
    (void) data;
75
414k
    (void) len;
76
414k
    return SILK_NO_ERROR;
77
414k
#endif
78
414k
}
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.26M
{
84
1.26M
    opus_int ret = SILK_NO_ERROR;
85
86
1.26M
    *decSizeBytes = sizeof( silk_decoder );
87
88
1.26M
    return ret;
89
1.26M
}
90
91
/* Reset decoder state */
92
opus_int silk_ResetDecoder(                              /* O    Returns error code                              */
93
    void                            *decState           /* I/O  State                                           */
94
)
95
516
{
96
516
    opus_int n, ret = SILK_NO_ERROR;
97
516
    silk_decoder_state *channel_state = ((silk_decoder *)decState)->channel_state;
98
99
1.54k
    for( n = 0; n < DECODER_NUM_CHANNELS; n++ ) {
100
1.03k
        ret  = silk_reset_decoder( &channel_state[ n ] );
101
1.03k
    }
102
516
    silk_memset(&((silk_decoder *)decState)->sStereo, 0, sizeof(((silk_decoder *)decState)->sStereo));
103
    /* Not strictly needed, but it's cleaner that way */
104
516
    ((silk_decoder *)decState)->prev_decode_only_middle = 0;
105
106
516
    return ret;
107
516
}
108
109
110
opus_int silk_InitDecoder(                              /* O    Returns error code                              */
111
    void                            *decState           /* I/O  State                                           */
112
)
113
414k
{
114
414k
    opus_int n, ret = SILK_NO_ERROR;
115
414k
    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
414k
#ifndef USE_WEIGHTS_FILE
120
    /* load osce models */
121
414k
    silk_LoadOSCEModels(decState, NULL, 0);
122
414k
#endif
123
124
1.24M
    for( n = 0; n < DECODER_NUM_CHANNELS; n++ ) {
125
829k
        ret  = silk_init_decoder( &channel_state[ n ] );
126
829k
    }
127
414k
    silk_memset(&((silk_decoder *)decState)->sStereo, 0, sizeof(((silk_decoder *)decState)->sStereo));
128
    /* Not strictly needed, but it's cleaner that way */
129
414k
    ((silk_decoder *)decState)->prev_decode_only_middle = 0;
130
131
414k
    return ret;
132
414k
}
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
877k
{
149
877k
    opus_int   i, n, decode_only_middle = 0, ret = SILK_NO_ERROR;
150
877k
    opus_int32 nSamplesOutDec, LBRR_symbol;
151
877k
    opus_int16 *samplesOut1_tmp[ 2 ];
152
877k
    VARDECL( opus_int16, samplesOut1_tmp_storage1 );
153
877k
    VARDECL( opus_int16, samplesOut2_tmp );
154
877k
    opus_int32 MS_pred_Q13[ 2 ] = { 0 };
155
877k
    opus_int16 *resample_out_ptr;
156
877k
    silk_decoder *psDec = ( silk_decoder * )decState;
157
877k
    silk_decoder_state *channel_state = psDec->channel_state;
158
877k
    opus_int has_side;
159
877k
    opus_int stereo_to_mono;
160
#ifdef ENABLE_OSCE_BWE
161
    VARDECL( opus_int16, resamp_buffer );
162
#endif
163
877k
    SAVE_STACK;
164
165
877k
    celt_assert( decControl->nChannelsInternal == 1 || decControl->nChannelsInternal == 2 );
166
167
    /**********************************/
168
    /* Test if first frame in payload */
169
    /**********************************/
170
877k
    if( newPacketFlag ) {
171
1.51M
        for( n = 0; n < decControl->nChannelsInternal; n++ ) {
172
949k
            channel_state[ n ].nFramesDecoded = 0;  /* Used to count frames in packet */
173
949k
        }
174
567k
    }
175
176
    /* If Mono -> Stereo transition in bitstream: init state of second channel */
177
877k
    if( decControl->nChannelsInternal > psDec->nChannelsInternal ) {
178
278k
        ret += silk_init_decoder( &channel_state[ 1 ] );
179
278k
    }
180
181
877k
    stereo_to_mono = decControl->nChannelsInternal == 1 && psDec->nChannelsInternal == 2 &&
182
0
                     ( decControl->internalSampleRate == 1000*channel_state[ 0 ].fs_kHz );
183
184
877k
    if( channel_state[ 0 ].nFramesDecoded == 0 ) {
185
1.51M
        for( n = 0; n < decControl->nChannelsInternal; n++ ) {
186
949k
            opus_int fs_kHz_dec;
187
949k
            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
949k
            } else if( decControl->payloadSize_ms == 10 ) {
192
454k
                channel_state[ n ].nFramesPerPacket = 1;
193
454k
                channel_state[ n ].nb_subfr = 2;
194
495k
            } else if( decControl->payloadSize_ms == 20 ) {
195
205k
                channel_state[ n ].nFramesPerPacket = 1;
196
205k
                channel_state[ n ].nb_subfr = 4;
197
289k
            } else if( decControl->payloadSize_ms == 40 ) {
198
27.4k
                channel_state[ n ].nFramesPerPacket = 2;
199
27.4k
                channel_state[ n ].nb_subfr = 4;
200
262k
            } else if( decControl->payloadSize_ms == 60 ) {
201
262k
                channel_state[ n ].nFramesPerPacket = 3;
202
262k
                channel_state[ n ].nb_subfr = 4;
203
262k
            } else {
204
0
                celt_assert( 0 );
205
0
                RESTORE_STACK;
206
0
                return SILK_DEC_INVALID_FRAME_SIZE;
207
0
            }
208
949k
            fs_kHz_dec = ( decControl->internalSampleRate >> 10 ) + 1;
209
949k
            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
949k
            ret += silk_decoder_set_fs( &channel_state[ n ], fs_kHz_dec, decControl->API_sampleRate );
215
949k
        }
216
567k
    }
217
218
877k
    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
877k
    psDec->nChannelsAPI      = decControl->nChannelsAPI;
224
877k
    psDec->nChannelsInternal = decControl->nChannelsInternal;
225
226
877k
    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
877k
    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
1.03M
        for( n = 0; n < decControl->nChannelsInternal; n++ ) {
236
1.82M
            for( i = 0; i < channel_state[ n ].nFramesPerPacket; i++ ) {
237
1.18M
                channel_state[ n ].VAD_flags[ i ] = ec_dec_bit_logp(psRangeDec, 1);
238
1.18M
            }
239
635k
            channel_state[ n ].LBRR_flag = ec_dec_bit_logp(psRangeDec, 1);
240
635k
        }
241
        /* Decode LBRR flags */
242
1.03M
        for( n = 0; n < decControl->nChannelsInternal; n++ ) {
243
635k
            silk_memset( channel_state[ n ].LBRR_flags, 0, sizeof( channel_state[ n ].LBRR_flags ) );
244
635k
            if( channel_state[ n ].LBRR_flag ) {
245
292k
                if( channel_state[ n ].nFramesPerPacket == 1 ) {
246
113k
                    channel_state[ n ].LBRR_flags[ 0 ] = 1;
247
179k
                } else {
248
179k
                    LBRR_symbol = ec_dec_icdf( psRangeDec, silk_LBRR_flags_iCDF_ptr[ channel_state[ n ].nFramesPerPacket - 2 ], 8 ) + 1;
249
700k
                    for( i = 0; i < channel_state[ n ].nFramesPerPacket; i++ ) {
250
521k
                        channel_state[ n ].LBRR_flags[ i ] = silk_RSHIFT( LBRR_symbol, i ) & 1;
251
521k
                    }
252
179k
                }
253
292k
            }
254
635k
        }
255
256
396k
        if( lostFlag == FLAG_DECODE_NORMAL ) {
257
            /* Regular decoding: skip all LBRR data */
258
852k
            for( i = 0; i < channel_state[ 0 ].nFramesPerPacket; i++ ) {
259
1.44M
                for( n = 0; n < decControl->nChannelsInternal; n++ ) {
260
907k
                    if( channel_state[ n ].LBRR_flags[ i ] ) {
261
321k
                        opus_int16 pulses[ MAX_FRAME_LENGTH ];
262
321k
                        opus_int condCoding;
263
264
321k
                        if( decControl->nChannelsInternal == 2 && n == 0 ) {
265
138k
                            silk_stereo_decode_pred( psRangeDec, MS_pred_Q13 );
266
138k
                            if( channel_state[ 1 ].LBRR_flags[ i ] == 0 ) {
267
58.3k
                                silk_stereo_decode_mid_only( psRangeDec, &decode_only_middle );
268
58.3k
                            }
269
138k
                        }
270
                        /* Use conditional coding if previous frame available */
271
321k
                        if( i > 0 && channel_state[ n ].LBRR_flags[ i - 1 ] ) {
272
104k
                            condCoding = CODE_CONDITIONALLY;
273
216k
                        } else {
274
216k
                            condCoding = CODE_INDEPENDENTLY;
275
216k
                        }
276
321k
                        silk_decode_indices( &channel_state[ n ], psRangeDec, i, 1, condCoding );
277
321k
                        silk_decode_pulses( psRangeDec, pulses, channel_state[ n ].indices.signalType,
278
321k
                            channel_state[ n ].indices.quantOffsetType, channel_state[ n ].frame_length );
279
321k
                    }
280
907k
                }
281
532k
            }
282
320k
        }
283
396k
    }
284
285
    /* Get MS predictor index */
286
877k
    if( decControl->nChannelsInternal == 2 ) {
287
625k
        if(   lostFlag == FLAG_DECODE_NORMAL ||
288
250k
            ( lostFlag == FLAG_DECODE_LBRR && channel_state[ 0 ].LBRR_flags[ channel_state[ 0 ].nFramesDecoded ] == 1 ) )
289
419k
        {
290
419k
            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
419k
            if( ( lostFlag == FLAG_DECODE_NORMAL && channel_state[ 1 ].VAD_flags[ channel_state[ 0 ].nFramesDecoded ] == 0 ) ||
293
237k
                ( lostFlag == FLAG_DECODE_LBRR && channel_state[ 1 ].LBRR_flags[ channel_state[ 0 ].nFramesDecoded ] == 0 ) )
294
206k
            {
295
206k
                silk_stereo_decode_mid_only( psRangeDec, &decode_only_middle );
296
213k
            } else {
297
213k
                decode_only_middle = 0;
298
213k
            }
299
419k
        } else {
300
617k
            for( n = 0; n < 2; n++ ) {
301
411k
                MS_pred_Q13[ n ] = psDec->sStereo.pred_prev_Q13[ n ];
302
411k
            }
303
205k
        }
304
625k
    }
305
306
    /* Reset side channel decoder prediction memory for first frame with side coding */
307
877k
    if( decControl->nChannelsInternal == 2 && decode_only_middle == 0 && psDec->prev_decode_only_middle == 1 ) {
308
45.5k
        silk_memset( psDec->channel_state[ 1 ].outBuf, 0, sizeof(psDec->channel_state[ 1 ].outBuf) );
309
45.5k
        silk_memset( psDec->channel_state[ 1 ].sLPC_Q14_buf, 0, sizeof(psDec->channel_state[ 1 ].sLPC_Q14_buf) );
310
45.5k
        psDec->channel_state[ 1 ].lagPrev        = 100;
311
45.5k
        psDec->channel_state[ 1 ].LastGainIndex  = 10;
312
45.5k
        psDec->channel_state[ 1 ].prevSignalType = TYPE_NO_VOICE_ACTIVITY;
313
45.5k
        psDec->channel_state[ 1 ].first_frame_after_reset = 1;
314
45.5k
    }
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
877k
    ALLOC( samplesOut1_tmp_storage1, decControl->nChannelsInternal*(channel_state[ 0 ].frame_length + 2 ),
320
877k
           opus_int16 );
321
877k
    samplesOut1_tmp[ 0 ] = samplesOut1_tmp_storage1;
322
877k
    samplesOut1_tmp[ 1 ] = samplesOut1_tmp_storage1 + channel_state[ 0 ].frame_length + 2;
323
324
877k
    if( lostFlag == FLAG_DECODE_NORMAL ) {
325
532k
        has_side = !decode_only_middle;
326
532k
    } else {
327
344k
        has_side = !psDec->prev_decode_only_middle
328
26.0k
              || (decControl->nChannelsInternal == 2 && lostFlag == FLAG_DECODE_LBRR && channel_state[1].LBRR_flags[ channel_state[1].nFramesDecoded ] == 1 );
329
344k
    }
330
877k
    channel_state[ 0 ].sPLC.enable_deep_plc = decControl->enable_deep_plc;
331
    /* Call decoder for one frame */
332
2.37M
    for( n = 0; n < decControl->nChannelsInternal; n++ ) {
333
1.50M
        if( n == 0 || has_side ) {
334
1.44M
            opus_int FrameIndex;
335
1.44M
            opus_int condCoding;
336
337
1.44M
            FrameIndex = channel_state[ 0 ].nFramesDecoded - n;
338
            /* Use independent coding if no previous frame available */
339
1.44M
            if( FrameIndex <= 0 ) {
340
918k
                condCoding = CODE_INDEPENDENTLY;
341
918k
            } else if( lostFlag == FLAG_DECODE_LBRR ) {
342
146k
                condCoding = channel_state[ n ].LBRR_flags[ FrameIndex - 1 ] ? CODE_CONDITIONALLY : CODE_INDEPENDENTLY;
343
378k
            } 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
13.6k
                condCoding = CODE_INDEPENDENTLY_NO_LTP_SCALING;
347
364k
            } else {
348
364k
                condCoding = CODE_CONDITIONALLY;
349
364k
            }
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.44M
            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.44M
                arch);
363
1.44M
        } else {
364
58.3k
            silk_memset( &samplesOut1_tmp[ n ][ 2 ], 0, nSamplesOutDec * sizeof( opus_int16 ) );
365
58.3k
        }
366
1.50M
        channel_state[ n ].nFramesDecoded++;
367
1.50M
    }
368
369
877k
    if( decControl->nChannelsAPI == 2 && decControl->nChannelsInternal == 2 ) {
370
        /* Convert Mid/Side to Left/Right */
371
228k
        silk_stereo_MS_to_LR( &psDec->sStereo, samplesOut1_tmp[ 0 ], samplesOut1_tmp[ 1 ], MS_pred_Q13, channel_state[ 0 ].fs_kHz, nSamplesOutDec );
372
649k
    } else {
373
        /* Buffering */
374
649k
        silk_memcpy( samplesOut1_tmp[ 0 ], psDec->sStereo.sMid, 2 * sizeof( opus_int16 ) );
375
649k
        silk_memcpy( psDec->sStereo.sMid, &samplesOut1_tmp[ 0 ][ nSamplesOutDec ], 2 * sizeof( opus_int16 ) );
376
649k
    }
377
378
    /* Number of output samples */
379
877k
    *nSamplesOut = silk_DIV32( nSamplesOutDec * decControl->API_sampleRate, silk_SMULBB( channel_state[ 0 ].fs_kHz, 1000 ) );
380
381
    /* Set up pointers to temp buffers */
382
877k
    ALLOC( samplesOut2_tmp, *nSamplesOut, opus_int16 );
383
877k
    resample_out_ptr = samplesOut2_tmp;
384
385
#ifdef ENABLE_OSCE_BWE
386
    ALLOC(resamp_buffer, 3 * MAX_FRAME_LENGTH, opus_int16);
387
#endif
388
389
1.98M
    for( n = 0; n < silk_min( decControl->nChannelsAPI, decControl->nChannelsInternal ); n++ ) {
390
391
#ifdef ENABLE_OSCE_BWE
392
        /* Resample or extend decoded signal to API_sampleRate */
393
        if (decControl->osce_extended_mode == OSCE_MODE_SILK_BBWE) {
394
            silk_assert(decControl->API_sampleRate == 48000);
395
396
            if (decControl->prev_osce_extended_mode != OSCE_MODE_SILK_BBWE) {
397
                /* Reset the BWE state */
398
                osce_bwe_reset( &channel_state[ n ].osce_bwe );
399
            }
400
401
            osce_bwe(&psDec->osce_model, &channel_state[ n ].osce_bwe,
402
                resample_out_ptr, &samplesOut1_tmp[ n ][ 1 ], nSamplesOutDec, arch);
403
404
            if (decControl->prev_osce_extended_mode == OSCE_MODE_SILK_ONLY ||
405
                decControl->prev_osce_extended_mode == OSCE_MODE_HYBRID) {
406
                    /* cross-fade with upsampled signal */
407
                    silk_resampler( &channel_state[ n ].resampler_state, resamp_buffer, &samplesOut1_tmp[ n ][ 1 ], nSamplesOutDec );
408
                    osce_bwe_cross_fade_10ms(resample_out_ptr, resamp_buffer, 480);
409
            }
410
        } else {
411
            ret += silk_resampler( &channel_state[ n ].resampler_state, resample_out_ptr, &samplesOut1_tmp[ n ][ 1 ], nSamplesOutDec );
412
            if (decControl->prev_osce_extended_mode == OSCE_MODE_SILK_BBWE && decControl->internalSampleRate == 16000) {
413
                /* fade out if internal sample rate did not change */
414
                osce_bwe(&psDec->osce_model, &channel_state[ n ].osce_bwe,
415
                    resamp_buffer, &samplesOut1_tmp[ n ][ 1 ], nSamplesOutDec, arch);
416
                /* cross-fade with upsampled signal */
417
                osce_bwe_cross_fade_10ms(resample_out_ptr, resamp_buffer, 480);
418
            }
419
        }
420
#else
421
        /* Resample decoded signal to API_sampleRate */
422
1.10M
        ret += silk_resampler( &channel_state[ n ].resampler_state, resample_out_ptr, &samplesOut1_tmp[ n ][ 1 ], nSamplesOutDec );
423
1.10M
#endif
424
        /* Interleave if stereo output and stereo stream */
425
1.10M
        if( decControl->nChannelsAPI == 2 ) {
426
247M
            for( i = 0; i < *nSamplesOut; i++ ) {
427
247M
                samplesOut[ n + 2 * i ] = INT16TORES(resample_out_ptr[ i ]);
428
247M
            }
429
573k
        } else {
430
212M
            for( i = 0; i < *nSamplesOut; i++ ) {
431
211M
                samplesOut[ i ] = INT16TORES(resample_out_ptr[ i ]);
432
211M
            }
433
531k
        }
434
1.10M
    }
435
436
#ifdef ENABLE_OSCE_BWE
437
    decControl->prev_osce_extended_mode = decControl->osce_extended_mode;
438
#endif
439
440
    /* Create two channel output from mono stream */
441
877k
    if( decControl->nChannelsAPI == 2 && decControl->nChannelsInternal == 1 ) {
442
117k
        if ( stereo_to_mono ){
443
            /* Resample right channel for newly collapsed stereo just in case
444
               we weren't doing collapsing when switching to mono */
445
0
            ret += silk_resampler( &channel_state[ 1 ].resampler_state, resample_out_ptr, &samplesOut1_tmp[ 0 ][ 1 ], nSamplesOutDec );
446
447
0
            for( i = 0; i < *nSamplesOut; i++ ) {
448
0
                samplesOut[ 1 + 2 * i ] = INT16TORES(resample_out_ptr[ i ]);
449
0
            }
450
117k
        } else {
451
51.2M
            for( i = 0; i < *nSamplesOut; i++ ) {
452
51.1M
                samplesOut[ 1 + 2 * i ] = samplesOut[ 0 + 2 * i ];
453
51.1M
            }
454
117k
        }
455
117k
    }
456
457
    /* Export pitch lag, measured at 48 kHz sampling rate */
458
877k
    if( channel_state[ 0 ].prevSignalType == TYPE_VOICED ) {
459
208k
        int mult_tab[ 3 ] = { 6, 4, 3 };
460
208k
        decControl->prevPitchLag = channel_state[ 0 ].lagPrev * mult_tab[ ( channel_state[ 0 ].fs_kHz - 8 ) >> 2 ];
461
669k
    } else {
462
669k
        decControl->prevPitchLag = 0;
463
669k
    }
464
465
877k
    if( lostFlag == FLAG_PACKET_LOST ) {
466
       /* On packet loss, remove the gain clamping to prevent having the energy "bounce back"
467
          if we lose packets when the energy is going down */
468
486k
       for ( i = 0; i < psDec->nChannelsInternal; i++ )
469
314k
          psDec->channel_state[ i ].LastGainIndex = 10;
470
705k
    } else {
471
705k
       psDec->prev_decode_only_middle = decode_only_middle;
472
705k
    }
473
877k
    RESTORE_STACK;
474
877k
    return ret;
475
877k
}
silk_Decode
Line
Count
Source
148
438k
{
149
438k
    opus_int   i, n, decode_only_middle = 0, ret = SILK_NO_ERROR;
150
438k
    opus_int32 nSamplesOutDec, LBRR_symbol;
151
438k
    opus_int16 *samplesOut1_tmp[ 2 ];
152
438k
    VARDECL( opus_int16, samplesOut1_tmp_storage1 );
153
438k
    VARDECL( opus_int16, samplesOut2_tmp );
154
438k
    opus_int32 MS_pred_Q13[ 2 ] = { 0 };
155
438k
    opus_int16 *resample_out_ptr;
156
438k
    silk_decoder *psDec = ( silk_decoder * )decState;
157
438k
    silk_decoder_state *channel_state = psDec->channel_state;
158
438k
    opus_int has_side;
159
438k
    opus_int stereo_to_mono;
160
#ifdef ENABLE_OSCE_BWE
161
    VARDECL( opus_int16, resamp_buffer );
162
#endif
163
438k
    SAVE_STACK;
164
165
438k
    celt_assert( decControl->nChannelsInternal == 1 || decControl->nChannelsInternal == 2 );
166
167
    /**********************************/
168
    /* Test if first frame in payload */
169
    /**********************************/
170
438k
    if( newPacketFlag ) {
171
758k
        for( n = 0; n < decControl->nChannelsInternal; n++ ) {
172
474k
            channel_state[ n ].nFramesDecoded = 0;  /* Used to count frames in packet */
173
474k
        }
174
283k
    }
175
176
    /* If Mono -> Stereo transition in bitstream: init state of second channel */
177
438k
    if( decControl->nChannelsInternal > psDec->nChannelsInternal ) {
178
139k
        ret += silk_init_decoder( &channel_state[ 1 ] );
179
139k
    }
180
181
438k
    stereo_to_mono = decControl->nChannelsInternal == 1 && psDec->nChannelsInternal == 2 &&
182
0
                     ( decControl->internalSampleRate == 1000*channel_state[ 0 ].fs_kHz );
183
184
438k
    if( channel_state[ 0 ].nFramesDecoded == 0 ) {
185
758k
        for( n = 0; n < decControl->nChannelsInternal; n++ ) {
186
474k
            opus_int fs_kHz_dec;
187
474k
            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
474k
            } else if( decControl->payloadSize_ms == 10 ) {
192
227k
                channel_state[ n ].nFramesPerPacket = 1;
193
227k
                channel_state[ n ].nb_subfr = 2;
194
247k
            } else if( decControl->payloadSize_ms == 20 ) {
195
102k
                channel_state[ n ].nFramesPerPacket = 1;
196
102k
                channel_state[ n ].nb_subfr = 4;
197
144k
            } else if( decControl->payloadSize_ms == 40 ) {
198
13.7k
                channel_state[ n ].nFramesPerPacket = 2;
199
13.7k
                channel_state[ n ].nb_subfr = 4;
200
131k
            } else if( decControl->payloadSize_ms == 60 ) {
201
131k
                channel_state[ n ].nFramesPerPacket = 3;
202
131k
                channel_state[ n ].nb_subfr = 4;
203
131k
            } else {
204
0
                celt_assert( 0 );
205
0
                RESTORE_STACK;
206
0
                return SILK_DEC_INVALID_FRAME_SIZE;
207
0
            }
208
474k
            fs_kHz_dec = ( decControl->internalSampleRate >> 10 ) + 1;
209
474k
            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
474k
            ret += silk_decoder_set_fs( &channel_state[ n ], fs_kHz_dec, decControl->API_sampleRate );
215
474k
        }
216
283k
    }
217
218
438k
    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
438k
    psDec->nChannelsAPI      = decControl->nChannelsAPI;
224
438k
    psDec->nChannelsInternal = decControl->nChannelsInternal;
225
226
438k
    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
438k
    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
515k
        for( n = 0; n < decControl->nChannelsInternal; n++ ) {
236
911k
            for( i = 0; i < channel_state[ n ].nFramesPerPacket; i++ ) {
237
593k
                channel_state[ n ].VAD_flags[ i ] = ec_dec_bit_logp(psRangeDec, 1);
238
593k
            }
239
317k
            channel_state[ n ].LBRR_flag = ec_dec_bit_logp(psRangeDec, 1);
240
317k
        }
241
        /* Decode LBRR flags */
242
515k
        for( n = 0; n < decControl->nChannelsInternal; n++ ) {
243
317k
            silk_memset( channel_state[ n ].LBRR_flags, 0, sizeof( channel_state[ n ].LBRR_flags ) );
244
317k
            if( channel_state[ n ].LBRR_flag ) {
245
146k
                if( channel_state[ n ].nFramesPerPacket == 1 ) {
246
56.5k
                    channel_state[ n ].LBRR_flags[ 0 ] = 1;
247
89.5k
                } else {
248
89.5k
                    LBRR_symbol = ec_dec_icdf( psRangeDec, silk_LBRR_flags_iCDF_ptr[ channel_state[ n ].nFramesPerPacket - 2 ], 8 ) + 1;
249
350k
                    for( i = 0; i < channel_state[ n ].nFramesPerPacket; i++ ) {
250
260k
                        channel_state[ n ].LBRR_flags[ i ] = silk_RSHIFT( LBRR_symbol, i ) & 1;
251
260k
                    }
252
89.5k
                }
253
146k
            }
254
317k
        }
255
256
198k
        if( lostFlag == FLAG_DECODE_NORMAL ) {
257
            /* Regular decoding: skip all LBRR data */
258
426k
            for( i = 0; i < channel_state[ 0 ].nFramesPerPacket; i++ ) {
259
720k
                for( n = 0; n < decControl->nChannelsInternal; n++ ) {
260
453k
                    if( channel_state[ n ].LBRR_flags[ i ] ) {
261
160k
                        opus_int16 pulses[ MAX_FRAME_LENGTH ];
262
160k
                        opus_int condCoding;
263
264
160k
                        if( decControl->nChannelsInternal == 2 && n == 0 ) {
265
69.4k
                            silk_stereo_decode_pred( psRangeDec, MS_pred_Q13 );
266
69.4k
                            if( channel_state[ 1 ].LBRR_flags[ i ] == 0 ) {
267
29.1k
                                silk_stereo_decode_mid_only( psRangeDec, &decode_only_middle );
268
29.1k
                            }
269
69.4k
                        }
270
                        /* Use conditional coding if previous frame available */
271
160k
                        if( i > 0 && channel_state[ n ].LBRR_flags[ i - 1 ] ) {
272
52.2k
                            condCoding = CODE_CONDITIONALLY;
273
108k
                        } else {
274
108k
                            condCoding = CODE_INDEPENDENTLY;
275
108k
                        }
276
160k
                        silk_decode_indices( &channel_state[ n ], psRangeDec, i, 1, condCoding );
277
160k
                        silk_decode_pulses( psRangeDec, pulses, channel_state[ n ].indices.signalType,
278
160k
                            channel_state[ n ].indices.quantOffsetType, channel_state[ n ].frame_length );
279
160k
                    }
280
453k
                }
281
266k
            }
282
160k
        }
283
198k
    }
284
285
    /* Get MS predictor index */
286
438k
    if( decControl->nChannelsInternal == 2 ) {
287
312k
        if(   lostFlag == FLAG_DECODE_NORMAL ||
288
125k
            ( lostFlag == FLAG_DECODE_LBRR && channel_state[ 0 ].LBRR_flags[ channel_state[ 0 ].nFramesDecoded ] == 1 ) )
289
209k
        {
290
209k
            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
209k
            if( ( lostFlag == FLAG_DECODE_NORMAL && channel_state[ 1 ].VAD_flags[ channel_state[ 0 ].nFramesDecoded ] == 0 ) ||
293
118k
                ( lostFlag == FLAG_DECODE_LBRR && channel_state[ 1 ].LBRR_flags[ channel_state[ 0 ].nFramesDecoded ] == 0 ) )
294
103k
            {
295
103k
                silk_stereo_decode_mid_only( psRangeDec, &decode_only_middle );
296
106k
            } else {
297
106k
                decode_only_middle = 0;
298
106k
            }
299
209k
        } else {
300
308k
            for( n = 0; n < 2; n++ ) {
301
205k
                MS_pred_Q13[ n ] = psDec->sStereo.pred_prev_Q13[ n ];
302
205k
            }
303
102k
        }
304
312k
    }
305
306
    /* Reset side channel decoder prediction memory for first frame with side coding */
307
438k
    if( decControl->nChannelsInternal == 2 && decode_only_middle == 0 && psDec->prev_decode_only_middle == 1 ) {
308
22.7k
        silk_memset( psDec->channel_state[ 1 ].outBuf, 0, sizeof(psDec->channel_state[ 1 ].outBuf) );
309
22.7k
        silk_memset( psDec->channel_state[ 1 ].sLPC_Q14_buf, 0, sizeof(psDec->channel_state[ 1 ].sLPC_Q14_buf) );
310
22.7k
        psDec->channel_state[ 1 ].lagPrev        = 100;
311
22.7k
        psDec->channel_state[ 1 ].LastGainIndex  = 10;
312
22.7k
        psDec->channel_state[ 1 ].prevSignalType = TYPE_NO_VOICE_ACTIVITY;
313
22.7k
        psDec->channel_state[ 1 ].first_frame_after_reset = 1;
314
22.7k
    }
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
438k
    ALLOC( samplesOut1_tmp_storage1, decControl->nChannelsInternal*(channel_state[ 0 ].frame_length + 2 ),
320
438k
           opus_int16 );
321
438k
    samplesOut1_tmp[ 0 ] = samplesOut1_tmp_storage1;
322
438k
    samplesOut1_tmp[ 1 ] = samplesOut1_tmp_storage1 + channel_state[ 0 ].frame_length + 2;
323
324
438k
    if( lostFlag == FLAG_DECODE_NORMAL ) {
325
266k
        has_side = !decode_only_middle;
326
266k
    } else {
327
172k
        has_side = !psDec->prev_decode_only_middle
328
13.0k
              || (decControl->nChannelsInternal == 2 && lostFlag == FLAG_DECODE_LBRR && channel_state[1].LBRR_flags[ channel_state[1].nFramesDecoded ] == 1 );
329
172k
    }
330
438k
    channel_state[ 0 ].sPLC.enable_deep_plc = decControl->enable_deep_plc;
331
    /* Call decoder for one frame */
332
1.18M
    for( n = 0; n < decControl->nChannelsInternal; n++ ) {
333
751k
        if( n == 0 || has_side ) {
334
722k
            opus_int FrameIndex;
335
722k
            opus_int condCoding;
336
337
722k
            FrameIndex = channel_state[ 0 ].nFramesDecoded - n;
338
            /* Use independent coding if no previous frame available */
339
722k
            if( FrameIndex <= 0 ) {
340
459k
                condCoding = CODE_INDEPENDENTLY;
341
459k
            } else if( lostFlag == FLAG_DECODE_LBRR ) {
342
73.4k
                condCoding = channel_state[ n ].LBRR_flags[ FrameIndex - 1 ] ? CODE_CONDITIONALLY : CODE_INDEPENDENTLY;
343
189k
            } 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
6.83k
                condCoding = CODE_INDEPENDENTLY_NO_LTP_SCALING;
347
182k
            } else {
348
182k
                condCoding = CODE_CONDITIONALLY;
349
182k
            }
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
722k
            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
722k
                arch);
363
722k
        } else {
364
29.1k
            silk_memset( &samplesOut1_tmp[ n ][ 2 ], 0, nSamplesOutDec * sizeof( opus_int16 ) );
365
29.1k
        }
366
751k
        channel_state[ n ].nFramesDecoded++;
367
751k
    }
368
369
438k
    if( decControl->nChannelsAPI == 2 && decControl->nChannelsInternal == 2 ) {
370
        /* Convert Mid/Side to Left/Right */
371
114k
        silk_stereo_MS_to_LR( &psDec->sStereo, samplesOut1_tmp[ 0 ], samplesOut1_tmp[ 1 ], MS_pred_Q13, channel_state[ 0 ].fs_kHz, nSamplesOutDec );
372
324k
    } else {
373
        /* Buffering */
374
324k
        silk_memcpy( samplesOut1_tmp[ 0 ], psDec->sStereo.sMid, 2 * sizeof( opus_int16 ) );
375
324k
        silk_memcpy( psDec->sStereo.sMid, &samplesOut1_tmp[ 0 ][ nSamplesOutDec ], 2 * sizeof( opus_int16 ) );
376
324k
    }
377
378
    /* Number of output samples */
379
438k
    *nSamplesOut = silk_DIV32( nSamplesOutDec * decControl->API_sampleRate, silk_SMULBB( channel_state[ 0 ].fs_kHz, 1000 ) );
380
381
    /* Set up pointers to temp buffers */
382
438k
    ALLOC( samplesOut2_tmp, *nSamplesOut, opus_int16 );
383
438k
    resample_out_ptr = samplesOut2_tmp;
384
385
#ifdef ENABLE_OSCE_BWE
386
    ALLOC(resamp_buffer, 3 * MAX_FRAME_LENGTH, opus_int16);
387
#endif
388
389
991k
    for( n = 0; n < silk_min( decControl->nChannelsAPI, decControl->nChannelsInternal ); n++ ) {
390
391
#ifdef ENABLE_OSCE_BWE
392
        /* Resample or extend decoded signal to API_sampleRate */
393
        if (decControl->osce_extended_mode == OSCE_MODE_SILK_BBWE) {
394
            silk_assert(decControl->API_sampleRate == 48000);
395
396
            if (decControl->prev_osce_extended_mode != OSCE_MODE_SILK_BBWE) {
397
                /* Reset the BWE state */
398
                osce_bwe_reset( &channel_state[ n ].osce_bwe );
399
            }
400
401
            osce_bwe(&psDec->osce_model, &channel_state[ n ].osce_bwe,
402
                resample_out_ptr, &samplesOut1_tmp[ n ][ 1 ], nSamplesOutDec, arch);
403
404
            if (decControl->prev_osce_extended_mode == OSCE_MODE_SILK_ONLY ||
405
                decControl->prev_osce_extended_mode == OSCE_MODE_HYBRID) {
406
                    /* cross-fade with upsampled signal */
407
                    silk_resampler( &channel_state[ n ].resampler_state, resamp_buffer, &samplesOut1_tmp[ n ][ 1 ], nSamplesOutDec );
408
                    osce_bwe_cross_fade_10ms(resample_out_ptr, resamp_buffer, 480);
409
            }
410
        } else {
411
            ret += silk_resampler( &channel_state[ n ].resampler_state, resample_out_ptr, &samplesOut1_tmp[ n ][ 1 ], nSamplesOutDec );
412
            if (decControl->prev_osce_extended_mode == OSCE_MODE_SILK_BBWE && decControl->internalSampleRate == 16000) {
413
                /* fade out if internal sample rate did not change */
414
                osce_bwe(&psDec->osce_model, &channel_state[ n ].osce_bwe,
415
                    resamp_buffer, &samplesOut1_tmp[ n ][ 1 ], nSamplesOutDec, arch);
416
                /* cross-fade with upsampled signal */
417
                osce_bwe_cross_fade_10ms(resample_out_ptr, resamp_buffer, 480);
418
            }
419
        }
420
#else
421
        /* Resample decoded signal to API_sampleRate */
422
552k
        ret += silk_resampler( &channel_state[ n ].resampler_state, resample_out_ptr, &samplesOut1_tmp[ n ][ 1 ], nSamplesOutDec );
423
552k
#endif
424
        /* Interleave if stereo output and stereo stream */
425
552k
        if( decControl->nChannelsAPI == 2 ) {
426
123M
            for( i = 0; i < *nSamplesOut; i++ ) {
427
123M
                samplesOut[ n + 2 * i ] = INT16TORES(resample_out_ptr[ i ]);
428
123M
            }
429
286k
        } else {
430
106M
            for( i = 0; i < *nSamplesOut; i++ ) {
431
105M
                samplesOut[ i ] = INT16TORES(resample_out_ptr[ i ]);
432
105M
            }
433
265k
        }
434
552k
    }
435
436
#ifdef ENABLE_OSCE_BWE
437
    decControl->prev_osce_extended_mode = decControl->osce_extended_mode;
438
#endif
439
440
    /* Create two channel output from mono stream */
441
438k
    if( decControl->nChannelsAPI == 2 && decControl->nChannelsInternal == 1 ) {
442
58.8k
        if ( stereo_to_mono ){
443
            /* Resample right channel for newly collapsed stereo just in case
444
               we weren't doing collapsing when switching to mono */
445
0
            ret += silk_resampler( &channel_state[ 1 ].resampler_state, resample_out_ptr, &samplesOut1_tmp[ 0 ][ 1 ], nSamplesOutDec );
446
447
0
            for( i = 0; i < *nSamplesOut; i++ ) {
448
0
                samplesOut[ 1 + 2 * i ] = INT16TORES(resample_out_ptr[ i ]);
449
0
            }
450
58.8k
        } else {
451
25.6M
            for( i = 0; i < *nSamplesOut; i++ ) {
452
25.5M
                samplesOut[ 1 + 2 * i ] = samplesOut[ 0 + 2 * i ];
453
25.5M
            }
454
58.8k
        }
455
58.8k
    }
456
457
    /* Export pitch lag, measured at 48 kHz sampling rate */
458
438k
    if( channel_state[ 0 ].prevSignalType == TYPE_VOICED ) {
459
104k
        int mult_tab[ 3 ] = { 6, 4, 3 };
460
104k
        decControl->prevPitchLag = channel_state[ 0 ].lagPrev * mult_tab[ ( channel_state[ 0 ].fs_kHz - 8 ) >> 2 ];
461
334k
    } else {
462
334k
        decControl->prevPitchLag = 0;
463
334k
    }
464
465
438k
    if( lostFlag == FLAG_PACKET_LOST ) {
466
       /* On packet loss, remove the gain clamping to prevent having the energy "bounce back"
467
          if we lose packets when the energy is going down */
468
243k
       for ( i = 0; i < psDec->nChannelsInternal; i++ )
469
157k
          psDec->channel_state[ i ].LastGainIndex = 10;
470
352k
    } else {
471
352k
       psDec->prev_decode_only_middle = decode_only_middle;
472
352k
    }
473
438k
    RESTORE_STACK;
474
438k
    return ret;
475
438k
}
silk_Decode
Line
Count
Source
148
438k
{
149
438k
    opus_int   i, n, decode_only_middle = 0, ret = SILK_NO_ERROR;
150
438k
    opus_int32 nSamplesOutDec, LBRR_symbol;
151
438k
    opus_int16 *samplesOut1_tmp[ 2 ];
152
438k
    VARDECL( opus_int16, samplesOut1_tmp_storage1 );
153
438k
    VARDECL( opus_int16, samplesOut2_tmp );
154
438k
    opus_int32 MS_pred_Q13[ 2 ] = { 0 };
155
438k
    opus_int16 *resample_out_ptr;
156
438k
    silk_decoder *psDec = ( silk_decoder * )decState;
157
438k
    silk_decoder_state *channel_state = psDec->channel_state;
158
438k
    opus_int has_side;
159
438k
    opus_int stereo_to_mono;
160
#ifdef ENABLE_OSCE_BWE
161
    VARDECL( opus_int16, resamp_buffer );
162
#endif
163
438k
    SAVE_STACK;
164
165
438k
    celt_assert( decControl->nChannelsInternal == 1 || decControl->nChannelsInternal == 2 );
166
167
    /**********************************/
168
    /* Test if first frame in payload */
169
    /**********************************/
170
438k
    if( newPacketFlag ) {
171
758k
        for( n = 0; n < decControl->nChannelsInternal; n++ ) {
172
474k
            channel_state[ n ].nFramesDecoded = 0;  /* Used to count frames in packet */
173
474k
        }
174
283k
    }
175
176
    /* If Mono -> Stereo transition in bitstream: init state of second channel */
177
438k
    if( decControl->nChannelsInternal > psDec->nChannelsInternal ) {
178
139k
        ret += silk_init_decoder( &channel_state[ 1 ] );
179
139k
    }
180
181
438k
    stereo_to_mono = decControl->nChannelsInternal == 1 && psDec->nChannelsInternal == 2 &&
182
0
                     ( decControl->internalSampleRate == 1000*channel_state[ 0 ].fs_kHz );
183
184
438k
    if( channel_state[ 0 ].nFramesDecoded == 0 ) {
185
758k
        for( n = 0; n < decControl->nChannelsInternal; n++ ) {
186
474k
            opus_int fs_kHz_dec;
187
474k
            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
474k
            } else if( decControl->payloadSize_ms == 10 ) {
192
227k
                channel_state[ n ].nFramesPerPacket = 1;
193
227k
                channel_state[ n ].nb_subfr = 2;
194
247k
            } else if( decControl->payloadSize_ms == 20 ) {
195
102k
                channel_state[ n ].nFramesPerPacket = 1;
196
102k
                channel_state[ n ].nb_subfr = 4;
197
144k
            } else if( decControl->payloadSize_ms == 40 ) {
198
13.7k
                channel_state[ n ].nFramesPerPacket = 2;
199
13.7k
                channel_state[ n ].nb_subfr = 4;
200
131k
            } else if( decControl->payloadSize_ms == 60 ) {
201
131k
                channel_state[ n ].nFramesPerPacket = 3;
202
131k
                channel_state[ n ].nb_subfr = 4;
203
131k
            } else {
204
0
                celt_assert( 0 );
205
0
                RESTORE_STACK;
206
0
                return SILK_DEC_INVALID_FRAME_SIZE;
207
0
            }
208
474k
            fs_kHz_dec = ( decControl->internalSampleRate >> 10 ) + 1;
209
474k
            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
474k
            ret += silk_decoder_set_fs( &channel_state[ n ], fs_kHz_dec, decControl->API_sampleRate );
215
474k
        }
216
283k
    }
217
218
438k
    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
438k
    psDec->nChannelsAPI      = decControl->nChannelsAPI;
224
438k
    psDec->nChannelsInternal = decControl->nChannelsInternal;
225
226
438k
    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
438k
    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
515k
        for( n = 0; n < decControl->nChannelsInternal; n++ ) {
236
911k
            for( i = 0; i < channel_state[ n ].nFramesPerPacket; i++ ) {
237
593k
                channel_state[ n ].VAD_flags[ i ] = ec_dec_bit_logp(psRangeDec, 1);
238
593k
            }
239
317k
            channel_state[ n ].LBRR_flag = ec_dec_bit_logp(psRangeDec, 1);
240
317k
        }
241
        /* Decode LBRR flags */
242
515k
        for( n = 0; n < decControl->nChannelsInternal; n++ ) {
243
317k
            silk_memset( channel_state[ n ].LBRR_flags, 0, sizeof( channel_state[ n ].LBRR_flags ) );
244
317k
            if( channel_state[ n ].LBRR_flag ) {
245
146k
                if( channel_state[ n ].nFramesPerPacket == 1 ) {
246
56.5k
                    channel_state[ n ].LBRR_flags[ 0 ] = 1;
247
89.5k
                } else {
248
89.5k
                    LBRR_symbol = ec_dec_icdf( psRangeDec, silk_LBRR_flags_iCDF_ptr[ channel_state[ n ].nFramesPerPacket - 2 ], 8 ) + 1;
249
350k
                    for( i = 0; i < channel_state[ n ].nFramesPerPacket; i++ ) {
250
260k
                        channel_state[ n ].LBRR_flags[ i ] = silk_RSHIFT( LBRR_symbol, i ) & 1;
251
260k
                    }
252
89.5k
                }
253
146k
            }
254
317k
        }
255
256
198k
        if( lostFlag == FLAG_DECODE_NORMAL ) {
257
            /* Regular decoding: skip all LBRR data */
258
426k
            for( i = 0; i < channel_state[ 0 ].nFramesPerPacket; i++ ) {
259
720k
                for( n = 0; n < decControl->nChannelsInternal; n++ ) {
260
453k
                    if( channel_state[ n ].LBRR_flags[ i ] ) {
261
160k
                        opus_int16 pulses[ MAX_FRAME_LENGTH ];
262
160k
                        opus_int condCoding;
263
264
160k
                        if( decControl->nChannelsInternal == 2 && n == 0 ) {
265
69.4k
                            silk_stereo_decode_pred( psRangeDec, MS_pred_Q13 );
266
69.4k
                            if( channel_state[ 1 ].LBRR_flags[ i ] == 0 ) {
267
29.1k
                                silk_stereo_decode_mid_only( psRangeDec, &decode_only_middle );
268
29.1k
                            }
269
69.4k
                        }
270
                        /* Use conditional coding if previous frame available */
271
160k
                        if( i > 0 && channel_state[ n ].LBRR_flags[ i - 1 ] ) {
272
52.2k
                            condCoding = CODE_CONDITIONALLY;
273
108k
                        } else {
274
108k
                            condCoding = CODE_INDEPENDENTLY;
275
108k
                        }
276
160k
                        silk_decode_indices( &channel_state[ n ], psRangeDec, i, 1, condCoding );
277
160k
                        silk_decode_pulses( psRangeDec, pulses, channel_state[ n ].indices.signalType,
278
160k
                            channel_state[ n ].indices.quantOffsetType, channel_state[ n ].frame_length );
279
160k
                    }
280
453k
                }
281
266k
            }
282
160k
        }
283
198k
    }
284
285
    /* Get MS predictor index */
286
438k
    if( decControl->nChannelsInternal == 2 ) {
287
312k
        if(   lostFlag == FLAG_DECODE_NORMAL ||
288
125k
            ( lostFlag == FLAG_DECODE_LBRR && channel_state[ 0 ].LBRR_flags[ channel_state[ 0 ].nFramesDecoded ] == 1 ) )
289
209k
        {
290
209k
            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
209k
            if( ( lostFlag == FLAG_DECODE_NORMAL && channel_state[ 1 ].VAD_flags[ channel_state[ 0 ].nFramesDecoded ] == 0 ) ||
293
118k
                ( lostFlag == FLAG_DECODE_LBRR && channel_state[ 1 ].LBRR_flags[ channel_state[ 0 ].nFramesDecoded ] == 0 ) )
294
103k
            {
295
103k
                silk_stereo_decode_mid_only( psRangeDec, &decode_only_middle );
296
106k
            } else {
297
106k
                decode_only_middle = 0;
298
106k
            }
299
209k
        } else {
300
308k
            for( n = 0; n < 2; n++ ) {
301
205k
                MS_pred_Q13[ n ] = psDec->sStereo.pred_prev_Q13[ n ];
302
205k
            }
303
102k
        }
304
312k
    }
305
306
    /* Reset side channel decoder prediction memory for first frame with side coding */
307
438k
    if( decControl->nChannelsInternal == 2 && decode_only_middle == 0 && psDec->prev_decode_only_middle == 1 ) {
308
22.7k
        silk_memset( psDec->channel_state[ 1 ].outBuf, 0, sizeof(psDec->channel_state[ 1 ].outBuf) );
309
22.7k
        silk_memset( psDec->channel_state[ 1 ].sLPC_Q14_buf, 0, sizeof(psDec->channel_state[ 1 ].sLPC_Q14_buf) );
310
22.7k
        psDec->channel_state[ 1 ].lagPrev        = 100;
311
22.7k
        psDec->channel_state[ 1 ].LastGainIndex  = 10;
312
22.7k
        psDec->channel_state[ 1 ].prevSignalType = TYPE_NO_VOICE_ACTIVITY;
313
22.7k
        psDec->channel_state[ 1 ].first_frame_after_reset = 1;
314
22.7k
    }
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
438k
    ALLOC( samplesOut1_tmp_storage1, decControl->nChannelsInternal*(channel_state[ 0 ].frame_length + 2 ),
320
438k
           opus_int16 );
321
438k
    samplesOut1_tmp[ 0 ] = samplesOut1_tmp_storage1;
322
438k
    samplesOut1_tmp[ 1 ] = samplesOut1_tmp_storage1 + channel_state[ 0 ].frame_length + 2;
323
324
438k
    if( lostFlag == FLAG_DECODE_NORMAL ) {
325
266k
        has_side = !decode_only_middle;
326
266k
    } else {
327
172k
        has_side = !psDec->prev_decode_only_middle
328
13.0k
              || (decControl->nChannelsInternal == 2 && lostFlag == FLAG_DECODE_LBRR && channel_state[1].LBRR_flags[ channel_state[1].nFramesDecoded ] == 1 );
329
172k
    }
330
438k
    channel_state[ 0 ].sPLC.enable_deep_plc = decControl->enable_deep_plc;
331
    /* Call decoder for one frame */
332
1.18M
    for( n = 0; n < decControl->nChannelsInternal; n++ ) {
333
751k
        if( n == 0 || has_side ) {
334
722k
            opus_int FrameIndex;
335
722k
            opus_int condCoding;
336
337
722k
            FrameIndex = channel_state[ 0 ].nFramesDecoded - n;
338
            /* Use independent coding if no previous frame available */
339
722k
            if( FrameIndex <= 0 ) {
340
459k
                condCoding = CODE_INDEPENDENTLY;
341
459k
            } else if( lostFlag == FLAG_DECODE_LBRR ) {
342
73.4k
                condCoding = channel_state[ n ].LBRR_flags[ FrameIndex - 1 ] ? CODE_CONDITIONALLY : CODE_INDEPENDENTLY;
343
189k
            } 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
6.83k
                condCoding = CODE_INDEPENDENTLY_NO_LTP_SCALING;
347
182k
            } else {
348
182k
                condCoding = CODE_CONDITIONALLY;
349
182k
            }
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
722k
            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
722k
                arch);
363
722k
        } else {
364
29.1k
            silk_memset( &samplesOut1_tmp[ n ][ 2 ], 0, nSamplesOutDec * sizeof( opus_int16 ) );
365
29.1k
        }
366
751k
        channel_state[ n ].nFramesDecoded++;
367
751k
    }
368
369
438k
    if( decControl->nChannelsAPI == 2 && decControl->nChannelsInternal == 2 ) {
370
        /* Convert Mid/Side to Left/Right */
371
114k
        silk_stereo_MS_to_LR( &psDec->sStereo, samplesOut1_tmp[ 0 ], samplesOut1_tmp[ 1 ], MS_pred_Q13, channel_state[ 0 ].fs_kHz, nSamplesOutDec );
372
324k
    } else {
373
        /* Buffering */
374
324k
        silk_memcpy( samplesOut1_tmp[ 0 ], psDec->sStereo.sMid, 2 * sizeof( opus_int16 ) );
375
324k
        silk_memcpy( psDec->sStereo.sMid, &samplesOut1_tmp[ 0 ][ nSamplesOutDec ], 2 * sizeof( opus_int16 ) );
376
324k
    }
377
378
    /* Number of output samples */
379
438k
    *nSamplesOut = silk_DIV32( nSamplesOutDec * decControl->API_sampleRate, silk_SMULBB( channel_state[ 0 ].fs_kHz, 1000 ) );
380
381
    /* Set up pointers to temp buffers */
382
438k
    ALLOC( samplesOut2_tmp, *nSamplesOut, opus_int16 );
383
438k
    resample_out_ptr = samplesOut2_tmp;
384
385
#ifdef ENABLE_OSCE_BWE
386
    ALLOC(resamp_buffer, 3 * MAX_FRAME_LENGTH, opus_int16);
387
#endif
388
389
991k
    for( n = 0; n < silk_min( decControl->nChannelsAPI, decControl->nChannelsInternal ); n++ ) {
390
391
#ifdef ENABLE_OSCE_BWE
392
        /* Resample or extend decoded signal to API_sampleRate */
393
        if (decControl->osce_extended_mode == OSCE_MODE_SILK_BBWE) {
394
            silk_assert(decControl->API_sampleRate == 48000);
395
396
            if (decControl->prev_osce_extended_mode != OSCE_MODE_SILK_BBWE) {
397
                /* Reset the BWE state */
398
                osce_bwe_reset( &channel_state[ n ].osce_bwe );
399
            }
400
401
            osce_bwe(&psDec->osce_model, &channel_state[ n ].osce_bwe,
402
                resample_out_ptr, &samplesOut1_tmp[ n ][ 1 ], nSamplesOutDec, arch);
403
404
            if (decControl->prev_osce_extended_mode == OSCE_MODE_SILK_ONLY ||
405
                decControl->prev_osce_extended_mode == OSCE_MODE_HYBRID) {
406
                    /* cross-fade with upsampled signal */
407
                    silk_resampler( &channel_state[ n ].resampler_state, resamp_buffer, &samplesOut1_tmp[ n ][ 1 ], nSamplesOutDec );
408
                    osce_bwe_cross_fade_10ms(resample_out_ptr, resamp_buffer, 480);
409
            }
410
        } else {
411
            ret += silk_resampler( &channel_state[ n ].resampler_state, resample_out_ptr, &samplesOut1_tmp[ n ][ 1 ], nSamplesOutDec );
412
            if (decControl->prev_osce_extended_mode == OSCE_MODE_SILK_BBWE && decControl->internalSampleRate == 16000) {
413
                /* fade out if internal sample rate did not change */
414
                osce_bwe(&psDec->osce_model, &channel_state[ n ].osce_bwe,
415
                    resamp_buffer, &samplesOut1_tmp[ n ][ 1 ], nSamplesOutDec, arch);
416
                /* cross-fade with upsampled signal */
417
                osce_bwe_cross_fade_10ms(resample_out_ptr, resamp_buffer, 480);
418
            }
419
        }
420
#else
421
        /* Resample decoded signal to API_sampleRate */
422
552k
        ret += silk_resampler( &channel_state[ n ].resampler_state, resample_out_ptr, &samplesOut1_tmp[ n ][ 1 ], nSamplesOutDec );
423
552k
#endif
424
        /* Interleave if stereo output and stereo stream */
425
552k
        if( decControl->nChannelsAPI == 2 ) {
426
123M
            for( i = 0; i < *nSamplesOut; i++ ) {
427
123M
                samplesOut[ n + 2 * i ] = INT16TORES(resample_out_ptr[ i ]);
428
123M
            }
429
286k
        } else {
430
106M
            for( i = 0; i < *nSamplesOut; i++ ) {
431
105M
                samplesOut[ i ] = INT16TORES(resample_out_ptr[ i ]);
432
105M
            }
433
265k
        }
434
552k
    }
435
436
#ifdef ENABLE_OSCE_BWE
437
    decControl->prev_osce_extended_mode = decControl->osce_extended_mode;
438
#endif
439
440
    /* Create two channel output from mono stream */
441
438k
    if( decControl->nChannelsAPI == 2 && decControl->nChannelsInternal == 1 ) {
442
58.8k
        if ( stereo_to_mono ){
443
            /* Resample right channel for newly collapsed stereo just in case
444
               we weren't doing collapsing when switching to mono */
445
0
            ret += silk_resampler( &channel_state[ 1 ].resampler_state, resample_out_ptr, &samplesOut1_tmp[ 0 ][ 1 ], nSamplesOutDec );
446
447
0
            for( i = 0; i < *nSamplesOut; i++ ) {
448
0
                samplesOut[ 1 + 2 * i ] = INT16TORES(resample_out_ptr[ i ]);
449
0
            }
450
58.8k
        } else {
451
25.6M
            for( i = 0; i < *nSamplesOut; i++ ) {
452
25.5M
                samplesOut[ 1 + 2 * i ] = samplesOut[ 0 + 2 * i ];
453
25.5M
            }
454
58.8k
        }
455
58.8k
    }
456
457
    /* Export pitch lag, measured at 48 kHz sampling rate */
458
438k
    if( channel_state[ 0 ].prevSignalType == TYPE_VOICED ) {
459
104k
        int mult_tab[ 3 ] = { 6, 4, 3 };
460
104k
        decControl->prevPitchLag = channel_state[ 0 ].lagPrev * mult_tab[ ( channel_state[ 0 ].fs_kHz - 8 ) >> 2 ];
461
334k
    } else {
462
334k
        decControl->prevPitchLag = 0;
463
334k
    }
464
465
438k
    if( lostFlag == FLAG_PACKET_LOST ) {
466
       /* On packet loss, remove the gain clamping to prevent having the energy "bounce back"
467
          if we lose packets when the energy is going down */
468
243k
       for ( i = 0; i < psDec->nChannelsInternal; i++ )
469
157k
          psDec->channel_state[ i ].LastGainIndex = 10;
470
352k
    } else {
471
352k
       psDec->prev_decode_only_middle = decode_only_middle;
472
352k
    }
473
438k
    RESTORE_STACK;
474
438k
    return ret;
475
438k
}
476
477
#if 0
478
/* Getting table of contents for a packet */
479
opus_int silk_get_TOC(
480
    const opus_uint8                *payload,           /* I    Payload data                                */
481
    const opus_int                  nBytesIn,           /* I    Number of input bytes                       */
482
    const opus_int                  nFramesPerPayload,  /* I    Number of SILK frames per payload           */
483
    silk_TOC_struct                 *Silk_TOC           /* O    Type of content                             */
484
)
485
{
486
    opus_int i, flags, ret = SILK_NO_ERROR;
487
488
    if( nBytesIn < 1 ) {
489
        return -1;
490
    }
491
    if( nFramesPerPayload < 0 || nFramesPerPayload > 3 ) {
492
        return -1;
493
    }
494
495
    silk_memset( Silk_TOC, 0, sizeof( *Silk_TOC ) );
496
497
    /* For stereo, extract the flags for the mid channel */
498
    flags = silk_RSHIFT( payload[ 0 ], 7 - nFramesPerPayload ) & ( silk_LSHIFT( 1, nFramesPerPayload + 1 ) - 1 );
499
500
    Silk_TOC->inbandFECFlag = flags & 1;
501
    for( i = nFramesPerPayload - 1; i >= 0 ; i-- ) {
502
        flags = silk_RSHIFT( flags, 1 );
503
        Silk_TOC->VADFlags[ i ] = flags & 1;
504
        Silk_TOC->VADFlag |= flags & 1;
505
    }
506
507
    return ret;
508
}
509
#endif