Line data Source code
1 : /*====================================================================================
2 : EVS Codec 3GPP TS26.452 Aug 12, 2021. Version 16.3.0
3 : ====================================================================================*/
4 :
5 :
6 : #include <stdint.h>
7 : #include "options.h"
8 : #include "basop_util.h"
9 : #include "rom_com.h"
10 : #include "cnst.h"
11 : #include "prot_fx.h"
12 :
13 : /*---------------------------------------------------------------------*
14 : * routine: lsf_dec_bfi()
15 : *
16 : * Estimate the LSFs in case of FER
17 : * Bad frame, all active speech coders
18 : *---------------------------------------------------------------------*/
19 9979 : void lsf_dec_bfi(
20 : const Word16 codec_mode, /* i: : codec mode: MODE1 | MODE2 */
21 : Word16 *lsf, /*!< o : 14Q1*1.28 quantized ISFs */
22 : const Word16 *lsfold, /*!< i : 14Q1*1.28 past quantized ISF */
23 : Word16 *lsf_adaptive_mean, /*!< i : 14Q1*1.28 ISF adaptive mean, updated when BFI==0 */
24 : const Word16 lsfBase[], /* i : base for differential lsf coding */
25 : Word16 *mem_MA, /*!< i/o: 14Q1*1.28 quantizer memory for MA model */
26 : Word16 *mem_AR, /*!< i/o: 14Q1*1.28 quantizer memory for MA model */
27 : Word16 stab_fac, /*!< i : ISF stability factor (shifted right by 1) Q15*/
28 : const Word16 last_coder_type, /*!< i : coding type in last good received fr. */
29 : Word16 L_frame,
30 : const Word16 last_good, /*!< i : last good received frame */
31 : const Word16 nbLostCmpt, /*!< i : counter of consecutive bad frames */
32 : const Word8 plcBackgroundNoiseUpdated, /* i : background noise already updated?*/
33 : Word16 *lsf_q_cng, /* o : quantized ISFs for background noise (14Q1*1.28) */
34 : Word16 *lsf_cng, /* Q2.56 */
35 : Word16 *old_lsf_q_cng, /* o : old quantized ISFs for background noise Q2.56*/
36 : const Word16 Last_GSC_pit_band_idx,
37 : const Word16 Opt_AMR_WB, /* i : IO flag */
38 : const Word8 tcxonly,
39 : const Word16 bwidth /* i: coded bandwidth */
40 : )
41 : {
42 : Word16 i;
43 : Word16 alpha;
44 : Word32 tmp;
45 : Word16 lsf_mean[M];
46 : const Word16 *pt_meansForFading;
47 : const Word16 *pt_meansForMemUpdate;
48 : Word16 beta;
49 : Word16 gap;
50 :
51 9979 : IF( EQ_16( codec_mode, MODE1 ) )
52 : {
53 5822 : pt_meansForMemUpdate = lsf_mean;
54 : /* Update inital guess to something stable, with proper sampling frequency and format (ISF/LSF)*/
55 5822 : IF( Opt_AMR_WB )
56 : {
57 0 : pt_meansForFading = Mean_isf_wb;
58 : }
59 : ELSE
60 : {
61 : /* 12.8kHz ACELP sampling */
62 5822 : IF( EQ_16( L_frame, L_FRAME ) )
63 : {
64 1310 : pt_meansForFading = GEWB_Ave_fx;
65 :
66 1310 : if ( bwidth == NB )
67 : {
68 0 : pt_meansForFading = GENB_Ave_fx;
69 : }
70 : }
71 : /* 16kHz ACELP sampling */
72 : ELSE
73 : {
74 4512 : pt_meansForFading = GEWB2_Ave_fx;
75 : }
76 : }
77 : }
78 : ELSE
79 : {
80 4157 : pt_meansForFading = pt_meansForMemUpdate = lsfBase;
81 4157 : test();
82 4157 : if ( lsf_cng != NULL && plcBackgroundNoiseUpdated )
83 : {
84 690 : pt_meansForFading = lsf_cng;
85 : }
86 : }
87 9979 : IF( LE_16( nbLostCmpt, 3 ) )
88 : {
89 7741 : test();
90 7741 : test();
91 7741 : IF( ( EQ_16( last_coder_type, UNVOICED ) ) ) /* Clear unvoiced last good frame */
92 : {
93 282 : move16();
94 282 : alpha = _ALPHA_UU_FX;
95 : }
96 7459 : ELSE IF( EQ_16( last_coder_type, AUDIO ) || EQ_16( last_good, INACTIVE_CLAS ) )
97 : {
98 120 : alpha = 32604 /*0.995f Q15*/;
99 120 : move16();
100 120 : test();
101 120 : if ( Last_GSC_pit_band_idx > 0 && GT_16( nbLostCmpt, 1 ) )
102 : {
103 14 : alpha = 26214 /*0.8f Q15*/;
104 14 : move16();
105 : }
106 : }
107 7339 : ELSE IF( EQ_16( last_good, UNVOICED_CLAS ) )
108 : {
109 2424 : IF( LE_16( nbLostCmpt, 1 ) )
110 : {
111 : /* If stable, do not flatten the spectrum in the 1st erased frame */
112 2117 : alpha = add( mult( stab_fac, 32768 - _ALPHA_U_FX_X_2 ), _ALPHA_U_FX_X_2 );
113 : }
114 307 : ELSE IF( EQ_16( nbLostCmpt, 2 ) )
115 : {
116 284 : alpha = sub( _ALPHA_U_FX_X_2, shr( _ALPHA_U_FX, 1 ) ); /* 0.6 */
117 : }
118 : ELSE
119 : {
120 23 : alpha = _ALPHA_U_FX;
121 23 : move16(); /* go rapidly to CNG spectrum */
122 : }
123 : }
124 4915 : ELSE IF( EQ_16( last_good, UNVOICED_TRANSITION ) )
125 : {
126 59 : alpha = _ALPHA_UT_FX;
127 59 : move16();
128 : }
129 4856 : ELSE IF( ( EQ_16( last_good, VOICED_CLAS ) ) || ( EQ_16( last_good, ONSET ) ) )
130 : {
131 : /* clearly voiced - mild convergence to the CNG spectrum for the first 3 erased frames */
132 4261 : move16();
133 4261 : alpha = _ALPHA_V_FX;
134 : }
135 595 : ELSE IF( EQ_16( last_good, SIN_ONSET ) )
136 : {
137 12 : alpha = _ALPHA_S_FX;
138 12 : move16();
139 : }
140 : ELSE
141 : {
142 583 : alpha = _ALPHA_VT_FX; /* rapid convergence to the CNG spectrum (long erasure, ONSETS) */
143 583 : move16();
144 : }
145 : }
146 : ELSE
147 : {
148 2238 : Word16 exp = 15;
149 2238 : move16();
150 2238 : alpha = Inv16( nbLostCmpt, &exp ); /*1.f/bfi_cnt;*/
151 2238 : alpha = shl( alpha, exp );
152 : }
153 9979 : IF( EQ_16( codec_mode, MODE1 ) )
154 : {
155 5822 : beta = BETA_FEC_FX;
156 5822 : move16();
157 : }
158 : ELSE
159 : {
160 4157 : beta = 8192 /*0.25f Q15*/;
161 4157 : move16();
162 4157 : if ( plcBackgroundNoiseUpdated )
163 : {
164 690 : beta = 0 /*0.f Q15*/;
165 690 : move16();
166 : }
167 : }
168 169643 : FOR( i = 0; i < M; i++ )
169 : {
170 :
171 159664 : lsf_mean[i] = mac_r( L_mult( beta, pt_meansForFading[i] ), sub( 32767 /*1.0F Q15*/, beta ), lsf_adaptive_mean[i] );
172 159664 : move16();
173 :
174 159664 : lsf[i] = mac_r( L_mult( alpha, lsfold[i] ), sub( 32767 /*1.0F Q15*/, alpha ), lsf_mean[i] );
175 159664 : move16();
176 :
177 159664 : IF( lsf_q_cng != NULL )
178 : {
179 46256 : lsf_q_cng[i] = mac_r( L_mult( s_max( alpha, 26214 /*0.8f Q15*/ ), old_lsf_q_cng[i] ), sub( 32767 /*1.0f Q15*/, s_max( alpha, 26214 /*0.8f Q15*/ ) ), pt_meansForFading[i] );
180 46256 : move16();
181 : }
182 : }
183 :
184 : /* check LSF stability through LSF ordering */
185 9979 : IF( Opt_AMR_WB )
186 : {
187 0 : reorder_isf_fx( lsf, ISF_GAP_FX, M, Fs_2 );
188 : }
189 : ELSE{
190 9979 : IF( EQ_16( L_frame, L_FRAME ) ){
191 2133 : IF( EQ_16( codec_mode, MODE1 ) ){
192 1310 : reorder_lsf_fx( lsf, MODE1_LSF_GAP_FX, M, INT_FS_FX ); /*arg1&2: 14Q1*1.18*/
193 : }
194 : ELSE
195 : {
196 823 : reorder_lsf_fx( lsf, LSF_GAP_FX, M, INT_FS_FX ); /*arg1&2: 14Q1*1.18*/
197 : }
198 :
199 2133 : IF( lsf_q_cng != NULL )
200 : {
201 563 : reorder_lsf_fx( lsf_q_cng, LSF_GAP_FX, M, INT_FS_FX );
202 : }
203 : }
204 7846 : ELSE IF( tcxonly != 0 )
205 : {
206 846 : IF( EQ_16( L_frame, 320 ) )
207 : {
208 0 : gap = 143;
209 0 : move16();
210 : }
211 846 : ELSE IF( EQ_16( L_frame, 512 ) )
212 : {
213 149 : gap = 90;
214 149 : move16();
215 : }
216 697 : ELSE IF( EQ_16( L_frame, 640 ) )
217 : {
218 697 : gap = 72;
219 697 : move16();
220 : }
221 : ELSE
222 : {
223 0 : gap = 48;
224 0 : move16();
225 : }
226 846 : reorder_lsf_fx( lsf, gap, M, INT_FS_FX );
227 : }
228 : ELSE
229 : {
230 7000 : reorder_lsf_fx( lsf, MODE1_LSF_GAP_FX, M, i_mult( L_frame, 50 ) ); /*arg1&2: 14Q1*1.18*/
231 7000 : IF( lsf_q_cng != NULL )
232 : {
233 1482 : reorder_lsf_fx( lsf_q_cng, LSF_GAP_FX, M, INT_FS_16k_FX );
234 : }
235 : }
236 : }
237 :
238 : /* update the AR memory to be used in the next frame */
239 : {
240 9979 : Copy( lsf, mem_AR, M );
241 : }
242 :
243 : /* update the MA memory to be used in the next frame */
244 169643 : FOR( i = 0; i < M; i++ )
245 : {
246 : /*factor 0x4000 means 0.5. Together with /2 in mem_MA-assignment,
247 : this results in an attenuation of the MA Q memory */
248 159664 : tmp = L_msu( L_mult( lsf[i], 0x4000 ), pt_meansForMemUpdate[i], 0x4000 );
249 : /* Update with quantized prediction error for MA model */
250 159664 : mem_MA[i] = msu_r( tmp, MU_MA_FX / 2, mem_MA[i] );
251 159664 : move16();
252 : }
253 :
254 :
255 9979 : return;
256 : }
257 :
258 4168 : Word16 const *PlcGetLsfBase(
259 : Word16 const lpcQuantization /*Q0*/,
260 : Word16 const narrowBand,
261 : Word32 const sr_core )
262 : {
263 : /* Not correct BW */
264 4168 : IF( lpcQuantization == 0 )
265 : {
266 : /* high rates, return value is never used; the correct value changes
267 : dynamically and is not available during PLC; therefore, the setting
268 : is kept as before (without the define PLC_FIX_XSF_HANDLING); the
269 : correct value would be isf[m] as returned by lpc_unquantize_fx()
270 : during normal decoding */
271 838 : IF( EQ_32( sr_core, 32000 ) )
272 : {
273 689 : return means_swb_cleanspeech_lsf32k0;
274 : }
275 149 : ELSE IF( EQ_32( sr_core, 25600 ) )
276 : {
277 149 : return means_swb_cleanspeech_lsf25k6;
278 : }
279 : ELSE
280 : {
281 0 : return means_wb_cleanspeech_lsf16k0;
282 : }
283 : }
284 :
285 : /* lpcQuntization == 1 is left */
286 :
287 3330 : IF( EQ_32( sr_core, INT_FS_16k ) )
288 : {
289 2507 : return GEWB2_Ave_fx;
290 : }
291 :
292 : /* sr_core == 12.8k is left */
293 :
294 823 : IF( narrowBand == 0 )
295 : {
296 823 : return GEWB_Ave_fx;
297 : }
298 :
299 : /* narrowBand == 1 is left */
300 0 : return GENB_Ave_fx;
301 : }
|