1 /* ----------------------------------------------------------------------------- 2 Software License for The Fraunhofer FDK AAC Codec Library for Android 3 4 Copyright 1995 - 2018 Fraunhofer-Gesellschaft zur Frderung 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 (at) 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, FIXP_PCM outSamples[], 222 const SHORT frameLen, const int frameOk, FIXP_DBL *pWorkBuffer1, 223 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, FIXP_PCM 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 int EvaluatePower43(FIXP_DBL *pValue, UINT lsb) { 248 FIXP_DBL value; 249 UINT freeBits; 250 UINT exponent; 251 252 value = *pValue; 253 freeBits = fNormz(value); 254 exponent = DFRACT_BITS - freeBits; 255 FDK_ASSERT(exponent < 14); 256 257 UINT x = (((int)value << freeBits) >> 19); 258 UINT tableIndex = (x & 0x0FFF) >> 4; 259 FIXP_DBL invQVal; 260 261 x = x & 0x0F; 262 263 UINT r0 = (LONG)InverseQuantTable[tableIndex + 0]; 264 UINT r1 = (LONG)InverseQuantTable[tableIndex + 1]; 265 USHORT nx = 16 - x; 266 UINT temp = (r0)*nx + (r1)*x; 267 invQVal = (FIXP_DBL)temp; 268 269 FDK_ASSERT(lsb < 4); 270 *pValue = fMultDiv2(invQVal, MantissaTable[lsb][exponent]); 271 272 /* + 1 compensates fMultDiv2(). */ 273 return ExponentTable[lsb][exponent] + 1; 274 } 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 FDK_INLINE int GetScaleFromValue(FIXP_DBL value, unsigned int lsb) { 284 if (value != (FIXP_DBL)0) { 285 int scale = EvaluatePower43(&value, lsb); 286 return CntLeadingZeros(value) - scale - 2; 287 } else 288 return 0; /* Return zero, because its useless to scale a zero value, saves 289 workload and avoids scaling overshifts. */ 290 } 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 { 304 UINT val; 305 UINT index = 0; 306 const USHORT(*CodeBook)[HuffmanEntries] = hcb->CodeBook; 307 308 while (1) { 309 val = CodeBook[index] 310 [FDKreadBits(bs, HuffmanBits)]; /* Expensive memory access */ 311 312 if ((val & 1) == 0) { 313 index = val >> 2; 314 continue; 315 } else { 316 if (val & 2) { 317 FDKpushBackCache(bs, 1); 318 } 319 320 val >>= 2; 321 break; 322 } 323 } 324 325 return val; 326 } 327 inline int CBlock_DecodeHuffmanWordCB( 328 HANDLE_FDK_BITSTREAM bs, /*!< pointer to bitstream */ 329 const USHORT ( 330 *CodeBook)[HuffmanEntries]) /*!< pointer to codebook description */ 331 { 332 UINT index = 0; 333 334 while (1) { 335 index = CodeBook[index][FDKread2Bits(bs)]; /* Expensive memory access */ 336 if (index & 1) break; 337 index >>= 2; 338 } 339 if (index & 2) { 340 FDKpushBackCache(bs, 1); 341 } 342 return index >> 2; 343 } 344 345 #endif /* #ifndef BLOCK_H */ 346