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 21709 : 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 21709 : int16_t m = 0;
76 : int16_t ptr, j, k;
77 : int16_t m1;
78 : float tmp[HO_HIST_SIZE * M];
79 21709 : 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 21709 : hTdCngDec = st->hTdCngDec;
98 :
99 : /*-----------------------------------------------------------------*
100 : * Decode CNG spectral envelope (only in SID frame)
101 : *-----------------------------------------------------------------*/
102 :
103 21709 : if ( st->core_brate == SID_1k75 || st->core_brate == SID_2k40 )
104 : {
105 : /* de-quantize the LSF vector */
106 3712 : if ( st->Opt_AMR_WB )
107 : {
108 104 : isf_dec_amr_wb( st, Aq, lsf_new, lsp_new );
109 : }
110 : else
111 : {
112 3608 : 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 17997 : mvr2r( st->lsp_old, lsp_new, M );
119 17997 : mvr2r( st->lsf_old, lsf_new, M );
120 : }
121 :
122 : /* Initialize the CNG spectral envelope in case of the very first CNG frame */
123 21709 : if ( st->first_CNG == 0 )
124 : {
125 122 : mvr2r( st->lsp_old, st->lspCNG, M );
126 : }
127 :
128 : /*-----------------------------------------------------------------*
129 : * Decode residual signal energy
130 : *-----------------------------------------------------------------*/
131 :
132 21709 : *allow_cn_step = 0;
133 21709 : if ( st->core_brate == SID_1k75 || st->core_brate == SID_2k40 )
134 : {
135 3712 : if ( st->core_brate == SID_2k40 )
136 : {
137 3608 : step = STEP_SID;
138 : }
139 : else
140 : {
141 104 : step = STEP_AMR_WB_SID;
142 : }
143 :
144 : /* intialize the energy quantization parameters */
145 3712 : if ( !st->Opt_AMR_WB )
146 : {
147 3608 : num_bits = 7;
148 : }
149 : else
150 : {
151 104 : num_bits = 6;
152 : }
153 :
154 : /* decode the energy index */
155 3712 : enr_index = get_next_indice( st, num_bits );
156 :
157 3712 : if ( st->last_core_brate <= SID_2k40 || st->prev_bfi == 1 )
158 : {
159 1953 : tmp1 = hTdCngDec->old_enr_index + 20;
160 : }
161 : else
162 : {
163 1759 : tmp1 = hTdCngDec->old_enr_index + 40;
164 : }
165 3712 : if ( ( enr_index > tmp1 ) && ( hTdCngDec->old_enr_index >= 0 ) ) /* Likely bit error , and not startup */
166 : {
167 81 : enr_index = tmp1;
168 81 : enr_index = min( enr_index, 127 );
169 81 : if ( st->Opt_AMR_WB != 0 )
170 : {
171 0 : enr_index = min( enr_index, 63 );
172 : }
173 : }
174 :
175 3712 : if ( st->last_core_brate > SID_2k40 &&
176 1808 : st->first_CNG != 0 &&
177 1686 : hTdCngDec->old_enr_index >= 0 &&
178 1686 : enr_index > hTdCngDec->old_enr_index + 1 )
179 : {
180 171 : *allow_cn_step = 1;
181 : }
182 3712 : hTdCngDec->old_enr_index = enr_index;
183 3712 : if ( enr_index == 0 )
184 : {
185 1082 : enr_index = -5;
186 : }
187 3712 : hTdCngDec->Enew = enr_index / step - 2.0f;
188 :
189 : /* find the new energy value */
190 3712 : hTdCngDec->Enew = (float) ( pow( 2.0, hTdCngDec->Enew ) );
191 :
192 : /* decode SID type */
193 3712 : if ( st->core_brate == SID_2k40 )
194 : {
195 3608 : burst_ho_cnt = get_next_indice( st, 3 );
196 :
197 3608 : *sid_bw = get_next_indice( st, 1 );
198 :
199 3608 : if ( *sid_bw == 0 )
200 : {
201 341 : env_idx[0] = get_next_indice( st, 6 );
202 :
203 : /* get quantized res_env_details */
204 7161 : for ( i = 0; i < NUM_ENV_CNG; i++ )
205 : {
206 6820 : 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 3712 : if ( st->bwidth == WB && st->first_CNG && st->L_frame != st->last_CNG_L_frame )
213 : {
214 9 : hTdCngDec->ho_hist_size = 0;
215 : }
216 : }
217 :
218 : /*---------------------------------------------------------------------*
219 : * CNG spectral envelope update
220 : * Find A(z) coefficients
221 : *---------------------------------------------------------------------*/
222 :
223 21709 : 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 19901 : if ( st->core_brate > FRAME_NO_DATA )
227 : {
228 1904 : hTdCngDec->num_ho = 0;
229 : }
230 :
231 : /* Update LSPs if last SID energy not outlier or insufficient number of hangover frames */
232 19901 : if ( hTdCngDec->num_ho < 3 || hTdCngDec->Enew < 1.5f * st->lp_ener )
233 : {
234 323680 : for ( i = 0; i < M; i++ )
235 : {
236 : /* AR low-pass filter */
237 304640 : 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 1808 : if ( ( st->Opt_AMR_WB || st->bwidth == WB ) && ( !st->first_CNG || hTdCngDec->act_cnt2 >= MIN_ACT_CNG_UPD ) )
245 : {
246 287 : if ( st->last_active_brate > ACELP_16k40 )
247 : {
248 148 : st->CNG_mode = -1;
249 : }
250 : else
251 : {
252 139 : 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 1808 : burst_ho_cnt = min( burst_ho_cnt, hTdCngDec->ho_circ_size );
258 1808 : hTdCngDec->act_cnt = 0;
259 1808 : s_ptr = hTdCngDec->ho_circ_ptr - burst_ho_cnt + 1;
260 1808 : if ( s_ptr < 0 )
261 : {
262 579 : s_ptr += hTdCngDec->ho_circ_size;
263 : }
264 :
265 7269 : for ( ll = burst_ho_cnt; ll > 0; ll-- )
266 : {
267 5461 : if ( ++( hTdCngDec->ho_hist_ptr ) == HO_HIST_SIZE )
268 : {
269 594 : hTdCngDec->ho_hist_ptr = 0;
270 : }
271 : /* Conversion between 12.8k and 16k LSPs */
272 5461 : 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 645 : lsp_convert_poly( &hTdCngDec->ho_lsp_circ[s_ptr * M], st->L_frame, 0 );
275 : }
276 :
277 : /* update circular buffers */
278 5461 : mvr2r( &( hTdCngDec->ho_lsp_circ[s_ptr * M] ), &( hTdCngDec->ho_lsp_hist[hTdCngDec->ho_hist_ptr * M] ), M );
279 5461 : mvr2r( &( hTdCngDec->ho_ener_circ[s_ptr] ), &( hTdCngDec->ho_ener_hist[hTdCngDec->ho_hist_ptr] ), 1 );
280 5461 : hTdCngDec->ho_sid_bw = ( hTdCngDec->ho_sid_bw & 0x3fffffffL ) << 1;
281 5461 : 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 5461 : hTdCngDec->ho_hist_size++;
284 5461 : if ( hTdCngDec->ho_hist_size > HO_HIST_SIZE )
285 : {
286 789 : hTdCngDec->ho_hist_size = HO_HIST_SIZE;
287 : }
288 :
289 5461 : s_ptr++;
290 :
291 5461 : if ( s_ptr == hTdCngDec->ho_circ_size )
292 : {
293 687 : s_ptr = 0;
294 : }
295 : }
296 :
297 1808 : if ( hTdCngDec->ho_hist_size > 0 )
298 : {
299 1480 : *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 1808 : if ( last_element_mode == IVAS_CPE_TD )
303 : {
304 6 : *allow_cn_step = 1;
305 : }
306 :
307 1808 : if ( !*allow_cn_step && hTdCngDec->ho_hist_size > 0 )
308 : {
309 1245 : ptr = hTdCngDec->ho_hist_ptr;
310 1245 : mvr2r( &( hTdCngDec->ho_lsp_hist[ptr * M] ), tmp, M );
311 1245 : m1 = 0;
312 1245 : if ( ( hTdCngDec->ho_sid_bw & 0x1L ) == 0 )
313 : {
314 993 : mvr2r( &hTdCngDec->ho_env_hist[ptr * NUM_ENV_CNG], tmp_env, NUM_ENV_CNG );
315 993 : m1 = 1;
316 : }
317 1245 : enr = W_DTX_HO[0] * hTdCngDec->ho_ener_hist[ptr];
318 1245 : weights = W_DTX_HO[0];
319 1245 : m = 1;
320 7352 : for ( k = 1; k < hTdCngDec->ho_hist_size; k++ )
321 : {
322 6107 : ptr--;
323 6107 : if ( ptr < 0 )
324 : {
325 690 : ptr = HO_HIST_SIZE - 1;
326 : }
327 :
328 6107 : if ( hTdCngDec->ho_ener_hist[ptr] < hTdCngDec->ho_ener_hist[hTdCngDec->ho_hist_ptr] * BUF_H_NRG &&
329 2832 : hTdCngDec->ho_ener_hist[ptr] > hTdCngDec->ho_ener_hist[hTdCngDec->ho_hist_ptr] * BUF_L_NRG )
330 : {
331 1656 : enr += W_DTX_HO[k] * hTdCngDec->ho_ener_hist[ptr];
332 1656 : weights += W_DTX_HO[k];
333 1656 : mvr2r( &hTdCngDec->ho_lsp_hist[ptr * M], &tmp[m * M], M );
334 1656 : if ( ( hTdCngDec->ho_sid_bw & ( 0x1L << k ) ) == 0 )
335 : {
336 1281 : mvr2r( &hTdCngDec->ho_env_hist[ptr * NUM_ENV_CNG], &tmp_env[m1 * NUM_ENV_CNG], NUM_ENV_CNG );
337 1281 : m1++;
338 : }
339 1656 : m++;
340 : }
341 : }
342 :
343 1245 : enr /= weights;
344 1245 : st->lp_ener = enr;
345 :
346 1245 : set_f( max_val, 0.0f, 2 );
347 1245 : set_s( max_idx, 0, 2 );
348 :
349 4146 : for ( i = 0; i < m; i++ )
350 : {
351 2901 : if ( st->L_frame == L_FRAME )
352 : {
353 1101 : lsp2lsf( &tmp[i * M], lsf_tmp, M, INT_FS_12k8 );
354 1101 : ftmp = 6400.0f / ( M + 1 );
355 1101 : C[i] = ( 6400.0f - lsf_tmp[M - 1] - ftmp ) * ( 6400.0f - lsf_tmp[M - 1] - ftmp );
356 : }
357 : else
358 : {
359 1800 : lsp2lsf( &tmp[i * M], lsf_tmp, M, INT_FS_16k );
360 1800 : ftmp = 8000.0f / ( M + 1 );
361 1800 : C[i] = ( 8000.0f - lsf_tmp[M - 1] - ftmp ) * ( 8000.0f - lsf_tmp[M - 1] - ftmp );
362 : }
363 :
364 2901 : C[i] += ( lsf_tmp[0] - ftmp ) * ( lsf_tmp[0] - ftmp );
365 :
366 46416 : for ( j = 0; j < M - 1; j++ )
367 : {
368 43515 : C[i] += ( lsf_tmp[j + 1] - lsf_tmp[j] - ftmp ) * ( lsf_tmp[j + 1] - lsf_tmp[j] - ftmp );
369 : }
370 :
371 2901 : C[i] *= 0.0588235f; /* 1/M+1 */
372 :
373 2901 : if ( C[i] > max_val[0] )
374 : {
375 2065 : max_val[1] = max_val[0];
376 2065 : max_idx[1] = max_idx[0];
377 2065 : max_val[0] = C[i];
378 2065 : max_idx[0] = i;
379 : }
380 836 : else if ( C[i] > max_val[1] )
381 : {
382 490 : max_val[1] = C[i];
383 490 : max_idx[1] = i;
384 : }
385 : }
386 :
387 1245 : if ( m == 1 )
388 : {
389 413 : mvr2r( tmp, lsp_tmp, M );
390 : }
391 832 : else if ( m < 4 )
392 : {
393 10999 : for ( i = 0; i < M; i++ )
394 : {
395 10352 : lsp_tmp[i] = 0.0f;
396 36368 : for ( j = 0; j < m; j++ )
397 : {
398 26016 : lsp_tmp[i] += tmp[j * M + i];
399 : }
400 :
401 10352 : lsp_tmp[i] -= tmp[max_idx[0] * M + i];
402 10352 : lsp_tmp[i] /= (float) ( m - 1 );
403 : }
404 : }
405 : else
406 : {
407 3145 : for ( i = 0; i < M; i++ )
408 : {
409 2960 : lsp_tmp[i] = 0.0f;
410 16752 : for ( j = 0; j < m; j++ )
411 : {
412 13792 : lsp_tmp[i] += tmp[j * M + i];
413 : }
414 :
415 2960 : lsp_tmp[i] -= ( tmp[max_idx[0] * M + i] + tmp[max_idx[1] * M + i] );
416 2960 : lsp_tmp[i] /= (float) ( m - 2 );
417 : }
418 : }
419 :
420 1245 : dist = 0.0f;
421 1245 : max_dev = 0.0f;
422 21165 : for ( i = 0; i < M; i++ )
423 : {
424 19920 : dev = (float) fabs( lsp_tmp[i] - lsp_new[i] );
425 19920 : dist += dev;
426 19920 : if ( dev > max_dev )
427 : {
428 5867 : max_dev = dev;
429 : }
430 : }
431 :
432 1245 : if ( dist > 0.4f || max_dev > 0.1f )
433 : {
434 1632 : for ( i = 0; i < M; i++ )
435 : {
436 1536 : st->lspCNG[i] = lsp_tmp[i];
437 : }
438 : }
439 : else
440 : {
441 19533 : for ( i = 0; i < M; i++ )
442 : {
443 : /* AR low-pass filter */
444 18384 : st->lspCNG[i] = 0.8f * lsp_tmp[i] + ( 1 - 0.8f ) * lsp_new[i];
445 : }
446 : }
447 1245 : if ( m1 > 0 )
448 : {
449 21609 : for ( i = 0; i < NUM_ENV_CNG; i++ )
450 : {
451 20580 : env[i] = 0;
452 66060 : for ( j = 0; j < m1; j++ )
453 : {
454 45480 : env[i] += tmp_env[j * NUM_ENV_CNG + i];
455 : }
456 :
457 20580 : env[i] /= (float) m1;
458 20580 : env[i] = env[i] - 2 * st->lp_ener;
459 : }
460 1029 : mvr2r( env, hTdCngDec->lp_env, NUM_ENV_CNG );
461 : }
462 : }
463 : else
464 : {
465 563 : mvr2r( lsp_new, st->lspCNG, M ); /* use newly analyzed parameters */
466 : }
467 : }
468 :
469 21709 : if ( st->core_brate == SID_1k75 || st->core_brate == SID_2k40 )
470 : {
471 : /* Update hangover memory during CNG */
472 3712 : if ( !*allow_cn_step && ( hTdCngDec->Enew < 1.5f * st->lp_ener ) )
473 : {
474 : /* update the pointer to circular buffer of old LSP vectors */
475 2306 : if ( ++( hTdCngDec->ho_hist_ptr ) == HO_HIST_SIZE )
476 : {
477 301 : hTdCngDec->ho_hist_ptr = 0;
478 : }
479 :
480 : /* update the circular buffer of old LSP vectors with the new LSP vector */
481 2306 : mvr2r( lsp_new, &( hTdCngDec->ho_lsp_hist[( hTdCngDec->ho_hist_ptr ) * M] ), M );
482 :
483 : /* update the hangover energy buffer */
484 2306 : hTdCngDec->ho_ener_hist[hTdCngDec->ho_hist_ptr] = hTdCngDec->Enew;
485 :
486 2306 : if ( st->core_brate == SID_2k40 && *sid_bw == 0 )
487 : {
488 336 : enr1 = (float) log10( hTdCngDec->Enew * st->L_frame + 0.1f ) / (float) log10( 2.0f );
489 7056 : for ( i = 0; i < NUM_ENV_CNG; i++ )
490 : {
491 : /* get quantized envelope */
492 6720 : env[i] = (float) ( pow( 2.0f, ( enr1 - q_env[i] ) ) + 2 * hTdCngDec->Enew );
493 : }
494 336 : hTdCngDec->ho_sid_bw = ( hTdCngDec->ho_sid_bw & 0x3fffffffL ) << 1;
495 336 : mvr2r( env, &( hTdCngDec->ho_env_hist[( hTdCngDec->ho_hist_ptr ) * NUM_ENV_CNG] ), NUM_ENV_CNG );
496 : }
497 1970 : else if ( *sid_bw != 0 )
498 : {
499 1868 : hTdCngDec->ho_sid_bw = ( hTdCngDec->ho_sid_bw & 0x3fffffffL ) << 1;
500 1868 : hTdCngDec->ho_sid_bw |= 0x1L;
501 : }
502 2306 : if ( ++( hTdCngDec->ho_hist_size ) > HO_HIST_SIZE )
503 : {
504 1391 : hTdCngDec->ho_hist_size = HO_HIST_SIZE;
505 : }
506 : }
507 :
508 : /* Update the frame length memory */
509 3712 : st->last_CNG_L_frame = st->L_frame;
510 :
511 3712 : if ( st->core_brate != SID_1k75 )
512 : {
513 3608 : hTdCngDec->num_ho = m;
514 : }
515 : }
516 :
517 21709 : if ( st->Opt_AMR_WB )
518 : {
519 891 : isp2a( st->lspCNG, Aq, M );
520 : }
521 : else
522 : {
523 20818 : lsp2a_stab( st->lspCNG, Aq, M );
524 : }
525 :
526 98828 : for ( i = 1; i < st->L_frame / L_SUBFR; i++ )
527 : {
528 77119 : mvr2r( Aq, &Aq[i * ( M + 1 )], M + 1 );
529 : }
530 :
531 21709 : return;
532 : }
533 :
534 :
535 : /*---------------------------------------------------------------------*
536 : * swb_CNG_dec()
537 : *
538 : * Comfort noise generation for SHB signal
539 : *---------------------------------------------------------------------*/
540 :
541 7336036 : 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 7336036 : if ( st->core_brate == FRAME_NO_DATA || st->core_brate == SID_2k40 )
549 : {
550 : /* SHB SID decoding and CNG */
551 118111 : if ( st->cng_type == LP_CNG && st->extl == SWB_CNG )
552 : {
553 15549 : shb_CNG_decod( st, synth, shb_synth, sid_bw );
554 : }
555 118111 : st->last_vad = 0;
556 118111 : st->hTdCngDec->burst_cnt = 0;
557 : }
558 : else
559 : {
560 7217925 : st->last_vad = 1;
561 7217925 : st->hTdCngDec->burst_cnt++;
562 7217925 : if ( st->hTdCngDec->burst_cnt > 10 )
563 : {
564 624499 : st->hTdCngDec->burst_cnt = 0;
565 : }
566 : }
567 :
568 7336036 : return;
569 : }
570 :
571 : /*---------------------------------------------------------------------*
572 : * shb_CNG_decod()
573 : *
574 : * Main routine of SHB SID decoding and CNG
575 : *---------------------------------------------------------------------*/
576 :
577 15549 : 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 15549 : int16_t allow_cn_step = 0;
600 :
601 15549 : hTdCngDec = st->hTdCngDec;
602 :
603 15549 : if ( !st->bfi )
604 : {
605 15549 : if ( st->core_brate == SID_2k40 && sid_bw == 1 )
606 : {
607 2466 : idx_ener = get_next_indice( st, 4 );
608 :
609 2466 : if ( idx_ener == 0 )
610 : {
611 549 : idx_ener = -15;
612 : }
613 :
614 : /* de-quantization of SHB CNG parameters */
615 2466 : 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 2466 : 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 15549 : interp = ( (float) hTdCngDec->shb_dtx_count ) / 32;
628 15549 : interp = min( interp, 1.0f );
629 171039 : for ( i = 0; i < LPC_SHB_ORDER; i++ )
630 : {
631 155490 : shb_lspCNG[i] = interp * hTdCngDec->lsp_shb_prev[i];
632 155490 : shb_lspCNG[i] += ( 1 - interp ) * hTdCngDec->lsp_shb_prev_prev[i];
633 : }
634 :
635 15549 : if ( hTdCngDec->shb_dtx_count < 1000 )
636 : {
637 15549 : hTdCngDec->shb_dtx_count++;
638 : }
639 :
640 15549 : lsp2a( shb_lpcCNG, shb_lspCNG, LPC_SHB_ORDER );
641 15549 : shb_lpcCNG[0] = 1.0f;
642 :
643 15549 : mvr2r( shb_lpcCNG, st->hTdCngDec->shb_lpcCNG, LPC_SHB_ORDER + 1 );
644 :
645 : /* SHB energy estimation */
646 15549 : wb_ener = 0.001f;
647 :
648 15549 : if ( st->element_mode != IVAS_CPE_DFT )
649 : {
650 1938384 : for ( i = 0; i < L_FRAME32k; i++ )
651 : {
652 1935360 : wb_ener += synth[i] * synth[i];
653 : }
654 : }
655 :
656 15549 : wb_ener /= L_FRAME32k;
657 15549 : wb_ener = 10 * (float) log10( wb_ener );
658 :
659 15549 : if ( st->first_CNG == 0 )
660 : {
661 87 : hTdCngDec->wb_cng_ener = wb_ener;
662 : }
663 15549 : if ( fabs( wb_ener - hTdCngDec->wb_cng_ener ) > 12.0f )
664 : {
665 6 : allow_cn_step = 1;
666 : }
667 :
668 15549 : if ( allow_cn_step == 1 )
669 : {
670 6 : hTdCngDec->wb_cng_ener = wb_ener;
671 : }
672 : else
673 : {
674 15543 : ftmp = wb_ener - hTdCngDec->wb_cng_ener;
675 15543 : hTdCngDec->wb_cng_ener += 0.9f * ftmp;
676 : }
677 15549 : if ( st->core_brate == SID_2k40 && sid_bw == 1 && !st->bfi )
678 : {
679 2466 : hTdCngDec->last_wb_cng_ener = hTdCngDec->wb_cng_ener;
680 :
681 2466 : if ( st->first_CNG == 0 )
682 : {
683 87 : hTdCngDec->shb_cng_ener = hTdCngDec->last_shb_cng_ener;
684 : }
685 : }
686 :
687 15549 : gain = hTdCngDec->wb_cng_ener - hTdCngDec->last_wb_cng_ener;
688 15549 : if ( gain > 15 )
689 : {
690 0 : gain = 15;
691 : }
692 15549 : step = gain + hTdCngDec->last_shb_cng_ener - hTdCngDec->shb_cng_ener;
693 15549 : if ( allow_cn_step == 1 || st->last_core_brate > SID_2k40 )
694 : {
695 1323 : hTdCngDec->shb_cng_ener += step;
696 : }
697 : else
698 : {
699 14226 : hTdCngDec->shb_cng_ener += 0.25f * step;
700 : }
701 : /* generate white noise excitation */
702 4991229 : for ( i = 0; i < L_FRAME16k; i++ )
703 : {
704 4975680 : excTmp[i] = (float) own_random( &hTdCngDec->swb_cng_seed );
705 : }
706 :
707 : /* synthesis filtering */
708 15549 : 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 15549 : ener_excSHB = 0.001f;
712 4991229 : for ( i = 0; i < L_FRAME16k; i++ )
713 : {
714 4975680 : ener_excSHB += excSHB[i] * excSHB[i];
715 : }
716 :
717 15549 : if ( st->last_vad == 1 )
718 : {
719 1320 : if ( hTdCngDec->burst_cnt > 3 && st->last_core != HQ_CORE )
720 : {
721 540 : hTdCngDec->trans_cnt = 5;
722 : }
723 : else
724 : {
725 780 : hTdCngDec->trans_cnt = 0;
726 : }
727 : }
728 :
729 15549 : if ( hTdCngDec->trans_cnt > 0 )
730 : {
731 1707 : i = (int16_t) ( (float) hTdCngDec->trans_cnt / 15.0f * 255 );
732 1707 : ener = hTdCngDec->shb_cng_ener + sin_table256[i] * ( hTdCngDec->last_shb_ener - hTdCngDec->shb_cng_ener );
733 1707 : hTdCngDec->trans_cnt--;
734 : }
735 : else
736 : {
737 13842 : ener = hTdCngDec->shb_cng_ener;
738 : }
739 :
740 :
741 15549 : gain = (float) sqrt( pow( 10, 0.1f * ener ) * L_FRAME16k / ener_excSHB );
742 :
743 15549 : 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 4991229 : for ( i = 0; i < L_FRAME16k; i++ )
749 : {
750 4975680 : shb_syn16k[i] = gain * excSHB[i];
751 : }
752 :
753 : /* generate 32kHz SHB signal (12.8 - 14.4kHz) from 12.8kHz signal */
754 15549 : 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 15549 : if ( st->output_Fs == 48000 )
757 : {
758 9072 : interpolate_3_over_2_allpass( shb_synth, L_FRAME32k, shb_synth, hTdCngDec->interpol_3_2_cng_dec );
759 : }
760 :
761 15549 : ResetSHBbuffer_Dec( st->hBWE_TD, st->extl );
762 :
763 15549 : return;
764 : }
765 :
766 :
767 : /*-------------------------------------------------------------------*
768 : * td_cng_dec_init()
769 : *
770 : *
771 : *-------------------------------------------------------------------*/
772 :
773 62200 : 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 62200 : hTdCngDec = st->hTdCngDec;
781 :
782 62200 : hTdCngDec->cng_seed = RANDOM_INITSEED;
783 62200 : hTdCngDec->cng_ener_seed = RANDOM_INITSEED;
784 62200 : hTdCngDec->cng_ener_seed1 = RANDOM_INITSEED;
785 62200 : hTdCngDec->old_enr_index = -1;
786 62200 : hTdCngDec->Enew = 0.0f;
787 62200 : mvr2r( st->lsp_old, st->lspCNG, M );
788 62200 : hTdCngDec->last_allow_cn_step = 0;
789 62200 : hTdCngDec->shb_cng_ener = -6.02f;
790 62200 : if ( st->element_mode != EVS_MONO )
791 : {
792 62136 : set_f( hTdCngDec->shb_lpcCNG, 0.0f, LPC_SHB_ORDER + 1 );
793 62136 : hTdCngDec->shb_lpcCNG[0] = 1.0f;
794 62136 : hTdCngDec->shb_cng_gain = -82.0; /* a dB value approximately corresponding to shb index 0(used as index -15) */
795 : }
796 :
797 62200 : hTdCngDec->wb_cng_ener = -6.02f;
798 62200 : hTdCngDec->last_wb_cng_ener = -6.02f;
799 62200 : hTdCngDec->last_shb_cng_ener = -6.02f;
800 62200 : hTdCngDec->swb_cng_seed = RANDOM_INITSEED;
801 62200 : hTdCngDec->ho_hist_ptr = -1;
802 62200 : hTdCngDec->ho_sid_bw = 0;
803 62200 : set_f( hTdCngDec->ho_lsp_hist, 0, HO_HIST_SIZE * M );
804 62200 : set_f( hTdCngDec->ho_ener_hist, 0, HO_HIST_SIZE );
805 62200 : set_f( hTdCngDec->ho_env_hist, 0, HO_HIST_SIZE * NUM_ENV_CNG );
806 62200 : hTdCngDec->ho_hist_size = 0;
807 62200 : hTdCngDec->act_cnt = 0;
808 62200 : hTdCngDec->ho_circ_ptr = -1;
809 62200 : set_f( hTdCngDec->ho_lsp_circ, 0, HO_HIST_SIZE * M );
810 62200 : set_f( hTdCngDec->ho_ener_circ, 0, HO_HIST_SIZE );
811 62200 : set_f( hTdCngDec->ho_env_circ, 0, HO_HIST_SIZE * NUM_ENV_CNG );
812 62200 : hTdCngDec->ho_circ_size = 0;
813 :
814 62200 : set_s( hTdCngDec->ho_16k_lsp, 0, HO_HIST_SIZE );
815 62200 : st->CNG_mode = -1;
816 62200 : hTdCngDec->act_cnt2 = 0;
817 62200 : hTdCngDec->num_ho = 0;
818 62200 : set_f( hTdCngDec->lp_env, 0.0f, NUM_ENV_CNG );
819 62200 : set_f( hTdCngDec->exc_mem, 0.0f, 24 );
820 62200 : set_f( hTdCngDec->exc_mem1, 0.0f, 30 );
821 62200 : set_f( hTdCngDec->old_env, 0.0f, NUM_ENV_CNG );
822 :
823 684200 : for ( i = 0; i < LPC_SHB_ORDER; i++ )
824 : {
825 622000 : if ( st->element_mode != EVS_MONO )
826 : {
827 621360 : hTdCngDec->lsp_shb_prev[i] = 0.5f * ( (float) ( i + 1 ) ) / ( (float) ( LPC_SHB_ORDER + 1 ) );
828 : }
829 : else
830 : {
831 640 : hTdCngDec->lsp_shb_prev[i] = 0.5f * ( (float) i ) / ( (float) LPC_SHB_ORDER );
832 : }
833 622000 : hTdCngDec->lsp_shb_prev_prev[i] = hTdCngDec->lsp_shb_prev[i];
834 : }
835 :
836 62200 : hTdCngDec->shb_dtx_count = 0;
837 62200 : hTdCngDec->trans_cnt = 0;
838 62200 : hTdCngDec->burst_cnt = 0;
839 :
840 62200 : hTdCngDec->last_shb_ener = 0.001f;
841 :
842 :
843 62200 : set_f( hTdCngDec->interpol_3_2_cng_dec, 0.0f, INTERP_3_2_MEM_LEN );
844 :
845 62200 : return;
846 : }
|