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 "prot.h"
45 : #include "rom_com.h"
46 : #include "wmc_auto.h"
47 :
48 : /*---------------------------------------------------------------------*
49 : * Local function prototypes
50 : *---------------------------------------------------------------------*/
51 :
52 : static void shb_CNG_decod( Decoder_State *st, const float *synth, float *shb_synth, const int16_t sid_bw );
53 :
54 :
55 : /*---------------------------------------------------------------------*
56 : * CNG_dec()
57 : *
58 : * Decoding of CNG parameters
59 : *---------------------------------------------------------------------*/
60 :
61 7170 : void CNG_dec(
62 : Decoder_State *st, /* i/o: State structure */
63 : const int16_t last_element_mode, /* i : last element mode */
64 : float Aq[], /* o : LP coefficients */
65 : float *lsp_new, /* i/o: current frame LSPs */
66 : float *lsf_new, /* i/o: current frame LSFs */
67 : int16_t *allow_cn_step, /* o : allow CN step */
68 : int16_t *sid_bw, /* i : 0-NB/WB, 1-SWB SID */
69 : float *q_env )
70 : {
71 : int16_t i, enr_index, num_bits;
72 : TD_CNG_DEC_HANDLE hTdCngDec;
73 : float step;
74 : float enr, weights;
75 7170 : int16_t m = 0;
76 : int16_t ptr, j, k;
77 : int16_t m1;
78 : float tmp[HO_HIST_SIZE * M];
79 7170 : int16_t burst_ho_cnt = 0;
80 : int16_t ll, s_ptr;
81 : float lsf_tmp[M];
82 : float C[M];
83 : float max_val[2];
84 : int16_t max_idx[2];
85 : float ftmp;
86 : float lsp_tmp[M];
87 : float dev;
88 : float max_dev;
89 : float dist;
90 : int16_t env_idx[2];
91 : float enr1;
92 : float env[NUM_ENV_CNG];
93 : float tmp_env[HO_HIST_SIZE * NUM_ENV_CNG];
94 : int16_t LSF_Q_prediction; /* o : LSF prediction mode - just temporary variable in CNG */
95 : int16_t tmp1;
96 :
97 7170 : hTdCngDec = st->hTdCngDec;
98 :
99 : /*-----------------------------------------------------------------*
100 : * Decode CNG spectral envelope (only in SID frame)
101 : *-----------------------------------------------------------------*/
102 :
103 7170 : if ( st->core_brate == SID_1k75 || st->core_brate == SID_2k40 )
104 : {
105 : /* de-quantize the LSF vector */
106 1299 : if ( st->Opt_AMR_WB )
107 : {
108 0 : isf_dec_amr_wb( st, Aq, lsf_new, lsp_new );
109 : }
110 : else
111 : {
112 1299 : lsf_dec( st, 0, Aq, &LSF_Q_prediction, lsf_new, lsp_new, 0, 0, NULL );
113 : }
114 : }
115 : else
116 : {
117 : /* Use old LSP vector */
118 5871 : mvr2r( st->lsp_old, lsp_new, M );
119 5871 : mvr2r( st->lsf_old, lsf_new, M );
120 : }
121 :
122 : /* Initialize the CNG spectral envelope in case of the very first CNG frame */
123 7170 : if ( st->first_CNG == 0 )
124 : {
125 84 : mvr2r( st->lsp_old, st->lspCNG, M );
126 : }
127 :
128 : /*-----------------------------------------------------------------*
129 : * Decode residual signal energy
130 : *-----------------------------------------------------------------*/
131 :
132 7170 : *allow_cn_step = 0;
133 7170 : if ( st->core_brate == SID_1k75 || st->core_brate == SID_2k40 )
134 : {
135 1299 : if ( st->core_brate == SID_2k40 )
136 : {
137 1299 : step = STEP_SID;
138 : }
139 : else
140 : {
141 0 : step = STEP_AMR_WB_SID;
142 : }
143 :
144 : /* intialize the energy quantization parameters */
145 1299 : if ( !st->Opt_AMR_WB )
146 : {
147 1299 : num_bits = 7;
148 : }
149 : else
150 : {
151 0 : num_bits = 6;
152 : }
153 :
154 : /* decode the energy index */
155 1299 : enr_index = get_next_indice( st, num_bits );
156 :
157 1299 : if ( st->last_core_brate <= SID_2k40 || st->prev_bfi == 1 )
158 : {
159 711 : tmp1 = hTdCngDec->old_enr_index + 20;
160 : }
161 : else
162 : {
163 588 : tmp1 = hTdCngDec->old_enr_index + 40;
164 : }
165 1299 : if ( ( enr_index > tmp1 ) && ( hTdCngDec->old_enr_index >= 0 ) ) /* Likely bit error , and not startup */
166 : {
167 0 : enr_index = tmp1;
168 0 : enr_index = min( enr_index, 127 );
169 0 : if ( st->Opt_AMR_WB != 0 )
170 : {
171 0 : enr_index = min( enr_index, 63 );
172 : }
173 : }
174 :
175 1299 : if ( st->last_core_brate > SID_2k40 &&
176 603 : st->first_CNG != 0 &&
177 519 : hTdCngDec->old_enr_index >= 0 &&
178 519 : enr_index > hTdCngDec->old_enr_index + 1 )
179 : {
180 0 : *allow_cn_step = 1;
181 : }
182 1299 : hTdCngDec->old_enr_index = enr_index;
183 1299 : if ( enr_index == 0 )
184 : {
185 54 : enr_index = -5;
186 : }
187 1299 : hTdCngDec->Enew = enr_index / step - 2.0f;
188 :
189 : /* find the new energy value */
190 1299 : hTdCngDec->Enew = (float) ( pow( 2.0, hTdCngDec->Enew ) );
191 :
192 : /* decode SID type */
193 1299 : if ( st->core_brate == SID_2k40 )
194 : {
195 1299 : burst_ho_cnt = get_next_indice( st, 3 );
196 :
197 1299 : *sid_bw = get_next_indice( st, 1 );
198 :
199 1299 : if ( *sid_bw == 0 )
200 : {
201 0 : env_idx[0] = get_next_indice( st, 6 );
202 :
203 : /* get quantized res_env_details */
204 0 : for ( i = 0; i < NUM_ENV_CNG; i++ )
205 : {
206 0 : q_env[i] = CNG_details_codebook[env_idx[0]][i];
207 : }
208 : }
209 : }
210 :
211 : /* Reset CNG history if CNG frame length is changed */
212 1299 : if ( st->bwidth == WB && st->first_CNG && st->L_frame != st->last_CNG_L_frame )
213 : {
214 3 : hTdCngDec->ho_hist_size = 0;
215 : }
216 : }
217 :
218 : /*---------------------------------------------------------------------*
219 : * CNG spectral envelope update
220 : * Find A(z) coefficients
221 : *---------------------------------------------------------------------*/
222 :
223 7170 : if ( st->last_core_brate == FRAME_NO_DATA || st->last_core_brate == SID_1k75 || st->last_core_brate == SID_2k40 )
224 : {
225 : /* Reset hangover counter if not first SID period */
226 6567 : if ( st->core_brate > FRAME_NO_DATA )
227 : {
228 696 : hTdCngDec->num_ho = 0;
229 : }
230 :
231 : /* Update LSPs if last SID energy not outlier or insufficient number of hangover frames */
232 6567 : if ( hTdCngDec->num_ho < 3 || hTdCngDec->Enew < 1.5f * st->lp_ener )
233 : {
234 110058 : for ( i = 0; i < M; i++ )
235 : {
236 : /* AR low-pass filter */
237 103584 : st->lspCNG[i] = CNG_ISF_FACT * st->lspCNG[i] + ( 1 - CNG_ISF_FACT ) * lsp_new[i];
238 : }
239 : }
240 : }
241 : else
242 : {
243 : /* Update CNG_mode, if allowed */
244 603 : if ( ( st->Opt_AMR_WB || st->bwidth == WB ) && ( !st->first_CNG || hTdCngDec->act_cnt2 >= MIN_ACT_CNG_UPD ) )
245 : {
246 42 : if ( st->last_active_brate > ACELP_16k40 )
247 : {
248 15 : st->CNG_mode = -1;
249 : }
250 : else
251 : {
252 27 : st->CNG_mode = get_cng_mode( st->last_active_brate );
253 : }
254 : }
255 :
256 : /* If first sid after active burst update LSF history from circ buffer */
257 603 : burst_ho_cnt = min( burst_ho_cnt, hTdCngDec->ho_circ_size );
258 603 : hTdCngDec->act_cnt = 0;
259 603 : s_ptr = hTdCngDec->ho_circ_ptr - burst_ho_cnt + 1;
260 603 : if ( s_ptr < 0 )
261 : {
262 117 : s_ptr += hTdCngDec->ho_circ_size;
263 : }
264 :
265 1470 : for ( ll = burst_ho_cnt; ll > 0; ll-- )
266 : {
267 867 : if ( ++( hTdCngDec->ho_hist_ptr ) == HO_HIST_SIZE )
268 : {
269 69 : hTdCngDec->ho_hist_ptr = 0;
270 : }
271 : /* Conversion between 12.8k and 16k LSPs */
272 867 : if ( ( st->L_frame == L_FRAME16k && hTdCngDec->ho_16k_lsp[s_ptr] == 0 ) || ( st->L_frame == L_FRAME && hTdCngDec->ho_16k_lsp[s_ptr] == 1 ) )
273 : {
274 183 : lsp_convert_poly( &hTdCngDec->ho_lsp_circ[s_ptr * M], st->L_frame, 0 );
275 : }
276 :
277 : /* update circular buffers */
278 867 : mvr2r( &( hTdCngDec->ho_lsp_circ[s_ptr * M] ), &( hTdCngDec->ho_lsp_hist[hTdCngDec->ho_hist_ptr * M] ), M );
279 867 : mvr2r( &( hTdCngDec->ho_ener_circ[s_ptr] ), &( hTdCngDec->ho_ener_hist[hTdCngDec->ho_hist_ptr] ), 1 );
280 867 : hTdCngDec->ho_sid_bw = ( hTdCngDec->ho_sid_bw & 0x3fffffffL ) << 1;
281 867 : mvr2r( &( hTdCngDec->ho_env_circ[s_ptr * NUM_ENV_CNG] ), &( hTdCngDec->ho_env_hist[hTdCngDec->ho_hist_ptr * NUM_ENV_CNG] ), NUM_ENV_CNG );
282 :
283 867 : hTdCngDec->ho_hist_size++;
284 867 : if ( hTdCngDec->ho_hist_size > HO_HIST_SIZE )
285 : {
286 96 : hTdCngDec->ho_hist_size = HO_HIST_SIZE;
287 : }
288 :
289 867 : s_ptr++;
290 :
291 867 : if ( s_ptr == hTdCngDec->ho_circ_size )
292 : {
293 123 : s_ptr = 0;
294 : }
295 : }
296 :
297 603 : if ( hTdCngDec->ho_hist_size > 0 )
298 : {
299 426 : *allow_cn_step |= ( st->first_CNG || st->element_mode == EVS_MONO ) && ( hTdCngDec->ho_ener_hist[hTdCngDec->ho_hist_ptr] > 4.0f * st->lp_ener );
300 : }
301 :
302 603 : if ( last_element_mode == IVAS_CPE_TD )
303 : {
304 45 : *allow_cn_step = 1;
305 : }
306 :
307 603 : if ( !*allow_cn_step && hTdCngDec->ho_hist_size > 0 )
308 : {
309 381 : ptr = hTdCngDec->ho_hist_ptr;
310 381 : mvr2r( &( hTdCngDec->ho_lsp_hist[ptr * M] ), tmp, M );
311 381 : m1 = 0;
312 381 : if ( ( hTdCngDec->ho_sid_bw & 0x1L ) == 0 )
313 : {
314 252 : mvr2r( &hTdCngDec->ho_env_hist[ptr * NUM_ENV_CNG], tmp_env, NUM_ENV_CNG );
315 252 : m1 = 1;
316 : }
317 381 : enr = W_DTX_HO[0] * hTdCngDec->ho_ener_hist[ptr];
318 381 : weights = W_DTX_HO[0];
319 381 : m = 1;
320 1635 : for ( k = 1; k < hTdCngDec->ho_hist_size; k++ )
321 : {
322 1254 : ptr--;
323 1254 : if ( ptr < 0 )
324 : {
325 60 : ptr = HO_HIST_SIZE - 1;
326 : }
327 :
328 1254 : if ( hTdCngDec->ho_ener_hist[ptr] < hTdCngDec->ho_ener_hist[hTdCngDec->ho_hist_ptr] * BUF_H_NRG &&
329 561 : hTdCngDec->ho_ener_hist[ptr] > hTdCngDec->ho_ener_hist[hTdCngDec->ho_hist_ptr] * BUF_L_NRG )
330 : {
331 486 : enr += W_DTX_HO[k] * hTdCngDec->ho_ener_hist[ptr];
332 486 : weights += W_DTX_HO[k];
333 486 : mvr2r( &hTdCngDec->ho_lsp_hist[ptr * M], &tmp[m * M], M );
334 486 : if ( ( hTdCngDec->ho_sid_bw & ( 0x1L << k ) ) == 0 )
335 : {
336 321 : mvr2r( &hTdCngDec->ho_env_hist[ptr * NUM_ENV_CNG], &tmp_env[m1 * NUM_ENV_CNG], NUM_ENV_CNG );
337 321 : m1++;
338 : }
339 486 : m++;
340 : }
341 : }
342 :
343 381 : enr /= weights;
344 381 : st->lp_ener = enr;
345 :
346 381 : set_f( max_val, 0.0f, 2 );
347 381 : set_s( max_idx, 0, 2 );
348 :
349 1248 : for ( i = 0; i < m; i++ )
350 : {
351 867 : if ( st->L_frame == L_FRAME )
352 : {
353 558 : lsp2lsf( &tmp[i * M], lsf_tmp, M, INT_FS_12k8 );
354 558 : ftmp = 6400.0f / ( M + 1 );
355 558 : C[i] = ( 6400.0f - lsf_tmp[M - 1] - ftmp ) * ( 6400.0f - lsf_tmp[M - 1] - ftmp );
356 : }
357 : else
358 : {
359 309 : lsp2lsf( &tmp[i * M], lsf_tmp, M, INT_FS_16k );
360 309 : ftmp = 8000.0f / ( M + 1 );
361 309 : C[i] = ( 8000.0f - lsf_tmp[M - 1] - ftmp ) * ( 8000.0f - lsf_tmp[M - 1] - ftmp );
362 : }
363 :
364 867 : C[i] += ( lsf_tmp[0] - ftmp ) * ( lsf_tmp[0] - ftmp );
365 :
366 13872 : for ( j = 0; j < M - 1; j++ )
367 : {
368 13005 : C[i] += ( lsf_tmp[j + 1] - lsf_tmp[j] - ftmp ) * ( lsf_tmp[j + 1] - lsf_tmp[j] - ftmp );
369 : }
370 :
371 867 : C[i] *= 0.0588235f; /* 1/M+1 */
372 :
373 867 : if ( C[i] > max_val[0] )
374 : {
375 636 : max_val[1] = max_val[0];
376 636 : max_idx[1] = max_idx[0];
377 636 : max_val[0] = C[i];
378 636 : max_idx[0] = i;
379 : }
380 231 : else if ( C[i] > max_val[1] )
381 : {
382 144 : max_val[1] = C[i];
383 144 : max_idx[1] = i;
384 : }
385 : }
386 :
387 381 : if ( m == 1 )
388 : {
389 132 : mvr2r( tmp, lsp_tmp, M );
390 : }
391 249 : else if ( m < 4 )
392 : {
393 3468 : for ( i = 0; i < M; i++ )
394 : {
395 3264 : lsp_tmp[i] = 0.0f;
396 11520 : for ( j = 0; j < m; j++ )
397 : {
398 8256 : lsp_tmp[i] += tmp[j * M + i];
399 : }
400 :
401 3264 : lsp_tmp[i] -= tmp[max_idx[0] * M + i];
402 3264 : lsp_tmp[i] /= (float) ( m - 1 );
403 : }
404 : }
405 : else
406 : {
407 765 : for ( i = 0; i < M; i++ )
408 : {
409 720 : lsp_tmp[i] = 0.0f;
410 4224 : for ( j = 0; j < m; j++ )
411 : {
412 3504 : lsp_tmp[i] += tmp[j * M + i];
413 : }
414 :
415 720 : lsp_tmp[i] -= ( tmp[max_idx[0] * M + i] + tmp[max_idx[1] * M + i] );
416 720 : lsp_tmp[i] /= (float) ( m - 2 );
417 : }
418 : }
419 :
420 381 : dist = 0.0f;
421 381 : max_dev = 0.0f;
422 6477 : for ( i = 0; i < M; i++ )
423 : {
424 6096 : dev = (float) fabs( lsp_tmp[i] - lsp_new[i] );
425 6096 : dist += dev;
426 6096 : if ( dev > max_dev )
427 : {
428 1917 : max_dev = dev;
429 : }
430 : }
431 :
432 381 : if ( dist > 0.4f || max_dev > 0.1f )
433 : {
434 204 : for ( i = 0; i < M; i++ )
435 : {
436 192 : st->lspCNG[i] = lsp_tmp[i];
437 : }
438 : }
439 : else
440 : {
441 6273 : for ( i = 0; i < M; i++ )
442 : {
443 : /* AR low-pass filter */
444 5904 : st->lspCNG[i] = 0.8f * lsp_tmp[i] + ( 1 - 0.8f ) * lsp_new[i];
445 : }
446 : }
447 381 : if ( m1 > 0 )
448 : {
449 6174 : for ( i = 0; i < NUM_ENV_CNG; i++ )
450 : {
451 5880 : env[i] = 0;
452 17340 : for ( j = 0; j < m1; j++ )
453 : {
454 11460 : env[i] += tmp_env[j * NUM_ENV_CNG + i];
455 : }
456 :
457 5880 : env[i] /= (float) m1;
458 5880 : env[i] = env[i] - 2 * st->lp_ener;
459 : }
460 294 : mvr2r( env, hTdCngDec->lp_env, NUM_ENV_CNG );
461 : }
462 : }
463 : else
464 : {
465 222 : mvr2r( lsp_new, st->lspCNG, M ); /* use newly analyzed parameters */
466 : }
467 : }
468 :
469 7170 : if ( st->core_brate == SID_1k75 || st->core_brate == SID_2k40 )
470 : {
471 : /* Update hangover memory during CNG */
472 1299 : if ( !*allow_cn_step && ( hTdCngDec->Enew < 1.5f * st->lp_ener ) )
473 : {
474 : /* update the pointer to circular buffer of old LSP vectors */
475 861 : if ( ++( hTdCngDec->ho_hist_ptr ) == HO_HIST_SIZE )
476 : {
477 108 : hTdCngDec->ho_hist_ptr = 0;
478 : }
479 :
480 : /* update the circular buffer of old LSP vectors with the new LSP vector */
481 861 : mvr2r( lsp_new, &( hTdCngDec->ho_lsp_hist[( hTdCngDec->ho_hist_ptr ) * M] ), M );
482 :
483 : /* update the hangover energy buffer */
484 861 : hTdCngDec->ho_ener_hist[hTdCngDec->ho_hist_ptr] = hTdCngDec->Enew;
485 :
486 861 : if ( st->core_brate == SID_2k40 && *sid_bw == 0 )
487 : {
488 0 : enr1 = (float) log10( hTdCngDec->Enew * st->L_frame + 0.1f ) / (float) log10( 2.0f );
489 0 : for ( i = 0; i < NUM_ENV_CNG; i++ )
490 : {
491 : /* get quantized envelope */
492 0 : env[i] = (float) ( pow( 2.0f, ( enr1 - q_env[i] ) ) + 2 * hTdCngDec->Enew );
493 : }
494 0 : hTdCngDec->ho_sid_bw = ( hTdCngDec->ho_sid_bw & 0x3fffffffL ) << 1;
495 0 : mvr2r( env, &( hTdCngDec->ho_env_hist[( hTdCngDec->ho_hist_ptr ) * NUM_ENV_CNG] ), NUM_ENV_CNG );
496 : }
497 861 : else if ( *sid_bw != 0 )
498 : {
499 861 : hTdCngDec->ho_sid_bw = ( hTdCngDec->ho_sid_bw & 0x3fffffffL ) << 1;
500 861 : hTdCngDec->ho_sid_bw |= 0x1L;
501 : }
502 861 : if ( ++( hTdCngDec->ho_hist_size ) > HO_HIST_SIZE )
503 : {
504 513 : hTdCngDec->ho_hist_size = HO_HIST_SIZE;
505 : }
506 : }
507 :
508 : /* Update the frame length memory */
509 1299 : st->last_CNG_L_frame = st->L_frame;
510 :
511 1299 : if ( st->core_brate != SID_1k75 )
512 : {
513 1299 : hTdCngDec->num_ho = m;
514 : }
515 : }
516 :
517 7170 : if ( st->Opt_AMR_WB )
518 : {
519 0 : isp2a( st->lspCNG, Aq, M );
520 : }
521 : else
522 : {
523 7170 : lsp2a_stab( st->lspCNG, Aq, M );
524 : }
525 :
526 33297 : for ( i = 1; i < st->L_frame / L_SUBFR; i++ )
527 : {
528 26127 : mvr2r( Aq, &Aq[i * ( M + 1 )], M + 1 );
529 : }
530 :
531 7170 : return;
532 : }
533 :
534 :
535 : /*---------------------------------------------------------------------*
536 : * swb_CNG_dec()
537 : *
538 : * Comfort noise generation for SHB signal
539 : *---------------------------------------------------------------------*/
540 :
541 688317 : void swb_CNG_dec(
542 : Decoder_State *st, /* i/o: State structure */
543 : const float *synth, /* i : ACELP core synthesis at 32kHz */
544 : float *shb_synth, /* o : high-band CNG synthesis */
545 : const int16_t sid_bw /* i : 0-NB/WB, 1-SWB SID */
546 : )
547 : {
548 688317 : if ( st->core_brate == FRAME_NO_DATA || st->core_brate == SID_2k40 )
549 : {
550 : /* SHB SID decoding and CNG */
551 38166 : if ( st->cng_type == LP_CNG && st->extl == SWB_CNG )
552 : {
553 5391 : shb_CNG_decod( st, synth, shb_synth, sid_bw );
554 : }
555 38166 : st->last_vad = 0;
556 38166 : st->hTdCngDec->burst_cnt = 0;
557 : }
558 : else
559 : {
560 650151 : st->last_vad = 1;
561 650151 : st->hTdCngDec->burst_cnt++;
562 650151 : if ( st->hTdCngDec->burst_cnt > 10 )
563 : {
564 56193 : st->hTdCngDec->burst_cnt = 0;
565 : }
566 : }
567 :
568 688317 : return;
569 : }
570 :
571 : /*---------------------------------------------------------------------*
572 : * shb_CNG_decod()
573 : *
574 : * Main routine of SHB SID decoding and CNG
575 : *---------------------------------------------------------------------*/
576 :
577 5391 : static void shb_CNG_decod(
578 : Decoder_State *st, /* i/o: State structure */
579 : const float *synth, /* i : ACELP core synthesis at 32kHz */
580 : float *shb_synth, /* o : high-band CNG synthesis */
581 : const int16_t sid_bw /* i : 0-NB/WB, 1-SWB SID */
582 : )
583 : {
584 : int16_t i;
585 : int16_t idx_ener;
586 : TD_CNG_DEC_HANDLE hTdCngDec;
587 : float shb_lpcCNG[LPC_SHB_ORDER + 1];
588 : float shb_lspCNG[LPC_SHB_ORDER];
589 : float excTmp[L_FRAME16k];
590 : float excSHB[L_FRAME16k];
591 : float ener_excSHB;
592 : float wb_ener;
593 : float gain;
594 : float shb_syn16k[L_FRAME16k];
595 : float ftmp;
596 : float step;
597 : float interp;
598 : float ener;
599 5391 : int16_t allow_cn_step = 0;
600 :
601 5391 : hTdCngDec = st->hTdCngDec;
602 :
603 5391 : if ( !st->bfi )
604 : {
605 5391 : if ( st->core_brate == SID_2k40 && sid_bw == 1 )
606 : {
607 1005 : idx_ener = get_next_indice( st, 4 );
608 :
609 1005 : if ( idx_ener == 0 )
610 : {
611 15 : idx_ener = -15;
612 : }
613 :
614 : /* de-quantization of SHB CNG parameters */
615 1005 : if ( st->element_mode == EVS_MONO )
616 : {
617 0 : hTdCngDec->last_shb_cng_ener = ( (float) idx_ener / 0.9f - 6.0f ) * (float) log10( 2.0f ) * 10.0f;
618 : }
619 : else
620 : {
621 1005 : hTdCngDec->last_shb_cng_ener = ( (float) idx_ener / 0.7f - 6.0f ) * (float) log10( 2.0f ) * 10.0f;
622 : }
623 : }
624 : }
625 :
626 : /* SHB spectrum estimation */
627 5391 : interp = ( (float) hTdCngDec->shb_dtx_count ) / 32;
628 5391 : interp = min( interp, 1.0f );
629 59301 : for ( i = 0; i < LPC_SHB_ORDER; i++ )
630 : {
631 53910 : shb_lspCNG[i] = interp * hTdCngDec->lsp_shb_prev[i];
632 53910 : shb_lspCNG[i] += ( 1 - interp ) * hTdCngDec->lsp_shb_prev_prev[i];
633 : }
634 :
635 5391 : if ( hTdCngDec->shb_dtx_count < 1000 )
636 : {
637 5391 : hTdCngDec->shb_dtx_count++;
638 : }
639 :
640 5391 : lsp2a( shb_lpcCNG, shb_lspCNG, LPC_SHB_ORDER );
641 5391 : shb_lpcCNG[0] = 1.0f;
642 :
643 5391 : mvr2r( shb_lpcCNG, st->hTdCngDec->shb_lpcCNG, LPC_SHB_ORDER + 1 );
644 :
645 : /* SHB energy estimation */
646 5391 : wb_ener = 0.001f;
647 :
648 5391 : if ( st->element_mode != IVAS_CPE_DFT )
649 : {
650 0 : for ( i = 0; i < L_FRAME32k; i++ )
651 : {
652 0 : wb_ener += synth[i] * synth[i];
653 : }
654 : }
655 :
656 5391 : wb_ener /= L_FRAME32k;
657 5391 : wb_ener = 10 * (float) log10( wb_ener );
658 :
659 5391 : if ( st->first_CNG == 0 )
660 : {
661 57 : hTdCngDec->wb_cng_ener = wb_ener;
662 : }
663 5391 : if ( fabs( wb_ener - hTdCngDec->wb_cng_ener ) > 12.0f )
664 : {
665 0 : allow_cn_step = 1;
666 : }
667 :
668 5391 : if ( allow_cn_step == 1 )
669 : {
670 0 : hTdCngDec->wb_cng_ener = wb_ener;
671 : }
672 : else
673 : {
674 5391 : ftmp = wb_ener - hTdCngDec->wb_cng_ener;
675 5391 : hTdCngDec->wb_cng_ener += 0.9f * ftmp;
676 : }
677 5391 : if ( st->core_brate == SID_2k40 && sid_bw == 1 && !st->bfi )
678 : {
679 1005 : hTdCngDec->last_wb_cng_ener = hTdCngDec->wb_cng_ener;
680 :
681 1005 : if ( st->first_CNG == 0 )
682 : {
683 57 : hTdCngDec->shb_cng_ener = hTdCngDec->last_shb_cng_ener;
684 : }
685 : }
686 :
687 5391 : gain = hTdCngDec->wb_cng_ener - hTdCngDec->last_wb_cng_ener;
688 5391 : if ( gain > 15 )
689 : {
690 0 : gain = 15;
691 : }
692 5391 : step = gain + hTdCngDec->last_shb_cng_ener - hTdCngDec->shb_cng_ener;
693 5391 : if ( allow_cn_step == 1 || st->last_core_brate > SID_2k40 )
694 : {
695 480 : hTdCngDec->shb_cng_ener += step;
696 : }
697 : else
698 : {
699 4911 : hTdCngDec->shb_cng_ener += 0.25f * step;
700 : }
701 : /* generate white noise excitation */
702 1730511 : for ( i = 0; i < L_FRAME16k; i++ )
703 : {
704 1725120 : excTmp[i] = (float) own_random( &hTdCngDec->swb_cng_seed );
705 : }
706 :
707 : /* synthesis filtering */
708 5391 : syn_filt( shb_lpcCNG, LPC_SHB_ORDER, excTmp, excSHB, L_FRAME16k, st->hBWE_TD->state_lpc_syn, 1 );
709 :
710 : /* synthesis signal gain shaping */
711 5391 : ener_excSHB = 0.001f;
712 1730511 : for ( i = 0; i < L_FRAME16k; i++ )
713 : {
714 1725120 : ener_excSHB += excSHB[i] * excSHB[i];
715 : }
716 :
717 5391 : if ( st->last_vad == 1 )
718 : {
719 480 : if ( hTdCngDec->burst_cnt > 3 && st->last_core != HQ_CORE )
720 : {
721 117 : hTdCngDec->trans_cnt = 5;
722 : }
723 : else
724 : {
725 363 : hTdCngDec->trans_cnt = 0;
726 : }
727 : }
728 :
729 5391 : if ( hTdCngDec->trans_cnt > 0 )
730 : {
731 228 : i = (int16_t) ( (float) hTdCngDec->trans_cnt / 15.0f * 255 );
732 228 : ener = hTdCngDec->shb_cng_ener + sin_table256[i] * ( hTdCngDec->last_shb_ener - hTdCngDec->shb_cng_ener );
733 228 : hTdCngDec->trans_cnt--;
734 : }
735 : else
736 : {
737 5163 : ener = hTdCngDec->shb_cng_ener;
738 : }
739 :
740 :
741 5391 : gain = (float) sqrt( pow( 10, 0.1f * ener ) * L_FRAME16k / ener_excSHB );
742 :
743 5391 : st->hTdCngDec->shb_cng_gain = ener;
744 : #ifdef DEBUGGING
745 : /* note: state shb_cng_gain is actually an energy value in dB */
746 : #endif
747 :
748 1730511 : for ( i = 0; i < L_FRAME16k; i++ )
749 : {
750 1725120 : shb_syn16k[i] = gain * excSHB[i];
751 : }
752 :
753 : /* generate 32kHz SHB signal (12.8 - 14.4kHz) from 12.8kHz signal */
754 5391 : GenSHBSynth( shb_syn16k, shb_synth, st->hBWE_TD->genSHBsynth_Hilbert_Mem, st->hBWE_TD->genSHBsynth_state_lsyn_filt_shb_local, st->L_frame, &( st->hBWE_TD->syn_dm_phase ) );
755 :
756 5391 : if ( st->output_Fs == 48000 )
757 : {
758 2922 : interpolate_3_over_2_allpass( shb_synth, L_FRAME32k, shb_synth, hTdCngDec->interpol_3_2_cng_dec );
759 : }
760 :
761 5391 : ResetSHBbuffer_Dec( st->hBWE_TD, st->extl );
762 :
763 5391 : return;
764 : }
765 :
766 :
767 : /*-------------------------------------------------------------------*
768 : * td_cng_dec_init()
769 : *
770 : *
771 : *-------------------------------------------------------------------*/
772 :
773 4500 : void td_cng_dec_init(
774 : DEC_CORE_HANDLE st /* i/o: decoder state structure */
775 : )
776 : {
777 : int16_t i;
778 : TD_CNG_DEC_HANDLE hTdCngDec;
779 :
780 4500 : hTdCngDec = st->hTdCngDec;
781 :
782 4500 : hTdCngDec->cng_seed = RANDOM_INITSEED;
783 4500 : hTdCngDec->cng_ener_seed = RANDOM_INITSEED;
784 4500 : hTdCngDec->cng_ener_seed1 = RANDOM_INITSEED;
785 4500 : hTdCngDec->old_enr_index = -1;
786 4500 : hTdCngDec->Enew = 0.0f;
787 4500 : mvr2r( st->lsp_old, st->lspCNG, M );
788 4500 : hTdCngDec->last_allow_cn_step = 0;
789 4500 : hTdCngDec->shb_cng_ener = -6.02f;
790 4500 : if ( st->element_mode != EVS_MONO )
791 : {
792 4491 : set_f( hTdCngDec->shb_lpcCNG, 0.0f, LPC_SHB_ORDER + 1 );
793 4491 : hTdCngDec->shb_lpcCNG[0] = 1.0f;
794 4491 : hTdCngDec->shb_cng_gain = -82.0; /* a dB value approximately corresponding to shb index 0(used as index -15) */
795 : }
796 :
797 4500 : hTdCngDec->wb_cng_ener = -6.02f;
798 4500 : hTdCngDec->last_wb_cng_ener = -6.02f;
799 4500 : hTdCngDec->last_shb_cng_ener = -6.02f;
800 4500 : hTdCngDec->swb_cng_seed = RANDOM_INITSEED;
801 4500 : hTdCngDec->ho_hist_ptr = -1;
802 4500 : hTdCngDec->ho_sid_bw = 0;
803 4500 : set_f( hTdCngDec->ho_lsp_hist, 0, HO_HIST_SIZE * M );
804 4500 : set_f( hTdCngDec->ho_ener_hist, 0, HO_HIST_SIZE );
805 4500 : set_f( hTdCngDec->ho_env_hist, 0, HO_HIST_SIZE * NUM_ENV_CNG );
806 4500 : hTdCngDec->ho_hist_size = 0;
807 4500 : hTdCngDec->act_cnt = 0;
808 4500 : hTdCngDec->ho_circ_ptr = -1;
809 4500 : set_f( hTdCngDec->ho_lsp_circ, 0, HO_HIST_SIZE * M );
810 4500 : set_f( hTdCngDec->ho_ener_circ, 0, HO_HIST_SIZE );
811 4500 : set_f( hTdCngDec->ho_env_circ, 0, HO_HIST_SIZE * NUM_ENV_CNG );
812 4500 : hTdCngDec->ho_circ_size = 0;
813 :
814 4500 : set_s( hTdCngDec->ho_16k_lsp, 0, HO_HIST_SIZE );
815 4500 : st->CNG_mode = -1;
816 4500 : hTdCngDec->act_cnt2 = 0;
817 4500 : hTdCngDec->num_ho = 0;
818 4500 : set_f( hTdCngDec->lp_env, 0.0f, NUM_ENV_CNG );
819 4500 : set_f( hTdCngDec->exc_mem, 0.0f, 24 );
820 4500 : set_f( hTdCngDec->exc_mem1, 0.0f, 30 );
821 4500 : set_f( hTdCngDec->old_env, 0.0f, NUM_ENV_CNG );
822 :
823 49500 : for ( i = 0; i < LPC_SHB_ORDER; i++ )
824 : {
825 45000 : if ( st->element_mode != EVS_MONO )
826 : {
827 44910 : hTdCngDec->lsp_shb_prev[i] = 0.5f * ( (float) ( i + 1 ) ) / ( (float) ( LPC_SHB_ORDER + 1 ) );
828 : }
829 : else
830 : {
831 90 : hTdCngDec->lsp_shb_prev[i] = 0.5f * ( (float) i ) / ( (float) LPC_SHB_ORDER );
832 : }
833 45000 : hTdCngDec->lsp_shb_prev_prev[i] = hTdCngDec->lsp_shb_prev[i];
834 : }
835 :
836 4500 : hTdCngDec->shb_dtx_count = 0;
837 4500 : hTdCngDec->trans_cnt = 0;
838 4500 : hTdCngDec->burst_cnt = 0;
839 :
840 4500 : hTdCngDec->last_shb_ener = 0.001f;
841 :
842 :
843 4500 : set_f( hTdCngDec->interpol_3_2_cng_dec, 0.0f, INTERP_3_2_MEM_LEN );
844 :
845 4500 : return;
846 : }
|