Line data Source code
1 : /*====================================================================================
2 : EVS Codec 3GPP TS26.452 Aug 12, 2021. Version 16.3.0
3 : ====================================================================================*/
4 : #include <stdint.h>
5 : #include "options.h" /* Compilation switches */
6 : #include "cnst.h" /* Common constants */
7 : #include "rom_com_fx.h"
8 : #include "rom_com.h"
9 : // #include "basop_mpy.h"
10 : #include <assert.h>
11 : #include "prot_fx.h" /* Function prototypes */
12 : #include "prot_fx_enc.h" /* Function prototypes */
13 :
14 : /*-------------------------------------------------------------------*
15 : * Local constants
16 : *-------------------------------------------------------------------*/
17 : #define ALPHA_ENER_FAST_FX 29491 /* Fast adaptation (noise down, speech up) */
18 : #define ALPHA_ENER_SLOW_FX 32440 /* Fast adaptation (noise down, speech up) */
19 :
20 : #define MIN_CNT 50 /* Minimum frame number before SID interval adaptation */
21 :
22 : #define SNR_H_FX 13056 /* Estimated SNR and corresponding SID interval */
23 : #define SNR_L_FX 9216 /* 51dB corresponds to 25dB SNR before noise supressor */
24 : #define INT_H 50
25 : #define INT_L 8
26 : #define RATIO ( INT_H - INT_L ) / ( ( SNR_H_FX - SNR_L_FX ) / 256 )
27 :
28 : #define LTE_VAR_FX -1024 /* Q8, -4.0f */
29 :
30 : #define CNG_TYPE_HO 20 /* hangover for switching between CNG types */
31 :
32 :
33 : #define MAX_BRATE_DTX_EVS ACELP_24k40 /* maximum bitrate to which the default DTX is applied in EVS; otherwise DTX is applied only in silence */
34 : #define MAX_BRATE_DTX_IVAS IVAS_80k /* maximum bitrate to which the default DTX is applied in IVAS; otherwise DTX is applied only in silence */
35 :
36 : /*-------------------------------------------------------------------*
37 : * Local function prototypes
38 : *-------------------------------------------------------------------*/
39 :
40 : static void update_SID_cnt_fx( DTX_ENC_HANDLE hDtxEnc, const Word32 core_brate, const Word16 Opt_AMR_WB );
41 : /*==================================================================================*/
42 : /* FUNCTION : dtx_ivas_fx() */
43 : /*----------------------------------------------------------------------------------*/
44 : /* PURPOSE : Discontinuous transmission operation */
45 : /*----------------------------------------------------------------------------------*/
46 : /* INPUT ARGUMENTS : */
47 : /* _ (Encoder_State_Fx) st_fx : encoder state structure */
48 : /* _ (Word16) vad : vad flag Q0 */
49 : /* _ (Word16[]) speech_fx : Pointer to the speech frame qSpeech */
50 : /* _ (Word16) qSpeech : speech buffer qformat value */
51 : /* _ (Word16*) qener : frame_ener/lt_ener_voiced/lt_ener_noise buf qformat */
52 : /*----------------------------------------------------------------------------------*/
53 : /* OUTPUT ARGUMENTS : */
54 : /* _ (Word16*) qener : frame_ener/lt_ener_voiced/lt_ener_noise buf qformat */
55 : /* _ (Encoder_State_Fx) st_fx : encoder state structure */
56 : /*----------------------------------------------------------------------------------*/
57 :
58 : /*----------------------------------------------------------------------------------*/
59 : /* RETURN ARGUMENTS : */
60 : /* _ None */
61 : /*==================================================================================*/
62 1150754 : void dtx_ivas_fx(
63 : Encoder_State *st_fx, /* i/o: encoder state structure */
64 : const Word32 last_ivas_total_brate, /* i : last IVAS total bitrate Q0*/
65 : const Word32 ivas_total_brate, /* i : IVAS total bitrate Q0*/
66 : const Word16 vad, /* i : vad flag for DTX Q0*/
67 : const Word16 speech[], /* i : Pointer to the speech frame Q_speech*/
68 : Word16 Q_speech /* i : Q factor for speech */
69 : )
70 : {
71 : Word16 alpha, i, j, Q_speech2;
72 : Word32 L_tmp;
73 1150754 : DTX_ENC_HANDLE hDtxEnc = st_fx->hDtxEnc;
74 1150754 : TD_CNG_ENC_HANDLE hTdCngEnc = st_fx->hTdCngEnc;
75 :
76 : Word16 last_br_cng_flag, last_br_flag, br_dtx_flag;
77 : #ifndef ISSUE_1867_replace_overflow_libenc
78 : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
79 : Flag Overflow = 0;
80 : move32();
81 : #endif
82 : #endif
83 : Word32 total_brate_ref;
84 :
85 1150754 : total_brate_ref = st_fx->total_brate;
86 1150754 : move32();
87 :
88 1150754 : IF( st_fx->dtx_sce_sba != 0 )
89 : {
90 11370 : last_br_cng_flag = 1;
91 11370 : last_br_flag = 1;
92 11370 : move16();
93 11370 : move16();
94 : }
95 : ELSE
96 : {
97 1139384 : test();
98 1139384 : test();
99 1139384 : test();
100 1139384 : last_br_cng_flag = LE_32( st_fx->last_total_brate_cng, MAX_BRATE_DTX_EVS ) || LT_32( ( st_fx->lp_noise_32fx ), DTX_THR * 16777216 ) || ( EQ_16( st_fx->element_mode, IVAS_SCE ) && LE_32( st_fx->last_total_brate_cng, MAX_BRATE_DTX_IVAS ) );
101 :
102 1139384 : test();
103 1139384 : test();
104 1139384 : test();
105 :
106 1139384 : last_br_flag = ( st_fx->element_mode == EVS_MONO && LE_32( st_fx->last_total_brate, MAX_BRATE_DTX_EVS ) ) ||
107 1861646 : ( st_fx->element_mode != EVS_MONO && LE_32( last_ivas_total_brate, MAX_BRATE_DTX_IVAS ) ) ||
108 722262 : LT_32( ( st_fx->lp_noise_32fx ), DTX_THR * 16777216 );
109 : }
110 :
111 : /* Initialization */
112 1150754 : IF( st_fx->ini_frame == 0 )
113 : {
114 8793 : st_fx->active_cnt = CNG_TYPE_HO;
115 8793 : move16();
116 :
117 8793 : st_fx->cng_type = FD_CNG;
118 8793 : move16();
119 8793 : test();
120 8793 : test();
121 8793 : test();
122 8793 : if ( ( EQ_16( st_fx->codec_mode, MODE1 ) || st_fx->Opt_AMR_WB ) && NE_16( st_fx->element_mode, IVAS_SCE ) && NE_16( st_fx->element_mode, IVAS_CPE_MDCT ) )
123 : {
124 132 : st_fx->cng_type = LP_CNG;
125 132 : move16();
126 : }
127 : }
128 1150754 : test();
129 1150754 : test();
130 1150754 : test();
131 1150754 : test();
132 1150754 : test();
133 1150754 : IF( st_fx->Opt_DTX_ON && vad == 0 &&
134 : GT_16( st_fx->ini_frame, 2 ) && /* CNG coding starts after 2 frames */
135 : st_fx->fd_cng_reset_flag == 0 &&
136 : NE_16( st_fx->last_core, AMR_WB_CORE ) &&
137 : st_fx->Opt_AMR_WB == 0 )
138 : {
139 34244 : test();
140 34244 : test();
141 34244 : test();
142 34244 : IF( GT_32( st_fx->last_core_brate, SID_2k40 ) && NE_32( st_fx->last_total_brate_cng, -1 ) &&
143 : NE_32( st_fx->last_total_brate_cng, st_fx->total_brate ) && last_br_cng_flag )
144 : {
145 4073 : st_fx->total_brate = st_fx->last_total_brate_cng;
146 4073 : move32();
147 4073 : test();
148 4073 : if ( !( EQ_32( st_fx->total_brate, ACELP_7k20 ) && st_fx->Opt_SC_VBR ) )
149 : {
150 4073 : st_fx->Opt_SC_VBR = 0;
151 4073 : move16();
152 : }
153 4073 : st_fx->rf_mode = st_fx->last_rf_mode_cng;
154 4073 : move16();
155 4073 : st_fx->bwidth = st_fx->last_bwidth_cng;
156 4073 : move16();
157 4073 : st_fx->codec_mode = st_fx->last_codec_mode_cng;
158 4073 : move16();
159 : }
160 34244 : test();
161 34244 : test();
162 34244 : IF( LE_32( st_fx->last_core_brate, SID_2k40 ) && NE_32( st_fx->last_total_brate, st_fx->total_brate ) && last_br_flag )
163 :
164 : {
165 2934 : st_fx->total_brate = st_fx->last_total_brate;
166 2934 : move32();
167 2934 : test();
168 2934 : if ( !( EQ_32( st_fx->total_brate, ACELP_7k20 ) && st_fx->Opt_SC_VBR ) )
169 : {
170 2934 : st_fx->Opt_SC_VBR = 0;
171 2934 : move16();
172 : }
173 :
174 2934 : st_fx->Opt_RF_ON = 0;
175 2934 : move16();
176 2934 : test();
177 2934 : test();
178 2934 : test();
179 2934 : if ( st_fx->rf_mode && st_fx->rf_fec_offset > 0 && EQ_32( st_fx->total_brate, ACELP_13k20 ) && ( st_fx->bwidth != NB ) )
180 : {
181 0 : st_fx->Opt_RF_ON = 1;
182 0 : move16();
183 : }
184 2934 : st_fx->rf_mode = st_fx->Opt_RF_ON;
185 2934 : move16();
186 2934 : st_fx->bwidth = st_fx->last_bwidth;
187 2934 : move32();
188 2934 : IF( st_fx->element_mode > EVS_MONO )
189 : {
190 2934 : st_fx->codec_mode = MODE1;
191 2934 : move16();
192 : }
193 : ELSE
194 : {
195 0 : st_fx->codec_mode = get_codec_mode( st_fx->total_brate );
196 0 : move16();
197 : }
198 : }
199 : }
200 :
201 : /*------------------------------------------------------------------------*
202 : * Select SID or FRAME_NO_DATA frame if DTX is enabled
203 : *------------------------------------------------------------------------*/
204 :
205 1150754 : br_dtx_flag = 1;
206 1150754 : move16();
207 :
208 1150754 : IF( st_fx->dtx_sce_sba == 0 )
209 : {
210 1139384 : test();
211 1139384 : test();
212 1139384 : test();
213 1139384 : test();
214 1139384 : br_dtx_flag = ( ( st_fx->element_mode == EVS_MONO ) && LE_32( st_fx->total_brate, MAX_BRATE_DTX_EVS ) ) ||
215 1866626 : ( ( st_fx->element_mode != EVS_MONO ) && LE_32( ivas_total_brate, MAX_BRATE_DTX_IVAS ) ) ||
216 727242 : LT_16( extract_h( st_fx->lp_noise_32fx ), DTX_THR * 256 );
217 : }
218 1150754 : test();
219 1150754 : test();
220 1150754 : test();
221 1150754 : test();
222 1150754 : IF( st_fx->Opt_DTX_ON && vad == 0 &&
223 : GT_16( st_fx->ini_frame, 2 ) && /* CNG coding starts after 2 frames */
224 : br_dtx_flag &&
225 : st_fx->fd_cng_reset_flag == 0 )
226 : {
227 : /* reset counter */
228 30535 : st_fx->active_cnt = 0;
229 30535 : move16();
230 :
231 30535 : IF( st_fx->Opt_AMR_WB )
232 : {
233 0 : st_fx->last_total_brate_cng = -1;
234 0 : move16();
235 : }
236 : ELSE
237 : {
238 30535 : st_fx->last_total_brate_cng = st_fx->total_brate;
239 30535 : move16();
240 30535 : st_fx->last_bwidth_cng = st_fx->bwidth;
241 30535 : move16();
242 30535 : st_fx->last_codec_mode_cng = st_fx->codec_mode;
243 30535 : move16();
244 30535 : st_fx->last_rf_mode_cng = st_fx->rf_mode;
245 30535 : move16();
246 : }
247 :
248 30535 : IF( hDtxEnc->cnt_SID == 0 )
249 : {
250 : /* this will be a SID frame */
251 5409 : IF( st_fx->Opt_AMR_WB )
252 : {
253 0 : st_fx->core_brate = SID_1k75;
254 0 : move32();
255 : }
256 : ELSE
257 : {
258 5409 : st_fx->core_brate = SID_2k40;
259 5409 : move32();
260 : }
261 : }
262 : ELSE
263 : {
264 : /* this will be a no data frame */
265 25126 : st_fx->core_brate = FRAME_NO_DATA;
266 25126 : move32();
267 : }
268 :
269 30535 : test();
270 30535 : test();
271 30535 : IF( ( st_fx->core_brate == FRAME_NO_DATA ) && ( st_fx->last_core != ACELP_CORE ) && !st_fx->Opt_AMR_WB )
272 : {
273 : /* force SID frame when switching from HQ core or AMR-WB IO mode into inactive frame in ACELP core when DTX is on */
274 2357 : st_fx->core_brate = SID_2k40;
275 2357 : move32();
276 : }
277 : // PMT("dtx_sce_sba code is missing")
278 30535 : test();
279 30535 : test();
280 30535 : IF( ( ( st_fx->last_core != ACELP_CORE ) || EQ_16( st_fx->cng_type, FD_CNG ) ) && EQ_16( st_fx->dtx_sce_sba, 1 ) )
281 : {
282 759 : st_fx->cng_type = FD_CNG;
283 759 : move16();
284 759 : test();
285 759 : test();
286 759 : test();
287 759 : test();
288 759 : test();
289 759 : test();
290 759 : IF( ( st_fx->element_mode == EVS_MONO ) && ( EQ_32( st_fx->total_brate, ACELP_9k60 ) || EQ_32( st_fx->total_brate, ACELP_16k40 ) || EQ_32( st_fx->total_brate, ACELP_24k40 ) || EQ_32( st_fx->total_brate, ACELP_48k ) || EQ_32( st_fx->total_brate, HQ_96k ) || EQ_32( st_fx->total_brate, HQ_128k ) ) )
291 : {
292 0 : st_fx->codec_mode = MODE2;
293 0 : move16();
294 : }
295 : }
296 : ELSE
297 : {
298 29776 : test();
299 29776 : test();
300 29776 : test();
301 29776 : test();
302 29776 : IF( ( EQ_16( st_fx->cng_type, FD_CNG ) && ( LE_32( st_fx->total_brate, MAX_BRATE_DTX_EVS ) || ( ( st_fx->element_mode != EVS_MONO ) && LE_32( ivas_total_brate, MAX_BRATE_DTX_IVAS ) ) ) ) || ( EQ_16( st_fx->element_mode, IVAS_CPE_MDCT ) ) ) /* at highest bitrates, use exclusively LP_CNG */
303 : {
304 27118 : test();
305 27118 : test();
306 27118 : test();
307 27118 : IF( ( st_fx->element_mode == EVS_MONO ) && ( EQ_32( st_fx->total_brate, ACELP_9k60 ) || EQ_32( st_fx->total_brate, ACELP_16k40 ) || EQ_32( st_fx->total_brate, ACELP_24k40 ) ) )
308 : {
309 0 : st_fx->codec_mode = MODE2;
310 0 : move16();
311 : }
312 : }
313 : ELSE
314 : {
315 2658 : st_fx->cng_type = LP_CNG;
316 2658 : move16();
317 2658 : if ( EQ_16( st_fx->codec_mode, MODE2 ) )
318 : {
319 0 : hTdCngEnc->lp_cng_mode2 = 1;
320 0 : move16();
321 : }
322 2658 : st_fx->codec_mode = MODE1;
323 2658 : move16();
324 : }
325 : }
326 : /* reset the bitstream (IVAS format signalling was already written) */
327 30535 : test();
328 30535 : IF( NE_16( st_fx->element_mode, IVAS_CPE_MDCT ) && st_fx->hBstr != NULL )
329 : {
330 :
331 21372 : reset_indices_enc_fx( st_fx->hBstr, st_fx->hBstr->nb_ind_tot );
332 : }
333 : }
334 1120219 : ELSE IF( st_fx->element_mode != EVS_MONO )
335 : {
336 1120219 : st_fx->total_brate = total_brate_ref;
337 1120219 : move32();
338 : }
339 :
340 : /*------------------------------------------------------------------------*
341 : * Reset counters when in active frame (not in SID or FRAME_NO_DATA frame)
342 : *------------------------------------------------------------------------*/
343 : /* NB core bit rate can be "-1" at startup , so one can not use core_brate_fx <=2400 */
344 1150754 : test();
345 1150754 : test();
346 1150754 : IF( ( NE_32( st_fx->core_brate, SID_2k40 ) ) && ( NE_32( st_fx->core_brate, SID_1k75 ) ) && ( st_fx->core_brate != FRAME_NO_DATA ) )
347 : {
348 1120219 : IF( hDtxEnc != NULL )
349 : {
350 108417 : hDtxEnc->cnt_SID = 0;
351 108417 : move16();
352 :
353 : /* change SID update rate */
354 : /* first SID update is only 8 (3) frames after the active speech end */
355 108417 : IF( !st_fx->Opt_AMR_WB )
356 : {
357 108417 : hDtxEnc->max_SID = FIXED_SID_RATE;
358 108417 : move16();
359 : }
360 : ELSE
361 : {
362 0 : hDtxEnc->max_SID = 3;
363 0 : move16(); /* first SID update is only 3 frames after the active speech end */
364 : }
365 :
366 108417 : if ( LT_16( hDtxEnc->interval_SID, hDtxEnc->max_SID ) )
367 : {
368 0 : hDtxEnc->max_SID = hDtxEnc->interval_SID;
369 0 : move16(); /* change SID update rate */
370 : }
371 :
372 108417 : hDtxEnc->cng_cnt = 0;
373 108417 : move16(); /* reset the counter of CNG frames for averaging */
374 : }
375 1120219 : test();
376 1120219 : test();
377 1120219 : IF( GE_16( st_fx->active_cnt, CNG_TYPE_HO ) && st_fx->Opt_AMR_WB == 0 && NE_16( st_fx->element_mode, IVAS_CPE_MDCT ) )
378 : {
379 404281 : IF( EQ_16( st_fx->element_mode, IVAS_SCE ) )
380 : {
381 : Word32 lp_thresh, fd_thresh;
382 : // PMT( "lp_thresh scaling is to be found" )
383 351055 : test();
384 351055 : IF( st_fx->Opt_DTX_ON && EQ_16( st_fx->dtx_sce_sba, 1 ) )
385 : {
386 10163 : lp_thresh = 327680; // 5 in Q16
387 10163 : move32();
388 10163 : fd_thresh = 131072; // 2 in Q16
389 10163 : move32();
390 : }
391 : ELSE
392 : {
393 340892 : lp_thresh = 655360; // 10 in Q16
394 340892 : move32();
395 340892 : fd_thresh = 327680; // 5 in Q16
396 340892 : move32();
397 : }
398 :
399 : /*More conservative selection of LP-CNG for SCE*/
400 351055 : test();
401 351055 : test();
402 351055 : test();
403 351055 : IF( st_fx->cng_type == LP_CNG && ( GT_32( st_fx->bckr_tilt_lt, lp_thresh ) ) )
404 : {
405 0 : st_fx->cng_type = FD_CNG;
406 0 : move16();
407 : }
408 351055 : ELSE IF( EQ_16( st_fx->cng_type, FD_CNG ) && ( LT_32( st_fx->bckr_tilt_lt, fd_thresh ) ) && ( GT_32( st_fx->lp_noise_32fx, 67108864 /* 2 in Q24 */ ) ) )
409 : {
410 64 : st_fx->cng_type = LP_CNG;
411 64 : move16();
412 : }
413 : }
414 : ELSE
415 : {
416 53226 : test();
417 53226 : test();
418 53226 : test();
419 53226 : test();
420 53226 : test();
421 53226 : test();
422 53226 : test();
423 53226 : test();
424 53226 : IF( ( st_fx->cng_type == LP_CNG ) && ( ( ( st_fx->input_bwidth == NB ) && GT_32( st_fx->bckr_tilt_lt, 589824 /*9.f Q16*/ ) ) || ( ( st_fx->input_bwidth > NB ) && GT_32( st_fx->bckr_tilt_lt, 2949120l /*45.f Q16*/ ) ) ) )
425 : {
426 66 : st_fx->cng_type = FD_CNG;
427 66 : move16();
428 : }
429 53160 : ELSE IF( EQ_16( st_fx->cng_type, FD_CNG ) && ( ( ( st_fx->input_bwidth == NB ) && LT_32( st_fx->bckr_tilt_lt, 131072l /*2.f Q16*/ ) ) || ( ( st_fx->input_bwidth > NB ) && LT_32( st_fx->bckr_tilt_lt, 655360l /*10.f Q16*/ ) ) ) )
430 : {
431 39 : st_fx->cng_type = LP_CNG;
432 39 : move16();
433 : }
434 : }
435 404281 : st_fx->last_total_brate_cng = -1;
436 404281 : move16();
437 : }
438 715938 : ELSE IF( st_fx->Opt_AMR_WB )
439 : {
440 0 : st_fx->cng_type = LP_CNG;
441 0 : move16();
442 : }
443 1120219 : st_fx->active_cnt = add( st_fx->active_cnt, 1 );
444 1120219 : move16();
445 1120219 : st_fx->active_cnt = s_min( st_fx->active_cnt, 200 );
446 1120219 : move16();
447 : }
448 :
449 : /*------------------------------------------------------------------------*
450 : * Update speech and background noise long-term energy
451 : *------------------------------------------------------------------------*/
452 1150754 : IF( hDtxEnc != NULL )
453 : {
454 138952 : hDtxEnc->frame_ener_fx = L_deposit_l( 0 );
455 138952 : move32();
456 :
457 138952 : IF( st_fx->Opt_DTX_ON )
458 : {
459 138952 : Q_speech2 = add( shl( Q_speech, 1 ), 7 );
460 2362184 : FOR( j = 0; j < 16; j++ )
461 : {
462 2223232 : L_tmp = L_mult0( *speech, *speech ); /*2*Q_speech*/
463 2223232 : speech++;
464 35571712 : FOR( i = 1; i < L_FRAME / 16; i++ )
465 : {
466 : #ifdef ISSUE_1867_replace_overflow_libenc
467 33348480 : L_tmp = L_mac0_sat( L_tmp, *speech, *speech ); /*2*Q_speech*/
468 : #else
469 : L_tmp = L_mac0_o( L_tmp, *speech, *speech, &Overflow ); /*2*Q_speech*/
470 : #endif
471 33348480 : speech++;
472 : }
473 2223232 : hDtxEnc->frame_ener_fx = L_add( hDtxEnc->frame_ener_fx, L_shr( L_tmp, Q_speech2 ) ); /* Q(-7) */
474 2223232 : move32();
475 : }
476 :
477 : /* Active speech (voiced) */
478 :
479 138952 : IF( EQ_16( st_fx->clas, VOICED_CLAS ) )
480 : {
481 20736 : alpha = ALPHA_ENER_SLOW_FX; /*Q15 */
482 20736 : move16();
483 20736 : if ( GT_32( hDtxEnc->frame_ener_fx, hDtxEnc->lt_ener_voiced_fx ) )
484 : {
485 6511 : alpha = ALPHA_ENER_FAST_FX; /*Q15 */
486 6511 : move16();
487 : }
488 :
489 : /*st_fx->lt_ener_voiced_fx = alpha * st_fx->lt_ener_voiced_fx + (1.0f-alpha) * st_fx->frame_ener_fx;*/
490 20736 : L_tmp = L_sub( hDtxEnc->lt_ener_voiced_fx, hDtxEnc->frame_ener_fx ); /*Q(-7)*/
491 20736 : L_tmp = Mult_32_16( L_tmp, alpha ); /*Q(-7)*/
492 20736 : hDtxEnc->lt_ener_voiced_fx = L_add( L_tmp, hDtxEnc->frame_ener_fx ); /* Q(-7) */
493 20736 : move32();
494 :
495 20736 : hDtxEnc->VarDTX_cnt_voiced = add( hDtxEnc->VarDTX_cnt_voiced, 1 );
496 20736 : move16();
497 :
498 20736 : hDtxEnc->VarDTX_cnt_voiced = s_min( hDtxEnc->VarDTX_cnt_voiced, MIN_CNT );
499 20736 : move16();
500 : }
501 : /* Background noise */
502 118216 : ELSE IF( !st_fx->Opt_AMR_WB )
503 : {
504 118216 : alpha = ALPHA_ENER_SLOW_FX; /*Q15*/
505 118216 : move16();
506 118216 : if ( LT_32( hDtxEnc->frame_ener_fx, hDtxEnc->lt_ener_noise_fx ) )
507 : {
508 30896 : alpha = ALPHA_ENER_FAST_FX; /*Q15*/
509 30896 : move16();
510 : }
511 :
512 : /*st_fx->lt_ener_noise_fx = alpha * st_fx->lt_ener_noise_fx + (1.0f-alpha) * st_fx->frame_ener_fx;*/
513 118216 : L_tmp = L_sub( hDtxEnc->lt_ener_noise_fx, hDtxEnc->frame_ener_fx ); /* Q(-7) */
514 118216 : L_tmp = Mult_32_16( L_tmp, alpha ); /* Q(-7) */
515 118216 : hDtxEnc->lt_ener_noise_fx = L_add( L_tmp, hDtxEnc->frame_ener_fx ); /* Q(-7) */
516 118216 : move32();
517 :
518 118216 : hDtxEnc->VarDTX_cnt_noise = add( hDtxEnc->VarDTX_cnt_noise, 1 );
519 118216 : move16();
520 :
521 118216 : hDtxEnc->VarDTX_cnt_noise = s_min( hDtxEnc->VarDTX_cnt_noise, MIN_CNT );
522 118216 : move16();
523 : }
524 : }
525 : }
526 :
527 : /* Update of the SID counter */
528 1150754 : update_SID_cnt_fx( hDtxEnc, st_fx->core_brate, st_fx->Opt_AMR_WB );
529 :
530 : /* Update encoded bandwidth */
531 1150754 : test();
532 1150754 : test();
533 1150754 : IF( st_fx->Opt_DTX_ON && ( EQ_32( st_fx->core_brate, SID_2k40 ) || ( st_fx->core_brate == FRAME_NO_DATA ) ) )
534 : {
535 :
536 30535 : st_fx->bwidth = st_fx->last_bwidth;
537 30535 : move16();
538 30535 : test();
539 30535 : IF( GT_32( st_fx->last_core_brate, SID_2k40 ) && NE_32( st_fx->last_total_brate_cng, -1 ) )
540 : {
541 7596 : st_fx->bwidth = st_fx->last_bwidth_cng;
542 7596 : move16();
543 : }
544 :
545 30535 : test();
546 30535 : test();
547 30535 : IF( st_fx->Opt_RF_ON && ( EQ_32( st_fx->total_brate, ACELP_13k20 ) ) && ( ( st_fx->bwidth == NB ) ) )
548 : {
549 0 : st_fx->codec_mode = MODE1;
550 0 : move16();
551 0 : reset_rf_indices_fx( st_fx );
552 0 : st_fx->Opt_RF_ON = 0;
553 0 : move16();
554 0 : st_fx->rf_mode = 0;
555 0 : move16();
556 : }
557 :
558 30535 : test();
559 30535 : IF( st_fx->Opt_RF_ON && NE_32( st_fx->total_brate, ACELP_13k20 ) )
560 : {
561 0 : reset_rf_indices_fx( st_fx );
562 0 : move16();
563 0 : st_fx->Opt_RF_ON = 0;
564 0 : move16();
565 0 : st_fx->rf_mode = 0;
566 : }
567 :
568 : /* Set and limit the encoded bandwidth */
569 30535 : IF( EQ_16( st_fx->codec_mode, MODE2 ) )
570 : {
571 : Word16 n, bits_frame_nominal;
572 :
573 : UWord16 lsb;
574 : Word16 tmpbandwidthMin;
575 :
576 0 : Mpy_32_16_ss( st_fx->total_brate, 5243, &L_tmp, &lsb ); /* 5243 is 1/50 in Q18. (0+18-15=3) */
577 0 : bits_frame_nominal = extract_l( L_shr( L_tmp, 3 ) ); /* Q0 */
578 :
579 0 : FOR( n = 0; n < FRAME_SIZE_NB; n++ )
580 : {
581 0 : IF( EQ_16( FrameSizeConfig[n].frame_bits, bits_frame_nominal ) )
582 : {
583 0 : BREAK;
584 : }
585 : }
586 0 : IF( EQ_16( n, FRAME_SIZE_NB ) )
587 : {
588 0 : assert( !"Bitrate not supported: not part of EVS" );
589 : }
590 0 : tmpbandwidthMin = FrameSizeConfig[n].bandwidth_min;
591 0 : move16();
592 0 : if ( EQ_16( st_fx->rf_mode, 1 ) )
593 : {
594 0 : tmpbandwidthMin = WB;
595 0 : move16();
596 : }
597 0 : st_fx->bwidth = s_max( s_min( st_fx->bwidth, FrameSizeConfig[n].bandwidth_max ), tmpbandwidthMin );
598 0 : move16();
599 : }
600 : }
601 :
602 1150754 : return;
603 : }
604 :
605 : /*==================================================================================*/
606 : /* FUNCTION : dtx_fx() */
607 : /*----------------------------------------------------------------------------------*/
608 : /* PURPOSE : Discontinuous transmission operation */
609 : /*----------------------------------------------------------------------------------*/
610 : /* INPUT ARGUMENTS : */
611 : /* _ (Encoder_State_Fx) st_fx : encoder state structure */
612 : /* _ (Word16) vad : vad flag Q0 */
613 : /* _ (Word16[]) speech_fx : Pointer to the speech frame qSpeech */
614 : /* _ (Word16) qSpeech : speech buffer qformat value */
615 : /* _ (Word16*) qener : frame_ener/lt_ener_voiced/lt_ener_noise buf qformat */
616 : /*----------------------------------------------------------------------------------*/
617 : /* OUTPUT ARGUMENTS : */
618 : /* _ (Word16*) qener : frame_ener/lt_ener_voiced/lt_ener_noise buf qformat */
619 : /* _ (Encoder_State_Fx) st_fx : encoder state structure */
620 : /*----------------------------------------------------------------------------------*/
621 :
622 : /*----------------------------------------------------------------------------------*/
623 : /* RETURN ARGUMENTS : */
624 : /* _ None */
625 : /*==================================================================================*/
626 3100 : void dtx_fx(
627 : Encoder_State *st_fx, /* i/o: encoder state structure */
628 : const Word16 vad, /* i : vad flag for DTX Q0*/
629 : const Word16 speech[], /* i : Pointer to the speech frame Q_speech*/
630 : Word16 Q_speech /* i : Q factor for speech */
631 :
632 : )
633 : {
634 : Word16 alpha, j, i, Q_speech2;
635 : Word32 L_tmp;
636 3100 : DTX_ENC_HANDLE hDtxEnc = st_fx->hDtxEnc;
637 3100 : TD_CNG_ENC_HANDLE hTdCngEnc = st_fx->hTdCngEnc;
638 :
639 : Word16 last_br_cng_flag, last_br_flag, br_dtx_flag;
640 : #ifndef ISSUE_1867_replace_overflow_libenc
641 : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
642 : Flag Overflow = 0;
643 : move32();
644 : #endif
645 : #endif
646 :
647 3100 : IF( st_fx->dtx_sce_sba != 0 )
648 : {
649 0 : last_br_cng_flag = 1;
650 0 : last_br_flag = 1;
651 0 : br_dtx_flag = 1;
652 0 : move16();
653 0 : move16();
654 0 : move16();
655 : }
656 : ELSE
657 : {
658 3100 : last_br_cng_flag = LE_32( st_fx->last_total_brate_cng, ACELP_24k40 ) || LT_16( st_fx->lp_noise_fx, DTX_THR * 256 ) || ( EQ_16( st_fx->element_mode, IVAS_SCE ) && LE_32( st_fx->last_total_brate_cng, ACELP_32k ) );
659 :
660 3100 : last_br_flag = LE_32( st_fx->last_total_brate, ACELP_24k40 ) || LT_16( st_fx->lp_noise_fx, DTX_THR * 256 ) || ( EQ_16( st_fx->element_mode, IVAS_SCE ) && LE_32( st_fx->last_total_brate, ACELP_32k ) );
661 3100 : br_dtx_flag = 0;
662 3100 : move16();
663 : }
664 : /* Initialization */
665 3100 : IF( st_fx->ini_frame == 0 )
666 : {
667 3 : st_fx->active_cnt = CNG_TYPE_HO;
668 3 : move16();
669 :
670 3 : st_fx->cng_type = FD_CNG;
671 3 : move16();
672 3 : test();
673 3 : IF( ( EQ_16( st_fx->codec_mode, MODE1 ) || st_fx->Opt_AMR_WB ) && EQ_16( st_fx->element_mode, IVAS_SCE ) && EQ_16( st_fx->element_mode, IVAS_CPE_MDCT ) )
674 : {
675 0 : st_fx->cng_type = LP_CNG;
676 0 : move16();
677 : }
678 : }
679 3100 : test();
680 3100 : test();
681 3100 : IF( st_fx->Opt_DTX_ON && vad == 0 &&
682 : GT_16( st_fx->ini_frame, 2 ) && /* CNG coding starts after 2 frames */
683 : st_fx->fd_cng_reset_flag == 0 &&
684 : NE_16( st_fx->last_core, AMR_WB_CORE ) &&
685 : st_fx->Opt_AMR_WB == 0 )
686 : {
687 0 : test();
688 0 : test();
689 0 : test();
690 0 : IF( GT_32( st_fx->last_core_brate, SID_2k40 ) && NE_32( st_fx->last_total_brate_cng, -1 ) &&
691 : NE_32( st_fx->last_total_brate_cng, st_fx->total_brate ) && last_br_cng_flag )
692 : {
693 0 : st_fx->total_brate = st_fx->last_total_brate_cng;
694 0 : move32();
695 0 : test();
696 0 : IF( !( EQ_32( st_fx->total_brate, ACELP_7k20 ) && st_fx->Opt_SC_VBR ) )
697 : {
698 0 : st_fx->Opt_SC_VBR = 0;
699 0 : move16();
700 : }
701 0 : st_fx->rf_mode = st_fx->last_rf_mode_cng;
702 0 : move16();
703 0 : st_fx->bwidth = st_fx->last_bwidth_cng;
704 0 : move16();
705 0 : st_fx->codec_mode = st_fx->last_codec_mode_cng;
706 0 : move16();
707 : }
708 0 : test();
709 0 : test();
710 0 : IF( LE_32( st_fx->last_core_brate, SID_2k40 ) && NE_32( st_fx->last_total_brate, st_fx->total_brate ) && last_br_flag )
711 :
712 : {
713 0 : st_fx->total_brate = st_fx->last_total_brate;
714 0 : move32();
715 0 : test();
716 0 : IF( !( EQ_32( st_fx->total_brate, ACELP_7k20 ) && st_fx->Opt_SC_VBR ) )
717 : {
718 0 : st_fx->Opt_SC_VBR = 0;
719 0 : move16();
720 : }
721 :
722 0 : st_fx->Opt_RF_ON = 0;
723 0 : move16();
724 0 : test();
725 0 : test();
726 0 : test();
727 0 : IF( st_fx->rf_mode && st_fx->rf_fec_offset > 0 && EQ_32( st_fx->total_brate, ACELP_13k20 ) && NE_16( st_fx->bwidth, NB ) )
728 : {
729 0 : st_fx->Opt_RF_ON = 1;
730 0 : move16();
731 : }
732 0 : st_fx->rf_mode = st_fx->Opt_RF_ON;
733 0 : move16();
734 0 : st_fx->bwidth = st_fx->last_bwidth;
735 0 : move32();
736 0 : IF( ( st_fx->element_mode > EVS_MONO ) )
737 : {
738 0 : st_fx->codec_mode = MODE1;
739 0 : move16();
740 : }
741 : ELSE
742 : {
743 0 : st_fx->codec_mode = get_codec_mode( st_fx->total_brate );
744 : }
745 : }
746 : }
747 :
748 : /*------------------------------------------------------------------------*
749 : * Select SID or FRAME_NO_DATA frame if DTX is enabled
750 : *------------------------------------------------------------------------*/
751 :
752 3100 : if ( st_fx->dtx_sce_sba == 0 )
753 : {
754 7200 : br_dtx_flag = LE_32( st_fx->total_brate, ACELP_24k40 ) || LT_16( st_fx->lp_noise_fx, DTX_THR * 256 ) || ( EQ_16( st_fx->element_mode, IVAS_SCE ) && LE_32( st_fx->total_brate, ACELP_32k ) ) ||
755 4100 : EQ_16( st_fx->element_mode, IVAS_CPE_DFT ) || ( EQ_16( st_fx->element_mode, IVAS_CPE_MDCT ) && ( LE_32( st_fx->element_brate, IVAS_64k ) || LT_16( st_fx->lp_noise_fx, DTX_THR * 256 ) ) );
756 : }
757 3100 : test();
758 3100 : test();
759 3100 : test();
760 3100 : test();
761 3100 : IF( st_fx->Opt_DTX_ON && vad == 0 &&
762 : GT_16( st_fx->ini_frame, 2 ) && /* CNG coding starts after 2 frames */
763 : br_dtx_flag &&
764 : st_fx->fd_cng_reset_flag == 0 )
765 : {
766 : /* reset counter */
767 0 : st_fx->active_cnt = 0;
768 0 : move16();
769 :
770 0 : IF( st_fx->Opt_AMR_WB )
771 : {
772 0 : st_fx->last_total_brate_cng = -1;
773 0 : st_fx->last_rf_mode_cng = st_fx->rf_mode;
774 0 : move32();
775 0 : move16();
776 : }
777 : ELSE
778 : {
779 0 : st_fx->last_total_brate_cng = st_fx->total_brate;
780 0 : st_fx->last_bwidth_cng = st_fx->bwidth;
781 0 : st_fx->last_codec_mode_cng = st_fx->codec_mode;
782 0 : move32();
783 0 : move16();
784 0 : move16();
785 : }
786 :
787 0 : IF( hDtxEnc->cnt_SID == 0 )
788 : {
789 : /* this will be a SID frame */
790 0 : IF( st_fx->Opt_AMR_WB )
791 : {
792 0 : st_fx->core_brate = SID_1k75;
793 0 : move32();
794 : }
795 : ELSE
796 : {
797 0 : st_fx->core_brate = SID_2k40;
798 0 : move32();
799 : }
800 : }
801 : ELSE
802 : {
803 : /* this will be a no data frame */
804 0 : st_fx->core_brate = FRAME_NO_DATA;
805 0 : move32();
806 : }
807 :
808 0 : test();
809 0 : test();
810 0 : IF( ( st_fx->core_brate == FRAME_NO_DATA ) && ( st_fx->last_core != ACELP_CORE ) && !st_fx->Opt_AMR_WB )
811 : {
812 : /* force SID frame when switching from HQ core or AMR-WB IO mode into inactive frame in ACELP core when DTX is on */
813 0 : st_fx->core_brate = SID_2k40;
814 0 : move32();
815 : }
816 :
817 : {
818 0 : test();
819 0 : test();
820 0 : test();
821 0 : test();
822 0 : IF( ( EQ_16( st_fx->cng_type, FD_CNG ) && ( LE_32( st_fx->total_brate, ACELP_24k40 ) || ( ( st_fx->element_mode != EVS_MONO ) && LE_32( st_fx->total_brate, ACELP_32k ) ) ) ) || ( EQ_16( st_fx->element_mode, IVAS_CPE_MDCT ) ) ) /* at highest bitrates, use exclusively LP_CNG */
823 : {
824 0 : test();
825 0 : test();
826 0 : IF( EQ_16( st_fx->element_mode, EVS_MONO ) && ( EQ_32( st_fx->total_brate, ACELP_9k60 ) || EQ_32( st_fx->total_brate, ACELP_16k40 ) || EQ_32( st_fx->total_brate, ACELP_24k40 ) ) )
827 : {
828 0 : st_fx->codec_mode = MODE2;
829 0 : move16();
830 : }
831 : }
832 : ELSE
833 : {
834 0 : st_fx->cng_type = LP_CNG;
835 0 : move16();
836 0 : IF( st_fx->codec_mode == MODE2 )
837 : {
838 0 : hTdCngEnc->lp_cng_mode2 = 1;
839 0 : move16();
840 : }
841 0 : st_fx->codec_mode = MODE1;
842 0 : move16();
843 : }
844 : }
845 :
846 : /* reset the bitstream (IVAS format signalling was already written) */
847 0 : IF( NE_16( st_fx->element_mode, IVAS_CPE_MDCT ) && st_fx->hBstr != NULL )
848 : {
849 0 : reset_indices_enc_fx( st_fx->hBstr, st_fx->hBstr->nb_ind_tot );
850 : }
851 : }
852 :
853 : /*------------------------------------------------------------------------*
854 : * Reset counters when in active frame (not in SID or FRAME_NO_DATA frame)
855 : *------------------------------------------------------------------------*/
856 : /* NB core bit rate can be "-1" at startup , so one can not use core_brate_fx <=2400 */
857 3100 : test();
858 3100 : test();
859 3100 : IF( ( NE_32( st_fx->core_brate, SID_2k40 ) ) && ( NE_32( st_fx->core_brate, SID_1k75 ) ) && ( st_fx->core_brate != FRAME_NO_DATA ) )
860 : {
861 3100 : IF( hDtxEnc != NULL )
862 : {
863 3100 : hDtxEnc->cnt_SID = 0;
864 3100 : move16();
865 :
866 : /* change SID update rate */
867 : /* first SID update is only 8 (3) frames after the active speech end */
868 3100 : IF( !st_fx->Opt_AMR_WB )
869 : {
870 3100 : hDtxEnc->max_SID = FIXED_SID_RATE;
871 3100 : move16();
872 : }
873 : ELSE
874 : {
875 0 : hDtxEnc->max_SID = 3;
876 0 : move16(); /* first SID update is only 3 frames after the active speech end */
877 : }
878 :
879 3100 : IF( LT_16( hDtxEnc->interval_SID, hDtxEnc->max_SID ) )
880 : {
881 0 : hDtxEnc->max_SID = hDtxEnc->interval_SID;
882 0 : move16(); /* change SID update rate */
883 : }
884 :
885 3100 : hDtxEnc->cng_cnt = 0;
886 3100 : move16(); /* reset the counter of CNG frames for averaging */
887 : }
888 3100 : test();
889 3100 : IF( GE_16( st_fx->active_cnt, CNG_TYPE_HO ) && st_fx->Opt_AMR_WB == 0 && NE_16( st_fx->element_mode, IVAS_CPE_MDCT ) )
890 : {
891 3100 : IF( EQ_16( st_fx->element_mode, IVAS_SCE ) )
892 : {
893 : }
894 : ELSE
895 : {
896 3100 : test();
897 3100 : test();
898 3100 : test();
899 3100 : test();
900 3100 : test();
901 3100 : test();
902 3100 : test();
903 3100 : test();
904 3100 : IF( EQ_16( st_fx->cng_type, LP_CNG ) && ( ( EQ_16( st_fx->input_bwidth, NB ) && GT_32( st_fx->bckr_tilt_lt, 589824l /*9.f Q16*/ ) ) || ( GT_16( st_fx->input_bwidth, NB ) && GT_32( st_fx->bckr_tilt_lt, 2949120l /*45.f Q16*/ ) ) ) )
905 : {
906 0 : st_fx->cng_type = FD_CNG;
907 0 : move16();
908 : }
909 3100 : ELSE IF( EQ_16( st_fx->cng_type, FD_CNG ) && ( ( EQ_16( st_fx->input_bwidth, NB ) && LT_32( st_fx->bckr_tilt_lt, 131072l /*2.f Q16*/ ) ) || ( GT_16( st_fx->input_bwidth, NB ) && LT_32( st_fx->bckr_tilt_lt, 655360l /*10.f Q16*/ ) ) ) )
910 : {
911 3 : st_fx->cng_type = LP_CNG;
912 3 : move16();
913 : }
914 : }
915 3100 : st_fx->last_total_brate_cng = -1;
916 : }
917 0 : ELSE IF( st_fx->Opt_AMR_WB )
918 : {
919 0 : st_fx->cng_type = LP_CNG;
920 0 : move16();
921 : }
922 3100 : st_fx->active_cnt = add( st_fx->active_cnt, 1 );
923 3100 : st_fx->active_cnt = s_min( st_fx->active_cnt, 200 );
924 : }
925 :
926 : /*------------------------------------------------------------------------*
927 : * Update speech and background noise long-term energy
928 : *------------------------------------------------------------------------*/
929 3100 : IF( hDtxEnc != NULL )
930 : {
931 3100 : hDtxEnc->frame_ener_fx = L_deposit_l( 0 );
932 :
933 3100 : IF( st_fx->Opt_DTX_ON )
934 : {
935 0 : Q_speech2 = add( shl( Q_speech, 1 ), 7 );
936 0 : FOR( j = 0; j < 16; j++ )
937 : {
938 0 : L_tmp = L_mult0( *speech, *speech ); /*2*Q_speech*/
939 0 : speech++;
940 0 : FOR( i = 1; i < L_FRAME / 16; i++ )
941 : {
942 : #ifdef ISSUE_1867_replace_overflow_libenc
943 0 : L_tmp = L_mac0_sat( L_tmp, *speech, *speech ); /*2*Q_speech*/
944 : #else
945 : L_tmp = L_mac0_o( L_tmp, *speech, *speech, &Overflow ); /*2*Q_speech*/
946 : #endif
947 0 : speech++;
948 : }
949 0 : hDtxEnc->frame_ener_fx = L_add( hDtxEnc->frame_ener_fx, L_shr( L_tmp, Q_speech2 ) ); /*Q(-7) */
950 : }
951 :
952 : /* Active speech (voiced) */
953 :
954 0 : IF( EQ_16( st_fx->clas, VOICED_CLAS ) )
955 : {
956 0 : alpha = ALPHA_ENER_SLOW_FX;
957 0 : move16();
958 0 : if ( GT_32( hDtxEnc->frame_ener_fx, hDtxEnc->lt_ener_voiced_fx ) )
959 : {
960 0 : alpha = ALPHA_ENER_FAST_FX; /*Q15 */
961 0 : move16();
962 : }
963 :
964 : /*st_fx->lt_ener_voiced_fx = alpha * st_fx->lt_ener_voiced_fx + (1.0f-alpha) * st_fx->frame_ener_fx;*/
965 0 : L_tmp = L_sub( hDtxEnc->lt_ener_voiced_fx, hDtxEnc->frame_ener_fx ); /*Q(-7)*/
966 0 : L_tmp = Mult_32_16( L_tmp, alpha ); /*Q(-7)*/
967 0 : hDtxEnc->lt_ener_voiced_fx = L_add( L_tmp, hDtxEnc->frame_ener_fx ); /*Q(-7)*/
968 :
969 0 : hDtxEnc->VarDTX_cnt_voiced = add( hDtxEnc->VarDTX_cnt_voiced, 1 );
970 :
971 0 : hDtxEnc->VarDTX_cnt_voiced = s_min( hDtxEnc->VarDTX_cnt_voiced, MIN_CNT );
972 : }
973 : /* Background noise */
974 0 : ELSE IF( !st_fx->Opt_AMR_WB )
975 : {
976 0 : alpha = ALPHA_ENER_SLOW_FX; /*Q15 */
977 0 : move16();
978 0 : if ( LT_32( hDtxEnc->frame_ener_fx, hDtxEnc->lt_ener_noise_fx ) )
979 : {
980 0 : alpha = ALPHA_ENER_FAST_FX; /*Q15 */
981 0 : move16();
982 : }
983 :
984 : /*st_fx->lt_ener_noise_fx = alpha * st_fx->lt_ener_noise_fx + (1.0f-alpha) * st_fx->frame_ener_fx;*/
985 0 : L_tmp = L_sub( hDtxEnc->lt_ener_noise_fx, hDtxEnc->frame_ener_fx ); /*Q(-7)*/
986 0 : L_tmp = Mult_32_16( L_tmp, alpha ); /*Q(-7)*/
987 0 : hDtxEnc->lt_ener_noise_fx = L_add( L_tmp, hDtxEnc->frame_ener_fx ); /*Q(-7)*/
988 0 : move32();
989 :
990 0 : hDtxEnc->VarDTX_cnt_noise = add( hDtxEnc->VarDTX_cnt_noise, 1 );
991 :
992 0 : hDtxEnc->VarDTX_cnt_noise = s_min( hDtxEnc->VarDTX_cnt_noise, MIN_CNT );
993 : }
994 : }
995 : }
996 :
997 : /* Update of the SID counter */
998 3100 : update_SID_cnt_fx( hDtxEnc, st_fx->core_brate, st_fx->Opt_AMR_WB );
999 :
1000 : /* Update encoded bandwidth */
1001 3100 : test();
1002 3100 : test();
1003 3100 : IF( st_fx->Opt_DTX_ON && ( st_fx->core_brate == SID_2k40 || st_fx->core_brate == FRAME_NO_DATA ) )
1004 : {
1005 :
1006 0 : st_fx->bwidth = st_fx->last_bwidth;
1007 0 : move16();
1008 0 : test();
1009 0 : IF( GT_32( st_fx->last_core_brate, SID_2k40 ) && NE_32( st_fx->last_total_brate_cng, -1 ) )
1010 : {
1011 0 : st_fx->bwidth = st_fx->last_bwidth_cng;
1012 0 : move16();
1013 : }
1014 :
1015 0 : test();
1016 0 : test();
1017 0 : IF( st_fx->Opt_RF_ON && ( EQ_32( st_fx->total_brate, ACELP_13k20 ) ) && ( EQ_16( st_fx->bwidth, NB ) ) )
1018 : {
1019 0 : st_fx->codec_mode = MODE1;
1020 0 : move16();
1021 0 : reset_rf_indices_fx( st_fx );
1022 0 : st_fx->Opt_RF_ON = 0;
1023 0 : move16();
1024 0 : st_fx->rf_mode = 0;
1025 0 : move16();
1026 : }
1027 :
1028 0 : test();
1029 0 : test();
1030 0 : IF( st_fx->Opt_RF_ON && NE_32( st_fx->total_brate, ACELP_13k20 ) )
1031 : {
1032 0 : reset_rf_indices_fx( st_fx );
1033 0 : move16();
1034 0 : st_fx->Opt_RF_ON = 0;
1035 0 : move16();
1036 0 : st_fx->rf_mode = 0;
1037 0 : move16();
1038 : }
1039 :
1040 : /* Set and limit the encoded bandwidth */
1041 0 : IF( EQ_16( st_fx->codec_mode, MODE2 ) )
1042 : {
1043 : Word16 n, bits_frame_nominal;
1044 :
1045 : UWord16 lsb;
1046 : Word16 tmpbandwidthMin;
1047 :
1048 0 : Mpy_32_16_ss( st_fx->total_brate, 5243, &L_tmp, &lsb ); /* 5243 is 1/50 in Q18. (0+18-15=3) */
1049 0 : bits_frame_nominal = extract_l( L_shr( L_tmp, 3 ) ); /* Q0 */
1050 :
1051 0 : FOR( n = 0; n < FRAME_SIZE_NB; n++ )
1052 : {
1053 0 : IF( EQ_16( FrameSizeConfig[n].frame_bits, bits_frame_nominal ) )
1054 : {
1055 0 : BREAK;
1056 : }
1057 : }
1058 0 : if ( n == FRAME_SIZE_NB )
1059 : {
1060 0 : assert( !"Bitrate not supported: not part of EVS" );
1061 : }
1062 0 : tmpbandwidthMin = FrameSizeConfig[n].bandwidth_min;
1063 0 : if ( EQ_16( st_fx->rf_mode, 1 ) )
1064 : {
1065 0 : tmpbandwidthMin = WB;
1066 : }
1067 0 : st_fx->bwidth = s_max( s_min( st_fx->bwidth, FrameSizeConfig[n].bandwidth_max ), tmpbandwidthMin );
1068 : }
1069 : }
1070 :
1071 3100 : return;
1072 : }
1073 :
1074 : /*---------------------------------------------------------------------*
1075 : * update_SID_cnt()
1076 : *
1077 : * Update of the SID counter
1078 : *---------------------------------------------------------------------*/
1079 :
1080 1153854 : static void update_SID_cnt_fx(
1081 : DTX_ENC_HANDLE hDtxEnc, /* i/o: common DTX handle */
1082 : const Word32 core_brate, /* i : core coder core bitrate Q0*/
1083 : const Word16 Opt_AMR_WB /* i : AMR BW IO mode? Q0*/
1084 : )
1085 : {
1086 : Word16 EstimatedSNR, delta, frac, exp;
1087 : Word32 L_tmp1, L_tmp2;
1088 :
1089 1153854 : test();
1090 1153854 : test();
1091 1153854 : IF( EQ_32( core_brate, SID_2k40 ) || EQ_32( core_brate, SID_1k75 ) || core_brate == FRAME_NO_DATA )
1092 : {
1093 : /* Adapt the SID interval */
1094 30535 : test();
1095 30535 : test();
1096 30535 : IF( hDtxEnc->var_SID_rate_flag != 0 && EQ_16( hDtxEnc->VarDTX_cnt_voiced, MIN_CNT ) && EQ_16( hDtxEnc->VarDTX_cnt_noise, MIN_CNT ) )
1097 : {
1098 : /* EstimatedSNR = 10.0f * (float)log10( (0.01f + st_fx->lt_ener_voiced) / (0.01f + st_fx->lt_ener_noise) ); */
1099 :
1100 0 : L_tmp1 = L_max( hDtxEnc->lt_ener_voiced_fx, 1 );
1101 0 : exp = norm_l( L_tmp1 );
1102 0 : frac = Log2_norm_lc( L_shl( L_tmp1, exp ) );
1103 0 : exp = sub( 30, exp );
1104 0 : L_tmp1 = L_Comp( exp, frac ); /*Q16*/
1105 0 : L_tmp2 = L_max( hDtxEnc->lt_ener_noise_fx, 1 );
1106 0 : exp = norm_l( L_tmp2 );
1107 0 : frac = Log2_norm_lc( L_shl( L_tmp2, exp ) );
1108 0 : exp = sub( 30, exp );
1109 0 : L_tmp1 = L_sub( L_tmp1, L_Comp( exp, frac ) ); /*Q16*/
1110 : /* 10 x Log10(a/b) = 10 x Log10(2) x [Log2(a) - Log2(b)] */
1111 : /* 10 x Log10(2) = ~3.0103 */
1112 0 : L_tmp1 = Mpy_32_16_1( L_tmp1, 24660 ); /* mult by 3.0103 / 4 in Q15 */
1113 0 : L_tmp1 = L_shl( L_tmp1, 2 + 8 ); /* mult by 4 and shift left 8 to go in Q24 */
1114 0 : EstimatedSNR = round_fx( L_tmp1 ); /* now in Q8 */
1115 0 : IF( GT_16( EstimatedSNR, SNR_H_FX ) )
1116 : {
1117 0 : hDtxEnc->interval_SID = INT_H;
1118 0 : move16();
1119 : }
1120 0 : ELSE IF( LT_16( EstimatedSNR, SNR_L_FX ) )
1121 : {
1122 0 : hDtxEnc->interval_SID = INT_L;
1123 0 : move16();
1124 : }
1125 : ELSE
1126 : {
1127 0 : hDtxEnc->interval_SID = extract_h( L_mac( INT_L * 65536L - SNR_L_FX / 256 * 65536L * RATIO, ( 32768 / 256 ) * RATIO, EstimatedSNR ) );
1128 : }
1129 0 : hDtxEnc->interval_SID = s_min( s_max( hDtxEnc->interval_SID, INT_L ), INT_H );
1130 :
1131 0 : test();
1132 :
1133 0 : IF( Opt_AMR_WB == 0 || NE_16( hDtxEnc->max_SID, 3 ) )
1134 : {
1135 0 : hDtxEnc->max_SID = hDtxEnc->interval_SID;
1136 0 : move16(); /* change SID update rate */
1137 : }
1138 : }
1139 30535 : test();
1140 30535 : IF( hDtxEnc->cnt_SID != 0 )
1141 : {
1142 25126 : L_tmp1 = L_max( hDtxEnc->lt_ener_noise_fx, 1 );
1143 25126 : exp = norm_l( L_tmp1 );
1144 25126 : frac = Log2_norm_lc( L_shl( L_tmp1, exp ) );
1145 25126 : exp = sub( 30, exp );
1146 25126 : L_tmp1 = L_Comp( exp, frac ); /*Q16*/
1147 25126 : L_tmp2 = L_max( hDtxEnc->lt_ener_last_SID_fx, 1 );
1148 25126 : exp = norm_l( L_tmp2 );
1149 25126 : frac = Log2_norm_lc( L_shl( L_tmp2, exp ) );
1150 25126 : exp = sub( 30, exp );
1151 25126 : L_tmp1 = L_sub( L_tmp1, L_Comp( exp, frac ) ); /*Q16*/
1152 : /* 10 x Log10(a/b) = 10 x Log10(2) x [Log2(a) - Log2(b)] */
1153 : /* 10 x Log10(2) = ~3.0103 */
1154 25126 : L_tmp1 = Mpy_32_16_1( L_tmp1, 24660 ); /* mult by 3.0103 / 4 in Q15 */
1155 25126 : L_tmp1 = L_shl( L_tmp1, 2 + 8 ); /* mult by 4 and shift left 8 to go in Q24 */
1156 25126 : delta = round_fx( L_tmp1 ); /* now in Q8 */
1157 25126 : test();
1158 25126 : test();
1159 25126 : IF( LT_16( delta, LTE_VAR_FX ) && EQ_16( hDtxEnc->VarDTX_cnt_voiced, MIN_CNT ) && EQ_16( hDtxEnc->VarDTX_cnt_noise, MIN_CNT ) )
1160 : {
1161 : /* Send SID frame, and reset lt_ener_noise */
1162 133 : hDtxEnc->lt_ener_noise_fx = hDtxEnc->frame_ener_fx; /* Q(-7) */
1163 133 : move32();
1164 : }
1165 : }
1166 : ELSE
1167 : {
1168 : /* If SID frame was sent, update long-term energy */
1169 5409 : hDtxEnc->lt_ener_last_SID_fx = hDtxEnc->lt_ener_noise_fx; /* Q(-7) */
1170 5409 : move32();
1171 : }
1172 30535 : hDtxEnc->cnt_SID = add( hDtxEnc->cnt_SID, 1 );
1173 :
1174 30535 : IF( hDtxEnc->var_SID_rate_flag )
1175 : {
1176 0 : test();
1177 0 : test();
1178 0 : test();
1179 :
1180 0 : IF( Opt_AMR_WB != 0 && EQ_16( hDtxEnc->max_SID, 3 ) && EQ_16( hDtxEnc->cnt_SID, 3 ) )
1181 : {
1182 : /* set the size of CNG history buffer for averaging to DTX_HIST_SIZE frames */
1183 : /* be sure that DTX_HIST_SIZE >= INT_L */
1184 0 : hDtxEnc->cng_hist_size = 3;
1185 0 : move16();
1186 : }
1187 0 : test();
1188 : /*else if ( st_fx->max_SID != 3 && st_fx->cnt_SID == DTX_HIST_SIZE )//compile error */
1189 0 : if ( NE_16( hDtxEnc->max_SID, 3 ) && EQ_16( hDtxEnc->cnt_SID, DTX_HIST_SIZE ) )
1190 : {
1191 : /* set the size of CNG history buffer for averaging to 3 frames */
1192 0 : hDtxEnc->cng_hist_size = DTX_HIST_SIZE;
1193 0 : move16();
1194 : }
1195 : }
1196 30535 : test();
1197 30535 : IF( hDtxEnc->var_SID_rate_flag == 0 && GT_16( hDtxEnc->interval_SID, 1 ) )
1198 : {
1199 : /* set the size of CNG history buffer for averaging to interval_SID frames */
1200 30535 : hDtxEnc->cng_hist_size = hDtxEnc->interval_SID;
1201 30535 : move16();
1202 30535 : if ( GT_16( hDtxEnc->cng_hist_size, DTX_HIST_SIZE ) )
1203 : {
1204 0 : hDtxEnc->cng_hist_size = DTX_HIST_SIZE;
1205 0 : move16();
1206 : }
1207 : }
1208 30535 : IF( GE_16( hDtxEnc->cnt_SID, hDtxEnc->max_SID ) )
1209 : {
1210 : /* adaptive SID update interval */
1211 3231 : hDtxEnc->max_SID = hDtxEnc->interval_SID;
1212 3231 : move16();
1213 3231 : hDtxEnc->cnt_SID = 0;
1214 3231 : move16();
1215 : }
1216 : }
1217 :
1218 1153854 : return;
1219 : }
1220 :
1221 :
1222 1510 : void dtx_hangover_control_fx(
1223 : Encoder_State *st_fx, /* i/o: encoder state structure */
1224 : const Word16 lsp_new_fx[M] /* i : current frame LSPs Q15*/
1225 : )
1226 : {
1227 : Word16 ptr;
1228 : Word16 i, j, m;
1229 : Word16 tmp_lsp[/*max(DTX_HIST_SIZE,*/ HO_HIST_SIZE /*)*/ * M];
1230 : Word32 tmp_enr[/*max(DTX_HIST_SIZE,*/ HO_HIST_SIZE /*)*/];
1231 : Word16 tmp[/*max(DTX_HIST_SIZE,*/ HO_HIST_SIZE /*)*/ * M];
1232 : Word16 enr_new;
1233 : Word16 weights;
1234 : Word32 enr_est;
1235 : Word16 enr_est_log;
1236 : Word16 lsp_est[M];
1237 : Word16 Dlsp, Denr;
1238 : Word16 lsf_tmp[M];
1239 : Word32 C[M];
1240 : Word32 max_val[2];
1241 : Word16 max_idx[2];
1242 : Word16 ftmp_fx;
1243 : Word16 Dlsp_n2e, Denr_n2e;
1244 : Word16 exp, fra, exp2, fra2;
1245 : Word16 S_max;
1246 : Word16 S_tmp;
1247 : Word32 L_tmp;
1248 1510 : VAD_HANDLE hVAD = st_fx->hVAD;
1249 1510 : DTX_ENC_HANDLE hDtxEnc = st_fx->hDtxEnc;
1250 1510 : TD_CNG_ENC_HANDLE hTdCngEnc = st_fx->hTdCngEnc;
1251 : #ifndef ISSUE_1867_replace_overflow_libenc
1252 : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
1253 : Flag Overflow = 0;
1254 : #endif
1255 : #endif
1256 :
1257 : /* get current frame exc energy in log2 */
1258 1510 : exp = norm_l( hTdCngEnc->ho_ener_circ_fx[hTdCngEnc->ho_circ_ptr] );
1259 1510 : fra = Log2_norm_lc( L_shl( hTdCngEnc->ho_ener_circ_fx[hTdCngEnc->ho_circ_ptr], exp ) );
1260 1510 : exp = sub( sub( 30, exp ), 6 );
1261 1510 : L_tmp = L_Comp( exp, fra ); /*Q16*/
1262 1510 : enr_new = round_fx( L_shl( L_tmp, 8 ) ); /*Q8 */
1263 :
1264 1510 : if ( enr_new < 0 )
1265 : {
1266 245 : enr_new = 0;
1267 245 : move16(); /*Q8 */
1268 : }
1269 :
1270 : /* get energies and lsps of hangover frames */
1271 1510 : ptr = sub( hTdCngEnc->ho_circ_ptr, sub( hTdCngEnc->burst_ho_cnt, 1 ) ); /*Q0*/
1272 1510 : IF( ptr < 0 )
1273 : {
1274 451 : ptr = add( hTdCngEnc->ho_circ_size, ptr ); /*Q0*/
1275 : }
1276 :
1277 5979 : FOR( i = 0; i < hTdCngEnc->burst_ho_cnt - 1; i++ )
1278 : {
1279 4469 : Copy( &( hTdCngEnc->ho_lsp_circ_fx[ptr * M] ), &( tmp_lsp[i * M] ), M ); /*Q15*/
1280 4469 : tmp_enr[i] = hTdCngEnc->ho_ener_circ_fx[ptr]; /*Q6 */
1281 4469 : move32();
1282 :
1283 4469 : ptr = add( ptr, 1 );
1284 4469 : if ( EQ_16( ptr, hTdCngEnc->ho_circ_size ) )
1285 : {
1286 451 : ptr = 0;
1287 451 : move16();
1288 : }
1289 : }
1290 :
1291 : /* get estimated CNG energy and lsps assuming terminate hangover at current frame */
1292 1510 : ptr = sub( hTdCngEnc->burst_ho_cnt, 2 );
1293 1510 : enr_est = Mpy_32_16_1( tmp_enr[ptr], W_DTX_HO_FX[0] ); /*Q6 */
1294 1510 : weights = W_DTX_HO_FX[0]; /*Q15 */
1295 1510 : move16();
1296 1510 : Copy( &( tmp_lsp[ptr * M] ), tmp, M );
1297 1510 : m = 1;
1298 1510 : move16();
1299 :
1300 3394 : FOR( i = 1; i < hTdCngEnc->burst_ho_cnt - 2; i++ )
1301 : {
1302 1884 : test();
1303 1884 : IF( LT_32( Mpy_32_16_1( tmp_enr[ptr - i], ONE_OVER_BUF_H_NRG_FX ), tmp_enr[ptr] ) &&
1304 : GT_32( tmp_enr[ptr - i], Mpy_32_16_1( tmp_enr[ptr], BUF_L_NRG_FX ) ) )
1305 : {
1306 705 : enr_est = L_add( enr_est, Mpy_32_16_1( tmp_enr[ptr - i], W_DTX_HO_FX[i] ) ); /*Q6 */
1307 705 : weights = add( weights, W_DTX_HO_FX[i] ); /*Q15 */
1308 705 : Copy( &tmp_lsp[( ptr - i ) * M], &tmp[m * M], M ); /*Q15*/
1309 705 : m = add( m, 1 );
1310 : }
1311 : }
1312 :
1313 1510 : exp = norm_l( enr_est );
1314 1510 : fra = round_fx_sat( L_shl_sat( enr_est, exp ) ); /*Q6+exp-16*/
1315 1510 : exp2 = norm_s( weights );
1316 1510 : fra2 = shl( weights, exp2 ); /*Q15+exp2*/
1317 1510 : exp = sub( sub( exp, 16 ), exp2 );
1318 1510 : IF( GT_16( fra, fra2 ) )
1319 : {
1320 515 : fra = shr( fra, 1 );
1321 515 : exp = sub( exp, 1 );
1322 : }
1323 1510 : L_tmp = L_deposit_l( div_s( fra, fra2 ) ); /*Q6+exp*/
1324 1510 : enr_est = L_shr( L_tmp, exp ); /*Q6 */
1325 :
1326 1510 : if ( LT_32( enr_est, 64 ) )
1327 : {
1328 260 : enr_est = 64; /*Q6 */
1329 260 : move16();
1330 : }
1331 :
1332 1510 : exp = norm_l( enr_est );
1333 1510 : fra = Log2_norm_lc( L_shl( enr_est, exp ) );
1334 1510 : exp = sub( sub( 30, exp ), 6 );
1335 1510 : L_tmp = L_Comp( exp, fra ); /*Q16*/
1336 1510 : enr_est_log = round_fx( L_shl( L_tmp, 8 ) ); /*Q8 */
1337 1510 : Denr_n2e = abs_s( sub( enr_new, enr_est_log ) ); /*Q8 */
1338 :
1339 1510 : IF( LT_16( m, 3 ) )
1340 : {
1341 1327 : enr_est = L_add( Mpy_32_16_1( enr_est, 26214 ), Mpy_32_16_1( hTdCngEnc->ho_ener_circ_fx[hTdCngEnc->ho_circ_ptr], 6554 ) ); /*Q6 */
1342 : }
1343 : ELSE
1344 : {
1345 183 : enr_est = L_add( Mpy_32_16_1( enr_est, 31130 ), Mpy_32_16_1( hTdCngEnc->ho_ener_circ_fx[hTdCngEnc->ho_circ_ptr], 1638 ) ); /*Q6 */
1346 : }
1347 :
1348 1510 : exp = norm_l( enr_est );
1349 1510 : fra = Log2_norm_lc( L_shl( enr_est, exp ) );
1350 1510 : exp = sub( sub( 30, exp ), 6 );
1351 1510 : L_tmp = L_Comp( exp, fra ); /*Q16*/
1352 1510 : enr_est_log = round_fx( L_shl( L_tmp, 8 ) ); /*Q8 */
1353 :
1354 1510 : if ( enr_est_log < 0 )
1355 : {
1356 267 : enr_est_log = 0;
1357 267 : move16();
1358 : }
1359 :
1360 1510 : set32_fx( max_val, 0, 2 );
1361 1510 : set16_fx( max_idx, 0, 2 );
1362 :
1363 3725 : FOR( i = 0; i < m; i++ )
1364 : {
1365 2215 : IF( EQ_16( st_fx->L_frame, L_FRAME ) )
1366 : {
1367 1101 : lsp2lsf_fx( &tmp[i * M], lsf_tmp, M, INT_FS_FX );
1368 1101 : ftmp_fx = 964; /*6400.0f / ( M + 1 ) * 2.56*/
1369 1101 : move16(); /*QX2.56 */
1370 1101 : S_tmp = sub( 16384, add( lsf_tmp[M - 1], ftmp_fx ) ); /*QX2.56 */
1371 1101 : C[i] = L_mult0( S_tmp, S_tmp ); /*QX6.5536 */
1372 : }
1373 : ELSE
1374 : {
1375 1114 : lsp2lsf_fx( &tmp[i * M], lsf_tmp, M, INT_FS_16k );
1376 1114 : ftmp_fx = 1205; /*8000.0f/ ( M + 1 ) * 2.56*/
1377 1114 : move16(); /*QX2.56 */
1378 1114 : S_tmp = sub( 20480, add( lsf_tmp[M - 1], ftmp_fx ) ); /*QX2.56 */
1379 1114 : C[i] = L_mult0( S_tmp, S_tmp ); /*QX6.5536 */
1380 : }
1381 :
1382 2215 : S_tmp = sub( lsf_tmp[0], ftmp_fx ); /*QX2.56 */
1383 2215 : C[i] = L_mac0( C[i], S_tmp, S_tmp ); /*QX6.5536 */
1384 35440 : FOR( j = 0; j < M - 1; j++ )
1385 : {
1386 33225 : S_tmp = sub( sub( lsf_tmp[j + 1], lsf_tmp[j] ), ftmp_fx ); /*QX2.56 */
1387 33225 : C[i] = L_mac0( C[i], S_tmp, S_tmp ); /*QX6.5536 */
1388 : }
1389 :
1390 2215 : C[i] = Mpy_32_16_1( C[i], 1928 /*Q15*/ ); /*QX6.5536 */
1391 :
1392 2215 : IF( GT_32( C[i], max_val[0] ) )
1393 : {
1394 1810 : max_val[1] = max_val[0]; /*x6.5536*/
1395 1810 : move16();
1396 1810 : max_idx[1] = max_idx[0]; /*Q0*/
1397 1810 : move16();
1398 1810 : max_val[0] = C[i]; /*x6.5536*/
1399 1810 : move16();
1400 1810 : max_idx[0] = i;
1401 1810 : move16();
1402 : }
1403 405 : ELSE IF( GT_32( C[i], max_val[1] ) )
1404 : {
1405 271 : max_val[1] = C[i]; /*x6.5536*/
1406 271 : move16();
1407 271 : max_idx[1] = i;
1408 271 : move16();
1409 : }
1410 : }
1411 :
1412 1510 : IF( EQ_16( m, 1 ) )
1413 : {
1414 1107 : Copy( tmp, lsp_est, M ); /*Q15*/
1415 : }
1416 403 : ELSE IF( LT_16( m, 4 ) )
1417 : {
1418 5525 : FOR( i = 0; i < M; i++ )
1419 : {
1420 5200 : lsp_est[i] = 0; /*Q15 */
1421 5200 : move16();
1422 17280 : FOR( j = 0; j < m; j++ )
1423 : {
1424 : #ifdef ISSUE_1867_replace_overflow_libenc
1425 12080 : lsp_est[i] = add_sat( lsp_est[i], tmp[j * M + i] ); /*Q15 */
1426 : #else
1427 : lsp_est[i] = add_o( lsp_est[i], tmp[j * M + i], &Overflow ); /*Q15 */
1428 : #endif
1429 : }
1430 :
1431 5200 : lsp_est[i] = sub( lsp_est[i], tmp[max_idx[0] * M + i] ); /*Q15 */
1432 5200 : S_tmp = div_s( 1, sub( m, 1 ) ); /*Q15 */
1433 5200 : lsp_est[i] = mult_r( lsp_est[i], S_tmp ); /*Q15 */
1434 : }
1435 : }
1436 : ELSE
1437 : {
1438 1326 : FOR( i = 0; i < M; i++ )
1439 : {
1440 1248 : lsp_est[i] = 0; /*Q15 */
1441 1248 : move16();
1442 6896 : FOR( j = 0; j < m; j++ )
1443 : {
1444 : #ifdef ISSUE_1867_replace_overflow_libenc
1445 5648 : lsp_est[i] = add_sat( lsp_est[i], tmp[j * M + i] ); /*Q15 */
1446 : #else
1447 : lsp_est[i] = add_o( lsp_est[i], tmp[j * M + i], &Overflow ); /*Q15 */
1448 : #endif
1449 : }
1450 :
1451 : #ifdef ISSUE_1867_replace_overflow_libenc
1452 1248 : lsp_est[i] = sub_sat( lsp_est[i], add_sat( tmp[max_idx[0] * M + i], tmp[max_idx[1] * M + i] ) ); /*Q15 */
1453 : #else
1454 : lsp_est[i] = sub_o( lsp_est[i], add_o( tmp[max_idx[0] * M + i], tmp[max_idx[1] * M + i], &Overflow ), &Overflow ); /*Q15 */
1455 : #endif
1456 1248 : S_tmp = div_s( 1, sub( m, 2 ) ); /*Q15 */
1457 1248 : lsp_est[i] = mult_r( lsp_est[i], S_tmp ); /*Q15 */
1458 : }
1459 : }
1460 :
1461 1510 : Dlsp_n2e = 0; /*Q15 */
1462 1510 : move16();
1463 25670 : FOR( i = 0; i < M; i++ )
1464 : {
1465 : #ifdef ISSUE_1867_replace_overflow_libenc
1466 24160 : Dlsp_n2e = add_sat( Dlsp_n2e, abs_s( sub( lsp_new_fx[i], lsp_est[i] ) ) ); /*Q15 */
1467 : #else
1468 : Dlsp_n2e = add_o( Dlsp_n2e, abs_s( sub( lsp_new_fx[i], lsp_est[i] ) ), &Overflow ); /*Q15 */
1469 : #endif
1470 24160 : lsp_est[i] = add( mult_r( 26214, lsp_est[i] ), mult_r( 6554, lsp_new_fx[i] ) ); /*Q15 */
1471 : }
1472 :
1473 : /* get deviation of CNG parameters between newly estimated and current state memory */
1474 1510 : Dlsp = 0;
1475 1510 : move16();
1476 1510 : S_max = 0;
1477 1510 : move16();
1478 :
1479 25670 : FOR( i = 0; i < M; i++ )
1480 : {
1481 24160 : S_tmp = abs_s( sub( hDtxEnc->lspCNG_fx[i], lsp_est[i] ) ); /*Q15 */
1482 : #ifdef ISSUE_1867_replace_overflow_libenc
1483 24160 : Dlsp = add_sat( Dlsp, S_tmp ); /*Q15 */
1484 : #else
1485 : Dlsp = add_o( Dlsp, S_tmp, &Overflow ); /*Q15 */
1486 : #endif
1487 24160 : IF( GT_16( S_tmp, S_max ) )
1488 : {
1489 6907 : S_max = S_tmp; /*Q15 */
1490 : }
1491 : }
1492 :
1493 1510 : exp = norm_l( hTdCngEnc->lp_ener_fx );
1494 1510 : fra = Log2_norm_lc( L_shl( hTdCngEnc->lp_ener_fx, exp ) );
1495 1510 : exp = sub( sub( 30, exp ), 6 );
1496 1510 : L_tmp = L_Comp( exp, fra ); /*Q16*/
1497 1510 : S_tmp = round_fx( L_shl( L_tmp, 8 ) ); /*Q8 */
1498 1510 : Denr = abs_s( sub( S_tmp, enr_est_log ) ); /*Q8 */
1499 :
1500 : /* make decision if DTX hangover can be terminated */
1501 1510 : hVAD->hangover_terminate_flag = 0; /*Q0 */
1502 1510 : move16();
1503 :
1504 1510 : test();
1505 1510 : test();
1506 1510 : test();
1507 1510 : test();
1508 1510 : test();
1509 1510 : test();
1510 1510 : test();
1511 1510 : test();
1512 1510 : test();
1513 1510 : test();
1514 1510 : test();
1515 1510 : IF( ( LT_16( Dlsp, 13107 ) && LT_16( Denr, 359 ) && LT_16( S_max, 3277 ) && LT_16( Dlsp_n2e, 13107 ) && LT_16( Denr_n2e, 308 ) && st_fx->Opt_SC_VBR == 1 ) ||
1516 : ( LT_16( Dlsp, 13107 ) && LT_16( Denr, 205 ) && LT_16( S_max, 3277 ) && LT_16( Dlsp_n2e, 13107 ) && LT_16( Denr_n2e, 205 ) && st_fx->Opt_SC_VBR == 0 ) )
1517 :
1518 : {
1519 65 : hVAD->hangover_terminate_flag = 1; /*Q0 */
1520 65 : move16();
1521 : }
1522 :
1523 1510 : return;
1524 : }
1525 :
1526 : /*-------------------------------------------------------------------*
1527 : * td_cng_enc_init_fx()
1528 : *
1529 : *
1530 : *-------------------------------------------------------------------*/
1531 :
1532 3 : void td_cng_enc_init_fx(
1533 : TD_CNG_ENC_HANDLE hTdCngEnc, /* i/o: DTX/TD CNG data handle */
1534 : const Word16 Opt_DTX_ON, /* i : flag indicating DTX operation Q0*/
1535 : const Word16 max_bwidth /* i : maximum encoded bandwidth Q0*/
1536 : )
1537 : {
1538 :
1539 3 : hTdCngEnc->cng_seed = RANDOM_INITSEED;
1540 3 : hTdCngEnc->cng_ener_seed = RANDOM_INITSEED;
1541 3 : hTdCngEnc->cng_ener_seed1 = RANDOM_INITSEED;
1542 3 : hTdCngEnc->lp_ener_fx = 0;
1543 3 : hTdCngEnc->old_enr_index = -1;
1544 3 : hTdCngEnc->Enew_fx = 0;
1545 :
1546 3 : hTdCngEnc->lp_sp_enr_fx = 0;
1547 3 : hTdCngEnc->last_allow_cn_step = 0;
1548 3 : move16();
1549 3 : move32();
1550 3 : move16();
1551 3 : move16();
1552 3 : move32();
1553 3 : move16();
1554 3 : move16();
1555 :
1556 3 : IF( Opt_DTX_ON )
1557 : {
1558 0 : hTdCngEnc->cng_hist_ptr = -1;
1559 0 : move16();
1560 0 : set16_fx( hTdCngEnc->cng_lsp_hist_fx, 0, DTX_HIST_SIZE * M );
1561 0 : set16_fx( hTdCngEnc->cng_ener_hist_fx, 0, DTX_HIST_SIZE );
1562 :
1563 0 : hTdCngEnc->ho_hist_ptr = -1;
1564 0 : move16();
1565 0 : hTdCngEnc->ho_sid_bw = L_deposit_l( 0 );
1566 0 : set16_fx( hTdCngEnc->ho_lsp_hist_fx, 0, HO_HIST_SIZE * M );
1567 0 : set32_fx( hTdCngEnc->ho_ener_hist_fx, 0, HO_HIST_SIZE );
1568 0 : set32_fx( hTdCngEnc->ho_env_hist_fx, 0, HO_HIST_SIZE * NUM_ENV_CNG );
1569 0 : hTdCngEnc->ho_hist_size = 0;
1570 0 : move16();
1571 0 : hTdCngEnc->act_cnt = 0;
1572 0 : move16();
1573 : }
1574 :
1575 3 : set16_fx( hTdCngEnc->ho_16k_lsp, 0, HO_HIST_SIZE );
1576 3 : hTdCngEnc->act_cnt2 = 0;
1577 3 : hTdCngEnc->num_ho = 0;
1578 3 : move16();
1579 3 : move16();
1580 :
1581 3 : hTdCngEnc->ho_circ_ptr = -1;
1582 3 : move16();
1583 3 : set16_fx( hTdCngEnc->ho_lsp_circ_fx, 0, HO_HIST_SIZE * M );
1584 3 : set32_fx( hTdCngEnc->ho_ener_circ_fx, 0, HO_HIST_SIZE );
1585 3 : set32_fx( hTdCngEnc->ho_env_circ_fx, 0, HO_HIST_SIZE * NUM_ENV_CNG );
1586 3 : hTdCngEnc->ho_circ_size = 0;
1587 3 : hTdCngEnc->burst_ho_cnt = 0;
1588 3 : move16();
1589 3 : move16();
1590 :
1591 3 : hTdCngEnc->cng_buf_cnt = 0;
1592 :
1593 :
1594 3 : set32_fx( hTdCngEnc->lp_env_fx, 0, 20 );
1595 3 : set32_fx( hTdCngEnc->cng_res_env_fx, 0, 20 * 8 );
1596 3 : set16_fx( hTdCngEnc->exc_mem_fx, 0, 24 );
1597 3 : set16_fx( hTdCngEnc->exc_mem1_fx, 0, 30 );
1598 3 : set16_fx( hTdCngEnc->exc_mem2_fx, 0, 30 );
1599 3 : set32_fx( hTdCngEnc->old_env_fx, 0, NUM_ENV_CNG );
1600 :
1601 : /* SWB CNG/DTX */
1602 3 : hTdCngEnc->last_wb_cng_ener_fx = -1541; /* Q8 */
1603 3 : move16();
1604 3 : hTdCngEnc->last_shb_cng_ener_fx = -1541; /* Q8 */
1605 3 : move16();
1606 3 : hTdCngEnc->mov_wb_cng_ener_fx = -1541; /* Q8 */
1607 3 : move16();
1608 3 : hTdCngEnc->mov_shb_cng_ener_fx = -1541; /* Q8 */
1609 3 : move16();
1610 3 : hTdCngEnc->shb_cng_ini_cnt = 1;
1611 3 : move16();
1612 3 : hTdCngEnc->shb_NO_DATA_cnt = 0;
1613 3 : move16();
1614 3 : hTdCngEnc->last_SID_bwidth = s_min( max_bwidth, SWB );
1615 3 : hTdCngEnc->last_vad = 0;
1616 3 : move16();
1617 :
1618 3 : return;
1619 : }
1620 :
1621 148 : void td_cng_enc_init_ivas_fx(
1622 : TD_CNG_ENC_HANDLE hTdCngEnc, /* i/o: DTX/TD CNG data handle */
1623 : const Word16 Opt_DTX_ON, /* i : flag indicating DTX operation Q0*/
1624 : const Word16 max_bwidth /* i : maximum encoded bandwidth Q0*/
1625 : )
1626 : {
1627 :
1628 148 : hTdCngEnc->cng_seed = RANDOM_INITSEED;
1629 148 : move16();
1630 148 : hTdCngEnc->cng_ener_seed = RANDOM_INITSEED;
1631 148 : move16();
1632 148 : hTdCngEnc->cng_ener_seed1 = RANDOM_INITSEED;
1633 148 : move16();
1634 148 : hTdCngEnc->lp_ener_fx = 0;
1635 148 : move16();
1636 148 : hTdCngEnc->old_enr_index = -1;
1637 148 : move16();
1638 148 : hTdCngEnc->Enew_fx = 0;
1639 148 : move16();
1640 :
1641 148 : hTdCngEnc->lp_sp_enr_fx = 0;
1642 148 : move16();
1643 148 : hTdCngEnc->last_allow_cn_step = 0;
1644 148 : move16();
1645 :
1646 148 : hTdCngEnc->CNG_att_fx = 0;
1647 148 : move16();
1648 :
1649 148 : IF( Opt_DTX_ON )
1650 : {
1651 148 : hTdCngEnc->cng_hist_ptr = -1;
1652 148 : move16();
1653 148 : set16_fx( hTdCngEnc->cng_lsp_hist_fx, 0, DTX_HIST_SIZE * M );
1654 148 : set16_fx( hTdCngEnc->cng_ener_hist_fx, 0, DTX_HIST_SIZE );
1655 148 : hTdCngEnc->ho_hist_ptr = -1;
1656 148 : move16();
1657 148 : hTdCngEnc->ho_sid_bw = 0;
1658 148 : move32();
1659 148 : set16_fx( hTdCngEnc->ho_lsp_hist_fx, 0, HO_HIST_SIZE * M );
1660 148 : set32_fx( hTdCngEnc->ho_ener_hist_fx, 0, HO_HIST_SIZE );
1661 148 : set32_fx( hTdCngEnc->ho_env_hist_fx, 0, HO_HIST_SIZE * NUM_ENV_CNG );
1662 148 : hTdCngEnc->ho_hist_size = 0;
1663 148 : move16();
1664 148 : hTdCngEnc->act_cnt = 0;
1665 148 : move16();
1666 : }
1667 :
1668 148 : set16_fx( hTdCngEnc->ho_16k_lsp, 0, HO_HIST_SIZE );
1669 148 : hTdCngEnc->act_cnt2 = 0;
1670 148 : move16();
1671 148 : hTdCngEnc->num_ho = 0;
1672 148 : move16();
1673 :
1674 148 : hTdCngEnc->ho_circ_ptr = -1;
1675 148 : move16();
1676 148 : set16_fx( hTdCngEnc->ho_lsp_circ_fx, 0, HO_HIST_SIZE * M );
1677 148 : set32_fx( hTdCngEnc->ho_ener_circ_fx, 0, HO_HIST_SIZE );
1678 148 : set32_fx( hTdCngEnc->ho_env_circ_fx, 0, HO_HIST_SIZE * NUM_ENV_CNG );
1679 148 : hTdCngEnc->ho_circ_size = 0;
1680 148 : move16();
1681 148 : hTdCngEnc->burst_ho_cnt = 0;
1682 148 : move16();
1683 148 : hTdCngEnc->cng_buf_cnt = 0;
1684 148 : move16();
1685 :
1686 148 : set32_fx( hTdCngEnc->lp_env_fx, 0, 20 );
1687 148 : set32_fx( hTdCngEnc->cng_res_env_fx, 0, 20 * 8 );
1688 148 : set16_fx( hTdCngEnc->exc_mem_fx, 0, 24 );
1689 148 : set16_fx( hTdCngEnc->exc_mem1_fx, 0, 30 );
1690 148 : set16_fx( hTdCngEnc->exc_mem2_fx, 0, 30 );
1691 148 : set32_fx( hTdCngEnc->old_env_fx, 0, NUM_ENV_CNG );
1692 148 : set16_fx( hTdCngEnc->cng_exc2_buf, 0, HO_HIST_SIZE * L_FFT );
1693 148 : set16_fx( hTdCngEnc->cng_Qexc_buf, Q15, HO_HIST_SIZE );
1694 :
1695 : /* SWB CNG/DTX */
1696 148 : hTdCngEnc->last_wb_cng_ener_fx = -1541; /* Q8 */
1697 148 : move16();
1698 148 : hTdCngEnc->last_shb_cng_ener_fx = -1541; /* Q8 */
1699 148 : move16();
1700 148 : hTdCngEnc->mov_wb_cng_ener_fx = -1541; /* Q8 */
1701 148 : move16();
1702 148 : hTdCngEnc->mov_shb_cng_ener_fx = -1541; /* Q8 */
1703 148 : move16();
1704 148 : hTdCngEnc->shb_cng_ini_cnt = 1;
1705 148 : move16();
1706 148 : hTdCngEnc->shb_NO_DATA_cnt = 0;
1707 148 : move16();
1708 148 : hTdCngEnc->last_SID_bwidth = s_min( max_bwidth, SWB );
1709 148 : move16();
1710 148 : hTdCngEnc->last_vad = 0;
1711 148 : move16();
1712 148 : hTdCngEnc->last_idx_ener = 0;
1713 148 : move16();
1714 :
1715 148 : return;
1716 : }
1717 :
1718 : /*-------------------------------------------------------------------*
1719 : * dtx_enc_init_fx()
1720 : *
1721 : * Initialize DTX parameters
1722 : *-------------------------------------------------------------------*/
1723 :
1724 614 : void dtx_enc_init_fx(
1725 : Encoder_State *st, /* i : Encoder state handle */
1726 : const Word16 var_SID_rate_flag, /* i : flag for variable SID update rate Q0*/
1727 : const Word16 interval_SID /* i : interval for SID update Q0*/
1728 : )
1729 : {
1730 : DTX_ENC_HANDLE hDtxEnc;
1731 614 : hDtxEnc = st->hDtxEnc;
1732 :
1733 614 : hDtxEnc->first_CNG = 0;
1734 614 : move16();
1735 614 : hDtxEnc->cnt_SID = 0;
1736 614 : move16();
1737 614 : hDtxEnc->max_SID = 2;
1738 614 : move16();
1739 614 : hDtxEnc->CNG_mode = -1;
1740 614 : move16();
1741 614 : Copy( st->lsp_old1_fx, hDtxEnc->lspCNG_fx, M );
1742 614 : hDtxEnc->VarDTX_cnt_voiced = 0;
1743 614 : move16();
1744 614 : hDtxEnc->VarDTX_cnt_noise = 0;
1745 614 : move16();
1746 614 : hDtxEnc->lt_ener_voiced_fx = 0;
1747 614 : move32();
1748 614 : hDtxEnc->lt_ener_noise_fx = 0;
1749 614 : move32();
1750 614 : hDtxEnc->frame_ener_fx = 0;
1751 614 : move32();
1752 614 : hDtxEnc->lt_ener_last_SID_fx = 0;
1753 614 : move32();
1754 614 : hDtxEnc->last_CNG_L_frame = L_FRAME;
1755 614 : move16();
1756 614 : hDtxEnc->var_SID_rate_flag = var_SID_rate_flag; /*Q0*/
1757 614 : move16();
1758 614 : hDtxEnc->last_active_brate = ACELP_7k20;
1759 614 : move32();
1760 614 : hDtxEnc->cng_cnt = 0;
1761 614 : move16();
1762 :
1763 614 : IF( hDtxEnc->var_SID_rate_flag )
1764 : {
1765 3 : hDtxEnc->interval_SID = 12;
1766 3 : move16();
1767 3 : hDtxEnc->cng_hist_size = DTX_HIST_SIZE;
1768 3 : move16();
1769 : }
1770 : ELSE
1771 : {
1772 611 : hDtxEnc->interval_SID = interval_SID;
1773 611 : move16();
1774 611 : if ( GE_16( hDtxEnc->interval_SID, DTX_HIST_SIZE ) )
1775 : {
1776 611 : hDtxEnc->cng_hist_size = hDtxEnc->interval_SID;
1777 611 : move16();
1778 : }
1779 : }
1780 614 : return;
1781 : }
|