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 <assert.h>
38 : #include <stdint.h>
39 : #include "options.h"
40 : #ifdef DEBUGGING
41 : #include "debug.h"
42 : #endif
43 : #include <math.h>
44 : #include "prot.h"
45 : #include "rom_com.h"
46 : #include "basop_proto_func.h"
47 : #include "wmc_auto.h"
48 :
49 : /*-------------------------------------------------------------------*
50 : * Local function prototypes
51 : *-------------------------------------------------------------------*/
52 :
53 : static void BITS_ALLOC_ACELP_config_rf( const int16_t coder_type, float *tilt_code, int16_t *rf_frame_type, int16_t *rf_target_bits, const int16_t nb_subfr, const int16_t rf_fec_indicator, float *pitch_buf );
54 :
55 : static void BITS_ALLOC_TCX_config_rf( int16_t *rf_frame_type, int16_t *rf_target_bits, const int16_t PLC_Mode, const int16_t coder_type, const int16_t last_core, const int16_t TD_Mode );
56 :
57 : static void closest_centroid_rf( const float *data, const float *weights, const float *quantizer, const int16_t centroids, const int16_t length, int16_t *ind_vec );
58 :
59 :
60 : /*-------------------------------------------------------------------*
61 : * core_encode_openloop()
62 : *
63 : * Open-loop core encoder
64 : *-------------------------------------------------------------------*/
65 :
66 1250 : void core_encode_openloop(
67 : Encoder_State *st, /* i/o: encoder state structure */
68 : const float Aw[NB_SUBFR16k * ( M + 1 )], /* i : weighted A(z) unquant. for subframes*/
69 : const float lsp_new[M], /* i : LSPs at the end of the frame */
70 : const float lsp_mid[M], /* i : LSPs at the middle of the frame */
71 : float *pitch_buf, /* i/o: floating pitch values for each subfr*/
72 : float *voice_factors, /* o : voicing factors */
73 : float *ptr_bwe_exc, /* o : excitation for SWB TBE */
74 : const int16_t vad_hover_flag /* i : VAD hangover flag */
75 : )
76 : {
77 1250 : TCX_ENC_HANDLE hTcxEnc = st->hTcxEnc;
78 :
79 : float lsf_q[M], lsp_q[M], lspmid_q[M];
80 : Word16 lspq_ind[M];
81 : Word16 A_q_ind[M + 1];
82 : float A_q_ace[NB_SUBFR16k * ( M + 1 )];
83 : float A_q_tcx[NB_SUBFR16k * ( M + 1 )];
84 : int16_t param_lpc[NPRM_LPC_NEW];
85 : int16_t nbits_lpc;
86 : int16_t param_core[2 * NPRM_DIV];
87 : int16_t target_bits;
88 : float stab_fac;
89 : int16_t indexBuffer[N_MAX + 1];
90 : CONTEXT_HM_CONFIG hm_cfg;
91 : float lsp_tcx_q[M], lsf_tcx_q[M];
92 : int16_t tcx_lpc_cdk;
93 : float A_w[M + 1];
94 : float gain_pitch_buf[NB_SUBFR16k];
95 : float gain_code_buf[NB_SUBFR16k];
96 : int16_t bits_param_lpc[10], no_param_lpc;
97 :
98 : /* lsf quant parameters */
99 : float lsp_q_rf[M];
100 : float Aq_rf[NB_SUBFR * ( M + 1 )];
101 : float stab_fac_rf;
102 : float *exc_rf;
103 : float *syn_rf;
104 : int16_t rf_PLC_Mode;
105 : int16_t TD_Mode;
106 : int16_t rf_tcx_lpc_cdk;
107 : float lsp[M], lsf[M];
108 : float rf_mem_MA[M];
109 : float exc_buf_rf[L_EXC_MEM + L_FRAME + 1];
110 : float syn_buf_rf[M + L_FRAME16k + L_FRAME16k / 2];
111 :
112 : float w_rf[M], lsf_uq_rf[M];
113 : float lsf_q_1st_rf[M], lsf_q_d_rf[M], lsf_q_rf[M];
114 : float lsp_old_q_rf[M], lsf_old_q_rf[M];
115 :
116 1250 : LPD_state_HANDLE hLPDmem = st->hLPDmem;
117 :
118 : /*--------------------------------------------------------------*
119 : * back up parameters for RF
120 : *---------------------------------------------------------------*/
121 :
122 : /* back up the old LSPs and LSFs */
123 1250 : mvr2r( st->lsp_old, lsp_old_q_rf, M );
124 1250 : mvr2r( st->lsf_old, lsf_old_q_rf, M );
125 :
126 : /* back up old exc before primary encoding */
127 1250 : set_f( exc_buf_rf, 0, ( L_EXC_MEM + L_FRAME + 1 ) );
128 1250 : exc_rf = exc_buf_rf + L_EXC_MEM;
129 1250 : mvr2r( hLPDmem->old_exc, exc_buf_rf, L_EXC_MEM );
130 :
131 : /* back up old synthesis before primary encoding */
132 1250 : set_f( syn_buf_rf, 0, ( M + L_FRAME16k + L_FRAME16k / 2 ) );
133 1250 : syn_rf = syn_buf_rf + M;
134 1250 : mvr2r( hLPDmem->mem_syn, syn_buf_rf, M );
135 :
136 1250 : if ( st->Opt_RF_ON )
137 : {
138 : /* back up syn2 mem */
139 0 : mvr2r( hLPDmem->mem_syn2, st->hRF->rf_mem_syn2, M );
140 :
141 : /* back up LPD mem_w0 target generation memory */
142 0 : st->hRF->rf_mem_w0 = hLPDmem->mem_w0;
143 :
144 : /* back up clip gain memory */
145 0 : mvr2r( st->clip_var, st->hRF->rf_clip_var, 6 );
146 :
147 : /* back up tilt code */
148 0 : st->hRF->rf_tilt_code = hLPDmem->tilt_code;
149 :
150 : /* back up dispMem */
151 0 : mvr2r( hLPDmem->dispMem, st->hRF->rf_dispMem, 8 );
152 :
153 : /* back up gc_threshold for noise addition */
154 0 : st->hRF->rf_gc_threshold = hLPDmem->gc_threshold;
155 : }
156 :
157 :
158 : /*--------------------------------------------------------------*
159 : * Initializations
160 : *---------------------------------------------------------------*/
161 :
162 1250 : tcx_lpc_cdk = 0;
163 1250 : set_s( param_lpc, 0, NPRM_LPC_NEW );
164 1250 : set_s( param_core, 0, 2 * NPRM_DIV );
165 1250 : mvs2s( hTcxEnc->tcxltp_param, ¶m_core[1 + NOISE_FILL_RANGES], LTPSIZE );
166 :
167 1250 : no_param_lpc = 0; /* avoid MSVC warnings */
168 1250 : nbits_lpc = 0; /* avoid MSVC warnings */
169 1250 : stab_fac = 0.0f; /* avoid MSVC warnings */
170 :
171 1250 : hm_cfg.indexBuffer = indexBuffer;
172 :
173 : /*--------------------------------------------------------------*
174 : * LPC Quantization
175 : *---------------------------------------------------------------*/
176 :
177 1250 : if ( st->lpcQuantization == 1 && st->coder_type == VOICED )
178 : {
179 517 : ( &( st->acelp_cfg ) )->midLpc = 0;
180 : }
181 : else
182 : {
183 733 : ( &( st->acelp_cfg ) )->midLpc = st->acelp_cfg.midLpc_enable;
184 : }
185 :
186 1250 : if ( st->core == ACELP_CORE || !st->enableTcxLpc )
187 : {
188 1250 : if ( st->envWeighted )
189 : {
190 : /* Unweight the envelope */
191 0 : E_LPC_lsp_unweight( st->lsp_old, st->lsp_old, st->lsf_old, 1.0f / st->gamma );
192 0 : st->envWeighted = 0;
193 : }
194 :
195 1250 : if ( st->core == TCX_20_CORE )
196 : {
197 646 : lpc_quantization( st, lsp_new, lsp_mid, lsp_q, lsf_q, lspmid_q, AUDIO, st->acelp_cfg.midLpc, param_lpc, &nbits_lpc, bits_param_lpc, &no_param_lpc );
198 : }
199 : else
200 : {
201 604 : lpc_quantization( st, lsp_new, lsp_mid, lsp_q, lsf_q, lspmid_q, st->coder_type, st->acelp_cfg.midLpc, param_lpc, &nbits_lpc, bits_param_lpc, &no_param_lpc );
202 : }
203 :
204 : /*-------------------------------------------------------------*
205 : * Rate switching: reset
206 : *-------------------------------------------------------------*/
207 :
208 1250 : if ( st->rate_switching_reset )
209 : {
210 21 : mvr2r( lsp_q, st->lsp_old, M );
211 21 : mvr2r( lsf_q, st->lsf_old, M );
212 21 : mvr2r( lsp_q, lspmid_q, M );
213 : }
214 :
215 : /*--------------------------------------------------------------*
216 : * LPC Interpolation
217 : *---------------------------------------------------------------*/
218 :
219 1250 : stab_fac = lsf_stab( lsf_q, st->lsf_old, 0, st->L_frame );
220 : }
221 :
222 :
223 : /*--------------------------------------------------------------*
224 : * Run ACELP
225 : *---------------------------------------------------------------*/
226 :
227 1250 : if ( st->core == ACELP_CORE )
228 : {
229 604 : if ( st->acelp_cfg.midLpc )
230 : {
231 336 : int_lsp4( st->L_frame, st->lsp_old, lspmid_q, lsp_q, A_q_ace, M, 0 );
232 : }
233 : else
234 : {
235 268 : int_lsp( st->L_frame, st->lsp_old, lsp_q, A_q_ace, M, interpol_frac_12k8, 0 );
236 : }
237 :
238 : /* Calculate target bits */
239 604 : target_bits = st->bits_frame_core - nbits_lpc - st->nb_bits_header_ace;
240 :
241 604 : if ( st->rf_mode )
242 : {
243 : /* joint bit allocation for redundant frame and TBE */
244 : /* calculate target bits for core coding */
245 0 : target_bits -= st->rf_target_bits_write;
246 : }
247 :
248 604 : if ( st->igf )
249 : {
250 604 : target_bits -= get_tbe_bits( st->total_brate, st->bwidth, st->rf_mode );
251 : }
252 :
253 604 : if ( st->acelp_cfg.midLpc )
254 : {
255 336 : target_bits -= MIDLSF_NBITS;
256 : }
257 :
258 604 : if ( st->hPlcExt && st->hPlcExt->enableGplc )
259 : {
260 604 : target_bits -= st->hPlcExt->nBits;
261 : }
262 :
263 : /* reset TBE buffers previous frame frame wasn't ACELP*/
264 604 : if ( st->last_core != ACELP_CORE )
265 : {
266 30 : TBEreset_enc( st->hBWE_TD, st->last_core, st->bwidth );
267 : }
268 :
269 : /* Run ACELP encoder */
270 604 : coder_acelp( st, Aw, A_q_ace, st->speech_enc_pe, hLPDmem, param_core, stab_fac, target_bits, gain_pitch_buf, gain_code_buf, pitch_buf, voice_factors, ptr_bwe_exc );
271 :
272 604 : if ( st->hPlcExt )
273 : {
274 604 : st->glr_idx[0] = encSideSpecPowDiffuseDetector( st->hPlcExt->last_lsf_ref, st->hPlcExt->last_lsf_con, st->last_sr_core, &( st->prev_lsf4_mean ), st->glr, st->coder_type );
275 :
276 604 : mvr2r( lsf_q, st->hPlcExt->last_lsf_ref, M );
277 604 : mvr2r( st->hPlcExt->lsf_con, st->hPlcExt->last_lsf_con, M );
278 604 : updateSpecPowDiffuseIdx( gain_pitch_buf, gain_code_buf, st->glr_idx, st->mean_gc );
279 :
280 604 : if ( st->last_stab_fac > 0.02 )
281 : {
282 493 : st->glr_idx[0] = 0;
283 : }
284 :
285 604 : st->hPlcExt->LPDmem = hLPDmem;
286 :
287 604 : encoderSideLossSimulation( st, st->hPlcExt, lsf_q, stab_fac, st->hPlcExt->calcOnlylsf, st->L_frame );
288 : }
289 604 : st->last_stab_fac = stab_fac;
290 :
291 604 : hTcxEnc->tcxltp_norm_corr_past = st->voicing[1];
292 604 : st->hTcxCfg->tcx_curr_overlap_mode = st->hTcxCfg->tcx_last_overlap_mode = ALDO_WINDOW;
293 : }
294 :
295 :
296 : /*--------------------------------------------------------------*
297 : * Run TCX20
298 : *---------------------------------------------------------------*/
299 :
300 1250 : if ( st->core == TCX_20_CORE )
301 : {
302 646 : if ( st->enableTcxLpc )
303 : {
304 0 : if ( st->rf_mode )
305 : {
306 0 : mvr2r( st->mem_MA, rf_mem_MA, M );
307 : }
308 :
309 0 : tcx_lpc_cdk = tcxlpc_get_cdk( st->hTcxCfg->coder_type );
310 :
311 : /* Get the envelope corresponding to the current frame */
312 0 : E_LPC_int_lpc_tcx( st->lspold_enc, lsp_new, A_q_tcx );
313 :
314 : /* Weight the envelope */
315 0 : weight_a( A_q_tcx, A_q_tcx, st->gamma, M );
316 :
317 : /* Save the weighted envelope */
318 0 : mvr2r( A_q_tcx, A_w, M + 1 );
319 :
320 : /* Convert to lsp and lsf */
321 0 : a2lsp_stab( A_q_tcx, lsp, lsp_new );
322 0 : lsp2lsf( lsp, lsf, M, INT_FS_12k8 );
323 :
324 : /* Quantize */
325 0 : Q_lsf_tcxlpc( lsf, lsf_tcx_q, lspq_ind, param_lpc, st->narrowBand, tcx_lpc_cdk, st->mem_MA, st->hTcxCfg->coder_type, st->Bin_E );
326 :
327 : /* Account for consumed bits */
328 0 : nbits_lpc = TCXLPC_NUMBITS;
329 0 : if ( param_lpc[0] )
330 : {
331 0 : nbits_lpc += TCXLPC_IND_NUMBITS;
332 : }
333 :
334 : /* Convert quantized lsf to lsp and A */
335 0 : lsf2lsp( lsf_tcx_q, lsp_tcx_q, M, INT_FS_12k8 );
336 0 : lsp2a_stab( lsp_tcx_q, A_q_tcx, M );
337 : }
338 : else
339 : {
340 646 : E_LPC_int_lpc_tcx( st->lsp_old, lsp_q, A_q_tcx );
341 : }
342 :
343 646 : if ( hTcxEnc->tcx_lpc_shaped_ari )
344 : {
345 0 : basop_E_LPC_f_lsp_a_conversion( lspq_ind, A_q_ind, M );
346 : }
347 :
348 : /* Calculate target bits */
349 646 : target_bits = st->bits_frame_core - nbits_lpc - st->nb_bits_header_tcx;
350 646 : if ( st->rf_mode )
351 : {
352 : /* joint bit allocation for redundant frame and TBE */
353 : /* calculate target bits for core coding */
354 0 : target_bits -= st->rf_target_bits_write;
355 : }
356 :
357 646 : if ( st->mdct_sw == MODE1 )
358 : {
359 : /* Account for core signaling bits difference: bandwidth and ACELP/TCX signaling bit are replaced */
360 275 : target_bits += ( FrameSizeConfig[st->frame_size_index].bandwidth_bits + 1 ) - signaling_mode1_tcx20_enc( st, 0 );
361 : }
362 371 : else if ( st->mdct_sw_enable == MODE2 )
363 : {
364 371 : --target_bits;
365 : }
366 :
367 646 : if ( st->hPlcExt && st->hPlcExt->enableGplc )
368 : {
369 371 : target_bits -= st->hPlcExt->nBits;
370 : }
371 :
372 : /* subtract bits for TCX overlap mode (1 bit: full, 2 bits: half or no overlap) */
373 646 : target_bits -= ( st->hTcxCfg->tcx_curr_overlap_mode == HALF_OVERLAP || st->hTcxCfg->tcx_curr_overlap_mode == MIN_OVERLAP ) ? 2 : 1;
374 :
375 646 : target_bits -= hTcxEnc->tcxltp_bits;
376 :
377 : /* Run TCX20 encoder */
378 646 : coder_tcx( st, st->hTcxCfg, A_q_tcx, A_q_ind, st->synth, st->L_frame, hTcxEnc->L_frameTCX, st->hTcxCfg->tcx_coded_lines, target_bits, hTcxEnc->spectrum_long, param_core, &hm_cfg, vad_hover_flag );
379 :
380 646 : coder_tcx_post( st, A_q_tcx, Aw );
381 :
382 646 : if ( st->hPlcExt )
383 : {
384 646 : st->hPlcExt->LPDmem = hLPDmem;
385 :
386 646 : GplcTcxEncSetup( hTcxEnc->tcxltp_pitch_int, st->hPlcExt );
387 : }
388 :
389 646 : if ( st->enableTcxLpc )
390 : {
391 0 : E_LPC_lsp_unweight( lsp_tcx_q, lsp_q, lsf_q, 1.0f / st->gamma ); /* Update lsf_q for encoderSideLossSimulation() */
392 : }
393 646 : if ( st->hPlcExt )
394 : {
395 646 : encoderSideLossSimulation( st, st->hPlcExt, lsf_q, stab_fac, 1, st->L_frame );
396 : }
397 : }
398 :
399 :
400 : /* Update lsp/lsf memory */
401 1250 : mvr2r( lsp_new, st->lspold_enc, M );
402 :
403 1250 : if ( st->enableTcxLpc && st->core != ACELP_CORE )
404 : {
405 : /* Update lsf / lsp memory */
406 0 : mvr2r( lsf_tcx_q, st->lsf_old, M );
407 0 : mvr2r( lsp_tcx_q, st->lsp_old, M );
408 0 : st->envWeighted = 1;
409 :
410 : /* Update ACELP quantizer state */
411 0 : lsf_update_memory( st->narrowBand, st->lsf_old, st->mem_MA, st->mem_MA );
412 0 : st->pstreaklen = 0;
413 0 : st->streaklimit = 1.0f;
414 :
415 : /* check resonance for pitch clipping algorithm */
416 0 : gp_clip_test_lsf( st->element_mode, st->core_brate, st->lsf_old, st->clip_var, 0 );
417 0 : mvr2r( st->lsf_old, st->mem_AR, M );
418 : }
419 : else
420 : {
421 1250 : mvr2r( lsf_q, st->lsf_old, M );
422 1250 : mvr2r( lsp_q, st->lsp_old, M );
423 : }
424 :
425 1250 : if ( st->Opt_DTX_ON )
426 : {
427 : /* update CNG parameters in active frames */
428 0 : if ( st->bwidth == NB && st->enableTcxLpc && st->core != ACELP_CORE )
429 0 : {
430 : float buf[L_LP], res[L_FRAME], A[M + 1], r[M + 1], tmp, lsptmp[M];
431 :
432 0 : assert( st->L_frame == L_FRAME );
433 :
434 0 : mvr2r( st->synth + L_FRAME - L_LP, buf, L_LP );
435 0 : tmp = st->synth[L_FRAME - L_LP - 1];
436 0 : preemph( buf, st->preemph_fac, L_LP, &tmp );
437 0 : autocorr( buf, r, M, L_LP, LP_assym_window, 0, 0, 0 );
438 0 : lag_wind( r, M, INT_FS_12k8, LAGW_WEAK );
439 0 : lev_dur( A, r, M, NULL );
440 0 : a2lsp_stab( A, lsptmp, lsp_new );
441 :
442 0 : residu( A, M, buf + L_LP - L_FRAME, res, L_FRAME );
443 :
444 0 : cng_params_upd( lsptmp, res, 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 );
445 : }
446 : else
447 : {
448 0 : cng_params_upd( lsp_new, hLPDmem->old_exc + L_EXC_MEM - st->L_frame, 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 );
449 : }
450 :
451 0 : if ( st->L_frame == L_FRAME )
452 : {
453 : /* store LSPs@16k, potentially to be used in CNG@16k */
454 0 : mvr2r( st->lsp_old16k, &( st->hTdCngEnc->ho_lsp_circ2[( st->hTdCngEnc->ho_circ_ptr ) * M] ), M );
455 : }
456 :
457 : /* Set 16k LSP flag for CNG buffer */
458 0 : st->hTdCngEnc->ho_16k_lsp[st->hTdCngEnc->ho_circ_ptr] = ( st->L_frame == L_FRAME ? 0 : 1 );
459 :
460 : /* efficient DTX hangover control */
461 0 : if ( st->hTdCngEnc->burst_ho_cnt > 1 )
462 : {
463 0 : dtx_hangover_control( st, lsp_new );
464 : }
465 : }
466 :
467 : /*--------------------------------------------------------------*
468 : * Adaptive Bass Post-filter
469 : *---------------------------------------------------------------*/
470 :
471 1250 : if ( st->core > ACELP_CORE || st->rate_switching_reset )
472 : {
473 : /*TCX mode: copy values*/
474 654 : set_zero( st->mem_bpf, 2 * L_FILT16k ); /*TCX->no gain*/
475 654 : set_zero( st->mem_error_bpf, 2 * L_FILT16k ); /*TCX->no gain*/
476 654 : st->bpf_gain_param = 0;
477 : }
478 596 : else if ( st->acelp_cfg.bpf_mode >= 1 )
479 : {
480 : /*ACELP: estimate bpf parameter with delay=0*/
481 :
482 : /*Estimate bpf parameter*/
483 596 : bass_pf_enc( st->speech_enc, st->synth, pitch_buf, gain_pitch_buf, st->L_frame, L_SUBFR, st->mem_bpf, st->mem_error_bpf, &( st->bpf_gain_param ), st->acelp_cfg.bpf_mode, &( st->pst_lp_ener ), &( st->pst_mem_deemp_err ) );
484 : }
485 :
486 : /*--------------------------------------------------------------*
487 : * Generate Bitstream
488 : *---------------------------------------------------------------*/
489 :
490 1250 : enc_prm( st, param_core, param_lpc, &hm_cfg, bits_param_lpc, no_param_lpc );
491 :
492 : /* Channel-aware mode - encode partial copy */
493 1250 : if ( st->rf_mode )
494 : {
495 0 : RF_ENC_HANDLE hRF = st->hRF;
496 :
497 0 : set_f( lsf_q_1st_rf, 0.0f, M );
498 :
499 0 : if ( st->core == ACELP_CORE )
500 : {
501 : /* convert lsp to lsf */
502 0 : lsp2lsf( lsp_new, lsf_uq_rf, M, st->sr_core );
503 :
504 : /* first stage VQ, 8 bits; reuse TCX high rate codebook */
505 0 : hRF->rf_indx_lsf[0][0] = vlpc_1st_cod( lsf_uq_rf, lsf_q_1st_rf, st->sr_core, w_rf );
506 0 : v_sub( lsf_uq_rf, lsf_q_1st_rf, lsf_q_d_rf, M );
507 :
508 : /* second stage vq */
509 0 : closest_centroid_rf( lsf_q_d_rf, w_rf, lsf_q_diff_cb_8b_rf, ( 1 << 8 ), M, &hRF->rf_indx_lsf[0][1] );
510 :
511 : /* quantized lsf from two stages */
512 0 : v_add( lsf_q_1st_rf, lsf_q_diff_cb_8b_rf + M * hRF->rf_indx_lsf[0][1], lsf_q_rf, M );
513 :
514 0 : v_sort( lsf_q_rf, 0, M - 1 );
515 0 : reorder_lsf( lsf_q_rf, LSF_GAP, M, st->sr_core );
516 : }
517 : else
518 : {
519 0 : rf_tcx_lpc_cdk = tcxlpc_get_cdk( GENERIC );
520 :
521 : /* Quantize */
522 0 : Q_lsf_tcxlpc( lsf, lsf_tcx_q, lspq_ind, param_lpc, st->narrowBand, rf_tcx_lpc_cdk, rf_mem_MA, GENERIC, st->Bin_E );
523 :
524 : /* VQ, 5+4+4 bits; reuse TCX low rate codebook */
525 0 : hRF->rf_indx_lsf[0][0] = param_lpc[1];
526 0 : hRF->rf_indx_lsf[0][1] = param_lpc[2];
527 0 : hRF->rf_indx_lsf[0][2] = param_lpc[3];
528 : }
529 :
530 0 : if ( st->core == ACELP_CORE )
531 : {
532 : /* current n-th ACELP frame and its corresponding partial copy */
533 0 : lsf2lsp( lsf_q_rf, lsp_q_rf, M, st->sr_core );
534 :
535 : /* Interpolate LSPs and convert to LPC */
536 0 : int_lsp( st->L_frame, lsp_old_q_rf, lsp_q_rf, Aq_rf, M, interpol_frac_12k8, 0 );
537 :
538 : /* stability estimation */
539 0 : stab_fac_rf = lsf_stab( lsf_q_rf, lsf_old_q_rf, 0, st->L_frame );
540 :
541 : /* Configure partial copy estimation of the current n-th frame to be packed in future with n+fec_offset frame */
542 : /* o: rf_frame_type, o: rf_target_bits */
543 0 : if ( st->Opt_RF_ON )
544 : {
545 0 : BITS_ALLOC_ACELP_config_rf( st->coder_type, hRF->rf_tilt_buf, &hRF->rf_frame_type, &hRF->rf_target_bits, st->nb_subfr, st->rf_fec_indicator, pitch_buf );
546 : }
547 :
548 : /* RF frame type in the buffer */
549 0 : hRF->rf_indx_frametype[0] = hRF->rf_frame_type;
550 0 : hRF->rf_targetbits_buff[0] = hRF->rf_target_bits;
551 :
552 0 : if ( hRF->rf_frame_type != RF_NO_DATA )
553 : {
554 : /* coder_acelp_rf does the partial copy encoding based on the rf frame type chosen for the RF encoding */
555 0 : coder_acelp_rf( hRF->rf_target_bits, st->speech_enc_pe, st->coder_type, hRF->rf_frame_type, Aw, Aq_rf, st->voicing, st->pitch, stab_fac_rf, st, &( hRF->acelp_cfg_rf ), exc_rf, syn_rf );
556 : }
557 : }
558 : else
559 : {
560 0 : TD_Mode = 1;
561 0 : hRF->rf_clas[0] = st->clas;
562 0 : hRF->rf_gain_tcx[0] = param_core[0];
563 :
564 : /* attenuate somewhat the gain for onset when the correlation with previous frame is too low: avoid preecho */
565 0 : if ( hRF->rf_gain_tcx[1] != 0 && hRF->rf_gain_tcx[0] > 1.6 * hRF->rf_gain_tcx[1] && hTcxEnc->tcxltp_gain <= 0.2 )
566 : {
567 0 : hRF->rf_gain_tcx[0] = (int16_t) ( 1.6f * hRF->rf_gain_tcx[1] );
568 :
569 0 : if ( hRF->rf_gain_tcx[0] > 127 )
570 : {
571 0 : hRF->rf_gain_tcx[0] = 127;
572 : }
573 : }
574 :
575 : /* get concealment decision*/
576 0 : rf_PLC_Mode = 0;
577 0 : if ( st->core == TCX_20_CORE && ( st->last_core == TCX_20_CORE ) && ( hRF->rf_second_last_core == TCX_20_CORE ) && ( ( hTcxEnc->tcxltp_pitch_int <= 0.5f * st->L_frame ) || ( hTcxEnc->tcxltp_gain <= 0.4f ) ) && ( hTcxEnc->tcxltp_pitch_int == hRF->rf_tcxltp_pitch_int_past ) && !hRF->rf_last_tns_active && !hRF->rf_second_last_tns_active && !( st->hTcxCfg->fIsTNSAllowed & hTcxEnc->fUseTns[0] ) )
578 : {
579 0 : rf_PLC_Mode = 1;
580 : }
581 0 : else if ( st->last_core != ACELP_CORE )
582 : {
583 0 : if ( ( st->clas <= UNVOICED_TRANSITION || st->last_clas <= UNVOICED_TRANSITION || hTcxEnc->tcxltp_gain <= 0.4f ) && st->last_core != -1 )
584 : {
585 0 : rf_PLC_Mode = st->last_core;
586 : }
587 : }
588 :
589 : /* call TD1 when the gain drop compare to previous frame*/
590 0 : if ( rf_PLC_Mode == 0 && hRF->rf_gain_tcx[1] != 0 &&
591 0 : ( ( st->hTranDet->transientDetector.bIsAttackPresent && hRF->rf_gain_tcx[0] < 0.97 * hRF->rf_gain_tcx[1] ) ||
592 0 : hRF->rf_gain_tcx[0] < 0.90 * hRF->rf_gain_tcx[1] ) )
593 : {
594 0 : TD_Mode = 0;
595 : }
596 : else
597 : {
598 0 : TD_Mode = 1;
599 : }
600 :
601 : /* updates */
602 0 : hRF->rf_tcxltp_pitch_int_past = hTcxEnc->tcxltp_pitch_int;
603 0 : hRF->rf_second_last_tns_active = hRF->rf_last_tns_active;
604 0 : hRF->rf_last_tns_active = st->hTcxCfg->fIsTNSAllowed & hTcxEnc->fUseTns[0];
605 0 : hRF->rf_second_last_core = st->last_core;
606 :
607 0 : hRF->rf_tcxltp_param[0] = hTcxEnc->tcxltp_param[1];
608 :
609 : /* Configure partial copy estimation of the current n-th frame to be packed in future with n+fec_offset frame */
610 0 : BITS_ALLOC_TCX_config_rf( &hRF->rf_frame_type, &hRF->rf_target_bits, rf_PLC_Mode, st->coder_type, st->last_core, TD_Mode );
611 :
612 : /* RF frame type in the buffer */
613 0 : hRF->rf_indx_frametype[0] = hRF->rf_frame_type;
614 0 : hRF->rf_targetbits_buff[0] = hRF->rf_target_bits;
615 : }
616 : }
617 :
618 1250 : return;
619 : }
620 :
621 :
622 : /*-------------------------------------------------------------------*
623 : * closest_centroid_rf()
624 : *
625 : * Determine a set of closest VQ centroids for a given input
626 : *-------------------------------------------------------------------*/
627 :
628 0 : static void closest_centroid_rf(
629 : const float *data, /* i : input data */
630 : const float *weights, /* i : weights */
631 : const float *quantizer, /* i : quantizer table */
632 : const int16_t centroids, /* i : number of centroids */
633 : const int16_t length, /* i : dimension of quantiser */
634 : int16_t *ind_vec /* o : list of best match indice vectors */
635 : )
636 : {
637 : int16_t i, j;
638 : float tmp, werr, best_werr;
639 :
640 0 : ind_vec[0] = 0;
641 0 : best_werr = 1.0E20f;
642 :
643 0 : for ( i = 0; i < centroids; i++ )
644 : {
645 0 : werr = 0.0f;
646 0 : for ( j = 0; j < length; j++ )
647 : {
648 0 : tmp = (float) *( data + j ) - quantizer[i * length + j];
649 0 : werr += (float) ( *( weights + j ) * tmp * tmp );
650 : }
651 :
652 0 : if ( werr < best_werr )
653 : {
654 0 : ind_vec[0] = i;
655 0 : best_werr = werr;
656 : }
657 : }
658 :
659 0 : return;
660 : }
661 :
662 :
663 : /*-------------------------------------------------------------------*
664 : * core_acelp_tcx20_switching()
665 : *
666 : * Open-loop ACELP/TCX20 core decision
667 : *-------------------------------------------------------------------*/
668 :
669 1325 : void core_acelp_tcx20_switching(
670 : Encoder_State *st, /* i/o: encoder state structure */
671 : float non_staX, /* i : unbound non-stationarity for sp/mu clas */
672 : float *pitch_fr, /* i/o: fraction pitch values */
673 : float *voicing_fr, /* i/o: fractional voicing values */
674 : const float currFlatness, /* i : flatness */
675 : const float lsp_mid[M], /* i : LSPs at the middle of the frame */
676 : const float stab_fac /* i : LP filter stability */
677 : )
678 : {
679 1325 : TCX_ENC_HANDLE hTcxEnc = st->hTcxEnc;
680 : int16_t i, j;
681 : float A_q_tcx[NB_SUBFR16k * ( M + 1 )];
682 : float dsnr, snr_tcx, snr_acelp;
683 : int16_t iter;
684 : float xn_buf[L_MDCT_OVLP_MAX + L_FRAME_PLUS + L_MDCT_OVLP_MAX];
685 : float Ap[M + 1];
686 : float gainlpc[FDNS_NPTS];
687 : float en[N_MAX / 4];
688 : float sqGain, ener, tmp, fac, offset;
689 1325 : int16_t L_frame = st->L_frame;
690 : int16_t overlap;
691 1325 : int16_t tcx_offset = st->hTcxCfg->tcx_offset;
692 1325 : float *x = hTcxEnc->spectrum_long;
693 : float target;
694 : int16_t T0;
695 : float gain, noise, scale;
696 : float *pt_ener_sfr, ener_sfr[NB_SUBFR16k];
697 :
698 : /* Check minimum pitch for quantization */
699 5300 : for ( i = 0; i < 3; i++ )
700 : {
701 : /* check minimum pitch for quantization */
702 3975 : if ( st->pitch[i] < PIT_MIN_SHORTER )
703 : {
704 354 : st->pitch[i] *= 2;
705 : }
706 :
707 : /* convert pitch values to 16kHz domain */
708 3975 : if ( st->L_frame == L_FRAME16k )
709 : {
710 3150 : st->pitch[i] = (int16_t) ( st->pitch[i] * 1.25f + 0.5f );
711 : }
712 : }
713 1325 : if ( st->narrowBand == 1 )
714 : {
715 0 : pitchDoubling_det( st->wspeech_enc, st->pitch, pitch_fr, voicing_fr );
716 : }
717 :
718 1325 : lsp2a_stab( lsp_mid, A_q_tcx, M );
719 :
720 1325 : tcx_ltp_encode( st, TCX_20, st->L_frame, st->speech_enc + st->encoderLookahead_enc, hTcxEnc->speech_ltp + st->encoderLookahead_enc, st->wspeech_enc + st->encoderLookahead_enc, st->pitch, hTcxEnc->tcxltp_param, &hTcxEnc->tcxltp_bits, A_q_tcx, 0, st->element_mode );
721 :
722 :
723 : /* Force TCX when TCX20 in MODE1 is selected */
724 1325 : if ( st->mdct_sw == MODE1 )
725 : {
726 275 : st->core = TCX_20_CORE;
727 : }
728 : else
729 : {
730 : /*--------------------------------------------------------------*
731 : * Estimate TCX SNR
732 : *---------------------------------------------------------------*/
733 :
734 1050 : target = 1000.f;
735 1050 : if ( st->sr_core == INT_FS_16k )
736 : {
737 1050 : target = 850.f;
738 : }
739 0 : else if ( st->sr_core == INT_FS_12k8 )
740 : {
741 0 : target = 850.f;
742 : }
743 :
744 1050 : if ( st->narrowBand == 1 )
745 : {
746 0 : target = 500.f;
747 : }
748 :
749 1050 : if ( st->last_core == ACELP_CORE )
750 : {
751 604 : L_frame += tcx_offset;
752 :
753 604 : if ( st->hTcxCfg->lfacNext < 0 )
754 : {
755 604 : L_frame -= st->hTcxCfg->lfacNext;
756 604 : tcx_offset = st->hTcxCfg->lfacNext;
757 : }
758 : else
759 : {
760 0 : tcx_offset = 0;
761 : }
762 : }
763 :
764 1050 : overlap = st->hTcxCfg->tcx_mdct_window_delay;
765 :
766 1050 : mvr2r( hTcxEnc->speech_ltp - ( overlap >> 1 ) + tcx_offset, xn_buf, L_frame + overlap );
767 :
768 1050 : if ( st->last_core == ACELP_CORE )
769 : {
770 604 : if ( tcx_offset < 0 )
771 : {
772 604 : set_f( xn_buf, 0.0f, overlap >> 1 );
773 : }
774 : }
775 : else
776 : {
777 62886 : for ( i = 0; i < overlap; i++ )
778 : {
779 62440 : xn_buf[i] *= st->hTcxCfg->tcx_mdct_window[i];
780 : }
781 : }
782 :
783 148050 : for ( i = 0; i < overlap; i++ )
784 : {
785 147000 : xn_buf[L_frame + i] *= st->hTcxCfg->tcx_mdct_window[overlap - 1 - i];
786 : }
787 :
788 1050 : TCX_MDCT( xn_buf, x, overlap, L_frame - overlap, overlap, st->element_mode );
789 :
790 385370 : for ( i = 0; i < L_frame; i++ )
791 : {
792 384320 : x[i] *= (float) ( L_frame / sqrt( 2 * NORM_MDCT_FACTOR ) );
793 : }
794 :
795 1050 : weight_a( A_q_tcx, Ap, st->gamma, M );
796 :
797 1050 : lpc2mdct( Ap, M, gainlpc, FDNS_NPTS, 0 );
798 :
799 1050 : mdct_preShaping( x, L_frame, gainlpc );
800 :
801 1050 : if ( st->narrowBand == 1 )
802 : {
803 0 : j = (int16_t) ( (float) L_frame * 0.625f );
804 :
805 0 : set_f( x + j, 0.0f, L_frame - j );
806 : }
807 :
808 97130 : for ( i = 0; i < L_frame; i += 4 )
809 : {
810 96080 : ener = 0.01f + x[i] * x[i] + x[i + 1] * x[i + 1] + x[i + 2] * x[i + 2] + x[i + 3] * x[i + 3];
811 96080 : en[i / 4] = 9.0f + 10.0f * (float) log10( ener );
812 : }
813 :
814 1050 : fac = 128.0f;
815 1050 : offset = fac;
816 :
817 11550 : for ( iter = 0; iter < 10; iter++ )
818 : {
819 10500 : fac *= 0.5f;
820 10500 : offset -= fac;
821 10500 : ener = 0.0f;
822 :
823 855889 : for ( i = 0; i < L_frame / 4; i++ )
824 : {
825 850464 : tmp = en[i] - offset;
826 :
827 850464 : if ( tmp > 3.0f )
828 : {
829 632034 : ener += tmp;
830 : }
831 :
832 850464 : if ( ener > target )
833 : {
834 5075 : offset += fac;
835 5075 : break;
836 : }
837 : }
838 : }
839 :
840 1050 : if ( offset <= 32.f )
841 : {
842 48 : offset = -128.f;
843 : }
844 :
845 1050 : sqGain = (float) pow( 10.0f, offset / 20.0f );
846 1050 : ener = sqGain * sqGain / 12.f * (float) sqrt( 2.f ) / (float) L_frame;
847 :
848 1050 : snr_tcx = 0.0f;
849 1050 : pt_ener_sfr = ener_sfr;
850 :
851 6300 : for ( i = 0; i < st->L_frame; i += L_SUBFR )
852 : {
853 5250 : *pt_ener_sfr = sum2_f( st->wspeech_enc + i, L_SUBFR ) + 1e-6f;
854 :
855 5250 : snr_tcx += (float) log10( *pt_ener_sfr / ( ener * L_SUBFR ) );
856 5250 : pt_ener_sfr++;
857 : }
858 1050 : snr_tcx *= ( (float) ( 10 * L_SUBFR ) ) / (float) st->L_frame;
859 :
860 :
861 : /*--------------------------------------------------------------*
862 : * Estimate ACELP SNR
863 : *---------------------------------------------------------------*/
864 :
865 1050 : scale = 0.055f;
866 1050 : if ( st->sr_core == INT_FS_16k )
867 : {
868 1050 : scale = 0.092f;
869 : }
870 0 : else if ( st->sr_core == INT_FS_12k8 )
871 : {
872 0 : scale = 0.059f;
873 : }
874 :
875 1050 : if ( st->narrowBand )
876 : {
877 0 : scale = 0.15f;
878 : }
879 :
880 1050 : snr_acelp = 0.0f;
881 1050 : fac = (float) st->sr_core / (float) INT_FS_12k8;
882 1050 : pt_ener_sfr = ener_sfr;
883 :
884 6300 : for ( i = 0; i < st->L_frame; i += L_SUBFR )
885 : {
886 5250 : T0 = (int16_t) ( ( fac * pitch_fr[(int16_t) ( (float) ( i / L_SUBFR ) / fac + 0.5f )] ) + 0.5f );
887 5250 : gain = get_gain( st->wspeech_enc + i, st->wspeech_enc + i - T0, L_SUBFR, NULL );
888 :
889 5250 : noise = 1e-6f;
890 341250 : for ( j = 0; j < L_SUBFR; j++ )
891 : {
892 336000 : tmp = st->wspeech_enc[i + j] - gain * st->wspeech_enc[i + j - T0];
893 336000 : noise += tmp * tmp;
894 : }
895 :
896 5250 : noise *= scale;
897 5250 : snr_acelp += (float) log10( *pt_ener_sfr / noise );
898 5250 : pt_ener_sfr++;
899 : }
900 :
901 1050 : snr_acelp *= ( (float) ( 10 * L_SUBFR ) ) / (float) st->L_frame;
902 :
903 :
904 : /*--------------------------------------------------------------*
905 : * Switching Decision
906 : *---------------------------------------------------------------*/
907 :
908 1050 : dsnr = 0.0f;
909 : /* hysteresis for very small SNR differences between ACELP and TCX */
910 :
911 : /* try to use TCX instead of ACELP on temporally stationary frames */
912 1050 : if ( ( snr_acelp > snr_tcx ) &&
913 612 : ( snr_acelp < snr_tcx + 2.0f ) &&
914 143 : ( st->prevTempFlatness + currFlatness < 3.25f || stab_fac == 1.0f || ( st->sr_core == INT_FS_12k8 && st->sp_aud_decision0 == 1 && st->prevTempFlatness + currFlatness < 20.f ) ) &&
915 63 : ( st->acelpFramesCount <= 6 ) )
916 : {
917 16 : dsnr = -2.0f;
918 : }
919 :
920 : /* try to use ACELP instead of TCX on transient and "buzzy" frames */
921 1050 : if ( ( snr_acelp < snr_tcx ) &&
922 438 : ( snr_acelp > snr_tcx - 2.0f ) &&
923 187 : ( st->prevTempFlatness + currFlatness > 3.25f ) &&
924 69 : ( st->acelpFramesCount >= 6 ) )
925 : {
926 8 : dsnr = 2.0f;
927 : }
928 :
929 1050 : if ( ( st->sr_core == INT_FS_12k8 ) && ( offset < 74.0f ) && ( non_staX > 5.0f ) && ( snr_acelp >= snr_tcx - 4 ) && st->acelpFramesCount >= 1 && ( ( ( st->hSpMusClas->lps > st->hSpMusClas->lpm ) && mean( voicing_fr, 4 ) >= 0.3f ) || ( st->acelpFramesCount >= 6 && ( st->hSpMusClas->lps > st->hSpMusClas->lpm - 1.5f ) ) ) && ( st->sp_aud_decision0 == 0 ) && st->vad_flag )
930 : {
931 : /* Fine tuned across various databases based on various metrics to detect TCX frames in speech.*/
932 0 : dsnr = 4.0f;
933 : }
934 :
935 1050 : if ( st->flag_noisy_speech_snr )
936 : {
937 0 : if ( st->vad_flag || st->Opt_DTX_ON )
938 : {
939 0 : dsnr += 2.f;
940 : }
941 : else
942 : {
943 0 : dsnr -= 2.f;
944 : }
945 : }
946 :
947 1050 : if ( st->sr_core == INT_FS_12k8 && ( non_staX < 2.f || ( st->flag_noisy_speech_snr == 0 && st->vad_flag == 1 && offset == -128.f && st->acelpFramesCount >= 6 ) ) && ( st->last_core == ACELP_CORE || st->last_core == TCX_20_CORE ) )
948 : {
949 0 : st->core = st->last_core;
950 : }
951 1050 : else if ( snr_acelp + dsnr > snr_tcx )
952 : {
953 604 : st->core = ACELP_CORE;
954 604 : st->acelpFramesCount = min( MAX16B - 1, st->acelpFramesCount + 1 );
955 : }
956 : else
957 : {
958 446 : st->core = TCX_20_CORE;
959 446 : st->acelpFramesCount = 0;
960 : }
961 : #ifdef DEBUGGING
962 : if ( st->force != -1 )
963 : {
964 : if ( st->force == FORCE_SPEECH )
965 : {
966 : st->core = ACELP_CORE;
967 : }
968 : else
969 : {
970 : st->core = TCX_20_CORE;
971 : }
972 : }
973 : #endif
974 : }
975 :
976 : /* Fixed Decision (using -C) */
977 1325 : if ( st->acelpEnabled == 1 && st->tcx20Enabled == 0 )
978 : {
979 0 : st->core = ACELP_CORE;
980 : }
981 :
982 1325 : if ( st->acelpEnabled == 0 && st->tcx20Enabled == 1 )
983 : {
984 0 : st->core = TCX_20_CORE;
985 : }
986 :
987 1325 : st->prevTempFlatness = currFlatness;
988 :
989 1325 : return;
990 : }
991 :
992 : /*-------------------------------------------------------------------*
993 : * BITS_ALLOC_ACELP_config_rf()
994 : *
995 : * configure channel aware mode
996 : *-------------------------------------------------------------------*/
997 :
998 0 : static void BITS_ALLOC_ACELP_config_rf(
999 : const int16_t coder_type,
1000 : float *tilt_code,
1001 : int16_t *rf_frame_type,
1002 : int16_t *rf_target_bits,
1003 : const int16_t nb_subfr,
1004 : const int16_t rf_fec_indicator,
1005 : float *pitch_buf )
1006 : {
1007 : float mean_tc, min_tilt_code, max_tilt_code;
1008 : int16_t nrgMode, ltfMode, ltpMode, gainsMode;
1009 :
1010 0 : int16_t en_partial_red = 1;
1011 : float dpit1, dpit2, dpit3;
1012 :
1013 : /* Init */
1014 0 : *rf_target_bits = 0;
1015 :
1016 : /* ----------------------------------------*
1017 : * RF frame type selection *
1018 : *-----------------------------------------*/
1019 :
1020 : /* Mean tilt code estimation */
1021 0 : mean_tc = 0;
1022 0 : mean_tc = mean( tilt_code, nb_subfr );
1023 :
1024 : /* Maximum tilt code estimation */
1025 0 : max_tilt_code = tilt_code[0];
1026 0 : maximum( tilt_code, nb_subfr, &max_tilt_code );
1027 :
1028 : /* Minimum tilt code estimation */
1029 0 : min_tilt_code = tilt_code[0];
1030 0 : minimum( tilt_code, nb_subfr, &min_tilt_code );
1031 :
1032 : /* ----------------------------------------*/
1033 : /* Decide Criticality */
1034 : /*-----------------------------------------*/
1035 0 : dpit1 = (float) fabs( pitch_buf[0] - pitch_buf[1] );
1036 0 : dpit2 = (float) fabs( pitch_buf[1] - pitch_buf[2] );
1037 0 : dpit3 = (float) fabs( pitch_buf[2] - pitch_buf[3] );
1038 :
1039 0 : if ( rf_fec_indicator == 1 )
1040 : {
1041 0 : if ( max_tilt_code > 0.48f && dpit1 <= 0.0f && dpit2 <= 0.0f && dpit3 <= 0.0f && coder_type == VOICED )
1042 : {
1043 0 : en_partial_red = 0;
1044 : }
1045 0 : if ( max_tilt_code > 0.47f && dpit1 <= 1.0f && dpit2 <= 1.0f && dpit3 <= 1.0f && coder_type == GENERIC )
1046 : {
1047 0 : en_partial_red = 0;
1048 : }
1049 : }
1050 : else
1051 : {
1052 0 : if ( max_tilt_code > 0.47 && dpit1 <= 0.25f && dpit2 <= 0.25f && dpit3 <= 0.25f && coder_type == VOICED )
1053 : {
1054 0 : en_partial_red = 0;
1055 : }
1056 0 : if ( max_tilt_code > 0.45 && dpit1 <= 1.25f && dpit2 <= 1.25f && dpit3 <= 1.25f && coder_type == GENERIC )
1057 : {
1058 0 : en_partial_red = 0;
1059 : }
1060 : }
1061 :
1062 :
1063 : /* ---------------------------------------------------------*
1064 : * Identify number of bits required as per rf frame type *
1065 : * ---------------------------------------------------------*/
1066 :
1067 : /* rf_mode, 1 bit */
1068 0 : *rf_target_bits += 1;
1069 :
1070 : /* rf_fec_offset 2 bits */
1071 0 : *rf_target_bits += 2;
1072 :
1073 : /* rf_frame_type, 3 bits */
1074 0 : *rf_target_bits += 3;
1075 :
1076 : /* LSF bits 8 + 8 bits */
1077 0 : *rf_target_bits += 16;
1078 :
1079 : /* Intialize the RF mode frame type to all-pred */
1080 0 : *rf_frame_type = RF_ALLPRED;
1081 :
1082 0 : if ( coder_type == INACTIVE || en_partial_red == 0 )
1083 : {
1084 0 : *rf_frame_type = RF_NO_DATA;
1085 : }
1086 0 : else if ( coder_type == UNVOICED || coder_type == INACTIVE )
1087 : {
1088 0 : *rf_frame_type = RF_NELP;
1089 : }
1090 0 : else if ( ( coder_type == GENERIC ) && max_tilt_code < 0.05f )
1091 : {
1092 0 : *rf_frame_type = RF_NOPRED;
1093 : }
1094 0 : else if ( ( coder_type == GENERIC ) && mean_tc < 0.3f )
1095 : {
1096 0 : *rf_frame_type = RF_GENPRED;
1097 : }
1098 :
1099 0 : nrgMode = ACELP_NRG_MODE[1][1][*rf_frame_type];
1100 0 : ltfMode = ACELP_LTF_MODE[1][1][*rf_frame_type];
1101 0 : ltpMode = ACELP_LTP_MODE[1][1][*rf_frame_type];
1102 0 : gainsMode = ACELP_GAINS_MODE[1][1][*rf_frame_type];
1103 :
1104 : /* Number of RF bits for different RF coder types */
1105 0 : switch ( *rf_frame_type )
1106 : {
1107 0 : case RF_ALLPRED:
1108 : /* Es_pred bits 3 bits, LTF: 1, pitch: 8,5,5,5, FCB: 0, gain: 7,0,7,0, Diff GFr: 4*/
1109 0 : *rf_target_bits += ( ACELP_NRG_BITS[nrgMode] + ACELP_LTF_BITS[ltfMode] + ACELP_LTP_BITS_SFR[ltpMode][0] + ACELP_LTP_BITS_SFR[ltpMode][1] + ACELP_LTP_BITS_SFR[ltpMode][2] + ACELP_LTP_BITS_SFR[ltpMode][3] + ACELP_GAINS_BITS[gainsMode] + ACELP_GAINS_BITS[gainsMode] + 2 /*2 bits for PartialCopy GainFrame*/ );
1110 0 : break;
1111 :
1112 0 : case RF_NOPRED:
1113 : /* Es_pred bits 3 bits, LTF: 0, pitch: 0, FCB: 7,7,7,7, gain: 6,0,6,0, Diff GFr: 2*/
1114 : /*bits += (3 + 0 + 0 + 28 + 12 + 2); */ /* 64 rf bits */
1115 0 : *rf_target_bits += ( ACELP_NRG_BITS[nrgMode] + ACELP_LTF_BITS[ltfMode] + 28 + ACELP_GAINS_BITS[gainsMode] + ACELP_GAINS_BITS[gainsMode] + 2 /*2 bits for PartialCopy GainFrame*/ );
1116 0 : break;
1117 :
1118 0 : case RF_GENPRED:
1119 : /* Es_pred bits 3 bits, LTF: 0, pitch: 8,0,8,0, FCB: 6,7,5,5, gain: 5,0,5,0, Diff GFr: 0*/
1120 : /*bits += (3 + 0 + 16 + 23 + 10 + 0); */ /* 72 rf bits */
1121 0 : *rf_target_bits += ( ACELP_NRG_BITS[nrgMode] + ACELP_LTF_BITS[ltfMode] + ACELP_LTP_BITS_SFR[ltpMode][0] + ACELP_LTP_BITS_SFR[ltpMode][1] + ACELP_LTP_BITS_SFR[ltpMode][2] + ACELP_LTP_BITS_SFR[ltpMode][3] + 14 + ACELP_GAINS_BITS[gainsMode] + ACELP_GAINS_BITS[gainsMode] + 2 /*2 bits for PartialCopy GainFrame*/ );
1122 0 : break;
1123 :
1124 0 : case RF_NELP:
1125 : /* gain: 19, Diff GFr: 5 */
1126 : /*bits += (19 + 5); */
1127 0 : *rf_target_bits += ( 19 + NUM_BITS_SHB_FRAMEGAIN );
1128 0 : break;
1129 :
1130 0 : case RF_NO_DATA:
1131 0 : *rf_target_bits = 6;
1132 0 : break;
1133 0 : default:
1134 0 : assert( !"RF_Frame_type does not belong to ACELP Partial copy frame types possible!" );
1135 : break;
1136 : }
1137 :
1138 0 : return;
1139 : }
1140 :
1141 :
1142 : /*-------------------------------------------------------------------*
1143 : * BITS_ALLOC_TCX_config_rf()
1144 : *
1145 : * configure channel aware mode
1146 : *-------------------------------------------------------------------*/
1147 :
1148 0 : static void BITS_ALLOC_TCX_config_rf( int16_t *rf_frame_type, int16_t *rf_target_bits, const int16_t PLC_Mode, const int16_t coder_type, const int16_t last_core, const int16_t TD_Mode )
1149 : {
1150 : /* Init: rf_mode + rf_fec_offset + rf_frame_type */
1151 0 : *rf_target_bits = 1 + 2 + 3;
1152 :
1153 0 : if ( coder_type == INACTIVE || last_core == ACELP_CORE )
1154 : {
1155 0 : *rf_frame_type = RF_NO_DATA;
1156 : }
1157 : else
1158 : {
1159 : /* classification */
1160 0 : *rf_target_bits += 2;
1161 :
1162 0 : if ( PLC_Mode )
1163 : {
1164 : /* TCX global gain = 7 bits */
1165 0 : *rf_target_bits += 7;
1166 0 : *rf_frame_type = RF_TCXFD;
1167 : }
1168 : else
1169 : {
1170 : /* pitch and gain */
1171 : /* LTP data */
1172 0 : if ( TD_Mode )
1173 : {
1174 0 : *rf_target_bits += 9;
1175 0 : *rf_frame_type = RF_TCXTD2;
1176 : }
1177 : else
1178 : {
1179 0 : *rf_target_bits += 9;
1180 0 : *rf_frame_type = RF_TCXTD1;
1181 : }
1182 : }
1183 :
1184 0 : if ( *rf_frame_type == RF_TCXFD )
1185 : {
1186 : /* TCXFD: LSF bits 5 + 4 + 4 bits */
1187 : /* only embed LSF for FD concealment */
1188 0 : *rf_target_bits += TCXLPC_NUMBITS;
1189 : }
1190 : }
1191 0 : return;
1192 : }
|