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" /* Compilation switches */
7 : #include "cnst.h" /* Common constants */
8 : #include "rom_enc.h" /* Encoder static table prototypes */
9 : #include "basop_util.h"
10 : #include "rom_com.h" /* Common constants */
11 : #include "prot_fx.h" /* Function prototypes */
12 : #include "basop_util.h" /* Function prototypes */
13 : #include "prot_fx_enc.h"
14 : #ifdef DEBUGGING
15 : #include "debug.h"
16 : #endif
17 :
18 : /*-------------------------------------------------------------------*
19 : * amr_wb_enc()
20 : *
21 : * AMR-WB encoder
22 : *--------------------------------------------------------------------*/
23 :
24 0 : void amr_wb_enc_fx(
25 : Encoder_State *st, /* i/o: encoder state structure */
26 : const Word16 input_sp[], /* i : input signal Q0*/
27 : Word32 *mem_hp20_in_fx, /* i/o: hp20 filter memory Qx*/
28 : const Word16 n_samples /* i : number of input samples Q0*/
29 : )
30 : {
31 : Word16 i, delay;
32 : Word16 old_inp[L_INP_12k8], *new_inp, *inp; /* buffer of old input signal */
33 : Word16 old_inp_16k[L_INP_12k8 + L_SUBFR], *inp_16k, *new_inp_16k; /* buffer of old input signal @16kHz*/
34 : Word16 old_exc[L_EXC], *exc; /* excitation signal buffer */
35 : Word16 old_wsp[L_WSP], *wsp; /* weighted input signal buffer */
36 : Word16 input_frame; /* frame length at input sampling freq. */
37 : Word32 fr_bands[2 * NB_BANDS]; /* energy in frequency bands */
38 : Word32 lf_E[2 * VOIC_BINS]; /* per bin spectrum energy in lf */
39 : Word32 tmpN[NB_BANDS]; /* temporary noise update */
40 : Word32 tmpE[NB_BANDS], PS[L_FFT / 2]; /* temporary averaged energy of 2 sf. */
41 : Word16 corr_shift; /* correlation shift */
42 : Word16 relE; /* frame relative energy */
43 : Word16 cor_map_sum, sp_div;
44 : Word16 Etot; /* total energy */
45 0 : Word32 ener = 0; /* residual energy from Levinson-Durbin */
46 : Word16 A[NB_SUBFR * ( M + 1 )]; /* A(z) unquantized for the 4 subframes */
47 : Word16 Aw[NB_SUBFR * ( M + 1 )]; /* A(z) unquantized for the 4 subframes */
48 : Word16 vad_hover_flag, noisy_speech_HO, clean_speech_HO, NB_speech_HO;
49 : Word16 epsP_h[M + 1]; /* LP prediction errors */
50 : Word16 epsP_l[M + 1]; /* LP prediction errors */
51 : Word16 isp_new[M]; /* ISPs at the end of the frame */
52 : Word16 isf_new[M]; /* ISFs at the end of the frame */
53 : Word16 isp_tmp[M];
54 : Word16 Aq[NB_SUBFR * ( M + 1 )]; /* A(z) quantized for the 4 subframes */
55 : Word16 syn[L_FRAME]; /* synthesis vector */
56 : Word16 res[L_FRAME]; /* residual signal for FER protection */
57 : Word16 exc2[L_FRAME]; /* enhanced excitation */
58 : Word16 pitch_buf[NB_SUBFR]; /* floating pitch for each subframe */
59 : Word16 dummy_buf[L_FRAME32k]; /* dummy buffer - no usage */
60 : Word16 snr_sum_he;
61 : Word16 allow_cn_step;
62 : Word16 tmps;
63 : Word16 harm_flag; /* Q0 */
64 : Word16 high_lpn_flag;
65 : Word16 localVAD_HE_SAD;
66 : Word16 vad_flag_dtx;
67 : Word16 hf_gain_fx[NB_SUBFR];
68 : Word16 Q_new, Q_exp, Q_r[2];
69 : Word16 excitation_max_test, shift;
70 : Word32 Le_min_scaled;
71 : Word16 Q_sp_div;
72 : Word16 non_staX, Scale_fac[2];
73 : Word16 sp_floor;
74 : Word16 fft_buff[2 * L_FFT];
75 : Word32 q_env[NUM_ENV_CNG];
76 0 : Word16 sid_bw = 0;
77 : Word16 exc3[L_FRAME];
78 : Word32 lp_bckr, hp_bckr, Ltmp;
79 : Word16 tmp, e_tmp;
80 : Word16 old_pitch1;
81 0 : TD_CNG_ENC_HANDLE hTdCngEnc = st->hTdCngEnc;
82 0 : NOISE_EST_HANDLE hNoiseEst = st->hNoiseEst;
83 0 : VAD_HANDLE hVAD = st->hVAD;
84 0 : DTX_ENC_HANDLE hDtxEnc = st->hDtxEnc;
85 0 : LPD_state_HANDLE hLPDmem = st->hLPDmem;
86 0 : GSC_ENC_HANDLE hGSCEnc = st->hGSCEnc;
87 0 : AMRWB_IO_ENC_HANDLE hAmrwb_IO = st->hAmrwb_IO;
88 0 : TD_BWE_ENC_HANDLE hBWE_TD = st->hBWE_TD;
89 0 : FD_BWE_ENC_HANDLE hBWE_FD = st->hBWE_FD;
90 :
91 0 : push_wmops( "amr_wb_enc_fx" );
92 :
93 : /*------------------------------------------------------------------*
94 : * Initialization
95 : *------------------------------------------------------------------*/
96 :
97 0 : st->L_frame = L_FRAME; /* Q0 */
98 0 : move16();
99 0 : st->gamma = GAMMA1; /* Q15 */
100 0 : move16();
101 0 : st->core = AMR_WB_CORE; /* Q0 */
102 0 : move16();
103 0 : st->core_brate = st->total_brate; /* Q0 */
104 0 : move16();
105 0 : st->input_bwidth = st->last_input_bwidth; /* Q0 */
106 0 : move16();
107 0 : st->bwidth = st->last_bwidth; /* Q0 */
108 0 : move16();
109 0 : st->coder_type = GENERIC;
110 0 : move16();
111 0 : input_frame = extract_l( Mpy_32_32_r( st->input_Fs, ONE_BY_FRAMES_PER_SEC_Q31 ) );
112 0 : move16(); /* frame length of the input signal */
113 0 : st->extl = -1;
114 0 : move16();
115 0 : st->encoderPastSamples_enc = ( L_FRAME * 9 ) / 16;
116 0 : move16();
117 0 : st->encoderLookahead_enc = L_LOOK_12k8;
118 0 : move16();
119 0 : st->bpf_off = 0;
120 0 : move16();
121 0 : test();
122 0 : if ( EQ_16( st->last_core, HQ_CORE ) || EQ_16( st->last_codec_mode, MODE2 ) )
123 : {
124 0 : st->bpf_off = 1;
125 0 : move16();
126 : }
127 0 : st->igf = 0;
128 0 : move16();
129 :
130 : /* Updates in case of EVS primary mode -> AMR-WB IO mode switching */
131 0 : IF( NE_16( st->last_core, AMR_WB_CORE ) )
132 : {
133 0 : updt_IO_switch_enc_fx( st, input_frame );
134 : }
135 :
136 : /* Updates in case of HQ -> AMR-WB IO mode switching */
137 0 : Q_new = 0;
138 0 : move16(); /* prevent compiler warning only*/
139 0 : core_switching_pre_enc_fx( st, NULL, NULL, 0, 0 );
140 :
141 0 : set16_fx( hf_gain_fx, 0, NB_SUBFR );
142 0 : set16_fx( old_inp, 0, L_INP_12k8 );
143 0 : exc = old_exc + L_EXC_MEM; /* pointer to excitation signal in the current frame */
144 :
145 0 : Copy( hLPDmem->old_exc, old_exc, L_EXC_MEM ); /* q_lpd_old_exc */
146 :
147 0 : new_inp = old_inp + L_INP_MEM; /* pointer to new samples of the input signal */
148 0 : inp = new_inp - L_LOOK_12k8; /* pointer to current frame of input signal */
149 0 : wsp = old_wsp + L_WSP_MEM; /* pointer to current frame of weighted signal */
150 :
151 0 : Copy( st->old_inp_12k8_fx, old_inp, L_INP_MEM ); /* exp_old_inp_12k8 */
152 0 : Copy( st->old_wsp_fx, old_wsp, L_WSP_MEM ); /* exp(exp_old_wsp) */
153 :
154 0 : new_inp_16k = old_inp_16k + L_INP_MEM; /* pointer to new samples of the input signal in 16kHz core */
155 0 : inp_16k = new_inp_16k - L_LOOK_16k; /* pointer to the current frame of input signal in 16kHz core */
156 0 : Copy( st->old_inp_16k_fx, old_inp_16k, L_INP_MEM ); /* q_old_inp */
157 :
158 : /* in case of switching, reset AMR-WB BWE memories */
159 0 : test();
160 0 : IF( EQ_32( st->total_brate, ACELP_23k85 ) && NE_32( st->last_core_brate, ACELP_23k85 ) )
161 : {
162 0 : hf_cod_init_fx( hAmrwb_IO->mem_hp400_enc_fx, hAmrwb_IO->mem_hf_enc_fx, hAmrwb_IO->mem_syn_hf_enc_fx, hAmrwb_IO->mem_hf2_enc_fx, &hAmrwb_IO->gain_alpha_fx );
163 : }
164 :
165 : /*----------------------------------------------------------------*
166 : * set input samples buffer
167 : *----------------------------------------------------------------*/
168 :
169 : /* get delay to synchronize ACELP and MDCT frame */
170 0 : delay = NS2SA_FX2( st->input_Fs, DELAY_FIR_RESAMPL_NS );
171 :
172 0 : Copy( st->input_fx - delay, st->old_input_signal_fx, input_frame + delay ); /* q_inp */
173 :
174 : /*----------------------------------------------------------------*
175 : * Buffering of input signal
176 : * HP filtering
177 : *----------------------------------------------------------------*/
178 :
179 0 : Copy( input_sp, st->input_fx, n_samples ); /* Q0 */
180 0 : FOR( i = n_samples; i < input_frame; i++ )
181 : {
182 0 : st->input_fx[i] = 0;
183 0 : move16();
184 : }
185 0 : hp20( st->input_fx, 1, input_frame, mem_hp20_in_fx, st->input_Fs );
186 :
187 : /*-----------------------------------------------------------------*
188 : * switching from ACELP@16k core to AMR-WB IO mode
189 : *-----------------------------------------------------------------*/
190 :
191 0 : st->rate_switching_reset = 0;
192 0 : move16();
193 0 : test();
194 0 : test();
195 0 : IF( NE_16( st->last_core, AMR_WB_CORE ) && EQ_16( st->last_L_frame, L_FRAME16k ) && NE_16( st->last_core, HQ_CORE ) )
196 : {
197 : /* in case of switching, do not apply BPF */
198 0 : st->bpf_off = 1;
199 0 : move16();
200 0 : st->rate_switching_reset = lsp_convert_poly_fx( st->lsp_old_fx, L_FRAME, 1 ); /* Q0 */
201 :
202 : /* convert old quantized LSF vector */
203 0 : lsp2lsf_fx( st->lsp_old_fx, st->lsf_old_fx, M, INT_FS_FX );
204 :
205 : /* Reset LPC mem */
206 0 : Copy( GEWB_Ave_fx, st->mem_AR_fx, M ); /* x2.56 */
207 0 : set16_fx( st->mem_MA_fx, 0, M );
208 :
209 : /* update synthesis filter memories */
210 0 : synth_mem_updt2( L_FRAME, st->last_L_frame, hLPDmem->old_exc, hLPDmem->mem_syn_r, hLPDmem->mem_syn1_fx, hLPDmem->mem_syn, ENC );
211 0 : Copy( hLPDmem->mem_syn1_fx, hLPDmem->mem_syn2, M ); /* q_mem_syn */
212 :
213 0 : Copy( hLPDmem->old_exc, old_exc, L_EXC_MEM ); /* q_lpd_old_exc */
214 0 : Copy( hLPDmem->mem_syn2, hLPDmem->mem_syn3, M ); /* q_mem_syn */
215 :
216 : /* lsp -> isp */
217 0 : Copy( stable_ISP_fx, isp_tmp, M );
218 0 : lsp2isp_fx( st->lsp_old_fx, st->lsp_old_fx, isp_tmp, M ); /* Q15 */
219 : }
220 :
221 : /* update buffer of old subframe pitch values */
222 0 : IF( NE_16( st->last_L_frame, L_FRAME ) )
223 : {
224 0 : move16();
225 0 : IF( EQ_16( st->last_L_frame, L_FRAME32k ) )
226 : {
227 : /* (float)12800/(float)32000; */
228 0 : tmp = 13107; /* 0.4 in Q15 */
229 : }
230 0 : ELSE IF( EQ_16( st->last_L_frame, 512 ) )
231 : {
232 : /* (float)12800/(float)25600; */
233 0 : tmp = 16384; /* 0.5 in Q15 */
234 : }
235 : ELSE /* st->last_L_frame == L_FRAME16k */
236 : {
237 : /* (float)12800/(float)16000; */
238 0 : tmp = 26214; /* 0.8 in Q15 */
239 : }
240 :
241 0 : FOR( i = NB_SUBFR16k - NB_SUBFR; i < NB_SUBFR16k; i++ )
242 : {
243 0 : st->old_pitch_buf_fx[i - 1] = mult_r( tmp, st->old_pitch_buf_fx[i] );
244 0 : move16();
245 : }
246 :
247 0 : FOR( i = 2 * NB_SUBFR16k - NB_SUBFR; i < 2 * NB_SUBFR16k; i++ )
248 : {
249 0 : st->old_pitch_buf_fx[i - 2] = mult_r( tmp, st->old_pitch_buf_fx[i] );
250 0 : move16();
251 : }
252 : }
253 :
254 0 : test();
255 0 : if ( EQ_16( st->last_bwidth, NB ) && st->ini_frame != 0 )
256 : {
257 0 : st->rate_switching_reset = 1; /* Q0 */
258 0 : move16();
259 : }
260 :
261 : /*----------------------------------------------------------------*
262 : * Change the sampling frequency to 12.8 kHz
263 : *----------------------------------------------------------------*/
264 0 : modify_Fs_fx( st->input_fx, input_frame, st->input_Fs, new_inp, 12800, st->mem_decim_fx, 0 );
265 :
266 : /* update signal buffer */
267 0 : Copy( new_inp, st->buf_speech_enc + L_FRAME, L_FRAME ); /* Q0 */
268 0 : Scale_sig( st->buf_speech_enc + L_FRAME, L_FRAME, 1 ); /* Q1 */
269 :
270 : /*------------------------------------------------------------------*
271 : * Perform fixed preemphasis through 1 - g*z^-1
272 : *-----------------------------------------------------------------*/
273 :
274 0 : Preemph_scaled( new_inp, &Q_new, &st->mem_preemph_fx, st->Q_max, PREEMPH_FAC, 0, 1, L_Q_MEM, L_FRAME, st->last_coder_type, 1 );
275 :
276 0 : Q_exp = sub( Q_new, st->Q_old );
277 0 : st->Q_old = Q_new;
278 0 : move16();
279 :
280 0 : Le_min_scaled = Scale_mem_pre_proc( st->ini_frame, Q_exp, &Q_new, old_inp, &( st->mem_wsp_fx ), hNoiseEst->enrO_fx, hNoiseEst->bckr_fx, hNoiseEst->ave_enr_fx,
281 0 : hNoiseEst->ave_enr2_fx, hNoiseEst->fr_bands1_fx, hNoiseEst->fr_bands2_fx, st->Bin_E_old_fx );
282 :
283 0 : Q_exp = sub( Q_new, st->prev_Q_new );
284 0 : Scale_mem_enc( Q_exp, old_inp_16k, old_exc, hBWE_TD->old_bwe_exc_fx, &( hLPDmem->mem_w0 ), hLPDmem->mem_syn, hLPDmem->mem_syn2,
285 0 : &st->mem_deemp_preQ_fx, hGSCEnc->last_exc_dct_in_fx, hBWE_FD->old_input_lp_fx );
286 :
287 : /*----------------------------------------------------------------*
288 : * Compute spectrum, find energy per critical frequency band
289 : * Track energy and signal dynamics
290 : * Detect NB spectrum in a 16kHz-sampled input
291 : *----------------------------------------------------------------*/
292 :
293 0 : analy_sp_fx( st->element_mode, inp, Q_new, fr_bands, lf_E, &Etot, st->min_band, st->max_band, Le_min_scaled, Scale_fac, st->Bin_E_fx,
294 0 : st->Bin_E_old_fx, PS, st->lgBin_E_fx, st->band_energies, fft_buff );
295 :
296 0 : noise_est_pre_fx( Etot, st->ini_frame, hNoiseEst, 0, EVS_MONO, EVS_MONO );
297 :
298 : /*----------------------------------------------------------------*
299 : * VAD
300 : *----------------------------------------------------------------*/
301 :
302 0 : st->vad_flag = wb_vad_fx( st, fr_bands, &noisy_speech_HO, &clean_speech_HO, &NB_speech_HO,
303 0 : &snr_sum_he, &localVAD_HE_SAD, &( st->flag_noisy_speech_snr ), Q_new, hVAD, hNoiseEst, st->lp_speech_fx, st->lp_noise_fx ); /* Q0 */
304 0 : move16();
305 :
306 0 : if ( st->vad_flag == 0 )
307 : {
308 0 : st->coder_type = INACTIVE;
309 0 : move16();
310 : }
311 :
312 : /* apply DTX hangover for CNG analysis */
313 0 : vad_flag_dtx = dtx_hangover_addition_fx( st, st->vad_flag, sub( st->lp_speech_fx, st->lp_noise_fx ), 0, &vad_hover_flag, NULL, NULL );
314 :
315 : /*-----------------------------------------------------------------*
316 : * Select SID or FRAME_NO_DATA frame if DTX enabled
317 : *-----------------------------------------------------------------*/
318 :
319 0 : IF( NE_16( st->last_core, AMR_WB_CORE ) )
320 : {
321 0 : st->fd_cng_reset_flag = 1; /* Q0 */
322 0 : move16();
323 : }
324 0 : ELSE IF( s_and( ( st->fd_cng_reset_flag > 0 ), (Word16) ( LT_16( st->fd_cng_reset_flag, 10 ) ) ) )
325 : {
326 0 : st->fd_cng_reset_flag = add( st->fd_cng_reset_flag, 1 ); /* Q0 */
327 0 : move16();
328 : }
329 : ELSE
330 : {
331 0 : st->fd_cng_reset_flag = 0;
332 0 : move16();
333 : }
334 :
335 0 : dtx_fx( st, vad_flag_dtx, inp, Q_new );
336 :
337 : /*----------------------------------------------------------------*
338 : * Noise energy down-ward update and total noise energy estimation
339 : * Long-term energies and relative frame energy updates
340 : * Correlation correction as a function of total noise level
341 : *----------------------------------------------------------------*/
342 :
343 0 : noise_est_down_fx( fr_bands, hNoiseEst->bckr_fx, tmpN, tmpE, st->min_band, st->max_band, &hNoiseEst->totalNoise_fx,
344 : Etot, &hNoiseEst->Etot_last_fx, &hNoiseEst->Etot_v_h2_fx, Q_new, Le_min_scaled );
345 :
346 0 : high_lpn_flag = 0;
347 0 : move16(); /* Q0 flag */
348 0 : long_enr_fx( st, Etot, localVAD_HE_SAD, high_lpn_flag );
349 0 : relE = sub( Etot, st->lp_speech_fx ); /* Q8 */
350 :
351 0 : IF( NE_16( st->bwidth, NB ) )
352 : {
353 0 : lp_bckr = Mean32( hNoiseEst->bckr_fx, 10 ); /* q_bckr */
354 : }
355 : ELSE
356 : {
357 0 : lp_bckr = Mean32( hNoiseEst->bckr_fx + 1, 9 ); /* q_bckr */
358 : }
359 0 : hp_bckr = L_shr( L_add_sat( hNoiseEst->bckr_fx[st->max_band - 1], hNoiseEst->bckr_fx[st->max_band] ), 1 ); /* q_bckr */
360 0 : if ( hp_bckr == 0 ) /* Avoid division by zero. */
361 : {
362 0 : hp_bckr = L_deposit_l( 1 );
363 : }
364 0 : tmp = BASOP_Util_Divide3232_Scale( lp_bckr, hp_bckr, &e_tmp ); /* exp(e_tmp) */
365 0 : Ltmp = L_shr_r( L_deposit_h( tmp ), sub( 15, e_tmp ) );
366 0 : st->bckr_tilt_lt = L_add( Mpy_32_16_r( st->bckr_tilt_lt, 29491 ), Mpy_32_16_r( Ltmp, 3277 ) ); /* Q16 */
367 :
368 0 : corr_shift = correlation_shift_fx( hNoiseEst->totalNoise_fx ); /* Q15 */
369 :
370 : /*----------------------------------------------------------------*
371 : * WB, SWB and FB bandwidth detector
372 : *----------------------------------------------------------------*/
373 :
374 0 : bw_detect_fx( st, st->input_fx, NULL, NULL, NULL, MONO_FORMAT, 0, 0 );
375 :
376 : /* in AMR_WB IO, limit the maximum band-width to WB */
377 0 : if ( GT_16( st->bwidth, WB ) )
378 : {
379 0 : st->bwidth = WB;
380 0 : move16();
381 : }
382 :
383 : /*----------------------------------------------------------------*
384 : * Perform LP analysis
385 : * Compute weighted inp
386 : * Perform open-loop pitch analysis
387 : * Perform 1/4 pitch precision improvement
388 : *----------------------------------------------------------------*/
389 :
390 0 : IF( st->vad_flag == 0 )
391 : {
392 : /* reset the OL pitch tracker memories during inactive frames */
393 0 : pitch_ol_init_fx( &st->old_thres_fx, &st->old_pitch, &st->delta_pit, &st->old_corr_fx );
394 : }
395 0 : old_pitch1 = st->pitch[1]; /* Q0 */
396 0 : move16();
397 0 : analy_lp_AMR_WB_fx( inp, &ener, A, epsP_h, epsP_l, isp_new, st->lsp_old1_fx,
398 0 : isf_new, st->old_pitch_la, st->old_voicing_la, Q_new, Q_r );
399 :
400 0 : find_wsp_fx( A, inp, wsp, &st->mem_wsp_fx, TILT_FAC_FX, L_FRAME, L_LOOK_12k8, L_SUBFR, Aw, GAMMA1, NB_SUBFR );
401 0 : Scale_wsp( wsp, &( st->old_wsp_max ), &shift, &Q_exp, &( st->old_wsp_shift ),
402 0 : st->old_wsp2_fx, st->mem_decim2_fx, st->old_wsp_fx, add( L_FRAME, L_LOOK_12k8 ) );
403 :
404 0 : excitation_max_test = -32768;
405 0 : move16();
406 0 : FOR( i = 0; i < L_EXC_MEM; i++ )
407 : {
408 0 : excitation_max_test = s_max( abs_s( old_exc[i] ), excitation_max_test );
409 : }
410 :
411 0 : test();
412 0 : if ( GT_16( excitation_max_test, 8192 ) && shift == 0 )
413 : {
414 0 : shift = -1;
415 0 : move16();
416 : }
417 0 : pitch_ol_fx( st->pitch, st->voicing_fx, &st->old_pitch, &st->old_corr_fx, corr_shift, &st->old_thres_fx, &st->delta_pit, st->old_wsp2_fx, wsp, st->mem_decim2_fx, relE, 0, st->bwidth, st->Opt_SC_VBR );
418 0 : st->old_pitch_la = st->pitch[2]; /* Q0 */
419 0 : move16();
420 0 : st->old_voicing_la = st->voicing_fx[2]; /* Q15 */
421 0 : move16();
422 :
423 : /* VAD parameters update */
424 0 : vad_param_updt_fx( st, old_pitch1, corr_shift, corr_shift, A, NULL, 1 );
425 :
426 : /*------------------------------------------------------------------*
427 : * Update estimated noise energy and voicing cut-off frequency
428 : *-----------------------------------------------------------------*/
429 :
430 0 : noise_est_fx( st, old_pitch1, tmpN, epsP_h, epsP_l, Etot, relE, corr_shift, tmpE, fr_bands, &cor_map_sum,
431 : NULL, &sp_div, &Q_sp_div, &non_staX, &harm_flag,
432 0 : lf_E, &hNoiseEst->harm_cor_cnt, hNoiseEst->Etot_l_lp_fx, hNoiseEst->Etot_v_h2_fx, &hNoiseEst->bg_cnt, st->lgBin_E_fx, Q_new, Le_min_scaled, &sp_floor, NULL,
433 0 : st->ini_frame );
434 :
435 : /*----------------------------------------------------------------*
436 : * Change the sampling frequency to 16 kHz,
437 : * input@16kHz needed for AMR-WB IO BWE @23.85kbps
438 : *----------------------------------------------------------------*/
439 :
440 0 : test();
441 0 : IF( EQ_32( st->input_Fs, 16000 ) )
442 : {
443 : /* no resampling needed, only delay adjustement to account for the FIR resampling delay */
444 0 : tmps = NS2SA( 16000, DELAY_FIR_RESAMPL_NS );
445 0 : Copy_Scale_sig( &st->mem_decim16k_fx[tmps], new_inp_16k, tmps, -1 ); /* Input in Q0 -> Output in Q-1 to mimic the resampling filter */
446 0 : Copy_Scale_sig( st->input_fx, new_inp_16k + tmps, sub( input_frame, tmps ), -1 ); /* Input in Q0 -> Output in Q-1 to mimic the resampling filter */
447 0 : Copy( st->input_fx + input_frame - shl( tmps, 1 ), st->mem_decim16k_fx, shl( tmps, 1 ) ); /* memory still in Q0 */
448 : }
449 0 : ELSE IF( EQ_32( st->input_Fs, 32000 ) || EQ_32( st->input_Fs, 48000 ) )
450 : {
451 0 : modify_Fs_fx( st->input_fx, input_frame, st->input_Fs, new_inp_16k, 16000, st->mem_decim16k_fx, 0 );
452 : }
453 :
454 : /*----------------------------------------------------------------*
455 : * Encoding of SID frames
456 : *----------------------------------------------------------------*/
457 :
458 0 : test();
459 0 : IF( EQ_32( st->core_brate, SID_1k75 ) || EQ_32( st->core_brate, FRAME_NO_DATA ) )
460 : {
461 : /* encode CNG parameters */
462 0 : CNG_enc_fx( st, Aq, inp, ener, isp_new, isp_new, isf_new, &allow_cn_step, sub( Q_new, 1 ), q_env, &sid_bw );
463 :
464 : /* comfort noise generation */
465 0 : CNG_exc_fx( st->core_brate, L_FRAME, &hTdCngEnc->Enew_fx, &hTdCngEnc->cng_seed, exc, exc2, &hTdCngEnc->lp_ener_fx,
466 : st->last_core_brate, &hDtxEnc->first_CNG, &hTdCngEnc->cng_ener_seed, dummy_buf, allow_cn_step,
467 0 : &hTdCngEnc->last_allow_cn_step, sub( st->prev_Q_new, 1 ), sub( Q_new, 1 ), hTdCngEnc->num_ho, q_env, hTdCngEnc->lp_env_fx,
468 0 : hTdCngEnc->old_env_fx, hTdCngEnc->exc_mem_fx, hTdCngEnc->exc_mem1_fx, &sid_bw, &hTdCngEnc->cng_ener_seed1, exc3, st->Opt_AMR_WB, st->element_mode );
469 :
470 0 : if ( hDtxEnc->first_CNG == 0 )
471 : {
472 0 : hDtxEnc->first_CNG = 1;
473 0 : move16();
474 : }
475 :
476 : /* synthesis */
477 0 : syn_12k8_fx( L_FRAME, Aq, exc2, dummy_buf, hLPDmem->mem_syn3, 1, Q_new, st->Q_syn );
478 :
479 : /* reset the encoder */
480 0 : CNG_reset_enc_fx( st, hLPDmem, pitch_buf, dummy_buf + L_FRAME, 0 );
481 :
482 : /* update st->mem_syn1 for ACELP core switching */
483 0 : Copy( hLPDmem->mem_syn3, hLPDmem->mem_syn1_fx, M ); /* q_mem_syn */
484 :
485 : /* update ACELP core synthesis filter memory */
486 0 : Copy( hLPDmem->mem_syn3, hLPDmem->mem_syn, M ); /* q_mem_syn */
487 :
488 : /* update old synthesis buffer - needed for ACELP internal sampling rate switching */
489 0 : Copy( dummy_buf + L_FRAME - L_SYN_MEM, hLPDmem->mem_syn_r, L_SYN_MEM ); /* st->Q_syn */
490 : /* Update MODE2 core switching memory */
491 0 : E_UTIL_deemph2( sub( Q_new, 1 ), dummy_buf, PREEMPH_FAC, L_FRAME, &( hLPDmem->syn[M] ) );
492 0 : Copy( dummy_buf + L_FRAME - M - 1, hLPDmem->syn, M + 1 ); /* st->Q_syn */
493 : }
494 :
495 : /*----------------------------------------------------------------*
496 : * Encoding of all other frames
497 : *----------------------------------------------------------------*/
498 :
499 : ELSE
500 : {
501 : /*-----------------------------------------------------------------*
502 : * After inactive period, use the most up-to-date ISPs
503 : *-----------------------------------------------------------------*/
504 0 : test();
505 0 : IF( EQ_32( st->last_core_brate, FRAME_NO_DATA ) || EQ_32( st->last_core_brate, SID_1k75 ) )
506 : {
507 0 : Copy( hDtxEnc->lspCNG_fx, st->lsp_old_fx, M ); /* Q15 */
508 0 : E_LPC_isp_isf_conversion( hDtxEnc->lspCNG_fx, st->lsf_old_fx, M );
509 0 : set16_fx( old_exc, 0, L_EXC_MEM );
510 : }
511 :
512 : /*-----------------------------------------------------------------*
513 : * ISF Quantization and interpolation
514 : *-----------------------------------------------------------------*/
515 :
516 0 : isf_enc_amr_wb_fx( st, isf_new, isp_new, Aq );
517 :
518 : /*---------------------------------------------------------------*
519 : * Calculation of LP residual (filtering through A[z] filter)
520 : *---------------------------------------------------------------*/
521 :
522 0 : calc_residu_fx( st, inp, res, Aq );
523 0 : if ( hTdCngEnc != NULL )
524 : {
525 0 : hTdCngEnc->burst_ho_cnt = 0;
526 : }
527 :
528 0 : move16();
529 :
530 : /*------------------------------------------------------------*
531 : * Encode excitation
532 : *------------------------------------------------------------*/
533 :
534 0 : encod_amr_wb_fx( st, inp, Aw, Aq, res, syn, exc, exc2, pitch_buf, hf_gain_fx, inp_16k, shift, Q_new );
535 :
536 : /* update st->mem_syn1 for ACELP core switching */
537 0 : Copy( hLPDmem->mem_syn, hLPDmem->mem_syn1_fx, M ); /* st->q_mem_syn */
538 :
539 : /* update old synthesis buffer - needed for ACELP internal sampling rate switching */
540 0 : Copy( syn + L_FRAME - L_SYN_MEM, hLPDmem->mem_syn_r, L_SYN_MEM ); /* st->Q_syn */
541 :
542 : /* Update MODE2 core switching memory */
543 0 : E_UTIL_deemph2( sub( Q_new, 1 ), syn, PREEMPH_FAC, L_FRAME, &( hLPDmem->syn[M] ) );
544 0 : Copy( syn + L_FRAME - M - 1, hLPDmem->syn, M + 1 ); /* st->Q_syn */
545 :
546 : /*--------------------------------------------------------------------------------------*
547 : * Write VAD information into the bitstream in AMR-WB IO mode
548 : *--------------------------------------------------------------------------------------*/
549 :
550 0 : push_indice( st->hBstr, IND_VAD_FLAG, st->vad_flag, 1 );
551 : }
552 :
553 : /*-----------------------------------------------------------------*
554 : * Updates
555 : *-----------------------------------------------------------------*/
556 :
557 : /* update old weighted speech buffer - for OL pitch analysis */
558 0 : Copy( &old_wsp[L_FRAME], st->old_wsp_fx, L_WSP_MEM ); /* exp(exp_old_wsp) */
559 :
560 : /* update old input signal buffer */
561 0 : Copy( &old_inp[L_FRAME], st->old_inp_12k8_fx, L_INP_MEM ); /* exp(exp_old_inp_12k8) */
562 :
563 : /* update old input signal @16kHz buffer */
564 0 : IF( GT_32( st->input_Fs, 8000 ) )
565 : {
566 0 : Copy( &old_inp_16k[L_FRAME16k], st->old_inp_16k_fx, L_INP_MEM ); /* exp(exp_old_inp_16k) */
567 : }
568 :
569 : /* update of old per-band energy spectrum */
570 0 : Copy32( fr_bands + NB_BANDS, hNoiseEst->enrO_fx, NB_BANDS ); /* Q_new */
571 :
572 : /* update the last bandwidth */
573 0 : st->last_input_bwidth = st->input_bwidth; /* Q0 */
574 0 : st->last_bwidth = st->bwidth; /* Q0 */
575 :
576 : /* update signal buffers */
577 0 : Copy( new_inp, st->buf_speech_enc_pe + L_FRAME, L_FRAME ); /* Q0 */
578 0 : Copy( wsp, st->buf_wspeech_enc + L_FRAME + L_SUBFR, L_FRAME + L_LOOK_12k8 ); /* exp(exp_buf_wspeech_enc) */
579 0 : updt_enc_fx( st, old_exc, pitch_buf, 0, Aq, isf_new, isp_new, dummy_buf );
580 :
581 : /* update main codec parameters */
582 0 : updt_enc_common_fx( st, Etot, Q_new );
583 :
584 : #ifdef DEBUG_MODE_INFO
585 : dbgwrite( &st->codec_mode, sizeof( short ), 1, input_frame, "res/codec" );
586 : dbgwrite( &st->core, sizeof( short ), 1, input_frame, "res/core" );
587 : dbgwrite( &st->extl, sizeof( short ), 1, input_frame, "res/extl" );
588 : dbgwrite( &st->bwidth, sizeof( short ), 1, input_frame, "res/bwidth" );
589 : float ener_flt = st->total_brate / 1000.0f;
590 : dbgwrite( &ener_flt, sizeof( float ), 1, input_frame, "res/total_brate" );
591 : ener_flt = st->core_brate / 1000.0f;
592 : dbgwrite( &ener_flt, sizeof( float ), 1, input_frame, "res/core_brate" );
593 : dbgwrite( &st->coder_type, sizeof( short ), 1, input_frame, "res/coder_type" );
594 : dbgwrite( &st->cng_type, sizeof( short ), 1, input_frame, "res/cng_type" );
595 : dbgwrite( &st->L_frame, sizeof( short ), 1, input_frame, "res/L_frame" );
596 : dbgwrite( &st->vad_flag, sizeof( short ), 1, input_frame, "res/vad_flag" );
597 : #endif
598 :
599 0 : pop_wmops();
600 :
601 0 : return;
602 : }
603 : /*---------------------------------------------------------------------*
604 : * amr_wb_enc_init_fx()
605 : *
606 : * Initialize AMR-WB encoder
607 : *---------------------------------------------------------------------*/
608 :
609 5 : void amr_wb_enc_init_fx(
610 : AMRWB_IO_ENC_HANDLE hAmrwb_IO /* i/o: AMR-WB IO encoder handle */
611 : )
612 : {
613 : Word16 i;
614 :
615 : /* HF (6-7kHz) BWE */
616 5 : hAmrwb_IO->seed2_enc = RANDOM_INITSEED;
617 5 : move16();
618 :
619 25 : FOR( i = 0; i < GAIN_PRED_ORDER; i++ )
620 : {
621 20 : hAmrwb_IO->past_qua_en_fx[i] = -14336; /* Q10 gain quantization memory (used also in AMR-WB IO mode) */
622 20 : move16();
623 : }
624 :
625 5 : hf_cod_init_fx( hAmrwb_IO->mem_hp400_enc_fx, hAmrwb_IO->mem_hf_enc_fx, hAmrwb_IO->mem_syn_hf_enc_fx, hAmrwb_IO->mem_hf2_enc_fx, &hAmrwb_IO->gain_alpha_fx );
626 :
627 5 : return;
628 : }
|