• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* -----------------------------------------------------------------------------
2 Software License for The Fraunhofer FDK AAC Codec Library for Android
3 
4 © Copyright  1995 - 2020 Fraunhofer-Gesellschaft zur Förderung der angewandten
5 Forschung e.V. All rights reserved.
6 
7  1.    INTRODUCTION
8 The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software
9 that implements the MPEG Advanced Audio Coding ("AAC") encoding and decoding
10 scheme for digital audio. This FDK AAC Codec software is intended to be used on
11 a wide variety of Android devices.
12 
13 AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient
14 general perceptual audio codecs. AAC-ELD is considered the best-performing
15 full-bandwidth communications codec by independent studies and is widely
16 deployed. AAC has been standardized by ISO and IEC as part of the MPEG
17 specifications.
18 
19 Patent licenses for necessary patent claims for the FDK AAC Codec (including
20 those of Fraunhofer) may be obtained through Via Licensing
21 (www.vialicensing.com) or through the respective patent owners individually for
22 the purpose of encoding or decoding bit streams in products that are compliant
23 with the ISO/IEC MPEG audio standards. Please note that most manufacturers of
24 Android devices already license these patent claims through Via Licensing or
25 directly from the patent owners, and therefore FDK AAC Codec software may
26 already be covered under those patent licenses when it is used for those
27 licensed purposes only.
28 
29 Commercially-licensed AAC software libraries, including floating-point versions
30 with enhanced sound quality, are also available from Fraunhofer. Users are
31 encouraged to check the Fraunhofer website for additional applications
32 information and documentation.
33 
34 2.    COPYRIGHT LICENSE
35 
36 Redistribution and use in source and binary forms, with or without modification,
37 are permitted without payment of copyright license fees provided that you
38 satisfy the following conditions:
39 
40 You must retain the complete text of this software license in redistributions of
41 the FDK AAC Codec or your modifications thereto in source code form.
42 
43 You must retain the complete text of this software license in the documentation
44 and/or other materials provided with redistributions of the FDK AAC Codec or
45 your modifications thereto in binary form. You must make available free of
46 charge copies of the complete source code of the FDK AAC Codec and your
47 modifications thereto to recipients of copies in binary form.
48 
49 The name of Fraunhofer may not be used to endorse or promote products derived
50 from this library without prior written permission.
51 
52 You may not charge copyright license fees for anyone to use, copy or distribute
53 the FDK AAC Codec software or your modifications thereto.
54 
55 Your modified versions of the FDK AAC Codec must carry prominent notices stating
56 that you changed the software and the date of any change. For modified versions
57 of the FDK AAC Codec, the term "Fraunhofer FDK AAC Codec Library for Android"
58 must be replaced by the term "Third-Party Modified Version of the Fraunhofer FDK
59 AAC Codec Library for Android."
60 
61 3.    NO PATENT LICENSE
62 
63 NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without
64 limitation the patents of Fraunhofer, ARE GRANTED BY THIS SOFTWARE LICENSE.
65 Fraunhofer provides no warranty of patent non-infringement with respect to this
66 software.
67 
68 You may use this FDK AAC Codec software or modifications thereto only for
69 purposes that are authorized by appropriate patent licenses.
70 
71 4.    DISCLAIMER
72 
73 This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright
74 holders and contributors "AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES,
75 including but not limited to the implied warranties of merchantability and
76 fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
77 CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary,
78 or consequential damages, including but not limited to procurement of substitute
79 goods or services; loss of use, data, or profits, or business interruption,
80 however caused and on any theory of liability, whether in contract, strict
81 liability, or tort (including negligence), arising in any way out of the use of
82 this software, even if advised of the possibility of such damage.
83 
84 5.    CONTACT INFORMATION
85 
86 Fraunhofer Institute for Integrated Circuits IIS
87 Attention: Audio and Multimedia Departments - FDK AAC LL
88 Am Wolfsmantel 33
89 91058 Erlangen, Germany
90 
91 www.iis.fraunhofer.de/amm
92 amm-info@iis.fraunhofer.de
93 ----------------------------------------------------------------------------- */
94 
95 /******************* MPEG transport format decoder library *********************
96 
97    Author(s):   Josef Hoepfl
98 
99    Description: ADTS interface
100 
101 *******************************************************************************/
102 
103 #include "tpdec_adts.h"
104 
105 #include "FDK_bitstream.h"
106 
adtsRead_CrcInit(HANDLE_ADTS pAdts)107 void adtsRead_CrcInit(
108     HANDLE_ADTS pAdts) /*!< pointer to adts crc info stucture */
109 {
110   FDKcrcInit(&pAdts->crcInfo, 0x8005, 0xFFFF, 16);
111 }
112 
adtsRead_CrcStartReg(HANDLE_ADTS pAdts,HANDLE_FDK_BITSTREAM hBs,int mBits)113 int adtsRead_CrcStartReg(
114     HANDLE_ADTS pAdts,        /*!< pointer to adts stucture */
115     HANDLE_FDK_BITSTREAM hBs, /*!< handle to current bit buffer structure */
116     int mBits                 /*!< number of bits in crc region */
117 ) {
118   if (pAdts->bs.protection_absent) {
119     return 0;
120   }
121 
122   return (FDKcrcStartReg(&pAdts->crcInfo, hBs, mBits));
123 }
124 
adtsRead_CrcEndReg(HANDLE_ADTS pAdts,HANDLE_FDK_BITSTREAM hBs,int reg)125 void adtsRead_CrcEndReg(
126     HANDLE_ADTS pAdts,        /*!< pointer to adts crc info stucture */
127     HANDLE_FDK_BITSTREAM hBs, /*!< handle to current bit buffer structure */
128     int reg                   /*!< crc region */
129 ) {
130   if (pAdts->bs.protection_absent == 0) {
131     FDKcrcEndReg(&pAdts->crcInfo, hBs, reg);
132   }
133 }
134 
adtsRead_CrcCheck(HANDLE_ADTS pAdts)135 TRANSPORTDEC_ERROR adtsRead_CrcCheck(HANDLE_ADTS pAdts) {
136   TRANSPORTDEC_ERROR ErrorStatus = TRANSPORTDEC_OK;
137   USHORT crc;
138 
139   if (pAdts->bs.protection_absent) return TRANSPORTDEC_OK;
140 
141   crc = FDKcrcGetCRC(&pAdts->crcInfo);
142   if (crc != pAdts->crcReadValue) {
143     return (TRANSPORTDEC_CRC_ERROR);
144   }
145 
146   return (ErrorStatus);
147 }
148 
149 #define Adts_Length_SyncWord 12
150 #define Adts_Length_Id 1
151 #define Adts_Length_Layer 2
152 #define Adts_Length_ProtectionAbsent 1
153 #define Adts_Length_Profile 2
154 #define Adts_Length_SamplingFrequencyIndex 4
155 #define Adts_Length_PrivateBit 1
156 #define Adts_Length_ChannelConfiguration 3
157 #define Adts_Length_OriginalCopy 1
158 #define Adts_Length_Home 1
159 #define Adts_Length_CopyrightIdentificationBit 1
160 #define Adts_Length_CopyrightIdentificationStart 1
161 #define Adts_Length_FrameLength 13
162 #define Adts_Length_BufferFullness 11
163 #define Adts_Length_NumberOfRawDataBlocksInFrame 2
164 #define Adts_Length_CrcCheck 16
165 
adtsRead_DecodeHeader(HANDLE_ADTS pAdts,CSAudioSpecificConfig * pAsc,HANDLE_FDK_BITSTREAM hBs,const INT ignoreBufferFullness)166 TRANSPORTDEC_ERROR adtsRead_DecodeHeader(HANDLE_ADTS pAdts,
167                                          CSAudioSpecificConfig *pAsc,
168                                          HANDLE_FDK_BITSTREAM hBs,
169                                          const INT ignoreBufferFullness) {
170   INT crcReg;
171 
172   INT valBits;
173   INT cmp_buffer_fullness;
174   int i, adtsHeaderLength;
175 
176   STRUCT_ADTS_BS bs;
177 
178   CProgramConfig oldPce;
179   /* Store the old PCE temporarily. Maybe we'll need it later if we
180      have channelConfig=0 and no PCE in this frame. */
181   FDKmemcpy(&oldPce, &pAsc->m_progrConfigElement, sizeof(CProgramConfig));
182 
183   valBits = FDKgetValidBits(hBs) + ADTS_SYNCLENGTH;
184 
185   if (valBits < ADTS_HEADERLENGTH) {
186     return TRANSPORTDEC_NOT_ENOUGH_BITS;
187   }
188 
189   /* adts_fixed_header */
190   bs.mpeg_id = FDKreadBits(hBs, Adts_Length_Id);
191   bs.layer = FDKreadBits(hBs, Adts_Length_Layer);
192   bs.protection_absent = FDKreadBits(hBs, Adts_Length_ProtectionAbsent);
193   bs.profile = FDKreadBits(hBs, Adts_Length_Profile);
194   bs.sample_freq_index = FDKreadBits(hBs, Adts_Length_SamplingFrequencyIndex);
195   bs.private_bit = FDKreadBits(hBs, Adts_Length_PrivateBit);
196   bs.channel_config = FDKreadBits(hBs, Adts_Length_ChannelConfiguration);
197   bs.original = FDKreadBits(hBs, Adts_Length_OriginalCopy);
198   bs.home = FDKreadBits(hBs, Adts_Length_Home);
199 
200   /* adts_variable_header */
201   bs.copyright_id = FDKreadBits(hBs, Adts_Length_CopyrightIdentificationBit);
202   bs.copyright_start =
203       FDKreadBits(hBs, Adts_Length_CopyrightIdentificationStart);
204   bs.frame_length = FDKreadBits(hBs, Adts_Length_FrameLength);
205   bs.adts_fullness = FDKreadBits(hBs, Adts_Length_BufferFullness);
206   bs.num_raw_blocks =
207       FDKreadBits(hBs, Adts_Length_NumberOfRawDataBlocksInFrame);
208   bs.num_pce_bits = 0;
209 
210   adtsHeaderLength = ADTS_HEADERLENGTH;
211 
212   if (valBits < bs.frame_length * 8) {
213     goto bail;
214   }
215 
216   FDKcrcReset(&pAdts->crcInfo);
217   if (!bs.protection_absent) {
218     FDKpushBack(hBs, 56); /* complete fixed and variable header! */
219     crcReg = FDKcrcStartReg(&pAdts->crcInfo, hBs, 0);
220     FDKpushFor(hBs, 56);
221   }
222 
223   if (!bs.protection_absent && bs.num_raw_blocks > 0) {
224     if ((INT)FDKgetValidBits(hBs) < bs.num_raw_blocks * 16) {
225       goto bail;
226     }
227     for (i = 0; i < bs.num_raw_blocks; i++) {
228       pAdts->rawDataBlockDist[i] = (USHORT)FDKreadBits(hBs, 16);
229       adtsHeaderLength += 16;
230     }
231     /* Change raw data blocks to delta values */
232     pAdts->rawDataBlockDist[bs.num_raw_blocks] =
233         bs.frame_length - 7 - bs.num_raw_blocks * 2 - 2;
234     for (i = bs.num_raw_blocks; i > 0; i--) {
235       pAdts->rawDataBlockDist[i] -= pAdts->rawDataBlockDist[i - 1];
236     }
237   }
238 
239   /* adts_error_check */
240   if (!bs.protection_absent) {
241     USHORT crc_check;
242 
243     FDKcrcEndReg(&pAdts->crcInfo, hBs, crcReg);
244 
245     if ((INT)FDKgetValidBits(hBs) < Adts_Length_CrcCheck) {
246       goto bail;
247     }
248 
249     crc_check = FDKreadBits(hBs, Adts_Length_CrcCheck);
250     adtsHeaderLength += Adts_Length_CrcCheck;
251 
252     pAdts->crcReadValue = crc_check;
253     /* Check header CRC in case of multiple raw data blocks */
254     if (bs.num_raw_blocks > 0) {
255       if (pAdts->crcReadValue != FDKcrcGetCRC(&pAdts->crcInfo)) {
256         return TRANSPORTDEC_CRC_ERROR;
257       }
258       /* Reset CRC for the upcoming raw_data_block() */
259       FDKcrcReset(&pAdts->crcInfo);
260     }
261   }
262 
263   /* check if valid header */
264   if ((bs.layer != 0) ||            // we only support MPEG ADTS
265       (bs.sample_freq_index >= 13)  // we only support 96kHz - 7350kHz
266   ) {
267     FDKpushFor(hBs, bs.frame_length * 8);  // try again one frame later
268     return TRANSPORTDEC_UNSUPPORTED_FORMAT;
269   }
270 
271   /* special treatment of id-bit */
272   if ((bs.mpeg_id == 0) && (pAdts->decoderCanDoMpeg4 == 0)) {
273     /* MPEG-2 decoder cannot play MPEG-4 bitstreams */
274 
275     FDKpushFor(hBs, bs.frame_length * 8);  // try again one frame later
276     return TRANSPORTDEC_UNSUPPORTED_FORMAT;
277   }
278 
279   if (!ignoreBufferFullness) {
280     cmp_buffer_fullness =
281         bs.frame_length * 8 +
282         bs.adts_fullness * 32 * getNumberOfEffectiveChannels(bs.channel_config);
283 
284     /* Evaluate buffer fullness */
285     if (bs.adts_fullness != 0x7FF) {
286       if (pAdts->BufferFullnesStartFlag) {
287         if (valBits < cmp_buffer_fullness) {
288           /* Condition for start of decoding is not fulfilled */
289 
290           /* The current frame will not be decoded */
291           FDKpushBack(hBs, adtsHeaderLength);
292 
293           if ((cmp_buffer_fullness + adtsHeaderLength) >
294               (((8192 * 4) << 3) - 7)) {
295             return TRANSPORTDEC_SYNC_ERROR;
296           } else {
297             return TRANSPORTDEC_NOT_ENOUGH_BITS;
298           }
299         } else {
300           pAdts->BufferFullnesStartFlag = 0;
301         }
302       }
303     }
304   }
305 
306   /* Get info from ADTS header */
307   AudioSpecificConfig_Init(pAsc);
308   pAsc->m_aot = (AUDIO_OBJECT_TYPE)(bs.profile + 1);
309   pAsc->m_samplingFrequencyIndex = bs.sample_freq_index;
310   pAsc->m_samplingFrequency = SamplingRateTable[bs.sample_freq_index];
311   pAsc->m_channelConfiguration = bs.channel_config;
312   pAsc->m_samplesPerFrame = 1024;
313 
314   if (bs.channel_config == 0) {
315     int pceBits = 0;
316     UINT alignAnchor = FDKgetValidBits(hBs);
317     CProgramConfig tmpPce;
318 
319     if (FDKreadBits(hBs, 3) == ID_PCE) {
320       /* Got luck! Parse the PCE */
321       crcReg = adtsRead_CrcStartReg(pAdts, hBs, 0);
322 
323       CProgramConfig_Init(&tmpPce);
324       CProgramConfig_Read(&tmpPce, hBs, alignAnchor);
325 
326       if (CProgramConfig_IsValid(&tmpPce)) {
327         if (CProgramConfig_IsValid(&oldPce)) {
328           /* Compare the new and the old PCE (tags ignored) */
329           switch (CProgramConfig_Compare(&tmpPce, &oldPce)) {
330             case 0: /* Nothing to do because PCE matches the old one exactly. */
331             case 1: /* Channel configuration not changed. Just new metadata. */
332               FDKmemcpy(&pAsc->m_progrConfigElement, &tmpPce,
333                         sizeof(CProgramConfig));
334               break;
335             case 2:  /* The number of channels are identical but not the config
336                       */
337             case -1: /* The channel configuration is completely different */
338             default:
339               FDKmemcpy(&pAsc->m_progrConfigElement, &oldPce,
340                         sizeof(CProgramConfig));
341               FDKpushBack(hBs, adtsHeaderLength);
342               return TRANSPORTDEC_PARSE_ERROR;
343           }
344         } else {
345           FDKmemcpy(&pAsc->m_progrConfigElement, &tmpPce,
346                     sizeof(CProgramConfig));
347         }
348       } else {
349         if (CProgramConfig_IsValid(&oldPce)) {
350           FDKmemcpy(&pAsc->m_progrConfigElement, &oldPce,
351                     sizeof(CProgramConfig));
352         } else {
353           FDKpushBack(hBs, adtsHeaderLength);
354           return TRANSPORTDEC_PARSE_ERROR;
355         }
356       }
357 
358       adtsRead_CrcEndReg(pAdts, hBs, crcReg);
359       pceBits = (INT)alignAnchor - (INT)FDKgetValidBits(hBs);
360       adtsHeaderLength += pceBits;
361 
362       if (pceBits > (INT)alignAnchor) {
363         goto bail;
364       }
365 
366       /* store the number of PCE bits */
367       bs.num_pce_bits = pceBits;
368     } else {
369       /* No PCE in this frame! Push back the ID tag bits. */
370       FDKpushBack(hBs, 3);
371 
372       /* Encoders do not have to write a PCE in each frame.
373          So if we already have a valid PCE we have to use it. */
374       if (oldPce.isValid &&
375           (bs.sample_freq_index ==
376            pAdts->bs.sample_freq_index) /* we could compare the complete fixed
377                                            header (bytes) here! */
378           && (bs.channel_config == pAdts->bs.channel_config) /* == 0 */
379           &&
380           (bs.mpeg_id ==
381            pAdts->bs.mpeg_id)) { /* Restore previous PCE which is still valid */
382         FDKmemcpy(&pAsc->m_progrConfigElement, &oldPce, sizeof(CProgramConfig));
383       } else if (bs.mpeg_id == 0) {
384         /* If not it seems that we have a implicit channel configuration.
385            This mode is not allowed in the context of ISO/IEC 14496-3.
386            Skip this frame and try the next one. */
387         FDKpushFor(hBs, (bs.frame_length << 3) - adtsHeaderLength - 3);
388         return TRANSPORTDEC_UNSUPPORTED_FORMAT;
389       }
390       /* else {
391          ISO/IEC 13818-7 implicit channel mapping is allowed.
392          So just open the box of chocolates to see what we got.
393       } */
394     }
395   }
396 
397   /* Copy bit stream data struct to persistent memory now, once we passed all
398    * sanity checks above. */
399   FDKmemcpy(&pAdts->bs, &bs, sizeof(STRUCT_ADTS_BS));
400 
401   return TRANSPORTDEC_OK;
402 
403 bail:
404   FDKpushBack(hBs, adtsHeaderLength);
405   return TRANSPORTDEC_NOT_ENOUGH_BITS;
406 }
407 
adtsRead_GetRawDataBlockLength(HANDLE_ADTS pAdts,INT blockNum)408 int adtsRead_GetRawDataBlockLength(HANDLE_ADTS pAdts, INT blockNum) {
409   int length;
410 
411   if (pAdts->bs.num_raw_blocks == 0) {
412     length =
413         (pAdts->bs.frame_length - 7)
414         << 3; /* aac_frame_length subtracted by the header size (7 bytes). */
415     if (pAdts->bs.protection_absent == 0)
416       length -= 16; /* substract 16 bit CRC */
417   } else {
418     if (pAdts->bs.protection_absent) {
419       length = -1; /* raw data block length is unknown */
420     } else {
421       if (blockNum < 0 || blockNum > 3) {
422         length = -1;
423       } else {
424         length = (pAdts->rawDataBlockDist[blockNum] << 3) - 16;
425       }
426     }
427   }
428   if (blockNum == 0 && length > 0) {
429     length -= pAdts->bs.num_pce_bits;
430   }
431   return length;
432 }
433