• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 /* -----------------------------------------------------------------------------------------------------------
3 Software License for The Fraunhofer FDK AAC Codec Library for Android
4 
5 � Copyright  1995 - 2015 Fraunhofer-Gesellschaft zur F�rderung der angewandten Forschung e.V.
6   All rights reserved.
7 
8  1.    INTRODUCTION
9 The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software that implements
10 the MPEG Advanced Audio Coding ("AAC") encoding and decoding scheme for digital audio.
11 This FDK AAC Codec software is intended to be used on a wide variety of Android devices.
12 
13 AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient general perceptual
14 audio codecs. AAC-ELD is considered the best-performing full-bandwidth communications codec by
15 independent studies and is widely deployed. AAC has been standardized by ISO and IEC as part
16 of the MPEG specifications.
17 
18 Patent licenses for necessary patent claims for the FDK AAC Codec (including those of Fraunhofer)
19 may be obtained through Via Licensing (www.vialicensing.com) or through the respective patent owners
20 individually for the purpose of encoding or decoding bit streams in products that are compliant with
21 the ISO/IEC MPEG audio standards. Please note that most manufacturers of Android devices already license
22 these patent claims through Via Licensing or directly from the patent owners, and therefore FDK AAC Codec
23 software may already be covered under those patent licenses when it is used for those licensed purposes only.
24 
25 Commercially-licensed AAC software libraries, including floating-point versions with enhanced sound quality,
26 are also available from Fraunhofer. Users are encouraged to check the Fraunhofer website for additional
27 applications information and documentation.
28 
29 2.    COPYRIGHT LICENSE
30 
31 Redistribution and use in source and binary forms, with or without modification, are permitted without
32 payment of copyright license fees provided that you satisfy the following conditions:
33 
34 You must retain the complete text of this software license in redistributions of the FDK AAC Codec or
35 your modifications thereto in source code form.
36 
37 You must retain the complete text of this software license in the documentation and/or other materials
38 provided with redistributions of the FDK AAC Codec or your modifications thereto in binary form.
39 You must make available free of charge copies of the complete source code of the FDK AAC Codec and your
40 modifications thereto to recipients of copies in binary form.
41 
42 The name of Fraunhofer may not be used to endorse or promote products derived from this library without
43 prior written permission.
44 
45 You may not charge copyright license fees for anyone to use, copy or distribute the FDK AAC Codec
46 software or your modifications thereto.
47 
48 Your modified versions of the FDK AAC Codec must carry prominent notices stating that you changed the software
49 and the date of any change. For modified versions of the FDK AAC Codec, the term
50 "Fraunhofer FDK AAC Codec Library for Android" must be replaced by the term
51 "Third-Party Modified Version of the Fraunhofer FDK AAC Codec Library for Android."
52 
53 3.    NO PATENT LICENSE
54 
55 NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without limitation the patents of Fraunhofer,
56 ARE GRANTED BY THIS SOFTWARE LICENSE. Fraunhofer provides no warranty of patent non-infringement with
57 respect to this software.
58 
59 You may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized
60 by appropriate patent licenses.
61 
62 4.    DISCLAIMER
63 
64 This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright holders and contributors
65 "AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, including but not limited to the implied warranties
66 of merchantability and fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
67 CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary, or consequential damages,
68 including but not limited to procurement of substitute goods or services; loss of use, data, or profits,
69 or business interruption, however caused and on any theory of liability, whether in contract, strict
70 liability, or tort (including negligence), arising in any way out of the use of this software, even if
71 advised of the possibility of such damage.
72 
73 5.    CONTACT INFORMATION
74 
75 Fraunhofer Institute for Integrated Circuits IIS
76 Attention: Audio and Multimedia Departments - FDK AAC LL
77 Am Wolfsmantel 33
78 91058 Erlangen, Germany
79 
80 www.iis.fraunhofer.de/amm
81 amm-info@iis.fraunhofer.de
82 ----------------------------------------------------------------------------------------------------------- */
83 
84 /*****************************  MPEG-4 AAC Decoder  **************************
85 
86    Author(s):   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,
122  *       -1 if the CRC failed,
123  *       -2 if invalid HeightInfo.
124  */
125 static
CProgramConfig_ReadHeightExt(CProgramConfig * pPce,HANDLE_FDK_BITSTREAM bs,int * const bytesAvailable,const UINT alignmentAnchor)126 int CProgramConfig_ReadHeightExt(
127                                   CProgramConfig *pPce,
128                                   HANDLE_FDK_BITSTREAM bs,
129                                   int * const bytesAvailable,
130                                   const UINT alignmentAnchor
131                                 )
132 {
133   int err = 0;
134   FDK_CRCINFO crcInfo;    /* CRC state info */
135   INT crcReg;
136   FDKcrcInit(&crcInfo, 0x07, 0xFF, 8);
137   crcReg = FDKcrcStartReg(&crcInfo, bs, 0);
138   UINT startAnchor = FDKgetValidBits(bs);
139 
140   FDK_ASSERT(pPce != NULL);
141   FDK_ASSERT(bs != NULL);
142   FDK_ASSERT(bytesAvailable != NULL);
143 
144   if ( (startAnchor >= 24) && (*bytesAvailable >= 3)
145     && (FDKreadBits(bs,8) == PCE_HEIGHT_EXT_SYNC) )
146   {
147     int i;
148 
149     for (i=0; i < pPce->NumFrontChannelElements; i++)
150     {
151       if ((pPce->FrontElementHeightInfo[i] = (UCHAR) FDKreadBits(bs,2)) >= PC_NUM_HEIGHT_LAYER) {
152         err = -2; /* height information is out of the valid range */
153       }
154     }
155     for (i=0; i < pPce->NumSideChannelElements; i++)
156     {
157       if ((pPce->SideElementHeightInfo[i] = (UCHAR) FDKreadBits(bs,2)) >= PC_NUM_HEIGHT_LAYER) {
158         err = -2; /* height information is out of the valid range */
159       }
160     }
161     for (i=0; i < pPce->NumBackChannelElements; i++)
162     {
163       if ((pPce->BackElementHeightInfo[i] = (UCHAR) FDKreadBits(bs,2)) >= PC_NUM_HEIGHT_LAYER) {
164         err = -2; /* height information is out of the valid range */
165       }
166     }
167     FDKbyteAlign(bs, alignmentAnchor);
168 
169     FDKcrcEndReg(&crcInfo, bs, crcReg);
170     if ((USHORT)FDKreadBits(bs,8) != FDKcrcGetCRC(&crcInfo)) {
171       /* CRC failed */
172       err = -1;
173     }
174     if (err!=0) {
175       /* Reset whole height information in case an error occured during parsing. The return
176          value ensures that pPce->isValid is set to 0 and implicit channel mapping is used. */
177       FDKmemclear(pPce->FrontElementHeightInfo, sizeof(pPce->FrontElementHeightInfo));
178       FDKmemclear(pPce->SideElementHeightInfo, sizeof(pPce->SideElementHeightInfo));
179       FDKmemclear(pPce->BackElementHeightInfo, sizeof(pPce->BackElementHeightInfo));
180     }
181   }
182   else {
183     /* No valid extension data found -> restore the initial bitbuffer state */
184     FDKpushBack(bs, startAnchor - FDKgetValidBits(bs));
185   }
186 
187   /* Always report the bytes read. */
188   *bytesAvailable -= (startAnchor - FDKgetValidBits(bs)) >> 3;
189 
190   return (err);
191 }
192 
CProgramConfig_Read(CProgramConfig * pPce,HANDLE_FDK_BITSTREAM bs,UINT alignmentAnchor)193 void CProgramConfig_Read(
194                           CProgramConfig *pPce,
195                           HANDLE_FDK_BITSTREAM bs,
196                           UINT alignmentAnchor
197                         )
198 {
199   int i, err = 0;
200   int commentBytes;
201 
202   pPce->NumEffectiveChannels = 0;
203   pPce->NumChannels = 0;
204   pPce->ElementInstanceTag = (UCHAR) FDKreadBits(bs,4);
205   pPce->Profile = (UCHAR) FDKreadBits(bs,2);
206   pPce->SamplingFrequencyIndex = (UCHAR) FDKreadBits(bs,4);
207   pPce->NumFrontChannelElements = (UCHAR) FDKreadBits(bs,4);
208   pPce->NumSideChannelElements = (UCHAR) FDKreadBits(bs,4);
209   pPce->NumBackChannelElements = (UCHAR) FDKreadBits(bs,4);
210   pPce->NumLfeChannelElements = (UCHAR) FDKreadBits(bs,2);
211   pPce->NumAssocDataElements = (UCHAR) FDKreadBits(bs,3);
212   pPce->NumValidCcElements = (UCHAR) FDKreadBits(bs,4);
213 
214   if ((pPce->MonoMixdownPresent = (UCHAR) FDKreadBits(bs,1)) != 0)
215   {
216     pPce->MonoMixdownElementNumber = (UCHAR) FDKreadBits(bs,4);
217   }
218 
219   if ((pPce->StereoMixdownPresent = (UCHAR) FDKreadBits(bs,1)) != 0)
220   {
221     pPce->StereoMixdownElementNumber = (UCHAR) FDKreadBits(bs,4);
222   }
223 
224   if ((pPce->MatrixMixdownIndexPresent = (UCHAR) FDKreadBits(bs,1)) != 0)
225   {
226     pPce->MatrixMixdownIndex = (UCHAR) FDKreadBits(bs,2);
227     pPce->PseudoSurroundEnable = (UCHAR) FDKreadBits(bs,1);
228   }
229 
230   for (i=0; i < pPce->NumFrontChannelElements; i++)
231   {
232     pPce->FrontElementIsCpe[i] = (UCHAR) FDKreadBits(bs,1);
233     pPce->FrontElementTagSelect[i] = (UCHAR) FDKreadBits(bs,4);
234     pPce->NumChannels += pPce->FrontElementIsCpe[i] ? 2 : 1;
235   }
236 
237   for (i=0; i < pPce->NumSideChannelElements; i++)
238   {
239     pPce->SideElementIsCpe[i] = (UCHAR) FDKreadBits(bs,1);
240     pPce->SideElementTagSelect[i] = (UCHAR) FDKreadBits(bs,4);
241     pPce->NumChannels += pPce->SideElementIsCpe[i] ? 2 : 1;
242   }
243 
244   for (i=0; i < pPce->NumBackChannelElements; i++)
245   {
246     pPce->BackElementIsCpe[i] = (UCHAR) FDKreadBits(bs,1);
247     pPce->BackElementTagSelect[i] = (UCHAR) FDKreadBits(bs,4);
248     pPce->NumChannels += pPce->BackElementIsCpe[i] ? 2 : 1;
249   }
250 
251   pPce->NumEffectiveChannels = pPce->NumChannels;
252 
253   for (i=0; i < pPce->NumLfeChannelElements; i++)
254   {
255     pPce->LfeElementTagSelect[i] = (UCHAR) FDKreadBits(bs,4);
256     pPce->NumChannels += 1;
257   }
258 
259   for (i=0; i < pPce->NumAssocDataElements; i++)
260   {
261     pPce->AssocDataElementTagSelect[i] = (UCHAR) FDKreadBits(bs,4);
262   }
263 
264   for (i=0; i < pPce->NumValidCcElements; i++)
265   {
266     pPce->CcElementIsIndSw[i] = (UCHAR) FDKreadBits(bs,1);
267     pPce->ValidCcElementTagSelect[i] = (UCHAR) FDKreadBits(bs,4);
268   }
269 
270   FDKbyteAlign(bs, alignmentAnchor);
271 
272   pPce->CommentFieldBytes = (UCHAR) FDKreadBits(bs,8);
273   commentBytes = pPce->CommentFieldBytes;
274 
275   /* Search for height info extension and read it if available */
276   err = CProgramConfig_ReadHeightExt( pPce, bs, &commentBytes, alignmentAnchor );
277 
278   for (i=0; i < commentBytes; i++)
279   {
280     UCHAR text;
281 
282     text = (UCHAR)FDKreadBits(bs,8);
283 
284     if (i < PC_COMMENTLENGTH)
285     {
286       pPce->Comment[i] = text;
287     }
288   }
289 
290   pPce->isValid = (err) ? 0 : 1;
291 }
292 
293 /*
294  * Compare two program configurations.
295  * Returns the result of the comparison:
296  *  -1 - completely different
297  *   0 - completely equal
298  *   1 - different but same channel configuration
299  *   2 - different channel configuration but same number of channels
300  */
CProgramConfig_Compare(const CProgramConfig * const pPce1,const CProgramConfig * const pPce2)301 int CProgramConfig_Compare ( const CProgramConfig * const pPce1,
302                              const CProgramConfig * const pPce2 )
303 {
304   int result = 0;  /* Innocent until proven false. */
305 
306   if (FDKmemcmp(pPce1, pPce2, sizeof(CProgramConfig)) != 0)
307   { /* Configurations are not completely different.
308        So look into details and analyse the channel configurations: */
309     result = -1;
310 
311     if (pPce1->NumChannels == pPce2->NumChannels)
312     { /* Now the logic changes. We first assume to have the same channel configuration
313          and then prove if this assumption is true. */
314       result = 1;
315 
316       /* Front channels */
317       if (pPce1->NumFrontChannelElements != pPce2->NumFrontChannelElements) {
318         result = 2;  /* different number of front channel elements */
319       } else {
320         int el, numCh1 = 0, numCh2 = 0;
321         for (el = 0; el < pPce1->NumFrontChannelElements; el += 1) {
322           if (pPce1->FrontElementHeightInfo[el] != pPce2->FrontElementHeightInfo[el]) {
323             result = 2; /* different height info */
324             break;
325           }
326           numCh1 += pPce1->FrontElementIsCpe[el] ? 2 : 1;
327           numCh2 += pPce2->FrontElementIsCpe[el] ? 2 : 1;
328         }
329         if (numCh1 != numCh2) {
330           result = 2;  /* different number of front channels */
331         }
332       }
333       /* Side channels */
334       if (pPce1->NumSideChannelElements != pPce2->NumSideChannelElements) {
335         result = 2;  /* different number of side channel elements */
336       } else {
337         int el, numCh1 = 0, numCh2 = 0;
338         for (el = 0; el < pPce1->NumSideChannelElements; el += 1) {
339           if (pPce1->SideElementHeightInfo[el] != pPce2->SideElementHeightInfo[el]) {
340             result = 2; /* different height info */
341             break;
342           }
343           numCh1 += pPce1->SideElementIsCpe[el] ? 2 : 1;
344           numCh2 += pPce2->SideElementIsCpe[el] ? 2 : 1;
345         }
346         if (numCh1 != numCh2) {
347           result = 2;  /* different number of side channels */
348         }
349       }
350       /* Back channels */
351       if (pPce1->NumBackChannelElements != pPce2->NumBackChannelElements) {
352         result = 2;  /* different number of back channel elements */
353       } else {
354         int el, numCh1 = 0, numCh2 = 0;
355         for (el = 0; el < pPce1->NumBackChannelElements; el += 1) {
356           if (pPce1->BackElementHeightInfo[el] != pPce2->BackElementHeightInfo[el]) {
357             result = 2; /* different height info */
358             break;
359           }
360           numCh1 += pPce1->BackElementIsCpe[el] ? 2 : 1;
361           numCh2 += pPce2->BackElementIsCpe[el] ? 2 : 1;
362         }
363         if (numCh1 != numCh2) {
364           result = 2;  /* different number of back channels */
365         }
366       }
367       /* LFE channels */
368       if (pPce1->NumLfeChannelElements != pPce2->NumLfeChannelElements) {
369         result = 2;  /* different number of lfe channels */
370       }
371       /* LFEs are always SCEs so we don't need to count the channels. */
372     }
373   }
374 
375   return result;
376 }
377 
CProgramConfig_GetDefault(CProgramConfig * pPce,const UINT channelConfig)378 void CProgramConfig_GetDefault( CProgramConfig *pPce,
379                                 const UINT channelConfig )
380 {
381   FDK_ASSERT(pPce != NULL);
382 
383   /* Init PCE */
384   CProgramConfig_Init(pPce);
385   pPce->Profile = 1;  /* Set AAC LC because it is the only supported object type. */
386 
387   switch (channelConfig) {
388   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
389   case 32: /* 7.1 side channel configuration as defined in FDK_audio.h */
390     pPce->NumFrontChannelElements  = 2;
391     pPce->FrontElementIsCpe[0]     = 0;
392     pPce->FrontElementIsCpe[1]     = 1;
393     pPce->NumSideChannelElements   = 1;
394     pPce->SideElementIsCpe[0]      = 1;
395     pPce->NumBackChannelElements   = 1;
396     pPce->BackElementIsCpe[0]      = 1;
397     pPce->NumLfeChannelElements    = 1;
398     pPce->NumChannels              = 8;
399     pPce->NumEffectiveChannels     = 7;
400     pPce->isValid                  = 1;
401     break;
402   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
403   case 12:  /* 3/0/4.1ch surround back */
404     pPce->BackElementIsCpe[1]      = 1;
405     pPce->NumChannels             += 1;
406     pPce->NumEffectiveChannels    += 1;
407   case 11:  /* 3/0/3.1ch */
408     pPce->NumFrontChannelElements += 2;
409     pPce->FrontElementIsCpe[0]     = 0;
410     pPce->FrontElementIsCpe[1]     = 1;
411     pPce->NumBackChannelElements  += 2;
412     pPce->BackElementIsCpe[0]      = 1;
413     pPce->BackElementIsCpe[1]     += 0;
414     pPce->NumLfeChannelElements   += 1;
415     pPce->NumChannels             += 7;
416     pPce->NumEffectiveChannels    += 6;
417     pPce->isValid                  = 1;
418     break;
419   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
420   case 14:  /* 2/0/0-3/0/2-0.1ch front height */
421     pPce->FrontElementHeightInfo[2] = 1;      /* Top speaker */
422   case 7:   /* 5/0/2.1ch front */
423     pPce->NumFrontChannelElements += 1;
424     pPce->FrontElementIsCpe[2]     = 1;
425     pPce->NumChannels             += 2;
426     pPce->NumEffectiveChannels    += 2;
427   case 6:   /* 3/0/2.1ch */
428     pPce->NumLfeChannelElements   += 1;
429     pPce->NumChannels             += 1;
430   case 5:   /* 3/0/2.0ch */
431   case 4:   /* 3/0/1.0ch */
432     pPce->NumBackChannelElements  += 1;
433     pPce->BackElementIsCpe[0]      = (channelConfig>4) ? 1 : 0;
434     pPce->NumChannels             += (channelConfig>4) ? 2 : 1;
435     pPce->NumEffectiveChannels    += (channelConfig>4) ? 2 : 1;
436   case 3:   /* 3/0/0.0ch */
437     pPce->NumFrontChannelElements += 1;
438     pPce->FrontElementIsCpe[1]     = 1;
439     pPce->NumChannels             += 2;
440     pPce->NumEffectiveChannels    += 2;
441   case 1:   /* 1/0/0.0ch */
442     pPce->NumFrontChannelElements += 1;
443     pPce->FrontElementIsCpe[0]     = 0;
444     pPce->NumChannels             += 1;
445     pPce->NumEffectiveChannels    += 1;
446     pPce->isValid                  = 1;
447     break;
448   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
449   case 2:   /* 2/0/0.ch */
450     pPce->NumFrontChannelElements  = 1;
451     pPce->FrontElementIsCpe[0]     = 1;
452     pPce->NumChannels             += 2;
453     pPce->NumEffectiveChannels    += 2;
454     pPce->isValid                  = 1;
455     break;
456   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
457   default:
458     pPce->isValid                  = 0;   /* To be explicit! */
459     break;
460   }
461 
462   if (pPce->isValid) {
463     /* Create valid element instance tags */
464     int el, elTagSce = 0, elTagCpe = 0;
465 
466     for (el = 0; el < pPce->NumFrontChannelElements; el += 1) {
467       pPce->FrontElementTagSelect[el] = (pPce->FrontElementIsCpe[el]) ? elTagCpe++ : elTagSce++;
468     }
469     for (el = 0; el < pPce->NumSideChannelElements; el += 1) {
470       pPce->SideElementTagSelect[el] = (pPce->SideElementIsCpe[el]) ? elTagCpe++ : elTagSce++;
471     }
472     for (el = 0; el < pPce->NumBackChannelElements; el += 1) {
473       pPce->BackElementTagSelect[el] = (pPce->BackElementIsCpe[el]) ? elTagCpe++ : elTagSce++;
474     }
475     elTagSce = 0;
476     for (el = 0; el < pPce->NumLfeChannelElements; el += 1) {
477       pPce->LfeElementTagSelect[el] = elTagSce++;
478     }
479   }
480 }
481 #endif /* TP_PCE_ENABLE */
482 
483 /**
484  * \brief get implicit audio channel type for given channelConfig and MPEG ordered channel index
485  * \param channelConfig MPEG channelConfiguration from 1 upto 14
486  * \param index MPEG channel order index
487  * \return audio channel type.
488  */
489 static
getImplicitAudioChannelTypeAndIndex(AUDIO_CHANNEL_TYPE * chType,UCHAR * chIndex,UINT channelConfig,UINT index)490 void getImplicitAudioChannelTypeAndIndex(
491         AUDIO_CHANNEL_TYPE *chType,
492         UCHAR *chIndex,
493         UINT channelConfig,
494         UINT index
495         )
496 {
497   if (index < 3) {
498     *chType = ACT_FRONT;
499     *chIndex = index;
500   } else {
501     switch (channelConfig) {
502       case 4:  /* SCE, CPE, SCE */
503       case 5:  /* SCE, CPE, CPE */
504       case 6:  /* SCE, CPE, CPE, LFE */
505         switch (index) {
506           case 3:
507           case 4:
508             *chType = ACT_BACK;
509             *chIndex = index - 3;
510             break;
511           case 5:
512             *chType = ACT_LFE;
513             *chIndex = 0;
514             break;
515         }
516         break;
517       case 7:  /* SCE,CPE,CPE,CPE,LFE */
518         switch (index) {
519           case 3:
520           case 4:
521             *chType = ACT_FRONT;
522             *chIndex = index;
523             break;
524           case 5:
525           case 6:
526             *chType = ACT_BACK;
527             *chIndex = index - 5;
528             break;
529           case 7:
530             *chType = ACT_LFE;
531             *chIndex = 0;
532             break;
533         }
534         break;
535       case 11:  /* SCE,CPE,CPE,SCE,LFE */
536         if (index < 6) {
537           *chType = ACT_BACK;
538           *chIndex = index - 3;
539         } else {
540           *chType = ACT_LFE;
541           *chIndex = 0;
542         }
543         break;
544       case 12:  /* SCE,CPE,CPE,CPE,LFE */
545         if (index < 7) {
546           *chType = ACT_BACK;
547           *chIndex = index - 3;
548         } else {
549           *chType = ACT_LFE;
550           *chIndex = 0;
551         }
552         break;
553       case 14:  /* SCE,CPE,CPE,LFE,CPE */
554         switch (index) {
555           case 3:
556           case 4:
557             *chType = ACT_BACK;
558             *chIndex = index - 3;
559             break;
560           case 5:
561             *chType = ACT_LFE;
562             *chIndex = 0;
563             break;
564           case 6:
565           case 7:
566             *chType = ACT_FRONT_TOP;
567             *chIndex = index - 6;  /* handle the top layer independently */
568             break;
569         }
570         break;
571       default:
572         *chType = ACT_NONE;
573         break;
574     }
575   }
576 }
577 
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)578 int CProgramConfig_LookupElement(
579         CProgramConfig *pPce,
580         UINT            channelConfig,
581         const UINT      tag,
582         const UINT      channelIdx,
583         UCHAR           chMapping[],
584         AUDIO_CHANNEL_TYPE chType[],
585         UCHAR           chIndex[],
586         UCHAR          *elMapping,
587         MP4_ELEMENT_ID  elList[],
588         MP4_ELEMENT_ID  elType
589        )
590 {
591   if (channelConfig > 0)
592   {
593     /* Constant channel mapping must have
594        been set during initialization. */
595     if ( elType == ID_SCE
596       || elType == ID_CPE
597       || elType == ID_LFE )
598     {
599       *elMapping = pPce->elCounter;
600       if (elList[pPce->elCounter] != elType) {
601         /* Not in the list */
602         if ( (channelConfig == 2) && (elType == ID_SCE) )
603         { /* This scenario occurs with HE-AAC v2 streams of buggy encoders.
604              Due to other decoder implementations decoding of these kind of streams is desired. */
605           channelConfig = 1;
606         } else {
607           return 0;
608         }
609       }
610       /* Assume all front channels */
611       getImplicitAudioChannelTypeAndIndex(&chType[channelIdx], &chIndex[channelIdx], channelConfig, channelIdx);
612       if (elType == ID_CPE) {
613         chType[channelIdx+1] = chType[channelIdx];
614         chIndex[channelIdx+1] = chIndex[channelIdx]+1;
615       }
616       pPce->elCounter++;
617     }
618     /* Accept all non-channel elements, too. */
619     return 1;
620   }
621   else
622   {
623 #ifdef TP_PCE_ENABLE
624     if (!pPce->isValid)
625 #endif /* TP_PCE_ENABLE */
626     {
627       /* Implicit channel mapping. */
628       if ( elType == ID_SCE
629         || elType == ID_CPE
630         || elType == ID_LFE )
631       {
632         /* Store all channel element IDs */
633         elList[pPce->elCounter] = elType;
634         *elMapping = pPce->elCounter++;
635       }
636     }
637 #ifdef  TP_PCE_ENABLE
638     else {
639       /* Accept the additional channel(s), only if the tag is in the lists */
640       int isCpe = 0, i;
641       /* Element counter */
642       int ec[PC_NUM_HEIGHT_LAYER] = {0};
643       /* Channel counters */
644       int cc[PC_NUM_HEIGHT_LAYER] = {0};
645       int fc[PC_NUM_HEIGHT_LAYER] = {0};
646       int sc[PC_NUM_HEIGHT_LAYER] = {0};
647       int bc[PC_NUM_HEIGHT_LAYER] = {0};
648       int lc = 0;;
649 
650       /* General MPEG (PCE) composition rules:
651          - Over all:
652              <normal height channels><top height channels><bottom height channels>
653          - Within each height layer:
654              <front channels><side channels><back channels>
655          - Exception:
656              The LFE channels have no height info and thus they are arranged at the very
657              end of the normal height layer channels.
658        */
659 
660       switch (elType)
661       {
662       case ID_CPE:
663         isCpe = 1;
664       case ID_SCE:
665         /* search in front channels */
666         for (i = 0; i < pPce->NumFrontChannelElements; i++) {
667           int heightLayer = pPce->FrontElementHeightInfo[i];
668           if (isCpe == pPce->FrontElementIsCpe[i] && pPce->FrontElementTagSelect[i] == tag) {
669             int h, elIdx = ec[heightLayer], chIdx = cc[heightLayer];
670             AUDIO_CHANNEL_TYPE aChType = (AUDIO_CHANNEL_TYPE)((heightLayer<<4) | ACT_FRONT);
671             for (h = heightLayer-1; h >= 0; h-=1) {
672               int el;
673               /* Count front channels/elements */
674               for (el = 0; el < pPce->NumFrontChannelElements; el+=1) {
675                 if (pPce->FrontElementHeightInfo[el] == h) {
676                   elIdx += 1;
677                   chIdx += (pPce->FrontElementIsCpe[el]) ? 2 : 1;
678                 }
679               }
680               /* Count side channels/elements */
681               for (el = 0; el < pPce->NumSideChannelElements; el+=1) {
682                 if (pPce->SideElementHeightInfo[el] == h) {
683                   elIdx += 1;
684                   chIdx += (pPce->SideElementIsCpe[el]) ? 2 : 1;
685                 }
686               }
687               /* Count back channels/elements */
688               for (el = 0; el < pPce->NumBackChannelElements; el+=1) {
689                 if (pPce->BackElementHeightInfo[el] == h) {
690                   elIdx += 1;
691                   chIdx += (pPce->BackElementIsCpe[el]) ? 2 : 1;
692                 }
693               }
694               if (h == 0) {  /* normal height */
695                 elIdx += pPce->NumLfeChannelElements;
696                 chIdx += pPce->NumLfeChannelElements;
697               }
698             }
699             chMapping[chIdx] = channelIdx;
700             chType[chIdx] = aChType;
701             chIndex[chIdx] = fc[heightLayer];
702             if (isCpe) {
703               chMapping[chIdx+1] = channelIdx+1;
704               chType[chIdx+1] = aChType;
705               chIndex[chIdx+1] = fc[heightLayer]+1;
706             }
707             *elMapping = elIdx;
708             return 1;
709           }
710           ec[heightLayer] += 1;
711           if (pPce->FrontElementIsCpe[i]) {
712             cc[heightLayer] += 2;
713             fc[heightLayer] += 2;
714           } else {
715             cc[heightLayer] += 1;
716             fc[heightLayer] += 1;
717           }
718         }
719         /* search in side channels */
720         for (i = 0; i < pPce->NumSideChannelElements; i++) {
721           int heightLayer = pPce->SideElementHeightInfo[i];
722           if (isCpe == pPce->SideElementIsCpe[i] && pPce->SideElementTagSelect[i] == tag) {
723             int h, elIdx = ec[heightLayer], chIdx = cc[heightLayer];
724             AUDIO_CHANNEL_TYPE aChType = (AUDIO_CHANNEL_TYPE)((heightLayer<<4) | ACT_SIDE);
725             for (h = heightLayer-1; h >= 0; h-=1) {
726               int el;
727               /* Count front channels/elements */
728               for (el = 0; el < pPce->NumFrontChannelElements; el+=1) {
729                 if (pPce->FrontElementHeightInfo[el] == h) {
730                   elIdx += 1;
731                   chIdx += (pPce->FrontElementIsCpe[el]) ? 2 : 1;
732                 }
733               }
734               /* Count side channels/elements */
735               for (el = 0; el < pPce->NumSideChannelElements; el+=1) {
736                 if (pPce->SideElementHeightInfo[el] == h) {
737                   elIdx += 1;
738                   chIdx += (pPce->SideElementIsCpe[el]) ? 2 : 1;
739                 }
740               }
741               /* Count back channels/elements */
742               for (el = 0; el < pPce->NumBackChannelElements; el+=1) {
743                 if (pPce->BackElementHeightInfo[el] == h) {
744                   elIdx += 1;
745                   chIdx += (pPce->BackElementIsCpe[el]) ? 2 : 1;
746                 }
747               }
748               if (h == 0) {  /* LFE channels belong to the normal height layer */
749                 elIdx += pPce->NumLfeChannelElements;
750                 chIdx += pPce->NumLfeChannelElements;
751               }
752             }
753             chMapping[chIdx] = channelIdx;
754             chType[chIdx] = aChType;
755             chIndex[chIdx] = sc[heightLayer];
756             if (isCpe) {
757               chMapping[chIdx+1] = channelIdx+1;
758               chType[chIdx+1] = aChType;
759               chIndex[chIdx+1] = sc[heightLayer]+1;
760             }
761             *elMapping = elIdx;
762             return 1;
763           }
764           ec[heightLayer] += 1;
765           if (pPce->SideElementIsCpe[i]) {
766             cc[heightLayer] += 2;
767             sc[heightLayer] += 2;
768           } else {
769             cc[heightLayer] += 1;
770             sc[heightLayer] += 1;
771           }
772         }
773         /* search in back channels */
774         for (i = 0; i < pPce->NumBackChannelElements; i++) {
775           int heightLayer = pPce->BackElementHeightInfo[i];
776           if (isCpe == pPce->BackElementIsCpe[i] && pPce->BackElementTagSelect[i] == tag) {
777             int h, elIdx = ec[heightLayer], chIdx = cc[heightLayer];
778             AUDIO_CHANNEL_TYPE aChType = (AUDIO_CHANNEL_TYPE)((heightLayer<<4) | ACT_BACK);
779             for (h = heightLayer-1; h >= 0; h-=1) {
780               int el;
781               /* Count front channels/elements */
782               for (el = 0; el < pPce->NumFrontChannelElements; el+=1) {
783                 if (pPce->FrontElementHeightInfo[el] == h) {
784                   elIdx += 1;
785                   chIdx += (pPce->FrontElementIsCpe[el]) ? 2 : 1;
786                 }
787               }
788               /* Count side channels/elements */
789               for (el = 0; el < pPce->NumSideChannelElements; el+=1) {
790                 if (pPce->SideElementHeightInfo[el] == h) {
791                   elIdx += 1;
792                   chIdx += (pPce->SideElementIsCpe[el]) ? 2 : 1;
793                 }
794               }
795               /* Count back channels/elements */
796               for (el = 0; el < pPce->NumBackChannelElements; el+=1) {
797                 if (pPce->BackElementHeightInfo[el] == h) {
798                   elIdx += 1;
799                   chIdx += (pPce->BackElementIsCpe[el]) ? 2 : 1;
800                 }
801               }
802               if (h == 0) {  /* normal height */
803                 elIdx += pPce->NumLfeChannelElements;
804                 chIdx += pPce->NumLfeChannelElements;
805               }
806             }
807             chMapping[chIdx] = channelIdx;
808             chType[chIdx] = aChType;
809             chIndex[chIdx] = bc[heightLayer];
810             if (isCpe) {
811               chMapping[chIdx+1] = channelIdx+1;
812               chType[chIdx+1] = aChType;
813               chIndex[chIdx+1] = bc[heightLayer]+1;
814             }
815             *elMapping = elIdx;
816             return 1;
817           }
818           ec[heightLayer] += 1;
819           if (pPce->BackElementIsCpe[i]) {
820             cc[heightLayer] += 2;
821             bc[heightLayer] += 2;
822           } else {
823             cc[heightLayer] += 1;
824             bc[heightLayer] += 1;
825           }
826         }
827         break;
828 
829       case ID_LFE:
830       { /* Unfortunately we have to go through all normal height
831            layer elements to get the position of the LFE channels.
832            Start with counting the front channels/elements at normal height */
833         for (i = 0; i < pPce->NumFrontChannelElements; i+=1) {
834           int heightLayer = pPce->FrontElementHeightInfo[i];
835           ec[heightLayer] += 1;
836           cc[heightLayer] += (pPce->FrontElementIsCpe[i]) ? 2 : 1;
837         }
838         /* Count side channels/elements at normal height */
839         for (i = 0; i < pPce->NumSideChannelElements; i+=1) {
840           int heightLayer = pPce->SideElementHeightInfo[i];
841           ec[heightLayer] += 1;
842           cc[heightLayer] += (pPce->SideElementIsCpe[i]) ? 2 : 1;
843         }
844         /* Count back channels/elements at normal height */
845         for (i = 0; i < pPce->NumBackChannelElements; i+=1) {
846           int heightLayer = pPce->BackElementHeightInfo[i];
847           ec[heightLayer] += 1;
848           cc[heightLayer] += (pPce->BackElementIsCpe[i]) ? 2 : 1;
849         }
850 
851         /* search in lfe channels */
852         for (i = 0; i < pPce->NumLfeChannelElements; i++) {
853           int elIdx = ec[0];  /* LFE channels belong to the normal height layer */
854           int chIdx = cc[0];
855           if ( pPce->LfeElementTagSelect[i] == tag ) {
856             chMapping[chIdx] = channelIdx;
857             *elMapping = elIdx;
858             chType[chIdx] = ACT_LFE;
859             chIndex[chIdx] = lc;
860             return 1;
861           }
862           ec[0] += 1;
863           cc[0] += 1;
864           lc += 1;
865         }
866       } break;
867 
868       /* Non audio elements */
869       case ID_CCE:
870         /* search in cce channels */
871         for (i = 0; i < pPce->NumValidCcElements; i++) {
872           if (pPce->ValidCcElementTagSelect[i] == tag) {
873             return 1;
874           }
875         }
876         break;
877       case ID_DSE:
878         /* search associated data elements */
879         for (i = 0; i < pPce->NumAssocDataElements; i++) {
880           if (pPce->AssocDataElementTagSelect[i] == tag) {
881             return 1;
882           }
883         }
884         break;
885       default:
886         return 0;
887       }
888       return 0;  /* not found in any list */
889     }
890 #endif /* TP_PCE_ENABLE */
891   }
892 
893   return 1;
894 }
895 
896 #ifdef  TP_PCE_ENABLE
CProgramConfig_GetElementTable(const CProgramConfig * pPce,MP4_ELEMENT_ID elList[],const INT elListSize,UCHAR * pChMapIdx)897 int CProgramConfig_GetElementTable(
898         const CProgramConfig *pPce,
899         MP4_ELEMENT_ID  elList[],
900         const INT elListSize,
901         UCHAR *pChMapIdx
902        )
903 {
904   int i, el = 0;
905 
906   FDK_ASSERT(elList != NULL);
907   FDK_ASSERT(pChMapIdx != NULL);
908 
909   *pChMapIdx = 0;
910 
911   if ( elListSize
912     < pPce->NumFrontChannelElements + pPce->NumSideChannelElements + pPce->NumBackChannelElements + pPce->NumLfeChannelElements
913     )
914   {
915     return 0;
916   }
917 
918   for (i=0; i < pPce->NumFrontChannelElements; i++)
919   {
920     elList[el++] = (pPce->FrontElementIsCpe[i]) ?  ID_CPE : ID_SCE;
921   }
922 
923   for (i=0; i < pPce->NumSideChannelElements; i++)
924   {
925     elList[el++] = (pPce->SideElementIsCpe[i]) ?  ID_CPE : ID_SCE;
926   }
927 
928   for (i=0; i < pPce->NumBackChannelElements; i++)
929   {
930     elList[el++] = (pPce->BackElementIsCpe[i]) ?  ID_CPE : ID_SCE;
931   }
932 
933   for (i=0; i < pPce->NumLfeChannelElements; i++)
934   {
935     elList[el++] = ID_LFE;
936   }
937 
938 
939   /* Find an corresponding channel configuration if possible */
940   switch (pPce->NumChannels) {
941   case 1: case 2: case 3: case 4: case 5: case 6:
942     /* One and two channels have no alternatives. The other ones are mapped directly to the
943        corresponding channel config. Because of legacy reasons or for lack of alternative mappings. */
944     *pChMapIdx = pPce->NumChannels;
945     break;
946   case 7:
947     {
948       C_ALLOC_SCRATCH_START(tmpPce, CProgramConfig, 1);
949       /* Create a PCE for the config to test ... */
950       CProgramConfig_GetDefault(tmpPce, 11);
951       /* ... and compare it with the given one. */
952       *pChMapIdx = (!(CProgramConfig_Compare(pPce, tmpPce)&0xE)) ? 11 : 0;
953       /* If compare result is 0 or 1 we can be sure that it is channel config 11. */
954       C_ALLOC_SCRATCH_END(tmpPce, CProgramConfig, 1);
955     }
956     break;
957   case 8:
958     { /* Try the four possible 7.1ch configurations. One after the other. */
959       UCHAR testCfg[4] = { 32, 14, 12, 7};
960       C_ALLOC_SCRATCH_START(tmpPce, CProgramConfig, 1);
961       for (i=0; i<4; i+=1) {
962         /* Create a PCE for the config to test ... */
963         CProgramConfig_GetDefault(tmpPce, testCfg[i]);
964         /* ... and compare it with the given one. */
965         if (!(CProgramConfig_Compare(pPce, tmpPce)&0xE)) {
966           /* If the compare result is 0 or 1 than the two channel configurations match. */
967           /* Explicit mapping of 7.1 side channel configuration to 7.1 rear channel mapping. */
968           *pChMapIdx = (testCfg[i]==32) ? 12 : testCfg[i];
969         }
970       }
971       C_ALLOC_SCRATCH_END(tmpPce, CProgramConfig, 1);
972     }
973     break;
974   default:
975     /* The PCE does not match any predefined channel configuration. */
976     *pChMapIdx = 0;
977     break;
978   }
979 
980   return el;
981 }
982 #endif
983 
getAOT(HANDLE_FDK_BITSTREAM bs)984 static AUDIO_OBJECT_TYPE getAOT(HANDLE_FDK_BITSTREAM bs)
985 {
986   int tmp = 0;
987 
988   tmp = FDKreadBits(bs,5);
989   if (tmp == AOT_ESCAPE) {
990     int tmp2 = FDKreadBits(bs,6);
991     tmp = 32 + tmp2;
992   }
993 
994   return (AUDIO_OBJECT_TYPE)tmp;
995 }
996 
getSampleRate(HANDLE_FDK_BITSTREAM bs,UCHAR * index,int nBits)997 static INT getSampleRate(HANDLE_FDK_BITSTREAM bs, UCHAR *index, int nBits)
998 {
999   INT sampleRate;
1000   int idx;
1001 
1002   idx = FDKreadBits(bs, nBits);
1003   if( idx == (1<<nBits)-1 ) {
1004     if(FDKgetValidBits(bs) < 24) {
1005       return 0;
1006     }
1007     sampleRate = FDKreadBits(bs,24);
1008   } else {
1009     sampleRate = SamplingRateTable[idx];
1010   }
1011 
1012   *index = idx;
1013 
1014   return sampleRate;
1015 }
1016 
1017 #ifdef TP_GA_ENABLE
1018 static
GaSpecificConfig_Parse(CSGaSpecificConfig * self,CSAudioSpecificConfig * asc,HANDLE_FDK_BITSTREAM bs,UINT ascStartAnchor)1019 TRANSPORTDEC_ERROR GaSpecificConfig_Parse( CSGaSpecificConfig    *self,
1020                                            CSAudioSpecificConfig *asc,
1021                                            HANDLE_FDK_BITSTREAM   bs,
1022                                            UINT                   ascStartAnchor )
1023 {
1024   TRANSPORTDEC_ERROR ErrorStatus = TRANSPORTDEC_OK;
1025 
1026   self->m_frameLengthFlag = FDKreadBits(bs,1);
1027 
1028   self->m_dependsOnCoreCoder = FDKreadBits(bs,1);
1029 
1030   if( self->m_dependsOnCoreCoder )
1031     self->m_coreCoderDelay = FDKreadBits(bs,14);
1032 
1033   self->m_extensionFlag = FDKreadBits(bs,1);
1034 
1035   if( asc->m_channelConfiguration == 0 ) {
1036     CProgramConfig_Read(&asc->m_progrConfigElement, bs, ascStartAnchor);
1037   }
1038 
1039   if ((asc->m_aot == AOT_AAC_SCAL) || (asc->m_aot == AOT_ER_AAC_SCAL)) {
1040     self->m_layer = FDKreadBits(bs,3);
1041   }
1042 
1043   if (self->m_extensionFlag) {
1044     if (asc->m_aot == AOT_ER_BSAC) {
1045       self->m_numOfSubFrame = FDKreadBits(bs,5);
1046       self->m_layerLength   = FDKreadBits(bs,11);
1047     }
1048 
1049     if ((asc->m_aot == AOT_ER_AAC_LC)   || (asc->m_aot == AOT_ER_AAC_LTP)  ||
1050         (asc->m_aot == AOT_ER_AAC_SCAL) || (asc->m_aot == AOT_ER_AAC_LD))
1051     {
1052       asc->m_vcb11Flag = FDKreadBits(bs,1); /* aacSectionDataResilienceFlag */
1053       asc->m_rvlcFlag  = FDKreadBits(bs,1); /* aacScalefactorDataResilienceFlag */
1054       asc->m_hcrFlag   = FDKreadBits(bs,1); /* aacSpectralDataResilienceFlag */
1055     }
1056 
1057     self->m_extensionFlag3 = FDKreadBits(bs,1);
1058 
1059   }
1060   return (ErrorStatus);
1061 }
1062 #endif /* TP_GA_ENABLE */
1063 
1064 
1065 
1066 
1067 
1068 #ifdef TP_ELD_ENABLE
1069 
ld_sbr_header(const CSAudioSpecificConfig * asc,HANDLE_FDK_BITSTREAM hBs,CSTpCallBacks * cb)1070 static INT ld_sbr_header( const CSAudioSpecificConfig *asc,
1071                            HANDLE_FDK_BITSTREAM hBs,
1072                            CSTpCallBacks *cb )
1073 {
1074   const int channelConfiguration = asc->m_channelConfiguration;
1075   int i = 0;
1076   INT error = 0;
1077 
1078   if (channelConfiguration == 2) {
1079     error = cb->cbSbr(cb->cbSbrData, hBs, asc->m_samplingFrequency, asc->m_extensionSamplingFrequency, asc->m_samplesPerFrame, AOT_ER_AAC_ELD, ID_CPE, i++);
1080   } else {
1081     error = cb->cbSbr(cb->cbSbrData, hBs, asc->m_samplingFrequency, asc->m_extensionSamplingFrequency, asc->m_samplesPerFrame, AOT_ER_AAC_ELD, ID_SCE, i++);
1082   }
1083 
1084   switch ( channelConfiguration ) {
1085     case 14:
1086     case 12:
1087     case 7:
1088       error |= cb->cbSbr(cb->cbSbrData, hBs, asc->m_samplingFrequency, asc->m_extensionSamplingFrequency, asc->m_samplesPerFrame, AOT_ER_AAC_ELD, ID_CPE, i++);
1089     case 6:
1090     case 5:
1091       error |= cb->cbSbr(cb->cbSbrData, hBs, asc->m_samplingFrequency, asc->m_extensionSamplingFrequency, asc->m_samplesPerFrame, AOT_ER_AAC_ELD, ID_CPE, i++);
1092     case 3:
1093       error |= cb->cbSbr(cb->cbSbrData, hBs, asc->m_samplingFrequency, asc->m_extensionSamplingFrequency, asc->m_samplesPerFrame, AOT_ER_AAC_ELD, ID_CPE, i++);
1094       break;
1095 
1096     case 11:
1097       error |= cb->cbSbr(cb->cbSbrData, hBs, asc->m_samplingFrequency, asc->m_extensionSamplingFrequency, asc->m_samplesPerFrame, AOT_ER_AAC_ELD, ID_CPE, i++);
1098     case 4:
1099       error |= cb->cbSbr(cb->cbSbrData, hBs, asc->m_samplingFrequency, asc->m_extensionSamplingFrequency, asc->m_samplesPerFrame, AOT_ER_AAC_ELD, ID_CPE, i++);
1100       error |= cb->cbSbr(cb->cbSbrData, hBs, asc->m_samplingFrequency, asc->m_extensionSamplingFrequency, asc->m_samplesPerFrame, AOT_ER_AAC_ELD, ID_SCE, i++);
1101       break;
1102   }
1103 
1104   return error;
1105 }
1106 
1107 static
EldSpecificConfig_Parse(CSAudioSpecificConfig * asc,HANDLE_FDK_BITSTREAM hBs,CSTpCallBacks * cb)1108 TRANSPORTDEC_ERROR EldSpecificConfig_Parse(
1109         CSAudioSpecificConfig *asc,
1110         HANDLE_FDK_BITSTREAM hBs,
1111         CSTpCallBacks *cb
1112         )
1113 {
1114   TRANSPORTDEC_ERROR ErrorStatus = TRANSPORTDEC_OK;
1115   CSEldSpecificConfig *esc = &asc->m_sc.m_eldSpecificConfig;
1116   ASC_ELD_EXT_TYPE eldExtType;
1117   int eldExtLen, len, cnt;
1118 
1119   FDKmemclear(esc, sizeof(CSEldSpecificConfig));
1120 
1121   esc->m_frameLengthFlag = FDKreadBits(hBs, 1 );
1122   if (esc->m_frameLengthFlag) {
1123     asc->m_samplesPerFrame = 480;
1124   } else {
1125     asc->m_samplesPerFrame = 512;
1126   }
1127 
1128   asc->m_vcb11Flag = FDKreadBits(hBs, 1 );
1129   asc->m_rvlcFlag  = FDKreadBits(hBs, 1 );
1130   asc->m_hcrFlag   = FDKreadBits(hBs, 1 );
1131 
1132   esc->m_sbrPresentFlag     = FDKreadBits(hBs, 1 );
1133 
1134   if (esc->m_sbrPresentFlag == 1) {
1135     esc->m_sbrSamplingRate    = FDKreadBits(hBs, 1 ); /* 0: single rate, 1: dual rate */
1136     esc->m_sbrCrcFlag         = FDKreadBits(hBs, 1 );
1137 
1138     asc->m_extensionSamplingFrequency = asc->m_samplingFrequency << esc->m_sbrSamplingRate;
1139 
1140     if (cb->cbSbr != NULL){
1141       if ( 0 != ld_sbr_header(asc, hBs, cb) ) {
1142         return TRANSPORTDEC_PARSE_ERROR;
1143       }
1144     } else {
1145       return TRANSPORTDEC_UNSUPPORTED_FORMAT;
1146     }
1147   }
1148   esc->m_useLdQmfTimeAlign = 0;
1149 
1150   /* new ELD syntax */
1151   /* parse ExtTypeConfigData */
1152   while ((eldExtType = (ASC_ELD_EXT_TYPE)FDKreadBits(hBs, 4 )) != ELDEXT_TERM) {
1153     eldExtLen = len = FDKreadBits(hBs, 4 );
1154     if ( len == 0xf ) {
1155       len = FDKreadBits(hBs, 8 );
1156       eldExtLen += len;
1157 
1158       if ( len == 0xff ) {
1159         len = FDKreadBits(hBs, 16 );
1160         eldExtLen += len;
1161       }
1162     }
1163 
1164     switch (eldExtType) {
1165       default:
1166         for(cnt=0; cnt<eldExtLen; cnt++) {
1167           FDKreadBits(hBs, 8 );
1168         }
1169         break;
1170       /* add future eld extension configs here */
1171     }
1172   }
1173 bail:
1174   return (ErrorStatus);
1175 }
1176 #endif /* TP_ELD_ENABLE */
1177 
1178 
1179 static
AudioSpecificConfig_ExtensionParse(CSAudioSpecificConfig * self,HANDLE_FDK_BITSTREAM bs,CSTpCallBacks * cb)1180 TRANSPORTDEC_ERROR AudioSpecificConfig_ExtensionParse(CSAudioSpecificConfig *self, HANDLE_FDK_BITSTREAM bs, CSTpCallBacks *cb)
1181 {
1182   TP_ASC_EXTENSION_ID  lastAscExt, ascExtId = ASCEXT_UNKOWN;
1183   INT  bitsAvailable = (INT)FDKgetValidBits(bs);
1184 
1185   while (bitsAvailable >= 11)
1186   {
1187     lastAscExt = ascExtId;
1188     ascExtId   = (TP_ASC_EXTENSION_ID)FDKreadBits(bs, 11);
1189     bitsAvailable -= 11;
1190 
1191     switch (ascExtId) {
1192     case ASCEXT_SBR:    /* 0x2b7 */
1193       if ( (self->m_extensionAudioObjectType != AOT_SBR) && (bitsAvailable >= 5) ) {
1194         self->m_extensionAudioObjectType = getAOT(bs);
1195 
1196         if ( (self->m_extensionAudioObjectType == AOT_SBR)
1197           || (self->m_extensionAudioObjectType == AOT_ER_BSAC) )
1198         { /* Get SBR extension configuration */
1199           self->m_sbrPresentFlag = FDKreadBits(bs, 1);
1200           bitsAvailable -= 1;
1201 
1202           if ( self->m_sbrPresentFlag == 1 ) {
1203             self->m_extensionSamplingFrequency = getSampleRate(bs, &self->m_extensionSamplingFrequencyIndex, 4);
1204 
1205             if ((INT)self->m_extensionSamplingFrequency <= 0) {
1206               return TRANSPORTDEC_PARSE_ERROR;
1207             }
1208           }
1209           if ( self->m_extensionAudioObjectType == AOT_ER_BSAC ) {
1210             self->m_extensionChannelConfiguration = FDKreadBits(bs, 4);
1211             bitsAvailable -= 4;
1212           }
1213         }
1214         /* Update counter because of variable length fields (AOT and sampling rate) */
1215         bitsAvailable = (INT)FDKgetValidBits(bs);
1216       }
1217       break;
1218     case ASCEXT_PS:     /* 0x548 */
1219       if ( (lastAscExt == ASCEXT_SBR)
1220         && (self->m_extensionAudioObjectType == AOT_SBR)
1221         && (bitsAvailable > 0) )
1222       { /* Get PS extension configuration */
1223         self->m_psPresentFlag = FDKreadBits(bs, 1);
1224         bitsAvailable -= 1;
1225       }
1226       break;
1227     default:
1228       /* Just ignore anything. */
1229       return TRANSPORTDEC_OK;
1230     }
1231   }
1232 
1233   return TRANSPORTDEC_OK;
1234 }
1235 
1236 /*
1237  * API Functions
1238  */
1239 
AudioSpecificConfig_Init(CSAudioSpecificConfig * asc)1240 void AudioSpecificConfig_Init(CSAudioSpecificConfig *asc)
1241 {
1242   FDKmemclear(asc, sizeof(CSAudioSpecificConfig));
1243 
1244   /* Init all values that should not be zero. */
1245   asc->m_aot                    = AOT_NONE;
1246   asc->m_samplingFrequencyIndex = 0xf;
1247   asc->m_epConfig               = -1;
1248   asc->m_extensionAudioObjectType        = AOT_NULL_OBJECT;
1249 #ifdef TP_PCE_ENABLE
1250   CProgramConfig_Init(&asc->m_progrConfigElement);
1251 #endif
1252 }
1253 
AudioSpecificConfig_Parse(CSAudioSpecificConfig * self,HANDLE_FDK_BITSTREAM bs,int fExplicitBackwardCompatible,CSTpCallBacks * cb)1254 TRANSPORTDEC_ERROR AudioSpecificConfig_Parse(
1255         CSAudioSpecificConfig *self,
1256         HANDLE_FDK_BITSTREAM   bs,
1257         int                    fExplicitBackwardCompatible,
1258         CSTpCallBacks      *cb
1259         )
1260 {
1261   TRANSPORTDEC_ERROR ErrorStatus = TRANSPORTDEC_OK;
1262   UINT ascStartAnchor = FDKgetValidBits(bs);
1263   int frameLengthFlag = -1;
1264 
1265   AudioSpecificConfig_Init(self);
1266 
1267   self->m_aot = getAOT(bs);
1268   self->m_samplingFrequency = getSampleRate(bs, &self->m_samplingFrequencyIndex, 4);
1269   if (self->m_samplingFrequency <= 0) {
1270     return TRANSPORTDEC_PARSE_ERROR;
1271   }
1272 
1273   self->m_channelConfiguration = FDKreadBits(bs,4);
1274 
1275   /* SBR extension ( explicit non-backwards compatible mode ) */
1276   self->m_sbrPresentFlag = 0;
1277   self->m_psPresentFlag  = 0;
1278 
1279   if ( self->m_aot == AOT_SBR || self->m_aot == AOT_PS ) {
1280     self->m_extensionAudioObjectType = AOT_SBR;
1281 
1282     self->m_sbrPresentFlag = 1;
1283     if ( self->m_aot == AOT_PS ) {
1284       self->m_psPresentFlag = 1;
1285     }
1286 
1287     self->m_extensionSamplingFrequency = getSampleRate(bs, &self->m_extensionSamplingFrequencyIndex, 4);
1288     self->m_aot = getAOT(bs);
1289 
1290   } else {
1291     self->m_extensionAudioObjectType = AOT_NULL_OBJECT;
1292   }
1293 
1294   /* Parse whatever specific configs */
1295   switch (self->m_aot)
1296   {
1297 #ifdef TP_GA_ENABLE
1298     case AOT_AAC_LC:
1299     case AOT_ER_AAC_LC:
1300     case AOT_ER_AAC_LD:
1301     case AOT_ER_AAC_SCAL:
1302     case AOT_ER_BSAC:
1303       if ((ErrorStatus = GaSpecificConfig_Parse(&self->m_sc.m_gaSpecificConfig, self, bs, ascStartAnchor)) != TRANSPORTDEC_OK ) {
1304         return (ErrorStatus);
1305       }
1306       frameLengthFlag = self->m_sc.m_gaSpecificConfig.m_frameLengthFlag;
1307       break;
1308 #endif /* TP_GA_ENABLE */
1309     case AOT_MPEGS:
1310       if (cb->cbSsc != NULL) {
1311         cb->cbSsc(
1312                 cb->cbSscData,
1313                 bs,
1314                 self->m_aot,
1315                 self->m_samplingFrequency,
1316                 1,
1317                 0  /* don't know the length */
1318                 );
1319       } else {
1320         return TRANSPORTDEC_UNSUPPORTED_FORMAT;
1321       }
1322       break;
1323 #ifdef TP_ELD_ENABLE
1324     case AOT_ER_AAC_ELD:
1325       if ((ErrorStatus = EldSpecificConfig_Parse(self, bs, cb)) != TRANSPORTDEC_OK ) {
1326         return (ErrorStatus);
1327       }
1328       frameLengthFlag = self->m_sc.m_eldSpecificConfig.m_frameLengthFlag;
1329       self->m_sbrPresentFlag = self->m_sc.m_eldSpecificConfig.m_sbrPresentFlag;
1330       self->m_extensionSamplingFrequency = (self->m_sc.m_eldSpecificConfig.m_sbrSamplingRate+1) * self->m_samplingFrequency;
1331       break;
1332 #endif /* TP_ELD_ENABLE */
1333 
1334     default:
1335       return TRANSPORTDEC_UNSUPPORTED_FORMAT;
1336       break;
1337   }
1338 
1339   /* Frame length */
1340   switch (self->m_aot)
1341   {
1342 #if defined(TP_GA_ENABLE) || defined(TP_USAC_ENABLE)
1343     case AOT_AAC_LC:
1344     case AOT_ER_AAC_LC:
1345     case AOT_ER_AAC_SCAL:
1346     case AOT_ER_BSAC:
1347     /*case AOT_USAC:*/
1348       if (!frameLengthFlag)
1349         self->m_samplesPerFrame = 1024;
1350       else
1351         self->m_samplesPerFrame = 960;
1352       break;
1353 #endif /* TP_GA_ENABLE */
1354 #if defined(TP_GA_ENABLE)
1355     case AOT_ER_AAC_LD:
1356       if (!frameLengthFlag)
1357         self->m_samplesPerFrame = 512;
1358       else
1359         self->m_samplesPerFrame = 480;
1360       break;
1361 #endif /* defined(TP_GA_ENABLE) */
1362     default:
1363       break;
1364   }
1365 
1366   switch (self->m_aot)
1367   {
1368     case AOT_ER_AAC_LC:
1369     case AOT_ER_AAC_LD:
1370     case AOT_ER_AAC_ELD:
1371     case AOT_ER_AAC_SCAL:
1372     case AOT_ER_CELP:
1373     case AOT_ER_HVXC:
1374     case AOT_ER_BSAC:
1375       self->m_epConfig = FDKreadBits(bs,2);
1376 
1377       if (self->m_epConfig > 1) {
1378         return TRANSPORTDEC_UNSUPPORTED_FORMAT; // EPCONFIG;
1379       }
1380       break;
1381     default:
1382       break;
1383   }
1384 
1385   if (fExplicitBackwardCompatible) {
1386     ErrorStatus = AudioSpecificConfig_ExtensionParse(self, bs, cb);
1387   }
1388 
1389   return (ErrorStatus);
1390 }
1391 
DrmRawSdcAudioConfig_Parse(CSAudioSpecificConfig * self,HANDLE_FDK_BITSTREAM bs)1392 TRANSPORTDEC_ERROR DrmRawSdcAudioConfig_Parse(
1393         CSAudioSpecificConfig *self,
1394         HANDLE_FDK_BITSTREAM   bs
1395         )
1396 {
1397   TRANSPORTDEC_ERROR ErrorStatus = TRANSPORTDEC_OK;
1398 
1399   AudioSpecificConfig_Init(self);
1400 
1401   if ((INT)FDKgetValidBits(bs) < 20) {
1402     ErrorStatus = TRANSPORTDEC_PARSE_ERROR;
1403     goto bail;
1404   }
1405   else {
1406     /* DRM - Audio information data entity - type 9
1407        - Short Id            2 bits
1408        - Stream Id           2 bits
1409        - audio coding        2 bits
1410        - SBR flag            1 bit
1411        - audio mode          2 bits
1412        - audio sampling rate 3 bits
1413        - text flag           1 bit
1414        - enhancement flag    1 bit
1415        - coder field         5 bits
1416        - rfa                 1 bit  */
1417 
1418     int audioCoding, audioMode, cSamplingFreq, coderField, sfIdx, sbrFlag;
1419 
1420     /* Read the SDC field */
1421     FDKreadBits(bs,4);   /* Short and Stream Id */
1422 
1423     audioCoding   = FDKreadBits(bs, 2);
1424     sbrFlag       = FDKreadBits(bs, 1);
1425     audioMode     = FDKreadBits(bs, 2);
1426     cSamplingFreq = FDKreadBits(bs, 3);    /* audio sampling rate */
1427 
1428     FDKreadBits(bs, 2);  /* Text and enhancement flag */
1429     coderField   = FDKreadBits(bs, 5);
1430     FDKreadBits(bs, 1);  /* rfa */
1431 
1432     /* Evaluate configuration and fill the ASC */
1433     switch (cSamplingFreq) {
1434     case 0: /*  8 kHz */
1435       sfIdx = 11;
1436       break;
1437     case 1: /* 12 kHz */
1438       sfIdx = 9;
1439       break;
1440     case 2: /* 16 kHz */
1441       sfIdx = 8;
1442       break;
1443     case 3: /* 24 kHz */
1444       sfIdx = 6;
1445       break;
1446     case 5: /* 48 kHz */
1447       sfIdx = 3;
1448       break;
1449     case 4: /* reserved */
1450     case 6: /* reserved */
1451     case 7: /* reserved */
1452     default:
1453       ErrorStatus = TRANSPORTDEC_PARSE_ERROR;
1454       goto bail;
1455     }
1456 
1457     self->m_samplingFrequencyIndex = sfIdx;
1458     self->m_samplingFrequency = SamplingRateTable[sfIdx];
1459 
1460     if ( sbrFlag ) {
1461       UINT i;
1462       int tmp = -1;
1463       self->m_sbrPresentFlag = 1;
1464       self->m_extensionAudioObjectType = AOT_SBR;
1465       self->m_extensionSamplingFrequency = self->m_samplingFrequency << 1;
1466       for (i=0; i<(sizeof(SamplingRateTable)/sizeof(SamplingRateTable[0])); i++){
1467         if (SamplingRateTable[i] == self->m_extensionSamplingFrequency){
1468           tmp = i;
1469           break;
1470         }
1471       }
1472       self->m_extensionSamplingFrequencyIndex = tmp;
1473     }
1474 
1475     switch (audioCoding) {
1476       case 0: /* AAC */
1477           self->m_aot = AOT_DRM_AAC     ;  /* Set pseudo AOT for Drm AAC */
1478 
1479         switch (audioMode) {
1480         case 1: /* parametric stereo */
1481           self->m_psPresentFlag = 1;
1482         case 0: /* mono */
1483           self->m_channelConfiguration = 1;
1484           break;
1485         case 2: /* stereo */
1486           self->m_channelConfiguration = 2;
1487           break;
1488         default:
1489           ErrorStatus = TRANSPORTDEC_PARSE_ERROR;
1490           goto bail;
1491         }
1492         self->m_vcb11Flag = 1;
1493         self->m_hcrFlag = 1;
1494         self->m_samplesPerFrame = 960;
1495         self->m_epConfig = 1;
1496         break;
1497       case 1: /* CELP */
1498         self->m_aot = AOT_ER_CELP;
1499         self->m_channelConfiguration = 1;
1500         break;
1501       case 2: /* HVXC */
1502         self->m_aot = AOT_ER_HVXC;
1503         self->m_channelConfiguration = 1;
1504         break;
1505       case 3: /* reserved */
1506       default:
1507         ErrorStatus = TRANSPORTDEC_PARSE_ERROR;
1508         self->m_aot = AOT_NONE;
1509         break;
1510     }
1511 
1512     if (self->m_psPresentFlag && !self->m_sbrPresentFlag) {
1513       ErrorStatus = TRANSPORTDEC_PARSE_ERROR;
1514       goto bail;
1515     }
1516   }
1517 
1518 bail:
1519   return (ErrorStatus);
1520 }
1521 
1522