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