• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* -----------------------------------------------------------------------------
2 Software License for The Fraunhofer FDK AAC Codec Library for Android
3 
4 © Copyright  1995 - 2018 Fraunhofer-Gesellschaft zur Förderung 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@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 */
FDKdeleteBitStream(HANDLE_FDK_BITSTREAM hBitStream)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 
FDKreadBits(HANDLE_FDK_BITSTREAM hBitStream,const UINT numberOfBits)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 
FDKreadBit(HANDLE_FDK_BITSTREAM hBitStream)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  */
FDKread2Bits(HANDLE_FDK_BITSTREAM hBitStream)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  */
FDKreadBitsBwd(HANDLE_FDK_BITSTREAM hBitStream,const UINT numberOfBits)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  */
escapedValue(HANDLE_FDK_BITSTREAM hBitStream,int nBits1,int nBits2,int nBits3)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  */
FDKgetBits(HANDLE_FDK_BITSTREAM hBitStream,UINT numBits)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  */
FDKwriteBits(HANDLE_FDK_BITSTREAM hBitStream,UINT value,const UINT numberOfBits)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  */
FDKwriteBitsBwd(HANDLE_FDK_BITSTREAM hBitStream,UINT value,const UINT numberOfBits)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  */
FDKwriteEscapedValue(HANDLE_FDK_BITSTREAM hBitStream,UINT value,UINT nBits1,UINT nBits2,UINT nBits3)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  */
FDKsyncCache(HANDLE_FDK_BITSTREAM hBitStream)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  */
FDKsyncCacheBwd(HANDLE_FDK_BITSTREAM hBitStream)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 with anchor
485  *        This function performs the byte_alignment() syntactic function on the
486  * input stream, i.e. some bits will be discarded so that the next bits to be
487  * read/written would be aligned on a byte boundary with respect to the
488  * given alignment anchor.
489  *
490  * \param hBitStream HANDLE_FDK_BITSTREAM handle
491  * \param alignmentAnchor bit position to be considered as origin for byte
492  * alignment
493  * \return void
494  */
FDKbyteAlign(HANDLE_FDK_BITSTREAM hBitStream,UINT alignmentAnchor)495 FDK_INLINE void FDKbyteAlign(HANDLE_FDK_BITSTREAM hBitStream,
496                              UINT alignmentAnchor) {
497   FDKsyncCache(hBitStream);
498   if (hBitStream->ConfigCache == BS_READER) {
499     FDK_pushForward(
500         &hBitStream->hBitBuf,
501         (UINT)((INT)8 - (((INT)alignmentAnchor -
502                           (INT)FDK_getValidBits(&hBitStream->hBitBuf)) &
503                          0x07)) &
504             0x07,
505         hBitStream->ConfigCache);
506   } else {
507     FDK_put(&hBitStream->hBitBuf, 0,
508             (8 - ((FDK_getValidBits(&hBitStream->hBitBuf) - alignmentAnchor) &
509                   0x07)) &
510                 0x07);
511   }
512 }
513 
514 /**
515  * \brief Push Back(Cache) / For / BiDirectional Function.
516  *        PushBackCache function ungets a number of bits erroneously
517  * read/written by the last Get() call. NB: The number of bits to be stuffed
518  * back into the stream may never exceed the number of bits returned by
519  * the immediately preceding Get() call.
520  *
521  *       PushBack function ungets a number of bits (combines cache and bitbuffer
522  * indices) PushFor  function gets a number of bits (combines cache and
523  * bitbuffer indices) PushBiDirectional gets/ungets number of bits as
524  * defined in PusBack/For function NB: The sign of bits is not known, so
525  * the function checks direction and calls appropriate function. (positive
526  * sign pushFor, negative sign pushBack )
527  *
528  * \param hBitStream HANDLE_FDK_BITSTREAM handle
529  * \param numberOfBits  The number of bits to be pushed back/for.
530  * \return void
531  */
FDKpushBackCache(HANDLE_FDK_BITSTREAM hBitStream,const UINT numberOfBits)532 FDK_INLINE void FDKpushBackCache(HANDLE_FDK_BITSTREAM hBitStream,
533                                  const UINT numberOfBits) {
534   FDK_ASSERT((hBitStream->BitsInCache + numberOfBits) <= CACHE_BITS);
535   hBitStream->BitsInCache += numberOfBits;
536 }
537 
FDKpushBack(HANDLE_FDK_BITSTREAM hBitStream,const UINT numberOfBits)538 FDK_INLINE void FDKpushBack(HANDLE_FDK_BITSTREAM hBitStream,
539                             const UINT numberOfBits) {
540   if ((hBitStream->BitsInCache + numberOfBits) < CACHE_BITS &&
541       (hBitStream->ConfigCache == BS_READER)) {
542     hBitStream->BitsInCache += numberOfBits;
543     FDKsyncCache(hBitStream); /* sync cache to avoid invalid cache */
544   } else {
545     FDKsyncCache(hBitStream);
546     FDK_pushBack(&hBitStream->hBitBuf, numberOfBits, hBitStream->ConfigCache);
547   }
548 }
549 
FDKpushFor(HANDLE_FDK_BITSTREAM hBitStream,const UINT numberOfBits)550 FDK_INLINE void FDKpushFor(HANDLE_FDK_BITSTREAM hBitStream,
551                            const UINT numberOfBits) {
552   if ((hBitStream->BitsInCache > numberOfBits) &&
553       (hBitStream->ConfigCache == BS_READER)) {
554     hBitStream->BitsInCache -= numberOfBits;
555   } else {
556     FDKsyncCache(hBitStream);
557     FDK_pushForward(&hBitStream->hBitBuf, numberOfBits,
558                     hBitStream->ConfigCache);
559   }
560 }
561 
FDKpushBiDirectional(HANDLE_FDK_BITSTREAM hBitStream,const INT numberOfBits)562 FDK_INLINE void FDKpushBiDirectional(HANDLE_FDK_BITSTREAM hBitStream,
563                                      const INT numberOfBits) {
564   if (numberOfBits >= 0)
565     FDKpushFor(hBitStream, numberOfBits);
566   else
567     FDKpushBack(hBitStream, -numberOfBits);
568 }
569 
570 /**
571  * \brief GetValidBits Function.  Clear cache and return valid Bits from
572  * Bitbuffer.
573  * \param hBitStream HANDLE_FDK_BITSTREAM handle
574  * \return amount of valid bits that still can be read or were already written.
575  *
576  */
FDKgetValidBits(HANDLE_FDK_BITSTREAM hBitStream)577 FDK_INLINE UINT FDKgetValidBits(HANDLE_FDK_BITSTREAM hBitStream) {
578   FDKsyncCache(hBitStream);
579   return FDK_getValidBits(&hBitStream->hBitBuf);
580 }
581 
582 /**
583  * \brief return amount of unused Bits from Bitbuffer.
584  * \param hBitStream HANDLE_FDK_BITSTREAM handle
585  * \return amount of free bits that still can be written into the bitstream
586  */
FDKgetFreeBits(HANDLE_FDK_BITSTREAM hBitStream)587 FDK_INLINE INT FDKgetFreeBits(HANDLE_FDK_BITSTREAM hBitStream) {
588   return FDK_getFreeBits(&hBitStream->hBitBuf);
589 }
590 
591 /**
592  * \brief Fill the BitBuffer with a number of input bytes from  external source.
593  *        The bytesValid variable returns the number of ramaining valid bytes in
594  * extern inputBuffer.
595  *
596  * \param hBitStream  HANDLE_FDK_BITSTREAM handle
597  * \param inputBuffer Pointer to input buffer with bitstream data.
598  * \param bufferSize  Total size of inputBuffer array.
599  * \param bytesValid  Input: number of valid bytes in inputBuffer. Output: bytes
600  * still left unread in inputBuffer.
601  * \return void
602  */
FDKfeedBuffer(HANDLE_FDK_BITSTREAM hBitStream,const UCHAR inputBuffer[],const UINT bufferSize,UINT * bytesValid)603 FDK_INLINE void FDKfeedBuffer(HANDLE_FDK_BITSTREAM hBitStream,
604                               const UCHAR inputBuffer[], const UINT bufferSize,
605                               UINT *bytesValid) {
606   FDKsyncCache(hBitStream);
607   FDK_Feed(&hBitStream->hBitBuf, inputBuffer, bufferSize, bytesValid);
608 }
609 
610 /**
611  * \brief fill destination BitBuffer with a number of bytes from source
612  * BitBuffer. The bytesValid variable returns the number of ramaining valid
613  * bytes in source BitBuffer.
614  *
615  * \param hBSDst            HANDLE_FDK_BITSTREAM handle to write data into
616  * \param hBSSrc            HANDLE_FDK_BITSTREAM handle to read data from
617  * \param bytesValid        Input: number of valid bytes in inputBuffer. Output:
618  * bytes still left unread in inputBuffer.
619  * \return void
620  */
FDKcopyBuffer(HANDLE_FDK_BITSTREAM hBSDst,HANDLE_FDK_BITSTREAM hBSSrc,UINT * bytesValid)621 FDK_INLINE void FDKcopyBuffer(HANDLE_FDK_BITSTREAM hBSDst,
622                               HANDLE_FDK_BITSTREAM hBSSrc, UINT *bytesValid) {
623   FDKsyncCache(hBSSrc);
624   FDK_Copy(&hBSDst->hBitBuf, &hBSSrc->hBitBuf, bytesValid);
625 }
626 
627 /**
628  * \brief fill the outputBuffer with all valid bytes hold in BitBuffer. The
629  * WriteBytes variable returns the number of written Bytes.
630  *
631  * \param hBitStream    HANDLE_FDK_BITSTREAM handle
632  * \param outputBuffer  Pointer to output buffer.
633  * \param writeBytes    Number of bytes write to output buffer.
634  * \return void
635  */
FDKfetchBuffer(HANDLE_FDK_BITSTREAM hBitStream,UCHAR * outputBuffer,UINT * writeBytes)636 FDK_INLINE void FDKfetchBuffer(HANDLE_FDK_BITSTREAM hBitStream,
637                                UCHAR *outputBuffer, UINT *writeBytes) {
638   FDKsyncCache(hBitStream);
639   FDK_Fetch(&hBitStream->hBitBuf, outputBuffer, writeBytes);
640 }
641 
642 #endif
643