LVPSA_Init.c revision 2c8e5cab3faa6d360e222b7a6c40a80083d021ac
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/*     Project::   PSA_01_ARMC_01                                       */
21/*     $Author: beq07716 $*/
22/*     $Revision: 1006 $*/
23/*     $Date: 2010-06-28 14:01:47 +0200 (Mon, 28 Jun 2010) $*/
24/*                                                                      */
25/************************************************************************/
26#include    "LVPSA.h"
27#include    "LVPSA_Private.h"
28#include    "InstAlloc.h"
29
30/************************************************************************************/
31/*                                                                                  */
32/* FUNCTION:            LVPSA_Init                                                  */
33/*                                                                                  */
34/* DESCRIPTION:                                                                     */
35/*  Initialize the LVPSA module                                                     */
36/*                                                                                  */
37/*                                                                                  */
38/* PARAMETERS:                                                                      */
39/*  phInstance          Pointer to pointer to the instance                          */
40/*  InitParams          Init parameters structure                                   */
41/*  ControlParams       Control parameters structure                                */
42/*  pMemoryTable        Memory table that contains memory areas definition          */
43/*                                                                                  */
44/*                                                                                  */
45/* RETURNS:                                                                         */
46/*  LVPSA_OK            Succeeds                                                    */
47/*  otherwise           Error due to bad parameters                                 */
48/*                                                                                  */
49/************************************************************************************/
50LVPSA_RETURN LVPSA_Init              ( pLVPSA_Handle_t             *phInstance,
51                                       LVPSA_InitParams_t          *pInitParams,
52                                       LVPSA_ControlParams_t       *pControlParams,
53                                       LVPSA_MemTab_t              *pMemoryTable )
54{
55    LVPSA_InstancePr_t          *pLVPSA_Inst;
56    LVPSA_RETURN                errorCode       = LVPSA_OK;
57    LVM_UINT32                  ii;
58    extern LVM_INT16            LVPSA_GainTable[];
59    LVM_UINT32                  BufferLength = 0;
60
61    /* Ints_Alloc instances, needed for memory alignment management */
62    INST_ALLOC          Instance;
63    INST_ALLOC          Scratch;
64    INST_ALLOC          Data;
65    INST_ALLOC          Coef;
66
67    /* Check parameters */
68    if((phInstance == LVM_NULL) || (pInitParams == LVM_NULL) || (pControlParams == LVM_NULL) || (pMemoryTable == LVM_NULL))
69    {
70        return(LVPSA_ERROR_NULLADDRESS);
71    }
72    if( (pInitParams->SpectralDataBufferDuration > LVPSA_MAXBUFFERDURATION)   ||
73        (pInitParams->SpectralDataBufferDuration == 0)                        ||
74        (pInitParams->MaxInputBlockSize > LVPSA_MAXINPUTBLOCKSIZE)      ||
75        (pInitParams->MaxInputBlockSize == 0)                           ||
76        (pInitParams->nBands < LVPSA_NBANDSMIN)                         ||
77        (pInitParams->nBands > LVPSA_NBANDSMAX)                         ||
78        (pInitParams->pFiltersParams == 0))
79    {
80        return(LVPSA_ERROR_INVALIDPARAM);
81    }
82    for(ii = 0; ii < pInitParams->nBands; ii++)
83    {
84        if((pInitParams->pFiltersParams[ii].CenterFrequency > LVPSA_MAXCENTERFREQ) ||
85           (pInitParams->pFiltersParams[ii].PostGain        > LVPSA_MAXPOSTGAIN)   ||
86           (pInitParams->pFiltersParams[ii].PostGain        < LVPSA_MINPOSTGAIN)   ||
87           (pInitParams->pFiltersParams[ii].QFactor < LVPSA_MINQFACTOR)            ||
88           (pInitParams->pFiltersParams[ii].QFactor > LVPSA_MAXQFACTOR))
89           {
90                return(LVPSA_ERROR_INVALIDPARAM);
91           }
92    }
93
94
95    /*Inst_Alloc instances initialization */
96    InstAlloc_Init( &Instance   , pMemoryTable->Region[LVPSA_MEMREGION_INSTANCE].pBaseAddress);
97    InstAlloc_Init( &Scratch    , pMemoryTable->Region[LVPSA_MEMREGION_SCRATCH].pBaseAddress);
98    InstAlloc_Init( &Data       , pMemoryTable->Region[LVPSA_MEMREGION_PERSISTENT_DATA].pBaseAddress);
99    InstAlloc_Init( &Coef       , pMemoryTable->Region[LVPSA_MEMREGION_PERSISTENT_COEF].pBaseAddress);
100
101
102    /* Set the instance handle if not already initialised */
103    if (*phInstance == LVM_NULL)
104    {
105        *phInstance = InstAlloc_AddMember( &Instance, sizeof(LVPSA_InstancePr_t) );
106    }
107    pLVPSA_Inst =(LVPSA_InstancePr_t*)*phInstance;
108
109
110    /* Check the memory table for NULL pointers */
111    for (ii = 0; ii < LVPSA_NR_MEMORY_REGIONS; ii++)
112    {
113        if (pMemoryTable->Region[ii].Size!=0)
114        {
115            if (pMemoryTable->Region[ii].pBaseAddress==LVM_NULL)
116            {
117                return(LVPSA_ERROR_NULLADDRESS);
118            }
119            pLVPSA_Inst->MemoryTable.Region[ii] = pMemoryTable->Region[ii];
120        }
121    }
122
123    /* Initialize module's internal parameters */
124    pLVPSA_Inst->bControlPending = LVM_FALSE;
125    pLVPSA_Inst->nBands = pInitParams->nBands;
126    pLVPSA_Inst->MaxInputBlockSize = pInitParams->MaxInputBlockSize;
127    pLVPSA_Inst->SpectralDataBufferDuration = pInitParams->SpectralDataBufferDuration;
128    pLVPSA_Inst->CurrentParams.Fs = LVM_FS_DUMMY;
129    pLVPSA_Inst->CurrentParams.LevelDetectionSpeed = LVPSA_SPEED_DUMMY;
130
131    {   /* for avoiding QAC warnings */
132        LVM_INT32 SDBD=(LVM_INT32)pLVPSA_Inst->SpectralDataBufferDuration;
133        LVM_INT32 IRTI=(LVM_INT32)LVPSA_InternalRefreshTimeInv;
134        LVM_INT32 BL;
135
136        MUL32x32INTO32(SDBD,IRTI,BL,LVPSA_InternalRefreshTimeShift)
137
138        BufferLength=(LVM_UINT32)BL;
139    }
140
141    if((BufferLength * LVPSA_InternalRefreshTime) != pLVPSA_Inst->SpectralDataBufferDuration)
142    {
143        pLVPSA_Inst->SpectralDataBufferLength = BufferLength + 1;
144    }
145    else
146    {
147        pLVPSA_Inst->SpectralDataBufferLength = BufferLength;
148    }
149
150
151    /* Assign the pointers */
152
153    pLVPSA_Inst->pPostGains                 = InstAlloc_AddMember( &Instance, pInitParams->nBands * sizeof(LVM_UINT16) );
154    pLVPSA_Inst->pFiltersParams             = InstAlloc_AddMember( &Instance, pInitParams->nBands * sizeof(LVPSA_FilterParam_t) );
155    pLVPSA_Inst->pSpectralDataBufferStart   = InstAlloc_AddMember( &Instance, pInitParams->nBands * pLVPSA_Inst->SpectralDataBufferLength * sizeof(LVM_UINT8) );
156    pLVPSA_Inst->pPreviousPeaks             = InstAlloc_AddMember( &Instance, pInitParams->nBands * sizeof(LVM_UINT8) );
157    pLVPSA_Inst->pBPFiltersPrecision        = InstAlloc_AddMember( &Instance, pInitParams->nBands * sizeof(LVPSA_BPFilterPrecision_en) );
158
159    pLVPSA_Inst->pBP_Instances          = InstAlloc_AddMember( &Coef, pInitParams->nBands * sizeof(Biquad_Instance_t) );
160    pLVPSA_Inst->pQPD_States            = InstAlloc_AddMember( &Coef, pInitParams->nBands * sizeof(QPD_State_t) );
161
162    pLVPSA_Inst->pBP_Taps               = InstAlloc_AddMember( &Data, pInitParams->nBands * sizeof(Biquad_1I_Order2_Taps_t) );
163    pLVPSA_Inst->pQPD_Taps              = InstAlloc_AddMember( &Data, pInitParams->nBands * sizeof(QPD_Taps_t) );
164
165
166    /* Copy filters parameters in the private instance */
167    for(ii = 0; ii < pLVPSA_Inst->nBands; ii++)
168    {
169        pLVPSA_Inst->pFiltersParams[ii] = pInitParams->pFiltersParams[ii];
170    }
171
172    /* Set Post filters gains*/
173    for(ii = 0; ii < pLVPSA_Inst->nBands; ii++)
174    {
175        pLVPSA_Inst->pPostGains[ii] =(LVM_UINT16) LVPSA_GainTable[pInitParams->pFiltersParams[ii].PostGain + 15];
176    }
177    pLVPSA_Inst->pSpectralDataBufferWritePointer = pLVPSA_Inst->pSpectralDataBufferStart;
178
179
180    /* Initialize control dependant internal parameters */
181    errorCode = LVPSA_Control (*phInstance, pControlParams);
182
183    if(errorCode!=0)
184    {
185        return errorCode;
186    }
187
188    errorCode = LVPSA_ApplyNewSettings (pLVPSA_Inst);
189
190    if(errorCode!=0)
191    {
192        return errorCode;
193    }
194
195    return(errorCode);
196}
197
198