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