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 /**************************** AAC decoder library ******************************
96 
97    Author(s):   Josef Hoepfl
98 
99    Description:
100 
101 *******************************************************************************/
102 
103 #ifndef AACDECODER_H
104 #define AACDECODER_H
105 
106 #include "common_fix.h"
107 
108 #include "FDK_bitstream.h"
109 
110 #include "channel.h"
111 
112 #include "tpdec_lib.h"
113 #include "FDK_audio.h"
114 
115 #include "block.h"
116 
117 #include "genericStds.h"
118 
119 #include "FDK_qmf_domain.h"
120 
121 #include "sbrdecoder.h"
122 
123 #include "aacdec_drc.h"
124 
125 #include "pcmdmx_lib.h"
126 
127 #include "FDK_drcDecLib.h"
128 
129 #include "limiter.h"
130 
131 #include "FDK_delay.h"
132 
133 #define TIME_DATA_FLUSH_SIZE (128)
134 #define TIME_DATA_FLUSH_SIZE_SF (7)
135 
136 #define AACDEC_MAX_NUM_PREROLL_AU_USAC (3)
137 #if (AACDEC_MAX_NUM_PREROLL_AU < 3)
138 #undef AACDEC_MAX_NUM_PREROLL_AU
139 #define AACDEC_MAX_NUM_PREROLL_AU AACDEC_MAX_NUM_PREROLL_AU_USAC
140 #endif
141 
142 typedef struct AAC_DECODER_INSTANCE *HANDLE_AACDECODER;
143 
144 enum { L = 0, R = 1 };
145 
146 typedef struct {
147   unsigned char *buffer;
148   int bufferSize;
149   int offset[8];
150   int nrElements;
151 } CAncData;
152 
153 typedef enum { NOT_DEFINED = -1, MODE_HQ = 0, MODE_LP = 1 } QMF_MODE;
154 
155 typedef struct {
156   int bsDelay;
157 } SBR_PARAMS;
158 
159 enum {
160   AACDEC_FLUSH_OFF = 0,
161   AACDEC_RSV60_CFG_CHANGE_ATSC_FLUSH_ON = 1,
162   AACDEC_RSV60_DASH_IPF_ATSC_FLUSH_ON = 2,
163   AACDEC_USAC_DASH_IPF_FLUSH_ON = 3
164 };
165 
166 enum {
167   AACDEC_BUILD_UP_OFF = 0,
168   AACDEC_RSV60_BUILD_UP_ON = 1,
169   AACDEC_RSV60_BUILD_UP_ON_IN_BAND = 2,
170   AACDEC_USAC_BUILD_UP_ON = 3,
171   AACDEC_RSV60_BUILD_UP_IDLE = 4,
172   AACDEC_RSV60_BUILD_UP_IDLE_IN_BAND = 5
173 };
174 
175 #define AACDEC_CROSSFADE_BITMASK_OFF                                    \
176   ((UCHAR)0) /*!< No cross-fade between frames shall be applied at next \
177                 config change. */
178 #define AACDEC_CROSSFADE_BITMASK_PREROLL \
179   ((UCHAR)1 << 1) /*!< applyCrossfade is signaled in AudioPreRoll */
180 
181 typedef struct {
182   /* Usac Extension Elements */
183   USAC_EXT_ELEMENT_TYPE usacExtElementType[(3)];
184   UINT usacExtElementDefaultLength[(3)];
185   UCHAR usacExtElementPayloadFrag[(3)];
186 } CUsacCoreExtensions;
187 
188 /* AAC decoder (opaque toward userland) struct declaration */
189 struct AAC_DECODER_INSTANCE {
190   INT aacChannels; /*!< Amount of AAC decoder channels allocated.        */
191   INT ascChannels[(1 *
192                    1)]; /*!< Amount of AAC decoder channels signalled in ASC. */
193   INT blockNumber;      /*!< frame counter                                    */
194 
195   INT nrOfLayers;
196 
197   INT outputInterleaved; /*!< PCM output format (interleaved/none interleaved).
198                           */
199 
200   INT aacOutDataHeadroom; /*!< Headroom of the output time signal to prevent
201                              clipping */
202 
203   HANDLE_TRANSPORTDEC hInput; /*!< Transport layer handle. */
204 
205   SamplingRateInfo
206       samplingRateInfo[(1 * 1)]; /*!< Sampling Rate information table */
207 
208   UCHAR
209   frameOK; /*!< Will be unset if a consistency check, e.g. CRC etc. fails */
210 
211   UINT flags[(1 * 1)]; /*!< Flags for internal decoder use. DO NOT USE
212                           self::streaminfo::flags ! */
213   UINT elFlags[(3 * ((8) * 2) + (((8) * 2)) / 2 + 4 * (1) +
214                 1)]; /*!< Flags for internal decoder use (element specific). DO
215                         NOT USE self::streaminfo::flags ! */
216 
217   MP4_ELEMENT_ID elements[(3 * ((8) * 2) + (((8) * 2)) / 2 + 4 * (1) +
218                            1)]; /*!< Table where the element Id's are listed */
219   UCHAR elTags[(3 * ((8) * 2) + (((8) * 2)) / 2 + 4 * (1) +
220                 1)]; /*!< Table where the elements id Tags are listed      */
221   UCHAR chMapping[((8) * 2)]; /*!< Table of MPEG canonical order to bitstream
222                                  channel order mapping. */
223 
224   AUDIO_CHANNEL_TYPE channelType[(8)]; /*!< Audio channel type of each output
225                                           audio channel (from 0 upto
226                                           numChannels).           */
227   UCHAR channelIndices[(8)]; /*!< Audio channel index for each output audio
228                                 channel (from 0 upto numChannels).         */
229   /* See ISO/IEC 13818-7:2005(E), 8.5.3.2 Explicit channel mapping using a
230    * program_config_element() */
231 
232   FDK_channelMapDescr mapDescr; /*!< Describes the output channel mapping. */
233   UCHAR chMapIndex; /*!< Index to access one line of the channelOutputMapping
234                        table. This is required because not all 8 channel
235                        configurations have the same output mapping. */
236   INT sbrDataLen;   /*!< Expected length of the SBR remaining in bitbuffer after
237                          the AAC payload has been pared.   */
238 
239   CProgramConfig pce;
240   CStreamInfo
241       streamInfo; /*!< Pointer to StreamInfo data (read from the bitstream) */
242   CAacDecoderChannelInfo
243       *pAacDecoderChannelInfo[(8)]; /*!< Temporal channel memory */
244   CAacDecoderStaticChannelInfo
245       *pAacDecoderStaticChannelInfo[(8)]; /*!< Persistent channel memory */
246 
247   FIXP_DBL *workBufferCore1;
248   FIXP_DBL *workBufferCore2;
249   PCM_DEC *pTimeData2;
250   INT timeData2Size;
251 
252   CpePersistentData *cpeStaticData[(
253       3 * ((8) * 2) + (((8) * 2)) / 2 + 4 * (1) +
254       1)]; /*!< Pointer to persistent data shared by both channels of a CPE.
255 This structure is allocated once for each CPE. */
256 
257   CConcealParams concealCommonData;
258   CConcealmentMethod concealMethodUser;
259 
260   CUsacCoreExtensions usacCoreExt; /*!< Data and handles to extend USAC FD/LPD
261                                       core decoder (SBR, MPS, ...) */
262   UINT numUsacElements[(1 * 1)];
263   UCHAR usacStereoConfigIndex[(3 * ((8) * 2) + (((8) * 2)) / 2 + 4 * (1) + 1)];
264   const CSUsacConfig *pUsacConfig[(1 * 1)];
265   INT nbDiv; /*!< number of frame divisions in LPD-domain */
266 
267   UCHAR useLdQmfTimeAlign;
268 
269   INT aacChannelsPrev; /*!< The amount of AAC core channels of the last
270                           successful decode call.         */
271   AUDIO_CHANNEL_TYPE channelTypePrev[(8)]; /*!< Array holding the channelType
272                                               values of the last successful
273                                               decode call.    */
274   UCHAR
275   channelIndicesPrev[(8)]; /*!< Array holding the channelIndices values of
276                               the last successful decode call. */
277 
278   UCHAR
279   downscaleFactor; /*!< Variable to store a supported ELD downscale factor
280                       of 1, 2, 3 or 4 */
281   UCHAR downscaleFactorInBS; /*!< Variable to store the (not necessarily
282                                 supported) ELD downscale factor discovered in
283                                 the bitstream */
284 
285   HANDLE_SBRDECODER hSbrDecoder; /*!< SBR decoder handle. */
286   UCHAR sbrEnabled;     /*!< flag to store if SBR has been detected     */
287   UCHAR sbrEnabledPrev; /*!< flag to store if SBR has been detected from
288                            previous frame */
289   UCHAR psPossible;     /*!< flag to store if PS is possible            */
290   SBR_PARAMS sbrParams; /*!< struct to store all sbr parameters         */
291 
292   UCHAR *pDrmBsBuffer; /*!< Pointer to dynamic buffer which is used to reverse
293                           the bits of the DRM SBR payload */
294   USHORT drmBsBufferSize; /*!< Size of the dynamic buffer which is used to
295                              reverse the bits of the DRM SBR payload */
296   FDK_QMF_DOMAIN
297   qmfDomain; /*!< Instance of module for QMF domain data handling */
298 
299   QMF_MODE qmfModeCurr; /*!< The current QMF mode                       */
300   QMF_MODE qmfModeUser; /*!< The QMF mode requested by the library user */
301 
302   HANDLE_AAC_DRC hDrcInfo; /*!< handle to DRC data structure               */
303   INT metadataExpiry;      /*!< Metadata expiry time in milli-seconds.     */
304 
305   void *pMpegSurroundDecoder; /*!< pointer to mpeg surround decoder structure */
306   UCHAR mpsEnableUser;        /*!< MPS enable user flag                       */
307   UCHAR mpsEnableCurr;        /*!< MPS enable decoder state                   */
308   UCHAR mpsApplicable;        /*!< MPS applicable                             */
309   SCHAR mpsOutputMode; /*!< setting: normal = 0, binaural = 1, stereo = 2, 5.1ch
310                           = 3 */
311   INT mpsOutChannelsLast; /*!< The amount of channels returned by the last
312                              successful MPS decoder call. */
313   INT mpsFrameSizeLast;   /*!< The frame length returned by the last successful
314                              MPS decoder call. */
315 
316   CAncData ancData; /*!< structure to handle ancillary data         */
317 
318   HANDLE_PCM_DOWNMIX hPcmUtils; /*!< privat data for the PCM utils. */
319 
320   TDLimiterPtr hLimiter;   /*!< Handle of time domain limiter.             */
321   UCHAR limiterEnableUser; /*!< The limiter configuration requested by the
322                               library user */
323   UCHAR limiterEnableCurr; /*!< The current limiter configuration.         */
324 
325   FIXP_DBL extGain[1]; /*!< Gain that must be applied to the output signal. */
326   UINT extGainDelay;   /*!< Delay that must be accounted for extGain. */
327 
328   HANDLE_DRC_DECODER hUniDrcDecoder;
329   UCHAR multibandDrcPresent;
330   UCHAR numTimeSlots;
331   UINT loudnessInfoSetPosition[3];
332   SCHAR defaultTargetLoudness;
333 
334   PCM_DEC
335   *pTimeDataFlush[((8) * 2)]; /*!< Pointer to the flushed time data which
336                                  will be used for the crossfade in case of
337                                  an USAC DASH IPF config change */
338 
339   UCHAR flushStatus;     /*!< Indicates flush status: on|off */
340   SCHAR flushCnt;        /*!< Flush frame counter */
341   UCHAR buildUpStatus;   /*!< Indicates build up status: on|off */
342   SCHAR buildUpCnt;      /*!< Build up frame counter */
343   UCHAR hasAudioPreRoll; /*!< Indicates preRoll status: on|off */
344   UINT prerollAULength[AACDEC_MAX_NUM_PREROLL_AU + 1]; /*!< Relative offset of
345                                                           the prerollAU end
346                                                           position to the AU
347                                                           start position in the
348                                                           bitstream */
349   INT accessUnit; /*!< Number of the actual processed preroll accessUnit */
350   UCHAR applyCrossfade; /*!< If any bit is set, cross-fade for seamless stream
351                            switching is applied */
352 
353   FDK_SignalDelay usacResidualDelay; /*!< Delay residual signal to compensate
354                                         for eSBR delay of DMX signal in case of
355                                         stereoConfigIndex==2. */
356 };
357 
358 #define AAC_DEBUG_EXTHLP \
359   "\
360 --- AAC-Core ---\n\
361     0x00010000 Header data\n\
362     0x00020000 CRC data\n\
363     0x00040000 Channel info\n\
364     0x00080000 Section data\n\
365     0x00100000 Scalefactor data\n\
366     0x00200000 Pulse data\n\
367     0x00400000 Tns data\n\
368     0x00800000 Quantized spectrum\n\
369     0x01000000 Requantized spectrum\n\
370     0x02000000 Time output\n\
371     0x04000000 Fatal errors\n\
372     0x08000000 Buffer fullness\n\
373     0x10000000 Average bitrate\n\
374     0x20000000 Synchronization\n\
375     0x40000000 Concealment\n\
376     0x7FFF0000 all AAC-Core-Info\n\
377 "
378 
379 /**
380  * \brief Synchronise QMF mode for all modules using QMF data.
381  * \param self decoder handle
382  */
383 void CAacDecoder_SyncQmfMode(HANDLE_AACDECODER self);
384 
385 /**
386  * \brief Signal a bit stream interruption to the decoder
387  * \param self decoder handle
388  */
389 void CAacDecoder_SignalInterruption(HANDLE_AACDECODER self);
390 
391 /*!
392   \brief Initialize ancillary buffer
393 
394   \ancData Pointer to ancillary data structure
395   \buffer Pointer to (external) anc data buffer
396   \size Size of the buffer pointed on by buffer
397 
398   \return  Error code
399 */
400 AAC_DECODER_ERROR CAacDecoder_AncDataInit(CAncData *ancData,
401                                           unsigned char *buffer, int size);
402 
403 /*!
404   \brief Get one ancillary data element
405 
406   \ancData Pointer to ancillary data structure
407   \index Index of the anc data element to get
408   \ptr Pointer to a buffer receiving a pointer to the requested anc data element
409   \size Pointer to a buffer receiving the length of the requested anc data
410   element
411 
412   \return  Error code
413 */
414 AAC_DECODER_ERROR CAacDecoder_AncDataGet(CAncData *ancData, int index,
415                                          unsigned char **ptr, int *size);
416 
417 /* initialization of aac decoder */
418 LINKSPEC_H HANDLE_AACDECODER CAacDecoder_Open(TRANSPORT_TYPE bsFormat);
419 
420 /* Initialization of channel elements */
421 LINKSPEC_H AAC_DECODER_ERROR CAacDecoder_Init(HANDLE_AACDECODER self,
422                                               const CSAudioSpecificConfig *asc,
423                                               UCHAR configMode,
424                                               UCHAR *configChanged);
425 /*!
426   \brief Decodes one aac frame
427 
428   The function decodes one aac frame. The decoding of coupling channel
429   elements are not supported. The transport layer might signal, that the
430   data of the current frame is invalid, e.g. as a result of a packet
431   loss in streaming mode.
432   The bitstream position of transportDec_GetBitstream(self->hInput) must
433   be exactly the end of the access unit, including all byte alignment bits.
434   For this purpose, the variable auStartAnchor is used.
435 
436   \return  error status
437 */
438 LINKSPEC_H AAC_DECODER_ERROR CAacDecoder_DecodeFrame(
439     HANDLE_AACDECODER self, const UINT flags, PCM_DEC *pTimeData,
440     const INT timeDataSize, const int timeDataChannelOffset);
441 
442 /* Free config dependent AAC memory */
443 LINKSPEC_H AAC_DECODER_ERROR CAacDecoder_FreeMem(HANDLE_AACDECODER self,
444                                                  const int subStreamIndex);
445 
446 /* Prepare crossfade for USAC DASH IPF config change */
447 LINKSPEC_H AAC_DECODER_ERROR CAacDecoder_PrepareCrossFade(
448     const PCM_DEC *pTimeData, PCM_DEC **pTimeDataFlush, const INT numChannels,
449     const INT frameSize, const INT interleaved);
450 
451 /* Apply crossfade for USAC DASH IPF config change */
452 LINKSPEC_H AAC_DECODER_ERROR CAacDecoder_ApplyCrossFade(
453     PCM_DEC *pTimeData, PCM_DEC **pTimeDataFlush, const INT numChannels,
454     const INT frameSize, const INT interleaved);
455 
456 /* Set flush and build up mode */
457 LINKSPEC_H AAC_DECODER_ERROR CAacDecoder_CtrlCFGChange(HANDLE_AACDECODER self,
458                                                        UCHAR flushStatus,
459                                                        SCHAR flushCnt,
460                                                        UCHAR buildUpStatus,
461                                                        SCHAR buildUpCnt);
462 
463 /* Parse preRoll Extension Payload */
464 LINKSPEC_H AAC_DECODER_ERROR CAacDecoder_PreRollExtensionPayloadParse(
465     HANDLE_AACDECODER self, UINT *numPrerollAU, UINT *prerollAUOffset,
466     UINT *prerollAULength);
467 
468 /* Destroy aac decoder */
469 LINKSPEC_H void CAacDecoder_Close(HANDLE_AACDECODER self);
470 
471 /* get streaminfo handle from decoder */
472 LINKSPEC_H CStreamInfo *CAacDecoder_GetStreamInfo(HANDLE_AACDECODER self);
473 
474 #endif /* #ifndef AACDECODER_H */
475