Line data Source code
1 : /******************************************************************************************************
2 :
3 : (C) 2022-2025 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 : #endif
42 : #include "cnst.h"
43 : #include "rom_com.h"
44 : #include "prot.h"
45 : #include "wmc_auto.h"
46 :
47 : /*-------------------------------------------------------------------*
48 : * updt_enc()
49 : *
50 : * Common updates (all frame types)
51 : *-------------------------------------------------------------------*/
52 :
53 173225 : void updt_enc(
54 : Encoder_State *st, /* i/o: state structure */
55 : const float *old_exc, /* i : buffer of excitation */
56 : const float *pitch_buf, /* i : floating pitch for each subframe */
57 : const float Es_pred, /* i : predicited scaled innovation energy */
58 : const float *Aq, /* i : A(z) quantized for all subframes */
59 : const float *lsf_new, /* i : current frame LSF vector */
60 : const float *lsp_new, /* i : current frame LSP vector */
61 : const float *old_bwe_exc /* i : buffer of excitation */
62 : )
63 : {
64 : int16_t i;
65 :
66 : /* update old excitation buffer */
67 173225 : mvr2r( &old_exc[st->L_frame], st->hLPDmem->old_exc, L_EXC_MEM );
68 173225 : if ( !st->Opt_AMR_WB && st->hBWE_TD != NULL )
69 : {
70 173099 : mvr2r( &old_bwe_exc[L_FRAME32k], st->hBWE_TD->old_bwe_exc, PIT16k_MAX * 2 );
71 : }
72 :
73 : /* update old LSP and LSF vector */
74 173225 : mvr2r( lsp_new, st->lsp_old, M );
75 173225 : mvr2r( lsf_new, st->lsf_old, M );
76 :
77 : /* update last coder type */
78 173225 : st->last_coder_type = st->coder_type;
79 173225 : if ( st->coder_type == INACTIVE || ( st->bpf_off == 1 && st->coder_type != AUDIO && st->coder_type != TRANSITION ) )
80 : {
81 20536 : st->last_coder_type = UNVOICED;
82 : }
83 :
84 : /* this ensures that st->last_coder_type is never set to INACTIVE in case of AVQ inactive because the FEC does not distinguish between GSC inactive and AVQ inactive */
85 173225 : if ( st->coder_type == INACTIVE && st->total_brate > ACELP_24k40 )
86 : {
87 3914 : st->last_coder_type = GENERIC;
88 : }
89 :
90 173225 : if ( st->Opt_AMR_WB && st->coder_type == INACTIVE && st->core_brate != SID_1k75 && st->core_brate != FRAME_NO_DATA )
91 : {
92 : /* overwrite previous coding type to help FEC */
93 0 : st->last_coder_type = UNVOICED;
94 : }
95 :
96 : /* AC mode (GSC) - in speech we can consider that the last pitch band reached the max */
97 173225 : if ( st->hGSCEnc != NULL && st->coder_type != AUDIO && st->coder_type != INACTIVE )
98 : {
99 144742 : st->hGSCEnc->mem_last_pit_band = 10 + BAND1k2;
100 144742 : st->hGSCEnc->past_dyn_dec = NOISE_LEVEL_SP0 - 1; /* tends to speech */
101 144742 : st->hGSCEnc->noise_lev = NOISE_LEVEL_SP0 - 1; /* tends to speech */
102 144742 : st->hGSCEnc->mid_dyn = 40.0f * 0.5f + st->hGSCEnc->mid_dyn * 0.5f;
103 : }
104 :
105 : /* convert old LSP vector from 12kHz domain to 16kHz domain (needed in case of ACELP@12k8 <-> ACELP@16kHz switching) */
106 173225 : if ( st->L_frame == L_FRAME )
107 : {
108 91279 : mvr2r( st->lsp_old, st->lsp_old16k, M );
109 :
110 91279 : st->rate_switching_reset_16kHz = lsp_convert_poly( st->lsp_old16k, L_FRAME16k, st->Opt_AMR_WB );
111 : }
112 :
113 : /* update buffer of old subframe pitch values */
114 173225 : if ( st->last_L_frame != st->L_frame )
115 : {
116 1814 : if ( st->L_frame == L_FRAME )
117 : {
118 6270 : for ( i = 0; i < NB_SUBFR; i++ )
119 : {
120 5016 : st->old_pitch_buf[NB_SUBFR + i] = 0.8f * st->old_pitch_buf[NB_SUBFR + i + 1];
121 : }
122 : }
123 : else
124 : {
125 2800 : for ( i = NB_SUBFR; i > 0; i-- )
126 : {
127 2240 : st->old_pitch_buf[NB_SUBFR + i] = 1.25f * st->old_pitch_buf[NB_SUBFR + i - 1];
128 : }
129 560 : st->old_pitch_buf[2 * NB_SUBFR16k - 1] = st->old_pitch_buf[2 * NB_SUBFR16k - 2];
130 : }
131 : }
132 :
133 173225 : mvr2r( &st->old_pitch_buf[st->L_frame / L_SUBFR], st->old_pitch_buf, st->L_frame / L_SUBFR );
134 173225 : mvr2r( pitch_buf, &st->old_pitch_buf[st->L_frame / L_SUBFR], st->L_frame / L_SUBFR );
135 :
136 : /* SC-VBR */
137 173225 : st->last_Opt_SC_VBR = st->Opt_SC_VBR;
138 173225 : if ( st->hSC_VBR != NULL )
139 : {
140 1410 : st->hSC_VBR->last_last_ppp_mode = st->hSC_VBR->last_ppp_mode;
141 1410 : st->hSC_VBR->last_ppp_mode = st->hSC_VBR->ppp_mode;
142 1410 : st->hSC_VBR->last_nelp_mode = st->hSC_VBR->nelp_mode;
143 : }
144 :
145 : /* core switching updates */
146 173225 : mvr2r( &Aq[( st->L_frame / L_SUBFR - 1 ) * ( M + 1 )], st->old_Aq_12_8, M + 1 );
147 173225 : st->old_Es_pred = Es_pred;
148 :
149 173225 : return;
150 : }
151 :
152 : /*-------------------------------------------------------------------*
153 : * updt_IO_switch()
154 : *
155 : * Common updates for AMR-WB IO mode and EVS primary mode switching
156 : *-------------------------------------------------------------------*/
157 :
158 0 : void updt_IO_switch_enc(
159 : Encoder_State *st, /* i/o: state structure */
160 : const int16_t input_frame /* i : input frame length */
161 : )
162 : {
163 : float xsp_tmp[M];
164 :
165 0 : if ( st->last_core == AMR_WB_CORE ) /* switching to EVS primary mode */
166 : {
167 : /* reset onset detection counter */
168 0 : st->tc_cnt = -1;
169 :
170 : /* force safety-net LSFQ in the first frames after the switching */
171 0 : st->Nb_ACELP_frames = 0;
172 :
173 : /* AMR-WB IO mode uses ISF(ISP), but EVS primary mode LSF(LSP) */
174 0 : mvr2r( stable_LSP, xsp_tmp, M );
175 0 : isf2lsf( st->lsf_old, st->lsf_old, xsp_tmp, M, INT_FS_12k8 );
176 0 : mvr2r( stable_LSP, xsp_tmp, M );
177 0 : isp2lsp( st->lsp_old, st->lsp_old, xsp_tmp, M );
178 0 : isp2lsp( st->lsp_old1, st->lsp_old1, xsp_tmp, M );
179 :
180 0 : mvr2r( stable_LSP, xsp_tmp, M );
181 0 : isp2lsp( st->hDtxEnc->lspCNG, st->hDtxEnc->lspCNG, xsp_tmp, M );
182 0 : if ( st->hTdCngEnc->old_enr_index >= 0 )
183 : {
184 0 : st->hTdCngEnc->old_enr_index = min( (int16_t) ( (float) st->hTdCngEnc->old_enr_index / STEP_AMR_WB_SID * STEP_SID ), 127 );
185 : }
186 : /* Perform preemphasis of the old input signal @16kHz */
187 0 : st->mem_preemph16k = 0;
188 0 : preemph( st->old_inp_16k, PREEMPH_FAC_16k, L_INP_MEM, &( st->mem_preemph16k ) );
189 :
190 : /* reset TD BWE buffers */
191 0 : set_f( st->hBWE_TD->old_speech_wb, 0.0f, ( L_LOOK_12k8 + L_SUBFR ) * 5 / 16 );
192 0 : set_f( st->hBWE_TD->old_bwe_exc, 0.0f, PIT16k_MAX * 2 );
193 0 : set_f( st->hBWE_TD->old_bwe_exc_extended, 0.0f, NL_BUFF_OFFSET );
194 0 : InitSWBencBufferStates( st->hBWE_TD, NULL );
195 :
196 0 : st->hBWE_TD->bwe_non_lin_prev_scale = 0.0;
197 0 : set_f( st->hBWE_TD->decim_state1, 0.0f, ( 2 * ALLPASSSECTIONS_STEEP + 1 ) );
198 0 : set_f( st->hBWE_TD->decim_state2, 0.0f, ( 2 * ALLPASSSECTIONS_STEEP + 1 ) );
199 0 : set_f( st->hBWE_FD->old_wtda_swb, 0, L_FRAME16k );
200 0 : set_f( st->hBWE_FD->old_input_wb, 0, NS2SA( 16000, DELAY_FD_BWE_ENC_NS ) );
201 :
202 0 : wb_tbe_extras_reset( st->hBWE_TD->mem_genSHBexc_filt_down_wb2, st->hBWE_TD->mem_genSHBexc_filt_down_wb3 );
203 0 : if ( input_frame >= L_FRAME32k )
204 : {
205 0 : swb_tbe_reset( st->hBWE_TD->mem_csfilt, st->hBWE_TD->mem_genSHBexc_filt_down_shb, st->hBWE_TD->state_lpc_syn, st->hBWE_TD->syn_overlap, st->hBWE_TD->state_syn_shbexc, &( st->hBWE_TD->tbe_demph ), &( st->hBWE_TD->tbe_premph ), st->hBWE_TD->mem_stp_swb, &( st->hBWE_TD->gain_prec_swb ) );
206 : }
207 :
208 0 : if ( input_frame == L_FRAME48k )
209 : {
210 0 : set_f( st->hBWE_TD->fb_state_lpc_syn, 0, LPC_SHB_ORDER );
211 0 : st->hBWE_TD->fb_tbe_demph = 0;
212 0 : fb_tbe_reset_enc( st->hBWE_TD->elliptic_bpf_2_48k_mem, &st->hBWE_TD->prev_fb_energy );
213 : }
214 :
215 : /* reset FD BWE buffers */
216 0 : st->hBWE_FD->prev_mode = NORMAL;
217 :
218 : /* reset the unvoiced/audio signal improvement memories */
219 0 : st->hGSCEnc->seed_tcx = 15687;
220 :
221 0 : st->use_acelp_preq = 0;
222 :
223 0 : set_f( st->hSpMusClas->finc_prev, 0.0f, ATT_NSEG );
224 0 : st->hSpMusClas->lt_finc = 0.0f;
225 0 : st->hSpMusClas->last_strong_attack = 0;
226 0 : set_f( st->hSpMusClas->tod_lt_Bin_E, 0.0f, TOD_NSPEC );
227 0 : set_f( st->hSpMusClas->tod_S_map_lt, 0.0f, TOD_NSPEC );
228 0 : st->hSpMusClas->tod_thr_lt = TOD_THR_MASS;
229 0 : st->hSpMusClas->tod_weight = 0.0f;
230 0 : st->hSpMusClas->tod_S_mass_prev = TOD_THR_MASS;
231 0 : st->hSpMusClas->tod_S_mass_lt = TOD_THR_MASS;
232 : }
233 : else /* switching to AMR-WB IO mode */
234 : {
235 0 : set_f( st->mem_MA, 0, M );
236 :
237 : /* AMR-WB IO mode uses ISF(ISP), but EVS primary mode LSF(LSP) */
238 0 : mvr2r( stable_ISP, xsp_tmp, M );
239 0 : lsf2isf( st->lsf_old, st->lsf_old, xsp_tmp, M, INT_FS_12k8 );
240 0 : mvr2r( stable_ISP, xsp_tmp, M );
241 0 : lsp2isp( st->lsp_old, st->lsp_old, xsp_tmp, M );
242 0 : mvr2r( st->lsp_old, st->lsp_old1, M );
243 0 : lsp2isp( st->lsp_old1, st->lsp_old1, xsp_tmp, M );
244 0 : mvr2r( stable_ISP, xsp_tmp, M );
245 0 : lsp2isp( st->hDtxEnc->lspCNG, st->hDtxEnc->lspCNG, xsp_tmp, M );
246 0 : if ( st->hTdCngEnc->old_enr_index >= 0 )
247 : {
248 0 : st->hTdCngEnc->old_enr_index = min( (int16_t) ( (float) st->hTdCngEnc->old_enr_index / STEP_SID * STEP_AMR_WB_SID ), 63 );
249 : }
250 :
251 : /* gain quantization memory */
252 0 : set_f( st->hAmrwb_IO->past_qua_en, -14.0f, GAIN_PRED_ORDER );
253 :
254 : /* reset VBR signaling */
255 0 : if ( st->Opt_SC_VBR )
256 : {
257 0 : st->hSC_VBR->ppp_mode = 0;
258 0 : st->hSC_VBR->nelp_mode = 0;
259 : }
260 :
261 : /* reset the unvoiced/audio signal improvement memories */
262 0 : st->hGSCEnc->seed_tcx = 15687;
263 : }
264 :
265 0 : if ( st->hTdCngEnc != NULL )
266 : {
267 : /* Force SID in case of AMR-WB IO mode/EVS primary mode switching */
268 0 : st->hDtxEnc->cnt_SID = 0;
269 0 : st->hDtxEnc->cng_cnt = 0;
270 0 : st->hTdCngEnc->ho_hist_size = 0;
271 0 : st->hTdCngEnc->burst_ho_cnt = 0;
272 : }
273 :
274 : /* LP memories */
275 0 : mvr2r( UVWB_Ave, st->mem_AR, M );
276 :
277 : /* FEC - update adaptive LSF mean vector */
278 0 : mvr2r( st->lsf_old, st->lsfoldbfi0, M );
279 0 : mvr2r( st->lsf_old, st->lsfoldbfi1, M );
280 0 : mvr2r( st->lsf_old, st->lsf_adaptive_mean, M );
281 :
282 0 : return;
283 : }
284 :
285 :
286 : /*-------------------------------------------------------------------*
287 : * updt_enc_common()
288 : *
289 : * Common updates for MODE1 and MODE2
290 : *-------------------------------------------------------------------*/
291 :
292 1132914 : void updt_enc_common(
293 : Encoder_State *st /* i/o: encoder state structure */
294 : )
295 : {
296 : /*---------------------------------------------------------------------*
297 : * Updates - main main codec parameters
298 : *---------------------------------------------------------------------*/
299 :
300 1132914 : st->last_sr_core = st->sr_core;
301 1132914 : st->last_codec_mode = st->codec_mode;
302 1132914 : st->last_L_frame = st->L_frame;
303 1132914 : st->last_core = st->core;
304 :
305 1132914 : st->last_total_brate = st->total_brate;
306 1132914 : st->last_bits_frame_nominal = st->bits_frame_nominal;
307 1132914 : st->last_core_brate = st->core_brate;
308 1132914 : st->last_extl = st->extl;
309 1132914 : st->last_input_bwidth = st->input_bwidth;
310 1132914 : st->last_bwidth = st->bwidth;
311 1132914 : st->last_coder_type_raw = st->coder_type_raw;
312 :
313 1132914 : if ( st->core_brate > SID_2k40 && st->hDtxEnc != NULL )
314 : {
315 96833 : st->hDtxEnc->last_active_brate = st->total_brate;
316 : }
317 :
318 1132914 : if ( st->hBWE_TD != NULL )
319 : {
320 418018 : if ( st->core == HQ_CORE )
321 : {
322 : /* in the HQ core, coder_type is not used so it could have been set to anything */
323 8457 : st->hBWE_TD->prev_coder_type = GENERIC;
324 : }
325 : else
326 : {
327 409561 : st->hBWE_TD->prev_coder_type = st->coder_type;
328 : }
329 : }
330 :
331 1132914 : if ( st->Opt_DTX_ON && st->hTdCngEnc != NULL )
332 : {
333 46560 : if ( st->core_brate > SID_2k40 )
334 : {
335 33286 : if ( st->hDtxEnc->first_CNG == 1 )
336 : {
337 17667 : if ( st->hTdCngEnc->act_cnt >= BUF_DEC_RATE )
338 : {
339 669 : st->hTdCngEnc->act_cnt = 0;
340 : }
341 17667 : st->hTdCngEnc->act_cnt++;
342 :
343 17667 : if ( st->hTdCngEnc->act_cnt == BUF_DEC_RATE && st->hTdCngEnc->ho_hist_size > 0 )
344 : {
345 244 : st->hTdCngEnc->ho_hist_size--;
346 : }
347 : }
348 :
349 33286 : if ( st->element_mode == IVAS_SCE || st->element_mode == IVAS_CPE_DFT || st->element_mode == IVAS_CPE_TD || st->hDtxEnc->first_CNG == 1 )
350 : {
351 33286 : if ( ++( st->hTdCngEnc->act_cnt2 ) >= MIN_ACT_CNG_UPD )
352 : {
353 26683 : st->hTdCngEnc->act_cnt2 = MIN_ACT_CNG_UPD;
354 : }
355 :
356 33286 : if ( ( st->element_mode == IVAS_SCE || st->element_mode == IVAS_CPE_DFT || st->element_mode == IVAS_CPE_TD ) && st->hTdCngEnc->act_cnt2 >= MIN_ACT_CNG_UPD )
357 : {
358 26683 : st->hTdCngEnc->CNG_att = 0.0f;
359 :
360 26683 : apply_scale( &st->hTdCngEnc->CNG_att, st->hFdCngEnc->hFdCngCom->CngBandwidth, st->hFdCngEnc->hFdCngCom->CngBitrate, scaleTableStereo, SIZE_SCALE_TABLE_STEREO );
361 : }
362 : }
363 : }
364 :
365 46560 : if ( ( st->core_brate == SID_2k40 || st->core_brate == FRAME_NO_DATA ) && st->hDtxEnc->first_CNG == 0 && st->cng_type == LP_CNG )
366 : {
367 28 : st->hDtxEnc->first_CNG = 1;
368 : }
369 : }
370 :
371 : /*-----------------------------------------------------------------*
372 : * Increase the counter of initialization frames
373 : * Limit the max number of init. frames
374 : *-----------------------------------------------------------------*/
375 :
376 1132914 : if ( st->ini_frame < MAX_FRAME_COUNTER )
377 : {
378 408598 : ( st->ini_frame )++;
379 : }
380 :
381 : /* synchronisation of CNG seeds */
382 1132914 : if ( st->hTdCngEnc != NULL && st->core_brate != FRAME_NO_DATA && st->core_brate != SID_2k40 && st->core != AMR_WB_CORE )
383 : {
384 36386 : own_random( &( st->hTdCngEnc->cng_seed ) );
385 36386 : own_random( &( st->hTdCngEnc->cng_ener_seed ) );
386 : }
387 :
388 : /*---------------------------------------------------------------------*
389 : * Updates - MODE2
390 : *---------------------------------------------------------------------*/
391 :
392 1132914 : if ( st->element_mode == EVS_MONO && st->mdct_sw == MODE2 )
393 : {
394 75 : st->codec_mode = MODE2;
395 :
396 75 : st->sr_core = getCoreSamplerateMode2( EVS_MONO, st->total_brate, st->bwidth, st->flag_ACELP16k, st->rf_mode, 0 );
397 :
398 75 : st->L_frame = (int16_t) ( st->sr_core / FRAMES_PER_SEC );
399 :
400 75 : if ( st->sr_core == INT_FS_12k8 )
401 : {
402 0 : st->preemph_fac = PREEMPH_FAC;
403 0 : st->gamma = GAMMA1;
404 : }
405 : else
406 : {
407 75 : st->preemph_fac = PREEMPH_FAC_16k;
408 75 : st->gamma = GAMMA16k;
409 : }
410 :
411 75 : st->igf = getIgfPresent( EVS_MONO, st->total_brate, st->bwidth, st->rf_mode );
412 : }
413 :
414 : /* update FER clas */
415 1132914 : if ( st->core != AMR_WB_CORE )
416 : {
417 1132914 : st->last_clas = st->clas;
418 : }
419 :
420 : /* Update Core */
421 1132914 : core_encode_update( st );
422 :
423 : /*---------------------------------------------------------------------*
424 : * RF mode updates
425 : *---------------------------------------------------------------------*/
426 :
427 1132914 : if ( st->rf_mode )
428 : {
429 0 : if ( st->hSC_VBR != NULL )
430 : {
431 0 : if ( st->hRF->rf_frame_type == RF_NELP )
432 : {
433 0 : st->hSC_VBR->last_nelp_mode = 1;
434 : }
435 : else
436 : {
437 0 : st->hSC_VBR->last_nelp_mode = 0;
438 : }
439 : }
440 : }
441 :
442 1132914 : st->rf_mode_last = st->rf_mode;
443 :
444 1132914 : if ( st->Opt_RF_ON )
445 : {
446 0 : st->L_frame = L_FRAME;
447 0 : st->rf_mode = 1;
448 : }
449 :
450 : /*---------------------------------------------------------------------*
451 : * Other updates
452 : *---------------------------------------------------------------------*/
453 :
454 1132914 : if ( st->element_mode > EVS_MONO && st->hTcxEnc != NULL )
455 : {
456 1126023 : st->hTcxEnc->tcxltp_norm_corr_mem = st->hTcxEnc->tcxltp_norm_corr_past;
457 : }
458 :
459 1132914 : return;
460 : }
|