Line data Source code
1 : /******************************************************************************************************
2 :
3 : (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
4 : Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
5 : Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
6 : Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
7 : contributors to this repository. All Rights Reserved.
8 :
9 : This software is protected by copyright law and by international treaties.
10 : The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
11 : Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
12 : Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
13 : Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
14 : contributors to this repository retain full ownership rights in their respective contributions in
15 : the software. This notice grants no license of any kind, including but not limited to patent
16 : license, nor is any license granted by implication, estoppel or otherwise.
17 :
18 : Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
19 : contributions.
20 :
21 : This software is provided "AS IS", without any express or implied warranties. The software is in the
22 : development stage. It is intended exclusively for experts who have experience with such software and
23 : solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
24 : and fitness for a particular purpose are hereby disclaimed and excluded.
25 :
26 : Any dispute, controversy or claim arising under or in relation to providing this software shall be
27 : submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
28 : accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
29 : the United Nations Convention on Contracts on the International Sales of Goods.
30 :
31 : *******************************************************************************************************/
32 :
33 : /*====================================================================================
34 : EVS Codec 3GPP TS26.443 Nov 04, 2021. Version 12.14.0 / 13.10.0 / 14.6.0 / 15.4.0 / 16.3.0
35 : ====================================================================================*/
36 :
37 : #include <stdint.h>
38 : #include "options.h"
39 : #ifdef DEBUGGING
40 : #include "debug.h"
41 : #endif
42 : #include <math.h>
43 : #include "cnst.h"
44 : #include "prot.h"
45 : #include "rom_com.h"
46 : #include "rom_enc.h"
47 : #include "basop_util.h"
48 : #include "basop_proto_func.h"
49 : #include "wmc_auto.h"
50 :
51 : /*---------------------------------------------------------------------*
52 : * Local function prototypes
53 : *---------------------------------------------------------------------*/
54 :
55 : static int16_t SWB_BWE_encoding( Encoder_State *st, const float *insig, const float *insig_lp, const float *insig_hp, const float *synth, const float *yos, float *SWB_fenv, const float tilt_nb, const int16_t st_offset );
56 : static void MSVQ_Interpol_Tran( float *SWB_env_energy, int16_t *indice );
57 : static void calculate_tonality( const float *org, const float *gen, float *SFM_org, float *SFM_gen, const int16_t length );
58 : static int16_t WB_BWE_encoding( Encoder_State *st, const float *yos, float *WB_fenv );
59 : static void energy_control( Encoder_State *st, const int16_t core, const int16_t mode, const int16_t coder_type, const float *org, const int16_t offset, float *energy_factor );
60 : static int16_t decision_hq_generic_class( const float *coefs, const int16_t hq_generic_offset );
61 :
62 :
63 : /*-------------------------------------------------------------------*
64 : * wb_bwe_enc()
65 : *
66 : * WB BWE encoder
67 : *-------------------------------------------------------------------*/
68 :
69 292211 : void wb_bwe_enc(
70 : Encoder_State *st, /* i/o: encoder state structure */
71 : const float *new_wb_speech /* i : original input signal at 16kHz */
72 : )
73 : {
74 : FD_BWE_ENC_HANDLE hBWE_FD;
75 : float old_input[NS2SA( 16000, DELAY_FD_BWE_ENC_NS + DELAY_FIR_RESAMPL_NS ) + L_FRAME16k];
76 : float *new_input; /* pointer to original input signal */
77 : float yorig[L_FRAME16k]; /* MDCT spectrum of weighted original */
78 292211 : int16_t mode = 0;
79 : float wtda_old_input[2 * L_FRAME16k];
80 : float WB_fenv[SWB_FENV];
81 : int16_t Sample_Delay_WB_BWE;
82 :
83 292211 : hBWE_FD = st->hBWE_FD;
84 :
85 292211 : if ( st->extl_brate > 0 )
86 : {
87 : /*---------------------------------------------------------------------*
88 : * Delay the original input signal to be synchronized with ACELP core synthesis
89 : *---------------------------------------------------------------------*/
90 :
91 27785 : set_f( old_input, 0, NS2SA( 16000, DELAY_FD_BWE_ENC_12k8_NS + DELAY_FIR_RESAMPL_NS ) + L_FRAME16k );
92 :
93 27785 : Sample_Delay_WB_BWE = NS2SA( 16000, DELAY_FD_BWE_ENC_12k8_NS );
94 :
95 27785 : new_input = old_input + Sample_Delay_WB_BWE;
96 27785 : mvr2r( hBWE_FD->old_input_wb, old_input, Sample_Delay_WB_BWE );
97 27785 : mvr2r( new_wb_speech, new_input, L_FRAME16k );
98 27785 : mvr2r( old_input + L_FRAME16k, hBWE_FD->old_input_wb, Sample_Delay_WB_BWE );
99 :
100 : /*---------------------------------------------------------------------*
101 : * WB BWE encoding
102 : *---------------------------------------------------------------------*/
103 :
104 : /* MDCT of the original signal */
105 27785 : wtda( old_input, wtda_old_input, hBWE_FD->old_wtda_swb, ALDO_WINDOW, ALDO_WINDOW, L_FRAME16k );
106 :
107 27785 : direct_transform( wtda_old_input, yorig, 0, L_FRAME16k, st->element_mode );
108 :
109 27785 : mode = WB_BWE_encoding( st, yorig, WB_fenv );
110 :
111 27785 : push_indice( st->hBstr, IND_WB_CLASS, mode - 2, 1 );
112 : }
113 :
114 292211 : hBWE_FD->prev_mode = mode;
115 :
116 292211 : return;
117 : }
118 :
119 : /*-------------------------------------------------------------------*
120 : * get_normalize_spec()
121 : *
122 : *-------------------------------------------------------------------*/
123 :
124 148626 : static void get_normalize_spec(
125 : const int16_t core, /* i : core selected */
126 : const int16_t extl, /* i : extension layer selected */
127 : const int16_t mode, /* i : SHB BWE class */
128 : const int16_t core_type, /* i : coding type */
129 : const float *org, /* i : input spectrum */
130 : float *SWB_signal, /* o : output spectrum */
131 : int16_t *prev_L_swb_norm, /* i : previous norm. len */
132 : const int16_t offset /* i : frequency offset */
133 : )
134 : {
135 : int16_t n_freq, L_swb_norm;
136 : float envelope[L_FRAME32k];
137 : int16_t frq_end;
138 :
139 148626 : set_f( SWB_signal, 0, HQ_GENERIC_HIGH0 + offset );
140 148626 : calc_normal_length( core, org, mode, extl, &L_swb_norm, prev_L_swb_norm );
141 :
142 148626 : if ( extl == SWB_BWE || extl == FB_BWE )
143 : {
144 95305 : if ( mode == HARMONIC )
145 : {
146 842 : mvr2r( org, &SWB_signal[240 + offset], 240 );
147 842 : mvr2r( &org[128], &SWB_signal[480 + offset], 80 );
148 : }
149 : else
150 : {
151 94463 : mvr2r( &org[112], &SWB_signal[240 + offset], 128 );
152 94463 : mvr2r( &org[112], &SWB_signal[368 + offset], 128 );
153 94463 : mvr2r( &org[176], &SWB_signal[496 + offset], 64 );
154 : }
155 95305 : frq_end = 560 + offset;
156 : }
157 53321 : else if ( extl == WB_BWE )
158 : {
159 27785 : if ( core_type == 0 )
160 : {
161 3559 : mvr2r( &org[160], &SWB_signal[240], 80 );
162 : }
163 : else
164 : {
165 24226 : mvr2r( &org[80], &SWB_signal[240], 80 );
166 : }
167 27785 : frq_end = L_FRAME16k;
168 : }
169 : else
170 : {
171 25536 : mvr2r( org + HQ_GENERIC_OFFSET, SWB_signal + HQ_GENERIC_HIGH0 + offset, HQ_GENERIC_LEN0 );
172 25536 : mvr2r( org + HQ_GENERIC_OFFSET, SWB_signal + HQ_GENERIC_HIGH1 + offset, HQ_GENERIC_LEN0 );
173 25536 : if ( offset == HQ_GENERIC_FOFFSET_24K4 )
174 : {
175 24651 : mvr2r( org + HQ_GENERIC_LOW0, SWB_signal + HQ_GENERIC_HIGH2 + offset, HQ_GENERIC_END_FREQ - HQ_GENERIC_HIGH2 );
176 : }
177 25536 : frq_end = L_FRAME32k;
178 : }
179 :
180 : /* calculate envelope */
181 148626 : calc_norm_envelop( SWB_signal, envelope, L_swb_norm, frq_end - offset, offset );
182 :
183 : /* Normalize with envelope */
184 40983906 : for ( n_freq = swb_bwe_subband[0] + offset; n_freq < frq_end; n_freq++ )
185 : {
186 40835280 : SWB_signal[n_freq] /= envelope[n_freq];
187 : }
188 :
189 148626 : return;
190 : }
191 :
192 : /*-------------------------------------------------------------------*
193 : * WB_BWE_fenv_q()
194 : *
195 : * Scalar quantizer routine
196 : *-------------------------------------------------------------------*/
197 :
198 : /*! r: quantized gain index */
199 27785 : static int16_t WB_BWE_fenv_q(
200 : float *x, /* i/o: energy of WB envelop */
201 : const float *cb, /* i : quantizer codebook */
202 : const int16_t cb_length, /* i : length of codebook */
203 : const int16_t cb_dim /* i : dimension of codebook */
204 : )
205 : {
206 27785 : int16_t i, j, indx = 0;
207 : float dist, min_dist;
208 27785 : const float *pit = cb;
209 :
210 27785 : min_dist = FLT_MAX;
211 916905 : for ( i = 0; i < cb_length; i++ )
212 : {
213 889120 : dist = 0.0f;
214 2667360 : for ( j = 0; j < cb_dim; j++ )
215 : {
216 1778240 : dist += ( x[j] - ( *pit ) ) * ( x[j] - ( *pit ) );
217 1778240 : pit++;
218 : }
219 :
220 889120 : if ( dist < min_dist )
221 : {
222 99874 : min_dist = dist;
223 99874 : indx = i;
224 : }
225 : }
226 :
227 83355 : for ( j = 0; j < cb_dim; j++ )
228 : {
229 55570 : x[j] = cb[cb_dim * indx + j];
230 : }
231 :
232 27785 : return ( indx );
233 : }
234 :
235 : /*-------------------------------------------------------------------*
236 : * FD_BWE_class()
237 : *
238 : * classify signal of above 6.4kHz, can be used for WB/SWB switch
239 : *-------------------------------------------------------------------*/
240 :
241 : /*! r: FD BWE class */
242 123090 : static int16_t FD_BWE_class(
243 : const float *fSpectrum, /* i : input spectrum */
244 : const float fGain, /* i : global gain */
245 : const float tilt_nb, /* i : BWE tilt */
246 : Encoder_State *st /* i/o: Encoder structure */
247 : )
248 : {
249 123090 : int16_t i, j, k, noise, sharpMod = 0;
250 : float peak, mean[20], mag;
251 : float sharpPeak;
252 123090 : const float *input_hi = 0;
253 : float sharp;
254 123090 : float gain_tmp = 0;
255 : int16_t mode;
256 123090 : float meanH, mean_d = 0;
257 : int16_t sharplimit;
258 : int16_t numsharp;
259 : int16_t numharmonic;
260 :
261 123090 : mode = NORMAL;
262 123090 : k = 0;
263 123090 : noise = 0;
264 123090 : sharpPeak = 0;
265 123090 : numsharp = 0;
266 123090 : numharmonic = 4;
267 123090 : sharplimit = 10;
268 :
269 123090 : if ( st->extl == SWB_BWE || st->extl == FB_BWE )
270 : {
271 95305 : input_hi = &fSpectrum[256];
272 95305 : numsharp = NUM_SHARP;
273 95305 : if ( ( st->last_extl == SWB_BWE && st->extl == SWB_BWE ) || ( st->last_extl == FB_BWE && st->extl == FB_BWE ) )
274 : {
275 77757 : gain_tmp = fGain / ( st->hBWE_FD->prev_global_gain + EPSILON );
276 77757 : if ( st->hBWE_FD->prev_mode == TRANSIENT )
277 : {
278 376 : numharmonic = numharmonic * 2;
279 : }
280 77381 : else if ( st->hBWE_FD->prev_mode == NORMAL || st->hBWE_FD->prev_mode == NOISE )
281 : {
282 76869 : numharmonic = 3 * numharmonic / 2;
283 : }
284 : }
285 : else
286 : {
287 17548 : gain_tmp = 1;
288 17548 : if ( st->hBWE_FD->prev_mode == HARMONIC )
289 : {
290 2 : numharmonic = numharmonic / 2;
291 2 : sharplimit = sharplimit / 2;
292 : }
293 : else
294 : {
295 17546 : numharmonic = numharmonic * 2;
296 17546 : sharplimit = sharplimit * 2;
297 : }
298 : }
299 : }
300 27785 : else if ( st->extl == WB_BWE )
301 : {
302 27785 : input_hi = &fSpectrum[224];
303 27785 : numsharp = NUM_SHARP / 3;
304 27785 : if ( st->hBWE_FD->prev_mode == HARMONIC )
305 : {
306 2058 : numharmonic = numharmonic / 4;
307 : }
308 : else
309 : {
310 25727 : numharmonic = numharmonic / 2;
311 : }
312 27785 : if ( st->last_extl != WB_BWE )
313 : {
314 1367 : if ( st->hBWE_FD->prev_mode == HARMONIC )
315 : {
316 0 : sharplimit = sharplimit / 2;
317 : }
318 : else
319 : {
320 1367 : sharplimit = sharplimit * 2;
321 : }
322 : }
323 : }
324 :
325 123090 : meanH = EPSILON;
326 1064190 : for ( i = 0; i < numsharp; i++ )
327 : {
328 941100 : peak = 0.0f;
329 941100 : mean[i] = 0;
330 :
331 31056300 : for ( j = 0; j < SHARP_WIDTH; j++ )
332 : {
333 30115200 : mag = (float) fabs( *input_hi );
334 30115200 : if ( mag > peak )
335 : {
336 3683023 : peak = mag;
337 : }
338 30115200 : mean[i] += mag;
339 30115200 : input_hi++;
340 : }
341 941100 : meanH += mean[i];
342 :
343 941100 : if ( mean[i] != peak )
344 : {
345 900570 : sharp = (float) ( peak * ( SHARP_WIDTH - 1 ) / ( mean[i] - peak ) );
346 : }
347 : else
348 : {
349 40530 : sharp = 0.0f;
350 : }
351 :
352 941100 : if ( sharp > 4.5 && peak > 8 )
353 : {
354 85520 : k += 1;
355 : }
356 855580 : else if ( sharp < 3.0 )
357 : {
358 398356 : noise += 1;
359 : }
360 :
361 941100 : if ( sharp > sharpPeak )
362 : {
363 297863 : sharpPeak = sharp;
364 : }
365 : }
366 :
367 123090 : if ( st->extl == SWB_BWE || st->extl == FB_BWE )
368 : {
369 95305 : if ( k >= numharmonic && gain_tmp > 0.5f && gain_tmp < 1.8f && sharpPeak > sharplimit )
370 : {
371 783 : sharpMod = 1;
372 : }
373 : else
374 : {
375 94522 : sharpMod = 0;
376 : }
377 95305 : meanH /= 288;
378 95305 : mean_d = 0.0f;
379 953050 : for ( i = 0; i < NUM_SHARP; i++ )
380 : {
381 857745 : mean_d += (float) fabs( mean[i] / 32 - meanH );
382 : }
383 : }
384 27785 : else if ( st->extl == WB_BWE )
385 : {
386 27785 : if ( k >= numharmonic && sharpPeak > sharplimit )
387 : {
388 1479 : sharpMod = 1;
389 : }
390 : else
391 : {
392 26306 : sharpMod = 0;
393 : }
394 : }
395 :
396 123090 : if ( sharpMod && st->hBWE_FD->modeCount < 12 )
397 : {
398 1802 : st->hBWE_FD->modeCount++;
399 : }
400 121288 : else if ( sharpMod == 0 && st->hBWE_FD->modeCount > 0 )
401 : {
402 1172 : st->hBWE_FD->modeCount--;
403 : }
404 :
405 123090 : if ( st->hBWE_FD->modeCount >= 2 )
406 : {
407 2219 : sharpMod = 1;
408 : }
409 :
410 123090 : if ( sharpMod )
411 : {
412 2926 : mode = HARMONIC;
413 : }
414 120164 : else if ( st->extl == SWB_BWE || st->extl == FB_BWE )
415 : {
416 94463 : if ( noise > 4 && mean_d < 4.8f * meanH && tilt_nb < 5 )
417 : {
418 11944 : mode = NOISE;
419 : }
420 : }
421 :
422 123090 : return mode;
423 : }
424 :
425 : /*-------------------------------------------------------------------*
426 : * WB_BWE_encoding()
427 : *
428 : * WB BWE main encoder
429 : *-------------------------------------------------------------------*/
430 :
431 : /*! r: classification of wb signal */
432 27785 : static int16_t WB_BWE_encoding(
433 : Encoder_State *st, /* i/o: Encoder structure */
434 : const float *yos, /* i : MDCT coefficients of weighted original */
435 : float *WB_fenv /* i/o: energy of WB envelope */
436 : )
437 : {
438 : int16_t mode;
439 : float global_gain;
440 : float energy;
441 : int16_t i, n_coeff, n_band;
442 : int16_t index;
443 : float energy_factor[4];
444 :
445 : /* Energy for the different bands and global energies */
446 27785 : global_gain = EPSILON;
447 :
448 83355 : for ( i = 0, n_band = 0; i < 2; i++ )
449 : {
450 55570 : energy = EPSILON;
451 2278370 : for ( n_coeff = swb_bwe_subband[n_band]; n_coeff < swb_bwe_subband[n_band + 2]; n_coeff++ )
452 : {
453 2222800 : energy += yos[n_coeff] * yos[n_coeff];
454 : }
455 :
456 55570 : WB_fenv[i] = energy;
457 55570 : n_band += 2;
458 55570 : global_gain += energy;
459 : }
460 :
461 27785 : mode = FD_BWE_class( yos, global_gain, 0, st );
462 :
463 27785 : energy_control( st, ACELP_CORE, mode, st->coder_type, yos, 0, energy_factor );
464 :
465 83355 : for ( i = 0; i < 2; i++ )
466 : {
467 55570 : WB_fenv[i] = (float) ( log10( WB_fenv[i] * energy_factor[i << 1] / 40 ) * FAC_LOG2 );
468 : }
469 :
470 27785 : index = WB_BWE_fenv_q( WB_fenv, F_2_5, 32, 2 );
471 :
472 27785 : push_indice( st->hBstr, IND_WB_FENV, index, 5 );
473 :
474 :
475 27785 : return ( mode );
476 : }
477 :
478 :
479 : /*-------------------------------------------------------------------*
480 : * swb_bwe_enc()
481 : *
482 : * SWB BWE encoder (only for 32kHz signals)
483 : *-------------------------------------------------------------------*/
484 :
485 96406 : void swb_bwe_enc(
486 : Encoder_State *st, /* i/o: encoder state structure */
487 : const int16_t last_element_mode, /* i : last element mode */
488 : const float *old_input_12k8, /* i : input signal @12.8kHz for SWB BWE */
489 : const float *old_input_16k, /* i : input signal @16kHz for SWB BWE */
490 : const float *old_syn_12k8_16k, /* i : ACELP core synthesis at 12.8kHz or 16kHz*/
491 : const float *new_swb_speech, /* i : original input signal at 32kHz */
492 : const float *shb_speech /* i : SHB target signal (6-14kHz) at 16kHz */
493 : )
494 : {
495 : int16_t i, inner_frame, idxGain;
496 : TD_BWE_ENC_HANDLE hBWE_TD;
497 : FD_BWE_ENC_HANDLE hBWE_FD;
498 : float *new_input;
499 : int32_t inner_Fs;
500 : float old_input[NS2SA( 48000, DELAY_FD_BWE_ENC_NS + DELAY_FIR_RESAMPL_NS ) + L_FRAME48k];
501 : float old_input_lp[L_FRAME16k], new_input_hp[L_FRAME16k];
502 : float yorig[L_FRAME48k];
503 : float wtda_old_input[2 * L_FRAME48k];
504 : float SWB_fenv[SWB_FENV];
505 : float tilt_nb;
506 : int16_t Sample_Delay_SWB_BWE, Sample_Delay_HP, Sample_Delay_LP;
507 : float ener_low, energy_fbe_fb, fb_ener_adjust, ener_adjust_quan;
508 : int16_t fb_band_begin;
509 :
510 96406 : hBWE_TD = st->hBWE_TD;
511 96406 : hBWE_FD = st->hBWE_FD;
512 :
513 96406 : ener_adjust_quan = 0.0f;
514 96406 : idxGain = 0;
515 :
516 : /*---------------------------------------------------------------------*
517 : * Delay the original input signal to be synchronized with ACELP core synthesis
518 : *---------------------------------------------------------------------*/
519 :
520 96406 : if ( st->extl == FB_BWE )
521 : {
522 18471 : inner_frame = L_FRAME48k;
523 18471 : inner_Fs = 48000;
524 : }
525 : else
526 : {
527 77935 : inner_frame = L_FRAME32k;
528 77935 : inner_Fs = 32000;
529 : }
530 :
531 96406 : set_f( old_input, 0.0f, NS2SA( inner_Fs, DELAY_FD_BWE_ENC_12k8_NS + DELAY_FIR_RESAMPL_NS ) + inner_frame );
532 :
533 96406 : if ( st->L_frame == L_FRAME )
534 : {
535 69639 : Sample_Delay_SWB_BWE = NS2SA( inner_Fs, DELAY_FD_BWE_ENC_12k8_NS + DELAY_FIR_RESAMPL_NS );
536 69639 : Sample_Delay_HP = NS2SA( 16000, ACELP_LOOK_NS + DELAY_FD_BWE_ENC_12k8_NS + DELAY_FIR_RESAMPL_NS - DELAY_CLDFB_NS );
537 69639 : if ( st->element_mode == IVAS_CPE_TD )
538 : {
539 192 : Sample_Delay_HP = NS2SA( 16000, ACELP_LOOK_NS + DELAY_FD_BWE_ENC_12k8_NS + DELAY_FIR_RESAMPL_NS - DELAY_CLDFB_NS - L_MEM_RECALC_TBE_NS );
540 : }
541 69639 : Sample_Delay_LP = NS2SA( 12800, ACELP_LOOK_NS + DELAY_FD_BWE_ENC_12k8_NS );
542 :
543 69639 : if ( st->element_mode > EVS_MONO )
544 : {
545 68265 : Sample_Delay_SWB_BWE -= NS2SA( inner_Fs, DELAY_FIR_RESAMPL_NS );
546 68265 : Sample_Delay_HP -= NS2SA( 16000, DELAY_FIR_RESAMPL_NS );
547 :
548 68265 : if ( st->element_mode == IVAS_CPE_DFT )
549 : {
550 32797 : mvr2r( old_input_12k8 + L_INP_MEM - Sample_Delay_LP, hBWE_FD->old_input_lp, Sample_Delay_LP );
551 32797 : mvr2r( hBWE_TD->old_speech_shb + L_LOOK_16k + L_SUBFR16k - Sample_Delay_HP, new_input_hp, Sample_Delay_HP );
552 : }
553 : }
554 :
555 69639 : mvr2r( hBWE_FD->old_input_lp, old_input_lp, Sample_Delay_LP );
556 69639 : mvr2r( old_input_12k8 + L_INP_MEM, &old_input_lp[Sample_Delay_LP], L_FRAME - Sample_Delay_LP );
557 69639 : mvr2r( old_input_12k8 + L_INP_MEM + L_FRAME - Sample_Delay_LP, hBWE_FD->old_input_lp, Sample_Delay_LP );
558 : }
559 : else
560 : {
561 26767 : Sample_Delay_SWB_BWE = NS2SA( inner_Fs, DELAY_FD_BWE_ENC_16k_NS + DELAY_FIR_RESAMPL_NS );
562 26767 : Sample_Delay_HP = NS2SA( 16000, ACELP_LOOK_NS + DELAY_FD_BWE_ENC_16k_NS + DELAY_FIR_RESAMPL_NS - DELAY_CLDFB_NS );
563 26767 : if ( st->element_mode == IVAS_CPE_TD )
564 : {
565 1251 : Sample_Delay_HP = NS2SA( 16000, ACELP_LOOK_NS + DELAY_FD_BWE_ENC_16k_NS + DELAY_FIR_RESAMPL_NS - DELAY_CLDFB_NS - L_MEM_RECALC_TBE_NS );
566 : }
567 26767 : Sample_Delay_LP = NS2SA( 16000, ACELP_LOOK_NS + DELAY_FD_BWE_ENC_16k_NS );
568 :
569 26767 : if ( st->element_mode > EVS_MONO )
570 : {
571 26767 : Sample_Delay_SWB_BWE -= NS2SA( inner_Fs, DELAY_FIR_RESAMPL_NS );
572 26767 : Sample_Delay_HP -= NS2SA( 16000, DELAY_FIR_RESAMPL_NS );
573 :
574 26767 : if ( st->element_mode == IVAS_CPE_DFT )
575 : {
576 15140 : mvr2r( old_input_16k + L_INP_MEM - Sample_Delay_LP, hBWE_FD->old_input_lp, Sample_Delay_LP );
577 15140 : mvr2r( hBWE_TD->old_speech_shb + L_LOOK_16k + L_SUBFR16k - Sample_Delay_HP, new_input_hp, Sample_Delay_HP );
578 : }
579 : }
580 :
581 26767 : mvr2r( hBWE_FD->old_input_lp, old_input_lp, Sample_Delay_LP );
582 26767 : mvr2r( old_input_16k + L_INP_MEM, &old_input_lp[Sample_Delay_LP], L_FRAME16k - Sample_Delay_LP );
583 26767 : mvr2r( old_input_16k + L_INP_MEM + L_FRAME16k - Sample_Delay_LP, hBWE_FD->old_input_lp, Sample_Delay_LP );
584 : }
585 :
586 96406 : mvr2r( hBWE_FD->new_input_hp, new_input_hp, Sample_Delay_HP );
587 96406 : mvr2r( shb_speech, new_input_hp + Sample_Delay_HP, L_FRAME16k - Sample_Delay_HP );
588 96406 : mvr2r( shb_speech + L_FRAME16k - Sample_Delay_HP, hBWE_FD->new_input_hp, Sample_Delay_HP );
589 :
590 96406 : new_input = old_input + Sample_Delay_SWB_BWE;
591 96406 : mvr2r( hBWE_FD->old_input, old_input, Sample_Delay_SWB_BWE );
592 96406 : mvr2r( new_swb_speech, new_input, inner_frame );
593 96406 : mvr2r( old_input + inner_frame, hBWE_FD->old_input, Sample_Delay_SWB_BWE );
594 :
595 : /*----------------------------------------------------------------------*
596 : * Calculate tilt of the input signal and the ACELP core synthesis
597 : *----------------------------------------------------------------------*/
598 :
599 96406 : calc_tilt_bwe( old_input_lp, &tilt_nb, L_FRAME );
600 :
601 : /*---------------------------------------------------------------------*
602 : * SWB BWE encoding
603 : * FB BWE encoding
604 : *---------------------------------------------------------------------*/
605 :
606 96406 : if ( st->idchan == 1 && last_element_mode == IVAS_CPE_DFT )
607 : {
608 0 : for ( i = 0; i < inner_frame; i++ )
609 : {
610 0 : hBWE_FD->old_wtda_swb[i] *= (float) i / (float) inner_frame;
611 : }
612 : }
613 :
614 : /* windowing of the original input signal */
615 96406 : wtda( old_input, wtda_old_input, hBWE_FD->old_wtda_swb, ALDO_WINDOW, ALDO_WINDOW, inner_frame );
616 :
617 : /* DCT of the original input signal */
618 96406 : direct_transform( wtda_old_input, yorig, 0, inner_frame, st->element_mode );
619 :
620 : /* high-band gain control in case of BWS */
621 96406 : if ( st->bwidth_sw_cnt > 0 )
622 : {
623 1148 : v_multc( yorig + L_FRAME16k, (float) ( st->bwidth_sw_cnt ) / (float) BWS_TRAN_PERIOD, yorig + L_FRAME16k, inner_frame - L_FRAME16k );
624 : }
625 :
626 : /* FB BWE encoding */
627 96406 : if ( st->extl == FB_BWE )
628 : {
629 18471 : fb_band_begin = FB_BAND_BEGIN;
630 18471 : if ( st->L_frame == L_FRAME )
631 : {
632 1518 : fb_band_begin = FB_BAND_BEGIN_12k8;
633 : }
634 :
635 18471 : energy_fbe_fb = sum2_f( yorig + fb_band_begin, FB_BAND_WIDTH ) + EPSILON;
636 18471 : ener_low = EPSILON;
637 3343251 : for ( i = fb_band_begin - FB_BAND_WIDTH; i < fb_band_begin; i++ )
638 : {
639 3324780 : ener_low += yorig[i] * yorig[i];
640 : }
641 :
642 18471 : fb_ener_adjust = (float) sqrt( energy_fbe_fb / ener_low );
643 18471 : fb_ener_adjust = min( fb_ener_adjust, FB_MAX_GAIN_VAR );
644 18471 : idxGain = (int16_t) usquant( fb_ener_adjust, &ener_adjust_quan, FB_GAIN_QLOW, FB_GAIN_QDELTA, 1 << NUM_BITS_FB_FRAMEGAIN );
645 : }
646 :
647 : /* SWB BWE encoding */
648 96406 : if ( st->L_frame == L_FRAME16k )
649 : {
650 26767 : SWB_BWE_encoding( st, old_input, old_input_lp, new_input_hp, old_syn_12k8_16k, yorig, SWB_fenv, tilt_nb, 80 );
651 : }
652 : else
653 : {
654 69639 : SWB_BWE_encoding( st, old_input, old_input_lp, new_input_hp, old_syn_12k8_16k, yorig, SWB_fenv, tilt_nb, 6 );
655 : }
656 :
657 : /* write FB BWE frame gain to the bitstream */
658 96406 : if ( st->extl == FB_BWE )
659 : {
660 18471 : push_indice( st->hBstr, IND_FB_SLOPE, idxGain, NUM_BITS_FB_FRAMEGAIN );
661 : }
662 :
663 96406 : return;
664 : }
665 :
666 :
667 : /*-------------------------------------------------------------------*
668 : * Freq_weights()
669 : *
670 : *-------------------------------------------------------------------*/
671 :
672 120841 : static void freq_weights(
673 : const float Band_Ener[], /* i : Band energy */
674 : const float f_weighting[], /* i : weigting coefs. */
675 : float w_env[], /* o : Freq. weighting */
676 : const int16_t Nbands /* i : Number of bands */
677 : )
678 : {
679 : int16_t i;
680 : float tmp, w1[SWB_FENV], w2[SWB_FENV];
681 : float min_b, max_b;
682 :
683 : /* Find Max band energy */
684 120841 : min_b = Band_Ener[0];
685 120841 : max_b = Band_Ener[0];
686 1690004 : for ( i = 1; i < Nbands; i++ )
687 : {
688 1569163 : if ( Band_Ener[i] < min_b )
689 : {
690 474268 : min_b = Band_Ener[i];
691 : }
692 1569163 : if ( Band_Ener[i] > max_b )
693 : {
694 188338 : max_b = Band_Ener[i];
695 : }
696 : }
697 :
698 : /* Find weighting function */
699 120841 : tmp = 1.f / ( max_b - min_b );
700 1810845 : for ( i = 0; i < Nbands; i++ )
701 : {
702 1690004 : w1[i] = ( Band_Ener[i] - min_b ) * tmp + 1.f; /*1<= var <=2 */
703 1690004 : w2[i] = f_weighting[i]; /*1~0.75*/
704 1690004 : w_env[i] = w1[i] * w2[i];
705 : }
706 :
707 120841 : return;
708 : }
709 :
710 :
711 : /*-------------------------------------------------------------------*
712 : * VQwithCand_w()
713 : *
714 : *-------------------------------------------------------------------*/
715 :
716 121942 : static void vqWithCand_w(
717 : const float *x, /* i : input vector */
718 : const float *E_ROM_dico, /* i : codebook */
719 : const int16_t dim, /* i : codebook dimension */
720 : const int16_t E_ROM_dico_size, /* i : codebook size */
721 : int16_t *index, /* o : survivors indices */
722 : const int16_t surv, /* i : survivor number */
723 : float dist_min[], /* o : minimum distortion */
724 : const float *w, /* i : weighting */
725 : const int16_t flag /* i : flag indicationg weighted distortion metric */
726 : )
727 : {
728 : int16_t i, j, k, l;
729 : const float *p_E_ROM_dico;
730 : float dist, temp1;
731 :
732 121942 : if ( flag )
733 : {
734 120841 : set_f( dist_min, 3.402823466e+38F, surv ); /* FLT_MAX */
735 :
736 362523 : for ( i = 0; i < surv; i++ )
737 : {
738 241682 : index[i] = i;
739 : }
740 :
741 120841 : p_E_ROM_dico = E_ROM_dico;
742 :
743 3987753 : for ( i = 0; i < E_ROM_dico_size; i++ )
744 : {
745 3866912 : dist = x[0] - *p_E_ROM_dico++;
746 3866912 : dist *= ( dist * w[0] );
747 :
748 27068384 : for ( j = 1; j < dim; j++ )
749 : {
750 23201472 : temp1 = x[j] - *p_E_ROM_dico++;
751 23201472 : dist += temp1 * temp1 * w[j];
752 : }
753 :
754 10064453 : for ( k = 0; k < surv; k++ )
755 : {
756 7141745 : if ( dist < dist_min[k] )
757 : {
758 1536283 : for ( l = surv - 1; l > k; l-- )
759 : {
760 592079 : dist_min[l] = dist_min[l - 1];
761 592079 : index[l] = index[l - 1];
762 : }
763 944204 : dist_min[k] = dist;
764 944204 : index[k] = i;
765 944204 : break;
766 : }
767 : }
768 : }
769 : }
770 : else
771 : {
772 1101 : set_f( dist_min, 3.402823466e+38F, surv ); /* FLT_MAX */
773 :
774 4404 : for ( i = 0; i < surv; i++ )
775 : {
776 3303 : index[i] = i;
777 : }
778 :
779 1101 : p_E_ROM_dico = E_ROM_dico;
780 :
781 142029 : for ( i = 0; i < E_ROM_dico_size; i++ )
782 : {
783 140928 : dist = x[0] - *p_E_ROM_dico++;
784 140928 : dist *= dist;
785 :
786 281856 : for ( j = 1; j < dim; j++ )
787 : {
788 140928 : temp1 = x[j] - *p_E_ROM_dico++;
789 140928 : dist += temp1 * temp1;
790 : }
791 :
792 531221 : for ( k = 0; k < surv; k++ )
793 : {
794 405472 : if ( dist < dist_min[k] )
795 : {
796 32491 : for ( l = surv - 1; l > k; l-- )
797 : {
798 17312 : dist_min[l] = dist_min[l - 1];
799 17312 : index[l] = index[l - 1];
800 : }
801 15179 : dist_min[k] = dist;
802 15179 : index[k] = i;
803 15179 : break;
804 : }
805 : }
806 : }
807 : }
808 :
809 121942 : return;
810 : }
811 :
812 : /*-------------------------------------------------------------------*
813 : * vqSimple_w()
814 : *
815 : *-------------------------------------------------------------------*/
816 :
817 983086 : static int16_t vqSimple_w(
818 : const float *x, /* i : input for quantizer */
819 : float *y, /* i : quantized value */
820 : const float *cb, /* i : codebooks */
821 : const float *w, /* i : weight */
822 : const int16_t dim, /* i : dimension */
823 : const int16_t l, /* i : number of candidates */
824 : const int16_t flag /* i : flag indicationg weighted distortion metric */
825 : )
826 : {
827 : int16_t i, j, index;
828 : const float *cbP;
829 : float dist_min, dist, temp;
830 :
831 983086 : index = 0;
832 983086 : dist_min = FLT_MAX;
833 983086 : cbP = cb;
834 :
835 983086 : if ( flag )
836 : {
837 70514504 : for ( i = 0; i < l; i++ )
838 : {
839 69547776 : dist = x[0] - *cbP++;
840 69547776 : dist *= ( dist * w[0] );
841 239465344 : for ( j = 1; j < dim; j++ )
842 : {
843 169917568 : temp = x[j] - *cbP++;
844 169917568 : dist += temp * temp * w[j];
845 : }
846 :
847 69547776 : if ( dist < dist_min )
848 : {
849 4439974 : dist_min = dist;
850 4439974 : index = i;
851 : }
852 : }
853 : }
854 : else
855 : {
856 645510 : for ( i = 0; i < l; i++ )
857 : {
858 629152 : dist = x[0] - *cbP++;
859 629152 : dist *= dist;
860 1676064 : for ( j = 1; j < dim; j++ )
861 : {
862 1046912 : temp = x[j] - *cbP++;
863 1046912 : dist += temp * temp;
864 : }
865 :
866 629152 : if ( dist < dist_min )
867 : {
868 66827 : dist_min = dist;
869 66827 : index = i;
870 : }
871 : }
872 : }
873 :
874 : /* Reading the selected vector */
875 983086 : mvr2r( &cb[index * dim], y, dim );
876 :
877 983086 : return ( index );
878 : }
879 :
880 : /*-------------------------------------------------------------------*
881 : * MSVQ_Interpol_Tran()
882 : *
883 : *-------------------------------------------------------------------*/
884 :
885 1101 : static void MSVQ_Interpol_Tran(
886 : float *SWB_env_energy, /* i/o: (original/quantized) energy */
887 : int16_t *indice /* o : quantized index */
888 : )
889 : {
890 : int16_t k, n_band, candInd[N_CAND_TR], ind_tmp[2];
891 : float env_temp11[SWB_FENV_TRANS / 2], env_temp12[SWB_FENV_TRANS / 2];
892 : float dist, minDist, tmp_q;
893 : float quant_tmp[SWB_FENV_TRANS], quant_tmp2[SWB_FENV_TRANS];
894 : float distCand[N_CAND_TR], quant_select[SWB_FENV_TRANS];
895 :
896 : /* Extract target vector */
897 3303 : for ( n_band = 0; n_band < DIM_TR1; n_band++ )
898 : {
899 2202 : env_temp11[n_band] = SWB_env_energy[2 * n_band];
900 2202 : env_temp12[n_band] = SWB_env_energy[2 * n_band + 1];
901 : }
902 :
903 1101 : vqWithCand_w( env_temp11, Env_TR_Cdbk1, DIM_TR1, N_CB_TR1, candInd, N_CAND_TR, distCand, NULL, 0 );
904 :
905 1101 : minDist = FLT_MAX;
906 4404 : for ( k = 0; k < N_CAND_TR; k++ )
907 : {
908 9909 : for ( n_band = 0; n_band < DIM_TR1; n_band++ )
909 : {
910 6606 : quant_tmp[n_band] = Env_TR_Cdbk1[candInd[k] * DIM_TR1 + n_band];
911 : }
912 :
913 6606 : for ( n_band = 0; n_band < DIM_TR2 - 1; n_band++ )
914 : {
915 3303 : quant_tmp2[n_band] = env_temp12[n_band] - ( ( quant_tmp[n_band] + quant_tmp[n_band + 1] ) / 2.f );
916 : }
917 3303 : quant_tmp2[n_band] = env_temp12[n_band] - quant_tmp[n_band];
918 3303 : ind_tmp[0] = vqSimple_w( quant_tmp2, quant_tmp2, Env_TR_Cdbk2, NULL, DIM_TR2, N_CB_TR2, 0 );
919 :
920 9909 : for ( n_band = 0; n_band < DIM_TR1; n_band++ )
921 : {
922 6606 : quant_select[n_band * 2] = quant_tmp[n_band];
923 : }
924 :
925 6606 : for ( n_band = 0; n_band < DIM_TR2 - 1; n_band++ )
926 : {
927 3303 : quant_select[n_band * 2 + 1] = ( ( quant_tmp[n_band] + quant_tmp[n_band + 1] ) / 2.f ) + quant_tmp2[n_band];
928 : }
929 3303 : quant_select[n_band * 2 + 1] = quant_tmp[n_band] + quant_tmp2[n_band];
930 :
931 3303 : dist = 0.f;
932 16515 : for ( n_band = 0; n_band < SWB_FENV_TRANS; n_band++ )
933 : {
934 13212 : tmp_q = SWB_env_energy[n_band] - quant_select[n_band];
935 13212 : dist += tmp_q * tmp_q;
936 : }
937 :
938 : /* Check optimal candidate */
939 3303 : if ( dist < minDist )
940 : {
941 1214 : minDist = dist;
942 1214 : indice[0] = candInd[k];
943 1214 : indice[1] = ind_tmp[0];
944 : }
945 : }
946 :
947 1101 : return;
948 : }
949 :
950 : /*-------------------------------------------------------------------*
951 : * MSVQ_Interpol()
952 : *
953 : *-------------------------------------------------------------------*/
954 119956 : static void msvq_interpol(
955 : float *SWB_env_energy, /* i/o: (original/quantized) energy */
956 : float *w_env, /* i/o: weighting coffecients */
957 : int16_t *indice /* o : quantized index */
958 : )
959 : {
960 : int16_t k, n_band, candInd[N_CAND], ind_tmp[4];
961 : float dist, minDist, tmp_q;
962 : float env_temp11[SWB_FENV / 2], env_temp12[SWB_FENV / 2];
963 : float quant_tmp[SWB_FENV], quant_tmp1[SWB_FENV], quant_tmp2[SWB_FENV], distCand[N_CAND];
964 : float quant_select[SWB_FENV], w_env11[SWB_FENV / 2], w_env12[SWB_FENV / 2];
965 : float synth_energy[SWB_FENV];
966 :
967 : /* Extract target vector */
968 959648 : for ( n_band = 0; n_band < DIM11; n_band++ )
969 : {
970 839692 : env_temp11[n_band] = SWB_env_energy[2 * n_band];
971 839692 : env_temp12[n_band] = SWB_env_energy[2 * n_band + 1];
972 :
973 839692 : w_env11[n_band] = w_env[2 * n_band];
974 839692 : w_env12[n_band] = w_env[2 * n_band + 1];
975 : }
976 :
977 119956 : vqWithCand_w( env_temp11, EnvCdbk11, DIM11, N_CB11, candInd, N_CAND, distCand, w_env11, 1 );
978 :
979 119956 : minDist = FLT_MAX;
980 359868 : for ( k = 0; k < N_CAND; k++ )
981 : {
982 1919296 : for ( n_band = 0; n_band < DIM11; n_band++ )
983 : {
984 1679384 : quant_tmp1[n_band] = EnvCdbk11[candInd[k] * DIM11 + n_band];
985 1679384 : quant_tmp2[n_band] = env_temp11[n_band] - quant_tmp1[n_band];
986 : }
987 :
988 239912 : ind_tmp[0] = vqSimple_w( quant_tmp2, quant_tmp2, EnvCdbk1st, w_env11, DIM1ST, N_CB1ST, 1 );
989 239912 : ind_tmp[1] = vqSimple_w( quant_tmp2 + DIM1ST, quant_tmp2 + DIM1ST, EnvCdbk2nd, w_env11 + DIM1ST, DIM2ND, N_CB2ND, 1 );
990 :
991 : /* Extract vector for odd position */
992 1919296 : for ( n_band = 0; n_band < DIM11; n_band++ )
993 : {
994 1679384 : quant_tmp[n_band] = quant_tmp1[n_band] + quant_tmp2[n_band];
995 : }
996 :
997 1679384 : for ( n_band = 0; n_band < DIM12 - 1; n_band++ )
998 : {
999 1439472 : quant_tmp2[n_band] = env_temp12[n_band] - ( ( quant_tmp[n_band] + quant_tmp[n_band + 1] ) / 2.f );
1000 : }
1001 :
1002 239912 : quant_tmp2[n_band] = env_temp12[n_band] - quant_tmp[n_band];
1003 :
1004 239912 : ind_tmp[2] = vqSimple_w( quant_tmp2, quant_tmp2, EnvCdbk3rd, w_env12, DIM3RD, N_CB3RD, 1 );
1005 239912 : ind_tmp[3] = vqSimple_w( quant_tmp2 + DIM3RD, quant_tmp2 + DIM3RD, EnvCdbk4th, w_env12 + DIM3RD, DIM4TH, N_CB4TH, 1 );
1006 :
1007 1919296 : for ( n_band = 0; n_band < DIM11; n_band++ )
1008 : {
1009 1679384 : quant_select[n_band * 2] = quant_tmp[n_band];
1010 : }
1011 :
1012 1679384 : for ( n_band = 0; n_band < DIM12 - 1; n_band++ )
1013 : {
1014 1439472 : quant_select[n_band * 2 + 1] = ( ( quant_tmp[n_band] + quant_tmp[n_band + 1] ) / 2.f ) + quant_tmp2[n_band];
1015 : }
1016 239912 : quant_select[n_band * 2 + 1] = quant_tmp[n_band] + quant_tmp2[n_band];
1017 :
1018 239912 : dist = 0.f;
1019 3598680 : for ( n_band = 0; n_band < SWB_FENV; n_band++ )
1020 : {
1021 3358768 : tmp_q = SWB_env_energy[n_band] - quant_select[n_band];
1022 3358768 : tmp_q = tmp_q * tmp_q;
1023 3358768 : dist += tmp_q * w_env[n_band];
1024 : }
1025 :
1026 : /* Check optimal candidate */
1027 239912 : if ( dist < minDist )
1028 : {
1029 148938 : minDist = dist;
1030 148938 : mvr2r( quant_select, synth_energy, SWB_FENV );
1031 :
1032 148938 : indice[0] = candInd[k];
1033 148938 : indice[1] = ind_tmp[0];
1034 148938 : indice[2] = ind_tmp[1];
1035 148938 : indice[3] = ind_tmp[2];
1036 148938 : indice[4] = ind_tmp[3];
1037 : }
1038 : }
1039 :
1040 119956 : mvr2r( synth_energy, SWB_env_energy, SWB_FENV );
1041 :
1042 119956 : return;
1043 : }
1044 :
1045 : /*-------------------------------------------------------------------*
1046 : * msvq_interpol_2()
1047 : *
1048 : *
1049 : *-------------------------------------------------------------------*/
1050 :
1051 885 : static void msvq_interpol_2(
1052 : float *hq_generic_fenv, /* i/o: (original/quantized) energy */
1053 : const float *w_env, /* i : weighting coffecients */
1054 : int16_t *indice, /* o : quantized index */
1055 : const int16_t nenv /* i : the number of envelopes */
1056 : )
1057 : {
1058 : int16_t k, n_band, candInd[N_CAND], ind_tmp[4];
1059 : float dist, minDist, tmp_q;
1060 : float env_temp11[SWB_FENV / 2], env_temp12[SWB_FENV / 2];
1061 : float quant_tmp[SWB_FENV], quant_tmp1[SWB_FENV], quant_tmp2[SWB_FENV], distCand[N_CAND];
1062 : float quant_select[SWB_FENV], w_env11[SWB_FENV / 2], w_env12[SWB_FENV / 2];
1063 : float synth_energy[SWB_FENV];
1064 :
1065 : /* Extract target vector */
1066 6195 : for ( n_band = 0; n_band < DIM11 - 1; n_band++ )
1067 : {
1068 5310 : env_temp11[n_band] = hq_generic_fenv[2 * n_band];
1069 5310 : w_env11[n_band] = w_env[2 * n_band];
1070 : }
1071 885 : env_temp11[DIM11 - 1] = hq_generic_fenv[2 * ( DIM11 - 2 ) + 1];
1072 885 : w_env11[DIM11 - 1] = w_env[2 * ( DIM11 - 2 ) + 1];
1073 :
1074 885 : env_temp12[0] = hq_generic_fenv[0];
1075 885 : w_env12[0] = w_env[0];
1076 5310 : for ( n_band = 1; n_band < DIM11 - 1; n_band++ )
1077 : {
1078 4425 : env_temp12[n_band] = hq_generic_fenv[2 * n_band - 1];
1079 4425 : w_env12[n_band] = w_env[2 * n_band - 1];
1080 : }
1081 :
1082 885 : vqWithCand_w( env_temp11, EnvCdbk11, DIM11, N_CB11, candInd, N_CAND, distCand, w_env11, 1 );
1083 :
1084 885 : minDist = FLT_MAX;
1085 2655 : for ( k = 0; k < N_CAND; k++ )
1086 : {
1087 14160 : for ( n_band = 0; n_band < DIM11; n_band++ )
1088 : {
1089 12390 : quant_tmp1[n_band] = EnvCdbk11[candInd[k] * DIM11 + n_band];
1090 12390 : quant_tmp2[n_band] = env_temp11[n_band] - quant_tmp1[n_band];
1091 : }
1092 :
1093 1770 : ind_tmp[0] = vqSimple_w( quant_tmp2, quant_tmp2, EnvCdbk1st, w_env11, DIM1ST, N_CB1ST, 1 );
1094 1770 : ind_tmp[1] = vqSimple_w( quant_tmp2 + DIM1ST, quant_tmp2 + DIM1ST, EnvCdbk2nd, w_env11 + DIM1ST, DIM2ND, N_CB2ND, 1 );
1095 :
1096 : /* Extract vector for odd position */
1097 14160 : for ( n_band = 0; n_band < DIM11; n_band++ )
1098 : {
1099 12390 : quant_tmp[n_band] = quant_tmp1[n_band] + quant_tmp2[n_band];
1100 : }
1101 :
1102 1770 : quant_tmp2[0] = env_temp12[0] - quant_tmp[0];
1103 10620 : for ( n_band = 1; n_band < DIM12 - 1; n_band++ )
1104 : {
1105 8850 : quant_tmp2[n_band] = env_temp12[n_band] - ( ( quant_tmp[n_band - 1] + quant_tmp[n_band] ) / 2.f );
1106 : }
1107 :
1108 1770 : ind_tmp[2] = vqSimple_w( quant_tmp2, quant_tmp2, EnvCdbk3rd, w_env12, DIM3RD, N_CB3RD, 1 );
1109 1770 : ind_tmp[3] = vqSimple_w( quant_tmp2 + DIM3RD, quant_tmp2 + DIM3RD, EnvCdbk3rd, w_env12 + DIM3RD, DIM3RD, N_CB3RD, 1 );
1110 :
1111 12390 : for ( n_band = 0; n_band < DIM12 - 1; n_band++ )
1112 : {
1113 10620 : quant_select[n_band * 2] = quant_tmp[n_band];
1114 : }
1115 1770 : quant_select[11] = quant_tmp[DIM12 - 1];
1116 :
1117 1770 : quant_select[0] += quant_tmp2[0];
1118 10620 : for ( n_band = 1; n_band < DIM12 - 1; n_band++ )
1119 : {
1120 8850 : quant_select[n_band * 2 - 1] = ( ( quant_tmp[n_band - 1] + quant_tmp[n_band] ) / 2.f ) + quant_tmp2[n_band];
1121 : }
1122 :
1123 1770 : dist = 0.f;
1124 23010 : for ( n_band = 0; n_band < SWB_FENV - 2; n_band++ )
1125 : {
1126 21240 : tmp_q = hq_generic_fenv[n_band] - quant_select[n_band];
1127 21240 : tmp_q = tmp_q * tmp_q;
1128 21240 : dist += tmp_q * w_env[n_band];
1129 : }
1130 :
1131 : /* Check optimal candidate */
1132 1770 : if ( dist < minDist )
1133 : {
1134 1147 : minDist = dist;
1135 1147 : mvr2r( quant_select, synth_energy, SWB_FENV - 2 );
1136 1147 : synth_energy[SWB_FENV - 2] = 0;
1137 1147 : synth_energy[SWB_FENV - 1] = 0;
1138 :
1139 1147 : indice[0] = candInd[k];
1140 1147 : indice[1] = ind_tmp[0];
1141 1147 : indice[2] = ind_tmp[1];
1142 1147 : indice[3] = ind_tmp[2];
1143 1147 : indice[4] = ind_tmp[3];
1144 : }
1145 : }
1146 :
1147 885 : mvr2r( synth_energy, hq_generic_fenv, nenv );
1148 :
1149 885 : return;
1150 : }
1151 :
1152 : /*-------------------------------------------------------------------*
1153 : * calculate_tonality()
1154 : *
1155 : * Calculate tonality
1156 : *-------------------------------------------------------------------*/
1157 :
1158 1745574 : static void calculate_tonality(
1159 : const float *org, /* i : MDCT coefficients of original */
1160 : const float *gen, /* i : MDCT coefficients of generated signal */
1161 : float *SFM_org, /* o : Spectral Flatness results */
1162 : float *SFM_gen, /* o : Spectral Flatness results */
1163 : const int16_t length /* i : length for calculating tonality */
1164 : )
1165 : {
1166 : int16_t n_coeff;
1167 : float am_org, am_gen, gm_org, gm_gen;
1168 : float inv_len, max_val, mult;
1169 : float org_spec[80], gen_spec[80];
1170 :
1171 : /* to reduce dynamic range of original spectrum */
1172 1745574 : max_val = EPSILON;
1173 42580854 : for ( n_coeff = 0; n_coeff < length; n_coeff++ )
1174 : {
1175 40835280 : org_spec[n_coeff] = (float) fabs( org[n_coeff] );
1176 :
1177 40835280 : if ( max_val < org_spec[n_coeff] )
1178 : {
1179 6301580 : max_val = org_spec[n_coeff];
1180 : }
1181 : }
1182 1745574 : mult = 25.f / max_val;
1183 :
1184 42580854 : for ( n_coeff = 0; n_coeff < length; n_coeff++ )
1185 : {
1186 40835280 : org_spec[n_coeff] *= mult;
1187 : }
1188 :
1189 1745574 : max_val = EPSILON;
1190 42580854 : for ( n_coeff = 0; n_coeff < length; n_coeff++ )
1191 : {
1192 40835280 : gen_spec[n_coeff] = (float) fabs( gen[n_coeff] );
1193 :
1194 40835280 : if ( max_val < gen_spec[n_coeff] )
1195 : {
1196 5940873 : max_val = gen_spec[n_coeff];
1197 : }
1198 : }
1199 1745574 : mult = 25.f / max_val;
1200 :
1201 42580854 : for ( n_coeff = 0; n_coeff < length; n_coeff++ )
1202 : {
1203 40835280 : gen_spec[n_coeff] *= mult;
1204 : }
1205 :
1206 1745574 : inv_len = 1.f / (float) length;
1207 :
1208 1745574 : am_org = EPSILON;
1209 1745574 : am_gen = EPSILON;
1210 1745574 : gm_org = 1.f;
1211 1745574 : gm_gen = 1.f;
1212 :
1213 42580854 : for ( n_coeff = 0; n_coeff < length; n_coeff++ )
1214 : {
1215 40835280 : am_org += org_spec[n_coeff];
1216 40835280 : am_gen += gen_spec[n_coeff];
1217 40835280 : gm_org *= org_spec[n_coeff];
1218 40835280 : gm_gen *= gen_spec[n_coeff];
1219 : }
1220 :
1221 1745574 : *SFM_org = 10.f * ( (float) log10( am_org * inv_len ) - inv_len * (float) log10( gm_org ) );
1222 1745574 : *SFM_org = max( 0.0001f, min( *SFM_org, 5.993f ) );
1223 1745574 : *SFM_gen = 10.f * ( (float) log10( am_gen * inv_len ) - inv_len * (float) log10( gm_gen ) );
1224 1745574 : *SFM_gen = max( 0.0001f, min( *SFM_gen, 5.993f ) );
1225 :
1226 1745574 : return;
1227 : }
1228 :
1229 : /*-------------------------------------------------------------------*
1230 : * energy_control()
1231 : *
1232 : *-------------------------------------------------------------------*/
1233 :
1234 148626 : static void energy_control(
1235 : Encoder_State *st, /* i/o: encoder structure */
1236 : const int16_t core, /* i : core */
1237 : const int16_t mode, /* i : SHB BWE class */
1238 : const int16_t coder_type, /* i : SHB BWE class */
1239 : const float *org, /* i : input spectrum */
1240 : const int16_t offset, /* i : frequency offset */
1241 : float *energy_factor /* o : energy factor */
1242 : )
1243 : {
1244 : int16_t n_band;
1245 : float gamma;
1246 : int16_t core_type;
1247 : float SWB_signal[L_FRAME32k], SFM_org[SWB_FENV], SFM_gen[SWB_FENV];
1248 148626 : int16_t max_band = SWB_FENV, band_step = 1;
1249 :
1250 148626 : if ( core == ACELP_CORE )
1251 : {
1252 123090 : gamma = 0.35f;
1253 123090 : if ( coder_type != AUDIO && st->total_brate <= ACELP_8k00 )
1254 : {
1255 3693 : core_type = 0;
1256 : }
1257 : else
1258 : {
1259 119397 : core_type = 1;
1260 : }
1261 :
1262 123090 : get_normalize_spec( core, st->extl, mode, core_type, org, SWB_signal, &( st->hBWE_FD->prev_L_swb_norm1 ), offset );
1263 :
1264 123090 : if ( st->extl == WB_BWE )
1265 : {
1266 27785 : max_band = 4;
1267 27785 : band_step = 2;
1268 : }
1269 : }
1270 : else /* HQ core */
1271 : {
1272 25536 : gamma = 0.55f;
1273 25536 : get_normalize_spec( core, -1, mode, -1, org, SWB_signal, &( st->hBWE_FD->prev_L_swb_norm1 ), offset );
1274 :
1275 25536 : if ( offset == HQ_GENERIC_FOFFSET_32K )
1276 : {
1277 885 : max_band = 12;
1278 : }
1279 : }
1280 :
1281 1894200 : for ( n_band = 0; n_band < max_band; n_band += band_step )
1282 : {
1283 1745574 : calculate_tonality( org + swb_bwe_subband[n_band] + offset, SWB_signal + swb_bwe_subband[n_band] + offset, &SFM_org[n_band], &SFM_gen[n_band], swb_bwe_subband[n_band + band_step] - swb_bwe_subband[n_band] );
1284 :
1285 1745574 : if ( SFM_gen[n_band] < 0.75 * SFM_org[n_band] )
1286 : {
1287 506355 : energy_factor[n_band] = ( SFM_gen[n_band] / SFM_org[n_band] );
1288 :
1289 506355 : if ( energy_factor[n_band] < gamma )
1290 : {
1291 70890 : energy_factor[n_band] = gamma;
1292 : }
1293 : }
1294 : else
1295 : {
1296 1239219 : energy_factor[n_band] = 1.0f;
1297 : }
1298 : }
1299 :
1300 148626 : return;
1301 : }
1302 :
1303 : /*-------------------------------------------------------------------*
1304 : * SWB_BWE_encoding()
1305 : *
1306 : * SWB BWE encoder
1307 : *-------------------------------------------------------------------*/
1308 :
1309 96406 : static int16_t SWB_BWE_encoding(
1310 : Encoder_State *st, /* i/o: Encoder state structure */
1311 : const float *insig, /* i : delayed original input signal at 32kHz */
1312 : const float *insig_lp, /* i : delayed original lowband input signal at 16kHz */
1313 : const float *insig_hp, /* i : delayed original highband input signal at 16kHz */
1314 : const float *synth, /* i : delayed ACELP core synthesis at 12.8kHz */
1315 : const float *yos, /* i : MDCT coefficients of the windowed original input signal at 32kHz */
1316 : float *SWB_fenv, /* o : frequency-domain quantized BWE envelope */
1317 : const float tilt_nb, /* i : SWB tilt */
1318 : const int16_t st_offset /* i : start frequency offset for BWE envelope */
1319 : )
1320 : {
1321 : int16_t IsTransient, mode;
1322 : int16_t index;
1323 : float SWB_tenv_tmp[SWB_TENV];
1324 : float SWB_tenv[SWB_TENV];
1325 : float global_gain;
1326 : float energy;
1327 : float max_val;
1328 : int16_t i, n_coeff, n_band, pos, indice[6];
1329 : float tilt, WB_tenv_orig, WB_tenv_syn, Rat_tenv;
1330 : float energy_factor[SWB_FENV], w_env[SWB_FENV];
1331 : int16_t L;
1332 : int16_t IsTransient_LF;
1333 96406 : BSTR_ENC_HANDLE hBstr = st->hBstr;
1334 :
1335 96406 : if ( st->L_frame == L_FRAME )
1336 : {
1337 69639 : L = L_SUBFR;
1338 : }
1339 : else
1340 : {
1341 26767 : L = L_SUBFR16k;
1342 : }
1343 :
1344 : /* HF transient detect */
1345 96406 : IsTransient = detect_transient( st, insig_hp, L_FRAME16k );
1346 :
1347 : /* LF transient detect */
1348 96406 : IsTransient_LF = 0;
1349 482030 : for ( n_band = 0; n_band < 4; n_band++ )
1350 : {
1351 385624 : energy = EPSILON;
1352 26778648 : for ( i = 0; i < L; i++ )
1353 : {
1354 26393024 : energy += insig_lp[i + n_band * L] * insig_lp[i + n_band * L];
1355 : }
1356 :
1357 385624 : if ( energy > 5.5f * st->hBWE_FD->EnergyLF )
1358 : {
1359 23665 : IsTransient_LF = 1;
1360 : }
1361 :
1362 385624 : st->hBWE_FD->EnergyLF = energy;
1363 : }
1364 :
1365 96406 : calc_tilt_bwe( insig, &tilt, L_FRAME32k );
1366 :
1367 96406 : if ( IsTransient == 1 && ( tilt > 8.0 || st->clas > 1 ) )
1368 : {
1369 558 : IsTransient = 0;
1370 558 : st->TransientHangOver = 0;
1371 : }
1372 :
1373 96406 : if ( IsTransient == 1 )
1374 : {
1375 1101 : mode = IsTransient;
1376 1101 : push_indice( hBstr, IND_SWB_CLASS, mode, 2 );
1377 :
1378 : /* Energy for the different bands and global energies */
1379 1101 : global_gain = 0;
1380 5505 : for ( n_band = 0; n_band < SWB_FENV_TRANS; n_band++ )
1381 : {
1382 4404 : energy = EPSILON;
1383 356724 : for ( n_coeff = swb_bwe_trans_subband[n_band] + st_offset; n_coeff < swb_bwe_trans_subband[n_band + 1] + st_offset; n_coeff++ )
1384 : {
1385 352320 : energy += yos[n_coeff] * yos[n_coeff];
1386 : }
1387 4404 : global_gain += energy;
1388 4404 : SWB_fenv[n_band] = energy;
1389 : }
1390 1101 : global_gain *= 0.5f;
1391 :
1392 5505 : for ( n_band = 0; n_band < SWB_FENV_TRANS; n_band++ )
1393 : {
1394 4404 : SWB_fenv[n_band] = 10.0f * (float) log10( SWB_fenv[n_band] / swb_bwe_trans_subband_width[n_band] ) - Mean_env_tr[n_band];
1395 : }
1396 :
1397 1101 : WB_tenv_orig = EPSILON;
1398 1101 : WB_tenv_syn = EPSILON;
1399 5505 : for ( n_band = 0; n_band < SWB_TENV; n_band++ )
1400 : {
1401 4404 : SWB_tenv[n_band] = EPSILON;
1402 :
1403 356724 : for ( i = 0; i < L_SUBFR16k; i++ )
1404 : {
1405 352320 : SWB_tenv[n_band] += insig_hp[i + n_band * L_SUBFR16k] * insig_hp[i + n_band * L_SUBFR16k];
1406 : }
1407 :
1408 295092 : for ( i = 0; i < L; i++ )
1409 : {
1410 290688 : WB_tenv_syn += synth[i + n_band * L] * synth[i + n_band * L];
1411 290688 : WB_tenv_orig += insig_lp[i + n_band * L] * insig_lp[i + n_band * L];
1412 : }
1413 :
1414 4404 : SWB_tenv[n_band] = (float) ( sqrt( SWB_tenv[n_band] * INV_L_SUBFR16k ) );
1415 : }
1416 :
1417 1101 : Rat_tenv = (float) sqrt( WB_tenv_syn / WB_tenv_orig );
1418 :
1419 1101 : if ( Rat_tenv < 0.5 )
1420 : {
1421 63 : Rat_tenv *= 1.2f;
1422 : }
1423 1038 : else if ( Rat_tenv > 1 )
1424 : {
1425 894 : Rat_tenv = 1.0f;
1426 : }
1427 :
1428 5505 : for ( n_band = 0; n_band < SWB_TENV; n_band++ )
1429 : {
1430 4404 : SWB_tenv[n_band] *= Rat_tenv;
1431 : }
1432 :
1433 1101 : max_val = SWB_tenv[0];
1434 1101 : pos = 0;
1435 4404 : for ( n_band = 1; n_band < SWB_TENV; n_band++ )
1436 : {
1437 3303 : if ( SWB_tenv[n_band] > max_val )
1438 : {
1439 1656 : max_val = SWB_tenv[n_band];
1440 1656 : pos = n_band;
1441 : }
1442 : }
1443 :
1444 3159 : for ( n_band = 1; n_band < SWB_TENV; n_band++ )
1445 : {
1446 2563 : if ( SWB_tenv[n_band] > 5.0f * SWB_tenv[n_band - 1] )
1447 : {
1448 505 : break;
1449 : }
1450 : }
1451 :
1452 1101 : if ( n_band < SWB_TENV )
1453 : {
1454 505 : energy = 0.0f;
1455 966 : for ( n_band = ( pos + 1 ); n_band < SWB_TENV; n_band++ )
1456 : {
1457 461 : energy += SWB_tenv[n_band];
1458 : }
1459 505 : if ( pos == SWB_TENV - 1 )
1460 : {
1461 205 : energy = 0.0f;
1462 : }
1463 : else
1464 : {
1465 300 : energy /= ( SWB_TENV - pos - 1 );
1466 : }
1467 :
1468 1559 : for ( n_band = 0; n_band < pos; n_band++ )
1469 : {
1470 1054 : SWB_tenv[n_band] *= 0.5f;
1471 : }
1472 :
1473 505 : SWB_tenv[pos] *= 1.005f;
1474 505 : if ( energy < SWB_tenv[pos] )
1475 : {
1476 966 : for ( n_band = pos + 1; n_band < SWB_TENV; n_band++ )
1477 : {
1478 461 : SWB_tenv[n_band] *= 0.9f;
1479 : }
1480 : }
1481 : }
1482 : else
1483 : {
1484 2384 : for ( n_band = 1; n_band < SWB_TENV; n_band++ )
1485 : {
1486 1788 : if ( SWB_tenv[n_band - 1] > SWB_tenv[n_band] )
1487 : {
1488 769 : SWB_tenv[n_band - 1] = 0.5f * ( SWB_tenv[n_band - 1] + SWB_tenv[n_band] );
1489 : }
1490 : else
1491 : {
1492 1019 : SWB_tenv[n_band] = 0.5f * ( SWB_tenv[n_band - 1] + SWB_tenv[n_band] );
1493 : }
1494 : }
1495 :
1496 2980 : for ( n_band = 0; n_band < SWB_TENV; n_band++ )
1497 : {
1498 2384 : SWB_tenv[n_band] *= 0.9f;
1499 : }
1500 : }
1501 :
1502 1101 : if ( IsTransient_LF == 0 && st->coder_type == INACTIVE && st->TransientHangOver == 1 )
1503 : {
1504 165 : for ( n_band = 0; n_band < SWB_TENV; n_band++ )
1505 : {
1506 132 : SWB_tenv[n_band] *= 0.5f;
1507 : }
1508 165 : for ( n_band = 0; n_band < SWB_FENV_TRANS; n_band++ )
1509 : {
1510 132 : SWB_fenv[n_band] *= 0.05f;
1511 : }
1512 : }
1513 : else
1514 : {
1515 1068 : SWB_fenv[2] *= 0.1f;
1516 1068 : SWB_fenv[3] *= 0.05f;
1517 : }
1518 :
1519 5505 : for ( n_band = 0; n_band < SWB_TENV; n_band++ )
1520 : {
1521 4404 : SWB_tenv_tmp[n_band] = (float) log10( SWB_tenv[n_band] + EPSILON ) * FAC_LOG2;
1522 4404 : if ( SWB_tenv_tmp[n_band] > 15 )
1523 : {
1524 0 : index = 15;
1525 : }
1526 4404 : else if ( SWB_tenv_tmp[n_band] < 0 )
1527 : {
1528 1677 : index = 0;
1529 : }
1530 : else
1531 : {
1532 2727 : index = (int16_t) ( SWB_tenv_tmp[n_band] + 0.5f );
1533 : }
1534 :
1535 4404 : push_indice( hBstr, IND_SWB_TENV, index, 4 );
1536 : }
1537 :
1538 1101 : MSVQ_Interpol_Tran( SWB_fenv, indice );
1539 :
1540 1101 : push_indice( hBstr, IND_SWB_FENV, indice[0], 7 );
1541 1101 : push_indice( hBstr, IND_SWB_FENV, indice[1], 6 );
1542 : }
1543 : else
1544 : {
1545 : /* Energy for the different bands and global energies */
1546 95305 : global_gain = 0;
1547 1429575 : for ( n_band = 0; n_band < SWB_FENV; n_band++ )
1548 : {
1549 1334270 : energy = EPSILON;
1550 31831870 : for ( n_coeff = swb_bwe_subband[n_band] + st_offset; n_coeff < swb_bwe_subband[n_band + 1] + st_offset; n_coeff++ )
1551 : {
1552 30497600 : energy += yos[n_coeff] * yos[n_coeff];
1553 : }
1554 :
1555 1334270 : if ( n_band < SWB_FENV - 2 )
1556 : {
1557 1143660 : global_gain += energy;
1558 : }
1559 1334270 : SWB_fenv[n_band] = energy;
1560 : }
1561 :
1562 95305 : global_gain *= 0.5f;
1563 :
1564 95305 : mode = FD_BWE_class( yos, global_gain, tilt_nb, st );
1565 :
1566 95305 : push_indice( hBstr, IND_SWB_CLASS, mode, 2 );
1567 :
1568 95305 : energy_control( st, ACELP_CORE, mode, -1, yos, st_offset, energy_factor );
1569 :
1570 1429575 : for ( n_band = 0; n_band < SWB_FENV; n_band++ )
1571 : {
1572 1334270 : SWB_fenv[n_band] *= energy_factor[n_band];
1573 1334270 : SWB_fenv[n_band] = 10.0f * (float) log10( SWB_fenv[n_band] * swb_inv_bwe_subband_width[n_band] );
1574 : }
1575 :
1576 95305 : freq_weights( SWB_fenv, w_NOR, w_env, SWB_FENV );
1577 :
1578 1429575 : for ( n_band = 0; n_band < SWB_FENV; n_band++ )
1579 : {
1580 1334270 : SWB_fenv[n_band] -= Mean_env[n_band];
1581 : }
1582 :
1583 : /* Energy VQ */
1584 95305 : msvq_interpol( SWB_fenv, w_env, indice );
1585 :
1586 95305 : push_indice( hBstr, IND_SWB_FENV, indice[0], 5 );
1587 95305 : push_indice( hBstr, IND_SWB_FENV, indice[1], 7 );
1588 95305 : push_indice( hBstr, IND_SWB_FENV, indice[2], 6 );
1589 95305 : push_indice( hBstr, IND_SWB_FENV, indice[3], 5 );
1590 95305 : push_indice( hBstr, IND_SWB_FENV, indice[4], 6 );
1591 : }
1592 :
1593 96406 : st->hBWE_FD->prev_mode = mode;
1594 96406 : st->hBWE_FD->prev_global_gain = global_gain;
1595 :
1596 96406 : return mode;
1597 : }
1598 :
1599 :
1600 : /*-------------------------------------------------------------------*
1601 : * decision_hq_generic_class()
1602 : *
1603 : *-------------------------------------------------------------------*/
1604 :
1605 23742 : static int16_t decision_hq_generic_class(
1606 : const float *coefs, /* i : original MDCT spectrum */
1607 : const int16_t hq_generic_offset /* i : frequency offset of high frequency spectrum */
1608 : )
1609 : {
1610 : int16_t i, k;
1611 : float p, a, e;
1612 : float p2a;
1613 : float avgp2a;
1614 : int16_t nband;
1615 :
1616 23742 : if ( hq_generic_offset == HQ_GENERIC_FOFFSET_24K4 )
1617 : {
1618 22979 : nband = 10;
1619 : }
1620 : else
1621 : {
1622 763 : nband = 8;
1623 : }
1624 :
1625 23742 : avgp2a = 0.f;
1626 259636 : for ( k = 0; k < nband; k++ )
1627 : {
1628 235894 : a = 0.0f;
1629 235894 : p = 0.0f;
1630 5137606 : for ( i = swb_bwe_subband[k] + hq_generic_offset; i < swb_bwe_subband[k + 1] + hq_generic_offset; i++ )
1631 : {
1632 4901712 : e = coefs[i] * coefs[i];
1633 :
1634 4901712 : if ( e > p )
1635 : {
1636 857869 : p = e;
1637 : }
1638 :
1639 4901712 : a += e;
1640 : }
1641 :
1642 235894 : if ( a > 0.0f )
1643 : {
1644 235894 : a *= swb_inv_bwe_subband_width[k];
1645 235894 : p2a = 10.0f * (float) log10( p / a );
1646 235894 : avgp2a += p2a;
1647 : }
1648 : }
1649 :
1650 23742 : avgp2a /= (float) ( nband );
1651 :
1652 23742 : if ( avgp2a > 8.6f )
1653 : {
1654 264 : return HQ_GENERIC_EXC1;
1655 : }
1656 : else
1657 : {
1658 23478 : return HQ_GENERIC_EXC0;
1659 : }
1660 : }
1661 :
1662 : /*-------------------------------------------------------------------*
1663 : * hq_generic_hf_encoding()
1664 : *
1665 : *-------------------------------------------------------------------*/
1666 :
1667 25536 : void hq_generic_hf_encoding(
1668 : const float *coefs, /* i : MDCT coefficients of weighted original */
1669 : float *hq_generic_fenv, /* i/o: energy of SWB envelope */
1670 : const int16_t hq_generic_offset, /* i : frequency offset for extracting energy */
1671 : Encoder_State *st, /* i/o: encoder state structure */
1672 : int16_t *hq_generic_exc_clas, /* o : HF excitation class */
1673 : const int16_t length /* i : Length of spectrum */
1674 : )
1675 : {
1676 : int16_t n_coeff, n_band;
1677 : float energy;
1678 : float energy_factor[SWB_FENV], w_env[SWB_FENV];
1679 : int16_t indice[HQ_GENERIC_NVQIDX];
1680 : int16_t nenv;
1681 25536 : BSTR_ENC_HANDLE hBstr = st->hBstr;
1682 :
1683 25536 : if ( hq_generic_offset <= HQ_GENERIC_FOFFSET_24K4 )
1684 : {
1685 24651 : nenv = SWB_FENV;
1686 : }
1687 : else
1688 : {
1689 885 : nenv = SWB_FENV - 2;
1690 : }
1691 :
1692 381270 : for ( n_band = 0; n_band < nenv; n_band++ )
1693 : {
1694 355734 : energy = EPSILON;
1695 8470614 : for ( n_coeff = swb_bwe_subband[n_band] + hq_generic_offset; n_coeff < swb_bwe_subband[n_band + 1] + hq_generic_offset; n_coeff++ )
1696 : {
1697 8114880 : energy += coefs[n_coeff] * coefs[n_coeff];
1698 : }
1699 :
1700 355734 : hq_generic_fenv[n_band] = energy;
1701 : }
1702 :
1703 25536 : if ( length == L_SPEC48k )
1704 : {
1705 52220 : for ( n_band = 0; n_band < DIM_FB; n_band++ )
1706 : {
1707 39165 : energy = EPSILON;
1708 2127965 : for ( n_coeff = fb_bwe_subband[n_band]; n_coeff < fb_bwe_subband[n_band + 1]; n_coeff++ )
1709 : {
1710 2088800 : energy += coefs[n_coeff] * coefs[n_coeff];
1711 : }
1712 :
1713 39165 : hq_generic_fenv[n_band + nenv] = energy;
1714 : }
1715 : }
1716 :
1717 25536 : energy_control( st, HQ_CORE, -1, -1, coefs, hq_generic_offset, energy_factor );
1718 :
1719 25536 : if ( st->hHQ_core->hq_generic_speech_class == 1 )
1720 : {
1721 1794 : push_indice( hBstr, IND_HQ_SWB_EXC_SP_CLAS, 1, 1 );
1722 1794 : *hq_generic_exc_clas = HQ_GENERIC_SP_EXC;
1723 : }
1724 : else
1725 : {
1726 23742 : *hq_generic_exc_clas = decision_hq_generic_class( coefs, hq_generic_offset );
1727 23742 : push_indice( hBstr, IND_HQ_SWB_EXC_SP_CLAS, 0, 1 );
1728 23742 : push_indice( hBstr, IND_HQ_SWB_EXC_CLAS, *hq_generic_exc_clas, 1 );
1729 : }
1730 :
1731 381270 : for ( n_band = 0; n_band < nenv; n_band++ )
1732 : {
1733 355734 : hq_generic_fenv[n_band] *= energy_factor[n_band];
1734 355734 : hq_generic_fenv[n_band] = 10.0f * (float) log10( hq_generic_fenv[n_band] * swb_inv_bwe_subband_width[n_band] );
1735 : }
1736 :
1737 25536 : if ( length == L_SPEC48k )
1738 : {
1739 52220 : for ( n_band = 0; n_band < DIM_FB; n_band++ )
1740 : {
1741 39165 : hq_generic_fenv[n_band + nenv] = 10.0f * (float) log10( hq_generic_fenv[n_band + nenv] * fb_inv_bwe_subband_width[n_band] );
1742 : }
1743 : }
1744 :
1745 25536 : freq_weights( hq_generic_fenv, w_NOR, w_env, nenv );
1746 :
1747 381270 : for ( n_band = 0; n_band < nenv; n_band++ )
1748 : {
1749 355734 : hq_generic_fenv[n_band] -= Mean_env[n_band];
1750 : }
1751 :
1752 25536 : if ( length == L_SPEC48k )
1753 : {
1754 52220 : for ( n_band = 0; n_band < DIM_FB; n_band++ )
1755 : {
1756 39165 : hq_generic_fenv[n_band + nenv] -= Mean_env_fb[n_band];
1757 : }
1758 : }
1759 :
1760 : /* Energy VQ */
1761 25536 : if ( hq_generic_offset <= HQ_GENERIC_FOFFSET_24K4 )
1762 : {
1763 24651 : msvq_interpol( hq_generic_fenv, w_env, indice );
1764 : }
1765 : else
1766 : {
1767 885 : msvq_interpol_2( hq_generic_fenv, w_env, indice, nenv );
1768 : }
1769 :
1770 25536 : if ( length == L_SPEC48k )
1771 : {
1772 13055 : indice[5] = vqSimple_w( hq_generic_fenv + nenv, hq_generic_fenv + nenv, EnvCdbkFB, NULL, DIM_FB, N_CB_FB, 0 );
1773 : }
1774 :
1775 25536 : push_indice( hBstr, IND_SWB_FENV_HQ, indice[0], 5 );
1776 25536 : push_indice( hBstr, IND_SWB_FENV_HQ, indice[1], 7 );
1777 25536 : push_indice( hBstr, IND_SWB_FENV_HQ, indice[2], 6 );
1778 25536 : push_indice( hBstr, IND_SWB_FENV_HQ, indice[3], 5 );
1779 :
1780 25536 : if ( hq_generic_offset <= HQ_GENERIC_FOFFSET_24K4 )
1781 : {
1782 24651 : push_indice( hBstr, IND_SWB_FENV_HQ, indice[4], 6 );
1783 : }
1784 : else
1785 : {
1786 885 : push_indice( hBstr, IND_SWB_FENV_HQ, indice[4], 5 );
1787 : }
1788 :
1789 25536 : if ( length == L_SPEC48k )
1790 : {
1791 13055 : push_indice( hBstr, IND_FB_FENV_HQ, indice[5], 5 );
1792 : }
1793 :
1794 381270 : for ( n_band = 0; n_band < nenv; n_band++ )
1795 : {
1796 : Word16 tmp, frac, exp;
1797 : Word32 L_tmp;
1798 : #ifdef BASOP_NOGLOB
1799 : Flag Overflow;
1800 :
1801 355734 : Overflow = 0;
1802 : #endif
1803 355734 : tmp = add( (int16_t) ( hq_generic_fenv[n_band] * 256 ), (int16_t) ( Mean_env[n_band] * 256 ) ); /*Q8 */
1804 :
1805 355734 : L_tmp = L_mult( tmp, 21771 ); /* 0.166096 in Q17 -> Q26 */
1806 355734 : L_tmp = L_shr( L_tmp, 10 ); /* From Q26 to Q16 */
1807 355734 : frac = L_Extract_lc( L_tmp, &exp ); /* Extract exponent of L_tmp */
1808 :
1809 355734 : tmp = extract_l( Pow2( 13, frac ) ); /* Put 13 as exponent so that */
1810 : /* output of Pow2() will be: */
1811 : /* 16384 < Pow2() <= 32767 */
1812 355734 : exp = sub( exp, 13 );
1813 : #ifdef BASOP_NOGLOB
1814 355734 : tmp = shl_o( tmp, add( exp, 1 ), &Overflow ); /*Q1 */
1815 : #else
1816 : tmp = shl( tmp, add( exp, 1 ) ); /*Q1 */
1817 : #endif
1818 355734 : hq_generic_fenv[n_band] = (float) tmp * 0.5f; /*Q1 */
1819 : }
1820 :
1821 25536 : if ( length == L_SPEC48k )
1822 : {
1823 52220 : for ( n_band = 0; n_band < DIM_FB; n_band++ )
1824 : {
1825 : Word16 tmp, frac, exp;
1826 : Word32 L_tmp;
1827 :
1828 39165 : tmp = add( (int16_t) ( hq_generic_fenv[n_band + nenv] * 128 ), (int16_t) ( Mean_env_fb[n_band] * 128 ) ); /*Q7 */
1829 39165 : L_tmp = L_mult( tmp, 21771 ); /* 0.166096 in Q17 -> Q25 */
1830 39165 : L_tmp = L_shr( L_tmp, 9 ); /* From Q25 to Q16 */
1831 39165 : frac = L_Extract_lc( L_tmp, &exp ); /* Extract exponent of L_tmp */
1832 :
1833 39165 : tmp = extract_l( Pow2( 13, frac ) ); /* Put 13 as exponent so that */
1834 : /* output of Pow2() will be: */
1835 : /* 16384 < Pow2() <= 32767 */
1836 39165 : exp = sub( exp, 13 );
1837 39165 : tmp = shl( tmp, add( exp, 1 ) ); /*Q1 */
1838 39165 : hq_generic_fenv[add( n_band, nenv )] = (float) tmp * 0.5f;
1839 : }
1840 : }
1841 :
1842 25536 : return;
1843 : }
1844 :
1845 :
1846 : /*-------------------------------------------------------------------*
1847 : * fd_bwe_enc_init()
1848 : *
1849 : * Initialize FD BWE state structure at the encoder
1850 : *-------------------------------------------------------------------*/
1851 :
1852 76346 : void fd_bwe_enc_init(
1853 : FD_BWE_ENC_HANDLE hBWE_FD /* i/o: FD BWE data handle */
1854 : )
1855 : {
1856 76346 : set_f( hBWE_FD->new_input_hp, 0, NS2SA( 16000, ACELP_LOOK_NS + DELAY_FD_BWE_ENC_12k8_NS + DELAY_FIR_RESAMPL_NS - DELAY_CLDFB_NS ) );
1857 76346 : set_f( hBWE_FD->old_input, 0, NS2SA( 48000, DELAY_FD_BWE_ENC_12k8_NS + DELAY_FIR_RESAMPL_NS ) );
1858 76346 : set_f( hBWE_FD->old_input_wb, 0, NS2SA( 16000, DELAY_FD_BWE_ENC_12k8_NS ) );
1859 76346 : set_f( hBWE_FD->old_input_lp, 0, NS2SA( 16000, ACELP_LOOK_NS + DELAY_FD_BWE_ENC_16k_NS ) );
1860 76346 : set_f( hBWE_FD->old_syn_12k8_16k, 0, NS2SA( 16000, DELAY_FD_BWE_ENC_NS ) );
1861 :
1862 76346 : hBWE_FD->prev_mode = NORMAL;
1863 76346 : set_f( hBWE_FD->old_wtda_swb, 0, L_FRAME48k );
1864 76346 : hBWE_FD->prev_L_swb_norm1 = 8;
1865 76346 : hBWE_FD->prev_global_gain = 0.0f;
1866 76346 : hBWE_FD->modeCount = 0;
1867 76346 : hBWE_FD->EnergyLF = 0.0f;
1868 76346 : hBWE_FD->mem_old_wtda_swb = 0.0f;
1869 :
1870 76346 : set_f( hBWE_FD->old_fdbwe_speech, 0.0f, L_FRAME48k );
1871 76346 : hBWE_FD->mem_deemph_old_syn = 0.0f;
1872 :
1873 76346 : return;
1874 : }
|