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 "basop_proto_func.h"
46 : #include "ivas_prot.h"
47 : #include "ivas_rom_com.h"
48 : #include "wmc_auto.h"
49 :
50 :
51 : /*---------------------------------------------------------------------*
52 : * Local function prototypes
53 : *---------------------------------------------------------------------*/
54 :
55 : static void lsf_mid_dec( Decoder_State *st, const float lsp_new[], const int16_t coder_type, float lsp_mid[] );
56 :
57 : static void dqlsf_CNG( Decoder_State *st, float *lsf_q );
58 :
59 :
60 : /*---------------------------------------------------------------------*
61 : * lsf_dec()
62 : *
63 : * LSF decoder
64 : *---------------------------------------------------------------------*/
65 :
66 460827 : void lsf_dec(
67 : Decoder_State *st, /* i/o: State structure */
68 : const int16_t tc_subfr, /* i : TC subframe index */
69 : float *Aq, /* o : quantized A(z) for 4 subframes */
70 : int16_t *LSF_Q_prediction, /* o : LSF prediction mode */
71 : float *lsf_new, /* o : de-quantized LSF vector */
72 : float *lsp_new, /* o : de-quantized LSP vector */
73 : float *lsp_mid, /* o : de-quantized mid-frame LSP vector */
74 : const int16_t tdm_low_rate_mode, /* i : secondary channel low rate mode flag */
75 : const float tdm_lsfQ_PCh[M] /* i : Q LSFs for primary channel */
76 : )
77 : {
78 : int16_t i, nBits, coder_type, no_param_lpc;
79 : int16_t param_lpc[NPRM_LPC_NEW];
80 : float tmp_old[M + 1], tmp_new[M + 1], enr_old, enr_new;
81 : float lsf_diff;
82 :
83 460827 : coder_type = st->coder_type;
84 460827 : if ( st->core_brate == SID_2k40 )
85 : {
86 1299 : coder_type = INACTIVE;
87 : }
88 :
89 460827 : if ( coder_type == AUDIO && st->GSC_IVAS_mode > 0 )
90 : {
91 14250 : coder_type = GENERIC;
92 : }
93 :
94 : /* initialize */
95 460827 : no_param_lpc = 0;
96 :
97 : /* Find the number of bits for LSF quantization */
98 460827 : nBits = 0;
99 460827 : if ( st->core_brate == SID_2k40 )
100 : {
101 1299 : nBits = LSF_BITS_CNG;
102 : }
103 : else
104 : {
105 459528 : if ( st->nelp_mode_dec == 0 && st->ppp_mode_dec == 0 )
106 : {
107 459528 : nBits = st->acelp_cfg.lsf_bits;
108 : }
109 0 : else if ( st->nelp_mode_dec == 1 )
110 : {
111 0 : if ( coder_type == UNVOICED )
112 : {
113 0 : if ( st->bwidth == NB )
114 : {
115 0 : nBits = 32;
116 : }
117 : else
118 : {
119 0 : nBits = 30;
120 : }
121 : }
122 : }
123 0 : else if ( st->ppp_mode_dec == 1 )
124 : {
125 0 : nBits = 26;
126 : }
127 : }
128 :
129 : /* LSF de-quantization */
130 460827 : lsf_end_dec( st, coder_type, st->bwidth, nBits, lsf_new, param_lpc, LSF_Q_prediction, &no_param_lpc,
131 : tdm_lsfQ_PCh );
132 :
133 : /* convert quantized LSFs to LSPs */
134 460827 : lsf2lsp( lsf_new, lsp_new, M, st->sr_core );
135 :
136 : /* set seed_acelp used in UC mode */
137 460827 : if ( coder_type == UNVOICED && st->element_mode > EVS_MONO )
138 : {
139 6057 : st->seed_acelp = 0;
140 6057 : for ( i = no_param_lpc - 1; i >= 0; i-- )
141 : {
142 : /* rightshift before *seed_acelp+param_lpc[i] to avoid overflows*/
143 0 : st->seed_acelp = (int16_t) ( ( ( ( st->seed_acelp ) >> 1 ) + param_lpc[i] ) * 31821L + 13849L );
144 : }
145 : }
146 :
147 460827 : if ( st->core_brate == SID_2k40 )
148 : {
149 : /* return if SID frame (conversion to A(z) done in the calling function) */
150 1299 : return;
151 : }
152 :
153 : /*-------------------------------------------------------------------------------------*
154 : * FEC - update adaptive LSF mean vector
155 : *-------------------------------------------------------------------------------------*/
156 :
157 7811976 : for ( i = 0; i < M; i++ )
158 : {
159 7352448 : st->lsf_adaptive_mean[i] = ( st->lsfoldbfi1[i] + st->lsfoldbfi0[i] + lsf_new[i] ) / 3;
160 : }
161 :
162 459528 : if ( st->prev_bfi && coder_type == TRANSITION && ( tc_subfr == ( st->L_frame - L_SUBFR ) ) )
163 : {
164 138 : lsf_diff = st->sr_core / (float) ( 2 * ( M + 1 ) );
165 138 : st->lsf_old[0] = lsf_diff;
166 :
167 2208 : for ( i = 1; i < M; i++ )
168 : {
169 2070 : st->lsf_old[i] = st->lsf_old[i - 1] + lsf_diff;
170 : }
171 :
172 138 : lsf2lsp( st->lsf_old, st->lsp_old, M, st->sr_core );
173 : }
174 :
175 : /*-------------------------------------------------------------------------------------*
176 : * Mid-frame LSF decoding
177 : * LSP interpolation and conversion of LSPs to A(z)
178 : *-------------------------------------------------------------------------------------*/
179 :
180 459528 : if ( st->rate_switching_reset )
181 : {
182 : /* extrapolation in case of unstable LSF convert */
183 0 : mvr2r( lsp_new, st->lsp_old, M );
184 0 : mvr2r( lsf_new, st->lsf_old, M );
185 : }
186 :
187 459528 : lsf_mid_dec( st, lsp_new, coder_type, lsp_mid );
188 :
189 459528 : if ( !( st->prev_bfi && ( coder_type == TRANSITION ) && ( tc_subfr == ( st->L_frame - L_SUBFR ) ) ) )
190 : {
191 459390 : if ( st->prev_bfi )
192 : {
193 : /* check, if LSP interpolation can be relaxed */
194 7083 : lsp2a_stab( st->lsp_old, tmp_old, M );
195 7083 : enr_old = enr_1_Az( tmp_old, 2 * L_SUBFR );
196 :
197 7083 : lsp2a_stab( lsp_new, tmp_new, M );
198 7083 : enr_new = enr_1_Az( tmp_new, 2 * L_SUBFR );
199 :
200 7083 : if ( enr_new / enr_old < 0.3f )
201 : {
202 966 : st->relax_prev_lsf_interp = -1;
203 966 : if ( st->clas_dec == UNVOICED_CLAS || st->clas_dec == SIN_ONSET || st->clas_dec == INACTIVE_CLAS || coder_type == GENERIC || coder_type == TRANSITION )
204 : {
205 918 : st->relax_prev_lsf_interp = 1;
206 : }
207 : }
208 : }
209 : }
210 :
211 459528 : if ( st->last_core == HQ_CORE && st->core == ACELP_CORE )
212 : {
213 : /* update old LSPs/LSFs in case of HQ->ACELP core switching */
214 759 : mvr2r( lsp_mid, st->lsp_old, M );
215 759 : lsp2lsf( lsp_mid, st->lsf_old, M, st->sr_core );
216 : }
217 :
218 459528 : if ( tdm_low_rate_mode == 1 && coder_type > UNVOICED )
219 : {
220 0 : if ( st->active_cnt == 1 )
221 : {
222 0 : mvr2r( lsp_mid, st->lsp_old, M );
223 0 : lsp2lsf( lsp_mid, st->lsf_old, M, st->sr_core );
224 0 : mvr2r( lsp_new, lsp_mid, M );
225 : }
226 :
227 : /* LSP interpolation and conversion of LSPs to A(z) - two-subframe mode */
228 0 : int_lsp4( st->L_frame, st->lsp_old, lsp_mid, lsp_new, Aq, M, -2 );
229 : }
230 : else
231 : {
232 : /* LSP interpolation and conversion of LSPs to A(z) */
233 459528 : int_lsp4( st->L_frame, st->lsp_old, lsp_mid, lsp_new, Aq, M, st->relax_prev_lsf_interp );
234 : }
235 :
236 : /*------------------------------------------------------------------*
237 : * Check LSF stability (distance between old LSFs and current LSFs)
238 : *------------------------------------------------------------------*/
239 :
240 459528 : st->stab_fac = lsf_stab( lsf_new, st->lsf_old, 0, st->L_frame );
241 :
242 459528 : return;
243 : }
244 :
245 :
246 : /*------------------------------------------------------------------------------------------*
247 : * lsf_end_dec()
248 : *
249 : * De-quantize frame end LSF vector
250 : *------------------------------------------------------------------------------------------*/
251 :
252 788037 : void lsf_end_dec(
253 : Decoder_State *st, /* i/o: decoder state structure */
254 : const int16_t coder_type_org, /* i : coding type */
255 : const int16_t bwidth, /* i : input signal bandwidth */
256 : const int16_t nBits_in, /* i : number of bits used for ISF quantization*/
257 : float *qlsf, /* o : quantized LSFs in the cosine domain */
258 : int16_t *lpc_param, /* i : LPC parameters */
259 : int16_t *LSF_Q_prediction, /* o : LSF prediction mode */
260 : int16_t *nb_indices, /* o : number of indices */
261 : const float tdm_lsfQ_PCh[M] /* i : Q LSFs for primary channel */
262 : )
263 : {
264 : float pred0[M]; /* Prediction for the safety-net quantizer (usually mean)*/
265 : float pred1[M], pred2[M]; /* Prediction for the predictive quantizer*/
266 : int16_t stages0; /* Amount of stages used by safety-net quantizer*/
267 : int16_t stages1; /* Amount of stages used by predictive quantizer*/
268 : int16_t levels0[MAX_VQ_STAGES]; /* Sizes of different codebook stages for safety-net quantizer*/
269 : int16_t levels1[MAX_VQ_STAGES]; /* Sizes of different codebook stages for predictive quantizer*/
270 : int16_t i;
271 : int16_t TCQIdx[M / 2 + 4];
272 : int16_t bits0[MAX_VQ_STAGES], bits1[MAX_VQ_STAGES];
273 : int16_t cumleft;
274 : int16_t lindice[MAX_VQ_STAGES + 3]; /* Predictor selector needs 1 bit and the LVQ indice uses 3 shorts */
275 : int16_t mode_lvq, mode_lvq_p;
276 : int16_t safety_net, predmode, stages, *levels;
277 788037 : const int16_t *Bit_alloc1 = NULL, *bits;
278 : int16_t num_bits;
279 : int16_t *p_lpc_param;
280 : int16_t nBits;
281 : int16_t coder_type;
282 : int16_t flag_1bit_gran;
283 : float pred3[M];
284 :
285 788037 : flag_1bit_gran = ( st->element_mode > EVS_MONO );
286 788037 : nBits = nBits_in;
287 788037 : *nb_indices = 0;
288 :
289 788037 : if ( coder_type_org == GENERIC && st->sr_core == INT_FS_16k && st->codec_mode == MODE1 && st->idchan == 0 )
290 : {
291 : /* this bit is used only for primary channel or mono */
292 179499 : coder_type = get_next_indice( st, 1 );
293 179499 : coder_type += 2;
294 179499 : if ( coder_type == GENERIC || ( coder_type == VOICED && flag_1bit_gran == 1 ) )
295 : {
296 178581 : nBits--;
297 : }
298 : }
299 : else
300 : {
301 608538 : coder_type = coder_type_org;
302 : }
303 :
304 : /*--------------------------------------------------------------------------------*
305 : * LSF de-quantization of SID frames
306 : *--------------------------------------------------------------------------------*/
307 :
308 788037 : if ( st->core_brate == SID_2k40 )
309 : {
310 1299 : dqlsf_CNG( st, qlsf );
311 1299 : v_sort( qlsf, 0, M - 1 );
312 1299 : reorder_lsf( qlsf, MODE1_LSF_GAP, M, st->sr_core );
313 :
314 1299 : return;
315 : }
316 :
317 786738 : find_pred_mode( &predmode, coder_type, bwidth, st->sr_core, &mode_lvq, &mode_lvq_p, st->total_brate );
318 :
319 : /*----------------------------------------------------------------*
320 : * Calculate number of stages and levels for each stage based on the allowed bit allocation
321 : * (subtract one bit for LSF predictor selection)
322 : *----------------------------------------------------------------*/
323 :
324 786738 : lsf_allocate( nBits - ( predmode >> 1 ), mode_lvq, mode_lvq_p, &stages0, &stages1, levels0, levels1, bits0, bits1 );
325 :
326 : /*--------------------------------------------------------------------------*
327 : * Select safety_net or predictive mode
328 : *--------------------------------------------------------------------------*/
329 :
330 786738 : p_lpc_param = lpc_param;
331 :
332 786738 : if ( st->tdm_LRTD_flag == 0 && st->idchan == 1 && tdm_lsfQ_PCh != NULL )
333 : {
334 : /* if secondary channel predmode is set to be > 2 */
335 306 : predmode += 3;
336 : }
337 :
338 786738 : if ( predmode == 0 )
339 : {
340 41832 : safety_net = 1;
341 : }
342 744906 : else if ( predmode == 1 )
343 : {
344 518532 : safety_net = 0;
345 : }
346 : else
347 : {
348 226374 : if ( st->codec_mode == MODE2 || st->core == TCX_20_CORE || st->core == TCX_10_CORE )
349 : {
350 : /* read from param_lpc */
351 108813 : safety_net = p_lpc_param[0];
352 108813 : p_lpc_param++;
353 108813 : *nb_indices += 1;
354 : }
355 : else
356 : {
357 117561 : safety_net = get_next_indice( st, 1 );
358 : }
359 : }
360 :
361 786738 : st->safety_net = safety_net;
362 :
363 :
364 : /* Make sure there are the correct bit allocations */
365 786738 : if ( st->idchan == 1 && predmode > 2 )
366 : {
367 : /* use same AR prediction bit allocation for intra and inter modes*/
368 306 : lsf_allocate( nBits - 1, mode_lvq, 9, &stages0, &stages1, levels0, levels1, bits0, bits1 );
369 306 : stages0 = stages1;
370 306 : mvs2s( levels1, levels0, stages0 );
371 306 : mvs2s( bits1, bits0, stages0 );
372 : }
373 :
374 : /*--------------------------------------------------------------------------*
375 : * Read indices from array
376 : *--------------------------------------------------------------------------*/
377 :
378 786738 : if ( st->sr_core == INT_FS_16k && coder_type == VOICED && flag_1bit_gran == 0 )
379 : {
380 : /* BC-TCVQ - only in VOICED_WB@16kHz */
381 1722 : if ( st->codec_mode == MODE2 || st->core == TCX_20_CORE || st->core == TCX_10_CORE )
382 : {
383 804 : *nb_indices = 10;
384 804 : TCQIdx[0] = safety_net;
385 8040 : for ( i = 1; i < *nb_indices; i++ )
386 : {
387 7236 : TCQIdx[i] = *p_lpc_param++;
388 : }
389 : }
390 : else
391 : {
392 918 : Bit_alloc1 = &BC_TCVQ_BIT_ALLOC_40B[1];
393 918 : TCQIdx[0] = safety_net;
394 11016 : for ( i = 0; i < M / 2 + 3; i++ )
395 : {
396 10098 : TCQIdx[i + 1] = get_next_indice( st, Bit_alloc1[i] );
397 : }
398 : }
399 : }
400 : else
401 : {
402 785016 : if ( safety_net )
403 : {
404 112299 : stages = stages0;
405 112299 : levels = levels0;
406 112299 : bits = bits0;
407 : }
408 : else
409 : {
410 672717 : stages = stages1;
411 672717 : levels = levels1;
412 672717 : bits = bits1;
413 : }
414 :
415 785016 : if ( st->codec_mode == MODE2 || st->core == TCX_20_CORE || st->core == TCX_10_CORE )
416 : {
417 581595 : for ( i = 0; i < stages - 1; i++ )
418 : {
419 255189 : num_bits = bits[i];
420 255189 : lindice[i + 1] = *p_lpc_param++;
421 : }
422 :
423 326406 : cumleft = levels[stages - 1];
424 979218 : while ( cumleft > 0 )
425 : {
426 652812 : if ( cumleft > LEN_INDICE )
427 : {
428 326406 : cumleft -= LEN_INDICE;
429 : }
430 : else
431 : {
432 326406 : cumleft = 0;
433 : }
434 :
435 652812 : lindice[i + 1] = *p_lpc_param++;
436 652812 : i++;
437 : }
438 326406 : *nb_indices += i;
439 : }
440 : else
441 : {
442 1057998 : for ( i = 0; i < stages - 1; i++ )
443 : {
444 599388 : num_bits = bits[i];
445 599388 : lindice[i + 1] = get_next_indice( st, num_bits );
446 : }
447 :
448 458610 : cumleft = levels[stages - 1];
449 1725765 : while ( cumleft > 0 )
450 : {
451 1267155 : if ( cumleft > LEN_INDICE )
452 : {
453 808545 : cumleft -= LEN_INDICE;
454 808545 : num_bits = LEN_INDICE;
455 : }
456 : else
457 : {
458 458610 : num_bits = cumleft;
459 458610 : cumleft = 0;
460 : }
461 :
462 1267155 : lindice[i + 1] = get_next_indice( st, num_bits );
463 1267155 : i++;
464 : }
465 : }
466 : }
467 :
468 786738 : if ( st->reset_mem_AR == 1 )
469 : {
470 0 : for ( i = 0; i < M; i++ )
471 : {
472 0 : st->mem_AR[i] = ModeMeans[mode_lvq][i];
473 : }
474 0 : st->reset_mem_AR = 0;
475 : }
476 :
477 : /*------------------------------------------------------------------------------------------*
478 : * De-quantize LSF vector
479 : *------------------------------------------------------------------------------------------*/
480 :
481 786738 : *LSF_Q_prediction = SAFETY_NET;
482 :
483 : /* VOICED_WB@16kHz */
484 786738 : if ( st->sr_core == INT_FS_16k && coder_type == VOICED && flag_1bit_gran == 0 )
485 : {
486 : /* BC-TCVQ decoder */
487 1722 : safety_net = qlsf_ARSN_tcvq_Dec_16k( qlsf, TCQIdx, nBits - 1 );
488 :
489 : /* Update mem_MA */
490 1722 : mvr2r( qlsf, st->mem_MA, M );
491 :
492 1722 : if ( safety_net )
493 : {
494 264 : mvr2r( ModeMeans[mode_lvq], pred0, M );
495 : }
496 : else
497 : {
498 24786 : for ( i = 0; i < M; i++ )
499 : {
500 23328 : pred0[i] = ModeMeans[mode_lvq][i] + Predictors[mode_lvq_p][i] * ( st->mem_AR[i] - ModeMeans[mode_lvq][i] );
501 : }
502 1458 : *LSF_Q_prediction = AUTO_REGRESSIVE;
503 : }
504 1722 : v_add( qlsf, pred0, qlsf, M );
505 : }
506 : else
507 : {
508 : /* Safety-net */
509 785016 : mvr2r( ModeMeans[mode_lvq], pred0, M );
510 :
511 : /* for mem_MA update */
512 13345272 : for ( i = 0; i < M; i++ )
513 : {
514 12560256 : pred1[i] = pred0[i] + MU_MA * st->mem_MA[i];
515 : }
516 :
517 : /* TD stereo SCh: perform intra-frame prediction with pulling-to-mean */
518 785016 : if ( st->tdm_LRTD_flag == 0 && st->idchan == 1 && tdm_lsfQ_PCh != NULL )
519 : {
520 306 : tdm_SCh_LSF_intra_pred( st->element_brate, tdm_lsfQ_PCh, pred3 );
521 : }
522 :
523 785016 : if ( safety_net )
524 : {
525 : /* LVQ */
526 112299 : if ( st->tdm_LRTD_flag == 0 && st->idchan == 1 && tdm_lsfQ_PCh != NULL )
527 : {
528 :
529 : /* intra mode*/
530 216 : st->BER_detect = st->BER_detect |
531 108 : vq_dec_lvq( 0, qlsf, &lindice[1], stages0, M, 9, /*mode_lvq_p,*/ levels0[stages0 - 1] );
532 :
533 108 : v_add( qlsf, pred3, qlsf, M );
534 108 : v_sub( qlsf, pred1, st->mem_MA, M );
535 : }
536 : else
537 : {
538 224382 : st->BER_detect = st->BER_detect |
539 112191 : vq_dec_lvq( 1, qlsf, &lindice[1], stages0, M, mode_lvq, levels0[stages0 - 1] );
540 :
541 112191 : v_add( qlsf, pred0, qlsf, M );
542 112191 : v_sub( qlsf, pred1, st->mem_MA, M );
543 : }
544 : }
545 : else
546 : {
547 672717 : if ( predmode == 4 )
548 : {
549 156 : mode_lvq_p = 9;
550 156 : predmode = 2;
551 : }
552 1345434 : st->BER_detect = st->BER_detect |
553 672717 : vq_dec_lvq( 0, qlsf, &lindice[1], stages1, M, mode_lvq_p, levels1[stages1 - 1] );
554 :
555 672717 : if ( predmode == 1 || ( predmode == 4 ) ) /* MA only */
556 : {
557 518532 : mvr2r( qlsf, st->mem_MA, M );
558 518532 : v_add( qlsf, pred1, qlsf, M );
559 518532 : *LSF_Q_prediction = MOVING_AVERAGE;
560 : }
561 : else
562 : {
563 : /* AR */
564 2621145 : for ( i = 0; i < M; i++ )
565 : {
566 2466960 : pred2[i] = pred0[i] + Predictors[mode_lvq_p][i] * ( st->mem_AR[i] - pred0[i] );
567 : }
568 154185 : v_add( qlsf, pred2, qlsf, M );
569 154185 : v_sub( qlsf, pred1, st->mem_MA, M );
570 154185 : *LSF_Q_prediction = AUTO_REGRESSIVE;
571 : }
572 : }
573 : }
574 :
575 :
576 : /*--------------------------------------------------------------------------*
577 : * Sort the quantized vector
578 : * Verify stability
579 : * Update AR-predictor memory
580 : *--------------------------------------------------------------------------*/
581 :
582 : /* Sort the quantized vector */
583 786738 : v_sort( qlsf, 0, M - 1 );
584 :
585 : /* Verify stability */
586 786738 : reorder_lsf( qlsf, MODE1_LSF_GAP, M, st->sr_core );
587 :
588 : /* Update predictor memory */
589 786738 : mvr2r( qlsf, st->mem_AR, M );
590 :
591 786738 : st->mode_lvq = mode_lvq;
592 :
593 786738 : return;
594 : }
595 :
596 :
597 : /*-------------------------------------------------------------------*
598 : * lsf_mid_dec()
599 : *
600 : * Decode mid-frame LSFs
601 : *-------------------------------------------------------------------*/
602 :
603 459528 : static void lsf_mid_dec(
604 : Decoder_State *st, /* i/o: decoder state structure */
605 : const float lsp_new[], /* i : quantized LSPs from frame end */
606 : const int16_t coder_type, /* i : coder type */
607 : float lsp_mid[] /* o : quantized LSPs */
608 : )
609 : {
610 : int16_t j, idx;
611 : int16_t nb_bits;
612 : float qlsf0[M], qlsf1[M], qlsf[M];
613 459528 : const float *ratio = NULL;
614 : int16_t bad_spacing;
615 :
616 459528 : bad_spacing = 0;
617 :
618 : /* Convert LSPs to LSFs */
619 459528 : lsp2lsf( st->lsp_old, qlsf0, M, st->sr_core );
620 459528 : lsp2lsf( lsp_new, qlsf1, M, st->sr_core );
621 :
622 : /* Codebook selection */
623 459528 : if ( st->ppp_mode_dec == 1 )
624 : {
625 0 : nb_bits = 1;
626 0 : ratio = &( tbl_mid_voi_wb_1b[0] );
627 : }
628 459528 : else if ( st->nelp_mode_dec == 1 )
629 : {
630 0 : nb_bits = 4;
631 0 : ratio = &( tbl_mid_unv_wb_4b[0] );
632 : }
633 : else
634 : {
635 459528 : nb_bits = st->acelp_cfg.mid_lsf_bits;
636 :
637 : /* codebook selection */
638 459528 : if ( coder_type == VOICED )
639 : {
640 25632 : switch ( nb_bits )
641 : {
642 5043 : case 5:
643 : {
644 5043 : ratio = tbl_mid_voi_wb_5b;
645 5043 : break;
646 : }
647 20589 : case 4:
648 : {
649 20589 : ratio = tbl_mid_voi_wb_4b;
650 20589 : break;
651 : }
652 0 : case 1:
653 : {
654 0 : ratio = tbl_mid_voi_wb_1b;
655 0 : break;
656 : }
657 : }
658 25632 : }
659 433896 : else if ( coder_type == UNVOICED )
660 : {
661 6057 : ratio = tbl_mid_unv_wb_5b;
662 : }
663 : else
664 : {
665 : /* GENERIC, TRANSITION, AUDIO and INACTIVE */
666 427839 : switch ( nb_bits )
667 : {
668 400563 : case 5:
669 : {
670 400563 : ratio = tbl_mid_gen_wb_5b;
671 400563 : break;
672 : }
673 2211 : case 4:
674 : {
675 2211 : ratio = tbl_mid_gen_wb_4b;
676 2211 : break;
677 : }
678 25065 : case 2:
679 : {
680 25065 : ratio = tbl_mid_gen_wb_2b;
681 25065 : break;
682 : }
683 : }
684 459528 : }
685 : }
686 :
687 : /* Retrieve mid-frame LSF index */
688 459528 : idx = get_next_indice( st, nb_bits );
689 :
690 : /* Calculation of mid-LSF vector */
691 7811976 : for ( j = 0; j < M; j++ )
692 : {
693 7352448 : qlsf[j] = ( 1.0f - ratio[idx * M + j] ) * qlsf0[j] + ratio[idx * M + j] * qlsf1[j];
694 : }
695 :
696 : /* check for incorrect LSF ordering */
697 459528 : if ( st->mid_lsf_int == 1 )
698 : {
699 610374 : for ( j = 1; j < M; j++ )
700 : {
701 572235 : if ( qlsf[j] < qlsf[j - 1] )
702 : {
703 15 : bad_spacing = 1;
704 15 : break;
705 : }
706 : }
707 : }
708 :
709 : /* Redo mid-LSF interpolation with 0.4 in case of LSF instability */
710 459528 : if ( st->prev_bfi || ( st->mid_lsf_int == 1 && bad_spacing ) )
711 : {
712 122961 : for ( j = 0; j < M; j++ )
713 : {
714 : /* redo mid-LSF interpolation with 0.4 */
715 115728 : qlsf[j] = 0.4f * qlsf0[j] + 0.6f * qlsf1[j];
716 :
717 : /* ensure correct ordering of LSF indices */
718 115728 : if ( j > 0 && j < M && qlsf[j] < qlsf[j - 1] + LSF_GAP_MID )
719 : {
720 309 : qlsf[j] = qlsf[j - 1] + LSF_GAP_MID;
721 : }
722 : }
723 : }
724 : else
725 : {
726 : /* otherwise, use regular LSF spacing and ordering as in the encoder */
727 7689015 : for ( j = 0; j < M; j++ )
728 : {
729 7236720 : if ( j > 0 && j < M && qlsf[j] < qlsf[j - 1] + LSF_GAP_MID )
730 : {
731 42663 : qlsf[j] = qlsf[j - 1] + LSF_GAP_MID;
732 : }
733 : }
734 : }
735 :
736 459528 : if ( st->prev_bfi )
737 : {
738 : /* continue redoing mid-LSF interpolation with 0.4 in order not to propagate the error */
739 7221 : st->mid_lsf_int = 1;
740 : }
741 :
742 459528 : if ( st->safety_net )
743 : {
744 : /* safety-net encountered -> stop redoing mid-LSF interpolation with 0.4 */
745 75639 : st->mid_lsf_int = 0;
746 : }
747 :
748 459528 : reorder_lsf( qlsf, LSF_GAP_MID, M, st->sr_core );
749 : /* convert back to LSPs */
750 459528 : lsf2lsp( qlsf, lsp_mid, M, st->sr_core );
751 :
752 459528 : return;
753 : }
754 :
755 : /*----------------------------------------------------------------------------------------------*
756 : * dqlsf_CNG()
757 : *
758 : * LSF de-quantizer for SID frames (uses 28 bits, 4 for VQ, 24 for LVQ)
759 : *
760 : * Note:
761 : * LP-CNG LSF decoder does not need to know the sampling rate,
762 : * the sampling rate data is embedded inside the LSF coefficients
763 : * If the highest order LSF coefficient (lsf_q[M-1]) is smaller than 6350 then Fs=12.8kHz
764 : * If the highest order LSF coefficient (lsf_q[M-1]) is larger than 6350 then Fs=16kHz
765 : *----------------------------------------------------------------------------------------------*/
766 :
767 1299 : static void dqlsf_CNG(
768 : Decoder_State *st, /* i/o: decoder state structure */
769 : float *lsf_q /* o : decoded LSFs */
770 : )
771 : {
772 : int16_t indice[4];
773 :
774 1299 : indice[0] = get_next_indice( st, 4 );
775 1299 : indice[1] = get_next_indice( st, LEN_INDICE );
776 1299 : indice[2] = get_next_indice( st, LSF_BITS_CNG - 4 - LEN_INDICE );
777 :
778 2598 : st->BER_detect = st->BER_detect |
779 1299 : deindex_lvq_cng( &indice[1], lsf_q, indice[0], LSF_BITS_CNG - 4 );
780 : /* The sampling frequency of the LP-CNG frame can be determined by checking the value of the highest order LSF
781 : coefficient (last coefficient of lsf_q). If the last decoded LSF coefficient (lsf_q[15]) is larger than 6350
782 : the decoded frame is WB2 with sampling rate of 16 kHz, otherwise it is sampled at 12.8kHz and contains
783 : either NB or WB LSF data. */
784 :
785 1299 : v_add( lsf_q, &CNG_SN1[indice[0] * M], lsf_q, M );
786 :
787 1299 : if ( ( ( st->L_frame == L_FRAME16k ) && ( lsf_q[M - 1] <= WB_LIMIT_LSF ) ) || ( ( st->L_frame < L_FRAME16k ) && ( lsf_q[M - 1] > WB_LIMIT_LSF ) ) )
788 : {
789 0 : st->BER_detect = 1;
790 : }
791 :
792 1299 : return;
793 : }
|