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 15420 : 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 15420 : 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 15420 : hBWE_FD = st->hBWE_FD;
84 :
85 15420 : if ( st->extl_brate > 0 )
86 : {
87 : /*---------------------------------------------------------------------*
88 : * Delay the original input signal to be synchronized with ACELP core synthesis
89 : *---------------------------------------------------------------------*/
90 :
91 5522 : set_f( old_input, 0, NS2SA( 16000, DELAY_FD_BWE_ENC_12k8_NS + DELAY_FIR_RESAMPL_NS ) + L_FRAME16k );
92 :
93 5522 : Sample_Delay_WB_BWE = NS2SA( 16000, DELAY_FD_BWE_ENC_12k8_NS );
94 :
95 5522 : new_input = old_input + Sample_Delay_WB_BWE;
96 5522 : mvr2r( hBWE_FD->old_input_wb, old_input, Sample_Delay_WB_BWE );
97 5522 : mvr2r( new_wb_speech, new_input, L_FRAME16k );
98 5522 : 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 5522 : wtda( old_input, wtda_old_input, hBWE_FD->old_wtda_swb, ALDO_WINDOW, ALDO_WINDOW, L_FRAME16k );
106 :
107 5522 : direct_transform( wtda_old_input, yorig, 0, L_FRAME16k, st->element_mode );
108 :
109 5522 : mode = WB_BWE_encoding( st, yorig, WB_fenv );
110 :
111 5522 : push_indice( st->hBstr, IND_WB_CLASS, mode - 2, 1 );
112 : }
113 :
114 15420 : hBWE_FD->prev_mode = mode;
115 :
116 15420 : return;
117 : }
118 :
119 : /*-------------------------------------------------------------------*
120 : * get_normalize_spec()
121 : *
122 : *-------------------------------------------------------------------*/
123 :
124 38099 : 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 38099 : set_f( SWB_signal, 0, HQ_GENERIC_HIGH0 + offset );
140 38099 : calc_normal_length( core, org, mode, extl, &L_swb_norm, prev_L_swb_norm );
141 :
142 38099 : if ( extl == SWB_BWE || extl == FB_BWE )
143 : {
144 27691 : if ( mode == HARMONIC )
145 : {
146 378 : mvr2r( org, &SWB_signal[240 + offset], 240 );
147 378 : mvr2r( &org[128], &SWB_signal[480 + offset], 80 );
148 : }
149 : else
150 : {
151 27313 : mvr2r( &org[112], &SWB_signal[240 + offset], 128 );
152 27313 : mvr2r( &org[112], &SWB_signal[368 + offset], 128 );
153 27313 : mvr2r( &org[176], &SWB_signal[496 + offset], 64 );
154 : }
155 27691 : frq_end = 560 + offset;
156 : }
157 10408 : else if ( extl == WB_BWE )
158 : {
159 5522 : if ( core_type == 0 )
160 : {
161 11 : mvr2r( &org[160], &SWB_signal[240], 80 );
162 : }
163 : else
164 : {
165 5511 : mvr2r( &org[80], &SWB_signal[240], 80 );
166 : }
167 5522 : frq_end = L_FRAME16k;
168 : }
169 : else
170 : {
171 4886 : mvr2r( org + HQ_GENERIC_OFFSET, SWB_signal + HQ_GENERIC_HIGH0 + offset, HQ_GENERIC_LEN0 );
172 4886 : mvr2r( org + HQ_GENERIC_OFFSET, SWB_signal + HQ_GENERIC_HIGH1 + offset, HQ_GENERIC_LEN0 );
173 4886 : if ( offset == HQ_GENERIC_FOFFSET_24K4 )
174 : {
175 4886 : mvr2r( org + HQ_GENERIC_LOW0, SWB_signal + HQ_GENERIC_HIGH2 + offset, HQ_GENERIC_END_FREQ - HQ_GENERIC_HIGH2 );
176 : }
177 4886 : frq_end = L_FRAME32k;
178 : }
179 :
180 : /* calculate envelope */
181 38099 : calc_norm_envelop( SWB_signal, envelope, L_swb_norm, frq_end - offset, offset );
182 :
183 : /* Normalize with envelope */
184 10904499 : for ( n_freq = swb_bwe_subband[0] + offset; n_freq < frq_end; n_freq++ )
185 : {
186 10866400 : SWB_signal[n_freq] /= envelope[n_freq];
187 : }
188 :
189 38099 : return;
190 : }
191 :
192 : /*-------------------------------------------------------------------*
193 : * WB_BWE_fenv_q()
194 : *
195 : * Scalar quantizer routine
196 : *-------------------------------------------------------------------*/
197 :
198 : /*! r: quantized gain index */
199 5522 : 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 5522 : int16_t i, j, indx = 0;
207 : float dist, min_dist;
208 5522 : const float *pit = cb;
209 :
210 5522 : min_dist = FLT_MAX;
211 182226 : for ( i = 0; i < cb_length; i++ )
212 : {
213 176704 : dist = 0.0f;
214 530112 : for ( j = 0; j < cb_dim; j++ )
215 : {
216 353408 : dist += ( x[j] - ( *pit ) ) * ( x[j] - ( *pit ) );
217 353408 : pit++;
218 : }
219 :
220 176704 : if ( dist < min_dist )
221 : {
222 23543 : min_dist = dist;
223 23543 : indx = i;
224 : }
225 : }
226 :
227 16566 : for ( j = 0; j < cb_dim; j++ )
228 : {
229 11044 : x[j] = cb[cb_dim * indx + j];
230 : }
231 :
232 5522 : 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 33213 : 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 33213 : int16_t i, j, k, noise, sharpMod = 0;
250 : float peak, mean[20], mag;
251 : float sharpPeak;
252 33213 : const float *input_hi = 0;
253 : float sharp;
254 33213 : float gain_tmp = 0;
255 : int16_t mode;
256 33213 : float meanH, mean_d = 0;
257 : int16_t sharplimit;
258 : int16_t numsharp;
259 : int16_t numharmonic;
260 :
261 33213 : mode = NORMAL;
262 33213 : k = 0;
263 33213 : noise = 0;
264 33213 : sharpPeak = 0;
265 33213 : numsharp = 0;
266 33213 : numharmonic = 4;
267 33213 : sharplimit = 10;
268 :
269 33213 : if ( st->extl == SWB_BWE || st->extl == FB_BWE )
270 : {
271 27691 : input_hi = &fSpectrum[256];
272 27691 : numsharp = NUM_SHARP;
273 27691 : if ( ( st->last_extl == SWB_BWE && st->extl == SWB_BWE ) || ( st->last_extl == FB_BWE && st->extl == FB_BWE ) )
274 : {
275 23485 : gain_tmp = fGain / ( st->hBWE_FD->prev_global_gain + EPSILON );
276 23485 : if ( st->hBWE_FD->prev_mode == TRANSIENT )
277 : {
278 47 : numharmonic = numharmonic * 2;
279 : }
280 23438 : else if ( st->hBWE_FD->prev_mode == NORMAL || st->hBWE_FD->prev_mode == NOISE )
281 : {
282 23182 : numharmonic = 3 * numharmonic / 2;
283 : }
284 : }
285 : else
286 : {
287 4206 : gain_tmp = 1;
288 4206 : if ( st->hBWE_FD->prev_mode == HARMONIC )
289 : {
290 0 : numharmonic = numharmonic / 2;
291 0 : sharplimit = sharplimit / 2;
292 : }
293 : else
294 : {
295 4206 : numharmonic = numharmonic * 2;
296 4206 : sharplimit = sharplimit * 2;
297 : }
298 : }
299 : }
300 5522 : else if ( st->extl == WB_BWE )
301 : {
302 5522 : input_hi = &fSpectrum[224];
303 5522 : numsharp = NUM_SHARP / 3;
304 5522 : if ( st->hBWE_FD->prev_mode == HARMONIC )
305 : {
306 1478 : numharmonic = numharmonic / 4;
307 : }
308 : else
309 : {
310 4044 : numharmonic = numharmonic / 2;
311 : }
312 5522 : if ( st->last_extl != WB_BWE )
313 : {
314 287 : if ( st->hBWE_FD->prev_mode == HARMONIC )
315 : {
316 0 : sharplimit = sharplimit / 2;
317 : }
318 : else
319 : {
320 287 : sharplimit = sharplimit * 2;
321 : }
322 : }
323 : }
324 :
325 33213 : meanH = EPSILON;
326 298998 : for ( i = 0; i < numsharp; i++ )
327 : {
328 265785 : peak = 0.0f;
329 265785 : mean[i] = 0;
330 :
331 8770905 : for ( j = 0; j < SHARP_WIDTH; j++ )
332 : {
333 8505120 : mag = (float) fabs( *input_hi );
334 8505120 : if ( mag > peak )
335 : {
336 1082294 : peak = mag;
337 : }
338 8505120 : mean[i] += mag;
339 8505120 : input_hi++;
340 : }
341 265785 : meanH += mean[i];
342 :
343 265785 : if ( mean[i] != peak )
344 : {
345 265785 : sharp = (float) ( peak * ( SHARP_WIDTH - 1 ) / ( mean[i] - peak ) );
346 : }
347 : else
348 : {
349 0 : sharp = 0.0f;
350 : }
351 :
352 265785 : if ( sharp > 4.5 && peak > 8 )
353 : {
354 26527 : k += 1;
355 : }
356 239258 : else if ( sharp < 3.0 )
357 : {
358 103471 : noise += 1;
359 : }
360 :
361 265785 : if ( sharp > sharpPeak )
362 : {
363 85406 : sharpPeak = sharp;
364 : }
365 : }
366 :
367 33213 : if ( st->extl == SWB_BWE || st->extl == FB_BWE )
368 : {
369 27691 : if ( k >= numharmonic && gain_tmp > 0.5f && gain_tmp < 1.8f && sharpPeak > sharplimit )
370 : {
371 340 : sharpMod = 1;
372 : }
373 : else
374 : {
375 27351 : sharpMod = 0;
376 : }
377 27691 : meanH /= 288;
378 27691 : mean_d = 0.0f;
379 276910 : for ( i = 0; i < NUM_SHARP; i++ )
380 : {
381 249219 : mean_d += (float) fabs( mean[i] / 32 - meanH );
382 : }
383 : }
384 5522 : else if ( st->extl == WB_BWE )
385 : {
386 5522 : if ( k >= numharmonic && sharpPeak > sharplimit )
387 : {
388 1012 : sharpMod = 1;
389 : }
390 : else
391 : {
392 4510 : sharpMod = 0;
393 : }
394 : }
395 :
396 33213 : if ( sharpMod && st->hBWE_FD->modeCount < 12 )
397 : {
398 1006 : st->hBWE_FD->modeCount++;
399 : }
400 32207 : else if ( sharpMod == 0 && st->hBWE_FD->modeCount > 0 )
401 : {
402 730 : st->hBWE_FD->modeCount--;
403 : }
404 :
405 33213 : if ( st->hBWE_FD->modeCount >= 2 )
406 : {
407 1595 : sharpMod = 1;
408 : }
409 :
410 33213 : if ( sharpMod )
411 : {
412 1862 : mode = HARMONIC;
413 : }
414 31351 : else if ( st->extl == SWB_BWE || st->extl == FB_BWE )
415 : {
416 27313 : if ( noise > 4 && mean_d < 4.8f * meanH && tilt_nb < 5 )
417 : {
418 2432 : mode = NOISE;
419 : }
420 : }
421 :
422 33213 : return mode;
423 : }
424 :
425 : /*-------------------------------------------------------------------*
426 : * WB_BWE_encoding()
427 : *
428 : * WB BWE main encoder
429 : *-------------------------------------------------------------------*/
430 :
431 : /*! r: classification of wb signal */
432 5522 : 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 5522 : global_gain = EPSILON;
447 :
448 16566 : for ( i = 0, n_band = 0; i < 2; i++ )
449 : {
450 11044 : energy = EPSILON;
451 452804 : for ( n_coeff = swb_bwe_subband[n_band]; n_coeff < swb_bwe_subband[n_band + 2]; n_coeff++ )
452 : {
453 441760 : energy += yos[n_coeff] * yos[n_coeff];
454 : }
455 :
456 11044 : WB_fenv[i] = energy;
457 11044 : n_band += 2;
458 11044 : global_gain += energy;
459 : }
460 :
461 5522 : mode = FD_BWE_class( yos, global_gain, 0, st );
462 :
463 5522 : energy_control( st, ACELP_CORE, mode, st->coder_type, yos, 0, energy_factor );
464 :
465 16566 : for ( i = 0; i < 2; i++ )
466 : {
467 11044 : WB_fenv[i] = (float) ( log10( WB_fenv[i] * energy_factor[i << 1] / 40 ) * FAC_LOG2 );
468 : }
469 :
470 5522 : index = WB_BWE_fenv_q( WB_fenv, F_2_5, 32, 2 );
471 :
472 5522 : push_indice( st->hBstr, IND_WB_FENV, index, 5 );
473 :
474 :
475 5522 : return ( mode );
476 : }
477 :
478 :
479 : /*-------------------------------------------------------------------*
480 : * swb_bwe_enc()
481 : *
482 : * SWB BWE encoder (only for 32kHz signals)
483 : *-------------------------------------------------------------------*/
484 :
485 27813 : 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 27813 : hBWE_TD = st->hBWE_TD;
511 27813 : hBWE_FD = st->hBWE_FD;
512 :
513 27813 : ener_adjust_quan = 0.0f;
514 27813 : idxGain = 0;
515 :
516 : /*---------------------------------------------------------------------*
517 : * Delay the original input signal to be synchronized with ACELP core synthesis
518 : *---------------------------------------------------------------------*/
519 :
520 27813 : if ( st->extl == FB_BWE )
521 : {
522 10277 : inner_frame = L_FRAME48k;
523 10277 : inner_Fs = 48000;
524 : }
525 : else
526 : {
527 17536 : inner_frame = L_FRAME32k;
528 17536 : inner_Fs = 32000;
529 : }
530 :
531 27813 : set_f( old_input, 0.0f, NS2SA( inner_Fs, DELAY_FD_BWE_ENC_12k8_NS + DELAY_FIR_RESAMPL_NS ) + inner_frame );
532 :
533 27813 : if ( st->L_frame == L_FRAME )
534 : {
535 17018 : Sample_Delay_SWB_BWE = NS2SA( inner_Fs, DELAY_FD_BWE_ENC_12k8_NS + DELAY_FIR_RESAMPL_NS );
536 17018 : Sample_Delay_HP = NS2SA( 16000, ACELP_LOOK_NS + DELAY_FD_BWE_ENC_12k8_NS + DELAY_FIR_RESAMPL_NS - DELAY_CLDFB_NS );
537 17018 : if ( st->element_mode == IVAS_CPE_TD )
538 : {
539 19 : 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 17018 : Sample_Delay_LP = NS2SA( 12800, ACELP_LOOK_NS + DELAY_FD_BWE_ENC_12k8_NS );
542 :
543 17018 : if ( st->element_mode > EVS_MONO )
544 : {
545 17012 : Sample_Delay_SWB_BWE -= NS2SA( inner_Fs, DELAY_FIR_RESAMPL_NS );
546 17012 : Sample_Delay_HP -= NS2SA( 16000, DELAY_FIR_RESAMPL_NS );
547 :
548 17012 : if ( st->element_mode == IVAS_CPE_DFT )
549 : {
550 8809 : mvr2r( old_input_12k8 + L_INP_MEM - Sample_Delay_LP, hBWE_FD->old_input_lp, Sample_Delay_LP );
551 8809 : mvr2r( hBWE_TD->old_speech_shb + L_LOOK_16k + L_SUBFR16k - Sample_Delay_HP, new_input_hp, Sample_Delay_HP );
552 : }
553 : }
554 :
555 17018 : mvr2r( hBWE_FD->old_input_lp, old_input_lp, Sample_Delay_LP );
556 17018 : mvr2r( old_input_12k8 + L_INP_MEM, &old_input_lp[Sample_Delay_LP], L_FRAME - Sample_Delay_LP );
557 17018 : 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 10795 : Sample_Delay_SWB_BWE = NS2SA( inner_Fs, DELAY_FD_BWE_ENC_16k_NS + DELAY_FIR_RESAMPL_NS );
562 10795 : Sample_Delay_HP = NS2SA( 16000, ACELP_LOOK_NS + DELAY_FD_BWE_ENC_16k_NS + DELAY_FIR_RESAMPL_NS - DELAY_CLDFB_NS );
563 10795 : if ( st->element_mode == IVAS_CPE_TD )
564 : {
565 17 : 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 10795 : Sample_Delay_LP = NS2SA( 16000, ACELP_LOOK_NS + DELAY_FD_BWE_ENC_16k_NS );
568 :
569 10795 : if ( st->element_mode > EVS_MONO )
570 : {
571 10795 : Sample_Delay_SWB_BWE -= NS2SA( inner_Fs, DELAY_FIR_RESAMPL_NS );
572 10795 : Sample_Delay_HP -= NS2SA( 16000, DELAY_FIR_RESAMPL_NS );
573 :
574 10795 : if ( st->element_mode == IVAS_CPE_DFT )
575 : {
576 652 : mvr2r( old_input_16k + L_INP_MEM - Sample_Delay_LP, hBWE_FD->old_input_lp, Sample_Delay_LP );
577 652 : mvr2r( hBWE_TD->old_speech_shb + L_LOOK_16k + L_SUBFR16k - Sample_Delay_HP, new_input_hp, Sample_Delay_HP );
578 : }
579 : }
580 :
581 10795 : mvr2r( hBWE_FD->old_input_lp, old_input_lp, Sample_Delay_LP );
582 10795 : mvr2r( old_input_16k + L_INP_MEM, &old_input_lp[Sample_Delay_LP], L_FRAME16k - Sample_Delay_LP );
583 10795 : mvr2r( old_input_16k + L_INP_MEM + L_FRAME16k - Sample_Delay_LP, hBWE_FD->old_input_lp, Sample_Delay_LP );
584 : }
585 :
586 27813 : mvr2r( hBWE_FD->new_input_hp, new_input_hp, Sample_Delay_HP );
587 27813 : mvr2r( shb_speech, new_input_hp + Sample_Delay_HP, L_FRAME16k - Sample_Delay_HP );
588 27813 : mvr2r( shb_speech + L_FRAME16k - Sample_Delay_HP, hBWE_FD->new_input_hp, Sample_Delay_HP );
589 :
590 27813 : new_input = old_input + Sample_Delay_SWB_BWE;
591 27813 : mvr2r( hBWE_FD->old_input, old_input, Sample_Delay_SWB_BWE );
592 27813 : mvr2r( new_swb_speech, new_input, inner_frame );
593 27813 : 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 27813 : calc_tilt_bwe( old_input_lp, &tilt_nb, L_FRAME );
600 :
601 : /*---------------------------------------------------------------------*
602 : * SWB BWE encoding
603 : * FB BWE encoding
604 : *---------------------------------------------------------------------*/
605 :
606 27813 : 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 27813 : 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 27813 : direct_transform( wtda_old_input, yorig, 0, inner_frame, st->element_mode );
619 :
620 : /* high-band gain control in case of BWS */
621 27813 : if ( st->bwidth_sw_cnt > 0 )
622 : {
623 11 : 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 27813 : if ( st->extl == FB_BWE )
628 : {
629 10277 : fb_band_begin = FB_BAND_BEGIN;
630 10277 : if ( st->L_frame == L_FRAME )
631 : {
632 749 : fb_band_begin = FB_BAND_BEGIN_12k8;
633 : }
634 :
635 10277 : energy_fbe_fb = sum2_f( yorig + fb_band_begin, FB_BAND_WIDTH ) + EPSILON;
636 10277 : ener_low = EPSILON;
637 1860137 : for ( i = fb_band_begin - FB_BAND_WIDTH; i < fb_band_begin; i++ )
638 : {
639 1849860 : ener_low += yorig[i] * yorig[i];
640 : }
641 :
642 10277 : fb_ener_adjust = (float) sqrt( energy_fbe_fb / ener_low );
643 10277 : fb_ener_adjust = min( fb_ener_adjust, FB_MAX_GAIN_VAR );
644 10277 : 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 27813 : if ( st->L_frame == L_FRAME16k )
649 : {
650 10795 : 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 17018 : 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 27813 : if ( st->extl == FB_BWE )
659 : {
660 10277 : push_indice( st->hBstr, IND_FB_SLOPE, idxGain, NUM_BITS_FB_FRAMEGAIN );
661 : }
662 :
663 27813 : return;
664 : }
665 :
666 :
667 : /*-------------------------------------------------------------------*
668 : * Freq_weights()
669 : *
670 : *-------------------------------------------------------------------*/
671 :
672 32577 : 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 32577 : min_b = Band_Ener[0];
685 32577 : max_b = Band_Ener[0];
686 456078 : for ( i = 1; i < Nbands; i++ )
687 : {
688 423501 : if ( Band_Ener[i] < min_b )
689 : {
690 128229 : min_b = Band_Ener[i];
691 : }
692 423501 : if ( Band_Ener[i] > max_b )
693 : {
694 45828 : max_b = Band_Ener[i];
695 : }
696 : }
697 :
698 : /* Find weighting function */
699 32577 : tmp = 1.f / ( max_b - min_b );
700 488655 : for ( i = 0; i < Nbands; i++ )
701 : {
702 456078 : w1[i] = ( Band_Ener[i] - min_b ) * tmp + 1.f; /*1<= var <=2 */
703 456078 : w2[i] = f_weighting[i]; /*1~0.75*/
704 456078 : w_env[i] = w1[i] * w2[i];
705 : }
706 :
707 32577 : return;
708 : }
709 :
710 :
711 : /*-------------------------------------------------------------------*
712 : * VQwithCand_w()
713 : *
714 : *-------------------------------------------------------------------*/
715 :
716 32699 : 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 32699 : if ( flag )
733 : {
734 32577 : set_f( dist_min, 3.402823466e+38F, surv ); /* FLT_MAX */
735 :
736 97731 : for ( i = 0; i < surv; i++ )
737 : {
738 65154 : index[i] = i;
739 : }
740 :
741 32577 : p_E_ROM_dico = E_ROM_dico;
742 :
743 1075041 : for ( i = 0; i < E_ROM_dico_size; i++ )
744 : {
745 1042464 : dist = x[0] - *p_E_ROM_dico++;
746 1042464 : dist *= ( dist * w[0] );
747 :
748 7297248 : for ( j = 1; j < dim; j++ )
749 : {
750 6254784 : temp1 = x[j] - *p_E_ROM_dico++;
751 6254784 : dist += temp1 * temp1 * w[j];
752 : }
753 :
754 2788075 : for ( k = 0; k < surv; k++ )
755 : {
756 1961293 : if ( dist < dist_min[k] )
757 : {
758 339317 : for ( l = surv - 1; l > k; l-- )
759 : {
760 123635 : dist_min[l] = dist_min[l - 1];
761 123635 : index[l] = index[l - 1];
762 : }
763 215682 : dist_min[k] = dist;
764 215682 : index[k] = i;
765 215682 : break;
766 : }
767 : }
768 : }
769 : }
770 : else
771 : {
772 122 : set_f( dist_min, 3.402823466e+38F, surv ); /* FLT_MAX */
773 :
774 488 : for ( i = 0; i < surv; i++ )
775 : {
776 366 : index[i] = i;
777 : }
778 :
779 122 : p_E_ROM_dico = E_ROM_dico;
780 :
781 15738 : for ( i = 0; i < E_ROM_dico_size; i++ )
782 : {
783 15616 : dist = x[0] - *p_E_ROM_dico++;
784 15616 : dist *= dist;
785 :
786 31232 : for ( j = 1; j < dim; j++ )
787 : {
788 15616 : temp1 = x[j] - *p_E_ROM_dico++;
789 15616 : dist += temp1 * temp1;
790 : }
791 :
792 59164 : for ( k = 0; k < surv; k++ )
793 : {
794 45023 : if ( dist < dist_min[k] )
795 : {
796 3300 : for ( l = surv - 1; l > k; l-- )
797 : {
798 1825 : dist_min[l] = dist_min[l - 1];
799 1825 : index[l] = index[l - 1];
800 : }
801 1475 : dist_min[k] = dist;
802 1475 : index[k] = i;
803 1475 : break;
804 : }
805 : }
806 : }
807 : }
808 :
809 32699 : return;
810 : }
811 :
812 : /*-------------------------------------------------------------------*
813 : * vqSimple_w()
814 : *
815 : *-------------------------------------------------------------------*/
816 :
817 264977 : 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 264977 : index = 0;
832 264977 : dist_min = FLT_MAX;
833 264977 : cbP = cb;
834 :
835 264977 : if ( flag )
836 : {
837 19024968 : for ( i = 0; i < l; i++ )
838 : {
839 18764352 : dist = x[0] - *cbP++;
840 18764352 : dist *= ( dist * w[0] );
841 64632768 : for ( j = 1; j < dim; j++ )
842 : {
843 45868416 : temp = x[j] - *cbP++;
844 45868416 : dist += temp * temp * w[j];
845 : }
846 :
847 18764352 : if ( dist < dist_min )
848 : {
849 1180098 : dist_min = dist;
850 1180098 : index = i;
851 : }
852 : }
853 : }
854 : else
855 : {
856 155625 : for ( i = 0; i < l; i++ )
857 : {
858 151264 : dist = x[0] - *cbP++;
859 151264 : dist *= dist;
860 430368 : for ( j = 1; j < dim; j++ )
861 : {
862 279104 : temp = x[j] - *cbP++;
863 279104 : dist += temp * temp;
864 : }
865 :
866 151264 : if ( dist < dist_min )
867 : {
868 13992 : dist_min = dist;
869 13992 : index = i;
870 : }
871 : }
872 : }
873 :
874 : /* Reading the selected vector */
875 264977 : mvr2r( &cb[index * dim], y, dim );
876 :
877 264977 : return ( index );
878 : }
879 :
880 : /*-------------------------------------------------------------------*
881 : * MSVQ_Interpol_Tran()
882 : *
883 : *-------------------------------------------------------------------*/
884 :
885 122 : 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 366 : for ( n_band = 0; n_band < DIM_TR1; n_band++ )
898 : {
899 244 : env_temp11[n_band] = SWB_env_energy[2 * n_band];
900 244 : env_temp12[n_band] = SWB_env_energy[2 * n_band + 1];
901 : }
902 :
903 122 : vqWithCand_w( env_temp11, Env_TR_Cdbk1, DIM_TR1, N_CB_TR1, candInd, N_CAND_TR, distCand, NULL, 0 );
904 :
905 122 : minDist = FLT_MAX;
906 488 : for ( k = 0; k < N_CAND_TR; k++ )
907 : {
908 1098 : for ( n_band = 0; n_band < DIM_TR1; n_band++ )
909 : {
910 732 : quant_tmp[n_band] = Env_TR_Cdbk1[candInd[k] * DIM_TR1 + n_band];
911 : }
912 :
913 732 : for ( n_band = 0; n_band < DIM_TR2 - 1; n_band++ )
914 : {
915 366 : quant_tmp2[n_band] = env_temp12[n_band] - ( ( quant_tmp[n_band] + quant_tmp[n_band + 1] ) / 2.f );
916 : }
917 366 : quant_tmp2[n_band] = env_temp12[n_band] - quant_tmp[n_band];
918 366 : ind_tmp[0] = vqSimple_w( quant_tmp2, quant_tmp2, Env_TR_Cdbk2, NULL, DIM_TR2, N_CB_TR2, 0 );
919 :
920 1098 : for ( n_band = 0; n_band < DIM_TR1; n_band++ )
921 : {
922 732 : quant_select[n_band * 2] = quant_tmp[n_band];
923 : }
924 :
925 732 : for ( n_band = 0; n_band < DIM_TR2 - 1; n_band++ )
926 : {
927 366 : quant_select[n_band * 2 + 1] = ( ( quant_tmp[n_band] + quant_tmp[n_band + 1] ) / 2.f ) + quant_tmp2[n_band];
928 : }
929 366 : quant_select[n_band * 2 + 1] = quant_tmp[n_band] + quant_tmp2[n_band];
930 :
931 366 : dist = 0.f;
932 1830 : for ( n_band = 0; n_band < SWB_FENV_TRANS; n_band++ )
933 : {
934 1464 : tmp_q = SWB_env_energy[n_band] - quant_select[n_band];
935 1464 : dist += tmp_q * tmp_q;
936 : }
937 :
938 : /* Check optimal candidate */
939 366 : if ( dist < minDist )
940 : {
941 124 : minDist = dist;
942 124 : indice[0] = candInd[k];
943 124 : indice[1] = ind_tmp[0];
944 : }
945 : }
946 :
947 122 : return;
948 : }
949 :
950 : /*-------------------------------------------------------------------*
951 : * MSVQ_Interpol()
952 : *
953 : *-------------------------------------------------------------------*/
954 32577 : 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 260616 : for ( n_band = 0; n_band < DIM11; n_band++ )
969 : {
970 228039 : env_temp11[n_band] = SWB_env_energy[2 * n_band];
971 228039 : env_temp12[n_band] = SWB_env_energy[2 * n_band + 1];
972 :
973 228039 : w_env11[n_band] = w_env[2 * n_band];
974 228039 : w_env12[n_band] = w_env[2 * n_band + 1];
975 : }
976 :
977 32577 : vqWithCand_w( env_temp11, EnvCdbk11, DIM11, N_CB11, candInd, N_CAND, distCand, w_env11, 1 );
978 :
979 32577 : minDist = FLT_MAX;
980 97731 : for ( k = 0; k < N_CAND; k++ )
981 : {
982 521232 : for ( n_band = 0; n_band < DIM11; n_band++ )
983 : {
984 456078 : quant_tmp1[n_band] = EnvCdbk11[candInd[k] * DIM11 + n_band];
985 456078 : quant_tmp2[n_band] = env_temp11[n_band] - quant_tmp1[n_band];
986 : }
987 :
988 65154 : ind_tmp[0] = vqSimple_w( quant_tmp2, quant_tmp2, EnvCdbk1st, w_env11, DIM1ST, N_CB1ST, 1 );
989 65154 : 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 521232 : for ( n_band = 0; n_band < DIM11; n_band++ )
993 : {
994 456078 : quant_tmp[n_band] = quant_tmp1[n_band] + quant_tmp2[n_band];
995 : }
996 :
997 456078 : for ( n_band = 0; n_band < DIM12 - 1; n_band++ )
998 : {
999 390924 : quant_tmp2[n_band] = env_temp12[n_band] - ( ( quant_tmp[n_band] + quant_tmp[n_band + 1] ) / 2.f );
1000 : }
1001 :
1002 65154 : quant_tmp2[n_band] = env_temp12[n_band] - quant_tmp[n_band];
1003 :
1004 65154 : ind_tmp[2] = vqSimple_w( quant_tmp2, quant_tmp2, EnvCdbk3rd, w_env12, DIM3RD, N_CB3RD, 1 );
1005 65154 : ind_tmp[3] = vqSimple_w( quant_tmp2 + DIM3RD, quant_tmp2 + DIM3RD, EnvCdbk4th, w_env12 + DIM3RD, DIM4TH, N_CB4TH, 1 );
1006 :
1007 521232 : for ( n_band = 0; n_band < DIM11; n_band++ )
1008 : {
1009 456078 : quant_select[n_band * 2] = quant_tmp[n_band];
1010 : }
1011 :
1012 456078 : for ( n_band = 0; n_band < DIM12 - 1; n_band++ )
1013 : {
1014 390924 : quant_select[n_band * 2 + 1] = ( ( quant_tmp[n_band] + quant_tmp[n_band + 1] ) / 2.f ) + quant_tmp2[n_band];
1015 : }
1016 65154 : quant_select[n_band * 2 + 1] = quant_tmp[n_band] + quant_tmp2[n_band];
1017 :
1018 65154 : dist = 0.f;
1019 977310 : for ( n_band = 0; n_band < SWB_FENV; n_band++ )
1020 : {
1021 912156 : tmp_q = SWB_env_energy[n_band] - quant_select[n_band];
1022 912156 : tmp_q = tmp_q * tmp_q;
1023 912156 : dist += tmp_q * w_env[n_band];
1024 : }
1025 :
1026 : /* Check optimal candidate */
1027 65154 : if ( dist < minDist )
1028 : {
1029 41218 : minDist = dist;
1030 41218 : mvr2r( quant_select, synth_energy, SWB_FENV );
1031 :
1032 41218 : indice[0] = candInd[k];
1033 41218 : indice[1] = ind_tmp[0];
1034 41218 : indice[2] = ind_tmp[1];
1035 41218 : indice[3] = ind_tmp[2];
1036 41218 : indice[4] = ind_tmp[3];
1037 : }
1038 : }
1039 :
1040 32577 : mvr2r( synth_energy, SWB_env_energy, SWB_FENV );
1041 :
1042 32577 : return;
1043 : }
1044 :
1045 : /*-------------------------------------------------------------------*
1046 : * msvq_interpol_2()
1047 : *
1048 : *
1049 : *-------------------------------------------------------------------*/
1050 :
1051 0 : 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 0 : for ( n_band = 0; n_band < DIM11 - 1; n_band++ )
1067 : {
1068 0 : env_temp11[n_band] = hq_generic_fenv[2 * n_band];
1069 0 : w_env11[n_band] = w_env[2 * n_band];
1070 : }
1071 0 : env_temp11[DIM11 - 1] = hq_generic_fenv[2 * ( DIM11 - 2 ) + 1];
1072 0 : w_env11[DIM11 - 1] = w_env[2 * ( DIM11 - 2 ) + 1];
1073 :
1074 0 : env_temp12[0] = hq_generic_fenv[0];
1075 0 : w_env12[0] = w_env[0];
1076 0 : for ( n_band = 1; n_band < DIM11 - 1; n_band++ )
1077 : {
1078 0 : env_temp12[n_band] = hq_generic_fenv[2 * n_band - 1];
1079 0 : w_env12[n_band] = w_env[2 * n_band - 1];
1080 : }
1081 :
1082 0 : vqWithCand_w( env_temp11, EnvCdbk11, DIM11, N_CB11, candInd, N_CAND, distCand, w_env11, 1 );
1083 :
1084 0 : minDist = FLT_MAX;
1085 0 : for ( k = 0; k < N_CAND; k++ )
1086 : {
1087 0 : for ( n_band = 0; n_band < DIM11; n_band++ )
1088 : {
1089 0 : quant_tmp1[n_band] = EnvCdbk11[candInd[k] * DIM11 + n_band];
1090 0 : quant_tmp2[n_band] = env_temp11[n_band] - quant_tmp1[n_band];
1091 : }
1092 :
1093 0 : ind_tmp[0] = vqSimple_w( quant_tmp2, quant_tmp2, EnvCdbk1st, w_env11, DIM1ST, N_CB1ST, 1 );
1094 0 : 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 0 : for ( n_band = 0; n_band < DIM11; n_band++ )
1098 : {
1099 0 : quant_tmp[n_band] = quant_tmp1[n_band] + quant_tmp2[n_band];
1100 : }
1101 :
1102 0 : quant_tmp2[0] = env_temp12[0] - quant_tmp[0];
1103 0 : for ( n_band = 1; n_band < DIM12 - 1; n_band++ )
1104 : {
1105 0 : quant_tmp2[n_band] = env_temp12[n_band] - ( ( quant_tmp[n_band - 1] + quant_tmp[n_band] ) / 2.f );
1106 : }
1107 :
1108 0 : ind_tmp[2] = vqSimple_w( quant_tmp2, quant_tmp2, EnvCdbk3rd, w_env12, DIM3RD, N_CB3RD, 1 );
1109 0 : ind_tmp[3] = vqSimple_w( quant_tmp2 + DIM3RD, quant_tmp2 + DIM3RD, EnvCdbk3rd, w_env12 + DIM3RD, DIM3RD, N_CB3RD, 1 );
1110 :
1111 0 : for ( n_band = 0; n_band < DIM12 - 1; n_band++ )
1112 : {
1113 0 : quant_select[n_band * 2] = quant_tmp[n_band];
1114 : }
1115 0 : quant_select[11] = quant_tmp[DIM12 - 1];
1116 :
1117 0 : quant_select[0] += quant_tmp2[0];
1118 0 : for ( n_band = 1; n_band < DIM12 - 1; n_band++ )
1119 : {
1120 0 : quant_select[n_band * 2 - 1] = ( ( quant_tmp[n_band - 1] + quant_tmp[n_band] ) / 2.f ) + quant_tmp2[n_band];
1121 : }
1122 :
1123 0 : dist = 0.f;
1124 0 : for ( n_band = 0; n_band < SWB_FENV - 2; n_band++ )
1125 : {
1126 0 : tmp_q = hq_generic_fenv[n_band] - quant_select[n_band];
1127 0 : tmp_q = tmp_q * tmp_q;
1128 0 : dist += tmp_q * w_env[n_band];
1129 : }
1130 :
1131 : /* Check optimal candidate */
1132 0 : if ( dist < minDist )
1133 : {
1134 0 : minDist = dist;
1135 0 : mvr2r( quant_select, synth_energy, SWB_FENV - 2 );
1136 0 : synth_energy[SWB_FENV - 2] = 0;
1137 0 : synth_energy[SWB_FENV - 1] = 0;
1138 :
1139 0 : indice[0] = candInd[k];
1140 0 : indice[1] = ind_tmp[0];
1141 0 : indice[2] = ind_tmp[1];
1142 0 : indice[3] = ind_tmp[2];
1143 0 : indice[4] = ind_tmp[3];
1144 : }
1145 : }
1146 :
1147 0 : mvr2r( synth_energy, hq_generic_fenv, nenv );
1148 :
1149 0 : return;
1150 : }
1151 :
1152 : /*-------------------------------------------------------------------*
1153 : * calculate_tonality()
1154 : *
1155 : * Calculate tonality
1156 : *-------------------------------------------------------------------*/
1157 :
1158 467122 : 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 467122 : max_val = EPSILON;
1173 11333522 : for ( n_coeff = 0; n_coeff < length; n_coeff++ )
1174 : {
1175 10866400 : org_spec[n_coeff] = (float) fabs( org[n_coeff] );
1176 :
1177 10866400 : if ( max_val < org_spec[n_coeff] )
1178 : {
1179 1741232 : max_val = org_spec[n_coeff];
1180 : }
1181 : }
1182 467122 : mult = 25.f / max_val;
1183 :
1184 11333522 : for ( n_coeff = 0; n_coeff < length; n_coeff++ )
1185 : {
1186 10866400 : org_spec[n_coeff] *= mult;
1187 : }
1188 :
1189 467122 : max_val = EPSILON;
1190 11333522 : for ( n_coeff = 0; n_coeff < length; n_coeff++ )
1191 : {
1192 10866400 : gen_spec[n_coeff] = (float) fabs( gen[n_coeff] );
1193 :
1194 10866400 : if ( max_val < gen_spec[n_coeff] )
1195 : {
1196 1658070 : max_val = gen_spec[n_coeff];
1197 : }
1198 : }
1199 467122 : mult = 25.f / max_val;
1200 :
1201 11333522 : for ( n_coeff = 0; n_coeff < length; n_coeff++ )
1202 : {
1203 10866400 : gen_spec[n_coeff] *= mult;
1204 : }
1205 :
1206 467122 : inv_len = 1.f / (float) length;
1207 :
1208 467122 : am_org = EPSILON;
1209 467122 : am_gen = EPSILON;
1210 467122 : gm_org = 1.f;
1211 467122 : gm_gen = 1.f;
1212 :
1213 11333522 : for ( n_coeff = 0; n_coeff < length; n_coeff++ )
1214 : {
1215 10866400 : am_org += org_spec[n_coeff];
1216 10866400 : am_gen += gen_spec[n_coeff];
1217 10866400 : gm_org *= org_spec[n_coeff];
1218 10866400 : gm_gen *= gen_spec[n_coeff];
1219 : }
1220 :
1221 467122 : *SFM_org = 10.f * ( (float) log10( am_org * inv_len ) - inv_len * (float) log10( gm_org ) );
1222 467122 : *SFM_org = max( 0.0001f, min( *SFM_org, 5.993f ) );
1223 467122 : *SFM_gen = 10.f * ( (float) log10( am_gen * inv_len ) - inv_len * (float) log10( gm_gen ) );
1224 467122 : *SFM_gen = max( 0.0001f, min( *SFM_gen, 5.993f ) );
1225 :
1226 467122 : return;
1227 : }
1228 :
1229 : /*-------------------------------------------------------------------*
1230 : * energy_control()
1231 : *
1232 : *-------------------------------------------------------------------*/
1233 :
1234 38099 : 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 38099 : int16_t max_band = SWB_FENV, band_step = 1;
1249 :
1250 38099 : if ( core == ACELP_CORE )
1251 : {
1252 33213 : gamma = 0.35f;
1253 33213 : if ( coder_type != AUDIO && st->total_brate <= ACELP_8k00 )
1254 : {
1255 23 : core_type = 0;
1256 : }
1257 : else
1258 : {
1259 33190 : core_type = 1;
1260 : }
1261 :
1262 33213 : get_normalize_spec( core, st->extl, mode, core_type, org, SWB_signal, &( st->hBWE_FD->prev_L_swb_norm1 ), offset );
1263 :
1264 33213 : if ( st->extl == WB_BWE )
1265 : {
1266 5522 : max_band = 4;
1267 5522 : band_step = 2;
1268 : }
1269 : }
1270 : else /* HQ core */
1271 : {
1272 4886 : gamma = 0.55f;
1273 4886 : get_normalize_spec( core, -1, mode, -1, org, SWB_signal, &( st->hBWE_FD->prev_L_swb_norm1 ), offset );
1274 :
1275 4886 : if ( offset == HQ_GENERIC_FOFFSET_32K )
1276 : {
1277 0 : max_band = 12;
1278 : }
1279 : }
1280 :
1281 505221 : for ( n_band = 0; n_band < max_band; n_band += band_step )
1282 : {
1283 467122 : 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 467122 : if ( SFM_gen[n_band] < 0.75 * SFM_org[n_band] )
1286 : {
1287 135285 : energy_factor[n_band] = ( SFM_gen[n_band] / SFM_org[n_band] );
1288 :
1289 135285 : if ( energy_factor[n_band] < gamma )
1290 : {
1291 13697 : energy_factor[n_band] = gamma;
1292 : }
1293 : }
1294 : else
1295 : {
1296 331837 : energy_factor[n_band] = 1.0f;
1297 : }
1298 : }
1299 :
1300 38099 : return;
1301 : }
1302 :
1303 : /*-------------------------------------------------------------------*
1304 : * SWB_BWE_encoding()
1305 : *
1306 : * SWB BWE encoder
1307 : *-------------------------------------------------------------------*/
1308 :
1309 27813 : 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 27813 : BSTR_ENC_HANDLE hBstr = st->hBstr;
1334 :
1335 27813 : if ( st->L_frame == L_FRAME )
1336 : {
1337 17018 : L = L_SUBFR;
1338 : }
1339 : else
1340 : {
1341 10795 : L = L_SUBFR16k;
1342 : }
1343 :
1344 : /* HF transient detect */
1345 27813 : IsTransient = detect_transient( st, insig_hp, L_FRAME16k );
1346 :
1347 : /* LF transient detect */
1348 27813 : IsTransient_LF = 0;
1349 139065 : for ( n_band = 0; n_band < 4; n_band++ )
1350 : {
1351 111252 : energy = EPSILON;
1352 7922260 : for ( i = 0; i < L; i++ )
1353 : {
1354 7811008 : energy += insig_lp[i + n_band * L] * insig_lp[i + n_band * L];
1355 : }
1356 :
1357 111252 : if ( energy > 5.5f * st->hBWE_FD->EnergyLF )
1358 : {
1359 5494 : IsTransient_LF = 1;
1360 : }
1361 :
1362 111252 : st->hBWE_FD->EnergyLF = energy;
1363 : }
1364 :
1365 27813 : calc_tilt_bwe( insig, &tilt, L_FRAME32k );
1366 :
1367 27813 : if ( IsTransient == 1 && ( tilt > 8.0 || st->clas > 1 ) )
1368 : {
1369 193 : IsTransient = 0;
1370 193 : st->TransientHangOver = 0;
1371 : }
1372 :
1373 27813 : if ( IsTransient == 1 )
1374 : {
1375 122 : mode = IsTransient;
1376 122 : push_indice( hBstr, IND_SWB_CLASS, mode, 2 );
1377 :
1378 : /* Energy for the different bands and global energies */
1379 122 : global_gain = 0;
1380 610 : for ( n_band = 0; n_band < SWB_FENV_TRANS; n_band++ )
1381 : {
1382 488 : energy = EPSILON;
1383 39528 : 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 39040 : energy += yos[n_coeff] * yos[n_coeff];
1386 : }
1387 488 : global_gain += energy;
1388 488 : SWB_fenv[n_band] = energy;
1389 : }
1390 122 : global_gain *= 0.5f;
1391 :
1392 610 : for ( n_band = 0; n_band < SWB_FENV_TRANS; n_band++ )
1393 : {
1394 488 : 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 122 : WB_tenv_orig = EPSILON;
1398 122 : WB_tenv_syn = EPSILON;
1399 610 : for ( n_band = 0; n_band < SWB_TENV; n_band++ )
1400 : {
1401 488 : SWB_tenv[n_band] = EPSILON;
1402 :
1403 39528 : for ( i = 0; i < L_SUBFR16k; i++ )
1404 : {
1405 39040 : SWB_tenv[n_band] += insig_hp[i + n_band * L_SUBFR16k] * insig_hp[i + n_band * L_SUBFR16k];
1406 : }
1407 :
1408 31784 : for ( i = 0; i < L; i++ )
1409 : {
1410 31296 : WB_tenv_syn += synth[i + n_band * L] * synth[i + n_band * L];
1411 31296 : WB_tenv_orig += insig_lp[i + n_band * L] * insig_lp[i + n_band * L];
1412 : }
1413 :
1414 488 : SWB_tenv[n_band] = (float) ( sqrt( SWB_tenv[n_band] * INV_L_SUBFR16k ) );
1415 : }
1416 :
1417 122 : Rat_tenv = (float) sqrt( WB_tenv_syn / WB_tenv_orig );
1418 :
1419 122 : if ( Rat_tenv < 0.5 )
1420 : {
1421 10 : Rat_tenv *= 1.2f;
1422 : }
1423 112 : else if ( Rat_tenv > 1 )
1424 : {
1425 88 : Rat_tenv = 1.0f;
1426 : }
1427 :
1428 610 : for ( n_band = 0; n_band < SWB_TENV; n_band++ )
1429 : {
1430 488 : SWB_tenv[n_band] *= Rat_tenv;
1431 : }
1432 :
1433 122 : max_val = SWB_tenv[0];
1434 122 : pos = 0;
1435 488 : for ( n_band = 1; n_band < SWB_TENV; n_band++ )
1436 : {
1437 366 : if ( SWB_tenv[n_band] > max_val )
1438 : {
1439 171 : max_val = SWB_tenv[n_band];
1440 171 : pos = n_band;
1441 : }
1442 : }
1443 :
1444 442 : for ( n_band = 1; n_band < SWB_TENV; n_band++ )
1445 : {
1446 348 : if ( SWB_tenv[n_band] > 5.0f * SWB_tenv[n_band - 1] )
1447 : {
1448 28 : break;
1449 : }
1450 : }
1451 :
1452 122 : if ( n_band < SWB_TENV )
1453 : {
1454 28 : energy = 0.0f;
1455 41 : for ( n_band = ( pos + 1 ); n_band < SWB_TENV; n_band++ )
1456 : {
1457 13 : energy += SWB_tenv[n_band];
1458 : }
1459 28 : if ( pos == SWB_TENV - 1 )
1460 : {
1461 17 : energy = 0.0f;
1462 : }
1463 : else
1464 : {
1465 11 : energy /= ( SWB_TENV - pos - 1 );
1466 : }
1467 :
1468 99 : for ( n_band = 0; n_band < pos; n_band++ )
1469 : {
1470 71 : SWB_tenv[n_band] *= 0.5f;
1471 : }
1472 :
1473 28 : SWB_tenv[pos] *= 1.005f;
1474 28 : if ( energy < SWB_tenv[pos] )
1475 : {
1476 41 : for ( n_band = pos + 1; n_band < SWB_TENV; n_band++ )
1477 : {
1478 13 : SWB_tenv[n_band] *= 0.9f;
1479 : }
1480 : }
1481 : }
1482 : else
1483 : {
1484 376 : for ( n_band = 1; n_band < SWB_TENV; n_band++ )
1485 : {
1486 282 : if ( SWB_tenv[n_band - 1] > SWB_tenv[n_band] )
1487 : {
1488 101 : SWB_tenv[n_band - 1] = 0.5f * ( SWB_tenv[n_band - 1] + SWB_tenv[n_band] );
1489 : }
1490 : else
1491 : {
1492 181 : SWB_tenv[n_band] = 0.5f * ( SWB_tenv[n_band - 1] + SWB_tenv[n_band] );
1493 : }
1494 : }
1495 :
1496 470 : for ( n_band = 0; n_band < SWB_TENV; n_band++ )
1497 : {
1498 376 : SWB_tenv[n_band] *= 0.9f;
1499 : }
1500 : }
1501 :
1502 122 : if ( IsTransient_LF == 0 && st->coder_type == INACTIVE && st->TransientHangOver == 1 )
1503 : {
1504 5 : for ( n_band = 0; n_band < SWB_TENV; n_band++ )
1505 : {
1506 4 : SWB_tenv[n_band] *= 0.5f;
1507 : }
1508 5 : for ( n_band = 0; n_band < SWB_FENV_TRANS; n_band++ )
1509 : {
1510 4 : SWB_fenv[n_band] *= 0.05f;
1511 : }
1512 : }
1513 : else
1514 : {
1515 121 : SWB_fenv[2] *= 0.1f;
1516 121 : SWB_fenv[3] *= 0.05f;
1517 : }
1518 :
1519 610 : for ( n_band = 0; n_band < SWB_TENV; n_band++ )
1520 : {
1521 488 : SWB_tenv_tmp[n_band] = (float) log10( SWB_tenv[n_band] + EPSILON ) * FAC_LOG2;
1522 488 : if ( SWB_tenv_tmp[n_band] > 15 )
1523 : {
1524 0 : index = 15;
1525 : }
1526 488 : else if ( SWB_tenv_tmp[n_band] < 0 )
1527 : {
1528 5 : index = 0;
1529 : }
1530 : else
1531 : {
1532 483 : index = (int16_t) ( SWB_tenv_tmp[n_band] + 0.5f );
1533 : }
1534 :
1535 488 : push_indice( hBstr, IND_SWB_TENV, index, 4 );
1536 : }
1537 :
1538 122 : MSVQ_Interpol_Tran( SWB_fenv, indice );
1539 :
1540 122 : push_indice( hBstr, IND_SWB_FENV, indice[0], 7 );
1541 122 : push_indice( hBstr, IND_SWB_FENV, indice[1], 6 );
1542 : }
1543 : else
1544 : {
1545 : /* Energy for the different bands and global energies */
1546 27691 : global_gain = 0;
1547 415365 : for ( n_band = 0; n_band < SWB_FENV; n_band++ )
1548 : {
1549 387674 : energy = EPSILON;
1550 9248794 : for ( n_coeff = swb_bwe_subband[n_band] + st_offset; n_coeff < swb_bwe_subband[n_band + 1] + st_offset; n_coeff++ )
1551 : {
1552 8861120 : energy += yos[n_coeff] * yos[n_coeff];
1553 : }
1554 :
1555 387674 : if ( n_band < SWB_FENV - 2 )
1556 : {
1557 332292 : global_gain += energy;
1558 : }
1559 387674 : SWB_fenv[n_band] = energy;
1560 : }
1561 :
1562 27691 : global_gain *= 0.5f;
1563 :
1564 27691 : mode = FD_BWE_class( yos, global_gain, tilt_nb, st );
1565 :
1566 27691 : push_indice( hBstr, IND_SWB_CLASS, mode, 2 );
1567 :
1568 27691 : energy_control( st, ACELP_CORE, mode, -1, yos, st_offset, energy_factor );
1569 :
1570 415365 : for ( n_band = 0; n_band < SWB_FENV; n_band++ )
1571 : {
1572 387674 : SWB_fenv[n_band] *= energy_factor[n_band];
1573 387674 : SWB_fenv[n_band] = 10.0f * (float) log10( SWB_fenv[n_band] * swb_inv_bwe_subband_width[n_band] );
1574 : }
1575 :
1576 27691 : freq_weights( SWB_fenv, w_NOR, w_env, SWB_FENV );
1577 :
1578 415365 : for ( n_band = 0; n_band < SWB_FENV; n_band++ )
1579 : {
1580 387674 : SWB_fenv[n_band] -= Mean_env[n_band];
1581 : }
1582 :
1583 : /* Energy VQ */
1584 27691 : msvq_interpol( SWB_fenv, w_env, indice );
1585 :
1586 27691 : push_indice( hBstr, IND_SWB_FENV, indice[0], 5 );
1587 27691 : push_indice( hBstr, IND_SWB_FENV, indice[1], 7 );
1588 27691 : push_indice( hBstr, IND_SWB_FENV, indice[2], 6 );
1589 27691 : push_indice( hBstr, IND_SWB_FENV, indice[3], 5 );
1590 27691 : push_indice( hBstr, IND_SWB_FENV, indice[4], 6 );
1591 : }
1592 :
1593 27813 : st->hBWE_FD->prev_mode = mode;
1594 27813 : st->hBWE_FD->prev_global_gain = global_gain;
1595 :
1596 27813 : return mode;
1597 : }
1598 :
1599 :
1600 : /*-------------------------------------------------------------------*
1601 : * decision_hq_generic_class()
1602 : *
1603 : *-------------------------------------------------------------------*/
1604 :
1605 4597 : 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 4597 : if ( hq_generic_offset == HQ_GENERIC_FOFFSET_24K4 )
1617 : {
1618 4597 : nband = 10;
1619 : }
1620 : else
1621 : {
1622 0 : nband = 8;
1623 : }
1624 :
1625 4597 : avgp2a = 0.f;
1626 50567 : for ( k = 0; k < nband; k++ )
1627 : {
1628 45970 : a = 0.0f;
1629 45970 : p = 0.0f;
1630 1002146 : for ( i = swb_bwe_subband[k] + hq_generic_offset; i < swb_bwe_subband[k + 1] + hq_generic_offset; i++ )
1631 : {
1632 956176 : e = coefs[i] * coefs[i];
1633 :
1634 956176 : if ( e > p )
1635 : {
1636 164139 : p = e;
1637 : }
1638 :
1639 956176 : a += e;
1640 : }
1641 :
1642 45970 : if ( a > 0.0f )
1643 : {
1644 45970 : a *= swb_inv_bwe_subband_width[k];
1645 45970 : p2a = 10.0f * (float) log10( p / a );
1646 45970 : avgp2a += p2a;
1647 : }
1648 : }
1649 :
1650 4597 : avgp2a /= (float) ( nband );
1651 :
1652 4597 : if ( avgp2a > 8.6f )
1653 : {
1654 15 : return HQ_GENERIC_EXC1;
1655 : }
1656 : else
1657 : {
1658 4582 : return HQ_GENERIC_EXC0;
1659 : }
1660 : }
1661 :
1662 : /*-------------------------------------------------------------------*
1663 : * hq_generic_hf_encoding()
1664 : *
1665 : *-------------------------------------------------------------------*/
1666 :
1667 4886 : 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 4886 : BSTR_ENC_HANDLE hBstr = st->hBstr;
1682 :
1683 4886 : if ( hq_generic_offset <= HQ_GENERIC_FOFFSET_24K4 )
1684 : {
1685 4886 : nenv = SWB_FENV;
1686 : }
1687 : else
1688 : {
1689 0 : nenv = SWB_FENV - 2;
1690 : }
1691 :
1692 73290 : for ( n_band = 0; n_band < nenv; n_band++ )
1693 : {
1694 68404 : energy = EPSILON;
1695 1631924 : 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 1563520 : energy += coefs[n_coeff] * coefs[n_coeff];
1698 : }
1699 :
1700 68404 : hq_generic_fenv[n_band] = energy;
1701 : }
1702 :
1703 4886 : if ( length == L_SPEC48k )
1704 : {
1705 15980 : for ( n_band = 0; n_band < DIM_FB; n_band++ )
1706 : {
1707 11985 : energy = EPSILON;
1708 651185 : for ( n_coeff = fb_bwe_subband[n_band]; n_coeff < fb_bwe_subband[n_band + 1]; n_coeff++ )
1709 : {
1710 639200 : energy += coefs[n_coeff] * coefs[n_coeff];
1711 : }
1712 :
1713 11985 : hq_generic_fenv[n_band + nenv] = energy;
1714 : }
1715 : }
1716 :
1717 4886 : energy_control( st, HQ_CORE, -1, -1, coefs, hq_generic_offset, energy_factor );
1718 :
1719 4886 : if ( st->hHQ_core->hq_generic_speech_class == 1 )
1720 : {
1721 289 : push_indice( hBstr, IND_HQ_SWB_EXC_SP_CLAS, 1, 1 );
1722 289 : *hq_generic_exc_clas = HQ_GENERIC_SP_EXC;
1723 : }
1724 : else
1725 : {
1726 4597 : *hq_generic_exc_clas = decision_hq_generic_class( coefs, hq_generic_offset );
1727 4597 : push_indice( hBstr, IND_HQ_SWB_EXC_SP_CLAS, 0, 1 );
1728 4597 : push_indice( hBstr, IND_HQ_SWB_EXC_CLAS, *hq_generic_exc_clas, 1 );
1729 : }
1730 :
1731 73290 : for ( n_band = 0; n_band < nenv; n_band++ )
1732 : {
1733 68404 : hq_generic_fenv[n_band] *= energy_factor[n_band];
1734 68404 : hq_generic_fenv[n_band] = 10.0f * (float) log10( hq_generic_fenv[n_band] * swb_inv_bwe_subband_width[n_band] );
1735 : }
1736 :
1737 4886 : if ( length == L_SPEC48k )
1738 : {
1739 15980 : for ( n_band = 0; n_band < DIM_FB; n_band++ )
1740 : {
1741 11985 : 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 4886 : freq_weights( hq_generic_fenv, w_NOR, w_env, nenv );
1746 :
1747 73290 : for ( n_band = 0; n_band < nenv; n_band++ )
1748 : {
1749 68404 : hq_generic_fenv[n_band] -= Mean_env[n_band];
1750 : }
1751 :
1752 4886 : if ( length == L_SPEC48k )
1753 : {
1754 15980 : for ( n_band = 0; n_band < DIM_FB; n_band++ )
1755 : {
1756 11985 : hq_generic_fenv[n_band + nenv] -= Mean_env_fb[n_band];
1757 : }
1758 : }
1759 :
1760 : /* Energy VQ */
1761 4886 : if ( hq_generic_offset <= HQ_GENERIC_FOFFSET_24K4 )
1762 : {
1763 4886 : msvq_interpol( hq_generic_fenv, w_env, indice );
1764 : }
1765 : else
1766 : {
1767 0 : msvq_interpol_2( hq_generic_fenv, w_env, indice, nenv );
1768 : }
1769 :
1770 4886 : if ( length == L_SPEC48k )
1771 : {
1772 3995 : indice[5] = vqSimple_w( hq_generic_fenv + nenv, hq_generic_fenv + nenv, EnvCdbkFB, NULL, DIM_FB, N_CB_FB, 0 );
1773 : }
1774 :
1775 4886 : push_indice( hBstr, IND_SWB_FENV_HQ, indice[0], 5 );
1776 4886 : push_indice( hBstr, IND_SWB_FENV_HQ, indice[1], 7 );
1777 4886 : push_indice( hBstr, IND_SWB_FENV_HQ, indice[2], 6 );
1778 4886 : push_indice( hBstr, IND_SWB_FENV_HQ, indice[3], 5 );
1779 :
1780 4886 : if ( hq_generic_offset <= HQ_GENERIC_FOFFSET_24K4 )
1781 : {
1782 4886 : push_indice( hBstr, IND_SWB_FENV_HQ, indice[4], 6 );
1783 : }
1784 : else
1785 : {
1786 0 : push_indice( hBstr, IND_SWB_FENV_HQ, indice[4], 5 );
1787 : }
1788 :
1789 4886 : if ( length == L_SPEC48k )
1790 : {
1791 3995 : push_indice( hBstr, IND_FB_FENV_HQ, indice[5], 5 );
1792 : }
1793 :
1794 73290 : 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 68404 : Overflow = 0;
1802 : #endif
1803 68404 : tmp = add( (int16_t) ( hq_generic_fenv[n_band] * 256 ), (int16_t) ( Mean_env[n_band] * 256 ) ); /*Q8 */
1804 :
1805 68404 : L_tmp = L_mult( tmp, 21771 ); /* 0.166096 in Q17 -> Q26 */
1806 68404 : L_tmp = L_shr( L_tmp, 10 ); /* From Q26 to Q16 */
1807 68404 : frac = L_Extract_lc( L_tmp, &exp ); /* Extract exponent of L_tmp */
1808 :
1809 68404 : tmp = extract_l( Pow2( 13, frac ) ); /* Put 13 as exponent so that */
1810 : /* output of Pow2() will be: */
1811 : /* 16384 < Pow2() <= 32767 */
1812 68404 : exp = sub( exp, 13 );
1813 : #ifdef BASOP_NOGLOB
1814 68404 : tmp = shl_o( tmp, add( exp, 1 ), &Overflow ); /*Q1 */
1815 : #else
1816 : tmp = shl( tmp, add( exp, 1 ) ); /*Q1 */
1817 : #endif
1818 68404 : hq_generic_fenv[n_band] = (float) tmp * 0.5f; /*Q1 */
1819 : }
1820 :
1821 4886 : if ( length == L_SPEC48k )
1822 : {
1823 15980 : for ( n_band = 0; n_band < DIM_FB; n_band++ )
1824 : {
1825 : Word16 tmp, frac, exp;
1826 : Word32 L_tmp;
1827 :
1828 11985 : tmp = add( (int16_t) ( hq_generic_fenv[n_band + nenv] * 128 ), (int16_t) ( Mean_env_fb[n_band] * 128 ) ); /*Q7 */
1829 11985 : L_tmp = L_mult( tmp, 21771 ); /* 0.166096 in Q17 -> Q25 */
1830 11985 : L_tmp = L_shr( L_tmp, 9 ); /* From Q25 to Q16 */
1831 11985 : frac = L_Extract_lc( L_tmp, &exp ); /* Extract exponent of L_tmp */
1832 :
1833 11985 : tmp = extract_l( Pow2( 13, frac ) ); /* Put 13 as exponent so that */
1834 : /* output of Pow2() will be: */
1835 : /* 16384 < Pow2() <= 32767 */
1836 11985 : exp = sub( exp, 13 );
1837 11985 : tmp = shl( tmp, add( exp, 1 ) ); /*Q1 */
1838 11985 : hq_generic_fenv[add( n_band, nenv )] = (float) tmp * 0.5f;
1839 : }
1840 : }
1841 :
1842 4886 : return;
1843 : }
1844 :
1845 :
1846 : /*-------------------------------------------------------------------*
1847 : * fd_bwe_enc_init()
1848 : *
1849 : * Initialize FD BWE state structure at the encoder
1850 : *-------------------------------------------------------------------*/
1851 :
1852 8460 : void fd_bwe_enc_init(
1853 : FD_BWE_ENC_HANDLE hBWE_FD /* i/o: FD BWE data handle */
1854 : )
1855 : {
1856 8460 : 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 8460 : set_f( hBWE_FD->old_input, 0, NS2SA( 48000, DELAY_FD_BWE_ENC_12k8_NS + DELAY_FIR_RESAMPL_NS ) );
1858 8460 : set_f( hBWE_FD->old_input_wb, 0, NS2SA( 16000, DELAY_FD_BWE_ENC_12k8_NS ) );
1859 8460 : set_f( hBWE_FD->old_input_lp, 0, NS2SA( 16000, ACELP_LOOK_NS + DELAY_FD_BWE_ENC_16k_NS ) );
1860 8460 : set_f( hBWE_FD->old_syn_12k8_16k, 0, NS2SA( 16000, DELAY_FD_BWE_ENC_NS ) );
1861 :
1862 8460 : hBWE_FD->prev_mode = NORMAL;
1863 8460 : set_f( hBWE_FD->old_wtda_swb, 0, L_FRAME48k );
1864 8460 : hBWE_FD->prev_L_swb_norm1 = 8;
1865 8460 : hBWE_FD->prev_global_gain = 0.0f;
1866 8460 : hBWE_FD->modeCount = 0;
1867 8460 : hBWE_FD->EnergyLF = 0.0f;
1868 8460 : hBWE_FD->mem_old_wtda_swb = 0.0f;
1869 :
1870 8460 : set_f( hBWE_FD->old_fdbwe_speech, 0.0f, L_FRAME48k );
1871 8460 : hBWE_FD->mem_deemph_old_syn = 0.0f;
1872 :
1873 8460 : return;
1874 : }
|