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 111700 : 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 111700 : st->core = -1;
70 111700 : st->core_brate = 0;
71 111700 : st->extl = -1;
72 111700 : st->extl_brate = 0;
73 111700 : st->ppp_mode_dec = 0;
74 111700 : st->nelp_mode_dec = 0;
75 111700 : st->igf = 0;
76 :
77 111700 : if ( st->total_brate > ACELP_8k00 )
78 : {
79 97645 : st->vbr_hw_BWE_disable_dec = 0;
80 : }
81 :
82 111700 : if ( st->mdct_sw == MODE2 )
83 : {
84 3105 : st->core = HQ_CORE;
85 : }
86 : else
87 : {
88 108595 : 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 106566 : else if ( st->total_brate == PPP_NELP_2k80 )
138 : {
139 433 : st->core = ACELP_CORE;
140 433 : st->core_brate = PPP_NELP_2k80;
141 433 : st->L_frame = L_FRAME;
142 433 : st->fscale = sr2fscale( INT_FS_12k8 );
143 :
144 433 : if ( st->ini_frame == 0 )
145 : {
146 : /* avoid switching of internal ACELP Fs in the very first frame */
147 6 : st->last_L_frame = st->L_frame;
148 6 : st->last_core = st->core;
149 6 : st->last_core_brate = st->core_brate;
150 6 : st->last_extl = st->extl;
151 : }
152 :
153 433 : st->vbr_hw_BWE_disable_dec = 1;
154 :
155 433 : get_next_indice( st, 1 );
156 :
157 433 : ppp_nelp_mode = get_next_indice( st, 2 );
158 :
159 : /* 0 - PPP_NB, 1 - PPP_WB, 2 - NELP_NB, 3 - NELP_WB */
160 433 : if ( ppp_nelp_mode == 0 )
161 : {
162 51 : st->ppp_mode_dec = 1;
163 51 : st->coder_type = VOICED;
164 51 : st->bwidth = NB;
165 : }
166 382 : else if ( ppp_nelp_mode == 1 )
167 : {
168 74 : st->ppp_mode_dec = 1;
169 74 : st->coder_type = VOICED;
170 74 : st->bwidth = WB;
171 : }
172 308 : else if ( ppp_nelp_mode == 2 )
173 : {
174 168 : st->nelp_mode_dec = 1;
175 168 : st->coder_type = UNVOICED;
176 168 : st->bwidth = NB;
177 : }
178 140 : else if ( ppp_nelp_mode == 3 )
179 : {
180 140 : st->nelp_mode_dec = 1;
181 140 : st->coder_type = UNVOICED;
182 140 : st->bwidth = WB;
183 : }
184 :
185 433 : return;
186 : }
187 :
188 : /*---------------------------------------------------------------------*
189 : * ACELP/HQ core selection
190 : *---------------------------------------------------------------------*/
191 :
192 106133 : if ( st->total_brate < ACELP_24k40 )
193 : {
194 52148 : st->core = ACELP_CORE;
195 : }
196 53985 : else if ( st->total_brate >= ACELP_24k40 && st->total_brate <= ACELP_64k )
197 : {
198 : /* read the ACELP/HQ core selection bit */
199 53985 : tmp = get_next_indice( st, 1 );
200 :
201 53985 : if ( tmp == 0 )
202 : {
203 38283 : st->core = ACELP_CORE;
204 : }
205 : else
206 : {
207 15702 : st->core = HQ_CORE;
208 : }
209 : }
210 : }
211 :
212 : /*-----------------------------------------------------------------*
213 : * Read ACELP signaling bits from the bitstream
214 : *-----------------------------------------------------------------*/
215 :
216 109238 : if ( st->core == ACELP_CORE )
217 : {
218 : /* find the section in the ACELP signaling table corresponding to bitrate */
219 90431 : start_idx = 0;
220 4665539 : while ( acelp_sig_tbl[start_idx] != st->total_brate )
221 : {
222 4575108 : start_idx++;
223 4575108 : 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 90431 : start_idx += 1;
233 :
234 : /* retrieve the number of bits */
235 90431 : nBits = (int16_t) acelp_sig_tbl[start_idx++];
236 :
237 90431 : start_idx += get_next_indice( st, nBits );
238 90431 : 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 90431 : ind = acelp_sig_tbl[start_idx];
247 :
248 : /* convert signaling indice into signaling information */
249 90431 : st->coder_type = ind & 0x7;
250 90431 : if ( st->coder_type == LR_MDCT )
251 : {
252 4389 : st->core = HQ_CORE;
253 4389 : st->bwidth = ( ind >> 3 ) & 0x7;
254 : }
255 : else
256 : {
257 86042 : st->bwidth = ( ind >> 3 ) & 0x7;
258 86042 : *sharpFlag = ( ind >> 6 ) & 0x1;
259 : }
260 : }
261 :
262 : /* detect corrupted signaling (due to bit errors) */
263 90431 : if ( ( st->BER_detect ) ||
264 90431 : ( ind >= 1 << 7 ) ||
265 90431 : ( st->total_brate <= ACELP_13k20 && st->bwidth == FB ) ||
266 90431 : ( st->total_brate >= ACELP_32k && st->bwidth == NB ) ||
267 90431 : ( st->total_brate >= ACELP_32k && !( st->coder_type == GENERIC || st->coder_type == TRANSITION || st->coder_type == INACTIVE ) ) ||
268 90431 : ( st->total_brate < ACELP_13k20 && st->bwidth != NB && st->coder_type == LR_MDCT ) ||
269 90431 : ( st->total_brate >= ACELP_13k20 && st->coder_type == UNVOICED ) ||
270 90431 : ( 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 109238 : if ( st->core == ACELP_CORE && st->bwidth == WB && st->total_brate < ACELP_9k60 )
338 : {
339 8754 : if ( st->vbr_hw_BWE_disable_dec == 0 )
340 : {
341 7653 : st->extl = WB_BWE;
342 : }
343 : }
344 100484 : 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 3695 : 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 3310 : st->extl = WB_TBE;
355 3310 : st->extl_brate = WB_TBE_1k05;
356 : }
357 : }
358 96789 : else if ( st->core == ACELP_CORE && ( st->bwidth == SWB || st->bwidth == FB ) && st->total_brate >= ACELP_13k20 )
359 : {
360 62898 : if ( st->total_brate >= ACELP_48k )
361 : {
362 26558 : st->extl = SWB_BWE_HIGHRATE;
363 26558 : if ( st->bwidth == FB )
364 : {
365 2205 : st->extl = FB_BWE_HIGHRATE;
366 : }
367 :
368 26558 : st->extl_brate = SWB_BWE_16k;
369 : }
370 :
371 : /* read the SWB TBE/BWE selection bit */
372 : else
373 : {
374 36340 : tmp = get_next_indice( st, 1 );
375 :
376 36340 : if ( tmp )
377 : {
378 4128 : st->extl = SWB_BWE;
379 4128 : st->extl_brate = SWB_BWE_1k6;
380 : }
381 : else
382 : {
383 32212 : st->extl = SWB_TBE;
384 32212 : st->extl_brate = SWB_TBE_1k6;
385 32212 : if ( st->total_brate >= ACELP_24k40 )
386 : {
387 5446 : st->extl_brate = SWB_TBE_2k8;
388 : }
389 : }
390 : }
391 :
392 : /* set FB TBE and FB BWE extension layers */
393 62898 : if ( st->bwidth == FB && st->total_brate >= ACELP_24k40 )
394 : {
395 4373 : if ( st->extl == SWB_BWE )
396 : {
397 0 : st->extl = FB_BWE;
398 0 : st->extl_brate = FB_BWE_1k8;
399 : }
400 4373 : else if ( st->extl == SWB_TBE )
401 : {
402 2168 : st->extl = FB_TBE;
403 2168 : st->extl_brate = FB_TBE_3k0;
404 : }
405 : }
406 : }
407 :
408 : /* set core bitrate */
409 109238 : 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 109238 : if ( st->core == HQ_CORE )
417 : {
418 23196 : if ( st->mdct_sw != MODE2 )
419 : {
420 : /* skip the HQ/TCX core switching flag */
421 20091 : get_next_indice_tmp( st, 1 );
422 : }
423 :
424 : /* read ACELP->HQ core switching flag */
425 23196 : *core_switching_flag = get_next_indice( st, 1 );
426 :
427 23196 : if ( *core_switching_flag == 1 )
428 : {
429 802 : st->last_L_frame_ori = st->last_L_frame;
430 :
431 : /* read ACELP L_frame info */
432 802 : if ( get_next_indice( st, 1 ) == 0 )
433 : {
434 155 : st->last_L_frame = L_FRAME;
435 : }
436 : else
437 : {
438 647 : st->last_L_frame = L_FRAME16k;
439 : }
440 : }
441 :
442 23196 : if ( st->mdct_sw != MODE2 )
443 : {
444 : /* read/set band-width (needed for different I/O sampling rate support) */
445 20091 : if ( st->total_brate > ACELP_16k40 )
446 : {
447 15702 : ind = get_next_indice( st, 2 );
448 :
449 15702 : if ( ind == 0 )
450 : {
451 0 : st->bwidth = NB;
452 : }
453 15702 : else if ( ind == 1 )
454 : {
455 2192 : st->bwidth = WB;
456 : }
457 13510 : else if ( ind == 2 )
458 : {
459 12874 : st->bwidth = SWB;
460 : }
461 : else
462 : {
463 636 : st->bwidth = FB;
464 : }
465 : }
466 : }
467 :
468 : /* detect bit errors in signaling */
469 23196 : if ( ( ( st->total_brate >= ACELP_24k40 && st->bwidth == NB ) ||
470 23196 : ( 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 23196 : *hq_core_type = NORMAL_HQ_CORE;
499 23196 : if ( ( st->bwidth == SWB || st->bwidth == WB ) && st->total_brate <= LRMDCT_CROSSOVER_POINT )
500 : {
501 4041 : *hq_core_type = LOW_RATE_HQ_CORE;
502 : }
503 19155 : else if ( st->bwidth == NB )
504 : {
505 425 : *hq_core_type = LOW_RATE_HQ_CORE;
506 : }
507 : }
508 :
509 : /*-----------------------------------------------------------------*
510 : * Set ACELP frame length
511 : *-----------------------------------------------------------------*/
512 :
513 109238 : 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 109238 : 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 109238 : 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 57013 : st->L_frame = L_FRAME16k;
526 : }
527 : else
528 : {
529 52225 : st->L_frame = L_FRAME;
530 : }
531 :
532 109238 : if ( st->L_frame == L_FRAME16k )
533 : {
534 57013 : st->nb_subfr = NB_SUBFR16k;
535 : }
536 : else
537 : {
538 52225 : st->nb_subfr = NB_SUBFR;
539 : }
540 :
541 109238 : st->extl_orig = st->extl;
542 109238 : st->extl_brate_orig = st->extl_brate;
543 :
544 109238 : if ( st->output_Fs == 8000 )
545 : {
546 5493 : st->extl = -1;
547 : }
548 103745 : else if ( st->output_Fs == 16000 && st->L_frame == L_FRAME16k )
549 : {
550 6962 : st->extl = -1;
551 6962 : st->extl_brate = 0;
552 : }
553 :
554 109238 : if ( st->ini_frame == 0 )
555 : {
556 : /* avoid switching of internal ACELP Fs in the very first frame */
557 432 : st->last_L_frame = st->L_frame;
558 432 : st->last_core = st->core;
559 432 : st->last_core_brate = st->core_brate;
560 432 : st->last_extl = st->extl;
561 : }
562 :
563 : /*-----------------------------------------------------------------*
564 : * set inactive coder_type flag in ACELP core
565 : *-----------------------------------------------------------------*/
566 :
567 109238 : st->inactive_coder_type_flag = 0; /* AVQ by default */
568 109238 : if ( st->total_brate <= MAX_GSC_INACTIVE_BRATE )
569 : {
570 55253 : st->inactive_coder_type_flag = 1; /* GSC */
571 : }
572 :
573 109238 : return;
574 : }
|