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 : #include <stdint.h>
34 : #include "options.h"
35 : #ifdef DEBUGGING
36 : #include "debug.h"
37 : #endif
38 : #include "stat_dec.h"
39 : #include "rom_com.h"
40 : #include "ivas_prot.h"
41 : #include "prot.h"
42 : #include "ivas_cnst.h"
43 : #include "wmc_auto.h"
44 :
45 : /*-----------------------------------------------------------------*
46 : * ivas_decision_matrix_dec()
47 : *
48 : * ACELP/TCX/HQ core selection
49 : * Read ACELP signaling bits from the bitstream
50 : * Set extension layers
51 : *-----------------------------------------------------------------*/
52 :
53 16250937 : void ivas_decision_matrix_dec(
54 : Decoder_State *st, /* i/o: decoder state structure */
55 : int16_t *sharpFlag, /* o : formant sharpening flag */
56 : int16_t *core_switching_flag, /* o : ACELP->HQ switching frame flag */
57 : const int32_t element_brate, /* i : element bitrate */
58 : const int16_t nchan_out /* i : Number of output channels */
59 : )
60 : {
61 : int16_t tmp;
62 : int32_t icbwe_brate;
63 :
64 : /* init */
65 16250937 : icbwe_brate = 0;
66 16250937 : st->core = -1;
67 16250937 : st->core_brate = 0;
68 16250937 : st->extl = -1;
69 16250937 : st->extl_brate = 0;
70 16250937 : st->ppp_mode_dec = 0;
71 16250937 : st->nelp_mode_dec = 0;
72 16250937 : st->igf = 0;
73 16250937 : st->vbr_hw_BWE_disable_dec = 0;
74 :
75 : /*-----------------------------------------------------------------*
76 : * Read SID signaling bits from the bitstream
77 : *-----------------------------------------------------------------*/
78 :
79 16250937 : if ( ( st->idchan == 0 && ( st->total_brate == FRAME_NO_DATA || st->total_brate == SID_2k40 ) ) || ( st->element_mode == IVAS_CPE_MDCT && st->total_brate <= SID_2k40 ) )
80 : {
81 83714 : st->core = ACELP_CORE;
82 83714 : st->core_brate = st->total_brate;
83 :
84 83714 : if ( st->total_brate == SID_2k40 && !( st->idchan == 1 && st->element_mode == IVAS_CPE_MDCT ) )
85 : {
86 9257 : if ( st->element_mode != IVAS_CPE_DFT )
87 : {
88 4736 : st->cng_type = get_next_indice( st, 1 );
89 :
90 4736 : if ( st->cng_type == FD_CNG )
91 : {
92 4736 : st->bwidth = get_next_indice( st, 2 );
93 : }
94 : }
95 9257 : if ( get_next_indice( st, 1 ) )
96 : {
97 5208 : st->L_frame = L_FRAME16k;
98 : }
99 : else
100 : {
101 4049 : st->L_frame = L_FRAME;
102 : }
103 : }
104 74457 : else if ( st->total_brate == SID_2k40 && st->idchan == 1 && st->element_mode == IVAS_CPE_MDCT && st->cng_sba_flag == 0 )
105 : {
106 : /* read channel coherence */
107 : #ifdef NONBE_MDCT_ST_DTX_FIX_SUBOPT_SPATIAL_CNG
108 1313 : st->hFdCngDec->hFdCngCom->coherence[0] = (float) get_next_indice( st, 4 ) / 15.f;
109 : #else
110 : st->hFdCngDec->hFdCngCom->coherence = (float) get_next_indice( st, 4 ) / 15.f;
111 : #endif
112 :
113 : /* read flag for no side noise shape */
114 1313 : st->hFdCngDec->hFdCngCom->no_side_flag = get_next_indice( st, 1 );
115 : }
116 :
117 83714 : if ( ( st->output_Fs >= 32000 && st->bwidth >= SWB ) || ( st->element_mode == IVAS_CPE_DFT && st->bwidth >= SWB && nchan_out == 2 && st->L_frame < L_FRAME16k ) )
118 : {
119 60935 : st->extl = SWB_CNG;
120 : }
121 :
122 83714 : if ( st->total_brate == FRAME_NO_DATA && st->prev_bfi && !st->bfi && st->L_frame > L_FRAME16k )
123 : {
124 0 : st->L_frame = st->last_CNG_L_frame;
125 : }
126 :
127 83714 : return;
128 : }
129 :
130 : /*---------------------------------------------------------------------*
131 : * ACELP/HQ core selection
132 : *---------------------------------------------------------------------*/
133 :
134 16167223 : if ( st->element_mode == IVAS_CPE_TD && st->idchan == 1 )
135 : {
136 : /* minimal signaling for the secondary channel, most of the parameters are deduced from the primary channel */
137 13720 : st->core = ACELP_CORE;
138 : }
139 16153503 : else if ( st->element_mode == IVAS_SCE && st->low_rate_mode )
140 : {
141 : /* ISM Low-rate mode -> always WB, ACELP core, IC coder_type */
142 25908 : st->core = ACELP_CORE;
143 : }
144 16127595 : else if ( st->element_mode == IVAS_CPE_MDCT )
145 : {
146 11164964 : st->core = TCX_20_CORE;
147 : }
148 : else
149 : {
150 4962631 : st->core = ACELP_CORE;
151 :
152 4962631 : if ( st->element_mode == IVAS_CPE_TD || st->total_brate >= STEREO_TCX_MIN_RATE )
153 : {
154 : /* ACELP/transform core selection bit */
155 4886825 : if ( get_next_indice( st, 1 ) )
156 : {
157 3480932 : st->core = HQ_CORE;
158 : }
159 : else
160 : {
161 1405893 : st->core = ACELP_CORE;
162 : }
163 : }
164 : }
165 :
166 : /*-----------------------------------------------------------------*
167 : * Read ACELP signaling bits from the bitstream
168 : *-----------------------------------------------------------------*/
169 :
170 16167223 : if ( st->core == ACELP_CORE )
171 : {
172 1521327 : if ( st->element_mode == IVAS_SCE && st->low_rate_mode )
173 : {
174 : /* ISM Low-rate mode */
175 25908 : st->bwidth = WB;
176 25908 : st->coder_type = INACTIVE;
177 25908 : *sharpFlag = 0;
178 : }
179 1495419 : else if ( st->idchan == 1 && st->element_mode == IVAS_CPE_TD )
180 : {
181 13720 : *sharpFlag = 0;
182 13720 : if ( st->coder_type == GENERIC || st->coder_type == VOICED )
183 : {
184 13399 : *sharpFlag = 1;
185 : }
186 :
187 13720 : st->core_brate = st->total_brate;
188 13720 : st->codec_mode = MODE1;
189 :
190 13720 : if ( st->idchan == 1 && ( st->tdm_LRTD_flag == 0 || st->bits_frame_channel < IVAS_16k4 / FRAMES_PER_SEC ) )
191 : {
192 927 : st->bwidth = WB; /* only WB in the secondary channel */
193 : }
194 : }
195 : else
196 : {
197 1481699 : if ( element_brate < FRMT_SHP_MIN_BRATE_IVAS )
198 : {
199 827989 : st->coder_type = get_next_indice( st, 3 );
200 827989 : *sharpFlag = 0;
201 :
202 827989 : if ( element_brate < IVAS_24k4 && ( st->coder_type == VOICED || st->coder_type == GENERIC || st->coder_type == TRANSITION ) )
203 : {
204 712274 : *sharpFlag = 1;
205 : }
206 : }
207 : else
208 : {
209 : /* get coder_type info */
210 653710 : st->coder_type = get_next_indice( st, 3 );
211 :
212 : /* get sharpening flag */
213 653710 : *sharpFlag = get_next_indice( st, 1 );
214 : }
215 : }
216 : }
217 :
218 : /*-----------------------------------------------------------------*
219 : * Set extension layers
220 : *-----------------------------------------------------------------*/
221 :
222 16167223 : if ( st->core == ACELP_CORE )
223 : {
224 1521327 : if ( st->bwidth == WB && st->low_rate_mode )
225 : {
226 25908 : st->extl = WB_BWE;
227 25908 : if ( st->total_brate >= MIN_BRATE_WB_BWE )
228 : {
229 211 : st->extl_brate = WB_BWE_0k35;
230 : }
231 : }
232 1495419 : else if ( st->bwidth == WB && ( st->total_brate < MIN_BRATE_WB_BWE || ( st->idchan == 1 && st->element_mode == IVAS_CPE_TD ) ) )
233 : {
234 3863 : if ( st->vbr_hw_BWE_disable_dec == 0 )
235 : {
236 3863 : st->extl = WB_BWE;
237 : }
238 : }
239 1491556 : else if ( st->bwidth == WB && st->total_brate >= MIN_BRATE_WB_BWE && !st->flag_ACELP16k )
240 : {
241 : /* read the WB TBE/BWE selection bit */
242 298815 : if ( get_next_indice( st, 1 ) )
243 : {
244 51205 : st->extl = WB_BWE;
245 51205 : st->extl_brate = WB_BWE_0k35;
246 : }
247 : else
248 : {
249 247610 : st->extl = WB_TBE;
250 247610 : if ( st->total_brate < MIN_BRATE_WB_TBE_1k05 || ( st->element_mode == IVAS_CPE_TD && st->total_brate < MIN_TDM_BRATE_WB_TBE_1k05 ) )
251 : {
252 58103 : st->extl_brate = WB_TBE_0k35;
253 : }
254 : else
255 : {
256 189507 : st->extl_brate = WB_TBE_1k05;
257 : }
258 : }
259 : }
260 1192741 : else if ( st->bwidth == SWB || st->bwidth == FB )
261 : {
262 1006894 : if ( st->total_brate >= MIN_BRATE_SWB_BWE || ( st->total_brate >= MIN_MIN_BRATE_LRTD_SWB_BWE && st->element_mode == IVAS_CPE_TD && st->bwidth == SWB && st->tdm_LRTD_flag ) || ( element_brate < IVAS_16k4 && st->total_brate >= MIN_MIN_BRATE_LRTD_SWB_BWE && st->element_mode == IVAS_CPE_TD && st->bwidth == SWB ) )
263 : {
264 : /* read the SWB TBE/BWE selection bit */
265 1006506 : tmp = get_next_indice( st, 1 );
266 :
267 1006506 : if ( tmp )
268 : {
269 85948 : st->extl = SWB_BWE;
270 85948 : st->extl_brate = SWB_BWE_1k6;
271 : }
272 : else
273 : {
274 920558 : st->extl = SWB_TBE;
275 920558 : st->extl_brate = SWB_TBE_1k6;
276 920558 : if ( st->total_brate >= MIN_BRATE_SWB_TBE_2k80 && st->flag_ACELP16k && st->element_mode == IVAS_SCE )
277 : {
278 196646 : st->extl_brate = SWB_TBE_2k8;
279 : }
280 723912 : else if ( st->tdm_LRTD_flag == 1 && st->element_mode == IVAS_CPE_TD )
281 : {
282 19368 : if ( st->element_brate < IVAS_24k4 )
283 : {
284 2610 : st->extl_brate = SWB_TBE_1k10;
285 : }
286 : else
287 : {
288 16758 : st->extl_brate = SWB_TBE_1k75;
289 : }
290 : }
291 704544 : else if ( st->total_brate < MIN_BRATE_SWB_TBE_1k60 )
292 : {
293 330400 : st->extl_brate = SWB_TBE_0k95;
294 : }
295 : }
296 : }
297 : else
298 : {
299 388 : st->extl = WB_BWE;
300 388 : st->extl_brate = 0;
301 : }
302 :
303 : /* set FB TBE and FB BWE extension layers */
304 1006894 : if ( st->bwidth == FB )
305 : {
306 255730 : if ( st->extl == SWB_BWE )
307 : {
308 20518 : st->extl = FB_BWE;
309 20518 : st->extl_brate = FB_BWE_1k8;
310 : }
311 235212 : else if ( st->extl == SWB_TBE )
312 : {
313 235196 : st->extl = FB_TBE;
314 235196 : st->extl_brate = FB_TBE_1k8;
315 235196 : if ( st->total_brate >= MIN_BRATE_SWB_TBE_2k80 && st->flag_ACELP16k && st->element_mode == IVAS_SCE )
316 : {
317 121956 : st->extl_brate = FB_TBE_3k0;
318 : }
319 : }
320 : }
321 :
322 : /* set IC-BWE bitrate */
323 1006894 : if ( st->element_mode == IVAS_CPE_TD && !( st->bwidth >= SWB && st->tdm_LRTD_flag ) )
324 : {
325 447 : icbwe_brate = STEREO_BITS_ICBWE * FRAMES_PER_SEC;
326 447 : if ( st->flag_ACELP16k == 0 )
327 : {
328 27 : icbwe_brate = ( STEREO_BITS_ICBWE - STEREO_ICBWE_SPBITS ) * FRAMES_PER_SEC;
329 : }
330 : }
331 1006447 : else if ( st->element_mode == IVAS_CPE_DFT )
332 : {
333 210287 : icbwe_brate = STEREO_BITS_ICBWE_DFT * FRAMES_PER_SEC;
334 210287 : if ( st->flag_ACELP16k == 0 )
335 : {
336 147817 : icbwe_brate = ( STEREO_BITS_ICBWE_DFT - STEREO_ICBWE_SPBITS ) * FRAMES_PER_SEC;
337 : }
338 : }
339 :
340 1006894 : if ( st->element_mode >= IVAS_CPE_DFT && st->core == ACELP_CORE && ( st->extl == SWB_TBE || st->extl == FB_TBE ) && !( st->element_mode == IVAS_CPE_TD && st->tdm_LRTD_flag ) )
341 : {
342 177853 : icbwe_brate += STEREO_ICBWE_MSFLAG_BITS * FRAMES_PER_SEC;
343 : }
344 : }
345 : }
346 :
347 : /* set core bitrate */
348 16167223 : st->core_brate = st->total_brate - st->extl_brate - icbwe_brate;
349 :
350 : /*-----------------------------------------------------------------*
351 : * Read transform core (TCX vs. HQ) signaling bit from the bitstream
352 : *-----------------------------------------------------------------*/
353 :
354 16167223 : if ( st->element_mode != IVAS_CPE_MDCT && !( st->idchan == 1 && st->element_mode == IVAS_CPE_TD ) && st->core == HQ_CORE )
355 : {
356 3480932 : if ( get_next_indice( st, 1 ) )
357 : {
358 3443908 : st->core = TCX_20_CORE;
359 : }
360 : else
361 : {
362 37024 : st->core = HQ_CORE;
363 : }
364 : }
365 :
366 16167223 : if ( st->element_mode != IVAS_CPE_MDCT && st->core == TCX_20_CORE )
367 : {
368 3443908 : st->extl = IGF_BWE;
369 3443908 : st->extl_brate = 0;
370 : }
371 :
372 : /*-----------------------------------------------------------------*
373 : * Read ACELP->HQ core switching flag
374 : *-----------------------------------------------------------------*/
375 :
376 16167223 : if ( st->core == HQ_CORE || st->core == TCX_20_CORE )
377 : {
378 14645896 : if ( st->core == HQ_CORE )
379 : {
380 : /* read ACELP->HQ core switching flag */
381 37024 : *core_switching_flag = get_next_indice( st, 1 );
382 : }
383 : else
384 : {
385 14608872 : *core_switching_flag = 0;
386 : }
387 :
388 14645896 : if ( *core_switching_flag == 1 )
389 : {
390 760 : st->last_core_from_bs = ACELP_CORE;
391 :
392 760 : if ( st->core == st->last_core )
393 : {
394 : /* A mismatch between the core_switching_flag and the st->core/st->last_core
395 : indicates a frame was lost. If prev_bfi is not set the frame loss
396 : occured during CNG and the prev_bfi needs to be set. */
397 12 : st->prev_bfi = 1;
398 : }
399 : }
400 : else
401 : {
402 14645136 : st->last_core_from_bs = HQ_CORE; /* Could also be TCX, but it does not make any difference */
403 : }
404 :
405 14645896 : st->last_L_frame_ori = st->last_L_frame;
406 : }
407 :
408 : /*-----------------------------------------------------------------*
409 : * Set ACELP frame length
410 : *-----------------------------------------------------------------*/
411 :
412 16167223 : if ( st->core_brate == FRAME_NO_DATA )
413 : {
414 : /* prevent "L_frame" changes in CNG segments */
415 0 : st->L_frame = st->last_L_frame;
416 : }
417 16167223 : else if ( st->core_brate == SID_2k40 && st->bwidth == WB && st->first_CNG && st->hTdCngDec->act_cnt2 < MIN_ACT_CNG_UPD )
418 : {
419 : /* prevent "L_frame" changes in SID frame after short segment of active frames */
420 0 : st->L_frame = st->last_CNG_L_frame;
421 : }
422 16167223 : else if ( ( st->core_brate == SID_2k40 && st->total_brate >= ACELP_9k60 && st->bwidth == WB ) || st->flag_ACELP16k )
423 : {
424 14581817 : st->L_frame = L_FRAME16k;
425 : }
426 : else
427 : {
428 1585406 : st->L_frame = L_FRAME;
429 : }
430 :
431 16167223 : if ( st->L_frame == L_FRAME16k )
432 : {
433 14581817 : st->nb_subfr = NB_SUBFR16k;
434 : }
435 : else
436 : {
437 1585406 : st->nb_subfr = NB_SUBFR;
438 : }
439 :
440 : /*-----------------------------------------------------------------*
441 : * set inactive coder_type flag in ACELP core
442 : *-----------------------------------------------------------------*/
443 :
444 16167223 : st->inactive_coder_type_flag = 0; /* AVQ by default */
445 :
446 16167223 : if ( st->total_brate <= MAX_GSC_INACTIVE_BRATE )
447 : {
448 2768102 : st->inactive_coder_type_flag = 1; /* GSC */
449 : }
450 :
451 : /*-----------------------------------------------------------------*
452 : * Reconfigure in case when output_Fs < input_Fs
453 : *-----------------------------------------------------------------*/
454 :
455 16167223 : st->extl_orig = st->extl;
456 16167223 : st->extl_brate_orig = st->extl_brate;
457 :
458 16167223 : if ( st->output_Fs == 16000 && st->L_frame == L_FRAME16k && st->extl != IGF_BWE )
459 : {
460 2958722 : st->extl = -1;
461 2958722 : st->extl_brate = 0;
462 : }
463 :
464 16167223 : if ( st->ini_frame == 0 )
465 : {
466 : /* avoid switching of internal ACELP Fs in the very first frame */
467 308899 : st->last_L_frame = st->L_frame;
468 308899 : st->last_core = st->core;
469 308899 : st->last_core_brate = st->core_brate;
470 308899 : st->last_extl = st->extl;
471 : }
472 :
473 16167223 : return;
474 : }
|