Coverage Report

Created: 2026-05-16 07:49

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/fdk-aac/libSBRdec/src/sbr_dec.cpp
Line
Count
Source
1
/* -----------------------------------------------------------------------------
2
Software License for The Fraunhofer FDK AAC Codec Library for Android
3
4
© Copyright  1995 - 2020 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
/**************************** SBR decoder library ******************************
96
97
   Author(s):
98
99
   Description:
100
101
*******************************************************************************/
102
103
/*!
104
  \file
105
  \brief  Sbr decoder
106
  This module provides the actual decoder implementation. The SBR data (side
107
  information) is already decoded. Only three functions are provided:
108
109
  \li 1.) createSbrDec(): One time initialization
110
  \li 2.) resetSbrDec(): Called by sbr_Apply() when the information contained in
111
  an SBR_HEADER_ELEMENT requires a reset and recalculation of important SBR
112
  structures. \li 3.) sbr_dec(): The actual decoder. Calls the different tools
113
  such as filterbanks, lppTransposer(), and calculateSbrEnvelope() [the envelope
114
  adjuster].
115
116
  \sa sbr_dec(), \ref documentationOverview
117
*/
118
119
#include "sbr_dec.h"
120
121
#include "sbr_ram.h"
122
#include "env_extr.h"
123
#include "env_calc.h"
124
#include "scale.h"
125
#include "FDK_matrixCalloc.h"
126
#include "hbe.h"
127
128
#include "genericStds.h"
129
130
#include "sbrdec_drc.h"
131
132
static void copyHarmonicSpectrum(int *xOverQmf, FIXP_DBL **qmfReal,
133
                                 FIXP_DBL **qmfImag, int noCols, int overlap,
134
7.55k
                                 KEEP_STATES_SYNCED_MODE keepStatesSynced) {
135
7.55k
  int patchBands;
136
7.55k
  int patch, band, col, target, sourceBands, i;
137
7.55k
  int numPatches = 0;
138
7.55k
  int slotOffset = 0;
139
140
7.55k
  FIXP_DBL **ppqmfReal = qmfReal + overlap;
141
7.55k
  FIXP_DBL **ppqmfImag = qmfImag + overlap;
142
143
7.55k
  if (keepStatesSynced == KEEP_STATES_SYNCED_NORMAL) {
144
1.22k
    slotOffset = noCols - overlap - LPC_ORDER;
145
1.22k
  }
146
147
7.55k
  if (keepStatesSynced == KEEP_STATES_SYNCED_OUTDIFF) {
148
581
    ppqmfReal = qmfReal;
149
581
    ppqmfImag = qmfImag;
150
581
  }
151
152
45.3k
  for (i = 1; i < MAX_NUM_PATCHES; i++) {
153
37.7k
    if (xOverQmf[i] != 0) {
154
16.3k
      numPatches++;
155
16.3k
    }
156
37.7k
  }
157
158
8.96k
  for (patch = (MAX_STRETCH_HBE - 1); patch < numPatches; patch++) {
159
1.40k
    patchBands = xOverQmf[patch + 1] - xOverQmf[patch];
160
1.40k
    target = xOverQmf[patch];
161
1.40k
    sourceBands = xOverQmf[MAX_STRETCH_HBE - 1] - xOverQmf[MAX_STRETCH_HBE - 2];
162
163
3.42k
    while (patchBands > 0) {
164
2.02k
      int numBands = sourceBands;
165
2.02k
      int startBand = xOverQmf[MAX_STRETCH_HBE - 1] - 1;
166
2.02k
      if (target + numBands >= xOverQmf[patch + 1]) {
167
1.40k
        numBands = xOverQmf[patch + 1] - target;
168
1.40k
      }
169
2.02k
      if ((((target + numBands - 1) % 2) +
170
2.02k
           ((xOverQmf[MAX_STRETCH_HBE - 1] - 1) % 2)) %
171
2.02k
          2) {
172
637
        if (numBands == sourceBands) {
173
0
          numBands--;
174
637
        } else {
175
637
          startBand--;
176
637
        }
177
637
      }
178
2.02k
      if (keepStatesSynced == KEEP_STATES_SYNCED_OUTDIFF) {
179
525
        for (col = slotOffset; col < overlap + LPC_ORDER; col++) {
180
490
          i = 0;
181
3.19k
          for (band = numBands; band > 0; band--) {
182
2.70k
            if ((target + band - 1 < 64) &&
183
2.70k
                (target + band - 1 < xOverQmf[patch + 1])) {
184
2.70k
              ppqmfReal[col][target + band - 1] = ppqmfReal[col][startBand - i];
185
2.70k
              ppqmfImag[col][target + band - 1] = ppqmfImag[col][startBand - i];
186
2.70k
              i++;
187
2.70k
            }
188
2.70k
          }
189
490
        }
190
1.98k
      } else {
191
86.4k
        for (col = slotOffset; col < noCols; col++) {
192
84.4k
          i = 0;
193
551k
          for (band = numBands; band > 0; band--) {
194
467k
            if ((target + band - 1 < 64) &&
195
467k
                (target + band - 1 < xOverQmf[patch + 1])) {
196
467k
              ppqmfReal[col][target + band - 1] = ppqmfReal[col][startBand - i];
197
467k
              ppqmfImag[col][target + band - 1] = ppqmfImag[col][startBand - i];
198
467k
              i++;
199
467k
            }
200
467k
          }
201
84.4k
        }
202
1.98k
      }
203
2.02k
      target += numBands;
204
2.02k
      patchBands -= numBands;
205
2.02k
    }
206
1.40k
  }
207
7.55k
}
208
209
/*!
210
  \brief      SBR decoder core function for one channel
211
212
  \image html  BufferMgmtDetailed-1632.png
213
214
  Besides the filter states of the QMF filter bank and the LPC-states of
215
  the LPP-Transposer, processing is mainly based on four buffers:
216
  #timeIn, #timeOut, #WorkBuffer2 and #OverlapBuffer. The #WorkBuffer2
217
  is reused for all channels and might be used by the core decoder, a
218
  static overlap buffer is required for each channel. Due to in-place
219
  processing, #timeIn and #timeOut point to identical locations.
220
221
  The spectral data is organized in so-called slots. Each slot
222
  contains 64 bands of complex data. The number of slots per frame
223
  depends on the frame size. For mp3PRO, there are 18 slots per frame
224
  and 6 slots per #OverlapBuffer. It is not necessary to have the slots
225
  in located consecutive address ranges.
226
227
  To optimize memory usage and to minimize the number of memory
228
  accesses, the memory management is organized as follows (slot numbers
229
  based on mp3PRO):
230
231
  1.) Input time domain signal is located in #timeIn. The last slots
232
  (0..5) of the spectral data of the previous frame are located in the
233
  #OverlapBuffer. In addition, #frameData of the current frame resides
234
  in the upper part of #timeIn.
235
236
  2.) During the cplxAnalysisQmfFiltering(), 32 samples from #timeIn are
237
  transformed into a slot of up to 32 complex spectral low band values at a
238
  time. The first spectral slot -- nr. 6 -- is written at slot number
239
  zero of #WorkBuffer2. #WorkBuffer2 will be completely filled with
240
  spectral data.
241
242
  3.) LPP-Transposition in lppTransposer() is processed on 24 slots. During the
243
  transposition, the high band part of the spectral data is replicated
244
  based on the low band data.
245
246
  Envelope Adjustment is processed on the high band part of the spectral
247
  data only by calculateSbrEnvelope().
248
249
  4.) The cplxSynthesisQmfFiltering() creates 64 time domain samples out
250
  of a slot of 64 complex spectral values at a time. The first 6 slots
251
  in #timeOut are filled from the results of spectral slots 0..5 in the
252
  #OverlapBuffer. The consecutive slots in timeOut are now filled with
253
  the results of spectral slots 6..17.
254
255
  5.) The preprocessed slots 18..23 have to be stored in the
256
  #OverlapBuffer.
257
258
*/
259
260
void sbr_dec(
261
    HANDLE_SBR_DEC hSbrDec,             /*!< handle to Decoder channel */
262
    LONG *timeIn,                       /*!< pointer to input time signal */
263
    LONG *timeOut,                      /*!< pointer to output time signal */
264
    HANDLE_SBR_DEC hSbrDecRight,        /*!< handle to Decoder channel right */
265
    LONG *timeOutRight,                 /*!< pointer to output time signal */
266
    const int strideOut,                /*!< Time data traversal strideOut */
267
    HANDLE_SBR_HEADER_DATA hHeaderData, /*!< Static control data */
268
    HANDLE_SBR_FRAME_DATA hFrameData,   /*!< Control data of current frame */
269
    HANDLE_SBR_PREV_FRAME_DATA
270
        hPrevFrameData,        /*!< Some control data of last frame */
271
    const int applyProcessing, /*!< Flag for SBR operation */
272
    HANDLE_PS_DEC h_ps_d, const UINT flags, const int codecFrameSize,
273
2.58M
    const INT sbrInDataHeadroom) {
274
2.58M
  int i, slot, reserve;
275
2.58M
  int saveLbScale;
276
2.58M
  int lastSlotOffs;
277
2.58M
  FIXP_DBL maxVal;
278
279
  /* temporary pointer / variable for QMF;
280
     required as we want to use temporary buffer
281
     creating one frame delay for HBE in LP mode */
282
2.58M
  LONG *pTimeInQmf = timeIn;
283
284
  /* Number of QMF timeslots in the overlap buffer: */
285
2.58M
  int ov_len = hSbrDec->LppTrans.pSettings->overlap;
286
287
  /* Number of QMF slots per frame */
288
2.58M
  int noCols = hHeaderData->numberTimeSlots * hHeaderData->timeStep;
289
290
  /* create pointer array for data to use for HBE and legacy sbr */
291
2.58M
  FIXP_DBL *pLowBandReal[(3 * 4) + 2 * ((1024) / (32) * (4) / 2)];
292
2.58M
  FIXP_DBL *pLowBandImag[(3 * 4) + 2 * ((1024) / (32) * (4) / 2)];
293
294
  /* set pReal to where QMF analysis writes in case of legacy SBR */
295
2.58M
  FIXP_DBL **pReal = pLowBandReal + ov_len;
296
2.58M
  FIXP_DBL **pImag = pLowBandImag + ov_len;
297
298
  /* map QMF buffer to pointer array (Overlap + Frame)*/
299
85.7M
  for (i = 0; i < noCols + ov_len; i++) {
300
83.1M
    pLowBandReal[i] = hSbrDec->qmfDomainInCh->hQmfSlotsReal[i];
301
83.1M
    pLowBandImag[i] = hSbrDec->qmfDomainInCh->hQmfSlotsImag[i];
302
83.1M
  }
303
304
2.58M
  if ((flags & SBRDEC_USAC_HARMONICSBR)) {
305
    /* in case of harmonic SBR and no HBE_LP map additional buffer for
306
       one more frame to pointer arry */
307
15.2M
    for (i = 0; i < noCols; i++) {
308
14.8M
      pLowBandReal[i + noCols + ov_len] = hSbrDec->hQmfHBESlotsReal[i];
309
14.8M
      pLowBandImag[i + noCols + ov_len] = hSbrDec->hQmfHBESlotsImag[i];
310
14.8M
    }
311
312
    /* shift scale values according to buffer */
313
410k
    hSbrDec->scale_ov = hSbrDec->scale_lb;
314
410k
    hSbrDec->scale_lb = hSbrDec->scale_hbe;
315
316
    /* set pReal to where QMF analysis writes in case of HBE */
317
410k
    pReal += noCols;
318
410k
    pImag += noCols;
319
410k
    if (flags & SBRDEC_SKIP_QMF_ANA) {
320
      /* stereoCfgIndex3 with HBE */
321
9.23k
      FDK_QmfDomain_QmfData2HBE(hSbrDec->qmfDomainInCh,
322
9.23k
                                hSbrDec->hQmfHBESlotsReal,
323
9.23k
                                hSbrDec->hQmfHBESlotsImag);
324
401k
    } else {
325
      /* We have to move old hbe frame data to lb area of buffer */
326
14.9M
      for (i = 0; i < noCols; i++) {
327
14.5M
        FDKmemcpy(pLowBandReal[ov_len + i], hSbrDec->hQmfHBESlotsReal[i],
328
14.5M
                  hHeaderData->numberOfAnalysisBands * sizeof(FIXP_DBL));
329
14.5M
        FDKmemcpy(pLowBandImag[ov_len + i], hSbrDec->hQmfHBESlotsImag[i],
330
14.5M
                  hHeaderData->numberOfAnalysisBands * sizeof(FIXP_DBL));
331
14.5M
      }
332
401k
    }
333
410k
  }
334
335
  /*
336
    low band codec signal subband filtering
337
   */
338
339
2.58M
  if (flags & SBRDEC_SKIP_QMF_ANA) {
340
68.4k
    if (!(flags & SBRDEC_USAC_HARMONICSBR)) /* stereoCfgIndex3 w/o HBE */
341
59.1k
      FDK_QmfDomain_WorkBuffer2ProcChannel(hSbrDec->qmfDomainInCh);
342
2.51M
  } else {
343
2.51M
    C_AALLOC_SCRATCH_START(qmfTemp, FIXP_DBL, 2 * (64));
344
2.51M
    qmfAnalysisFiltering(&hSbrDec->qmfDomainInCh->fb, pReal, pImag,
345
2.51M
                         &hSbrDec->qmfDomainInCh->scaling, pTimeInQmf,
346
2.51M
                         0 + sbrInDataHeadroom, 1, qmfTemp);
347
348
2.51M
    C_AALLOC_SCRATCH_END(qmfTemp, FIXP_DBL, 2 * (64));
349
2.51M
  }
350
351
  /*
352
    Clear upper half of spectrum
353
  */
354
2.58M
  if (!((flags & SBRDEC_USAC_HARMONICSBR) &&
355
2.29M
        (hFrameData->sbrPatchingMode == 0))) {
356
2.29M
    int nAnalysisBands = hHeaderData->numberOfAnalysisBands;
357
358
2.29M
    if (!(flags & SBRDEC_LOW_POWER)) {
359
40.4M
      for (slot = ov_len; slot < noCols + ov_len; slot++) {
360
39.2M
        FDKmemclear(&pLowBandReal[slot][nAnalysisBands],
361
39.2M
                    ((64) - nAnalysisBands) * sizeof(FIXP_DBL));
362
39.2M
        FDKmemclear(&pLowBandImag[slot][nAnalysisBands],
363
39.2M
                    ((64) - nAnalysisBands) * sizeof(FIXP_DBL));
364
39.2M
      }
365
1.21M
    } else {
366
23.2M
      for (slot = ov_len; slot < noCols + ov_len; slot++) {
367
22.1M
        FDKmemclear(&pLowBandReal[slot][nAnalysisBands],
368
22.1M
                    ((64) - nAnalysisBands) * sizeof(FIXP_DBL));
369
22.1M
      }
370
1.08M
    }
371
2.29M
  }
372
373
  /*
374
    Shift spectral data left to gain accuracy in transposer and adjustor
375
  */
376
  /* Range was increased from lsb to no_channels because in some cases (e.g.
377
     USAC conf eSbr_4_Pvc.mp4 and some HBE cases) it could be observed that the
378
     signal between lsb and no_channels is used for the patching process.
379
  */
380
2.58M
  maxVal = maxSubbandSample(pReal, (flags & SBRDEC_LOW_POWER) ? NULL : pImag, 0,
381
2.58M
                            hSbrDec->qmfDomainInCh->fb.no_channels, 0, noCols);
382
383
2.58M
  reserve = fixMax(0, CntLeadingZeros(maxVal) - 1);
384
2.58M
  reserve = fixMin(reserve,
385
2.58M
                   DFRACT_BITS - 1 - hSbrDec->qmfDomainInCh->scaling.lb_scale);
386
387
  /* If all data is zero, lb_scale could become too large */
388
2.58M
  rescaleSubbandSamples(pReal, (flags & SBRDEC_LOW_POWER) ? NULL : pImag, 0,
389
2.58M
                        hSbrDec->qmfDomainInCh->fb.no_channels, 0, noCols,
390
2.58M
                        reserve);
391
392
2.58M
  hSbrDec->qmfDomainInCh->scaling.lb_scale += reserve;
393
394
2.58M
  if ((flags & SBRDEC_USAC_HARMONICSBR)) {
395
    /* actually this is our hbe_scale */
396
410k
    hSbrDec->scale_hbe = hSbrDec->qmfDomainInCh->scaling.lb_scale;
397
    /* the real lb_scale is stored in scale_lb from sbr */
398
410k
    hSbrDec->qmfDomainInCh->scaling.lb_scale = hSbrDec->scale_lb;
399
410k
  }
400
  /*
401
    save low band scale, wavecoding or parametric stereo may modify it
402
  */
403
2.58M
  saveLbScale = hSbrDec->qmfDomainInCh->scaling.lb_scale;
404
405
2.58M
  if (applyProcessing) {
406
592k
    UCHAR *borders = hFrameData->frameInfo.borders;
407
592k
    lastSlotOffs = borders[hFrameData->frameInfo.nEnvelopes] -
408
592k
                   hHeaderData->numberTimeSlots;
409
410
592k
    FIXP_DBL degreeAlias[(64)];
411
592k
    PVC_DYNAMIC_DATA pvcDynamicData;
412
592k
    pvcInitFrame(
413
592k
        &hSbrDec->PvcStaticData, &pvcDynamicData,
414
592k
        (hHeaderData->frameErrorFlag ? 0 : hHeaderData->bs_info.pvc_mode),
415
592k
        hFrameData->ns, hHeaderData->timeStep,
416
592k
        hHeaderData->freqBandData.lowSubband,
417
592k
        hFrameData->frameInfo.pvcBorders[0], hFrameData->pvcID);
418
419
592k
    if (!hHeaderData->frameErrorFlag && (hHeaderData->bs_info.pvc_mode > 0)) {
420
2.28k
      pvcDecodeFrame(&hSbrDec->PvcStaticData, &pvcDynamicData, pLowBandReal,
421
2.28k
                     pLowBandImag, ov_len,
422
2.28k
                     SCALE2EXP(hSbrDec->qmfDomainInCh->scaling.ov_lb_scale),
423
2.28k
                     SCALE2EXP(hSbrDec->qmfDomainInCh->scaling.lb_scale));
424
2.28k
    }
425
592k
    pvcEndFrame(&hSbrDec->PvcStaticData, &pvcDynamicData);
426
427
    /* The transposer will override most values in degreeAlias[].
428
       The array needs to be cleared at least from lowSubband to highSubband
429
       before. */
430
592k
    if (flags & SBRDEC_LOW_POWER)
431
412k
      FDKmemclear(&degreeAlias[hHeaderData->freqBandData.lowSubband],
432
412k
                  (hHeaderData->freqBandData.highSubband -
433
412k
                   hHeaderData->freqBandData.lowSubband) *
434
412k
                      sizeof(FIXP_DBL));
435
436
    /*
437
      Inverse filtering of lowband and transposition into the SBR-frequency
438
      range
439
    */
440
441
592k
    {
442
592k
      KEEP_STATES_SYNCED_MODE keepStatesSyncedMode =
443
592k
          ((flags & SBRDEC_USAC_HARMONICSBR) &&
444
107k
           (hFrameData->sbrPatchingMode != 0))
445
592k
              ? KEEP_STATES_SYNCED_NORMAL
446
592k
              : KEEP_STATES_SYNCED_OFF;
447
448
592k
      if (flags & SBRDEC_USAC_HARMONICSBR) {
449
107k
        if (flags & SBRDEC_QUAD_RATE) {
450
6.97k
          pReal -= 32;
451
6.97k
          pImag -= 32;
452
6.97k
        }
453
454
107k
        if ((hSbrDec->savedStates == 0) && (hFrameData->sbrPatchingMode == 1)) {
455
          /* copy saved states from previous frame to legacy SBR lpc filterstate
456
           * buffer   */
457
251k
          for (i = 0; i < LPC_ORDER + ov_len; i++) {
458
224k
            FDKmemcpy(
459
224k
                hSbrDec->LppTrans.lpcFilterStatesRealLegSBR[i],
460
224k
                hSbrDec->codecQMFBufferReal[noCols - LPC_ORDER - ov_len + i],
461
224k
                hSbrDec->hHBE->noChannels * sizeof(FIXP_DBL));
462
224k
            FDKmemcpy(
463
224k
                hSbrDec->LppTrans.lpcFilterStatesImagLegSBR[i],
464
224k
                hSbrDec->codecQMFBufferImag[noCols - LPC_ORDER - ov_len + i],
465
224k
                hSbrDec->hHBE->noChannels * sizeof(FIXP_DBL));
466
224k
          }
467
27.1k
        }
468
469
        /* saving unmodified QMF states in case we are switching from legacy SBR
470
         * to HBE */
471
3.78M
        for (i = 0; i < hSbrDec->hHBE->noCols; i++) {
472
3.67M
          FDKmemcpy(hSbrDec->codecQMFBufferReal[i], pLowBandReal[ov_len + i],
473
3.67M
                    hSbrDec->hHBE->noChannels * sizeof(FIXP_DBL));
474
3.67M
          FDKmemcpy(hSbrDec->codecQMFBufferImag[i], pLowBandImag[ov_len + i],
475
3.67M
                    hSbrDec->hHBE->noChannels * sizeof(FIXP_DBL));
476
3.67M
        }
477
478
107k
        QmfTransposerApply(
479
107k
            hSbrDec->hHBE, pReal, pImag, noCols, pLowBandReal, pLowBandImag,
480
107k
            hSbrDec->LppTrans.lpcFilterStatesRealHBE,
481
107k
            hSbrDec->LppTrans.lpcFilterStatesImagHBE,
482
107k
            hFrameData->sbrPitchInBins, hSbrDec->scale_lb, hSbrDec->scale_hbe,
483
107k
            &hSbrDec->qmfDomainInCh->scaling.hb_scale, hHeaderData->timeStep,
484
107k
            borders[0], ov_len, keepStatesSyncedMode);
485
486
107k
        if (flags & SBRDEC_QUAD_RATE) {
487
6.97k
          int *xOverQmf = GetxOverBandQmfTransposer(hSbrDec->hHBE);
488
489
6.97k
          copyHarmonicSpectrum(xOverQmf, pLowBandReal, pLowBandImag, noCols,
490
6.97k
                               ov_len, keepStatesSyncedMode);
491
6.97k
        }
492
107k
      }
493
592k
    }
494
495
592k
    if ((flags & SBRDEC_USAC_HARMONICSBR) &&
496
107k
        (hFrameData->sbrPatchingMode == 0)) {
497
80.1k
      hSbrDec->prev_frame_lSbr = 0;
498
80.1k
      hSbrDec->prev_frame_hbeSbr = 1;
499
500
80.1k
      lppTransposerHBE(
501
80.1k
          &hSbrDec->LppTrans, hSbrDec->hHBE, &hSbrDec->qmfDomainInCh->scaling,
502
80.1k
          pLowBandReal, pLowBandImag, hHeaderData->timeStep, borders[0],
503
80.1k
          lastSlotOffs, hHeaderData->freqBandData.nInvfBands,
504
80.1k
          hFrameData->sbr_invf_mode, hPrevFrameData->sbr_invf_mode);
505
506
512k
    } else {
507
512k
      if (flags & SBRDEC_USAC_HARMONICSBR) {
508
258k
        for (i = 0; i < LPC_ORDER + hSbrDec->LppTrans.pSettings->overlap; i++) {
509
          /*
510
          Store the unmodified qmf Slots values for upper part of spectrum
511
          (required for LPC filtering) required if next frame is a HBE frame
512
          */
513
230k
          FDKmemcpy(hSbrDec->LppTrans.lpcFilterStatesRealHBE[i],
514
230k
                    hSbrDec->qmfDomainInCh
515
230k
                        ->hQmfSlotsReal[hSbrDec->hHBE->noCols - LPC_ORDER + i],
516
230k
                    (64) * sizeof(FIXP_DBL));
517
230k
          FDKmemcpy(hSbrDec->LppTrans.lpcFilterStatesImagHBE[i],
518
230k
                    hSbrDec->qmfDomainInCh
519
230k
                        ->hQmfSlotsImag[hSbrDec->hHBE->noCols - LPC_ORDER + i],
520
230k
                    (64) * sizeof(FIXP_DBL));
521
230k
        }
522
27.8k
      }
523
512k
      {
524
512k
        hSbrDec->prev_frame_lSbr = 1;
525
512k
        hSbrDec->prev_frame_hbeSbr = 0;
526
512k
      }
527
528
512k
      lppTransposer(
529
512k
          &hSbrDec->LppTrans, &hSbrDec->qmfDomainInCh->scaling, pLowBandReal,
530
512k
          degreeAlias,  // only used if useLP = 1
531
512k
          pLowBandImag, flags & SBRDEC_LOW_POWER,
532
512k
          hHeaderData->bs_info.sbr_preprocessing,
533
512k
          hHeaderData->freqBandData.v_k_master[0], hHeaderData->timeStep,
534
512k
          borders[0], lastSlotOffs, hHeaderData->freqBandData.nInvfBands,
535
512k
          hFrameData->sbr_invf_mode, hPrevFrameData->sbr_invf_mode);
536
512k
    }
537
538
    /*
539
      Adjust envelope of current frame.
540
    */
541
542
592k
    if ((hFrameData->sbrPatchingMode !=
543
592k
         hSbrDec->SbrCalculateEnvelope.sbrPatchingMode)) {
544
18
      ResetLimiterBands(hHeaderData->freqBandData.limiterBandTable,
545
18
                        &hHeaderData->freqBandData.noLimiterBands,
546
18
                        hHeaderData->freqBandData.freqBandTable[0],
547
18
                        hHeaderData->freqBandData.nSfb[0],
548
18
                        hSbrDec->LppTrans.pSettings->patchParam,
549
18
                        hSbrDec->LppTrans.pSettings->noOfPatches,
550
18
                        hHeaderData->bs_data.limiterBands,
551
18
                        hFrameData->sbrPatchingMode,
552
18
                        (flags & SBRDEC_USAC_HARMONICSBR) &&
553
18
                                (hFrameData->sbrPatchingMode == 0)
554
18
                            ? GetxOverBandQmfTransposer(hSbrDec->hHBE)
555
18
                            : NULL,
556
18
                        Get41SbrQmfTransposer(hSbrDec->hHBE));
557
558
18
      hSbrDec->SbrCalculateEnvelope.sbrPatchingMode =
559
18
          hFrameData->sbrPatchingMode;
560
18
    }
561
562
592k
    calculateSbrEnvelope(
563
592k
        &hSbrDec->qmfDomainInCh->scaling, &hSbrDec->SbrCalculateEnvelope,
564
592k
        hHeaderData, hFrameData, &pvcDynamicData, pLowBandReal, pLowBandImag,
565
592k
        flags & SBRDEC_LOW_POWER,
566
567
592k
        degreeAlias, flags,
568
592k
        (hHeaderData->frameErrorFlag || hPrevFrameData->frameErrorFlag));
569
570
#if (SBRDEC_MAX_HB_FADE_FRAMES > 0)
571
    /* Avoid hard onsets of high band */
572
    if (hHeaderData->frameErrorFlag) {
573
      if (hSbrDec->highBandFadeCnt < SBRDEC_MAX_HB_FADE_FRAMES) {
574
        hSbrDec->highBandFadeCnt += 1;
575
      }
576
    } else {
577
      if (hSbrDec->highBandFadeCnt >
578
          0) { /* Manipulate high band scale factor to get a smooth fade-in */
579
        hSbrDec->qmfDomainInCh->scaling.hb_scale += hSbrDec->highBandFadeCnt;
580
        hSbrDec->qmfDomainInCh->scaling.hb_scale =
581
            fMin(hSbrDec->qmfDomainInCh->scaling.hb_scale, DFRACT_BITS - 1);
582
        hSbrDec->highBandFadeCnt -= 1;
583
      }
584
    }
585
586
#endif
587
    /*
588
      Update hPrevFrameData (to be used in the next frame)
589
    */
590
1.88M
    for (i = 0; i < hHeaderData->freqBandData.nInvfBands; i++) {
591
1.29M
      hPrevFrameData->sbr_invf_mode[i] = hFrameData->sbr_invf_mode[i];
592
1.29M
    }
593
592k
    hPrevFrameData->coupling = hFrameData->coupling;
594
592k
    hPrevFrameData->stopPos = borders[hFrameData->frameInfo.nEnvelopes];
595
592k
    hPrevFrameData->ampRes = hFrameData->ampResolutionCurrentFrame;
596
592k
    hPrevFrameData->prevSbrPitchInBins = hFrameData->sbrPitchInBins;
597
    /* could be done in extractFrameInfo_pvc() but hPrevFrameData is not
598
     * available there */
599
592k
    FDKmemcpy(&hPrevFrameData->prevFrameInfo, &hFrameData->frameInfo,
600
592k
              sizeof(FRAME_INFO));
601
1.99M
  } else {
602
    /* rescale from lsb to nAnalysisBands in order to compensate scaling with
603
     * hb_scale in this area, done by synthesisFiltering*/
604
1.99M
    int rescale;
605
1.99M
    int lsb;
606
1.99M
    int length;
607
608
    /* Reset hb_scale if no highband is present, because hb_scale is considered
609
     * in the QMF-synthesis */
610
1.99M
    hSbrDec->qmfDomainInCh->scaling.hb_scale = saveLbScale;
611
612
1.99M
    rescale = hSbrDec->qmfDomainInCh->scaling.hb_scale -
613
1.99M
              hSbrDec->qmfDomainInCh->scaling.ov_lb_scale;
614
1.99M
    lsb = hSbrDec->qmfDomainOutCh->fb.lsb;
615
1.99M
    length = (hSbrDec->qmfDomainInCh->fb.no_channels - lsb);
616
617
1.99M
    if ((rescale < 0) && (length > 0)) {
618
143k
      if (!(flags & SBRDEC_LOW_POWER)) {
619
792k
        for (i = 0; i < ov_len; i++) {
620
678k
          scaleValues(&pLowBandReal[i][lsb], length, rescale);
621
678k
          scaleValues(&pLowBandImag[i][lsb], length, rescale);
622
678k
        }
623
113k
      } else {
624
136k
        for (i = 0; i < ov_len; i++) {
625
107k
          scaleValues(&pLowBandReal[i][lsb], length, rescale);
626
107k
        }
627
29.3k
      }
628
143k
    }
629
1.99M
  }
630
631
2.58M
  if (!(flags & SBRDEC_USAC_HARMONICSBR)) {
632
2.17M
    int length = hSbrDec->qmfDomainInCh->fb.lsb;
633
2.17M
    if (flags & SBRDEC_SYNTAX_USAC) {
634
947k
      length = hSbrDec->qmfDomainInCh->fb.no_channels;
635
947k
    }
636
637
    /* in case of legacy sbr saving of filter states here */
638
15.0M
    for (i = 0; i < LPC_ORDER + ov_len; i++) {
639
      /*
640
        Store the unmodified qmf Slots values (required for LPC filtering)
641
      */
642
12.8M
      if (!(flags & SBRDEC_LOW_POWER)) {
643
8.55M
        FDKmemcpy(hSbrDec->LppTrans.lpcFilterStatesRealLegSBR[i],
644
8.55M
                  pLowBandReal[noCols - LPC_ORDER + i],
645
8.55M
                  length * sizeof(FIXP_DBL));
646
8.55M
        FDKmemcpy(hSbrDec->LppTrans.lpcFilterStatesImagLegSBR[i],
647
8.55M
                  pLowBandImag[noCols - LPC_ORDER + i],
648
8.55M
                  length * sizeof(FIXP_DBL));
649
8.55M
      } else
650
4.31M
        FDKmemcpy(hSbrDec->LppTrans.lpcFilterStatesRealLegSBR[i],
651
4.31M
                  pLowBandReal[noCols - LPC_ORDER + i],
652
4.31M
                  length * sizeof(FIXP_DBL));
653
12.8M
    }
654
2.17M
  }
655
656
  /*
657
    Synthesis subband filtering.
658
  */
659
660
2.58M
  if (!(flags & SBRDEC_PS_DECODED)) {
661
2.57M
    if (!(flags & SBRDEC_SKIP_QMF_SYN)) {
662
2.48M
      int outScalefactor = -(8);
663
664
2.48M
      if (h_ps_d != NULL) {
665
412k
        h_ps_d->procFrameBased = 1; /* we here do frame based processing */
666
412k
      }
667
668
2.48M
      sbrDecoder_drcApply(&hSbrDec->sbrDrcChannel, pLowBandReal,
669
2.48M
                          (flags & SBRDEC_LOW_POWER) ? NULL : pLowBandImag,
670
2.48M
                          hSbrDec->qmfDomainOutCh->fb.no_col, &outScalefactor);
671
672
2.48M
      qmfChangeOutScalefactor(&hSbrDec->qmfDomainOutCh->fb, outScalefactor);
673
674
2.48M
      {
675
2.48M
        HANDLE_FREQ_BAND_DATA hFreq = &hHeaderData->freqBandData;
676
2.48M
        int save_usb = hSbrDec->qmfDomainOutCh->fb.usb;
677
678
2.48M
#if (QMF_MAX_SYNTHESIS_BANDS <= 64)
679
2.48M
        C_AALLOC_SCRATCH_START(qmfTemp, FIXP_DBL, 2 * QMF_MAX_SYNTHESIS_BANDS);
680
#else
681
        C_AALLOC_STACK_START(qmfTemp, FIXP_DBL, 2 * QMF_MAX_SYNTHESIS_BANDS);
682
#endif
683
2.48M
        if (hSbrDec->qmfDomainOutCh->fb.usb < hFreq->ov_highSubband) {
684
          /* we need to patch usb for this frame as overlap may contain higher
685
             frequency range if headerchange occured; fb. usb is always limited
686
             to maximum fb.no_channels; In case of wrongly decoded headers it
687
             might be that ov_highSubband is higher than the number of synthesis
688
             channels (fb.no_channels), which is forbidden, therefore we need to
689
             limit ov_highSubband with fMin function to avoid not allowed usb in
690
             synthesis filterbank. */
691
54.1k
          hSbrDec->qmfDomainOutCh->fb.usb =
692
54.1k
              fMin((UINT)hFreq->ov_highSubband,
693
54.1k
                   (UINT)hSbrDec->qmfDomainOutCh->fb.no_channels);
694
54.1k
        }
695
2.48M
        {
696
2.48M
          qmfSynthesisFiltering(
697
2.48M
              &hSbrDec->qmfDomainOutCh->fb, pLowBandReal,
698
2.48M
              (flags & SBRDEC_LOW_POWER) ? NULL : pLowBandImag,
699
2.48M
              &hSbrDec->qmfDomainInCh->scaling,
700
2.48M
              hSbrDec->LppTrans.pSettings->overlap, timeOut, strideOut,
701
2.48M
              qmfTemp);
702
2.48M
        }
703
        /* restore saved value */
704
2.48M
        hSbrDec->qmfDomainOutCh->fb.usb = save_usb;
705
2.48M
        hFreq->ov_highSubband = save_usb;
706
2.48M
#if (QMF_MAX_SYNTHESIS_BANDS <= 64)
707
2.48M
        C_AALLOC_SCRATCH_END(qmfTemp, FIXP_DBL, 2 * QMF_MAX_SYNTHESIS_BANDS);
708
#else
709
        C_AALLOC_STACK_END(qmfTemp, FIXP_DBL, 2 * QMF_MAX_SYNTHESIS_BANDS);
710
#endif
711
2.48M
      }
712
2.48M
    }
713
714
2.57M
  } else { /* (flags & SBRDEC_PS_DECODED) */
715
14.1k
    INT sdiff;
716
14.1k
    INT scaleFactorHighBand, scaleFactorLowBand_ov, scaleFactorLowBand_no_ov,
717
14.1k
        outScalefactor, outScalefactorR, outScalefactorL;
718
719
14.1k
    HANDLE_QMF_FILTER_BANK synQmf = &hSbrDec->qmfDomainOutCh->fb;
720
14.1k
    HANDLE_QMF_FILTER_BANK synQmfRight = &hSbrDecRight->qmfDomainOutCh->fb;
721
722
    /* adapt scaling */
723
14.1k
    sdiff = hSbrDec->qmfDomainInCh->scaling.lb_scale -
724
14.1k
            reserve; /* Scaling difference */
725
14.1k
    scaleFactorHighBand = sdiff - hSbrDec->qmfDomainInCh->scaling.hb_scale;
726
14.1k
    scaleFactorLowBand_ov = sdiff - hSbrDec->qmfDomainInCh->scaling.ov_lb_scale;
727
14.1k
    scaleFactorLowBand_no_ov = sdiff - hSbrDec->qmfDomainInCh->scaling.lb_scale;
728
729
    /* Scale of low band overlapping QMF data */
730
14.1k
    scaleFactorLowBand_ov =
731
14.1k
        fMin(DFRACT_BITS - 1, fMax(-(DFRACT_BITS - 1), scaleFactorLowBand_ov));
732
    /* Scale of low band current QMF data     */
733
14.1k
    scaleFactorLowBand_no_ov = fMin(
734
14.1k
        DFRACT_BITS - 1, fMax(-(DFRACT_BITS - 1), scaleFactorLowBand_no_ov));
735
    /* Scale of current high band */
736
14.1k
    scaleFactorHighBand =
737
14.1k
        fMin(DFRACT_BITS - 1, fMax(-(DFRACT_BITS - 1), scaleFactorHighBand));
738
739
14.1k
    if (h_ps_d->procFrameBased == 1) /* If we have switched from frame to slot
740
                                        based processing copy filter states */
741
11.4k
    {                                /* procFrameBased will be unset later */
742
      /* copy filter states from left to right */
743
      /* was ((640)-(64))*sizeof(FIXP_QSS)
744
         flexible amount of synthesis bands needed for QMF based resampling
745
      */
746
11.4k
      FDK_ASSERT(hSbrDec->qmfDomainInCh->pGlobalConf->nBandsSynthesis <=
747
11.4k
                 QMF_MAX_SYNTHESIS_BANDS);
748
11.4k
      synQmfRight->outScalefactor = synQmf->outScalefactor;
749
11.4k
      FDKmemcpy(synQmfRight->FilterStates, synQmf->FilterStates,
750
11.4k
                9 * hSbrDec->qmfDomainInCh->pGlobalConf->nBandsSynthesis *
751
11.4k
                    sizeof(FIXP_QSS));
752
11.4k
    }
753
754
    /* Feed delaylines when parametric stereo is switched on. */
755
14.1k
    PreparePsProcessing(h_ps_d, pLowBandReal, pLowBandImag,
756
14.1k
                        scaleFactorLowBand_ov);
757
758
    /* use the same synthese qmf values for left and right channel */
759
14.1k
    synQmfRight->no_col = synQmf->no_col;
760
14.1k
    synQmfRight->lsb = synQmf->lsb;
761
14.1k
    synQmfRight->usb = synQmf->usb;
762
763
14.1k
    int env = 0;
764
765
14.1k
    {
766
14.1k
#if (QMF_MAX_SYNTHESIS_BANDS <= 64)
767
14.1k
      C_AALLOC_SCRATCH_START(pWorkBuffer, FIXP_DBL,
768
14.1k
                             2 * QMF_MAX_SYNTHESIS_BANDS);
769
#else
770
      C_AALLOC_STACK_START(pWorkBuffer, FIXP_DBL, 2 * QMF_MAX_SYNTHESIS_BANDS);
771
#endif
772
773
14.1k
      int maxShift = 0;
774
775
14.1k
      if (hSbrDec->sbrDrcChannel.enable != 0) {
776
1.30k
        if (hSbrDec->sbrDrcChannel.prevFact_exp > maxShift) {
777
1.24k
          maxShift = hSbrDec->sbrDrcChannel.prevFact_exp;
778
1.24k
        }
779
1.30k
        if (hSbrDec->sbrDrcChannel.currFact_exp > maxShift) {
780
119
          maxShift = hSbrDec->sbrDrcChannel.currFact_exp;
781
119
        }
782
1.30k
        if (hSbrDec->sbrDrcChannel.nextFact_exp > maxShift) {
783
172
          maxShift = hSbrDec->sbrDrcChannel.nextFact_exp;
784
172
        }
785
1.30k
      }
786
787
      /* copy DRC data to right channel (with PS both channels use the same DRC
788
       * gains) */
789
14.1k
      FDKmemcpy(&hSbrDecRight->sbrDrcChannel, &hSbrDec->sbrDrcChannel,
790
14.1k
                sizeof(SBRDEC_DRC_CHANNEL));
791
792
14.1k
      outScalefactor = maxShift - (8);
793
14.1k
      outScalefactorL = outScalefactorR =
794
14.1k
          sbrInDataHeadroom + 1; /* +1: psDiffScale! (MPEG-PS) */
795
796
468k
      for (i = 0; i < synQmf->no_col; i++) { /* ----- no_col loop ----- */
797
798
        /* qmf timeslot of right channel */
799
453k
        FIXP_DBL *rQmfReal = pWorkBuffer;
800
453k
        FIXP_DBL *rQmfImag = pWorkBuffer + synQmf->no_channels;
801
802
453k
        {
803
453k
          if (i ==
804
453k
              h_ps_d->bsData[h_ps_d->processSlot].mpeg.aEnvStartStop[env]) {
805
41.8k
            initSlotBasedRotation(h_ps_d, env,
806
41.8k
                                  hHeaderData->freqBandData.highSubband);
807
41.8k
            env++;
808
41.8k
          }
809
810
453k
          ApplyPsSlot(
811
453k
              h_ps_d,             /* parametric stereo decoder handle  */
812
453k
              (pLowBandReal + i), /* one timeslot of left/mono channel */
813
453k
              (pLowBandImag + i), /* one timeslot of left/mono channel */
814
453k
              rQmfReal,           /* one timeslot or right channel     */
815
453k
              rQmfImag,           /* one timeslot or right channel     */
816
453k
              scaleFactorLowBand_no_ov,
817
453k
              (i < hSbrDec->LppTrans.pSettings->overlap)
818
453k
                  ? scaleFactorLowBand_ov
819
453k
                  : scaleFactorLowBand_no_ov,
820
453k
              scaleFactorHighBand, synQmf->lsb, synQmf->usb);
821
453k
        }
822
823
453k
        sbrDecoder_drcApplySlot(/* right channel */
824
453k
                                &hSbrDecRight->sbrDrcChannel, rQmfReal,
825
453k
                                rQmfImag, i, synQmfRight->no_col, maxShift);
826
827
453k
        sbrDecoder_drcApplySlot(/* left channel */
828
453k
                                &hSbrDec->sbrDrcChannel, *(pLowBandReal + i),
829
453k
                                *(pLowBandImag + i), i, synQmf->no_col,
830
453k
                                maxShift);
831
832
453k
        if (!(flags & SBRDEC_SKIP_QMF_SYN)) {
833
453k
          qmfChangeOutScalefactor(synQmf, outScalefactor);
834
453k
          qmfChangeOutScalefactor(synQmfRight, outScalefactor);
835
836
453k
          qmfSynthesisFilteringSlot(
837
453k
              synQmfRight, rQmfReal, /* QMF real buffer */
838
453k
              rQmfImag,              /* QMF imag buffer */
839
453k
              outScalefactorL, outScalefactorL,
840
453k
              timeOutRight + (i * synQmf->no_channels * strideOut), strideOut,
841
453k
              pWorkBuffer);
842
843
453k
          qmfSynthesisFilteringSlot(
844
453k
              synQmf, *(pLowBandReal + i), /* QMF real buffer */
845
453k
              *(pLowBandImag + i),         /* QMF imag buffer */
846
453k
              outScalefactorR, outScalefactorR,
847
453k
              timeOut + (i * synQmf->no_channels * strideOut), strideOut,
848
453k
              pWorkBuffer);
849
453k
        }
850
453k
      } /* no_col loop  i  */
851
14.1k
#if (QMF_MAX_SYNTHESIS_BANDS <= 64)
852
14.1k
      C_AALLOC_SCRATCH_END(pWorkBuffer, FIXP_DBL, 2 * QMF_MAX_SYNTHESIS_BANDS);
853
#else
854
      C_AALLOC_STACK_END(pWorkBuffer, FIXP_DBL, 2 * QMF_MAX_SYNTHESIS_BANDS);
855
#endif
856
14.1k
    }
857
14.1k
  }
858
859
2.58M
  sbrDecoder_drcUpdateChannel(&hSbrDec->sbrDrcChannel);
860
861
  /*
862
    Update overlap buffer
863
    Even bands above usb are copied to avoid outdated spectral data in case
864
    the stop frequency raises.
865
  */
866
867
2.58M
  if (!(flags & SBRDEC_SKIP_QMF_SYN)) {
868
2.49M
    {
869
2.49M
      FDK_QmfDomain_SaveOverlap(hSbrDec->qmfDomainInCh, 0);
870
2.49M
      FDK_ASSERT(hSbrDec->qmfDomainInCh->scaling.ov_lb_scale == saveLbScale);
871
2.49M
    }
872
2.49M
  }
873
874
2.58M
  hSbrDec->savedStates = 0;
875
876
  /* Save current frame status */
877
2.58M
  hPrevFrameData->frameErrorFlag = hHeaderData->frameErrorFlag;
878
2.58M
  hSbrDec->applySbrProc_old = applyProcessing;
879
880
2.58M
} /* sbr_dec() */
881
882
/*!
883
  \brief     Creates sbr decoder structure
884
  \return    errorCode, 0 if successful
885
*/
886
SBR_ERROR
887
createSbrDec(SBR_CHANNEL *hSbrChannel,
888
             HANDLE_SBR_HEADER_DATA hHeaderData, /*!< Static control data */
889
             TRANSPOSER_SETTINGS *pSettings,
890
             const int downsampleFac, /*!< Downsampling factor */
891
             const UINT qmfFlags, /*!< flags -> 1: HQ/LP selector, 2: CLDFB */
892
             const UINT flags, const int overlap,
893
             int chan, /*!< Channel for which to assign buffers etc. */
894
             int codecFrameSize)
895
896
170k
{
897
170k
  SBR_ERROR err = SBRDEC_OK;
898
170k
  int timeSlots =
899
170k
      hHeaderData->numberTimeSlots; /* Number of SBR slots per frame */
900
170k
  int noCols =
901
170k
      timeSlots * hHeaderData->timeStep; /* Number of QMF slots per frame */
902
170k
  HANDLE_SBR_DEC hs = &(hSbrChannel->SbrDec);
903
904
#if (SBRDEC_MAX_HB_FADE_FRAMES > 0)
905
  hs->highBandFadeCnt = SBRDEC_MAX_HB_FADE_FRAMES;
906
907
#endif
908
170k
  hs->scale_hbe = 15;
909
170k
  hs->scale_lb = 15;
910
170k
  hs->scale_ov = 15;
911
912
170k
  hs->prev_frame_lSbr = 0;
913
170k
  hs->prev_frame_hbeSbr = 0;
914
915
170k
  hs->codecFrameSize = codecFrameSize;
916
917
  /*
918
    create envelope calculator
919
  */
920
170k
  err = createSbrEnvelopeCalc(&hs->SbrCalculateEnvelope, hHeaderData, chan,
921
170k
                              flags);
922
170k
  if (err != SBRDEC_OK) {
923
14
    return err;
924
14
  }
925
926
170k
  initSbrPrevFrameData(&hSbrChannel->prevFrameData, timeSlots);
927
928
  /*
929
    create transposer
930
  */
931
170k
  err = createLppTransposer(
932
170k
      &hs->LppTrans, pSettings, hHeaderData->freqBandData.lowSubband,
933
170k
      hHeaderData->freqBandData.v_k_master, hHeaderData->freqBandData.numMaster,
934
170k
      hHeaderData->freqBandData.highSubband, timeSlots, noCols,
935
170k
      hHeaderData->freqBandData.freqBandTableNoise,
936
170k
      hHeaderData->freqBandData.nNfb, hHeaderData->sbrProcSmplRate, chan,
937
170k
      overlap);
938
170k
  if (err != SBRDEC_OK) {
939
12
    return err;
940
12
  }
941
942
170k
  if (flags & SBRDEC_USAC_HARMONICSBR) {
943
2.66k
    int noChannels, bSbr41 = flags & SBRDEC_QUAD_RATE ? 1 : 0;
944
945
2.66k
    noChannels =
946
2.66k
        QMF_SYNTH_CHANNELS /
947
2.66k
        ((bSbr41 + 1) * 2); /* 32 for (32:64 and 24:64) and 16 for 16:64 */
948
949
    /* shared memory between hbeLightTimeDelayBuffer and hQmfHBESlotsReal if
950
     * SBRDEC_HBE_ENABLE */
951
2.66k
    hSbrChannel->SbrDec.tmp_memory = (FIXP_DBL **)fdkCallocMatrix2D_aligned(
952
2.66k
        noCols, noChannels, sizeof(FIXP_DBL));
953
2.66k
    if (hSbrChannel->SbrDec.tmp_memory == NULL) {
954
0
      return SBRDEC_MEM_ALLOC_FAILED;
955
0
    }
956
957
2.66k
    hSbrChannel->SbrDec.hQmfHBESlotsReal = hSbrChannel->SbrDec.tmp_memory;
958
2.66k
    hSbrChannel->SbrDec.hQmfHBESlotsImag =
959
2.66k
        (FIXP_DBL **)fdkCallocMatrix2D_aligned(noCols, noChannels,
960
2.66k
                                               sizeof(FIXP_DBL));
961
2.66k
    if (hSbrChannel->SbrDec.hQmfHBESlotsImag == NULL) {
962
0
      return SBRDEC_MEM_ALLOC_FAILED;
963
0
    }
964
965
    /* buffers containing unmodified qmf data; required when switching from
966
     * legacy SBR to HBE                       */
967
    /* buffer can be used as LPCFilterstates buffer because legacy SBR needs
968
     * exactly these values for LPC filtering */
969
2.66k
    hSbrChannel->SbrDec.codecQMFBufferReal =
970
2.66k
        (FIXP_DBL **)fdkCallocMatrix2D_aligned(noCols, noChannels,
971
2.66k
                                               sizeof(FIXP_DBL));
972
2.66k
    if (hSbrChannel->SbrDec.codecQMFBufferReal == NULL) {
973
0
      return SBRDEC_MEM_ALLOC_FAILED;
974
0
    }
975
976
2.66k
    hSbrChannel->SbrDec.codecQMFBufferImag =
977
2.66k
        (FIXP_DBL **)fdkCallocMatrix2D_aligned(noCols, noChannels,
978
2.66k
                                               sizeof(FIXP_DBL));
979
2.66k
    if (hSbrChannel->SbrDec.codecQMFBufferImag == NULL) {
980
0
      return SBRDEC_MEM_ALLOC_FAILED;
981
0
    }
982
983
2.66k
    err = QmfTransposerCreate(&hs->hHBE, codecFrameSize, 0, bSbr41);
984
2.66k
    if (err != SBRDEC_OK) {
985
0
      return err;
986
0
    }
987
2.66k
  }
988
989
170k
  return err;
990
170k
}
991
992
/*!
993
  \brief     Delete sbr decoder structure
994
  \return    errorCode, 0 if successful
995
*/
996
167k
int deleteSbrDec(SBR_CHANNEL *hSbrChannel) {
997
167k
  HANDLE_SBR_DEC hs = &hSbrChannel->SbrDec;
998
999
167k
  deleteSbrEnvelopeCalc(&hs->SbrCalculateEnvelope);
1000
1001
167k
  if (hs->tmp_memory != NULL) {
1002
2.66k
    FDK_FREE_MEMORY_2D_ALIGNED(hs->tmp_memory);
1003
2.66k
  }
1004
1005
  /* modify here */
1006
167k
  FDK_FREE_MEMORY_2D_ALIGNED(hs->hQmfHBESlotsImag);
1007
1008
167k
  if (hs->hHBE != NULL) QmfTransposerClose(hs->hHBE);
1009
1010
167k
  if (hs->codecQMFBufferReal != NULL) {
1011
2.66k
    FDK_FREE_MEMORY_2D_ALIGNED(hs->codecQMFBufferReal);
1012
2.66k
  }
1013
1014
167k
  if (hs->codecQMFBufferImag != NULL) {
1015
2.66k
    FDK_FREE_MEMORY_2D_ALIGNED(hs->codecQMFBufferImag);
1016
2.66k
  }
1017
1018
167k
  return 0;
1019
167k
}
1020
1021
/*!
1022
  \brief     resets sbr decoder structure
1023
  \return    errorCode, 0 if successful
1024
*/
1025
SBR_ERROR
1026
resetSbrDec(HANDLE_SBR_DEC hSbrDec, HANDLE_SBR_HEADER_DATA hHeaderData,
1027
            HANDLE_SBR_PREV_FRAME_DATA hPrevFrameData, const int downsampleFac,
1028
2.23M
            const UINT flags, HANDLE_SBR_FRAME_DATA hFrameData) {
1029
2.23M
  SBR_ERROR sbrError = SBRDEC_OK;
1030
2.23M
  int i;
1031
2.23M
  FIXP_DBL *pLowBandReal[128];
1032
2.23M
  FIXP_DBL *pLowBandImag[128];
1033
2.23M
  int useLP = flags & SBRDEC_LOW_POWER;
1034
1035
2.23M
  int old_lsb = hSbrDec->qmfDomainInCh->fb.lsb;
1036
2.23M
  int old_usb = hSbrDec->qmfDomainInCh->fb.usb;
1037
2.23M
  int new_lsb = hHeaderData->freqBandData.lowSubband;
1038
  /* int new_usb = hHeaderData->freqBandData.highSubband; */
1039
2.23M
  int l, startBand, stopBand, startSlot, size;
1040
1041
2.23M
  FIXP_DBL **OverlapBufferReal = hSbrDec->qmfDomainInCh->hQmfSlotsReal;
1042
2.23M
  FIXP_DBL **OverlapBufferImag = hSbrDec->qmfDomainInCh->hQmfSlotsImag;
1043
1044
  /* in case the previous frame was not active in terms of SBR processing, the
1045
     full band from 0 to no_channels was rescaled and not overwritten. Thats why
1046
     the scaling factor lb_scale can be seen as assigned to all bands from 0 to
1047
     no_channels in the previous frame. The same states for the current frame if
1048
     the current frame is not active in terms of SBR processing
1049
  */
1050
2.23M
  int applySbrProc = (hHeaderData->syncState == SBR_ACTIVE ||
1051
2.23M
                      (hHeaderData->frameErrorFlag == 0 &&
1052
259k
                       hHeaderData->syncState == SBR_HEADER));
1053
2.23M
  int applySbrProc_old = hSbrDec->applySbrProc_old;
1054
1055
2.23M
  if (!applySbrProc) {
1056
2.15M
    new_lsb = (hSbrDec->qmfDomainInCh->fb).no_channels;
1057
2.15M
  }
1058
2.23M
  if (!applySbrProc_old) {
1059
2.20M
    old_lsb = (hSbrDec->qmfDomainInCh->fb).no_channels;
1060
2.20M
    old_usb = old_lsb;
1061
2.20M
  }
1062
1063
2.23M
  resetSbrEnvelopeCalc(&hSbrDec->SbrCalculateEnvelope);
1064
1065
  /* Change lsb and usb */
1066
  /* Synthesis */
1067
2.23M
  FDK_ASSERT(hSbrDec->qmfDomainOutCh != NULL);
1068
2.23M
  hSbrDec->qmfDomainOutCh->fb.lsb =
1069
2.23M
      fixMin((INT)hSbrDec->qmfDomainOutCh->fb.no_channels,
1070
2.23M
             (INT)hHeaderData->freqBandData.lowSubband);
1071
2.23M
  hSbrDec->qmfDomainOutCh->fb.usb =
1072
2.23M
      fixMin((INT)hSbrDec->qmfDomainOutCh->fb.no_channels,
1073
2.23M
             (INT)hHeaderData->freqBandData.highSubband);
1074
  /* Analysis */
1075
2.23M
  FDK_ASSERT(hSbrDec->qmfDomainInCh != NULL);
1076
2.23M
  hSbrDec->qmfDomainInCh->fb.lsb = hSbrDec->qmfDomainOutCh->fb.lsb;
1077
2.23M
  hSbrDec->qmfDomainInCh->fb.usb = hSbrDec->qmfDomainOutCh->fb.usb;
1078
1079
  /*
1080
    The following initialization of spectral data in the overlap buffer
1081
    is required for dynamic x-over or a change of the start-freq for 2 reasons:
1082
1083
    1. If the lowband gets _wider_, unadjusted data would remain
1084
1085
    2. If the lowband becomes _smaller_, the highest bands of the old lowband
1086
       must be cleared because the whitening would be affected
1087
  */
1088
2.23M
  startBand = old_lsb;
1089
2.23M
  stopBand = new_lsb;
1090
2.23M
  startSlot = fMax(0, hHeaderData->timeStep * (hPrevFrameData->stopPos -
1091
2.23M
                                               hHeaderData->numberTimeSlots));
1092
2.23M
  size = fMax(0, stopBand - startBand);
1093
1094
  /* in case of USAC we don't want to zero out the memory, as this can lead to
1095
     holes in the spectrum; fix shall only be applied for USAC not for MPEG-4
1096
     SBR, in this case setting zero remains         */
1097
2.23M
  if (!(flags & SBRDEC_SYNTAX_USAC)) {
1098
    /* keep already adjusted data in the x-over-area */
1099
991k
    if (!useLP) {
1100
1.26M
      for (l = startSlot; l < hSbrDec->LppTrans.pSettings->overlap; l++) {
1101
1.05M
        FDKmemclear(&OverlapBufferReal[l][startBand], size * sizeof(FIXP_DBL));
1102
1.05M
        FDKmemclear(&OverlapBufferImag[l][startBand], size * sizeof(FIXP_DBL));
1103
1.05M
      }
1104
790k
    } else {
1105
3.06M
      for (l = startSlot; l < hSbrDec->LppTrans.pSettings->overlap; l++) {
1106
2.27M
        FDKmemclear(&OverlapBufferReal[l][startBand], size * sizeof(FIXP_DBL));
1107
2.27M
      }
1108
790k
    }
1109
1110
    /*
1111
    reset LPC filter states
1112
    */
1113
991k
    startBand = fixMin(old_lsb, new_lsb);
1114
991k
    stopBand = fixMax(old_lsb, new_lsb);
1115
991k
    size = fixMax(0, stopBand - startBand);
1116
1117
991k
    FDKmemclear(&hSbrDec->LppTrans.lpcFilterStatesRealLegSBR[0][startBand],
1118
991k
                size * sizeof(FIXP_DBL));
1119
991k
    FDKmemclear(&hSbrDec->LppTrans.lpcFilterStatesRealLegSBR[1][startBand],
1120
991k
                size * sizeof(FIXP_DBL));
1121
991k
    if (!useLP) {
1122
201k
      FDKmemclear(&hSbrDec->LppTrans.lpcFilterStatesImagLegSBR[0][startBand],
1123
201k
                  size * sizeof(FIXP_DBL));
1124
201k
      FDKmemclear(&hSbrDec->LppTrans.lpcFilterStatesImagLegSBR[1][startBand],
1125
201k
                  size * sizeof(FIXP_DBL));
1126
201k
    }
1127
991k
  }
1128
1129
2.23M
  if (startSlot != 0) {
1130
7.05k
    int source_exp, target_exp, delta_exp, target_lsb, target_usb, reserve;
1131
7.05k
    FIXP_DBL maxVal;
1132
1133
    /*
1134
    Rescale already processed spectral data between old and new x-over
1135
    frequency. This must be done because of the separate scalefactors for
1136
    lowband and highband.
1137
    */
1138
1139
    /* We have four relevant transitions to cover:
1140
    1. old_usb is lower than new_lsb; old SBR area is completely below new SBR
1141
    area.
1142
       -> entire old area was highband and belongs to lowband now
1143
          and has to be rescaled.
1144
    2. old_lsb is higher than new_usb; new SBR area is completely below old SBR
1145
    area.
1146
       -> old area between new_lsb and old_lsb was lowband and belongs to
1147
    highband now and has to be rescaled to match new highband scale.
1148
    3. old_lsb is lower and old_usb is higher than new_lsb; old and new SBR
1149
    areas overlap.
1150
       -> old area between old_lsb and new_lsb was highband and belongs to
1151
    lowband now and has to be rescaled to match new lowband scale.
1152
    4. new_lsb is lower and new_usb_is higher than old_lsb; old and new SBR
1153
    areas overlap.
1154
       -> old area between new_lsb and old_usb was lowband and belongs to
1155
    highband now and has to be rescaled to match new highband scale.
1156
    */
1157
1158
7.05k
    if (new_lsb > old_lsb) {
1159
      /* case 1 and 3 */
1160
1.95k
      source_exp = SCALE2EXP(hSbrDec->qmfDomainInCh->scaling.ov_hb_scale);
1161
1.95k
      target_exp = SCALE2EXP(hSbrDec->qmfDomainInCh->scaling.ov_lb_scale);
1162
1163
1.95k
      startBand = old_lsb;
1164
1165
1.95k
      if (new_lsb >= old_usb) {
1166
        /* case 1 */
1167
1.69k
        stopBand = old_usb;
1168
1.69k
      } else {
1169
        /* case 3 */
1170
262
        stopBand = new_lsb;
1171
262
      }
1172
1173
1.95k
      target_lsb = 0;
1174
1.95k
      target_usb = old_lsb;
1175
5.10k
    } else {
1176
      /* case 2 and 4 */
1177
5.10k
      source_exp = SCALE2EXP(hSbrDec->qmfDomainInCh->scaling.ov_lb_scale);
1178
5.10k
      target_exp = SCALE2EXP(hSbrDec->qmfDomainInCh->scaling.ov_hb_scale);
1179
1180
5.10k
      startBand = new_lsb;
1181
5.10k
      stopBand = old_lsb;
1182
1183
5.10k
      target_lsb = old_lsb;
1184
5.10k
      target_usb = old_usb;
1185
5.10k
    }
1186
1187
7.05k
    maxVal =
1188
7.05k
        maxSubbandSample(OverlapBufferReal, (useLP) ? NULL : OverlapBufferImag,
1189
7.05k
                         startBand, stopBand, 0, startSlot);
1190
1191
7.05k
    reserve = ((LONG)maxVal != 0 ? CntLeadingZeros(maxVal) - 1 : 0);
1192
7.05k
    reserve = fixMin(
1193
7.05k
        reserve,
1194
7.05k
        DFRACT_BITS - 1 -
1195
7.05k
            EXP2SCALE(
1196
7.05k
                source_exp)); /* what is this line for, why do we need it? */
1197
1198
    /* process only if x-over-area is not dominant after rescale;
1199
       otherwise I'm not sure if all buffers are scaled correctly;
1200
    */
1201
7.05k
    if (target_exp - (source_exp - reserve) >= 0) {
1202
5.88k
      rescaleSubbandSamples(OverlapBufferReal,
1203
5.88k
                            (useLP) ? NULL : OverlapBufferImag, startBand,
1204
5.88k
                            stopBand, 0, startSlot, reserve);
1205
5.88k
      source_exp -= reserve;
1206
5.88k
    }
1207
1208
7.05k
    delta_exp = target_exp - source_exp;
1209
1210
7.05k
    if (delta_exp < 0) { /* x-over-area is dominant */
1211
1.17k
      startBand = target_lsb;
1212
1.17k
      stopBand = target_usb;
1213
1.17k
      delta_exp = -delta_exp;
1214
1215
1.17k
      if (new_lsb > old_lsb) {
1216
        /* The lowband has to be rescaled */
1217
1.08k
        hSbrDec->qmfDomainInCh->scaling.ov_lb_scale = EXP2SCALE(source_exp);
1218
1.08k
      } else {
1219
        /* The highband has to be rescaled */
1220
88
        hSbrDec->qmfDomainInCh->scaling.ov_hb_scale = EXP2SCALE(source_exp);
1221
88
      }
1222
1.17k
    }
1223
1224
7.05k
    FDK_ASSERT(startBand <= stopBand);
1225
1226
7.05k
    if (!useLP) {
1227
47.7k
      for (l = 0; l < startSlot; l++) {
1228
40.8k
        scaleValues(OverlapBufferReal[l] + startBand, stopBand - startBand,
1229
40.8k
                    -delta_exp);
1230
40.8k
        scaleValues(OverlapBufferImag[l] + startBand, stopBand - startBand,
1231
40.8k
                    -delta_exp);
1232
40.8k
      }
1233
6.90k
    } else
1234
844
      for (l = 0; l < startSlot; l++) {
1235
692
        scaleValues(OverlapBufferReal[l] + startBand, stopBand - startBand,
1236
692
                    -delta_exp);
1237
692
      }
1238
7.05k
  } /* startSlot != 0 */
1239
1240
  /*
1241
    Initialize transposer and limiter
1242
  */
1243
2.23M
  sbrError = resetLppTransposer(
1244
2.23M
      &hSbrDec->LppTrans, hHeaderData->freqBandData.lowSubband,
1245
2.23M
      hHeaderData->freqBandData.v_k_master, hHeaderData->freqBandData.numMaster,
1246
2.23M
      hHeaderData->freqBandData.freqBandTableNoise,
1247
2.23M
      hHeaderData->freqBandData.nNfb, hHeaderData->freqBandData.highSubband,
1248
2.23M
      hHeaderData->sbrProcSmplRate);
1249
2.23M
  if (sbrError != SBRDEC_OK) return sbrError;
1250
1251
2.22M
  hSbrDec->savedStates = 0;
1252
1253
2.22M
  if ((flags & SBRDEC_USAC_HARMONICSBR) && applySbrProc) {
1254
4.14k
    sbrError = QmfTransposerReInit(hSbrDec->hHBE,
1255
4.14k
                                   hHeaderData->freqBandData.freqBandTable,
1256
4.14k
                                   hHeaderData->freqBandData.nSfb);
1257
4.14k
    if (sbrError != SBRDEC_OK) return sbrError;
1258
1259
    /* copy saved states from previous frame to legacy SBR lpc filterstate
1260
     * buffer   */
1261
41.2k
    for (i = 0; i < LPC_ORDER + hSbrDec->LppTrans.pSettings->overlap; i++) {
1262
37.1k
      FDKmemcpy(
1263
37.1k
          hSbrDec->LppTrans.lpcFilterStatesRealLegSBR[i],
1264
37.1k
          hSbrDec->codecQMFBufferReal[hSbrDec->hHBE->noCols - LPC_ORDER -
1265
37.1k
                                      hSbrDec->LppTrans.pSettings->overlap + i],
1266
37.1k
          hSbrDec->hHBE->noChannels * sizeof(FIXP_DBL));
1267
37.1k
      FDKmemcpy(
1268
37.1k
          hSbrDec->LppTrans.lpcFilterStatesImagLegSBR[i],
1269
37.1k
          hSbrDec->codecQMFBufferImag[hSbrDec->hHBE->noCols - LPC_ORDER -
1270
37.1k
                                      hSbrDec->LppTrans.pSettings->overlap + i],
1271
37.1k
          hSbrDec->hHBE->noChannels * sizeof(FIXP_DBL));
1272
37.1k
    }
1273
4.14k
    hSbrDec->savedStates = 1;
1274
1275
4.14k
    {
1276
      /* map QMF buffer to pointer array (Overlap + Frame)*/
1277
41.2k
      for (i = 0; i < hSbrDec->LppTrans.pSettings->overlap + LPC_ORDER; i++) {
1278
37.1k
        pLowBandReal[i] = hSbrDec->LppTrans.lpcFilterStatesRealHBE[i];
1279
37.1k
        pLowBandImag[i] = hSbrDec->LppTrans.lpcFilterStatesImagHBE[i];
1280
37.1k
      }
1281
1282
      /* map QMF buffer to pointer array (Overlap + Frame)*/
1283
157k
      for (i = 0; i < hSbrDec->hHBE->noCols; i++) {
1284
153k
        pLowBandReal[i + hSbrDec->LppTrans.pSettings->overlap + LPC_ORDER] =
1285
153k
            hSbrDec->codecQMFBufferReal[i];
1286
153k
        pLowBandImag[i + hSbrDec->LppTrans.pSettings->overlap + LPC_ORDER] =
1287
153k
            hSbrDec->codecQMFBufferImag[i];
1288
153k
      }
1289
1290
4.14k
      if (flags & SBRDEC_QUAD_RATE) {
1291
655
        if (hFrameData->sbrPatchingMode == 0) {
1292
581
          int *xOverQmf = GetxOverBandQmfTransposer(hSbrDec->hHBE);
1293
1294
          /* in case of harmonic SBR and no HBE_LP map additional buffer for
1295
          one more frame to pointer arry */
1296
19.1k
          for (i = 0; i < hSbrDec->hHBE->noCols / 2; i++) {
1297
18.5k
            pLowBandReal[i + hSbrDec->hHBE->noCols +
1298
18.5k
                         hSbrDec->LppTrans.pSettings->overlap + LPC_ORDER] =
1299
18.5k
                hSbrDec->hQmfHBESlotsReal[i];
1300
18.5k
            pLowBandImag[i + hSbrDec->hHBE->noCols +
1301
18.5k
                         hSbrDec->LppTrans.pSettings->overlap + LPC_ORDER] =
1302
18.5k
                hSbrDec->hQmfHBESlotsImag[i];
1303
18.5k
          }
1304
1305
581
          QmfTransposerApply(
1306
581
              hSbrDec->hHBE,
1307
581
              pLowBandReal + hSbrDec->LppTrans.pSettings->overlap +
1308
581
                  hSbrDec->hHBE->noCols / 2 + LPC_ORDER,
1309
581
              pLowBandImag + hSbrDec->LppTrans.pSettings->overlap +
1310
581
                  hSbrDec->hHBE->noCols / 2 + LPC_ORDER,
1311
581
              hSbrDec->hHBE->noCols, pLowBandReal, pLowBandImag,
1312
581
              hSbrDec->LppTrans.lpcFilterStatesRealHBE,
1313
581
              hSbrDec->LppTrans.lpcFilterStatesImagHBE,
1314
581
              hPrevFrameData->prevSbrPitchInBins, hSbrDec->scale_lb,
1315
581
              hSbrDec->scale_hbe, &hSbrDec->qmfDomainInCh->scaling.hb_scale,
1316
581
              hHeaderData->timeStep, hFrameData->frameInfo.borders[0],
1317
581
              hSbrDec->LppTrans.pSettings->overlap, KEEP_STATES_SYNCED_OUTDIFF);
1318
1319
581
          copyHarmonicSpectrum(
1320
581
              xOverQmf, pLowBandReal, pLowBandImag, hSbrDec->hHBE->noCols,
1321
581
              hSbrDec->LppTrans.pSettings->overlap, KEEP_STATES_SYNCED_OUTDIFF);
1322
581
        }
1323
3.49k
      } else {
1324
        /* in case of harmonic SBR and no HBE_LP map additional buffer for
1325
        one more frame to pointer arry */
1326
115k
        for (i = 0; i < hSbrDec->hHBE->noCols; i++) {
1327
111k
          pLowBandReal[i + hSbrDec->hHBE->noCols +
1328
111k
                       hSbrDec->LppTrans.pSettings->overlap + LPC_ORDER] =
1329
111k
              hSbrDec->hQmfHBESlotsReal[i];
1330
111k
          pLowBandImag[i + hSbrDec->hHBE->noCols +
1331
111k
                       hSbrDec->LppTrans.pSettings->overlap + LPC_ORDER] =
1332
111k
              hSbrDec->hQmfHBESlotsImag[i];
1333
111k
        }
1334
1335
3.49k
        if (hFrameData->sbrPatchingMode == 0) {
1336
2.88k
          QmfTransposerApply(
1337
2.88k
              hSbrDec->hHBE,
1338
2.88k
              pLowBandReal + hSbrDec->LppTrans.pSettings->overlap + LPC_ORDER,
1339
2.88k
              pLowBandImag + hSbrDec->LppTrans.pSettings->overlap + LPC_ORDER,
1340
2.88k
              hSbrDec->hHBE->noCols, pLowBandReal, pLowBandImag,
1341
2.88k
              hSbrDec->LppTrans.lpcFilterStatesRealHBE,
1342
2.88k
              hSbrDec->LppTrans.lpcFilterStatesImagHBE,
1343
2.88k
              0 /* not required for keeping states updated in this frame*/,
1344
2.88k
              hSbrDec->scale_lb, hSbrDec->scale_lb,
1345
2.88k
              &hSbrDec->qmfDomainInCh->scaling.hb_scale, hHeaderData->timeStep,
1346
2.88k
              hFrameData->frameInfo.borders[0],
1347
2.88k
              hSbrDec->LppTrans.pSettings->overlap, KEEP_STATES_SYNCED_NOOUT);
1348
2.88k
        }
1349
1350
3.49k
        QmfTransposerApply(
1351
3.49k
            hSbrDec->hHBE,
1352
3.49k
            pLowBandReal + hSbrDec->LppTrans.pSettings->overlap +
1353
3.49k
                hSbrDec->hHBE->noCols + LPC_ORDER,
1354
3.49k
            pLowBandImag + hSbrDec->LppTrans.pSettings->overlap +
1355
3.49k
                hSbrDec->hHBE->noCols + LPC_ORDER,
1356
3.49k
            hSbrDec->hHBE->noCols, pLowBandReal, pLowBandImag,
1357
3.49k
            hSbrDec->LppTrans.lpcFilterStatesRealHBE,
1358
3.49k
            hSbrDec->LppTrans.lpcFilterStatesImagHBE,
1359
3.49k
            hPrevFrameData->prevSbrPitchInBins, hSbrDec->scale_lb,
1360
3.49k
            hSbrDec->scale_hbe, &hSbrDec->qmfDomainInCh->scaling.hb_scale,
1361
3.49k
            hHeaderData->timeStep, hFrameData->frameInfo.borders[0],
1362
3.49k
            hSbrDec->LppTrans.pSettings->overlap, KEEP_STATES_SYNCED_OUTDIFF);
1363
3.49k
      }
1364
1365
4.14k
      if (hFrameData->sbrPatchingMode == 0) {
1366
27.7k
        for (i = startSlot; i < hSbrDec->LppTrans.pSettings->overlap; i++) {
1367
          /*
1368
          Store the unmodified qmf Slots values for upper part of spectrum
1369
          (required for LPC filtering) required if next frame is a HBE frame
1370
          */
1371
24.3k
          FDKmemcpy(hSbrDec->qmfDomainInCh->hQmfSlotsReal[i],
1372
24.3k
                    hSbrDec->LppTrans.lpcFilterStatesRealHBE[i + LPC_ORDER],
1373
24.3k
                    (64) * sizeof(FIXP_DBL));
1374
24.3k
          FDKmemcpy(hSbrDec->qmfDomainInCh->hQmfSlotsImag[i],
1375
24.3k
                    hSbrDec->LppTrans.lpcFilterStatesImagHBE[i + LPC_ORDER],
1376
24.3k
                    (64) * sizeof(FIXP_DBL));
1377
24.3k
        }
1378
1379
27.7k
        for (i = startSlot; i < hSbrDec->LppTrans.pSettings->overlap; i++) {
1380
          /*
1381
          Store the unmodified qmf Slots values for upper part of spectrum
1382
          (required for LPC filtering) required if next frame is a HBE frame
1383
          */
1384
24.3k
          FDKmemcpy(
1385
24.3k
              hSbrDec->qmfDomainInCh->hQmfSlotsReal[i],
1386
24.3k
              hSbrDec->codecQMFBufferReal[hSbrDec->hHBE->noCols -
1387
24.3k
                                          hSbrDec->LppTrans.pSettings->overlap +
1388
24.3k
                                          i],
1389
24.3k
              new_lsb * sizeof(FIXP_DBL));
1390
24.3k
          FDKmemcpy(
1391
24.3k
              hSbrDec->qmfDomainInCh->hQmfSlotsImag[i],
1392
24.3k
              hSbrDec->codecQMFBufferImag[hSbrDec->hHBE->noCols -
1393
24.3k
                                          hSbrDec->LppTrans.pSettings->overlap +
1394
24.3k
                                          i],
1395
24.3k
              new_lsb * sizeof(FIXP_DBL));
1396
24.3k
        }
1397
3.47k
      }
1398
4.14k
    }
1399
4.14k
  }
1400
1401
2.22M
  {
1402
2.22M
    int adapt_lb = 0, diff = 0,
1403
2.22M
        new_scale = hSbrDec->qmfDomainInCh->scaling.ov_lb_scale;
1404
1405
2.22M
    if ((hSbrDec->qmfDomainInCh->scaling.ov_lb_scale !=
1406
2.22M
         hSbrDec->qmfDomainInCh->scaling.lb_scale) &&
1407
67.8k
        startSlot != 0) {
1408
      /* we need to adapt spectrum to have equal scale factor, always larger
1409
       * than zero */
1410
1.08k
      diff = SCALE2EXP(hSbrDec->qmfDomainInCh->scaling.ov_lb_scale) -
1411
1.08k
             SCALE2EXP(hSbrDec->qmfDomainInCh->scaling.lb_scale);
1412
1413
1.08k
      if (diff > 0) {
1414
1.08k
        adapt_lb = 1;
1415
1.08k
        diff = -diff;
1416
1.08k
        new_scale = hSbrDec->qmfDomainInCh->scaling.ov_lb_scale;
1417
1.08k
      }
1418
1419
1.08k
      stopBand = new_lsb;
1420
1.08k
    }
1421
1422
2.22M
    if (hFrameData->sbrPatchingMode == 1) {
1423
      /* scale states from LegSBR filterstates buffer */
1424
9.08M
      for (i = 0; i < hSbrDec->LppTrans.pSettings->overlap + LPC_ORDER; i++) {
1425
8.01M
        scaleValues(hSbrDec->LppTrans.lpcFilterStatesRealLegSBR[i], new_lsb,
1426
8.01M
                    diff);
1427
8.01M
        if (!useLP) {
1428
7.71M
          scaleValues(hSbrDec->LppTrans.lpcFilterStatesImagLegSBR[i], new_lsb,
1429
7.71M
                      diff);
1430
7.71M
        }
1431
8.01M
      }
1432
1433
1.07M
      if (flags & SBRDEC_SYNTAX_USAC) {
1434
        /* get missing states between old and new x_over from LegSBR
1435
         * filterstates buffer */
1436
        /* in case of legacy SBR we leave these values zeroed out */
1437
6.40M
        for (i = startSlot; i < hSbrDec->LppTrans.pSettings->overlap; i++) {
1438
5.51M
          FDKmemcpy(&OverlapBufferReal[i][old_lsb],
1439
5.51M
                    &hSbrDec->LppTrans
1440
5.51M
                         .lpcFilterStatesRealLegSBR[LPC_ORDER + i][old_lsb],
1441
5.51M
                    fMax(new_lsb - old_lsb, 0) * sizeof(FIXP_DBL));
1442
5.51M
          if (!useLP) {
1443
5.51M
            FDKmemcpy(&OverlapBufferImag[i][old_lsb],
1444
5.51M
                      &hSbrDec->LppTrans
1445
5.51M
                           .lpcFilterStatesImagLegSBR[LPC_ORDER + i][old_lsb],
1446
5.51M
                      fMax(new_lsb - old_lsb, 0) * sizeof(FIXP_DBL));
1447
5.51M
          }
1448
5.51M
        }
1449
889k
      }
1450
1451
1.07M
      if (new_lsb > old_lsb) {
1452
15.8k
        stopBand = old_lsb;
1453
15.8k
      }
1454
1.07M
    }
1455
2.22M
    if ((adapt_lb == 1) && (stopBand > startBand)) {
1456
1.33k
      for (l = startSlot; l < hSbrDec->LppTrans.pSettings->overlap; l++) {
1457
254
        scaleValues(OverlapBufferReal[l] + startBand, stopBand - startBand,
1458
254
                    diff);
1459
254
        if (!useLP) {
1460
232
          scaleValues(OverlapBufferImag[l] + startBand, stopBand - startBand,
1461
232
                      diff);
1462
232
        }
1463
254
      }
1464
1.08k
    }
1465
2.22M
    hSbrDec->qmfDomainInCh->scaling.ov_lb_scale = new_scale;
1466
2.22M
  }
1467
1468
2.22M
  sbrError = ResetLimiterBands(hHeaderData->freqBandData.limiterBandTable,
1469
2.22M
                               &hHeaderData->freqBandData.noLimiterBands,
1470
2.22M
                               hHeaderData->freqBandData.freqBandTable[0],
1471
2.22M
                               hHeaderData->freqBandData.nSfb[0],
1472
2.22M
                               hSbrDec->LppTrans.pSettings->patchParam,
1473
2.22M
                               hSbrDec->LppTrans.pSettings->noOfPatches,
1474
2.22M
                               hHeaderData->bs_data.limiterBands,
1475
2.22M
                               hFrameData->sbrPatchingMode,
1476
2.22M
                               GetxOverBandQmfTransposer(hSbrDec->hHBE),
1477
2.22M
                               Get41SbrQmfTransposer(hSbrDec->hHBE));
1478
1479
2.22M
  hSbrDec->SbrCalculateEnvelope.sbrPatchingMode = hFrameData->sbrPatchingMode;
1480
1481
2.22M
  return sbrError;
1482
2.22M
}