Coverage Report

Created: 2026-01-17 06:32

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/aac/libSBRdec/src/lpp_tran.cpp
Line
Count
Source
1
/* -----------------------------------------------------------------------------
2
Software License for The Fraunhofer FDK AAC Codec Library for Android
3
4
© Copyright  1995 - 2021 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  Low Power Profile Transposer
106
  This module provides the transposer. The main entry point is lppTransposer().
107
  The function generates high frequency content by copying data from the low
108
  band (provided by core codec) into the high band. This process is also
109
  referred to as "patching". The function also implements spectral whitening by
110
  means of inverse filtering based on LPC coefficients.
111
112
  Together with the QMF filterbank the transposer can be tested using a supplied
113
  test program. See main_audio.cpp for details. This module does use fractional
114
  arithmetic and the accuracy of the computations has an impact on the overall
115
  sound quality. The module also needs to take into account the different
116
  scaling of spectral data.
117
118
  \sa lppTransposer(), main_audio.cpp, sbr_scale.h, \ref documentationOverview
119
*/
120
121
#if __has_include(<android/ndk-version.h>)
122
#include <android/ndk-version.h>
123
#endif
124
125
#if defined __ANDROID__ && !defined __ANDROID_NDK__
126
#include "log/log.h"
127
#endif
128
129
#include "lpp_tran.h"
130
131
#include "sbr_ram.h"
132
#include "sbr_rom.h"
133
134
#include "genericStds.h"
135
#include "autocorr2nd.h"
136
137
#include "HFgen_preFlat.h"
138
139
23.1M
#define LPC_SCALE_FACTOR 2
140
141
/*!
142
 *
143
 * \brief Get bandwidth expansion factor from filtering level
144
 *
145
 * Returns a filter parameter (bandwidth expansion factor) depending on
146
 * the desired filtering level signalled in the bitstream.
147
 * When switching the filtering level from LOW to OFF, an additional
148
 * level is being inserted to achieve a smooth transition.
149
 */
150
151
static FIXP_DBL mapInvfMode(INVF_MODE mode, INVF_MODE prevMode,
152
720k
                            WHITENING_FACTORS whFactors) {
153
720k
  switch (mode) {
154
227k
    case INVF_LOW_LEVEL:
155
227k
      if (prevMode == INVF_OFF)
156
26.5k
        return whFactors.transitionLevel;
157
200k
      else
158
200k
        return whFactors.lowLevel;
159
160
45.4k
    case INVF_MID_LEVEL:
161
45.4k
      return whFactors.midLevel;
162
163
102k
    case INVF_HIGH_LEVEL:
164
102k
      return whFactors.highLevel;
165
166
345k
    default:
167
345k
      if (prevMode == INVF_LOW_LEVEL)
168
18.8k
        return whFactors.transitionLevel;
169
326k
      else
170
326k
        return whFactors.off;
171
720k
  }
172
720k
}
173
174
/*!
175
 *
176
 * \brief Perform inverse filtering level emphasis
177
 *
178
 * Retrieve bandwidth expansion factor and apply smoothing for each filter band
179
 *
180
 */
181
182
static void inverseFilteringLevelEmphasis(
183
    HANDLE_SBR_LPP_TRANS hLppTrans, /*!< Handle of lpp transposer  */
184
    UCHAR nInvfBands,              /*!< Number of bands for inverse filtering */
185
    INVF_MODE *sbr_invf_mode,      /*!< Current inverse filtering modes */
186
    INVF_MODE *sbr_invf_mode_prev, /*!< Previous inverse filtering modes */
187
    FIXP_DBL *bwVector             /*!< Resulting filtering levels */
188
298k
) {
189
1.01M
  for (int i = 0; i < nInvfBands; i++) {
190
720k
    FIXP_DBL accu;
191
720k
    FIXP_DBL bwTmp = mapInvfMode(sbr_invf_mode[i], sbr_invf_mode_prev[i],
192
720k
                                 hLppTrans->pSettings->whFactors);
193
194
720k
    if (bwTmp < hLppTrans->bwVectorOld[i]) {
195
99.9k
      accu = fMultDiv2(FL2FXCONST_DBL(0.75f), bwTmp) +
196
99.9k
             fMultDiv2(FL2FXCONST_DBL(0.25f), hLppTrans->bwVectorOld[i]);
197
620k
    } else {
198
620k
      accu = fMultDiv2(FL2FXCONST_DBL(0.90625f), bwTmp) +
199
620k
             fMultDiv2(FL2FXCONST_DBL(0.09375f), hLppTrans->bwVectorOld[i]);
200
620k
    }
201
202
720k
    if (accu<FL2FXCONST_DBL(0.015625f)>> 1) {
203
278k
      bwVector[i] = FL2FXCONST_DBL(0.0f);
204
442k
    } else {
205
442k
      bwVector[i] = fixMin(accu << 1, FL2FXCONST_DBL(0.99609375f));
206
442k
    }
207
720k
  }
208
298k
}
209
210
/* Resulting autocorrelation determinant exponent */
211
#define ACDET_EXP \
212
  (2 * (DFRACT_BITS + sbrScaleFactor->lb_scale + 10 - ac.det_scale))
213
#define AC_EXP (-sbrScaleFactor->lb_scale + LPC_SCALE_FACTOR)
214
#define ALPHA_EXP (-sbrScaleFactor->lb_scale + LPC_SCALE_FACTOR + 1)
215
/* Resulting transposed QMF values exponent 16 bit normalized samplebits
216
 * assumed. */
217
#define QMFOUT_EXP ((SAMPLE_BITS - 15) - sbrScaleFactor->lb_scale)
218
219
static inline void calc_qmfBufferReal(FIXP_DBL **qmfBufferReal,
220
                                      const FIXP_DBL *const lowBandReal,
221
                                      const int startSample,
222
                                      const int stopSample, const UCHAR hiBand,
223
                                      const int dynamicScale,
224
89.7k
                                      const FIXP_SGL a0r, const FIXP_SGL a1r) {
225
89.7k
  const int dynscale = fixMax(0, dynamicScale - 1) + 1;
226
89.7k
  const int rescale = -fixMin(0, dynamicScale - 1) + 1;
227
89.7k
  const int descale =
228
89.7k
      fixMin(DFRACT_BITS - 1, LPC_SCALE_FACTOR + dynamicScale + rescale);
229
230
2.24M
  for (int i = 0; i < stopSample - startSample; i++) {
231
2.15M
    FIXP_DBL accu;
232
233
2.15M
    accu = fMultDiv2(a1r, lowBandReal[i]) + fMultDiv2(a0r, lowBandReal[i + 1]);
234
2.15M
    accu = (lowBandReal[i + 2] >> descale) + (accu >> dynscale);
235
236
2.15M
    qmfBufferReal[i + startSample][hiBand] =
237
2.15M
        SATURATE_LEFT_SHIFT(accu, rescale, DFRACT_BITS);
238
2.15M
  }
239
89.7k
}
240
241
/*!
242
 *
243
 * \brief Perform transposition by patching of subband samples.
244
 * This function serves as the main entry point into the module. The function
245
 * determines the areas for the patching process (these are the source range as
246
 * well as the target range) and implements spectral whitening by means of
247
 * inverse filtering. The function autoCorrelation2nd() is an auxiliary function
248
 * for calculating the LPC coefficients for the filtering.  The actual
249
 * calculation of the LPC coefficients and the implementation of the filtering
250
 * are done as part of lppTransposer().
251
 *
252
 * Note that the filtering is done on all available QMF subsamples, whereas the
253
 * patching is only done on those QMF subsamples that will be used in the next
254
 * QMF synthesis. The filtering is also implemented before the patching includes
255
 * further dependencies on parameters from the SBR data.
256
 *
257
 */
258
259
void lppTransposer(
260
    HANDLE_SBR_LPP_TRANS hLppTrans,   /*!< Handle of lpp transposer  */
261
    QMF_SCALE_FACTOR *sbrScaleFactor, /*!< Scaling factors */
262
    FIXP_DBL **qmfBufferReal, /*!< Pointer to pointer to real part of subband
263
                                 samples (source) */
264
265
    FIXP_DBL *degreeAlias,    /*!< Vector for results of aliasing estimation */
266
    FIXP_DBL **qmfBufferImag, /*!< Pointer to pointer to imaginary part of
267
                                 subband samples (source) */
268
    const int useLP, const int fPreWhitening, const int v_k_master0,
269
    const int timeStep,       /*!< Time step of envelope */
270
    const int firstSlotOffs,  /*!< Start position in time */
271
    const int lastSlotOffs,   /*!< Number of overlap-slots into next frame */
272
    const int nInvfBands,     /*!< Number of bands for inverse filtering */
273
    INVF_MODE *sbr_invf_mode, /*!< Current inverse filtering modes */
274
    INVF_MODE *sbr_invf_mode_prev /*!< Previous inverse filtering modes */
275
256k
) {
276
256k
  INT bwIndex[MAX_NUM_PATCHES];
277
256k
  FIXP_DBL bwVector[MAX_NUM_PATCHES]; /*!< pole moving factors */
278
256k
  FIXP_DBL preWhiteningGains[(64) / 2];
279
256k
  int preWhiteningGains_exp[(64) / 2];
280
281
256k
  int i;
282
256k
  int loBand, start, stop;
283
256k
  TRANSPOSER_SETTINGS *pSettings = hLppTrans->pSettings;
284
256k
  PATCH_PARAM *patchParam = pSettings->patchParam;
285
256k
  int patch;
286
287
256k
  FIXP_SGL alphar[LPC_ORDER], a0r, a1r;
288
256k
  FIXP_SGL alphai[LPC_ORDER], a0i = 0, a1i = 0;
289
256k
  FIXP_SGL bw = FL2FXCONST_SGL(0.0f);
290
291
256k
  int autoCorrLength;
292
293
256k
  FIXP_DBL k1, k1_below = 0, k1_below2 = 0;
294
295
256k
  ACORR_COEFS ac;
296
256k
  int startSample;
297
256k
  int stopSample;
298
256k
  int stopSampleClear;
299
300
256k
  int comLowBandScale;
301
256k
  int ovLowBandShift;
302
256k
  int lowBandShift;
303
  /*  int ovHighBandShift;*/
304
305
256k
  alphai[0] = FL2FXCONST_SGL(0.0f);
306
256k
  alphai[1] = FL2FXCONST_SGL(0.0f);
307
308
256k
  startSample = firstSlotOffs * timeStep;
309
256k
  stopSample = pSettings->nCols + lastSlotOffs * timeStep;
310
256k
  FDK_ASSERT((lastSlotOffs * timeStep) <= pSettings->overlap);
311
312
256k
  inverseFilteringLevelEmphasis(hLppTrans, nInvfBands, sbr_invf_mode,
313
256k
                                sbr_invf_mode_prev, bwVector);
314
315
256k
  stopSampleClear = stopSample;
316
317
256k
  autoCorrLength = pSettings->nCols + pSettings->overlap;
318
319
256k
  if (pSettings->noOfPatches > 0) {
320
    /* Set upper subbands to zero:
321
       This is required in case that the patches do not cover the complete
322
       highband (because the last patch would be too short). Possible
323
       optimization: Clearing bands up to usb would be sufficient here. */
324
256k
    int targetStopBand =
325
256k
        patchParam[pSettings->noOfPatches - 1].targetStartBand +
326
256k
        patchParam[pSettings->noOfPatches - 1].numBandsInPatch;
327
328
256k
    int memSize = ((64) - targetStopBand) * sizeof(FIXP_DBL);
329
330
256k
    if (!useLP) {
331
7.55M
      for (i = startSample; i < stopSampleClear; i++) {
332
7.33M
        FDKmemclear(&qmfBufferReal[i][targetStopBand], memSize);
333
7.33M
        FDKmemclear(&qmfBufferImag[i][targetStopBand], memSize);
334
7.33M
      }
335
218k
    } else {
336
1.01M
      for (i = startSample; i < stopSampleClear; i++) {
337
978k
        FDKmemclear(&qmfBufferReal[i][targetStopBand], memSize);
338
978k
      }
339
37.0k
    }
340
256k
  }
341
#if defined __ANDROID__ && !defined __ANDROID_NDK__
342
  else {
343
    // Safetynet logging
344
    android_errorWriteLog(0x534e4554, "112160868");
345
  }
346
#endif
347
348
  /* init bwIndex for each patch */
349
256k
  FDKmemclear(bwIndex, sizeof(bwIndex));
350
351
  /*
352
    Calc common low band scale factor
353
  */
354
256k
  comLowBandScale =
355
256k
      fixMin(sbrScaleFactor->ov_lb_scale, sbrScaleFactor->lb_scale);
356
357
256k
  ovLowBandShift = sbrScaleFactor->ov_lb_scale - comLowBandScale;
358
256k
  lowBandShift = sbrScaleFactor->lb_scale - comLowBandScale;
359
  /*  ovHighBandShift = firstSlotOffs == 0 ? ovLowBandShift:0;*/
360
361
256k
  if (fPreWhitening) {
362
36.0k
    sbrDecoder_calculateGainVec(
363
36.0k
        qmfBufferReal, qmfBufferImag,
364
36.0k
        DFRACT_BITS - 1 - 16 -
365
36.0k
            sbrScaleFactor->ov_lb_scale, /* convert scale to exponent */
366
36.0k
        DFRACT_BITS - 1 - 16 -
367
36.0k
            sbrScaleFactor->lb_scale, /* convert scale to exponent */
368
36.0k
        pSettings->overlap, preWhiteningGains, preWhiteningGains_exp,
369
36.0k
        v_k_master0, startSample, stopSample);
370
36.0k
  }
371
372
  /* outer loop over bands to do analysis only once for each band */
373
374
256k
  if (!useLP) {
375
218k
    start = pSettings->lbStartPatching;
376
218k
    stop = pSettings->lbStopPatching;
377
218k
  } else {
378
37.0k
    start = fixMax(1, pSettings->lbStartPatching - 2);
379
37.0k
    stop = patchParam[0].targetStartBand;
380
37.0k
  }
381
382
3.88M
  for (loBand = start; loBand < stop; loBand++) {
383
3.63M
    FIXP_DBL lowBandReal[(((1024) / (32) * (4) / 2) + (3 * (4))) + LPC_ORDER];
384
3.63M
    FIXP_DBL *plowBandReal = lowBandReal;
385
3.63M
    FIXP_DBL **pqmfBufferReal =
386
3.63M
        qmfBufferReal + firstSlotOffs * timeStep /* + pSettings->overlap */;
387
3.63M
    FIXP_DBL lowBandImag[(((1024) / (32) * (4) / 2) + (3 * (4))) + LPC_ORDER];
388
3.63M
    FIXP_DBL *plowBandImag = lowBandImag;
389
3.63M
    FIXP_DBL **pqmfBufferImag =
390
3.63M
        qmfBufferImag + firstSlotOffs * timeStep /* + pSettings->overlap */;
391
3.63M
    int resetLPCCoeffs = 0;
392
3.63M
    int dynamicScale = DFRACT_BITS - 1 - LPC_SCALE_FACTOR;
393
3.63M
    int acDetScale = 0; /* scaling of autocorrelation determinant */
394
395
3.63M
    for (i = 0;
396
18.9M
         i < LPC_ORDER + firstSlotOffs * timeStep /*+pSettings->overlap*/;
397
15.3M
         i++) {
398
15.3M
      *plowBandReal++ = hLppTrans->lpcFilterStatesRealLegSBR[i][loBand];
399
15.3M
      if (!useLP)
400
13.6M
        *plowBandImag++ = hLppTrans->lpcFilterStatesImagLegSBR[i][loBand];
401
15.3M
    }
402
403
    /*
404
      Take old slope length qmf slot source values out of (overlap)qmf buffer
405
    */
406
3.63M
    if (!useLP) {
407
2.90M
      for (i = 0;
408
106M
           i < pSettings->nCols + pSettings->overlap - firstSlotOffs * timeStep;
409
104M
           i++) {
410
104M
        *plowBandReal++ = (*pqmfBufferReal++)[loBand];
411
104M
        *plowBandImag++ = (*pqmfBufferImag++)[loBand];
412
104M
      }
413
2.90M
    } else {
414
      /* pSettings->overlap is always even */
415
723k
      FDK_ASSERT((pSettings->overlap & 1) == 0);
416
12.5M
      for (i = 0; i < ((pSettings->nCols + pSettings->overlap -
417
12.5M
                        firstSlotOffs * timeStep) >>
418
12.5M
                       1);
419
11.8M
           i++) {
420
11.8M
        *plowBandReal++ = (*pqmfBufferReal++)[loBand];
421
11.8M
        *plowBandReal++ = (*pqmfBufferReal++)[loBand];
422
11.8M
      }
423
723k
      if (pSettings->nCols & 1) {
424
110k
        *plowBandReal++ = (*pqmfBufferReal++)[loBand];
425
110k
      }
426
723k
    }
427
428
    /*
429
      Determine dynamic scaling value.
430
     */
431
3.63M
    dynamicScale =
432
3.63M
        fixMin(dynamicScale,
433
3.63M
               getScalefactor(lowBandReal, LPC_ORDER + pSettings->overlap) +
434
3.63M
                   ovLowBandShift);
435
3.63M
    dynamicScale =
436
3.63M
        fixMin(dynamicScale,
437
3.63M
               getScalefactor(&lowBandReal[LPC_ORDER + pSettings->overlap],
438
3.63M
                              pSettings->nCols) +
439
3.63M
                   lowBandShift);
440
3.63M
    if (!useLP) {
441
2.90M
      dynamicScale =
442
2.90M
          fixMin(dynamicScale,
443
2.90M
                 getScalefactor(lowBandImag, LPC_ORDER + pSettings->overlap) +
444
2.90M
                     ovLowBandShift);
445
2.90M
      dynamicScale =
446
2.90M
          fixMin(dynamicScale,
447
2.90M
                 getScalefactor(&lowBandImag[LPC_ORDER + pSettings->overlap],
448
2.90M
                                pSettings->nCols) +
449
2.90M
                     lowBandShift);
450
2.90M
    }
451
452
3.63M
    if (dynamicScale == 0) {
453
      /* In this special case the available headroom bits as well as
454
         ovLowBandShift and lowBandShift are zero. The spectrum is limited to
455
         prevent -1.0, so negative values for dynamicScale can be avoided. */
456
5.11M
      for (i = 0; i < (LPC_ORDER + pSettings->overlap + pSettings->nCols);
457
4.99M
           i++) {
458
4.99M
        lowBandReal[i] = fixMax(lowBandReal[i], (FIXP_DBL)0x80000001);
459
4.99M
      }
460
126k
      if (!useLP) {
461
4.99M
        for (i = 0; i < (LPC_ORDER + pSettings->overlap + pSettings->nCols);
462
4.87M
             i++) {
463
4.87M
          lowBandImag[i] = fixMax(lowBandImag[i], (FIXP_DBL)0x80000001);
464
4.87M
        }
465
121k
      }
466
3.50M
    } else {
467
3.50M
      dynamicScale =
468
3.50M
          fixMax(0, dynamicScale -
469
3.50M
                        1); /* one additional bit headroom to prevent -1.0 */
470
3.50M
    }
471
472
    /*
473
      Scale temporal QMF buffer.
474
     */
475
3.63M
    scaleValues(&lowBandReal[0], LPC_ORDER + pSettings->overlap,
476
3.63M
                dynamicScale - ovLowBandShift);
477
3.63M
    scaleValues(&lowBandReal[LPC_ORDER + pSettings->overlap], pSettings->nCols,
478
3.63M
                dynamicScale - lowBandShift);
479
480
3.63M
    if (!useLP) {
481
2.90M
      scaleValues(&lowBandImag[0], LPC_ORDER + pSettings->overlap,
482
2.90M
                  dynamicScale - ovLowBandShift);
483
2.90M
      scaleValues(&lowBandImag[LPC_ORDER + pSettings->overlap],
484
2.90M
                  pSettings->nCols, dynamicScale - lowBandShift);
485
2.90M
    }
486
487
3.63M
    if (!useLP) {
488
2.90M
      acDetScale += autoCorr2nd_cplx(&ac, lowBandReal + LPC_ORDER,
489
2.90M
                                     lowBandImag + LPC_ORDER, autoCorrLength);
490
2.90M
    } else {
491
723k
      acDetScale +=
492
723k
          autoCorr2nd_real(&ac, lowBandReal + LPC_ORDER, autoCorrLength);
493
723k
    }
494
495
    /* Examine dynamic of determinant in autocorrelation. */
496
3.63M
    acDetScale += 2 * (comLowBandScale + dynamicScale);
497
3.63M
    acDetScale *= 2;            /* two times reflection coefficent scaling */
498
3.63M
    acDetScale += ac.det_scale; /* ac scaling of determinant */
499
500
    /* In case of determinant < 10^-38, resetLPCCoeffs=1 has to be enforced. */
501
3.63M
    if (acDetScale > 126) {
502
1.25M
      resetLPCCoeffs = 1;
503
1.25M
    }
504
505
3.63M
    alphar[1] = FL2FXCONST_SGL(0.0f);
506
3.63M
    if (!useLP) alphai[1] = FL2FXCONST_SGL(0.0f);
507
508
3.63M
    if (ac.det != FL2FXCONST_DBL(0.0f)) {
509
2.36M
      FIXP_DBL tmp, absTmp, absDet;
510
511
2.36M
      absDet = fixp_abs(ac.det);
512
513
2.36M
      if (!useLP) {
514
2.23M
        tmp = (fMultDiv2(ac.r01r, ac.r12r) >> (LPC_SCALE_FACTOR - 1)) -
515
2.23M
              ((fMultDiv2(ac.r01i, ac.r12i) + fMultDiv2(ac.r02r, ac.r11r)) >>
516
2.23M
               (LPC_SCALE_FACTOR - 1));
517
2.23M
      } else {
518
129k
        tmp = (fMultDiv2(ac.r01r, ac.r12r) >> (LPC_SCALE_FACTOR - 1)) -
519
129k
              (fMultDiv2(ac.r02r, ac.r11r) >> (LPC_SCALE_FACTOR - 1));
520
129k
      }
521
2.36M
      absTmp = fixp_abs(tmp);
522
523
      /*
524
        Quick check: is first filter coeff >= 1(4)
525
       */
526
2.36M
      {
527
2.36M
        INT scale;
528
2.36M
        FIXP_DBL result = fDivNorm(absTmp, absDet, &scale);
529
2.36M
        scale = scale + ac.det_scale;
530
531
2.36M
        if ((scale > 0) && (result >= (FIXP_DBL)MAXVAL_DBL >> scale)) {
532
7.85k
          resetLPCCoeffs = 1;
533
2.35M
        } else {
534
2.35M
          alphar[1] = FX_DBL2FX_SGL(scaleValueSaturate(result, scale));
535
2.35M
          if ((tmp < FL2FX_DBL(0.0f)) ^ (ac.det < FL2FX_DBL(0.0f))) {
536
2.06M
            alphar[1] = -alphar[1];
537
2.06M
          }
538
2.35M
        }
539
2.36M
      }
540
541
2.36M
      if (!useLP) {
542
2.23M
        tmp = (fMultDiv2(ac.r01i, ac.r12r) >> (LPC_SCALE_FACTOR - 1)) +
543
2.23M
              ((fMultDiv2(ac.r01r, ac.r12i) -
544
2.23M
                (FIXP_DBL)fMultDiv2(ac.r02i, ac.r11r)) >>
545
2.23M
               (LPC_SCALE_FACTOR - 1));
546
547
2.23M
        absTmp = fixp_abs(tmp);
548
549
        /*
550
        Quick check: is second filter coeff >= 1(4)
551
        */
552
2.23M
        {
553
2.23M
          INT scale;
554
2.23M
          FIXP_DBL result = fDivNorm(absTmp, absDet, &scale);
555
2.23M
          scale = scale + ac.det_scale;
556
557
2.23M
          if ((scale > 0) &&
558
144k
              (result >= /*FL2FXCONST_DBL(1.f)*/ (FIXP_DBL)MAXVAL_DBL >>
559
144k
               scale)) {
560
3.89k
            resetLPCCoeffs = 1;
561
2.23M
          } else {
562
2.23M
            alphai[1] = FX_DBL2FX_SGL(scaleValueSaturate(result, scale));
563
2.23M
            if ((tmp < FL2FX_DBL(0.0f)) ^ (ac.det < FL2FX_DBL(0.0f))) {
564
823k
              alphai[1] = -alphai[1];
565
823k
            }
566
2.23M
          }
567
2.23M
        }
568
2.23M
      }
569
2.36M
    }
570
571
3.63M
    alphar[0] = FL2FXCONST_SGL(0.0f);
572
3.63M
    if (!useLP) alphai[0] = FL2FXCONST_SGL(0.0f);
573
574
3.63M
    if (ac.r11r != FL2FXCONST_DBL(0.0f)) {
575
      /* ac.r11r is always >=0 */
576
2.36M
      FIXP_DBL tmp, absTmp;
577
578
2.36M
      if (!useLP) {
579
2.23M
        tmp = (ac.r01r >> (LPC_SCALE_FACTOR + 1)) +
580
2.23M
              (fMultDiv2(alphar[1], ac.r12r) + fMultDiv2(alphai[1], ac.r12i));
581
2.23M
      } else {
582
130k
        if (ac.r01r >= FL2FXCONST_DBL(0.0f))
583
80.3k
          tmp = (ac.r01r >> (LPC_SCALE_FACTOR + 1)) +
584
80.3k
                fMultDiv2(alphar[1], ac.r12r);
585
50.2k
        else
586
50.2k
          tmp = -((-ac.r01r) >> (LPC_SCALE_FACTOR + 1)) +
587
50.2k
                fMultDiv2(alphar[1], ac.r12r);
588
130k
      }
589
590
2.36M
      absTmp = fixp_abs(tmp);
591
592
      /*
593
        Quick check: is first filter coeff >= 1(4)
594
      */
595
596
2.36M
      if (absTmp >= (ac.r11r >> 1)) {
597
3.55k
        resetLPCCoeffs = 1;
598
2.36M
      } else {
599
2.36M
        INT scale;
600
2.36M
        FIXP_DBL result = fDivNorm(absTmp, fixp_abs(ac.r11r), &scale);
601
2.36M
        alphar[0] = FX_DBL2FX_SGL(scaleValueSaturate(result, scale + 1));
602
603
2.36M
        if ((tmp > FL2FX_DBL(0.0f)) ^ (ac.r11r < FL2FX_DBL(0.0f)))
604
1.30M
          alphar[0] = -alphar[0];
605
2.36M
      }
606
607
2.36M
      if (!useLP) {
608
2.23M
        tmp = (ac.r01i >> (LPC_SCALE_FACTOR + 1)) +
609
2.23M
              (fMultDiv2(alphai[1], ac.r12r) - fMultDiv2(alphar[1], ac.r12i));
610
611
2.23M
        absTmp = fixp_abs(tmp);
612
613
        /*
614
        Quick check: is second filter coeff >= 1(4)
615
        */
616
2.23M
        if (absTmp >= (ac.r11r >> 1)) {
617
3.03k
          resetLPCCoeffs = 1;
618
2.23M
        } else {
619
2.23M
          INT scale;
620
2.23M
          FIXP_DBL result = fDivNorm(absTmp, fixp_abs(ac.r11r), &scale);
621
2.23M
          alphai[0] = FX_DBL2FX_SGL(scaleValueSaturate(result, scale + 1));
622
2.23M
          if ((tmp > FL2FX_DBL(0.0f)) ^ (ac.r11r < FL2FX_DBL(0.0f)))
623
1.01M
            alphai[0] = -alphai[0];
624
2.23M
        }
625
2.23M
      }
626
2.36M
    }
627
628
3.63M
    if (!useLP) {
629
      /* Now check the quadratic criteria */
630
2.90M
      if ((fMultDiv2(alphar[0], alphar[0]) + fMultDiv2(alphai[0], alphai[0])) >=
631
2.90M
          FL2FXCONST_DBL(0.5f))
632
717
        resetLPCCoeffs = 1;
633
2.90M
      if ((fMultDiv2(alphar[1], alphar[1]) + fMultDiv2(alphai[1], alphai[1])) >=
634
2.90M
          FL2FXCONST_DBL(0.5f))
635
741
        resetLPCCoeffs = 1;
636
2.90M
    }
637
638
3.63M
    if (resetLPCCoeffs) {
639
1.26M
      alphar[0] = FL2FXCONST_SGL(0.0f);
640
1.26M
      alphar[1] = FL2FXCONST_SGL(0.0f);
641
1.26M
      if (!useLP) {
642
673k
        alphai[0] = FL2FXCONST_SGL(0.0f);
643
673k
        alphai[1] = FL2FXCONST_SGL(0.0f);
644
673k
      }
645
1.26M
    }
646
647
3.63M
    if (useLP) {
648
      /* Aliasing detection */
649
723k
      if (ac.r11r == FL2FXCONST_DBL(0.0f)) {
650
592k
        k1 = FL2FXCONST_DBL(0.0f);
651
592k
      } else {
652
130k
        if (fixp_abs(ac.r01r) >= fixp_abs(ac.r11r)) {
653
4.86k
          if (fMultDiv2(ac.r01r, ac.r11r) < FL2FX_DBL(0.0f)) {
654
1.18k
            k1 = (FIXP_DBL)MAXVAL_DBL /*FL2FXCONST_SGL(1.0f)*/;
655
3.67k
          } else {
656
            /* Since this value is squared later, it must not ever become -1.0f.
657
             */
658
3.67k
            k1 = (FIXP_DBL)(MINVAL_DBL + 1) /*FL2FXCONST_SGL(-1.0f)*/;
659
3.67k
          }
660
125k
        } else {
661
125k
          INT scale;
662
125k
          FIXP_DBL result =
663
125k
              fDivNorm(fixp_abs(ac.r01r), fixp_abs(ac.r11r), &scale);
664
125k
          k1 = scaleValueSaturate(result, scale);
665
666
125k
          if (!((ac.r01r < FL2FX_DBL(0.0f)) ^ (ac.r11r < FL2FX_DBL(0.0f)))) {
667
76.6k
            k1 = -k1;
668
76.6k
          }
669
125k
        }
670
130k
      }
671
723k
      if ((loBand > 1) && (loBand < v_k_master0)) {
672
        /* Check if the gain should be locked */
673
597k
        FIXP_DBL deg =
674
597k
            /*FL2FXCONST_DBL(1.0f)*/ (FIXP_DBL)MAXVAL_DBL - fPow2(k1_below);
675
597k
        degreeAlias[loBand] = FL2FXCONST_DBL(0.0f);
676
597k
        if (((loBand & 1) == 0) && (k1 < FL2FXCONST_DBL(0.0f))) {
677
33.3k
          if (k1_below < FL2FXCONST_DBL(0.0f)) { /* 2-Ch Aliasing Detection */
678
16.9k
            degreeAlias[loBand] = (FIXP_DBL)MAXVAL_DBL /*FL2FXCONST_DBL(1.0f)*/;
679
16.9k
            if (k1_below2 >
680
16.9k
                FL2FXCONST_DBL(0.0f)) { /* 3-Ch Aliasing Detection */
681
4.23k
              degreeAlias[loBand - 1] = deg;
682
4.23k
            }
683
16.9k
          } else if (k1_below2 >
684
16.3k
                     FL2FXCONST_DBL(0.0f)) { /* 3-Ch Aliasing Detection */
685
4.26k
            degreeAlias[loBand] = deg;
686
4.26k
          }
687
33.3k
        }
688
597k
        if (((loBand & 1) == 1) && (k1 > FL2FXCONST_DBL(0.0f))) {
689
22.3k
          if (k1_below > FL2FXCONST_DBL(0.0f)) { /* 2-CH Aliasing Detection */
690
8.88k
            degreeAlias[loBand] = (FIXP_DBL)MAXVAL_DBL /*FL2FXCONST_DBL(1.0f)*/;
691
8.88k
            if (k1_below2 <
692
8.88k
                FL2FXCONST_DBL(0.0f)) { /* 3-CH Aliasing Detection */
693
3.26k
              degreeAlias[loBand - 1] = deg;
694
3.26k
            }
695
13.5k
          } else if (k1_below2 <
696
13.5k
                     FL2FXCONST_DBL(0.0f)) { /* 3-CH Aliasing Detection */
697
5.39k
            degreeAlias[loBand] = deg;
698
5.39k
          }
699
22.3k
        }
700
597k
      }
701
      /* remember k1 values of the 2 QMF channels below the current channel */
702
723k
      k1_below2 = k1_below;
703
723k
      k1_below = k1;
704
723k
    }
705
706
3.63M
    patch = 0;
707
708
12.7M
    while (patch < pSettings->noOfPatches) { /* inner loop over every patch */
709
710
9.12M
      int hiBand = loBand + patchParam[patch].targetBandOffs;
711
712
9.12M
      if (loBand < patchParam[patch].sourceStartBand ||
713
6.36M
          loBand >= patchParam[patch].sourceStopBand
714
          //|| hiBand >= hLppTrans->pSettings->noChannels
715
9.12M
      ) {
716
        /* Lowband not in current patch - proceed */
717
3.14M
        patch++;
718
3.14M
        continue;
719
3.14M
      }
720
721
5.98M
      FDK_ASSERT(hiBand < (64));
722
723
      /* bwIndex[patch] is already initialized with value from previous band
724
       * inside this patch */
725
6.79M
      while (hiBand >= pSettings->bwBorders[bwIndex[patch]] &&
726
809k
             bwIndex[patch] < MAX_NUM_PATCHES - 1) {
727
809k
        bwIndex[patch]++;
728
809k
      }
729
730
      /*
731
        Filter Step 2: add the left slope with the current filter to the buffer
732
                       pure source values are already in there
733
      */
734
5.98M
      bw = FX_DBL2FX_SGL(bwVector[bwIndex[patch]]);
735
736
5.98M
      a0r = FX_DBL2FX_SGL(
737
5.98M
          fMult(bw, alphar[0])); /* Apply current bandwidth expansion factor */
738
739
5.98M
      if (!useLP) a0i = FX_DBL2FX_SGL(fMult(bw, alphai[0]));
740
5.98M
      bw = FX_DBL2FX_SGL(fPow2(bw));
741
5.98M
      a1r = FX_DBL2FX_SGL(fMult(bw, alphar[1]));
742
5.98M
      if (!useLP) a1i = FX_DBL2FX_SGL(fMult(bw, alphai[1]));
743
744
      /*
745
        Filter Step 3: insert the middle part which won't be windowed
746
      */
747
5.98M
      if (bw <= FL2FXCONST_SGL(0.0f)) {
748
2.30M
        if (!useLP) {
749
1.77M
          int descale =
750
1.77M
              fixMin(DFRACT_BITS - 1, (LPC_SCALE_FACTOR + dynamicScale));
751
60.3M
          for (i = startSample; i < stopSample; i++) {
752
58.5M
            FIXP_DBL accu1, accu2;
753
58.5M
            accu1 = lowBandReal[LPC_ORDER + i] >> descale;
754
58.5M
            accu2 = lowBandImag[LPC_ORDER + i] >> descale;
755
58.5M
            if (fPreWhitening) {
756
21.1M
              accu1 = scaleValueSaturate(
757
21.1M
                  fMultDiv2(accu1, preWhiteningGains[loBand]),
758
21.1M
                  preWhiteningGains_exp[loBand] + 1);
759
21.1M
              accu2 = scaleValueSaturate(
760
21.1M
                  fMultDiv2(accu2, preWhiteningGains[loBand]),
761
21.1M
                  preWhiteningGains_exp[loBand] + 1);
762
21.1M
            }
763
58.5M
            qmfBufferReal[i][hiBand] = accu1;
764
58.5M
            qmfBufferImag[i][hiBand] = accu2;
765
58.5M
          }
766
1.77M
        } else {
767
527k
          int descale =
768
527k
              fixMin(DFRACT_BITS - 1, (LPC_SCALE_FACTOR + dynamicScale));
769
16.5M
          for (i = startSample; i < stopSample; i++) {
770
15.9M
            qmfBufferReal[i][hiBand] = lowBandReal[LPC_ORDER + i] >> descale;
771
15.9M
          }
772
527k
        }
773
3.68M
      } else { /* bw <= 0 */
774
775
3.68M
        if (!useLP) {
776
3.59M
          const int dynscale = fixMax(0, dynamicScale - 2) + 1;
777
3.59M
          const int rescale = -fixMin(0, dynamicScale - 2) + 1;
778
3.59M
          const int descale = fixMin(DFRACT_BITS - 1,
779
3.59M
                                     LPC_SCALE_FACTOR + dynamicScale + rescale);
780
781
122M
          for (i = startSample; i < stopSample; i++) {
782
118M
            FIXP_DBL accu1, accu2;
783
784
118M
            accu1 = ((fMultDiv2(a0r, lowBandReal[LPC_ORDER + i - 1]) -
785
118M
                      fMultDiv2(a0i, lowBandImag[LPC_ORDER + i - 1])) >>
786
118M
                     1) +
787
118M
                    ((fMultDiv2(a1r, lowBandReal[LPC_ORDER + i - 2]) -
788
118M
                      fMultDiv2(a1i, lowBandImag[LPC_ORDER + i - 2])) >>
789
118M
                     1);
790
118M
            accu2 = ((fMultDiv2(a0i, lowBandReal[LPC_ORDER + i - 1]) +
791
118M
                      fMultDiv2(a0r, lowBandImag[LPC_ORDER + i - 1])) >>
792
118M
                     1) +
793
118M
                    ((fMultDiv2(a1i, lowBandReal[LPC_ORDER + i - 2]) +
794
118M
                      fMultDiv2(a1r, lowBandImag[LPC_ORDER + i - 2])) >>
795
118M
                     1);
796
797
118M
            accu1 =
798
118M
                (lowBandReal[LPC_ORDER + i] >> descale) + (accu1 >> dynscale);
799
118M
            accu2 =
800
118M
                (lowBandImag[LPC_ORDER + i] >> descale) + (accu2 >> dynscale);
801
118M
            if (fPreWhitening) {
802
15.6M
              qmfBufferReal[i][hiBand] = scaleValueSaturate(
803
15.6M
                  fMultDiv2(accu1, preWhiteningGains[loBand]),
804
15.6M
                  preWhiteningGains_exp[loBand] + 1 + rescale);
805
15.6M
              qmfBufferImag[i][hiBand] = scaleValueSaturate(
806
15.6M
                  fMultDiv2(accu2, preWhiteningGains[loBand]),
807
15.6M
                  preWhiteningGains_exp[loBand] + 1 + rescale);
808
103M
            } else {
809
103M
              qmfBufferReal[i][hiBand] =
810
103M
                  SATURATE_LEFT_SHIFT(accu1, rescale, DFRACT_BITS);
811
103M
              qmfBufferImag[i][hiBand] =
812
103M
                  SATURATE_LEFT_SHIFT(accu2, rescale, DFRACT_BITS);
813
103M
            }
814
118M
          }
815
3.59M
        } else {
816
89.7k
          FDK_ASSERT(dynamicScale >= 0);
817
89.7k
          calc_qmfBufferReal(
818
89.7k
              qmfBufferReal, &(lowBandReal[LPC_ORDER + startSample - 2]),
819
89.7k
              startSample, stopSample, hiBand, dynamicScale, a0r, a1r);
820
89.7k
        }
821
3.68M
      } /* bw <= 0 */
822
823
5.98M
      patch++;
824
825
5.98M
    } /* inner loop over patches */
826
827
    /*
828
     * store the unmodified filter coefficients if there is
829
     * an overlapping envelope
830
     *****************************************************************/
831
832
3.63M
  } /* outer loop over bands (loBand) */
833
834
256k
  if (useLP) {
835
37.0k
    for (loBand = pSettings->lbStartPatching;
836
553k
         loBand < pSettings->lbStopPatching; loBand++) {
837
516k
      patch = 0;
838
1.17M
      while (patch < pSettings->noOfPatches) {
839
659k
        UCHAR hiBand = loBand + patchParam[patch].targetBandOffs;
840
841
659k
        if (loBand < patchParam[patch].sourceStartBand ||
842
621k
            loBand >= patchParam[patch].sourceStopBand ||
843
616k
            hiBand >= (64) /* Highband out of range (biterror) */
844
659k
        ) {
845
          /* Lowband not in current patch or highband out of range (might be
846
           * caused by biterrors)- proceed */
847
42.6k
          patch++;
848
42.6k
          continue;
849
42.6k
        }
850
851
616k
        if (hiBand != patchParam[patch].targetStartBand)
852
568k
          degreeAlias[hiBand] = degreeAlias[loBand];
853
854
616k
        patch++;
855
616k
      }
856
516k
    } /* end  for loop */
857
37.0k
  }
858
859
838k
  for (i = 0; i < nInvfBands; i++) {
860
582k
    hLppTrans->bwVectorOld[i] = bwVector[i];
861
582k
  }
862
863
  /*
864
    set high band scale factor
865
  */
866
256k
  sbrScaleFactor->hb_scale = comLowBandScale - (LPC_SCALE_FACTOR);
867
256k
}
868
869
void lppTransposerHBE(
870
    HANDLE_SBR_LPP_TRANS hLppTrans, /*!< Handle of lpp transposer  */
871
    HANDLE_HBE_TRANSPOSER hQmfTransposer,
872
    QMF_SCALE_FACTOR *sbrScaleFactor, /*!< Scaling factors */
873
    FIXP_DBL **qmfBufferReal, /*!< Pointer to pointer to real part of subband
874
                                 samples (source) */
875
    FIXP_DBL **qmfBufferImag, /*!< Pointer to pointer to imaginary part of
876
                                 subband samples (source) */
877
    const int timeStep,       /*!< Time step of envelope */
878
    const int firstSlotOffs,  /*!< Start position in time */
879
    const int lastSlotOffs,   /*!< Number of overlap-slots into next frame */
880
    const int nInvfBands,     /*!< Number of bands for inverse filtering */
881
    INVF_MODE *sbr_invf_mode, /*!< Current inverse filtering modes */
882
    INVF_MODE *sbr_invf_mode_prev /*!< Previous inverse filtering modes */
883
42.8k
) {
884
42.8k
  INT bwIndex;
885
42.8k
  FIXP_DBL bwVector[MAX_NUM_PATCHES_HBE]; /*!< pole moving factors */
886
887
42.8k
  int i;
888
42.8k
  int loBand, start, stop;
889
42.8k
  TRANSPOSER_SETTINGS *pSettings = hLppTrans->pSettings;
890
42.8k
  PATCH_PARAM *patchParam = pSettings->patchParam;
891
892
42.8k
  FIXP_SGL alphar[LPC_ORDER], a0r, a1r;
893
42.8k
  FIXP_SGL alphai[LPC_ORDER], a0i = 0, a1i = 0;
894
42.8k
  FIXP_SGL bw = FL2FXCONST_SGL(0.0f);
895
896
42.8k
  int autoCorrLength;
897
898
42.8k
  ACORR_COEFS ac;
899
42.8k
  int startSample;
900
42.8k
  int stopSample;
901
42.8k
  int stopSampleClear;
902
903
42.8k
  int comBandScale;
904
42.8k
  int ovLowBandShift;
905
42.8k
  int lowBandShift;
906
  /*  int ovHighBandShift;*/
907
908
42.8k
  alphai[0] = FL2FXCONST_SGL(0.0f);
909
42.8k
  alphai[1] = FL2FXCONST_SGL(0.0f);
910
911
42.8k
  startSample = firstSlotOffs * timeStep;
912
42.8k
  stopSample = pSettings->nCols + lastSlotOffs * timeStep;
913
914
42.8k
  inverseFilteringLevelEmphasis(hLppTrans, nInvfBands, sbr_invf_mode,
915
42.8k
                                sbr_invf_mode_prev, bwVector);
916
917
42.8k
  stopSampleClear = stopSample;
918
919
42.8k
  autoCorrLength = pSettings->nCols + pSettings->overlap;
920
921
42.8k
  if (pSettings->noOfPatches > 0) {
922
    /* Set upper subbands to zero:
923
       This is required in case that the patches do not cover the complete
924
       highband (because the last patch would be too short). Possible
925
       optimization: Clearing bands up to usb would be sufficient here. */
926
42.8k
    int targetStopBand =
927
42.8k
        patchParam[pSettings->noOfPatches - 1].targetStartBand +
928
42.8k
        patchParam[pSettings->noOfPatches - 1].numBandsInPatch;
929
930
42.8k
    int memSize = ((64) - targetStopBand) * sizeof(FIXP_DBL);
931
932
2.17M
    for (i = startSample; i < stopSampleClear; i++) {
933
2.12M
      FDKmemclear(&qmfBufferReal[i][targetStopBand], memSize);
934
2.12M
      FDKmemclear(&qmfBufferImag[i][targetStopBand], memSize);
935
2.12M
    }
936
42.8k
  }
937
#if defined __ANDROID__ && !defined __ANDROID_NDK__
938
  else {
939
    // Safetynet logging
940
    android_errorWriteLog(0x534e4554, "112160868");
941
  }
942
#endif
943
944
  /*
945
  Calc common low band scale factor
946
  */
947
42.8k
  comBandScale = sbrScaleFactor->hb_scale;
948
949
42.8k
  ovLowBandShift = sbrScaleFactor->hb_scale - comBandScale;
950
42.8k
  lowBandShift = sbrScaleFactor->hb_scale - comBandScale;
951
  /*  ovHighBandShift = firstSlotOffs == 0 ? ovLowBandShift:0;*/
952
953
  /* outer loop over bands to do analysis only once for each band */
954
955
42.8k
  start = hQmfTransposer->startBand;
956
42.8k
  stop = hQmfTransposer->stopBand;
957
958
1.53M
  for (loBand = start; loBand < stop; loBand++) {
959
1.48M
    bwIndex = 0;
960
961
1.48M
    FIXP_DBL lowBandReal[(((1024) / (32) * (4) / 2) + (3 * (4))) + LPC_ORDER];
962
1.48M
    FIXP_DBL lowBandImag[(((1024) / (32) * (4) / 2) + (3 * (4))) + LPC_ORDER];
963
964
1.48M
    int resetLPCCoeffs = 0;
965
1.48M
    int dynamicScale = DFRACT_BITS - 1 - LPC_SCALE_FACTOR;
966
1.48M
    int acDetScale = 0; /* scaling of autocorrelation determinant */
967
968
4.46M
    for (i = 0; i < LPC_ORDER; i++) {
969
2.97M
      lowBandReal[i] = hLppTrans->lpcFilterStatesRealHBE[i][loBand];
970
2.97M
      lowBandImag[i] = hLppTrans->lpcFilterStatesImagHBE[i][loBand];
971
2.97M
    }
972
973
3.24M
    for (; i < LPC_ORDER + firstSlotOffs * timeStep; i++) {
974
1.75M
      lowBandReal[i] = hLppTrans->lpcFilterStatesRealHBE[i][loBand];
975
1.75M
      lowBandImag[i] = hLppTrans->lpcFilterStatesImagHBE[i][loBand];
976
1.75M
    }
977
978
    /*
979
    Take old slope length qmf slot source values out of (overlap)qmf buffer
980
    */
981
1.48M
    for (i = firstSlotOffs * timeStep;
982
82.8M
         i < pSettings->nCols + pSettings->overlap; i++) {
983
81.3M
      lowBandReal[i + LPC_ORDER] = qmfBufferReal[i][loBand];
984
81.3M
      lowBandImag[i + LPC_ORDER] = qmfBufferImag[i][loBand];
985
81.3M
    }
986
987
    /* store unmodified values to buffer */
988
17.5M
    for (i = 0; i < LPC_ORDER + pSettings->overlap; i++) {
989
16.0M
      hLppTrans->lpcFilterStatesRealHBE[i][loBand] =
990
16.0M
          qmfBufferReal[pSettings->nCols - LPC_ORDER + i][loBand];
991
16.0M
      hLppTrans->lpcFilterStatesImagHBE[i][loBand] =
992
16.0M
          qmfBufferImag[pSettings->nCols - LPC_ORDER + i][loBand];
993
16.0M
    }
994
995
    /*
996
    Determine dynamic scaling value.
997
    */
998
1.48M
    dynamicScale =
999
1.48M
        fixMin(dynamicScale,
1000
1.48M
               getScalefactor(lowBandReal, LPC_ORDER + pSettings->overlap) +
1001
1.48M
                   ovLowBandShift);
1002
1.48M
    dynamicScale =
1003
1.48M
        fixMin(dynamicScale,
1004
1.48M
               getScalefactor(&lowBandReal[LPC_ORDER + pSettings->overlap],
1005
1.48M
                              pSettings->nCols) +
1006
1.48M
                   lowBandShift);
1007
1.48M
    dynamicScale =
1008
1.48M
        fixMin(dynamicScale,
1009
1.48M
               getScalefactor(lowBandImag, LPC_ORDER + pSettings->overlap) +
1010
1.48M
                   ovLowBandShift);
1011
1.48M
    dynamicScale =
1012
1.48M
        fixMin(dynamicScale,
1013
1.48M
               getScalefactor(&lowBandImag[LPC_ORDER + pSettings->overlap],
1014
1.48M
                              pSettings->nCols) +
1015
1.48M
                   lowBandShift);
1016
1017
1.48M
    dynamicScale =
1018
1.48M
        dynamicScale - 1; /* one additional bit headroom to prevent -1.0 */
1019
1020
    /*
1021
    Scale temporal QMF buffer.
1022
    */
1023
1.48M
    scaleValues(&lowBandReal[0], LPC_ORDER + pSettings->overlap,
1024
1.48M
                dynamicScale - ovLowBandShift);
1025
1.48M
    scaleValues(&lowBandReal[LPC_ORDER + pSettings->overlap], pSettings->nCols,
1026
1.48M
                dynamicScale - lowBandShift);
1027
1.48M
    scaleValues(&lowBandImag[0], LPC_ORDER + pSettings->overlap,
1028
1.48M
                dynamicScale - ovLowBandShift);
1029
1.48M
    scaleValues(&lowBandImag[LPC_ORDER + pSettings->overlap], pSettings->nCols,
1030
1.48M
                dynamicScale - lowBandShift);
1031
1032
1.48M
    acDetScale += autoCorr2nd_cplx(&ac, lowBandReal + LPC_ORDER,
1033
1.48M
                                   lowBandImag + LPC_ORDER, autoCorrLength);
1034
1035
    /* Examine dynamic of determinant in autocorrelation. */
1036
1.48M
    acDetScale += 2 * (comBandScale + dynamicScale);
1037
1.48M
    acDetScale *= 2;            /* two times reflection coefficent scaling */
1038
1.48M
    acDetScale += ac.det_scale; /* ac scaling of determinant */
1039
1040
    /* In case of determinant < 10^-38, resetLPCCoeffs=1 has to be enforced. */
1041
1.48M
    if (acDetScale > 126) {
1042
833k
      resetLPCCoeffs = 1;
1043
833k
    }
1044
1045
1.48M
    alphar[1] = FL2FXCONST_SGL(0.0f);
1046
1.48M
    alphai[1] = FL2FXCONST_SGL(0.0f);
1047
1048
1.48M
    if (ac.det != FL2FXCONST_DBL(0.0f)) {
1049
653k
      FIXP_DBL tmp, absTmp, absDet;
1050
1051
653k
      absDet = fixp_abs(ac.det);
1052
1053
653k
      tmp = (fMultDiv2(ac.r01r, ac.r12r) >> (LPC_SCALE_FACTOR - 1)) -
1054
653k
            ((fMultDiv2(ac.r01i, ac.r12i) + fMultDiv2(ac.r02r, ac.r11r)) >>
1055
653k
             (LPC_SCALE_FACTOR - 1));
1056
653k
      absTmp = fixp_abs(tmp);
1057
1058
      /*
1059
      Quick check: is first filter coeff >= 1(4)
1060
      */
1061
653k
      {
1062
653k
        INT scale;
1063
653k
        FIXP_DBL result = fDivNorm(absTmp, absDet, &scale);
1064
653k
        scale = scale + ac.det_scale;
1065
1066
653k
        if ((scale > 0) && (result >= (FIXP_DBL)MAXVAL_DBL >> scale)) {
1067
21.4k
          resetLPCCoeffs = 1;
1068
631k
        } else {
1069
631k
          alphar[1] = FX_DBL2FX_SGL(scaleValueSaturate(result, scale));
1070
631k
          if ((tmp < FL2FX_DBL(0.0f)) ^ (ac.det < FL2FX_DBL(0.0f))) {
1071
422k
            alphar[1] = -alphar[1];
1072
422k
          }
1073
631k
        }
1074
653k
      }
1075
1076
653k
      tmp = (fMultDiv2(ac.r01i, ac.r12r) >> (LPC_SCALE_FACTOR - 1)) +
1077
653k
            ((fMultDiv2(ac.r01r, ac.r12i) -
1078
653k
              (FIXP_DBL)fMultDiv2(ac.r02i, ac.r11r)) >>
1079
653k
             (LPC_SCALE_FACTOR - 1));
1080
1081
653k
      absTmp = fixp_abs(tmp);
1082
1083
      /*
1084
      Quick check: is second filter coeff >= 1(4)
1085
      */
1086
653k
      {
1087
653k
        INT scale;
1088
653k
        FIXP_DBL result = fDivNorm(absTmp, absDet, &scale);
1089
653k
        scale = scale + ac.det_scale;
1090
1091
653k
        if ((scale > 0) &&
1092
33.8k
            (result >= /*FL2FXCONST_DBL(1.f)*/ (FIXP_DBL)MAXVAL_DBL >> scale)) {
1093
20.1k
          resetLPCCoeffs = 1;
1094
633k
        } else {
1095
633k
          alphai[1] = FX_DBL2FX_SGL(scaleValueSaturate(result, scale));
1096
633k
          if ((tmp < FL2FX_DBL(0.0f)) ^ (ac.det < FL2FX_DBL(0.0f))) {
1097
289k
            alphai[1] = -alphai[1];
1098
289k
          }
1099
633k
        }
1100
653k
      }
1101
653k
    }
1102
1103
1.48M
    alphar[0] = FL2FXCONST_SGL(0.0f);
1104
1.48M
    alphai[0] = FL2FXCONST_SGL(0.0f);
1105
1106
1.48M
    if (ac.r11r != FL2FXCONST_DBL(0.0f)) {
1107
      /* ac.r11r is always >=0 */
1108
655k
      FIXP_DBL tmp, absTmp;
1109
1110
655k
      tmp = (ac.r01r >> (LPC_SCALE_FACTOR + 1)) +
1111
655k
            (fMultDiv2(alphar[1], ac.r12r) + fMultDiv2(alphai[1], ac.r12i));
1112
1113
655k
      absTmp = fixp_abs(tmp);
1114
1115
      /*
1116
      Quick check: is first filter coeff >= 1(4)
1117
      */
1118
1119
655k
      if (absTmp >= (ac.r11r >> 1)) {
1120
1.23k
        resetLPCCoeffs = 1;
1121
653k
      } else {
1122
653k
        INT scale;
1123
653k
        FIXP_DBL result = fDivNorm(absTmp, fixp_abs(ac.r11r), &scale);
1124
653k
        alphar[0] = FX_DBL2FX_SGL(scaleValueSaturate(result, scale + 1));
1125
1126
653k
        if ((tmp > FL2FX_DBL(0.0f)) ^ (ac.r11r < FL2FX_DBL(0.0f)))
1127
423k
          alphar[0] = -alphar[0];
1128
653k
      }
1129
1130
655k
      tmp = (ac.r01i >> (LPC_SCALE_FACTOR + 1)) +
1131
655k
            (fMultDiv2(alphai[1], ac.r12r) - fMultDiv2(alphar[1], ac.r12i));
1132
1133
655k
      absTmp = fixp_abs(tmp);
1134
1135
      /*
1136
      Quick check: is second filter coeff >= 1(4)
1137
      */
1138
655k
      if (absTmp >= (ac.r11r >> 1)) {
1139
2.32k
        resetLPCCoeffs = 1;
1140
652k
      } else {
1141
652k
        INT scale;
1142
652k
        FIXP_DBL result = fDivNorm(absTmp, fixp_abs(ac.r11r), &scale);
1143
652k
        alphai[0] = FX_DBL2FX_SGL(scaleValueSaturate(result, scale + 1));
1144
652k
        if ((tmp > FL2FX_DBL(0.0f)) ^ (ac.r11r < FL2FX_DBL(0.0f))) {
1145
323k
          alphai[0] = -alphai[0];
1146
323k
        }
1147
652k
      }
1148
655k
    }
1149
1150
    /* Now check the quadratic criteria */
1151
1.48M
    if ((fMultDiv2(alphar[0], alphar[0]) + fMultDiv2(alphai[0], alphai[0])) >=
1152
1.48M
        FL2FXCONST_DBL(0.5f)) {
1153
1.05k
      resetLPCCoeffs = 1;
1154
1.05k
    }
1155
1.48M
    if ((fMultDiv2(alphar[1], alphar[1]) + fMultDiv2(alphai[1], alphai[1])) >=
1156
1.48M
        FL2FXCONST_DBL(0.5f)) {
1157
3.12k
      resetLPCCoeffs = 1;
1158
3.12k
    }
1159
1160
1.48M
    if (resetLPCCoeffs) {
1161
868k
      alphar[0] = FL2FXCONST_SGL(0.0f);
1162
868k
      alphar[1] = FL2FXCONST_SGL(0.0f);
1163
868k
      alphai[0] = FL2FXCONST_SGL(0.0f);
1164
868k
      alphai[1] = FL2FXCONST_SGL(0.0f);
1165
868k
    }
1166
1167
4.56M
    while (bwIndex < MAX_NUM_PATCHES - 1 &&
1168
4.56M
           loBand >= pSettings->bwBorders[bwIndex]) {
1169
3.07M
      bwIndex++;
1170
3.07M
    }
1171
1172
    /*
1173
    Filter Step 2: add the left slope with the current filter to the buffer
1174
    pure source values are already in there
1175
    */
1176
1.48M
    bw = FX_DBL2FX_SGL(bwVector[bwIndex]);
1177
1178
1.48M
    a0r = FX_DBL2FX_SGL(
1179
1.48M
        fMult(bw, alphar[0])); /* Apply current bandwidth expansion factor */
1180
1.48M
    a0i = FX_DBL2FX_SGL(fMult(bw, alphai[0]));
1181
1.48M
    bw = FX_DBL2FX_SGL(fPow2(bw));
1182
1.48M
    a1r = FX_DBL2FX_SGL(fMult(bw, alphar[1]));
1183
1.48M
    a1i = FX_DBL2FX_SGL(fMult(bw, alphai[1]));
1184
1185
    /*
1186
    Filter Step 3: insert the middle part which won't be windowed
1187
    */
1188
1.48M
    if (bw <= FL2FXCONST_SGL(0.0f)) {
1189
949k
      int descale = fixMin(DFRACT_BITS - 1, (LPC_SCALE_FACTOR + dynamicScale));
1190
40.2M
      for (i = startSample; i < stopSample; i++) {
1191
39.2M
        qmfBufferReal[i][loBand] = lowBandReal[LPC_ORDER + i] >> descale;
1192
39.2M
        qmfBufferImag[i][loBand] = lowBandImag[LPC_ORDER + i] >> descale;
1193
39.2M
      }
1194
949k
    } else { /* bw <= 0 */
1195
1196
540k
      int descale = fixMin(DFRACT_BITS - 1, (LPC_SCALE_FACTOR + dynamicScale));
1197
540k
      dynamicScale +=
1198
540k
          1; /* prevent negativ scale factor due to 'one additional bit
1199
                headroom' */
1200
1201
30.5M
      for (i = startSample; i < stopSample; i++) {
1202
30.0M
        FIXP_DBL accu1, accu2;
1203
1204
30.0M
        accu1 = (fMultDiv2(a0r, lowBandReal[LPC_ORDER + i - 1]) -
1205
30.0M
                 fMultDiv2(a0i, lowBandImag[LPC_ORDER + i - 1]) +
1206
30.0M
                 fMultDiv2(a1r, lowBandReal[LPC_ORDER + i - 2]) -
1207
30.0M
                 fMultDiv2(a1i, lowBandImag[LPC_ORDER + i - 2])) >>
1208
30.0M
                dynamicScale;
1209
30.0M
        accu2 = (fMultDiv2(a0i, lowBandReal[LPC_ORDER + i - 1]) +
1210
30.0M
                 fMultDiv2(a0r, lowBandImag[LPC_ORDER + i - 1]) +
1211
30.0M
                 fMultDiv2(a1i, lowBandReal[LPC_ORDER + i - 2]) +
1212
30.0M
                 fMultDiv2(a1r, lowBandImag[LPC_ORDER + i - 2])) >>
1213
30.0M
                dynamicScale;
1214
1215
30.0M
        qmfBufferReal[i][loBand] =
1216
30.0M
            (lowBandReal[LPC_ORDER + i] >> descale) + (accu1 << (1 + 1));
1217
30.0M
        qmfBufferImag[i][loBand] =
1218
30.0M
            (lowBandImag[LPC_ORDER + i] >> descale) + (accu2 << (1 + 1));
1219
30.0M
      }
1220
540k
    } /* bw <= 0 */
1221
1222
    /*
1223
     * store the unmodified filter coefficients if there is
1224
     * an overlapping envelope
1225
     *****************************************************************/
1226
1227
1.48M
  } /* outer loop over bands (loBand) */
1228
1229
180k
  for (i = 0; i < nInvfBands; i++) {
1230
137k
    hLppTrans->bwVectorOld[i] = bwVector[i];
1231
137k
  }
1232
1233
  /*
1234
  set high band scale factor
1235
  */
1236
42.8k
  sbrScaleFactor->hb_scale = comBandScale - (LPC_SCALE_FACTOR);
1237
42.8k
}
1238
1239
/*!
1240
 *
1241
 * \brief Initialize one low power transposer instance
1242
 *
1243
 *
1244
 */
1245
SBR_ERROR
1246
createLppTransposer(
1247
    HANDLE_SBR_LPP_TRANS hs,        /*!< Handle of low power transposer  */
1248
    TRANSPOSER_SETTINGS *pSettings, /*!< Pointer to settings */
1249
    const int highBandStartSb,      /*!< ? */
1250
    UCHAR *v_k_master,              /*!< Master table */
1251
    const int numMaster,            /*!< Valid entries in master table */
1252
    const int usb,                  /*!< Highband area stop subband */
1253
    const int timeSlots,            /*!< Number of time slots */
1254
    const int nCols,                /*!< Number of colums (codec qmf bank) */
1255
    UCHAR *noiseBandTable,  /*!< Mapping of SBR noise bands to QMF bands */
1256
    const int noNoiseBands, /*!< Number of noise bands */
1257
    UINT fs,                /*!< Sample Frequency */
1258
    const int chan,         /*!< Channel number */
1259
148k
    const int overlap) {
1260
  /* FB inverse filtering settings */
1261
148k
  hs->pSettings = pSettings;
1262
1263
148k
  pSettings->nCols = nCols;
1264
148k
  pSettings->overlap = overlap;
1265
1266
148k
  switch (timeSlots) {
1267
48.2k
    case 15:
1268
148k
    case 16:
1269
148k
      break;
1270
1271
0
    default:
1272
0
      return SBRDEC_UNSUPPORTED_CONFIG; /* Unimplemented */
1273
148k
  }
1274
1275
148k
  if (chan == 0) {
1276
    /* Init common data only once */
1277
97.4k
    hs->pSettings->nCols = nCols;
1278
1279
97.4k
    return resetLppTransposer(hs, highBandStartSb, v_k_master, numMaster,
1280
97.4k
                              noiseBandTable, noNoiseBands, usb, fs);
1281
97.4k
  }
1282
51.3k
  return SBRDEC_OK;
1283
148k
}
1284
1285
static int findClosestEntry(UCHAR goalSb, UCHAR *v_k_master, UCHAR numMaster,
1286
758k
                            UCHAR direction) {
1287
758k
  int index;
1288
1289
758k
  if (goalSb <= v_k_master[0]) return v_k_master[0];
1290
1291
732k
  if (goalSb >= v_k_master[numMaster]) return v_k_master[numMaster];
1292
1293
401k
  if (direction) {
1294
18.0k
    index = 0;
1295
240k
    while (v_k_master[index] < goalSb) {
1296
222k
      index++;
1297
222k
    }
1298
382k
  } else {
1299
382k
    index = numMaster;
1300
2.01M
    while (v_k_master[index] > goalSb) {
1301
1.63M
      index--;
1302
1.63M
    }
1303
382k
  }
1304
1305
401k
  return v_k_master[index];
1306
732k
}
1307
1308
/*!
1309
 *
1310
 * \brief Reset memory for one lpp transposer instance
1311
 *
1312
 * \return SBRDEC_OK on success, SBRDEC_UNSUPPORTED_CONFIG on error
1313
 */
1314
SBR_ERROR
1315
resetLppTransposer(
1316
    HANDLE_SBR_LPP_TRANS hLppTrans, /*!< Handle of lpp transposer  */
1317
    UCHAR highBandStartSb,          /*!< High band area: start subband */
1318
    UCHAR *v_k_master,              /*!< Master table */
1319
    UCHAR numMaster,                /*!< Valid entries in master table */
1320
    UCHAR *noiseBandTable, /*!< Mapping of SBR noise bands to QMF bands */
1321
    UCHAR noNoiseBands,    /*!< Number of noise bands */
1322
    UCHAR usb,             /*!< High band area: stop subband */
1323
    UINT fs                /*!< SBR output sampling frequency */
1324
371k
) {
1325
371k
  TRANSPOSER_SETTINGS *pSettings = hLppTrans->pSettings;
1326
371k
  PATCH_PARAM *patchParam = pSettings->patchParam;
1327
1328
371k
  int i, patch;
1329
371k
  int targetStopBand;
1330
371k
  int sourceStartBand;
1331
371k
  int patchDistance;
1332
371k
  int numBandsInPatch;
1333
1334
371k
  int lsb = v_k_master[0]; /* Start subband expressed in "non-critical" sampling
1335
                              terms*/
1336
371k
  int xoverOffset = highBandStartSb -
1337
371k
                    lsb; /* Calculate distance in QMF bands between k0 and kx */
1338
371k
  int startFreqHz;
1339
1340
371k
  int desiredBorder;
1341
1342
371k
  usb = fixMin(usb, v_k_master[numMaster]); /* Avoid endless loops (compare with
1343
                                               float code). */
1344
1345
  /*
1346
   * Plausibility check
1347
   */
1348
1349
371k
  if (pSettings->nCols == 64) {
1350
22.1k
    if (lsb < 4) {
1351
      /* 4:1 SBR Requirement k0 >= 4 missed! */
1352
93
      return SBRDEC_UNSUPPORTED_CONFIG;
1353
93
    }
1354
349k
  } else if (lsb - SHIFT_START_SB < 4) {
1355
200
    return SBRDEC_UNSUPPORTED_CONFIG;
1356
200
  }
1357
1358
  /*
1359
   * Initialize the patching parameter
1360
   */
1361
  /* ISO/IEC 14496-3 (Figure 4.48): goalSb = round( 2.048e6 / fs ) */
1362
371k
  desiredBorder = (((2048000 * 2) / fs) + 1) >> 1;
1363
1364
371k
  desiredBorder = findClosestEntry(desiredBorder, v_k_master, numMaster,
1365
371k
                                   1); /* Adapt region to master-table */
1366
1367
  /* First patch */
1368
371k
  sourceStartBand = SHIFT_START_SB + xoverOffset;
1369
371k
  targetStopBand = lsb + xoverOffset; /* upperBand */
1370
1371
  /* Even (odd) numbered channel must be patched to even (odd) numbered channel
1372
   */
1373
371k
  patch = 0;
1374
1.06M
  while (targetStopBand < usb) {
1375
    /* Too many patches?
1376
       Allow MAX_NUM_PATCHES+1 patches here.
1377
       we need to check later again, since patch might be the highest patch
1378
       AND contain less than 3 bands => actual number of patches will be reduced
1379
       by 1.
1380
    */
1381
690k
    if (patch > MAX_NUM_PATCHES) {
1382
236
      return SBRDEC_UNSUPPORTED_CONFIG;
1383
236
    }
1384
1385
690k
    patchParam[patch].guardStartBand = targetStopBand;
1386
690k
    patchParam[patch].targetStartBand = targetStopBand;
1387
1388
690k
    numBandsInPatch =
1389
690k
        desiredBorder - targetStopBand; /* Get the desired range of the patch */
1390
1391
690k
    if (numBandsInPatch >= lsb - sourceStartBand) {
1392
      /* Desired number bands are not available -> patch whole source range */
1393
387k
      patchDistance =
1394
387k
          targetStopBand - sourceStartBand; /* Get the targetOffset */
1395
387k
      patchDistance =
1396
387k
          patchDistance & ~1; /* Rounding off odd numbers and make all even */
1397
387k
      numBandsInPatch =
1398
387k
          lsb - (targetStopBand -
1399
387k
                 patchDistance); /* Update number of bands to be patched */
1400
387k
      numBandsInPatch = findClosestEntry(targetStopBand + numBandsInPatch,
1401
387k
                                         v_k_master, numMaster, 0) -
1402
387k
                        targetStopBand; /* Adapt region to master-table */
1403
387k
    }
1404
1405
690k
    if (pSettings->nCols == 64) {
1406
56.9k
      if (numBandsInPatch == 0 && sourceStartBand == SHIFT_START_SB) {
1407
187
        return SBRDEC_UNSUPPORTED_CONFIG;
1408
187
      }
1409
56.9k
    }
1410
1411
    /* Desired number bands are available -> get the minimal even patching
1412
     * distance */
1413
690k
    patchDistance =
1414
690k
        numBandsInPatch + targetStopBand - lsb; /* Get minimal distance */
1415
690k
    patchDistance = (patchDistance + 1) &
1416
690k
                    ~1; /* Rounding up odd numbers and make all even */
1417
1418
690k
    if (numBandsInPatch > 0) {
1419
662k
      patchParam[patch].sourceStartBand = targetStopBand - patchDistance;
1420
662k
      patchParam[patch].targetBandOffs = patchDistance;
1421
662k
      patchParam[patch].numBandsInPatch = numBandsInPatch;
1422
662k
      patchParam[patch].sourceStopBand =
1423
662k
          patchParam[patch].sourceStartBand + numBandsInPatch;
1424
1425
662k
      targetStopBand += patchParam[patch].numBandsInPatch;
1426
662k
      patch++;
1427
662k
    }
1428
1429
    /* All patches but first */
1430
690k
    sourceStartBand = SHIFT_START_SB;
1431
1432
    /* Check if we are close to desiredBorder */
1433
690k
    if (desiredBorder - targetStopBand < 3) /* MPEG doc */
1434
335k
    {
1435
335k
      desiredBorder = usb;
1436
335k
    }
1437
690k
  }
1438
1439
370k
  patch--;
1440
1441
  /* If highest patch contains less than three subband: skip it */
1442
370k
  if ((patch > 0) && (patchParam[patch].numBandsInPatch < 3)) {
1443
28.0k
    patch--;
1444
28.0k
    targetStopBand =
1445
28.0k
        patchParam[patch].targetStartBand + patchParam[patch].numBandsInPatch;
1446
28.0k
  }
1447
1448
  /* now check if we don't have one too many */
1449
370k
  if (patch >= MAX_NUM_PATCHES) {
1450
1.38k
    return SBRDEC_UNSUPPORTED_CONFIG;
1451
1.38k
  }
1452
1453
369k
  pSettings->noOfPatches = patch + 1;
1454
1455
  /* Check lowest and highest source subband */
1456
369k
  pSettings->lbStartPatching = targetStopBand;
1457
369k
  pSettings->lbStopPatching = 0;
1458
991k
  for (patch = 0; patch < pSettings->noOfPatches; patch++) {
1459
621k
    pSettings->lbStartPatching =
1460
621k
        fixMin(pSettings->lbStartPatching, patchParam[patch].sourceStartBand);
1461
621k
    pSettings->lbStopPatching =
1462
621k
        fixMax(pSettings->lbStopPatching, patchParam[patch].sourceStopBand);
1463
621k
  }
1464
1465
1.23M
  for (i = 0; i < noNoiseBands; i++) {
1466
870k
    pSettings->bwBorders[i] = noiseBandTable[i + 1];
1467
870k
  }
1468
3.19M
  for (; i < MAX_NUM_NOISE_VALUES; i++) {
1469
2.82M
    pSettings->bwBorders[i] = 255;
1470
2.82M
  }
1471
1472
  /*
1473
   * Choose whitening factors
1474
   */
1475
1476
369k
  startFreqHz =
1477
369k
      ((lsb + xoverOffset) * fs) >> 7; /* Shift does a division by 2*(64) */
1478
1479
1.05M
  for (i = 1; i < NUM_WHFACTOR_TABLE_ENTRIES; i++) {
1480
1.00M
    if (startFreqHz < FDK_sbrDecoder_sbr_whFactorsIndex[i]) break;
1481
1.00M
  }
1482
369k
  i--;
1483
1484
369k
  pSettings->whFactors.off = FDK_sbrDecoder_sbr_whFactorsTable[i][0];
1485
369k
  pSettings->whFactors.transitionLevel =
1486
369k
      FDK_sbrDecoder_sbr_whFactorsTable[i][1];
1487
369k
  pSettings->whFactors.lowLevel = FDK_sbrDecoder_sbr_whFactorsTable[i][2];
1488
369k
  pSettings->whFactors.midLevel = FDK_sbrDecoder_sbr_whFactorsTable[i][3];
1489
369k
  pSettings->whFactors.highLevel = FDK_sbrDecoder_sbr_whFactorsTable[i][4];
1490
1491
369k
  return SBRDEC_OK;
1492
370k
}