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 7401 : 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 7401 : Word16 shift_q = sub( 15, q_sig32 );
88 :
89 7401 : q16p1 = add( q_sig16, 1 );
90 7401 : qmemp1 = q16p1;
91 :
92 7401 : IF( LE_32( core_brate, HQ_32k ) )
93 : {
94 :
95 4794 : mean_prev_fx_loc = L_add( *mean_prev_fx, 0 );
96 4794 : mean_prev_hb_fx_loc = L_shl_sat( *mean_prev_hb_fx, shl( q_sig16, 1 ) ); /*Q0 to q_sig16*/
97 4794 : mean_prev_nc_fx_loc = L_add( *mean_prev_nc_fx, 0 );
98 4794 : framelength_m1 = sub( framelength, 1 );
99 4794 : nb_flag = 0;
100 4794 : move16();
101 4794 : if ( ( bwidth == NB ) )
102 : {
103 0 : nb_flag = 1;
104 0 : move16();
105 : }
106 4794 : limzcr = 16;
107 4794 : move16();
108 4794 : smooth_len = 4;
109 4794 : move16();
110 4794 : invsmoothlenp1_fx = 6554; /*0.2 in Q15*/
111 4794 : move16();
112 4794 : 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 4794 : limmaxnzcr = mult( framelength, 1365 ); /*1/24 in Q15*/ /*Q0*/
123 4794 : num_subsubframes = 8;
124 4794 : move16();
125 4794 : log2_num_subsubframes = 3;
126 4794 : move16();
127 :
128 4794 : 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 4794 : 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 4794 : fxptr1 = imdct_mem_fx;
140 901914 : FOR( i = 0; i < len3xLp20; i++ )
141 : {
142 897120 : *fxptr1++ = negate( extract_h( L_shl_sat( wtda_audio_fx[len3xLp20 - 1 - i], shift_q ) ) ); /*Q-1*/
143 897120 : move16(); /*convert to Word16 Q-1 with saturation (saturation not a problem here) */
144 : }
145 2055354 : FOR( i = 0; i < framelength >> 1; i++ )
146 : {
147 2050560 : *fxptr1++ = negate( extract_h( L_shl_sat( wtda_audio_fx[i], shift_q ) ) ); /*Q-1*/
148 2050560 : move16(); /*convert to Word16 Q-1 with saturation (saturation not a problem here) */
149 : }
150 :
151 4794 : qmemp1 = 0; /*already in q-1*/
152 4794 : move16();
153 :
154 4794 : subframelength = shr( framelength, LOG2_NUMSF ); /*Q0*/
155 4794 : subsubframelength = shr( subframelength, log2_num_subsubframes ); /*Q0*/
156 4794 : wmold_fx = *smoothmem_fx; /*Q15*/
157 4794 : move16();
158 4794 : subframelength_s2 = shr( subframelength, 1 ); /*Q0*/
159 4794 : subframelength_s34 = mult( subframelength, 24576 /*3/4 in Q15*/ ); /*Q0*/
160 :
161 4794 : cntnzcr = -1;
162 4794 : move16();
163 :
164 4794 : lim16_fx = 3277; /*Q15*/
165 4794 : move16();
166 4794 : lim32_fx = 328; /*Q15*/
167 4794 : move16();
168 4794 : savehalfe_fx = L_deposit_l( 0 );
169 4794 : savehalfe_hb_fx = L_deposit_l( 0 );
170 :
171 4794 : 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 651 : lim16_fx = 32767; /*Q15*/
175 651 : move16();
176 651 : lim32_fx = 32767; /*Q15*/
177 651 : move16();
178 : }
179 :
180 4794 : *pastpre = 2;
181 4794 : move16();
182 4794 : fxptr1 = rec_sig_lb_fx; /*q_sig16*/
183 4794 : fxptr2 = rec_sig_fx; /*q_sig16*/
184 4794 : fxptr3 = rec_sig_fx + 1; /*q_sig16*/
185 4794 : fxptr4 = rec_sig_fx + 2; /*q_sig16*/
186 :
187 4794 : tmp_fxL1 = L_mult( shl_sat( *memfilt_lb_fx, q_sig16 ), 8192 ); /* *memfilt_lb_fx in q0 */
188 4794 : tmp_fxL1 = L_mac( tmp_fxL1, *fxptr3, 8192 /*Q15*/ ); /*Q16*/
189 4794 : *fxptr1 = mac_r( tmp_fxL1, *fxptr2, 16384 /*Q15*/ ); /*Q0*/
190 4794 : move16();
191 4794 : fxptr1++;
192 :
193 4096326 : FOR( j = 2; j < framelength; j++ )
194 : {
195 4091532 : tmp_fxL1 = L_mult( *fxptr2, 8192 /*Q15*/ ); /*Q16*/
196 4091532 : tmp_fxL1 = L_mac( tmp_fxL1, *fxptr4, 8192 /*Q15*/ ); /*Q16*/
197 4091532 : *fxptr1 = mac_r( tmp_fxL1, *fxptr3, 16384 /*Q15*/ ); /*Q0*/
198 4091532 : move16();
199 4091532 : fxptr1++;
200 4091532 : fxptr2++;
201 4091532 : fxptr3++;
202 4091532 : fxptr4++;
203 : }
204 :
205 4794 : tmp_fxL1 = L_mult( *fxptr2, 8192 ); /*Q16*/
206 4794 : *fxptr1 = mac_r( tmp_fxL1, *fxptr3, 16384 ); /*Q0*/
207 4794 : move16();
208 4794 : fxptr1 = rec_sig_lb_fx; /*q_sig16*/
209 4794 : fxptr2 = rec_sig_fx; /*q_sig16*/
210 4794 : fxptr3 = rec_sig_hb_fx; /*q_sig16*/
211 :
212 4105914 : FOR( j = 0; j < framelength; j++ )
213 : {
214 4101120 : *fxptr3 = sub( *fxptr2, *fxptr1 );
215 4101120 : move16();
216 4101120 : fxptr1++;
217 4101120 : fxptr2++;
218 4101120 : fxptr3++;
219 : }
220 :
221 4794 : fxptr2--;
222 4794 : *memfilt_lb_fx = shr_sat( *fxptr2, q_sig16 ); /*Q0*/
223 4794 : move16(); /* *memfilt_lb_fx in q0 */
224 :
225 : /* energy of low bands 8 present and 1 future sub-frames */
226 4794 : sptr1 = zcr; /*Q0*/
227 4794 : sptr1_loc = 0;
228 4794 : move16();
229 4794 : sptr2 = maxnzcr; /*Q0*/
230 :
231 4794 : fxptr2 = rec_sig_fx; /*q_sig16*/
232 4794 : fxptr3 = rec_sig_hb_fx; /*q_sig16*/
233 4794 : fx32ptr1 = es_mdct_fx;
234 4794 : fx32ptr5 = es_mdct_half_fx;
235 4794 : fx32ptr6 = es_mdct_quart_fx;
236 4794 : fx32ptr4 = es_mdct_hb_fx;
237 4794 : firstnzcr = 0;
238 4794 : move16();
239 43146 : FOR( j = 0; j < NUMSF; j++ ) /* 8 present subframes */
240 : {
241 38352 : tmp_fx2 = sub( j, 1 );
242 38352 : tmp_fx1 = shr_sat( *fxptr2, q16p1 ); /*q-1 to avoisd saturation in energy*/
243 38352 : tmp_fxL1 = L_mac0_sat( 25, tmp_fx1, tmp_fx1 ); /*2*(Q-1)*/
244 38352 : tmp_fxL2 = L_mac0_sat( 100, *fxptr3, *fxptr3 ); /*2*(q_sig16)*/
245 38352 : sptr2_loc = 0;
246 38352 : move16();
247 :
248 38352 : fxptr2++;
249 38352 : fxptr3++;
250 :
251 4101120 : FOR( i = 1; i < subframelength; i++ )
252 : {
253 4062768 : if ( EQ_16( i, subframelength_s2 ) )
254 : {
255 38352 : *fx32ptr5 = tmp_fxL1; /*2*(Q-1)*/
256 38352 : move32();
257 : }
258 :
259 4062768 : if ( EQ_16( i, subframelength_s34 ) )
260 : {
261 38352 : *fx32ptr6 = tmp_fxL1; /*2*(Q-1)*/
262 38352 : move32();
263 : }
264 4062768 : tmp_fx1 = shr_sat( *fxptr2, q16p1 ); /*q-1 to avoisd saturation in energy*/
265 4062768 : tmp_fxL1 = L_mac0_sat( tmp_fxL1, tmp_fx1, tmp_fx1 ); /*2*(Q-1)*/
266 4062768 : tmp_fxL2 = L_mac0_sat( tmp_fxL2, *fxptr3, *fxptr3 ); /*2*(q_sig16)*/
267 4062768 : cntnzcr = add( cntnzcr, 1 );
268 4062768 : IF( L_mult0( *fxptr2, *( fxptr2 - 1 ) ) <= 0 )
269 : {
270 644830 : sptr1_loc = add( sptr1_loc, 1 );
271 644830 : sptr2_loc = s_max( sptr2_loc, cntnzcr );
272 :
273 644830 : test();
274 644830 : if ( ( firstnzcr > 0 ) && ( GT_16( cntnzcr, maxnzcr[tmp_fx2] ) ) )
275 : {
276 7520 : maxnzcr[tmp_fx2] = cntnzcr; /*Q0*/
277 7520 : move16();
278 : }
279 :
280 644830 : firstnzcr = 0;
281 644830 : move16();
282 644830 : cntnzcr = -1;
283 644830 : move16();
284 : }
285 4062768 : fxptr2++;
286 4062768 : fxptr3++;
287 : }
288 38352 : if ( LT_16( j, NUMSF_M1 ) )
289 : {
290 33558 : cntnzcr = add( cntnzcr, 1 );
291 : }
292 38352 : sptr2_loc = s_max( sptr2_loc, cntnzcr );
293 38352 : *fx32ptr4 = tmp_fxL2; /*2*(q_sig16)*/
294 38352 : move32();
295 38352 : fx32ptr4++;
296 38352 : *sptr1 = sptr1_loc; /*Q0*/
297 38352 : move16();
298 38352 : *sptr2 = sptr2_loc; /*Q0*/
299 38352 : move16();
300 38352 : sptr1++;
301 38352 : sptr2++;
302 :
303 38352 : test();
304 38352 : if ( ( firstnzcr > 0 ) && ( GT_16( cntnzcr, maxnzcr[tmp_fx2] ) ) )
305 : {
306 351 : maxnzcr[tmp_fx2] = cntnzcr; /*Q0*/
307 351 : move16();
308 : }
309 :
310 38352 : sptr1_loc = 0;
311 38352 : move16();
312 38352 : test();
313 38352 : firstnzcr = 1;
314 38352 : move16();
315 38352 : IF( ( LT_16( j, NUMSF_M1 ) ) && ( L_mult0( *fxptr2, *( fxptr2 - 1 ) ) <= 0 ) ) /* zcr between 2 subframes */
316 : {
317 5813 : sptr1_loc = add( sptr1_loc, 1 ); /* counts for the nexte subframe */
318 5813 : cntnzcr = -1;
319 5813 : move16();
320 5813 : firstnzcr = 0;
321 5813 : move16();
322 : }
323 :
324 38352 : *fx32ptr1 = tmp_fxL1; /*2*(Q-1)*/
325 38352 : move32();
326 38352 : if ( LT_32( *fx32ptr5, L_shr( *fx32ptr1, 1 ) ) )
327 : {
328 19002 : tmp_fxL1 = L_shl_sat( L_sub_sat( *fx32ptr1, *fx32ptr5 ), 1 ); /*2*(Q-1)*/
329 : }
330 38352 : *fx32ptr5 = tmp_fxL1; /*2*(Q-1)*/
331 38352 : move32();
332 :
333 38352 : fx32ptr1++;
334 38352 : fx32ptr5++;
335 38352 : fx32ptr6++;
336 : }
337 :
338 4794 : fxptr2 = imdct_mem_fx; /* q_sig16 or q-1*/
339 4794 : j = NUMSF;
340 4794 : move16(); /* one future subframe but 96 samples (not 80) (enough with ALDO window) */
341 4794 : tmp_fx1 = shr( *fxptr2, qmemp1 ); /* q-1 shr to avoid overflow in es_mdct_fx*/
342 4794 : tmp_fxL1 = L_mac0( 25, tmp_fx1, tmp_fx1 ); /*2*(Q-1)*/
343 :
344 4794 : sptr1_loc = 0;
345 4794 : move16();
346 4794 : fxptr2++;
347 4794 : tmp_fx3 = sub( len3xLp20, 1 );
348 897120 : FOR( i = 1; i < len3xLp20; i++ )
349 : {
350 892326 : tmp_fx1 = shr( *fxptr2, qmemp1 ); /*q-1 to avoisd saturation in energy*/
351 892326 : tmp_fxL1 = L_mac0_sat( tmp_fxL1, tmp_fx1, tmp_fx1 ); /*2*(Q-1)*/
352 892326 : if ( *fxptr2 * *( fxptr2 - 1 ) <= 0 )
353 : {
354 158052 : sptr1_loc = add( sptr1_loc, 1 );
355 : }
356 :
357 892326 : fxptr2++;
358 : }
359 4794 : *fx32ptr1 = tmp_fxL1; /*2*(Q-1)*/
360 4794 : move32();
361 4794 : *sptr1 = sptr1_loc; /*Q0*/
362 4794 : fxptr2 = imdct_mem_fx; /*q_sig16*/
363 4794 : fxptr3 = imdct_mem_fx + 1; /*q_sig16*/
364 4794 : fxptr4 = imdct_mem_fx + 2; /*q_sig16*/
365 4794 : tmp_fxL1 = L_mult( rec_sig_fx[framelength_m1], -8192 /*Q15*/ ); /*q_sig16+Q16*/
366 4794 : tmp_fxL1 = L_mac( tmp_fxL1, *fxptr3, -8192 ); /*q_sig16+Q16*/
367 4794 : tmp_fx1 = mac_r( tmp_fxL1, *fxptr2, 16384 /*Q15*/ ); /*q_sig16*/
368 :
369 4794 : tmp_fxL2 = L_deposit_l( 100 );
370 4794 : tmp_fxL2 = L_mac0( tmp_fxL2, tmp_fx1, tmp_fx1 ); /*2*(q_sig16)*/
371 :
372 892326 : FOR( j = 1; j < tmp_fx3; j++ ) /* tmp_fx3 still contains subframelength*1.2-1 */
373 : {
374 887532 : tmp_fxL1 = L_mult( *fxptr2, -8192 /*Q15*/ ); /*q_sig16+Q16*/
375 887532 : tmp_fxL1 = L_mac_sat( tmp_fxL1, *fxptr4, -8192 ); /*q_sig16+Q16*/
376 887532 : tmp_fx1 = mac_r_sat( tmp_fxL1, *fxptr3, 16384 /*Q15*/ ); /*q_sig16*/
377 :
378 887532 : tmp_fxL2 = L_mac0_sat( tmp_fxL2, tmp_fx1, tmp_fx1 ); /*2*(q_sig16)*/
379 887532 : fxptr2++;
380 887532 : fxptr3++;
381 887532 : fxptr4++;
382 : }
383 :
384 4794 : tmp_fxL1 = L_mult( *fxptr2, -8192 /*Q15*/ ); /*q_sig16+Q16*/
385 4794 : tmp_fx1 = mac_r_sat( tmp_fxL1, *fxptr3, 16384 /*Q15*/ ); /*q_sig16*/
386 4794 : es_mdct_hb_fx[NUMSF] = L_mac0_sat( tmp_fxL2, tmp_fx1, tmp_fx1 ); /*2*(q_sig16)*/
387 4794 : move32();
388 :
389 4794 : max_es_hb_fx = L_add( es_mdct_hb_fx[0], 0 ); /* for memorising the max energy */
390 4794 : max_es_fx = L_add( es_mdct_fx[0], 0 ); /* for memorising the max energy */
391 4794 : maxind = 0;
392 4794 : move16();
393 43146 : FOR( i = 1; i <= NUMSF; i++ )
394 : {
395 38352 : 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 38352 : max_es_fx = L_max( max_es_fx, es_mdct_fx[i] ); /* max energy low band, 8 present and 1 future subframes */
398 :
399 38352 : if ( GE_32( es_mdct_fx[i], max_es_fx ) ) /* '=' to handle the first window*/
400 : {
401 8842 : maxind = i;
402 8842 : move16();
403 : }
404 : }
405 :
406 4794 : cnt2 = cnt5 = 0;
407 4794 : move16();
408 4794 : move16();
409 4794 : test();
410 4794 : if ( *prevflag != 0 || LT_32( max_es_fx, L_mult0( subframelength, 2500 ) ) )
411 : {
412 348 : maxind = 0;
413 348 : move16();
414 : }
415 :
416 4794 : if ( LT_32( max_es_fx, L_shl_sat( mean_prev_fx_loc, 2 ) ) ) /*OK if saturated*/
417 : {
418 3984 : maxind = 0;
419 3984 : move16();
420 : }
421 4794 : *prevflag = 0;
422 4794 : move16();
423 :
424 5734 : FOR( i = 0; i < maxind; i++ ) /* only subbands before max energy subband are handled */
425 : {
426 940 : g_fx = 32767; /*Q15*/
427 940 : move16(); /* default gain */
428 940 : min_g_fx[i] = 32767; /*Q15*/
429 940 : move16();
430 940 : min_g_hb_fx[i] = 32767; /*Q15*/
431 940 : move16();
432 :
433 940 : Mpy_32_16_ss( es_mdct_half_fx[i], 328, &tmp_fxL1, &tmp_u16 ); /* 328 for 1/100*/
434 940 : Mpy_32_16_ss( es_mdct_half_fx[i], 3277, &tmp_fxL2, &tmp_u16 ); /* 3277 for 1/10*/
435 940 : Mpy_32_16_ss( es_mdct_fx[i], 5461, &tmp_fxL3, &tmp_u16 ); /* 5461 for 1/6*/
436 940 : test();
437 940 : test();
438 940 : test();
439 940 : 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 223 : maxind = i;
444 223 : move16(); /* no preecho reduction after the first subframe with gain 1 */
445 223 : *prevflag = 1;
446 223 : move16();
447 260 : 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 6 : maxind = j;
452 6 : move16();
453 : }
454 : }
455 : }
456 : ELSE
457 : {
458 717 : IF( LT_32( es_mdct_fx[i], L_shr( max_es_fx, 4 ) ) )
459 : {
460 277 : g_fx = lim16_fx; /*Q15*/
461 277 : move16();
462 277 : cnt5 = add( cnt5, 1 );
463 :
464 277 : IF( LT_32( es_mdct_fx[i], L_shr( max_es_fx, 5 ) ) )
465 : {
466 94 : g_fx = lim32_fx; /*Q15*/
467 94 : move16();
468 94 : cnt2 = add( cnt2, 1 );
469 : }
470 :
471 277 : IF( LT_32( mean_prev_fx_loc, es_mdct_fx[i] ) )
472 : {
473 152 : tmp_fx1 = norm_l( es_mdct_fx[i] );
474 152 : tmp_fxL1 = L_shl( es_mdct_fx[i], tmp_fx1 );
475 152 : tmp_fxL2 = L_shl( mean_prev_fx_loc, tmp_fx1 );
476 152 : tmp_fx1 = round_fx_sat( tmp_fxL1 );
477 152 : tmp_fx2 = round_fx( tmp_fxL2 );
478 152 : tmp_fx3 = div_s( tmp_fx2, tmp_fx1 );
479 152 : min_g_fx[i] = Frac_sqrt( tmp_fx3 );
480 152 : move16();
481 : }
482 :
483 277 : IF( LT_32( mean_prev_hb_fx_loc, es_mdct_hb_fx[i] ) )
484 : {
485 193 : tmp_fx1 = norm_l( es_mdct_hb_fx[i] );
486 193 : tmp_fxL1 = L_shl( es_mdct_hb_fx[i], tmp_fx1 );
487 193 : tmp_fxL2 = L_shl( mean_prev_hb_fx_loc, tmp_fx1 );
488 193 : tmp_fx1 = round_fx_sat( tmp_fxL1 );
489 193 : tmp_fx2 = round_fx( tmp_fxL2 );
490 193 : tmp_fx3 = div_s( tmp_fx2, tmp_fx1 );
491 193 : min_g_hb_fx[i] = Frac_sqrt( tmp_fx3 );
492 193 : move16();
493 : }
494 277 : test();
495 277 : IF( ( LT_16( zcr[i], shr( limzcr, 1 ) ) ) || ( GT_16( maxnzcr[i], limmaxnzcr ) ) )
496 : {
497 31 : if ( LT_16( min_g_fx[i], 32767 ) ) /* *mean_prev < es_mdct[i]) */
498 : {
499 16 : mean_prev_fx_loc = L_add( es_mdct_fx[i], 0 );
500 : }
501 31 : min_g_fx[i] = 32767; /*Q15*/
502 31 : move16(); /* not noise-like, do not touch the amplitude, but may do in HB*/
503 : }
504 : }
505 : ELSE
506 : {
507 440 : test();
508 440 : if ( i > 0 && LT_16( maxind, NUMSF ) )
509 : {
510 47 : *prevflag = 1;
511 47 : move16();
512 : }
513 440 : maxind = i;
514 440 : move16(); /* no preecho reduction after the first subframe with gain 1*/
515 : }
516 : }
517 940 : gt_fx[i] = g_fx; /*Q15*/
518 940 : move16();
519 940 : gt_hb_fx[i] = g_fx; /*Q15*/
520 940 : move16();
521 : }
522 :
523 47669 : FOR( i = maxind; i <= NUMSF; i++ ) /* also for the first memory subframe */
524 : {
525 42875 : gt_fx[i] = 32767; /*Q15*/
526 42875 : move16();
527 42875 : min_g_fx[i] = 32767; /*Q15*/
528 42875 : move16();
529 42875 : gt_hb_fx[i] = 32767; /*Q15*/
530 42875 : move16();
531 42875 : min_g_hb_fx[i] = 32767; /*Q15*/
532 42875 : move16();
533 : }
534 :
535 4794 : ind2 = 0;
536 4794 : move16();
537 43146 : FOR( i = 0; i < NUMSF; i++ )
538 : {
539 38352 : if ( LT_16( gt_fx[i], 32767 ) ) /*gt not yet limited by min_g*/
540 : {
541 188 : ind2 = add( i, 1 ); /* first subframe with gain = 1 after last gain < 1 --> frame with the attack*/
542 : }
543 : }
544 :
545 4794 : test();
546 4794 : 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 4707 : ind2 = 0;
550 4707 : move16();
551 : }
552 4794 : ind2_m1 = sub( ind2, 1 );
553 4794 : ind2_sfl = i_mult( subframelength, ind2 );
554 4794 : numsf_ind2 = sub( NUMSF, ind2 );
555 4794 : fxptr3 = gt_fx; /*Q15*/
556 4794 : fxptr4 = gt_hb_fx; /*Q15*/
557 4794 : fxptr5 = min_g_fx; /*Q15*/
558 4794 : fxptr6 = min_g_hb_fx; /*Q15*/
559 :
560 4794 : fxptr1 = preechogain_fx + smooth_len;
561 4794 : pre_g_ch_tab[0] = smooth_len;
562 4794 : move16(); /*1st after smoothmem*/
563 4794 : fxptr2 = preechogain_hb_fx;
564 4955 : FOR( i = 0; i < ind2; i++ ) /* only subbands before max energy subband are handled*/
565 : {
566 161 : *fxptr3 = s_max( *fxptr3, *fxptr5 );
567 161 : move16();
568 :
569 161 : *fxptr4 = s_max( *fxptr4, *fxptr6 );
570 161 : move16();
571 :
572 18081 : FOR( j = 0; j < subframelength; j++ )
573 : {
574 17920 : *fxptr1 = *fxptr3;
575 17920 : move16();
576 17920 : *fxptr2 = *fxptr4;
577 17920 : move16();
578 17920 : fxptr1++;
579 17920 : fxptr2++;
580 : }
581 161 : pre_g_ch_tab[( i + 1 )] = add( pre_g_ch_tab[i], subframelength );
582 161 : fxptr3++;
583 161 : fxptr4++;
584 161 : fxptr5++;
585 161 : fxptr6++;
586 : }
587 :
588 4794 : max_plus_es_mdct_fx = L_deposit_l( 0 );
589 4794 : adv = smooth_len;
590 4794 : move16(); /* samples needed to have near 1 gain after smoothing at the beggining of the attack subframe*/
591 4794 : advmem = adv; /*Q0*/
592 4794 : move16();
593 :
594 4794 : test();
595 4794 : test();
596 4794 : IF( ind2 > 0 || LT_16( wmold_fx, 32767 ) || LT_16( *wmold_hb_fx, 32767 ) )
597 : {
598 53 : ptr_fx = imdct_mem_fx; /*q_sig16*/
599 53 : qtmp = qmemp1;
600 53 : pluslim = num_subsubframes;
601 53 : move16(); /* if ind2 == NUMSF */
602 53 : IF( numsf_ind2 > 0 )
603 : {
604 52 : ptr_fx = rec_sig_fx + ind2_sfl; /*q_sig16*/
605 52 : qtmp = q16p1;
606 52 : move16();
607 52 : pluslim = i_mult( numsf_ind2, num_subsubframes );
608 : }
609 :
610 53 : maxcrit_fx = L_add( mean_prev_nc_fx_loc, 0 );
611 53 : 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 53 : sum_plus_es_fx = es_mdct_fx[ind2_m1];
624 53 : move32(); /* 8 times mean sususb enenrgy (=maxcrit)*/
625 53 : oldgain_fx = gt_fx[ind2_m1]; /*Q15*/
626 53 : move16();
627 53 : oldgain_hb_fx = gt_hb_fx[ind2_m1]; /*Q15*/
628 53 : move16();
629 :
630 53 : tmp_fx1 = mult_r( gt_fx[ind2_m1], gt_fx[ind2_m1] ); /*Q15*/
631 53 : Mpy_32_16_ss( es_mdct_fx[ind2_m1], tmp_fx1, &maxcrit_fx, &tmp_u16 );
632 53 : Mpy_32_16_ss( max_es_fx, 410, &tmp_fxL1, &tmp_u16 ); /* 410 for 1/80*/
633 :
634 53 : test();
635 53 : if ( ( GT_32( tmp_fxL1, maxcrit_fx ) ) && ( GT_16( zcr[ind2], limzcr ) ) )
636 : {
637 13 : maxcrit_fx = L_add( tmp_fxL1, 0 ); /* still 10 times smaller then mean max_es*/
638 : }
639 : }
640 53 : fx32ptr1 = plus_es_mdct_fx;
641 53 : fx32ptr4 = mean_plus_es_fx + 1;
642 2165 : FOR( j = 0; j < pluslim; j++ ) /* 8 sub-subframes */
643 : {
644 2112 : tmp_fxL1 = 100;
645 2112 : move16();
646 31352 : FOR( i = 0; i < subsubframelength; i++ )
647 : {
648 :
649 29240 : tmp_fx1 = shr( *ptr_fx, qtmp ); /* q-1, to have same shift as es_mdct_.. */
650 29240 : tmp_fxL1 = L_mac0_sat( tmp_fxL1, tmp_fx1, tmp_fx1 ); /*2*(Q-1)*/
651 29240 : ptr_fx++;
652 : }
653 2112 : if ( GT_32( tmp_fxL1, max_plus_es_mdct_fx ) )
654 : {
655 362 : max_plus_es_mdct_fx = L_add( tmp_fxL1, 0 );
656 : }
657 :
658 2112 : sum_plus_es_fx = L_add_sat( sum_plus_es_fx, L_shl_sat( tmp_fxL1, 2 ) ); /*2*(Q-1)*/
659 2112 : *fx32ptr1 = tmp_fxL1; /*2*(Q-1)*/
660 2112 : fx32ptr1++;
661 2112 : Mpy_32_16_ss( sum_plus_es_fx, inv_jp2[j], fx32ptr4, &tmp_u16 ); /* 410 for 1/80*/
662 2112 : if ( LT_32( *fx32ptr4, maxcrit_fx ) )
663 : {
664 291 : *fx32ptr4 = maxcrit_fx;
665 291 : move32();
666 : }
667 2112 : fx32ptr4++;
668 : }
669 53 : *fx32ptr4 = -1;
670 53 : move32(); /*mean_plus_es_fx[pluslim] = -1; */
671 53 : *mean_plus_es_fx = *plus_es_mdct_fx; /*2*(Q-1)*/
672 53 : move32(); /* index [0] */
673 53 : if ( LT_32( *mean_plus_es_fx, maxcrit_fx ) )
674 : {
675 45 : *mean_plus_es_fx = maxcrit_fx; /*2*(Q-1)*/
676 45 : move32();
677 : }
678 :
679 53 : j = 0;
680 53 : move16();
681 272 : 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 219 : test();
684 219 : j = add( j, 1 );
685 : }
686 53 : tmp_fx3 = i_mult( j, subsubframelength );
687 53 : adv = sub( adv, tmp_fx3 );
688 53 : IF( numsf_ind2 > 0 ) /* onset not in future frame */
689 : {
690 52 : fxptr1 = preechogain_fx + ind2_sfl + smooth_len;
691 52 : fxptr2 = preechogain_hb_fx + ind2_sfl;
692 :
693 2967 : FOR( i = 0; i < tmp_fx3; i++ )
694 : {
695 2915 : *fxptr1 = oldgain_fx; /*Q15*/
696 2915 : move16(); /*keep the gain of the previous subframe*/
697 2915 : *fxptr2 = oldgain_hb_fx; /*Q15*/
698 2915 : move16(); /*keep the gain of the previous subframe*/
699 2915 : fxptr1++;
700 2915 : fxptr2++;
701 : }
702 : }
703 : }
704 :
705 4794 : IF( ind2 > 0 )
706 : {
707 : /* check increasing energy of preecho by regression last 3 subframes (if possible) */
708 53 : ind3 = add( ind2, shr( j, log2_num_subsubframes ) ); /* return (with rounding) to subframe basis */
709 53 : ind4 = sub( ind3, 1 );
710 53 : ind5 = sub( ind3, 2 );
711 53 : ind6 = sub( ind3, 3 );
712 53 : IF( ind4 > 0 )
713 : {
714 : /* case of 3 points is simply */
715 53 : eshbmean2_fx = L_add( es_mdct_hb_fx[ind4], es_mdct_hb_fx[ind5] ); /*2*(q_sig16)*/
716 :
717 53 : 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 53 : IF( GT_16( ind3, 2 ) )
720 : {
721 29 : tmp_fxL1 = L_add( eshbmean2_fx, es_mdct_hb_fx[ind6] ); /*2*(q_sig16)*/
722 29 : Mpy_32_16_ss( tmp_fxL1, 4369, &eshbmean3_fx, &tmp_u16 ); /*10922 : 1/3*/
723 29 : 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 29 : tmp_fxL1 = L_sub( es_mdct_hb_fx[ind4], es_mdct_hb_fx[ind6] ); /*2*(q_sig16)*/
725 29 : test();
726 29 : IF( ( LT_32( tmp_fxL1, eshbmean3_fx ) ) || ( sxylb3_fx < 0 ) )
727 : {
728 17 : ind2 = 0;
729 17 : move16();
730 17 : ind2_sfl = 0;
731 17 : move16();
732 17 : adv = advmem;
733 17 : move16();
734 : }
735 : }
736 : ELSE
737 : {
738 24 : IF( sxyhb2_fx < 0 )
739 : {
740 1 : ind2 = 0;
741 1 : move16();
742 1 : ind2_sfl = 0;
743 1 : move16();
744 1 : adv = advmem;
745 1 : move16(); /* 04042013: small bug corection*/
746 : }
747 : }
748 :
749 53 : tmp_fxL1 = L_add( eshbmean2_fx, es_mdct_hb_fx[ind3] ); /*2*(q_sig16)*/
750 53 : Mpy_32_16_ss( tmp_fxL1, 4369, &eshbmean3_fx, &tmp_u16 ); /*10922 : 1/3*/
751 :
752 53 : tmp_fxL1 = L_sub( es_mdct_hb_fx[ind3], es_mdct_hb_fx[ind5] ); /*2*(q_sig16)*/
753 53 : IF( LT_32( tmp_fxL1, eshbmean3_fx ) )
754 : {
755 2 : ind2 = 0;
756 2 : move16();
757 2 : ind2_sfl = 0;
758 2 : move16();
759 2 : adv = advmem;
760 2 : move16();
761 : }
762 : }
763 : }
764 :
765 4794 : ind2_m1 = sub( ind2, 1 ); /*ind2_m1 needs to be recomputed as ind2 could have changed since*/
766 :
767 4794 : stind = sub( ind2_sfl, adv );
768 4794 : stind_hb = add( stind, advmem );
769 4794 : if ( stind < 0 )
770 : {
771 4759 : stind = 0;
772 4759 : move16();
773 : }
774 :
775 4794 : if ( stind_hb < 0 )
776 : {
777 0 : stind_hb = 0;
778 0 : move16();
779 : }
780 :
781 4794 : tmp_fx1 = add( stind, smooth_len );
782 4794 : fxptr1 = preechogain_fx + tmp_fx1; /*Q15*/
783 4794 : fxptr2 = preechogain_hb_fx + stind_hb; /*Q15*/
784 :
785 4075478 : FOR( i = tmp_fx1; i < framelength; i++ ) /* rest of the gains, without 4 (PREECHO_SMOOTH_LEN) 1 for fadeout */
786 : {
787 4070684 : *( fxptr1++ ) = 32767; /*Q15*/
788 4070684 : move16();
789 : }
790 4794 : pre_g_ch_tab[ind2] = s_min( tmp_fx1, framelength ); /*Q0*/
791 4794 : move16();
792 :
793 4094514 : FOR( i = stind_hb; i < framelength; i++ ) /* rest of the gains*/
794 : {
795 4089720 : *( fxptr2++ ) = 32767; /*Q15*/
796 4089720 : move16();
797 : }
798 :
799 4794 : fxptr1 = preechogain_fx; /*Q15*/
800 23970 : FOR( i = 0; i < smooth_len; i++ )
801 : {
802 19176 : *( fxptr1++ ) = *smoothmem_fx; /*Q15*/
803 19176 : move16();
804 : }
805 :
806 4794 : fattnext_fx = 32767; /*Q15*/
807 4794 : move16();
808 4794 : if ( GT_16( stind, framelength ) )
809 : {
810 0 : fattnext_fx = gt_fx[ind2_m1]; /*Q15*/
811 0 : move16();
812 : }
813 :
814 4794 : fxptr1 = preechogain_fx + framelength; /*Q15*/
815 23970 : FOR( i = 0; i < smooth_len; i++ )
816 : {
817 19176 : *( fxptr1++ ) = fattnext_fx; /*Q15*/
818 19176 : move16();
819 : }
820 :
821 9674 : FOR( i = 0; i <= ind2; i++ )
822 : {
823 4880 : tmp_fx1 = pre_g_ch_tab[i]; /*Q0*/
824 4880 : move16();
825 4880 : tmp_fx2 = sub( tmp_fx1, smooth_len ); /* any index in the previous subframe*/
826 4880 : tmp_fx3 = mult_r( sub( preechogain_fx[tmp_fx1], preechogain_fx[tmp_fx2] ), invsmoothlenp1_fx ); /*step Q15*/
827 4880 : tmp_fx1 = tmp_fx3; /*Q15*/
828 4880 : move16(); /*cumulated step*/
829 4880 : fxptr1 = preechogain_fx + tmp_fx2; /*Q15*/
830 24400 : FOR( j = 0; j < smooth_len; j++ )
831 : {
832 19520 : *fxptr1 = add_sat( *fxptr1, tmp_fx1 ); /*Q15*/
833 19520 : move16();
834 19520 : tmp_fx1 = add( tmp_fx1, tmp_fx3 ); /*Q15*/
835 19520 : fxptr1++;
836 : }
837 : }
838 :
839 4794 : *smoothmem_fx = fattnext_fx; /*Q15*/
840 4794 : move16();
841 4794 : *wmold_hb_fx = preechogain_hb_fx[framelength_m1]; /*Q15*/
842 4794 : move16();
843 :
844 : /* apply gain */
845 4794 : fxptr1 = preechogain_fx; /*Q15*/
846 4794 : fxptr2 = preechogain_hb_fx; /*Q15*/
847 4794 : fxptr3 = rec_sig_fx; /*q_sig16*/
848 4794 : fxptr4 = rec_sig_lb_fx; /*q_sig16*/
849 4794 : fxptr5 = rec_sig_hb_fx; /*q_sig16*/
850 4105914 : FOR( i = 0; i < framelength; i++ )
851 : {
852 4101120 : tmp_fxL1 = L_mult( *fxptr4, *fxptr1 ); /*q_sig16 + Q16*/
853 4101120 : *fxptr3 = mac_r( tmp_fxL1, *fxptr5, *fxptr2 ); /*q_sig16*/
854 4101120 : move16();
855 4101120 : fxptr1++;
856 4101120 : fxptr2++;
857 4101120 : fxptr3++;
858 4101120 : fxptr4++;
859 4101120 : fxptr5++;
860 : }
861 :
862 4794 : 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 38352 : FOR( i = 1; i < NUMSF; i++ ) /* all present subbands */
865 : {
866 33558 : if ( EQ_16( i, NUMSF_S2 ) )
867 : {
868 4794 : savehalfe_fx = L_add( mean_prev_nc_fx_loc, 0 ); /*2*(Q-1)*/
869 : }
870 33558 : mean_prev_nc_fx_loc = L_add_sat( mean_prev_nc_fx_loc, es_mdct_fx[i] ); /*2*(Q-1)*/
871 : }
872 :
873 4794 : if ( LT_32( savehalfe_fx, L_shr( mean_prev_nc_fx_loc, 1 ) ) )
874 : {
875 2075 : mean_prev_nc_fx_loc = L_shl_sat( L_sub_sat( mean_prev_nc_fx_loc, savehalfe_fx ), 1 ); /*2*(Q-1)*/
876 : }
877 4794 : mean_prev_nc_fx_loc = L_shr( mean_prev_nc_fx_loc, 3 ); /* >> LOG2_NUMSF in fixpoint 2*(Q-1)*/
878 :
879 4880 : FOR( i = 0; i < ind2; i++ ) /* only subbands before max energy subband are handled*/
880 : {
881 86 : tmp_fx1 = mult_r( gt_fx[i], gt_fx[i] ); /*Q15*/
882 86 : Mpy_32_16_ss( es_mdct_fx[i], tmp_fx1, &es_mdct_fx[i], &tmp_u16 );
883 :
884 86 : tmp_fx1 = mult_r( gt_hb_fx[i], gt_hb_fx[i] ); /*Q15*/
885 86 : Mpy_32_16_ss( es_mdct_hb_fx[i], tmp_fx1, &es_mdct_hb_fx[i], &tmp_u16 );
886 : }
887 :
888 4794 : mean_prev_fx_loc = L_shr( es_mdct_fx[0], 3 ); /* compute mean used in next frame to limit gain 2*(Q-1)*/
889 4794 : 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 38352 : FOR( i = 1; i < NUMSF; i++ ) /* all present subbands */
892 : {
893 33558 : IF( EQ_16( i, NUMSF_S2 ) )
894 : {
895 4794 : savehalfe_fx = L_add( mean_prev_fx_loc, 0 ); /*2*(Q-1)*/
896 4794 : savehalfe_hb_fx = L_add( mean_prev_hb_fx_loc, 0 ); /*2*(q_sig16)*/
897 : }
898 :
899 33558 : mean_prev_fx_loc = L_add( mean_prev_fx_loc, L_shr( es_mdct_fx[i], 3 ) ); /*2*(Q-1)*/
900 33558 : 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 4794 : tmp_fxL1 = L_sub( mean_prev_fx_loc, savehalfe_fx ); /*2*(Q-1)*/
904 4794 : if ( LT_32( savehalfe_fx, L_shr( mean_prev_fx_loc, 1 ) ) )
905 : {
906 2077 : mean_prev_fx_loc = L_shl( tmp_fxL1, 1 ); /*2*(Q-1)*/
907 : }
908 :
909 4794 : tmp_fxL1 = L_sub( mean_prev_hb_fx_loc, savehalfe_hb_fx ); /*2*(q_sig16)*/
910 4794 : if ( LT_32( savehalfe_hb_fx, L_shr( mean_prev_hb_fx_loc, 1 ) ) )
911 : {
912 2394 : mean_prev_hb_fx_loc = L_shl( tmp_fxL1, 1 ); /*2*(q_sig16)*/
913 : }
914 :
915 4794 : last2_fx = L_shr( L_add_sat( es_mdct_fx[NUMSF_M1], es_mdct_fx[NUMSF_M2] ), 1 ); /*q_sig16*/
916 4794 : last2_hb_fx = L_shr( L_add_sat( es_mdct_hb_fx[NUMSF_M1], es_mdct_hb_fx[NUMSF_M2] ), 1 ); /*q_sig16*/
917 4794 : if ( GT_32( last2_fx, mean_prev_fx_loc ) )
918 : {
919 1715 : mean_prev_fx_loc = L_add( last2_fx, 0 );
920 : }
921 :
922 4794 : if ( GT_32( last2_hb_fx, mean_prev_hb_fx_loc ) )
923 : {
924 2170 : mean_prev_hb_fx_loc = L_add( last2_hb_fx, 0 ); /*2*(q_sig16)*/
925 : }
926 4794 : *mean_prev_fx = mean_prev_fx_loc; /*2*(Q-1)*/
927 4794 : move32();
928 4794 : *mean_prev_hb_fx = L_shr_sat( mean_prev_hb_fx_loc, shl_sat( q_sig16, 1 ) ); /*Q0*/
929 4794 : move32(); /*save in Q0*/
930 4794 : *mean_prev_nc_fx = mean_prev_nc_fx_loc; /*2*(Q-1)*/
931 4794 : move32();
932 : }
933 :
934 7401 : return;
935 : }
936 :
937 : /*--------------------------------------------------------------------------*
938 : * Inverse_Transform()
939 : *
940 : * Inverse transform from the DCT domain to time domain
941 : *--------------------------------------------------------------------------*/
942 :
943 44084 : 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 44084 : IF( is_transient )
972 : {
973 1406 : segment_length = shr( L, 1 );
974 1406 : segment_length_div2 = shr( L, 2 );
975 1406 : segment_length_div4 = shr( L, 3 );
976 :
977 1406 : IF( EQ_16( L, L_FRAME48k ) )
978 : {
979 413 : win = short_window_48kHz_fx; /*Q15*/
980 : }
981 993 : ELSE IF( EQ_16( L, L_FRAME32k ) )
982 : {
983 111 : win = short_window_32kHz_fx; /*Q15*/
984 : }
985 882 : ELSE IF( EQ_16( L, L_FRAME16k ) )
986 : {
987 882 : win = short_window_16kHz_fx; /*Q15*/
988 : }
989 : ELSE /* L == L_FRAME8k */
990 : {
991 0 : win = short_window_8kHz_fx; /*Q15*/
992 : }
993 :
994 1406 : set32_fx( out_alias, 0, L );
995 :
996 1406 : in_segment = in_mdct; /*Q*/
997 1406 : in_segment_modif = in_mdct_modif;
998 :
999 1406 : tmp16 = sub( L, L_inner );
1000 1406 : IF( tmp16 == 0 )
1001 : {
1002 357 : Copy32( in_mdct, in_mdct_modif, L ); /*Q*/
1003 : }
1004 1049 : ELSE IF( tmp16 > 0 )
1005 : {
1006 505 : FOR( seg = 0; seg < NUM_TIME_SWITCHING_BLOCKS; seg++ )
1007 : {
1008 59924 : FOR( ta = 0; ta < L_inner; ta += NUM_TIME_SWITCHING_BLOCKS )
1009 : {
1010 59520 : *in_segment_modif++ = *in_segment++; /*Q*/
1011 59520 : move32();
1012 : }
1013 :
1014 37844 : FOR( ta = 0; ta < tmp16; ta += NUM_TIME_SWITCHING_BLOCKS )
1015 : {
1016 37440 : *in_segment_modif++ = 0L;
1017 37440 : move32();
1018 : }
1019 : }
1020 : }
1021 : ELSE /* L < L_inner */
1022 : {
1023 4740 : FOR( seg = 0; seg < NUM_TIME_SWITCHING_BLOCKS; seg++ )
1024 : {
1025 338512 : FOR( ta = 0; ta < segment_length_div2; ta++ )
1026 : {
1027 334720 : *in_segment_modif++ = *in_segment++; /*Q*/
1028 334720 : move32();
1029 : }
1030 3792 : in_segment += shr( sub( L_inner, L ), 2 ); /*Q*/
1031 3792 : move32();
1032 : }
1033 : }
1034 :
1035 1406 : out_segment = out_alias - segment_length_div4;
1036 1406 : in_segment = in_mdct_modif; /*Q*/
1037 :
1038 1406 : tmp = *Q;
1039 : /* output of 'iedct_short_fx' has up to 'output frame length'/2 # of Elements */
1040 1406 : iedct_short_fx( in_segment, &tmp, alias, segment_length );
1041 1406 : 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 1386 : q_out = 0;
1049 1386 : move16();
1050 : }
1051 :
1052 95126 : FOR( ta = segment_length_div4; ta < segment_length_div2; ta++ )
1053 : {
1054 93720 : out_segment[ta] = L_shr( alias[ta], tmp ); /*q_out*/
1055 93720 : move32();
1056 : }
1057 : /* This previous loop fills the output buffer from [0..seg_len_div4-1] */
1058 :
1059 1406 : win2 = &win[segment_length_div2]; /*Q15*/
1060 188846 : FOR( ta = segment_length_div2; ta < segment_length; ta++ )
1061 : {
1062 187440 : out_segment[ta] = L_shr( Mult_32_16( alias[ta], *--win2 ), tmp ); /*q_out*/
1063 187440 : move32();
1064 : }
1065 : /* This previous loop fills the output buffer from [seg_len_div4..seg_len-seg_len_div4-1] */
1066 :
1067 1406 : out_segment += segment_length_div2; /*q_out*/
1068 1406 : in_segment += segment_length_div2; /*Q*/
1069 :
1070 4218 : FOR( seg = 1; seg < NUM_TIME_SWITCHING_BLOCKS - 1; seg++ )
1071 : {
1072 2812 : tmp = *Q;
1073 2812 : move16();
1074 : /* output of 'iedct_short_fx' has up to 'output frame length'/2 # of Elements */
1075 2812 : iedct_short_fx( in_segment, &tmp, alias, segment_length );
1076 2812 : tmp = sub( tmp, q_out );
1077 :
1078 377692 : FOR( ta = 0; ta < segment_length_div2; ta++ )
1079 : {
1080 374880 : out_segment[ta] = L_add( out_segment[ta], L_shr( Mult_32_16( alias[ta], *win2++ ), tmp ) ); /*q_out*/
1081 374880 : move32();
1082 : }
1083 377692 : FOR( ; ta < segment_length; ta++ )
1084 : {
1085 374880 : out_segment[ta] = L_add( out_segment[ta], L_shr( Mult_32_16( alias[ta], *--win2 ), tmp ) ); /*q_out*/
1086 374880 : move32();
1087 : }
1088 :
1089 2812 : in_segment += segment_length_div2; /*Q*/
1090 2812 : out_segment += segment_length_div2; /*q_out*/
1091 : }
1092 :
1093 1406 : tmp = *Q;
1094 1406 : move16();
1095 1406 : iedct_short_fx( in_segment, &tmp, alias, segment_length );
1096 1406 : tmp = sub( tmp, q_out );
1097 :
1098 188846 : FOR( ta = 0; ta < segment_length_div2; ta++ )
1099 : {
1100 187440 : out_segment[ta] = L_add( out_segment[ta], L_shr( Mult_32_16( alias[ta], *win2++ ), tmp ) ); /*q_out*/
1101 187440 : move32();
1102 : }
1103 :
1104 1406 : seg = add( segment_length_div2, shr( segment_length_div2, 1 ) ); /* seg = 3*segment_length/4 Q0*/
1105 95126 : FOR( ta = segment_length_div2; ta < seg; ta++ )
1106 : {
1107 93720 : out_segment[ta] = L_shr( alias[ta], tmp ); /*q_out*/
1108 93720 : move32();
1109 : }
1110 :
1111 376286 : FOR( ta = 0; ta < segment_length; ta++ )
1112 : {
1113 374880 : L_temp = L_add( out_alias[ta], 0 );
1114 374880 : out[ta] = out_alias[L - 1 - ta]; /*q_out*/
1115 374880 : move32();
1116 374880 : out[L - 1 - ta] = L_temp; /*q_out*/
1117 374880 : move32();
1118 : }
1119 :
1120 1406 : *Q = q_out;
1121 1406 : move16();
1122 : }
1123 : ELSE
1124 : {
1125 42678 : edct_fx( in_mdct, out, L, Q );
1126 : }
1127 44084 : }
|