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 32986423 : 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 32986423 : icbwe_brate = 0;
66 32986423 : st->core = -1;
67 32986423 : st->core_brate = 0;
68 32986423 : st->extl = -1;
69 32986423 : st->extl_brate = 0;
70 32986423 : st->ppp_mode_dec = 0;
71 32986423 : st->nelp_mode_dec = 0;
72 32986423 : st->igf = 0;
73 32986423 : st->vbr_hw_BWE_disable_dec = 0;
74 :
75 : /*-----------------------------------------------------------------*
76 : * Read SID signaling bits from the bitstream
77 : *-----------------------------------------------------------------*/
78 :
79 32986423 : 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 161179 : st->core = ACELP_CORE;
82 161179 : st->core_brate = st->total_brate;
83 :
84 161179 : if ( st->total_brate == SID_2k40 && !( st->idchan == 1 && st->element_mode == IVAS_CPE_MDCT ) )
85 : {
86 18336 : if ( st->element_mode != IVAS_CPE_DFT )
87 : {
88 9971 : st->cng_type = get_next_indice( st, 1 );
89 :
90 9971 : if ( st->cng_type == FD_CNG )
91 : {
92 9833 : st->bwidth = get_next_indice( st, 2 );
93 : }
94 : }
95 18336 : if ( get_next_indice( st, 1 ) )
96 : {
97 11189 : st->L_frame = L_FRAME16k;
98 : }
99 : else
100 : {
101 7147 : st->L_frame = L_FRAME;
102 : }
103 : }
104 142843 : 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 2386 : st->hFdCngDec->hFdCngCom->coherence = (float) get_next_indice( st, 4 ) / 15.f;
108 :
109 : /* read flag for no side noise shape */
110 2386 : st->hFdCngDec->hFdCngCom->no_side_flag = get_next_indice( st, 1 );
111 : }
112 :
113 161179 : 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 117068 : st->extl = SWB_CNG;
116 : }
117 :
118 161179 : 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 161179 : return;
124 : }
125 :
126 : /*---------------------------------------------------------------------*
127 : * ACELP/HQ core selection
128 : *---------------------------------------------------------------------*/
129 :
130 32825244 : 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 37897 : st->core = ACELP_CORE;
134 : }
135 32787347 : 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 279804 : st->core = ACELP_CORE;
139 : }
140 32507543 : else if ( st->element_mode == IVAS_CPE_MDCT )
141 : {
142 23734032 : st->core = TCX_20_CORE;
143 : }
144 : else
145 : {
146 8773511 : st->core = ACELP_CORE;
147 :
148 8773511 : if ( st->element_mode == IVAS_CPE_TD || st->total_brate >= STEREO_TCX_MIN_RATE )
149 : {
150 : /* ACELP/transform core selection bit */
151 8678944 : if ( get_next_indice( st, 1 ) )
152 : {
153 6080799 : st->core = HQ_CORE;
154 : }
155 : else
156 : {
157 2598145 : st->core = ACELP_CORE;
158 : }
159 : }
160 : }
161 :
162 : /*-----------------------------------------------------------------*
163 : * Read ACELP signaling bits from the bitstream
164 : *-----------------------------------------------------------------*/
165 :
166 32825244 : if ( st->core == ACELP_CORE )
167 : {
168 3010413 : if ( st->element_mode == IVAS_SCE && st->low_rate_mode )
169 : {
170 : /* ISM Low-rate mode */
171 279804 : st->bwidth = WB;
172 279804 : st->coder_type = INACTIVE;
173 279804 : *sharpFlag = 0;
174 : }
175 2730609 : else if ( st->idchan == 1 && st->element_mode == IVAS_CPE_TD )
176 : {
177 37897 : *sharpFlag = 0;
178 37897 : if ( st->coder_type == GENERIC || st->coder_type == VOICED )
179 : {
180 30402 : *sharpFlag = 1;
181 : }
182 :
183 37897 : st->core_brate = st->total_brate;
184 37897 : st->codec_mode = MODE1;
185 :
186 37897 : if ( st->idchan == 1 && ( st->tdm_LRTD_flag == 0 || st->bits_frame_channel < IVAS_16k4 / FRAMES_PER_SEC ) )
187 : {
188 2935 : st->bwidth = WB; /* only WB in the secondary channel */
189 : }
190 : }
191 : else
192 : {
193 2692712 : if ( element_brate < FRMT_SHP_MIN_BRATE_IVAS )
194 : {
195 1264564 : st->coder_type = get_next_indice( st, 3 );
196 1264564 : *sharpFlag = 0;
197 :
198 1264564 : if ( element_brate < IVAS_24k4 && ( st->coder_type == VOICED || st->coder_type == GENERIC || st->coder_type == TRANSITION ) )
199 : {
200 1079699 : *sharpFlag = 1;
201 : }
202 : }
203 : else
204 : {
205 : /* get coder_type info */
206 1428148 : st->coder_type = get_next_indice( st, 3 );
207 :
208 : /* get sharpening flag */
209 1428148 : *sharpFlag = get_next_indice( st, 1 );
210 : }
211 : }
212 : }
213 :
214 : /*-----------------------------------------------------------------*
215 : * Set extension layers
216 : *-----------------------------------------------------------------*/
217 :
218 32825244 : if ( st->core == ACELP_CORE )
219 : {
220 3010413 : if ( st->bwidth == WB && st->low_rate_mode )
221 : {
222 279804 : st->extl = WB_BWE;
223 279804 : if ( st->total_brate >= MIN_BRATE_WB_BWE )
224 : {
225 7240 : st->extl_brate = WB_BWE_0k35;
226 : }
227 : }
228 2730609 : else if ( st->bwidth == WB && ( st->total_brate < MIN_BRATE_WB_BWE || ( st->idchan == 1 && st->element_mode == IVAS_CPE_TD ) ) )
229 : {
230 7330 : if ( st->vbr_hw_BWE_disable_dec == 0 )
231 : {
232 7330 : st->extl = WB_BWE;
233 : }
234 : }
235 2723279 : 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 367983 : if ( get_next_indice( st, 1 ) )
239 : {
240 66053 : st->extl = WB_BWE;
241 66053 : st->extl_brate = WB_BWE_0k35;
242 : }
243 : else
244 : {
245 301930 : st->extl = WB_TBE;
246 301930 : 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 65215 : st->extl_brate = WB_TBE_0k35;
249 : }
250 : else
251 : {
252 236715 : st->extl_brate = WB_TBE_1k05;
253 : }
254 : }
255 : }
256 2355296 : else if ( st->bwidth == SWB || st->bwidth == FB )
257 : {
258 1984519 : 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 1979660 : tmp = get_next_indice( st, 1 );
262 :
263 1979660 : if ( tmp )
264 : {
265 147986 : st->extl = SWB_BWE;
266 147986 : st->extl_brate = SWB_BWE_1k6;
267 : }
268 : else
269 : {
270 1831674 : st->extl = SWB_TBE;
271 1831674 : st->extl_brate = SWB_TBE_1k6;
272 1831674 : if ( st->total_brate >= MIN_BRATE_SWB_TBE_2k80 && st->flag_ACELP16k && st->element_mode == IVAS_SCE )
273 : {
274 399026 : st->extl_brate = SWB_TBE_2k8;
275 : }
276 1432648 : else if ( st->tdm_LRTD_flag == 1 && st->element_mode == IVAS_CPE_TD )
277 : {
278 55454 : if ( st->element_brate < IVAS_24k4 )
279 : {
280 3605 : st->extl_brate = SWB_TBE_1k10;
281 : }
282 : else
283 : {
284 51849 : st->extl_brate = SWB_TBE_1k75;
285 : }
286 : }
287 1377194 : else if ( st->total_brate < MIN_BRATE_SWB_TBE_1k60 )
288 : {
289 526980 : st->extl_brate = SWB_TBE_0k95;
290 : }
291 : }
292 : }
293 : else
294 : {
295 4859 : st->extl = WB_BWE;
296 4859 : st->extl_brate = 0;
297 : }
298 :
299 : /* set FB TBE and FB BWE extension layers */
300 1984519 : if ( st->bwidth == FB )
301 : {
302 593235 : if ( st->extl == SWB_BWE )
303 : {
304 28761 : st->extl = FB_BWE;
305 28761 : st->extl_brate = FB_BWE_1k8;
306 : }
307 564474 : else if ( st->extl == SWB_TBE )
308 : {
309 560912 : st->extl = FB_TBE;
310 560912 : st->extl_brate = FB_TBE_1k8;
311 560912 : if ( st->total_brate >= MIN_BRATE_SWB_TBE_2k80 && st->flag_ACELP16k && st->element_mode == IVAS_SCE )
312 : {
313 251301 : st->extl_brate = FB_TBE_3k0;
314 : }
315 : }
316 : }
317 :
318 : /* set IC-BWE bitrate */
319 1984519 : if ( st->element_mode == IVAS_CPE_TD && !( st->bwidth >= SWB && st->tdm_LRTD_flag ) )
320 : {
321 1941 : icbwe_brate = STEREO_BITS_ICBWE * FRAMES_PER_SEC;
322 1941 : if ( st->flag_ACELP16k == 0 )
323 : {
324 48 : icbwe_brate = ( STEREO_BITS_ICBWE - STEREO_ICBWE_SPBITS ) * FRAMES_PER_SEC;
325 : }
326 : }
327 1982578 : else if ( st->element_mode == IVAS_CPE_DFT )
328 : {
329 462627 : icbwe_brate = STEREO_BITS_ICBWE_DFT * FRAMES_PER_SEC;
330 462627 : if ( st->flag_ACELP16k == 0 )
331 : {
332 270685 : icbwe_brate = ( STEREO_BITS_ICBWE_DFT - STEREO_ICBWE_SPBITS ) * FRAMES_PER_SEC;
333 : }
334 : }
335 :
336 1984519 : 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 393260 : icbwe_brate += STEREO_ICBWE_MSFLAG_BITS * FRAMES_PER_SEC;
339 : }
340 : }
341 : }
342 :
343 : /* set core bitrate */
344 32825244 : 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 32825244 : if ( st->element_mode != IVAS_CPE_MDCT && !( st->idchan == 1 && st->element_mode == IVAS_CPE_TD ) && st->core == HQ_CORE )
351 : {
352 6080799 : if ( get_next_indice( st, 1 ) )
353 : {
354 5964356 : st->core = TCX_20_CORE;
355 : }
356 : else
357 : {
358 116443 : st->core = HQ_CORE;
359 : }
360 : }
361 :
362 32825244 : if ( st->element_mode != IVAS_CPE_MDCT && st->core == TCX_20_CORE )
363 : {
364 5964356 : st->extl = IGF_BWE;
365 5964356 : st->extl_brate = 0;
366 : }
367 :
368 : /*-----------------------------------------------------------------*
369 : * Read ACELP->HQ core switching flag
370 : *-----------------------------------------------------------------*/
371 :
372 32825244 : if ( st->core == HQ_CORE || st->core == TCX_20_CORE )
373 : {
374 29814831 : if ( st->core == HQ_CORE )
375 : {
376 : /* read ACELP->HQ core switching flag */
377 116443 : *core_switching_flag = get_next_indice( st, 1 );
378 : }
379 : else
380 : {
381 29698388 : *core_switching_flag = 0;
382 : }
383 :
384 29814831 : if ( *core_switching_flag == 1 )
385 : {
386 2876 : st->last_core_from_bs = ACELP_CORE;
387 :
388 2876 : 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 100 : st->prev_bfi = 1;
394 : }
395 : }
396 : else
397 : {
398 29811955 : st->last_core_from_bs = HQ_CORE; /* Could also be TCX, but it does not make any difference */
399 : }
400 :
401 29814831 : st->last_L_frame_ori = st->last_L_frame;
402 : }
403 :
404 : /*-----------------------------------------------------------------*
405 : * Set ACELP frame length
406 : *-----------------------------------------------------------------*/
407 :
408 32825244 : 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 32825244 : 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 32825244 : else if ( ( st->core_brate == SID_2k40 && st->total_brate >= ACELP_9k60 && st->bwidth == WB ) || st->flag_ACELP16k )
419 : {
420 30010833 : st->L_frame = L_FRAME16k;
421 : }
422 : else
423 : {
424 2814411 : st->L_frame = L_FRAME;
425 : }
426 :
427 32825244 : if ( st->L_frame == L_FRAME16k )
428 : {
429 30010833 : st->nb_subfr = NB_SUBFR16k;
430 : }
431 : else
432 : {
433 2814411 : st->nb_subfr = NB_SUBFR;
434 : }
435 :
436 : /*-----------------------------------------------------------------*
437 : * set inactive coder_type flag in ACELP core
438 : *-----------------------------------------------------------------*/
439 :
440 32825244 : st->inactive_coder_type_flag = 0; /* AVQ by default */
441 :
442 32825244 : if ( st->total_brate <= MAX_GSC_INACTIVE_BRATE )
443 : {
444 5828307 : st->inactive_coder_type_flag = 1; /* GSC */
445 : }
446 :
447 : /*-----------------------------------------------------------------*
448 : * Reconfigure in case when output_Fs < input_Fs
449 : *-----------------------------------------------------------------*/
450 :
451 32825244 : st->extl_orig = st->extl;
452 32825244 : st->extl_brate_orig = st->extl_brate;
453 :
454 32825244 : if ( st->output_Fs == 16000 && st->L_frame == L_FRAME16k && st->extl != IGF_BWE )
455 : {
456 5025686 : st->extl = -1;
457 5025686 : st->extl_brate = 0;
458 : }
459 :
460 32825244 : if ( st->ini_frame == 0 )
461 : {
462 : /* avoid switching of internal ACELP Fs in the very first frame */
463 413152 : st->last_L_frame = st->L_frame;
464 413152 : st->last_core = st->core;
465 413152 : st->last_core_brate = st->core_brate;
466 413152 : st->last_extl = st->extl;
467 : }
468 :
469 32825244 : return;
470 : }
|