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 
OI_CODEC_SBC_Alloc(OI_CODEC_SBC_COMMON_CONTEXT * common,OI_UINT32 * codecDataAligned,OI_UINT32 codecDataBytes,OI_UINT8 maxChannels,OI_UINT8 pcmStride)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 {
33     int i;
34     size_t filterBufferCount;
35     size_t subdataSize;
36     OI_BYTE *codecData = (OI_BYTE*)codecDataAligned;
37 
38     if (maxChannels < 1 || maxChannels > 2) {
39         return OI_STATUS_INVALID_PARAMETERS;
40     }
41 
42     if (pcmStride < 1 || pcmStride > maxChannels) {
43         return OI_STATUS_INVALID_PARAMETERS;
44     }
45 
46     common->maxChannels = maxChannels;
47     common->pcmStride = pcmStride;
48 
49     /* Compute sizes needed for the memory regions, and bail if we don't have
50      * enough memory for them. */
51     subdataSize = maxChannels * sizeof(common->subdata[0]) * SBC_MAX_BANDS * SBC_MAX_BLOCKS;
52     if (subdataSize > codecDataBytes) {
53         return OI_STATUS_OUT_OF_MEMORY;
54     }
55 
56     filterBufferCount = (codecDataBytes - subdataSize) / (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 = (OI_INT32*)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