Home | History | Annotate | Download | only in src
      1 
      2 /* -----------------------------------------------------------------------------------------------------------
      3 Software License for The Fraunhofer FDK AAC Codec Library for Android
      4 
      5  Copyright  1995 - 2012 Fraunhofer-Gesellschaft zur Frderung der angewandten Forschung e.V.
      6   All rights reserved.
      7 
      8  1.    INTRODUCTION
      9 The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software that implements
     10 the MPEG Advanced Audio Coding ("AAC") encoding and decoding scheme for digital audio.
     11 This FDK AAC Codec software is intended to be used on a wide variety of Android devices.
     12 
     13 AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient general perceptual
     14 audio codecs. AAC-ELD is considered the best-performing full-bandwidth communications codec by
     15 independent studies and is widely deployed. AAC has been standardized by ISO and IEC as part
     16 of the MPEG specifications.
     17 
     18 Patent licenses for necessary patent claims for the FDK AAC Codec (including those of Fraunhofer)
     19 may be obtained through Via Licensing (www.vialicensing.com) or through the respective patent owners
     20 individually for the purpose of encoding or decoding bit streams in products that are compliant with
     21 the ISO/IEC MPEG audio standards. Please note that most manufacturers of Android devices already license
     22 these patent claims through Via Licensing or directly from the patent owners, and therefore FDK AAC Codec
     23 software may already be covered under those patent licenses when it is used for those licensed purposes only.
     24 
     25 Commercially-licensed AAC software libraries, including floating-point versions with enhanced sound quality,
     26 are also available from Fraunhofer. Users are encouraged to check the Fraunhofer website for additional
     27 applications information and documentation.
     28 
     29 2.    COPYRIGHT LICENSE
     30 
     31 Redistribution and use in source and binary forms, with or without modification, are permitted without
     32 payment of copyright license fees provided that you satisfy the following conditions:
     33 
     34 You must retain the complete text of this software license in redistributions of the FDK AAC Codec or
     35 your modifications thereto in source code form.
     36 
     37 You must retain the complete text of this software license in the documentation and/or other materials
     38 provided with redistributions of the FDK AAC Codec or your modifications thereto in binary form.
     39 You must make available free of charge copies of the complete source code of the FDK AAC Codec and your
     40 modifications thereto to recipients of copies in binary form.
     41 
     42 The name of Fraunhofer may not be used to endorse or promote products derived from this library without
     43 prior written permission.
     44 
     45 You may not charge copyright license fees for anyone to use, copy or distribute the FDK AAC Codec
     46 software or your modifications thereto.
     47 
     48 Your modified versions of the FDK AAC Codec must carry prominent notices stating that you changed the software
     49 and the date of any change. For modified versions of the FDK AAC Codec, the term
     50 "Fraunhofer FDK AAC Codec Library for Android" must be replaced by the term
     51 "Third-Party Modified Version of the Fraunhofer FDK AAC Codec Library for Android."
     52 
     53 3.    NO PATENT LICENSE
     54 
     55 NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without limitation the patents of Fraunhofer,
     56 ARE GRANTED BY THIS SOFTWARE LICENSE. Fraunhofer provides no warranty of patent non-infringement with
     57 respect to this software.
     58 
     59 You may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized
     60 by appropriate patent licenses.
     61 
     62 4.    DISCLAIMER
     63 
     64 This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright holders and contributors
     65 "AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, including but not limited to the implied warranties
     66 of merchantability and fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
     67 CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary, or consequential damages,
     68 including but not limited to procurement of substitute goods or services; loss of use, data, or profits,
     69 or business interruption, however caused and on any theory of liability, whether in contract, strict
     70 liability, or tort (including negligence), arising in any way out of the use of this software, even if
     71 advised of the possibility of such damage.
     72 
     73 5.    CONTACT INFORMATION
     74 
     75 Fraunhofer Institute for Integrated Circuits IIS
     76 Attention: Audio and Multimedia Departments - FDK AAC LL
     77 Am Wolfsmantel 33
     78 91058 Erlangen, Germany
     79 
     80 www.iis.fraunhofer.de/amm
     81 amm-info (at) iis.fraunhofer.de
     82 ----------------------------------------------------------------------------------------------------------- */
     83 
     84 /*****************************  MPEG-4 AAC Decoder  **************************
     85 
     86    Author(s):   Manuel Jander
     87    Description:
     88 
     89 ******************************************************************************/
     90 
     91 #include "aacdecoder_lib.h"
     92 
     93 #include "aac_ram.h"
     94 #include "aacdecoder.h"
     95 #include "tpdec_lib.h"
     96 #include "FDK_core.h" /* FDK_tools version info */
     97 
     98 
     99  #include "sbrdecoder.h"
    100 
    101 
    102 
    103 
    104 #include "conceal.h"
    105 
    106  #include "aacdec_drc.h"
    107 
    108 
    109 
    110 /* Decoder library info */
    111 #define AACDECODER_LIB_VL0 2
    112 #define AACDECODER_LIB_VL1 4
    113 #define AACDECODER_LIB_VL2 7
    114 #define AACDECODER_LIB_TITLE "AAC Decoder Lib"
    115 #define AACDECODER_LIB_BUILD_DATE __DATE__
    116 #define AACDECODER_LIB_BUILD_TIME __TIME__
    117 
    118 static AAC_DECODER_ERROR
    119 setConcealMethod ( const HANDLE_AACDECODER  self,
    120                    const INT                method );
    121 
    122 
    123 LINKSPEC_CPP AAC_DECODER_ERROR aacDecoder_GetFreeBytes ( const HANDLE_AACDECODER  self, UINT *pFreeBytes){
    124 
    125   /* reset free bytes */
    126   *pFreeBytes = 0;
    127 
    128   /* check handle */
    129   if(!self)
    130     return AAC_DEC_INVALID_HANDLE;
    131 
    132   /* return nr of free bytes */
    133   HANDLE_FDK_BITSTREAM hBs = transportDec_GetBitstream(self->hInput, 0);
    134   *pFreeBytes = FDKgetFreeBits(hBs) >> 3;
    135 
    136   /* success */
    137   return AAC_DEC_OK;
    138 }
    139 
    140 /**
    141  * Config Decoder using a CSAudioSpecificConfig struct.
    142  */
    143 static
    144 LINKSPEC_CPP AAC_DECODER_ERROR aacDecoder_Config(HANDLE_AACDECODER self, const CSAudioSpecificConfig *pAscStruct)
    145 {
    146   AAC_DECODER_ERROR err;
    147 
    148   /* Initialize AAC core decoder, and update self->streaminfo */
    149   err = CAacDecoder_Init(self, pAscStruct);
    150 
    151   return err;
    152 }
    153 
    154 LINKSPEC_CPP AAC_DECODER_ERROR aacDecoder_ConfigRaw (
    155         HANDLE_AACDECODER self,
    156         UCHAR *conf[],
    157         const UINT length[] )
    158 {
    159   AAC_DECODER_ERROR err = AAC_DEC_OK;
    160   TRANSPORTDEC_ERROR   errTp;
    161   UINT layer, nrOfLayers = self->nrOfLayers;
    162 
    163   for(layer = 0; layer < nrOfLayers; layer++){
    164     if(length[layer] > 0){
    165       errTp = transportDec_OutOfBandConfig(self->hInput, conf[layer], length[layer], layer);
    166       if (errTp != TRANSPORTDEC_OK) {
    167         switch (errTp) {
    168         case TRANSPORTDEC_NEED_TO_RESTART:
    169           err = AAC_DEC_NEED_TO_RESTART;
    170           break;
    171         case TRANSPORTDEC_UNSUPPORTED_FORMAT:
    172           err = AAC_DEC_UNSUPPORTED_FORMAT;
    173           break;
    174         default:
    175           err = AAC_DEC_UNKNOWN;
    176           break;
    177         }
    178         /* if baselayer is OK we continue decoding */
    179         if(layer  >= 1){
    180           self->nrOfLayers = layer;
    181         }
    182         break;
    183       }
    184     }
    185   }
    186 
    187   return err;
    188 }
    189 
    190 
    191 
    192 static INT aacDecoder_ConfigCallback(void *handle, const CSAudioSpecificConfig *pAscStruct)
    193 {
    194   HANDLE_AACDECODER self = (HANDLE_AACDECODER)handle;
    195   AAC_DECODER_ERROR err = AAC_DEC_OK;
    196   TRANSPORTDEC_ERROR errTp;
    197 
    198   {
    199     {
    200       err = aacDecoder_Config(self, pAscStruct);
    201     }
    202   }
    203   if (err == AAC_DEC_OK) {
    204     if ( self->flags & (AC_USAC|AC_RSVD50|AC_LD|AC_ELD)
    205       && CConcealment_GetDelay(&self->concealCommonData) > 0 )
    206     {
    207       /* Revert to error concealment method Noise Substitution.
    208          Because interpolation is not implemented for USAC/RSVD50 or
    209          the additional delay is unwanted for low delay codecs. */
    210       setConcealMethod(self, 1);
    211 #ifdef DEBUG
    212       FDKprintf("  Concealment method was reverted to 1 !\n");
    213 #endif
    214     }
    215     errTp = TRANSPORTDEC_OK;
    216   } else {
    217     if (IS_INIT_ERROR(err)) {
    218       errTp = TRANSPORTDEC_UNSUPPORTED_FORMAT;
    219     } /* Fatal errors */
    220     else if (err == AAC_DEC_NEED_TO_RESTART) {
    221       errTp = TRANSPORTDEC_NEED_TO_RESTART;
    222     } else {
    223       errTp = TRANSPORTDEC_UNKOWN_ERROR;
    224     }
    225   }
    226 
    227   return errTp;
    228 }
    229 
    230 
    231 
    232 LINKSPEC_CPP AAC_DECODER_ERROR
    233 aacDecoder_AncDataInit ( HANDLE_AACDECODER self,
    234                          UCHAR *buffer,
    235                          int size )
    236 {
    237   CAncData *ancData = &self->ancData;
    238 
    239   return CAacDecoder_AncDataInit(ancData, buffer, size);
    240 }
    241 
    242 
    243 LINKSPEC_CPP AAC_DECODER_ERROR
    244 aacDecoder_AncDataGet ( HANDLE_AACDECODER self,
    245                         int     index,
    246                         UCHAR **ptr,
    247                         int    *size )
    248 {
    249   CAncData *ancData = &self->ancData;
    250 
    251   return CAacDecoder_AncDataGet(ancData, index, ptr, size);
    252 }
    253 
    254 
    255 static AAC_DECODER_ERROR
    256 setConcealMethod ( const HANDLE_AACDECODER  self,   /*!< Handle of the decoder instance */
    257                    const INT                method )
    258 {
    259   AAC_DECODER_ERROR errorStatus = AAC_DEC_OK;
    260   CConcealParams  *pConcealData = NULL;
    261   HANDLE_SBRDECODER hSbrDec = NULL;
    262   HANDLE_AAC_DRC hDrcInfo = NULL;
    263   HANDLE_PCM_DOWNMIX hPcmDmx = NULL;
    264   CConcealmentMethod backupMethod;
    265   int backupDelay = 0;
    266   int bsDelay = 0;
    267 
    268   /* check decoder handle */
    269   if (self != NULL) {
    270     pConcealData = &self->concealCommonData;
    271     hSbrDec = self->hSbrDecoder;
    272     hDrcInfo = self->hDrcInfo;
    273     hPcmDmx = self->hPcmUtils;
    274   }
    275 
    276 
    277   /* Get current method/delay */
    278   backupMethod = CConcealment_GetMethod(pConcealData);
    279   backupDelay  = CConcealment_GetDelay(pConcealData);
    280 
    281   /* Be sure to set AAC and SBR concealment method simultaneously! */
    282   errorStatus =
    283     CConcealment_SetParams(
    284       pConcealData,
    285       (int)method,                         // concealMethod
    286       AACDEC_CONCEAL_PARAM_NOT_SPECIFIED,  // concealFadeOutSlope
    287       AACDEC_CONCEAL_PARAM_NOT_SPECIFIED,  // concealFadeInSlope
    288       AACDEC_CONCEAL_PARAM_NOT_SPECIFIED,  // concealMuteRelease
    289       AACDEC_CONCEAL_PARAM_NOT_SPECIFIED   // concealComfNoiseLevel
    290     );
    291   if ( (errorStatus != AAC_DEC_OK)
    292     && (errorStatus != AAC_DEC_INVALID_HANDLE) ) {
    293     goto bail;
    294   }
    295 
    296   /* Get new delay */
    297   bsDelay = CConcealment_GetDelay(pConcealData);
    298 
    299   {
    300     SBR_ERROR sbrErr = SBRDEC_OK;
    301 
    302     /* set SBR bitstream delay */
    303     sbrErr = sbrDecoder_SetParam (
    304       hSbrDec,
    305       SBR_SYSTEM_BITSTREAM_DELAY,
    306       bsDelay
    307     );
    308 
    309     switch (sbrErr) {
    310     case SBRDEC_OK:
    311     case SBRDEC_NOT_INITIALIZED:
    312       if (self != NULL) {
    313         /* save the param value and set later
    314            (when SBR has been initialized) */
    315         self->sbrParams.bsDelay = bsDelay;
    316       }
    317       break;
    318     default:
    319       errorStatus = AAC_DEC_SET_PARAM_FAIL;
    320       goto bail;
    321     }
    322   }
    323 
    324   errorStatus =
    325     aacDecoder_drcSetParam (
    326       hDrcInfo,
    327       DRC_BS_DELAY,
    328       bsDelay
    329     );
    330   if ( (errorStatus != AAC_DEC_OK)
    331     && (errorStatus != AAC_DEC_INVALID_HANDLE) ) {
    332     goto bail;
    333   }
    334 
    335   if (errorStatus == AAC_DEC_OK) {
    336     PCMDMX_ERROR err =
    337       pcmDmx_SetParam (
    338         hPcmDmx,
    339         DMX_BS_DATA_DELAY,
    340         bsDelay
    341       );
    342     switch (err) {
    343     case PCMDMX_INVALID_HANDLE:
    344       errorStatus = AAC_DEC_INVALID_HANDLE;
    345     case PCMDMX_OK:
    346       break;
    347     default:
    348       errorStatus = AAC_DEC_SET_PARAM_FAIL;
    349       goto bail;
    350     }
    351   }
    352 
    353 
    354 bail:
    355   if ( (errorStatus != AAC_DEC_OK)
    356     && (errorStatus != AAC_DEC_INVALID_HANDLE) )
    357   {
    358     /* Revert to the initial state */
    359     CConcealment_SetParams (
    360         pConcealData,
    361         (int)backupMethod,
    362         AACDEC_CONCEAL_PARAM_NOT_SPECIFIED,
    363         AACDEC_CONCEAL_PARAM_NOT_SPECIFIED,
    364         AACDEC_CONCEAL_PARAM_NOT_SPECIFIED,
    365         AACDEC_CONCEAL_PARAM_NOT_SPECIFIED
    366       );
    367     /* Revert SBR bitstream delay */
    368     sbrDecoder_SetParam (
    369         hSbrDec,
    370         SBR_SYSTEM_BITSTREAM_DELAY,
    371         backupDelay
    372       );
    373     /* Revert DRC bitstream delay */
    374     aacDecoder_drcSetParam (
    375         hDrcInfo,
    376         DRC_BS_DELAY,
    377         backupDelay
    378       );
    379     /* Revert PCM mixdown bitstream delay */
    380     pcmDmx_SetParam (
    381         hPcmDmx,
    382         DMX_BS_DATA_DELAY,
    383         backupDelay
    384       );
    385   }
    386 
    387   return errorStatus;
    388 }
    389 
    390 
    391 LINKSPEC_CPP AAC_DECODER_ERROR
    392 aacDecoder_SetParam ( const HANDLE_AACDECODER  self,   /*!< Handle of the decoder instance */
    393                       const AACDEC_PARAM       param,  /*!< Parameter to set               */
    394                       const INT                value)  /*!< Parameter valued               */
    395 {
    396   AAC_DECODER_ERROR errorStatus = AAC_DEC_OK;
    397   CConcealParams  *pConcealData = NULL;
    398   HANDLE_AAC_DRC hDrcInfo = NULL;
    399 
    400   /* check decoder handle */
    401   if (self != NULL) {
    402     pConcealData = &self->concealCommonData;
    403     hDrcInfo = self->hDrcInfo;
    404   }
    405 
    406   /* configure the subsystems */
    407   switch (param)
    408   {
    409   case AAC_PCM_OUTPUT_INTERLEAVED:
    410     if (value < 0 || value > 1) {
    411       return AAC_DEC_SET_PARAM_FAIL;
    412     }
    413     if (self == NULL) {
    414       return AAC_DEC_INVALID_HANDLE;
    415     }
    416     self->outputInterleaved = value;
    417     break;
    418 
    419   case AAC_PCM_OUTPUT_CHANNELS:
    420     {
    421       PCMDMX_ERROR err;
    422 
    423       err = pcmDmx_SetParam (
    424               self->hPcmUtils,
    425               NUMBER_OF_OUTPUT_CHANNELS,
    426               value );
    427 
    428       switch (err) {
    429       case PCMDMX_OK:
    430         break;
    431       case PCMDMX_INVALID_HANDLE:
    432         return AAC_DEC_INVALID_HANDLE;
    433       default:
    434         return AAC_DEC_SET_PARAM_FAIL;
    435       }
    436     }
    437     break;
    438 
    439   case AAC_PCM_DUAL_CHANNEL_OUTPUT_MODE:
    440     {
    441       PCMDMX_ERROR err;
    442 
    443       err = pcmDmx_SetParam (
    444               self->hPcmUtils,
    445               DUAL_CHANNEL_DOWNMIX_MODE,
    446               value );
    447 
    448       switch (err) {
    449       case PCMDMX_OK:
    450         break;
    451       case PCMDMX_INVALID_HANDLE:
    452         return AAC_DEC_INVALID_HANDLE;
    453       default:
    454         return AAC_DEC_SET_PARAM_FAIL;
    455       }
    456     }
    457     break;
    458 
    459   case AAC_PCM_OUTPUT_CHANNEL_MAPPING:
    460     switch (value) {
    461       case 0:
    462         self->channelOutputMapping = channelMappingTablePassthrough;
    463         break;
    464       case 1:
    465         self->channelOutputMapping = channelMappingTableWAV;
    466         break;
    467       default:
    468         errorStatus = AAC_DEC_SET_PARAM_FAIL;
    469         break;
    470     }
    471     break;
    472 
    473 
    474   case AAC_QMF_LOWPOWER:
    475     if (self == NULL) {
    476       return AAC_DEC_INVALID_HANDLE;
    477     }
    478 
    479     /**
    480      * Set QMF mode (might be overriden)
    481      *  0:HQ (complex)
    482      *  1:LP (partially complex)
    483      */
    484     self->qmfModeUser = (QMF_MODE)value;
    485     break;
    486 
    487 
    488   case AAC_DRC_ATTENUATION_FACTOR:
    489     /* DRC compression factor (where 0 is no and 127 is max compression) */
    490     errorStatus =
    491       aacDecoder_drcSetParam (
    492         hDrcInfo,
    493         DRC_CUT_SCALE,
    494         value
    495       );
    496     break;
    497 
    498   case AAC_DRC_BOOST_FACTOR:
    499     /* DRC boost factor (where 0 is no and 127 is max boost) */
    500     errorStatus =
    501       aacDecoder_drcSetParam (
    502         hDrcInfo,
    503         DRC_BOOST_SCALE,
    504         value
    505       );
    506     break;
    507 
    508   case AAC_DRC_REFERENCE_LEVEL:
    509     /* DRC reference level quantized in 0.25dB steps using values [0..127] it is '-' for analog scaling */
    510     errorStatus =
    511       aacDecoder_drcSetParam (
    512         hDrcInfo,
    513         TARGET_REF_LEVEL,
    514         value
    515       );
    516     break;
    517 
    518   case AAC_DRC_HEAVY_COMPRESSION:
    519     /* Don't need to overwrite cut/boost values */
    520     errorStatus =
    521       aacDecoder_drcSetParam (
    522         hDrcInfo,
    523         APPLY_HEAVY_COMPRESSION,
    524         value
    525       );
    526     break;
    527 
    528 
    529   case AAC_TPDEC_CLEAR_BUFFER:
    530     transportDec_SetParam(self->hInput, TPDEC_PARAM_RESET, 1);
    531     self->streamInfo.numLostAccessUnits = 0;
    532     self->streamInfo.numBadBytes = 0;
    533     self->streamInfo.numTotalBytes = 0;
    534     /* aacDecoder_SignalInterruption(self); */
    535     break;
    536 
    537   case AAC_CONCEAL_METHOD:
    538     /* Changing the concealment method can introduce additional bitstream delay. And
    539        that in turn affects sub libraries and modules which makes the whole thing quite
    540        complex.  So the complete changing routine is packed into a helper function which
    541        keeps all modules and libs in a consistent state even in the case an error occures. */
    542     errorStatus = setConcealMethod ( self, value );
    543     break;
    544 
    545   default:
    546     return AAC_DEC_SET_PARAM_FAIL;
    547   }  /* switch(param) */
    548 
    549   return (errorStatus);
    550 }
    551 
    552 
    553 LINKSPEC_CPP HANDLE_AACDECODER aacDecoder_Open(TRANSPORT_TYPE transportFmt, UINT nrOfLayers)
    554 {
    555   AAC_DECODER_INSTANCE *aacDec = NULL;
    556   HANDLE_TRANSPORTDEC pIn;
    557   int err = 0;
    558 
    559   /* Allocate transport layer struct. */
    560   pIn = transportDec_Open(transportFmt, TP_FLAG_MPEG4);
    561   if (pIn == NULL) {
    562     return NULL;
    563   }
    564 
    565   transportDec_SetParam(pIn, TPDEC_PARAM_IGNORE_BUFFERFULLNESS, 1);
    566 
    567   /* Allocate AAC decoder core struct. */
    568   aacDec = CAacDecoder_Open(transportFmt);
    569 
    570   if (aacDec == NULL) {
    571     transportDec_Close(&pIn);
    572     goto bail;
    573   }
    574   aacDec->hInput = pIn;
    575 
    576   aacDec->nrOfLayers = nrOfLayers;
    577 
    578   aacDec->channelOutputMapping = channelMappingTableWAV;
    579 
    580   /* Register Config Update callback. */
    581   transportDec_RegisterAscCallback(pIn, aacDecoder_ConfigCallback, (void*)aacDec);
    582 
    583   /* open SBR decoder */
    584   if ( SBRDEC_OK != sbrDecoder_Open ( &aacDec->hSbrDecoder )) {
    585     err = -1;
    586     goto bail;
    587   }
    588   aacDec->qmfModeUser = NOT_DEFINED;
    589   transportDec_RegisterSbrCallback(aacDec->hInput, (cbSbr_t)sbrDecoder_Header, (void*)aacDec->hSbrDecoder);
    590 
    591 
    592   pcmDmx_Open( &aacDec->hPcmUtils );
    593   if (aacDec->hPcmUtils == NULL) {
    594     err = -1;
    595     goto bail;
    596   }
    597 
    598 
    599 
    600   /* Assure that all modules have same delay */
    601   if ( setConcealMethod(aacDec, CConcealment_GetMethod(&aacDec->concealCommonData)) ) {
    602     err = -1;
    603     goto bail;
    604   }
    605 
    606 bail:
    607   if (err == -1) {
    608     aacDecoder_Close(aacDec);
    609     aacDec = NULL;
    610   }
    611   return aacDec;
    612 }
    613 
    614 LINKSPEC_CPP AAC_DECODER_ERROR aacDecoder_Fill(
    615         HANDLE_AACDECODER   self,
    616         UCHAR              *pBuffer[],
    617         const UINT          bufferSize[],
    618         UINT               *pBytesValid
    619         )
    620 {
    621   TRANSPORTDEC_ERROR tpErr;
    622   /* loop counter for layers; if not TT_MP4_RAWPACKETS used as index for only
    623      available layer                                                           */
    624   INT layer      = 0;
    625   INT nrOfLayers = self->nrOfLayers;
    626 
    627   {
    628     for (layer = 0; layer < nrOfLayers; layer++){
    629       {
    630         tpErr = transportDec_FillData( self->hInput, pBuffer[layer], bufferSize[layer], &pBytesValid[layer], layer );
    631         if (tpErr != TRANSPORTDEC_OK) {
    632           return AAC_DEC_UNKNOWN;  /* Must be an internal error */
    633         }
    634       }
    635     }
    636   }
    637 
    638   return AAC_DEC_OK;
    639 }
    640 
    641 
    642 static void aacDecoder_SignalInterruption(HANDLE_AACDECODER self)
    643 {
    644   CAacDecoder_SignalInterruption(self);
    645 
    646   if ( self->hSbrDecoder != NULL ) {
    647     sbrDecoder_SetParam(self->hSbrDecoder, SBR_BS_INTERRUPTION, 0);
    648   }
    649 }
    650 
    651 static void aacDecoder_UpdateBitStreamCounters(CStreamInfo *pSi, HANDLE_FDK_BITSTREAM hBs, int nBits, AAC_DECODER_ERROR ErrorStatus)
    652 {
    653   /* calculate bit difference (amount of bits moved forward) */
    654   nBits = nBits - FDKgetValidBits(hBs);
    655 
    656   /* Note: The amount of bits consumed might become negative when parsing a
    657      bit stream with several sub frames, and we find out at the last sub frame
    658      that the total frame length does not match the sum of sub frame length.
    659      If this happens, the transport decoder might want to rewind to the supposed
    660      ending of the transport frame, and this position might be before the last
    661      access unit beginning. */
    662 
    663   /* Calc bitrate. */
    664   if (pSi->frameSize > 0) {
    665     pSi->bitRate = (nBits * pSi->sampleRate)/pSi->frameSize;
    666   }
    667 
    668   /* bit/byte counters */
    669   {
    670     int nBytes;
    671 
    672     nBytes = nBits>>3;
    673     pSi->numTotalBytes += nBytes;
    674     if (IS_OUTPUT_VALID(ErrorStatus)) {
    675       pSi->numTotalAccessUnits++;
    676     }
    677     if (IS_DECODE_ERROR(ErrorStatus)) {
    678       pSi->numBadBytes += nBytes;
    679       pSi->numBadAccessUnits++;
    680     }
    681   }
    682 }
    683 
    684 static INT aacDecoder_EstimateNumberOfLostFrames(HANDLE_AACDECODER self)
    685 {
    686   INT n;
    687 
    688   transportDec_GetMissingAccessUnitCount( &n, self->hInput);
    689 
    690   return n;
    691 }
    692 
    693 LINKSPEC_CPP AAC_DECODER_ERROR aacDecoder_DecodeFrame(
    694         HANDLE_AACDECODER  self,
    695         INT_PCM           *pTimeData,
    696         const INT          timeDataSize,
    697         const UINT         flags)
    698 {
    699     AAC_DECODER_ERROR ErrorStatus;
    700     INT layer;
    701     INT nBits;
    702     INT interleaved = self->outputInterleaved;
    703     HANDLE_FDK_BITSTREAM hBs;
    704     int fTpInterruption = 0;  /* Transport originated interruption detection. */
    705     int fTpConceal = 0;       /* Transport originated concealment. */
    706 
    707 
    708     if (self == NULL) {
    709       return AAC_DEC_INVALID_HANDLE;
    710     }
    711 
    712     if (flags & AACDEC_INTR) {
    713       self->streamInfo.numLostAccessUnits = 0;
    714     }
    715 
    716     hBs = transportDec_GetBitstream(self->hInput, 0);
    717 
    718     /* Get current bits position for bitrate calculation. */
    719     nBits = FDKgetValidBits(hBs);
    720     if (! (flags & (AACDEC_CONCEAL | AACDEC_FLUSH) ) )
    721     {
    722       TRANSPORTDEC_ERROR err;
    723 
    724       for(layer = 0; layer < self->nrOfLayers; layer++)
    725       {
    726         err = transportDec_ReadAccessUnit(self->hInput, layer);
    727         if (err != TRANSPORTDEC_OK) {
    728           switch (err) {
    729           case TRANSPORTDEC_NOT_ENOUGH_BITS:
    730             ErrorStatus = AAC_DEC_NOT_ENOUGH_BITS;
    731             goto bail;
    732           case TRANSPORTDEC_SYNC_ERROR:
    733             self->streamInfo.numLostAccessUnits = aacDecoder_EstimateNumberOfLostFrames(self);
    734             fTpInterruption = 1;
    735             break;
    736           case TRANSPORTDEC_NEED_TO_RESTART:
    737             ErrorStatus = AAC_DEC_NEED_TO_RESTART;
    738             goto bail;
    739           case TRANSPORTDEC_CRC_ERROR:
    740             fTpConceal = 1;
    741             break;
    742           default:
    743             ErrorStatus = AAC_DEC_UNKNOWN;
    744             goto bail;
    745           }
    746         }
    747       }
    748     } else {
    749       if (self->streamInfo.numLostAccessUnits > 0) {
    750         self->streamInfo.numLostAccessUnits--;
    751       }
    752     }
    753 
    754     /* Signal bit stream interruption to other modules if required. */
    755     if ( fTpInterruption || (flags & (AACDEC_INTR|AACDEC_CLRHIST)) )
    756     {
    757       aacDecoder_SignalInterruption(self);
    758       if ( ! (flags & AACDEC_INTR) ) {
    759         ErrorStatus = AAC_DEC_TRANSPORT_SYNC_ERROR;
    760         goto bail;
    761       }
    762     }
    763 
    764     /* Empty bit buffer in case of flush request. */
    765     if (flags & AACDEC_FLUSH)
    766     {
    767       transportDec_SetParam(self->hInput, TPDEC_PARAM_RESET, 1);
    768       self->streamInfo.numLostAccessUnits = 0;
    769       self->streamInfo.numBadBytes = 0;
    770       self->streamInfo.numTotalBytes = 0;
    771     }
    772 
    773 
    774     ErrorStatus = CAacDecoder_DecodeFrame(self,
    775                                           flags | (fTpConceal ? AACDEC_CONCEAL : 0),
    776                                           pTimeData,
    777                                           timeDataSize,
    778                                           interleaved);
    779 
    780     if (!(flags & (AACDEC_CONCEAL|AACDEC_FLUSH))) {
    781       TRANSPORTDEC_ERROR tpErr;
    782       tpErr = transportDec_EndAccessUnit(self->hInput);
    783       if (tpErr != TRANSPORTDEC_OK) {
    784         self->frameOK = 0;
    785       }
    786     }
    787 
    788     /* If the current pTimeData does not contain a valid signal, there nothing else we can do, so bail. */
    789     if ( ! IS_OUTPUT_VALID(ErrorStatus) ) {
    790       goto bail;
    791     }
    792 
    793     {
    794       /* Export data into streaminfo structure */
    795       self->streamInfo.sampleRate = self->streamInfo.aacSampleRate;
    796       self->streamInfo.frameSize  = self->streamInfo.aacSamplesPerFrame;
    797       self->streamInfo.numChannels = self->aacChannels;
    798     }
    799 
    800 
    801 
    802     CAacDecoder_SyncQmfMode(self);
    803 
    804 /* sbr decoder */
    805 
    806     if (ErrorStatus || (flags & AACDEC_CONCEAL) || self->pAacDecoderStaticChannelInfo[0]->concealmentInfo.concealState > ConcealState_FadeIn)
    807     {
    808       self->frameOK = 0;  /* if an error has occured do concealment in the SBR decoder too */
    809     }
    810 
    811     if (self->sbrEnabled)
    812     {
    813       SBR_ERROR sbrError = SBRDEC_OK;
    814 
    815       /* set params */
    816       sbrDecoder_SetParam ( self->hSbrDecoder,
    817                             SBR_SYSTEM_BITSTREAM_DELAY,
    818                             self->sbrParams.bsDelay);
    819 
    820       if ( self->streamInfo.aot == AOT_ER_AAC_ELD ) {
    821         /* Configure QMF */
    822         sbrDecoder_SetParam ( self->hSbrDecoder,
    823                               SBR_LD_QMF_TIME_ALIGN,
    824                               (self->flags & AC_LD_MPS) ? 1 : 0 );
    825       }
    826 
    827 
    828 
    829 
    830       /* apply SBR processing */
    831       sbrError = sbrDecoder_Apply ( self->hSbrDecoder,
    832                                     pTimeData,
    833                                    &self->streamInfo.numChannels,
    834                                    &self->streamInfo.sampleRate,
    835                                     self->channelOutputMapping[self->aacChannels-1],
    836                                     interleaved,
    837                                     self->frameOK,
    838                                    &self->psPossible);
    839 
    840 
    841      if (sbrError == SBRDEC_OK) {
    842 
    843        /* Update data in streaminfo structure. Assume that the SBR upsampling factor is either 1 or 2 */
    844        self->flags |= AC_SBR_PRESENT;
    845        if (self->streamInfo.aacSampleRate != self->streamInfo.sampleRate) {
    846          if (self->streamInfo.frameSize == 768) {
    847            self->streamInfo.frameSize =  (self->streamInfo.aacSamplesPerFrame * 8) / 3;
    848          } else {
    849            self->streamInfo.frameSize =  self->streamInfo.aacSamplesPerFrame << 1;
    850          }
    851        }
    852 
    853        if (self->psPossible) {
    854          self->flags |= AC_PS_PRESENT;
    855          self->channelType[0] = ACT_FRONT;
    856          self->channelType[1] = ACT_FRONT;
    857          self->channelIndices[0] = 0;
    858          self->channelIndices[1] = 1;
    859        } else {
    860          self->flags &= ~AC_PS_PRESENT;
    861        }
    862      }
    863    }
    864 
    865 
    866     if ( flags & (AACDEC_INTR | AACDEC_CLRHIST) ) {
    867       /* delete data from the past (e.g. mixdown coeficients) */
    868       pcmDmx_Reset( self->hPcmUtils, PCMDMX_RESET_BS_DATA );
    869     }
    870     /* do PCM post processing */
    871     pcmDmx_ApplyFrame (
    872             self->hPcmUtils,
    873             pTimeData,
    874             self->streamInfo.frameSize,
    875            &self->streamInfo.numChannels,
    876             interleaved,
    877             self->channelType,
    878             self->channelIndices,
    879             self->channelOutputMapping
    880       );
    881 
    882 
    883 
    884     /* Signal interruption to take effect in next frame. */
    885     if ( flags & AACDEC_FLUSH ) {
    886       aacDecoder_SignalInterruption(self);
    887     }
    888 
    889     /* Update externally visible copy of flags */
    890     self->streamInfo.flags = self->flags;
    891 
    892 bail:
    893 
    894     /* Update Statistics */
    895     aacDecoder_UpdateBitStreamCounters(&self->streamInfo, hBs, nBits, ErrorStatus);
    896 
    897     return ErrorStatus;
    898 }
    899 
    900 LINKSPEC_CPP void aacDecoder_Close ( HANDLE_AACDECODER self )
    901 {
    902   if (self == NULL)
    903     return;
    904 
    905 
    906 
    907   if (self->hPcmUtils != NULL) {
    908     pcmDmx_Close( &self->hPcmUtils );
    909   }
    910 
    911 
    912 
    913   if (self->hSbrDecoder != NULL) {
    914     sbrDecoder_Close(&self->hSbrDecoder);
    915   }
    916 
    917   if (self->hInput != NULL) {
    918     transportDec_Close(&self->hInput);
    919   }
    920 
    921   CAacDecoder_Close(self);
    922 }
    923 
    924 
    925 LINKSPEC_CPP CStreamInfo* aacDecoder_GetStreamInfo ( HANDLE_AACDECODER self )
    926 {
    927   return CAacDecoder_GetStreamInfo(self);
    928 }
    929 
    930 LINKSPEC_CPP INT aacDecoder_GetLibInfo ( LIB_INFO *info )
    931 {
    932   int i;
    933 
    934   if (info == NULL) {
    935     return -1;
    936   }
    937 
    938   sbrDecoder_GetLibInfo( info );
    939   transportDec_GetLibInfo( info );
    940   FDK_toolsGetLibInfo( info );
    941   pcmDmx_GetLibInfo( info );
    942 
    943   /* search for next free tab */
    944   for (i = 0; i < FDK_MODULE_LAST; i++) {
    945     if (info[i].module_id == FDK_NONE) break;
    946   }
    947   if (i == FDK_MODULE_LAST) {
    948     return -1;
    949   }
    950   info += i;
    951 
    952   info->module_id = FDK_AACDEC;
    953   /* build own library info */
    954   info->version = LIB_VERSION(AACDECODER_LIB_VL0, AACDECODER_LIB_VL1, AACDECODER_LIB_VL2);
    955   LIB_VERSION_STRING(info);
    956   info->build_date = AACDECODER_LIB_BUILD_DATE;
    957   info->build_time = AACDECODER_LIB_BUILD_TIME;
    958   info->title = AACDECODER_LIB_TITLE;
    959 
    960   /* Set flags */
    961   info->flags = 0
    962       | CAPF_AAC_LC
    963       | CAPF_AAC_VCB11
    964       | CAPF_AAC_HCR
    965       | CAPF_AAC_RVLC
    966       | CAPF_ER_AAC_LD
    967       | CAPF_ER_AAC_ELD
    968       | CAPF_AAC_CONCEALMENT
    969       | CAPF_AAC_DRC
    970 
    971       | CAPF_AAC_MPEG4
    972 
    973 
    974       | CAPF_AAC_1024
    975       | CAPF_AAC_960
    976 
    977       | CAPF_AAC_512
    978 
    979       | CAPF_AAC_480
    980 
    981       ;
    982   /* End of flags */
    983 
    984   return 0;
    985 }
    986 
    987 
    988 
    989 
    990