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