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 5770849 : 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 5770849 : coder_type = st->coder_type;
84 5770849 : if ( st->core_brate == SID_2k40 )
85 : {
86 5406 : coder_type = INACTIVE;
87 : }
88 :
89 5770849 : if ( coder_type == AUDIO && st->GSC_IVAS_mode > 0 )
90 : {
91 210460 : coder_type = GENERIC;
92 : }
93 :
94 : /* initialize */
95 5770849 : no_param_lpc = 0;
96 :
97 : /* Find the number of bits for LSF quantization */
98 5770849 : nBits = 0;
99 5770849 : if ( st->core_brate == SID_2k40 )
100 : {
101 5406 : nBits = LSF_BITS_CNG;
102 : }
103 : else
104 : {
105 5765443 : if ( st->nelp_mode_dec == 0 && st->ppp_mode_dec == 0 )
106 : {
107 5765010 : nBits = st->acelp_cfg.lsf_bits;
108 : }
109 433 : else if ( st->nelp_mode_dec == 1 )
110 : {
111 308 : if ( coder_type == UNVOICED )
112 : {
113 308 : if ( st->bwidth == NB )
114 : {
115 168 : nBits = 32;
116 : }
117 : else
118 : {
119 140 : nBits = 30;
120 : }
121 : }
122 : }
123 125 : else if ( st->ppp_mode_dec == 1 )
124 : {
125 125 : nBits = 26;
126 : }
127 : }
128 :
129 : /* LSF de-quantization */
130 5770849 : 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 5770849 : lsf2lsp( lsf_new, lsp_new, M, st->sr_core );
135 :
136 : /* set seed_acelp used in UC mode */
137 5770849 : if ( coder_type == UNVOICED && st->element_mode > EVS_MONO )
138 : {
139 64826 : st->seed_acelp = 0;
140 64826 : 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 5770849 : if ( st->core_brate == SID_2k40 )
148 : {
149 : /* return if SID frame (conversion to A(z) done in the calling function) */
150 5406 : return;
151 : }
152 :
153 : /*-------------------------------------------------------------------------------------*
154 : * FEC - update adaptive LSF mean vector
155 : *-------------------------------------------------------------------------------------*/
156 :
157 98012531 : for ( i = 0; i < M; i++ )
158 : {
159 92247088 : st->lsf_adaptive_mean[i] = ( st->lsfoldbfi1[i] + st->lsfoldbfi0[i] + lsf_new[i] ) / 3;
160 : }
161 :
162 5765443 : if ( st->prev_bfi && coder_type == TRANSITION && ( tc_subfr == ( st->L_frame - L_SUBFR ) ) )
163 : {
164 3084 : lsf_diff = st->sr_core / (float) ( 2 * ( M + 1 ) );
165 3084 : st->lsf_old[0] = lsf_diff;
166 :
167 49344 : for ( i = 1; i < M; i++ )
168 : {
169 46260 : st->lsf_old[i] = st->lsf_old[i - 1] + lsf_diff;
170 : }
171 :
172 3084 : 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 5765443 : if ( st->rate_switching_reset )
181 : {
182 : /* extrapolation in case of unstable LSF convert */
183 5 : mvr2r( lsp_new, st->lsp_old, M );
184 5 : mvr2r( lsf_new, st->lsf_old, M );
185 : }
186 :
187 5765443 : lsf_mid_dec( st, lsp_new, coder_type, lsp_mid );
188 :
189 5765443 : if ( !( st->prev_bfi && ( coder_type == TRANSITION ) && ( tc_subfr == ( st->L_frame - L_SUBFR ) ) ) )
190 : {
191 5762359 : if ( st->prev_bfi )
192 : {
193 : /* check, if LSP interpolation can be relaxed */
194 134941 : lsp2a_stab( st->lsp_old, tmp_old, M );
195 134941 : enr_old = enr_1_Az( tmp_old, 2 * L_SUBFR );
196 :
197 134941 : lsp2a_stab( lsp_new, tmp_new, M );
198 134941 : enr_new = enr_1_Az( tmp_new, 2 * L_SUBFR );
199 :
200 134941 : if ( enr_new / enr_old < 0.3f )
201 : {
202 12963 : st->relax_prev_lsf_interp = -1;
203 12963 : if ( st->clas_dec == UNVOICED_CLAS || st->clas_dec == SIN_ONSET || st->clas_dec == INACTIVE_CLAS || coder_type == GENERIC || coder_type == TRANSITION )
204 : {
205 12682 : st->relax_prev_lsf_interp = 1;
206 : }
207 : }
208 : }
209 : }
210 :
211 5765443 : if ( st->last_core == HQ_CORE && st->core == ACELP_CORE )
212 : {
213 : /* update old LSPs/LSFs in case of HQ->ACELP core switching */
214 6696 : mvr2r( lsp_mid, st->lsp_old, M );
215 6696 : lsp2lsf( lsp_mid, st->lsf_old, M, st->sr_core );
216 : }
217 :
218 5765443 : if ( tdm_low_rate_mode == 1 && coder_type > UNVOICED )
219 : {
220 536 : 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 536 : 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 5764907 : 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 5765443 : st->stab_fac = lsf_stab( lsf_new, st->lsf_old, 0, st->L_frame );
241 :
242 5765443 : return;
243 : }
244 :
245 :
246 : /*------------------------------------------------------------------------------------------*
247 : * lsf_end_dec()
248 : *
249 : * De-quantize frame end LSF vector
250 : *------------------------------------------------------------------------------------------*/
251 :
252 11667927 : 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 11667927 : 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 11667927 : flag_1bit_gran = ( st->element_mode > EVS_MONO );
286 11667927 : nBits = nBits_in;
287 11667927 : *nb_indices = 0;
288 :
289 11667927 : 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 2007758 : coder_type = get_next_indice( st, 1 );
293 2007758 : coder_type += 2;
294 2007758 : if ( coder_type == GENERIC || ( coder_type == VOICED && flag_1bit_gran == 1 ) )
295 : {
296 1996397 : nBits--;
297 : }
298 : }
299 : else
300 : {
301 9660169 : coder_type = coder_type_org;
302 : }
303 :
304 : /*--------------------------------------------------------------------------------*
305 : * LSF de-quantization of SID frames
306 : *--------------------------------------------------------------------------------*/
307 :
308 11667927 : if ( st->core_brate == SID_2k40 )
309 : {
310 5406 : dqlsf_CNG( st, qlsf );
311 5406 : v_sort( qlsf, 0, M - 1 );
312 5406 : reorder_lsf( qlsf, MODE1_LSF_GAP, M, st->sr_core );
313 :
314 5406 : return;
315 : }
316 :
317 11662521 : 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 11662521 : 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 11662521 : p_lpc_param = lpc_param;
331 :
332 11662521 : 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 5719 : predmode += 3;
336 : }
337 :
338 11662521 : if ( predmode == 0 )
339 : {
340 548402 : safety_net = 1;
341 : }
342 11114119 : else if ( predmode == 1 )
343 : {
344 8491701 : safety_net = 0;
345 : }
346 : else
347 : {
348 2622418 : if ( st->codec_mode == MODE2 || st->core == TCX_20_CORE || st->core == TCX_10_CORE )
349 : {
350 : /* read from param_lpc */
351 1460070 : safety_net = p_lpc_param[0];
352 1460070 : p_lpc_param++;
353 1460070 : *nb_indices += 1;
354 : }
355 : else
356 : {
357 1162348 : safety_net = get_next_indice( st, 1 );
358 : }
359 : }
360 :
361 11662521 : st->safety_net = safety_net;
362 :
363 :
364 : /* Make sure there are the correct bit allocations */
365 11662521 : if ( st->idchan == 1 && predmode > 2 )
366 : {
367 : /* use same AR prediction bit allocation for intra and inter modes*/
368 5719 : lsf_allocate( nBits - 1, mode_lvq, 9, &stages0, &stages1, levels0, levels1, bits0, bits1 );
369 5719 : stages0 = stages1;
370 5719 : mvs2s( levels1, levels0, stages0 );
371 5719 : mvs2s( bits1, bits0, stages0 );
372 : }
373 :
374 : /*--------------------------------------------------------------------------*
375 : * Read indices from array
376 : *--------------------------------------------------------------------------*/
377 :
378 11662521 : if ( st->sr_core == INT_FS_16k && coder_type == VOICED && flag_1bit_gran == 0 )
379 : {
380 : /* BC-TCVQ - only in VOICED_WB@16kHz */
381 21976 : if ( st->codec_mode == MODE2 || st->core == TCX_20_CORE || st->core == TCX_10_CORE )
382 : {
383 10615 : *nb_indices = 10;
384 10615 : TCQIdx[0] = safety_net;
385 106150 : for ( i = 1; i < *nb_indices; i++ )
386 : {
387 95535 : TCQIdx[i] = *p_lpc_param++;
388 : }
389 : }
390 : else
391 : {
392 11361 : Bit_alloc1 = &BC_TCVQ_BIT_ALLOC_40B[1];
393 11361 : TCQIdx[0] = safety_net;
394 136332 : for ( i = 0; i < M / 2 + 3; i++ )
395 : {
396 124971 : TCQIdx[i + 1] = get_next_indice( st, Bit_alloc1[i] );
397 : }
398 : }
399 : }
400 : else
401 : {
402 11640545 : if ( safety_net )
403 : {
404 1312844 : stages = stages0;
405 1312844 : levels = levels0;
406 1312844 : bits = bits0;
407 : }
408 : else
409 : {
410 10327701 : stages = stages1;
411 10327701 : levels = levels1;
412 10327701 : bits = bits1;
413 : }
414 :
415 11640545 : if ( st->codec_mode == MODE2 || st->core == TCX_20_CORE || st->core == TCX_10_CORE )
416 : {
417 10779465 : for ( i = 0; i < stages - 1; i++ )
418 : {
419 4893002 : num_bits = bits[i];
420 4893002 : lindice[i + 1] = *p_lpc_param++;
421 : }
422 :
423 5886463 : cumleft = levels[stages - 1];
424 17659389 : while ( cumleft > 0 )
425 : {
426 11772926 : if ( cumleft > LEN_INDICE )
427 : {
428 5886463 : cumleft -= LEN_INDICE;
429 : }
430 : else
431 : {
432 5886463 : cumleft = 0;
433 : }
434 :
435 11772926 : lindice[i + 1] = *p_lpc_param++;
436 11772926 : i++;
437 : }
438 5886463 : *nb_indices += i;
439 : }
440 : else
441 : {
442 13197814 : for ( i = 0; i < stages - 1; i++ )
443 : {
444 7443732 : num_bits = bits[i];
445 7443732 : lindice[i + 1] = get_next_indice( st, num_bits );
446 : }
447 :
448 5754082 : cumleft = levels[stages - 1];
449 21364737 : while ( cumleft > 0 )
450 : {
451 15610655 : if ( cumleft > LEN_INDICE )
452 : {
453 9856573 : cumleft -= LEN_INDICE;
454 9856573 : num_bits = LEN_INDICE;
455 : }
456 : else
457 : {
458 5754082 : num_bits = cumleft;
459 5754082 : cumleft = 0;
460 : }
461 :
462 15610655 : lindice[i + 1] = get_next_indice( st, num_bits );
463 15610655 : i++;
464 : }
465 : }
466 : }
467 :
468 11662521 : if ( st->reset_mem_AR == 1 )
469 : {
470 1088 : for ( i = 0; i < M; i++ )
471 : {
472 1024 : st->mem_AR[i] = ModeMeans[mode_lvq][i];
473 : }
474 64 : st->reset_mem_AR = 0;
475 : }
476 :
477 : /*------------------------------------------------------------------------------------------*
478 : * De-quantize LSF vector
479 : *------------------------------------------------------------------------------------------*/
480 :
481 11662521 : *LSF_Q_prediction = SAFETY_NET;
482 :
483 : /* VOICED_WB@16kHz */
484 11662521 : if ( st->sr_core == INT_FS_16k && coder_type == VOICED && flag_1bit_gran == 0 )
485 : {
486 : /* BC-TCVQ decoder */
487 21976 : safety_net = qlsf_ARSN_tcvq_Dec_16k( qlsf, TCQIdx, nBits - 1 );
488 :
489 : /* Update mem_MA */
490 21976 : mvr2r( qlsf, st->mem_MA, M );
491 :
492 21976 : if ( safety_net )
493 : {
494 3616 : mvr2r( ModeMeans[mode_lvq], pred0, M );
495 : }
496 : else
497 : {
498 312120 : for ( i = 0; i < M; i++ )
499 : {
500 293760 : pred0[i] = ModeMeans[mode_lvq][i] + Predictors[mode_lvq_p][i] * ( st->mem_AR[i] - ModeMeans[mode_lvq][i] );
501 : }
502 18360 : *LSF_Q_prediction = AUTO_REGRESSIVE;
503 : }
504 21976 : v_add( qlsf, pred0, qlsf, M );
505 : }
506 : else
507 : {
508 : /* Safety-net */
509 11640545 : mvr2r( ModeMeans[mode_lvq], pred0, M );
510 :
511 : /* for mem_MA update */
512 197889265 : for ( i = 0; i < M; i++ )
513 : {
514 186248720 : 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 11640545 : if ( st->tdm_LRTD_flag == 0 && st->idchan == 1 && tdm_lsfQ_PCh != NULL )
519 : {
520 5719 : tdm_SCh_LSF_intra_pred( st->element_brate, tdm_lsfQ_PCh, pred3 );
521 : }
522 :
523 11640545 : if ( safety_net )
524 : {
525 : /* LVQ */
526 1312844 : if ( st->tdm_LRTD_flag == 0 && st->idchan == 1 && tdm_lsfQ_PCh != NULL )
527 : {
528 :
529 : /* intra mode*/
530 6252 : st->BER_detect = st->BER_detect |
531 3126 : vq_dec_lvq( 0, qlsf, &lindice[1], stages0, M, 9, /*mode_lvq_p,*/ levels0[stages0 - 1] );
532 :
533 3126 : v_add( qlsf, pred3, qlsf, M );
534 3126 : v_sub( qlsf, pred1, st->mem_MA, M );
535 : }
536 : else
537 : {
538 2619436 : st->BER_detect = st->BER_detect |
539 1309718 : vq_dec_lvq( 1, qlsf, &lindice[1], stages0, M, mode_lvq, levels0[stages0 - 1] );
540 :
541 1309718 : v_add( qlsf, pred0, qlsf, M );
542 1309718 : v_sub( qlsf, pred1, st->mem_MA, M );
543 : }
544 : }
545 : else
546 : {
547 10327701 : if ( predmode == 4 )
548 : {
549 2263 : mode_lvq_p = 9;
550 2263 : predmode = 2;
551 : }
552 20655402 : st->BER_detect = st->BER_detect |
553 10327701 : vq_dec_lvq( 0, qlsf, &lindice[1], stages1, M, mode_lvq_p, levels1[stages1 - 1] );
554 :
555 10327701 : if ( predmode == 1 || ( predmode == 4 ) ) /* MA only */
556 : {
557 8491701 : mvr2r( qlsf, st->mem_MA, M );
558 8491701 : v_add( qlsf, pred1, qlsf, M );
559 8491701 : *LSF_Q_prediction = MOVING_AVERAGE;
560 : }
561 : else
562 : {
563 : /* AR */
564 31212000 : for ( i = 0; i < M; i++ )
565 : {
566 29376000 : pred2[i] = pred0[i] + Predictors[mode_lvq_p][i] * ( st->mem_AR[i] - pred0[i] );
567 : }
568 1836000 : v_add( qlsf, pred2, qlsf, M );
569 1836000 : v_sub( qlsf, pred1, st->mem_MA, M );
570 1836000 : *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 11662521 : v_sort( qlsf, 0, M - 1 );
584 :
585 : /* Verify stability */
586 11662521 : reorder_lsf( qlsf, MODE1_LSF_GAP, M, st->sr_core );
587 :
588 : /* Update predictor memory */
589 11662521 : mvr2r( qlsf, st->mem_AR, M );
590 :
591 11662521 : st->mode_lvq = mode_lvq;
592 :
593 11662521 : return;
594 : }
595 :
596 :
597 : /*-------------------------------------------------------------------*
598 : * lsf_mid_dec()
599 : *
600 : * Decode mid-frame LSFs
601 : *-------------------------------------------------------------------*/
602 :
603 5765443 : 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 5765443 : const float *ratio = NULL;
614 : int16_t bad_spacing;
615 :
616 5765443 : bad_spacing = 0;
617 :
618 : /* Convert LSPs to LSFs */
619 5765443 : lsp2lsf( st->lsp_old, qlsf0, M, st->sr_core );
620 5765443 : lsp2lsf( lsp_new, qlsf1, M, st->sr_core );
621 :
622 : /* Codebook selection */
623 5765443 : if ( st->ppp_mode_dec == 1 )
624 : {
625 125 : nb_bits = 1;
626 125 : ratio = &( tbl_mid_voi_wb_1b[0] );
627 : }
628 5765318 : else if ( st->nelp_mode_dec == 1 )
629 : {
630 308 : nb_bits = 4;
631 308 : ratio = &( tbl_mid_unv_wb_4b[0] );
632 : }
633 : else
634 : {
635 5765010 : nb_bits = st->acelp_cfg.mid_lsf_bits;
636 :
637 : /* codebook selection */
638 5765010 : if ( coder_type == VOICED )
639 : {
640 256666 : switch ( nb_bits )
641 : {
642 52946 : case 5:
643 : {
644 52946 : ratio = tbl_mid_voi_wb_5b;
645 52946 : break;
646 : }
647 203720 : case 4:
648 : {
649 203720 : ratio = tbl_mid_voi_wb_4b;
650 203720 : break;
651 : }
652 0 : case 1:
653 : {
654 0 : ratio = tbl_mid_voi_wb_1b;
655 0 : break;
656 : }
657 : }
658 256666 : }
659 5508344 : else if ( coder_type == UNVOICED )
660 : {
661 65065 : ratio = tbl_mid_unv_wb_5b;
662 : }
663 : else
664 : {
665 : /* GENERIC, TRANSITION, AUDIO and INACTIVE */
666 5443279 : switch ( nb_bits )
667 : {
668 4564593 : case 5:
669 : {
670 4564593 : ratio = tbl_mid_gen_wb_5b;
671 4564593 : break;
672 : }
673 14315 : case 4:
674 : {
675 14315 : ratio = tbl_mid_gen_wb_4b;
676 14315 : break;
677 : }
678 864371 : case 2:
679 : {
680 864371 : ratio = tbl_mid_gen_wb_2b;
681 864371 : break;
682 : }
683 : }
684 5765443 : }
685 : }
686 :
687 : /* Retrieve mid-frame LSF index */
688 5765443 : idx = get_next_indice( st, nb_bits );
689 :
690 : /* Calculation of mid-LSF vector */
691 98012531 : for ( j = 0; j < M; j++ )
692 : {
693 92247088 : 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 5765443 : if ( st->mid_lsf_int == 1 )
698 : {
699 10361534 : for ( j = 1; j < M; j++ )
700 : {
701 9714047 : if ( qlsf[j] < qlsf[j - 1] )
702 : {
703 239 : bad_spacing = 1;
704 239 : break;
705 : }
706 : }
707 : }
708 :
709 : /* Redo mid-LSF interpolation with 0.4 in case of LSF instability */
710 5765443 : if ( st->prev_bfi || ( st->mid_lsf_int == 1 && bad_spacing ) )
711 : {
712 2349094 : for ( j = 0; j < M; j++ )
713 : {
714 : /* redo mid-LSF interpolation with 0.4 */
715 2210912 : qlsf[j] = 0.4f * qlsf0[j] + 0.6f * qlsf1[j];
716 :
717 : /* ensure correct ordering of LSF indices */
718 2210912 : if ( j > 0 && j < M && qlsf[j] < qlsf[j - 1] + LSF_GAP_MID )
719 : {
720 6057 : 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 95663437 : for ( j = 0; j < M; j++ )
728 : {
729 90036176 : if ( j > 0 && j < M && qlsf[j] < qlsf[j - 1] + LSF_GAP_MID )
730 : {
731 481074 : qlsf[j] = qlsf[j - 1] + LSF_GAP_MID;
732 : }
733 : }
734 : }
735 :
736 5765443 : if ( st->prev_bfi )
737 : {
738 : /* continue redoing mid-LSF interpolation with 0.4 in order not to propagate the error */
739 138025 : st->mid_lsf_int = 1;
740 : }
741 :
742 5765443 : if ( st->safety_net )
743 : {
744 : /* safety-net encountered -> stop redoing mid-LSF interpolation with 0.4 */
745 864997 : st->mid_lsf_int = 0;
746 : }
747 :
748 5765443 : reorder_lsf( qlsf, LSF_GAP_MID, M, st->sr_core );
749 : /* convert back to LSPs */
750 5765443 : lsf2lsp( qlsf, lsp_mid, M, st->sr_core );
751 :
752 5765443 : 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 5406 : 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 5406 : indice[0] = get_next_indice( st, 4 );
775 5406 : indice[1] = get_next_indice( st, LEN_INDICE );
776 5406 : indice[2] = get_next_indice( st, LSF_BITS_CNG - 4 - LEN_INDICE );
777 :
778 10812 : st->BER_detect = st->BER_detect |
779 5406 : 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 5406 : v_add( lsf_q, &CNG_SN1[indice[0] * M], lsf_q, M );
786 :
787 5406 : 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 5406 : return;
793 : }
|