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