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 encoder library *********************
     96 
     97    Author(s):   Alex Groeschel
     98 
     99    Description: ADTS Transport Headers support
    100 
    101 *******************************************************************************/
    102 
    103 #include "tpenc_adts.h"
    104 
    105 #include "tpenc_lib.h"
    106 #include "tpenc_asc.h"
    107 
    108 int adtsWrite_CrcStartReg(
    109     HANDLE_ADTS pAdts,        /*!< pointer to adts stucture */
    110     HANDLE_FDK_BITSTREAM hBs, /*!< handle to current bit buffer structure */
    111     int mBits                 /*!< number of bits in crc region */
    112 ) {
    113   if (pAdts->protection_absent) {
    114     return 0;
    115   }
    116   return (FDKcrcStartReg(&pAdts->crcInfo, hBs, mBits));
    117 }
    118 
    119 void adtsWrite_CrcEndReg(
    120     HANDLE_ADTS pAdts,        /*!< pointer to adts crc info stucture */
    121     HANDLE_FDK_BITSTREAM hBs, /*!< handle to current bit buffer structure */
    122     int reg                   /*!< crc region */
    123 ) {
    124   if (pAdts->protection_absent == 0) {
    125     FDKcrcEndReg(&pAdts->crcInfo, hBs, reg);
    126   }
    127 }
    128 
    129 int adtsWrite_GetHeaderBits(HANDLE_ADTS hAdts) {
    130   int bits = 0;
    131 
    132   if (hAdts->currentBlock == 0) {
    133     /* Static and variable header bits */
    134     bits = 56;
    135     if (!hAdts->protection_absent) {
    136       /* Add header/ single raw data block CRC bits */
    137       bits += 16;
    138       if (hAdts->num_raw_blocks > 0) {
    139         /* Add bits of raw data block position markers */
    140         bits += (hAdts->num_raw_blocks) * 16;
    141       }
    142     }
    143   }
    144   if (!hAdts->protection_absent && hAdts->num_raw_blocks > 0) {
    145     /* Add raw data block CRC bits. Not really part of the header, put they
    146      * cause bit overhead to be accounted. */
    147     bits += 16;
    148   }
    149 
    150   hAdts->headerBits = bits;
    151 
    152   return bits;
    153 }
    154 
    155 INT adtsWrite_Init(HANDLE_ADTS hAdts, CODER_CONFIG *config) {
    156   /* Sanity checks */
    157   if (config->nSubFrames < 1 || config->nSubFrames > 4 ||
    158       (int)config->aot > 4 || (int)config->aot < 1) {
    159     return -1;
    160   }
    161 
    162   /* fixed header */
    163   if (config->flags & CC_MPEG_ID) {
    164     hAdts->mpeg_id = 0; /* MPEG 4 */
    165   } else {
    166     hAdts->mpeg_id = 1; /* MPEG 2 */
    167   }
    168   hAdts->layer = 0;
    169   hAdts->protection_absent = !(config->flags & CC_PROTECTION);
    170   hAdts->profile = ((int)config->aot) - 1;
    171   hAdts->sample_freq_index = getSamplingRateIndex(config->samplingRate, 4);
    172   hAdts->sample_freq = config->samplingRate;
    173   hAdts->private_bit = 0;
    174   hAdts->channel_mode = config->channelMode;
    175   hAdts->original = 0;
    176   hAdts->home = 0;
    177   /* variable header */
    178   hAdts->copyright_id = 0;
    179   hAdts->copyright_start = 0;
    180 
    181   hAdts->num_raw_blocks = config->nSubFrames - 1; /* 0 means 1 raw data block */
    182 
    183   hAdts->channel_config_zero = config->channelConfigZero;
    184 
    185   FDKcrcInit(&hAdts->crcInfo, 0x8005, 0xFFFF, 16);
    186 
    187   hAdts->currentBlock = 0;
    188 
    189   return 0;
    190 }
    191 
    192 int adtsWrite_EncodeHeader(HANDLE_ADTS hAdts, HANDLE_FDK_BITSTREAM hBitStream,
    193                            int buffer_fullness, int frame_length) {
    194   INT crcIndex = 0;
    195 
    196   hAdts->headerBits = adtsWrite_GetHeaderBits(hAdts);
    197 
    198   FDK_ASSERT(((frame_length + hAdts->headerBits) / 8) < 0x2000); /*13 bit*/
    199   FDK_ASSERT(buffer_fullness < 0x800);                           /* 11 bit   */
    200 
    201   if (!hAdts->protection_absent) {
    202     FDKcrcReset(&hAdts->crcInfo);
    203   }
    204 
    205   if (hAdts->currentBlock == 0) {
    206     FDKresetBitbuffer(hBitStream, BS_WRITER);
    207   }
    208 
    209   hAdts->subFrameStartBit = FDKgetValidBits(hBitStream);
    210 
    211   /* Skip new header if this is raw data block 1..n */
    212   if (hAdts->currentBlock == 0) {
    213     FDKresetBitbuffer(hBitStream, BS_WRITER);
    214 
    215     if (hAdts->num_raw_blocks == 0) {
    216       crcIndex = adtsWrite_CrcStartReg(hAdts, hBitStream, 0);
    217     }
    218 
    219     /* fixed header */
    220     FDKwriteBits(hBitStream, 0xFFF, 12);
    221     FDKwriteBits(hBitStream, hAdts->mpeg_id, 1);
    222     FDKwriteBits(hBitStream, hAdts->layer, 2);
    223     FDKwriteBits(hBitStream, hAdts->protection_absent, 1);
    224     FDKwriteBits(hBitStream, hAdts->profile, 2);
    225     FDKwriteBits(hBitStream, hAdts->sample_freq_index, 4);
    226     FDKwriteBits(hBitStream, hAdts->private_bit, 1);
    227     FDKwriteBits(
    228         hBitStream,
    229         getChannelConfig(hAdts->channel_mode, hAdts->channel_config_zero), 3);
    230     FDKwriteBits(hBitStream, hAdts->original, 1);
    231     FDKwriteBits(hBitStream, hAdts->home, 1);
    232     /* variable header */
    233     FDKwriteBits(hBitStream, hAdts->copyright_id, 1);
    234     FDKwriteBits(hBitStream, hAdts->copyright_start, 1);
    235     FDKwriteBits(hBitStream, (frame_length + hAdts->headerBits) >> 3, 13);
    236     FDKwriteBits(hBitStream, buffer_fullness, 11);
    237     FDKwriteBits(hBitStream, hAdts->num_raw_blocks, 2);
    238 
    239     if (!hAdts->protection_absent) {
    240       int i;
    241 
    242       /* End header CRC portion for single raw data block and write dummy zero
    243        * values for unknown fields. */
    244       if (hAdts->num_raw_blocks == 0) {
    245         adtsWrite_CrcEndReg(hAdts, hBitStream, crcIndex);
    246       } else {
    247         for (i = 0; i < hAdts->num_raw_blocks; i++) {
    248           FDKwriteBits(hBitStream, 0, 16);
    249         }
    250       }
    251       FDKwriteBits(hBitStream, 0, 16);
    252     }
    253   } /* End of ADTS header */
    254 
    255   return 0;
    256 }
    257 
    258 void adtsWrite_EndRawDataBlock(HANDLE_ADTS hAdts, HANDLE_FDK_BITSTREAM hBs,
    259                                int *pBits) {
    260   if (!hAdts->protection_absent) {
    261     FDK_BITSTREAM bsWriter;
    262 
    263     FDKinitBitStream(&bsWriter, hBs->hBitBuf.Buffer, hBs->hBitBuf.bufSize, 0,
    264                      BS_WRITER);
    265     FDKpushFor(&bsWriter, 56);
    266 
    267     if (hAdts->num_raw_blocks == 0) {
    268       FDKwriteBits(&bsWriter, FDKcrcGetCRC(&hAdts->crcInfo), 16);
    269     } else {
    270       int distance;
    271 
    272       /* Write CRC of current raw data block */
    273       FDKwriteBits(hBs, FDKcrcGetCRC(&hAdts->crcInfo), 16);
    274 
    275       /* Write distance to current data block */
    276       if (hAdts->currentBlock < hAdts->num_raw_blocks) {
    277         FDKpushFor(&bsWriter, hAdts->currentBlock * 16);
    278         distance =
    279             FDKgetValidBits(hBs) - (56 + (hAdts->num_raw_blocks) * 16 + 16);
    280         FDKwriteBits(&bsWriter, distance >> 3, 16);
    281       }
    282     }
    283     FDKsyncCache(&bsWriter);
    284   }
    285 
    286   /* Write total frame lenth for multiple raw data blocks and header CRC */
    287   if (hAdts->num_raw_blocks > 0 &&
    288       hAdts->currentBlock == hAdts->num_raw_blocks) {
    289     FDK_BITSTREAM bsWriter;
    290     int crcIndex = 0;
    291 
    292     FDKinitBitStream(&bsWriter, hBs->hBitBuf.Buffer, hBs->hBitBuf.bufSize, 0,
    293                      BS_WRITER);
    294 
    295     if (!hAdts->protection_absent) {
    296       FDKcrcReset(&hAdts->crcInfo);
    297       crcIndex = FDKcrcStartReg(&hAdts->crcInfo, &bsWriter, 0);
    298     }
    299     /* Write total frame length */
    300     FDKpushFor(&bsWriter, 56 - 28 + 2);
    301     FDKwriteBits(&bsWriter, FDKgetValidBits(hBs) >> 3, 13);
    302 
    303     /* Write header CRC */
    304     if (!hAdts->protection_absent) {
    305       FDKpushFor(&bsWriter, 11 + 2 + (hAdts->num_raw_blocks) * 16);
    306       FDKcrcEndReg(&hAdts->crcInfo, &bsWriter, crcIndex);
    307       FDKwriteBits(&bsWriter, FDKcrcGetCRC(&hAdts->crcInfo), 16);
    308     }
    309     FDKsyncCache(&bsWriter);
    310   }
    311 
    312   /* Correct *pBits to reflect the amount of bits of the current subframe */
    313   *pBits -= hAdts->subFrameStartBit;
    314   if (!hAdts->protection_absent && hAdts->num_raw_blocks > 0) {
    315     /* Fixup CRC bits, since they come after each raw data block */
    316     *pBits += 16;
    317   }
    318   hAdts->currentBlock++;
    319 }
    320