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 : * Local function prototypes
49 : *--------------------------------------------------------------------------*/
50 :
51 : static void overlap_hq_bwe( const float *hq_swb_overlap_buf, float *coeff_out1, const int16_t n_swb_overlap_offset, const int16_t n_swb_overlap, const int16_t *R, const int16_t num_env_bands, const int16_t num_sfm, const int16_t *sfm_end );
52 :
53 : /*--------------------------------------------------------------------------*
54 : * hq_swb_harmonic_calc_norm_envelop()
55 : *
56 : * Calculate normalization envelop
57 : *--------------------------------------------------------------------------*/
58 :
59 6219 : static void hq_swb_harmonic_calc_norm_envelop(
60 : float *SWB_signal, /* i : input signal */
61 : float *envelope, /* o : output envelope */
62 : const int16_t L_swb_norm, /* i : length of normaliztion */
63 : const int16_t SWB_flength /* i : length of input signal */
64 : )
65 : {
66 : int16_t i, lookback, env_index;
67 : int16_t n_freq, n_lag_now, n_lag;
68 :
69 6219 : lookback = L_swb_norm / 2;
70 6219 : env_index = 0;
71 134595 : for ( n_freq = 0; n_freq < lookback; n_freq++ )
72 : {
73 128376 : n_lag_now = lookback + n_freq;
74 :
75 : /* Apply MA filter */
76 128376 : envelope[env_index] = EPSILON;
77 4537890 : for ( n_lag = 0; n_lag < n_lag_now; n_lag++ )
78 : {
79 4409514 : envelope[env_index] += (float) fabs( SWB_signal[n_lag] );
80 : }
81 128376 : env_index++;
82 : }
83 :
84 6219 : n_lag_now = L_swb_norm;
85 :
86 1002663 : for ( n_freq = 0; n_freq < SWB_flength - L_swb_norm; n_freq++ )
87 : {
88 : /* Apply MA filter */
89 996444 : envelope[env_index] = EPSILON;
90 41288910 : for ( n_lag = 0; n_lag < n_lag_now; n_lag++ )
91 : {
92 40292466 : envelope[env_index] += (float) fabs( SWB_signal[n_freq + n_lag] );
93 : }
94 996444 : env_index++;
95 : }
96 :
97 137637 : for ( n_freq = SWB_flength - L_swb_norm, i = 0; n_freq < SWB_flength - lookback; n_freq++, i++ )
98 : {
99 131418 : n_lag_now = L_swb_norm - i;
100 :
101 : /* Apply MA filter */
102 131418 : envelope[env_index] = EPSILON;
103 4798854 : for ( n_lag = 0; n_lag < n_lag_now; n_lag++ )
104 : {
105 4667436 : envelope[env_index] += (float) fabs( SWB_signal[n_freq + n_lag] );
106 : }
107 131418 : env_index++;
108 : }
109 :
110 6219 : return;
111 : }
112 :
113 : /*--------------------------------------------------------------------------*
114 : * noise_level_calc()
115 : *
116 : * Calculate noise level and limited band
117 : *--------------------------------------------------------------------------*/
118 :
119 3158 : void limit_band_noise_level_calc(
120 : const int16_t *wnorm, /* i : reordered norm of sub-vectors */
121 : int16_t *limit, /* o : highest band of bit allocation */
122 : const int32_t core_brate, /* i : core bitrate */
123 : float *noise_level /* o : noise level */
124 : )
125 : {
126 : float ener_limit, ener_sum, fact;
127 : int16_t i;
128 : int16_t nb_sfm;
129 :
130 3158 : nb_sfm = *limit;
131 3158 : ener_limit = 1e-5f;
132 3158 : *noise_level = 0.0f;
133 34738 : for ( i = 0; i < 10; i++ )
134 : {
135 31580 : ener_limit += wnorm[i];
136 31580 : *noise_level += (float) abs( wnorm[i + 1] - wnorm[i] );
137 : }
138 3158 : ener_sum = ener_limit;
139 :
140 65412 : for ( i = 10; i < ( nb_sfm - 1 ); i++ )
141 : {
142 62254 : ener_sum += wnorm[i];
143 62254 : *noise_level += (float) abs( wnorm[i + 1] - wnorm[i] );
144 : }
145 3158 : ener_sum += wnorm[nb_sfm - 1];
146 :
147 3158 : if ( core_brate < HQ_BWE_CROSSOVER_BRATE )
148 : {
149 1622 : fact = 0.885f;
150 : }
151 : else
152 : {
153 1536 : fact = 0.942f;
154 : }
155 :
156 3158 : i = 9;
157 55614 : while ( ener_limit < ener_sum * fact && i + 1 < nb_sfm )
158 : {
159 52456 : ener_limit += wnorm[++i];
160 : }
161 3158 : *limit = i;
162 :
163 : /* calculate noise level for spectrum filling */
164 3158 : *noise_level /= ener_sum;
165 3158 : if ( *noise_level < 0 )
166 : {
167 0 : *noise_level = 0.25f;
168 : }
169 :
170 3158 : *noise_level = 0.25f - *noise_level;
171 :
172 3158 : if ( *noise_level < 0.0f )
173 : {
174 539 : *noise_level = 0.0f;
175 : }
176 :
177 3158 : return;
178 : }
179 :
180 : /*--------------------------------------------------------------------------*
181 : * build_nf_codebook()
182 : *
183 : * Build noise-fill codebook for HQ mode
184 : *--------------------------------------------------------------------------*/
185 :
186 : /*! r: Number of coefficients in nf codebook */
187 20838 : int16_t build_nf_codebook(
188 : const int16_t flag_32K_env_ho, /* i : Envelope attenuation hangover flag */
189 : const float *coeff, /* i : Coded spectral coefficients */
190 : const int16_t *sfm_start, /* i : Subband start indices */
191 : const int16_t *sfmsize, /* i : Subband widths */
192 : const int16_t *sfm_end, /* i : Subband end indices */
193 : const int16_t last_sfm, /* i : Last coded band */
194 : const int16_t *R, /* i : Per-band bit allocation */
195 : float *CodeBook, /* o : Noise-fill codebook */
196 : float *CodeBook_mod /* o : Densified noise-fill codebook */
197 : )
198 : {
199 : int16_t sfm_base;
200 : int16_t sfm;
201 : int16_t E_cb_vec;
202 : int16_t i, j;
203 : int16_t cb_size;
204 :
205 : /* Build codebook */
206 20838 : cb_size = 0;
207 626151 : for ( sfm = 0; sfm <= last_sfm; sfm++ )
208 : {
209 605313 : if ( R[sfm] != 0 )
210 : {
211 449634 : if ( flag_32K_env_ho )
212 : {
213 : /* Build compressed (+/- 1) noise-fill codebook */
214 23661 : sfm_base = sfm_start[sfm];
215 63723 : for ( i = 0; i < sfmsize[sfm] / 8; i++ )
216 : {
217 40062 : E_cb_vec = 0;
218 360558 : for ( j = sfm_base + i * 8; j < sfm_base + ( i + 1 ) * 8; j++ )
219 : {
220 320496 : if ( coeff[j] > 0.0f )
221 : {
222 56436 : CodeBook_mod[cb_size] = 1.0f;
223 56436 : E_cb_vec++;
224 : }
225 264060 : else if ( coeff[j] < 0.0f )
226 : {
227 56973 : CodeBook_mod[cb_size] = -1.0f;
228 56973 : E_cb_vec++;
229 : }
230 : else
231 : {
232 207087 : CodeBook_mod[cb_size] = 0.0f;
233 : }
234 320496 : cb_size++;
235 : }
236 :
237 40062 : if ( E_cb_vec < 2 )
238 : {
239 9411 : cb_size -= 8;
240 : }
241 : }
242 : }
243 : else
244 : {
245 5901429 : for ( j = sfm_start[sfm]; j < sfm_end[sfm]; j++ )
246 : {
247 5475456 : CodeBook[cb_size] = coeff[j];
248 5475456 : cb_size++;
249 : }
250 : }
251 : }
252 : }
253 :
254 20838 : if ( flag_32K_env_ho )
255 : {
256 246567 : for ( j = 0; j < cb_size; j++ )
257 : {
258 245208 : if ( CodeBook_mod[j] != 0.0f )
259 : {
260 : /* Densify codebook */
261 107088 : CodeBook[j] = sign( CodeBook_mod[j] ) * ( CodeBook_mod[j] * CodeBook_mod[j] + CodeBook_mod[cb_size - j - 1] * CodeBook_mod[cb_size - j - 1] );
262 : }
263 : else
264 : {
265 138120 : CodeBook[j] = CodeBook_mod[cb_size - j - 1];
266 : }
267 : }
268 : }
269 :
270 20838 : return cb_size;
271 : }
272 :
273 : /*--------------------------------------------------------------------------*
274 : * find_last_band()
275 : *
276 : * Find the last band which has bits allocated
277 : *--------------------------------------------------------------------------*/
278 :
279 : /*! r: index of last band */
280 25755 : int16_t find_last_band(
281 : const int16_t *bitalloc, /* i : bit allocation */
282 : const int16_t nb_sfm /* i : number of possibly coded bands */
283 : )
284 : {
285 : int16_t sfm, core_sfm;
286 :
287 25755 : core_sfm = nb_sfm - 1;
288 :
289 372341 : for ( sfm = nb_sfm - 1; sfm >= 0; sfm-- )
290 : {
291 372341 : if ( bitalloc[sfm] != 0 )
292 : {
293 25755 : core_sfm = sfm;
294 25755 : break;
295 : }
296 : }
297 :
298 25755 : return core_sfm;
299 : }
300 :
301 : /*--------------------------------------------------------------------------*
302 : * apply_noisefill_HQ()
303 : *
304 : * Inject noise in non-coded bands
305 : *--------------------------------------------------------------------------*/
306 :
307 20838 : void apply_noisefill_HQ(
308 : const int16_t *R, /* i : bit allocation */
309 : const int16_t length, /* i : input frame length */
310 : const int16_t flag_32K_env_ho, /* i : envelope stability hangover flag */
311 : const int32_t core_brate, /* i : core bitrate */
312 : const int16_t last_sfm, /* i : last coded subband */
313 : const float *CodeBook, /* i : Noise-fill codebook */
314 : const float *CodeBook_mod, /* i : Densified noise-fill codebook */
315 : const int16_t cb_size, /* i : Codebook length */
316 : const int16_t *sfm_start, /* i : Subband start coefficient */
317 : const int16_t *sfm_end, /* i : Subband end coefficient */
318 : const int16_t *sfmsize, /* i : Subband band width */
319 : float *coeff /* i/o: coded/noisefilled spectrum */
320 : )
321 : {
322 : int16_t sfm;
323 : int16_t cb_pos;
324 : float E_cb_vec;
325 : float E_corr;
326 : float cb_buff[64];
327 : int16_t i, j;
328 :
329 20838 : if ( length >= L_FRAME32k || core_brate != HQ_32k )
330 : {
331 : /* Read from codebook */
332 20838 : cb_pos = 0;
333 :
334 626151 : for ( sfm = 0; sfm <= last_sfm; sfm++ )
335 : {
336 605313 : if ( R[sfm] == 0 )
337 : {
338 155679 : if ( flag_32K_env_ho )
339 : {
340 15642 : E_cb_vec = 0.0f;
341 15642 : if ( sfm < 20 )
342 : {
343 114489 : for ( i = 0; i < sfmsize[sfm]; i++ )
344 : {
345 103536 : cb_buff[i] = CodeBook_mod[cb_pos];
346 103536 : E_cb_vec += cb_buff[i] * cb_buff[i];
347 103536 : cb_pos++;
348 103536 : if ( cb_pos >= cb_size )
349 : {
350 186 : cb_pos = 0;
351 : }
352 : }
353 : }
354 : else
355 : {
356 104553 : for ( i = 0; i < sfmsize[sfm]; i++ )
357 : {
358 99864 : cb_buff[i] = CodeBook[cb_pos];
359 99864 : E_cb_vec += cb_buff[i] * cb_buff[i];
360 99864 : cb_pos++;
361 99864 : if ( cb_pos >= cb_size )
362 : {
363 663 : cb_pos = 0;
364 : }
365 : }
366 : }
367 :
368 15642 : E_corr = E_cb_vec / ( (float) sfmsize[sfm] );
369 15642 : E_corr = 1.0f / (float) sqrt( E_corr );
370 :
371 219042 : for ( j = sfm_start[sfm]; j < sfm_end[sfm]; j++ )
372 : {
373 203400 : coeff[j] = cb_buff[j - sfm_start[sfm]] * E_corr;
374 : }
375 : }
376 : else
377 : {
378 2359641 : for ( j = sfm_start[sfm]; j < sfm_end[sfm]; j++ )
379 : {
380 2219604 : coeff[j] = CodeBook[cb_pos];
381 2219604 : cb_pos++;
382 2219604 : cb_pos = ( cb_pos >= cb_size ) ? 0 : cb_pos;
383 : }
384 : }
385 : }
386 : }
387 : }
388 :
389 20838 : return;
390 : }
391 :
392 :
393 : /*--------------------------------------------------------------------------*
394 : * harm_bwe_fine()
395 : *
396 : * Prepare harmonic BWE fine structure
397 : *--------------------------------------------------------------------------*/
398 :
399 2352 : void harm_bwe_fine(
400 : const int16_t *R, /* i : bit allocation */
401 : const int16_t last_sfm, /* i : last coded subband */
402 : const int16_t high_sfm, /* i : higher transition band to BWE */
403 : const int16_t num_sfm, /* i : total number of bands */
404 : const int16_t *norm, /* i : quantization indices for norms */
405 : const int16_t *sfm_start, /* i : Subband start coefficient */
406 : const int16_t *sfm_end, /* i : Subband end coefficient */
407 : int16_t *prev_L_swb_norm, /* i/o: last normalize length */
408 : float *coeff, /* i/o: coded/noisefilled normalized spectrum */
409 : float *coeff_out, /* o : coded/noisefilled spectrum */
410 : float *coeff_fine /* o : BWE fine structure */
411 : )
412 : {
413 : int16_t sfm;
414 : int16_t i;
415 : float normq;
416 : float SWB_signal[L_HARMONIC_EXC];
417 : float envelope[L_HARMONIC_EXC];
418 : float *src, *dst, *end;
419 :
420 2352 : int16_t norm_width = 64;
421 :
422 : /* shape the spectrum */
423 62364 : for ( sfm = 0; sfm <= last_sfm; sfm++ )
424 : {
425 60012 : if ( R[sfm] != 0 )
426 : {
427 48246 : normq = dicn[norm[sfm]];
428 720990 : for ( i = sfm_start[sfm]; i < sfm_end[sfm]; i++ )
429 : {
430 672744 : coeff_out[i] = coeff[i] * normq;
431 : }
432 : }
433 : else
434 : {
435 161214 : for ( i = sfm_start[sfm]; i < sfm_end[sfm]; i++ )
436 : {
437 149448 : coeff_out[i] = 0.0f;
438 : }
439 : }
440 : }
441 :
442 : /* excitation replication */
443 2352 : mvr2r( coeff_out, SWB_signal, L_HARMONIC_EXC );
444 2352 : calc_normal_length( HQ_CORE, coeff_out, HQ_HARMONIC, -1, &norm_width, prev_L_swb_norm );
445 2352 : hq_swb_harmonic_calc_norm_envelop( SWB_signal, envelope, norm_width, L_HARMONIC_EXC );
446 :
447 : /* Normalize with envelope */
448 477456 : for ( i = 0; i < L_HARMONIC_EXC; i++ )
449 : {
450 475104 : SWB_signal[i] = SWB_signal[i] / envelope[i];
451 : }
452 :
453 2352 : dst = coeff_fine + sfm_end[last_sfm];
454 2352 : end = coeff_fine + sfm_end[num_sfm - 1];
455 :
456 2352 : if ( ( sfm_end[last_sfm] - sfm_end[high_sfm] ) <= L_HARMONIC_EXC - START_EXC )
457 : {
458 1770 : src = SWB_signal + START_EXC + ( sfm_end[last_sfm] - sfm_end[high_sfm] );
459 : }
460 : else
461 : {
462 582 : src = SWB_signal + L_HARMONIC_EXC - 1;
463 : }
464 :
465 7785 : while ( dst < end )
466 : {
467 496752 : while ( dst < end && src < &SWB_signal[L_HARMONIC_EXC] )
468 : {
469 491319 : *dst++ = *src++;
470 : }
471 5433 : src--;
472 :
473 519282 : while ( dst < end && src >= &SWB_signal[START_EXC] )
474 : {
475 513849 : *dst++ = *src--;
476 : }
477 5433 : src++;
478 : }
479 :
480 2352 : return;
481 : }
482 :
483 : /*--------------------------------------------------------------------------*
484 : * hvq_bwe_fine()
485 : *
486 : * Prepare HVQ BWE fine structure
487 : *--------------------------------------------------------------------------*/
488 :
489 3867 : void hvq_bwe_fine(
490 : const int16_t last_sfm, /* i : last coded subband */
491 : const int16_t num_sfm, /* i : total number of bands */
492 : const int16_t *sfm_end, /* i : Subband end coefficient */
493 : const int16_t *peak_idx, /* i : Peak index */
494 : const int16_t Npeaks, /* i : Number of peaks */
495 : int16_t *peak_pos, /* i/o: Peak positions */
496 : int16_t *prev_L_swb_norm, /* i/o: last normalize length */
497 : float *coeff, /* i/o: coded/noisefilled normalized spectrum */
498 : int16_t *bwe_peaks, /* o : Positions of peaks in BWE */
499 : float *coeff_fine /* o : HVQ BWE fine structure */
500 : )
501 : {
502 : int16_t i, j;
503 : float SWB_signal[L_HARMONIC_EXC];
504 : float envelope[L_HARMONIC_EXC];
505 : float *src, *dst, *end;
506 : int16_t *peak_dst, *peak_src;
507 3867 : int16_t norm_width = 64;
508 :
509 : /* excitation replication */
510 3867 : mvr2r( coeff, SWB_signal, L_HARMONIC_EXC );
511 3867 : calc_normal_length( HQ_CORE, coeff, HQ_HVQ, -1, &norm_width, prev_L_swb_norm );
512 :
513 3867 : hq_swb_harmonic_calc_norm_envelop( SWB_signal, envelope, norm_width, L_HARMONIC_EXC );
514 :
515 : /* Normalize with envelope */
516 785001 : for ( i = 0; i < L_HARMONIC_EXC; i++ )
517 : {
518 781134 : SWB_signal[i] = SWB_signal[i] / envelope[i];
519 : }
520 :
521 3867 : dst = coeff_fine;
522 3867 : end = coeff_fine + sfm_end[num_sfm - 1] - sfm_end[last_sfm];
523 :
524 3867 : src = SWB_signal + START_EXC;
525 3867 : peak_src = peak_pos + START_EXC;
526 :
527 70191 : for ( i = 0; i < Npeaks; i++ )
528 : {
529 66324 : if ( peak_idx[i] < L_HARMONIC_EXC )
530 : {
531 51606 : peak_pos[peak_idx[i]] = 1;
532 : }
533 : }
534 :
535 3867 : i = L_HARMONIC_EXC - 1;
536 68331 : while ( i-- > 0 )
537 : {
538 68331 : if ( peak_pos[i] == 1 )
539 : {
540 3867 : break;
541 : }
542 : }
543 :
544 3867 : if ( i < 180 )
545 : {
546 843 : i = 180;
547 : }
548 :
549 57906 : for ( j = L_HARMONIC_EXC - 1; j > i + 1; j-- )
550 : {
551 54039 : SWB_signal[j] = 0.0f;
552 : }
553 :
554 3867 : peak_dst = bwe_peaks + sfm_end[last_sfm];
555 13305 : while ( dst < end )
556 : {
557 1114140 : while ( dst < end && src < &SWB_signal[L_HARMONIC_EXC] )
558 : {
559 1104702 : *dst++ = *src++;
560 1104702 : *peak_dst++ = *peak_src++;
561 : }
562 9438 : peak_src--;
563 9438 : src--;
564 :
565 888000 : while ( dst < end && src >= &SWB_signal[START_EXC] )
566 : {
567 878562 : *dst++ = *src--;
568 878562 : *peak_dst++ = *peak_src--;
569 : }
570 9438 : peak_src++;
571 9438 : src++;
572 : }
573 :
574 3867 : return;
575 : }
576 :
577 : /*--------------------------------------------------------------------------*
578 : * hq_fold_bwe()
579 : *
580 : * HQ mode folding BWE
581 : *--------------------------------------------------------------------------*/
582 :
583 7866 : void hq_fold_bwe(
584 : const int16_t last_sfm, /* i : last coded subband */
585 : const int16_t *sfm_end, /* i : Subband end coefficient */
586 : const int16_t num_sfm, /* i : Number of subbands */
587 : float *coeff /* i/o: coded/noisefilled normalized spectrum */
588 : )
589 : {
590 : int16_t low_coeff;
591 : int16_t first_coeff;
592 : float *src, *dst, *end;
593 :
594 : /* Find replication range for BWE */
595 7866 : low_coeff = sfm_end[last_sfm] >> 1;
596 7866 : src = coeff + sfm_end[last_sfm] - 1;
597 :
598 7866 : first_coeff = sfm_end[last_sfm];
599 7866 : dst = coeff + sfm_end[last_sfm];
600 7866 : end = coeff + sfm_end[num_sfm - 1];
601 :
602 : /* Generate BWE with spectral folding */
603 17694 : while ( dst < end )
604 : {
605 1357551 : while ( dst < end && src >= &coeff[low_coeff] )
606 : {
607 1347723 : *dst++ = *src--;
608 : }
609 :
610 9828 : src++;
611 :
612 726717 : while ( dst < end && src < &coeff[first_coeff] )
613 : {
614 716889 : *dst++ = *src++;
615 : }
616 : }
617 :
618 7866 : return;
619 : }
620 :
621 : /*--------------------------------------------------------------------------*
622 : * apply_nf_gain()
623 : *
624 : * Apply noise fill gain
625 : *--------------------------------------------------------------------------*/
626 :
627 20580 : void apply_nf_gain(
628 : const int16_t nf_idx, /* i : noise fill gain index */
629 : const int16_t last_sfm, /* i : last coded subband */
630 : const int16_t *R, /* i : bit allocation */
631 : const int16_t *sfm_start, /* i : Subband start coefficient */
632 : const int16_t *sfm_end, /* i : Subband end coefficient */
633 : float *coeff /* i/o: coded/noisefilled normalized spectrum */
634 : )
635 : {
636 : int16_t sfm;
637 : int16_t j;
638 : float nf_scale;
639 :
640 20580 : nf_scale = 1.0f / ( 1 << nf_idx );
641 619476 : for ( sfm = 0; sfm <= last_sfm; sfm++ )
642 : {
643 598896 : if ( R[sfm] == 0 )
644 : {
645 2566821 : for ( j = sfm_start[sfm]; j < sfm_end[sfm]; j++ )
646 : {
647 : /* Scale NoiseFill */
648 2411964 : coeff[j] = coeff[j] * nf_scale;
649 : }
650 : }
651 : }
652 :
653 20580 : return;
654 : }
655 :
656 : /*--------------------------------------------------------------------------*
657 : * hq_generic_fine()
658 : *
659 : * Prepare HQ SWB BWE fine structure
660 : *--------------------------------------------------------------------------*/
661 :
662 9003 : void hq_generic_fine(
663 : float *coeff, /* i : coded/noisefilled normalized spectrum */
664 : const int16_t last_sfm, /* i : Last coded band */
665 : const int16_t *sfm_start, /* i : Subband start coefficient */
666 : const int16_t *sfm_end, /* i : Subband end coefficient */
667 : int16_t *bwe_seed, /* i/o: random seed for generating BWE input */
668 : float *coeff_out1 /* o : HQ SWB BWE input */
669 : )
670 : {
671 : int16_t sfm;
672 : int16_t i;
673 : float multi;
674 :
675 9003 : multi = 1.0f;
676 252117 : for ( sfm = 0; sfm <= last_sfm; sfm++ )
677 : {
678 3196890 : for ( i = sfm_start[sfm]; i < sfm_end[sfm]; i++ )
679 : {
680 2953776 : if ( coeff[i] == 0.f )
681 : {
682 1681359 : coeff_out1[i] = (float) multi * ( own_random( bwe_seed ) > 0 ? 1.0f : -1.0f ) * 0.5f;
683 : }
684 : else
685 : {
686 1272417 : coeff_out1[i] = coeff[i];
687 : }
688 : }
689 : }
690 :
691 9003 : return;
692 : }
693 :
694 : /*--------------------------------------------------------------------------*
695 : * harm_bwe()
696 : *
697 : * HQ Harmonic BWE
698 : *--------------------------------------------------------------------------*/
699 :
700 2352 : void harm_bwe(
701 : const float *coeff_fine, /* i : fine structure for BWE */
702 : const float *coeff, /* i : coded/noisefilled normalized spectrum */
703 : const int16_t num_sfm, /* i : Number of subbands */
704 : const int16_t *sfm_start, /* i : Subband start coefficient */
705 : const int16_t *sfm_end, /* i : Subband end coefficient */
706 : const int16_t last_sfm, /* i : last coded subband */
707 : const int16_t high_sfm, /* i : higher transition band to BWE */
708 : const int16_t *R, /* i : bit allocation */
709 : const int16_t prev_hq_mode, /* i : previous hq mode */
710 : int16_t *norm, /* i/o: quantization indices for norms */
711 : float *noise_level, /* i/o: noise levels for harmonic modes */
712 : float *prev_noise_level, /* i/o: noise factor in previous frame */
713 : int16_t *bwe_seed, /* i/o: random seed for generating BWE input */
714 : float *coeff_out, /* o : coded/noisefilled spectrum */
715 : const int16_t element_mode /* i : element mode */
716 : )
717 : {
718 : int16_t i, j;
719 : int16_t sfm;
720 : float normq;
721 : float norm_adj;
722 : float E_L;
723 :
724 2352 : float alfa = 0.5f;
725 : float alpha, beta;
726 : int16_t idx;
727 : float fac;
728 : float *src, *dst;
729 :
730 62364 : for ( sfm = 0; sfm <= last_sfm; sfm++ )
731 : {
732 60012 : if ( R[sfm] == 0 )
733 : {
734 11766 : normq = dicn[norm[sfm]];
735 161214 : for ( i = sfm_start[sfm]; i < sfm_end[sfm]; i++ )
736 : {
737 149448 : coeff_out[i] = coeff[i] * normq;
738 : }
739 : }
740 : }
741 2352 : noise_level[1] = noise_level[0];
742 :
743 : /* shaping the BWE spectrum further by envelopes and noise factors */
744 2352 : noise_level[0] = 0.9f * prev_noise_level[0] + 0.1f * noise_level[0];
745 2352 : noise_level[1] = 0.9f * prev_noise_level[1] + 0.1f * noise_level[1];
746 :
747 2352 : if ( prev_hq_mode == HQ_NORMAL || prev_hq_mode == HQ_GEN_SWB )
748 : {
749 456 : if ( noise_level[0] < 0.25f )
750 : {
751 456 : noise_level[0] *= 4.0f;
752 : }
753 :
754 456 : if ( noise_level[1] < 0.25f )
755 : {
756 456 : noise_level[1] *= 4.0f;
757 : }
758 : }
759 :
760 2352 : E_L = EPSILON;
761 19278 : for ( i = last_sfm + 1; i < num_sfm; i++ )
762 : {
763 16926 : E_L = EPSILON;
764 1022094 : for ( j = sfm_start[i]; j < sfm_end[i]; j++ )
765 : {
766 1005168 : E_L += coeff_fine[j] * coeff_fine[j];
767 : }
768 16926 : E_L = (float) sqrt( ( sfm_end[i] - sfm_start[i] ) / E_L );
769 :
770 16926 : normq = dicn[norm[i]];
771 16926 : norm_adj = normq * E_L;
772 16926 : alfa = ( i > 27 ) ? noise_level[1] : noise_level[0];
773 16926 : beta = (float) sqrt( 1.0f - alfa );
774 16926 : if ( element_mode > EVS_MONO )
775 : {
776 16407 : alpha = (float) sqrt( alfa );
777 : }
778 : else
779 : {
780 519 : alpha = (float) sqrt( alfa ) * 0.5f;
781 : }
782 :
783 1022094 : for ( sfm = sfm_start[i]; sfm < sfm_end[i]; sfm++ )
784 : {
785 1005168 : coeff_out[sfm] = ( beta * coeff_fine[sfm] * norm_adj + alpha * own_random( bwe_seed ) / PCM16_TO_FLT_FAC * normq );
786 : }
787 : }
788 :
789 2352 : prev_noise_level[0] = noise_level[0];
790 2352 : prev_noise_level[1] = noise_level[1];
791 2352 : idx = 16;
792 2352 : src = &coeff_out[sfm_end[high_sfm] + L_HARMONIC_EXC - START_EXC];
793 2352 : dst = src - 1;
794 :
795 39984 : for ( i = 0; i < idx; i++ )
796 : {
797 37632 : fac = i / ( 2.0f * idx );
798 37632 : *src++ *= 0.5f + fac;
799 37632 : *dst-- *= 0.5f + fac;
800 : }
801 2352 : if ( num_sfm == 33 )
802 : {
803 2013 : set_f( &coeff_out[800], 0, 160 );
804 : }
805 2352 : return;
806 : }
807 :
808 : /*--------------------------------------------------------------------------*
809 : * HVQ_bwe()
810 : *
811 : * HQ HVQ BWE
812 : *--------------------------------------------------------------------------*/
813 :
814 3867 : void hvq_bwe(
815 : const float *coeff, /* i : coded/noisefilled spectrum */
816 : const float *coeff_fine, /* i : BWE fine structure */
817 : const int16_t *sfm_start, /* i : Subband start coefficient */
818 : const int16_t *sfm_end, /* i : Subband end coefficient */
819 : const int16_t *sfm_len, /* i : Subband length */
820 : const int16_t last_sfm, /* i : last coded subband */
821 : const int16_t prev_hq_mode, /* i : previous hq mode */
822 : const int16_t *bwe_peaks, /* i : HVQ bwe peaks */
823 : const int16_t bin_th, /* i : HVQ transition bin */
824 : const int16_t num_sfm, /* i : Number of bands */
825 : const int32_t core_brate, /* i : Core bitrate */
826 : const int16_t *R, /* i : Bit allocation */
827 : int16_t *norm, /* i/o: quantization indices for norms */
828 : float *noise_level, /* i/o: noise levels for harmonic modes */
829 : float *prev_noise_level, /* i/o: noise factor in previous frame */
830 : int16_t *bwe_seed, /* i/o: random seed for generating BWE input */
831 : float *coeff_out /* o : coded/noisefilled spectrum */
832 : )
833 : {
834 : int16_t i, j;
835 : int16_t N;
836 : float normq;
837 : float E_L;
838 :
839 3867 : int16_t bwe_noise_th = 0;
840 : int16_t peak_band, low, high, sel_norm;
841 : int16_t norm_ind;
842 3867 : float tmp_norm = 0;
843 : int16_t idx;
844 : float fac;
845 : float *src, *dst;
846 : int16_t istart, iend;
847 3867 : int16_t offset = sfm_end[last_sfm];
848 :
849 3867 : mvr2r( coeff, coeff_out, L_FRAME48k );
850 :
851 3867 : bwe_noise_th = bin_th + ( sfm_end[num_sfm - 1] - bin_th ) / HVQ_BWE_NOISE_BANDS;
852 3867 : logqnorm( &coeff_out[sfm_start[last_sfm]], &norm[last_sfm], 40, sfm_len[last_sfm], thren_HQ );
853 :
854 : /* shaping the BWE spectrum further by envelopes and noise factors */
855 3867 : noise_level[0] = 0.9f * prev_noise_level[0] + 0.1f * noise_level[0];
856 3867 : noise_level[1] = 0.9f * prev_noise_level[1] + 0.1f * noise_level[1];
857 :
858 3867 : if ( prev_hq_mode == HQ_NORMAL || prev_hq_mode == HQ_GEN_SWB )
859 : {
860 369 : if ( noise_level[0] < 0.25f )
861 : {
862 369 : noise_level[0] *= 4.0f;
863 : }
864 :
865 369 : if ( noise_level[1] < 0.25f )
866 : {
867 369 : noise_level[1] *= 4.0f;
868 : }
869 : }
870 :
871 3867 : norm_ind = last_sfm + 1;
872 :
873 3867 : if ( core_brate < HQ_BWE_CROSSOVER_BRATE )
874 : {
875 2229 : peak_band = 0;
876 2229 : E_L = EPSILON;
877 144885 : for ( i = sfm_start[norm_ind]; i < sfm_end[norm_ind + 1]; i++ )
878 : {
879 142656 : if ( bwe_peaks[i] )
880 : {
881 6189 : peak_band = 1;
882 : }
883 142656 : E_L += coeff_fine[i - offset] * coeff_fine[i - offset];
884 : }
885 2229 : E_L = (float) sqrt( ( sfm_end[norm_ind + 1] - sfm_start[norm_ind] ) / E_L );
886 :
887 2229 : normq = 0.1f * dicn[norm[norm_ind - 1]] + 0.8f * dicn[norm[norm_ind]] + 0.1f * dicn[norm[norm_ind + 1]];
888 2229 : tmp_norm = 0.1f * dicn[norm[norm_ind]] + 0.8f * dicn[norm[norm_ind + 1]] + 0.1f * dicn[norm[norm_ind + 2]];
889 :
890 2229 : istart = sfm_start[norm_ind];
891 2229 : iend = istart + sfm_len[norm_ind] / 2;
892 37893 : for ( i = istart; i < iend; i++ )
893 : {
894 35664 : coeff_out[i] = ( ( 1.0f - noise_level[i / bwe_noise_th] ) * coeff_fine[i - offset] * E_L + noise_level[i / bwe_noise_th] * own_random( bwe_seed ) / PCM16_TO_FLT_FAC ) * normq;
895 : }
896 :
897 2229 : j = 0;
898 2229 : N = sfm_len[norm_ind] / 2 + sfm_len[norm_ind + 1] / 2 - 1;
899 2229 : istart = iend;
900 2229 : iend = sfm_start[norm_ind + 1] + sfm_len[norm_ind + 1] / 2;
901 73557 : for ( i = istart; i < iend; i++ )
902 : {
903 71328 : coeff_out[i] = ( (float) ( N - j ) / N * normq + (float) j / N * tmp_norm ) * ( ( 1.0f - noise_level[i / bwe_noise_th] ) * coeff_fine[i - offset] * E_L + noise_level[i / bwe_noise_th] * own_random( bwe_seed ) / PCM16_TO_FLT_FAC );
904 71328 : j++;
905 : }
906 :
907 2229 : istart = iend;
908 2229 : iend = sfm_end[norm_ind + 1];
909 37893 : for ( i = istart; i < iend; i++ )
910 : {
911 35664 : coeff_out[i] = ( ( 1.0f - noise_level[i / bwe_noise_th] ) * coeff_fine[i - offset] * E_L + noise_level[i / bwe_noise_th] * own_random( bwe_seed ) / PCM16_TO_FLT_FAC ) * tmp_norm;
912 : }
913 :
914 2229 : norm_ind += 2;
915 : }
916 :
917 35946 : for ( ; norm_ind < num_sfm; norm_ind++ )
918 : {
919 32079 : if ( R[norm_ind] == 0 )
920 : {
921 31254 : peak_band = 0;
922 31254 : E_L = EPSILON;
923 :
924 529380 : for ( i = sfm_start[norm_ind]; i < sfm_end[norm_ind]; i++ )
925 : {
926 527634 : if ( bwe_peaks[i] )
927 : {
928 29508 : peak_band = 1;
929 29508 : break;
930 : }
931 : }
932 :
933 31254 : istart = sfm_start[norm_ind];
934 31254 : iend = sfm_end[norm_ind];
935 :
936 31254 : if ( peak_band == 1 && norm_ind > last_sfm + 1 && norm_ind < num_sfm - 1 )
937 : {
938 24228 : istart -= sfm_len[norm_ind - 1] / 2;
939 24228 : iend += sfm_len[norm_ind + 1] / 2;
940 : }
941 :
942 3167838 : for ( i = istart; i < iend; i++ )
943 : {
944 3136584 : E_L += coeff_fine[i - offset] * coeff_fine[i - offset];
945 : }
946 31254 : E_L = (float) sqrt( ( iend - istart ) / E_L );
947 :
948 31254 : if ( peak_band )
949 : {
950 29508 : if ( norm_ind + 1 > num_sfm - 1 )
951 : {
952 3840 : normq = 0.15f * dicn[norm[norm_ind - 1]] + 0.85f * dicn[norm[norm_ind]];
953 : }
954 : else
955 : {
956 25668 : normq = 0.1f * dicn[norm[norm_ind - 1]] + 0.8f * dicn[norm[norm_ind]] + 0.1f * dicn[norm[norm_ind + 1]];
957 : }
958 : }
959 : else
960 : {
961 1746 : low = norm_ind;
962 1746 : high = min( norm_ind + 1, num_sfm - 1 );
963 1746 : sel_norm = norm[norm_ind - 1];
964 5211 : for ( j = low; j <= high; j++ )
965 : {
966 3465 : if ( norm[j] > sel_norm )
967 : {
968 1770 : sel_norm = norm[j];
969 : }
970 : }
971 1746 : normq = dicn[sel_norm];
972 : }
973 :
974 1834422 : for ( i = sfm_start[norm_ind]; i < sfm_end[norm_ind]; i++ )
975 : {
976 1803168 : coeff_out[i] = ( ( 1.0f - noise_level[i / bwe_noise_th] ) * coeff_fine[i - offset] * E_L + noise_level[i / bwe_noise_th] * own_random( bwe_seed ) / PCM16_TO_FLT_FAC ) * normq;
977 : }
978 : }
979 : else /* R[norm_ind] > 0 */
980 : {
981 38265 : for ( i = sfm_start[norm_ind]; i < sfm_end[norm_ind]; i++ )
982 : {
983 37440 : coeff_out[i] = coeff[i]; /* Scaling already applied */
984 : }
985 : }
986 : }
987 :
988 3867 : prev_noise_level[0] = noise_level[0];
989 3867 : prev_noise_level[1] = noise_level[1];
990 3867 : idx = 16;
991 3867 : src = &coeff_out[sfm_end[last_sfm] + L_HARMONIC_EXC - START_EXC];
992 3867 : dst = src - 1;
993 :
994 65739 : for ( i = 0; i < idx; i++ )
995 : {
996 61872 : fac = i / ( 2.0f * idx );
997 61872 : *src++ *= 0.5f + fac;
998 61872 : *dst-- *= 0.5f + fac;
999 : }
1000 :
1001 3867 : return;
1002 : }
1003 :
1004 :
1005 : /*-------------------------------------------------------------------*
1006 : * hvq_concat_bands()
1007 : *
1008 : * Compute the band limits for concatenated bands for PVQ target signal in HVQ
1009 : *--------------------------------------------------------------------------*/
1010 :
1011 5194 : void hvq_concat_bands(
1012 : const int16_t pvq_bands, /* i : Number of bands in concatenated PVQ target */
1013 : const int16_t *sel_bnds, /* i : Array of selected high bands */
1014 : const int16_t n_sel_bnds, /* i : Number of selected high bands */
1015 : int16_t *hvq_band_start, /* o : Band start indices */
1016 : int16_t *hvq_band_width, /* o : Band widths */
1017 : int16_t *hvq_band_end /* o : Band end indices */
1018 : )
1019 : {
1020 : int16_t k;
1021 : int16_t s;
1022 :
1023 5194 : s = 0;
1024 :
1025 15373 : for ( k = 0; k < pvq_bands; k++ )
1026 : {
1027 :
1028 10179 : if ( k >= pvq_bands - n_sel_bnds )
1029 : {
1030 1405 : hvq_band_start[k] = hvq_band_end[k - 1];
1031 1405 : hvq_band_width[k] = band_len_harm[sel_bnds[s]];
1032 1405 : hvq_band_end[k] = hvq_band_end[k - 1] + band_len_harm[sel_bnds[s]];
1033 1405 : s++;
1034 : }
1035 : else
1036 : {
1037 8774 : hvq_band_start[k] = k * HVQ_PVQ_COEFS;
1038 8774 : hvq_band_width[k] = HVQ_PVQ_COEFS;
1039 8774 : hvq_band_end[k] = ( k + 1 ) * HVQ_PVQ_COEFS;
1040 : }
1041 : }
1042 :
1043 5194 : return;
1044 : }
1045 :
1046 :
1047 : /*--------------------------------------------------------------------------*
1048 : * map_hq_generic_fenv_norm()
1049 : *
1050 : * mapping high frequency envelope to high band norm
1051 : *--------------------------------------------------------------------------*/
1052 :
1053 12072 : void map_hq_generic_fenv_norm(
1054 : const int16_t hqswb_clas, /* i : signal classification flag */
1055 : const float *hq_generic_fenv, /* i : HQ GENERIC envelope */
1056 : int16_t *ynrm, /* o : high band norm indices */
1057 : int16_t *normqlg2, /* o : high band norm values */
1058 : const int16_t num_env_bands, /* i : Number coded envelope bands */
1059 : const int16_t nb_sfm, /* i : Number of envelope bands */
1060 : const int16_t hq_generic_offset /* i : Freq offset for HQ GENERIC */
1061 : )
1062 : {
1063 : float env_fl[17];
1064 : int16_t i, idx;
1065 :
1066 12072 : set_f( env_fl, 0, 17 );
1067 12072 : if ( hq_generic_offset == 144 )
1068 : {
1069 0 : env_fl[0] = hq_generic_fenv[1];
1070 0 : env_fl[1] = hq_generic_fenv[2] * 0.6640625f + hq_generic_fenv[3] * 0.3359375f;
1071 0 : env_fl[2] = hq_generic_fenv[3] * 0.6640625f + hq_generic_fenv[4] * 0.3359375f;
1072 0 : env_fl[3] = hq_generic_fenv[4] * 0.3359375f + hq_generic_fenv[5] * 0.6640625f;
1073 0 : env_fl[4] = hq_generic_fenv[5] * 0.3359375f + hq_generic_fenv[6] * 0.6640625f;
1074 0 : env_fl[5] = hq_generic_fenv[7];
1075 0 : env_fl[6] = hq_generic_fenv[8] * 0.75f + hq_generic_fenv[9] * 0.25f;
1076 0 : env_fl[7] = hq_generic_fenv[9] * 0.75f + hq_generic_fenv[10] * 0.25f;
1077 0 : env_fl[8] = hq_generic_fenv[10] * 0.25f + hq_generic_fenv[11] * 0.75f;
1078 : }
1079 : else
1080 : {
1081 12072 : env_fl[0] = hq_generic_fenv[0] * 0.3359375f + hq_generic_fenv[1] * 0.6640625f;
1082 12072 : env_fl[1] = hq_generic_fenv[1] * 0.3359375f + hq_generic_fenv[2] * 0.6640625f;
1083 12072 : env_fl[2] = hq_generic_fenv[3];
1084 12072 : env_fl[3] = hq_generic_fenv[4] * 0.6640625f + hq_generic_fenv[5] * 0.3359375f;
1085 12072 : env_fl[4] = hq_generic_fenv[5] * 0.6640625f + hq_generic_fenv[6] * 0.3359375f;
1086 12072 : env_fl[5] = hq_generic_fenv[6] * 0.3359375f + hq_generic_fenv[7] * 0.6640625f;
1087 12072 : env_fl[6] = hq_generic_fenv[7] * 0.3359375f + hq_generic_fenv[8] * 0.6640625f;
1088 12072 : env_fl[7] = hq_generic_fenv[8] * 0.3359375f + hq_generic_fenv[9] * 0.6640625f;
1089 12072 : env_fl[8] = hq_generic_fenv[9] * 0.3359375f + hq_generic_fenv[10] * 0.6640625f;
1090 12072 : env_fl[9] = hq_generic_fenv[10] * 0.25f + hq_generic_fenv[11] * 0.75f;
1091 12072 : env_fl[10] = hq_generic_fenv[12];
1092 12072 : env_fl[11] = hq_generic_fenv[13];
1093 : }
1094 :
1095 12072 : if ( hqswb_clas == HQ_GEN_FB )
1096 : {
1097 9074 : if ( hq_generic_offset == 144 )
1098 : {
1099 0 : env_fl[9] = hq_generic_fenv[12];
1100 0 : env_fl[10] = hq_generic_fenv[12] * 0.25f + hq_generic_fenv[13] * 0.75f;
1101 0 : env_fl[11] = hq_generic_fenv[13] * 0.5f + hq_generic_fenv[14] * 0.5f;
1102 0 : env_fl[12] = hq_generic_fenv[14];
1103 0 : env_fl[13] = hq_generic_fenv[14];
1104 : }
1105 : else
1106 : {
1107 9074 : env_fl[12] = hq_generic_fenv[14];
1108 9074 : env_fl[13] = hq_generic_fenv[14] * 0.25f + hq_generic_fenv[15] * 0.75f;
1109 9074 : env_fl[14] = hq_generic_fenv[15] * 0.5f + hq_generic_fenv[16] * 0.5f;
1110 9074 : env_fl[15] = hq_generic_fenv[16];
1111 9074 : env_fl[16] = hq_generic_fenv[16];
1112 : }
1113 : }
1114 :
1115 12072 : logqnorm_2( env_fl, 40, num_env_bands, nb_sfm, ynrm + num_env_bands, normqlg2 + num_env_bands, thren_HQ );
1116 :
1117 202306 : for ( i = num_env_bands; i < nb_sfm; ++i )
1118 : {
1119 190234 : idx = min( ynrm[i] + 10, 39 );
1120 190234 : normqlg2[i] = dicnlg2[idx];
1121 : }
1122 :
1123 12072 : return;
1124 : }
1125 :
1126 :
1127 : /*-------------------------------------------------------------------*
1128 : * update_rsubband()
1129 : *
1130 : * Update subband bit allocation
1131 : *--------------------------------------------------------------------------*/
1132 :
1133 12 : static void update_rsubband(
1134 : const int16_t nb_sfm, /* i : Number of sub-bands */
1135 : int16_t *Rsubband, /* o : Sub-bands bit allocation */
1136 : int16_t b_add_bits_denv /* i/o: Bits to redistribute */
1137 : )
1138 : {
1139 : int16_t i;
1140 :
1141 : /* updating bit allocation */
1142 24 : while ( b_add_bits_denv > 0 )
1143 : {
1144 12 : i = nb_sfm - 1;
1145 224 : while ( b_add_bits_denv > 0 && i >= 0 )
1146 : {
1147 212 : if ( Rsubband[i] > 24 )
1148 : {
1149 112 : Rsubband[i] -= 8;
1150 112 : b_add_bits_denv--;
1151 : }
1152 212 : i--;
1153 : }
1154 : }
1155 :
1156 12 : return;
1157 : }
1158 :
1159 :
1160 : /*-------------------------------------------------------------------*
1161 : * get_nor_delta_hf()
1162 : *
1163 : *
1164 : *--------------------------------------------------------------------------*/
1165 :
1166 : /*! r: Number of bits consumed for the delta coding */
1167 9003 : int16_t get_nor_delta_hf(
1168 : Decoder_State *st, /* i/o: Decoder state */
1169 : int16_t *ynrm, /* i/o: norm indices */
1170 : int16_t *Rsubband, /* i/o: sub-band bit allocation */
1171 : const int16_t num_env_bands, /* i : Number coded envelope bands */
1172 : const int16_t nb_sfm, /* i : Number of envelope bands */
1173 : const int16_t core_sfm ) /* i : index of the end band for core */
1174 : {
1175 : int16_t i;
1176 : int16_t delta, bitsforDelta, add_bits_denv;
1177 :
1178 9003 : add_bits_denv = 0;
1179 9003 : if ( core_sfm >= num_env_bands )
1180 : {
1181 9 : bitsforDelta = get_next_indice( st, 2 );
1182 9 : bitsforDelta += 2;
1183 9 : add_bits_denv += 2;
1184 :
1185 117 : for ( i = num_env_bands; i < nb_sfm; ++i )
1186 : {
1187 108 : if ( Rsubband[i] != 0 )
1188 : {
1189 33 : delta = get_next_indice( st, bitsforDelta );
1190 33 : ynrm[i] += delta - ( 1 << ( bitsforDelta - 1 ) );
1191 :
1192 : /* safety check in case of bit errors */
1193 33 : if ( ynrm[i] < 0 || ynrm[i] > 39 )
1194 : {
1195 0 : ynrm[i] = 39;
1196 0 : st->BER_detect = 1;
1197 : }
1198 33 : add_bits_denv += bitsforDelta;
1199 : }
1200 : }
1201 :
1202 9 : update_rsubband( nb_sfm, Rsubband, add_bits_denv );
1203 : }
1204 :
1205 9003 : return add_bits_denv;
1206 : }
1207 :
1208 :
1209 : /*-------------------------------------------------------------------*
1210 : * calc_nor_delta_hf()
1211 : *
1212 : *
1213 : *--------------------------------------------------------------------------*/
1214 :
1215 : /*! r: Number of bits consumed for the delta coding */
1216 3069 : int16_t calc_nor_delta_hf(
1217 : BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle */
1218 : const float *t_audio, /* i : transform-domain coefficients */
1219 : int16_t *ynrm, /* i/o: norm indices */
1220 : int16_t *Rsubband, /* i/o: sub-band bit allocation */
1221 : const int16_t num_env_bands, /* i : Number coded envelope bands */
1222 : const int16_t nb_sfm, /* i : Number of envelope bands */
1223 : const int16_t *sfmsize, /* i : band length */
1224 : const int16_t *sfm_start, /* i : Start index of bands */
1225 : const int16_t core_sfm /* i : index of the end band for core */
1226 : )
1227 : {
1228 : int16_t i;
1229 : int16_t ynrm_t[44], normqlg2_t[44];
1230 : int16_t delta, max_delta, min_delta, bitsforDelta, add_bits_denv;
1231 :
1232 3069 : max_delta = -100;
1233 3069 : calc_norm( t_audio, ynrm_t, normqlg2_t, 0, nb_sfm, sfmsize, sfm_start );
1234 3069 : add_bits_denv = 0;
1235 51442 : for ( i = num_env_bands; i < nb_sfm; ++i )
1236 : {
1237 48373 : if ( Rsubband[i] != 0 )
1238 : {
1239 11 : delta = ynrm_t[i] - ynrm[i];
1240 11 : if ( delta > 0 )
1241 : {
1242 0 : delta += 1;
1243 : }
1244 : else
1245 : {
1246 11 : delta = -delta;
1247 : }
1248 11 : if ( delta > max_delta )
1249 : {
1250 4 : max_delta = delta;
1251 : }
1252 : }
1253 : }
1254 3069 : if ( core_sfm >= num_env_bands )
1255 : {
1256 3 : if ( max_delta < 16 )
1257 : {
1258 3 : bitsforDelta = 2;
1259 3 : while ( max_delta >= 2 )
1260 : {
1261 0 : bitsforDelta++;
1262 0 : max_delta >>= 1;
1263 : }
1264 : }
1265 : else
1266 : {
1267 0 : bitsforDelta = 5;
1268 : }
1269 3 : max_delta = ( 1 << ( bitsforDelta - 1 ) ) - 1;
1270 3 : min_delta = ( max_delta + 1 ) * ( -1 );
1271 :
1272 : /* updating norm & storing delta norm */
1273 3 : add_bits_denv = 2;
1274 3 : push_indice( hBstr, IND_DELTA_ENV_HQ, bitsforDelta - 2, 2 );
1275 39 : for ( i = num_env_bands; i < nb_sfm; ++i )
1276 : {
1277 36 : if ( Rsubband[i] != 0 )
1278 : {
1279 11 : delta = ynrm_t[i] - ynrm[i];
1280 11 : if ( delta > max_delta )
1281 : {
1282 0 : delta = max_delta;
1283 : }
1284 11 : else if ( delta < min_delta )
1285 : {
1286 0 : delta = min_delta;
1287 : }
1288 11 : push_indice( hBstr, IND_DELTA_ENV_HQ, delta - min_delta, bitsforDelta );
1289 11 : ynrm[i] += delta;
1290 11 : add_bits_denv += bitsforDelta;
1291 : }
1292 : }
1293 :
1294 : /* updating bit allocation */
1295 3 : update_rsubband( nb_sfm, Rsubband, add_bits_denv );
1296 : }
1297 :
1298 3069 : return add_bits_denv;
1299 : }
1300 :
1301 : /*-------------------------------------------------------------------*
1302 : * hq_generic_bwe()
1303 : *
1304 : * HQ GENERIC BWE
1305 : *--------------------------------------------------------------------------*/
1306 :
1307 9003 : void hq_generic_bwe(
1308 : const int16_t HQ_mode, /* i : HQ mode */
1309 : float *coeff_out1, /* i/o: BWE input & temporary buffer */
1310 : const float *hq_generic_fenv, /* i : SWB frequency envelopes */
1311 : float *coeff_out, /* o : SWB signal in MDCT domain */
1312 : const int16_t hq_generic_offset, /* i : frequency offset for representing hq generic*/
1313 : int16_t *prev_L_swb_norm, /* i/o: last normalize length */
1314 : const int16_t hq_generic_exc_clas, /* i : hq generic hf excitation class */
1315 : const int16_t *sfm_end, /* i : End of bands */
1316 : const int16_t num_sfm, /* i : Number of bands */
1317 : const int16_t num_env_bands, /* i : Number of coded envelope bands */
1318 : const int16_t *R /* i : Bit allocation */
1319 : )
1320 : {
1321 : int16_t n_swb_overlap_offset, n_swb_overlap;
1322 : float hq_swb_overlap_buf[640];
1323 :
1324 9003 : n_swb_overlap_offset = swb_bwe_subband[0] + hq_generic_offset;
1325 :
1326 9003 : n_swb_overlap = sfm_end[num_env_bands - 1] - n_swb_overlap_offset;
1327 9003 : mvr2r( &coeff_out[n_swb_overlap_offset], hq_swb_overlap_buf, n_swb_overlap + sfm_end[num_sfm - 1] - sfm_end[num_env_bands - 1] );
1328 :
1329 9003 : hq_generic_hf_decoding( HQ_mode, coeff_out1, hq_generic_fenv, coeff_out, hq_generic_offset, prev_L_swb_norm, hq_generic_exc_clas, R );
1330 :
1331 9003 : overlap_hq_bwe( hq_swb_overlap_buf, coeff_out, n_swb_overlap_offset, n_swb_overlap, R, num_env_bands, num_sfm, sfm_end );
1332 :
1333 9003 : return;
1334 : }
1335 :
1336 :
1337 : /*--------------------------------------------------------------------------*
1338 : * hq_wb_nf_bwe()
1339 : *
1340 : * HQ WB noisefill and BWE
1341 : *--------------------------------------------------------------------------*/
1342 :
1343 1443 : void hq_wb_nf_bwe(
1344 : const float *coeff, /* i : coded/noisefilled normalized spectrum */
1345 : const int16_t is_transient, /* i : is transient flag */
1346 : const int16_t prev_bfi, /* i : previous bad frame indicator */
1347 : const float *normq_v, /* i : norms */
1348 : const int16_t num_sfm, /* i : Number of subbands */
1349 : const int16_t *sfm_start, /* i : Subband start coefficient */
1350 : const int16_t *sfm_end, /* i : Subband end coefficient */
1351 : const int16_t *sfmsize, /* i : Subband band width */
1352 : const int16_t last_sfm, /* i : last coded subband */
1353 : const int16_t *R, /* i : bit allocation */
1354 : const int16_t prev_is_transient, /* i : previous transient flag */
1355 : float *prev_normq, /* i/o: previous norms */
1356 : float *prev_env, /* i/o: previous noise envelopes */
1357 : int16_t *bwe_seed, /* i/o: random seed for generating BWE input */
1358 : float *prev_coeff_out, /* i/o: decoded spectrum in previous frame */
1359 : int16_t *prev_R, /* i/o: bit allocation info. in previous frame */
1360 : float *coeff_out /* o : coded/noisefilled spectrum */
1361 : )
1362 : {
1363 : int16_t i;
1364 : int16_t sfm;
1365 : int16_t total_bit;
1366 : int16_t num;
1367 :
1368 : float bitalloc_var;
1369 : float sharp;
1370 : float mean;
1371 : float peak;
1372 : float fabs_coeff_out;
1373 : float harm_para;
1374 1443 : float alfa = 0.5;
1375 : float env;
1376 : float step;
1377 : float min_coef;
1378 : float avrg_norm;
1379 : float prev_avrg_norm;
1380 :
1381 1443 : if ( is_transient == 0 )
1382 : {
1383 1410 : if ( prev_bfi == 1 )
1384 : {
1385 18 : mvr2r( normq_v, prev_normq, SFM_N_WB );
1386 : }
1387 :
1388 : /* the variance of bit allocation */
1389 1410 : total_bit = 0;
1390 1410 : bitalloc_var = 0.0f;
1391 24537 : for ( sfm = 8; sfm <= last_sfm; sfm++ )
1392 : {
1393 23127 : bitalloc_var += (float) abs( R[sfm] - R[sfm - 1] );
1394 23127 : total_bit += R[sfm];
1395 : }
1396 1410 : bitalloc_var = ( last_sfm > 8 && total_bit > 0 ) ? ( bitalloc_var / total_bit ) : 0;
1397 :
1398 : /* calculate the peak-average ratio of saturable subbands */
1399 1410 : num = 0;
1400 1410 : sharp = EPSILON;
1401 24537 : for ( sfm = last_sfm; sfm >= 8; sfm-- )
1402 : {
1403 23127 : if ( R[sfm] >= rat[sfm] * sfmsize[sfm] )
1404 : {
1405 7266 : peak = 0.0f;
1406 7266 : mean = EPSILON;
1407 83778 : for ( i = sfm_start[sfm]; i < sfm_end[sfm]; i++ )
1408 : {
1409 76512 : fabs_coeff_out = (float) fabs( coeff_out[i] );
1410 76512 : mean += fabs_coeff_out;
1411 76512 : if ( fabs_coeff_out > peak )
1412 : {
1413 12426 : peak = fabs_coeff_out;
1414 : }
1415 : }
1416 7266 : sharp += sfmsize[sfm] * peak / mean;
1417 7266 : num++;
1418 : }
1419 : }
1420 :
1421 1410 : sharp = ( num != 0 ) ? 2.0f * num / sharp : 1.0f;
1422 1410 : harm_para = sharp;
1423 1410 : if ( last_sfm == 0 )
1424 : {
1425 0 : step = 0;
1426 : }
1427 : else
1428 : {
1429 1410 : step = 5.0f * sharp / last_sfm;
1430 : }
1431 1410 : alfa = 2.5f;
1432 :
1433 : /* fill noise for the insaturable subbands */
1434 38070 : for ( sfm = 0; sfm < num_sfm; sfm++ )
1435 : {
1436 36660 : env = 0.0f;
1437 36660 : if ( R[sfm] != 0 && R[sfm] < 1.5f * sfmsize[sfm] )
1438 : {
1439 : /* calculate the energy of the undecoded coefficients */
1440 16689 : peak = 0.0f;
1441 16689 : min_coef = FLT_MAX;
1442 16689 : env = normq_v[sfm] * normq_v[sfm] * sfmsize[sfm];
1443 255801 : for ( i = sfm_start[sfm]; i < sfm_end[sfm]; i++ )
1444 : {
1445 239112 : fabs_coeff_out = (float) fabs( coeff_out[i] );
1446 239112 : if ( fabs_coeff_out < min_coef && coeff_out[i] != 0 )
1447 : {
1448 18930 : min_coef = fabs_coeff_out;
1449 : }
1450 239112 : if ( fabs_coeff_out > peak )
1451 : {
1452 19203 : peak = fabs_coeff_out;
1453 : }
1454 239112 : env -= coeff_out[i] * coeff_out[i];
1455 : }
1456 :
1457 16689 : if ( env > 0 )
1458 : {
1459 16689 : if ( sfm == 0 )
1460 : {
1461 483 : avrg_norm = normq_v[0] + normq_v[1] + normq_v[2];
1462 483 : prev_avrg_norm = prev_normq[0] + prev_normq[1] + prev_normq[2];
1463 : }
1464 16206 : else if ( sfm == 25 )
1465 : {
1466 903 : avrg_norm = normq_v[23] + normq_v[24] + normq_v[25];
1467 903 : prev_avrg_norm = prev_normq[23] + prev_normq[24] + prev_normq[25];
1468 : }
1469 : else
1470 : {
1471 15303 : avrg_norm = normq_v[sfm - 1] + normq_v[sfm] + normq_v[sfm + 1];
1472 15303 : prev_avrg_norm = prev_normq[sfm - 1] + prev_normq[sfm] + prev_normq[sfm + 1];
1473 : }
1474 :
1475 16689 : if ( bitalloc_var > 0.3f || 4.0f * normq_v[sfm] < peak )
1476 : {
1477 : /* calculate the noise magnitude of harmonic signal */
1478 16401 : env = (float) ( avrg_norm * harm_para * sqrt( env / sfmsize[sfm] ) / peak );
1479 : }
1480 : else
1481 : {
1482 : /* calculate the noise magnitude of normal signal */
1483 288 : env = sharp * (float) sqrt( env / sfmsize[sfm] );
1484 288 : if ( alfa * normq_v[sfm] < peak )
1485 : {
1486 3 : env *= env / peak;
1487 : }
1488 288 : sharp += step;
1489 : }
1490 16689 : if ( env > 0.5f * min_coef )
1491 : {
1492 7224 : env = 0.5f * min_coef;
1493 : }
1494 :
1495 16689 : if ( prev_bfi == 1 )
1496 : {
1497 195 : prev_env[sfm] = env;
1498 : }
1499 : /* smooth the noise magnitudes between inter-frame */
1500 16689 : if ( prev_avrg_norm > 0.5f * avrg_norm && prev_avrg_norm < 2.0f * avrg_norm && prev_is_transient == 0 )
1501 : {
1502 11826 : env = 0.5f * env + 0.5f * prev_env[sfm];
1503 : }
1504 :
1505 255801 : for ( i = sfm_start[sfm]; i < sfm_end[sfm]; i++ )
1506 : {
1507 239112 : if ( coeff[i] == 0 )
1508 : {
1509 199011 : coeff_out[i] = (float) ( own_random( bwe_seed ) ) / PCM16_TO_FLT_FAC;
1510 199011 : coeff_out[i] *= env;
1511 : }
1512 : }
1513 : }
1514 : else
1515 : {
1516 0 : env = 0.0f;
1517 : }
1518 : }
1519 19971 : else if ( R[sfm] == 0 )
1520 : {
1521 : /* fill random noise for 0 bit subbands */
1522 109746 : for ( i = sfm_start[sfm]; i < sfm_end[sfm]; i++ )
1523 : {
1524 103128 : if ( coeff[i] == 0 )
1525 : {
1526 50850 : coeff_out[i] = (float) ( own_random( bwe_seed ) ) / PCM16_TO_FLT_FAC;
1527 50850 : coeff_out[i] *= normq_v[sfm];
1528 : }
1529 : }
1530 :
1531 6618 : env = normq_v[sfm];
1532 : }
1533 36660 : if ( sfm == SFM_N_WB - 1 && prev_is_transient == 0 && prev_normq[sfm] > 0.5f * normq_v[sfm] && prev_normq[sfm] < 2.0f * normq_v[sfm] && bitalloc_var <= 0.3f )
1534 : {
1535 21 : float *p_prev_coeff_out = prev_coeff_out;
1536 441 : for ( i = sfm_start[sfm] + 12; i < sfm_end[sfm]; i++ )
1537 : {
1538 420 : if ( fabs( coeff_out[i] ) > 4.0f * fabs( *p_prev_coeff_out ) ||
1539 327 : fabs( coeff_out[i] ) < 0.25f * fabs( *p_prev_coeff_out ) ||
1540 276 : ( R[sfm] * ( *prev_R ) == 0 && R[sfm] + ( *prev_R ) != 0 ) )
1541 : {
1542 144 : coeff_out[i] = ( coeff_out[i] > 0 ) ? (float) ( 0.5f * ( fabs( coeff_out[i] ) + fabs( *p_prev_coeff_out ) ) ) : (float) ( -0.5f * ( fabs( coeff_out[i] ) + fabs( *p_prev_coeff_out ) ) );
1543 : }
1544 420 : p_prev_coeff_out++;
1545 : }
1546 : }
1547 :
1548 36660 : prev_env[sfm] = env;
1549 : }
1550 : }
1551 : else
1552 : {
1553 : /* fill random noise for 0 bit subbands of transient frame */
1554 891 : for ( sfm = 0; sfm < num_sfm; sfm++ )
1555 : {
1556 858 : if ( R[sfm] == 0 )
1557 : {
1558 4548 : for ( i = sfm_start[sfm]; i < sfm_end[sfm]; i++ )
1559 : {
1560 4200 : coeff_out[i] = (float) ( own_random( bwe_seed ) ) / PCM16_TO_FLT_FAC;
1561 4200 : coeff_out[i] *= normq_v[sfm];
1562 : }
1563 : }
1564 : }
1565 :
1566 33 : set_f( prev_env, 0, SFM_N_WB );
1567 : }
1568 :
1569 1443 : mvr2r( normq_v, prev_normq, SFM_N_WB );
1570 1443 : mvr2r( coeff_out + L_FRAME16k - L_HQ_WB_BWE, prev_coeff_out, L_HQ_WB_BWE );
1571 1443 : *prev_R = R[SFM_N_WB - 1];
1572 :
1573 1443 : return;
1574 : }
1575 :
1576 :
1577 : /*--------------------------------------------------------------------------*
1578 : * enforce_zero_for_min_envelope()
1579 : *
1580 : * Detect minimum level of envelope and set corresponding bands to zero
1581 : *--------------------------------------------------------------------------*/
1582 :
1583 24705 : void enforce_zero_for_min_envelope(
1584 : const int16_t hqswb_clas, /* i : HQ coding mode */
1585 : const int16_t *ynrm, /* i : Envelope indices */
1586 : float *coefsq, /* i/o: Quantized spectrum/zeroed spectrum */
1587 : int16_t nb_sfm, /* i : Number of coded sub bands */
1588 : const int16_t *sfm_start, /* i : Sub band start indices */
1589 : const int16_t *sfm_end /* i : Sub band end indices */
1590 : )
1591 : {
1592 : int16_t i, j;
1593 :
1594 : /* prevent non-zero output for all-zero input */
1595 24705 : if ( hqswb_clas != HQ_HVQ )
1596 : {
1597 20838 : if ( ynrm[0] == 31 )
1598 : {
1599 108 : for ( j = sfm_start[0]; j < sfm_end[0]; j++ )
1600 : {
1601 96 : coefsq[j] = 0.0f;
1602 : }
1603 : }
1604 :
1605 837591 : for ( i = 1; i < nb_sfm; i++ )
1606 : {
1607 816753 : if ( ynrm[i] == 39 )
1608 : {
1609 528639 : for ( j = sfm_start[i]; j < sfm_end[i]; j++ )
1610 : {
1611 510432 : coefsq[j] = 0.0f;
1612 : }
1613 : }
1614 : }
1615 : }
1616 :
1617 24705 : return;
1618 : }
1619 :
1620 :
1621 : /*--------------------------------------------------------------------------*
1622 : * overlap_hq_bwe()
1623 : *
1624 : * Overlapping at the boundary between HQ core and BWE
1625 : *--------------------------------------------------------------------------*/
1626 :
1627 9003 : static void overlap_hq_bwe(
1628 : const float *hq_swb_overlap_buf, /* i : spectrum from HQ core */
1629 : float *coeff_out, /* i/o: spectrum from BWE, overlapped output */
1630 : const int16_t n_swb_overlap_offset, /* i : starting offset of overlapping */
1631 : const int16_t n_swb_overlap, /* i : length of overlapping */
1632 : const int16_t *R, /* i : Bit allocation */
1633 : const int16_t num_env_bands, /* i : Number of coded envelope bands */
1634 : const int16_t num_sfm, /* i : Number of bands */
1635 : const int16_t *sfm_end /* i : Band end indices */
1636 : )
1637 : {
1638 : int16_t i;
1639 : float step;
1640 : float weighting;
1641 : int16_t n_band;
1642 :
1643 9003 : if ( R[num_env_bands - 1] != 0 )
1644 : {
1645 4893 : mvr2r( hq_swb_overlap_buf, &coeff_out[n_swb_overlap_offset], n_swb_overlap );
1646 : }
1647 : else
1648 : {
1649 : /*weighting = 0.8f;*/
1650 4110 : step = 1.0f / (float) n_swb_overlap;
1651 4110 : weighting = 1.0f;
1652 36990 : for ( i = 0; i < n_swb_overlap; i++ )
1653 : {
1654 32880 : coeff_out[n_swb_overlap_offset + i] = hq_swb_overlap_buf[i] * weighting + coeff_out[n_swb_overlap_offset + i] * ( 1.0f - weighting );
1655 32880 : weighting -= step;
1656 : }
1657 : }
1658 :
1659 :
1660 150864 : for ( n_band = num_env_bands; n_band < num_sfm; n_band++ )
1661 : {
1662 141861 : if ( R[n_band] != 0 )
1663 : {
1664 825 : for ( i = sfm_end[n_band - 1]; i < sfm_end[n_band]; ++i )
1665 : {
1666 792 : coeff_out[i] = hq_swb_overlap_buf[i - n_swb_overlap_offset];
1667 : }
1668 : }
1669 : }
1670 :
1671 9003 : return;
1672 : }
1673 :
1674 :
1675 : /*--------------------------------------------------------------------------*
1676 : * apply_envelope()
1677 : *
1678 : * Apply spectral envelope with envelope adjustments
1679 : *--------------------------------------------------------------------------*/
1680 :
1681 18486 : void apply_envelope(
1682 : const float *coeff, /* i/o: Coded/noisefilled normalized spectrum */
1683 : const int16_t *norm, /* i : Envelope */
1684 : const float *norm_adj, /* i : Envelope adjustment */
1685 : const int16_t num_sfm, /* i : Total number of bands */
1686 : const int16_t last_sfm, /* i : Last coded band */
1687 : const int16_t HQ_mode, /* i : HQ mode */
1688 : const int16_t length, /* i : Frame length */
1689 : const int16_t *sfm_start, /* i : Sub band start indices */
1690 : const int16_t *sfm_end, /* i : Sub band end indices */
1691 : float *normq_v, /* o : Envelope with adjustment */
1692 : float *coeff_out, /* o : coded/noisefilled spectrum */
1693 : float *coeff_out1 /* o : noisefilled spectrum for HQ SWE BWE */
1694 : )
1695 : {
1696 : int16_t i;
1697 : int16_t sfm;
1698 : float normq;
1699 : int16_t len;
1700 :
1701 18486 : len = num_sfm;
1702 18486 : if ( HQ_mode == HQ_GEN_SWB || HQ_mode == HQ_GEN_FB )
1703 : {
1704 9003 : len = last_sfm + 1;
1705 : }
1706 :
1707 18486 : if ( length == L_FRAME16k )
1708 : {
1709 48681 : for ( sfm = 0; sfm < num_sfm; sfm++ )
1710 : {
1711 46878 : normq_v[sfm] = dicn[norm[sfm]];
1712 46878 : normq = normq_v[sfm] * norm_adj[sfm];
1713 :
1714 623838 : for ( i = sfm_start[sfm]; i < sfm_end[sfm]; i++ )
1715 : {
1716 576960 : coeff_out[i] = coeff[i] * normq;
1717 : }
1718 : }
1719 : }
1720 : else
1721 : {
1722 588630 : for ( sfm = 0; sfm < len; sfm++ )
1723 : {
1724 571947 : normq_v[sfm] = dicn[norm[sfm]];
1725 571947 : normq_v[sfm] *= norm_adj[sfm];
1726 :
1727 571947 : normq = normq_v[sfm];
1728 9456363 : for ( i = sfm_start[sfm]; i < sfm_end[sfm]; i++ )
1729 : {
1730 8884416 : coeff_out[i] = coeff[i] * normq;
1731 : }
1732 : }
1733 :
1734 16683 : if ( HQ_mode == HQ_GEN_SWB || HQ_mode == HQ_GEN_FB )
1735 : {
1736 252117 : for ( sfm = 0; sfm <= last_sfm; sfm++ )
1737 : {
1738 243114 : normq = normq_v[sfm];
1739 3196890 : for ( i = sfm_start[sfm]; i < sfm_end[sfm]; i++ )
1740 : {
1741 2953776 : coeff_out1[i] = coeff_out1[i] * normq;
1742 : }
1743 : }
1744 : }
1745 : }
1746 :
1747 18486 : return;
1748 : }
1749 :
1750 :
1751 : /*--------------------------------------------------------------------------*
1752 : * apply_envelope_enc()
1753 : *
1754 : * Apply spectral envelope without envelope adjustments and noisefill
1755 : *--------------------------------------------------------------------------*/
1756 :
1757 7096 : void apply_envelope_enc(
1758 : float *coeff, /* i/o: Normalized/scaled normalized spectrum */
1759 : const int16_t *norm, /* i : Envelope */
1760 : const int16_t num_sfm, /* i : Total number of bands */
1761 : const int16_t *sfm_start, /* i : Sub band start indices */
1762 : const int16_t *sfm_end /* i : Sub band end indices */
1763 : )
1764 : {
1765 : int16_t sfm;
1766 : int16_t i;
1767 : float normq;
1768 :
1769 292454 : for ( sfm = 0; sfm < num_sfm; sfm++ )
1770 : {
1771 285358 : normq = dicn[norm[sfm]];
1772 :
1773 5459598 : for ( i = sfm_start[sfm]; i < sfm_end[sfm]; i++ )
1774 : {
1775 5174240 : coeff[i] *= normq;
1776 : }
1777 : }
1778 :
1779 7096 : return;
1780 : }
1781 :
1782 :
1783 : /*-----------------------------------------------------------------------------
1784 : * floating_point_add()
1785 : *
1786 : * Add two floating point numbers in integer representation: x <- x + y
1787 : * Ported from BASOP code to ensure interoperability
1788 : *----------------------------------------------------------------------------*/
1789 :
1790 89040 : void floating_point_add(
1791 : int32_t *mx, /* i/o: mantissa of the addend Q31 */
1792 : int16_t *ex, /* i/o: exponent of the addend Q0 */
1793 : const int32_t my, /* i : mantissa of the adder Q31 */
1794 : const int16_t ey /* i : exponent of the adder Q0 */
1795 : )
1796 : {
1797 : Word32 accX, accY;
1798 : Word16 align, expo;
1799 :
1800 89040 : accX = *mx >> 1;
1801 89040 : accY = my >> 1;
1802 89040 : align = *ex - ey;
1803 89040 : if ( align < 0 )
1804 : {
1805 70268 : if ( align > -32 ) /* If align < -32, (accY >> (-align) = 0 */
1806 : {
1807 70268 : accX = accX + ( accY >> ( -align ) );
1808 : }
1809 : }
1810 : else
1811 : {
1812 18772 : if ( align < 32 ) /* If align > 32, (accX >> align) = 0 */
1813 : {
1814 18772 : accX = accY + ( accX >> align );
1815 : }
1816 : else
1817 : {
1818 0 : accX = accY;
1819 : }
1820 18772 : *ex = ey;
1821 : }
1822 89040 : expo = norm_l( accX ); /* aligned to BASOP */
1823 89040 : *mx = accX << expo;
1824 89040 : *ex = *ex + expo - 1;
1825 :
1826 89040 : return;
1827 : }
|