• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "LVM_Private.h"
25 #include "LVM_Tables.h"
26 #include "VectorArithmetic.h"
27 #include "InstAlloc.h"
28 
29 /****************************************************************************************/
30 /*                                                                                      */
31 /* FUNCTION:                LVM_GetMemoryTable                                          */
32 /*                                                                                      */
33 /* DESCRIPTION:                                                                         */
34 /*  This function is used for memory allocation and free. It can be called in           */
35 /*  two ways:                                                                           */
36 /*                                                                                      */
37 /*      hInstance = NULL                Returns the memory requirements                 */
38 /*      hInstance = Instance handle     Returns the memory requirements and             */
39 /*                                      allocated base addresses for the instance       */
40 /*                                                                                      */
41 /*  When this function is called for memory allocation (hInstance=NULL) the memory      */
42 /*  base address pointers are NULL on return.                                           */
43 /*                                                                                      */
44 /*  When the function is called for free (hInstance = Instance Handle) the memory       */
45 /*  table returns the allocated memory and base addresses used during initialisation.   */
46 /*                                                                                      */
47 /* PARAMETERS:                                                                          */
48 /*  hInstance               Instance Handle                                             */
49 /*  pMemoryTable            Pointer to an empty memory definition table                 */
50 /*  pCapabilities           Pointer to the default capabilities                         */
51 /*                                                                                      */
52 /* RETURNS:                                                                             */
53 /*  LVM_SUCCESS             Succeeded                                                   */
54 /*  LVM_NULLADDRESS         When one of pMemoryTable or pInstParams is NULL             */
55 /*  LVM_OUTOFRANGE          When any of the Instance parameters are out of range        */
56 /*                                                                                      */
57 /* NOTES:                                                                               */
58 /*  1.  This function may be interrupted by the LVM_Process function                    */
59 /*  2.  The scratch memory is the largest required by any of the sub-modules plus any   */
60 /*      additional scratch requirements of the bundle                                   */
61 /*                                                                                      */
62 /****************************************************************************************/
63 
64 /*
65  * 4 Types of Memory Regions of LVM
66  * TODO: Allocate on the fly.
67  * i)   LVM_MEMREGION_PERSISTENT_SLOW_DATA - For Instance Handles
68  * ii)  LVM_MEMREGION_PERSISTENT_FAST_DATA - Persistent Buffers
69  * iii) LVM_MEMREGION_PERSISTENT_FAST_COEF - For Holding Structure values
70  * iv)  LVM_MEMREGION_TEMPORARY_FAST       - For Holding Structure values
71  *
72  * LVM_MEMREGION_PERSISTENT_SLOW_DATA:
73  *   Total Memory size:
74  *     sizeof(LVM_Instance_t) + \
75  *     sizeof(LVM_Buffer_t) + \
76  *     sizeof(LVPSA_InstancePr_t) + \
77  *     sizeof(LVM_Buffer_t) - needed if buffer mode is LVM_MANAGED_BUFFER
78  *
79  * LVM_MEMREGION_PERSISTENT_FAST_DATA:
80  *   Total Memory size:
81  *     sizeof(LVM_TE_Data_t) + \
82  *     2 * pInstParams->EQNB_NumBands * sizeof(LVM_EQNB_BandDef_t) + \
83  *     sizeof(LVCS_Data_t) + \
84  *     sizeof(LVDBE_Data_FLOAT_t) + \
85  *     sizeof(Biquad_2I_Order2_FLOAT_Taps_t) + \
86  *     sizeof(Biquad_2I_Order2_FLOAT_Taps_t) + \
87  *     pInstParams->EQNB_NumBands * sizeof(Biquad_2I_Order2_FLOAT_Taps_t) + \
88  *     pInstParams->EQNB_NumBands * sizeof(LVEQNB_BandDef_t) + \
89  *     pInstParams->EQNB_NumBands * sizeof(LVEQNB_BiquadType_en) + \
90  *     2 * LVM_HEADROOM_MAX_NBANDS * sizeof(LVM_HeadroomBandDef_t) + \
91  *     PSA_InitParams.nBands * sizeof(Biquad_1I_Order2_Taps_t) + \
92  *     PSA_InitParams.nBands * sizeof(QPD_Taps_t)
93  *
94  * LVM_MEMREGION_PERSISTENT_FAST_COEF:
95  *   Total Memory size:
96  *     sizeof(LVM_TE_Coefs_t) + \
97  *     sizeof(LVCS_Coefficient_t) + \
98  *     sizeof(LVDBE_Coef_FLOAT_t) + \
99  *     sizeof(Biquad_FLOAT_Instance_t) + \
100  *     sizeof(Biquad_FLOAT_Instance_t) + \
101  *     pInstParams->EQNB_NumBands * sizeof(Biquad_FLOAT_Instance_t) + \
102  *     PSA_InitParams.nBands * sizeof(Biquad_Instance_t) + \
103  *     PSA_InitParams.nBands * sizeof(QPD_State_t)
104  *
105  * LVM_MEMREGION_TEMPORARY_FAST (Scratch):
106  *   Total Memory Size:
107  *     BundleScratchSize + \
108  *     MAX_INTERNAL_BLOCKSIZE * sizeof(LVM_FLOAT) + \
109  *     MaxScratchOf (CS, EQNB, DBE, PSA)
110  *
111  *     a)BundleScratchSize:
112  *         3 * LVM_MAX_CHANNELS \
113  *         * (MIN_INTERNAL_BLOCKSIZE + InternalBlockSize) * sizeof(LVM_FLOAT)
114  *       This Memory is allocated only when Buffer mode is LVM_MANAGED_BUFFER.
115  *     b)MaxScratchOf (CS, EQNB, DBE, PSA)
116  *       This Memory is needed for scratch usage for CS, EQNB, DBE, PSA.
117  *       CS   = (LVCS_SCRATCHBUFFERS * sizeof(LVM_FLOAT)
118  *               * pCapabilities->MaxBlockSize)
119  *       EQNB = (LVEQNB_SCRATCHBUFFERS * sizeof(LVM_FLOAT)
120  *               * pCapabilities->MaxBlockSize)
121  *       DBE  = (LVDBE_SCRATCHBUFFERS_INPLACE*sizeof(LVM_FLOAT)
122  *               * pCapabilities->MaxBlockSize)
123  *       PSA  = (2 * pInitParams->MaxInputBlockSize * sizeof(LVM_FLOAT))
124  *              one MaxInputBlockSize for input and another for filter output
125  *     c)MAX_INTERNAL_BLOCKSIZE
126  *       This Memory is needed for PSAInput - Temp memory to store output
127  *       from McToMono block and given as input to PSA block
128  */
129 
LVM_GetMemoryTable(LVM_Handle_t hInstance,LVM_MemTab_t * pMemoryTable,LVM_InstParams_t * pInstParams)130 LVM_ReturnStatus_en LVM_GetMemoryTable(LVM_Handle_t         hInstance,
131                                        LVM_MemTab_t         *pMemoryTable,
132                                        LVM_InstParams_t     *pInstParams)
133 {
134 
135     LVM_Instance_t      *pInstance = (LVM_Instance_t *)hInstance;
136     LVM_UINT32          AlgScratchSize;
137     LVM_UINT32          BundleScratchSize;
138     LVM_UINT16          InternalBlockSize;
139     INST_ALLOC          AllocMem[LVM_NR_MEMORY_REGIONS];
140     LVM_INT16           i;
141 
142 
143     /*
144      * Check parameters
145      */
146     if(pMemoryTable == LVM_NULL)
147     {
148         return LVM_NULLADDRESS;
149     }
150 
151 
152     /*
153      * Return memory table if the instance has already been created
154      */
155     if (hInstance != LVM_NULL)
156     {
157        /* Read back memory allocation table */
158         *pMemoryTable = pInstance->MemoryTable;
159         return(LVM_SUCCESS);
160     }
161 
162     if(pInstParams == LVM_NULL)
163     {
164         return LVM_NULLADDRESS;
165     }
166 
167     /*
168      *  Power Spectrum Analyser
169      */
170     if(pInstParams->PSA_Included > LVM_PSA_ON)
171     {
172         return (LVM_OUTOFRANGE);
173     }
174 
175     /*
176      * Check the instance parameters
177      */
178     if( (pInstParams->BufferMode != LVM_MANAGED_BUFFERS) && (pInstParams->BufferMode != LVM_UNMANAGED_BUFFERS) )
179     {
180         return (LVM_OUTOFRANGE);
181     }
182 
183     /* N-Band Equalizer */
184     if( pInstParams->EQNB_NumBands > 32 )
185     {
186         return (LVM_OUTOFRANGE);
187     }
188 
189     if(pInstParams->BufferMode == LVM_MANAGED_BUFFERS)
190     {
191         if( (pInstParams->MaxBlockSize < LVM_MIN_MAXBLOCKSIZE ) || (pInstParams->MaxBlockSize > LVM_MANAGED_MAX_MAXBLOCKSIZE ) )
192         {
193             return (LVM_OUTOFRANGE);
194         }
195     }
196     else
197     {
198         if( (pInstParams->MaxBlockSize < LVM_MIN_MAXBLOCKSIZE ) || (pInstParams->MaxBlockSize > LVM_UNMANAGED_MAX_MAXBLOCKSIZE) )
199         {
200             return (LVM_OUTOFRANGE);
201         }
202     }
203 
204     /*
205     * Initialise the AllocMem structures
206     */
207     for (i=0; i<LVM_NR_MEMORY_REGIONS; i++)
208     {
209         InstAlloc_Init(&AllocMem[i], LVM_NULL);
210     }
211     InternalBlockSize = (LVM_UINT16)((pInstParams->MaxBlockSize) & MIN_INTERNAL_BLOCKMASK); /* Force to a multiple of MIN_INTERNAL_BLOCKSIZE */
212 
213     if (InternalBlockSize < MIN_INTERNAL_BLOCKSIZE)
214     {
215         InternalBlockSize = MIN_INTERNAL_BLOCKSIZE;
216     }
217 
218     /* Maximum Internal Black Size should not be more than MAX_INTERNAL_BLOCKSIZE*/
219     if(InternalBlockSize > MAX_INTERNAL_BLOCKSIZE)
220     {
221         InternalBlockSize = MAX_INTERNAL_BLOCKSIZE;
222     }
223 
224     /*
225     * Bundle requirements
226     */
227     InstAlloc_AddMember(&AllocMem[LVM_MEMREGION_PERSISTENT_SLOW_DATA],
228         sizeof(LVM_Instance_t));
229 
230 
231     /*
232      * Set the algorithm and bundle scratch requirements
233      */
234     AlgScratchSize    = 0;
235     if (pInstParams->BufferMode == LVM_MANAGED_BUFFERS)
236     {
237 #ifdef BUILD_FLOAT
238         BundleScratchSize = 3 * LVM_MAX_CHANNELS \
239                             * (MIN_INTERNAL_BLOCKSIZE + InternalBlockSize) \
240                             * sizeof(LVM_FLOAT);
241 #else
242         BundleScratchSize = 6 * (MIN_INTERNAL_BLOCKSIZE + InternalBlockSize) * sizeof(LVM_INT16);
243 #endif
244         InstAlloc_AddMember(&AllocMem[LVM_MEMREGION_TEMPORARY_FAST],        /* Scratch buffer */
245                             BundleScratchSize);
246         InstAlloc_AddMember(&AllocMem[LVM_MEMREGION_PERSISTENT_SLOW_DATA],
247                             sizeof(LVM_Buffer_t));
248     }
249 
250     /*
251      * Treble Enhancement requirements
252      */
253     InstAlloc_AddMember(&AllocMem[LVM_MEMREGION_PERSISTENT_FAST_DATA],
254                         sizeof(LVM_TE_Data_t));
255     InstAlloc_AddMember(&AllocMem[LVM_MEMREGION_PERSISTENT_FAST_COEF],
256                         sizeof(LVM_TE_Coefs_t));
257 
258     /*
259      * N-Band Equalizer requirements
260      */
261     InstAlloc_AddMember(&AllocMem[LVM_MEMREGION_PERSISTENT_FAST_DATA],      /* Local storage */
262                         (pInstParams->EQNB_NumBands * sizeof(LVM_EQNB_BandDef_t)));
263     InstAlloc_AddMember(&AllocMem[LVM_MEMREGION_PERSISTENT_FAST_DATA],      /* User storage */
264                         (pInstParams->EQNB_NumBands * sizeof(LVM_EQNB_BandDef_t)));
265 
266     /*
267      * Concert Sound requirements
268      */
269     {
270         LVCS_MemTab_t           CS_MemTab;
271         LVCS_Capabilities_t     CS_Capabilities;
272 
273         /*
274          * Set the capabilities
275          */
276         CS_Capabilities.MaxBlockSize     = InternalBlockSize;
277 
278         /*
279          * Get the memory requirements
280          */
281         LVCS_Memory(LVM_NULL,
282                     &CS_MemTab,
283                     &CS_Capabilities);
284 
285         /*
286          * Update the memory allocation structures
287          */
288         InstAlloc_AddMember(&AllocMem[LVM_MEMREGION_PERSISTENT_FAST_DATA],
289                             CS_MemTab.Region[LVM_MEMREGION_PERSISTENT_FAST_DATA].Size);
290         InstAlloc_AddMember(&AllocMem[LVM_MEMREGION_PERSISTENT_FAST_COEF],
291                             CS_MemTab.Region[LVM_MEMREGION_PERSISTENT_FAST_COEF].Size);
292         if (CS_MemTab.Region[LVM_MEMREGION_TEMPORARY_FAST].Size > AlgScratchSize) AlgScratchSize = CS_MemTab.Region[LVM_MEMREGION_TEMPORARY_FAST].Size;
293 
294     }
295 
296 
297     /*
298      * Dynamic Bass Enhancement requirements
299      */
300     {
301         LVDBE_MemTab_t          DBE_MemTab;
302         LVDBE_Capabilities_t    DBE_Capabilities;
303 
304         /*
305          * Set the capabilities
306          */
307 #if defined(BUILD_FLOAT) && defined(HIGHER_FS)
308         DBE_Capabilities.SampleRate      = LVDBE_CAP_FS_8000 | LVDBE_CAP_FS_11025 |
309                                            LVDBE_CAP_FS_12000 | LVDBE_CAP_FS_16000 |
310                                            LVDBE_CAP_FS_22050 | LVDBE_CAP_FS_24000 |
311                                            LVDBE_CAP_FS_32000 | LVDBE_CAP_FS_44100 |
312                                            LVDBE_CAP_FS_48000 | LVDBE_CAP_FS_88200 |
313                                            LVDBE_CAP_FS_96000 | LVDBE_CAP_FS_176400 |
314                                            LVDBE_CAP_FS_192000;
315 #else
316         DBE_Capabilities.SampleRate      = LVDBE_CAP_FS_8000 | LVDBE_CAP_FS_11025 | LVDBE_CAP_FS_12000 | LVDBE_CAP_FS_16000 | LVDBE_CAP_FS_22050 | LVDBE_CAP_FS_24000 | LVDBE_CAP_FS_32000 | LVDBE_CAP_FS_44100 | LVDBE_CAP_FS_48000;
317 #endif
318         DBE_Capabilities.CentreFrequency = LVDBE_CAP_CENTRE_55Hz | LVDBE_CAP_CENTRE_55Hz | LVDBE_CAP_CENTRE_66Hz | LVDBE_CAP_CENTRE_78Hz | LVDBE_CAP_CENTRE_90Hz;
319         DBE_Capabilities.MaxBlockSize    = InternalBlockSize;
320 
321         /*
322          * Get the memory requirements
323          */
324         LVDBE_Memory(LVM_NULL,
325                     &DBE_MemTab,
326 
327                     &DBE_Capabilities);
328         /*
329          * Update the bundle table
330          */
331         InstAlloc_AddMember(&AllocMem[LVM_MEMREGION_PERSISTENT_FAST_DATA],
332                             DBE_MemTab.Region[LVM_MEMREGION_PERSISTENT_FAST_DATA].Size);
333         InstAlloc_AddMember(&AllocMem[LVM_MEMREGION_PERSISTENT_FAST_COEF],
334                             DBE_MemTab.Region[LVM_MEMREGION_PERSISTENT_FAST_COEF].Size);
335         if (DBE_MemTab.Region[LVM_MEMREGION_TEMPORARY_FAST].Size > AlgScratchSize) AlgScratchSize = DBE_MemTab.Region[LVM_MEMREGION_TEMPORARY_FAST].Size;
336 
337     }
338 
339 
340     /*
341      * N-Band equaliser requirements
342      */
343     {
344         LVEQNB_MemTab_t         EQNB_MemTab;            /* For N-Band Equaliser */
345         LVEQNB_Capabilities_t   EQNB_Capabilities;
346 
347         /*
348          * Set the capabilities
349          */
350 #if defined(BUILD_FLOAT) && defined(HIGHER_FS)
351         EQNB_Capabilities.SampleRate   = LVEQNB_CAP_FS_8000 | LVEQNB_CAP_FS_11025 |
352                                          LVEQNB_CAP_FS_12000 | LVEQNB_CAP_FS_16000 |
353                                          LVEQNB_CAP_FS_22050 | LVEQNB_CAP_FS_24000 |
354                                          LVEQNB_CAP_FS_32000 | LVEQNB_CAP_FS_44100 |
355                                          LVEQNB_CAP_FS_48000 | LVEQNB_CAP_FS_88200 |
356                                          LVEQNB_CAP_FS_96000 | LVEQNB_CAP_FS_176400 |
357                                          LVEQNB_CAP_FS_192000;
358 #else
359         EQNB_Capabilities.SampleRate   = LVEQNB_CAP_FS_8000 | LVEQNB_CAP_FS_11025 | LVEQNB_CAP_FS_12000 | LVEQNB_CAP_FS_16000 | LVEQNB_CAP_FS_22050 | LVEQNB_CAP_FS_24000 | LVEQNB_CAP_FS_32000 | LVEQNB_CAP_FS_44100 | LVEQNB_CAP_FS_48000;
360 #endif
361         EQNB_Capabilities.SourceFormat = LVEQNB_CAP_STEREO | LVEQNB_CAP_MONOINSTEREO;
362         EQNB_Capabilities.MaxBlockSize = InternalBlockSize;
363         EQNB_Capabilities.MaxBands     = pInstParams->EQNB_NumBands;
364 
365         /*
366          * Get the memory requirements
367          */
368         LVEQNB_Memory(LVM_NULL,
369                       &EQNB_MemTab,
370                       &EQNB_Capabilities);
371 
372         /*
373          * Update the bundle table
374          */
375         InstAlloc_AddMember(&AllocMem[LVM_MEMREGION_PERSISTENT_FAST_DATA],
376                             EQNB_MemTab.Region[LVM_MEMREGION_PERSISTENT_FAST_DATA].Size);
377         InstAlloc_AddMember(&AllocMem[LVM_MEMREGION_PERSISTENT_FAST_COEF],
378                             EQNB_MemTab.Region[LVM_MEMREGION_PERSISTENT_FAST_COEF].Size);
379         if (EQNB_MemTab.Region[LVM_MEMREGION_TEMPORARY_FAST].Size > AlgScratchSize) AlgScratchSize = EQNB_MemTab.Region[LVM_MEMREGION_TEMPORARY_FAST].Size;
380 
381     }
382 
383     /*
384      * Headroom management memory allocation
385      */
386     InstAlloc_AddMember(&AllocMem[LVM_MEMREGION_PERSISTENT_FAST_DATA],
387                        (LVM_HEADROOM_MAX_NBANDS * sizeof(LVM_HeadroomBandDef_t)));
388     InstAlloc_AddMember(&AllocMem[LVM_MEMREGION_PERSISTENT_FAST_DATA],
389                        (LVM_HEADROOM_MAX_NBANDS * sizeof(LVM_HeadroomBandDef_t)));
390 
391 
392     /*
393      * Spectrum Analyzer memory requirements
394      */
395     {
396         pLVPSA_Handle_t     hPSAInst = LVM_NULL;
397         LVPSA_MemTab_t      PSA_MemTab;
398         LVPSA_InitParams_t  PSA_InitParams;
399         LVPSA_FilterParam_t FiltersParams[9];
400         LVPSA_RETURN        PSA_Status;
401 
402         if(pInstParams->PSA_Included == LVM_PSA_ON)
403         {
404             PSA_InitParams.SpectralDataBufferDuration   = (LVM_UINT16) 500;
405             PSA_InitParams.MaxInputBlockSize            = (LVM_UINT16) 1000;
406             PSA_InitParams.nBands                       = (LVM_UINT16) 9;
407 
408             PSA_InitParams.pFiltersParams = &FiltersParams[0];
409             for(i = 0; i < PSA_InitParams.nBands; i++)
410             {
411                 FiltersParams[i].CenterFrequency    = (LVM_UINT16) 1000;
412                 FiltersParams[i].QFactor            = (LVM_UINT16) 25;
413                 FiltersParams[i].PostGain           = (LVM_INT16)  0;
414             }
415 
416             /*
417             * Get the memory requirements
418             */
419             PSA_Status = LVPSA_Memory (hPSAInst,
420                                         &PSA_MemTab,
421                                         &PSA_InitParams);
422 
423             if (PSA_Status != LVPSA_OK)
424             {
425                 return((LVM_ReturnStatus_en) LVM_ALGORITHMPSA);
426             }
427 
428             /*
429             * Update the bundle table
430             */
431             /* Slow Data */
432             InstAlloc_AddMember(&AllocMem[LVM_MEMREGION_PERSISTENT_SLOW_DATA],
433                 PSA_MemTab.Region[LVM_PERSISTENT_SLOW_DATA].Size);
434 
435             /* Fast Data */
436             InstAlloc_AddMember(&AllocMem[LVM_MEMREGION_PERSISTENT_FAST_DATA],
437                 PSA_MemTab.Region[LVM_PERSISTENT_FAST_DATA].Size);
438 
439             /* Fast Coef */
440             InstAlloc_AddMember(&AllocMem[LVM_MEMREGION_PERSISTENT_FAST_COEF],
441                 PSA_MemTab.Region[LVM_PERSISTENT_FAST_COEF].Size);
442 
443             /* Fast Temporary */
444 #ifdef BUILD_FLOAT
445             InstAlloc_AddMember(&AllocMem[LVM_TEMPORARY_FAST],
446                                 MAX_INTERNAL_BLOCKSIZE * sizeof(LVM_FLOAT));
447 #else
448             InstAlloc_AddMember(&AllocMem[LVM_TEMPORARY_FAST],
449                                 MAX_INTERNAL_BLOCKSIZE * sizeof(LVM_INT16));
450 #endif
451 
452             if (PSA_MemTab.Region[LVM_TEMPORARY_FAST].Size > AlgScratchSize)
453             {
454                 AlgScratchSize = PSA_MemTab.Region[LVM_TEMPORARY_FAST].Size;
455             }
456         }
457     }
458 
459     /*
460      * Return the memory table
461      */
462     pMemoryTable->Region[LVM_MEMREGION_PERSISTENT_SLOW_DATA].Size         = InstAlloc_GetTotal(&AllocMem[LVM_MEMREGION_PERSISTENT_SLOW_DATA]);
463     pMemoryTable->Region[LVM_MEMREGION_PERSISTENT_SLOW_DATA].Type         = LVM_PERSISTENT_SLOW_DATA;
464     pMemoryTable->Region[LVM_MEMREGION_PERSISTENT_SLOW_DATA].pBaseAddress = LVM_NULL;
465 
466     pMemoryTable->Region[LVM_MEMREGION_PERSISTENT_FAST_DATA].Size         = InstAlloc_GetTotal(&AllocMem[LVM_MEMREGION_PERSISTENT_FAST_DATA]);
467     pMemoryTable->Region[LVM_MEMREGION_PERSISTENT_FAST_DATA].Type         = LVM_PERSISTENT_FAST_DATA;
468     pMemoryTable->Region[LVM_MEMREGION_PERSISTENT_FAST_DATA].pBaseAddress = LVM_NULL;
469     if (pMemoryTable->Region[LVM_MEMREGION_PERSISTENT_FAST_DATA].Size < 4)
470     {
471         pMemoryTable->Region[LVM_MEMREGION_PERSISTENT_FAST_DATA].Size = 0;
472     }
473 
474     pMemoryTable->Region[LVM_MEMREGION_PERSISTENT_FAST_COEF].Size         = InstAlloc_GetTotal(&AllocMem[LVM_MEMREGION_PERSISTENT_FAST_COEF]);
475     pMemoryTable->Region[LVM_MEMREGION_PERSISTENT_FAST_COEF].Type         = LVM_PERSISTENT_FAST_COEF;
476     pMemoryTable->Region[LVM_MEMREGION_PERSISTENT_FAST_COEF].pBaseAddress = LVM_NULL;
477     if (pMemoryTable->Region[LVM_MEMREGION_PERSISTENT_FAST_COEF].Size < 4)
478     {
479         pMemoryTable->Region[LVM_MEMREGION_PERSISTENT_FAST_COEF].Size = 0;
480     }
481 
482     InstAlloc_AddMember(&AllocMem[LVM_MEMREGION_TEMPORARY_FAST],
483                         AlgScratchSize);
484     pMemoryTable->Region[LVM_MEMREGION_TEMPORARY_FAST].Size             = InstAlloc_GetTotal(&AllocMem[LVM_MEMREGION_TEMPORARY_FAST]);
485     pMemoryTable->Region[LVM_MEMREGION_TEMPORARY_FAST].Type             = LVM_TEMPORARY_FAST;
486     pMemoryTable->Region[LVM_MEMREGION_TEMPORARY_FAST].pBaseAddress     = LVM_NULL;
487     if (pMemoryTable->Region[LVM_MEMREGION_TEMPORARY_FAST].Size < 4)
488     {
489         pMemoryTable->Region[LVM_MEMREGION_TEMPORARY_FAST].Size = 0;
490     }
491 
492     return(LVM_SUCCESS);
493 
494 }
495 
496 
497 /****************************************************************************************/
498 /*                                                                                      */
499 /* FUNCTION:                LVM_GetInstanceHandle                                       */
500 /*                                                                                      */
501 /* DESCRIPTION:                                                                         */
502 /*  This function is used to create a bundle instance. It returns the created instance  */
503 /*  handle through phInstance. All parameters are set to their default, inactive state. */
504 /*                                                                                      */
505 /* PARAMETERS:                                                                          */
506 /*  phInstance              pointer to the instance handle                              */
507 /*  pMemoryTable            Pointer to the memory definition table                      */
508 /*  pInstParams             Pointer to the initialisation capabilities                  */
509 /*                                                                                      */
510 /* RETURNS:                                                                             */
511 /*  LVM_SUCCESS             Initialisation succeeded                                    */
512 /*  LVM_OUTOFRANGE          When any of the Instance parameters are out of range        */
513 /*  LVM_NULLADDRESS         When one of phInstance, pMemoryTable or pInstParams are NULL*/
514 /*                                                                                      */
515 /* NOTES:                                                                               */
516 /*  1. This function must not be interrupted by the LVM_Process function                */
517 /*                                                                                      */
518 /****************************************************************************************/
519 
LVM_GetInstanceHandle(LVM_Handle_t * phInstance,LVM_MemTab_t * pMemoryTable,LVM_InstParams_t * pInstParams)520 LVM_ReturnStatus_en LVM_GetInstanceHandle(LVM_Handle_t           *phInstance,
521                                           LVM_MemTab_t           *pMemoryTable,
522                                           LVM_InstParams_t       *pInstParams)
523 {
524 
525     LVM_ReturnStatus_en     Status = LVM_SUCCESS;
526     LVM_Instance_t          *pInstance;
527     INST_ALLOC              AllocMem[LVM_NR_MEMORY_REGIONS];
528     LVM_INT16               i;
529     LVM_UINT16              InternalBlockSize;
530     LVM_INT32               BundleScratchSize;
531 
532 
533     /*
534      * Check valid points have been given
535      */
536     if ((phInstance == LVM_NULL) || (pMemoryTable == LVM_NULL) || (pInstParams == LVM_NULL))
537     {
538         return (LVM_NULLADDRESS);
539     }
540 
541     /*
542      * Check the memory table for NULL pointers
543      */
544     for (i=0; i<LVM_NR_MEMORY_REGIONS; i++)
545     {
546         if ((pMemoryTable->Region[i].Size != 0) &&
547             (pMemoryTable->Region[i].pBaseAddress==LVM_NULL))
548         {
549             return(LVM_NULLADDRESS);
550         }
551     }
552 
553     /*
554      * Check the instance parameters
555      */
556     if( (pInstParams->BufferMode != LVM_MANAGED_BUFFERS) && (pInstParams->BufferMode != LVM_UNMANAGED_BUFFERS) )
557     {
558         return (LVM_OUTOFRANGE);
559     }
560 
561     if( pInstParams->EQNB_NumBands > 32 )
562     {
563         return (LVM_OUTOFRANGE);
564     }
565 
566     if(pInstParams->BufferMode == LVM_MANAGED_BUFFERS)
567     {
568         if( (pInstParams->MaxBlockSize < LVM_MIN_MAXBLOCKSIZE ) || (pInstParams->MaxBlockSize > LVM_MANAGED_MAX_MAXBLOCKSIZE ) )
569         {
570             return (LVM_OUTOFRANGE);
571         }
572     }
573     else
574     {
575         if( (pInstParams->MaxBlockSize < LVM_MIN_MAXBLOCKSIZE ) || (pInstParams->MaxBlockSize > LVM_UNMANAGED_MAX_MAXBLOCKSIZE) )
576         {
577             return (LVM_OUTOFRANGE);
578         }
579     }
580 
581     if(pInstParams->PSA_Included > LVM_PSA_ON)
582     {
583         return (LVM_OUTOFRANGE);
584     }
585 
586     /*
587      * Initialise the AllocMem structures
588      */
589     for (i=0; i<LVM_NR_MEMORY_REGIONS; i++)
590     {
591         InstAlloc_Init(&AllocMem[i],
592                        pMemoryTable->Region[i].pBaseAddress);
593     }
594 
595 
596     /*
597      * Set the instance handle
598      */
599     *phInstance  = (LVM_Handle_t)InstAlloc_AddMember(&AllocMem[LVM_MEMREGION_PERSISTENT_SLOW_DATA],
600                                                      sizeof(LVM_Instance_t));
601     pInstance =(LVM_Instance_t  *)*phInstance;
602 
603 
604     /*
605      * Save the memory table, parameters and capabilities
606      */
607     pInstance->MemoryTable    = *pMemoryTable;
608     pInstance->InstParams     = *pInstParams;
609 
610 
611     /*
612      * Set the bundle scratch memory and initialse the buffer management
613      */
614     InternalBlockSize = (LVM_UINT16)((pInstParams->MaxBlockSize) & MIN_INTERNAL_BLOCKMASK); /* Force to a multiple of MIN_INTERNAL_BLOCKSIZE */
615     if (InternalBlockSize < MIN_INTERNAL_BLOCKSIZE)
616     {
617         InternalBlockSize = MIN_INTERNAL_BLOCKSIZE;
618     }
619 
620     /* Maximum Internal Black Size should not be more than MAX_INTERNAL_BLOCKSIZE*/
621     if(InternalBlockSize > MAX_INTERNAL_BLOCKSIZE)
622     {
623         InternalBlockSize = MAX_INTERNAL_BLOCKSIZE;
624     }
625     pInstance->InternalBlockSize = (LVM_INT16)InternalBlockSize;
626 
627 
628     /*
629      * Common settings for managed and unmanaged buffers
630      */
631     pInstance->SamplesToProcess = 0;                /* No samples left to process */
632     if (pInstParams->BufferMode == LVM_MANAGED_BUFFERS)
633     {
634         /*
635          * Managed buffers required
636          */
637         pInstance->pBufferManagement = InstAlloc_AddMember(&AllocMem[LVM_MEMREGION_PERSISTENT_SLOW_DATA],
638                                                            sizeof(LVM_Buffer_t));
639 #ifdef BUILD_FLOAT
640         BundleScratchSize = (LVM_INT32)
641                             (3 * LVM_MAX_CHANNELS \
642                              * (MIN_INTERNAL_BLOCKSIZE + InternalBlockSize) \
643                              * sizeof(LVM_FLOAT));
644 #else
645         BundleScratchSize = (LVM_INT32)(6 * (MIN_INTERNAL_BLOCKSIZE + InternalBlockSize) * sizeof(LVM_INT16));
646 #endif
647         pInstance->pBufferManagement->pScratch = InstAlloc_AddMember(&AllocMem[LVM_MEMREGION_TEMPORARY_FAST],   /* Scratch 1 buffer */
648                                                                      (LVM_UINT32)BundleScratchSize);
649 #ifdef BUILD_FLOAT
650         LoadConst_Float(0,                                   /* Clear the input delay buffer */
651                         (LVM_FLOAT *)&pInstance->pBufferManagement->InDelayBuffer,
652                         (LVM_INT16)(LVM_MAX_CHANNELS * MIN_INTERNAL_BLOCKSIZE));
653 #else
654         LoadConst_16(0,                                                        /* Clear the input delay buffer */
655                      (LVM_INT16 *)&pInstance->pBufferManagement->InDelayBuffer,
656                      (LVM_INT16)(2 * MIN_INTERNAL_BLOCKSIZE));
657 #endif
658         pInstance->pBufferManagement->InDelaySamples = MIN_INTERNAL_BLOCKSIZE; /* Set the number of delay samples */
659         pInstance->pBufferManagement->OutDelaySamples = 0;                     /* No samples in the output buffer */
660         pInstance->pBufferManagement->BufferState = LVM_FIRSTCALL;             /* Set the state ready for the first call */
661     }
662 
663 
664     /*
665      * Set default parameters
666      */
667     pInstance->Params.OperatingMode    = LVM_MODE_OFF;
668     pInstance->Params.SampleRate       = LVM_FS_8000;
669     pInstance->Params.SourceFormat     = LVM_MONO;
670     pInstance->Params.SpeakerType      = LVM_HEADPHONES;
671     pInstance->Params.VC_EffectLevel   = 0;
672     pInstance->Params.VC_Balance       = 0;
673 
674     /*
675      * Set callback
676      */
677     pInstance->CallBack = LVM_AlgoCallBack;
678 
679 
680     /*
681      * DC removal filter
682      */
683 #ifdef SUPPORT_MC
684     DC_Mc_D16_TRC_WRA_01_Init(&pInstance->DC_RemovalInstance);
685 #else
686     DC_2I_D16_TRC_WRA_01_Init(&pInstance->DC_RemovalInstance);
687 #endif
688 
689     /*
690      * Treble Enhancement
691      */
692     pInstance->pTE_Taps  = (LVM_TE_Data_t *)InstAlloc_AddMember(&AllocMem[LVM_MEMREGION_PERSISTENT_FAST_DATA],
693                                                                 sizeof(LVM_TE_Data_t));
694 
695     pInstance->pTE_State = (LVM_TE_Coefs_t *)InstAlloc_AddMember(&AllocMem[LVM_MEMREGION_PERSISTENT_FAST_COEF],
696                                                                  sizeof(LVM_TE_Coefs_t));
697     pInstance->Params.TE_OperatingMode = LVM_TE_OFF;
698     pInstance->Params.TE_EffectLevel   = 0;
699     pInstance->TE_Active               = LVM_FALSE;
700 
701 
702     /*
703      * Set the volume control and initialise Current to Target
704      */
705     pInstance->VC_Volume.MixerStream[0].CallbackParam      = 0;
706     pInstance->VC_Volume.MixerStream[0].CallbackSet        = 0;
707     pInstance->VC_Volume.MixerStream[0].pCallbackHandle    = pInstance;
708     pInstance->VC_Volume.MixerStream[0].pCallBack          = LVM_VCCallBack;
709 
710     /* In managed buffering, start with low signal level as delay in buffer management causes a click*/
711     if (pInstParams->BufferMode == LVM_MANAGED_BUFFERS)
712     {
713 #ifdef BUILD_FLOAT
714         LVC_Mixer_Init(&pInstance->VC_Volume.MixerStream[0], 0, 0);
715 #else
716         LVC_Mixer_Init(&pInstance->VC_Volume.MixerStream[0],0,0);
717 #endif
718     }
719     else
720     {
721 #ifdef BUILD_FLOAT
722         LVC_Mixer_Init(&pInstance->VC_Volume.MixerStream[0], LVM_MAXFLOAT, LVM_MAXFLOAT);
723 #else
724         LVC_Mixer_Init(&pInstance->VC_Volume.MixerStream[0],LVM_MAXINT_16,LVM_MAXINT_16);
725 #endif
726     }
727 
728 #ifdef BUILD_FLOAT
729     LVC_Mixer_SetTimeConstant(&pInstance->VC_Volume.MixerStream[0],0,LVM_FS_8000,2);
730 #else
731     LVC_Mixer_SetTimeConstant(&pInstance->VC_Volume.MixerStream[0], 0, LVM_FS_8000, 2);
732 #endif
733 
734     pInstance->VC_VolumedB                  = 0;
735     pInstance->VC_AVLFixedVolume            = 0;
736     pInstance->VC_Active                    = LVM_FALSE;
737 
738     pInstance->VC_BalanceMix.MixerStream[0].CallbackParam      = 0;
739     pInstance->VC_BalanceMix.MixerStream[0].CallbackSet        = 0;
740     pInstance->VC_BalanceMix.MixerStream[0].pCallbackHandle    = pInstance;
741     pInstance->VC_BalanceMix.MixerStream[0].pCallBack          = LVM_VCCallBack;
742 #ifdef BUILD_FLOAT
743     LVC_Mixer_Init(&pInstance->VC_BalanceMix.MixerStream[0], LVM_MAXFLOAT, LVM_MAXFLOAT);
744 #else
745     LVC_Mixer_Init(&pInstance->VC_BalanceMix.MixerStream[0],LVM_MAXINT_16,LVM_MAXINT_16);
746 #endif
747     LVC_Mixer_VarSlope_SetTimeConstant(&pInstance->VC_BalanceMix.MixerStream[0],LVM_VC_MIXER_TIME,LVM_FS_8000,2);
748 
749     pInstance->VC_BalanceMix.MixerStream[1].CallbackParam      = 0;
750     pInstance->VC_BalanceMix.MixerStream[1].CallbackSet        = 0;
751     pInstance->VC_BalanceMix.MixerStream[1].pCallbackHandle    = pInstance;
752     pInstance->VC_BalanceMix.MixerStream[1].pCallBack          = LVM_VCCallBack;
753 #ifdef BUILD_FLOAT
754     LVC_Mixer_Init(&pInstance->VC_BalanceMix.MixerStream[1], LVM_MAXFLOAT, LVM_MAXFLOAT);
755 #else
756     LVC_Mixer_Init(&pInstance->VC_BalanceMix.MixerStream[1],LVM_MAXINT_16,LVM_MAXINT_16);
757 #endif
758     LVC_Mixer_VarSlope_SetTimeConstant(&pInstance->VC_BalanceMix.MixerStream[1],LVM_VC_MIXER_TIME,LVM_FS_8000,2);
759 
760     /*
761      * Set the default EQNB pre-gain and pointer to the band definitions
762      */
763     pInstance->pEQNB_BandDefs = InstAlloc_AddMember(&AllocMem[LVM_MEMREGION_PERSISTENT_FAST_DATA],
764                                                     (pInstParams->EQNB_NumBands * sizeof(LVM_EQNB_BandDef_t)));
765     pInstance->pEQNB_UserDefs = InstAlloc_AddMember(&AllocMem[LVM_MEMREGION_PERSISTENT_FAST_DATA],
766                                                    (pInstParams->EQNB_NumBands * sizeof(LVM_EQNB_BandDef_t)));
767 
768 
769     /*
770      * Initialise the Concert Sound module
771      */
772     {
773         LVCS_Handle_t           hCSInstance;                /* Instance handle */
774         LVCS_MemTab_t           CS_MemTab;                  /* Memory table */
775         LVCS_Capabilities_t     CS_Capabilities;            /* Initial capabilities */
776         LVCS_ReturnStatus_en    LVCS_Status;                /* Function call status */
777 
778         /*
779          * Set default parameters
780          */
781         pInstance->Params.VirtualizerReverbLevel    = 100;
782         pInstance->Params.VirtualizerType           = LVM_CONCERTSOUND;
783         pInstance->Params.VirtualizerOperatingMode  = LVM_MODE_OFF;
784         pInstance->CS_Active                        = LVM_FALSE;
785 
786         /*
787          * Set the initialisation capabilities
788          */
789         CS_Capabilities.MaxBlockSize    = (LVM_UINT16)InternalBlockSize;
790         CS_Capabilities.CallBack = pInstance->CallBack;
791         CS_Capabilities.pBundleInstance = (void*)pInstance;
792 
793 
794         /*
795          * Get the memory requirements and then set the address pointers, forcing alignment
796          */
797         LVCS_Status = LVCS_Memory(LVM_NULL,                /* Get the memory requirements */
798                                   &CS_MemTab,
799                                   &CS_Capabilities);
800         CS_MemTab.Region[LVCS_MEMREGION_PERSISTENT_SLOW_DATA].pBaseAddress = &pInstance->CS_Instance;
801         CS_MemTab.Region[LVCS_MEMREGION_PERSISTENT_FAST_DATA].pBaseAddress = (void *)InstAlloc_AddMember(&AllocMem[LVM_MEMREGION_PERSISTENT_FAST_DATA],
802                                                                                                          CS_MemTab.Region[LVCS_MEMREGION_PERSISTENT_FAST_DATA].Size);
803         CS_MemTab.Region[LVCS_MEMREGION_PERSISTENT_FAST_COEF].pBaseAddress = (void *)InstAlloc_AddMember(&AllocMem[LVM_MEMREGION_PERSISTENT_FAST_COEF],
804                                                                                                          CS_MemTab.Region[LVCS_MEMREGION_PERSISTENT_FAST_COEF].Size);
805         CS_MemTab.Region[LVCS_MEMREGION_TEMPORARY_FAST].pBaseAddress       = (void *)InstAlloc_AddMember(&AllocMem[LVM_MEMREGION_TEMPORARY_FAST],
806                                                                                                          0);
807 
808         /*
809          * Initialise the Concert Sound instance and save the instance handle
810          */
811         hCSInstance = LVM_NULL;                            /* Set to NULL to return handle */
812         LVCS_Status = LVCS_Init(&hCSInstance,              /* Initiailse */
813                                 &CS_MemTab,
814                                 &CS_Capabilities);
815         if (LVCS_Status != LVCS_SUCCESS) return((LVM_ReturnStatus_en)LVCS_Status);
816         pInstance->hCSInstance = hCSInstance;              /* Save the instance handle */
817 
818     }
819 
820     /*
821      * Initialise the Bass Enhancement module
822      */
823     {
824         LVDBE_Handle_t          hDBEInstance;               /* Instance handle */
825         LVDBE_MemTab_t          DBE_MemTab;                 /* Memory table */
826         LVDBE_Capabilities_t    DBE_Capabilities;           /* Initial capabilities */
827         LVDBE_ReturnStatus_en   LVDBE_Status;               /* Function call status */
828 
829 
830         /*
831          * Set the initialisation parameters
832          */
833         pInstance->Params.BE_OperatingMode = LVM_BE_OFF;
834         pInstance->Params.BE_CentreFreq    = LVM_BE_CENTRE_55Hz;
835         pInstance->Params.BE_EffectLevel   = 0;
836         pInstance->Params.BE_HPF           = LVM_BE_HPF_OFF;
837 
838         pInstance->DBE_Active              = LVM_FALSE;
839 
840 
841 
842         /*
843          * Set the initialisation capabilities
844          */
845 #if defined(BUILD_FLOAT) && defined(HIGHER_FS)
846         DBE_Capabilities.SampleRate      = LVDBE_CAP_FS_8000 | LVDBE_CAP_FS_11025 |
847                                            LVDBE_CAP_FS_12000 | LVDBE_CAP_FS_16000 |
848                                            LVDBE_CAP_FS_22050 | LVDBE_CAP_FS_24000 |
849                                            LVDBE_CAP_FS_32000 | LVDBE_CAP_FS_44100 |
850                                            LVDBE_CAP_FS_48000 | LVDBE_CAP_FS_88200 |
851                                            LVDBE_CAP_FS_96000 | LVDBE_CAP_FS_176400 |
852                                            LVDBE_CAP_FS_192000;
853 #else
854         DBE_Capabilities.SampleRate      = LVDBE_CAP_FS_8000 | LVDBE_CAP_FS_11025 | LVDBE_CAP_FS_12000 | LVDBE_CAP_FS_16000 | LVDBE_CAP_FS_22050 | LVDBE_CAP_FS_24000 | LVDBE_CAP_FS_32000 | LVDBE_CAP_FS_44100 | LVDBE_CAP_FS_48000;
855 #endif
856         DBE_Capabilities.CentreFrequency = LVDBE_CAP_CENTRE_55Hz | LVDBE_CAP_CENTRE_55Hz | LVDBE_CAP_CENTRE_66Hz | LVDBE_CAP_CENTRE_78Hz | LVDBE_CAP_CENTRE_90Hz;
857         DBE_Capabilities.MaxBlockSize    = (LVM_UINT16)InternalBlockSize;
858 
859 
860         /*
861          * Get the memory requirements and then set the address pointers
862          */
863         LVDBE_Status = LVDBE_Memory(LVM_NULL,               /* Get the memory requirements */
864                                     &DBE_MemTab,
865                                     &DBE_Capabilities);
866         DBE_MemTab.Region[LVDBE_MEMREGION_INSTANCE].pBaseAddress        = &pInstance->DBE_Instance;
867         DBE_MemTab.Region[LVDBE_MEMREGION_PERSISTENT_DATA].pBaseAddress = (void *)InstAlloc_AddMember(&AllocMem[LVM_MEMREGION_PERSISTENT_FAST_DATA],
868                                                                                                       DBE_MemTab.Region[LVDBE_MEMREGION_PERSISTENT_DATA].Size);
869         DBE_MemTab.Region[LVDBE_MEMREGION_PERSISTENT_COEF].pBaseAddress = (void *)InstAlloc_AddMember(&AllocMem[LVM_MEMREGION_PERSISTENT_FAST_COEF],
870                                                                                                       DBE_MemTab.Region[LVDBE_MEMREGION_PERSISTENT_COEF].Size);
871         DBE_MemTab.Region[LVDBE_MEMREGION_SCRATCH].pBaseAddress         = (void *)InstAlloc_AddMember(&AllocMem[LVM_MEMREGION_TEMPORARY_FAST],
872                                                                                                       0);
873 
874 
875         /*
876          * Initialise the Dynamic Bass Enhancement instance and save the instance handle
877          */
878         hDBEInstance = LVM_NULL;                            /* Set to NULL to return handle */
879         LVDBE_Status = LVDBE_Init(&hDBEInstance,            /* Initiailse */
880                                   &DBE_MemTab,
881                                   &DBE_Capabilities);
882         if (LVDBE_Status != LVDBE_SUCCESS) return((LVM_ReturnStatus_en)LVDBE_Status);
883         pInstance->hDBEInstance = hDBEInstance;             /* Save the instance handle */
884     }
885 
886 
887     /*
888      * Initialise the N-Band Equaliser module
889      */
890     {
891         LVEQNB_Handle_t          hEQNBInstance;             /* Instance handle */
892         LVEQNB_MemTab_t          EQNB_MemTab;               /* Memory table */
893         LVEQNB_Capabilities_t    EQNB_Capabilities;         /* Initial capabilities */
894         LVEQNB_ReturnStatus_en   LVEQNB_Status;             /* Function call status */
895 
896 
897         /*
898          * Set the initialisation parameters
899          */
900         pInstance->Params.EQNB_OperatingMode   = LVM_EQNB_OFF;
901         pInstance->Params.EQNB_NBands          = 0;
902         pInstance->Params.pEQNB_BandDefinition = LVM_NULL;
903         pInstance->EQNB_Active                 = LVM_FALSE;
904 
905 
906         /*
907          * Set the initialisation capabilities
908          */
909 #if defined(BUILD_FLOAT) && defined(HIGHER_FS)
910         EQNB_Capabilities.SampleRate      = LVEQNB_CAP_FS_8000 | LVEQNB_CAP_FS_11025 |
911                                             LVEQNB_CAP_FS_12000 | LVEQNB_CAP_FS_16000 |
912                                             LVEQNB_CAP_FS_22050 | LVEQNB_CAP_FS_24000 |
913                                             LVEQNB_CAP_FS_32000 | LVEQNB_CAP_FS_44100 |
914                                             LVEQNB_CAP_FS_48000 | LVEQNB_CAP_FS_88200 |
915                                             LVEQNB_CAP_FS_96000 | LVEQNB_CAP_FS_176400 |
916                                             LVEQNB_CAP_FS_192000;
917 #else
918         EQNB_Capabilities.SampleRate      = LVEQNB_CAP_FS_8000 | LVEQNB_CAP_FS_11025 | LVEQNB_CAP_FS_12000 | LVEQNB_CAP_FS_16000 | LVEQNB_CAP_FS_22050 | LVEQNB_CAP_FS_24000 | LVEQNB_CAP_FS_32000 | LVEQNB_CAP_FS_44100 | LVEQNB_CAP_FS_48000;
919 #endif
920         EQNB_Capabilities.MaxBlockSize    = (LVM_UINT16)InternalBlockSize;
921         EQNB_Capabilities.MaxBands        = pInstParams->EQNB_NumBands;
922         EQNB_Capabilities.SourceFormat    = LVEQNB_CAP_STEREO | LVEQNB_CAP_MONOINSTEREO;
923         EQNB_Capabilities.CallBack        = pInstance->CallBack;
924         EQNB_Capabilities.pBundleInstance  = (void*)pInstance;
925 
926 
927         /*
928          * Get the memory requirements and then set the address pointers, forcing alignment
929          */
930         LVEQNB_Status = LVEQNB_Memory(LVM_NULL,             /* Get the memory requirements */
931                                       &EQNB_MemTab,
932                                       &EQNB_Capabilities);
933         EQNB_MemTab.Region[LVEQNB_MEMREGION_INSTANCE].pBaseAddress        = &pInstance->EQNB_Instance;
934         EQNB_MemTab.Region[LVEQNB_MEMREGION_PERSISTENT_DATA].pBaseAddress = (void *)InstAlloc_AddMember(&AllocMem[LVM_MEMREGION_PERSISTENT_FAST_DATA],
935                                                                                                         EQNB_MemTab.Region[LVEQNB_MEMREGION_PERSISTENT_DATA].Size);
936         EQNB_MemTab.Region[LVEQNB_MEMREGION_PERSISTENT_COEF].pBaseAddress = (void *)InstAlloc_AddMember(&AllocMem[LVM_MEMREGION_PERSISTENT_FAST_COEF],
937                                                                                                         EQNB_MemTab.Region[LVEQNB_MEMREGION_PERSISTENT_COEF].Size);
938         EQNB_MemTab.Region[LVEQNB_MEMREGION_SCRATCH].pBaseAddress         = (void *)InstAlloc_AddMember(&AllocMem[LVM_MEMREGION_TEMPORARY_FAST],
939                                                                                                         0);
940 
941 
942         /*
943          * Initialise the Dynamic Bass Enhancement instance and save the instance handle
944          */
945         hEQNBInstance = LVM_NULL;                           /* Set to NULL to return handle */
946         LVEQNB_Status = LVEQNB_Init(&hEQNBInstance,         /* Initiailse */
947                                     &EQNB_MemTab,
948                                     &EQNB_Capabilities);
949         if (LVEQNB_Status != LVEQNB_SUCCESS) return((LVM_ReturnStatus_en)LVEQNB_Status);
950         pInstance->hEQNBInstance = hEQNBInstance;           /* Save the instance handle */
951     }
952 
953     /*
954      * Headroom management memory allocation
955      */
956     {
957         pInstance->pHeadroom_BandDefs = InstAlloc_AddMember(&AllocMem[LVM_MEMREGION_PERSISTENT_FAST_DATA],
958                                                         (LVM_HEADROOM_MAX_NBANDS * sizeof(LVM_HeadroomBandDef_t)));
959         pInstance->pHeadroom_UserDefs = InstAlloc_AddMember(&AllocMem[LVM_MEMREGION_PERSISTENT_FAST_DATA],
960                                                        (LVM_HEADROOM_MAX_NBANDS * sizeof(LVM_HeadroomBandDef_t)));
961 
962         /* Headroom management parameters initialisation */
963         pInstance->NewHeadroomParams.NHeadroomBands = 2;
964         pInstance->NewHeadroomParams.pHeadroomDefinition = pInstance->pHeadroom_BandDefs;
965         pInstance->NewHeadroomParams.pHeadroomDefinition[0].Limit_Low          = 20;
966         pInstance->NewHeadroomParams.pHeadroomDefinition[0].Limit_High         = 4999;
967         pInstance->NewHeadroomParams.pHeadroomDefinition[0].Headroom_Offset    = 3;
968         pInstance->NewHeadroomParams.pHeadroomDefinition[1].Limit_Low          = 5000;
969         pInstance->NewHeadroomParams.pHeadroomDefinition[1].Limit_High         = 24000;
970         pInstance->NewHeadroomParams.pHeadroomDefinition[1].Headroom_Offset    = 4;
971         pInstance->NewHeadroomParams.Headroom_OperatingMode = LVM_HEADROOM_ON;
972 
973         pInstance->Headroom =0;
974     }
975 
976 
977     /*
978      * Initialise the PSA module
979      */
980     {
981         pLVPSA_Handle_t     hPSAInstance = LVM_NULL;   /* Instance handle */
982         LVPSA_MemTab_t      PSA_MemTab;
983         LVPSA_RETURN        PSA_Status;                 /* Function call status */
984         LVPSA_FilterParam_t FiltersParams[9];
985 
986         if(pInstParams->PSA_Included==LVM_PSA_ON)
987         {
988             pInstance->PSA_InitParams.SpectralDataBufferDuration   = (LVM_UINT16) 500;
989             pInstance->PSA_InitParams.MaxInputBlockSize            = (LVM_UINT16) 2048;
990             pInstance->PSA_InitParams.nBands                       = (LVM_UINT16) 9;
991             pInstance->PSA_InitParams.pFiltersParams               = &FiltersParams[0];
992             for(i = 0; i < pInstance->PSA_InitParams.nBands; i++)
993             {
994                 FiltersParams[i].CenterFrequency    = (LVM_UINT16) 1000;
995                 FiltersParams[i].QFactor            = (LVM_UINT16) 100;
996                 FiltersParams[i].PostGain           = (LVM_INT16)  0;
997             }
998 
999             /*Get the memory requirements and then set the address pointers*/
1000             PSA_Status = LVPSA_Memory (hPSAInstance,
1001                                           &PSA_MemTab,
1002                                           &pInstance->PSA_InitParams);
1003 
1004             if (PSA_Status != LVPSA_OK)
1005             {
1006                 return((LVM_ReturnStatus_en) LVM_ALGORITHMPSA);
1007             }
1008 
1009             /* Slow Data */
1010             PSA_MemTab.Region[LVM_PERSISTENT_SLOW_DATA].pBaseAddress = (void *)InstAlloc_AddMember(&AllocMem[LVM_MEMREGION_PERSISTENT_SLOW_DATA],
1011                 PSA_MemTab.Region[LVM_PERSISTENT_SLOW_DATA].Size);
1012 
1013 
1014             /* Fast Data */
1015             PSA_MemTab.Region[LVM_PERSISTENT_FAST_DATA].pBaseAddress = (void *)InstAlloc_AddMember(&AllocMem[LVM_MEMREGION_PERSISTENT_FAST_DATA],
1016                 PSA_MemTab.Region[LVM_PERSISTENT_FAST_DATA].Size);
1017 
1018 
1019             /* Fast Coef */
1020             PSA_MemTab.Region[LVM_PERSISTENT_FAST_COEF].pBaseAddress = (void *)InstAlloc_AddMember(&AllocMem[LVM_MEMREGION_PERSISTENT_FAST_COEF],
1021                 PSA_MemTab.Region[LVM_PERSISTENT_FAST_COEF].Size);
1022 
1023             /* Fast Temporary */
1024 #ifdef BUILD_FLOAT
1025             pInstance->pPSAInput = InstAlloc_AddMember(&AllocMem[LVM_TEMPORARY_FAST],
1026                                                        (LVM_UINT32) MAX_INTERNAL_BLOCKSIZE * \
1027                                                        sizeof(LVM_FLOAT));
1028 #else
1029             pInstance->pPSAInput = InstAlloc_AddMember(&AllocMem[LVM_TEMPORARY_FAST],
1030                                                        (LVM_UINT32) MAX_INTERNAL_BLOCKSIZE * sizeof(LVM_INT16));
1031 #endif
1032             PSA_MemTab.Region[LVM_TEMPORARY_FAST].pBaseAddress       = (void *)InstAlloc_AddMember(&AllocMem[LVM_MEMREGION_TEMPORARY_FAST],0);
1033 
1034 
1035             /*Initialise PSA instance and save the instance handle*/
1036             pInstance->PSA_ControlParams.Fs = LVM_FS_48000;
1037             pInstance->PSA_ControlParams.LevelDetectionSpeed  = LVPSA_SPEED_MEDIUM;
1038             PSA_Status = LVPSA_Init (&hPSAInstance,
1039                                     &pInstance->PSA_InitParams,
1040                                     &pInstance->PSA_ControlParams,
1041                                     &PSA_MemTab);
1042 
1043             if (PSA_Status != LVPSA_OK)
1044             {
1045                 return((LVM_ReturnStatus_en) LVM_ALGORITHMPSA);
1046             }
1047 
1048             pInstance->hPSAInstance = hPSAInstance;       /* Save the instance handle */
1049             pInstance->PSA_GainOffset = 0;
1050         }
1051         else
1052         {
1053             pInstance->hPSAInstance = LVM_NULL;
1054         }
1055 
1056         /*
1057          * Set the initialisation parameters.
1058          */
1059         pInstance->Params.PSA_PeakDecayRate   = LVM_PSA_SPEED_MEDIUM;
1060         pInstance->Params.PSA_Enable          = LVM_PSA_OFF;
1061     }
1062 
1063     /*
1064      * Copy the initial parameters to the new parameters for correct readback of
1065      * the settings.
1066      */
1067     pInstance->NewParams = pInstance->Params;
1068 
1069 
1070     /*
1071      * Create configuration number
1072      */
1073     pInstance->ConfigurationNumber = 0x00000000;
1074     pInstance->ConfigurationNumber += LVM_CS_MASK;
1075     pInstance->ConfigurationNumber += LVM_EQNB_MASK;
1076     pInstance->ConfigurationNumber += LVM_DBE_MASK;
1077     pInstance->ConfigurationNumber += LVM_VC_MASK;
1078     pInstance->ConfigurationNumber += LVM_PSA_MASK;
1079 
1080     if(((pInstance->ConfigurationNumber  & LVM_CS_MASK)!=0)  ||
1081         ((pInstance->ConfigurationNumber & LVM_DBE_MASK)!=0) ||
1082         ((pInstance->ConfigurationNumber & LVM_EQNB_MASK)!=0)||
1083         ((pInstance->ConfigurationNumber & LVM_TE_MASK)!=0)  ||
1084         ((pInstance->ConfigurationNumber & LVM_VC_MASK)!=0))
1085     {
1086         pInstance->BlickSizeMultiple    = 4;
1087     }
1088     else
1089     {
1090         pInstance->BlickSizeMultiple    = 1;
1091     }
1092 
1093     return(Status);
1094 }
1095 
1096 
1097 /****************************************************************************************/
1098 /*                                                                                      */
1099 /* FUNCTION:                LVM_ClearAudioBuffers                                       */
1100 /*                                                                                      */
1101 /* DESCRIPTION:                                                                         */
1102 /*  This function is used to clear the internal audio buffers of the bundle.            */
1103 /*                                                                                      */
1104 /* PARAMETERS:                                                                          */
1105 /*  hInstance               Instance handle                                             */
1106 /*                                                                                      */
1107 /* RETURNS:                                                                             */
1108 /*  LVM_SUCCESS             Initialisation succeeded                                    */
1109 /*  LVM_NULLADDRESS         Instance or scratch memory has a NULL pointer               */
1110 /*                                                                                      */
1111 /* NOTES:                                                                               */
1112 /*  1. This function must not be interrupted by the LVM_Process function                */
1113 /*                                                                                      */
1114 /****************************************************************************************/
1115 
LVM_ClearAudioBuffers(LVM_Handle_t hInstance)1116 LVM_ReturnStatus_en LVM_ClearAudioBuffers(LVM_Handle_t  hInstance)
1117 {
1118     LVM_MemTab_t            MemTab;                                     /* Memory table */
1119     LVM_InstParams_t        InstParams;                                 /* Instance parameters */
1120     LVM_ControlParams_t     Params;                                     /* Control Parameters */
1121     LVM_Instance_t          *pInstance  = (LVM_Instance_t  *)hInstance; /* Pointer to Instance */
1122     LVM_HeadroomParams_t    HeadroomParams;
1123 
1124 
1125     if(hInstance == LVM_NULL){
1126         return LVM_NULLADDRESS;
1127     }
1128 
1129     /* Save the control parameters */ /* coverity[unchecked_value] */ /* Do not check return value internal function calls */
1130     LVM_GetControlParameters(hInstance, &Params);
1131 
1132     /*Save the headroom parameters*/
1133     LVM_GetHeadroomParams(hInstance, &HeadroomParams);
1134 
1135     /*  Retrieve allocated buffers in memtab */
1136     LVM_GetMemoryTable(hInstance, &MemTab,  LVM_NULL);
1137 
1138     /*  Save the instance parameters */
1139     InstParams = pInstance->InstParams;
1140 
1141     /*  Call  LVM_GetInstanceHandle to re-initialise the bundle */
1142     LVM_GetInstanceHandle( &hInstance,
1143                            &MemTab,
1144                            &InstParams);
1145 
1146     /* Restore control parameters */ /* coverity[unchecked_value] */ /* Do not check return value internal function calls */
1147     LVM_SetControlParameters(hInstance, &Params);
1148 
1149     /*Restore the headroom parameters*/
1150     LVM_SetHeadroomParams(hInstance, &HeadroomParams);
1151 
1152     /* DC removal filter */
1153 #ifdef SUPPORT_MC
1154     DC_Mc_D16_TRC_WRA_01_Init(&pInstance->DC_RemovalInstance);
1155 #else
1156     DC_2I_D16_TRC_WRA_01_Init(&pInstance->DC_RemovalInstance);
1157 #endif
1158 
1159     return LVM_SUCCESS;
1160 }
1161 
1162 
1163 
1164