Line data Source code
1 : /*====================================================================================
2 : EVS Codec 3GPP TS26.452 Aug 12, 2021. Version 16.3.0
3 : ====================================================================================*/
4 :
5 : #include <assert.h>
6 : #include <stdint.h>
7 : #include "options.h"
8 : #include "basop_util.h"
9 : #include "prot_fx.h"
10 : #include "rom_com.h"
11 :
12 32 : void mode_switch_decoder_LPD_fx(
13 : Decoder_State *st, /* i/o: decoder state structure */
14 : Word16 bwidth, /* i : audio bandwidth Q0*/
15 : Word32 total_brate, /* i : total bitrate Q0*/
16 : Word16 frame_size_index /* i : index determining the frame size Q0*/
17 : )
18 : {
19 : Word16 fscale, switchWB;
20 : Word32 sr_core;
21 : Word8 bSwitchFromAmrwbIO;
22 : Word16 frame_size;
23 : TD_BWE_DEC_HANDLE hBWE_TD;
24 : TCX_DEC_HANDLE hTcxDec;
25 :
26 32 : hBWE_TD = st->hBWE_TD;
27 32 : hTcxDec = st->hTcxDec;
28 :
29 32 : switchWB = 0;
30 32 : move16();
31 32 : bSwitchFromAmrwbIO = 0;
32 32 : move16();
33 :
34 32 : if ( EQ_16( st->last_core, AMR_WB_CORE ) )
35 : {
36 0 : bSwitchFromAmrwbIO = 1;
37 0 : move16();
38 : }
39 :
40 32 : sr_core = getCoreSamplerateMode2( st->element_mode, total_brate, bwidth, st->flag_ACELP16k, st->rf_flag, st->is_ism_format );
41 :
42 32 : fscale = sr2fscale_fx( sr_core );
43 32 : move16();
44 :
45 : /* set number of coded lines */
46 32 : st->hTcxCfg->tcx_coded_lines = getNumTcxCodedLines( bwidth ); /* Q0 */
47 32 : move16();
48 32 : test();
49 32 : test();
50 32 : IF( ( ( GE_16( bwidth, WB ) ) && ( EQ_16( fscale, 640 ) ) && ( EQ_16( fscale, st->fscale ) ) ) )
51 : {
52 11 : test();
53 11 : test();
54 11 : test();
55 11 : IF( ( ( GT_32( total_brate, 32000 ) ) && ( ( st->tcxonly == 0 ) ) ) || ( ( LE_32( total_brate, 32000 ) ) && ( ( st->tcxonly != 0 ) ) ) )
56 : {
57 0 : switchWB = 1;
58 0 : move16();
59 : }
60 : }
61 :
62 32 : test();
63 32 : if ( GT_16( st->last_L_frame, L_FRAME16k ) && LE_32( total_brate, ACELP_32k ) )
64 : {
65 0 : switchWB = 1;
66 0 : move16();
67 : }
68 :
69 32 : st->igf = getIgfPresent_fx( st->element_mode, total_brate, bwidth, st->rf_flag );
70 32 : move16();
71 :
72 32 : IF( st->hIGFDec != NULL )
73 : {
74 32 : st->hIGFDec->infoIGFStopFreq = -1;
75 32 : move16();
76 : }
77 32 : move16();
78 32 : test();
79 32 : test();
80 32 : IF( st->igf && ( st->idchan == 0 || EQ_16( st->element_mode, IVAS_CPE_MDCT ) ) )
81 : {
82 : /* switch IGF configuration */
83 32 : IGFDecSetMode( st->hIGFDec, total_brate, bwidth, st->element_mode, -1, -1, st->rf_flag );
84 : }
85 :
86 32 : test();
87 32 : test();
88 32 : test();
89 32 : test();
90 32 : IF( NE_16( fscale, st->fscale ) || ( switchWB != 0 ) || ( bSwitchFromAmrwbIO != 0 ) || EQ_16( st->last_codec_mode, MODE1 ) || st->force_lpd_reset )
91 : {
92 32 : open_decoder_LPD_fx( st, total_brate, bwidth );
93 : }
94 : ELSE
95 : {
96 0 : assert( fscale > ( FSCALE_DENOM / 2 ) );
97 :
98 0 : st->fscale_old = st->fscale;
99 0 : move16();
100 0 : st->fscale = fscale;
101 0 : move16();
102 0 : st->L_frame = extract_l( Mult_32_16( st->sr_core, 0x0290 ) ); /* Q0 */
103 0 : IF( hTcxDec != NULL )
104 : {
105 0 : hTcxDec->L_frameTCX = extract_l( Mult_32_16( st->output_Fs, 0x0290 ) ); /* Q0 */
106 0 : move16();
107 : }
108 0 : IF( st->hTcxCfg != NULL )
109 : {
110 0 : st->hTcxCfg->ctx_hm = getCtxHm( st->element_mode, total_brate, st->rf_flag );
111 0 : move16();
112 0 : st->hTcxCfg->resq = getResq( total_brate );
113 0 : move16();
114 : }
115 0 : move16();
116 :
117 0 : hTcxDec->tcx_lpc_shaped_ari = getTcxLpcShapedAri( total_brate, st->rf_flag, st->element_mode );
118 :
119 0 : st->narrowBand = 0;
120 0 : move16();
121 0 : if ( EQ_16( bwidth, NB ) )
122 : {
123 0 : st->narrowBand = 1;
124 0 : move16();
125 : }
126 0 : st->TcxBandwidth = getTcxBandwidth( bwidth );
127 0 : move16();
128 0 : IF( st->hTcxCfg != NULL )
129 : {
130 0 : st->hTcxCfg->pCurrentTnsConfig = NULL;
131 0 : st->hTcxCfg->fIsTNSAllowed = getTnsAllowed( total_brate, st->igf, st->element_mode );
132 0 : move16();
133 :
134 0 : IF( ( st->hTcxCfg->fIsTNSAllowed != 0 ) && st->hIGFDec != NULL )
135 : {
136 0 : InitTnsConfigs( bwidth, st->hTcxCfg->tcx_coded_lines, st->hTcxCfg->tnsConfig, st->hIGFDec->infoIGFStopFreq, total_brate, st->element_mode, 0 /* 0 should be replaced with MCT_flag*/ );
137 : }
138 : }
139 : }
140 :
141 32 : frame_size = FrameSizeConfig[frame_size_index].frame_net_bits;
142 32 : move16();
143 :
144 32 : reconfig_decoder_LPD_fx( st, frame_size, bwidth, total_brate, st->last_L_frame );
145 :
146 32 : test();
147 32 : IF( hTcxDec->envWeighted != 0 && hTcxDec->enableTcxLpc == 0 )
148 : {
149 0 : Copy( st->lspold_uw, st->lsp_old_fx, M ); /* Q15 */
150 0 : Copy( st->lsfold_uw, st->lsf_old_fx, M ); /* Q2.56 */
151 0 : hTcxDec->envWeighted = 0;
152 0 : move16();
153 : }
154 :
155 : /* update PLC LSF memories */
156 32 : IF( st->tcxonly == 0 )
157 : {
158 32 : lsp2lsf_fx( st->lsp_old_fx, st->lsfoldbfi1_fx, M, extract_l( st->sr_core ) );
159 : }
160 : ELSE
161 : {
162 0 : E_LPC_lsp_lsf_conversion( st->lsp_old_fx, st->lsfoldbfi1_fx, M );
163 : }
164 32 : Copy( st->lsfoldbfi1_fx, st->lsfoldbfi0_fx, M ); /* Q2.56 */
165 32 : Copy( st->lsfoldbfi1_fx, st->lsf_adaptive_mean_fx, M ); /* Q2.56 */
166 :
167 32 : IF( st->igf != 0 && hBWE_TD != NULL )
168 : {
169 32 : test();
170 32 : test();
171 32 : test();
172 32 : test();
173 32 : test();
174 32 : IF( ( EQ_16( st->bwidth, WB ) && NE_16( st->last_extl, WB_TBE ) ) ||
175 : ( EQ_16( st->bwidth, SWB ) && NE_16( st->last_extl, SWB_TBE ) ) ||
176 : ( EQ_16( st->bwidth, FB ) && NE_16( st->last_extl, FB_TBE ) ) )
177 : {
178 19 : TBEreset_dec_fx( st );
179 : }
180 : ELSE
181 : {
182 13 : set16_fx( hBWE_TD->state_lpc_syn_fx, 0, LPC_SHB_ORDER );
183 13 : set16_fx( hBWE_TD->state_syn_shbexc_fx, 0, L_SHB_LAHEAD );
184 13 : set16_fx( hBWE_TD->mem_stp_swb_fx, 0, LPC_SHB_ORDER );
185 13 : set16_fx( hBWE_TD->mem_zero_swb_fx, 0, LPC_SHB_ORDER );
186 13 : hBWE_TD->gain_prec_swb_fx = 16384;
187 13 : move16(); /*Q14 = 1 */
188 : }
189 : }
190 :
191 32 : test();
192 32 : test();
193 32 : test();
194 32 : IF( ( EQ_16( bwidth, SWB ) ) && ( EQ_32( total_brate, ACELP_16k40 ) || EQ_32( total_brate, ACELP_24k40 ) ) && ( st->element_mode == EVS_MONO ) )
195 : {
196 12 : if ( st->tec_tfa == 0 )
197 : {
198 0 : set16_fx( st->hTECDec->loBuffer, 0, MAX_TEC_SMOOTHING_DEG );
199 : }
200 12 : st->tec_tfa = 1;
201 12 : move16();
202 : }
203 : ELSE
204 : {
205 20 : st->tec_tfa = 0;
206 20 : move16();
207 : }
208 :
209 32 : st->tec_flag = 0;
210 32 : move16();
211 32 : st->tfa_flag = 0;
212 32 : move16();
213 :
214 : /* needed in decoder to read the bitstream */
215 32 : st->enableGplc = 0;
216 32 : move16();
217 32 : test();
218 32 : test();
219 32 : IF( GE_16( bwidth, WB ) && EQ_32( total_brate, 24400 ) && ( st->element_mode == EVS_MONO ) )
220 : {
221 12 : st->enableGplc = 1;
222 12 : move16();
223 : }
224 32 : move16();
225 32 : st->dec_glr = 0;
226 32 : test();
227 32 : test();
228 32 : test();
229 32 : IF( ( EQ_32( total_brate, 9600 ) || EQ_32( total_brate, 16400 ) || EQ_32( total_brate, 24400 ) ) && st->element_mode == EVS_MONO )
230 : {
231 12 : st->dec_glr = 1;
232 12 : move16();
233 : }
234 32 : move16();
235 32 : st->dec_glr_idx = 0;
236 32 : }
237 :
238 21302 : void mode_switch_decoder_LPD_ivas_fx(
239 : Decoder_State *st, /* i/o: decoder state structure */
240 : const Word16 bwidth, /* i : audio bandwidth Q0*/
241 : const Word32 total_brate, /* i : total bitrate Q0*/
242 : const Word32 last_total_brate, /* i : last frame total bitrate Q0*/
243 : const Word16 frame_size_index, /* i : index determining the frame size Q0*/
244 : const Word16 MCT_flag, /* i : hMCT handle allocated (1) or not (0) Q0*/
245 : const Word16 last_element_mode, /* i : last element mode Q0*/
246 : Word16 *Q_syn_Overl_TDAC,
247 : Word16 *Q_fer_samples,
248 : Word16 *Q_syn_Overl,
249 : Word16 *Q_syn_Overl_TDACFB,
250 : Word16 *Q_syn_OverlFB,
251 : Word16 *Q_old_out,
252 : Word16 *Q_old_outLB,
253 : Word16 *Q_old_Aq_12_8 )
254 : {
255 : Word16 fscale, switchWB;
256 : Word32 sr_core;
257 : Word16 bSwitchFromAmrwbIO;
258 : Word16 frame_size;
259 : TCX_DEC_HANDLE hTcxDec;
260 : TD_BWE_DEC_HANDLE hBWE_TD;
261 :
262 21302 : hTcxDec = st->hTcxDec;
263 21302 : hBWE_TD = st->hBWE_TD;
264 21302 : switchWB = 0;
265 21302 : move16();
266 21302 : bSwitchFromAmrwbIO = 0;
267 21302 : move16();
268 21302 : IF( EQ_16( st->last_core, AMR_WB_CORE ) )
269 : {
270 0 : bSwitchFromAmrwbIO = 1;
271 0 : move16();
272 : }
273 21302 : sr_core = getCoreSamplerateMode2( st->element_mode, total_brate, bwidth, st->flag_ACELP16k, st->rf_flag, st->is_ism_format );
274 21302 : fscale = sr2fscale_fx( sr_core );
275 21302 : move16();
276 :
277 : /* set number of coded lines */
278 21302 : st->hTcxCfg->tcx_coded_lines = getNumTcxCodedLines( bwidth );
279 21302 : test();
280 21302 : test();
281 :
282 21302 : IF( ( GE_16( bwidth, WB ) ) && ( EQ_16( fscale, 640 ) ) && ( EQ_16( fscale, st->fscale ) ) )
283 : {
284 5598 : test();
285 5598 : test();
286 5598 : test();
287 5598 : IF( ( ( GT_32( total_brate, ACELP_32k ) ) && ( ( st->tcxonly == 0 ) ) ) || ( ( LE_32( total_brate, ACELP_32k ) ) && EQ_16( st->tcxonly, 1 ) ) )
288 : {
289 881 : switchWB = 1;
290 881 : move16();
291 : }
292 : }
293 :
294 21302 : test();
295 21302 : IF( GT_16( st->last_L_frame, L_FRAME16k ) && LE_32( total_brate, ACELP_32k ) )
296 : {
297 559 : switchWB = 1; /*force init when coming from MODE1*/
298 559 : move16();
299 : }
300 :
301 21302 : st->igf = getIgfPresent_fx( st->element_mode, total_brate, bwidth, st->rf_flag );
302 21302 : move16();
303 :
304 21302 : IF( st->hIGFDec != NULL )
305 : {
306 21302 : st->hIGFDec->infoIGFStopFreq = -1;
307 21302 : move16();
308 : }
309 :
310 21302 : move16();
311 21302 : test();
312 21302 : test();
313 21302 : IF( st->igf && ( ( st->idchan == 0 ) || EQ_16( st->element_mode, IVAS_CPE_MDCT ) ) )
314 : {
315 : /* switch IGF configuration */
316 17135 : IGFDecSetMode_ivas_fx( st->hIGFDec, total_brate, bwidth, st->element_mode, -1, -1, st->rf_flag );
317 : }
318 21302 : test();
319 21302 : test();
320 21302 : test();
321 21302 : test();
322 :
323 21302 : assert( (Word16) ( st->sr_core / FRAMES_PER_SEC ) == extract_l( Mult_32_16( st->sr_core, 0x0290 ) ) );
324 21302 : IF( NE_16( fscale, st->fscale ) || switchWB || bSwitchFromAmrwbIO || EQ_16( st->last_codec_mode, MODE1 ) || st->force_lpd_reset )
325 : {
326 21302 : open_decoder_LPD_ivas_fx( st, total_brate, last_total_brate, bwidth, MCT_flag, last_element_mode, 0, Q_syn_Overl_TDAC, Q_fer_samples, Q_syn_Overl, Q_syn_Overl_TDACFB, Q_syn_OverlFB, Q_old_out, Q_old_outLB, Q_old_Aq_12_8 );
327 : }
328 : ELSE
329 : {
330 0 : assert( fscale > ( FSCALE_DENOM / 2 ) );
331 0 : st->fscale_old = st->fscale;
332 0 : move16();
333 0 : st->fscale = fscale;
334 0 : move16();
335 0 : st->L_frame = extract_l( Mult_32_16( st->sr_core, 0x0290 ) );
336 0 : move16();
337 0 : IF( st->hTcxDec != NULL )
338 : {
339 0 : st->hTcxDec->L_frameTCX = extract_l( Mult_32_16( st->output_Fs, 0x0290 ) ); /* Q0 */
340 0 : move16();
341 0 : st->output_frame_fx = st->hTcxDec->L_frameTCX; /* Q0 */
342 0 : move16();
343 : }
344 :
345 0 : IF( st->hTcxCfg != NULL )
346 : {
347 0 : st->hTcxCfg->ctx_hm = getCtxHm( st->element_mode, total_brate, st->rf_flag ); /* Q0 */
348 0 : move16();
349 0 : st->hTcxCfg->resq = getResq( total_brate ); /* Q0 */
350 0 : move16();
351 : }
352 0 : move16();
353 :
354 0 : hTcxDec->tcx_lpc_shaped_ari = getTcxLpcShapedAri( total_brate, st->rf_flag, st->element_mode ); /* Q0 */
355 :
356 0 : st->narrowBand = 0;
357 0 : move16();
358 0 : IF( EQ_16( bwidth, NB ) )
359 : {
360 0 : st->narrowBand = 1;
361 0 : move16();
362 : }
363 : ELSE
364 : {
365 0 : st->narrowBand = 0;
366 0 : move16();
367 : }
368 0 : st->TcxBandwidth = getTcxBandwidth( bwidth );
369 0 : move16();
370 :
371 0 : IF( st->hTcxCfg != NULL )
372 : {
373 0 : st->hTcxCfg->pCurrentTnsConfig = NULL;
374 0 : st->hTcxCfg->fIsTNSAllowed = getTnsAllowed( total_brate, st->igf, st->element_mode ); /* Q0 */
375 0 : move16();
376 : }
377 :
378 0 : IF( st->hTcxCfg->fIsTNSAllowed && st->hIGFDec != NULL && st->hTcxCfg != NULL )
379 : {
380 0 : InitTnsConfigs( bwidth, st->hTcxCfg->tcx_coded_lines, st->hTcxCfg->tnsConfig, st->hIGFDec->infoIGFStopFreq, total_brate, st->element_mode, MCT_flag );
381 :
382 0 : SetAllowTnsOnWhite( st->hTcxCfg->tnsConfig, (Word8) EQ_16( st->element_mode, IVAS_CPE_MDCT ) );
383 : }
384 : }
385 :
386 21302 : frame_size = FrameSizeConfig[frame_size_index].frame_net_bits;
387 21302 : move16();
388 21302 : reconfig_decoder_LPD_ivas_fx( st, frame_size, bwidth, total_brate, st->last_L_frame );
389 :
390 21302 : test();
391 21302 : IF( hTcxDec->envWeighted && !hTcxDec->enableTcxLpc )
392 : {
393 0 : Copy( st->lspold_uw, st->lsp_old_fx, M ); /* Q15 */
394 0 : Copy( st->lsfold_uw, st->lsf_old_fx, M ); /* Q2.56 */
395 0 : hTcxDec->envWeighted = 0;
396 0 : move16();
397 : }
398 : /* update PLC LSF memories */
399 21302 : IF( EQ_16( st->tcxonly, 0 ) )
400 : {
401 8761 : lsp2lsf_fx( st->lsp_old_fx, st->lsfoldbfi1_fx, M, extract_l( st->sr_core ) );
402 : }
403 : ELSE
404 : {
405 12541 : E_LPC_lsp_lsf_conversion( st->lsp_old_fx, st->lsfoldbfi1_fx, M );
406 : }
407 21302 : Copy( st->lsfoldbfi1_fx, st->lsfoldbfi0_fx, M ); /* Qx2.56 */
408 21302 : Copy( st->lsfoldbfi1_fx, st->lsf_adaptive_mean_fx, M ); /* Qx2.56 */
409 :
410 21302 : IF( st->igf && hBWE_TD != NULL )
411 : {
412 10374 : test();
413 10374 : test();
414 10374 : test();
415 10374 : test();
416 10374 : test();
417 : /* reset TBE */
418 10374 : IF( ( EQ_16( st->bwidth, WB ) && NE_16( st->last_extl, WB_TBE ) ) ||
419 : ( EQ_16( st->bwidth, SWB ) && NE_16( st->last_extl, SWB_TBE ) ) ||
420 : ( EQ_16( st->bwidth, FB ) && NE_16( st->last_extl, FB_TBE ) ) )
421 : {
422 5159 : TBEreset_dec_fx( st );
423 : }
424 : ELSE
425 : {
426 5215 : set16_fx( hBWE_TD->state_lpc_syn_fx, 0, LPC_SHB_ORDER );
427 5215 : set16_fx( hBWE_TD->state_syn_shbexc_fx, 0, L_SHB_LAHEAD );
428 5215 : set16_fx( hBWE_TD->mem_stp_swb_fx, 0, LPC_SHB_ORDER );
429 5215 : set16_fx( hBWE_TD->mem_zero_swb_fx, 0, LPC_SHB_ORDER );
430 5215 : hBWE_TD->gain_prec_swb_fx = 16384;
431 5215 : move16(); /*Q14 = 1 */
432 : }
433 : }
434 21302 : test();
435 21302 : test();
436 21302 : test();
437 21302 : IF( EQ_16( bwidth, SWB ) && ( EQ_32( total_brate, ACELP_16k40 ) || EQ_32( total_brate, ACELP_24k40 ) ) && ( st->element_mode == EVS_MONO ) )
438 : {
439 0 : IF( EQ_16( st->tec_tfa, 0 ) && st->hTECDec != NULL )
440 : {
441 0 : set16_fx( st->hTECDec->loBuffer, 0, MAX_TEC_SMOOTHING_DEG );
442 : }
443 0 : st->tec_tfa = 1;
444 0 : move16();
445 : }
446 : ELSE
447 : {
448 21302 : st->tec_tfa = 0;
449 21302 : move16();
450 : }
451 :
452 21302 : st->tec_flag = 0;
453 21302 : move16();
454 21302 : st->tfa_flag = 0;
455 21302 : move16();
456 :
457 : /* needed in decoder to read the bitstream */
458 21302 : st->enableGplc = 0;
459 21302 : move16();
460 21302 : test();
461 21302 : test();
462 21302 : IF( GE_16( bwidth, WB ) && EQ_32( total_brate, ACELP_24k40 ) && st->element_mode == EVS_MONO )
463 : {
464 0 : st->enableGplc = 1;
465 0 : move16();
466 : }
467 21302 : move16();
468 21302 : st->dec_glr = 0;
469 21302 : test();
470 21302 : test();
471 21302 : test();
472 21302 : IF( ( EQ_32( total_brate, ACELP_9k60 ) || EQ_32( total_brate, ACELP_16k40 ) || EQ_32( total_brate, ACELP_24k40 ) ) && EQ_16( st->element_mode, EVS_MONO ) )
473 : {
474 0 : st->dec_glr = 1;
475 0 : move16();
476 : }
477 21302 : move16();
478 21302 : st->dec_glr_idx = 0;
479 21302 : }
|