xref: /btstack/3rd-party/bluedroid/decoder/srce/alloc.c (revision bc37f7b0d0a3eaa5763a873c5730bc14b849aaa0)
1 /******************************************************************************
2  *
3  *  Copyright (C) 2014 The Android Open Source Project
4  *  Copyright 2003 - 2004 Open Interface North America, Inc. All rights reserved.
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at:
9  *
10  *  http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  ******************************************************************************/
19 
20 #include <stdlib.h>
21 #include <oi_codec_sbc_private.h>
22 
23 /**********************************************************************************
24   $Revision: #1 $
25 ***********************************************************************************/
26 
27 PRIVATE OI_STATUS OI_CODEC_SBC_Alloc(OI_CODEC_SBC_COMMON_CONTEXT *common,
28                                      OI_UINT32 *codecDataAligned,
29                                      OI_UINT32 codecDataBytes,
30                                      OI_UINT8 maxChannels,
31                                      OI_UINT8 pcmStride);
32 PRIVATE OI_STATUS OI_CODEC_SBC_Alloc(OI_CODEC_SBC_COMMON_CONTEXT *common,
33                                      OI_UINT32 *codecDataAligned,
34                                      OI_UINT32 codecDataBytes,
35                                      OI_UINT8 maxChannels,
36                                      OI_UINT8 pcmStride)
37 {
38     int i;
39     size_t filterBufferCount;
40     size_t subdataSize;
41     OI_BYTE *codecData = (OI_BYTE*)codecDataAligned;
42 
43     if (maxChannels < 1 || maxChannels > 2) {
44         return OI_STATUS_INVALID_PARAMETERS;
45     }
46 
47     if (pcmStride < 1 || pcmStride > maxChannels) {
48         return OI_STATUS_INVALID_PARAMETERS;
49     }
50 
51     common->maxChannels = maxChannels;
52     common->pcmStride = pcmStride;
53 
54     /* Compute sizes needed for the memory regions, and bail if we don't have
55      * enough memory for them. */
56     subdataSize = maxChannels * sizeof(common->subdata[0]) * SBC_MAX_BANDS * SBC_MAX_BLOCKS;
57     if (subdataSize > codecDataBytes) {
58         return OI_STATUS_OUT_OF_MEMORY;
59     }
60 
61     filterBufferCount = (codecDataBytes - subdataSize) / (sizeof(common->filterBuffer[0][0]) * SBC_MAX_BANDS * maxChannels);
62     if (filterBufferCount < SBC_CODEC_MIN_FILTER_BUFFERS) {
63         return OI_STATUS_OUT_OF_MEMORY;
64     }
65     common->filterBufferLen = filterBufferCount * SBC_MAX_BANDS;
66 
67     /* Allocate memory for the subband data */
68     common->subdata = (OI_INT32*)codecData;
69     codecData += subdataSize;
70     OI_ASSERT(codecDataBytes >= subdataSize);
71     codecDataBytes -= subdataSize;
72 
73     /* Allocate memory for the synthesis buffers */
74     for (i = 0; i < maxChannels; ++i) {
75         size_t allocSize = common->filterBufferLen * sizeof(common->filterBuffer[0][0]);
76         common->filterBuffer[i] = (SBC_BUFFER_T*)codecData;
77         OI_ASSERT(codecDataBytes >= allocSize);
78         codecData += allocSize;
79         codecDataBytes -= allocSize;
80     }
81 
82     return OI_OK;
83 }
84