Home | History | Annotate | Download | only in src
      1 
      2 /* -----------------------------------------------------------------------------------------------------------
      3 Software License for The Fraunhofer FDK AAC Codec Library for Android
      4 
      5  Copyright  1995 - 2012 Fraunhofer-Gesellschaft zur Frderung der angewandten Forschung e.V.
      6   All rights reserved.
      7 
      8  1.    INTRODUCTION
      9 The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software that implements
     10 the MPEG Advanced Audio Coding ("AAC") encoding and decoding scheme for digital audio.
     11 This FDK AAC Codec software is intended to be used on a wide variety of Android devices.
     12 
     13 AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient general perceptual
     14 audio codecs. AAC-ELD is considered the best-performing full-bandwidth communications codec by
     15 independent studies and is widely deployed. AAC has been standardized by ISO and IEC as part
     16 of the MPEG specifications.
     17 
     18 Patent licenses for necessary patent claims for the FDK AAC Codec (including those of Fraunhofer)
     19 may be obtained through Via Licensing (www.vialicensing.com) or through the respective patent owners
     20 individually for the purpose of encoding or decoding bit streams in products that are compliant with
     21 the ISO/IEC MPEG audio standards. Please note that most manufacturers of Android devices already license
     22 these patent claims through Via Licensing or directly from the patent owners, and therefore FDK AAC Codec
     23 software may already be covered under those patent licenses when it is used for those licensed purposes only.
     24 
     25 Commercially-licensed AAC software libraries, including floating-point versions with enhanced sound quality,
     26 are also available from Fraunhofer. Users are encouraged to check the Fraunhofer website for additional
     27 applications information and documentation.
     28 
     29 2.    COPYRIGHT LICENSE
     30 
     31 Redistribution and use in source and binary forms, with or without modification, are permitted without
     32 payment of copyright license fees provided that you satisfy the following conditions:
     33 
     34 You must retain the complete text of this software license in redistributions of the FDK AAC Codec or
     35 your modifications thereto in source code form.
     36 
     37 You must retain the complete text of this software license in the documentation and/or other materials
     38 provided with redistributions of the FDK AAC Codec or your modifications thereto in binary form.
     39 You must make available free of charge copies of the complete source code of the FDK AAC Codec and your
     40 modifications thereto to recipients of copies in binary form.
     41 
     42 The name of Fraunhofer may not be used to endorse or promote products derived from this library without
     43 prior written permission.
     44 
     45 You may not charge copyright license fees for anyone to use, copy or distribute the FDK AAC Codec
     46 software or your modifications thereto.
     47 
     48 Your modified versions of the FDK AAC Codec must carry prominent notices stating that you changed the software
     49 and the date of any change. For modified versions of the FDK AAC Codec, the term
     50 "Fraunhofer FDK AAC Codec Library for Android" must be replaced by the term
     51 "Third-Party Modified Version of the Fraunhofer FDK AAC Codec Library for Android."
     52 
     53 3.    NO PATENT LICENSE
     54 
     55 NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without limitation the patents of Fraunhofer,
     56 ARE GRANTED BY THIS SOFTWARE LICENSE. Fraunhofer provides no warranty of patent non-infringement with
     57 respect to this software.
     58 
     59 You may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized
     60 by appropriate patent licenses.
     61 
     62 4.    DISCLAIMER
     63 
     64 This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright holders and contributors
     65 "AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, including but not limited to the implied warranties
     66 of merchantability and fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
     67 CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary, or consequential damages,
     68 including but not limited to procurement of substitute goods or services; loss of use, data, or profits,
     69 or business interruption, however caused and on any theory of liability, whether in contract, strict
     70 liability, or tort (including negligence), arising in any way out of the use of this software, even if
     71 advised of the possibility of such damage.
     72 
     73 5.    CONTACT INFORMATION
     74 
     75 Fraunhofer Institute for Integrated Circuits IIS
     76 Attention: Audio and Multimedia Departments - FDK AAC LL
     77 Am Wolfsmantel 33
     78 91058 Erlangen, Germany
     79 
     80 www.iis.fraunhofer.de/amm
     81 amm-info (at) iis.fraunhofer.de
     82 ----------------------------------------------------------------------------------------------------------- */
     83 
     84 /*!
     85   \file
     86   \brief  SBR bit writing routines
     87 */
     88 
     89 
     90 #include "bit_sbr.h"
     91 
     92 #include "code_env.h"
     93 #include "cmondata.h"
     94 #include "sbr.h"
     95 
     96 #include "ps_main.h"
     97 
     98 typedef enum {
     99   SBR_ID_SCE = 1,
    100   SBR_ID_CPE
    101 } SBR_ELEMENT_TYPE;
    102 
    103 
    104 static INT encodeSbrData (HANDLE_SBR_ENV_DATA       sbrEnvDataLeft,
    105                           HANDLE_SBR_ENV_DATA       sbrEnvDataRight,
    106                           HANDLE_PARAMETRIC_STEREO  hParametricStereo,
    107                           HANDLE_COMMON_DATA        cmonData,
    108                           SBR_ELEMENT_TYPE          sbrElem,
    109                           INT                       coupling,
    110                           UINT                      sbrSyntaxFlags);
    111 
    112 static INT encodeSbrHeader (HANDLE_SBR_HEADER_DATA     sbrHeaderData,
    113                             HANDLE_SBR_BITSTREAM_DATA  sbrBitstreamData,
    114                             HANDLE_COMMON_DATA         cmonData);
    115 
    116 
    117 static INT encodeSbrHeaderData (HANDLE_SBR_HEADER_DATA sbrHeaderData,
    118                                 HANDLE_FDK_BITSTREAM   hBitStream);
    119 
    120 static INT encodeSbrSingleChannelElement (HANDLE_SBR_ENV_DATA      sbrEnvData,
    121                                           HANDLE_FDK_BITSTREAM     hBitStream
    122                                           ,HANDLE_PARAMETRIC_STEREO hParametricStereo
    123                                           ,UINT  sbrSyntaxFlags
    124                                           );
    125 
    126 
    127 
    128 static INT encodeSbrChannelPairElement (HANDLE_SBR_ENV_DATA      sbrEnvDataLeft,
    129                                         HANDLE_SBR_ENV_DATA      sbrEnvDataRight,
    130                                         HANDLE_PARAMETRIC_STEREO hParametricStereo,
    131                                         HANDLE_FDK_BITSTREAM     hBitStream,
    132                                         INT                      coupling);
    133 
    134 
    135 static INT encodeSbrGrid (HANDLE_SBR_ENV_DATA   sbrEnvData,
    136                           HANDLE_FDK_BITSTREAM  hBitStream);
    137 
    138 static int encodeLowDelaySbrGrid ( HANDLE_SBR_ENV_DATA sbrEnvData,
    139                                    HANDLE_FDK_BITSTREAM hBitStream,
    140                                    int transmitFreqs);
    141 
    142 static INT encodeSbrDtdf (HANDLE_SBR_ENV_DATA   sbrEnvData,
    143                           HANDLE_FDK_BITSTREAM  hBitStream);
    144 
    145 static INT writeNoiseLevelData (HANDLE_SBR_ENV_DATA   sbrEnvData,
    146                                 HANDLE_FDK_BITSTREAM  hBitStream,
    147                                 INT                   coupling);
    148 
    149 static INT writeEnvelopeData (HANDLE_SBR_ENV_DATA    sbrEnvData,
    150                               HANDLE_FDK_BITSTREAM   hBitStream,
    151                               INT                    coupling);
    152 
    153 static INT writeSyntheticCodingData (HANDLE_SBR_ENV_DATA  sbrEnvData,
    154                                      HANDLE_FDK_BITSTREAM hBitStream);
    155 
    156 
    157 static INT encodeExtendedData (HANDLE_PARAMETRIC_STEREO hParametricStereo,
    158                                HANDLE_FDK_BITSTREAM     hBitStream);
    159 
    160 
    161 
    162 static INT getSbrExtendedDataSize (HANDLE_PARAMETRIC_STEREO  hParametricStereo);
    163 
    164 /*****************************************************************************
    165 
    166     functionname: FDKsbrEnc_WriteEnvSingleChannelElement
    167     description:  writes pure SBR single channel data element
    168     returns:      number of bits written
    169     input:
    170     output:
    171 
    172 *****************************************************************************/
    173 INT
    174 FDKsbrEnc_WriteEnvSingleChannelElement(
    175                                        HANDLE_SBR_HEADER_DATA sbrHeaderData,
    176                                        HANDLE_PARAMETRIC_STEREO         hParametricStereo,
    177                                        HANDLE_SBR_BITSTREAM_DATA        sbrBitstreamData,
    178                                        HANDLE_SBR_ENV_DATA              sbrEnvData,
    179                                        HANDLE_COMMON_DATA               cmonData,
    180                                        UINT                             sbrSyntaxFlags
    181                                       )
    182 
    183 {
    184   INT payloadBits = 0;
    185 
    186   cmonData->sbrHdrBits  = 0;
    187   cmonData->sbrDataBits = 0;
    188 
    189   /* write pure sbr data */
    190   if (sbrEnvData != NULL) {
    191 
    192     /* write header */
    193     payloadBits += encodeSbrHeader (sbrHeaderData,
    194                                     sbrBitstreamData,
    195                                     cmonData);
    196 
    197 
    198     /* write data */
    199     payloadBits += encodeSbrData (sbrEnvData,
    200                                   NULL,
    201                                   hParametricStereo,
    202                                   cmonData,
    203                                   SBR_ID_SCE,
    204                                   0,
    205                                   sbrSyntaxFlags);
    206 
    207   }
    208   return payloadBits;
    209 }
    210 
    211 /*****************************************************************************
    212 
    213     functionname: FDKsbrEnc_WriteEnvChannelPairElement
    214     description:  writes pure SBR channel pair data element
    215     returns:      number of bits written
    216     input:
    217     output:
    218 
    219 *****************************************************************************/
    220 INT
    221 FDKsbrEnc_WriteEnvChannelPairElement (HANDLE_SBR_HEADER_DATA     sbrHeaderData,
    222                                       HANDLE_PARAMETRIC_STEREO   hParametricStereo,
    223                                       HANDLE_SBR_BITSTREAM_DATA  sbrBitstreamData,
    224                                       HANDLE_SBR_ENV_DATA        sbrEnvDataLeft,
    225                                       HANDLE_SBR_ENV_DATA        sbrEnvDataRight,
    226                                       HANDLE_COMMON_DATA         cmonData,
    227                                       UINT                       sbrSyntaxFlags)
    228 
    229 {
    230   INT payloadBits = 0;
    231   cmonData->sbrHdrBits  = 0;
    232   cmonData->sbrDataBits = 0;
    233 
    234   /* write pure sbr data */
    235   if ((sbrEnvDataLeft != NULL) && (sbrEnvDataRight != NULL)) {
    236 
    237     /* write header */
    238     payloadBits += encodeSbrHeader (sbrHeaderData,
    239                                     sbrBitstreamData,
    240                                     cmonData);
    241 
    242     /* write data */
    243     payloadBits += encodeSbrData (sbrEnvDataLeft,
    244                                   sbrEnvDataRight,
    245                                   hParametricStereo,
    246                                   cmonData,
    247                                   SBR_ID_CPE,
    248                                   sbrHeaderData->coupling,
    249                                   sbrSyntaxFlags);
    250 
    251   }
    252   return payloadBits;
    253 }
    254 
    255 INT
    256 FDKsbrEnc_CountSbrChannelPairElement (HANDLE_SBR_HEADER_DATA     sbrHeaderData,
    257                                       HANDLE_PARAMETRIC_STEREO   hParametricStereo,
    258                                       HANDLE_SBR_BITSTREAM_DATA  sbrBitstreamData,
    259                                       HANDLE_SBR_ENV_DATA        sbrEnvDataLeft,
    260                                       HANDLE_SBR_ENV_DATA        sbrEnvDataRight,
    261                                       HANDLE_COMMON_DATA         cmonData,
    262                                       UINT                       sbrSyntaxFlags)
    263 {
    264   INT payloadBits;
    265   INT bitPos = FDKgetValidBits(&cmonData->sbrBitbuf);
    266 
    267   payloadBits = FDKsbrEnc_WriteEnvChannelPairElement(sbrHeaderData,
    268                                            hParametricStereo,
    269                                            sbrBitstreamData,
    270                                            sbrEnvDataLeft,
    271                                            sbrEnvDataRight,
    272                                            cmonData,
    273                                            sbrSyntaxFlags);
    274 
    275   FDKpushBack(&cmonData->sbrBitbuf, (FDKgetValidBits(&cmonData->sbrBitbuf) - bitPos) );
    276 
    277   return payloadBits;
    278 }
    279 
    280 
    281 void sbrEncoder_GetHeader(SBR_ENCODER   *sbrEncoder,
    282                           HANDLE_FDK_BITSTREAM hBs,
    283                           INT            element_index,
    284                           int            fSendHeaders)
    285 {
    286   int bits;
    287 
    288   bits = encodeSbrHeaderData (&sbrEncoder->sbrElement[element_index]->sbrHeaderData, hBs);
    289 
    290   if (fSendHeaders == 0) {
    291     /* Prevent header being embedded into the SBR payload. */
    292     sbrEncoder->sbrElement[element_index]->sbrBitstreamData.NrSendHeaderData = -1;
    293     sbrEncoder->sbrElement[element_index]->sbrBitstreamData.HeaderActive = 0;
    294     sbrEncoder->sbrElement[element_index]->sbrBitstreamData.CountSendHeaderData = -1;
    295   }
    296 }
    297 
    298 
    299 /*****************************************************************************
    300 
    301     functionname: encodeSbrHeader
    302     description:  encodes SBR Header information
    303     returns:      number of bits written
    304     input:
    305     output:
    306 
    307 *****************************************************************************/
    308 static INT
    309 encodeSbrHeader (HANDLE_SBR_HEADER_DATA     sbrHeaderData,
    310                  HANDLE_SBR_BITSTREAM_DATA  sbrBitstreamData,
    311                  HANDLE_COMMON_DATA         cmonData)
    312 {
    313   INT payloadBits = 0;
    314 
    315   if (sbrBitstreamData->HeaderActive) {
    316     payloadBits += FDKwriteBits (&cmonData->sbrBitbuf, 1, 1);
    317     payloadBits += encodeSbrHeaderData (sbrHeaderData,
    318                                         &cmonData->sbrBitbuf);
    319   }
    320   else {
    321     payloadBits += FDKwriteBits (&cmonData->sbrBitbuf, 0, 1);
    322   }
    323 
    324   cmonData->sbrHdrBits = payloadBits;
    325 
    326   return payloadBits;
    327 }
    328 
    329 
    330 
    331 /*****************************************************************************
    332 
    333     functionname: encodeSbrHeaderData
    334     description:  writes sbr_header()
    335                   bs_protocol_version through bs_header_extra_2
    336     returns:      number of bits written
    337     input:
    338     output:
    339 
    340 *****************************************************************************/
    341 static INT
    342 encodeSbrHeaderData (HANDLE_SBR_HEADER_DATA sbrHeaderData,
    343                      HANDLE_FDK_BITSTREAM hBitStream)
    344 
    345 {
    346   INT payloadBits = 0;
    347   if (sbrHeaderData != NULL) {
    348     payloadBits += FDKwriteBits (hBitStream, sbrHeaderData->sbr_amp_res,
    349                               SI_SBR_AMP_RES_BITS);
    350     payloadBits += FDKwriteBits (hBitStream, sbrHeaderData->sbr_start_frequency,
    351                               SI_SBR_START_FREQ_BITS);
    352     payloadBits += FDKwriteBits (hBitStream, sbrHeaderData->sbr_stop_frequency,
    353                               SI_SBR_STOP_FREQ_BITS);
    354     payloadBits += FDKwriteBits (hBitStream, sbrHeaderData->sbr_xover_band,
    355                               SI_SBR_XOVER_BAND_BITS);
    356 
    357     payloadBits += FDKwriteBits (hBitStream, 0,
    358                               SI_SBR_RESERVED_BITS);
    359 
    360     payloadBits += FDKwriteBits (hBitStream, sbrHeaderData->header_extra_1,
    361                               SI_SBR_HEADER_EXTRA_1_BITS);
    362     payloadBits += FDKwriteBits (hBitStream, sbrHeaderData->header_extra_2,
    363                               SI_SBR_HEADER_EXTRA_2_BITS);
    364 
    365 
    366     if (sbrHeaderData->header_extra_1) {
    367       payloadBits += FDKwriteBits (hBitStream, sbrHeaderData->freqScale,
    368                                 SI_SBR_FREQ_SCALE_BITS);
    369       payloadBits += FDKwriteBits (hBitStream, sbrHeaderData->alterScale,
    370                                 SI_SBR_ALTER_SCALE_BITS);
    371       payloadBits += FDKwriteBits (hBitStream, sbrHeaderData->sbr_noise_bands,
    372                                 SI_SBR_NOISE_BANDS_BITS);
    373     } /* sbrHeaderData->header_extra_1 */
    374 
    375     if (sbrHeaderData->header_extra_2) {
    376       payloadBits += FDKwriteBits (hBitStream, sbrHeaderData->sbr_limiter_bands,
    377                                 SI_SBR_LIMITER_BANDS_BITS);
    378       payloadBits += FDKwriteBits (hBitStream, sbrHeaderData->sbr_limiter_gains,
    379                                 SI_SBR_LIMITER_GAINS_BITS);
    380       payloadBits += FDKwriteBits (hBitStream, sbrHeaderData->sbr_interpol_freq,
    381                                 SI_SBR_INTERPOL_FREQ_BITS);
    382       payloadBits += FDKwriteBits (hBitStream, sbrHeaderData->sbr_smoothing_length,
    383                                 SI_SBR_SMOOTHING_LENGTH_BITS);
    384 
    385     } /* sbrHeaderData->header_extra_2 */
    386   } /* sbrHeaderData != NULL */
    387 
    388   return payloadBits;
    389 }
    390 
    391 
    392 /*****************************************************************************
    393 
    394     functionname: encodeSbrData
    395     description:  encodes sbr Data information
    396     returns:      number of bits written
    397     input:
    398     output:
    399 
    400 *****************************************************************************/
    401 static INT
    402 encodeSbrData (HANDLE_SBR_ENV_DATA   sbrEnvDataLeft,
    403                HANDLE_SBR_ENV_DATA   sbrEnvDataRight,
    404                HANDLE_PARAMETRIC_STEREO   hParametricStereo,
    405                HANDLE_COMMON_DATA    cmonData,
    406                SBR_ELEMENT_TYPE      sbrElem,
    407                INT                   coupling,
    408                UINT                  sbrSyntaxFlags)
    409 {
    410   INT payloadBits = 0;
    411 
    412   switch (sbrElem) {
    413   case SBR_ID_SCE:
    414     payloadBits += encodeSbrSingleChannelElement (sbrEnvDataLeft, &cmonData->sbrBitbuf, hParametricStereo, sbrSyntaxFlags);
    415     break;
    416   case SBR_ID_CPE:
    417     payloadBits += encodeSbrChannelPairElement (sbrEnvDataLeft, sbrEnvDataRight, hParametricStereo, &cmonData->sbrBitbuf, coupling);
    418     break;
    419   default:
    420     /* we never should apply SBR to any other element type */
    421     FDK_ASSERT (0);
    422   }
    423 
    424   cmonData->sbrDataBits = payloadBits;
    425 
    426   return payloadBits;
    427 }
    428 
    429 #define MODE_FREQ_TANS                          1
    430 #define MODE_NO_FREQ_TRAN                       0
    431 #define LD_TRANSMISSION                         MODE_FREQ_TANS
    432 static int encodeFreqs (int mode) {
    433     return ((mode & MODE_FREQ_TANS) ? 1 : 0);
    434 }
    435 
    436 
    437 /*****************************************************************************
    438 
    439     functionname: encodeSbrSingleChannelElement
    440     description:  encodes sbr SCE information
    441     returns:      number of bits written
    442     input:
    443     output:
    444 
    445 *****************************************************************************/
    446 static INT
    447 encodeSbrSingleChannelElement (HANDLE_SBR_ENV_DATA   sbrEnvData,
    448                                HANDLE_FDK_BITSTREAM  hBitStream
    449                                ,HANDLE_PARAMETRIC_STEREO hParametricStereo
    450                                ,UINT  sbrSyntaxFlags
    451                                )
    452 {
    453   INT i, payloadBits = 0;
    454 
    455   payloadBits += FDKwriteBits (hBitStream, 0, SI_SBR_DATA_EXTRA_BITS); /* no reserved bits */
    456 
    457   if (sbrEnvData->ldGrid) {
    458       if ( sbrEnvData->hSbrBSGrid->frameClass != FIXFIXonly ) {
    459         /* encode normal SbrGrid */
    460         payloadBits += encodeSbrGrid (sbrEnvData, hBitStream);
    461       } else {
    462         /* use FIXFIXonly frame Grid */
    463         payloadBits += encodeLowDelaySbrGrid ( sbrEnvData, hBitStream, encodeFreqs(LD_TRANSMISSION));
    464       }
    465   }
    466   else
    467   {
    468     if (sbrSyntaxFlags & SBR_SYNTAX_SCALABLE) {
    469       payloadBits += FDKwriteBits (hBitStream, 1, SI_SBR_COUPLING_BITS);
    470     }
    471     payloadBits += encodeSbrGrid (sbrEnvData, hBitStream);
    472   }
    473 
    474   payloadBits += encodeSbrDtdf (sbrEnvData, hBitStream);
    475 
    476   for (i = 0; i < sbrEnvData->noOfnoisebands; i++) {
    477     payloadBits += FDKwriteBits (hBitStream, sbrEnvData->sbr_invf_mode_vec[i], SI_SBR_INVF_MODE_BITS);
    478   }
    479 
    480   payloadBits += writeEnvelopeData (sbrEnvData, hBitStream, 0);
    481   payloadBits += writeNoiseLevelData (sbrEnvData, hBitStream, 0);
    482 
    483   payloadBits += writeSyntheticCodingData (sbrEnvData,hBitStream);
    484 
    485   payloadBits += encodeExtendedData(hParametricStereo, hBitStream);
    486 
    487   return payloadBits;
    488 }
    489 
    490 
    491 /*****************************************************************************
    492 
    493     functionname: encodeSbrChannelPairElement
    494     description:  encodes sbr CPE information
    495     returns:
    496     input:
    497     output:
    498 
    499 *****************************************************************************/
    500 static INT
    501 encodeSbrChannelPairElement (HANDLE_SBR_ENV_DATA   sbrEnvDataLeft,
    502                              HANDLE_SBR_ENV_DATA   sbrEnvDataRight,
    503                              HANDLE_PARAMETRIC_STEREO   hParametricStereo,
    504                              HANDLE_FDK_BITSTREAM  hBitStream,
    505                              INT                   coupling)
    506 {
    507   INT payloadBits = 0;
    508   INT i = 0;
    509 
    510   payloadBits += FDKwriteBits (hBitStream, 0, SI_SBR_DATA_EXTRA_BITS); /* no reserved bits */
    511 
    512   payloadBits += FDKwriteBits (hBitStream, coupling, SI_SBR_COUPLING_BITS);
    513 
    514   if (coupling) {
    515     if (sbrEnvDataLeft->ldGrid) {
    516           if ( sbrEnvDataLeft->hSbrBSGrid->frameClass != FIXFIXonly ) {
    517             /* normal SbrGrid */
    518             payloadBits += encodeSbrGrid (sbrEnvDataLeft, hBitStream);
    519 
    520           } else {
    521               /* FIXFIXonly frame Grid */
    522               payloadBits += encodeLowDelaySbrGrid ( sbrEnvDataLeft, hBitStream, encodeFreqs(LD_TRANSMISSION));
    523           }
    524     } else
    525         payloadBits += encodeSbrGrid (sbrEnvDataLeft, hBitStream);
    526 
    527     payloadBits += encodeSbrDtdf (sbrEnvDataLeft, hBitStream);
    528     payloadBits += encodeSbrDtdf (sbrEnvDataRight, hBitStream);
    529 
    530     for (i = 0; i < sbrEnvDataLeft->noOfnoisebands; i++) {
    531       payloadBits += FDKwriteBits (hBitStream, sbrEnvDataLeft->sbr_invf_mode_vec[i], SI_SBR_INVF_MODE_BITS);
    532     }
    533 
    534     payloadBits += writeEnvelopeData  (sbrEnvDataLeft,  hBitStream,1);
    535     payloadBits += writeNoiseLevelData (sbrEnvDataLeft,  hBitStream,1);
    536     payloadBits += writeEnvelopeData  (sbrEnvDataRight, hBitStream,1);
    537     payloadBits += writeNoiseLevelData (sbrEnvDataRight, hBitStream,1);
    538 
    539     payloadBits += writeSyntheticCodingData (sbrEnvDataLeft,hBitStream);
    540     payloadBits += writeSyntheticCodingData (sbrEnvDataRight,hBitStream);
    541 
    542   } else { /* no coupling */
    543     FDK_ASSERT(sbrEnvDataLeft->ldGrid == sbrEnvDataRight->ldGrid);
    544 
    545     if (sbrEnvDataLeft->ldGrid || sbrEnvDataRight->ldGrid) {
    546     /* sbrEnvDataLeft (left channel) */
    547       if ( sbrEnvDataLeft->hSbrBSGrid->frameClass != FIXFIXonly) {
    548         /* no FIXFIXonly Frame so we dont need encodeLowDelaySbrGrid */
    549         /* normal SbrGrid */
    550         payloadBits += encodeSbrGrid (sbrEnvDataLeft,  hBitStream);
    551 
    552       } else {
    553         /* FIXFIXonly frame Grid */
    554           payloadBits += encodeLowDelaySbrGrid ( sbrEnvDataLeft, hBitStream, encodeFreqs(LD_TRANSMISSION));
    555       }
    556 
    557       /* sbrEnvDataRight (right channel) */
    558       if ( sbrEnvDataRight->hSbrBSGrid->frameClass != FIXFIXonly) {
    559         /* no FIXFIXonly Frame so we dont need encodeLowDelaySbrGrid */
    560         /* normal SbrGrid */
    561         payloadBits += encodeSbrGrid (sbrEnvDataRight, hBitStream);
    562 
    563       } else {
    564         /* FIXFIXonly frame Grid */
    565           payloadBits += encodeLowDelaySbrGrid ( sbrEnvDataRight, hBitStream, encodeFreqs(LD_TRANSMISSION));
    566       }
    567     } else
    568     {
    569         payloadBits += encodeSbrGrid (sbrEnvDataLeft,  hBitStream);
    570         payloadBits += encodeSbrGrid (sbrEnvDataRight, hBitStream);
    571     }
    572     payloadBits += encodeSbrDtdf (sbrEnvDataLeft,  hBitStream);
    573     payloadBits += encodeSbrDtdf (sbrEnvDataRight, hBitStream);
    574 
    575     for (i = 0; i < sbrEnvDataLeft->noOfnoisebands; i++) {
    576       payloadBits += FDKwriteBits (hBitStream, sbrEnvDataLeft->sbr_invf_mode_vec[i],
    577                                 SI_SBR_INVF_MODE_BITS);
    578     }
    579     for (i = 0; i < sbrEnvDataRight->noOfnoisebands; i++) {
    580       payloadBits += FDKwriteBits (hBitStream, sbrEnvDataRight->sbr_invf_mode_vec[i],
    581                                 SI_SBR_INVF_MODE_BITS);
    582     }
    583 
    584     payloadBits += writeEnvelopeData  (sbrEnvDataLeft,  hBitStream,0);
    585     payloadBits += writeEnvelopeData  (sbrEnvDataRight, hBitStream,0);
    586     payloadBits += writeNoiseLevelData (sbrEnvDataLeft,  hBitStream,0);
    587     payloadBits += writeNoiseLevelData (sbrEnvDataRight, hBitStream,0);
    588 
    589     payloadBits += writeSyntheticCodingData (sbrEnvDataLeft,hBitStream);
    590     payloadBits += writeSyntheticCodingData (sbrEnvDataRight,hBitStream);
    591 
    592   } /* coupling */
    593 
    594   payloadBits += encodeExtendedData(hParametricStereo, hBitStream);
    595 
    596   return payloadBits;
    597 }
    598 
    599 static INT ceil_ln2(INT x)
    600 {
    601   INT tmp=-1;
    602   while((1<<++tmp) < x);
    603   return(tmp);
    604 }
    605 
    606 
    607 /*****************************************************************************
    608 
    609     functionname: encodeSbrGrid
    610     description:  if hBitStream != NULL writes bits that describes the
    611                   time/frequency grouping of a frame; else counts them only
    612     returns:      number of bits written or counted
    613     input:
    614     output:
    615 
    616 *****************************************************************************/
    617 static INT
    618 encodeSbrGrid (HANDLE_SBR_ENV_DATA sbrEnvData, HANDLE_FDK_BITSTREAM hBitStream)
    619 {
    620   INT payloadBits = 0;
    621   INT i, temp;
    622   INT bufferFrameStart = sbrEnvData->hSbrBSGrid->bufferFrameStart;
    623   INT numberTimeSlots  = sbrEnvData->hSbrBSGrid->numberTimeSlots;
    624 
    625   if (sbrEnvData->ldGrid)
    626     payloadBits += FDKwriteBits (hBitStream,
    627                                  sbrEnvData->hSbrBSGrid->frameClass,
    628                                  SBR_CLA_BITS_LD);
    629   else
    630     payloadBits += FDKwriteBits (hBitStream,
    631                                  sbrEnvData->hSbrBSGrid->frameClass,
    632                                  SBR_CLA_BITS);
    633 
    634   switch (sbrEnvData->hSbrBSGrid->frameClass) {
    635   case FIXFIXonly:
    636     FDK_ASSERT(0 /* Fatal error in encodeSbrGrid! */);
    637     break;
    638   case FIXFIX:
    639     temp = ceil_ln2(sbrEnvData->hSbrBSGrid->bs_num_env);
    640     payloadBits += FDKwriteBits (hBitStream, temp, SBR_ENV_BITS);
    641     if ((sbrEnvData->ldGrid) && (sbrEnvData->hSbrBSGrid->bs_num_env==1))
    642       payloadBits += FDKwriteBits(hBitStream, sbrEnvData->currentAmpResFF, SI_SBR_AMP_RES_BITS);
    643     payloadBits += FDKwriteBits (hBitStream, sbrEnvData->hSbrBSGrid->v_f[0], SBR_RES_BITS);
    644 
    645     break;
    646 
    647   case FIXVAR:
    648   case VARFIX:
    649     if (sbrEnvData->hSbrBSGrid->frameClass == FIXVAR)
    650       temp = sbrEnvData->hSbrBSGrid->bs_abs_bord - (bufferFrameStart + numberTimeSlots);
    651     else
    652       temp = sbrEnvData->hSbrBSGrid->bs_abs_bord - bufferFrameStart;
    653 
    654     payloadBits += FDKwriteBits (hBitStream, temp, SBR_ABS_BITS);
    655     payloadBits += FDKwriteBits (hBitStream, sbrEnvData->hSbrBSGrid->n, SBR_NUM_BITS);
    656 
    657     for (i = 0; i < sbrEnvData->hSbrBSGrid->n; i++) {
    658       temp = (sbrEnvData->hSbrBSGrid->bs_rel_bord[i] - 2) >> 1;
    659       payloadBits += FDKwriteBits (hBitStream, temp, SBR_REL_BITS);
    660     }
    661 
    662     temp = ceil_ln2(sbrEnvData->hSbrBSGrid->n + 2);
    663     payloadBits += FDKwriteBits (hBitStream, sbrEnvData->hSbrBSGrid->p, temp);
    664 
    665     for (i = 0; i < sbrEnvData->hSbrBSGrid->n + 1; i++) {
    666       payloadBits += FDKwriteBits (hBitStream, sbrEnvData->hSbrBSGrid->v_f[i],
    667                                 SBR_RES_BITS);
    668     }
    669     break;
    670 
    671   case VARVAR:
    672     temp = sbrEnvData->hSbrBSGrid->bs_abs_bord_0 - bufferFrameStart;
    673     payloadBits += FDKwriteBits (hBitStream, temp, SBR_ABS_BITS);
    674     temp = sbrEnvData->hSbrBSGrid->bs_abs_bord_1 - (bufferFrameStart + numberTimeSlots);
    675     payloadBits += FDKwriteBits (hBitStream, temp, SBR_ABS_BITS);
    676 
    677     payloadBits += FDKwriteBits (hBitStream, sbrEnvData->hSbrBSGrid->bs_num_rel_0, SBR_NUM_BITS);
    678     payloadBits += FDKwriteBits (hBitStream, sbrEnvData->hSbrBSGrid->bs_num_rel_1, SBR_NUM_BITS);
    679 
    680     for (i = 0; i < sbrEnvData->hSbrBSGrid->bs_num_rel_0; i++) {
    681       temp = (sbrEnvData->hSbrBSGrid->bs_rel_bord_0[i] - 2) >> 1;
    682       payloadBits += FDKwriteBits (hBitStream, temp, SBR_REL_BITS);
    683     }
    684 
    685     for (i = 0; i < sbrEnvData->hSbrBSGrid->bs_num_rel_1; i++) {
    686       temp = (sbrEnvData->hSbrBSGrid->bs_rel_bord_1[i] - 2) >> 1;
    687       payloadBits += FDKwriteBits (hBitStream, temp, SBR_REL_BITS);
    688     }
    689 
    690     temp = ceil_ln2(sbrEnvData->hSbrBSGrid->bs_num_rel_0 +
    691                              sbrEnvData->hSbrBSGrid->bs_num_rel_1 + 2);
    692     payloadBits +=  FDKwriteBits (hBitStream, sbrEnvData->hSbrBSGrid->p, temp);
    693 
    694     temp = sbrEnvData->hSbrBSGrid->bs_num_rel_0 +
    695            sbrEnvData->hSbrBSGrid->bs_num_rel_1 + 1;
    696 
    697     for (i = 0; i < temp; i++) {
    698       payloadBits += FDKwriteBits (hBitStream, sbrEnvData->hSbrBSGrid->v_fLR[i],
    699                                 SBR_RES_BITS);
    700     }
    701     break;
    702   }
    703 
    704   return payloadBits;
    705 }
    706 
    707 #define SBR_CLA_BITS_LD 1
    708 /*****************************************************************************
    709 
    710     functionname: encodeLowDelaySbrGrid
    711     description:  if hBitStream != NULL writes bits that describes the
    712                   time/frequency grouping of a frame;
    713                   else counts them only
    714                   (this function only write the FIXFIXonly Bitstream data)
    715     returns:      number of bits written or counted
    716     input:
    717     output:
    718 
    719 *****************************************************************************/
    720 static int
    721 encodeLowDelaySbrGrid (  HANDLE_SBR_ENV_DATA sbrEnvData,
    722                          HANDLE_FDK_BITSTREAM hBitStream,
    723                          int transmitFreqs
    724                        )
    725 {
    726   int payloadBits = 0;
    727   int i;
    728 
    729   /* write FIXFIXonly Grid */
    730   /* write frameClass [1 bit] for FIXFIXonly Grid */
    731   payloadBits += FDKwriteBits(hBitStream, 1, SBR_CLA_BITS_LD);
    732 
    733   /* absolute Borders are fix: 0,X,X,X,nTimeSlots; so we dont have to transmit them */
    734   /* only transmit the transient position! */
    735   /* with this info (b1) we can reconstruct the Frame on Decoder side : */
    736   /* border[0] = 0; border[1] = b1; border[2]=b1+2; border[3] = nrTimeSlots */
    737 
    738   /* use 3 or 4bits for transient border (border) */
    739   if (sbrEnvData->hSbrBSGrid->numberTimeSlots == 8)
    740     payloadBits += FDKwriteBits ( hBitStream, sbrEnvData->hSbrBSGrid->bs_abs_bord, 3);
    741   else
    742     payloadBits += FDKwriteBits ( hBitStream, sbrEnvData->hSbrBSGrid->bs_abs_bord, 4);
    743 
    744   if (transmitFreqs) {
    745     /* write FreqRes grid */
    746     for (i = 0; i < sbrEnvData->hSbrBSGrid->bs_num_env; i++) {
    747       payloadBits += FDKwriteBits (hBitStream, sbrEnvData->hSbrBSGrid->v_f[i], SBR_RES_BITS);
    748     }
    749   }
    750 
    751   return payloadBits;
    752 }
    753 
    754 /*****************************************************************************
    755 
    756     functionname: encodeSbrDtdf
    757     description:  writes bits that describes the direction of the envelopes of a frame
    758     returns:      number of bits written
    759     input:
    760     output:
    761 
    762 *****************************************************************************/
    763 static INT
    764 encodeSbrDtdf (HANDLE_SBR_ENV_DATA sbrEnvData, HANDLE_FDK_BITSTREAM hBitStream)
    765 {
    766   INT i, payloadBits = 0, noOfNoiseEnvelopes;
    767 
    768   noOfNoiseEnvelopes = sbrEnvData->noOfEnvelopes > 1 ? 2 : 1;
    769 
    770   for (i = 0; i < sbrEnvData->noOfEnvelopes; ++i) {
    771     payloadBits += FDKwriteBits (hBitStream, sbrEnvData->domain_vec[i], SBR_DIR_BITS);
    772   }
    773   for (i = 0; i < noOfNoiseEnvelopes; ++i) {
    774     payloadBits +=  FDKwriteBits (hBitStream, sbrEnvData->domain_vec_noise[i], SBR_DIR_BITS);
    775   }
    776 
    777   return payloadBits;
    778 }
    779 
    780 
    781 /*****************************************************************************
    782 
    783     functionname: writeNoiseLevelData
    784     description:  writes bits corresponding to the noise-floor-level
    785     returns:      number of bits written
    786     input:
    787     output:
    788 
    789 *****************************************************************************/
    790 static INT
    791 writeNoiseLevelData (HANDLE_SBR_ENV_DATA sbrEnvData, HANDLE_FDK_BITSTREAM hBitStream, INT coupling)
    792 {
    793   INT j, i, payloadBits = 0;
    794   INT nNoiseEnvelopes = sbrEnvData->noOfEnvelopes > 1 ? 2 : 1;
    795 
    796   for (i = 0; i < nNoiseEnvelopes; i++) {
    797     switch (sbrEnvData->domain_vec_noise[i]) {
    798     case FREQ:
    799       if (coupling && sbrEnvData->balance) {
    800         payloadBits += FDKwriteBits (hBitStream,
    801                                   sbrEnvData->sbr_noise_levels[i * sbrEnvData->noOfnoisebands],
    802                                   sbrEnvData->si_sbr_start_noise_bits_balance);
    803       } else {
    804         payloadBits += FDKwriteBits (hBitStream,
    805                                   sbrEnvData->sbr_noise_levels[i * sbrEnvData->noOfnoisebands],
    806                                   sbrEnvData->si_sbr_start_noise_bits);
    807       }
    808 
    809       for (j = 1 + i * sbrEnvData->noOfnoisebands; j < (sbrEnvData->noOfnoisebands * (1 + i)); j++) {
    810         if (coupling) {
    811           if (sbrEnvData->balance) {
    812             /* coupling && balance */
    813             payloadBits += FDKwriteBits (hBitStream,
    814                                       sbrEnvData->hufftableNoiseBalanceFreqC[sbrEnvData->sbr_noise_levels[j] +
    815                                                                             CODE_BOOK_SCF_LAV_BALANCE11],
    816                                       sbrEnvData->hufftableNoiseBalanceFreqL[sbrEnvData->sbr_noise_levels[j] +
    817                                                                             CODE_BOOK_SCF_LAV_BALANCE11]);
    818           } else {
    819             /* coupling && !balance */
    820             payloadBits += FDKwriteBits (hBitStream,
    821                                       sbrEnvData->hufftableNoiseLevelFreqC[sbrEnvData->sbr_noise_levels[j] +
    822                                                                           CODE_BOOK_SCF_LAV11],
    823                                       sbrEnvData->hufftableNoiseLevelFreqL[sbrEnvData->sbr_noise_levels[j] +
    824                                                                           CODE_BOOK_SCF_LAV11]);
    825           }
    826         } else {
    827           /* !coupling */
    828           payloadBits += FDKwriteBits (hBitStream,
    829                                     sbrEnvData->hufftableNoiseFreqC[sbrEnvData->sbr_noise_levels[j] +
    830                                                                    CODE_BOOK_SCF_LAV11],
    831                                     sbrEnvData->hufftableNoiseFreqL[sbrEnvData->sbr_noise_levels[j] +
    832                                                                    CODE_BOOK_SCF_LAV11]);
    833         }
    834       }
    835       break;
    836 
    837     case TIME:
    838       for (j = i * sbrEnvData->noOfnoisebands; j < (sbrEnvData->noOfnoisebands * (1 + i)); j++) {
    839         if (coupling) {
    840           if (sbrEnvData->balance) {
    841             /* coupling && balance */
    842             payloadBits += FDKwriteBits (hBitStream,
    843                                       sbrEnvData->hufftableNoiseBalanceTimeC[sbrEnvData->sbr_noise_levels[j] +
    844                                                                             CODE_BOOK_SCF_LAV_BALANCE11],
    845                                       sbrEnvData->hufftableNoiseBalanceTimeL[sbrEnvData->sbr_noise_levels[j] +
    846                                                                             CODE_BOOK_SCF_LAV_BALANCE11]);
    847           } else {
    848             /* coupling && !balance */
    849             payloadBits += FDKwriteBits (hBitStream,
    850                                       sbrEnvData->hufftableNoiseLevelTimeC[sbrEnvData->sbr_noise_levels[j] +
    851                                                                           CODE_BOOK_SCF_LAV11],
    852                                       sbrEnvData->hufftableNoiseLevelTimeL[sbrEnvData->sbr_noise_levels[j] +
    853                                                                           CODE_BOOK_SCF_LAV11]);
    854           }
    855         } else {
    856           /* !coupling */
    857           payloadBits += FDKwriteBits (hBitStream,
    858                                     sbrEnvData->hufftableNoiseLevelTimeC[sbrEnvData->sbr_noise_levels[j] +
    859                                                                         CODE_BOOK_SCF_LAV11],
    860                                     sbrEnvData->hufftableNoiseLevelTimeL[sbrEnvData->sbr_noise_levels[j] +
    861                                                                         CODE_BOOK_SCF_LAV11]);
    862         }
    863       }
    864       break;
    865     }
    866   }
    867   return payloadBits;
    868 }
    869 
    870 
    871 /*****************************************************************************
    872 
    873     functionname: writeEnvelopeData
    874     description:  writes bits corresponding to the envelope
    875     returns:      number of bits written
    876     input:
    877     output:
    878 
    879 *****************************************************************************/
    880 static INT
    881 writeEnvelopeData (HANDLE_SBR_ENV_DATA sbrEnvData, HANDLE_FDK_BITSTREAM hBitStream, INT coupling)
    882 {
    883   INT payloadBits = 0, j, i, delta;
    884 
    885   for (j = 0; j < sbrEnvData->noOfEnvelopes; j++) { /* loop over all envelopes */
    886     if (sbrEnvData->domain_vec[j] == FREQ) {
    887       if (coupling && sbrEnvData->balance) {
    888         payloadBits += FDKwriteBits (hBitStream, sbrEnvData->ienvelope[j][0], sbrEnvData->si_sbr_start_env_bits_balance);
    889       } else {
    890         payloadBits += FDKwriteBits (hBitStream, sbrEnvData->ienvelope[j][0], sbrEnvData->si_sbr_start_env_bits);
    891       }
    892     }
    893 
    894     for (i = 1 - sbrEnvData->domain_vec[j]; i < sbrEnvData->noScfBands[j]; i++) {
    895       delta = sbrEnvData->ienvelope[j][i];
    896       if (coupling && sbrEnvData->balance) {
    897         FDK_ASSERT (fixp_abs (delta) <= sbrEnvData->codeBookScfLavBalance);
    898       } else {
    899         FDK_ASSERT (fixp_abs (delta) <= sbrEnvData->codeBookScfLav);
    900       }
    901       if (coupling) {
    902         if (sbrEnvData->balance) {
    903           if (sbrEnvData->domain_vec[j]) {
    904             /* coupling && balance && TIME */
    905             payloadBits += FDKwriteBits (hBitStream,
    906                                       sbrEnvData->hufftableBalanceTimeC[delta + sbrEnvData->codeBookScfLavBalance],
    907                                       sbrEnvData->hufftableBalanceTimeL[delta + sbrEnvData->codeBookScfLavBalance]);
    908           } else {
    909             /* coupling && balance && FREQ */
    910             payloadBits += FDKwriteBits (hBitStream,
    911                                       sbrEnvData->hufftableBalanceFreqC[delta + sbrEnvData->codeBookScfLavBalance],
    912                                       sbrEnvData->hufftableBalanceFreqL[delta + sbrEnvData->codeBookScfLavBalance]);
    913           }
    914         } else {
    915           if (sbrEnvData->domain_vec[j]) {
    916             /* coupling && !balance && TIME */
    917             payloadBits += FDKwriteBits (hBitStream,
    918                                       sbrEnvData->hufftableLevelTimeC[delta + sbrEnvData->codeBookScfLav],
    919                                       sbrEnvData->hufftableLevelTimeL[delta + sbrEnvData->codeBookScfLav]);
    920           } else {
    921             /* coupling && !balance && FREQ */
    922             payloadBits += FDKwriteBits (hBitStream,
    923                                       sbrEnvData->hufftableLevelFreqC[delta + sbrEnvData->codeBookScfLav],
    924                                       sbrEnvData->hufftableLevelFreqL[delta + sbrEnvData->codeBookScfLav]);
    925           }
    926         }
    927       } else {
    928         if (sbrEnvData->domain_vec[j]) {
    929           /* !coupling && TIME */
    930           payloadBits += FDKwriteBits (hBitStream,
    931                                     sbrEnvData->hufftableTimeC[delta + sbrEnvData->codeBookScfLav],
    932                                     sbrEnvData->hufftableTimeL[delta + sbrEnvData->codeBookScfLav]);
    933         } else {
    934           /* !coupling && FREQ */
    935           payloadBits += FDKwriteBits (hBitStream,
    936                                     sbrEnvData->hufftableFreqC[delta + sbrEnvData->codeBookScfLav],
    937                                     sbrEnvData->hufftableFreqL[delta + sbrEnvData->codeBookScfLav]);
    938         }
    939       }
    940     }
    941   }
    942   return payloadBits;
    943 }
    944 
    945 
    946 /*****************************************************************************
    947 
    948     functionname: encodeExtendedData
    949     description:  writes bits corresponding to the extended data
    950     returns:      number of bits written
    951     input:
    952     output:
    953 
    954 *****************************************************************************/
    955 static INT encodeExtendedData (HANDLE_PARAMETRIC_STEREO  hParametricStereo,
    956                                HANDLE_FDK_BITSTREAM hBitStream)
    957 {
    958   INT extDataSize;
    959   INT payloadBits = 0;
    960 
    961   extDataSize = getSbrExtendedDataSize(hParametricStereo);
    962 
    963 
    964   if (extDataSize != 0) {
    965     INT maxExtSize = (1<<SI_SBR_EXTENSION_SIZE_BITS) - 1;
    966     INT writtenNoBits = 0; /* needed to byte align the extended data */
    967 
    968     payloadBits += FDKwriteBits (hBitStream, 1, SI_SBR_EXTENDED_DATA_BITS);
    969     FDK_ASSERT(extDataSize <= SBR_EXTENDED_DATA_MAX_CNT);
    970 
    971     if (extDataSize < maxExtSize) {
    972       payloadBits += FDKwriteBits (hBitStream, extDataSize, SI_SBR_EXTENSION_SIZE_BITS);
    973     } else {
    974       payloadBits += FDKwriteBits (hBitStream, maxExtSize, SI_SBR_EXTENSION_SIZE_BITS);
    975       payloadBits += FDKwriteBits (hBitStream, extDataSize - maxExtSize, SI_SBR_EXTENSION_ESC_COUNT_BITS);
    976     }
    977 
    978     /* parametric coding signalled here? */
    979     if(hParametricStereo){
    980       writtenNoBits += FDKwriteBits (hBitStream, EXTENSION_ID_PS_CODING, SI_SBR_EXTENSION_ID_BITS);
    981       writtenNoBits += FDKsbrEnc_PSEnc_WritePSData(hParametricStereo, hBitStream);
    982     }
    983 
    984     payloadBits += writtenNoBits;
    985 
    986     /* byte alignment */
    987     writtenNoBits = writtenNoBits%8;
    988     if(writtenNoBits)
    989       payloadBits += FDKwriteBits(hBitStream, 0, (8 - writtenNoBits));
    990   } else {
    991     payloadBits += FDKwriteBits (hBitStream, 0, SI_SBR_EXTENDED_DATA_BITS);
    992   }
    993 
    994   return payloadBits;
    995 }
    996 
    997 
    998 /*****************************************************************************
    999 
   1000     functionname: writeSyntheticCodingData
   1001     description:  writes bits corresponding to the "synthetic-coding"-extension
   1002     returns:      number of bits written
   1003     input:
   1004     output:
   1005 
   1006 *****************************************************************************/
   1007 static INT writeSyntheticCodingData (HANDLE_SBR_ENV_DATA  sbrEnvData,
   1008                                      HANDLE_FDK_BITSTREAM hBitStream)
   1009 
   1010 {
   1011   INT i;
   1012   INT payloadBits = 0;
   1013 
   1014   payloadBits += FDKwriteBits (hBitStream, sbrEnvData->addHarmonicFlag, 1);
   1015 
   1016   if (sbrEnvData->addHarmonicFlag) {
   1017     for (i = 0; i < sbrEnvData->noHarmonics; i++) {
   1018       payloadBits += FDKwriteBits (hBitStream, sbrEnvData->addHarmonic[i], 1);
   1019     }
   1020   }
   1021 
   1022   return payloadBits;
   1023 }
   1024 
   1025 /*****************************************************************************
   1026 
   1027     functionname: getSbrExtendedDataSize
   1028     description:  counts the number of bits needed for encoding the
   1029                   extended data (including extension id)
   1030 
   1031     returns:      number of bits needed for the extended data
   1032     input:
   1033     output:
   1034 
   1035 *****************************************************************************/
   1036 static INT
   1037 getSbrExtendedDataSize (HANDLE_PARAMETRIC_STEREO  hParametricStereo)
   1038 {
   1039   INT extDataBits = 0;
   1040 
   1041   /* add your new extended data counting methods here */
   1042 
   1043   /*
   1044     no extended data
   1045   */
   1046 
   1047   if(hParametricStereo){
   1048     /* PS extended data */
   1049     extDataBits += SI_SBR_EXTENSION_ID_BITS;
   1050     extDataBits += FDKsbrEnc_PSEnc_WritePSData(hParametricStereo, NULL);
   1051   }
   1052 
   1053   return (extDataBits+7) >> 3;
   1054 }
   1055 
   1056 
   1057 
   1058 
   1059 
   1060