summaryrefslogtreecommitdiffstats
path: root/libAACdec/src/block.h
blob: a4ba0855e17ecece4191bbe4b64de30cd597cce2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*****************************  MPEG-4 AAC Decoder  **************************

                        (C) Copyright Fraunhofer IIS (2004)
                               All Rights Reserved

    Please be advised that this software and/or program delivery is
    Confidential Information of Fraunhofer and subject to and covered by the

    Fraunhofer IIS Software Evaluation Agreement
    between Google Inc. and  Fraunhofer
    effective and in full force since March 1, 2012.

    You may use this software and/or program only under the terms and
    conditions described in the above mentioned Fraunhofer IIS Software
    Evaluation Agreement. Any other and/or further use requires a separate agreement.


   $Id$
   Author(s):   Josef Hoepfl
   Description: long/short-block decoding

   This software and/or program is protected by copyright law and international
   treaties. Any reproduction or distribution of this software and/or program,
   or any portion of it, may result in severe civil and criminal penalties, and
   will be prosecuted to the maximum extent possible under law.

******************************************************************************/

#ifndef BLOCK_H
#define BLOCK_H

#include "common_fix.h"

#include "channelinfo.h"
#include "FDK_bitstream.h"

/* PNS (of block) */
void CPns_Read (CPnsData *pPnsData,
                HANDLE_FDK_BITSTREAM bs,
                const CodeBookDescription *hcb,
                SHORT *pScaleFactor,
                UCHAR global_gain,
                int band,
                int group);


void CPns_Apply (const CPnsData *pPnsData,
                 const CIcsInfo *pIcsInfo,
                 SPECTRAL_PTR pSpectrum,
                 const SHORT    *pSpecScale,
                 const SHORT    *pScaleFactor,
                 const SamplingRateInfo *pSamplingRateInfo,
                 const INT granuleLength,
                 const int channel);



/* TNS (of block) */
/*!
  \brief Read tns data-present flag from bitstream

  The function reads the data-present flag for tns from
  the bitstream.

  \return  none
*/
void CTns_ReadDataPresentFlag(HANDLE_FDK_BITSTREAM bs,
                              CTnsData *pTnsData);

void CTns_ReadDataPresentUsac(
        HANDLE_FDK_BITSTREAM hBs,
        CTnsData *pTnsData0,
        CTnsData *pTnsData1,
        const CIcsInfo *pIcsInfo,
        const UINT flags,
        const int fCommonWindow
        );

AAC_DECODER_ERROR CTns_Read(HANDLE_FDK_BITSTREAM bs,
                            CTnsData *pTnsData,
                            const CIcsInfo *pIcsInfo,
                            const UINT flags);

void CTns_Apply ( CTnsData *RESTRICT pTnsData, /*!< pointer to aac decoder info */
                  const CIcsInfo *pIcsInfo,
                  SPECTRAL_PTR pSpectralCoefficient,
                  const SamplingRateInfo *pSamplingRateInfo,
                  const INT granuleLength);

/* Block */

LONG CBlock_GetEscape(HANDLE_FDK_BITSTREAM bs, const LONG q);

/**
 * \brief Read scale factor data. See chapter 4.6.2.3.2 of ISO/IEC 14496-3.
 *        The SF_OFFSET = 100 value referenced in chapter 4.6.2.3.3 is already substracted
 *        from the scale factor values. Also includes PNS data reading.
 * \param bs bit stream handle data source
 * \param pAacDecoderChannelInfo channel context info were decoded data is stored into.
 * \param flags the decoder flags.
 */
AAC_DECODER_ERROR CBlock_ReadScaleFactorData(
        CAacDecoderChannelInfo *pAacDecoderChannelInfo,
        HANDLE_FDK_BITSTREAM bs,
        const UINT flags
        );

/**
 * \brief Read Huffman encoded spectral data.
 * \param pAacDecoderChannelInfo channel context info.
 * \param pSamplingRateInfo sampling rate info (sfb offsets).
 * \param flags syntax flags.
 */
AAC_DECODER_ERROR  CBlock_ReadSpectralData(HANDLE_FDK_BITSTREAM bs,
                                           CAacDecoderChannelInfo *pAacDecoderChannelInfo,
                                           const SamplingRateInfo *pSamplingRateInfo,
                                           const UINT  flags);


AAC_DECODER_ERROR CBlock_ReadSectionData(HANDLE_FDK_BITSTREAM bs,
                                         CAacDecoderChannelInfo *pAacDecoderChannelInfo,
                                         const SamplingRateInfo *pSamplingRateInfo,
                                         const UINT  flags);

/**
 * \brief find a common exponent (shift factor) for all sfb in each Spectral window, and store them into
 *        CAacDecoderChannelInfo::specScale.
 * \param pAacDecoderChannelInfo channel context info.
 * \param pSamplingRateInfo sampling rate info (sfb offsets).
 */
void CBlock_ScaleSpectralData(CAacDecoderChannelInfo *pAacDecoderChannelInfo, SamplingRateInfo *pSamplingRateInfo);

/**
 * \brief Apply TNS and PNS tools.
 */
void ApplyTools ( CAacDecoderChannelInfo *pAacDecoderChannelInfo[],
                  const SamplingRateInfo *pSamplingRateInfo,
                  const UINT flags,
                  const int channel );

/**
 * \brief Transform MDCT spectral data into time domain
 */
void CBlock_FrequencyToTime(CAacDecoderStaticChannelInfo *pAacDecoderStaticChannelInfo,
                            CAacDecoderChannelInfo *pAacDecoderChannelInfo,
                            INT_PCM outSamples[],
                            const SHORT frameLen,
                            const int stride,
                            const int frameOk,
                            FIXP_DBL *pWorkBuffer1);

/**
 * \brief Transform double lapped MDCT (AAC-ELD) spectral data into time domain.
 */
void CBlock_FrequencyToTimeLowDelay(CAacDecoderStaticChannelInfo *pAacDecoderStaticChannelInfo,
                                    CAacDecoderChannelInfo *pAacDecoderChannelInfo,
                                    INT_PCM outSamples[],
                                    const short frameLen,
                                    const char stride);

AAC_DECODER_ERROR CBlock_InverseQuantizeSpectralData(CAacDecoderChannelInfo *pAacDecoderChannelInfo, SamplingRateInfo *pSamplingRateInfo);

/**
 * \brief Calculate 2^(lsb/4) * value^(4/3)
 * \param pValue pointer to quantized value. The inverse quantized result is stored back here.
 * \param lsb 2 LSBs of the scale factor (scaleFactor % 4) applied as power 2 factor to the
 *        resulting inverse quantized value.
 * \return the exponent of the result (mantissa) stored into *pValue.
 */
FDK_INLINE
int EvaluatePower43 ( FIXP_DBL *pValue,
                      UINT lsb )
{
  FIXP_DBL value;
  UINT freeBits;
  UINT exponent;

  value = *pValue;
  freeBits = fNormz (value) ;
  exponent = DFRACT_BITS - freeBits ;
  FDK_ASSERT (exponent < 14);

  UINT x = (((int)value << freeBits) >> 19) ;
  UINT tableIndex = (x & 0x0FFF) >> 4 ;
  FIXP_DBL invQVal ;

  x = x & 0x0F;

  UINT r0=(LONG)InverseQuantTable [tableIndex+0];
  UINT r1=(LONG)InverseQuantTable [tableIndex+1];
  USHORT nx=16-x;
  UINT temp=(r0)*nx+(r1)*x;
  invQVal = (FIXP_DBL)temp;

  FDK_ASSERT(lsb < 4);
  *pValue = fMultDiv2 (invQVal, MantissaTable [lsb][exponent]) ;

  /* + 1 compensates fMultDiv2(). */
  return ExponentTable [lsb][exponent] + 1;
}

/**
 * \brief determine the required shift scale for the given quantized value and scale (factor % 4) value.
 */
FDK_INLINE int GetScaleFromValue (FIXP_DBL value, unsigned int lsb)
{
  if (value!=(FIXP_DBL)0)
  {
    int scale = EvaluatePower43 (&value, lsb) ;
    return CntLeadingZeros (value) - scale - 2 ;
  }
  else
    return 0; /* Return zero, because its useless to scale a zero value, saves workload and avoids scaling overshifts. */
}


//#ifdef AACDEC_HUFFMANDECODER_ENABLE

/*!
  \brief Read huffman codeword

  The function reads the huffman codeword from the bitstream and
  returns the index value.

  \return  index value
*/
inline int CBlock_DecodeHuffmanWord( HANDLE_FDK_BITSTREAM bs,                           /*!< pointer to bitstream */
                                     const CodeBookDescription *hcb )                   /*!< pointer to codebook description */
{
  UINT val;
  UINT index = 0;
  const USHORT (*CodeBook) [HuffmanEntries] = hcb->CodeBook;

  while (1)
  {
    val = CodeBook[index][FDKreadBits(bs,HuffmanBits)]; /* Expensive memory access */

    if ((val & 1) == 0)
    {
      index = val>>2;
      continue;
    }
    else
    {
      if (val & 2)
      {
        FDKpushBackCache(bs,1);
      }

      val >>= 2;
      break;
    }
  }

  return val;
}

//#endif /* AACDEC_HUFFMANDECODER_ENABLE */

#endif /* #ifndef BLOCK_H */