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 32485 : 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 32485 : lookback = L_swb_norm / 2;
70 32485 : env_index = 0;
71 677818 : for ( n_freq = 0; n_freq < lookback; n_freq++ )
72 : {
73 645333 : n_lag_now = lookback + n_freq;
74 :
75 : /* Apply MA filter */
76 645333 : envelope[env_index] = EPSILON;
77 22033791 : for ( n_lag = 0; n_lag < n_lag_now; n_lag++ )
78 : {
79 21388458 : envelope[env_index] += (float) fabs( SWB_signal[n_lag] );
80 : }
81 645333 : env_index++;
82 : }
83 :
84 32485 : n_lag_now = L_swb_norm;
85 :
86 5288448 : for ( n_freq = 0; n_freq < SWB_flength - L_swb_norm; n_freq++ )
87 : {
88 : /* Apply MA filter */
89 5255963 : envelope[env_index] = EPSILON;
90 209931012 : for ( n_lag = 0; n_lag < n_lag_now; n_lag++ )
91 : {
92 204675049 : envelope[env_index] += (float) fabs( SWB_signal[n_freq + n_lag] );
93 : }
94 5255963 : env_index++;
95 : }
96 :
97 693159 : for ( n_freq = SWB_flength - L_swb_norm, i = 0; n_freq < SWB_flength - lookback; n_freq++, i++ )
98 : {
99 660674 : n_lag_now = L_swb_norm - i;
100 :
101 : /* Apply MA filter */
102 660674 : envelope[env_index] = EPSILON;
103 23323152 : for ( n_lag = 0; n_lag < n_lag_now; n_lag++ )
104 : {
105 22662478 : envelope[env_index] += (float) fabs( SWB_signal[n_freq + n_lag] );
106 : }
107 660674 : env_index++;
108 : }
109 :
110 32485 : return;
111 : }
112 :
113 : /*--------------------------------------------------------------------------*
114 : * noise_level_calc()
115 : *
116 : * Calculate noise level and limited band
117 : *--------------------------------------------------------------------------*/
118 :
119 22110 : 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 22110 : nb_sfm = *limit;
131 22110 : ener_limit = 1e-5f;
132 22110 : *noise_level = 0.0f;
133 243210 : for ( i = 0; i < 10; i++ )
134 : {
135 221100 : ener_limit += wnorm[i];
136 221100 : *noise_level += (float) abs( wnorm[i + 1] - wnorm[i] );
137 : }
138 22110 : ener_sum = ener_limit;
139 :
140 441962 : for ( i = 10; i < ( nb_sfm - 1 ); i++ )
141 : {
142 419852 : ener_sum += wnorm[i];
143 419852 : *noise_level += (float) abs( wnorm[i + 1] - wnorm[i] );
144 : }
145 22110 : ener_sum += wnorm[nb_sfm - 1];
146 :
147 22110 : if ( core_brate < HQ_BWE_CROSSOVER_BRATE )
148 : {
149 18004 : fact = 0.885f;
150 : }
151 : else
152 : {
153 4106 : fact = 0.942f;
154 : }
155 :
156 22110 : i = 9;
157 366957 : while ( ener_limit < ener_sum * fact && i + 1 < nb_sfm )
158 : {
159 344847 : ener_limit += wnorm[++i];
160 : }
161 22110 : *limit = i;
162 :
163 : /* calculate noise level for spectrum filling */
164 22110 : *noise_level /= ener_sum;
165 22110 : if ( *noise_level < 0 )
166 : {
167 0 : *noise_level = 0.25f;
168 : }
169 :
170 22110 : *noise_level = 0.25f - *noise_level;
171 :
172 22110 : if ( *noise_level < 0.0f )
173 : {
174 2344 : *noise_level = 0.0f;
175 : }
176 :
177 22110 : 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 106173 : 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 106173 : cb_size = 0;
207 3164810 : for ( sfm = 0; sfm <= last_sfm; sfm++ )
208 : {
209 3058637 : if ( R[sfm] != 0 )
210 : {
211 2308519 : if ( flag_32K_env_ho )
212 : {
213 : /* Build compressed (+/- 1) noise-fill codebook */
214 189625 : sfm_base = sfm_start[sfm];
215 497512 : for ( i = 0; i < sfmsize[sfm] / 8; i++ )
216 : {
217 307887 : E_cb_vec = 0;
218 2770983 : for ( j = sfm_base + i * 8; j < sfm_base + ( i + 1 ) * 8; j++ )
219 : {
220 2463096 : if ( coeff[j] > 0.0f )
221 : {
222 444690 : CodeBook_mod[cb_size] = 1.0f;
223 444690 : E_cb_vec++;
224 : }
225 2018406 : else if ( coeff[j] < 0.0f )
226 : {
227 447012 : CodeBook_mod[cb_size] = -1.0f;
228 447012 : E_cb_vec++;
229 : }
230 : else
231 : {
232 1571394 : CodeBook_mod[cb_size] = 0.0f;
233 : }
234 2463096 : cb_size++;
235 : }
236 :
237 307887 : if ( E_cb_vec < 2 )
238 : {
239 64788 : cb_size -= 8;
240 : }
241 : }
242 : }
243 : else
244 : {
245 28831610 : for ( j = sfm_start[sfm]; j < sfm_end[sfm]; j++ )
246 : {
247 26712716 : CodeBook[cb_size] = coeff[j];
248 26712716 : cb_size++;
249 : }
250 : }
251 : }
252 : }
253 :
254 106173 : if ( flag_32K_env_ho )
255 : {
256 1955473 : for ( j = 0; j < cb_size; j++ )
257 : {
258 1944792 : if ( CodeBook_mod[j] != 0.0f )
259 : {
260 : /* Densify codebook */
261 848641 : 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 1096151 : CodeBook[j] = CodeBook_mod[cb_size - j - 1];
266 : }
267 : }
268 : }
269 :
270 106173 : 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 168516 : 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 168516 : core_sfm = nb_sfm - 1;
288 :
289 1830839 : for ( sfm = nb_sfm - 1; sfm >= 0; sfm-- )
290 : {
291 1830839 : if ( bitalloc[sfm] != 0 )
292 : {
293 168516 : core_sfm = sfm;
294 168516 : break;
295 : }
296 : }
297 :
298 168516 : return core_sfm;
299 : }
300 :
301 : /*--------------------------------------------------------------------------*
302 : * apply_noisefill_HQ()
303 : *
304 : * Inject noise in non-coded bands
305 : *--------------------------------------------------------------------------*/
306 :
307 106173 : 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 106173 : if ( length >= L_FRAME32k || core_brate != HQ_32k )
330 : {
331 : /* Read from codebook */
332 105835 : cb_pos = 0;
333 :
334 3156064 : for ( sfm = 0; sfm <= last_sfm; sfm++ )
335 : {
336 3050229 : if ( R[sfm] == 0 )
337 : {
338 749520 : if ( flag_32K_env_ho )
339 : {
340 106279 : E_cb_vec = 0.0f;
341 106279 : if ( sfm < 20 )
342 : {
343 821534 : for ( i = 0; i < sfmsize[sfm]; i++ )
344 : {
345 743272 : cb_buff[i] = CodeBook_mod[cb_pos];
346 743272 : E_cb_vec += cb_buff[i] * cb_buff[i];
347 743272 : cb_pos++;
348 743272 : if ( cb_pos >= cb_size )
349 : {
350 1046 : cb_pos = 0;
351 : }
352 : }
353 : }
354 : else
355 : {
356 605489 : for ( i = 0; i < sfmsize[sfm]; i++ )
357 : {
358 577472 : cb_buff[i] = CodeBook[cb_pos];
359 577472 : E_cb_vec += cb_buff[i] * cb_buff[i];
360 577472 : cb_pos++;
361 577472 : if ( cb_pos >= cb_size )
362 : {
363 3578 : cb_pos = 0;
364 : }
365 : }
366 : }
367 :
368 106279 : E_corr = E_cb_vec / ( (float) sfmsize[sfm] );
369 106279 : E_corr = 1.0f / (float) sqrt( E_corr );
370 :
371 1427023 : for ( j = sfm_start[sfm]; j < sfm_end[sfm]; j++ )
372 : {
373 1320744 : coeff[j] = cb_buff[j - sfm_start[sfm]] * E_corr;
374 : }
375 : }
376 : else
377 : {
378 11554337 : for ( j = sfm_start[sfm]; j < sfm_end[sfm]; j++ )
379 : {
380 10911096 : coeff[j] = CodeBook[cb_pos];
381 10911096 : cb_pos++;
382 10911096 : cb_pos = ( cb_pos >= cb_size ) ? 0 : cb_pos;
383 : }
384 : }
385 : }
386 : }
387 : }
388 :
389 106173 : return;
390 : }
391 :
392 :
393 : /*--------------------------------------------------------------------------*
394 : * harm_bwe_fine()
395 : *
396 : * Prepare harmonic BWE fine structure
397 : *--------------------------------------------------------------------------*/
398 :
399 13389 : 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 13389 : int16_t norm_width = 64;
421 :
422 : /* shape the spectrum */
423 340016 : for ( sfm = 0; sfm <= last_sfm; sfm++ )
424 : {
425 326627 : if ( R[sfm] != 0 )
426 : {
427 278340 : normq = dicn[norm[sfm]];
428 3772580 : for ( i = sfm_start[sfm]; i < sfm_end[sfm]; i++ )
429 : {
430 3494240 : coeff_out[i] = coeff[i] * normq;
431 : }
432 : }
433 : else
434 : {
435 666415 : for ( i = sfm_start[sfm]; i < sfm_end[sfm]; i++ )
436 : {
437 618128 : coeff_out[i] = 0.0f;
438 : }
439 : }
440 : }
441 :
442 : /* excitation replication */
443 13389 : mvr2r( coeff_out, SWB_signal, L_HARMONIC_EXC );
444 13389 : calc_normal_length( HQ_CORE, coeff_out, HQ_HARMONIC, -1, &norm_width, prev_L_swb_norm );
445 13389 : hq_swb_harmonic_calc_norm_envelop( SWB_signal, envelope, norm_width, L_HARMONIC_EXC );
446 :
447 : /* Normalize with envelope */
448 2717967 : for ( i = 0; i < L_HARMONIC_EXC; i++ )
449 : {
450 2704578 : SWB_signal[i] = SWB_signal[i] / envelope[i];
451 : }
452 :
453 13389 : dst = coeff_fine + sfm_end[last_sfm];
454 13389 : end = coeff_fine + sfm_end[num_sfm - 1];
455 :
456 13389 : if ( ( sfm_end[last_sfm] - sfm_end[high_sfm] ) <= L_HARMONIC_EXC - START_EXC )
457 : {
458 11450 : src = SWB_signal + START_EXC + ( sfm_end[last_sfm] - sfm_end[high_sfm] );
459 : }
460 : else
461 : {
462 1939 : src = SWB_signal + L_HARMONIC_EXC - 1;
463 : }
464 :
465 44139 : while ( dst < end )
466 : {
467 2930378 : while ( dst < end && src < &SWB_signal[L_HARMONIC_EXC] )
468 : {
469 2899628 : *dst++ = *src++;
470 : }
471 30750 : src--;
472 :
473 2658114 : while ( dst < end && src >= &SWB_signal[START_EXC] )
474 : {
475 2627364 : *dst++ = *src--;
476 : }
477 30750 : src++;
478 : }
479 :
480 13389 : return;
481 : }
482 :
483 : /*--------------------------------------------------------------------------*
484 : * hvq_bwe_fine()
485 : *
486 : * Prepare HVQ BWE fine structure
487 : *--------------------------------------------------------------------------*/
488 :
489 19096 : 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 19096 : int16_t norm_width = 64;
508 :
509 : /* excitation replication */
510 19096 : mvr2r( coeff, SWB_signal, L_HARMONIC_EXC );
511 19096 : calc_normal_length( HQ_CORE, coeff, HQ_HVQ, -1, &norm_width, prev_L_swb_norm );
512 :
513 19096 : hq_swb_harmonic_calc_norm_envelop( SWB_signal, envelope, norm_width, L_HARMONIC_EXC );
514 :
515 : /* Normalize with envelope */
516 3876488 : for ( i = 0; i < L_HARMONIC_EXC; i++ )
517 : {
518 3857392 : SWB_signal[i] = SWB_signal[i] / envelope[i];
519 : }
520 :
521 19096 : dst = coeff_fine;
522 19096 : end = coeff_fine + sfm_end[num_sfm - 1] - sfm_end[last_sfm];
523 :
524 19096 : src = SWB_signal + START_EXC;
525 19096 : peak_src = peak_pos + START_EXC;
526 :
527 321572 : for ( i = 0; i < Npeaks; i++ )
528 : {
529 302476 : if ( peak_idx[i] < L_HARMONIC_EXC )
530 : {
531 253552 : peak_pos[peak_idx[i]] = 1;
532 : }
533 : }
534 :
535 19096 : i = L_HARMONIC_EXC - 1;
536 384062 : while ( i-- > 0 )
537 : {
538 384062 : if ( peak_pos[i] == 1 )
539 : {
540 19096 : break;
541 : }
542 : }
543 :
544 19096 : if ( i < 180 )
545 : {
546 4994 : i = 180;
547 : }
548 :
549 320491 : for ( j = L_HARMONIC_EXC - 1; j > i + 1; j-- )
550 : {
551 301395 : SWB_signal[j] = 0.0f;
552 : }
553 :
554 19096 : peak_dst = bwe_peaks + sfm_end[last_sfm];
555 63770 : while ( dst < end )
556 : {
557 5285244 : while ( dst < end && src < &SWB_signal[L_HARMONIC_EXC] )
558 : {
559 5240570 : *dst++ = *src++;
560 5240570 : *peak_dst++ = *peak_src++;
561 : }
562 44674 : peak_src--;
563 44674 : src--;
564 :
565 3871528 : while ( dst < end && src >= &SWB_signal[START_EXC] )
566 : {
567 3826854 : *dst++ = *src--;
568 3826854 : *peak_dst++ = *peak_src--;
569 : }
570 44674 : peak_src++;
571 44674 : src++;
572 : }
573 :
574 19096 : return;
575 : }
576 :
577 : /*--------------------------------------------------------------------------*
578 : * hq_fold_bwe()
579 : *
580 : * HQ mode folding BWE
581 : *--------------------------------------------------------------------------*/
582 :
583 48317 : 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 48317 : low_coeff = sfm_end[last_sfm] >> 1;
596 48317 : src = coeff + sfm_end[last_sfm] - 1;
597 :
598 48317 : first_coeff = sfm_end[last_sfm];
599 48317 : dst = coeff + sfm_end[last_sfm];
600 48317 : end = coeff + sfm_end[num_sfm - 1];
601 :
602 : /* Generate BWE with spectral folding */
603 78679 : while ( dst < end )
604 : {
605 3841180 : while ( dst < end && src >= &coeff[low_coeff] )
606 : {
607 3810818 : *dst++ = *src--;
608 : }
609 :
610 30362 : src++;
611 :
612 1976148 : while ( dst < end && src < &coeff[first_coeff] )
613 : {
614 1945786 : *dst++ = *src++;
615 : }
616 : }
617 :
618 48317 : return;
619 : }
620 :
621 : /*--------------------------------------------------------------------------*
622 : * apply_nf_gain()
623 : *
624 : * Apply noise fill gain
625 : *--------------------------------------------------------------------------*/
626 :
627 100292 : 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 100292 : nf_scale = 1.0f / ( 1 << nf_idx );
641 3012667 : for ( sfm = 0; sfm <= last_sfm; sfm++ )
642 : {
643 2912375 : if ( R[sfm] == 0 )
644 : {
645 12827261 : for ( j = sfm_start[sfm]; j < sfm_end[sfm]; j++ )
646 : {
647 : /* Scale NoiseFill */
648 12088492 : coeff[j] = coeff[j] * nf_scale;
649 : }
650 : }
651 : }
652 :
653 100292 : return;
654 : }
655 :
656 : /*--------------------------------------------------------------------------*
657 : * hq_generic_fine()
658 : *
659 : * Prepare HQ SWB BWE fine structure
660 : *--------------------------------------------------------------------------*/
661 :
662 39084 : 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 39084 : multi = 1.0f;
676 1097136 : for ( sfm = 0; sfm <= last_sfm; sfm++ )
677 : {
678 13944420 : for ( i = sfm_start[sfm]; i < sfm_end[sfm]; i++ )
679 : {
680 12886368 : if ( coeff[i] == 0.f )
681 : {
682 7284370 : coeff_out1[i] = (float) multi * ( own_random( bwe_seed ) > 0 ? 1.0f : -1.0f ) * 0.5f;
683 : }
684 : else
685 : {
686 5601998 : coeff_out1[i] = coeff[i];
687 : }
688 : }
689 : }
690 :
691 39084 : return;
692 : }
693 :
694 : /*--------------------------------------------------------------------------*
695 : * harm_bwe()
696 : *
697 : * HQ Harmonic BWE
698 : *--------------------------------------------------------------------------*/
699 :
700 13389 : 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 13389 : float alfa = 0.5f;
725 : float alpha, beta;
726 : int16_t idx;
727 : float fac;
728 : float *src, *dst;
729 :
730 340016 : for ( sfm = 0; sfm <= last_sfm; sfm++ )
731 : {
732 326627 : if ( R[sfm] == 0 )
733 : {
734 48287 : normq = dicn[norm[sfm]];
735 666415 : for ( i = sfm_start[sfm]; i < sfm_end[sfm]; i++ )
736 : {
737 618128 : coeff_out[i] = coeff[i] * normq;
738 : }
739 : }
740 : }
741 13389 : noise_level[1] = noise_level[0];
742 :
743 : /* shaping the BWE spectrum further by envelopes and noise factors */
744 13389 : noise_level[0] = 0.9f * prev_noise_level[0] + 0.1f * noise_level[0];
745 13389 : noise_level[1] = 0.9f * prev_noise_level[1] + 0.1f * noise_level[1];
746 :
747 13389 : if ( prev_hq_mode == HQ_NORMAL || prev_hq_mode == HQ_GEN_SWB )
748 : {
749 3482 : if ( noise_level[0] < 0.25f )
750 : {
751 3482 : noise_level[0] *= 4.0f;
752 : }
753 :
754 3482 : if ( noise_level[1] < 0.25f )
755 : {
756 3482 : noise_level[1] *= 4.0f;
757 : }
758 : }
759 :
760 13389 : E_L = EPSILON;
761 115201 : for ( i = last_sfm + 1; i < num_sfm; i++ )
762 : {
763 101812 : E_L = EPSILON;
764 5628804 : for ( j = sfm_start[i]; j < sfm_end[i]; j++ )
765 : {
766 5526992 : E_L += coeff_fine[j] * coeff_fine[j];
767 : }
768 101812 : E_L = (float) sqrt( ( sfm_end[i] - sfm_start[i] ) / E_L );
769 :
770 101812 : normq = dicn[norm[i]];
771 101812 : norm_adj = normq * E_L;
772 101812 : alfa = ( i > 27 ) ? noise_level[1] : noise_level[0];
773 101812 : beta = (float) sqrt( 1.0f - alfa );
774 101812 : if ( element_mode > EVS_MONO )
775 : {
776 99511 : alpha = (float) sqrt( alfa );
777 : }
778 : else
779 : {
780 2301 : alpha = (float) sqrt( alfa ) * 0.5f;
781 : }
782 :
783 5628804 : for ( sfm = sfm_start[i]; sfm < sfm_end[i]; sfm++ )
784 : {
785 5526992 : coeff_out[sfm] = ( beta * coeff_fine[sfm] * norm_adj + alpha * own_random( bwe_seed ) / PCM16_TO_FLT_FAC * normq );
786 : }
787 : }
788 :
789 13389 : prev_noise_level[0] = noise_level[0];
790 13389 : prev_noise_level[1] = noise_level[1];
791 13389 : idx = 16;
792 13389 : src = &coeff_out[sfm_end[high_sfm] + L_HARMONIC_EXC - START_EXC];
793 13389 : dst = src - 1;
794 :
795 227613 : for ( i = 0; i < idx; i++ )
796 : {
797 214224 : fac = i / ( 2.0f * idx );
798 214224 : *src++ *= 0.5f + fac;
799 214224 : *dst-- *= 0.5f + fac;
800 : }
801 13389 : if ( num_sfm == 33 )
802 : {
803 6690 : set_f( &coeff_out[800], 0, 160 );
804 : }
805 13389 : return;
806 : }
807 :
808 : /*--------------------------------------------------------------------------*
809 : * HVQ_bwe()
810 : *
811 : * HQ HVQ BWE
812 : *--------------------------------------------------------------------------*/
813 :
814 19096 : 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 19096 : int16_t bwe_noise_th = 0;
840 : int16_t peak_band, low, high, sel_norm;
841 : int16_t norm_ind;
842 19096 : float tmp_norm = 0;
843 : int16_t idx;
844 : float fac;
845 : float *src, *dst;
846 : int16_t istart, iend;
847 19096 : int16_t offset = sfm_end[last_sfm];
848 :
849 19096 : mvr2r( coeff, coeff_out, L_FRAME48k );
850 :
851 19096 : bwe_noise_th = bin_th + ( sfm_end[num_sfm - 1] - bin_th ) / HVQ_BWE_NOISE_BANDS;
852 19096 : 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 19096 : noise_level[0] = 0.9f * prev_noise_level[0] + 0.1f * noise_level[0];
856 19096 : noise_level[1] = 0.9f * prev_noise_level[1] + 0.1f * noise_level[1];
857 :
858 19096 : if ( prev_hq_mode == HQ_NORMAL || prev_hq_mode == HQ_GEN_SWB )
859 : {
860 1315 : if ( noise_level[0] < 0.25f )
861 : {
862 1315 : noise_level[0] *= 4.0f;
863 : }
864 :
865 1315 : if ( noise_level[1] < 0.25f )
866 : {
867 1315 : noise_level[1] *= 4.0f;
868 : }
869 : }
870 :
871 19096 : norm_ind = last_sfm + 1;
872 :
873 19096 : if ( core_brate < HQ_BWE_CROSSOVER_BRATE )
874 : {
875 13984 : peak_band = 0;
876 13984 : E_L = EPSILON;
877 908960 : for ( i = sfm_start[norm_ind]; i < sfm_end[norm_ind + 1]; i++ )
878 : {
879 894976 : if ( bwe_peaks[i] )
880 : {
881 33293 : peak_band = 1;
882 : }
883 894976 : E_L += coeff_fine[i - offset] * coeff_fine[i - offset];
884 : }
885 13984 : E_L = (float) sqrt( ( sfm_end[norm_ind + 1] - sfm_start[norm_ind] ) / E_L );
886 :
887 13984 : normq = 0.1f * dicn[norm[norm_ind - 1]] + 0.8f * dicn[norm[norm_ind]] + 0.1f * dicn[norm[norm_ind + 1]];
888 13984 : tmp_norm = 0.1f * dicn[norm[norm_ind]] + 0.8f * dicn[norm[norm_ind + 1]] + 0.1f * dicn[norm[norm_ind + 2]];
889 :
890 13984 : istart = sfm_start[norm_ind];
891 13984 : iend = istart + sfm_len[norm_ind] / 2;
892 237728 : for ( i = istart; i < iend; i++ )
893 : {
894 223744 : 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 13984 : j = 0;
898 13984 : N = sfm_len[norm_ind] / 2 + sfm_len[norm_ind + 1] / 2 - 1;
899 13984 : istart = iend;
900 13984 : iend = sfm_start[norm_ind + 1] + sfm_len[norm_ind + 1] / 2;
901 461472 : for ( i = istart; i < iend; i++ )
902 : {
903 447488 : 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 447488 : j++;
905 : }
906 :
907 13984 : istart = iend;
908 13984 : iend = sfm_end[norm_ind + 1];
909 237728 : for ( i = istart; i < iend; i++ )
910 : {
911 223744 : 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 13984 : norm_ind += 2;
915 : }
916 :
917 167834 : for ( ; norm_ind < num_sfm; norm_ind++ )
918 : {
919 148738 : if ( R[norm_ind] == 0 )
920 : {
921 146848 : peak_band = 0;
922 146848 : E_L = EPSILON;
923 :
924 2789324 : for ( i = sfm_start[norm_ind]; i < sfm_end[norm_ind]; i++ )
925 : {
926 2777613 : if ( bwe_peaks[i] )
927 : {
928 135137 : peak_band = 1;
929 135137 : break;
930 : }
931 : }
932 :
933 146848 : istart = sfm_start[norm_ind];
934 146848 : iend = sfm_end[norm_ind];
935 :
936 146848 : if ( peak_band == 1 && norm_ind > last_sfm + 1 && norm_ind < num_sfm - 1 )
937 : {
938 113106 : istart -= sfm_len[norm_ind - 1] / 2;
939 113106 : iend += sfm_len[norm_ind + 1] / 2;
940 : }
941 :
942 14152456 : for ( i = istart; i < iend; i++ )
943 : {
944 14005608 : E_L += coeff_fine[i - offset] * coeff_fine[i - offset];
945 : }
946 146848 : E_L = (float) sqrt( ( iend - istart ) / E_L );
947 :
948 146848 : if ( peak_band )
949 : {
950 135137 : if ( norm_ind + 1 > num_sfm - 1 )
951 : {
952 17876 : normq = 0.15f * dicn[norm[norm_ind - 1]] + 0.85f * dicn[norm[norm_ind]];
953 : }
954 : else
955 : {
956 117261 : 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 11711 : low = norm_ind;
962 11711 : high = min( norm_ind + 1, num_sfm - 1 );
963 11711 : sel_norm = norm[norm_ind - 1];
964 33913 : for ( j = low; j <= high; j++ )
965 : {
966 22202 : if ( norm[j] > sel_norm )
967 : {
968 9404 : sel_norm = norm[j];
969 : }
970 : }
971 11711 : normq = dicn[sel_norm];
972 : }
973 :
974 8237168 : for ( i = sfm_start[norm_ind]; i < sfm_end[norm_ind]; i++ )
975 : {
976 8090320 : 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 84018 : for ( i = sfm_start[norm_ind]; i < sfm_end[norm_ind]; i++ )
982 : {
983 82128 : coeff_out[i] = coeff[i]; /* Scaling already applied */
984 : }
985 : }
986 : }
987 :
988 19096 : prev_noise_level[0] = noise_level[0];
989 19096 : prev_noise_level[1] = noise_level[1];
990 19096 : idx = 16;
991 19096 : src = &coeff_out[sfm_end[last_sfm] + L_HARMONIC_EXC - START_EXC];
992 19096 : dst = src - 1;
993 :
994 324632 : for ( i = 0; i < idx; i++ )
995 : {
996 305536 : fac = i / ( 2.0f * idx );
997 305536 : *src++ *= 0.5f + fac;
998 305536 : *dst-- *= 0.5f + fac;
999 : }
1000 :
1001 19096 : 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 30503 : 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 30503 : s = 0;
1024 :
1025 75951 : for ( k = 0; k < pvq_bands; k++ )
1026 : {
1027 :
1028 45448 : if ( k >= pvq_bands - n_sel_bnds )
1029 : {
1030 5528 : hvq_band_start[k] = hvq_band_end[k - 1];
1031 5528 : hvq_band_width[k] = band_len_harm[sel_bnds[s]];
1032 5528 : hvq_band_end[k] = hvq_band_end[k - 1] + band_len_harm[sel_bnds[s]];
1033 5528 : s++;
1034 : }
1035 : else
1036 : {
1037 39920 : hvq_band_start[k] = k * HVQ_PVQ_COEFS;
1038 39920 : hvq_band_width[k] = HVQ_PVQ_COEFS;
1039 39920 : hvq_band_end[k] = ( k + 1 ) * HVQ_PVQ_COEFS;
1040 : }
1041 : }
1042 :
1043 30503 : return;
1044 : }
1045 :
1046 :
1047 : /*--------------------------------------------------------------------------*
1048 : * map_hq_generic_fenv_norm()
1049 : *
1050 : * mapping high frequency envelope to high band norm
1051 : *--------------------------------------------------------------------------*/
1052 :
1053 64620 : 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 64620 : set_f( env_fl, 0, 17 );
1067 64620 : if ( hq_generic_offset == 144 )
1068 : {
1069 1754 : env_fl[0] = hq_generic_fenv[1];
1070 1754 : env_fl[1] = hq_generic_fenv[2] * 0.6640625f + hq_generic_fenv[3] * 0.3359375f;
1071 1754 : env_fl[2] = hq_generic_fenv[3] * 0.6640625f + hq_generic_fenv[4] * 0.3359375f;
1072 1754 : env_fl[3] = hq_generic_fenv[4] * 0.3359375f + hq_generic_fenv[5] * 0.6640625f;
1073 1754 : env_fl[4] = hq_generic_fenv[5] * 0.3359375f + hq_generic_fenv[6] * 0.6640625f;
1074 1754 : env_fl[5] = hq_generic_fenv[7];
1075 1754 : env_fl[6] = hq_generic_fenv[8] * 0.75f + hq_generic_fenv[9] * 0.25f;
1076 1754 : env_fl[7] = hq_generic_fenv[9] * 0.75f + hq_generic_fenv[10] * 0.25f;
1077 1754 : env_fl[8] = hq_generic_fenv[10] * 0.25f + hq_generic_fenv[11] * 0.75f;
1078 : }
1079 : else
1080 : {
1081 62866 : env_fl[0] = hq_generic_fenv[0] * 0.3359375f + hq_generic_fenv[1] * 0.6640625f;
1082 62866 : env_fl[1] = hq_generic_fenv[1] * 0.3359375f + hq_generic_fenv[2] * 0.6640625f;
1083 62866 : env_fl[2] = hq_generic_fenv[3];
1084 62866 : env_fl[3] = hq_generic_fenv[4] * 0.6640625f + hq_generic_fenv[5] * 0.3359375f;
1085 62866 : env_fl[4] = hq_generic_fenv[5] * 0.6640625f + hq_generic_fenv[6] * 0.3359375f;
1086 62866 : env_fl[5] = hq_generic_fenv[6] * 0.3359375f + hq_generic_fenv[7] * 0.6640625f;
1087 62866 : env_fl[6] = hq_generic_fenv[7] * 0.3359375f + hq_generic_fenv[8] * 0.6640625f;
1088 62866 : env_fl[7] = hq_generic_fenv[8] * 0.3359375f + hq_generic_fenv[9] * 0.6640625f;
1089 62866 : env_fl[8] = hq_generic_fenv[9] * 0.3359375f + hq_generic_fenv[10] * 0.6640625f;
1090 62866 : env_fl[9] = hq_generic_fenv[10] * 0.25f + hq_generic_fenv[11] * 0.75f;
1091 62866 : env_fl[10] = hq_generic_fenv[12];
1092 62866 : env_fl[11] = hq_generic_fenv[13];
1093 : }
1094 :
1095 64620 : if ( hqswb_clas == HQ_GEN_FB )
1096 : {
1097 34150 : if ( hq_generic_offset == 144 )
1098 : {
1099 530 : env_fl[9] = hq_generic_fenv[12];
1100 530 : env_fl[10] = hq_generic_fenv[12] * 0.25f + hq_generic_fenv[13] * 0.75f;
1101 530 : env_fl[11] = hq_generic_fenv[13] * 0.5f + hq_generic_fenv[14] * 0.5f;
1102 530 : env_fl[12] = hq_generic_fenv[14];
1103 530 : env_fl[13] = hq_generic_fenv[14];
1104 : }
1105 : else
1106 : {
1107 33620 : env_fl[12] = hq_generic_fenv[14];
1108 33620 : env_fl[13] = hq_generic_fenv[14] * 0.25f + hq_generic_fenv[15] * 0.75f;
1109 33620 : env_fl[14] = hq_generic_fenv[15] * 0.5f + hq_generic_fenv[16] * 0.5f;
1110 33620 : env_fl[15] = hq_generic_fenv[16];
1111 33620 : env_fl[16] = hq_generic_fenv[16];
1112 : }
1113 : }
1114 :
1115 64620 : logqnorm_2( env_fl, 40, num_env_bands, nb_sfm, ynrm + num_env_bands, normqlg2 + num_env_bands, thren_HQ );
1116 :
1117 1005548 : for ( i = num_env_bands; i < nb_sfm; ++i )
1118 : {
1119 940928 : idx = min( ynrm[i] + 10, 39 );
1120 940928 : normqlg2[i] = dicnlg2[idx];
1121 : }
1122 :
1123 64620 : return;
1124 : }
1125 :
1126 :
1127 : /*-------------------------------------------------------------------*
1128 : * update_rsubband()
1129 : *
1130 : * Update subband bit allocation
1131 : *--------------------------------------------------------------------------*/
1132 :
1133 85 : 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 172 : while ( b_add_bits_denv > 0 )
1143 : {
1144 87 : i = nb_sfm - 1;
1145 2144 : while ( b_add_bits_denv > 0 && i >= 0 )
1146 : {
1147 2057 : if ( Rsubband[i] > 24 )
1148 : {
1149 837 : Rsubband[i] -= 8;
1150 837 : b_add_bits_denv--;
1151 : }
1152 2057 : i--;
1153 : }
1154 : }
1155 :
1156 85 : 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 39084 : 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 39084 : add_bits_denv = 0;
1179 39084 : if ( core_sfm >= num_env_bands )
1180 : {
1181 45 : bitsforDelta = get_next_indice( st, 2 );
1182 45 : bitsforDelta += 2;
1183 45 : add_bits_denv += 2;
1184 :
1185 679 : for ( i = num_env_bands; i < nb_sfm; ++i )
1186 : {
1187 634 : if ( Rsubband[i] != 0 )
1188 : {
1189 142 : delta = get_next_indice( st, bitsforDelta );
1190 142 : ynrm[i] += delta - ( 1 << ( bitsforDelta - 1 ) );
1191 :
1192 : /* safety check in case of bit errors */
1193 142 : if ( ynrm[i] < 0 || ynrm[i] > 39 )
1194 : {
1195 0 : ynrm[i] = 39;
1196 0 : st->BER_detect = 1;
1197 : }
1198 142 : add_bits_denv += bitsforDelta;
1199 : }
1200 : }
1201 :
1202 45 : update_rsubband( nb_sfm, Rsubband, add_bits_denv );
1203 : }
1204 :
1205 39084 : 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 25536 : 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 25536 : max_delta = -100;
1233 25536 : calc_norm( t_audio, ynrm_t, normqlg2_t, 0, nb_sfm, sfmsize, sfm_start );
1234 25536 : add_bits_denv = 0;
1235 394588 : for ( i = num_env_bands; i < nb_sfm; ++i )
1236 : {
1237 369052 : if ( Rsubband[i] != 0 )
1238 : {
1239 121 : delta = ynrm_t[i] - ynrm[i];
1240 121 : if ( delta > 0 )
1241 : {
1242 1 : delta += 1;
1243 : }
1244 : else
1245 : {
1246 120 : delta = -delta;
1247 : }
1248 121 : if ( delta > max_delta )
1249 : {
1250 49 : max_delta = delta;
1251 : }
1252 : }
1253 : }
1254 25536 : if ( core_sfm >= num_env_bands )
1255 : {
1256 40 : if ( max_delta < 16 )
1257 : {
1258 40 : bitsforDelta = 2;
1259 80 : while ( max_delta >= 2 )
1260 : {
1261 40 : bitsforDelta++;
1262 40 : max_delta >>= 1;
1263 : }
1264 : }
1265 : else
1266 : {
1267 0 : bitsforDelta = 5;
1268 : }
1269 40 : max_delta = ( 1 << ( bitsforDelta - 1 ) ) - 1;
1270 40 : min_delta = ( max_delta + 1 ) * ( -1 );
1271 :
1272 : /* updating norm & storing delta norm */
1273 40 : add_bits_denv = 2;
1274 40 : push_indice( hBstr, IND_DELTA_ENV_HQ, bitsforDelta - 2, 2 );
1275 614 : for ( i = num_env_bands; i < nb_sfm; ++i )
1276 : {
1277 574 : if ( Rsubband[i] != 0 )
1278 : {
1279 121 : delta = ynrm_t[i] - ynrm[i];
1280 121 : if ( delta > max_delta )
1281 : {
1282 0 : delta = max_delta;
1283 : }
1284 121 : else if ( delta < min_delta )
1285 : {
1286 0 : delta = min_delta;
1287 : }
1288 121 : push_indice( hBstr, IND_DELTA_ENV_HQ, delta - min_delta, bitsforDelta );
1289 121 : ynrm[i] += delta;
1290 121 : add_bits_denv += bitsforDelta;
1291 : }
1292 : }
1293 :
1294 : /* updating bit allocation */
1295 40 : update_rsubband( nb_sfm, Rsubband, add_bits_denv );
1296 : }
1297 :
1298 25536 : return add_bits_denv;
1299 : }
1300 :
1301 : /*-------------------------------------------------------------------*
1302 : * hq_generic_bwe()
1303 : *
1304 : * HQ GENERIC BWE
1305 : *--------------------------------------------------------------------------*/
1306 :
1307 39084 : 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 39084 : n_swb_overlap_offset = swb_bwe_subband[0] + hq_generic_offset;
1325 :
1326 39084 : n_swb_overlap = sfm_end[num_env_bands - 1] - n_swb_overlap_offset;
1327 39084 : 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 39084 : 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 39084 : 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 39084 : return;
1334 : }
1335 :
1336 :
1337 : /*--------------------------------------------------------------------------*
1338 : * hq_wb_nf_bwe()
1339 : *
1340 : * HQ WB noisefill and BWE
1341 : *--------------------------------------------------------------------------*/
1342 :
1343 21437 : 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 21437 : 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 21437 : if ( is_transient == 0 )
1382 : {
1383 20653 : if ( prev_bfi == 1 )
1384 : {
1385 1287 : mvr2r( normq_v, prev_normq, SFM_N_WB );
1386 : }
1387 :
1388 : /* the variance of bit allocation */
1389 20653 : total_bit = 0;
1390 20653 : bitalloc_var = 0.0f;
1391 377124 : for ( sfm = 8; sfm <= last_sfm; sfm++ )
1392 : {
1393 356471 : bitalloc_var += (float) abs( R[sfm] - R[sfm - 1] );
1394 356471 : total_bit += R[sfm];
1395 : }
1396 20653 : bitalloc_var = ( last_sfm > 8 && total_bit > 0 ) ? ( bitalloc_var / total_bit ) : 0;
1397 :
1398 : /* calculate the peak-average ratio of saturable subbands */
1399 20653 : num = 0;
1400 20653 : sharp = EPSILON;
1401 377124 : for ( sfm = last_sfm; sfm >= 8; sfm-- )
1402 : {
1403 356471 : if ( R[sfm] >= rat[sfm] * sfmsize[sfm] )
1404 : {
1405 106561 : peak = 0.0f;
1406 106561 : mean = EPSILON;
1407 1331329 : for ( i = sfm_start[sfm]; i < sfm_end[sfm]; i++ )
1408 : {
1409 1224768 : fabs_coeff_out = (float) fabs( coeff_out[i] );
1410 1224768 : mean += fabs_coeff_out;
1411 1224768 : if ( fabs_coeff_out > peak )
1412 : {
1413 181013 : peak = fabs_coeff_out;
1414 : }
1415 : }
1416 106561 : sharp += sfmsize[sfm] * peak / mean;
1417 106561 : num++;
1418 : }
1419 : }
1420 :
1421 20653 : sharp = ( num != 0 ) ? 2.0f * num / sharp : 1.0f;
1422 20653 : harm_para = sharp;
1423 20653 : if ( last_sfm == 0 )
1424 : {
1425 0 : step = 0;
1426 : }
1427 : else
1428 : {
1429 20653 : step = 5.0f * sharp / last_sfm;
1430 : }
1431 20653 : alfa = 2.5f;
1432 :
1433 : /* fill noise for the insaturable subbands */
1434 557631 : for ( sfm = 0; sfm < num_sfm; sfm++ )
1435 : {
1436 536978 : env = 0.0f;
1437 536978 : if ( R[sfm] != 0 && R[sfm] < 1.5f * sfmsize[sfm] )
1438 : {
1439 : /* calculate the energy of the undecoded coefficients */
1440 275837 : peak = 0.0f;
1441 275837 : min_coef = FLT_MAX;
1442 275837 : env = normq_v[sfm] * normq_v[sfm] * sfmsize[sfm];
1443 4253133 : for ( i = sfm_start[sfm]; i < sfm_end[sfm]; i++ )
1444 : {
1445 3977296 : fabs_coeff_out = (float) fabs( coeff_out[i] );
1446 3977296 : if ( fabs_coeff_out < min_coef && coeff_out[i] != 0 )
1447 : {
1448 311409 : min_coef = fabs_coeff_out;
1449 : }
1450 3977296 : if ( fabs_coeff_out > peak )
1451 : {
1452 318592 : peak = fabs_coeff_out;
1453 : }
1454 3977296 : env -= coeff_out[i] * coeff_out[i];
1455 : }
1456 :
1457 275837 : if ( env > 0 )
1458 : {
1459 275837 : if ( sfm == 0 )
1460 : {
1461 1407 : avrg_norm = normq_v[0] + normq_v[1] + normq_v[2];
1462 1407 : prev_avrg_norm = prev_normq[0] + prev_normq[1] + prev_normq[2];
1463 : }
1464 274430 : else if ( sfm == 25 )
1465 : {
1466 16028 : avrg_norm = normq_v[23] + normq_v[24] + normq_v[25];
1467 16028 : prev_avrg_norm = prev_normq[23] + prev_normq[24] + prev_normq[25];
1468 : }
1469 : else
1470 : {
1471 258402 : avrg_norm = normq_v[sfm - 1] + normq_v[sfm] + normq_v[sfm + 1];
1472 258402 : prev_avrg_norm = prev_normq[sfm - 1] + prev_normq[sfm] + prev_normq[sfm + 1];
1473 : }
1474 :
1475 275837 : if ( bitalloc_var > 0.3f || 4.0f * normq_v[sfm] < peak )
1476 : {
1477 : /* calculate the noise magnitude of harmonic signal */
1478 263311 : env = (float) ( avrg_norm * harm_para * sqrt( env / sfmsize[sfm] ) / peak );
1479 : }
1480 : else
1481 : {
1482 : /* calculate the noise magnitude of normal signal */
1483 12526 : env = sharp * (float) sqrt( env / sfmsize[sfm] );
1484 12526 : if ( alfa * normq_v[sfm] < peak )
1485 : {
1486 110 : env *= env / peak;
1487 : }
1488 12526 : sharp += step;
1489 : }
1490 275837 : if ( env > 0.5f * min_coef )
1491 : {
1492 128640 : env = 0.5f * min_coef;
1493 : }
1494 :
1495 275837 : if ( prev_bfi == 1 )
1496 : {
1497 17948 : prev_env[sfm] = env;
1498 : }
1499 : /* smooth the noise magnitudes between inter-frame */
1500 275837 : if ( prev_avrg_norm > 0.5f * avrg_norm && prev_avrg_norm < 2.0f * avrg_norm && prev_is_transient == 0 )
1501 : {
1502 219756 : env = 0.5f * env + 0.5f * prev_env[sfm];
1503 : }
1504 :
1505 4253133 : for ( i = sfm_start[sfm]; i < sfm_end[sfm]; i++ )
1506 : {
1507 3977296 : if ( coeff[i] == 0 )
1508 : {
1509 3273658 : coeff_out[i] = (float) ( own_random( bwe_seed ) ) / PCM16_TO_FLT_FAC;
1510 3273658 : coeff_out[i] *= env;
1511 : }
1512 : }
1513 : }
1514 : else
1515 : {
1516 0 : env = 0.0f;
1517 : }
1518 : }
1519 261141 : else if ( R[sfm] == 0 )
1520 : {
1521 : /* fill random noise for 0 bit subbands */
1522 1106588 : for ( i = sfm_start[sfm]; i < sfm_end[sfm]; i++ )
1523 : {
1524 1038024 : if ( coeff[i] == 0 )
1525 : {
1526 473229 : coeff_out[i] = (float) ( own_random( bwe_seed ) ) / PCM16_TO_FLT_FAC;
1527 473229 : coeff_out[i] *= normq_v[sfm];
1528 : }
1529 : }
1530 :
1531 68564 : env = normq_v[sfm];
1532 : }
1533 536978 : 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 472 : float *p_prev_coeff_out = prev_coeff_out;
1536 9912 : for ( i = sfm_start[sfm] + 12; i < sfm_end[sfm]; i++ )
1537 : {
1538 9440 : if ( fabs( coeff_out[i] ) > 4.0f * fabs( *p_prev_coeff_out ) ||
1539 8013 : fabs( coeff_out[i] ) < 0.25f * fabs( *p_prev_coeff_out ) ||
1540 6698 : ( R[sfm] * ( *prev_R ) == 0 && R[sfm] + ( *prev_R ) != 0 ) )
1541 : {
1542 4062 : 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 9440 : p_prev_coeff_out++;
1545 : }
1546 : }
1547 :
1548 536978 : prev_env[sfm] = env;
1549 : }
1550 : }
1551 : else
1552 : {
1553 : /* fill random noise for 0 bit subbands of transient frame */
1554 21168 : for ( sfm = 0; sfm < num_sfm; sfm++ )
1555 : {
1556 20384 : if ( R[sfm] == 0 )
1557 : {
1558 91979 : for ( i = sfm_start[sfm]; i < sfm_end[sfm]; i++ )
1559 : {
1560 85384 : coeff_out[i] = (float) ( own_random( bwe_seed ) ) / PCM16_TO_FLT_FAC;
1561 85384 : coeff_out[i] *= normq_v[sfm];
1562 : }
1563 : }
1564 : }
1565 :
1566 784 : set_f( prev_env, 0, SFM_N_WB );
1567 : }
1568 :
1569 21437 : mvr2r( normq_v, prev_normq, SFM_N_WB );
1570 21437 : mvr2r( coeff_out + L_FRAME16k - L_HQ_WB_BWE, prev_coeff_out, L_HQ_WB_BWE );
1571 21437 : *prev_R = R[SFM_N_WB - 1];
1572 :
1573 21437 : 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 125269 : 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 125269 : if ( hqswb_clas != HQ_HVQ )
1596 : {
1597 106173 : if ( ynrm[0] == 31 )
1598 : {
1599 12416 : for ( j = sfm_start[0]; j < sfm_end[0]; j++ )
1600 : {
1601 11038 : coefsq[j] = 0.0f;
1602 : }
1603 : }
1604 :
1605 3939256 : for ( i = 1; i < nb_sfm; i++ )
1606 : {
1607 3833083 : if ( ynrm[i] == 39 )
1608 : {
1609 3258055 : for ( j = sfm_start[i]; j < sfm_end[i]; j++ )
1610 : {
1611 3137382 : coefsq[j] = 0.0f;
1612 : }
1613 : }
1614 : }
1615 : }
1616 :
1617 125269 : return;
1618 : }
1619 :
1620 :
1621 : /*--------------------------------------------------------------------------*
1622 : * overlap_hq_bwe()
1623 : *
1624 : * Overlapping at the boundary between HQ core and BWE
1625 : *--------------------------------------------------------------------------*/
1626 :
1627 39084 : 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 39084 : if ( R[num_env_bands - 1] != 0 )
1644 : {
1645 20822 : mvr2r( hq_swb_overlap_buf, &coeff_out[n_swb_overlap_offset], n_swb_overlap );
1646 : }
1647 : else
1648 : {
1649 : /*weighting = 0.8f;*/
1650 18262 : step = 1.0f / (float) n_swb_overlap;
1651 18262 : weighting = 1.0f;
1652 168470 : for ( i = 0; i < n_swb_overlap; i++ )
1653 : {
1654 150208 : coeff_out[n_swb_overlap_offset + i] = hq_swb_overlap_buf[i] * weighting + coeff_out[n_swb_overlap_offset + i] * ( 1.0f - weighting );
1655 150208 : weighting -= step;
1656 : }
1657 : }
1658 :
1659 :
1660 610960 : for ( n_band = num_env_bands; n_band < num_sfm; n_band++ )
1661 : {
1662 571876 : if ( R[n_band] != 0 )
1663 : {
1664 3550 : for ( i = sfm_end[n_band - 1]; i < sfm_end[n_band]; ++i )
1665 : {
1666 3408 : coeff_out[i] = hq_swb_overlap_buf[i - n_swb_overlap_offset];
1667 : }
1668 : }
1669 : }
1670 :
1671 39084 : return;
1672 : }
1673 :
1674 :
1675 : /*--------------------------------------------------------------------------*
1676 : * apply_envelope()
1677 : *
1678 : * Apply spectral envelope with envelope adjustments
1679 : *--------------------------------------------------------------------------*/
1680 :
1681 92784 : 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 92784 : len = num_sfm;
1702 92784 : if ( HQ_mode == HQ_GEN_SWB || HQ_mode == HQ_GEN_FB )
1703 : {
1704 39084 : len = last_sfm + 1;
1705 : }
1706 :
1707 92784 : if ( length == L_FRAME16k )
1708 : {
1709 649593 : for ( sfm = 0; sfm < num_sfm; sfm++ )
1710 : {
1711 625534 : normq_v[sfm] = dicn[norm[sfm]];
1712 625534 : normq = normq_v[sfm] * norm_adj[sfm];
1713 :
1714 8324414 : for ( i = sfm_start[sfm]; i < sfm_end[sfm]; i++ )
1715 : {
1716 7698880 : coeff_out[i] = coeff[i] * normq;
1717 : }
1718 : }
1719 : }
1720 : else
1721 : {
1722 2382309 : for ( sfm = 0; sfm < len; sfm++ )
1723 : {
1724 2313584 : normq_v[sfm] = dicn[norm[sfm]];
1725 2313584 : normq_v[sfm] *= norm_adj[sfm];
1726 :
1727 2313584 : normq = normq_v[sfm];
1728 37674952 : for ( i = sfm_start[sfm]; i < sfm_end[sfm]; i++ )
1729 : {
1730 35361368 : coeff_out[i] = coeff[i] * normq;
1731 : }
1732 : }
1733 :
1734 68725 : if ( HQ_mode == HQ_GEN_SWB || HQ_mode == HQ_GEN_FB )
1735 : {
1736 1097136 : for ( sfm = 0; sfm <= last_sfm; sfm++ )
1737 : {
1738 1058052 : normq = normq_v[sfm];
1739 13944420 : for ( i = sfm_start[sfm]; i < sfm_end[sfm]; i++ )
1740 : {
1741 12886368 : coeff_out1[i] = coeff_out1[i] * normq;
1742 : }
1743 : }
1744 : }
1745 : }
1746 :
1747 92784 : return;
1748 : }
1749 :
1750 :
1751 : /*--------------------------------------------------------------------------*
1752 : * apply_envelope_enc()
1753 : *
1754 : * Apply spectral envelope without envelope adjustments and noisefill
1755 : *--------------------------------------------------------------------------*/
1756 :
1757 71340 : 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 2760514 : for ( sfm = 0; sfm < num_sfm; sfm++ )
1770 : {
1771 2689174 : normq = dicn[norm[sfm]];
1772 :
1773 49494934 : for ( i = sfm_start[sfm]; i < sfm_end[sfm]; i++ )
1774 : {
1775 46805760 : coeff[i] *= normq;
1776 : }
1777 : }
1778 :
1779 71340 : 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 478289 : 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 478289 : accX = *mx >> 1;
1801 478289 : accY = my >> 1;
1802 478289 : align = *ex - ey;
1803 478289 : if ( align < 0 )
1804 : {
1805 359428 : if ( align > -32 ) /* If align < -32, (accY >> (-align) = 0 */
1806 : {
1807 359428 : accX = accX + ( accY >> ( -align ) );
1808 : }
1809 : }
1810 : else
1811 : {
1812 118861 : if ( align < 32 ) /* If align > 32, (accX >> align) = 0 */
1813 : {
1814 118861 : accX = accY + ( accX >> align );
1815 : }
1816 : else
1817 : {
1818 0 : accX = accY;
1819 : }
1820 118861 : *ex = ey;
1821 : }
1822 478289 : expo = norm_l( accX ); /* aligned to BASOP */
1823 478289 : *mx = accX << expo;
1824 478289 : *ex = *ex + expo - 1;
1825 :
1826 478289 : return;
1827 : }
|