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 "cnst.h"
44 : #include "rom_com.h"
45 : #include "prot.h"
46 : #include "wmc_auto.h"
47 :
48 :
49 : /*-------------------------------------------------------------------*
50 : * getTCXMode()
51 : *
52 : * get TCX mode
53 : *--------------------------------------------------------------------*/
54 :
55 2571432 : void getTCXMode(
56 : Decoder_State *st, /* i/o: decoder memory state */
57 : Decoder_State *st0, /* i : bitstream */
58 : const int16_t MCT_flag /* i : hMCT handle allocated (1) or not (0) */
59 : )
60 : {
61 : uint16_t ind;
62 :
63 2571432 : if ( st->tcxonly )
64 : {
65 : /* get core */
66 2193264 : ind = get_next_indice( st0, 1 );
67 2193264 : st->core = ind + TCX_20_CORE;
68 :
69 : /* get class */
70 2193264 : ind = get_next_indice( st0, 2 );
71 :
72 2193264 : st->clas_dec = ONSET;
73 2193264 : if ( ind == 0 )
74 : {
75 1139394 : st->clas_dec = UNVOICED_CLAS;
76 : }
77 1053870 : else if ( ind == 1 )
78 : {
79 132912 : if ( st->last_good >= VOICED_TRANSITION )
80 : {
81 107262 : st->clas_dec = VOICED_TRANSITION;
82 : }
83 : else
84 : {
85 25650 : st->clas_dec = UNVOICED_TRANSITION;
86 : }
87 : }
88 920958 : else if ( ind == 2 )
89 : {
90 801297 : st->clas_dec = VOICED_CLAS;
91 : }
92 :
93 2193264 : st->coder_type = INACTIVE;
94 2193264 : if ( st->element_mode == IVAS_CPE_MDCT && !MCT_flag )
95 : {
96 480312 : st->VAD = get_next_indice( st0, 1 );
97 : }
98 : else
99 : {
100 1712952 : st->VAD = 0;
101 : }
102 : }
103 : else
104 : {
105 378168 : if ( st->mdct_sw == MODE1 )
106 : {
107 : /* 2 bits instead of 3 as TCX is already signaled */
108 375243 : st->core = TCX_20_CORE;
109 375243 : st->hTcxCfg->coder_type = get_next_indice( st0, 2 );
110 375243 : st->coder_type = st->hTcxCfg->coder_type;
111 : }
112 : else
113 : {
114 2925 : if ( st->mdct_sw_enable == MODE2 )
115 : {
116 2925 : if ( get_next_indice_1( st0 ) ) /* TCX */
117 : {
118 1113 : ind = get_next_indice( st0, 3 );
119 1113 : assert( !( ind & 4 ) || !"HQ_CORE encountered in dec_prm" );
120 1113 : st->core = TCX_20_CORE;
121 1113 : st->hTcxCfg->coder_type = ind;
122 1113 : st->coder_type = st->hTcxCfg->coder_type;
123 : }
124 : else /* ACELP */
125 : {
126 1812 : st->core = ACELP_CORE;
127 1812 : st->coder_type = get_next_indice( st0, 2 );
128 : }
129 : }
130 : else
131 : {
132 0 : if ( st->rf_flag == 1 )
133 : {
134 0 : if ( !st->use_partial_copy )
135 : {
136 0 : ind = get_next_indice( st0, 1 );
137 0 : if ( ind == 0 )
138 : {
139 0 : st->core = ACELP_CORE;
140 : }
141 : else
142 : {
143 0 : st->core = TCX_20_CORE;
144 0 : st->hTcxCfg->coder_type = st->coder_type;
145 : }
146 : }
147 : }
148 : else
149 : {
150 0 : ind = get_next_indice( st, 3 );
151 0 : if ( ind < ACELP_MODE_MAX )
152 : {
153 0 : st->core = ACELP_CORE;
154 0 : st->coder_type = ind;
155 : }
156 : else
157 : {
158 0 : st->core = TCX_20_CORE;
159 0 : st->hTcxCfg->coder_type = ind - ACELP_MODE_MAX;
160 0 : st->coder_type = st->hTcxCfg->coder_type;
161 : }
162 : }
163 : }
164 : }
165 :
166 378168 : if ( st->element_mode == EVS_MONO )
167 : {
168 3750 : if ( st->igf && st->core == ACELP_CORE )
169 : {
170 1812 : st->bits_frame_core -= get_tbe_bits( st->total_brate, st->bwidth, st->rf_flag );
171 : }
172 :
173 3750 : if ( st->rf_flag )
174 : {
175 0 : st->bits_frame_core -= ( st->rf_target_bits + 1 ); /* +1 as flag-bit not considered in rf_target_bits */
176 : }
177 : }
178 :
179 : /* Inactive frame detection on non-DTX mode */
180 378168 : if ( st->coder_type == INACTIVE )
181 : {
182 39915 : st->VAD = 0;
183 : }
184 : else
185 : {
186 338253 : st->VAD = 1;
187 : }
188 : }
189 :
190 : /*Core extended mode mapping for correct PLC classification*/
191 2571432 : st->core_ext_mode = st->coder_type;
192 2571432 : if ( st->coder_type == INACTIVE )
193 : {
194 2233179 : st->core_ext_mode = UNVOICED;
195 : }
196 :
197 2571432 : return;
198 : }
199 :
200 :
201 : /*-------------------------------------------------------------------*
202 : * getTCXWindowing()
203 : *
204 : * get TCX transform type for each subframe
205 : *--------------------------------------------------------------------*/
206 :
207 2571432 : void getTCXWindowing(
208 : const int16_t core, /* i : current core */
209 : const int16_t last_core, /* i : last frame core */
210 : const int16_t element_mode, /* i : element mode */
211 : TCX_CONFIG_HANDLE hTcxCfg, /* i/o: TCX configuration handle */
212 : Decoder_State *st0 /* i : bitstream */
213 : )
214 : {
215 : int16_t overlap_code;
216 :
217 : /* Set the last overlap mode based on the previous and current frame type and coded overlap mode */
218 2571432 : if ( last_core == ACELP_CORE || last_core == AMR_WB_CORE )
219 : {
220 29202 : hTcxCfg->tcx_last_overlap_mode = TRANSITION_OVERLAP;
221 : }
222 2542230 : else if ( core == TCX_10_CORE && hTcxCfg->tcx_curr_overlap_mode == ALDO_WINDOW )
223 : {
224 38238 : hTcxCfg->tcx_last_overlap_mode = FULL_OVERLAP;
225 : }
226 2503992 : else if ( core != TCX_10_CORE && hTcxCfg->tcx_curr_overlap_mode == FULL_OVERLAP )
227 : {
228 62412 : hTcxCfg->tcx_last_overlap_mode = ALDO_WINDOW;
229 : }
230 : else
231 : {
232 2441580 : hTcxCfg->tcx_last_overlap_mode = hTcxCfg->tcx_curr_overlap_mode;
233 : }
234 :
235 : /* Set the current overlap mode based on the current frame type and coded overlap mode */
236 2571432 : hTcxCfg->tcx_curr_overlap_mode = ALDO_WINDOW;
237 :
238 2571432 : if ( core != ACELP_CORE )
239 : {
240 2569620 : overlap_code = 0;
241 2569620 : if ( get_next_indice( st0, 1 ) )
242 : {
243 46038 : overlap_code = 2 + get_next_indice( st0, 1 );
244 : }
245 :
246 : assert( MIN_OVERLAP == 2 && HALF_OVERLAP == 3 );
247 2569620 : hTcxCfg->tcx_curr_overlap_mode = overlap_code;
248 :
249 : /*TCX10 : always symmetric windows*/
250 2569620 : if ( core == TCX_20_CORE && overlap_code == 0 && last_core != ACELP_CORE && last_core != AMR_WB_CORE )
251 : {
252 2463981 : hTcxCfg->tcx_curr_overlap_mode = ALDO_WINDOW;
253 : }
254 : }
255 :
256 2571432 : if ( element_mode != EVS_MONO && core == TCX_10_CORE )
257 : {
258 : /* also read last overlap */
259 49620 : overlap_code = 0;
260 :
261 49620 : if ( get_next_indice( st0, 1 ) )
262 : {
263 11094 : overlap_code = 2 + get_next_indice( st0, 1 );
264 : }
265 :
266 49620 : hTcxCfg->tcx_last_overlap_mode = overlap_code;
267 : }
268 :
269 2571432 : return;
270 : }
271 :
272 :
273 : /*-------------------------------------------------------------------*
274 : * getLPCparam()
275 : *
276 : * get LPC parameters
277 : *--------------------------------------------------------------------*/
278 :
279 2291592 : void getLPCparam(
280 : Decoder_State *st, /* i/o: decoder memory state */
281 : int16_t param_lpc[], /* o : LTP parameters */
282 : Decoder_State *st0, /* i : bitstream */
283 : const int16_t ch, /* i : channel */
284 : const int16_t sns_low_br_mode /* i : SNS low-bitrate mode */
285 : )
286 : {
287 2291592 : if ( st->use_partial_copy == 0 )
288 : {
289 : /* Number of sets of LPC parameters (does not include mid-lpc) */
290 2291592 : if ( st->tcxonly == 0 || st->core < TCX_10_CORE )
291 : {
292 2248995 : st->numlpc = 1;
293 : }
294 : else
295 : {
296 42597 : st->numlpc = 2;
297 : }
298 :
299 : /* Decode LPC parameters */
300 2291592 : if ( st->hTcxDec->enableTcxLpc && st->core != ACELP_CORE )
301 50958 : {
302 : int16_t tcx_lpc_cdk;
303 50958 : tcx_lpc_cdk = tcxlpc_get_cdk( st->coder_type );
304 50958 : dec_lsf_tcxlpc( st0, ¶m_lpc, st->narrowBand, tcx_lpc_cdk );
305 : }
306 : else
307 : {
308 2240634 : if ( st->lpcQuantization == 0 )
309 : {
310 1913424 : decode_lpc_avq( st0, st->numlpc, param_lpc, ch, st->element_mode, sns_low_br_mode );
311 : }
312 327210 : else if ( st->lpcQuantization == 1 )
313 : {
314 327210 : if ( st->sr_core == INT_FS_16k && st->coder_type == VOICED && st->core == ACELP_CORE )
315 : {
316 804 : assert( st->element_mode == EVS_MONO );
317 :
318 804 : lsf_bctcvq_decprm( st0, param_lpc );
319 : }
320 : else
321 : {
322 326406 : lsf_msvq_ma_decprm( st0, param_lpc );
323 : }
324 : }
325 : else
326 : {
327 0 : assert( 0 );
328 : }
329 : }
330 : }
331 : else
332 : {
333 0 : st->numlpc = 1;
334 :
335 0 : if ( st->rf_frame_type == RF_TCXFD )
336 : {
337 0 : param_lpc[0] = 0;
338 0 : param_lpc[1] = get_next_indice( st0, lsf_numbits[0] ); /* VQ 1 */
339 0 : param_lpc[2] = get_next_indice( st0, lsf_numbits[1] ); /* VQ 2 */
340 0 : param_lpc[3] = get_next_indice( st0, lsf_numbits[2] ); /* VQ 3 */
341 : }
342 0 : else if ( st->rf_frame_type >= RF_ALLPRED && st->rf_frame_type <= RF_NELP )
343 : {
344 : /* LSF indices */
345 0 : param_lpc[0] = get_next_indice( st0, 8 ); /* VQ 1 */
346 0 : param_lpc[1] = get_next_indice( st0, 8 ); /* VQ 2 */
347 : }
348 : }
349 :
350 2291592 : return;
351 : }
352 :
353 : /*-------------------------------------------------------------------*
354 : * getTCXparam()
355 : *
356 : * get TCX core parameters
357 : *-------------------------------------------------------------------*/
358 :
359 4446984 : void getTCXparam(
360 : Decoder_State *st, /* i/o: Decoder State handle */
361 : Decoder_State *st0, /* i : bitstream */
362 : CONTEXT_HM_CONFIG hm_cfg, /* i/o: HM config */
363 : int16_t param[], /* o : decoded parameters */
364 : const int16_t bits_common, /* i : number of common bits */
365 : const int16_t start_bit_pos, /* i : position of the start bit */
366 : const int16_t *no_param_tns, /* i : number of TNS parameters per subframe */
367 : int16_t p_param[2], /* o : pointer to parameters for next round of bs reading*/
368 : int16_t nTnsBitsTCX10[2],
369 : const int16_t pre_past_flag )
370 : {
371 : int16_t ix, j, k, core, last_core, nSubframes;
372 : int16_t lg, lgFB, flag_ctx_hm, hm_size;
373 : int16_t PeriodicityIndex, useHarmonicModel;
374 : int16_t tcxltp_prm_0, tcxltp_prm_1, tcxltp_prm_2, nbits_igf, nbits_tcx;
375 : int16_t *prm, *prms;
376 : int16_t nTnsParams, nTnsBits;
377 : int16_t pre_part, post_part;
378 4446984 : TCX_LTP_DEC_HANDLE hTcxLtpDec = st->hTcxLtpDec;
379 4446984 : TCX_DEC_HANDLE hTcxDec = st->hTcxDec;
380 :
381 :
382 4446984 : if ( pre_past_flag == 0 )
383 : {
384 1877364 : pre_part = 1;
385 1877364 : post_part = 0;
386 : }
387 2569620 : else if ( pre_past_flag == 1 )
388 : {
389 1877364 : pre_part = 0;
390 1877364 : post_part = 1;
391 : }
392 : else
393 : {
394 692256 : pre_part = 1;
395 692256 : post_part = 1;
396 : }
397 :
398 : /* initialization */
399 4446984 : tcxltp_prm_0 = 0;
400 4446984 : tcxltp_prm_1 = 0;
401 4446984 : tcxltp_prm_2 = 0;
402 4446984 : nbits_igf = 0;
403 :
404 4446984 : PeriodicityIndex = 0;
405 4446984 : useHarmonicModel = 0;
406 :
407 4446984 : core = st->core;
408 4446984 : last_core = st->last_core;
409 :
410 4446984 : nSubframes = 1;
411 4446984 : if ( core == TCX_10_CORE )
412 : {
413 93129 : nSubframes = 2;
414 : }
415 :
416 4446984 : if ( st->element_mode < IVAS_CPE_MDCT && st->igf && core == TCX_10_CORE )
417 : {
418 : /* get IGF */
419 17775 : for ( k = 0; k < nSubframes; k++ )
420 : {
421 11850 : ix = st->next_bit_pos;
422 :
423 11850 : IGFDecReadLevel( st->hIGFDec, st0, IGF_GRID_LB_SHORT, 1 - k );
424 :
425 11850 : IGFDecReadData( st->hIGFDec, st0, IGF_GRID_LB_SHORT, 1 - k );
426 :
427 11850 : IGFDecStoreTCX10SubFrameData( st->hIGFDec, k );
428 :
429 11850 : nbits_igf += st0->next_bit_pos - ix;
430 : }
431 : }
432 :
433 : /* loop over subframes */
434 8987097 : for ( k = 0; k < nSubframes; k++ )
435 : {
436 4540113 : flag_ctx_hm = 0;
437 :
438 4540113 : prm = param + ( k * DEC_NPRM_DIV );
439 4540113 : j = 0;
440 :
441 4540113 : nbits_tcx = st0->next_bit_pos - start_bit_pos;
442 :
443 4540113 : if ( pre_part && st->enablePlcWaveadjust && k == ( nSubframes - 1 ) )
444 : {
445 0 : st->tonality_flag = get_next_indice( st0, 1 );
446 : }
447 :
448 4540113 : if ( post_part )
449 : {
450 : /* TCX Gain */
451 2619240 : prm[j++] = get_next_indice( st0, NBITS_TCX_GAIN );
452 :
453 : /* TCX Noise Filling = NBITS_NOISE_FILL_LEVEL bits */
454 2619240 : prm[j++] = get_next_indice( st0, NBITS_NOISE_FILL_LEVEL );
455 : }
456 : else
457 : {
458 1920873 : j += 1 + NOISE_FILL_RANGES;
459 : }
460 :
461 : /* LTP data */
462 4540113 : if ( pre_part )
463 : {
464 2619240 : if ( ( k == 0 ) && ( hTcxLtpDec->tcxltp || ( st->sr_core > 25600 ) ) ) /* PLC pitch info for HB */
465 : {
466 2569620 : prm[j] = get_next_indice( st0, 1 );
467 :
468 2569620 : if ( prm[j] )
469 : {
470 1725120 : prm[j + 1] = get_next_indice( st0, 9 );
471 1725120 : prm[j + 2] = get_next_indice( st0, 2 );
472 :
473 1725120 : tcxltp_prm_0 = prm[j];
474 1725120 : tcxltp_prm_1 = prm[j + 1];
475 1725120 : tcxltp_prm_2 = prm[j + 2];
476 : }
477 2569620 : st->BER_detect = st->BER_detect | tcx_ltp_decode_params( &prm[j], &( hTcxLtpDec->tcxltp_pitch_int ), &( hTcxLtpDec->tcxltp_pitch_fr ), &( hTcxLtpDec->tcxltp_gain ), st->pit_min, st->pit_fr1, st->pit_fr2, st->pit_max, st->pit_res_max );
478 :
479 2569620 : hTcxDec->tcxltp_last_gain_unmodified = hTcxLtpDec->tcxltp_gain;
480 :
481 2569620 : if ( core == TCX_20_CORE && !st->tcxonly && hTcxLtpDec->tcxltp_pitch_int < st->L_frame )
482 : {
483 376356 : hTcxDec->tcx_hm_LtpPitchLag = ( ( 2 * st->L_frame * st->pit_res_max ) << kLtpHmFractionalResolution ) / ( hTcxLtpDec->tcxltp_pitch_int * st->pit_res_max + hTcxLtpDec->tcxltp_pitch_fr );
484 : }
485 : else
486 : {
487 2193264 : hTcxDec->tcx_hm_LtpPitchLag = -1;
488 : }
489 2569620 : j += 3;
490 : }
491 : else
492 : {
493 49620 : prm[j++] = tcxltp_prm_0;
494 49620 : prm[j++] = tcxltp_prm_1;
495 49620 : prm[j++] = tcxltp_prm_2;
496 : }
497 : }
498 :
499 : /* TCX spectral data */
500 4540113 : lg = st->L_frame >> ( nSubframes - 1 );
501 4540113 : lgFB = st->hTcxCfg->tcx_coded_lines >> ( nSubframes - 1 );
502 :
503 4540113 : if ( post_part && k == 0 && st->last_core_from_bs == ACELP_CORE )
504 : {
505 : /* ACE->TCX transition */
506 27390 : lg += st->hTcxCfg->tcx_offset;
507 27390 : lgFB += lgFB >> ( 3 - nSubframes );
508 :
509 27390 : if ( st->hTcxCfg->lfacNext < 0 )
510 : {
511 27390 : lg -= st->hTcxCfg->lfacNext;
512 : }
513 : }
514 :
515 4540113 : if ( pre_part )
516 : {
517 : /* TNS data */
518 2619240 : nTnsParams = 0;
519 2619240 : nTnsBits = 0;
520 :
521 2619240 : if ( st->hTcxCfg->fIsTNSAllowed )
522 : {
523 2398068 : if ( core == TCX_10_CORE && last_core == ACELP_CORE && k == 0 )
524 : {
525 0 : st0->BER_detect = 1;
526 0 : last_core = TCX_20_CORE;
527 : }
528 :
529 2398068 : SetTnsConfig( st->hTcxCfg, core == TCX_20_CORE, ( st->last_core_from_bs == ACELP_CORE ) && ( k == 0 ) );
530 2398068 : ix = 0;
531 2398068 : if ( no_param_tns )
532 : {
533 31680 : ix = get_next_indice( st0, 1 ); /* common_tns_data[] for subframe k */
534 : }
535 2398068 : if ( ix )
536 : {
537 7620 : prm[j] = no_param_tns[k] * -1; /* - signals common TNS and its size */
538 7620 : nTnsParams = no_param_tns[k];
539 : }
540 : else
541 : {
542 2390448 : ReadTnsData( st->hTcxCfg->pCurrentTnsConfig, st0, &nTnsBits, prm + j, &nTnsParams );
543 : }
544 2398068 : hTcxDec->tnsActive[k] = ( prm[j] != 0 ? 1 : 0 ) * nTnsParams;
545 2398068 : if ( nTnsBitsTCX10 != NULL )
546 : {
547 1920873 : nTnsBitsTCX10[k] = nTnsBits + ( no_param_tns ? 1 : 0 );
548 : }
549 :
550 2398068 : j += nTnsParams;
551 : }
552 :
553 2619240 : if ( post_part )
554 : {
555 698367 : if ( core == TCX_20_CORE )
556 : {
557 686145 : hm_size = (int16_t) ( 2.0f * st->TcxBandwidth * (float) lg );
558 :
559 686145 : if ( hTcxDec->tcx_lpc_shaped_ari && st->last_core_from_bs != ACELP_CORE )
560 : {
561 46014 : dec_prm_hm( st0, &prm[j], hm_size );
562 : }
563 :
564 686145 : nbits_tcx = st->bits_frame_core - ( st0->next_bit_pos - start_bit_pos );
565 : }
566 : else
567 : {
568 12222 : hm_size = (int16_t) ( 2.0f * st->TcxBandwidth * (float) lgFB );
569 :
570 12222 : nbits_tcx = ( ( st->bits_frame_core - bits_common - nbits_igf + 1 - k ) >> 1 ) - ( ( st0->next_bit_pos - start_bit_pos ) - nbits_tcx );
571 : }
572 :
573 : /*Context HM flag*/
574 698367 : if ( st->hTcxCfg->ctx_hm && !( st->last_core_from_bs == ACELP_CORE && k == 0 ) )
575 : {
576 531105 : useHarmonicModel = get_next_indice( st0, 1 );
577 531105 : prm[j] = useHarmonicModel;
578 531105 : nbits_tcx--;
579 :
580 531105 : if ( useHarmonicModel )
581 : {
582 38094 : ix = DecodeIndex( st0, hm_size >= 256, prm + j + 1 );
583 38094 : flag_ctx_hm = 1;
584 :
585 38094 : PeriodicityIndex = *( prm + j + 1 );
586 38094 : if ( st->element_mode == EVS_MONO )
587 : {
588 165 : ConfigureContextHm( lgFB, nbits_tcx, PeriodicityIndex, hTcxDec->tcx_hm_LtpPitchLag, &hm_cfg );
589 : }
590 38094 : nbits_tcx -= ix;
591 : }
592 : }
593 : }
594 2619240 : j += NPRM_CTX_HM;
595 :
596 : /* read IGF payload */
597 2619240 : if ( post_part && core == TCX_20_CORE )
598 : {
599 686145 : if ( st->igf )
600 : {
601 657042 : ix = st->next_bit_pos;
602 657042 : IGFDecReadLevel( st->hIGFDec, st0, ( st->last_core_from_bs == ACELP_CORE ) ? IGF_GRID_LB_TRAN : IGF_GRID_LB_NORM, 1 );
603 :
604 657042 : IGFDecReadData( st->hIGFDec, st0, ( st->last_core_from_bs == ACELP_CORE ) ? IGF_GRID_LB_TRAN : IGF_GRID_LB_NORM, 1 );
605 :
606 657042 : nbits_tcx -= ( st0->next_bit_pos - ix );
607 : }
608 686145 : nbits_tcx = st->bits_frame_core - ( st0->next_bit_pos - start_bit_pos );
609 : }
610 1933095 : else if ( p_param != NULL )
611 : {
612 1920873 : p_param[k] = j;
613 : }
614 : }
615 : else
616 : {
617 1920873 : j = p_param[k];
618 :
619 1920873 : nbits_tcx = st->bits_frame_channel;
620 1920873 : if ( st->core == TCX_10_CORE )
621 : {
622 87018 : nbits_tcx = nTnsBitsTCX10[k] - NBITS_TCX_GAIN - NOISE_FILL_RANGES * NBITS_NOISE_FILL_LEVEL;
623 : }
624 : }
625 :
626 4540113 : if ( post_part )
627 : {
628 2619240 : if ( hTcxDec->tcx_lpc_shaped_ari && core == TCX_20_CORE )
629 : {
630 50958 : prm[j++] = nbits_tcx; /* store length of buffer */
631 50958 : prms = &prm[j];
632 8254722 : for ( ix = 0; ix < nbits_tcx; ix++ )
633 : {
634 8203764 : prms[ix] = get_next_indice_1( st0 );
635 : }
636 1681614 : for ( ix = 0; ix < 32; ix++ )
637 : {
638 1630656 : prms[ix + nbits_tcx] = 1;
639 : }
640 50958 : j += nbits_tcx;
641 : }
642 : else
643 : {
644 2568282 : if ( st->element_mode > EVS_MONO )
645 : {
646 2566344 : if ( useHarmonicModel )
647 : {
648 37929 : ConfigureContextHm( lgFB, nbits_tcx, PeriodicityIndex, hTcxDec->tcx_hm_LtpPitchLag, &hm_cfg );
649 : }
650 2566344 : hTcxDec->resQBits[k] = RCcontextMapping_decode2_no_mem_s17_LCS( st0, prm + j, lgFB, nbits_tcx, NPRM_RESQ * st->hTcxCfg->resq, flag_ctx_hm ? &hm_cfg : NULL );
651 : }
652 : else
653 : {
654 1938 : hTcxDec->resQBits[k] = ACcontextMapping_decode2_no_mem_s17_LC( st0, prm + j, lgFB, nbits_tcx, NPRM_RESQ * st->hTcxCfg->resq, flag_ctx_hm ? &hm_cfg : NULL );
655 : }
656 : }
657 : }
658 : }
659 :
660 4446984 : return;
661 : }
662 :
663 :
664 : /*-----------------------------------------------------------------*
665 : * dec_prm_hm()
666 : *
667 : *
668 : *-----------------------------------------------------------------*/
669 :
670 46014 : void dec_prm_hm(
671 : Decoder_State *st,
672 : int16_t *prm_hm,
673 : const int16_t hm_size )
674 : {
675 : /* Disable HM for non-GC,VC modes */
676 46014 : if ( st->hTcxCfg->coder_type != VOICED && st->hTcxCfg->coder_type != GENERIC )
677 : {
678 6291 : prm_hm[0] = 0;
679 :
680 6291 : return;
681 : }
682 :
683 39723 : prm_hm[1] = -1;
684 39723 : prm_hm[2] = 0;
685 :
686 : /* Flag */
687 39723 : prm_hm[0] = get_next_indice( st, 1 );
688 :
689 39723 : if ( prm_hm[0] )
690 : {
691 : /* Periodicity index */
692 20118 : DecodeIndex( st, hm_size >= 256, &prm_hm[1] );
693 :
694 : /* Gain index */
695 20118 : if ( st->hTcxCfg->coder_type == VOICED )
696 : {
697 7014 : prm_hm[2] = get_next_indice( st, kTcxHmNumGainBits );
698 : }
699 : }
700 :
701 39723 : return;
702 : }
703 :
704 :
705 : /*-----------------------------------------------------------------*
706 : * Function dec_prm() *
707 : * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ *
708 : *
709 : * SQ is used for TCX modes
710 : *
711 : * decode parameters according to selected mode *
712 : *-----------------------------------------------------------------*/
713 :
714 3750 : void dec_prm(
715 : Decoder_State *st, /* i/o: decoder memory state */
716 : int16_t param[], /* o : decoded parameters */
717 : int16_t param_lpc[], /* o : LPC parameters */
718 : int16_t *total_nbbits, /* i/o: number of bits / decoded bits */
719 : int16_t *bitsRead )
720 : {
721 : int16_t j, n, sfr, core, nb_subfr;
722 : int16_t *prm;
723 : int16_t start_bit_pos, bits_common, acelp_target_bits;
724 : int16_t ix, j_old, wordcnt, bitcnt;
725 : CONTEXT_HM_CONFIG hm_cfg;
726 : int16_t indexBuffer[N_MAX + 1];
727 : int16_t ind;
728 : int16_t ltp_mode, gains_mode;
729 : int16_t prm_ltp[LTPSIZE];
730 3750 : TCX_LTP_DEC_HANDLE hTcxLtpDec = st->hTcxLtpDec;
731 3750 : TCX_DEC_HANDLE hTcxDec = st->hTcxDec;
732 :
733 : /*--------------------------------------------------------------------------------*
734 : * INIT
735 : *--------------------------------------------------------------------------------*/
736 :
737 3750 : hm_cfg.indexBuffer = indexBuffer;
738 :
739 3750 : core = st->core;
740 :
741 3750 : if ( st->mdct_sw == MODE1 )
742 : {
743 825 : start_bit_pos = 0; /* count from frame start */
744 :
745 : /* Adjust st->bits_frame_core not to subtract MODE2 bandwidth signaling */
746 4950 : for ( n = 0; n < FRAME_SIZE_NB; n++ )
747 : {
748 4950 : if ( FrameSizeConfig[n].frame_bits == st->total_brate / FRAMES_PER_SEC )
749 : {
750 825 : st->bits_frame_core += FrameSizeConfig[n].bandwidth_bits;
751 825 : break;
752 : }
753 : }
754 : }
755 : else
756 : {
757 2925 : if ( st->rf_flag == 1 )
758 : {
759 : /*Inherent adjustment to accommodate the compact packing used in the RF mode*/
760 0 : start_bit_pos = st->next_bit_pos - 2;
761 : }
762 : else
763 : {
764 2925 : start_bit_pos = st->next_bit_pos;
765 : }
766 : }
767 :
768 : /* Framing parameters */
769 3750 : nb_subfr = st->nb_subfr;
770 :
771 : /* Initialize pointers */
772 3750 : prm = param;
773 :
774 : /* Init counters */
775 3750 : j = 0;
776 :
777 : /* Init LTP data */
778 3750 : if ( hTcxDec != NULL )
779 : {
780 3750 : hTcxDec->tcx_hm_LtpPitchLag = -1;
781 3750 : hTcxLtpDec->tcxltp_gain = 0.0f;
782 : }
783 :
784 : /*--------------------------------------------------------------------------------*
785 : * core-coder header
786 : *--------------------------------------------------------------------------------*/
787 :
788 : /* Modes (ACE_GC, ACE_UC, TCX20, TCX10...) */
789 3750 : getTCXMode( st, st, 0 /* <- MCT_flag */ );
790 3750 : core = st->core;
791 :
792 : /* Decode last_core for error concealment */
793 3750 : if ( !( core == ACELP_CORE && st->hTcxCfg->lfacNext <= 0 ) && !st->use_partial_copy )
794 : {
795 :
796 1938 : st->last_core_from_bs = get_next_indice( st, 1 );
797 : /*
798 : need to introduce special error handling for lost transition frames from CNG:
799 : in such cases, the bitstream reader continues with CNG, setting bfi = 0, total_brate = 0
800 : this might result in a not matching last_core transmitted in the BS - we should use this
801 : only for interpreting the bitstream and re-use the internal state for the proper
802 : transition handling; still, for voiced onsets rather stick to wrong windowing...
803 : */
804 1938 : if ( ( !( ( st->last_total_brate == 0 ) && ( st->clas_dec != VOICED_CLAS ) ) ) && ( st->last_core_from_bs != st->last_core ) )
805 : {
806 42 : st->last_core = st->last_core_from_bs;
807 : }
808 :
809 : /*for TCX 10 force last_core to be TCX since ACELP as previous core is forbidden*/
810 1938 : if ( core == TCX_10_CORE )
811 : {
812 0 : st->last_core = TCX_20_CORE;
813 0 : st->last_core_from_bs = TCX_20_CORE;
814 : }
815 : }
816 :
817 3750 : if ( st->rf_flag && st->use_partial_copy && !st->tcxonly )
818 : {
819 : int32_t nbits_total;
820 0 : nbits_total = st->total_brate / FRAMES_PER_SEC;
821 0 : st->bits_frame_core = st->rf_target_bits;
822 : /* offset the indices to read the acelp partial copy */
823 0 : get_next_indice_tmp( st, (int16_t) ( start_bit_pos + nbits_total - st->rf_target_bits - 3 - st->next_bit_pos ) );
824 : }
825 :
826 3750 : if ( !st->use_partial_copy )
827 : {
828 3750 : getTCXWindowing( st->core, st->last_core, st->element_mode, st->hTcxCfg, st );
829 :
830 3750 : if ( st->enableGplc ) /* SIDE INFO. DECODING */
831 : {
832 : int16_t pitchDiff;
833 : int16_t bits_per_subfr, search_range;
834 2925 : bits_per_subfr = 4;
835 2925 : search_range = 8;
836 2925 : st->flagGuidedAcelp = get_next_indice( st, 1 );
837 :
838 2925 : pitchDiff = 0;
839 2925 : if ( st->flagGuidedAcelp )
840 : {
841 1293 : pitchDiff = get_next_indice( st, bits_per_subfr );
842 1293 : st->guidedT0 = ( pitchDiff - search_range );
843 : }
844 2925 : if ( pitchDiff == 0 && st->flagGuidedAcelp )
845 : {
846 30 : st->flagGuidedAcelp = 0;
847 : }
848 : }
849 : else
850 : {
851 825 : st->flagGuidedAcelp = 0;
852 : }
853 :
854 3750 : if ( st->dec_glr )
855 : {
856 2925 : if ( core == ACELP_CORE )
857 : {
858 1812 : st->dec_glr_idx = get_next_indice( st, G_LPC_RECOVERY_BITS );
859 : }
860 : else
861 : {
862 1113 : st->dec_glr_idx = -1;
863 : }
864 : }
865 : }
866 :
867 : /*--------------------------------------------------------------------------------*
868 : * LPC parameters
869 : *--------------------------------------------------------------------------------*/
870 :
871 : /*Initialization of LPC Mid flag*/
872 3750 : if ( ( st->lpcQuantization == 1 && st->coder_type == VOICED ) || ( st->use_partial_copy ) )
873 : {
874 1551 : ( &( st->acelp_cfg ) )->midLpc = 0;
875 : }
876 : else
877 : {
878 2199 : ( &( st->acelp_cfg ) )->midLpc = st->acelp_cfg.midLpc_enable;
879 : }
880 :
881 3750 : getLPCparam( st, param_lpc, st, -1, 0 );
882 :
883 3750 : bits_common = st->next_bit_pos - start_bit_pos;
884 :
885 : /*--------------------------------------------------------------------------------*
886 : * ACELP parameters
887 : *--------------------------------------------------------------------------------*/
888 :
889 3750 : if ( core == ACELP_CORE && st->use_partial_copy == 0 )
890 1812 : {
891 : int16_t tmp;
892 :
893 : /* Target Bits */
894 :
895 : /* needed in decoder to read the bitstream */
896 1812 : acelp_target_bits = st->bits_frame_core - bits_common;
897 :
898 : /* Configure ACELP */
899 1812 : tmp = BITS_ALLOC_config_acelp( acelp_target_bits, st->coder_type, &( st->acelp_cfg ), st->narrowBand, st->nb_subfr );
900 1812 : if ( tmp < 0 )
901 : {
902 : /* erroneous configuration, resulting from a corrupt bitstream */
903 0 : st->BER_detect = 1;
904 : }
905 :
906 : /* Adaptive BPF (2 bits)*/
907 1812 : n = ACELP_BPF_BITS[st->acelp_cfg.bpf_mode];
908 1812 : if ( n != 0 )
909 : {
910 1734 : st->bpf_gain_param = get_next_indice( st, n );
911 : }
912 : else
913 : {
914 78 : st->bpf_gain_param = ( st->acelp_cfg.bpf_mode ) * 2;
915 : }
916 :
917 : /* Mean energy (2 or 3 bits) */
918 1812 : n = ACELP_NRG_BITS[st->acelp_cfg.nrg_mode];
919 1812 : if ( n != 0 )
920 : {
921 1812 : prm[j++] = get_next_indice( st, n );
922 : }
923 :
924 : /* Subframe parameters */
925 10872 : for ( sfr = 0; sfr < nb_subfr; sfr++ )
926 : {
927 : /* Pitch lag (4, 5, 6, 8 or 9 bits) */
928 9060 : n = ACELP_LTP_BITS_SFR[st->acelp_cfg.ltp_mode][sfr];
929 :
930 9060 : if ( n != 0 )
931 : {
932 9060 : prm[j] = get_next_indice( st, n );
933 9060 : j++;
934 : }
935 :
936 : /* Adaptive codebook filtering (1 bit) */
937 9060 : if ( st->acelp_cfg.ltf_mode == 2 )
938 : {
939 4020 : prm[j] = get_next_indice( st, 1 );
940 4020 : j++;
941 : }
942 :
943 : /* Innovative codebook */
944 : {
945 : /* Decode pulse positions. */
946 9060 : j_old = j;
947 :
948 9060 : wordcnt = ACELP_FIXED_CDK_BITS( st->acelp_cfg.fixed_cdk_index[sfr] ) / 16;
949 9060 : bitcnt = ACELP_FIXED_CDK_BITS( st->acelp_cfg.fixed_cdk_index[sfr] ) & 15;
950 :
951 : /* sanity check - can happen in case of bit errors */
952 9060 : if ( ( st->acelp_cfg.fixed_cdk_index[sfr] >= ACELP_FIXED_CDK_NB ) || ( st->acelp_cfg.fixed_cdk_index[sfr] < 0 ) )
953 : {
954 0 : st->acelp_cfg.fixed_cdk_index[sfr] = 0;
955 0 : st->BER_detect = 1;
956 : }
957 :
958 36642 : for ( ix = 0; ix < wordcnt; ix++ )
959 : {
960 27582 : prm[j] = get_next_indice( st, 16 );
961 27582 : j++;
962 : }
963 :
964 9060 : if ( bitcnt )
965 : {
966 9060 : prm[j] = get_next_indice( st, bitcnt );
967 : }
968 9060 : j = j_old + 8;
969 : }
970 :
971 : /* Gains (5b, 6b or 7b / subfr) */
972 9060 : n = ACELP_GAINS_BITS[st->acelp_cfg.gains_mode[sfr]];
973 9060 : prm[j++] = get_next_indice( st, n );
974 : } /*end of subfr loop*/
975 : }
976 1938 : else if ( st->rf_frame_type >= RF_ALLPRED && st->use_partial_copy )
977 : {
978 0 : int16_t tmp = BITS_ALLOC_config_acelp( st->rf_target_bits, /* target bits ranges from 56 to 72 depending on rf_type */
979 0 : st->rf_frame_type, /* already offset by 4 to parse the config elements for partial copy */
980 : &( st->acelp_cfg ), /* acelp_cfg_rf*/
981 : 0, /* is narrowBand */
982 0 : st->nb_subfr );
983 0 : if ( tmp < 0 )
984 : {
985 : /* erroneous configuration, resulting from a corrupt bitstream */
986 0 : st->BER_detect = 1;
987 : }
988 :
989 : /* rf_frame_type NELP: 7 */
990 0 : if ( st->rf_frame_type == RF_NELP )
991 : {
992 : /* NELP gain indices */
993 0 : st->rf_indx_nelp_iG1 = get_next_indice( st, 5 );
994 0 : st->rf_indx_nelp_iG2[0] = get_next_indice( st, 6 );
995 0 : st->rf_indx_nelp_iG2[1] = get_next_indice( st, 6 );
996 :
997 : /* NELP filter selection index */
998 0 : st->rf_indx_nelp_fid = get_next_indice( st, 2 );
999 :
1000 : /* tbe gainFr */
1001 0 : st->rf_indx_tbeGainFr = get_next_indice( st, 5 );
1002 : }
1003 : else
1004 : {
1005 : /* rf_frame_type ALL_PRED: 4, NO_PRED: 5, GEN_PRED: 6*/
1006 : /* ES pred */
1007 0 : prm[j++] = get_next_indice( st, 3 );
1008 :
1009 0 : ltp_mode = ACELP_LTP_MODE[1][1][st->rf_frame_type];
1010 0 : gains_mode = ACELP_GAINS_MODE[1][1][st->rf_frame_type];
1011 :
1012 : /* Subframe parameters */
1013 0 : for ( sfr = 0; sfr < nb_subfr; sfr++ )
1014 : {
1015 : /* Pitch lag (5, or 8 bits) */
1016 0 : n = ACELP_LTP_BITS_SFR[ltp_mode][sfr];
1017 0 : if ( n != 0 )
1018 : {
1019 0 : prm[j++] = get_next_indice( st, n );
1020 : }
1021 :
1022 :
1023 : /*Innovative codebook*/
1024 0 : if ( ( st->rf_frame_type == RF_NOPRED ) || ( st->rf_frame_type == RF_GENPRED && ( sfr == 0 || sfr == 2 ) ) )
1025 : {
1026 : /* NOTE: FCB actual bits need to be backed up as well */
1027 : /*n = ACELP_FIXED_CDK_BITS(st->rf_indx_fcb[fec_offset][sfr]) & 15;*/
1028 0 : prm[j] = get_next_indice( st, 7 );
1029 0 : j = j + 8;
1030 : }
1031 :
1032 : /* Gains (5b, 6b or 7b / subfr) */
1033 0 : if ( sfr == 0 || sfr == 2 )
1034 : {
1035 0 : n = ACELP_GAINS_BITS[gains_mode];
1036 0 : prm[j++] = get_next_indice( st, n );
1037 : }
1038 : }
1039 0 : st->rf_indx_tbeGainFr = get_next_indice( st, 2 );
1040 : }
1041 : }
1042 :
1043 : /*--------------------------------------------------------------------------------*
1044 : * TCX20/10 parameters
1045 : *--------------------------------------------------------------------------------*/
1046 :
1047 3750 : if ( ( core == TCX_20_CORE || core == TCX_10_CORE ) && st->use_partial_copy == 0 )
1048 : {
1049 1938 : getTCXparam( st, st, hm_cfg, param, bits_common, start_bit_pos, NULL, NULL, NULL, -1 );
1050 : }
1051 :
1052 3750 : if ( st->rf_frame_type >= RF_TCXFD && st->rf_frame_type <= RF_TCXTD2 && st->use_partial_copy == 1 )
1053 : {
1054 : /* classification */
1055 0 : ind = get_next_indice( st, 2 );
1056 0 : st->clas_dec = ONSET;
1057 0 : if ( ind == 0 )
1058 : {
1059 0 : st->clas_dec = UNVOICED_CLAS;
1060 : }
1061 0 : else if ( ind == 1 )
1062 : {
1063 0 : if ( st->last_good >= VOICED_TRANSITION )
1064 : {
1065 0 : st->clas_dec = VOICED_TRANSITION;
1066 : }
1067 : else
1068 : {
1069 0 : st->clas_dec = UNVOICED_TRANSITION;
1070 : }
1071 : }
1072 0 : else if ( ind == 2 )
1073 : {
1074 0 : st->clas_dec = VOICED_CLAS;
1075 : }
1076 :
1077 0 : if ( st->rf_frame_type == RF_TCXFD )
1078 : {
1079 : /* TCX Gain */
1080 0 : hTcxDec->old_gaintcx_bfi = get_next_indice( st, NBITS_TCX_GAIN );
1081 : }
1082 : else
1083 : {
1084 : /* LTP data */
1085 0 : if ( hTcxLtpDec->tcxltp )
1086 : {
1087 0 : if ( st->rf_frame_type == RF_TCXTD2 || st->rf_frame_type == RF_TCXTD1 )
1088 : {
1089 0 : prm_ltp[0] = 1; /* LTP active*/
1090 0 : prm_ltp[1] = get_next_indice( st, 9 );
1091 0 : prm_ltp[2] = 3; /* max ampl. quantizer output (2bits), anyway not used later*/
1092 :
1093 0 : if ( !st->prev_bfi )
1094 : {
1095 0 : st->BER_detect = st->BER_detect |
1096 0 : tcx_ltp_decode_params( &prm_ltp[0], &( hTcxLtpDec->tcxltp_pitch_int ), &( hTcxLtpDec->tcxltp_pitch_fr ), &( hTcxLtpDec->tcxltp_gain ), st->pit_min, st->pit_fr1, st->pit_fr2, st->pit_max, st->pit_res_max );
1097 :
1098 0 : hTcxDec->tcxltp_last_gain_unmodified = hTcxLtpDec->tcxltp_gain;
1099 : }
1100 : }
1101 : }
1102 : }
1103 : }
1104 :
1105 3750 : if ( !st->use_partial_copy )
1106 : {
1107 3750 : if ( *total_nbbits - bitsRead[0] < ( st->next_bit_pos - start_bit_pos ) )
1108 : {
1109 0 : st->BER_detect = 1;
1110 0 : st->next_bit_pos = start_bit_pos + *total_nbbits - bitsRead[0];
1111 : }
1112 3750 : bitsRead[0] = st->next_bit_pos - start_bit_pos;
1113 : }
1114 3750 : return;
1115 : }
|