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_INT16            LVPSA_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 
87     /*Inst_Alloc instances initialization */
88     InstAlloc_Init( &Instance   , pMemoryTable->Region[LVPSA_MEMREGION_INSTANCE].pBaseAddress);
89     InstAlloc_Init( &Scratch    , pMemoryTable->Region[LVPSA_MEMREGION_SCRATCH].pBaseAddress);
90     InstAlloc_Init( &Data       , pMemoryTable->Region[LVPSA_MEMREGION_PERSISTENT_DATA].pBaseAddress);
91     InstAlloc_Init( &Coef       , pMemoryTable->Region[LVPSA_MEMREGION_PERSISTENT_COEF].pBaseAddress);
92 
93 
94     /* Set the instance handle if not already initialised */
95     if (*phInstance == LVM_NULL)
96     {
97         *phInstance = InstAlloc_AddMember( &Instance, sizeof(LVPSA_InstancePr_t) );
98     }
99     pLVPSA_Inst =(LVPSA_InstancePr_t*)*phInstance;
100 
101 
102     /* Check the memory table for NULL pointers */
103     for (ii = 0; ii < LVPSA_NR_MEMORY_REGIONS; ii++)
104     {
105         if (pMemoryTable->Region[ii].Size!=0)
106         {
107             if (pMemoryTable->Region[ii].pBaseAddress==LVM_NULL)
108             {
109                 return(LVPSA_ERROR_NULLADDRESS);
110             }
111             pLVPSA_Inst->MemoryTable.Region[ii] = pMemoryTable->Region[ii];
112         }
113     }
114 
115     /* Initialize module's internal parameters */
116     pLVPSA_Inst->bControlPending = LVM_FALSE;
117     pLVPSA_Inst->nBands = pInitParams->nBands;
118     pLVPSA_Inst->MaxInputBlockSize = pInitParams->MaxInputBlockSize;
119     pLVPSA_Inst->SpectralDataBufferDuration = pInitParams->SpectralDataBufferDuration;
120     pLVPSA_Inst->CurrentParams.Fs = LVM_FS_DUMMY;
121     pLVPSA_Inst->CurrentParams.LevelDetectionSpeed = LVPSA_SPEED_DUMMY;
122 
123     {   /* for avoiding QAC warnings */
124         LVM_INT32 SDBD=(LVM_INT32)pLVPSA_Inst->SpectralDataBufferDuration;
125         LVM_INT32 IRTI=(LVM_INT32)LVPSA_InternalRefreshTimeInv;
126         LVM_INT32 BL;
127 
128         MUL32x32INTO32(SDBD,IRTI,BL,LVPSA_InternalRefreshTimeShift)
129 
130         BufferLength=(LVM_UINT32)BL;
131     }
132 
133     if((BufferLength * LVPSA_InternalRefreshTime) != pLVPSA_Inst->SpectralDataBufferDuration)
134     {
135         pLVPSA_Inst->SpectralDataBufferLength = BufferLength + 1;
136     }
137     else
138     {
139         pLVPSA_Inst->SpectralDataBufferLength = BufferLength;
140     }
141 
142 
143     /* Assign the pointers */
144 
145     pLVPSA_Inst->pPostGains                 = InstAlloc_AddMember( &Instance, pInitParams->nBands * sizeof(LVM_UINT16) );
146     pLVPSA_Inst->pFiltersParams             = InstAlloc_AddMember( &Instance, pInitParams->nBands * sizeof(LVPSA_FilterParam_t) );
147     pLVPSA_Inst->pSpectralDataBufferStart   = InstAlloc_AddMember( &Instance, pInitParams->nBands * pLVPSA_Inst->SpectralDataBufferLength * sizeof(LVM_UINT8) );
148     pLVPSA_Inst->pPreviousPeaks             = InstAlloc_AddMember( &Instance, pInitParams->nBands * sizeof(LVM_UINT8) );
149     pLVPSA_Inst->pBPFiltersPrecision        = InstAlloc_AddMember( &Instance, pInitParams->nBands * sizeof(LVPSA_BPFilterPrecision_en) );
150 
151     pLVPSA_Inst->pBP_Instances          = InstAlloc_AddMember( &Coef, pInitParams->nBands * sizeof(Biquad_Instance_t) );
152     pLVPSA_Inst->pQPD_States            = InstAlloc_AddMember( &Coef, pInitParams->nBands * sizeof(QPD_State_t) );
153 
154     pLVPSA_Inst->pBP_Taps               = InstAlloc_AddMember( &Data, pInitParams->nBands * sizeof(Biquad_1I_Order2_Taps_t) );
155     pLVPSA_Inst->pQPD_Taps              = InstAlloc_AddMember( &Data, pInitParams->nBands * sizeof(QPD_Taps_t) );
156 
157 
158     /* Copy filters parameters in the private instance */
159     for(ii = 0; ii < pLVPSA_Inst->nBands; ii++)
160     {
161         pLVPSA_Inst->pFiltersParams[ii] = pInitParams->pFiltersParams[ii];
162     }
163 
164     /* Set Post filters gains*/
165     for(ii = 0; ii < pLVPSA_Inst->nBands; ii++)
166     {
167         pLVPSA_Inst->pPostGains[ii] =(LVM_UINT16) LVPSA_GainTable[pInitParams->pFiltersParams[ii].PostGain + 15];
168     }
169     pLVPSA_Inst->pSpectralDataBufferWritePointer = pLVPSA_Inst->pSpectralDataBufferStart;
170 
171 
172     /* Initialize control dependant internal parameters */
173     errorCode = LVPSA_Control (*phInstance, pControlParams);
174 
175     if(errorCode!=0)
176     {
177         return errorCode;
178     }
179 
180     errorCode = LVPSA_ApplyNewSettings (pLVPSA_Inst);
181 
182     if(errorCode!=0)
183     {
184         return errorCode;
185     }
186 
187     return(errorCode);
188 }
189 
190