Line data Source code
1 : /*====================================================================================
2 : EVS Codec 3GPP TS26.452 Aug 12, 2021. Version 16.3.0
3 : ====================================================================================*/
4 : #include <stdint.h>
5 : #include "options.h" /* Compilation switches */
6 : #include "prot_fx.h" /* Function prototypes */
7 : #include "rom_com.h" /* Function prototypes */
8 : #include "rom_dec.h"
9 :
10 :
11 : /*--------------------------------------------------------------------------*
12 : * Local constants
13 : *--------------------------------------------------------------------------*/
14 :
15 : #define NUMSF 8
16 : #define NUMSF_M1 ( NUMSF - 1 )
17 : #define NUMSF_M2 ( NUMSF - 2 )
18 : #define NUMSF_S2 ( NUMSF / 2 )
19 : #define LOG2_NUMSF 3
20 :
21 : /*--------------------------------------------------------------------------*
22 : * preecho_sb()
23 : *
24 : * Time-domain sub-band based pre-echo reduction
25 : *--------------------------------------------------------------------------*/
26 :
27 7516 : void preecho_sb_fx(
28 : const Word32 core_brate, /* i Q0 : core bit-rate */
29 : Word32 *wtda_audio_fx, /* i q_sig32 : imdct signal, used to compute imdct_mem_fx when not 24400 bps */
30 : Word16 q_sig32, /* i Q value for wtda_audio_fx */
31 : Word16 *rec_sig_fx, /* i q_sig16 : reconstructed signal, output of the imdct transform */
32 : Word16 q_sig16, /* i Q value for rec_sig_fx and imdct_mem_fx */
33 : const Word16 framelength, /* i Q0 : frame length */
34 : Word16 *memfilt_lb_fx, /* i/o Q0 : memory */
35 : Word32 *mean_prev_hb_fx, /* i/o Q0 : memory */
36 : Word16 *smoothmem_fx, /* i/o Q15 : memory */
37 : Word32 *mean_prev_fx, /* i/o Q0 : memory */
38 : Word32 *mean_prev_nc_fx, /* i/o Q0 : memory */
39 : Word16 *wmold_hb_fx, /* i/o Q15 : memory */
40 : Word16 *prevflag, /* i/o Q0 : flag */
41 : Word16 *pastpre, /* i/o Q0 : flag */
42 : const Word16 bwidth /* i Q0 : bandwidth */
43 : )
44 : {
45 : Word16 i, j, len3xLp20;
46 : Word16 zcr[9]; /* 0..3 (0..7): zero crossing of the 4 (8) subframes, 4..5: (8..10) zero crossing of the future subframes */
47 : Word16 maxnzcr[8], cntnzcr; /* max number of samples without zero crossing */
48 :
49 : Word16 maxind, stind, stind_hb, cnt2, cnt5, adv, advmem;
50 : Word16 ind2, ind3, ind4, ind5, ind6, pluslim, ind2_m1, ind2_sfl, numsf_ind2;
51 : Word16 subframelength, subsubframelength;
52 : Word16 *ptr_fx, *fxptr1, *fxptr2, *fxptr3, *fxptr4, *fxptr5, *fxptr6 /*, *fxptr7, *fxptr8*/;
53 : Word32 *fx32ptr1, *fx32ptr4, *fx32ptr5, *fx32ptr6;
54 : Word16 *sptr1, *sptr2, sptr1_loc, sptr2_loc;
55 : Word16 framelength_m1;
56 : Word16 limzcr, limmaxnzcr;
57 : Word16 num_subsubframes, log2_num_subsubframes;
58 : Word16 nb_flag, smooth_len;
59 : Word16 firstnzcr;
60 : Word16 invsmoothlenp1_fx;
61 : Word16 subframelength_s2, subframelength_s34;
62 : Word16 tmp_fx1, tmp_fx2, tmp_fx3;
63 : Word32 tmp_fxL1, tmp_fxL2, tmp_fxL3;
64 : Word32 es_mdct_fx[9]; /* 0..3 (0..7): energy of the 4 (8) subframes, 4..5: (8..10) energy of the future subframes */
65 : Word32 es_mdct_hb_fx[9]; /* 0..3 (0..7): energy of the 4 (8) subframes, 4..5: (8..10) energy of the future subframes */
66 : Word32 es_mdct_half_fx[9];
67 : Word32 es_mdct_quart_fx[9];
68 : Word32 savehalfe_fx, last2_fx, maxcrit_fx, sum_plus_es_fx, mean_plus_es_fx[65];
69 : Word32 savehalfe_hb_fx, last2_hb_fx;
70 : Word32 plus_es_mdct_fx[64], max_es_fx, max_es_hb_fx, max_plus_es_mdct_fx;
71 : Word16 imdct_mem_fx[L_FRAME48k]; /* memory of the imdct transform, used in the next frame */
72 : Word16 rec_sig_lb_fx[L_FRAME48k], rec_sig_hb_fx[L_FRAME48k]; /* 960 max frame length at 48 kHz */
73 :
74 : Word16 min_g_fx[13], g_fx, gt_fx[13];
75 : Word16 min_g_hb_fx[13], gt_hb_fx[13];
76 : Word16 preechogain_fx[L_FRAME48k + PREECHO_SMOOTH_LEN];
77 : Word16 preechogain_hb_fx[L_FRAME48k];
78 : Word16 pre_g_ch_tab[9];
79 : Word32 eshbmean2_fx, eshbmean3_fx, sxyhb2_fx, sxylb3_fx;
80 : Word16 wmold_fx;
81 : Word16 lim16_fx, lim32_fx;
82 : Word16 fattnext_fx;
83 : Word16 oldgain_fx, oldgain_hb_fx;
84 : UWord16 tmp_u16;
85 : Word32 mean_prev_hb_fx_loc, mean_prev_nc_fx_loc, mean_prev_fx_loc; /* */
86 : Word16 q16p1, qmemp1, qtmp;
87 7516 : Word16 shift_q = sub( 15, q_sig32 );
88 :
89 7516 : q16p1 = add( q_sig16, 1 );
90 7516 : qmemp1 = q16p1;
91 :
92 7516 : IF( LE_32( core_brate, HQ_32k ) )
93 : {
94 :
95 4914 : mean_prev_fx_loc = L_add( *mean_prev_fx, 0 );
96 4914 : mean_prev_hb_fx_loc = L_shl_sat( *mean_prev_hb_fx, shl( q_sig16, 1 ) ); /*Q0 to q_sig16*/
97 4914 : mean_prev_nc_fx_loc = L_add( *mean_prev_nc_fx, 0 );
98 4914 : framelength_m1 = sub( framelength, 1 );
99 4914 : nb_flag = 0;
100 4914 : move16();
101 4914 : if ( ( bwidth == NB ) )
102 : {
103 0 : nb_flag = 1;
104 0 : move16();
105 : }
106 4914 : limzcr = 16;
107 4914 : move16();
108 4914 : smooth_len = 4;
109 4914 : move16();
110 4914 : invsmoothlenp1_fx = 6554; /*0.2 in Q15*/
111 4914 : move16();
112 4914 : IF( EQ_16( nb_flag, 1 ) )
113 : {
114 0 : limzcr = 10;
115 0 : move16();
116 0 : smooth_len = PREECHO_SMOOTH_LEN;
117 0 : move16();
118 0 : invsmoothlenp1_fx = INV_PREECHO_SMOOTH_LENP1_FX;
119 0 : move16();
120 : }
121 :
122 4914 : limmaxnzcr = mult( framelength, 1365 ); /*1/24 in Q15*/ /*Q0*/
123 4914 : num_subsubframes = 8;
124 4914 : move16();
125 4914 : log2_num_subsubframes = 3;
126 4914 : move16();
127 :
128 4914 : IF( EQ_16( framelength, L_FRAME8k ) )
129 : {
130 0 : num_subsubframes = 4;
131 0 : move16();
132 0 : log2_num_subsubframes = 2;
133 0 : move16();
134 : }
135 :
136 4914 : len3xLp20 = mult_r( framelength, 7168 ); /*7*framelength/32;*/ /*Q0*/
137 : /* len3xLp20 = framelength/2-(short)((float)framelength*N_ZERO_MDCT/FRAME_SIZE_MS); in float*/
138 :
139 4914 : fxptr1 = imdct_mem_fx;
140 914494 : FOR( i = 0; i < len3xLp20; i++ )
141 : {
142 909580 : *fxptr1++ = negate( extract_h( L_shl_sat( wtda_audio_fx[len3xLp20 - 1 - i], shift_q ) ) ); /*Q-1*/
143 909580 : move16(); /*convert to Word16 Q-1 with saturation (saturation not a problem here) */
144 : }
145 2083954 : FOR( i = 0; i < framelength >> 1; i++ )
146 : {
147 2079040 : *fxptr1++ = negate( extract_h( L_shl_sat( wtda_audio_fx[i], shift_q ) ) ); /*Q-1*/
148 2079040 : move16(); /*convert to Word16 Q-1 with saturation (saturation not a problem here) */
149 : }
150 :
151 4914 : qmemp1 = 0; /*already in q-1*/
152 4914 : move16();
153 :
154 4914 : subframelength = shr( framelength, LOG2_NUMSF ); /*Q0*/
155 4914 : subsubframelength = shr( subframelength, log2_num_subsubframes ); /*Q0*/
156 4914 : wmold_fx = *smoothmem_fx; /*Q15*/
157 4914 : move16();
158 4914 : subframelength_s2 = shr( subframelength, 1 ); /*Q0*/
159 4914 : subframelength_s34 = mult( subframelength, 24576 /*3/4 in Q15*/ ); /*Q0*/
160 :
161 4914 : cntnzcr = -1;
162 4914 : move16();
163 :
164 4914 : lim16_fx = 3277; /*Q15*/
165 4914 : move16();
166 4914 : lim32_fx = 328; /*Q15*/
167 4914 : move16();
168 4914 : savehalfe_fx = L_deposit_l( 0 );
169 4914 : savehalfe_hb_fx = L_deposit_l( 0 );
170 :
171 4914 : IF( *pastpre == 0 )
172 : {
173 : /* if past frame mean energies are not known (no preecho_sb in the past frame), limit max attenuation to 1*/
174 679 : lim16_fx = 32767; /*Q15*/
175 679 : move16();
176 679 : lim32_fx = 32767; /*Q15*/
177 679 : move16();
178 : }
179 :
180 4914 : *pastpre = 2;
181 4914 : move16();
182 4914 : fxptr1 = rec_sig_lb_fx; /*q_sig16*/
183 4914 : fxptr2 = rec_sig_fx; /*q_sig16*/
184 4914 : fxptr3 = rec_sig_fx + 1; /*q_sig16*/
185 4914 : fxptr4 = rec_sig_fx + 2; /*q_sig16*/
186 :
187 4914 : tmp_fxL1 = L_mult( shl_sat( *memfilt_lb_fx, q_sig16 ), 8192 ); /* *memfilt_lb_fx in q0 */
188 4914 : tmp_fxL1 = L_mac( tmp_fxL1, *fxptr3, 8192 /*Q15*/ ); /*Q16*/
189 4914 : *fxptr1 = mac_r( tmp_fxL1, *fxptr2, 16384 /*Q15*/ ); /*Q0*/
190 4914 : move16();
191 4914 : fxptr1++;
192 :
193 4153166 : FOR( j = 2; j < framelength; j++ )
194 : {
195 4148252 : tmp_fxL1 = L_mult( *fxptr2, 8192 /*Q15*/ ); /*Q16*/
196 4148252 : tmp_fxL1 = L_mac( tmp_fxL1, *fxptr4, 8192 /*Q15*/ ); /*Q16*/
197 4148252 : *fxptr1 = mac_r( tmp_fxL1, *fxptr3, 16384 /*Q15*/ ); /*Q0*/
198 4148252 : move16();
199 4148252 : fxptr1++;
200 4148252 : fxptr2++;
201 4148252 : fxptr3++;
202 4148252 : fxptr4++;
203 : }
204 :
205 4914 : tmp_fxL1 = L_mult( *fxptr2, 8192 ); /*Q16*/
206 4914 : *fxptr1 = mac_r( tmp_fxL1, *fxptr3, 16384 ); /*Q0*/
207 4914 : move16();
208 4914 : fxptr1 = rec_sig_lb_fx; /*q_sig16*/
209 4914 : fxptr2 = rec_sig_fx; /*q_sig16*/
210 4914 : fxptr3 = rec_sig_hb_fx; /*q_sig16*/
211 :
212 4162994 : FOR( j = 0; j < framelength; j++ )
213 : {
214 4158080 : *fxptr3 = sub( *fxptr2, *fxptr1 );
215 4158080 : move16();
216 4158080 : fxptr1++;
217 4158080 : fxptr2++;
218 4158080 : fxptr3++;
219 : }
220 :
221 4914 : fxptr2--;
222 4914 : *memfilt_lb_fx = shr_sat( *fxptr2, q_sig16 ); /*Q0*/
223 4914 : move16(); /* *memfilt_lb_fx in q0 */
224 :
225 : /* energy of low bands 8 present and 1 future sub-frames */
226 4914 : sptr1 = zcr; /*Q0*/
227 4914 : sptr1_loc = 0;
228 4914 : move16();
229 4914 : sptr2 = maxnzcr; /*Q0*/
230 :
231 4914 : fxptr2 = rec_sig_fx; /*q_sig16*/
232 4914 : fxptr3 = rec_sig_hb_fx; /*q_sig16*/
233 4914 : fx32ptr1 = es_mdct_fx;
234 4914 : fx32ptr5 = es_mdct_half_fx;
235 4914 : fx32ptr6 = es_mdct_quart_fx;
236 4914 : fx32ptr4 = es_mdct_hb_fx;
237 4914 : firstnzcr = 0;
238 4914 : move16();
239 44226 : FOR( j = 0; j < NUMSF; j++ ) /* 8 present subframes */
240 : {
241 39312 : tmp_fx2 = sub( j, 1 );
242 39312 : tmp_fx1 = shr_sat( *fxptr2, q16p1 ); /*q-1 to avoisd saturation in energy*/
243 39312 : tmp_fxL1 = L_mac0_sat( 25, tmp_fx1, tmp_fx1 ); /*2*(Q-1)*/
244 39312 : tmp_fxL2 = L_mac0_sat( 100, *fxptr3, *fxptr3 ); /*2*(q_sig16)*/
245 39312 : sptr2_loc = 0;
246 39312 : move16();
247 :
248 39312 : fxptr2++;
249 39312 : fxptr3++;
250 :
251 4158080 : FOR( i = 1; i < subframelength; i++ )
252 : {
253 4118768 : if ( EQ_16( i, subframelength_s2 ) )
254 : {
255 39312 : *fx32ptr5 = tmp_fxL1; /*2*(Q-1)*/
256 39312 : move32();
257 : }
258 :
259 4118768 : if ( EQ_16( i, subframelength_s34 ) )
260 : {
261 39312 : *fx32ptr6 = tmp_fxL1; /*2*(Q-1)*/
262 39312 : move32();
263 : }
264 4118768 : tmp_fx1 = shr_sat( *fxptr2, q16p1 ); /*q-1 to avoisd saturation in energy*/
265 4118768 : tmp_fxL1 = L_mac0_sat( tmp_fxL1, tmp_fx1, tmp_fx1 ); /*2*(Q-1)*/
266 4118768 : tmp_fxL2 = L_mac0_sat( tmp_fxL2, *fxptr3, *fxptr3 ); /*2*(q_sig16)*/
267 4118768 : cntnzcr = add( cntnzcr, 1 );
268 4118768 : IF( L_mult0( *fxptr2, *( fxptr2 - 1 ) ) <= 0 )
269 : {
270 639865 : sptr1_loc = add( sptr1_loc, 1 );
271 639865 : sptr2_loc = s_max( sptr2_loc, cntnzcr );
272 :
273 639865 : test();
274 639865 : if ( ( firstnzcr > 0 ) && ( GT_16( cntnzcr, maxnzcr[tmp_fx2] ) ) )
275 : {
276 8350 : maxnzcr[tmp_fx2] = cntnzcr; /*Q0*/
277 8350 : move16();
278 : }
279 :
280 639865 : firstnzcr = 0;
281 639865 : move16();
282 639865 : cntnzcr = -1;
283 639865 : move16();
284 : }
285 4118768 : fxptr2++;
286 4118768 : fxptr3++;
287 : }
288 39312 : if ( LT_16( j, NUMSF_M1 ) )
289 : {
290 34398 : cntnzcr = add( cntnzcr, 1 );
291 : }
292 39312 : sptr2_loc = s_max( sptr2_loc, cntnzcr );
293 39312 : *fx32ptr4 = tmp_fxL2; /*2*(q_sig16)*/
294 39312 : move32();
295 39312 : fx32ptr4++;
296 39312 : *sptr1 = sptr1_loc; /*Q0*/
297 39312 : move16();
298 39312 : *sptr2 = sptr2_loc; /*Q0*/
299 39312 : move16();
300 39312 : sptr1++;
301 39312 : sptr2++;
302 :
303 39312 : test();
304 39312 : if ( ( firstnzcr > 0 ) && ( GT_16( cntnzcr, maxnzcr[tmp_fx2] ) ) )
305 : {
306 465 : maxnzcr[tmp_fx2] = cntnzcr; /*Q0*/
307 465 : move16();
308 : }
309 :
310 39312 : sptr1_loc = 0;
311 39312 : move16();
312 39312 : test();
313 39312 : firstnzcr = 1;
314 39312 : move16();
315 39312 : IF( ( LT_16( j, NUMSF_M1 ) ) && ( L_mult0( *fxptr2, *( fxptr2 - 1 ) ) <= 0 ) ) /* zcr between 2 subframes */
316 : {
317 5778 : sptr1_loc = add( sptr1_loc, 1 ); /* counts for the nexte subframe */
318 5778 : cntnzcr = -1;
319 5778 : move16();
320 5778 : firstnzcr = 0;
321 5778 : move16();
322 : }
323 :
324 39312 : *fx32ptr1 = tmp_fxL1; /*2*(Q-1)*/
325 39312 : move32();
326 39312 : if ( LT_32( *fx32ptr5, L_shr( *fx32ptr1, 1 ) ) )
327 : {
328 19516 : tmp_fxL1 = L_shl_sat( L_sub_sat( *fx32ptr1, *fx32ptr5 ), 1 ); /*2*(Q-1)*/
329 : }
330 39312 : *fx32ptr5 = tmp_fxL1; /*2*(Q-1)*/
331 39312 : move32();
332 :
333 39312 : fx32ptr1++;
334 39312 : fx32ptr5++;
335 39312 : fx32ptr6++;
336 : }
337 :
338 4914 : fxptr2 = imdct_mem_fx; /* q_sig16 or q-1*/
339 4914 : j = NUMSF;
340 4914 : move16(); /* one future subframe but 96 samples (not 80) (enough with ALDO window) */
341 4914 : tmp_fx1 = shr( *fxptr2, qmemp1 ); /* q-1 shr to avoid overflow in es_mdct_fx*/
342 4914 : tmp_fxL1 = L_mac0( 25, tmp_fx1, tmp_fx1 ); /*2*(Q-1)*/
343 :
344 4914 : sptr1_loc = 0;
345 4914 : move16();
346 4914 : fxptr2++;
347 4914 : tmp_fx3 = sub( len3xLp20, 1 );
348 909580 : FOR( i = 1; i < len3xLp20; i++ )
349 : {
350 904666 : tmp_fx1 = shr( *fxptr2, qmemp1 ); /*q-1 to avoisd saturation in energy*/
351 904666 : tmp_fxL1 = L_mac0_sat( tmp_fxL1, tmp_fx1, tmp_fx1 ); /*2*(Q-1)*/
352 904666 : if ( *fxptr2 * *( fxptr2 - 1 ) <= 0 )
353 : {
354 158677 : sptr1_loc = add( sptr1_loc, 1 );
355 : }
356 :
357 904666 : fxptr2++;
358 : }
359 4914 : *fx32ptr1 = tmp_fxL1; /*2*(Q-1)*/
360 4914 : move32();
361 4914 : *sptr1 = sptr1_loc; /*Q0*/
362 4914 : fxptr2 = imdct_mem_fx; /*q_sig16*/
363 4914 : fxptr3 = imdct_mem_fx + 1; /*q_sig16*/
364 4914 : fxptr4 = imdct_mem_fx + 2; /*q_sig16*/
365 4914 : tmp_fxL1 = L_mult( rec_sig_fx[framelength_m1], -8192 /*Q15*/ ); /*q_sig16+Q16*/
366 4914 : tmp_fxL1 = L_mac( tmp_fxL1, *fxptr3, -8192 ); /*q_sig16+Q16*/
367 4914 : tmp_fx1 = mac_r( tmp_fxL1, *fxptr2, 16384 /*Q15*/ ); /*q_sig16*/
368 :
369 4914 : tmp_fxL2 = L_deposit_l( 100 );
370 4914 : tmp_fxL2 = L_mac0( tmp_fxL2, tmp_fx1, tmp_fx1 ); /*2*(q_sig16)*/
371 :
372 904666 : FOR( j = 1; j < tmp_fx3; j++ ) /* tmp_fx3 still contains subframelength*1.2-1 */
373 : {
374 899752 : tmp_fxL1 = L_mult( *fxptr2, -8192 /*Q15*/ ); /*q_sig16+Q16*/
375 899752 : tmp_fxL1 = L_mac_sat( tmp_fxL1, *fxptr4, -8192 ); /*q_sig16+Q16*/
376 899752 : tmp_fx1 = mac_r_sat( tmp_fxL1, *fxptr3, 16384 /*Q15*/ ); /*q_sig16*/
377 :
378 899752 : tmp_fxL2 = L_mac0_sat( tmp_fxL2, tmp_fx1, tmp_fx1 ); /*2*(q_sig16)*/
379 899752 : fxptr2++;
380 899752 : fxptr3++;
381 899752 : fxptr4++;
382 : }
383 :
384 4914 : tmp_fxL1 = L_mult( *fxptr2, -8192 /*Q15*/ ); /*q_sig16+Q16*/
385 4914 : tmp_fx1 = mac_r_sat( tmp_fxL1, *fxptr3, 16384 /*Q15*/ ); /*q_sig16*/
386 4914 : es_mdct_hb_fx[NUMSF] = L_mac0_sat( tmp_fxL2, tmp_fx1, tmp_fx1 ); /*2*(q_sig16)*/
387 4914 : move32();
388 :
389 4914 : max_es_hb_fx = L_add( es_mdct_hb_fx[0], 0 ); /* for memorising the max energy */
390 4914 : max_es_fx = L_add( es_mdct_fx[0], 0 ); /* for memorising the max energy */
391 4914 : maxind = 0;
392 4914 : move16();
393 44226 : FOR( i = 1; i <= NUMSF; i++ )
394 : {
395 39312 : max_es_hb_fx = L_max( max_es_hb_fx, es_mdct_hb_fx[i] ); /* max energy low band, 8 present and 1 future subframes */
396 :
397 39312 : max_es_fx = L_max( max_es_fx, es_mdct_fx[i] ); /* max energy low band, 8 present and 1 future subframes */
398 :
399 39312 : if ( GE_32( es_mdct_fx[i], max_es_fx ) ) /* '=' to handle the first window*/
400 : {
401 9090 : maxind = i;
402 9090 : move16();
403 : }
404 : }
405 :
406 4914 : cnt2 = cnt5 = 0;
407 4914 : move16();
408 4914 : move16();
409 4914 : test();
410 4914 : if ( *prevflag != 0 || LT_32( max_es_fx, L_mult0( subframelength, 2500 ) ) )
411 : {
412 520 : maxind = 0;
413 520 : move16();
414 : }
415 :
416 4914 : if ( LT_32( max_es_fx, L_shl_sat( mean_prev_fx_loc, 2 ) ) ) /*OK if saturated*/
417 : {
418 4059 : maxind = 0;
419 4059 : move16();
420 : }
421 4914 : *prevflag = 0;
422 4914 : move16();
423 :
424 5877 : FOR( i = 0; i < maxind; i++ ) /* only subbands before max energy subband are handled */
425 : {
426 963 : g_fx = 32767; /*Q15*/
427 963 : move16(); /* default gain */
428 963 : min_g_fx[i] = 32767; /*Q15*/
429 963 : move16();
430 963 : min_g_hb_fx[i] = 32767; /*Q15*/
431 963 : move16();
432 :
433 963 : Mpy_32_16_ss( es_mdct_half_fx[i], 328, &tmp_fxL1, &tmp_u16 ); /* 328 for 1/100*/
434 963 : Mpy_32_16_ss( es_mdct_half_fx[i], 3277, &tmp_fxL2, &tmp_u16 ); /* 3277 for 1/10*/
435 963 : Mpy_32_16_ss( es_mdct_fx[i], 5461, &tmp_fxL3, &tmp_u16 ); /* 5461 for 1/6*/
436 963 : test();
437 963 : test();
438 963 : test();
439 963 : IF( ( GT_32( tmp_fxL1, L_add( mean_prev_nc_fx_loc, 125000 ) ) ) || /* less then 20% energy in 3/4 of the subframe -> starting onset in the last quarter */
440 : ( ( GT_32( tmp_fxL2, L_add( mean_prev_nc_fx_loc, 125000 ) ) ) &&
441 : ( ( LT_16( zcr[i], limzcr ) ) || ( LT_32( es_mdct_quart_fx[i], tmp_fxL3 ) ) ) ) ) /* already an offset, plosif, do not touch */
442 : {
443 201 : maxind = i;
444 201 : move16(); /* no preecho reduction after the first subframe with gain 1 */
445 201 : *prevflag = 1;
446 201 : move16();
447 238 : FOR( j = sub( i, 1 ); j >= 0; j-- )
448 : {
449 37 : if ( GT_32( es_mdct_fx[j], L_shr( es_mdct_fx[i], 1 ) ) )
450 : {
451 8 : maxind = j;
452 8 : move16();
453 : }
454 : }
455 : }
456 : ELSE
457 : {
458 762 : IF( LT_32( es_mdct_fx[i], L_shr( max_es_fx, 4 ) ) )
459 : {
460 282 : g_fx = lim16_fx; /*Q15*/
461 282 : move16();
462 282 : cnt5 = add( cnt5, 1 );
463 :
464 282 : IF( LT_32( es_mdct_fx[i], L_shr( max_es_fx, 5 ) ) )
465 : {
466 89 : g_fx = lim32_fx; /*Q15*/
467 89 : move16();
468 89 : cnt2 = add( cnt2, 1 );
469 : }
470 :
471 282 : IF( LT_32( mean_prev_fx_loc, es_mdct_fx[i] ) )
472 : {
473 169 : tmp_fx1 = norm_l( es_mdct_fx[i] );
474 169 : tmp_fxL1 = L_shl( es_mdct_fx[i], tmp_fx1 );
475 169 : tmp_fxL2 = L_shl( mean_prev_fx_loc, tmp_fx1 );
476 169 : tmp_fx1 = round_fx_sat( tmp_fxL1 );
477 169 : tmp_fx2 = round_fx( tmp_fxL2 );
478 169 : tmp_fx3 = div_s( tmp_fx2, tmp_fx1 );
479 169 : min_g_fx[i] = Frac_sqrt( tmp_fx3 );
480 169 : move16();
481 : }
482 :
483 282 : IF( LT_32( mean_prev_hb_fx_loc, es_mdct_hb_fx[i] ) )
484 : {
485 210 : tmp_fx1 = norm_l( es_mdct_hb_fx[i] );
486 210 : tmp_fxL1 = L_shl( es_mdct_hb_fx[i], tmp_fx1 );
487 210 : tmp_fxL2 = L_shl( mean_prev_hb_fx_loc, tmp_fx1 );
488 210 : tmp_fx1 = round_fx_sat( tmp_fxL1 );
489 210 : tmp_fx2 = round_fx( tmp_fxL2 );
490 210 : tmp_fx3 = div_s( tmp_fx2, tmp_fx1 );
491 210 : min_g_hb_fx[i] = Frac_sqrt( tmp_fx3 );
492 210 : move16();
493 : }
494 282 : test();
495 282 : IF( ( LT_16( zcr[i], shr( limzcr, 1 ) ) ) || ( GT_16( maxnzcr[i], limmaxnzcr ) ) )
496 : {
497 35 : if ( LT_16( min_g_fx[i], 32767 ) ) /* *mean_prev < es_mdct[i]) */
498 : {
499 21 : mean_prev_fx_loc = L_add( es_mdct_fx[i], 0 );
500 : }
501 35 : min_g_fx[i] = 32767; /*Q15*/
502 35 : move16(); /* not noise-like, do not touch the amplitude, but may do in HB*/
503 : }
504 : }
505 : ELSE
506 : {
507 480 : test();
508 480 : if ( i > 0 && LT_16( maxind, NUMSF ) )
509 : {
510 49 : *prevflag = 1;
511 49 : move16();
512 : }
513 480 : maxind = i;
514 480 : move16(); /* no preecho reduction after the first subframe with gain 1*/
515 : }
516 : }
517 963 : gt_fx[i] = g_fx; /*Q15*/
518 963 : move16();
519 963 : gt_hb_fx[i] = g_fx; /*Q15*/
520 963 : move16();
521 : }
522 :
523 48866 : FOR( i = maxind; i <= NUMSF; i++ ) /* also for the first memory subframe */
524 : {
525 43952 : gt_fx[i] = 32767; /*Q15*/
526 43952 : move16();
527 43952 : min_g_fx[i] = 32767; /*Q15*/
528 43952 : move16();
529 43952 : gt_hb_fx[i] = 32767; /*Q15*/
530 43952 : move16();
531 43952 : min_g_hb_fx[i] = 32767; /*Q15*/
532 43952 : move16();
533 : }
534 :
535 4914 : ind2 = 0;
536 4914 : move16();
537 44226 : FOR( i = 0; i < NUMSF; i++ )
538 : {
539 39312 : if ( LT_16( gt_fx[i], 32767 ) ) /*gt not yet limited by min_g*/
540 : {
541 192 : ind2 = add( i, 1 ); /* first subframe with gain = 1 after last gain < 1 --> frame with the attack*/
542 : }
543 : }
544 :
545 4914 : test();
546 4914 : if ( ( GT_16( wmold_fx, 16384 ) ) && ( LT_16( add( cnt2, cnt5 ), 2 ) ) ) /* mini either 1 cnt2 (and so also cnt5) or 2 cnt5 */
547 : {
548 : /* maxind = 0; false alarm, no echo reduction */
549 4823 : ind2 = 0;
550 4823 : move16();
551 : }
552 4914 : ind2_m1 = sub( ind2, 1 );
553 4914 : ind2_sfl = i_mult( subframelength, ind2 );
554 4914 : numsf_ind2 = sub( NUMSF, ind2 );
555 4914 : fxptr3 = gt_fx; /*Q15*/
556 4914 : fxptr4 = gt_hb_fx; /*Q15*/
557 4914 : fxptr5 = min_g_fx; /*Q15*/
558 4914 : fxptr6 = min_g_hb_fx; /*Q15*/
559 :
560 4914 : fxptr1 = preechogain_fx + smooth_len;
561 4914 : pre_g_ch_tab[0] = smooth_len;
562 4914 : move16(); /*1st after smoothmem*/
563 4914 : fxptr2 = preechogain_hb_fx;
564 5083 : FOR( i = 0; i < ind2; i++ ) /* only subbands before max energy subband are handled*/
565 : {
566 169 : *fxptr3 = s_max( *fxptr3, *fxptr5 );
567 169 : move16();
568 :
569 169 : *fxptr4 = s_max( *fxptr4, *fxptr6 );
570 169 : move16();
571 :
572 18729 : FOR( j = 0; j < subframelength; j++ )
573 : {
574 18560 : *fxptr1 = *fxptr3;
575 18560 : move16();
576 18560 : *fxptr2 = *fxptr4;
577 18560 : move16();
578 18560 : fxptr1++;
579 18560 : fxptr2++;
580 : }
581 169 : pre_g_ch_tab[( i + 1 )] = add( pre_g_ch_tab[i], subframelength );
582 169 : fxptr3++;
583 169 : fxptr4++;
584 169 : fxptr5++;
585 169 : fxptr6++;
586 : }
587 :
588 4914 : max_plus_es_mdct_fx = L_deposit_l( 0 );
589 4914 : adv = smooth_len;
590 4914 : move16(); /* samples needed to have near 1 gain after smoothing at the beggining of the attack subframe*/
591 4914 : advmem = adv; /*Q0*/
592 4914 : move16();
593 :
594 4914 : test();
595 4914 : test();
596 4914 : IF( ind2 > 0 || LT_16( wmold_fx, 32767 ) || LT_16( *wmold_hb_fx, 32767 ) )
597 : {
598 54 : ptr_fx = imdct_mem_fx; /*q_sig16*/
599 54 : qtmp = qmemp1;
600 54 : pluslim = num_subsubframes;
601 54 : move16(); /* if ind2 == NUMSF */
602 54 : IF( numsf_ind2 > 0 )
603 : {
604 54 : ptr_fx = rec_sig_fx + ind2_sfl; /*q_sig16*/
605 54 : qtmp = q16p1;
606 54 : move16();
607 54 : pluslim = i_mult( numsf_ind2, num_subsubframes );
608 : }
609 :
610 54 : maxcrit_fx = L_add( mean_prev_nc_fx_loc, 0 );
611 54 : IF( ind2 == 0 )
612 : {
613 0 : sum_plus_es_fx = L_add( mean_prev_nc_fx_loc, 0 ); /* 8 times mean sususb enenrgy (=maxcrit)*/
614 0 : pluslim = num_subsubframes;
615 0 : move16();
616 0 : oldgain_fx = wmold_fx; /*Q15*/
617 0 : move16();
618 0 : oldgain_hb_fx = *wmold_hb_fx; /*Q15*/
619 0 : move16();
620 : }
621 : ELSE /* ind2 > 0*/
622 : {
623 54 : sum_plus_es_fx = es_mdct_fx[ind2_m1];
624 54 : move32(); /* 8 times mean sususb enenrgy (=maxcrit)*/
625 54 : oldgain_fx = gt_fx[ind2_m1]; /*Q15*/
626 54 : move16();
627 54 : oldgain_hb_fx = gt_hb_fx[ind2_m1]; /*Q15*/
628 54 : move16();
629 :
630 54 : tmp_fx1 = mult_r( gt_fx[ind2_m1], gt_fx[ind2_m1] ); /*Q15*/
631 54 : Mpy_32_16_ss( es_mdct_fx[ind2_m1], tmp_fx1, &maxcrit_fx, &tmp_u16 );
632 54 : Mpy_32_16_ss( max_es_fx, 410, &tmp_fxL1, &tmp_u16 ); /* 410 for 1/80*/
633 :
634 54 : test();
635 54 : if ( ( GT_32( tmp_fxL1, maxcrit_fx ) ) && ( GT_16( zcr[ind2], limzcr ) ) )
636 : {
637 18 : maxcrit_fx = L_add( tmp_fxL1, 0 ); /* still 10 times smaller then mean max_es*/
638 : }
639 : }
640 54 : fx32ptr1 = plus_es_mdct_fx;
641 54 : fx32ptr4 = mean_plus_es_fx + 1;
642 2158 : FOR( j = 0; j < pluslim; j++ ) /* 8 sub-subframes */
643 : {
644 2104 : tmp_fxL1 = 100;
645 2104 : move16();
646 30904 : FOR( i = 0; i < subsubframelength; i++ )
647 : {
648 :
649 28800 : tmp_fx1 = shr( *ptr_fx, qtmp ); /* q-1, to have same shift as es_mdct_.. */
650 28800 : tmp_fxL1 = L_mac0_sat( tmp_fxL1, tmp_fx1, tmp_fx1 ); /*2*(Q-1)*/
651 28800 : ptr_fx++;
652 : }
653 2104 : if ( GT_32( tmp_fxL1, max_plus_es_mdct_fx ) )
654 : {
655 378 : max_plus_es_mdct_fx = L_add( tmp_fxL1, 0 );
656 : }
657 :
658 2104 : sum_plus_es_fx = L_add_sat( sum_plus_es_fx, L_shl_sat( tmp_fxL1, 2 ) ); /*2*(Q-1)*/
659 2104 : *fx32ptr1 = tmp_fxL1; /*2*(Q-1)*/
660 2104 : fx32ptr1++;
661 2104 : Mpy_32_16_ss( sum_plus_es_fx, inv_jp2[j], fx32ptr4, &tmp_u16 ); /* 410 for 1/80*/
662 2104 : if ( LT_32( *fx32ptr4, maxcrit_fx ) )
663 : {
664 290 : *fx32ptr4 = maxcrit_fx;
665 290 : move32();
666 : }
667 2104 : fx32ptr4++;
668 : }
669 54 : *fx32ptr4 = -1;
670 54 : move32(); /*mean_plus_es_fx[pluslim] = -1; */
671 54 : *mean_plus_es_fx = *plus_es_mdct_fx; /*2*(Q-1)*/
672 54 : move32(); /* index [0] */
673 54 : if ( LT_32( *mean_plus_es_fx, maxcrit_fx ) )
674 : {
675 46 : *mean_plus_es_fx = maxcrit_fx; /*2*(Q-1)*/
676 46 : move32();
677 : }
678 :
679 54 : j = 0;
680 54 : move16();
681 282 : WHILE( ( LT_32( plus_es_mdct_fx[j], mean_plus_es_fx[j] ) ) && ( LT_32( plus_es_mdct_fx[j], L_shr( max_plus_es_mdct_fx, 3 ) ) ) )
682 : {
683 228 : test();
684 228 : j = add( j, 1 );
685 : }
686 54 : tmp_fx3 = i_mult( j, subsubframelength );
687 54 : adv = sub( adv, tmp_fx3 );
688 54 : IF( numsf_ind2 > 0 ) /* onset not in future frame */
689 : {
690 54 : fxptr1 = preechogain_fx + ind2_sfl + smooth_len;
691 54 : fxptr2 = preechogain_hb_fx + ind2_sfl;
692 :
693 3124 : FOR( i = 0; i < tmp_fx3; i++ )
694 : {
695 3070 : *fxptr1 = oldgain_fx; /*Q15*/
696 3070 : move16(); /*keep the gain of the previous subframe*/
697 3070 : *fxptr2 = oldgain_hb_fx; /*Q15*/
698 3070 : move16(); /*keep the gain of the previous subframe*/
699 3070 : fxptr1++;
700 3070 : fxptr2++;
701 : }
702 : }
703 : }
704 :
705 4914 : IF( ind2 > 0 )
706 : {
707 : /* check increasing energy of preecho by regression last 3 subframes (if possible) */
708 54 : ind3 = add( ind2, shr( j, log2_num_subsubframes ) ); /* return (with rounding) to subframe basis */
709 54 : ind4 = sub( ind3, 1 );
710 54 : ind5 = sub( ind3, 2 );
711 54 : ind6 = sub( ind3, 3 );
712 54 : IF( ind4 > 0 )
713 : {
714 : /* case of 3 points is simply */
715 54 : eshbmean2_fx = L_add( es_mdct_hb_fx[ind4], es_mdct_hb_fx[ind5] ); /*2*(q_sig16)*/
716 :
717 54 : sxyhb2_fx = L_sub( es_mdct_hb_fx[ind4], es_mdct_hb_fx[ind5] ); /* / eshbmean2 * 2; 04042013: division not needed, only sign of sxyhb2 is used*/
718 :
719 54 : IF( GT_16( ind3, 2 ) )
720 : {
721 32 : tmp_fxL1 = L_add( eshbmean2_fx, es_mdct_hb_fx[ind6] ); /*2*(q_sig16)*/
722 32 : Mpy_32_16_ss( tmp_fxL1, 4369, &eshbmean3_fx, &tmp_u16 ); /*10922 : 1/3*/
723 32 : sxylb3_fx = L_sub( es_mdct_fx[ind4], es_mdct_fx[ind6] ); /* /eslbmean3 / 2; /2 for 3 points regression calc; 04042013: division not needed, only sign of sxylb3 is used*/
724 32 : tmp_fxL1 = L_sub( es_mdct_hb_fx[ind4], es_mdct_hb_fx[ind6] ); /*2*(q_sig16)*/
725 32 : test();
726 32 : IF( ( LT_32( tmp_fxL1, eshbmean3_fx ) ) || ( sxylb3_fx < 0 ) )
727 : {
728 18 : ind2 = 0;
729 18 : move16();
730 18 : ind2_sfl = 0;
731 18 : move16();
732 18 : adv = advmem;
733 18 : move16();
734 : }
735 : }
736 : ELSE
737 : {
738 22 : IF( sxyhb2_fx < 0 )
739 : {
740 0 : ind2 = 0;
741 0 : move16();
742 0 : ind2_sfl = 0;
743 0 : move16();
744 0 : adv = advmem;
745 0 : move16(); /* 04042013: small bug corection*/
746 : }
747 : }
748 :
749 54 : tmp_fxL1 = L_add( eshbmean2_fx, es_mdct_hb_fx[ind3] ); /*2*(q_sig16)*/
750 54 : Mpy_32_16_ss( tmp_fxL1, 4369, &eshbmean3_fx, &tmp_u16 ); /*10922 : 1/3*/
751 :
752 54 : tmp_fxL1 = L_sub( es_mdct_hb_fx[ind3], es_mdct_hb_fx[ind5] ); /*2*(q_sig16)*/
753 54 : IF( LT_32( tmp_fxL1, eshbmean3_fx ) )
754 : {
755 8 : ind2 = 0;
756 8 : move16();
757 8 : ind2_sfl = 0;
758 8 : move16();
759 8 : adv = advmem;
760 8 : move16();
761 : }
762 : }
763 : }
764 :
765 4914 : ind2_m1 = sub( ind2, 1 ); /*ind2_m1 needs to be recomputed as ind2 could have changed since*/
766 :
767 4914 : stind = sub( ind2_sfl, adv );
768 4914 : stind_hb = add( stind, advmem );
769 4914 : if ( stind < 0 )
770 : {
771 4879 : stind = 0;
772 4879 : move16();
773 : }
774 :
775 4914 : if ( stind_hb < 0 )
776 : {
777 0 : stind_hb = 0;
778 0 : move16();
779 : }
780 :
781 4914 : tmp_fx1 = add( stind, smooth_len );
782 4914 : fxptr1 = preechogain_fx + tmp_fx1; /*Q15*/
783 4914 : fxptr2 = preechogain_hb_fx + stind_hb; /*Q15*/
784 :
785 4131523 : FOR( i = tmp_fx1; i < framelength; i++ ) /* rest of the gains, without 4 (PREECHO_SMOOTH_LEN) 1 for fadeout */
786 : {
787 4126609 : *( fxptr1++ ) = 32767; /*Q15*/
788 4126609 : move16();
789 : }
790 4914 : pre_g_ch_tab[ind2] = s_min( tmp_fx1, framelength ); /*Q0*/
791 4914 : move16();
792 :
793 4151039 : FOR( i = stind_hb; i < framelength; i++ ) /* rest of the gains*/
794 : {
795 4146125 : *( fxptr2++ ) = 32767; /*Q15*/
796 4146125 : move16();
797 : }
798 :
799 4914 : fxptr1 = preechogain_fx; /*Q15*/
800 24570 : FOR( i = 0; i < smooth_len; i++ )
801 : {
802 19656 : *( fxptr1++ ) = *smoothmem_fx; /*Q15*/
803 19656 : move16();
804 : }
805 :
806 4914 : fattnext_fx = 32767; /*Q15*/
807 4914 : move16();
808 4914 : if ( GT_16( stind, framelength ) )
809 : {
810 0 : fattnext_fx = gt_fx[ind2_m1]; /*Q15*/
811 0 : move16();
812 : }
813 :
814 4914 : fxptr1 = preechogain_fx + framelength; /*Q15*/
815 24570 : FOR( i = 0; i < smooth_len; i++ )
816 : {
817 19656 : *( fxptr1++ ) = fattnext_fx; /*Q15*/
818 19656 : move16();
819 : }
820 :
821 9916 : FOR( i = 0; i <= ind2; i++ )
822 : {
823 5002 : tmp_fx1 = pre_g_ch_tab[i]; /*Q0*/
824 5002 : move16();
825 5002 : tmp_fx2 = sub( tmp_fx1, smooth_len ); /* any index in the previous subframe*/
826 5002 : tmp_fx3 = mult_r( sub( preechogain_fx[tmp_fx1], preechogain_fx[tmp_fx2] ), invsmoothlenp1_fx ); /*step Q15*/
827 5002 : tmp_fx1 = tmp_fx3; /*Q15*/
828 5002 : move16(); /*cumulated step*/
829 5002 : fxptr1 = preechogain_fx + tmp_fx2; /*Q15*/
830 25010 : FOR( j = 0; j < smooth_len; j++ )
831 : {
832 20008 : *fxptr1 = add_sat( *fxptr1, tmp_fx1 ); /*Q15*/
833 20008 : move16();
834 20008 : tmp_fx1 = add( tmp_fx1, tmp_fx3 ); /*Q15*/
835 20008 : fxptr1++;
836 : }
837 : }
838 :
839 4914 : *smoothmem_fx = fattnext_fx; /*Q15*/
840 4914 : move16();
841 4914 : *wmold_hb_fx = preechogain_hb_fx[framelength_m1]; /*Q15*/
842 4914 : move16();
843 :
844 : /* apply gain */
845 4914 : fxptr1 = preechogain_fx; /*Q15*/
846 4914 : fxptr2 = preechogain_hb_fx; /*Q15*/
847 4914 : fxptr3 = rec_sig_fx; /*q_sig16*/
848 4914 : fxptr4 = rec_sig_lb_fx; /*q_sig16*/
849 4914 : fxptr5 = rec_sig_hb_fx; /*q_sig16*/
850 4162994 : FOR( i = 0; i < framelength; i++ )
851 : {
852 4158080 : tmp_fxL1 = L_mult( *fxptr4, *fxptr1 ); /*q_sig16 + Q16*/
853 4158080 : *fxptr3 = mac_r( tmp_fxL1, *fxptr5, *fxptr2 ); /*q_sig16*/
854 4158080 : move16();
855 4158080 : fxptr1++;
856 4158080 : fxptr2++;
857 4158080 : fxptr3++;
858 4158080 : fxptr4++;
859 4158080 : fxptr5++;
860 : }
861 :
862 4914 : mean_prev_nc_fx_loc = L_add( es_mdct_fx[0], 0 ); /* compute mean not corrected by the actual gains 2*(Q-1)*/
863 :
864 39312 : FOR( i = 1; i < NUMSF; i++ ) /* all present subbands */
865 : {
866 34398 : if ( EQ_16( i, NUMSF_S2 ) )
867 : {
868 4914 : savehalfe_fx = L_add( mean_prev_nc_fx_loc, 0 ); /*2*(Q-1)*/
869 : }
870 34398 : mean_prev_nc_fx_loc = L_add_sat( mean_prev_nc_fx_loc, es_mdct_fx[i] ); /*2*(Q-1)*/
871 : }
872 :
873 4914 : if ( LT_32( savehalfe_fx, L_shr( mean_prev_nc_fx_loc, 1 ) ) )
874 : {
875 2133 : mean_prev_nc_fx_loc = L_shl_sat( L_sub_sat( mean_prev_nc_fx_loc, savehalfe_fx ), 1 ); /*2*(Q-1)*/
876 : }
877 4914 : mean_prev_nc_fx_loc = L_shr( mean_prev_nc_fx_loc, 3 ); /* >> LOG2_NUMSF in fixpoint 2*(Q-1)*/
878 :
879 5002 : FOR( i = 0; i < ind2; i++ ) /* only subbands before max energy subband are handled*/
880 : {
881 88 : tmp_fx1 = mult_r( gt_fx[i], gt_fx[i] ); /*Q15*/
882 88 : Mpy_32_16_ss( es_mdct_fx[i], tmp_fx1, &es_mdct_fx[i], &tmp_u16 );
883 :
884 88 : tmp_fx1 = mult_r( gt_hb_fx[i], gt_hb_fx[i] ); /*Q15*/
885 88 : Mpy_32_16_ss( es_mdct_hb_fx[i], tmp_fx1, &es_mdct_hb_fx[i], &tmp_u16 );
886 : }
887 :
888 4914 : mean_prev_fx_loc = L_shr( es_mdct_fx[0], 3 ); /* compute mean used in next frame to limit gain 2*(Q-1)*/
889 4914 : mean_prev_hb_fx_loc = L_shr( es_mdct_hb_fx[0], 3 ); /* compute mean used in next frame to limit gain 2*(q_sig16)*/
890 :
891 39312 : FOR( i = 1; i < NUMSF; i++ ) /* all present subbands */
892 : {
893 34398 : IF( EQ_16( i, NUMSF_S2 ) )
894 : {
895 4914 : savehalfe_fx = L_add( mean_prev_fx_loc, 0 ); /*2*(Q-1)*/
896 4914 : savehalfe_hb_fx = L_add( mean_prev_hb_fx_loc, 0 ); /*2*(q_sig16)*/
897 : }
898 :
899 34398 : mean_prev_fx_loc = L_add( mean_prev_fx_loc, L_shr( es_mdct_fx[i], 3 ) ); /*2*(Q-1)*/
900 34398 : mean_prev_hb_fx_loc = L_add( mean_prev_hb_fx_loc, L_shr( es_mdct_hb_fx[i], 3 ) ); /*2*(q_sig16)*/
901 : }
902 :
903 4914 : tmp_fxL1 = L_sub( mean_prev_fx_loc, savehalfe_fx ); /*2*(Q-1)*/
904 4914 : if ( LT_32( savehalfe_fx, L_shr( mean_prev_fx_loc, 1 ) ) )
905 : {
906 2136 : mean_prev_fx_loc = L_shl( tmp_fxL1, 1 ); /*2*(Q-1)*/
907 : }
908 :
909 4914 : tmp_fxL1 = L_sub( mean_prev_hb_fx_loc, savehalfe_hb_fx ); /*2*(q_sig16)*/
910 4914 : if ( LT_32( savehalfe_hb_fx, L_shr( mean_prev_hb_fx_loc, 1 ) ) )
911 : {
912 2535 : mean_prev_hb_fx_loc = L_shl( tmp_fxL1, 1 ); /*2*(q_sig16)*/
913 : }
914 :
915 4914 : last2_fx = L_shr( L_add_sat( es_mdct_fx[NUMSF_M1], es_mdct_fx[NUMSF_M2] ), 1 ); /*q_sig16*/
916 4914 : last2_hb_fx = L_shr( L_add_sat( es_mdct_hb_fx[NUMSF_M1], es_mdct_hb_fx[NUMSF_M2] ), 1 ); /*q_sig16*/
917 4914 : if ( GT_32( last2_fx, mean_prev_fx_loc ) )
918 : {
919 1764 : mean_prev_fx_loc = L_add( last2_fx, 0 );
920 : }
921 :
922 4914 : if ( GT_32( last2_hb_fx, mean_prev_hb_fx_loc ) )
923 : {
924 2349 : mean_prev_hb_fx_loc = L_add( last2_hb_fx, 0 ); /*2*(q_sig16)*/
925 : }
926 4914 : *mean_prev_fx = mean_prev_fx_loc; /*2*(Q-1)*/
927 4914 : move32();
928 4914 : *mean_prev_hb_fx = L_shr_sat( mean_prev_hb_fx_loc, shl_sat( q_sig16, 1 ) ); /*Q0*/
929 4914 : move32(); /*save in Q0*/
930 4914 : *mean_prev_nc_fx = mean_prev_nc_fx_loc; /*2*(Q-1)*/
931 4914 : move32();
932 : }
933 :
934 7516 : return;
935 : }
936 :
937 : /*--------------------------------------------------------------------------*
938 : * Inverse_Transform()
939 : *
940 : * Inverse transform from the DCT domain to time domain
941 : *--------------------------------------------------------------------------*/
942 :
943 44253 : void Inverse_Transform(
944 : const Word32 *in_mdct, /* i : input MDCT vector Q */
945 : Word16 *Q, /* i/o: Q value of input */
946 : Word32 *out, /* o : output vector Q */
947 : const Word16 is_transient, /* i : transient flag Q0 */
948 : const Word16 L, /* i : output frame length Q0 */
949 : const Word16 L_inner, /* i : length of the transform Q0 */
950 : const Word16 element_mode /* i : IVAS element mode Q0 */
951 : )
952 : {
953 : Word16 ta, seg, tmp16;
954 : Word16 segment_length;
955 : const Word16 *win, *win2;
956 : Word32 out_alias[L_FRAME48k];
957 : Word32 alias[MAX_SEGMENT_LENGTH];
958 : Word32 in_mdct_modif[L_FRAME48k];
959 : Word32 *in_segment_modif;
960 : const Word32 *in_segment;
961 : Word32 *out_segment;
962 : Word16 segment_length_div2, segment_length_div4;
963 : Word16 tmp, q_out;
964 : Word32 L_temp;
965 : (void) ( element_mode );
966 : /* This value is used to right shift all vectors returned by 'iedct_short_fx()' */
967 : /* to bring them to a scaling that is equal to the 1st 'Q' returned by the 1st */
968 : /* call to 'iedct_short_fx()' minus these guard bits. */
969 : #define N_GUARD_BITS ( 9 + 1 ) /* 9 is enough but we put one extra bit */
970 :
971 44253 : IF( is_transient )
972 : {
973 1513 : segment_length = shr( L, 1 );
974 1513 : segment_length_div2 = shr( L, 2 );
975 1513 : segment_length_div4 = shr( L, 3 );
976 :
977 1513 : IF( EQ_16( L, L_FRAME48k ) )
978 : {
979 446 : win = short_window_48kHz_fx; /*Q15*/
980 : }
981 1067 : ELSE IF( EQ_16( L, L_FRAME32k ) )
982 : {
983 132 : win = short_window_32kHz_fx; /*Q15*/
984 : }
985 935 : ELSE IF( EQ_16( L, L_FRAME16k ) )
986 : {
987 935 : win = short_window_16kHz_fx; /*Q15*/
988 : }
989 : ELSE /* L == L_FRAME8k */
990 : {
991 0 : win = short_window_8kHz_fx; /*Q15*/
992 : }
993 :
994 1513 : set32_fx( out_alias, 0, L );
995 :
996 1513 : in_segment = in_mdct; /*Q*/
997 1513 : in_segment_modif = in_mdct_modif;
998 :
999 1513 : tmp16 = sub( L, L_inner );
1000 1513 : IF( tmp16 == 0 )
1001 : {
1002 379 : Copy32( in_mdct, in_mdct_modif, L ); /*Q*/
1003 : }
1004 1134 : ELSE IF( tmp16 > 0 )
1005 : {
1006 640 : FOR( seg = 0; seg < NUM_TIME_SWITCHING_BLOCKS; seg++ )
1007 : {
1008 76352 : FOR( ta = 0; ta < L_inner; ta += NUM_TIME_SWITCHING_BLOCKS )
1009 : {
1010 75840 : *in_segment_modif++ = *in_segment++; /*Q*/
1011 75840 : move32();
1012 : }
1013 :
1014 47552 : FOR( ta = 0; ta < tmp16; ta += NUM_TIME_SWITCHING_BLOCKS )
1015 : {
1016 47040 : *in_segment_modif++ = 0L;
1017 47040 : move32();
1018 : }
1019 : }
1020 : }
1021 : ELSE /* L < L_inner */
1022 : {
1023 5030 : FOR( seg = 0; seg < NUM_TIME_SWITCHING_BLOCKS; seg++ )
1024 : {
1025 359864 : FOR( ta = 0; ta < segment_length_div2; ta++ )
1026 : {
1027 355840 : *in_segment_modif++ = *in_segment++; /*Q*/
1028 355840 : move32();
1029 : }
1030 4024 : in_segment += shr( sub( L_inner, L ), 2 ); /*Q*/
1031 4024 : move32();
1032 : }
1033 : }
1034 :
1035 1513 : out_segment = out_alias - segment_length_div4;
1036 1513 : in_segment = in_mdct_modif; /*Q*/
1037 :
1038 1513 : tmp = *Q;
1039 : /* output of 'iedct_short_fx' has up to 'output frame length'/2 # of Elements */
1040 1513 : iedct_short_fx( in_segment, &tmp, alias, segment_length );
1041 1513 : IF( GT_16( tmp, N_GUARD_BITS ) )
1042 : {
1043 20 : q_out = sub( tmp, N_GUARD_BITS );
1044 20 : tmp = sub( tmp, q_out );
1045 : }
1046 : ELSE
1047 : {
1048 1493 : q_out = 0;
1049 1493 : move16();
1050 : }
1051 :
1052 102993 : FOR( ta = segment_length_div4; ta < segment_length_div2; ta++ )
1053 : {
1054 101480 : out_segment[ta] = L_shr( alias[ta], tmp ); /*q_out*/
1055 101480 : move32();
1056 : }
1057 : /* This previous loop fills the output buffer from [0..seg_len_div4-1] */
1058 :
1059 1513 : win2 = &win[segment_length_div2]; /*Q15*/
1060 204473 : FOR( ta = segment_length_div2; ta < segment_length; ta++ )
1061 : {
1062 202960 : out_segment[ta] = L_shr( Mult_32_16( alias[ta], *--win2 ), tmp ); /*q_out*/
1063 202960 : move32();
1064 : }
1065 : /* This previous loop fills the output buffer from [seg_len_div4..seg_len-seg_len_div4-1] */
1066 :
1067 1513 : out_segment += segment_length_div2; /*q_out*/
1068 1513 : in_segment += segment_length_div2; /*Q*/
1069 :
1070 4539 : FOR( seg = 1; seg < NUM_TIME_SWITCHING_BLOCKS - 1; seg++ )
1071 : {
1072 3026 : tmp = *Q;
1073 3026 : move16();
1074 : /* output of 'iedct_short_fx' has up to 'output frame length'/2 # of Elements */
1075 3026 : iedct_short_fx( in_segment, &tmp, alias, segment_length );
1076 3026 : tmp = sub( tmp, q_out );
1077 :
1078 408946 : FOR( ta = 0; ta < segment_length_div2; ta++ )
1079 : {
1080 405920 : out_segment[ta] = L_add( out_segment[ta], L_shr( Mult_32_16( alias[ta], *win2++ ), tmp ) ); /*q_out*/
1081 405920 : move32();
1082 : }
1083 408946 : FOR( ; ta < segment_length; ta++ )
1084 : {
1085 405920 : out_segment[ta] = L_add( out_segment[ta], L_shr( Mult_32_16( alias[ta], *--win2 ), tmp ) ); /*q_out*/
1086 405920 : move32();
1087 : }
1088 :
1089 3026 : in_segment += segment_length_div2; /*Q*/
1090 3026 : out_segment += segment_length_div2; /*q_out*/
1091 : }
1092 :
1093 1513 : tmp = *Q;
1094 1513 : move16();
1095 1513 : iedct_short_fx( in_segment, &tmp, alias, segment_length );
1096 1513 : tmp = sub( tmp, q_out );
1097 :
1098 204473 : FOR( ta = 0; ta < segment_length_div2; ta++ )
1099 : {
1100 202960 : out_segment[ta] = L_add( out_segment[ta], L_shr( Mult_32_16( alias[ta], *win2++ ), tmp ) ); /*q_out*/
1101 202960 : move32();
1102 : }
1103 :
1104 1513 : seg = add( segment_length_div2, shr( segment_length_div2, 1 ) ); /* seg = 3*segment_length/4 Q0*/
1105 102993 : FOR( ta = segment_length_div2; ta < seg; ta++ )
1106 : {
1107 101480 : out_segment[ta] = L_shr( alias[ta], tmp ); /*q_out*/
1108 101480 : move32();
1109 : }
1110 :
1111 407433 : FOR( ta = 0; ta < segment_length; ta++ )
1112 : {
1113 405920 : L_temp = L_add( out_alias[ta], 0 );
1114 405920 : out[ta] = out_alias[L - 1 - ta]; /*q_out*/
1115 405920 : move32();
1116 405920 : out[L - 1 - ta] = L_temp; /*q_out*/
1117 405920 : move32();
1118 : }
1119 :
1120 1513 : *Q = q_out;
1121 1513 : move16();
1122 : }
1123 : ELSE
1124 : {
1125 42740 : edct_fx( in_mdct, out, L, Q );
1126 : }
1127 44253 : }
|