• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 /* -----------------------------------------------------------------------------------------------------------
3 Software License for The Fraunhofer FDK AAC Codec Library for Android
4 
5 � Copyright  1995 - 2015 Fraunhofer-Gesellschaft zur F�rderung der angewandten Forschung e.V.
6   All rights reserved.
7 
8  1.    INTRODUCTION
9 The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software that implements
10 the MPEG Advanced Audio Coding ("AAC") encoding and decoding scheme for digital audio.
11 This FDK AAC Codec software is intended to be used on a wide variety of Android devices.
12 
13 AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient general perceptual
14 audio codecs. AAC-ELD is considered the best-performing full-bandwidth communications codec by
15 independent studies and is widely deployed. AAC has been standardized by ISO and IEC as part
16 of the MPEG specifications.
17 
18 Patent licenses for necessary patent claims for the FDK AAC Codec (including those of Fraunhofer)
19 may be obtained through Via Licensing (www.vialicensing.com) or through the respective patent owners
20 individually for the purpose of encoding or decoding bit streams in products that are compliant with
21 the ISO/IEC MPEG audio standards. Please note that most manufacturers of Android devices already license
22 these patent claims through Via Licensing or directly from the patent owners, and therefore FDK AAC Codec
23 software may already be covered under those patent licenses when it is used for those licensed purposes only.
24 
25 Commercially-licensed AAC software libraries, including floating-point versions with enhanced sound quality,
26 are also available from Fraunhofer. Users are encouraged to check the Fraunhofer website for additional
27 applications information and documentation.
28 
29 2.    COPYRIGHT LICENSE
30 
31 Redistribution and use in source and binary forms, with or without modification, are permitted without
32 payment of copyright license fees provided that you satisfy the following conditions:
33 
34 You must retain the complete text of this software license in redistributions of the FDK AAC Codec or
35 your modifications thereto in source code form.
36 
37 You must retain the complete text of this software license in the documentation and/or other materials
38 provided with redistributions of the FDK AAC Codec or your modifications thereto in binary form.
39 You must make available free of charge copies of the complete source code of the FDK AAC Codec and your
40 modifications thereto to recipients of copies in binary form.
41 
42 The name of Fraunhofer may not be used to endorse or promote products derived from this library without
43 prior written permission.
44 
45 You may not charge copyright license fees for anyone to use, copy or distribute the FDK AAC Codec
46 software or your modifications thereto.
47 
48 Your modified versions of the FDK AAC Codec must carry prominent notices stating that you changed the software
49 and the date of any change. For modified versions of the FDK AAC Codec, the term
50 "Fraunhofer FDK AAC Codec Library for Android" must be replaced by the term
51 "Third-Party Modified Version of the Fraunhofer FDK AAC Codec Library for Android."
52 
53 3.    NO PATENT LICENSE
54 
55 NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without limitation the patents of Fraunhofer,
56 ARE GRANTED BY THIS SOFTWARE LICENSE. Fraunhofer provides no warranty of patent non-infringement with
57 respect to this software.
58 
59 You may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized
60 by appropriate patent licenses.
61 
62 4.    DISCLAIMER
63 
64 This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright holders and contributors
65 "AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, including but not limited to the implied warranties
66 of merchantability and fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
67 CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary, or consequential damages,
68 including but not limited to procurement of substitute goods or services; loss of use, data, or profits,
69 or business interruption, however caused and on any theory of liability, whether in contract, strict
70 liability, or tort (including negligence), arising in any way out of the use of this software, even if
71 advised of the possibility of such damage.
72 
73 5.    CONTACT INFORMATION
74 
75 Fraunhofer Institute for Integrated Circuits IIS
76 Attention: Audio and Multimedia Departments - FDK AAC LL
77 Am Wolfsmantel 33
78 91058 Erlangen, Germany
79 
80 www.iis.fraunhofer.de/amm
81 amm-info@iis.fraunhofer.de
82 ----------------------------------------------------------------------------------------------------------- */
83 
84 /*****************************  MPEG-4 AAC Decoder  **************************
85 
86    Author(s):   Josef Hoepfl
87    Description: long/short-block decoding
88 
89 ******************************************************************************/
90 
91 #include "block.h"
92 
93 #include "aac_rom.h"
94 #include "FDK_bitstream.h"
95 #include "FDK_tools_rom.h"
96 
97 
98 
99 
100 #include "aacdec_hcr.h"
101 #include "rvlc.h"
102 
103 
104 #if defined(__arm__)
105 #include "arm/block_arm.cpp"
106 #endif
107 
108 /*!
109   \brief Read escape sequence of codeword
110 
111   The function reads the escape sequence from the bitstream,
112   if the absolute value of the quantized coefficient has the
113   value 16.
114 
115   \return  quantized coefficient
116 */
CBlock_GetEscape(HANDLE_FDK_BITSTREAM bs,const LONG q)117 LONG CBlock_GetEscape(HANDLE_FDK_BITSTREAM bs, /*!< pointer to bitstream */
118                      const LONG q)        /*!< quantized coefficient */
119 {
120   LONG i, off, neg ;
121 
122   if (q < 0)
123   {
124     if (q != -16) return q;
125     neg = 1;
126   }
127   else
128   {
129     if (q != +16) return q;
130     neg = 0;
131   }
132 
133   for (i=4; ; i++)
134   {
135     if (FDKreadBits(bs,1) == 0)
136       break;
137   }
138 
139   if (i > 16)
140   {
141     if (i - 16 > CACHE_BITS) { /* cannot read more than "CACHE_BITS" bits at once in the function FDKreadBits() */
142       return (MAX_QUANTIZED_VALUE + 1); /* returning invalid value that will be captured later */
143     }
144 
145     off = FDKreadBits(bs,i-16) << 16;
146     off |= FDKreadBits(bs,16);
147   }
148   else
149   {
150     off = FDKreadBits(bs,i);
151   }
152 
153   i = off + (1 << i);
154 
155   if (neg) i = -i;
156 
157   return i;
158 }
159 
CBlock_ReadScaleFactorData(CAacDecoderChannelInfo * pAacDecoderChannelInfo,HANDLE_FDK_BITSTREAM bs,UINT flags)160 AAC_DECODER_ERROR CBlock_ReadScaleFactorData(
161         CAacDecoderChannelInfo *pAacDecoderChannelInfo,
162         HANDLE_FDK_BITSTREAM bs,
163         UINT flags
164         )
165 {
166   int temp;
167   int band;
168   int group;
169   int position = 0; /* accu for intensity delta coding */
170   int factor = pAacDecoderChannelInfo->pDynData->RawDataInfo.GlobalGain; /* accu for scale factor delta coding */
171   UCHAR *pCodeBook = pAacDecoderChannelInfo->pDynData->aCodeBook;
172   SHORT *pScaleFactor = pAacDecoderChannelInfo->pDynData->aScaleFactor;
173   const CodeBookDescription *hcb =&AACcodeBookDescriptionTable[BOOKSCL];
174 
175   int ScaleFactorBandsTransmitted = GetScaleFactorBandsTransmitted(&pAacDecoderChannelInfo->icsInfo);
176   for (group=0; group < GetWindowGroups(&pAacDecoderChannelInfo->icsInfo); group++)
177   {
178     for (band=0; band < ScaleFactorBandsTransmitted; band++)
179     {
180       switch (pCodeBook[group*16+band]) {
181 
182       case ZERO_HCB: /* zero book */
183         pScaleFactor[group*16+band] = 0;
184         break;
185 
186       default: /* decode scale factor */
187         {
188           temp = CBlock_DecodeHuffmanWord(bs,hcb);
189           factor += temp - 60; /* MIDFAC 1.5 dB */
190         }
191         pScaleFactor[group*16+band] = factor - 100;
192         break;
193 
194       case INTENSITY_HCB: /* intensity steering */
195       case INTENSITY_HCB2:
196         temp = CBlock_DecodeHuffmanWord(bs,hcb);
197         position += temp - 60;
198         pScaleFactor[group*16+band] = position - 100;
199         break;
200 
201       case NOISE_HCB: /* PNS */
202         if (flags & (AC_MPS_RES|AC_USAC|AC_RSVD50)) {
203           return AAC_DEC_PARSE_ERROR;
204         }
205         CPns_Read( &pAacDecoderChannelInfo->data.aac.PnsData, bs, hcb, pAacDecoderChannelInfo->pDynData->aScaleFactor, pAacDecoderChannelInfo->pDynData->RawDataInfo.GlobalGain, band, group);
206         break;
207       }
208     }
209   }
210 
211   return AAC_DEC_OK;
212 }
213 
CBlock_ScaleSpectralData(CAacDecoderChannelInfo * pAacDecoderChannelInfo,SamplingRateInfo * pSamplingRateInfo)214 void CBlock_ScaleSpectralData(CAacDecoderChannelInfo *pAacDecoderChannelInfo, SamplingRateInfo *pSamplingRateInfo)
215 {
216   int band;
217   int window;
218   const SHORT * RESTRICT pSfbScale  = pAacDecoderChannelInfo->pDynData->aSfbScale;
219   SHORT * RESTRICT pSpecScale = pAacDecoderChannelInfo->specScale;
220   int groupwin,group;
221   const SHORT * RESTRICT BandOffsets = GetScaleFactorBandOffsets(&pAacDecoderChannelInfo->icsInfo, pSamplingRateInfo);
222   SPECTRAL_PTR RESTRICT pSpectralCoefficient = pAacDecoderChannelInfo->pSpectralCoefficient;
223 
224 
225   FDKmemclear(pSpecScale, 8*sizeof(SHORT));
226 
227   int max_band = GetScaleFactorBandsTransmitted(&pAacDecoderChannelInfo->icsInfo);
228   for (window=0, group=0; group < GetWindowGroups(&pAacDecoderChannelInfo->icsInfo); group++)
229   {
230     for (groupwin=0; groupwin < GetWindowGroupLength(&pAacDecoderChannelInfo->icsInfo,group); groupwin++, window++)
231     {
232       int SpecScale_window = pSpecScale[window];
233       FIXP_DBL *pSpectrum = SPEC(pSpectralCoefficient, window,  pAacDecoderChannelInfo->granuleLength);
234 
235       /* find scaling for current window */
236       for (band=0; band < max_band; band++)
237       {
238         SpecScale_window = fMax(SpecScale_window, (int)pSfbScale[window*16+band]);
239       }
240 
241       if (pAacDecoderChannelInfo->pDynData->TnsData.Active) {
242         SpecScale_window += TNS_SCALE;
243       }
244 
245       /* store scaling of current window */
246       pSpecScale[window] = SpecScale_window;
247 
248 #ifdef FUNCTION_CBlock_ScaleSpectralData_func1
249 
250       CBlock_ScaleSpectralData_func1(pSpectrum, max_band, BandOffsets, SpecScale_window, pSfbScale, window);
251 
252 #else /* FUNCTION_CBlock_ScaleSpectralData_func1 */
253       for (band=0; band < max_band; band++)
254       {
255         int scale = SpecScale_window - pSfbScale[window*16+band];
256         if (scale)
257         {
258           /* following relation can be used for optimizations: (BandOffsets[i]%4) == 0 for all i */
259           int max_index = BandOffsets[band+1];
260           for (int index = BandOffsets[band]; index < max_index; index++)
261           {
262             pSpectrum[index] >>= scale;
263           }
264         }
265       }
266 #endif  /* FUNCTION_CBlock_ScaleSpectralData_func1 */
267     }
268   }
269 
270 }
271 
CBlock_ReadSectionData(HANDLE_FDK_BITSTREAM bs,CAacDecoderChannelInfo * pAacDecoderChannelInfo,const SamplingRateInfo * pSamplingRateInfo,const UINT flags)272 AAC_DECODER_ERROR CBlock_ReadSectionData(HANDLE_FDK_BITSTREAM bs,
273                                          CAacDecoderChannelInfo *pAacDecoderChannelInfo,
274                                          const SamplingRateInfo *pSamplingRateInfo,
275                                          const UINT  flags)
276 {
277   int top, band;
278   int sect_len, sect_len_incr;
279   int group;
280   UCHAR sect_cb;
281   UCHAR *pCodeBook = pAacDecoderChannelInfo->pDynData->aCodeBook;
282   /* HCR input (long) */
283   SHORT *pNumLinesInSec    = pAacDecoderChannelInfo->pDynData->specificTo.aac.aNumLineInSec4Hcr;
284   int    numLinesInSecIdx  = 0;
285   UCHAR *pHcrCodeBook      = pAacDecoderChannelInfo->pDynData->specificTo.aac.aCodeBooks4Hcr;
286   const SHORT *BandOffsets = GetScaleFactorBandOffsets(&pAacDecoderChannelInfo->icsInfo, pSamplingRateInfo);
287   pAacDecoderChannelInfo->pDynData->specificTo.aac.numberSection = 0;
288   AAC_DECODER_ERROR ErrorStatus = AAC_DEC_OK;
289 
290   FDKmemclear(pCodeBook, sizeof(UCHAR)*(8*16));
291 
292   const int nbits = (IsLongBlock(&pAacDecoderChannelInfo->icsInfo) == 1) ? 5 : 3;
293 
294   int sect_esc_val = (1 << nbits) - 1 ;
295 
296   UCHAR ScaleFactorBandsTransmitted = GetScaleFactorBandsTransmitted(&pAacDecoderChannelInfo->icsInfo);
297   for (group=0; group<GetWindowGroups(&pAacDecoderChannelInfo->icsInfo); group++)
298   {
299     for (band=0; band < ScaleFactorBandsTransmitted; )
300     {
301       sect_len = 0;
302       if ( flags & AC_ER_VCB11 )  {
303         sect_cb = (UCHAR) FDKreadBits(bs,5);
304       }
305       else
306         sect_cb = (UCHAR) FDKreadBits(bs,4);
307 
308       if ( ((flags & AC_ER_VCB11) == 0) || ( sect_cb < 11 ) || ((sect_cb > 11) && (sect_cb < 16)) ) {
309         sect_len_incr = FDKreadBits(bs, nbits);
310         while (sect_len_incr == sect_esc_val)
311         {
312           sect_len += sect_esc_val;
313           sect_len_incr = FDKreadBits(bs, nbits);
314         }
315       }
316       else {
317         sect_len_incr = 1;
318       }
319 
320       sect_len += sect_len_incr;
321 
322 
323       top = band + sect_len;
324 
325       if (flags & AC_ER_HCR) {
326         /* HCR input (long) -- collecting sideinfo (for HCR-_long_ only) */
327         if (numLinesInSecIdx >= MAX_SFB_HCR) {
328           return AAC_DEC_PARSE_ERROR;
329         }
330         pNumLinesInSec[numLinesInSecIdx] = BandOffsets[top] - BandOffsets[band];
331         numLinesInSecIdx++;
332         if (sect_cb == BOOKSCL)
333         {
334           return AAC_DEC_INVALID_CODE_BOOK;
335         } else {
336           *pHcrCodeBook++ = sect_cb;
337         }
338         pAacDecoderChannelInfo->pDynData->specificTo.aac.numberSection++;
339       }
340 
341       /* Check spectral line limits */
342       if (IsLongBlock( &(pAacDecoderChannelInfo->icsInfo) ))
343       {
344         if (top > 64) {
345           return AAC_DEC_DECODE_FRAME_ERROR;
346         }
347       } else { /* short block */
348         if (top + group*16 > (8 * 16)) {
349           return AAC_DEC_DECODE_FRAME_ERROR;
350         }
351       }
352 
353       /* Check if decoded codebook index is feasible */
354       if ( (sect_cb == BOOKSCL)
355        || ( (sect_cb == INTENSITY_HCB || sect_cb == INTENSITY_HCB2) && pAacDecoderChannelInfo->pDynData->RawDataInfo.CommonWindow == 0)
356          )
357       {
358         return AAC_DEC_INVALID_CODE_BOOK;
359       }
360 
361       /* Store codebook index */
362       for (; band < top; band++)
363       {
364         pCodeBook[group*16+band] = sect_cb;
365       }
366     }
367   }
368 
369 
370   return ErrorStatus;
371 }
372 
373 /* mso: provides a faster way to i-quantize a whole band in one go */
374 
375 /**
376  * \brief inverse quantize one sfb. Each value of the sfb is processed according to the
377  *        formula: spectrum[i] = Sign(spectrum[i]) * Matissa(spectrum[i])^(4/3) * 2^(lsb/4).
378  * \param spectrum pointer to first line of the sfb to be inverse quantized.
379  * \param noLines number of lines belonging to the sfb.
380  * \param lsb last 2 bits of the scale factor of the sfb.
381  * \param scale max allowed shift scale for the sfb.
382  */
383 static
InverseQuantizeBand(FIXP_DBL * RESTRICT spectrum,INT noLines,INT lsb,INT scale)384 void InverseQuantizeBand( FIXP_DBL * RESTRICT spectrum,
385                               INT noLines,
386                               INT lsb,
387                               INT scale )
388 {
389     const FIXP_DBL * RESTRICT InverseQuantTabler=(FIXP_DBL *)InverseQuantTable;
390     const FIXP_DBL * RESTRICT MantissaTabler=(FIXP_DBL *)MantissaTable[lsb];
391     const SCHAR* RESTRICT ExponentTabler=(SCHAR*)ExponentTable[lsb];
392 
393     FIXP_DBL *ptr = spectrum;
394     FIXP_DBL signedValue;
395 
396     FDK_ASSERT(noLines>2);
397     for (INT i=noLines; i--; )
398     {
399         if ((signedValue = *ptr++) != FL2FXCONST_DBL(0))
400         {
401           FIXP_DBL value = fAbs(signedValue);
402           UINT freeBits = CntLeadingZeros(value);
403           UINT exponent = 32 - freeBits;
404 
405           UINT x = (UINT) (LONG)value << (INT) freeBits;
406           x <<= 1;                                  /* shift out sign bit to avoid masking later on */
407           UINT tableIndex = x >> 24;
408           x = (x >> 20) &  0x0F;
409 
410           UINT r0=(UINT)(LONG)InverseQuantTabler[tableIndex+0];
411           UINT r1=(UINT)(LONG)InverseQuantTabler[tableIndex+1];
412           UINT temp= (r1 - r0)*x + (r0 << 4);
413 
414           value = fMultDiv2((FIXP_DBL)temp, MantissaTabler[exponent]);
415 
416           /* + 1 compensates fMultDiv2() */
417           scaleValueInPlace(&value, scale + ExponentTabler[exponent] + 1);
418 
419           signedValue = (signedValue < (FIXP_DBL)0) ? -value : value;
420           ptr[-1] = signedValue;
421         }
422     }
423 }
424 
CBlock_InverseQuantizeSpectralData(CAacDecoderChannelInfo * pAacDecoderChannelInfo,SamplingRateInfo * pSamplingRateInfo)425 AAC_DECODER_ERROR CBlock_InverseQuantizeSpectralData(CAacDecoderChannelInfo *pAacDecoderChannelInfo, SamplingRateInfo *pSamplingRateInfo)
426 {
427   int window, group, groupwin, band;
428   int ScaleFactorBandsTransmitted = GetScaleFactorBandsTransmitted(&pAacDecoderChannelInfo->icsInfo);
429   UCHAR *RESTRICT pCodeBook = pAacDecoderChannelInfo->pDynData->aCodeBook;
430   SHORT *RESTRICT pSfbScale = pAacDecoderChannelInfo->pDynData->aSfbScale;
431   SHORT *RESTRICT pScaleFactor = pAacDecoderChannelInfo->pDynData->aScaleFactor;
432   const SHORT *RESTRICT BandOffsets = GetScaleFactorBandOffsets(&pAacDecoderChannelInfo->icsInfo, pSamplingRateInfo);
433 
434   FDKmemclear(pAacDecoderChannelInfo->pDynData->aSfbScale, (8*16)*sizeof(SHORT));
435 
436   for (window=0, group=0; group < GetWindowGroups(&pAacDecoderChannelInfo->icsInfo); group++)
437   {
438     for (groupwin=0; groupwin < GetWindowGroupLength(&pAacDecoderChannelInfo->icsInfo,group); groupwin++, window++)
439     {
440       /* inverse quantization */
441       for (band=0; band < ScaleFactorBandsTransmitted; band++)
442       {
443         FIXP_DBL *pSpectralCoefficient = SPEC(pAacDecoderChannelInfo->pSpectralCoefficient, window, pAacDecoderChannelInfo->granuleLength) + BandOffsets[band];
444 
445         int noLines = BandOffsets[band+1] - BandOffsets[band];
446         int bnds = group*16+band;
447         int i;
448 
449         if ((pCodeBook[bnds] == ZERO_HCB)
450          || (pCodeBook[bnds] == INTENSITY_HCB)
451          || (pCodeBook[bnds] == INTENSITY_HCB2)
452            )
453           continue;
454 
455         if (pCodeBook[bnds] == NOISE_HCB)
456         {
457           /* Leave headroom for PNS values. + 1 because ceil(log2(2^(0.25*3))) = 1,
458              worst case of additional headroom required because of the scalefactor. */
459           pSfbScale[window*16+band] = (pScaleFactor [bnds] >> 2) + 1 ;
460           continue;
461         }
462 
463         /* Find max spectral line value of the current sfb */
464         FIXP_DBL locMax = (FIXP_DBL)0;
465 
466         for (i = noLines; i-- ; ) {
467           /* Expensive memory access */
468           locMax = fMax(fixp_abs(pSpectralCoefficient[i]), locMax);
469         }
470 
471         /* Cheap robustness improvement - Do not remove!!! */
472         if (fixp_abs(locMax) > (FIXP_DBL)MAX_QUANTIZED_VALUE) {
473           return AAC_DEC_DECODE_FRAME_ERROR;
474         }
475 
476         /*
477            The inverse quantized spectral lines are defined by:
478         pSpectralCoefficient[i] = Sign(pSpectralCoefficient[i]) * 2^(0.25*pScaleFactor[bnds]) * pSpectralCoefficient[i]^(4/3)
479            This is equivalent to:
480         pSpectralCoefficient[i]    = Sign(pSpectralCoefficient[i]) * (2^(pScaleFactor[bnds] % 4) * pSpectralCoefficient[i]^(4/3))
481         pSpectralCoefficient_e[i] += pScaleFactor[bnds]/4
482         */
483         {
484           int msb = pScaleFactor [bnds] >> 2 ;
485           int lsb = pScaleFactor [bnds] & 0x03 ;
486 
487           int scale = GetScaleFromValue(locMax, lsb);
488 
489           pSfbScale[window*16+band] = msb - scale;
490           InverseQuantizeBand(pSpectralCoefficient, noLines, lsb, scale);
491         }
492       }
493     }
494   }
495 
496 
497   return AAC_DEC_OK;
498 }
499 
500 
CBlock_ReadSpectralData(HANDLE_FDK_BITSTREAM bs,CAacDecoderChannelInfo * pAacDecoderChannelInfo,const SamplingRateInfo * pSamplingRateInfo,const UINT flags)501 AAC_DECODER_ERROR  CBlock_ReadSpectralData(HANDLE_FDK_BITSTREAM bs,
502                                            CAacDecoderChannelInfo *pAacDecoderChannelInfo,
503                                            const SamplingRateInfo *pSamplingRateInfo,
504                                            const UINT  flags)
505 {
506   int i,index;
507   int window,group,groupwin,groupoffset,band;
508   UCHAR *RESTRICT pCodeBook = pAacDecoderChannelInfo->pDynData->aCodeBook;
509   const SHORT *RESTRICT BandOffsets = GetScaleFactorBandOffsets(&pAacDecoderChannelInfo->icsInfo, pSamplingRateInfo);
510 
511   SPECTRAL_PTR pSpectralCoefficient = pAacDecoderChannelInfo->pSpectralCoefficient;
512   FIXP_DBL locMax;
513 
514   int ScaleFactorBandsTransmitted = GetScaleFactorBandsTransmitted(&pAacDecoderChannelInfo->icsInfo);
515 
516   FDK_ASSERT(BandOffsets != NULL);
517 
518   FDKmemclear(pSpectralCoefficient, sizeof(SPECTRUM));
519 
520   if ( (flags & AC_ER_HCR) == 0 )
521   {
522     groupoffset = 0;
523 
524     /* plain huffman decoder  short */
525     for (group=0; group < GetWindowGroups(&pAacDecoderChannelInfo->icsInfo); group++)
526     {
527       for (band=0; band < ScaleFactorBandsTransmitted; band++)
528       {
529         int bnds = group*16+band;
530         UCHAR currentCB = pCodeBook[bnds];
531 
532         /* patch to run plain-huffman-decoder with vcb11 input codebooks (LAV-checking might be possible below using the virtual cb and a LAV-table) */
533         if ((currentCB >= 16) && (currentCB <= 31)) {
534           pCodeBook[bnds] = currentCB = 11;
535         }
536         if ( !((currentCB == ZERO_HCB)
537             || (currentCB == NOISE_HCB)
538             || (currentCB == INTENSITY_HCB)
539             || (currentCB == INTENSITY_HCB2)) )
540         {
541           const CodeBookDescription *hcb = &AACcodeBookDescriptionTable[currentCB];
542           int step = hcb->Dimension;
543           int offset = hcb->Offset;
544           int bits = hcb->numBits;
545           int mask = (1<<bits)-1;
546 
547           for (groupwin=0; groupwin < GetWindowGroupLength(&pAacDecoderChannelInfo->icsInfo,group); groupwin++)
548           {
549             window = groupoffset + groupwin;
550 
551             FIXP_DBL *mdctSpectrum = SPEC(pSpectralCoefficient, window, pAacDecoderChannelInfo->granuleLength);
552 
553             locMax = (FIXP_DBL)0 ;
554 
555             for (index=BandOffsets[band]; index < BandOffsets[band+1]; index+=step)
556             {
557               int idx = CBlock_DecodeHuffmanWord(bs,hcb);
558 
559               for (i=0; i<step; i++) {
560                 FIXP_DBL tmp;
561 
562                 tmp = (FIXP_DBL)((idx & mask)-offset);
563                 idx >>= bits;
564 
565                 if (offset == 0) {
566                   if (tmp != FIXP_DBL(0))
567                     tmp = (FDKreadBits(bs,1))? -tmp : tmp;
568                 }
569                 mdctSpectrum[index+i] = tmp;
570               }
571 
572               if (currentCB == ESCBOOK)
573               {
574                 mdctSpectrum[index+0] = (FIXP_DBL)CBlock_GetEscape(bs, (LONG)mdctSpectrum[index+0]);
575                 mdctSpectrum[index+1] = (FIXP_DBL)CBlock_GetEscape(bs, (LONG)mdctSpectrum[index+1]);
576 
577               }
578             }
579           }
580         }
581       }
582       groupoffset += GetWindowGroupLength(&pAacDecoderChannelInfo->icsInfo,group);
583     }
584     /* plain huffman decoding (short) finished */
585   }
586   /* HCR - Huffman Codeword Reordering  short */
587   else  /* if ( flags & AC_ER_HCR ) */
588   {
589     H_HCR_INFO hHcr = &pAacDecoderChannelInfo->pComData->overlay.aac.erHcrInfo;
590     int hcrStatus = 0;
591 
592     /* advanced Huffman decoding starts here (HCR decoding :) */
593     if ( pAacDecoderChannelInfo->pDynData->specificTo.aac.lenOfReorderedSpectralData != 0 ) {
594 
595       /* HCR initialization short */
596       hcrStatus = HcrInit(hHcr, pAacDecoderChannelInfo, pSamplingRateInfo, bs);
597 
598       if (hcrStatus != 0) {
599         return AAC_DEC_DECODE_FRAME_ERROR;
600       }
601 
602       /* HCR decoding short */
603       hcrStatus = HcrDecoder(hHcr, pAacDecoderChannelInfo, pSamplingRateInfo, bs);
604 
605       if (hcrStatus != 0) {
606 #if HCR_ERROR_CONCEALMENT
607         HcrMuteErroneousLines(hHcr);
608 #else
609         return AAC_DEC_DECODE_FRAME_ERROR;
610 #endif /* HCR_ERROR_CONCEALMENT */
611       }
612 
613       FDKpushFor (bs, pAacDecoderChannelInfo->pDynData->specificTo.aac.lenOfReorderedSpectralData);
614     }
615   }
616   /* HCR - Huffman Codeword Reordering short finished */
617 
618 
619 
620   if ( IsLongBlock(&pAacDecoderChannelInfo->icsInfo) && !(flags & (AC_ELD|AC_SCALABLE)) )
621   {
622     /* apply pulse data */
623     CPulseData_Apply(&pAacDecoderChannelInfo->pDynData->specificTo.aac.PulseData,
624                       GetScaleFactorBandOffsets(&pAacDecoderChannelInfo->icsInfo, pSamplingRateInfo),
625                       SPEC_LONG(pSpectralCoefficient));
626   }
627 
628 
629   return AAC_DEC_OK;
630 }
631 
632 
633 
ApplyTools(CAacDecoderChannelInfo * pAacDecoderChannelInfo[],const SamplingRateInfo * pSamplingRateInfo,const UINT flags,const int channel)634 void ApplyTools ( CAacDecoderChannelInfo *pAacDecoderChannelInfo[],
635                   const SamplingRateInfo *pSamplingRateInfo,
636                   const UINT flags,
637                   const int channel )
638 {
639 
640   if ( !(flags & (AC_USAC|AC_RSVD50|AC_MPS_RES)) ) {
641     CPns_Apply(
642            &pAacDecoderChannelInfo[channel]->data.aac.PnsData,
643            &pAacDecoderChannelInfo[channel]->icsInfo,
644             pAacDecoderChannelInfo[channel]->pSpectralCoefficient,
645             pAacDecoderChannelInfo[channel]->specScale,
646             pAacDecoderChannelInfo[channel]->pDynData->aScaleFactor,
647             pSamplingRateInfo,
648             pAacDecoderChannelInfo[channel]->granuleLength,
649             channel
650             );
651   }
652 
653   CTns_Apply (
654          &pAacDecoderChannelInfo[channel]->pDynData->TnsData,
655          &pAacDecoderChannelInfo[channel]->icsInfo,
656           pAacDecoderChannelInfo[channel]->pSpectralCoefficient,
657           pSamplingRateInfo,
658           pAacDecoderChannelInfo[channel]->granuleLength
659           );
660 }
661 
662 static
getWindow2Nr(int length,int shape)663 int getWindow2Nr(int length, int shape)
664 {
665   int nr = 0;
666 
667   if (shape == 2) {
668     /* Low Overlap, 3/4 zeroed */
669     nr = (length * 3)>>2;
670   }
671 
672   return nr;
673 }
674 
CBlock_FrequencyToTime(CAacDecoderStaticChannelInfo * pAacDecoderStaticChannelInfo,CAacDecoderChannelInfo * pAacDecoderChannelInfo,INT_PCM outSamples[],const SHORT frameLen,const int stride,const int frameOk,FIXP_DBL * pWorkBuffer1)675 void CBlock_FrequencyToTime(CAacDecoderStaticChannelInfo *pAacDecoderStaticChannelInfo,
676                             CAacDecoderChannelInfo *pAacDecoderChannelInfo,
677                             INT_PCM outSamples[],
678                             const SHORT frameLen,
679                             const int stride,
680                             const int frameOk,
681                             FIXP_DBL *pWorkBuffer1 )
682 {
683   int fr, fl, tl, nSamples, nSpec;
684 
685   /* Determine left slope length (fl), right slope length (fr) and transform length (tl).
686      USAC: The slope length may mismatch with the previous frame in case of LPD / FD
687            transitions. The adjustment is handled by the imdct implementation.
688   */
689   tl = frameLen;
690   nSpec = 1;
691 
692   switch( pAacDecoderChannelInfo->icsInfo.WindowSequence ) {
693     default:
694     case OnlyLongSequence:
695       fl = frameLen;
696       fr = frameLen - getWindow2Nr(frameLen, GetWindowShape(&pAacDecoderChannelInfo->icsInfo));
697       break;
698     case LongStopSequence:
699       fl = frameLen >> 3;
700       fr = frameLen;
701       break;
702     case LongStartSequence: /* or StopStartSequence */
703       fl = frameLen;
704       fr = frameLen >> 3;
705       break;
706     case EightShortSequence:
707       fl = fr = frameLen >> 3;
708       tl >>= 3;
709       nSpec = 8;
710       break;
711   }
712 
713   {
714     int i;
715 
716     {
717       FIXP_DBL *tmp = pAacDecoderChannelInfo->pComData->workBufferCore1->mdctOutTemp;
718 
719       nSamples = imdct_block(
720              &pAacDecoderStaticChannelInfo->IMdct,
721               tmp,
722               SPEC_LONG(pAacDecoderChannelInfo->pSpectralCoefficient),
723               pAacDecoderChannelInfo->specScale,
724               nSpec,
725               frameLen,
726               tl,
727               FDKgetWindowSlope(fl, GetWindowShape(&pAacDecoderChannelInfo->icsInfo)),
728               fl,
729               FDKgetWindowSlope(fr, GetWindowShape(&pAacDecoderChannelInfo->icsInfo)),
730               fr,
731               (FIXP_DBL)0 );
732 
733       for (i=0; i<frameLen; i++) {
734         outSamples[i*stride] = IMDCT_SCALE(tmp[i]);
735       }
736     }
737   }
738 
739   FDK_ASSERT(nSamples == frameLen);
740 
741 }
742 
743 #include "ldfiltbank.h"
CBlock_FrequencyToTimeLowDelay(CAacDecoderStaticChannelInfo * pAacDecoderStaticChannelInfo,CAacDecoderChannelInfo * pAacDecoderChannelInfo,INT_PCM outSamples[],const short frameLen,const char stride)744 void CBlock_FrequencyToTimeLowDelay( CAacDecoderStaticChannelInfo *pAacDecoderStaticChannelInfo,
745                                      CAacDecoderChannelInfo *pAacDecoderChannelInfo,
746                                      INT_PCM outSamples[],
747                                      const short frameLen,
748                                      const char stride )
749 {
750   InvMdctTransformLowDelay_fdk (
751           SPEC_LONG(pAacDecoderChannelInfo->pSpectralCoefficient),
752           pAacDecoderChannelInfo->specScale[0],
753           outSamples,
754           pAacDecoderStaticChannelInfo->pOverlapBuffer,
755           stride,
756           frameLen
757           );
758 }
759