• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_latm.h"
92 
93 
94 #include "FDK_bitstream.h"
95 
96 
97 #define TPDEC_TRACKINDEX(p,l) (2*(p) + (l))
98 
99 static
CLatmDemux_GetValue(HANDLE_FDK_BITSTREAM bs)100 UINT CLatmDemux_GetValue(HANDLE_FDK_BITSTREAM bs)
101 {
102   UCHAR bytesForValue = 0, tmp = 0;
103   int value = 0;
104 
105   bytesForValue = (UCHAR) FDKreadBits(bs,2);
106 
107   for (UINT i=0; i<=bytesForValue; i++) {
108     value <<= 8;
109     tmp = (UCHAR) FDKreadBits(bs,8);
110     value += tmp;
111   }
112 
113   return value;
114 }
115 
116 
117 static
CLatmDemux_ReadAudioMuxElement(HANDLE_FDK_BITSTREAM bs,CLatmDemux * pLatmDemux,int m_muxConfigPresent,CSTpCallBacks * pTpDecCallbacks,CSAudioSpecificConfig * pAsc,int * pfConfigFound)118 TRANSPORTDEC_ERROR CLatmDemux_ReadAudioMuxElement(
119         HANDLE_FDK_BITSTREAM bs,
120         CLatmDemux *pLatmDemux,
121         int m_muxConfigPresent,
122         CSTpCallBacks *pTpDecCallbacks,
123         CSAudioSpecificConfig *pAsc,
124         int *pfConfigFound
125         )
126 {
127   TRANSPORTDEC_ERROR ErrorStatus = TRANSPORTDEC_OK;
128 
129   if (m_muxConfigPresent) {
130     pLatmDemux->m_useSameStreamMux = FDKreadBits(bs,1);
131 
132     if (!pLatmDemux->m_useSameStreamMux) {
133       if ((ErrorStatus = CLatmDemux_ReadStreamMuxConfig(bs, pLatmDemux, pTpDecCallbacks, pAsc, pfConfigFound))) {
134         return (ErrorStatus);
135       }
136     }
137   }
138 
139   /* If there was no configuration read, its not possible to parse PayloadLengthInfo below. */
140   if (! *pfConfigFound) {
141     return TRANSPORTDEC_SYNC_ERROR;
142   }
143 
144   if (pLatmDemux->m_AudioMuxVersionA == 0) {
145     /* Do only once per call, because parsing and decoding is done in-line. */
146     if ((ErrorStatus = CLatmDemux_ReadPayloadLengthInfo(bs,pLatmDemux))) {
147       return (ErrorStatus);
148     }
149   } else {
150     /* audioMuxVersionA > 0 is reserved for future extensions */
151     ErrorStatus = TRANSPORTDEC_UNSUPPORTED_FORMAT;
152   }
153 
154   return (ErrorStatus);
155 }
156 
CLatmDemux_Read(HANDLE_FDK_BITSTREAM bs,CLatmDemux * pLatmDemux,TRANSPORT_TYPE tt,CSTpCallBacks * pTpDecCallbacks,CSAudioSpecificConfig * pAsc,int * pfConfigFound,const INT ignoreBufferFullness)157 TRANSPORTDEC_ERROR CLatmDemux_Read(
158         HANDLE_FDK_BITSTREAM bs,
159         CLatmDemux *pLatmDemux,
160         TRANSPORT_TYPE tt,
161         CSTpCallBacks *pTpDecCallbacks,
162         CSAudioSpecificConfig *pAsc,
163         int *pfConfigFound,
164         const INT ignoreBufferFullness
165         )
166 {
167   UINT cntBits;
168   UINT cmpBufferFullness;
169   UINT audioMuxLengthBytesLast = 0;
170   TRANSPORTDEC_ERROR ErrorStatus = TRANSPORTDEC_OK;
171 
172   cntBits = FDKgetValidBits(bs);
173 
174   if ((INT)cntBits < MIN_LATM_HEADERLENGTH) {
175     return TRANSPORTDEC_NOT_ENOUGH_BITS;
176   }
177 
178   if ((ErrorStatus = CLatmDemux_ReadAudioMuxElement(bs, pLatmDemux, (tt != TT_MP4_LATM_MCP0), pTpDecCallbacks, pAsc, pfConfigFound)))
179     return (ErrorStatus);
180 
181   if (!ignoreBufferFullness)
182   {
183     cmpBufferFullness =   24+audioMuxLengthBytesLast*8
184                         + pLatmDemux->m_linfo[0][0].m_bufferFullness* pAsc[TPDEC_TRACKINDEX(0,0)].m_channelConfiguration*32;
185 
186     /* evaluate buffer fullness */
187 
188     if (pLatmDemux->m_linfo[0][0].m_bufferFullness != 0xFF)
189     {
190       if (!pLatmDemux->BufferFullnessAchieved)
191       {
192         if (cntBits < cmpBufferFullness)
193         {
194           /* condition for start of decoding is not fulfilled */
195 
196           /* the current frame will not be decoded */
197           return TRANSPORTDEC_NOT_ENOUGH_BITS;
198         }
199         else
200         {
201           pLatmDemux->BufferFullnessAchieved = 1;
202         }
203       }
204     }
205   }
206 
207   return (ErrorStatus);
208 }
209 
210 
CLatmDemux_ReadStreamMuxConfig(HANDLE_FDK_BITSTREAM bs,CLatmDemux * pLatmDemux,CSTpCallBacks * pTpDecCallbacks,CSAudioSpecificConfig * pAsc,int * pfConfigFound)211 TRANSPORTDEC_ERROR CLatmDemux_ReadStreamMuxConfig(
212         HANDLE_FDK_BITSTREAM bs,
213         CLatmDemux *pLatmDemux,
214         CSTpCallBacks *pTpDecCallbacks,
215         CSAudioSpecificConfig *pAsc,
216         int * pfConfigFound
217         )
218 {
219   LATM_LAYER_INFO *p_linfo = NULL;
220   TRANSPORTDEC_ERROR ErrorStatus = TRANSPORTDEC_OK;
221 
222   pLatmDemux->m_AudioMuxVersion = FDKreadBits(bs,1);
223 
224   if (pLatmDemux->m_AudioMuxVersion == 0) {
225     pLatmDemux->m_AudioMuxVersionA = 0;
226   } else {
227     pLatmDemux->m_AudioMuxVersionA = FDKreadBits(bs,1);
228   }
229 
230   if (pLatmDemux->m_AudioMuxVersionA == 0) {
231     if (pLatmDemux->m_AudioMuxVersion == 1) {
232       pLatmDemux->m_taraBufferFullness = CLatmDemux_GetValue(bs);
233     }
234     pLatmDemux->m_allStreamsSameTimeFraming = FDKreadBits(bs,1);
235     pLatmDemux->m_noSubFrames = FDKreadBits(bs,6) + 1;
236     pLatmDemux->m_numProgram  = FDKreadBits(bs,4) + 1;
237 
238     if (pLatmDemux->m_numProgram > 1) {
239       return TRANSPORTDEC_UNSUPPORTED_FORMAT;
240     }
241 
242     int idCnt = 0;
243     for (UINT prog = 0; prog < pLatmDemux->m_numProgram; prog++) {
244       pLatmDemux->m_numLayer = FDKreadBits(bs,3) + 1;
245       if (pLatmDemux->m_numLayer > 2) {
246         return TRANSPORTDEC_UNSUPPORTED_FORMAT;
247       }
248 
249       for (UINT lay = 0; lay < pLatmDemux->m_numLayer; lay++) {
250         p_linfo = &pLatmDemux->m_linfo[prog][lay];
251 
252         p_linfo->m_streamID = idCnt++;
253         p_linfo->m_frameLengthInBits = 0;
254 
255         if( (prog == 0) && (lay == 0) ) {
256           pLatmDemux->m_useSameConfig = 0;
257         } else {
258           pLatmDemux->m_useSameConfig = FDKreadBits(bs,1);
259         }
260 
261         if (pLatmDemux->m_useSameConfig) {
262           if (lay > 1) {
263             FDKmemcpy(&pAsc[TPDEC_TRACKINDEX(prog,lay)], &pAsc[TPDEC_TRACKINDEX(prog,lay-1)], sizeof(CSAudioSpecificConfig));
264           } else {
265             return TRANSPORTDEC_PARSE_ERROR;
266           }
267         } else {
268           if (pLatmDemux->m_AudioMuxVersion == 1)
269           {
270             FDK_BITSTREAM tmpBs;
271             UINT ascStartPos, ascLen=0;
272 
273             ascLen = CLatmDemux_GetValue(bs);
274             ascStartPos = FDKgetValidBits(bs);
275             tmpBs = *bs;
276             FDKsyncCache(&tmpBs);
277             tmpBs.hBitBuf.ValidBits = ascLen;
278 
279             /* Read ASC */
280             if ((ErrorStatus = AudioSpecificConfig_Parse(&pAsc[TPDEC_TRACKINDEX(prog,lay)], &tmpBs, 1, pTpDecCallbacks))) {
281               return (ErrorStatus);
282             }
283             *pfConfigFound = 1;
284 
285             /* The field p_linfo->m_ascLen could be wrong, so check if */
286             if ( 0 > (INT)FDKgetValidBits(&tmpBs)) {
287               return TRANSPORTDEC_PARSE_ERROR;
288             }
289             FDKpushFor(bs, ascLen); /* position bitstream after ASC */
290           }
291           else {
292             /* Read ASC */
293             if ((ErrorStatus = AudioSpecificConfig_Parse(&pAsc[TPDEC_TRACKINDEX(prog,lay)], bs, 0, pTpDecCallbacks))) {
294               return (ErrorStatus);
295             }
296           }
297           {
298             int cbError;
299 
300             cbError = pTpDecCallbacks->cbUpdateConfig(pTpDecCallbacks->cbUpdateConfigData, &pAsc[TPDEC_TRACKINDEX(prog,lay)]);
301             if (cbError != 0) {
302               return TRANSPORTDEC_UNKOWN_ERROR;
303             }
304             *pfConfigFound = 1;
305           }
306         }
307 
308         p_linfo->m_frameLengthType = FDKreadBits(bs,3);
309         switch( p_linfo->m_frameLengthType ) {
310         case 0:
311           p_linfo->m_bufferFullness = FDKreadBits(bs,8);
312 
313           if (!pLatmDemux->m_allStreamsSameTimeFraming) {
314             if ((lay > 0) && (pAsc[TPDEC_TRACKINDEX(prog,lay)].m_aot == AOT_AAC_SCAL || pAsc[TPDEC_TRACKINDEX(prog,lay)].m_aot == AOT_ER_AAC_SCAL)) {
315               return TRANSPORTDEC_UNSUPPORTED_FORMAT;
316             }
317           }
318           break;
319         case 1:
320           /* frameLength = FDKreadBits(bs,9); */
321         case 3:
322         case 4:
323         case 5:
324           /* CELP */
325         case 6:
326         case 7:
327           /* HVXC */
328         default:
329           return TRANSPORTDEC_PARSE_ERROR; //_LATM_INVALIDFRAMELENGTHTYPE;
330 
331         }  /* switch framelengthtype*/
332 
333       }  /* layer loop */
334     }  /* prog loop */
335 
336     pLatmDemux->m_otherDataPresent = FDKreadBits(bs,1);
337     pLatmDemux->m_otherDataLength  = 0;
338 
339     if (pLatmDemux->m_otherDataPresent) {
340       int otherDataLenEsc  = 0;
341       do {
342         pLatmDemux->m_otherDataLength <<= 8;   // *= 256
343         otherDataLenEsc = FDKreadBits(bs,1);
344         pLatmDemux->m_otherDataLength += FDKreadBits(bs,8);
345       } while (otherDataLenEsc);
346     }
347 
348     pLatmDemux->m_crcCheckPresent = FDKreadBits(bs,1);
349     pLatmDemux->m_crcCheckSum     = 0;
350 
351     if (pLatmDemux->m_crcCheckPresent) {
352       pLatmDemux->m_crcCheckSum = FDKreadBits(bs,8);
353     }
354 
355   }
356   else {
357     /* audioMuxVersionA > 0 is reserved for future extensions */
358     ErrorStatus = TRANSPORTDEC_UNSUPPORTED_FORMAT;
359   }
360   return (ErrorStatus);
361 }
362 
CLatmDemux_ReadPayloadLengthInfo(HANDLE_FDK_BITSTREAM bs,CLatmDemux * pLatmDemux)363 TRANSPORTDEC_ERROR CLatmDemux_ReadPayloadLengthInfo(HANDLE_FDK_BITSTREAM bs, CLatmDemux *pLatmDemux)
364 {
365   TRANSPORTDEC_ERROR ErrorStatus = TRANSPORTDEC_OK;
366   int totalPayloadBits = 0;
367 
368   if( pLatmDemux->m_allStreamsSameTimeFraming == 1 ) {
369     for (UINT prog=0; prog<pLatmDemux->m_numProgram; prog++ ) {
370       for (UINT lay=0; lay<pLatmDemux->m_numLayer; lay++ ) {
371         LATM_LAYER_INFO *p_linfo = &pLatmDemux->m_linfo[prog][lay];
372 
373         switch (p_linfo->m_frameLengthType ) {
374         case 0:
375           p_linfo->m_frameLengthInBits = CLatmDemux_ReadAuChunkLengthInfo(bs);
376           totalPayloadBits += p_linfo->m_frameLengthInBits;
377           break;
378         case 3:
379         case 5:
380         case 7:
381         default:
382           return TRANSPORTDEC_PARSE_ERROR; //AAC_DEC_LATM_INVALIDFRAMELENGTHTYPE;
383         }
384       }
385     }
386   }
387   else {
388     ErrorStatus = TRANSPORTDEC_PARSE_ERROR; //AAC_DEC_LATM_TIMEFRAMING;
389   }
390   if (pLatmDemux->m_audioMuxLengthBytes > (UINT)0 && totalPayloadBits > (int)pLatmDemux->m_audioMuxLengthBytes*8) {
391     return TRANSPORTDEC_PARSE_ERROR;
392   }
393   return (ErrorStatus);
394 }
395 
CLatmDemux_ReadAuChunkLengthInfo(HANDLE_FDK_BITSTREAM bs)396 int CLatmDemux_ReadAuChunkLengthInfo(HANDLE_FDK_BITSTREAM bs)
397 {
398   UCHAR endFlag;
399   int len = 0;
400 
401   do {
402     UCHAR tmp = (UCHAR) FDKreadBits(bs,8);
403     endFlag = (tmp < 255);
404 
405     len += tmp;
406 
407   } while( endFlag == 0 );
408 
409   len <<= 3;  /* convert from bytes to bits */
410 
411   return len;
412 }
413 
CLatmDemux_GetFrameLengthInBits(CLatmDemux * pLatmDemux)414 int CLatmDemux_GetFrameLengthInBits(CLatmDemux *pLatmDemux)
415 {
416   return pLatmDemux->m_linfo[0][0].m_frameLengthInBits;
417 }
418 
CLatmDemux_GetOtherDataPresentFlag(CLatmDemux * pLatmDemux)419 int CLatmDemux_GetOtherDataPresentFlag(CLatmDemux *pLatmDemux)
420 {
421   return pLatmDemux->m_otherDataPresent ? 1 : 0;
422 }
423 
CLatmDemux_GetOtherDataLength(CLatmDemux * pLatmDemux)424 int CLatmDemux_GetOtherDataLength(CLatmDemux *pLatmDemux)
425 {
426   return pLatmDemux->m_otherDataLength;
427 }
428 
CLatmDemux_GetNrOfSubFrames(CLatmDemux * pLatmDemux)429 UINT CLatmDemux_GetNrOfSubFrames(CLatmDemux *pLatmDemux)
430 {
431   return pLatmDemux->m_noSubFrames;
432 }
433 
434