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):
     98 
     99    Description:
    100 
    101 *******************************************************************************/
    102 
    103 #include "tp_data.h"
    104 
    105 #include "tpenc_lib.h"
    106 #include "tpenc_asc.h"
    107 #include "FDK_bitstream.h"
    108 #include "genericStds.h"
    109 
    110 #include "FDK_crc.h"
    111 
    112 #define PCE_HEIGHT_EXT_SYNC (0xAC)
    113 #define HEIGHT_NORMAL 0
    114 #define HEIGHT_TOP 1
    115 #define HEIGHT_BOTTOM 2
    116 #define MAX_FRONT_ELEMENTS 8
    117 #define MAX_SIDE_ELEMENTS 3
    118 #define MAX_BACK_ELEMENTS 4
    119 
    120 /**
    121  *  Describe additional PCE height information for front, side and back channel
    122  * elements.
    123  */
    124 typedef struct {
    125   UCHAR
    126   num_front_height_channel_elements[2];      /*!< Number of front channel
    127                                                 elements in top [0] and bottom
    128                                                 [1] plane. */
    129   UCHAR num_side_height_channel_elements[2]; /*!< Number of side channel
    130                                                 elements in top [0] and bottom
    131                                                 [1] plane. */
    132   UCHAR num_back_height_channel_elements[2]; /*!< Number of back channel
    133                                                 elements in top [0] and bottom
    134                                                 [1] plane. */
    135 } PCE_HEIGHT_NUM;
    136 
    137 /**
    138  *  Describe a PCE based on placed channel elements and element type sequence.
    139  */
    140 typedef struct {
    141   UCHAR num_front_channel_elements; /*!< Number of front channel elements. */
    142   UCHAR num_side_channel_elements;  /*!< Number of side channel elements. */
    143   UCHAR num_back_channel_elements;  /*!< Number of back channel elements. */
    144   UCHAR num_lfe_channel_elements;   /*!< Number of lfe channel elements. */
    145   const MP4_ELEMENT_ID
    146       *pEl_type; /*!< List contains sequence describing the elements
    147                       in present channel mode. (MPEG order) */
    148   const PCE_HEIGHT_NUM *pHeight_num;
    149 } PCE_CONFIGURATION;
    150 
    151 /**
    152  *  Map an incoming channel mode to a existing PCE configuration entry.
    153  */
    154 typedef struct {
    155   CHANNEL_MODE channel_mode; /*!< Present channel mode. */
    156   PCE_CONFIGURATION
    157   pce_configuration; /*!< Program config element description. */
    158 
    159 } CHANNEL_CONFIGURATION;
    160 
    161 /**
    162  * The following arrays provide the IDs of the consecutive elements for each
    163  * mode.
    164  */
    165 static const MP4_ELEMENT_ID elType_1[] = {ID_SCE};
    166 static const MP4_ELEMENT_ID elType_2[] = {ID_CPE};
    167 static const MP4_ELEMENT_ID elType_1_2[] = {ID_SCE, ID_CPE};
    168 static const MP4_ELEMENT_ID elType_1_2_1[] = {ID_SCE, ID_CPE, ID_SCE};
    169 static const MP4_ELEMENT_ID elType_1_2_2[] = {ID_SCE, ID_CPE, ID_CPE};
    170 static const MP4_ELEMENT_ID elType_1_2_2_1[] = {ID_SCE, ID_CPE, ID_CPE, ID_LFE};
    171 static const MP4_ELEMENT_ID elType_1_2_2_2_1[] = {ID_SCE, ID_CPE, ID_CPE,
    172                                                   ID_CPE, ID_LFE};
    173 static const MP4_ELEMENT_ID elType_6_1[] = {ID_SCE, ID_CPE, ID_CPE, ID_SCE,
    174                                             ID_LFE};
    175 static const MP4_ELEMENT_ID elType_7_1_back[] = {ID_SCE, ID_CPE, ID_CPE, ID_CPE,
    176                                                  ID_LFE};
    177 static const MP4_ELEMENT_ID elType_7_1_top_front[] = {ID_SCE, ID_CPE, ID_CPE,
    178                                                       ID_LFE, ID_CPE};
    179 static const MP4_ELEMENT_ID elType_7_1_rear_surround[] = {
    180     ID_SCE, ID_CPE, ID_CPE, ID_CPE, ID_LFE};
    181 static const MP4_ELEMENT_ID elType_7_1_front_center[] = {ID_SCE, ID_CPE, ID_CPE,
    182                                                          ID_CPE, ID_LFE};
    183 
    184 /**
    185  * The following arrays provide information on how many front, side and back
    186  * elements are assigned to the top or bottom plane for each mode that comprises
    187  * height information.
    188  */
    189 static const PCE_HEIGHT_NUM heightNum_7_1_top_front = {{1, 0}, {0, 0}, {0, 0}};
    190 
    191 /**
    192  * \brief Table contains all supported channel modes and according PCE
    193  configuration description.
    194  *
    195  * The mode identifier is followed by the number of front, side, back, and LFE
    196  elements.
    197  * These are followed by a pointer to the IDs of the consecutive elements
    198  (ID_SCE, ID_CPE, ID_LFE).
    199  *
    200  * For some modes (MODE_7_1_TOP_FRONT and MODE_22_2) additional height
    201  information is transmitted.
    202  * In this case the additional pointer provides information on how many front,
    203  side and back elements
    204  * are assigned to the top or bottom plane.The elements are arranged in the
    205  following order: normal height (front, side, back, LFE), top height (front,
    206  side, back), bottom height (front, side, back).
    207  *
    208  *
    209  * E.g. MODE_7_1_TOP_FRONT means:
    210  *                          - 3 elements are front channel elements.
    211  *                          - 0 elements are side channel elements.
    212  *                          - 1 element is back channel element.
    213  *                          - 1 element is an LFE channel element.
    214  *                          - the element order is ID_SCE, ID_CPE, ID_CPE,
    215  ID_LFE, ID_CPE.
    216  *                          - 1 of the front elements is in the top plane.
    217  *
    218  * This leads to the following mapping for the cconsecutive elements in the
    219  MODE_7_1_TOP_FRONT bitstream:
    220  *                          - ID_SCE -> normal height front,
    221                             - ID_CPE -> normal height front,
    222                             - ID_CPE -> normal height back,
    223                             - ID_LFE -> normal height LFE,
    224                             - ID_CPE -> top height front.
    225  */
    226 static const CHANNEL_CONFIGURATION pceConfigTab[] = {
    227     {MODE_1,
    228      {1, 0, 0, 0, elType_1,
    229       NULL}}, /* don't transmit height information in this mode */
    230     {MODE_2,
    231      {1, 0, 0, 0, elType_2,
    232       NULL}}, /* don't transmit height information in this mode */
    233     {MODE_1_2,
    234      {2, 0, 0, 0, elType_1_2,
    235       NULL}}, /* don't transmit height information in this mode */
    236     {MODE_1_2_1,
    237      {2, 0, 1, 0, elType_1_2_1,
    238       NULL}}, /* don't transmit height information in this mode */
    239     {MODE_1_2_2,
    240      {2, 0, 1, 0, elType_1_2_2,
    241       NULL}}, /* don't transmit height information in this mode */
    242     {MODE_1_2_2_1,
    243      {2, 0, 1, 1, elType_1_2_2_1,
    244       NULL}}, /* don't transmit height information in this mode */
    245     {MODE_1_2_2_2_1,
    246      {3, 0, 1, 1, elType_1_2_2_2_1,
    247       NULL}}, /* don't transmit height information in this mode */
    248 
    249     {MODE_6_1,
    250      {2, 0, 2, 1, elType_6_1,
    251       NULL}}, /* don't transmit height information in this mode */
    252     {MODE_7_1_BACK,
    253      {2, 0, 2, 1, elType_7_1_back,
    254       NULL}}, /* don't transmit height information in this mode */
    255     {MODE_7_1_TOP_FRONT,
    256      {3, 0, 1, 1, elType_7_1_top_front, &heightNum_7_1_top_front}},
    257 
    258     {MODE_7_1_REAR_SURROUND,
    259      {2, 0, 2, 1, elType_7_1_rear_surround,
    260       NULL}}, /* don't transmit height information in this mode */
    261     {MODE_7_1_FRONT_CENTER,
    262      {3, 0, 1, 1, elType_7_1_front_center,
    263       NULL}} /* don't transmit height information in this mode */
    264 };
    265 
    266 /**
    267  * \brief  Get program config element description for existing channel mode.
    268  *
    269  * \param channel_mode          Current channel mode.
    270  *
    271  * \return
    272  *          - Pointer to PCE_CONFIGURATION entry, on success.
    273  *          - NULL, on failure.
    274  */
    275 static const PCE_CONFIGURATION *getPceEntry(const CHANNEL_MODE channel_mode) {
    276   UINT i;
    277   const PCE_CONFIGURATION *pce_config = NULL;
    278 
    279   for (i = 0; i < (sizeof(pceConfigTab) / sizeof(CHANNEL_CONFIGURATION)); i++) {
    280     if (pceConfigTab[i].channel_mode == channel_mode) {
    281       pce_config = &pceConfigTab[i].pce_configuration;
    282       break;
    283     }
    284   }
    285 
    286   return pce_config;
    287 }
    288 
    289 int getChannelConfig(const CHANNEL_MODE channel_mode,
    290                      const UCHAR channel_config_zero) {
    291   INT chan_config = 0;
    292 
    293   if (channel_config_zero != 0) {
    294     chan_config = 0;
    295   } else {
    296     switch (channel_mode) {
    297       case MODE_1:
    298         chan_config = 1;
    299         break;
    300       case MODE_2:
    301         chan_config = 2;
    302         break;
    303       case MODE_1_2:
    304         chan_config = 3;
    305         break;
    306       case MODE_1_2_1:
    307         chan_config = 4;
    308         break;
    309       case MODE_1_2_2:
    310         chan_config = 5;
    311         break;
    312       case MODE_1_2_2_1:
    313         chan_config = 6;
    314         break;
    315       case MODE_1_2_2_2_1:
    316         chan_config = 7;
    317         break;
    318       case MODE_6_1:
    319         chan_config = 11;
    320         break;
    321       case MODE_7_1_BACK:
    322         chan_config = 12;
    323         break;
    324       case MODE_7_1_TOP_FRONT:
    325         chan_config = 14;
    326         break;
    327       default:
    328         chan_config = 0;
    329     }
    330   }
    331 
    332   return chan_config;
    333 }
    334 
    335 CHANNEL_MODE transportEnc_GetChannelMode(int noChannels) {
    336   CHANNEL_MODE chMode;
    337 
    338   if (noChannels <= 8 && noChannels > 0)
    339     chMode = (CHANNEL_MODE)(
    340         (noChannels == 8) ? 7
    341                           : noChannels); /* see : iso/mpeg4 v1 audio subpart1*/
    342   else
    343     chMode = MODE_UNKNOWN;
    344 
    345   return chMode;
    346 }
    347 
    348 int transportEnc_writePCE(HANDLE_FDK_BITSTREAM hBs, CHANNEL_MODE channelMode,
    349                           INT sampleRate, int instanceTagPCE, int profile,
    350                           int matrixMixdownA, int pseudoSurroundEnable,
    351                           UINT alignAnchor) {
    352   int sampleRateIndex, i;
    353   const PCE_CONFIGURATION *config = NULL;
    354   const MP4_ELEMENT_ID *pEl_list = NULL;
    355   UCHAR cpeCnt = 0, sceCnt = 0, lfeCnt = 0, frntCnt = 0, sdCnt = 0, bckCnt = 0,
    356         isCpe = 0, tag = 0, normalFrontEnd = 0, normalSideEnd = 0,
    357         normalBackEnd = 0, topFrontEnd = 0, topSideEnd = 0, topBackEnd = 0,
    358         bottomFrontEnd = 0, bottomSideEnd = 0;
    359 #ifdef FDK_ASSERT_ENABLE
    360   UCHAR bottomBackEnd = 0;
    361 #endif
    362   enum elementDepth { FRONT, SIDE, BACK } elDepth;
    363 
    364   sampleRateIndex = getSamplingRateIndex(sampleRate, 4);
    365   if (sampleRateIndex == 15) {
    366     return -1;
    367   }
    368 
    369   if ((config = getPceEntry(channelMode)) == NULL) {
    370     return -1;
    371   }
    372 
    373   FDK_ASSERT(config->num_front_channel_elements <= MAX_FRONT_ELEMENTS);
    374   FDK_ASSERT(config->num_side_channel_elements <= MAX_SIDE_ELEMENTS);
    375   FDK_ASSERT(config->num_back_channel_elements <= MAX_BACK_ELEMENTS);
    376 
    377   UCHAR frontIsCpe[MAX_FRONT_ELEMENTS] = {0},
    378         frontTag[MAX_FRONT_ELEMENTS] = {0}, sideIsCpe[MAX_SIDE_ELEMENTS] = {0},
    379         sideTag[MAX_SIDE_ELEMENTS] = {0}, backIsCpe[MAX_BACK_ELEMENTS] = {0},
    380         backTag[MAX_BACK_ELEMENTS] = {0};
    381 
    382   /* Write general information */
    383 
    384   FDKwriteBits(hBs, instanceTagPCE, 4);  /* Element instance tag */
    385   FDKwriteBits(hBs, profile, 2);         /* Object type */
    386   FDKwriteBits(hBs, sampleRateIndex, 4); /* Sample rate index*/
    387 
    388   FDKwriteBits(hBs, config->num_front_channel_elements,
    389                4); /* Front channel Elements */
    390   FDKwriteBits(hBs, config->num_side_channel_elements,
    391                4); /* No Side Channel Elements */
    392   FDKwriteBits(hBs, config->num_back_channel_elements,
    393                4); /* No Back channel Elements */
    394   FDKwriteBits(hBs, config->num_lfe_channel_elements,
    395                2); /* No Lfe channel elements */
    396 
    397   FDKwriteBits(hBs, 0, 3); /* No assoc data elements */
    398   FDKwriteBits(hBs, 0, 4); /* No valid cc elements */
    399   FDKwriteBits(hBs, 0, 1); /* Mono mixdown present */
    400   FDKwriteBits(hBs, 0, 1); /* Stereo mixdown present */
    401 
    402   if (matrixMixdownA != 0 &&
    403       ((channelMode == MODE_1_2_2) || (channelMode == MODE_1_2_2_1))) {
    404     FDKwriteBits(hBs, 1, 1); /* Matrix mixdown present */
    405     FDKwriteBits(hBs, (matrixMixdownA - 1) & 0x3, 2); /* matrix_mixdown_idx */
    406     FDKwriteBits(hBs, (pseudoSurroundEnable) ? 1 : 0,
    407                  1); /* pseudo_surround_enable */
    408   } else {
    409     FDKwriteBits(hBs, 0, 1); /* Matrix mixdown not present */
    410   }
    411 
    412   if (config->pHeight_num != NULL) {
    413     /* we have up to three different height levels, and in each height level we
    414      * may have front, side and back channels. We need to know where each
    415      * section ends to correctly count the tags */
    416     normalFrontEnd = config->num_front_channel_elements -
    417                      config->pHeight_num->num_front_height_channel_elements[0] -
    418                      config->pHeight_num->num_front_height_channel_elements[1];
    419     normalSideEnd = normalFrontEnd + config->num_side_channel_elements -
    420                     config->pHeight_num->num_side_height_channel_elements[0] -
    421                     config->pHeight_num->num_side_height_channel_elements[1];
    422     normalBackEnd = normalSideEnd + config->num_back_channel_elements -
    423                     config->pHeight_num->num_back_height_channel_elements[0] -
    424                     config->pHeight_num->num_back_height_channel_elements[1];
    425 
    426     topFrontEnd =
    427         normalBackEnd + config->num_lfe_channel_elements +
    428         config->pHeight_num->num_front_height_channel_elements[0]; /* only
    429                                                                       normal
    430                                                                       height
    431                                                                       LFEs
    432                                                                       assumed */
    433     topSideEnd =
    434         topFrontEnd + config->pHeight_num->num_side_height_channel_elements[0];
    435     topBackEnd =
    436         topSideEnd + config->pHeight_num->num_back_height_channel_elements[0];
    437 
    438     bottomFrontEnd =
    439         topBackEnd + config->pHeight_num->num_front_height_channel_elements[1];
    440     bottomSideEnd = bottomFrontEnd +
    441                     config->pHeight_num->num_side_height_channel_elements[1];
    442 #ifdef FDK_ASSERT_ENABLE
    443     bottomBackEnd = bottomSideEnd +
    444                     config->pHeight_num->num_back_height_channel_elements[1];
    445 #endif
    446 
    447   } else {
    448     /* we have only one height level, so we don't care about top or bottom */
    449     normalFrontEnd = config->num_front_channel_elements;
    450     normalSideEnd = normalFrontEnd + config->num_side_channel_elements;
    451     normalBackEnd = normalSideEnd + config->num_back_channel_elements;
    452   }
    453 
    454   /* assign cpe and tag information to either front, side or back channels */
    455 
    456   pEl_list = config->pEl_type;
    457 
    458   for (i = 0; i < config->num_front_channel_elements +
    459                       config->num_side_channel_elements +
    460                       config->num_back_channel_elements +
    461                       config->num_lfe_channel_elements;
    462        i++) {
    463     if (*pEl_list == ID_LFE) {
    464       pEl_list++;
    465       continue;
    466     }
    467     isCpe = (*pEl_list++ == ID_CPE) ? 1 : 0;
    468     tag = (isCpe) ? cpeCnt++ : sceCnt++;
    469 
    470     if (i < normalFrontEnd)
    471       elDepth = FRONT;
    472     else if (i < normalSideEnd)
    473       elDepth = SIDE;
    474     else if (i < normalBackEnd)
    475       elDepth = BACK;
    476     else if (i < topFrontEnd)
    477       elDepth = FRONT;
    478     else if (i < topSideEnd)
    479       elDepth = SIDE;
    480     else if (i < topBackEnd)
    481       elDepth = BACK;
    482     else if (i < bottomFrontEnd)
    483       elDepth = FRONT;
    484     else if (i < bottomSideEnd)
    485       elDepth = SIDE;
    486     else {
    487       elDepth = BACK;
    488       FDK_ASSERT(i < bottomBackEnd); /* won't fail if implementation of pce
    489                                         configuration table is correct */
    490     }
    491 
    492     switch (elDepth) {
    493       case FRONT:
    494         FDK_ASSERT(frntCnt < config->num_front_channel_elements);
    495         frontIsCpe[frntCnt] = isCpe;
    496         frontTag[frntCnt++] = tag;
    497         break;
    498       case SIDE:
    499         FDK_ASSERT(sdCnt < config->num_side_channel_elements);
    500         sideIsCpe[sdCnt] = isCpe;
    501         sideTag[sdCnt++] = tag;
    502         break;
    503       case BACK:
    504         FDK_ASSERT(bckCnt < config->num_back_channel_elements);
    505         backIsCpe[bckCnt] = isCpe;
    506         backTag[bckCnt++] = tag;
    507         break;
    508     }
    509   }
    510 
    511   /* Write front channel isCpe and tags */
    512   for (i = 0; i < config->num_front_channel_elements; i++) {
    513     FDKwriteBits(hBs, frontIsCpe[i], 1);
    514     FDKwriteBits(hBs, frontTag[i], 4);
    515   }
    516   /* Write side channel isCpe and tags */
    517   for (i = 0; i < config->num_side_channel_elements; i++) {
    518     FDKwriteBits(hBs, sideIsCpe[i], 1);
    519     FDKwriteBits(hBs, sideTag[i], 4);
    520   }
    521   /* Write back channel isCpe and tags */
    522   for (i = 0; i < config->num_back_channel_elements; i++) {
    523     FDKwriteBits(hBs, backIsCpe[i], 1);
    524     FDKwriteBits(hBs, backTag[i], 4);
    525   }
    526   /* Write LFE information */
    527   for (i = 0; i < config->num_lfe_channel_elements; i++) {
    528     FDKwriteBits(hBs, lfeCnt++, 4); /* LFE channel Instance Tag. */
    529   }
    530 
    531   /* - num_valid_cc_elements always 0.
    532      - num_assoc_data_elements always 0. */
    533 
    534   /* Byte alignment: relative to alignAnchor
    535        ADTS: align with respect to the first bit of the raw_data_block()
    536        ADIF: align with respect to the first bit of the header
    537        LATM: align with respect to the first bit of the ASC */
    538   FDKbyteAlign(hBs, alignAnchor); /* Alignment */
    539 
    540   /* Write comment information */
    541 
    542   if (config->pHeight_num != NULL) {
    543     /* embed height information in comment field */
    544 
    545     INT commentBytes =
    546         1 /* PCE_HEIGHT_EXT_SYNC */
    547         + ((((config->num_front_channel_elements +
    548               config->num_side_channel_elements +
    549               config->num_back_channel_elements)
    550              << 1) +
    551             7) >>
    552            3) /* 2 bit height info per element, round up to full bytes */
    553         + 1;  /* CRC */
    554 
    555     FDKwriteBits(hBs, commentBytes, 8); /* comment size. */
    556 
    557     FDK_CRCINFO crcInfo; /* CRC state info */
    558     INT crcReg;
    559 
    560     FDKcrcInit(&crcInfo, 0x07, 0xFF, 8);
    561     crcReg = FDKcrcStartReg(&crcInfo, hBs, 0);
    562 
    563     FDKwriteBits(hBs, PCE_HEIGHT_EXT_SYNC, 8); /* indicate height extension */
    564 
    565     /* front channel height information */
    566     for (i = 0;
    567          i < config->num_front_channel_elements -
    568                  config->pHeight_num->num_front_height_channel_elements[0] -
    569                  config->pHeight_num->num_front_height_channel_elements[1];
    570          i++)
    571       FDKwriteBits(hBs, HEIGHT_NORMAL, 2);
    572     for (i = 0; i < config->pHeight_num->num_front_height_channel_elements[0];
    573          i++)
    574       FDKwriteBits(hBs, HEIGHT_TOP, 2);
    575     for (i = 0; i < config->pHeight_num->num_front_height_channel_elements[1];
    576          i++)
    577       FDKwriteBits(hBs, HEIGHT_BOTTOM, 2);
    578 
    579     /* side channel height information */
    580     for (i = 0;
    581          i < config->num_side_channel_elements -
    582                  config->pHeight_num->num_side_height_channel_elements[0] -
    583                  config->pHeight_num->num_side_height_channel_elements[1];
    584          i++)
    585       FDKwriteBits(hBs, HEIGHT_NORMAL, 2);
    586     for (i = 0; i < config->pHeight_num->num_side_height_channel_elements[0];
    587          i++)
    588       FDKwriteBits(hBs, HEIGHT_TOP, 2);
    589     for (i = 0; i < config->pHeight_num->num_side_height_channel_elements[1];
    590          i++)
    591       FDKwriteBits(hBs, HEIGHT_BOTTOM, 2);
    592 
    593     /* back channel height information */
    594     for (i = 0;
    595          i < config->num_back_channel_elements -
    596                  config->pHeight_num->num_back_height_channel_elements[0] -
    597                  config->pHeight_num->num_back_height_channel_elements[1];
    598          i++)
    599       FDKwriteBits(hBs, HEIGHT_NORMAL, 2);
    600     for (i = 0; i < config->pHeight_num->num_back_height_channel_elements[0];
    601          i++)
    602       FDKwriteBits(hBs, HEIGHT_TOP, 2);
    603     for (i = 0; i < config->pHeight_num->num_back_height_channel_elements[1];
    604          i++)
    605       FDKwriteBits(hBs, HEIGHT_BOTTOM, 2);
    606 
    607     FDKbyteAlign(hBs, alignAnchor); /* Alignment */
    608 
    609     FDKcrcEndReg(&crcInfo, hBs, crcReg);
    610     FDKwriteBits(hBs, FDKcrcGetCRC(&crcInfo), 8);
    611 
    612   } else {
    613     FDKwriteBits(hBs, 0,
    614                  8); /* Do no write any comment or height information. */
    615   }
    616 
    617   return 0;
    618 }
    619 
    620 int transportEnc_GetPCEBits(CHANNEL_MODE channelMode, int matrixMixdownA,
    621                             int bits) {
    622   const PCE_CONFIGURATION *config = NULL;
    623 
    624   if ((config = getPceEntry(channelMode)) == NULL) {
    625     return -1; /* unsupported channelmapping */
    626   }
    627 
    628   bits +=
    629       4 + 2 + 4; /* Element instance tag  + Object type + Sample rate index */
    630   bits += 4 + 4 + 4 + 2; /* No (front + side + back + lfe channel) elements */
    631   bits += 3 + 4;         /* No (assoc data + valid cc) elements */
    632   bits += 1 + 1 + 1;     /* Mono + Stereo + Matrix mixdown present */
    633 
    634   if (matrixMixdownA != 0 &&
    635       ((channelMode == MODE_1_2_2) || (channelMode == MODE_1_2_2_1))) {
    636     bits += 3; /* matrix_mixdown_idx + pseudo_surround_enable */
    637   }
    638 
    639   bits += (1 + 4) * (INT)config->num_front_channel_elements;
    640   bits += (1 + 4) * (INT)config->num_side_channel_elements;
    641   bits += (1 + 4) * (INT)config->num_back_channel_elements;
    642   bits += (4) * (INT)config->num_lfe_channel_elements;
    643 
    644   /* - num_valid_cc_elements always 0.
    645      - num_assoc_data_elements always 0. */
    646 
    647   if ((bits % 8) != 0) {
    648     bits += (8 - (bits % 8)); /* Alignment */
    649   }
    650 
    651   bits += 8; /* Comment field  bytes */
    652 
    653   if (config->pHeight_num != NULL) {
    654     /* Comment field (height extension) */
    655 
    656     bits +=
    657         8 /* PCE_HEIGHT_EXT_SYNC */
    658         +
    659         ((config->num_front_channel_elements +
    660           config->num_side_channel_elements + config->num_back_channel_elements)
    661          << 1) /* 2 bit height info per element */
    662         + 8;   /* CRC */
    663 
    664     if ((bits % 8) != 0) {
    665       bits += (8 - (bits % 8)); /* Alignment */
    666     }
    667   }
    668 
    669   return bits;
    670 }
    671 
    672 static void writeAot(HANDLE_FDK_BITSTREAM hBitstreamBuffer,
    673                      AUDIO_OBJECT_TYPE aot) {
    674   int tmp = (int)aot;
    675 
    676   if (tmp > 31) {
    677     FDKwriteBits(hBitstreamBuffer, AOT_ESCAPE, 5);
    678     FDKwriteBits(hBitstreamBuffer, tmp - 32, 6); /* AudioObjectType */
    679   } else {
    680     FDKwriteBits(hBitstreamBuffer, tmp, 5);
    681   }
    682 }
    683 
    684 static void writeSampleRate(HANDLE_FDK_BITSTREAM hBs, int sampleRate,
    685                             int nBits) {
    686   int srIdx = getSamplingRateIndex(sampleRate, nBits);
    687 
    688   FDKwriteBits(hBs, srIdx, nBits);
    689   if (srIdx == (1 << nBits) - 1) {
    690     FDKwriteBits(hBs, sampleRate, 24);
    691   }
    692 }
    693 
    694 static int transportEnc_writeGASpecificConfig(HANDLE_FDK_BITSTREAM asc,
    695                                               CODER_CONFIG *config, int extFlg,
    696                                               UINT alignAnchor) {
    697   int aot = config->aot;
    698   int samplesPerFrame = config->samplesPerFrame;
    699 
    700   /* start of GASpecificConfig according to ISO/IEC 14496-3 Subpart 4, 4.4.1 */
    701   FDKwriteBits(asc,
    702                ((samplesPerFrame == 960 || samplesPerFrame == 480) ? 1 : 0),
    703                1); /* frameLengthFlag: 1 for a 960/480 (I)MDCT, 0 for a 1024/512
    704                       (I)MDCT*/
    705   FDKwriteBits(asc, 0,
    706                1); /* dependsOnCoreCoder: Sampling Rate Coder Specific, see in
    707                       ISO/IEC 14496-3 Subpart 4, 4.4.1 */
    708   FDKwriteBits(asc, extFlg,
    709                1); /* Extension Flag: Shall be 1 for aot = 17,19,20,21,22,23 */
    710 
    711   /* Write PCE if channel config is not 1-7 */
    712   if (getChannelConfig(config->channelMode, config->channelConfigZero) == 0) {
    713     transportEnc_writePCE(asc, config->channelMode, config->samplingRate, 0, 1,
    714                           config->matrixMixdownA,
    715                           (config->flags & CC_PSEUDO_SURROUND) ? 1 : 0,
    716                           alignAnchor);
    717   }
    718   if ((aot == AOT_AAC_SCAL) || (aot == AOT_ER_AAC_SCAL)) {
    719     FDKwriteBits(asc, 0, 3); /* layerNr */
    720   }
    721   if (extFlg) {
    722     if (aot == AOT_ER_BSAC) {
    723       FDKwriteBits(asc, config->BSACnumOfSubFrame, 5); /* numOfSubFrame */
    724       FDKwriteBits(asc, config->BSAClayerLength, 11);  /* layer_length */
    725     }
    726     if ((aot == AOT_ER_AAC_LC) || (aot == AOT_ER_AAC_LTP) ||
    727         (aot == AOT_ER_AAC_SCAL) || (aot == AOT_ER_AAC_LD)) {
    728       FDKwriteBits(asc, (config->flags & CC_VCB11) ? 1 : 0,
    729                    1); /* aacSectionDataResillienceFlag */
    730       FDKwriteBits(asc, (config->flags & CC_RVLC) ? 1 : 0,
    731                    1); /* aacScaleFactorDataResillienceFlag */
    732       FDKwriteBits(asc, (config->flags & CC_HCR) ? 1 : 0,
    733                    1); /* aacSpectralDataResillienceFlag */
    734     }
    735     FDKwriteBits(asc, 0, 1); /* extensionFlag3: reserved. Shall be '0' */
    736   }
    737   return 0;
    738 }
    739 
    740 static int transportEnc_writeELDSpecificConfig(HANDLE_FDK_BITSTREAM hBs,
    741                                                CODER_CONFIG *config,
    742                                                int epConfig,
    743                                                CSTpCallBacks *cb) {
    744   UINT frameLengthFlag = 0;
    745   switch (config->samplesPerFrame) {
    746     case 512:
    747     case 256:
    748     case 128:
    749     case 64:
    750       frameLengthFlag = 0;
    751       break;
    752     case 480:
    753     case 240:
    754     case 160:
    755     case 120:
    756     case 60:
    757       frameLengthFlag = 1;
    758       break;
    759   }
    760 
    761   FDKwriteBits(hBs, frameLengthFlag, 1);
    762 
    763   FDKwriteBits(hBs, (config->flags & CC_VCB11) ? 1 : 0, 1);
    764   FDKwriteBits(hBs, (config->flags & CC_RVLC) ? 1 : 0, 1);
    765   FDKwriteBits(hBs, (config->flags & CC_HCR) ? 1 : 0, 1);
    766 
    767   FDKwriteBits(hBs, (config->flags & CC_SBR) ? 1 : 0, 1); /* SBR header flag */
    768   if ((config->flags & CC_SBR)) {
    769     FDKwriteBits(hBs, (config->samplingRate == config->extSamplingRate) ? 0 : 1,
    770                  1); /* Samplerate Flag */
    771     FDKwriteBits(hBs, (config->flags & CC_SBRCRC) ? 1 : 0, 1); /* SBR CRC flag*/
    772 
    773     if (cb->cbSbr != NULL) {
    774       const PCE_CONFIGURATION *pPce;
    775       int e, sbrElementIndex = 0;
    776 
    777       pPce = getPceEntry(config->channelMode);
    778 
    779       for (e = 0; e < pPce->num_front_channel_elements +
    780                           pPce->num_side_channel_elements +
    781                           pPce->num_back_channel_elements +
    782                           pPce->num_lfe_channel_elements;
    783            e++) {
    784         if ((pPce->pEl_type[e] == ID_SCE) || (pPce->pEl_type[e] == ID_CPE)) {
    785           cb->cbSbr(cb->cbSbrData, hBs, 0, 0, 0, config->aot, pPce->pEl_type[e],
    786                     sbrElementIndex, 0, 0, 0, NULL, 1);
    787           sbrElementIndex++;
    788         }
    789       }
    790     }
    791   }
    792 
    793   if ((config->flags & CC_SAC) && (cb->cbSsc != NULL)) {
    794     FDKwriteBits(hBs, ELDEXT_LDSAC, 4);
    795 
    796     const INT eldExtLen =
    797         (cb->cbSsc(cb->cbSscData, NULL, config->aot, config->extSamplingRate, 0,
    798                    0, 0, 0, 0, NULL) +
    799          7) >>
    800         3;
    801     INT cnt = eldExtLen;
    802 
    803     if (cnt < 0xF) {
    804       FDKwriteBits(hBs, cnt, 4);
    805     } else {
    806       FDKwriteBits(hBs, 0xF, 4);
    807       cnt -= 0xF;
    808 
    809       if (cnt < 0xFF) {
    810         FDKwriteBits(hBs, cnt, 8);
    811       } else {
    812         FDKwriteBits(hBs, 0xFF, 8);
    813         cnt -= 0xFF;
    814 
    815         FDK_ASSERT(cnt <= 0xFFFF);
    816         FDKwriteBits(hBs, cnt, 16);
    817       }
    818     }
    819 
    820     cb->cbSsc(cb->cbSscData, hBs, config->aot, config->extSamplingRate, 0, 0, 0,
    821               0, 0, NULL);
    822   }
    823 
    824   if (config->downscaleSamplingRate != 0 &&
    825       config->downscaleSamplingRate != config->extSamplingRate) {
    826     /* downscale active */
    827 
    828     /* eldExtLenDsc: Number of bytes for the ELD downscale extension (srIdx
    829        needs 1 byte
    830        + downscaleSamplingRate needs additional 3 bytes) */
    831     int eldExtLenDsc = 1;
    832     int downscaleSamplingRate = config->downscaleSamplingRate;
    833     FDKwriteBits(hBs, ELDEXT_DOWNSCALEINFO, 4); /* ELDEXT_DOWNSCALEINFO */
    834 
    835     if ((downscaleSamplingRate != 96000) && (downscaleSamplingRate != 88200) &&
    836         (downscaleSamplingRate != 64000) && (downscaleSamplingRate != 48000) &&
    837         (downscaleSamplingRate != 44100) && (downscaleSamplingRate != 32000) &&
    838         (downscaleSamplingRate != 24000) && (downscaleSamplingRate != 22050) &&
    839         (downscaleSamplingRate != 16000) && (downscaleSamplingRate != 12000) &&
    840         (downscaleSamplingRate != 11025) && (downscaleSamplingRate != 8000) &&
    841         (downscaleSamplingRate != 7350)) {
    842       eldExtLenDsc = 4; /* length extends to 4 if downscaleSamplingRate's value
    843                            is not one of the listed values */
    844     }
    845 
    846     FDKwriteBits(hBs, eldExtLenDsc, 4);
    847     writeSampleRate(hBs, downscaleSamplingRate, 4);
    848     FDKwriteBits(hBs, 0x0, 4); /* fill_nibble */
    849   }
    850 
    851   FDKwriteBits(hBs, ELDEXT_TERM, 4); /* ELDEXT_TERM */
    852 
    853   return 0;
    854 }
    855 
    856 static int transportEnc_writeUsacSpecificConfig(HANDLE_FDK_BITSTREAM hBs,
    857                                                 int extFlag, CODER_CONFIG *cc,
    858                                                 CSTpCallBacks *cb) {
    859   FDK_BITSTREAM usacConf;
    860   int usacConfigBits = cc->rawConfigBits;
    861 
    862   if ((usacConfigBits <= 0) ||
    863       ((usacConfigBits + 7) / 8 > (int)sizeof(cc->rawConfig))) {
    864     return TRANSPORTENC_UNSUPPORTED_FORMAT;
    865   }
    866   FDKinitBitStream(&usacConf, cc->rawConfig, BUFSIZE_DUMMY_VALUE,
    867                    usacConfigBits, BS_READER);
    868 
    869   for (; usacConfigBits > 0; usacConfigBits--) {
    870     UINT tmp = FDKreadBit(&usacConf);
    871     FDKwriteBits(hBs, tmp, 1);
    872   }
    873   FDKsyncCache(hBs);
    874 
    875   return TRANSPORTENC_OK;
    876 }
    877 
    878 int transportEnc_writeASC(HANDLE_FDK_BITSTREAM asc, CODER_CONFIG *config,
    879                           CSTpCallBacks *cb) {
    880   UINT extFlag = 0;
    881   int err;
    882   int epConfig = 0;
    883 
    884   /* Required for the PCE. */
    885   UINT alignAnchor = FDKgetValidBits(asc);
    886 
    887   /* Extension Flag: Shall be 1 for aot = 17,19,20,21,22,23,39 */
    888   switch (config->aot) {
    889     case AOT_ER_AAC_LC:
    890     case AOT_ER_AAC_LTP:
    891     case AOT_ER_AAC_SCAL:
    892     case AOT_ER_TWIN_VQ:
    893     case AOT_ER_BSAC:
    894     case AOT_ER_AAC_LD:
    895     case AOT_ER_AAC_ELD:
    896     case AOT_USAC:
    897       extFlag = 1;
    898       break;
    899     default:
    900       break;
    901   }
    902 
    903   if (config->sbrSignaling == SIG_EXPLICIT_HIERARCHICAL && config->sbrPresent)
    904     writeAot(asc, config->extAOT);
    905   else
    906     writeAot(asc, config->aot);
    907 
    908   /* In case of USAC it is the output not the core sampling rate */
    909   writeSampleRate(asc, config->samplingRate, 4);
    910 
    911   /* Try to guess a reasonable channel mode if not given */
    912   if (config->channelMode == MODE_INVALID) {
    913     config->channelMode = transportEnc_GetChannelMode(config->noChannels);
    914     if (config->channelMode == MODE_INVALID) return -1;
    915   }
    916 
    917   FDKwriteBits(
    918       asc, getChannelConfig(config->channelMode, config->channelConfigZero), 4);
    919 
    920   if (config->sbrSignaling == SIG_EXPLICIT_HIERARCHICAL && config->sbrPresent) {
    921     writeSampleRate(asc, config->extSamplingRate, 4);
    922     writeAot(asc, config->aot);
    923   }
    924 
    925   switch (config->aot) {
    926     case AOT_AAC_MAIN:
    927     case AOT_AAC_LC:
    928     case AOT_AAC_SSR:
    929     case AOT_AAC_LTP:
    930     case AOT_AAC_SCAL:
    931     case AOT_TWIN_VQ:
    932     case AOT_ER_AAC_LC:
    933     case AOT_ER_AAC_LTP:
    934     case AOT_ER_AAC_SCAL:
    935     case AOT_ER_TWIN_VQ:
    936     case AOT_ER_BSAC:
    937     case AOT_ER_AAC_LD:
    938       err =
    939           transportEnc_writeGASpecificConfig(asc, config, extFlag, alignAnchor);
    940       if (err) return err;
    941       break;
    942     case AOT_ER_AAC_ELD:
    943       err = transportEnc_writeELDSpecificConfig(asc, config, epConfig, cb);
    944       if (err) return err;
    945       break;
    946     case AOT_USAC:
    947       err = transportEnc_writeUsacSpecificConfig(asc, extFlag, config, cb);
    948       if (err) {
    949         return err;
    950       }
    951       break;
    952     default:
    953       return -1;
    954   }
    955 
    956   switch (config->aot) {
    957     case AOT_ER_AAC_LC:
    958     case AOT_ER_AAC_LTP:
    959     case AOT_ER_AAC_SCAL:
    960     case AOT_ER_TWIN_VQ:
    961     case AOT_ER_BSAC:
    962     case AOT_ER_AAC_LD:
    963     case AOT_ER_CELP:
    964     case AOT_ER_HVXC:
    965     case AOT_ER_HILN:
    966     case AOT_ER_PARA:
    967     case AOT_ER_AAC_ELD:
    968       FDKwriteBits(asc, 0, 2); /* epconfig 0 */
    969       break;
    970     default:
    971       break;
    972   }
    973 
    974   /* backward compatible explicit signaling of extension AOT */
    975   if (config->sbrSignaling == SIG_EXPLICIT_BW_COMPATIBLE) {
    976     TP_ASC_EXTENSION_ID ascExtId = ASCEXT_UNKOWN;
    977 
    978     if (config->sbrPresent) {
    979       ascExtId = ASCEXT_SBR;
    980       FDKwriteBits(asc, ascExtId, 11);
    981       writeAot(asc, config->extAOT);
    982       FDKwriteBits(asc, 1, 1); /* sbrPresentFlag=1 */
    983       writeSampleRate(asc, config->extSamplingRate, 4);
    984       if (config->psPresent) {
    985         ascExtId = ASCEXT_PS;
    986         FDKwriteBits(asc, ascExtId, 11);
    987         FDKwriteBits(asc, 1, 1); /* psPresentFlag=1 */
    988       }
    989     }
    990   }
    991 
    992   /* Make sure all bits are sync'ed */
    993   FDKsyncCache(asc);
    994 
    995   return 0;
    996 }
    997