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 "prot.h"
44 : #include "rom_com.h"
45 : #include "wmc_auto.h"
46 :
47 : /*-------------------------------------------------------------------*
48 : * swb_bwe_dec_hr()
49 : *
50 : * HR SWB BWE decoder
51 : *-------------------------------------------------------------------*/
52 :
53 20656 : void swb_bwe_dec_hr(
54 : Decoder_State *st, /* i/o: decoder state structure */
55 : const float *syn_12k8_16k, /* i : ACELP core synthesis @16kHz */
56 : float *hb_synth, /* o : SHB synthesis */
57 : const int16_t output_frame, /* i : frame length */
58 : const int16_t unbits, /* i : number of core unused bits */
59 : const float pitch_buf[] /* i : pitch buffer */
60 : )
61 : {
62 : int16_t i, j, k, nBits, nBits_total, nBits_block, Nsv, Nsv2, width_noncoded;
63 : int16_t is_transient, tmpS, incr, IsTransient, pos;
64 : FD_BWE_DEC_HANDLE hBWE_FD;
65 : HR_BWE_DEC_HANDLE hBWE_FD_HR;
66 : int16_t x_norm[NSV_MAX * ( WIDTH_BAND + 1 )], x_norm1[NSV_MAX * ( WIDTH_BAND + 1 )];
67 : float t_audio[L_FRAME48k], t_audio_tmp[L_FRAME48k];
68 20656 : float gain = 0.0f, gain2, en_band[N_BANDS_BWE_HR];
69 : int16_t ind1, ind2;
70 : float EnergyLT, Energy;
71 : int16_t nq[NSV_MAX], nq2[NSV_MAX], nq_tmp[NSV_MAX];
72 : float tilt_wb, min_env, max_env;
73 : float tmpF, tmp2, pitch, alpha;
74 : float en_noncoded;
75 20656 : float env = 0.0f;
76 :
77 20656 : hBWE_FD = st->hBWE_FD;
78 20656 : hBWE_FD_HR = st->hBWE_FD_HR;
79 :
80 : /*---------------------------------------------------------------------*
81 : * initializations
82 : *---------------------------------------------------------------------*/
83 :
84 20656 : set_f( t_audio, 0.0f, output_frame );
85 :
86 20656 : hBWE_FD_HR->bwe_highrate_seed = (int16_t) ( (int16_t) ( pitch_buf[0] * 64.0f ) * (int16_t) ( pitch_buf[3] * 64.0f ) );
87 :
88 20656 : ind2 = 0; /* only to suppress warnings */
89 20656 : Nsv2 = 0; /* only to suppress warnings */
90 20656 : gain2 = 0; /* only to suppress warnings */
91 :
92 : /* reset memories in case that last frame was a different technology */
93 20656 : if ( st->last_core == HQ_CORE || st->last_extl != st->extl )
94 : {
95 266 : set_f( hBWE_FD->old_wtda_swb, 0, L_FRAME48k );
96 : }
97 :
98 : /* calculate SWB BWE bit-budget */
99 20656 : nBits = (int16_t) ( st->extl_brate ) / FRAMES_PER_SEC + unbits;
100 20656 : nBits_total = nBits;
101 :
102 : /*---------------------------------------------------------------------*
103 : * calculate tilt of the core synthesis
104 : *---------------------------------------------------------------------*/
105 :
106 20656 : calc_tilt_bwe( syn_12k8_16k, &tilt_wb, L_FRAME16k );
107 20656 : pitch = sum_f( pitch_buf, NB_SUBFR16k ) + EPSILON;
108 :
109 : /*---------------------------------------------------------------------*
110 : * FEC, or good frame decoding
111 : *---------------------------------------------------------------------*/
112 :
113 20656 : if ( st->bfi )
114 : {
115 34 : is_transient = hBWE_FD_HR->old_is_transient_hr_bwe;
116 :
117 : /* Replication of the last spectrum, with an attenuation */
118 34 : if ( ( st->clas_dec == VOICED_CLAS || st->clas_dec == INACTIVE_CLAS ) && st->nbLostCmpt <= 3 )
119 : {
120 20 : alpha = 0.8f;
121 : }
122 14 : else if ( is_transient )
123 : {
124 3 : alpha = 0.15f;
125 : }
126 : else
127 : {
128 11 : alpha = 0.3f;
129 : }
130 :
131 34 : if ( is_transient )
132 : {
133 : /* set BWE spectrum length */
134 3 : if ( output_frame == L_FRAME32k )
135 : {
136 3 : tmpS = L_FRAME32k / NUM_TIME_SWITCHING_BLOCKS - NUM_TRANS_START_FREQ_COEF;
137 : }
138 : else /* output_frame == L_FRAME48k */
139 : {
140 0 : tmpS = ( 2 * END_FREQ_BWE_FULL_FB / FRAMES_PER_SEC ) / NUM_TIME_SWITCHING_BLOCKS - NUM_TRANS_START_FREQ_COEF;
141 : }
142 :
143 : /* reconstruct */
144 15 : for ( k = 0; k < NUM_TIME_SWITCHING_BLOCKS; k++ )
145 : {
146 1020 : for ( i = 0; i < tmpS; i++ )
147 : {
148 1008 : t_audio[NUM_TRANS_START_FREQ_COEF + k * output_frame / NUM_TIME_SWITCHING_BLOCKS + i] = alpha * hBWE_FD_HR->t_audio_prev[i + k * tmpS];
149 : }
150 : /* save transform coefficients for the next frame (needed in case of frame erasures) */
151 12 : mvr2r( t_audio + NUM_TRANS_START_FREQ_COEF + k * output_frame / NUM_TIME_SWITCHING_BLOCKS, hBWE_FD_HR->t_audio_prev + k * tmpS, tmpS );
152 : }
153 : }
154 : else
155 : {
156 : /* set BWE spectrum length */
157 31 : if ( output_frame == L_FRAME32k )
158 : {
159 31 : tmpS = L_FRAME32k - NUM_NONTRANS_START_FREQ_COEF;
160 : }
161 : else /* output_frame == L_FRAME48k */
162 : {
163 0 : tmpS = 2 * END_FREQ_BWE_FULL_FB / FRAMES_PER_SEC - NUM_NONTRANS_START_FREQ_COEF;
164 : }
165 :
166 : /* reconstruct */
167 10447 : for ( i = 0; i < tmpS; i++ )
168 : {
169 10416 : t_audio[NUM_NONTRANS_START_FREQ_COEF + i] = alpha * hBWE_FD_HR->t_audio_prev[i];
170 : }
171 : /* Save transform coefficients for the next frame (needed in case of frame erasures) */
172 31 : mvr2r( t_audio + NUM_NONTRANS_START_FREQ_COEF, hBWE_FD_HR->t_audio_prev, tmpS );
173 : }
174 :
175 34 : hBWE_FD_HR->mem_EnergyLT *= alpha;
176 34 : gain = (float) ( 2.0f * sqrt( hBWE_FD_HR->mem_EnergyLT / output_frame ) );
177 34 : env = 1.0f;
178 : }
179 : else
180 : {
181 : /*---------------------------------------------------------------------*
182 : * get transient frame flag
183 : *---------------------------------------------------------------------*/
184 :
185 20622 : is_transient = get_next_indice( st, 1 );
186 :
187 20622 : if ( is_transient )
188 : {
189 1312 : nBits = -1; /* is_transient flag */
190 1312 : nBits_block = nBits_total / NUM_TIME_SWITCHING_BLOCKS;
191 1312 : nBits += nBits_total % NUM_TIME_SWITCHING_BLOCKS;
192 :
193 : /* set width of noncoded (blind estimated) spectrum */
194 1312 : if ( st->extl == SWB_BWE_HIGHRATE || output_frame == L_FRAME32k )
195 : {
196 1248 : width_noncoded = L_FRAME32k / NUM_TIME_SWITCHING_BLOCKS - NUM_TRANS_END_FREQ_COEF;
197 1248 : tmpS = L_FRAME32k / NUM_TIME_SWITCHING_BLOCKS - NUM_TRANS_END_FREQ_COEF_EFF;
198 : }
199 : else /* st->extl == FB_BWE_HIGHRATE */
200 : {
201 64 : width_noncoded = ( 2 * END_FREQ_BWE_FULL_FB / FRAMES_PER_SEC ) / NUM_TIME_SWITCHING_BLOCKS - NUM_TRANS_END_FREQ_COEF;
202 64 : tmpS = ( 2 * END_FREQ_BWE_FULL_FB / FRAMES_PER_SEC ) / NUM_TIME_SWITCHING_BLOCKS - NUM_TRANS_END_FREQ_COEF_EFF;
203 : }
204 :
205 : /*---------------------------------------------------------------------*
206 : * transient frames: processing in blocks (subframes)
207 : *---------------------------------------------------------------------*/
208 :
209 6560 : for ( k = 0; k < NUM_TIME_SWITCHING_BLOCKS; k++ )
210 : {
211 5248 : nBits += nBits_block;
212 :
213 : /*---------------------------------------------------------------------*
214 : * global gain and envelope decoding
215 : *---------------------------------------------------------------------*/
216 :
217 : /* get global gain */
218 5248 : ind1 = get_next_indice( st, NBITS_GLOB_GAIN_BWE_HR );
219 5248 : gain = gain_dequant( ind1, MIN_GLOB_GAIN_BWE_HR, MAX_GLOB_GAIN_BWE_HR, NBITS_GLOB_GAIN_BWE_HR );
220 5248 : nBits -= NBITS_GLOB_GAIN_BWE_HR;
221 :
222 : /* get energy per band */
223 5248 : if ( k == 0 )
224 : {
225 1312 : ind1 = get_next_indice( st, NBITS_ENVELOPE_BWE_HR_TR );
226 1312 : ind2 = ind1;
227 1312 : nBits -= NBITS_ENVELOPE_BWE_HR_TR;
228 : }
229 : else
230 : {
231 3936 : if ( ind2 < 8 )
232 : {
233 774 : ind1 = get_next_indice( st, NBITS_ENVELOPE_BWE_HR_TR - 1 );
234 : }
235 : else
236 : {
237 3162 : ind1 = get_next_indice( st, NBITS_ENVELOPE_BWE_HR_TR - 1 ) + NUM_ENVLOPE_CODE_HR_TR2;
238 : }
239 3936 : nBits -= ( NBITS_ENVELOPE_BWE_HR_TR - 1 );
240 : }
241 :
242 5248 : en_band[0] = swb_hr_env_code3[2 * ind1];
243 5248 : en_band[1] = swb_hr_env_code3[2 * ind1 + 1];
244 5248 : env = 0.5f * ( en_band[0] + en_band[1] );
245 :
246 : /*---------------------------------------------------------------------*
247 : * estimate energy of noncoded spectrum (14.4-20kHz)
248 : *---------------------------------------------------------------------*/
249 :
250 5248 : en_noncoded = en_band[N_BANDS_TRANS_BWE_HR - 1];
251 :
252 5248 : if ( st->extl == FB_BWE_HIGHRATE )
253 : {
254 256 : ind1 = get_next_indice( st, NBITS_HF_GAIN_BWE_HR );
255 256 : nBits -= NBITS_HF_GAIN_BWE_HR;
256 :
257 256 : if ( ind1 == 1 )
258 : {
259 3 : en_noncoded *= BWE_HR_TRANS_EN_LIMIT1;
260 : }
261 253 : else if ( ind1 == 2 )
262 : {
263 74 : en_noncoded *= BWE_HR_TRANS_EN_LIMIT2;
264 : }
265 179 : else if ( ind1 == 3 )
266 : {
267 52 : en_noncoded *= BWE_HR_TRANS_EN_LIMIT3;
268 : }
269 : }
270 :
271 : /*---------------------------------------------------------------------*
272 : * AVQ decoding (dequantize normalized spectrum)
273 : *---------------------------------------------------------------------*/
274 :
275 5248 : Nsv = ( NUM_TRANS_END_FREQ_COEF - NUM_TRANS_START_FREQ_COEF ) / WIDTH_BAND;
276 5248 : AVQ_demuxdec( st, x_norm, &nBits, Nsv, nq, 0, Nsv - 1 );
277 341120 : for ( i = 0; i < Nsv * WIDTH_BAND; i++ )
278 : {
279 335872 : t_audio[k * output_frame / NUM_TIME_SWITCHING_BLOCKS + NUM_TRANS_START_FREQ_COEF + i] = (float) ( x_norm[i] );
280 : }
281 :
282 : /* apply noise-fill */
283 5248 : swb_hr_noise_fill( is_transient, NUM_TRANS_START_FREQ_COEF, NUM_TRANS_END_FREQ_COEF, tilt_wb, pitch, nq, Nsv, &hBWE_FD_HR->bwe_highrate_seed, t_audio + NUM_TRANS_START_FREQ_COEF + k * output_frame / NUM_TIME_SWITCHING_BLOCKS );
284 :
285 : /*---------------------------------------------------------------------*
286 : * reconstruction
287 : *---------------------------------------------------------------------*/
288 :
289 : /* reconstruct 14-16(20) kHz spectrum */
290 120448 : for ( j = 0; j < tmpS; j++ )
291 : {
292 115200 : t_audio[k * output_frame / NUM_TIME_SWITCHING_BLOCKS + NUM_TRANS_END_FREQ_COEF_EFF + j] = 0.5f * t_audio[k * output_frame / NUM_TIME_SWITCHING_BLOCKS + NUM_TRANS_END_FREQ_COEF_EFF - tmpS + j];
293 : }
294 :
295 : /* envelope denormalization */
296 15744 : for ( i = 0; i < N_BANDS_TRANS_BWE_HR; i++ )
297 : {
298 367360 : for ( j = 0; j < WIDTH_TRANS_FREQ_COEF; j++ )
299 : {
300 356864 : t_audio[k * output_frame / NUM_TIME_SWITCHING_BLOCKS + NUM_TRANS_START_FREQ_COEF + i * WIDTH_TRANS_FREQ_COEF + j] *= en_band[i];
301 : }
302 : }
303 :
304 : /* envelope denormalization of 14.4-16(20) kHz spectrum */
305 99456 : for ( j = tmpS - width_noncoded; j < tmpS; j++ )
306 : {
307 94208 : t_audio[k * output_frame / NUM_TIME_SWITCHING_BLOCKS + NUM_TRANS_END_FREQ_COEF_EFF + j] *= en_noncoded;
308 : }
309 :
310 : /* overlap region */
311 5248 : if ( output_frame == L_FRAME48k )
312 : {
313 24740 : for ( i = 0; i < NSV_OVERLAP * WIDTH_BAND / NUM_TIME_SWITCHING_BLOCKS; i++ )
314 : {
315 19792 : t_audio[k * output_frame / NUM_TIME_SWITCHING_BLOCKS + NUM_TRANS_START_FREQ_COEF + i] *= overlap_coefs_48kHz[i * 4];
316 : }
317 : }
318 : else
319 : {
320 1500 : for ( i = 0; i < NSV_OVERLAP * WIDTH_BAND / NUM_TIME_SWITCHING_BLOCKS; i++ )
321 : {
322 1200 : t_audio[k * output_frame / NUM_TIME_SWITCHING_BLOCKS + NUM_TRANS_START_FREQ_COEF + i] *= overlap_coefs[i * 4];
323 : }
324 : }
325 :
326 : /* apply global gain */
327 456320 : for ( i = 0; i < WIDTH_TRANS_FREQ_COEF * N_BANDS_TRANS_BWE_HR + width_noncoded; i++ )
328 : {
329 451072 : t_audio[NUM_TRANS_START_FREQ_COEF + k * output_frame / NUM_TIME_SWITCHING_BLOCKS + i] *= gain;
330 : }
331 :
332 : /* save transform coefficients for the next frame (needed in case of frame erasures) */
333 5248 : if ( output_frame == L_FRAME32k )
334 : {
335 300 : mvr2r( t_audio + NUM_TRANS_START_FREQ_COEF + k * output_frame / NUM_TIME_SWITCHING_BLOCKS, hBWE_FD_HR->t_audio_prev + k * ( L_FRAME32k / NUM_TIME_SWITCHING_BLOCKS - NUM_TRANS_START_FREQ_COEF ), L_FRAME32k / NUM_TIME_SWITCHING_BLOCKS - NUM_TRANS_START_FREQ_COEF );
336 : }
337 : else /* output_frame == L_FRAME48k */
338 : {
339 4948 : mvr2r( t_audio + NUM_TRANS_START_FREQ_COEF + k * output_frame / NUM_TIME_SWITCHING_BLOCKS, hBWE_FD_HR->t_audio_prev + k * ( ( 2 * END_FREQ_BWE_FULL_FB / FRAMES_PER_SEC ) / NUM_TIME_SWITCHING_BLOCKS - NUM_TRANS_START_FREQ_COEF ), ( 2 * END_FREQ_BWE_FULL_FB / FRAMES_PER_SEC ) / NUM_TIME_SWITCHING_BLOCKS - NUM_TRANS_START_FREQ_COEF );
340 : }
341 :
342 : /* attenuate HFs in case of band-width switching */
343 5248 : if ( st->bws_cnt1 > 0 )
344 : {
345 16 : if ( output_frame == L_FRAME32k )
346 : {
347 16 : j = L_FRAME32k / NUM_TIME_SWITCHING_BLOCKS - NUM_TRANS_START_FREQ_COEF;
348 : }
349 : else /* output_frame == L_FRAME48k */
350 : {
351 0 : j = ( 2 * END_FREQ_BWE_FULL_FB / FRAMES_PER_SEC ) / NUM_TIME_SWITCHING_BLOCKS - NUM_TRANS_START_FREQ_COEF;
352 : }
353 :
354 1360 : for ( i = 0; i < j; i++ )
355 : {
356 1344 : t_audio[NUM_TRANS_START_FREQ_COEF + k * output_frame / NUM_TIME_SWITCHING_BLOCKS + i] *= (float) st->bws_cnt1 / (float) N_NS2W_FRAMES;
357 : }
358 : }
359 : }
360 : }
361 : else /* !is_transient */
362 : {
363 : /* subtract one bit for is_transient flag */
364 19310 : nBits--;
365 :
366 : /*---------------------------------------------------------------------*
367 : * global gain and envelope decoding
368 : *---------------------------------------------------------------------*/
369 :
370 : /* get global gain */
371 19310 : ind1 = get_next_indice( st, NBITS_GLOB_GAIN_BWE_HR );
372 19310 : gain = gain_dequant( ind1, MIN_GLOB_GAIN_BWE_HR, MAX_GLOB_GAIN_BWE_HR, NBITS_GLOB_GAIN_BWE_HR );
373 :
374 : /* get energy per band */
375 19310 : ind1 = get_next_indice( st, NBITS_ENVELOPE_BWE_HR1 );
376 19310 : ind2 = get_next_indice( st, NBITS_ENVELOPE_BWE_HR2 );
377 :
378 19310 : en_band[0] = swb_hr_env_code1[2 * ind1];
379 19310 : en_band[1] = swb_hr_env_code1[2 * ind1 + 1];
380 19310 : en_band[2] = swb_hr_env_code2[2 * ind2];
381 19310 : en_band[3] = swb_hr_env_code2[2 * ind2 + 1];
382 19310 : env = 0.25f * ( en_band[0] + en_band[1] + en_band[2] + en_band[3] );
383 :
384 : /*---------------------------------------------------------------------*
385 : * choose sub-bands to be dequantized
386 : *---------------------------------------------------------------------*/
387 :
388 : /* find the subband with the min envelope */
389 19310 : pos = 0;
390 19310 : min_env = en_band[0];
391 19310 : max_env = en_band[0];
392 77240 : for ( j = 1; j < N_BANDS_BWE_HR; j++ )
393 : {
394 57930 : if ( en_band[j] < min_env )
395 : {
396 38032 : pos = j;
397 38032 : min_env = en_band[j];
398 : }
399 57930 : if ( en_band[j] > max_env )
400 : {
401 9530 : max_env = en_band[j];
402 : }
403 : }
404 :
405 : /* decide the spectrum to be dequantized */
406 19310 : if ( nBits_total > NBITS_THRESH_BWE_HR )
407 : {
408 150 : i = NUM_NONTRANS_END_FREQ_COEF - NUM_NONTRANS_START_FREQ_COEF;
409 : }
410 : else
411 : {
412 19160 : i = NUM_NONTRANS_END_FREQ_COEF - NUM_NONTRANS_START_FREQ_COEF - 64 - 8 * ( pos % 2 );
413 : }
414 :
415 19310 : nBits -= NBITS_GLOB_GAIN_BWE_HR + NBITS_ENVELOPE_BWE_HR1 + NBITS_ENVELOPE_BWE_HR2;
416 :
417 : /*---------------------------------------------------------------------*
418 : * estimate energy of noncoded spectrum (14.4-20kHz)
419 : *---------------------------------------------------------------------*/
420 :
421 19310 : en_noncoded = 0.5f * min_env;
422 :
423 19310 : if ( st->extl == FB_BWE_HIGHRATE )
424 : {
425 605 : ind1 = get_next_indice( st, NBITS_HF_GAIN_BWE_HR );
426 605 : nBits -= NBITS_HF_GAIN_BWE_HR;
427 :
428 605 : if ( ind1 == 1 )
429 : {
430 231 : en_noncoded *= BWE_HR_NONTRANS_EN_LIMIT1;
431 : }
432 374 : else if ( ind1 == 2 )
433 : {
434 80 : en_noncoded *= 2.0f * BWE_HR_NONTRANS_EN_LIMIT2;
435 : }
436 294 : else if ( ind1 == 3 )
437 : {
438 174 : en_noncoded *= 2.0f * BWE_HR_NONTRANS_EN_LIMIT3;
439 : }
440 : }
441 :
442 : /*---------------------------------------------------------------------*
443 : * AVQ decoding (dequantize normalized spectrum)
444 : *---------------------------------------------------------------------*/
445 :
446 19310 : Nsv = i / WIDTH_BAND;
447 19310 : AVQ_demuxdec( st, x_norm, &nBits, Nsv, nq, 0, Nsv - 1 );
448 : /*---------------------------------------------------------------------*
449 : * second stage decoding
450 : *---------------------------------------------------------------------*/
451 :
452 19310 : if ( nBits >= 9 + NBITS_GLOB_GAIN_BWE_HR && sum_s( nq, Nsv ) > 0 )
453 : {
454 18617 : ind1 = get_next_indice( st, NBITS_GLOB_GAIN_BWE_HR );
455 18617 : gain2 = gain_dequant( ind1, MIN_GLOB_GAIN_BWE_HR, MAX_GLOB_GAIN_BWE_HR, NBITS_GLOB_GAIN_BWE_HR );
456 18617 : gain2 *= 0.0625f;
457 :
458 : /* calculate the number of subbands according to the rest bits */
459 18617 : if ( nBits > 396 )
460 : {
461 69 : Nsv2 = 33;
462 : }
463 : else
464 : {
465 18548 : Nsv2 = nBits / 12;
466 : }
467 :
468 18617 : nBits -= NBITS_GLOB_GAIN_BWE_HR;
469 18617 : AVQ_demuxdec( st, x_norm1, &nBits, Nsv2, nq2, 0, Nsv2 - 1 );
470 : }
471 :
472 : /*---------------------------------------------------------------------*
473 : * dequantization
474 : *---------------------------------------------------------------------*/
475 :
476 3915558 : for ( i = 0; i < Nsv * WIDTH_BAND; i++ )
477 : {
478 3896248 : t_audio_tmp[i] = (float) ( x_norm[i] );
479 : }
480 :
481 19310 : mvs2s( nq, nq_tmp, Nsv );
482 19310 : if ( Nsv2 > Nsv )
483 : {
484 : /* Safety check, happens rarely */
485 0 : set_s( nq_tmp + Nsv, 0, Nsv2 - Nsv );
486 : }
487 :
488 19310 : k = 0;
489 19310 : incr = 0;
490 506341 : for ( i = 0; i < Nsv; i++ )
491 : {
492 487031 : if ( nq[i] == 0 && incr < Nsv2 )
493 : {
494 714006 : for ( j = 0; j < WIDTH_BAND; j++ )
495 : {
496 634672 : t_audio_tmp[i * WIDTH_BAND + j] = gain2 * x_norm1[k++];
497 : }
498 79334 : nq[i] += nq2[incr++];
499 : }
500 : }
501 :
502 31134 : for ( i = 0; incr < Nsv2; i++ )
503 : {
504 : /* safety check, happens rarely */
505 11824 : if ( i >= Nsv2 )
506 : {
507 0 : break;
508 : }
509 :
510 11824 : if ( nq_tmp[i] != 0 )
511 : {
512 89514 : for ( j = 0; j < WIDTH_BAND; j++ )
513 : {
514 79568 : t_audio_tmp[i * WIDTH_BAND + j] += gain2 * x_norm1[k++];
515 : }
516 9946 : nq[i] += nq2[incr++];
517 : }
518 : }
519 :
520 : /*---------------------------------------------------------------------*
521 : * reorder the decoded spectrum
522 : *---------------------------------------------------------------------*/
523 :
524 19310 : if ( nBits_total > NBITS_THRESH_BWE_HR )
525 : {
526 150 : mvr2r( t_audio_tmp, t_audio + NUM_NONTRANS_START_FREQ_COEF, NUM_NONTRANS_END_FREQ_COEF - NUM_NONTRANS_START_FREQ_COEF );
527 : }
528 : else
529 : {
530 19160 : ind1 = ( pos * 64 + pos / 2 * WIDTH_BAND );
531 19160 : mvr2r( t_audio_tmp, t_audio + NUM_NONTRANS_START_FREQ_COEF, ind1 );
532 :
533 19160 : ind2 = ( ( pos + 1 ) * 64 + ( pos + 1 ) / 2 * WIDTH_BAND );
534 19160 : mvr2r( t_audio_tmp + ind1, t_audio + NUM_NONTRANS_START_FREQ_COEF + ind2, NUM_NONTRANS_END_FREQ_COEF - NUM_NONTRANS_START_FREQ_COEF - ind2 );
535 :
536 : /* reconstruct non-encoded subband */
537 19160 : if ( pos == 3 )
538 : {
539 13008 : mvr2r( t_audio + NUM_NONTRANS_START_FREQ_COEF + 128, t_audio + NUM_NONTRANS_START_FREQ_COEF + 200, 72 );
540 :
541 13008 : mvs2s( nq + 16, nq + 25, 9 );
542 : }
543 : else
544 : {
545 6152 : pos %= 2;
546 6152 : mvr2r( t_audio + NUM_NONTRANS_START_FREQ_COEF + ind2, t_audio + NUM_NONTRANS_START_FREQ_COEF + ind1, 64 + pos * WIDTH_BAND );
547 6152 : ind1 /= WIDTH_BAND;
548 6152 : ind2 /= WIDTH_BAND;
549 :
550 6152 : j = 0;
551 111258 : for ( i = Nsv - 1; i >= ind1; i-- )
552 : {
553 105106 : nq[33 - j++] = nq[i];
554 : }
555 :
556 6152 : mvs2s( nq + ind2, nq + ind1, WIDTH_BAND + pos );
557 : }
558 : }
559 :
560 : /* apply noise-fill */
561 19310 : if ( nBits < 200 )
562 : {
563 19259 : swb_hr_noise_fill( is_transient, NUM_NONTRANS_START_FREQ_COEF, NUM_NONTRANS_END_FREQ_COEF, tilt_wb, pitch, nq, Nsv, &hBWE_FD_HR->bwe_highrate_seed, t_audio + NUM_NONTRANS_START_FREQ_COEF );
564 : }
565 :
566 : /*---------------------------------------------------------------------*
567 : * reconstruction
568 : *---------------------------------------------------------------------*/
569 :
570 : /* smoothing 12.6-12.8kHz */
571 19310 : if ( pos == 3 && nBits_total <= 400 )
572 : {
573 13008 : tmpF = sum2_f( t_audio + NUM_NONTRANS_START_FREQ_COEF + 200 - WIDTH_BAND, WIDTH_BAND ) + EPSILON;
574 13008 : tmp2 = sum2_f( t_audio + NUM_NONTRANS_START_FREQ_COEF + 200, WIDTH_BAND ) + EPSILON;
575 13008 : tmpF = (float) sqrt( tmpF / tmp2 );
576 117072 : for ( i = 0; i < WIDTH_BAND; i++ )
577 : {
578 104064 : t_audio[NUM_NONTRANS_START_FREQ_COEF + 200 + i] *= ( ( 1.0f - i / (float) WIDTH_BAND ) * tmpF + i / (float) WIDTH_BAND );
579 : }
580 : }
581 :
582 : /* reconstruct 14.4-16(20) kHz spectrum */
583 19310 : if ( st->extl == SWB_BWE_HIGHRATE || output_frame == L_FRAME32k )
584 : {
585 18705 : width_noncoded = L_FRAME32k - NUM_NONTRANS_END_FREQ_COEF;
586 : }
587 : else /* st->extl == FB_BWE_HIGHRATE */
588 : {
589 605 : width_noncoded = 2 * END_FREQ_BWE_FULL_FB / FRAMES_PER_SEC - NUM_NONTRANS_END_FREQ_COEF;
590 : }
591 19310 : mvr2r( t_audio + NUM_NONTRANS_END_FREQ_COEF - width_noncoded, t_audio + NUM_NONTRANS_END_FREQ_COEF, width_noncoded );
592 :
593 : /* smoothing 14.4-14.8kHz */
594 19310 : tmpF = sum2_f( t_audio + NUM_NONTRANS_END_FREQ_COEF - WIDTH_BAND, WIDTH_BAND ) + EPSILON;
595 19310 : tmp2 = sum2_f( t_audio + NUM_NONTRANS_END_FREQ_COEF, WIDTH_BAND ) + EPSILON;
596 19310 : tmpF = (float) sqrt( tmpF / tmp2 );
597 173790 : for ( i = 0; i < WIDTH_BAND; i++ )
598 : {
599 154480 : t_audio[NUM_NONTRANS_END_FREQ_COEF + i] *= tmpF;
600 : }
601 :
602 : /* envelope denormalization */
603 96550 : for ( i = 0; i < N_BANDS_BWE_HR; i++ )
604 : {
605 5329560 : for ( j = 0; j < WIDTH_NONTRANS_FREQ_COEF; j++ )
606 : {
607 5252320 : t_audio[NUM_NONTRANS_START_FREQ_COEF + i * WIDTH_NONTRANS_FREQ_COEF + j] *= en_band[i];
608 : }
609 : }
610 :
611 : /* equalize 14.4-16(20) kHz spectrum */
612 19310 : tmpF = max_env / min_env;
613 19310 : if ( st->extl == SWB_BWE_HIGHRATE || tmpF < 2.2f )
614 : {
615 170262 : for ( j = 0; j < WIDTH_BAND; j++ )
616 : {
617 151344 : t_audio[NUM_NONTRANS_END_FREQ_COEF + j] *= ( ( 1.0f - j / (float) WIDTH_BAND ) * en_band[3] + ( j / (float) WIDTH_BAND ) * en_noncoded );
618 : }
619 :
620 1112406 : for ( j = WIDTH_BAND; j < width_noncoded; j++ )
621 : {
622 1093488 : t_audio[NUM_NONTRANS_END_FREQ_COEF + j] *= en_noncoded;
623 : }
624 : }
625 : else
626 : {
627 392 : if ( output_frame == L_FRAME48k )
628 : {
629 392 : tmpS = width_noncoded - 2 * WIDTH_NONTRANS_FREQ_COEF;
630 : }
631 : else
632 : {
633 0 : tmpS = L_FRAME32k - NUM_NONTRANS_END_FREQ_COEF;
634 : }
635 :
636 392 : k = 0;
637 34888 : for ( j = 0; j < tmpS; j++ )
638 : {
639 34496 : t_audio[NUM_NONTRANS_END_FREQ_COEF + j] *= 2.2f * en_noncoded * ( 1 - (float) k / (float) 160 );
640 34496 : k++;
641 : }
642 :
643 392 : k = 0;
644 53704 : for ( ; j < width_noncoded; j++ )
645 : {
646 53312 : t_audio[NUM_NONTRANS_END_FREQ_COEF + j] *= 0.65f * en_noncoded * ( 1 - (float) k / (float) 320 );
647 53312 : k++;
648 : }
649 : }
650 :
651 : /* overlap region */
652 19310 : if ( output_frame == L_FRAME48k )
653 : {
654 316591 : for ( i = 0; i < NSV_OVERLAP * WIDTH_BAND; i++ )
655 : {
656 297968 : t_audio[NUM_NONTRANS_START_FREQ_COEF + i] *= overlap_coefs_48kHz[i];
657 : }
658 : }
659 : else
660 : {
661 11679 : for ( i = 0; i < NSV_OVERLAP * WIDTH_BAND; i++ )
662 : {
663 10992 : t_audio[NUM_NONTRANS_START_FREQ_COEF + i] *= overlap_coefs[i];
664 : }
665 : }
666 :
667 : /* apply global gain */
668 19310 : if ( nBits_total <= NBITS_THRESH_BWE_HR )
669 : {
670 19160 : gain *= 0.85f;
671 : }
672 :
673 6604270 : for ( i = 0; i < WIDTH_NONTRANS_FREQ_COEF * N_BANDS_BWE_HR + width_noncoded; i++ )
674 : {
675 6584960 : t_audio[NUM_NONTRANS_START_FREQ_COEF + i] *= gain;
676 : }
677 :
678 : /* save transform coefficients for the next frame (needed in case of frame erasures) */
679 19310 : if ( output_frame == L_FRAME32k )
680 : {
681 687 : mvr2r( t_audio + NUM_NONTRANS_START_FREQ_COEF, hBWE_FD_HR->t_audio_prev, L_FRAME32k - NUM_NONTRANS_START_FREQ_COEF );
682 : }
683 : else /* output_frame == L_FRAME48k */
684 : {
685 18623 : mvr2r( t_audio + NUM_NONTRANS_START_FREQ_COEF, hBWE_FD_HR->t_audio_prev, 2 * END_FREQ_BWE_FULL_FB / FRAMES_PER_SEC - NUM_NONTRANS_START_FREQ_COEF );
686 : }
687 :
688 : /* attenuate HFs in case of band-width switching */
689 19310 : if ( st->bws_cnt1 > 0 )
690 : {
691 16 : if ( output_frame == L_FRAME32k )
692 : {
693 16 : j = L_FRAME32k - NUM_NONTRANS_START_FREQ_COEF;
694 : }
695 : else /* output_frame == L_FRAME48k */
696 : {
697 0 : j = 2 * END_FREQ_BWE_FULL_FB / FRAMES_PER_SEC - NUM_NONTRANS_START_FREQ_COEF;
698 : }
699 :
700 5392 : for ( i = 0; i < j; i++ )
701 : {
702 5376 : t_audio[NUM_NONTRANS_START_FREQ_COEF + i] *= (float) st->bws_cnt1 / (float) N_NS2W_FRAMES;
703 : }
704 : }
705 : }
706 : }
707 :
708 20656 : st->prev_ener_shb = gain * env;
709 309840 : for ( i = 0; i < SWB_FENV; i++ )
710 : {
711 289184 : hBWE_FD->prev_SWB_fenv[i] = gain * env;
712 : }
713 :
714 : /*---------------------------------------------------------------------*
715 : * iOLA and iMDCT
716 : *---------------------------------------------------------------------*/
717 :
718 20656 : inverse_transform( t_audio, t_audio_tmp, is_transient, output_frame, output_frame, st->element_mode );
719 :
720 20656 : window_ola( t_audio_tmp, hb_synth, hBWE_FD->old_wtda_swb, output_frame, ALDO_WINDOW, ALDO_WINDOW, 0, 0, 0 );
721 :
722 : /*---------------------------------------------------------------------*
723 : * final adjustments
724 : *---------------------------------------------------------------------*/
725 :
726 20656 : if ( !st->bfi )
727 : {
728 20622 : IsTransient = 0;
729 20622 : EnergyLT = hBWE_FD_HR->mem_EnergyLT;
730 20622 : pos = 0;
731 103110 : for ( j = 0; j < 4; j++ )
732 : {
733 82488 : Energy = sum2_f( hb_synth + j * ( output_frame / 4 ), output_frame / 4 ) + EPSILON;
734 82488 : if ( Energy > 12.5f * EnergyLT )
735 : {
736 565 : IsTransient = 1;
737 565 : pos = j;
738 : }
739 :
740 82488 : EnergyLT = 0.75f * EnergyLT + 0.25f * Energy;
741 : }
742 :
743 20622 : if ( IsTransient == 1 && pos > 0 && tilt_wb < 3.0f && pitch > 500 )
744 : {
745 84 : Nsv = pos * ( output_frame / 4 );
746 84 : Energy = sum2_f( hb_synth, Nsv ) + EPSILON;
747 84 : if ( st->last_extl != st->extl )
748 : {
749 11 : hBWE_FD_HR->mem_EnergyLT = Energy;
750 : }
751 84 : gain = (float) sqrt( pos * hBWE_FD_HR->mem_EnergyLT / Energy );
752 :
753 84 : gain *= 0.2f;
754 41604 : for ( i = 0; i < Nsv; i++ )
755 : {
756 41520 : hb_synth[i] *= gain;
757 : }
758 :
759 84 : alpha = (float) WIDTH_BAND / output_frame;
760 10044 : for ( i = 0; i < output_frame / WIDTH_BAND; i++ )
761 : {
762 9960 : hb_synth[i + Nsv] *= ( ( 1.0f - i * alpha ) * gain + i * alpha );
763 : }
764 : }
765 :
766 20622 : hBWE_FD_HR->mem_EnergyLT = EnergyLT;
767 20622 : hBWE_FD_HR->old_is_transient_hr_bwe = is_transient;
768 : }
769 :
770 : /* post-processing in case of TD/FD switching */
771 20656 : if ( st->last_core == HQ_CORE || st->last_extl != st->extl )
772 : {
773 266 : if ( tilt_wb < 3.0f )
774 : {
775 219 : gain = td_postprocess( hb_synth, output_frame, st->last_extl );
776 :
777 202139 : for ( i = 0; i < output_frame; i++ )
778 : {
779 201920 : hBWE_FD->old_wtda_swb[i] *= gain;
780 : }
781 :
782 219 : tmpS = L_FRAME32k - NUM_NONTRANS_START_FREQ_COEF;
783 219 : if ( output_frame == L_FRAME48k )
784 : {
785 193 : tmpS = 2 * END_FREQ_BWE_FULL_FB / FRAMES_PER_SEC - NUM_NONTRANS_START_FREQ_COEF;
786 : }
787 :
788 104683 : for ( i = 0; i < tmpS; i++ )
789 : {
790 104464 : hBWE_FD_HR->t_audio_prev[i] *= gain;
791 : }
792 : }
793 : }
794 :
795 20656 : return;
796 : }
797 :
798 : /*-------------------------------------------------------------------*
799 : * hr_bwe_dec_init()
800 : *
801 : * Initialize HR BWE state structure at the decoder
802 : *-------------------------------------------------------------------*/
803 :
804 64 : void hr_bwe_dec_init(
805 : HR_BWE_DEC_HANDLE hBWE_FD_HR /* i/o: HR BWE data handle */
806 : )
807 : {
808 64 : set_f( hBWE_FD_HR->t_audio_prev, 0, 2 * END_FREQ_BWE_FULL_FB / FRAMES_PER_SEC - NUM_NONTRANS_START_FREQ_COEF );
809 64 : hBWE_FD_HR->old_is_transient_hr_bwe = 0;
810 64 : hBWE_FD_HR->bwe_highrate_seed = 12345;
811 64 : hBWE_FD_HR->mem_EnergyLT = 0.0f;
812 :
813 64 : return;
814 : }
|