Line data Source code
1 : /******************************************************************************************************
2 :
3 : (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
4 : Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
5 : Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
6 : Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
7 : contributors to this repository. All Rights Reserved.
8 :
9 : This software is protected by copyright law and by international treaties.
10 : The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
11 : Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
12 : Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
13 : Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
14 : contributors to this repository retain full ownership rights in their respective contributions in
15 : the software. This notice grants no license of any kind, including but not limited to patent
16 : license, nor is any license granted by implication, estoppel or otherwise.
17 :
18 : Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
19 : contributions.
20 :
21 : This software is provided "AS IS", without any express or implied warranties. The software is in the
22 : development stage. It is intended exclusively for experts who have experience with such software and
23 : solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
24 : and fitness for a particular purpose are hereby disclaimed and excluded.
25 :
26 : Any dispute, controversy or claim arising under or in relation to providing this software shall be
27 : submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
28 : accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
29 : the United Nations Convention on Contracts on the International Sales of Goods.
30 :
31 : *******************************************************************************************************/
32 :
33 : /*====================================================================================
34 : EVS Codec 3GPP TS26.443 Nov 04, 2021. Version 12.14.0 / 13.10.0 / 14.6.0 / 15.4.0 / 16.3.0
35 : ====================================================================================*/
36 :
37 :
38 : #include <stdint.h>
39 : #include "options.h"
40 : #include "cnst.h"
41 : #include "prot.h"
42 : #include "rom_com.h"
43 : #include "wmc_auto.h"
44 :
45 : /*---------------------------------------------------------------------*
46 : * routine: lsf_dec_bfi()
47 : *
48 : * Estimate the lsfs in case of FER
49 : * Bad frame, all active speech coders
50 : *---------------------------------------------------------------------*/
51 :
52 38903 : void lsf_dec_bfi(
53 : const int16_t codec_mode, /* i : codec mode: MODE1 | MODE2 */
54 : float *lsf, /* o : estimated LSF vector */
55 : const float *lsfold, /* i : past quantized lsf */
56 : float *lsf_adaptive_mean, /* i : lsf adaptive mean, updated when BFI==0 */
57 : const float lsfBase[], /* i : base for differential lsf coding */
58 : float *mem_MA, /* i/o: quantizer memory for MA model */
59 : float *mem_AR, /* o : quantizer memory for AR model */
60 : const float stab_fac, /* i : lsf stability factor */
61 : const int16_t last_coder_type, /* i : last coder type */
62 : const int16_t L_frame, /* i : frame length */
63 : const int16_t last_good, /* i : last good received frame */
64 : const int16_t nbLostCmpt, /* i : counter of consecutive bad frames */
65 : const int16_t plcBackgroundNoiseUpdated, /* i : background noise already updated?*/
66 : float *lsf_q_cng, /* o : quantized lsfs of background noise */
67 : float *lsf_cng, /* i : long term target for fading to bg noise*/
68 : float *old_lsf_q_cng, /* i : old quantized lsfs for background noise*/
69 : const int16_t Last_GSC_pit_band_idx, /* i : AC mode (GSC) - Last pitch band index */
70 : const int16_t Opt_AMR_WB, /* i : IO flag */
71 : const int16_t bwidth /* i : coded bandwidth */
72 : )
73 : {
74 : int16_t i;
75 : float alpha, tmp;
76 : float lsf_mean[M];
77 : const float *pt_meansForFading;
78 : const float *pt_meansForMemUpdate;
79 : float beta;
80 :
81 : /* init vectors */
82 38903 : if ( codec_mode == MODE1 )
83 : {
84 23796 : pt_meansForMemUpdate = &lsf_mean[0];
85 :
86 23796 : if ( Opt_AMR_WB )
87 : {
88 0 : pt_meansForFading = mean_isf_amr_wb;
89 : }
90 : else
91 : {
92 23796 : if ( L_frame == L_FRAME )
93 : {
94 7782 : if ( bwidth == NB )
95 : {
96 0 : pt_meansForFading = GENB_Ave;
97 : }
98 : else
99 : {
100 7782 : pt_meansForFading = GEWB_Ave;
101 : }
102 : }
103 : else
104 : {
105 16014 : pt_meansForFading = GEWB2_Ave;
106 : }
107 : }
108 : }
109 : else
110 : {
111 15107 : pt_meansForFading = pt_meansForMemUpdate = lsfBase;
112 15107 : if ( lsf_cng != NULL && plcBackgroundNoiseUpdated )
113 : {
114 3831 : pt_meansForFading = lsf_cng;
115 : }
116 : }
117 :
118 : /*----------------------------------------------------------------------*
119 : * Initialize the alpha factor
120 : *----------------------------------------------------------------------*/
121 :
122 38903 : if ( nbLostCmpt <= 3 )
123 : {
124 31994 : if ( last_coder_type == UNVOICED )
125 : {
126 : /* clearly unvoiced */
127 1269 : alpha = ALPHA_UU;
128 : }
129 30725 : else if ( last_coder_type == AUDIO || last_good == INACTIVE_CLAS )
130 : {
131 1212 : if ( Last_GSC_pit_band_idx > 0 && nbLostCmpt > 1 )
132 : {
133 258 : alpha = 0.8f;
134 : }
135 : else
136 : {
137 954 : alpha = 0.995f;
138 : }
139 : }
140 29513 : else if ( last_good == UNVOICED_CLAS )
141 : {
142 9030 : if ( nbLostCmpt <= 1 )
143 : {
144 : /* if stable, do not flatten the spectrum in the 1st erased frame */
145 7146 : alpha = stab_fac * ( 1.0f - 2.0f * ALPHA_U ) + 2.0f * ALPHA_U; /* [0.8, 1.0] */
146 : }
147 1884 : else if ( nbLostCmpt == 2 )
148 : {
149 1701 : alpha = ALPHA_U * 1.5f; /* 0.6 */
150 : }
151 : else
152 : {
153 : /* go rapidly to CNG spectrum */
154 183 : alpha = ALPHA_U;
155 : }
156 : }
157 20483 : else if ( last_good == UNVOICED_TRANSITION )
158 : {
159 272 : alpha = ALPHA_UT;
160 : }
161 20211 : else if ( last_good == VOICED_CLAS || last_good == ONSET )
162 : {
163 : /* clearly voiced - mild convergence to the CNG spectrum for the first 3 erased frames */
164 17800 : alpha = ALPHA_V;
165 : }
166 2411 : else if ( last_good == SIN_ONSET )
167 : {
168 36 : alpha = ALPHA_S;
169 : }
170 : else
171 : {
172 2375 : alpha = ALPHA_VT;
173 : }
174 : }
175 : else
176 : {
177 6909 : alpha = 1.f / nbLostCmpt;
178 : }
179 :
180 38903 : if ( codec_mode == MODE1 )
181 : {
182 23796 : beta = BETA_FEC;
183 : }
184 : else
185 : {
186 15107 : if ( plcBackgroundNoiseUpdated )
187 : {
188 3831 : beta = 0.f;
189 : }
190 : else
191 : {
192 11276 : beta = 0.25f;
193 : }
194 : }
195 :
196 661351 : for ( i = 0; i < M; i++ )
197 : {
198 622448 : lsf_mean[i] = beta * pt_meansForFading[i] + ( 1 - beta ) * lsf_adaptive_mean[i];
199 622448 : lsf[i] = alpha * lsfold[i] + ( 1.0f - alpha ) * lsf_mean[i]; /* towards the CNG spectral envelope */
200 :
201 622448 : if ( lsf_q_cng != NULL )
202 : {
203 221712 : lsf_q_cng[i] = max( alpha, 0.8f ) * old_lsf_q_cng[i] + ( 1.0f - max( alpha, 0.8f ) ) * pt_meansForFading[i];
204 : }
205 : }
206 :
207 : /*-----------------------------------------------------------------*
208 : * - estimate past quantized residual to be used in next frame
209 : * - Check A(z) filter stability through lsf ordering
210 : *-----------------------------------------------------------------*/
211 :
212 38903 : if ( Opt_AMR_WB )
213 : {
214 0 : reorder_isf( lsf, ISF_GAP, M, INT_FS_12k8 );
215 : }
216 : else
217 : {
218 38903 : if ( L_frame == L_FRAME )
219 : {
220 11138 : reorder_lsf( lsf, codec_mode == MODE1 ? MODE1_LSF_GAP : LSF_GAP, M, INT_FS_12k8 );
221 :
222 11138 : if ( lsf_q_cng != NULL )
223 : {
224 3081 : reorder_lsf( lsf_q_cng, LSF_GAP, M, INT_FS_12k8 );
225 : }
226 : }
227 : else /* L_frame > L_FRAME */
228 : {
229 27765 : reorder_lsf( lsf, MODE1_LSF_GAP, M, L_frame * FRAMES_PER_SEC );
230 27765 : if ( lsf_q_cng != NULL )
231 : {
232 10776 : reorder_lsf( lsf_q_cng, MODE1_LSF_GAP, M, L_frame * FRAMES_PER_SEC );
233 : }
234 : }
235 : }
236 : /* update the AR memory to be used in the next frame */
237 38903 : mvr2r( lsf, mem_AR, M );
238 :
239 661351 : for ( i = 0; i < M; i++ )
240 : {
241 622448 : tmp = lsf[i] - pt_meansForMemUpdate[i];
242 622448 : mem_MA[i] = (float) ( tmp - MU_MA * mem_MA[i] ); /* Update with quantized prediction error for MA model */
243 622448 : mem_MA[i] *= 0.5f; /* Attenuate the MA Q memory */
244 : }
245 :
246 38903 : return;
247 : }
248 :
249 : /*---------------------------------------------------------------------*
250 : * routine: PlcGetlsfBase()
251 : *
252 : *
253 : *---------------------------------------------------------------------*/
254 :
255 15089 : const float *PlcGetlsfBase(
256 : const int16_t lpcQuantization,
257 : const int16_t narrowBand,
258 : const int32_t sr_core )
259 : {
260 15089 : if ( lpcQuantization == 0 )
261 : {
262 : /* high rates, return value is never used; the correct value changes
263 : dynamically and is not available during PLC; therefore, the setting
264 : is kept as before (without the define PLC_FIX_XSF_HANDLING); the
265 : correct value would be isf[m] as returned by lpc_unquantize()
266 : during normal decoding */
267 :
268 4842 : if ( sr_core == 32000 )
269 : {
270 4125 : return means_swb_cleanspeech_lsf32k0;
271 : }
272 717 : else if ( sr_core == 25600 )
273 : {
274 717 : return means_swb_cleanspeech_lsf25k6;
275 : }
276 : else
277 : {
278 0 : return means_wb_cleanspeech_lsf16k0;
279 : }
280 : }
281 :
282 : /* lpcQuntization == 1 is left */
283 :
284 10247 : if ( sr_core == INT_FS_16k )
285 : {
286 6891 : return GEWB2_Ave;
287 : }
288 :
289 : /* sr_core == 12.8k is left */
290 :
291 3356 : if ( narrowBand == 0 )
292 : {
293 3356 : return GEWB_Ave;
294 : }
295 :
296 : /* narrowBand == 1 is left */
297 0 : return GENB_Ave;
298 : }
|