alloc.c 2.9 KB

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