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 2304 : test();
278 2304 : if ( EQ_16( st->vad_flag, 1 ) && vad_flag_cldfb == 0 )
279 : {
280 0 : st->localVAD = 0;
281 0 : move16();
282 : }
283 :
284 2304 : st->vad_flag = vad_flag_cldfb;
285 2304 : 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 961 : st->Pos_relE_cnt = 0;
318 961 : move16();
319 : }
320 2139 : ELSE IF( relE < 0 && EQ_16( st->vad_flag, 1 ) )
321 : {
322 1903 : 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 2939 : st->currEnergyHF_e_fx = tmp_e;
394 2939 : 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 : #ifdef REMOVE_EVS_DUPLICATES
418 3100 : analy_lp_ivas_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 );
419 : #else
420 : 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, -1 /*IVAS_CODE !! LowRateFlag*/, *Q_new, Q_r );
421 : #endif
422 :
423 3100 : lsp2lsf_fx( lsp_new, lsf_new, M, INT_FS_12k8 );
424 3100 : stab_fac = lsf_stab_fx( lsf_new, st->lsf_old1_fx, 0, L_FRAME );
425 3100 : Copy( lsf_new, st->lsf_old1_fx, M );
426 :
427 : /*----------------------------------------------------------------*
428 : * Compute weighted input (for OL pitch analysis)
429 : * OL pitch analysis
430 : * 1/4 pitch precision improvement
431 : *----------------------------------------------------------------*/
432 :
433 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 );
434 :
435 3100 : Q_wsp_exp = Q_exp;
436 3100 : move16();
437 3100 : Scale_wsp( wsp, &( st->old_wsp_max ), shift, &Q_wsp_exp, &( st->old_wsp_shift ), st->old_wsp2_fx,
438 3100 : st->mem_decim2_fx, old_wsp, add( L_FRAME, L_look ) );
439 3100 : shift_exp = sub( Q_wsp_exp, Q_exp );
440 :
441 3100 : IF( st->vad_flag == 0 )
442 : {
443 : /* reset the OL pitch tracker memories during inactive frames */
444 21 : pitch_ol_init_fx( &st->old_thres_fx, &st->old_pitch, &st->delta_pit, &st->old_corr_fx );
445 : }
446 3100 : old_pitch1 = st->pitch[1];
447 :
448 3100 : pitch_ol_fx( st->pitch, st->voicing_fx, &st->old_pitch, &st->old_corr_fx, corr_shift, &st->old_thres_fx,
449 3100 : &st->delta_pit, st->old_wsp2_fx, wsp, st->mem_decim2_fx, relE, st->clas, st->bwidth, st->Opt_SC_VBR );
450 :
451 : /* Updates for adaptive lag window memory */
452 3100 : st->old_pitch_la = st->pitch[2];
453 3100 : move16();
454 3100 : st->old_voicing_la = st->voicing_fx[2];
455 3100 : move16();
456 :
457 : /* Detection of very short stable st->pitch period (MODE1 bit-rates) */
458 3100 : StableHighPitchDetect_fx( &flag_spitch, st->pitch, st->voicing_fx, wsp, st->localVAD, &st->voicing_sm_fx, &st->voicing0_sm_fx,
459 3100 : &st->LF_EnergyRatio_sm_fx, &st->predecision_flag, &st->diff_sm_fx, &st->energy_sm_fx, *Q_new, st->lgBin_E_fx );
460 :
461 : /* 1/4 pitch precision improvement */
462 3100 : IF( LE_32( st->total_brate, ACELP_24k40 ) )
463 : {
464 : /* 1/4 pitch precision improvement */
465 2100 : pitch_ol2_fx( PIT_MIN_EXTEND, st->pitch[0], &pitch_fr[0], &voicing_fr[0], 0, wsp, 7 );
466 2100 : pitch_ol2_fx( PIT_MIN_EXTEND, st->pitch[0], &pitch_fr[1], &voicing_fr[1], L_SUBFR, wsp, 7 );
467 2100 : pitch_ol2_fx( PIT_MIN_EXTEND, st->pitch[1], &pitch_fr[2], &voicing_fr[2], 2 * L_SUBFR, wsp, 7 );
468 2100 : pitch_ol2_fx( PIT_MIN_EXTEND, st->pitch[1], &pitch_fr[3], &voicing_fr[3], 3 * L_SUBFR, wsp, 7 );
469 : }
470 : ELSE
471 : {
472 1000 : pitch_fr[0] = st->pitch[0];
473 1000 : move16();
474 1000 : pitch_fr[1] = st->pitch[0];
475 1000 : move16();
476 1000 : pitch_fr[2] = st->pitch[1];
477 1000 : move16();
478 1000 : pitch_fr[3] = st->pitch[1];
479 1000 : move16();
480 :
481 1000 : voicing_fr[0] = st->voicing_fx[0];
482 1000 : move16();
483 1000 : voicing_fr[1] = st->voicing_fx[0];
484 1000 : move16();
485 1000 : voicing_fr[2] = st->voicing_fx[1];
486 1000 : move16();
487 1000 : voicing_fr[3] = st->voicing_fx[1];
488 1000 : move16();
489 : }
490 :
491 :
492 : /*------------------------------------------------------------------*
493 : * Update estimated noise energy and voicing cut-off frequency
494 : *-----------------------------------------------------------------*/
495 :
496 3100 : noise_est_fx( st, old_pitch1, tmpN, epsP_h, epsP_l, *Etot, relE, corr_shift, tmpE, fr_bands, &cor_map_sum,
497 3100 : NULL, &sp_div, &Q_sp_div, &non_staX, &loc_harm, lf_E, &hNoiseEst->harm_cor_cnt, hNoiseEst->Etot_l_lp_fx,
498 3100 : hNoiseEst->Etot_v_h2_fx, &hNoiseEst->bg_cnt, st->lgBin_E_fx, *Q_new, Le_min_scaled, &sp_floor, NULL,
499 3100 : st->ini_frame );
500 :
501 : /*------------------------------------------------------------------*
502 : * Update parameters used in the VAD and DTX
503 : *-----------------------------------------------------------------*/
504 3100 : vad_param_updt_fx( st, old_pitch1, corr_shift, corr_shift, A, NULL, 1 );
505 :
506 : /*-----------------------------------------------------------------*
507 : * Find spectral tilt
508 : * UC and VC frame selection
509 : *-----------------------------------------------------------------*/
510 :
511 3100 : find_tilt_fx( fr_bands, hNoiseEst->bckr_fx, ee, st->pitch, st->voicing_fx, lf_E, corr_shift, st->input_bwidth,
512 3100 : st->max_band, hp_E, st->codec_mode, *Q_new, &( st->bckr_tilt_lt ), st->Opt_SC_VBR );
513 :
514 6200 : st->coder_type = find_uv_fx( st, pitch_fr, voicing_fr, inp_12k8, ee,
515 3100 : corr_shift, relE, *Etot, hp_E, *Q_new, &flag_spitch, *shift, last_core_orig );
516 :
517 : /*----------------------------------------------------------------*
518 : * channel aware mode configuration *
519 : *-----------------------------------------------------------------*/
520 3100 : test();
521 3100 : test();
522 3100 : IF( !st->Opt_RF_ON )
523 : {
524 3100 : st->rf_mode = 0;
525 3100 : move16();
526 3100 : st->rf_target_bits_write = 0;
527 3100 : move16();
528 : }
529 0 : ELSE IF( st->rf_mode && st->core_brate != FRAME_NO_DATA && NE_32( st->core_brate, SID_2k40 ) )
530 : {
531 : /* the RF config is for (n- fec_offset)th frame that will be packed along with the n-th frame bitstream */
532 0 : st->rf_mode = 1;
533 0 : move16();
534 0 : st->codec_mode = MODE2;
535 0 : move16();
536 :
537 0 : st->rf_target_bits_write = hRF->rf_targetbits_buff[st->rf_fec_offset];
538 0 : move16();
539 : }
540 : ELSE
541 : {
542 0 : st->rf_mode = 0;
543 0 : move16();
544 0 : st->codec_mode = MODE1;
545 0 : move16();
546 0 : IF( st->Opt_RF_ON )
547 : {
548 0 : hRF->rf_indx_frametype[0] = RF_NO_DATA;
549 0 : move16();
550 0 : hRF->rf_targetbits_buff[0] = 6; /* rf_mode: 1, rf_frame_type: 3, and fec_offset: 2 */
551 0 : move16();
552 : }
553 : }
554 :
555 : /*-----------------------------------------------------------------*
556 : * Signal classification for FEC
557 : * TC frame selection
558 : *-----------------------------------------------------------------*/
559 :
560 3100 : st->clas = signal_clas_fx( st, inp_12k8, ee, relE, L_look, &clas_mod );
561 :
562 3100 : select_TC_fx( st->codec_mode, st->tc_cnt, &st->coder_type, st->localVAD );
563 :
564 : /* limit coder_type depending on the bitrate */
565 3100 : coder_type_modif_fx( st, relE );
566 :
567 :
568 3100 : if ( st->Opt_SC_VBR )
569 : {
570 0 : hSC_VBR->Local_VAD = st->localVAD;
571 0 : move16();
572 : }
573 : /*----------------------------------------------------------------*
574 : * Speech/music classification
575 : * AC frame selection
576 : *----------------------------------------------------------------*/
577 :
578 55800 : FOR( i = 0; i < M + 1; i++ )
579 : {
580 52700 : epsP[i] = L_Comp( epsP_h[i], epsP_l[i] );
581 52700 : move32();
582 : }
583 :
584 3100 : Q_esp = add( 2 * ( *Q_new ), add( Q_r[0], 1 ) );
585 :
586 3100 : speech_music_classif_fx( st, new_inp_12k8, inp_12k8, localVAD_HE_SAD, lsp_new, cor_map_sum, epsP, PS,
587 3100 : *Etot, old_cor, attack_flag, non_staX, relE, Q_esp, *Q_new, &high_lpn_flag, flag_spitch );
588 :
589 3100 : long_enr_fx( st, *Etot, localVAD_HE_SAD, high_lpn_flag ); /* has to be after after sp_music classfier */
590 :
591 : /*----------------------------------------------------------------*
592 : * Rewrite the VAD flag by VAD flag with DTX hangover for further processing)
593 : *----------------------------------------------------------------*/
594 :
595 3100 : if ( st->Opt_DTX_ON )
596 : {
597 0 : st->vad_flag = vad_flag_dtx;
598 0 : move16(); /* flag now with the DTX-HO for use in further high rate encoding below */
599 : }
600 :
601 : /*----------------------------------------------------------------*
602 : * Selection of internal ACELP Fs (12.8 kHz or 16 kHz)
603 : *----------------------------------------------------------------*/
604 :
605 3100 : IF( EQ_16( st->codec_mode, MODE1 ) )
606 : {
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 : test();
618 2050 : test();
619 2050 : test();
620 2050 : test();
621 2050 : IF( st->core_brate == FRAME_NO_DATA )
622 : {
623 : /* prevent "L_frame" changes in CNG segments */
624 0 : st->L_frame = st->last_L_frame;
625 0 : move16();
626 : }
627 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 ) ) )
628 : {
629 : /* prevent "L_frame" changes in SID frame after short segment of active frames */
630 0 : st->L_frame = hDtxEnc->last_CNG_L_frame;
631 0 : move16();
632 : }
633 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 ) ) ) ) ||
634 : ( 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 ) ) )
635 : {
636 1000 : st->L_frame = L_FRAME16k;
637 1000 : move16();
638 : }
639 : ELSE
640 : {
641 1050 : st->L_frame = L_FRAME;
642 1050 : move16();
643 : }
644 :
645 2050 : if ( st->ini_frame == 0 )
646 : {
647 : /* avoid switching of internal ACELP Fs in the very first frame */
648 2 : st->last_L_frame = st->L_frame;
649 2 : move16();
650 : }
651 :
652 2050 : IF( EQ_16( st->L_frame, L_FRAME ) )
653 : {
654 1050 : st->gamma = GAMMA1;
655 1050 : move16();
656 1050 : st->preemph_fac = PREEMPH_FAC;
657 1050 : move16();
658 : }
659 : ELSE
660 : {
661 1000 : st->gamma = GAMMA16k;
662 1000 : move16();
663 1000 : st->preemph_fac = PREEMPH_FAC_16k;
664 1000 : move16();
665 : }
666 :
667 2050 : st->sr_core = L_mult0( FRAMES_PER_SEC, st->L_frame );
668 2050 : move32();
669 2050 : st->encoderLookahead_enc = NS2SA_FX2( st->sr_core, ACELP_LOOK_NS );
670 2050 : move16();
671 2050 : st->encoderPastSamples_enc = shr( imult1616( st->L_frame, 9 ), 4 );
672 2050 : move16();
673 : }
674 :
675 : /*-----------------------------------------------------------------*
676 : * coder_type rewriting in case of switching
677 : * IC frames selection
678 : * enforce TC frames in case of switching
679 : *-----------------------------------------------------------------*/
680 :
681 3100 : IF( EQ_16( st->codec_mode, MODE1 ) )
682 : {
683 : /* enforce TRANSITION frames */
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 : test();
694 2050 : test();
695 2050 : test();
696 2050 : test();
697 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 ) ) )
698 : {
699 : /* enforce TC frame in case of ACELP@12k8 <-> ACELP@16k core switching */
700 0 : st->coder_type = TRANSITION;
701 0 : move16();
702 : }
703 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 ) )
704 : {
705 : /* enforce TC frame in case of HQ/TCX -> ACELP core switching */
706 621 : st->coder_type = TRANSITION;
707 621 : move16();
708 : }
709 1429 : ELSE IF( LE_32( st->last_core_brate, SID_2k40 ) && EQ_16( st->cng_type, FD_CNG ) )
710 : {
711 : /* enforce TC frame in case of FD_CNG -> ACELP switching (past excitation not available) */
712 0 : st->coder_type = TRANSITION;
713 0 : move16();
714 : }
715 :
716 : /* select INACTIVE frames */
717 1429 : ELSE IF( LE_32( st->total_brate, ACELP_24k40 ) && st->vad_flag == 0 )
718 : {
719 : /* inactive frames will be coded by GSC technology */
720 : /* except for the VBR mode. VBR mode uses NELP for that */
721 7 : test();
722 7 : IF( !( EQ_16( st->Opt_SC_VBR, 1 ) && EQ_16( vad_flag_dtx, 1 ) ) )
723 : {
724 7 : st->coder_type = INACTIVE;
725 7 : move16();
726 7 : hGSCEnc->noise_lev = NOISE_LEVEL_SP3;
727 7 : move16();
728 : }
729 : }
730 1422 : ELSE IF( GT_32( st->total_brate, ACELP_24k40 ) &&
731 : ( ( 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 ) ) ) ) )
732 : {
733 : /* inactive frames will be coded by AVQ technology */
734 0 : st->coder_type = INACTIVE;
735 0 : move16();
736 : }
737 : }
738 : ELSE
739 : {
740 1050 : IF( !( st->vad_flag ) )
741 : {
742 10 : st->coder_type = INACTIVE;
743 10 : move16();
744 : }
745 1040 : ELSE IF( GT_16( st->coder_type, GENERIC ) )
746 : {
747 0 : st->coder_type = GENERIC;
748 0 : move16();
749 : }
750 : }
751 :
752 : /*---------------------------------------------------------------*
753 : * SC-VBR - decision about PPP/NELP mode
754 : *---------------------------------------------------------------*/
755 :
756 3100 : IF( st->Opt_SC_VBR )
757 : {
758 0 : set_ppp_mode_fx( st, noisy_speech_HO, clean_speech_HO, NB_speech_HO, localVAD_HE_SAD );
759 : }
760 3100 : test();
761 3100 : IF( !st->Opt_AMR_WB && !st->rf_mode )
762 : {
763 3100 : test();
764 3100 : test();
765 3100 : IF( EQ_32( st->total_brate, ACELP_13k20 ) || EQ_32( st->total_brate, ACELP_32k ) )
766 : {
767 1050 : st->mdct_sw_enable = MODE1;
768 1050 : move16();
769 : }
770 2050 : ELSE IF( LE_32( ACELP_16k40, st->total_brate ) && LE_32( st->total_brate, ACELP_24k40 ) )
771 : {
772 1050 : st->mdct_sw_enable = MODE2;
773 1050 : move16();
774 : }
775 : }
776 :
777 : /*---------------------------------------------------------------------*
778 : * Decision matrix (selection of technologies)
779 : *---------------------------------------------------------------------*/
780 3100 : IF( EQ_16( st->codec_mode, MODE1 ) )
781 : {
782 :
783 2050 : decision_matrix_enc_fx( st, hq_core_type );
784 :
785 : /* HQ_CORE/TCX_20_CORE decision */
786 2050 : IF( EQ_16( st->core, HQ_CORE ) ) /* Decision matrix decided for MDCT coding */
787 : {
788 623 : test();
789 623 : test();
790 623 : IF( ( EQ_16( st->bwidth, SWB ) || EQ_16( st->bwidth, FB ) ) && EQ_32( st->total_brate, ACELP_32k ) )
791 : {
792 : /* Select MDCT Core */
793 0 : st->core = mdct_classifier_fx( fft_buff, st, enerBuffer, sub( enerBuffer_exp, 31 ), st->total_brate );
794 : }
795 623 : test();
796 623 : IF( ( EQ_32( st->total_brate, ACELP_13k20 ) ) && ( NE_16( st->bwidth, FB ) ) )
797 : {
798 297 : MDCT_selector_fx( st, sp_floor, *Etot, cor_map_sum, enerBuffer, enerBuffer_exp );
799 : }
800 : }
801 : ELSE
802 : {
803 1427 : MDCT_selector_reset_fx( hTcxEnc );
804 : }
805 :
806 : /* Switch to MODE2 if TCX_20_CORE */
807 2050 : IF( EQ_16( st->core, TCX_20_CORE ) )
808 : {
809 260 : st->codec_mode = MODE2;
810 260 : move16();
811 260 : IF( EQ_16( st->last_codec_mode, MODE1 ) )
812 : {
813 20 : Word32 last_total_brate = L_add( st->last_total_brate, 0 );
814 20 : st->last_total_brate = -1;
815 20 : move32();
816 20 : SetModeIndex_fx( st, st->last_total_brate, EVS_MONO, 0, *shift );
817 20 : st->last_total_brate = last_total_brate;
818 20 : move32();
819 : }
820 : ELSE
821 : {
822 240 : SetModeIndex_fx( st, st->last_total_brate, EVS_MONO, 0, *shift );
823 240 : st->sr_core = getCoreSamplerateMode2( st->element_mode, st->total_brate, st->bwidth, st->flag_ACELP16k, st->rf_mode, 0 );
824 240 : move32();
825 240 : Mpy_32_16_ss( st->sr_core, 5243, &L_tmp, &lsb ); /* 5243 is 1/50 in Q18. (0+18-15=3) */
826 240 : st->L_frame = extract_l( L_shr( L_tmp, 3 ) ); /* Q0 */
827 240 : move16();
828 240 : st->encoderLookahead_enc = NS2SA_FX2( st->sr_core, ACELP_LOOK_NS );
829 240 : move16();
830 240 : st->encoderPastSamples_enc = shr( imult1616( st->L_frame, 9 ), 4 );
831 240 : move16();
832 240 : assert( st->L_frame == st->sr_core / 50 );
833 :
834 240 : IF( EQ_32( st->sr_core, INT_FS_12k8 ) )
835 : {
836 240 : st->preemph_fac = PREEMPH_FAC;
837 240 : move16();
838 240 : st->gamma = GAMMA1;
839 240 : move16();
840 : }
841 : ELSE
842 : {
843 0 : st->preemph_fac = PREEMPH_FAC_16k;
844 0 : move16();
845 0 : st->gamma = GAMMA16k;
846 0 : move16();
847 : }
848 :
849 240 : st->igf = getIgfPresent_fx( st->element_mode, st->total_brate, st->bwidth, st->rf_mode );
850 : }
851 :
852 260 : st->coder_type = st->coder_type_raw;
853 260 : move16();
854 :
855 260 : IF( st->vad_flag == 0 )
856 : {
857 2 : st->coder_type = INACTIVE;
858 2 : move16();
859 : }
860 258 : ELSE IF( GT_16( ( st->coder_type ), GENERIC ) )
861 : {
862 0 : st->coder_type = GENERIC;
863 0 : move16();
864 : }
865 :
866 260 : st->mdct_sw = MODE1;
867 260 : move16();
868 : }
869 : }
870 :
871 : /*-----------------------------------------------------------------*
872 : * Update of ACELP harmonicity counter (used in ACELP transform codebook @32kbps)
873 : *-----------------------------------------------------------------*/
874 :
875 3100 : test();
876 3100 : test();
877 3100 : test();
878 3100 : test();
879 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 ) )
880 : {
881 0 : st->last_harm_flag_acelp = add( st->last_harm_flag_acelp, 1 );
882 0 : move16();
883 0 : st->last_harm_flag_acelp = s_min( st->last_harm_flag_acelp, 10 );
884 0 : move16();
885 : }
886 : ELSE
887 : {
888 3100 : st->last_harm_flag_acelp = 0;
889 3100 : move16();
890 : }
891 :
892 : /*-----------------------------------------------------------------*
893 : * Update audio frames counter (used for UV decision)
894 : *-----------------------------------------------------------------*/
895 :
896 3100 : IF( EQ_16( st->coder_type, AUDIO ) )
897 : {
898 18 : st->audio_frame_cnt = add( st->audio_frame_cnt, AUDIO_COUNTER_STEP );
899 18 : move16();
900 : }
901 3082 : ELSE IF( st->coder_type != INACTIVE )
902 : {
903 3063 : st->audio_frame_cnt = sub( st->audio_frame_cnt, 1 );
904 3063 : move16();
905 : }
906 :
907 3100 : st->audio_frame_cnt = s_min( st->audio_frame_cnt, AUDIO_COUNTER_MAX );
908 3100 : move16();
909 3100 : st->audio_frame_cnt = s_max( st->audio_frame_cnt, 0 );
910 3100 : move16();
911 :
912 : /*-----------------------------------------------------------------*
913 : * Set formant sharpening flag
914 : *-----------------------------------------------------------------*/
915 :
916 3100 : st->sharpFlag = 0;
917 3100 : move16();
918 3100 : IF( EQ_16( st->coder_type, TRANSITION ) )
919 : {
920 470 : test();
921 470 : test();
922 470 : test();
923 470 : test();
924 470 : test();
925 470 : IF( ( GT_32( st->total_brate, ACELP_48k ) && LT_16( st->bwidth, SWB ) ) || /* Deactivate for core bitrates higher than 48.0 kb/s */
926 : ( 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 */
927 : ( 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 */
928 : {
929 109 : st->sharpFlag = 0;
930 109 : move16();
931 : }
932 : ELSE
933 : {
934 361 : st->sharpFlag = 1;
935 361 : move16();
936 : }
937 : }
938 :
939 3100 : test();
940 3100 : IF( EQ_16( st->coder_type, GENERIC ) || EQ_16( st->coder_type, VOICED ) )
941 : {
942 2541 : test();
943 2541 : test();
944 2541 : test();
945 2541 : test();
946 2541 : test();
947 2541 : IF( *vad_hover_flag ||
948 : ( GT_32( st->total_brate, ACELP_48k ) && LT_16( st->bwidth, SWB ) ) || /* Deactivate for core bitrates higher than 48.0 kb/s */
949 : ( 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 */
950 : && GT_32( st->total_brate, CNA_MAX_BRATE ) ) )
951 : {
952 70 : st->sharpFlag = 0;
953 70 : move16();
954 : }
955 : ELSE
956 : {
957 2471 : st->sharpFlag = 1;
958 2471 : move16();
959 : }
960 : }
961 :
962 : /* channel-aware mode - due to lack of signalling bit, sharpFlag is 1 always in RF mode */
963 3100 : test();
964 3100 : test();
965 3100 : if ( EQ_16( st->rf_mode, 1 ) && ( EQ_16( st->coder_type, VOICED ) || EQ_16( st->coder_type, GENERIC ) ) )
966 : {
967 0 : st->sharpFlag = 1;
968 0 : move16();
969 : }
970 :
971 : /*-----------------------------------------------------------------*
972 : * Set voicing flag for HQ FEC
973 : *-----------------------------------------------------------------*/
974 :
975 3100 : *Voicing_flag = 0;
976 3100 : move16();
977 3100 : test();
978 3100 : test();
979 3100 : if ( st->sp_aud_decision1 == 0 && ( EQ_16( st->coder_type, VOICED ) || EQ_16( st->coder_type, GENERIC ) ) )
980 : {
981 2032 : *Voicing_flag = 1;
982 2032 : move16();
983 : }
984 :
985 : /*---------------------------------------------------------------*
986 : * Preprocessing at other sampling frequency rate (16/25.6/32kHz)
987 : *----------------------------------------------------------------*/
988 :
989 3100 : sr_core_tmp = L_max( INT_FS_16k_FX, st->sr_core ); /* indicates the ACELP sampling rate for MODE2, 16 kHz for MODE1 */
990 3100 : if ( EQ_16( st->codec_mode, MODE1 ) )
991 : {
992 1790 : sr_core_tmp = INT_FS_16k_FX;
993 1790 : move32();
994 : }
995 :
996 3100 : L_frame_tmp = s_max( L_FRAME16k, st->L_frame );
997 3100 : if ( EQ_16( st->codec_mode, MODE1 ) )
998 : {
999 1790 : L_frame_tmp = L_FRAME16k;
1000 1790 : move16();
1001 : }
1002 3100 : L_look = NS2SA_FX2( sr_core_tmp, ACELP_LOOK_NS ); /* lookahead at other sampling rate (16kHz, 25.5kHz, 32kHz) */
1003 :
1004 3100 : new_inp_16k = old_inp_16k + L_INP_MEM; /* pointer to new samples of the input signal in 16kHz core */
1005 3100 : inp_16k = new_inp_16k - L_look; /* pointer to the current frame of input signal in 16kHz core */
1006 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*/
1007 :
1008 : /*---------------------------------------------------------------*
1009 : * Change the sampling frequency to 16/25.6/32 kHz
1010 : *----------------------------------------------------------------*/
1011 :
1012 3100 : test();
1013 3100 : IF( EQ_32( st->input_Fs, sr_core_tmp ) )
1014 : {
1015 : /* no resampling needed, only delay adjustement to account for the FIR resampling delay */
1016 0 : delay = NS2SA_FX2( st->input_Fs, DELAY_FIR_RESAMPL_NS );
1017 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 */
1018 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 */
1019 0 : Copy( signal_in + input_frame - shl( delay, 1 ), st->mem_decim16k_fx, shl( delay, 1 ) ); /* memory still in Q0 */
1020 : }
1021 3100 : ELSE IF( EQ_32( st->input_Fs, 32000 ) || EQ_32( st->input_Fs, 48000 ) )
1022 : {
1023 3100 : modify_Fs_fx( signal_in, input_frame, st->input_Fs, new_inp_16k, sr_core_tmp, st->mem_decim16k_fx, 0 );
1024 : }
1025 : ELSE /* keep memories up-to-date in case of bitrate switching */
1026 : {
1027 : /* no resampling needed, only delay adjustement to account for the FIR resampling delay */
1028 0 : delay = NS2SA_FX2( st->input_Fs, DELAY_FIR_RESAMPL_NS );
1029 0 : Copy( st->mem_decim16k_fx + delay, new_inp_16k, delay );
1030 0 : Copy( signal_in, new_inp_16k + delay, sub( input_frame, delay ) );
1031 0 : Copy( signal_in + sub( input_frame, shl( delay, 1 ) ), st->mem_decim16k_fx, shl( delay, 1 ) );
1032 : }
1033 :
1034 3100 : IF( EQ_32( sr_core_tmp, INT_FS_16k ) )
1035 : {
1036 : /* save input resampled at 16kHz, non-preemhasised */
1037 3100 : Copy( new_inp_16k, new_inp_resamp16k, L_FRAME16k );
1038 : }
1039 0 : ELSE IF( GT_32( sr_core_tmp, INT_FS_16k ) )
1040 : {
1041 : /* reset the buffer, the signal is needed for WB BWEs */
1042 0 : set16_fx( new_inp_resamp16k, 0, L_FRAME16k );
1043 : }
1044 :
1045 : /*------------------------------------------------------------------*
1046 : * Perform fixed preemphasis (16kHz signal) through 1 - g*z^-1
1047 : *-----------------------------------------------------------------*/
1048 :
1049 3100 : test();
1050 3100 : test();
1051 3100 : IF( ( ( st->tcxonly == 0 ) || ( EQ_16( st->codec_mode, MODE1 ) ) ) && GT_32( st->input_Fs, 8000 ) )
1052 : {
1053 3100 : st->mem_preemph_enc = shl_o( new_inp_16k[sub( L_frame_tmp, 1 )], 1, &Overflow );
1054 3100 : move16();
1055 : }
1056 :
1057 3100 : test();
1058 3100 : IF( GT_32( st->input_Fs, 8000 ) && EQ_32( sr_core_tmp, INT_FS_16k ) )
1059 : {
1060 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 );
1061 : }
1062 0 : ELSE IF( GT_32( st->input_Fs, 8000 ) ) /* keep memory up-to-date in case of bit-rate switching */
1063 : {
1064 0 : st->mem_preemph16k_fx = new_inp_16k[L_frame_tmp - 1];
1065 0 : move16();
1066 : }
1067 :
1068 : /*------------------------------------------------------------------*
1069 : * Core-encoder memories scaling
1070 : *-----------------------------------------------------------------*/
1071 :
1072 3100 : test();
1073 3100 : test();
1074 3100 : test();
1075 3100 : test();
1076 3100 : test();
1077 3100 : test();
1078 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 ) ) ||
1079 : ( EQ_16( st->L_frame, L_FRAME16k ) && EQ_16( st->codec_mode, MODE1 ) ) )
1080 : {
1081 2050 : *Q_new = Q_new_16k;
1082 2050 : move16();
1083 : }
1084 : ELSE
1085 : {
1086 1050 : IF( GT_32( st->input_Fs, 8000 ) && EQ_32( sr_core_tmp, 16000 ) )
1087 : {
1088 1050 : Scale_sig( new_inp_16k, L_FRAME16k, sub( *Q_new, Q_new_16k ) );
1089 : }
1090 : }
1091 :
1092 : /* Above computed Q_new is used to scale primary copy exc and memory here by (Q_new, st->prev_Q_new) */
1093 :
1094 3100 : Q_exp = sub( *Q_new, st->prev_Q_new );
1095 3100 : Q_wsp_exp = add( Q_exp, shift_exp );
1096 :
1097 3100 : Scale_mem_enc( Q_exp, old_inp_16k, old_exc, hBWE_TD->old_bwe_exc_fx, &( hLPDmem->mem_w0 ), hLPDmem->mem_syn,
1098 3100 : hLPDmem->mem_syn2, &st->mem_deemp_preQ_fx, hGSCEnc->last_exc_dct_in_fx, hBWE_FD->old_input_lp_fx );
1099 :
1100 : /*-----------------------------------------------------------------*
1101 : * Redo LP analysis at 16kHz if ACELP@16k core was selected
1102 : * update buffers
1103 : *-----------------------------------------------------------------*/
1104 :
1105 3100 : test();
1106 3100 : test();
1107 3100 : test();
1108 3100 : test();
1109 3100 : test();
1110 3100 : test();
1111 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 ) ) )
1112 : {
1113 : /* update signal buffers */
1114 2050 : Copy( new_inp_resamp16k, st->buf_speech_enc + L_FRAME16k, L_FRAME16k );
1115 2050 : Scale_sig( st->buf_speech_enc + L_FRAME16k, L_FRAME16k, 1 );
1116 2050 : Copy( new_inp_16k, st->buf_speech_enc_pe + L_FRAME16k, L_FRAME16k );
1117 2050 : IF( Q_exp != 0 )
1118 : {
1119 383 : Scale_sig( st->buf_speech_enc_pe, st->encoderPastSamples_enc + st->encoderLookahead_enc, Q_exp );
1120 383 : Scale_sig( &( st->mem_wsp_enc ), 1, Q_exp );
1121 : }
1122 :
1123 : /*--------------------------------------------------------------*
1124 : * LPC analysis
1125 : *---------------------------------------------------------------*/
1126 :
1127 2050 : test();
1128 2050 : IF( EQ_16( st->last_L_frame, L_FRAME ) && EQ_16( st->codec_mode, MODE1 ) )
1129 : {
1130 : /* this is just an approximation, but it is sufficient */
1131 0 : Copy( st->lsp_old1_fx, st->lspold_enc_fx, M );
1132 : }
1133 :
1134 : #ifdef REMOVE_EVS_DUPLICATES
1135 2050 : analy_lp_ivas_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 );
1136 : #else
1137 : 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, -1 /*IVAS_CODE !! LowRateFlag*/, *Q_new, Q_r );
1138 : #endif
1139 :
1140 : /*--------------------------------------------------------------*
1141 : * Compute Weighted Input
1142 : *---------------------------------------------------------------*/
1143 :
1144 2050 : IF( EQ_16( st->codec_mode, MODE2 ) )
1145 : {
1146 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 );
1147 :
1148 : /* This need to be harmonized with MODE2 */
1149 1050 : Scale_sig( st->wspeech_enc, L_FRAME16k + L_LOOK_16k, *shift );
1150 : }
1151 : ELSE
1152 : {
1153 1000 : weight_a_subfr_fx( NB_SUBFR16k, A, Aw, GAMMA16k, M );
1154 : }
1155 : }
1156 : ELSE
1157 : {
1158 : /* update signal buffers */
1159 1050 : Copy( new_inp_12k8, st->buf_speech_enc_pe + st->L_frame, L_FRAME );
1160 1050 : Copy( st->buf_speech_enc + L_FRAME32k, st->buf_speech_enc + st->L_frame, L_FRAME );
1161 :
1162 1050 : if ( st->tcxonly == 0 )
1163 : {
1164 1050 : Copy( wsp, st->buf_wspeech_enc + st->L_frame + L_SUBFR, L_FRAME + L_LOOK_12k8 );
1165 : }
1166 1050 : test();
1167 1050 : test();
1168 1050 : IF( EQ_16( st->codec_mode, MODE2 ) && st->tcxonly == 0 && Q_exp != 0 )
1169 : {
1170 43 : Scale_sig( st->buf_speech_enc_pe, st->encoderPastSamples_enc + st->encoderLookahead_enc, Q_exp );
1171 43 : Scale_sig( &( st->mem_wsp_enc ), 1, Q_exp );
1172 : }
1173 : }
1174 :
1175 3100 : excitation_max_test = -32768;
1176 3100 : move16();
1177 995100 : FOR( i = 0; i < L_EXC_MEM; i++ )
1178 : {
1179 992000 : excitation_max_test = s_max( abs_s( old_exc[i] ), excitation_max_test );
1180 : }
1181 :
1182 3100 : test();
1183 3100 : IF( GT_16( excitation_max_test, 8192 ) && *shift == 0 )
1184 : {
1185 64 : excitation_max_test = 1;
1186 64 : move16();
1187 64 : *shift = -1;
1188 64 : move16();
1189 64 : st->old_wsp_shift = -1;
1190 64 : move16();
1191 64 : Scale_sig( old_wsp, L_WSP_MEM + L_FRAME + L_LOOK_12k8, -1 );
1192 : }
1193 : ELSE
1194 : {
1195 3036 : excitation_max_test = 0;
1196 3036 : move16();
1197 : }
1198 :
1199 3100 : test();
1200 3100 : IF( EQ_16( st->codec_mode, MODE2 ) && st->tcxonly == 0 )
1201 : {
1202 1310 : IF( Q_wsp_exp != 0 )
1203 : {
1204 440 : Scale_sig( st->buf_wspeech_enc, st->L_frame + L_SUBFR, Q_wsp_exp );
1205 : }
1206 1310 : IF( EQ_16( excitation_max_test, 1 ) )
1207 : {
1208 16 : Scale_sig( st->buf_wspeech_enc, st->L_frame + L_SUBFR + st->L_frame + st->encoderLookahead_enc, -1 );
1209 : }
1210 : }
1211 :
1212 : /*-----------------------------------------------------------------*
1213 : * ACELP/TCX20/HQ Switching Decision
1214 : *-----------------------------------------------------------------*/
1215 :
1216 : #ifndef FIX_I4_OL_PITCH
1217 3100 : Copy( st->pitch, pitch_orig, 3 );
1218 : #endif
1219 3100 : IF( EQ_16( st->codec_mode, MODE2 ) )
1220 : {
1221 1310 : test();
1222 1310 : test();
1223 1310 : IF( ( st->core_brate != FRAME_NO_DATA && NE_32( st->core_brate, SID_2k40 ) && st->tcxonly == 0 ) )
1224 : {
1225 1310 : core_acelp_tcx20_switching_fx( st, non_staX, pitch_fr, voicing_fr, currFlatness, lsp_mid, stab_fac, *Q_new, *shift );
1226 : }
1227 :
1228 1310 : test();
1229 1310 : IF( EQ_16( st->mdct_sw_enable, MODE2 ) && !st->rf_mode )
1230 : {
1231 1050 : IF( EQ_16( st->core, TCX_20_CORE ) ) /* Switching only possible from TCX_20 frames, not from TCX_10 frames */
1232 : {
1233 : /* Select MDCT Core */
1234 446 : test();
1235 446 : test();
1236 446 : IF( ( EQ_16( st->bwidth, SWB ) || EQ_16( st->bwidth, FB ) ) && EQ_32( st->total_brate, ACELP_24k40 ) )
1237 : {
1238 446 : st->core = mdct_classifier_fx( fft_buff, st, enerBuffer, sub( enerBuffer_exp, 31 ), st->total_brate );
1239 : }
1240 446 : test();
1241 446 : IF( ( EQ_32( st->total_brate, ACELP_16k40 ) ) && ( NE_16( st->bwidth, FB ) ) )
1242 : {
1243 0 : MDCT_selector_fx( st, sp_floor, *Etot, cor_map_sum, enerBuffer, enerBuffer_exp );
1244 : }
1245 : }
1246 : ELSE
1247 : {
1248 604 : MDCT_selector_reset_fx( hTcxEnc );
1249 : }
1250 :
1251 : /* Do the switching that was decided in the MDCT selector */
1252 1050 : test();
1253 1050 : IF( EQ_16( st->core, HQ_CORE ) )
1254 : {
1255 44 : st->codec_mode = MODE1;
1256 44 : move16();
1257 44 : st->mdct_sw = MODE2;
1258 44 : move16();
1259 : }
1260 1006 : ELSE IF( EQ_16( st->last_codec_mode, MODE1 ) && EQ_16( st->last_core, HQ_CORE ) )
1261 : {
1262 0 : Word16 L_frame_old = st->last_L_frame;
1263 0 : move16();
1264 0 : st->last_L_frame = st->L_frame;
1265 0 : move16();
1266 0 : SetModeIndex_fx( st, st->last_total_brate, EVS_MONO, 0, *shift );
1267 0 : st->last_L_frame = L_frame_old;
1268 0 : move16();
1269 : }
1270 : }
1271 :
1272 : /*--------------------------------------------------------------*
1273 : * TCX mode decision
1274 : *---------------------------------------------------------------*/
1275 :
1276 1310 : SetTCXModeInfo_fx( st, &st->transientDetection, &st->hTcxCfg->tcx_curr_overlap_mode );
1277 : }
1278 :
1279 : /*-----------------------------------------------------------------*
1280 : * Updates
1281 : *-----------------------------------------------------------------*/
1282 :
1283 : /* update old weighted speech buffer - for OL pitch analysis */
1284 3100 : Copy( &old_wsp[L_FRAME], st->old_wsp_fx, L_WSP_MEM );
1285 :
1286 : /* update old input signal buffer */
1287 3100 : Copy( &old_inp_12k8[L_FRAME], st->old_inp_12k8_fx, L_INP_MEM );
1288 :
1289 : /* update old input signal @16kHz buffer */
1290 3100 : test();
1291 3100 : IF( GT_32( st->input_Fs, 8000 ) && EQ_32( sr_core_tmp, INT_FS_16k ) )
1292 : {
1293 3100 : Copy( &old_inp_16k[L_frame_tmp], st->old_inp_16k_fx, L_INP_MEM );
1294 : }
1295 0 : ELSE IF( GT_32( st->input_Fs, 8000 ) )
1296 : {
1297 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 );
1298 0 : Scale_sig( st->old_inp_16k_fx, L_INP_MEM, sub( *Q_new, st->Q_old ) );
1299 : }
1300 :
1301 3100 : test();
1302 3100 : test();
1303 3100 : IF( ( EQ_32( sr_core_tmp, INT_FS_16k ) ) && st->tcxonly && EQ_16( st->codec_mode, MODE2 ) )
1304 : {
1305 : /* copy input resampled at 16kHz, non-preemhasised */
1306 0 : Copy( new_inp_resamp16k, new_inp_16k, L_FRAME16k );
1307 : }
1308 :
1309 : /* update of old per-band energy spectrum */
1310 3100 : Copy32( fr_bands + NB_BANDS, hNoiseEst->enrO_fx, NB_BANDS );
1311 :
1312 : /* set the pointer of the current frame for the ACELP core */
1313 3100 : *inp = inp_16k;
1314 3100 : if ( EQ_16( st->L_frame, L_FRAME ) )
1315 : {
1316 1050 : *inp = inp_12k8;
1317 : }
1318 :
1319 : /* Update vAD hangover frame counter in active frames */
1320 3100 : if ( ( NE_16( st->core, HQ_CORE ) && st->tcxonly == 0 ) || st->core == ACELP_CORE )
1321 : {
1322 :
1323 2693 : IF( !( EQ_32( st->core_brate, SID_2k40 ) || st->core_brate == FRAME_NO_DATA ) )
1324 : {
1325 2693 : test();
1326 2693 : IF( st->Opt_DTX_ON != 0 && *vad_hover_flag != 0 )
1327 : {
1328 0 : hTdCngEnc->burst_ho_cnt = add( hTdCngEnc->burst_ho_cnt, 1 );
1329 0 : move16();
1330 0 : hTdCngEnc->burst_ho_cnt = s_min( hTdCngEnc->burst_ho_cnt, HO_HIST_SIZE );
1331 0 : move16();
1332 : }
1333 : ELSE
1334 : {
1335 2693 : IF( hTdCngEnc != NULL && vad_flag_dtx != 0 )
1336 : {
1337 2690 : hTdCngEnc->burst_ho_cnt = 0;
1338 2690 : move16();
1339 : }
1340 : }
1341 : }
1342 : }
1343 :
1344 3100 : return;
1345 : }
|