1
2/* -----------------------------------------------------------------------------------------------------------
3Software License for The Fraunhofer FDK AAC Codec Library for Android
4
5� Copyright  1995 - 2013 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:
88
89******************************************************************************/
90
91#include "channel.h"
92#include "aacdecoder.h"
93#include "block.h"
94#include "aacdec_tns.h"
95#include "FDK_bitstream.h"
96#include "FDK_tools_rom.h"
97
98#include "conceal.h"
99
100#include "rvlc.h"
101
102#include "aacdec_hcr.h"
103
104
105static
106void MapMidSideMaskToPnsCorrelation (CAacDecoderChannelInfo *pAacDecoderChannelInfo[2])
107{
108  int group;
109
110  for (group = 0 ; group < pAacDecoderChannelInfo[L]->icsInfo.WindowGroups; group++) {
111    UCHAR groupMask = 1 << group;
112
113    for (UCHAR band = 0 ; band < pAacDecoderChannelInfo[L]->icsInfo.MaxSfBands; band++) {
114      if (pAacDecoderChannelInfo[L]->pComData->jointStereoData.MsUsed[band] & groupMask) { /* channels are correlated */
115        CPns_SetCorrelation(&pAacDecoderChannelInfo[L]->data.aac.PnsData, group, band, 0);
116
117        if (CPns_IsPnsUsed(&pAacDecoderChannelInfo[L]->data.aac.PnsData, group, band) &&
118            CPns_IsPnsUsed(&pAacDecoderChannelInfo[R]->data.aac.PnsData, group, band))
119          pAacDecoderChannelInfo[L]->pComData->jointStereoData.MsUsed[band] ^= groupMask; /* clear the groupMask-bit */
120      }
121    }
122  }
123}
124
125/*!
126  \brief Decode channel pair element
127
128  The function decodes a channel pair element.
129
130  \return  none
131*/
132void CChannelElement_Decode( CAacDecoderChannelInfo *pAacDecoderChannelInfo[2], /*!< pointer to aac decoder channel info */
133                             CAacDecoderStaticChannelInfo *pAacDecoderStaticChannelInfo[2],
134                             SamplingRateInfo *pSamplingRateInfo,
135                             UINT  flags,
136                             int el_channels)
137{
138  int ch, maybe_jstereo = 0;
139
140  maybe_jstereo = (el_channels > 1);
141
142  for (ch = 0; ch < el_channels; ch++) {
143    if ( pAacDecoderChannelInfo[ch]->renderMode == AACDEC_RENDER_IMDCT
144      || pAacDecoderChannelInfo[ch]->renderMode == AACDEC_RENDER_ELDFB )
145    {
146      CBlock_InverseQuantizeSpectralData(pAacDecoderChannelInfo[ch], pSamplingRateInfo);
147    }
148  }
149
150
151
152  if (maybe_jstereo) {
153    /* apply ms */
154    if (pAacDecoderChannelInfo[L]->pDynData->RawDataInfo.CommonWindow) {
155      int maxSfBandsL = GetScaleFactorBandsTransmitted(&pAacDecoderChannelInfo[L]->icsInfo);
156      int maxSfBandsR = GetScaleFactorBandsTransmitted(&pAacDecoderChannelInfo[R]->icsInfo);
157      if (pAacDecoderChannelInfo[L]->data.aac.PnsData.PnsActive || pAacDecoderChannelInfo[R]->data.aac.PnsData.PnsActive) {
158        MapMidSideMaskToPnsCorrelation(pAacDecoderChannelInfo);
159      }
160
161      CJointStereo_ApplyMS(pAacDecoderChannelInfo,
162                           GetScaleFactorBandOffsets(&pAacDecoderChannelInfo[L]->icsInfo, pSamplingRateInfo),
163                           GetWindowGroupLengthTable(&pAacDecoderChannelInfo[L]->icsInfo),
164                           GetWindowGroups(&pAacDecoderChannelInfo[L]->icsInfo),
165                           maxSfBandsL,
166                           maxSfBandsR);
167    }
168
169    /* apply intensity stereo */ /* modifies pAacDecoderChannelInfo[]->aSpecSfb */
170    CJointStereo_ApplyIS(pAacDecoderChannelInfo,
171                         GetScaleFactorBandOffsets(&pAacDecoderChannelInfo[L]->icsInfo, pSamplingRateInfo),
172                         GetWindowGroupLengthTable(&pAacDecoderChannelInfo[L]->icsInfo),
173                         GetWindowGroups(&pAacDecoderChannelInfo[L]->icsInfo),
174                         GetScaleFactorBandsTransmitted(&pAacDecoderChannelInfo[L]->icsInfo),
175                         pAacDecoderChannelInfo[L]->pDynData->RawDataInfo.CommonWindow ? 1 : 0);
176
177  }
178
179  for (ch = 0; ch < el_channels; ch++)
180  {
181    {
182      /* write pAacDecoderChannelInfo[ch]->specScale */
183      CBlock_ScaleSpectralData(pAacDecoderChannelInfo[ch], pSamplingRateInfo);
184
185      ApplyTools (pAacDecoderChannelInfo, pSamplingRateInfo, flags, ch);
186    }
187
188  }
189
190  CRvlc_ElementCheck(
191          pAacDecoderChannelInfo,
192          pAacDecoderStaticChannelInfo,
193          flags,
194          el_channels
195          );
196}
197
198void CChannel_CodebookTableInit(CAacDecoderChannelInfo *pAacDecoderChannelInfo)
199{
200  int b, w, maxBands, maxWindows;
201  int maxSfb = GetScaleFactorBandsTransmitted(&pAacDecoderChannelInfo->icsInfo);
202  UCHAR *pCodeBook = pAacDecoderChannelInfo->pDynData->aCodeBook;
203
204  if ( IsLongBlock(&pAacDecoderChannelInfo->icsInfo) ) {
205    maxBands = 64;
206    maxWindows = 1;
207  } else {
208    maxBands = 16;
209    maxWindows = 8;
210  }
211
212  for (w = 0; w<maxWindows; w++) {
213    for (b = 0; b < maxSfb; b++) {
214      pCodeBook[b] = ESCBOOK;
215    }
216    for (; b<maxBands; b++) {
217      pCodeBook[b] = ZERO_HCB;
218    }
219    pCodeBook += maxBands;
220  }
221}
222
223
224/*
225 * Arbitrary order bitstream parser
226 */
227
228AAC_DECODER_ERROR CChannelElement_Read(HANDLE_FDK_BITSTREAM hBs,
229                                       CAacDecoderChannelInfo *pAacDecoderChannelInfo[],
230                                       CAacDecoderStaticChannelInfo *pAacDecoderStaticChannelInfo[],
231                                       const AUDIO_OBJECT_TYPE aot,
232                                       const SamplingRateInfo *pSamplingRateInfo,
233                                       const UINT  flags,
234                                       const UINT  frame_length,
235                                       const UCHAR numberOfChannels,
236                                       const SCHAR epConfig,
237                                       HANDLE_TRANSPORTDEC pTpDec
238                                       )
239{
240  AAC_DECODER_ERROR error = AAC_DEC_OK;
241  const element_list_t *list;
242  int i, ch, decision_bit;
243  int crcReg1 = -1, crcReg2 = -1;
244
245  FDK_ASSERT( (numberOfChannels == 1) || (numberOfChannels == 2) );
246
247  /* Get channel element sequence table */
248  list = getBitstreamElementList(aot, epConfig, numberOfChannels, 0);
249  if (list == NULL) {
250    error = AAC_DEC_UNSUPPORTED_FORMAT;
251    goto bail;
252  }
253
254  CTns_Reset(&pAacDecoderChannelInfo[0]->pDynData->TnsData);
255  if (numberOfChannels == 2) {
256    CTns_Reset(&pAacDecoderChannelInfo[1]->pDynData->TnsData);
257  }
258
259  if (flags & (AC_ELD|AC_SCALABLE)) {
260    pAacDecoderChannelInfo[0]->pDynData->RawDataInfo.CommonWindow = 1;
261    if (numberOfChannels == 2) {
262      pAacDecoderChannelInfo[1]->pDynData->RawDataInfo.CommonWindow = pAacDecoderChannelInfo[0]->pDynData->RawDataInfo.CommonWindow;
263    }
264    if (numberOfChannels == 2) {
265      pAacDecoderChannelInfo[1]->pDynData->RawDataInfo.CommonWindow = pAacDecoderChannelInfo[0]->pDynData->RawDataInfo.CommonWindow;
266    }
267  }
268
269  /* Iterate through sequence table */
270  i = 0;
271  ch = 0;
272  decision_bit = 0;
273  do {
274    switch (list->id[i]) {
275    case element_instance_tag:
276      pAacDecoderChannelInfo[0]->ElementInstanceTag = FDKreadBits(hBs, 4);
277      if (numberOfChannels == 2) {
278        pAacDecoderChannelInfo[1]->ElementInstanceTag = pAacDecoderChannelInfo[0]->ElementInstanceTag;
279      }
280      break;
281    case common_window:
282      decision_bit = pAacDecoderChannelInfo[ch]->pDynData->RawDataInfo.CommonWindow = FDKreadBits(hBs, 1);
283      if (numberOfChannels == 2) {
284        pAacDecoderChannelInfo[1]->pDynData->RawDataInfo.CommonWindow = pAacDecoderChannelInfo[0]->pDynData->RawDataInfo.CommonWindow;
285      }
286      break;
287    case ics_info:
288      /* Read individual channel info */
289      error = IcsRead( hBs,
290                      &pAacDecoderChannelInfo[ch]->icsInfo,
291                       pSamplingRateInfo,
292                       flags );
293
294      if (numberOfChannels == 2 && pAacDecoderChannelInfo[0]->pDynData->RawDataInfo.CommonWindow) {
295        pAacDecoderChannelInfo[1]->icsInfo = pAacDecoderChannelInfo[0]->icsInfo;
296      }
297      break;
298
299
300    case ltp_data_present:
301      if (FDKreadBits(hBs, 1) != 0) {
302        error = AAC_DEC_UNSUPPORTED_PREDICTION;
303      }
304      break;
305
306    case ms:
307      if ( CJointStereo_Read(
308              hBs,
309             &pAacDecoderChannelInfo[0]->pComData->jointStereoData,
310              GetWindowGroups(&pAacDecoderChannelInfo[0]->icsInfo),
311              GetScaleMaxFactorBandsTransmitted(&pAacDecoderChannelInfo[0]->icsInfo,
312                                                &pAacDecoderChannelInfo[1]->icsInfo),
313              flags) )
314      {
315        error = AAC_DEC_PARSE_ERROR;
316      }
317      break;
318
319    case global_gain:
320      pAacDecoderChannelInfo[ch]->pDynData->RawDataInfo.GlobalGain = (UCHAR) FDKreadBits(hBs,8);
321      break;
322
323    case section_data:
324      error = CBlock_ReadSectionData( hBs,
325                                      pAacDecoderChannelInfo[ch],
326                                      pSamplingRateInfo,
327                                      flags );
328      break;
329
330
331    case scale_factor_data:
332      if (flags & AC_ER_RVLC) {
333        /* read RVLC data from bitstream (error sens. cat. 1) */
334        CRvlc_Read(pAacDecoderChannelInfo[ch], hBs);
335      }
336      else
337      {
338        error = CBlock_ReadScaleFactorData(pAacDecoderChannelInfo[ch], hBs, flags);
339      }
340      break;
341
342    case pulse:
343      if ( CPulseData_Read( hBs,
344                           &pAacDecoderChannelInfo[ch]->pDynData->specificTo.aac.PulseData,
345                            pSamplingRateInfo->ScaleFactorBands_Long, /* pulse data is only allowed to be present in long blocks! */
346                            (void*)&pAacDecoderChannelInfo[ch]->icsInfo,
347                            frame_length
348                          ) != 0 )
349      {
350        error = AAC_DEC_DECODE_FRAME_ERROR;
351      }
352      break;
353    case tns_data_present:
354      CTns_ReadDataPresentFlag(hBs, &pAacDecoderChannelInfo[ch]->pDynData->TnsData);
355      break;
356    case tns_data:
357      /* tns_data_present is checked inside CTns_Read(). */
358      error = CTns_Read(hBs, &pAacDecoderChannelInfo[ch]->pDynData->TnsData, &pAacDecoderChannelInfo[ch]->icsInfo, flags);
359      break;
360
361    case gain_control_data:
362      break;
363
364    case gain_control_data_present:
365      if (FDKreadBits(hBs, 1)) {
366        error = AAC_DEC_UNSUPPORTED_GAIN_CONTROL_DATA;
367      }
368      break;
369
370    case esc2_rvlc:
371      if (flags & AC_ER_RVLC) {
372        CRvlc_Decode(
373                pAacDecoderChannelInfo[ch],
374                pAacDecoderStaticChannelInfo[ch],
375                hBs
376                );
377      }
378      break;
379
380    case esc1_hcr:
381      if (flags & AC_ER_HCR) {
382        CHcr_Read(hBs, pAacDecoderChannelInfo[ch] );
383      }
384      break;
385
386    case spectral_data:
387      error = CBlock_ReadSpectralData( hBs,
388                                       pAacDecoderChannelInfo[ch],
389                                       pSamplingRateInfo,
390                                       flags );
391      if (flags & AC_ELD) {
392        pAacDecoderChannelInfo[ch]->renderMode = AACDEC_RENDER_ELDFB;
393      } else {
394        pAacDecoderChannelInfo[ch]->renderMode = AACDEC_RENDER_IMDCT;
395      }
396      break;
397
398
399      /* CRC handling */
400    case adtscrc_start_reg1:
401      if (pTpDec != NULL) {
402        crcReg1 = transportDec_CrcStartReg(pTpDec, 192);
403      }
404      break;
405    case adtscrc_start_reg2:
406      if (pTpDec != NULL) {
407        crcReg2 = transportDec_CrcStartReg(pTpDec, 128);
408      }
409      break;
410    case adtscrc_end_reg1:
411    case drmcrc_end_reg:
412      if (pTpDec != NULL) {
413        transportDec_CrcEndReg(pTpDec, crcReg1);
414      }
415      break;
416    case adtscrc_end_reg2:
417      if (pTpDec != NULL) {
418        transportDec_CrcEndReg(pTpDec, crcReg2);
419      }
420      break;
421    case drmcrc_start_reg:
422      if (pTpDec != NULL) {
423        crcReg1 = transportDec_CrcStartReg(pTpDec, 0);
424      }
425      break;
426
427      /* Non data cases */
428    case next_channel:
429      ch = (ch + 1) % numberOfChannels;
430      break;
431    case link_sequence:
432      list = list->next[decision_bit];
433      i=-1;
434      break;
435
436    default:
437      error = AAC_DEC_UNSUPPORTED_FORMAT;
438      break;
439    }
440
441    if (error != AAC_DEC_OK) {
442      goto bail;
443    }
444
445    i++;
446
447  } while (list->id[i] != end_of_sequence);
448
449bail:
450  return error;
451}
452