Line data Source code
1 : /*====================================================================================
2 : EVS Codec 3GPP TS26.452 Aug 12, 2021. Version 16.3.0
3 : ====================================================================================*/
4 :
5 :
6 : #include <stdint.h>
7 : #include <assert.h>
8 : #include "options.h"
9 : #include "rom_com.h"
10 : #include "stat_com.h"
11 : #include "prot_fx.h"
12 : #include "basop_util.h"
13 1248 : static void decode_frame_type_fx( Decoder_State *st )
14 : {
15 : Word32 L_tmp;
16 : Word16 num_bits;
17 : UWord16 lsb;
18 : Word16 frame_size_index;
19 : Word16 n;
20 : Word32 total_brate;
21 : Word16 frame_len_indicator;
22 :
23 1248 : frame_size_index = 0;
24 1248 : move16();
25 1248 : total_brate = st->total_brate;
26 1248 : move32();
27 :
28 1248 : Mpy_32_16_ss( st->total_brate, 5243, &L_tmp, &lsb ); /* 5243 is 1/50 in Q18. (0+18-15=3) */
29 1248 : num_bits = extract_l( L_shr( L_tmp, 3 ) ); /* Q0 */
30 :
31 :
32 : /* Get Frame Type (NULL,SID,ACTIVE) and Frame Mode (2kbps, 4kbps,...) */
33 :
34 1248 : IF( EQ_16( st->mdct_sw, MODE1 ) )
35 : {
36 264 : st->m_frame_type = ACTIVE_FRAME;
37 264 : move16();
38 :
39 1584 : FOR( n = 0; n < FRAME_SIZE_NB; n++ )
40 : {
41 1584 : IF( EQ_16( FrameSizeConfig[n].frame_bits, num_bits ) )
42 : {
43 264 : frame_size_index = n;
44 264 : move16();
45 264 : BREAK;
46 : }
47 : }
48 : }
49 : ELSE
50 : {
51 :
52 : /* ZERO Frame */
53 984 : IF( ( st->total_brate == FRAME_NO_DATA ) )
54 : {
55 0 : st->bwidth = st->last_bwidth;
56 0 : move16();
57 0 : st->m_frame_type = ZERO_FRAME;
58 0 : move16();
59 : }
60 :
61 : /* SID frame */
62 984 : ELSE IF( EQ_32( st->total_brate, SID_2k40 ) )
63 : {
64 0 : st->cng_type = get_next_indice( st, 1 );
65 0 : move16();
66 0 : if ( NE_16( st->cng_type, FD_CNG ) )
67 : {
68 0 : st->BER_detect = 1;
69 0 : move16();
70 : }
71 0 : st->m_frame_type = SID_FRAME;
72 0 : move16();
73 0 : frame_size_index = 1;
74 0 : move16();
75 0 : st->bwidth = get_next_indice( st, 2 );
76 0 : move16();
77 :
78 0 : frame_len_indicator = get_next_indice( st, 1 );
79 0 : IF( EQ_16( st->bwidth, NB ) )
80 : {
81 0 : if ( frame_len_indicator )
82 : {
83 0 : st->BER_detect = 1;
84 0 : move16();
85 : }
86 0 : frame_len_indicator = 0;
87 0 : move16();
88 : }
89 0 : IF( frame_len_indicator == 0 )
90 : {
91 0 : st->L_frame = L_FRAME;
92 0 : move16();
93 0 : st->total_brate = ACELP_9k60;
94 0 : move32();
95 : }
96 : ELSE
97 : {
98 0 : st->L_frame = L_FRAME16k;
99 0 : move16();
100 0 : test();
101 0 : IF( EQ_32( st->last_total_brate, ACELP_16k40 ) || EQ_32( st->last_total_brate, ACELP_24k40 ) )
102 : {
103 0 : st->total_brate = st->last_total_brate;
104 0 : move32();
105 : }
106 : ELSE
107 : {
108 0 : st->total_brate = ACELP_16k40;
109 0 : move32();
110 : }
111 : }
112 :
113 0 : Mpy_32_16_ss( st->total_brate, 5243, &L_tmp, &lsb ); /* 5243 is 1/50 in Q18. (0+18-15=3) */
114 0 : num_bits = extract_l( L_shr( L_tmp, 3 ) ); /* Q0 */
115 0 : FOR( n = 0; n < FRAME_SIZE_NB; n++ )
116 : {
117 0 : IF( EQ_16( FrameSizeConfig[n].frame_bits, num_bits ) )
118 : {
119 0 : frame_size_index = n;
120 0 : move16();
121 0 : BREAK;
122 : }
123 : }
124 : }
125 :
126 : /* EVS MODES */
127 : ELSE
128 : {
129 : /* Get Frame mode */
130 984 : st->m_frame_type = ACTIVE_FRAME;
131 984 : move16();
132 :
133 7872 : FOR( n = 0; n < FRAME_SIZE_NB; n++ )
134 : {
135 :
136 7872 : IF( EQ_16( FrameSizeConfig[n].frame_bits, num_bits ) )
137 : {
138 984 : frame_size_index = n;
139 984 : move16();
140 984 : BREAK;
141 : }
142 : }
143 :
144 :
145 984 : IF( st->rf_flag == 0 )
146 : {
147 :
148 : /* Get bandwidth mode */
149 984 : st->bwidth = get_next_indice( st, FrameSizeConfig[frame_size_index].bandwidth_bits );
150 984 : move16();
151 :
152 984 : st->bwidth = add( st->bwidth, FrameSizeConfig[frame_size_index].bandwidth_min );
153 984 : move16();
154 : }
155 : ELSE
156 : {
157 0 : st->bwidth = add( st->bwidth, FrameSizeConfig[frame_size_index].bandwidth_min );
158 0 : move16();
159 : }
160 :
161 984 : IF( GT_16( st->bwidth, FB ) )
162 : {
163 0 : st->bwidth = FB;
164 0 : move16();
165 0 : st->BER_detect = 1;
166 0 : move16();
167 : }
168 :
169 984 : test();
170 984 : IF( GT_16( st->bwidth, SWB ) && LT_32( st->total_brate, ACELP_16k40 ) )
171 : {
172 0 : st->bwidth = SWB;
173 0 : move16();
174 0 : st->BER_detect = 1;
175 0 : move16();
176 : }
177 :
178 : /* Get reserved bits */
179 984 : test();
180 984 : IF( FrameSizeConfig[frame_size_index].reserved_bits && st->rf_flag == 0 )
181 : {
182 : Word16 dummyBit;
183 984 : dummyBit = (Word8) get_next_indice( st, 1 );
184 984 : move16();
185 984 : if ( dummyBit != 0 )
186 : {
187 0 : st->BER_detect = 1;
188 0 : move16();
189 : }
190 984 : assert( FrameSizeConfig[frame_size_index].reserved_bits == 1 );
191 : }
192 : }
193 : }
194 :
195 1248 : st->rate_switching_init = 0;
196 1248 : move16();
197 :
198 1248 : test();
199 1248 : IF( NE_16( st->last_codec_mode, MODE2 ) || !st->BER_detect )
200 : {
201 : /* Mode or Rate Change */
202 :
203 1248 : test();
204 1248 : test();
205 1248 : IF( ( EQ_16( st->m_frame_type, ACTIVE_FRAME ) || EQ_16( st->m_frame_type, SID_FRAME ) ) && ( s_or( s_or( s_or( s_or( (Word16) NE_32( st->total_brate, st->last_total_brate ), (Word16) NE_16( st->bwidth, st->last_bwidth ) ), (Word16) EQ_16( st->last_codec_mode, MODE1 ) ), (Word16) NE_16( st->rf_flag, st->rf_flag_last ) ), st->force_lpd_reset ) ) )
206 : {
207 32 : st->rate_switching_init = 1;
208 32 : move16();
209 :
210 : /* Reconf Core */
211 32 : mode_switch_decoder_LPD_fx( st, st->bwidth, st->total_brate, frame_size_index );
212 : /* Reconf CLDFB */
213 32 : IF( NE_16( i_mult( st->cldfbAna->no_channels, st->cldfbAna->no_col ), st->L_frame ) )
214 : {
215 1 : Word16 newCldfbBands = CLDFB_getNumChannels( L_mult0( st->L_frame, 50 ) );
216 :
217 1 : resampleCldfb( st->cldfbAna, newCldfbBands, st->L_frame, 0 );
218 1 : IF( LE_16( st->L_frame, L_FRAME16k ) )
219 : {
220 1 : resampleCldfb( st->cldfbBPF, newCldfbBands, st->L_frame, 0 );
221 : }
222 : }
223 32 : IF( EQ_16( st->bwidth, NB ) )
224 : {
225 0 : st->cldfbSyn->bandsToZero = sub( st->cldfbSyn->no_channels, 10 );
226 0 : move16();
227 : }
228 : ELSE
229 : {
230 32 : st->cldfbSyn->bandsToZero = 0;
231 32 : move16();
232 : }
233 :
234 : /* Reconf FD-CNG */
235 32 : L_tmp = st->total_brate;
236 32 : move32();
237 32 : test();
238 32 : IF( EQ_16( st->rf_flag, 1 ) && EQ_32( st->total_brate, ACELP_13k20 ) )
239 : {
240 0 : L_tmp = ACELP_9k60;
241 0 : move32();
242 : }
243 32 : configureFdCngDec_fx( st->hFdCngDec, st->bwidth, L_tmp, st->L_frame, st->last_L_frame, st->element_mode );
244 32 : test();
245 32 : test();
246 32 : IF( ( NE_16( st->last_L_frame, st->L_frame ) ) && ( LE_16( st->L_frame, L_FRAME16k ) ) && ( LE_16( st->last_L_frame, L_FRAME16k ) ) )
247 : {
248 0 : lerp( st->hFdCngDec->hFdCngCom->olapBufferSynth2, st->hFdCngDec->hFdCngCom->olapBufferSynth2, st->L_frame * 2, st->last_L_frame * 2 );
249 0 : test();
250 0 : IF( EQ_16( st->m_frame_type, SID_FRAME ) && NE_16( st->hFdCngDec->hFdCngCom->frame_type_previous, ACTIVE_FRAME ) )
251 : {
252 0 : lerp( st->hFdCngDec->hFdCngCom->olapBufferSynth, st->hFdCngDec->hFdCngCom->olapBufferSynth, st->L_frame * 2, st->last_L_frame * 2 );
253 :
254 0 : IF( EQ_16( st->L_frame, L_FRAME ) )
255 : {
256 0 : FOR( n = 0; n < shl( st->L_frame, 1 ); n++ )
257 : {
258 0 : st->hFdCngDec->hFdCngCom->olapBufferSynth[n] = mult_r( st->hFdCngDec->hFdCngCom->olapBufferSynth[n], 20480 /*1.25f in Q14*/ );
259 0 : move16();
260 : }
261 : }
262 : ELSE
263 : {
264 0 : FOR( n = 0; n < shl( st->L_frame, 1 ); n++ )
265 : {
266 0 : st->hFdCngDec->hFdCngCom->olapBufferSynth[n] = mult_r( shl( st->hFdCngDec->hFdCngCom->olapBufferSynth[n], 1 ), 26214 /* 1/1.25 in Q15*/ );
267 0 : move16();
268 : }
269 : }
270 : }
271 : }
272 32 : IF( NE_16( st->bwidth, st->last_bwidth ) )
273 : {
274 1 : st->hFdCngDec->hFdCngCom->msFrCnt_init_counter = 0;
275 1 : move16();
276 1 : st->hFdCngDec->hFdCngCom->init_old = 32767;
277 1 : move16();
278 : }
279 :
280 : /* Reconf BPF */
281 32 : st->p_bpf_noise_buf = NULL;
282 32 : IF( st->tcxonly == 0 )
283 : {
284 32 : st->p_bpf_noise_buf = st->bpf_noise_buf;
285 : }
286 : }
287 : }
288 :
289 1248 : st->total_brate = total_brate;
290 1248 : move32();
291 1248 : }
292 :
293 :
294 1248 : Word16 dec_acelp_tcx_frame_fx(
295 : Decoder_State *st, /* i/o: decoder state structure */
296 : Word16 *concealWholeFrame, /* i/o: concealment flag */
297 : Word16 *pcmBuf, /* o : synthesis Q0 */
298 : Word16 *bpf_noise_buf, /* i/o: BPF noise buffer Qx */
299 : Word16 *pcmbufFB, /* o : synthesis @output_FS Qx */
300 : Word32 bwe_exc_extended[], /* i/o: bandwidth extended excitation Q0 */
301 : Word16 *voice_factors, /* o : voicing factors Q15 */
302 : Word16 pitch_buf[] /* o : floating pitch for each subframe Q6 */
303 : )
304 : {
305 : Word16 num_bits;
306 : Word32 L_tmp;
307 : UWord16 lsb;
308 : Word16 bitsRead;
309 : Word16 tmp;
310 : Word16 i;
311 : Word16 start_bit_pos;
312 : Word16 param[DEC_NPRM_DIV * NB_DIV + NPRM_LPC_NEW];
313 : Word16 old_bwe_exc[( PIT16k_MAX + ( L_FRAME16k + 1 ) + L_SUBFR16k ) * 2]; /* excitation buffer */
314 : Word16 *ptr_bwe_exc; /* pointer to BWE excitation signal in the current frame */
315 : TD_BWE_DEC_HANDLE hBWE_TD;
316 1248 : hBWE_TD = st->hBWE_TD;
317 :
318 1248 : Mpy_32_16_ss( st->total_brate, 5243, &L_tmp, &lsb ); /* 5243 is 1/50 in Q18. (0+18-15=3) */
319 1248 : num_bits = extract_l( L_shr( L_tmp, 3 ) ); /* Q0 */
320 :
321 : /* -------------------------------------------------------------- */
322 : /* Set the bit-stream */
323 : /* -------------------------------------------------------------- */
324 :
325 :
326 1248 : start_bit_pos = st->next_bit_pos;
327 1248 : move16();
328 :
329 1248 : IF( EQ_16( st->rf_flag, 1 ) )
330 : {
331 0 : start_bit_pos = sub( start_bit_pos, 2 );
332 : }
333 : /* -------------------------------------------------------------- */
334 : /* IDENTIFY FRAME TYPE */
335 : /* -------------------------------------------------------------- */
336 :
337 1248 : st->m_old_frame_type = st->m_frame_type;
338 1248 : move16();
339 :
340 1248 : IF( *concealWholeFrame == 0 )
341 : {
342 :
343 1248 : Word16 m_frame_type = st->m_frame_type;
344 1248 : move16();
345 1248 : Word16 bwidth = st->bwidth;
346 1248 : move16();
347 1248 : Word16 cng_type = st->cng_type;
348 1248 : move16();
349 1248 : Word16 L_frame = st->L_frame;
350 1248 : move16();
351 1248 : Word32 total_brate = st->last_total_brate;
352 1248 : move32();
353 :
354 1248 : decode_frame_type_fx( st );
355 :
356 1248 : st->force_lpd_reset = 0;
357 1248 : move16();
358 :
359 1248 : test();
360 1248 : test();
361 1248 : test();
362 1248 : test();
363 1248 : IF( ( EQ_16( st->last_codec_mode, MODE2 ) ) && ( st->BER_detect || ( st->prev_bfi && ( EQ_16( st->m_frame_type, ZERO_FRAME ) ) && ( EQ_16( st->m_old_frame_type, ACTIVE_FRAME ) ) ) ) )
364 : {
365 : /* Copy back parameters from previous frame, because there is a high risk they are corrupt
366 : * DO concealment with configuration used in previous frame */
367 0 : st->m_frame_type = (uint8_t) m_frame_type;
368 0 : move16();
369 0 : st->bwidth = bwidth;
370 0 : move16();
371 0 : st->cng_type = cng_type;
372 0 : move16();
373 0 : st->L_frame = L_frame;
374 0 : move16();
375 0 : if ( st->ini_frame != 0 )
376 : {
377 0 : st->total_brate = total_brate;
378 0 : move32();
379 : }
380 :
381 0 : *concealWholeFrame = 1;
382 0 : move16();
383 0 : st->m_decodeMode = DEC_CONCEALMENT_EXT;
384 0 : move16();
385 0 : st->BER_detect = 0;
386 0 : move16();
387 :
388 0 : test();
389 0 : test();
390 0 : if ( ( NE_16( st->bwidth, st->last_bwidth ) ) || ( NE_16( st->rf_flag, st->rf_flag_last ) ) || ( NE_32( st->total_brate, st->last_total_brate ) ) )
391 : {
392 0 : st->force_lpd_reset = 1;
393 0 : move16();
394 : }
395 :
396 0 : st->core_brate = st->last_core_brate;
397 0 : move32();
398 0 : st->bfi = 1;
399 0 : move16();
400 0 : test();
401 0 : IF( st->ini_frame == 0 && st->hTcxCfg != NULL )
402 : {
403 0 : st->hTcxCfg->tcx_coded_lines = getNumTcxCodedLines( SWB );
404 0 : move16();
405 : }
406 : }
407 : ELSE
408 : {
409 1248 : st->core_brate = st->total_brate;
410 1248 : move32();
411 1248 : bpf_noise_buf = st->p_bpf_noise_buf;
412 1248 : move16();
413 : }
414 : }
415 :
416 1248 : IF( *concealWholeFrame != 0 )
417 : {
418 : /* add two zero bytes for arithmetic coder flush */
419 0 : FOR( i = 0; i < 8 * 2; i++ )
420 : {
421 0 : st->bit_stream[i] = 0;
422 0 : move16();
423 : }
424 : }
425 :
426 1248 : IF( s_and( (Word16) NE_16( st->m_frame_type, SID_FRAME ), (Word16) NE_16( st->m_frame_type, ZERO_FRAME ) ) ) /* test */
427 : {
428 :
429 : /* -------------------------------------------------------------- */
430 : /* DECODE CORE */
431 : /* -------------------------------------------------------------- */
432 :
433 1248 : tmp = sub( num_bits, sub( st->next_bit_pos, start_bit_pos ) );
434 1248 : bitsRead = 0;
435 1248 : move16();
436 :
437 1248 : set16_fx( old_bwe_exc + PIT16k_MAX * 2, 0, ( ( L_FRAME16k + 1 ) + L_SUBFR16k ) * 2 );
438 1248 : ptr_bwe_exc = old_bwe_exc + PIT16k_MAX * 2;
439 1248 : Copy( hBWE_TD->old_bwe_exc_fx, old_bwe_exc, PIT16k_MAX * 2 );
440 :
441 1248 : IF( EQ_16( st->m_decodeMode, DEC_NO_FRAM_LOSS ) )
442 : {
443 1248 : decoder_LPD_fx( pcmBuf, pcmbufFB, &tmp, st, bpf_noise_buf, 0, &bitsRead, param, pitch_buf, voice_factors, ptr_bwe_exc );
444 :
445 1248 : test();
446 1248 : test();
447 1248 : test();
448 1248 : test();
449 1248 : test();
450 1248 : IF( st->rate_switching_init == 0 && EQ_16( ( st->last_codec_mode ), MODE2 ) && !( st->use_partial_copy && GE_16( st->rf_frame_type, RF_TCXFD ) && LE_16( st->rf_frame_type, RF_TCXTD2 ) ) && EQ_16( st->bfi, 1 ) )
451 : {
452 0 : *concealWholeFrame = 1;
453 0 : move16();
454 0 : st->m_decodeMode = DEC_CONCEALMENT_EXT;
455 0 : move16();
456 0 : st->BER_detect = 0;
457 0 : move16();
458 : }
459 : }
460 0 : ELSE IF( EQ_16( st->m_decodeMode, DEC_CONCEALMENT_EXT ) )
461 : {
462 : /* Decode the LPD data */
463 0 : decoder_LPD_fx( pcmBuf, pcmbufFB, NULL, st, bpf_noise_buf, 1, &bitsRead, NULL, pitch_buf, voice_factors, ptr_bwe_exc );
464 : }
465 :
466 1248 : IF( st->hBWE_TD != NULL )
467 : {
468 1248 : test();
469 1248 : test();
470 1248 : test();
471 1248 : test();
472 1248 : IF( ( st->bfi == 0 && ( EQ_16( st->prev_bfi, 1 ) || EQ_16( st->prev_use_partial_copy, 1 ) ) ) || ( ( EQ_16( st->last_vbr_hw_BWE_disable_dec, 1 ) ) && ( st->vbr_hw_BWE_disable_dec == 0 ) ) )
473 : {
474 0 : hBWE_TD->bwe_non_lin_prev_scale_fx = 0;
475 0 : move32();
476 0 : set16_fx( hBWE_TD->old_bwe_exc_extended_fx, 0, NL_BUFF_OFFSET );
477 : }
478 :
479 1248 : test();
480 1248 : test();
481 1248 : IF( st->core == ACELP_CORE && st->igf != 0 && st->con_tcx == 0 )
482 : {
483 610 : non_linearity_fx( ptr_bwe_exc, bwe_exc_extended, L_FRAME32k, &hBWE_TD->bwe_non_lin_prev_scale_fx, st->Q_exc, st->coder_type, voice_factors, st->L_frame );
484 : /* update the old BWE exe memory */
485 610 : Copy( &old_bwe_exc[L_FRAME32k], hBWE_TD->old_bwe_exc_fx, PIT16k_MAX * 2 );
486 : }
487 : ELSE
488 : {
489 638 : set16_fx( hBWE_TD->old_bwe_exc_extended_fx, 0, NL_BUFF_OFFSET );
490 638 : set16_fx( hBWE_TD->old_bwe_exc_fx, 0, PIT16k_MAX * 2 );
491 638 : hBWE_TD->bwe_non_lin_prev_scale_fx = 0;
492 638 : move32();
493 : }
494 : }
495 :
496 : /* for ACELP mode, skip core data to read TD-BWE side info */
497 1248 : test();
498 1248 : test();
499 1248 : IF( ( !st->bfi ) && EQ_16( st->core, ACELP_CORE ) && st->total_brate > 0 )
500 : {
501 : /* target bs-position "-3": -2 as earlier "start_bit_pos -= 2;" are included in st->rf_target_bits
502 : -1 as flag-bit not considered in rf_target_bits */
503 610 : IF( EQ_16( st->rf_flag, 1 ) )
504 : {
505 0 : get_next_indice_tmp_fx( st, sub( sub( sub( sub( add( start_bit_pos, num_bits ), st->rf_target_bits ), 3 ), get_tbe_bits_fx( st->total_brate, st->bwidth, st->rf_flag ) ), st->next_bit_pos ) );
506 : }
507 : ELSE
508 : {
509 610 : get_next_indice_tmp_fx( st, sub( sub( sub( add( start_bit_pos, num_bits ), st->rf_target_bits ), get_tbe_bits_fx( st->total_brate, st->bwidth, st->rf_flag ) ), st->next_bit_pos ) );
510 : }
511 610 : tbe_read_bitstream_fx( st );
512 : }
513 :
514 : /* updates */
515 1248 : st->last_voice_factor_fx = voice_factors[st->nb_subfr - 1];
516 1248 : move16();
517 1248 : st->last_coder_type = st->coder_type;
518 1248 : move16();
519 : }
520 : ELSE
521 : {
522 :
523 0 : IF( EQ_16( st->m_frame_type, SID_FRAME ) )
524 : {
525 0 : FdCng_decodeSID_fx( st->hFdCngDec->hFdCngCom, st );
526 : }
527 :
528 : /* updates */
529 0 : st->last_voice_factor_fx = 0;
530 0 : move16();
531 0 : st->last_coder_type = INACTIVE;
532 0 : move16();
533 : }
534 :
535 1248 : return 0;
536 : }
|