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 60722 : 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 60722 : lookback = L_swb_norm / 2;
70 60722 : env_index = 0;
71 1263846 : for ( n_freq = 0; n_freq < lookback; n_freq++ )
72 : {
73 1203124 : n_lag_now = lookback + n_freq;
74 :
75 : /* Apply MA filter */
76 1203124 : envelope[env_index] = EPSILON;
77 41012525 : for ( n_lag = 0; n_lag < n_lag_now; n_lag++ )
78 : {
79 39809401 : envelope[env_index] += (float) fabs( SWB_signal[n_lag] );
80 : }
81 1203124 : env_index++;
82 : }
83 :
84 60722 : n_lag_now = L_swb_norm;
85 :
86 9890760 : for ( n_freq = 0; n_freq < SWB_flength - L_swb_norm; n_freq++ )
87 : {
88 : /* Apply MA filter */
89 9830038 : envelope[env_index] = EPSILON;
90 391729484 : for ( n_lag = 0; n_lag < n_lag_now; n_lag++ )
91 : {
92 381899446 : envelope[env_index] += (float) fabs( SWB_signal[n_freq + n_lag] );
93 : }
94 9830038 : env_index++;
95 : }
96 :
97 1293404 : for ( n_freq = SWB_flength - L_swb_norm, i = 0; n_freq < SWB_flength - lookback; n_freq++, i++ )
98 : {
99 1232682 : n_lag_now = L_swb_norm - i;
100 :
101 : /* Apply MA filter */
102 1232682 : envelope[env_index] = EPSILON;
103 43445385 : for ( n_lag = 0; n_lag < n_lag_now; n_lag++ )
104 : {
105 42212703 : envelope[env_index] += (float) fabs( SWB_signal[n_freq + n_lag] );
106 : }
107 1232682 : env_index++;
108 : }
109 :
110 60722 : return;
111 : }
112 :
113 : /*--------------------------------------------------------------------------*
114 : * noise_level_calc()
115 : *
116 : * Calculate noise level and limited band
117 : *--------------------------------------------------------------------------*/
118 :
119 37439 : 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 37439 : nb_sfm = *limit;
131 37439 : ener_limit = 1e-5f;
132 37439 : *noise_level = 0.0f;
133 411829 : for ( i = 0; i < 10; i++ )
134 : {
135 374390 : ener_limit += wnorm[i];
136 374390 : *noise_level += (float) abs( wnorm[i + 1] - wnorm[i] );
137 : }
138 37439 : ener_sum = ener_limit;
139 :
140 748561 : for ( i = 10; i < ( nb_sfm - 1 ); i++ )
141 : {
142 711122 : ener_sum += wnorm[i];
143 711122 : *noise_level += (float) abs( wnorm[i + 1] - wnorm[i] );
144 : }
145 37439 : ener_sum += wnorm[nb_sfm - 1];
146 :
147 37439 : if ( core_brate < HQ_BWE_CROSSOVER_BRATE )
148 : {
149 30142 : fact = 0.885f;
150 : }
151 : else
152 : {
153 7297 : fact = 0.942f;
154 : }
155 :
156 37439 : i = 9;
157 621073 : while ( ener_limit < ener_sum * fact && i + 1 < nb_sfm )
158 : {
159 583634 : ener_limit += wnorm[++i];
160 : }
161 37439 : *limit = i;
162 :
163 : /* calculate noise level for spectrum filling */
164 37439 : *noise_level /= ener_sum;
165 37439 : if ( *noise_level < 0 )
166 : {
167 0 : *noise_level = 0.25f;
168 : }
169 :
170 37439 : *noise_level = 0.25f - *noise_level;
171 :
172 37439 : if ( *noise_level < 0.0f )
173 : {
174 3904 : *noise_level = 0.0f;
175 : }
176 :
177 37439 : 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 221326 : 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 221326 : cb_size = 0;
207 6727283 : for ( sfm = 0; sfm <= last_sfm; sfm++ )
208 : {
209 6505957 : if ( R[sfm] != 0 )
210 : {
211 4841424 : if ( flag_32K_env_ho )
212 : {
213 : /* Build compressed (+/- 1) noise-fill codebook */
214 359864 : sfm_base = sfm_start[sfm];
215 945954 : for ( i = 0; i < sfmsize[sfm] / 8; i++ )
216 : {
217 586090 : E_cb_vec = 0;
218 5274810 : for ( j = sfm_base + i * 8; j < sfm_base + ( i + 1 ) * 8; j++ )
219 : {
220 4688720 : if ( coeff[j] > 0.0f )
221 : {
222 834841 : CodeBook_mod[cb_size] = 1.0f;
223 834841 : E_cb_vec++;
224 : }
225 3853879 : else if ( coeff[j] < 0.0f )
226 : {
227 831236 : CodeBook_mod[cb_size] = -1.0f;
228 831236 : E_cb_vec++;
229 : }
230 : else
231 : {
232 3022643 : CodeBook_mod[cb_size] = 0.0f;
233 : }
234 4688720 : cb_size++;
235 : }
236 :
237 586090 : if ( E_cb_vec < 2 )
238 : {
239 128609 : cb_size -= 8;
240 : }
241 : }
242 : }
243 : else
244 : {
245 61134620 : for ( j = sfm_start[sfm]; j < sfm_end[sfm]; j++ )
246 : {
247 56653060 : CodeBook[cb_size] = coeff[j];
248 56653060 : cb_size++;
249 : }
250 : }
251 : }
252 : }
253 :
254 221326 : if ( flag_32K_env_ho )
255 : {
256 3681035 : for ( j = 0; j < cb_size; j++ )
257 : {
258 3659848 : if ( CodeBook_mod[j] != 0.0f )
259 : {
260 : /* Densify codebook */
261 1584071 : 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 2075777 : CodeBook[j] = CodeBook_mod[cb_size - j - 1];
266 : }
267 : }
268 : }
269 :
270 221326 : 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 287755 : 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 287755 : core_sfm = nb_sfm - 1;
288 :
289 3301669 : for ( sfm = nb_sfm - 1; sfm >= 0; sfm-- )
290 : {
291 3301669 : if ( bitalloc[sfm] != 0 )
292 : {
293 287755 : core_sfm = sfm;
294 287755 : break;
295 : }
296 : }
297 :
298 287755 : return core_sfm;
299 : }
300 :
301 : /*--------------------------------------------------------------------------*
302 : * apply_noisefill_HQ()
303 : *
304 : * Inject noise in non-coded bands
305 : *--------------------------------------------------------------------------*/
306 :
307 221326 : 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 221326 : if ( length >= L_FRAME32k || core_brate != HQ_32k )
330 : {
331 : /* Read from codebook */
332 220988 : cb_pos = 0;
333 :
334 6718537 : for ( sfm = 0; sfm <= last_sfm; sfm++ )
335 : {
336 6497549 : if ( R[sfm] == 0 )
337 : {
338 1663935 : if ( flag_32K_env_ho )
339 : {
340 235718 : E_cb_vec = 0.0f;
341 235718 : if ( sfm < 20 )
342 : {
343 1767945 : for ( i = 0; i < sfmsize[sfm]; i++ )
344 : {
345 1600056 : cb_buff[i] = CodeBook_mod[cb_pos];
346 1600056 : E_cb_vec += cb_buff[i] * cb_buff[i];
347 1600056 : cb_pos++;
348 1600056 : if ( cb_pos >= cb_size )
349 : {
350 4185 : cb_pos = 0;
351 : }
352 : }
353 : }
354 : else
355 : {
356 1503501 : for ( i = 0; i < sfmsize[sfm]; i++ )
357 : {
358 1435672 : cb_buff[i] = CodeBook[cb_pos];
359 1435672 : E_cb_vec += cb_buff[i] * cb_buff[i];
360 1435672 : cb_pos++;
361 1435672 : if ( cb_pos >= cb_size )
362 : {
363 11092 : cb_pos = 0;
364 : }
365 : }
366 : }
367 :
368 235718 : E_corr = E_cb_vec / ( (float) sfmsize[sfm] );
369 235718 : E_corr = 1.0f / (float) sqrt( E_corr );
370 :
371 3271446 : for ( j = sfm_start[sfm]; j < sfm_end[sfm]; j++ )
372 : {
373 3035728 : coeff[j] = cb_buff[j - sfm_start[sfm]] * E_corr;
374 : }
375 : }
376 : else
377 : {
378 26602381 : for ( j = sfm_start[sfm]; j < sfm_end[sfm]; j++ )
379 : {
380 25174164 : coeff[j] = CodeBook[cb_pos];
381 25174164 : cb_pos++;
382 25174164 : cb_pos = ( cb_pos >= cb_size ) ? 0 : cb_pos;
383 : }
384 : }
385 : }
386 : }
387 : }
388 :
389 221326 : return;
390 : }
391 :
392 :
393 : /*--------------------------------------------------------------------------*
394 : * harm_bwe_fine()
395 : *
396 : * Prepare harmonic BWE fine structure
397 : *--------------------------------------------------------------------------*/
398 :
399 27647 : 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 27647 : int16_t norm_width = 64;
421 :
422 : /* shape the spectrum */
423 699140 : for ( sfm = 0; sfm <= last_sfm; sfm++ )
424 : {
425 671493 : if ( R[sfm] != 0 )
426 : {
427 576103 : normq = dicn[norm[sfm]];
428 7734895 : for ( i = sfm_start[sfm]; i < sfm_end[sfm]; i++ )
429 : {
430 7158792 : coeff_out[i] = coeff[i] * normq;
431 : }
432 : }
433 : else
434 : {
435 1337718 : for ( i = sfm_start[sfm]; i < sfm_end[sfm]; i++ )
436 : {
437 1242328 : coeff_out[i] = 0.0f;
438 : }
439 : }
440 : }
441 :
442 : /* excitation replication */
443 27647 : mvr2r( coeff_out, SWB_signal, L_HARMONIC_EXC );
444 27647 : calc_normal_length( HQ_CORE, coeff_out, HQ_HARMONIC, -1, &norm_width, prev_L_swb_norm );
445 27647 : hq_swb_harmonic_calc_norm_envelop( SWB_signal, envelope, norm_width, L_HARMONIC_EXC );
446 :
447 : /* Normalize with envelope */
448 5612341 : for ( i = 0; i < L_HARMONIC_EXC; i++ )
449 : {
450 5584694 : SWB_signal[i] = SWB_signal[i] / envelope[i];
451 : }
452 :
453 27647 : dst = coeff_fine + sfm_end[last_sfm];
454 27647 : end = coeff_fine + sfm_end[num_sfm - 1];
455 :
456 27647 : if ( ( sfm_end[last_sfm] - sfm_end[high_sfm] ) <= L_HARMONIC_EXC - START_EXC )
457 : {
458 23764 : src = SWB_signal + START_EXC + ( sfm_end[last_sfm] - sfm_end[high_sfm] );
459 : }
460 : else
461 : {
462 3883 : src = SWB_signal + L_HARMONIC_EXC - 1;
463 : }
464 :
465 90702 : while ( dst < end )
466 : {
467 6151038 : while ( dst < end && src < &SWB_signal[L_HARMONIC_EXC] )
468 : {
469 6087983 : *dst++ = *src++;
470 : }
471 63055 : src--;
472 :
473 5437152 : while ( dst < end && src >= &SWB_signal[START_EXC] )
474 : {
475 5374097 : *dst++ = *src--;
476 : }
477 63055 : src++;
478 : }
479 :
480 27647 : return;
481 : }
482 :
483 : /*--------------------------------------------------------------------------*
484 : * hvq_bwe_fine()
485 : *
486 : * Prepare HVQ BWE fine structure
487 : *--------------------------------------------------------------------------*/
488 :
489 33075 : 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 33075 : int16_t norm_width = 64;
508 :
509 : /* excitation replication */
510 33075 : mvr2r( coeff, SWB_signal, L_HARMONIC_EXC );
511 33075 : calc_normal_length( HQ_CORE, coeff, HQ_HVQ, -1, &norm_width, prev_L_swb_norm );
512 :
513 33075 : hq_swb_harmonic_calc_norm_envelop( SWB_signal, envelope, norm_width, L_HARMONIC_EXC );
514 :
515 : /* Normalize with envelope */
516 6714225 : for ( i = 0; i < L_HARMONIC_EXC; i++ )
517 : {
518 6681150 : SWB_signal[i] = SWB_signal[i] / envelope[i];
519 : }
520 :
521 33075 : dst = coeff_fine;
522 33075 : end = coeff_fine + sfm_end[num_sfm - 1] - sfm_end[last_sfm];
523 :
524 33075 : src = SWB_signal + START_EXC;
525 33075 : peak_src = peak_pos + START_EXC;
526 :
527 544745 : for ( i = 0; i < Npeaks; i++ )
528 : {
529 511670 : if ( peak_idx[i] < L_HARMONIC_EXC )
530 : {
531 432758 : peak_pos[peak_idx[i]] = 1;
532 : }
533 : }
534 :
535 33075 : i = L_HARMONIC_EXC - 1;
536 684755 : while ( i-- > 0 )
537 : {
538 684755 : if ( peak_pos[i] == 1 )
539 : {
540 33075 : break;
541 : }
542 : }
543 :
544 33075 : if ( i < 180 )
545 : {
546 9811 : i = 180;
547 : }
548 :
549 556385 : for ( j = L_HARMONIC_EXC - 1; j > i + 1; j-- )
550 : {
551 523310 : SWB_signal[j] = 0.0f;
552 : }
553 :
554 33075 : peak_dst = bwe_peaks + sfm_end[last_sfm];
555 109337 : while ( dst < end )
556 : {
557 9207058 : while ( dst < end && src < &SWB_signal[L_HARMONIC_EXC] )
558 : {
559 9130796 : *dst++ = *src++;
560 9130796 : *peak_dst++ = *peak_src++;
561 : }
562 76262 : peak_src--;
563 76262 : src--;
564 :
565 6535354 : while ( dst < end && src >= &SWB_signal[START_EXC] )
566 : {
567 6459092 : *dst++ = *src--;
568 6459092 : *peak_dst++ = *peak_src--;
569 : }
570 76262 : peak_src++;
571 76262 : src++;
572 : }
573 :
574 33075 : return;
575 : }
576 :
577 : /*--------------------------------------------------------------------------*
578 : * hq_fold_bwe()
579 : *
580 : * HQ mode folding BWE
581 : *--------------------------------------------------------------------------*/
582 :
583 101775 : 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 101775 : low_coeff = sfm_end[last_sfm] >> 1;
596 101775 : src = coeff + sfm_end[last_sfm] - 1;
597 :
598 101775 : first_coeff = sfm_end[last_sfm];
599 101775 : dst = coeff + sfm_end[last_sfm];
600 101775 : end = coeff + sfm_end[num_sfm - 1];
601 :
602 : /* Generate BWE with spectral folding */
603 175507 : while ( dst < end )
604 : {
605 9412655 : while ( dst < end && src >= &coeff[low_coeff] )
606 : {
607 9338923 : *dst++ = *src--;
608 : }
609 :
610 73732 : src++;
611 :
612 4919665 : while ( dst < end && src < &coeff[first_coeff] )
613 : {
614 4845933 : *dst++ = *src++;
615 : }
616 : }
617 :
618 101775 : return;
619 : }
620 :
621 : /*--------------------------------------------------------------------------*
622 : * apply_nf_gain()
623 : *
624 : * Apply noise fill gain
625 : *--------------------------------------------------------------------------*/
626 :
627 214203 : 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 214203 : nf_scale = 1.0f / ( 1 << nf_idx );
641 6547747 : for ( sfm = 0; sfm <= last_sfm; sfm++ )
642 : {
643 6333544 : if ( R[sfm] == 0 )
644 : {
645 29694636 : for ( j = sfm_start[sfm]; j < sfm_end[sfm]; j++ )
646 : {
647 : /* Scale NoiseFill */
648 28043464 : coeff[j] = coeff[j] * nf_scale;
649 : }
650 : }
651 : }
652 :
653 214203 : return;
654 : }
655 :
656 : /*--------------------------------------------------------------------------*
657 : * hq_generic_fine()
658 : *
659 : * Prepare HQ SWB BWE fine structure
660 : *--------------------------------------------------------------------------*/
661 :
662 80945 : 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 80945 : multi = 1.0f;
676 2269597 : for ( sfm = 0; sfm <= last_sfm; sfm++ )
677 : {
678 28813900 : for ( i = sfm_start[sfm]; i < sfm_end[sfm]; i++ )
679 : {
680 26625248 : if ( coeff[i] == 0.f )
681 : {
682 14772863 : coeff_out1[i] = (float) multi * ( own_random( bwe_seed ) > 0 ? 1.0f : -1.0f ) * 0.5f;
683 : }
684 : else
685 : {
686 11852385 : coeff_out1[i] = coeff[i];
687 : }
688 : }
689 : }
690 :
691 80945 : return;
692 : }
693 :
694 : /*--------------------------------------------------------------------------*
695 : * harm_bwe()
696 : *
697 : * HQ Harmonic BWE
698 : *--------------------------------------------------------------------------*/
699 :
700 27647 : 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 27647 : float alfa = 0.5f;
725 : float alpha, beta;
726 : int16_t idx;
727 : float fac;
728 : float *src, *dst;
729 :
730 699140 : for ( sfm = 0; sfm <= last_sfm; sfm++ )
731 : {
732 671493 : if ( R[sfm] == 0 )
733 : {
734 95390 : normq = dicn[norm[sfm]];
735 1337718 : for ( i = sfm_start[sfm]; i < sfm_end[sfm]; i++ )
736 : {
737 1242328 : coeff_out[i] = coeff[i] * normq;
738 : }
739 : }
740 : }
741 27647 : noise_level[1] = noise_level[0];
742 :
743 : /* shaping the BWE spectrum further by envelopes and noise factors */
744 27647 : noise_level[0] = 0.9f * prev_noise_level[0] + 0.1f * noise_level[0];
745 27647 : noise_level[1] = 0.9f * prev_noise_level[1] + 0.1f * noise_level[1];
746 :
747 27647 : if ( prev_hq_mode == HQ_NORMAL || prev_hq_mode == HQ_GEN_SWB )
748 : {
749 6810 : if ( noise_level[0] < 0.25f )
750 : {
751 6810 : noise_level[0] *= 4.0f;
752 : }
753 :
754 6810 : if ( noise_level[1] < 0.25f )
755 : {
756 6810 : noise_level[1] *= 4.0f;
757 : }
758 : }
759 :
760 27647 : E_L = EPSILON;
761 240325 : for ( i = last_sfm + 1; i < num_sfm; i++ )
762 : {
763 212678 : E_L = EPSILON;
764 11674758 : for ( j = sfm_start[i]; j < sfm_end[i]; j++ )
765 : {
766 11462080 : E_L += coeff_fine[j] * coeff_fine[j];
767 : }
768 212678 : E_L = (float) sqrt( ( sfm_end[i] - sfm_start[i] ) / E_L );
769 :
770 212678 : normq = dicn[norm[i]];
771 212678 : norm_adj = normq * E_L;
772 212678 : alfa = ( i > 27 ) ? noise_level[1] : noise_level[0];
773 212678 : beta = (float) sqrt( 1.0f - alfa );
774 212678 : if ( element_mode > EVS_MONO )
775 : {
776 208660 : alpha = (float) sqrt( alfa );
777 : }
778 : else
779 : {
780 4018 : alpha = (float) sqrt( alfa ) * 0.5f;
781 : }
782 :
783 11674758 : for ( sfm = sfm_start[i]; sfm < sfm_end[i]; sfm++ )
784 : {
785 11462080 : coeff_out[sfm] = ( beta * coeff_fine[sfm] * norm_adj + alpha * own_random( bwe_seed ) / PCM16_TO_FLT_FAC * normq );
786 : }
787 : }
788 :
789 27647 : prev_noise_level[0] = noise_level[0];
790 27647 : prev_noise_level[1] = noise_level[1];
791 27647 : idx = 16;
792 27647 : src = &coeff_out[sfm_end[high_sfm] + L_HARMONIC_EXC - START_EXC];
793 27647 : dst = src - 1;
794 :
795 469999 : for ( i = 0; i < idx; i++ )
796 : {
797 442352 : fac = i / ( 2.0f * idx );
798 442352 : *src++ *= 0.5f + fac;
799 442352 : *dst-- *= 0.5f + fac;
800 : }
801 27647 : if ( num_sfm == 33 )
802 : {
803 13557 : set_f( &coeff_out[800], 0, 160 );
804 : }
805 27647 : return;
806 : }
807 :
808 : /*--------------------------------------------------------------------------*
809 : * HVQ_bwe()
810 : *
811 : * HQ HVQ BWE
812 : *--------------------------------------------------------------------------*/
813 :
814 33075 : 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 33075 : int16_t bwe_noise_th = 0;
840 : int16_t peak_band, low, high, sel_norm;
841 : int16_t norm_ind;
842 33075 : float tmp_norm = 0;
843 : int16_t idx;
844 : float fac;
845 : float *src, *dst;
846 : int16_t istart, iend;
847 33075 : int16_t offset = sfm_end[last_sfm];
848 :
849 33075 : mvr2r( coeff, coeff_out, L_FRAME48k );
850 :
851 33075 : bwe_noise_th = bin_th + ( sfm_end[num_sfm - 1] - bin_th ) / HVQ_BWE_NOISE_BANDS;
852 33075 : 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 33075 : noise_level[0] = 0.9f * prev_noise_level[0] + 0.1f * noise_level[0];
856 33075 : noise_level[1] = 0.9f * prev_noise_level[1] + 0.1f * noise_level[1];
857 :
858 33075 : if ( prev_hq_mode == HQ_NORMAL || prev_hq_mode == HQ_GEN_SWB )
859 : {
860 2232 : if ( noise_level[0] < 0.25f )
861 : {
862 2232 : noise_level[0] *= 4.0f;
863 : }
864 :
865 2232 : if ( noise_level[1] < 0.25f )
866 : {
867 2232 : noise_level[1] *= 4.0f;
868 : }
869 : }
870 :
871 33075 : norm_ind = last_sfm + 1;
872 :
873 33075 : if ( core_brate < HQ_BWE_CROSSOVER_BRATE )
874 : {
875 25213 : peak_band = 0;
876 25213 : E_L = EPSILON;
877 1638845 : for ( i = sfm_start[norm_ind]; i < sfm_end[norm_ind + 1]; i++ )
878 : {
879 1613632 : if ( bwe_peaks[i] )
880 : {
881 59064 : peak_band = 1;
882 : }
883 1613632 : E_L += coeff_fine[i - offset] * coeff_fine[i - offset];
884 : }
885 25213 : E_L = (float) sqrt( ( sfm_end[norm_ind + 1] - sfm_start[norm_ind] ) / E_L );
886 :
887 25213 : normq = 0.1f * dicn[norm[norm_ind - 1]] + 0.8f * dicn[norm[norm_ind]] + 0.1f * dicn[norm[norm_ind + 1]];
888 25213 : tmp_norm = 0.1f * dicn[norm[norm_ind]] + 0.8f * dicn[norm[norm_ind + 1]] + 0.1f * dicn[norm[norm_ind + 2]];
889 :
890 25213 : istart = sfm_start[norm_ind];
891 25213 : iend = istart + sfm_len[norm_ind] / 2;
892 428621 : for ( i = istart; i < iend; i++ )
893 : {
894 403408 : 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 25213 : j = 0;
898 25213 : N = sfm_len[norm_ind] / 2 + sfm_len[norm_ind + 1] / 2 - 1;
899 25213 : istart = iend;
900 25213 : iend = sfm_start[norm_ind + 1] + sfm_len[norm_ind + 1] / 2;
901 832029 : for ( i = istart; i < iend; i++ )
902 : {
903 806816 : 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 806816 : j++;
905 : }
906 :
907 25213 : istart = iend;
908 25213 : iend = sfm_end[norm_ind + 1];
909 428621 : for ( i = istart; i < iend; i++ )
910 : {
911 403408 : 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 25213 : norm_ind += 2;
915 : }
916 :
917 289056 : for ( ; norm_ind < num_sfm; norm_ind++ )
918 : {
919 255981 : if ( R[norm_ind] == 0 )
920 : {
921 252750 : peak_band = 0;
922 252750 : E_L = EPSILON;
923 :
924 4740910 : for ( i = sfm_start[norm_ind]; i < sfm_end[norm_ind]; i++ )
925 : {
926 4720903 : if ( bwe_peaks[i] )
927 : {
928 232743 : peak_band = 1;
929 232743 : break;
930 : }
931 : }
932 :
933 252750 : istart = sfm_start[norm_ind];
934 252750 : iend = sfm_end[norm_ind];
935 :
936 252750 : if ( peak_band == 1 && norm_ind > last_sfm + 1 && norm_ind < num_sfm - 1 )
937 : {
938 194496 : istart -= sfm_len[norm_ind - 1] / 2;
939 194496 : iend += sfm_len[norm_ind + 1] / 2;
940 : }
941 :
942 24186766 : for ( i = istart; i < iend; i++ )
943 : {
944 23934016 : E_L += coeff_fine[i - offset] * coeff_fine[i - offset];
945 : }
946 252750 : E_L = (float) sqrt( ( iend - istart ) / E_L );
947 :
948 252750 : if ( peak_band )
949 : {
950 232743 : if ( norm_ind + 1 > num_sfm - 1 )
951 : {
952 31654 : normq = 0.15f * dicn[norm[norm_ind - 1]] + 0.85f * dicn[norm[norm_ind]];
953 : }
954 : else
955 : {
956 201089 : 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 20007 : low = norm_ind;
962 20007 : high = min( norm_ind + 1, num_sfm - 1 );
963 20007 : sel_norm = norm[norm_ind - 1];
964 58600 : for ( j = low; j <= high; j++ )
965 : {
966 38593 : if ( norm[j] > sel_norm )
967 : {
968 15786 : sel_norm = norm[j];
969 : }
970 : }
971 20007 : normq = dicn[sel_norm];
972 : }
973 :
974 14086302 : for ( i = sfm_start[norm_ind]; i < sfm_end[norm_ind]; i++ )
975 : {
976 13833552 : 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 145935 : for ( i = sfm_start[norm_ind]; i < sfm_end[norm_ind]; i++ )
982 : {
983 142704 : coeff_out[i] = coeff[i]; /* Scaling already applied */
984 : }
985 : }
986 : }
987 :
988 33075 : prev_noise_level[0] = noise_level[0];
989 33075 : prev_noise_level[1] = noise_level[1];
990 33075 : idx = 16;
991 33075 : src = &coeff_out[sfm_end[last_sfm] + L_HARMONIC_EXC - START_EXC];
992 33075 : dst = src - 1;
993 :
994 562275 : for ( i = 0; i < idx; i++ )
995 : {
996 529200 : fac = i / ( 2.0f * idx );
997 529200 : *src++ *= 0.5f + fac;
998 529200 : *dst-- *= 0.5f + fac;
999 : }
1000 :
1001 33075 : 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 45695 : 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 45695 : s = 0;
1024 :
1025 114936 : for ( k = 0; k < pvq_bands; k++ )
1026 : {
1027 :
1028 69241 : if ( k >= pvq_bands - n_sel_bnds )
1029 : {
1030 7658 : hvq_band_start[k] = hvq_band_end[k - 1];
1031 7658 : hvq_band_width[k] = band_len_harm[sel_bnds[s]];
1032 7658 : hvq_band_end[k] = hvq_band_end[k - 1] + band_len_harm[sel_bnds[s]];
1033 7658 : s++;
1034 : }
1035 : else
1036 : {
1037 61583 : hvq_band_start[k] = k * HVQ_PVQ_COEFS;
1038 61583 : hvq_band_width[k] = HVQ_PVQ_COEFS;
1039 61583 : hvq_band_end[k] = ( k + 1 ) * HVQ_PVQ_COEFS;
1040 : }
1041 : }
1042 :
1043 45695 : return;
1044 : }
1045 :
1046 :
1047 : /*--------------------------------------------------------------------------*
1048 : * map_hq_generic_fenv_norm()
1049 : *
1050 : * mapping high frequency envelope to high band norm
1051 : *--------------------------------------------------------------------------*/
1052 :
1053 110815 : 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 110815 : set_f( env_fl, 0, 17 );
1067 110815 : if ( hq_generic_offset == 144 )
1068 : {
1069 1772 : env_fl[0] = hq_generic_fenv[1];
1070 1772 : env_fl[1] = hq_generic_fenv[2] * 0.6640625f + hq_generic_fenv[3] * 0.3359375f;
1071 1772 : env_fl[2] = hq_generic_fenv[3] * 0.6640625f + hq_generic_fenv[4] * 0.3359375f;
1072 1772 : env_fl[3] = hq_generic_fenv[4] * 0.3359375f + hq_generic_fenv[5] * 0.6640625f;
1073 1772 : env_fl[4] = hq_generic_fenv[5] * 0.3359375f + hq_generic_fenv[6] * 0.6640625f;
1074 1772 : env_fl[5] = hq_generic_fenv[7];
1075 1772 : env_fl[6] = hq_generic_fenv[8] * 0.75f + hq_generic_fenv[9] * 0.25f;
1076 1772 : env_fl[7] = hq_generic_fenv[9] * 0.75f + hq_generic_fenv[10] * 0.25f;
1077 1772 : env_fl[8] = hq_generic_fenv[10] * 0.25f + hq_generic_fenv[11] * 0.75f;
1078 : }
1079 : else
1080 : {
1081 109043 : env_fl[0] = hq_generic_fenv[0] * 0.3359375f + hq_generic_fenv[1] * 0.6640625f;
1082 109043 : env_fl[1] = hq_generic_fenv[1] * 0.3359375f + hq_generic_fenv[2] * 0.6640625f;
1083 109043 : env_fl[2] = hq_generic_fenv[3];
1084 109043 : env_fl[3] = hq_generic_fenv[4] * 0.6640625f + hq_generic_fenv[5] * 0.3359375f;
1085 109043 : env_fl[4] = hq_generic_fenv[5] * 0.6640625f + hq_generic_fenv[6] * 0.3359375f;
1086 109043 : env_fl[5] = hq_generic_fenv[6] * 0.3359375f + hq_generic_fenv[7] * 0.6640625f;
1087 109043 : env_fl[6] = hq_generic_fenv[7] * 0.3359375f + hq_generic_fenv[8] * 0.6640625f;
1088 109043 : env_fl[7] = hq_generic_fenv[8] * 0.3359375f + hq_generic_fenv[9] * 0.6640625f;
1089 109043 : env_fl[8] = hq_generic_fenv[9] * 0.3359375f + hq_generic_fenv[10] * 0.6640625f;
1090 109043 : env_fl[9] = hq_generic_fenv[10] * 0.25f + hq_generic_fenv[11] * 0.75f;
1091 109043 : env_fl[10] = hq_generic_fenv[12];
1092 109043 : env_fl[11] = hq_generic_fenv[13];
1093 : }
1094 :
1095 110815 : if ( hqswb_clas == HQ_GEN_FB )
1096 : {
1097 60534 : if ( hq_generic_offset == 144 )
1098 : {
1099 548 : env_fl[9] = hq_generic_fenv[12];
1100 548 : env_fl[10] = hq_generic_fenv[12] * 0.25f + hq_generic_fenv[13] * 0.75f;
1101 548 : env_fl[11] = hq_generic_fenv[13] * 0.5f + hq_generic_fenv[14] * 0.5f;
1102 548 : env_fl[12] = hq_generic_fenv[14];
1103 548 : env_fl[13] = hq_generic_fenv[14];
1104 : }
1105 : else
1106 : {
1107 59986 : env_fl[12] = hq_generic_fenv[14];
1108 59986 : env_fl[13] = hq_generic_fenv[14] * 0.25f + hq_generic_fenv[15] * 0.75f;
1109 59986 : env_fl[14] = hq_generic_fenv[15] * 0.5f + hq_generic_fenv[16] * 0.5f;
1110 59986 : env_fl[15] = hq_generic_fenv[16];
1111 59986 : env_fl[16] = hq_generic_fenv[16];
1112 : }
1113 : }
1114 :
1115 110815 : logqnorm_2( env_fl, 40, num_env_bands, nb_sfm, ynrm + num_env_bands, normqlg2 + num_env_bands, thren_HQ );
1116 :
1117 1737949 : for ( i = num_env_bands; i < nb_sfm; ++i )
1118 : {
1119 1627134 : idx = min( ynrm[i] + 10, 39 );
1120 1627134 : normqlg2[i] = dicnlg2[idx];
1121 : }
1122 :
1123 110815 : return;
1124 : }
1125 :
1126 :
1127 : /*-------------------------------------------------------------------*
1128 : * update_rsubband()
1129 : *
1130 : * Update subband bit allocation
1131 : *--------------------------------------------------------------------------*/
1132 :
1133 169 : 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 342 : while ( b_add_bits_denv > 0 )
1143 : {
1144 173 : i = nb_sfm - 1;
1145 4204 : while ( b_add_bits_denv > 0 && i >= 0 )
1146 : {
1147 4031 : if ( Rsubband[i] > 24 )
1148 : {
1149 1663 : Rsubband[i] -= 8;
1150 1663 : b_add_bits_denv--;
1151 : }
1152 4031 : i--;
1153 : }
1154 : }
1155 :
1156 169 : 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 80945 : 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 80945 : add_bits_denv = 0;
1179 80945 : if ( core_sfm >= num_env_bands )
1180 : {
1181 122 : bitsforDelta = get_next_indice( st, 2 );
1182 122 : bitsforDelta += 2;
1183 122 : add_bits_denv += 2;
1184 :
1185 1850 : for ( i = num_env_bands; i < nb_sfm; ++i )
1186 : {
1187 1728 : if ( Rsubband[i] != 0 )
1188 : {
1189 389 : delta = get_next_indice( st, bitsforDelta );
1190 389 : ynrm[i] += delta - ( 1 << ( bitsforDelta - 1 ) );
1191 :
1192 : /* safety check in case of bit errors */
1193 389 : if ( ynrm[i] < 0 || ynrm[i] > 39 )
1194 : {
1195 0 : ynrm[i] = 39;
1196 0 : st->BER_detect = 1;
1197 : }
1198 389 : add_bits_denv += bitsforDelta;
1199 : }
1200 : }
1201 :
1202 122 : update_rsubband( nb_sfm, Rsubband, add_bits_denv );
1203 : }
1204 :
1205 80945 : 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 29870 : 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 29870 : max_delta = -100;
1233 29870 : calc_norm( t_audio, ynrm_t, normqlg2_t, 0, nb_sfm, sfmsize, sfm_start );
1234 29870 : add_bits_denv = 0;
1235 469429 : for ( i = num_env_bands; i < nb_sfm; ++i )
1236 : {
1237 439559 : if ( Rsubband[i] != 0 )
1238 : {
1239 152 : delta = ynrm_t[i] - ynrm[i];
1240 152 : if ( delta > 0 )
1241 : {
1242 1 : delta += 1;
1243 : }
1244 : else
1245 : {
1246 151 : delta = -delta;
1247 : }
1248 152 : if ( delta > max_delta )
1249 : {
1250 59 : max_delta = delta;
1251 : }
1252 : }
1253 : }
1254 29870 : if ( core_sfm >= num_env_bands )
1255 : {
1256 47 : if ( max_delta < 16 )
1257 : {
1258 47 : bitsforDelta = 2;
1259 83 : while ( max_delta >= 2 )
1260 : {
1261 36 : bitsforDelta++;
1262 36 : max_delta >>= 1;
1263 : }
1264 : }
1265 : else
1266 : {
1267 0 : bitsforDelta = 5;
1268 : }
1269 47 : max_delta = ( 1 << ( bitsforDelta - 1 ) ) - 1;
1270 47 : min_delta = ( max_delta + 1 ) * ( -1 );
1271 :
1272 : /* updating norm & storing delta norm */
1273 47 : add_bits_denv = 2;
1274 47 : push_indice( hBstr, IND_DELTA_ENV_HQ, bitsforDelta - 2, 2 );
1275 695 : for ( i = num_env_bands; i < nb_sfm; ++i )
1276 : {
1277 648 : if ( Rsubband[i] != 0 )
1278 : {
1279 152 : delta = ynrm_t[i] - ynrm[i];
1280 152 : if ( delta > max_delta )
1281 : {
1282 0 : delta = max_delta;
1283 : }
1284 152 : else if ( delta < min_delta )
1285 : {
1286 0 : delta = min_delta;
1287 : }
1288 152 : push_indice( hBstr, IND_DELTA_ENV_HQ, delta - min_delta, bitsforDelta );
1289 152 : ynrm[i] += delta;
1290 152 : add_bits_denv += bitsforDelta;
1291 : }
1292 : }
1293 :
1294 : /* updating bit allocation */
1295 47 : update_rsubband( nb_sfm, Rsubband, add_bits_denv );
1296 : }
1297 :
1298 29870 : return add_bits_denv;
1299 : }
1300 :
1301 : /*-------------------------------------------------------------------*
1302 : * hq_generic_bwe()
1303 : *
1304 : * HQ GENERIC BWE
1305 : *--------------------------------------------------------------------------*/
1306 :
1307 80945 : 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 80945 : n_swb_overlap_offset = swb_bwe_subband[0] + hq_generic_offset;
1325 :
1326 80945 : n_swb_overlap = sfm_end[num_env_bands - 1] - n_swb_overlap_offset;
1327 80945 : 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 80945 : 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 80945 : 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 80945 : return;
1334 : }
1335 :
1336 :
1337 : /*--------------------------------------------------------------------------*
1338 : * hq_wb_nf_bwe()
1339 : *
1340 : * HQ WB noisefill and BWE
1341 : *--------------------------------------------------------------------------*/
1342 :
1343 32450 : 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 32450 : 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 32450 : if ( is_transient == 0 )
1382 : {
1383 31331 : if ( prev_bfi == 1 )
1384 : {
1385 1431 : mvr2r( normq_v, prev_normq, SFM_N_WB );
1386 : }
1387 :
1388 : /* the variance of bit allocation */
1389 31331 : total_bit = 0;
1390 31331 : bitalloc_var = 0.0f;
1391 566845 : for ( sfm = 8; sfm <= last_sfm; sfm++ )
1392 : {
1393 535514 : bitalloc_var += (float) abs( R[sfm] - R[sfm - 1] );
1394 535514 : total_bit += R[sfm];
1395 : }
1396 31331 : bitalloc_var = ( last_sfm > 8 && total_bit > 0 ) ? ( bitalloc_var / total_bit ) : 0;
1397 :
1398 : /* calculate the peak-average ratio of saturable subbands */
1399 31331 : num = 0;
1400 31331 : sharp = EPSILON;
1401 566845 : for ( sfm = last_sfm; sfm >= 8; sfm-- )
1402 : {
1403 535514 : if ( R[sfm] >= rat[sfm] * sfmsize[sfm] )
1404 : {
1405 160369 : peak = 0.0f;
1406 160369 : mean = EPSILON;
1407 1974801 : for ( i = sfm_start[sfm]; i < sfm_end[sfm]; i++ )
1408 : {
1409 1814432 : fabs_coeff_out = (float) fabs( coeff_out[i] );
1410 1814432 : mean += fabs_coeff_out;
1411 1814432 : if ( fabs_coeff_out > peak )
1412 : {
1413 273051 : peak = fabs_coeff_out;
1414 : }
1415 : }
1416 160369 : sharp += sfmsize[sfm] * peak / mean;
1417 160369 : num++;
1418 : }
1419 : }
1420 :
1421 31331 : sharp = ( num != 0 ) ? 2.0f * num / sharp : 1.0f;
1422 31331 : harm_para = sharp;
1423 31331 : if ( last_sfm == 0 )
1424 : {
1425 0 : step = 0;
1426 : }
1427 : else
1428 : {
1429 31331 : step = 5.0f * sharp / last_sfm;
1430 : }
1431 31331 : alfa = 2.5f;
1432 :
1433 : /* fill noise for the insaturable subbands */
1434 845937 : for ( sfm = 0; sfm < num_sfm; sfm++ )
1435 : {
1436 814606 : env = 0.0f;
1437 814606 : if ( R[sfm] != 0 && R[sfm] < 1.5f * sfmsize[sfm] )
1438 : {
1439 : /* calculate the energy of the undecoded coefficients */
1440 410982 : peak = 0.0f;
1441 410982 : min_coef = FLT_MAX;
1442 410982 : env = normq_v[sfm] * normq_v[sfm] * sfmsize[sfm];
1443 6289294 : for ( i = sfm_start[sfm]; i < sfm_end[sfm]; i++ )
1444 : {
1445 5878312 : fabs_coeff_out = (float) fabs( coeff_out[i] );
1446 5878312 : if ( fabs_coeff_out < min_coef && coeff_out[i] != 0 )
1447 : {
1448 463403 : min_coef = fabs_coeff_out;
1449 : }
1450 5878312 : if ( fabs_coeff_out > peak )
1451 : {
1452 475025 : peak = fabs_coeff_out;
1453 : }
1454 5878312 : env -= coeff_out[i] * coeff_out[i];
1455 : }
1456 :
1457 410982 : if ( env > 0 )
1458 : {
1459 410982 : if ( sfm == 0 )
1460 : {
1461 2790 : avrg_norm = normq_v[0] + normq_v[1] + normq_v[2];
1462 2790 : prev_avrg_norm = prev_normq[0] + prev_normq[1] + prev_normq[2];
1463 : }
1464 408192 : else if ( sfm == 25 )
1465 : {
1466 23011 : avrg_norm = normq_v[23] + normq_v[24] + normq_v[25];
1467 23011 : prev_avrg_norm = prev_normq[23] + prev_normq[24] + prev_normq[25];
1468 : }
1469 : else
1470 : {
1471 385181 : avrg_norm = normq_v[sfm - 1] + normq_v[sfm] + normq_v[sfm + 1];
1472 385181 : prev_avrg_norm = prev_normq[sfm - 1] + prev_normq[sfm] + prev_normq[sfm + 1];
1473 : }
1474 :
1475 410982 : if ( bitalloc_var > 0.3f || 4.0f * normq_v[sfm] < peak )
1476 : {
1477 : /* calculate the noise magnitude of harmonic signal */
1478 394404 : env = (float) ( avrg_norm * harm_para * sqrt( env / sfmsize[sfm] ) / peak );
1479 : }
1480 : else
1481 : {
1482 : /* calculate the noise magnitude of normal signal */
1483 16578 : env = sharp * (float) sqrt( env / sfmsize[sfm] );
1484 16578 : if ( alfa * normq_v[sfm] < peak )
1485 : {
1486 151 : env *= env / peak;
1487 : }
1488 16578 : sharp += step;
1489 : }
1490 410982 : if ( env > 0.5f * min_coef )
1491 : {
1492 192494 : env = 0.5f * min_coef;
1493 : }
1494 :
1495 410982 : if ( prev_bfi == 1 )
1496 : {
1497 19907 : prev_env[sfm] = env;
1498 : }
1499 : /* smooth the noise magnitudes between inter-frame */
1500 410982 : if ( prev_avrg_norm > 0.5f * avrg_norm && prev_avrg_norm < 2.0f * avrg_norm && prev_is_transient == 0 )
1501 : {
1502 328624 : env = 0.5f * env + 0.5f * prev_env[sfm];
1503 : }
1504 :
1505 6289294 : for ( i = sfm_start[sfm]; i < sfm_end[sfm]; i++ )
1506 : {
1507 5878312 : if ( coeff[i] == 0 )
1508 : {
1509 4835431 : coeff_out[i] = (float) ( own_random( bwe_seed ) ) / PCM16_TO_FLT_FAC;
1510 4835431 : coeff_out[i] *= env;
1511 : }
1512 : }
1513 : }
1514 : else
1515 : {
1516 0 : env = 0.0f;
1517 : }
1518 : }
1519 403624 : else if ( R[sfm] == 0 )
1520 : {
1521 : /* fill random noise for 0 bit subbands */
1522 1878236 : for ( i = sfm_start[sfm]; i < sfm_end[sfm]; i++ )
1523 : {
1524 1763408 : if ( coeff[i] == 0 )
1525 : {
1526 806615 : coeff_out[i] = (float) ( own_random( bwe_seed ) ) / PCM16_TO_FLT_FAC;
1527 806615 : coeff_out[i] *= normq_v[sfm];
1528 : }
1529 : }
1530 :
1531 114828 : env = normq_v[sfm];
1532 : }
1533 814606 : 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 807 : float *p_prev_coeff_out = prev_coeff_out;
1536 16947 : for ( i = sfm_start[sfm] + 12; i < sfm_end[sfm]; i++ )
1537 : {
1538 16140 : if ( fabs( coeff_out[i] ) > 4.0f * fabs( *p_prev_coeff_out ) ||
1539 14004 : fabs( coeff_out[i] ) < 0.25f * fabs( *p_prev_coeff_out ) ||
1540 11710 : ( R[sfm] * ( *prev_R ) == 0 && R[sfm] + ( *prev_R ) != 0 ) )
1541 : {
1542 6436 : 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 16140 : p_prev_coeff_out++;
1545 : }
1546 : }
1547 :
1548 814606 : prev_env[sfm] = env;
1549 : }
1550 : }
1551 : else
1552 : {
1553 : /* fill random noise for 0 bit subbands of transient frame */
1554 30213 : for ( sfm = 0; sfm < num_sfm; sfm++ )
1555 : {
1556 29094 : if ( R[sfm] == 0 )
1557 : {
1558 130603 : for ( i = sfm_start[sfm]; i < sfm_end[sfm]; i++ )
1559 : {
1560 121296 : coeff_out[i] = (float) ( own_random( bwe_seed ) ) / PCM16_TO_FLT_FAC;
1561 121296 : coeff_out[i] *= normq_v[sfm];
1562 : }
1563 : }
1564 : }
1565 :
1566 1119 : set_f( prev_env, 0, SFM_N_WB );
1567 : }
1568 :
1569 32450 : mvr2r( normq_v, prev_normq, SFM_N_WB );
1570 32450 : mvr2r( coeff_out + L_FRAME16k - L_HQ_WB_BWE, prev_coeff_out, L_HQ_WB_BWE );
1571 32450 : *prev_R = R[SFM_N_WB - 1];
1572 :
1573 32450 : 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 254401 : 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 254401 : if ( hqswb_clas != HQ_HVQ )
1596 : {
1597 221326 : if ( ynrm[0] == 31 )
1598 : {
1599 47301 : for ( j = sfm_start[0]; j < sfm_end[0]; j++ )
1600 : {
1601 42050 : coefsq[j] = 0.0f;
1602 : }
1603 : }
1604 :
1605 8417072 : for ( i = 1; i < nb_sfm; i++ )
1606 : {
1607 8195746 : if ( ynrm[i] == 39 )
1608 : {
1609 8924377 : for ( j = sfm_start[i]; j < sfm_end[i]; j++ )
1610 : {
1611 8592194 : coefsq[j] = 0.0f;
1612 : }
1613 : }
1614 : }
1615 : }
1616 :
1617 254401 : return;
1618 : }
1619 :
1620 :
1621 : /*--------------------------------------------------------------------------*
1622 : * overlap_hq_bwe()
1623 : *
1624 : * Overlapping at the boundary between HQ core and BWE
1625 : *--------------------------------------------------------------------------*/
1626 :
1627 80945 : 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 80945 : if ( R[num_env_bands - 1] != 0 )
1644 : {
1645 38348 : mvr2r( hq_swb_overlap_buf, &coeff_out[n_swb_overlap_offset], n_swb_overlap );
1646 : }
1647 : else
1648 : {
1649 : /*weighting = 0.8f;*/
1650 42597 : step = 1.0f / (float) n_swb_overlap;
1651 42597 : weighting = 1.0f;
1652 387613 : for ( i = 0; i < n_swb_overlap; i++ )
1653 : {
1654 345016 : coeff_out[n_swb_overlap_offset + i] = hq_swb_overlap_buf[i] * weighting + coeff_out[n_swb_overlap_offset + i] * ( 1.0f - weighting );
1655 345016 : weighting -= step;
1656 : }
1657 : }
1658 :
1659 :
1660 1268520 : for ( n_band = num_env_bands; n_band < num_sfm; n_band++ )
1661 : {
1662 1187575 : if ( R[n_band] != 0 )
1663 : {
1664 9725 : for ( i = sfm_end[n_band - 1]; i < sfm_end[n_band]; ++i )
1665 : {
1666 9336 : coeff_out[i] = hq_swb_overlap_buf[i - n_swb_overlap_offset];
1667 : }
1668 : }
1669 : }
1670 :
1671 80945 : return;
1672 : }
1673 :
1674 :
1675 : /*--------------------------------------------------------------------------*
1676 : * apply_envelope()
1677 : *
1678 : * Apply spectral envelope with envelope adjustments
1679 : *--------------------------------------------------------------------------*/
1680 :
1681 193679 : 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 193679 : len = num_sfm;
1702 193679 : if ( HQ_mode == HQ_GEN_SWB || HQ_mode == HQ_GEN_FB )
1703 : {
1704 80945 : len = last_sfm + 1;
1705 : }
1706 :
1707 193679 : if ( length == L_FRAME16k )
1708 : {
1709 1038744 : for ( sfm = 0; sfm < num_sfm; sfm++ )
1710 : {
1711 1000272 : normq_v[sfm] = dicn[norm[sfm]];
1712 1000272 : normq = normq_v[sfm] * norm_adj[sfm];
1713 :
1714 13311312 : for ( i = sfm_start[sfm]; i < sfm_end[sfm]; i++ )
1715 : {
1716 12311040 : coeff_out[i] = coeff[i] * normq;
1717 : }
1718 : }
1719 : }
1720 : else
1721 : {
1722 5500743 : for ( sfm = 0; sfm < len; sfm++ )
1723 : {
1724 5345536 : normq_v[sfm] = dicn[norm[sfm]];
1725 5345536 : normq_v[sfm] *= norm_adj[sfm];
1726 :
1727 5345536 : normq = normq_v[sfm];
1728 88378264 : for ( i = sfm_start[sfm]; i < sfm_end[sfm]; i++ )
1729 : {
1730 83032728 : coeff_out[i] = coeff[i] * normq;
1731 : }
1732 : }
1733 :
1734 155207 : if ( HQ_mode == HQ_GEN_SWB || HQ_mode == HQ_GEN_FB )
1735 : {
1736 2269597 : for ( sfm = 0; sfm <= last_sfm; sfm++ )
1737 : {
1738 2188652 : normq = normq_v[sfm];
1739 28813900 : for ( i = sfm_start[sfm]; i < sfm_end[sfm]; i++ )
1740 : {
1741 26625248 : coeff_out1[i] = coeff_out1[i] * normq;
1742 : }
1743 : }
1744 : }
1745 : }
1746 :
1747 193679 : return;
1748 : }
1749 :
1750 :
1751 : /*--------------------------------------------------------------------------*
1752 : * apply_envelope_enc()
1753 : *
1754 : * Apply spectral envelope without envelope adjustments and noisefill
1755 : *--------------------------------------------------------------------------*/
1756 :
1757 82571 : 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 3232061 : for ( sfm = 0; sfm < num_sfm; sfm++ )
1770 : {
1771 3149490 : normq = dicn[norm[sfm]];
1772 :
1773 58292170 : for ( i = sfm_start[sfm]; i < sfm_end[sfm]; i++ )
1774 : {
1775 55142680 : coeff[i] *= normq;
1776 : }
1777 : }
1778 :
1779 82571 : 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 708628 : 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 708628 : accX = *mx >> 1;
1801 708628 : accY = my >> 1;
1802 708628 : align = *ex - ey;
1803 708628 : if ( align < 0 )
1804 : {
1805 532227 : if ( align > -32 ) /* If align < -32, (accY >> (-align) = 0 */
1806 : {
1807 532227 : accX = accX + ( accY >> ( -align ) );
1808 : }
1809 : }
1810 : else
1811 : {
1812 176401 : if ( align < 32 ) /* If align > 32, (accX >> align) = 0 */
1813 : {
1814 176401 : accX = accY + ( accX >> align );
1815 : }
1816 : else
1817 : {
1818 0 : accX = accY;
1819 : }
1820 176401 : *ex = ey;
1821 : }
1822 708628 : expo = norm_l( accX ); /* aligned to BASOP */
1823 708628 : *mx = accX << expo;
1824 708628 : *ex = *ex + expo - 1;
1825 :
1826 708628 : return;
1827 : }
|