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 "basop_util.h"
47 : #include "basop_proto_func.h"
48 : #include "wmc_auto.h"
49 :
50 : /*-------------------------------------------------------------------*
51 : * para_pred_bws()
52 : *
53 : * predict SWB parameters for bandwidth switching
54 : *-------------------------------------------------------------------*/
55 :
56 82222 : static int16_t para_pred_bws(
57 : Decoder_State *st, /* i/o: decoder state structure */
58 : float *signal_wb, /* i : wideband frequency signal */
59 : float *SWB_fenv /* o : frequency-domain BWE envelope */
60 : )
61 : {
62 : int16_t i, j, k;
63 : int16_t mode;
64 : FD_BWE_DEC_HANDLE hBWE_FD;
65 : float *input_hi;
66 : float peak, mean[7], mag, min_val;
67 : float avrg1, avrg2;
68 : float att;
69 :
70 82222 : hBWE_FD = st->hBWE_FD;
71 :
72 82222 : mode = NORMAL;
73 :
74 82222 : k = 0;
75 82222 : input_hi = &signal_wb[SHARP_WIDTH];
76 657776 : for ( i = 0; i < 7; i++ )
77 : {
78 575554 : peak = 0.0f;
79 575554 : mean[i] = 0;
80 18993282 : for ( j = 0; j < SHARP_WIDTH; j++ )
81 : {
82 18417728 : mag = (float) fabs( *input_hi );
83 18417728 : if ( mag > peak )
84 : {
85 3436271 : peak = mag;
86 : }
87 18417728 : mean[i] += mag;
88 18417728 : input_hi++;
89 : }
90 :
91 575554 : if ( peak * ( SHARP_WIDTH + 3.5f ) > 4.5f * mean[i] && peak > 8.0f )
92 : {
93 2815 : k += 1;
94 : }
95 : }
96 :
97 82222 : avrg1 = 0.0f;
98 82222 : avrg2 = 0.0f;
99 328888 : for ( i = 1; i < 4; i++ )
100 : {
101 246666 : avrg1 += mean[i];
102 246666 : avrg2 += mean[i + 3];
103 : }
104 82222 : avrg1 /= 3;
105 82222 : avrg2 /= 3;
106 :
107 82222 : min_val = FLT_MAX;
108 82222 : peak = 0.0f;
109 328888 : for ( i = 4; i < 7; i++ )
110 : {
111 246666 : if ( mean[i] > 2.0f * avrg2 )
112 : {
113 58613 : mean[i] *= 2 * avrg2 / mean[i];
114 : }
115 246666 : if ( mean[i] < min_val )
116 : {
117 100373 : min_val = mean[i];
118 : }
119 246666 : if ( mean[i] > peak )
120 : {
121 224447 : peak = mean[i];
122 : }
123 : }
124 :
125 82222 : if ( st->tilt_wb > 8 )
126 : {
127 853 : min_val = min( st->tilt_wb / 15.0f, 1.0f ) * peak;
128 : }
129 :
130 82222 : if ( peak == 0 || min_val == 0 )
131 : {
132 102 : set_f( SWB_fenv, 0, SWB_FENV );
133 : }
134 : else
135 : {
136 1231800 : for ( i = 0; i < SWB_FENV; i++ )
137 : {
138 1149680 : SWB_fenv[i] = min_val * mean[i / 5 + 4] / ( 64 * peak );
139 : }
140 : }
141 :
142 657776 : for ( j = 0, i = SWB_FENV / 2; i < SWB_FENV; i++ )
143 : {
144 575554 : SWB_fenv[i] *= ( 1.0f - (float) j++ / SWB_FENV );
145 : }
146 :
147 82222 : if ( avrg1 > 8.0f * avrg2 )
148 : {
149 2715 : for ( i = 0; i < SWB_FENV; i++ )
150 : {
151 2534 : SWB_fenv[i] *= 0.5f;
152 : }
153 : }
154 82222 : if ( st->last_core != HQ_CORE && st->last_codec_mode == MODE1 &&
155 82221 : ( st->enerLH > 0.5f * st->prev_enerLH && st->enerLH < 2.0f * st->prev_enerLH ) &&
156 22 : ( st->enerLL > 0.5f * st->prev_enerLL && st->enerLL < 2.0f * st->prev_enerLL ) )
157 : {
158 330 : for ( i = 0; i < SWB_FENV; i++ )
159 : {
160 308 : if ( st->prev_coder_type != st->coder_type && SWB_fenv[i] > 2.0f * hBWE_FD->prev_SWB_fenv[i] )
161 : {
162 35 : SWB_fenv[i] = 0.1f * SWB_fenv[i] + 0.9f * hBWE_FD->prev_SWB_fenv[i];
163 : }
164 : else
165 : {
166 273 : SWB_fenv[i] = st->attenu1 * SWB_fenv[i] + ( 1.0f - st->attenu1 ) * hBWE_FD->prev_SWB_fenv[i];
167 : }
168 : }
169 :
170 22 : if ( st->attenu1 < 0.9f )
171 : {
172 22 : st->attenu1 += 0.05f;
173 : }
174 : }
175 : else
176 : {
177 82200 : if ( st->core_brate != st->last_core_brate || ( st->enerLH > 0.5f * st->prev_enerLH && st->enerLH < 2.0f * st->prev_enerLH ) ||
178 71758 : ( st->enerLL > 0.5f * st->prev_enerLL && st->enerLL < 2.0f * st->prev_enerLL ) )
179 : {
180 156780 : for ( i = 0; i < SWB_FENV; i++ )
181 : {
182 146328 : if ( SWB_fenv[i] > 2.0f * hBWE_FD->prev_SWB_fenv[i] )
183 : {
184 56346 : SWB_fenv[i] = hBWE_FD->prev_SWB_fenv[i];
185 : }
186 : }
187 : }
188 :
189 1233000 : for ( i = 0; i < SWB_FENV; i++ )
190 : {
191 1150800 : SWB_fenv[i] = 0.9f * SWB_fenv[i] + 0.1f * hBWE_FD->prev_SWB_fenv[i];
192 : }
193 :
194 82200 : st->attenu1 = 0.1f;
195 : }
196 :
197 82222 : if ( k > 3 )
198 : {
199 7 : mode = HARMONIC;
200 : }
201 :
202 82222 : att = ( (float) N_WS2N_FRAMES - (float) st->bws_cnt ) / (float) N_WS2N_FRAMES;
203 82222 : if ( st->L_frame == L_FRAME16k )
204 : {
205 295 : for ( i = 0; i < 4; i++ )
206 : {
207 236 : SWB_fenv[i] *= att;
208 : }
209 : }
210 :
211 904442 : for ( i = 4; i < SWB_FENV; i++ )
212 : {
213 822220 : SWB_fenv[i] *= att;
214 : }
215 :
216 82222 : return mode;
217 : }
218 :
219 : /*-------------------------------------------------------------------*
220 : * WB_BWE_gain_deq()
221 : *
222 : * Decoding of WB parameters
223 : *-------------------------------------------------------------------*/
224 :
225 72543 : static int16_t WB_BWE_gain_deq(
226 : Decoder_State *st, /* i/o: decoder state structure */
227 : float *WB_fenv )
228 : {
229 : int16_t mode;
230 : int16_t index;
231 :
232 72543 : index = get_next_indice( st, 5 );
233 72543 : mode = get_next_indice( st, 1 ) + 2;
234 :
235 72543 : WB_fenv[0] = (float) pow( 2, 0.5f * F_2_5[2 * index] );
236 72543 : WB_fenv[1] = (float) pow( 2, 0.5f * F_2_5[2 * index + 1] );
237 :
238 72543 : return ( mode );
239 : }
240 :
241 : /*-------------------------------------------------------------------*
242 : * wb_bwe_dec()
243 : *
244 : * WB BWE decoder (only for 16kHz signals)
245 : *-------------------------------------------------------------------*/
246 :
247 285632 : void wb_bwe_dec(
248 : Decoder_State *st, /* i/o: decoder state structure */
249 : const float output[], /* i : synthesis @internal Fs */
250 : float *synth, /* i/o: ACELP core synthesis/final synthesis */
251 : float *hb_synth, /* o : SHB synthesis/final synthesis */
252 : const int16_t use_cldfb_for_dft, /* i : flag to use of CLDFB for DFT Stereo */
253 : const int16_t output_frame, /* i : frame length */
254 : const float voice_factors[], /* i : voicing factors */
255 : const float pitch_buf[] /* i : pitch buffer */
256 : )
257 : {
258 : int16_t i, mode;
259 : FD_BWE_DEC_HANDLE hBWE_FD;
260 : float ysynth[L_FRAME48k]; /* MDCT spectrum of core synthesis */
261 : float yerror[L_FRAME48k]; /* MDCT spectrum of error */
262 : float wtda_synth[2 * L_FRAME48k];
263 : float WB_fenv[SWB_FENV];
264 :
265 285632 : hBWE_FD = st->hBWE_FD;
266 :
267 : /* MDCT of the core synthesis signal */
268 285632 : if ( st->element_mode == IVAS_CPE_DFT && !use_cldfb_for_dft )
269 : {
270 : /* IVAS_fmToDo: wtda() does not support L_FRAME length; thus temporarily resample the signal */
271 : /* IVAS_fmToDo: delay output[] by 1.25ms ? */
272 4730 : lerp( output, ysynth, L_FRAME16k, st->L_frame );
273 :
274 4730 : wtda( ysynth, wtda_synth, hBWE_FD->old_wtda_swb, ALDO_WINDOW, ALDO_WINDOW, /*st->L_frame*/ L_FRAME16k );
275 4730 : direct_transform( wtda_synth, ysynth, 0, /*st->L_frame*/ L_FRAME16k, st->element_mode );
276 : }
277 : else
278 : {
279 280902 : wtda( synth, wtda_synth, hBWE_FD->old_wtda_swb, ALDO_WINDOW, ALDO_WINDOW, output_frame );
280 280902 : direct_transform( wtda_synth, ysynth, 0, output_frame, st->element_mode );
281 : }
282 :
283 285632 : if ( !st->bfi )
284 : {
285 279180 : if ( st->extl_brate > 0 )
286 : {
287 : /* de-quantization */
288 72543 : mode = WB_BWE_gain_deq( st, WB_fenv );
289 72543 : hBWE_FD->last_wb_bwe_ener = 0.5f * ( WB_fenv[0] + WB_fenv[1] );
290 : }
291 : else
292 : {
293 : int32_t tmp_brate;
294 :
295 206637 : tmp_brate = st->last_core_brate;
296 206637 : if ( st->last_total_brate == ACELP_9k60 && st->last_extl == SWB_TBE )
297 : {
298 2 : tmp_brate = ACELP_8k00; /* this is needed in order to stay BE wrt. EVS mono */
299 : }
300 :
301 206637 : if ( st->last_extl != WB_BWE )
302 : {
303 1019 : hBWE_FD->prev_SWB_fenv[0] = 0.0f;
304 : }
305 :
306 206637 : mode = WB_BWE_gain_pred( WB_fenv, ysynth, st->coder_type, st->prev_coder_type, hBWE_FD->prev_SWB_fenv[0], voice_factors, pitch_buf, tmp_brate, hBWE_FD->last_wb_bwe_ener, st->last_extl, st->tilt_wb );
307 : }
308 : }
309 : else
310 : {
311 : /* FEC */
312 6452 : mode = NORMAL;
313 19356 : for ( i = 0; i < 2; i++ )
314 : {
315 12904 : WB_fenv[i] = 0.75f * hBWE_FD->prev_SWB_fenv[i];
316 : }
317 : }
318 :
319 285632 : if ( st->last_extl != WB_BWE || st->bfi )
320 : {
321 13859 : mvr2r( WB_fenv, hBWE_FD->prev_SWB_fenv, 2 );
322 : }
323 :
324 : /* reconstruction of MDCT spectrum of the error signal */
325 285632 : WB_BWE_decoding( ysynth, WB_fenv, yerror, L_FRAME16k, mode, st->last_extl, &hBWE_FD->prev_Energy_wb, hBWE_FD->prev_SWB_fenv, &hBWE_FD->prev_L_swb_norm, st->extl, st->coder_type, st->total_brate, &hBWE_FD->Seed, &hBWE_FD->prev_flag, st->prev_coder_type );
326 :
327 285632 : if ( st->output_Fs == 32000 )
328 : {
329 19515 : set_f( &yerror[L_FRAME16k], 0, L_FRAME16k );
330 : }
331 266117 : else if ( st->output_Fs == 48000 )
332 : {
333 217350 : set_f( &yerror[L_FRAME16k], 0, L_FRAME32k );
334 : }
335 :
336 285632 : inverse_transform( yerror, wtda_synth, 0, output_frame, -1, st->element_mode );
337 :
338 285632 : window_ola( wtda_synth, hb_synth, hBWE_FD->mem_imdct, output_frame, ALDO_WINDOW, ALDO_WINDOW, 0, 0, 0 );
339 :
340 285632 : if ( st->element_mode == IVAS_CPE_DFT && !use_cldfb_for_dft )
341 : {
342 : /* add HB synth from hf_synth() */
343 4730 : v_add( hb_synth, synth, hb_synth, output_frame );
344 : }
345 :
346 285632 : st->hBWE_FD->prev_mode = mode;
347 :
348 285632 : return;
349 : }
350 :
351 : /*-------------------------------------------------------------------*
352 : * swb_bwe_gain_deq()
353 : *
354 : * Decoding of SWB parameters
355 : *-------------------------------------------------------------------*/
356 :
357 : /*! r: BWE class */
358 187485 : int16_t swb_bwe_gain_deq(
359 : Decoder_State *st, /* i/o: decoder state structure */
360 : const int16_t core, /* i : core */
361 : float *SWB_tenv, /* o : time-domain BWE envelope */
362 : float *SWB_fenv, /* o : frequency-domain BWE envelope */
363 : const int16_t hr_flag, /* i : high rate flag */
364 : const int16_t hqswb_clas /* i : HQ BWE class */
365 : )
366 : {
367 : int16_t index, mode, n_band;
368 : int16_t indice[6];
369 : float quant_tmp[SWB_FENV / 2], quant_tmp2[SWB_FENV / 2];
370 : int16_t nb_bits[6];
371 : int16_t nenv;
372 :
373 187485 : if ( hqswb_clas > 0 )
374 : {
375 39084 : mode = get_next_indice( st, 1 );
376 39084 : if ( mode == 0 )
377 : {
378 36339 : mode = get_next_indice( st, 1 );
379 : }
380 : else
381 : {
382 2745 : mode = HQ_GENERIC_SP_EXC;
383 : }
384 : }
385 : else
386 : {
387 148401 : mode = get_next_indice( st, 2 );
388 : }
389 :
390 187485 : if ( mode == 1 && core == ACELP_CORE )
391 : {
392 8080 : for ( n_band = 0; n_band < SWB_TENV; n_band++ )
393 : {
394 6464 : index = get_next_indice( st, 4 );
395 6464 : SWB_tenv[n_band] = (float) ( 1 << index );
396 : }
397 :
398 1616 : indice[0] = get_next_indice( st, 7 );
399 1616 : indice[1] = get_next_indice( st, 6 );
400 :
401 4848 : for ( n_band = 0; n_band < DIM_TR1; n_band++ )
402 : {
403 3232 : quant_tmp[2 * n_band] = Env_TR_Cdbk1[indice[0] * DIM_TR1 + n_band];
404 : }
405 :
406 1616 : quant_tmp[1] = ( quant_tmp[0] + quant_tmp[2] ) * 0.5f + Env_TR_Cdbk2[indice[1] * DIM_TR2];
407 1616 : quant_tmp[3] = quant_tmp[2] + Env_TR_Cdbk2[indice[1] * DIM_TR2 + 1];
408 :
409 8080 : for ( n_band = 0; n_band < SWB_FENV_TRANS; n_band++ )
410 : {
411 6464 : SWB_fenv[n_band] = (float) pow( 10, 0.025f * ( quant_tmp[n_band] + Mean_env_tr[n_band] ) );
412 : }
413 :
414 : /* in case of band-width switching, attenuate frame gain */
415 1616 : if ( st->bws_cnt1 > 0 )
416 : {
417 40 : for ( n_band = 0; n_band < SWB_TENV; n_band++ )
418 : {
419 32 : SWB_tenv[n_band] *= (float) st->bws_cnt1 / (float) N_NS2W_FRAMES;
420 : }
421 :
422 40 : for ( n_band = 0; n_band < SWB_FENV_TRANS; n_band++ )
423 : {
424 32 : SWB_fenv[n_band] *= (float) st->bws_cnt1 / (float) N_NS2W_FRAMES;
425 : }
426 : }
427 : }
428 : else
429 : {
430 185869 : nb_bits[0] = 5;
431 185869 : nb_bits[1] = 7;
432 185869 : nb_bits[2] = 6;
433 185869 : nb_bits[3] = 5;
434 :
435 185869 : if ( hr_flag == 1 )
436 : {
437 869 : nb_bits[4] = 5;
438 869 : nenv = SWB_FENV - 2;
439 : }
440 : else
441 : {
442 185000 : nb_bits[4] = 6;
443 185000 : nenv = SWB_FENV;
444 : }
445 :
446 1115214 : for ( n_band = 0; n_band < 5; n_band++ )
447 : {
448 929345 : indice[n_band] = get_next_indice( st, nb_bits[n_band] );
449 : }
450 :
451 185869 : if ( hqswb_clas == HQ_GEN_FB )
452 : {
453 21095 : indice[n_band] = get_next_indice( st, 5 );
454 : }
455 :
456 185869 : mvr2r( &EnvCdbk11[indice[0] * DIM11], quant_tmp, DIM11 );
457 185869 : mvr2r( &EnvCdbk1st[indice[1] * DIM1ST], quant_tmp2, DIM1ST );
458 185869 : mvr2r( &EnvCdbk2nd[indice[2] * DIM2ND], quant_tmp2 + DIM1ST, DIM2ND );
459 :
460 1301083 : for ( n_band = 0; n_band < DIM11 - 1; n_band++ )
461 : {
462 1115214 : quant_tmp[n_band] += quant_tmp2[n_band];
463 1115214 : SWB_fenv[n_band * 2] = quant_tmp[n_band];
464 : }
465 :
466 185869 : if ( hr_flag == 1 )
467 : {
468 869 : quant_tmp[6] += quant_tmp2[6];
469 869 : SWB_fenv[11] = quant_tmp[6];
470 :
471 :
472 869 : mvr2r( &EnvCdbk3rd[indice[3] * DIM3RD], quant_tmp2, DIM3RD );
473 869 : mvr2r( &EnvCdbk3rd[indice[4] * DIM3RD], quant_tmp2 + DIM3RD, DIM3RD );
474 :
475 5214 : for ( n_band = 0; n_band < 5; n_band++ )
476 : {
477 4345 : SWB_fenv[n_band * 2 + 1] = ( ( quant_tmp[n_band] + quant_tmp[n_band + 1] ) / 2.f ) + quant_tmp2[n_band + 1];
478 : }
479 :
480 869 : SWB_fenv[0] += quant_tmp2[0];
481 : }
482 : else
483 : {
484 185000 : quant_tmp[DIM11 - 1] += quant_tmp2[DIM11 - 1];
485 185000 : SWB_fenv[( DIM11 - 1 ) * 2] = quant_tmp[DIM11 - 1];
486 :
487 185000 : mvr2r( &EnvCdbk3rd[indice[3] * DIM3RD], quant_tmp2, DIM3RD );
488 185000 : mvr2r( &EnvCdbk4th[indice[4] * DIM4TH], quant_tmp2 + DIM3RD, DIM4TH );
489 :
490 1295000 : for ( n_band = 0; n_band < DIM12 - 1; n_band++ )
491 : {
492 1110000 : SWB_fenv[n_band * 2 + 1] = ( ( quant_tmp[n_band] + quant_tmp[n_band + 1] ) / 2.f ) + quant_tmp2[n_band];
493 : }
494 :
495 185000 : SWB_fenv[n_band * 2 + 1] = quant_tmp[n_band] + quant_tmp2[n_band];
496 : }
497 :
498 2786297 : for ( n_band = 0; n_band < nenv; n_band++ )
499 : {
500 : Word16 tmp, frac, exp;
501 : Word32 L_tmp;
502 : #ifdef BASOP_NOGLOB
503 : Flag Overflow;
504 : #endif
505 2600428 : tmp = add( (int16_t) ( SWB_fenv[n_band] * 256 ), (int16_t) ( Mean_env[n_band] * 256 ) ); /*Q8 */
506 :
507 2600428 : L_tmp = L_mult( tmp, 21771 ); /* 0.166096 in Q17 -> Q26 */
508 2600428 : L_tmp = L_shr( L_tmp, 10 ); /* From Q26 to Q16 */
509 2600428 : frac = L_Extract_lc( L_tmp, &exp ); /* Extract exponent of L_tmp */
510 :
511 2600428 : tmp = extract_l( Pow2( 13, frac ) ); /* Put 13 as exponent so that */
512 : /* output of Pow2() will be: */
513 : /* 16384 < Pow2() <= 32767 */
514 2600428 : exp = sub( exp, 13 );
515 : #ifdef BASOP_NOGLOB
516 2600428 : tmp = shl_o( tmp, add( exp, 1 ), &Overflow ); /*Q1 */
517 : #else
518 : tmp = shl( tmp, add( exp, 1 ) ); /*Q1 */
519 : #endif
520 2600428 : SWB_fenv[n_band] = (float) tmp * 0.5f; /*Q1 */
521 : }
522 :
523 185869 : if ( hqswb_clas == HQ_GEN_FB )
524 : {
525 21095 : mvr2r( &EnvCdbkFB[indice[5] * DIM_FB], &SWB_fenv[nenv], DIM_FB );
526 84380 : for ( n_band = 0; n_band < DIM_FB; n_band++ )
527 : {
528 : Word16 tmp, frac, exp;
529 : Word32 L_tmp;
530 :
531 63285 : tmp = add( (int16_t) ( SWB_fenv[n_band + nenv] * 128 ), (int16_t) ( Mean_env_fb[n_band] * 128 ) ); /*Q7 */
532 63285 : L_tmp = L_mult( tmp, 21771 ); /* 0.166096 in Q17 -> Q25 */
533 63285 : L_tmp = L_shr( L_tmp, 9 ); /* From Q25 to Q16 */
534 63285 : frac = L_Extract_lc( L_tmp, &exp ); /* Extract exponent of L_tmp */
535 :
536 63285 : tmp = extract_l( Pow2( 13, frac ) ); /* Put 13 as exponent so that */
537 : /* output of Pow2() will be: */
538 : /* 16384 < Pow2() <= 32767 */
539 63285 : exp = sub( exp, 13 );
540 63285 : tmp = shl( tmp, add( exp, 1 ) );
541 63285 : move16();
542 63285 : SWB_fenv[add( n_band, nenv )] = (float) tmp * 0.5f;
543 : }
544 : }
545 : }
546 :
547 187485 : return mode;
548 : }
549 :
550 : /*-------------------------------------------------------------------*
551 : * swb_bwe_dec()
552 : *
553 : * SWB BWE decoder
554 : *-------------------------------------------------------------------*/
555 :
556 235512 : void swb_bwe_dec(
557 : Decoder_State *st, /* i/o: decoder state structure */
558 : const float output[], /* i : synthesis @internal Fs */
559 : const float *synth, /* i : ACELP core synthesis/final synthesis */
560 : float *hb_synth, /* o : SHB synthesis/final synthesis */
561 : const int16_t use_cldfb_for_dft, /* i : flag to use of CLDFB for DFT Stereo */
562 : const int16_t output_frame /* i : frame length */
563 : )
564 : {
565 : int16_t i, l_subfr;
566 : FD_BWE_DEC_HANDLE hBWE_FD;
567 : float ysynth[L_FRAME48k];
568 : float yerror[L_FRAME48k];
569 : float wtda_synth[2 * L_FRAME48k];
570 : float SWB_tenv[SWB_TENV];
571 : float SWB_fenv[SWB_FENV];
572 : int16_t L;
573 : int16_t mode;
574 235512 : int16_t frica_flag = 0;
575 235512 : float fb_ener_adjust = 0.0f;
576 235512 : int16_t j = 0;
577 : float ener_adjust_quan;
578 : int16_t idxGain;
579 : int16_t fb_band_begin;
580 :
581 235512 : hBWE_FD = st->hBWE_FD;
582 :
583 : /*---------------------------------------------------------------------*
584 : * SWB BWE decoding
585 : *---------------------------------------------------------------------*/
586 :
587 235512 : if ( st->element_mode == IVAS_CPE_DFT && !use_cldfb_for_dft )
588 : {
589 : /* todo - wtda() does not support L_FRAME length; thus temporarily resample the signal */
590 : /* todo - delay output[] by 1.25ms ? */
591 23590 : lerp( output, ysynth, L_FRAME16k, st->L_frame );
592 :
593 : /* windowing of the ACELP core synthesis */
594 23590 : wtda( ysynth, wtda_synth, hBWE_FD->old_wtda_swb, ALDO_WINDOW, ALDO_WINDOW, /*st->L_frame*/ L_FRAME16k );
595 :
596 : /* DCT of the ACELP core synthesis */
597 23590 : direct_transform( wtda_synth, ysynth, 0, /*st->L_frame*/ L_FRAME16k, st->element_mode );
598 : }
599 : else
600 : {
601 : /* windowing of the ACELP core synthesis */
602 211922 : wtda( synth, wtda_synth, hBWE_FD->old_wtda_swb, ALDO_WINDOW, ALDO_WINDOW, output_frame );
603 :
604 : /* DCT of the ACELP core synthesis */
605 211922 : direct_transform( wtda_synth, ysynth, 0, output_frame, st->element_mode );
606 : }
607 :
608 235512 : if ( !st->bfi )
609 : {
610 230623 : if ( st->bws_cnt > 0 )
611 : {
612 : /* estimate parameters */
613 82222 : mode = para_pred_bws( st, ysynth, SWB_fenv );
614 : }
615 : else
616 : {
617 : /* de-quantization */
618 148401 : mode = swb_bwe_gain_deq( st, ACELP_CORE, SWB_tenv, SWB_fenv, 0, -1 );
619 : }
620 :
621 230623 : L = mode == TRANSIENT ? SWB_FENV_TRANS : SWB_FENV;
622 230623 : st->prev_ener_shb = 0.0f;
623 3443185 : for ( i = 0; i < L; i++ )
624 : {
625 3212562 : st->prev_ener_shb += SWB_fenv[i];
626 : }
627 230623 : st->prev_ener_shb /= L;
628 : }
629 : else
630 : {
631 : /* SHB FEC */
632 4889 : if ( hBWE_FD->prev_mode != TRANSIENT )
633 : {
634 4874 : mode = hBWE_FD->prev_mode;
635 : }
636 : else
637 : {
638 15 : mode = NORMAL;
639 : }
640 :
641 4889 : mvr2r( hBWE_FD->prev_SWB_fenv, SWB_fenv, SWB_FENV );
642 : }
643 :
644 : /* reconstruction of MDCT spectrum of the error signal */
645 235512 : set_f( yerror, 0, output_frame );
646 :
647 235512 : if ( st->L_frame == L_FRAME16k )
648 : {
649 36642 : SWB_BWE_decoding( ysynth, SWB_fenv, yerror, L_FRAME32k - 80, mode, &frica_flag, &hBWE_FD->prev_Energy, hBWE_FD->prev_SWB_fenv, &hBWE_FD->prev_L_swb_norm, st->tilt_wb, &hBWE_FD->Seed, 80, &hBWE_FD->prev_weight, st->extl, st->last_extl );
650 : }
651 : else
652 : {
653 198870 : SWB_BWE_decoding( ysynth, SWB_fenv, yerror, L_FRAME32k - 80, mode, &frica_flag, &hBWE_FD->prev_Energy, hBWE_FD->prev_SWB_fenv, &hBWE_FD->prev_L_swb_norm, st->tilt_wb, &hBWE_FD->Seed, 6, &hBWE_FD->prev_weight, st->extl, st->last_extl );
654 : }
655 :
656 235512 : if ( hBWE_FD->prev_frica_flag == 1 && frica_flag == 0 )
657 : {
658 38090 : for ( i = 0; i < L_SUBFR; i++ )
659 : {
660 37504 : hBWE_FD->mem_imdct[i] *= 1.0f - i * 0.015625f;
661 : }
662 :
663 456842 : for ( ; i < output_frame; i++ )
664 : {
665 456256 : hBWE_FD->mem_imdct[i] = 0.0f;
666 : }
667 : }
668 :
669 : /* decode information */
670 235512 : if ( st->extl == FB_BWE )
671 : {
672 28622 : if ( !st->bfi )
673 : {
674 28427 : idxGain = get_next_indice( st, NUM_BITS_FB_FRAMEGAIN );
675 28427 : fb_ener_adjust = usdequant( idxGain, FB_GAIN_QLOW, FB_GAIN_QDELTA );
676 : }
677 195 : else if ( st->bfi )
678 : {
679 195 : fb_ener_adjust = hBWE_FD->prev_fb_ener_adjust;
680 : }
681 :
682 28622 : hBWE_FD->prev_fb_ener_adjust = fb_ener_adjust;
683 28622 : if ( mode == TRANSIENT )
684 : {
685 110 : ener_adjust_quan = fb_ener_adjust;
686 : }
687 : else
688 : {
689 28512 : if ( SWB_fenv[7] < 0.01f )
690 : {
691 6145 : ener_adjust_quan = 0.0f;
692 : }
693 : else
694 : {
695 22367 : ener_adjust_quan = min( SWB_fenv[13] / SWB_fenv[7], 4.0f );
696 : }
697 : }
698 :
699 28622 : fb_band_begin = FB_BAND_BEGIN;
700 28622 : if ( st->L_frame == L_FRAME )
701 : {
702 3098 : fb_band_begin = FB_BAND_BEGIN_12k8;
703 : }
704 :
705 944526 : for ( i = fb_band_begin; i < fb_band_begin + DE_OFFSET1; i++ )
706 : {
707 915904 : yerror[i] = yerror[i - FB_BAND_WIDTH] * ( ( 1.0f - j * FB_GAIN_QDELTA ) * ener_adjust_quan + j * FB_GAIN_QDELTA * fb_ener_adjust );
708 915904 : j++;
709 : }
710 4450558 : for ( ; i < FB_BAND_END; i++ )
711 : {
712 4421936 : yerror[i] = yerror[i - FB_BAND_WIDTH] * fb_ener_adjust;
713 : }
714 : }
715 :
716 : /* iDCT of the error signal */
717 235512 : inverse_transform( yerror, wtda_synth, 0, output_frame, -1, st->element_mode );
718 :
719 : /* inverse windowing of the error signal */
720 235512 : window_ola( wtda_synth, hb_synth, hBWE_FD->mem_imdct, output_frame, ALDO_WINDOW, ALDO_WINDOW, 0, 0, 0 );
721 235512 : l_subfr = output_frame / 4;
722 :
723 235512 : if ( mode == TRANSIENT )
724 : {
725 8080 : for ( i = 0; i < SWB_TENV; i++ )
726 : {
727 6464 : SWB_tenv[i] *= 0.8f;
728 : }
729 :
730 : /* time envelope shaping when the current frame is TRANSIENT frame */
731 1616 : time_envelop_shaping( hb_synth, SWB_tenv, output_frame );
732 :
733 1616 : hBWE_FD->prev_td_energy = SWB_tenv[3];
734 : }
735 233896 : else if ( frica_flag == 1 && hBWE_FD->prev_frica_flag == 0 )
736 : {
737 : /* IVAS_fmToDo: synth[] is @internal_Fs!!! */
738 1591 : time_reduce_pre_echo( synth, hb_synth, hBWE_FD->prev_td_energy, l_subfr );
739 : }
740 : else
741 : {
742 232305 : hBWE_FD->prev_td_energy = 0.0f;
743 51628145 : for ( i = 0; i < l_subfr; i++ )
744 : {
745 51395840 : hBWE_FD->prev_td_energy += hb_synth[3 * l_subfr + i] * hb_synth[3 * l_subfr + i];
746 : }
747 232305 : hBWE_FD->prev_td_energy = (float) sqrt( hBWE_FD->prev_td_energy / l_subfr );
748 : }
749 :
750 235512 : if ( st->element_mode == IVAS_CPE_DFT && !use_cldfb_for_dft )
751 : {
752 : /* add HB synth from hf_synth() */
753 23590 : v_add( hb_synth, synth, hb_synth, output_frame );
754 : }
755 :
756 235512 : hBWE_FD->prev_frica_flag = frica_flag;
757 235512 : hBWE_FD->prev_mode = mode;
758 :
759 235512 : return;
760 : }
761 :
762 : /*-------------------------------------------------------------------*
763 : * fd_bwe_dec_init()
764 : *
765 : * Initialize FD BWE state structure at the decoder
766 : *-------------------------------------------------------------------*/
767 :
768 157457 : void fd_bwe_dec_init(
769 : FD_BWE_DEC_HANDLE hBWE_FD /* i/o: FD BWE data handle */
770 : )
771 : {
772 157457 : set_f( hBWE_FD->old_wtda_swb, 0, L_FRAME48k );
773 157457 : set_f( hBWE_FD->old_syn_12k8_16k, 0, NS2SA( 16000, DELAY_FD_BWE_ENC_NS ) );
774 157457 : hBWE_FD->prev_mode = NORMAL;
775 157457 : set_f( hBWE_FD->prev_SWB_fenv, 0, SWB_FENV );
776 157457 : hBWE_FD->prev_Energy = 0.0f;
777 157457 : hBWE_FD->prev_L_swb_norm = 8;
778 157457 : hBWE_FD->Seed = 21211;
779 157457 : hBWE_FD->prev_frica_flag = 0;
780 157457 : set_f( hBWE_FD->mem_imdct, 0, L_FRAME48k );
781 157457 : hBWE_FD->prev_td_energy = 0.0f;
782 157457 : hBWE_FD->prev_weight = 0.2f;
783 157457 : hBWE_FD->prev_flag = 0;
784 157457 : hBWE_FD->last_wb_bwe_ener = 0.0f;
785 157457 : hBWE_FD->prev_Energy_wb = 0.0f;
786 :
787 157457 : hBWE_FD->mem_deemph_old_syn = 0.0f;
788 :
789 157457 : hBWE_FD->prev_fb_ener_adjust = 0.0f;
790 :
791 157457 : return;
792 : }
|