1 
2 /* -----------------------------------------------------------------------------------------------------------
3 Software 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
9 The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software that implements
10 the MPEG Advanced Audio Coding ("AAC") encoding and decoding scheme for digital audio.
11 This FDK AAC Codec software is intended to be used on a wide variety of Android devices.
12 
13 AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient general perceptual
14 audio codecs. AAC-ELD is considered the best-performing full-bandwidth communications codec by
15 independent studies and is widely deployed. AAC has been standardized by ISO and IEC as part
16 of the MPEG specifications.
17 
18 Patent licenses for necessary patent claims for the FDK AAC Codec (including those of Fraunhofer)
19 may be obtained through Via Licensing (www.vialicensing.com) or through the respective patent owners
20 individually for the purpose of encoding or decoding bit streams in products that are compliant with
21 the ISO/IEC MPEG audio standards. Please note that most manufacturers of Android devices already license
22 these patent claims through Via Licensing or directly from the patent owners, and therefore FDK AAC Codec
23 software may already be covered under those patent licenses when it is used for those licensed purposes only.
24 
25 Commercially-licensed AAC software libraries, including floating-point versions with enhanced sound quality,
26 are also available from Fraunhofer. Users are encouraged to check the Fraunhofer website for additional
27 applications information and documentation.
28 
29 2.    COPYRIGHT LICENSE
30 
31 Redistribution and use in source and binary forms, with or without modification, are permitted without
32 payment of copyright license fees provided that you satisfy the following conditions:
33 
34 You must retain the complete text of this software license in redistributions of the FDK AAC Codec or
35 your modifications thereto in source code form.
36 
37 You must retain the complete text of this software license in the documentation and/or other materials
38 provided with redistributions of the FDK AAC Codec or your modifications thereto in binary form.
39 You must make available free of charge copies of the complete source code of the FDK AAC Codec and your
40 modifications thereto to recipients of copies in binary form.
41 
42 The name of Fraunhofer may not be used to endorse or promote products derived from this library without
43 prior written permission.
44 
45 You may not charge copyright license fees for anyone to use, copy or distribute the FDK AAC Codec
46 software or your modifications thereto.
47 
48 Your modified versions of the FDK AAC Codec must carry prominent notices stating that you changed the software
49 and 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 
53 3.    NO PATENT LICENSE
54 
55 NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without limitation the patents of Fraunhofer,
56 ARE GRANTED BY THIS SOFTWARE LICENSE. Fraunhofer provides no warranty of patent non-infringement with
57 respect to this software.
58 
59 You may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized
60 by appropriate patent licenses.
61 
62 4.    DISCLAIMER
63 
64 This 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
66 of merchantability and fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
67 CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary, or consequential damages,
68 including but not limited to procurement of substitute goods or services; loss of use, data, or profits,
69 or business interruption, however caused and on any theory of liability, whether in contract, strict
70 liability, or tort (including negligence), arising in any way out of the use of this software, even if
71 advised of the possibility of such damage.
72 
73 5.    CONTACT INFORMATION
74 
75 Fraunhofer Institute for Integrated Circuits IIS
76 Attention: Audio and Multimedia Departments - FDK AAC LL
77 Am Wolfsmantel 33
78 91058 Erlangen, Germany
79 
80 www.iis.fraunhofer.de/amm
81 amm-info@iis.fraunhofer.de
82 ----------------------------------------------------------------------------------------------------------- */
83 
84 /*!
85   \file
86   \brief  Envelope extraction prototypes
87 */
88 
89 #ifndef __ENVELOPE_EXTRACTION_H
90 #define __ENVELOPE_EXTRACTION_H
91 
92 #include "sbrdecoder.h"
93 
94 #include "FDK_bitstream.h"
95 #include "lpp_tran.h"
96 
97 #include "psdec.h"
98 
99 #define ENV_EXP_FRACT 0
100 /*!< Shift raw envelope data to support fractional numbers.
101   Can be set to 8 instead of 0 to enhance accuracy during concealment.
102   This is not required for conformance and #requantizeEnvelopeData() will
103   become more expensive.
104 */
105 
106 #define EXP_BITS 6
107 /*!< Size of exponent-part of a pseudo float envelope value (should be at least 6).
108   The remaining bits in each word are used for the mantissa (should be at least 10).
109   This format is used in the arrays iEnvelope[] and sbrNoiseFloorLevel[]
110   in the FRAME_DATA struct which must fit in a certain part of the output buffer
111   (See buffer management in sbr_dec.cpp).
112   Exponents and mantissas could also be stored in separate arrays.
113   Accessing the exponent or the mantissa would be simplified and the masks #MASK_E
114   resp. #MASK_M would   no longer be required.
115 */
116 
117 #define MASK_M (((1 << (FRACT_BITS - EXP_BITS)) - 1) << EXP_BITS)  /*!< Mask for extracting the mantissa of a pseudo float envelope value */
118 #define MASK_E ((1 << EXP_BITS) - 1)           /*!< Mask for extracting the exponent of a pseudo float envelope value */
119 
120 #define SIGN_EXT ( ((SCHAR)-1) ^ MASK_E)        /*!< a CHAR-constant with all bits above our sign-bit set */
121 #define ROUNDING ( (FIXP_SGL)(1<<(EXP_BITS-1)) ) /*!< 0.5-offset for rounding the mantissa of a pseudo-float envelope value */
122 #define NRG_EXP_OFFSET  16                     /*!< Will be added to the reference energy's exponent to prevent negative numbers */
123 #define NOISE_EXP_OFFSET  38                   /*!< Will be added to the noise level exponent to prevent negative numbers */
124 
125 typedef enum
126 {
127   HEADER_NOT_PRESENT,
128   HEADER_ERROR,
129   HEADER_OK,
130   HEADER_RESET
131 }
132 SBR_HEADER_STATUS;
133 
134 typedef enum
135 {
136   SBR_NOT_INITIALIZED = 0,
137   UPSAMPLING = 1,
138   SBR_HEADER = 2,
139   SBR_ACTIVE = 3
140 }
141 SBR_SYNC_STATE;
142 
143 
144 typedef enum
145 {
146   COUPLING_OFF = 0,
147   COUPLING_LEVEL,
148   COUPLING_BAL
149 }
150 COUPLING_MODE;
151 
152 typedef struct
153 {
154   UCHAR nSfb[2];           /*!< Number of SBR-bands for low and high freq-resolution */
155   UCHAR nNfb;              /*!< Actual number of noise bands to read from the bitstream*/
156   UCHAR numMaster;         /*!< Number of SBR-bands in v_k_master */
157   UCHAR lowSubband;        /*!< QMF-band where SBR frequency range starts */
158   UCHAR highSubband;       /*!< QMF-band where SBR frequency range ends */
159   UCHAR limiterBandTable[MAX_NUM_LIMITERS+1]; /*!< Limiter band table. */
160   UCHAR noLimiterBands;    /*!< Number of limiter bands. */
161   UCHAR nInvfBands;        /*!< Number of bands for inverse filtering */
162   UCHAR *freqBandTable[2]; /*!< Pointers to freqBandTableLo and freqBandTableHi */
163   UCHAR freqBandTableLo[MAX_FREQ_COEFFS/2+1];
164                                    /*!< Mapping of SBR bands to QMF bands for low frequency resolution */
165   UCHAR freqBandTableHi[MAX_FREQ_COEFFS+1];
166                                    /*!< Mapping of SBR bands to QMF bands for high frequency resolution */
167   UCHAR freqBandTableNoise[MAX_NOISE_COEFFS+1];
168                                    /*!< Mapping of SBR noise bands to QMF bands */
169   UCHAR v_k_master[MAX_FREQ_COEFFS+1];
170                                    /*!< Master BandTable which freqBandTable is derived from */
171 }
172 FREQ_BAND_DATA;
173 
174 typedef FREQ_BAND_DATA *HANDLE_FREQ_BAND_DATA;
175 
176 #define SBRDEC_ELD_GRID        1
177 #define SBRDEC_SYNTAX_SCAL     2
178 #define SBRDEC_SYNTAX_USAC     4
179 #define SBRDEC_SYNTAX_RSVD50   8
180 #define SBRDEC_LOW_POWER      16  /* Flag indicating that Low Power QMF mode shall be used. */
181 #define SBRDEC_PS_DECODED     32  /* Flag indicating that PS was decoded and rendered. */
182 #define SBRDEC_LD_MPS_QMF    512  /* Flag indicating that the LD-MPS QMF shall be used. */
183 #define SBRDEC_SYNTAX_DRM   2048  /* Flag indicating that DRM30/DRM+ reverse syntax is being used. */
184 #define SBRDEC_DOWNSAMPLE   8192  /* Flag indicating that the downsampling mode is used. */
185 #define SBRDEC_FLUSH       16384  /* Flag is used to flush all elements in use. */
186 #define SBRDEC_FORCE_RESET 32768  /* Flag is used to force a reset of all elements in use. */
187 
188 #define SBRDEC_HDR_STAT_RESET  1
189 #define SBRDEC_HDR_STAT_UPDATE 2
190 
191 typedef struct {
192   UCHAR ampResolution;       /*!< Amplitude resolution of envelope values (0: 1.5dB, 1: 3dB) */
193   UCHAR xover_band;          /*!< Start index in #v_k_master[] used for dynamic crossover frequency */
194   UCHAR sbr_preprocessing;   /*!< SBR prewhitening flag. */
195 } SBR_HEADER_DATA_BS_INFO;
196 
197 typedef struct {
198   /* Changes in these variables causes a reset of the decoder */
199   UCHAR startFreq;           /*!< Index for SBR start frequency */
200   UCHAR stopFreq;            /*!< Index for SBR highest frequency */
201   UCHAR freqScale;           /*!< 0: linear scale,  1-3 logarithmic scales */
202   UCHAR alterScale;          /*!< Flag for coarser frequency resolution */
203   UCHAR noise_bands;         /*!< Noise bands per octave, read from bitstream*/
204 
205   /* don't require reset */
206   UCHAR limiterBands;        /*!< Index for number of limiter bands per octave */
207   UCHAR limiterGains;        /*!< Index to select gain limit */
208   UCHAR interpolFreq;        /*!< Select gain calculation method (1: per QMF channel, 0: per SBR band) */
209   UCHAR smoothingLength;     /*!< Smoothing of gains over time (0: on  1: off) */
210 
211 } SBR_HEADER_DATA_BS;
212 
213 typedef struct
214 {
215   SBR_SYNC_STATE syncState;    /*!< The current initialization status of the header */
216 
217   UCHAR status;                /*!< Flags field used for signaling a reset right before the processing starts and an update from config (e.g. ASC). */
218   UCHAR frameErrorFlag;        /*!< Frame data valid flag. CAUTION: This variable will be overwritten by the flag stored in the element structure.
219                                     This is necessary because of the frame delay. There it might happen that different slots use the same header. */
220   UCHAR numberTimeSlots;       /*!< AAC: 16,15 */
221   UCHAR numberOfAnalysisBands; /*!< Number of QMF analysis bands */
222   UCHAR timeStep;              /*!< Time resolution of SBR in QMF-slots */
223   UINT  sbrProcSmplRate;       /*!< SBR processing sampling frequency (!= OutputSamplingRate)
224                                      (always: CoreSamplingRate * UpSamplingFactor; even in single rate mode) */
225 
226   SBR_HEADER_DATA_BS      bs_data;  /*!< current SBR header. */
227   SBR_HEADER_DATA_BS_INFO bs_info;  /*!< SBR info. */
228 
229   FREQ_BAND_DATA freqBandData;  /*!< Pointer to struct #FREQ_BAND_DATA */
230 }
231 SBR_HEADER_DATA;
232 
233 typedef SBR_HEADER_DATA *HANDLE_SBR_HEADER_DATA;
234 
235 
236 typedef struct
237 {
238   UCHAR frameClass;               /*!< Select grid type */
239   UCHAR nEnvelopes;               /*!< Number of envelopes */
240   UCHAR borders[MAX_ENVELOPES+1]; /*!< Envelope borders (in SBR-timeslots, e.g. mp3PRO: 0..11) */
241   UCHAR freqRes[MAX_ENVELOPES];   /*!< Frequency resolution for each envelope (0=low, 1=high) */
242   SCHAR  tranEnv;                 /*!< Transient envelope, -1 if none */
243   UCHAR nNoiseEnvelopes;          /*!< Number of noise envelopes */
244   UCHAR bordersNoise[MAX_NOISE_ENVELOPES+1];/*!< borders of noise envelopes */
245 }
246 FRAME_INFO;
247 
248 
249 typedef struct
250 {
251   FIXP_SGL sfb_nrg_prev[MAX_FREQ_COEFFS];    /*!< Previous envelope (required for differential-coded values) */
252   FIXP_SGL prevNoiseLevel[MAX_NOISE_COEFFS]; /*!< Previous noise envelope (required for differential-coded values) */
253   COUPLING_MODE coupling;                    /*!< Stereo-mode of previous frame */
254   INVF_MODE sbr_invf_mode[MAX_INVF_BANDS];   /*!< Previous strength of filtering in transposer */
255   UCHAR ampRes;                              /*!< Previous amplitude resolution (0: 1.5dB, 1: 3dB) */
256   UCHAR stopPos;                             /*!< Position in time where last envelope ended */
257   UCHAR frameErrorFlag;                      /*!< Previous frame status */
258 }
259 SBR_PREV_FRAME_DATA;
260 
261 typedef SBR_PREV_FRAME_DATA *HANDLE_SBR_PREV_FRAME_DATA;
262 
263 
264 typedef struct
265 {
266   int nScaleFactors;                    /*!< total number of scalefactors in frame */
267 
268   FRAME_INFO frameInfo;                 /*!< time grid for current frame */
269   UCHAR domain_vec[MAX_ENVELOPES];      /*!< Bitfield containing direction of delta-coding for each envelope (0:frequency, 1:time) */
270   UCHAR domain_vec_noise[MAX_NOISE_ENVELOPES]; /*!< Same as above, but for noise envelopes */
271 
272   INVF_MODE sbr_invf_mode[MAX_INVF_BANDS]; /*!< Strength of filtering in transposer */
273   COUPLING_MODE coupling;                  /*!< Stereo-mode */
274   int ampResolutionCurrentFrame;           /*!< Amplitude resolution of envelope values (0: 1.5dB, 1: 3dB) */
275 
276   UCHAR addHarmonics[MAX_FREQ_COEFFS];     /*!< Flags for synthetic sine addition */
277 
278   FIXP_SGL iEnvelope[MAX_NUM_ENVELOPE_VALUES];       /*!< Envelope data */
279   FIXP_SGL sbrNoiseFloorLevel[MAX_NUM_NOISE_VALUES]; /*!< Noise envelope data */
280 }
281 SBR_FRAME_DATA;
282 
283 typedef SBR_FRAME_DATA *HANDLE_SBR_FRAME_DATA;
284 
285 void initSbrPrevFrameData (HANDLE_SBR_PREV_FRAME_DATA h_prev_data,
286                            int timeSlots);
287 
288 
289 int sbrGetSingleChannelElement (HANDLE_SBR_HEADER_DATA hHeaderData,
290                                 HANDLE_SBR_FRAME_DATA  hFrameData,
291                                 HANDLE_FDK_BITSTREAM   hBitBuf,
292                                 HANDLE_PS_DEC hParametricStereoDec,
293                                 const UINT             flags,
294                                 const int              overlap
295                                );
296 
297 int sbrGetChannelPairElement (HANDLE_SBR_HEADER_DATA hHeaderData,
298                               HANDLE_SBR_FRAME_DATA  hFrameDataLeft,
299                               HANDLE_SBR_FRAME_DATA  hFrameDataRight,
300                               HANDLE_FDK_BITSTREAM   hBitBuf,
301                               const UINT             flags,
302                               const int              overlap);
303 
304 SBR_HEADER_STATUS
305 sbrGetHeaderData (HANDLE_SBR_HEADER_DATA headerData,
306                   HANDLE_FDK_BITSTREAM   hBitBuf,
307                   const UINT             flags,
308                   const int              fIsSbrData);
309 
310 /*!
311   \brief     Initialize SBR header data
312 
313   Copy default values to the header data struct and patch some entries
314   depending on the core codec.
315 */
316 SBR_ERROR
317 initHeaderData (
318         HANDLE_SBR_HEADER_DATA  hHeaderData,
319         const int               sampleRateIn,
320         const int               sampleRateOut,
321         const int               samplesPerFrame,
322         const UINT              flags
323         );
324 #endif
325