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 : #include <stdint.h>
38 : #include "options.h"
39 : #ifdef DEBUGGING
40 : #include "debug.h"
41 : #endif
42 : #include <math.h>
43 : #include "cnst.h"
44 : #include "rom_com.h"
45 : #include "prot.h"
46 : #include "wmc_auto.h"
47 :
48 : /*------------------------------------------------------------------*
49 : * amr_wb_dec()
50 : *
51 : * AMR-WB decoder
52 : *------------------------------------------------------------------*/
53 :
54 0 : ivas_error amr_wb_dec(
55 : Decoder_State *st, /* i/o: decoder state structure */
56 : float mem_hp20_out[L_HP20_MEM], /* i/o: HP filter memory for synthesis */
57 : float *output /* o : synthesis output */
58 : )
59 : {
60 : int16_t i;
61 : int16_t vad_flag;
62 : float ftmp;
63 : float old_exc[L_EXC_DEC], *exc; /* excitation signal buffer */
64 : float syn_tmp[L_FRAME + 2], *syn; /* synthesis signal buffer */
65 : float synth_out[L_FRAME48k]; /* synthesis output */
66 : int16_t output_frame; /* frame length at output sampling freq. */
67 : float lsf_new[M]; /* LSFs at the end of the frame */
68 : float lsp_new[M]; /* LSPs at the end of the frame */
69 : float Aq[NB_SUBFR * ( M + 1 )]; /* A(q) quantized for the 4 subframes */
70 : float exc2[L_FRAME]; /* total excitation buffer */
71 : float mem_tmp[M]; /* temporary synthesis filter memory */
72 : float pitch_buf[NB_SUBFR]; /* floating pitch for each subframe */
73 : float enr_q; /* E information for FER protection */
74 : float tmp_noise; /* Long term temporary noise energy */
75 : float FEC_pitch; /* FEC pitch */
76 : float dummy_buf[L_FRAME32k]; /* dummy buffer - no usage */
77 : int16_t allow_cn_step;
78 : int16_t locattack, amr_io_class;
79 : int16_t tmps;
80 : float xsp_tmp[M];
81 : float tmp_buffer[L_FRAME48k];
82 : float dct_buffer[DCT_L_POST];
83 : float frame_e;
84 : float exc_buffer[DCT_L_POST];
85 : float class_para;
86 : float voice_factors[NB_SUBFR];
87 : int16_t hf_gain[NB_SUBFR];
88 : int16_t delay_comp;
89 : int16_t nZeros;
90 : float tmp;
91 : int16_t last_core_ori;
92 0 : int16_t sid_bw = 0;
93 : float bpf_error_signal[L_FRAME];
94 : float ng_ener;
95 : float gain_buf[NB_SUBFR16k];
96 : float q_env[20];
97 : float exc3[L_FRAME];
98 : float *realBuffer[CLDFB_NO_COL_MAX], *imagBuffer[CLDFB_NO_COL_MAX];
99 : float realBufferTmp[CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX], imagBufferTmp[CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX];
100 : int16_t flag_cna;
101 0 : int16_t waveadj_rec = 0;
102 0 : TCX_LTP_DEC_HANDLE hTcxLtpDec = st->hTcxLtpDec;
103 : ivas_error error;
104 :
105 0 : error = IVAS_ERR_OK;
106 :
107 : /*------------------------------------------------------------------*
108 : * Initialization
109 : *------------------------------------------------------------------*/
110 :
111 0 : st->idchan = 0;
112 :
113 0 : syn_tmp[0] = 0;
114 0 : syn_tmp[1] = 0;
115 0 : syn = syn_tmp + 2;
116 :
117 0 : for ( i = 0; i < CLDFB_NO_COL_MAX; i++ )
118 : {
119 0 : set_f( realBufferTmp[i], 0, CLDFB_NO_CHANNELS_MAX );
120 0 : set_f( imagBufferTmp[i], 0, CLDFB_NO_CHANNELS_MAX );
121 0 : realBuffer[i] = realBufferTmp[i];
122 0 : imagBuffer[i] = imagBufferTmp[i];
123 : }
124 :
125 0 : set_f( gain_buf, 0, NB_SUBFR16k );
126 :
127 0 : st->use_partial_copy = 0;
128 0 : st->rf_frame_type = RF_NO_DATA;
129 0 : st->rf_flag = 0;
130 0 : st->rf_flag_last = 0;
131 :
132 0 : st->L_frame = L_FRAME;
133 0 : st->nb_subfr = NB_SUBFR;
134 0 : st->core = AMR_WB_CORE;
135 0 : st->core_brate = st->total_brate;
136 0 : st->extl = -1;
137 0 : st->bwidth = WB;
138 0 : st->coder_type = GENERIC;
139 0 : output_frame = (int16_t) ( st->output_Fs / FRAMES_PER_SEC ); /* frame length of the input signal */
140 :
141 0 : st->bpf_off = 0;
142 0 : if ( st->last_core == HQ_CORE )
143 : {
144 0 : st->bpf_off = 1;
145 0 : st->hPFstat->on = 0;
146 : }
147 0 : st->igf = 0;
148 :
149 0 : st->sr_core = st->L_frame * FRAMES_PER_SEC;
150 0 : st->fscale_old = st->fscale;
151 0 : st->fscale = sr2fscale( st->sr_core );
152 :
153 : /* Initialization in case that the first frame is the good received AMR-WB (IO) frame */
154 0 : if ( st->ini_frame == 0 )
155 : {
156 0 : st->last_core = AMR_WB_CORE;
157 0 : mvr2r( mean_isf_amr_wb, st->lsf_old, M );
158 0 : isf2isp( st->lsf_old, st->lsp_old, M, INT_FS_12k8 );
159 : }
160 :
161 : /* Updates in case of EVS primary mode -> AMR-WB IO mode switching */
162 0 : if ( st->last_core != AMR_WB_CORE )
163 : {
164 0 : updt_IO_switch_dec( output_frame, st );
165 : }
166 :
167 : /* Updates in case of HQ -> AMR-WB IO switching */
168 0 : if ( ( error = core_switching_pre_dec( st, output_frame, -1, -1, EVS_MONO, -1 ) ) != IVAS_ERR_OK )
169 : {
170 0 : return error;
171 : }
172 :
173 :
174 0 : last_core_ori = st->last_core;
175 0 : set_s( hf_gain, 0, NB_SUBFR );
176 :
177 0 : enr_q = 0.0f;
178 0 : tmp_noise = 0.0f;
179 0 : amr_io_class = UNVOICED_CLAS;
180 :
181 0 : mvr2r( st->old_exc, old_exc, L_EXC_MEM_DEC );
182 0 : exc = old_exc + L_EXC_MEM_DEC;
183 :
184 : /* reset post-filter in case of switching */
185 0 : if ( st->hPFstat->on == 0 )
186 : {
187 0 : st->hPFstat->reset = 1;
188 : }
189 :
190 0 : if ( st->bfi )
191 : {
192 0 : st->nbLostCmpt++;
193 : }
194 : else
195 : {
196 0 : st->nbLostCmpt = 0;
197 : }
198 :
199 : /* PLC: [TCX: Fade-out-recovery]
200 : * PLC: overlapping part needs to be attenuated for first good frame */
201 0 : if ( !st->bfi && st->prev_bfi && ( st->last_codec_mode == MODE2 ) && ( st->last_core_bfi == TCX_20_CORE || st->last_core_bfi == TCX_10_CORE ) )
202 : {
203 0 : v_multc( st->hHQ_core->old_out, st->hPlcInfo->recovery_gain, st->hHQ_core->old_out, st->hTcxDec->L_frameTCX );
204 : }
205 :
206 : /*-----------------------------------------------------------------*
207 : * switching from ACELP@16k core to AMR-WB IO mode
208 : *-----------------------------------------------------------------*/
209 :
210 0 : st->rate_switching_reset = 0;
211 :
212 0 : if ( st->last_core != AMR_WB_CORE && st->last_L_frame == L_FRAME16k && st->last_core != HQ_CORE )
213 : {
214 : /* in case of switching, do not apply BPF */
215 0 : st->bpf_off = 1;
216 0 : if ( st->hPFstat->on != 0 )
217 : {
218 : int16_t mem_syn_r_size_old, mem_syn_r_size_new;
219 :
220 0 : mem_syn_r_size_old = (int16_t) ( 1.25 * st->last_L_frame / 20.f );
221 0 : mem_syn_r_size_new = (int16_t) ( 1.25 * st->L_frame / 20.f );
222 0 : lerp( st->hPFstat->mem_stp + L_SYN_MEM - mem_syn_r_size_old, st->hPFstat->mem_stp + L_SYN_MEM - mem_syn_r_size_new, mem_syn_r_size_new, mem_syn_r_size_old );
223 0 : lerp( st->hPFstat->mem_pf_in + L_SYN_MEM - mem_syn_r_size_old, st->hPFstat->mem_pf_in + L_SYN_MEM - mem_syn_r_size_new, mem_syn_r_size_new, mem_syn_r_size_old );
224 : }
225 :
226 : /* convert old quantized LSP vector */
227 0 : st->rate_switching_reset = lsp_convert_poly( st->lsp_old, L_FRAME, 1 );
228 :
229 : /* convert old quantized LSF vector */
230 0 : lsp2lsf( st->lsp_old, st->lsf_old, M, INT_FS_12k8 );
231 :
232 : /* FEC - update adaptive LSF mean vector */
233 0 : mvr2r( st->lsf_old, st->lsfoldbfi1, M );
234 0 : mvr2r( st->lsf_old, st->lsfoldbfi0, M );
235 0 : mvr2r( st->lsf_old, st->lsf_adaptive_mean, M );
236 :
237 : /* Reset LPC mem */
238 0 : mvr2r( GEWB_Ave, st->mem_AR, M );
239 0 : set_zero( st->mem_MA, M );
240 :
241 : /* update synthesis filter memories */
242 0 : synth_mem_updt2( L_FRAME, st->last_L_frame, st->old_exc, st->mem_syn_r, st->mem_syn2, NULL, DEC );
243 0 : mvr2r( st->old_exc, old_exc, L_EXC_MEM_DEC );
244 0 : mvr2r( st->mem_syn2, st->mem_syn1, M );
245 0 : mvr2r( st->mem_syn2, st->mem_syn3, M );
246 :
247 : /* LSP -> ISP */
248 0 : mvr2r( stable_ISP, xsp_tmp, M );
249 0 : lsp2isp( st->lsp_old, st->lsp_old, xsp_tmp, M );
250 : }
251 :
252 : /* update buffer of old subframe pitch values */
253 0 : if ( st->last_L_frame != L_FRAME )
254 : {
255 0 : if ( st->last_L_frame == L_FRAME32k )
256 : {
257 0 : tmp = (float) 12800 / (float) 32000;
258 : }
259 0 : else if ( st->last_L_frame == 512 )
260 : {
261 0 : tmp = (float) 12800 / (float) 25600;
262 : }
263 : else /* st->last_L_frame == L_FRAME16k */
264 : {
265 0 : tmp = (float) 12800 / (float) 16000;
266 : }
267 :
268 0 : for ( i = NB_SUBFR16k - NB_SUBFR; i < NB_SUBFR16k; i++ )
269 : {
270 0 : st->old_pitch_buf[i - 1] = tmp * st->old_pitch_buf[i];
271 : }
272 :
273 0 : for ( i = 2 * NB_SUBFR16k - NB_SUBFR; i < 2 * NB_SUBFR16k; i++ )
274 : {
275 0 : st->old_pitch_buf[i - 2] = tmp * st->old_pitch_buf[i];
276 : }
277 : }
278 :
279 0 : if ( st->bfi_pitch_frame != L_FRAME )
280 : {
281 0 : if ( st->bfi_pitch_frame == L_FRAME32k )
282 : {
283 0 : tmp = (float) 12800 / (float) 32000;
284 : }
285 0 : else if ( st->bfi_pitch_frame == 512 )
286 : {
287 0 : tmp = (float) 12800 / (float) 25600;
288 : }
289 : else /* st->bfi_pitch_frame == L_FRAME16k */
290 : {
291 0 : tmp = (float) 12800 / (float) 16000;
292 : }
293 :
294 0 : st->bfi_pitch *= tmp;
295 0 : st->bfi_pitch_frame = L_FRAME;
296 : }
297 :
298 0 : if ( st->last_bwidth == NB && st->ini_frame != 0 )
299 : {
300 0 : st->rate_switching_reset = 1;
301 : }
302 :
303 : /*----------------------------------------------------------------------*
304 : * GOOD frame
305 : *----------------------------------------------------------------------*/
306 :
307 0 : if ( !st->bfi )
308 : {
309 : /*----------------------------------------------------------------*
310 : * Processing of FRAME_NO_DATA frames
311 : * Decoding of SID frames
312 : *----------------------------------------------------------------*/
313 :
314 0 : if ( st->core_brate == FRAME_NO_DATA || st->core_brate == SID_1k75 )
315 : {
316 : /* decode CNG parameters */
317 0 : CNG_dec( st, EVS_MONO, Aq, lsp_new, lsf_new, &allow_cn_step, &sid_bw, q_env );
318 :
319 : /* comfort noise generation */
320 0 : CNG_exc( st->core_brate, L_FRAME, &st->hTdCngDec->Enew, &st->hTdCngDec->cng_seed, exc, exc2, &st->lp_ener, st->last_core_brate, &st->first_CNG, &st->hTdCngDec->cng_ener_seed, dummy_buf, allow_cn_step, &st->hTdCngDec->last_allow_cn_step, st->hTdCngDec->num_ho, q_env, st->hTdCngDec->lp_env, st->hTdCngDec->old_env, st->hTdCngDec->exc_mem, st->hTdCngDec->exc_mem1, &sid_bw, &st->hTdCngDec->cng_ener_seed1, exc3, st->Opt_AMR_WB, EVS_MONO );
321 0 : set_f( voice_factors, 1.0f, NB_SUBFR );
322 0 : class_para = 0.0f;
323 :
324 : /* update past excitation signals for LD music post-filter */
325 0 : mvr2r( st->hMusicPF->dct_post_old_exc + L_FRAME, st->hMusicPF->dct_post_old_exc, DCT_L_POST - L_FRAME - OFFSET2 );
326 0 : mvr2r( exc2, st->hMusicPF->dct_post_old_exc + ( DCT_L_POST - L_FRAME - OFFSET2 ), L_FRAME );
327 :
328 : /* synthesis at 12k8 Hz sampling rate */
329 0 : syn_12k8( L_FRAME, Aq, exc2, syn, st->mem_syn2, 1 );
330 0 : syn_12k8( st->L_frame, Aq, exc2, dummy_buf, st->mem_syn3, 1 );
331 :
332 : /* reset the decoder */
333 0 : CNG_reset_dec( st, pitch_buf, dummy_buf + L_FRAME );
334 :
335 : /* update st->mem_syn1 for ACELP core switching */
336 0 : mvr2r( st->mem_syn3, st->mem_syn1, M );
337 0 : if ( output_frame != L_FRAME8k )
338 : {
339 0 : frame_e = 10.0f * (float) log10( dotp( syn, syn, L_FRAME ) / (float) L_FRAME );
340 0 : st->psf_lp_noise = 0.99f * st->psf_lp_noise + 0.01f * frame_e;
341 : }
342 :
343 : /* update old synthesis for classification */
344 0 : mvr2r( syn + L_FRAME - L_SYN_MEM_CLAS_ESTIM, st->mem_syn_clas_estim, L_SYN_MEM_CLAS_ESTIM );
345 :
346 : /* Update music post processing values */
347 : /* Filter energies update */
348 0 : for ( i = 0; i < DCT_L_POST; i++ )
349 : {
350 0 : st->hMusicPF->filt_lfE[i] = 0.3f + 0.7f * st->hMusicPF->filt_lfE[i];
351 : }
352 :
353 0 : vad_flag = 0;
354 : }
355 :
356 : /*----------------------------------------------------------------*
357 : * Decoding of all other frames
358 : *----------------------------------------------------------------*/
359 :
360 : else
361 : {
362 : /*-----------------------------------------------------------------*
363 : * After CNG period, use the most up-to-date ISPs
364 : *-----------------------------------------------------------------*/
365 :
366 0 : if ( st->last_core_brate == FRAME_NO_DATA || st->last_core_brate == SID_1k75 )
367 : {
368 0 : mvr2r( st->lspCNG, st->lsp_old, M );
369 0 : isp2isf( st->lspCNG, st->lsf_old, M, INT_FS_12k8 );
370 0 : set_f( old_exc, 0, L_EXC_MEM_DEC );
371 : }
372 :
373 : /*------------------------------------------------------------*
374 : * Extracts VAD information from the bitstream in AMR-WB IO mode
375 : *------------------------------------------------------------*/
376 :
377 0 : vad_flag = get_next_indice( st, 1 );
378 :
379 0 : if ( vad_flag == 0 )
380 : {
381 0 : st->coder_type = INACTIVE;
382 : }
383 : else
384 : {
385 0 : st->coder_type = GENERIC;
386 : }
387 :
388 : /*-----------------------------------------------------------------*
389 : * ISF de-quantization and interpolation
390 : *-----------------------------------------------------------------*/
391 :
392 0 : isf_dec_amr_wb( st, Aq, lsf_new, lsp_new );
393 :
394 : /*------------------------------------------------------------*
395 : * Decode excitation
396 : *------------------------------------------------------------*/
397 :
398 0 : decod_amr_wb( st, Aq, pitch_buf, exc, exc2, hf_gain, voice_factors, gain_buf );
399 :
400 : /* synthesis for ACELP core switching and SWB BWE */
401 0 : syn_12k8( L_FRAME, Aq, exc, tmp_buffer, st->mem_syn1, 1 );
402 :
403 : /*------------------------------------------------------------*
404 : * Update long-term energies for FEC
405 : * Update ISP vector for CNG
406 : *------------------------------------------------------------*/
407 :
408 0 : if ( st->coder_type == INACTIVE )
409 : {
410 0 : if ( st->unv_cnt > 20 )
411 : {
412 0 : ftmp = st->lp_gainc * st->lp_gainc;
413 0 : st->lp_ener = 0.7f * st->lp_ener + 0.3f * ftmp;
414 0 : for ( i = 0; i < M; i++ )
415 : {
416 0 : st->lspCNG[i] = (float) ( 0.9f * st->lspCNG[i] + 0.1f * lsp_new[i] );
417 : }
418 : }
419 : else
420 : {
421 0 : st->unv_cnt++;
422 : }
423 : }
424 : else
425 : {
426 0 : st->unv_cnt = 0;
427 : }
428 :
429 : /*------------------------------------------------------------*
430 : * Save filter memory in case the synthesis is redone after scaling
431 : * Core synthesis at 12k8 Hz
432 : *------------------------------------------------------------*/
433 :
434 0 : mvr2r( st->mem_syn2, mem_tmp, M );
435 0 : syn_12k8( L_FRAME, Aq, exc2, syn, st->mem_syn2, 1 );
436 :
437 : /*------------------------------------------------------------*
438 : * FEC - Estimate the classification information
439 : *------------------------------------------------------------*/
440 :
441 0 : FEC_clas_estim( syn, pitch_buf, st->L_frame, st->coder_type, st->codec_mode, st->mem_syn_clas_estim, &st->clas_dec, &st->lp_ener_bfi, st->Opt_AMR_WB, &st->decision_hyst, &locattack, &st->hAmrwb_IO->UV_cnt, &st->hAmrwb_IO->LT_UV_cnt, &st->hAmrwb_IO->Last_ener, &amr_io_class, st->hAmrwb_IO->lt_diff_etot, &class_para, 0, 0, CLASSIFIER_ACELP, 0, 0, 0, st->last_core_brate, -1 );
442 :
443 : /* update past excitation signals for LD music post-filter */
444 0 : mvr2r( st->hMusicPF->dct_post_old_exc + L_FRAME, st->hMusicPF->dct_post_old_exc, DCT_L_POST - L_FRAME - OFFSET2 );
445 0 : mvr2r( exc2, st->hMusicPF->dct_post_old_exc + ( DCT_L_POST - L_FRAME - OFFSET2 ), L_FRAME );
446 0 : mvr2r( st->hMusicPF->dct_post_old_exc, exc_buffer, DCT_L_POST - OFFSET2 );
447 :
448 0 : if ( output_frame != L_FRAME8k )
449 : {
450 0 : if ( st->coder_type == INACTIVE )
451 : {
452 0 : frame_energy( L_FRAME, pitch_buf, syn, 0.0f, &frame_e );
453 0 : st->psf_lp_noise = 0.99f * st->psf_lp_noise + 0.01f * frame_e;
454 : }
455 : }
456 :
457 0 : if ( amr_io_class != UNVOICED_CLAS && st->coder_type != INACTIVE && st->psf_lp_noise < 15.0f )
458 0 : {
459 0 : int16_t tmp_coder_type = AUDIO;
460 0 : if ( st->last_coder_type == INACTIVE || st->last_coder_type == UNVOICED )
461 : {
462 0 : tmp_coder_type = INACTIVE;
463 : }
464 : /* Extrapolation of the last future part, windowing and high resolution DCT transform */
465 0 : Prep_music_postP( exc_buffer, dct_buffer, st->hMusicPF->filt_lfE, st->last_core, pitch_buf, st->hMusicPF->LDm_enh_lp_gbin );
466 :
467 : /* LD music post-filter */
468 0 : LD_music_post_filter( st->hMusicPF, dct_buffer, dct_buffer, st->core_brate, -1, tmp_coder_type );
469 :
470 : /* Inverse DCT transform, retrieval of the aligned excitation, re-synthesis */
471 0 : Post_music_postP( dct_buffer, exc_buffer, exc2, mem_tmp, st->mem_syn2, Aq, syn );
472 : }
473 : else
474 : {
475 : /*------------------------------------------------------------*
476 : * Improvement for unvoiced and audio signals
477 : *------------------------------------------------------------*/
478 :
479 0 : improv_amr_wb_gs( amr_io_class, st->coder_type, st->core_brate, &st->hGSCDec->seed_tcx, st->hAmrwb_IO->old_Aq, st->mem_syn2, st->hAmrwb_IO->lt_voice_fac, locattack, Aq, exc2, mem_tmp, syn, pitch_buf, st->hAmrwb_IO->Last_ener, st->rate_switching_reset, st->last_coder_type, 0 );
480 :
481 0 : for ( i = 0; i < DCT_L_POST; i++ )
482 : {
483 0 : st->hMusicPF->filt_lfE[i] = 0.3f + 0.7f * st->hMusicPF->filt_lfE[i];
484 : }
485 : }
486 :
487 : /*------------------------------------------------------------*
488 : * FEC - Estimate pitch
489 : *------------------------------------------------------------*/
490 :
491 0 : FEC_pitch_estim( 1, st->last_core, L_FRAME, st->clas_dec, st->last_good, pitch_buf, st->old_pitch_buf, &st->bfi_pitch, &st->bfi_pitch_frame, &st->upd_cnt, GENERIC );
492 :
493 : /*------------------------------------------------------------*
494 : * FEC - Smooth the speech energy evolution when recovering after a BAD frame
495 : * (smoothing is performed in the excitation domain and signal is resynthesized after)
496 : *------------------------------------------------------------*/
497 :
498 0 : FEC_scale_syn( L_FRAME, st->clas_dec, st->last_good, syn, pitch_buf, st->enr_old, enr_q, -1, MOVING_AVERAGE, &st->scaling_flag, &st->lp_ener_FEC_av, &st->lp_ener_FEC_max, st->bfi, st->total_brate, st->prev_bfi, st->last_core_brate, exc, exc2, Aq, &st->old_enr_LP, mem_tmp, st->mem_syn2, st->last_con_tcx && ( st->L_frameTCX_past != st->L_frame ) && ( st->last_core != ACELP_CORE ), 0 );
499 :
500 : /* estimate the pitch-synchronous speech energy per sample to be used when normal operation recovers */
501 0 : fer_energy( L_FRAME, st->clas_dec, syn, pitch_buf[3], &st->enr_old, L_FRAME );
502 : }
503 :
504 : } /* End of GOOD FRAME */
505 :
506 : /*----------------------------------------------------------------*
507 : * BAD frame
508 : *----------------------------------------------------------------*/
509 :
510 : else
511 : {
512 : /* long burst frame erasures */
513 0 : if ( st->nbLostCmpt > 5 && st->clas_dec >= VOICED_CLAS )
514 : {
515 0 : st->last_good = VOICED_TRANSITION;
516 : }
517 :
518 0 : vad_flag = st->last_vad;
519 0 : amr_io_class = st->last_good;
520 0 : class_para = 0.0f;
521 :
522 : /* LSF estimation and A(z) calculation */
523 0 : lsf_dec_bfi( MODE1, lsf_new, st->lsf_old, st->lsf_adaptive_mean, NULL, st->mem_MA, st->mem_AR, st->stab_fac, st->last_coder_type, L_FRAME, st->last_good, st->nbLostCmpt, 0, NULL, NULL, NULL, st->hGSCDec->Last_GSC_pit_band_idx, st->Opt_AMR_WB, st->bwidth );
524 :
525 0 : FEC_lsf2lsp_interp( st, L_FRAME, Aq, lsf_new, lsp_new );
526 :
527 : /* calculation of excitation signal */
528 0 : FEC_exc_estim( st, L_FRAME, exc, exc2, tmp_buffer, pitch_buf, voice_factors, &FEC_pitch, dummy_buf, lsf_new, &tmp_noise );
529 :
530 : /* synthesis for ACELP core switching and SWB BWE */
531 0 : syn_12k8( L_FRAME, Aq, exc, tmp_buffer, st->mem_syn1, 1 );
532 :
533 : /* update past excitation signals */
534 0 : mvr2r( st->hMusicPF->dct_post_old_exc + L_FRAME, st->hMusicPF->dct_post_old_exc, DCT_L_POST - L_FRAME - OFFSET2 );
535 0 : mvr2r( exc2, st->hMusicPF->dct_post_old_exc + ( DCT_L_POST - L_FRAME - OFFSET2 ), L_FRAME );
536 :
537 : /* Update music post processing values */
538 : /* Update circular buffer, keep last energy difference unchanged */
539 0 : for ( i = 1; i < MAX_LT; i++ )
540 : {
541 0 : st->hMusicPF->LDm_lt_diff_etot[i - 1] = st->hMusicPF->LDm_lt_diff_etot[i];
542 : }
543 : /* Filter energies update */
544 0 : for ( i = 0; i < DCT_L_POST; i++ )
545 : {
546 0 : st->hMusicPF->filt_lfE[i] = 0.3f + 0.7f * st->hMusicPF->filt_lfE[i];
547 : }
548 :
549 : /* synthesis at 12k8 Hz sampling rate */
550 0 : mvr2r( st->mem_syn2, mem_tmp, M );
551 0 : syn_12k8( L_FRAME, Aq, exc2, syn, st->mem_syn2, 1 );
552 :
553 : /* update old synthesis for classification */
554 0 : mvr2r( syn + L_FRAME - L_SYN_MEM_CLAS_ESTIM, st->mem_syn_clas_estim, L_SYN_MEM_CLAS_ESTIM );
555 :
556 :
557 : /*------------------------------------------------------------*
558 : * FEC - Smooth the speech energy evolution when recovering after a BAD frame
559 : * (smoothing is performed in the excitation domain and signal is resynthesized after)
560 : *------------------------------------------------------------*/
561 :
562 0 : FEC_scale_syn( L_FRAME, st->clas_dec, st->last_good, syn, pitch_buf, st->enr_old, enr_q, -1, MOVING_AVERAGE, &st->scaling_flag, &st->lp_ener_FEC_av, &st->lp_ener_FEC_max, st->bfi, st->total_brate, st->prev_bfi, st->last_core_brate, exc, exc2, Aq, &st->old_enr_LP, mem_tmp, st->mem_syn2, 0, 0 );
563 :
564 : /* estimate the pitch-synchronous speech energy per sample to be used when normal operation recovers */
565 0 : fer_energy( L_FRAME, st->last_good, syn, FEC_pitch, &st->enr_old, L_FRAME );
566 : }
567 :
568 : /*--------------------------------------------------------*
569 : * NB post-filter
570 : *--------------------------------------------------------*/
571 :
572 0 : if ( output_frame == L_FRAME8k || st->last_bwidth == NB )
573 : {
574 0 : if ( output_frame == L_FRAME8k )
575 : {
576 0 : st->hPFstat->on = 1;
577 0 : nb_post_filt( L_FRAME, L_SUBFR, st->hPFstat, &st->psf_lp_noise, tmp_noise, syn, Aq, pitch_buf, st->coder_type, st->BER_detect, 0 );
578 : }
579 : else
580 : {
581 0 : st->hPFstat->on = 0;
582 0 : nb_post_filt( L_FRAME, L_SUBFR, st->hPFstat, &st->psf_lp_noise, tmp_noise, syn, Aq, pitch_buf, AUDIO, st->BER_detect, 0 );
583 : }
584 : }
585 :
586 : /*------------------------------------------------------------------*
587 : * Perform fixed deemphasis through 1/(1 - g*z^-1)
588 : *-----------------------------------------------------------------*/
589 :
590 : /* update old synthesis buffer - needed for ACELP internal sampling rate switching */
591 0 : mvr2r( syn + L_FRAME - L_SYN_MEM, st->mem_syn_r, L_SYN_MEM );
592 :
593 :
594 0 : deemph( syn, PREEMPH_FAC, L_FRAME, &( st->mem_deemph ) );
595 0 : AGC_dec( syn, st->agc_mem2, L_FRAME );
596 0 : if ( st->hTcxDec != NULL )
597 : {
598 0 : mvr2r( syn + L_FRAME / 2, st->hTcxDec->old_syn_Overl, L_FRAME / 2 );
599 : }
600 0 : mvr2r( syn + L_FRAME - M - 1, st->syn, M + 1 );
601 :
602 : /*------------------------------------------------------------------*
603 : * Formant post-filter
604 : *-----------------------------------------------------------------*/
605 :
606 0 : mvr2r( syn, tmp_buffer + L_SYN_MEM, L_FRAME );
607 :
608 0 : if ( output_frame != L_FRAME8k && st->last_bwidth != NB )
609 : {
610 0 : st->hPFstat->on = 1;
611 0 : formant_post_filt( st->hPFstat, tmp_buffer + L_SYN_MEM, Aq, syn, L_FRAME, L_SUBFR, st->psf_lp_noise, st->total_brate, amr_io_class == AUDIO_CLAS );
612 : }
613 :
614 : /*----------------------------------------------------------------*
615 : * Comfort Noise Addition
616 : *----------------------------------------------------------------*/
617 :
618 0 : flag_cna = 0;
619 0 : if ( st->psf_lp_noise >= 15.f || st->coder_type == INACTIVE )
620 : {
621 : /*VAD only for non inactive frame*/
622 0 : st->VAD = ( st->VAD && ( st->coder_type != INACTIVE ) );
623 :
624 0 : ApplyFdCng( syn, NULL, NULL, NULL, st, 0, 0 );
625 :
626 0 : st->hFdCngDec->hFdCngCom->frame_type_previous = st->m_frame_type;
627 :
628 : /*Noisy speech detector*/
629 0 : noisy_speech_detection( st->hFdCngDec, st->VAD, syn );
630 :
631 0 : st->hFdCngDec->hFdCngCom->likelihood_noisy_speech = 0.99f * st->hFdCngDec->hFdCngCom->likelihood_noisy_speech + 0.01f * (float) st->hFdCngDec->hFdCngCom->flag_noisy_speech;
632 0 : st->lp_noise = st->hFdCngDec->lp_noise;
633 :
634 0 : if ( st->flag_cna && ( st->psf_lp_noise >= 15.f ) )
635 : {
636 0 : flag_cna = 1;
637 0 : generate_masking_noise( syn, st->hFdCngDec->hFdCngCom, st->hFdCngDec->hFdCngCom->frameSize, AMR_WB_CORE, 0, 0, st->element_mode, NULL, -1 );
638 : }
639 0 : else if ( st->flag_cna )
640 : {
641 0 : generate_masking_noise_update_seed( st->hFdCngDec->hFdCngCom );
642 : }
643 : }
644 0 : else if ( st->flag_cna )
645 : {
646 0 : generate_masking_noise_update_seed( st->hFdCngDec->hFdCngCom );
647 : }
648 :
649 0 : if ( flag_cna == 0 )
650 : {
651 0 : if ( st->last_flag_cna == 1 && ( ( st->last_core == ACELP_CORE && st->last_coder_type != AUDIO ) || st->last_core == AMR_WB_CORE ) )
652 : {
653 0 : v_multc( st->hFdCngDec->hFdCngCom->olapBufferSynth2 + 5 * L_FRAME / 4, 256.f, tmp_buffer, L_FRAME / 2 );
654 0 : v_add( tmp_buffer, syn, syn, L_FRAME / 2 );
655 : }
656 0 : set_f( st->hFdCngDec->hFdCngCom->olapBufferSynth2, 0.f, L_FRAME * 2 );
657 : }
658 :
659 : /*----------------------------------------------------------------*
660 : * Change the sampling frequency to 8/16/32 kHz
661 : * Bass post-filter
662 : *----------------------------------------------------------------*/
663 :
664 : /* check if the cldfb works on the right sample rate */
665 0 : if ( ( st->cldfbAna->no_channels * st->cldfbAna->no_col ) != L_FRAME )
666 : {
667 : /* resample to ACELP internal sampling rate */
668 0 : resampleCldfb( st->cldfbAna, INT_FS_12k8 );
669 0 : resampleCldfb( st->cldfbBPF, INT_FS_12k8 );
670 :
671 0 : if ( st->ini_frame > 0 )
672 : {
673 0 : st->cldfbSyn->bandsToZero = st->cldfbSyn->no_channels - st->cldfbAna->no_channels;
674 : }
675 : }
676 :
677 : /* bass post-filter */
678 0 : bass_psfilter( st->hBPF, st->Opt_AMR_WB, syn, L_FRAME, pitch_buf, st->bpf_off, st->stab_fac, &st->stab_fac_smooth, GENERIC, bpf_error_signal );
679 :
680 : /* analysis of the synthesis at internal sampling rate */
681 0 : cldfbAnalysis( syn, realBuffer, imagBuffer, -1, st->cldfbAna );
682 :
683 : /* analysis and add the BPF error signal */
684 0 : addBassPostFilter( bpf_error_signal, st->bpf_off ? 0 : -1, realBuffer, imagBuffer, st->cldfbBPF );
685 :
686 0 : if ( st->cldfbSyn->bandsToZero != st->cldfbSyn->no_channels - st->cldfbAna->no_channels )
687 : {
688 : /* in case of BW switching, re-init to default */
689 0 : st->cldfbSyn->bandsToZero = st->cldfbSyn->no_channels - st->cldfbAna->no_channels;
690 : }
691 :
692 0 : cldfb_synth_set_bandsToZero( st, realBuffer, imagBuffer, CLDFB_NO_COL_MAX );
693 :
694 : /* synthesis of the combined signal */
695 0 : cldfbSynthesis( realBuffer, imagBuffer, synth_out, -1, st->cldfbSyn );
696 :
697 : /* save synthesis - needed in case of core switching */
698 0 : mvr2r( synth_out, st->previoussynth, output_frame );
699 :
700 : /*--------------------------------------------------------*
701 : * calculate the average frame energy
702 : *--------------------------------------------------------*/
703 :
704 0 : fer_energy( L_FRAME, st->clas_dec, syn, pitch_buf[3], &ng_ener, L_FRAME );
705 :
706 : /*--------------------------------------------------------*
707 : * optimized for NO_S@-26dBov with street noise @ SNR=25dB
708 : *--------------------------------------------------------*/
709 :
710 0 : ng_ener = 10.0f * (float) log10( ng_ener + 0.01f ) - 90.3087f + 15;
711 0 : st->ng_ener_ST = 0.7f * st->ng_ener_ST + 0.3f * ng_ener;
712 :
713 : /*-----------------------------------------------------------------*
714 : * Bandwidth extension 6kHz-8kHz
715 : *-----------------------------------------------------------------*/
716 :
717 0 : if ( output_frame >= L_FRAME16k && ( ( st->cldfbSyn->bandsToZero - st->cldfbSyn->no_channels + 10 ) != 0 || st->last_flag_filter_NB != 1 ) )
718 : {
719 0 : hf_synth_amr_wb( st->hAmrwb_IO, st->hBWE_zero, st->core_brate, output_frame, Aq, exc2, syn, &amr_io_class, synth_out, class_para, hf_gain, voice_factors, pitch_buf, st->ng_ener_ST, lsf_new );
720 : }
721 : else
722 : {
723 0 : hf_synth_amr_wb_reset( st->hAmrwb_IO, st->hBWE_zero );
724 : }
725 :
726 : /*----------------------------------------------------------------------*
727 : * Updates
728 : *----------------------------------------------------------------------*/
729 :
730 0 : updt_dec( st, old_exc, pitch_buf, 0, Aq, lsf_new, lsp_new, voice_factors, dummy_buf, gain_buf );
731 :
732 : /* update old_Aq[] - needed in improv_amr_wb_gs_fx() */
733 0 : mvr2r( Aq, st->hAmrwb_IO->old_Aq, NB_SUBFR * ( M + 1 ) );
734 :
735 0 : if ( !st->bfi && st->prev_bfi && st->last_total_brate >= HQ_48k && st->last_codec_mode == MODE2 && ( st->last_core_bfi == TCX_20_CORE || st->last_core_bfi == TCX_10_CORE ) && st->hPlcInfo->concealment_method == TCX_NONTONAL && st->hPlcInfo->nbLostCmpt < 4 )
736 : {
737 0 : waveadj_rec = 1;
738 : }
739 :
740 0 : st->last_vad = vad_flag;
741 :
742 : /* Update main codec parameters */
743 0 : updt_dec_common( st, -1, -1, NULL );
744 :
745 : /*----------------------------------------------------------------*
746 : * Overlap of ACELP synthesis with old MDCT memory
747 : *----------------------------------------------------------------*/
748 :
749 0 : if ( st->bfi )
750 : {
751 : /* calculate another loss frame to fill gap in case of switching frame loss */
752 0 : if ( ( error = acelp_core_switch_dec_bfi( st ) ) != IVAS_ERR_OK )
753 : {
754 0 : return error;
755 : }
756 : }
757 :
758 0 : delay_comp = NS2SA( st->output_Fs, DELAY_CLDFB_NS );
759 0 : if ( last_core_ori == HQ_CORE )
760 : {
761 0 : nZeros = (int16_t) ( NS2SA( st->output_Fs, N_ZERO_MDCT_NS ) );
762 :
763 0 : if ( st->prev_bfi && st->hHQ_core->HqVoicing )
764 : {
765 0 : mvr2r( st->hHQ_core->fer_samples, st->hHQ_core->old_out + nZeros, NS2SA( st->output_Fs, 6000000 ) );
766 : }
767 :
768 : /* copy the HQ/ACELP delay synchroniation buffer to the beginning of ACELP frame */
769 0 : mvr2r( st->delay_buf_out, synth_out, delay_comp );
770 :
771 0 : tmp = 1.0f / (float) NS2SA( st->output_Fs, 6000000 );
772 0 : for ( i = 0; i < NS2SA( st->output_Fs, 6000000 ); i++ )
773 : {
774 0 : synth_out[i + delay_comp] = ( 1 - tmp * (float) i ) * st->hHQ_core->old_out[i + nZeros] + tmp * (float) i * synth_out[i + delay_comp];
775 : }
776 : }
777 :
778 : /*----------------------------------------------------------------*
779 : * HP filtering
780 : * Final synthesis output
781 : *----------------------------------------------------------------*/
782 :
783 : /* Delay ACELP synthesis by DELAY_BWE_TOTAL_NS - DELAY_CLDFB_NS delay */
784 0 : if ( output_frame >= L_FRAME16k )
785 : {
786 0 : tmps = NS2SA( st->output_Fs, DELAY_BWE_TOTAL_NS - DELAY_CLDFB_NS );
787 0 : delay_signal( synth_out, output_frame, st->prev_synth_buffer, tmps );
788 : }
789 :
790 0 : if ( waveadj_rec )
791 : {
792 0 : tmps = 0;
793 0 : if ( output_frame >= L_FRAME16k )
794 : {
795 0 : tmps = NS2SA( st->output_Fs, DELAY_BWE_TOTAL_NS );
796 : }
797 0 : waveform_adj2( st->hPlcInfo, st->hTonalMDCTConc->secondLastPcmOut, synth_out + tmps, tmps, st->hPlcInfo->nbLostCmpt + 1, st->bfi );
798 : }
799 :
800 : /* HP filter */
801 0 : hp20( synth_out, output_frame, mem_hp20_out, st->output_Fs );
802 :
803 : /* save synthesis for core switching */
804 0 : mvr2r( synth_out + NS2SA( st->output_Fs, ACELP_LOOK_NS + DELAY_BWE_TOTAL_NS ), st->old_synth_sw, NS2SA( st->output_Fs, FRAME_SIZE_NS - ACELP_LOOK_NS - DELAY_BWE_TOTAL_NS ) );
805 :
806 : /* TCX-LTP Postfilter: used in AMR-WB IO to update memories and to avoid discontinuities when the past frame was TCX */
807 0 : tcx_ltp_post( st, hTcxLtpDec, ACELP_CORE, output_frame, 0, synth_out, NULL );
808 :
809 : /* final output of synthesis signal */
810 0 : mvr2r( synth_out, output, output_frame );
811 :
812 : #ifdef DEBUG_MODE_INFO
813 : dbgwrite( &st->clas_dec, sizeof( int16_t ), 1, output_frame, "res/clas.dec" );
814 : dbgwrite( &st->codec_mode, sizeof( int16_t ), 1, output_frame, "res/codec.dec" );
815 : dbgwrite( &st->core, sizeof( int16_t ), 1, output_frame, "res/core.dec" );
816 : dbgwrite( &st->extl, sizeof( int16_t ), 1, output_frame, "res/extl.dec" );
817 : dbgwrite( &st->bwidth, sizeof( int16_t ), 1, output_frame, "res/bwidth.dec" );
818 : dbgwrite( &st->cng_type, sizeof( int16_t ), 1, output_frame, "res/cng_type.dec" );
819 : tmp = st->core_brate / 1000.0f;
820 : dbgwrite( &tmp, sizeof( float ), 1, output_frame, "res/core_brate.dec" );
821 : dbgwrite( &tmp, sizeof( float ), 1, output_frame, "res/total_brate.dec" );
822 : dbgwrite( &st->coder_type, sizeof( int16_t ), 1, output_frame, "res/coder_type.dec" );
823 : dbgwrite( &st->L_frame, sizeof( int16_t ), 1, output_frame, "res/L_frame.dec" );
824 : dbgwrite( &st->bfi, sizeof( int16_t ), 1, output_frame, "res/bfi" );
825 : #endif
826 :
827 0 : return error;
828 : }
829 :
830 :
831 : /*------------------------------------------------------------------*
832 : * amr_wb_dec_init()
833 : *
834 : * AMR-WB decoder initialization
835 : *------------------------------------------------------------------*/
836 :
837 9 : void amr_wb_dec_init(
838 : AMRWB_IO_DEC_HANDLE hAmrwb_IO /* i/o: AMR-WB IO data handle */
839 : )
840 : {
841 : /* gain quantization memory (used in AMR-WB IO mode) */
842 9 : set_f( hAmrwb_IO->past_qua_en, -14.0f, GAIN_PRED_ORDER );
843 :
844 : /* Improvement of unvoiced and audio signals in AMR-WB IO mode */
845 9 : hAmrwb_IO->UV_cnt = 30;
846 9 : hAmrwb_IO->LT_UV_cnt = 60.0f;
847 9 : set_f( hAmrwb_IO->lt_diff_etot, 0, MAX_LT );
848 9 : hAmrwb_IO->Last_ener = 0.0f;
849 :
850 9 : set_f( hAmrwb_IO->old_Aq, 0, NB_SUBFR * ( M + 1 ) );
851 9 : hAmrwb_IO->old_Aq[0] = 1.0f;
852 9 : hAmrwb_IO->old_Aq[M + 1] = 1.0f;
853 9 : hAmrwb_IO->old_Aq[2 * ( M + 1 )] = 1.0f;
854 9 : hAmrwb_IO->old_Aq[3 * ( M + 1 )] = 1.0f;
855 :
856 9 : hAmrwb_IO->lt_voice_fac = 0.0f;
857 :
858 9 : return;
859 : }
|