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