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