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 "LVCS.h"
25 #include "LVCS_Private.h"
26 #include "LVCS_Tables.h"
27 
28 /****************************************************************************************/
29 /*                                                                                      */
30 /* FUNCTION:                LVCS_Memory                                                 */
31 /*                                                                                      */
32 /* DESCRIPTION:                                                                         */
33 /*  This function is used for memory allocation and free. It can be called in           */
34 /*  two ways:                                                                           */
35 /*                                                                                      */
36 /*      hInstance = NULL                Returns the memory requirements                 */
37 /*      hInstance = Instance handle     Returns the memory requirements and             */
38 /*                                      allocated base addresses for the instance       */
39 /*                                                                                      */
40 /*  When this function is called for memory allocation (hInstance=NULL) it is           */
41 /*  passed the default capabilities.                                                    */
42 /*                                                                                      */
43 /*  When called for memory allocation the memory base address pointers are NULL on      */
44 /*  return.                                                                             */
45 /*                                                                                      */
46 /*  When the function is called for free (hInstance = Instance Handle) the              */
47 /*  capabilities are ignored and the memory table returns the allocated memory and      */
48 /*  base addresses used during initialisation.                                          */
49 /*                                                                                      */
50 /* PARAMETERS:                                                                          */
51 /*  hInstance               Instance Handle                                             */
52 /*  pMemoryTable            Pointer to an empty memory definition table                 */
53 /*  pCapabilities           Pointer to the default capabilites                          */
54 /*                                                                                      */
55 /* RETURNS:                                                                             */
56 /*  LVCS_Success            Succeeded                                                   */
57 /*                                                                                      */
58 /* NOTES:                                                                               */
59 /*  1.  This function may be interrupted by the LVCS_Process function                   */
60 /*                                                                                      */
61 /****************************************************************************************/
62 
LVCS_Memory(LVCS_Handle_t hInstance,LVCS_MemTab_t * pMemoryTable,LVCS_Capabilities_t * pCapabilities)63 LVCS_ReturnStatus_en LVCS_Memory(LVCS_Handle_t          hInstance,
64                                  LVCS_MemTab_t          *pMemoryTable,
65                                  LVCS_Capabilities_t    *pCapabilities)
66 {
67 
68     LVM_UINT32          ScratchSize;
69     LVCS_Instance_t     *pInstance = (LVCS_Instance_t *)hInstance;
70 
71     /*
72      * Fill in the memory table
73      */
74     if (hInstance == LVM_NULL)
75     {
76         /*
77          * Instance memory
78          */
79         pMemoryTable->Region[LVCS_MEMREGION_PERSISTENT_SLOW_DATA].Size         = (LVM_UINT32)sizeof(LVCS_Instance_t);
80         pMemoryTable->Region[LVCS_MEMREGION_PERSISTENT_SLOW_DATA].Type         = LVCS_PERSISTENT;
81         pMemoryTable->Region[LVCS_MEMREGION_PERSISTENT_SLOW_DATA].pBaseAddress = LVM_NULL;
82 
83         /*
84          * Data memory
85          */
86         pMemoryTable->Region[LVCS_MEMREGION_PERSISTENT_FAST_DATA].Size         = (LVM_UINT32)sizeof(LVCS_Data_t);
87         pMemoryTable->Region[LVCS_MEMREGION_PERSISTENT_FAST_DATA].Type         = LVCS_DATA;
88         pMemoryTable->Region[LVCS_MEMREGION_PERSISTENT_FAST_DATA].pBaseAddress = LVM_NULL;
89 
90         /*
91          * Coefficient memory
92          */
93         pMemoryTable->Region[LVCS_MEMREGION_PERSISTENT_FAST_COEF].Size         = (LVM_UINT32)sizeof(LVCS_Coefficient_t);
94         pMemoryTable->Region[LVCS_MEMREGION_PERSISTENT_FAST_COEF].Type         = LVCS_COEFFICIENT;
95         pMemoryTable->Region[LVCS_MEMREGION_PERSISTENT_FAST_COEF].pBaseAddress = LVM_NULL;
96 
97         /*
98          * Scratch memory
99          */
100         /* Inplace processing */
101         ScratchSize = (LVM_UINT32) \
102                         (LVCS_SCRATCHBUFFERS * sizeof(LVM_FLOAT) * pCapabilities->MaxBlockSize);
103         pMemoryTable->Region[LVCS_MEMREGION_TEMPORARY_FAST].Size         = ScratchSize;
104         pMemoryTable->Region[LVCS_MEMREGION_TEMPORARY_FAST].Type         = LVCS_SCRATCH;
105         pMemoryTable->Region[LVCS_MEMREGION_TEMPORARY_FAST].pBaseAddress = LVM_NULL;
106     }
107     else
108     {
109         /* Read back memory allocation table */
110         *pMemoryTable = pInstance->MemoryTable;
111     }
112 
113     return(LVCS_SUCCESS);
114 }
115 
116 /************************************************************************************/
117 /*                                                                                  */
118 /* FUNCTION:                LVCS_Init                                               */
119 /*                                                                                  */
120 /* DESCRIPTION:                                                                     */
121 /*  Create and initialisation function for the Concert Sound module                 */
122 /*                                                                                  */
123 /*  This function can be used to create an algorithm instance by calling with       */
124 /*  hInstance set to LVM_NULL. In this case the algorithm returns the new instance  */
125 /*  handle.                                                                         */
126 /*                                                                                  */
127 /*  This function can be used to force a full re-initialisation of the algorithm    */
128 /*  by calling with hInstance = Instance Handle. In this case the memory table      */
129 /*  should be correct for the instance, this can be ensured by calling the function */
130 /*  LVCS_Memory before calling this function.                                       */
131 /*                                                                                  */
132 /* PARAMETERS:                                                                      */
133 /*  hInstance               Instance handle                                         */
134 /*  pMemoryTable            Pointer to the memory definition table                  */
135 /*  pCapabilities           Pointer to the capabilities structure                   */
136 /*                                                                                  */
137 /* RETURNS:                                                                         */
138 /*  LVCS_Success            Initialisation succeeded                                */
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 LVCS_Process function          */
144 /*  3.  This function must be called with the same capabilities as used for the     */
145 /*      call to the memory function                                                 */
146 /*                                                                                  */
147 /************************************************************************************/
148 
LVCS_Init(LVCS_Handle_t * phInstance,LVCS_MemTab_t * pMemoryTable,LVCS_Capabilities_t * pCapabilities)149 LVCS_ReturnStatus_en LVCS_Init(LVCS_Handle_t         *phInstance,
150                                LVCS_MemTab_t         *pMemoryTable,
151                                LVCS_Capabilities_t   *pCapabilities)
152 {
153 
154     LVCS_Instance_t                 *pInstance;
155     LVCS_VolCorrect_t               *pLVCS_VolCorrectTable;
156 
157     /*
158      * Set the instance handle if not already initialised
159      */
160     if (*phInstance == LVM_NULL)
161     {
162         *phInstance = (LVCS_Handle_t)pMemoryTable->Region[LVCS_MEMREGION_PERSISTENT_SLOW_DATA].pBaseAddress;
163     }
164     pInstance =(LVCS_Instance_t  *)*phInstance;
165 
166     /*
167      * Save the capabilities in the instance structure
168      */
169     pInstance->Capabilities = *pCapabilities;
170 
171     /*
172      * Save the memory table in the instance structure
173      */
174     pInstance->MemoryTable = *pMemoryTable;
175 
176     /*
177      * Set all initial parameters to invalid to force a full initialisation
178      */
179     pInstance->Params.OperatingMode  = LVCS_OFF;
180     pInstance->Params.SpeakerType    = LVCS_SPEAKERTYPE_MAX;
181     pInstance->OutputDevice          = LVCS_HEADPHONE;
182     pInstance->Params.SourceFormat   = LVCS_SOURCEMAX;
183     pInstance->Params.CompressorMode = LVM_MODE_OFF;
184     pInstance->Params.SampleRate     = LVM_FS_INVALID;
185     pInstance->Params.EffectLevel    = 0;
186     pInstance->Params.ReverbLevel    = (LVM_UINT16)0x8000;
187     pLVCS_VolCorrectTable            = (LVCS_VolCorrect_t*)&LVCS_VolCorrectTable[0];
188     pInstance->VolCorrect            = pLVCS_VolCorrectTable[0];
189     pInstance->TransitionGain        = 0;
190 
191     /* These current and target values are intialized again in LVCS_Control.c */
192     LVC_Mixer_Init(&pInstance->BypassMix.Mixer_Instance.MixerStream[0],0,0);
193     /* These current and target values are intialized again in LVCS_Control.c */
194     LVC_Mixer_Init(&pInstance->BypassMix.Mixer_Instance.MixerStream[1],0,0);
195 
196     /*
197      * Initialise the bypass variables
198      */
199     pInstance->MSTarget0=0;
200     pInstance->MSTarget1=0;
201     pInstance->bInOperatingModeTransition          = LVM_FALSE;
202     pInstance->bTimerDone                        = LVM_FALSE;
203     pInstance->TimerParams.CallBackParam         = 0;
204     pInstance->TimerParams.pCallBack             = LVCS_TimerCallBack;
205     pInstance->TimerParams.pCallbackInstance     = pInstance;
206     pInstance->TimerParams.pCallBackParams       = LVM_NULL;
207 
208     return(LVCS_SUCCESS);
209 }
210 
211