tpdec_asc.cpp revision 2ddc922da87bb675b8ab8c305566436e806df0d9
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):   Daniel Homm
87   Description:
88
89******************************************************************************/
90
91#include "tpdec_lib.h"
92#include "tp_data.h"
93
94
95void CProgramConfig_Reset(CProgramConfig *pPce)
96{
97  pPce->elCounter = 0;
98}
99
100void CProgramConfig_Init(CProgramConfig *pPce)
101{
102  FDKmemclear(pPce, sizeof(CProgramConfig));
103#ifdef TP_PCE_ENABLE
104  pPce->SamplingFrequencyIndex = 0xf;
105#endif
106}
107
108int  CProgramConfig_IsValid ( const CProgramConfig *pPce )
109{
110  return ( (pPce->isValid) ? 1 : 0);
111}
112
113#ifdef TP_PCE_ENABLE
114void CProgramConfig_Read(
115                          CProgramConfig *pPce,
116                          HANDLE_FDK_BITSTREAM bs,
117                          UINT alignmentAnchor
118                        )
119{
120  int i;
121
122  pPce->NumEffectiveChannels = 0;
123  pPce->NumChannels = 0;
124  pPce->ElementInstanceTag = (UCHAR) FDKreadBits(bs,4);
125  pPce->Profile = (UCHAR) FDKreadBits(bs,2);
126  pPce->SamplingFrequencyIndex = (UCHAR) FDKreadBits(bs,4);
127  pPce->NumFrontChannelElements = (UCHAR) FDKreadBits(bs,4);
128  pPce->NumSideChannelElements = (UCHAR) FDKreadBits(bs,4);
129  pPce->NumBackChannelElements = (UCHAR) FDKreadBits(bs,4);
130  pPce->NumLfeChannelElements = (UCHAR) FDKreadBits(bs,2);
131  pPce->NumAssocDataElements = (UCHAR) FDKreadBits(bs,3);
132  pPce->NumValidCcElements = (UCHAR) FDKreadBits(bs,4);
133
134  if ((pPce->MonoMixdownPresent = (UCHAR) FDKreadBits(bs,1)) != 0)
135  {
136    pPce->MonoMixdownElementNumber = (UCHAR) FDKreadBits(bs,4);
137  }
138
139  if ((pPce->StereoMixdownPresent = (UCHAR) FDKreadBits(bs,1)) != 0)
140  {
141    pPce->StereoMixdownElementNumber = (UCHAR) FDKreadBits(bs,4);
142  }
143
144  if ((pPce->MatrixMixdownIndexPresent = (UCHAR) FDKreadBits(bs,1)) != 0)
145  {
146    pPce->MatrixMixdownIndex = (UCHAR) FDKreadBits(bs,2);
147    pPce->PseudoSurroundEnable = (UCHAR) FDKreadBits(bs,1);
148  }
149
150  for (i=0; i < pPce->NumFrontChannelElements; i++)
151  {
152    pPce->FrontElementIsCpe[i] = (UCHAR) FDKreadBits(bs,1);
153    pPce->FrontElementTagSelect[i] = (UCHAR) FDKreadBits(bs,4);
154    pPce->NumChannels += pPce->FrontElementIsCpe[i] ? 2 : 1;
155  }
156
157  for (i=0; i < pPce->NumSideChannelElements; i++)
158  {
159    pPce->SideElementIsCpe[i] = (UCHAR) FDKreadBits(bs,1);
160    pPce->SideElementTagSelect[i] = (UCHAR) FDKreadBits(bs,4);
161    pPce->NumChannels += pPce->SideElementIsCpe[i] ? 2 : 1;
162  }
163
164  for (i=0; i < pPce->NumBackChannelElements; i++)
165  {
166    pPce->BackElementIsCpe[i] = (UCHAR) FDKreadBits(bs,1);
167    pPce->BackElementTagSelect[i] = (UCHAR) FDKreadBits(bs,4);
168    pPce->NumChannels += pPce->BackElementIsCpe[i] ? 2 : 1;
169  }
170
171  pPce->NumEffectiveChannels = pPce->NumChannels;
172
173  for (i=0; i < pPce->NumLfeChannelElements; i++)
174  {
175    pPce->LfeElementTagSelect[i] = (UCHAR) FDKreadBits(bs,4);
176    pPce->NumChannels += 1;
177  }
178
179  for (i=0; i < pPce->NumAssocDataElements; i++)
180  {
181    pPce->AssocDataElementTagSelect[i] = (UCHAR) FDKreadBits(bs,4);
182  }
183
184  for (i=0; i < pPce->NumValidCcElements; i++)
185  {
186    pPce->CcElementIsIndSw[i] = (UCHAR) FDKreadBits(bs,1);
187    pPce->ValidCcElementTagSelect[i] = (UCHAR) FDKreadBits(bs,4);
188  }
189
190  FDKbyteAlign(bs, alignmentAnchor);
191
192  pPce->CommentFieldBytes = (UCHAR) FDKreadBits(bs,8);
193
194  for (i=0; i < pPce->CommentFieldBytes; i++)
195  {
196    UCHAR text;
197
198    text = (UCHAR)FDKreadBits(bs,8);
199
200    if (i < PC_COMMENTLENGTH)
201    {
202      pPce->Comment[i] = text;
203    }
204  }
205
206  pPce->isValid = 1;
207}
208#endif /* TP_PCE_ENABLE */
209
210/**
211 * \brief get implicit audio channel type for given channelConfig and MPEG ordered channel index
212 * \param channelConfig MPEG channelConfiguration from 1 upto 7
213 * \param index MPEG channel order index
214 * \return audio channel type.
215 */
216void getImplicitAudioChannelTypeAndIndex(
217        AUDIO_CHANNEL_TYPE *chType,
218        UCHAR *chIndex,
219        UINT channelConfig,
220        UINT index
221        )
222{
223  if (index < 3) {
224    *chType = ACT_FRONT;
225    *chIndex = index;
226  } else {
227    switch (channelConfig) {
228      case MODE_1_2_1:
229      case MODE_1_2_2:
230      case MODE_1_2_2_1:
231        switch (index) {
232          case 3:
233          case 4:
234            *chType = ACT_BACK;
235            *chIndex = index - 3;
236            break;
237          case 5:
238            *chType = ACT_LFE;
239            *chIndex = 0;
240            break;
241        }
242        break;
243      case MODE_1_2_2_2_1:
244        switch (index) {
245          case 3:
246          case 4:
247            *chType = ACT_SIDE;
248            *chIndex = index - 3;
249            break;
250          case 5:
251          case 6:
252            *chType = ACT_BACK;
253            *chIndex = index - 5;
254            break;
255          case 7:
256            *chType = ACT_LFE;
257            *chIndex = 0;
258            break;
259        }
260        break;
261      default:
262        *chType = ACT_NONE;
263        break;
264    }
265  }
266}
267
268int CProgramConfig_LookupElement(
269        CProgramConfig *pPce,
270        UINT            channelConfig,
271        const UINT      tag,
272        const UINT      channelIdx,
273        UCHAR           chMapping[],
274        AUDIO_CHANNEL_TYPE chType[],
275        UCHAR           chIndex[],
276        UCHAR          *elMapping,
277        MP4_ELEMENT_ID  elList[],
278        MP4_ELEMENT_ID  elType
279       )
280{
281  if (channelConfig > 0)
282  {
283    /* Constant channel mapping must have
284       been set during initialization. */
285    if ( elType == ID_SCE
286      || elType == ID_CPE
287      || elType == ID_LFE )
288    {
289      *elMapping = pPce->elCounter;
290      if (elList[pPce->elCounter] != elType) {
291        /* Not in the list */
292        if ( (channelConfig == 2) && (elType == ID_SCE) )
293        { /* This scenario occurs with HE-AAC v2 streams of buggy encoders.
294             Due to other decoder implementations decoding of these kind of streams is desired. */
295          channelConfig = 1;
296        } else {
297          return 0;
298        }
299      }
300      /* Assume all front channels */
301      getImplicitAudioChannelTypeAndIndex(&chType[channelIdx], &chIndex[channelIdx], channelConfig, channelIdx);
302      if (elType == ID_CPE) {
303        chType[channelIdx+1] = chType[channelIdx];
304        chIndex[channelIdx+1] = chIndex[channelIdx]+1;
305      }
306      pPce->elCounter++;
307    }
308    /* Accept all non-channel elements, too. */
309    return 1;
310  }
311  else
312  {
313#ifdef TP_PCE_ENABLE
314    if (!pPce->isValid)
315#endif /* TP_PCE_ENABLE */
316    {
317      /* Implicit channel mapping. */
318      if ( elType == ID_SCE
319        || elType == ID_CPE
320        || elType == ID_LFE )
321      {
322        /* Store all channel element IDs */
323        elList[pPce->elCounter] = elType;
324        *elMapping = pPce->elCounter++;
325      }
326    }
327#ifdef  TP_PCE_ENABLE
328    else {
329      /* Accept the additional channel(s), only if the tag is in the lists */
330      int isCpe = 0, i;
331      int cc = 0, fc = 0, sc = 0, bc = 0, lc = 0, ec = 0; /* Channel and element counters */
332
333      switch (elType)
334      {
335      case ID_CPE:
336        isCpe = 1;
337      case ID_SCE:
338        /* search in front channels */
339        for (i = 0; i < pPce->NumFrontChannelElements; i++) {
340          if (isCpe == pPce->FrontElementIsCpe[i] && pPce->FrontElementTagSelect[i] == tag) {
341            chMapping[cc] = channelIdx;
342            chType[cc] = ACT_FRONT;
343            chIndex[cc] = fc;
344            if (isCpe) {
345              chMapping[cc+1] = channelIdx+1;
346              chType[cc+1] = ACT_FRONT;
347              chIndex[cc+1] = fc+1;
348            }
349            *elMapping = ec;
350            return 1;
351          }
352          ec++;
353          if (pPce->FrontElementIsCpe[i]) {
354            cc+=2; fc+=2;
355          } else {
356            cc++; fc++;
357          }
358        }
359        /* search in side channels */
360        for (i = 0; i < pPce->NumSideChannelElements; i++) {
361          if (isCpe == pPce->SideElementIsCpe[i] && pPce->SideElementTagSelect[i] == tag) {
362            chMapping[cc] = channelIdx;
363            chType[cc] = ACT_SIDE;
364            chIndex[cc] = sc;
365            if (isCpe) {
366              chMapping[cc+1] = channelIdx+1;
367              chType[cc+1] = ACT_SIDE;
368              chIndex[cc+1] = sc+1;
369            }
370            *elMapping = ec;
371            return 1;
372          }
373          ec++;
374          if (pPce->SideElementIsCpe[i]) {
375            cc+=2; sc+=2;
376          } else {
377            cc++; sc++;
378          }
379        }
380        /* search in back channels */
381        for (i = 0; i < pPce->NumBackChannelElements; i++) {
382          if (isCpe == pPce->BackElementIsCpe[i] && pPce->BackElementTagSelect[i] == tag) {
383            chMapping[cc] = channelIdx;
384            chType[cc] = ACT_BACK;
385            chIndex[cc] = bc;
386            if (isCpe) {
387              chMapping[cc+1] = channelIdx+1;
388              chType[cc+1] = ACT_BACK;
389              chIndex[cc+1] = bc+1;
390            }
391            *elMapping = ec;
392            return 1;
393          }
394          ec++;
395          if (pPce->BackElementIsCpe[i]) {
396            cc+=2; bc+=2;
397          } else {
398            cc++; bc++;
399          }
400        }
401        break;
402
403      case ID_LFE:
404        /* Initialize channel counter and element counter */
405        cc = pPce->NumEffectiveChannels;
406        ec = pPce->NumFrontChannelElements+ pPce->NumSideChannelElements + pPce->NumBackChannelElements;
407        /* search in lfe channels */
408        for (i = 0; i < pPce->NumLfeChannelElements; i++) {
409          if ( pPce->LfeElementTagSelect[i] == tag ) {
410            chMapping[cc] = channelIdx;
411            *elMapping = ec;
412            chType[cc] = ACT_LFE;
413            chIndex[cc] = lc;
414            return 1;
415          }
416          ec++;
417          cc++;
418          lc++;
419        }
420        break;
421
422      /* Non audio elements */
423      case ID_CCE:
424        /* search in cce channels */
425        for (i = 0; i < pPce->NumValidCcElements; i++) {
426          if (pPce->ValidCcElementTagSelect[i] == tag) {
427            return 1;
428          }
429        }
430        break;
431      case ID_DSE:
432        /* search associated data elements */
433        for (i = 0; i < pPce->NumAssocDataElements; i++) {
434          if (pPce->AssocDataElementTagSelect[i] == tag) {
435            return 1;
436          }
437        }
438        break;
439      default:
440        return 0;
441      }
442      return 0;  /* not found in any list */
443    }
444#endif /* TP_PCE_ENABLE */
445  }
446
447  return 1;
448}
449
450#ifdef  TP_PCE_ENABLE
451int CProgramConfig_GetElementTable(
452        const CProgramConfig *pPce,
453        MP4_ELEMENT_ID  elList[],
454        const INT elListSize
455       )
456{
457  int i, el = 0;
458
459  if ( elListSize
460    < pPce->NumFrontChannelElements + pPce->NumSideChannelElements + pPce->NumBackChannelElements + pPce->NumLfeChannelElements
461    )
462  {
463    return 0;
464  }
465
466  for (i=0; i < pPce->NumFrontChannelElements; i++)
467  {
468    elList[el++] = (pPce->FrontElementIsCpe[i]) ?  ID_CPE : ID_SCE;
469  }
470
471  for (i=0; i < pPce->NumSideChannelElements; i++)
472  {
473    elList[el++] = (pPce->SideElementIsCpe[i]) ?  ID_CPE : ID_SCE;
474  }
475
476  for (i=0; i < pPce->NumBackChannelElements; i++)
477  {
478    elList[el++] = (pPce->BackElementIsCpe[i]) ?  ID_CPE : ID_SCE;
479  }
480
481  for (i=0; i < pPce->NumLfeChannelElements; i++)
482  {
483    elList[el++] = ID_LFE;
484  }
485
486
487  return el;
488}
489#endif
490
491static AUDIO_OBJECT_TYPE getAOT(HANDLE_FDK_BITSTREAM bs)
492{
493  int tmp = 0;
494
495  tmp = FDKreadBits(bs,5);
496  if (tmp == AOT_ESCAPE) {
497    int tmp2 = FDKreadBits(bs,6);
498    tmp = 32 + tmp2;
499  }
500
501  return (AUDIO_OBJECT_TYPE)tmp;
502}
503
504static INT getSampleRate(HANDLE_FDK_BITSTREAM bs, UCHAR *index, int nBits)
505{
506  INT sampleRate;
507  int idx;
508
509  idx = FDKreadBits(bs, nBits);
510  if( idx == (1<<nBits)-1 ) {
511    if(FDKgetValidBits(bs) < 24) {
512      return 0;
513    }
514    sampleRate = FDKreadBits(bs,24);
515  } else {
516    sampleRate = SamplingRateTable[idx];
517  }
518
519  *index = idx;
520
521  return sampleRate;
522}
523
524#ifdef TP_GA_ENABLE
525static
526TRANSPORTDEC_ERROR GaSpecificConfig_Parse( CSGaSpecificConfig    *self,
527                                           CSAudioSpecificConfig *asc,
528                                           HANDLE_FDK_BITSTREAM   bs,
529                                           UINT                   ascStartAnchor )
530{
531  TRANSPORTDEC_ERROR ErrorStatus = TRANSPORTDEC_OK;
532
533  self->m_frameLengthFlag = FDKreadBits(bs,1);
534
535  self->m_dependsOnCoreCoder = FDKreadBits(bs,1);
536
537  if( self->m_dependsOnCoreCoder )
538    self->m_coreCoderDelay = FDKreadBits(bs,14);
539
540  self->m_extensionFlag = FDKreadBits(bs,1);
541
542  if( asc->m_channelConfiguration == 0 ) {
543    CProgramConfig_Read(&asc->m_progrConfigElement, bs, ascStartAnchor);
544  }
545
546  if ((asc->m_aot == AOT_AAC_SCAL) || (asc->m_aot == AOT_ER_AAC_SCAL)) {
547    self->m_layer = FDKreadBits(bs,3);
548  }
549
550  if (self->m_extensionFlag) {
551    if (asc->m_aot == AOT_ER_BSAC) {
552      self->m_numOfSubFrame = FDKreadBits(bs,5);
553      self->m_layerLength   = FDKreadBits(bs,11);
554    }
555
556    if ((asc->m_aot == AOT_ER_AAC_LC)   || (asc->m_aot == AOT_ER_AAC_LTP)  ||
557        (asc->m_aot == AOT_ER_AAC_SCAL) || (asc->m_aot == AOT_ER_AAC_LD))
558    {
559      asc->m_vcb11Flag = FDKreadBits(bs,1); /* aacSectionDataResilienceFlag */
560      asc->m_rvlcFlag  = FDKreadBits(bs,1); /* aacScalefactorDataResilienceFlag */
561      asc->m_hcrFlag   = FDKreadBits(bs,1); /* aacSpectralDataResilienceFlag */
562    }
563
564    self->m_extensionFlag3 = FDKreadBits(bs,1);
565
566  }
567  return (ErrorStatus);
568}
569#endif /* TP_GA_ENABLE */
570
571
572
573
574
575#ifdef TP_ELD_ENABLE
576
577static INT ld_sbr_header( const CSAudioSpecificConfig *asc,
578                           HANDLE_FDK_BITSTREAM hBs,
579                           CSTpCallBacks *cb )
580{
581  const int channelConfiguration = asc->m_channelConfiguration;
582  int i = 0;
583  INT error = 0;
584
585  if (channelConfiguration == 2) {
586    error = cb->cbSbr(cb->cbSbrData, hBs, asc->m_samplingFrequency, asc->m_extensionSamplingFrequency, asc->m_samplesPerFrame, AOT_ER_AAC_ELD, ID_CPE, i++);
587  } else {
588    error = cb->cbSbr(cb->cbSbrData, hBs, asc->m_samplingFrequency, asc->m_extensionSamplingFrequency, asc->m_samplesPerFrame, AOT_ER_AAC_ELD, ID_SCE, i++);
589  }
590
591  switch ( channelConfiguration ) {
592    case 5:
593      error |= cb->cbSbr(cb->cbSbrData, hBs, asc->m_samplingFrequency, asc->m_extensionSamplingFrequency, asc->m_samplesPerFrame, AOT_ER_AAC_ELD, ID_CPE, i++);
594    case 3:
595      error |= cb->cbSbr(cb->cbSbrData, hBs, asc->m_samplingFrequency, asc->m_extensionSamplingFrequency, asc->m_samplesPerFrame, AOT_ER_AAC_ELD, ID_CPE, i++);
596      break;
597
598    case 7:
599      error |= cb->cbSbr(cb->cbSbrData, hBs, asc->m_samplingFrequency, asc->m_extensionSamplingFrequency, asc->m_samplesPerFrame, AOT_ER_AAC_ELD, ID_SCE, i++);
600    case 6:
601      error |= cb->cbSbr(cb->cbSbrData, hBs, asc->m_samplingFrequency, asc->m_extensionSamplingFrequency, asc->m_samplesPerFrame, AOT_ER_AAC_ELD, ID_CPE, i++);
602    case 4:
603      error |= cb->cbSbr(cb->cbSbrData, hBs, asc->m_samplingFrequency, asc->m_extensionSamplingFrequency, asc->m_samplesPerFrame, AOT_ER_AAC_ELD, ID_CPE, i++);
604      break;
605  }
606
607  return error;
608}
609
610static
611TRANSPORTDEC_ERROR EldSpecificConfig_Parse(
612        CSAudioSpecificConfig *asc,
613        HANDLE_FDK_BITSTREAM hBs,
614        CSTpCallBacks *cb
615        )
616{
617  TRANSPORTDEC_ERROR ErrorStatus = TRANSPORTDEC_OK;
618  CSEldSpecificConfig *esc = &asc->m_sc.m_eldSpecificConfig;
619  ASC_ELD_EXT_TYPE eldExtType;
620  int eldExtLen, len, cnt;
621
622  FDKmemclear(esc, sizeof(CSEldSpecificConfig));
623
624  esc->m_frameLengthFlag = FDKreadBits(hBs, 1 );
625  if (esc->m_frameLengthFlag) {
626    asc->m_samplesPerFrame = 480;
627  } else {
628    asc->m_samplesPerFrame = 512;
629  }
630
631  asc->m_vcb11Flag = FDKreadBits(hBs, 1 );
632  asc->m_rvlcFlag  = FDKreadBits(hBs, 1 );
633  asc->m_hcrFlag   = FDKreadBits(hBs, 1 );
634
635  esc->m_sbrPresentFlag     = FDKreadBits(hBs, 1 );
636
637  if (esc->m_sbrPresentFlag == 1) {
638    esc->m_sbrSamplingRate    = FDKreadBits(hBs, 1 ); /* 0: single rate, 1: dual rate */
639    esc->m_sbrCrcFlag         = FDKreadBits(hBs, 1 );
640
641    asc->m_extensionSamplingFrequency = asc->m_samplingFrequency << esc->m_sbrSamplingRate;
642
643    if (cb->cbSbr != NULL){
644      if ( 0 != ld_sbr_header(asc, hBs, cb) ) {
645        return TRANSPORTDEC_PARSE_ERROR;
646      }
647    }
648  }
649  esc->m_useLdQmfTimeAlign = 0;
650
651  /* new ELD syntax */
652  /* parse ExtTypeConfigData */
653  while ((eldExtType = (ASC_ELD_EXT_TYPE)FDKreadBits(hBs, 4 )) != ELDEXT_TERM) {
654    eldExtLen = len = FDKreadBits(hBs, 4 );
655    if ( len == 0xf ) {
656      len = FDKreadBits(hBs, 8 );
657      eldExtLen += len;
658
659      if ( len == 0xff ) {
660        len = FDKreadBits(hBs, 16 );
661        eldExtLen += len;
662      }
663    }
664
665    switch (eldExtType) {
666      case ELDEXT_LDSAC:
667        esc->m_useLdQmfTimeAlign = 1;
668        if (cb->cbSsc != NULL) {
669          ErrorStatus = (TRANSPORTDEC_ERROR)cb->cbSsc(
670                  cb->cbSscData,
671                  hBs,
672                  asc->m_aot,
673                  asc->m_samplingFrequency,
674                  1,  /* muxMode */
675                  len
676                  );
677        } else {
678          ErrorStatus = TRANSPORTDEC_UNSUPPORTED_FORMAT;
679        }
680        if (ErrorStatus != TRANSPORTDEC_OK) {
681          goto bail;
682        }
683        break;
684      default:
685        for(cnt=0; cnt<len; cnt++) {
686          FDKreadBits(hBs, 8 );
687        }
688        break;
689      /* add future eld extension configs here */
690    }
691  }
692bail:
693  return (ErrorStatus);
694}
695#endif /* TP_ELD_ENABLE */
696
697
698
699/*
700 * API Functions
701 */
702
703void AudioSpecificConfig_Init(CSAudioSpecificConfig *asc)
704{
705  FDKmemclear(asc, sizeof(CSAudioSpecificConfig));
706
707  /* Init all values that should not be zero. */
708  asc->m_aot                    = AOT_NONE;
709  asc->m_samplingFrequencyIndex = 0xf;
710  asc->m_epConfig               = -1;
711  asc->m_extensionAudioObjectType        = AOT_NULL_OBJECT;
712#ifdef TP_PCE_ENABLE
713  CProgramConfig_Init(&asc->m_progrConfigElement);
714#endif
715}
716
717TRANSPORTDEC_ERROR AudioSpecificConfig_Parse(
718        CSAudioSpecificConfig *self,
719        HANDLE_FDK_BITSTREAM   bs,
720        int                    fExplicitBackwardCompatible,
721        CSTpCallBacks      *cb
722        )
723{
724  TRANSPORTDEC_ERROR ErrorStatus = TRANSPORTDEC_OK;
725  UINT ascStartAnchor = FDKgetValidBits(bs);
726  int frameLengthFlag = -1;
727
728  AudioSpecificConfig_Init(self);
729
730  self->m_aot = getAOT(bs);
731  self->m_samplingFrequency = getSampleRate(bs, &self->m_samplingFrequencyIndex, 4);
732  if (self->m_samplingFrequency <= 0) {
733    return TRANSPORTDEC_PARSE_ERROR;
734  }
735
736  self->m_channelConfiguration = FDKreadBits(bs,4);
737
738  /* SBR extension ( explicit non-backwards compatible mode ) */
739  self->m_sbrPresentFlag = 0;
740  self->m_psPresentFlag  = 0;
741
742  if ( self->m_aot == AOT_SBR || self->m_aot == AOT_PS ) {
743    self->m_extensionAudioObjectType = AOT_SBR;
744
745    self->m_sbrPresentFlag = 1;
746    if ( self->m_aot == AOT_PS ) {
747      self->m_psPresentFlag = 1;
748    }
749
750    self->m_extensionSamplingFrequency = getSampleRate(bs, &self->m_extensionSamplingFrequencyIndex, 4);
751    self->m_aot = getAOT(bs);
752
753  } else {
754    self->m_extensionAudioObjectType = AOT_NULL_OBJECT;
755  }
756
757  /* Parse whatever specific configs */
758  switch (self->m_aot)
759  {
760#ifdef TP_GA_ENABLE
761    case AOT_AAC_LC:
762    case AOT_ER_AAC_LC:
763    case AOT_ER_AAC_LD:
764    case AOT_ER_AAC_SCAL:
765    case AOT_ER_BSAC:
766      if ((ErrorStatus = GaSpecificConfig_Parse(&self->m_sc.m_gaSpecificConfig, self, bs, ascStartAnchor)) != TRANSPORTDEC_OK ) {
767        return (ErrorStatus);
768      }
769      frameLengthFlag = self->m_sc.m_gaSpecificConfig.m_frameLengthFlag;
770      break;
771#endif /* TP_GA_ENABLE */
772    case AOT_MPEGS:
773      if (cb->cbSsc != NULL) {
774        cb->cbSsc(
775                cb->cbSscData,
776                bs,
777                self->m_aot,
778                self->m_samplingFrequency,
779                1,
780                0  /* don't know the length */
781                );
782      } else {
783        return TRANSPORTDEC_UNSUPPORTED_FORMAT;
784      }
785      break;
786#ifdef TP_ELD_ENABLE
787    case AOT_ER_AAC_ELD:
788      if ((ErrorStatus = EldSpecificConfig_Parse(self, bs, cb)) != TRANSPORTDEC_OK ) {
789        return (ErrorStatus);
790      }
791      frameLengthFlag = self->m_sc.m_eldSpecificConfig.m_frameLengthFlag;
792      self->m_sbrPresentFlag = self->m_sc.m_eldSpecificConfig.m_sbrPresentFlag;
793      self->m_extensionSamplingFrequency = (self->m_sc.m_eldSpecificConfig.m_sbrSamplingRate+1) * self->m_samplingFrequency;
794      break;
795#endif /* TP_ELD_ENABLE */
796
797    default:
798      return TRANSPORTDEC_UNSUPPORTED_FORMAT;
799      break;
800  }
801
802  /* Frame length */
803  switch (self->m_aot)
804  {
805#if defined(TP_GA_ENABLE) || defined(TP_USAC_ENABLE)
806    case AOT_AAC_LC:
807    case AOT_ER_AAC_LC:
808    case AOT_ER_AAC_SCAL:
809    case AOT_ER_BSAC:
810    /*case AOT_USAC:*/
811      if (!frameLengthFlag)
812        self->m_samplesPerFrame = 1024;
813      else
814        self->m_samplesPerFrame = 960;
815      break;
816#endif /* TP_GA_ENABLE */
817#if defined(TP_GA_ENABLE)
818    case AOT_ER_AAC_LD:
819      if (!frameLengthFlag)
820        self->m_samplesPerFrame = 512;
821      else
822        self->m_samplesPerFrame = 480;
823      break;
824#endif /* defined(TP_GA_ENABLE) */
825    default:
826      break;
827  }
828
829  switch (self->m_aot)
830  {
831    case AOT_ER_AAC_LC:
832    case AOT_ER_AAC_LD:
833    case AOT_ER_AAC_ELD:
834    case AOT_ER_AAC_SCAL:
835    case AOT_ER_CELP:
836    case AOT_ER_HVXC:
837    case AOT_ER_BSAC:
838      self->m_epConfig = FDKreadBits(bs,2);
839
840      if (self->m_epConfig > 1) {
841        return TRANSPORTDEC_UNSUPPORTED_FORMAT; // EPCONFIG;
842      }
843      break;
844    default:
845      break;
846  }
847
848
849  return (ErrorStatus);
850}
851
852
853