Coverage Report

Created: 2026-01-13 06:51

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/aac/libAACdec/src/usacdec_fac.cpp
Line
Count
Source
1
/* -----------------------------------------------------------------------------
2
Software License for The Fraunhofer FDK AAC Codec Library for Android
3
4
© Copyright  1995 - 2019 Fraunhofer-Gesellschaft zur Förderung der angewandten
5
Forschung e.V. All rights reserved.
6
7
 1.    INTRODUCTION
8
The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software
9
that implements the MPEG Advanced Audio Coding ("AAC") encoding and decoding
10
scheme for digital audio. This FDK AAC Codec software is intended to be used on
11
a wide variety of Android devices.
12
13
AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient
14
general perceptual audio codecs. AAC-ELD is considered the best-performing
15
full-bandwidth communications codec by independent studies and is widely
16
deployed. AAC has been standardized by ISO and IEC as part of the MPEG
17
specifications.
18
19
Patent licenses for necessary patent claims for the FDK AAC Codec (including
20
those of Fraunhofer) may be obtained through Via Licensing
21
(www.vialicensing.com) or through the respective patent owners individually for
22
the purpose of encoding or decoding bit streams in products that are compliant
23
with the ISO/IEC MPEG audio standards. Please note that most manufacturers of
24
Android devices already license these patent claims through Via Licensing or
25
directly from the patent owners, and therefore FDK AAC Codec software may
26
already be covered under those patent licenses when it is used for those
27
licensed purposes only.
28
29
Commercially-licensed AAC software libraries, including floating-point versions
30
with enhanced sound quality, are also available from Fraunhofer. Users are
31
encouraged to check the Fraunhofer website for additional applications
32
information and documentation.
33
34
2.    COPYRIGHT LICENSE
35
36
Redistribution and use in source and binary forms, with or without modification,
37
are permitted without payment of copyright license fees provided that you
38
satisfy the following conditions:
39
40
You must retain the complete text of this software license in redistributions of
41
the FDK AAC Codec or your modifications thereto in source code form.
42
43
You must retain the complete text of this software license in the documentation
44
and/or other materials provided with redistributions of the FDK AAC Codec or
45
your modifications thereto in binary form. You must make available free of
46
charge copies of the complete source code of the FDK AAC Codec and your
47
modifications thereto to recipients of copies in binary form.
48
49
The name of Fraunhofer may not be used to endorse or promote products derived
50
from this library without prior written permission.
51
52
You may not charge copyright license fees for anyone to use, copy or distribute
53
the FDK AAC Codec software or your modifications thereto.
54
55
Your modified versions of the FDK AAC Codec must carry prominent notices stating
56
that you changed the software and the date of any change. For modified versions
57
of the FDK AAC Codec, the term "Fraunhofer FDK AAC Codec Library for Android"
58
must be replaced by the term "Third-Party Modified Version of the Fraunhofer FDK
59
AAC Codec Library for Android."
60
61
3.    NO PATENT LICENSE
62
63
NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without
64
limitation the patents of Fraunhofer, ARE GRANTED BY THIS SOFTWARE LICENSE.
65
Fraunhofer provides no warranty of patent non-infringement with respect to this
66
software.
67
68
You may use this FDK AAC Codec software or modifications thereto only for
69
purposes that are authorized by appropriate patent licenses.
70
71
4.    DISCLAIMER
72
73
This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright
74
holders and contributors "AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES,
75
including but not limited to the implied warranties of merchantability and
76
fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
77
CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary,
78
or consequential damages, including but not limited to procurement of substitute
79
goods or services; loss of use, data, or profits, or business interruption,
80
however caused and on any theory of liability, whether in contract, strict
81
liability, or tort (including negligence), arising in any way out of the use of
82
this software, even if advised of the possibility of such damage.
83
84
5.    CONTACT INFORMATION
85
86
Fraunhofer Institute for Integrated Circuits IIS
87
Attention: Audio and Multimedia Departments - FDK AAC LL
88
Am Wolfsmantel 33
89
91058 Erlangen, Germany
90
91
www.iis.fraunhofer.de/amm
92
amm-info@iis.fraunhofer.de
93
----------------------------------------------------------------------------- */
94
95
/**************************** AAC decoder library ******************************
96
97
   Author(s):   Manuel Jander
98
99
   Description: USAC FAC
100
101
*******************************************************************************/
102
103
#include "usacdec_fac.h"
104
105
#include "usacdec_const.h"
106
#include "usacdec_lpc.h"
107
#include "usacdec_acelp.h"
108
#include "usacdec_rom.h"
109
#include "dct.h"
110
#include "FDK_tools_rom.h"
111
#include "mdct.h"
112
113
57.1k
#define SPEC_FAC(ptr, i, gl) ((ptr) + ((i) * (gl)))
114
115
FIXP_DBL *CLpd_FAC_GetMemory(CAacDecoderChannelInfo *pAacDecoderChannelInfo,
116
58.7k
                             UCHAR mod[NB_DIV], int *pState) {
117
58.7k
  FIXP_DBL *ptr;
118
58.7k
  int i;
119
58.7k
  int k = 0;
120
58.7k
  int max_windows = 8;
121
122
58.7k
  FDK_ASSERT(*pState >= 0 && *pState < max_windows);
123
124
  /* Look for free space to store FAC data. 2 FAC data blocks fit into each TCX
125
   * spectral data block. */
126
144k
  for (i = *pState; i < max_windows; i++) {
127
143k
    if (mod[i >> 1] == 0) {
128
57.1k
      break;
129
57.1k
    }
130
143k
  }
131
132
58.7k
  *pState = i + 1;
133
134
58.7k
  if (i == max_windows) {
135
1.60k
    ptr = pAacDecoderChannelInfo->data.usac.fac_data0;
136
57.1k
  } else {
137
57.1k
    FDK_ASSERT(mod[(i >> 1)] == 0);
138
57.1k
    ptr = SPEC_FAC(pAacDecoderChannelInfo->pSpectralCoefficient, i,
139
57.1k
                   pAacDecoderChannelInfo->granuleLength << k);
140
57.1k
  }
141
142
58.7k
  return ptr;
143
58.7k
}
144
145
int CLpd_FAC_Read(HANDLE_FDK_BITSTREAM hBs, FIXP_DBL *pFac, SCHAR *pFacScale,
146
91.6k
                  int length, int use_gain, int frame) {
147
91.6k
  FIXP_DBL fac_gain;
148
91.6k
  int fac_gain_e = 0;
149
150
91.6k
  if (use_gain) {
151
35.9k
    CLpd_DecodeGain(&fac_gain, &fac_gain_e, FDKreadBits(hBs, 7));
152
35.9k
  }
153
154
91.6k
  if (CLpc_DecodeAVQ(hBs, pFac, 1, 1, length) != 0) {
155
269
    return -1;
156
269
  }
157
158
91.3k
  {
159
91.3k
    int scale;
160
161
91.3k
    scale = getScalefactor(pFac, length);
162
91.3k
    scaleValues(pFac, length, scale);
163
91.3k
    pFacScale[frame] = DFRACT_BITS - 1 - scale;
164
91.3k
  }
165
166
91.3k
  if (use_gain) {
167
35.6k
    int i;
168
169
35.6k
    pFacScale[frame] += fac_gain_e;
170
171
2.59M
    for (i = 0; i < length; i++) {
172
2.55M
      pFac[i] = fMult(pFac[i], fac_gain);
173
2.55M
    }
174
35.6k
  }
175
91.3k
  return 0;
176
91.6k
}
177
178
/**
179
 * \brief Apply synthesis filter with zero input to x. The overall filter gain
180
 * is 1.0.
181
 * \param a LPC filter coefficients.
182
 * \param length length of the input/output data vector x.
183
 * \param x input/output vector, where the synthesis filter is applied in place.
184
 */
185
static void Syn_filt_zero(const FIXP_LPC a[], const INT a_exp, INT length,
186
77.4k
                          FIXP_DBL x[]) {
187
77.4k
  int i, j;
188
77.4k
  FIXP_DBL L_tmp;
189
190
15.4M
  for (i = 0; i < length; i++) {
191
15.3M
    L_tmp = (FIXP_DBL)0;
192
193
251M
    for (j = 0; j < fMin(i, M_LP_FILTER_ORDER); j++) {
194
235M
      L_tmp -= fMultDiv2(a[j], x[i - (j + 1)]) >> (LP_FILTER_SCALE - 1);
195
235M
    }
196
197
15.3M
    L_tmp = scaleValue(L_tmp, a_exp + LP_FILTER_SCALE);
198
15.3M
    x[i] = fAddSaturate(x[i], L_tmp);
199
15.3M
  }
200
77.4k
}
201
202
/* Table is also correct for coreCoderFrameLength = 768. Factor 3/4 is canceled
203
   out: gainFac = 0.5 * sqrt(fac_length/lFrame)
204
*/
205
static const FIXP_DBL gainFac[4] = {0x40000000, 0x2d413ccd, 0x20000000,
206
                                    0x16a09e66};
207
208
void CFac_ApplyGains(FIXP_DBL fac_data[LFAC], const INT fac_length,
209
                     const FIXP_DBL tcx_gain, const FIXP_DBL alfd_gains[],
210
50.2k
                     const INT mod) {
211
50.2k
  FIXP_DBL facFactor;
212
50.2k
  int i;
213
214
50.2k
  FDK_ASSERT((fac_length == 128) || (fac_length == 96));
215
216
  /* 2) Apply gain factor to FAC data */
217
50.2k
  facFactor = fMult(gainFac[mod], tcx_gain);
218
5.62M
  for (i = 0; i < fac_length; i++) {
219
5.57M
    fac_data[i] = fMult(fac_data[i], facFactor);
220
5.57M
  }
221
222
  /* 3) Apply spectrum deshaping using alfd_gains */
223
1.44M
  for (i = 0; i < fac_length / 4; i++) {
224
1.39M
    int k;
225
226
1.39M
    k = i >> (3 - mod);
227
1.39M
    fac_data[i] = fMult(fac_data[i], alfd_gains[k])
228
1.39M
                  << 1; /* alfd_gains is scaled by one bit. */
229
1.39M
  }
230
50.2k
}
231
232
static void CFac_CalcFacSignal(FIXP_DBL *pOut, FIXP_DBL *pFac,
233
                               const int fac_scale, const int fac_length,
234
                               const FIXP_LPC A[M_LP_FILTER_ORDER],
235
                               const INT A_exp, const int fAddZir,
236
77.4k
                               const int isFdFac) {
237
77.4k
  FIXP_LPC wA[M_LP_FILTER_ORDER];
238
77.4k
  FIXP_DBL tf_gain = (FIXP_DBL)0;
239
77.4k
  int wlength;
240
77.4k
  int scale = fac_scale;
241
242
  /* obtain tranform gain. */
243
77.4k
  imdct_gain(&tf_gain, &scale, isFdFac ? 0 : fac_length);
244
245
  /* 4) Compute inverse DCT-IV of FAC data. Output scale of DCT IV is 16 bits.
246
   */
247
77.4k
  dct_IV(pFac, fac_length, &scale);
248
  /* dct_IV scale = log2(fac_length). "- 7" is a factor of 2/128 */
249
77.4k
  if (tf_gain != (FIXP_DBL)0) { /* non-radix 2 transform gain */
250
31.7k
    int i;
251
252
3.08M
    for (i = 0; i < fac_length; i++) {
253
3.05M
      pFac[i] = fMult(tf_gain, pFac[i]);
254
3.05M
    }
255
31.7k
  }
256
77.4k
  scaleValuesSaturate(pOut, pFac, fac_length,
257
77.4k
                      scale); /* Avoid overflow issues and saturate. */
258
259
77.4k
  E_LPC_a_weight(wA, A, M_LP_FILTER_ORDER);
260
261
  /* We need the output of the IIR filter to be longer than "fac_length".
262
  For this reason we run it with zero input appended to the end of the input
263
  sequence, i.e. we generate its ZIR and extend the output signal.*/
264
77.4k
  FDKmemclear(pOut + fac_length, fac_length * sizeof(FIXP_DBL));
265
77.4k
  wlength = 2 * fac_length;
266
267
  /* 5) Apply weighted synthesis filter to FAC data, including optional Zir (5.
268
   * item 4). */
269
77.4k
  Syn_filt_zero(wA, A_exp, wlength, pOut);
270
77.4k
}
271
272
INT CLpd_FAC_Mdct2Acelp(H_MDCT hMdct, FIXP_DBL *output, FIXP_DBL *pFac,
273
                        const int fac_scale, FIXP_LPC *A, INT A_exp,
274
                        INT nrOutSamples, const INT fac_length,
275
51.6k
                        const INT isFdFac, UCHAR prevWindowShape) {
276
51.6k
  FIXP_DBL *pOvl;
277
51.6k
  FIXP_DBL *pOut0;
278
51.6k
  const FIXP_WTP *pWindow;
279
51.6k
  int i, fl, nrSamples = 0;
280
281
51.6k
  FDK_ASSERT(fac_length <= 1024 / (4 * 2));
282
283
51.6k
  fl = fac_length * 2;
284
285
51.6k
  pWindow = FDKgetWindowSlope(fl, prevWindowShape);
286
287
  /* Adapt window slope length in case of frame loss. */
288
51.6k
  if (hMdct->prev_fr != fl) {
289
13.9k
    int nl = 0;
290
13.9k
    imdct_adapt_parameters(hMdct, &fl, &nl, fac_length, pWindow, nrOutSamples);
291
13.9k
    FDK_ASSERT(nl == 0);
292
13.9k
  }
293
294
51.6k
  if (nrSamples < nrOutSamples) {
295
35.4k
    pOut0 = output;
296
35.4k
    nrSamples += hMdct->ov_offset;
297
    /* Purge buffered output. */
298
35.4k
    FDKmemcpy(pOut0, hMdct->overlap.time, hMdct->ov_offset * sizeof(pOut0[0]));
299
35.4k
    hMdct->ov_offset = 0;
300
35.4k
  }
301
302
51.6k
  pOvl = hMdct->overlap.freq + hMdct->ov_size - 1;
303
304
51.6k
  if (nrSamples >= nrOutSamples) {
305
16.2k
    pOut0 = hMdct->overlap.time + hMdct->ov_offset;
306
16.2k
    hMdct->ov_offset += hMdct->prev_nr + fl / 2;
307
35.4k
  } else {
308
35.4k
    pOut0 = output + nrSamples;
309
35.4k
    nrSamples += hMdct->prev_nr + fl / 2;
310
35.4k
  }
311
51.6k
  if (hMdct->prevPrevAliasSymmetry == 0) {
312
5.30M
    for (i = 0; i < hMdct->prev_nr; i++) {
313
5.25M
      FIXP_DBL x = -(*pOvl--);
314
5.25M
      *pOut0 = IMDCT_SCALE_DBL(x);
315
5.25M
      pOut0++;
316
5.25M
    }
317
51.6k
  } else {
318
0
    for (i = 0; i < hMdct->prev_nr; i++) {
319
0
      FIXP_DBL x = (*pOvl--);
320
0
      *pOut0 = IMDCT_SCALE_DBL(x);
321
0
      pOut0++;
322
0
    }
323
0
  }
324
51.6k
  hMdct->prev_nr = 0;
325
326
51.6k
  {
327
51.6k
    if (pFac != NULL) {
328
      /* Note: The FAC gain might have been applied directly after bit stream
329
       * parse in this case. */
330
33.7k
      CFac_CalcFacSignal(pOut0, pFac, fac_scale, fac_length, A, A_exp, 0,
331
33.7k
                         isFdFac);
332
33.7k
    } else {
333
      /* Clear buffer because of the overlap and ADD! */
334
17.9k
      FDKmemclear(pOut0, fac_length * sizeof(FIXP_DBL));
335
17.9k
    }
336
51.6k
  }
337
338
51.6k
  i = 0;
339
340
51.6k
  if (hMdct->prevPrevAliasSymmetry == 0) {
341
5.33M
    for (; i < fl / 2; i++) {
342
5.28M
      FIXP_DBL x0;
343
344
      /* Overlap Add */
345
5.28M
      x0 = -fMult(*pOvl--, pWindow[i].v.re);
346
347
5.28M
      *pOut0 = fAddSaturate(*pOut0, IMDCT_SCALE_DBL(x0));
348
5.28M
      pOut0++;
349
5.28M
    }
350
51.6k
  } else {
351
0
    for (; i < fl / 2; i++) {
352
0
      FIXP_DBL x0;
353
354
      /* Overlap Add */
355
0
      x0 = fMult(*pOvl--, pWindow[i].v.re);
356
357
0
      *pOut0 = fAddSaturate(*pOut0, IMDCT_SCALE_DBL(x0));
358
0
      pOut0++;
359
0
    }
360
0
  }
361
51.6k
  if (hMdct->pFacZir !=
362
51.6k
      0) { /* this should only happen for ACELP -> TCX20 -> ACELP transition */
363
12.6k
    FIXP_DBL *pOut = pOut0 - fl / 2; /* fl/2 == fac_length */
364
1.39M
    for (i = 0; i < fl / 2; i++) {
365
1.38M
      pOut[i] = fAddSaturate(pOut[i], IMDCT_SCALE_DBL(hMdct->pFacZir[i]));
366
1.38M
    }
367
12.6k
    hMdct->pFacZir = NULL;
368
12.6k
  }
369
370
51.6k
  hMdct->prev_fr = 0;
371
51.6k
  hMdct->prev_nr = 0;
372
51.6k
  hMdct->prev_tl = 0;
373
51.6k
  hMdct->prevPrevAliasSymmetry = hMdct->prevAliasSymmetry;
374
375
51.6k
  return nrSamples;
376
51.6k
}
377
378
INT CLpd_FAC_Acelp2Mdct(H_MDCT hMdct, FIXP_DBL *output, FIXP_DBL *_pSpec,
379
                        const SHORT spec_scale[], const int nSpec,
380
                        FIXP_DBL *pFac, const int fac_scale,
381
                        const INT fac_length, INT noOutSamples, const INT tl,
382
                        const FIXP_WTP *wrs, const INT fr, FIXP_LPC A[16],
383
                        INT A_exp, CAcelpStaticMem *acelp_mem,
384
                        const FIXP_DBL gain, const int last_frame_lost,
385
                        const int isFdFac, const UCHAR last_lpd_mode,
386
43.7k
                        const int k, int currAliasingSymmetry) {
387
43.7k
  FIXP_DBL *pCurr, *pOvl, *pSpec;
388
43.7k
  const FIXP_WTP *pWindow;
389
43.7k
  const FIXP_WTB *FacWindowZir_conceal;
390
43.7k
  UCHAR doFacZirConceal = 0;
391
43.7k
  int doDeemph = 1;
392
43.7k
  const FIXP_WTB *FacWindowZir, *FacWindowSynth;
393
43.7k
  FIXP_DBL *pOut0 = output, *pOut1;
394
43.7k
  int w, i, fl, nl, nr, f_len, nrSamples = 0, s = 0, scale, total_gain_e;
395
43.7k
  FIXP_DBL *pF, *pFAC_and_FAC_ZIR = NULL;
396
43.7k
  FIXP_DBL total_gain = gain;
397
398
43.7k
  FDK_ASSERT(fac_length <= 1024 / (4 * 2));
399
43.7k
  switch (fac_length) {
400
    /* coreCoderFrameLength = 1024 */
401
14.3k
    case 128:
402
14.3k
      pWindow = SineWindow256;
403
14.3k
      FacWindowZir = FacWindowZir128;
404
14.3k
      FacWindowSynth = FacWindowSynth128;
405
14.3k
      break;
406
4.45k
    case 64:
407
4.45k
      pWindow = SineWindow128;
408
4.45k
      FacWindowZir = FacWindowZir64;
409
4.45k
      FacWindowSynth = FacWindowSynth64;
410
4.45k
      break;
411
0
    case 32:
412
0
      pWindow = SineWindow64;
413
0
      FacWindowZir = FacWindowZir32;
414
0
      FacWindowSynth = FacWindowSynth32;
415
0
      break;
416
    /* coreCoderFrameLength = 768 */
417
16.4k
    case 96:
418
16.4k
      pWindow = SineWindow192;
419
16.4k
      FacWindowZir = FacWindowZir96;
420
16.4k
      FacWindowSynth = FacWindowSynth96;
421
16.4k
      break;
422
8.53k
    case 48:
423
8.53k
      pWindow = SineWindow96;
424
8.53k
      FacWindowZir = FacWindowZir48;
425
8.53k
      FacWindowSynth = FacWindowSynth48;
426
8.53k
      break;
427
0
    default:
428
0
      FDK_ASSERT(0);
429
0
      return 0;
430
43.7k
  }
431
432
43.7k
  FacWindowZir_conceal = FacWindowSynth;
433
  /* Derive NR and NL */
434
43.7k
  fl = fac_length * 2;
435
43.7k
  nl = (tl - fl) >> 1;
436
43.7k
  nr = (tl - fr) >> 1;
437
438
43.7k
  if (noOutSamples > nrSamples) {
439
    /* Purge buffered output. */
440
27.7k
    FDKmemcpy(pOut0, hMdct->overlap.time, hMdct->ov_offset * sizeof(pOut0[0]));
441
27.7k
    nrSamples = hMdct->ov_offset;
442
27.7k
    hMdct->ov_offset = 0;
443
27.7k
  }
444
445
43.7k
  if (nrSamples >= noOutSamples) {
446
15.9k
    pOut1 = hMdct->overlap.time + hMdct->ov_offset;
447
15.9k
    if (hMdct->ov_offset < fac_length) {
448
11.9k
      pOut0 = output + nrSamples;
449
11.9k
    } else {
450
3.97k
      pOut0 = pOut1;
451
3.97k
    }
452
15.9k
    hMdct->ov_offset += fac_length + nl;
453
27.7k
  } else {
454
27.7k
    pOut1 = output + nrSamples;
455
27.7k
    pOut0 = output + nrSamples;
456
27.7k
  }
457
458
43.7k
  {
459
43.7k
    pFAC_and_FAC_ZIR = CLpd_ACELP_GetFreeExcMem(acelp_mem, 2 * fac_length);
460
43.7k
    {
461
43.7k
      const FIXP_DBL *pTmp1, *pTmp2;
462
463
43.7k
      doFacZirConceal |= ((last_frame_lost != 0) && (k == 0));
464
43.7k
      doDeemph &= (last_lpd_mode != 4);
465
43.7k
      if (doFacZirConceal) {
466
        /* ACELP contribution in concealment case:
467
           Use ZIR with a modified ZIR window to preserve some more energy.
468
           Dont use FAC, which contains wrong information for concealed frame
469
           Dont use last ACELP samples, but double ZIR, instead (afterwards) */
470
11
        FDKmemclear(pFAC_and_FAC_ZIR, 2 * fac_length * sizeof(FIXP_DBL));
471
11
        FacWindowSynth = (FIXP_WTB *)pFAC_and_FAC_ZIR;
472
11
        FacWindowZir = FacWindowZir_conceal;
473
43.7k
      } else {
474
43.7k
        CFac_CalcFacSignal(pFAC_and_FAC_ZIR, pFac, fac_scale + s, fac_length, A,
475
43.7k
                           A_exp, 1, isFdFac);
476
43.7k
      }
477
      /* 6) Get windowed past ACELP samples and ACELP ZIR signal */
478
479
      /*
480
       * Get ACELP ZIR (pFac[]) and ACELP past samples (pOut0[]) and add them
481
       * to the FAC synth signal contribution on pOut1[].
482
       */
483
43.7k
      {
484
43.7k
        {
485
43.7k
          CLpd_Acelp_Zir(A, A_exp, acelp_mem, fac_length, pFac, doDeemph);
486
487
43.7k
          pTmp1 = pOut0;
488
43.7k
          pTmp2 = pFac;
489
43.7k
        }
490
491
4.14M
        for (i = 0, w = 0; i < fac_length; i++) {
492
4.10M
          FIXP_DBL x;
493
          /* Div2 is compensated by table scaling */
494
4.10M
          x = fMultDiv2(pTmp2[i], FacWindowZir[w]);
495
4.10M
          x += fMultDiv2(pTmp1[-i - 1], FacWindowSynth[w]);
496
4.10M
          pOut1[i] = fAddSaturate(x, pFAC_and_FAC_ZIR[i]);
497
4.10M
          w++;
498
4.10M
        }
499
43.7k
      }
500
501
43.7k
      if (doFacZirConceal) {
502
        /* ZIR is the only ACELP contribution, so double it */
503
11
        scaleValues(pOut1, fac_length, 1);
504
11
      }
505
43.7k
    }
506
43.7k
  }
507
508
43.7k
  if (nrSamples < noOutSamples) {
509
27.7k
    nrSamples += fac_length + nl;
510
27.7k
  }
511
512
  /* Obtain transform gain */
513
43.7k
  total_gain = gain;
514
43.7k
  total_gain_e = 0;
515
43.7k
  imdct_gain(&total_gain, &total_gain_e, tl);
516
517
  /* IMDCT overlap add */
518
43.7k
  scale = total_gain_e;
519
43.7k
  pSpec = _pSpec;
520
521
  /* Note:when comming from an LPD frame (TCX/ACELP) the previous alisaing
522
   * symmetry must always be 0 */
523
43.7k
  if (currAliasingSymmetry == 0) {
524
43.7k
    dct_IV(pSpec, tl, &scale);
525
43.7k
  } else {
526
0
    FIXP_DBL _tmp[1024 + ALIGNMENT_DEFAULT / sizeof(FIXP_DBL)];
527
0
    FIXP_DBL *tmp = (FIXP_DBL *)ALIGN_PTR(_tmp);
528
0
    C_ALLOC_ALIGNED_REGISTER(tmp, sizeof(_tmp));
529
0
    dst_III(pSpec, tmp, tl, &scale);
530
0
    C_ALLOC_ALIGNED_UNREGISTER(tmp);
531
0
  }
532
533
  /* Optional scaling of time domain - no yet windowed - of current spectrum */
534
43.7k
  if (total_gain != (FIXP_DBL)0) {
535
9.16M
    for (i = 0; i < tl; i++) {
536
9.12M
      pSpec[i] = fMult(pSpec[i], total_gain);
537
9.12M
    }
538
35.6k
  }
539
43.7k
  int loc_scale = fixmin_I(spec_scale[0] + scale, (INT)DFRACT_BITS - 1);
540
43.7k
  scaleValuesSaturate(pSpec, tl, loc_scale);
541
542
43.7k
  pOut1 += fl / 2 - 1;
543
43.7k
  pCurr = pSpec + tl - fl / 2;
544
545
4.14M
  for (i = 0; i < fl / 2; i++) {
546
4.10M
    FIXP_DBL x1;
547
548
    /* FAC signal is already on pOut1, because of that the += operator. */
549
4.10M
    x1 = fMult(*pCurr++, pWindow[i].v.re);
550
4.10M
    FDK_ASSERT((pOut1 >= hMdct->overlap.time &&
551
4.10M
                pOut1 < hMdct->overlap.time + hMdct->ov_size) ||
552
4.10M
               (pOut1 >= output && pOut1 < output + 1024));
553
4.10M
    *pOut1 = fAddSaturate(*pOut1, IMDCT_SCALE_DBL(-x1));
554
4.10M
    pOut1--;
555
4.10M
  }
556
557
  /* NL output samples TL/2+FL/2..TL. - current[FL/2..0] */
558
43.7k
  pOut1 += (fl / 2) + 1;
559
560
43.7k
  pFAC_and_FAC_ZIR += fac_length; /* set pointer to beginning of FAC ZIR */
561
562
43.7k
  if (nl == 0) {
563
    /* save pointer to write FAC ZIR data later */
564
30.1k
    hMdct->pFacZir = pFAC_and_FAC_ZIR;
565
30.1k
  } else {
566
13.5k
    FDK_ASSERT(nl >= fac_length);
567
    /* FAC ZIR will be added now ... */
568
13.5k
    hMdct->pFacZir = NULL;
569
13.5k
  }
570
571
43.7k
  pF = pFAC_and_FAC_ZIR;
572
43.7k
  f_len = fac_length;
573
574
43.7k
  pCurr = pSpec + tl - fl / 2 - 1;
575
2.62M
  for (i = 0; i < nl; i++) {
576
2.57M
    FIXP_DBL x = -(*pCurr--);
577
    /* 5) (item 4) Synthesis filter Zir component, FAC ZIR (another one). */
578
2.57M
    if (i < f_len) {
579
1.50M
      x = fAddSaturate(x, *pF++);
580
1.50M
    }
581
582
2.57M
    FDK_ASSERT((pOut1 >= hMdct->overlap.time &&
583
2.57M
                pOut1 < hMdct->overlap.time + hMdct->ov_size) ||
584
2.57M
               (pOut1 >= output && pOut1 < output + 1024));
585
2.57M
    *pOut1 = IMDCT_SCALE_DBL(x);
586
2.57M
    pOut1++;
587
2.57M
  }
588
589
43.7k
  hMdct->prev_nr = nr;
590
43.7k
  hMdct->prev_fr = fr;
591
43.7k
  hMdct->prev_wrs = wrs;
592
43.7k
  hMdct->prev_tl = tl;
593
43.7k
  hMdct->prevPrevAliasSymmetry = hMdct->prevAliasSymmetry;
594
43.7k
  hMdct->prevAliasSymmetry = currAliasingSymmetry;
595
43.7k
  fl = fr;
596
43.7k
  nl = nr;
597
598
43.7k
  pOvl = pSpec + tl / 2 - 1;
599
43.7k
  pOut0 = pOut1;
600
601
134k
  for (w = 1; w < nSpec; w++) /* for ACELP -> FD short */
602
90.8k
  {
603
90.8k
    const FIXP_WTP *pWindow_prev;
604
605
    /* Setup window pointers */
606
90.8k
    pWindow_prev = hMdct->prev_wrs;
607
608
    /* Current spectrum */
609
90.8k
    pSpec = _pSpec + w * tl;
610
611
90.8k
    scale = total_gain_e;
612
613
    /* For the second, third, etc. short frames the alisaing symmetry is equal,
614
     * either (0,0) or (1,1) */
615
90.8k
    if (currAliasingSymmetry == 0) {
616
      /* DCT IV of current spectrum */
617
90.8k
      dct_IV(pSpec, tl, &scale);
618
90.8k
    } else {
619
0
      dst_IV(pSpec, tl, &scale);
620
0
    }
621
622
    /* Optional scaling of time domain - no yet windowed - of current spectrum
623
     */
624
    /* and de-scale current spectrum signal (time domain, no yet windowed) */
625
90.8k
    if (total_gain != (FIXP_DBL)0) {
626
5.79M
      for (i = 0; i < tl; i++) {
627
5.73M
        pSpec[i] = fMult(pSpec[i], total_gain);
628
5.73M
      }
629
59.7k
    }
630
90.8k
    loc_scale = fixmin_I(spec_scale[w] + scale, (INT)DFRACT_BITS - 1);
631
90.8k
    scaleValuesSaturate(pSpec, tl, loc_scale);
632
633
90.8k
    if (noOutSamples <= nrSamples) {
634
      /* Divert output first half to overlap buffer if we already got enough
635
       * output samples. */
636
38.9k
      pOut0 = hMdct->overlap.time + hMdct->ov_offset;
637
38.9k
      hMdct->ov_offset += hMdct->prev_nr + fl / 2;
638
51.9k
    } else {
639
      /* Account output samples */
640
51.9k
      nrSamples += hMdct->prev_nr + fl / 2;
641
51.9k
    }
642
643
    /* NR output samples 0 .. NR. -overlap[TL/2..TL/2-NR] */
644
90.8k
    for (i = 0; i < hMdct->prev_nr; i++) {
645
0
      FIXP_DBL x = -(*pOvl--);
646
0
      *pOut0 = IMDCT_SCALE_DBL(x);
647
0
      pOut0++;
648
0
    }
649
650
90.8k
    if (noOutSamples <= nrSamples) {
651
      /* Divert output second half to overlap buffer if we already got enough
652
       * output samples. */
653
51.9k
      pOut1 = hMdct->overlap.time + hMdct->ov_offset + fl / 2 - 1;
654
51.9k
      hMdct->ov_offset += fl / 2 + nl;
655
51.9k
    } else {
656
38.9k
      pOut1 = pOut0 + (fl - 1);
657
38.9k
      nrSamples += fl / 2 + nl;
658
38.9k
    }
659
660
    /* output samples before window crossing point NR .. TL/2.
661
     * -overlap[TL/2-NR..TL/2-NR-FL/2] + current[NR..TL/2] */
662
    /* output samples after window crossing point TL/2 .. TL/2+FL/2.
663
     * -overlap[0..FL/2] - current[TL/2..FL/2] */
664
90.8k
    pCurr = pSpec + tl - fl / 2;
665
90.8k
    if (currAliasingSymmetry == 0) {
666
4.95M
      for (i = 0; i < fl / 2; i++) {
667
4.86M
        FIXP_DBL x0, x1;
668
669
4.86M
        cplxMultDiv2(&x1, &x0, *pCurr++, -*pOvl--, pWindow_prev[i]);
670
4.86M
        *pOut0 = IMDCT_SCALE_DBL_LSH1(x0);
671
4.86M
        *pOut1 = IMDCT_SCALE_DBL_LSH1(-x1);
672
4.86M
        pOut0++;
673
4.86M
        pOut1--;
674
4.86M
      }
675
90.8k
    } else {
676
0
      if (hMdct->prevPrevAliasSymmetry == 0) {
677
        /* Jump DST II -> DST IV for the second window */
678
0
        for (i = 0; i < fl / 2; i++) {
679
0
          FIXP_DBL x0, x1;
680
681
0
          cplxMultDiv2(&x1, &x0, *pCurr++, -*pOvl--, pWindow_prev[i]);
682
0
          *pOut0 = IMDCT_SCALE_DBL_LSH1(x0);
683
0
          *pOut1 = IMDCT_SCALE_DBL_LSH1(x1);
684
0
          pOut0++;
685
0
          pOut1--;
686
0
        }
687
0
      } else {
688
        /* Jump DST IV -> DST IV from the second window on */
689
0
        for (i = 0; i < fl / 2; i++) {
690
0
          FIXP_DBL x0, x1;
691
692
0
          cplxMultDiv2(&x1, &x0, *pCurr++, *pOvl--, pWindow_prev[i]);
693
0
          *pOut0 = IMDCT_SCALE_DBL_LSH1(x0);
694
0
          *pOut1 = IMDCT_SCALE_DBL_LSH1(x1);
695
0
          pOut0++;
696
0
          pOut1--;
697
0
        }
698
0
      }
699
0
    }
700
701
90.8k
    if (hMdct->pFacZir != 0) {
702
      /* add FAC ZIR of previous ACELP -> mdct transition */
703
12.9k
      FIXP_DBL *pOut = pOut0 - fl / 2;
704
12.9k
      FDK_ASSERT(fl / 2 <= 128);
705
707k
      for (i = 0; i < fl / 2; i++) {
706
694k
        pOut[i] = fAddSaturate(pOut[i], IMDCT_SCALE_DBL(hMdct->pFacZir[i]));
707
694k
      }
708
12.9k
      hMdct->pFacZir = NULL;
709
12.9k
    }
710
90.8k
    pOut0 += (fl / 2);
711
712
    /* NL output samples TL/2+FL/2..TL. - current[FL/2..0] */
713
90.8k
    pOut1 += (fl / 2) + 1;
714
90.8k
    pCurr = pSpec + tl - fl / 2 - 1;
715
90.8k
    for (i = 0; i < nl; i++) {
716
0
      FIXP_DBL x = -(*pCurr--);
717
0
      *pOut1 = IMDCT_SCALE_DBL(x);
718
0
      pOut1++;
719
0
    }
720
721
    /* Set overlap source pointer for next window pOvl = pSpec + tl/2 - 1; */
722
90.8k
    pOvl = pSpec + tl / 2 - 1;
723
724
    /* Previous window values. */
725
90.8k
    hMdct->prev_nr = nr;
726
90.8k
    hMdct->prev_fr = fr;
727
90.8k
    hMdct->prev_tl = tl;
728
90.8k
    hMdct->prev_wrs = pWindow_prev;
729
90.8k
    hMdct->prevPrevAliasSymmetry = hMdct->prevAliasSymmetry;
730
90.8k
    hMdct->prevAliasSymmetry = currAliasingSymmetry;
731
90.8k
  }
732
733
  /* Save overlap */
734
735
43.7k
  pOvl = hMdct->overlap.freq + hMdct->ov_size - tl / 2;
736
43.7k
  FDK_ASSERT(pOvl >= hMdct->overlap.time + hMdct->ov_offset);
737
43.7k
  FDK_ASSERT(tl / 2 <= hMdct->ov_size);
738
6.72M
  for (i = 0; i < tl / 2; i++) {
739
6.68M
    pOvl[i] = _pSpec[i + (w - 1) * tl];
740
6.68M
  }
741
742
43.7k
  return nrSamples;
743
43.7k
}