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 <stdlib.h>
19 #include "LVPSA.h"
20 #include "LVPSA_Private.h"
21
22 /************************************************************************************/
23 /* */
24 /* FUNCTION: LVPSA_Init */
25 /* */
26 /* DESCRIPTION: */
27 /* Create and Initialize the LVPSA module including instance handle */
28 /* */
29 /* */
30 /* PARAMETERS: */
31 /* phInstance Pointer to the instance handle */
32 /* InitParams Init parameters structure */
33 /* ControlParams Control parameters structure */
34 /* pScratch Pointer to bundle scratch memory area */
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,void * pScratch)42 LVPSA_RETURN LVPSA_Init(pLVPSA_Handle_t* phInstance, LVPSA_InitParams_t* pInitParams,
43 LVPSA_ControlParams_t* pControlParams, void* pScratch) {
44 LVPSA_InstancePr_t* pLVPSA_Inst;
45 LVPSA_RETURN errorCode = LVPSA_OK;
46 LVM_UINT32 ii;
47 extern LVM_FLOAT LVPSA_Float_GainTable[];
48 LVM_UINT32 BufferLength = 0;
49
50 /* Set the instance handle if not already initialised */
51 *phInstance = new LVPSA_InstancePr_t{};
52 pLVPSA_Inst = (LVPSA_InstancePr_t*)*phInstance;
53
54 pLVPSA_Inst->pScratch = pScratch;
55
56 /* Initialize module's internal parameters */
57 pLVPSA_Inst->bControlPending = LVM_FALSE;
58 pLVPSA_Inst->nBands = pInitParams->nBands;
59 pLVPSA_Inst->MaxInputBlockSize = pInitParams->MaxInputBlockSize;
60 pLVPSA_Inst->SpectralDataBufferDuration = pInitParams->SpectralDataBufferDuration;
61 pLVPSA_Inst->CurrentParams.Fs = LVM_FS_DUMMY;
62 pLVPSA_Inst->CurrentParams.LevelDetectionSpeed = LVPSA_SPEED_DUMMY;
63
64 { /* for avoiding QAC warnings */
65 LVM_INT32 SDBD = (LVM_INT32)pLVPSA_Inst->SpectralDataBufferDuration;
66 LVM_INT32 IRTI = (LVM_INT32)LVPSA_InternalRefreshTimeInv;
67 LVM_INT32 BL;
68
69 MUL32x32INTO32(SDBD, IRTI, BL, LVPSA_InternalRefreshTimeShift)
70
71 BufferLength = (LVM_UINT32)BL;
72 }
73
74 if ((BufferLength * LVPSA_InternalRefreshTime) != pLVPSA_Inst->SpectralDataBufferDuration) {
75 pLVPSA_Inst->SpectralDataBufferLength = BufferLength + 1;
76 } else {
77 pLVPSA_Inst->SpectralDataBufferLength = BufferLength;
78 }
79
80 /* Assign the pointers */
81 pLVPSA_Inst->pPostGains =
82 (LVM_FLOAT*)calloc(pInitParams->nBands, sizeof(*(pLVPSA_Inst->pPostGains)));
83 if (pLVPSA_Inst->pPostGains == LVM_NULL) {
84 return LVPSA_ERROR_NULLADDRESS;
85 }
86 pLVPSA_Inst->pFiltersParams = (LVPSA_FilterParam_t*)calloc(
87 pInitParams->nBands, sizeof(*(pLVPSA_Inst->pFiltersParams)));
88 if (pLVPSA_Inst->pFiltersParams == LVM_NULL) {
89 return LVPSA_ERROR_NULLADDRESS;
90 }
91 pLVPSA_Inst->pSpectralDataBufferStart = (LVM_UINT8*)calloc(
92 pInitParams->nBands, pLVPSA_Inst->SpectralDataBufferLength *
93 sizeof(*(pLVPSA_Inst->pSpectralDataBufferStart)));
94 if (pLVPSA_Inst->pSpectralDataBufferStart == LVM_NULL) {
95 return LVPSA_ERROR_NULLADDRESS;
96 }
97 pLVPSA_Inst->pPreviousPeaks =
98 (LVM_UINT8*)calloc(pInitParams->nBands, sizeof(*(pLVPSA_Inst->pPreviousPeaks)));
99 if (pLVPSA_Inst->pPreviousPeaks == LVM_NULL) {
100 return LVPSA_ERROR_NULLADDRESS;
101 }
102 pLVPSA_Inst->pBPFiltersPrecision = (LVPSA_BPFilterPrecision_en*)calloc(
103 pInitParams->nBands, sizeof(*(pLVPSA_Inst->pBPFiltersPrecision)));
104 if (pLVPSA_Inst->pBPFiltersPrecision == LVM_NULL) {
105 return LVPSA_ERROR_NULLADDRESS;
106 }
107 pLVPSA_Inst->pQPD_States =
108 (QPD_FLOAT_State_t*)calloc(pInitParams->nBands, sizeof(*(pLVPSA_Inst->pQPD_States)));
109 if (pLVPSA_Inst->pQPD_States == LVM_NULL) {
110 return LVPSA_ERROR_NULLADDRESS;
111 }
112 pLVPSA_Inst->pQPD_Taps =
113 (QPD_FLOAT_Taps_t*)calloc(pInitParams->nBands, sizeof(*(pLVPSA_Inst->pQPD_Taps)));
114 if (pLVPSA_Inst->pQPD_Taps == LVM_NULL) {
115 return LVPSA_ERROR_NULLADDRESS;
116 }
117
118 /* Copy filters parameters in the private instance */
119 for (ii = 0; ii < pLVPSA_Inst->nBands; ii++) {
120 pLVPSA_Inst->pFiltersParams[ii] = pInitParams->pFiltersParams[ii];
121 }
122
123 /* Set Post filters gains*/
124 for (ii = 0; ii < pLVPSA_Inst->nBands; ii++) {
125 pLVPSA_Inst->pPostGains[ii] =
126 LVPSA_Float_GainTable[15 + pInitParams->pFiltersParams[ii].PostGain];
127 }
128 pLVPSA_Inst->pSpectralDataBufferWritePointer = pLVPSA_Inst->pSpectralDataBufferStart;
129
130 /* Initialize control dependant internal parameters */
131 errorCode = LVPSA_Control(*phInstance, pControlParams);
132
133 if (errorCode != 0) {
134 return errorCode;
135 }
136
137 errorCode = LVPSA_ApplyNewSettings(pLVPSA_Inst);
138
139 if (errorCode != 0) {
140 return errorCode;
141 }
142
143 return (errorCode);
144 }
145
146 /************************************************************************************/
147 /* */
148 /* FUNCTION: LVPSA_DeInit */
149 /* */
150 /* DESCRIPTION: */
151 /* Free the memories created in LVPSA_Init call including instance handle */
152 /* */
153 /* PARAMETERS: */
154 /* phInstance Pointer to the instance handle */
155 /* */
156 /************************************************************************************/
LVPSA_DeInit(pLVPSA_Handle_t * phInstance)157 void LVPSA_DeInit(pLVPSA_Handle_t* phInstance) {
158 LVPSA_InstancePr_t* pLVPSA_Inst = (LVPSA_InstancePr_t*)*phInstance;
159 if (pLVPSA_Inst == LVM_NULL) {
160 return;
161 }
162 if (pLVPSA_Inst->pPostGains != LVM_NULL) {
163 free(pLVPSA_Inst->pPostGains);
164 pLVPSA_Inst->pPostGains = LVM_NULL;
165 }
166 if (pLVPSA_Inst->pFiltersParams != LVM_NULL) {
167 free(pLVPSA_Inst->pFiltersParams);
168 pLVPSA_Inst->pFiltersParams = LVM_NULL;
169 }
170 if (pLVPSA_Inst->pSpectralDataBufferStart != LVM_NULL) {
171 free(pLVPSA_Inst->pSpectralDataBufferStart);
172 pLVPSA_Inst->pSpectralDataBufferStart = LVM_NULL;
173 }
174 if (pLVPSA_Inst->pPreviousPeaks != LVM_NULL) {
175 free(pLVPSA_Inst->pPreviousPeaks);
176 pLVPSA_Inst->pPreviousPeaks = LVM_NULL;
177 }
178 if (pLVPSA_Inst->pBPFiltersPrecision != LVM_NULL) {
179 free(pLVPSA_Inst->pBPFiltersPrecision);
180 pLVPSA_Inst->pBPFiltersPrecision = LVM_NULL;
181 }
182 if (pLVPSA_Inst->pQPD_States != LVM_NULL) {
183 free(pLVPSA_Inst->pQPD_States);
184 pLVPSA_Inst->pQPD_States = LVM_NULL;
185 }
186 if (pLVPSA_Inst->pQPD_Taps != LVM_NULL) {
187 free(pLVPSA_Inst->pQPD_Taps);
188 pLVPSA_Inst->pQPD_Taps = LVM_NULL;
189 }
190 delete pLVPSA_Inst;
191 *phInstance = LVM_NULL;
192 }
193