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 extern LVM_FLOAT LVPSA_Float_GainTable[];
51 LVM_UINT32 BufferLength = 0;
52
53 /* Ints_Alloc instances, needed for memory alignment management */
54 INST_ALLOC Instance;
55 INST_ALLOC Scratch;
56 INST_ALLOC Data;
57 INST_ALLOC Coef;
58
59 /* Check parameters */
60 if((phInstance == LVM_NULL) || (pInitParams == LVM_NULL) || (pControlParams == LVM_NULL) || (pMemoryTable == LVM_NULL))
61 {
62 return(LVPSA_ERROR_NULLADDRESS);
63 }
64 if( (pInitParams->SpectralDataBufferDuration > LVPSA_MAXBUFFERDURATION) ||
65 (pInitParams->SpectralDataBufferDuration == 0) ||
66 (pInitParams->MaxInputBlockSize > LVPSA_MAXINPUTBLOCKSIZE) ||
67 (pInitParams->MaxInputBlockSize == 0) ||
68 (pInitParams->nBands < LVPSA_NBANDSMIN) ||
69 (pInitParams->nBands > LVPSA_NBANDSMAX) ||
70 (pInitParams->pFiltersParams == 0))
71 {
72 return(LVPSA_ERROR_INVALIDPARAM);
73 }
74 for(ii = 0; ii < pInitParams->nBands; ii++)
75 {
76 if((pInitParams->pFiltersParams[ii].CenterFrequency > LVPSA_MAXCENTERFREQ) ||
77 (pInitParams->pFiltersParams[ii].PostGain > LVPSA_MAXPOSTGAIN) ||
78 (pInitParams->pFiltersParams[ii].PostGain < LVPSA_MINPOSTGAIN) ||
79 (pInitParams->pFiltersParams[ii].QFactor < LVPSA_MINQFACTOR) ||
80 (pInitParams->pFiltersParams[ii].QFactor > LVPSA_MAXQFACTOR))
81 {
82 return(LVPSA_ERROR_INVALIDPARAM);
83 }
84 }
85
86 /*Inst_Alloc instances initialization */
87 InstAlloc_Init( &Instance , pMemoryTable->Region[LVPSA_MEMREGION_INSTANCE].pBaseAddress);
88 InstAlloc_Init( &Scratch , pMemoryTable->Region[LVPSA_MEMREGION_SCRATCH].pBaseAddress);
89 InstAlloc_Init( &Data , pMemoryTable->Region[LVPSA_MEMREGION_PERSISTENT_DATA].pBaseAddress);
90 InstAlloc_Init( &Coef , pMemoryTable->Region[LVPSA_MEMREGION_PERSISTENT_COEF].pBaseAddress);
91
92 /* Set the instance handle if not already initialised */
93 if (*phInstance == LVM_NULL)
94 {
95 *phInstance = InstAlloc_AddMember( &Instance, sizeof(LVPSA_InstancePr_t) );
96 }
97 pLVPSA_Inst =(LVPSA_InstancePr_t*)*phInstance;
98
99 /* Check the memory table for NULL pointers */
100 for (ii = 0; ii < LVPSA_NR_MEMORY_REGIONS; ii++)
101 {
102 if (pMemoryTable->Region[ii].Size!=0)
103 {
104 if (pMemoryTable->Region[ii].pBaseAddress==LVM_NULL)
105 {
106 return(LVPSA_ERROR_NULLADDRESS);
107 }
108 pLVPSA_Inst->MemoryTable.Region[ii] = pMemoryTable->Region[ii];
109 }
110 }
111
112 /* Initialize module's internal parameters */
113 pLVPSA_Inst->bControlPending = LVM_FALSE;
114 pLVPSA_Inst->nBands = pInitParams->nBands;
115 pLVPSA_Inst->MaxInputBlockSize = pInitParams->MaxInputBlockSize;
116 pLVPSA_Inst->SpectralDataBufferDuration = pInitParams->SpectralDataBufferDuration;
117 pLVPSA_Inst->CurrentParams.Fs = LVM_FS_DUMMY;
118 pLVPSA_Inst->CurrentParams.LevelDetectionSpeed = LVPSA_SPEED_DUMMY;
119
120 { /* for avoiding QAC warnings */
121 LVM_INT32 SDBD=(LVM_INT32)pLVPSA_Inst->SpectralDataBufferDuration;
122 LVM_INT32 IRTI=(LVM_INT32)LVPSA_InternalRefreshTimeInv;
123 LVM_INT32 BL;
124
125 MUL32x32INTO32(SDBD,IRTI,BL,LVPSA_InternalRefreshTimeShift)
126
127 BufferLength=(LVM_UINT32)BL;
128 }
129
130 if((BufferLength * LVPSA_InternalRefreshTime) != pLVPSA_Inst->SpectralDataBufferDuration)
131 {
132 pLVPSA_Inst->SpectralDataBufferLength = BufferLength + 1;
133 }
134 else
135 {
136 pLVPSA_Inst->SpectralDataBufferLength = BufferLength;
137 }
138
139 /* Assign the pointers */
140 pLVPSA_Inst->pPostGains =
141 (LVM_FLOAT *)InstAlloc_AddMember(&Instance, pInitParams->nBands * sizeof(LVM_FLOAT));
142 pLVPSA_Inst->pFiltersParams = (LVPSA_FilterParam_t *)
143 InstAlloc_AddMember(&Instance, pInitParams->nBands * sizeof(LVPSA_FilterParam_t));
144 pLVPSA_Inst->pSpectralDataBufferStart = (LVM_UINT8 *)
145 InstAlloc_AddMember(&Instance, pInitParams->nBands * \
146 pLVPSA_Inst->SpectralDataBufferLength * sizeof(LVM_UINT8));
147 pLVPSA_Inst->pPreviousPeaks = (LVM_UINT8 *)
148 InstAlloc_AddMember(&Instance, pInitParams->nBands * sizeof(LVM_UINT8));
149 pLVPSA_Inst->pBPFiltersPrecision = (LVPSA_BPFilterPrecision_en *)
150 InstAlloc_AddMember(&Instance, pInitParams->nBands * \
151 sizeof(LVPSA_BPFilterPrecision_en));
152 pLVPSA_Inst->pBP_Instances = (Biquad_FLOAT_Instance_t *)
153 InstAlloc_AddMember(&Coef, pInitParams->nBands * \
154 sizeof(Biquad_FLOAT_Instance_t));
155 pLVPSA_Inst->pQPD_States = (QPD_FLOAT_State_t *)
156 InstAlloc_AddMember(&Coef, pInitParams->nBands * \
157 sizeof(QPD_FLOAT_State_t));
158
159 pLVPSA_Inst->pBP_Taps = (Biquad_1I_Order2_FLOAT_Taps_t *)
160 InstAlloc_AddMember(&Data, pInitParams->nBands * \
161 sizeof(Biquad_1I_Order2_FLOAT_Taps_t));
162 pLVPSA_Inst->pQPD_Taps = (QPD_FLOAT_Taps_t *)
163 InstAlloc_AddMember(&Data, pInitParams->nBands * \
164 sizeof(QPD_FLOAT_Taps_t));
165
166 /* Copy filters parameters in the private instance */
167 for(ii = 0; ii < pLVPSA_Inst->nBands; ii++)
168 {
169 pLVPSA_Inst->pFiltersParams[ii] = pInitParams->pFiltersParams[ii];
170 }
171
172 /* Set Post filters gains*/
173 for(ii = 0; ii < pLVPSA_Inst->nBands; ii++)
174 {
175 pLVPSA_Inst->pPostGains[ii] = LVPSA_Float_GainTable[15 + \
176 pInitParams->pFiltersParams[ii].PostGain];
177 }
178 pLVPSA_Inst->pSpectralDataBufferWritePointer = pLVPSA_Inst->pSpectralDataBufferStart;
179
180 /* Initialize control dependant internal parameters */
181 errorCode = LVPSA_Control (*phInstance, pControlParams);
182
183 if(errorCode!=0)
184 {
185 return errorCode;
186 }
187
188 errorCode = LVPSA_ApplyNewSettings (pLVPSA_Inst);
189
190 if(errorCode!=0)
191 {
192 return errorCode;
193 }
194
195 return(errorCode);
196 }
197
198