Coverage Report

Created: 2025-11-16 06:35

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/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
40.0k
                                 KEEP_STATES_SYNCED_MODE keepStatesSynced) {
135
40.0k
  int patchBands;
136
40.0k
  int patch, band, col, target, sourceBands, i;
137
40.0k
  int numPatches = 0;
138
40.0k
  int slotOffset = 0;
139
140
40.0k
  FIXP_DBL **ppqmfReal = qmfReal + overlap;
141
40.0k
  FIXP_DBL **ppqmfImag = qmfImag + overlap;
142
143
40.0k
  if (keepStatesSynced == KEEP_STATES_SYNCED_NORMAL) {
144
9.76k
    slotOffset = noCols - overlap - LPC_ORDER;
145
9.76k
  }
146
147
40.0k
  if (keepStatesSynced == KEEP_STATES_SYNCED_OUTDIFF) {
148
7.00k
    ppqmfReal = qmfReal;
149
7.00k
    ppqmfImag = qmfImag;
150
7.00k
  }
151
152
240k
  for (i = 1; i < MAX_NUM_PATCHES; i++) {
153
200k
    if (xOverQmf[i] != 0) {
154
109k
      numPatches++;
155
109k
    }
156
200k
  }
157
158
66.0k
  for (patch = (MAX_STRETCH_HBE - 1); patch < numPatches; patch++) {
159
25.9k
    patchBands = xOverQmf[patch + 1] - xOverQmf[patch];
160
25.9k
    target = xOverQmf[patch];
161
25.9k
    sourceBands = xOverQmf[MAX_STRETCH_HBE - 1] - xOverQmf[MAX_STRETCH_HBE - 2];
162
163
53.7k
    while (patchBands > 0) {
164
27.8k
      int numBands = sourceBands;
165
27.8k
      int startBand = xOverQmf[MAX_STRETCH_HBE - 1] - 1;
166
27.8k
      if (target + numBands >= xOverQmf[patch + 1]) {
167
25.9k
        numBands = xOverQmf[patch + 1] - target;
168
25.9k
      }
169
27.8k
      if ((((target + numBands - 1) % 2) +
170
27.8k
           ((xOverQmf[MAX_STRETCH_HBE - 1] - 1) % 2)) %
171
27.8k
          2) {
172
24.7k
        if (numBands == sourceBands) {
173
1.39k
          numBands--;
174
23.3k
        } else {
175
23.3k
          startBand--;
176
23.3k
        }
177
24.7k
      }
178
27.8k
      if (keepStatesSynced == KEEP_STATES_SYNCED_OUTDIFF) {
179
51.7k
        for (col = slotOffset; col < overlap + LPC_ORDER; col++) {
180
48.3k
          i = 0;
181
375k
          for (band = numBands; band > 0; band--) {
182
327k
            if ((target + band - 1 < 64) &&
183
327k
                (target + band - 1 < xOverQmf[patch + 1])) {
184
327k
              ppqmfReal[col][target + band - 1] = ppqmfReal[col][startBand - i];
185
327k
              ppqmfImag[col][target + band - 1] = ppqmfImag[col][startBand - i];
186
327k
              i++;
187
327k
            }
188
327k
          }
189
48.3k
        }
190
24.3k
      } else {
191
1.30M
        for (col = slotOffset; col < noCols; col++) {
192
1.28M
          i = 0;
193
11.8M
          for (band = numBands; band > 0; band--) {
194
10.6M
            if ((target + band - 1 < 64) &&
195
10.6M
                (target + band - 1 < xOverQmf[patch + 1])) {
196
10.6M
              ppqmfReal[col][target + band - 1] = ppqmfReal[col][startBand - i];
197
10.6M
              ppqmfImag[col][target + band - 1] = ppqmfImag[col][startBand - i];
198
10.6M
              i++;
199
10.6M
            }
200
10.6M
          }
201
1.28M
        }
202
24.3k
      }
203
27.8k
      target += numBands;
204
27.8k
      patchBands -= numBands;
205
27.8k
    }
206
25.9k
  }
207
40.0k
}
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
468k
    const INT sbrInDataHeadroom) {
274
468k
  int i, slot, reserve;
275
468k
  int saveLbScale;
276
468k
  int lastSlotOffs;
277
468k
  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
468k
  LONG *pTimeInQmf = timeIn;
283
284
  /* Number of QMF timeslots in the overlap buffer: */
285
468k
  int ov_len = hSbrDec->LppTrans.pSettings->overlap;
286
287
  /* Number of QMF slots per frame */
288
468k
  int noCols = hHeaderData->numberTimeSlots * hHeaderData->timeStep;
289
290
  /* create pointer array for data to use for HBE and legacy sbr */
291
468k
  FIXP_DBL *pLowBandReal[(3 * 4) + 2 * ((1024) / (32) * (4) / 2)];
292
468k
  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
468k
  FIXP_DBL **pReal = pLowBandReal + ov_len;
296
468k
  FIXP_DBL **pImag = pLowBandImag + ov_len;
297
298
  /* map QMF buffer to pointer array (Overlap + Frame)*/
299
16.8M
  for (i = 0; i < noCols + ov_len; i++) {
300
16.4M
    pLowBandReal[i] = hSbrDec->qmfDomainInCh->hQmfSlotsReal[i];
301
16.4M
    pLowBandImag[i] = hSbrDec->qmfDomainInCh->hQmfSlotsImag[i];
302
16.4M
  }
303
304
468k
  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
4.40M
    for (i = 0; i < noCols; i++) {
308
4.30M
      pLowBandReal[i + noCols + ov_len] = hSbrDec->hQmfHBESlotsReal[i];
309
4.30M
      pLowBandImag[i + noCols + ov_len] = hSbrDec->hQmfHBESlotsImag[i];
310
4.30M
    }
311
312
    /* shift scale values according to buffer */
313
99.3k
    hSbrDec->scale_ov = hSbrDec->scale_lb;
314
99.3k
    hSbrDec->scale_lb = hSbrDec->scale_hbe;
315
316
    /* set pReal to where QMF analysis writes in case of HBE */
317
99.3k
    pReal += noCols;
318
99.3k
    pImag += noCols;
319
99.3k
    if (flags & SBRDEC_SKIP_QMF_ANA) {
320
      /* stereoCfgIndex3 with HBE */
321
34.0k
      FDK_QmfDomain_QmfData2HBE(hSbrDec->qmfDomainInCh,
322
34.0k
                                hSbrDec->hQmfHBESlotsReal,
323
34.0k
                                hSbrDec->hQmfHBESlotsImag);
324
65.2k
    } else {
325
      /* We have to move old hbe frame data to lb area of buffer */
326
2.63M
      for (i = 0; i < noCols; i++) {
327
2.56M
        FDKmemcpy(pLowBandReal[ov_len + i], hSbrDec->hQmfHBESlotsReal[i],
328
2.56M
                  hHeaderData->numberOfAnalysisBands * sizeof(FIXP_DBL));
329
2.56M
        FDKmemcpy(pLowBandImag[ov_len + i], hSbrDec->hQmfHBESlotsImag[i],
330
2.56M
                  hHeaderData->numberOfAnalysisBands * sizeof(FIXP_DBL));
331
2.56M
      }
332
65.2k
    }
333
99.3k
  }
334
335
  /*
336
    low band codec signal subband filtering
337
   */
338
339
468k
  if (flags & SBRDEC_SKIP_QMF_ANA) {
340
36.4k
    if (!(flags & SBRDEC_USAC_HARMONICSBR)) /* stereoCfgIndex3 w/o HBE */
341
2.44k
      FDK_QmfDomain_WorkBuffer2ProcChannel(hSbrDec->qmfDomainInCh);
342
432k
  } else {
343
432k
    C_AALLOC_SCRATCH_START(qmfTemp, FIXP_DBL, 2 * (64));
344
432k
    qmfAnalysisFiltering(&hSbrDec->qmfDomainInCh->fb, pReal, pImag,
345
432k
                         &hSbrDec->qmfDomainInCh->scaling, pTimeInQmf,
346
432k
                         0 + sbrInDataHeadroom, 1, qmfTemp);
347
348
432k
    C_AALLOC_SCRATCH_END(qmfTemp, FIXP_DBL, 2 * (64));
349
432k
  }
350
351
  /*
352
    Clear upper half of spectrum
353
  */
354
468k
  if (!((flags & SBRDEC_USAC_HARMONICSBR) &&
355
408k
        (hFrameData->sbrPatchingMode == 0))) {
356
408k
    int nAnalysisBands = hHeaderData->numberOfAnalysisBands;
357
358
408k
    if (!(flags & SBRDEC_LOW_POWER)) {
359
8.61M
      for (slot = ov_len; slot < noCols + ov_len; slot++) {
360
8.36M
        FDKmemclear(&pLowBandReal[slot][nAnalysisBands],
361
8.36M
                    ((64) - nAnalysisBands) * sizeof(FIXP_DBL));
362
8.36M
        FDKmemclear(&pLowBandImag[slot][nAnalysisBands],
363
8.36M
                    ((64) - nAnalysisBands) * sizeof(FIXP_DBL));
364
8.36M
      }
365
250k
    } else {
366
3.17M
      for (slot = ov_len; slot < noCols + ov_len; slot++) {
367
3.01M
        FDKmemclear(&pLowBandReal[slot][nAnalysisBands],
368
3.01M
                    ((64) - nAnalysisBands) * sizeof(FIXP_DBL));
369
3.01M
      }
370
158k
    }
371
408k
  }
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
468k
  maxVal = maxSubbandSample(pReal, (flags & SBRDEC_LOW_POWER) ? NULL : pImag, 0,
381
468k
                            hSbrDec->qmfDomainInCh->fb.no_channels, 0, noCols);
382
383
468k
  reserve = fixMax(0, CntLeadingZeros(maxVal) - 1);
384
468k
  reserve = fixMin(reserve,
385
468k
                   DFRACT_BITS - 1 - hSbrDec->qmfDomainInCh->scaling.lb_scale);
386
387
  /* If all data is zero, lb_scale could become too large */
388
468k
  rescaleSubbandSamples(pReal, (flags & SBRDEC_LOW_POWER) ? NULL : pImag, 0,
389
468k
                        hSbrDec->qmfDomainInCh->fb.no_channels, 0, noCols,
390
468k
                        reserve);
391
392
468k
  hSbrDec->qmfDomainInCh->scaling.lb_scale += reserve;
393
394
468k
  if ((flags & SBRDEC_USAC_HARMONICSBR)) {
395
    /* actually this is our hbe_scale */
396
99.3k
    hSbrDec->scale_hbe = hSbrDec->qmfDomainInCh->scaling.lb_scale;
397
    /* the real lb_scale is stored in scale_lb from sbr */
398
99.3k
    hSbrDec->qmfDomainInCh->scaling.lb_scale = hSbrDec->scale_lb;
399
99.3k
  }
400
  /*
401
    save low band scale, wavecoding or parametric stereo may modify it
402
  */
403
468k
  saveLbScale = hSbrDec->qmfDomainInCh->scaling.lb_scale;
404
405
468k
  if (applyProcessing) {
406
281k
    UCHAR *borders = hFrameData->frameInfo.borders;
407
281k
    lastSlotOffs = borders[hFrameData->frameInfo.nEnvelopes] -
408
281k
                   hHeaderData->numberTimeSlots;
409
410
281k
    FIXP_DBL degreeAlias[(64)];
411
281k
    PVC_DYNAMIC_DATA pvcDynamicData;
412
281k
    pvcInitFrame(
413
281k
        &hSbrDec->PvcStaticData, &pvcDynamicData,
414
281k
        (hHeaderData->frameErrorFlag ? 0 : hHeaderData->bs_info.pvc_mode),
415
281k
        hFrameData->ns, hHeaderData->timeStep,
416
281k
        hHeaderData->freqBandData.lowSubband,
417
281k
        hFrameData->frameInfo.pvcBorders[0], hFrameData->pvcID);
418
419
281k
    if (!hHeaderData->frameErrorFlag && (hHeaderData->bs_info.pvc_mode > 0)) {
420
85.2k
      pvcDecodeFrame(&hSbrDec->PvcStaticData, &pvcDynamicData, pLowBandReal,
421
85.2k
                     pLowBandImag, ov_len,
422
85.2k
                     SCALE2EXP(hSbrDec->qmfDomainInCh->scaling.ov_lb_scale),
423
85.2k
                     SCALE2EXP(hSbrDec->qmfDomainInCh->scaling.lb_scale));
424
85.2k
    }
425
281k
    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
281k
    if (flags & SBRDEC_LOW_POWER)
431
35.1k
      FDKmemclear(&degreeAlias[hHeaderData->freqBandData.lowSubband],
432
35.1k
                  (hHeaderData->freqBandData.highSubband -
433
35.1k
                   hHeaderData->freqBandData.lowSubband) *
434
35.1k
                      sizeof(FIXP_DBL));
435
436
    /*
437
      Inverse filtering of lowband and transposition into the SBR-frequency
438
      range
439
    */
440
441
281k
    {
442
281k
      KEEP_STATES_SYNCED_MODE keepStatesSyncedMode =
443
281k
          ((flags & SBRDEC_USAC_HARMONICSBR) &&
444
77.6k
           (hFrameData->sbrPatchingMode != 0))
445
281k
              ? KEEP_STATES_SYNCED_NORMAL
446
281k
              : KEEP_STATES_SYNCED_OFF;
447
448
281k
      if (flags & SBRDEC_USAC_HARMONICSBR) {
449
77.6k
        if (flags & SBRDEC_QUAD_RATE) {
450
33.0k
          pReal -= 32;
451
33.0k
          pImag -= 32;
452
33.0k
        }
453
454
77.6k
        if ((hSbrDec->savedStates == 0) && (hFrameData->sbrPatchingMode == 1)) {
455
          /* copy saved states from previous frame to legacy SBR lpc filterstate
456
           * buffer   */
457
220k
          for (i = 0; i < LPC_ORDER + ov_len; i++) {
458
201k
            FDKmemcpy(
459
201k
                hSbrDec->LppTrans.lpcFilterStatesRealLegSBR[i],
460
201k
                hSbrDec->codecQMFBufferReal[noCols - LPC_ORDER - ov_len + i],
461
201k
                hSbrDec->hHBE->noChannels * sizeof(FIXP_DBL));
462
201k
            FDKmemcpy(
463
201k
                hSbrDec->LppTrans.lpcFilterStatesImagLegSBR[i],
464
201k
                hSbrDec->codecQMFBufferImag[noCols - LPC_ORDER - ov_len + i],
465
201k
                hSbrDec->hHBE->noChannels * sizeof(FIXP_DBL));
466
201k
          }
467
18.3k
        }
468
469
        /* saving unmodified QMF states in case we are switching from legacy SBR
470
         * to HBE */
471
3.62M
        for (i = 0; i < hSbrDec->hHBE->noCols; i++) {
472
3.54M
          FDKmemcpy(hSbrDec->codecQMFBufferReal[i], pLowBandReal[ov_len + i],
473
3.54M
                    hSbrDec->hHBE->noChannels * sizeof(FIXP_DBL));
474
3.54M
          FDKmemcpy(hSbrDec->codecQMFBufferImag[i], pLowBandImag[ov_len + i],
475
3.54M
                    hSbrDec->hHBE->noChannels * sizeof(FIXP_DBL));
476
3.54M
        }
477
478
77.6k
        QmfTransposerApply(
479
77.6k
            hSbrDec->hHBE, pReal, pImag, noCols, pLowBandReal, pLowBandImag,
480
77.6k
            hSbrDec->LppTrans.lpcFilterStatesRealHBE,
481
77.6k
            hSbrDec->LppTrans.lpcFilterStatesImagHBE,
482
77.6k
            hFrameData->sbrPitchInBins, hSbrDec->scale_lb, hSbrDec->scale_hbe,
483
77.6k
            &hSbrDec->qmfDomainInCh->scaling.hb_scale, hHeaderData->timeStep,
484
77.6k
            borders[0], ov_len, keepStatesSyncedMode);
485
486
77.6k
        if (flags & SBRDEC_QUAD_RATE) {
487
33.0k
          int *xOverQmf = GetxOverBandQmfTransposer(hSbrDec->hHBE);
488
489
33.0k
          copyHarmonicSpectrum(xOverQmf, pLowBandReal, pLowBandImag, noCols,
490
33.0k
                               ov_len, keepStatesSyncedMode);
491
33.0k
        }
492
77.6k
      }
493
281k
    }
494
495
281k
    if ((flags & SBRDEC_USAC_HARMONICSBR) &&
496
77.6k
        (hFrameData->sbrPatchingMode == 0)) {
497
42.1k
      hSbrDec->prev_frame_lSbr = 0;
498
42.1k
      hSbrDec->prev_frame_hbeSbr = 1;
499
500
42.1k
      lppTransposerHBE(
501
42.1k
          &hSbrDec->LppTrans, hSbrDec->hHBE, &hSbrDec->qmfDomainInCh->scaling,
502
42.1k
          pLowBandReal, pLowBandImag, hHeaderData->timeStep, borders[0],
503
42.1k
          lastSlotOffs, hHeaderData->freqBandData.nInvfBands,
504
42.1k
          hFrameData->sbr_invf_mode, hPrevFrameData->sbr_invf_mode);
505
506
239k
    } else {
507
239k
      if (flags & SBRDEC_USAC_HARMONICSBR) {
508
378k
        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
342k
          FDKmemcpy(hSbrDec->LppTrans.lpcFilterStatesRealHBE[i],
514
342k
                    hSbrDec->qmfDomainInCh
515
342k
                        ->hQmfSlotsReal[hSbrDec->hHBE->noCols - LPC_ORDER + i],
516
342k
                    (64) * sizeof(FIXP_DBL));
517
342k
          FDKmemcpy(hSbrDec->LppTrans.lpcFilterStatesImagHBE[i],
518
342k
                    hSbrDec->qmfDomainInCh
519
342k
                        ->hQmfSlotsImag[hSbrDec->hHBE->noCols - LPC_ORDER + i],
520
342k
                    (64) * sizeof(FIXP_DBL));
521
342k
        }
522
35.5k
      }
523
239k
      {
524
239k
        hSbrDec->prev_frame_lSbr = 1;
525
239k
        hSbrDec->prev_frame_hbeSbr = 0;
526
239k
      }
527
528
239k
      lppTransposer(
529
239k
          &hSbrDec->LppTrans, &hSbrDec->qmfDomainInCh->scaling, pLowBandReal,
530
239k
          degreeAlias,  // only used if useLP = 1
531
239k
          pLowBandImag, flags & SBRDEC_LOW_POWER,
532
239k
          hHeaderData->bs_info.sbr_preprocessing,
533
239k
          hHeaderData->freqBandData.v_k_master[0], hHeaderData->timeStep,
534
239k
          borders[0], lastSlotOffs, hHeaderData->freqBandData.nInvfBands,
535
239k
          hFrameData->sbr_invf_mode, hPrevFrameData->sbr_invf_mode);
536
239k
    }
537
538
    /*
539
      Adjust envelope of current frame.
540
    */
541
542
281k
    if ((hFrameData->sbrPatchingMode !=
543
281k
         hSbrDec->SbrCalculateEnvelope.sbrPatchingMode)) {
544
15.2k
      ResetLimiterBands(hHeaderData->freqBandData.limiterBandTable,
545
15.2k
                        &hHeaderData->freqBandData.noLimiterBands,
546
15.2k
                        hHeaderData->freqBandData.freqBandTable[0],
547
15.2k
                        hHeaderData->freqBandData.nSfb[0],
548
15.2k
                        hSbrDec->LppTrans.pSettings->patchParam,
549
15.2k
                        hSbrDec->LppTrans.pSettings->noOfPatches,
550
15.2k
                        hHeaderData->bs_data.limiterBands,
551
15.2k
                        hFrameData->sbrPatchingMode,
552
15.2k
                        (flags & SBRDEC_USAC_HARMONICSBR) &&
553
15.1k
                                (hFrameData->sbrPatchingMode == 0)
554
15.2k
                            ? GetxOverBandQmfTransposer(hSbrDec->hHBE)
555
15.2k
                            : NULL,
556
15.2k
                        Get41SbrQmfTransposer(hSbrDec->hHBE));
557
558
15.2k
      hSbrDec->SbrCalculateEnvelope.sbrPatchingMode =
559
15.2k
          hFrameData->sbrPatchingMode;
560
15.2k
    }
561
562
281k
    calculateSbrEnvelope(
563
281k
        &hSbrDec->qmfDomainInCh->scaling, &hSbrDec->SbrCalculateEnvelope,
564
281k
        hHeaderData, hFrameData, &pvcDynamicData, pLowBandReal, pLowBandImag,
565
281k
        flags & SBRDEC_LOW_POWER,
566
567
281k
        degreeAlias, flags,
568
281k
        (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
975k
    for (i = 0; i < hHeaderData->freqBandData.nInvfBands; i++) {
591
694k
      hPrevFrameData->sbr_invf_mode[i] = hFrameData->sbr_invf_mode[i];
592
694k
    }
593
281k
    hPrevFrameData->coupling = hFrameData->coupling;
594
281k
    hPrevFrameData->stopPos = borders[hFrameData->frameInfo.nEnvelopes];
595
281k
    hPrevFrameData->ampRes = hFrameData->ampResolutionCurrentFrame;
596
281k
    hPrevFrameData->prevSbrPitchInBins = hFrameData->sbrPitchInBins;
597
    /* could be done in extractFrameInfo_pvc() but hPrevFrameData is not
598
     * available there */
599
281k
    FDKmemcpy(&hPrevFrameData->prevFrameInfo, &hFrameData->frameInfo,
600
281k
              sizeof(FRAME_INFO));
601
281k
  } else {
602
    /* rescale from lsb to nAnalysisBands in order to compensate scaling with
603
     * hb_scale in this area, done by synthesisFiltering*/
604
186k
    int rescale;
605
186k
    int lsb;
606
186k
    int length;
607
608
    /* Reset hb_scale if no highband is present, because hb_scale is considered
609
     * in the QMF-synthesis */
610
186k
    hSbrDec->qmfDomainInCh->scaling.hb_scale = saveLbScale;
611
612
186k
    rescale = hSbrDec->qmfDomainInCh->scaling.hb_scale -
613
186k
              hSbrDec->qmfDomainInCh->scaling.ov_lb_scale;
614
186k
    lsb = hSbrDec->qmfDomainOutCh->fb.lsb;
615
186k
    length = (hSbrDec->qmfDomainInCh->fb.no_channels - lsb);
616
617
186k
    if ((rescale < 0) && (length > 0)) {
618
13.6k
      if (!(flags & SBRDEC_LOW_POWER)) {
619
35.1k
        for (i = 0; i < ov_len; i++) {
620
30.1k
          scaleValues(&pLowBandReal[i][lsb], length, rescale);
621
30.1k
          scaleValues(&pLowBandImag[i][lsb], length, rescale);
622
30.1k
        }
623
8.71k
      } else {
624
10.6k
        for (i = 0; i < ov_len; i++) {
625
1.88k
          scaleValues(&pLowBandReal[i][lsb], length, rescale);
626
1.88k
        }
627
8.71k
      }
628
13.6k
    }
629
186k
  }
630
631
468k
  if (!(flags & SBRDEC_USAC_HARMONICSBR)) {
632
369k
    int length = hSbrDec->qmfDomainInCh->fb.lsb;
633
369k
    if (flags & SBRDEC_SYNTAX_USAC) {
634
202k
      length = hSbrDec->qmfDomainInCh->fb.no_channels;
635
202k
    }
636
637
    /* in case of legacy sbr saving of filter states here */
638
2.60M
    for (i = 0; i < LPC_ORDER + ov_len; i++) {
639
      /*
640
        Store the unmodified qmf Slots values (required for LPC filtering)
641
      */
642
2.23M
      if (!(flags & SBRDEC_LOW_POWER)) {
643
1.68M
        FDKmemcpy(hSbrDec->LppTrans.lpcFilterStatesRealLegSBR[i],
644
1.68M
                  pLowBandReal[noCols - LPC_ORDER + i],
645
1.68M
                  length * sizeof(FIXP_DBL));
646
1.68M
        FDKmemcpy(hSbrDec->LppTrans.lpcFilterStatesImagLegSBR[i],
647
1.68M
                  pLowBandImag[noCols - LPC_ORDER + i],
648
1.68M
                  length * sizeof(FIXP_DBL));
649
1.68M
      } else
650
547k
        FDKmemcpy(hSbrDec->LppTrans.lpcFilterStatesRealLegSBR[i],
651
547k
                  pLowBandReal[noCols - LPC_ORDER + i],
652
547k
                  length * sizeof(FIXP_DBL));
653
2.23M
    }
654
369k
  }
655
656
  /*
657
    Synthesis subband filtering.
658
  */
659
660
468k
  if (!(flags & SBRDEC_PS_DECODED)) {
661
465k
    if (!(flags & SBRDEC_SKIP_QMF_SYN)) {
662
354k
      int outScalefactor = -(8);
663
664
354k
      if (h_ps_d != NULL) {
665
25.0k
        h_ps_d->procFrameBased = 1; /* we here do frame based processing */
666
25.0k
      }
667
668
354k
      sbrDecoder_drcApply(&hSbrDec->sbrDrcChannel, pLowBandReal,
669
354k
                          (flags & SBRDEC_LOW_POWER) ? NULL : pLowBandImag,
670
354k
                          hSbrDec->qmfDomainOutCh->fb.no_col, &outScalefactor);
671
672
354k
      qmfChangeOutScalefactor(&hSbrDec->qmfDomainOutCh->fb, outScalefactor);
673
674
354k
      {
675
354k
        HANDLE_FREQ_BAND_DATA hFreq = &hHeaderData->freqBandData;
676
354k
        int save_usb = hSbrDec->qmfDomainOutCh->fb.usb;
677
678
354k
#if (QMF_MAX_SYNTHESIS_BANDS <= 64)
679
354k
        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
354k
        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
40.0k
          hSbrDec->qmfDomainOutCh->fb.usb =
692
40.0k
              fMin((UINT)hFreq->ov_highSubband,
693
40.0k
                   (UINT)hSbrDec->qmfDomainOutCh->fb.no_channels);
694
40.0k
        }
695
354k
        {
696
354k
          qmfSynthesisFiltering(
697
354k
              &hSbrDec->qmfDomainOutCh->fb, pLowBandReal,
698
354k
              (flags & SBRDEC_LOW_POWER) ? NULL : pLowBandImag,
699
354k
              &hSbrDec->qmfDomainInCh->scaling,
700
354k
              hSbrDec->LppTrans.pSettings->overlap, timeOut, strideOut,
701
354k
              qmfTemp);
702
354k
        }
703
        /* restore saved value */
704
354k
        hSbrDec->qmfDomainOutCh->fb.usb = save_usb;
705
354k
        hFreq->ov_highSubband = save_usb;
706
354k
#if (QMF_MAX_SYNTHESIS_BANDS <= 64)
707
354k
        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
354k
      }
712
354k
    }
713
714
465k
  } else { /* (flags & SBRDEC_PS_DECODED) */
715
3.14k
    INT sdiff;
716
3.14k
    INT scaleFactorHighBand, scaleFactorLowBand_ov, scaleFactorLowBand_no_ov,
717
3.14k
        outScalefactor, outScalefactorR, outScalefactorL;
718
719
3.14k
    HANDLE_QMF_FILTER_BANK synQmf = &hSbrDec->qmfDomainOutCh->fb;
720
3.14k
    HANDLE_QMF_FILTER_BANK synQmfRight = &hSbrDecRight->qmfDomainOutCh->fb;
721
722
    /* adapt scaling */
723
3.14k
    sdiff = hSbrDec->qmfDomainInCh->scaling.lb_scale -
724
3.14k
            reserve; /* Scaling difference */
725
3.14k
    scaleFactorHighBand = sdiff - hSbrDec->qmfDomainInCh->scaling.hb_scale;
726
3.14k
    scaleFactorLowBand_ov = sdiff - hSbrDec->qmfDomainInCh->scaling.ov_lb_scale;
727
3.14k
    scaleFactorLowBand_no_ov = sdiff - hSbrDec->qmfDomainInCh->scaling.lb_scale;
728
729
    /* Scale of low band overlapping QMF data */
730
3.14k
    scaleFactorLowBand_ov =
731
3.14k
        fMin(DFRACT_BITS - 1, fMax(-(DFRACT_BITS - 1), scaleFactorLowBand_ov));
732
    /* Scale of low band current QMF data     */
733
3.14k
    scaleFactorLowBand_no_ov = fMin(
734
3.14k
        DFRACT_BITS - 1, fMax(-(DFRACT_BITS - 1), scaleFactorLowBand_no_ov));
735
    /* Scale of current high band */
736
3.14k
    scaleFactorHighBand =
737
3.14k
        fMin(DFRACT_BITS - 1, fMax(-(DFRACT_BITS - 1), scaleFactorHighBand));
738
739
3.14k
    if (h_ps_d->procFrameBased == 1) /* If we have switched from frame to slot
740
                                        based processing copy filter states */
741
466
    {                                /* 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
466
      FDK_ASSERT(hSbrDec->qmfDomainInCh->pGlobalConf->nBandsSynthesis <=
747
466
                 QMF_MAX_SYNTHESIS_BANDS);
748
466
      synQmfRight->outScalefactor = synQmf->outScalefactor;
749
466
      FDKmemcpy(synQmfRight->FilterStates, synQmf->FilterStates,
750
466
                9 * hSbrDec->qmfDomainInCh->pGlobalConf->nBandsSynthesis *
751
466
                    sizeof(FIXP_QSS));
752
466
    }
753
754
    /* Feed delaylines when parametric stereo is switched on. */
755
3.14k
    PreparePsProcessing(h_ps_d, pLowBandReal, pLowBandImag,
756
3.14k
                        scaleFactorLowBand_ov);
757
758
    /* use the same synthese qmf values for left and right channel */
759
3.14k
    synQmfRight->no_col = synQmf->no_col;
760
3.14k
    synQmfRight->lsb = synQmf->lsb;
761
3.14k
    synQmfRight->usb = synQmf->usb;
762
763
3.14k
    int env = 0;
764
765
3.14k
    {
766
3.14k
#if (QMF_MAX_SYNTHESIS_BANDS <= 64)
767
3.14k
      C_AALLOC_SCRATCH_START(pWorkBuffer, FIXP_DBL,
768
3.14k
                             2 * QMF_MAX_SYNTHESIS_BANDS);
769
#else
770
      C_AALLOC_STACK_START(pWorkBuffer, FIXP_DBL, 2 * QMF_MAX_SYNTHESIS_BANDS);
771
#endif
772
773
3.14k
      int maxShift = 0;
774
775
3.14k
      if (hSbrDec->sbrDrcChannel.enable != 0) {
776
561
        if (hSbrDec->sbrDrcChannel.prevFact_exp > maxShift) {
777
238
          maxShift = hSbrDec->sbrDrcChannel.prevFact_exp;
778
238
        }
779
561
        if (hSbrDec->sbrDrcChannel.currFact_exp > maxShift) {
780
39
          maxShift = hSbrDec->sbrDrcChannel.currFact_exp;
781
39
        }
782
561
        if (hSbrDec->sbrDrcChannel.nextFact_exp > maxShift) {
783
45
          maxShift = hSbrDec->sbrDrcChannel.nextFact_exp;
784
45
        }
785
561
      }
786
787
      /* copy DRC data to right channel (with PS both channels use the same DRC
788
       * gains) */
789
3.14k
      FDKmemcpy(&hSbrDecRight->sbrDrcChannel, &hSbrDec->sbrDrcChannel,
790
3.14k
                sizeof(SBRDEC_DRC_CHANNEL));
791
792
3.14k
      outScalefactor = maxShift - (8);
793
3.14k
      outScalefactorL = outScalefactorR =
794
3.14k
          sbrInDataHeadroom + 1; /* +1: psDiffScale! (MPEG-PS) */
795
796
100k
      for (i = 0; i < synQmf->no_col; i++) { /* ----- no_col loop ----- */
797
798
        /* qmf timeslot of right channel */
799
97.4k
        FIXP_DBL *rQmfReal = pWorkBuffer;
800
97.4k
        FIXP_DBL *rQmfImag = pWorkBuffer + synQmf->no_channels;
801
802
97.4k
        {
803
97.4k
          if (i ==
804
97.4k
              h_ps_d->bsData[h_ps_d->processSlot].mpeg.aEnvStartStop[env]) {
805
7.85k
            initSlotBasedRotation(h_ps_d, env,
806
7.85k
                                  hHeaderData->freqBandData.highSubband);
807
7.85k
            env++;
808
7.85k
          }
809
810
97.4k
          ApplyPsSlot(
811
97.4k
              h_ps_d,             /* parametric stereo decoder handle  */
812
97.4k
              (pLowBandReal + i), /* one timeslot of left/mono channel */
813
97.4k
              (pLowBandImag + i), /* one timeslot of left/mono channel */
814
97.4k
              rQmfReal,           /* one timeslot or right channel     */
815
97.4k
              rQmfImag,           /* one timeslot or right channel     */
816
97.4k
              scaleFactorLowBand_no_ov,
817
97.4k
              (i < hSbrDec->LppTrans.pSettings->overlap)
818
97.4k
                  ? scaleFactorLowBand_ov
819
97.4k
                  : scaleFactorLowBand_no_ov,
820
97.4k
              scaleFactorHighBand, synQmf->lsb, synQmf->usb);
821
97.4k
        }
822
823
97.4k
        sbrDecoder_drcApplySlot(/* right channel */
824
97.4k
                                &hSbrDecRight->sbrDrcChannel, rQmfReal,
825
97.4k
                                rQmfImag, i, synQmfRight->no_col, maxShift);
826
827
97.4k
        sbrDecoder_drcApplySlot(/* left channel */
828
97.4k
                                &hSbrDec->sbrDrcChannel, *(pLowBandReal + i),
829
97.4k
                                *(pLowBandImag + i), i, synQmf->no_col,
830
97.4k
                                maxShift);
831
832
97.4k
        if (!(flags & SBRDEC_SKIP_QMF_SYN)) {
833
97.4k
          qmfChangeOutScalefactor(synQmf, outScalefactor);
834
97.4k
          qmfChangeOutScalefactor(synQmfRight, outScalefactor);
835
836
97.4k
          qmfSynthesisFilteringSlot(
837
97.4k
              synQmfRight, rQmfReal, /* QMF real buffer */
838
97.4k
              rQmfImag,              /* QMF imag buffer */
839
97.4k
              outScalefactorL, outScalefactorL,
840
97.4k
              timeOutRight + (i * synQmf->no_channels * strideOut), strideOut,
841
97.4k
              pWorkBuffer);
842
843
97.4k
          qmfSynthesisFilteringSlot(
844
97.4k
              synQmf, *(pLowBandReal + i), /* QMF real buffer */
845
97.4k
              *(pLowBandImag + i),         /* QMF imag buffer */
846
97.4k
              outScalefactorR, outScalefactorR,
847
97.4k
              timeOut + (i * synQmf->no_channels * strideOut), strideOut,
848
97.4k
              pWorkBuffer);
849
97.4k
        }
850
97.4k
      } /* no_col loop  i  */
851
3.14k
#if (QMF_MAX_SYNTHESIS_BANDS <= 64)
852
3.14k
      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
3.14k
    }
857
3.14k
  }
858
859
468k
  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
468k
  if (!(flags & SBRDEC_SKIP_QMF_SYN)) {
868
357k
    {
869
357k
      FDK_QmfDomain_SaveOverlap(hSbrDec->qmfDomainInCh, 0);
870
357k
      FDK_ASSERT(hSbrDec->qmfDomainInCh->scaling.ov_lb_scale == saveLbScale);
871
357k
    }
872
357k
  }
873
874
468k
  hSbrDec->savedStates = 0;
875
876
  /* Save current frame status */
877
468k
  hPrevFrameData->frameErrorFlag = hHeaderData->frameErrorFlag;
878
468k
  hSbrDec->applySbrProc_old = applyProcessing;
879
880
468k
} /* 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
196k
{
897
196k
  SBR_ERROR err = SBRDEC_OK;
898
196k
  int timeSlots =
899
196k
      hHeaderData->numberTimeSlots; /* Number of SBR slots per frame */
900
196k
  int noCols =
901
196k
      timeSlots * hHeaderData->timeStep; /* Number of QMF slots per frame */
902
196k
  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
196k
  hs->scale_hbe = 15;
909
196k
  hs->scale_lb = 15;
910
196k
  hs->scale_ov = 15;
911
912
196k
  hs->prev_frame_lSbr = 0;
913
196k
  hs->prev_frame_hbeSbr = 0;
914
915
196k
  hs->codecFrameSize = codecFrameSize;
916
917
  /*
918
    create envelope calculator
919
  */
920
196k
  err = createSbrEnvelopeCalc(&hs->SbrCalculateEnvelope, hHeaderData, chan,
921
196k
                              flags);
922
196k
  if (err != SBRDEC_OK) {
923
431
    return err;
924
431
  }
925
926
195k
  initSbrPrevFrameData(&hSbrChannel->prevFrameData, timeSlots);
927
928
  /*
929
    create transposer
930
  */
931
195k
  err = createLppTransposer(
932
195k
      &hs->LppTrans, pSettings, hHeaderData->freqBandData.lowSubband,
933
195k
      hHeaderData->freqBandData.v_k_master, hHeaderData->freqBandData.numMaster,
934
195k
      hHeaderData->freqBandData.highSubband, timeSlots, noCols,
935
195k
      hHeaderData->freqBandData.freqBandTableNoise,
936
195k
      hHeaderData->freqBandData.nNfb, hHeaderData->sbrProcSmplRate, chan,
937
195k
      overlap);
938
195k
  if (err != SBRDEC_OK) {
939
839
    return err;
940
839
  }
941
942
194k
  if (flags & SBRDEC_USAC_HARMONICSBR) {
943
27.0k
    int noChannels, bSbr41 = flags & SBRDEC_QUAD_RATE ? 1 : 0;
944
945
27.0k
    noChannels =
946
27.0k
        QMF_SYNTH_CHANNELS /
947
27.0k
        ((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
27.0k
    hSbrChannel->SbrDec.tmp_memory = (FIXP_DBL **)fdkCallocMatrix2D_aligned(
952
27.0k
        noCols, noChannels, sizeof(FIXP_DBL));
953
27.0k
    if (hSbrChannel->SbrDec.tmp_memory == NULL) {
954
0
      return SBRDEC_MEM_ALLOC_FAILED;
955
0
    }
956
957
27.0k
    hSbrChannel->SbrDec.hQmfHBESlotsReal = hSbrChannel->SbrDec.tmp_memory;
958
27.0k
    hSbrChannel->SbrDec.hQmfHBESlotsImag =
959
27.0k
        (FIXP_DBL **)fdkCallocMatrix2D_aligned(noCols, noChannels,
960
27.0k
                                               sizeof(FIXP_DBL));
961
27.0k
    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
27.0k
    hSbrChannel->SbrDec.codecQMFBufferReal =
970
27.0k
        (FIXP_DBL **)fdkCallocMatrix2D_aligned(noCols, noChannels,
971
27.0k
                                               sizeof(FIXP_DBL));
972
27.0k
    if (hSbrChannel->SbrDec.codecQMFBufferReal == NULL) {
973
0
      return SBRDEC_MEM_ALLOC_FAILED;
974
0
    }
975
976
27.0k
    hSbrChannel->SbrDec.codecQMFBufferImag =
977
27.0k
        (FIXP_DBL **)fdkCallocMatrix2D_aligned(noCols, noChannels,
978
27.0k
                                               sizeof(FIXP_DBL));
979
27.0k
    if (hSbrChannel->SbrDec.codecQMFBufferImag == NULL) {
980
0
      return SBRDEC_MEM_ALLOC_FAILED;
981
0
    }
982
983
27.0k
    err = QmfTransposerCreate(&hs->hHBE, codecFrameSize, 0, bSbr41);
984
27.0k
    if (err != SBRDEC_OK) {
985
0
      return err;
986
0
    }
987
27.0k
  }
988
989
194k
  return err;
990
194k
}
991
992
/*!
993
  \brief     Delete sbr decoder structure
994
  \return    errorCode, 0 if successful
995
*/
996
197k
int deleteSbrDec(SBR_CHANNEL *hSbrChannel) {
997
197k
  HANDLE_SBR_DEC hs = &hSbrChannel->SbrDec;
998
999
197k
  deleteSbrEnvelopeCalc(&hs->SbrCalculateEnvelope);
1000
1001
197k
  if (hs->tmp_memory != NULL) {
1002
27.0k
    FDK_FREE_MEMORY_2D_ALIGNED(hs->tmp_memory);
1003
27.0k
  }
1004
1005
  /* modify here */
1006
197k
  FDK_FREE_MEMORY_2D_ALIGNED(hs->hQmfHBESlotsImag);
1007
1008
197k
  if (hs->hHBE != NULL) QmfTransposerClose(hs->hHBE);
1009
1010
197k
  if (hs->codecQMFBufferReal != NULL) {
1011
27.0k
    FDK_FREE_MEMORY_2D_ALIGNED(hs->codecQMFBufferReal);
1012
27.0k
  }
1013
1014
197k
  if (hs->codecQMFBufferImag != NULL) {
1015
27.0k
    FDK_FREE_MEMORY_2D_ALIGNED(hs->codecQMFBufferImag);
1016
27.0k
  }
1017
1018
197k
  return 0;
1019
197k
}
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
272k
            const UINT flags, HANDLE_SBR_FRAME_DATA hFrameData) {
1029
272k
  SBR_ERROR sbrError = SBRDEC_OK;
1030
272k
  int i;
1031
272k
  FIXP_DBL *pLowBandReal[128];
1032
272k
  FIXP_DBL *pLowBandImag[128];
1033
272k
  int useLP = flags & SBRDEC_LOW_POWER;
1034
1035
272k
  int old_lsb = hSbrDec->qmfDomainInCh->fb.lsb;
1036
272k
  int old_usb = hSbrDec->qmfDomainInCh->fb.usb;
1037
272k
  int new_lsb = hHeaderData->freqBandData.lowSubband;
1038
  /* int new_usb = hHeaderData->freqBandData.highSubband; */
1039
272k
  int l, startBand, stopBand, startSlot, size;
1040
1041
272k
  FIXP_DBL **OverlapBufferReal = hSbrDec->qmfDomainInCh->hQmfSlotsReal;
1042
272k
  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
272k
  int applySbrProc = (hHeaderData->syncState == SBR_ACTIVE ||
1051
272k
                      (hHeaderData->frameErrorFlag == 0 &&
1052
154k
                       hHeaderData->syncState == SBR_HEADER));
1053
272k
  int applySbrProc_old = hSbrDec->applySbrProc_old;
1054
1055
272k
  if (!applySbrProc) {
1056
189k
    new_lsb = (hSbrDec->qmfDomainInCh->fb).no_channels;
1057
189k
  }
1058
272k
  if (!applySbrProc_old) {
1059
202k
    old_lsb = (hSbrDec->qmfDomainInCh->fb).no_channels;
1060
202k
    old_usb = old_lsb;
1061
202k
  }
1062
1063
272k
  resetSbrEnvelopeCalc(&hSbrDec->SbrCalculateEnvelope);
1064
1065
  /* Change lsb and usb */
1066
  /* Synthesis */
1067
272k
  FDK_ASSERT(hSbrDec->qmfDomainOutCh != NULL);
1068
272k
  hSbrDec->qmfDomainOutCh->fb.lsb =
1069
272k
      fixMin((INT)hSbrDec->qmfDomainOutCh->fb.no_channels,
1070
272k
             (INT)hHeaderData->freqBandData.lowSubband);
1071
272k
  hSbrDec->qmfDomainOutCh->fb.usb =
1072
272k
      fixMin((INT)hSbrDec->qmfDomainOutCh->fb.no_channels,
1073
272k
             (INT)hHeaderData->freqBandData.highSubband);
1074
  /* Analysis */
1075
272k
  FDK_ASSERT(hSbrDec->qmfDomainInCh != NULL);
1076
272k
  hSbrDec->qmfDomainInCh->fb.lsb = hSbrDec->qmfDomainOutCh->fb.lsb;
1077
272k
  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
272k
  startBand = old_lsb;
1089
272k
  stopBand = new_lsb;
1090
272k
  startSlot = fMax(0, hHeaderData->timeStep * (hPrevFrameData->stopPos -
1091
272k
                                               hHeaderData->numberTimeSlots));
1092
272k
  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
272k
  if (!(flags & SBRDEC_SYNTAX_USAC)) {
1098
    /* keep already adjusted data in the x-over-area */
1099
147k
    if (!useLP) {
1100
47.9k
      for (l = startSlot; l < hSbrDec->LppTrans.pSettings->overlap; l++) {
1101
38.6k
        FDKmemclear(&OverlapBufferReal[l][startBand], size * sizeof(FIXP_DBL));
1102
38.6k
        FDKmemclear(&OverlapBufferImag[l][startBand], size * sizeof(FIXP_DBL));
1103
38.6k
      }
1104
137k
    } else {
1105
286k
      for (l = startSlot; l < hSbrDec->LppTrans.pSettings->overlap; l++) {
1106
148k
        FDKmemclear(&OverlapBufferReal[l][startBand], size * sizeof(FIXP_DBL));
1107
148k
      }
1108
137k
    }
1109
1110
    /*
1111
    reset LPC filter states
1112
    */
1113
147k
    startBand = fixMin(old_lsb, new_lsb);
1114
147k
    stopBand = fixMax(old_lsb, new_lsb);
1115
147k
    size = fixMax(0, stopBand - startBand);
1116
1117
147k
    FDKmemclear(&hSbrDec->LppTrans.lpcFilterStatesRealLegSBR[0][startBand],
1118
147k
                size * sizeof(FIXP_DBL));
1119
147k
    FDKmemclear(&hSbrDec->LppTrans.lpcFilterStatesRealLegSBR[1][startBand],
1120
147k
                size * sizeof(FIXP_DBL));
1121
147k
    if (!useLP) {
1122
9.33k
      FDKmemclear(&hSbrDec->LppTrans.lpcFilterStatesImagLegSBR[0][startBand],
1123
9.33k
                  size * sizeof(FIXP_DBL));
1124
9.33k
      FDKmemclear(&hSbrDec->LppTrans.lpcFilterStatesImagLegSBR[1][startBand],
1125
9.33k
                  size * sizeof(FIXP_DBL));
1126
9.33k
    }
1127
147k
  }
1128
1129
272k
  if (startSlot != 0) {
1130
27.0k
    int source_exp, target_exp, delta_exp, target_lsb, target_usb, reserve;
1131
27.0k
    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
27.0k
    if (new_lsb > old_lsb) {
1159
      /* case 1 and 3 */
1160
8.43k
      source_exp = SCALE2EXP(hSbrDec->qmfDomainInCh->scaling.ov_hb_scale);
1161
8.43k
      target_exp = SCALE2EXP(hSbrDec->qmfDomainInCh->scaling.ov_lb_scale);
1162
1163
8.43k
      startBand = old_lsb;
1164
1165
8.43k
      if (new_lsb >= old_usb) {
1166
        /* case 1 */
1167
1.61k
        stopBand = old_usb;
1168
6.82k
      } else {
1169
        /* case 3 */
1170
6.82k
        stopBand = new_lsb;
1171
6.82k
      }
1172
1173
8.43k
      target_lsb = 0;
1174
8.43k
      target_usb = old_lsb;
1175
18.6k
    } else {
1176
      /* case 2 and 4 */
1177
18.6k
      source_exp = SCALE2EXP(hSbrDec->qmfDomainInCh->scaling.ov_lb_scale);
1178
18.6k
      target_exp = SCALE2EXP(hSbrDec->qmfDomainInCh->scaling.ov_hb_scale);
1179
1180
18.6k
      startBand = new_lsb;
1181
18.6k
      stopBand = old_lsb;
1182
1183
18.6k
      target_lsb = old_lsb;
1184
18.6k
      target_usb = old_usb;
1185
18.6k
    }
1186
1187
27.0k
    maxVal =
1188
27.0k
        maxSubbandSample(OverlapBufferReal, (useLP) ? NULL : OverlapBufferImag,
1189
27.0k
                         startBand, stopBand, 0, startSlot);
1190
1191
27.0k
    reserve = ((LONG)maxVal != 0 ? CntLeadingZeros(maxVal) - 1 : 0);
1192
27.0k
    reserve = fixMin(
1193
27.0k
        reserve,
1194
27.0k
        DFRACT_BITS - 1 -
1195
27.0k
            EXP2SCALE(
1196
27.0k
                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
27.0k
    if (target_exp - (source_exp - reserve) >= 0) {
1202
17.1k
      rescaleSubbandSamples(OverlapBufferReal,
1203
17.1k
                            (useLP) ? NULL : OverlapBufferImag, startBand,
1204
17.1k
                            stopBand, 0, startSlot, reserve);
1205
17.1k
      source_exp -= reserve;
1206
17.1k
    }
1207
1208
27.0k
    delta_exp = target_exp - source_exp;
1209
1210
27.0k
    if (delta_exp < 0) { /* x-over-area is dominant */
1211
9.93k
      startBand = target_lsb;
1212
9.93k
      stopBand = target_usb;
1213
9.93k
      delta_exp = -delta_exp;
1214
1215
9.93k
      if (new_lsb > old_lsb) {
1216
        /* The lowband has to be rescaled */
1217
2.85k
        hSbrDec->qmfDomainInCh->scaling.ov_lb_scale = EXP2SCALE(source_exp);
1218
7.08k
      } else {
1219
        /* The highband has to be rescaled */
1220
7.08k
        hSbrDec->qmfDomainInCh->scaling.ov_hb_scale = EXP2SCALE(source_exp);
1221
7.08k
      }
1222
9.93k
    }
1223
1224
27.0k
    FDK_ASSERT(startBand <= stopBand);
1225
1226
27.0k
    if (!useLP) {
1227
149k
      for (l = 0; l < startSlot; l++) {
1228
124k
        scaleValues(OverlapBufferReal[l] + startBand, stopBand - startBand,
1229
124k
                    -delta_exp);
1230
124k
        scaleValues(OverlapBufferImag[l] + startBand, stopBand - startBand,
1231
124k
                    -delta_exp);
1232
124k
      }
1233
25.0k
    } else
1234
9.98k
      for (l = 0; l < startSlot; l++) {
1235
7.99k
        scaleValues(OverlapBufferReal[l] + startBand, stopBand - startBand,
1236
7.99k
                    -delta_exp);
1237
7.99k
      }
1238
27.0k
  } /* startSlot != 0 */
1239
1240
  /*
1241
    Initialize transposer and limiter
1242
  */
1243
272k
  sbrError = resetLppTransposer(
1244
272k
      &hSbrDec->LppTrans, hHeaderData->freqBandData.lowSubband,
1245
272k
      hHeaderData->freqBandData.v_k_master, hHeaderData->freqBandData.numMaster,
1246
272k
      hHeaderData->freqBandData.freqBandTableNoise,
1247
272k
      hHeaderData->freqBandData.nNfb, hHeaderData->freqBandData.highSubband,
1248
272k
      hHeaderData->sbrProcSmplRate);
1249
272k
  if (sbrError != SBRDEC_OK) return sbrError;
1250
1251
272k
  hSbrDec->savedStates = 0;
1252
1253
272k
  if ((flags & SBRDEC_USAC_HARMONICSBR) && applySbrProc) {
1254
34.9k
    sbrError = QmfTransposerReInit(hSbrDec->hHBE,
1255
34.9k
                                   hHeaderData->freqBandData.freqBandTable,
1256
34.9k
                                   hHeaderData->freqBandData.nSfb);
1257
34.9k
    if (sbrError != SBRDEC_OK) return sbrError;
1258
1259
    /* copy saved states from previous frame to legacy SBR lpc filterstate
1260
     * buffer   */
1261
359k
    for (i = 0; i < LPC_ORDER + hSbrDec->LppTrans.pSettings->overlap; i++) {
1262
325k
      FDKmemcpy(
1263
325k
          hSbrDec->LppTrans.lpcFilterStatesRealLegSBR[i],
1264
325k
          hSbrDec->codecQMFBufferReal[hSbrDec->hHBE->noCols - LPC_ORDER -
1265
325k
                                      hSbrDec->LppTrans.pSettings->overlap + i],
1266
325k
          hSbrDec->hHBE->noChannels * sizeof(FIXP_DBL));
1267
325k
      FDKmemcpy(
1268
325k
          hSbrDec->LppTrans.lpcFilterStatesImagLegSBR[i],
1269
325k
          hSbrDec->codecQMFBufferImag[hSbrDec->hHBE->noCols - LPC_ORDER -
1270
325k
                                      hSbrDec->LppTrans.pSettings->overlap + i],
1271
325k
          hSbrDec->hHBE->noChannels * sizeof(FIXP_DBL));
1272
325k
    }
1273
34.9k
    hSbrDec->savedStates = 1;
1274
1275
34.9k
    {
1276
      /* map QMF buffer to pointer array (Overlap + Frame)*/
1277
359k
      for (i = 0; i < hSbrDec->LppTrans.pSettings->overlap + LPC_ORDER; i++) {
1278
325k
        pLowBandReal[i] = hSbrDec->LppTrans.lpcFilterStatesRealHBE[i];
1279
325k
        pLowBandImag[i] = hSbrDec->LppTrans.lpcFilterStatesImagHBE[i];
1280
325k
      }
1281
1282
      /* map QMF buffer to pointer array (Overlap + Frame)*/
1283
1.39M
      for (i = 0; i < hSbrDec->hHBE->noCols; i++) {
1284
1.36M
        pLowBandReal[i + hSbrDec->LppTrans.pSettings->overlap + LPC_ORDER] =
1285
1.36M
            hSbrDec->codecQMFBufferReal[i];
1286
1.36M
        pLowBandImag[i + hSbrDec->LppTrans.pSettings->overlap + LPC_ORDER] =
1287
1.36M
            hSbrDec->codecQMFBufferImag[i];
1288
1.36M
      }
1289
1290
34.9k
      if (flags & SBRDEC_QUAD_RATE) {
1291
7.55k
        if (hFrameData->sbrPatchingMode == 0) {
1292
7.00k
          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
231k
          for (i = 0; i < hSbrDec->hHBE->noCols / 2; i++) {
1297
224k
            pLowBandReal[i + hSbrDec->hHBE->noCols +
1298
224k
                         hSbrDec->LppTrans.pSettings->overlap + LPC_ORDER] =
1299
224k
                hSbrDec->hQmfHBESlotsReal[i];
1300
224k
            pLowBandImag[i + hSbrDec->hHBE->noCols +
1301
224k
                         hSbrDec->LppTrans.pSettings->overlap + LPC_ORDER] =
1302
224k
                hSbrDec->hQmfHBESlotsImag[i];
1303
224k
          }
1304
1305
7.00k
          QmfTransposerApply(
1306
7.00k
              hSbrDec->hHBE,
1307
7.00k
              pLowBandReal + hSbrDec->LppTrans.pSettings->overlap +
1308
7.00k
                  hSbrDec->hHBE->noCols / 2 + LPC_ORDER,
1309
7.00k
              pLowBandImag + hSbrDec->LppTrans.pSettings->overlap +
1310
7.00k
                  hSbrDec->hHBE->noCols / 2 + LPC_ORDER,
1311
7.00k
              hSbrDec->hHBE->noCols, pLowBandReal, pLowBandImag,
1312
7.00k
              hSbrDec->LppTrans.lpcFilterStatesRealHBE,
1313
7.00k
              hSbrDec->LppTrans.lpcFilterStatesImagHBE,
1314
7.00k
              hPrevFrameData->prevSbrPitchInBins, hSbrDec->scale_lb,
1315
7.00k
              hSbrDec->scale_hbe, &hSbrDec->qmfDomainInCh->scaling.hb_scale,
1316
7.00k
              hHeaderData->timeStep, hFrameData->frameInfo.borders[0],
1317
7.00k
              hSbrDec->LppTrans.pSettings->overlap, KEEP_STATES_SYNCED_OUTDIFF);
1318
1319
7.00k
          copyHarmonicSpectrum(
1320
7.00k
              xOverQmf, pLowBandReal, pLowBandImag, hSbrDec->hHBE->noCols,
1321
7.00k
              hSbrDec->LppTrans.pSettings->overlap, KEEP_STATES_SYNCED_OUTDIFF);
1322
7.00k
        }
1323
27.4k
      } else {
1324
        /* in case of harmonic SBR and no HBE_LP map additional buffer for
1325
        one more frame to pointer arry */
1326
904k
        for (i = 0; i < hSbrDec->hHBE->noCols; i++) {
1327
876k
          pLowBandReal[i + hSbrDec->hHBE->noCols +
1328
876k
                       hSbrDec->LppTrans.pSettings->overlap + LPC_ORDER] =
1329
876k
              hSbrDec->hQmfHBESlotsReal[i];
1330
876k
          pLowBandImag[i + hSbrDec->hHBE->noCols +
1331
876k
                       hSbrDec->LppTrans.pSettings->overlap + LPC_ORDER] =
1332
876k
              hSbrDec->hQmfHBESlotsImag[i];
1333
876k
        }
1334
1335
27.4k
        if (hFrameData->sbrPatchingMode == 0) {
1336
10.7k
          QmfTransposerApply(
1337
10.7k
              hSbrDec->hHBE,
1338
10.7k
              pLowBandReal + hSbrDec->LppTrans.pSettings->overlap + LPC_ORDER,
1339
10.7k
              pLowBandImag + hSbrDec->LppTrans.pSettings->overlap + LPC_ORDER,
1340
10.7k
              hSbrDec->hHBE->noCols, pLowBandReal, pLowBandImag,
1341
10.7k
              hSbrDec->LppTrans.lpcFilterStatesRealHBE,
1342
10.7k
              hSbrDec->LppTrans.lpcFilterStatesImagHBE,
1343
10.7k
              0 /* not required for keeping states updated in this frame*/,
1344
10.7k
              hSbrDec->scale_lb, hSbrDec->scale_lb,
1345
10.7k
              &hSbrDec->qmfDomainInCh->scaling.hb_scale, hHeaderData->timeStep,
1346
10.7k
              hFrameData->frameInfo.borders[0],
1347
10.7k
              hSbrDec->LppTrans.pSettings->overlap, KEEP_STATES_SYNCED_NOOUT);
1348
10.7k
        }
1349
1350
27.4k
        QmfTransposerApply(
1351
27.4k
            hSbrDec->hHBE,
1352
27.4k
            pLowBandReal + hSbrDec->LppTrans.pSettings->overlap +
1353
27.4k
                hSbrDec->hHBE->noCols + LPC_ORDER,
1354
27.4k
            pLowBandImag + hSbrDec->LppTrans.pSettings->overlap +
1355
27.4k
                hSbrDec->hHBE->noCols + LPC_ORDER,
1356
27.4k
            hSbrDec->hHBE->noCols, pLowBandReal, pLowBandImag,
1357
27.4k
            hSbrDec->LppTrans.lpcFilterStatesRealHBE,
1358
27.4k
            hSbrDec->LppTrans.lpcFilterStatesImagHBE,
1359
27.4k
            hPrevFrameData->prevSbrPitchInBins, hSbrDec->scale_lb,
1360
27.4k
            hSbrDec->scale_hbe, &hSbrDec->qmfDomainInCh->scaling.hb_scale,
1361
27.4k
            hHeaderData->timeStep, hFrameData->frameInfo.borders[0],
1362
27.4k
            hSbrDec->LppTrans.pSettings->overlap, KEEP_STATES_SYNCED_OUTDIFF);
1363
27.4k
      }
1364
1365
34.9k
      if (hFrameData->sbrPatchingMode == 0) {
1366
143k
        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
125k
          FDKmemcpy(hSbrDec->qmfDomainInCh->hQmfSlotsReal[i],
1372
125k
                    hSbrDec->LppTrans.lpcFilterStatesRealHBE[i + LPC_ORDER],
1373
125k
                    (64) * sizeof(FIXP_DBL));
1374
125k
          FDKmemcpy(hSbrDec->qmfDomainInCh->hQmfSlotsImag[i],
1375
125k
                    hSbrDec->LppTrans.lpcFilterStatesImagHBE[i + LPC_ORDER],
1376
125k
                    (64) * sizeof(FIXP_DBL));
1377
125k
        }
1378
1379
143k
        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
125k
          FDKmemcpy(
1385
125k
              hSbrDec->qmfDomainInCh->hQmfSlotsReal[i],
1386
125k
              hSbrDec->codecQMFBufferReal[hSbrDec->hHBE->noCols -
1387
125k
                                          hSbrDec->LppTrans.pSettings->overlap +
1388
125k
                                          i],
1389
125k
              new_lsb * sizeof(FIXP_DBL));
1390
125k
          FDKmemcpy(
1391
125k
              hSbrDec->qmfDomainInCh->hQmfSlotsImag[i],
1392
125k
              hSbrDec->codecQMFBufferImag[hSbrDec->hHBE->noCols -
1393
125k
                                          hSbrDec->LppTrans.pSettings->overlap +
1394
125k
                                          i],
1395
125k
              new_lsb * sizeof(FIXP_DBL));
1396
125k
        }
1397
17.7k
      }
1398
34.9k
    }
1399
34.9k
  }
1400
1401
272k
  {
1402
272k
    int adapt_lb = 0, diff = 0,
1403
272k
        new_scale = hSbrDec->qmfDomainInCh->scaling.ov_lb_scale;
1404
1405
272k
    if ((hSbrDec->qmfDomainInCh->scaling.ov_lb_scale !=
1406
272k
         hSbrDec->qmfDomainInCh->scaling.lb_scale) &&
1407
23.0k
        startSlot != 0) {
1408
      /* we need to adapt spectrum to have equal scale factor, always larger
1409
       * than zero */
1410
3.23k
      diff = SCALE2EXP(hSbrDec->qmfDomainInCh->scaling.ov_lb_scale) -
1411
3.23k
             SCALE2EXP(hSbrDec->qmfDomainInCh->scaling.lb_scale);
1412
1413
3.23k
      if (diff > 0) {
1414
2.55k
        adapt_lb = 1;
1415
2.55k
        diff = -diff;
1416
2.55k
        new_scale = hSbrDec->qmfDomainInCh->scaling.ov_lb_scale;
1417
2.55k
      }
1418
1419
3.23k
      stopBand = new_lsb;
1420
3.23k
    }
1421
1422
272k
    if (hFrameData->sbrPatchingMode == 1) {
1423
      /* scale states from LegSBR filterstates buffer */
1424
819k
      for (i = 0; i < hSbrDec->LppTrans.pSettings->overlap + LPC_ORDER; i++) {
1425
713k
        scaleValues(hSbrDec->LppTrans.lpcFilterStatesRealLegSBR[i], new_lsb,
1426
713k
                    diff);
1427
713k
        if (!useLP) {
1428
557k
          scaleValues(hSbrDec->LppTrans.lpcFilterStatesImagLegSBR[i], new_lsb,
1429
557k
                      diff);
1430
557k
        }
1431
713k
      }
1432
1433
105k
      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
378k
        for (i = startSlot; i < hSbrDec->LppTrans.pSettings->overlap; i++) {
1438
310k
          FDKmemcpy(&OverlapBufferReal[i][old_lsb],
1439
310k
                    &hSbrDec->LppTrans
1440
310k
                         .lpcFilterStatesRealLegSBR[LPC_ORDER + i][old_lsb],
1441
310k
                    fMax(new_lsb - old_lsb, 0) * sizeof(FIXP_DBL));
1442
310k
          if (!useLP) {
1443
310k
            FDKmemcpy(&OverlapBufferImag[i][old_lsb],
1444
310k
                      &hSbrDec->LppTrans
1445
310k
                           .lpcFilterStatesImagLegSBR[LPC_ORDER + i][old_lsb],
1446
310k
                      fMax(new_lsb - old_lsb, 0) * sizeof(FIXP_DBL));
1447
310k
          }
1448
310k
        }
1449
67.3k
      }
1450
1451
105k
      if (new_lsb > old_lsb) {
1452
36.0k
        stopBand = old_lsb;
1453
36.0k
      }
1454
105k
    }
1455
272k
    if ((adapt_lb == 1) && (stopBand > startBand)) {
1456
6.61k
      for (l = startSlot; l < hSbrDec->LppTrans.pSettings->overlap; l++) {
1457
4.08k
        scaleValues(OverlapBufferReal[l] + startBand, stopBand - startBand,
1458
4.08k
                    diff);
1459
4.08k
        if (!useLP) {
1460
552
          scaleValues(OverlapBufferImag[l] + startBand, stopBand - startBand,
1461
552
                      diff);
1462
552
        }
1463
4.08k
      }
1464
2.53k
    }
1465
272k
    hSbrDec->qmfDomainInCh->scaling.ov_lb_scale = new_scale;
1466
272k
  }
1467
1468
272k
  sbrError = ResetLimiterBands(hHeaderData->freqBandData.limiterBandTable,
1469
272k
                               &hHeaderData->freqBandData.noLimiterBands,
1470
272k
                               hHeaderData->freqBandData.freqBandTable[0],
1471
272k
                               hHeaderData->freqBandData.nSfb[0],
1472
272k
                               hSbrDec->LppTrans.pSettings->patchParam,
1473
272k
                               hSbrDec->LppTrans.pSettings->noOfPatches,
1474
272k
                               hHeaderData->bs_data.limiterBands,
1475
272k
                               hFrameData->sbrPatchingMode,
1476
272k
                               GetxOverBandQmfTransposer(hSbrDec->hHBE),
1477
272k
                               Get41SbrQmfTransposer(hSbrDec->hHBE));
1478
1479
272k
  hSbrDec->SbrCalculateEnvelope.sbrPatchingMode = hFrameData->sbrPatchingMode;
1480
1481
272k
  return sbrError;
1482
272k
}