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 23865 : 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 23865 : if ( core_brate <= HQ_32k )
118 : {
119 15597 : nb_flag = 0;
120 15597 : if ( bwidth == NB )
121 : {
122 0 : nb_flag = 1;
123 : }
124 :
125 15597 : limzcr = 16;
126 15597 : smooth_len = 4;
127 15597 : invsmoothlenp1 = 0.2;
128 :
129 15597 : if ( nb_flag == 1 )
130 : {
131 0 : limzcr = 10;
132 0 : smooth_len = PREECHO_SMOOTH_LEN;
133 0 : invsmoothlenp1 = INV_PREECHO_SMOOTH_LENP1;
134 : }
135 :
136 15597 : limmaxnzcr = framelength / 24;
137 15597 : num_subsubframes = 8;
138 15597 : log2_num_subsubframes = 3;
139 :
140 15597 : if ( framelength == L_FRAME8k )
141 : {
142 0 : num_subsubframes = 4;
143 0 : log2_num_subsubframes = 2;
144 : }
145 15597 : len3xLp20 = framelength / 2 - (int16_t) ( (float) framelength * N_ZERO_MDCT_NS / FRAME_SIZE_NS );
146 2917167 : for ( i = 0; i < len3xLp20; i++ )
147 : {
148 2901570 : imdct_mem[i] = -wtda_audio[len3xLp20 - 1 - i];
149 : }
150 :
151 6647757 : for ( i = 0; i < framelength / 2; i++ )
152 : {
153 6632160 : imdct_mem[len3xLp20 + i] = -wtda_audio[i];
154 : }
155 :
156 :
157 15597 : subframelength = framelength >> LOG2_NUMSF;
158 15597 : subsubframelength = subframelength >> log2_num_subsubframes;
159 15597 : wmold = *smoothmem;
160 15597 : subframelength_s2 = subframelength / 2;
161 15597 : subframelength_s34 = subframelength * 3 / 4;
162 :
163 15597 : cntnzcr = -1;
164 :
165 15597 : lim16 = 0.1f;
166 15597 : lim32 = 0.01f;
167 15597 : savehalfe = 0.0f;
168 15597 : savehalfe_hb = 0.0f;
169 :
170 15597 : 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 2280 : lim16 = (float) 1;
174 2280 : lim32 = (float) 1;
175 : }
176 :
177 15597 : *pastpre = 2;
178 15597 : fptr1 = rec_sig_lb;
179 15597 : fptr2 = rec_sig;
180 15597 : fptr3 = rec_sig + 1;
181 15597 : fptr4 = rec_sig + 2;
182 15597 : *fptr1 = (float) ( 0.25 * ( *memfilt_lb + *fptr3 ) + 0.5 * *fptr2 );
183 15597 : fptr1++;
184 :
185 13248723 : for ( j = 1; j < framelength - 1; j++ )
186 : {
187 13233126 : *fptr1 = (float) ( 0.25 * ( *fptr2 + *fptr4 ) + 0.5 * *fptr3 );
188 13233126 : fptr1++;
189 13233126 : fptr2++;
190 13233126 : fptr3++;
191 13233126 : fptr4++;
192 : }
193 :
194 15597 : *fptr1 = (float) ( 0.25 * ( *fptr2 ) + 0.5 * *fptr3 );
195 15597 : fptr1 = rec_sig_lb;
196 15597 : fptr2 = rec_sig;
197 15597 : fptr3 = rec_sig_hb;
198 :
199 13279917 : for ( j = 0; j < framelength; j++ )
200 : {
201 13264320 : *fptr3 = *fptr2 - *fptr1;
202 13264320 : fptr1++;
203 13264320 : fptr2++;
204 13264320 : fptr3++;
205 : }
206 :
207 15597 : fptr2--;
208 15597 : *memfilt_lb = *fptr2;
209 :
210 : /* energy of low bands 8 present and 1 future sub-frames */
211 15597 : fptr1 = es_mdct;
212 15597 : fptr5 = es_mdct_half;
213 15597 : fptr6 = es_mdct_quart;
214 15597 : fptr4 = es_mdct_hb;
215 15597 : fptr2 = rec_sig;
216 15597 : fptr3 = rec_sig_hb;
217 15597 : sptr1 = zcr;
218 15597 : *sptr1 = 0;
219 15597 : sptr2 = maxnzcr;
220 140373 : for ( j = 0; j < NUMSF; j++ ) /* 8 present subframes */
221 : {
222 124776 : *fptr1 = 100 + *fptr2 * *fptr2;
223 124776 : *fptr4 = 100 + *fptr3 * *fptr3;
224 :
225 124776 : *sptr2 = 0;
226 124776 : firstnzcr = 1;
227 124776 : if ( j == 0 )
228 : {
229 15597 : firstnzcr = 0;
230 : }
231 :
232 124776 : fptr2++;
233 124776 : fptr3++;
234 :
235 13264320 : for ( i = 1; i < subframelength; i++ )
236 : {
237 13139544 : if ( i == subframelength_s2 )
238 : {
239 124776 : *fptr5 = *fptr1;
240 : }
241 :
242 13139544 : if ( i == subframelength_s34 )
243 : {
244 124776 : *fptr6 = *fptr1;
245 : }
246 13139544 : *fptr1 += *fptr2 * *fptr2;
247 13139544 : *fptr4 += *fptr3 * *fptr3;
248 13139544 : cntnzcr++;
249 13139544 : if ( *fptr2 * *( fptr2 - 1 ) < 0 )
250 : {
251 2022372 : ( *sptr1 )++;
252 2022372 : if ( cntnzcr > *sptr2 )
253 : {
254 266553 : *sptr2 = cntnzcr;
255 : }
256 :
257 2022372 : if ( ( firstnzcr > 0 ) && ( cntnzcr > maxnzcr[j - 1] ) )
258 : {
259 25959 : maxnzcr[j - 1] = cntnzcr;
260 : }
261 :
262 2022372 : firstnzcr = 0;
263 2022372 : cntnzcr = -1;
264 : }
265 13139544 : fptr2++;
266 13139544 : fptr3++;
267 : }
268 :
269 124776 : if ( cntnzcr > *sptr2 )
270 : {
271 13098 : *sptr2 = cntnzcr;
272 : }
273 124776 : fptr4++;
274 124776 : sptr1++;
275 124776 : sptr2++;
276 :
277 124776 : if ( ( firstnzcr > 0 ) && ( cntnzcr > maxnzcr[j - 1] ) )
278 : {
279 1554 : maxnzcr[j - 1] = cntnzcr;
280 : }
281 :
282 124776 : *sptr1 = 0;
283 124776 : if ( ( j < NUMSF - 1 ) && ( *fptr2 * *( fptr2 - 1 ) < 0 ) ) /* zcr between 2 subframes */
284 : {
285 18288 : ( *sptr1 )++; /* counts for the nexte subframe */
286 18288 : cntnzcr = -1;
287 : }
288 :
289 124776 : if ( *fptr5 < ( *fptr1 ) / 2 )
290 : {
291 61923 : *fptr5 = 2 * ( *fptr1 - *fptr5 );
292 : }
293 : else
294 : {
295 62853 : *fptr5 = *fptr1;
296 : }
297 124776 : fptr1++;
298 124776 : fptr5++;
299 124776 : fptr6++;
300 : }
301 :
302 15597 : fptr2 = imdct_mem;
303 15597 : j = NUMSF;
304 15597 : *fptr1 = 100 + *fptr2 * *fptr2;
305 15597 : *sptr1 = 0;
306 15597 : fptr2++;
307 2901570 : for ( i = 1; i < len3xLp20; i++ ) /* one future subframe but 140 samples (not 80) (enough with ALDO window) */
308 : {
309 2885973 : *fptr1 += *fptr2 * *fptr2;
310 :
311 2885973 : if ( *fptr2 * *( fptr2 - 1 ) < 0 )
312 : {
313 438072 : ( *sptr1 )++;
314 : }
315 :
316 2885973 : fptr2++;
317 : }
318 :
319 15597 : fptr2 = imdct_mem;
320 15597 : fptr3 = imdct_mem + 1;
321 15597 : fptr4 = imdct_mem + 2;
322 15597 : ftmp = (float) ( -0.25 * ( rec_sig[framelength - 1] + *fptr3 ) + 0.5 * *fptr2 );
323 15597 : es_mdct_hb[NUMSF] = 100 + ftmp * ftmp;
324 :
325 2885973 : for ( j = 1; j < len3xLp20 - 1; j++ )
326 : {
327 2870376 : ftmp = (float) ( -0.25 * ( *fptr2 + *fptr4 ) + 0.5 * *fptr3 );
328 2870376 : es_mdct_hb[NUMSF] += ftmp * ftmp;
329 2870376 : fptr2++;
330 2870376 : fptr3++;
331 2870376 : fptr4++;
332 : }
333 :
334 15597 : ftmp = (float) ( -0.25 * ( *fptr2 ) + 0.5 * *fptr3 );
335 15597 : es_mdct_hb[NUMSF] += ftmp * ftmp;
336 15597 : max_es_hb = es_mdct_hb[0]; /* for memorising the max energy */
337 :
338 15597 : max_es = es_mdct[0]; /* for memorising the max energy */
339 15597 : maxind = 0;
340 140373 : for ( i = 1; i <= NUMSF; i++ )
341 : {
342 124776 : if ( es_mdct_hb[i] >= max_es_hb ) /* '=' to handle the first window*/
343 : {
344 37782 : max_es_hb = es_mdct_hb[i]; /* max energy low band, 8 present and 1 future subframes */
345 : }
346 :
347 124776 : if ( es_mdct[i] >= max_es ) /* '=' to handle the first window*/
348 : {
349 35496 : max_es = es_mdct[i]; /* max energy low band, 8 present and 1 future subframes */
350 35496 : maxind = i;
351 : }
352 : }
353 :
354 15597 : cnt2 = cnt5 = 0;
355 15597 : if ( *prevflag != 0 || max_es < subframelength * 10000 )
356 : {
357 1746 : maxind = 0;
358 : }
359 :
360 15597 : if ( max_es < 4 * *mean_prev )
361 : {
362 12354 : maxind = 0;
363 : }
364 15597 : *prevflag = 0;
365 :
366 19323 : for ( i = 0; i < maxind; i++ ) /* only subbands before max energy subband are handled */
367 : {
368 3726 : g = 1; /* default gain */
369 3726 : min_g[i] = 1;
370 3726 : min_g_hb[i] = 1;
371 3726 : 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 3228 : ( ( es_mdct_half[i] > 10 * ( *mean_prev_nc + 500000 ) ) &&
373 1236 : ( ( zcr[i] < limzcr ) || ( es_mdct_quart[i] < es_mdct[i] / 6 ) ) ) ) /* already an offset, plosif, do not touch */
374 : {
375 993 : maxind = i; /* no preecho reduction after the first subframe with gain 1 */
376 993 : *prevflag = 1;
377 1200 : for ( j = i - 1; j >= 0; j-- )
378 : {
379 207 : if ( es_mdct[j] > es_mdct[i] / 2 )
380 : {
381 45 : maxind = j;
382 : }
383 : }
384 : }
385 : else
386 : {
387 2733 : if ( es_mdct[i] < max_es / 16 )
388 : {
389 1017 : g = lim16;
390 1017 : cnt5++;
391 :
392 1017 : if ( es_mdct[i] < max_es / 32 )
393 : {
394 321 : g = lim32;
395 321 : cnt2++;
396 : }
397 :
398 1017 : min_g[i] = (float) sqrt( (double) ( *mean_prev / es_mdct[i] ) ); /*limitation of attenuation gain */
399 1017 : min_g_hb[i] = (float) sqrt( (double) ( *mean_prev_hb / es_mdct_hb[i] ) ); /*limitation of attenuation gain */
400 1017 : if ( ( zcr[i] < limzcr / 2 ) || ( maxnzcr[i] > limmaxnzcr ) )
401 : {
402 138 : if ( min_g[i] < 1 ) /* *mean_prev < es_mdct[i]) */
403 : {
404 84 : *mean_prev = es_mdct[i];
405 : }
406 138 : min_g[i] = 1; /* not noise-like, do not touch the amplitude, but may do in HB*/
407 : }
408 :
409 1017 : if ( min_g[i] > 1 ) /* in fix point will be min_g = min(min_g, 1), weight 1 */
410 : {
411 327 : min_g[i] = (float) 1;
412 : }
413 :
414 1017 : if ( min_g_hb[i] > 1 ) /* in fix point will be min_g = min(min_g, 1), weight 1 */
415 : {
416 252 : min_g_hb[i] = (float) 1;
417 : }
418 : }
419 : else
420 : {
421 1716 : if ( i > 0 && maxind < NUMSF )
422 : {
423 117 : *prevflag = 1;
424 : }
425 1716 : maxind = i; /* no preecho reduction after the first subframe with gain 1*/
426 : }
427 : }
428 3726 : gt[i] = g;
429 3726 : gt_hb[i] = g;
430 : }
431 :
432 154998 : for ( i = maxind; i <= NUMSF; i++ ) /* also for the first memory subframe */
433 : {
434 139401 : gt[i] = 1;
435 139401 : min_g[i] = 1;
436 139401 : gt_hb[i] = 1;
437 139401 : min_g_hb[i] = 1;
438 : }
439 :
440 15597 : ind2 = 0;
441 140373 : for ( i = 0; i < NUMSF; i++ )
442 : {
443 124776 : if ( gt[i] < 1 ) /*gt not yet limited by min_g*/
444 : {
445 672 : ind2 = i + 1; /* first subframe with gain = 1 after last gain < 1 --> frame with the attack*/
446 : }
447 : }
448 :
449 15597 : 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 15285 : ind2 = 0;
453 : }
454 :
455 15597 : fptr3 = gt;
456 15597 : fptr4 = gt_hb;
457 15597 : fptr5 = min_g;
458 15597 : fptr6 = min_g_hb;
459 :
460 16170 : for ( i = 0; i < ind2; i++ ) /* only subbands before max energy subband are handled*/
461 : {
462 573 : if ( *fptr3 < *fptr5 ) /* in fix point will be g = max(g, min_g), weight 1 */
463 : {
464 573 : *fptr3 = *fptr5;
465 : }
466 :
467 573 : if ( *fptr4 < *fptr6 ) /* in fix point will be g = max(g, min_g), weight 1 */
468 : {
469 573 : *fptr4 = *fptr6;
470 : }
471 :
472 573 : fptr1 = preechogain + i * subframelength + smooth_len;
473 573 : fptr2 = preechogain_hb + i * subframelength;
474 64173 : for ( j = 0; j < subframelength; j++ )
475 : {
476 63600 : *fptr1 = *fptr3;
477 63600 : *fptr2 = *fptr4;
478 63600 : fptr1++;
479 63600 : fptr2++;
480 : }
481 :
482 573 : fptr3++;
483 573 : fptr4++;
484 573 : fptr5++;
485 573 : fptr6++;
486 : }
487 :
488 15597 : max_plus_es_mdct = 0;
489 15597 : adv = smooth_len; /* samples needed to have near 1 gain after smoothing at the beggining of the attack subframe*/
490 15597 : advmem = adv;
491 :
492 15597 : if ( ind2 > 0 || wmold < 1 || *wmold_hb < 1 )
493 : {
494 177 : ptr = imdct_mem;
495 177 : pluslim = num_subsubframes; /* if ind2 == NUMSF */
496 :
497 177 : if ( ind2 < NUMSF )
498 : {
499 174 : ptr = rec_sig + subframelength * ind2;
500 174 : pluslim = ( NUMSF - ind2 ) * num_subsubframes;
501 : }
502 :
503 177 : maxcrit = *mean_prev_nc;
504 177 : if ( ind2 == 0 )
505 : {
506 0 : sum_plus_es = *mean_prev_nc; /* 8 times mean sususb enenrgy (=maxcrit)*/
507 0 : pluslim = num_subsubframes;
508 0 : oldgain = wmold;
509 0 : oldgain_hb = *wmold_hb;
510 : }
511 : else /* ind2 > 0*/
512 : {
513 177 : sum_plus_es = es_mdct[ind2 - 1]; /* 8 times mean sususb enenrgy (=maxcrit)*/
514 177 : oldgain = gt[ind2 - 1];
515 177 : oldgain_hb = gt_hb[ind2 - 1];
516 177 : maxcrit = es_mdct[ind2 - 1] * gt[ind2 - 1] * gt[ind2 - 1]; /* /1 (iso /8) : 8 times of the pevious subframe mean*/
517 :
518 177 : if ( ( max_es / 80 > maxcrit ) && ( zcr[ind2] > limzcr ) )
519 : {
520 57 : maxcrit = max_es / 80; /* still 10 times smaller then mean max_es*/
521 : }
522 : }
523 :
524 6945 : for ( j = 0; j < pluslim; j++ ) /* 8 sub-subframes */
525 : {
526 6768 : plus_es_mdct[j] = 100;
527 100008 : for ( i = 0; i < subsubframelength; i++ )
528 : {
529 :
530 93240 : ftmp = *ptr * *ptr;
531 93240 : plus_es_mdct[j] += ftmp;
532 93240 : if ( plus_es_mdct[j] > max_plus_es_mdct )
533 : {
534 7485 : max_plus_es_mdct = plus_es_mdct[j];
535 : }
536 93240 : ptr++;
537 : }
538 :
539 6768 : sum_plus_es += 4 * plus_es_mdct[j];
540 6768 : mean_plus_es[j + 1] = sum_plus_es / ( j + 2 ); /* 4/j could be tabulated in fixed point */
541 6768 : if ( mean_plus_es[j + 1] < maxcrit )
542 : {
543 1056 : mean_plus_es[j + 1] = maxcrit;
544 : }
545 : }
546 177 : mean_plus_es[0] = plus_es_mdct[0];
547 177 : if ( mean_plus_es[0] < maxcrit )
548 : {
549 150 : mean_plus_es[0] = maxcrit;
550 : }
551 177 : mean_plus_es[pluslim] = -1;
552 :
553 177 : j = 0;
554 861 : while ( ( plus_es_mdct[j] < mean_plus_es[j] ) && ( plus_es_mdct[j] < max_plus_es_mdct / 8 ) )
555 : {
556 684 : j++;
557 : }
558 :
559 177 : adv -= j * subsubframelength;
560 :
561 177 : if ( ind2 < NUMSF ) /* onset not in future frame */
562 : {
563 174 : fptr1 = preechogain + ind2 * subframelength + smooth_len;
564 174 : fptr2 = preechogain_hb + ind2 * subframelength;
565 :
566 9294 : for ( i = 0; i < j * subsubframelength; i++ )
567 : {
568 9120 : *fptr1 = oldgain; /*keep the gain of the previous subframe*/
569 9120 : *fptr2 = oldgain_hb; /*keep the gain of the previous subframe*/
570 9120 : fptr1++;
571 9120 : fptr2++;
572 : }
573 : }
574 : }
575 :
576 15597 : if ( ind2 > 0 )
577 : {
578 : /* check increasing energy of preecho by regression last 3 subframes (if possible) */
579 177 : ind3 = ind2 + ( j >> log2_num_subsubframes ); /* return (with rounding) to subframe basis */
580 177 : if ( ind3 > 1 )
581 : {
582 : /* case of 3 points is simply */
583 177 : eshbmean2 = es_mdct_hb[ind3 - 1] + es_mdct_hb[ind3 - 2];
584 177 : 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 177 : if ( ind3 > 2 )
587 : {
588 102 : eshbmean3 = ( eshbmean2 + es_mdct_hb[ind3 - 3] ) / 3;
589 102 : 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 102 : sxyhb3 = ( es_mdct_hb[ind3 - 1] - es_mdct_hb[ind3 - 3] ) / eshbmean3 / 2;
591 102 : if ( ( sxyhb3 < 0.2f ) || ( sxylb3 < 0 ) )
592 : {
593 60 : ind2 = 0;
594 60 : adv = advmem;
595 : }
596 : }
597 : else
598 : {
599 75 : if ( sxyhb2 < 0.0f )
600 : {
601 0 : ind2 = 0;
602 0 : adv = advmem; /* 04042013: small bug corection*/
603 : }
604 : }
605 :
606 177 : eshbmean3 = ( eshbmean2 + es_mdct_hb[ind3] ) / 3; /*verif toward future subsubframe*/
607 177 : sxyhb3 = ( es_mdct_hb[ind3] - es_mdct_hb[ind3 - 2] ) / eshbmean3 / 2;
608 177 : if ( sxyhb3 < 0.2f )
609 : {
610 18 : ind2 = 0;
611 18 : adv = advmem;
612 : }
613 : }
614 : }
615 :
616 15597 : stind = ind2 * subframelength - adv;
617 15597 : stind_hb = stind + advmem;
618 15597 : if ( stind < 0 )
619 : {
620 15486 : stind = 0;
621 : }
622 :
623 15597 : if ( stind_hb < 0 )
624 : {
625 0 : stind_hb = 0;
626 : }
627 :
628 15597 : fptr1 = preechogain + stind + smooth_len;
629 15597 : fptr2 = preechogain_hb + stind_hb;
630 :
631 13181028 : for ( i = stind + smooth_len; i < framelength; i++ ) /* rest of the gains, without 4 (PREECHO_SMOOTH_LEN) 1 for fadeout */
632 : {
633 13165431 : *( fptr1++ ) = 1;
634 : }
635 :
636 13242972 : for ( i = stind_hb; i < framelength; i++ ) /* rest of the gains*/
637 : {
638 13227375 : *( fptr2++ ) = 1;
639 : }
640 :
641 77985 : for ( i = 0; i < smooth_len; i++ )
642 : {
643 62388 : preechogain[i] = *smoothmem;
644 : }
645 :
646 15597 : fattnext = 1;
647 15597 : if ( stind > framelength )
648 : {
649 0 : fattnext = gt[ind2 - 1];
650 : }
651 :
652 77985 : for ( i = 0; i < smooth_len; i++ )
653 : {
654 62388 : preechogain[framelength + i] = fattnext;
655 : }
656 :
657 15597 : fptr1 = preechogain;
658 13279917 : for ( i = 0; i < framelength; i++ )
659 : {
660 13264320 : fptr2 = fptr1;
661 66321600 : for ( j = 1; j <= smooth_len; j++ )
662 : {
663 53057280 : *fptr1 += *( ++fptr2 );
664 : }
665 :
666 13264320 : *fptr1 *= (float) invsmoothlenp1;
667 13264320 : fptr1++;
668 : }
669 :
670 15597 : *smoothmem = fattnext;
671 15597 : *wmold_hb = preechogain_hb[framelength - 1];
672 :
673 : /* apply gain */
674 15597 : fptr1 = preechogain;
675 15597 : fptr2 = preechogain_hb;
676 15597 : fptr3 = rec_sig;
677 15597 : fptr4 = rec_sig_lb;
678 15597 : fptr5 = rec_sig_hb;
679 :
680 13279917 : for ( i = 0; i < framelength; i++ )
681 : {
682 13264320 : *fptr3 = *fptr4 * *fptr1 + *fptr5 * *fptr2;
683 13264320 : fptr1++;
684 13264320 : fptr2++;
685 13264320 : fptr3++;
686 13264320 : fptr4++;
687 13264320 : fptr5++;
688 : }
689 :
690 15597 : *mean_prev_nc = es_mdct[0]; /* compute mean not corrected by the actual gains*/
691 :
692 124776 : for ( i = 1; i < NUMSF; i++ ) /* all present subbands */
693 : {
694 109179 : if ( i == NUMSF / 2 )
695 : {
696 15597 : savehalfe = *mean_prev_nc;
697 : }
698 109179 : *mean_prev_nc += es_mdct[i];
699 : }
700 :
701 15597 : if ( savehalfe < *mean_prev_nc / 2 )
702 : {
703 6882 : *mean_prev_nc = 2 * ( *mean_prev_nc - savehalfe );
704 : }
705 15597 : *mean_prev_nc = *mean_prev_nc * INV_NUMSF; /* >> LOG2_NUMSF in fixpoint */
706 :
707 15870 : for ( i = 0; i < ind2; i++ ) /* only subbands before max energy subband are handled*/
708 : {
709 273 : es_mdct[i] = es_mdct[i] * gt[i] * gt[i];
710 273 : es_mdct_hb[i] = es_mdct_hb[i] * gt_hb[i] * gt_hb[i];
711 : }
712 :
713 15597 : *mean_prev = es_mdct[0]; /* compute mean used in next frame to limit gain*/
714 15597 : *mean_prev_hb = es_mdct_hb[0]; /* compute mean used in next frame to limit gain*/
715 :
716 124776 : for ( i = 1; i < NUMSF; i++ ) /* all present subbands */
717 : {
718 109179 : if ( i == NUMSF / 2 )
719 : {
720 15597 : savehalfe = *mean_prev;
721 15597 : savehalfe_hb = *mean_prev_hb;
722 : }
723 :
724 109179 : *mean_prev += es_mdct[i];
725 109179 : *mean_prev_hb += es_mdct_hb[i];
726 : }
727 :
728 15597 : if ( savehalfe < *mean_prev / 2 )
729 : {
730 6882 : *mean_prev = 2 * ( *mean_prev - savehalfe );
731 : }
732 :
733 15597 : if ( savehalfe_hb < *mean_prev_hb / 2 )
734 : {
735 8076 : *mean_prev_hb = 2 * ( *mean_prev_hb - savehalfe_hb );
736 : }
737 :
738 15597 : *mean_prev = *mean_prev * INV_NUMSF; /* >> LOG2_NUMSF in fixpoint */
739 15597 : *mean_prev_hb = *mean_prev_hb * INV_NUMSF; /* >> LOG2_NUMSF in fixpoint */
740 15597 : last2 = ( es_mdct[NUMSF - 1] + es_mdct[NUMSF - 2] ) / 2;
741 15597 : last2_hb = ( es_mdct_hb[NUMSF - 1] + es_mdct_hb[NUMSF - 2] ) / 2;
742 :
743 15597 : if ( last2 > *mean_prev )
744 : {
745 5535 : *mean_prev = last2;
746 : }
747 :
748 15597 : if ( last2_hb > *mean_prev_hb )
749 : {
750 7410 : *mean_prev_hb = last2_hb;
751 : }
752 : }
753 :
754 23865 : return;
755 : }
756 :
757 :
758 : /*--------------------------------------------------------------------------*
759 : * inverse_transform()
760 : *
761 : * Inverse transform from the DCT domain to time domain
762 : *--------------------------------------------------------------------------*/
763 :
764 130287 : 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 130287 : segment_length = L / 2;
785 :
786 130287 : if ( is_transient )
787 : {
788 3895 : if ( L == L_FRAME48k )
789 : {
790 1431 : win = short_window_48kHz;
791 : }
792 2464 : else if ( L == L_FRAME32k )
793 : {
794 387 : win = short_window_32kHz;
795 : }
796 2077 : else if ( L == L_FRAME16k )
797 : {
798 2077 : win = short_window_16kHz;
799 : }
800 : else /* L == L_FRAME8k */
801 : {
802 0 : win = short_window_8kHz;
803 : }
804 :
805 3895 : set_f( out_alias, 0.0f, L );
806 :
807 3895 : in_segment = in_mdct;
808 3895 : in_segment_modif = in_mdct_modif;
809 :
810 3895 : if ( L == L_inner )
811 : {
812 1207 : mvr2r( in_mdct, in_mdct_modif, L );
813 : }
814 2688 : else if ( L > L_inner )
815 : {
816 1875 : for ( seg = 0; seg < NUM_TIME_SWITCHING_BLOCKS; seg++ )
817 : {
818 223260 : for ( ta = 0; ta < L_inner / NUM_TIME_SWITCHING_BLOCKS; ta++ )
819 : {
820 221760 : *in_segment_modif++ = *in_segment++;
821 : }
822 :
823 139740 : for ( ta = 0; ta < ( L - L_inner ) / NUM_TIME_SWITCHING_BLOCKS; ta++ )
824 : {
825 138240 : *in_segment_modif++ = 0;
826 : }
827 : }
828 : }
829 : else /* L < L_inner */
830 : {
831 11565 : for ( seg = 0; seg < NUM_TIME_SWITCHING_BLOCKS; seg++ )
832 : {
833 849252 : for ( ta = 0; ta < segment_length / 2; ta++ )
834 : {
835 840000 : *in_segment_modif++ = *in_segment++;
836 : }
837 9252 : in_segment += ( L_inner - L ) / NUM_TIME_SWITCHING_BLOCKS;
838 : }
839 : }
840 :
841 3895 : out_segment = out_alias - segment_length / 4;
842 3895 : in_segment = in_mdct_modif;
843 :
844 3895 : iedct_short( in_segment, alias, segment_length, element_mode );
845 :
846 289655 : for ( ta = segment_length / 4; ta < segment_length / 2; ta++ )
847 : {
848 285760 : out_segment[ta] = alias[ta];
849 : }
850 :
851 575415 : for ( ta = segment_length / 2; ta < segment_length; ta++ )
852 : {
853 571520 : out_segment[ta] = alias[ta] * win[ta];
854 : }
855 :
856 3895 : out_segment = out_segment + segment_length / 2;
857 3895 : in_segment = in_segment + segment_length / 2;
858 :
859 11685 : for ( seg = 1; seg < NUM_TIME_SWITCHING_BLOCKS - 1; seg++ )
860 : {
861 7790 : iedct_short( in_segment, alias, segment_length, element_mode );
862 :
863 2293870 : for ( ta = 0; ta < segment_length; ta++ )
864 : {
865 2286080 : out_segment[ta] = out_segment[ta] + alias[ta] * win[ta];
866 : }
867 :
868 7790 : in_segment = in_segment + segment_length / 2;
869 7790 : out_segment = out_segment + segment_length / 2;
870 : }
871 :
872 3895 : iedct_short( in_segment, alias, segment_length, element_mode );
873 :
874 575415 : for ( ta = 0; ta < segment_length / 2; ta++ )
875 : {
876 571520 : out_segment[ta] = out_segment[ta] + alias[ta] * win[ta];
877 : }
878 :
879 289655 : for ( ta = segment_length / 2; ta < 3 * segment_length / 4; ta++ )
880 : {
881 285760 : out_segment[ta] = alias[ta];
882 : }
883 :
884 1146935 : for ( ta = 0; ta < L / 2; ta++ )
885 : {
886 1143040 : tmp = out_alias[ta];
887 1143040 : out[ta] = out_alias[L - 1 - ta];
888 1143040 : out[L - 1 - ta] = tmp;
889 : }
890 : }
891 : else
892 : {
893 126392 : edct( in_mdct, out, L, element_mode );
894 : }
895 :
896 130287 : return;
897 : }
|