bitalloc-sbc.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. $Revision: #1 $
  21. ***********************************************************************************/
  22. /** @file
  23. @ingroup codec_internal
  24. */
  25. /**@addgroup codec_internal*/
  26. /**@{*/
  27. #include "common/bt_target.h"
  28. #include <oi_codec_sbc_private.h>
  29. #if (defined(SBC_DEC_INCLUDED) && SBC_DEC_INCLUDED == TRUE)
  30. static void dualBitAllocation(OI_CODEC_SBC_COMMON_CONTEXT *common)
  31. {
  32. OI_UINT bitcountL;
  33. OI_UINT bitcountR;
  34. OI_UINT bitpoolPreferenceL = 0;
  35. OI_UINT bitpoolPreferenceR = 0;
  36. BITNEED_UNION1 bitneedsL;
  37. BITNEED_UNION1 bitneedsR;
  38. bitcountL = computeBitneed(common, bitneedsL.uint8, 0, &bitpoolPreferenceL);
  39. bitcountR = computeBitneed(common, bitneedsR.uint8, 1, &bitpoolPreferenceR);
  40. oneChannelBitAllocation(common, &bitneedsL, 0, bitcountL);
  41. oneChannelBitAllocation(common, &bitneedsR, 1, bitcountR);
  42. }
  43. static void stereoBitAllocation(OI_CODEC_SBC_COMMON_CONTEXT *common)
  44. {
  45. const OI_UINT nrof_subbands = common->frameInfo.nrof_subbands;
  46. BITNEED_UNION2 bitneeds;
  47. OI_UINT excess;
  48. OI_INT bitadjust;
  49. OI_UINT bitcount;
  50. OI_UINT sbL;
  51. OI_UINT sbR;
  52. OI_UINT bitpoolPreference = 0;
  53. bitcount = computeBitneed(common, &bitneeds.uint8[0], 0, &bitpoolPreference);
  54. bitcount += computeBitneed(common, &bitneeds.uint8[nrof_subbands], 1, &bitpoolPreference);
  55. {
  56. OI_UINT ex;
  57. bitadjust = adjustToFitBitpool(common->frameInfo.bitpool, bitneeds.uint32, 2 * nrof_subbands, bitcount, &ex);
  58. /* We want the compiler to put excess into a register */
  59. excess = ex;
  60. }
  61. sbL = 0;
  62. sbR = nrof_subbands;
  63. while (sbL < nrof_subbands) {
  64. excess = allocAdjustedBits(&common->bits.uint8[sbL], bitneeds.uint8[sbL] + bitadjust, excess);
  65. ++sbL;
  66. excess = allocAdjustedBits(&common->bits.uint8[sbR], bitneeds.uint8[sbR] + bitadjust, excess);
  67. ++sbR;
  68. }
  69. sbL = 0;
  70. sbR = nrof_subbands;
  71. while (excess) {
  72. excess = allocExcessBits(&common->bits.uint8[sbL], excess);
  73. ++sbL;
  74. if (!excess) {
  75. break;
  76. }
  77. excess = allocExcessBits(&common->bits.uint8[sbR], excess);
  78. ++sbR;
  79. }
  80. }
  81. static const BIT_ALLOC balloc[] = {
  82. monoBitAllocation, /* SBC_MONO */
  83. dualBitAllocation, /* SBC_DUAL_CHANNEL */
  84. stereoBitAllocation, /* SBC_STEREO */
  85. stereoBitAllocation /* SBC_JOINT_STEREO */
  86. };
  87. PRIVATE void OI_SBC_ComputeBitAllocation(OI_CODEC_SBC_COMMON_CONTEXT *common)
  88. {
  89. OI_ASSERT(common->frameInfo.bitpool <= OI_SBC_MaxBitpool(&common->frameInfo));
  90. OI_ASSERT(common->frameInfo.mode < OI_ARRAYSIZE(balloc));
  91. /*
  92. * Using an array of function pointers prevents the compiler from creating a suboptimal
  93. * monolithic inlined bit allocation function.
  94. */
  95. balloc[common->frameInfo.mode](common);
  96. }
  97. OI_UINT32 OI_CODEC_SBC_CalculateBitrate(OI_CODEC_SBC_FRAME_INFO *frame)
  98. {
  99. return internal_CalculateBitrate(frame);
  100. }
  101. /*
  102. * Return the current maximum bitneed and clear it.
  103. */
  104. OI_UINT8 OI_CODEC_SBC_GetMaxBitneed(OI_CODEC_SBC_COMMON_CONTEXT *common)
  105. {
  106. OI_UINT8 max = common->maxBitneed;
  107. common->maxBitneed = 0;
  108. return max;
  109. }
  110. /*
  111. * Calculates the bitpool size for a given frame length
  112. */
  113. OI_UINT16 OI_CODEC_SBC_CalculateBitpool(OI_CODEC_SBC_FRAME_INFO *frame,
  114. OI_UINT16 frameLen)
  115. {
  116. OI_UINT16 nrof_subbands = frame->nrof_subbands;
  117. OI_UINT16 nrof_blocks = frame->nrof_blocks;
  118. OI_UINT16 hdr;
  119. OI_UINT16 bits;
  120. if (frame->mode == SBC_JOINT_STEREO) {
  121. hdr = 9 * nrof_subbands;
  122. } else {
  123. if (frame->mode == SBC_MONO) {
  124. hdr = 4 * nrof_subbands;
  125. } else {
  126. hdr = 8 * nrof_subbands;
  127. }
  128. if (frame->mode == SBC_DUAL_CHANNEL) {
  129. nrof_blocks *= 2;
  130. }
  131. }
  132. bits = 8 * (frameLen - SBC_HEADER_LEN) - hdr;
  133. return DIVIDE(bits, nrof_blocks);
  134. }
  135. OI_UINT16 OI_CODEC_SBC_CalculatePcmBytes(OI_CODEC_SBC_COMMON_CONTEXT *common)
  136. {
  137. return sizeof(OI_INT16) * common->pcmStride * common->frameInfo.nrof_subbands * common->frameInfo.nrof_blocks;
  138. }
  139. OI_UINT16 OI_CODEC_SBC_CalculateFramelen(OI_CODEC_SBC_FRAME_INFO *frame)
  140. {
  141. return internal_CalculateFramelen(frame);
  142. }
  143. /**@}*/
  144. #endif /* #if (defined(SBC_DEC_INCLUDED) && SBC_DEC_INCLUDED == TRUE) */