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 - 2013 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):   Josef Hoepfl
     87    Description: ADTS interface
     88 
     89 ******************************************************************************/
     90 
     91 #include "tpdec_adts.h"
     92 
     93 
     94 #include "FDK_bitstream.h"
     95 
     96 
     97 
     98 void adtsRead_CrcInit(HANDLE_ADTS pAdts) /*!< pointer to adts crc info stucture */
     99 {
    100   FDKcrcInit(&pAdts->crcInfo, 0x8005, 0xFFFF, 16);
    101 }
    102 
    103 int adtsRead_CrcStartReg(
    104                      HANDLE_ADTS pAdts,          /*!< pointer to adts stucture */
    105                      HANDLE_FDK_BITSTREAM hBs,   /*!< handle to current bit buffer structure */
    106                      int mBits                   /*!< number of bits in crc region */
    107                    )
    108 {
    109   if (pAdts->bs.protection_absent) {
    110     return 0;
    111   }
    112 
    113   return ( FDKcrcStartReg(&pAdts->crcInfo, hBs, mBits) );
    114 
    115 }
    116 
    117 void adtsRead_CrcEndReg(
    118                     HANDLE_ADTS pAdts, /*!< pointer to adts crc info stucture */
    119                     HANDLE_FDK_BITSTREAM hBs,   /*!< handle to current bit buffer structure */
    120                     int reg                    /*!< crc region */
    121                   )
    122 {
    123   if (pAdts->bs.protection_absent == 0)
    124   {
    125     FDKcrcEndReg(&pAdts->crcInfo, hBs, reg);
    126   }
    127 }
    128 
    129 TRANSPORTDEC_ERROR adtsRead_CrcCheck( HANDLE_ADTS pAdts )
    130 {
    131   TRANSPORTDEC_ERROR ErrorStatus = TRANSPORTDEC_OK;
    132   USHORT crc;
    133 
    134   if (pAdts->bs.protection_absent)
    135     return TRANSPORTDEC_OK;
    136 
    137   crc = FDKcrcGetCRC(&pAdts->crcInfo);
    138   if (crc != pAdts->crcReadValue)
    139   {
    140     return (TRANSPORTDEC_CRC_ERROR);
    141   }
    142 
    143   return (ErrorStatus);
    144 }
    145 
    146 
    147 
    148 #define Adts_Length_SyncWord                     12
    149 #define Adts_Length_Id                            1
    150 #define Adts_Length_Layer                         2
    151 #define Adts_Length_ProtectionAbsent              1
    152 #define Adts_Length_Profile                       2
    153 #define Adts_Length_SamplingFrequencyIndex        4
    154 #define Adts_Length_PrivateBit                    1
    155 #define Adts_Length_ChannelConfiguration          3
    156 #define Adts_Length_OriginalCopy                  1
    157 #define Adts_Length_Home                          1
    158 #define Adts_Length_CopyrightIdentificationBit    1
    159 #define Adts_Length_CopyrightIdentificationStart  1
    160 #define Adts_Length_FrameLength                  13
    161 #define Adts_Length_BufferFullness               11
    162 #define Adts_Length_NumberOfRawDataBlocksInFrame  2
    163 #define Adts_Length_CrcCheck                     16
    164 
    165 TRANSPORTDEC_ERROR adtsRead_DecodeHeader(
    166         HANDLE_ADTS           pAdts,
    167         CSAudioSpecificConfig *pAsc,
    168         HANDLE_FDK_BITSTREAM  hBs,
    169         const INT             ignoreBufferFullness
    170         )
    171 {
    172   INT crcReg;
    173 
    174   INT valBits;
    175   INT cmp_buffer_fullness;
    176   int i, adtsHeaderLength;
    177 
    178   STRUCT_ADTS_BS bs;
    179 
    180 #ifdef TP_PCE_ENABLE
    181   CProgramConfig oldPce;
    182   /* Store the old PCE temporarily. Maybe we'll need it later if we
    183      have channelConfig=0 and no PCE in this frame. */
    184   FDKmemcpy(&oldPce, &pAsc->m_progrConfigElement, sizeof(CProgramConfig));
    185 #endif
    186 
    187   valBits = FDKgetValidBits(hBs);
    188 
    189   /* adts_fixed_header */
    190   bs.mpeg_id           = FDKreadBits(hBs, Adts_Length_Id);
    191   bs.layer             = FDKreadBits(hBs, Adts_Length_Layer);
    192   bs.protection_absent = FDKreadBits(hBs, Adts_Length_ProtectionAbsent);
    193   bs.profile           = FDKreadBits(hBs, Adts_Length_Profile);
    194   bs.sample_freq_index = FDKreadBits(hBs, Adts_Length_SamplingFrequencyIndex);
    195   bs.private_bit       = FDKreadBits(hBs, Adts_Length_PrivateBit);
    196   bs.channel_config    = FDKreadBits(hBs, Adts_Length_ChannelConfiguration);
    197   bs.original          = FDKreadBits(hBs, Adts_Length_OriginalCopy);
    198   bs.home              = FDKreadBits(hBs, Adts_Length_Home);
    199 
    200   /* adts_variable_header */
    201   bs.copyright_id    = FDKreadBits(hBs, Adts_Length_CopyrightIdentificationBit);
    202   bs.copyright_start = FDKreadBits(hBs, Adts_Length_CopyrightIdentificationStart);
    203   bs.frame_length    = FDKreadBits(hBs, Adts_Length_FrameLength);
    204   bs.adts_fullness   = FDKreadBits(hBs, Adts_Length_BufferFullness);
    205   bs.num_raw_blocks  = FDKreadBits(hBs, Adts_Length_NumberOfRawDataBlocksInFrame);
    206   bs.num_pce_bits    = 0;
    207 
    208   adtsHeaderLength = ADTS_HEADERLENGTH;
    209 
    210   if (!bs.protection_absent) {
    211     FDKcrcReset(&pAdts->crcInfo);
    212     FDKpushBack(hBs, 56);   /* complete fixed and variable header! */
    213     crcReg = FDKcrcStartReg(&pAdts->crcInfo, hBs, 0);
    214     FDKpushFor(hBs, 56);
    215   }
    216 
    217   if (! bs.protection_absent && bs.num_raw_blocks>0) {
    218     for (i=0; i<bs.num_raw_blocks; i++) {
    219       pAdts->rawDataBlockDist[i] = (USHORT)FDKreadBits(hBs, 16);
    220       adtsHeaderLength += 16;
    221     }
    222     /* Change raw data blocks to delta values */
    223     pAdts->rawDataBlockDist[bs.num_raw_blocks] = bs.frame_length - 7 - bs.num_raw_blocks*2 - 2 ;
    224     for (i=bs.num_raw_blocks; i>0; i--) {
    225       pAdts->rawDataBlockDist[i] -= pAdts->rawDataBlockDist[i-1];
    226     }
    227   }
    228 
    229   /* adts_error_check */
    230   if (!bs.protection_absent)
    231   {
    232     USHORT crc_check;
    233 
    234     FDKcrcEndReg(&pAdts->crcInfo, hBs, crcReg);
    235     crc_check = FDKreadBits(hBs, Adts_Length_CrcCheck);
    236     adtsHeaderLength += Adts_Length_CrcCheck;
    237 
    238     pAdts->crcReadValue = crc_check;
    239     /* Check header CRC in case of multiple raw data blocks */
    240     if (bs.num_raw_blocks > 0) {
    241       if (pAdts->crcReadValue != FDKcrcGetCRC(&pAdts->crcInfo)) {
    242         return TRANSPORTDEC_CRC_ERROR;
    243       }
    244       /* Reset CRC for the upcoming raw_data_block() */
    245       FDKcrcReset(&pAdts->crcInfo);
    246     }
    247   }
    248 
    249 
    250   /* check if valid header */
    251   if (
    252       (bs.layer != 0) ||                          // we only support MPEG ADTS
    253       (bs.sample_freq_index >= 13)                // we only support 96kHz - 7350kHz
    254       ) {
    255     FDKpushFor(hBs, bs.frame_length * 8);   // try again one frame later
    256     return TRANSPORTDEC_UNSUPPORTED_FORMAT;
    257   }
    258 
    259   /* special treatment of id-bit */
    260   if ( (bs.mpeg_id == 0) && (pAdts->decoderCanDoMpeg4 == 0) )
    261   {
    262     /* MPEG-2 decoder cannot play MPEG-4 bitstreams */
    263 
    264 
    265     FDKpushFor(hBs, bs.frame_length * 8);  // try again one frame later
    266     return TRANSPORTDEC_UNSUPPORTED_FORMAT;
    267   }
    268 
    269   if (!ignoreBufferFullness)
    270   {
    271     cmp_buffer_fullness = bs.frame_length*8 + bs.adts_fullness*32*getNumberOfEffectiveChannels(bs.channel_config);
    272 
    273 
    274     /* Evaluate buffer fullness */
    275     if (bs.adts_fullness != 0x7FF)
    276     {
    277       if (pAdts->BufferFullnesStartFlag)
    278       {
    279         if ( valBits < cmp_buffer_fullness )
    280         {
    281           /* Condition for start of decoding is not fulfilled */
    282 
    283           /* The current frame will not be decoded */
    284           FDKpushBack(hBs, adtsHeaderLength);
    285 
    286           if ( (cmp_buffer_fullness+adtsHeaderLength) > ((TRANSPORTDEC_INBUF_SIZE<<3)-7) ) {
    287             return TRANSPORTDEC_SYNC_ERROR;
    288           } else {
    289             return TRANSPORTDEC_NOT_ENOUGH_BITS;
    290           }
    291         }
    292         else
    293         {
    294           pAdts->BufferFullnesStartFlag = 0;
    295         }
    296       }
    297     }
    298   }
    299 
    300 
    301   /* Get info from ADTS header */
    302   AudioSpecificConfig_Init(pAsc);
    303   pAsc->m_aot                    = (AUDIO_OBJECT_TYPE)(bs.profile + 1);
    304   pAsc->m_samplingFrequencyIndex = bs.sample_freq_index;
    305   pAsc->m_samplingFrequency      = SamplingRateTable[bs.sample_freq_index];
    306   pAsc->m_channelConfiguration   = bs.channel_config;
    307   pAsc->m_samplesPerFrame        = 1024;
    308 
    309 #ifdef TP_PCE_ENABLE
    310   if (bs.channel_config == 0)
    311   {
    312     int pceBits = 0;
    313     UINT alignAnchor = FDKgetValidBits(hBs);
    314 
    315     if (FDKreadBits(hBs,3) == ID_PCE) {
    316       /* Got luck! Parse the PCE */
    317       int crcReg;
    318       crcReg = adtsRead_CrcStartReg(pAdts, hBs, 0);
    319 
    320       CProgramConfig_Read(&pAsc->m_progrConfigElement, hBs, alignAnchor);
    321 
    322       adtsRead_CrcEndReg(pAdts, hBs, crcReg);
    323       pceBits = alignAnchor - FDKgetValidBits(hBs);
    324       /* store the number of PCE bits */
    325       bs.num_pce_bits = pceBits;
    326     }
    327     else {
    328       /* No PCE in this frame! Push back the ID tag bits. */
    329       FDKpushBack(hBs,3);
    330 
    331       /* Encoders do not have to write a PCE in each frame.
    332          So if we already have a valid PCE we have to use it. */
    333       if (  oldPce.isValid
    334         && (bs.sample_freq_index == pAdts->bs.sample_freq_index)  /* we could compare the complete fixed header (bytes) here! */
    335         && (bs.channel_config    == pAdts->bs.channel_config) /* == 0 */
    336         && (bs.mpeg_id           == pAdts->bs.mpeg_id) )
    337       { /* Restore previous PCE which is still valid */
    338         FDKmemcpy(&pAsc->m_progrConfigElement, &oldPce, sizeof(CProgramConfig));
    339       }
    340       else if (bs.mpeg_id == 0) {
    341         /* If not it seems that we have a implicit channel configuration.
    342            This mode is not allowed in the context of ISO/IEC 14496-3.
    343            Skip this frame and try the next one. */
    344         FDKpushFor(hBs, (bs.frame_length<<3) - adtsHeaderLength - 3);
    345         return TRANSPORTDEC_UNSUPPORTED_FORMAT;
    346       }
    347       /* else {
    348          ISO/IEC 13818-7 implicit channel mapping is allowed.
    349          So just open the box of chocolates to see what we got.
    350       } */
    351     }
    352   }
    353 #endif /* TP_PCE_ENABLE */
    354 
    355   /* Copy bit stream data struct to persistent memory now, once we passed all sanity checks above. */
    356   FDKmemcpy(&pAdts->bs, &bs, sizeof(STRUCT_ADTS_BS));
    357 
    358   return TRANSPORTDEC_OK;
    359 }
    360 
    361 int adtsRead_GetRawDataBlockLength(
    362         HANDLE_ADTS pAdts,
    363         INT         blockNum
    364         )
    365 {
    366   int length;
    367 
    368   if (pAdts->bs.num_raw_blocks == 0) {
    369     length = (pAdts->bs.frame_length - 7) << 3;    /* aac_frame_length subtracted by the header size (7 bytes). */
    370     if (pAdts->bs.protection_absent == 0)
    371       length -= 16;                                                                   /* substract 16 bit CRC */
    372   } else {
    373     if (pAdts->bs.protection_absent) {
    374       length = -1; /* raw data block length is unknown */
    375     } else {
    376       if (blockNum < 0 || blockNum > 3) {
    377         length = -1;
    378       }
    379       length = (pAdts->rawDataBlockDist[blockNum] << 3) - 16;
    380     }
    381   }
    382   if (blockNum == 0 && length > 0) {
    383     length -= pAdts->bs.num_pce_bits;
    384   }
    385   return length;
    386 }
    387 
    388 
    389