1
2/* -----------------------------------------------------------------------------------------------------------
3Software 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
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#define BITRES_MAX_LD   4000
102#define BITRES_MIN_LD    500
103#define BITRATE_MAX_LD 70000 /* Max assumed bitrate for bitres calculation */
104#define BITRATE_MIN_LD 12000 /* Min assumed bitrate for bitres calculation */
105
106#ifdef __cplusplus
107extern "C" {
108#endif
109
110/*
111 * AAC-LC error codes.
112 */
113typedef enum {
114  AAC_ENC_OK       = 0x0000,                          /*!< All fine. */
115
116  AAC_ENC_UNKNOWN  = 0x0002,                          /*!< Error condition is of unknown reason, or from another module.              */
117
118  /* initialization errors */
119  aac_enc_init_error_start      = 0x2000,
120  AAC_ENC_INVALID_HANDLE        = 0x2020,             /*!< The handle passed to the function call was invalid (probably NULL).        */
121  AAC_ENC_INVALID_FRAME_LENGTH  = 0x2080,             /*!< Invalid frame length.                                                      */
122  AAC_ENC_INVALID_N_CHANNELS    = 0x20e0,             /*!< Invalid amount of audio input channels.                                    */
123  AAC_ENC_INVALID_SFB_TABLE     = 0x2140,             /*!< Internal encoder error.                                                    */
124
125  AAC_ENC_UNSUPPORTED_AOT            = 0x3000,        /*!< The Audio Object Type (AOT) is not supported.                              */
126  AAC_ENC_UNSUPPORTED_BITRATE        = 0x3020,        /*!< The chosen bitrate is not supported.                                       */
127  AAC_ENC_UNSUPPORTED_BITRATE_MODE   = 0x3028,        /*!< Unsupported bit rate mode (CBR or VBR).                                    */
128  AAC_ENC_UNSUPPORTED_ANC_BITRATE    = 0x3040,        /*!< Unsupported ancillay bitrate.                                              */
129  AAC_ENC_UNSUPPORTED_ANC_MODE       = 0x3060,
130  AAC_ENC_UNSUPPORTED_TRANSPORT_TYPE = 0x3080,        /*!< The bitstream format is not supported.                                     */
131  AAC_ENC_UNSUPPORTED_ER_FORMAT      = 0x30a0,        /*!< The error resilience tool format is not supported.                         */
132  AAC_ENC_UNSUPPORTED_EPCONFIG       = 0x30c0,        /*!< The error protection format is not supported.                              */
133  AAC_ENC_UNSUPPORTED_CHANNELCONFIG  = 0x30e0,        /*!< The channel configuration (either number or arrangement) is not supported. */
134  AAC_ENC_UNSUPPORTED_SAMPLINGRATE   = 0x3100,        /*!< Sample rate of audio input is not supported.                               */
135  AAC_ENC_NO_MEMORY                  = 0x3120,        /*!< Could not allocate memory. */
136  AAC_ENC_PE_INIT_TABLE_NOT_FOUND    = 0x3140,        /*!< Internal encoder error.    */
137
138  aac_enc_init_error_end,
139
140  /* encode errors */
141  aac_enc_error_start                       = 0x4000,
142  AAC_ENC_QUANT_ERROR                       = 0x4020,       /*!< Too many bits used in quantization.             */
143  AAC_ENC_WRITTEN_BITS_ERROR                = 0x4040,       /*!< Unexpected number of written bits, differs to
144                                                                 calculated number of bits.                      */
145  AAC_ENC_PNS_TABLE_ERROR                   = 0x4060,       /*!< PNS level out of range.                         */
146  AAC_ENC_GLOBAL_GAIN_TOO_HIGH              = 0x4080,       /*!< Internal quantizer error.                       */
147  AAC_ENC_BITRES_TOO_LOW                    = 0x40a0,       /*!< Too few  bits in bit reservoir.                 */
148  AAC_ENC_BITRES_TOO_HIGH                   = 0x40a1,       /*!< Too many bits in bit reservoir.                 */
149  AAC_ENC_INVALID_CHANNEL_BITRATE           = 0x4100,
150  AAC_ENC_INVALID_ELEMENTINFO_TYPE          = 0x4120,       /*!< Internal encoder error.                         */
151
152  AAC_ENC_WRITE_SCAL_ERROR                  = 0x41e0,       /*!< Error writing scalefacData.                     */
153  AAC_ENC_WRITE_SEC_ERROR                   = 0x4200,       /*!< Error writing sectionData.                      */
154  AAC_ENC_WRITE_SPEC_ERROR                  = 0x4220,       /*!< Error writing spectralData.                     */
155  aac_enc_error_end
156
157} AAC_ENCODER_ERROR;
158/*-------------------------- defines --------------------------------------*/
159
160#define ANC_DATA_BUFFERSIZE 1024      /* ancBuffer size */
161
162#define MAX_TOTAL_EXT_PAYLOADS  (((8) * (1)) + (2+2))
163
164
165typedef enum {
166  AACENC_BR_MODE_INVALID = -1,  /*!< Invalid bitrate mode.                            */
167  AACENC_BR_MODE_CBR     =  0,  /*!< Constant bitrate mode.                           */
168  AACENC_BR_MODE_VBR_1   =  1,  /*!< Variable bitrate mode, about 32 kbps/channel.    */
169  AACENC_BR_MODE_VBR_2   =  2,  /*!< Variable bitrate mode, about 40 kbps/channel.    */
170  AACENC_BR_MODE_VBR_3   =  3,  /*!< Variable bitrate mode, about 48-56 kbps/channel. */
171  AACENC_BR_MODE_VBR_4   =  4,  /*!< Variable bitrate mode, about 64 kbps/channel.    */
172  AACENC_BR_MODE_VBR_5   =  5,  /*!< Variable bitrate mode, about 80-96 kbps/channel. */
173  AACENC_BR_MODE_FF      =  6,  /*!< Fixed frame mode.                                */
174  AACENC_BR_MODE_SFR     =  7   /*!< Superframe mode.                                 */
175
176} AACENC_BITRATE_MODE;
177
178typedef enum {
179
180  CH_ORDER_MPEG = 0,  /*!< MPEG channel ordering (e. g. 5.1: C, L, R, SL, SR, LFE)           */
181  CH_ORDER_WAV        /*!< WAV fileformat channel ordering (e. g. 5.1: L, R, C, LFE, SL, SR) */
182
183} CHANNEL_ORDER;
184
185/*-------------------- structure definitions ------------------------------*/
186
187struct AACENC_CONFIG {
188  INT   sampleRate;             /* encoder sample rate */
189  INT   bitRate;                /* encoder bit rate in bits/sec */
190  INT   ancDataBitRate;         /* additional bits consumed by anc data or sbr have to be consiedered while configuration */
191
192  INT   nSubFrames;             /* number of frames in super frame (not ADTS/LATM subframes !) */
193  AUDIO_OBJECT_TYPE audioObjectType;   /* Audio Object Type  */
194
195  INT   averageBits;            /* encoder bit rate in bits/superframe */
196  INT   bitrateMode;            /* encoder bitrate mode (CBR/VBR) */
197  INT   nChannels;              /* number of channels to process */
198  CHANNEL_ORDER channelOrder;   /* Input Channel ordering scheme. */
199  INT   bandWidth;              /* targeted audio bandwidth in Hz */
200  CHANNEL_MODE  channelMode;    /* encoder channel mode configuration */
201  INT   framelength;            /* used frame size */
202
203  UINT  syntaxFlags;            /* bitstreams syntax configuration */
204  SCHAR epConfig;               /* error protection configuration */
205
206  INT   anc_Rate;               /* ancillary rate, 0 (disabled), -1 (default) else desired rate */
207  UINT  maxAncBytesPerAU;
208  INT   minBitsPerFrame;        /* minimum number of bits in AU */
209  INT   maxBitsPerFrame;        /* maximum number of bits in AU */
210  INT   bitreservoir;           /* size of bitreservoir */
211
212  INT   audioMuxVersion;        /* audio mux version in loas/latm transport format */
213
214  UINT  sbrRatio;               /* sbr sampling rate ratio: dual- or single-rate */
215
216  UCHAR useTns;                 /* flag: use temporal noise shaping */
217  UCHAR usePns;                 /* flag: use perceptual noise substitution */
218  UCHAR useIS;                  /* flag: use intensity coding */
219
220  UCHAR useRequant;             /* flag: use afterburner */
221};
222
223typedef struct {
224  UCHAR            *pData;      /* pointer to extension payload data */
225  UINT              dataSize;   /* extension payload data size in bits */
226  EXT_PAYLOAD_TYPE  dataType;   /* extension payload data type */
227  INT               associatedChElement;  /* number of the channel element the data is assigned to */
228} AACENC_EXT_PAYLOAD;
229
230typedef struct AAC_ENC *HANDLE_AAC_ENC;
231
232/**
233 * \brief Calculate framesize in bits for given bit rate, frame length and sampling rate.
234 *
235 * \param bitRate               Ttarget bitrate in bits per second.
236 * \param frameLength           Number of audio samples in one frame.
237 * \param samplingRate          Sampling rate in Hz.
238 *
239 * \return                      Framesize in bits per frame.
240*/
241INT FDKaacEnc_CalcBitsPerFrame(
242        const INT bitRate,
243        const INT frameLength,
244        const INT samplingRate
245        );
246
247/**
248 * \brief Calculate bitrate in bits per second for given framesize, frame length and sampling rate.
249 *
250 * \param bitsPerFrame          Framesize in bits per frame.
251 * \param frameLength           Number of audio samples in one frame.
252 * \param samplingRate          Sampling rate in Hz.
253 *
254 * \return                      Bitrate in bits per second.
255*/
256INT FDKaacEnc_CalcBitrate(
257        const INT bitsPerFrame,
258        const INT frameLength,
259        const INT samplingRate
260        );
261
262/**
263 * \brief Limit given bit rate to a valid value
264 * \param hTpEnc transport encoder handle
265 * \param coreSamplingRate the sample rate to be used for the AAC encoder
266 * \param frameLength the frameLength to be used for the AAC encoder
267 * \param nChannels number of total channels
268 * \param nChannelsEff number of effective channels
269 * \param bitRate the initial bit rate value for which the closest valid bit rate value is searched for
270 * \param averageBits average bits per frame for fixed framing. Set to -1 if not available.
271 * \param optional pointer where the current bits per frame are stored into.
272 * \param bitrateMode the current bit rate mode
273 * \param nSubFrames number of sub frames for super framing (not transport frames).
274 * \return a valid bit rate value as close as possible or identical to bitRate
275 */
276INT FDKaacEnc_LimitBitrate(
277        HANDLE_TRANSPORTENC hTpEnc,
278        INT coreSamplingRate,
279        INT frameLength,
280        INT nChannels,
281        INT nChannelsEff,
282        INT bitRate,
283        INT averageBits,
284        INT *pAverageBitsPerFrame,
285        INT bitrateMode,
286        INT nSubFrames
287        );
288
289 /*-----------------------------------------------------------------------------
290
291     functionname: FDKaacEnc_GetVBRBitrate
292     description:  Get VBR bitrate from vbr quality
293     input params: int vbrQuality (VBR0, VBR1, VBR2)
294                   channelMode
295     returns:      vbr bitrate
296
297 ------------------------------------------------------------------------------*/
298 INT FDKaacEnc_GetVBRBitrate(INT bitrateMode, CHANNEL_MODE channelMode);
299
300
301/*-----------------------------------------------------------------------------
302
303     functionname: FDKaacEnc_AacInitDefaultConfig
304     description:  gives reasonable default configuration
305     returns:      ---
306
307 ------------------------------------------------------------------------------*/
308void FDKaacEnc_AacInitDefaultConfig(AACENC_CONFIG     *config);
309
310/*---------------------------------------------------------------------------
311
312	    functionname:FDKaacEnc_Open
313    description: allocate and initialize a new encoder instance
314    returns:     0 if success
315
316  ---------------------------------------------------------------------------*/
317AAC_ENCODER_ERROR FDKaacEnc_Open(HANDLE_AAC_ENC  *phAacEnc,              /* pointer to an encoder handle, initialized on return */
318                                 const INT        nElements,             /* number of maximal elements in instance to support */
319                                 const INT        nChannels,             /* number of maximal channels in instance to support */
320                                 const INT        nSubFrames);           /* support superframing in instance */
321
322
323AAC_ENCODER_ERROR FDKaacEnc_Initialize(HANDLE_AAC_ENC     hAacEncoder,   /* pointer to an encoder handle, initialized on return */
324                                       AACENC_CONFIG    *config,         /* pre-initialized config struct */
325                                       HANDLE_TRANSPORTENC hTpEnc,
326                                       ULONG               initFlags);
327
328
329/*---------------------------------------------------------------------------
330
331    functionname: FDKaacEnc_EncodeFrame
332    description:  encode one frame
333    returns:      0 if success
334
335  ---------------------------------------------------------------------------*/
336
337AAC_ENCODER_ERROR FDKaacEnc_EncodeFrame( HANDLE_AAC_ENC       hAacEnc,         /* encoder handle */
338                                         HANDLE_TRANSPORTENC  hTpEnc,
339                                         INT_PCM*             inputBuffer,
340                                         INT*                 numOutBytes,
341                                         AACENC_EXT_PAYLOAD   extPayload[MAX_TOTAL_EXT_PAYLOADS]
342                                       );
343
344/*---------------------------------------------------------------------------
345
346    functionname:FDKaacEnc_Close
347    description: delete encoder instance
348    returns:
349
350  ---------------------------------------------------------------------------*/
351
352void FDKaacEnc_Close( HANDLE_AAC_ENC*  phAacEnc);   /* encoder handle */
353
354#ifdef __cplusplus
355}
356#endif
357
358#endif /* _aacenc_h_ */
359
360