Coverage Report

Created: 2026-07-25 07:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/fdk-aac/libSBRdec/src/env_dec.cpp
Line
Count
Source
1
/* -----------------------------------------------------------------------------
2
Software License for The Fraunhofer FDK AAC Codec Library for Android
3
4
© Copyright  1995 - 2018 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  envelope decoding
106
  This module provides envelope decoding and error concealment algorithms. The
107
  main entry point is decodeSbrData().
108
109
  \sa decodeSbrData(),\ref documentationOverview
110
*/
111
112
#include "env_dec.h"
113
114
#include "env_extr.h"
115
#include "transcendent.h"
116
117
#include "genericStds.h"
118
119
static void decodeEnvelope(HANDLE_SBR_HEADER_DATA hHeaderData,
120
                           HANDLE_SBR_FRAME_DATA h_sbr_data,
121
                           HANDLE_SBR_PREV_FRAME_DATA h_prev_data,
122
                           HANDLE_SBR_PREV_FRAME_DATA h_prev_data_otherChannel);
123
static void sbr_envelope_unmapping(HANDLE_SBR_HEADER_DATA hHeaderData,
124
                                   HANDLE_SBR_FRAME_DATA h_data_left,
125
                                   HANDLE_SBR_FRAME_DATA h_data_right);
126
static void requantizeEnvelopeData(HANDLE_SBR_FRAME_DATA h_sbr_data,
127
                                   int ampResolution);
128
static void deltaToLinearPcmEnvelopeDecoding(
129
    HANDLE_SBR_HEADER_DATA hHeaderData, HANDLE_SBR_FRAME_DATA h_sbr_data,
130
    HANDLE_SBR_PREV_FRAME_DATA h_prev_data);
131
static void decodeNoiseFloorlevels(HANDLE_SBR_HEADER_DATA hHeaderData,
132
                                   HANDLE_SBR_FRAME_DATA h_sbr_data,
133
                                   HANDLE_SBR_PREV_FRAME_DATA h_prev_data);
134
static void timeCompensateFirstEnvelope(HANDLE_SBR_HEADER_DATA hHeaderData,
135
                                        HANDLE_SBR_FRAME_DATA h_sbr_data,
136
                                        HANDLE_SBR_PREV_FRAME_DATA h_prev_data);
137
static int checkEnvelopeData(HANDLE_SBR_HEADER_DATA hHeaderData,
138
                             HANDLE_SBR_FRAME_DATA h_sbr_data,
139
                             HANDLE_SBR_PREV_FRAME_DATA h_prev_data);
140
141
33.0k
#define SBR_ENERGY_PAN_OFFSET (12 << ENV_EXP_FRACT)
142
77.8k
#define SBR_MAX_ENERGY (35 << ENV_EXP_FRACT)
143
144
520k
#define DECAY (1 << ENV_EXP_FRACT)
145
146
#if ENV_EXP_FRACT
147
#define DECAY_COUPLING \
148
  (1 << (ENV_EXP_FRACT - 1)) /*!< corresponds to a value of 0.5 */
149
#else
150
#define DECAY_COUPLING \
151
9.58k
  1 /*!< If the energy data is not shifted, use 1 instead of 0.5 */
152
#endif
153
154
/*!
155
  \brief  Convert table index
156
*/
157
static int indexLow2High(int offset, /*!< mapping factor */
158
                         int index,  /*!< index to scalefactor band */
159
                         int res)    /*!< frequency resolution */
160
5.61M
{
161
5.61M
  if (res == 0) {
162
46.5k
    if (offset >= 0) {
163
46.5k
      if (index < offset)
164
2.71k
        return (index);
165
43.8k
      else
166
43.8k
        return (2 * index - offset);
167
46.5k
    } else {
168
0
      offset = -offset;
169
0
      if (index < offset)
170
0
        return (2 * index + index);
171
0
      else
172
0
        return (2 * index + offset);
173
0
    }
174
46.5k
  } else
175
5.56M
    return (index);
176
5.61M
}
177
178
/*!
179
  \brief  Update previous envelope value for delta-coding
180
181
  The current envelope values needs to be stored for delta-coding
182
  in the next frame.  The stored envelope is always represented with
183
  the high frequency resolution.  If the current envelope uses the
184
  low frequency resolution, the energy value will be mapped to the
185
  corresponding high-res bands.
186
*/
187
static void mapLowResEnergyVal(
188
    FIXP_SGL currVal,   /*!< current energy value */
189
    FIXP_SGL *prevData, /*!< pointer to previous data vector */
190
    int offset,         /*!< mapping factor */
191
    int index,          /*!< index to scalefactor band */
192
    int res)            /*!< frequeny resolution */
193
6.10M
{
194
6.10M
  if (res == 0) {
195
405k
    if (offset >= 0) {
196
405k
      if (index < offset)
197
32.6k
        prevData[index] = currVal;
198
372k
      else {
199
372k
        prevData[2 * index - offset] = currVal;
200
372k
        prevData[2 * index + 1 - offset] = currVal;
201
372k
      }
202
405k
    } else {
203
0
      offset = -offset;
204
0
      if (index < offset) {
205
0
        prevData[3 * index] = currVal;
206
0
        prevData[3 * index + 1] = currVal;
207
0
        prevData[3 * index + 2] = currVal;
208
0
      } else {
209
0
        prevData[2 * index + offset] = currVal;
210
0
        prevData[2 * index + 1 + offset] = currVal;
211
0
      }
212
0
    }
213
405k
  } else
214
5.70M
    prevData[index] = currVal;
215
6.10M
}
216
217
/*!
218
  \brief    Convert raw envelope and noisefloor data to energy levels
219
220
  This function is being called by sbrDecoder_ParseElement() and provides two
221
  important algorithms:
222
223
  First the function decodes envelopes and noise floor levels as described in
224
  requantizeEnvelopeData() and sbr_envelope_unmapping(). The function also
225
  implements concealment algorithms in case there are errors within the sbr
226
  data. For both operations fractional arithmetic is used. Therefore you might
227
  encounter different output values on your target system compared to the
228
  reference implementation.
229
*/
230
void decodeSbrData(
231
    HANDLE_SBR_HEADER_DATA hHeaderData, /*!< Static control data */
232
    HANDLE_SBR_FRAME_DATA
233
        h_data_left, /*!< pointer to left channel frame data */
234
    HANDLE_SBR_PREV_FRAME_DATA
235
        h_prev_data_left, /*!< pointer to left channel previous frame data */
236
    HANDLE_SBR_FRAME_DATA
237
        h_data_right, /*!< pointer to right channel frame data */
238
    HANDLE_SBR_PREV_FRAME_DATA
239
        h_prev_data_right) /*!< pointer to right channel previous frame data */
240
511k
{
241
511k
  FIXP_SGL tempSfbNrgPrev[MAX_FREQ_COEFFS];
242
511k
  int errLeft;
243
244
  /* Save previous energy values to be able to reuse them later for concealment.
245
   */
246
511k
  FDKmemcpy(tempSfbNrgPrev, h_prev_data_left->sfb_nrg_prev,
247
511k
            MAX_FREQ_COEFFS * sizeof(FIXP_SGL));
248
249
511k
  if (hHeaderData->frameErrorFlag || hHeaderData->bs_info.pvc_mode == 0) {
250
508k
    decodeEnvelope(hHeaderData, h_data_left, h_prev_data_left,
251
508k
                   h_prev_data_right);
252
508k
  } else {
253
2.31k
    FDK_ASSERT(h_data_right == NULL);
254
2.31k
  }
255
511k
  decodeNoiseFloorlevels(hHeaderData, h_data_left, h_prev_data_left);
256
257
511k
  if (h_data_right != NULL) {
258
82.5k
    errLeft = hHeaderData->frameErrorFlag;
259
82.5k
    decodeEnvelope(hHeaderData, h_data_right, h_prev_data_right,
260
82.5k
                   h_prev_data_left);
261
82.5k
    decodeNoiseFloorlevels(hHeaderData, h_data_right, h_prev_data_right);
262
263
82.5k
    if (!errLeft && hHeaderData->frameErrorFlag) {
264
      /* If an error occurs in the right channel where the left channel seemed
265
         ok, we apply concealment also on the left channel. This ensures that
266
         the coupling modes of both channels match and that we have the same
267
         number of envelopes in coupling mode. However, as the left channel has
268
         already been processed before, the resulting energy levels are not the
269
         same as if the left channel had been concealed during the first call of
270
         decodeEnvelope().
271
      */
272
      /* Restore previous energy values for concealment, because the values have
273
         been overwritten by the first call of decodeEnvelope(). */
274
1.03k
      FDKmemcpy(h_prev_data_left->sfb_nrg_prev, tempSfbNrgPrev,
275
1.03k
                MAX_FREQ_COEFFS * sizeof(FIXP_SGL));
276
      /* Do concealment */
277
1.03k
      decodeEnvelope(hHeaderData, h_data_left, h_prev_data_left,
278
1.03k
                     h_prev_data_right);
279
1.03k
    }
280
281
82.5k
    if (h_data_left->coupling) {
282
11.3k
      sbr_envelope_unmapping(hHeaderData, h_data_left, h_data_right);
283
11.3k
    }
284
82.5k
  }
285
286
  /* Display the data for debugging: */
287
511k
}
288
289
/*!
290
  \brief   Convert from coupled channels to independent L/R data
291
*/
292
static void sbr_envelope_unmapping(
293
    HANDLE_SBR_HEADER_DATA hHeaderData, /*!< Static control data */
294
    HANDLE_SBR_FRAME_DATA h_data_left,  /*!< pointer to left channel */
295
    HANDLE_SBR_FRAME_DATA h_data_right) /*!< pointer to right channel */
296
11.3k
{
297
11.3k
  int i;
298
11.3k
  FIXP_SGL tempL_m, tempR_m, tempRplus1_m, newL_m, newR_m;
299
11.3k
  SCHAR tempL_e, tempR_e, tempRplus1_e, newL_e, newR_e;
300
301
  /* 1. Unmap (already dequantized) coupled envelope energies */
302
303
143k
  for (i = 0; i < h_data_left->nScaleFactors; i++) {
304
132k
    tempR_m = (FIXP_SGL)((LONG)h_data_right->iEnvelope[i] & MASK_M);
305
132k
    tempR_e = (SCHAR)((LONG)h_data_right->iEnvelope[i] & MASK_E);
306
307
132k
    tempR_e -= (18 + NRG_EXP_OFFSET); /* -18 = ld(UNMAPPING_SCALE /
308
                                         h_data_right->nChannels) */
309
132k
    tempL_m = (FIXP_SGL)((LONG)h_data_left->iEnvelope[i] & MASK_M);
310
132k
    tempL_e = (SCHAR)((LONG)h_data_left->iEnvelope[i] & MASK_E);
311
312
132k
    tempL_e -= NRG_EXP_OFFSET;
313
314
    /* Calculate tempRight+1 */
315
132k
    FDK_add_MantExp(tempR_m, tempR_e, FL2FXCONST_SGL(0.5f), 1, /* 1.0 */
316
132k
                    &tempRplus1_m, &tempRplus1_e);
317
318
132k
    FDK_divide_MantExp(tempL_m, tempL_e + 1, /*  2 * tempLeft */
319
132k
                       tempRplus1_m, tempRplus1_e, &newR_m, &newR_e);
320
321
132k
    if (newR_m >= ((FIXP_SGL)MAXVAL_SGL - ROUNDING)) {
322
117
      newR_m >>= 1;
323
117
      newR_e += 1;
324
117
    }
325
326
132k
    newL_m = FX_DBL2FX_SGL(fMult(tempR_m, newR_m));
327
132k
    newL_e = tempR_e + newR_e;
328
329
132k
    h_data_right->iEnvelope[i] =
330
132k
        ((FIXP_SGL)((SHORT)(FIXP_SGL)(newR_m + ROUNDING) & MASK_M)) +
331
132k
        (FIXP_SGL)((SHORT)(FIXP_SGL)(newR_e + NRG_EXP_OFFSET) & MASK_E);
332
132k
    h_data_left->iEnvelope[i] =
333
132k
        ((FIXP_SGL)((SHORT)(FIXP_SGL)(newL_m + ROUNDING) & MASK_M)) +
334
132k
        (FIXP_SGL)((SHORT)(FIXP_SGL)(newL_e + NRG_EXP_OFFSET) & MASK_E);
335
132k
  }
336
337
  /* 2. Dequantize and unmap coupled noise floor levels */
338
339
35.4k
  for (i = 0; i < hHeaderData->freqBandData.nNfb *
340
35.4k
                      h_data_left->frameInfo.nNoiseEnvelopes;
341
24.0k
       i++) {
342
24.0k
    tempL_e = (SCHAR)(6 - (LONG)h_data_left->sbrNoiseFloorLevel[i]);
343
24.0k
    tempR_e = (SCHAR)((LONG)h_data_right->sbrNoiseFloorLevel[i] -
344
24.0k
                      12) /*SBR_ENERGY_PAN_OFFSET*/;
345
346
    /* Calculate tempR+1 */
347
24.0k
    FDK_add_MantExp(FL2FXCONST_SGL(0.5f), 1 + tempR_e, /* tempR */
348
24.0k
                    FL2FXCONST_SGL(0.5f), 1,           /*  1.0  */
349
24.0k
                    &tempRplus1_m, &tempRplus1_e);
350
351
    /* Calculate 2*tempLeft/(tempR+1) */
352
24.0k
    FDK_divide_MantExp(FL2FXCONST_SGL(0.5f), tempL_e + 2, /*  2 * tempLeft */
353
24.0k
                       tempRplus1_m, tempRplus1_e, &newR_m, &newR_e);
354
355
    /* if (newR_m >= ((FIXP_SGL)MAXVAL_SGL - ROUNDING)) {
356
      newR_m >>= 1;
357
      newR_e += 1;
358
    } */
359
360
    /* L = tempR * R */
361
24.0k
    newL_m = newR_m;
362
24.0k
    newL_e = newR_e + tempR_e;
363
24.0k
    h_data_right->sbrNoiseFloorLevel[i] =
364
24.0k
        ((FIXP_SGL)((SHORT)(FIXP_SGL)(newR_m + ROUNDING) & MASK_M)) +
365
24.0k
        (FIXP_SGL)((SHORT)(FIXP_SGL)(newR_e + NOISE_EXP_OFFSET) & MASK_E);
366
24.0k
    h_data_left->sbrNoiseFloorLevel[i] =
367
24.0k
        ((FIXP_SGL)((SHORT)(FIXP_SGL)(newL_m + ROUNDING) & MASK_M)) +
368
24.0k
        (FIXP_SGL)((SHORT)(FIXP_SGL)(newL_e + NOISE_EXP_OFFSET) & MASK_E);
369
24.0k
  }
370
11.3k
}
371
372
/*!
373
  \brief    Simple alternative to the real SBR concealment
374
375
  If the real frameInfo is not available due to a frame loss, a replacement will
376
  be constructed with 1 envelope spanning the whole frame (FIX-FIX).
377
  The delta-coded energies are set to negative values, resulting in a fade-down.
378
  In case of coupling, the balance-channel will move towards the center.
379
*/
380
static void leanSbrConcealment(
381
    HANDLE_SBR_HEADER_DATA hHeaderData,    /*!< Static control data */
382
    HANDLE_SBR_FRAME_DATA h_sbr_data,      /*!< pointer to current data */
383
    HANDLE_SBR_PREV_FRAME_DATA h_prev_data /*!< pointer to data of last frame */
384
529k
) {
385
529k
  FIXP_SGL target; /* targeted level for sfb_nrg_prev during fade-down */
386
529k
  FIXP_SGL step;   /* speed of fade */
387
529k
  int i;
388
389
529k
  int currentStartPos =
390
529k
      fMax(0, h_prev_data->stopPos - hHeaderData->numberTimeSlots);
391
529k
  int currentStopPos = hHeaderData->numberTimeSlots;
392
393
  /* Use some settings of the previous frame */
394
529k
  h_sbr_data->ampResolutionCurrentFrame = h_prev_data->ampRes;
395
529k
  h_sbr_data->coupling = h_prev_data->coupling;
396
3.17M
  for (i = 0; i < MAX_INVF_BANDS; i++)
397
2.64M
    h_sbr_data->sbr_invf_mode[i] = h_prev_data->sbr_invf_mode[i];
398
399
  /* Generate concealing control data */
400
401
529k
  h_sbr_data->frameInfo.nEnvelopes = 1;
402
529k
  h_sbr_data->frameInfo.borders[0] = currentStartPos;
403
529k
  h_sbr_data->frameInfo.borders[1] = currentStopPos;
404
529k
  h_sbr_data->frameInfo.freqRes[0] = 1;
405
529k
  h_sbr_data->frameInfo.tranEnv = -1; /* no transient */
406
529k
  h_sbr_data->frameInfo.nNoiseEnvelopes = 1;
407
529k
  h_sbr_data->frameInfo.bordersNoise[0] = currentStartPos;
408
529k
  h_sbr_data->frameInfo.bordersNoise[1] = currentStopPos;
409
410
529k
  h_sbr_data->nScaleFactors = hHeaderData->freqBandData.nSfb[1];
411
412
  /* Generate fake envelope data */
413
414
529k
  h_sbr_data->domain_vec[0] = 1;
415
416
529k
  if (h_sbr_data->coupling == COUPLING_BAL) {
417
9.58k
    target = (FIXP_SGL)SBR_ENERGY_PAN_OFFSET;
418
9.58k
    step = (FIXP_SGL)DECAY_COUPLING;
419
520k
  } else {
420
520k
    target = FL2FXCONST_SGL(0.0f);
421
520k
    step = (FIXP_SGL)DECAY;
422
520k
  }
423
529k
  if (hHeaderData->bs_info.ampResolution == 0) {
424
332k
    target <<= 1;
425
332k
    step <<= 1;
426
332k
  }
427
428
6.01M
  for (i = 0; i < h_sbr_data->nScaleFactors; i++) {
429
5.48M
    if (h_prev_data->sfb_nrg_prev[i] > target)
430
3.27M
      h_sbr_data->iEnvelope[i] = -step;
431
2.21M
    else
432
2.21M
      h_sbr_data->iEnvelope[i] = step;
433
5.48M
  }
434
435
  /* Noisefloor levels are always cleared ... */
436
437
529k
  h_sbr_data->domain_vec_noise[0] = 1;
438
529k
  FDKmemclear(h_sbr_data->sbrNoiseFloorLevel,
439
529k
              sizeof(h_sbr_data->sbrNoiseFloorLevel));
440
441
  /* ... and so are the sines */
442
529k
  FDKmemclear(h_sbr_data->addHarmonics,
443
529k
              sizeof(ULONG) * ADD_HARMONICS_FLAGS_SIZE);
444
529k
}
445
446
/*!
447
  \brief   Build reference energies and noise levels from bitstream elements
448
*/
449
static void decodeEnvelope(
450
    HANDLE_SBR_HEADER_DATA hHeaderData, /*!< Static control data */
451
    HANDLE_SBR_FRAME_DATA h_sbr_data,   /*!< pointer to current data */
452
    HANDLE_SBR_PREV_FRAME_DATA
453
        h_prev_data, /*!< pointer to data of last frame */
454
    HANDLE_SBR_PREV_FRAME_DATA
455
        otherChannel /*!< other channel's last frame data */
456
607k
) {
457
607k
  int i;
458
607k
  int fFrameError = hHeaderData->frameErrorFlag;
459
607k
  FIXP_SGL tempSfbNrgPrev[MAX_FREQ_COEFFS];
460
461
607k
  if (!fFrameError) {
462
    /*
463
      To avoid distortions after bad frames, set the error flag if delta coding
464
      in time occurs. However, SBR can take a little longer to come up again.
465
    */
466
89.4k
    if (h_prev_data->frameErrorFlag) {
467
67.4k
      if (h_sbr_data->domain_vec[0] != 0) {
468
9.50k
        fFrameError = 1;
469
9.50k
      }
470
67.4k
    } else {
471
      /* Check that the previous stop position and the current start position
472
         match. (Could be done in checkFrameInfo(), but the previous frame data
473
         is not available there) */
474
22.0k
      if (h_sbr_data->frameInfo.borders[0] !=
475
22.0k
          h_prev_data->stopPos - hHeaderData->numberTimeSlots) {
476
        /* Both the previous as well as the current frame are flagged to be ok,
477
         * but they do not match! */
478
2.16k
        if (h_sbr_data->domain_vec[0] == 1) {
479
          /* Prefer concealment over delta-time coding between the mismatching
480
           * frames */
481
127
          fFrameError = 1;
482
2.04k
        } else {
483
          /* Close the gap in time by triggering timeCompensateFirstEnvelope()
484
           */
485
2.04k
          fFrameError = 1;
486
2.04k
        }
487
2.16k
      }
488
22.0k
    }
489
89.4k
  }
490
491
607k
  if (fFrameError) /* Error is detected */
492
529k
  {
493
529k
    leanSbrConcealment(hHeaderData, h_sbr_data, h_prev_data);
494
495
    /* decode the envelope data to linear PCM */
496
529k
    deltaToLinearPcmEnvelopeDecoding(hHeaderData, h_sbr_data, h_prev_data);
497
529k
  } else /*Do a temporary dummy decoding and check that the envelope values are
498
            within limits */
499
77.8k
  {
500
77.8k
    if (h_prev_data->frameErrorFlag) {
501
57.9k
      timeCompensateFirstEnvelope(hHeaderData, h_sbr_data, h_prev_data);
502
57.9k
      if (h_sbr_data->coupling != h_prev_data->coupling) {
503
        /*
504
          Coupling mode has changed during concealment.
505
           The stored energy levels need to be converted.
506
         */
507
59.6k
        for (i = 0; i < hHeaderData->freqBandData.nSfb[1]; i++) {
508
          /* Former Level-Channel will be used for both channels */
509
53.6k
          if (h_prev_data->coupling == COUPLING_BAL) {
510
11.4k
            h_prev_data->sfb_nrg_prev[i] =
511
11.4k
                (otherChannel != NULL) ? otherChannel->sfb_nrg_prev[i]
512
11.4k
                                       : (FIXP_SGL)SBR_ENERGY_PAN_OFFSET;
513
11.4k
          }
514
          /* Former L/R will be combined as the new Level-Channel */
515
42.2k
          else if (h_sbr_data->coupling == COUPLING_LEVEL &&
516
14.3k
                   otherChannel != NULL) {
517
14.3k
            h_prev_data->sfb_nrg_prev[i] = (h_prev_data->sfb_nrg_prev[i] +
518
14.3k
                                            otherChannel->sfb_nrg_prev[i]) >>
519
14.3k
                                           1;
520
27.9k
          } else if (h_sbr_data->coupling == COUPLING_BAL) {
521
11.9k
            h_prev_data->sfb_nrg_prev[i] = (FIXP_SGL)SBR_ENERGY_PAN_OFFSET;
522
11.9k
          }
523
53.6k
        }
524
5.95k
      }
525
57.9k
    }
526
77.8k
    FDKmemcpy(tempSfbNrgPrev, h_prev_data->sfb_nrg_prev,
527
77.8k
              MAX_FREQ_COEFFS * sizeof(FIXP_SGL));
528
529
77.8k
    deltaToLinearPcmEnvelopeDecoding(hHeaderData, h_sbr_data, h_prev_data);
530
531
77.8k
    fFrameError = checkEnvelopeData(hHeaderData, h_sbr_data, h_prev_data);
532
533
77.8k
    if (fFrameError) {
534
15.1k
      hHeaderData->frameErrorFlag = 1;
535
15.1k
      FDKmemcpy(h_prev_data->sfb_nrg_prev, tempSfbNrgPrev,
536
15.1k
                MAX_FREQ_COEFFS * sizeof(FIXP_SGL));
537
15.1k
      decodeEnvelope(hHeaderData, h_sbr_data, h_prev_data, otherChannel);
538
15.1k
      return;
539
15.1k
    }
540
77.8k
  }
541
542
592k
  requantizeEnvelopeData(h_sbr_data, h_sbr_data->ampResolutionCurrentFrame);
543
544
592k
  hHeaderData->frameErrorFlag = fFrameError;
545
592k
}
546
547
/*!
548
  \brief   Verify that envelope energies are within the allowed range
549
  \return  0 if all is fine, 1 if an envelope value was too high
550
*/
551
static int checkEnvelopeData(
552
    HANDLE_SBR_HEADER_DATA hHeaderData,    /*!< Static control data */
553
    HANDLE_SBR_FRAME_DATA h_sbr_data,      /*!< pointer to current data */
554
    HANDLE_SBR_PREV_FRAME_DATA h_prev_data /*!< pointer to data of last frame */
555
77.8k
) {
556
77.8k
  FIXP_SGL *iEnvelope = h_sbr_data->iEnvelope;
557
77.8k
  FIXP_SGL *sfb_nrg_prev = h_prev_data->sfb_nrg_prev;
558
77.8k
  int i = 0, errorFlag = 0;
559
77.8k
  FIXP_SGL sbr_max_energy = (h_sbr_data->ampResolutionCurrentFrame == 1)
560
77.8k
                                ? SBR_MAX_ENERGY
561
77.8k
                                : (SBR_MAX_ENERGY << 1);
562
563
  /*
564
    Range check for current energies
565
  */
566
697k
  for (i = 0; i < h_sbr_data->nScaleFactors; i++) {
567
619k
    if (iEnvelope[i] > sbr_max_energy) {
568
76.5k
      errorFlag = 1;
569
76.5k
    }
570
619k
    if (iEnvelope[i] < FL2FXCONST_SGL(0.0f)) {
571
31.9k
      errorFlag = 1;
572
      /* iEnvelope[i] = FL2FXCONST_SGL(0.0f); */
573
31.9k
    }
574
619k
  }
575
576
  /*
577
    Range check for previous energies
578
  */
579
664k
  for (i = 0; i < hHeaderData->freqBandData.nSfb[1]; i++) {
580
586k
    sfb_nrg_prev[i] = fixMax(sfb_nrg_prev[i], FL2FXCONST_SGL(0.0f));
581
586k
    sfb_nrg_prev[i] = fixMin(sfb_nrg_prev[i], sbr_max_energy);
582
586k
  }
583
584
77.8k
  return (errorFlag);
585
77.8k
}
586
587
/*!
588
  \brief   Verify that the noise levels are within the allowed range
589
590
  The function is equivalent to checkEnvelopeData().
591
  When the noise-levels are being decoded, it is already too late for
592
  concealment. Therefore the noise levels are simply limited here.
593
*/
594
static void limitNoiseLevels(
595
    HANDLE_SBR_HEADER_DATA hHeaderData, /*!< Static control data */
596
    HANDLE_SBR_FRAME_DATA h_sbr_data)   /*!< pointer to current data */
597
593k
{
598
593k
  int i;
599
593k
  int nNfb = hHeaderData->freqBandData.nNfb;
600
601
/*
602
  Set range limits. The exact values depend on the coupling mode.
603
  However this limitation is primarily intended to avoid unlimited
604
  accumulation of the delta-coded noise levels.
605
*/
606
593k
#define lowerLimit \
607
593k
  ((FIXP_SGL)0) /* lowerLimit actually refers to the _highest_ noise energy */
608
593k
#define upperLimit \
609
593k
  ((FIXP_SGL)35) /* upperLimit actually refers to the _lowest_ noise energy */
610
611
  /*
612
    Range check for current noise levels
613
  */
614
1.94M
  for (i = 0; i < h_sbr_data->frameInfo.nNoiseEnvelopes * nNfb; i++) {
615
1.34M
    h_sbr_data->sbrNoiseFloorLevel[i] =
616
1.34M
        fixMin(h_sbr_data->sbrNoiseFloorLevel[i], upperLimit);
617
1.34M
    h_sbr_data->sbrNoiseFloorLevel[i] =
618
1.34M
        fixMax(h_sbr_data->sbrNoiseFloorLevel[i], lowerLimit);
619
1.34M
  }
620
593k
}
621
622
/*!
623
  \brief   Compensate for the wrong timing that might occur after a frame error.
624
*/
625
static void timeCompensateFirstEnvelope(
626
    HANDLE_SBR_HEADER_DATA hHeaderData, /*!< Static control data */
627
    HANDLE_SBR_FRAME_DATA h_sbr_data,   /*!< pointer to actual data */
628
    HANDLE_SBR_PREV_FRAME_DATA
629
        h_prev_data) /*!< pointer to data of last frame */
630
57.9k
{
631
57.9k
  int i, nScalefactors;
632
57.9k
  FRAME_INFO *pFrameInfo = &h_sbr_data->frameInfo;
633
57.9k
  UCHAR *nSfb = hHeaderData->freqBandData.nSfb;
634
57.9k
  int estimatedStartPos =
635
57.9k
      fMax(0, h_prev_data->stopPos - hHeaderData->numberTimeSlots);
636
57.9k
  int refLen, newLen, shift;
637
57.9k
  FIXP_SGL deltaExp;
638
639
  /* Original length of first envelope according to bitstream */
640
57.9k
  refLen = pFrameInfo->borders[1] - pFrameInfo->borders[0];
641
  /* Corrected length of first envelope (concealing can make the first envelope
642
   * longer) */
643
57.9k
  newLen = pFrameInfo->borders[1] - estimatedStartPos;
644
645
57.9k
  if (newLen <= 0) {
646
    /* An envelope length of <= 0 would not work, so we don't use it.
647
       May occur if the previous frame was flagged bad due to a mismatch
648
       of the old and new frame infos. */
649
0
    newLen = refLen;
650
0
    estimatedStartPos = pFrameInfo->borders[0];
651
0
  }
652
653
57.9k
  deltaExp = FDK_getNumOctavesDiv8(newLen, refLen);
654
655
  /* Shift by -3 to rescale ld-table, ampRes-1 to enable coarser steps */
656
57.9k
  shift = (FRACT_BITS - 1 - ENV_EXP_FRACT - 1 +
657
57.9k
           h_sbr_data->ampResolutionCurrentFrame - 3);
658
57.9k
  deltaExp = deltaExp >> shift;
659
57.9k
  pFrameInfo->borders[0] = estimatedStartPos;
660
57.9k
  pFrameInfo->bordersNoise[0] = estimatedStartPos;
661
662
57.9k
  if (h_sbr_data->coupling != COUPLING_BAL) {
663
56.2k
    nScalefactors = (pFrameInfo->freqRes[0]) ? nSfb[1] : nSfb[0];
664
665
338k
    for (i = 0; i < nScalefactors; i++)
666
282k
      h_sbr_data->iEnvelope[i] = h_sbr_data->iEnvelope[i] + deltaExp;
667
56.2k
  }
668
57.9k
}
669
670
/*!
671
  \brief   Convert each envelope value from logarithmic to linear domain
672
673
  Energy levels are transmitted in powers of 2, i.e. only the exponent
674
  is extracted from the bitstream.
675
  Therefore, normally only integer exponents can occur. However during
676
  fading (in case of a corrupt bitstream), a fractional part can also
677
  occur. The data in the array iEnvelope is shifted left by ENV_EXP_FRACT
678
  compared to an integer representation so that numbers smaller than 1
679
  can be represented.
680
681
  This function calculates a mantissa corresponding to the fractional
682
  part of the exponent for each reference energy. The array iEnvelope
683
  is converted in place to save memory. Input and output data must
684
  be interpreted differently, as shown in the below figure:
685
686
  \image html  EnvelopeData.png
687
688
  The data is then used in calculateSbrEnvelope().
689
*/
690
static void requantizeEnvelopeData(HANDLE_SBR_FRAME_DATA h_sbr_data,
691
592k
                                   int ampResolution) {
692
592k
  int i;
693
592k
  FIXP_SGL mantissa;
694
592k
  int ampShift = 1 - ampResolution;
695
592k
  int exponent;
696
697
  /* In case that ENV_EXP_FRACT is changed to something else but 0 or 8,
698
     the initialization of this array has to be adapted!
699
  */
700
#if ENV_EXP_FRACT
701
  static const FIXP_SGL pow2[ENV_EXP_FRACT] = {
702
      FL2FXCONST_SGL(0.5f * pow(2.0f, pow(0.5f, 1))), /* 0.7071 */
703
      FL2FXCONST_SGL(0.5f * pow(2.0f, pow(0.5f, 2))), /* 0.5946 */
704
      FL2FXCONST_SGL(0.5f * pow(2.0f, pow(0.5f, 3))),
705
      FL2FXCONST_SGL(0.5f * pow(2.0f, pow(0.5f, 4))),
706
      FL2FXCONST_SGL(0.5f * pow(2.0f, pow(0.5f, 5))),
707
      FL2FXCONST_SGL(0.5f * pow(2.0f, pow(0.5f, 6))),
708
      FL2FXCONST_SGL(0.5f * pow(2.0f, pow(0.5f, 7))),
709
      FL2FXCONST_SGL(0.5f * pow(2.0f, pow(0.5f, 8))) /* 0.5013 */
710
  };
711
712
  int bit, mask;
713
#endif
714
715
6.48M
  for (i = 0; i < h_sbr_data->nScaleFactors; i++) {
716
5.88M
    exponent = (LONG)h_sbr_data->iEnvelope[i];
717
718
#if ENV_EXP_FRACT
719
720
    exponent = exponent >> ampShift;
721
    mantissa = 0.5f;
722
723
    /* Amplify mantissa according to the fractional part of the
724
       exponent (result will be between 0.500000 and 0.999999)
725
    */
726
    mask = 1; /* begin with lowest bit of exponent */
727
728
    for (bit = ENV_EXP_FRACT - 1; bit >= 0; bit--) {
729
      if (exponent & mask) {
730
        /* The current bit of the exponent is set,
731
           multiply mantissa with the corresponding factor: */
732
        mantissa = (FIXP_SGL)((mantissa * pow2[bit]) << 1);
733
      }
734
      /* Advance to next bit */
735
      mask = mask << 1;
736
    }
737
738
    /* Make integer part of exponent right aligned */
739
    exponent = exponent >> ENV_EXP_FRACT;
740
741
#else
742
    /* In case of the high amplitude resolution, 1 bit of the exponent gets lost
743
       by the shift. This will be compensated by a mantissa of 0.5*sqrt(2)
744
       instead of 0.5 if that bit is 1. */
745
5.88M
    mantissa = (exponent & ampShift) ? FL2FXCONST_SGL(0.707106781186548f)
746
5.88M
                                     : FL2FXCONST_SGL(0.5f);
747
5.88M
    exponent = exponent >> ampShift;
748
5.88M
#endif
749
750
    /*
751
      Mantissa was set to 0.5 (instead of 1.0, therefore increase exponent by
752
      1). Multiply by L=nChannels=64 by increasing exponent by another 6.
753
      => Increase exponent by 7
754
    */
755
5.88M
    exponent += 7 + NRG_EXP_OFFSET;
756
757
    /* Combine mantissa and exponent and write back the result */
758
5.88M
    h_sbr_data->iEnvelope[i] =
759
5.88M
        ((FIXP_SGL)((SHORT)(FIXP_SGL)mantissa & MASK_M)) +
760
5.88M
        (FIXP_SGL)((SHORT)(FIXP_SGL)exponent & MASK_E);
761
5.88M
  }
762
592k
}
763
764
/*!
765
  \brief   Build new reference energies from old ones and delta coded data
766
*/
767
static void deltaToLinearPcmEnvelopeDecoding(
768
    HANDLE_SBR_HEADER_DATA hHeaderData,     /*!< Static control data */
769
    HANDLE_SBR_FRAME_DATA h_sbr_data,       /*!< pointer to current data */
770
    HANDLE_SBR_PREV_FRAME_DATA h_prev_data) /*!< pointer to previous data */
771
607k
{
772
607k
  int i, domain, no_of_bands, band, freqRes;
773
774
607k
  FIXP_SGL *sfb_nrg_prev = h_prev_data->sfb_nrg_prev;
775
607k
  FIXP_SGL *ptr_nrg = h_sbr_data->iEnvelope;
776
777
607k
  int offset =
778
607k
      2 * hHeaderData->freqBandData.nSfb[0] - hHeaderData->freqBandData.nSfb[1];
779
780
1.25M
  for (i = 0; i < h_sbr_data->frameInfo.nEnvelopes; i++) {
781
651k
    domain = h_sbr_data->domain_vec[i];
782
651k
    freqRes = h_sbr_data->frameInfo.freqRes[i];
783
784
651k
    FDK_ASSERT(freqRes >= 0 && freqRes <= 1);
785
786
651k
    no_of_bands = hHeaderData->freqBandData.nSfb[freqRes];
787
788
651k
    FDK_ASSERT(no_of_bands < (64));
789
790
651k
    if (domain == 0) {
791
94.5k
      mapLowResEnergyVal(*ptr_nrg, sfb_nrg_prev, offset, 0, freqRes);
792
94.5k
      ptr_nrg++;
793
493k
      for (band = 1; band < no_of_bands; band++) {
794
398k
        *ptr_nrg = *ptr_nrg + *(ptr_nrg - 1);
795
398k
        mapLowResEnergyVal(*ptr_nrg, sfb_nrg_prev, offset, band, freqRes);
796
398k
        ptr_nrg++;
797
398k
      }
798
556k
    } else {
799
6.17M
      for (band = 0; band < no_of_bands; band++) {
800
5.61M
        *ptr_nrg =
801
5.61M
            *ptr_nrg + sfb_nrg_prev[indexLow2High(offset, band, freqRes)];
802
5.61M
        mapLowResEnergyVal(*ptr_nrg, sfb_nrg_prev, offset, band, freqRes);
803
5.61M
        ptr_nrg++;
804
5.61M
      }
805
556k
    }
806
651k
  }
807
607k
}
808
809
/*!
810
  \brief   Build new noise levels from old ones and delta coded data
811
*/
812
static void decodeNoiseFloorlevels(
813
    HANDLE_SBR_HEADER_DATA hHeaderData,     /*!< Static control data */
814
    HANDLE_SBR_FRAME_DATA h_sbr_data,       /*!< pointer to current data */
815
    HANDLE_SBR_PREV_FRAME_DATA h_prev_data) /*!< pointer to previous data */
816
593k
{
817
593k
  int i;
818
593k
  int nNfb = hHeaderData->freqBandData.nNfb;
819
593k
  int nNoiseFloorEnvelopes = h_sbr_data->frameInfo.nNoiseEnvelopes;
820
821
  /* Decode first noise envelope */
822
823
593k
  if (h_sbr_data->domain_vec_noise[0] == 0) {
824
50.1k
    FIXP_SGL noiseLevel = h_sbr_data->sbrNoiseFloorLevel[0];
825
93.8k
    for (i = 1; i < nNfb; i++) {
826
43.6k
      noiseLevel += h_sbr_data->sbrNoiseFloorLevel[i];
827
43.6k
      h_sbr_data->sbrNoiseFloorLevel[i] = noiseLevel;
828
43.6k
    }
829
543k
  } else {
830
1.76M
    for (i = 0; i < nNfb; i++) {
831
1.22M
      h_sbr_data->sbrNoiseFloorLevel[i] += h_prev_data->prevNoiseLevel[i];
832
1.22M
    }
833
543k
  }
834
835
  /* If present, decode the second noise envelope
836
     Note:  nNoiseFloorEnvelopes can only be 1 or 2 */
837
838
593k
  if (nNoiseFloorEnvelopes > 1) {
839
19.1k
    if (h_sbr_data->domain_vec_noise[1] == 0) {
840
11.2k
      FIXP_SGL noiseLevel = h_sbr_data->sbrNoiseFloorLevel[nNfb];
841
18.7k
      for (i = nNfb + 1; i < 2 * nNfb; i++) {
842
7.52k
        noiseLevel += h_sbr_data->sbrNoiseFloorLevel[i];
843
7.52k
        h_sbr_data->sbrNoiseFloorLevel[i] = noiseLevel;
844
7.52k
      }
845
11.2k
    } else {
846
20.9k
      for (i = 0; i < nNfb; i++) {
847
13.0k
        h_sbr_data->sbrNoiseFloorLevel[i + nNfb] +=
848
13.0k
            h_sbr_data->sbrNoiseFloorLevel[i];
849
13.0k
      }
850
7.93k
    }
851
19.1k
  }
852
853
593k
  limitNoiseLevels(hHeaderData, h_sbr_data);
854
855
  /* Update prevNoiseLevel with the last noise envelope */
856
1.90M
  for (i = 0; i < nNfb; i++)
857
1.31M
    h_prev_data->prevNoiseLevel[i] =
858
1.31M
        h_sbr_data->sbrNoiseFloorLevel[i + nNfb * (nNoiseFloorEnvelopes - 1)];
859
860
  /* Requantize the noise floor levels in COUPLING_OFF-mode */
861
593k
  if (!h_sbr_data->coupling) {
862
571k
    int nf_e;
863
864
1.86M
    for (i = 0; i < nNoiseFloorEnvelopes * nNfb; i++) {
865
1.29M
      nf_e = 6 - (LONG)h_sbr_data->sbrNoiseFloorLevel[i] + 1 + NOISE_EXP_OFFSET;
866
      /* +1 to compensate for a mantissa of 0.5 instead of 1.0 */
867
868
1.29M
      h_sbr_data->sbrNoiseFloorLevel[i] =
869
1.29M
          (FIXP_SGL)(((LONG)FL2FXCONST_SGL(0.5f)) + /* mantissa */
870
1.29M
                     (nf_e & MASK_E));              /* exponent */
871
1.29M
    }
872
571k
  }
873
593k
}