Line data Source code
1 : /******************************************************************************************************
2 :
3 : (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
4 : Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
5 : Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
6 : Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
7 : contributors to this repository. All Rights Reserved.
8 :
9 : This software is protected by copyright law and by international treaties.
10 : The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
11 : Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
12 : Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
13 : Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
14 : contributors to this repository retain full ownership rights in their respective contributions in
15 : the software. This notice grants no license of any kind, including but not limited to patent
16 : license, nor is any license granted by implication, estoppel or otherwise.
17 :
18 : Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
19 : contributions.
20 :
21 : This software is provided "AS IS", without any express or implied warranties. The software is in the
22 : development stage. It is intended exclusively for experts who have experience with such software and
23 : solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
24 : and fitness for a particular purpose are hereby disclaimed and excluded.
25 :
26 : Any dispute, controversy or claim arising under or in relation to providing this software shall be
27 : submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
28 : accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
29 : the United Nations Convention on Contracts on the International Sales of Goods.
30 :
31 : *******************************************************************************************************/
32 :
33 : /*====================================================================================
34 : EVS Codec 3GPP TS26.443 Nov 04, 2021. Version 12.14.0 / 13.10.0 / 14.6.0 / 15.4.0 / 16.3.0
35 : ====================================================================================*/
36 :
37 : #include <stdint.h>
38 : #include "options.h"
39 : #ifdef DEBUGGING
40 : #include "debug.h"
41 : #endif
42 : #include <math.h>
43 : #include "cnst.h"
44 : #include "rom_com.h"
45 : #include "prot.h"
46 : #include "wmc_auto.h"
47 :
48 : /*--------------------------------------------------------------------------*
49 : * Local constants
50 : *--------------------------------------------------------------------------*/
51 :
52 : #define NUMSF 8
53 : #define LOG2_NUMSF 3
54 : #define INV_NUMSF 0.125f
55 :
56 : /*--------------------------------------------------------------------------*
57 : * preecho_sb()
58 : *
59 : * Time-domain sub-band based pre-echo reduction
60 : *--------------------------------------------------------------------------*/
61 :
62 242251 : void preecho_sb(
63 : const int32_t core_brate, /* i : core bitrate */
64 : const float wtda_audio[], /* i : imdct signal */
65 : float *rec_sig, /* i : reconstructed signal, output of the imdct transform */
66 : const int16_t framelength, /* i : frame length */
67 : float *memfilt_lb, /* i/o: memory */
68 : float *mean_prev_hb, /* i/o: memory */
69 : float *smoothmem, /* i/o: memory */
70 : float *mean_prev, /* i/o: memory */
71 : float *mean_prev_nc, /* i/o: memory */
72 : float *wmold_hb, /* i/o: memory */
73 : int16_t *prevflag, /* i/o: flag */
74 : int16_t *pastpre, /* i/o: flag */
75 : const int16_t bwidth /* i : audio bandwidth */
76 : )
77 : {
78 : int16_t i, j, len3xLp20;
79 : float es_mdct[9]; /* 0..3 (0..7): energy of the 4 (8) subframes, 4..5: (8..10) energy of the future subframes */
80 : float es_mdct_hb[9]; /* 0..3 (0..7): energy of the 4 (8) subframes, 4..5: (8..10) energy of the future subframes */
81 : int16_t zcr[9]; /* 0..3 (0..7): zero crossing of the 4 (8) subframes, 4..5: (8..10) zero crossing of the future subframes */
82 : int16_t maxnzcr[8], cntnzcr; /* max number of samples without zero crossing */
83 : float plus_es_mdct[64]; /* 8*8 subsubframes */
84 : float imdct_mem[L_FRAME48k]; /* memory of the imdct transform, used in the next frame */
85 :
86 : int16_t maxind, stind, stind_hb, cnt2, cnt5, adv, advmem;
87 : int16_t ind2, ind3, pluslim;
88 : float max_es;
89 : float *ptr;
90 : float min_g[13], g, gt[13];
91 : float min_g_hb[13], gt_hb[13];
92 : float preechogain[L_FRAME48k + PREECHO_SMOOTH_LEN];
93 : float preechogain_hb[L_FRAME48k];
94 : int16_t subframelength, subsubframelength;
95 : float maxcrit, savehalfe, savehalfe_hb;
96 : float rec_sig_lb[L_FRAME48k], rec_sig_hb[L_FRAME48k];
97 : float eshbmean2, eshbmean3, sxyhb2, sxylb3, sxyhb3;
98 : float *fptr1, *fptr2, *fptr3, *fptr4, *fptr5, *fptr6;
99 : int16_t *sptr1, *sptr2;
100 : float wmold;
101 : float lim16, lim32;
102 : int16_t limzcr, limmaxnzcr;
103 : float max_es_hb;
104 : int16_t num_subsubframes, log2_num_subsubframes;
105 : float ftmp, fattnext;
106 : float sum_plus_es, mean_plus_es[65];
107 : float last2, last2_hb;
108 : float max_plus_es_mdct;
109 : int16_t nb_flag, smooth_len;
110 : int16_t firstnzcr;
111 : float es_mdct_half[9];
112 : float es_mdct_quart[9];
113 : double invsmoothlenp1; /*double just to have bit-exactness with the previous version, otherwise rounding difference of +-1*/
114 : int16_t subframelength_s2, subframelength_s34;
115 : float oldgain, oldgain_hb;
116 :
117 242251 : if ( core_brate <= HQ_32k )
118 : {
119 168181 : nb_flag = 0;
120 168181 : if ( bwidth == NB )
121 : {
122 427 : nb_flag = 1;
123 : }
124 :
125 168181 : limzcr = 16;
126 168181 : smooth_len = 4;
127 168181 : invsmoothlenp1 = 0.2;
128 :
129 168181 : if ( nb_flag == 1 )
130 : {
131 427 : limzcr = 10;
132 427 : smooth_len = PREECHO_SMOOTH_LEN;
133 427 : invsmoothlenp1 = INV_PREECHO_SMOOTH_LENP1;
134 : }
135 :
136 168181 : limmaxnzcr = framelength / 24;
137 168181 : num_subsubframes = 8;
138 168181 : log2_num_subsubframes = 3;
139 :
140 168181 : if ( framelength == L_FRAME8k )
141 : {
142 731 : num_subsubframes = 4;
143 731 : log2_num_subsubframes = 2;
144 : }
145 168181 : len3xLp20 = framelength / 2 - (int16_t) ( (float) framelength * N_ZERO_MDCT_NS / FRAME_SIZE_NS );
146 28104096 : for ( i = 0; i < len3xLp20; i++ )
147 : {
148 27935915 : imdct_mem[i] = -wtda_audio[len3xLp20 - 1 - i];
149 : }
150 :
151 64021701 : for ( i = 0; i < framelength / 2; i++ )
152 : {
153 63853520 : imdct_mem[len3xLp20 + i] = -wtda_audio[i];
154 : }
155 :
156 :
157 168181 : subframelength = framelength >> LOG2_NUMSF;
158 168181 : subsubframelength = subframelength >> log2_num_subsubframes;
159 168181 : wmold = *smoothmem;
160 168181 : subframelength_s2 = subframelength / 2;
161 168181 : subframelength_s34 = subframelength * 3 / 4;
162 :
163 168181 : cntnzcr = -1;
164 :
165 168181 : lim16 = 0.1f;
166 168181 : lim32 = 0.01f;
167 168181 : savehalfe = 0.0f;
168 168181 : savehalfe_hb = 0.0f;
169 :
170 168181 : if ( *pastpre == 0 )
171 : {
172 : /* if past frame mean energies are not known (no preecho_sb in the past frame), limit max attenuation to 1*/
173 20145 : lim16 = (float) 1;
174 20145 : lim32 = (float) 1;
175 : }
176 :
177 168181 : *pastpre = 2;
178 168181 : fptr1 = rec_sig_lb;
179 168181 : fptr2 = rec_sig;
180 168181 : fptr3 = rec_sig + 1;
181 168181 : fptr4 = rec_sig + 2;
182 168181 : *fptr1 = (float) ( 0.25 * ( *memfilt_lb + *fptr3 ) + 0.5 * *fptr2 );
183 168181 : fptr1++;
184 :
185 127538859 : for ( j = 1; j < framelength - 1; j++ )
186 : {
187 127370678 : *fptr1 = (float) ( 0.25 * ( *fptr2 + *fptr4 ) + 0.5 * *fptr3 );
188 127370678 : fptr1++;
189 127370678 : fptr2++;
190 127370678 : fptr3++;
191 127370678 : fptr4++;
192 : }
193 :
194 168181 : *fptr1 = (float) ( 0.25 * ( *fptr2 ) + 0.5 * *fptr3 );
195 168181 : fptr1 = rec_sig_lb;
196 168181 : fptr2 = rec_sig;
197 168181 : fptr3 = rec_sig_hb;
198 :
199 127875221 : for ( j = 0; j < framelength; j++ )
200 : {
201 127707040 : *fptr3 = *fptr2 - *fptr1;
202 127707040 : fptr1++;
203 127707040 : fptr2++;
204 127707040 : fptr3++;
205 : }
206 :
207 168181 : fptr2--;
208 168181 : *memfilt_lb = *fptr2;
209 :
210 : /* energy of low bands 8 present and 1 future sub-frames */
211 168181 : fptr1 = es_mdct;
212 168181 : fptr5 = es_mdct_half;
213 168181 : fptr6 = es_mdct_quart;
214 168181 : fptr4 = es_mdct_hb;
215 168181 : fptr2 = rec_sig;
216 168181 : fptr3 = rec_sig_hb;
217 168181 : sptr1 = zcr;
218 168181 : *sptr1 = 0;
219 168181 : sptr2 = maxnzcr;
220 1513629 : for ( j = 0; j < NUMSF; j++ ) /* 8 present subframes */
221 : {
222 1345448 : *fptr1 = 100 + *fptr2 * *fptr2;
223 1345448 : *fptr4 = 100 + *fptr3 * *fptr3;
224 :
225 1345448 : *sptr2 = 0;
226 1345448 : firstnzcr = 1;
227 1345448 : if ( j == 0 )
228 : {
229 168181 : firstnzcr = 0;
230 : }
231 :
232 1345448 : fptr2++;
233 1345448 : fptr3++;
234 :
235 127707040 : for ( i = 1; i < subframelength; i++ )
236 : {
237 126361592 : if ( i == subframelength_s2 )
238 : {
239 1345448 : *fptr5 = *fptr1;
240 : }
241 :
242 126361592 : if ( i == subframelength_s34 )
243 : {
244 1345448 : *fptr6 = *fptr1;
245 : }
246 126361592 : *fptr1 += *fptr2 * *fptr2;
247 126361592 : *fptr4 += *fptr3 * *fptr3;
248 126361592 : cntnzcr++;
249 126361592 : if ( *fptr2 * *( fptr2 - 1 ) < 0 )
250 : {
251 19248378 : ( *sptr1 )++;
252 19248378 : if ( cntnzcr > *sptr2 )
253 : {
254 2666510 : *sptr2 = cntnzcr;
255 : }
256 :
257 19248378 : if ( ( firstnzcr > 0 ) && ( cntnzcr > maxnzcr[j - 1] ) )
258 : {
259 273774 : maxnzcr[j - 1] = cntnzcr;
260 : }
261 :
262 19248378 : firstnzcr = 0;
263 19248378 : cntnzcr = -1;
264 : }
265 126361592 : fptr2++;
266 126361592 : fptr3++;
267 : }
268 :
269 1345448 : if ( cntnzcr > *sptr2 )
270 : {
271 181089 : *sptr2 = cntnzcr;
272 : }
273 1345448 : fptr4++;
274 1345448 : sptr1++;
275 1345448 : sptr2++;
276 :
277 1345448 : if ( ( firstnzcr > 0 ) && ( cntnzcr > maxnzcr[j - 1] ) )
278 : {
279 44702 : maxnzcr[j - 1] = cntnzcr;
280 : }
281 :
282 1345448 : *sptr1 = 0;
283 1345448 : if ( ( j < NUMSF - 1 ) && ( *fptr2 * *( fptr2 - 1 ) < 0 ) ) /* zcr between 2 subframes */
284 : {
285 207067 : ( *sptr1 )++; /* counts for the nexte subframe */
286 207067 : cntnzcr = -1;
287 : }
288 :
289 1345448 : if ( *fptr5 < ( *fptr1 ) / 2 )
290 : {
291 666715 : *fptr5 = 2 * ( *fptr1 - *fptr5 );
292 : }
293 : else
294 : {
295 678733 : *fptr5 = *fptr1;
296 : }
297 1345448 : fptr1++;
298 1345448 : fptr5++;
299 1345448 : fptr6++;
300 : }
301 :
302 168181 : fptr2 = imdct_mem;
303 168181 : j = NUMSF;
304 168181 : *fptr1 = 100 + *fptr2 * *fptr2;
305 168181 : *sptr1 = 0;
306 168181 : fptr2++;
307 27935915 : for ( i = 1; i < len3xLp20; i++ ) /* one future subframe but 140 samples (not 80) (enough with ALDO window) */
308 : {
309 27767734 : *fptr1 += *fptr2 * *fptr2;
310 :
311 27767734 : if ( *fptr2 * *( fptr2 - 1 ) < 0 )
312 : {
313 4233935 : ( *sptr1 )++;
314 : }
315 :
316 27767734 : fptr2++;
317 : }
318 :
319 168181 : fptr2 = imdct_mem;
320 168181 : fptr3 = imdct_mem + 1;
321 168181 : fptr4 = imdct_mem + 2;
322 168181 : ftmp = (float) ( -0.25 * ( rec_sig[framelength - 1] + *fptr3 ) + 0.5 * *fptr2 );
323 168181 : es_mdct_hb[NUMSF] = 100 + ftmp * ftmp;
324 :
325 27767734 : for ( j = 1; j < len3xLp20 - 1; j++ )
326 : {
327 27599553 : ftmp = (float) ( -0.25 * ( *fptr2 + *fptr4 ) + 0.5 * *fptr3 );
328 27599553 : es_mdct_hb[NUMSF] += ftmp * ftmp;
329 27599553 : fptr2++;
330 27599553 : fptr3++;
331 27599553 : fptr4++;
332 : }
333 :
334 168181 : ftmp = (float) ( -0.25 * ( *fptr2 ) + 0.5 * *fptr3 );
335 168181 : es_mdct_hb[NUMSF] += ftmp * ftmp;
336 168181 : max_es_hb = es_mdct_hb[0]; /* for memorising the max energy */
337 :
338 168181 : max_es = es_mdct[0]; /* for memorising the max energy */
339 168181 : maxind = 0;
340 1513629 : for ( i = 1; i <= NUMSF; i++ )
341 : {
342 1345448 : if ( es_mdct_hb[i] >= max_es_hb ) /* '=' to handle the first window*/
343 : {
344 415131 : max_es_hb = es_mdct_hb[i]; /* max energy low band, 8 present and 1 future subframes */
345 : }
346 :
347 1345448 : if ( es_mdct[i] >= max_es ) /* '=' to handle the first window*/
348 : {
349 384145 : max_es = es_mdct[i]; /* max energy low band, 8 present and 1 future subframes */
350 384145 : maxind = i;
351 : }
352 : }
353 :
354 168181 : cnt2 = cnt5 = 0;
355 168181 : if ( *prevflag != 0 || max_es < subframelength * 10000 )
356 : {
357 14915 : maxind = 0;
358 : }
359 :
360 168181 : if ( max_es < 4 * *mean_prev )
361 : {
362 138790 : maxind = 0;
363 : }
364 168181 : *prevflag = 0;
365 :
366 200766 : for ( i = 0; i < maxind; i++ ) /* only subbands before max energy subband are handled */
367 : {
368 32585 : g = 1; /* default gain */
369 32585 : min_g[i] = 1;
370 32585 : min_g_hb[i] = 1;
371 32585 : if ( ( es_mdct_half[i] > 100 * ( *mean_prev_nc + 500000 ) ) || /* less then 20% energy in 3/4 of the subframe -> starting onset in the last quarter */
372 27009 : ( ( es_mdct_half[i] > 10 * ( *mean_prev_nc + 500000 ) ) &&
373 7761 : ( ( zcr[i] < limzcr ) || ( es_mdct_quart[i] < es_mdct[i] / 6 ) ) ) ) /* already an offset, plosif, do not touch */
374 : {
375 9461 : maxind = i; /* no preecho reduction after the first subframe with gain 1 */
376 9461 : *prevflag = 1;
377 10940 : for ( j = i - 1; j >= 0; j-- )
378 : {
379 1479 : if ( es_mdct[j] > es_mdct[i] / 2 )
380 : {
381 108 : maxind = j;
382 : }
383 : }
384 : }
385 : else
386 : {
387 23124 : if ( es_mdct[i] < max_es / 16 )
388 : {
389 7688 : g = lim16;
390 7688 : cnt5++;
391 :
392 7688 : if ( es_mdct[i] < max_es / 32 )
393 : {
394 3984 : g = lim32;
395 3984 : cnt2++;
396 : }
397 :
398 7688 : min_g[i] = (float) sqrt( (double) ( *mean_prev / es_mdct[i] ) ); /*limitation of attenuation gain */
399 7688 : min_g_hb[i] = (float) sqrt( (double) ( *mean_prev_hb / es_mdct_hb[i] ) ); /*limitation of attenuation gain */
400 7688 : if ( ( zcr[i] < limzcr / 2 ) || ( maxnzcr[i] > limmaxnzcr ) )
401 : {
402 2563 : if ( min_g[i] < 1 ) /* *mean_prev < es_mdct[i]) */
403 : {
404 1005 : *mean_prev = es_mdct[i];
405 : }
406 2563 : min_g[i] = 1; /* not noise-like, do not touch the amplitude, but may do in HB*/
407 : }
408 :
409 7688 : if ( min_g[i] > 1 ) /* in fix point will be min_g = min(min_g, 1), weight 1 */
410 : {
411 2441 : min_g[i] = (float) 1;
412 : }
413 :
414 7688 : if ( min_g_hb[i] > 1 ) /* in fix point will be min_g = min(min_g, 1), weight 1 */
415 : {
416 3113 : min_g_hb[i] = (float) 1;
417 : }
418 : }
419 : else
420 : {
421 15436 : if ( i > 0 && maxind < NUMSF )
422 : {
423 674 : *prevflag = 1;
424 : }
425 15436 : maxind = i; /* no preecho reduction after the first subframe with gain 1*/
426 : }
427 : }
428 32585 : gt[i] = g;
429 32585 : gt_hb[i] = g;
430 : }
431 :
432 1674242 : for ( i = maxind; i <= NUMSF; i++ ) /* also for the first memory subframe */
433 : {
434 1506061 : gt[i] = 1;
435 1506061 : min_g[i] = 1;
436 1506061 : gt_hb[i] = 1;
437 1506061 : min_g_hb[i] = 1;
438 : }
439 :
440 168181 : ind2 = 0;
441 1513629 : for ( i = 0; i < NUMSF; i++ )
442 : {
443 1345448 : if ( gt[i] < 1 ) /*gt not yet limited by min_g*/
444 : {
445 5727 : ind2 = i + 1; /* first subframe with gain = 1 after last gain < 1 --> frame with the attack*/
446 : }
447 : }
448 :
449 168181 : if ( ( wmold > 0.5 ) && ( ( cnt2 + cnt5 ) < 2 ) ) /* mini either 1 cnt2 (and so also cnt5) or 2 cnt5 */
450 : {
451 : /* maxind = 0; false alarm, no echo reduction */
452 166222 : ind2 = 0;
453 : }
454 :
455 168181 : fptr3 = gt;
456 168181 : fptr4 = gt_hb;
457 168181 : fptr5 = min_g;
458 168181 : fptr6 = min_g_hb;
459 :
460 173190 : for ( i = 0; i < ind2; i++ ) /* only subbands before max energy subband are handled*/
461 : {
462 5009 : if ( *fptr3 < *fptr5 ) /* in fix point will be g = max(g, min_g), weight 1 */
463 : {
464 5009 : *fptr3 = *fptr5;
465 : }
466 :
467 5009 : if ( *fptr4 < *fptr6 ) /* in fix point will be g = max(g, min_g), weight 1 */
468 : {
469 5009 : *fptr4 = *fptr6;
470 : }
471 :
472 5009 : fptr1 = preechogain + i * subframelength + smooth_len;
473 5009 : fptr2 = preechogain_hb + i * subframelength;
474 458229 : for ( j = 0; j < subframelength; j++ )
475 : {
476 453220 : *fptr1 = *fptr3;
477 453220 : *fptr2 = *fptr4;
478 453220 : fptr1++;
479 453220 : fptr2++;
480 : }
481 :
482 5009 : fptr3++;
483 5009 : fptr4++;
484 5009 : fptr5++;
485 5009 : fptr6++;
486 : }
487 :
488 168181 : max_plus_es_mdct = 0;
489 168181 : adv = smooth_len; /* samples needed to have near 1 gain after smoothing at the beggining of the attack subframe*/
490 168181 : advmem = adv;
491 :
492 168181 : if ( ind2 > 0 || wmold < 1 || *wmold_hb < 1 )
493 : {
494 1307 : ptr = imdct_mem;
495 1307 : pluslim = num_subsubframes; /* if ind2 == NUMSF */
496 :
497 1307 : if ( ind2 < NUMSF )
498 : {
499 1200 : ptr = rec_sig + subframelength * ind2;
500 1200 : pluslim = ( NUMSF - ind2 ) * num_subsubframes;
501 : }
502 :
503 1307 : maxcrit = *mean_prev_nc;
504 1307 : if ( ind2 == 0 )
505 : {
506 7 : sum_plus_es = *mean_prev_nc; /* 8 times mean sususb enenrgy (=maxcrit)*/
507 7 : pluslim = num_subsubframes;
508 7 : oldgain = wmold;
509 7 : oldgain_hb = *wmold_hb;
510 : }
511 : else /* ind2 > 0*/
512 : {
513 1300 : sum_plus_es = es_mdct[ind2 - 1]; /* 8 times mean sususb enenrgy (=maxcrit)*/
514 1300 : oldgain = gt[ind2 - 1];
515 1300 : oldgain_hb = gt_hb[ind2 - 1];
516 1300 : maxcrit = es_mdct[ind2 - 1] * gt[ind2 - 1] * gt[ind2 - 1]; /* /1 (iso /8) : 8 times of the pevious subframe mean*/
517 :
518 1300 : if ( ( max_es / 80 > maxcrit ) && ( zcr[ind2] > limzcr ) )
519 : {
520 101 : maxcrit = max_es / 80; /* still 10 times smaller then mean max_es*/
521 : }
522 : }
523 :
524 44999 : for ( j = 0; j < pluslim; j++ ) /* 8 sub-subframes */
525 : {
526 43692 : plus_es_mdct[j] = 100;
527 536672 : for ( i = 0; i < subsubframelength; i++ )
528 : {
529 :
530 492980 : ftmp = *ptr * *ptr;
531 492980 : plus_es_mdct[j] += ftmp;
532 492980 : if ( plus_es_mdct[j] > max_plus_es_mdct )
533 : {
534 48722 : max_plus_es_mdct = plus_es_mdct[j];
535 : }
536 492980 : ptr++;
537 : }
538 :
539 43692 : sum_plus_es += 4 * plus_es_mdct[j];
540 43692 : mean_plus_es[j + 1] = sum_plus_es / ( j + 2 ); /* 4/j could be tabulated in fixed point */
541 43692 : if ( mean_plus_es[j + 1] < maxcrit )
542 : {
543 3581 : mean_plus_es[j + 1] = maxcrit;
544 : }
545 : }
546 1307 : mean_plus_es[0] = plus_es_mdct[0];
547 1307 : if ( mean_plus_es[0] < maxcrit )
548 : {
549 1220 : mean_plus_es[0] = maxcrit;
550 : }
551 1307 : mean_plus_es[pluslim] = -1;
552 :
553 1307 : j = 0;
554 4923 : while ( ( plus_es_mdct[j] < mean_plus_es[j] ) && ( plus_es_mdct[j] < max_plus_es_mdct / 8 ) )
555 : {
556 3616 : j++;
557 : }
558 :
559 1307 : adv -= j * subsubframelength;
560 :
561 1307 : if ( ind2 < NUMSF ) /* onset not in future frame */
562 : {
563 1200 : fptr1 = preechogain + ind2 * subframelength + smooth_len;
564 1200 : fptr2 = preechogain_hb + ind2 * subframelength;
565 :
566 41295 : for ( i = 0; i < j * subsubframelength; i++ )
567 : {
568 40095 : *fptr1 = oldgain; /*keep the gain of the previous subframe*/
569 40095 : *fptr2 = oldgain_hb; /*keep the gain of the previous subframe*/
570 40095 : fptr1++;
571 40095 : fptr2++;
572 : }
573 : }
574 : }
575 :
576 168181 : if ( ind2 > 0 )
577 : {
578 : /* check increasing energy of preecho by regression last 3 subframes (if possible) */
579 1300 : ind3 = ind2 + ( j >> log2_num_subsubframes ); /* return (with rounding) to subframe basis */
580 1300 : if ( ind3 > 1 )
581 : {
582 : /* case of 3 points is simply */
583 1124 : eshbmean2 = es_mdct_hb[ind3 - 1] + es_mdct_hb[ind3 - 2];
584 1124 : sxyhb2 = ( es_mdct_hb[ind3 - 1] - es_mdct_hb[ind3 - 2] ); /* / eshbmean2 * 2; 04042013: division not needed, only sign of sxyhb2 is used*/
585 :
586 1124 : if ( ind3 > 2 )
587 : {
588 872 : eshbmean3 = ( eshbmean2 + es_mdct_hb[ind3 - 3] ) / 3;
589 872 : sxylb3 = ( es_mdct[ind3 - 1] - es_mdct[ind3 - 3] ); /* /eslbmean3 / 2; /2 for 3 points regression calc; 04042013: division not needed, only sign of sxylb3 is used*/
590 872 : sxyhb3 = ( es_mdct_hb[ind3 - 1] - es_mdct_hb[ind3 - 3] ) / eshbmean3 / 2;
591 872 : if ( ( sxyhb3 < 0.2f ) || ( sxylb3 < 0 ) )
592 : {
593 433 : ind2 = 0;
594 433 : adv = advmem;
595 : }
596 : }
597 : else
598 : {
599 252 : if ( sxyhb2 < 0.0f )
600 : {
601 52 : ind2 = 0;
602 52 : adv = advmem; /* 04042013: small bug corection*/
603 : }
604 : }
605 :
606 1124 : eshbmean3 = ( eshbmean2 + es_mdct_hb[ind3] ) / 3; /*verif toward future subsubframe*/
607 1124 : sxyhb3 = ( es_mdct_hb[ind3] - es_mdct_hb[ind3 - 2] ) / eshbmean3 / 2;
608 1124 : if ( sxyhb3 < 0.2f )
609 : {
610 326 : ind2 = 0;
611 326 : adv = advmem;
612 : }
613 : }
614 : }
615 :
616 168181 : stind = ind2 * subframelength - adv;
617 168181 : stind_hb = stind + advmem;
618 168181 : if ( stind < 0 )
619 : {
620 167447 : stind = 0;
621 : }
622 :
623 168181 : if ( stind_hb < 0 )
624 : {
625 0 : stind_hb = 0;
626 : }
627 :
628 168181 : fptr1 = preechogain + stind + smooth_len;
629 168181 : fptr2 = preechogain_hb + stind_hb;
630 :
631 126950529 : for ( i = stind + smooth_len; i < framelength; i++ ) /* rest of the gains, without 4 (PREECHO_SMOOTH_LEN) 1 for fadeout */
632 : {
633 126782348 : *( fptr1++ ) = 1;
634 : }
635 :
636 127626941 : for ( i = stind_hb; i < framelength; i++ ) /* rest of the gains*/
637 : {
638 127458760 : *( fptr2++ ) = 1;
639 : }
640 :
641 847737 : for ( i = 0; i < smooth_len; i++ )
642 : {
643 679556 : preechogain[i] = *smoothmem;
644 : }
645 :
646 168181 : fattnext = 1;
647 168181 : if ( stind > framelength )
648 : {
649 21 : fattnext = gt[ind2 - 1];
650 : }
651 :
652 847737 : for ( i = 0; i < smooth_len; i++ )
653 : {
654 679556 : preechogain[framelength + i] = fattnext;
655 : }
656 :
657 168181 : fptr1 = preechogain;
658 127875221 : for ( i = 0; i < framelength; i++ )
659 : {
660 127707040 : fptr2 = fptr1;
661 640089120 : for ( j = 1; j <= smooth_len; j++ )
662 : {
663 512382080 : *fptr1 += *( ++fptr2 );
664 : }
665 :
666 127707040 : *fptr1 *= (float) invsmoothlenp1;
667 127707040 : fptr1++;
668 : }
669 :
670 168181 : *smoothmem = fattnext;
671 168181 : *wmold_hb = preechogain_hb[framelength - 1];
672 :
673 : /* apply gain */
674 168181 : fptr1 = preechogain;
675 168181 : fptr2 = preechogain_hb;
676 168181 : fptr3 = rec_sig;
677 168181 : fptr4 = rec_sig_lb;
678 168181 : fptr5 = rec_sig_hb;
679 :
680 127875221 : for ( i = 0; i < framelength; i++ )
681 : {
682 127707040 : *fptr3 = *fptr4 * *fptr1 + *fptr5 * *fptr2;
683 127707040 : fptr1++;
684 127707040 : fptr2++;
685 127707040 : fptr3++;
686 127707040 : fptr4++;
687 127707040 : fptr5++;
688 : }
689 :
690 168181 : *mean_prev_nc = es_mdct[0]; /* compute mean not corrected by the actual gains*/
691 :
692 1345448 : for ( i = 1; i < NUMSF; i++ ) /* all present subbands */
693 : {
694 1177267 : if ( i == NUMSF / 2 )
695 : {
696 168181 : savehalfe = *mean_prev_nc;
697 : }
698 1177267 : *mean_prev_nc += es_mdct[i];
699 : }
700 :
701 168181 : if ( savehalfe < *mean_prev_nc / 2 )
702 : {
703 77506 : *mean_prev_nc = 2 * ( *mean_prev_nc - savehalfe );
704 : }
705 168181 : *mean_prev_nc = *mean_prev_nc * INV_NUMSF; /* >> LOG2_NUMSF in fixpoint */
706 :
707 170476 : for ( i = 0; i < ind2; i++ ) /* only subbands before max energy subband are handled*/
708 : {
709 2295 : es_mdct[i] = es_mdct[i] * gt[i] * gt[i];
710 2295 : es_mdct_hb[i] = es_mdct_hb[i] * gt_hb[i] * gt_hb[i];
711 : }
712 :
713 168181 : *mean_prev = es_mdct[0]; /* compute mean used in next frame to limit gain*/
714 168181 : *mean_prev_hb = es_mdct_hb[0]; /* compute mean used in next frame to limit gain*/
715 :
716 1345448 : for ( i = 1; i < NUMSF; i++ ) /* all present subbands */
717 : {
718 1177267 : if ( i == NUMSF / 2 )
719 : {
720 168181 : savehalfe = *mean_prev;
721 168181 : savehalfe_hb = *mean_prev_hb;
722 : }
723 :
724 1177267 : *mean_prev += es_mdct[i];
725 1177267 : *mean_prev_hb += es_mdct_hb[i];
726 : }
727 :
728 168181 : if ( savehalfe < *mean_prev / 2 )
729 : {
730 77507 : *mean_prev = 2 * ( *mean_prev - savehalfe );
731 : }
732 :
733 168181 : if ( savehalfe_hb < *mean_prev_hb / 2 )
734 : {
735 89699 : *mean_prev_hb = 2 * ( *mean_prev_hb - savehalfe_hb );
736 : }
737 :
738 168181 : *mean_prev = *mean_prev * INV_NUMSF; /* >> LOG2_NUMSF in fixpoint */
739 168181 : *mean_prev_hb = *mean_prev_hb * INV_NUMSF; /* >> LOG2_NUMSF in fixpoint */
740 168181 : last2 = ( es_mdct[NUMSF - 1] + es_mdct[NUMSF - 2] ) / 2;
741 168181 : last2_hb = ( es_mdct_hb[NUMSF - 1] + es_mdct_hb[NUMSF - 2] ) / 2;
742 :
743 168181 : if ( last2 > *mean_prev )
744 : {
745 62630 : *mean_prev = last2;
746 : }
747 :
748 168181 : if ( last2_hb > *mean_prev_hb )
749 : {
750 79172 : *mean_prev_hb = last2_hb;
751 : }
752 : }
753 :
754 242251 : return;
755 : }
756 :
757 :
758 : /*--------------------------------------------------------------------------*
759 : * inverse_transform()
760 : *
761 : * Inverse transform from the DCT domain to time domain
762 : *--------------------------------------------------------------------------*/
763 :
764 1877037 : void inverse_transform(
765 : const float *in_mdct, /* i : input MDCT vector */
766 : float *out, /* o : output vector */
767 : const int16_t is_transient, /* i : transient flag */
768 : const int16_t L, /* i : output frame length */
769 : const int16_t L_inner, /* i : length of the transform */
770 : const int16_t element_mode /* i : IVAS element mode */
771 : )
772 : {
773 : float out_alias[L_FRAME48k];
774 : float alias[MAX_SEGMENT_LENGTH];
775 : const float *in_segment;
776 : float *out_segment;
777 : float tmp;
778 : int16_t ta, seg;
779 : int16_t segment_length;
780 : float in_mdct_modif[L_FRAME48k];
781 : float *in_segment_modif;
782 : const float *win;
783 :
784 1877037 : segment_length = L / 2;
785 :
786 1877037 : if ( is_transient )
787 : {
788 28110 : if ( L == L_FRAME48k )
789 : {
790 9372 : win = short_window_48kHz;
791 : }
792 18738 : else if ( L == L_FRAME32k )
793 : {
794 2516 : win = short_window_32kHz;
795 : }
796 16222 : else if ( L == L_FRAME16k )
797 : {
798 16139 : win = short_window_16kHz;
799 : }
800 : else /* L == L_FRAME8k */
801 : {
802 83 : win = short_window_8kHz;
803 : }
804 :
805 28110 : set_f( out_alias, 0.0f, L );
806 :
807 28110 : in_segment = in_mdct;
808 28110 : in_segment_modif = in_mdct_modif;
809 :
810 28110 : if ( L == L_inner )
811 : {
812 10117 : mvr2r( in_mdct, in_mdct_modif, L );
813 : }
814 17993 : else if ( L > L_inner )
815 : {
816 18950 : for ( seg = 0; seg < NUM_TIME_SWITCHING_BLOCKS; seg++ )
817 : {
818 2203800 : for ( ta = 0; ta < L_inner / NUM_TIME_SWITCHING_BLOCKS; ta++ )
819 : {
820 2188640 : *in_segment_modif++ = *in_segment++;
821 : }
822 :
823 1375960 : for ( ta = 0; ta < ( L - L_inner ) / NUM_TIME_SWITCHING_BLOCKS; ta++ )
824 : {
825 1360800 : *in_segment_modif++ = 0;
826 : }
827 : }
828 : }
829 : else /* L < L_inner */
830 : {
831 71015 : for ( seg = 0; seg < NUM_TIME_SWITCHING_BLOCKS; seg++ )
832 : {
833 4793932 : for ( ta = 0; ta < segment_length / 2; ta++ )
834 : {
835 4737120 : *in_segment_modif++ = *in_segment++;
836 : }
837 56812 : in_segment += ( L_inner - L ) / NUM_TIME_SWITCHING_BLOCKS;
838 : }
839 : }
840 :
841 28110 : out_segment = out_alias - segment_length / 4;
842 28110 : in_segment = in_mdct_modif;
843 :
844 28110 : iedct_short( in_segment, alias, segment_length, element_mode );
845 :
846 2001250 : for ( ta = segment_length / 4; ta < segment_length / 2; ta++ )
847 : {
848 1973140 : out_segment[ta] = alias[ta];
849 : }
850 :
851 3974390 : for ( ta = segment_length / 2; ta < segment_length; ta++ )
852 : {
853 3946280 : out_segment[ta] = alias[ta] * win[ta];
854 : }
855 :
856 28110 : out_segment = out_segment + segment_length / 2;
857 28110 : in_segment = in_segment + segment_length / 2;
858 :
859 84330 : for ( seg = 1; seg < NUM_TIME_SWITCHING_BLOCKS - 1; seg++ )
860 : {
861 56220 : iedct_short( in_segment, alias, segment_length, element_mode );
862 :
863 15841340 : for ( ta = 0; ta < segment_length; ta++ )
864 : {
865 15785120 : out_segment[ta] = out_segment[ta] + alias[ta] * win[ta];
866 : }
867 :
868 56220 : in_segment = in_segment + segment_length / 2;
869 56220 : out_segment = out_segment + segment_length / 2;
870 : }
871 :
872 28110 : iedct_short( in_segment, alias, segment_length, element_mode );
873 :
874 3974390 : for ( ta = 0; ta < segment_length / 2; ta++ )
875 : {
876 3946280 : out_segment[ta] = out_segment[ta] + alias[ta] * win[ta];
877 : }
878 :
879 2001250 : for ( ta = segment_length / 2; ta < 3 * segment_length / 4; ta++ )
880 : {
881 1973140 : out_segment[ta] = alias[ta];
882 : }
883 :
884 7920670 : for ( ta = 0; ta < L / 2; ta++ )
885 : {
886 7892560 : tmp = out_alias[ta];
887 7892560 : out[ta] = out_alias[L - 1 - ta];
888 7892560 : out[L - 1 - ta] = tmp;
889 : }
890 : }
891 : else
892 : {
893 1848927 : edct( in_mdct, out, L, element_mode );
894 : }
895 :
896 1877037 : return;
897 : }
|