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"
7 : #include "cnst.h"
8 : //#include "prot_fx.h"
9 : #include "rom_com_fx.h"
10 : #include "rom_com.h" /* Common constants */
11 : #include "prot_fx.h" /* Function prototypes */
12 : #include "prot_fx_enc.h" /* Function prototypes */
13 : #include "basop_util.h" /* Function prototypes */
14 :
15 : /*-------------------------------------------------------------------*
16 : * Local functions
17 : *--------------------------------------------------------------------*/
18 : static void synthesis_filter_fx( Word16 b[], Word16 x[], Word16 y[], Word16 buf[], Word16 P, Word16 N );
19 : static Word32 DTFS_freq_corr_fx( DTFS_STRUCTURE X1_DTFS_fx, DTFS_STRUCTURE X2_DTFS_fx, Word16 lband, Word16 hband, Word16 *Qout );
20 : static Word16 DTFS_alignment_extract_td_fx( Word16 *x1, Word16 *x2, Word16 lag );
21 : static Word32 DTFS_getEngy_band_fx( DTFS_STRUCTURE X_fx, const Word16 lband, const Word16 hband );
22 :
23 : /*=======================================================================================*/
24 : /* FUNCTION : ppp_voiced_encoder_fx() */
25 : /*---------------------------------------------------------------------------------------*/
26 : /* PURPOSE : */
27 : /*---------------------------------------------------------------------------------------*/
28 : /* INPUT ARGUMENTS : */
29 : /* _ (Word16) delay_fx: open loop pitch, Q6 - WRONG, it is Q0 */
30 : /* _ (Word16) hSC_VBR->vadsnr_fx: SNR for current frame Q7 */
31 : /* _ (Word16) prevCW_lag_fx: Previous lag, Q0 */
32 : /* _ (Word16 *) in_fx : residual signal (Q_res) */
33 : /* _ (Word16 *) lpc1_fx : prev frame de-emphasized LPC Q12 */
34 : /* _ (Word16 *) lpc2_fx : current frame de-emphasized LPC Q12 */
35 : /* _ (Word16 *) exc_fx : prrevious frame quantized excitation (Q_exc) */
36 : /* _ (Word16) Q_res: Q factor for res */
37 : /* _ (Word16) Q_exc: Q factor for exc */
38 : /*---------------------------------------------------------------------------------------*/
39 : /* OUTPUT ARGUMENTS : */
40 : /* _ (Word16*) pitch_fx: floating pitch values for each subframe(Q6) */
41 : /* _ (Word16*) out_fx: Quantized residual signal (Q0) */
42 : /* _ Encoder_State *st_fx: */
43 : /* _ lastLgainE_fx - Q11 */
44 : /* _ lastHgainE_fx - Q11 */
45 : /* _ lasterbE_fx - Q13 */
46 : /*---------------------------------------------------------------------------------------*/
47 : /* INPUT/OUTPUT ARGUMENTS : */
48 : /* _ Encoder_State *st_fx: */
49 : /* _ hSC_VBR->dtfs_enc_xxxx */
50 : /* _ a nd b in hSC_VBR->dtfs_enc_Q */
51 : /* rest all in Q0 */
52 : /* - bump_up_fx - Q0 */
53 : /*---------------------------------------------------------------------------------------*/
54 : /* RETURN ARGUMENTS : */
55 : /* _ None. */
56 : /*---------------------------------------------------------------------------------------*/
57 : /* CALLED FROM : TX */
58 : /*=======================================================================================*/
59 :
60 0 : ivas_error ppp_voiced_encoder_fx(
61 : BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle */
62 : SC_VBR_ENC_HANDLE hSC_VBR,
63 : const Word16 bwidth_fx, /* i : audio bandwidth */
64 : const Word16 last_coder_type_raw, /* i : raw last_coder_type */
65 : const Word16 old_pitch_buf[], /* i : buffer of old subframe pitch values */
66 : Word16 *in_fx, /* i : residual signal */
67 : Word16 *out_fx, /* o : Quantized residual signal */
68 : Word16 delay_fx, /* i : open loop pitch */
69 : Word16 *lpc1_fx, /* i : prev frame de-emphasized LPC */
70 : Word16 *lpc2_fx, /* i : current frame de-emphasized LPC */
71 : Word16 *exc_fx, /* i: previous frame quantized excitation */
72 : Word16 *pitch_fx, /* o: floating pitch values for each subframe */
73 : Word16 Qres )
74 : {
75 : Word16 i;
76 0 : Word16 spike_near_edge = 0;
77 0 : move16();
78 : Word16 flag;
79 0 : Word16 delta_lag_E = 0, PPP_MODE_E, Q_delta_lag = 0;
80 0 : move16(); /*delta_lag_E*/
81 0 : move16(); /*Q_delta_lag*/
82 0 : Word16 out_of_bound = 0;
83 0 : move16();
84 : Word16 tmp, tmptmp, tmptmp1;
85 : Word16 pl, l;
86 : Word16 interp_delay[3], temp_pl, temp_l;
87 0 : Word16 upper_cut_off_freq_of_interest_fx = 0;
88 0 : move16(); /*upper_cut_off_freq_of_interest_fx*/
89 0 : Word16 upper_cut_off_freq_of_interest_norm_fx = 0, upper_cut_off_freq_norm_fx = 0;
90 0 : move16(); /*upper_cut_off_freq_of_interest_norm_fx*/
91 0 : move16(); /*upper_cut_off_freq_norm_fx*/
92 : Word16 S_fx[PIT_MAX * 4 + 1], C_fx[PIT_MAX * 4 + 1];
93 : Word16 Qtmpres;
94 : Word32 Ltemp, logLag, Ltemp_q;
95 : Word32 Lacc, Lacc1;
96 : Word16 Ql, Qh, n, sft, flag1;
97 :
98 : Word16 pf_temp1[MAXLAG_WI]; /*maynot need more than MAXLAG_WI/2+1 */
99 : Word16 pf_temp2[MAXLAG_WI];
100 : Word16 pf_temp[MAXLAG_WI];
101 : Word16 pf_n2[MAXLAG_WI];
102 : Word16 exp, expa, expb, fraca, fracb, scale, frac;
103 : Word32 L_tmp;
104 :
105 : Word16 x_fx;
106 : Word16 impzi_fx[160], impzo_fx[160];
107 : Word16 exp_ee, frac_ee;
108 : Word16 Qtmp;
109 0 : Word32 res_enratio_fx = 0;
110 0 : move32();
111 : Word16 mem_fx[10];
112 0 : Word32 energy_impz_fx = 0, tmpres_fx;
113 0 : move32(); /*energy_impz_fx*/
114 : Word32 pos_nq0_fx, neg_nq0_fx, Ltmp;
115 : Word32 Ltmp_32, Ltmp1_32, Ltemp1, Ltemp2, Ltemp_fx;
116 : Word16 Qadj;
117 :
118 0 : Word32 tmp_fx = 0, sp_hb_enratio_fx = 0, sp_enratio_fx = 0;
119 0 : move32(); /*tmp_fx*/
120 0 : move32(); /*sp_hb_enratio_fx*/
121 0 : move32(); /*sp_enratio_fx*/
122 : Word32 low_band_en_fx;
123 : Word32 curr_Engy, prev_Engy;
124 : Word16 temp_Fs;
125 : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
126 0 : Flag Overflow = 0;
127 0 : move32();
128 : #endif
129 : DTFS_STRUCTURE *CURRP_NQ_FX;
130 : DTFS_STRUCTURE *TMPDTFS_FX;
131 : DTFS_STRUCTURE *TMPDTFS2_FX;
132 : DTFS_STRUCTURE *TMPDTFS3_FX;
133 : DTFS_STRUCTURE *CURRP_Q_E_FX;
134 : DTFS_STRUCTURE *dtfs_temp_fx;
135 :
136 : ivas_error error;
137 :
138 0 : error = IVAS_ERR_OK;
139 :
140 :
141 0 : IF( ( error = DTFS_new_fx( &CURRP_NQ_FX ) ) != IVAS_ERR_OK )
142 : {
143 0 : IVAS_ERROR( error, "Error creating DTFS structure" );
144 : }
145 0 : IF( ( error = DTFS_new_fx( &TMPDTFS_FX ) ) != IVAS_ERR_OK )
146 : {
147 0 : IVAS_ERROR( error, "Error creating DTFS structure" );
148 : }
149 0 : IF( ( error = DTFS_new_fx( &TMPDTFS2_FX ) ) != IVAS_ERR_OK )
150 : {
151 0 : IVAS_ERROR( error, "Error creating DTFS structure" );
152 : }
153 0 : IF( ( error = DTFS_new_fx( &TMPDTFS3_FX ) ) != IVAS_ERR_OK )
154 : {
155 0 : IVAS_ERROR( error, "Error creating DTFS structure" );
156 : }
157 0 : IF( ( error = DTFS_new_fx( &CURRP_Q_E_FX ) ) != IVAS_ERR_OK )
158 : {
159 0 : IVAS_ERROR( error, "Error creating DTFS structure" );
160 : }
161 0 : IF( ( error = DTFS_new_fx( &dtfs_temp_fx ) ) != IVAS_ERR_OK )
162 : {
163 0 : IVAS_ERROR( error, "Error creating DTFS structure" );
164 : }
165 :
166 :
167 0 : temp_Fs = 8000;
168 0 : move16();
169 :
170 0 : if ( EQ_16( bwidth_fx, WB ) )
171 : {
172 0 : temp_Fs = 16000;
173 0 : move16();
174 : }
175 :
176 0 : test();
177 0 : IF( EQ_16( bwidth_fx, WB ) || EQ_16( bwidth_fx, SWB ) )
178 : {
179 0 : upper_cut_off_freq_of_interest_fx = 4000;
180 0 : move16();
181 0 : upper_cut_off_freq_of_interest_norm_fx = 10240;
182 0 : move16(); /*value normalized by 12800 */
183 0 : upper_cut_off_freq_norm_fx = 16384;
184 0 : move16(); /*value normalized by 12800 */
185 : }
186 0 : ELSE IF( bwidth_fx == NB )
187 : {
188 0 : upper_cut_off_freq_of_interest_fx = 3300;
189 0 : move16();
190 0 : upper_cut_off_freq_of_interest_norm_fx = 8448;
191 0 : move16(); /*value normalized by 12800 */
192 0 : upper_cut_off_freq_norm_fx = 10240;
193 0 : move16(); /*value normalized by 12800 */
194 : }
195 :
196 : /* Initialization */
197 0 : IF( hSC_VBR->firstTime_voicedenc )
198 : {
199 0 : hSC_VBR->firstTime_voicedenc = 0;
200 0 : move16();
201 0 : hSC_VBR->dtfs_enc_lag = 0;
202 0 : move16();
203 0 : hSC_VBR->dtfs_enc_nH = 0;
204 0 : move16();
205 0 : hSC_VBR->dtfs_enc_nH_4kHz = 0;
206 0 : move16();
207 0 : hSC_VBR->dtfs_enc_upper_cut_off_freq_of_interest_fx = 3300;
208 0 : move16();
209 0 : hSC_VBR->dtfs_enc_upper_cut_off_freq_fx = 4000;
210 0 : move16();
211 0 : set16_fx( hSC_VBR->dtfs_enc_a_fx, 0, MAXLAG_WI );
212 0 : set16_fx( hSC_VBR->dtfs_enc_b_fx, 0, MAXLAG_WI );
213 : }
214 0 : test();
215 : /* Figure out the PPP_MODE */
216 0 : IF( EQ_16( hSC_VBR->last_ppp_mode, 1 ) && !hSC_VBR->mode_QQF )
217 : {
218 0 : hSC_VBR->bump_up = 1;
219 0 : move16();
220 0 : free( CURRP_NQ_FX );
221 0 : free( TMPDTFS_FX );
222 0 : free( TMPDTFS2_FX );
223 0 : free( TMPDTFS3_FX );
224 0 : free( CURRP_Q_E_FX );
225 0 : free( dtfs_temp_fx );
226 0 : return error;
227 : }
228 :
229 : /* Use the aggresive bumpups if there are two consecutive Q frames */
230 : /* Aggresive bump upsare only used in the second Q frame */
231 0 : if ( hSC_VBR->last_ppp_mode == 1 )
232 : {
233 0 : hSC_VBR->rate_control = 0;
234 0 : move16();
235 : }
236 :
237 0 : PPP_MODE_E = 'Q';
238 0 : move16();
239 :
240 0 : pl = s_min( MAX_LAG_PIT, rint_new_fx( L_shl( old_pitch_buf[( 2 * NB_SUBFR ) - 1], 10 ) ) );
241 0 : l = s_min( MAX_LAG_PIT, rint_new_fx( L_deposit_h( delay_fx ) ) );
242 : /* old_pitch_buf in Q6*/
243 :
244 : /* Bump up if the lag is out_fx of range */
245 0 : test();
246 0 : test();
247 0 : test();
248 0 : IF( GT_16( sub( l, pl ), 13 ) || LT_16( sub( l, pl ), -11 ) || LT_16( l, 19 ) || LT_16( pl, 19 ) )
249 : {
250 0 : hSC_VBR->bump_up = 1;
251 0 : move16();
252 0 : free( CURRP_NQ_FX );
253 0 : free( TMPDTFS_FX );
254 0 : free( TMPDTFS2_FX );
255 0 : free( TMPDTFS3_FX );
256 0 : free( CURRP_Q_E_FX );
257 0 : free( dtfs_temp_fx );
258 0 : return error;
259 : }
260 :
261 0 : IF( NE_16( hSC_VBR->last_ppp_mode, 1 ) )
262 : {
263 : /* Obtain DTFS of last pl values of past excitation */
264 0 : GetSinCosTab_fx( pl, S_fx, C_fx );
265 0 : DTFS_to_fs_fx( exc_fx - pl, pl, dtfs_temp_fx, temp_Fs, 0, S_fx, C_fx );
266 : }
267 :
268 0 : if ( EQ_16( last_coder_type_raw, UNVOICED ) )
269 : {
270 0 : pl = l;
271 0 : move16(); /* if prev frame was sil/uv */
272 : }
273 :
274 : /* Use the out_fx array as a temp storage for currp */
275 0 : spike_near_edge = ppp_extract_pitch_period_fx( in_fx, out_fx, l, &out_of_bound, Qres ); /*Q0*/
276 0 : move16();
277 : /* out_fx in Qres */
278 :
279 0 : IF( out_of_bound == 1 )
280 : {
281 0 : hSC_VBR->bump_up = 1;
282 0 : move16();
283 0 : free( CURRP_NQ_FX );
284 0 : free( TMPDTFS_FX );
285 0 : free( TMPDTFS2_FX );
286 0 : free( TMPDTFS3_FX );
287 0 : free( CURRP_Q_E_FX );
288 0 : free( dtfs_temp_fx );
289 0 : return error;
290 : }
291 :
292 : /* Get DTFS of current prototype */
293 0 : GetSinCosTab_fx( l, S_fx, C_fx );
294 :
295 0 : DTFS_to_fs_fx( out_fx, l, CURRP_NQ_FX, temp_Fs, 0, S_fx, C_fx );
296 : /* This requires input out_fx in Q0, but currently in Qres */
297 :
298 : /* Ensure the extracted prototype is time-synchronous to the
299 : * last l samples of the frame. This proves to eliminate
300 : * some of the PPP-CELP transition problems.
301 : * Convert last samples into DTFS */
302 0 : IF( spike_near_edge != 0 )
303 : {
304 : /* These two function calls are combined in one call DTFS_alignment_extract_td_fx() */
305 : /* DTFS_to_fs(in+L_FRAME-l, l, TMPDTFS,(short) st->input_Fs,0); */
306 : /* tmp = DTFS_alignment_extract(*TMPDTFS, *CURRP_NQ, 0.0, lpc2) ; */
307 0 : tmp = DTFS_alignment_extract_td_fx( out_fx, in_fx + L_FRAME - l, l );
308 0 : move16(); /*Q0 */
309 0 : tmp = negate( shl( tmp, 2 ) ); /*Q2 */
310 0 : Q2phaseShift_fx( CURRP_NQ_FX, tmp, l, S_fx, C_fx );
311 : /* output CURRP_NQ is correct */
312 : }
313 0 : temp_pl = pl;
314 0 : move16();
315 0 : temp_l = l;
316 0 : move16();
317 :
318 0 : FOR( i = 0; i < NB_SUBFR; i++ )
319 : {
320 : /* do the linear pitch_fx interp to drive the nb_post_filt_fx */
321 0 : Interpol_delay_fx( interp_delay, temp_pl, temp_l, i, frac_4sf_fx ); /* interp_delay in Q4 */
322 0 : pitch_fx[i] = shl( interp_delay[0], 2 );
323 0 : move16(); /* pitch_fx in Q6 */
324 : }
325 0 : curr_Engy = DTFS_getEngy_P2A_fx( CURRP_NQ_FX ); /*2Q where Q=CURRP_NQ_FX->Q */
326 0 : move32();
327 : /* Restoring PPP memories when the last frame is non-PPP */
328 0 : IF( NE_16( hSC_VBR->last_ppp_mode, 1 ) )
329 : {
330 :
331 0 : hSC_VBR->ph_offset_E_fx = 0;
332 0 : move16();
333 :
334 : /* st->prev_cw_en = DTFS_getEngy(*dtfs_temp); */
335 0 : Lacc = DTFS_getEngy_P2A_fx( dtfs_temp_fx ); /*2Q where Q = dtfs_temp_fx->Q */
336 :
337 :
338 0 : prev_Engy = L_add( Lacc, 0 );
339 :
340 0 : hSC_VBR->Q_prev_cw_en_fx = norm_l( Lacc ); /* = K = headroom */
341 0 : move16();
342 : /*hSC_VBR->Q_prev_cw_en_fx = (Lacc==0)?31: hSC_VBR->Q_prev_cw_en_fx; */
343 0 : if ( Lacc == 0 )
344 : {
345 0 : hSC_VBR->Q_prev_cw_en_fx = 31;
346 0 : move16();
347 : }
348 :
349 0 : hSC_VBR->prev_cw_en_fx = (Word32) L_shl( Lacc, hSC_VBR->Q_prev_cw_en_fx ); /*2Q+K */
350 0 : move32();
351 0 : hSC_VBR->Q_prev_cw_en_fx = add( hSC_VBR->Q_prev_cw_en_fx, shl( dtfs_temp_fx->Q, 1 ) );
352 0 : move16();
353 : /* hSC_VBR->Q_prev_cw_en_fx = 2*(dtfs_temp_fx->Q) + K */
354 :
355 0 : DTFS_copy_fx( TMPDTFS_FX, *dtfs_temp_fx ); /* output = TMPDTFS_FX */
356 :
357 0 : DTFS_car2pol_fx( TMPDTFS_FX );
358 :
359 0 : logLag = log10_fx( TMPDTFS_FX->lag_fx ); /* logLag=10*log10(pl), Q23 */
360 0 : Ltemp_q = L_shl( L_mult( shl( TMPDTFS_FX->Q, 1 ), 24660 ), 9 ); /* Ltemp_q=2Q*10log10(2), Q23 */
361 :
362 : /* Process low band */
363 0 : Ltemp = DTFS_setEngyHarm_fx( 236, 2828, 0, 2828, 1, 0, &Ql, TMPDTFS_FX ); /* Q of Ltemp = 2*(TMPDTFS_FX->Q) = Ql ? */
364 0 : move32();
365 : /* Compensate for Q factor of energy to get log10(lag*eng) */
366 0 : Ltemp = log10_fx( Ltemp ); /* Ltemp=10log10(eng), Q23 */
367 0 : Ltemp = L_add( L_sub( Ltemp, Ltemp_q ), logLag ); /* Ltemp=10*log10(lag*eng), Q23 */
368 : /*hSC_VBR->lastLgainE_fx=round_fx(L_shl((Word32)Mpy_32_16(extract_h(Ltemp),extract_l(Ltemp),0x6666),1)); // Q11, 0x6666 = 0.1 in Q18 */
369 0 : hSC_VBR->lastLgainE_fx = round_fx( L_shl( Mult_32_16( Ltemp, 0x6666 ), 1 ) ); /* Q11, 0x6666 = 0.1 in Q18 */
370 0 : move16();
371 :
372 : /* Process high band */
373 0 : Ltemp = DTFS_setEngyHarm_fx( 2828, upper_cut_off_freq_of_interest_norm_fx, 2828, upper_cut_off_freq_norm_fx, 1, 0, &Qh, TMPDTFS_FX );
374 0 : move32();
375 0 : Ltemp = log10_fx( Ltemp );
376 0 : Ltemp = L_add( L_sub( Ltemp, Ltemp_q ), logLag ); /* Ltemp=10*log10(lag*eng), Q23 */
377 0 : hSC_VBR->lastHgainE_fx = round_fx( L_shl( Mult_32_16( Ltemp, 0x6666 ), 1 ) ); /* Q11 */
378 0 : move16();
379 : /* Need to unify the Q factors of both bands */
380 0 : TMPDTFS_FX->Q = s_min( Ql, Qh );
381 0 : move16(); /* set Q factor to be the smaller one of Ql and Qh */
382 0 : n = sub( Ql, Qh ); /* compare band Q factors */
383 :
384 0 : IF( n < 0 )
385 : {
386 0 : rshiftHarmBand_fx( TMPDTFS_FX, 2828, upper_cut_off_freq_norm_fx, n );
387 : }
388 0 : ELSE IF( n > 0 )
389 : {
390 0 : rshiftHarmBand_fx( TMPDTFS_FX, 0, 2828, sub( Qh, Ql ) );
391 : }
392 :
393 0 : DTFS_to_erb_fx( *TMPDTFS_FX, hSC_VBR->lasterbE_fx ); /* output lasterbE_fx in Q13 */
394 0 : Lacc1 = L_max( prev_Engy, 1 );
395 : }
396 : ELSE
397 : {
398 : /* Copy DTFS related parameters from 'st_fx' to 'dtfs_temp' structure */
399 0 : dtfs_temp_fx->lag_fx = hSC_VBR->dtfs_enc_lag;
400 0 : move16(); /*Q0*/
401 0 : dtfs_temp_fx->nH_fx = hSC_VBR->dtfs_enc_nH;
402 0 : move16(); /*Q0*/
403 0 : dtfs_temp_fx->nH_4kHz_fx = hSC_VBR->dtfs_enc_nH_4kHz;
404 0 : move16(); /*Q0*/
405 0 : dtfs_temp_fx->upper_cut_off_freq_of_interest_fx = hSC_VBR->dtfs_enc_upper_cut_off_freq_of_interest_fx;
406 0 : move16();
407 0 : dtfs_temp_fx->upper_cut_off_freq_fx = hSC_VBR->dtfs_enc_upper_cut_off_freq_fx;
408 0 : move16();
409 :
410 0 : Copy( hSC_VBR->dtfs_enc_a_fx, dtfs_temp_fx->a_fx, MAXLAG_WI );
411 0 : Copy( hSC_VBR->dtfs_enc_b_fx, dtfs_temp_fx->b_fx, MAXLAG_WI );
412 :
413 0 : dtfs_temp_fx->Q = hSC_VBR->dtfs_enc_Q;
414 0 : move16();
415 0 : Lacc1 = DTFS_getEngy_P2A_fx( dtfs_temp_fx );
416 0 : move32();
417 0 : prev_Engy = L_add( Lacc1, 0 );
418 : }
419 :
420 : /*-----Open-loop Bump-Up-------- */
421 :
422 : /* Energy ratio calculation in_fx residual and speech domain */
423 : /* Also, compute correlation between the previous and the */
424 : /* current prototype */
425 :
426 : /* res_enratio = DTFS_getEngy(*CURRP_NQ) / DTFS_getEngy(*dtfs_temp); */
427 0 : Lacc = L_add( curr_Engy, 0 );
428 : /* Lacc1 has been handled above */
429 :
430 0 : sft = add( shl( sub( CURRP_NQ_FX->Q, dtfs_temp_fx->Q ), 1 ), 4 );
431 0 : IF( sft > 0 )
432 : {
433 0 : if ( LT_32( Lacc1, L_shr( Lacc, sft ) ) )
434 : {
435 0 : res_enratio_fx = 0x7FFF;
436 0 : move16();
437 : }
438 : }
439 : ELSE
440 : {
441 0 : if ( LT_32( L_shr( Lacc1, negate( sft ) ), Lacc ) )
442 : {
443 0 : res_enratio_fx = 0x7FFF;
444 0 : move16();
445 : }
446 : }
447 : /* max value res_enratio compared against is 0x7400 (14.5 in Q11) */
448 :
449 0 : IF( NE_32( res_enratio_fx, 0x7FFF ) )
450 : {
451 :
452 0 : expb = norm_l( Lacc );
453 0 : fracb = extract_h( L_shl( Lacc, expb ) );
454 0 : expb = sub( 30, add( expb, shl( CURRP_NQ_FX->Q, 1 ) ) );
455 :
456 :
457 0 : expa = norm_l( Lacc1 );
458 0 : fraca = extract_h( L_shl( Lacc1, expa ) );
459 0 : expa = sub( 30, add( expa, shl( dtfs_temp_fx->Q, 1 ) ) );
460 :
461 0 : scale = shr( sub( fraca, fracb ), 15 );
462 0 : fracb = shl( fracb, scale );
463 0 : expb = sub( expb, scale );
464 :
465 0 : tmp = div_s( fracb, fraca );
466 0 : exp = sub( expb, expa );
467 0 : res_enratio_fx = shl( tmp, sub( exp, 4 ) );
468 : }
469 : /* res_enratio_fx is Q11 */
470 :
471 : /* Copy over CURRP_NQ into TMPDTFS */
472 0 : DTFS_copy_fx( TMPDTFS_FX, *CURRP_NQ_FX ); /* output = TMPDTFS_FX with Q = CURRP_NQ_FX->Q */
473 :
474 : /* Copy over dtfs_temp into TMPDTFS2 */
475 0 : DTFS_copy_fx( TMPDTFS2_FX, *dtfs_temp_fx ); /* output = TMPDTFS2_FX with Q = dtfs_temp_fx->Q */
476 :
477 0 : tmptmp = DTFS_alignment_full_fx( *TMPDTFS2_FX, *TMPDTFS_FX, hSC_VBR->ph_offset_E_fx, S_fx, C_fx, 0 );
478 0 : move16();
479 0 : tmptmp1 = sub( shl( TMPDTFS_FX->lag_fx, 1 ), tmptmp ); /* (C_l-tmptmp) , Q1 */
480 :
481 0 : Q2phaseShift_fx( TMPDTFS_FX, negate( shl( tmptmp1, 1 ) ), TMPDTFS_FX->lag_fx, S_fx, C_fx ); /* fixed bug , phase shift by tmp computed in_fx TMP.lag domain (above) */
482 :
483 : /*tmpres = (float)(DTFS_freq_corr(*TMPDTFS, *TMPDTFS2, 100.0f, 3700.0f));*/
484 0 : tmpres_fx = DTFS_freq_corr_fx( *TMPDTFS_FX, *TMPDTFS2_FX, 100, 3700, &Qtmpres ); /* tmpres_fx has Q factor tmpres */
485 0 : move32();
486 :
487 0 : poleFilter_setup_fx( lpc2_fx, M + 1, *TMPDTFS_FX, S_fx, C_fx, pf_temp1, pf_temp2, pf_temp, pf_n2 );
488 0 : DTFS_poleFilter_fx_9( TMPDTFS_FX, pf_temp1, pf_temp2, pf_temp, pf_n2 );
489 : /* lpc2_fx in Q12 */
490 :
491 0 : DTFS_adjustLag_fx( TMPDTFS2_FX, TMPDTFS_FX->lag_fx ); /* operate in_fx CL domain */
492 :
493 0 : DTFS_poleFilter_fx( TMPDTFS2_FX, lpc1_fx, M + 1, S_fx, C_fx ); /* lpc1_fx in Q12 */
494 :
495 0 : tmp_fx = DTFS_freq_corr_fx( *TMPDTFS_FX, *TMPDTFS2_FX, 100, 3700, &Qtmp );
496 0 : move32();
497 : /* tmp_fx Q = Qtmp */
498 :
499 :
500 : /*******************************************************************************
501 : if ( DTFS_getEngy(*TMPDTFS2) > 0 )
502 : {
503 : sp_enratio = DTFS_getEngy(*TMPDTFS)/DTFS_getEngy(*TMPDTFS2);
504 : }
505 : else
506 : {
507 : sp_enratio = 0.0f;
508 : }
509 : *******************************************************************************/
510 0 : Ltmp_32 = ( DTFS_getEngy_fx( TMPDTFS2_FX ) ); /* Output Q=2*(TMPDTFS2_FX->Q) */
511 0 : move32();
512 0 : IF( Ltmp_32 > 0 )
513 : {
514 : /*sp_enratio = DTFS_getEngy(*TMPDTFS)/DTFS_getEngy(*TMPDTFS2); in Q15 */
515 0 : Lacc = DTFS_getEngy_P2A_fx( TMPDTFS_FX );
516 0 : move32();
517 0 : Lacc1 = Ltmp_32;
518 0 : move32();
519 : /* IF (L_sub(sp_enratio_fx,0x7FFF)!=0) */
520 : {
521 0 : expb = norm_l( Lacc );
522 0 : fracb = extract_h( L_shl( Lacc, expb ) );
523 :
524 0 : expb = sub( 30, add( expb, shl( TMPDTFS_FX->Q, 1 ) ) );
525 :
526 0 : expa = norm_l( Lacc1 );
527 0 : fraca = extract_h( L_shl( Lacc1, expa ) );
528 :
529 0 : expa = sub( 30, add( expa, shl( TMPDTFS2_FX->Q, 1 ) ) );
530 :
531 0 : scale = shr( sub( fraca, fracb ), 15 );
532 0 : fracb = shl( fracb, scale );
533 0 : expb = sub( expb, scale );
534 :
535 0 : tmp = div_s( fracb, fraca );
536 0 : exp = sub( expb, expa );
537 0 : sp_enratio_fx = L_shl_sat( tmp, exp ); /* Q15 */
538 : }
539 : }
540 : ELSE
541 : {
542 0 : sp_enratio_fx = L_deposit_l( 0 ); /* Q15 */
543 : }
544 :
545 : /*******************************************************************************/
546 0 : IF( EQ_16( PPP_MODE_E, 'Q' ) )
547 : {
548 : /* Bump up if the lag is out_fx of range */
549 0 : test();
550 0 : IF( GT_16( sub( l, pl ), 13 ) || LT_16( sub( l, pl ), -11 ) )
551 : {
552 0 : PPP_MODE_E = 'B';
553 0 : move16();
554 : }
555 : ELSE
556 : {
557 0 : delta_lag_E = sub( l, pl );
558 : }
559 :
560 : /* Bump up if big change between the previous and the current CWs */
561 0 : IF( LT_16( shl_o( hSC_VBR->vadsnr_fx, 1, &Overflow ), hSC_VBR->SNR_THLD_fx ) ) /*Q8 */
562 : {
563 : /*if ( res_enratio > 5.0 && tmp < 0.65 ) */
564 : /* 5 in Q11, 0.65 in Q15 // L_shl(tmp_fx,sub(31,Qtmp)) makes tmp_fx FIXED Q31 */
565 0 : test();
566 0 : if ( ( GT_32( res_enratio_fx, 10240 ) ) && ( LT_16( extract_h( L_shl_o( tmp_fx, sub( 31, Qtmp ), &Overflow ) ), 21299 ) ) )
567 : {
568 0 : PPP_MODE_E = 'B';
569 0 : move16();
570 : }
571 : }
572 : ELSE
573 : {
574 : /* if ( res_enratio > 3.0 && tmp < 1.2 ) */
575 : /*3 in Q11, 1.2 in Q14 // L_shl(tmp_fx,sub(31,Qtmp)) makes tmp_fx FIXED Q14 */
576 0 : test();
577 0 : if ( ( GT_32( res_enratio_fx, 6144 ) ) && ( LT_16( extract_h( L_shl_o( tmp_fx, sub( 30, Qtmp ), &Overflow ) ), 19661 ) ) )
578 : {
579 0 : PPP_MODE_E = 'B';
580 0 : move16();
581 : }
582 : }
583 : }
584 :
585 : /* Rapid rampdown frame where time resolution is important */
586 : /* Not a suitable PPP frame -> Bump to CELP */
587 :
588 0 : IF( LT_16( shl_o( hSC_VBR->vadsnr_fx, 1, &Overflow ), hSC_VBR->SNR_THLD_fx ) ) /*Q8 */
589 : {
590 : /* if (res_enratio < 0.025) */
591 0 : IF( LT_32( L_shl_o( res_enratio_fx, 4, &Overflow ), 819 ) ) /*0x0333 = 0.025 in Q15, res_enratio_fx in Q15 after shl 4 */
592 : {
593 0 : hSC_VBR->bump_up = 1;
594 0 : move16();
595 0 : free( CURRP_NQ_FX );
596 0 : free( TMPDTFS_FX );
597 0 : free( TMPDTFS2_FX );
598 0 : free( TMPDTFS3_FX );
599 0 : free( CURRP_Q_E_FX );
600 0 : free( dtfs_temp_fx );
601 0 : return error;
602 : }
603 : }
604 : ELSE
605 : {
606 : /* if ( res_enratio < 0.092f) */
607 0 : if ( LT_32( L_shl_o( res_enratio_fx, 4, &Overflow ), 3015 ) ) /*3015 = 0.092 in Q15, res_enratio_fx in Q15 after shl 4 */
608 : {
609 0 : hSC_VBR->bump_up = 1;
610 0 : move16();
611 : }
612 : }
613 :
614 : /* if (min(res_enratio, sp_enratio) < 0.075 && tmp < -0.5f)) : 2458 = 0.075 in Q15 */
615 0 : test();
616 0 : if ( LT_32( L_min( L_shl_o( res_enratio_fx, 4, &Overflow ), sp_enratio_fx ), 2458 ) && LT_32( tmp_fx, shl_o( -1, sub( Qtmp, 1 ), &Overflow ) ) )
617 : {
618 0 : hSC_VBR->bump_up = 1;
619 0 : move16();
620 : }
621 :
622 : /* Rapid rampup frame where time resolution is important */
623 : /* Not a suitable PPP frame -> Bump to CELP */
624 0 : IF( LT_16( shl_o( hSC_VBR->vadsnr_fx, 1, &Overflow ), hSC_VBR->SNR_THLD_fx ) ) /*Q8 */
625 : {
626 0 : IF( GT_32( res_enratio_fx, 29696 ) ) /*14.5 in Q11 */
627 : {
628 0 : hSC_VBR->bump_up = 1;
629 0 : move16();
630 0 : free( CURRP_NQ_FX );
631 0 : free( TMPDTFS_FX );
632 0 : free( TMPDTFS2_FX );
633 0 : free( TMPDTFS3_FX );
634 0 : free( CURRP_Q_E_FX );
635 0 : free( dtfs_temp_fx );
636 0 : return error;
637 : }
638 : }
639 : ELSE
640 : {
641 0 : if ( GT_32( res_enratio_fx, 14336 ) ) /* 7.0 in Q11 */
642 : {
643 0 : hSC_VBR->bump_up = 1;
644 0 : move16();
645 : }
646 : }
647 :
648 0 : IF( EQ_16( hSC_VBR->bump_up, 1 ) )
649 : {
650 0 : free( CURRP_NQ_FX );
651 0 : free( TMPDTFS_FX );
652 0 : free( TMPDTFS2_FX );
653 0 : free( TMPDTFS3_FX );
654 0 : free( CURRP_Q_E_FX );
655 0 : free( dtfs_temp_fx );
656 0 : return error;
657 : }
658 :
659 : /* Bump up when the previous frame is an unvoiced or a silent frame */
660 0 : IF( EQ_16( last_coder_type_raw, UNVOICED ) )
661 : {
662 0 : hSC_VBR->bump_up = 1;
663 0 : move16();
664 0 : free( CURRP_NQ_FX );
665 0 : free( TMPDTFS_FX );
666 0 : free( TMPDTFS2_FX );
667 0 : free( TMPDTFS3_FX );
668 0 : free( CURRP_Q_E_FX );
669 0 : free( dtfs_temp_fx );
670 0 : return error;
671 : }
672 : /* -----End Open-loop Bump-Up */
673 :
674 : /* PPP-WI Quantization */
675 0 : IF( EQ_16( PPP_MODE_E, 'Q' ) )
676 : {
677 0 : flag = 1;
678 0 : move16();
679 0 : IF( EQ_16( PPP_MODE_E, 'Q' ) )
680 : {
681 0 : IF( ( error = ppp_quarter_encoder_fx( &flag, CURRP_Q_E_FX, TMPDTFS_FX, dtfs_temp_fx->lag_fx, *CURRP_NQ_FX, lpc2_fx, &hSC_VBR->lastLgainE_fx, &hSC_VBR->lastHgainE_fx, hSC_VBR->lasterbE_fx, *dtfs_temp_fx, S_fx, C_fx, hBstr ) ) != IVAS_ERR_OK )
682 : {
683 0 : free( CURRP_NQ_FX );
684 0 : free( TMPDTFS_FX );
685 0 : free( TMPDTFS2_FX );
686 0 : free( TMPDTFS3_FX );
687 0 : free( CURRP_Q_E_FX );
688 0 : free( dtfs_temp_fx );
689 0 : return error;
690 : }
691 : }
692 :
693 0 : IF( flag )
694 : {
695 : /* TMPDTFS : Target prototype: Amp Quantized + Phase Unquantized */
696 : /* TMPDTFS2: Quantized prototype: Amp Quantized + Phase Quantized */
697 : /* TMPDTFS3: Delta prototype: Diff betw. target and quant. in_fx speech dom */
698 :
699 : /* ----- Closed-loop Bump-Up ---------- */
700 : Word32 pos_nq_fx, neg_nq_fx, pos_q_fx, neg_q_fx;
701 : Word16 Qposnq, Qnegnq, Qposq, Qnegq;
702 :
703 0 : DTFS_peaktoaverage_fx( *TMPDTFS_FX, &pos_nq_fx, &Qposnq, &neg_nq_fx, &Qnegnq );
704 0 : DTFS_peaktoaverage_fx( *CURRP_Q_E_FX, &pos_q_fx, &Qposq, &neg_q_fx, &Qnegq );
705 :
706 :
707 : /* Before we perform the peak-to-average ratio comparison, we have to */
708 : /* ensure that the energy is not decaying and also the pitch_fx pulse */
709 : /* is clearly defined */
710 :
711 : /* Usually triggers in the slow ramp down frames. Does not fall under the test condition (res_enratio < 0.025) as
712 : both frames have little energy and the ratio is not very small. Not suitable for PPP */
713 :
714 0 : IF( GT_16( CURRP_Q_E_FX->upper_cut_off_freq_fx, 4000 ) )
715 : {
716 0 : Ltemp2 = DTFS_getEngy_band_wb_fx( *CURRP_Q_E_FX, 0, 2000 );
717 0 : move32();
718 : /* Use this bump-up only for WB signals */
719 0 : IF( Ltemp2 > 0 )
720 : {
721 : /* sp_hb_enratio = DTFS_getEngy_band_wb(*CURRP_Q_E, 2000.0, 6400.0)/DTFS_getEngy_band_wb(*CURRP_Q_E, 0.0, 2000.0); */
722 0 : Ltemp1 = DTFS_getEngy_band_wb_fx( *CURRP_Q_E_FX, 2000, 6400 ); /*Q13 */
723 0 : move32();
724 : /*sp_hb_enratio_fx = divide_dp(Ltemp1,Ltemp2,0, 1);//Q29 */
725 0 : Qadj = 0;
726 0 : move16();
727 : /*----------------------------------------------------------*/
728 : /* Ltemp_fx = (Word32)divide_dp(Ltemp1, Ltemp2, Qadj,1); Q29*/
729 : /*----------------------------------------------------------*/
730 0 : if ( Ltemp1 < 0 )
731 : {
732 0 : Ltemp1 = L_negate( Ltemp1 );
733 : }
734 0 : expa = norm_l( Ltemp2 );
735 0 : fraca = extract_h( L_shl_o( Ltemp2, expa, &Overflow ) );
736 0 : expa = sub( 30, expa );
737 :
738 0 : expb = norm_l( Ltemp1 );
739 0 : fracb = round_fx( L_shl( Ltemp1, expb ) );
740 0 : expb = sub( 30, add( expb, Qadj ) );
741 :
742 0 : scale = shr( sub( fraca, fracb ), 15 );
743 0 : fracb = shl_o( fracb, scale, &Overflow );
744 0 : expb = sub( expb, scale );
745 :
746 0 : tmp = div_s( fracb, fraca );
747 0 : exp = sub( expb, expa );
748 0 : Ltemp_fx = L_shl_o( tmp, add( exp, 14 ), &Overflow );
749 : /*-------------------------------------------*/
750 0 : sp_hb_enratio_fx = L_add( Ltemp_fx, 0 ); /* Q29 */
751 : }
752 : ELSE
753 : {
754 0 : sp_hb_enratio_fx = L_deposit_l( 0 );
755 : }
756 0 : low_band_en_fx = Ltemp2; /*Q13 */
757 0 : move32();
758 : /* if ( low_band_en < 25.0f && sp_hb_enratio < 1.6f ) */
759 : /* 25.0 in Q13 = 204800, 1.6 in Q29 = 858993459 */
760 0 : test();
761 0 : if ( LT_32( low_band_en_fx, 204800 ) && LT_32( sp_hb_enratio_fx, 858993459 ) )
762 : {
763 0 : PPP_MODE_E = 'B';
764 0 : move16();
765 : }
766 : }
767 :
768 0 : Ltmp_32 = DTFS_getEngy_fx( CURRP_NQ_FX ); /*Q = 2*(CURRP_NQ_FX->Q) */
769 0 : move32();
770 0 : Qadj = sub( hSC_VBR->Q_prev_cw_en_fx, shl( CURRP_NQ_FX->Q, 1 ) );
771 :
772 0 : Ltmp_32 = L_shl_o( Ltmp_32, Qadj, &Overflow ); /* shift left required to adjust Q of CURRP_NQ_FX = Q_prev_cw_en_fx */
773 : /* Ltmp1_32 = 0.8f * st->prev_cw_en */
774 0 : Ltmp1_32 = Mult_32_16( hSC_VBR->prev_cw_en_fx, 26214 ); /* Q = (Q_prev_cw_en_fx + Q15+1)-Q16 = Q_prev_cw_en_fx */
775 :
776 0 : IF( LT_16( shl_o( hSC_VBR->vadsnr_fx, 1, &Overflow ), hSC_VBR->SNR_THLD_fx ) ) /*Q8 */
777 : {
778 : /* if ( DTFS_getEngy(*CURRP_NQ) > 0.8f * st->prev_cw_en && max(pos_nq, neg_nq) > 3.0f && st->rate_control ) */
779 : /* pos_nq_fx and neg_nq_fx in Q28 ???? */
780 0 : test();
781 0 : test();
782 0 : IF( GT_32( Ltmp_32, Ltmp1_32 ) && GT_32( L_max( pos_nq_fx, neg_nq_fx ), 805306368 ) && hSC_VBR->rate_control )
783 : {
784 : /*if ( pos_nq > neg_nq && pos_nq > 2.0f * pos_q ) */
785 0 : test();
786 0 : if ( GT_32( pos_nq_fx, neg_nq_fx ) && GT_32( Mult_32_16( pos_nq_fx, 16384 ), pos_q_fx ) )
787 : {
788 0 : PPP_MODE_E = 'B';
789 0 : move16();
790 : }
791 :
792 0 : test();
793 : /*if ( pos_nq < neg_nq && neg_nq > 2.0f * neg_q ) */
794 0 : if ( LT_32( pos_nq_fx, neg_nq_fx ) && GT_32( Mult_32_16( neg_nq_fx, 16384 ), neg_q_fx ) )
795 : {
796 0 : PPP_MODE_E = 'B';
797 0 : move16();
798 : }
799 : }
800 : }
801 :
802 : ELSE
803 : {
804 : /* if ((((DTFS_getEngy(*CURRP_NQ) >(st->prev_cw_en))&&(max(pos_nq,neg_nq)>3.5))&&(st->rate_control))|| */
805 : /* (((DTFS_getEngy(*CURRP_NQ) >0.8*(st->prev_cw_en))&&(max(pos_nq,neg_nq)>3.0))&&(!st->rate_control))) */
806 0 : test();
807 0 : test();
808 0 : test();
809 0 : test();
810 0 : test();
811 0 : IF( ( ( ( L_sub( Ltmp_32, ( hSC_VBR->prev_cw_en_fx ) > 0 ) ) && ( GT_32( L_max( pos_nq_fx, neg_nq_fx ), 939524096 ) ) ) && ( hSC_VBR->rate_control ) ) ||
812 : ( ( ( GT_32( Ltmp_32, Ltmp1_32 ) ) && ( GT_32( L_max( pos_nq_fx, neg_nq_fx ), 805306368 ) ) ) && ( !hSC_VBR->rate_control ) ) )
813 : {
814 : /* if (((pos_nq > neg_nq) && (pos_nq > 2.5*pos_q)&&(st->rate_control))||
815 : ((pos_nq > neg_nq) && (pos_nq > 2.0*pos_q)&&(!st->rate_control))) */
816 0 : test();
817 0 : test();
818 0 : test();
819 0 : test();
820 0 : test();
821 0 : if ( ( GT_32( pos_nq_fx, neg_nq_fx ) && GT_32( Mult_32_16( pos_nq_fx, 13107 ), pos_q_fx ) && ( hSC_VBR->rate_control ) ) ||
822 0 : ( GT_32( pos_nq_fx, neg_nq_fx ) && GT_32( Mult_32_16( pos_nq_fx, 16384 ), pos_q_fx ) && ( !hSC_VBR->rate_control ) ) )
823 : {
824 0 : PPP_MODE_E = 'B';
825 0 : move16();
826 : }
827 :
828 : /* if ((((pos_nq < neg_nq) && (neg_nq > 2.5*neg_q))&&(st->rate_control))||
829 : ((pos_nq < neg_nq) && (neg_nq > 2.0*neg_q)&&(!st->rate_control))) */
830 0 : test();
831 0 : test();
832 0 : test();
833 0 : test();
834 0 : test();
835 0 : if ( ( LT_32( pos_nq_fx, neg_nq_fx ) && GT_32( Mult_32_16( neg_nq_fx, 13107 ), neg_q_fx ) && ( hSC_VBR->rate_control ) ) ||
836 0 : ( LT_32( pos_nq_fx, neg_nq_fx ) && GT_32( Mult_32_16( neg_nq_fx, 16384 ), neg_q_fx ) && ( !hSC_VBR->rate_control ) ) )
837 : {
838 0 : PPP_MODE_E = 'B';
839 0 : move16();
840 : }
841 : }
842 :
843 :
844 0 : IF( hSC_VBR->rate_control )
845 : {
846 :
847 0 : DTFS_peaktoaverage_fx( *CURRP_NQ_FX, &pos_nq0_fx, &Qposnq, &neg_nq0_fx, &Qnegnq );
848 :
849 0 : impzi_fx[0] = 1;
850 0 : move16();
851 0 : FOR( x_fx = 1; x_fx < 160; x_fx++ )
852 : {
853 0 : impzi_fx[x_fx] = 0;
854 0 : move16();
855 : }
856 :
857 0 : FOR( x_fx = 0; x_fx < 160; x_fx++ )
858 : {
859 0 : impzo_fx[x_fx] = 0;
860 0 : move16();
861 : }
862 :
863 0 : FOR( x_fx = 0; x_fx < 10; x_fx++ )
864 : {
865 0 : mem_fx[x_fx] = 0;
866 0 : move16();
867 : }
868 :
869 : /* lpc2_fx in Q12, so Qadj is set to 3 toi bring it to Q15 */
870 0 : Qadj = 15 - 12;
871 0 : move16();
872 0 : synthesis_filter_fx( lpc2_fx, &impzi_fx[0], &impzo_fx[0], &mem_fx[0], 10, 160 );
873 :
874 : /* compute energy of impz */
875 0 : FOR( x_fx = 0; x_fx < 160; x_fx++ )
876 : {
877 0 : energy_impz_fx = L_add_o( energy_impz_fx, L_mult0( impzo_fx[x_fx], impzo_fx[x_fx] ), &Overflow );
878 : }
879 :
880 : /*energy_impz = (float)(10*log10((float)energy_impz)); */
881 0 : exp_ee = norm_l( energy_impz_fx );
882 0 : frac_ee = Log2_norm_lc( L_shl( energy_impz_fx, exp_ee ) );
883 0 : exp_ee = sub( 30, exp_ee ); /*30-exp-Q0 */
884 0 : Ltmp = Mpy_32_16( exp_ee, frac_ee, LG10 ); /* Ltmp Q14 */
885 0 : energy_impz_fx = L_shr( Ltmp, 3 ); /* 16+11(4bits for 15 no) = 14+x => x= 11 */
886 : /* energy_impz_fx is Q11 */
887 :
888 0 : Ltmp_32 = DTFS_getEngy_fx( CURRP_Q_E_FX ); /*Q = 2*(CURRP_Q_E_FX->Q) */
889 0 : move32();
890 0 : Qadj = sub( hSC_VBR->Q_prev_cw_en_fx, shl( CURRP_Q_E_FX->Q, 1 ) );
891 0 : Ltmp_32 = L_shl_o( Ltmp_32, Qadj, &Overflow ); /* shift left required to adjust Q of CURRP_Q_E_FX = Q_prev_cw_en_fx */
892 : /* if ((DTFS_getEngy(*CURRP_Q_E) > st->prev_cw_en)&&(max(pos_q,neg_q)>3.5) && energy_impz>15.0 && tmpres>0.7) */
893 0 : test();
894 0 : test();
895 0 : test();
896 0 : IF( ( GT_32( Ltmp_32, hSC_VBR->prev_cw_en_fx ) ) && ( GT_32( L_max( pos_q_fx, neg_q_fx ), 939524096 ) ) && ( GT_32( energy_impz_fx, 30720 ) ) && ( GT_32( Mult_32_16( tmpres_fx, 23265 ), shl_o( 1, sub( Qtmpres, 1 ), &Overflow ) ) ) )
897 : {
898 : /* if ((pos_q > neg_q) && ((pos_q>3.0*pos_nq0) || ((pos_q > 1.5*pos_nq0) && (neg_q < 1.5*neg_nq0)))) */
899 0 : test();
900 0 : test();
901 0 : test();
902 0 : if ( ( GT_32( pos_q_fx, neg_q_fx ) ) && ( ( GT_32( Mult_32_16( pos_q_fx, 10923 ), L_shr( pos_nq0_fx, sub( Qposnq, 28 ) ) ) ) || ( ( GT_32( Mult_32_16( pos_q_fx, 21845 ), L_shr( pos_nq0_fx, sub( Qposnq, 28 ) ) ) ) && ( LT_32( Mult_32_16( neg_q_fx, 21846 ), L_shr( neg_nq0_fx, sub( Qnegnq, 28 ) ) ) ) ) ) )
903 : /* 10923 = (1/3) oin Q15, pos_q_fx is Q28, so result of Mult_32_16(pos_q_fx,10923) = Q28 */
904 : /* L_shr(pos_nq0_fx,sub(Qposnq,28)) brings pos_nq0_fx with variable Q to fixed Q28 */
905 : {
906 0 : PPP_MODE_E = 'B';
907 0 : move16();
908 : }
909 0 : test();
910 0 : test();
911 0 : test();
912 : /* if ((pos_q <= neg_q) && ((neg_q>3.0*neg_nq0)|| ((neg_q > 1.5*neg_nq0) && (pos_q < 1.5*pos_nq0)))) */
913 0 : if ( ( LE_32( pos_q_fx, neg_q_fx ) ) && ( ( GT_32( Mult_32_16( neg_q_fx, 10923 ), L_shr( neg_nq0_fx, sub( Qnegnq, 28 ) ) ) ) || ( ( GT_32( Mult_32_16( neg_q_fx, 21846 ), L_shr( neg_nq0_fx, sub( Qnegnq, 28 ) ) ) ) && ( LT_32( Mult_32_16( pos_q_fx, 21846 ), L_shr( pos_nq0_fx, sub( Qposnq, 28 ) ) ) ) ) ) )
914 : {
915 0 : PPP_MODE_E = 'B';
916 0 : move16();
917 : }
918 : }
919 : }
920 : }
921 :
922 0 : DTFS_copy_fx( TMPDTFS2_FX, *CURRP_Q_E_FX );
923 0 : DTFS_poleFilter_fx_9( TMPDTFS_FX, pf_temp1, pf_temp2, pf_temp, pf_n2 );
924 0 : DTFS_poleFilter_fx_9( TMPDTFS2_FX, pf_temp1, pf_temp2, pf_temp, pf_n2 );
925 :
926 0 : DTFS_sub_fx( TMPDTFS3_FX, *TMPDTFS_FX, *TMPDTFS2_FX );
927 :
928 :
929 : /* operate in ADR mode only the rate control is active. This adds some bumpups to improve the speech quality */
930 : /* if ((DTFS_getEngy_band(*TMPDTFS, 1500.0, upper_cut_off_freq_of_interest)/DTFS_getEngy(*TMPDTFS) > 0.05)&&(!st->rate_control)) */
931 0 : Ltemp1 = DTFS_getEngy_band_fx( *TMPDTFS_FX, 1500, upper_cut_off_freq_of_interest_fx ); /* Q = 2*TMPDTFS_FX->Q*/
932 0 : move32();
933 0 : Ltemp2 = DTFS_getEngy_fx( TMPDTFS_FX ); /* Q = 2*TMPDTFS_FX->Q */
934 0 : move32();
935 0 : IF( Ltemp2 == 0 )
936 : {
937 0 : Ltemp_fx = 0;
938 0 : move32();
939 : }
940 : ELSE
941 : {
942 0 : expb = norm_l( Ltemp1 );
943 0 : fracb = extract_h( L_shl( Ltemp1, expb ) );
944 0 : expb = sub( 30, add( expb, shl( TMPDTFS_FX->Q, 1 ) ) );
945 0 : expa = norm_l( Ltemp2 );
946 0 : fraca = extract_h( L_shl( Ltemp2, expa ) );
947 0 : expa = sub( 30, add( expa, shl( TMPDTFS_FX->Q, 1 ) ) );
948 :
949 0 : scale = shr( sub( fraca, fracb ), 15 );
950 0 : fracb = shl( fracb, scale );
951 0 : expb = sub( expb, scale );
952 :
953 0 : tmp = div_s( fracb, fraca ); /* tmp in Q15 */
954 0 : exp = sub( expb, expa ); /* ans = tmp*2^(exp) */
955 0 : Ltemp_fx = L_shl_o( tmp, add( exp, 12 ), &Overflow ); /* make tmp Q27 */
956 : }
957 0 : test();
958 0 : IF( GT_32( Ltemp_fx, 6710886 ) && ( !hSC_VBR->rate_control ) ) /* 0.05 in Q27 = 6710886 */
959 : {
960 : /*if (10.0*log10(DTFS_getEngy_band(*TMPDTFS,1500.0,upper_cut_off_freq_of_interest)/ */
961 : /*DTFS_getEngy_band(*TMPDTFS3,1500.0,upper_cut_off_freq_of_interest)) < 0.1) */
962 :
963 0 : Ltemp1 = DTFS_getEngy_band_fx( *TMPDTFS_FX, 1500, upper_cut_off_freq_of_interest_fx );
964 0 : move32();
965 0 : Ltemp2 = DTFS_getEngy_band_fx( *TMPDTFS3_FX, 1500, upper_cut_off_freq_of_interest_fx );
966 0 : move32();
967 : /*--------------------------------------------------------------*/
968 : /* Ltemp_fx = (Word32)divide_dp(Ltemp1, Ltemp2, Qadj,1);//Q29+1 */
969 : /*--------------------------------------------------------------*/
970 0 : IF( Ltemp2 == 0 )
971 : {
972 0 : Ltemp_fx = 0;
973 0 : move32();
974 : }
975 : ELSE
976 : {
977 0 : expa = norm_l( Ltemp2 );
978 0 : fraca = extract_h( L_shl( Ltemp2, expa ) );
979 0 : expa = sub( 30, expa );
980 :
981 0 : expb = norm_l( Ltemp1 );
982 0 : fracb = round_fx( L_shl( Ltemp1, expb ) );
983 0 : expb = sub( 30, expb );
984 :
985 0 : scale = shr( sub( fraca, fracb ), 15 );
986 0 : fracb = shl( fracb, scale );
987 0 : expb = sub( expb, scale );
988 :
989 0 : tmp = div_s( fracb, fraca );
990 0 : exp = sub( expb, expa );
991 0 : Ltemp_fx = L_shl_o( tmp, add( exp, 14 ), &Overflow ); /* answer in Q29 */
992 : }
993 : /*-------------------------------------------*/
994 :
995 : /* 10.0*log10(Ltemp_fx) */
996 0 : exp_ee = norm_l( Ltemp_fx );
997 0 : frac_ee = Log2_norm_lc( L_shl( Ltemp_fx, exp_ee ) );
998 0 : exp_ee = sub( 30, add( exp_ee, 29 ) ); /* 30 fixed here, 29 is the Q of Ltemp_fx */
999 0 : Ltmp = Mpy_32_16( exp_ee, frac_ee, LG10 ); /* LG10 in Q13, so answer Ltmp in Q14 */
1000 :
1001 0 : IF( LT_32( Ltmp, 1638 ) ) /* 1638 = 0.1 in Q14 */
1002 : {
1003 : /* if (res_enratio > 0.8) */
1004 0 : if ( GT_32( res_enratio_fx, 1638 ) ) /* 1638 = 0.8 in Q11, res_enratio_fx in Q11 */
1005 : {
1006 0 : PPP_MODE_E = 'B';
1007 0 : move16();
1008 : }
1009 : }
1010 : }
1011 :
1012 : /* To increase bump up, raise first threshold, lower second */
1013 : /*tmp = (float)(10.0*log10(DTFS_getEngy(*TMPDTFS)/DTFS_getEngy(*TMPDTFS3)));*/
1014 0 : Lacc = DTFS_getEngy_P2A_fx( TMPDTFS_FX ); /* Q = 2*(TMPDTFS_FX->Q) */
1015 0 : move32();
1016 0 : Lacc1 = DTFS_getEngy_P2A_fx( TMPDTFS3_FX ); /* Q = 2*(TMPDTFS3_FX->Q) */
1017 0 : move32();
1018 :
1019 0 : sft = shl( sub( TMPDTFS_FX->Q, TMPDTFS3_FX->Q ), 1 ); /* to check if Lacc<=2*Lacc1 */
1020 0 : flag1 = 0;
1021 0 : move16();
1022 0 : IF( sft > 0 )
1023 : {
1024 : /*if (L_sub40_40(L_shr40(Lacc,sft),Lacc1)<=0) */
1025 0 : Lacc = L_shr( Lacc, sft );
1026 0 : flag1 = 1;
1027 0 : move16(); /* do the divide */
1028 : }
1029 : ELSE
1030 : {
1031 : /*if (L_sub40_40(Lacc,L_shr40(Lacc1,negate(sft)))<=0) */
1032 0 : Lacc1 = L_shr( Lacc1, negate( sft ) );
1033 0 : flag1 = 1;
1034 0 : move16(); /* do the divide */
1035 : }
1036 0 : IF( EQ_16( flag1, 1 ) )
1037 : {
1038 0 : expb = norm_l( Lacc );
1039 0 : fracb = extract_h( L_shl( Lacc, expb ) );
1040 0 : expb = sub( 30, add( expb, shl( TMPDTFS_FX->Q, 1 ) ) );
1041 :
1042 0 : expa = norm_l( Lacc1 );
1043 0 : fraca = extract_h( L_shl( Lacc1, expa ) );
1044 0 : expa = sub( 30, add( expa, shl( TMPDTFS3_FX->Q, 1 ) ) );
1045 :
1046 0 : scale = shr( sub( fraca, fracb ), 15 );
1047 0 : fracb = shl( fracb, scale );
1048 0 : expb = sub( expb, scale );
1049 :
1050 0 : tmp = div_s( fracb, fraca ); /* tmp is always Q15 */
1051 0 : exp = sub( expb, expa ); /* Answer after division Lacc/Lacc1 = (2^exp)*(tmp/2^15) */
1052 :
1053 0 : L_tmp = L_deposit_h( tmp ); /* tmp is always Q15, L_tmp is always Q31 */
1054 0 : expa = norm_l( L_tmp );
1055 0 : L_tmp = L_shl( L_tmp, expa );
1056 0 : exp = sub( 30, add( expa, sub( 31, exp ) ) );
1057 0 : frac = Log2_norm_lc( L_tmp );
1058 0 : L_tmp = Mpy_32_16( exp, frac, 12330 ); /* L_tmp is always Q13 */
1059 0 : Ltemp = L_shl_o( L_tmp, 10, &Overflow ); /* Ltemp is always Q23 */
1060 : }
1061 : ELSE
1062 : {
1063 0 : Ltemp = L_add( MAX_32, 0 );
1064 : }
1065 :
1066 0 : test();
1067 0 : if ( ( Ltemp <= 0 ) && ( !hSC_VBR->rate_control ) )
1068 : {
1069 0 : PPP_MODE_E = 'B';
1070 0 : move16();
1071 : }
1072 :
1073 0 : IF( LT_16( shl_o( hSC_VBR->vadsnr_fx, 1, &Overflow ), hSC_VBR->SNR_THLD_fx ) ) /* Q8 */
1074 : {
1075 : /* if ((( tmp < 3.05 && max(res_enratio,sp_enratio) > 0.8 ) && (st->rate_control))||
1076 : (( tmp < 2.8 && max(res_enratio,sp_enratio) > 0.65 ) && (!st->rate_control))) */
1077 : /* First comparison in Q23, Second comparison in Q15 */
1078 0 : test();
1079 0 : test();
1080 0 : test();
1081 0 : test();
1082 0 : test();
1083 0 : if ( ( ( LT_32( Ltemp, 25585254 ) && GT_32( L_max( L_shl( res_enratio_fx, 4 ), sp_enratio_fx ), 6554 ) ) && ( hSC_VBR->rate_control ) ) ||
1084 0 : ( ( LT_32( Ltemp, 23488102 ) && GT_32( L_max( L_shl( res_enratio_fx, 4 ), sp_enratio_fx ), 5325 ) ) && ( !hSC_VBR->rate_control ) ) )
1085 : {
1086 0 : PPP_MODE_E = 'B';
1087 0 : move16();
1088 : }
1089 : }
1090 : ELSE
1091 : {
1092 : /* if ((( tmp < 2.4 && max(res_enratio,sp_enratio) > 0.94) && (st->rate_control))||
1093 : (( tmp < 4.5 && max(res_enratio,sp_enratio) > 0.5 ) && (!st->rate_control))) */
1094 0 : test();
1095 0 : test();
1096 0 : test();
1097 0 : test();
1098 0 : test();
1099 0 : if ( ( ( LT_32( Ltemp, 20132659 ) && GT_32( L_max( L_shl( res_enratio_fx, 4 ), sp_enratio_fx ), 7700 ) ) && ( hSC_VBR->rate_control ) ) ||
1100 0 : ( ( LT_32( Ltemp, 37748736 ) && GT_32( L_max( L_shl( res_enratio_fx, 4 ), sp_enratio_fx ), 4096 ) ) && ( !hSC_VBR->rate_control ) ) )
1101 : {
1102 0 : PPP_MODE_E = 'B';
1103 0 : move16();
1104 : }
1105 : }
1106 :
1107 : /* -----End closed-loop Bump-Up */
1108 : }
1109 : ELSE
1110 : {
1111 0 : PPP_MODE_E = 'B';
1112 0 : move16(); /*Amplitude quantization is failing*/
1113 : }
1114 : }
1115 : ELSE
1116 : {
1117 : }
1118 0 : IF( PPP_MODE_E == 'B' )
1119 : {
1120 0 : hSC_VBR->bump_up = 1;
1121 0 : move16();
1122 0 : free( CURRP_NQ_FX );
1123 0 : free( TMPDTFS_FX );
1124 0 : free( TMPDTFS2_FX );
1125 0 : free( TMPDTFS3_FX );
1126 0 : free( CURRP_Q_E_FX );
1127 0 : free( dtfs_temp_fx );
1128 0 : return error;
1129 : }
1130 :
1131 0 : IF( hSC_VBR->Q_to_F )
1132 : {
1133 0 : hSC_VBR->patterncount = add( hSC_VBR->patterncount, hSC_VBR->pattern_m );
1134 0 : move16();
1135 :
1136 0 : IF( GE_16( hSC_VBR->patterncount, 1000 ) )
1137 : {
1138 0 : hSC_VBR->patterncount = sub( hSC_VBR->patterncount, 1000 );
1139 0 : move16();
1140 0 : PPP_MODE_E = 'B';
1141 0 : move16();
1142 0 : hSC_VBR->bump_up = 1;
1143 0 : move16();
1144 0 : free( CURRP_NQ_FX );
1145 0 : free( TMPDTFS_FX );
1146 0 : free( TMPDTFS2_FX );
1147 0 : free( TMPDTFS3_FX );
1148 0 : free( CURRP_Q_E_FX );
1149 0 : free( dtfs_temp_fx );
1150 0 : return error;
1151 : }
1152 : }
1153 :
1154 : /* packetization of the delta lag in_fx PPP */
1155 0 : IF( EQ_16( PPP_MODE_E, 'Q' ) )
1156 : {
1157 0 : Q_delta_lag = add( delta_lag_E, 11 ); /* to make it positive always */
1158 :
1159 0 : push_indice( hBstr, IND_DELTALAG, Q_delta_lag, 5 );
1160 : }
1161 :
1162 0 : WIsyn_fx( *dtfs_temp_fx, CURRP_Q_E_FX, lpc2_fx, &( hSC_VBR->ph_offset_E_fx ), out_fx, L_FRAME, 0, S_fx, C_fx,
1163 : pf_temp1, pf_temp2, pf_temp, pf_n2 );
1164 : /* i/o ph_offset_fx in Q15, out_fx in Q0 */
1165 :
1166 0 : DTFS_copy_fx( dtfs_temp_fx, *CURRP_Q_E_FX );
1167 0 : Lacc = DTFS_getEngy_P2A_fx( CURRP_NQ_FX );
1168 0 : move32();
1169 0 : hSC_VBR->Q_prev_cw_en_fx = norm_l( Lacc );
1170 0 : move16();
1171 : /* hSC_VBR->Q_prev_cw_en_fx = (Lacc==0)?31: hSC_VBR->Q_prev_cw_en_fx;move16(); */
1172 0 : if ( Lacc == 0 )
1173 : {
1174 0 : hSC_VBR->Q_prev_cw_en_fx = 31;
1175 0 : move16();
1176 : }
1177 :
1178 0 : hSC_VBR->prev_cw_en_fx = (Word32) L_shl( Lacc, hSC_VBR->Q_prev_cw_en_fx ); /*2Q+Q_prev_cw_en_fx */
1179 0 : move32();
1180 0 : hSC_VBR->Q_prev_cw_en_fx = add( hSC_VBR->Q_prev_cw_en_fx, shl( CURRP_NQ_FX->Q, 1 ) );
1181 0 : move16();
1182 : /* Copy DTFS related parameters from 'dtfs_temp' to 'st_fx' structure */
1183 0 : hSC_VBR->dtfs_enc_lag = dtfs_temp_fx->lag_fx;
1184 0 : move16();
1185 0 : hSC_VBR->dtfs_enc_nH = dtfs_temp_fx->nH_fx;
1186 0 : move16();
1187 0 : hSC_VBR->dtfs_enc_nH_4kHz = dtfs_temp_fx->nH_4kHz_fx;
1188 0 : move16();
1189 0 : hSC_VBR->dtfs_enc_upper_cut_off_freq_of_interest_fx = dtfs_temp_fx->upper_cut_off_freq_of_interest_fx;
1190 0 : move16();
1191 0 : hSC_VBR->dtfs_enc_upper_cut_off_freq_fx = dtfs_temp_fx->upper_cut_off_freq_fx;
1192 0 : move16();
1193 0 : Copy( dtfs_temp_fx->a_fx, hSC_VBR->dtfs_enc_a_fx, MAXLAG_WI );
1194 0 : Copy( dtfs_temp_fx->b_fx, hSC_VBR->dtfs_enc_b_fx, MAXLAG_WI );
1195 :
1196 0 : hSC_VBR->dtfs_enc_Q = dtfs_temp_fx->Q;
1197 0 : move16();
1198 :
1199 0 : free( CURRP_NQ_FX );
1200 0 : free( TMPDTFS_FX );
1201 0 : free( TMPDTFS2_FX );
1202 0 : free( TMPDTFS3_FX );
1203 0 : free( CURRP_Q_E_FX );
1204 0 : free( dtfs_temp_fx );
1205 :
1206 0 : return error;
1207 : }
1208 :
1209 :
1210 : /*===================================================================*/
1211 : /* FUNCTION : synthesis_filter_fx () */
1212 : /*-------------------------------------------------------------------*/
1213 : /* PURPOSE : IIR-filter residual by the LPC sysnthesis filter */
1214 : /*-------------------------------------------------------------------*/
1215 : /* INPUT ARGUMENTS : */
1216 : /* */
1217 : /* _ (Word16 []) b : filter coefficients (Qc). */
1218 : /* _ (Word16 []) x : residual input (Qn). */
1219 : /* _ (Word16) P : filter order. */
1220 : /* _ (Word16) N : number of input samples. */
1221 : /* _ (Word16) Qa : Q factor compensation (Qa=15-Qc) */
1222 : /*-------------------------------------------------------------------*/
1223 : /* OUTPUT ARGUMENTS : */
1224 : /* */
1225 : /* _ (Word16 []) y : output speech (Qn) */
1226 : /*-------------------------------------------------------------------*/
1227 : /* INPUT/OUTPUT ARGUMENTS : */
1228 : /* */
1229 : /* _ (Word32 []) buf : filter memory (Q16+Qn) */
1230 : /*-------------------------------------------------------------------*/
1231 : /* RETURN ARGUMENTS : _ None. */
1232 : /*===================================================================*/
1233 : /* y(n)=sum(i=1 to 10){ */
1234 : /* rxLpcCoeff(i)*rxFormSynthMem(n-i) */
1235 : /* } */
1236 : /* +fres(n) */
1237 : /*===================================================================*/
1238 0 : static void synthesis_filter_fx( Word16 b[], Word16 x[], Word16 y[], Word16 buf[], Word16 P, Word16 N )
1239 : {
1240 : Word32 acc;
1241 : Word16 i, j;
1242 : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
1243 0 : Flag Overflow = 0;
1244 0 : move32();
1245 : #endif
1246 0 : FOR( i = 0; i < N; i++ )
1247 : {
1248 0 : acc = L_deposit_h( *x++ ); /*Q16 */
1249 0 : acc = L_shr( acc, 3 ); /*Q13 */
1250 :
1251 0 : FOR( j = P - 1; j > 0; j-- )
1252 : {
1253 : /* acc = L_sub(acc, L_mult(memory[j], coef[j])); */
1254 0 : acc = L_msu_o( acc, buf[j], b[j], &Overflow ); /*Q13 */
1255 0 : buf[j] = buf[j - 1];
1256 0 : move16();
1257 : }
1258 : /* acc = L_sub(acc, L_mult(memory[0], coef[0])); */
1259 0 : acc = L_msu_o( acc, buf[0], b[0], &Overflow );
1260 0 : acc = L_shl_o( acc, 3, &Overflow );
1261 :
1262 0 : *y++ = round_fx_o( acc, &Overflow );
1263 0 : move16();
1264 0 : buf[0] = round_fx_o( acc, &Overflow );
1265 0 : move16();
1266 : }
1267 0 : }
1268 : /*==============================================================================*/
1269 : /* FUNCTION : DTFS_freq_corr_fx () */
1270 : /*------------------------------------------------------------------------------*/
1271 : /* PURPOSE : */
1272 : /*------------------------------------------------------------------------------*/
1273 : /* INPUT ARGUMENTS : */
1274 : /* _ (struct DTFS_STRUCTURE_FX) X1_DTFS_fx : a_fx/b_fx in X1_DTFS_fx.Q */
1275 : /* _ (struct DTFS_STRUCTURE_FX) X2_DTFS_fx : a_fx/b_fx in X2_DTFS_fx.Q */
1276 : /* _ (Word16) lband: Q0 */
1277 : /* _ (Word16) hband: Q0 */
1278 : /*------------------------------------------------------------------------------*/
1279 : /* OUTPUT ARGUMENTS : */
1280 : /* _ (Word16) *Qout : Q of output result */
1281 : /*------------------------------------------------------------------------------*/
1282 : /* INPUT/OUTPUT ARGUMENTS : */
1283 : /* _ None */
1284 : /*------------------------------------------------------------------------------*/
1285 : /* RETURN ARGUMENTS : */
1286 : /* _ (Word32) Result : Qout */
1287 : /*------------------------------------------------------------------------------*/
1288 : /* CALLED FROM : TX */
1289 : /*==============================================================================*/
1290 :
1291 0 : static Word32 DTFS_freq_corr_fx(
1292 : DTFS_STRUCTURE X1_DTFS_fx,
1293 : DTFS_STRUCTURE X2_DTFS_fx,
1294 : Word16 lband,
1295 : Word16 hband,
1296 : Word16 *Qout )
1297 : {
1298 : Word16 k, HalfLag, lk, hk;
1299 : Word32 corr_fx;
1300 : Word32 freq_fx, L_lband, L_hband;
1301 : Word32 E_fx;
1302 : Word32 Num, Den, Result;
1303 : Word16 E1_fx, E2_fx, q1, q2, Qr;
1304 : Word16 expa, expb, fraca, fracb, scale;
1305 : Word16 exp, tmp;
1306 : Word32 L_tmp;
1307 : Word16 Q_num, Q_den;
1308 : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
1309 0 : Flag Overflow = 0;
1310 0 : move32();
1311 : #endif
1312 0 : IF( LT_16( X1_DTFS_fx.lag_fx, X2_DTFS_fx.lag_fx ) )
1313 : {
1314 0 : DTFS_zeroPadd_fx( X2_DTFS_fx.lag_fx, &X1_DTFS_fx );
1315 : }
1316 :
1317 0 : corr_fx = L_deposit_l( 0 );
1318 :
1319 0 : L_lband = L_mult( lband, X2_DTFS_fx.lag_fx ); /* Q0 * Q0 -> Q1 */
1320 0 : L_hband = L_mult( hband, X2_DTFS_fx.lag_fx ); /* Q0 * Q0 -> Q1 */
1321 0 : HalfLag = s_min( shr( X2_DTFS_fx.lag_fx, 1 ), X2_DTFS_fx.nH_4kHz_fx );
1322 :
1323 : /* get lband and hband */
1324 0 : FOR( k = 0; k <= HalfLag; k++ )
1325 : {
1326 0 : freq_fx = L_mult( k, 12800 );
1327 0 : if ( GE_32( freq_fx, L_lband ) )
1328 : {
1329 0 : BREAK;
1330 : }
1331 : }
1332 0 : lk = k;
1333 0 : FOR( k = 0; k <= HalfLag; k++ )
1334 : {
1335 0 : freq_fx = L_mult( k, 12800 );
1336 0 : if ( GE_32( freq_fx, L_hband ) )
1337 : {
1338 0 : BREAK;
1339 : }
1340 : }
1341 0 : hk = k;
1342 0 : move16();
1343 :
1344 0 : FOR( k = lk; k < hk; k++ )
1345 : {
1346 0 : corr_fx = L_mac0_o( corr_fx, X1_DTFS_fx.a_fx[k], X2_DTFS_fx.a_fx[k], &Overflow ); /* Q(1) */
1347 0 : corr_fx = L_mac0_o( corr_fx, X1_DTFS_fx.b_fx[k], X2_DTFS_fx.b_fx[k], &Overflow ); /* Q(1) */
1348 : }
1349 :
1350 0 : Qr = norm_l( corr_fx );
1351 0 : if ( corr_fx == 0 )
1352 : {
1353 0 : Qr = 31;
1354 0 : move16();
1355 : }
1356 :
1357 0 : E1_fx = round_fx_o( L_shl_o( corr_fx, Qr, &Overflow ), &Overflow ); /* Q(Qr-16) */
1358 0 : Num = L_mult0( E1_fx, E1_fx ); /* Q(2+2*Qr-32+1) */
1359 0 : Q_num = sub( shl( add( add( X1_DTFS_fx.Q, X2_DTFS_fx.Q ), Qr ), 1 ), 32 );
1360 :
1361 : /* PORTING: Handling the functions with variable no. of arguments */
1362 0 : E_fx = DTFS_getEngy_band_fx( X1_DTFS_fx, lband, hband ); /* Q(1) */
1363 0 : move32();
1364 0 : q1 = norm_l( E_fx );
1365 0 : if ( E_fx == 0 )
1366 : {
1367 0 : q1 = 31;
1368 0 : move16();
1369 : }
1370 :
1371 0 : E1_fx = round_fx_o( L_shl_o( E_fx, q1, &Overflow ), &Overflow ); /* Q(1+q1-16) */
1372 : /* PORTING: Handling the functions with variable no. of arguments */
1373 0 : E_fx = DTFS_getEngy_band_fx( X2_DTFS_fx, lband, hband ); /* Q(1) */
1374 0 : q2 = norm_l( E_fx );
1375 0 : if ( E_fx == 0 )
1376 : {
1377 0 : q2 = 31;
1378 0 : move16();
1379 : }
1380 :
1381 0 : E2_fx = round_fx_o( L_shl_o( E_fx, q2, &Overflow ), &Overflow ); /* Q(1+q2-16) */
1382 0 : Den = L_mult0( E1_fx, E2_fx ); /* Q(2+q1+q2-32+1) */
1383 0 : Q_den = sub( add( shl( add( X2_DTFS_fx.Q, X1_DTFS_fx.Q ), 1 ), add( q1, q2 ) ), 32 );
1384 :
1385 0 : Num = L_max( Num, 1 );
1386 :
1387 0 : IF( Num == 0 )
1388 : {
1389 0 : Result = 0;
1390 0 : move32();
1391 0 : *Qout = 31;
1392 0 : move16();
1393 : }
1394 : ELSE
1395 : {
1396 :
1397 0 : expa = norm_l( Num );
1398 0 : fraca = extract_h( L_shl_o( Num, expa, &Overflow ) );
1399 0 : expa = sub( 30, add( expa, Q_num ) );
1400 :
1401 :
1402 0 : expb = norm_l( Den );
1403 0 : fracb = round_fx_o( L_shl_o( Den, expb, &Overflow ), &Overflow );
1404 0 : expb = sub( 30, add( expb, Q_den ) );
1405 :
1406 :
1407 0 : scale = shr( sub( fraca, fracb ), 15 );
1408 0 : fracb = shl_o( fracb, scale, &Overflow );
1409 0 : expb = sub( expb, scale );
1410 :
1411 0 : tmp = div_s( fracb, fraca );
1412 0 : exp = sub( expb, expa );
1413 :
1414 0 : L_tmp = Isqrt_lc( L_deposit_h( tmp ), &exp ); /* Q(31-exp) */
1415 0 : IF( corr_fx > 0 )
1416 : {
1417 0 : Result = L_add( L_tmp, 0 );
1418 : }
1419 : ELSE
1420 : {
1421 0 : Result = L_negate( L_tmp );
1422 : }
1423 0 : *Qout = sub( 30, exp );
1424 0 : move16();
1425 : }
1426 0 : return Result;
1427 : }
1428 : /*===================================================================*/
1429 : /* FUNCTION : DTFS_alignment_extract_td_fx () */
1430 : /*-------------------------------------------------------------------*/
1431 : /* PURPOSE : search for alignment in time domain */
1432 : /*-------------------------------------------------------------------*/
1433 : /* INPUT ARGUMENTS : */
1434 : /* _ (Word16 *) x1: Q? */
1435 : /* _ (Word16 *) x2: Q? */
1436 : /*-------------------------------------------------------------------*/
1437 : /* OUTPUT ARGUMENTS : */
1438 : /* _ (Word16) lag : Q0 */
1439 : /*-------------------------------------------------------------------*/
1440 : /* INPUT/OUTPUT ARGUMENTS : */
1441 : /* _ None */
1442 : /*-------------------------------------------------------------------*/
1443 : /* RETURN ARGUMENTS : _ (Word16 *) idx: Q0 */
1444 : /*-------------------------------------------------------------------*/
1445 : /* CALLED FROM : TX */
1446 : /*===================================================================*/
1447 :
1448 0 : static Word16 DTFS_alignment_extract_td_fx( Word16 *x1, Word16 *x2, Word16 lag )
1449 : {
1450 : Word16 j, k, idx, Adiff_fx;
1451 : Word32 maxcorr, corr;
1452 : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
1453 0 : Flag Overflow = 0;
1454 0 : move32();
1455 : #endif
1456 0 : maxcorr = L_add( MIN_32, 0 );
1457 0 : Adiff_fx = (Word16) ( s_max( 4, shr( lag, 3 ) ) );
1458 :
1459 0 : idx = 0;
1460 0 : move16();
1461 0 : FOR( j = negate( Adiff_fx ); j <= Adiff_fx; j++ )
1462 : {
1463 0 : corr = L_deposit_l( 0 );
1464 0 : FOR( k = 0; k < lag; k++ )
1465 : {
1466 0 : corr = L_mac_o( corr, x1[k], x2[( k - j + lag ) % lag], &Overflow );
1467 : }
1468 0 : IF( GT_32( corr, maxcorr ) )
1469 : {
1470 0 : idx = j;
1471 0 : move16();
1472 0 : maxcorr = L_add( corr, 0 );
1473 : }
1474 : }
1475 0 : return idx;
1476 : }
1477 : /*=================================================================================*/
1478 : /* FUNCTION : DTFS_getEngy_band_fx (Word16 lband, Word16 hband) */
1479 : /*---------------------------------------------------------------------------------*/
1480 : /* PURPOSE : compute the energy of X1.a[k] and X2.b[k] */
1481 : /*---------------------------------------------------------------------------------*/
1482 : /* INPUT ARGUMENTS : */
1483 : /* _ (struct DTFS_STRUCTURE_FX) X_fx : a_fx/b_fx in X_fx.Q, lag in Q0 */
1484 : /* _ (Word16) lband: Q0 */
1485 : /* _ (Word16) hband: Q0 */
1486 : /*---------------------------------------------------------------------------------*/
1487 : /* OUTPUT ARGUMENTS : */
1488 : /* _ (Word40) en_fx : 2*X1.Q */
1489 : /*---------------------------------------------------------------------------------*/
1490 : /* INPUT/OUTPUT ARGUMENTS : */
1491 : /* _ None */
1492 : /*---------------------------------------------------------------------------------*/
1493 : /* RETURN ARGUMENTS : _ None. */
1494 : /*---------------------------------------------------------------------------------*/
1495 : /* CALLED FROM : TX */
1496 : /*=================================================================================*/
1497 :
1498 : /* PORTING: Handling the functions with variable no. of arguments */
1499 :
1500 0 : static Word32 DTFS_getEngy_band_fx(
1501 : DTFS_STRUCTURE X_fx,
1502 : const Word16 lband,
1503 : const Word16 hband )
1504 : {
1505 : Word16 k, lk, hk, HalfLag;
1506 : Word32 freq_fx, L_lband, L_hband;
1507 0 : Word32 en_fx = 0;
1508 0 : move32();
1509 : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
1510 0 : Flag Overflow = 0;
1511 0 : move32();
1512 : #endif
1513 0 : L_lband = L_mult( lband, X_fx.lag_fx ); /* Q0 * Q0 -> Q1 */
1514 0 : L_hband = L_mult( hband, X_fx.lag_fx ); /* Q0 * Q0 -> Q1 */
1515 0 : HalfLag = s_min( shr( sub( X_fx.lag_fx, 1 ), 1 ), X_fx.nH_4kHz_fx );
1516 : /* get lband and hband */
1517 0 : FOR( k = 1; k <= HalfLag; k++ )
1518 : {
1519 0 : freq_fx = L_mult( k, 12800 );
1520 0 : if ( GE_32( freq_fx, L_lband ) )
1521 : {
1522 0 : BREAK;
1523 : }
1524 : }
1525 0 : lk = k;
1526 0 : move16();
1527 0 : FOR( k = 1; k <= HalfLag; k++ )
1528 : {
1529 0 : freq_fx = L_mult( k, 12800 );
1530 0 : if ( GE_32( freq_fx, L_hband ) )
1531 : {
1532 0 : BREAK;
1533 : }
1534 : }
1535 0 : hk = k;
1536 0 : move16();
1537 :
1538 0 : FOR( k = lk; k < hk; k++ )
1539 : {
1540 0 : en_fx = L_mac0_o( en_fx, X_fx.a_fx[k], X_fx.a_fx[k], &Overflow ); /* 2*X1.Q+1 */
1541 0 : en_fx = L_mac0_o( en_fx, X_fx.b_fx[k], X_fx.b_fx[k], &Overflow );
1542 : }
1543 :
1544 0 : if ( lband == 0 )
1545 : {
1546 0 : en_fx = L_mac0( en_fx, X_fx.a_fx[0], X_fx.a_fx[0] ); /* 2*X1.Q+1 */
1547 : }
1548 :
1549 : /* IF ((X_fx.lag_fx%2 == 0) && (hband == X_fx.upper_cut_off_freq_fx)) */
1550 0 : test();
1551 0 : IF( ( s_and( X_fx.lag_fx, 1 ) == 0 ) && ( hband == X_fx.upper_cut_off_freq_fx ) )
1552 : {
1553 0 : en_fx = L_mac0_o( en_fx, X_fx.a_fx[k], X_fx.a_fx[k], &Overflow );
1554 0 : en_fx = L_mac0_o( en_fx, X_fx.b_fx[k], X_fx.b_fx[k], &Overflow );
1555 : }
1556 :
1557 0 : return en_fx; /* 2*X1.Q */
1558 : }
1559 : /*---------------------------------------------------------------------*
1560 : * sc_vbr_enc_init_fx()
1561 : *
1562 : * Initialize SC-VBR encoder
1563 : *---------------------------------------------------------------------*/
1564 :
1565 3 : void sc_vbr_enc_init_fx(
1566 : SC_VBR_ENC_HANDLE hSC_VBR /* i/o: SC-VBR encoder handle */
1567 : )
1568 : {
1569 3 : hSC_VBR->nelp_enc_seed = 0;
1570 3 : move16();
1571 3 : hSC_VBR->last_nelp_mode = 0;
1572 3 : move16();
1573 3 : hSC_VBR->pppcountE = 0;
1574 3 : move16();
1575 3 : hSC_VBR->last_ppp_mode = 0;
1576 3 : move16();
1577 3 : hSC_VBR->last_last_ppp_mode = 0;
1578 3 : move16();
1579 3 : hSC_VBR->firstTime_voicedenc = 1;
1580 3 : move16();
1581 3 : hSC_VBR->prev_ppp_gain_pit_fx = 0;
1582 3 : move16();
1583 3 : hSC_VBR->prev_tilt_code_fx = 0;
1584 3 : move16();
1585 :
1586 3 : hSC_VBR->ppp_mode = 0;
1587 3 : move16();
1588 3 : hSC_VBR->nelp_mode = 0;
1589 3 : move16();
1590 :
1591 :
1592 3 : hSC_VBR->pattern_m = 0;
1593 3 : move16();
1594 3 : hSC_VBR->Last_Resort = 0;
1595 3 : move16();
1596 3 : hSC_VBR->Q_to_F = 0;
1597 3 : move16();
1598 :
1599 3 : hSC_VBR->numactive = 0; /* keep the count of the frames inside current 600 frame bloack.*/
1600 3 : move16();
1601 3 : hSC_VBR->sum_of_rates_fx = 0; /* sum of the rates of past 600 active frames*/
1602 3 : move32();
1603 3 : hSC_VBR->global_avr_rate_fx = 0; /* global rate upto current time. recorded a (rate in kbps) *6000*/
1604 3 : move32();
1605 3 : hSC_VBR->global_frame_cnt = 0; /* 600 active frame block count. Used to update the global rate*/
1606 3 : move16();
1607 3 : hSC_VBR->rate_control = 0;
1608 3 : move16();
1609 3 : hSC_VBR->SNR_THLD_fx = SNR_THLD_FX_Q8;
1610 3 : move16();
1611 3 : hSC_VBR->mode_QQF = 1;
1612 3 : move16();
1613 :
1614 3 : hSC_VBR->vbr_generic_ho = 0;
1615 3 : move16();
1616 3 : hSC_VBR->Local_VAD = 0;
1617 3 : move16();
1618 3 : hSC_VBR->Last_Resort = 0;
1619 3 : move16();
1620 3 : hSC_VBR->set_ppp_generic = 0;
1621 3 : move16();
1622 3 : hSC_VBR->last_7k2_coder_type = GENERIC;
1623 3 : move16();
1624 :
1625 3 : set16_fx( hSC_VBR->shape1_filt_mem_fx, 0, 10 );
1626 3 : set16_fx( hSC_VBR->shape2_filt_mem_fx, 0, 10 );
1627 3 : set16_fx( hSC_VBR->shape3_filt_mem_fx, 0, 10 );
1628 3 : set16_fx( hSC_VBR->txlpf1_filt1_mem_fx, 0, 10 );
1629 3 : set16_fx( hSC_VBR->txlpf1_filt2_mem_fx, 0, 10 );
1630 3 : set16_fx( hSC_VBR->txhpf1_filt1_mem_fx, 0, 10 );
1631 3 : set16_fx( hSC_VBR->txhpf1_filt2_mem_fx, 0, 10 );
1632 3 : hSC_VBR->qprevIn_fx = 0;
1633 3 : move16();
1634 3 : hSC_VBR->qprevGain_fx = 0;
1635 3 : move16();
1636 :
1637 3 : return;
1638 : }
|