Home | History | Annotate | Download | only in include
      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 /******************* Library for basic calculation routines ********************
     96 
     97    Author(s):   M. Lohwasser
     98 
     99    Description: bitstream interface to bitbuffer routines
    100 
    101 *******************************************************************************/
    102 
    103 #ifndef FDK_BITSTREAM_H
    104 #define FDK_BITSTREAM_H
    105 
    106 #include "FDK_bitbuffer.h"
    107 #include "machine_type.h"
    108 
    109 #include "genericStds.h"
    110 
    111 #define CACHE_BITS 32
    112 
    113 #define BUFSIZE_DUMMY_VALUE MAX_BUFSIZE_BYTES
    114 
    115 typedef enum { BS_READER, BS_WRITER } FDK_BS_CFG;
    116 
    117 typedef struct {
    118   UINT CacheWord;
    119   UINT BitsInCache;
    120   FDK_BITBUF hBitBuf;
    121   UINT ConfigCache;
    122 } FDK_BITSTREAM;
    123 
    124 typedef FDK_BITSTREAM *HANDLE_FDK_BITSTREAM;
    125 
    126 /**
    127  * \brief CreateBitStream Function.
    128  *
    129  * Create and initialize bitstream with extern allocated buffer.
    130  *
    131  * \param pBuffer  Pointer to BitBuffer array.
    132  * \param bufSize  Length of BitBuffer array. (awaits size 2^n and <=
    133  * MAX_BUFSIZE_BYTES)
    134  * \param config   Initialize BitStream as Reader or Writer.
    135  */
    136 FDK_INLINE
    137 HANDLE_FDK_BITSTREAM FDKcreateBitStream(UCHAR *pBuffer, UINT bufSize,
    138                                         FDK_BS_CFG config = BS_READER) {
    139   HANDLE_FDK_BITSTREAM hBitStream =
    140       (HANDLE_FDK_BITSTREAM)FDKcalloc(1, sizeof(FDK_BITSTREAM));
    141   if (hBitStream == NULL) return NULL;
    142   FDK_InitBitBuffer(&hBitStream->hBitBuf, pBuffer, bufSize, 0);
    143 
    144   /* init cache */
    145   hBitStream->CacheWord = hBitStream->BitsInCache = 0;
    146   hBitStream->ConfigCache = config;
    147 
    148   return hBitStream;
    149 }
    150 
    151 /**
    152  * \brief Initialize BistreamBuffer. BitBuffer can point to filled BitBuffer
    153  * array .
    154  *
    155  * \param hBitStream HANDLE_FDK_BITSTREAM handle
    156  * \param pBuffer    Pointer to BitBuffer array.
    157  * \param bufSize    Length of BitBuffer array in bytes. (awaits size 2^n and <=
    158  * MAX_BUFSIZE_BYTES)
    159  * \param validBits  Number of valid BitBuffer filled Bits.
    160  * \param config     Initialize BitStream as Reader or Writer.
    161  * \return void
    162  */
    163 FDK_INLINE
    164 void FDKinitBitStream(HANDLE_FDK_BITSTREAM hBitStream, UCHAR *pBuffer,
    165                       UINT bufSize, UINT validBits,
    166                       FDK_BS_CFG config = BS_READER) {
    167   FDK_InitBitBuffer(&hBitStream->hBitBuf, pBuffer, bufSize, validBits);
    168 
    169   /* init cache */
    170   hBitStream->CacheWord = hBitStream->BitsInCache = 0;
    171   hBitStream->ConfigCache = config;
    172 }
    173 
    174 /**
    175  * \brief ResetBitbuffer Function. Reset states in BitBuffer and Cache.
    176  *
    177  * \param hBitStream HANDLE_FDK_BITSTREAM handle
    178  * \param config     Initialize BitStream as Reader or Writer.
    179  * \return void
    180  */
    181 FDK_INLINE void FDKresetBitbuffer(HANDLE_FDK_BITSTREAM hBitStream,
    182                                   FDK_BS_CFG config = BS_READER) {
    183   FDK_ResetBitBuffer(&hBitStream->hBitBuf);
    184 
    185   /* init cache */
    186   hBitStream->CacheWord = hBitStream->BitsInCache = 0;
    187   hBitStream->ConfigCache = config;
    188 }
    189 
    190 /** DeleteBitStream.
    191 
    192     Deletes the in Create Bitstream allocated BitStream and BitBuffer.
    193 */
    194 FDK_INLINE void FDKdeleteBitStream(HANDLE_FDK_BITSTREAM hBitStream) {
    195   FDK_DeleteBitBuffer(&hBitStream->hBitBuf);
    196   FDKfree(hBitStream);
    197 }
    198 
    199 /**
    200  * \brief ReadBits Function (forward). This function returns a number of
    201  * sequential bits from the input bitstream.
    202  *
    203  * \param hBitStream HANDLE_FDK_BITSTREAM handle
    204  * \param numberOfBits  The number of bits to be retrieved. ( (0),1 <=
    205  * numberOfBits <= 32)
    206  * \return the requested bits, right aligned
    207  * \return
    208  */
    209 
    210 FDK_INLINE UINT FDKreadBits(HANDLE_FDK_BITSTREAM hBitStream,
    211                             const UINT numberOfBits) {
    212   UINT bits = 0;
    213   INT missingBits = (INT)numberOfBits - (INT)hBitStream->BitsInCache;
    214 
    215   FDK_ASSERT(numberOfBits <= 32);
    216   if (missingBits > 0) {
    217     if (missingBits != 32) bits = hBitStream->CacheWord << missingBits;
    218     hBitStream->CacheWord = FDK_get32(&hBitStream->hBitBuf);
    219     hBitStream->BitsInCache += CACHE_BITS;
    220   }
    221 
    222   hBitStream->BitsInCache -= numberOfBits;
    223 
    224   return (bits | (hBitStream->CacheWord >> hBitStream->BitsInCache)) &
    225          BitMask[numberOfBits];
    226 }
    227 
    228 FDK_INLINE UINT FDKreadBit(HANDLE_FDK_BITSTREAM hBitStream) {
    229   if (!hBitStream->BitsInCache) {
    230     hBitStream->CacheWord = FDK_get32(&hBitStream->hBitBuf);
    231     hBitStream->BitsInCache = CACHE_BITS - 1;
    232     return hBitStream->CacheWord >> 31;
    233   }
    234   hBitStream->BitsInCache--;
    235 
    236   return (hBitStream->CacheWord >> hBitStream->BitsInCache) & 1;
    237 }
    238 
    239 /**
    240  * \brief Read2Bits Function (forward). This function reads 2 sequential
    241  *        bits from the input bitstream. It is the optimized version
    242           of FDKreadBits() for reading 2 bits.
    243  *
    244  * \param hBitStream HANDLE_FDK_BITSTREAM handle
    245  * \return the requested bits, right aligned
    246  * \return
    247  */
    248 FDK_INLINE UINT FDKread2Bits(HANDLE_FDK_BITSTREAM hBitStream) {
    249   /*
    250   ** Version corresponds to optimized FDKreadBits implementation
    251   ** calling FDK_get32, that keeps read pointer aligned.
    252   */
    253   UINT bits = 0;
    254   INT missingBits = 2 - (INT)hBitStream->BitsInCache;
    255   if (missingBits > 0) {
    256     bits = hBitStream->CacheWord << missingBits;
    257     hBitStream->CacheWord = FDK_get32(&hBitStream->hBitBuf);
    258     hBitStream->BitsInCache += CACHE_BITS;
    259   }
    260 
    261   hBitStream->BitsInCache -= 2;
    262 
    263   return (bits | (hBitStream->CacheWord >> hBitStream->BitsInCache)) & 0x3;
    264 }
    265 
    266 /**
    267  * \brief ReadBits Function (backward). This function returns a number of
    268  * sequential bits from the input bitstream.
    269  *
    270  * \param hBitStream HANDLE_FDK_BITSTREAM handle
    271  * \param numberOfBits  The number of bits to be retrieved.
    272  * \return the requested bits, right aligned
    273  */
    274 FDK_INLINE UINT FDKreadBitsBwd(HANDLE_FDK_BITSTREAM hBitStream,
    275                                const UINT numberOfBits) {
    276   const UINT validMask = BitMask[numberOfBits];
    277 
    278   if (hBitStream->BitsInCache <= numberOfBits) {
    279     const INT freeBits = (CACHE_BITS - 1) - hBitStream->BitsInCache;
    280 
    281     hBitStream->CacheWord = (hBitStream->CacheWord << freeBits) |
    282                             FDK_getBwd(&hBitStream->hBitBuf, freeBits);
    283     hBitStream->BitsInCache += freeBits;
    284   }
    285 
    286   hBitStream->BitsInCache -= numberOfBits;
    287 
    288   return (hBitStream->CacheWord >> hBitStream->BitsInCache) & validMask;
    289 }
    290 
    291 /**
    292  * \brief read an integer value using a varying number of bits from the
    293  * bitstream
    294  *
    295  *        q.v. ISO/IEC FDIS 23003-3  Table 16
    296  *
    297  * \param hBitStream HANDLE_FDK_BITSTREAM handle
    298  * \param nBits1  number of bits to read for a small integer value or escape
    299  * value
    300  * \param nBits2  number of bits to read for a medium sized integer value or
    301  * escape value
    302  * \param nBits3  number of bits to read for a large integer value
    303  * \return integer value read from bitstream
    304  */
    305 FDK_INLINE UINT escapedValue(HANDLE_FDK_BITSTREAM hBitStream, int nBits1,
    306                              int nBits2, int nBits3) {
    307   UINT value = FDKreadBits(hBitStream, nBits1);
    308 
    309   if (value == (UINT)(1 << nBits1) - 1) {
    310     UINT valueAdd = FDKreadBits(hBitStream, nBits2);
    311     value += valueAdd;
    312     if (valueAdd == (UINT)(1 << nBits2) - 1) {
    313       value += FDKreadBits(hBitStream, nBits3);
    314     }
    315   }
    316 
    317   return value;
    318 }
    319 
    320 /**
    321  * \brief return a number of bits from the bitBuffer.
    322  *        You have to know what you do! Cache has to be synchronized before
    323  * using this function.
    324  *
    325  * \param hBitStream HANDLE_FDK_BITSTREAM handle
    326  * \param numBits The number of bits to be retrieved.
    327  * \return the requested bits, right aligned
    328  */
    329 FDK_INLINE UINT FDKgetBits(HANDLE_FDK_BITSTREAM hBitStream, UINT numBits) {
    330   return FDK_get(&hBitStream->hBitBuf, numBits);
    331 }
    332 
    333 /**
    334  * \brief WriteBits Function. This function writes numberOfBits of value into
    335  * bitstream.
    336  *
    337  * \param hBitStream HANDLE_FDK_BITSTREAM handle
    338  * \param value         The data to be written
    339  * \param numberOfBits  The number of bits to be written
    340  * \return              Number of bits written
    341  */
    342 FDK_INLINE UCHAR FDKwriteBits(HANDLE_FDK_BITSTREAM hBitStream, UINT value,
    343                               const UINT numberOfBits) {
    344   const UINT validMask = BitMask[numberOfBits];
    345 
    346   if (hBitStream == NULL) {
    347     return numberOfBits;
    348   }
    349 
    350   if ((hBitStream->BitsInCache + numberOfBits) < CACHE_BITS) {
    351     hBitStream->BitsInCache += numberOfBits;
    352     hBitStream->CacheWord =
    353         (hBitStream->CacheWord << numberOfBits) | (value & validMask);
    354   } else {
    355     /* Put always 32 bits into memory             */
    356     /* - fill cache's LSBits with MSBits of value */
    357     /* - store 32 bits in memory using subroutine */
    358     /* - fill remaining bits into cache's LSBits  */
    359     /* - upper bits in cache are don't care       */
    360 
    361     /* Compute number of bits to be filled into cache */
    362     int missing_bits = CACHE_BITS - hBitStream->BitsInCache;
    363     int remaining_bits = numberOfBits - missing_bits;
    364     value = value & validMask;
    365     /* Avoid shift left by 32 positions */
    366     UINT CacheWord =
    367         (missing_bits == 32) ? 0 : (hBitStream->CacheWord << missing_bits);
    368     CacheWord |= (value >> (remaining_bits));
    369     FDK_put(&hBitStream->hBitBuf, CacheWord, 32);
    370 
    371     hBitStream->CacheWord = value;
    372     hBitStream->BitsInCache = remaining_bits;
    373   }
    374 
    375   return numberOfBits;
    376 }
    377 
    378 /**
    379  * \brief WriteBits Function (backward). This function writes numberOfBits of
    380  * value into bitstream.
    381  *
    382  * \param hBitStream HANDLE_FDK_BITSTREAM handle
    383  * \param value         Variable holds data to be written.
    384  * \param numberOfBits  The number of bits to be written.
    385  * \return number of bits written
    386  */
    387 FDK_INLINE UCHAR FDKwriteBitsBwd(HANDLE_FDK_BITSTREAM hBitStream, UINT value,
    388                                  const UINT numberOfBits) {
    389   const UINT validMask = BitMask[numberOfBits];
    390 
    391   if ((hBitStream->BitsInCache + numberOfBits) <= CACHE_BITS) {
    392     hBitStream->BitsInCache += numberOfBits;
    393     hBitStream->CacheWord =
    394         (hBitStream->CacheWord << numberOfBits) | (value & validMask);
    395   } else {
    396     FDK_putBwd(&hBitStream->hBitBuf, hBitStream->CacheWord,
    397                hBitStream->BitsInCache);
    398     hBitStream->BitsInCache = numberOfBits;
    399     hBitStream->CacheWord = (value & validMask);
    400   }
    401 
    402   return numberOfBits;
    403 }
    404 
    405 /**
    406  * \brief write an integer value using a varying number of bits from the
    407  * bitstream
    408  *
    409  *        q.v. ISO/IEC FDIS 23003-3  Table 16
    410  *
    411  * \param hBitStream HANDLE_FDK_BITSTREAM handle
    412  * \param value   the data to be written
    413  * \param nBits1  number of bits to write for a small integer value or escape
    414  * value
    415  * \param nBits2  number of bits to write for a medium sized integer value or
    416  * escape value
    417  * \param nBits3  number of bits to write for a large integer value
    418  * \return number of bits written
    419  */
    420 FDK_INLINE UCHAR FDKwriteEscapedValue(HANDLE_FDK_BITSTREAM hBitStream,
    421                                       UINT value, UINT nBits1, UINT nBits2,
    422                                       UINT nBits3) {
    423   UCHAR nbits = 0;
    424   UINT tmp = (1 << nBits1) - 1;
    425 
    426   if (value < tmp) {
    427     nbits += FDKwriteBits(hBitStream, value, nBits1);
    428   } else {
    429     nbits += FDKwriteBits(hBitStream, tmp, nBits1);
    430     value -= tmp;
    431     tmp = (1 << nBits2) - 1;
    432 
    433     if (value < tmp) {
    434       nbits += FDKwriteBits(hBitStream, value, nBits2);
    435     } else {
    436       nbits += FDKwriteBits(hBitStream, tmp, nBits2);
    437       value -= tmp;
    438 
    439       nbits += FDKwriteBits(hBitStream, value, nBits3);
    440     }
    441   }
    442 
    443   return nbits;
    444 }
    445 
    446 /**
    447  * \brief SyncCache Function. Clear cache after read forward.
    448  *
    449  * \param hBitStream HANDLE_FDK_BITSTREAM handle
    450  * \return void
    451  */
    452 FDK_INLINE void FDKsyncCache(HANDLE_FDK_BITSTREAM hBitStream) {
    453   if (hBitStream->ConfigCache == BS_READER)
    454     FDK_pushBack(&hBitStream->hBitBuf, hBitStream->BitsInCache,
    455                  hBitStream->ConfigCache);
    456   else if (hBitStream->BitsInCache) /* BS_WRITER */
    457     FDK_put(&hBitStream->hBitBuf, hBitStream->CacheWord,
    458             hBitStream->BitsInCache);
    459 
    460   hBitStream->BitsInCache = 0;
    461   hBitStream->CacheWord = 0;
    462 }
    463 
    464 /**
    465  * \brief SyncCache Function. Clear cache after read backwards.
    466  *
    467  * \param  hBitStream HANDLE_FDK_BITSTREAM handle
    468  * \return void
    469  */
    470 FDK_INLINE void FDKsyncCacheBwd(HANDLE_FDK_BITSTREAM hBitStream) {
    471   if (hBitStream->ConfigCache == BS_READER) {
    472     FDK_pushForward(&hBitStream->hBitBuf, hBitStream->BitsInCache,
    473                     hBitStream->ConfigCache);
    474   } else { /* BS_WRITER */
    475     FDK_putBwd(&hBitStream->hBitBuf, hBitStream->CacheWord,
    476                hBitStream->BitsInCache);
    477   }
    478 
    479   hBitStream->BitsInCache = 0;
    480   hBitStream->CacheWord = 0;
    481 }
    482 
    483 /**
    484  * \brief Byte Alignment Function.
    485  *        This function performs the byte_alignment() syntactic function on the
    486  * input stream, i.e. some bits will be discarded/padded so that the next bits
    487  * to be read/written will be aligned on a byte boundary with respect to
    488  * the bit position 0.
    489  *
    490  * \param  hBitStream HANDLE_FDK_BITSTREAM handle
    491  * \return void
    492  */
    493 FDK_INLINE void FDKbyteAlign(HANDLE_FDK_BITSTREAM hBitStream) {
    494   FDKsyncCache(hBitStream);
    495   FDK_byteAlign(&hBitStream->hBitBuf, (UCHAR)hBitStream->ConfigCache);
    496 }
    497 
    498 /**
    499  * \brief Byte Alignment Function with anchor
    500  *        This function performs the byte_alignment() syntactic function on the
    501  * input stream, i.e. some bits will be discarded so that the next bits to be
    502  * read/written would be aligned on a byte boundary with respect to the
    503  * given alignment anchor.
    504  *
    505  * \param hBitStream HANDLE_FDK_BITSTREAM handle
    506  * \param alignmentAnchor bit position to be considered as origin for byte
    507  * alignment
    508  * \return void
    509  */
    510 FDK_INLINE void FDKbyteAlign(HANDLE_FDK_BITSTREAM hBitStream,
    511                              UINT alignmentAnchor) {
    512   FDKsyncCache(hBitStream);
    513   if (hBitStream->ConfigCache == BS_READER) {
    514     FDK_pushForward(
    515         &hBitStream->hBitBuf,
    516         (UINT)((INT)8 - (((INT)alignmentAnchor -
    517                           (INT)FDK_getValidBits(&hBitStream->hBitBuf)) &
    518                          0x07)) &
    519             0x07,
    520         hBitStream->ConfigCache);
    521   } else {
    522     FDK_put(&hBitStream->hBitBuf, 0,
    523             (8 - ((FDK_getValidBits(&hBitStream->hBitBuf) - alignmentAnchor) &
    524                   0x07)) &
    525                 0x07);
    526   }
    527 }
    528 
    529 /**
    530  * \brief Push Back(Cache) / For / BiDirectional Function.
    531  *        PushBackCache function ungets a number of bits erroneously
    532  * read/written by the last Get() call. NB: The number of bits to be stuffed
    533  * back into the stream may never exceed the number of bits returned by
    534  * the immediately preceding Get() call.
    535  *
    536  *       PushBack function ungets a number of bits (combines cache and bitbuffer
    537  * indices) PushFor  function gets a number of bits (combines cache and
    538  * bitbuffer indices) PushBiDirectional gets/ungets number of bits as
    539  * defined in PusBack/For function NB: The sign of bits is not known, so
    540  * the function checks direction and calls appropriate function. (positive
    541  * sign pushFor, negative sign pushBack )
    542  *
    543  * \param hBitStream HANDLE_FDK_BITSTREAM handle
    544  * \param numberOfBits  The number of bits to be pushed back/for.
    545  * \return void
    546  */
    547 FDK_INLINE void FDKpushBackCache(HANDLE_FDK_BITSTREAM hBitStream,
    548                                  const UINT numberOfBits) {
    549   FDK_ASSERT((hBitStream->BitsInCache + numberOfBits) <= CACHE_BITS);
    550   hBitStream->BitsInCache += numberOfBits;
    551 }
    552 
    553 FDK_INLINE void FDKpushBack(HANDLE_FDK_BITSTREAM hBitStream,
    554                             const UINT numberOfBits) {
    555   if ((hBitStream->BitsInCache + numberOfBits) < CACHE_BITS &&
    556       (hBitStream->ConfigCache == BS_READER)) {
    557     hBitStream->BitsInCache += numberOfBits;
    558     FDKsyncCache(hBitStream); /* sync cache to avoid invalid cache */
    559   } else {
    560     FDKsyncCache(hBitStream);
    561     FDK_pushBack(&hBitStream->hBitBuf, numberOfBits, hBitStream->ConfigCache);
    562   }
    563 }
    564 
    565 FDK_INLINE void FDKpushFor(HANDLE_FDK_BITSTREAM hBitStream,
    566                            const UINT numberOfBits) {
    567   if ((hBitStream->BitsInCache > numberOfBits) &&
    568       (hBitStream->ConfigCache == BS_READER)) {
    569     hBitStream->BitsInCache -= numberOfBits;
    570   } else {
    571     FDKsyncCache(hBitStream);
    572     FDK_pushForward(&hBitStream->hBitBuf, numberOfBits,
    573                     hBitStream->ConfigCache);
    574   }
    575 }
    576 
    577 FDK_INLINE void FDKpushBiDirectional(HANDLE_FDK_BITSTREAM hBitStream,
    578                                      const INT numberOfBits) {
    579   if (numberOfBits >= 0)
    580     FDKpushFor(hBitStream, numberOfBits);
    581   else
    582     FDKpushBack(hBitStream, -numberOfBits);
    583 }
    584 
    585 /**
    586  * \brief GetValidBits Function.  Clear cache and return valid Bits from
    587  * Bitbuffer.
    588  * \param hBitStream HANDLE_FDK_BITSTREAM handle
    589  * \return amount of valid bits that still can be read or were already written.
    590  *
    591  */
    592 FDK_INLINE UINT FDKgetValidBits(HANDLE_FDK_BITSTREAM hBitStream) {
    593   FDKsyncCache(hBitStream);
    594   return FDK_getValidBits(&hBitStream->hBitBuf);
    595 }
    596 
    597 /**
    598  * \brief return amount of unused Bits from Bitbuffer.
    599  * \param hBitStream HANDLE_FDK_BITSTREAM handle
    600  * \return amount of free bits that still can be written into the bitstream
    601  */
    602 FDK_INLINE INT FDKgetFreeBits(HANDLE_FDK_BITSTREAM hBitStream) {
    603   return FDK_getFreeBits(&hBitStream->hBitBuf);
    604 }
    605 
    606 /**
    607  * \brief reset bitcounter in bitBuffer to zero.
    608  * \param hBitStream HANDLE_FDK_BITSTREAM handle
    609  * \return void
    610  */
    611 FDK_INLINE void FDKresetBitCnt(HANDLE_FDK_BITSTREAM hBitStream) {
    612   FDKsyncCache(hBitStream);
    613   FDK_setBitCnt(&hBitStream->hBitBuf, 0);
    614 }
    615 
    616 /**
    617  * \brief set bitcoutner in bitBuffer to given value.
    618  * \param hBitStream HANDLE_FDK_BITSTREAM handle
    619  * \param value new value to be assigned to the bit counter
    620  * \return void
    621  */
    622 FDK_INLINE void FDKsetBitCnt(HANDLE_FDK_BITSTREAM hBitStream, UINT value) {
    623   FDKsyncCache(hBitStream);
    624   FDK_setBitCnt(&hBitStream->hBitBuf, value);
    625 }
    626 
    627 /**
    628  * \brief get bitcounter state from bitBuffer.
    629  * \param hBitStream HANDLE_FDK_BITSTREAM handle
    630  * \return current bit counter value
    631  */
    632 FDK_INLINE INT FDKgetBitCnt(HANDLE_FDK_BITSTREAM hBitStream) {
    633   FDKsyncCache(hBitStream);
    634   return FDK_getBitCnt(&hBitStream->hBitBuf);
    635 }
    636 
    637 /**
    638  * \brief Fill the BitBuffer with a number of input bytes from  external source.
    639  *        The bytesValid variable returns the number of ramaining valid bytes in
    640  * extern inputBuffer.
    641  *
    642  * \param hBitStream  HANDLE_FDK_BITSTREAM handle
    643  * \param inputBuffer Pointer to input buffer with bitstream data.
    644  * \param bufferSize  Total size of inputBuffer array.
    645  * \param bytesValid  Input: number of valid bytes in inputBuffer. Output: bytes
    646  * still left unread in inputBuffer.
    647  * \return void
    648  */
    649 FDK_INLINE void FDKfeedBuffer(HANDLE_FDK_BITSTREAM hBitStream,
    650                               const UCHAR inputBuffer[], const UINT bufferSize,
    651                               UINT *bytesValid) {
    652   FDKsyncCache(hBitStream);
    653   FDK_Feed(&hBitStream->hBitBuf, inputBuffer, bufferSize, bytesValid);
    654 }
    655 
    656 /**
    657  * \brief fill destination BitBuffer with a number of bytes from source
    658  * BitBuffer. The bytesValid variable returns the number of ramaining valid
    659  * bytes in source BitBuffer.
    660  *
    661  * \param hBSDst            HANDLE_FDK_BITSTREAM handle to write data into
    662  * \param hBSSrc            HANDLE_FDK_BITSTREAM handle to read data from
    663  * \param bytesValid        Input: number of valid bytes in inputBuffer. Output:
    664  * bytes still left unread in inputBuffer.
    665  * \return void
    666  */
    667 FDK_INLINE void FDKcopyBuffer(HANDLE_FDK_BITSTREAM hBSDst,
    668                               HANDLE_FDK_BITSTREAM hBSSrc, UINT *bytesValid) {
    669   FDKsyncCache(hBSSrc);
    670   FDK_Copy(&hBSDst->hBitBuf, &hBSSrc->hBitBuf, bytesValid);
    671 }
    672 
    673 /**
    674  * \brief fill the outputBuffer with all valid bytes hold in BitBuffer. The
    675  * WriteBytes variable returns the number of written Bytes.
    676  *
    677  * \param hBitStream    HANDLE_FDK_BITSTREAM handle
    678  * \param outputBuffer  Pointer to output buffer.
    679  * \param writeBytes    Number of bytes write to output buffer.
    680  * \return void
    681  */
    682 FDK_INLINE void FDKfetchBuffer(HANDLE_FDK_BITSTREAM hBitStream,
    683                                UCHAR *outputBuffer, UINT *writeBytes) {
    684   FDKsyncCache(hBitStream);
    685   FDK_Fetch(&hBitStream->hBitBuf, outputBuffer, writeBytes);
    686 }
    687 
    688 #endif
    689