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 /****************************************************************************************/
19 /* */
20 /* Includes */
21 /* */
22 /****************************************************************************************/
23
24 #include "LVEQNB.h"
25 #include "LVEQNB_Private.h"
26 #include "InstAlloc.h"
27 #include <string.h> /* For memset */
28
29 /****************************************************************************************/
30 /* */
31 /* FUNCTION: LVEQNB_Memory */
32 /* */
33 /* DESCRIPTION: */
34 /* This function is used for memory allocation and free. It can be called in */
35 /* two ways: */
36 /* */
37 /* hInstance = NULL Returns the memory requirements */
38 /* hInstance = Instance handle Returns the memory requirements and */
39 /* allocated base addresses for the instance */
40 /* */
41 /* When this function is called for memory allocation (hInstance=NULL) the memory */
42 /* base address pointers are NULL on return. */
43 /* */
44 /* When the function is called for free (hInstance = Instance Handle) the memory */
45 /* table returns the allocated memory and base addresses used during initialisation. */
46 /* */
47 /* PARAMETERS: */
48 /* hInstance Instance Handle */
49 /* pMemoryTable Pointer to an empty memory definition table */
50 /* pCapabilities Pointer to the instance capabilities */
51 /* */
52 /* RETURNS: */
53 /* LVEQNB_SUCCESS Succeeded */
54 /* LVEQNB_NULLADDRESS When any of pMemoryTable and pCapabilities is NULL address */
55 /* */
56 /* NOTES: */
57 /* 1. This function may be interrupted by the LVEQNB_Process function */
58 /* */
59 /****************************************************************************************/
60
LVEQNB_Memory(LVEQNB_Handle_t hInstance,LVEQNB_MemTab_t * pMemoryTable,LVEQNB_Capabilities_t * pCapabilities)61 LVEQNB_ReturnStatus_en LVEQNB_Memory(LVEQNB_Handle_t hInstance,
62 LVEQNB_MemTab_t *pMemoryTable,
63 LVEQNB_Capabilities_t *pCapabilities)
64 {
65
66 INST_ALLOC AllocMem;
67 LVEQNB_Instance_t *pInstance = (LVEQNB_Instance_t *)hInstance;
68
69 if((pMemoryTable == LVM_NULL)|| (pCapabilities == LVM_NULL))
70 {
71 return LVEQNB_NULLADDRESS;
72 }
73
74 /*
75 * Fill in the memory table
76 */
77 if (hInstance == LVM_NULL)
78 {
79 /*
80 * Instance memory
81 */
82 InstAlloc_Init(&AllocMem,
83 LVM_NULL);
84 InstAlloc_AddMember(&AllocMem, /* Low pass filter */
85 sizeof(LVEQNB_Instance_t));
86 pMemoryTable->Region[LVEQNB_MEMREGION_INSTANCE].Size = InstAlloc_GetTotal(&AllocMem);
87 pMemoryTable->Region[LVEQNB_MEMREGION_INSTANCE].Alignment = LVEQNB_INSTANCE_ALIGN;
88 pMemoryTable->Region[LVEQNB_MEMREGION_INSTANCE].Type = LVEQNB_PERSISTENT;
89 pMemoryTable->Region[LVEQNB_MEMREGION_INSTANCE].pBaseAddress = LVM_NULL;
90
91 /*
92 * Persistant data memory
93 */
94 InstAlloc_Init(&AllocMem,
95 LVM_NULL);
96 InstAlloc_AddMember(&AllocMem, /* Low pass filter */
97 sizeof(Biquad_2I_Order2_FLOAT_Taps_t));
98 InstAlloc_AddMember(&AllocMem, /* High pass filter */
99 sizeof(Biquad_2I_Order2_FLOAT_Taps_t));
100 /* Equaliser Biquad Taps */
101 InstAlloc_AddMember(&AllocMem,
102 (pCapabilities->MaxBands * sizeof(Biquad_2I_Order2_FLOAT_Taps_t)));
103 /* Filter definitions */
104 InstAlloc_AddMember(&AllocMem,
105 (pCapabilities->MaxBands * sizeof(LVEQNB_BandDef_t)));
106 /* Biquad types */
107 InstAlloc_AddMember(&AllocMem,
108 (pCapabilities->MaxBands * sizeof(LVEQNB_BiquadType_en)));
109 pMemoryTable->Region[LVEQNB_MEMREGION_PERSISTENT_DATA].Size = InstAlloc_GetTotal(&AllocMem);
110 pMemoryTable->Region[LVEQNB_MEMREGION_PERSISTENT_DATA].Alignment = LVEQNB_DATA_ALIGN;
111 pMemoryTable->Region[LVEQNB_MEMREGION_PERSISTENT_DATA].Type = LVEQNB_PERSISTENT_DATA;
112 pMemoryTable->Region[LVEQNB_MEMREGION_PERSISTENT_DATA].pBaseAddress = LVM_NULL;
113
114 /*
115 * Persistant coefficient memory
116 */
117 InstAlloc_Init(&AllocMem,
118 LVM_NULL);
119 InstAlloc_AddMember(&AllocMem, /* Low pass filter */
120 sizeof(Biquad_FLOAT_Instance_t));
121 InstAlloc_AddMember(&AllocMem, /* High pass filter */
122 sizeof(Biquad_FLOAT_Instance_t));
123 /* Equaliser Biquad Instance */
124 InstAlloc_AddMember(&AllocMem,
125 pCapabilities->MaxBands * sizeof(Biquad_FLOAT_Instance_t));
126 pMemoryTable->Region[LVEQNB_MEMREGION_PERSISTENT_COEF].Size = InstAlloc_GetTotal(&AllocMem);
127 pMemoryTable->Region[LVEQNB_MEMREGION_PERSISTENT_COEF].Alignment = LVEQNB_COEF_ALIGN;
128 pMemoryTable->Region[LVEQNB_MEMREGION_PERSISTENT_COEF].Type = LVEQNB_PERSISTENT_COEF;
129 pMemoryTable->Region[LVEQNB_MEMREGION_PERSISTENT_COEF].pBaseAddress = LVM_NULL;
130
131 /*
132 * Scratch memory
133 */
134 InstAlloc_Init(&AllocMem,
135 LVM_NULL);
136 InstAlloc_AddMember(&AllocMem, /* Low pass filter */
137 LVEQNB_SCRATCHBUFFERS * sizeof(LVM_FLOAT) * \
138 pCapabilities->MaxBlockSize);
139 pMemoryTable->Region[LVEQNB_MEMREGION_SCRATCH].Size = InstAlloc_GetTotal(&AllocMem);
140 pMemoryTable->Region[LVEQNB_MEMREGION_SCRATCH].Alignment = LVEQNB_SCRATCH_ALIGN;
141 pMemoryTable->Region[LVEQNB_MEMREGION_SCRATCH].Type = LVEQNB_SCRATCH;
142 pMemoryTable->Region[LVEQNB_MEMREGION_SCRATCH].pBaseAddress = LVM_NULL;
143 }
144 else
145 {
146 /* Read back memory allocation table */
147 *pMemoryTable = pInstance->MemoryTable;
148 }
149
150 return(LVEQNB_SUCCESS);
151 }
152
153 /****************************************************************************************/
154 /* */
155 /* FUNCTION: LVEQNB_Init */
156 /* */
157 /* DESCRIPTION: */
158 /* Create and initialisation function for the N-Band equaliser module */
159 /* */
160 /* This function can be used to create an algorithm instance by calling with */
161 /* hInstance set to NULL. In this case the algorithm returns the new instance */
162 /* handle. */
163 /* */
164 /* This function can be used to force a full re-initialisation of the algorithm */
165 /* by calling with hInstance = Instance Handle. In this case the memory table */
166 /* should be correct for the instance, this can be ensured by calling the function */
167 /* DBE_Memory before calling this function. */
168 /* */
169 /* PARAMETERS: */
170 /* hInstance Instance handle */
171 /* pMemoryTable Pointer to the memory definition table */
172 /* pCapabilities Pointer to the instance capabilities */
173 /* */
174 /* RETURNS: */
175 /* LVEQNB_SUCCESS Initialisation succeeded */
176 /* LVEQNB_NULLADDRESS When pCapabilities or pMemoryTableis or phInstance are NULL */
177 /* LVEQNB_NULLADDRESS One or more of the memory regions has a NULL base address */
178 /* pointer for a memory region with a non-zero size. */
179 /* */
180 /* NOTES: */
181 /* 1. The instance handle is the pointer to the base address of the first memory */
182 /* region. */
183 /* 2. This function must not be interrupted by the LVEQNB_Process function */
184 /* */
185 /****************************************************************************************/
186
LVEQNB_Init(LVEQNB_Handle_t * phInstance,LVEQNB_MemTab_t * pMemoryTable,LVEQNB_Capabilities_t * pCapabilities)187 LVEQNB_ReturnStatus_en LVEQNB_Init(LVEQNB_Handle_t *phInstance,
188 LVEQNB_MemTab_t *pMemoryTable,
189 LVEQNB_Capabilities_t *pCapabilities)
190 {
191
192 LVEQNB_Instance_t *pInstance;
193 LVM_UINT32 MemSize;
194 INST_ALLOC AllocMem;
195 LVM_INT32 i;
196
197 /*
198 * Check for NULL pointers
199 */
200 if((phInstance == LVM_NULL) || (pMemoryTable == LVM_NULL) || (pCapabilities == LVM_NULL))
201 {
202 return LVEQNB_NULLADDRESS;
203 }
204
205 /*
206 * Check the memory table for NULL pointers
207 */
208 for (i = 0; i < LVEQNB_NR_MEMORY_REGIONS; i++)
209 {
210 if (pMemoryTable->Region[i].Size!=0)
211 {
212 if (pMemoryTable->Region[i].pBaseAddress==LVM_NULL)
213 {
214 return(LVEQNB_NULLADDRESS);
215 }
216 }
217 }
218
219 /*
220 * Set the instance handle if not already initialised
221 */
222
223 InstAlloc_Init(&AllocMem, pMemoryTable->Region[LVEQNB_MEMREGION_INSTANCE].pBaseAddress);
224
225 if (*phInstance == LVM_NULL)
226 {
227 *phInstance = InstAlloc_AddMember(&AllocMem, sizeof(LVEQNB_Instance_t));
228 }
229 pInstance =(LVEQNB_Instance_t *)*phInstance;
230
231 /*
232 * Save the memory table in the instance structure
233 */
234 pInstance->Capabilities = *pCapabilities;
235
236 /*
237 * Save the memory table in the instance structure and
238 * set the structure pointers
239 */
240 pInstance->MemoryTable = *pMemoryTable;
241
242 /*
243 * Allocate coefficient memory
244 */
245 InstAlloc_Init(&AllocMem,
246 pMemoryTable->Region[LVEQNB_MEMREGION_PERSISTENT_COEF].pBaseAddress);
247
248 /* Equaliser Biquad Instance */
249 pInstance->pEQNB_FilterState_Float = (Biquad_FLOAT_Instance_t *)
250 InstAlloc_AddMember(&AllocMem, pCapabilities->MaxBands * \
251 sizeof(Biquad_FLOAT_Instance_t));
252
253 /*
254 * Allocate data memory
255 */
256 InstAlloc_Init(&AllocMem,
257 pMemoryTable->Region[LVEQNB_MEMREGION_PERSISTENT_DATA].pBaseAddress);
258
259 MemSize = (pCapabilities->MaxBands * sizeof(Biquad_2I_Order2_FLOAT_Taps_t));
260 pInstance->pEQNB_Taps_Float = (Biquad_2I_Order2_FLOAT_Taps_t *)InstAlloc_AddMember(&AllocMem,
261 MemSize);
262 MemSize = (pCapabilities->MaxBands * sizeof(LVEQNB_BandDef_t));
263 pInstance->pBandDefinitions = (LVEQNB_BandDef_t *)InstAlloc_AddMember(&AllocMem,
264 MemSize);
265 // clear all the bands, setting their gain to 0, otherwise when applying new params,
266 // it will compare against uninitialized values
267 memset(pInstance->pBandDefinitions, 0, MemSize);
268 MemSize = (pCapabilities->MaxBands * sizeof(LVEQNB_BiquadType_en));
269 pInstance->pBiquadType = (LVEQNB_BiquadType_en *)InstAlloc_AddMember(&AllocMem,
270 MemSize);
271
272 /*
273 * Internally map, structure and allign scratch memory
274 */
275 InstAlloc_Init(&AllocMem,
276 pMemoryTable->Region[LVEQNB_MEMREGION_SCRATCH].pBaseAddress);
277
278 pInstance->pFastTemporary = (LVM_FLOAT *)InstAlloc_AddMember(&AllocMem,
279 sizeof(LVM_FLOAT));
280
281 /*
282 * Update the instance parameters
283 */
284 pInstance->Params.NBands = 0;
285 pInstance->Params.OperatingMode = LVEQNB_BYPASS;
286 pInstance->Params.pBandDefinition = LVM_NULL;
287 pInstance->Params.SampleRate = LVEQNB_FS_8000;
288 pInstance->Params.SourceFormat = LVEQNB_STEREO;
289
290 /*
291 * Initialise the filters
292 */
293 LVEQNB_SetFilters(pInstance, /* Set the filter types */
294 &pInstance->Params);
295
296 LVEQNB_SetCoefficients(pInstance); /* Set the filter coefficients */
297
298 LVEQNB_ClearFilterHistory(pInstance); /* Clear the filter history */
299
300 /*
301 * Initialise the bypass variables
302 */
303 pInstance->BypassMixer.MixerStream[0].CallbackSet = 0;
304 pInstance->BypassMixer.MixerStream[0].CallbackParam = 0;
305 pInstance->BypassMixer.MixerStream[0].pCallbackHandle = (void*)pInstance;
306 pInstance->BypassMixer.MixerStream[0].pCallBack = LVEQNB_BypassMixerCallBack;
307
308 LVC_Mixer_Init(&pInstance->BypassMixer.MixerStream[0],0,0);
309 LVC_Mixer_SetTimeConstant(&pInstance->BypassMixer.MixerStream[0],0,LVM_FS_8000,2);
310
311 pInstance->BypassMixer.MixerStream[1].CallbackSet = 1;
312 pInstance->BypassMixer.MixerStream[1].CallbackParam = 0;
313 pInstance->BypassMixer.MixerStream[1].pCallbackHandle = LVM_NULL;
314 pInstance->BypassMixer.MixerStream[1].pCallBack = LVM_NULL;
315 LVC_Mixer_Init(&pInstance->BypassMixer.MixerStream[1], 0, 1.0f);
316 LVC_Mixer_SetTimeConstant(&pInstance->BypassMixer.MixerStream[1], 0, LVM_FS_8000, 2);
317
318 pInstance->bInOperatingModeTransition = LVM_FALSE;
319
320 return(LVEQNB_SUCCESS);
321 }
322
323