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