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