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 #include "common/bt_target.h"
20 #include <stdlib.h>
21 #include <oi_codec_sbc_private.h>
22 
23 #if (defined(SBC_DEC_INCLUDED) && SBC_DEC_INCLUDED == TRUE)
24 
25 /**********************************************************************************
26   $Revision: #1 $
27 ***********************************************************************************/
28 
OI_CODEC_SBC_Alloc(OI_CODEC_SBC_COMMON_CONTEXT * common,OI_UINT32 * codecDataAligned,OI_UINT32 codecDataBytes,OI_UINT8 maxChannels,OI_UINT8 pcmStride)29 PRIVATE OI_STATUS OI_CODEC_SBC_Alloc(OI_CODEC_SBC_COMMON_CONTEXT *common,
30                                      OI_UINT32 *codecDataAligned,
31                                      OI_UINT32 codecDataBytes,
32                                      OI_UINT8 maxChannels,
33                                      OI_UINT8 pcmStride)
34 {
35     int i;
36     size_t filterBufferCount;
37     size_t subdataSize;
38     OI_BYTE *codecData = (OI_BYTE *)codecDataAligned;
39 
40     if (maxChannels < 1 || maxChannels > 2) {
41         return OI_STATUS_INVALID_PARAMETERS;
42     }
43 
44     if (pcmStride < 1 || pcmStride > maxChannels) {
45         return OI_STATUS_INVALID_PARAMETERS;
46     }
47 
48     common->maxChannels = maxChannels;
49     common->pcmStride = pcmStride;
50 
51     /* Compute sizes needed for the memory regions, and bail if we don't have
52      * enough memory for them. */
53     subdataSize = maxChannels * sizeof(common->subdata[0]) * SBC_MAX_BANDS * SBC_MAX_BLOCKS;
54     if (subdataSize > codecDataBytes) {
55         return OI_STATUS_OUT_OF_MEMORY;
56     }
57 
58     filterBufferCount = (codecDataBytes - subdataSize) / (sizeof(common->filterBuffer[0][0]) * SBC_MAX_BANDS * maxChannels);
59     if (filterBufferCount < SBC_CODEC_MIN_FILTER_BUFFERS) {
60         return OI_STATUS_OUT_OF_MEMORY;
61     }
62     common->filterBufferLen = filterBufferCount * SBC_MAX_BANDS;
63 
64     /* Allocate memory for the subband data */
65     common->subdata = (OI_INT32 *)codecData;
66     codecData += subdataSize;
67     OI_ASSERT(codecDataBytes >= subdataSize);
68     codecDataBytes -= subdataSize;
69 
70     /* Allocate memory for the synthesis buffers */
71     for (i = 0; i < maxChannels; ++i) {
72         size_t allocSize = common->filterBufferLen * sizeof(common->filterBuffer[0][0]);
73         common->filterBuffer[i] = (SBC_BUFFER_T *)codecData;
74         OI_ASSERT(codecDataBytes >= allocSize);
75         codecData += allocSize;
76         codecDataBytes -= allocSize;
77     }
78 
79     return OI_OK;
80 }
81 
82 #endif /* #if (defined(SBC_DEC_INCLUDED) && SBC_DEC_INCLUDED == TRUE) */
83