Line data Source code
1 : /*====================================================================================
2 : EVS Codec 3GPP TS26.452 Aug 12, 2021. Version 16.3.0
3 : ====================================================================================*/
4 :
5 : #include <stdint.h>
6 : #include "options.h" /* Compilation switches */
7 : #include "cnst.h" /* Common constants */
8 : #include "rom_com.h" /* Static table prototypes */
9 : #include "prot_fx.h" /* Function prototypes */
10 : #include "prot_fx_enc.h" /* Function prototypes */
11 :
12 :
13 : /*-------------------------------------------------------------------*
14 : * Local constantes
15 : *-------------------------------------------------------------------*/
16 :
17 : #define NB_VOIC_FX 13
18 : #define DIV_NB_VOIC_FX 2521
19 :
20 : #define ALPA_FX 31130
21 : #define ALPAM1_FX ( 32768 - ALPA_FX )
22 :
23 : #define BETA_FX 819
24 : #define AFREQ_THR 2
25 :
26 : #define HANGOVER_DELAY 2
27 :
28 :
29 : /*======================================================================*/
30 : /* FUNCTION : Pit_exc_contribution_len_fx() */
31 : /*----------------------------------------------------------------------*/
32 : /* PURPOSE : Determine up to which band the pit contribution is significant*/
33 : /*----------------------------------------------------------------------*/
34 : /* INPUT ARGUMENTS : */
35 : /* _ (Struct) st_fx : encoder static memory */
36 : /* _ (Word16[]) dct_res : DCT of residual Qnew */
37 : /* _ (Word16[]) dct_pitex : DCT of pitch contribution Qnew */
38 : /* _ (Word16[]) pitch_buf : Pitch per subframe Q6 */
39 : /* _ (Word16[]) nb_subfr : Number of subframe considered */
40 : /* _ (Word16) hangover : hangover for the time contribution switching*/
41 : /* _ (Word16) Qnew : */
42 : /*-----------------------------------------------------------------------*/
43 : /* OUTPUT ARGUMENTS : */
44 : /* _ (Word16[]) dct_res : DCT of residual Qnew */
45 : /* _ (Word16[]) dct_pitex : DCT of pitch contribution Qnew */
46 : /*-----------------------------------------------------------------------*/
47 : /*-----------------------------------------------------------------------*/
48 : /* RETURN ARGUMENTS : */
49 : /* _ None */
50 : /*=======================================================================*/
51 :
52 : /* o : bin where pitch contribution is significant */
53 8 : Word16 Pit_exc_contribution_len_fx(
54 : Encoder_State *st_fx, /* i/o: state structure */
55 : const Word16 *dct_res, /* i : DCT of residual Qnew*/
56 : Word16 *dct_pitex, /* i/o: DCT of pitch contribution Qnew*/
57 : Word16 *pitch_buf, /* i/o: Pitch per subframe Q6*/
58 : const Word16 nb_subfr, /* i : Number of subframe considered Q0*/
59 : Word16 *hangover, /* i : hangover for the time contribution switching Q0*/
60 : Word16 Qnew )
61 : {
62 : Word16 corr_dct_pit[MBANDS_LOC];
63 : Word32 corr_tmp, L_tmp;
64 : Word16 av_corr, min_corr, ftmp, tmp_ex, tmp_res;
65 : Word16 freq, i, j;
66 : Word16 last_pit_band, pit_contr_idx, last_pit_bin;
67 : Word32 ener_res;
68 : Word32 ener_pit;
69 : Word16 low_pit, F1st_harm, F8th_harm;
70 : Word16 corr_dct_pit_tmp[MBANDS_LOC];
71 8 : Word16 time_flg = 0;
72 : Word16 Len, max_len;
73 : Word16 tmp_dec;
74 8 : Word16 Mbands_loc = MBANDS_LOC - 2;
75 : Word16 exp1, tmp, exp2;
76 : Word32 L_tmp1, ener_init;
77 : Word16 exp_norm;
78 : Word16 norm;
79 : Word16 val_thrs;
80 8 : SP_MUS_CLAS_HANDLE hSpMusClas = st_fx->hSpMusClas;
81 8 : BSTR_ENC_HANDLE hBstr = st_fx->hBstr;
82 8 : GSC_ENC_HANDLE hGSCEnc = st_fx->hGSCEnc;
83 8 : move16();
84 8 : move16();
85 :
86 8 : if ( EQ_16( st_fx->L_frame, L_FRAME16k ) )
87 : {
88 0 : Mbands_loc = MBANDS_LOC;
89 0 : move16();
90 : }
91 :
92 8 : minimum_fx( pitch_buf, nb_subfr, &low_pit );
93 8 : exp1 = norm_s( low_pit );
94 8 : tmp = shl( low_pit, exp1 );
95 8 : tmp_dec = 12800;
96 8 : move16();
97 8 : if ( EQ_16( st_fx->L_frame, L_FRAME16k ) )
98 : {
99 0 : tmp_dec = 16000;
100 0 : move16();
101 : }
102 : /*F1st_harm = (12800.0f|160000.f)/low_pit;*/
103 8 : tmp = div_s( tmp_dec, tmp ); /*15-6-exp1(->9-exp1)*/
104 8 : F1st_harm = shr_r( tmp, sub( 5, exp1 ) ); /*Q4*/
105 :
106 : /*F8th_harm = 8.0f*F1st_harm;*/
107 8 : F8th_harm = extract_l( L_shr_r( L_mult0( F1st_harm, 8 ), 2 ) ); /*Q2*/
108 :
109 8 : freq = 0;
110 8 : move16();
111 8 : ener_init = L_shl( 3, 2 * Qnew - 5 ); /*(0.1->3 in Q5) 2*Qnew*/
112 112 : FOR( i = 0; i < Mbands_loc; i++ ) /* up to maximum allowed voiced critical band */
113 : {
114 104 : corr_tmp = L_deposit_l( 0 );
115 104 : ener_res = L_add( ener_init, 0 );
116 104 : ener_pit = L_add( ener_init, 0 );
117 :
118 1640 : FOR( j = 0; j < mfreq_bindiv_loc[i]; j++ ) /* up to maximum allowed voiced critical band */
119 : {
120 1536 : tmp_ex = mult_r( dct_pitex[j + freq], 8192 /* 0.25 in Q15 */ );
121 1536 : tmp_res = mult_r( dct_res[j + freq], 8192 /* 0.25 in Q15 */ );
122 1536 : corr_tmp = L_mac0( corr_tmp, tmp_res, tmp_ex ); /*2*Qnew*/
123 1536 : ener_res = L_mac0( ener_res, tmp_res, tmp_res ); /*2*Qnew*/
124 1536 : ener_pit = L_mac0( ener_pit, tmp_ex, tmp_ex ); /*2*Qnew*/
125 : }
126 :
127 104 : L_tmp1 = Mult_32_32( ener_res, ener_pit );
128 104 : exp2 = norm_l( L_tmp1 );
129 104 : L_tmp1 = L_shl( L_tmp1, exp2 );
130 104 : exp_norm = sub( 30, exp2 );
131 104 : L_tmp1 = Isqrt_lc( L_tmp1, &exp_norm );
132 104 : norm = extract_h( L_tmp1 ); /*15-exp_norm*/
133 104 : L_tmp1 = L_shl( Mult_32_16( corr_tmp, norm ), exp_norm );
134 104 : corr_dct_pit[i] = round_fx( L_shl( L_tmp1, 14 ) ); /*Q14*/
135 :
136 104 : freq = add( freq, mfreq_bindiv_loc[i] );
137 : }
138 :
139 8 : val_thrs = 8192;
140 8 : move16(); /* 0.5 in Q14*/
141 : /* Smooth the inter-correlation value and skip the last band for the average (since last band is almost always 0)*/
142 8 : tmp = mac_r( L_mult( ALPA_FX, corr_dct_pit[0] ), ALPAM1_FX, corr_dct_pit[1] ); /*Qnew*/
143 8 : tmp = s_max( tmp, val_thrs );
144 :
145 8 : corr_dct_pit_tmp[0] = shl( sub( tmp, val_thrs ), 1 );
146 8 : move16();
147 :
148 96 : FOR( i = 1; i < Mbands_loc - 1; i++ ) /* up to maximum allowed voiced critical band */
149 : {
150 88 : L_tmp = L_mult( BETA_FX, corr_dct_pit[i - 1] ); /* Q30 */
151 88 : L_tmp = L_mac( L_tmp, BETA_FX, corr_dct_pit[i + 1] ); /* Q30 */
152 :
153 88 : tmp = mac_r( L_tmp, ALPA_FX, corr_dct_pit[i] ); /* Q30 */
154 88 : tmp = s_max( tmp, val_thrs );
155 :
156 88 : corr_dct_pit_tmp[i] = shl( sub( tmp, val_thrs ), 1 );
157 88 : move16();
158 : }
159 8 : tmp = mac_r( L_mult( ALPA_FX, corr_dct_pit[i] ), ALPAM1_FX, corr_dct_pit[i - 1] ); /*Qnew*/
160 8 : tmp = s_max( tmp, val_thrs );
161 8 : corr_dct_pit_tmp[i] = shl( sub( tmp, val_thrs ), 1 );
162 8 : move16();
163 :
164 8 : Copy( corr_dct_pit_tmp, corr_dct_pit, Mbands_loc );
165 :
166 8 : L_tmp1 = L_mult( DIV_NB_VOIC_FX, corr_dct_pit[0] ); /*Qnew*/
167 104 : FOR( i = 1; i < NB_VOIC_FX; i++ ) /* up to maximum allowed voiced critical band */
168 : {
169 96 : L_tmp1 = L_mac( L_tmp1, DIV_NB_VOIC_FX, corr_dct_pit[i] );
170 : }
171 8 : av_corr = round_fx( L_tmp1 ); /*Qnew*/
172 :
173 : /* Find the cut-off freq similarly to HSX */
174 8 : last_pit_band = 0;
175 8 : move16();
176 8 : av_corr = round_fx( L_shl( L_mult0( av_corr, 6400 ), 16 - 12 ) ); /*Q14*Q0-12=Q2*/
177 :
178 8 : if ( EQ_16( st_fx->L_frame, L_FRAME16k ) )
179 : {
180 : /*av_corr *= 1.25f;*/
181 0 : av_corr = add( av_corr, shr( av_corr, 2 ) ); /* Q2 */
182 : }
183 8 : test();
184 8 : if ( GE_16( st_fx->GSC_IVAS_mode, 1 ) || LT_32( st_fx->core_brate, ACELP_9k60 ) )
185 : {
186 0 : av_corr = shl_sat( av_corr, 1 ); /*Q2 Correlation really poor at low rate, time domain still valide*/
187 : }
188 8 : min_corr = abs_s( sub( mfreq_loc_Q2fx[0], av_corr ) ); /*Q2*/
189 :
190 104 : FOR( i = 1; i < Mbands_loc; i++ ) /* up to maximum allowed voiced critical band */
191 : {
192 96 : ftmp = abs_s( sub( mfreq_loc_Q2fx[i], av_corr ) ); /*Q2*/
193 :
194 96 : IF( LT_16( ftmp, min_corr ) )
195 : {
196 2 : last_pit_band = i;
197 2 : move16();
198 2 : min_corr = ftmp; /* Q2 */
199 2 : move16();
200 : }
201 : }
202 :
203 8 : IF( GT_16( F8th_harm, mfreq_loc_Q2fx[last_pit_band] ) )
204 : {
205 : DO
206 : {
207 65 : last_pit_band = add( last_pit_band, 1 );
208 : }
209 65 : WHILE( GE_16( F8th_harm, mfreq_loc_Q2fx[last_pit_band] ) );
210 : }
211 8 : if ( GE_16( st_fx->GSC_IVAS_mode, 1 ) )
212 : {
213 0 : last_pit_band = s_max( last_pit_band, 7 );
214 : }
215 8 : test();
216 8 : test();
217 8 : test();
218 8 : IF( GT_16( last_pit_band, 7 + BAND1k2 ) && ( LT_32( st_fx->core_brate, CFREQ_BITRATE ) || EQ_16( st_fx->bwidth, NB ) ) ) /*Added for 9.1*/
219 : {
220 0 : last_pit_band = 7 + BAND1k2;
221 0 : move16();
222 : }
223 8 : ELSE IF( GT_16( last_pit_band, 10 + BAND1k2 ) && GE_32( st_fx->core_brate, CFREQ_BITRATE ) )
224 : {
225 0 : last_pit_band = add( 10, BAND1k2 );
226 : }
227 :
228 8 : time_flg = 0;
229 8 : move16();
230 8 : test();
231 8 : test();
232 8 : test();
233 8 : test();
234 8 : test();
235 8 : test();
236 8 : test();
237 8 : test();
238 8 : test();
239 8 : test();
240 8 : IF( ( hGSCEnc->mem_last_pit_band > 0 && GT_16( st_fx->old_corr_fx, 16384 ) && GT_16( hSpMusClas->mold_corr_fx, 16384 ) && GE_16( hGSCEnc->lt_gpitch_fx, 19661 ) /*1.5f*GPIT_THR*/ ) || ( GT_16( last_pit_band, 6 ) ) || ( GE_16( last_pit_band, 4 ) && GE_16( hGSCEnc->lt_gpitch_fx, 19661 ) /*1.5f*GPIT_THR*/ && GT_16( st_fx->old_corr_fx, 22938 ) ) || ( GT_16( last_pit_band, BAND1k2 ) && GT_16( hSpMusClas->mold_corr_fx, 26214 ) && GE_16( hGSCEnc->lt_gpitch_fx, 13107 ) /*GPIT_THR*/ ) )
241 : {
242 8 : tmp_dec = 1;
243 8 : move16();
244 : }
245 : ELSE
246 : {
247 0 : tmp_dec = 0;
248 0 : move16();
249 : }
250 :
251 : /* Different past and current decision */
252 8 : test();
253 8 : test();
254 8 : test();
255 8 : IF( ( hGSCEnc->mem_last_pit_band == 0 && EQ_16( tmp_dec, 1 ) ) || ( hGSCEnc->mem_last_pit_band > 0 && tmp_dec == 0 ) )
256 : {
257 0 : IF( *hangover == 0 )
258 : {
259 0 : time_flg = tmp_dec;
260 0 : move16();
261 0 : *hangover = HANGOVER_DELAY;
262 0 : move16();
263 : }
264 : ELSE
265 : {
266 0 : time_flg = 0;
267 0 : move16();
268 0 : if ( hGSCEnc->mem_last_pit_band > 0 )
269 : {
270 0 : time_flg = 1;
271 0 : move16();
272 : }
273 :
274 0 : ( *hangover ) = sub( ( *hangover ), 1 );
275 0 : if ( *hangover < 0 )
276 : {
277 0 : *hangover = 0;
278 0 : move16();
279 : }
280 : }
281 : }
282 : ELSE
283 : {
284 8 : time_flg = tmp_dec;
285 8 : move16();
286 8 : *hangover = HANGOVER_DELAY;
287 8 : move16();
288 : }
289 :
290 : /* Decicison on final length of time contribution */
291 8 : pit_contr_idx = 0;
292 8 : move16();
293 8 : test();
294 8 : test();
295 8 : IF( EQ_16( time_flg, 1 ) || NE_16( st_fx->coder_type, INACTIVE ) || st_fx->GSC_noisy_speech )
296 : {
297 8 : test();
298 8 : test();
299 : /*if(st_fx->core_brate <ACELP_9k60)*/
300 8 : IF( LT_32( st_fx->core_brate, ACELP_9k60 ) && LT_16( low_pit, 4096 ) )
301 : {
302 0 : last_pit_band = add( 9, BAND1k2 );
303 0 : if ( EQ_16( st_fx->bwidth, NB ) )
304 : {
305 0 : last_pit_band = add( 7, BAND1k2 );
306 : }
307 : }
308 8 : ELSE IF( LT_32( st_fx->core_brate, ACELP_9k60 ) && LT_16( low_pit, 8192 ) )
309 : {
310 0 : last_pit_band = add( 5, BAND1k2 );
311 : }
312 8 : ELSE IF( LT_32( st_fx->core_brate, ACELP_9k60 ) )
313 : {
314 0 : last_pit_band = add( 3, BAND1k2 );
315 : }
316 8 : ELSE IF( LT_16( last_pit_band, add( BAND1k2, 1 ) ) )
317 : {
318 1 : last_pit_band = add( BAND1k2, 1 );
319 : }
320 8 : last_pit_bin = mfreq_loc_div_25[last_pit_band]; /* Q0 */
321 8 : move16();
322 :
323 8 : st_fx->bpf_off = 0;
324 8 : move16();
325 :
326 8 : max_len = sub( st_fx->L_frame, last_pit_bin ); /* Q0 */
327 :
328 8 : if ( EQ_16( st_fx->bwidth, NB ) )
329 : {
330 0 : max_len = sub( 160, last_pit_bin );
331 : }
332 :
333 8 : Len = 80;
334 8 : move16();
335 8 : if ( LT_16( max_len, 80 ) )
336 : {
337 1 : Len = max_len;
338 1 : move16();
339 : }
340 8 : test();
341 8 : IF( ( EQ_32( st_fx->core_brate, ACELP_8k00 ) ) && ( NE_16( st_fx->bwidth, NB ) ) )
342 : {
343 0 : move16(); /*ptr init*/
344 0 : FOR( i = 0; i < max_len; i++ )
345 : {
346 0 : dct_pitex[i + last_pit_bin] = 0;
347 0 : move16();
348 : }
349 : }
350 : ELSE
351 : {
352 :
353 617 : FOR( i = 0; i < Len; i++ )
354 : {
355 609 : dct_pitex[i + last_pit_bin] = mult_r( dct_pitex[i + last_pit_bin], sm_table_fx[i] ); /* Q_new */
356 : }
357 351 : FOR( ; i < max_len; i++ )
358 : {
359 343 : dct_pitex[i + last_pit_bin] = 0;
360 343 : move16();
361 : }
362 : }
363 8 : hGSCEnc->mem_last_pit_band = last_pit_band; /* Q0 */
364 8 : move16();
365 8 : pit_contr_idx = sub( last_pit_band, BAND1k2 );
366 : }
367 : ELSE
368 : {
369 0 : set16_fx( dct_pitex, 0, st_fx->L_frame );
370 0 : st_fx->bpf_off = 1;
371 0 : move16();
372 0 : last_pit_bin = 0;
373 0 : move16();
374 0 : last_pit_band = 0;
375 0 : move16();
376 0 : pit_contr_idx = 0;
377 0 : move16();
378 0 : hGSCEnc->mem_last_pit_band = 0;
379 0 : move16();
380 :
381 0 : set16_fx( pitch_buf, shl( L_SUBFR, 6 ), NB_SUBFR16k );
382 :
383 : /* pitch contribution useless - delete all previously written indices belonging to pitch contribution */
384 0 : for ( i = TAG_ACELP_SUBFR_LOOP_START; i < TAG_ACELP_SUBFR_LOOP_END; i++ )
385 : {
386 0 : delete_indice( hBstr, i );
387 : }
388 :
389 0 : delete_indice( hBstr, IND_ES_PRED );
390 : }
391 8 : IF( LT_32( st_fx->core_brate, CFREQ_BITRATE ) )
392 : {
393 0 : IF( LT_32( st_fx->core_brate, ACELP_9k60 ) )
394 : {
395 0 : if ( pit_contr_idx > 0 )
396 : {
397 0 : pit_contr_idx = 1;
398 0 : move16();
399 : }
400 :
401 0 : IF( EQ_16( st_fx->coder_type, INACTIVE ) )
402 : {
403 0 : push_indice( hBstr, IND_PIT_CONTR_IDX, pit_contr_idx, 1 );
404 : }
405 : }
406 : ELSE
407 : {
408 0 : push_indice( hBstr, IND_PIT_CONTR_IDX, pit_contr_idx, 3 );
409 : }
410 : }
411 : ELSE
412 : {
413 8 : push_indice( hBstr, IND_PIT_CONTR_IDX, pit_contr_idx, 4 );
414 : }
415 :
416 8 : return last_pit_bin;
417 : }
418 :
419 :
420 15087 : Word16 Pit_exc_contribution_len_ivas_fx( /* o : bin where pitch contribution is significant */
421 : Encoder_State *st_fx, /* i/o: state structure */
422 : const Word16 *dct_res, /* i : DCT of residual Q_new*/
423 : Word16 *dct_pitex, /* i/o: DCT of pitch contribution Q_new*/
424 : Word16 *pitch_buf, /* i/o: Pitch per subframe Q6*/
425 : const Word16 nb_subfr, /* i : Number of subframe considered Q0*/
426 : Word16 *hangover, /* i : hangover for the time contribution switching Q0*/
427 : Word16 Qnew )
428 : {
429 :
430 : Word16 corr_dct_pit[MBANDS_LOC];
431 : Word32 corr_tmp, L_tmp;
432 : Word16 av_corr, min_corr, ftmp, tmp_ex, tmp_res;
433 : Word16 freq, i, j;
434 : Word16 last_pit_band, pit_contr_idx, last_pit_bin;
435 : Word32 ener_res;
436 : Word32 ener_pit;
437 : Word16 low_pit, F1st_harm, F8th_harm;
438 : Word16 corr_dct_pit_tmp[MBANDS_LOC];
439 15087 : Word16 time_flg = 0;
440 : Word16 Len, max_len;
441 : Word16 tmp_dec;
442 15087 : Word16 Mbands_loc = MBANDS_LOC - 2;
443 : Word16 exp1, tmp, exp2;
444 : Word32 L_tmp1, ener_init;
445 : Word16 exp_norm;
446 : Word16 norm;
447 : Word16 val_thrs;
448 15087 : SP_MUS_CLAS_HANDLE hSpMusClas = st_fx->hSpMusClas;
449 15087 : BSTR_ENC_HANDLE hBstr = st_fx->hBstr;
450 15087 : GSC_ENC_HANDLE hGSCEnc = st_fx->hGSCEnc;
451 15087 : move16();
452 :
453 15087 : if ( EQ_16( st_fx->L_frame, L_FRAME16k ) )
454 : {
455 2659 : Mbands_loc = MBANDS_LOC;
456 2659 : move16();
457 : }
458 :
459 15087 : minimum_fx( pitch_buf, nb_subfr, &low_pit );
460 15087 : exp1 = norm_s( low_pit );
461 15087 : tmp = shl( low_pit, exp1 );
462 15087 : tmp_dec = 12800;
463 15087 : move16();
464 15087 : if ( EQ_16( st_fx->L_frame, L_FRAME16k ) )
465 : {
466 2659 : tmp_dec = 16000;
467 2659 : move16();
468 : }
469 : /*F1st_harm = (12800.0f|160000.f)/low_pit;*/
470 15087 : tmp = div_s( tmp_dec, tmp ); /*15-6-exp1(->9-exp1)*/
471 15087 : F1st_harm = shr_r( tmp, sub( 5, exp1 ) ); /*Q4*/
472 :
473 : /*F8th_harm = 8.0f*F1st_harm;*/
474 15087 : F8th_harm = extract_l( L_shr_r( L_mult0( F1st_harm, 8 ), 2 ) ); /*Q2*/
475 :
476 15087 : freq = 0;
477 15087 : move16();
478 15087 : ener_init = L_shl( 3, 2 * Qnew - 5 ); /*(0.1->3 in Q5) 2*Qnew*/
479 216536 : FOR( i = 0; i < Mbands_loc; i++ ) /* up to maximum allowed voiced critical band */
480 : {
481 201449 : corr_tmp = L_deposit_l( 0 );
482 201449 : ener_res = L_add( ener_init, 0 );
483 201449 : ener_pit = L_add( ener_init, 0 );
484 :
485 3183241 : FOR( j = 0; j < mfreq_bindiv_loc[i]; j++ ) /* up to maximum allowed voiced critical band */
486 : {
487 2981792 : tmp_ex = mult_r( dct_pitex[j + freq], 8192 );
488 2981792 : tmp_res = mult_r( dct_res[j + freq], 8192 );
489 2981792 : corr_tmp = L_mac0( corr_tmp, tmp_res, tmp_ex ); /*2*Qnew*/
490 2981792 : ener_res = L_mac0( ener_res, tmp_res, tmp_res ); /*2*Qnew*/
491 2981792 : ener_pit = L_mac0( ener_pit, tmp_ex, tmp_ex ); /*2*Qnew*/
492 : }
493 :
494 201449 : L_tmp1 = Mult_32_32( ener_res, ener_pit );
495 201449 : exp2 = norm_l( L_tmp1 );
496 201449 : L_tmp1 = L_shl( L_tmp1, exp2 );
497 201449 : exp_norm = sub( 30, exp2 );
498 201449 : L_tmp1 = Isqrt_lc( L_tmp1, &exp_norm );
499 201449 : norm = extract_h( L_tmp1 ); /*15-exp_norm*/
500 201449 : L_tmp1 = L_shl( Mult_32_16( corr_tmp, norm ), exp_norm );
501 201449 : corr_dct_pit[i] = round_fx( L_shl( L_tmp1, 14 ) ); /*Q14*/
502 :
503 201449 : freq = add( freq, mfreq_bindiv_loc[i] );
504 : }
505 :
506 15087 : val_thrs = 8192;
507 15087 : move16(); /* 0.5 in Q14*/
508 : /* Smooth the inter-correlation value and skip the last band for the average (since last band is almost always 0)*/
509 15087 : tmp = mac_r( L_mult( ALPA_FX, corr_dct_pit[0] ), ALPAM1_FX, corr_dct_pit[1] ); /*Qnew*/
510 15087 : tmp = s_max( tmp, val_thrs ); /*Qnew */
511 :
512 15087 : corr_dct_pit_tmp[0] = shl( sub( tmp, val_thrs ), 1 ); /* Q_new + 1 */
513 15087 : move16();
514 :
515 186362 : FOR( i = 1; i < Mbands_loc - 1; i++ ) /* up to maximum allowed voiced critical band */
516 : {
517 171275 : L_tmp = L_mult( BETA_FX, corr_dct_pit[i - 1] ); /* Q_new + 16 */
518 171275 : L_tmp = L_mac( L_tmp, BETA_FX, corr_dct_pit[i + 1] ); /* Q_new + 16 */
519 :
520 171275 : tmp = mac_r( L_tmp, ALPA_FX, corr_dct_pit[i] ); /* Q_new */
521 171275 : tmp = s_max( tmp, val_thrs ); /* Q_new */
522 :
523 171275 : corr_dct_pit_tmp[i] = shl( sub( tmp, val_thrs ), 1 ); /* Q_new + 1 */
524 171275 : move16();
525 : }
526 15087 : tmp = mac_r( L_mult( ALPA_FX, corr_dct_pit[i] ), ALPAM1_FX, corr_dct_pit[i - 1] ); /*Qnew*/
527 15087 : tmp = s_max( tmp, val_thrs ); /* Q_new */
528 15087 : corr_dct_pit_tmp[i] = shl( sub( tmp, val_thrs ), 1 ); /* Q_new + 1 */
529 15087 : move16();
530 :
531 15087 : Copy( corr_dct_pit_tmp, corr_dct_pit, Mbands_loc ); /* Q_new + 1 */
532 :
533 15087 : L_tmp1 = L_mult( DIV_NB_VOIC_FX, corr_dct_pit[0] ); /*Qnew*/
534 196131 : FOR( i = 1; i < NB_VOIC_FX; i++ ) /* up to maximum allowed voiced critical band */
535 : {
536 181044 : L_tmp1 = L_mac( L_tmp1, DIV_NB_VOIC_FX, corr_dct_pit[i] ); /* Q_new + 16 */
537 : }
538 15087 : av_corr = round_fx( L_tmp1 ); /*Qnew*/
539 :
540 : /* Find the cut-off freq similarly to HSX */
541 15087 : last_pit_band = 0;
542 15087 : move16();
543 15087 : av_corr = round_fx( L_shl( L_mult0( av_corr, 6400 ), 16 - 12 ) ); /*Q14*Q0-12=Q2*/
544 :
545 15087 : if ( EQ_16( st_fx->L_frame, L_FRAME16k ) )
546 : {
547 : /*av_corr *= 1.25f;*/
548 2659 : av_corr = add( av_corr, shr( av_corr, 2 ) );
549 : }
550 15087 : test();
551 15087 : if ( GE_16( st_fx->GSC_IVAS_mode, 1 ) || LT_32( st_fx->core_brate, ACELP_9k60 ) )
552 : {
553 10408 : av_corr = shl_sat( av_corr, 1 ); /*Q2 Correlation really poor at low rate, time domain still valide*/
554 : }
555 15087 : min_corr = abs_s( sub( mfreq_loc_Q2fx[0], av_corr ) ); /*Q2*/
556 :
557 201449 : FOR( i = 1; i < Mbands_loc; i++ ) /* up to maximum allowed voiced critical band */
558 : {
559 186362 : ftmp = abs_s( sub( mfreq_loc_Q2fx[i], av_corr ) ); /*Q2*/
560 :
561 186362 : IF( LT_16( ftmp, min_corr ) )
562 : {
563 31846 : last_pit_band = i;
564 31846 : move16();
565 31846 : min_corr = ftmp;
566 31846 : move16();
567 : }
568 : }
569 :
570 15087 : IF( GT_16( F8th_harm, mfreq_loc_Q2fx[last_pit_band] ) )
571 : {
572 : DO
573 : {
574 60290 : last_pit_band = add( last_pit_band, 1 );
575 : }
576 60290 : WHILE( GE_16( F8th_harm, mfreq_loc_Q2fx[last_pit_band] ) );
577 : }
578 15087 : if ( GE_16( st_fx->GSC_IVAS_mode, 1 ) )
579 : {
580 4805 : last_pit_band = s_max( last_pit_band, 7 );
581 : }
582 15087 : test();
583 15087 : test();
584 15087 : test();
585 15087 : IF( GT_16( last_pit_band, 7 + BAND1k2 ) && ( LT_32( st_fx->core_brate, CFREQ_BITRATE ) || EQ_16( st_fx->bwidth, NB ) ) ) /*Added for 9.1*/
586 : {
587 1333 : last_pit_band = 7 + BAND1k2;
588 1333 : move16();
589 : }
590 13754 : ELSE IF( GT_16( last_pit_band, 10 + BAND1k2 ) && GE_32( st_fx->core_brate, CFREQ_BITRATE ) )
591 : {
592 243 : last_pit_band = add( 10, BAND1k2 );
593 : }
594 :
595 15087 : time_flg = 0;
596 15087 : move16();
597 15087 : test();
598 15087 : test();
599 15087 : test();
600 15087 : test();
601 15087 : test();
602 15087 : test();
603 15087 : test();
604 15087 : test();
605 15087 : test();
606 15087 : test();
607 15087 : IF( ( hGSCEnc->mem_last_pit_band > 0 && GT_16( st_fx->old_corr_fx, 16384 ) && GT_16( hSpMusClas->mold_corr_fx, 16384 ) && GE_16( hGSCEnc->lt_gpitch_fx, 19661 ) /*1.5f*GPIT_THR*/ ) || ( GT_16( last_pit_band, 6 ) ) || ( GE_16( last_pit_band, 4 ) && GE_16( hGSCEnc->lt_gpitch_fx, 19661 ) /*1.5f*GPIT_THR*/ && GT_16( st_fx->old_corr_fx, 22938 ) ) || ( GT_16( last_pit_band, BAND1k2 ) && GT_16( hSpMusClas->mold_corr_fx, 26214 ) && GE_16( hGSCEnc->lt_gpitch_fx, 13107 ) /*GPIT_THR*/ ) )
608 : {
609 10173 : tmp_dec = 1;
610 10173 : move16();
611 : }
612 : ELSE
613 : {
614 4914 : tmp_dec = 0;
615 4914 : move16();
616 : }
617 :
618 : /* Different past and current decision */
619 15087 : test();
620 15087 : test();
621 15087 : test();
622 15087 : IF( ( hGSCEnc->mem_last_pit_band == 0 && EQ_16( tmp_dec, 1 ) ) || ( hGSCEnc->mem_last_pit_band > 0 && tmp_dec == 0 ) )
623 : {
624 4511 : IF( *hangover == 0 )
625 : {
626 833 : time_flg = tmp_dec;
627 833 : move16();
628 833 : *hangover = HANGOVER_DELAY;
629 833 : move16();
630 : }
631 : ELSE
632 : {
633 3678 : time_flg = 0;
634 3678 : move16();
635 3678 : if ( hGSCEnc->mem_last_pit_band > 0 )
636 : {
637 2887 : time_flg = 1;
638 2887 : move16();
639 : }
640 :
641 3678 : ( *hangover ) = sub( ( *hangover ), 1 ); /* Q0 */
642 3678 : if ( *hangover < 0 )
643 : {
644 0 : *hangover = 0;
645 0 : move16();
646 : }
647 : }
648 : }
649 : ELSE
650 : {
651 10576 : time_flg = tmp_dec;
652 10576 : move16();
653 10576 : *hangover = HANGOVER_DELAY;
654 10576 : move16();
655 : }
656 :
657 : /* Decicison on final length of time contribution */
658 15087 : pit_contr_idx = 0;
659 15087 : move16();
660 15087 : test();
661 15087 : test();
662 15087 : IF( EQ_16( time_flg, 1 ) || NE_16( st_fx->coder_type, INACTIVE ) || st_fx->GSC_noisy_speech )
663 : {
664 12778 : test();
665 12778 : test();
666 : /*if(st_fx->core_brate <ACELP_9k60)*/
667 12778 : IF( LT_32( st_fx->core_brate, ACELP_9k60 ) && LT_16( low_pit, 4096 /* 64 in Q6 */ ) )
668 : {
669 3325 : last_pit_band = add( 9, BAND1k2 );
670 3325 : if ( EQ_16( st_fx->bwidth, NB ) )
671 : {
672 0 : last_pit_band = add( 7, BAND1k2 );
673 : }
674 : }
675 9453 : ELSE IF( LT_32( st_fx->core_brate, ACELP_9k60 ) && LT_16( low_pit, 8192 /* 128 in Q6 */ ) )
676 : {
677 2362 : last_pit_band = add( 5, BAND1k2 );
678 : }
679 7091 : ELSE IF( LT_32( st_fx->core_brate, ACELP_9k60 ) )
680 : {
681 1822 : last_pit_band = add( 3, BAND1k2 );
682 : }
683 5269 : ELSE IF( LT_16( last_pit_band, add( BAND1k2, 1 ) ) )
684 : {
685 717 : last_pit_band = add( BAND1k2, 1 );
686 : }
687 12778 : last_pit_bin = mfreq_loc_div_25[last_pit_band]; /* Q15 */
688 12778 : move16();
689 :
690 12778 : st_fx->bpf_off = 0;
691 12778 : move16();
692 :
693 12778 : max_len = sub( st_fx->L_frame, last_pit_bin );
694 :
695 12778 : if ( EQ_16( st_fx->bwidth, NB ) )
696 : {
697 0 : max_len = sub( 160, last_pit_bin );
698 : }
699 :
700 12778 : Len = 80;
701 12778 : move16();
702 12778 : if ( LT_16( max_len, 80 ) )
703 : {
704 3538 : Len = max_len;
705 3538 : move16();
706 : }
707 12778 : test();
708 12778 : IF( ( EQ_32( st_fx->core_brate, ACELP_8k00 ) ) && ( NE_16( st_fx->bwidth, NB ) ) )
709 : {
710 43 : move16(); /*ptr init*/
711 4022 : FOR( i = 0; i < max_len; i++ )
712 : {
713 3979 : dct_pitex[i + last_pit_bin] = 0;
714 3979 : move16();
715 : }
716 : }
717 : ELSE
718 : {
719 :
720 976663 : FOR( i = 0; i < Len; i++ )
721 : {
722 963928 : dct_pitex[i + last_pit_bin] = mult_r( dct_pitex[i + last_pit_bin], sm_table_fx[i] ); /* Q_new */
723 : }
724 666390 : FOR( ; i < max_len; i++ )
725 : {
726 653655 : dct_pitex[i + last_pit_bin] = 0;
727 653655 : move16();
728 : }
729 : }
730 12778 : hGSCEnc->mem_last_pit_band = last_pit_band;
731 12778 : move16();
732 12778 : pit_contr_idx = sub( last_pit_band, BAND1k2 );
733 : }
734 : ELSE
735 : {
736 2309 : set16_fx( dct_pitex, 0, st_fx->L_frame );
737 2309 : st_fx->bpf_off = 1;
738 2309 : move16();
739 2309 : last_pit_bin = 0;
740 2309 : move16();
741 2309 : last_pit_band = 0;
742 2309 : move16();
743 2309 : pit_contr_idx = 0;
744 2309 : move16();
745 2309 : hGSCEnc->mem_last_pit_band = 0;
746 2309 : move16();
747 :
748 2309 : set16_fx( pitch_buf, shl( L_SUBFR, 6 ), NB_SUBFR16k );
749 : /* pitch contribution useless - delete all previously written indices belonging to pitch contribution */
750 695009 : FOR( i = TAG_ACELP_SUBFR_LOOP_START; i < TAG_ACELP_SUBFR_LOOP_END; i++ )
751 : {
752 692700 : delete_indice( hBstr, i );
753 : }
754 2309 : delete_indice( hBstr, IND_ES_PRED );
755 : }
756 15087 : IF( LT_32( st_fx->core_brate, CFREQ_BITRATE ) )
757 : {
758 9710 : IF( LT_32( st_fx->core_brate, ACELP_9k60 ) )
759 : {
760 7640 : if ( pit_contr_idx > 0 )
761 : {
762 7509 : pit_contr_idx = 1;
763 7509 : move16();
764 : }
765 :
766 7640 : IF( EQ_16( st_fx->coder_type, INACTIVE ) )
767 : {
768 312 : push_indice( hBstr, IND_PIT_CONTR_IDX, pit_contr_idx, 1 );
769 : }
770 : }
771 : ELSE
772 : {
773 2070 : push_indice( hBstr, IND_PIT_CONTR_IDX, pit_contr_idx, 3 );
774 : }
775 : }
776 : ELSE
777 : {
778 5377 : push_indice( hBstr, IND_PIT_CONTR_IDX, pit_contr_idx, 4 );
779 : }
780 :
781 15087 : return last_pit_bin;
782 : }
|