Line data Source code
1 : /******************************************************************************************************
2 :
3 : (C) 2022-2026 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
4 : Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
5 : Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
6 : Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
7 : contributors to this repository. All Rights Reserved.
8 :
9 : This software is protected by copyright law and by international treaties.
10 : The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
11 : Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
12 : Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
13 : Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
14 : contributors to this repository retain full ownership rights in their respective contributions in
15 : the software. This notice grants no license of any kind, including but not limited to patent
16 : license, nor is any license granted by implication, estoppel or otherwise.
17 :
18 : Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
19 : contributions.
20 :
21 : This software is provided "AS IS", without any express or implied warranties. The software is in the
22 : development stage. It is intended exclusively for experts who have experience with such software and
23 : solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
24 : and fitness for a particular purpose are hereby disclaimed and excluded.
25 :
26 : Any dispute, controversy or claim arising under or in relation to providing this software shall be
27 : submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
28 : accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
29 : the United Nations Convention on Contracts on the International Sales of Goods.
30 :
31 : *******************************************************************************************************/
32 :
33 : /*====================================================================================
34 : EVS Codec 3GPP TS26.443 Nov 04, 2021. Version 12.14.0 / 13.10.0 / 14.6.0 / 15.4.0 / 16.3.0
35 : ====================================================================================*/
36 :
37 : #include <stdint.h>
38 : #include "options.h"
39 : #ifdef DEBUGGING
40 : #include "debug.h"
41 : #include "string.h"
42 : #endif
43 : #include <math.h>
44 : #include "cnst.h"
45 : #include "rom_enc.h"
46 : #include "rom_com.h"
47 : #include "prot.h"
48 : #include "ivas_cnst.h"
49 : #include "ivas_prot.h"
50 : #include "ivas_rom_com.h"
51 : #include "wmc_auto.h"
52 :
53 : /*-------------------------------------------------------------------*
54 : * acelp_core_enc()
55 : *
56 : * ACELP core encoder
57 : *--------------------------------------------------------------------*/
58 :
59 350967 : ivas_error acelp_core_enc(
60 : Encoder_State *st, /* i/o: encoder state structure */
61 : const float inp[], /* i : input signal of the current frame */
62 : #ifndef ALIGN_ACELP_CORE
63 : const float ener, /* i : residual energy from Levinson-Durbin*/
64 : #endif
65 : float A[NB_SUBFR16k * ( M + 1 )], /* i : A(z) unquantized for the 4 subframes*/
66 : float Aw[NB_SUBFR16k * ( M + 1 )], /* i : weighted A(z) unquant. for subframes*/
67 : const float epsP[M + 1], /* i : LP prediction errors */
68 : float lsp_new[M], /* i : LSPs at the end of the frame */
69 : float lsp_mid[M], /* i : LSPs in the middle of the frame */
70 : const int16_t vad_hover_flag, /* i : VAD hangover flag */
71 : const int16_t attack_flag, /* i : attack flag (GSC or TC) */
72 : float bwe_exc_extended[], /* i/o: bandwidth extended excitation */
73 : float *voice_factors, /* o : voicing factors */
74 : float old_syn_12k8_16k[], /* o : intermediate ACELP synthesis at 12.8kHz or 16kHz to be used by SWB BWE */
75 : float pitch_buf[NB_SUBFR16k], /* o : floating pitch for each subframe */
76 : int16_t *unbits, /* o : number of unused bits */
77 : STEREO_TD_ENC_DATA_HANDLE hStereoTD, /* i/o: TD stereo encoder handle */
78 : const float tdm_lsfQ_PCh[M] /* i : Q LSFs for primary channel */
79 : )
80 : {
81 : int16_t i, nBits; /* reserved bits */
82 : LPD_state_HANDLE hLPDmem; /* i/o: acelp memories */
83 : float old_exc[L_EXC], *exc; /* excitation signal buffer */
84 : float lsf_new[M]; /* ISFs at the end of the frame */
85 : float Aq[NB_SUBFR16k * ( M + 1 )]; /* A(z) quantized for the 4 subframes */
86 : float syn[L_FRAME16k]; /* synthesis signal buffer */
87 : float res[L_FRAME16k]; /* Residual signal for FER protection */
88 : float exc2[L_FRAME16k]; /* enhanced excitation */
89 : float Es_pred; /* predicited scaled innovation energy */
90 : float tmp_noise; /* NB post-filter long-term noise energy*/
91 : int16_t tc_subfr; /* TC sub-frame indication */
92 : float old_bwe_exc[( PIT16k_MAX + ( L_FRAME16k + 1 ) + L_SUBFR16k ) * 2]; /* excitation buffer */
93 : float *bwe_exc; /* excitation for SWB TBE */
94 : int16_t allow_cn_step;
95 : int32_t int_fs;
96 : float att;
97 : int16_t nb_bits; /* parameters handling */
98 :
99 : /* SC-VBR - back-up memories for LSF quantizer and synthesis filter */
100 : int16_t pstreaklen;
101 : float mem_MA[M], mem_AR[M], Bin_E[L_FFT], Bin_E_old[L_FFT / 2], lsp_new_bck[M], lsp_mid_bck[M], mem_syn_bck[M];
102 : float clip_var, mem_w0_bck, streaklimit;
103 :
104 : float q_env[NUM_ENV_CNG];
105 350967 : int16_t sid_bw = -1;
106 : float exc3[L_FRAME16k];
107 : float syn1[L_FRAME16k];
108 :
109 : float tilt_code_bck;
110 : float gc_threshold_bck;
111 : float clip_var_bck[6];
112 : int16_t next_force_sf_bck;
113 : int16_t uc_two_stage_flag;
114 : int16_t position;
115 : float tmpF;
116 : int16_t ppp_mode, nelp_mode;
117 : int16_t tdm_lp_reuse_flag, tdm_low_rate_mode, tdm_Pitch_reuse_flag;
118 : float *tdm_Pri_pitch_buf;
119 :
120 : /* bitstream */
121 350967 : BSTR_ENC_HANDLE hBstr = st->hBstr;
122 :
123 : ivas_error error;
124 :
125 350967 : error = IVAS_ERR_OK;
126 :
127 350967 : if ( st->element_mode == IVAS_CPE_MDCT && st->core_brate <= SID_2k40 )
128 : {
129 : /* Core was ACELP because of DTX in MDCT-Stereo, but SID encoding for that is done in separate function */
130 8870 : return error;
131 : }
132 :
133 342097 : push_wmops( "acelp_core_enc" );
134 : /*------------------------------------------------------------------*
135 : * Initialization
136 : *------------------------------------------------------------------*/
137 :
138 342097 : hLPDmem = st->hLPDmem;
139 :
140 342097 : Es_pred = 0;
141 :
142 342097 : exc = old_exc + L_EXC_MEM; /* pointer to excitation signal in the current frame */
143 342097 : mvr2r( hLPDmem->old_exc, old_exc, L_EXC_MEM );
144 :
145 342097 : if ( st->hBWE_TD != NULL )
146 : {
147 341909 : bwe_exc = old_bwe_exc + PIT16k_MAX * 2; /* pointer to BWE excitation signal in the current frame */
148 341909 : mvr2r( st->hBWE_TD->old_bwe_exc, old_bwe_exc, PIT16k_MAX * 2 );
149 : }
150 : else
151 : {
152 188 : bwe_exc = NULL;
153 : }
154 :
155 342097 : st->bpf_off = 0;
156 342097 : if ( st->last_core == HQ_CORE || st->last_codec_mode == MODE2 || st->last_core == TCX_20_CORE || st->last_core == TCX_10_CORE )
157 : {
158 : /* in case of HQ->ACELP switching, do not apply BPF */
159 19231 : st->bpf_off = 1;
160 : /* reset the GSC pre echo energy threshold in case of switching */
161 19231 : if ( st->hGSCEnc != NULL )
162 : {
163 19231 : st->hGSCEnc->Last_frame_ener = (float) MAX_32;
164 : }
165 : }
166 :
167 : /* force safety-net LSFQ in the first frames after CNG segment */
168 342097 : if ( st->last_core_brate <= SID_2k40 )
169 : {
170 16939 : st->Nb_ACELP_frames = 0;
171 : }
172 342097 : if ( st->Nb_ACELP_frames < MAX16B )
173 : {
174 342097 : st->Nb_ACELP_frames++;
175 : }
176 :
177 342097 : if ( st->L_frame == L_FRAME )
178 : {
179 181080 : int_fs = INT_FS_12k8;
180 : }
181 : else
182 : {
183 161017 : int_fs = INT_FS_16k;
184 : }
185 :
186 342097 : tmp_noise = 0;
187 342097 : tc_subfr = -1;
188 342097 : position = -1;
189 :
190 : /* SC-VBR temporary variables */
191 342097 : pstreaklen = 0;
192 342097 : clip_var = 0;
193 342097 : mem_w0_bck = 0;
194 342097 : streaklimit = 0;
195 :
196 : /* channel-aware mode */
197 342097 : reset_rf_indices( st->hRF, st->L_frame, &st->rf_target_bits_write );
198 :
199 : /* VBR modes */
200 342097 : if ( st->Opt_SC_VBR )
201 : {
202 200 : ppp_mode = st->hSC_VBR->ppp_mode;
203 200 : nelp_mode = st->hSC_VBR->nelp_mode;
204 : }
205 : else
206 : {
207 341897 : ppp_mode = 0;
208 341897 : nelp_mode = 0;
209 : }
210 :
211 : /* TD stereo */
212 342097 : if ( st->element_mode == IVAS_CPE_TD && st->idchan == 1 )
213 : {
214 4591 : tdm_lp_reuse_flag = hStereoTD->tdm_lp_reuse_flag;
215 4591 : tdm_low_rate_mode = hStereoTD->tdm_low_rate_mode;
216 4591 : tdm_Pitch_reuse_flag = hStereoTD->tdm_Pitch_reuse_flag;
217 4591 : tdm_Pri_pitch_buf = hStereoTD->tdm_Pri_pitch_buf;
218 : }
219 : else
220 : {
221 337506 : tdm_lp_reuse_flag = 0;
222 337506 : tdm_low_rate_mode = 0;
223 337506 : if ( st->element_mode == IVAS_SCE && st->low_rate_mode )
224 : {
225 10041 : tdm_low_rate_mode = 1;
226 : }
227 337506 : tdm_Pitch_reuse_flag = 0;
228 337506 : tdm_Pri_pitch_buf = NULL;
229 : }
230 :
231 : /*-----------------------------------------------------------------*
232 : * ACELP@12k8 / ACELP@16k switching
233 : *-----------------------------------------------------------------*/
234 :
235 342097 : if ( st->last_L_frame != st->L_frame && ( st->last_core == ACELP_CORE || st->last_core == AMR_WB_CORE ) )
236 : {
237 : /* in case of switching, do not apply BPF (flag employed also in updt_enc()) */
238 2384 : st->bpf_off = 1;
239 :
240 : /* force safety-net LSFQ in the first frames after ACELP@12k8/ACELP@16k switching */
241 2384 : st->Nb_ACELP_frames = 1;
242 :
243 : /* convert old quantized LSP vector */
244 2384 : if ( st->L_frame == L_FRAME )
245 : {
246 790 : st->rate_switching_reset = lsp_convert_poly( st->lsp_old, st->L_frame, 0 );
247 : }
248 : else
249 : {
250 1594 : st->rate_switching_reset = st->rate_switching_reset_16kHz;
251 1594 : mvr2r( st->lsp_old16k, st->lsp_old, M );
252 : }
253 :
254 : /* convert old quantized LSF vector */
255 2384 : lsp2lsf( st->lsp_old, st->lsf_old, M, int_fs );
256 :
257 : /* interpolation of unquantized ISPs */
258 2384 : if ( st->rate_switching_reset )
259 : {
260 : /*extrapolation in case of unstable LSP*/
261 0 : int_lsp4( st->L_frame, lsp_mid, lsp_mid, lsp_new, A, M, 0 );
262 : }
263 : else
264 : {
265 2384 : int_lsp4( st->L_frame, st->lsp_old, lsp_mid, lsp_new, A, M, 0 );
266 : }
267 :
268 : /* Reset LPC mem */
269 2384 : mvr2r( GEWB_Ave, st->mem_AR, M );
270 2384 : set_zero( st->mem_MA, M );
271 :
272 : /* update synthesis filter memories */
273 2384 : synth_mem_updt2( st->L_frame, st->last_L_frame, hLPDmem->old_exc, hLPDmem->mem_syn_r, hLPDmem->mem_syn2, hLPDmem->mem_syn, ENC );
274 2384 : mvr2r( hLPDmem->old_exc, old_exc, L_EXC_MEM );
275 2384 : mvr2r( hLPDmem->mem_syn2, hLPDmem->mem_syn1, M );
276 2384 : mvr2r( hLPDmem->mem_syn2, hLPDmem->mem_syn3, M );
277 :
278 : /* update Aw[] coefficients */
279 2384 : weight_a_subfr( st->L_frame / L_SUBFR, A, Aw, st->gamma, M );
280 : }
281 :
282 342097 : if ( st->last_bwidth == NB && st->bwidth != NB && st->ini_frame != 0 )
283 : {
284 0 : st->rate_switching_reset = 1;
285 : }
286 :
287 : /*----------------------------------------------------------------*
288 : * Encoding of CNG frames
289 : *----------------------------------------------------------------*/
290 :
291 342097 : if ( st->core_brate == SID_2k40 || st->core_brate == FRAME_NO_DATA )
292 : {
293 16395 : if ( st->cng_type == LP_CNG )
294 : {
295 : /* Run CNG post parameter update */
296 2548 : cng_params_postupd( st->hTdCngEnc->ho_circ_ptr, &st->hTdCngEnc->cng_buf_cnt, st->hTdCngEnc->cng_exc2_buf, st->hTdCngEnc->cng_brate_buf, st->hTdCngEnc->ho_env_circ, st->element_mode, st->hFdCngEnc->hFdCngCom->CngBandwidth );
297 :
298 : /* encode CNG parameters */
299 : #ifdef ALIGN_ACELP_CORE
300 2548 : CNG_enc( st, Aq, inp, lsp_mid, lsp_new, lsf_new, &allow_cn_step, q_env, &sid_bw );
301 : #else
302 : CNG_enc( st, Aq, inp, ener, lsp_mid, lsp_new, lsf_new, &allow_cn_step, q_env, &sid_bw );
303 : #endif
304 :
305 : /* comfort noise generation */
306 2548 : CNG_exc( st->core_brate, st->L_frame, &st->hTdCngEnc->Enew, &st->hTdCngEnc->cng_seed, exc, exc2, &st->hTdCngEnc->lp_ener, st->last_core_brate, &st->hDtxEnc->first_CNG, &st->hTdCngEnc->cng_ener_seed, bwe_exc, allow_cn_step, &st->hTdCngEnc->last_allow_cn_step, st->hTdCngEnc->num_ho, q_env, st->hTdCngEnc->lp_env, st->hTdCngEnc->old_env, st->hTdCngEnc->exc_mem, st->hTdCngEnc->exc_mem1, &sid_bw, &st->hTdCngEnc->cng_ener_seed1, exc3, st->Opt_AMR_WB, EVS_MONO );
307 : }
308 : else
309 : {
310 13847 : if ( st->core_brate == SID_2k40 && st->element_mode != IVAS_CPE_MDCT )
311 : {
312 2281 : FdCng_encodeSID( st );
313 2281 : st->hDtxEnc->last_CNG_L_frame = st->L_frame;
314 : }
315 :
316 13847 : generate_comfort_noise_enc( st );
317 :
318 13847 : FdCng_exc( st->hFdCngEnc->hFdCngCom, &st->hDtxEnc->CNG_mode, st->L_frame, st->lsp_old, st->hDtxEnc->first_CNG, st->hDtxEnc->lspCNG, Aq, lsp_new, lsf_new, exc, exc2, bwe_exc );
319 13847 : mvr2r( exc2, exc3, st->L_frame );
320 :
321 13847 : if ( st->core_brate == SID_2k40 )
322 : {
323 2281 : if ( st->hTdCngEnc != NULL )
324 : {
325 1547 : tmpF = cng_energy( st->element_mode, st->bwidth, st->hDtxEnc->CNG_mode, st->hTdCngEnc->CNG_att, exc, st->L_frame );
326 1547 : i = (int16_t) ( ( tmpF + 2.0f ) * STEP_SID );
327 1547 : i = min( max( i, 0 ), 127 );
328 1547 : st->hTdCngEnc->old_enr_index = i;
329 : }
330 : }
331 : }
332 :
333 : #ifdef DEBUG_MODE_ACELP
334 : dbgwrite( exc, sizeof( float ), st->L_frame, 1, "res/exc.enc" );
335 : #endif
336 : /* Reset HO counter in the first SID frame */
337 16395 : if ( st->hTdCngEnc != NULL )
338 : {
339 13765 : st->hTdCngEnc->burst_ho_cnt = 0;
340 : }
341 :
342 : /* synthesis at 12.8kHz sampling rate */
343 16395 : syn_12k8( st->L_frame, Aq, exc3, syn1, hLPDmem->mem_syn3, 1 );
344 :
345 : /* reset the encoder */
346 16395 : CNG_reset_enc( st, pitch_buf, voice_factors, 0 );
347 :
348 : /* update st->mem_syn1 for ACELP core switching */
349 16395 : mvr2r( hLPDmem->mem_syn3, hLPDmem->mem_syn1, M );
350 :
351 : /* update ACELP core synthesis filter memory */
352 16395 : mvr2r( hLPDmem->mem_syn3, hLPDmem->mem_syn, M );
353 :
354 : /* update old synthesis buffer - needed for ACELP internal sampling rate switching */
355 16395 : mvr2r( syn1 + st->L_frame - L_SYN_MEM, hLPDmem->mem_syn_r, L_SYN_MEM );
356 :
357 : /* save and delay synthesis to be used by SWB BWE */
358 16395 : if ( st->hBWE_FD != NULL )
359 : {
360 16395 : save_old_syn( st->L_frame, syn1, old_syn_12k8_16k, st->hBWE_FD->old_syn_12k8_16k, st->preemph_fac, &st->hBWE_FD->mem_deemph_old_syn );
361 : }
362 :
363 : /*Update MODE2 core switching memory*/
364 16395 : deemph( syn1, st->preemph_fac, st->L_frame, &( hLPDmem->syn[M] ) );
365 16395 : mvr2r( syn1 + st->L_frame - M - 1, hLPDmem->syn, M + 1 );
366 : }
367 :
368 : /*----------------------------------------------------------------*
369 : * Encoding of all other frames
370 : *----------------------------------------------------------------*/
371 :
372 : else
373 : {
374 : /*-----------------------------------------------------------------*
375 : * Configure ACELP bit allocation
376 : *-----------------------------------------------------------------*/
377 :
378 325702 : nb_bits = 0;
379 325702 : st->acelp_cfg.FEC_mode = 0;
380 325702 : uc_two_stage_flag = 0;
381 :
382 325702 : if ( !nelp_mode && !ppp_mode )
383 : {
384 325587 : config_acelp1( ENC, st->total_brate, st->core_brate, st->core, st->extl, st->extl_brate, st->L_frame, st->GSC_noisy_speech, &( st->acelp_cfg ), hBstr->nb_bits_tot, st->coder_type, st->inactive_coder_type_flag, tc_subfr, 0, &nb_bits, unbits, st->element_mode, &uc_two_stage_flag, tdm_lp_reuse_flag, tdm_low_rate_mode, st->idchan, tdm_Pitch_reuse_flag, st->tdm_LRTD_flag, st->GSC_IVAS_mode );
385 : }
386 :
387 : /*-----------------------------------------------------------------*
388 : * After inactive period, use the most up-to-date ISPs
389 : *-----------------------------------------------------------------*/
390 :
391 325702 : if ( st->hDtxEnc != NULL && ( st->last_core_brate == FRAME_NO_DATA || st->last_core_brate == SID_2k40 ) )
392 : {
393 1652 : mvr2r( st->hDtxEnc->lspCNG, st->lsp_old, M );
394 1652 : lsp2lsf( st->hDtxEnc->lspCNG, st->lsf_old, M, int_fs );
395 : }
396 :
397 : /*-----------------------------------------------------------------*
398 : * Reset higher ACELP pre-quantizer in case of switching
399 : *-----------------------------------------------------------------*/
400 :
401 325702 : if ( !st->use_acelp_preq )
402 : {
403 284749 : st->mem_deemp_preQ = 0.0f;
404 284749 : st->mem_preemp_preQ = 0.0f;
405 284749 : st->last_code_preq = 0;
406 284749 : st->last_nq_preQ = 0;
407 : }
408 325702 : st->use_acelp_preq = 0;
409 :
410 : /*-----------------------------------------------------------------*
411 : * LSF Quantization
412 : * A[z] calculation
413 : *-----------------------------------------------------------------*/
414 :
415 : /* SC-VBR & channel-aware mode - back-up memories for LSF quantizer and synthesis filter */
416 325702 : lsf_syn_mem_backup( st, &tilt_code_bck, &gc_threshold_bck, clip_var_bck, &next_force_sf_bck, lsp_new, lsp_mid, &clip_var, mem_AR, mem_MA, lsp_new_bck, lsp_mid_bck, Bin_E, Bin_E_old, mem_syn_bck, &mem_w0_bck, &streaklimit, &pstreaklen );
417 :
418 325702 : if ( !tdm_lp_reuse_flag )
419 : {
420 325423 : lsf_enc( st, lsf_new, lsp_new, lsp_mid, Aq, tdm_low_rate_mode, st->GSC_IVAS_mode, tdm_lsfQ_PCh );
421 : }
422 : else
423 : {
424 : const float *pt_interp_2;
425 : int16_t beta_index;
426 : float lsf_wgts[M];
427 :
428 : /* intra_frame prediction for the LSFs */
429 279 : lsp2lsf( lsp_new, lsf_new, M, 12800 );
430 279 : Unified_weighting( &st->Bin_E[L_FFT / 2], lsf_new, lsf_wgts, st->bwidth == NB, st->coder_type == UNVOICED, st->sr_core, M );
431 279 : tdm_SCh_lsf_reuse( ENC, st->element_brate, lsf_new, lsp_new, tdm_lsfQ_PCh, lsf_wgts, &beta_index );
432 279 : push_indice( hBstr, IND_IC_LSF_PRED, beta_index, TDM_IC_LSF_PRED_BITS );
433 :
434 279 : pt_interp_2 = interpol_frac_12k8;
435 279 : if ( tdm_low_rate_mode == 1 && st->coder_type > UNVOICED )
436 : {
437 0 : pt_interp_2 = interpol_frac2;
438 : }
439 :
440 279 : if ( st->active_cnt == 1 )
441 : {
442 0 : mvr2r( lsp_new, st->lsp_old, M );
443 0 : lsp2lsf( lsp_new, st->lsf_old, M, st->sr_core );
444 0 : lsp2lsf( lsp_new, lsf_new, M, st->sr_core );
445 : }
446 :
447 : /* LSP interpolation and conversion of LSPs to A(z) */
448 279 : int_lsp( st->L_frame, st->lsp_old, lsp_new, Aq, M, pt_interp_2, 0 );
449 :
450 : /* Check LSF stability (distance between old LSFs and current LSFs) */
451 279 : st->stab_fac = lsf_stab( lsf_new, st->lsf_old, 0, st->L_frame );
452 : }
453 :
454 325702 : if ( st->last_core == HQ_CORE && st->element_mode > EVS_MONO )
455 : {
456 : /* Prepare ACB memory from last HQ frame */
457 289 : tmpF = hLPDmem->old_exc[0];
458 289 : preemph( hLPDmem->old_exc, st->preemph_fac, st->L_frame, &tmpF );
459 289 : mvr2r( hLPDmem->old_exc + st->L_frame - M, hLPDmem->mem_syn, M );
460 289 : residu( Aq, M, hLPDmem->old_exc, old_exc, st->L_frame );
461 : }
462 :
463 325702 : if ( st->last_core != ACELP_CORE && st->element_mode > EVS_MONO )
464 : {
465 : /* Prepare ACB memory of old_bwe_exc */
466 18737 : if ( st->L_frame == L_FRAME )
467 : {
468 9263 : lerp( old_exc, old_bwe_exc, L_EXC_MEM_DEC * HIBND_ACB_L_FAC, L_EXC_MEM_DEC );
469 : }
470 : else
471 : {
472 9474 : lerp( old_exc, old_bwe_exc, L_EXC_MEM_DEC * 2, L_EXC_MEM_DEC );
473 : }
474 : }
475 :
476 :
477 : /*---------------------------------------------------------------*
478 : * Calculation of LP residual (filtering through A[z] filter)
479 : *---------------------------------------------------------------*/
480 :
481 325702 : calc_residu( inp, res, Aq, st->L_frame );
482 :
483 325702 : calculate_hangover_attenuation_gain( st, &att, vad_hover_flag );
484 325702 : if ( att != 1.0f )
485 : {
486 1546 : v_multc( res, att, res, st->L_frame );
487 : }
488 :
489 : /*-----------------------------------------------------------------*
490 : * Determine TC subframe classification
491 : *-----------------------------------------------------------------*/
492 :
493 325702 : if ( st->coder_type == TRANSITION )
494 : {
495 35274 : tc_classif_enc( st->L_frame, &tc_subfr, &position, attack_flag, st->pitch[0], res );
496 :
497 35274 : config_acelp1( ENC, st->total_brate, st->core_brate, st->core, st->extl, st->extl_brate, st->L_frame, -1, &( st->acelp_cfg ), hBstr->nb_bits_tot, st->coder_type, st->inactive_coder_type_flag, tc_subfr, 1, NULL, unbits, st->element_mode, &uc_two_stage_flag, tdm_lp_reuse_flag, tdm_low_rate_mode, st->idchan, tdm_Pitch_reuse_flag, st->tdm_LRTD_flag, st->GSC_IVAS_mode );
498 : }
499 :
500 : /*---------------------------------------------------------------*
501 : * Calculation of prediction for scaled innovation energy
502 : * (for memory-less gain quantizer)
503 : *---------------------------------------------------------------*/
504 :
505 325702 : if ( nb_bits > 0 )
506 : {
507 279795 : Es_pred_enc( &Es_pred, &i, st->L_frame, L_SUBFR, res, st->voicing, nb_bits, uc_two_stage_flag );
508 279795 : push_indice( hBstr, IND_ES_PRED, i, nb_bits );
509 : }
510 :
511 : /*------------------------------------------------------------*
512 : * Encode excitation according to coding type
513 : *------------------------------------------------------------*/
514 :
515 325702 : if ( tdm_low_rate_mode ) /* tdm stereo low rate mode */
516 : {
517 10104 : if ( st->coder_type <= UNVOICED )
518 : {
519 10084 : tdm_low_rate_enc( st, Aq, res, syn, exc, pitch_buf, voice_factors, bwe_exc, 0 /*attack_flag*/, lsf_new, &tmp_noise );
520 : }
521 : else /* GENERIC */
522 : {
523 20 : encod_gen_2sbfr( st, inp, Aw, Aq, res, syn, exc, exc2, pitch_buf, voice_factors, bwe_exc, tdm_Pitch_reuse_flag, tdm_Pri_pitch_buf );
524 : }
525 : }
526 315598 : else if ( nelp_mode )
527 : {
528 : /* SC-VBR - NELP frames */
529 20 : encod_nelp( st, inp, Aw, Aq, res, syn, &tmp_noise, exc, exc2, pitch_buf, voice_factors, bwe_exc );
530 : }
531 315578 : else if ( st->coder_type == UNVOICED )
532 : {
533 : /* UNVOICED frames (Gauss. excitation) */
534 3493 : encod_unvoiced( st, inp, Aw, Aq, Es_pred, uc_two_stage_flag, res, syn, &tmp_noise, exc, pitch_buf, voice_factors, bwe_exc );
535 : }
536 312085 : else if ( st->coder_type == TRANSITION )
537 : {
538 35274 : encod_tran( st, inp, Aw, Aq, Es_pred, res, syn, exc, exc2, pitch_buf, voice_factors, bwe_exc, tc_subfr, position, unbits );
539 : }
540 276811 : else if ( ppp_mode )
541 : {
542 : /* SC-VBR - PPP frames */
543 95 : if ( ( error = encod_ppp( st, inp, Aw, Aq, res, syn, exc, exc2, pitch_buf, voice_factors, bwe_exc ) ) != IVAS_ERR_OK )
544 : {
545 0 : return error;
546 : }
547 :
548 :
549 95 : if ( st->hSC_VBR->bump_up ) /* PPP failed, bump up */
550 : {
551 : /* restore memories of LSF quantizer and synthesis filter */
552 72 : lsf_syn_mem_restore( st, tilt_code_bck, gc_threshold_bck, clip_var_bck, next_force_sf_bck, lsp_new, lsp_mid, clip_var, mem_AR, mem_MA, lsp_new_bck, lsp_mid_bck, Bin_E, Bin_E_old, mem_syn_bck, mem_w0_bck, streaklimit, pstreaklen );
553 :
554 : /* Configure ACELP bit allocation */
555 72 : config_acelp1( ENC, st->total_brate, st->core_brate, st->core, st->extl, st->extl_brate, st->L_frame, -1, &( st->acelp_cfg ), hBstr->nb_bits_tot, st->coder_type, st->inactive_coder_type_flag, tc_subfr, 0, &nb_bits, unbits, 0, &uc_two_stage_flag, 0, 0, st->idchan, tdm_Pitch_reuse_flag, st->tdm_LRTD_flag, st->GSC_IVAS_mode );
556 :
557 : /* redo LSF quantization */
558 72 : lsf_enc( st, lsf_new, lsp_new, lsp_mid, Aq, tdm_low_rate_mode, 0, NULL );
559 :
560 : /* recalculation of LP residual (filtering through A[z] filter) */
561 72 : calc_residu( inp, res, Aq, st->L_frame );
562 72 : st->hTdCngEnc->burst_ho_cnt = 0;
563 :
564 : /* VOICED frames in SC-VBR */
565 72 : encod_gen_voic( st, inp, Aw, Aq, Es_pred, res, syn, exc, exc2, pitch_buf, voice_factors, bwe_exc, unbits, tdm_Pitch_reuse_flag, tdm_Pri_pitch_buf );
566 : }
567 : }
568 276716 : else if ( st->coder_type == AUDIO || ( st->coder_type == INACTIVE && st->inactive_coder_type_flag ) )
569 : {
570 : /* AUDIO and INACTIVE frames (coded by GSC technology) */
571 30059 : encod_audio( st, inp, Aw, Aq, res, syn, exc, pitch_buf, voice_factors, bwe_exc, attack_flag, lsf_new, &tmp_noise, tdm_Pitch_reuse_flag, tdm_Pri_pitch_buf );
572 : }
573 : else
574 : {
575 : /* GENERIC, VOICED and INACTIVE frames (coded by AVQ technology) */
576 246657 : encod_gen_voic( st, inp, Aw, Aq, Es_pred, res, syn, exc, exc2, pitch_buf, voice_factors, bwe_exc, unbits, tdm_Pitch_reuse_flag, tdm_Pri_pitch_buf );
577 : }
578 :
579 : #ifdef DEBUG_MODE_ACELP
580 : dbgwrite( exc, sizeof( float ), st->L_frame, 1, fname( debug_dir, "exc.enc", st->idchan, st->id_element, ENC ) );
581 : dbgwrite( res, sizeof( float ), st->L_frame, 1, fname( debug_dir, "resid", st->idchan, st->id_element, ENC ) );
582 : #endif
583 :
584 : #ifdef DEBUGGING
585 : /* SNR measuremenet of CELP output */
586 : if ( ppp_mode == 0 )
587 : {
588 : char name[50] = "CELP_output_ch ";
589 :
590 : if ( st->id_element == 0 )
591 : {
592 : name[14] = (char) ( st->idchan + '0' );
593 : }
594 : else
595 : {
596 : char name2[50] = "CELP_output.idX_chX ";
597 : name2[14] = (char) ( st->id_element + '0' );
598 : name2[18] = (char) ( st->idchan + '0' );
599 : strcpy( name, name2 );
600 : }
601 :
602 : if ( st->idchan == 0 )
603 : {
604 : snr_celp( st->L_frame, L_SUBFR, st->gamma, st->preemph_fac, st->vad_flag, st->coder_type, inp, syn, A, 0, name );
605 : }
606 : else if ( st->idchan == 1 )
607 : {
608 : snr_celp( st->L_frame, L_SUBFR, st->gamma, st->preemph_fac, st->vad_flag, st->coder_type, inp, syn, A, 1, name );
609 : }
610 : }
611 : #endif
612 : /* update mem_syn1 for ACELP core switching */
613 325702 : mvr2r( hLPDmem->mem_syn, hLPDmem->mem_syn1, M );
614 :
615 : /* update old synthesis buffer - needed for ACELP internal sampling rate switching */
616 325702 : mvr2r( syn + st->L_frame - L_SYN_MEM, hLPDmem->mem_syn_r, L_SYN_MEM );
617 :
618 : /* save and delay synthesis to be used by SWB BWE */
619 325702 : if ( st->hBWE_FD != NULL )
620 : {
621 325514 : save_old_syn( st->L_frame, syn, old_syn_12k8_16k, st->hBWE_FD->old_syn_12k8_16k, st->preemph_fac, &st->hBWE_FD->mem_deemph_old_syn );
622 : }
623 :
624 : /*Update MODE2 core switching memory*/
625 325702 : mvr2r( syn, syn1, st->L_frame );
626 325702 : deemph( syn1, st->preemph_fac, st->L_frame, &( hLPDmem->syn[M] ) );
627 325702 : mvr2r( syn1 + st->L_frame - M - 1, hLPDmem->syn, M + 1 );
628 :
629 325702 : if ( st->element_mode > EVS_MONO && st->hTcxEnc != NULL )
630 : {
631 314957 : mvr2r( syn1 + st->L_frame / 2, st->hTcxEnc->Txnq, st->L_frame / 2 );
632 : }
633 :
634 : /*--------------------------------------------------------------------------------------*
635 : * Modify the excitation signal when the noise is stationary
636 : *--------------------------------------------------------------------------------------*/
637 :
638 325702 : if ( !( st->idchan == 1 && st->element_mode == IVAS_CPE_TD ) && nelp_mode != 1 && !( st->element_mode == IVAS_SCE && tdm_low_rate_mode ) )
639 : {
640 : /* exc2 buffer is needed only for updating of Aq[] which is needed for core switching */
641 311050 : mvr2r( exc, exc2, st->L_frame );
642 311050 : stat_noise_uv_enc( st, epsP, lsp_new, lsp_mid, Aq, exc2, uc_two_stage_flag );
643 : }
644 :
645 : /*-----------------------------------------------------------------*
646 : * Encode supplementary information for Frame Error Concealment
647 : *-----------------------------------------------------------------*/
648 :
649 325702 : FEC_encode( hBstr, st->acelp_cfg, syn, st->coder_type, st->clas, pitch_buf, res, &st->Last_pulse_pos, st->L_frame, st->total_brate );
650 :
651 325702 : if ( st->hBWE_TD != NULL )
652 : {
653 325514 : if ( st->L_frame == L_FRAME )
654 : {
655 172348 : mvr2r( Aq + 2 * ( M + 1 ), st->hBWE_TD->cur_sub_Aq, ( M + 1 ) );
656 : }
657 : else
658 : {
659 153166 : mvr2r( Aq + 3 * ( M + 1 ), st->hBWE_TD->cur_sub_Aq, ( M + 1 ) );
660 : }
661 : }
662 :
663 :
664 : } /* end of active inp coding */
665 :
666 :
667 : /*-----------------------------------------------------------------*
668 : * Write ACELP unused bits
669 : *-----------------------------------------------------------------*/
670 :
671 342097 : if ( st->core_brate != SID_2k40 && st->core_brate != FRAME_NO_DATA && st->core_brate != PPP_NELP_2k80 )
672 : {
673 325657 : nBits = st->acelp_cfg.ubits;
674 :
675 367218 : while ( nBits > 0 )
676 : {
677 41561 : i = min( nBits, 16 );
678 41561 : push_indice( hBstr, IND_UNUSED, 0, i );
679 41561 : nBits -= i;
680 : #ifdef DEBUGGING
681 : if ( st->idchan == 1 && st->element_mode == IVAS_CPE_TD )
682 : {
683 : printf( "Issue with unused bits at Ln 653 in acelp_core_enc channel 1 at frame %d\n", frame );
684 : }
685 : #endif
686 : }
687 : }
688 :
689 : /*-----------------------------------------------------------------*
690 : * Apply non linearity in case of SWB TBE
691 : *-----------------------------------------------------------------*/
692 :
693 342097 : if ( st->hBWE_TD != NULL )
694 : {
695 341909 : if ( ( st->last_Opt_SC_VBR == 1 && st->Opt_SC_VBR == 0 ) || ( ( st->extl == SWB_TBE || st->extl == WB_TBE || st->extl == FB_TBE ) && st->last_extl != SWB_TBE && st->last_extl != WB_TBE && st->last_extl != FB_TBE ) || ( st->idchan == 1 && st->element_mode == IVAS_CPE_TD && !st->tdm_LRTD_flag ) )
696 : {
697 18419 : st->hBWE_TD->bwe_non_lin_prev_scale = 0.0f;
698 18419 : set_f( st->hBWE_TD->old_bwe_exc_extended, 0.0f, NL_BUFF_OFFSET );
699 : }
700 :
701 341909 : if ( !st->Opt_SC_VBR && ( st->idchan == 0 || st->element_mode != IVAS_CPE_TD || ( st->idchan == 1 && st->element_mode == IVAS_CPE_TD && st->tdm_LRTD_flag ) ) )
702 : {
703 : /* Apply a non linearity to the SHB excitation */
704 341709 : non_linearity( bwe_exc, bwe_exc_extended, st->hBWE_TD->old_bwe_exc_extended, L_FRAME32k, &st->hBWE_TD->bwe_non_lin_prev_scale, st->coder_type, voice_factors, st->L_frame, st->element_mode );
705 : }
706 :
707 341909 : if ( st->core_brate == SID_2k40 || st->core_brate == FRAME_NO_DATA )
708 : {
709 16395 : st->hBWE_TD->bwe_non_lin_prev_scale = 0.0f;
710 : }
711 : }
712 :
713 : /*-----------------------------------------------------------------*
714 : * Updates
715 : *-----------------------------------------------------------------*/
716 :
717 342097 : updt_enc( st, old_exc, pitch_buf, Es_pred, Aq, lsf_new, lsp_new, old_bwe_exc );
718 :
719 342097 : if ( st->hTdCngEnc != NULL && st->Opt_DTX_ON && st->core_brate > SID_2k40 )
720 : {
721 : /* update CNG parameters in active frames */
722 30314 : cng_params_upd( lsp_new, exc, st->L_frame, &st->hTdCngEnc->ho_circ_ptr, st->hTdCngEnc->ho_ener_circ, &st->hTdCngEnc->ho_circ_size, st->hTdCngEnc->ho_lsp_circ, ENC, st->hTdCngEnc->ho_env_circ, &st->hTdCngEnc->cng_buf_cnt, st->hTdCngEnc->cng_exc2_buf, st->hTdCngEnc->cng_brate_buf, st->hDtxEnc->last_active_brate, st->element_mode, st->hFdCngEnc->hFdCngCom->CngBandwidth );
723 :
724 30314 : if ( st->L_frame == L_FRAME )
725 : {
726 : /* store LSPs@16k, potentially to be used in CNG@16k */
727 13825 : mvr2r( st->lsp_old16k, &( st->hTdCngEnc->ho_lsp_circ2[( st->hTdCngEnc->ho_circ_ptr ) * M] ), M );
728 : }
729 :
730 : /* set LSP@16k flag for the first buffer */
731 30314 : st->hTdCngEnc->ho_16k_lsp[st->hTdCngEnc->ho_circ_ptr] = ( st->L_frame == L_FRAME ? 0 : 1 );
732 :
733 : /* efficient DTX hangover control */
734 30314 : if ( st->hTdCngEnc->burst_ho_cnt > 1 )
735 : {
736 944 : dtx_hangover_control( st, lsp_new );
737 : }
738 : }
739 :
740 : /* SC-VBR update of average data rate */
741 342097 : if ( st->vad_flag == 1 )
742 : {
743 : /* reset in case of bitrate switching in EVS */
744 301319 : if ( st->hSC_VBR != NULL )
745 : {
746 6147 : update_average_rate( st->hSC_VBR, st->core_brate );
747 : }
748 : }
749 :
750 342097 : pop_wmops();
751 :
752 342097 : return error;
753 : }
|