• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* -----------------------------------------------------------------------------
2 Software License for The Fraunhofer FDK AAC Codec Library for Android
3 
4 © Copyright  1995 - 2019 Fraunhofer-Gesellschaft zur Förderung der angewandten
5 Forschung e.V. All rights reserved.
6 
7  1.    INTRODUCTION
8 The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software
9 that implements the MPEG Advanced Audio Coding ("AAC") encoding and decoding
10 scheme for digital audio. This FDK AAC Codec software is intended to be used on
11 a wide variety of Android devices.
12 
13 AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient
14 general perceptual audio codecs. AAC-ELD is considered the best-performing
15 full-bandwidth communications codec by independent studies and is widely
16 deployed. AAC has been standardized by ISO and IEC as part of the MPEG
17 specifications.
18 
19 Patent licenses for necessary patent claims for the FDK AAC Codec (including
20 those of Fraunhofer) may be obtained through Via Licensing
21 (www.vialicensing.com) or through the respective patent owners individually for
22 the purpose of encoding or decoding bit streams in products that are compliant
23 with the ISO/IEC MPEG audio standards. Please note that most manufacturers of
24 Android devices already license these patent claims through Via Licensing or
25 directly from the patent owners, and therefore FDK AAC Codec software may
26 already be covered under those patent licenses when it is used for those
27 licensed purposes only.
28 
29 Commercially-licensed AAC software libraries, including floating-point versions
30 with enhanced sound quality, are also available from Fraunhofer. Users are
31 encouraged to check the Fraunhofer website for additional applications
32 information and documentation.
33 
34 2.    COPYRIGHT LICENSE
35 
36 Redistribution and use in source and binary forms, with or without modification,
37 are permitted without payment of copyright license fees provided that you
38 satisfy the following conditions:
39 
40 You must retain the complete text of this software license in redistributions of
41 the FDK AAC Codec or your modifications thereto in source code form.
42 
43 You must retain the complete text of this software license in the documentation
44 and/or other materials provided with redistributions of the FDK AAC Codec or
45 your modifications thereto in binary form. You must make available free of
46 charge copies of the complete source code of the FDK AAC Codec and your
47 modifications thereto to recipients of copies in binary form.
48 
49 The name of Fraunhofer may not be used to endorse or promote products derived
50 from this library without prior written permission.
51 
52 You may not charge copyright license fees for anyone to use, copy or distribute
53 the FDK AAC Codec software or your modifications thereto.
54 
55 Your modified versions of the FDK AAC Codec must carry prominent notices stating
56 that you changed the software and the date of any change. For modified versions
57 of the FDK AAC Codec, the term "Fraunhofer FDK AAC Codec Library for Android"
58 must be replaced by the term "Third-Party Modified Version of the Fraunhofer FDK
59 AAC Codec Library for Android."
60 
61 3.    NO PATENT LICENSE
62 
63 NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without
64 limitation the patents of Fraunhofer, ARE GRANTED BY THIS SOFTWARE LICENSE.
65 Fraunhofer provides no warranty of patent non-infringement with respect to this
66 software.
67 
68 You may use this FDK AAC Codec software or modifications thereto only for
69 purposes that are authorized by appropriate patent licenses.
70 
71 4.    DISCLAIMER
72 
73 This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright
74 holders and contributors "AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES,
75 including but not limited to the implied warranties of merchantability and
76 fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
77 CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary,
78 or consequential damages, including but not limited to procurement of substitute
79 goods or services; loss of use, data, or profits, or business interruption,
80 however caused and on any theory of liability, whether in contract, strict
81 liability, or tort (including negligence), arising in any way out of the use of
82 this software, even if advised of the possibility of such damage.
83 
84 5.    CONTACT INFORMATION
85 
86 Fraunhofer Institute for Integrated Circuits IIS
87 Attention: Audio and Multimedia Departments - FDK AAC LL
88 Am Wolfsmantel 33
89 91058 Erlangen, Germany
90 
91 www.iis.fraunhofer.de/amm
92 amm-info@iis.fraunhofer.de
93 ----------------------------------------------------------------------------- */
94 
95 /**************************** 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 
copyHarmonicSpectrum(int * xOverQmf,FIXP_DBL ** qmfReal,FIXP_DBL ** qmfImag,int noCols,int overlap,KEEP_STATES_SYNCED_MODE keepStatesSynced)132 static void copyHarmonicSpectrum(int *xOverQmf, FIXP_DBL **qmfReal,
133                                  FIXP_DBL **qmfImag, int noCols, int overlap,
134                                  KEEP_STATES_SYNCED_MODE keepStatesSynced) {
135   int patchBands;
136   int patch, band, col, target, sourceBands, i;
137   int numPatches = 0;
138   int slotOffset = 0;
139 
140   FIXP_DBL **ppqmfReal = qmfReal + overlap;
141   FIXP_DBL **ppqmfImag = qmfImag + overlap;
142 
143   if (keepStatesSynced == KEEP_STATES_SYNCED_NORMAL) {
144     slotOffset = noCols - overlap - LPC_ORDER;
145   }
146 
147   if (keepStatesSynced == KEEP_STATES_SYNCED_OUTDIFF) {
148     ppqmfReal = qmfReal;
149     ppqmfImag = qmfImag;
150   }
151 
152   for (i = 1; i < MAX_NUM_PATCHES; i++) {
153     if (xOverQmf[i] != 0) {
154       numPatches++;
155     }
156   }
157 
158   for (patch = (MAX_STRETCH_HBE - 1); patch < numPatches; patch++) {
159     patchBands = xOverQmf[patch + 1] - xOverQmf[patch];
160     target = xOverQmf[patch];
161     sourceBands = xOverQmf[MAX_STRETCH_HBE - 1] - xOverQmf[MAX_STRETCH_HBE - 2];
162 
163     while (patchBands > 0) {
164       int numBands = sourceBands;
165       int startBand = xOverQmf[MAX_STRETCH_HBE - 1] - 1;
166       if (target + numBands >= xOverQmf[patch + 1]) {
167         numBands = xOverQmf[patch + 1] - target;
168       }
169       if ((((target + numBands - 1) % 2) +
170            ((xOverQmf[MAX_STRETCH_HBE - 1] - 1) % 2)) %
171           2) {
172         if (numBands == sourceBands) {
173           numBands--;
174         } else {
175           startBand--;
176         }
177       }
178       if (keepStatesSynced == KEEP_STATES_SYNCED_OUTDIFF) {
179         for (col = slotOffset; col < overlap + LPC_ORDER; col++) {
180           i = 0;
181           for (band = numBands; band > 0; band--) {
182             if ((target + band - 1 < 64) &&
183                 (target + band - 1 < xOverQmf[patch + 1])) {
184               ppqmfReal[col][target + band - 1] = ppqmfReal[col][startBand - i];
185               ppqmfImag[col][target + band - 1] = ppqmfImag[col][startBand - i];
186               i++;
187             }
188           }
189         }
190       } else {
191         for (col = slotOffset; col < noCols; col++) {
192           i = 0;
193           for (band = numBands; band > 0; band--) {
194             if ((target + band - 1 < 64) &&
195                 (target + band - 1 < xOverQmf[patch + 1])) {
196               ppqmfReal[col][target + band - 1] = ppqmfReal[col][startBand - i];
197               ppqmfImag[col][target + band - 1] = ppqmfImag[col][startBand - i];
198               i++;
199             }
200           }
201         }
202       }
203       target += numBands;
204       patchBands -= numBands;
205     }
206   }
207 }
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 
sbr_dec(HANDLE_SBR_DEC hSbrDec,LONG * timeIn,LONG * timeOut,HANDLE_SBR_DEC hSbrDecRight,LONG * timeOutRight,const int strideOut,HANDLE_SBR_HEADER_DATA hHeaderData,HANDLE_SBR_FRAME_DATA hFrameData,HANDLE_SBR_PREV_FRAME_DATA hPrevFrameData,const int applyProcessing,HANDLE_PS_DEC h_ps_d,const UINT flags,const int codecFrameSize,const INT sbrInDataHeadroom)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     const INT sbrInDataHeadroom) {
274   int i, slot, reserve;
275   int saveLbScale;
276   int lastSlotOffs;
277   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   LONG *pTimeInQmf = timeIn;
283 
284   /* Number of QMF timeslots in the overlap buffer: */
285   int ov_len = hSbrDec->LppTrans.pSettings->overlap;
286 
287   /* Number of QMF slots per frame */
288   int noCols = hHeaderData->numberTimeSlots * hHeaderData->timeStep;
289 
290   /* create pointer array for data to use for HBE and legacy sbr */
291   FIXP_DBL *pLowBandReal[(3 * 4) + 2 * ((1024) / (32) * (4) / 2)];
292   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   FIXP_DBL **pReal = pLowBandReal + ov_len;
296   FIXP_DBL **pImag = pLowBandImag + ov_len;
297 
298   /* map QMF buffer to pointer array (Overlap + Frame)*/
299   for (i = 0; i < noCols + ov_len; i++) {
300     pLowBandReal[i] = hSbrDec->qmfDomainInCh->hQmfSlotsReal[i];
301     pLowBandImag[i] = hSbrDec->qmfDomainInCh->hQmfSlotsImag[i];
302   }
303 
304   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     for (i = 0; i < noCols; i++) {
308       pLowBandReal[i + noCols + ov_len] = hSbrDec->hQmfHBESlotsReal[i];
309       pLowBandImag[i + noCols + ov_len] = hSbrDec->hQmfHBESlotsImag[i];
310     }
311 
312     /* shift scale values according to buffer */
313     hSbrDec->scale_ov = hSbrDec->scale_lb;
314     hSbrDec->scale_lb = hSbrDec->scale_hbe;
315 
316     /* set pReal to where QMF analysis writes in case of HBE */
317     pReal += noCols;
318     pImag += noCols;
319     if (flags & SBRDEC_SKIP_QMF_ANA) {
320       /* stereoCfgIndex3 with HBE */
321       FDK_QmfDomain_QmfData2HBE(hSbrDec->qmfDomainInCh,
322                                 hSbrDec->hQmfHBESlotsReal,
323                                 hSbrDec->hQmfHBESlotsImag);
324     } else {
325       /* We have to move old hbe frame data to lb area of buffer */
326       for (i = 0; i < noCols; i++) {
327         FDKmemcpy(pLowBandReal[ov_len + i], hSbrDec->hQmfHBESlotsReal[i],
328                   hHeaderData->numberOfAnalysisBands * sizeof(FIXP_DBL));
329         FDKmemcpy(pLowBandImag[ov_len + i], hSbrDec->hQmfHBESlotsImag[i],
330                   hHeaderData->numberOfAnalysisBands * sizeof(FIXP_DBL));
331       }
332     }
333   }
334 
335   /*
336     low band codec signal subband filtering
337    */
338 
339   if (flags & SBRDEC_SKIP_QMF_ANA) {
340     if (!(flags & SBRDEC_USAC_HARMONICSBR)) /* stereoCfgIndex3 w/o HBE */
341       FDK_QmfDomain_WorkBuffer2ProcChannel(hSbrDec->qmfDomainInCh);
342   } else {
343     C_AALLOC_SCRATCH_START(qmfTemp, FIXP_DBL, 2 * (64));
344     qmfAnalysisFiltering(&hSbrDec->qmfDomainInCh->fb, pReal, pImag,
345                          &hSbrDec->qmfDomainInCh->scaling, pTimeInQmf,
346                          0 + sbrInDataHeadroom, 1, qmfTemp);
347 
348     C_AALLOC_SCRATCH_END(qmfTemp, FIXP_DBL, 2 * (64));
349   }
350 
351   /*
352     Clear upper half of spectrum
353   */
354   if (!((flags & SBRDEC_USAC_HARMONICSBR) &&
355         (hFrameData->sbrPatchingMode == 0))) {
356     int nAnalysisBands = hHeaderData->numberOfAnalysisBands;
357 
358     if (!(flags & SBRDEC_LOW_POWER)) {
359       for (slot = ov_len; slot < noCols + ov_len; slot++) {
360         FDKmemclear(&pLowBandReal[slot][nAnalysisBands],
361                     ((64) - nAnalysisBands) * sizeof(FIXP_DBL));
362         FDKmemclear(&pLowBandImag[slot][nAnalysisBands],
363                     ((64) - nAnalysisBands) * sizeof(FIXP_DBL));
364       }
365     } else {
366       for (slot = ov_len; slot < noCols + ov_len; slot++) {
367         FDKmemclear(&pLowBandReal[slot][nAnalysisBands],
368                     ((64) - nAnalysisBands) * sizeof(FIXP_DBL));
369       }
370     }
371   }
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   maxVal = maxSubbandSample(pReal, (flags & SBRDEC_LOW_POWER) ? NULL : pImag, 0,
381                             hSbrDec->qmfDomainInCh->fb.no_channels, 0, noCols);
382 
383   reserve = fixMax(0, CntLeadingZeros(maxVal) - 1);
384   reserve = fixMin(reserve,
385                    DFRACT_BITS - 1 - hSbrDec->qmfDomainInCh->scaling.lb_scale);
386 
387   /* If all data is zero, lb_scale could become too large */
388   rescaleSubbandSamples(pReal, (flags & SBRDEC_LOW_POWER) ? NULL : pImag, 0,
389                         hSbrDec->qmfDomainInCh->fb.no_channels, 0, noCols,
390                         reserve);
391 
392   hSbrDec->qmfDomainInCh->scaling.lb_scale += reserve;
393 
394   if ((flags & SBRDEC_USAC_HARMONICSBR)) {
395     /* actually this is our hbe_scale */
396     hSbrDec->scale_hbe = hSbrDec->qmfDomainInCh->scaling.lb_scale;
397     /* the real lb_scale is stored in scale_lb from sbr */
398     hSbrDec->qmfDomainInCh->scaling.lb_scale = hSbrDec->scale_lb;
399   }
400   /*
401     save low band scale, wavecoding or parametric stereo may modify it
402   */
403   saveLbScale = hSbrDec->qmfDomainInCh->scaling.lb_scale;
404 
405   if (applyProcessing) {
406     UCHAR *borders = hFrameData->frameInfo.borders;
407     lastSlotOffs = borders[hFrameData->frameInfo.nEnvelopes] -
408                    hHeaderData->numberTimeSlots;
409 
410     FIXP_DBL degreeAlias[(64)];
411     PVC_DYNAMIC_DATA pvcDynamicData;
412     pvcInitFrame(
413         &hSbrDec->PvcStaticData, &pvcDynamicData,
414         (hHeaderData->frameErrorFlag ? 0 : hHeaderData->bs_info.pvc_mode),
415         hFrameData->ns, hHeaderData->timeStep,
416         hHeaderData->freqBandData.lowSubband,
417         hFrameData->frameInfo.pvcBorders[0], hFrameData->pvcID);
418 
419     if (!hHeaderData->frameErrorFlag && (hHeaderData->bs_info.pvc_mode > 0)) {
420       pvcDecodeFrame(&hSbrDec->PvcStaticData, &pvcDynamicData, pLowBandReal,
421                      pLowBandImag, ov_len,
422                      SCALE2EXP(hSbrDec->qmfDomainInCh->scaling.ov_lb_scale),
423                      SCALE2EXP(hSbrDec->qmfDomainInCh->scaling.lb_scale));
424     }
425     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     if (flags & SBRDEC_LOW_POWER)
431       FDKmemclear(&degreeAlias[hHeaderData->freqBandData.lowSubband],
432                   (hHeaderData->freqBandData.highSubband -
433                    hHeaderData->freqBandData.lowSubband) *
434                       sizeof(FIXP_DBL));
435 
436     /*
437       Inverse filtering of lowband and transposition into the SBR-frequency
438       range
439     */
440 
441     {
442       KEEP_STATES_SYNCED_MODE keepStatesSyncedMode =
443           ((flags & SBRDEC_USAC_HARMONICSBR) &&
444            (hFrameData->sbrPatchingMode != 0))
445               ? KEEP_STATES_SYNCED_NORMAL
446               : KEEP_STATES_SYNCED_OFF;
447 
448       if (flags & SBRDEC_USAC_HARMONICSBR) {
449         if (flags & SBRDEC_QUAD_RATE) {
450           pReal -= 32;
451           pImag -= 32;
452         }
453 
454         if ((hSbrDec->savedStates == 0) && (hFrameData->sbrPatchingMode == 1)) {
455           /* copy saved states from previous frame to legacy SBR lpc filterstate
456            * buffer   */
457           for (i = 0; i < LPC_ORDER + ov_len; i++) {
458             FDKmemcpy(
459                 hSbrDec->LppTrans.lpcFilterStatesRealLegSBR[i],
460                 hSbrDec->codecQMFBufferReal[noCols - LPC_ORDER - ov_len + i],
461                 hSbrDec->hHBE->noChannels * sizeof(FIXP_DBL));
462             FDKmemcpy(
463                 hSbrDec->LppTrans.lpcFilterStatesImagLegSBR[i],
464                 hSbrDec->codecQMFBufferImag[noCols - LPC_ORDER - ov_len + i],
465                 hSbrDec->hHBE->noChannels * sizeof(FIXP_DBL));
466           }
467         }
468 
469         /* saving unmodified QMF states in case we are switching from legacy SBR
470          * to HBE */
471         for (i = 0; i < hSbrDec->hHBE->noCols; i++) {
472           FDKmemcpy(hSbrDec->codecQMFBufferReal[i], pLowBandReal[ov_len + i],
473                     hSbrDec->hHBE->noChannels * sizeof(FIXP_DBL));
474           FDKmemcpy(hSbrDec->codecQMFBufferImag[i], pLowBandImag[ov_len + i],
475                     hSbrDec->hHBE->noChannels * sizeof(FIXP_DBL));
476         }
477 
478         QmfTransposerApply(
479             hSbrDec->hHBE, pReal, pImag, noCols, pLowBandReal, pLowBandImag,
480             hSbrDec->LppTrans.lpcFilterStatesRealHBE,
481             hSbrDec->LppTrans.lpcFilterStatesImagHBE,
482             hFrameData->sbrPitchInBins, hSbrDec->scale_lb, hSbrDec->scale_hbe,
483             &hSbrDec->qmfDomainInCh->scaling.hb_scale, hHeaderData->timeStep,
484             borders[0], ov_len, keepStatesSyncedMode);
485 
486         if (flags & SBRDEC_QUAD_RATE) {
487           int *xOverQmf = GetxOverBandQmfTransposer(hSbrDec->hHBE);
488 
489           copyHarmonicSpectrum(xOverQmf, pLowBandReal, pLowBandImag, noCols,
490                                ov_len, keepStatesSyncedMode);
491         }
492       }
493     }
494 
495     if ((flags & SBRDEC_USAC_HARMONICSBR) &&
496         (hFrameData->sbrPatchingMode == 0)) {
497       hSbrDec->prev_frame_lSbr = 0;
498       hSbrDec->prev_frame_hbeSbr = 1;
499 
500       lppTransposerHBE(
501           &hSbrDec->LppTrans, hSbrDec->hHBE, &hSbrDec->qmfDomainInCh->scaling,
502           pLowBandReal, pLowBandImag, hHeaderData->timeStep, borders[0],
503           lastSlotOffs, hHeaderData->freqBandData.nInvfBands,
504           hFrameData->sbr_invf_mode, hPrevFrameData->sbr_invf_mode);
505 
506     } else {
507       if (flags & SBRDEC_USAC_HARMONICSBR) {
508         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           FDKmemcpy(hSbrDec->LppTrans.lpcFilterStatesRealHBE[i],
514                     hSbrDec->qmfDomainInCh
515                         ->hQmfSlotsReal[hSbrDec->hHBE->noCols - LPC_ORDER + i],
516                     (64) * sizeof(FIXP_DBL));
517           FDKmemcpy(hSbrDec->LppTrans.lpcFilterStatesImagHBE[i],
518                     hSbrDec->qmfDomainInCh
519                         ->hQmfSlotsImag[hSbrDec->hHBE->noCols - LPC_ORDER + i],
520                     (64) * sizeof(FIXP_DBL));
521         }
522       }
523       {
524         hSbrDec->prev_frame_lSbr = 1;
525         hSbrDec->prev_frame_hbeSbr = 0;
526       }
527 
528       lppTransposer(
529           &hSbrDec->LppTrans, &hSbrDec->qmfDomainInCh->scaling, pLowBandReal,
530           degreeAlias,  // only used if useLP = 1
531           pLowBandImag, flags & SBRDEC_LOW_POWER,
532           hHeaderData->bs_info.sbr_preprocessing,
533           hHeaderData->freqBandData.v_k_master[0], hHeaderData->timeStep,
534           borders[0], lastSlotOffs, hHeaderData->freqBandData.nInvfBands,
535           hFrameData->sbr_invf_mode, hPrevFrameData->sbr_invf_mode);
536     }
537 
538     /*
539       Adjust envelope of current frame.
540     */
541 
542     if ((hFrameData->sbrPatchingMode !=
543          hSbrDec->SbrCalculateEnvelope.sbrPatchingMode)) {
544       ResetLimiterBands(hHeaderData->freqBandData.limiterBandTable,
545                         &hHeaderData->freqBandData.noLimiterBands,
546                         hHeaderData->freqBandData.freqBandTable[0],
547                         hHeaderData->freqBandData.nSfb[0],
548                         hSbrDec->LppTrans.pSettings->patchParam,
549                         hSbrDec->LppTrans.pSettings->noOfPatches,
550                         hHeaderData->bs_data.limiterBands,
551                         hFrameData->sbrPatchingMode,
552                         (flags & SBRDEC_USAC_HARMONICSBR) &&
553                                 (hFrameData->sbrPatchingMode == 0)
554                             ? GetxOverBandQmfTransposer(hSbrDec->hHBE)
555                             : NULL,
556                         Get41SbrQmfTransposer(hSbrDec->hHBE));
557 
558       hSbrDec->SbrCalculateEnvelope.sbrPatchingMode =
559           hFrameData->sbrPatchingMode;
560     }
561 
562     calculateSbrEnvelope(
563         &hSbrDec->qmfDomainInCh->scaling, &hSbrDec->SbrCalculateEnvelope,
564         hHeaderData, hFrameData, &pvcDynamicData, pLowBandReal, pLowBandImag,
565         flags & SBRDEC_LOW_POWER,
566 
567         degreeAlias, flags,
568         (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     for (i = 0; i < hHeaderData->freqBandData.nInvfBands; i++) {
591       hPrevFrameData->sbr_invf_mode[i] = hFrameData->sbr_invf_mode[i];
592     }
593     hPrevFrameData->coupling = hFrameData->coupling;
594     hPrevFrameData->stopPos = borders[hFrameData->frameInfo.nEnvelopes];
595     hPrevFrameData->ampRes = hFrameData->ampResolutionCurrentFrame;
596     hPrevFrameData->prevSbrPitchInBins = hFrameData->sbrPitchInBins;
597     /* could be done in extractFrameInfo_pvc() but hPrevFrameData is not
598      * available there */
599     FDKmemcpy(&hPrevFrameData->prevFrameInfo, &hFrameData->frameInfo,
600               sizeof(FRAME_INFO));
601   } else {
602     /* rescale from lsb to nAnalysisBands in order to compensate scaling with
603      * hb_scale in this area, done by synthesisFiltering*/
604     int rescale;
605     int lsb;
606     int length;
607 
608     /* Reset hb_scale if no highband is present, because hb_scale is considered
609      * in the QMF-synthesis */
610     hSbrDec->qmfDomainInCh->scaling.hb_scale = saveLbScale;
611 
612     rescale = hSbrDec->qmfDomainInCh->scaling.hb_scale -
613               hSbrDec->qmfDomainInCh->scaling.ov_lb_scale;
614     lsb = hSbrDec->qmfDomainOutCh->fb.lsb;
615     length = (hSbrDec->qmfDomainInCh->fb.no_channels - lsb);
616 
617     if ((rescale < 0) && (length > 0)) {
618       if (!(flags & SBRDEC_LOW_POWER)) {
619         for (i = 0; i < ov_len; i++) {
620           scaleValues(&pLowBandReal[i][lsb], length, rescale);
621           scaleValues(&pLowBandImag[i][lsb], length, rescale);
622         }
623       } else {
624         for (i = 0; i < ov_len; i++) {
625           scaleValues(&pLowBandReal[i][lsb], length, rescale);
626         }
627       }
628     }
629   }
630 
631   if (!(flags & SBRDEC_USAC_HARMONICSBR)) {
632     int length = hSbrDec->qmfDomainInCh->fb.lsb;
633     if (flags & SBRDEC_SYNTAX_USAC) {
634       length = hSbrDec->qmfDomainInCh->fb.no_channels;
635     }
636 
637     /* in case of legacy sbr saving of filter states here */
638     for (i = 0; i < LPC_ORDER + ov_len; i++) {
639       /*
640         Store the unmodified qmf Slots values (required for LPC filtering)
641       */
642       if (!(flags & SBRDEC_LOW_POWER)) {
643         FDKmemcpy(hSbrDec->LppTrans.lpcFilterStatesRealLegSBR[i],
644                   pLowBandReal[noCols - LPC_ORDER + i],
645                   length * sizeof(FIXP_DBL));
646         FDKmemcpy(hSbrDec->LppTrans.lpcFilterStatesImagLegSBR[i],
647                   pLowBandImag[noCols - LPC_ORDER + i],
648                   length * sizeof(FIXP_DBL));
649       } else
650         FDKmemcpy(hSbrDec->LppTrans.lpcFilterStatesRealLegSBR[i],
651                   pLowBandReal[noCols - LPC_ORDER + i],
652                   length * sizeof(FIXP_DBL));
653     }
654   }
655 
656   /*
657     Synthesis subband filtering.
658   */
659 
660   if (!(flags & SBRDEC_PS_DECODED)) {
661     if (!(flags & SBRDEC_SKIP_QMF_SYN)) {
662       int outScalefactor = -(8);
663 
664       if (h_ps_d != NULL) {
665         h_ps_d->procFrameBased = 1; /* we here do frame based processing */
666       }
667 
668       sbrDecoder_drcApply(&hSbrDec->sbrDrcChannel, pLowBandReal,
669                           (flags & SBRDEC_LOW_POWER) ? NULL : pLowBandImag,
670                           hSbrDec->qmfDomainOutCh->fb.no_col, &outScalefactor);
671 
672       qmfChangeOutScalefactor(&hSbrDec->qmfDomainOutCh->fb, outScalefactor);
673 
674       {
675         HANDLE_FREQ_BAND_DATA hFreq = &hHeaderData->freqBandData;
676         int save_usb = hSbrDec->qmfDomainOutCh->fb.usb;
677 
678 #if (QMF_MAX_SYNTHESIS_BANDS <= 64)
679         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         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           hSbrDec->qmfDomainOutCh->fb.usb =
692               fMin((UINT)hFreq->ov_highSubband,
693                    (UINT)hSbrDec->qmfDomainOutCh->fb.no_channels);
694         }
695         {
696           qmfSynthesisFiltering(
697               &hSbrDec->qmfDomainOutCh->fb, pLowBandReal,
698               (flags & SBRDEC_LOW_POWER) ? NULL : pLowBandImag,
699               &hSbrDec->qmfDomainInCh->scaling,
700               hSbrDec->LppTrans.pSettings->overlap, timeOut, strideOut,
701               qmfTemp);
702         }
703         /* restore saved value */
704         hSbrDec->qmfDomainOutCh->fb.usb = save_usb;
705         hFreq->ov_highSubband = save_usb;
706 #if (QMF_MAX_SYNTHESIS_BANDS <= 64)
707         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       }
712     }
713 
714   } else { /* (flags & SBRDEC_PS_DECODED) */
715     INT sdiff;
716     INT scaleFactorHighBand, scaleFactorLowBand_ov, scaleFactorLowBand_no_ov;
717 
718     HANDLE_QMF_FILTER_BANK synQmf = &hSbrDec->qmfDomainOutCh->fb;
719     HANDLE_QMF_FILTER_BANK synQmfRight = &hSbrDecRight->qmfDomainOutCh->fb;
720 
721     /* adapt scaling */
722     sdiff = hSbrDec->qmfDomainInCh->scaling.lb_scale -
723             reserve; /* Scaling difference */
724     scaleFactorHighBand = sdiff - hSbrDec->qmfDomainInCh->scaling.hb_scale;
725     scaleFactorLowBand_ov = sdiff - hSbrDec->qmfDomainInCh->scaling.ov_lb_scale;
726     scaleFactorLowBand_no_ov = sdiff - hSbrDec->qmfDomainInCh->scaling.lb_scale;
727 
728     /* Scale of low band overlapping QMF data */
729     scaleFactorLowBand_ov =
730         fMin(DFRACT_BITS - 1, fMax(-(DFRACT_BITS - 1), scaleFactorLowBand_ov));
731     /* Scale of low band current QMF data     */
732     scaleFactorLowBand_no_ov = fMin(
733         DFRACT_BITS - 1, fMax(-(DFRACT_BITS - 1), scaleFactorLowBand_no_ov));
734     /* Scale of current high band */
735     scaleFactorHighBand =
736         fMin(DFRACT_BITS - 1, fMax(-(DFRACT_BITS - 1), scaleFactorHighBand));
737 
738     if (h_ps_d->procFrameBased == 1) /* If we have switched from frame to slot
739                                         based processing copy filter states */
740     {                                /* procFrameBased will be unset later */
741       /* copy filter states from left to right */
742       /* was ((640)-(64))*sizeof(FIXP_QSS)
743          flexible amount of synthesis bands needed for QMF based resampling
744       */
745       FDK_ASSERT(hSbrDec->qmfDomainInCh->pGlobalConf->nBandsSynthesis <=
746                  QMF_MAX_SYNTHESIS_BANDS);
747       qmfChangeOutScalefactor(synQmfRight, -(8));
748       FDKmemcpy(synQmfRight->FilterStates, synQmf->FilterStates,
749                 9 * hSbrDec->qmfDomainInCh->pGlobalConf->nBandsSynthesis *
750                     sizeof(FIXP_QSS));
751     }
752 
753     /* Feed delaylines when parametric stereo is switched on. */
754     PreparePsProcessing(h_ps_d, pLowBandReal, pLowBandImag,
755                         scaleFactorLowBand_ov);
756 
757     /* use the same synthese qmf values for left and right channel */
758     synQmfRight->no_col = synQmf->no_col;
759     synQmfRight->lsb = synQmf->lsb;
760     synQmfRight->usb = synQmf->usb;
761 
762     int env = 0;
763 
764     {
765 #if (QMF_MAX_SYNTHESIS_BANDS <= 64)
766       C_AALLOC_SCRATCH_START(pWorkBuffer, FIXP_DBL,
767                              2 * QMF_MAX_SYNTHESIS_BANDS);
768 #else
769       C_AALLOC_STACK_START(pWorkBuffer, FIXP_DBL, 2 * QMF_MAX_SYNTHESIS_BANDS);
770 #endif
771 
772       int maxShift = 0;
773 
774       if (hSbrDec->sbrDrcChannel.enable != 0) {
775         if (hSbrDec->sbrDrcChannel.prevFact_exp > maxShift) {
776           maxShift = hSbrDec->sbrDrcChannel.prevFact_exp;
777         }
778         if (hSbrDec->sbrDrcChannel.currFact_exp > maxShift) {
779           maxShift = hSbrDec->sbrDrcChannel.currFact_exp;
780         }
781         if (hSbrDec->sbrDrcChannel.nextFact_exp > maxShift) {
782           maxShift = hSbrDec->sbrDrcChannel.nextFact_exp;
783         }
784       }
785 
786       /* copy DRC data to right channel (with PS both channels use the same DRC
787        * gains) */
788       FDKmemcpy(&hSbrDecRight->sbrDrcChannel, &hSbrDec->sbrDrcChannel,
789                 sizeof(SBRDEC_DRC_CHANNEL));
790 
791       for (i = 0; i < synQmf->no_col; i++) { /* ----- no_col loop ----- */
792 
793         INT outScalefactorR, outScalefactorL;
794 
795         /* qmf timeslot of right channel */
796         FIXP_DBL *rQmfReal = pWorkBuffer;
797         FIXP_DBL *rQmfImag = pWorkBuffer + synQmf->no_channels;
798 
799         {
800           if (i ==
801               h_ps_d->bsData[h_ps_d->processSlot].mpeg.aEnvStartStop[env]) {
802             initSlotBasedRotation(h_ps_d, env,
803                                   hHeaderData->freqBandData.highSubband);
804             env++;
805           }
806 
807           ApplyPsSlot(
808               h_ps_d,             /* parametric stereo decoder handle  */
809               (pLowBandReal + i), /* one timeslot of left/mono channel */
810               (pLowBandImag + i), /* one timeslot of left/mono channel */
811               rQmfReal,           /* one timeslot or right channel     */
812               rQmfImag,           /* one timeslot or right channel     */
813               scaleFactorLowBand_no_ov,
814               (i < hSbrDec->LppTrans.pSettings->overlap)
815                   ? scaleFactorLowBand_ov
816                   : scaleFactorLowBand_no_ov,
817               scaleFactorHighBand, synQmf->lsb, synQmf->usb);
818 
819           outScalefactorL = outScalefactorR =
820               1 + sbrInDataHeadroom; /* psDiffScale! (MPEG-PS) */
821         }
822 
823         sbrDecoder_drcApplySlot(/* right channel */
824                                 &hSbrDecRight->sbrDrcChannel, rQmfReal,
825                                 rQmfImag, i, synQmfRight->no_col, maxShift);
826 
827         outScalefactorR += maxShift;
828 
829         sbrDecoder_drcApplySlot(/* left channel */
830                                 &hSbrDec->sbrDrcChannel, *(pLowBandReal + i),
831                                 *(pLowBandImag + i), i, synQmf->no_col,
832                                 maxShift);
833 
834         outScalefactorL += maxShift;
835 
836         if (!(flags & SBRDEC_SKIP_QMF_SYN)) {
837           qmfChangeOutScalefactor(synQmf, -(8));
838           qmfChangeOutScalefactor(synQmfRight, -(8));
839 
840           qmfSynthesisFilteringSlot(
841               synQmfRight, rQmfReal, /* QMF real buffer */
842               rQmfImag,              /* QMF imag buffer */
843               outScalefactorL, outScalefactorL,
844               timeOutRight + (i * synQmf->no_channels * strideOut), strideOut,
845               pWorkBuffer);
846 
847           qmfSynthesisFilteringSlot(
848               synQmf, *(pLowBandReal + i), /* QMF real buffer */
849               *(pLowBandImag + i),         /* QMF imag buffer */
850               outScalefactorR, outScalefactorR,
851               timeOut + (i * synQmf->no_channels * strideOut), strideOut,
852               pWorkBuffer);
853         }
854       } /* no_col loop  i  */
855 #if (QMF_MAX_SYNTHESIS_BANDS <= 64)
856       C_AALLOC_SCRATCH_END(pWorkBuffer, FIXP_DBL, 2 * QMF_MAX_SYNTHESIS_BANDS);
857 #else
858       C_AALLOC_STACK_END(pWorkBuffer, FIXP_DBL, 2 * QMF_MAX_SYNTHESIS_BANDS);
859 #endif
860     }
861   }
862 
863   sbrDecoder_drcUpdateChannel(&hSbrDec->sbrDrcChannel);
864 
865   /*
866     Update overlap buffer
867     Even bands above usb are copied to avoid outdated spectral data in case
868     the stop frequency raises.
869   */
870 
871   if (!(flags & SBRDEC_SKIP_QMF_SYN)) {
872     {
873       FDK_QmfDomain_SaveOverlap(hSbrDec->qmfDomainInCh, 0);
874       FDK_ASSERT(hSbrDec->qmfDomainInCh->scaling.ov_lb_scale == saveLbScale);
875     }
876   }
877 
878   hSbrDec->savedStates = 0;
879 
880   /* Save current frame status */
881   hPrevFrameData->frameErrorFlag = hHeaderData->frameErrorFlag;
882   hSbrDec->applySbrProc_old = applyProcessing;
883 
884 } /* sbr_dec() */
885 
886 /*!
887   \brief     Creates sbr decoder structure
888   \return    errorCode, 0 if successful
889 */
890 SBR_ERROR
createSbrDec(SBR_CHANNEL * hSbrChannel,HANDLE_SBR_HEADER_DATA hHeaderData,TRANSPOSER_SETTINGS * pSettings,const int downsampleFac,const UINT qmfFlags,const UINT flags,const int overlap,int chan,int codecFrameSize)891 createSbrDec(SBR_CHANNEL *hSbrChannel,
892              HANDLE_SBR_HEADER_DATA hHeaderData, /*!< Static control data */
893              TRANSPOSER_SETTINGS *pSettings,
894              const int downsampleFac, /*!< Downsampling factor */
895              const UINT qmfFlags, /*!< flags -> 1: HQ/LP selector, 2: CLDFB */
896              const UINT flags, const int overlap,
897              int chan, /*!< Channel for which to assign buffers etc. */
898              int codecFrameSize)
899 
900 {
901   SBR_ERROR err = SBRDEC_OK;
902   int timeSlots =
903       hHeaderData->numberTimeSlots; /* Number of SBR slots per frame */
904   int noCols =
905       timeSlots * hHeaderData->timeStep; /* Number of QMF slots per frame */
906   HANDLE_SBR_DEC hs = &(hSbrChannel->SbrDec);
907 
908 #if (SBRDEC_MAX_HB_FADE_FRAMES > 0)
909   hs->highBandFadeCnt = SBRDEC_MAX_HB_FADE_FRAMES;
910 
911 #endif
912   hs->scale_hbe = 15;
913   hs->scale_lb = 15;
914   hs->scale_ov = 15;
915 
916   hs->prev_frame_lSbr = 0;
917   hs->prev_frame_hbeSbr = 0;
918 
919   hs->codecFrameSize = codecFrameSize;
920 
921   /*
922     create envelope calculator
923   */
924   err = createSbrEnvelopeCalc(&hs->SbrCalculateEnvelope, hHeaderData, chan,
925                               flags);
926   if (err != SBRDEC_OK) {
927     return err;
928   }
929 
930   initSbrPrevFrameData(&hSbrChannel->prevFrameData, timeSlots);
931 
932   /*
933     create transposer
934   */
935   err = createLppTransposer(
936       &hs->LppTrans, pSettings, hHeaderData->freqBandData.lowSubband,
937       hHeaderData->freqBandData.v_k_master, hHeaderData->freqBandData.numMaster,
938       hHeaderData->freqBandData.highSubband, timeSlots, noCols,
939       hHeaderData->freqBandData.freqBandTableNoise,
940       hHeaderData->freqBandData.nNfb, hHeaderData->sbrProcSmplRate, chan,
941       overlap);
942   if (err != SBRDEC_OK) {
943     return err;
944   }
945 
946   if (flags & SBRDEC_USAC_HARMONICSBR) {
947     int noChannels, bSbr41 = flags & SBRDEC_QUAD_RATE ? 1 : 0;
948 
949     noChannels =
950         QMF_SYNTH_CHANNELS /
951         ((bSbr41 + 1) * 2); /* 32 for (32:64 and 24:64) and 16 for 16:64 */
952 
953     /* shared memory between hbeLightTimeDelayBuffer and hQmfHBESlotsReal if
954      * SBRDEC_HBE_ENABLE */
955     hSbrChannel->SbrDec.tmp_memory = (FIXP_DBL **)fdkCallocMatrix2D_aligned(
956         noCols, noChannels, sizeof(FIXP_DBL));
957     if (hSbrChannel->SbrDec.tmp_memory == NULL) {
958       return SBRDEC_MEM_ALLOC_FAILED;
959     }
960 
961     hSbrChannel->SbrDec.hQmfHBESlotsReal = hSbrChannel->SbrDec.tmp_memory;
962     hSbrChannel->SbrDec.hQmfHBESlotsImag =
963         (FIXP_DBL **)fdkCallocMatrix2D_aligned(noCols, noChannels,
964                                                sizeof(FIXP_DBL));
965     if (hSbrChannel->SbrDec.hQmfHBESlotsImag == NULL) {
966       return SBRDEC_MEM_ALLOC_FAILED;
967     }
968 
969     /* buffers containing unmodified qmf data; required when switching from
970      * legacy SBR to HBE                       */
971     /* buffer can be used as LPCFilterstates buffer because legacy SBR needs
972      * exactly these values for LPC filtering */
973     hSbrChannel->SbrDec.codecQMFBufferReal =
974         (FIXP_DBL **)fdkCallocMatrix2D_aligned(noCols, noChannels,
975                                                sizeof(FIXP_DBL));
976     if (hSbrChannel->SbrDec.codecQMFBufferReal == NULL) {
977       return SBRDEC_MEM_ALLOC_FAILED;
978     }
979 
980     hSbrChannel->SbrDec.codecQMFBufferImag =
981         (FIXP_DBL **)fdkCallocMatrix2D_aligned(noCols, noChannels,
982                                                sizeof(FIXP_DBL));
983     if (hSbrChannel->SbrDec.codecQMFBufferImag == NULL) {
984       return SBRDEC_MEM_ALLOC_FAILED;
985     }
986 
987     err = QmfTransposerCreate(&hs->hHBE, codecFrameSize, 0, bSbr41);
988     if (err != SBRDEC_OK) {
989       return err;
990     }
991   }
992 
993   return err;
994 }
995 
996 /*!
997   \brief     Delete sbr decoder structure
998   \return    errorCode, 0 if successful
999 */
deleteSbrDec(SBR_CHANNEL * hSbrChannel)1000 int deleteSbrDec(SBR_CHANNEL *hSbrChannel) {
1001   HANDLE_SBR_DEC hs = &hSbrChannel->SbrDec;
1002 
1003   deleteSbrEnvelopeCalc(&hs->SbrCalculateEnvelope);
1004 
1005   if (hs->tmp_memory != NULL) {
1006     FDK_FREE_MEMORY_2D_ALIGNED(hs->tmp_memory);
1007   }
1008 
1009   /* modify here */
1010   FDK_FREE_MEMORY_2D_ALIGNED(hs->hQmfHBESlotsImag);
1011 
1012   if (hs->hHBE != NULL) QmfTransposerClose(hs->hHBE);
1013 
1014   if (hs->codecQMFBufferReal != NULL) {
1015     FDK_FREE_MEMORY_2D_ALIGNED(hs->codecQMFBufferReal);
1016   }
1017 
1018   if (hs->codecQMFBufferImag != NULL) {
1019     FDK_FREE_MEMORY_2D_ALIGNED(hs->codecQMFBufferImag);
1020   }
1021 
1022   return 0;
1023 }
1024 
1025 /*!
1026   \brief     resets sbr decoder structure
1027   \return    errorCode, 0 if successful
1028 */
1029 SBR_ERROR
resetSbrDec(HANDLE_SBR_DEC hSbrDec,HANDLE_SBR_HEADER_DATA hHeaderData,HANDLE_SBR_PREV_FRAME_DATA hPrevFrameData,const int downsampleFac,const UINT flags,HANDLE_SBR_FRAME_DATA hFrameData)1030 resetSbrDec(HANDLE_SBR_DEC hSbrDec, HANDLE_SBR_HEADER_DATA hHeaderData,
1031             HANDLE_SBR_PREV_FRAME_DATA hPrevFrameData, const int downsampleFac,
1032             const UINT flags, HANDLE_SBR_FRAME_DATA hFrameData) {
1033   SBR_ERROR sbrError = SBRDEC_OK;
1034   int i;
1035   FIXP_DBL *pLowBandReal[128];
1036   FIXP_DBL *pLowBandImag[128];
1037   int useLP = flags & SBRDEC_LOW_POWER;
1038 
1039   int old_lsb = hSbrDec->qmfDomainInCh->fb.lsb;
1040   int old_usb = hSbrDec->qmfDomainInCh->fb.usb;
1041   int new_lsb = hHeaderData->freqBandData.lowSubband;
1042   /* int new_usb = hHeaderData->freqBandData.highSubband; */
1043   int l, startBand, stopBand, startSlot, size;
1044 
1045   FIXP_DBL **OverlapBufferReal = hSbrDec->qmfDomainInCh->hQmfSlotsReal;
1046   FIXP_DBL **OverlapBufferImag = hSbrDec->qmfDomainInCh->hQmfSlotsImag;
1047 
1048   /* in case the previous frame was not active in terms of SBR processing, the
1049      full band from 0 to no_channels was rescaled and not overwritten. Thats why
1050      the scaling factor lb_scale can be seen as assigned to all bands from 0 to
1051      no_channels in the previous frame. The same states for the current frame if
1052      the current frame is not active in terms of SBR processing
1053   */
1054   int applySbrProc = (hHeaderData->syncState == SBR_ACTIVE ||
1055                       (hHeaderData->frameErrorFlag == 0 &&
1056                        hHeaderData->syncState == SBR_HEADER));
1057   int applySbrProc_old = hSbrDec->applySbrProc_old;
1058 
1059   if (!applySbrProc) {
1060     new_lsb = (hSbrDec->qmfDomainInCh->fb).no_channels;
1061   }
1062   if (!applySbrProc_old) {
1063     old_lsb = (hSbrDec->qmfDomainInCh->fb).no_channels;
1064     old_usb = old_lsb;
1065   }
1066 
1067   resetSbrEnvelopeCalc(&hSbrDec->SbrCalculateEnvelope);
1068 
1069   /* Change lsb and usb */
1070   /* Synthesis */
1071   FDK_ASSERT(hSbrDec->qmfDomainOutCh != NULL);
1072   hSbrDec->qmfDomainOutCh->fb.lsb =
1073       fixMin((INT)hSbrDec->qmfDomainOutCh->fb.no_channels,
1074              (INT)hHeaderData->freqBandData.lowSubband);
1075   hSbrDec->qmfDomainOutCh->fb.usb =
1076       fixMin((INT)hSbrDec->qmfDomainOutCh->fb.no_channels,
1077              (INT)hHeaderData->freqBandData.highSubband);
1078   /* Analysis */
1079   FDK_ASSERT(hSbrDec->qmfDomainInCh != NULL);
1080   hSbrDec->qmfDomainInCh->fb.lsb = hSbrDec->qmfDomainOutCh->fb.lsb;
1081   hSbrDec->qmfDomainInCh->fb.usb = hSbrDec->qmfDomainOutCh->fb.usb;
1082 
1083   /*
1084     The following initialization of spectral data in the overlap buffer
1085     is required for dynamic x-over or a change of the start-freq for 2 reasons:
1086 
1087     1. If the lowband gets _wider_, unadjusted data would remain
1088 
1089     2. If the lowband becomes _smaller_, the highest bands of the old lowband
1090        must be cleared because the whitening would be affected
1091   */
1092   startBand = old_lsb;
1093   stopBand = new_lsb;
1094   startSlot = fMax(0, hHeaderData->timeStep * (hPrevFrameData->stopPos -
1095                                                hHeaderData->numberTimeSlots));
1096   size = fMax(0, stopBand - startBand);
1097 
1098   /* in case of USAC we don't want to zero out the memory, as this can lead to
1099      holes in the spectrum; fix shall only be applied for USAC not for MPEG-4
1100      SBR, in this case setting zero remains         */
1101   if (!(flags & SBRDEC_SYNTAX_USAC)) {
1102     /* keep already adjusted data in the x-over-area */
1103     if (!useLP) {
1104       for (l = startSlot; l < hSbrDec->LppTrans.pSettings->overlap; l++) {
1105         FDKmemclear(&OverlapBufferReal[l][startBand], size * sizeof(FIXP_DBL));
1106         FDKmemclear(&OverlapBufferImag[l][startBand], size * sizeof(FIXP_DBL));
1107       }
1108     } else {
1109       for (l = startSlot; l < hSbrDec->LppTrans.pSettings->overlap; l++) {
1110         FDKmemclear(&OverlapBufferReal[l][startBand], size * sizeof(FIXP_DBL));
1111       }
1112     }
1113 
1114     /*
1115     reset LPC filter states
1116     */
1117     startBand = fixMin(old_lsb, new_lsb);
1118     stopBand = fixMax(old_lsb, new_lsb);
1119     size = fixMax(0, stopBand - startBand);
1120 
1121     FDKmemclear(&hSbrDec->LppTrans.lpcFilterStatesRealLegSBR[0][startBand],
1122                 size * sizeof(FIXP_DBL));
1123     FDKmemclear(&hSbrDec->LppTrans.lpcFilterStatesRealLegSBR[1][startBand],
1124                 size * sizeof(FIXP_DBL));
1125     if (!useLP) {
1126       FDKmemclear(&hSbrDec->LppTrans.lpcFilterStatesImagLegSBR[0][startBand],
1127                   size * sizeof(FIXP_DBL));
1128       FDKmemclear(&hSbrDec->LppTrans.lpcFilterStatesImagLegSBR[1][startBand],
1129                   size * sizeof(FIXP_DBL));
1130     }
1131   }
1132 
1133   if (startSlot != 0) {
1134     int source_exp, target_exp, delta_exp, target_lsb, target_usb, reserve;
1135     FIXP_DBL maxVal;
1136 
1137     /*
1138     Rescale already processed spectral data between old and new x-over
1139     frequency. This must be done because of the separate scalefactors for
1140     lowband and highband.
1141     */
1142 
1143     /* We have four relevant transitions to cover:
1144     1. old_usb is lower than new_lsb; old SBR area is completely below new SBR
1145     area.
1146        -> entire old area was highband and belongs to lowband now
1147           and has to be rescaled.
1148     2. old_lsb is higher than new_usb; new SBR area is completely below old SBR
1149     area.
1150        -> old area between new_lsb and old_lsb was lowband and belongs to
1151     highband now and has to be rescaled to match new highband scale.
1152     3. old_lsb is lower and old_usb is higher than new_lsb; old and new SBR
1153     areas overlap.
1154        -> old area between old_lsb and new_lsb was highband and belongs to
1155     lowband now and has to be rescaled to match new lowband scale.
1156     4. new_lsb is lower and new_usb_is higher than old_lsb; old and new SBR
1157     areas overlap.
1158        -> old area between new_lsb and old_usb was lowband and belongs to
1159     highband now and has to be rescaled to match new highband scale.
1160     */
1161 
1162     if (new_lsb > old_lsb) {
1163       /* case 1 and 3 */
1164       source_exp = SCALE2EXP(hSbrDec->qmfDomainInCh->scaling.ov_hb_scale);
1165       target_exp = SCALE2EXP(hSbrDec->qmfDomainInCh->scaling.ov_lb_scale);
1166 
1167       startBand = old_lsb;
1168 
1169       if (new_lsb >= old_usb) {
1170         /* case 1 */
1171         stopBand = old_usb;
1172       } else {
1173         /* case 3 */
1174         stopBand = new_lsb;
1175       }
1176 
1177       target_lsb = 0;
1178       target_usb = old_lsb;
1179     } else {
1180       /* case 2 and 4 */
1181       source_exp = SCALE2EXP(hSbrDec->qmfDomainInCh->scaling.ov_lb_scale);
1182       target_exp = SCALE2EXP(hSbrDec->qmfDomainInCh->scaling.ov_hb_scale);
1183 
1184       startBand = new_lsb;
1185       stopBand = old_lsb;
1186 
1187       target_lsb = old_lsb;
1188       target_usb = old_usb;
1189     }
1190 
1191     maxVal =
1192         maxSubbandSample(OverlapBufferReal, (useLP) ? NULL : OverlapBufferImag,
1193                          startBand, stopBand, 0, startSlot);
1194 
1195     reserve = ((LONG)maxVal != 0 ? CntLeadingZeros(maxVal) - 1 : 0);
1196     reserve = fixMin(
1197         reserve,
1198         DFRACT_BITS - 1 -
1199             EXP2SCALE(
1200                 source_exp)); /* what is this line for, why do we need it? */
1201 
1202     /* process only if x-over-area is not dominant after rescale;
1203        otherwise I'm not sure if all buffers are scaled correctly;
1204     */
1205     if (target_exp - (source_exp - reserve) >= 0) {
1206       rescaleSubbandSamples(OverlapBufferReal,
1207                             (useLP) ? NULL : OverlapBufferImag, startBand,
1208                             stopBand, 0, startSlot, reserve);
1209       source_exp -= reserve;
1210     }
1211 
1212     delta_exp = target_exp - source_exp;
1213 
1214     if (delta_exp < 0) { /* x-over-area is dominant */
1215       startBand = target_lsb;
1216       stopBand = target_usb;
1217       delta_exp = -delta_exp;
1218 
1219       if (new_lsb > old_lsb) {
1220         /* The lowband has to be rescaled */
1221         hSbrDec->qmfDomainInCh->scaling.ov_lb_scale = EXP2SCALE(source_exp);
1222       } else {
1223         /* The highband has to be rescaled */
1224         hSbrDec->qmfDomainInCh->scaling.ov_hb_scale = EXP2SCALE(source_exp);
1225       }
1226     }
1227 
1228     FDK_ASSERT(startBand <= stopBand);
1229 
1230     if (!useLP) {
1231       for (l = 0; l < startSlot; l++) {
1232         scaleValues(OverlapBufferReal[l] + startBand, stopBand - startBand,
1233                     -delta_exp);
1234         scaleValues(OverlapBufferImag[l] + startBand, stopBand - startBand,
1235                     -delta_exp);
1236       }
1237     } else
1238       for (l = 0; l < startSlot; l++) {
1239         scaleValues(OverlapBufferReal[l] + startBand, stopBand - startBand,
1240                     -delta_exp);
1241       }
1242   } /* startSlot != 0 */
1243 
1244   /*
1245     Initialize transposer and limiter
1246   */
1247   sbrError = resetLppTransposer(
1248       &hSbrDec->LppTrans, hHeaderData->freqBandData.lowSubband,
1249       hHeaderData->freqBandData.v_k_master, hHeaderData->freqBandData.numMaster,
1250       hHeaderData->freqBandData.freqBandTableNoise,
1251       hHeaderData->freqBandData.nNfb, hHeaderData->freqBandData.highSubband,
1252       hHeaderData->sbrProcSmplRate);
1253   if (sbrError != SBRDEC_OK) return sbrError;
1254 
1255   hSbrDec->savedStates = 0;
1256 
1257   if ((flags & SBRDEC_USAC_HARMONICSBR) && applySbrProc) {
1258     sbrError = QmfTransposerReInit(hSbrDec->hHBE,
1259                                    hHeaderData->freqBandData.freqBandTable,
1260                                    hHeaderData->freqBandData.nSfb);
1261     if (sbrError != SBRDEC_OK) return sbrError;
1262 
1263     /* copy saved states from previous frame to legacy SBR lpc filterstate
1264      * buffer   */
1265     for (i = 0; i < LPC_ORDER + hSbrDec->LppTrans.pSettings->overlap; i++) {
1266       FDKmemcpy(
1267           hSbrDec->LppTrans.lpcFilterStatesRealLegSBR[i],
1268           hSbrDec->codecQMFBufferReal[hSbrDec->hHBE->noCols - LPC_ORDER -
1269                                       hSbrDec->LppTrans.pSettings->overlap + i],
1270           hSbrDec->hHBE->noChannels * sizeof(FIXP_DBL));
1271       FDKmemcpy(
1272           hSbrDec->LppTrans.lpcFilterStatesImagLegSBR[i],
1273           hSbrDec->codecQMFBufferImag[hSbrDec->hHBE->noCols - LPC_ORDER -
1274                                       hSbrDec->LppTrans.pSettings->overlap + i],
1275           hSbrDec->hHBE->noChannels * sizeof(FIXP_DBL));
1276     }
1277     hSbrDec->savedStates = 1;
1278 
1279     {
1280       /* map QMF buffer to pointer array (Overlap + Frame)*/
1281       for (i = 0; i < hSbrDec->LppTrans.pSettings->overlap + LPC_ORDER; i++) {
1282         pLowBandReal[i] = hSbrDec->LppTrans.lpcFilterStatesRealHBE[i];
1283         pLowBandImag[i] = hSbrDec->LppTrans.lpcFilterStatesImagHBE[i];
1284       }
1285 
1286       /* map QMF buffer to pointer array (Overlap + Frame)*/
1287       for (i = 0; i < hSbrDec->hHBE->noCols; i++) {
1288         pLowBandReal[i + hSbrDec->LppTrans.pSettings->overlap + LPC_ORDER] =
1289             hSbrDec->codecQMFBufferReal[i];
1290         pLowBandImag[i + hSbrDec->LppTrans.pSettings->overlap + LPC_ORDER] =
1291             hSbrDec->codecQMFBufferImag[i];
1292       }
1293 
1294       if (flags & SBRDEC_QUAD_RATE) {
1295         if (hFrameData->sbrPatchingMode == 0) {
1296           int *xOverQmf = GetxOverBandQmfTransposer(hSbrDec->hHBE);
1297 
1298           /* in case of harmonic SBR and no HBE_LP map additional buffer for
1299           one more frame to pointer arry */
1300           for (i = 0; i < hSbrDec->hHBE->noCols / 2; i++) {
1301             pLowBandReal[i + hSbrDec->hHBE->noCols +
1302                          hSbrDec->LppTrans.pSettings->overlap + LPC_ORDER] =
1303                 hSbrDec->hQmfHBESlotsReal[i];
1304             pLowBandImag[i + hSbrDec->hHBE->noCols +
1305                          hSbrDec->LppTrans.pSettings->overlap + LPC_ORDER] =
1306                 hSbrDec->hQmfHBESlotsImag[i];
1307           }
1308 
1309           QmfTransposerApply(
1310               hSbrDec->hHBE,
1311               pLowBandReal + hSbrDec->LppTrans.pSettings->overlap +
1312                   hSbrDec->hHBE->noCols / 2 + LPC_ORDER,
1313               pLowBandImag + hSbrDec->LppTrans.pSettings->overlap +
1314                   hSbrDec->hHBE->noCols / 2 + LPC_ORDER,
1315               hSbrDec->hHBE->noCols, pLowBandReal, pLowBandImag,
1316               hSbrDec->LppTrans.lpcFilterStatesRealHBE,
1317               hSbrDec->LppTrans.lpcFilterStatesImagHBE,
1318               hPrevFrameData->prevSbrPitchInBins, hSbrDec->scale_lb,
1319               hSbrDec->scale_hbe, &hSbrDec->qmfDomainInCh->scaling.hb_scale,
1320               hHeaderData->timeStep, hFrameData->frameInfo.borders[0],
1321               hSbrDec->LppTrans.pSettings->overlap, KEEP_STATES_SYNCED_OUTDIFF);
1322 
1323           copyHarmonicSpectrum(
1324               xOverQmf, pLowBandReal, pLowBandImag, hSbrDec->hHBE->noCols,
1325               hSbrDec->LppTrans.pSettings->overlap, KEEP_STATES_SYNCED_OUTDIFF);
1326         }
1327       } else {
1328         /* in case of harmonic SBR and no HBE_LP map additional buffer for
1329         one more frame to pointer arry */
1330         for (i = 0; i < hSbrDec->hHBE->noCols; i++) {
1331           pLowBandReal[i + hSbrDec->hHBE->noCols +
1332                        hSbrDec->LppTrans.pSettings->overlap + LPC_ORDER] =
1333               hSbrDec->hQmfHBESlotsReal[i];
1334           pLowBandImag[i + hSbrDec->hHBE->noCols +
1335                        hSbrDec->LppTrans.pSettings->overlap + LPC_ORDER] =
1336               hSbrDec->hQmfHBESlotsImag[i];
1337         }
1338 
1339         if (hFrameData->sbrPatchingMode == 0) {
1340           QmfTransposerApply(
1341               hSbrDec->hHBE,
1342               pLowBandReal + hSbrDec->LppTrans.pSettings->overlap + LPC_ORDER,
1343               pLowBandImag + hSbrDec->LppTrans.pSettings->overlap + LPC_ORDER,
1344               hSbrDec->hHBE->noCols, pLowBandReal, pLowBandImag,
1345               hSbrDec->LppTrans.lpcFilterStatesRealHBE,
1346               hSbrDec->LppTrans.lpcFilterStatesImagHBE,
1347               0 /* not required for keeping states updated in this frame*/,
1348               hSbrDec->scale_lb, hSbrDec->scale_lb,
1349               &hSbrDec->qmfDomainInCh->scaling.hb_scale, hHeaderData->timeStep,
1350               hFrameData->frameInfo.borders[0],
1351               hSbrDec->LppTrans.pSettings->overlap, KEEP_STATES_SYNCED_NOOUT);
1352         }
1353 
1354         QmfTransposerApply(
1355             hSbrDec->hHBE,
1356             pLowBandReal + hSbrDec->LppTrans.pSettings->overlap +
1357                 hSbrDec->hHBE->noCols + LPC_ORDER,
1358             pLowBandImag + hSbrDec->LppTrans.pSettings->overlap +
1359                 hSbrDec->hHBE->noCols + LPC_ORDER,
1360             hSbrDec->hHBE->noCols, pLowBandReal, pLowBandImag,
1361             hSbrDec->LppTrans.lpcFilterStatesRealHBE,
1362             hSbrDec->LppTrans.lpcFilterStatesImagHBE,
1363             hPrevFrameData->prevSbrPitchInBins, hSbrDec->scale_lb,
1364             hSbrDec->scale_hbe, &hSbrDec->qmfDomainInCh->scaling.hb_scale,
1365             hHeaderData->timeStep, hFrameData->frameInfo.borders[0],
1366             hSbrDec->LppTrans.pSettings->overlap, KEEP_STATES_SYNCED_OUTDIFF);
1367       }
1368 
1369       if (hFrameData->sbrPatchingMode == 0) {
1370         for (i = startSlot; i < hSbrDec->LppTrans.pSettings->overlap; i++) {
1371           /*
1372           Store the unmodified qmf Slots values for upper part of spectrum
1373           (required for LPC filtering) required if next frame is a HBE frame
1374           */
1375           FDKmemcpy(hSbrDec->qmfDomainInCh->hQmfSlotsReal[i],
1376                     hSbrDec->LppTrans.lpcFilterStatesRealHBE[i + LPC_ORDER],
1377                     (64) * sizeof(FIXP_DBL));
1378           FDKmemcpy(hSbrDec->qmfDomainInCh->hQmfSlotsImag[i],
1379                     hSbrDec->LppTrans.lpcFilterStatesImagHBE[i + LPC_ORDER],
1380                     (64) * sizeof(FIXP_DBL));
1381         }
1382 
1383         for (i = startSlot; i < hSbrDec->LppTrans.pSettings->overlap; i++) {
1384           /*
1385           Store the unmodified qmf Slots values for upper part of spectrum
1386           (required for LPC filtering) required if next frame is a HBE frame
1387           */
1388           FDKmemcpy(
1389               hSbrDec->qmfDomainInCh->hQmfSlotsReal[i],
1390               hSbrDec->codecQMFBufferReal[hSbrDec->hHBE->noCols -
1391                                           hSbrDec->LppTrans.pSettings->overlap +
1392                                           i],
1393               new_lsb * sizeof(FIXP_DBL));
1394           FDKmemcpy(
1395               hSbrDec->qmfDomainInCh->hQmfSlotsImag[i],
1396               hSbrDec->codecQMFBufferImag[hSbrDec->hHBE->noCols -
1397                                           hSbrDec->LppTrans.pSettings->overlap +
1398                                           i],
1399               new_lsb * sizeof(FIXP_DBL));
1400         }
1401       }
1402     }
1403   }
1404 
1405   {
1406     int adapt_lb = 0, diff = 0,
1407         new_scale = hSbrDec->qmfDomainInCh->scaling.ov_lb_scale;
1408 
1409     if ((hSbrDec->qmfDomainInCh->scaling.ov_lb_scale !=
1410          hSbrDec->qmfDomainInCh->scaling.lb_scale) &&
1411         startSlot != 0) {
1412       /* we need to adapt spectrum to have equal scale factor, always larger
1413        * than zero */
1414       diff = SCALE2EXP(hSbrDec->qmfDomainInCh->scaling.ov_lb_scale) -
1415              SCALE2EXP(hSbrDec->qmfDomainInCh->scaling.lb_scale);
1416 
1417       if (diff > 0) {
1418         adapt_lb = 1;
1419         diff = -diff;
1420         new_scale = hSbrDec->qmfDomainInCh->scaling.ov_lb_scale;
1421       }
1422 
1423       stopBand = new_lsb;
1424     }
1425 
1426     if (hFrameData->sbrPatchingMode == 1) {
1427       /* scale states from LegSBR filterstates buffer */
1428       for (i = 0; i < hSbrDec->LppTrans.pSettings->overlap + LPC_ORDER; i++) {
1429         scaleValues(hSbrDec->LppTrans.lpcFilterStatesRealLegSBR[i], new_lsb,
1430                     diff);
1431         if (!useLP) {
1432           scaleValues(hSbrDec->LppTrans.lpcFilterStatesImagLegSBR[i], new_lsb,
1433                       diff);
1434         }
1435       }
1436 
1437       if (flags & SBRDEC_SYNTAX_USAC) {
1438         /* get missing states between old and new x_over from LegSBR
1439          * filterstates buffer */
1440         /* in case of legacy SBR we leave these values zeroed out */
1441         for (i = startSlot; i < hSbrDec->LppTrans.pSettings->overlap; i++) {
1442           FDKmemcpy(&OverlapBufferReal[i][old_lsb],
1443                     &hSbrDec->LppTrans
1444                          .lpcFilterStatesRealLegSBR[LPC_ORDER + i][old_lsb],
1445                     fMax(new_lsb - old_lsb, 0) * sizeof(FIXP_DBL));
1446           if (!useLP) {
1447             FDKmemcpy(&OverlapBufferImag[i][old_lsb],
1448                       &hSbrDec->LppTrans
1449                            .lpcFilterStatesImagLegSBR[LPC_ORDER + i][old_lsb],
1450                       fMax(new_lsb - old_lsb, 0) * sizeof(FIXP_DBL));
1451           }
1452         }
1453       }
1454 
1455       if (new_lsb > old_lsb) {
1456         stopBand = old_lsb;
1457       }
1458     }
1459     if ((adapt_lb == 1) && (stopBand > startBand)) {
1460       for (l = startSlot; l < hSbrDec->LppTrans.pSettings->overlap; l++) {
1461         scaleValues(OverlapBufferReal[l] + startBand, stopBand - startBand,
1462                     diff);
1463         if (!useLP) {
1464           scaleValues(OverlapBufferImag[l] + startBand, stopBand - startBand,
1465                       diff);
1466         }
1467       }
1468     }
1469     hSbrDec->qmfDomainInCh->scaling.ov_lb_scale = new_scale;
1470   }
1471 
1472   sbrError = ResetLimiterBands(hHeaderData->freqBandData.limiterBandTable,
1473                                &hHeaderData->freqBandData.noLimiterBands,
1474                                hHeaderData->freqBandData.freqBandTable[0],
1475                                hHeaderData->freqBandData.nSfb[0],
1476                                hSbrDec->LppTrans.pSettings->patchParam,
1477                                hSbrDec->LppTrans.pSettings->noOfPatches,
1478                                hHeaderData->bs_data.limiterBands,
1479                                hFrameData->sbrPatchingMode,
1480                                GetxOverBandQmfTransposer(hSbrDec->hHBE),
1481                                Get41SbrQmfTransposer(hSbrDec->hHBE));
1482 
1483   hSbrDec->SbrCalculateEnvelope.sbrPatchingMode = hFrameData->sbrPatchingMode;
1484 
1485   return sbrError;
1486 }
1487