1
2/* -----------------------------------------------------------------------------------------------------------
3Software 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
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-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*/
117LONG 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
160AAC_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
214void 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
272AAC_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 (
333             (sect_cb == BOOKSCL) )
334        {
335          return AAC_DEC_INVALID_CODE_BOOK;
336        } else {
337          *pHcrCodeBook++ = sect_cb;
338        }
339        pAacDecoderChannelInfo->pDynData->specificTo.aac.numberSection++;
340      }
341
342      /* Check spectral line limits */
343      if (IsLongBlock( &(pAacDecoderChannelInfo->icsInfo) ))
344      {
345        if (top > 64) {
346          return AAC_DEC_DECODE_FRAME_ERROR;
347        }
348      } else { /* short block */
349        if (top + group*16 > (8 * 16)) {
350          return AAC_DEC_DECODE_FRAME_ERROR;
351        }
352      }
353
354      /* Check if decoded codebook index is feasible */
355      if ( (sect_cb == BOOKSCL)
356       || ( (sect_cb == INTENSITY_HCB || sect_cb == INTENSITY_HCB2) && pAacDecoderChannelInfo->pDynData->RawDataInfo.CommonWindow == 0)
357         )
358      {
359        return AAC_DEC_INVALID_CODE_BOOK;
360      }
361
362      /* Store codebook index */
363      for (; band < top; band++)
364      {
365        pCodeBook[group*16+band] = sect_cb;
366      }
367    }
368  }
369
370
371  return ErrorStatus;
372}
373
374/* mso: provides a faster way to i-quantize a whole band in one go */
375
376/**
377 * \brief inverse quantize one sfb. Each value of the sfb is processed according to the
378 *        formula: spectrum[i] = Sign(spectrum[i]) * Matissa(spectrum[i])^(4/3) * 2^(lsb/4).
379 * \param spectrum pointer to first line of the sfb to be inverse quantized.
380 * \param noLines number of lines belonging to the sfb.
381 * \param lsb last 2 bits of the scale factor of the sfb.
382 * \param scale max allowed shift scale for the sfb.
383 */
384static
385void InverseQuantizeBand( FIXP_DBL * RESTRICT spectrum,
386                              INT noLines,
387                              INT lsb,
388                              INT scale )
389{
390    const FIXP_DBL * RESTRICT InverseQuantTabler=(FIXP_DBL *)InverseQuantTable;
391    const FIXP_DBL * RESTRICT MantissaTabler=(FIXP_DBL *)MantissaTable[lsb];
392    const SCHAR* RESTRICT ExponentTabler=(SCHAR*)ExponentTable[lsb];
393
394    FIXP_DBL *ptr = spectrum;
395    FIXP_DBL signedValue;
396
397    FDK_ASSERT(noLines>2);
398    for (INT i=noLines; i--; )
399    {
400        if ((signedValue = *ptr++) != FL2FXCONST_DBL(0))
401        {
402          FIXP_DBL value = fAbs(signedValue);
403          UINT freeBits = CntLeadingZeros(value);
404          UINT exponent = 32 - freeBits;
405
406          UINT x = (UINT) (LONG)value << (INT) freeBits;
407          x <<= 1;                                  /* shift out sign bit to avoid masking later on */
408          UINT tableIndex = x >> 24;
409          x = (x >> 20) &  0x0F;
410
411          UINT r0=(UINT)(LONG)InverseQuantTabler[tableIndex+0];
412          UINT r1=(UINT)(LONG)InverseQuantTabler[tableIndex+1];
413          UINT temp= (r1 - r0)*x + (r0 << 4);
414
415          value = fMultDiv2((FIXP_DBL)temp, MantissaTabler[exponent]);
416
417          /* + 1 compensates fMultDiv2() */
418          scaleValueInPlace(&value, scale + ExponentTabler[exponent] + 1);
419
420          signedValue = (signedValue < (FIXP_DBL)0) ? -value : value;
421          ptr[-1] = signedValue;
422        }
423    }
424}
425
426AAC_DECODER_ERROR CBlock_InverseQuantizeSpectralData(CAacDecoderChannelInfo *pAacDecoderChannelInfo, SamplingRateInfo *pSamplingRateInfo)
427{
428  int window, group, groupwin, band;
429  int ScaleFactorBandsTransmitted = GetScaleFactorBandsTransmitted(&pAacDecoderChannelInfo->icsInfo);
430  UCHAR *RESTRICT pCodeBook = pAacDecoderChannelInfo->pDynData->aCodeBook;
431  SHORT *RESTRICT pSfbScale = pAacDecoderChannelInfo->pDynData->aSfbScale;
432  SHORT *RESTRICT pScaleFactor = pAacDecoderChannelInfo->pDynData->aScaleFactor;
433  const SHORT *RESTRICT BandOffsets = GetScaleFactorBandOffsets(&pAacDecoderChannelInfo->icsInfo, pSamplingRateInfo);
434
435  FDKmemclear(pAacDecoderChannelInfo->pDynData->aSfbScale, (8*16)*sizeof(SHORT));
436
437  for (window=0, group=0; group < GetWindowGroups(&pAacDecoderChannelInfo->icsInfo); group++)
438  {
439    for (groupwin=0; groupwin < GetWindowGroupLength(&pAacDecoderChannelInfo->icsInfo,group); groupwin++, window++)
440    {
441      /* inverse quantization */
442      for (band=0; band < ScaleFactorBandsTransmitted; band++)
443      {
444        FIXP_DBL *pSpectralCoefficient = SPEC(pAacDecoderChannelInfo->pSpectralCoefficient, window, pAacDecoderChannelInfo->granuleLength) + BandOffsets[band];
445
446        int noLines = BandOffsets[band+1] - BandOffsets[band];
447        int bnds = group*16+band;
448        int i;
449
450        if ((pCodeBook[bnds] == ZERO_HCB)
451         || (pCodeBook[bnds] == INTENSITY_HCB)
452         || (pCodeBook[bnds] == INTENSITY_HCB2)
453           )
454          continue;
455
456        if (pCodeBook[bnds] == NOISE_HCB)
457        {
458          /* Leave headroom for PNS values. + 1 because ceil(log2(2^(0.25*3))) = 1,
459             worst case of additional headroom required because of the scalefactor. */
460          pSfbScale[window*16+band] = (pScaleFactor [bnds] >> 2) + 1 ;
461          continue;
462        }
463
464        /* Find max spectral line value of the current sfb */
465        FIXP_DBL locMax = (FIXP_DBL)0;
466
467        for (i = noLines; i-- ; ) {
468          /* Expensive memory access */
469          locMax = fMax(fixp_abs(pSpectralCoefficient[i]), locMax);
470        }
471
472        /* Cheap robustness improvement - Do not remove!!! */
473        if (fixp_abs(locMax) > (FIXP_DBL)MAX_QUANTIZED_VALUE) {
474          return AAC_DEC_DECODE_FRAME_ERROR;
475        }
476
477        /*
478           The inverse quantized spectral lines are defined by:
479        pSpectralCoefficient[i] = Sign(pSpectralCoefficient[i]) * 2^(0.25*pScaleFactor[bnds]) * pSpectralCoefficient[i]^(4/3)
480           This is equivalent to:
481        pSpectralCoefficient[i]    = Sign(pSpectralCoefficient[i]) * (2^(pScaleFactor[bnds] % 4) * pSpectralCoefficient[i]^(4/3))
482        pSpectralCoefficient_e[i] += pScaleFactor[bnds]/4
483        */
484        {
485          int msb = pScaleFactor [bnds] >> 2 ;
486          int lsb = pScaleFactor [bnds] & 0x03 ;
487
488          int scale = GetScaleFromValue(locMax, lsb);
489
490          pSfbScale[window*16+band] = msb - scale;
491          InverseQuantizeBand(pSpectralCoefficient, noLines, lsb, scale);
492        }
493      }
494    }
495  }
496
497
498  return AAC_DEC_OK;
499}
500
501
502AAC_DECODER_ERROR  CBlock_ReadSpectralData(HANDLE_FDK_BITSTREAM bs,
503                                           CAacDecoderChannelInfo *pAacDecoderChannelInfo,
504                                           const SamplingRateInfo *pSamplingRateInfo,
505                                           const UINT  flags)
506{
507  int i,index;
508  int window,group,groupwin,groupoffset,band;
509  UCHAR *RESTRICT pCodeBook = pAacDecoderChannelInfo->pDynData->aCodeBook;
510  const SHORT *RESTRICT BandOffsets = GetScaleFactorBandOffsets(&pAacDecoderChannelInfo->icsInfo, pSamplingRateInfo);
511
512  SPECTRAL_PTR pSpectralCoefficient = pAacDecoderChannelInfo->pSpectralCoefficient;
513  FIXP_DBL locMax;
514
515  int ScaleFactorBandsTransmitted = GetScaleFactorBandsTransmitted(&pAacDecoderChannelInfo->icsInfo);
516
517  FDK_ASSERT(BandOffsets != NULL);
518
519  FDKmemclear(pSpectralCoefficient, sizeof(SPECTRUM));
520
521  if ( (flags & AC_ER_HCR) == 0 )
522  {
523    groupoffset = 0;
524
525    /* plain huffman decoder  short */
526    for (group=0; group < GetWindowGroups(&pAacDecoderChannelInfo->icsInfo); group++)
527    {
528      for (band=0; band < ScaleFactorBandsTransmitted; band++)
529      {
530        int bnds = group*16+band;
531        UCHAR currentCB = pCodeBook[bnds];
532
533        /* patch to run plain-huffman-decoder with vcb11 input codebooks (LAV-checking might be possible below using the virtual cb and a LAV-table) */
534        if ((currentCB >= 16) && (currentCB <= 31)) {
535          pCodeBook[bnds] = currentCB = 11;
536        }
537        if ( !((currentCB == ZERO_HCB)
538            || (currentCB == NOISE_HCB)
539            || (currentCB == INTENSITY_HCB)
540            || (currentCB == INTENSITY_HCB2)) )
541        {
542          const CodeBookDescription *hcb = &AACcodeBookDescriptionTable[currentCB];
543          int step = hcb->Dimension;
544          int offset = hcb->Offset;
545          int bits = hcb->numBits;
546          int mask = (1<<bits)-1;
547
548          for (groupwin=0; groupwin < GetWindowGroupLength(&pAacDecoderChannelInfo->icsInfo,group); groupwin++)
549          {
550            window = groupoffset + groupwin;
551
552            FIXP_DBL *mdctSpectrum = SPEC(pSpectralCoefficient, window, pAacDecoderChannelInfo->granuleLength);
553
554            locMax = (FIXP_DBL)0 ;
555
556            for (index=BandOffsets[band]; index < BandOffsets[band+1]; index+=step)
557            {
558              int idx = CBlock_DecodeHuffmanWord(bs,hcb);
559
560              for (i=0; i<step; i++) {
561                FIXP_DBL tmp;
562
563                tmp = (FIXP_DBL)((idx & mask)-offset);
564                idx >>= bits;
565
566                if (offset == 0) {
567                  if (tmp != FIXP_DBL(0))
568                    tmp = (FDKreadBits(bs,1))? -tmp : tmp;
569                }
570                mdctSpectrum[index+i] = tmp;
571              }
572
573              if (currentCB == ESCBOOK)
574              {
575                mdctSpectrum[index+0] = (FIXP_DBL)CBlock_GetEscape(bs, (LONG)mdctSpectrum[index+0]);
576                mdctSpectrum[index+1] = (FIXP_DBL)CBlock_GetEscape(bs, (LONG)mdctSpectrum[index+1]);
577
578              }
579            }
580          }
581        }
582      }
583      groupoffset += GetWindowGroupLength(&pAacDecoderChannelInfo->icsInfo,group);
584    }
585    /* plain huffman decoding (short) finished */
586  }
587  /* HCR - Huffman Codeword Reordering  short */
588  else  /* if ( flags & AC_ER_HCR ) */
589  {
590    H_HCR_INFO hHcr = &pAacDecoderChannelInfo->pComData->overlay.aac.erHcrInfo;
591    int hcrStatus = 0;
592
593    /* advanced Huffman decoding starts here (HCR decoding :) */
594    if ( pAacDecoderChannelInfo->pDynData->specificTo.aac.lenOfReorderedSpectralData != 0 ) {
595
596      /* HCR initialization short */
597      hcrStatus = HcrInit(hHcr, pAacDecoderChannelInfo, pSamplingRateInfo, bs);
598
599      if (hcrStatus != 0) {
600        return AAC_DEC_DECODE_FRAME_ERROR;
601      }
602
603      /* HCR decoding short */
604      hcrStatus = HcrDecoder(hHcr, pAacDecoderChannelInfo, pSamplingRateInfo, bs);
605
606      if (hcrStatus != 0) {
607#if HCR_ERROR_CONCEALMENT
608        HcrMuteErroneousLines(hHcr);
609#else
610        return AAC_DEC_DECODE_FRAME_ERROR;
611#endif /* HCR_ERROR_CONCEALMENT */
612      }
613
614      FDKpushFor (bs, pAacDecoderChannelInfo->pDynData->specificTo.aac.lenOfReorderedSpectralData);
615    }
616  }
617  /* HCR - Huffman Codeword Reordering short finished */
618
619
620
621  if ( IsLongBlock(&pAacDecoderChannelInfo->icsInfo) && !(flags & (AC_ELD|AC_SCALABLE)) )
622  {
623    /* apply pulse data */
624    CPulseData_Apply(&pAacDecoderChannelInfo->pDynData->specificTo.aac.PulseData,
625                      GetScaleFactorBandOffsets(&pAacDecoderChannelInfo->icsInfo, pSamplingRateInfo),
626                      SPEC_LONG(pSpectralCoefficient));
627  }
628
629
630  return AAC_DEC_OK;
631}
632
633
634
635void ApplyTools ( CAacDecoderChannelInfo *pAacDecoderChannelInfo[],
636                  const SamplingRateInfo *pSamplingRateInfo,
637                  const UINT flags,
638                  const int channel )
639{
640
641  if ( !(flags & (AC_USAC|AC_RSVD50|AC_MPS_RES)) ) {
642    CPns_Apply(
643           &pAacDecoderChannelInfo[channel]->data.aac.PnsData,
644           &pAacDecoderChannelInfo[channel]->icsInfo,
645            pAacDecoderChannelInfo[channel]->pSpectralCoefficient,
646            pAacDecoderChannelInfo[channel]->specScale,
647            pAacDecoderChannelInfo[channel]->pDynData->aScaleFactor,
648            pSamplingRateInfo,
649            pAacDecoderChannelInfo[channel]->granuleLength,
650            channel
651            );
652  }
653
654  CTns_Apply (
655         &pAacDecoderChannelInfo[channel]->pDynData->TnsData,
656         &pAacDecoderChannelInfo[channel]->icsInfo,
657          pAacDecoderChannelInfo[channel]->pSpectralCoefficient,
658          pSamplingRateInfo,
659          pAacDecoderChannelInfo[channel]->granuleLength
660          );
661}
662
663static
664int getWindow2Nr(int length, int shape)
665{
666  int nr = 0;
667
668  if (shape == 2) {
669    /* Low Overlap, 3/4 zeroed */
670    nr = (length * 3)>>2;
671  }
672
673  return nr;
674}
675
676void CBlock_FrequencyToTime(CAacDecoderStaticChannelInfo *pAacDecoderStaticChannelInfo,
677                            CAacDecoderChannelInfo *pAacDecoderChannelInfo,
678                            INT_PCM outSamples[],
679                            const SHORT frameLen,
680                            const int stride,
681                            const int frameOk,
682                            FIXP_DBL *pWorkBuffer1 )
683{
684  int fr, fl, tl, nSamples, nSpec;
685
686  /* Determine left slope length (fl), right slope length (fr) and transform length (tl).
687     USAC: The slope length may mismatch with the previous frame in case of LPD / FD
688           transitions. The adjustment is handled by the imdct implementation.
689  */
690  tl = frameLen;
691  nSpec = 1;
692
693  switch( pAacDecoderChannelInfo->icsInfo.WindowSequence ) {
694    default:
695    case OnlyLongSequence:
696      fl = frameLen;
697      fr = frameLen - getWindow2Nr(frameLen, GetWindowShape(&pAacDecoderChannelInfo->icsInfo));
698      break;
699    case LongStopSequence:
700      fl = frameLen >> 3;
701      fr = frameLen;
702      break;
703    case LongStartSequence: /* or StopStartSequence */
704      fl = frameLen;
705      fr = frameLen >> 3;
706      break;
707    case EightShortSequence:
708      fl = fr = frameLen >> 3;
709      tl >>= 3;
710      nSpec = 8;
711      break;
712  }
713
714  {
715    int i;
716
717    {
718      FIXP_DBL *tmp = pAacDecoderChannelInfo->pComData->workBufferCore1->mdctOutTemp;
719
720      nSamples = imdct_block(
721             &pAacDecoderStaticChannelInfo->IMdct,
722              tmp,
723              SPEC_LONG(pAacDecoderChannelInfo->pSpectralCoefficient),
724              pAacDecoderChannelInfo->specScale,
725              nSpec,
726              frameLen,
727              tl,
728              FDKgetWindowSlope(fl, GetWindowShape(&pAacDecoderChannelInfo->icsInfo)),
729              fl,
730              FDKgetWindowSlope(fr, GetWindowShape(&pAacDecoderChannelInfo->icsInfo)),
731              fr,
732              (FIXP_DBL)0 );
733
734      for (i=0; i<frameLen; i++) {
735        outSamples[i*stride] = IMDCT_SCALE(tmp[i]);
736      }
737    }
738  }
739
740  FDK_ASSERT(nSamples == frameLen);
741
742}
743
744#include "ldfiltbank.h"
745void CBlock_FrequencyToTimeLowDelay( CAacDecoderStaticChannelInfo *pAacDecoderStaticChannelInfo,
746                                     CAacDecoderChannelInfo *pAacDecoderChannelInfo,
747                                     INT_PCM outSamples[],
748                                     const short frameLen,
749                                     const char stride )
750{
751  InvMdctTransformLowDelay_fdk (
752          SPEC_LONG(pAacDecoderChannelInfo->pSpectralCoefficient),
753          pAacDecoderChannelInfo->specScale[0],
754          outSamples,
755          pAacDecoderStaticChannelInfo->pOverlapBuffer,
756          stride,
757          frameLen
758          );
759}
760