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,
31 uint32_t codecDataBytes,
32 uint8_t maxChannels, uint8_t pcmStride) {
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 =
52 maxChannels * sizeof(common->subdata[0]) * SBC_MAX_BANDS * SBC_MAX_BLOCKS;
53 if (subdataSize > codecDataBytes) {
54 return OI_STATUS_OUT_OF_MEMORY;
55 }
56
57 filterBufferCount =
58 (codecDataBytes - subdataSize) /
59 (sizeof(common->filterBuffer[0][0]) * SBC_MAX_BANDS * maxChannels);
60 if (filterBufferCount < SBC_CODEC_MIN_FILTER_BUFFERS) {
61 return OI_STATUS_OUT_OF_MEMORY;
62 }
63 common->filterBufferLen = filterBufferCount * SBC_MAX_BANDS;
64
65 /* Allocate memory for the subband data */
66 common->subdata = (int32_t*)codecData;
67 codecData += subdataSize;
68 OI_ASSERT(codecDataBytes >= subdataSize);
69 codecDataBytes -= subdataSize;
70
71 /* Allocate memory for the synthesis buffers */
72 for (i = 0; i < maxChannels; ++i) {
73 size_t allocSize =
74 common->filterBufferLen * sizeof(common->filterBuffer[0][0]);
75 common->filterBuffer[i] = (SBC_BUFFER_T*)codecData;
76 OI_ASSERT(codecDataBytes >= allocSize);
77 codecData += allocSize;
78 codecDataBytes -= allocSize;
79 }
80
81 return OI_OK;
82 }
83