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