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