Coverage Report

Created: 2026-01-16 07:48

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/fdk-aac/libAACdec/src/aacdec_pns.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
/**************************** AAC decoder library ******************************
96
97
   Author(s):   Josef Hoepfl
98
99
   Description: perceptual noise substitution tool
100
101
*******************************************************************************/
102
103
#include "aacdec_pns.h"
104
105
#include "aac_ram.h"
106
#include "aac_rom.h"
107
#include "channelinfo.h"
108
#include "block.h"
109
#include "FDK_bitstream.h"
110
111
#include "genericStds.h"
112
113
42.0k
#define NOISE_OFFSET 90 /* cf. ISO 14496-3 p. 175 */
114
115
/*!
116
  \brief Reset InterChannel and PNS data
117
118
  The function resets the InterChannel and PNS data
119
*/
120
void CPns_ResetData(CPnsData *pPnsData,
121
2.33M
                    CPnsInterChannelData *pPnsInterChannelData) {
122
2.33M
  FDK_ASSERT(pPnsData != NULL);
123
2.33M
  FDK_ASSERT(pPnsInterChannelData != NULL);
124
  /* Assign pointer always, since pPnsData is not persistent data */
125
2.33M
  pPnsData->pPnsInterChannelData = pPnsInterChannelData;
126
2.33M
  pPnsData->PnsActive = 0;
127
2.33M
  pPnsData->CurrentEnergy = 0;
128
129
2.33M
  FDKmemclear(pPnsData->pnsUsed, (8 * 16) * sizeof(UCHAR));
130
2.33M
  FDKmemclear(pPnsInterChannelData->correlated, (8 * 16) * sizeof(UCHAR));
131
2.33M
}
132
133
/*!
134
  \brief Update PNS noise generator state.
135
136
  The function sets the seed for PNS noise generation.
137
  It can be used to link two or more channels in terms of PNS.
138
*/
139
void CPns_UpdateNoiseState(CPnsData *pPnsData, INT *currentSeed,
140
1.00M
                           INT *randomSeed) {
141
  /* use pointer because seed has to be
142
     same, left and right channel ! */
143
1.00M
  pPnsData->currentSeed = currentSeed;
144
1.00M
  pPnsData->randomSeed = randomSeed;
145
1.00M
}
146
147
/*!
148
  \brief Indicates if PNS is used
149
150
  The function returns a value indicating whether PNS is used or not
151
  acordding to the noise energy
152
153
  \return  PNS used
154
*/
155
334k
int CPns_IsPnsUsed(const CPnsData *pPnsData, const int group, const int band) {
156
334k
  unsigned pns_band = group * 16 + band;
157
158
334k
  return pPnsData->pnsUsed[pns_band] & (UCHAR)1;
159
334k
}
160
161
/*!
162
  \brief Set correlation
163
164
  The function activates the noise correlation between the channel pair
165
*/
166
void CPns_SetCorrelation(CPnsData *pPnsData, const int group, const int band,
167
31.0k
                         const int outofphase) {
168
31.0k
  CPnsInterChannelData *pInterChannelData = pPnsData->pPnsInterChannelData;
169
31.0k
  unsigned pns_band = group * 16 + band;
170
171
31.0k
  pInterChannelData->correlated[pns_band] = (outofphase) ? 3 : 1;
172
31.0k
}
173
174
/*!
175
  \brief Indicates if correlation is used
176
177
  The function indicates if the noise correlation between the channel pair
178
  is activated
179
180
  \return  PNS is correlated
181
*/
182
static int CPns_IsCorrelated(const CPnsData *pPnsData, const int group,
183
2.43k
                             const int band) {
184
2.43k
  CPnsInterChannelData *pInterChannelData = pPnsData->pPnsInterChannelData;
185
2.43k
  unsigned pns_band = group * 16 + band;
186
187
2.43k
  return (pInterChannelData->correlated[pns_band] & 0x01) ? 1 : 0;
188
2.43k
}
189
190
/*!
191
  \brief Indicates if correlated out of phase mode is used.
192
193
  The function indicates if the noise correlation between the channel pair
194
  is activated in out-of-phase mode.
195
196
  \return  PNS is out-of-phase
197
*/
198
static int CPns_IsOutOfPhase(const CPnsData *pPnsData, const int group,
199
55.6k
                             const int band) {
200
55.6k
  CPnsInterChannelData *pInterChannelData = pPnsData->pPnsInterChannelData;
201
55.6k
  unsigned pns_band = group * 16 + band;
202
203
55.6k
  return (pInterChannelData->correlated[pns_band] & 0x02) ? 1 : 0;
204
55.6k
}
205
206
/*!
207
  \brief Read PNS information
208
209
  The function reads the PNS information from the bitstream
210
*/
211
void CPns_Read(CPnsData *pPnsData, HANDLE_FDK_BITSTREAM bs,
212
               const CodeBookDescription *hcb, SHORT *pScaleFactor,
213
258k
               UCHAR global_gain, int band, int group /* = 0 */) {
214
258k
  int delta;
215
258k
  UINT pns_band = group * 16 + band;
216
217
258k
  if (pPnsData->PnsActive) {
218
    /* Next PNS band case */
219
216k
    delta = CBlock_DecodeHuffmanWord(bs, hcb) - 60;
220
216k
  } else {
221
    /* First PNS band case */
222
42.0k
    int noiseStartValue = FDKreadBits(bs, 9);
223
224
42.0k
    delta = noiseStartValue - 256;
225
42.0k
    pPnsData->PnsActive = 1;
226
42.0k
    pPnsData->CurrentEnergy = global_gain - NOISE_OFFSET;
227
42.0k
  }
228
229
258k
  pPnsData->CurrentEnergy += delta;
230
258k
  pScaleFactor[pns_band] = pPnsData->CurrentEnergy;
231
232
258k
  pPnsData->pnsUsed[pns_band] = 1;
233
258k
}
234
235
/**
236
 * \brief Generate a vector of noise of given length. The noise values are
237
 *        scaled in order to yield a noise energy of 1.0
238
 * \param spec pointer to were the noise values will be written to.
239
 * \param size amount of noise values to be generated.
240
 * \param pRandomState pointer to the state of the random generator being used.
241
 * \return exponent of generated noise vector.
242
 */
243
static int GenerateRandomVector(FIXP_DBL *RESTRICT spec, int size,
244
55.6k
                                int *pRandomState) {
245
55.6k
  int i, invNrg_e = 0, nrg_e = 0;
246
55.6k
  FIXP_DBL invNrg_m, nrg_m = FL2FXCONST_DBL(0.0f);
247
55.6k
  FIXP_DBL *RESTRICT ptr = spec;
248
55.6k
  int randomState = *pRandomState;
249
250
409k
#define GEN_NOISE_NRG_SCALE 7
251
252
  /* Generate noise and calculate energy. */
253
409k
  for (i = 0; i < size; i++) {
254
353k
    randomState =
255
353k
        (((INT64)1664525 * randomState) + (INT64)1013904223) & 0xFFFFFFFF;
256
353k
    nrg_m = fPow2AddDiv2(nrg_m, (FIXP_DBL)randomState >> GEN_NOISE_NRG_SCALE);
257
353k
    *ptr++ = (FIXP_DBL)randomState;
258
353k
  }
259
55.6k
  nrg_e = GEN_NOISE_NRG_SCALE * 2 + 1;
260
261
  /* weight noise with = 1 / sqrt_nrg; */
262
55.6k
  invNrg_m = invSqrtNorm2(nrg_m << 1, &invNrg_e);
263
55.6k
  invNrg_e += -((nrg_e - 1) >> 1);
264
265
409k
  for (i = size; i--;) {
266
353k
    spec[i] = fMult(spec[i], invNrg_m);
267
353k
  }
268
269
  /* Store random state */
270
55.6k
  *pRandomState = randomState;
271
272
55.6k
  return invNrg_e;
273
55.6k
}
274
275
static void ScaleBand(FIXP_DBL *RESTRICT spec, int size, int scaleFactor,
276
55.6k
                      int specScale, int noise_e, int out_of_phase) {
277
55.6k
  int i, shift, sfExponent;
278
55.6k
  FIXP_DBL sfMatissa;
279
280
  /* Get gain from scale factor value = 2^(scaleFactor * 0.25) */
281
55.6k
  sfMatissa = MantissaTable[scaleFactor & 0x03][0];
282
  /* sfExponent = (scaleFactor >> 2) + ExponentTable[scaleFactor & 0x03][0]; */
283
  /* Note:  ExponentTable[scaleFactor & 0x03][0] is always 1. */
284
55.6k
  sfExponent = (scaleFactor >> 2) + 1;
285
286
55.6k
  if (out_of_phase != 0) {
287
0
    sfMatissa = -sfMatissa;
288
0
  }
289
290
  /* +1 because of fMultDiv2 below. */
291
55.6k
  shift = sfExponent - specScale + 1 + noise_e;
292
293
  /* Apply gain to noise values */
294
55.6k
  if (shift >= 0) {
295
15.8k
    shift = fixMin(shift, DFRACT_BITS - 1);
296
84.1k
    for (i = size; i-- != 0;) {
297
68.3k
      spec[i] = fMultDiv2(spec[i], sfMatissa) << shift;
298
68.3k
    }
299
39.8k
  } else {
300
39.8k
    shift = fixMin(-shift, DFRACT_BITS - 1);
301
325k
    for (i = size; i-- != 0;) {
302
285k
      spec[i] = fMultDiv2(spec[i], sfMatissa) >> shift;
303
285k
    }
304
39.8k
  }
305
55.6k
}
306
307
/*!
308
  \brief Apply PNS
309
310
  The function applies PNS (i.e. it generates noise) on the bands
311
  flagged as noisy bands
312
313
*/
314
void CPns_Apply(const CPnsData *pPnsData, const CIcsInfo *pIcsInfo,
315
                SPECTRAL_PTR pSpectrum, const SHORT *pSpecScale,
316
                const SHORT *pScaleFactor,
317
                const SamplingRateInfo *pSamplingRateInfo,
318
309k
                const INT granuleLength, const int channel) {
319
309k
  if (pPnsData->PnsActive) {
320
16.9k
    const short *BandOffsets =
321
16.9k
        GetScaleFactorBandOffsets(pIcsInfo, pSamplingRateInfo);
322
323
16.9k
    int ScaleFactorBandsTransmitted = GetScaleFactorBandsTransmitted(pIcsInfo);
324
325
89.8k
    for (int window = 0, group = 0; group < GetWindowGroups(pIcsInfo);
326
72.8k
         group++) {
327
187k
      for (int groupwin = 0; groupwin < GetWindowGroupLength(pIcsInfo, group);
328
114k
           groupwin++, window++) {
329
114k
        FIXP_DBL *spectrum = SPEC(pSpectrum, window, granuleLength);
330
331
405k
        for (int band = 0; band < ScaleFactorBandsTransmitted; band++) {
332
290k
          if (CPns_IsPnsUsed(pPnsData, group, band)) {
333
55.6k
            UINT pns_band = window * 16 + band;
334
335
55.6k
            int bandWidth = BandOffsets[band + 1] - BandOffsets[band];
336
55.6k
            int noise_e;
337
338
55.6k
            FDK_ASSERT(bandWidth >= 0);
339
340
55.6k
            if (channel > 0 && CPns_IsCorrelated(pPnsData, group, band)) {
341
1.08k
              noise_e =
342
1.08k
                  GenerateRandomVector(spectrum + BandOffsets[band], bandWidth,
343
1.08k
                                       &pPnsData->randomSeed[pns_band]);
344
54.5k
            } else {
345
54.5k
              pPnsData->randomSeed[pns_band] = *pPnsData->currentSeed;
346
347
54.5k
              noise_e = GenerateRandomVector(spectrum + BandOffsets[band],
348
54.5k
                                             bandWidth, pPnsData->currentSeed);
349
54.5k
            }
350
351
55.6k
            int outOfPhase = CPns_IsOutOfPhase(pPnsData, group, band);
352
353
55.6k
            ScaleBand(spectrum + BandOffsets[band], bandWidth,
354
55.6k
                      pScaleFactor[group * 16 + band], pSpecScale[window],
355
55.6k
                      noise_e, outOfPhase);
356
55.6k
          }
357
290k
        }
358
114k
      }
359
72.8k
    }
360
16.9k
  }
361
309k
}