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 "LVDBE.h"
25 #include "LVDBE_Private.h"
26 
27 /****************************************************************************************/
28 /*                                                                                      */
29 /* FUNCTION:                 LVDBE_Memory                                               */
30 /*                                                                                      */
31 /* DESCRIPTION:                                                                         */
32 /*    This function is used for memory allocation and free. It can be called in         */
33 /*    two ways:                                                                         */
34 /*                                                                                      */
35 /*        hInstance = NULL                Returns the memory requirements               */
36 /*        hInstance = Instance handle        Returns the memory requirements and        */
37 /*                                        allocated base addresses for the instance     */
38 /*                                                                                      */
39 /*    When this function is called for memory allocation (hInstance=NULL) the memory    */
40 /*  base address pointers are NULL on return.                                           */
41 /*                                                                                      */
42 /*    When the function is called for free (hInstance = Instance Handle) the memory     */
43 /*  table returns the allocated memory and base addresses used during initialisation.   */
44 /*                                                                                      */
45 /* PARAMETERS:                                                                          */
46 /*  hInstance                Instance Handle                                            */
47 /*  pMemoryTable             Pointer to an empty memory definition table                */
48 /*    pCapabilities           Pointer to the instance capabilities                      */
49 /*                                                                                      */
50 /* RETURNS:                                                                             */
51 /*  LVDBE_SUCCESS            Succeeded                                                  */
52 /*                                                                                      */
53 /* NOTES:                                                                               */
54 /*    1.    This function may be interrupted by the LVDBE_Process function              */
55 /*                                                                                      */
56 /****************************************************************************************/
57 
LVDBE_Memory(LVDBE_Handle_t hInstance,LVDBE_MemTab_t * pMemoryTable,LVDBE_Capabilities_t * pCapabilities)58 LVDBE_ReturnStatus_en LVDBE_Memory(LVDBE_Handle_t            hInstance,
59                                    LVDBE_MemTab_t            *pMemoryTable,
60                                    LVDBE_Capabilities_t      *pCapabilities)
61 {
62 
63     LVM_UINT32          ScratchSize;
64     LVDBE_Instance_t    *pInstance = (LVDBE_Instance_t *)hInstance;
65 
66     /*
67      * Fill in the memory table
68      */
69     if (hInstance == LVM_NULL)
70     {
71         /*
72          * Instance memory
73          */
74         pMemoryTable->Region[LVDBE_MEMREGION_INSTANCE].Size         = sizeof(LVDBE_Instance_t);
75         pMemoryTable->Region[LVDBE_MEMREGION_INSTANCE].Alignment    = LVDBE_INSTANCE_ALIGN;
76         pMemoryTable->Region[LVDBE_MEMREGION_INSTANCE].Type         = LVDBE_PERSISTENT;
77         pMemoryTable->Region[LVDBE_MEMREGION_INSTANCE].pBaseAddress = LVM_NULL;
78 
79         /*
80          * Data memory
81          */
82         pMemoryTable->Region[LVDBE_MEMREGION_PERSISTENT_DATA].Size   = sizeof(LVDBE_Data_FLOAT_t);
83         pMemoryTable->Region[LVDBE_MEMREGION_PERSISTENT_DATA].Alignment    = LVDBE_PERSISTENT_DATA_ALIGN;
84         pMemoryTable->Region[LVDBE_MEMREGION_PERSISTENT_DATA].Type         = LVDBE_PERSISTENT_DATA;
85         pMemoryTable->Region[LVDBE_MEMREGION_PERSISTENT_DATA].pBaseAddress = LVM_NULL;
86 
87         /*
88          * Coef memory
89          */
90         pMemoryTable->Region[LVDBE_MEMREGION_PERSISTENT_COEF].Size   = sizeof(LVDBE_Coef_FLOAT_t);
91         pMemoryTable->Region[LVDBE_MEMREGION_PERSISTENT_COEF].Alignment    = LVDBE_PERSISTENT_COEF_ALIGN;
92         pMemoryTable->Region[LVDBE_MEMREGION_PERSISTENT_COEF].Type         = LVDBE_PERSISTENT_COEF;
93         pMemoryTable->Region[LVDBE_MEMREGION_PERSISTENT_COEF].pBaseAddress = LVM_NULL;
94 
95         /*
96          * Scratch memory
97          */
98         ScratchSize = (LVM_UINT32)(LVDBE_SCRATCHBUFFERS_INPLACE*sizeof(LVM_FLOAT) * \
99                                         pCapabilities->MaxBlockSize);
100         pMemoryTable->Region[LVDBE_MEMREGION_SCRATCH].Size         = ScratchSize;
101         pMemoryTable->Region[LVDBE_MEMREGION_SCRATCH].Alignment    = LVDBE_SCRATCH_ALIGN;
102         pMemoryTable->Region[LVDBE_MEMREGION_SCRATCH].Type         = LVDBE_SCRATCH;
103         pMemoryTable->Region[LVDBE_MEMREGION_SCRATCH].pBaseAddress = LVM_NULL;
104     }
105     else
106     {
107         /* Read back memory allocation table */
108         *pMemoryTable = pInstance->MemoryTable;
109     }
110 
111     return(LVDBE_SUCCESS);
112 }
113 
114 /****************************************************************************************/
115 /*                                                                                      */
116 /* FUNCTION:                 LVDBE_Init                                                 */
117 /*                                                                                      */
118 /* DESCRIPTION:                                                                         */
119 /*    Create and initialisation function for the Dynamic Bass Enhancement module        */
120 /*                                                                                      */
121 /*    This function can be used to create an algorithm instance by calling with         */
122 /*    hInstance set to NULL. In this case the algorithm returns the new instance        */
123 /*    handle.                                                                           */
124 /*                                                                                      */
125 /*    This function can be used to force a full re-initialisation of the algorithm      */
126 /*    by calling with hInstance = Instance Handle. In this case the memory table        */
127 /*    should be correct for the instance, this can be ensured by calling the function   */
128 /*    DBE_Memory before calling this function.                                          */
129 /*                                                                                      */
130 /* PARAMETERS:                                                                          */
131 /*  hInstance                  Instance handle                                          */
132 /*  pMemoryTable             Pointer to the memory definition table                     */
133 /*  pCapabilities              Pointer to the instance capabilities                     */
134 /*                                                                                      */
135 /* RETURNS:                                                                             */
136 /*  LVDBE_SUCCESS            Initialisation succeeded                                   */
137 /*  LVDBE_ALIGNMENTERROR    Instance or scratch memory on incorrect alignment           */
138 /*    LVDBE_NULLADDRESS        Instance or scratch memory has a NULL pointer            */
139 /*                                                                                      */
140 /* NOTES:                                                                               */
141 /*  1.     The instance handle is the pointer to the base address of the first memory   */
142 /*        region.                                                                       */
143 /*    2.    This function must not be interrupted by the LVDBE_Process function         */
144 /*                                                                                      */
145 /****************************************************************************************/
146 
LVDBE_Init(LVDBE_Handle_t * phInstance,LVDBE_MemTab_t * pMemoryTable,LVDBE_Capabilities_t * pCapabilities)147 LVDBE_ReturnStatus_en LVDBE_Init(LVDBE_Handle_t         *phInstance,
148                                    LVDBE_MemTab_t       *pMemoryTable,
149                                    LVDBE_Capabilities_t *pCapabilities)
150 {
151 
152     LVDBE_Instance_t      *pInstance;
153     LVMixer3_1St_FLOAT_st       *pMixer_Instance;
154     LVMixer3_2St_FLOAT_st       *pBypassMixer_Instance;
155     LVM_FLOAT             MixGain;
156     LVM_INT16             i;
157 
158     /*
159      * Set the instance handle if not already initialised
160      */
161     if (*phInstance == LVM_NULL)
162     {
163         *phInstance = (LVDBE_Handle_t)pMemoryTable->Region[LVDBE_MEMREGION_INSTANCE].pBaseAddress;
164     }
165     pInstance =(LVDBE_Instance_t  *)*phInstance;
166 
167     /*
168      * Check the memory table for NULL pointers and incorrectly aligned data
169      */
170     for (i=0; i<LVDBE_NR_MEMORY_REGIONS; i++)
171     {
172         if (pMemoryTable->Region[i].Size!=0)
173         {
174             if (pMemoryTable->Region[i].pBaseAddress==LVM_NULL)
175             {
176                 return(LVDBE_NULLADDRESS);
177             }
178             if (((uintptr_t)pMemoryTable->Region[i].pBaseAddress % pMemoryTable->Region[i].Alignment)!=0){
179                 return(LVDBE_ALIGNMENTERROR);
180             }
181         }
182     }
183 
184     /*
185      * Save the memory table in the instance structure
186      */
187     pInstance->Capabilities = *pCapabilities;
188 
189     /*
190      * Save the memory table in the instance structure
191      */
192     pInstance->MemoryTable = *pMemoryTable;
193 
194     /*
195      * Set the default instance parameters
196      */
197     pInstance->Params.CentreFrequency   =    LVDBE_CENTRE_55HZ;
198     pInstance->Params.EffectLevel       =    0;
199     pInstance->Params.HeadroomdB        =    0;
200     pInstance->Params.HPFSelect         =    LVDBE_HPF_OFF;
201     pInstance->Params.OperatingMode     =    LVDBE_OFF;
202     pInstance->Params.SampleRate        =    LVDBE_FS_8000;
203     pInstance->Params.VolumeControl     =    LVDBE_VOLUME_OFF;
204     pInstance->Params.VolumedB          =    0;
205 
206     /*
207      * Set pointer to data and coef memory
208      */
209     pInstance->pData =
210          (LVDBE_Data_FLOAT_t *)pMemoryTable->Region[LVDBE_MEMREGION_PERSISTENT_DATA].pBaseAddress;
211     pInstance->pCoef =
212          (LVDBE_Coef_FLOAT_t *)pMemoryTable->Region[LVDBE_MEMREGION_PERSISTENT_COEF].pBaseAddress;
213 
214     /*
215      * Initialise the filters
216      */
217     LVDBE_SetFilters(pInstance,                 /* Set the filter taps and coefficients */
218                      &pInstance->Params);
219 
220     /*
221      * Initialise the AGC
222      */
223     LVDBE_SetAGC(pInstance,                                     /* Set the AGC gain */
224                  &pInstance->Params);
225     pInstance->pData->AGCInstance.AGC_Gain = pInstance->pData->AGCInstance.AGC_MaxGain;
226                                                 /* Default to the bass boost setting */
227 
228     // initialize the mixer with some fixes values since otherwise LVDBE_SetVolume ends up
229     // reading uninitialized data
230     pMixer_Instance = &pInstance->pData->BypassVolume;
231     LVC_Mixer_Init(&pMixer_Instance->MixerStream[0], 1.0, 1.0);
232 
233     /*
234      * Initialise the volume
235      */
236     LVDBE_SetVolume(pInstance,                                         /* Set the Volume */
237                     &pInstance->Params);
238 
239     pInstance->pData->AGCInstance.Volume = pInstance->pData->AGCInstance.Target;
240                                                 /* Initialise as the target */
241     MixGain = LVC_Mixer_GetTarget(&pMixer_Instance->MixerStream[0]);
242     LVC_Mixer_Init(&pMixer_Instance->MixerStream[0], MixGain, MixGain);
243 
244     /* Configure the mixer process path */
245     pMixer_Instance->MixerStream[0].CallbackParam = 0;
246     pMixer_Instance->MixerStream[0].pCallbackHandle = LVM_NULL;
247     pMixer_Instance->MixerStream[0].pCallBack = LVM_NULL;
248     pMixer_Instance->MixerStream[0].CallbackSet = 0;
249 
250     /*
251      * Initialise the clicks minimisation BypassMixer
252      */
253 
254     pBypassMixer_Instance = &pInstance->pData->BypassMixer;
255 
256     /*
257      * Setup the mixer gain for the processed path
258      */
259     pBypassMixer_Instance->MixerStream[0].CallbackParam = 0;
260     pBypassMixer_Instance->MixerStream[0].pCallbackHandle = LVM_NULL;
261     pBypassMixer_Instance->MixerStream[0].pCallBack = LVM_NULL;
262     pBypassMixer_Instance->MixerStream[0].CallbackSet=0;
263 
264     LVC_Mixer_Init(&pBypassMixer_Instance->MixerStream[0],0,0);
265     LVC_Mixer_SetTimeConstant(&pBypassMixer_Instance->MixerStream[0],
266         LVDBE_BYPASS_MIXER_TC,(LVM_Fs_en)pInstance->Params.SampleRate,2);
267 
268     /*
269      * Setup the mixer gain for the unprocessed path
270      */
271     pBypassMixer_Instance->MixerStream[1].CallbackParam = 0;
272     pBypassMixer_Instance->MixerStream[1].pCallbackHandle = LVM_NULL;
273     pBypassMixer_Instance->MixerStream[1].pCallBack = LVM_NULL;
274     pBypassMixer_Instance->MixerStream[1].CallbackSet=0;
275     LVC_Mixer_Init(&pBypassMixer_Instance->MixerStream[1], 1.0, 1.0);
276     LVC_Mixer_SetTimeConstant(&pBypassMixer_Instance->MixerStream[1],
277         LVDBE_BYPASS_MIXER_TC,(LVM_Fs_en)pInstance->Params.SampleRate, 2);
278 
279     return(LVDBE_SUCCESS);
280 }
281