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: 100 101 *******************************************************************************/ 102 103 #ifndef AACDECODER_H 104 #define AACDECODER_H 105 106 #include "common_fix.h" 107 108 #include "FDK_bitstream.h" 109 110 #include "channel.h" 111 112 #include "tpdec_lib.h" 113 #include "FDK_audio.h" 114 115 #include "block.h" 116 117 #include "genericStds.h" 118 119 #include "FDK_qmf_domain.h" 120 121 #include "sbrdecoder.h" 122 123 #include "aacdec_drc.h" 124 125 #include "pcmdmx_lib.h" 126 127 #include "FDK_drcDecLib.h" 128 129 #include "limiter.h" 130 131 #include "FDK_delay.h" 132 133 #define TIME_DATA_FLUSH_SIZE (128) 134 #define TIME_DATA_FLUSH_SIZE_SF (7) 135 136 #define AACDEC_MAX_NUM_PREROLL_AU_USAC (3) 137 #if (AACDEC_MAX_NUM_PREROLL_AU < 3) 138 #undef AACDEC_MAX_NUM_PREROLL_AU 139 #define AACDEC_MAX_NUM_PREROLL_AU AACDEC_MAX_NUM_PREROLL_AU_USAC 140 #endif 141 142 typedef struct AAC_DECODER_INSTANCE *HANDLE_AACDECODER; 143 144 enum { L = 0, R = 1 }; 145 146 typedef struct { 147 unsigned char *buffer; 148 int bufferSize; 149 int offset[8]; 150 int nrElements; 151 } CAncData; 152 153 typedef enum { NOT_DEFINED = -1, MODE_HQ = 0, MODE_LP = 1 } QMF_MODE; 154 155 typedef struct { 156 int bsDelay; 157 } SBR_PARAMS; 158 159 enum { 160 AACDEC_FLUSH_OFF = 0, 161 AACDEC_RSV60_CFG_CHANGE_ATSC_FLUSH_ON = 1, 162 AACDEC_RSV60_DASH_IPF_ATSC_FLUSH_ON = 2, 163 AACDEC_USAC_DASH_IPF_FLUSH_ON = 3 164 }; 165 166 enum { 167 AACDEC_BUILD_UP_OFF = 0, 168 AACDEC_RSV60_BUILD_UP_ON = 1, 169 AACDEC_RSV60_BUILD_UP_ON_IN_BAND = 2, 170 AACDEC_USAC_BUILD_UP_ON = 3, 171 AACDEC_RSV60_BUILD_UP_IDLE = 4, 172 AACDEC_RSV60_BUILD_UP_IDLE_IN_BAND = 5 173 }; 174 175 typedef struct { 176 /* Usac Extension Elements */ 177 USAC_EXT_ELEMENT_TYPE usacExtElementType[(3)]; 178 UINT usacExtElementDefaultLength[(3)]; 179 UCHAR usacExtElementPayloadFrag[(3)]; 180 } CUsacCoreExtensions; 181 182 /* AAC decoder (opaque toward userland) struct declaration */ 183 struct AAC_DECODER_INSTANCE { 184 INT aacChannels; /*!< Amount of AAC decoder channels allocated. */ 185 INT ascChannels[(1 * 186 1)]; /*!< Amount of AAC decoder channels signalled in ASC. */ 187 INT blockNumber; /*!< frame counter */ 188 189 INT nrOfLayers; 190 191 INT outputInterleaved; /*!< PCM output format (interleaved/none interleaved). 192 */ 193 194 INT aacOutDataHeadroom; /*!< Headroom of the output time signal to prevent 195 clipping */ 196 197 HANDLE_TRANSPORTDEC hInput; /*!< Transport layer handle. */ 198 199 SamplingRateInfo 200 samplingRateInfo[(1 * 1)]; /*!< Sampling Rate information table */ 201 202 UCHAR 203 frameOK; /*!< Will be unset if a consistency check, e.g. CRC etc. fails */ 204 205 UINT flags[(1 * 1)]; /*!< Flags for internal decoder use. DO NOT USE 206 self::streaminfo::flags ! */ 207 UINT elFlags[(3 * ((8) * 2) + (((8) * 2)) / 2 + 4 * (1) + 208 1)]; /*!< Flags for internal decoder use (element specific). DO 209 NOT USE self::streaminfo::flags ! */ 210 211 MP4_ELEMENT_ID elements[(3 * ((8) * 2) + (((8) * 2)) / 2 + 4 * (1) + 212 1)]; /*!< Table where the element Id's are listed */ 213 UCHAR elTags[(3 * ((8) * 2) + (((8) * 2)) / 2 + 4 * (1) + 214 1)]; /*!< Table where the elements id Tags are listed */ 215 UCHAR chMapping[((8) * 2)]; /*!< Table of MPEG canonical order to bitstream 216 channel order mapping. */ 217 218 AUDIO_CHANNEL_TYPE channelType[(8)]; /*!< Audio channel type of each output 219 audio channel (from 0 upto 220 numChannels). */ 221 UCHAR channelIndices[(8)]; /*!< Audio channel index for each output audio 222 channel (from 0 upto numChannels). */ 223 /* See ISO/IEC 13818-7:2005(E), 8.5.3.2 Explicit channel mapping using a 224 * program_config_element() */ 225 226 FDK_channelMapDescr mapDescr; /*!< Describes the output channel mapping. */ 227 UCHAR chMapIndex; /*!< Index to access one line of the channelOutputMapping 228 table. This is required because not all 8 channel 229 configurations have the same output mapping. */ 230 INT sbrDataLen; /*!< Expected length of the SBR remaining in bitbuffer after 231 the AAC payload has been pared. */ 232 233 CProgramConfig pce; 234 CStreamInfo 235 streamInfo; /*!< Pointer to StreamInfo data (read from the bitstream) */ 236 CAacDecoderChannelInfo 237 *pAacDecoderChannelInfo[(8)]; /*!< Temporal channel memory */ 238 CAacDecoderStaticChannelInfo 239 *pAacDecoderStaticChannelInfo[(8)]; /*!< Persistent channel memory */ 240 241 FIXP_DBL *workBufferCore1; 242 FIXP_DBL *workBufferCore2; 243 PCM_DEC *pTimeData2; 244 INT timeData2Size; 245 246 CpePersistentData *cpeStaticData[( 247 3 * ((8) * 2) + (((8) * 2)) / 2 + 4 * (1) + 248 1)]; /*!< Pointer to persistent data shared by both channels of a CPE. 249 This structure is allocated once for each CPE. */ 250 251 CConcealParams concealCommonData; 252 CConcealmentMethod concealMethodUser; 253 254 CUsacCoreExtensions usacCoreExt; /*!< Data and handles to extend USAC FD/LPD 255 core decoder (SBR, MPS, ...) */ 256 UINT numUsacElements[(1 * 1)]; 257 UCHAR usacStereoConfigIndex[(3 * ((8) * 2) + (((8) * 2)) / 2 + 4 * (1) + 1)]; 258 const CSUsacConfig *pUsacConfig[(1 * 1)]; 259 INT nbDiv; /*!< number of frame divisions in LPD-domain */ 260 261 UCHAR useLdQmfTimeAlign; 262 263 INT aacChannelsPrev; /*!< The amount of AAC core channels of the last 264 successful decode call. */ 265 AUDIO_CHANNEL_TYPE channelTypePrev[(8)]; /*!< Array holding the channelType 266 values of the last successful 267 decode call. */ 268 UCHAR 269 channelIndicesPrev[(8)]; /*!< Array holding the channelIndices values of 270 the last successful decode call. */ 271 272 UCHAR 273 downscaleFactor; /*!< Variable to store a supported ELD downscale factor 274 of 1, 2, 3 or 4 */ 275 UCHAR downscaleFactorInBS; /*!< Variable to store the (not necessarily 276 supported) ELD downscale factor discovered in 277 the bitstream */ 278 279 HANDLE_SBRDECODER hSbrDecoder; /*!< SBR decoder handle. */ 280 UCHAR sbrEnabled; /*!< flag to store if SBR has been detected */ 281 UCHAR sbrEnabledPrev; /*!< flag to store if SBR has been detected from 282 previous frame */ 283 UCHAR psPossible; /*!< flag to store if PS is possible */ 284 SBR_PARAMS sbrParams; /*!< struct to store all sbr parameters */ 285 286 UCHAR *pDrmBsBuffer; /*!< Pointer to dynamic buffer which is used to reverse 287 the bits of the DRM SBR payload */ 288 USHORT drmBsBufferSize; /*!< Size of the dynamic buffer which is used to 289 reverse the bits of the DRM SBR payload */ 290 FDK_QMF_DOMAIN 291 qmfDomain; /*!< Instance of module for QMF domain data handling */ 292 293 QMF_MODE qmfModeCurr; /*!< The current QMF mode */ 294 QMF_MODE qmfModeUser; /*!< The QMF mode requested by the library user */ 295 296 HANDLE_AAC_DRC hDrcInfo; /*!< handle to DRC data structure */ 297 INT metadataExpiry; /*!< Metadata expiry time in milli-seconds. */ 298 299 void *pMpegSurroundDecoder; /*!< pointer to mpeg surround decoder structure */ 300 UCHAR mpsEnableUser; /*!< MPS enable user flag */ 301 UCHAR mpsEnableCurr; /*!< MPS enable decoder state */ 302 UCHAR mpsApplicable; /*!< MPS applicable */ 303 SCHAR mpsOutputMode; /*!< setting: normal = 0, binaural = 1, stereo = 2, 5.1ch 304 = 3 */ 305 INT mpsOutChannelsLast; /*!< The amount of channels returned by the last 306 successful MPS decoder call. */ 307 INT mpsFrameSizeLast; /*!< The frame length returned by the last successful 308 MPS decoder call. */ 309 310 CAncData ancData; /*!< structure to handle ancillary data */ 311 312 HANDLE_PCM_DOWNMIX hPcmUtils; /*!< privat data for the PCM utils. */ 313 314 TDLimiterPtr hLimiter; /*!< Handle of time domain limiter. */ 315 UCHAR limiterEnableUser; /*!< The limiter configuration requested by the 316 library user */ 317 UCHAR limiterEnableCurr; /*!< The current limiter configuration. */ 318 319 FIXP_DBL extGain[1]; /*!< Gain that must be applied to the output signal. */ 320 UINT extGainDelay; /*!< Delay that must be accounted for extGain. */ 321 322 HANDLE_DRC_DECODER hUniDrcDecoder; 323 UCHAR multibandDrcPresent; 324 UCHAR numTimeSlots; 325 UINT loudnessInfoSetPosition[3]; 326 SCHAR defaultTargetLoudness; 327 328 INT_PCM 329 *pTimeDataFlush[((8) * 2)]; /*!< Pointer to the flushed time data which 330 will be used for the crossfade in case of 331 an USAC DASH IPF config change */ 332 333 UCHAR flushStatus; /*!< Indicates flush status: on|off */ 334 SCHAR flushCnt; /*!< Flush frame counter */ 335 UCHAR buildUpStatus; /*!< Indicates build up status: on|off */ 336 SCHAR buildUpCnt; /*!< Build up frame counter */ 337 UCHAR hasAudioPreRoll; /*!< Indicates preRoll status: on|off */ 338 UINT prerollAULength[AACDEC_MAX_NUM_PREROLL_AU + 1]; /*!< Relative offset of 339 the prerollAU end 340 position to the AU 341 start position in the 342 bitstream */ 343 INT accessUnit; /*!< Number of the actual processed preroll accessUnit */ 344 UCHAR applyCrossfade; /*!< if set crossfade for seamless stream switching is 345 applied */ 346 347 FDK_SignalDelay usacResidualDelay; /*!< Delay residual signal to compensate 348 for eSBR delay of DMX signal in case of 349 stereoConfigIndex==2. */ 350 }; 351 352 #define AAC_DEBUG_EXTHLP \ 353 "\ 354 --- AAC-Core ---\n\ 355 0x00010000 Header data\n\ 356 0x00020000 CRC data\n\ 357 0x00040000 Channel info\n\ 358 0x00080000 Section data\n\ 359 0x00100000 Scalefactor data\n\ 360 0x00200000 Pulse data\n\ 361 0x00400000 Tns data\n\ 362 0x00800000 Quantized spectrum\n\ 363 0x01000000 Requantized spectrum\n\ 364 0x02000000 Time output\n\ 365 0x04000000 Fatal errors\n\ 366 0x08000000 Buffer fullness\n\ 367 0x10000000 Average bitrate\n\ 368 0x20000000 Synchronization\n\ 369 0x40000000 Concealment\n\ 370 0x7FFF0000 all AAC-Core-Info\n\ 371 " 372 373 /** 374 * \brief Synchronise QMF mode for all modules using QMF data. 375 * \param self decoder handle 376 */ 377 void CAacDecoder_SyncQmfMode(HANDLE_AACDECODER self); 378 379 /** 380 * \brief Signal a bit stream interruption to the decoder 381 * \param self decoder handle 382 */ 383 void CAacDecoder_SignalInterruption(HANDLE_AACDECODER self); 384 385 /*! 386 \brief Initialize ancillary buffer 387 388 \ancData Pointer to ancillary data structure 389 \buffer Pointer to (external) anc data buffer 390 \size Size of the buffer pointed on by buffer 391 392 \return Error code 393 */ 394 AAC_DECODER_ERROR CAacDecoder_AncDataInit(CAncData *ancData, 395 unsigned char *buffer, int size); 396 397 /*! 398 \brief Get one ancillary data element 399 400 \ancData Pointer to ancillary data structure 401 \index Index of the anc data element to get 402 \ptr Pointer to a buffer receiving a pointer to the requested anc data element 403 \size Pointer to a buffer receiving the length of the requested anc data 404 element 405 406 \return Error code 407 */ 408 AAC_DECODER_ERROR CAacDecoder_AncDataGet(CAncData *ancData, int index, 409 unsigned char **ptr, int *size); 410 411 /* initialization of aac decoder */ 412 LINKSPEC_H HANDLE_AACDECODER CAacDecoder_Open(TRANSPORT_TYPE bsFormat); 413 414 /* Initialization of channel elements */ 415 LINKSPEC_H AAC_DECODER_ERROR CAacDecoder_Init(HANDLE_AACDECODER self, 416 const CSAudioSpecificConfig *asc, 417 UCHAR configMode, 418 UCHAR *configChanged); 419 /*! 420 \brief Decodes one aac frame 421 422 The function decodes one aac frame. The decoding of coupling channel 423 elements are not supported. The transport layer might signal, that the 424 data of the current frame is invalid, e.g. as a result of a packet 425 loss in streaming mode. 426 The bitstream position of transportDec_GetBitstream(self->hInput) must 427 be exactly the end of the access unit, including all byte alignment bits. 428 For this purpose, the variable auStartAnchor is used. 429 430 \return error status 431 */ 432 LINKSPEC_H AAC_DECODER_ERROR CAacDecoder_DecodeFrame( 433 HANDLE_AACDECODER self, const UINT flags, PCM_DEC *pTimeData, 434 const INT timeDataSize, const int timeDataChannelOffset); 435 436 /* Free config dependent AAC memory */ 437 LINKSPEC_H AAC_DECODER_ERROR CAacDecoder_FreeMem(HANDLE_AACDECODER self, 438 const int subStreamIndex); 439 440 /* Prepare crossfade for USAC DASH IPF config change */ 441 LINKSPEC_H AAC_DECODER_ERROR CAacDecoder_PrepareCrossFade( 442 const INT_PCM *pTimeData, INT_PCM **pTimeDataFlush, const INT numChannels, 443 const INT frameSize, const INT interleaved); 444 445 /* Apply crossfade for USAC DASH IPF config change */ 446 LINKSPEC_H AAC_DECODER_ERROR CAacDecoder_ApplyCrossFade( 447 INT_PCM *pTimeData, INT_PCM **pTimeDataFlush, const INT numChannels, 448 const INT frameSize, const INT interleaved); 449 450 /* Set flush and build up mode */ 451 LINKSPEC_H AAC_DECODER_ERROR CAacDecoder_CtrlCFGChange(HANDLE_AACDECODER self, 452 UCHAR flushStatus, 453 SCHAR flushCnt, 454 UCHAR buildUpStatus, 455 SCHAR buildUpCnt); 456 457 /* Parse preRoll Extension Payload */ 458 LINKSPEC_H AAC_DECODER_ERROR CAacDecoder_PreRollExtensionPayloadParse( 459 HANDLE_AACDECODER self, UINT *numPrerollAU, UINT *prerollAUOffset, 460 UINT *prerollAULength); 461 462 /* Destroy aac decoder */ 463 LINKSPEC_H void CAacDecoder_Close(HANDLE_AACDECODER self); 464 465 /* get streaminfo handle from decoder */ 466 LINKSPEC_H CStreamInfo *CAacDecoder_GetStreamInfo(HANDLE_AACDECODER self); 467 468 #endif /* #ifndef AACDECODER_H */ 469