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 <math.h>
39 : #include "options.h"
40 : #ifdef DEBUGGING
41 : #include "debug.h"
42 : #endif
43 : #include "cnst.h"
44 : #include "rom_com.h"
45 : #include "prot.h"
46 : #include "ivas_prot.h"
47 : #include "wmc_auto.h"
48 :
49 : /*-------------------------------------------------------------------*
50 : * decod_audio()
51 : *
52 : * Decode audio (AC) frames
53 : *-------------------------------------------------------------------*/
54 :
55 45210 : void decod_audio(
56 : Decoder_State *st, /* i/o: decoder static memory */
57 : float dct_epit[], /* o : GSC excitation in DCT domain */
58 : const float *Aq, /* i : LP filter coefficient */
59 : float *tmp_noise, /* o : long term temporary noise energy */
60 : float *pitch_buf, /* o : floating pitch values for each subframe */
61 : float *voice_factors, /* o : voicing factors */
62 : float *exc, /* i/o: adapt. excitation exc */
63 : float *exc2, /* i/o: adapt. excitation/total exc */
64 : float *bwe_exc, /* o : excitation for SWB TBE */
65 : float *lsf_new, /* i : ISFs at the end of the frame */
66 : float *gain_buf, /* o : floating pitch gain for each subframe */
67 : const int16_t tdm_lp_reuse_flag, /* i : LPC reuse flag */
68 : const int16_t tdm_low_rate_mode, /* i : secondary channel low rate mode flag */
69 : const int16_t tdm_Pitch_reuse_flag, /* i : primary channel pitch reuse flag */
70 : const float tdm_Pri_pitch_buf[] /* i : primary channel pitch buffer */
71 : )
72 : {
73 : int16_t tmp_nb_bits_tot, pit_band_idx;
74 : GSC_DEC_HANDLE hGSCDec;
75 : float code[4 * L_SUBFR];
76 : int16_t Diff_len, nb_subfr, i;
77 : int16_t nb_frame_flg;
78 45210 : float Es_pred = 0.0f;
79 : int16_t Len, max_len;
80 : int16_t attack_flag;
81 : float low_pit;
82 : int16_t last_bin;
83 : int16_t nbits;
84 : float exc_wo_nf[L_FRAME16k];
85 : int16_t nb_bits; /* number of bits */
86 : int16_t indice; /* parameter indices to read */
87 :
88 45210 : hGSCDec = st->hGSCDec;
89 :
90 : /*---------------------------------------------------------------*
91 : * Initialization
92 : *---------------------------------------------------------------*/
93 :
94 45210 : Diff_len = 0;
95 :
96 : /* decode GSC attack flag (used to reduce possible pre-echo) */
97 45210 : attack_flag = get_next_indice( st, 1 );
98 :
99 : /* decode GSC SWB speech flag */
100 45210 : if ( st->GSC_IVAS_mode >= 1 || ( st->coder_type != INACTIVE && ( ( st->element_mode == EVS_MONO && st->total_brate >= ACELP_13k20 ) ||
101 18606 : ( st->element_mode > EVS_MONO && st->total_brate > MIN_BRATE_GSC_NOISY_FLAG && st->bwidth >= SWB && !st->flag_ACELP16k ) ) ) )
102 : {
103 16704 : st->GSC_noisy_speech = get_next_indice( st, 1 );
104 : }
105 :
106 : /* safety check in case of bit errors */
107 45210 : if ( st->GSC_noisy_speech && st->bwidth < SWB && st->GSC_IVAS_mode == 0 )
108 : {
109 0 : st->BER_detect = 1;
110 0 : st->GSC_noisy_speech = 0;
111 : }
112 :
113 : /* set bit-allocation */
114 45210 : config_acelp1( DEC, st->total_brate, st->core_brate, st->core, st->extl_orig, st->extl_brate_orig, st->L_frame, st->GSC_noisy_speech, &( st->acelp_cfg ), st->next_bit_pos, st->coder_type, st->inactive_coder_type_flag, -1, 1, &nb_bits, NULL, st->element_mode, &nbits /*dummy*/, tdm_lp_reuse_flag, tdm_low_rate_mode, st->idchan, st->active_cnt, tdm_Pitch_reuse_flag, st->tdm_LRTD_flag, st->GSC_IVAS_mode );
115 :
116 : /*---------------------------------------------------------------*
117 : * Decode energy dynamics
118 : *---------------------------------------------------------------*/
119 :
120 45210 : if ( st->GSC_IVAS_mode >= 1 || ( st->GSC_noisy_speech == 1 && st->GSC_IVAS_mode == 0 ) )
121 : {
122 16698 : nb_subfr = NB_SUBFR;
123 16698 : hGSCDec->cor_strong_limit = 0;
124 16698 : hGSCDec->noise_lev = NOISE_LEVEL_SP3;
125 :
126 16698 : if ( st->GSC_IVAS_mode >= 1 )
127 : {
128 14643 : if ( st->core_brate < GSC_L_RATE_STG && st->GSC_IVAS_mode < 3 )
129 : {
130 5988 : nb_subfr = 2;
131 : }
132 14643 : hGSCDec->noise_lev = NOISE_LEVEL_SP2;
133 :
134 14643 : if ( st->GSC_IVAS_mode == 3 ) /* Music like */
135 : {
136 8532 : hGSCDec->noise_lev = NOISE_LEVEL_SP0;
137 : }
138 6111 : else if ( st->GSC_noisy_speech == 0 ) /* speech like but not noisy */
139 : {
140 5787 : hGSCDec->noise_lev = NOISE_LEVEL_SP3;
141 : }
142 : }
143 : }
144 : else
145 : {
146 28512 : if ( st->core_brate <= ACELP_8k00 )
147 : {
148 15186 : hGSCDec->noise_lev = get_next_indice( st, 2 ) + NOISE_LEVEL_SP2;
149 : }
150 : else
151 : {
152 13326 : hGSCDec->noise_lev = get_next_indice( st, 3 ) + NOISE_LEVEL_SP0;
153 : }
154 :
155 : /*---------------------------------------------------------------*
156 : * Decode number of subframes
157 : *---------------------------------------------------------------*/
158 :
159 28512 : if ( st->L_frame == L_FRAME16k && ( st->core_brate <= ACELP_13k20 || st->coder_type == INACTIVE ) )
160 : {
161 7872 : hGSCDec->cor_strong_limit = 0;
162 7872 : nb_subfr = 1;
163 : }
164 : else
165 : {
166 20640 : hGSCDec->cor_strong_limit = 1;
167 20640 : nb_subfr = SWNB_SUBFR;
168 20640 : if ( st->core_brate >= ACELP_9k60 )
169 : {
170 3864 : nbits = 1;
171 :
172 3864 : if ( st->L_frame == L_FRAME16k && st->core_brate >= MIN_RATE_4SBFR )
173 : {
174 0 : nbits = 2;
175 : }
176 :
177 3864 : nb_frame_flg = get_next_indice( st, nbits );
178 :
179 3864 : if ( ( nb_frame_flg & 0x1 ) == 0 )
180 : {
181 3843 : nb_subfr = 2 * SWNB_SUBFR;
182 3843 : hGSCDec->cor_strong_limit = 0;
183 : }
184 21 : else if ( st->L_frame == L_FRAME16k && st->core_brate >= MIN_RATE_4SBFR )
185 : {
186 0 : nb_subfr = 2 * SWNB_SUBFR; /* cor_strong already set to 1 */
187 : }
188 :
189 3864 : if ( ( nb_frame_flg >> 1 ) == 1 )
190 : {
191 0 : nb_subfr *= 2;
192 : }
193 : }
194 : }
195 : }
196 :
197 45210 : if ( st->L_frame == L_FRAME16k && nb_subfr == NB_SUBFR )
198 : {
199 0 : nb_subfr = NB_SUBFR16k;
200 : }
201 :
202 : /*---------------------------------------------------------------*
203 : * Decode the last band where the adaptive (pitch) contribution is significant
204 : *---------------------------------------------------------------*/
205 :
206 45210 : if ( st->core_brate < CFREQ_BITRATE )
207 : {
208 29364 : if ( st->core_brate < ACELP_9k60 && st->coder_type == INACTIVE )
209 : {
210 993 : nbits = 1;
211 : }
212 : else
213 : {
214 28371 : nbits = 3;
215 : }
216 : }
217 : else
218 : {
219 15846 : nbits = 4;
220 : }
221 :
222 45210 : if ( st->core_brate < ACELP_9k60 && st->coder_type != INACTIVE )
223 : {
224 22287 : pit_band_idx = 1;
225 : }
226 : else
227 : {
228 22923 : pit_band_idx = get_next_indice( st, nbits );
229 : }
230 :
231 45210 : if ( pit_band_idx != 0 )
232 : {
233 38538 : if ( st->core_brate < ACELP_9k60 )
234 : {
235 22776 : pit_band_idx = 7 + BAND1k2; /* At low rate, if pitch model is chosen, then for to be use on extented and constant frequency range */
236 : }
237 : else
238 : {
239 15762 : pit_band_idx += BAND1k2;
240 : }
241 :
242 : /* detect bit errors in the bitstream */
243 38538 : if ( pit_band_idx > 13 ) /* The maximum decodable index is 10 + BAND1k2 (3) = 13 */
244 : {
245 0 : pit_band_idx = 13;
246 0 : st->BER_detect = 1;
247 : }
248 :
249 38538 : Diff_len = (int16_t) ( mfreq_loc[pit_band_idx] / BIN_SIZE );
250 : }
251 :
252 45210 : hGSCDec->Last_GSC_pit_band_idx = pit_band_idx;
253 :
254 : /*--------------------------------------------------------------------------------------*
255 : * Decode adaptive (pitch) excitation contribution
256 : * Reset unvaluable part of the adaptive (pitch) excitation contribution
257 : *--------------------------------------------------------------------------------------*/
258 :
259 45210 : if ( pit_band_idx > BAND1k2 )
260 : {
261 : /*---------------------------------------------------------------*
262 : * Decode adaptive (pitch) excitation contribution
263 : *---------------------------------------------------------------*/
264 :
265 38538 : if ( !( st->GSC_IVAS_mode > 0 && st->L_frame / nb_subfr == 2 * L_SUBFR && st->GSC_IVAS_mode < 3 ) &&
266 32550 : ( ( st->core_brate >= MIN_RATE_FCB || st->GSC_noisy_speech ) &&
267 3051 : ( ( nb_subfr == NB_SUBFR && st->L_frame == L_FRAME ) || ( nb_subfr == NB_SUBFR16k && st->L_frame == L_FRAME16k ) ) ) )
268 : {
269 2055 : if ( st->element_mode > EVS_MONO )
270 : {
271 2055 : nb_bits = 5;
272 : }
273 : else
274 : {
275 0 : nb_bits = Es_pred_bits_tbl[BIT_ALLOC_IDX( st->core_brate, GENERIC, -1, -1 )];
276 : }
277 :
278 2055 : indice = get_next_indice( st, nb_bits );
279 2055 : Es_pred_dec( &Es_pred, indice, nb_bits, 0 );
280 : }
281 :
282 38538 : dec_pit_exc( st, st->L_frame, Aq, Es_pred, pitch_buf, code, exc, nb_subfr, gain_buf, tdm_Pitch_reuse_flag, tdm_Pri_pitch_buf );
283 :
284 38538 : if ( st->core_brate < ACELP_9k60 )
285 : {
286 22776 : minimum( pitch_buf, st->L_frame >> 6, &low_pit );
287 :
288 22776 : if ( low_pit < 64 )
289 : {
290 10026 : pit_band_idx = 9 + BAND1k2;
291 10026 : if ( st->bwidth == NB )
292 : {
293 0 : pit_band_idx = 7 + BAND1k2;
294 : }
295 : }
296 12750 : else if ( low_pit < 128 )
297 : {
298 7290 : pit_band_idx = 5 + BAND1k2;
299 : }
300 : else
301 : {
302 5460 : pit_band_idx = 3 + BAND1k2;
303 : }
304 22776 : Diff_len = (int16_t) ( mfreq_loc[pit_band_idx] / BIN_SIZE );
305 22776 : hGSCDec->Last_GSC_pit_band_idx = pit_band_idx;
306 : }
307 :
308 : /*---------------------------------------------------------------*
309 : * DCT transform
310 : *---------------------------------------------------------------*/
311 :
312 38538 : edct( exc, dct_epit, st->L_frame, st->element_mode );
313 :
314 : /*---------------------------------------------------------------*
315 : * Reset unvaluable part of the adaptive (pitch) excitation contribution
316 : *---------------------------------------------------------------*/
317 :
318 38538 : max_len = st->L_frame - Diff_len;
319 :
320 38538 : if ( st->bwidth == NB )
321 : {
322 0 : max_len = 160 - Diff_len;
323 : }
324 :
325 38538 : Len = 80;
326 38538 : if ( max_len < 80 )
327 : {
328 10788 : Len = max_len;
329 : }
330 :
331 38538 : if ( st->core_brate == ACELP_8k00 && st->bwidth != NB )
332 : {
333 12258 : for ( i = 0; i < max_len; i++ )
334 : {
335 12129 : dct_epit[i + Diff_len] = 0.0f;
336 : }
337 : }
338 : else
339 : {
340 2941410 : for ( i = 0; i < Len; i++ )
341 : {
342 2903001 : dct_epit[i + Diff_len] *= sm_table[i];
343 : }
344 1991961 : for ( ; i < max_len; i++ )
345 : {
346 1953552 : dct_epit[i + Diff_len] = 0.0f;
347 : }
348 : }
349 :
350 38538 : st->bfi_pitch = (int16_t) ( mean( pitch_buf, nb_subfr ) + 0.5f );
351 38538 : st->bfi_pitch_frame = st->L_frame;
352 38538 : Diff_len++;
353 38538 : st->bpf_off = 0;
354 : }
355 : else
356 : {
357 : /* No adaptive (pitch) excitation contribution */
358 6672 : st->bpf_off = 1;
359 :
360 6672 : set_f( dct_epit, 0.0f, st->L_frame );
361 :
362 6672 : if ( st->L_frame == L_FRAME16k )
363 : {
364 4641 : set_f( pitch_buf, (float) L_SUBFR16k, NB_SUBFR16k );
365 : }
366 : else
367 : {
368 2031 : set_f( pitch_buf, (float) L_SUBFR, NB_SUBFR );
369 : }
370 :
371 6672 : set_f( gain_buf, 0.f, NB_SUBFR16k );
372 :
373 6672 : st->bfi_pitch = L_SUBFR;
374 6672 : st->bfi_pitch_frame = st->L_frame;
375 6672 : st->lp_gainp = 0.0f;
376 6672 : st->lp_gainc = 0.0f;
377 6672 : st->tilt_code = 0;
378 6672 : pit_band_idx = 0;
379 6672 : Diff_len = 0;
380 : }
381 :
382 : /*--------------------------------------------------------------------------------------*
383 : * GSC decoder
384 : *--------------------------------------------------------------------------------------*/
385 :
386 : /* find the current total number of bits used */
387 45210 : tmp_nb_bits_tot = st->next_bit_pos;
388 :
389 45210 : if ( st->extl_brate_orig > 0 )
390 : {
391 : /* subtract 1 bit for TBE/BWE BWE flag (bit counted in extl_brate) */
392 44865 : tmp_nb_bits_tot--;
393 : }
394 :
395 45210 : if ( st->coder_type == INACTIVE && st->core_brate <= ACELP_9k60 && st->idchan == 0 )
396 : {
397 1038 : tmp_nb_bits_tot += 5; /* for noisiness */
398 : }
399 :
400 45210 : if ( st->idchan == 1 )
401 : {
402 0 : tmp_nb_bits_tot += TDM_SIGNAL_BITS_READ_FROM_THE_END_OF_BS;
403 0 : if ( st->tdm_LRTD_flag == 1 )
404 : {
405 0 : tmp_nb_bits_tot -= STEREO_BITS_TCA;
406 : }
407 : }
408 :
409 45210 : gsc_dec( st, dct_epit, pit_band_idx, Diff_len, tmp_nb_bits_tot, nb_subfr, st->coder_type, &last_bin, lsf_new, exc_wo_nf, tmp_noise );
410 :
411 : /*--------------------------------------------------------------------------------------*
412 : * iDCT transform
413 : *--------------------------------------------------------------------------------------*/
414 :
415 45210 : edct( dct_epit, exc, st->L_frame, st->element_mode );
416 :
417 45210 : edct( exc_wo_nf, exc_wo_nf, st->L_frame, st->element_mode );
418 :
419 : /*----------------------------------------------------------------------*
420 : * Remove potential pre-echo in case an onset has been detected
421 : *----------------------------------------------------------------------*/
422 :
423 45210 : pre_echo_att( &hGSCDec->Last_frame_ener, exc, attack_flag, st->last_coder_type, st->L_frame );
424 :
425 : /*--------------------------------------------------------------------------------------*
426 : * Update BWE excitation
427 : *--------------------------------------------------------------------------------------*/
428 :
429 45210 : if ( st->hBWE_TD != NULL )
430 : {
431 45210 : if ( st->L_frame == L_FRAME16k )
432 : {
433 7872 : set_f( voice_factors, 0.0f, NB_SUBFR16k );
434 7872 : interp_code_4over2( exc, bwe_exc, st->L_frame );
435 : }
436 : else
437 : {
438 37338 : set_f( voice_factors, 0.0f, NB_SUBFR16k );
439 37338 : interp_code_5over2( exc, bwe_exc, L_FRAME );
440 : }
441 : }
442 :
443 : /*--------------------------------------------------------------------------------------*
444 : * Updates
445 : *--------------------------------------------------------------------------------------*/
446 :
447 45210 : mvr2r( exc, exc2, st->L_frame );
448 45210 : mvr2r( exc_wo_nf, exc, st->L_frame );
449 :
450 : /*--------------------------------------------------------------------------------------*
451 : * Channel aware mode parameters
452 : *--------------------------------------------------------------------------------------*/
453 :
454 45210 : set_f( st->tilt_code_dec, 0, NB_SUBFR16k );
455 :
456 45210 : return;
457 : }
458 :
459 :
460 : /*-------------------------------------------------------------------*
461 : * gsc_dec()
462 : *
463 : * Generic audio signal decoder
464 : *-------------------------------------------------------------------*/
465 :
466 69279 : void gsc_dec(
467 : Decoder_State *st, /* i/o: State structure */
468 : float exc_dct_in[], /* i/o: dct of pitch-only excitation / total excitation */
469 : const int16_t pit_band_idx, /* i : bin position of the cut-off frequency */
470 : const int16_t Diff_len, /* i : Lenght of the difference signal (before pure spectral)*/
471 : const int16_t bits_used, /* i : Number of bit used before frequency Q */
472 : const int16_t nb_subfr, /* i : Number of subframe considered */
473 : const int16_t coder_type, /* i : coding type */
474 : int16_t *last_bin, /* i : last bin of bit allocation */
475 : const float *lsf_new, /* i : ISFs at the end of the frame */
476 : float *exc_wo_nf, /* o : excitation (in f domain) without noisefill */
477 : float *tmp_noise /* o : long-term noise energy */
478 : )
479 : {
480 : int16_t i, j, bit, nb_subbands, pvq_len;
481 : GSC_DEC_HANDLE hGSCDec;
482 : int16_t bitallocation_band[MBANDS_GN_BITALLOC16k];
483 : int16_t bitallocation_exc[2];
484 : float Ener_per_bd_iQ[MBANDS_GN16k];
485 : int16_t max_ener_band[MBANDS_GN_BITALLOC16k];
486 : float exc_diffQ[L_FRAME16k];
487 : int16_t bits_per_bands[MBANDS_GN_BITALLOC16k]; /*Q3*/
488 : float concat_out[L_FRAME16k];
489 : int16_t npulses[NB_SFM];
490 : int16_t maxpulse[NB_SFM];
491 : float mean_gain;
492 69279 : int16_t Mbands_gn = 16;
493 : int16_t seed_init;
494 : float max_eq, max_eq_val;
495 :
496 69279 : hGSCDec = st->hGSCDec;
497 :
498 : /*--------------------------------------------------------------------------------------*
499 : * Initialization
500 : *--------------------------------------------------------------------------------------*/
501 :
502 69279 : bit = bits_used;
503 69279 : if ( coder_type == INACTIVE && ( st->tdm_LRTD_flag == 1 || st->element_mode == IVAS_SCE ) && st->core_brate <= GSC_LRES_GAINQ_LIMIT )
504 : {
505 20970 : bit += GSC_LRES_NB_NITS;
506 : }
507 :
508 69279 : if ( st->L_frame == L_FRAME16k )
509 : {
510 7998 : Mbands_gn = MBANDS_GN16k;
511 : }
512 :
513 69279 : set_f( exc_diffQ, 0.0f, st->L_frame );
514 69279 : set_f( Ener_per_bd_iQ, 0.0f, Mbands_gn );
515 :
516 : /*--------------------------------------------------------------------------------------*
517 : * Gain decoding
518 : *--------------------------------------------------------------------------------------*/
519 :
520 69279 : if ( st->bfi || st->BER_detect )
521 : {
522 : /* copy old gain */
523 1560 : mvr2r( hGSCDec->old_y_gain, Ener_per_bd_iQ, Mbands_gn );
524 1560 : mean_gain = st->lp_gainc / 10.0f;
525 26772 : for ( i = 0; i < Mbands_gn; i++ )
526 : {
527 25212 : Ener_per_bd_iQ[i] += mean_gain;
528 : }
529 :
530 1560 : st->lp_gainc *= 0.98f;
531 : }
532 : else
533 : {
534 67719 : i = 0;
535 314907 : while ( i < SIZE_BRATE_INTERMED_TBL )
536 : {
537 314907 : if ( st->core_brate <= brate_intermed_tbl[i] )
538 : {
539 67719 : break;
540 : }
541 247188 : i++;
542 : }
543 :
544 67719 : if ( st->element_mode > EVS_MONO && coder_type == AUDIO && st->core_brate <= STEREO_GSC_BIT_RATE_ALLOC && brate_intermed_tbl[i] == ACELP_9k60 ) /* Bit allocation is mapped to 8 kb/s instead of 9.6 kb/s in this case */
545 : {
546 4086 : i--;
547 : }
548 67719 : mean_gain = gsc_gaindec( st, Ener_per_bd_iQ, brate_intermed_tbl[i], hGSCDec->old_y_gain, coder_type, st->bwidth );
549 :
550 67719 : st->lp_gainc = 10.0f * mean_gain;
551 : }
552 :
553 69279 : *tmp_noise = st->lp_gainc;
554 :
555 69279 : *last_bin = 0;
556 69279 : if ( st->core_brate == ACELP_8k00 && st->bwidth != NB )
557 : {
558 153 : bitallocation_exc[0] = 0;
559 153 : bitallocation_exc[1] = 0;
560 : }
561 :
562 69279 : set_s( bitallocation_band, 0, MBANDS_GN );
563 :
564 69279 : if ( st->bfi || st->BER_detect )
565 : {
566 1560 : max_eq = 1.0f;
567 :
568 : /*--------------------------------------------------------------------------------------*
569 : * Copy old spectrum
570 : * reduce spectral dynamic
571 : * save spectrum
572 : *--------------------------------------------------------------------------------------*/
573 :
574 1560 : if ( st->last_good == INACTIVE_CLAS || st->Last_GSC_noisy_speech_flag == 1 )
575 : {
576 169974 : for ( i = 0; i < st->L_frame; i++ )
577 : {
578 169344 : hGSCDec->Last_GSC_spectrum[i] = 0.8f * own_random( &hGSCDec->seed_tcx ) / PCM16_TO_FLT_FAC + 0.2f * hGSCDec->Last_GSC_spectrum[i];
579 : }
580 :
581 630 : mvr2r( hGSCDec->Last_GSC_spectrum, exc_diffQ, st->L_frame );
582 : }
583 :
584 1560 : mvr2r( hGSCDec->Last_GSC_spectrum, exc_diffQ, st->L_frame );
585 :
586 408984 : for ( i = 0; i < st->L_frame; i++ )
587 : {
588 407424 : hGSCDec->Last_GSC_spectrum[i] *= 0.75f;
589 : }
590 : }
591 : else
592 : {
593 : /*--------------------------------------------------------------------------------------*
594 : * PVQ decoder
595 : *--------------------------------------------------------------------------------------*/
596 :
597 67719 : bands_and_bit_alloc( hGSCDec->cor_strong_limit, hGSCDec->noise_lev, st->core_brate, Diff_len, bit, &bit, Ener_per_bd_iQ, max_ener_band, bits_per_bands, &nb_subbands, NULL, NULL, &pvq_len, coder_type, st->bwidth, st->GSC_noisy_speech, st->L_frame, st->element_mode, st->GSC_IVAS_mode );
598 :
599 67719 : set_s( npulses, 0, NB_SFM );
600 :
601 67719 : if ( bit == 0 )
602 : {
603 0 : set_zero( concat_out, L_FRAME16k );
604 : }
605 : else
606 : {
607 67719 : pvq_core_dec( st, gsc_sfm_start, gsc_sfm_end, gsc_sfm_size, concat_out, bit, nb_subbands, bits_per_bands, NULL, npulses, maxpulse, ACELP_CORE );
608 : }
609 :
610 67719 : max_eq = 0.0f;
611 67719 : max_eq_val = 1.0f;
612 :
613 67719 : if ( ( ( st->core_brate < ACELP_7k20 && st->GSC_noisy_speech == 1 ) || st->core_brate < 6000 ) && coder_type <= UNVOICED )
614 : {
615 22323 : j = emaximum( concat_out, nb_subbands * 16, &max_eq );
616 22323 : max_eq = (float) ( max_eq_val / ( fabs( concat_out[j] ) + 0.01f ) );
617 22323 : max_eq = min( max_eq_val, max_eq );
618 : }
619 :
620 67719 : seed_init = 0;
621 554043 : for ( j = 0; j < nb_subbands; j++ )
622 : {
623 486324 : mvr2r( concat_out + j * 16, exc_diffQ + max_ener_band[j] * 16, 16 );
624 :
625 486324 : if ( max_ener_band[j] > *last_bin )
626 : {
627 350748 : *last_bin = max_ener_band[j];
628 : }
629 :
630 486324 : bitallocation_band[max_ener_band[j]] = 1;
631 :
632 486324 : seed_init += npulses[j];
633 : }
634 :
635 67719 : if ( st->last_coder_type != AUDIO /* First audio frame */
636 40317 : && st->last_coder_type != UNVOICED ) /* last_coder_type == INACTIVE is overwritten in update_dec to UNVOICED */
637 : {
638 1342671 : for ( j = 0; j < nb_subbands * 16; j++ )
639 : {
640 1332624 : if ( concat_out[j] > 0 )
641 : {
642 193041 : seed_init = (int16_t) ( (int32_t) seed_init * 8 );
643 : }
644 1332624 : if ( concat_out[j] < 0 )
645 : {
646 192549 : seed_init = (int16_t) ( (int32_t) seed_init + 3 );
647 : }
648 : }
649 :
650 10047 : hGSCDec->seed_tcx = seed_init;
651 : }
652 :
653 67719 : if ( st->core_brate == ACELP_8k00 && st->bwidth != NB )
654 : {
655 129 : if ( exc_diffQ[L_FRAME8k - 2] != 0 )
656 : {
657 3 : bitallocation_exc[0] = 1;
658 : }
659 :
660 129 : if ( exc_diffQ[L_FRAME8k - 1] != 0 )
661 : {
662 15 : bitallocation_exc[1] = 1;
663 : }
664 : }
665 :
666 67719 : mvr2r( exc_diffQ, hGSCDec->Last_GSC_spectrum, st->L_frame );
667 :
668 : /*--------------------------------------------------------------------------------------*
669 : * Skip adaptive (pitch) contribution frequency band (no noise added over the time contribution)
670 : * Find x pulses between 1.6-3.2kHz to code in the spectrum of the residual signal
671 : * Gain is based on the inter-correlation gain between the pulses found and residual signal
672 : *--------------------------------------------------------------------------------------*/
673 :
674 67719 : if ( st->GSC_IVAS_mode >= 1 && st->GSC_noisy_speech == 1 )
675 : {
676 62532 : for ( i = 64; i < st->L_frame; i++ )
677 : {
678 62208 : exc_diffQ[i] *= max_eq;
679 : }
680 : }
681 67395 : else if ( ( ( st->core_brate < ACELP_7k20 && st->GSC_noisy_speech == 1 ) || st->core_brate < 6000 ) && coder_type <= UNVOICED )
682 : {
683 5737011 : for ( i = 0; i < L_FRAME; i++ )
684 : {
685 5714688 : exc_diffQ[i] *= max_eq;
686 : }
687 : }
688 : else
689 : {
690 45072 : freq_dnw_scaling( hGSCDec->cor_strong_limit, coder_type, hGSCDec->noise_lev, st->core_brate, exc_diffQ, st->L_frame );
691 : }
692 : }
693 :
694 : /*--------------------------------------------------------------------------------------*
695 : * Estimate noise level
696 : *--------------------------------------------------------------------------------------*/
697 :
698 69279 : highband_exc_dct_in( st->core_brate, mfreq_bindiv_loc, *last_bin, Diff_len, hGSCDec->noise_lev, pit_band_idx, exc_diffQ, &hGSCDec->seed_tcx, Ener_per_bd_iQ, nb_subfr, exc_dct_in, st->last_coder_type, bitallocation_band, lsf_new, hGSCDec->last_exc_dct_in, &hGSCDec->last_ener, hGSCDec->last_bitallocation_band, bitallocation_exc, st->bfi, coder_type, st->bwidth, exc_wo_nf, st->GSC_noisy_speech, hGSCDec->lt_ener_per_band, st->L_frame, st->element_mode, st->GSC_IVAS_mode );
699 :
700 69279 : exc_dct_in[0] = 0;
701 :
702 69279 : return;
703 : }
704 :
705 : /*-------------------------------------------------------------------*
706 : * GSC_dec_init()
707 : *
708 : * Initialize GSC decoder state structure
709 : *-------------------------------------------------------------------*/
710 :
711 11598 : void GSC_dec_init(
712 : GSC_DEC_HANDLE hGSCDec /* i/o: GSC data handle */
713 : )
714 : {
715 11598 : hGSCDec->seed_tcx = 15687;
716 11598 : hGSCDec->cor_strong_limit = 1;
717 11598 : set_f( hGSCDec->old_y_gain, 0, MBANDS_GN );
718 11598 : hGSCDec->noise_lev = NOISE_LEVEL_SP0;
719 11598 : set_f( hGSCDec->Last_GSC_spectrum, 0.0f, L_FRAME16k );
720 11598 : hGSCDec->Last_GSC_pit_band_idx = 0;
721 :
722 11598 : set_f( hGSCDec->lt_ener_per_band, 1.0f, MBANDS_GN16k );
723 11598 : set_f( hGSCDec->last_exc_dct_in, 0, L_FRAME16k );
724 11598 : hGSCDec->last_ener = 0.0f;
725 11598 : set_s( hGSCDec->last_bitallocation_band, 0, 6 );
726 :
727 11598 : hGSCDec->Last_frame_ener = (float) MAX_32;
728 :
729 11598 : return;
730 : }
|