1 
2 /* -----------------------------------------------------------------------------------------------------------
3 Software 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
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  Sbr decoder
87 */
88 #ifndef __PSDEC_H
89 #define __PSDEC_H
90 
91 #include "sbrdecoder.h"
92 
93 
94 
95 /* This PS decoder implements the baseline version. So it always uses the     */
96 /* hybrid filter structure for 20 stereo bands and does not implemet IPD/OPD  */
97 /* synthesis. The baseline version has to support the complete PS bitstream   */
98 /* syntax. But IPD/OPD data is ignored and set to 0. If 34 stereo band config */
99 /* is used in the bitstream for IIS/ICC the decoded parameters are mapped to  */
100 /* 20 stereo bands.                                                           */
101 
102 
103 #include "FDK_bitstream.h"
104 
105 #include "psdec_hybrid.h"
106 
107 #define SCAL_HEADROOM                     ( 2 )
108 
109 #define PS_EXTENSION_SIZE_BITS            ( 4 )
110 #define PS_EXTENSION_ESC_COUNT_BITS       ( 8 )
111 
112 #define NO_QMF_CHANNELS                   ( 64 )
113 #define MAX_NUM_COL                       ( 32 )
114 
115 
116   #define NO_QMF_BANDS_HYBRID20           ( 3  )
117   #define NO_SUB_QMF_CHANNELS             ( 12 )
118 
119   #define NRG_INT_COEFF                   ( 0.75f )
120   #define INT_FILTER_COEFF                (FL2FXCONST_DBL( 1.0f - NRG_INT_COEFF ))
121   #define PEAK_DECAY_FACTOR               (FL2FXCONST_DBL( 0.765928338364649f ))
122   #define TRANSIENT_IMPACT_FACTOR         (FL2FXCONST_DBL( 2.0 / 3.0 ))
123 
124   #define NO_SERIAL_ALLPASS_LINKS         ( 3 )
125   #define MAX_NO_PS_ENV                   ( 4 + 1 )   /* +1 needed for VAR_BORDER */
126 
127   #define MAX_DELAY_BUFFER_SIZE           ( 14 )
128   #define NO_DELAY_BUFFER_BANDS           ( 35 )
129 
130   #define NO_HI_RES_BINS                  ( 34 )
131   #define NO_MID_RES_BINS                 ( 20 )
132   #define NO_LOW_RES_BINS                 ( 10 )
133 
134   #define FIRST_DELAY_SB                  ( 23 )
135   #define NO_SAMPLE_DELAY_ALLPASS         ( 2 )
136   #define NO_DELAY_LENGTH_VECTORS         ( 12 )     /* d(m): d(0)=3 + d(1)=4 + d(2)=5 */
137 
138   #define NO_HI_RES_IID_BINS              ( NO_HI_RES_BINS )
139   #define NO_HI_RES_ICC_BINS              ( NO_HI_RES_BINS )
140 
141   #define NO_MID_RES_IID_BINS             ( NO_MID_RES_BINS )
142   #define NO_MID_RES_ICC_BINS             ( NO_MID_RES_BINS )
143 
144   #define NO_LOW_RES_IID_BINS             ( NO_LOW_RES_BINS )
145   #define NO_LOW_RES_ICC_BINS             ( NO_LOW_RES_BINS )
146 
147   #define SUBQMF_GROUPS                   ( 10 )
148   #define QMF_GROUPS                      ( 12 )
149 
150   #define SUBQMF_GROUPS_HI_RES            ( 32 )
151   #define QMF_GROUPS_HI_RES               ( 18 )
152 
153   #define NO_IID_GROUPS                   ( SUBQMF_GROUPS + QMF_GROUPS )
154   #define NO_IID_GROUPS_HI_RES            ( SUBQMF_GROUPS_HI_RES + QMF_GROUPS_HI_RES )
155 
156   #define NO_IID_STEPS                    ( 7 )  /* 1 .. + 7 */
157   #define NO_IID_STEPS_FINE               ( 15 ) /* 1 .. +15 */
158   #define NO_ICC_STEPS                    ( 8 )  /* 0 .. + 7 */
159 
160   #define NO_IID_LEVELS                   ( 2 * NO_IID_STEPS + 1 )      /* - 7 ..  + 7 */
161   #define NO_IID_LEVELS_FINE              ( 2 * NO_IID_STEPS_FINE + 1 ) /* -15 ..  +15 */
162   #define NO_ICC_LEVELS                   ( NO_ICC_STEPS )              /*   0 ..  + 7 */
163 
164   #define FIXP_SQRT05                     ((FIXP_DBL)0x5a827980) /* 1/SQRT2 */
165 
166   struct PS_DEC_COEFFICIENTS {
167 
168     FIXP_DBL H11r[NO_IID_GROUPS];                       /*!< coefficients of the sub-subband groups                               */
169     FIXP_DBL H12r[NO_IID_GROUPS];                       /*!< coefficients of the sub-subband groups                               */
170     FIXP_DBL H21r[NO_IID_GROUPS];                       /*!< coefficients of the sub-subband groups                               */
171     FIXP_DBL H22r[NO_IID_GROUPS];                       /*!< coefficients of the sub-subband groups                               */
172 
173     FIXP_DBL DeltaH11r[NO_IID_GROUPS];                  /*!< coefficients of the sub-subband groups                               */
174     FIXP_DBL DeltaH12r[NO_IID_GROUPS];                  /*!< coefficients of the sub-subband groups                               */
175     FIXP_DBL DeltaH21r[NO_IID_GROUPS];                  /*!< coefficients of the sub-subband groups                               */
176     FIXP_DBL DeltaH22r[NO_IID_GROUPS];                  /*!< coefficients of the sub-subband groups                               */
177 
178     SCHAR aaIidIndexMapped[MAX_NO_PS_ENV][NO_HI_RES_IID_BINS];       /*!< The mapped IID index for all envelopes and all IID bins */
179     SCHAR aaIccIndexMapped[MAX_NO_PS_ENV][NO_HI_RES_ICC_BINS];       /*!< The mapped ICC index for all envelopes and all ICC bins */
180 
181   };
182 
183 
184 
185 
186 typedef enum {
187   ppt_none = 0,
188   ppt_mpeg = 1,
189   ppt_drm  = 2
190 } PS_PAYLOAD_TYPE;
191 
192 
193 typedef struct {
194   UCHAR bPsHeaderValid;                                      /*!< set if new header is available from bitstream                 */
195 
196   UCHAR bEnableIid;                                          /*!< One bit denoting the presence of IID parameters               */
197   UCHAR bEnableIcc;                                          /*!< One bit denoting the presence of ICC parameters               */
198   UCHAR bEnableExt;                                          /*!< The PS extension layer is enabled using the enable_ext bit.
199                                                                   If it is set to %1 the IPD and OPD parameters are sent.
200                                                                   If it is disabled, i.e. %0, the extension layer is skipped.   */
201 
202   UCHAR modeIid;                                             /*!< The configuration of IID parameters (number of bands and
203                                                                   quantisation grid, iid_quant) is determined by iid_mode.      */
204   UCHAR modeIcc;                                             /*!< The configuration of Inter-channel Coherence parameters
205                                                                   (number of bands and quantisation grid) is determined by
206                                                                   icc_mode.                                                     */
207 
208   UCHAR freqResIid;                                          /*!< 0=low, 1=mid or 2=high frequency resolution for iid           */
209   UCHAR freqResIcc;                                          /*!< 0=low, 1=mid or 2=high frequency resolution for icc           */
210 
211   UCHAR bFineIidQ;                                           /*!< Use fine Iid quantisation.                                    */
212 
213   UCHAR bFrameClass;                                         /*!< The frame_class bit determines whether the parameter
214                                                                   positions of the current frame are uniformly spaced
215                                                                   accross the frame or they are defined using the positions
216                                                                   described by border_position.                                 */
217 
218   UCHAR noEnv;                                               /*!< The number of envelopes per frame                             */
219   UCHAR aEnvStartStop[MAX_NO_PS_ENV+1];                      /*!< In case of variable parameter spacing the parameter
220                                                                   positions are determined by border_position                   */
221 
222   SCHAR abIidDtFlag[MAX_NO_PS_ENV];                                /*!< Deltacoding time/freq flag for IID, 0 => freq           */
223   SCHAR abIccDtFlag[MAX_NO_PS_ENV];                                /*!< Deltacoding time/freq flag for ICC, 0 => freq           */
224 
225   SCHAR aaIidIndex[MAX_NO_PS_ENV][NO_HI_RES_IID_BINS];             /*!< The IID index for all envelopes and all IID bins        */
226   SCHAR aaIccIndex[MAX_NO_PS_ENV][NO_HI_RES_ICC_BINS];             /*!< The ICC index for all envelopes and all ICC bins        */
227 
228 } MPEG_PS_BS_DATA;
229 
230 
231 
232 struct PS_DEC {
233 
234   SCHAR noSubSamples;
235   SCHAR noChannels;
236 
237   SCHAR procFrameBased;                                      /*!< Helper to detected switching from frame based to slot based
238                                                                   processing                                                    */
239 
240   PS_PAYLOAD_TYPE bPsDataAvail[(1)+1];   /*!< set if new data available from bitstream                      */
241   UCHAR psDecodedPrv;                                        /*!< set if PS has been processed in the last frame                */
242 
243   /* helpers for frame delay line */
244   UCHAR bsLastSlot;                                          /*!< Index of last read slot.                                      */
245   UCHAR bsReadSlot;                                          /*!< Index of current read slot for additional delay.              */
246   UCHAR processSlot;                                         /*!< Index of current slot for processing (need for add. delay).   */
247 
248 
249   INT  rescal;
250   INT  sf_IntBuffer;
251 
252   union {  /* Bitstream data */
253     MPEG_PS_BS_DATA  mpeg;                                   /*!< Struct containing all MPEG specific PS data from bitstream.   */
254   } bsData[(1)+1];
255 
256   shouldBeUnion {  /* Static data */
257     struct {
258       SCHAR aIidPrevFrameIndex[NO_HI_RES_IID_BINS];          /*!< The IID index for previous frame                              */
259       SCHAR aIccPrevFrameIndex[NO_HI_RES_ICC_BINS];          /*!< The ICC index for previous frame                              */
260 
261       UCHAR delayBufIndex;                                         /*!< Pointer to where the latest sample is in buffer         */
262       UCHAR noSampleDelay;                                         /*!< How many QMF samples delay is used.                     */
263       UCHAR lastUsb;                                               /*!< uppermost WMF delay band of last frame                  */
264 
265       UCHAR aDelayRBufIndexSer[NO_SERIAL_ALLPASS_LINKS];             /*!< Delay buffer for reverb filter                        */
266       UCHAR aDelayBufIndexDelayQmf[NO_QMF_CHANNELS-FIRST_DELAY_SB];  /*!< Delay buffer for ICC group 20 & 21                    */
267 
268       SCHAR scaleFactorPsDelayBuffer;                                /*!< Scale factor for ps delay buffer                      */
269 
270       /* hybrid filter bank delay lines */
271       FIXP_DBL aaQmfDelayBufReal[(NO_QMF_CHANNELS-FIRST_DELAY_SB) + (MAX_DELAY_BUFFER_SIZE-1)*(NO_DELAY_BUFFER_BANDS-FIRST_DELAY_SB)];
272       FIXP_DBL aaQmfDelayBufImag[(NO_QMF_CHANNELS-FIRST_DELAY_SB) + (MAX_DELAY_BUFFER_SIZE-1)*(NO_DELAY_BUFFER_BANDS-FIRST_DELAY_SB)];
273 
274       FIXP_DBL *pAaRealDelayBufferQmf[MAX_DELAY_BUFFER_SIZE];          /*!< Real part delay buffer                                  */
275       FIXP_DBL *pAaImagDelayBufferQmf[MAX_DELAY_BUFFER_SIZE];          /*!< Imaginary part delay buffer                             */
276 
277       FIXP_DBL aaRealDelayBufferQmf[NO_SAMPLE_DELAY_ALLPASS][FIRST_DELAY_SB]; /*!< Real part delay buffer     */
278       FIXP_DBL aaImagDelayBufferQmf[NO_SAMPLE_DELAY_ALLPASS][FIRST_DELAY_SB]; /*!< Imaginary part delay buffer*/
279 
280       FIXP_DBL aaRealDelayBufferSubQmf[NO_SAMPLE_DELAY_ALLPASS][NO_SUB_QMF_CHANNELS];          /*!< Real part delay buffer          */
281       FIXP_DBL aaImagDelayBufferSubQmf[NO_SAMPLE_DELAY_ALLPASS][NO_SUB_QMF_CHANNELS];          /*!< Imaginary part delay buffer     */
282 
283       FIXP_DBL aaaRealDelayRBufferSerQmf[FIRST_DELAY_SB][NO_DELAY_LENGTH_VECTORS];             /*!< Real part delay buffer          */
284       FIXP_DBL aaaImagDelayRBufferSerQmf[FIRST_DELAY_SB][NO_DELAY_LENGTH_VECTORS];             /*!< Imaginary part delay buffer     */
285 
286       FIXP_DBL aaaRealDelayRBufferSerSubQmf[NO_SUB_QMF_CHANNELS][NO_DELAY_LENGTH_VECTORS];     /*!< Real part delay buffer          */
287       FIXP_DBL aaaImagDelayRBufferSerSubQmf[NO_SUB_QMF_CHANNELS][NO_DELAY_LENGTH_VECTORS];     /*!< Imaginary part delay buffer     */
288 
289       HYBRID hybrid;                                      /*!< hybrid filter bank struct 1 or 2.                          */
290 
291       FIXP_DBL aPrevNrgBin[NO_MID_RES_BINS];              /*!< energy of previous frame                                             */
292       FIXP_DBL aPrevPeakDiffBin[NO_MID_RES_BINS];         /*!< peak difference of previous frame                                    */
293       FIXP_DBL aPeakDecayFastBin[NO_MID_RES_BINS];        /*!< Saved max. peak decay value per bin                                  */
294       SCHAR aPowerPrevScal[NO_MID_RES_BINS];              /*!< Last power value (each bin) of previous frame                        */
295 
296       FIXP_DBL h11rPrev[NO_IID_GROUPS];                   /*!< previous calculated h(xy) coefficients                               */
297       FIXP_DBL h12rPrev[NO_IID_GROUPS];                   /*!< previous calculated h(xy) coefficients                               */
298       FIXP_DBL h21rPrev[NO_IID_GROUPS];                   /*!< previous calculated h(xy) coefficients                               */
299       FIXP_DBL h22rPrev[NO_IID_GROUPS];                   /*!< previous calculated h(xy) coefficients                               */
300 
301       PS_DEC_COEFFICIENTS  coef;                         /*!< temporal coefficients (reusable scratch memory)                 */
302 
303     } mpeg;
304 
305   } specificTo;
306 
307 
308 };
309 
310 typedef struct PS_DEC *HANDLE_PS_DEC;
311 
312 
313 int CreatePsDec(HANDLE_PS_DEC *h_PS_DEC, int aacSamplesPerFrame);
314 
315 int DeletePsDec(HANDLE_PS_DEC *h_PS_DEC);
316 
317 void
318 scalFilterBankValues( HANDLE_PS_DEC   h_ps_d,                        /* parametric stereo decoder handle     */
319                       FIXP_DBL      **fixpQmfReal,                   /* qmf filterbank values                */
320                       FIXP_DBL      **fixpQmfImag,                   /* qmf filterbank values                */
321                       int             lsb,                           /* sbr start subband                    */
322                       int             scaleFactorLowBandSplitLow,
323                       int             scaleFactorLowBandSplitHigh,
324                       SCHAR          *scaleFactorLowBand_lb,
325                       SCHAR          *scaleFactorLowBand_hb,
326                       int             scaleFactorHighBands,
327                       INT            *scaleFactorHighBand,
328                       INT             noCols);
329 
330 void
331 rescalFilterBankValues( HANDLE_PS_DEC   h_ps_d,                      /* parametric stereo decoder handle     */
332                         FIXP_DBL      **QmfBufferReal,               /* qmf filterbank values                */
333                         FIXP_DBL      **QmfBufferImag,               /* qmf filterbank values                */
334                         int             lsb,                         /* sbr start subband                    */
335                         INT             noCols);
336 
337 
338 void
339 initSlotBasedRotation( HANDLE_PS_DEC h_ps_d,
340                        int env,
341                        int usb);
342 
343 void
344 ApplyPsSlot( HANDLE_PS_DEC h_ps_d,            /* parametric stereo decoder handle    */
345              FIXP_DBL **rIntBufferLeft,       /* real values of left qmf timeslot    */
346              FIXP_DBL **iIntBufferLeft,       /* imag values of left qmf timeslot    */
347              FIXP_DBL *rIntBufferRight,       /* real values of right qmf timeslot   */
348              FIXP_DBL *iIntBufferRight);      /* imag values of right qmf timeslot   */
349 
350 
351 
352 #endif  /* __PSDEC_H */
353