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 3323415 : 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 3323415 : icbwe_brate = 0;
66 3323415 : st->core = -1;
67 3323415 : st->core_brate = 0;
68 3323415 : st->extl = -1;
69 3323415 : st->extl_brate = 0;
70 3323415 : st->ppp_mode_dec = 0;
71 3323415 : st->nelp_mode_dec = 0;
72 3323415 : st->igf = 0;
73 3323415 : st->vbr_hw_BWE_disable_dec = 0;
74 :
75 : /*-----------------------------------------------------------------*
76 : * Read SID signaling bits from the bitstream
77 : *-----------------------------------------------------------------*/
78 :
79 3323415 : 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 77808 : st->core = ACELP_CORE;
82 77808 : st->core_brate = st->total_brate;
83 :
84 77808 : if ( st->total_brate == SID_2k40 && !( st->idchan == 1 && st->element_mode == IVAS_CPE_MDCT ) )
85 : {
86 8481 : if ( st->element_mode != IVAS_CPE_DFT )
87 : {
88 4194 : st->cng_type = get_next_indice( st, 1 );
89 :
90 4194 : if ( st->cng_type == FD_CNG )
91 : {
92 4194 : st->bwidth = get_next_indice( st, 2 );
93 : }
94 : }
95 8481 : if ( get_next_indice( st, 1 ) )
96 : {
97 4920 : st->L_frame = L_FRAME16k;
98 : }
99 : else
100 : {
101 3561 : st->L_frame = L_FRAME;
102 : }
103 : }
104 69327 : 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 1203 : st->hFdCngDec->hFdCngCom->coherence = (float) get_next_indice( st, 4 ) / 15.f;
108 :
109 : /* read flag for no side noise shape */
110 1203 : st->hFdCngDec->hFdCngCom->no_side_flag = get_next_indice( st, 1 );
111 : }
112 :
113 77808 : 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 ) )
114 : {
115 55803 : st->extl = SWB_CNG;
116 : }
117 :
118 77808 : if ( st->total_brate == FRAME_NO_DATA && st->prev_bfi && !st->bfi && st->L_frame > L_FRAME16k )
119 : {
120 0 : st->L_frame = st->last_CNG_L_frame;
121 : }
122 :
123 77808 : return;
124 : }
125 :
126 : /*---------------------------------------------------------------------*
127 : * ACELP/HQ core selection
128 : *---------------------------------------------------------------------*/
129 :
130 3245607 : if ( st->element_mode == IVAS_CPE_TD && st->idchan == 1 )
131 : {
132 : /* minimal signaling for the secondary channel, most of the parameters are deduced from the primary channel */
133 10956 : st->core = ACELP_CORE;
134 : }
135 3234651 : else if ( st->element_mode == IVAS_SCE && st->low_rate_mode )
136 : {
137 : /* ISM Low-rate mode -> always WB, ACELP core, IC coder_type */
138 22428 : st->core = ACELP_CORE;
139 : }
140 3212223 : else if ( st->element_mode == IVAS_CPE_MDCT )
141 : {
142 2076078 : st->core = TCX_20_CORE;
143 : }
144 : else
145 : {
146 1136145 : st->core = ACELP_CORE;
147 :
148 1136145 : if ( st->element_mode == IVAS_CPE_TD || st->total_brate >= STEREO_TCX_MIN_RATE )
149 : {
150 : /* ACELP/transform core selection bit */
151 1111731 : if ( get_next_indice( st, 1 ) )
152 : {
153 713805 : st->core = HQ_CORE;
154 : }
155 : else
156 : {
157 397926 : st->core = ACELP_CORE;
158 : }
159 : }
160 : }
161 :
162 : /*-----------------------------------------------------------------*
163 : * Read ACELP signaling bits from the bitstream
164 : *-----------------------------------------------------------------*/
165 :
166 3245607 : if ( st->core == ACELP_CORE )
167 : {
168 455724 : if ( st->element_mode == IVAS_SCE && st->low_rate_mode )
169 : {
170 : /* ISM Low-rate mode */
171 22428 : st->bwidth = WB;
172 22428 : st->coder_type = INACTIVE;
173 22428 : *sharpFlag = 0;
174 : }
175 433296 : else if ( st->idchan == 1 && st->element_mode == IVAS_CPE_TD )
176 : {
177 10956 : *sharpFlag = 0;
178 10956 : if ( st->coder_type == GENERIC || st->coder_type == VOICED )
179 : {
180 10671 : *sharpFlag = 1;
181 : }
182 :
183 10956 : st->core_brate = st->total_brate;
184 10956 : st->codec_mode = MODE1;
185 :
186 10956 : if ( st->idchan == 1 && ( st->tdm_LRTD_flag == 0 || st->bits_frame_channel < IVAS_16k4 / FRAMES_PER_SEC ) )
187 : {
188 354 : st->bwidth = WB; /* only WB in the secondary channel */
189 : }
190 : }
191 : else
192 : {
193 422340 : if ( element_brate < FRMT_SHP_MIN_BRATE_IVAS )
194 : {
195 202284 : st->coder_type = get_next_indice( st, 3 );
196 202284 : *sharpFlag = 0;
197 :
198 202284 : if ( element_brate < IVAS_24k4 && ( st->coder_type == VOICED || st->coder_type == GENERIC || st->coder_type == TRANSITION ) )
199 : {
200 159606 : *sharpFlag = 1;
201 : }
202 : }
203 : else
204 : {
205 : /* get coder_type info */
206 220056 : st->coder_type = get_next_indice( st, 3 );
207 :
208 : /* get sharpening flag */
209 220056 : *sharpFlag = get_next_indice( st, 1 );
210 : }
211 : }
212 : }
213 :
214 : /*-----------------------------------------------------------------*
215 : * Set extension layers
216 : *-----------------------------------------------------------------*/
217 :
218 3245607 : if ( st->core == ACELP_CORE )
219 : {
220 455724 : if ( st->bwidth == WB && st->low_rate_mode )
221 : {
222 22428 : st->extl = WB_BWE;
223 22428 : if ( st->total_brate >= MIN_BRATE_WB_BWE )
224 : {
225 183 : st->extl_brate = WB_BWE_0k35;
226 : }
227 : }
228 433296 : else if ( st->bwidth == WB && ( st->total_brate < MIN_BRATE_WB_BWE || ( st->idchan == 1 && st->element_mode == IVAS_CPE_TD ) ) )
229 : {
230 2658 : if ( st->vbr_hw_BWE_disable_dec == 0 )
231 : {
232 2658 : st->extl = WB_BWE;
233 : }
234 : }
235 430638 : else if ( st->bwidth == WB && st->total_brate >= MIN_BRATE_WB_BWE && !st->flag_ACELP16k )
236 : {
237 : /* read the WB TBE/BWE selection bit */
238 32898 : if ( get_next_indice( st, 1 ) )
239 : {
240 12081 : st->extl = WB_BWE;
241 12081 : st->extl_brate = WB_BWE_0k35;
242 : }
243 : else
244 : {
245 20817 : st->extl = WB_TBE;
246 20817 : if ( st->total_brate < MIN_BRATE_WB_TBE_1k05 || ( st->element_mode == IVAS_CPE_TD && st->total_brate < MIN_TDM_BRATE_WB_TBE_1k05 ) )
247 : {
248 7341 : st->extl_brate = WB_TBE_0k35;
249 : }
250 : else
251 : {
252 13476 : st->extl_brate = WB_TBE_1k05;
253 : }
254 : }
255 : }
256 397740 : else if ( st->bwidth == SWB || st->bwidth == FB )
257 : {
258 372348 : 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 ) )
259 : {
260 : /* read the SWB TBE/BWE selection bit */
261 372348 : tmp = get_next_indice( st, 1 );
262 :
263 372348 : if ( tmp )
264 : {
265 35238 : st->extl = SWB_BWE;
266 35238 : st->extl_brate = SWB_BWE_1k6;
267 : }
268 : else
269 : {
270 337110 : st->extl = SWB_TBE;
271 337110 : st->extl_brate = SWB_TBE_1k6;
272 337110 : if ( st->total_brate >= MIN_BRATE_SWB_TBE_2k80 && st->flag_ACELP16k && st->element_mode == IVAS_SCE )
273 : {
274 73923 : st->extl_brate = SWB_TBE_2k8;
275 : }
276 263187 : else if ( st->tdm_LRTD_flag == 1 && st->element_mode == IVAS_CPE_TD )
277 : {
278 16335 : if ( st->element_brate < IVAS_24k4 )
279 : {
280 2550 : st->extl_brate = SWB_TBE_1k10;
281 : }
282 : else
283 : {
284 13785 : st->extl_brate = SWB_TBE_1k75;
285 : }
286 : }
287 246852 : else if ( st->total_brate < MIN_BRATE_SWB_TBE_1k60 )
288 : {
289 94608 : st->extl_brate = SWB_TBE_0k95;
290 : }
291 : }
292 : }
293 : else
294 : {
295 0 : st->extl = WB_BWE;
296 0 : st->extl_brate = 0;
297 : }
298 :
299 : /* set FB TBE and FB BWE extension layers */
300 372348 : if ( st->bwidth == FB )
301 : {
302 142596 : if ( st->extl == SWB_BWE )
303 : {
304 14190 : st->extl = FB_BWE;
305 14190 : st->extl_brate = FB_BWE_1k8;
306 : }
307 128406 : else if ( st->extl == SWB_TBE )
308 : {
309 128406 : st->extl = FB_TBE;
310 128406 : st->extl_brate = FB_TBE_1k8;
311 128406 : if ( st->total_brate >= MIN_BRATE_SWB_TBE_2k80 && st->flag_ACELP16k && st->element_mode == IVAS_SCE )
312 : {
313 59460 : st->extl_brate = FB_TBE_3k0;
314 : }
315 : }
316 : }
317 :
318 : /* set IC-BWE bitrate */
319 372348 : if ( st->element_mode == IVAS_CPE_TD && !( st->bwidth >= SWB && st->tdm_LRTD_flag ) )
320 : {
321 78 : icbwe_brate = STEREO_BITS_ICBWE * FRAMES_PER_SEC;
322 78 : if ( st->flag_ACELP16k == 0 )
323 : {
324 27 : icbwe_brate = ( STEREO_BITS_ICBWE - STEREO_ICBWE_SPBITS ) * FRAMES_PER_SEC;
325 : }
326 : }
327 372270 : else if ( st->element_mode == IVAS_CPE_DFT )
328 : {
329 78102 : icbwe_brate = STEREO_BITS_ICBWE_DFT * FRAMES_PER_SEC;
330 78102 : if ( st->flag_ACELP16k == 0 )
331 : {
332 39855 : icbwe_brate = ( STEREO_BITS_ICBWE_DFT - STEREO_ICBWE_SPBITS ) * FRAMES_PER_SEC;
333 : }
334 : }
335 :
336 372348 : 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 ) )
337 : {
338 70401 : icbwe_brate += STEREO_ICBWE_MSFLAG_BITS * FRAMES_PER_SEC;
339 : }
340 : }
341 : }
342 :
343 : /* set core bitrate */
344 3245607 : st->core_brate = st->total_brate - st->extl_brate - icbwe_brate;
345 :
346 : /*-----------------------------------------------------------------*
347 : * Read transform core (TCX vs. HQ) signaling bit from the bitstream
348 : *-----------------------------------------------------------------*/
349 :
350 3245607 : if ( st->element_mode != IVAS_CPE_MDCT && !( st->idchan == 1 && st->element_mode == IVAS_CPE_TD ) && st->core == HQ_CORE )
351 : {
352 713805 : if ( get_next_indice( st, 1 ) )
353 : {
354 690318 : st->core = TCX_20_CORE;
355 : }
356 : else
357 : {
358 23487 : st->core = HQ_CORE;
359 : }
360 : }
361 :
362 3245607 : if ( st->element_mode != IVAS_CPE_MDCT && st->core == TCX_20_CORE )
363 : {
364 690318 : st->extl = IGF_BWE;
365 690318 : st->extl_brate = 0;
366 : }
367 :
368 : /*-----------------------------------------------------------------*
369 : * Read ACELP->HQ core switching flag
370 : *-----------------------------------------------------------------*/
371 :
372 3245607 : if ( st->core == HQ_CORE || st->core == TCX_20_CORE )
373 : {
374 2789883 : if ( st->core == HQ_CORE )
375 : {
376 : /* read ACELP->HQ core switching flag */
377 23487 : *core_switching_flag = get_next_indice( st, 1 );
378 : }
379 : else
380 : {
381 2766396 : *core_switching_flag = 0;
382 : }
383 :
384 2789883 : if ( *core_switching_flag == 1 )
385 : {
386 588 : st->last_core_from_bs = ACELP_CORE;
387 :
388 588 : if ( st->core == st->last_core )
389 : {
390 : /* A mismatch between the core_switching_flag and the st->core/st->last_core
391 : indicates a frame was lost. If prev_bfi is not set the frame loss
392 : occured during CNG and the prev_bfi needs to be set. */
393 12 : st->prev_bfi = 1;
394 : }
395 : }
396 : else
397 : {
398 2789295 : st->last_core_from_bs = HQ_CORE; /* Could also be TCX, but it does not make any difference */
399 : }
400 :
401 2789883 : st->last_L_frame_ori = st->last_L_frame;
402 : }
403 :
404 : /*-----------------------------------------------------------------*
405 : * Set ACELP frame length
406 : *-----------------------------------------------------------------*/
407 :
408 3245607 : if ( st->core_brate == FRAME_NO_DATA )
409 : {
410 : /* prevent "L_frame" changes in CNG segments */
411 0 : st->L_frame = st->last_L_frame;
412 : }
413 3245607 : else if ( st->core_brate == SID_2k40 && st->bwidth == WB && st->first_CNG && st->hTdCngDec->act_cnt2 < MIN_ACT_CNG_UPD )
414 : {
415 : /* prevent "L_frame" changes in SID frame after short segment of active frames */
416 0 : st->L_frame = st->last_CNG_L_frame;
417 : }
418 3245607 : else if ( ( st->core_brate == SID_2k40 && st->total_brate >= ACELP_9k60 && st->bwidth == WB ) || st->flag_ACELP16k )
419 : {
420 2844582 : st->L_frame = L_FRAME16k;
421 : }
422 : else
423 : {
424 401025 : st->L_frame = L_FRAME;
425 : }
426 :
427 3245607 : if ( st->L_frame == L_FRAME16k )
428 : {
429 2844582 : st->nb_subfr = NB_SUBFR16k;
430 : }
431 : else
432 : {
433 401025 : st->nb_subfr = NB_SUBFR;
434 : }
435 :
436 : /*-----------------------------------------------------------------*
437 : * set inactive coder_type flag in ACELP core
438 : *-----------------------------------------------------------------*/
439 :
440 3245607 : st->inactive_coder_type_flag = 0; /* AVQ by default */
441 :
442 3245607 : if ( st->total_brate <= MAX_GSC_INACTIVE_BRATE )
443 : {
444 685335 : st->inactive_coder_type_flag = 1; /* GSC */
445 : }
446 :
447 : /*-----------------------------------------------------------------*
448 : * Reconfigure in case when output_Fs < input_Fs
449 : *-----------------------------------------------------------------*/
450 :
451 3245607 : st->extl_orig = st->extl;
452 3245607 : st->extl_brate_orig = st->extl_brate;
453 :
454 3245607 : if ( st->output_Fs == 16000 && st->L_frame == L_FRAME16k && st->extl != IGF_BWE )
455 : {
456 194439 : st->extl = -1;
457 194439 : st->extl_brate = 0;
458 : }
459 :
460 3245607 : if ( st->ini_frame == 0 )
461 : {
462 : /* avoid switching of internal ACELP Fs in the very first frame */
463 26367 : st->last_L_frame = st->L_frame;
464 26367 : st->last_core = st->core;
465 26367 : st->last_core_brate = st->core_brate;
466 26367 : st->last_extl = st->extl;
467 : }
468 :
469 3245607 : return;
470 : }
|