1
2/* -----------------------------------------------------------------------------------------------------------
3Software License for The Fraunhofer FDK AAC Codec Library for Android
4
5� Copyright  1995 - 2012 Fraunhofer-Gesellschaft zur F�rderung der angewandten Forschung e.V.
6  All rights reserved.
7
8 1.    INTRODUCTION
9The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software that implements
10the MPEG Advanced Audio Coding ("AAC") encoding and decoding scheme for digital audio.
11This FDK AAC Codec software is intended to be used on a wide variety of Android devices.
12
13AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient general perceptual
14audio codecs. AAC-ELD is considered the best-performing full-bandwidth communications codec by
15independent studies and is widely deployed. AAC has been standardized by ISO and IEC as part
16of the MPEG specifications.
17
18Patent licenses for necessary patent claims for the FDK AAC Codec (including those of Fraunhofer)
19may be obtained through Via Licensing (www.vialicensing.com) or through the respective patent owners
20individually for the purpose of encoding or decoding bit streams in products that are compliant with
21the ISO/IEC MPEG audio standards. Please note that most manufacturers of Android devices already license
22these patent claims through Via Licensing or directly from the patent owners, and therefore FDK AAC Codec
23software may already be covered under those patent licenses when it is used for those licensed purposes only.
24
25Commercially-licensed AAC software libraries, including floating-point versions with enhanced sound quality,
26are also available from Fraunhofer. Users are encouraged to check the Fraunhofer website for additional
27applications information and documentation.
28
292.    COPYRIGHT LICENSE
30
31Redistribution and use in source and binary forms, with or without modification, are permitted without
32payment of copyright license fees provided that you satisfy the following conditions:
33
34You must retain the complete text of this software license in redistributions of the FDK AAC Codec or
35your modifications thereto in source code form.
36
37You must retain the complete text of this software license in the documentation and/or other materials
38provided with redistributions of the FDK AAC Codec or your modifications thereto in binary form.
39You must make available free of charge copies of the complete source code of the FDK AAC Codec and your
40modifications thereto to recipients of copies in binary form.
41
42The name of Fraunhofer may not be used to endorse or promote products derived from this library without
43prior written permission.
44
45You may not charge copyright license fees for anyone to use, copy or distribute the FDK AAC Codec
46software or your modifications thereto.
47
48Your modified versions of the FDK AAC Codec must carry prominent notices stating that you changed the software
49and 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
533.    NO PATENT LICENSE
54
55NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without limitation the patents of Fraunhofer,
56ARE GRANTED BY THIS SOFTWARE LICENSE. Fraunhofer provides no warranty of patent non-infringement with
57respect to this software.
58
59You may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized
60by appropriate patent licenses.
61
624.    DISCLAIMER
63
64This 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
66of merchantability and fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
67CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary, or consequential damages,
68including but not limited to procurement of substitute goods or services; loss of use, data, or profits,
69or business interruption, however caused and on any theory of liability, whether in contract, strict
70liability, or tort (including negligence), arising in any way out of the use of this software, even if
71advised of the possibility of such damage.
72
735.    CONTACT INFORMATION
74
75Fraunhofer Institute for Integrated Circuits IIS
76Attention: Audio and Multimedia Departments - FDK AAC LL
77Am Wolfsmantel 33
7891058 Erlangen, Germany
79
80www.iis.fraunhofer.de/amm
81amm-info@iis.fraunhofer.de
82----------------------------------------------------------------------------------------------------------- */
83
84/*************************  Fast MPEG AAC Audio Encoder  **********************
85
86   Initial author:       M. Schug / A. Groeschel
87   contents/description: fast aac coder interface library functions
88
89******************************************************************************/
90
91#ifndef _aacenc_h_
92#define _aacenc_h_
93
94#include "common_fix.h"
95#include "FDK_audio.h"
96
97#include "tpenc_lib.h"
98
99#include "sbr_encoder.h"
100
101#ifdef __cplusplus
102extern "C" {
103#endif
104
105/*
106 * AAC-LC error codes.
107 */
108typedef enum {
109  AAC_ENC_OK       = 0x0000,                          /*!< All fine. */
110
111  AAC_ENC_UNKNOWN  = 0x0002,                          /*!< Error condition is of unknown reason, or from another module.              */
112
113  /* initialization errors */
114  aac_enc_init_error_start      = 0x2000,
115  AAC_ENC_INVALID_HANDLE        = 0x2020,             /*!< The handle passed to the function call was invalid (probably NULL).        */
116  AAC_ENC_INVALID_FRAME_LENGTH  = 0x2080,             /*!< Invalid frame length (must be 1024 or 960).                                */
117  AAC_ENC_INVALID_N_CHANNELS    = 0x20e0,             /*!< Invalid amount of audio input channels.                                    */
118  AAC_ENC_INVALID_SFB_TABLE     = 0x2140,             /*!< Internal encoder error.                                                    */
119
120  AAC_ENC_UNSUPPORTED_AOT            = 0x3000,        /*!< The Audio Object Type (AOT) is not supported.                              */
121  AAC_ENC_UNSUPPORTED_BITRATE        = 0x3020,        /*!< The chosen bitrate is not supported.                                       */
122  AAC_ENC_UNSUPPORTED_BITRATE_MODE   = 0x3028,        /*!< Unsupported bit rate mode (CBR or VBR).                                    */
123  AAC_ENC_UNSUPPORTED_ANC_BITRATE    = 0x3040,        /*!< Unsupported ancillay bitrate.                                              */
124  AAC_ENC_UNSUPPORTED_ANC_MODE       = 0x3060,
125  AAC_ENC_UNSUPPORTED_TRANSPORT_TYPE = 0x3080,        /*!< The bitstream format is not supported.                                     */
126  AAC_ENC_UNSUPPORTED_ER_FORMAT      = 0x30a0,        /*!< The error resilience tool format is not supported.                         */
127  AAC_ENC_UNSUPPORTED_EPCONFIG       = 0x30c0,        /*!< The error protection format is not supported.                              */
128  AAC_ENC_UNSUPPORTED_CHANNELCONFIG  = 0x30e0,        /*!< The channel configuration (either number or arrangement) is not supported. */
129  AAC_ENC_UNSUPPORTED_SAMPLINGRATE   = 0x3100,        /*!< Sample rate of audio input is not supported.                               */
130  AAC_ENC_NO_MEMORY                  = 0x3120,        /*!< Could not allocate memory. */
131  AAC_ENC_PE_INIT_TABLE_NOT_FOUND    = 0x3140,        /*!< Internal encoder error.    */
132
133  aac_enc_init_error_end,
134
135  /* encode errors */
136  aac_enc_error_start                       = 0x4000,
137  AAC_ENC_QUANT_ERROR                       = 0x4020,       /*!< Too many bits used in quantization.             */
138  AAC_ENC_WRITTEN_BITS_ERROR                = 0x4040,       /*!< Unexpected number of written bits, differs to
139                                                                 calculated number of bits.                      */
140  AAC_ENC_PNS_TABLE_ERROR                   = 0x4060,       /*!< PNS level out of range.                         */
141  AAC_ENC_GLOBAL_GAIN_TOO_HIGH              = 0x4080,       /*!< Internal quantizer error.                       */
142  AAC_ENC_BITRES_TOO_LOW                    = 0x40a0,       /*!< Too few  bits in bit reservoir.                 */
143  AAC_ENC_BITRES_TOO_HIGH                   = 0x40a1,       /*!< Too many bits in bit reservoir.                 */
144  AAC_ENC_INVALID_CHANNEL_BITRATE           = 0x4100,
145  AAC_ENC_INVALID_ELEMENTINFO_TYPE          = 0x4120,       /*!< Internal encoder error.                         */
146
147  AAC_ENC_WRITE_SCAL_ERROR                  = 0x41e0,       /*!< Error writing scalefacData.                     */
148  AAC_ENC_WRITE_SEC_ERROR                   = 0x4200,       /*!< Error writing sectionData.                      */
149  AAC_ENC_WRITE_SPEC_ERROR                  = 0x4220,       /*!< Error writing spectralData.                     */
150  aac_enc_error_end
151
152} AAC_ENCODER_ERROR;
153/*-------------------------- defines --------------------------------------*/
154
155#define ANC_DATA_BUFFERSIZE 1024      /* ancBuffer size */
156
157#define MAX_TOTAL_EXT_PAYLOADS  (((6) * (1)) + (2+2))
158
159
160typedef enum {
161  AACENC_BR_MODE_INVALID = -1,  /*!< Invalid bitrate mode.                            */
162  AACENC_BR_MODE_CBR     =  0,  /*!< Constant bitrate mode.                           */
163  AACENC_BR_MODE_VBR_1   =  1,  /*!< Variable bitrate mode, about 32 kbps/channel.    */
164  AACENC_BR_MODE_VBR_2   =  2,  /*!< Variable bitrate mode, about 40 kbps/channel.    */
165  AACENC_BR_MODE_VBR_3   =  3,  /*!< Variable bitrate mode, about 48-56 kbps/channel. */
166  AACENC_BR_MODE_VBR_4   =  4,  /*!< Variable bitrate mode, about 64 kbps/channel.    */
167  AACENC_BR_MODE_VBR_5   =  5,  /*!< Variable bitrate mode, about 80-96 kbps/channel. */
168  AACENC_BR_MODE_FF      =  6,  /*!< Fixed frame mode.                                */
169  AACENC_BR_MODE_SFR     =  7   /*!< Superframe mode.                                 */
170
171} AACENC_BITRATE_MODE;
172
173typedef enum {
174
175  CH_ORDER_MPEG = 0,  /*!< MPEG channel ordering (e. g. 5.1: C, L, R, SL, SR, LFE)           */
176  CH_ORDER_WAV        /*!< WAV fileformat channel ordering (e. g. 5.1: L, R, C, LFE, SL, SR) */
177
178} CHANNEL_ORDER;
179
180/*-------------------- structure definitions ------------------------------*/
181
182struct AACENC_CONFIG {
183  INT   sampleRate;             /* encoder sample rate */
184  INT   bitRate;                /* encoder bit rate in bits/sec */
185  INT   ancDataBitRate;         /* additional bits consumed by anc data or sbr have to be consiedered while configuration */
186
187  INT   nSubFrames;             /* number of frames in super frame (not ADTS/LATM subframes !) */
188  AUDIO_OBJECT_TYPE audioObjectType;   /* Audio Object Type  */
189
190  INT   averageBits;            /* encoder bit rate in bits/superframe */
191  INT   bitrateMode;            /* encoder bitrate mode (CBR/VBR) */
192  INT   nChannels;              /* number of channels to process */
193  CHANNEL_ORDER channelOrder;   /* Input Channel ordering scheme. */
194  INT   bandWidth;              /* targeted audio bandwidth in Hz */
195  CHANNEL_MODE  channelMode;    /* encoder channel mode configuration */
196  INT   framelength;            /* used frame size */
197
198  UINT  syntaxFlags;            /* bitstreams syntax configuration */
199  SCHAR epConfig;               /* error protection configuration */
200
201  INT   anc_Rate;               /* ancillary rate, 0 (disabled), -1 (default) else desired rate */
202  UINT  maxAncBytesPerAU;
203  INT   minBitsPerFrame;        /* minimum number of bits in AU */
204  INT   maxBitsPerFrame;        /* maximum number of bits in AU */
205  INT   bitreservoir;           /* size of bitreservoir */
206
207  UCHAR useTns;                 /* flag: use temporal noise shaping */
208  UCHAR usePns;                 /* flag: use perceptual noise substitution */
209  UCHAR useIS;                  /* flag: use intensity coding */
210
211  UCHAR useRequant;             /* flag: use afterburner */
212};
213
214typedef struct {
215  UCHAR            *pData;      /* pointer to extension payload data */
216  UINT              dataSize;   /* extension payload data size in bits */
217  EXT_PAYLOAD_TYPE  dataType;   /* extension payload data type */
218  INT               associatedChElement;  /* number of the channel element the data is assigned to */
219} AACENC_EXT_PAYLOAD;
220
221typedef struct AAC_ENC *HANDLE_AAC_ENC;
222
223/**
224 * \brief Limit given bit rate to a valid value
225 * \param hTpEnc transport encoder handle
226 * \param coreSamplingRate the sample rate to be used for the AAC encoder
227 * \param frameLength the frameLength to be used for the AAC encoder
228 * \param nChannels number of total channels
229 * \param nChannelsEff number of effective channels
230 * \param bitRate the initial bit rate value for which the closest valid bit rate value is searched for
231 * \param averageBits average bits per frame for fixed framing. Set to -1 if not available.
232 * \param optional pointer where the current bits per frame are stored into.
233 * \param bitrateMode the current bit rate mode
234 * \param nSubFrames number of sub frames for super framing (not transport frames).
235 * \return a valid bit rate value as close as possible or identical to bitRate
236 */
237INT FDKaacEnc_LimitBitrate(
238        HANDLE_TRANSPORTENC hTpEnc,
239        INT coreSamplingRate,
240        INT frameLength,
241        INT nChannels,
242        INT nChannelsEff,
243        INT bitRate,
244        INT averageBits,
245        INT *pAverageBitsPerFrame,
246        INT bitrateMode,
247        INT nSubFrames
248        );
249
250 /*-----------------------------------------------------------------------------
251
252     functionname: FDKaacEnc_GetVBRBitrate
253     description:  Get VBR bitrate from vbr quality
254     input params: int vbrQuality (VBR0, VBR1, VBR2)
255                   channelMode
256     returns:      vbr bitrate
257
258 ------------------------------------------------------------------------------*/
259 INT FDKaacEnc_GetVBRBitrate(INT bitrateMode, CHANNEL_MODE channelMode);
260
261
262/*-----------------------------------------------------------------------------
263
264     functionname: FDKaacEnc_AacInitDefaultConfig
265     description:  gives reasonable default configuration
266     returns:      ---
267
268 ------------------------------------------------------------------------------*/
269void FDKaacEnc_AacInitDefaultConfig(AACENC_CONFIG     *config);
270
271/*---------------------------------------------------------------------------
272
273	    functionname:FDKaacEnc_Open
274    description: allocate and initialize a new encoder instance
275    returns:     0 if success
276
277  ---------------------------------------------------------------------------*/
278AAC_ENCODER_ERROR FDKaacEnc_Open(HANDLE_AAC_ENC  *phAacEnc,              /* pointer to an encoder handle, initialized on return */
279                                 const INT        nElements,             /* number of maximal elements in instance to support */
280                                 const INT        nChannels,             /* number of maximal channels in instance to support */
281                                 const INT        nSubFrames);           /* support superframing in instance */
282
283
284AAC_ENCODER_ERROR FDKaacEnc_Initialize(HANDLE_AAC_ENC     hAacEncoder,   /* pointer to an encoder handle, initialized on return */
285                                       AACENC_CONFIG    *config,         /* pre-initialized config struct */
286                                       HANDLE_TRANSPORTENC hTpEnc,
287                                       ULONG               initFlags);
288
289
290/*---------------------------------------------------------------------------
291
292    functionname: FDKaacEnc_EncodeFrame
293    description:  encode one frame
294    returns:      0 if success
295
296  ---------------------------------------------------------------------------*/
297
298AAC_ENCODER_ERROR FDKaacEnc_EncodeFrame( HANDLE_AAC_ENC       hAacEnc,         /* encoder handle */
299                                         HANDLE_TRANSPORTENC  hTpEnc,
300                                         INT_PCM*             inputBuffer,
301                                         INT*                 numOutBytes,
302                                         AACENC_EXT_PAYLOAD   extPayload[MAX_TOTAL_EXT_PAYLOADS]
303                                       );
304
305/*---------------------------------------------------------------------------
306
307    functionname:FDKaacEnc_Close
308    description: delete encoder instance
309    returns:
310
311  ---------------------------------------------------------------------------*/
312
313void FDKaacEnc_Close( HANDLE_AAC_ENC*  phAacEnc);   /* encoder handle */
314
315#ifdef __cplusplus
316}
317#endif
318
319#endif /* _aacenc_h_ */
320
321