2 /* -----------------------------------------------------------------------------------------------------------
3 Software License for The Fraunhofer FDK AAC Codec Library for Android
5 © Copyright 1995 - 2013 Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V.
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.
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.
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.
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.
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:
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.
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.
42 The name of Fraunhofer may not be used to endorse or promote products derived from this library without
43 prior written permission.
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.
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."
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.
59 You may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized
60 by appropriate patent licenses.
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.
73 5. CONTACT INFORMATION
75 Fraunhofer Institute for Integrated Circuits IIS
76 Attention: Audio and Multimedia Departments - FDK AAC LL
78 91058 Erlangen, Germany
80 www.iis.fraunhofer.de/amm
81 amm-info@iis.fraunhofer.de
82 ----------------------------------------------------------------------------------------------------------- */
84 /*************************** Fraunhofer IIS FDK Tools ***********************
86 Author(s): M. Lohwasser
87 Description: bitstream interface to bitbuffer routines
89 ******************************************************************************/
91 #ifndef __FDK_BITSTREAM_H__
92 #define __FDK_BITSTREAM_H__
96 #include "FDK_bitbuffer.h"
97 #include "machine_type.h"
99 #include "genericStds.h"
101 #define CACHE_BITS 32
117 typedef FDK_BITSTREAM
* HANDLE_FDK_BITSTREAM
;
120 * \brief CreateBitStream Function.
122 * Create and initialize bitstream with extern allocated buffer.
124 * \param pBuffer Pointer to BitBuffer array.
125 * \param bufSize Length of BitBuffer array. (awaits size 2^n)
126 * \param config Initialize BitStream as Reader or Writer.
129 HANDLE_FDK_BITSTREAM
FDKcreateBitStream (UCHAR
*pBuffer
,
131 FDK_BS_CFG config
= BS_READER
)
133 HANDLE_FDK_BITSTREAM hBitStream
= (HANDLE_FDK_BITSTREAM
) FDKcalloc(1, sizeof(FDK_BITSTREAM
));
134 FDK_InitBitBuffer(&hBitStream
->hBitBuf
, pBuffer
, bufSize
, 0) ;
137 hBitStream
->CacheWord
= hBitStream
->BitsInCache
= 0 ;
138 hBitStream
->ConfigCache
= config
;
145 * \brief Initialize BistreamBuffer. BitBuffer can point to filled BitBuffer array .
147 * \param hBitStream HANDLE_FDK_BITSTREAM handle
148 * \param pBuffer Pointer to BitBuffer array.
149 * \param bufSize Length of BitBuffer array. (awaits size 2^n)
150 * \param validBits Number of valid BitBuffer filled Bits.
151 * \param config Initialize BitStream as Reader or Writer.
155 void FDKinitBitStream (HANDLE_FDK_BITSTREAM hBitStream
,
159 FDK_BS_CFG config
= BS_READER
)
161 FDK_InitBitBuffer(&hBitStream
->hBitBuf
, pBuffer
, bufSize
, validBits
) ;
164 hBitStream
->CacheWord
= hBitStream
->BitsInCache
= 0 ;
165 hBitStream
->ConfigCache
= config
;
170 * \brief ResetBitbuffer Function. Reset states in BitBuffer and Cache.
172 * \param hBitStream HANDLE_FDK_BITSTREAM handle
173 * \param config Initialize BitStream as Reader or Writer.
176 FDK_INLINE
void FDKresetBitbuffer( HANDLE_FDK_BITSTREAM hBitStream
, FDK_BS_CFG config
= BS_READER
)
178 FDK_ResetBitBuffer( &hBitStream
->hBitBuf
) ;
181 hBitStream
->CacheWord
= hBitStream
->BitsInCache
= 0 ;
182 hBitStream
->ConfigCache
= config
;
188 Deletes the in Create Bitstream allocated BitStream and BitBuffer.
190 FDK_INLINE
void FDKdeleteBitStream (HANDLE_FDK_BITSTREAM hBitStream
)
192 FDK_DeleteBitBuffer(&hBitStream
->hBitBuf
) ;
193 FDKfree(hBitStream
) ;
198 * \brief ReadBits Function (forward). This function returns a number of sequential
199 * bits from the input bitstream.
201 * \param hBitStream HANDLE_FDK_BITSTREAM handle
202 * \param numberOfBits The number of bits to be retrieved.
203 * \return the requested bits, right aligned
206 #define OPTIMIZE_FDKREADBITS
208 FDK_INLINE UINT
FDKreadBits(HANDLE_FDK_BITSTREAM hBitStream
,
209 const UINT numberOfBits
)
211 #ifdef noOPTIMIZE_FDKREADBITS
212 INT missingBits
= numberOfBits
- hBitStream
->BitsInCache
;
215 UINT bits
= hBitStream
->CacheWord
<< missingBits
;
216 hBitStream
->CacheWord
= FDK_get32 (&hBitStream
->hBitBuf
) ;
217 hBitStream
->BitsInCache
= CACHE_BITS
- missingBits
;
218 return ( bits
| (hBitStream
->CacheWord
>> hBitStream
->BitsInCache
)) & BitMask
[numberOfBits
];
221 hBitStream
->BitsInCache
-= numberOfBits
;
222 return ( hBitStream
->CacheWord
>> hBitStream
->BitsInCache
) & BitMask
[numberOfBits
];
225 const UINT validMask
= BitMask
[numberOfBits
] ;
227 if (hBitStream
->BitsInCache
<= numberOfBits
)
229 const INT freeBits
= (CACHE_BITS
-1) - hBitStream
->BitsInCache
;
231 hBitStream
->CacheWord
= (hBitStream
->CacheWord
<< freeBits
) | FDK_get (&hBitStream
->hBitBuf
,freeBits
) ;
232 hBitStream
->BitsInCache
+= freeBits
;
235 hBitStream
->BitsInCache
-= numberOfBits
;
237 return (hBitStream
->CacheWord
>> hBitStream
->BitsInCache
) & validMask
;
241 FDK_INLINE UINT
FDKreadBit(HANDLE_FDK_BITSTREAM hBitStream
)
243 #ifdef OPTIMIZE_FDKREADBITS
244 if (!hBitStream
->BitsInCache
)
246 hBitStream
->CacheWord
= FDK_get32 (&hBitStream
->hBitBuf
);
247 hBitStream
->BitsInCache
= CACHE_BITS
;
249 hBitStream
->BitsInCache
--;
251 return (hBitStream
->CacheWord
>> hBitStream
->BitsInCache
) & 1;
253 return FDKreadBits(hBitStream
,1);
258 * \brief Read2Bits Function (forward). This function 2 sequential
259 * bits from the input bitstream. It is the optimized version
260 of FDKreadBits() for readign 2 bits.
262 * \param hBitStream HANDLE_FDK_BITSTREAM handle
263 * \return the requested bits, right aligned
266 inline UINT
FDKread2Bits(HANDLE_FDK_BITSTREAM hBitStream
)
268 UINT BitsInCache
= hBitStream
->BitsInCache
;
269 if (BitsInCache
< 2) /* Comparison changed from 'less-equal' to 'less' */
271 const INT freeBits
= (CACHE_BITS
-1) - BitsInCache
;
273 hBitStream
->CacheWord
= (hBitStream
->CacheWord
<< freeBits
) | FDK_get (&hBitStream
->hBitBuf
,freeBits
) ;
274 BitsInCache
+= freeBits
;
276 hBitStream
->BitsInCache
= BitsInCache
- 2;
277 return (hBitStream
->CacheWord
>> hBitStream
->BitsInCache
) & 0x3;
281 * \brief ReadBits Function (backward). This function returns a number of sequential bits
282 * from the input bitstream.
284 * \param hBitStream HANDLE_FDK_BITSTREAM handle
285 * \param numberOfBits The number of bits to be retrieved.
286 * \return the requested bits, right aligned
288 FDK_INLINE UINT
FDKreadBitsBwd(HANDLE_FDK_BITSTREAM hBitStream
,
289 const UINT numberOfBits
)
291 const UINT validMask
= BitMask
[numberOfBits
] ;
293 if (hBitStream
->BitsInCache
<= numberOfBits
)
295 const INT freeBits
= (CACHE_BITS
-1) - hBitStream
->BitsInCache
;
297 hBitStream
->CacheWord
= (hBitStream
->CacheWord
<< freeBits
) | FDK_getBwd (&hBitStream
->hBitBuf
,freeBits
) ;
298 hBitStream
->BitsInCache
+= freeBits
;
301 hBitStream
->BitsInCache
-= numberOfBits
;
303 return (hBitStream
->CacheWord
>> hBitStream
->BitsInCache
) & validMask
;
308 * \brief return a number of bits from the bitBuffer.
309 * You have to know what you do! Cache has to be synchronized before using this
312 * \param hBitStream HANDLE_FDK_BITSTREAM handle
313 * \param numBits The number of bits to be retrieved.
314 * \return the requested bits, right aligned
316 FDK_INLINE UINT
FDKgetBits (HANDLE_FDK_BITSTREAM hBitStream
, UINT numBits
)
318 return FDK_get (&hBitStream
->hBitBuf
, numBits
) ;
323 * \brief WriteBits Function. This function writes numberOfBits of value into bitstream.
325 * \param hBitStream HANDLE_FDK_BITSTREAM handle
326 * \param value Variable holds data to be written.
327 * \param numberOfBits The number of bits to be written.
328 * \return number of bits written
330 FDK_INLINE UCHAR
FDKwriteBits(HANDLE_FDK_BITSTREAM hBitStream
, UINT value
,
331 const UINT numberOfBits
)
333 const UINT validMask
= BitMask
[numberOfBits
] ;
335 if ((hBitStream
->BitsInCache
+numberOfBits
) < CACHE_BITS
)
337 hBitStream
->BitsInCache
+= numberOfBits
;
338 hBitStream
->CacheWord
= (hBitStream
->CacheWord
<< numberOfBits
) | (value
& validMask
);
342 FDK_put(&hBitStream
->hBitBuf
, hBitStream
->CacheWord
, hBitStream
->BitsInCache
) ;
343 hBitStream
->BitsInCache
= numberOfBits
;
344 hBitStream
->CacheWord
= (value
& validMask
) ;
352 * \brief WriteBits Function (backward). This function writes numberOfBits of value into bitstream.
354 * \param hBitStream HANDLE_FDK_BITSTREAM handle
355 * \param value Variable holds data to be written.
356 * \param numberOfBits The number of bits to be written.
357 * \return number of bits written
359 FDK_INLINE UCHAR
FDKwriteBitsBwd(HANDLE_FDK_BITSTREAM hBitStream
, UINT value
,
360 const UINT numberOfBits
)
362 const UINT validMask
= BitMask
[numberOfBits
] ;
364 if ((hBitStream
->BitsInCache
+numberOfBits
) <= CACHE_BITS
)
366 hBitStream
->BitsInCache
+= numberOfBits
;
367 hBitStream
->CacheWord
= (hBitStream
->CacheWord
<< numberOfBits
) | (value
& validMask
);
371 FDK_putBwd(&hBitStream
->hBitBuf
, hBitStream
->CacheWord
, hBitStream
->BitsInCache
) ;
372 hBitStream
->BitsInCache
= numberOfBits
;
373 hBitStream
->CacheWord
= (value
& validMask
) ;
381 * \brief SyncCache Function. Clear cache after read forward.
383 * \param hBitStream HANDLE_FDK_BITSTREAM handle
386 FDK_INLINE
void FDKsyncCache (HANDLE_FDK_BITSTREAM hBitStream
)
388 if (hBitStream
->ConfigCache
== BS_READER
)
389 FDK_pushBack (&hBitStream
->hBitBuf
,hBitStream
->BitsInCache
,hBitStream
->ConfigCache
) ;
391 FDK_put(&hBitStream
->hBitBuf
, hBitStream
->CacheWord
, hBitStream
->BitsInCache
) ;
393 hBitStream
->BitsInCache
= 0 ;
394 hBitStream
->CacheWord
= 0 ;
399 * \brief SyncCache Function. Clear cache after read backwards.
401 * \param hBitStream HANDLE_FDK_BITSTREAM handle
404 FDK_INLINE
void FDKsyncCacheBwd (HANDLE_FDK_BITSTREAM hBitStream
)
406 if (hBitStream
->ConfigCache
== BS_READER
) {
407 FDK_pushForward (&hBitStream
->hBitBuf
,hBitStream
->BitsInCache
,hBitStream
->ConfigCache
) ;
408 } else { /* BS_WRITER */
409 FDK_putBwd (&hBitStream
->hBitBuf
, hBitStream
->CacheWord
, hBitStream
->BitsInCache
) ;
412 hBitStream
->BitsInCache
= 0 ;
413 hBitStream
->CacheWord
= 0 ;
418 * \brief Byte Alignment Function.
419 * This function performs the byte_alignment() syntactic function on the input stream,
420 * i.e. some bits will be discarded/padded so that the next bits to be read/written will
421 * be aligned on a byte boundary with respect to the bit position 0.
423 * \param hBitStream HANDLE_FDK_BITSTREAM handle
426 FDK_INLINE
void FDKbyteAlign (HANDLE_FDK_BITSTREAM hBitStream
)
428 FDKsyncCache (hBitStream
) ;
429 FDK_byteAlign (&hBitStream
->hBitBuf
, (UCHAR
)hBitStream
->ConfigCache
) ;
434 * \brief Byte Alignment Function with anchor
435 * This function performs the byte_alignment() syntactic function on the input stream,
436 * i.e. some bits will be discarded so that the next bits to be read/written would be aligned
437 * on a byte boundary with respect to the given alignment anchor.
439 * \param hBitStream HANDLE_FDK_BITSTREAM handle
440 * \param alignmentAnchor bit position to be considered as origin for byte alignment
443 FDK_INLINE
void FDKbyteAlign (HANDLE_FDK_BITSTREAM hBitStream
, UINT alignmentAnchor
)
445 FDKsyncCache (hBitStream
) ;
446 if (hBitStream
->ConfigCache
== BS_READER
) {
447 FDK_pushForward (&hBitStream
->hBitBuf
,
448 (8 - ((alignmentAnchor
- FDK_getValidBits(&hBitStream
->hBitBuf
)) & 0x07)) & 0x07,
449 hBitStream
->ConfigCache
) ;
452 FDK_put (&hBitStream
->hBitBuf
,
454 (8 - ((FDK_getValidBits(&hBitStream
->hBitBuf
)-alignmentAnchor
) & 0x07)) & 0x07 );
460 * \brief Push Back(Cache) / For / BiDirectional Function.
461 * PushBackCache function ungets a number of bits erroneously read/written by the last Get() call.
462 * NB: The number of bits to be stuffed back into the stream may never exceed the
463 * number of bits returned by the immediately preceding Get() call.
465 * PushBack function ungets a number of bits (combines cache and bitbuffer indices)
466 * PushFor function gets a number of bits (combines cache and bitbuffer indices)
467 * PushBiDirectional gets/ungets number of bits as defined in PusBack/For function
468 * NB: The sign of bits is not known, so the function checks direction and calls
469 * appropriate function. (positive sign pushFor, negative sign pushBack )
471 * \param hBitStream HANDLE_FDK_BITSTREAM handle
472 * \param numberOfBits The number of bits to be pushed back/for.
475 FDK_INLINE
void FDKpushBackCache (HANDLE_FDK_BITSTREAM hBitStream
, const UINT numberOfBits
)
477 FDK_ASSERT ((hBitStream
->BitsInCache
+numberOfBits
)<=CACHE_BITS
);
478 hBitStream
->BitsInCache
+= numberOfBits
;
481 FDK_INLINE
void FDKpushBack (HANDLE_FDK_BITSTREAM hBitStream
, const UINT numberOfBits
)
483 if ((hBitStream
->BitsInCache
+numberOfBits
)<CACHE_BITS
&& (hBitStream
->ConfigCache
== BS_READER
) ) {
484 hBitStream
->BitsInCache
+= numberOfBits
;
485 FDKsyncCache(hBitStream
) ; /* sync cache to avoid invalid cache */
488 FDKsyncCache(hBitStream
) ;
489 FDK_pushBack(&hBitStream
->hBitBuf
,numberOfBits
,hBitStream
->ConfigCache
);
493 FDK_INLINE
void FDKpushFor (HANDLE_FDK_BITSTREAM hBitStream
, const UINT numberOfBits
)
495 if ( (hBitStream
->BitsInCache
>numberOfBits
) && (hBitStream
->ConfigCache
== BS_READER
) ) {
496 hBitStream
->BitsInCache
-= numberOfBits
;
499 FDKsyncCache(hBitStream
) ;
500 FDK_pushForward(&hBitStream
->hBitBuf
,numberOfBits
,hBitStream
->ConfigCache
);
504 FDK_INLINE
void FDKpushBiDirectional (HANDLE_FDK_BITSTREAM hBitStream
, const INT numberOfBits
)
506 if(numberOfBits
>=0) FDKpushFor(hBitStream
, numberOfBits
) ;
507 else FDKpushBack(hBitStream
, -numberOfBits
) ;
512 * \brief GetValidBits Function. Clear cache and return valid Bits from Bitbuffer.
513 * \param hBitStream HANDLE_FDK_BITSTREAM handle
514 * \return amount of valid bits that still can be read or were already written.
517 FDK_INLINE UINT
FDKgetValidBits (HANDLE_FDK_BITSTREAM hBitStream
)
519 FDKsyncCache(hBitStream
) ;
520 return FDK_getValidBits(&hBitStream
->hBitBuf
) ;
525 * \brief return amount of unused Bits from Bitbuffer.
526 * \param hBitStream HANDLE_FDK_BITSTREAM handle
527 * \return amount of free bits that still can be written into the bitstream
529 FDK_INLINE INT
FDKgetFreeBits (HANDLE_FDK_BITSTREAM hBitStream
)
531 return FDK_getFreeBits (&hBitStream
->hBitBuf
) ;
535 * \brief reset bitcounter in bitBuffer to zero.
536 * \param hBitStream HANDLE_FDK_BITSTREAM handle
539 FDK_INLINE
void FDKresetBitCnt (HANDLE_FDK_BITSTREAM hBitStream
)
541 FDKsyncCache (hBitStream
) ;
542 FDK_setBitCnt (&hBitStream
->hBitBuf
, 0) ;
546 * \brief set bitcoutner in bitBuffer to given value.
547 * \param hBitStream HANDLE_FDK_BITSTREAM handle
548 * \param value new value to be assigned to the bit counter
551 FDK_INLINE
void FDKsetBitCnt (HANDLE_FDK_BITSTREAM hBitStream
, UINT value
)
553 FDKsyncCache (hBitStream
) ;
554 FDK_setBitCnt (&hBitStream
->hBitBuf
, value
) ;
558 * \brief get bitcounter state from bitBuffer.
559 * \param hBitStream HANDLE_FDK_BITSTREAM handle
560 * \return current bit counter value
562 FDK_INLINE INT
FDKgetBitCnt (HANDLE_FDK_BITSTREAM hBitStream
)
564 FDKsyncCache(hBitStream
) ;
565 return FDK_getBitCnt(&hBitStream
->hBitBuf
) ;
570 * \brief Fill the BitBuffer with a number of input bytes from external source.
571 * The bytesValid variable returns the number of ramaining valid bytes in extern inputBuffer.
573 * \param hBitStream HANDLE_FDK_BITSTREAM handle
574 * \param inputBuffer Pointer to input buffer with bitstream data.
575 * \param bufferSize Total size of inputBuffer array.
576 * \param bytesValid Input: number of valid bytes in inputBuffer. Output: bytes still left unread in inputBuffer.
579 FDK_INLINE
void FDKfeedBuffer (HANDLE_FDK_BITSTREAM hBitStream
, const UCHAR inputBuffer
[], const UINT bufferSize
, UINT
*bytesValid
)
581 FDKsyncCache (hBitStream
) ;
582 FDK_Feed(&hBitStream
->hBitBuf
, (UCHAR
*)inputBuffer
, bufferSize
, bytesValid
) ;
587 * \brief fill destination BitBuffer with a number of bytes from source BitBuffer. The
588 * bytesValid variable returns the number of ramaining valid bytes in source BitBuffer.
590 * \param hBSDst HANDLE_FDK_BITSTREAM handle to write data into
591 * \param hBSSrc HANDLE_FDK_BITSTREAM handle to read data from
592 * \param bytesValid Input: number of valid bytes in inputBuffer. Output: bytes still left unread in inputBuffer.
595 FDK_INLINE
void FDKcopyBuffer (HANDLE_FDK_BITSTREAM hBSDst
, HANDLE_FDK_BITSTREAM hBSSrc
, UINT
*bytesValid
)
597 FDKsyncCache (hBSSrc
) ;
598 FDK_Copy (&hBSDst
->hBitBuf
, &hBSSrc
->hBitBuf
, bytesValid
) ;
603 * \brief fill the outputBuffer with all valid bytes hold in BitBuffer. The WriteBytes
604 * variable returns the number of written Bytes.
606 * \param hBitStream HANDLE_FDK_BITSTREAM handle
607 * \param outputBuffer Pointer to output buffer.
608 * \param writeBytes Number of bytes write to output buffer.
611 FDK_INLINE
void FDKfetchBuffer(HANDLE_FDK_BITSTREAM hBitStream
, UCHAR
*outputBuffer
, UINT
*writeBytes
)
613 FDKsyncCache (hBitStream
) ;
614 FDK_Fetch(&hBitStream
->hBitBuf
, outputBuffer
, writeBytes
);