xref: /aosp_15_r20/external/libopus/silk/decode_frame.c (revision a58d3d2adb790c104798cd88c8a3aff4fa8b82cc)
1 /***********************************************************************
2 Copyright (c) 2006-2011, Skype Limited. All rights reserved.
3 Redistribution and use in source and binary forms, with or without
4 modification, are permitted provided that the following conditions
5 are met:
6 - Redistributions of source code must retain the above copyright notice,
7 this list of conditions and the following disclaimer.
8 - Redistributions in binary form must reproduce the above copyright
9 notice, this list of conditions and the following disclaimer in the
10 documentation and/or other materials provided with the distribution.
11 - Neither the name of Internet Society, IETF or IETF Trust, nor the
12 names of specific contributors, may be used to endorse or promote
13 products derived from this software without specific prior written
14 permission.
15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 POSSIBILITY OF SUCH DAMAGE.
26 ***********************************************************************/
27 
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31 
32 #include "main.h"
33 #include "stack_alloc.h"
34 #include "PLC.h"
35 
36 #ifdef ENABLE_OSCE
37 #include "osce.h"
38 #endif
39 
40 /****************/
41 /* Decode frame */
42 /****************/
silk_decode_frame(silk_decoder_state * psDec,ec_dec * psRangeDec,opus_int16 pOut[],opus_int32 * pN,opus_int lostFlag,opus_int condCoding,LPCNetPLCState * lpcnet,OSCEModel * osce_model,int arch)43 opus_int silk_decode_frame(
44     silk_decoder_state          *psDec,                         /* I/O  Pointer to Silk decoder state               */
45     ec_dec                      *psRangeDec,                    /* I/O  Compressor data structure                   */
46     opus_int16                  pOut[],                         /* O    Pointer to output speech frame              */
47     opus_int32                  *pN,                            /* O    Pointer to size of output frame             */
48     opus_int                    lostFlag,                       /* I    0: no loss, 1 loss, 2 decode fec            */
49     opus_int                    condCoding,                     /* I    The type of conditional coding to use       */
50 #ifdef ENABLE_DEEP_PLC
51     LPCNetPLCState              *lpcnet,
52 #endif
53 #ifdef ENABLE_OSCE
54     OSCEModel                   *osce_model,
55 #endif
56     int                         arch                            /* I    Run-time architecture                       */
57 )
58 {
59     VARDECL( silk_decoder_control, psDecCtrl );
60     opus_int         L, mv_len, ret = 0;
61     SAVE_STACK;
62 
63     L = psDec->frame_length;
64     ALLOC( psDecCtrl, 1, silk_decoder_control );
65     psDecCtrl->LTP_scale_Q14 = 0;
66 
67     /* Safety checks */
68     celt_assert( L > 0 && L <= MAX_FRAME_LENGTH );
69 
70     if(   lostFlag == FLAG_DECODE_NORMAL ||
71         ( lostFlag == FLAG_DECODE_LBRR && psDec->LBRR_flags[ psDec->nFramesDecoded ] == 1 ) )
72     {
73         VARDECL( opus_int16, pulses );
74 #ifdef ENABLE_OSCE
75         opus_int32  ec_start;
76         ec_start = ec_tell(psRangeDec);
77 #endif
78         ALLOC( pulses, (L + SHELL_CODEC_FRAME_LENGTH - 1) &
79                        ~(SHELL_CODEC_FRAME_LENGTH - 1), opus_int16 );
80         /*********************************************/
81         /* Decode quantization indices of side info  */
82         /*********************************************/
83         silk_decode_indices( psDec, psRangeDec, psDec->nFramesDecoded, lostFlag, condCoding );
84 
85         /*********************************************/
86         /* Decode quantization indices of excitation */
87         /*********************************************/
88         silk_decode_pulses( psRangeDec, pulses, psDec->indices.signalType,
89                 psDec->indices.quantOffsetType, psDec->frame_length );
90 
91         /********************************************/
92         /* Decode parameters and pulse signal       */
93         /********************************************/
94         silk_decode_parameters( psDec, psDecCtrl, condCoding );
95 
96         /********************************************************/
97         /* Run inverse NSQ                                      */
98         /********************************************************/
99         silk_decode_core( psDec, psDecCtrl, pOut, pulses, arch );
100 
101         /*************************/
102         /* Update output buffer. */
103         /*************************/
104         celt_assert( psDec->ltp_mem_length >= psDec->frame_length );
105         mv_len = psDec->ltp_mem_length - psDec->frame_length;
106         silk_memmove( psDec->outBuf, &psDec->outBuf[ psDec->frame_length ], mv_len * sizeof(opus_int16) );
107         silk_memcpy( &psDec->outBuf[ mv_len ], pOut, psDec->frame_length * sizeof( opus_int16 ) );
108 
109 #ifdef ENABLE_OSCE
110         /********************************************************/
111         /* Run SILK enhancer                                    */
112         /********************************************************/
113         osce_enhance_frame( osce_model, psDec, psDecCtrl, pOut, ec_tell(psRangeDec) - ec_start, arch );
114 #endif
115 
116         /********************************************************/
117         /* Update PLC state                                     */
118         /********************************************************/
119         silk_PLC( psDec, psDecCtrl, pOut, 0,
120 #ifdef ENABLE_DEEP_PLC
121             lpcnet,
122 #endif
123             arch );
124 
125         psDec->lossCnt = 0;
126         psDec->prevSignalType = psDec->indices.signalType;
127         celt_assert( psDec->prevSignalType >= 0 && psDec->prevSignalType <= 2 );
128 
129         /* A frame has been decoded without errors */
130         psDec->first_frame_after_reset = 0;
131     } else {
132         /* Handle packet loss by extrapolation */
133         silk_PLC( psDec, psDecCtrl, pOut, 1,
134 #ifdef ENABLE_DEEP_PLC
135             lpcnet,
136 #endif
137             arch );
138 
139 #ifdef ENABLE_OSCE
140         osce_reset( &psDec->osce, psDec->osce.method );
141 #endif
142         /*************************/
143         /* Update output buffer. */
144         /*************************/
145         celt_assert( psDec->ltp_mem_length >= psDec->frame_length );
146         mv_len = psDec->ltp_mem_length - psDec->frame_length;
147         silk_memmove( psDec->outBuf, &psDec->outBuf[ psDec->frame_length ], mv_len * sizeof(opus_int16) );
148         silk_memcpy( &psDec->outBuf[ mv_len ], pOut, psDec->frame_length * sizeof( opus_int16 ) );
149     }
150 
151     /************************************************/
152     /* Comfort noise generation / estimation        */
153     /************************************************/
154     silk_CNG( psDec, psDecCtrl, pOut, L );
155 
156     /****************************************************************/
157     /* Ensure smooth connection of extrapolated and good frames     */
158     /****************************************************************/
159     silk_PLC_glue_frames( psDec, pOut, L );
160 
161     /* Update some decoder state variables */
162     psDec->lagPrev = psDecCtrl->pitchL[ psDec->nb_subfr - 1 ];
163 
164     /* Set output frame length */
165     *pN = L;
166 
167     RESTORE_STACK;
168     return ret;
169 }
170