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 1006699 : 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 1006699 : if ( codec_mode == MODE1 )
83 : {
84 465089 : pt_meansForMemUpdate = &lsf_mean[0];
85 :
86 465089 : if ( Opt_AMR_WB )
87 : {
88 938 : pt_meansForFading = mean_isf_amr_wb;
89 : }
90 : else
91 : {
92 464151 : if ( L_frame == L_FRAME )
93 : {
94 152581 : if ( bwidth == NB )
95 : {
96 346 : pt_meansForFading = GENB_Ave;
97 : }
98 : else
99 : {
100 152235 : pt_meansForFading = GEWB_Ave;
101 : }
102 : }
103 : else
104 : {
105 311570 : pt_meansForFading = GEWB2_Ave;
106 : }
107 : }
108 : }
109 : else
110 : {
111 541610 : pt_meansForFading = pt_meansForMemUpdate = lsfBase;
112 541610 : if ( lsf_cng != NULL && plcBackgroundNoiseUpdated )
113 : {
114 247098 : pt_meansForFading = lsf_cng;
115 : }
116 : }
117 :
118 : /*----------------------------------------------------------------------*
119 : * Initialize the alpha factor
120 : *----------------------------------------------------------------------*/
121 :
122 1006699 : if ( nbLostCmpt <= 3 )
123 : {
124 620100 : if ( last_coder_type == UNVOICED )
125 : {
126 : /* clearly unvoiced */
127 24811 : alpha = ALPHA_UU;
128 : }
129 595289 : else if ( last_coder_type == AUDIO || last_good == INACTIVE_CLAS )
130 : {
131 11036 : if ( Last_GSC_pit_band_idx > 0 && nbLostCmpt > 1 )
132 : {
133 3304 : alpha = 0.8f;
134 : }
135 : else
136 : {
137 7732 : alpha = 0.995f;
138 : }
139 : }
140 584253 : else if ( last_good == UNVOICED_CLAS )
141 : {
142 181432 : if ( nbLostCmpt <= 1 )
143 : {
144 : /* if stable, do not flatten the spectrum in the 1st erased frame */
145 124750 : alpha = stab_fac * ( 1.0f - 2.0f * ALPHA_U ) + 2.0f * ALPHA_U; /* [0.8, 1.0] */
146 : }
147 56682 : else if ( nbLostCmpt == 2 )
148 : {
149 41028 : alpha = ALPHA_U * 1.5f; /* 0.6 */
150 : }
151 : else
152 : {
153 : /* go rapidly to CNG spectrum */
154 15654 : alpha = ALPHA_U;
155 : }
156 : }
157 402821 : else if ( last_good == UNVOICED_TRANSITION )
158 : {
159 7683 : alpha = ALPHA_UT;
160 : }
161 395138 : 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 349931 : alpha = ALPHA_V;
165 : }
166 45207 : else if ( last_good == SIN_ONSET )
167 : {
168 444 : alpha = ALPHA_S;
169 : }
170 : else
171 : {
172 44763 : alpha = ALPHA_VT;
173 : }
174 : }
175 : else
176 : {
177 386599 : alpha = 1.f / nbLostCmpt;
178 : }
179 :
180 1006699 : if ( codec_mode == MODE1 )
181 : {
182 465089 : beta = BETA_FEC;
183 : }
184 : else
185 : {
186 541610 : if ( plcBackgroundNoiseUpdated )
187 : {
188 247098 : beta = 0.f;
189 : }
190 : else
191 : {
192 294512 : beta = 0.25f;
193 : }
194 : }
195 :
196 17113883 : for ( i = 0; i < M; i++ )
197 : {
198 16107184 : lsf_mean[i] = beta * pt_meansForFading[i] + ( 1 - beta ) * lsf_adaptive_mean[i];
199 16107184 : lsf[i] = alpha * lsfold[i] + ( 1.0f - alpha ) * lsf_mean[i]; /* towards the CNG spectral envelope */
200 :
201 16107184 : if ( lsf_q_cng != NULL )
202 : {
203 8128320 : 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 1006699 : if ( Opt_AMR_WB )
213 : {
214 938 : reorder_isf( lsf, ISF_GAP, M, INT_FS_12k8 );
215 : }
216 : else
217 : {
218 1005761 : if ( L_frame == L_FRAME )
219 : {
220 227782 : reorder_lsf( lsf, codec_mode == MODE1 ? MODE1_LSF_GAP : LSF_GAP, M, INT_FS_12k8 );
221 :
222 227782 : if ( lsf_q_cng != NULL )
223 : {
224 62972 : reorder_lsf( lsf_q_cng, LSF_GAP, M, INT_FS_12k8 );
225 : }
226 : }
227 : else /* L_frame > L_FRAME */
228 : {
229 777979 : reorder_lsf( lsf, MODE1_LSF_GAP, M, L_frame * FRAMES_PER_SEC );
230 777979 : if ( lsf_q_cng != NULL )
231 : {
232 445048 : 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 1006699 : mvr2r( lsf, mem_AR, M );
238 :
239 17113883 : for ( i = 0; i < M; i++ )
240 : {
241 16107184 : tmp = lsf[i] - pt_meansForMemUpdate[i];
242 16107184 : mem_MA[i] = (float) ( tmp - MU_MA * mem_MA[i] ); /* Update with quantized prediction error for MA model */
243 16107184 : mem_MA[i] *= 0.5f; /* Attenuate the MA Q memory */
244 : }
245 :
246 1006699 : return;
247 : }
248 :
249 : /*---------------------------------------------------------------------*
250 : * routine: PlcGetlsfBase()
251 : *
252 : *
253 : *---------------------------------------------------------------------*/
254 :
255 542527 : const float *PlcGetlsfBase(
256 : const int16_t lpcQuantization,
257 : const int16_t narrowBand,
258 : const int32_t sr_core )
259 : {
260 542527 : 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 148963 : if ( sr_core == 32000 )
269 : {
270 87239 : return means_swb_cleanspeech_lsf32k0;
271 : }
272 61724 : else if ( sr_core == 25600 )
273 : {
274 17295 : return means_swb_cleanspeech_lsf25k6;
275 : }
276 : else
277 : {
278 44429 : return means_wb_cleanspeech_lsf16k0;
279 : }
280 : }
281 :
282 : /* lpcQuntization == 1 is left */
283 :
284 393564 : if ( sr_core == INT_FS_16k )
285 : {
286 318137 : return GEWB2_Ave;
287 : }
288 :
289 : /* sr_core == 12.8k is left */
290 :
291 75427 : if ( narrowBand == 0 )
292 : {
293 72895 : return GEWB_Ave;
294 : }
295 :
296 : /* narrowBand == 1 is left */
297 2532 : return GENB_Ave;
298 : }
|