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