Line data Source code
1 : /*====================================================================================
2 : EVS Codec 3GPP TS26.452 Aug 12, 2021. Version 16.3.0
3 : ====================================================================================*/
4 :
5 : #include <stdint.h>
6 : #include "options.h"
7 : #include "cnst.h"
8 : #include "rom_com_fx.h"
9 : #include "rom_com.h" /* Common constants */
10 : #include "prot_fx.h" /* Function prototypes */
11 : #include "prot_fx_enc.h" /* Function prototypes */
12 : #include "basop_util.h" /* Function prototypes */
13 :
14 : /*-------------------------------------------------------------------*
15 : * pre_proc()
16 : *
17 : * Pre-processing (spectral analysis, LP analysis, VAD,
18 : * OL pitch calculation, coder mode selection, ...)
19 : *--------------------------------------------------------------------*/
20 :
21 3100 : void pre_proc_fx(
22 : Encoder_State *st, /* i/o: encoder state structure */
23 : const Word16 input_frame, /* i : frame length */
24 : Word16 old_inp_12k8[], /* i/o: buffer of old input signal */
25 : Word16 old_inp_16k[], /* i/o: buffer of old input signal @ 16kHz */
26 : Word16 **inp, /* o : ptr. to inp. signal in the current frame */
27 : Word32 fr_bands[2 * NB_BANDS], /* o : energy in frequency bands */
28 : Word16 *Etot, /* o : total energy */
29 : Word32 *ener, /* o : residual energy from Levinson-Durbin */
30 : #ifndef FIX_I4_OL_PITCH
31 : Word16 pitch_orig[3], /* o : open-loop pitch values for quantization */
32 : #endif
33 : Word16 A[NB_SUBFR16k * ( M + 1 )], /* o : A(z) unquantized for the 4 subframes */
34 : Word16 Aw[NB_SUBFR16k * ( M + 1 )], /* o : weighted A(z) unquantized for subframes */
35 : Word16 epsP_h[M + 1], /* o : LP prediction errors */
36 : Word16 epsP_l[M + 1], /* o : LP prediction errors */
37 : Word32 epsP[M + 1], /* o : LP prediction errors */
38 : Word16 lsp_new[M], /* o : LSPs at the end of the frame */
39 : Word16 lsp_mid[M], /* o : LSPs in the middle of the frame */
40 : Word16 *vad_hover_flag,
41 : Word16 *attack_flag, /* o : flag signalling attack encoded by AC mode (GSC) */
42 : Word16 *new_inp_resamp16k, /* o : new input signal @16kHz, non pre-emphasised, used by the WB TBE/BWE */
43 : Word16 *Voicing_flag, /* o : voicing flag for HQ FEC */
44 :
45 : Word32 realBuffer[CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX], /* i/o : cldfb real buffer */
46 : Word32 imagBuffer[CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX], /* i/o : cldfb imag buffer */
47 : CLDFB_SCALE_FACTOR *cldfbScale, /* o : cldfb scale */
48 : Word16 *old_exc, /* i/o: static excitation memory */
49 : Word16 *hq_core_type, /* o : HQ core type */
50 : Word16 *Q_new,
51 : Word16 *shift,
52 : Word16 *Q_r )
53 : {
54 : Word16 delay;
55 : const Word16 *signal_in;
56 :
57 : Word16 i;
58 : Word16 *inp_12k8, *new_inp_12k8, *inp_16k, *new_inp_16k; /* pointers to current frame and new data */
59 : Word16 old_wsp[L_WSP], *wsp; /* weighted input signal buffer */
60 : Word16 pitch_fr[NB_SUBFR]; /* fractional pitch values */
61 : Word16 voicing_fr[NB_SUBFR]; /* fractional pitch gains */
62 : Word32 lf_E[2 * VOIC_BINS]; /* per bin spectrum energy in lf */
63 : Word32 tmpN[NB_BANDS]; /* Temporary noise update */
64 : Word32 tmpE[NB_BANDS]; /* Temporary averaged energy of 2 sf. */
65 : Word32 ee[2]; /* Spectral tilt */
66 : Word16 corr_shift; /* correlation shift */
67 : Word16 relE; /* frame relative energy */
68 : Word16 loc_harm; /* harmonicity flag */
69 : Word16 cor_map_sum, sp_div; /* speech/music clasif. parameters */
70 : Word32 PS[128];
71 : Word16 L_look; /* length of look-ahead */
72 :
73 : Word16 Q_sp_div, Q_esp;
74 : Word16 localVAD_HE_SAD; /* HE SAD parameters */
75 : Word16 snr_sum_he; /* HE SAD parameters */
76 :
77 : Word16 vad_flag_cldfb;
78 :
79 : Word16 vad_flag_dtx;
80 : Word16 old_cor;
81 : Word32 hp_E[2]; /* Energy in HF */
82 : Word16 noisy_speech_HO, clean_speech_HO, NB_speech_HO; /* SC-VBR HO flags */
83 : Word16 non_staX; /* unbound non-stationarity for sp/mus clas. */
84 : Word32 sr_core_tmp;
85 : Word16 L_frame_tmp;
86 : Word16 Q_exp, Q_wsp_exp, Q_new_16k;
87 : Word16 shift_exp;
88 : Word16 Scale_fac[2];
89 : Word32 Le_min_scaled;
90 : Word16 excitation_max_test;
91 : Word16 lsf_new[M], stab_fac;
92 : Word32 enerBuffer[CLDFB_NO_CHANNELS_MAX];
93 : Word16 enerBuffer_exp; /*[CLDFB_NO_CHANNELS_MAX];*/
94 : Word16 realBuffer16[CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX];
95 : Word16 imagBuffer16[CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX];
96 : Word16 tmp_e;
97 : Word16 currFlatness;
98 : Word16 high_lpn_flag;
99 : Word16 alw_pitch_lag_12k8[2];
100 : Word16 alw_voicing[2];
101 : Word16 flag_spitch;
102 : Word16 sf_energySum[CLDFB_NO_CHANNELS_MAX];
103 : Word32 L_tmp;
104 : UWord16 lsb;
105 : Word16 fft_buff[2 * L_FFT];
106 : Word16 sp_floor;
107 3100 : Word16 freqTable_local[2] = { 20, 40 };
108 3100 : move16();
109 3100 : move16();
110 : Word16 last_core_orig;
111 : Word16 headroom;
112 3100 : Word16 cldfb_addition = 0;
113 3100 : move16();
114 : Word16 old_pitch1;
115 : Word16 clas_mod;
116 3100 : SC_VBR_ENC_HANDLE hSC_VBR = st->hSC_VBR;
117 3100 : GSC_ENC_HANDLE hGSCEnc = st->hGSCEnc;
118 3100 : NOISE_EST_HANDLE hNoiseEst = st->hNoiseEst;
119 3100 : VAD_HANDLE hVAD = st->hVAD;
120 3100 : TD_CNG_ENC_HANDLE hTdCngEnc = st->hTdCngEnc;
121 :
122 3100 : DTX_ENC_HANDLE hDtxEnc = st->hDtxEnc;
123 3100 : TCX_ENC_HANDLE hTcxEnc = st->hTcxEnc;
124 3100 : TD_BWE_ENC_HANDLE hBWE_TD = st->hBWE_TD;
125 3100 : RF_ENC_HANDLE hRF = st->hRF;
126 3100 : LPD_state_HANDLE hLPDmem = st->hLPDmem;
127 3100 : FD_BWE_ENC_HANDLE hBWE_FD = st->hBWE_FD;
128 :
129 : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
130 3100 : Flag Overflow = 0;
131 3100 : move16();
132 : #endif
133 :
134 : /*------------------------------------------------------------------*
135 : * Initializations
136 : *------------------------------------------------------------------*/
137 3100 : signal_in = st->input_fx;
138 :
139 3100 : vad_flag_dtx = 0;
140 3100 : move16();
141 3100 : localVAD_HE_SAD = 0;
142 3100 : move16();
143 3100 : NB_speech_HO = 0;
144 3100 : move16();
145 3100 : clean_speech_HO = 0;
146 3100 : move16();
147 3100 : noisy_speech_HO = 0;
148 3100 : move16();
149 3100 : snr_sum_he = 0;
150 3100 : move16();
151 3100 : currFlatness = 0;
152 3100 : move16();
153 :
154 3100 : Q_new_16k = 0;
155 3100 : move16();
156 :
157 3100 : *vad_hover_flag = 0;
158 3100 : move16();
159 3100 : st->sp_aud_decision1 = 0;
160 3100 : move16();
161 3100 : st->sp_aud_decision2 = 0;
162 3100 : move16();
163 3100 : st->coder_type = GENERIC;
164 3100 : move16();
165 3100 : hGSCEnc->noise_lev = NOISE_LEVEL_SP0;
166 3100 : move16();
167 3100 : *attack_flag = 0;
168 3100 : move16();
169 :
170 3100 : IF( hSC_VBR != NULL )
171 : {
172 3100 : hSC_VBR->bump_up = 0;
173 3100 : move16();
174 3100 : hSC_VBR->ppp_mode = 0;
175 3100 : move16();
176 3100 : hSC_VBR->nelp_mode = 0;
177 3100 : move16();
178 3100 : hSC_VBR->avoid_HQ_VBR_NB = 0;
179 3100 : move16();
180 : }
181 3100 : L_look = L_LOOK_12k8;
182 3100 : move16(); /* lookahead at 12.8kHz */
183 :
184 3100 : new_inp_12k8 = old_inp_12k8 + L_INP_MEM; /* pointer to new samples of the input signal in 12.8kHz core */
185 3100 : inp_12k8 = new_inp_12k8 - L_look; /* pointer to the current frame of input signal in 12.8kHz core */
186 3100 : Copy( st->old_inp_12k8_fx, old_inp_12k8, L_INP_MEM );
187 :
188 3100 : Copy( st->old_wsp_fx, old_wsp, L_WSP_MEM );
189 3100 : wsp = old_wsp + L_WSP_MEM; /* pointer to the current frame of weighted signal in 12.8kHz core */
190 :
191 3100 : old_cor = st->old_corr_fx;
192 3100 : move16(); /* save old_cor for speech/music classifier */
193 :
194 3100 : st->rf_mode = st->Opt_RF_ON;
195 3100 : move16();
196 :
197 3100 : last_core_orig = st->last_core;
198 3100 : move16();
199 :
200 : /*--------------------------------------------------------------*
201 : * Cldfb analysis
202 : *---------------------------------------------------------------*/
203 :
204 3100 : move32();
205 3100 : move16();
206 3100 : st->prevEnergyHF_fx = st->currEnergyHF_fx;
207 3100 : tmp_e = st->currEnergyHF_e_fx;
208 :
209 3100 : analysisCldfbEncoder_fx( st, signal_in, realBuffer, imagBuffer, realBuffer16, imagBuffer16, enerBuffer, &enerBuffer_exp, cldfbScale );
210 3100 : cldfbScale->hb_scale = cldfbScale->lb_scale;
211 3100 : move16();
212 :
213 : /*----------------------------------------------------------------*
214 : * Change the sampling frequency to 12.8 kHz
215 : *----------------------------------------------------------------*/
216 3100 : modify_Fs_fx( signal_in, input_frame, st->input_Fs, new_inp_12k8, INT_FS_12k8, st->mem_decim_fx, ( const Word16 )( EQ_16( st->max_bwidth, NB ) ) );
217 3100 : Copy( new_inp_12k8, st->buf_speech_enc + L_FRAME32k, L_FRAME );
218 3100 : Scale_sig( st->buf_speech_enc + L_FRAME32k, L_FRAME, 1 );
219 : /*------------------------------------------------------------------*
220 : * Perform fixed preemphasis (12.8 kHz signal) through 1 - g*z^-1
221 : *-----------------------------------------------------------------*/
222 :
223 : /* rf_mode: first time Q_new is computed here inside Preemph_scaled() for primary copy
224 : these are the same memories used in partial frame assembly as well */
225 :
226 3100 : headroom = 1;
227 3100 : move16();
228 3100 : test();
229 3100 : test();
230 : /* reserve an extra bit of headroom in case of NB coding and if there is a chance of energy above 4 kHz */
231 : /* st->bwidth refers to the coded bandwidth of the previous frame */
232 3100 : if ( ( ( st->bwidth == NB ) || ( st->max_bwidth == NB ) ) && ( GT_32( st->input_Fs, 8000 ) ) )
233 : {
234 0 : headroom = add( headroom, 1 );
235 : }
236 3100 : Preemph_scaled( new_inp_12k8, Q_new, &st->mem_preemph_fx, st->Q_max,
237 3100 : PREEMPH_FAC, 0, headroom, L_Q_MEM, L_FRAME, st->last_coder_type, 1 );
238 :
239 3100 : Q_exp = sub( *Q_new, st->Q_old );
240 3100 : st->prev_Q_old = st->Q_old;
241 3100 : move16();
242 3100 : st->Q_old = *Q_new;
243 3100 : move16();
244 :
245 : /*------------------------------------------------------------------*
246 : * Scaling of memories
247 : *-----------------------------------------------------------------*/
248 :
249 3100 : Le_min_scaled = Scale_mem_pre_proc( st->ini_frame, Q_exp, Q_new, old_inp_12k8, &( st->mem_wsp_fx ), hNoiseEst->enrO_fx, hNoiseEst->bckr_fx,
250 3100 : hNoiseEst->ave_enr_fx, hNoiseEst->ave_enr2_fx, hNoiseEst->fr_bands1_fx, hNoiseEst->fr_bands2_fx, st->Bin_E_old_fx );
251 :
252 : /*-------------------------------------------------------------------------*
253 : * Spectral analysis
254 : *--------------------------------------------------------------------------*/
255 :
256 3100 : analy_sp_fx( -1, inp_12k8, *Q_new, fr_bands, lf_E, Etot, st->min_band, st->max_band, Le_min_scaled, Scale_fac, st->Bin_E_fx, st->Bin_E_old_fx,
257 3100 : PS, st->lgBin_E_fx, st->band_energies, fft_buff );
258 :
259 3100 : st->band_energies_exp = sub( sub( WORD32_BITS - 1, *Q_new ), QSCALE );
260 3100 : move16();
261 :
262 : /*----------------------------------------------------------------*
263 : * SAD (1-signal, 0-noise)
264 : *----------------------------------------------------------------*/
265 :
266 3100 : noise_est_pre_fx( *Etot, st->ini_frame, hNoiseEst, 0, EVS_MONO, EVS_MONO );
267 :
268 6200 : st->vad_flag = wb_vad_fx( st, fr_bands, &noisy_speech_HO, &clean_speech_HO, &NB_speech_HO,
269 3100 : &snr_sum_he, &localVAD_HE_SAD, &( st->flag_noisy_speech_snr ), *Q_new, hVAD, hNoiseEst, st->lp_speech_fx, st->lp_noise_fx );
270 :
271 3100 : vad_flag_cldfb = vad_proc_fx( &( st->vad_st ), realBuffer, imagBuffer, cldfbScale->lb_scale, &cldfb_addition,
272 3100 : enerBuffer, enerBuffer_exp, st->cldfbAnaEnc->no_channels, st->vad_flag );
273 :
274 3100 : IF( LT_16( st->Pos_relE_cnt, 20 ) ) /* Ensure the level is high enough and cldfb decision is reliable */
275 : {
276 : /* Combine decisions from SADS */
277 2344 : test();
278 2344 : if ( EQ_16( st->vad_flag, 1 ) && vad_flag_cldfb == 0 )
279 : {
280 0 : st->localVAD = 0;
281 0 : move16();
282 : }
283 :
284 2344 : st->vad_flag = vad_flag_cldfb;
285 2344 : move16();
286 : }
287 :
288 : /* apply DTX hangover for CNG analysis */
289 3100 : vad_flag_dtx = dtx_hangover_addition_fx( st, st->vad_flag, sub( st->lp_speech_fx, st->lp_noise_fx ), cldfb_addition, vad_hover_flag, hVAD, hNoiseEst );
290 :
291 : /*----------------------------------------------------------------*
292 : * NB/WB/SWB/FB bandwidth detector
293 : *----------------------------------------------------------------*/
294 :
295 189100 : FOR( i = 0; i < CLDFB_NO_CHANNELS_MAX; i++ )
296 : {
297 186000 : sf_energySum[i] = enerBuffer_exp;
298 186000 : move16();
299 : }
300 :
301 3100 : bw_detect_fx( st, signal_in, NULL, enerBuffer, sf_energySum, MONO_FORMAT, 0, 0 );
302 :
303 : /*----------------------------------------------------------------*
304 : * Noise energy down-ward update and total noise energy estimation
305 : * Long-term energies and relative frame energy updates
306 : * Correlation correction as a function of total noise level
307 : *----------------------------------------------------------------*/
308 :
309 3100 : noise_est_down_fx( fr_bands, hNoiseEst->bckr_fx, tmpN, tmpE, st->min_band, st->max_band, &hNoiseEst->totalNoise_fx,
310 3100 : *Etot, &hNoiseEst->Etot_last_fx, &hNoiseEst->Etot_v_h2_fx, *Q_new, Le_min_scaled );
311 :
312 3100 : relE = sub( *Etot, st->lp_speech_fx ); /* Q8 */ /* relE = *Etot - st->lp_speech;*/
313 :
314 3100 : test();
315 3100 : IF( GT_16( relE, 384 ) ) /*relE > 1.5 */
316 : {
317 978 : st->Pos_relE_cnt = 0;
318 978 : move16();
319 : }
320 2122 : ELSE IF( relE < 0 && EQ_16( st->vad_flag, 1 ) )
321 : {
322 1906 : st->Pos_relE_cnt = add( st->Pos_relE_cnt, 1 );
323 : }
324 :
325 3100 : corr_shift = correlation_shift_fx( hNoiseEst->totalNoise_fx );
326 :
327 : /*----------------------------------------------------------------*
328 : * FD-CNG Noise Estimator
329 : *----------------------------------------------------------------*/
330 :
331 3100 : resetFdCngEnc_fx( st );
332 3100 : perform_noise_estimation_enc_fx( st->band_energies, st->band_energies_exp, enerBuffer, enerBuffer_exp, st->hFdCngEnc );
333 :
334 : /*-----------------------------------------------------------------*
335 : * Select SID or FRAME_NO_DATA frame if DTX enabled
336 : *-----------------------------------------------------------------*/
337 :
338 3100 : dtx_fx( st, vad_flag_dtx, inp_12k8, *Q_new );
339 :
340 : /*----------------------------------------------------------------*
341 : * Adjust FD-CNG Noise Estimator
342 : *----------------------------------------------------------------*/
343 3100 : test();
344 3100 : IF( ( NE_32( st->last_total_brate, st->total_brate ) ) || ( NE_16( st->last_bwidth, st->bwidth ) ) )
345 : {
346 0 : L_tmp = st->total_brate;
347 0 : move32();
348 0 : test();
349 0 : if ( st->rf_mode && EQ_32( st->total_brate, ACELP_13k20 ) )
350 : {
351 0 : L_tmp = ACELP_9k60;
352 0 : move32();
353 : }
354 0 : configureFdCngEnc_fx( st->hFdCngEnc, st->bwidth, L_tmp );
355 : }
356 3100 : test();
357 3100 : IF( st->hFdCngEnc != NULL && st->Opt_DTX_ON )
358 : {
359 0 : AdjustFirstSID_fx( st->hFdCngEnc->hFdCngCom->npart, st->hFdCngEnc->msPeriodog_fx, st->hFdCngEnc->msPeriodog_fx_exp, st->hFdCngEnc->energy_ho_fx,
360 0 : &st->hFdCngEnc->energy_ho_fx_exp, st->hFdCngEnc->msNoiseEst_fx, &st->hFdCngEnc->msNoiseEst_fx_exp, st->hFdCngEnc->msNoiseEst_old_fx,
361 0 : &st->hFdCngEnc->msNoiseEst_old_fx_exp, &( st->hFdCngEnc->hFdCngCom->active_frame_counter ), st );
362 : }
363 :
364 : /*----------------------------------------------------------------*
365 : * Reconfigure MODE2
366 : *----------------------------------------------------------------*/
367 :
368 3100 : IF( EQ_16( st->codec_mode, MODE2 ) )
369 : {
370 1050 : SetModeIndex_fx( st, st->last_total_brate, EVS_MONO, 0, *shift );
371 : }
372 :
373 3100 : calcLoEnvCheckCorrHiLo_Fix( st->cldfbAnaEnc->no_col, freqTable_local, st->hTECEnc->loBuffer, st->hTECEnc->loTempEnv,
374 3100 : st->hTECEnc->loTempEnv_ns, st->hTECEnc->hiTempEnv, &( st->hTECEnc->corrFlag ) );
375 :
376 : /*---------------------------------------------------------------*
377 : * Time-domain transient detector
378 : *---------------------------------------------------------------*/
379 :
380 : /* Adjust prevEnergyHF and currEnergyHF to same exponent */
381 3100 : i = sub( st->currEnergyHF_e_fx, tmp_e );
382 :
383 : /* If i > 0: currEnergyHF is higher => shr prevEnergyHF, exponent remains as is */
384 3100 : st->prevEnergyHF_fx = L_shr( st->prevEnergyHF_fx, s_max( 0, i ) );
385 3100 : move32();
386 :
387 : /* If i < 0: currEnergyHF is lower => shr currEnergyHF, exponent changes to previous */
388 3100 : st->currEnergyHF_fx = L_shl( st->currEnergyHF_fx, s_min( 0, i ) );
389 3100 : move32();
390 :
391 3100 : if ( i < 0 )
392 : {
393 2927 : st->currEnergyHF_e_fx = tmp_e;
394 2927 : move16();
395 : }
396 :
397 3100 : test();
398 3100 : IF( st->tcx10Enabled || st->tcx20Enabled )
399 : {
400 3100 : RunTransientDetection_fx( signal_in, input_frame, &st->transientDetection );
401 3100 : currFlatness = GetTCXAvgTemporalFlatnessMeasure_fx( &st->transientDetection, NSUBBLOCKS, 0 );
402 : }
403 :
404 : /*----------------------------------------------------------------*
405 : * LP analysis
406 : *----------------------------------------------------------------*/
407 :
408 3100 : alw_pitch_lag_12k8[0] = st->old_pitch_la;
409 3100 : move16();
410 3100 : alw_pitch_lag_12k8[1] = st->old_pitch_la;
411 3100 : move16();
412 3100 : alw_voicing[0] = st->voicing_fx[2];
413 3100 : move16();
414 3100 : alw_voicing[1] = st->voicing_fx[2];
415 3100 : move16();
416 :
417 3100 : analy_lp_fx( inp_12k8, L_FRAME, L_look, ener, A, epsP_h, epsP_l, lsp_new, lsp_mid, st->lsp_old1_fx, alw_pitch_lag_12k8, alw_voicing, INT_FS_12k8, EVS_MONO, 0, *Q_new, Q_r );
418 :
419 3100 : lsp2lsf_fx( lsp_new, lsf_new, M, INT_FS_12k8 );
420 3100 : stab_fac = lsf_stab_fx( lsf_new, st->lsf_old1_fx, 0, L_FRAME );
421 3100 : Copy( lsf_new, st->lsf_old1_fx, M );
422 :
423 : /*----------------------------------------------------------------*
424 : * Compute weighted input (for OL pitch analysis)
425 : * OL pitch analysis
426 : * 1/4 pitch precision improvement
427 : *----------------------------------------------------------------*/
428 :
429 3100 : find_wsp_fx( A, inp_12k8, wsp, &st->mem_wsp_fx, TILT_FAC_FX, L_FRAME, L_look, L_SUBFR, Aw, GAMMA1, NB_SUBFR );
430 :
431 3100 : Q_wsp_exp = Q_exp;
432 3100 : move16();
433 3100 : Scale_wsp( wsp, &( st->old_wsp_max ), shift, &Q_wsp_exp, &( st->old_wsp_shift ), st->old_wsp2_fx,
434 3100 : st->mem_decim2_fx, old_wsp, add( L_FRAME, L_look ) );
435 3100 : shift_exp = sub( Q_wsp_exp, Q_exp );
436 :
437 3100 : IF( st->vad_flag == 0 )
438 : {
439 : /* reset the OL pitch tracker memories during inactive frames */
440 19 : pitch_ol_init_fx( &st->old_thres_fx, &st->old_pitch, &st->delta_pit, &st->old_corr_fx );
441 : }
442 3100 : old_pitch1 = st->pitch[1];
443 :
444 3100 : pitch_ol_fx( st->pitch, st->voicing_fx, &st->old_pitch, &st->old_corr_fx, corr_shift, &st->old_thres_fx,
445 3100 : &st->delta_pit, st->old_wsp2_fx, wsp, st->mem_decim2_fx, relE, st->clas, st->bwidth, st->Opt_SC_VBR );
446 :
447 : /* Updates for adaptive lag window memory */
448 3100 : st->old_pitch_la = st->pitch[2];
449 3100 : move16();
450 3100 : st->old_voicing_la = st->voicing_fx[2];
451 3100 : move16();
452 :
453 : /* Detection of very short stable st->pitch period (MODE1 bit-rates) */
454 3100 : StableHighPitchDetect_fx( &flag_spitch, st->pitch, st->voicing_fx, wsp, st->localVAD, &st->voicing_sm_fx, &st->voicing0_sm_fx,
455 3100 : &st->LF_EnergyRatio_sm_fx, &st->predecision_flag, &st->diff_sm_fx, &st->energy_sm_fx, *Q_new, st->lgBin_E_fx );
456 :
457 : /* 1/4 pitch precision improvement */
458 3100 : IF( LE_32( st->total_brate, ACELP_24k40 ) )
459 : {
460 : /* 1/4 pitch precision improvement */
461 2100 : pitch_ol2_fx( PIT_MIN_EXTEND, st->pitch[0], &pitch_fr[0], &voicing_fr[0], 0, wsp, 7, EVS_MONO );
462 2100 : pitch_ol2_fx( PIT_MIN_EXTEND, st->pitch[0], &pitch_fr[1], &voicing_fr[1], L_SUBFR, wsp, 7, EVS_MONO );
463 2100 : pitch_ol2_fx( PIT_MIN_EXTEND, st->pitch[1], &pitch_fr[2], &voicing_fr[2], 2 * L_SUBFR, wsp, 7, EVS_MONO );
464 2100 : pitch_ol2_fx( PIT_MIN_EXTEND, st->pitch[1], &pitch_fr[3], &voicing_fr[3], 3 * L_SUBFR, wsp, 7, EVS_MONO );
465 : }
466 : ELSE
467 : {
468 1000 : pitch_fr[0] = st->pitch[0];
469 1000 : move16();
470 1000 : pitch_fr[1] = st->pitch[0];
471 1000 : move16();
472 1000 : pitch_fr[2] = st->pitch[1];
473 1000 : move16();
474 1000 : pitch_fr[3] = st->pitch[1];
475 1000 : move16();
476 :
477 1000 : voicing_fr[0] = st->voicing_fx[0];
478 1000 : move16();
479 1000 : voicing_fr[1] = st->voicing_fx[0];
480 1000 : move16();
481 1000 : voicing_fr[2] = st->voicing_fx[1];
482 1000 : move16();
483 1000 : voicing_fr[3] = st->voicing_fx[1];
484 1000 : move16();
485 : }
486 :
487 :
488 : /*------------------------------------------------------------------*
489 : * Update estimated noise energy and voicing cut-off frequency
490 : *-----------------------------------------------------------------*/
491 :
492 3100 : noise_est_fx( st, old_pitch1, tmpN, epsP_h, epsP_l, *Etot, relE, corr_shift, tmpE, fr_bands, &cor_map_sum,
493 3100 : NULL, &sp_div, &Q_sp_div, &non_staX, &loc_harm, lf_E, &hNoiseEst->harm_cor_cnt, hNoiseEst->Etot_l_lp_fx,
494 3100 : hNoiseEst->Etot_v_h2_fx, &hNoiseEst->bg_cnt, st->lgBin_E_fx, *Q_new, Le_min_scaled, &sp_floor, NULL,
495 3100 : st->ini_frame );
496 :
497 : /*------------------------------------------------------------------*
498 : * Update parameters used in the VAD and DTX
499 : *-----------------------------------------------------------------*/
500 3100 : vad_param_updt_fx( st, old_pitch1, corr_shift, corr_shift, A, NULL, 1 );
501 :
502 : /*-----------------------------------------------------------------*
503 : * Find spectral tilt
504 : * UC and VC frame selection
505 : *-----------------------------------------------------------------*/
506 :
507 3100 : find_tilt_fx( fr_bands, hNoiseEst->bckr_fx, ee, st->pitch, st->voicing_fx, lf_E, corr_shift, st->input_bwidth,
508 3100 : st->max_band, hp_E, st->codec_mode, *Q_new, &( st->bckr_tilt_lt ), st->Opt_SC_VBR );
509 :
510 6200 : st->coder_type = find_uv_fx( st, pitch_fr, voicing_fr, inp_12k8, ee,
511 3100 : corr_shift, relE, *Etot, hp_E, *Q_new, &flag_spitch, *shift, last_core_orig );
512 :
513 : /*----------------------------------------------------------------*
514 : * channel aware mode configuration *
515 : *-----------------------------------------------------------------*/
516 3100 : test();
517 3100 : test();
518 3100 : IF( !st->Opt_RF_ON )
519 : {
520 3100 : st->rf_mode = 0;
521 3100 : move16();
522 3100 : st->rf_target_bits_write = 0;
523 3100 : move16();
524 : }
525 0 : ELSE IF( st->rf_mode && st->core_brate != FRAME_NO_DATA && NE_32( st->core_brate, SID_2k40 ) )
526 : {
527 : /* the RF config is for (n- fec_offset)th frame that will be packed along with the n-th frame bitstream */
528 0 : st->rf_mode = 1;
529 0 : move16();
530 0 : st->codec_mode = MODE2;
531 0 : move16();
532 :
533 0 : st->rf_target_bits_write = hRF->rf_targetbits_buff[st->rf_fec_offset];
534 0 : move16();
535 : }
536 : ELSE
537 : {
538 0 : st->rf_mode = 0;
539 0 : move16();
540 0 : st->codec_mode = MODE1;
541 0 : move16();
542 0 : IF( st->Opt_RF_ON )
543 : {
544 0 : hRF->rf_indx_frametype[0] = RF_NO_DATA;
545 0 : move16();
546 0 : hRF->rf_targetbits_buff[0] = 6; /* rf_mode: 1, rf_frame_type: 3, and fec_offset: 2 */
547 0 : move16();
548 : }
549 : }
550 :
551 : /*-----------------------------------------------------------------*
552 : * Signal classification for FEC
553 : * TC frame selection
554 : *-----------------------------------------------------------------*/
555 :
556 3100 : st->clas = signal_clas_fx( st, inp_12k8, ee, relE, L_look, &clas_mod );
557 :
558 3100 : select_TC_fx( st->codec_mode, st->tc_cnt, &st->coder_type, st->localVAD );
559 :
560 : /* limit coder_type depending on the bitrate */
561 3100 : coder_type_modif_fx( st, relE );
562 :
563 :
564 3100 : if ( st->Opt_SC_VBR )
565 : {
566 0 : hSC_VBR->Local_VAD = st->localVAD;
567 0 : move16();
568 : }
569 : /*----------------------------------------------------------------*
570 : * Speech/music classification
571 : * AC frame selection
572 : *----------------------------------------------------------------*/
573 :
574 55800 : FOR( i = 0; i < M + 1; i++ )
575 : {
576 52700 : epsP[i] = L_Comp( epsP_h[i], epsP_l[i] );
577 52700 : move32();
578 : }
579 :
580 3100 : Q_esp = add( 2 * ( *Q_new ), add( Q_r[0], 1 ) );
581 :
582 3100 : speech_music_classif_fx( st, new_inp_12k8, inp_12k8, localVAD_HE_SAD, lsp_new, cor_map_sum, epsP, PS,
583 3100 : *Etot, old_cor, attack_flag, non_staX, relE, Q_esp, *Q_new, &high_lpn_flag, flag_spitch );
584 :
585 3100 : long_enr_fx( st, *Etot, localVAD_HE_SAD, high_lpn_flag ); /* has to be after after sp_music classfier */
586 :
587 : /*----------------------------------------------------------------*
588 : * Rewrite the VAD flag by VAD flag with DTX hangover for further processing)
589 : *----------------------------------------------------------------*/
590 :
591 3100 : if ( st->Opt_DTX_ON )
592 : {
593 0 : st->vad_flag = vad_flag_dtx;
594 0 : move16(); /* flag now with the DTX-HO for use in further high rate encoding below */
595 : }
596 :
597 : /*----------------------------------------------------------------*
598 : * Selection of internal ACELP Fs (12.8 kHz or 16 kHz)
599 : *----------------------------------------------------------------*/
600 :
601 3100 : IF( EQ_16( st->codec_mode, MODE1 ) )
602 : {
603 2050 : test();
604 2050 : test();
605 2050 : test();
606 2050 : test();
607 2050 : test();
608 2050 : test();
609 2050 : test();
610 2050 : test();
611 2050 : test();
612 2050 : test();
613 2050 : test();
614 2050 : test();
615 2050 : test();
616 2050 : test();
617 2050 : IF( st->core_brate == FRAME_NO_DATA )
618 : {
619 : /* prevent "L_frame" changes in CNG segments */
620 0 : st->L_frame = st->last_L_frame;
621 0 : move16();
622 : }
623 2050 : ELSE IF( EQ_32( st->core_brate, SID_2k40 ) && GE_16( st->bwidth, WB ) && hDtxEnc->first_CNG && ( hTdCngEnc != NULL && LT_16( hTdCngEnc->act_cnt2, MIN_ACT_CNG_UPD ) ) )
624 : {
625 : /* prevent "L_frame" changes in SID frame after short segment of active frames */
626 0 : st->L_frame = hDtxEnc->last_CNG_L_frame;
627 0 : move16();
628 : }
629 2050 : ELSE IF( ( EQ_32( st->core_brate, SID_2k40 ) && GE_32( st->total_brate, ACELP_9k60 ) && ( ( EQ_16( st->bwidth, WB ) && !( EQ_32( st->total_brate, ACELP_13k20 ) && EQ_16( st->cng_type, FD_CNG ) ) ) || ( EQ_16( st->cng_type, LP_CNG ) && GT_16( st->bwidth, WB ) && GE_32( st->total_brate, ACELP_16k40 ) ) ) ) ||
630 : ( GT_32( st->total_brate, ACELP_24k40 ) && LT_32( st->total_brate, HQ_96k ) ) || ( EQ_32( st->total_brate, ACELP_24k40 ) && GE_16( st->bwidth, WB ) ) )
631 : {
632 1000 : st->L_frame = L_FRAME16k;
633 1000 : move16();
634 : }
635 : ELSE
636 : {
637 1050 : st->L_frame = L_FRAME;
638 1050 : move16();
639 : }
640 :
641 2050 : if ( st->ini_frame == 0 )
642 : {
643 : /* avoid switching of internal ACELP Fs in the very first frame */
644 2 : st->last_L_frame = st->L_frame;
645 2 : move16();
646 : }
647 :
648 2050 : IF( EQ_16( st->L_frame, L_FRAME ) )
649 : {
650 1050 : st->gamma = GAMMA1;
651 1050 : move16();
652 1050 : st->preemph_fac = PREEMPH_FAC;
653 1050 : move16();
654 : }
655 : ELSE
656 : {
657 1000 : st->gamma = GAMMA16k;
658 1000 : move16();
659 1000 : st->preemph_fac = PREEMPH_FAC_16k;
660 1000 : move16();
661 : }
662 :
663 2050 : st->sr_core = L_mult0( FRAMES_PER_SEC, st->L_frame );
664 2050 : move32();
665 2050 : st->encoderLookahead_enc = NS2SA_FX2( st->sr_core, ACELP_LOOK_NS );
666 2050 : move16();
667 2050 : st->encoderPastSamples_enc = shr( imult1616( st->L_frame, 9 ), 4 );
668 2050 : move16();
669 : }
670 :
671 : /*-----------------------------------------------------------------*
672 : * coder_type rewriting in case of switching
673 : * IC frames selection
674 : * enforce TC frames in case of switching
675 : *-----------------------------------------------------------------*/
676 :
677 3100 : IF( EQ_16( st->codec_mode, MODE1 ) )
678 : {
679 : /* enforce TRANSITION frames */
680 2050 : test();
681 2050 : test();
682 2050 : test();
683 2050 : test();
684 2050 : test();
685 2050 : test();
686 2050 : test();
687 2050 : test();
688 2050 : test();
689 2050 : test();
690 2050 : test();
691 2050 : test();
692 2050 : test();
693 2050 : IF( NE_16( st->last_L_frame, st->L_frame ) && st->core_brate != FRAME_NO_DATA && NE_32( st->core_brate, SID_2k40 ) && ( NE_16( st->coder_type_raw, VOICED ) ) )
694 : {
695 : /* enforce TC frame in case of ACELP@12k8 <-> ACELP@16k core switching */
696 0 : st->coder_type = TRANSITION;
697 0 : move16();
698 : }
699 2050 : ELSE IF( EQ_16( st->last_core, HQ_CORE ) || EQ_16( st->last_core, TCX_10_CORE ) || EQ_16( st->last_core, TCX_20_CORE ) )
700 : {
701 : /* enforce TC frame in case of HQ/TCX -> ACELP core switching */
702 622 : st->coder_type = TRANSITION;
703 622 : move16();
704 : }
705 1428 : ELSE IF( LE_32( st->last_core_brate, SID_2k40 ) && EQ_16( st->cng_type, FD_CNG ) )
706 : {
707 : /* enforce TC frame in case of FD_CNG -> ACELP switching (past excitation not available) */
708 0 : st->coder_type = TRANSITION;
709 0 : move16();
710 : }
711 :
712 : /* select INACTIVE frames */
713 1428 : ELSE IF( LE_32( st->total_brate, ACELP_24k40 ) && st->vad_flag == 0 )
714 : {
715 : /* inactive frames will be coded by GSC technology */
716 : /* except for the VBR mode. VBR mode uses NELP for that */
717 5 : test();
718 5 : IF( !( EQ_16( st->Opt_SC_VBR, 1 ) && EQ_16( vad_flag_dtx, 1 ) ) )
719 : {
720 5 : st->coder_type = INACTIVE;
721 5 : move16();
722 5 : hGSCEnc->noise_lev = NOISE_LEVEL_SP3;
723 5 : move16();
724 : }
725 : }
726 1423 : ELSE IF( GT_32( st->total_brate, ACELP_24k40 ) &&
727 : ( ( st->vad_flag == 0 && GE_16( st->bwidth, SWB ) && GE_16( st->max_bwidth, SWB ) ) || ( st->localVAD == 0 && ( LE_16( st->bwidth, WB ) || LE_16( st->max_bwidth, WB ) ) ) ) )
728 : {
729 : /* inactive frames will be coded by AVQ technology */
730 0 : st->coder_type = INACTIVE;
731 0 : move16();
732 : }
733 : }
734 : ELSE
735 : {
736 1050 : IF( !( st->vad_flag ) )
737 : {
738 10 : st->coder_type = INACTIVE;
739 10 : move16();
740 : }
741 1040 : ELSE IF( GT_16( st->coder_type, GENERIC ) )
742 : {
743 0 : st->coder_type = GENERIC;
744 0 : move16();
745 : }
746 : }
747 :
748 : /*---------------------------------------------------------------*
749 : * SC-VBR - decision about PPP/NELP mode
750 : *---------------------------------------------------------------*/
751 :
752 3100 : IF( st->Opt_SC_VBR )
753 : {
754 0 : set_ppp_mode_fx( st, noisy_speech_HO, clean_speech_HO, NB_speech_HO, localVAD_HE_SAD );
755 : }
756 3100 : test();
757 3100 : IF( !st->Opt_AMR_WB && !st->rf_mode )
758 : {
759 3100 : test();
760 3100 : test();
761 3100 : IF( EQ_32( st->total_brate, ACELP_13k20 ) || EQ_32( st->total_brate, ACELP_32k ) )
762 : {
763 1050 : st->mdct_sw_enable = MODE1;
764 1050 : move16();
765 : }
766 2050 : ELSE IF( LE_32( ACELP_16k40, st->total_brate ) && LE_32( st->total_brate, ACELP_24k40 ) )
767 : {
768 1050 : st->mdct_sw_enable = MODE2;
769 1050 : move16();
770 : }
771 : }
772 :
773 : /*---------------------------------------------------------------------*
774 : * Decision matrix (selection of technologies)
775 : *---------------------------------------------------------------------*/
776 3100 : IF( EQ_16( st->codec_mode, MODE1 ) )
777 : {
778 :
779 2050 : decision_matrix_enc_fx( st, hq_core_type );
780 :
781 : /* HQ_CORE/TCX_20_CORE decision */
782 2050 : IF( EQ_16( st->core, HQ_CORE ) ) /* Decision matrix decided for MDCT coding */
783 : {
784 623 : test();
785 623 : test();
786 623 : IF( ( EQ_16( st->bwidth, SWB ) || EQ_16( st->bwidth, FB ) ) && EQ_32( st->total_brate, ACELP_32k ) )
787 : {
788 : /* Select MDCT Core */
789 0 : st->core = mdct_classifier_fx( fft_buff, st, enerBuffer, sub( enerBuffer_exp, 31 ), st->total_brate );
790 : }
791 623 : test();
792 623 : IF( ( EQ_32( st->total_brate, ACELP_13k20 ) ) && ( NE_16( st->bwidth, FB ) ) )
793 : {
794 297 : MDCT_selector_fx( st, sp_floor, *Etot, cor_map_sum, enerBuffer, enerBuffer_exp );
795 : }
796 : }
797 : ELSE
798 : {
799 1427 : MDCT_selector_reset_fx( hTcxEnc );
800 : }
801 :
802 : /* Switch to MODE2 if TCX_20_CORE */
803 2050 : IF( EQ_16( st->core, TCX_20_CORE ) )
804 : {
805 261 : st->codec_mode = MODE2;
806 261 : move16();
807 261 : IF( EQ_16( st->last_codec_mode, MODE1 ) )
808 : {
809 19 : Word32 last_total_brate = L_add( st->last_total_brate, 0 );
810 19 : st->last_total_brate = -1;
811 19 : move32();
812 19 : SetModeIndex_fx( st, st->last_total_brate, EVS_MONO, 0, *shift );
813 19 : st->last_total_brate = last_total_brate;
814 19 : move32();
815 : }
816 : ELSE
817 : {
818 242 : SetModeIndex_fx( st, st->last_total_brate, EVS_MONO, 0, *shift );
819 242 : st->sr_core = getCoreSamplerateMode2( st->element_mode, st->total_brate, st->bwidth, st->flag_ACELP16k, st->rf_mode, 0 );
820 242 : move32();
821 242 : Mpy_32_16_ss( st->sr_core, 5243, &L_tmp, &lsb ); /* 5243 is 1/50 in Q18. (0+18-15=3) */
822 242 : st->L_frame = extract_l( L_shr( L_tmp, 3 ) ); /* Q0 */
823 242 : move16();
824 242 : st->encoderLookahead_enc = NS2SA_FX2( st->sr_core, ACELP_LOOK_NS );
825 242 : move16();
826 242 : st->encoderPastSamples_enc = shr( imult1616( st->L_frame, 9 ), 4 );
827 242 : move16();
828 242 : assert( st->L_frame == st->sr_core / 50 );
829 :
830 242 : IF( EQ_32( st->sr_core, INT_FS_12k8 ) )
831 : {
832 242 : st->preemph_fac = PREEMPH_FAC;
833 242 : move16();
834 242 : st->gamma = GAMMA1;
835 242 : move16();
836 : }
837 : ELSE
838 : {
839 0 : st->preemph_fac = PREEMPH_FAC_16k;
840 0 : move16();
841 0 : st->gamma = GAMMA16k;
842 0 : move16();
843 : }
844 :
845 242 : st->igf = getIgfPresent_fx( st->element_mode, st->total_brate, st->bwidth, st->rf_mode );
846 : }
847 :
848 261 : st->coder_type = st->coder_type_raw;
849 261 : move16();
850 :
851 261 : IF( st->vad_flag == 0 )
852 : {
853 2 : st->coder_type = INACTIVE;
854 2 : move16();
855 : }
856 259 : ELSE IF( GT_16( ( st->coder_type ), GENERIC ) )
857 : {
858 0 : st->coder_type = GENERIC;
859 0 : move16();
860 : }
861 :
862 261 : st->mdct_sw = MODE1;
863 261 : move16();
864 : }
865 : }
866 :
867 : /*-----------------------------------------------------------------*
868 : * Update of ACELP harmonicity counter (used in ACELP transform codebook @32kbps)
869 : *-----------------------------------------------------------------*/
870 :
871 3100 : test();
872 3100 : test();
873 3100 : test();
874 3100 : test();
875 3100 : IF( EQ_32( st->total_brate, ACELP_32k ) && EQ_16( loc_harm, 1 ) && GT_16( cor_map_sum, 50 << 8 ) && EQ_16( st->clas, VOICED_CLAS ) && EQ_16( st->coder_type, GENERIC ) )
876 : {
877 0 : st->last_harm_flag_acelp = add( st->last_harm_flag_acelp, 1 );
878 0 : move16();
879 0 : st->last_harm_flag_acelp = s_min( st->last_harm_flag_acelp, 10 );
880 0 : move16();
881 : }
882 : ELSE
883 : {
884 3100 : st->last_harm_flag_acelp = 0;
885 3100 : move16();
886 : }
887 :
888 : /*-----------------------------------------------------------------*
889 : * Update audio frames counter (used for UV decision)
890 : *-----------------------------------------------------------------*/
891 :
892 3100 : IF( EQ_16( st->coder_type, AUDIO ) )
893 : {
894 19 : st->audio_frame_cnt = add( st->audio_frame_cnt, AUDIO_COUNTER_STEP );
895 19 : move16();
896 : }
897 3081 : ELSE IF( st->coder_type != INACTIVE )
898 : {
899 3064 : st->audio_frame_cnt = sub( st->audio_frame_cnt, 1 );
900 3064 : move16();
901 : }
902 :
903 3100 : st->audio_frame_cnt = s_min( st->audio_frame_cnt, AUDIO_COUNTER_MAX );
904 3100 : move16();
905 3100 : st->audio_frame_cnt = s_max( st->audio_frame_cnt, 0 );
906 3100 : move16();
907 :
908 : /*-----------------------------------------------------------------*
909 : * Set formant sharpening flag
910 : *-----------------------------------------------------------------*/
911 :
912 3100 : st->sharpFlag = 0;
913 3100 : move16();
914 3100 : IF( EQ_16( st->coder_type, TRANSITION ) )
915 : {
916 464 : test();
917 464 : test();
918 464 : test();
919 464 : test();
920 464 : test();
921 464 : IF( ( GT_32( st->total_brate, ACELP_48k ) && LT_16( st->bwidth, SWB ) ) || /* Deactivate for core bitrates higher than 48.0 kb/s */
922 : ( GE_32( st->total_brate, ACELP_13k20 ) && LE_32( st->total_brate, ACELP_16k40 ) ) || /* Deactivate for bitrates <13.2, 16.4> kb/s (this is basically due to lack of signaling configurations */
923 : ( GT_32( st->total_brate, ACELP_16k40 ) && GT_16( st->lp_noise_fx, FORMANT_SHARPENING_NOISE_THRESHOLD_FX ) ) ) /* Deactivate for bitrates >= 24.4 kb/s if the long-term noise level exceeds 34 dB */
924 : {
925 103 : st->sharpFlag = 0;
926 103 : move16();
927 : }
928 : ELSE
929 : {
930 361 : st->sharpFlag = 1;
931 361 : move16();
932 : }
933 : }
934 :
935 3100 : test();
936 3100 : IF( EQ_16( st->coder_type, GENERIC ) || EQ_16( st->coder_type, VOICED ) )
937 : {
938 2551 : test();
939 2551 : test();
940 2551 : test();
941 2551 : test();
942 2551 : test();
943 2551 : IF( *vad_hover_flag ||
944 : ( GT_32( st->total_brate, ACELP_48k ) && LT_16( st->bwidth, SWB ) ) || /* Deactivate for core bitrates higher than 48.0 kb/s */
945 : ( GE_32( st->total_brate, ACELP_13k20 ) && GT_16( st->lp_noise_fx, FORMANT_SHARPENING_NOISE_THRESHOLD_FX ) /* Deactivate for bitrates >= 13.2 kb/s if the long-term noise level exceeds 34 dB */
946 : && GT_32( st->total_brate, CNA_MAX_BRATE ) ) )
947 : {
948 75 : st->sharpFlag = 0;
949 75 : move16();
950 : }
951 : ELSE
952 : {
953 2476 : st->sharpFlag = 1;
954 2476 : move16();
955 : }
956 : }
957 :
958 : /* channel-aware mode - due to lack of signalling bit, sharpFlag is 1 always in RF mode */
959 3100 : test();
960 3100 : test();
961 3100 : if ( EQ_16( st->rf_mode, 1 ) && ( EQ_16( st->coder_type, VOICED ) || EQ_16( st->coder_type, GENERIC ) ) )
962 : {
963 0 : st->sharpFlag = 1;
964 0 : move16();
965 : }
966 :
967 : /*-----------------------------------------------------------------*
968 : * Set voicing flag for HQ FEC
969 : *-----------------------------------------------------------------*/
970 :
971 3100 : *Voicing_flag = 0;
972 3100 : move16();
973 3100 : test();
974 3100 : test();
975 3100 : if ( st->sp_aud_decision1 == 0 && ( EQ_16( st->coder_type, VOICED ) || EQ_16( st->coder_type, GENERIC ) ) )
976 : {
977 2034 : *Voicing_flag = 1;
978 2034 : move16();
979 : }
980 :
981 : /*---------------------------------------------------------------*
982 : * Preprocessing at other sampling frequency rate (16/25.6/32kHz)
983 : *----------------------------------------------------------------*/
984 :
985 3100 : sr_core_tmp = L_max( INT_FS_16k_FX, st->sr_core ); /* indicates the ACELP sampling rate for MODE2, 16 kHz for MODE1 */
986 3100 : if ( EQ_16( st->codec_mode, MODE1 ) )
987 : {
988 1789 : sr_core_tmp = INT_FS_16k_FX;
989 1789 : move32();
990 : }
991 :
992 3100 : L_frame_tmp = s_max( L_FRAME16k, st->L_frame );
993 3100 : if ( EQ_16( st->codec_mode, MODE1 ) )
994 : {
995 1789 : L_frame_tmp = L_FRAME16k;
996 1789 : move16();
997 : }
998 3100 : L_look = NS2SA_FX2( sr_core_tmp, ACELP_LOOK_NS ); /* lookahead at other sampling rate (16kHz, 25.5kHz, 32kHz) */
999 :
1000 3100 : new_inp_16k = old_inp_16k + L_INP_MEM; /* pointer to new samples of the input signal in 16kHz core */
1001 3100 : inp_16k = new_inp_16k - L_look; /* pointer to the current frame of input signal in 16kHz core */
1002 3100 : Copy( st->old_inp_16k_fx, old_inp_16k, L_INP_MEM ); /* Note: The merory has been rescaled at the begining of the function*/
1003 :
1004 : /*---------------------------------------------------------------*
1005 : * Change the sampling frequency to 16/25.6/32 kHz
1006 : *----------------------------------------------------------------*/
1007 :
1008 3100 : test();
1009 3100 : IF( EQ_32( st->input_Fs, sr_core_tmp ) )
1010 : {
1011 : /* no resampling needed, only delay adjustement to account for the FIR resampling delay */
1012 0 : delay = NS2SA_FX2( st->input_Fs, DELAY_FIR_RESAMPL_NS );
1013 0 : Copy_Scale_sig( st->mem_decim16k_fx + delay, new_inp_16k, delay, -1 ); /* Input in Q0 -> Output in Q-1 to mimic the resampling filter */
1014 0 : Copy_Scale_sig( signal_in, new_inp_16k + delay, input_frame - delay, -1 ); /* Input in Q0 -> Output in Q-1 to mimic the resampling filter */
1015 0 : Copy( signal_in + input_frame - shl( delay, 1 ), st->mem_decim16k_fx, shl( delay, 1 ) ); /* memory still in Q0 */
1016 : }
1017 3100 : ELSE IF( EQ_32( st->input_Fs, 32000 ) || EQ_32( st->input_Fs, 48000 ) )
1018 : {
1019 3100 : modify_Fs_fx( signal_in, input_frame, st->input_Fs, new_inp_16k, sr_core_tmp, st->mem_decim16k_fx, 0 );
1020 : }
1021 : ELSE /* keep memories up-to-date in case of bitrate switching */
1022 : {
1023 : /* no resampling needed, only delay adjustement to account for the FIR resampling delay */
1024 0 : delay = NS2SA_FX2( st->input_Fs, DELAY_FIR_RESAMPL_NS );
1025 0 : Copy( st->mem_decim16k_fx + delay, new_inp_16k, delay );
1026 0 : Copy( signal_in, new_inp_16k + delay, sub( input_frame, delay ) );
1027 0 : Copy( signal_in + sub( input_frame, shl( delay, 1 ) ), st->mem_decim16k_fx, shl( delay, 1 ) );
1028 : }
1029 :
1030 3100 : IF( EQ_32( sr_core_tmp, INT_FS_16k ) )
1031 : {
1032 : /* save input resampled at 16kHz, non-preemhasised */
1033 3100 : Copy( new_inp_16k, new_inp_resamp16k, L_FRAME16k );
1034 : }
1035 0 : ELSE IF( GT_32( sr_core_tmp, INT_FS_16k ) )
1036 : {
1037 : /* reset the buffer, the signal is needed for WB BWEs */
1038 0 : set16_fx( new_inp_resamp16k, 0, L_FRAME16k );
1039 : }
1040 :
1041 : /*------------------------------------------------------------------*
1042 : * Perform fixed preemphasis (16kHz signal) through 1 - g*z^-1
1043 : *-----------------------------------------------------------------*/
1044 :
1045 3100 : test();
1046 3100 : test();
1047 3100 : IF( ( ( st->tcxonly == 0 ) || ( EQ_16( st->codec_mode, MODE1 ) ) ) && GT_32( st->input_Fs, 8000 ) )
1048 : {
1049 3100 : st->mem_preemph_enc = shl_o( new_inp_16k[sub( L_frame_tmp, 1 )], 1, &Overflow );
1050 3100 : move16();
1051 : }
1052 :
1053 3100 : test();
1054 3100 : IF( GT_32( st->input_Fs, 8000 ) && EQ_32( sr_core_tmp, INT_FS_16k ) )
1055 : {
1056 3100 : Preemph_scaled( new_inp_16k, &Q_new_16k, &( st->mem_preemph16k_fx ), st->Q_max_16k, PREEMPH_FAC_16k, 0, 1, L_Q_MEM, L_FRAME16k, st->last_coder_type, 1 );
1057 : }
1058 0 : ELSE IF( GT_32( st->input_Fs, 8000 ) ) /* keep memory up-to-date in case of bit-rate switching */
1059 : {
1060 0 : st->mem_preemph16k_fx = new_inp_16k[L_frame_tmp - 1];
1061 0 : move16();
1062 : }
1063 :
1064 : /*------------------------------------------------------------------*
1065 : * Core-encoder memories scaling
1066 : *-----------------------------------------------------------------*/
1067 :
1068 3100 : test();
1069 3100 : test();
1070 3100 : test();
1071 3100 : test();
1072 3100 : test();
1073 3100 : test();
1074 3100 : IF( ( ( ( st->tcxonly == 0 ) || !( st->core_brate != FRAME_NO_DATA || NE_32( st->core_brate, SID_2k40 ) ) ) && EQ_16( st->L_frame, L_FRAME16k ) && EQ_16( st->codec_mode, MODE2 ) ) ||
1075 : ( EQ_16( st->L_frame, L_FRAME16k ) && EQ_16( st->codec_mode, MODE1 ) ) )
1076 : {
1077 2050 : *Q_new = Q_new_16k;
1078 2050 : move16();
1079 : }
1080 : ELSE
1081 : {
1082 1050 : IF( GT_32( st->input_Fs, 8000 ) && EQ_32( sr_core_tmp, 16000 ) )
1083 : {
1084 1050 : Scale_sig( new_inp_16k, L_FRAME16k, sub( *Q_new, Q_new_16k ) );
1085 : }
1086 : }
1087 :
1088 : /* Above computed Q_new is used to scale primary copy exc and memory here by (Q_new, st->prev_Q_new) */
1089 :
1090 3100 : Q_exp = sub( *Q_new, st->prev_Q_new );
1091 3100 : Q_wsp_exp = add( Q_exp, shift_exp );
1092 :
1093 3100 : Scale_mem_enc( Q_exp, old_inp_16k, old_exc, hBWE_TD->old_bwe_exc_fx, &( hLPDmem->mem_w0 ), hLPDmem->mem_syn,
1094 3100 : hLPDmem->mem_syn2, &st->mem_deemp_preQ_fx, hGSCEnc->last_exc_dct_in_fx, hBWE_FD->old_input_lp_fx );
1095 :
1096 : /*-----------------------------------------------------------------*
1097 : * Redo LP analysis at 16kHz if ACELP@16k core was selected
1098 : * update buffers
1099 : *-----------------------------------------------------------------*/
1100 :
1101 3100 : test();
1102 3100 : test();
1103 3100 : test();
1104 3100 : test();
1105 3100 : test();
1106 3100 : test();
1107 3100 : IF( ( ( ( st->tcxonly == 0 ) || !( st->core_brate != FRAME_NO_DATA && NE_32( st->core_brate, SID_2k40 ) ) ) && EQ_16( st->L_frame, L_FRAME16k ) && EQ_16( st->codec_mode, MODE2 ) ) || ( EQ_16( st->L_frame, L_FRAME16k ) && EQ_16( st->codec_mode, MODE1 ) ) )
1108 : {
1109 : /* update signal buffers */
1110 2050 : Copy( new_inp_resamp16k, st->buf_speech_enc + L_FRAME16k, L_FRAME16k );
1111 2050 : Scale_sig( st->buf_speech_enc + L_FRAME16k, L_FRAME16k, 1 );
1112 2050 : Copy( new_inp_16k, st->buf_speech_enc_pe + L_FRAME16k, L_FRAME16k );
1113 2050 : IF( Q_exp != 0 )
1114 : {
1115 379 : Scale_sig( st->buf_speech_enc_pe, st->encoderPastSamples_enc + st->encoderLookahead_enc, Q_exp );
1116 379 : Scale_sig( &( st->mem_wsp_enc ), 1, Q_exp );
1117 : }
1118 :
1119 : /*--------------------------------------------------------------*
1120 : * LPC analysis
1121 : *---------------------------------------------------------------*/
1122 :
1123 2050 : test();
1124 2050 : IF( EQ_16( st->last_L_frame, L_FRAME ) && EQ_16( st->codec_mode, MODE1 ) )
1125 : {
1126 : /* this is just an approximation, but it is sufficient */
1127 0 : Copy( st->lsp_old1_fx, st->lspold_enc_fx, M );
1128 : }
1129 :
1130 2050 : analy_lp_fx( inp_16k, L_FRAME16k, L_look, ener, A, epsP_h, epsP_l, lsp_new, lsp_mid, st->lspold_enc_fx, st->pitch, st->voicing_fx, 16000, EVS_MONO, 0, *Q_new, Q_r );
1131 :
1132 : /*--------------------------------------------------------------*
1133 : * Compute Weighted Input
1134 : *---------------------------------------------------------------*/
1135 :
1136 2050 : IF( EQ_16( st->codec_mode, MODE2 ) )
1137 : {
1138 1050 : find_wsp_fx( A, st->speech_enc_pe, st->wspeech_enc, &st->mem_wsp_enc, PREEMPH_FAC_16k, L_FRAME16k, L_LOOK_16k, L_SUBFR, Aw, st->gamma, st->nb_subfr );
1139 :
1140 : /* This need to be harmonized with MODE2 */
1141 1050 : Scale_sig( st->wspeech_enc, L_FRAME16k + L_LOOK_16k, *shift );
1142 : }
1143 : ELSE
1144 : {
1145 1000 : weight_a_subfr_fx( NB_SUBFR16k, A, Aw, GAMMA16k, M );
1146 : }
1147 : }
1148 : ELSE
1149 : {
1150 : /* update signal buffers */
1151 1050 : Copy( new_inp_12k8, st->buf_speech_enc_pe + st->L_frame, L_FRAME );
1152 1050 : Copy( st->buf_speech_enc + L_FRAME32k, st->buf_speech_enc + st->L_frame, L_FRAME );
1153 :
1154 1050 : if ( st->tcxonly == 0 )
1155 : {
1156 1050 : Copy( wsp, st->buf_wspeech_enc + st->L_frame + L_SUBFR, L_FRAME + L_LOOK_12k8 );
1157 : }
1158 1050 : test();
1159 1050 : test();
1160 1050 : IF( EQ_16( st->codec_mode, MODE2 ) && st->tcxonly == 0 && Q_exp != 0 )
1161 : {
1162 45 : Scale_sig( st->buf_speech_enc_pe, st->encoderPastSamples_enc + st->encoderLookahead_enc, Q_exp );
1163 45 : Scale_sig( &( st->mem_wsp_enc ), 1, Q_exp );
1164 : }
1165 : }
1166 :
1167 3100 : excitation_max_test = -32768;
1168 3100 : move16();
1169 995100 : FOR( i = 0; i < L_EXC_MEM; i++ )
1170 : {
1171 992000 : excitation_max_test = s_max( abs_s( old_exc[i] ), excitation_max_test );
1172 : }
1173 :
1174 3100 : test();
1175 3100 : IF( GT_16( excitation_max_test, 8192 ) && *shift == 0 )
1176 : {
1177 58 : excitation_max_test = 1;
1178 58 : move16();
1179 58 : *shift = -1;
1180 58 : move16();
1181 58 : st->old_wsp_shift = -1;
1182 58 : move16();
1183 58 : Scale_sig( old_wsp, L_WSP_MEM + L_FRAME + L_LOOK_12k8, -1 );
1184 : }
1185 : ELSE
1186 : {
1187 3042 : excitation_max_test = 0;
1188 3042 : move16();
1189 : }
1190 :
1191 3100 : test();
1192 3100 : IF( EQ_16( st->codec_mode, MODE2 ) && st->tcxonly == 0 )
1193 : {
1194 1311 : IF( Q_wsp_exp != 0 )
1195 : {
1196 438 : Scale_sig( st->buf_wspeech_enc, st->L_frame + L_SUBFR, Q_wsp_exp );
1197 : }
1198 1311 : IF( EQ_16( excitation_max_test, 1 ) )
1199 : {
1200 12 : Scale_sig( st->buf_wspeech_enc, st->L_frame + L_SUBFR + st->L_frame + st->encoderLookahead_enc, -1 );
1201 : }
1202 : }
1203 :
1204 : /*-----------------------------------------------------------------*
1205 : * ACELP/TCX20/HQ Switching Decision
1206 : *-----------------------------------------------------------------*/
1207 :
1208 : #ifndef FIX_I4_OL_PITCH
1209 3100 : Copy( st->pitch, pitch_orig, 3 );
1210 : #endif
1211 3100 : IF( EQ_16( st->codec_mode, MODE2 ) )
1212 : {
1213 1311 : test();
1214 1311 : test();
1215 1311 : IF( ( st->core_brate != FRAME_NO_DATA && NE_32( st->core_brate, SID_2k40 ) && st->tcxonly == 0 ) )
1216 : {
1217 1311 : core_acelp_tcx20_switching_fx( st, non_staX, pitch_fr, voicing_fr, currFlatness, lsp_mid, stab_fac, *Q_new, *shift );
1218 : }
1219 :
1220 1311 : test();
1221 1311 : IF( EQ_16( st->mdct_sw_enable, MODE2 ) && !st->rf_mode )
1222 : {
1223 1050 : IF( EQ_16( st->core, TCX_20_CORE ) ) /* Switching only possible from TCX_20 frames, not from TCX_10 frames */
1224 : {
1225 : /* Select MDCT Core */
1226 438 : test();
1227 438 : test();
1228 438 : IF( ( EQ_16( st->bwidth, SWB ) || EQ_16( st->bwidth, FB ) ) && EQ_32( st->total_brate, ACELP_24k40 ) )
1229 : {
1230 438 : st->core = mdct_classifier_fx( fft_buff, st, enerBuffer, sub( enerBuffer_exp, 31 ), st->total_brate );
1231 : }
1232 438 : test();
1233 438 : IF( ( EQ_32( st->total_brate, ACELP_16k40 ) ) && ( NE_16( st->bwidth, FB ) ) )
1234 : {
1235 0 : MDCT_selector_fx( st, sp_floor, *Etot, cor_map_sum, enerBuffer, enerBuffer_exp );
1236 : }
1237 : }
1238 : ELSE
1239 : {
1240 612 : MDCT_selector_reset_fx( hTcxEnc );
1241 : }
1242 :
1243 : /* Do the switching that was decided in the MDCT selector */
1244 1050 : test();
1245 1050 : IF( EQ_16( st->core, HQ_CORE ) )
1246 : {
1247 62 : st->codec_mode = MODE1;
1248 62 : move16();
1249 62 : st->mdct_sw = MODE2;
1250 62 : move16();
1251 : }
1252 988 : ELSE IF( EQ_16( st->last_codec_mode, MODE1 ) && EQ_16( st->last_core, HQ_CORE ) )
1253 : {
1254 0 : Word16 L_frame_old = st->last_L_frame;
1255 0 : move16();
1256 0 : st->last_L_frame = st->L_frame;
1257 0 : move16();
1258 0 : SetModeIndex_fx( st, st->last_total_brate, EVS_MONO, 0, *shift );
1259 0 : st->last_L_frame = L_frame_old;
1260 0 : move16();
1261 : }
1262 : }
1263 :
1264 : /*--------------------------------------------------------------*
1265 : * TCX mode decision
1266 : *---------------------------------------------------------------*/
1267 :
1268 1311 : SetTCXModeInfo_fx( st, &st->transientDetection, &st->hTcxCfg->tcx_curr_overlap_mode );
1269 : }
1270 :
1271 : /*-----------------------------------------------------------------*
1272 : * Updates
1273 : *-----------------------------------------------------------------*/
1274 :
1275 : /* update old weighted speech buffer - for OL pitch analysis */
1276 3100 : Copy( &old_wsp[L_FRAME], st->old_wsp_fx, L_WSP_MEM );
1277 :
1278 : /* update old input signal buffer */
1279 3100 : Copy( &old_inp_12k8[L_FRAME], st->old_inp_12k8_fx, L_INP_MEM );
1280 :
1281 : /* update old input signal @16kHz buffer */
1282 3100 : test();
1283 3100 : IF( GT_32( st->input_Fs, 8000 ) && EQ_32( sr_core_tmp, INT_FS_16k ) )
1284 : {
1285 3100 : Copy( &old_inp_16k[L_frame_tmp], st->old_inp_16k_fx, L_INP_MEM );
1286 : }
1287 0 : ELSE IF( GT_32( st->input_Fs, 8000 ) )
1288 : {
1289 0 : lerp( st->old_inp_12k8_fx + L_INP_MEM - L_INP_MEM * 4 / 5, st->old_inp_16k_fx, L_INP_MEM, L_INP_MEM * 4 / 5 );
1290 0 : Scale_sig( st->old_inp_16k_fx, L_INP_MEM, sub( *Q_new, st->Q_old ) );
1291 : }
1292 :
1293 3100 : test();
1294 3100 : test();
1295 3100 : IF( ( EQ_32( sr_core_tmp, INT_FS_16k ) ) && st->tcxonly && EQ_16( st->codec_mode, MODE2 ) )
1296 : {
1297 : /* copy input resampled at 16kHz, non-preemhasised */
1298 0 : Copy( new_inp_resamp16k, new_inp_16k, L_FRAME16k );
1299 : }
1300 :
1301 : /* update of old per-band energy spectrum */
1302 3100 : Copy32( fr_bands + NB_BANDS, hNoiseEst->enrO_fx, NB_BANDS );
1303 :
1304 : /* set the pointer of the current frame for the ACELP core */
1305 3100 : *inp = inp_16k;
1306 3100 : if ( EQ_16( st->L_frame, L_FRAME ) )
1307 : {
1308 1050 : *inp = inp_12k8;
1309 : }
1310 :
1311 : /* Update vAD hangover frame counter in active frames */
1312 3100 : if ( ( NE_16( st->core, HQ_CORE ) && st->tcxonly == 0 ) || st->core == ACELP_CORE )
1313 : {
1314 :
1315 2676 : IF( !( EQ_32( st->core_brate, SID_2k40 ) || st->core_brate == FRAME_NO_DATA ) )
1316 : {
1317 2676 : test();
1318 2676 : IF( st->Opt_DTX_ON != 0 && *vad_hover_flag != 0 )
1319 : {
1320 0 : hTdCngEnc->burst_ho_cnt = add( hTdCngEnc->burst_ho_cnt, 1 );
1321 0 : move16();
1322 0 : hTdCngEnc->burst_ho_cnt = s_min( hTdCngEnc->burst_ho_cnt, HO_HIST_SIZE );
1323 0 : move16();
1324 : }
1325 : ELSE
1326 : {
1327 2676 : IF( hTdCngEnc != NULL && vad_flag_dtx != 0 )
1328 : {
1329 2675 : hTdCngEnc->burst_ho_cnt = 0;
1330 2675 : move16();
1331 : }
1332 : }
1333 : }
1334 : }
1335 :
1336 3100 : return;
1337 : }
|