1 /*
2 * Copyright (C) 2004-2010 NXP Software
3 * Copyright (C) 2010 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 #include "LVPSA.h"
19 #include "LVPSA_Private.h"
20 #include "InstAlloc.h"
21
22 /************************************************************************************/
23 /* */
24 /* FUNCTION: LVPSA_Init */
25 /* */
26 /* DESCRIPTION: */
27 /* Initialize the LVPSA module */
28 /* */
29 /* */
30 /* PARAMETERS: */
31 /* phInstance Pointer to pointer to the instance */
32 /* InitParams Init parameters structure */
33 /* ControlParams Control parameters structure */
34 /* pMemoryTable Memory table that contains memory areas definition */
35 /* */
36 /* */
37 /* RETURNS: */
38 /* LVPSA_OK Succeeds */
39 /* otherwise Error due to bad parameters */
40 /* */
41 /************************************************************************************/
LVPSA_Init(pLVPSA_Handle_t * phInstance,LVPSA_InitParams_t * pInitParams,LVPSA_ControlParams_t * pControlParams,LVPSA_MemTab_t * pMemoryTable)42 LVPSA_RETURN LVPSA_Init ( pLVPSA_Handle_t *phInstance,
43 LVPSA_InitParams_t *pInitParams,
44 LVPSA_ControlParams_t *pControlParams,
45 LVPSA_MemTab_t *pMemoryTable )
46 {
47 LVPSA_InstancePr_t *pLVPSA_Inst;
48 LVPSA_RETURN errorCode = LVPSA_OK;
49 LVM_UINT32 ii;
50 #ifndef BUILD_FLOAT
51 extern LVM_INT16 LVPSA_GainTable[];
52 #else
53 extern LVM_FLOAT LVPSA_Float_GainTable[];
54 #endif
55 LVM_UINT32 BufferLength = 0;
56
57 /* Ints_Alloc instances, needed for memory alignment management */
58 INST_ALLOC Instance;
59 INST_ALLOC Scratch;
60 INST_ALLOC Data;
61 INST_ALLOC Coef;
62
63 /* Check parameters */
64 if((phInstance == LVM_NULL) || (pInitParams == LVM_NULL) || (pControlParams == LVM_NULL) || (pMemoryTable == LVM_NULL))
65 {
66 return(LVPSA_ERROR_NULLADDRESS);
67 }
68 if( (pInitParams->SpectralDataBufferDuration > LVPSA_MAXBUFFERDURATION) ||
69 (pInitParams->SpectralDataBufferDuration == 0) ||
70 (pInitParams->MaxInputBlockSize > LVPSA_MAXINPUTBLOCKSIZE) ||
71 (pInitParams->MaxInputBlockSize == 0) ||
72 (pInitParams->nBands < LVPSA_NBANDSMIN) ||
73 (pInitParams->nBands > LVPSA_NBANDSMAX) ||
74 (pInitParams->pFiltersParams == 0))
75 {
76 return(LVPSA_ERROR_INVALIDPARAM);
77 }
78 for(ii = 0; ii < pInitParams->nBands; ii++)
79 {
80 if((pInitParams->pFiltersParams[ii].CenterFrequency > LVPSA_MAXCENTERFREQ) ||
81 (pInitParams->pFiltersParams[ii].PostGain > LVPSA_MAXPOSTGAIN) ||
82 (pInitParams->pFiltersParams[ii].PostGain < LVPSA_MINPOSTGAIN) ||
83 (pInitParams->pFiltersParams[ii].QFactor < LVPSA_MINQFACTOR) ||
84 (pInitParams->pFiltersParams[ii].QFactor > LVPSA_MAXQFACTOR))
85 {
86 return(LVPSA_ERROR_INVALIDPARAM);
87 }
88 }
89
90
91 /*Inst_Alloc instances initialization */
92 InstAlloc_Init( &Instance , pMemoryTable->Region[LVPSA_MEMREGION_INSTANCE].pBaseAddress);
93 InstAlloc_Init( &Scratch , pMemoryTable->Region[LVPSA_MEMREGION_SCRATCH].pBaseAddress);
94 InstAlloc_Init( &Data , pMemoryTable->Region[LVPSA_MEMREGION_PERSISTENT_DATA].pBaseAddress);
95 InstAlloc_Init( &Coef , pMemoryTable->Region[LVPSA_MEMREGION_PERSISTENT_COEF].pBaseAddress);
96
97
98 /* Set the instance handle if not already initialised */
99 if (*phInstance == LVM_NULL)
100 {
101 *phInstance = InstAlloc_AddMember( &Instance, sizeof(LVPSA_InstancePr_t) );
102 }
103 pLVPSA_Inst =(LVPSA_InstancePr_t*)*phInstance;
104
105
106 /* Check the memory table for NULL pointers */
107 for (ii = 0; ii < LVPSA_NR_MEMORY_REGIONS; ii++)
108 {
109 if (pMemoryTable->Region[ii].Size!=0)
110 {
111 if (pMemoryTable->Region[ii].pBaseAddress==LVM_NULL)
112 {
113 return(LVPSA_ERROR_NULLADDRESS);
114 }
115 pLVPSA_Inst->MemoryTable.Region[ii] = pMemoryTable->Region[ii];
116 }
117 }
118
119 /* Initialize module's internal parameters */
120 pLVPSA_Inst->bControlPending = LVM_FALSE;
121 pLVPSA_Inst->nBands = pInitParams->nBands;
122 pLVPSA_Inst->MaxInputBlockSize = pInitParams->MaxInputBlockSize;
123 pLVPSA_Inst->SpectralDataBufferDuration = pInitParams->SpectralDataBufferDuration;
124 pLVPSA_Inst->CurrentParams.Fs = LVM_FS_DUMMY;
125 pLVPSA_Inst->CurrentParams.LevelDetectionSpeed = LVPSA_SPEED_DUMMY;
126
127 { /* for avoiding QAC warnings */
128 LVM_INT32 SDBD=(LVM_INT32)pLVPSA_Inst->SpectralDataBufferDuration;
129 LVM_INT32 IRTI=(LVM_INT32)LVPSA_InternalRefreshTimeInv;
130 LVM_INT32 BL;
131
132 MUL32x32INTO32(SDBD,IRTI,BL,LVPSA_InternalRefreshTimeShift)
133
134 BufferLength=(LVM_UINT32)BL;
135 }
136
137 if((BufferLength * LVPSA_InternalRefreshTime) != pLVPSA_Inst->SpectralDataBufferDuration)
138 {
139 pLVPSA_Inst->SpectralDataBufferLength = BufferLength + 1;
140 }
141 else
142 {
143 pLVPSA_Inst->SpectralDataBufferLength = BufferLength;
144 }
145
146
147 /* Assign the pointers */
148 #ifndef BUILD_FLOAT
149 pLVPSA_Inst->pPostGains = InstAlloc_AddMember( &Instance, pInitParams->nBands * sizeof(LVM_UINT16) );
150 #else
151 pLVPSA_Inst->pPostGains = InstAlloc_AddMember( &Instance, pInitParams->nBands * \
152 sizeof(LVM_FLOAT) );
153 #endif
154 pLVPSA_Inst->pFiltersParams = InstAlloc_AddMember( &Instance, pInitParams->nBands * sizeof(LVPSA_FilterParam_t) );
155 pLVPSA_Inst->pSpectralDataBufferStart = InstAlloc_AddMember( &Instance, pInitParams->nBands * pLVPSA_Inst->SpectralDataBufferLength * sizeof(LVM_UINT8) );
156 pLVPSA_Inst->pPreviousPeaks = InstAlloc_AddMember( &Instance, pInitParams->nBands * sizeof(LVM_UINT8) );
157 pLVPSA_Inst->pBPFiltersPrecision = InstAlloc_AddMember( &Instance, pInitParams->nBands * sizeof(LVPSA_BPFilterPrecision_en) );
158 #ifndef BUILD_FLOAT
159 pLVPSA_Inst->pBP_Instances = InstAlloc_AddMember( &Coef, pInitParams->nBands * sizeof(Biquad_Instance_t) );
160 pLVPSA_Inst->pQPD_States = InstAlloc_AddMember( &Coef, pInitParams->nBands * sizeof(QPD_State_t) );
161 #else
162 pLVPSA_Inst->pBP_Instances = InstAlloc_AddMember( &Coef, pInitParams->nBands * \
163 sizeof(Biquad_FLOAT_Instance_t) );
164 pLVPSA_Inst->pQPD_States = InstAlloc_AddMember( &Coef, pInitParams->nBands * \
165 sizeof(QPD_FLOAT_State_t) );
166 #endif
167
168 #ifndef BUILD_FLOAT
169 pLVPSA_Inst->pBP_Taps = InstAlloc_AddMember( &Data, pInitParams->nBands * sizeof(Biquad_1I_Order2_Taps_t) );
170 pLVPSA_Inst->pQPD_Taps = InstAlloc_AddMember( &Data, pInitParams->nBands * sizeof(QPD_Taps_t) );
171
172 #else
173 pLVPSA_Inst->pBP_Taps = InstAlloc_AddMember( &Data,
174 pInitParams->nBands * \
175 sizeof(Biquad_1I_Order2_FLOAT_Taps_t));
176 pLVPSA_Inst->pQPD_Taps = InstAlloc_AddMember( &Data, pInitParams->nBands * \
177 sizeof(QPD_FLOAT_Taps_t) );
178 #endif
179
180 /* Copy filters parameters in the private instance */
181 for(ii = 0; ii < pLVPSA_Inst->nBands; ii++)
182 {
183 pLVPSA_Inst->pFiltersParams[ii] = pInitParams->pFiltersParams[ii];
184 }
185
186 /* Set Post filters gains*/
187 for(ii = 0; ii < pLVPSA_Inst->nBands; ii++)
188 {
189 #ifndef BUILD_FLOAT
190 pLVPSA_Inst->pPostGains[ii] =(LVM_UINT16) LVPSA_GainTable[pInitParams->pFiltersParams[ii].PostGain + 15];
191 #else
192 pLVPSA_Inst->pPostGains[ii] = LVPSA_Float_GainTable[15 + \
193 pInitParams->pFiltersParams[ii].PostGain];
194 #endif
195 }
196 pLVPSA_Inst->pSpectralDataBufferWritePointer = pLVPSA_Inst->pSpectralDataBufferStart;
197
198
199 /* Initialize control dependant internal parameters */
200 errorCode = LVPSA_Control (*phInstance, pControlParams);
201
202 if(errorCode!=0)
203 {
204 return errorCode;
205 }
206
207 errorCode = LVPSA_ApplyNewSettings (pLVPSA_Inst);
208
209 if(errorCode!=0)
210 {
211 return errorCode;
212 }
213
214 return(errorCode);
215 }
216
217