1 
2 /* -----------------------------------------------------------------------------------------------------------
3 Software 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
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):   Daniel Homm
87    Description:
88 
89 ******************************************************************************/
90 
91 #include "tpdec_lib.h"
92 #include "tp_data.h"
93 #ifdef TP_PCE_ENABLE
94 #include "FDK_crc.h"
95 #endif
96 
97 
CProgramConfig_Reset(CProgramConfig * pPce)98 void CProgramConfig_Reset(CProgramConfig *pPce)
99 {
100   pPce->elCounter = 0;
101 }
102 
CProgramConfig_Init(CProgramConfig * pPce)103 void CProgramConfig_Init(CProgramConfig *pPce)
104 {
105   FDKmemclear(pPce, sizeof(CProgramConfig));
106 #ifdef TP_PCE_ENABLE
107   pPce->SamplingFrequencyIndex = 0xf;
108 #endif
109 }
110 
CProgramConfig_IsValid(const CProgramConfig * pPce)111 int  CProgramConfig_IsValid ( const CProgramConfig *pPce )
112 {
113   return ( (pPce->isValid) ? 1 : 0);
114 }
115 
116 #ifdef TP_PCE_ENABLE
117 #define PCE_HEIGHT_EXT_SYNC  ( 0xAC )
118 
119 /*
120  * Read the extension for height info.
121  * return 0 if successfull or -1 if the CRC failed.
122  */
123 static
CProgramConfig_ReadHeightExt(CProgramConfig * pPce,HANDLE_FDK_BITSTREAM bs,int * const bytesAvailable,const UINT alignmentAnchor)124 int CProgramConfig_ReadHeightExt(
125                                   CProgramConfig *pPce,
126                                   HANDLE_FDK_BITSTREAM bs,
127                                   int * const bytesAvailable,
128                                   const UINT alignmentAnchor
129                                 )
130 {
131   int err = 0;
132   FDK_CRCINFO crcInfo;    /* CRC state info */
133   INT crcReg;
134   FDKcrcInit(&crcInfo, 0x07, 0xFF, 8);
135   crcReg = FDKcrcStartReg(&crcInfo, bs, 0);
136   UINT startAnchor = FDKgetValidBits(bs);
137 
138   FDK_ASSERT(pPce != NULL);
139   FDK_ASSERT(bs != NULL);
140   FDK_ASSERT(bytesAvailable != NULL);
141 
142   if ( (startAnchor >= 24) && (*bytesAvailable >= 3)
143     && (FDKreadBits(bs,8) == PCE_HEIGHT_EXT_SYNC) )
144   {
145     int i;
146 
147     for (i=0; i < pPce->NumFrontChannelElements; i++)
148     {
149       pPce->FrontElementHeightInfo[i] = (UCHAR) FDKreadBits(bs,2);
150     }
151     for (i=0; i < pPce->NumSideChannelElements; i++)
152     {
153       pPce->SideElementHeightInfo[i] = (UCHAR) FDKreadBits(bs,2);
154     }
155     for (i=0; i < pPce->NumBackChannelElements; i++)
156     {
157       pPce->BackElementHeightInfo[i] = (UCHAR) FDKreadBits(bs,2);
158     }
159     FDKbyteAlign(bs, alignmentAnchor);
160 
161     FDKcrcEndReg(&crcInfo, bs, crcReg);
162     if ((USHORT)FDKreadBits(bs,8) != FDKcrcGetCRC(&crcInfo)) {
163       /* CRC failed */
164       err = -1;
165     }
166   }
167   else {
168     /* No valid extension data found -> restore the initial bitbuffer state */
169     FDKpushBack(bs, startAnchor - FDKgetValidBits(bs));
170   }
171 
172   /* Always report the bytes read. */
173   *bytesAvailable -= (startAnchor - FDKgetValidBits(bs)) >> 3;
174 
175   return (err);
176 }
177 
CProgramConfig_Read(CProgramConfig * pPce,HANDLE_FDK_BITSTREAM bs,UINT alignmentAnchor)178 void CProgramConfig_Read(
179                           CProgramConfig *pPce,
180                           HANDLE_FDK_BITSTREAM bs,
181                           UINT alignmentAnchor
182                         )
183 {
184   int i, err = 0;
185   int commentBytes;
186 
187   pPce->NumEffectiveChannels = 0;
188   pPce->NumChannels = 0;
189   pPce->ElementInstanceTag = (UCHAR) FDKreadBits(bs,4);
190   pPce->Profile = (UCHAR) FDKreadBits(bs,2);
191   pPce->SamplingFrequencyIndex = (UCHAR) FDKreadBits(bs,4);
192   pPce->NumFrontChannelElements = (UCHAR) FDKreadBits(bs,4);
193   pPce->NumSideChannelElements = (UCHAR) FDKreadBits(bs,4);
194   pPce->NumBackChannelElements = (UCHAR) FDKreadBits(bs,4);
195   pPce->NumLfeChannelElements = (UCHAR) FDKreadBits(bs,2);
196   pPce->NumAssocDataElements = (UCHAR) FDKreadBits(bs,3);
197   pPce->NumValidCcElements = (UCHAR) FDKreadBits(bs,4);
198 
199   if ((pPce->MonoMixdownPresent = (UCHAR) FDKreadBits(bs,1)) != 0)
200   {
201     pPce->MonoMixdownElementNumber = (UCHAR) FDKreadBits(bs,4);
202   }
203 
204   if ((pPce->StereoMixdownPresent = (UCHAR) FDKreadBits(bs,1)) != 0)
205   {
206     pPce->StereoMixdownElementNumber = (UCHAR) FDKreadBits(bs,4);
207   }
208 
209   if ((pPce->MatrixMixdownIndexPresent = (UCHAR) FDKreadBits(bs,1)) != 0)
210   {
211     pPce->MatrixMixdownIndex = (UCHAR) FDKreadBits(bs,2);
212     pPce->PseudoSurroundEnable = (UCHAR) FDKreadBits(bs,1);
213   }
214 
215   for (i=0; i < pPce->NumFrontChannelElements; i++)
216   {
217     pPce->FrontElementIsCpe[i] = (UCHAR) FDKreadBits(bs,1);
218     pPce->FrontElementTagSelect[i] = (UCHAR) FDKreadBits(bs,4);
219     pPce->NumChannels += pPce->FrontElementIsCpe[i] ? 2 : 1;
220   }
221 
222   for (i=0; i < pPce->NumSideChannelElements; i++)
223   {
224     pPce->SideElementIsCpe[i] = (UCHAR) FDKreadBits(bs,1);
225     pPce->SideElementTagSelect[i] = (UCHAR) FDKreadBits(bs,4);
226     pPce->NumChannels += pPce->SideElementIsCpe[i] ? 2 : 1;
227   }
228 
229   for (i=0; i < pPce->NumBackChannelElements; i++)
230   {
231     pPce->BackElementIsCpe[i] = (UCHAR) FDKreadBits(bs,1);
232     pPce->BackElementTagSelect[i] = (UCHAR) FDKreadBits(bs,4);
233     pPce->NumChannels += pPce->BackElementIsCpe[i] ? 2 : 1;
234   }
235 
236   pPce->NumEffectiveChannels = pPce->NumChannels;
237 
238   for (i=0; i < pPce->NumLfeChannelElements; i++)
239   {
240     pPce->LfeElementTagSelect[i] = (UCHAR) FDKreadBits(bs,4);
241     pPce->NumChannels += 1;
242   }
243 
244   for (i=0; i < pPce->NumAssocDataElements; i++)
245   {
246     pPce->AssocDataElementTagSelect[i] = (UCHAR) FDKreadBits(bs,4);
247   }
248 
249   for (i=0; i < pPce->NumValidCcElements; i++)
250   {
251     pPce->CcElementIsIndSw[i] = (UCHAR) FDKreadBits(bs,1);
252     pPce->ValidCcElementTagSelect[i] = (UCHAR) FDKreadBits(bs,4);
253   }
254 
255   FDKbyteAlign(bs, alignmentAnchor);
256 
257   pPce->CommentFieldBytes = (UCHAR) FDKreadBits(bs,8);
258   commentBytes = pPce->CommentFieldBytes;
259 
260   /* Search for height info extension and read it if available */
261   err = CProgramConfig_ReadHeightExt( pPce, bs, &commentBytes, alignmentAnchor );
262 
263   for (i=0; i < commentBytes; i++)
264   {
265     UCHAR text;
266 
267     text = (UCHAR)FDKreadBits(bs,8);
268 
269     if (i < PC_COMMENTLENGTH)
270     {
271       pPce->Comment[i] = text;
272     }
273   }
274 
275   pPce->isValid = (err) ? 0 : 1;
276 }
277 
278 /*
279  * Compare two program configurations.
280  * Returns the result of the comparison:
281  *  -1 - completely different
282  *   0 - completely equal
283  *   1 - different but same channel configuration
284  *   2 - different channel configuration but same number of channels
285  */
CProgramConfig_Compare(const CProgramConfig * const pPce1,const CProgramConfig * const pPce2)286 int CProgramConfig_Compare ( const CProgramConfig * const pPce1,
287                              const CProgramConfig * const pPce2 )
288 {
289   int result = 0;  /* Innocent until proven false. */
290 
291   if (FDKmemcmp(pPce1, pPce2, sizeof(CProgramConfig)) != 0)
292   { /* Configurations are not completely different.
293        So look into details and analyse the channel configurations: */
294     result = -1;
295 
296     if (pPce1->NumChannels == pPce2->NumChannels)
297     { /* Now the logic changes. We first assume to have the same channel configuration
298          and then prove if this assumption is true. */
299       result = 1;
300 
301       /* Front channels */
302       if (pPce1->NumFrontChannelElements != pPce2->NumFrontChannelElements) {
303         result = 2;  /* different number of front channel elements */
304       } else {
305         int el, numCh1 = 0, numCh2 = 0;
306         for (el = 0; el < pPce1->NumFrontChannelElements; el += 1) {
307           if (pPce1->FrontElementHeightInfo[el] != pPce2->FrontElementHeightInfo[el]) {
308             result = 2; /* different height info */
309             break;
310           }
311           numCh1 += pPce1->FrontElementIsCpe[el] ? 2 : 1;
312           numCh2 += pPce2->FrontElementIsCpe[el] ? 2 : 1;
313         }
314         if (numCh1 != numCh2) {
315           result = 2;  /* different number of front channels */
316         }
317       }
318       /* Side channels */
319       if (pPce1->NumSideChannelElements != pPce2->NumSideChannelElements) {
320         result = 2;  /* different number of side channel elements */
321       } else {
322         int el, numCh1 = 0, numCh2 = 0;
323         for (el = 0; el < pPce1->NumSideChannelElements; el += 1) {
324           if (pPce1->SideElementHeightInfo[el] != pPce2->SideElementHeightInfo[el]) {
325             result = 2; /* different height info */
326             break;
327           }
328           numCh1 += pPce1->SideElementIsCpe[el] ? 2 : 1;
329           numCh2 += pPce2->SideElementIsCpe[el] ? 2 : 1;
330         }
331         if (numCh1 != numCh2) {
332           result = 2;  /* different number of side channels */
333         }
334       }
335       /* Back channels */
336       if (pPce1->NumBackChannelElements != pPce2->NumBackChannelElements) {
337         result = 2;  /* different number of back channel elements */
338       } else {
339         int el, numCh1 = 0, numCh2 = 0;
340         for (el = 0; el < pPce1->NumBackChannelElements; el += 1) {
341           if (pPce1->BackElementHeightInfo[el] != pPce2->BackElementHeightInfo[el]) {
342             result = 2; /* different height info */
343             break;
344           }
345           numCh1 += pPce1->BackElementIsCpe[el] ? 2 : 1;
346           numCh2 += pPce2->BackElementIsCpe[el] ? 2 : 1;
347         }
348         if (numCh1 != numCh2) {
349           result = 2;  /* different number of back channels */
350         }
351       }
352       /* LFE channels */
353       if (pPce1->NumLfeChannelElements != pPce2->NumLfeChannelElements) {
354         result = 2;  /* different number of lfe channels */
355       }
356       /* LFEs are always SCEs so we don't need to count the channels. */
357     }
358   }
359 
360   return result;
361 }
362 
CProgramConfig_GetDefault(CProgramConfig * pPce,const UINT channelConfig)363 void CProgramConfig_GetDefault( CProgramConfig *pPce,
364                                 const UINT channelConfig )
365 {
366   FDK_ASSERT(pPce != NULL);
367 
368   /* Init PCE */
369   CProgramConfig_Init(pPce);
370   pPce->Profile = 1;  /* Set AAC LC because it is the only supported object type. */
371 
372   switch (channelConfig) {
373   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
374   case 32: /* 7.1 side channel configuration as defined in FDK_audio.h */
375     pPce->NumFrontChannelElements  = 2;
376     pPce->FrontElementIsCpe[0]     = 0;
377     pPce->FrontElementIsCpe[1]     = 1;
378     pPce->NumSideChannelElements   = 1;
379     pPce->SideElementIsCpe[0]      = 1;
380     pPce->NumBackChannelElements   = 1;
381     pPce->BackElementIsCpe[0]      = 1;
382     pPce->NumLfeChannelElements    = 1;
383     pPce->NumChannels              = 8;
384     pPce->NumEffectiveChannels     = 7;
385     pPce->isValid                  = 1;
386     break;
387   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
388   case 12:  /* 3/0/4.1ch surround back */
389     pPce->BackElementIsCpe[1]      = 1;
390     pPce->NumChannels             += 1;
391     pPce->NumEffectiveChannels    += 1;
392   case 11:  /* 3/0/3.1ch */
393     pPce->NumFrontChannelElements += 2;
394     pPce->FrontElementIsCpe[0]     = 0;
395     pPce->FrontElementIsCpe[1]     = 1;
396     pPce->NumBackChannelElements  += 2;
397     pPce->BackElementIsCpe[0]      = 1;
398     pPce->BackElementIsCpe[1]     += 0;
399     pPce->NumLfeChannelElements   += 1;
400     pPce->NumChannels             += 7;
401     pPce->NumEffectiveChannels    += 6;
402     pPce->isValid                  = 1;
403     break;
404   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
405   case 14:  /* 2/0/0-3/0/2-0.1ch front height */
406     pPce->FrontElementHeightInfo[2] = 1;      /* Top speaker */
407   case 7:   /* 5/0/2.1ch front */
408     pPce->NumFrontChannelElements += 1;
409     pPce->FrontElementIsCpe[2]     = 1;
410     pPce->NumChannels             += 2;
411     pPce->NumEffectiveChannels    += 2;
412   case 6:   /* 3/0/2.1ch */
413     pPce->NumLfeChannelElements   += 1;
414     pPce->NumChannels             += 1;
415   case 5:   /* 3/0/2.0ch */
416   case 4:   /* 3/0/1.0ch */
417     pPce->NumBackChannelElements  += 1;
418     pPce->BackElementIsCpe[0]      = (channelConfig>4) ? 1 : 0;
419     pPce->NumChannels             += (channelConfig>4) ? 2 : 1;
420     pPce->NumEffectiveChannels    += (channelConfig>4) ? 2 : 1;
421   case 3:   /* 3/0/0.0ch */
422     pPce->NumFrontChannelElements += 1;
423     pPce->FrontElementIsCpe[1]     = 1;
424     pPce->NumChannels             += 2;
425     pPce->NumEffectiveChannels    += 2;
426   case 1:   /* 1/0/0.0ch */
427     pPce->NumFrontChannelElements += 1;
428     pPce->FrontElementIsCpe[0]     = 0;
429     pPce->NumChannels             += 1;
430     pPce->NumEffectiveChannels    += 1;
431     pPce->isValid                  = 1;
432     break;
433   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
434   case 2:   /* 2/0/0.ch */
435     pPce->NumFrontChannelElements  = 1;
436     pPce->FrontElementIsCpe[0]     = 1;
437     pPce->NumChannels             += 2;
438     pPce->NumEffectiveChannels    += 2;
439     pPce->isValid                  = 1;
440     break;
441   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
442   default:
443     pPce->isValid                  = 0;   /* To be explicit! */
444     break;
445   }
446 
447   if (pPce->isValid) {
448     /* Create valid element instance tags */
449     int el, elTagSce = 0, elTagCpe = 0;
450 
451     for (el = 0; el < pPce->NumFrontChannelElements; el += 1) {
452       pPce->FrontElementTagSelect[el] = (pPce->FrontElementIsCpe[el]) ? elTagCpe++ : elTagSce++;
453     }
454     for (el = 0; el < pPce->NumSideChannelElements; el += 1) {
455       pPce->SideElementTagSelect[el] = (pPce->SideElementIsCpe[el]) ? elTagCpe++ : elTagSce++;
456     }
457     for (el = 0; el < pPce->NumBackChannelElements; el += 1) {
458       pPce->BackElementTagSelect[el] = (pPce->BackElementIsCpe[el]) ? elTagCpe++ : elTagSce++;
459     }
460     elTagSce = 0;
461     for (el = 0; el < pPce->NumLfeChannelElements; el += 1) {
462       pPce->LfeElementTagSelect[el] = elTagSce++;
463     }
464   }
465 }
466 #endif /* TP_PCE_ENABLE */
467 
468 /**
469  * \brief get implicit audio channel type for given channelConfig and MPEG ordered channel index
470  * \param channelConfig MPEG channelConfiguration from 1 upto 14
471  * \param index MPEG channel order index
472  * \return audio channel type.
473  */
474 static
getImplicitAudioChannelTypeAndIndex(AUDIO_CHANNEL_TYPE * chType,UCHAR * chIndex,UINT channelConfig,UINT index)475 void getImplicitAudioChannelTypeAndIndex(
476         AUDIO_CHANNEL_TYPE *chType,
477         UCHAR *chIndex,
478         UINT channelConfig,
479         UINT index
480         )
481 {
482   if (index < 3) {
483     *chType = ACT_FRONT;
484     *chIndex = index;
485   } else {
486     switch (channelConfig) {
487       case 4:  /* SCE, CPE, SCE */
488       case 5:  /* SCE, CPE, CPE */
489       case 6:  /* SCE, CPE, CPE, LFE */
490         switch (index) {
491           case 3:
492           case 4:
493             *chType = ACT_BACK;
494             *chIndex = index - 3;
495             break;
496           case 5:
497             *chType = ACT_LFE;
498             *chIndex = 0;
499             break;
500         }
501         break;
502       case 7:  /* SCE,CPE,CPE,CPE,LFE */
503         switch (index) {
504           case 3:
505           case 4:
506             *chType = ACT_FRONT;
507             *chIndex = index;
508             break;
509           case 5:
510           case 6:
511             *chType = ACT_BACK;
512             *chIndex = index - 5;
513             break;
514           case 7:
515             *chType = ACT_LFE;
516             *chIndex = 0;
517             break;
518         }
519         break;
520       case 11:  /* SCE,CPE,CPE,SCE,LFE */
521         if (index < 6) {
522           *chType = ACT_BACK;
523           *chIndex = index - 3;
524         } else {
525           *chType = ACT_LFE;
526           *chIndex = 0;
527         }
528         break;
529       case 12:  /* SCE,CPE,CPE,CPE,LFE */
530         if (index < 7) {
531           *chType = ACT_BACK;
532           *chIndex = index - 3;
533         } else {
534           *chType = ACT_LFE;
535           *chIndex = 0;
536         }
537         break;
538       case 14:  /* SCE,CPE,CPE,LFE,CPE */
539         switch (index) {
540           case 3:
541           case 4:
542             *chType = ACT_BACK;
543             *chIndex = index - 3;
544             break;
545           case 5:
546             *chType = ACT_LFE;
547             *chIndex = 0;
548             break;
549           case 6:
550           case 7:
551             *chType = ACT_FRONT_TOP;
552             *chIndex = index - 6;  /* handle the top layer independently */
553             break;
554         }
555         break;
556       default:
557         *chType = ACT_NONE;
558         break;
559     }
560   }
561 }
562 
CProgramConfig_LookupElement(CProgramConfig * pPce,UINT channelConfig,const UINT tag,const UINT channelIdx,UCHAR chMapping[],AUDIO_CHANNEL_TYPE chType[],UCHAR chIndex[],UCHAR * elMapping,MP4_ELEMENT_ID elList[],MP4_ELEMENT_ID elType)563 int CProgramConfig_LookupElement(
564         CProgramConfig *pPce,
565         UINT            channelConfig,
566         const UINT      tag,
567         const UINT      channelIdx,
568         UCHAR           chMapping[],
569         AUDIO_CHANNEL_TYPE chType[],
570         UCHAR           chIndex[],
571         UCHAR          *elMapping,
572         MP4_ELEMENT_ID  elList[],
573         MP4_ELEMENT_ID  elType
574        )
575 {
576   if (channelConfig > 0)
577   {
578     /* Constant channel mapping must have
579        been set during initialization. */
580     if ( elType == ID_SCE
581       || elType == ID_CPE
582       || elType == ID_LFE )
583     {
584       *elMapping = pPce->elCounter;
585       if (elList[pPce->elCounter] != elType) {
586         /* Not in the list */
587         if ( (channelConfig == 2) && (elType == ID_SCE) )
588         { /* This scenario occurs with HE-AAC v2 streams of buggy encoders.
589              Due to other decoder implementations decoding of these kind of streams is desired. */
590           channelConfig = 1;
591         } else {
592           return 0;
593         }
594       }
595       /* Assume all front channels */
596       getImplicitAudioChannelTypeAndIndex(&chType[channelIdx], &chIndex[channelIdx], channelConfig, channelIdx);
597       if (elType == ID_CPE) {
598         chType[channelIdx+1] = chType[channelIdx];
599         chIndex[channelIdx+1] = chIndex[channelIdx]+1;
600       }
601       pPce->elCounter++;
602     }
603     /* Accept all non-channel elements, too. */
604     return 1;
605   }
606   else
607   {
608 #ifdef TP_PCE_ENABLE
609     if (!pPce->isValid)
610 #endif /* TP_PCE_ENABLE */
611     {
612       /* Implicit channel mapping. */
613       if ( elType == ID_SCE
614         || elType == ID_CPE
615         || elType == ID_LFE )
616       {
617         /* Store all channel element IDs */
618         elList[pPce->elCounter] = elType;
619         *elMapping = pPce->elCounter++;
620       }
621     }
622 #ifdef  TP_PCE_ENABLE
623     else {
624       /* Accept the additional channel(s), only if the tag is in the lists */
625       int isCpe = 0, i;
626       /* Element counter */
627       int ec[PC_NUM_HEIGHT_LAYER] = {0};
628       /* Channel counters */
629       int cc[PC_NUM_HEIGHT_LAYER] = {0};
630       int fc[PC_NUM_HEIGHT_LAYER] = {0};
631       int sc[PC_NUM_HEIGHT_LAYER] = {0};
632       int bc[PC_NUM_HEIGHT_LAYER] = {0};
633       int lc = 0;;
634 
635       /* General MPEG (PCE) composition rules:
636          - Over all:
637              <normal height channels><top height channels><bottom height channels>
638          - Within each height layer:
639              <front channels><side channels><back channels>
640          - Exception:
641              The LFE channels have no height info and thus they are arranged at the very
642              end of the normal height layer channels.
643        */
644 
645       switch (elType)
646       {
647       case ID_CPE:
648         isCpe = 1;
649       case ID_SCE:
650         /* search in front channels */
651         for (i = 0; i < pPce->NumFrontChannelElements; i++) {
652           int heightLayer = pPce->FrontElementHeightInfo[i];
653           if (isCpe == pPce->FrontElementIsCpe[i] && pPce->FrontElementTagSelect[i] == tag) {
654             int h, elIdx = ec[heightLayer], chIdx = cc[heightLayer];
655             AUDIO_CHANNEL_TYPE aChType = (AUDIO_CHANNEL_TYPE)((heightLayer<<4) | ACT_FRONT);
656             for (h = heightLayer-1; h >= 0; h-=1) {
657               int el;
658               /* Count front channels/elements */
659               for (el = 0; el < pPce->NumFrontChannelElements; el+=1) {
660                 if (pPce->FrontElementHeightInfo[el] == h) {
661                   elIdx += 1;
662                   chIdx += (pPce->FrontElementIsCpe[el]) ? 2 : 1;
663                 }
664               }
665               /* Count side channels/elements */
666               for (el = 0; el < pPce->NumSideChannelElements; el+=1) {
667                 if (pPce->SideElementHeightInfo[el] == h) {
668                   elIdx += 1;
669                   chIdx += (pPce->SideElementIsCpe[el]) ? 2 : 1;
670                 }
671               }
672               /* Count back channels/elements */
673               for (el = 0; el < pPce->NumBackChannelElements; el+=1) {
674                 if (pPce->BackElementHeightInfo[el] == h) {
675                   elIdx += 1;
676                   chIdx += (pPce->BackElementIsCpe[el]) ? 2 : 1;
677                 }
678               }
679               if (h == 0) {  /* normal height */
680                 elIdx += pPce->NumLfeChannelElements;
681                 chIdx += pPce->NumLfeChannelElements;
682               }
683             }
684             chMapping[chIdx] = channelIdx;
685             chType[chIdx] = aChType;
686             chIndex[chIdx] = fc[heightLayer];
687             if (isCpe) {
688               chMapping[chIdx+1] = channelIdx+1;
689               chType[chIdx+1] = aChType;
690               chIndex[chIdx+1] = fc[heightLayer]+1;
691             }
692             *elMapping = elIdx;
693             return 1;
694           }
695           ec[heightLayer] += 1;
696           if (pPce->FrontElementIsCpe[i]) {
697             cc[heightLayer] += 2;
698             fc[heightLayer] += 2;
699           } else {
700             cc[heightLayer] += 1;
701             fc[heightLayer] += 1;
702           }
703         }
704         /* search in side channels */
705         for (i = 0; i < pPce->NumSideChannelElements; i++) {
706           int heightLayer = pPce->SideElementHeightInfo[i];
707           if (isCpe == pPce->SideElementIsCpe[i] && pPce->SideElementTagSelect[i] == tag) {
708             int h, elIdx = ec[heightLayer], chIdx = cc[heightLayer];
709             AUDIO_CHANNEL_TYPE aChType = (AUDIO_CHANNEL_TYPE)((heightLayer<<4) | ACT_SIDE);
710             for (h = heightLayer-1; h >= 0; h-=1) {
711               int el;
712               /* Count front channels/elements */
713               for (el = 0; el < pPce->NumFrontChannelElements; el+=1) {
714                 if (pPce->FrontElementHeightInfo[el] == h) {
715                   elIdx += 1;
716                   chIdx += (pPce->FrontElementIsCpe[el]) ? 2 : 1;
717                 }
718               }
719               /* Count side channels/elements */
720               for (el = 0; el < pPce->NumSideChannelElements; el+=1) {
721                 if (pPce->SideElementHeightInfo[el] == h) {
722                   elIdx += 1;
723                   chIdx += (pPce->SideElementIsCpe[el]) ? 2 : 1;
724                 }
725               }
726               /* Count back channels/elements */
727               for (el = 0; el < pPce->NumBackChannelElements; el+=1) {
728                 if (pPce->BackElementHeightInfo[el] == h) {
729                   elIdx += 1;
730                   chIdx += (pPce->BackElementIsCpe[el]) ? 2 : 1;
731                 }
732               }
733               if (h == 0) {  /* LFE channels belong to the normal height layer */
734                 elIdx += pPce->NumLfeChannelElements;
735                 chIdx += pPce->NumLfeChannelElements;
736               }
737             }
738             chMapping[chIdx] = channelIdx;
739             chType[chIdx] = aChType;
740             chIndex[chIdx] = sc[heightLayer];
741             if (isCpe) {
742               chMapping[chIdx+1] = channelIdx+1;
743               chType[chIdx+1] = aChType;
744               chIndex[chIdx+1] = sc[heightLayer]+1;
745             }
746             *elMapping = elIdx;
747             return 1;
748           }
749           ec[heightLayer] += 1;
750           if (pPce->SideElementIsCpe[i]) {
751             cc[heightLayer] += 2;
752             sc[heightLayer] += 2;
753           } else {
754             cc[heightLayer] += 1;
755             sc[heightLayer] += 1;
756           }
757         }
758         /* search in back channels */
759         for (i = 0; i < pPce->NumBackChannelElements; i++) {
760           int heightLayer = pPce->BackElementHeightInfo[i];
761           if (isCpe == pPce->BackElementIsCpe[i] && pPce->BackElementTagSelect[i] == tag) {
762             int h, elIdx = ec[heightLayer], chIdx = cc[heightLayer];
763             AUDIO_CHANNEL_TYPE aChType = (AUDIO_CHANNEL_TYPE)((heightLayer<<4) | ACT_BACK);
764             for (h = heightLayer-1; h >= 0; h-=1) {
765               int el;
766               /* Count front channels/elements */
767               for (el = 0; el < pPce->NumFrontChannelElements; el+=1) {
768                 if (pPce->FrontElementHeightInfo[el] == h) {
769                   elIdx += 1;
770                   chIdx += (pPce->FrontElementIsCpe[el]) ? 2 : 1;
771                 }
772               }
773               /* Count side channels/elements */
774               for (el = 0; el < pPce->NumSideChannelElements; el+=1) {
775                 if (pPce->SideElementHeightInfo[el] == h) {
776                   elIdx += 1;
777                   chIdx += (pPce->SideElementIsCpe[el]) ? 2 : 1;
778                 }
779               }
780               /* Count back channels/elements */
781               for (el = 0; el < pPce->NumBackChannelElements; el+=1) {
782                 if (pPce->BackElementHeightInfo[el] == h) {
783                   elIdx += 1;
784                   chIdx += (pPce->BackElementIsCpe[el]) ? 2 : 1;
785                 }
786               }
787               if (h == 0) {  /* normal height */
788                 elIdx += pPce->NumLfeChannelElements;
789                 chIdx += pPce->NumLfeChannelElements;
790               }
791             }
792             chMapping[chIdx] = channelIdx;
793             chType[chIdx] = aChType;
794             chIndex[chIdx] = bc[heightLayer];
795             if (isCpe) {
796               chMapping[chIdx+1] = channelIdx+1;
797               chType[chIdx+1] = aChType;
798               chIndex[chIdx+1] = bc[heightLayer]+1;
799             }
800             *elMapping = elIdx;
801             return 1;
802           }
803           ec[heightLayer] += 1;
804           if (pPce->BackElementIsCpe[i]) {
805             cc[heightLayer] += 2;
806             bc[heightLayer] += 2;
807           } else {
808             cc[heightLayer] += 1;
809             bc[heightLayer] += 1;
810           }
811         }
812         break;
813 
814       case ID_LFE:
815       { /* Unfortunately we have to go through all normal height
816            layer elements to get the position of the LFE channels.
817            Start with counting the front channels/elements at normal height */
818         for (i = 0; i < pPce->NumFrontChannelElements; i+=1) {
819           int heightLayer = pPce->FrontElementHeightInfo[i];
820           ec[heightLayer] += 1;
821           cc[heightLayer] += (pPce->FrontElementIsCpe[i]) ? 2 : 1;
822         }
823         /* Count side channels/elements at normal height */
824         for (i = 0; i < pPce->NumSideChannelElements; i+=1) {
825           int heightLayer = pPce->SideElementHeightInfo[i];
826           ec[heightLayer] += 1;
827           cc[heightLayer] += (pPce->SideElementIsCpe[i]) ? 2 : 1;
828         }
829         /* Count back channels/elements at normal height */
830         for (i = 0; i < pPce->NumBackChannelElements; i+=1) {
831           int heightLayer = pPce->BackElementHeightInfo[i];
832           ec[heightLayer] += 1;
833           cc[heightLayer] += (pPce->BackElementIsCpe[i]) ? 2 : 1;
834         }
835 
836         /* search in lfe channels */
837         for (i = 0; i < pPce->NumLfeChannelElements; i++) {
838           int elIdx = ec[0];  /* LFE channels belong to the normal height layer */
839           int chIdx = cc[0];
840           if ( pPce->LfeElementTagSelect[i] == tag ) {
841             chMapping[chIdx] = channelIdx;
842             *elMapping = elIdx;
843             chType[chIdx] = ACT_LFE;
844             chIndex[chIdx] = lc;
845             return 1;
846           }
847           ec[0] += 1;
848           cc[0] += 1;
849           lc += 1;
850         }
851       } break;
852 
853       /* Non audio elements */
854       case ID_CCE:
855         /* search in cce channels */
856         for (i = 0; i < pPce->NumValidCcElements; i++) {
857           if (pPce->ValidCcElementTagSelect[i] == tag) {
858             return 1;
859           }
860         }
861         break;
862       case ID_DSE:
863         /* search associated data elements */
864         for (i = 0; i < pPce->NumAssocDataElements; i++) {
865           if (pPce->AssocDataElementTagSelect[i] == tag) {
866             return 1;
867           }
868         }
869         break;
870       default:
871         return 0;
872       }
873       return 0;  /* not found in any list */
874     }
875 #endif /* TP_PCE_ENABLE */
876   }
877 
878   return 1;
879 }
880 
881 #ifdef  TP_PCE_ENABLE
CProgramConfig_GetElementTable(const CProgramConfig * pPce,MP4_ELEMENT_ID elList[],const INT elListSize,UCHAR * pChMapIdx)882 int CProgramConfig_GetElementTable(
883         const CProgramConfig *pPce,
884         MP4_ELEMENT_ID  elList[],
885         const INT elListSize,
886         UCHAR *pChMapIdx
887        )
888 {
889   int i, el = 0;
890 
891   FDK_ASSERT(elList != NULL);
892   FDK_ASSERT(pChMapIdx != NULL);
893 
894   *pChMapIdx = 0;
895 
896   if ( elListSize
897     < pPce->NumFrontChannelElements + pPce->NumSideChannelElements + pPce->NumBackChannelElements + pPce->NumLfeChannelElements
898     )
899   {
900     return 0;
901   }
902 
903   for (i=0; i < pPce->NumFrontChannelElements; i++)
904   {
905     elList[el++] = (pPce->FrontElementIsCpe[i]) ?  ID_CPE : ID_SCE;
906   }
907 
908   for (i=0; i < pPce->NumSideChannelElements; i++)
909   {
910     elList[el++] = (pPce->SideElementIsCpe[i]) ?  ID_CPE : ID_SCE;
911   }
912 
913   for (i=0; i < pPce->NumBackChannelElements; i++)
914   {
915     elList[el++] = (pPce->BackElementIsCpe[i]) ?  ID_CPE : ID_SCE;
916   }
917 
918   for (i=0; i < pPce->NumLfeChannelElements; i++)
919   {
920     elList[el++] = ID_LFE;
921   }
922 
923 
924   /* Find an corresponding channel configuration if possible */
925   switch (pPce->NumChannels) {
926   case 1: case 2: case 3: case 4: case 5: case 6:
927     /* One and two channels have no alternatives. The other ones are mapped directly to the
928        corresponding channel config. Because of legacy reasons or for lack of alternative mappings. */
929     *pChMapIdx = pPce->NumChannels;
930     break;
931   case 7:
932     {
933       C_ALLOC_SCRATCH_START(tmpPce, CProgramConfig, 1);
934       /* Create a PCE for the config to test ... */
935       CProgramConfig_GetDefault(tmpPce, 11);
936       /* ... and compare it with the given one. */
937       *pChMapIdx = (!(CProgramConfig_Compare(pPce, tmpPce)&0xE)) ? 11 : 0;
938       /* If compare result is 0 or 1 we can be sure that it is channel config 11. */
939       C_ALLOC_SCRATCH_END(tmpPce, CProgramConfig, 1);
940     }
941     break;
942   case 8:
943     { /* Try the four possible 7.1ch configurations. One after the other. */
944       UCHAR testCfg[4] = { 32, 14, 12, 7};
945       C_ALLOC_SCRATCH_START(tmpPce, CProgramConfig, 1);
946       for (i=0; i<4; i+=1) {
947         /* Create a PCE for the config to test ... */
948         CProgramConfig_GetDefault(tmpPce, testCfg[i]);
949         /* ... and compare it with the given one. */
950         if (!(CProgramConfig_Compare(pPce, tmpPce)&0xE)) {
951           /* If the compare result is 0 or 1 than the two channel configurations match. */
952           /* Explicit mapping of 7.1 side channel configuration to 7.1 rear channel mapping. */
953           *pChMapIdx = (testCfg[i]==32) ? 12 : testCfg[i];
954         }
955       }
956       C_ALLOC_SCRATCH_END(tmpPce, CProgramConfig, 1);
957     }
958     break;
959   default:
960     /* The PCE does not match any predefined channel configuration. */
961     *pChMapIdx = 0;
962     break;
963   }
964 
965   return el;
966 }
967 #endif
968 
getAOT(HANDLE_FDK_BITSTREAM bs)969 static AUDIO_OBJECT_TYPE getAOT(HANDLE_FDK_BITSTREAM bs)
970 {
971   int tmp = 0;
972 
973   tmp = FDKreadBits(bs,5);
974   if (tmp == AOT_ESCAPE) {
975     int tmp2 = FDKreadBits(bs,6);
976     tmp = 32 + tmp2;
977   }
978 
979   return (AUDIO_OBJECT_TYPE)tmp;
980 }
981 
getSampleRate(HANDLE_FDK_BITSTREAM bs,UCHAR * index,int nBits)982 static INT getSampleRate(HANDLE_FDK_BITSTREAM bs, UCHAR *index, int nBits)
983 {
984   INT sampleRate;
985   int idx;
986 
987   idx = FDKreadBits(bs, nBits);
988   if( idx == (1<<nBits)-1 ) {
989     if(FDKgetValidBits(bs) < 24) {
990       return 0;
991     }
992     sampleRate = FDKreadBits(bs,24);
993   } else {
994     sampleRate = SamplingRateTable[idx];
995   }
996 
997   *index = idx;
998 
999   return sampleRate;
1000 }
1001 
1002 #ifdef TP_GA_ENABLE
1003 static
GaSpecificConfig_Parse(CSGaSpecificConfig * self,CSAudioSpecificConfig * asc,HANDLE_FDK_BITSTREAM bs,UINT ascStartAnchor)1004 TRANSPORTDEC_ERROR GaSpecificConfig_Parse( CSGaSpecificConfig    *self,
1005                                            CSAudioSpecificConfig *asc,
1006                                            HANDLE_FDK_BITSTREAM   bs,
1007                                            UINT                   ascStartAnchor )
1008 {
1009   TRANSPORTDEC_ERROR ErrorStatus = TRANSPORTDEC_OK;
1010 
1011   self->m_frameLengthFlag = FDKreadBits(bs,1);
1012 
1013   self->m_dependsOnCoreCoder = FDKreadBits(bs,1);
1014 
1015   if( self->m_dependsOnCoreCoder )
1016     self->m_coreCoderDelay = FDKreadBits(bs,14);
1017 
1018   self->m_extensionFlag = FDKreadBits(bs,1);
1019 
1020   if( asc->m_channelConfiguration == 0 ) {
1021     CProgramConfig_Read(&asc->m_progrConfigElement, bs, ascStartAnchor);
1022   }
1023 
1024   if ((asc->m_aot == AOT_AAC_SCAL) || (asc->m_aot == AOT_ER_AAC_SCAL)) {
1025     self->m_layer = FDKreadBits(bs,3);
1026   }
1027 
1028   if (self->m_extensionFlag) {
1029     if (asc->m_aot == AOT_ER_BSAC) {
1030       self->m_numOfSubFrame = FDKreadBits(bs,5);
1031       self->m_layerLength   = FDKreadBits(bs,11);
1032     }
1033 
1034     if ((asc->m_aot == AOT_ER_AAC_LC)   || (asc->m_aot == AOT_ER_AAC_LTP)  ||
1035         (asc->m_aot == AOT_ER_AAC_SCAL) || (asc->m_aot == AOT_ER_AAC_LD))
1036     {
1037       asc->m_vcb11Flag = FDKreadBits(bs,1); /* aacSectionDataResilienceFlag */
1038       asc->m_rvlcFlag  = FDKreadBits(bs,1); /* aacScalefactorDataResilienceFlag */
1039       asc->m_hcrFlag   = FDKreadBits(bs,1); /* aacSpectralDataResilienceFlag */
1040     }
1041 
1042     self->m_extensionFlag3 = FDKreadBits(bs,1);
1043 
1044   }
1045   return (ErrorStatus);
1046 }
1047 #endif /* TP_GA_ENABLE */
1048 
1049 
1050 
1051 
1052 
1053 #ifdef TP_ELD_ENABLE
1054 
ld_sbr_header(const CSAudioSpecificConfig * asc,HANDLE_FDK_BITSTREAM hBs,CSTpCallBacks * cb)1055 static INT ld_sbr_header( const CSAudioSpecificConfig *asc,
1056                            HANDLE_FDK_BITSTREAM hBs,
1057                            CSTpCallBacks *cb )
1058 {
1059   const int channelConfiguration = asc->m_channelConfiguration;
1060   int i = 0;
1061   INT error = 0;
1062 
1063   if (channelConfiguration == 2) {
1064     error = cb->cbSbr(cb->cbSbrData, hBs, asc->m_samplingFrequency, asc->m_extensionSamplingFrequency, asc->m_samplesPerFrame, AOT_ER_AAC_ELD, ID_CPE, i++);
1065   } else {
1066     error = cb->cbSbr(cb->cbSbrData, hBs, asc->m_samplingFrequency, asc->m_extensionSamplingFrequency, asc->m_samplesPerFrame, AOT_ER_AAC_ELD, ID_SCE, i++);
1067   }
1068 
1069   switch ( channelConfiguration ) {
1070     case 14:
1071     case 12:
1072     case 7:
1073       error |= cb->cbSbr(cb->cbSbrData, hBs, asc->m_samplingFrequency, asc->m_extensionSamplingFrequency, asc->m_samplesPerFrame, AOT_ER_AAC_ELD, ID_CPE, i++);
1074     case 6:
1075     case 5:
1076       error |= cb->cbSbr(cb->cbSbrData, hBs, asc->m_samplingFrequency, asc->m_extensionSamplingFrequency, asc->m_samplesPerFrame, AOT_ER_AAC_ELD, ID_CPE, i++);
1077     case 3:
1078       error |= cb->cbSbr(cb->cbSbrData, hBs, asc->m_samplingFrequency, asc->m_extensionSamplingFrequency, asc->m_samplesPerFrame, AOT_ER_AAC_ELD, ID_CPE, i++);
1079       break;
1080 
1081     case 11:
1082       error |= cb->cbSbr(cb->cbSbrData, hBs, asc->m_samplingFrequency, asc->m_extensionSamplingFrequency, asc->m_samplesPerFrame, AOT_ER_AAC_ELD, ID_CPE, i++);
1083     case 4:
1084       error |= cb->cbSbr(cb->cbSbrData, hBs, asc->m_samplingFrequency, asc->m_extensionSamplingFrequency, asc->m_samplesPerFrame, AOT_ER_AAC_ELD, ID_CPE, i++);
1085       error |= cb->cbSbr(cb->cbSbrData, hBs, asc->m_samplingFrequency, asc->m_extensionSamplingFrequency, asc->m_samplesPerFrame, AOT_ER_AAC_ELD, ID_SCE, i++);
1086       break;
1087   }
1088 
1089   return error;
1090 }
1091 
1092 static
EldSpecificConfig_Parse(CSAudioSpecificConfig * asc,HANDLE_FDK_BITSTREAM hBs,CSTpCallBacks * cb)1093 TRANSPORTDEC_ERROR EldSpecificConfig_Parse(
1094         CSAudioSpecificConfig *asc,
1095         HANDLE_FDK_BITSTREAM hBs,
1096         CSTpCallBacks *cb
1097         )
1098 {
1099   TRANSPORTDEC_ERROR ErrorStatus = TRANSPORTDEC_OK;
1100   CSEldSpecificConfig *esc = &asc->m_sc.m_eldSpecificConfig;
1101   ASC_ELD_EXT_TYPE eldExtType;
1102   int eldExtLen, len, cnt;
1103 
1104   FDKmemclear(esc, sizeof(CSEldSpecificConfig));
1105 
1106   esc->m_frameLengthFlag = FDKreadBits(hBs, 1 );
1107   if (esc->m_frameLengthFlag) {
1108     asc->m_samplesPerFrame = 480;
1109   } else {
1110     asc->m_samplesPerFrame = 512;
1111   }
1112 
1113   asc->m_vcb11Flag = FDKreadBits(hBs, 1 );
1114   asc->m_rvlcFlag  = FDKreadBits(hBs, 1 );
1115   asc->m_hcrFlag   = FDKreadBits(hBs, 1 );
1116 
1117   esc->m_sbrPresentFlag     = FDKreadBits(hBs, 1 );
1118 
1119   if (esc->m_sbrPresentFlag == 1) {
1120     esc->m_sbrSamplingRate    = FDKreadBits(hBs, 1 ); /* 0: single rate, 1: dual rate */
1121     esc->m_sbrCrcFlag         = FDKreadBits(hBs, 1 );
1122 
1123     asc->m_extensionSamplingFrequency = asc->m_samplingFrequency << esc->m_sbrSamplingRate;
1124 
1125     if (cb->cbSbr != NULL){
1126       if ( 0 != ld_sbr_header(asc, hBs, cb) ) {
1127         return TRANSPORTDEC_PARSE_ERROR;
1128       }
1129     }
1130   }
1131   esc->m_useLdQmfTimeAlign = 0;
1132 
1133   /* new ELD syntax */
1134   /* parse ExtTypeConfigData */
1135   while ((eldExtType = (ASC_ELD_EXT_TYPE)FDKreadBits(hBs, 4 )) != ELDEXT_TERM) {
1136     eldExtLen = len = FDKreadBits(hBs, 4 );
1137     if ( len == 0xf ) {
1138       len = FDKreadBits(hBs, 8 );
1139       eldExtLen += len;
1140 
1141       if ( len == 0xff ) {
1142         len = FDKreadBits(hBs, 16 );
1143         eldExtLen += len;
1144       }
1145     }
1146 
1147     switch (eldExtType) {
1148       default:
1149         for(cnt=0; cnt<len; cnt++) {
1150           FDKreadBits(hBs, 8 );
1151         }
1152         break;
1153       /* add future eld extension configs here */
1154     }
1155   }
1156 bail:
1157   return (ErrorStatus);
1158 }
1159 #endif /* TP_ELD_ENABLE */
1160 
1161 
1162 static
AudioSpecificConfig_ExtensionParse(CSAudioSpecificConfig * self,HANDLE_FDK_BITSTREAM bs,CSTpCallBacks * cb)1163 TRANSPORTDEC_ERROR AudioSpecificConfig_ExtensionParse(CSAudioSpecificConfig *self, HANDLE_FDK_BITSTREAM bs, CSTpCallBacks *cb)
1164 {
1165   TP_ASC_EXTENSION_ID  lastAscExt, ascExtId = ASCEXT_UNKOWN;
1166   INT  bitsAvailable = (INT)FDKgetValidBits(bs);
1167 
1168   while (bitsAvailable >= 11)
1169   {
1170     lastAscExt = ascExtId;
1171     ascExtId   = (TP_ASC_EXTENSION_ID)FDKreadBits(bs, 11);
1172     bitsAvailable -= 11;
1173 
1174     switch (ascExtId) {
1175     case ASCEXT_SBR:    /* 0x2b7 */
1176       if ( (self->m_extensionAudioObjectType != AOT_SBR) && (bitsAvailable >= 5) ) {
1177         self->m_extensionAudioObjectType = getAOT(bs);
1178 
1179         if ( (self->m_extensionAudioObjectType == AOT_SBR)
1180           || (self->m_extensionAudioObjectType == AOT_ER_BSAC) )
1181         { /* Get SBR extension configuration */
1182           self->m_sbrPresentFlag = FDKreadBits(bs, 1);
1183           bitsAvailable -= 1;
1184 
1185           if ( self->m_sbrPresentFlag == 1 ) {
1186             self->m_extensionSamplingFrequency = getSampleRate(bs, &self->m_extensionSamplingFrequencyIndex, 4);
1187 
1188             if ((INT)self->m_extensionSamplingFrequency <= 0) {
1189               return TRANSPORTDEC_PARSE_ERROR;
1190             }
1191           }
1192           if ( self->m_extensionAudioObjectType == AOT_ER_BSAC ) {
1193             self->m_extensionChannelConfiguration = FDKreadBits(bs, 4);
1194             bitsAvailable -= 4;
1195           }
1196         }
1197         /* Update counter because of variable length fields (AOT and sampling rate) */
1198         bitsAvailable = (INT)FDKgetValidBits(bs);
1199       }
1200       break;
1201     case ASCEXT_PS:     /* 0x548 */
1202       if ( (lastAscExt == ASCEXT_SBR)
1203         && (self->m_extensionAudioObjectType == AOT_SBR)
1204         && (bitsAvailable > 0) )
1205       { /* Get PS extension configuration */
1206         self->m_psPresentFlag = FDKreadBits(bs, 1);
1207         bitsAvailable -= 1;
1208       }
1209       break;
1210     default:
1211       /* Just ignore anything. */
1212       return TRANSPORTDEC_OK;
1213     }
1214   }
1215 
1216   return TRANSPORTDEC_OK;
1217 }
1218 
1219 /*
1220  * API Functions
1221  */
1222 
AudioSpecificConfig_Init(CSAudioSpecificConfig * asc)1223 void AudioSpecificConfig_Init(CSAudioSpecificConfig *asc)
1224 {
1225   FDKmemclear(asc, sizeof(CSAudioSpecificConfig));
1226 
1227   /* Init all values that should not be zero. */
1228   asc->m_aot                    = AOT_NONE;
1229   asc->m_samplingFrequencyIndex = 0xf;
1230   asc->m_epConfig               = -1;
1231   asc->m_extensionAudioObjectType        = AOT_NULL_OBJECT;
1232 #ifdef TP_PCE_ENABLE
1233   CProgramConfig_Init(&asc->m_progrConfigElement);
1234 #endif
1235 }
1236 
AudioSpecificConfig_Parse(CSAudioSpecificConfig * self,HANDLE_FDK_BITSTREAM bs,int fExplicitBackwardCompatible,CSTpCallBacks * cb)1237 TRANSPORTDEC_ERROR AudioSpecificConfig_Parse(
1238         CSAudioSpecificConfig *self,
1239         HANDLE_FDK_BITSTREAM   bs,
1240         int                    fExplicitBackwardCompatible,
1241         CSTpCallBacks      *cb
1242         )
1243 {
1244   TRANSPORTDEC_ERROR ErrorStatus = TRANSPORTDEC_OK;
1245   UINT ascStartAnchor = FDKgetValidBits(bs);
1246   int frameLengthFlag = -1;
1247 
1248   AudioSpecificConfig_Init(self);
1249 
1250   self->m_aot = getAOT(bs);
1251   self->m_samplingFrequency = getSampleRate(bs, &self->m_samplingFrequencyIndex, 4);
1252   if (self->m_samplingFrequency <= 0) {
1253     return TRANSPORTDEC_PARSE_ERROR;
1254   }
1255 
1256   self->m_channelConfiguration = FDKreadBits(bs,4);
1257 
1258   /* SBR extension ( explicit non-backwards compatible mode ) */
1259   self->m_sbrPresentFlag = 0;
1260   self->m_psPresentFlag  = 0;
1261 
1262   if ( self->m_aot == AOT_SBR || self->m_aot == AOT_PS ) {
1263     self->m_extensionAudioObjectType = AOT_SBR;
1264 
1265     self->m_sbrPresentFlag = 1;
1266     if ( self->m_aot == AOT_PS ) {
1267       self->m_psPresentFlag = 1;
1268     }
1269 
1270     self->m_extensionSamplingFrequency = getSampleRate(bs, &self->m_extensionSamplingFrequencyIndex, 4);
1271     self->m_aot = getAOT(bs);
1272 
1273   } else {
1274     self->m_extensionAudioObjectType = AOT_NULL_OBJECT;
1275   }
1276 
1277   /* Parse whatever specific configs */
1278   switch (self->m_aot)
1279   {
1280 #ifdef TP_GA_ENABLE
1281     case AOT_AAC_LC:
1282     case AOT_ER_AAC_LC:
1283     case AOT_ER_AAC_LD:
1284     case AOT_ER_AAC_SCAL:
1285     case AOT_ER_BSAC:
1286       if ((ErrorStatus = GaSpecificConfig_Parse(&self->m_sc.m_gaSpecificConfig, self, bs, ascStartAnchor)) != TRANSPORTDEC_OK ) {
1287         return (ErrorStatus);
1288       }
1289       frameLengthFlag = self->m_sc.m_gaSpecificConfig.m_frameLengthFlag;
1290       break;
1291 #endif /* TP_GA_ENABLE */
1292     case AOT_MPEGS:
1293       if (cb->cbSsc != NULL) {
1294         cb->cbSsc(
1295                 cb->cbSscData,
1296                 bs,
1297                 self->m_aot,
1298                 self->m_samplingFrequency,
1299                 1,
1300                 0  /* don't know the length */
1301                 );
1302       } else {
1303         return TRANSPORTDEC_UNSUPPORTED_FORMAT;
1304       }
1305       break;
1306 #ifdef TP_ELD_ENABLE
1307     case AOT_ER_AAC_ELD:
1308       if ((ErrorStatus = EldSpecificConfig_Parse(self, bs, cb)) != TRANSPORTDEC_OK ) {
1309         return (ErrorStatus);
1310       }
1311       frameLengthFlag = self->m_sc.m_eldSpecificConfig.m_frameLengthFlag;
1312       self->m_sbrPresentFlag = self->m_sc.m_eldSpecificConfig.m_sbrPresentFlag;
1313       self->m_extensionSamplingFrequency = (self->m_sc.m_eldSpecificConfig.m_sbrSamplingRate+1) * self->m_samplingFrequency;
1314       break;
1315 #endif /* TP_ELD_ENABLE */
1316 
1317     default:
1318       return TRANSPORTDEC_UNSUPPORTED_FORMAT;
1319       break;
1320   }
1321 
1322   /* Frame length */
1323   switch (self->m_aot)
1324   {
1325 #if defined(TP_GA_ENABLE) || defined(TP_USAC_ENABLE)
1326     case AOT_AAC_LC:
1327     case AOT_ER_AAC_LC:
1328     case AOT_ER_AAC_SCAL:
1329     case AOT_ER_BSAC:
1330     /*case AOT_USAC:*/
1331       if (!frameLengthFlag)
1332         self->m_samplesPerFrame = 1024;
1333       else
1334         self->m_samplesPerFrame = 960;
1335       break;
1336 #endif /* TP_GA_ENABLE */
1337 #if defined(TP_GA_ENABLE)
1338     case AOT_ER_AAC_LD:
1339       if (!frameLengthFlag)
1340         self->m_samplesPerFrame = 512;
1341       else
1342         self->m_samplesPerFrame = 480;
1343       break;
1344 #endif /* defined(TP_GA_ENABLE) */
1345     default:
1346       break;
1347   }
1348 
1349   switch (self->m_aot)
1350   {
1351     case AOT_ER_AAC_LC:
1352     case AOT_ER_AAC_LD:
1353     case AOT_ER_AAC_ELD:
1354     case AOT_ER_AAC_SCAL:
1355     case AOT_ER_CELP:
1356     case AOT_ER_HVXC:
1357     case AOT_ER_BSAC:
1358       self->m_epConfig = FDKreadBits(bs,2);
1359 
1360       if (self->m_epConfig > 1) {
1361         return TRANSPORTDEC_UNSUPPORTED_FORMAT; // EPCONFIG;
1362       }
1363       break;
1364     default:
1365       break;
1366   }
1367 
1368   if (fExplicitBackwardCompatible) {
1369     ErrorStatus = AudioSpecificConfig_ExtensionParse(self, bs, cb);
1370   }
1371 
1372   return (ErrorStatus);
1373 }
1374 
1375 
1376