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 #include "LVREV_Private.h"
24 #include "InstAlloc.h"
25 
26 
27 /****************************************************************************************/
28 /*                                                                                      */
29 /* FUNCTION:                LVREV_GetInstanceHandle                                     */
30 /*                                                                                      */
31 /* DESCRIPTION:                                                                         */
32 /*  This function is used to create a LVREV module instance. It returns the created     */
33 /*  instance handle through phInstance. All parameters are set to their default,        */
34 /*  inactive state.                                                                     */
35 /*                                                                                      */
36 /* PARAMETERS:                                                                          */
37 /*  phInstance              pointer to the instance handle                              */
38 /*  pMemoryTable            Pointer to the memory definition table                      */
39 /*  pInstanceParams         Pointer to the instance parameters                          */
40 /*                                                                                      */
41 /* RETURNS:                                                                             */
42 /*  LVREV_SUCCESS           Succeeded                                                   */
43 /*  LVREV_NULLADDRESS       When phInstance or pMemoryTable or pInstanceParams is NULL  */
44 /*  LVREV_NULLADDRESS       When one of the memory regions has a NULL pointer           */
45 /*                                                                                      */
46 /* NOTES:                                                                               */
47 /*                                                                                      */
48 /****************************************************************************************/
LVREV_GetInstanceHandle(LVREV_Handle_t * phInstance,LVREV_MemoryTable_st * pMemoryTable,LVREV_InstanceParams_st * pInstanceParams)49 LVREV_ReturnStatus_en LVREV_GetInstanceHandle(LVREV_Handle_t            *phInstance,
50                                               LVREV_MemoryTable_st      *pMemoryTable,
51                                               LVREV_InstanceParams_st   *pInstanceParams)
52 {
53 
54     INST_ALLOC              SlowData;
55     INST_ALLOC              FastData;
56     INST_ALLOC              FastCoef;
57     INST_ALLOC              Temporary;
58     LVREV_Instance_st       *pLVREV_Private;
59     LVM_INT16               i;
60     LVM_UINT16              MaxBlockSize;
61 
62 
63     /*
64      * Check for error conditions
65      */
66     /* Check for NULL pointers */
67     if((phInstance == LVM_NULL) || (pMemoryTable == LVM_NULL) || (pInstanceParams == LVM_NULL))
68     {
69         return LVREV_NULLADDRESS;
70     }
71     /* Check the memory table for NULL pointers */
72     for (i = 0; i < LVREV_NR_MEMORY_REGIONS; i++)
73     {
74         if (pMemoryTable->Region[i].Size!=0)
75         {
76             if (pMemoryTable->Region[i].pBaseAddress==LVM_NULL)
77             {
78                 return(LVREV_NULLADDRESS);
79             }
80         }
81     }
82 
83     /*
84      * Check all instance parameters are in range
85      */
86     /* Check for a non-zero block size */
87     if (pInstanceParams->MaxBlockSize == 0)
88     {
89         return LVREV_OUTOFRANGE;
90     }
91 
92     /* Check for a valid number of delay lines */
93     if ((pInstanceParams->NumDelays != LVREV_DELAYLINES_1)&&
94         (pInstanceParams->NumDelays != LVREV_DELAYLINES_2)&&
95         (pInstanceParams->NumDelays != LVREV_DELAYLINES_4))
96     {
97         return LVREV_OUTOFRANGE;
98     }
99 
100     /*
101      * Initialise the InstAlloc instances
102      */
103     InstAlloc_Init(&SlowData,  pMemoryTable->Region[LVM_PERSISTENT_SLOW_DATA].pBaseAddress);
104     InstAlloc_Init(&FastData,  pMemoryTable->Region[LVM_PERSISTENT_FAST_DATA].pBaseAddress);
105     InstAlloc_Init(&FastCoef,  pMemoryTable->Region[LVM_PERSISTENT_FAST_COEF].pBaseAddress);
106     InstAlloc_Init(&Temporary, pMemoryTable->Region[LVM_TEMPORARY_FAST].pBaseAddress);
107 
108     /*
109      * Zero all memory regions
110      */
111      LoadConst_16(0, (LVM_INT16 *)pMemoryTable->Region[LVM_PERSISTENT_SLOW_DATA].pBaseAddress, (LVM_INT16)((pMemoryTable->Region[LVM_PERSISTENT_SLOW_DATA].Size)/sizeof(LVM_INT16)));
112      LoadConst_16(0, (LVM_INT16 *)pMemoryTable->Region[LVM_PERSISTENT_FAST_DATA].pBaseAddress, (LVM_INT16)((pMemoryTable->Region[LVM_PERSISTENT_FAST_DATA].Size)/sizeof(LVM_INT16)));
113      LoadConst_16(0, (LVM_INT16 *)pMemoryTable->Region[LVM_PERSISTENT_FAST_COEF].pBaseAddress, (LVM_INT16)((pMemoryTable->Region[LVM_PERSISTENT_FAST_COEF].Size)/sizeof(LVM_INT16)));
114      LoadConst_16(0, (LVM_INT16 *)pMemoryTable->Region[LVM_TEMPORARY_FAST].pBaseAddress, (LVM_INT16)((pMemoryTable->Region[LVM_TEMPORARY_FAST].Size)/sizeof(LVM_INT16)));
115 
116     /*
117      * Set the instance handle if not already initialised
118      */
119     if (*phInstance == LVM_NULL)
120     {
121         *phInstance = InstAlloc_AddMember(&SlowData, sizeof(LVREV_Instance_st));
122     }
123     pLVREV_Private              =(LVREV_Instance_st *)*phInstance;
124     pLVREV_Private->MemoryTable = *pMemoryTable;
125 
126     if(pInstanceParams->NumDelays ==LVREV_DELAYLINES_4)
127     {
128         MaxBlockSize = LVREV_MAX_AP3_DELAY;
129     }
130     else if(pInstanceParams->NumDelays ==LVREV_DELAYLINES_2)
131     {
132         MaxBlockSize = LVREV_MAX_AP1_DELAY;
133     }
134     else
135     {
136         MaxBlockSize = LVREV_MAX_AP0_DELAY;
137     }
138 
139     if(MaxBlockSize>pInstanceParams->MaxBlockSize)
140     {
141         MaxBlockSize=pInstanceParams->MaxBlockSize;
142     }
143 
144 
145     /*
146      * Set the data, coefficient and temporary memory pointers
147      */
148     pLVREV_Private->pFastData = InstAlloc_AddMember(&FastData, sizeof(LVREV_FastData_st));                              /* Fast data memory base address */
149 
150     if(pInstanceParams->NumDelays == LVREV_DELAYLINES_4)
151     {
152         pLVREV_Private->pDelay_T[3]     = InstAlloc_AddMember(&FastData, LVREV_MAX_T3_DELAY  * sizeof(LVM_INT32));
153         pLVREV_Private->pDelay_T[2]     = InstAlloc_AddMember(&FastData, LVREV_MAX_T2_DELAY  * sizeof(LVM_INT32));
154         pLVREV_Private->pDelay_T[1]     = InstAlloc_AddMember(&FastData, LVREV_MAX_T1_DELAY * sizeof(LVM_INT32));
155         pLVREV_Private->pDelay_T[0]     = InstAlloc_AddMember(&FastData, LVREV_MAX_T0_DELAY * sizeof(LVM_INT32));
156 
157         for( i = 0; i < 4; i++)
158         {
159             pLVREV_Private->pScratchDelayLine[i] = InstAlloc_AddMember(&Temporary, sizeof(LVM_INT32) * MaxBlockSize);       /* Scratch for each delay line output */
160         }
161 
162         LoadConst_32(0,pLVREV_Private->pDelay_T[3]  ,(LVM_INT16)LVREV_MAX_T3_DELAY);
163         LoadConst_32(0,pLVREV_Private->pDelay_T[2]  ,(LVM_INT16)LVREV_MAX_T2_DELAY);
164         LoadConst_32(0,pLVREV_Private->pDelay_T[1]  ,(LVM_INT16)LVREV_MAX_T1_DELAY);
165         LoadConst_32(0,pLVREV_Private->pDelay_T[0]  ,(LVM_INT16)LVREV_MAX_T0_DELAY);
166     }
167 
168     if(pInstanceParams->NumDelays == LVREV_DELAYLINES_2)
169     {
170         pLVREV_Private->pDelay_T[1]  = InstAlloc_AddMember(&FastData, LVREV_MAX_T1_DELAY * sizeof(LVM_INT32));
171         pLVREV_Private->pDelay_T[0]  = InstAlloc_AddMember(&FastData, LVREV_MAX_T0_DELAY * sizeof(LVM_INT32));
172 
173         for( i = 0; i < 2; i++)
174         {
175             pLVREV_Private->pScratchDelayLine[i] = InstAlloc_AddMember(&Temporary, sizeof(LVM_INT32) * MaxBlockSize);       /* Scratch for each delay line output */
176         }
177 
178         LoadConst_32(0,pLVREV_Private->pDelay_T[1] , (LVM_INT16)LVREV_MAX_T1_DELAY);
179         LoadConst_32(0,pLVREV_Private->pDelay_T[0] , (LVM_INT16)LVREV_MAX_T0_DELAY);
180     }
181 
182     if(pInstanceParams->NumDelays == LVREV_DELAYLINES_1)
183     {
184         pLVREV_Private->pDelay_T[0]  = InstAlloc_AddMember(&FastData, LVREV_MAX_T0_DELAY * sizeof(LVM_INT32));
185 
186         for( i = 0; i < 1; i++)
187         {
188             pLVREV_Private->pScratchDelayLine[i] = InstAlloc_AddMember(&Temporary, sizeof(LVM_INT32) * MaxBlockSize);       /* Scratch for each delay line output */
189         }
190 
191         LoadConst_32(0,pLVREV_Private->pDelay_T[0]  , (LVM_INT16)LVREV_MAX_T0_DELAY);
192     }
193 
194     /* All-pass delay buffer addresses and sizes */
195     pLVREV_Private->T[0]         = LVREV_MAX_T0_DELAY;
196     pLVREV_Private->T[1]         = LVREV_MAX_T1_DELAY;
197     pLVREV_Private->T[2]         = LVREV_MAX_T2_DELAY;
198     pLVREV_Private->T[3]         = LVREV_MAX_T3_DELAY;
199     pLVREV_Private->AB_Selection = 1;       /* Select smoothing A to B */
200 
201 
202     pLVREV_Private->pFastCoef       = InstAlloc_AddMember(&FastCoef, sizeof(LVREV_FastCoef_st));                        /* Fast coefficient memory base address */
203     pLVREV_Private->pScratch        = InstAlloc_AddMember(&Temporary, sizeof(LVM_INT32) * MaxBlockSize);                /* General purpose scratch */
204     pLVREV_Private->pInputSave      = InstAlloc_AddMember(&Temporary, 2 * sizeof(LVM_INT32) * MaxBlockSize);            /* Mono->stereo input save for end mix */
205     LoadConst_32(0, pLVREV_Private->pInputSave, (LVM_INT16)(MaxBlockSize*2));
206 
207 
208     /*
209      * Save the instance parameters in the instance structure
210      */
211     pLVREV_Private->InstanceParams = *pInstanceParams;
212 
213 
214     /*
215      * Set the parameters to invalid
216      */
217     pLVREV_Private->CurrentParams.SampleRate    = LVM_FS_INVALID;
218     pLVREV_Private->CurrentParams.OperatingMode = LVM_MODE_DUMMY;
219     pLVREV_Private->CurrentParams.SourceFormat  = LVM_SOURCE_DUMMY;
220 
221     pLVREV_Private->bControlPending             = LVM_FALSE;
222     pLVREV_Private->bFirstControl               = LVM_TRUE;
223     pLVREV_Private->bDisableReverb              = LVM_FALSE;
224 
225 
226     /*
227      * Set mixer parameters
228      */
229     pLVREV_Private->BypassMixer.CallbackParam2      = 0;
230     pLVREV_Private->BypassMixer.pCallbackHandle2    = pLVREV_Private;
231     pLVREV_Private->BypassMixer.pGeneralPurpose2    = LVM_NULL;
232     pLVREV_Private->BypassMixer.pCallBack2          = BypassMixer_Callback;
233     pLVREV_Private->BypassMixer.CallbackSet2        = LVM_FALSE;
234     pLVREV_Private->BypassMixer.Current2            = 0;
235     pLVREV_Private->BypassMixer.Target2             = 0;
236     pLVREV_Private->BypassMixer.CallbackParam1      = 0;
237     pLVREV_Private->BypassMixer.pCallbackHandle1    = LVM_NULL;
238     pLVREV_Private->BypassMixer.pGeneralPurpose1    = LVM_NULL;
239     pLVREV_Private->BypassMixer.pCallBack1          = LVM_NULL;
240     pLVREV_Private->BypassMixer.CallbackSet1        = LVM_FALSE;
241     pLVREV_Private->BypassMixer.Current1            = 0x00000000;
242     pLVREV_Private->BypassMixer.Target1             = 0x00000000;
243 
244     pLVREV_Private->RoomSizeInms                    = 100;  // 100 msec
245 
246 
247     /*
248      *  Set the output gain mixer parameters
249      */
250     pLVREV_Private->GainMixer.CallbackParam      = 0;
251     pLVREV_Private->GainMixer.pCallbackHandle    = LVM_NULL;
252     pLVREV_Private->GainMixer.pGeneralPurpose    = LVM_NULL;
253     pLVREV_Private->GainMixer.pCallBack          = LVM_NULL;
254     pLVREV_Private->GainMixer.CallbackSet        = LVM_FALSE;
255     pLVREV_Private->GainMixer.Current            = 0x03ffffff;
256     pLVREV_Private->GainMixer.Target             = 0x03ffffff;
257 
258 
259     /*
260      * Set the All-Pass Filter mixers
261      */
262     for (i=0; i<4; i++)
263     {
264         pLVREV_Private->pOffsetA[i] = pLVREV_Private->pDelay_T[i];
265         pLVREV_Private->pOffsetB[i] = pLVREV_Private->pDelay_T[i];
266         /* Delay tap selection mixer */
267         pLVREV_Private->Mixer_APTaps[i].CallbackParam2   = 0;
268         pLVREV_Private->Mixer_APTaps[i].pCallbackHandle2 = LVM_NULL;
269         pLVREV_Private->Mixer_APTaps[i].pGeneralPurpose2 = LVM_NULL;
270         pLVREV_Private->Mixer_APTaps[i].pCallBack2       = LVM_NULL;
271         pLVREV_Private->Mixer_APTaps[i].CallbackSet2     = LVM_FALSE;
272         pLVREV_Private->Mixer_APTaps[i].Current2         = 0;
273         pLVREV_Private->Mixer_APTaps[i].Target2          = 0;
274         pLVREV_Private->Mixer_APTaps[i].CallbackParam1   = 0;
275         pLVREV_Private->Mixer_APTaps[i].pCallbackHandle1 = LVM_NULL;
276         pLVREV_Private->Mixer_APTaps[i].pGeneralPurpose1 = LVM_NULL;
277         pLVREV_Private->Mixer_APTaps[i].pCallBack1       = LVM_NULL;
278         pLVREV_Private->Mixer_APTaps[i].CallbackSet1     = LVM_FALSE;
279         pLVREV_Private->Mixer_APTaps[i].Current1         = 0;
280         pLVREV_Private->Mixer_APTaps[i].Target1          = 0x7fffffff;
281         /* Feedforward mixer */
282         pLVREV_Private->Mixer_SGFeedforward[i].CallbackParam   = 0;
283         pLVREV_Private->Mixer_SGFeedforward[i].pCallbackHandle = LVM_NULL;
284         pLVREV_Private->Mixer_SGFeedforward[i].pGeneralPurpose = LVM_NULL;
285         pLVREV_Private->Mixer_SGFeedforward[i].pCallBack       = LVM_NULL;
286         pLVREV_Private->Mixer_SGFeedforward[i].CallbackSet     = LVM_FALSE;
287         pLVREV_Private->Mixer_SGFeedforward[i].Current         = 0;
288         pLVREV_Private->Mixer_SGFeedforward[i].Target          = 0;
289         /* Feedback mixer */
290         pLVREV_Private->Mixer_SGFeedback[i].CallbackParam   = 0;
291         pLVREV_Private->Mixer_SGFeedback[i].pCallbackHandle = LVM_NULL;
292         pLVREV_Private->Mixer_SGFeedback[i].pGeneralPurpose = LVM_NULL;
293         pLVREV_Private->Mixer_SGFeedback[i].pCallBack       = LVM_NULL;
294         pLVREV_Private->Mixer_SGFeedback[i].CallbackSet     = LVM_FALSE;
295         pLVREV_Private->Mixer_SGFeedback[i].Current         = 0;
296         pLVREV_Private->Mixer_SGFeedback[i].Target          = 0;
297         /* Feedback gain mixer */
298         pLVREV_Private->FeedbackMixer[i].CallbackParam    = 0;
299         pLVREV_Private->FeedbackMixer[i].pCallbackHandle  = LVM_NULL;
300         pLVREV_Private->FeedbackMixer[i].pGeneralPurpose  = LVM_NULL;
301         pLVREV_Private->FeedbackMixer[i].pCallBack        = LVM_NULL;
302         pLVREV_Private->FeedbackMixer[i].CallbackSet      = LVM_FALSE;
303         pLVREV_Private->FeedbackMixer[i].Current          = 0;
304         pLVREV_Private->FeedbackMixer[i].Target           = 0;
305     }
306     /* Delay tap index */
307     pLVREV_Private->A_DelaySize[0] = LVREV_MAX_AP0_DELAY;
308     pLVREV_Private->B_DelaySize[0] = LVREV_MAX_AP0_DELAY;
309     pLVREV_Private->A_DelaySize[1] = LVREV_MAX_AP1_DELAY;
310     pLVREV_Private->B_DelaySize[1] = LVREV_MAX_AP1_DELAY;
311     pLVREV_Private->A_DelaySize[2] = LVREV_MAX_AP2_DELAY;
312     pLVREV_Private->B_DelaySize[2] = LVREV_MAX_AP2_DELAY;
313     pLVREV_Private->A_DelaySize[3] = LVREV_MAX_AP3_DELAY;
314     pLVREV_Private->B_DelaySize[3] = LVREV_MAX_AP3_DELAY;
315 
316 
317     LVREV_ClearAudioBuffers(*phInstance);
318 
319     return LVREV_SUCCESS;
320 }
321 
322 /* End of file */
323