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 "stat_dec.h"
43 : #include "rom_com.h"
44 : #include "prot.h"
45 : #include "wmc_auto.h"
46 :
47 : /*-----------------------------------------------------------------*
48 : * decision_matrix_dec()
49 : *
50 : * ACELP/HQ core selection
51 : * Read ACELP signaling bits from the bitstream
52 : * Set extension layers
53 : *-----------------------------------------------------------------*/
54 :
55 14392 : void decision_matrix_dec(
56 : Decoder_State *st, /* i/o: decoder state structure */
57 : int16_t *sharpFlag, /* o : formant sharpening flag */
58 : int16_t *hq_core_type, /* o : HQ core type */
59 : int16_t *core_switching_flag /* o : ACELP->HQ switching frame flag */
60 : )
61 : {
62 : int16_t start_idx;
63 : int16_t ppp_nelp_mode;
64 : int32_t ind;
65 : int16_t nBits;
66 : int16_t tmp;
67 :
68 : /* init */
69 14392 : st->core = -1;
70 14392 : st->core_brate = 0;
71 14392 : st->extl = -1;
72 14392 : st->extl_brate = 0;
73 14392 : st->ppp_mode_dec = 0;
74 14392 : st->nelp_mode_dec = 0;
75 14392 : st->igf = 0;
76 :
77 14392 : if ( st->total_brate > ACELP_8k00 )
78 : {
79 12580 : st->vbr_hw_BWE_disable_dec = 0;
80 : }
81 :
82 14392 : if ( st->mdct_sw == MODE2 )
83 : {
84 710 : st->core = HQ_CORE;
85 : }
86 : else
87 : {
88 13682 : if ( st->total_brate == FRAME_NO_DATA || st->total_brate == SID_2k40 )
89 : {
90 0 : st->core = ACELP_CORE;
91 0 : st->core_brate = st->total_brate;
92 :
93 0 : if ( st->total_brate != FRAME_NO_DATA )
94 : {
95 0 : st->cng_type = get_next_indice( st, 1 );
96 :
97 0 : if ( st->cng_type == LP_CNG )
98 : {
99 0 : st->L_frame = L_FRAME;
100 :
101 0 : if ( get_next_indice( st, 1 ) == 1 )
102 : {
103 0 : st->L_frame = L_FRAME16k;
104 : #ifdef FIX_2252_LP_CNG_STARTS_SID
105 0 : st->nb_subfr = NB_SUBFR16k;
106 0 : st->bwidth = max( st->bwidth, WB );
107 : #endif
108 : }
109 : }
110 : else
111 : {
112 0 : st->bwidth = get_next_indice( st, 2 );
113 :
114 0 : if ( get_next_indice( st, 1 ) == 0 )
115 : {
116 0 : st->L_frame = L_FRAME;
117 : }
118 : else
119 : {
120 0 : st->L_frame = L_FRAME16k;
121 : }
122 : }
123 : }
124 :
125 0 : if ( st->output_Fs >= 32000 && st->bwidth >= SWB )
126 : {
127 0 : st->extl = SWB_CNG;
128 : }
129 :
130 0 : if ( st->total_brate == FRAME_NO_DATA && st->prev_bfi && !st->bfi && st->L_frame > L_FRAME16k )
131 : {
132 0 : st->L_frame = st->last_CNG_L_frame;
133 : }
134 :
135 0 : return;
136 : }
137 :
138 : /* SC-VBR */
139 13682 : else if ( st->total_brate == PPP_NELP_2k80 )
140 : {
141 78 : st->core = ACELP_CORE;
142 78 : st->core_brate = PPP_NELP_2k80;
143 78 : st->L_frame = L_FRAME;
144 78 : st->fscale = sr2fscale( INT_FS_12k8 );
145 :
146 78 : if ( st->ini_frame == 0 )
147 : {
148 : /* avoid switching of internal ACELP Fs in the very first frame */
149 4 : st->last_L_frame = st->L_frame;
150 4 : st->last_core = st->core;
151 4 : st->last_core_brate = st->core_brate;
152 4 : st->last_extl = st->extl;
153 : }
154 :
155 78 : st->vbr_hw_BWE_disable_dec = 1;
156 :
157 78 : get_next_indice( st, 1 );
158 :
159 78 : ppp_nelp_mode = get_next_indice( st, 2 );
160 :
161 : /* 0 - PPP_NB, 1 - PPP_WB, 2 - NELP_NB, 3 - NELP_WB */
162 78 : if ( ppp_nelp_mode == 0 )
163 : {
164 9 : st->ppp_mode_dec = 1;
165 9 : st->coder_type = VOICED;
166 9 : st->bwidth = NB;
167 : }
168 69 : else if ( ppp_nelp_mode == 1 )
169 : {
170 29 : st->ppp_mode_dec = 1;
171 29 : st->coder_type = VOICED;
172 29 : st->bwidth = WB;
173 : }
174 40 : else if ( ppp_nelp_mode == 2 )
175 : {
176 16 : st->nelp_mode_dec = 1;
177 16 : st->coder_type = UNVOICED;
178 16 : st->bwidth = NB;
179 : }
180 24 : else if ( ppp_nelp_mode == 3 )
181 : {
182 24 : st->nelp_mode_dec = 1;
183 24 : st->coder_type = UNVOICED;
184 24 : st->bwidth = WB;
185 : }
186 :
187 78 : return;
188 : }
189 :
190 : /*---------------------------------------------------------------------*
191 : * ACELP/HQ core selection
192 : *---------------------------------------------------------------------*/
193 :
194 13604 : if ( st->total_brate < ACELP_24k40 )
195 : {
196 7416 : st->core = ACELP_CORE;
197 : }
198 6188 : else if ( st->total_brate >= ACELP_24k40 && st->total_brate <= ACELP_64k )
199 : {
200 : /* read the ACELP/HQ core selection bit */
201 6188 : tmp = get_next_indice( st, 1 );
202 :
203 6188 : if ( tmp == 0 )
204 : {
205 5195 : st->core = ACELP_CORE;
206 : }
207 : else
208 : {
209 993 : st->core = HQ_CORE;
210 : }
211 : }
212 : }
213 :
214 : /*-----------------------------------------------------------------*
215 : * Read ACELP signaling bits from the bitstream
216 : *-----------------------------------------------------------------*/
217 :
218 14314 : if ( st->core == ACELP_CORE )
219 : {
220 : /* find the section in the ACELP signaling table corresponding to bitrate */
221 12611 : start_idx = 0;
222 631207 : while ( acelp_sig_tbl[start_idx] != st->total_brate )
223 : {
224 618596 : start_idx++;
225 618596 : if ( start_idx >= MAX_ACELP_SIG )
226 : {
227 0 : st->BER_detect = 1;
228 0 : start_idx = 0;
229 0 : break;
230 : }
231 : }
232 :
233 : /* skip the bitrate */
234 12611 : start_idx += 1;
235 :
236 : /* retrieve the number of bits */
237 12611 : nBits = (int16_t) acelp_sig_tbl[start_idx++];
238 :
239 12611 : start_idx += get_next_indice( st, nBits );
240 12611 : if ( start_idx >= MAX_ACELP_SIG )
241 : {
242 0 : ind = 0;
243 0 : st->BER_detect = 1;
244 : }
245 : else
246 : {
247 : /* retrieve the signaling indice */
248 12611 : ind = acelp_sig_tbl[start_idx];
249 :
250 : /* convert signaling indice into signaling information */
251 12611 : st->coder_type = ind & 0x7;
252 12611 : if ( st->coder_type == LR_MDCT )
253 : {
254 178 : st->core = HQ_CORE;
255 178 : st->bwidth = ( ind >> 3 ) & 0x7;
256 : }
257 : else
258 : {
259 12433 : st->bwidth = ( ind >> 3 ) & 0x7;
260 12433 : *sharpFlag = ( ind >> 6 ) & 0x1;
261 : }
262 : }
263 :
264 : /* detect corrupted signaling (due to bit errors) */
265 12611 : if ( ( st->BER_detect ) ||
266 12611 : ( ind >= 1 << 7 ) ||
267 12611 : ( st->total_brate <= ACELP_13k20 && st->bwidth == FB ) ||
268 12611 : ( st->total_brate >= ACELP_32k && st->bwidth == NB ) ||
269 12611 : ( st->total_brate >= ACELP_32k && !( st->coder_type == GENERIC || st->coder_type == TRANSITION || st->coder_type == INACTIVE ) ) ||
270 12611 : ( st->total_brate < ACELP_13k20 && st->bwidth != NB && st->coder_type == LR_MDCT ) ||
271 12611 : ( st->total_brate >= ACELP_13k20 && st->coder_type == UNVOICED ) ||
272 12611 : ( st->total_brate >= ACELP_13k20 && st->coder_type == AUDIO && st->bwidth == NB ) )
273 : {
274 0 : st->BER_detect = 0;
275 0 : st->bfi = 1;
276 0 : if ( st->ini_frame == 0 )
277 : {
278 0 : st->core = ACELP_CORE;
279 0 : st->L_frame = L_FRAME;
280 0 : st->last_core = st->core;
281 0 : st->last_core_brate = st->core_brate;
282 : }
283 0 : else if ( st->last_total_brate == -1 ) /* can happen in case of BER when no good frame was received before */
284 : {
285 0 : st->coder_type = st->last_coder_type;
286 0 : st->bwidth = st->last_bwidth;
287 0 : st->total_brate = st->last_total_brate_ber;
288 0 : if ( st->last_core == AMR_WB_CORE )
289 : {
290 0 : st->core = ACELP_CORE;
291 0 : st->codec_mode = MODE1;
292 : }
293 0 : else if ( st->last_core_bfi == TCX_20_CORE || st->last_core_bfi == TCX_10_CORE )
294 : {
295 0 : st->core = st->last_core_bfi;
296 0 : st->codec_mode = MODE2;
297 : }
298 : else
299 : {
300 0 : st->core = st->last_core;
301 0 : st->codec_mode = MODE1;
302 : }
303 0 : st->core_brate = st->last_core_brate;
304 0 : st->extl = st->last_extl;
305 0 : st->extl_brate = st->total_brate - st->core_brate;
306 : }
307 : else
308 : {
309 0 : st->coder_type = st->last_coder_type;
310 0 : st->bwidth = st->last_bwidth;
311 0 : st->total_brate = st->last_total_brate;
312 0 : if ( st->last_core == AMR_WB_CORE )
313 : {
314 0 : st->core = ACELP_CORE;
315 0 : st->codec_mode = MODE1;
316 : }
317 0 : else if ( st->last_core == TCX_20_CORE || st->last_core == TCX_10_CORE )
318 : {
319 0 : st->core = st->last_core;
320 0 : st->codec_mode = MODE2;
321 : }
322 : else
323 : {
324 0 : st->core = st->last_core;
325 0 : st->codec_mode = MODE1;
326 : }
327 0 : st->core_brate = st->last_core_brate;
328 0 : st->extl = st->last_extl;
329 0 : st->extl_brate = st->total_brate - st->core_brate;
330 : }
331 0 : return;
332 : }
333 : }
334 :
335 : /*-----------------------------------------------------------------*
336 : * Set extension layers
337 : *-----------------------------------------------------------------*/
338 :
339 14314 : if ( st->core == ACELP_CORE && st->bwidth == WB && st->total_brate < ACELP_9k60 )
340 : {
341 1199 : if ( st->vbr_hw_BWE_disable_dec == 0 )
342 : {
343 1020 : st->extl = WB_BWE;
344 : }
345 : }
346 13115 : else if ( st->core == ACELP_CORE && st->bwidth == WB && st->total_brate >= ACELP_9k60 && st->total_brate <= ACELP_16k40 )
347 : {
348 : /* read the WB TBE/BWE selection bit */
349 562 : if ( get_next_indice( st, 1 ) )
350 : {
351 0 : st->extl = WB_BWE;
352 0 : st->extl_brate = WB_BWE_0k35;
353 : }
354 : else
355 : {
356 562 : st->extl = WB_TBE;
357 562 : st->extl_brate = WB_TBE_1k05;
358 : }
359 : }
360 12553 : else if ( st->core == ACELP_CORE && ( st->bwidth == SWB || st->bwidth == FB ) && st->total_brate >= ACELP_13k20 )
361 : {
362 8742 : if ( st->total_brate >= ACELP_48k )
363 : {
364 2979 : st->extl = SWB_BWE_HIGHRATE;
365 2979 : if ( st->bwidth == FB )
366 : {
367 392 : st->extl = FB_BWE_HIGHRATE;
368 : }
369 :
370 2979 : st->extl_brate = SWB_BWE_16k;
371 : }
372 :
373 : /* read the SWB TBE/BWE selection bit */
374 : else
375 : {
376 5763 : tmp = get_next_indice( st, 1 );
377 :
378 5763 : if ( tmp )
379 : {
380 18 : st->extl = SWB_BWE;
381 18 : st->extl_brate = SWB_BWE_1k6;
382 : }
383 : else
384 : {
385 5745 : st->extl = SWB_TBE;
386 5745 : st->extl_brate = SWB_TBE_1k6;
387 5745 : if ( st->total_brate >= ACELP_24k40 )
388 : {
389 1032 : st->extl_brate = SWB_TBE_2k8;
390 : }
391 : }
392 : }
393 :
394 : /* set FB TBE and FB BWE extension layers */
395 8742 : if ( st->bwidth == FB && st->total_brate >= ACELP_24k40 )
396 : {
397 744 : if ( st->extl == SWB_BWE )
398 : {
399 0 : st->extl = FB_BWE;
400 0 : st->extl_brate = FB_BWE_1k8;
401 : }
402 744 : else if ( st->extl == SWB_TBE )
403 : {
404 352 : st->extl = FB_TBE;
405 352 : st->extl_brate = FB_TBE_3k0;
406 : }
407 : }
408 : }
409 :
410 : /* set core bitrate */
411 14314 : st->core_brate = st->total_brate - st->extl_brate;
412 :
413 : /*-----------------------------------------------------------------*
414 : * Read HQ signaling bits from the bitstream
415 : * Set HQ core type
416 : *-----------------------------------------------------------------*/
417 :
418 14314 : if ( st->core == HQ_CORE )
419 : {
420 1881 : if ( st->mdct_sw != MODE2 )
421 : {
422 : /* skip the HQ/TCX core switching flag */
423 1171 : get_next_indice_tmp( st, 1 );
424 : }
425 :
426 : /* read ACELP->HQ core switching flag */
427 1881 : *core_switching_flag = get_next_indice( st, 1 );
428 :
429 1881 : if ( *core_switching_flag == 1 )
430 : {
431 125 : st->last_L_frame_ori = st->last_L_frame;
432 :
433 : /* read ACELP L_frame info */
434 125 : if ( get_next_indice( st, 1 ) == 0 )
435 : {
436 21 : st->last_L_frame = L_FRAME;
437 : }
438 : else
439 : {
440 104 : st->last_L_frame = L_FRAME16k;
441 : }
442 : }
443 :
444 1881 : if ( st->mdct_sw != MODE2 )
445 : {
446 : /* read/set band-width (needed for different I/O sampling rate support) */
447 1171 : if ( st->total_brate > ACELP_16k40 )
448 : {
449 993 : ind = get_next_indice( st, 2 );
450 :
451 993 : if ( ind == 0 )
452 : {
453 0 : st->bwidth = NB;
454 : }
455 993 : else if ( ind == 1 )
456 : {
457 0 : st->bwidth = WB;
458 : }
459 993 : else if ( ind == 2 )
460 : {
461 993 : st->bwidth = SWB;
462 : }
463 : else
464 : {
465 0 : st->bwidth = FB;
466 : }
467 : }
468 : }
469 :
470 : /* detect bit errors in signaling */
471 1881 : if ( ( ( st->total_brate >= ACELP_24k40 && st->bwidth == NB ) ||
472 1881 : ( st->core == HQ_CORE && st->total_brate <= LRMDCT_CROSSOVER_POINT && st->bwidth == FB ) ) )
473 : {
474 0 : st->bfi = 1;
475 :
476 0 : st->core_brate = st->total_brate;
477 0 : st->extl = -1;
478 0 : st->extl_brate = 0;
479 0 : if ( st->last_core == AMR_WB_CORE )
480 : {
481 0 : st->core = ACELP_CORE;
482 0 : st->L_frame = L_FRAME;
483 0 : st->codec_mode = MODE1;
484 0 : st->last_L_frame = L_FRAME;
485 :
486 0 : if ( st->total_brate >= ACELP_16k40 )
487 : {
488 0 : st->total_brate = ACELP_13k20;
489 0 : st->core_brate = st->total_brate;
490 : }
491 : }
492 : else
493 : {
494 : /* make sure, we are in a valid configuration wrt to bandwidth */
495 0 : st->bwidth = WB;
496 : }
497 : }
498 :
499 : /* set HQ core type */
500 1881 : *hq_core_type = NORMAL_HQ_CORE;
501 1881 : if ( ( st->bwidth == SWB || st->bwidth == WB ) && st->total_brate <= LRMDCT_CROSSOVER_POINT )
502 : {
503 162 : *hq_core_type = LOW_RATE_HQ_CORE;
504 : }
505 1719 : else if ( st->bwidth == NB )
506 : {
507 16 : *hq_core_type = LOW_RATE_HQ_CORE;
508 : }
509 : }
510 :
511 : /*-----------------------------------------------------------------*
512 : * Set ACELP frame length
513 : *-----------------------------------------------------------------*/
514 :
515 14314 : if ( st->core_brate == FRAME_NO_DATA )
516 : {
517 : /* prevent "L_frame" changes in CNG segments */
518 0 : st->L_frame = st->last_L_frame;
519 : }
520 14314 : else if ( st->core_brate == SID_2k40 && st->bwidth == WB && st->first_CNG && st->hTdCngDec->act_cnt2 < MIN_ACT_CNG_UPD )
521 : {
522 : /* prevent "L_frame" changes in SID frame after short segment of active frames */
523 0 : st->L_frame = st->last_CNG_L_frame;
524 : }
525 14314 : else if ( ( st->core_brate == SID_2k40 && st->total_brate >= ACELP_9k60 && st->bwidth == WB ) || ( st->total_brate >= ACELP_32k && st->total_brate < HQ_96k ) || ( st->total_brate == ACELP_24k40 && st->bwidth >= WB ) )
526 : {
527 6898 : st->L_frame = L_FRAME16k;
528 : }
529 : else
530 : {
531 7416 : st->L_frame = L_FRAME;
532 : }
533 :
534 14314 : if ( st->L_frame == L_FRAME16k )
535 : {
536 6898 : st->nb_subfr = NB_SUBFR16k;
537 : }
538 : else
539 : {
540 7416 : st->nb_subfr = NB_SUBFR;
541 : }
542 :
543 14314 : st->extl_orig = st->extl;
544 14314 : st->extl_brate_orig = st->extl_brate;
545 :
546 14314 : if ( st->output_Fs == 8000 )
547 : {
548 762 : st->extl = -1;
549 : }
550 13552 : else if ( st->output_Fs == 16000 && st->L_frame == L_FRAME16k )
551 : {
552 1184 : st->extl = -1;
553 1184 : st->extl_brate = 0;
554 : }
555 :
556 14314 : if ( st->ini_frame == 0 )
557 : {
558 : /* avoid switching of internal ACELP Fs in the very first frame */
559 145 : st->last_L_frame = st->L_frame;
560 145 : st->last_core = st->core;
561 145 : st->last_core_brate = st->core_brate;
562 145 : st->last_extl = st->extl;
563 : }
564 :
565 : /*-----------------------------------------------------------------*
566 : * set inactive coder_type flag in ACELP core
567 : *-----------------------------------------------------------------*/
568 :
569 14314 : st->inactive_coder_type_flag = 0; /* AVQ by default */
570 14314 : if ( st->total_brate <= MAX_GSC_INACTIVE_BRATE )
571 : {
572 8126 : st->inactive_coder_type_flag = 1; /* GSC */
573 : }
574 :
575 14314 : return;
576 : }
|