xref: /aosp_15_r20/external/aac/libAACdec/src/aacdec_pns.cpp (revision e54365361535b070c2db7374cec45c159c7d0e7a)
1*e5436536SAndroid Build Coastguard Worker /* -----------------------------------------------------------------------------
2*e5436536SAndroid Build Coastguard Worker Software License for The Fraunhofer FDK AAC Codec Library for Android
3*e5436536SAndroid Build Coastguard Worker 
4*e5436536SAndroid Build Coastguard Worker © Copyright  1995 - 2018 Fraunhofer-Gesellschaft zur Förderung der angewandten
5*e5436536SAndroid Build Coastguard Worker Forschung e.V. All rights reserved.
6*e5436536SAndroid Build Coastguard Worker 
7*e5436536SAndroid Build Coastguard Worker  1.    INTRODUCTION
8*e5436536SAndroid Build Coastguard Worker The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software
9*e5436536SAndroid Build Coastguard Worker that implements the MPEG Advanced Audio Coding ("AAC") encoding and decoding
10*e5436536SAndroid Build Coastguard Worker scheme for digital audio. This FDK AAC Codec software is intended to be used on
11*e5436536SAndroid Build Coastguard Worker a wide variety of Android devices.
12*e5436536SAndroid Build Coastguard Worker 
13*e5436536SAndroid Build Coastguard Worker AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient
14*e5436536SAndroid Build Coastguard Worker general perceptual audio codecs. AAC-ELD is considered the best-performing
15*e5436536SAndroid Build Coastguard Worker full-bandwidth communications codec by independent studies and is widely
16*e5436536SAndroid Build Coastguard Worker deployed. AAC has been standardized by ISO and IEC as part of the MPEG
17*e5436536SAndroid Build Coastguard Worker specifications.
18*e5436536SAndroid Build Coastguard Worker 
19*e5436536SAndroid Build Coastguard Worker Patent licenses for necessary patent claims for the FDK AAC Codec (including
20*e5436536SAndroid Build Coastguard Worker those of Fraunhofer) may be obtained through Via Licensing
21*e5436536SAndroid Build Coastguard Worker (www.vialicensing.com) or through the respective patent owners individually for
22*e5436536SAndroid Build Coastguard Worker the purpose of encoding or decoding bit streams in products that are compliant
23*e5436536SAndroid Build Coastguard Worker with the ISO/IEC MPEG audio standards. Please note that most manufacturers of
24*e5436536SAndroid Build Coastguard Worker Android devices already license these patent claims through Via Licensing or
25*e5436536SAndroid Build Coastguard Worker directly from the patent owners, and therefore FDK AAC Codec software may
26*e5436536SAndroid Build Coastguard Worker already be covered under those patent licenses when it is used for those
27*e5436536SAndroid Build Coastguard Worker licensed purposes only.
28*e5436536SAndroid Build Coastguard Worker 
29*e5436536SAndroid Build Coastguard Worker Commercially-licensed AAC software libraries, including floating-point versions
30*e5436536SAndroid Build Coastguard Worker with enhanced sound quality, are also available from Fraunhofer. Users are
31*e5436536SAndroid Build Coastguard Worker encouraged to check the Fraunhofer website for additional applications
32*e5436536SAndroid Build Coastguard Worker information and documentation.
33*e5436536SAndroid Build Coastguard Worker 
34*e5436536SAndroid Build Coastguard Worker 2.    COPYRIGHT LICENSE
35*e5436536SAndroid Build Coastguard Worker 
36*e5436536SAndroid Build Coastguard Worker Redistribution and use in source and binary forms, with or without modification,
37*e5436536SAndroid Build Coastguard Worker are permitted without payment of copyright license fees provided that you
38*e5436536SAndroid Build Coastguard Worker satisfy the following conditions:
39*e5436536SAndroid Build Coastguard Worker 
40*e5436536SAndroid Build Coastguard Worker You must retain the complete text of this software license in redistributions of
41*e5436536SAndroid Build Coastguard Worker the FDK AAC Codec or your modifications thereto in source code form.
42*e5436536SAndroid Build Coastguard Worker 
43*e5436536SAndroid Build Coastguard Worker You must retain the complete text of this software license in the documentation
44*e5436536SAndroid Build Coastguard Worker and/or other materials provided with redistributions of the FDK AAC Codec or
45*e5436536SAndroid Build Coastguard Worker your modifications thereto in binary form. You must make available free of
46*e5436536SAndroid Build Coastguard Worker charge copies of the complete source code of the FDK AAC Codec and your
47*e5436536SAndroid Build Coastguard Worker modifications thereto to recipients of copies in binary form.
48*e5436536SAndroid Build Coastguard Worker 
49*e5436536SAndroid Build Coastguard Worker The name of Fraunhofer may not be used to endorse or promote products derived
50*e5436536SAndroid Build Coastguard Worker from this library without prior written permission.
51*e5436536SAndroid Build Coastguard Worker 
52*e5436536SAndroid Build Coastguard Worker You may not charge copyright license fees for anyone to use, copy or distribute
53*e5436536SAndroid Build Coastguard Worker the FDK AAC Codec software or your modifications thereto.
54*e5436536SAndroid Build Coastguard Worker 
55*e5436536SAndroid Build Coastguard Worker Your modified versions of the FDK AAC Codec must carry prominent notices stating
56*e5436536SAndroid Build Coastguard Worker that you changed the software and the date of any change. For modified versions
57*e5436536SAndroid Build Coastguard Worker of the FDK AAC Codec, the term "Fraunhofer FDK AAC Codec Library for Android"
58*e5436536SAndroid Build Coastguard Worker must be replaced by the term "Third-Party Modified Version of the Fraunhofer FDK
59*e5436536SAndroid Build Coastguard Worker AAC Codec Library for Android."
60*e5436536SAndroid Build Coastguard Worker 
61*e5436536SAndroid Build Coastguard Worker 3.    NO PATENT LICENSE
62*e5436536SAndroid Build Coastguard Worker 
63*e5436536SAndroid Build Coastguard Worker NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without
64*e5436536SAndroid Build Coastguard Worker limitation the patents of Fraunhofer, ARE GRANTED BY THIS SOFTWARE LICENSE.
65*e5436536SAndroid Build Coastguard Worker Fraunhofer provides no warranty of patent non-infringement with respect to this
66*e5436536SAndroid Build Coastguard Worker software.
67*e5436536SAndroid Build Coastguard Worker 
68*e5436536SAndroid Build Coastguard Worker You may use this FDK AAC Codec software or modifications thereto only for
69*e5436536SAndroid Build Coastguard Worker purposes that are authorized by appropriate patent licenses.
70*e5436536SAndroid Build Coastguard Worker 
71*e5436536SAndroid Build Coastguard Worker 4.    DISCLAIMER
72*e5436536SAndroid Build Coastguard Worker 
73*e5436536SAndroid Build Coastguard Worker This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright
74*e5436536SAndroid Build Coastguard Worker holders and contributors "AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES,
75*e5436536SAndroid Build Coastguard Worker including but not limited to the implied warranties of merchantability and
76*e5436536SAndroid Build Coastguard Worker fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
77*e5436536SAndroid Build Coastguard Worker CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary,
78*e5436536SAndroid Build Coastguard Worker or consequential damages, including but not limited to procurement of substitute
79*e5436536SAndroid Build Coastguard Worker goods or services; loss of use, data, or profits, or business interruption,
80*e5436536SAndroid Build Coastguard Worker however caused and on any theory of liability, whether in contract, strict
81*e5436536SAndroid Build Coastguard Worker liability, or tort (including negligence), arising in any way out of the use of
82*e5436536SAndroid Build Coastguard Worker this software, even if advised of the possibility of such damage.
83*e5436536SAndroid Build Coastguard Worker 
84*e5436536SAndroid Build Coastguard Worker 5.    CONTACT INFORMATION
85*e5436536SAndroid Build Coastguard Worker 
86*e5436536SAndroid Build Coastguard Worker Fraunhofer Institute for Integrated Circuits IIS
87*e5436536SAndroid Build Coastguard Worker Attention: Audio and Multimedia Departments - FDK AAC LL
88*e5436536SAndroid Build Coastguard Worker Am Wolfsmantel 33
89*e5436536SAndroid Build Coastguard Worker 91058 Erlangen, Germany
90*e5436536SAndroid Build Coastguard Worker 
91*e5436536SAndroid Build Coastguard Worker www.iis.fraunhofer.de/amm
92*e5436536SAndroid Build Coastguard Worker [email protected]
93*e5436536SAndroid Build Coastguard Worker ----------------------------------------------------------------------------- */
94*e5436536SAndroid Build Coastguard Worker 
95*e5436536SAndroid Build Coastguard Worker /**************************** AAC decoder library ******************************
96*e5436536SAndroid Build Coastguard Worker 
97*e5436536SAndroid Build Coastguard Worker    Author(s):   Josef Hoepfl
98*e5436536SAndroid Build Coastguard Worker 
99*e5436536SAndroid Build Coastguard Worker    Description: perceptual noise substitution tool
100*e5436536SAndroid Build Coastguard Worker 
101*e5436536SAndroid Build Coastguard Worker *******************************************************************************/
102*e5436536SAndroid Build Coastguard Worker 
103*e5436536SAndroid Build Coastguard Worker #include "aacdec_pns.h"
104*e5436536SAndroid Build Coastguard Worker 
105*e5436536SAndroid Build Coastguard Worker #include "aac_ram.h"
106*e5436536SAndroid Build Coastguard Worker #include "aac_rom.h"
107*e5436536SAndroid Build Coastguard Worker #include "channelinfo.h"
108*e5436536SAndroid Build Coastguard Worker #include "block.h"
109*e5436536SAndroid Build Coastguard Worker #include "FDK_bitstream.h"
110*e5436536SAndroid Build Coastguard Worker 
111*e5436536SAndroid Build Coastguard Worker #include "genericStds.h"
112*e5436536SAndroid Build Coastguard Worker 
113*e5436536SAndroid Build Coastguard Worker #define NOISE_OFFSET 90 /* cf. ISO 14496-3 p. 175 */
114*e5436536SAndroid Build Coastguard Worker 
115*e5436536SAndroid Build Coastguard Worker /*!
116*e5436536SAndroid Build Coastguard Worker   \brief Reset InterChannel and PNS data
117*e5436536SAndroid Build Coastguard Worker 
118*e5436536SAndroid Build Coastguard Worker   The function resets the InterChannel and PNS data
119*e5436536SAndroid Build Coastguard Worker */
CPns_ResetData(CPnsData * pPnsData,CPnsInterChannelData * pPnsInterChannelData)120*e5436536SAndroid Build Coastguard Worker void CPns_ResetData(CPnsData *pPnsData,
121*e5436536SAndroid Build Coastguard Worker                     CPnsInterChannelData *pPnsInterChannelData) {
122*e5436536SAndroid Build Coastguard Worker   FDK_ASSERT(pPnsData != NULL);
123*e5436536SAndroid Build Coastguard Worker   FDK_ASSERT(pPnsInterChannelData != NULL);
124*e5436536SAndroid Build Coastguard Worker   /* Assign pointer always, since pPnsData is not persistent data */
125*e5436536SAndroid Build Coastguard Worker   pPnsData->pPnsInterChannelData = pPnsInterChannelData;
126*e5436536SAndroid Build Coastguard Worker   pPnsData->PnsActive = 0;
127*e5436536SAndroid Build Coastguard Worker   pPnsData->CurrentEnergy = 0;
128*e5436536SAndroid Build Coastguard Worker 
129*e5436536SAndroid Build Coastguard Worker   FDKmemclear(pPnsData->pnsUsed, (8 * 16) * sizeof(UCHAR));
130*e5436536SAndroid Build Coastguard Worker   FDKmemclear(pPnsInterChannelData->correlated, (8 * 16) * sizeof(UCHAR));
131*e5436536SAndroid Build Coastguard Worker }
132*e5436536SAndroid Build Coastguard Worker 
133*e5436536SAndroid Build Coastguard Worker /*!
134*e5436536SAndroid Build Coastguard Worker   \brief Update PNS noise generator state.
135*e5436536SAndroid Build Coastguard Worker 
136*e5436536SAndroid Build Coastguard Worker   The function sets the seed for PNS noise generation.
137*e5436536SAndroid Build Coastguard Worker   It can be used to link two or more channels in terms of PNS.
138*e5436536SAndroid Build Coastguard Worker */
CPns_UpdateNoiseState(CPnsData * pPnsData,INT * currentSeed,INT * randomSeed)139*e5436536SAndroid Build Coastguard Worker void CPns_UpdateNoiseState(CPnsData *pPnsData, INT *currentSeed,
140*e5436536SAndroid Build Coastguard Worker                            INT *randomSeed) {
141*e5436536SAndroid Build Coastguard Worker   /* use pointer because seed has to be
142*e5436536SAndroid Build Coastguard Worker      same, left and right channel ! */
143*e5436536SAndroid Build Coastguard Worker   pPnsData->currentSeed = currentSeed;
144*e5436536SAndroid Build Coastguard Worker   pPnsData->randomSeed = randomSeed;
145*e5436536SAndroid Build Coastguard Worker }
146*e5436536SAndroid Build Coastguard Worker 
147*e5436536SAndroid Build Coastguard Worker /*!
148*e5436536SAndroid Build Coastguard Worker   \brief Indicates if PNS is used
149*e5436536SAndroid Build Coastguard Worker 
150*e5436536SAndroid Build Coastguard Worker   The function returns a value indicating whether PNS is used or not
151*e5436536SAndroid Build Coastguard Worker   acordding to the noise energy
152*e5436536SAndroid Build Coastguard Worker 
153*e5436536SAndroid Build Coastguard Worker   \return  PNS used
154*e5436536SAndroid Build Coastguard Worker */
CPns_IsPnsUsed(const CPnsData * pPnsData,const int group,const int band)155*e5436536SAndroid Build Coastguard Worker int CPns_IsPnsUsed(const CPnsData *pPnsData, const int group, const int band) {
156*e5436536SAndroid Build Coastguard Worker   unsigned pns_band = group * 16 + band;
157*e5436536SAndroid Build Coastguard Worker 
158*e5436536SAndroid Build Coastguard Worker   return pPnsData->pnsUsed[pns_band] & (UCHAR)1;
159*e5436536SAndroid Build Coastguard Worker }
160*e5436536SAndroid Build Coastguard Worker 
161*e5436536SAndroid Build Coastguard Worker /*!
162*e5436536SAndroid Build Coastguard Worker   \brief Set correlation
163*e5436536SAndroid Build Coastguard Worker 
164*e5436536SAndroid Build Coastguard Worker   The function activates the noise correlation between the channel pair
165*e5436536SAndroid Build Coastguard Worker */
CPns_SetCorrelation(CPnsData * pPnsData,const int group,const int band,const int outofphase)166*e5436536SAndroid Build Coastguard Worker void CPns_SetCorrelation(CPnsData *pPnsData, const int group, const int band,
167*e5436536SAndroid Build Coastguard Worker                          const int outofphase) {
168*e5436536SAndroid Build Coastguard Worker   CPnsInterChannelData *pInterChannelData = pPnsData->pPnsInterChannelData;
169*e5436536SAndroid Build Coastguard Worker   unsigned pns_band = group * 16 + band;
170*e5436536SAndroid Build Coastguard Worker 
171*e5436536SAndroid Build Coastguard Worker   pInterChannelData->correlated[pns_band] = (outofphase) ? 3 : 1;
172*e5436536SAndroid Build Coastguard Worker }
173*e5436536SAndroid Build Coastguard Worker 
174*e5436536SAndroid Build Coastguard Worker /*!
175*e5436536SAndroid Build Coastguard Worker   \brief Indicates if correlation is used
176*e5436536SAndroid Build Coastguard Worker 
177*e5436536SAndroid Build Coastguard Worker   The function indicates if the noise correlation between the channel pair
178*e5436536SAndroid Build Coastguard Worker   is activated
179*e5436536SAndroid Build Coastguard Worker 
180*e5436536SAndroid Build Coastguard Worker   \return  PNS is correlated
181*e5436536SAndroid Build Coastguard Worker */
CPns_IsCorrelated(const CPnsData * pPnsData,const int group,const int band)182*e5436536SAndroid Build Coastguard Worker static int CPns_IsCorrelated(const CPnsData *pPnsData, const int group,
183*e5436536SAndroid Build Coastguard Worker                              const int band) {
184*e5436536SAndroid Build Coastguard Worker   CPnsInterChannelData *pInterChannelData = pPnsData->pPnsInterChannelData;
185*e5436536SAndroid Build Coastguard Worker   unsigned pns_band = group * 16 + band;
186*e5436536SAndroid Build Coastguard Worker 
187*e5436536SAndroid Build Coastguard Worker   return (pInterChannelData->correlated[pns_band] & 0x01) ? 1 : 0;
188*e5436536SAndroid Build Coastguard Worker }
189*e5436536SAndroid Build Coastguard Worker 
190*e5436536SAndroid Build Coastguard Worker /*!
191*e5436536SAndroid Build Coastguard Worker   \brief Indicates if correlated out of phase mode is used.
192*e5436536SAndroid Build Coastguard Worker 
193*e5436536SAndroid Build Coastguard Worker   The function indicates if the noise correlation between the channel pair
194*e5436536SAndroid Build Coastguard Worker   is activated in out-of-phase mode.
195*e5436536SAndroid Build Coastguard Worker 
196*e5436536SAndroid Build Coastguard Worker   \return  PNS is out-of-phase
197*e5436536SAndroid Build Coastguard Worker */
CPns_IsOutOfPhase(const CPnsData * pPnsData,const int group,const int band)198*e5436536SAndroid Build Coastguard Worker static int CPns_IsOutOfPhase(const CPnsData *pPnsData, const int group,
199*e5436536SAndroid Build Coastguard Worker                              const int band) {
200*e5436536SAndroid Build Coastguard Worker   CPnsInterChannelData *pInterChannelData = pPnsData->pPnsInterChannelData;
201*e5436536SAndroid Build Coastguard Worker   unsigned pns_band = group * 16 + band;
202*e5436536SAndroid Build Coastguard Worker 
203*e5436536SAndroid Build Coastguard Worker   return (pInterChannelData->correlated[pns_band] & 0x02) ? 1 : 0;
204*e5436536SAndroid Build Coastguard Worker }
205*e5436536SAndroid Build Coastguard Worker 
206*e5436536SAndroid Build Coastguard Worker /*!
207*e5436536SAndroid Build Coastguard Worker   \brief Read PNS information
208*e5436536SAndroid Build Coastguard Worker 
209*e5436536SAndroid Build Coastguard Worker   The function reads the PNS information from the bitstream
210*e5436536SAndroid Build Coastguard Worker */
CPns_Read(CPnsData * pPnsData,HANDLE_FDK_BITSTREAM bs,const CodeBookDescription * hcb,SHORT * pScaleFactor,UCHAR global_gain,int band,int group)211*e5436536SAndroid Build Coastguard Worker void CPns_Read(CPnsData *pPnsData, HANDLE_FDK_BITSTREAM bs,
212*e5436536SAndroid Build Coastguard Worker                const CodeBookDescription *hcb, SHORT *pScaleFactor,
213*e5436536SAndroid Build Coastguard Worker                UCHAR global_gain, int band, int group /* = 0 */) {
214*e5436536SAndroid Build Coastguard Worker   int delta;
215*e5436536SAndroid Build Coastguard Worker   UINT pns_band = group * 16 + band;
216*e5436536SAndroid Build Coastguard Worker 
217*e5436536SAndroid Build Coastguard Worker   if (pPnsData->PnsActive) {
218*e5436536SAndroid Build Coastguard Worker     /* Next PNS band case */
219*e5436536SAndroid Build Coastguard Worker     delta = CBlock_DecodeHuffmanWord(bs, hcb) - 60;
220*e5436536SAndroid Build Coastguard Worker   } else {
221*e5436536SAndroid Build Coastguard Worker     /* First PNS band case */
222*e5436536SAndroid Build Coastguard Worker     int noiseStartValue = FDKreadBits(bs, 9);
223*e5436536SAndroid Build Coastguard Worker 
224*e5436536SAndroid Build Coastguard Worker     delta = noiseStartValue - 256;
225*e5436536SAndroid Build Coastguard Worker     pPnsData->PnsActive = 1;
226*e5436536SAndroid Build Coastguard Worker     pPnsData->CurrentEnergy = global_gain - NOISE_OFFSET;
227*e5436536SAndroid Build Coastguard Worker   }
228*e5436536SAndroid Build Coastguard Worker 
229*e5436536SAndroid Build Coastguard Worker   pPnsData->CurrentEnergy += delta;
230*e5436536SAndroid Build Coastguard Worker   pScaleFactor[pns_band] = pPnsData->CurrentEnergy;
231*e5436536SAndroid Build Coastguard Worker 
232*e5436536SAndroid Build Coastguard Worker   pPnsData->pnsUsed[pns_band] = 1;
233*e5436536SAndroid Build Coastguard Worker }
234*e5436536SAndroid Build Coastguard Worker 
235*e5436536SAndroid Build Coastguard Worker /**
236*e5436536SAndroid Build Coastguard Worker  * \brief Generate a vector of noise of given length. The noise values are
237*e5436536SAndroid Build Coastguard Worker  *        scaled in order to yield a noise energy of 1.0
238*e5436536SAndroid Build Coastguard Worker  * \param spec pointer to were the noise values will be written to.
239*e5436536SAndroid Build Coastguard Worker  * \param size amount of noise values to be generated.
240*e5436536SAndroid Build Coastguard Worker  * \param pRandomState pointer to the state of the random generator being used.
241*e5436536SAndroid Build Coastguard Worker  * \return exponent of generated noise vector.
242*e5436536SAndroid Build Coastguard Worker  */
GenerateRandomVector(FIXP_DBL * RESTRICT spec,int size,int * pRandomState)243*e5436536SAndroid Build Coastguard Worker static int GenerateRandomVector(FIXP_DBL *RESTRICT spec, int size,
244*e5436536SAndroid Build Coastguard Worker                                 int *pRandomState) {
245*e5436536SAndroid Build Coastguard Worker   int i, invNrg_e = 0, nrg_e = 0;
246*e5436536SAndroid Build Coastguard Worker   FIXP_DBL invNrg_m, nrg_m = FL2FXCONST_DBL(0.0f);
247*e5436536SAndroid Build Coastguard Worker   FIXP_DBL *RESTRICT ptr = spec;
248*e5436536SAndroid Build Coastguard Worker   int randomState = *pRandomState;
249*e5436536SAndroid Build Coastguard Worker 
250*e5436536SAndroid Build Coastguard Worker #define GEN_NOISE_NRG_SCALE 7
251*e5436536SAndroid Build Coastguard Worker 
252*e5436536SAndroid Build Coastguard Worker   /* Generate noise and calculate energy. */
253*e5436536SAndroid Build Coastguard Worker   for (i = 0; i < size; i++) {
254*e5436536SAndroid Build Coastguard Worker     randomState =
255*e5436536SAndroid Build Coastguard Worker         (((INT64)1664525 * randomState) + (INT64)1013904223) & 0xFFFFFFFF;
256*e5436536SAndroid Build Coastguard Worker     nrg_m = fPow2AddDiv2(nrg_m, (FIXP_DBL)randomState >> GEN_NOISE_NRG_SCALE);
257*e5436536SAndroid Build Coastguard Worker     *ptr++ = (FIXP_DBL)randomState;
258*e5436536SAndroid Build Coastguard Worker   }
259*e5436536SAndroid Build Coastguard Worker   nrg_e = GEN_NOISE_NRG_SCALE * 2 + 1;
260*e5436536SAndroid Build Coastguard Worker 
261*e5436536SAndroid Build Coastguard Worker   /* weight noise with = 1 / sqrt_nrg; */
262*e5436536SAndroid Build Coastguard Worker   invNrg_m = invSqrtNorm2(nrg_m << 1, &invNrg_e);
263*e5436536SAndroid Build Coastguard Worker   invNrg_e += -((nrg_e - 1) >> 1);
264*e5436536SAndroid Build Coastguard Worker 
265*e5436536SAndroid Build Coastguard Worker   for (i = size; i--;) {
266*e5436536SAndroid Build Coastguard Worker     spec[i] = fMult(spec[i], invNrg_m);
267*e5436536SAndroid Build Coastguard Worker   }
268*e5436536SAndroid Build Coastguard Worker 
269*e5436536SAndroid Build Coastguard Worker   /* Store random state */
270*e5436536SAndroid Build Coastguard Worker   *pRandomState = randomState;
271*e5436536SAndroid Build Coastguard Worker 
272*e5436536SAndroid Build Coastguard Worker   return invNrg_e;
273*e5436536SAndroid Build Coastguard Worker }
274*e5436536SAndroid Build Coastguard Worker 
ScaleBand(FIXP_DBL * RESTRICT spec,int size,int scaleFactor,int specScale,int noise_e,int out_of_phase)275*e5436536SAndroid Build Coastguard Worker static void ScaleBand(FIXP_DBL *RESTRICT spec, int size, int scaleFactor,
276*e5436536SAndroid Build Coastguard Worker                       int specScale, int noise_e, int out_of_phase) {
277*e5436536SAndroid Build Coastguard Worker   int i, shift, sfExponent;
278*e5436536SAndroid Build Coastguard Worker   FIXP_DBL sfMatissa;
279*e5436536SAndroid Build Coastguard Worker 
280*e5436536SAndroid Build Coastguard Worker   /* Get gain from scale factor value = 2^(scaleFactor * 0.25) */
281*e5436536SAndroid Build Coastguard Worker   sfMatissa = MantissaTable[scaleFactor & 0x03][0];
282*e5436536SAndroid Build Coastguard Worker   /* sfExponent = (scaleFactor >> 2) + ExponentTable[scaleFactor & 0x03][0]; */
283*e5436536SAndroid Build Coastguard Worker   /* Note:  ExponentTable[scaleFactor & 0x03][0] is always 1. */
284*e5436536SAndroid Build Coastguard Worker   sfExponent = (scaleFactor >> 2) + 1;
285*e5436536SAndroid Build Coastguard Worker 
286*e5436536SAndroid Build Coastguard Worker   if (out_of_phase != 0) {
287*e5436536SAndroid Build Coastguard Worker     sfMatissa = -sfMatissa;
288*e5436536SAndroid Build Coastguard Worker   }
289*e5436536SAndroid Build Coastguard Worker 
290*e5436536SAndroid Build Coastguard Worker   /* +1 because of fMultDiv2 below. */
291*e5436536SAndroid Build Coastguard Worker   shift = sfExponent - specScale + 1 + noise_e;
292*e5436536SAndroid Build Coastguard Worker 
293*e5436536SAndroid Build Coastguard Worker   /* Apply gain to noise values */
294*e5436536SAndroid Build Coastguard Worker   if (shift >= 0) {
295*e5436536SAndroid Build Coastguard Worker     shift = fixMin(shift, DFRACT_BITS - 1);
296*e5436536SAndroid Build Coastguard Worker     for (i = size; i-- != 0;) {
297*e5436536SAndroid Build Coastguard Worker       spec[i] = fMultDiv2(spec[i], sfMatissa) << shift;
298*e5436536SAndroid Build Coastguard Worker     }
299*e5436536SAndroid Build Coastguard Worker   } else {
300*e5436536SAndroid Build Coastguard Worker     shift = fixMin(-shift, DFRACT_BITS - 1);
301*e5436536SAndroid Build Coastguard Worker     for (i = size; i-- != 0;) {
302*e5436536SAndroid Build Coastguard Worker       spec[i] = fMultDiv2(spec[i], sfMatissa) >> shift;
303*e5436536SAndroid Build Coastguard Worker     }
304*e5436536SAndroid Build Coastguard Worker   }
305*e5436536SAndroid Build Coastguard Worker }
306*e5436536SAndroid Build Coastguard Worker 
307*e5436536SAndroid Build Coastguard Worker /*!
308*e5436536SAndroid Build Coastguard Worker   \brief Apply PNS
309*e5436536SAndroid Build Coastguard Worker 
310*e5436536SAndroid Build Coastguard Worker   The function applies PNS (i.e. it generates noise) on the bands
311*e5436536SAndroid Build Coastguard Worker   flagged as noisy bands
312*e5436536SAndroid Build Coastguard Worker 
313*e5436536SAndroid Build Coastguard Worker */
CPns_Apply(const CPnsData * pPnsData,const CIcsInfo * pIcsInfo,SPECTRAL_PTR pSpectrum,const SHORT * pSpecScale,const SHORT * pScaleFactor,const SamplingRateInfo * pSamplingRateInfo,const INT granuleLength,const int channel)314*e5436536SAndroid Build Coastguard Worker void CPns_Apply(const CPnsData *pPnsData, const CIcsInfo *pIcsInfo,
315*e5436536SAndroid Build Coastguard Worker                 SPECTRAL_PTR pSpectrum, const SHORT *pSpecScale,
316*e5436536SAndroid Build Coastguard Worker                 const SHORT *pScaleFactor,
317*e5436536SAndroid Build Coastguard Worker                 const SamplingRateInfo *pSamplingRateInfo,
318*e5436536SAndroid Build Coastguard Worker                 const INT granuleLength, const int channel) {
319*e5436536SAndroid Build Coastguard Worker   if (pPnsData->PnsActive) {
320*e5436536SAndroid Build Coastguard Worker     const short *BandOffsets =
321*e5436536SAndroid Build Coastguard Worker         GetScaleFactorBandOffsets(pIcsInfo, pSamplingRateInfo);
322*e5436536SAndroid Build Coastguard Worker 
323*e5436536SAndroid Build Coastguard Worker     int ScaleFactorBandsTransmitted = GetScaleFactorBandsTransmitted(pIcsInfo);
324*e5436536SAndroid Build Coastguard Worker 
325*e5436536SAndroid Build Coastguard Worker     for (int window = 0, group = 0; group < GetWindowGroups(pIcsInfo);
326*e5436536SAndroid Build Coastguard Worker          group++) {
327*e5436536SAndroid Build Coastguard Worker       for (int groupwin = 0; groupwin < GetWindowGroupLength(pIcsInfo, group);
328*e5436536SAndroid Build Coastguard Worker            groupwin++, window++) {
329*e5436536SAndroid Build Coastguard Worker         FIXP_DBL *spectrum = SPEC(pSpectrum, window, granuleLength);
330*e5436536SAndroid Build Coastguard Worker 
331*e5436536SAndroid Build Coastguard Worker         for (int band = 0; band < ScaleFactorBandsTransmitted; band++) {
332*e5436536SAndroid Build Coastguard Worker           if (CPns_IsPnsUsed(pPnsData, group, band)) {
333*e5436536SAndroid Build Coastguard Worker             UINT pns_band = window * 16 + band;
334*e5436536SAndroid Build Coastguard Worker 
335*e5436536SAndroid Build Coastguard Worker             int bandWidth = BandOffsets[band + 1] - BandOffsets[band];
336*e5436536SAndroid Build Coastguard Worker             int noise_e;
337*e5436536SAndroid Build Coastguard Worker 
338*e5436536SAndroid Build Coastguard Worker             FDK_ASSERT(bandWidth >= 0);
339*e5436536SAndroid Build Coastguard Worker 
340*e5436536SAndroid Build Coastguard Worker             if (channel > 0 && CPns_IsCorrelated(pPnsData, group, band)) {
341*e5436536SAndroid Build Coastguard Worker               noise_e =
342*e5436536SAndroid Build Coastguard Worker                   GenerateRandomVector(spectrum + BandOffsets[band], bandWidth,
343*e5436536SAndroid Build Coastguard Worker                                        &pPnsData->randomSeed[pns_band]);
344*e5436536SAndroid Build Coastguard Worker             } else {
345*e5436536SAndroid Build Coastguard Worker               pPnsData->randomSeed[pns_band] = *pPnsData->currentSeed;
346*e5436536SAndroid Build Coastguard Worker 
347*e5436536SAndroid Build Coastguard Worker               noise_e = GenerateRandomVector(spectrum + BandOffsets[band],
348*e5436536SAndroid Build Coastguard Worker                                              bandWidth, pPnsData->currentSeed);
349*e5436536SAndroid Build Coastguard Worker             }
350*e5436536SAndroid Build Coastguard Worker 
351*e5436536SAndroid Build Coastguard Worker             int outOfPhase = CPns_IsOutOfPhase(pPnsData, group, band);
352*e5436536SAndroid Build Coastguard Worker 
353*e5436536SAndroid Build Coastguard Worker             ScaleBand(spectrum + BandOffsets[band], bandWidth,
354*e5436536SAndroid Build Coastguard Worker                       pScaleFactor[group * 16 + band], pSpecScale[window],
355*e5436536SAndroid Build Coastguard Worker                       noise_e, outOfPhase);
356*e5436536SAndroid Build Coastguard Worker           }
357*e5436536SAndroid Build Coastguard Worker         }
358*e5436536SAndroid Build Coastguard Worker       }
359*e5436536SAndroid Build Coastguard Worker     }
360*e5436536SAndroid Build Coastguard Worker   }
361*e5436536SAndroid Build Coastguard Worker }
362