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 "prot.h"
44 : #include "ivas_prot.h"
45 : #include "rom_com.h"
46 : #include "wmc_auto.h"
47 :
48 : /*-----------------------------------------------------------------------*
49 : * Local function prototypes
50 : *-----------------------------------------------------------------------*/
51 :
52 : static void init_tcx( Encoder_State *st, const int16_t L_frame_old, const int32_t total_brate, const int32_t last_total_brate, const int16_t MCT_flag );
53 : static void init_sig_buffers( Encoder_State *st, const int16_t L_frame_old, const int16_t L_subfr, const int32_t total_brate, const int32_t last_total_brate );
54 : static void init_core_sig_ana( Encoder_State *st );
55 : static void init_acelp( Encoder_State *st, const int16_t L_frame_old, const int32_t total_brate, const int32_t last_total_brate );
56 : static void init_modes( Encoder_State *st, const int32_t last_total_brate );
57 :
58 : /*-----------------------------------------------------------------------*
59 : * init_coder_ace_plus()
60 : *
61 : * Initialization of state variables
62 : *-----------------------------------------------------------------------*/
63 :
64 563476 : void init_coder_ace_plus(
65 : Encoder_State *st, /* i : Encoder state */
66 : const int32_t last_total_brate, /* i : last total bitrate */
67 : const int32_t igf_brate, /* i : IGF configuration bitrate */
68 : const int16_t MCT_flag /* i : hMCT handle allocated (1) or not (0) */
69 : )
70 : {
71 563476 : TCX_ENC_HANDLE hTcxEnc = st->hTcxEnc;
72 : int16_t L_frame_old; /*keep old frame size for switching */
73 : int16_t L_subfr;
74 :
75 : /* Bitrate */
76 563476 : st->tcxonly = getTcxonly( st->element_mode, st->total_brate, MCT_flag, st->is_ism_format );
77 :
78 : /* Core Sampling Rate */
79 563476 : st->sr_core = getCoreSamplerateMode2( st->element_mode, st->total_brate, st->bwidth, st->flag_ACELP16k, st->rf_mode, st->is_ism_format );
80 563476 : st->fscale = sr2fscale( st->sr_core );
81 :
82 : /* Narrowband? */
83 563476 : st->narrowBand = ( st->bwidth == NB ) ? 1 : 0;
84 :
85 : /* Core Framing */
86 563476 : L_frame_old = st->last_L_frame;
87 563476 : st->L_frame = (int16_t) ( st->sr_core / FRAMES_PER_SEC );
88 563476 : st->L_frame_past = -1;
89 :
90 563476 : if ( hTcxEnc != NULL )
91 : {
92 563063 : hTcxEnc->L_frameTCX = (int16_t) ( st->input_Fs / FRAMES_PER_SEC );
93 :
94 563063 : if ( st->ini_frame == 0 )
95 : {
96 244559 : set_zero( hTcxEnc->Txnq, L_FRAME32k / 2 + 64 );
97 244559 : hTcxEnc->acelp_zir = hTcxEnc->Txnq + L_FRAME / 2;
98 244559 : hTcxEnc->tcx_target_bits_fac = 1.0f;
99 : }
100 : }
101 :
102 563476 : if ( ( st->element_mode == EVS_MONO && st->L_frame == L_FRAME16k && st->total_brate <= ACELP_32k ) || ( st->element_mode > EVS_MONO && st->L_frame == L_FRAME16k && st->total_brate <= MAX_ACELP_BRATE ) )
103 : {
104 189843 : st->nb_subfr = NB_SUBFR16k;
105 : }
106 : else
107 : {
108 373633 : st->nb_subfr = NB_SUBFR;
109 : }
110 563476 : L_subfr = st->L_frame / st->nb_subfr;
111 :
112 : /* Core Lookahead */
113 563476 : st->encoderLookahead_enc = NS2SA( st->sr_core, ACELP_LOOK_NS );
114 563476 : st->encoderLookahead_FB = NS2SA( st->input_Fs, ACELP_LOOK_NS );
115 :
116 563476 : if ( st->ini_frame == 0 )
117 : {
118 244972 : st->acelpFramesCount = 0;
119 244972 : st->prevTempFlatness = 1.0f;
120 : }
121 :
122 : /* Initialize TBE */
123 563476 : if ( st->hBWE_TD != NULL )
124 : {
125 277567 : st->hBWE_TD->prev_coder_type = GENERIC;
126 277567 : set_f( st->hBWE_TD->prev_lsf_diff, 0.5f, LPC_SHB_ORDER - 2 );
127 277567 : st->hBWE_TD->prev_tilt_para = 0.0f;
128 277567 : set_zero( st->hBWE_TD->cur_sub_Aq, M + 1 );
129 : }
130 :
131 563476 : st->currEnergyHF = 0;
132 :
133 : /* Initialize LPC analysis/quantization */
134 563476 : if ( st->sr_core <= INT_FS_16k && st->tcxonly == 0 )
135 : {
136 246727 : st->lpcQuantization = 1;
137 : }
138 : else
139 : {
140 316749 : st->lpcQuantization = 0;
141 : }
142 :
143 563476 : st->next_force_safety_net = 0;
144 563476 : if ( ( st->last_L_frame != st->L_frame ) || ( st->last_core == AMR_WB_CORE ) || ( st->last_core == HQ_CORE ) )
145 : {
146 353331 : set_f( st->mem_MA, 0.0f, M );
147 353331 : mvr2r( GEWB_Ave, st->mem_AR, M );
148 : }
149 :
150 : /* Initialize IGF */
151 563476 : if ( st->hIGFEnc != NULL )
152 : {
153 466691 : st->hIGFEnc->infoStopFrequency = -1;
154 : }
155 :
156 563476 : if ( st->igf && st->hIGFEnc != NULL )
157 : {
158 465218 : IGFEncSetMode( st->hIGFEnc, igf_brate, st->bwidth, st->element_mode, st->rf_mode );
159 : }
160 98258 : else if ( st->hIGFEnc != NULL )
161 : {
162 1473 : st->hIGFEnc->infoTotalBitsWritten = 0;
163 1473 : st->hIGFEnc->infoTotalBitsPerFrameWritten = 0;
164 : }
165 :
166 : /* Initialize Core Signal Analysis Module */
167 563476 : init_core_sig_ana( st );
168 :
169 : /* Initialize TCX */
170 563476 : if ( hTcxEnc != NULL )
171 : {
172 563063 : init_tcx( st, L_frame_old, st->total_brate, last_total_brate, MCT_flag );
173 : }
174 :
175 : /* Initialize Signal Buffers */
176 563476 : init_sig_buffers( st, L_frame_old, L_subfr, st->total_brate, last_total_brate );
177 :
178 : /* Initialize ACELP */
179 563476 : init_acelp( st, L_frame_old, st->total_brate, last_total_brate );
180 :
181 563476 : if ( st->ini_frame == 0 )
182 : {
183 244972 : st->tec_tfa = 0;
184 : }
185 :
186 563476 : if ( st->hTECEnc != NULL )
187 : {
188 1809 : resetTecEnc( st->hTECEnc, st->tec_tfa );
189 : }
190 :
191 563476 : if ( st->bwidth == SWB && ( st->total_brate == ACELP_16k40 || st->total_brate == ACELP_24k40 ) && st->element_mode == EVS_MONO )
192 : {
193 919 : st->tec_tfa = 1;
194 : }
195 : else
196 : {
197 562557 : st->tec_tfa = 0;
198 : }
199 :
200 563476 : st->tec_flag = 0;
201 563476 : st->tfa_flag = 0;
202 :
203 :
204 563476 : if ( ( st->total_brate == ACELP_9k60 || st->total_brate == ACELP_16k40 || st->total_brate == ACELP_24k40 ) && st->element_mode == EVS_MONO )
205 : {
206 1159 : st->glr = 1;
207 : }
208 : else
209 : {
210 562317 : st->glr = 0;
211 : }
212 :
213 563476 : st->glr_reset = 0;
214 :
215 : /* Initialize ACELP/TCX Modes */
216 563476 : init_modes( st, last_total_brate );
217 :
218 : /* Adaptive BPF */
219 563476 : set_zero( st->mem_bpf, 2 * L_FILT16k );
220 563476 : set_zero( st->mem_error_bpf, 2 * L_FILT16k );
221 :
222 563476 : if ( st->total_brate >= HQ_48k )
223 : {
224 260329 : st->enablePlcWaveadjust = 1;
225 : }
226 : else
227 : {
228 303147 : st->enablePlcWaveadjust = 0;
229 : }
230 :
231 563476 : if ( st->hPlcExt )
232 : {
233 1809 : init_PLC_enc( st->hPlcExt, st->sr_core );
234 : }
235 :
236 563476 : st->glr_idx[0] = 0;
237 563476 : st->glr_idx[1] = 0;
238 563476 : st->mean_gc[0] = 0.0f;
239 563476 : st->mean_gc[1] = 0.0f;
240 563476 : st->prev_lsf4_mean = 0.0f;
241 563476 : st->last_stab_fac = 0.0f;
242 :
243 563476 : return;
244 : }
245 :
246 : /*-----------------------------------------------------------------------*
247 : * init_tcx()
248 : *
249 : * Initialization of TCX
250 : *-----------------------------------------------------------------------*/
251 :
252 563063 : static void init_tcx(
253 : Encoder_State *st,
254 : const int16_t L_frame_old,
255 : const int32_t total_brate,
256 : const int32_t last_total_brate,
257 : const int16_t MCT_flag )
258 : {
259 563063 : TCX_ENC_HANDLE hTcxEnc = st->hTcxEnc;
260 :
261 : /* Share the memories for 2xTCX10/4xTCX5 and for TCX20 */
262 563063 : hTcxEnc->spectrum[0] = hTcxEnc->spectrum_long;
263 563063 : hTcxEnc->spectrum[1] = hTcxEnc->spectrum_long + N_TCX10_MAX;
264 :
265 563063 : init_tcx_cfg( st->hTcxCfg, total_brate, st->sr_core, st->input_Fs, st->L_frame, st->bwidth, hTcxEnc->L_frameTCX, st->fscale, st->encoderLookahead_enc, st->encoderLookahead_FB, st->preemph_fac, st->tcxonly, st->rf_mode, st->igf, st->hIGFEnc != NULL ? st->hIGFEnc->infoStopFrequency : 0, st->element_mode, st->ini_frame, MCT_flag );
266 :
267 : /* Init TCX target bits correction factor */
268 563063 : hTcxEnc->tcx_target_bits_fac = 1.0f;
269 563063 : hTcxEnc->measuredBwRatio = 1.0f;
270 563063 : hTcxEnc->noiseTiltFactor = 0.5625f;
271 563063 : hTcxEnc->noiseLevelMemory_cnt = 0;
272 :
273 563063 : set_f( hTcxEnc->ltpGainMemory, 0.0f, N_LTP_GAIN_MEMS );
274 563063 : set_s( hTcxEnc->memQuantZeros, 0, L_FRAME_PLUS );
275 :
276 : /* TCX-LTP */
277 563063 : hTcxEnc->tcxltp = getTcxLtp( st->sr_core );
278 :
279 563063 : if ( st->ini_frame == 0 )
280 : {
281 244559 : hTcxEnc->tcxltp_pitch_int_past = st->L_frame;
282 244559 : hTcxEnc->tcxltp_pitch_fr_past = 0;
283 244559 : hTcxEnc->tcxltp_gain_past = 0.f;
284 244559 : hTcxEnc->tcxltp_norm_corr_past = 0.f;
285 244559 : hTcxEnc->tcxltp_norm_corr_mem = 0.f;
286 244559 : hTcxEnc->kernel_switch_corr_past = 0.f;
287 244559 : hTcxEnc->kernel_symmetry_past = 0; /* MDCT_IV & 1 */
288 244559 : hTcxEnc->enc_ste_pre_corr_past = 0;
289 244559 : hTcxEnc->tfm_mem = 0.75f;
290 244559 : hTcxEnc->tcxltp_on_mem = 0;
291 : }
292 318504 : else if ( st->L_frame != L_frame_old && !( ( total_brate >= ACELP_16k40 && total_brate <= ACELP_24k40 ) &&
293 363 : ( total_brate == last_total_brate ) && ( st->last_bwidth == st->bwidth ) ) )
294 : {
295 : int16_t pitres, pitres_old;
296 : float pit, pit_old;
297 :
298 95843 : if ( L_frame_old % 160 == 0 )
299 : {
300 47017 : pitres_old = 6;
301 : }
302 : else
303 : {
304 48826 : pitres_old = 4;
305 : }
306 95843 : pit_old = (float) hTcxEnc->tcxltp_pitch_int_past + (float) hTcxEnc->tcxltp_pitch_fr_past / (float) pitres_old;
307 95843 : if ( st->L_frame % 160 == 0 )
308 : {
309 52559 : pitres = 6;
310 : }
311 : else
312 : {
313 43284 : pitres = 4;
314 : }
315 95843 : pit = pit_old * (float) st->L_frame / (float) L_frame_old;
316 95843 : hTcxEnc->tcxltp_pitch_int_past = (int16_t) pit;
317 95843 : hTcxEnc->tcxltp_pitch_fr_past = (int16_t) ( ( pit - (float) hTcxEnc->tcxltp_pitch_int_past ) * (float) pitres );
318 : }
319 :
320 563063 : hTcxEnc->tcx_lpc_shaped_ari = getTcxLpcShapedAri( total_brate, st->rf_mode, st->element_mode );
321 :
322 563063 : return;
323 : }
324 :
325 : /*-----------------------------------------------------------------------*
326 : * init_sig_buffers()
327 : *
328 : * Initialization of signal buffers
329 : *-----------------------------------------------------------------------*/
330 :
331 563476 : static void init_sig_buffers(
332 : Encoder_State *st,
333 : const int16_t L_frame_old,
334 : const int16_t L_subfr,
335 : const int32_t total_brate,
336 : const int32_t last_total_brate )
337 : {
338 563476 : TCX_ENC_HANDLE hTcxEnc = st->hTcxEnc;
339 :
340 : /* Encoder Past Samples at encoder-sampling-rate */
341 563476 : st->encoderPastSamples_enc = ( st->L_frame * 9 ) / 16;
342 :
343 : /* Initialize Signal Buffers and Pointers at encoder-sampling-rate */
344 563476 : if ( st->ini_frame == 0 )
345 : {
346 244972 : set_zero( st->buf_speech_enc, L_PAST_MAX_32k + L_FRAME32k + L_NEXT_MAX_32k );
347 244972 : set_zero( st->buf_speech_enc_pe, L_PAST_MAX_32k + L_FRAME32k + L_NEXT_MAX_32k );
348 244972 : set_zero( st->buf_wspeech_enc, L_FRAME16k + L_SUBFR + L_FRAME16k + L_NEXT_MAX_16k + 320 );
349 244972 : if ( hTcxEnc != NULL )
350 : {
351 244559 : set_zero( hTcxEnc->buf_speech_ltp, L_PAST_MAX_32k + L_FRAME32k + L_NEXT_MAX_32k );
352 : }
353 : }
354 318504 : else if ( st->L_frame != L_frame_old && !( ( total_brate >= ACELP_16k40 && total_brate <= ACELP_24k40 ) &&
355 363 : ( total_brate == last_total_brate ) && ( st->last_bwidth == st->bwidth ) ) )
356 : {
357 95843 : lerp( st->buf_speech_enc, st->buf_speech_enc, st->L_frame, L_frame_old );
358 :
359 95843 : if ( ( st->last_core != TCX_20_CORE ) && ( st->last_core != TCX_10_CORE ) )
360 : {
361 19360 : mvr2r( st->buf_speech_enc, hTcxEnc->buf_speech_ltp, st->L_frame );
362 : }
363 :
364 95843 : mvr2r( st->old_wsp, st->buf_wspeech_enc + st->L_frame + L_SUBFR - L_WSP_MEM, L_WSP_MEM );
365 :
366 : /*Resamp buffers needed only for ACELP*/
367 95843 : if ( st->L_frame == L_FRAME && !st->tcxonly )
368 : {
369 7992 : mvr2r( st->old_inp_12k8, st->buf_speech_enc_pe + st->L_frame - L_INP_MEM, L_INP_MEM );
370 : }
371 87851 : else if ( st->L_frame == L_FRAME16k && !st->tcxonly )
372 : {
373 13963 : lerp( st->buf_wspeech_enc + st->L_frame + L_SUBFR - L_WSP_MEM, st->buf_wspeech_enc + st->L_frame + L_SUBFR - 310, 310, L_WSP_MEM );
374 13963 : mvr2r( st->old_inp_16k, st->buf_speech_enc_pe + st->L_frame - L_INP_MEM, L_INP_MEM );
375 : }
376 :
377 95843 : st->mem_preemph_enc = st->buf_speech_enc[st->L_frame - 1];
378 95843 : st->mem_wsp_enc = st->buf_wspeech_enc[st->L_frame + L_SUBFR - 1];
379 : }
380 222661 : else if ( !st->tcxonly && last_total_brate > ACELP_32k ) /* coming from TCXonly modes */
381 : {
382 6542 : mvr2r( st->old_wsp, st->buf_wspeech_enc + st->L_frame + L_SUBFR - L_WSP_MEM, L_WSP_MEM );
383 :
384 : /*Resamp buffers needed only for ACELP*/
385 6542 : if ( st->L_frame == L_FRAME16k )
386 : {
387 6477 : lerp( st->buf_wspeech_enc + st->L_frame + L_SUBFR - L_WSP_MEM, st->buf_wspeech_enc + st->L_frame + L_SUBFR - 310, 310, L_WSP_MEM );
388 : }
389 6542 : st->hLPDmem->mem_w0 = 0;
390 6542 : st->mem_wsp_enc = st->buf_wspeech_enc[st->L_frame + L_SUBFR - 1];
391 : }
392 :
393 563476 : st->new_speech_enc = st->buf_speech_enc + st->encoderPastSamples_enc + st->encoderLookahead_enc;
394 563476 : st->new_speech_enc_pe = st->buf_speech_enc_pe + st->encoderPastSamples_enc + st->encoderLookahead_enc;
395 :
396 563476 : if ( hTcxEnc != NULL )
397 : {
398 563063 : hTcxEnc->new_speech_ltp = hTcxEnc->buf_speech_ltp + st->encoderPastSamples_enc + st->encoderLookahead_enc;
399 : }
400 :
401 563476 : if ( st->hTcxEnc != NULL )
402 : {
403 563063 : st->hTcxEnc->new_speech_TCX = st->input_buff + st->input_Fs / FRAMES_PER_SEC; /* note: in EVS st->new_speech_TCX == st->input - 0.9375ms; in IVAS st->new_speech_TCX == st->input */
404 563063 : st->hTcxEnc->speech_TCX = st->hTcxEnc->new_speech_TCX - st->encoderLookahead_FB;
405 : }
406 :
407 563476 : st->speech_enc = st->buf_speech_enc + st->encoderPastSamples_enc;
408 563476 : st->speech_enc_pe = st->buf_speech_enc_pe + st->encoderPastSamples_enc;
409 :
410 563476 : if ( hTcxEnc != NULL )
411 : {
412 563063 : hTcxEnc->speech_ltp = hTcxEnc->buf_speech_ltp + st->encoderPastSamples_enc;
413 : }
414 :
415 563476 : if ( st->element_mode > EVS_MONO )
416 : {
417 561667 : st->wspeech_enc = st->buf_wspeech_enc + st->L_frame + L_SUBFR;
418 : }
419 : else
420 : {
421 1809 : st->wspeech_enc = st->buf_wspeech_enc + st->L_frame + L_subfr;
422 : }
423 :
424 563476 : if ( st->ini_frame == 0 || st->L_frame != L_frame_old || st->last_codec_mode == MODE1 )
425 : {
426 563463 : set_zero( st->buf_synth, OLD_SYNTH_SIZE_ENC + L_FRAME32k );
427 : }
428 563476 : st->synth = st->buf_synth + st->L_frame + L_subfr;
429 :
430 563476 : return;
431 : }
432 :
433 :
434 : /*-----------------------------------------------------------------------*
435 : * init_core_sig_ana()
436 : *
437 : *
438 : *-----------------------------------------------------------------------*/
439 :
440 563476 : static void init_core_sig_ana(
441 : Encoder_State *st )
442 : {
443 : /* Pre-emphasis factor and memory */
444 563476 : if ( st->fscale < ( 16000 * FSCALE_DENOM ) / 12800 )
445 : {
446 61125 : st->preemph_fac = PREEMPH_FAC; /*WB*/
447 : }
448 502351 : else if ( st->fscale < ( 24000 * FSCALE_DENOM ) / 12800 )
449 : {
450 201991 : st->preemph_fac = PREEMPH_FAC_16k; /*WB*/
451 : }
452 : else
453 : {
454 300360 : st->preemph_fac = PREEMPH_FAC_SWB; /*SWB*/
455 : }
456 :
457 563476 : if ( st->sr_core == INT_FS_16k )
458 : {
459 201991 : st->gamma = GAMMA16k;
460 : }
461 : else
462 : {
463 361485 : st->gamma = GAMMA1;
464 : }
465 :
466 563476 : if ( st->narrowBand )
467 : {
468 65 : st->min_band = 1;
469 65 : st->max_band = 16;
470 : }
471 : else
472 : {
473 563411 : st->min_band = 0;
474 563411 : st->max_band = 19;
475 : }
476 :
477 563476 : return;
478 : }
479 :
480 :
481 : /*-----------------------------------------------------------------------*
482 : * init_acelp()
483 : *
484 : *
485 : *-----------------------------------------------------------------------*/
486 :
487 563476 : static void init_acelp(
488 : Encoder_State *st,
489 : const int16_t L_frame_old,
490 : const int32_t total_brate,
491 : const int32_t last_total_brate )
492 : {
493 : int16_t mem_syn_r_size_old;
494 : int16_t mem_syn_r_size_new;
495 :
496 563476 : LPD_state_HANDLE hLPDmem = st->hLPDmem;
497 :
498 : /* Init pitch lag */
499 563476 : if ( st->element_mode == IVAS_CPE_MDCT )
500 : {
501 : /* TCX LTP ana always runs @12.8kHz */
502 285496 : st->pit_res_max = initPitchLagParameters( 12800, &st->pit_min, &st->pit_fr1, &st->pit_fr1b, &st->pit_fr2, &st->pit_max );
503 : }
504 : else
505 : {
506 277980 : st->pit_res_max = initPitchLagParameters( st->sr_core, &st->pit_min, &st->pit_fr1, &st->pit_fr1b, &st->pit_fr2, &st->pit_max );
507 : }
508 :
509 : /* Init LPDmem */
510 563476 : if ( st->ini_frame == 0 )
511 : {
512 244972 : if ( hLPDmem != NULL )
513 : {
514 82603 : set_zero( hLPDmem->syn, 1 + M );
515 82603 : set_zero( hLPDmem->mem_syn_r, L_SYN_MEM );
516 : }
517 :
518 244972 : if ( st->hTcxEnc != NULL )
519 : {
520 244559 : set_zero( st->hTcxEnc->Txnq, L_FRAME32k / 2 + 64 );
521 244559 : st->hTcxEnc->acelp_zir = st->hTcxEnc->Txnq + ( st->L_frame / 2 );
522 : }
523 : }
524 : else /*Rate switching*/
525 : {
526 318504 : if ( st->hTcxEnc != NULL )
527 : {
528 318504 : if ( st->last_core == ACELP_CORE )
529 : {
530 99858 : lerp( st->hTcxEnc->Txnq, st->hTcxEnc->Txnq, st->L_frame / 2, L_frame_old / 2 );
531 : }
532 : else
533 : {
534 218646 : lerp( st->hTcxEnc->Txnq, st->hTcxEnc->Txnq, st->hTcxCfg->tcx_mdct_window_length, st->hTcxCfg->tcx_mdct_window_length_old );
535 : }
536 318504 : st->hTcxEnc->acelp_zir = st->hTcxEnc->Txnq + ( st->L_frame / 2 );
537 : }
538 :
539 : /* Rate switching */
540 318504 : if ( st->last_codec_mode == MODE1 && st->element_mode == EVS_MONO )
541 : {
542 1402 : if ( hLPDmem != NULL )
543 : {
544 1402 : mvr2r( hLPDmem->mem_syn1, hLPDmem->mem_syn2, M );
545 1402 : set_zero( hLPDmem->syn, M );
546 : }
547 :
548 1402 : if ( st->hTcxEnc != NULL )
549 : {
550 1402 : set_zero( st->hTcxEnc->Txnq, L_FRAME32k / 2 + 64 );
551 : }
552 : }
553 :
554 318504 : if ( st->last_core == AMR_WB_CORE )
555 : {
556 14 : st->next_force_safety_net = 1;
557 14 : st->last_core = ACELP_CORE;
558 : }
559 :
560 318504 : if ( st->last_codec_mode == MODE1 && st->last_core == HQ_CORE )
561 : {
562 : /*Reset of ACELP memories*/
563 74005 : st->next_force_safety_net = 1;
564 74005 : st->rate_switching_reset = 1;
565 74005 : if ( hLPDmem != NULL )
566 : {
567 73859 : hLPDmem->tilt_code = TILT_CODE;
568 73859 : set_zero( hLPDmem->old_exc, L_EXC_MEM );
569 73859 : set_zero( hLPDmem->syn, 1 + M );
570 73859 : hLPDmem->mem_w0 = 0.0f;
571 73859 : set_zero( hLPDmem->mem_syn, M );
572 73859 : set_zero( hLPDmem->mem_syn2, M );
573 : }
574 :
575 : /* unquantized LPC*/
576 74005 : if ( !( ( total_brate >= ACELP_16k40 && total_brate <= ACELP_24k40 ) && total_brate == last_total_brate && st->last_bwidth == st->bwidth ) )
577 : {
578 20212 : mvr2r( st->lsp_old1, st->lspold_enc, M ); /*lsp old @12.8kHz*/
579 20212 : if ( st->L_frame == L_FRAME16k )
580 : {
581 19818 : lsp_convert_poly( st->lspold_enc, st->L_frame, 0 );
582 : }
583 : }
584 74005 : mvr2r( st->lspold_enc, st->lsp_old, M ); /*used unquantized values for mid-LSF Q*/
585 74005 : lsp2lsf( st->lsp_old, st->lsf_old, M, st->sr_core );
586 :
587 74005 : st->last_core = TCX_20_CORE;
588 :
589 74005 : st->hTcxCfg->last_aldo = 1; /*It was previously ALDO*/
590 74005 : st->hTcxCfg->tcx_curr_overlap_mode = ALDO_WINDOW;
591 :
592 : /*ALDO overlap windowed past: also used in MODE1 but for other MDCT-FB*/
593 74005 : set_f( st->hTcxEnc->old_out, 0, st->L_frame );
594 : }
595 : else
596 : {
597 244499 : if ( st->L_frame != L_frame_old && st->L_frame <= L_FRAME16k && L_frame_old <= L_FRAME16k ) /* Rate switching between 12.8 and 16 kHz*/
598 11323 : {
599 : float tmp, A[M + 1], Ap[M + 1], tmp_buf[M + 1];
600 :
601 : /* convert quantized LSP vector */
602 11323 : st->rate_switching_reset = lsp_convert_poly( st->lsp_old, st->L_frame, 0 );
603 11323 : lsp2lsf( st->lsp_old, st->lsf_old, M, st->sr_core );
604 :
605 11323 : if ( st->L_frame == L_FRAME16k )
606 : {
607 7010 : mvr2r( st->lsp_old, st->lspold_enc, M );
608 : }
609 : else
610 : {
611 4313 : mvr2r( st->lsp_old1, st->lspold_enc, M );
612 : }
613 :
614 : /* Update wsyn */
615 11323 : if ( hLPDmem != NULL )
616 : {
617 11301 : synth_mem_updt2( st->L_frame, st->last_L_frame, hLPDmem->old_exc, hLPDmem->mem_syn_r, hLPDmem->mem_syn2, hLPDmem->mem_syn, ENC );
618 11301 : lsp2a_stab( st->lsp_old, A, M );
619 11301 : weight_a( A, Ap, GAMMA1, M );
620 11301 : tmp = 0.f;
621 11301 : tmp_buf[0] = 0.f;
622 11301 : mvr2r( hLPDmem->mem_syn2, tmp_buf + 1, M );
623 11301 : deemph( tmp_buf + 1, st->preemph_fac, M, &tmp );
624 11301 : residu( Ap, M, tmp_buf + M, &tmp, 1 );
625 11301 : hLPDmem->mem_w0 = st->wspeech_enc[-1] - tmp;
626 : }
627 : }
628 233176 : else if ( st->L_frame != L_frame_old ) /* Rate switching involving TCX only modes */
629 : {
630 : /*Partial reset of ACELP memories*/
631 84335 : st->next_force_safety_net = 1;
632 84335 : st->rate_switching_reset = 1;
633 :
634 : /*reset partly some memories*/
635 84335 : if ( hLPDmem != NULL )
636 : {
637 17612 : hLPDmem->tilt_code = TILT_CODE;
638 17612 : set_zero( hLPDmem->old_exc, L_EXC_MEM );
639 :
640 : /*Resamp others memories*/
641 : /*Size of LPC syn memory*/
642 17612 : mem_syn_r_size_old = (int16_t) ( 1.25 * L_frame_old / 20.f );
643 17612 : mem_syn_r_size_new = (int16_t) ( 1.25 * st->L_frame / 20.f );
644 17612 : lerp( hLPDmem->mem_syn_r + L_SYN_MEM - mem_syn_r_size_old, hLPDmem->mem_syn_r + L_SYN_MEM - mem_syn_r_size_new, mem_syn_r_size_new, mem_syn_r_size_old );
645 17612 : mvr2r( hLPDmem->mem_syn_r + L_SYN_MEM - M, hLPDmem->mem_syn, M );
646 17612 : mvr2r( hLPDmem->mem_syn, hLPDmem->mem_syn2, M );
647 :
648 : /*Untouched memories : LPDmem->syn & LPDmem->mem_w0*/
649 17612 : hLPDmem->mem_w0 = 0;
650 : }
651 :
652 : /* unquantized LPC*/
653 84335 : mvr2r( st->lsp_old1, st->lspold_enc, M ); /*lsp old @12.8kHz*/
654 84335 : if ( st->L_frame == L_FRAME16k )
655 : {
656 15440 : lsp_convert_poly( st->lspold_enc, st->L_frame, 0 );
657 : }
658 84335 : mvr2r( st->lspold_enc, st->lsp_old, M ); /*used unquantized values for mid-LSF Q*/
659 84335 : lsp2lsf( st->lsp_old, st->lsf_old, M, st->sr_core );
660 : }
661 : /* necessary in BASOP only, due to different representations of st->lsf_old */
662 : /* else if ( !st->tcxonly && (st->L_frame == L_FRAME16k) && (st->last_total_brate > ACELP_32k) ) */
663 : /* { */
664 : /* lsp2lsf( st->lsp_old, st->lsf_old, M, st->sr_core ); */
665 : /* } */
666 : }
667 : }
668 :
669 563476 : if ( st->last_bwidth == NB && st->bwidth != NB && st->ini_frame != 0 )
670 : {
671 2 : st->rate_switching_reset = 1;
672 : }
673 :
674 : /* Post-processing */
675 563476 : if ( hLPDmem != NULL )
676 : {
677 277567 : set_zero( hLPDmem->dispMem, 8 );
678 277567 : hLPDmem->gc_threshold = 0.0f;
679 : }
680 :
681 : /* Pulse Search configuration */
682 563476 : st->acelp_autocorr = 1;
683 :
684 : /*Use for 12.8 kHz sampling rate and low bitrates, the conventional pulse search->better SNR*/
685 563476 : if ( ( total_brate <= ACELP_9k60 || st->rf_mode == 1 ) && st->sr_core == INT_FS_12k8 )
686 : {
687 17912 : st->acelp_autocorr = 0;
688 : }
689 :
690 : /*BPF parameters for adjusting gain in function of background noise*/
691 563476 : if ( st->codec_mode == MODE2 )
692 : {
693 1661 : st->pst_lp_ener = 0.0f;
694 1661 : if ( st->last_codec_mode == MODE1 )
695 : {
696 1402 : st->pst_mem_deemp_err = 0.0f;
697 : }
698 : }
699 :
700 :
701 563476 : return;
702 : }
703 :
704 : /*-----------------------------------------------------------------------*
705 : * init_modes()
706 : *
707 : *
708 : *-----------------------------------------------------------------------*/
709 :
710 563476 : static void init_modes(
711 : Encoder_State *st,
712 : const int32_t last_total_brate )
713 : {
714 : int16_t n;
715 :
716 : /* Restrict ACE/TCX20/TCX10 mode */
717 563476 : st->restrictedMode = getRestrictedMode( st->element_mode, st->total_brate, st->Opt_AMR_WB );
718 :
719 563476 : st->acelpEnabled = ( st->restrictedMode & 1 ) == 1;
720 563476 : st->tcx20Enabled = ( st->restrictedMode & 2 ) == 2;
721 563476 : st->tcx10Enabled = ( st->restrictedMode & 4 ) == 4;
722 :
723 : /* TCX mode (TCX20 TCX10_10 or NO_TCX) */
724 563476 : if ( st->hTcxEnc != NULL )
725 : {
726 563063 : st->hTcxEnc->tcxMode = NO_TCX;
727 : }
728 :
729 : /*st->bits_frame_nominal = (int16_t)( (float)st->L_frame * (float)FSCALE_DENOM * (float)st->total_brate / ( (float)st->fscale * 12800.0f ) );*/
730 563476 : st->bits_frame_nominal = (int16_t) ( (float) st->L_frame / (float) st->fscale * (float) FSCALE_DENOM / 128.0f * (float) st->total_brate / 100.0f + 0.49f );
731 :
732 563476 : if ( st->Opt_AMR_WB )
733 : {
734 48 : st->bits_frame = st->bits_frame_nominal;
735 48 : st->bits_frame_core = st->bits_frame_nominal;
736 48 : st->frame_size_index = 0;
737 : }
738 : else
739 : {
740 5227045 : for ( n = 0; n < FRAME_SIZE_NB; n++ )
741 : {
742 :
743 5227045 : if ( n < FRAME_SIZE_NB - 1 )
744 : {
745 5153814 : if ( FrameSizeConfig[n].frame_bits <= st->bits_frame_nominal && FrameSizeConfig[n + 1].frame_bits > st->bits_frame_nominal )
746 : {
747 490197 : st->frame_size_index = n;
748 490197 : st->bits_frame = st->bits_frame_nominal;
749 490197 : st->bits_frame_core = st->bits_frame_nominal - FrameSizeConfig[n].transmission_bits - FrameSizeConfig[n].bandwidth_bits - FrameSizeConfig[n].reserved_bits;
750 490197 : break;
751 : }
752 : }
753 : else
754 : {
755 73231 : if ( FrameSizeConfig[n].frame_bits <= st->bits_frame_nominal )
756 : {
757 73231 : st->frame_size_index = n;
758 73231 : st->bits_frame = st->bits_frame_nominal;
759 73231 : st->bits_frame_core = st->bits_frame_nominal - FrameSizeConfig[n].transmission_bits - FrameSizeConfig[n].bandwidth_bits - FrameSizeConfig[n].reserved_bits;
760 73231 : break;
761 : }
762 : }
763 : }
764 : }
765 :
766 : /* Reconfigure core */
767 563476 : core_coder_reconfig( st, last_total_brate );
768 :
769 563476 : return;
770 : }
|