Coverage Report

Created: 2025-11-24 06:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/aac/libAACenc/src/band_nrg.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 encoder library ******************************
96
97
   Author(s):   M. Werner
98
99
   Description: Band/Line energy calculations
100
101
*******************************************************************************/
102
103
#include "band_nrg.h"
104
105
/*****************************************************************************
106
  functionname: FDKaacEnc_CalcSfbMaxScaleSpec
107
  description:
108
  input:
109
  output:
110
*****************************************************************************/
111
void FDKaacEnc_CalcSfbMaxScaleSpec(const FIXP_DBL *RESTRICT mdctSpectrum,
112
                                   const INT *RESTRICT bandOffset,
113
                                   INT *RESTRICT sfbMaxScaleSpec,
114
0
                                   const INT numBands) {
115
0
  INT i, j;
116
0
  FIXP_DBL maxSpc, tmp;
117
118
0
  for (i = 0; i < numBands; i++) {
119
0
    maxSpc = (FIXP_DBL)0;
120
121
0
    DWORD_ALIGNED(mdctSpectrum);
122
123
0
    for (j = bandOffset[i]; j < bandOffset[i + 1]; j++) {
124
0
      tmp = fixp_abs(mdctSpectrum[j]);
125
0
      maxSpc = fixMax(maxSpc, tmp);
126
0
    }
127
0
    j = CntLeadingZeros(maxSpc) - 1;
128
0
    sfbMaxScaleSpec[i] = fixMin((DFRACT_BITS - 2), j);
129
    /* CountLeadingBits() is not necessary here since test value is always > 0
130
     */
131
0
  }
132
0
}
133
134
/*****************************************************************************
135
  functionname: FDKaacEnc_CheckBandEnergyOptim
136
  description:
137
  input:
138
  output:
139
*****************************************************************************/
140
FIXP_DBL
141
FDKaacEnc_CheckBandEnergyOptim(const FIXP_DBL *const RESTRICT mdctSpectrum,
142
                               const INT *const RESTRICT sfbMaxScaleSpec,
143
                               const INT *const RESTRICT bandOffset,
144
                               const INT numBands,
145
                               FIXP_DBL *RESTRICT bandEnergy,
146
                               FIXP_DBL *RESTRICT bandEnergyLdData,
147
0
                               const INT minSpecShift) {
148
0
  INT i, j, scale, nr = 0;
149
0
  FIXP_DBL maxNrgLd = FL2FXCONST_DBL(-1.0f);
150
0
  FIXP_DBL maxNrg = 0;
151
0
  FIXP_DBL spec;
152
153
0
  for (i = 0; i < numBands; i++) {
154
0
    scale = fixMax(0, sfbMaxScaleSpec[i] - 4);
155
0
    FIXP_DBL tmp = 0;
156
157
0
    DWORD_ALIGNED(mdctSpectrum);
158
159
0
    for (j = bandOffset[i]; j < bandOffset[i + 1]; j++) {
160
0
      spec = mdctSpectrum[j] << scale;
161
0
      tmp = fPow2AddDiv2(tmp, spec);
162
0
    }
163
0
    bandEnergy[i] = tmp << 1;
164
165
    /* calculate ld of bandNrg, subtract scaling */
166
0
    bandEnergyLdData[i] = CalcLdData(bandEnergy[i]);
167
0
    if (bandEnergyLdData[i] != FL2FXCONST_DBL(-1.0f)) {
168
0
      bandEnergyLdData[i] -= scale * FL2FXCONST_DBL(2.0 / 64);
169
0
    }
170
    /* find index of maxNrg */
171
0
    if (bandEnergyLdData[i] > maxNrgLd) {
172
0
      maxNrgLd = bandEnergyLdData[i];
173
0
      nr = i;
174
0
    }
175
0
  }
176
177
  /* return unscaled maxNrg*/
178
0
  scale = fixMax(0, sfbMaxScaleSpec[nr] - 4);
179
0
  scale = fixMax(2 * (minSpecShift - scale), -(DFRACT_BITS - 1));
180
181
0
  maxNrg = scaleValue(bandEnergy[nr], scale);
182
183
0
  return maxNrg;
184
0
}
185
186
/*****************************************************************************
187
  functionname: FDKaacEnc_CalcBandEnergyOptimLong
188
  description:
189
  input:
190
  output:
191
*****************************************************************************/
192
INT FDKaacEnc_CalcBandEnergyOptimLong(const FIXP_DBL *RESTRICT mdctSpectrum,
193
                                      INT *RESTRICT sfbMaxScaleSpec,
194
                                      const INT *RESTRICT bandOffset,
195
                                      const INT numBands,
196
                                      FIXP_DBL *RESTRICT bandEnergy,
197
0
                                      FIXP_DBL *RESTRICT bandEnergyLdData) {
198
0
  INT i, j, shiftBits = 0;
199
0
  FIXP_DBL maxNrgLd = FL2FXCONST_DBL(0.0f);
200
201
0
  FIXP_DBL spec;
202
203
0
  for (i = 0; i < numBands; i++) {
204
0
    INT leadingBits = sfbMaxScaleSpec[i] -
205
0
                      4; /* max sfbWidth = 96 ; 2^7=128 => 7/2 = 4 (spc*spc) */
206
0
    FIXP_DBL tmp = FL2FXCONST_DBL(0.0);
207
    /* don't use scaleValue() here, it increases workload quite sufficiently...
208
     */
209
0
    if (leadingBits >= 0) {
210
0
      for (j = bandOffset[i]; j < bandOffset[i + 1]; j++) {
211
0
        spec = mdctSpectrum[j] << leadingBits;
212
0
        tmp = fPow2AddDiv2(tmp, spec);
213
0
      }
214
0
    } else {
215
0
      INT shift = -leadingBits;
216
0
      for (j = bandOffset[i]; j < bandOffset[i + 1]; j++) {
217
0
        spec = mdctSpectrum[j] >> shift;
218
0
        tmp = fPow2AddDiv2(tmp, spec);
219
0
      }
220
0
    }
221
0
    bandEnergy[i] = tmp << 1;
222
0
  }
223
224
  /* calculate ld of bandNrg, subtract scaling */
225
0
  LdDataVector(bandEnergy, bandEnergyLdData, numBands);
226
0
  for (i = numBands; i-- != 0;) {
227
0
    FIXP_DBL scaleDiff = (sfbMaxScaleSpec[i] - 4) * FL2FXCONST_DBL(2.0 / 64);
228
229
0
    bandEnergyLdData[i] = (bandEnergyLdData[i] >=
230
0
                           ((FL2FXCONST_DBL(-1.f) >> 1) + (scaleDiff >> 1)))
231
0
                              ? bandEnergyLdData[i] - scaleDiff
232
0
                              : FL2FXCONST_DBL(-1.f);
233
    /* find maxNrgLd */
234
0
    maxNrgLd = fixMax(maxNrgLd, bandEnergyLdData[i]);
235
0
  }
236
237
0
  if (maxNrgLd <= (FIXP_DBL)0) {
238
0
    for (i = numBands; i-- != 0;) {
239
0
      INT scale = fixMin((sfbMaxScaleSpec[i] - 4) << 1, (DFRACT_BITS - 1));
240
0
      bandEnergy[i] = scaleValue(bandEnergy[i], -scale);
241
0
    }
242
0
    return 0;
243
0
  } else { /* scale down NRGs */
244
0
    while (maxNrgLd > FL2FXCONST_DBL(0.0f)) {
245
0
      maxNrgLd -= FL2FXCONST_DBL(2.0 / 64);
246
0
      shiftBits++;
247
0
    }
248
0
    for (i = numBands; i-- != 0;) {
249
0
      INT scale = fixMin(((sfbMaxScaleSpec[i] - 4) + shiftBits) << 1,
250
0
                         (DFRACT_BITS - 1));
251
0
      bandEnergyLdData[i] -= shiftBits * FL2FXCONST_DBL(2.0 / 64);
252
0
      bandEnergy[i] = scaleValue(bandEnergy[i], -scale);
253
0
    }
254
0
    return shiftBits;
255
0
  }
256
0
}
257
258
/*****************************************************************************
259
  functionname: FDKaacEnc_CalcBandEnergyOptimShort
260
  description:
261
  input:
262
  output:
263
*****************************************************************************/
264
void FDKaacEnc_CalcBandEnergyOptimShort(const FIXP_DBL *RESTRICT mdctSpectrum,
265
                                        INT *RESTRICT sfbMaxScaleSpec,
266
                                        const INT *RESTRICT bandOffset,
267
                                        const INT numBands,
268
0
                                        FIXP_DBL *RESTRICT bandEnergy) {
269
0
  INT i, j;
270
271
0
  for (i = 0; i < numBands; i++) {
272
0
    int leadingBits = sfbMaxScaleSpec[i] -
273
0
                      3; /* max sfbWidth = 36 ; 2^6=64 => 6/2 = 3 (spc*spc) */
274
0
    FIXP_DBL tmp = FL2FXCONST_DBL(0.0);
275
0
    for (j = bandOffset[i]; j < bandOffset[i + 1]; j++) {
276
0
      FIXP_DBL spec = scaleValue(mdctSpectrum[j], leadingBits);
277
0
      tmp = fPow2AddDiv2(tmp, spec);
278
0
    }
279
0
    bandEnergy[i] = tmp;
280
0
  }
281
282
0
  for (i = 0; i < numBands; i++) {
283
0
    INT scale = (2 * (sfbMaxScaleSpec[i] - 3)) -
284
0
                1; /* max sfbWidth = 36 ; 2^6=64 => 6/2 = 3 (spc*spc) */
285
0
    scale = fixMax(fixMin(scale, (DFRACT_BITS - 1)), -(DFRACT_BITS - 1));
286
0
    bandEnergy[i] = scaleValueSaturate(bandEnergy[i], -scale);
287
0
  }
288
0
}
289
290
/*****************************************************************************
291
  functionname: FDKaacEnc_CalcBandNrgMSOpt
292
  description:
293
  input:
294
  output:
295
*****************************************************************************/
296
void FDKaacEnc_CalcBandNrgMSOpt(
297
    const FIXP_DBL *RESTRICT mdctSpectrumLeft,
298
    const FIXP_DBL *RESTRICT mdctSpectrumRight,
299
    INT *RESTRICT sfbMaxScaleSpecLeft, INT *RESTRICT sfbMaxScaleSpecRight,
300
    const INT *RESTRICT bandOffset, const INT numBands,
301
    FIXP_DBL *RESTRICT bandEnergyMid, FIXP_DBL *RESTRICT bandEnergySide,
302
    INT calcLdData, FIXP_DBL *RESTRICT bandEnergyMidLdData,
303
0
    FIXP_DBL *RESTRICT bandEnergySideLdData) {
304
0
  INT i, j, minScale;
305
0
  FIXP_DBL NrgMid, NrgSide, specm, specs;
306
307
0
  for (i = 0; i < numBands; i++) {
308
0
    NrgMid = NrgSide = FL2FXCONST_DBL(0.0);
309
0
    minScale = fixMin(sfbMaxScaleSpecLeft[i], sfbMaxScaleSpecRight[i]) - 4;
310
0
    minScale = fixMax(0, minScale);
311
312
0
    if (minScale > 0) {
313
0
      for (j = bandOffset[i]; j < bandOffset[i + 1]; j++) {
314
0
        FIXP_DBL specL = mdctSpectrumLeft[j] << (minScale - 1);
315
0
        FIXP_DBL specR = mdctSpectrumRight[j] << (minScale - 1);
316
0
        specm = specL + specR;
317
0
        specs = specL - specR;
318
0
        NrgMid = fPow2AddDiv2(NrgMid, specm);
319
0
        NrgSide = fPow2AddDiv2(NrgSide, specs);
320
0
      }
321
0
    } else {
322
0
      for (j = bandOffset[i]; j < bandOffset[i + 1]; j++) {
323
0
        FIXP_DBL specL = mdctSpectrumLeft[j] >> 1;
324
0
        FIXP_DBL specR = mdctSpectrumRight[j] >> 1;
325
0
        specm = specL + specR;
326
0
        specs = specL - specR;
327
0
        NrgMid = fPow2AddDiv2(NrgMid, specm);
328
0
        NrgSide = fPow2AddDiv2(NrgSide, specs);
329
0
      }
330
0
    }
331
0
    bandEnergyMid[i] = fMin(NrgMid, (FIXP_DBL)MAXVAL_DBL >> 1) << 1;
332
0
    bandEnergySide[i] = fMin(NrgSide, (FIXP_DBL)MAXVAL_DBL >> 1) << 1;
333
0
  }
334
335
0
  if (calcLdData) {
336
0
    LdDataVector(bandEnergyMid, bandEnergyMidLdData, numBands);
337
0
    LdDataVector(bandEnergySide, bandEnergySideLdData, numBands);
338
0
  }
339
340
0
  for (i = 0; i < numBands; i++) {
341
0
    minScale = fixMin(sfbMaxScaleSpecLeft[i], sfbMaxScaleSpecRight[i]);
342
0
    INT scale = fixMax(0, 2 * (minScale - 4));
343
344
0
    if (calcLdData) {
345
      /* using the minimal scaling of left and right channel can cause very
346
      small energies; check ldNrg before subtract scaling multiplication:
347
      fract*INT we don't need fMult */
348
349
0
      int minus = scale * FL2FXCONST_DBL(1.0 / 64);
350
351
0
      if (bandEnergyMidLdData[i] != FL2FXCONST_DBL(-1.0f))
352
0
        bandEnergyMidLdData[i] -= minus;
353
354
0
      if (bandEnergySideLdData[i] != FL2FXCONST_DBL(-1.0f))
355
0
        bandEnergySideLdData[i] -= minus;
356
0
    }
357
0
    scale = fixMin(scale, (DFRACT_BITS - 1));
358
0
    bandEnergyMid[i] >>= scale;
359
0
    bandEnergySide[i] >>= scale;
360
0
  }
361
0
}