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