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