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 4040 : 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 4040 : 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 4040 : int16_t waveadj_rec = 0;
102 4040 : TCX_LTP_DEC_HANDLE hTcxLtpDec = st->hTcxLtpDec;
103 : ivas_error error;
104 :
105 4040 : error = IVAS_ERR_OK;
106 :
107 : /*------------------------------------------------------------------*
108 : * Initialization
109 : *------------------------------------------------------------------*/
110 :
111 4040 : st->idchan = 0;
112 :
113 4040 : syn_tmp[0] = 0;
114 4040 : syn_tmp[1] = 0;
115 4040 : syn = syn_tmp + 2;
116 :
117 68680 : for ( i = 0; i < CLDFB_NO_COL_MAX; i++ )
118 : {
119 64640 : set_f( realBufferTmp[i], 0, CLDFB_NO_CHANNELS_MAX );
120 64640 : set_f( imagBufferTmp[i], 0, CLDFB_NO_CHANNELS_MAX );
121 64640 : realBuffer[i] = realBufferTmp[i];
122 64640 : imagBuffer[i] = imagBufferTmp[i];
123 : }
124 :
125 4040 : set_f( gain_buf, 0, NB_SUBFR16k );
126 :
127 4040 : st->use_partial_copy = 0;
128 4040 : st->rf_frame_type = RF_NO_DATA;
129 4040 : st->rf_flag = 0;
130 4040 : st->rf_flag_last = 0;
131 :
132 4040 : st->L_frame = L_FRAME;
133 4040 : st->nb_subfr = NB_SUBFR;
134 4040 : st->core = AMR_WB_CORE;
135 4040 : st->core_brate = st->total_brate;
136 4040 : st->extl = -1;
137 4040 : st->bwidth = WB;
138 4040 : st->coder_type = GENERIC;
139 4040 : output_frame = (int16_t) ( st->output_Fs / FRAMES_PER_SEC ); /* frame length of the input signal */
140 :
141 4040 : st->bpf_off = 0;
142 4040 : if ( st->last_core == HQ_CORE )
143 : {
144 0 : st->bpf_off = 1;
145 0 : st->hPFstat->on = 0;
146 : }
147 4040 : st->igf = 0;
148 :
149 4040 : st->sr_core = st->L_frame * FRAMES_PER_SEC;
150 4040 : st->fscale_old = st->fscale;
151 4040 : st->fscale = sr2fscale( st->sr_core );
152 :
153 : /* Initialization in case that the first frame is the good received AMR-WB (IO) frame */
154 4040 : if ( st->ini_frame == 0 )
155 : {
156 88 : st->last_core = AMR_WB_CORE;
157 88 : mvr2r( mean_isf_amr_wb, st->lsf_old, M );
158 88 : 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 4040 : 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 4040 : 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 4040 : last_core_ori = st->last_core;
175 4040 : set_s( hf_gain, 0, NB_SUBFR );
176 :
177 4040 : enr_q = 0.0f;
178 4040 : tmp_noise = 0.0f;
179 4040 : amr_io_class = UNVOICED_CLAS;
180 :
181 4040 : mvr2r( st->old_exc, old_exc, L_EXC_MEM_DEC );
182 4040 : exc = old_exc + L_EXC_MEM_DEC;
183 :
184 : /* reset post-filter in case of switching */
185 4040 : if ( st->hPFstat->on == 0 )
186 : {
187 176 : st->hPFstat->reset = 1;
188 : }
189 :
190 4040 : if ( st->bfi )
191 : {
192 440 : st->nbLostCmpt++;
193 : }
194 : else
195 : {
196 3600 : st->nbLostCmpt = 0;
197 : }
198 :
199 : /* PLC: [TCX: Fade-out-recovery]
200 : * PLC: overlapping part needs to be attenuated for first good frame */
201 4040 : 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 4040 : st->rate_switching_reset = 0;
211 :
212 4040 : 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 4040 : 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 4040 : 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 4040 : if ( st->last_core != AMR_WB_CORE )
299 : {
300 : /* reset the unvoiced/audio signal improvement memories */
301 0 : isp2a( st->lsp_old, st->hAmrwb_IO->old_Aq, M );
302 0 : mvr2r( st->hAmrwb_IO->old_Aq, st->hAmrwb_IO->old_Aq + ( M + 1 ), M + 1 );
303 0 : mvr2r( st->hAmrwb_IO->old_Aq, st->hAmrwb_IO->old_Aq + 2 * ( M + 1 ), M + 1 );
304 0 : mvr2r( st->hAmrwb_IO->old_Aq, st->hAmrwb_IO->old_Aq + 3 * ( M + 1 ), M + 1 );
305 : }
306 :
307 4040 : if ( st->last_bwidth == NB && st->ini_frame != 0 )
308 : {
309 0 : st->rate_switching_reset = 1;
310 : }
311 :
312 : /*----------------------------------------------------------------------*
313 : * GOOD frame
314 : *----------------------------------------------------------------------*/
315 :
316 4040 : if ( !st->bfi )
317 : {
318 : /*----------------------------------------------------------------*
319 : * Processing of FRAME_NO_DATA frames
320 : * Decoding of SID frames
321 : *----------------------------------------------------------------*/
322 :
323 3600 : if ( st->core_brate == FRAME_NO_DATA || st->core_brate == SID_1k75 )
324 : {
325 : /* decode CNG parameters */
326 0 : CNG_dec( st, EVS_MONO, Aq, lsp_new, lsf_new, &allow_cn_step, &sid_bw, q_env );
327 :
328 : /* comfort noise generation */
329 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 );
330 0 : set_f( voice_factors, 1.0f, NB_SUBFR );
331 0 : class_para = 0.0f;
332 :
333 : /* update past excitation signals for LD music post-filter */
334 0 : mvr2r( st->hMusicPF->dct_post_old_exc + L_FRAME, st->hMusicPF->dct_post_old_exc, DCT_L_POST - L_FRAME - OFFSET2 );
335 0 : mvr2r( exc2, st->hMusicPF->dct_post_old_exc + ( DCT_L_POST - L_FRAME - OFFSET2 ), L_FRAME );
336 :
337 : /* synthesis at 12k8 Hz sampling rate */
338 0 : syn_12k8( L_FRAME, Aq, exc2, syn, st->mem_syn2, 1 );
339 0 : syn_12k8( st->L_frame, Aq, exc2, dummy_buf, st->mem_syn3, 1 );
340 :
341 : /* reset the decoder */
342 0 : CNG_reset_dec( st, pitch_buf, dummy_buf + L_FRAME );
343 :
344 : /* update st->mem_syn1 for ACELP core switching */
345 0 : mvr2r( st->mem_syn3, st->mem_syn1, M );
346 0 : if ( output_frame != L_FRAME8k )
347 : {
348 0 : frame_e = 10.0f * (float) log10( dotp( syn, syn, L_FRAME ) / (float) L_FRAME );
349 0 : st->psf_lp_noise = 0.99f * st->psf_lp_noise + 0.01f * frame_e;
350 : }
351 :
352 : /* update old synthesis for classification */
353 0 : mvr2r( syn + L_FRAME - L_SYN_MEM_CLAS_ESTIM, st->mem_syn_clas_estim, L_SYN_MEM_CLAS_ESTIM );
354 :
355 : /* Update music post processing values */
356 : /* Filter energies update */
357 0 : for ( i = 0; i < DCT_L_POST; i++ )
358 : {
359 0 : st->hMusicPF->filt_lfE[i] = 0.3f + 0.7f * st->hMusicPF->filt_lfE[i];
360 : }
361 :
362 0 : vad_flag = 0;
363 : }
364 :
365 : /*----------------------------------------------------------------*
366 : * Decoding of all other frames
367 : *----------------------------------------------------------------*/
368 :
369 : else
370 : {
371 : /*-----------------------------------------------------------------*
372 : * After CNG period, use the most up-to-date ISPs
373 : *-----------------------------------------------------------------*/
374 :
375 3600 : if ( st->last_core_brate == FRAME_NO_DATA || st->last_core_brate == SID_1k75 )
376 : {
377 0 : mvr2r( st->lspCNG, st->lsp_old, M );
378 0 : isp2isf( st->lspCNG, st->lsf_old, M, INT_FS_12k8 );
379 0 : set_f( old_exc, 0, L_EXC_MEM_DEC );
380 : }
381 :
382 : /*------------------------------------------------------------*
383 : * Extracts VAD information from the bitstream in AMR-WB IO mode
384 : *------------------------------------------------------------*/
385 :
386 3600 : vad_flag = get_next_indice( st, 1 );
387 :
388 3600 : if ( vad_flag == 0 )
389 : {
390 0 : st->coder_type = INACTIVE;
391 : }
392 : else
393 : {
394 3600 : st->coder_type = GENERIC;
395 : }
396 :
397 : /*-----------------------------------------------------------------*
398 : * ISF de-quantization and interpolation
399 : *-----------------------------------------------------------------*/
400 :
401 3600 : isf_dec_amr_wb( st, Aq, lsf_new, lsp_new );
402 :
403 : /*------------------------------------------------------------*
404 : * Decode excitation
405 : *------------------------------------------------------------*/
406 :
407 3600 : decod_amr_wb( st, Aq, pitch_buf, exc, exc2, hf_gain, voice_factors, gain_buf );
408 :
409 : /* synthesis for ACELP core switching and SWB BWE */
410 3600 : syn_12k8( L_FRAME, Aq, exc, tmp_buffer, st->mem_syn1, 1 );
411 :
412 : /*------------------------------------------------------------*
413 : * Update long-term energies for FEC
414 : * Update ISP vector for CNG
415 : *------------------------------------------------------------*/
416 :
417 3600 : if ( st->coder_type == INACTIVE )
418 : {
419 0 : if ( st->unv_cnt > 20 )
420 : {
421 0 : ftmp = st->lp_gainc * st->lp_gainc;
422 0 : st->lp_ener = 0.7f * st->lp_ener + 0.3f * ftmp;
423 0 : for ( i = 0; i < M; i++ )
424 : {
425 0 : st->lspCNG[i] = (float) ( 0.9f * st->lspCNG[i] + 0.1f * lsp_new[i] );
426 : }
427 : }
428 : else
429 : {
430 0 : st->unv_cnt++;
431 : }
432 : }
433 : else
434 : {
435 3600 : st->unv_cnt = 0;
436 : }
437 :
438 : /*------------------------------------------------------------*
439 : * Save filter memory in case the synthesis is redone after scaling
440 : * Core synthesis at 12k8 Hz
441 : *------------------------------------------------------------*/
442 :
443 3600 : mvr2r( st->mem_syn2, mem_tmp, M );
444 3600 : syn_12k8( L_FRAME, Aq, exc2, syn, st->mem_syn2, 1 );
445 :
446 : /*------------------------------------------------------------*
447 : * FEC - Estimate the classification information
448 : *------------------------------------------------------------*/
449 :
450 3600 : 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 );
451 :
452 : /* update past excitation signals for LD music post-filter */
453 3600 : mvr2r( st->hMusicPF->dct_post_old_exc + L_FRAME, st->hMusicPF->dct_post_old_exc, DCT_L_POST - L_FRAME - OFFSET2 );
454 3600 : mvr2r( exc2, st->hMusicPF->dct_post_old_exc + ( DCT_L_POST - L_FRAME - OFFSET2 ), L_FRAME );
455 3600 : mvr2r( st->hMusicPF->dct_post_old_exc, exc_buffer, DCT_L_POST - OFFSET2 );
456 :
457 3600 : if ( output_frame != L_FRAME8k )
458 : {
459 3600 : if ( st->coder_type == INACTIVE )
460 : {
461 0 : frame_energy( L_FRAME, pitch_buf, syn, 0.0f, &frame_e );
462 0 : st->psf_lp_noise = 0.99f * st->psf_lp_noise + 0.01f * frame_e;
463 : }
464 : }
465 :
466 3600 : if ( amr_io_class != UNVOICED_CLAS && st->coder_type != INACTIVE && st->psf_lp_noise < 15.0f )
467 3034 : {
468 3034 : int16_t tmp_coder_type = AUDIO;
469 3034 : if ( st->last_coder_type == INACTIVE || st->last_coder_type == UNVOICED )
470 : {
471 0 : tmp_coder_type = INACTIVE;
472 : }
473 : /* Extrapolation of the last future part, windowing and high resolution DCT transform */
474 3034 : Prep_music_postP( exc_buffer, dct_buffer, st->hMusicPF->filt_lfE, st->last_core, pitch_buf, st->hMusicPF->LDm_enh_lp_gbin );
475 :
476 : /* LD music post-filter */
477 3034 : LD_music_post_filter( st->hMusicPF, dct_buffer, dct_buffer, st->core_brate, -1, tmp_coder_type );
478 :
479 : /* Inverse DCT transform, retrieval of the aligned excitation, re-synthesis */
480 3034 : Post_music_postP( dct_buffer, exc_buffer, exc2, mem_tmp, st->mem_syn2, Aq, syn );
481 : }
482 : else
483 : {
484 : /*------------------------------------------------------------*
485 : * Improvement for unvoiced and audio signals
486 : *------------------------------------------------------------*/
487 :
488 566 : 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 );
489 :
490 362806 : for ( i = 0; i < DCT_L_POST; i++ )
491 : {
492 362240 : st->hMusicPF->filt_lfE[i] = 0.3f + 0.7f * st->hMusicPF->filt_lfE[i];
493 : }
494 : }
495 :
496 : /*------------------------------------------------------------*
497 : * FEC - Estimate pitch
498 : *------------------------------------------------------------*/
499 :
500 3600 : 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 );
501 :
502 : /*------------------------------------------------------------*
503 : * FEC - Smooth the speech energy evolution when recovering after a BAD frame
504 : * (smoothing is performed in the excitation domain and signal is resynthesized after)
505 : *------------------------------------------------------------*/
506 :
507 3600 : 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 );
508 :
509 : /* estimate the pitch-synchronous speech energy per sample to be used when normal operation recovers */
510 3600 : fer_energy( L_FRAME, st->clas_dec, syn, pitch_buf[3], &st->enr_old, L_FRAME );
511 : }
512 :
513 : } /* End of GOOD FRAME */
514 :
515 : /*----------------------------------------------------------------*
516 : * BAD frame
517 : *----------------------------------------------------------------*/
518 :
519 : else
520 : {
521 : /* long burst frame erasures */
522 440 : if ( st->nbLostCmpt > 5 && st->clas_dec >= VOICED_CLAS )
523 : {
524 40 : st->last_good = VOICED_TRANSITION;
525 : }
526 :
527 440 : vad_flag = st->last_vad;
528 440 : amr_io_class = st->last_good;
529 440 : class_para = 0.0f;
530 :
531 : /* LSF estimation and A(z) calculation */
532 440 : 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 );
533 :
534 440 : FEC_lsf2lsp_interp( st, L_FRAME, Aq, lsf_new, lsp_new );
535 :
536 : /* calculation of excitation signal */
537 440 : FEC_exc_estim( st, L_FRAME, exc, exc2, tmp_buffer, pitch_buf, voice_factors, &FEC_pitch, dummy_buf, lsf_new, &tmp_noise );
538 :
539 : /* synthesis for ACELP core switching and SWB BWE */
540 440 : syn_12k8( L_FRAME, Aq, exc, tmp_buffer, st->mem_syn1, 1 );
541 :
542 : /* update past excitation signals */
543 440 : mvr2r( st->hMusicPF->dct_post_old_exc + L_FRAME, st->hMusicPF->dct_post_old_exc, DCT_L_POST - L_FRAME - OFFSET2 );
544 440 : mvr2r( exc2, st->hMusicPF->dct_post_old_exc + ( DCT_L_POST - L_FRAME - OFFSET2 ), L_FRAME );
545 :
546 : /* Update music post processing values */
547 : /* Update circular buffer, keep last energy difference unchanged */
548 17600 : for ( i = 1; i < MAX_LT; i++ )
549 : {
550 17160 : st->hMusicPF->LDm_lt_diff_etot[i - 1] = st->hMusicPF->LDm_lt_diff_etot[i];
551 : }
552 : /* Filter energies update */
553 282040 : for ( i = 0; i < DCT_L_POST; i++ )
554 : {
555 281600 : st->hMusicPF->filt_lfE[i] = 0.3f + 0.7f * st->hMusicPF->filt_lfE[i];
556 : }
557 :
558 : /* synthesis at 12k8 Hz sampling rate */
559 440 : mvr2r( st->mem_syn2, mem_tmp, M );
560 440 : syn_12k8( L_FRAME, Aq, exc2, syn, st->mem_syn2, 1 );
561 :
562 : /* update old synthesis for classification */
563 440 : mvr2r( syn + L_FRAME - L_SYN_MEM_CLAS_ESTIM, st->mem_syn_clas_estim, L_SYN_MEM_CLAS_ESTIM );
564 :
565 :
566 : /*------------------------------------------------------------*
567 : * FEC - Smooth the speech energy evolution when recovering after a BAD frame
568 : * (smoothing is performed in the excitation domain and signal is resynthesized after)
569 : *------------------------------------------------------------*/
570 :
571 440 : 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 );
572 :
573 : /* estimate the pitch-synchronous speech energy per sample to be used when normal operation recovers */
574 440 : fer_energy( L_FRAME, st->last_good, syn, FEC_pitch, &st->enr_old, L_FRAME );
575 : }
576 :
577 : /*--------------------------------------------------------*
578 : * NB post-filter
579 : *--------------------------------------------------------*/
580 :
581 4040 : if ( output_frame == L_FRAME8k || st->last_bwidth == NB )
582 : {
583 88 : if ( output_frame == L_FRAME8k )
584 : {
585 0 : st->hPFstat->on = 1;
586 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 );
587 : }
588 : else
589 : {
590 88 : st->hPFstat->on = 0;
591 88 : nb_post_filt( L_FRAME, L_SUBFR, st->hPFstat, &st->psf_lp_noise, tmp_noise, syn, Aq, pitch_buf, AUDIO, st->BER_detect, 0 );
592 : }
593 : }
594 :
595 : /*------------------------------------------------------------------*
596 : * Perform fixed deemphasis through 1/(1 - g*z^-1)
597 : *-----------------------------------------------------------------*/
598 :
599 : /* update old synthesis buffer - needed for ACELP internal sampling rate switching */
600 4040 : mvr2r( syn + L_FRAME - L_SYN_MEM, st->mem_syn_r, L_SYN_MEM );
601 :
602 :
603 4040 : deemph( syn, PREEMPH_FAC, L_FRAME, &( st->mem_deemph ) );
604 4040 : AGC_dec( syn, st->agc_mem2, L_FRAME );
605 4040 : if ( st->hTcxDec != NULL )
606 : {
607 4040 : mvr2r( syn + L_FRAME / 2, st->hTcxDec->old_syn_Overl, L_FRAME / 2 );
608 : }
609 4040 : mvr2r( syn + L_FRAME - M - 1, st->syn, M + 1 );
610 :
611 : /*------------------------------------------------------------------*
612 : * Formant post-filter
613 : *-----------------------------------------------------------------*/
614 :
615 4040 : mvr2r( syn, tmp_buffer + L_SYN_MEM, L_FRAME );
616 :
617 4040 : if ( output_frame != L_FRAME8k && st->last_bwidth != NB )
618 : {
619 3952 : st->hPFstat->on = 1;
620 3952 : 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 );
621 : }
622 :
623 : /*----------------------------------------------------------------*
624 : * Comfort Noise Addition
625 : *----------------------------------------------------------------*/
626 :
627 4040 : flag_cna = 0;
628 4040 : if ( st->psf_lp_noise >= 15.f || st->coder_type == INACTIVE )
629 : {
630 : /*VAD only for non inactive frame*/
631 0 : st->VAD = ( st->VAD && ( st->coder_type != INACTIVE ) );
632 :
633 0 : ApplyFdCng( syn, NULL, NULL, NULL, st, 0, 0 );
634 :
635 0 : st->hFdCngDec->hFdCngCom->frame_type_previous = st->m_frame_type;
636 :
637 : /*Noisy speech detector*/
638 0 : noisy_speech_detection( st->hFdCngDec, st->VAD, syn );
639 :
640 0 : st->hFdCngDec->hFdCngCom->likelihood_noisy_speech = 0.99f * st->hFdCngDec->hFdCngCom->likelihood_noisy_speech + 0.01f * (float) st->hFdCngDec->hFdCngCom->flag_noisy_speech;
641 0 : st->lp_noise = st->hFdCngDec->lp_noise;
642 :
643 0 : if ( st->flag_cna && ( st->psf_lp_noise >= 15.f ) )
644 : {
645 0 : flag_cna = 1;
646 0 : generate_masking_noise( syn, st->hFdCngDec->hFdCngCom, st->hFdCngDec->hFdCngCom->frameSize, AMR_WB_CORE, 0, 0, st->element_mode, NULL, -1 );
647 : }
648 0 : else if ( st->flag_cna )
649 : {
650 0 : generate_masking_noise_update_seed( st->hFdCngDec->hFdCngCom );
651 : }
652 : }
653 4040 : else if ( st->flag_cna )
654 : {
655 944 : generate_masking_noise_update_seed( st->hFdCngDec->hFdCngCom );
656 : }
657 :
658 4040 : if ( flag_cna == 0 )
659 : {
660 4040 : if ( st->last_flag_cna == 1 && ( ( st->last_core == ACELP_CORE && st->last_coder_type != AUDIO ) || st->last_core == AMR_WB_CORE ) )
661 : {
662 912 : v_multc( st->hFdCngDec->hFdCngCom->olapBufferSynth2 + 5 * L_FRAME / 4, 256.f, tmp_buffer, L_FRAME / 2 );
663 912 : v_add( tmp_buffer, syn, syn, L_FRAME / 2 );
664 : }
665 4040 : set_f( st->hFdCngDec->hFdCngCom->olapBufferSynth2, 0.f, L_FRAME * 2 );
666 : }
667 :
668 : /*----------------------------------------------------------------*
669 : * Change the sampling frequency to 8/16/32 kHz
670 : * Bass post-filter
671 : *----------------------------------------------------------------*/
672 :
673 : /* check if the cldfb works on the right sample rate */
674 4040 : if ( ( st->cldfbAna->no_channels * st->cldfbAna->no_col ) != L_FRAME )
675 : {
676 : /* resample to ACELP internal sampling rate */
677 0 : resampleCldfb( st->cldfbAna, INT_FS_12k8 );
678 0 : resampleCldfb( st->cldfbBPF, INT_FS_12k8 );
679 :
680 0 : if ( st->ini_frame > 0 )
681 : {
682 0 : st->cldfbSyn->bandsToZero = st->cldfbSyn->no_channels - st->cldfbAna->no_channels;
683 : }
684 : }
685 :
686 : /* bass post-filter */
687 4040 : 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 );
688 :
689 : /* analysis of the synthesis at internal sampling rate */
690 4040 : cldfbAnalysis( syn, realBuffer, imagBuffer, -1, st->cldfbAna );
691 :
692 : /* analysis and add the BPF error signal */
693 4040 : addBassPostFilter( bpf_error_signal, st->bpf_off ? 0 : -1, realBuffer, imagBuffer, st->cldfbBPF );
694 :
695 4040 : if ( st->cldfbSyn->bandsToZero != st->cldfbSyn->no_channels - st->cldfbAna->no_channels )
696 : {
697 : /* in case of BW switching, re-init to default */
698 88 : st->cldfbSyn->bandsToZero = st->cldfbSyn->no_channels - st->cldfbAna->no_channels;
699 : }
700 :
701 4040 : cldfb_synth_set_bandsToZero( st, realBuffer, imagBuffer, CLDFB_NO_COL_MAX );
702 :
703 : /* synthesis of the combined signal */
704 4040 : cldfbSynthesis( realBuffer, imagBuffer, synth_out, -1, st->cldfbSyn );
705 :
706 : /* save synthesis - needed in case of core switching */
707 4040 : mvr2r( synth_out, st->previoussynth, output_frame );
708 :
709 : /*--------------------------------------------------------*
710 : * calculate the average frame energy
711 : *--------------------------------------------------------*/
712 :
713 4040 : fer_energy( L_FRAME, st->clas_dec, syn, pitch_buf[3], &ng_ener, L_FRAME );
714 :
715 : /*--------------------------------------------------------*
716 : * optimized for NO_S@-26dBov with street noise @ SNR=25dB
717 : *--------------------------------------------------------*/
718 :
719 4040 : ng_ener = 10.0f * (float) log10( ng_ener + 0.01f ) - 90.3087f + 15;
720 4040 : st->ng_ener_ST = 0.7f * st->ng_ener_ST + 0.3f * ng_ener;
721 :
722 : /*-----------------------------------------------------------------*
723 : * Bandwidth extension 6kHz-8kHz
724 : *-----------------------------------------------------------------*/
725 :
726 4040 : if ( output_frame >= L_FRAME16k && ( ( st->cldfbSyn->bandsToZero - st->cldfbSyn->no_channels + 10 ) != 0 || st->last_flag_filter_NB != 1 ) )
727 : {
728 4040 : 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 );
729 : }
730 : else
731 : {
732 0 : hf_synth_amr_wb_reset( st->hAmrwb_IO, st->hBWE_zero );
733 : }
734 :
735 : /*----------------------------------------------------------------------*
736 : * Updates
737 : *----------------------------------------------------------------------*/
738 :
739 4040 : updt_dec( st, old_exc, pitch_buf, 0, Aq, lsf_new, lsp_new, voice_factors, dummy_buf, gain_buf );
740 :
741 : /* update old_Aq[] - needed in improv_amr_wb_gs_fx() */
742 4040 : mvr2r( Aq, st->hAmrwb_IO->old_Aq, NB_SUBFR * ( M + 1 ) );
743 :
744 4040 : 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 )
745 : {
746 0 : waveadj_rec = 1;
747 : }
748 :
749 4040 : st->last_vad = vad_flag;
750 :
751 : /* Update main codec parameters */
752 4040 : updt_dec_common( st, -1, -1, NULL );
753 :
754 : /*----------------------------------------------------------------*
755 : * Overlap of ACELP synthesis with old MDCT memory
756 : *----------------------------------------------------------------*/
757 :
758 4040 : if ( st->bfi )
759 : {
760 : /* calculate another loss frame to fill gap in case of switching frame loss */
761 440 : if ( ( error = acelp_core_switch_dec_bfi( st ) ) != IVAS_ERR_OK )
762 : {
763 0 : return error;
764 : }
765 : }
766 :
767 4040 : delay_comp = NS2SA( st->output_Fs, DELAY_CLDFB_NS );
768 4040 : if ( last_core_ori == HQ_CORE )
769 : {
770 0 : nZeros = (int16_t) ( NS2SA( st->output_Fs, N_ZERO_MDCT_NS ) );
771 :
772 0 : if ( st->prev_bfi && st->hHQ_core->HqVoicing )
773 : {
774 0 : mvr2r( st->hHQ_core->fer_samples, st->hHQ_core->old_out + nZeros, NS2SA( st->output_Fs, 6000000 ) );
775 : }
776 :
777 : /* copy the HQ/ACELP delay synchroniation buffer to the beginning of ACELP frame */
778 0 : mvr2r( st->delay_buf_out, synth_out, delay_comp );
779 :
780 0 : tmp = 1.0f / (float) NS2SA( st->output_Fs, 6000000 );
781 0 : for ( i = 0; i < NS2SA( st->output_Fs, 6000000 ); i++ )
782 : {
783 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];
784 : }
785 : }
786 :
787 : /*----------------------------------------------------------------*
788 : * HP filtering
789 : * Final synthesis output
790 : *----------------------------------------------------------------*/
791 :
792 : /* Delay ACELP synthesis by DELAY_BWE_TOTAL_NS - DELAY_CLDFB_NS delay */
793 4040 : if ( output_frame >= L_FRAME16k )
794 : {
795 4040 : tmps = NS2SA( st->output_Fs, DELAY_BWE_TOTAL_NS - DELAY_CLDFB_NS );
796 4040 : delay_signal( synth_out, output_frame, st->prev_synth_buffer, tmps );
797 : }
798 :
799 4040 : if ( waveadj_rec )
800 : {
801 0 : tmps = 0;
802 0 : if ( output_frame >= L_FRAME16k )
803 : {
804 0 : tmps = NS2SA( st->output_Fs, DELAY_BWE_TOTAL_NS );
805 : }
806 0 : waveform_adj2( st->hPlcInfo, st->hTonalMDCTConc->secondLastPcmOut, synth_out + tmps, tmps, st->hPlcInfo->nbLostCmpt + 1, st->bfi );
807 : }
808 :
809 : /* HP filter */
810 4040 : hp20( synth_out, output_frame, mem_hp20_out, st->output_Fs );
811 :
812 : /* save synthesis for core switching */
813 4040 : 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 ) );
814 :
815 : /* TCX-LTP Postfilter: used in AMR-WB IO to update memories and to avoid discontinuities when the past frame was TCX */
816 4040 : tcx_ltp_post( st, hTcxLtpDec, ACELP_CORE, output_frame, 0, synth_out, NULL );
817 :
818 : /* final output of synthesis signal */
819 4040 : mvr2r( synth_out, output, output_frame );
820 :
821 : #ifdef DEBUG_MODE_INFO
822 : dbgwrite( &st->clas_dec, sizeof( int16_t ), 1, output_frame, "res/clas.dec" );
823 : dbgwrite( &st->codec_mode, sizeof( int16_t ), 1, output_frame, "res/codec.dec" );
824 : dbgwrite( &st->core, sizeof( int16_t ), 1, output_frame, "res/core.dec" );
825 : dbgwrite( &st->extl, sizeof( int16_t ), 1, output_frame, "res/extl.dec" );
826 : dbgwrite( &st->bwidth, sizeof( int16_t ), 1, output_frame, "res/bwidth.dec" );
827 : dbgwrite( &st->cng_type, sizeof( int16_t ), 1, output_frame, "res/cng_type.dec" );
828 : tmp = st->core_brate / 1000.0f;
829 : dbgwrite( &tmp, sizeof( float ), 1, output_frame, "res/core_brate.dec" );
830 : dbgwrite( &tmp, sizeof( float ), 1, output_frame, "res/total_brate.dec" );
831 : dbgwrite( &st->coder_type, sizeof( int16_t ), 1, output_frame, "res/coder_type.dec" );
832 : dbgwrite( &st->L_frame, sizeof( int16_t ), 1, output_frame, "res/L_frame.dec" );
833 : dbgwrite( &st->bfi, sizeof( int16_t ), 1, output_frame, "res/bfi" );
834 : #endif
835 :
836 4040 : return error;
837 : }
838 :
839 :
840 : /*------------------------------------------------------------------*
841 : * amr_wb_dec_init()
842 : *
843 : * AMR-WB decoder initialization
844 : *------------------------------------------------------------------*/
845 :
846 495 : void amr_wb_dec_init(
847 : AMRWB_IO_DEC_HANDLE hAmrwb_IO /* i/o: AMR-WB IO data handle */
848 : )
849 : {
850 : /* gain quantization memory (used in AMR-WB IO mode) */
851 495 : set_f( hAmrwb_IO->past_qua_en, -14.0f, GAIN_PRED_ORDER );
852 :
853 : /* Improvement of unvoiced and audio signals in AMR-WB IO mode */
854 495 : hAmrwb_IO->UV_cnt = 30;
855 495 : hAmrwb_IO->LT_UV_cnt = 60.0f;
856 495 : set_f( hAmrwb_IO->lt_diff_etot, 0, MAX_LT );
857 495 : hAmrwb_IO->Last_ener = 0.0f;
858 :
859 495 : set_f( hAmrwb_IO->old_Aq, 0, NB_SUBFR * ( M + 1 ) );
860 495 : hAmrwb_IO->old_Aq[0] = 1.0f;
861 495 : hAmrwb_IO->old_Aq[M + 1] = 1.0f;
862 495 : hAmrwb_IO->old_Aq[2 * ( M + 1 )] = 1.0f;
863 495 : hAmrwb_IO->old_Aq[3 * ( M + 1 )] = 1.0f;
864 :
865 495 : hAmrwb_IO->lt_voice_fac = 0.0f;
866 :
867 495 : return;
868 : }
|