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 : /*====================================================================================
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 <assert.h>
38 : #include "prot.h"
39 : #include "rom_com.h"
40 : #include <stdint.h>
41 : #include "options.h"
42 : #ifdef DEBUGGING
43 : #include "debug.h"
44 : #endif
45 : #include "stat_dec.h"
46 : #include "wmc_auto.h"
47 :
48 : /*-------------------------------------------------------------------*
49 : * decode_frame_type()
50 : *
51 : *
52 : *--------------------------------------------------------------------*/
53 :
54 3750 : static void decode_frame_type(
55 : Decoder_State *st, /* i/o: decoder state structure */
56 : STEREO_CNG_DEC_HANDLE hStereoCng /* i/o: Stereo CNG data structure */
57 : )
58 : {
59 : int16_t frame_size_index, n;
60 : int32_t total_brate;
61 :
62 3750 : frame_size_index = 0;
63 3750 : total_brate = st->total_brate;
64 :
65 : /* Get Frame Type (NULL,SID,ACTIVE) and Frame Mode (2kbps, 4kbps,...) */
66 :
67 3750 : if ( st->mdct_sw == MODE1 )
68 : {
69 825 : st->m_frame_type = ACTIVE_FRAME;
70 :
71 4950 : for ( n = 0; n < FRAME_SIZE_NB; n++ )
72 : {
73 4950 : if ( FrameSizeConfig[n].frame_bits == st->total_brate / FRAMES_PER_SEC )
74 : {
75 825 : frame_size_index = n;
76 825 : break;
77 : }
78 : }
79 : }
80 : else
81 : {
82 : /* ZERO Frame */
83 2925 : if ( st->total_brate == FRAME_NO_DATA )
84 : {
85 0 : st->bwidth = st->last_bwidth;
86 0 : st->m_frame_type = ZERO_FRAME;
87 : }
88 :
89 : /* SID frame */
90 2925 : else if ( st->total_brate == SID_2k40 )
91 : {
92 : uint16_t frame_len_indicator;
93 :
94 0 : st->cng_type = get_next_indice( st, 1 );
95 :
96 0 : if ( st->cng_type != FD_CNG )
97 : {
98 0 : st->BER_detect = 1;
99 0 : st->cng_type = FD_CNG;
100 : }
101 0 : st->m_frame_type = SID_FRAME;
102 0 : frame_size_index = 1;
103 0 : st->bwidth = get_next_indice( st, 2 );
104 :
105 0 : frame_len_indicator = get_next_indice( st, 1 );
106 0 : if ( st->bwidth == NB )
107 : {
108 0 : if ( frame_len_indicator )
109 : {
110 0 : st->BER_detect = 1;
111 : }
112 0 : frame_len_indicator = 0;
113 : }
114 0 : if ( frame_len_indicator == 0 )
115 : {
116 0 : st->L_frame = L_FRAME;
117 0 : st->total_brate = ACELP_9k60;
118 : }
119 : else
120 : {
121 0 : st->L_frame = L_FRAME16k;
122 0 : if ( st->last_total_brate == ACELP_16k40 || st->last_total_brate == ACELP_24k40 )
123 : {
124 0 : st->total_brate = st->last_total_brate;
125 : }
126 : else
127 : {
128 0 : st->total_brate = 16400;
129 : }
130 : }
131 :
132 0 : for ( n = 0; n < FRAME_SIZE_NB; n++ )
133 : {
134 0 : if ( FrameSizeConfig[n].frame_bits == st->total_brate / FRAMES_PER_SEC )
135 : {
136 0 : frame_size_index = n;
137 0 : break;
138 : }
139 : }
140 : }
141 : /* EVS MODES */
142 : else
143 : {
144 : /* Get Frame mode */
145 2925 : st->m_frame_type = ACTIVE_FRAME;
146 :
147 23400 : for ( n = 0; n < FRAME_SIZE_NB; n++ )
148 : {
149 23400 : if ( FrameSizeConfig[n].frame_bits == st->total_brate / FRAMES_PER_SEC )
150 : {
151 2925 : frame_size_index = n;
152 2925 : break;
153 : }
154 : }
155 :
156 2925 : if ( st->rf_flag == 0 )
157 : {
158 : /* Get bandwidth info */
159 2925 : st->bwidth = get_next_indice( st, FrameSizeConfig[frame_size_index].bandwidth_bits );
160 2925 : st->bwidth += FrameSizeConfig[frame_size_index].bandwidth_min;
161 : }
162 : else
163 : {
164 0 : st->bwidth += FrameSizeConfig[frame_size_index].bandwidth_min;
165 : }
166 :
167 2925 : if ( st->bwidth > FB )
168 : {
169 0 : st->bwidth = FB;
170 0 : st->BER_detect = 1;
171 : }
172 :
173 2925 : if ( st->bwidth > SWB && st->total_brate < ACELP_16k40 )
174 : {
175 0 : st->bwidth = SWB;
176 0 : st->BER_detect = 1;
177 : }
178 :
179 : /* Get reserved bit */
180 2925 : if ( FrameSizeConfig[frame_size_index].reserved_bits && st->rf_flag == 0 )
181 : {
182 2925 : if ( get_next_indice( st, 1 ) != 0 )
183 : {
184 0 : st->BER_detect = 1;
185 : }
186 2925 : assert( FrameSizeConfig[frame_size_index].reserved_bits == 1 );
187 : }
188 : }
189 : }
190 :
191 3750 : st->rate_switching_init = 0;
192 :
193 3750 : if ( st->last_codec_mode != MODE2 || !st->BER_detect )
194 : {
195 : /* Mode or Rate Change */
196 3750 : if ( ( st->m_frame_type == ACTIVE_FRAME || st->m_frame_type == SID_FRAME ) && ( ( st->total_brate != st->last_total_brate ) || ( st->bwidth != st->last_bwidth ) || ( st->last_codec_mode == MODE1 ) || ( st->rf_flag != st->rf_flag_last ) || st->force_lpd_reset ) )
197 : {
198 114 : st->rate_switching_init = 1;
199 :
200 : /* Reconf Core */
201 114 : mode_switch_decoder_LPD( st, st->bwidth, st->total_brate, st->last_total_brate, frame_size_index, 0, st->element_mode );
202 :
203 : /* Reconf. CLDFB: check if the CLDFB works on the right sample rate */
204 114 : if ( ( st->cldfbAna->no_channels * st->cldfbAna->no_col ) != st->L_frame )
205 : {
206 3 : resampleCldfb( st->cldfbAna, ( st->L_frame * FRAMES_PER_SEC ) );
207 3 : if ( st->L_frame <= L_FRAME16k )
208 : {
209 3 : resampleCldfb( st->cldfbBPF, ( st->L_frame * FRAMES_PER_SEC ) );
210 : }
211 : }
212 114 : if ( st->bwidth == NB )
213 : {
214 0 : int16_t nBand_nb = (int16_t) ( 8000 * st->cldfbSyn->no_channels / st->output_Fs );
215 0 : st->cldfbSyn->bandsToZero = st->cldfbSyn->no_channels - nBand_nb;
216 : }
217 : else
218 : {
219 114 : st->cldfbSyn->bandsToZero = 0;
220 : }
221 :
222 : /*Reconf Frequency-domain based CNG*/
223 114 : configureFdCngDec( st->hFdCngDec, st->bwidth, st->rf_flag == 1 && st->total_brate == ACELP_13k20 ? ACELP_9k60 : st->total_brate, st->L_frame, st->last_L_frame, st->element_mode );
224 114 : if ( st->last_L_frame != st->L_frame && st->L_frame <= L_FRAME16k && st->last_L_frame <= L_FRAME16k )
225 : {
226 0 : lerp( st->hFdCngDec->hFdCngCom->olapBufferSynth2, st->hFdCngDec->hFdCngCom->olapBufferSynth2, st->L_frame * 2, st->last_L_frame * 2 );
227 0 : if ( st->m_frame_type == SID_FRAME && st->hFdCngDec->hFdCngCom->frame_type_previous != ACTIVE_FRAME )
228 : {
229 0 : lerp( st->hFdCngDec->hFdCngCom->olapBufferSynth, st->hFdCngDec->hFdCngCom->olapBufferSynth, st->L_frame * 2, st->last_L_frame * 2 );
230 0 : lerp( hStereoCng->olapBufferSynth22, hStereoCng->olapBufferSynth22, st->L_frame * 2, st->last_L_frame * 2 );
231 :
232 0 : if ( st->L_frame == L_FRAME )
233 : {
234 0 : for ( n = 0; n < st->L_frame * 2; n++ )
235 : {
236 0 : st->hFdCngDec->hFdCngCom->olapBufferSynth[n] = st->hFdCngDec->hFdCngCom->olapBufferSynth[n] * 1.25f;
237 : }
238 : }
239 : else
240 : {
241 0 : for ( n = 0; n < st->L_frame * 2; n++ )
242 : {
243 0 : st->hFdCngDec->hFdCngCom->olapBufferSynth[n] = st->hFdCngDec->hFdCngCom->olapBufferSynth[n] / 1.25f;
244 : }
245 : }
246 : }
247 : }
248 :
249 114 : if ( st->bwidth != st->last_bwidth )
250 : {
251 3 : st->hFdCngDec->hFdCngCom->msFrCnt_init_counter = 0;
252 3 : st->hFdCngDec->hFdCngCom->init_old = FLT_MAX;
253 : }
254 :
255 114 : if ( st->tcxonly )
256 : {
257 0 : st->p_bpf_noise_buf = NULL;
258 : }
259 : else
260 : {
261 114 : st->p_bpf_noise_buf = st->bpf_noise_buf;
262 : }
263 : }
264 : }
265 :
266 3750 : st->total_brate = total_brate;
267 :
268 3750 : return;
269 : }
270 :
271 :
272 : /*-------------------------------------------------------------------*
273 : * dec_acelp_tcx_frame()
274 : *
275 : * Main decoding function
276 : *--------------------------------------------------------------------*/
277 :
278 3750 : void dec_acelp_tcx_frame(
279 : Decoder_State *st, /* i/o: decoder state structure */
280 : int16_t *concealWholeFrame, /* i/o: concealment flag */
281 : float *output, /* o : synthesis */
282 : float *bpf_noise_buf, /* i/o: BPF noise buffer */
283 : float *pcmbufFB, /* o : synthesis @output_FS */
284 : float bwe_exc_extended[], /* i/o: bandwidth extended excitation */
285 : float *voice_factors, /* o : voicing factors */
286 : float pitch_buf[], /* o : floating pitch for each subframe */
287 : STEREO_CNG_DEC_HANDLE hStereoCng /* i : stereo CNG handle */
288 : )
289 : {
290 : int16_t i;
291 : int16_t start_bit_pos;
292 : int16_t tmp;
293 : int16_t bitsRead;
294 : int16_t param[DEC_NPRM_DIV * NB_DIV];
295 : float old_bwe_exc[( PIT16k_MAX + ( L_FRAME16k + 1 ) + L_SUBFR16k ) * 2]; /* excitation buffer */
296 : float *ptr_bwe_exc; /* pointer to BWE excitation signal in the current frame */
297 :
298 3750 : push_wmops( "dec_acelp_tcx_frame" );
299 :
300 3750 : start_bit_pos = st->next_bit_pos;
301 3750 : if ( st->rf_flag == 1 )
302 : {
303 0 : start_bit_pos -= 2;
304 : }
305 :
306 : /* -------------------------------------------------------------- */
307 : /* IDENTIFY FRAME TYPE */
308 : /* -------------------------------------------------------------- */
309 :
310 3750 : st->m_old_frame_type = st->m_frame_type;
311 :
312 3750 : if ( *concealWholeFrame == 0 )
313 : {
314 3750 : uint8_t m_frame_type = st->m_frame_type;
315 3750 : int16_t bwidth = st->bwidth;
316 3750 : int16_t cng_type = st->cng_type;
317 3750 : int16_t L_frame = st->L_frame;
318 3750 : int32_t total_brate = st->last_total_brate;
319 :
320 3750 : decode_frame_type( st, hStereoCng );
321 :
322 3750 : st->force_lpd_reset = 0;
323 :
324 3750 : if ( ( st->last_codec_mode ) == MODE2 && ( st->BER_detect || ( st->prev_bfi && st->m_frame_type == ZERO_FRAME && st->m_old_frame_type == ACTIVE_FRAME ) ) )
325 : {
326 : /* Copy back parameters from previous frame, because there is a high risk they are corrupt
327 : * Do concealment with configuration used in previous frame */
328 0 : st->m_frame_type = m_frame_type;
329 0 : st->bwidth = bwidth;
330 0 : st->cng_type = cng_type;
331 0 : st->L_frame = L_frame;
332 0 : if ( st->ini_frame != 0 )
333 : {
334 0 : st->total_brate = total_brate;
335 : }
336 :
337 0 : *concealWholeFrame = 1;
338 0 : st->m_decodeMode = DEC_CONCEALMENT_EXT;
339 0 : st->BER_detect = 0;
340 :
341 0 : if ( ( st->bwidth != st->last_bwidth ) || ( st->rf_flag != st->rf_flag_last ) || ( st->total_brate != st->last_total_brate ) )
342 : {
343 0 : st->force_lpd_reset = 1;
344 : }
345 :
346 0 : st->core_brate = st->last_core_brate;
347 0 : st->bfi = 1;
348 0 : if ( st->ini_frame == 0 && st->hTcxCfg != NULL )
349 : {
350 0 : st->hTcxCfg->tcx_coded_lines = getNumTcxCodedLines( SWB );
351 : }
352 : }
353 : else
354 : {
355 3750 : st->core_brate = st->total_brate;
356 3750 : bpf_noise_buf = st->p_bpf_noise_buf;
357 : }
358 : }
359 :
360 3750 : if ( *concealWholeFrame != 0 )
361 : {
362 : /* add two zero bytes for arithmetic coder flush */
363 0 : for ( i = 0; i < 8 * 2; i++ )
364 : {
365 0 : st->bit_stream[i] = 0;
366 : }
367 : }
368 :
369 3750 : if ( !( st->m_frame_type == SID_FRAME || st->m_frame_type == ZERO_FRAME ) )
370 3750 : {
371 :
372 : /* -------------------------------------------------------------- */
373 : /* DECODE CORE */
374 : /* -------------------------------------------------------------- */
375 : int32_t nbits_total;
376 3750 : nbits_total = st->total_brate / FRAMES_PER_SEC;
377 3750 : if ( *concealWholeFrame )
378 : {
379 :
380 0 : tmp = 0; /*to avoid empty counting */
381 : }
382 :
383 :
384 3750 : tmp = (int16_t) ( nbits_total - ( st->next_bit_pos - start_bit_pos ) );
385 :
386 3750 : bitsRead = 0;
387 :
388 : /* update old BWE excitation buffer */
389 3750 : if ( st->hBWE_TD != NULL )
390 : {
391 3750 : set_f( old_bwe_exc + PIT16k_MAX * 2, 0.f, ( ( L_FRAME16k + 1 ) + L_SUBFR16k ) * 2 );
392 3750 : ptr_bwe_exc = old_bwe_exc + PIT16k_MAX * 2;
393 3750 : mvr2r( st->hBWE_TD->old_bwe_exc, old_bwe_exc, PIT16k_MAX * 2 );
394 : }
395 : else
396 : {
397 0 : ptr_bwe_exc = NULL;
398 : }
399 :
400 : /* Decode the LPD data */
401 3750 : if ( st->m_decodeMode == DEC_NO_FRAM_LOSS )
402 : {
403 3750 : decoder_LPD( st, output, pcmbufFB, &tmp, bpf_noise_buf, 0, &bitsRead, param, pitch_buf, voice_factors, ptr_bwe_exc );
404 :
405 3750 : if ( !st->rate_switching_init && ( st->last_codec_mode ) == MODE2 && !( st->use_partial_copy && st->rf_frame_type >= RF_TCXFD && st->rf_frame_type <= RF_TCXTD2 ) && st->bfi )
406 : {
407 0 : *concealWholeFrame = 1;
408 0 : st->m_decodeMode = DEC_CONCEALMENT_EXT;
409 0 : st->BER_detect = 0;
410 : }
411 : }
412 0 : else if ( st->m_decodeMode == DEC_CONCEALMENT_EXT )
413 : {
414 0 : decoder_LPD( st, output, pcmbufFB, NULL, bpf_noise_buf, 1, /* bfi - st->bfi can be 0 here - MODE2 stays in PLC when DTX appears after a loss */
415 : &bitsRead, NULL, pitch_buf, voice_factors, ptr_bwe_exc );
416 : }
417 :
418 3750 : if ( st->hBWE_TD != NULL )
419 : {
420 3750 : if ( ( !st->bfi && ( st->prev_bfi || st->prev_use_partial_copy ) ) || ( ( st->last_vbr_hw_BWE_disable_dec == 1 ) && ( st->vbr_hw_BWE_disable_dec == 0 ) ) )
421 : {
422 0 : st->hBWE_TD->bwe_non_lin_prev_scale = 0.0f;
423 0 : set_f( st->hBWE_TD->old_bwe_exc_extended, 0.0f, NL_BUFF_OFFSET );
424 : }
425 :
426 3750 : if ( st->core == ACELP_CORE && st->igf && st->con_tcx == 0 )
427 : {
428 1812 : non_linearity( ptr_bwe_exc, bwe_exc_extended, st->hBWE_TD->old_bwe_exc_extended, L_FRAME32k, &st->hBWE_TD->bwe_non_lin_prev_scale, st->coder_type, voice_factors, st->L_frame );
429 :
430 : /* update the old BWE exe memory */
431 1812 : mvr2r( &old_bwe_exc[L_FRAME32k], st->hBWE_TD->old_bwe_exc, PIT16k_MAX * 2 );
432 : }
433 : else
434 : {
435 1938 : set_f( st->hBWE_TD->old_bwe_exc_extended, 0, NL_BUFF_OFFSET );
436 1938 : set_f( st->hBWE_TD->old_bwe_exc, 0, PIT16k_MAX * 2 ); /* reset old non_linear exc during igf frames */
437 1938 : st->hBWE_TD->bwe_non_lin_prev_scale = 0.0f;
438 : }
439 : }
440 :
441 : /* for ACELP mode, skip core data to read TD-BWE side info */
442 3750 : if ( ( !st->bfi ) && st->core == ACELP_CORE && st->total_brate > 0 )
443 : {
444 1812 : nbits_total = st->total_brate / FRAMES_PER_SEC;
445 : /* target bs-position "-2", because of earlier "start_bit_pos -= 2;", which are included in "st->rf_target_bits"*/
446 : /* from "-2" to "-3" as flag-bit not considered in rf_target_bits */
447 1812 : if ( st->rf_flag )
448 : {
449 0 : get_next_indice_tmp( st, (int16_t) ( start_bit_pos + nbits_total - st->rf_target_bits - 3 - get_tbe_bits( st->total_brate, st->bwidth, st->rf_flag ) - st->next_bit_pos ) );
450 : }
451 : else
452 : {
453 1812 : get_next_indice_tmp( st, (int16_t) ( start_bit_pos + nbits_total - st->rf_target_bits - get_tbe_bits( st->total_brate, st->bwidth, st->rf_flag ) - st->next_bit_pos ) );
454 : }
455 1812 : tbe_read_bitstream( st );
456 : }
457 :
458 3750 : if ( *concealWholeFrame )
459 : {
460 : /*"LPD dec - All BFI"*/
461 :
462 0 : tmp = 0; /*to avoid empty counting */
463 : }
464 :
465 : /* updates */
466 3750 : st->last_voice_factor = voice_factors[st->nb_subfr - 1];
467 3750 : st->last_coder_type = st->coder_type;
468 : }
469 : else
470 : {
471 0 : if ( st->m_frame_type == SID_FRAME )
472 : {
473 : /* Decode the FD-CNG bitstream */
474 0 : FdCng_decodeSID( st );
475 : }
476 :
477 : /* updates */
478 0 : st->last_voice_factor = 0;
479 0 : st->last_coder_type = INACTIVE;
480 : }
481 :
482 3750 : pop_wmops();
483 3750 : return;
484 : }
|