alloc.c 3.1 KB

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