1
2/* -----------------------------------------------------------------------------------------------------------
3Software License for The Fraunhofer FDK AAC Codec Library for Android
4
5© Copyright  1995 - 2012 Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V.
6  All rights reserved.
7
8 1.    INTRODUCTION
9The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software that implements
10the MPEG Advanced Audio Coding ("AAC") encoding and decoding scheme for digital audio.
11This FDK AAC Codec software is intended to be used on a wide variety of Android devices.
12
13AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient general perceptual
14audio codecs. AAC-ELD is considered the best-performing full-bandwidth communications codec by
15independent studies and is widely deployed. AAC has been standardized by ISO and IEC as part
16of the MPEG specifications.
17
18Patent licenses for necessary patent claims for the FDK AAC Codec (including those of Fraunhofer)
19may be obtained through Via Licensing (www.vialicensing.com) or through the respective patent owners
20individually for the purpose of encoding or decoding bit streams in products that are compliant with
21the ISO/IEC MPEG audio standards. Please note that most manufacturers of Android devices already license
22these patent claims through Via Licensing or directly from the patent owners, and therefore FDK AAC Codec
23software may already be covered under those patent licenses when it is used for those licensed purposes only.
24
25Commercially-licensed AAC software libraries, including floating-point versions with enhanced sound quality,
26are also available from Fraunhofer. Users are encouraged to check the Fraunhofer website for additional
27applications information and documentation.
28
292.    COPYRIGHT LICENSE
30
31Redistribution and use in source and binary forms, with or without modification, are permitted without
32payment of copyright license fees provided that you satisfy the following conditions:
33
34You must retain the complete text of this software license in redistributions of the FDK AAC Codec or
35your modifications thereto in source code form.
36
37You must retain the complete text of this software license in the documentation and/or other materials
38provided with redistributions of the FDK AAC Codec or your modifications thereto in binary form.
39You must make available free of charge copies of the complete source code of the FDK AAC Codec and your
40modifications thereto to recipients of copies in binary form.
41
42The name of Fraunhofer may not be used to endorse or promote products derived from this library without
43prior written permission.
44
45You may not charge copyright license fees for anyone to use, copy or distribute the FDK AAC Codec
46software or your modifications thereto.
47
48Your modified versions of the FDK AAC Codec must carry prominent notices stating that you changed the software
49and 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
533.    NO PATENT LICENSE
54
55NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without limitation the patents of Fraunhofer,
56ARE GRANTED BY THIS SOFTWARE LICENSE. Fraunhofer provides no warranty of patent non-infringement with
57respect to this software.
58
59You may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized
60by appropriate patent licenses.
61
624.    DISCLAIMER
63
64This 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
66of merchantability and fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
67CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary, or consequential damages,
68including but not limited to procurement of substitute goods or services; loss of use, data, or profits,
69or business interruption, however caused and on any theory of liability, whether in contract, strict
70liability, or tort (including negligence), arising in any way out of the use of this software, even if
71advised of the possibility of such damage.
72
735.    CONTACT INFORMATION
74
75Fraunhofer Institute for Integrated Circuits IIS
76Attention: Audio and Multimedia Departments - FDK AAC LL
77Am Wolfsmantel 33
7891058 Erlangen, Germany
79
80www.iis.fraunhofer.de/amm
81amm-info@iis.fraunhofer.de
82----------------------------------------------------------------------------------------------------------- */
83
84/******************************** MPEG Audio Encoder **************************
85
86   Initial author:
87   contents/description: CRC calculation
88
89******************************************************************************/
90
91#include "FDK_crc.h"
92
93
94
95/*---------------- constants -----------------------*/
96
97/**
98 * \brief  This table defines precalculated lookup tables for crc polynom  x^16 + x^15 + x^2 + x^0.
99 */
100static const USHORT crcLookup_16_15_2_0[256] =
101{
102  0x0000, 0x8005, 0x800f, 0x000a, 0x801b, 0x001e, 0x0014, 0x8011,
103  0x8033, 0x0036, 0x003c, 0x8039, 0x0028, 0x802d, 0x8027, 0x0022,
104  0x8063, 0x0066, 0x006c, 0x8069, 0x0078, 0x807d, 0x8077, 0x0072,
105  0x0050, 0x8055, 0x805f, 0x005a, 0x804b, 0x004e, 0x0044, 0x8041,
106  0x80c3, 0x00c6, 0x00cc, 0x80c9, 0x00d8, 0x80dd, 0x80d7, 0x00d2,
107  0x00f0, 0x80f5, 0x80ff, 0x00fa, 0x80eb, 0x00ee, 0x00e4, 0x80e1,
108  0x00a0, 0x80a5, 0x80af, 0x00aa, 0x80bb, 0x00be, 0x00b4, 0x80b1,
109  0x8093, 0x0096, 0x009c, 0x8099, 0x0088, 0x808d, 0x8087, 0x0082,
110  0x8183, 0x0186, 0x018c, 0x8189, 0x0198, 0x819d, 0x8197, 0x0192,
111  0x01b0, 0x81b5, 0x81bf, 0x01ba, 0x81ab, 0x01ae, 0x01a4, 0x81a1,
112  0x01e0, 0x81e5, 0x81ef, 0x01ea, 0x81fb, 0x01fe, 0x01f4, 0x81f1,
113  0x81d3, 0x01d6, 0x01dc, 0x81d9, 0x01c8, 0x81cd, 0x81c7, 0x01c2,
114  0x0140, 0x8145, 0x814f, 0x014a, 0x815b, 0x015e, 0x0154, 0x8151,
115  0x8173, 0x0176, 0x017c, 0x8179, 0x0168, 0x816d, 0x8167, 0x0162,
116  0x8123, 0x0126, 0x012c, 0x8129, 0x0138, 0x813d, 0x8137, 0x0132,
117  0x0110, 0x8115, 0x811f, 0x011a, 0x810b, 0x010e, 0x0104, 0x8101,
118  0x8303, 0x0306, 0x030c, 0x8309, 0x0318, 0x831d, 0x8317, 0x0312,
119  0x0330, 0x8335, 0x833f, 0x033a, 0x832b, 0x032e, 0x0324, 0x8321,
120  0x0360, 0x8365, 0x836f, 0x036a, 0x837b, 0x037e, 0x0374, 0x8371,
121  0x8353, 0x0356, 0x035c, 0x8359, 0x0348, 0x834d, 0x8347, 0x0342,
122  0x03c0, 0x83c5, 0x83cf, 0x03ca, 0x83db, 0x03de, 0x03d4, 0x83d1,
123  0x83f3, 0x03f6, 0x03fc, 0x83f9, 0x03e8, 0x83ed, 0x83e7, 0x03e2,
124  0x83a3, 0x03a6, 0x03ac, 0x83a9, 0x03b8, 0x83bd, 0x83b7, 0x03b2,
125  0x0390, 0x8395, 0x839f, 0x039a, 0x838b, 0x038e, 0x0384, 0x8381,
126  0x0280, 0x8285, 0x828f, 0x028a, 0x829b, 0x029e, 0x0294, 0x8291,
127  0x82b3, 0x02b6, 0x02bc, 0x82b9, 0x02a8, 0x82ad, 0x82a7, 0x02a2,
128  0x82e3, 0x02e6, 0x02ec, 0x82e9, 0x02f8, 0x82fd, 0x82f7, 0x02f2,
129  0x02d0, 0x82d5, 0x82df, 0x02da, 0x82cb, 0x02ce, 0x02c4, 0x82c1,
130  0x8243, 0x0246, 0x024c, 0x8249, 0x0258, 0x825d, 0x8257, 0x0252,
131  0x0270, 0x8275, 0x827f, 0x027a, 0x826b, 0x026e, 0x0264, 0x8261,
132  0x0220, 0x8225, 0x822f, 0x022a, 0x823b, 0x023e, 0x0234, 0x8231,
133  0x8213, 0x0216, 0x021c, 0x8219, 0x0208, 0x820d, 0x8207, 0x0202
134};
135
136/**
137 * \brief  This table defines precalculated lookup tables for crc polynom  x^16 + x^12 + x^5 + x^0.
138 */
139static const USHORT crcLookup_16_12_5_0[256] =
140{
141  0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7,
142  0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
143  0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6,
144  0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de,
145  0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485,
146  0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d,
147  0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4,
148  0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc,
149  0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823,
150  0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b,
151  0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12,
152  0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a,
153  0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41,
154  0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49,
155  0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70,
156  0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78,
157  0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f,
158  0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067,
159  0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e,
160  0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256,
161  0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d,
162  0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
163  0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c,
164  0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634,
165  0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab,
166  0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3,
167  0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a,
168  0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92,
169  0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9,
170  0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1,
171  0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8,
172  0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0
173};
174
175
176/*--------------- function declarations --------------------*/
177
178static inline INT calcCrc_Bits(
179        USHORT * const                  pCrc,
180        USHORT                          crcMask,
181        USHORT                          crcPoly,
182        HANDLE_FDK_BITSTREAM            hBs,
183        INT                             nBits
184        );
185
186static inline INT calcCrc_Bytes(
187        USHORT * const                  pCrc,
188        const USHORT *                  pCrcLookup,
189        HANDLE_FDK_BITSTREAM            hBs,
190        INT                             nBytes
191        );
192
193static void crcCalc(
194        HANDLE_FDK_CRCINFO              hCrcInfo,
195        HANDLE_FDK_BITSTREAM            hBs,
196        const INT                       reg
197        );
198
199
200/*------------- function definitions ----------------*/
201
202void FDKcrcInit(
203        HANDLE_FDK_CRCINFO              hCrcInfo,
204        const UINT                      crcPoly,
205        const UINT                      crcStartValue,
206        const UINT                      crcLen
207        )
208{
209  /* crc polynom example:
210  x^16 + x^15 + x^2 + x^0        (1) 1000 0000 0000 0101 -> 0x8005
211  x^16 + x^12 + x^5 + x^0        (1) 0001 0000 0010 0001 -> 0x1021
212  x^8 + x^4 + x^3 + x^2 + x^0              (1) 0001 1101 -> 0x001d */
213
214  hCrcInfo->crcLen     = crcLen;
215  hCrcInfo->crcPoly    = crcPoly;
216  hCrcInfo->startValue = crcStartValue;
217  hCrcInfo->crcMask    = (crcLen) ? (1<<(crcLen-1)) : 0;
218
219  FDKcrcReset(hCrcInfo);
220
221  hCrcInfo->pCrcLookup = 0;
222
223  if (hCrcInfo->crcLen==16) {
224    switch ( crcPoly ) {
225      case 0x8005:
226        hCrcInfo->pCrcLookup = crcLookup_16_15_2_0;
227        break;
228      case 0x1021:
229        hCrcInfo->pCrcLookup = crcLookup_16_12_5_0;
230        break;
231      case 0x001d:
232      default:
233        /* no lookup table */
234        hCrcInfo->pCrcLookup = 0;
235    }
236  }
237
238
239}
240
241void FDKcrcReset(HANDLE_FDK_CRCINFO hCrcInfo)
242{
243  int i;
244
245  hCrcInfo->crcValue = hCrcInfo->startValue;
246
247  for(i=0;i<MAX_CRC_REGS;i++) {
248    hCrcInfo->crcRegData[i].isActive = 0;
249  }
250  hCrcInfo->regStart = 0;
251  hCrcInfo->regStop  = 0;
252}
253
254INT FDKcrcStartReg(
255        HANDLE_FDK_CRCINFO              hCrcInfo,
256        const HANDLE_FDK_BITSTREAM      hBs,
257        const INT                       mBits
258        )
259{
260  int reg = hCrcInfo->regStart;
261
262  FDK_ASSERT(hCrcInfo->crcRegData[reg].isActive==0);
263  hCrcInfo->crcRegData[reg].isActive      = 1;
264  hCrcInfo->crcRegData[reg].maxBits       = mBits;
265  hCrcInfo->crcRegData[reg].validBits     = FDKgetValidBits(hBs) ;
266  hCrcInfo->crcRegData[reg].bitBufCntBits = 0;
267
268  hCrcInfo->regStart = (hCrcInfo->regStart+1)%MAX_CRC_REGS;
269
270  return (reg);
271}
272
273INT FDKcrcEndReg(
274        HANDLE_FDK_CRCINFO              hCrcInfo,
275        const HANDLE_FDK_BITSTREAM      hBs,
276        const INT                       reg
277        )
278{
279  FDK_ASSERT((reg==(INT)hCrcInfo->regStop)&&(hCrcInfo->crcRegData[reg].isActive==1));
280
281  if (hBs->ConfigCache==BS_WRITER) {
282    hCrcInfo->crcRegData[reg].bitBufCntBits = FDKgetValidBits(hBs) - hCrcInfo->crcRegData[reg].validBits;
283  }
284  else {
285    hCrcInfo->crcRegData[reg].bitBufCntBits = hCrcInfo->crcRegData[reg].validBits - FDKgetValidBits(hBs);
286  }
287
288  if (hCrcInfo->crcRegData[reg].maxBits == 0) {
289    hCrcInfo->crcRegData[reg].maxBits = hCrcInfo->crcRegData[reg].bitBufCntBits;
290  }
291
292  crcCalc( hCrcInfo, hBs, reg);
293
294  hCrcInfo->crcRegData[reg].isActive = 0;
295  hCrcInfo->regStop = (hCrcInfo->regStop+1)%MAX_CRC_REGS;
296
297  return 0;
298}
299
300USHORT FDKcrcGetCRC(
301        const HANDLE_FDK_CRCINFO        hCrcInfo
302        )
303{
304  return ( hCrcInfo->crcValue & (((hCrcInfo->crcMask-1)<<1)+1) );
305}
306
307/**
308 * \brief  Calculate crc bits.
309 *
310 * Calculate crc starting at current bitstream postion over nBits.
311 *
312 * \param pCrc                  Pointer to an outlying allocated crc info structure.
313 * \param crcMask               CrcMask in use.
314 * \param crcPoly               Crc polynom in use.
315 * \param hBs                   Handle to current bit buffer structure.
316 * \param nBits                 Number of processing bits.
317 *
318 * \return  Number of processed bits.
319 */
320static inline INT calcCrc_Bits(
321        USHORT * const                  pCrc,
322        USHORT                          crcMask,
323        USHORT                          crcPoly,
324        HANDLE_FDK_BITSTREAM            hBs,
325        INT                             nBits
326        )
327{
328  int i;
329  USHORT crc = *pCrc; /* get crc value */
330
331  if (hBs!=NULL) {
332    for (i = 0; (i < nBits); i++) {
333      USHORT tmp = FDKreadBits(hBs,1);     // process single bit
334      tmp ^= ( (crc & crcMask) ? 1 : 0 );
335      tmp *= crcPoly;
336      crc <<= 1;
337      crc ^= tmp;
338    }
339  }
340  else {
341    for (i = 0; (i < nBits); i++) {
342      USHORT tmp = 0;     // process single bit
343      tmp ^= ( (crc & crcMask) ? 1 : 0 );
344      tmp *= crcPoly;
345      crc <<= 1;
346      crc ^= tmp;
347    }
348  }
349  *pCrc = crc; /* update crc value */
350
351  return nBits;
352}
353
354/**
355 * \brief  Calculate crc bytes.
356 *
357 * Calculate crc starting at current bitstream postion over nBytes.
358 *
359 * \param pCrc                  Pointer to an outlying allocated crc info structure.
360 * \param pCrcLookup            Pointer to lookup table used for fast crc calculation.
361 * \param hBs                   Handle to current bit buffer structure.
362 * \param nBits                 Number of processing bytes.
363 *
364 * \return  Number of processed bits.
365 */
366static inline INT calcCrc_Bytes(
367        USHORT * const                  pCrc,
368        const USHORT *                  pCrcLookup,
369        HANDLE_FDK_BITSTREAM            hBs,
370        INT                             nBytes
371        )
372{
373  int i;
374  USHORT crc = *pCrc; /* get crc value */
375
376  if (hBs!=NULL) {
377    for (i=0; i<nBytes; i++) {
378      crc = (crc<<8)^pCrcLookup[((crc>>8)^((UCHAR)FDKreadBits(hBs,8)))&0xFF];
379    }
380  }
381  else {
382    for (i=0; i<nBytes; i++) {
383      crc = (crc<<8)^pCrcLookup[((crc>>8)^((UCHAR)0))&0xFF];
384    }
385  }
386
387  *pCrc = crc; /* update crc value */
388
389  return (i);
390}
391
392/**
393 * \brief  Calculate crc.
394 *
395 * Calculate crc. Lenght depends on mBits parameter in FDKcrcStartReg() configuration.
396 *
397 * \param hCrcInfo              Pointer to an outlying allocated crc info structure.
398 * \param hBs                   Pointer to current bit buffer structure.
399 * \param reg                   Crc region ID.
400 *
401 * \return  Number of processed bits.
402 */
403static void crcCalc(
404        HANDLE_FDK_CRCINFO              hCrcInfo,
405        HANDLE_FDK_BITSTREAM            hBs,
406        const INT                       reg
407        )
408{
409  USHORT crc = hCrcInfo->crcValue;
410  CCrcRegData *rD = &hCrcInfo->crcRegData[reg];
411  FDK_BITSTREAM bsReader;
412
413  if (hBs->ConfigCache==BS_READER) {
414    bsReader = *hBs;
415    FDKpushBiDirectional(&bsReader, -(INT)(rD->validBits-FDKgetValidBits(&bsReader)));
416  }
417  else {
418    FDKinitBitStream(&bsReader, hBs->hBitBuf.Buffer, hBs->hBitBuf.bufSize, hBs->hBitBuf.ValidBits, BS_READER);
419    FDKpushBiDirectional(&bsReader, rD->validBits);
420  }
421
422  int bits, rBits;
423  rBits = (rD->maxBits>=0) ? rD->maxBits : -rD->maxBits; /* ramaining bits */
424  if ((rD->maxBits>0) && (((INT)rD->bitBufCntBits>>3<<3)<rBits) ) {
425    bits = rD->bitBufCntBits;
426  }
427  else {
428    bits = rBits;
429  }
430
431  int words = bits >> 3;  /* processing bytes */
432  int mBits = bits & 0x7; /* modulo bits */
433
434  if(hCrcInfo->pCrcLookup) {
435    rBits -= (calcCrc_Bytes(&crc, hCrcInfo->pCrcLookup, &bsReader, words)<<3);
436  }
437  else {
438    rBits -= calcCrc_Bits(&crc, hCrcInfo->crcMask, hCrcInfo->crcPoly, &bsReader, words<<3 );
439  }
440
441  /* remaining valid bits*/
442  if(mBits!=0) {
443    rBits -= calcCrc_Bits(&crc, hCrcInfo->crcMask, hCrcInfo->crcPoly, &bsReader, mBits );
444  }
445
446  if (rBits!=0) {
447    /* zero bytes */
448    if ( (hCrcInfo->pCrcLookup) && (rBits>8) ) {
449      rBits -= (calcCrc_Bytes(&crc, hCrcInfo->pCrcLookup, NULL, rBits>>3)<<3);
450    }
451    /* remaining zero bits */
452    if (rBits!=0) {
453      rBits -= calcCrc_Bits(&crc, hCrcInfo->crcMask, hCrcInfo->crcPoly, NULL, rBits );
454    }
455  }
456
457  hCrcInfo->crcValue = crc;
458}
459
460