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