1
2/* -----------------------------------------------------------------------------------------------------------
3Software 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
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/***************************  Fraunhofer IIS ***********************
85
86   Author(s):
87   Description: SBR encoder top level processing prototype
88
89******************************************************************************/
90
91#ifndef __SBR_ENCODER_H
92#define __SBR_ENCODER_H
93
94#include "common_fix.h"
95#include "FDK_audio.h"
96
97#include "FDK_bitstream.h"
98
99/* core coder helpers */
100#define MAX_TRANS_FAC         8
101#define MAX_CODEC_FRAME_RATIO 2
102#define MAX_PAYLOAD_SIZE    256
103
104typedef enum codecType
105{
106  CODEC_AAC=0,
107  CODEC_AACLD=1,
108  CODEC_UNSPECIFIED=99
109} CODEC_TYPE;
110
111
112typedef struct
113{
114  INT bitRate;
115  INT nChannels;
116  INT sampleFreq;
117  INT transFac;
118  INT standardBitrate;
119} CODEC_PARAM;
120
121typedef enum
122{
123  SBR_MONO,
124  SBR_LEFT_RIGHT,
125  SBR_COUPLING,
126  SBR_SWITCH_LRC
127} SBR_STEREO_MODE;
128
129/* bitstream syntax flags */
130enum
131{
132  SBR_SYNTAX_LOW_DELAY = 0x0001,
133  SBR_SYNTAX_SCALABLE  = 0x0002,
134  SBR_SYNTAX_CRC       = 0x0004,
135  SBR_SYNTAX_DRM_CRC   = 0x0008
136};
137
138typedef struct
139{
140  CODEC_TYPE       coreCoder;        /*!< LC or ELD */
141  UINT             bitrateFrom;      /*!< inclusive */
142  UINT             bitrateTo;        /*!< exclusive */
143
144  UINT             sampleRate;       /*!<   */
145  UCHAR            numChannels;      /*!<   */
146
147  UCHAR            startFreq;        /*!< bs_start_freq */
148  UCHAR            startFreqSpeech;  /*!< bs_start_freq for speech config flag */
149  UCHAR            stopFreq;         /*!< bs_stop_freq */
150  UCHAR            stopFreqSpeech;   /*!< bs_stop_freq for speech config flag */
151
152  UCHAR            numNoiseBands;    /*!<   */
153  UCHAR            noiseFloorOffset; /*!<   */
154  SCHAR            noiseMaxLevel;    /*!<   */
155  SBR_STEREO_MODE  stereoMode;       /*!<   */
156  UCHAR            freqScale;        /*!<   */
157} sbrTuningTable_t;
158
159typedef struct sbrConfiguration
160{
161  /*
162     core coder dependent configurations
163  */
164  CODEC_PARAM codecSettings;  /*!< Core coder settings. To be set from core coder. */
165  INT SendHeaderDataTime;     /*!< SBR header send update frequency in ms. */
166  INT useWaveCoding;          /*!< Flag: usage of wavecoding tool. */
167  INT crcSbr;                 /*!< Flag: usage of SBR-CRC. */
168  INT dynBwSupported;         /*!< Flag: support for dynamic bandwidth in this combination. */
169  INT parametricCoding;       /*!< Flag: usage of parametric coding tool. */
170  INT downSampleFactor;       /*!< Sampling rate relation between the SBR and the core encoder. */
171  int freq_res_fixfix[3];     /*!< Frequency resolution of envelopes in frame class FIXFIX
172                                 0=1 Env; 1=2 Env; 2=4 Env; */
173  /*
174     core coder dependent tuning parameters
175  */
176  INT tran_thr;             /*!< SBR transient detector threshold (* 100). */
177  INT noiseFloorOffset;     /*!< Noise floor offset.      */
178  UINT useSpeechConfig;     /*!< Flag: adapt tuning parameters according to speech. */
179
180
181
182  /*
183     core coder independent configurations
184  */
185  INT sbrFrameSize;           /*!< SBR frame size in samples. Will be calculated from core coder settings. */
186  INT sbr_data_extra;         /*!< Flag usage of data extra. */
187  INT amp_res;                /*!< Amplitude resolution. */
188  INT ana_max_level;          /*!< Noise insertion maximum level. */
189  INT tran_fc;                /*!< Transient detector start frequency. */
190  INT tran_det_mode;          /*!< Transient detector mode. */
191  INT spread;                 /*!< Flag: usage of SBR spread. */
192  INT stat;                   /*!< Flag: usage of static framing. */
193  INT e;                      /*!< Number of envelopes when static framing is chosen. */
194  SBR_STEREO_MODE stereoMode; /*!< SBR stereo mode. */
195  INT deltaTAcrossFrames;     /*!< Flag: allow time-delta coding. */
196  FIXP_DBL dF_edge_1stEnv;    /*!< Extra fraction delta-F coding is allowed to be more expensive. */
197  FIXP_DBL dF_edge_incr;      /*!< Increment dF_edge_1stEnv this much if dT-coding was used this frame. */
198  INT sbr_invf_mode;          /*!< Inverse filtering mode. */
199  INT sbr_xpos_mode;          /*!< Transposer mode. */
200  INT sbr_xpos_ctrl;          /*!< Transposer control. */
201  INT sbr_xpos_level;         /*!< Transposer 3rd order level. */
202  INT startFreq;              /*!< The start frequency table index. */
203  INT stopFreq;               /*!< The stop frequency table index. */
204  INT useSaPan;               /*!< Flag: usage of SAPAN stereo. */
205  INT dynBwEnabled;           /*!< Flag: usage of dynamic bandwidth. */
206  INT bParametricStereo;      /*!< Flag: usage of parametric stereo coding tool. */
207
208  /*
209     header_extra1 configuration
210  */
211  UCHAR freqScale;            /*!< Frequency grouping. */
212  INT alterScale;             /*!< Scale resolution. */
213  INT sbr_noise_bands;        /*!< Number of noise bands. */
214
215
216  /*
217     header_extra2 configuration
218  */
219  INT sbr_limiter_bands;      /*!< Number of limiter bands. */
220  INT sbr_limiter_gains;      /*!< Gain of limiter. */
221  INT sbr_interpol_freq;      /*!< Flag: use interpolation in freq. direction. */
222  INT sbr_smoothing_length;   /*!< Flag: choose length 4 or 0 (=on, off). */
223  UCHAR init_amp_res_FF;
224} sbrConfiguration, *sbrConfigurationPtr ;
225
226typedef struct SBR_CONFIG_DATA
227{
228  UINT sbrSyntaxFlags;                  /**< SBR syntax flags derived from AOT. */
229  INT nChannels;                        /**< Number of channels.  */
230
231  INT nSfb[2];                          /**< Number of SBR scalefactor bands for LO_RES and HI_RES (?) */
232  INT num_Master;                       /**< Number of elements in v_k_master. */
233  INT sampleFreq;                       /**< SBR sampling frequency. */
234  INT frameSize;
235  INT xOverFreq;                        /**< The SBR start frequency. */
236  INT dynXOverFreq;                     /**< Used crossover frequency when dynamic bandwidth is enabled. */
237  INT noQmfBands;                       /**< Number of QMF frequency bands. */
238  INT noQmfSlots;                       /**< Number of QMF slots. */
239
240  UCHAR *freqBandTable[2];              /**< Frequency table for low and hires, only MAX_FREQ_COEFFS/2 +1 coeefs actually needed for lowres. */
241  UCHAR *v_k_master;                    /**< Master BandTable where freqBandTable is derived from. */
242
243
244  SBR_STEREO_MODE stereoMode;
245  INT noEnvChannels;                    /**< Number of envelope channels. */
246
247  INT useWaveCoding;                    /**< Flag indicates whether to use wave coding at all.      */
248  INT useParametricCoding;              /**< Flag indicates whether to use para coding at all.      */
249  INT xposCtrlSwitch;                   /**< Flag indicates whether to switch xpos ctrl on the fly. */
250  INT switchTransposers;                /**< Flag indicates whether to switch xpos on the fly .     */
251  UCHAR initAmpResFF;
252} SBR_CONFIG_DATA, *HANDLE_SBR_CONFIG_DATA;
253
254typedef struct {
255  MP4_ELEMENT_ID elType;
256  INT bitRate;
257  int instanceTag;
258  UCHAR fParametricStereo;
259  UCHAR nChannelsInEl;
260  UCHAR ChannelIndex[2];
261} SBR_ELEMENT_INFO;
262
263#ifdef __cplusplus
264extern "C" {
265#endif
266
267typedef struct SBR_ENCODER *HANDLE_SBR_ENCODER;
268
269/**
270 * \brief  Get the max required input buffer size including delay balancing space
271 *         for N audio channels.
272 * \param noChannels  Number of audio channels.
273 * \return            Max required input buffer size in bytes.
274 */
275INT sbrEncoder_GetInBufferSize(int noChannels);
276
277INT sbrEncoder_Open(
278        HANDLE_SBR_ENCODER  *phSbrEncoder,
279        INT                  nElements,
280        INT                  nChannels,
281        INT                  supportPS
282        );
283
284/**
285 * \brief                 Get closest working bitrate to specified desired
286 *                        bitrate for a single SBR element.
287 * \param bitRate         The desired target bit rate
288 * \param numChannels     The amount of audio channels
289 * \param coreSampleRate  The sample rate of the core coder
290 * \param aot             The current Audio Object Type
291 * \return                Closest working bit rate to bitRate value
292 */
293UINT sbrEncoder_LimitBitRate(UINT bitRate, UINT numChannels, UINT coreSampleRate, AUDIO_OBJECT_TYPE aot);
294
295
296/**
297 * \brief                Check whether downsampled SBR single rate is possible
298 *                       with given audio object type.
299 * \param aot            The Audio object type.
300 * \return               0 when downsampled SBR is not possible,
301 *                       1 when downsampled SBR is possible.
302 */
303UINT sbrEncoder_IsSingleRatePossible(AUDIO_OBJECT_TYPE aot);
304
305/**
306 * \brief                  Initialize SBR Encoder instance.
307 * \param phSbrEncoder     Pointer to a SBR Encoder instance.
308 * \param elInfo           Structure that describes the element/channel arrangement.
309 * \param noElements       Amount of elements described in elInfo.
310 * \param inputBuffer      Pointer to the encoder audio buffer
311 * \param bandwidth        Returns the core audio encoder bandwidth (output)
312 * \param bufferOffset     Returns the offset for the audio input data in order to do delay balancing.
313 * \param numChannels      Input: Encoder input channels. output: core encoder channels.
314 * \param sampleRate       Input: Encoder samplerate. output core encoder samplerate.
315 * \param downSampleFactor Input: Relation between SBR and core coder sampling rate;
316 * \param frameLength      Input: Encoder frameLength. output core encoder frameLength.
317 * \param aot              Input: Desired AOT. output AOT to be used after parameter checking.
318 * \param delay            Input: core encoder delay. Output: total delay because of SBR.
319 * \param transformFactor  The core encoder transform factor (blockswitching).
320 * \param headerPeriod     Repetition rate of the SBR header:
321 *                           - (-1) means intern configuration.
322 *                           - (1-10) corresponds to header repetition rate in frames.
323 * \return                 0 on success, and non-zero if failed.
324 */
325INT sbrEncoder_Init(
326        HANDLE_SBR_ENCODER   hSbrEncoder,
327        SBR_ELEMENT_INFO     elInfo[(8)],
328        int                  noElements,
329        INT_PCM             *inputBuffer,
330        INT                 *coreBandwidth,
331        INT                 *inputBufferOffset,
332        INT                 *numChannels,
333        INT                 *sampleRate,
334        UINT                *downSampleFactor,
335        INT                 *frameLength,
336        AUDIO_OBJECT_TYPE    aot,
337        int                 *delay,
338        int                  transformFactor,
339        const int            headerPeriod,
340        ULONG                statesInitFlag
341        );
342
343/**
344 * \brief             Do delay line buffers housekeeping. To be called after each encoded audio frame.
345 * \param hEnvEnc     SBR Encoder handle.
346 * \param timeBuffer  Pointer to the encoder audio buffer.
347 * \return            0 on success, and non-zero if failed.
348 */
349INT sbrEncoder_UpdateBuffers(HANDLE_SBR_ENCODER hEnvEnc,
350                             INT_PCM *timeBuffer
351                            );
352
353/**
354 * \brief               Close SBR encoder instance.
355 * \param phEbrEncoder  Handle of SBR encoder instance to be closed.
356 * \return              void
357 */
358void sbrEncoder_Close(HANDLE_SBR_ENCODER *phEbrEncoder);
359
360/**
361 * \brief               Encode SBR data of one complete audio frame.
362 * \param hEnvEncoder   Handle of SBR encoder instance.
363 * \param samples       Time samples, always interleaved.
364 * \param timeInStride  Channel stride factor of samples buffer.
365 * \param sbrDataBits   Size of SBR payload in bits.
366 * \param sbrData       SBR payload.
367 * \return              0 on success, and non-zero if failed.
368 */
369INT sbrEncoder_EncodeFrame(HANDLE_SBR_ENCODER  hEnvEncoder,
370                           INT_PCM            *samples,
371                           UINT                timeInStride,
372                           UINT                sbrDataBits[(8)],
373                           UCHAR               sbrData[(8)][MAX_PAYLOAD_SIZE]
374                          );
375
376/**
377 * \brief               Write SBR headers of one SBR element.
378 * \param sbrEncoder    Handle of the SBR encoder instance.
379 * \param hBs           Handle of bit stream handle to write SBR header to.
380 * \param element_index Index of the SBR element which header should be written.
381 * \param fSendHeaders  Flag indicating that the SBR encoder should send more headers in the SBR payload or not.
382 * \return              void
383 */
384void sbrEncoder_GetHeader(HANDLE_SBR_ENCODER   sbrEncoder,
385                          HANDLE_FDK_BITSTREAM hBs,
386                          INT            element_index,
387                          int            fSendHeaders);
388
389/**
390 * \brief              SBR encoder bitrate estimation.
391 * \param hSbrEncoder  SBR encoder handle.
392 * \return             Estimated bitrate.
393 */
394INT sbrEncoder_GetEstimateBitrate(HANDLE_SBR_ENCODER hSbrEncoder);
395
396
397/**
398 * \brief              Delay between input data and downsampled output data.
399 * \param hSbrEncoder  SBR encoder handle.
400 * \return             Delay.
401 */
402INT sbrEncoder_GetInputDataDelay(HANDLE_SBR_ENCODER hSbrEncoder);
403
404/**
405 * \brief       Get decoder library version info.
406 * \param info  Pointer to an allocated LIB_INFO struct, where library info is written to.
407 * \return      0 on sucess.
408 */
409INT sbrEncoder_GetLibInfo(LIB_INFO *info);
410
411void sbrPrintRAM(void);
412
413void sbrPrintROM(void);
414
415#ifdef __cplusplus
416        }
417#endif
418
419#endif /* ifndef __SBR_MAIN_H */
420