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 230414 : 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 230414 : hBWE_FD = st->hBWE_FD;
71 :
72 230414 : mode = NORMAL;
73 :
74 230414 : k = 0;
75 230414 : input_hi = &signal_wb[SHARP_WIDTH];
76 1843312 : for ( i = 0; i < 7; i++ )
77 : {
78 1612898 : peak = 0.0f;
79 1612898 : mean[i] = 0;
80 53225634 : for ( j = 0; j < SHARP_WIDTH; j++ )
81 : {
82 51612736 : mag = (float) fabs( *input_hi );
83 51612736 : if ( mag > peak )
84 : {
85 9760394 : peak = mag;
86 : }
87 51612736 : mean[i] += mag;
88 51612736 : input_hi++;
89 : }
90 :
91 1612898 : if ( peak * ( SHARP_WIDTH + 3.5f ) > 4.5f * mean[i] && peak > 8.0f )
92 : {
93 3961 : k += 1;
94 : }
95 : }
96 :
97 230414 : avrg1 = 0.0f;
98 230414 : avrg2 = 0.0f;
99 921656 : for ( i = 1; i < 4; i++ )
100 : {
101 691242 : avrg1 += mean[i];
102 691242 : avrg2 += mean[i + 3];
103 : }
104 230414 : avrg1 /= 3;
105 230414 : avrg2 /= 3;
106 :
107 230414 : min_val = FLT_MAX;
108 230414 : peak = 0.0f;
109 921656 : for ( i = 4; i < 7; i++ )
110 : {
111 691242 : if ( mean[i] > 2.0f * avrg2 )
112 : {
113 169162 : mean[i] *= 2 * avrg2 / mean[i];
114 : }
115 691242 : if ( mean[i] < min_val )
116 : {
117 272142 : min_val = mean[i];
118 : }
119 691242 : if ( mean[i] > peak )
120 : {
121 639729 : peak = mean[i];
122 : }
123 : }
124 :
125 230414 : if ( st->tilt_wb > 8 )
126 : {
127 2119 : min_val = min( st->tilt_wb / 15.0f, 1.0f ) * peak;
128 : }
129 :
130 230414 : if ( peak == 0 || min_val == 0 )
131 : {
132 306 : set_f( SWB_fenv, 0, SWB_FENV );
133 : }
134 : else
135 : {
136 3451620 : for ( i = 0; i < SWB_FENV; i++ )
137 : {
138 3221512 : SWB_fenv[i] = min_val * mean[i / 5 + 4] / ( 64 * peak );
139 : }
140 : }
141 :
142 1843312 : for ( j = 0, i = SWB_FENV / 2; i < SWB_FENV; i++ )
143 : {
144 1612898 : SWB_fenv[i] *= ( 1.0f - (float) j++ / SWB_FENV );
145 : }
146 :
147 230414 : if ( avrg1 > 8.0f * avrg2 )
148 : {
149 3435 : for ( i = 0; i < SWB_FENV; i++ )
150 : {
151 3206 : SWB_fenv[i] *= 0.5f;
152 : }
153 : }
154 230414 : if ( st->last_core != HQ_CORE && st->last_codec_mode == MODE1 &&
155 230411 : ( 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 230392 : if ( st->core_brate != st->last_core_brate || ( st->enerLH > 0.5f * st->prev_enerLH && st->enerLH < 2.0f * st->prev_enerLH ) ||
178 201531 : ( st->enerLL > 0.5f * st->prev_enerLL && st->enerLL < 2.0f * st->prev_enerLL ) )
179 : {
180 433065 : for ( i = 0; i < SWB_FENV; i++ )
181 : {
182 404194 : if ( SWB_fenv[i] > 2.0f * hBWE_FD->prev_SWB_fenv[i] )
183 : {
184 154576 : SWB_fenv[i] = hBWE_FD->prev_SWB_fenv[i];
185 : }
186 : }
187 : }
188 :
189 3455880 : for ( i = 0; i < SWB_FENV; i++ )
190 : {
191 3225488 : SWB_fenv[i] = 0.9f * SWB_fenv[i] + 0.1f * hBWE_FD->prev_SWB_fenv[i];
192 : }
193 :
194 230392 : st->attenu1 = 0.1f;
195 : }
196 :
197 230414 : if ( k > 3 )
198 : {
199 7 : mode = HARMONIC;
200 : }
201 :
202 230414 : att = ( (float) N_WS2N_FRAMES - (float) st->bws_cnt ) / (float) N_WS2N_FRAMES;
203 230414 : if ( st->L_frame == L_FRAME16k )
204 : {
205 485 : for ( i = 0; i < 4; i++ )
206 : {
207 388 : SWB_fenv[i] *= att;
208 : }
209 : }
210 :
211 2534554 : for ( i = 4; i < SWB_FENV; i++ )
212 : {
213 2304140 : SWB_fenv[i] *= att;
214 : }
215 :
216 230414 : return mode;
217 : }
218 :
219 : /*-------------------------------------------------------------------*
220 : * WB_BWE_gain_deq()
221 : *
222 : * Decoding of WB parameters
223 : *-------------------------------------------------------------------*/
224 :
225 119675 : 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 119675 : index = get_next_indice( st, 5 );
233 119675 : mode = get_next_indice( st, 1 ) + 2;
234 :
235 119675 : WB_fenv[0] = (float) pow( 2, 0.5f * F_2_5[2 * index] );
236 119675 : WB_fenv[1] = (float) pow( 2, 0.5f * F_2_5[2 * index + 1] );
237 :
238 119675 : return ( mode );
239 : }
240 :
241 : /*-------------------------------------------------------------------*
242 : * wb_bwe_dec()
243 : *
244 : * WB BWE decoder (only for 16kHz signals)
245 : *-------------------------------------------------------------------*/
246 :
247 709787 : 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 709787 : hBWE_FD = st->hBWE_FD;
266 :
267 : /* MDCT of the core synthesis signal */
268 709787 : 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 12612 : lerp( output, ysynth, L_FRAME16k, st->L_frame );
273 :
274 12612 : wtda( ysynth, wtda_synth, hBWE_FD->old_wtda_swb, ALDO_WINDOW, ALDO_WINDOW, /*st->L_frame*/ L_FRAME16k );
275 12612 : direct_transform( wtda_synth, ysynth, 0, /*st->L_frame*/ L_FRAME16k, st->element_mode );
276 : }
277 : else
278 : {
279 697175 : wtda( synth, wtda_synth, hBWE_FD->old_wtda_swb, ALDO_WINDOW, ALDO_WINDOW, output_frame );
280 697175 : direct_transform( wtda_synth, ysynth, 0, output_frame, st->element_mode );
281 : }
282 :
283 709787 : if ( !st->bfi )
284 : {
285 699207 : if ( st->extl_brate > 0 )
286 : {
287 : /* de-quantization */
288 119675 : mode = WB_BWE_gain_deq( st, WB_fenv );
289 119675 : 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 579532 : tmp_brate = st->last_core_brate;
296 579532 : if ( st->last_total_brate == ACELP_9k60 && st->last_extl == SWB_TBE )
297 : {
298 6 : tmp_brate = ACELP_8k00; /* this is needed in order to stay BE wrt. EVS mono */
299 : }
300 :
301 579532 : if ( st->last_extl != WB_BWE )
302 : {
303 2218 : hBWE_FD->prev_SWB_fenv[0] = 0.0f;
304 : }
305 :
306 579532 : 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 10580 : mode = NORMAL;
313 31740 : for ( i = 0; i < 2; i++ )
314 : {
315 21160 : WB_fenv[i] = 0.75f * hBWE_FD->prev_SWB_fenv[i];
316 : }
317 : }
318 :
319 709787 : if ( st->last_extl != WB_BWE || st->bfi )
320 : {
321 21350 : mvr2r( WB_fenv, hBWE_FD->prev_SWB_fenv, 2 );
322 : }
323 :
324 : /* reconstruction of MDCT spectrum of the error signal */
325 709787 : 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 709787 : if ( st->output_Fs == 32000 )
328 : {
329 39901 : set_f( &yerror[L_FRAME16k], 0, L_FRAME16k );
330 : }
331 669886 : else if ( st->output_Fs == 48000 )
332 : {
333 609476 : set_f( &yerror[L_FRAME16k], 0, L_FRAME32k );
334 : }
335 :
336 709787 : inverse_transform( yerror, wtda_synth, 0, output_frame, -1, st->element_mode );
337 :
338 709787 : window_ola( wtda_synth, hb_synth, hBWE_FD->mem_imdct, output_frame, ALDO_WINDOW, ALDO_WINDOW, 0, 0, 0 );
339 :
340 709787 : if ( st->element_mode == IVAS_CPE_DFT && !use_cldfb_for_dft )
341 : {
342 : /* add HB synth from hf_synth() */
343 12612 : v_add( hb_synth, synth, hb_synth, output_frame );
344 : }
345 :
346 709787 : st->hBWE_FD->prev_mode = mode;
347 :
348 709787 : return;
349 : }
350 :
351 : /*-------------------------------------------------------------------*
352 : * swb_bwe_gain_deq()
353 : *
354 : * Decoding of SWB parameters
355 : *-------------------------------------------------------------------*/
356 :
357 : /*! r: BWE class */
358 415158 : 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 415158 : if ( hqswb_clas > 0 )
374 : {
375 78719 : mode = get_next_indice( st, 1 );
376 78719 : if ( mode == 0 )
377 : {
378 73158 : mode = get_next_indice( st, 1 );
379 : }
380 : else
381 : {
382 5561 : mode = HQ_GENERIC_SP_EXC;
383 : }
384 : }
385 : else
386 : {
387 336439 : mode = get_next_indice( st, 2 );
388 : }
389 :
390 415158 : if ( mode == 1 && core == ACELP_CORE )
391 : {
392 17750 : for ( n_band = 0; n_band < SWB_TENV; n_band++ )
393 : {
394 14200 : index = get_next_indice( st, 4 );
395 14200 : SWB_tenv[n_band] = (float) ( 1 << index );
396 : }
397 :
398 3550 : indice[0] = get_next_indice( st, 7 );
399 3550 : indice[1] = get_next_indice( st, 6 );
400 :
401 10650 : for ( n_band = 0; n_band < DIM_TR1; n_band++ )
402 : {
403 7100 : quant_tmp[2 * n_band] = Env_TR_Cdbk1[indice[0] * DIM_TR1 + n_band];
404 : }
405 :
406 3550 : quant_tmp[1] = ( quant_tmp[0] + quant_tmp[2] ) * 0.5f + Env_TR_Cdbk2[indice[1] * DIM_TR2];
407 3550 : quant_tmp[3] = quant_tmp[2] + Env_TR_Cdbk2[indice[1] * DIM_TR2 + 1];
408 :
409 17750 : for ( n_band = 0; n_band < SWB_FENV_TRANS; n_band++ )
410 : {
411 14200 : 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 3550 : if ( st->bws_cnt1 > 0 )
416 : {
417 70 : for ( n_band = 0; n_band < SWB_TENV; n_band++ )
418 : {
419 56 : SWB_tenv[n_band] *= (float) st->bws_cnt1 / (float) N_NS2W_FRAMES;
420 : }
421 :
422 70 : for ( n_band = 0; n_band < SWB_FENV_TRANS; n_band++ )
423 : {
424 56 : SWB_fenv[n_band] *= (float) st->bws_cnt1 / (float) N_NS2W_FRAMES;
425 : }
426 : }
427 : }
428 : else
429 : {
430 411608 : nb_bits[0] = 5;
431 411608 : nb_bits[1] = 7;
432 411608 : nb_bits[2] = 6;
433 411608 : nb_bits[3] = 5;
434 :
435 411608 : if ( hr_flag == 1 )
436 : {
437 881 : nb_bits[4] = 5;
438 881 : nenv = SWB_FENV - 2;
439 : }
440 : else
441 : {
442 410727 : nb_bits[4] = 6;
443 410727 : nenv = SWB_FENV;
444 : }
445 :
446 2469648 : for ( n_band = 0; n_band < 5; n_band++ )
447 : {
448 2058040 : indice[n_band] = get_next_indice( st, nb_bits[n_band] );
449 : }
450 :
451 411608 : if ( hqswb_clas == HQ_GEN_FB )
452 : {
453 41916 : indice[n_band] = get_next_indice( st, 5 );
454 : }
455 :
456 411608 : mvr2r( &EnvCdbk11[indice[0] * DIM11], quant_tmp, DIM11 );
457 411608 : mvr2r( &EnvCdbk1st[indice[1] * DIM1ST], quant_tmp2, DIM1ST );
458 411608 : mvr2r( &EnvCdbk2nd[indice[2] * DIM2ND], quant_tmp2 + DIM1ST, DIM2ND );
459 :
460 2881256 : for ( n_band = 0; n_band < DIM11 - 1; n_band++ )
461 : {
462 2469648 : quant_tmp[n_band] += quant_tmp2[n_band];
463 2469648 : SWB_fenv[n_band * 2] = quant_tmp[n_band];
464 : }
465 :
466 411608 : if ( hr_flag == 1 )
467 : {
468 881 : quant_tmp[6] += quant_tmp2[6];
469 881 : SWB_fenv[11] = quant_tmp[6];
470 :
471 :
472 881 : mvr2r( &EnvCdbk3rd[indice[3] * DIM3RD], quant_tmp2, DIM3RD );
473 881 : mvr2r( &EnvCdbk3rd[indice[4] * DIM3RD], quant_tmp2 + DIM3RD, DIM3RD );
474 :
475 5286 : for ( n_band = 0; n_band < 5; n_band++ )
476 : {
477 4405 : 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 881 : SWB_fenv[0] += quant_tmp2[0];
481 : }
482 : else
483 : {
484 410727 : quant_tmp[DIM11 - 1] += quant_tmp2[DIM11 - 1];
485 410727 : SWB_fenv[( DIM11 - 1 ) * 2] = quant_tmp[DIM11 - 1];
486 :
487 410727 : mvr2r( &EnvCdbk3rd[indice[3] * DIM3RD], quant_tmp2, DIM3RD );
488 410727 : mvr2r( &EnvCdbk4th[indice[4] * DIM4TH], quant_tmp2 + DIM3RD, DIM4TH );
489 :
490 2875089 : for ( n_band = 0; n_band < DIM12 - 1; n_band++ )
491 : {
492 2464362 : SWB_fenv[n_band * 2 + 1] = ( ( quant_tmp[n_band] + quant_tmp[n_band + 1] ) / 2.f ) + quant_tmp2[n_band];
493 : }
494 :
495 410727 : SWB_fenv[n_band * 2 + 1] = quant_tmp[n_band] + quant_tmp2[n_band];
496 : }
497 :
498 6172358 : 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 5760750 : tmp = add( (int16_t) ( SWB_fenv[n_band] * 256 ), (int16_t) ( Mean_env[n_band] * 256 ) ); /*Q8 */
506 :
507 5760750 : L_tmp = L_mult( tmp, 21771 ); /* 0.166096 in Q17 -> Q26 */
508 5760750 : L_tmp = L_shr( L_tmp, 10 ); /* From Q26 to Q16 */
509 5760750 : frac = L_Extract_lc( L_tmp, &exp ); /* Extract exponent of L_tmp */
510 :
511 5760750 : tmp = extract_l( Pow2( 13, frac ) ); /* Put 13 as exponent so that */
512 : /* output of Pow2() will be: */
513 : /* 16384 < Pow2() <= 32767 */
514 5760750 : exp = sub( exp, 13 );
515 : #ifdef BASOP_NOGLOB
516 5760750 : tmp = shl_o( tmp, add( exp, 1 ), &Overflow ); /*Q1 */
517 : #else
518 : tmp = shl( tmp, add( exp, 1 ) ); /*Q1 */
519 : #endif
520 5760750 : SWB_fenv[n_band] = (float) tmp * 0.5f; /*Q1 */
521 : }
522 :
523 411608 : if ( hqswb_clas == HQ_GEN_FB )
524 : {
525 41916 : mvr2r( &EnvCdbkFB[indice[5] * DIM_FB], &SWB_fenv[nenv], DIM_FB );
526 167664 : for ( n_band = 0; n_band < DIM_FB; n_band++ )
527 : {
528 : Word16 tmp, frac, exp;
529 : Word32 L_tmp;
530 :
531 125748 : tmp = add( (int16_t) ( SWB_fenv[n_band + nenv] * 128 ), (int16_t) ( Mean_env_fb[n_band] * 128 ) ); /*Q7 */
532 125748 : L_tmp = L_mult( tmp, 21771 ); /* 0.166096 in Q17 -> Q25 */
533 125748 : L_tmp = L_shr( L_tmp, 9 ); /* From Q25 to Q16 */
534 125748 : frac = L_Extract_lc( L_tmp, &exp ); /* Extract exponent of L_tmp */
535 :
536 125748 : tmp = extract_l( Pow2( 13, frac ) ); /* Put 13 as exponent so that */
537 : /* output of Pow2() will be: */
538 : /* 16384 < Pow2() <= 32767 */
539 125748 : exp = sub( exp, 13 );
540 125748 : tmp = shl( tmp, add( exp, 1 ) );
541 125748 : move16();
542 125748 : SWB_fenv[add( n_band, nenv )] = (float) tmp * 0.5f;
543 : }
544 : }
545 : }
546 :
547 415158 : return mode;
548 : }
549 :
550 : /*-------------------------------------------------------------------*
551 : * swb_bwe_dec()
552 : *
553 : * SWB BWE decoder
554 : *-------------------------------------------------------------------*/
555 :
556 575761 : 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 575761 : int16_t frica_flag = 0;
575 575761 : float fb_ener_adjust = 0.0f;
576 575761 : int16_t j = 0;
577 : float ener_adjust_quan;
578 : int16_t idxGain;
579 : int16_t fb_band_begin;
580 :
581 575761 : hBWE_FD = st->hBWE_FD;
582 :
583 : /*---------------------------------------------------------------------*
584 : * SWB BWE decoding
585 : *---------------------------------------------------------------------*/
586 :
587 575761 : 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 64035 : lerp( output, ysynth, L_FRAME16k, st->L_frame );
592 :
593 : /* windowing of the ACELP core synthesis */
594 64035 : 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 64035 : 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 511726 : wtda( synth, wtda_synth, hBWE_FD->old_wtda_swb, ALDO_WINDOW, ALDO_WINDOW, output_frame );
603 :
604 : /* DCT of the ACELP core synthesis */
605 511726 : direct_transform( wtda_synth, ysynth, 0, output_frame, st->element_mode );
606 : }
607 :
608 575761 : if ( !st->bfi )
609 : {
610 566853 : if ( st->bws_cnt > 0 )
611 : {
612 : /* estimate parameters */
613 230414 : mode = para_pred_bws( st, ysynth, SWB_fenv );
614 : }
615 : else
616 : {
617 : /* de-quantization */
618 336439 : mode = swb_bwe_gain_deq( st, ACELP_CORE, SWB_tenv, SWB_fenv, 0, -1 );
619 : }
620 :
621 566853 : L = mode == TRANSIENT ? SWB_FENV_TRANS : SWB_FENV;
622 566853 : st->prev_ener_shb = 0.0f;
623 8467295 : for ( i = 0; i < L; i++ )
624 : {
625 7900442 : st->prev_ener_shb += SWB_fenv[i];
626 : }
627 566853 : st->prev_ener_shb /= L;
628 : }
629 : else
630 : {
631 : /* SHB FEC */
632 8908 : if ( hBWE_FD->prev_mode != TRANSIENT )
633 : {
634 8863 : mode = hBWE_FD->prev_mode;
635 : }
636 : else
637 : {
638 45 : mode = NORMAL;
639 : }
640 :
641 8908 : mvr2r( hBWE_FD->prev_SWB_fenv, SWB_fenv, SWB_FENV );
642 : }
643 :
644 : /* reconstruction of MDCT spectrum of the error signal */
645 575761 : set_f( yerror, 0, output_frame );
646 :
647 575761 : if ( st->L_frame == L_FRAME16k )
648 : {
649 94926 : 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 480835 : 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 575761 : if ( hBWE_FD->prev_frica_flag == 1 && frica_flag == 0 )
657 : {
658 80470 : for ( i = 0; i < L_SUBFR; i++ )
659 : {
660 79232 : hBWE_FD->mem_imdct[i] *= 1.0f - i * 0.015625f;
661 : }
662 :
663 979286 : for ( ; i < output_frame; i++ )
664 : {
665 978048 : hBWE_FD->mem_imdct[i] = 0.0f;
666 : }
667 : }
668 :
669 : /* decode information */
670 575761 : if ( st->extl == FB_BWE )
671 : {
672 70585 : if ( !st->bfi )
673 : {
674 69838 : idxGain = get_next_indice( st, NUM_BITS_FB_FRAMEGAIN );
675 69838 : fb_ener_adjust = usdequant( idxGain, FB_GAIN_QLOW, FB_GAIN_QDELTA );
676 : }
677 747 : else if ( st->bfi )
678 : {
679 747 : fb_ener_adjust = hBWE_FD->prev_fb_ener_adjust;
680 : }
681 :
682 70585 : hBWE_FD->prev_fb_ener_adjust = fb_ener_adjust;
683 70585 : if ( mode == TRANSIENT )
684 : {
685 325 : ener_adjust_quan = fb_ener_adjust;
686 : }
687 : else
688 : {
689 70260 : if ( SWB_fenv[7] < 0.01f )
690 : {
691 18205 : ener_adjust_quan = 0.0f;
692 : }
693 : else
694 : {
695 52055 : ener_adjust_quan = min( SWB_fenv[13] / SWB_fenv[7], 4.0f );
696 : }
697 : }
698 :
699 70585 : fb_band_begin = FB_BAND_BEGIN;
700 70585 : if ( st->L_frame == L_FRAME )
701 : {
702 5439 : fb_band_begin = FB_BAND_BEGIN_12k8;
703 : }
704 :
705 2329305 : for ( i = fb_band_begin; i < fb_band_begin + DE_OFFSET1; i++ )
706 : {
707 2258720 : yerror[i] = yerror[i - FB_BAND_WIDTH] * ( ( 1.0f - j * FB_GAIN_QDELTA ) * ener_adjust_quan + j * FB_GAIN_QDELTA * fb_ener_adjust );
708 2258720 : j++;
709 : }
710 10843505 : for ( ; i < FB_BAND_END; i++ )
711 : {
712 10772920 : yerror[i] = yerror[i - FB_BAND_WIDTH] * fb_ener_adjust;
713 : }
714 : }
715 :
716 : /* iDCT of the error signal */
717 575761 : inverse_transform( yerror, wtda_synth, 0, output_frame, -1, st->element_mode );
718 :
719 : /* inverse windowing of the error signal */
720 575761 : window_ola( wtda_synth, hb_synth, hBWE_FD->mem_imdct, output_frame, ALDO_WINDOW, ALDO_WINDOW, 0, 0, 0 );
721 575761 : l_subfr = output_frame / 4;
722 :
723 575761 : if ( mode == TRANSIENT )
724 : {
725 17750 : for ( i = 0; i < SWB_TENV; i++ )
726 : {
727 14200 : SWB_tenv[i] *= 0.8f;
728 : }
729 :
730 : /* time envelope shaping when the current frame is TRANSIENT frame */
731 3550 : time_envelop_shaping( hb_synth, SWB_tenv, output_frame );
732 :
733 3550 : hBWE_FD->prev_td_energy = SWB_tenv[3];
734 : }
735 572211 : else if ( frica_flag == 1 && hBWE_FD->prev_frica_flag == 0 )
736 : {
737 : /* IVAS_fmToDo: synth[] is @internal_Fs!!! */
738 3708 : time_reduce_pre_echo( synth, hb_synth, hBWE_FD->prev_td_energy, l_subfr );
739 : }
740 : else
741 : {
742 568503 : hBWE_FD->prev_td_energy = 0.0f;
743 128189703 : for ( i = 0; i < l_subfr; i++ )
744 : {
745 127621200 : hBWE_FD->prev_td_energy += hb_synth[3 * l_subfr + i] * hb_synth[3 * l_subfr + i];
746 : }
747 568503 : hBWE_FD->prev_td_energy = (float) sqrt( hBWE_FD->prev_td_energy / l_subfr );
748 : }
749 :
750 575761 : if ( st->element_mode == IVAS_CPE_DFT && !use_cldfb_for_dft )
751 : {
752 : /* add HB synth from hf_synth() */
753 64035 : v_add( hb_synth, synth, hb_synth, output_frame );
754 : }
755 :
756 575761 : hBWE_FD->prev_frica_flag = frica_flag;
757 575761 : hBWE_FD->prev_mode = mode;
758 :
759 575761 : return;
760 : }
761 :
762 : /*-------------------------------------------------------------------*
763 : * fd_bwe_dec_init()
764 : *
765 : * Initialize FD BWE state structure at the decoder
766 : *-------------------------------------------------------------------*/
767 :
768 282695 : void fd_bwe_dec_init(
769 : FD_BWE_DEC_HANDLE hBWE_FD /* i/o: FD BWE data handle */
770 : )
771 : {
772 282695 : set_f( hBWE_FD->old_wtda_swb, 0, L_FRAME48k );
773 282695 : set_f( hBWE_FD->old_syn_12k8_16k, 0, NS2SA( 16000, DELAY_FD_BWE_ENC_NS ) );
774 282695 : hBWE_FD->prev_mode = NORMAL;
775 282695 : set_f( hBWE_FD->prev_SWB_fenv, 0, SWB_FENV );
776 282695 : hBWE_FD->prev_Energy = 0.0f;
777 282695 : hBWE_FD->prev_L_swb_norm = 8;
778 282695 : hBWE_FD->Seed = 21211;
779 282695 : hBWE_FD->prev_frica_flag = 0;
780 282695 : set_f( hBWE_FD->mem_imdct, 0, L_FRAME48k );
781 282695 : hBWE_FD->prev_td_energy = 0.0f;
782 282695 : hBWE_FD->prev_weight = 0.2f;
783 282695 : hBWE_FD->prev_flag = 0;
784 282695 : hBWE_FD->last_wb_bwe_ener = 0.0f;
785 282695 : hBWE_FD->prev_Energy_wb = 0.0f;
786 :
787 282695 : hBWE_FD->mem_deemph_old_syn = 0.0f;
788 :
789 282695 : hBWE_FD->prev_fb_ener_adjust = 0.0f;
790 :
791 282695 : return;
792 : }
|