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 38 : 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 38 : hBWE_TD = st->hBWE_TD;
27 38 : hTcxDec = st->hTcxDec;
28 :
29 38 : switchWB = 0;
30 38 : move16();
31 38 : bSwitchFromAmrwbIO = 0;
32 38 : move16();
33 :
34 38 : if ( EQ_16( st->last_core, AMR_WB_CORE ) )
35 : {
36 0 : bSwitchFromAmrwbIO = 1;
37 0 : move16();
38 : }
39 :
40 38 : sr_core = getCoreSamplerateMode2( st->element_mode, total_brate, bwidth, st->flag_ACELP16k, st->rf_flag, st->is_ism_format );
41 :
42 38 : fscale = sr2fscale_fx( sr_core );
43 38 : move16();
44 :
45 : /* set number of coded lines */
46 38 : st->hTcxCfg->tcx_coded_lines = getNumTcxCodedLines( bwidth ); /* Q0 */
47 38 : move16();
48 38 : test();
49 38 : test();
50 38 : IF( ( ( GE_16( bwidth, WB ) ) && ( EQ_16( fscale, 640 ) ) && ( EQ_16( fscale, st->fscale ) ) ) )
51 : {
52 13 : test();
53 13 : test();
54 13 : test();
55 13 : 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 38 : test();
63 38 : 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 38 : st->igf = getIgfPresent_fx( st->element_mode, total_brate, bwidth, st->rf_flag );
70 38 : move16();
71 :
72 38 : IF( st->hIGFDec != NULL )
73 : {
74 38 : st->hIGFDec->infoIGFStopFreq = -1;
75 38 : move16();
76 : }
77 38 : move16();
78 38 : test();
79 38 : test();
80 38 : IF( st->igf && ( st->idchan == 0 || EQ_16( st->element_mode, IVAS_CPE_MDCT ) ) )
81 : {
82 : /* switch IGF configuration */
83 38 : IGFDecSetMode( st->hIGFDec, total_brate, bwidth, st->element_mode, -1, -1, st->rf_flag );
84 : }
85 :
86 38 : test();
87 38 : test();
88 38 : test();
89 38 : test();
90 38 : IF( NE_16( fscale, st->fscale ) || ( switchWB != 0 ) || ( bSwitchFromAmrwbIO != 0 ) || EQ_16( st->last_codec_mode, MODE1 ) || st->force_lpd_reset )
91 : {
92 38 : 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 38 : frame_size = FrameSizeConfig[frame_size_index].frame_net_bits;
142 38 : move16();
143 :
144 38 : reconfig_decoder_LPD_fx( st, frame_size, bwidth, total_brate, st->last_L_frame );
145 :
146 38 : test();
147 38 : 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 38 : IF( st->tcxonly == 0 )
157 : {
158 38 : 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 38 : Copy( st->lsfoldbfi1_fx, st->lsfoldbfi0_fx, M ); /* Q2.56 */
165 38 : Copy( st->lsfoldbfi1_fx, st->lsf_adaptive_mean_fx, M ); /* Q2.56 */
166 :
167 38 : IF( st->igf != 0 && hBWE_TD != NULL )
168 : {
169 38 : test();
170 38 : test();
171 38 : test();
172 38 : test();
173 38 : test();
174 38 : 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 22 : TBEreset_dec_fx( st );
179 : }
180 : ELSE
181 : {
182 16 : set16_fx( hBWE_TD->state_lpc_syn_fx, 0, LPC_SHB_ORDER );
183 16 : set16_fx( hBWE_TD->state_syn_shbexc_fx, 0, L_SHB_LAHEAD );
184 16 : set16_fx( hBWE_TD->mem_stp_swb_fx, 0, LPC_SHB_ORDER );
185 16 : set16_fx( hBWE_TD->mem_zero_swb_fx, 0, LPC_SHB_ORDER );
186 16 : hBWE_TD->gain_prec_swb_fx = 16384;
187 16 : move16(); /*Q14 = 1 */
188 : }
189 : }
190 :
191 38 : test();
192 38 : test();
193 38 : test();
194 38 : IF( ( EQ_16( bwidth, SWB ) ) && ( EQ_32( total_brate, ACELP_16k40 ) || EQ_32( total_brate, ACELP_24k40 ) ) && ( st->element_mode == EVS_MONO ) )
195 : {
196 14 : if ( st->tec_tfa == 0 )
197 : {
198 0 : set16_fx( st->hTECDec->loBuffer, 0, MAX_TEC_SMOOTHING_DEG );
199 : }
200 14 : st->tec_tfa = 1;
201 14 : move16();
202 : }
203 : ELSE
204 : {
205 24 : st->tec_tfa = 0;
206 24 : move16();
207 : }
208 :
209 38 : st->tec_flag = 0;
210 38 : move16();
211 38 : st->tfa_flag = 0;
212 38 : move16();
213 :
214 : /* needed in decoder to read the bitstream */
215 38 : st->enableGplc = 0;
216 38 : move16();
217 38 : test();
218 38 : test();
219 38 : IF( GE_16( bwidth, WB ) && EQ_32( total_brate, 24400 ) && ( st->element_mode == EVS_MONO ) )
220 : {
221 14 : st->enableGplc = 1;
222 14 : move16();
223 : }
224 38 : move16();
225 38 : st->dec_glr = 0;
226 38 : test();
227 38 : test();
228 38 : test();
229 38 : IF( ( EQ_32( total_brate, 9600 ) || EQ_32( total_brate, 16400 ) || EQ_32( total_brate, 24400 ) ) && st->element_mode == EVS_MONO )
230 : {
231 14 : st->dec_glr = 1;
232 14 : move16();
233 : }
234 38 : move16();
235 38 : st->dec_glr_idx = 0;
236 38 : }
237 :
238 23291 : 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 23291 : hTcxDec = st->hTcxDec;
263 23291 : hBWE_TD = st->hBWE_TD;
264 23291 : switchWB = 0;
265 23291 : move16();
266 23291 : bSwitchFromAmrwbIO = 0;
267 23291 : move16();
268 23291 : IF( EQ_16( st->last_core, AMR_WB_CORE ) )
269 : {
270 0 : bSwitchFromAmrwbIO = 1;
271 0 : move16();
272 : }
273 23291 : sr_core = getCoreSamplerateMode2( st->element_mode, total_brate, bwidth, st->flag_ACELP16k, st->rf_flag, st->is_ism_format );
274 23291 : fscale = sr2fscale_fx( sr_core );
275 23291 : move16();
276 :
277 : /* set number of coded lines */
278 23291 : st->hTcxCfg->tcx_coded_lines = getNumTcxCodedLines( bwidth );
279 23291 : test();
280 23291 : test();
281 :
282 23291 : IF( ( GE_16( bwidth, WB ) ) && ( EQ_16( fscale, 640 ) ) && ( EQ_16( fscale, st->fscale ) ) )
283 : {
284 6125 : test();
285 6125 : test();
286 6125 : test();
287 6125 : IF( ( ( GT_32( total_brate, ACELP_32k ) ) && ( ( st->tcxonly == 0 ) ) ) || ( ( LE_32( total_brate, ACELP_32k ) ) && EQ_16( st->tcxonly, 1 ) ) )
288 : {
289 990 : switchWB = 1;
290 990 : move16();
291 : }
292 : }
293 :
294 23291 : test();
295 23291 : IF( GT_16( st->last_L_frame, L_FRAME16k ) && LE_32( total_brate, ACELP_32k ) )
296 : {
297 483 : switchWB = 1; /*force init when coming from MODE1*/
298 483 : move16();
299 : }
300 :
301 23291 : st->igf = getIgfPresent_fx( st->element_mode, total_brate, bwidth, st->rf_flag );
302 23291 : move16();
303 :
304 23291 : IF( st->hIGFDec != NULL )
305 : {
306 23291 : st->hIGFDec->infoIGFStopFreq = -1;
307 23291 : move16();
308 : }
309 :
310 23291 : move16();
311 23291 : test();
312 23291 : test();
313 23291 : IF( st->igf && ( ( st->idchan == 0 ) || EQ_16( st->element_mode, IVAS_CPE_MDCT ) ) )
314 : {
315 : /* switch IGF configuration */
316 18518 : IGFDecSetMode_ivas_fx( st->hIGFDec, total_brate, bwidth, st->element_mode, -1, -1, st->rf_flag );
317 : }
318 23291 : test();
319 23291 : test();
320 23291 : test();
321 23291 : test();
322 :
323 23291 : assert( (Word16) ( st->sr_core / FRAMES_PER_SEC ) == extract_l( Mult_32_16( st->sr_core, 0x0290 ) ) );
324 23291 : IF( NE_16( fscale, st->fscale ) || switchWB || bSwitchFromAmrwbIO || EQ_16( st->last_codec_mode, MODE1 ) || st->force_lpd_reset )
325 : {
326 23291 : 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 23291 : frame_size = FrameSizeConfig[frame_size_index].frame_net_bits;
387 23291 : move16();
388 23291 : reconfig_decoder_LPD_ivas_fx( st, frame_size, bwidth, total_brate, st->last_L_frame );
389 :
390 23291 : test();
391 23291 : 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 23291 : IF( EQ_16( st->tcxonly, 0 ) )
400 : {
401 8957 : lsp2lsf_fx( st->lsp_old_fx, st->lsfoldbfi1_fx, M, extract_l( st->sr_core ) );
402 : }
403 : ELSE
404 : {
405 14334 : E_LPC_lsp_lsf_conversion( st->lsp_old_fx, st->lsfoldbfi1_fx, M );
406 : }
407 23291 : Copy( st->lsfoldbfi1_fx, st->lsfoldbfi0_fx, M ); /* Qx2.56 */
408 23291 : Copy( st->lsfoldbfi1_fx, st->lsf_adaptive_mean_fx, M ); /* Qx2.56 */
409 :
410 23291 : IF( st->igf && hBWE_TD != NULL )
411 : {
412 10655 : test();
413 10655 : test();
414 10655 : test();
415 10655 : test();
416 10655 : test();
417 : /* reset TBE */
418 10655 : 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 5219 : TBEreset_dec_fx( st );
423 : }
424 : ELSE
425 : {
426 5436 : set16_fx( hBWE_TD->state_lpc_syn_fx, 0, LPC_SHB_ORDER );
427 5436 : set16_fx( hBWE_TD->state_syn_shbexc_fx, 0, L_SHB_LAHEAD );
428 5436 : set16_fx( hBWE_TD->mem_stp_swb_fx, 0, LPC_SHB_ORDER );
429 5436 : set16_fx( hBWE_TD->mem_zero_swb_fx, 0, LPC_SHB_ORDER );
430 5436 : hBWE_TD->gain_prec_swb_fx = 16384;
431 5436 : move16(); /*Q14 = 1 */
432 : }
433 : }
434 23291 : test();
435 23291 : test();
436 23291 : test();
437 23291 : 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 23291 : st->tec_tfa = 0;
449 23291 : move16();
450 : }
451 :
452 23291 : st->tec_flag = 0;
453 23291 : move16();
454 23291 : st->tfa_flag = 0;
455 23291 : move16();
456 :
457 : /* needed in decoder to read the bitstream */
458 23291 : st->enableGplc = 0;
459 23291 : move16();
460 23291 : test();
461 23291 : test();
462 23291 : 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 23291 : move16();
468 23291 : st->dec_glr = 0;
469 23291 : test();
470 23291 : test();
471 23291 : test();
472 23291 : 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 23291 : move16();
478 23291 : st->dec_glr_idx = 0;
479 23291 : }
|