Coverage Report

Created: 2024-06-17 06:33

/src/aac/libAACdec/src/block.h
Line
Count
Source
1
/* -----------------------------------------------------------------------------
2
Software License for The Fraunhofer FDK AAC Codec Library for Android
3
4
© Copyright  1995 - 2019 Fraunhofer-Gesellschaft zur Förderung der angewandten
5
Forschung e.V. All rights reserved.
6
7
 1.    INTRODUCTION
8
The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software
9
that implements the MPEG Advanced Audio Coding ("AAC") encoding and decoding
10
scheme for digital audio. This FDK AAC Codec software is intended to be used on
11
a wide variety of Android devices.
12
13
AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient
14
general perceptual audio codecs. AAC-ELD is considered the best-performing
15
full-bandwidth communications codec by independent studies and is widely
16
deployed. AAC has been standardized by ISO and IEC as part of the MPEG
17
specifications.
18
19
Patent licenses for necessary patent claims for the FDK AAC Codec (including
20
those of Fraunhofer) may be obtained through Via Licensing
21
(www.vialicensing.com) or through the respective patent owners individually for
22
the purpose of encoding or decoding bit streams in products that are compliant
23
with the ISO/IEC MPEG audio standards. Please note that most manufacturers of
24
Android devices already license these patent claims through Via Licensing or
25
directly from the patent owners, and therefore FDK AAC Codec software may
26
already be covered under those patent licenses when it is used for those
27
licensed purposes only.
28
29
Commercially-licensed AAC software libraries, including floating-point versions
30
with enhanced sound quality, are also available from Fraunhofer. Users are
31
encouraged to check the Fraunhofer website for additional applications
32
information and documentation.
33
34
2.    COPYRIGHT LICENSE
35
36
Redistribution and use in source and binary forms, with or without modification,
37
are permitted without payment of copyright license fees provided that you
38
satisfy the following conditions:
39
40
You must retain the complete text of this software license in redistributions of
41
the FDK AAC Codec or your modifications thereto in source code form.
42
43
You must retain the complete text of this software license in the documentation
44
and/or other materials provided with redistributions of the FDK AAC Codec or
45
your modifications thereto in binary form. You must make available free of
46
charge copies of the complete source code of the FDK AAC Codec and your
47
modifications thereto to recipients of copies in binary form.
48
49
The name of Fraunhofer may not be used to endorse or promote products derived
50
from this library without prior written permission.
51
52
You may not charge copyright license fees for anyone to use, copy or distribute
53
the FDK AAC Codec software or your modifications thereto.
54
55
Your modified versions of the FDK AAC Codec must carry prominent notices stating
56
that you changed the software and the date of any change. For modified versions
57
of the FDK AAC Codec, the term "Fraunhofer FDK AAC Codec Library for Android"
58
must be replaced by the term "Third-Party Modified Version of the Fraunhofer FDK
59
AAC Codec Library for Android."
60
61
3.    NO PATENT LICENSE
62
63
NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without
64
limitation the patents of Fraunhofer, ARE GRANTED BY THIS SOFTWARE LICENSE.
65
Fraunhofer provides no warranty of patent non-infringement with respect to this
66
software.
67
68
You may use this FDK AAC Codec software or modifications thereto only for
69
purposes that are authorized by appropriate patent licenses.
70
71
4.    DISCLAIMER
72
73
This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright
74
holders and contributors "AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES,
75
including but not limited to the implied warranties of merchantability and
76
fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
77
CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary,
78
or consequential damages, including but not limited to procurement of substitute
79
goods or services; loss of use, data, or profits, or business interruption,
80
however caused and on any theory of liability, whether in contract, strict
81
liability, or tort (including negligence), arising in any way out of the use of
82
this software, even if advised of the possibility of such damage.
83
84
5.    CONTACT INFORMATION
85
86
Fraunhofer Institute for Integrated Circuits IIS
87
Attention: Audio and Multimedia Departments - FDK AAC LL
88
Am Wolfsmantel 33
89
91058 Erlangen, Germany
90
91
www.iis.fraunhofer.de/amm
92
amm-info@iis.fraunhofer.de
93
----------------------------------------------------------------------------- */
94
95
/**************************** AAC decoder library ******************************
96
97
   Author(s):   Josef Hoepfl
98
99
   Description: long/short-block decoding
100
101
*******************************************************************************/
102
103
#ifndef BLOCK_H
104
#define BLOCK_H
105
106
#include "common_fix.h"
107
108
#include "channelinfo.h"
109
#include "FDK_bitstream.h"
110
111
/* PNS (of block) */
112
void CPns_Read(CPnsData *pPnsData, HANDLE_FDK_BITSTREAM bs,
113
               const CodeBookDescription *hcb, SHORT *pScaleFactor,
114
               UCHAR global_gain, int band, int group);
115
116
void CPns_Apply(const CPnsData *pPnsData, const CIcsInfo *pIcsInfo,
117
                SPECTRAL_PTR pSpectrum, const SHORT *pSpecScale,
118
                const SHORT *pScaleFactor,
119
                const SamplingRateInfo *pSamplingRateInfo,
120
                const INT granuleLength, const int channel);
121
122
void CBlock_ApplyNoise(CAacDecoderChannelInfo *pAacDecoderChannelInfo,
123
                       SamplingRateInfo *pSamplingRateInfo, ULONG *nfRandomSeed,
124
                       UCHAR *band_is_noise);
125
126
/* TNS (of block) */
127
/*!
128
  \brief Read tns data-present flag from bitstream
129
130
  The function reads the data-present flag for tns from
131
  the bitstream.
132
133
  \return  none
134
*/
135
void CTns_ReadDataPresentFlag(HANDLE_FDK_BITSTREAM bs, CTnsData *pTnsData);
136
137
void CTns_ReadDataPresentUsac(HANDLE_FDK_BITSTREAM hBs, CTnsData *pTnsData0,
138
                              CTnsData *pTnsData1, UCHAR *ptns_on_lr,
139
                              const CIcsInfo *pIcsInfo, const UINT flags,
140
                              const UINT elFlags, const int fCommonWindow);
141
142
AAC_DECODER_ERROR CTns_Read(HANDLE_FDK_BITSTREAM bs, CTnsData *pTnsData,
143
                            const CIcsInfo *pIcsInfo, const UINT flags);
144
145
void CTns_Apply(CTnsData *RESTRICT pTnsData, /*!< pointer to aac decoder info */
146
                const CIcsInfo *pIcsInfo, SPECTRAL_PTR pSpectralCoefficient,
147
                const SamplingRateInfo *pSamplingRateInfo,
148
                const INT granuleLength, const UCHAR nbands,
149
                const UCHAR igf_active, const UINT flags);
150
151
/* Block */
152
153
LONG CBlock_GetEscape(HANDLE_FDK_BITSTREAM bs, const LONG q);
154
155
/**
156
 * \brief Read scale factor data. See chapter 4.6.2.3.2 of ISO/IEC 14496-3.
157
 *        The SF_OFFSET = 100 value referenced in chapter 4.6.2.3.3 is already
158
 * substracted from the scale factor values. Also includes PNS data reading.
159
 * \param bs bit stream handle data source
160
 * \param pAacDecoderChannelInfo channel context info were decoded data is
161
 * stored into.
162
 * \param flags the decoder flags.
163
 */
164
AAC_DECODER_ERROR CBlock_ReadScaleFactorData(
165
    CAacDecoderChannelInfo *pAacDecoderChannelInfo, HANDLE_FDK_BITSTREAM bs,
166
    const UINT flags);
167
168
/**
169
 * \brief Read Huffman encoded spectral data.
170
 * \param pAacDecoderChannelInfo channel context info.
171
 * \param pSamplingRateInfo sampling rate info (sfb offsets).
172
 * \param flags syntax flags.
173
 */
174
AAC_DECODER_ERROR CBlock_ReadSpectralData(
175
    HANDLE_FDK_BITSTREAM bs, CAacDecoderChannelInfo *pAacDecoderChannelInfo,
176
    const SamplingRateInfo *pSamplingRateInfo, const UINT flags);
177
178
/**
179
 * \brief Read Arithmetic encoded spectral data.
180
 * \param pAacDecoderChannelInfo channel context info.
181
 * \param pAacDecoderStaticChannelInfo static channel context info.
182
 * \param pSamplingRateInfo sampling rate info (sfb offsets).
183
 * \param frame_length spectral window length.
184
 * \param flags syntax flags.
185
 * \return error code.
186
 */
187
AAC_DECODER_ERROR CBlock_ReadAcSpectralData(
188
    HANDLE_FDK_BITSTREAM hBs, CAacDecoderChannelInfo *pAacDecoderChannelInfo,
189
    CAacDecoderStaticChannelInfo *pAacDecoderStaticChannelInfo,
190
    const SamplingRateInfo *pSamplingRateInfo, const UINT frame_length,
191
    const UINT flags);
192
193
AAC_DECODER_ERROR CBlock_ReadSectionData(
194
    HANDLE_FDK_BITSTREAM bs, CAacDecoderChannelInfo *pAacDecoderChannelInfo,
195
    const SamplingRateInfo *pSamplingRateInfo, const UINT flags);
196
197
/**
198
 * \brief find a common exponent (shift factor) for all sfb in each Spectral
199
 * window, and store them into CAacDecoderChannelInfo::specScale.
200
 * \param pAacDecoderChannelInfo channel context info.
201
 * \param UCHAR maxSfbs maximum number of SFBs to be processed (might differ
202
 * from pAacDecoderChannelInfo->icsInfo.MaxSfBands)
203
 * \param pSamplingRateInfo sampling rate info (sfb offsets).
204
 */
205
void CBlock_ScaleSpectralData(CAacDecoderChannelInfo *pAacDecoderChannelInfo,
206
                              UCHAR maxSfbs,
207
                              SamplingRateInfo *pSamplingRateInfo);
208
209
/**
210
 * \brief Apply TNS and PNS tools.
211
 */
212
void ApplyTools(CAacDecoderChannelInfo *pAacDecoderChannelInfo[],
213
                const SamplingRateInfo *pSamplingRateInfo, const UINT flags,
214
                const UINT elFlags, const int channel, const int maybe_jstereo);
215
216
/**
217
 * \brief Transform MDCT spectral data into time domain
218
 */
219
void CBlock_FrequencyToTime(
220
    CAacDecoderStaticChannelInfo *pAacDecoderStaticChannelInfo,
221
    CAacDecoderChannelInfo *pAacDecoderChannelInfo, PCM_DEC outSamples[],
222
    const SHORT frameLen, const int frameOk, FIXP_DBL *pWorkBuffer1,
223
    const INT aacOutDataHeadroom, UINT elFlags, INT elCh);
224
225
/**
226
 * \brief Transform double lapped MDCT (AAC-ELD) spectral data into time domain.
227
 */
228
void CBlock_FrequencyToTimeLowDelay(
229
    CAacDecoderStaticChannelInfo *pAacDecoderStaticChannelInfo,
230
    CAacDecoderChannelInfo *pAacDecoderChannelInfo, PCM_DEC outSamples[],
231
    const short frameLen);
232
233
AAC_DECODER_ERROR CBlock_InverseQuantizeSpectralData(
234
    CAacDecoderChannelInfo *pAacDecoderChannelInfo,
235
    SamplingRateInfo *pSamplingRateInfo, UCHAR *band_is_noise,
236
    UCHAR active_band_search);
237
238
/**
239
 * \brief Calculate 2^(lsb/4) * value^(4/3)
240
 * \param pValue pointer to quantized value. The inverse quantized result is
241
 * stored back here.
242
 * \param lsb 2 LSBs of the scale factor (scaleFactor % 4) applied as power 2
243
 * factor to the resulting inverse quantized value.
244
 * \return the exponent of the result (mantissa) stored into *pValue.
245
 */
246
FDK_INLINE
247
1.38M
int EvaluatePower43(FIXP_DBL *pValue, UINT lsb) {
248
1.38M
  FIXP_DBL value;
249
1.38M
  UINT freeBits;
250
1.38M
  UINT exponent;
251
252
1.38M
  value = *pValue;
253
1.38M
  freeBits = fNormz(value);
254
1.38M
  exponent = DFRACT_BITS - freeBits;
255
1.38M
  FDK_ASSERT(exponent < 14);
256
257
1.38M
  UINT x = (((int)value << freeBits) >> 19);
258
1.38M
  UINT tableIndex = (x & 0x0FFF) >> 4;
259
1.38M
  FIXP_DBL invQVal;
260
261
1.38M
  x = x & 0x0F;
262
263
1.38M
  UINT r0 = (LONG)InverseQuantTable[tableIndex + 0];
264
1.38M
  UINT r1 = (LONG)InverseQuantTable[tableIndex + 1];
265
1.38M
  USHORT nx = 16 - x;
266
1.38M
  UINT temp = (r0)*nx + (r1)*x;
267
1.38M
  invQVal = (FIXP_DBL)temp;
268
269
1.38M
  FDK_ASSERT(lsb < 4);
270
1.38M
  *pValue = fMultDiv2(invQVal, MantissaTable[lsb][exponent]);
271
272
  /* + 1 compensates fMultDiv2(). */
273
1.38M
  return ExponentTable[lsb][exponent] + 1;
274
1.38M
}
Unexecuted instantiation: aac_ram.cpp:EvaluatePower43(int*, unsigned int)
aacdec_hcr.cpp:EvaluatePower43(int*, unsigned int)
Line
Count
Source
247
39.8k
int EvaluatePower43(FIXP_DBL *pValue, UINT lsb) {
248
39.8k
  FIXP_DBL value;
249
39.8k
  UINT freeBits;
250
39.8k
  UINT exponent;
251
252
39.8k
  value = *pValue;
253
39.8k
  freeBits = fNormz(value);
254
39.8k
  exponent = DFRACT_BITS - freeBits;
255
39.8k
  FDK_ASSERT(exponent < 14);
256
257
39.8k
  UINT x = (((int)value << freeBits) >> 19);
258
39.8k
  UINT tableIndex = (x & 0x0FFF) >> 4;
259
39.8k
  FIXP_DBL invQVal;
260
261
39.8k
  x = x & 0x0F;
262
263
39.8k
  UINT r0 = (LONG)InverseQuantTable[tableIndex + 0];
264
39.8k
  UINT r1 = (LONG)InverseQuantTable[tableIndex + 1];
265
39.8k
  USHORT nx = 16 - x;
266
39.8k
  UINT temp = (r0)*nx + (r1)*x;
267
39.8k
  invQVal = (FIXP_DBL)temp;
268
269
39.8k
  FDK_ASSERT(lsb < 4);
270
39.8k
  *pValue = fMultDiv2(invQVal, MantissaTable[lsb][exponent]);
271
272
  /* + 1 compensates fMultDiv2(). */
273
39.8k
  return ExponentTable[lsb][exponent] + 1;
274
39.8k
}
Unexecuted instantiation: aacdec_hcrs.cpp:EvaluatePower43(int*, unsigned int)
Unexecuted instantiation: aacdec_pns.cpp:EvaluatePower43(int*, unsigned int)
Unexecuted instantiation: aacdecoder.cpp:EvaluatePower43(int*, unsigned int)
Unexecuted instantiation: aacdecoder_lib.cpp:EvaluatePower43(int*, unsigned int)
block.cpp:EvaluatePower43(int*, unsigned int)
Line
Count
Source
247
1.34M
int EvaluatePower43(FIXP_DBL *pValue, UINT lsb) {
248
1.34M
  FIXP_DBL value;
249
1.34M
  UINT freeBits;
250
1.34M
  UINT exponent;
251
252
1.34M
  value = *pValue;
253
1.34M
  freeBits = fNormz(value);
254
1.34M
  exponent = DFRACT_BITS - freeBits;
255
1.34M
  FDK_ASSERT(exponent < 14);
256
257
1.34M
  UINT x = (((int)value << freeBits) >> 19);
258
1.34M
  UINT tableIndex = (x & 0x0FFF) >> 4;
259
1.34M
  FIXP_DBL invQVal;
260
261
1.34M
  x = x & 0x0F;
262
263
1.34M
  UINT r0 = (LONG)InverseQuantTable[tableIndex + 0];
264
1.34M
  UINT r1 = (LONG)InverseQuantTable[tableIndex + 1];
265
1.34M
  USHORT nx = 16 - x;
266
1.34M
  UINT temp = (r0)*nx + (r1)*x;
267
1.34M
  invQVal = (FIXP_DBL)temp;
268
269
1.34M
  FDK_ASSERT(lsb < 4);
270
1.34M
  *pValue = fMultDiv2(invQVal, MantissaTable[lsb][exponent]);
271
272
  /* + 1 compensates fMultDiv2(). */
273
1.34M
  return ExponentTable[lsb][exponent] + 1;
274
1.34M
}
Unexecuted instantiation: channel.cpp:EvaluatePower43(int*, unsigned int)
Unexecuted instantiation: channelinfo.cpp:EvaluatePower43(int*, unsigned int)
Unexecuted instantiation: conceal.cpp:EvaluatePower43(int*, unsigned int)
Unexecuted instantiation: rvlc.cpp:EvaluatePower43(int*, unsigned int)
Unexecuted instantiation: rvlcbit.cpp:EvaluatePower43(int*, unsigned int)
Unexecuted instantiation: rvlcconceal.cpp:EvaluatePower43(int*, unsigned int)
Unexecuted instantiation: stereo.cpp:EvaluatePower43(int*, unsigned int)
Unexecuted instantiation: usacdec_lpd.cpp:EvaluatePower43(int*, unsigned int)
275
276
/* Recalculate gain */
277
FIXP_DBL get_gain(const FIXP_DBL *x, const FIXP_DBL *y, int n);
278
279
/**
280
 * \brief determine the required shift scale for the given quantized value and
281
 * scale (factor % 4) value.
282
 */
283
165k
FDK_INLINE int GetScaleFromValue(FIXP_DBL value, unsigned int lsb) {
284
165k
  if (value != (FIXP_DBL)0) {
285
39.8k
    int scale = EvaluatePower43(&value, lsb);
286
39.8k
    return CntLeadingZeros(value) - scale - 2;
287
39.8k
  } else
288
125k
    return 0; /* Return zero, because its useless to scale a zero value, saves
289
                 workload and avoids scaling overshifts. */
290
165k
}
Unexecuted instantiation: aac_ram.cpp:GetScaleFromValue(int, unsigned int)
aacdec_hcr.cpp:GetScaleFromValue(int, unsigned int)
Line
Count
Source
283
165k
FDK_INLINE int GetScaleFromValue(FIXP_DBL value, unsigned int lsb) {
284
165k
  if (value != (FIXP_DBL)0) {
285
39.8k
    int scale = EvaluatePower43(&value, lsb);
286
39.8k
    return CntLeadingZeros(value) - scale - 2;
287
39.8k
  } else
288
125k
    return 0; /* Return zero, because its useless to scale a zero value, saves
289
                 workload and avoids scaling overshifts. */
290
165k
}
Unexecuted instantiation: aacdec_hcrs.cpp:GetScaleFromValue(int, unsigned int)
Unexecuted instantiation: aacdec_pns.cpp:GetScaleFromValue(int, unsigned int)
Unexecuted instantiation: aacdecoder.cpp:GetScaleFromValue(int, unsigned int)
Unexecuted instantiation: aacdecoder_lib.cpp:GetScaleFromValue(int, unsigned int)
Unexecuted instantiation: block.cpp:GetScaleFromValue(int, unsigned int)
Unexecuted instantiation: channel.cpp:GetScaleFromValue(int, unsigned int)
Unexecuted instantiation: channelinfo.cpp:GetScaleFromValue(int, unsigned int)
Unexecuted instantiation: conceal.cpp:GetScaleFromValue(int, unsigned int)
Unexecuted instantiation: rvlc.cpp:GetScaleFromValue(int, unsigned int)
Unexecuted instantiation: rvlcbit.cpp:GetScaleFromValue(int, unsigned int)
Unexecuted instantiation: rvlcconceal.cpp:GetScaleFromValue(int, unsigned int)
Unexecuted instantiation: stereo.cpp:GetScaleFromValue(int, unsigned int)
Unexecuted instantiation: usacdec_lpd.cpp:GetScaleFromValue(int, unsigned int)
291
292
/*!
293
  \brief Read huffman codeword
294
295
  The function reads the huffman codeword from the bitstream and
296
  returns the index value.
297
298
  \return  index value
299
*/
300
inline int CBlock_DecodeHuffmanWord(
301
    HANDLE_FDK_BITSTREAM bs,        /*!< pointer to bitstream */
302
    const CodeBookDescription *hcb) /*!< pointer to codebook description */
303
62.1k
{
304
62.1k
  UINT val;
305
62.1k
  UINT index = 0;
306
62.1k
  const USHORT(*CodeBook)[HuffmanEntries] = hcb->CodeBook;
307
308
104k
  while (1) {
309
104k
    val = CodeBook[index]
310
104k
                  [FDKreadBits(bs, HuffmanBits)]; /* Expensive memory access */
311
312
104k
    if ((val & 1) == 0) {
313
42.0k
      index = val >> 2;
314
42.0k
      continue;
315
62.1k
    } else {
316
62.1k
      if (val & 2) {
317
47.6k
        FDKpushBackCache(bs, 1);
318
47.6k
      }
319
320
62.1k
      val >>= 2;
321
62.1k
      break;
322
62.1k
    }
323
104k
  }
324
325
62.1k
  return val;
326
62.1k
}
327
inline int CBlock_DecodeHuffmanWordCB(
328
    HANDLE_FDK_BITSTREAM bs, /*!< pointer to bitstream */
329
    const USHORT (
330
        *CodeBook)[HuffmanEntries]) /*!< pointer to codebook description */
331
2.58M
{
332
2.58M
  UINT index = 0;
333
334
4.26M
  while (1) {
335
4.26M
    index = CodeBook[index][FDKread2Bits(bs)]; /* Expensive memory access */
336
4.26M
    if (index & 1) break;
337
1.67M
    index >>= 2;
338
1.67M
  }
339
2.58M
  if (index & 2) {
340
2.05M
    FDKpushBackCache(bs, 1);
341
2.05M
  }
342
2.58M
  return index >> 2;
343
2.58M
}
344
345
#endif /* #ifndef BLOCK_H */