decoder-private.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. /**
  23. @file
  24. This file drives SBC decoding.
  25. @ingroup codec_internal
  26. */
  27. /**
  28. @addtogroup codec_internal
  29. @{
  30. */
  31. #include "common/bt_target.h"
  32. #include "oi_codec_sbc_private.h"
  33. #include "oi_bitstream.h"
  34. #include <stdio.h>
  35. #if (defined(SBC_DEC_INCLUDED) && SBC_DEC_INCLUDED == TRUE)
  36. OI_CHAR *const OI_Codec_Copyright = "Copyright 2002-2007 Open Interface North America, Inc. All rights reserved";
  37. INLINE OI_STATUS internal_DecoderReset(OI_CODEC_SBC_DECODER_CONTEXT *context,
  38. OI_UINT32 *decoderData,
  39. OI_UINT32 decoderDataBytes,
  40. OI_BYTE maxChannels,
  41. OI_BYTE pcmStride,
  42. OI_BOOL enhanced,
  43. OI_BOOL msbc_enable)
  44. {
  45. OI_UINT i;
  46. OI_STATUS status;
  47. for (i = 0; i < sizeof(*context); i++) {
  48. ((char *)context)[i] = 0;
  49. }
  50. #ifdef SBC_ENHANCED
  51. context->enhancedEnabled = enhanced ? TRUE : FALSE;
  52. #else
  53. context->enhancedEnabled = FALSE;
  54. if (enhanced) {
  55. return OI_STATUS_INVALID_PARAMETERS;
  56. }
  57. #endif
  58. if (msbc_enable) {
  59. context->sbc_mode = OI_SBC_MODE_MSBC;
  60. } else {
  61. context->sbc_mode = OI_SBC_MODE_STD;
  62. }
  63. status = OI_CODEC_SBC_Alloc(&context->common, decoderData, decoderDataBytes, maxChannels, pcmStride);
  64. if (!OI_SUCCESS(status)) {
  65. return status;
  66. }
  67. context->common.codecInfo = OI_Codec_Copyright;
  68. context->common.maxBitneed = 0;
  69. context->limitFrameFormat = FALSE;
  70. OI_SBC_ExpandFrameFields(&context->common.frameInfo);
  71. /*PLATFORM_DECODER_RESET(context);*/
  72. return OI_OK;
  73. }
  74. /**
  75. * Read the SBC header up to but not including the joint stereo mask. The syncword has already been
  76. * examined, and the enhanced mode flag set, by FindSyncword.
  77. */
  78. INLINE void OI_SBC_ReadHeader(OI_CODEC_SBC_COMMON_CONTEXT *common, const OI_BYTE *data)
  79. {
  80. OI_CODEC_SBC_FRAME_INFO *frame = &common->frameInfo;
  81. OI_UINT8 d1;
  82. OI_ASSERT(data[0] == OI_SBC_SYNCWORD || data[0] == OI_SBC_ENHANCED_SYNCWORD
  83. || data[0] == OI_mSBC_SYNCWORD);
  84. /**
  85. * For mSBC, just set those parameters
  86. */
  87. if (data[0] == OI_mSBC_SYNCWORD){
  88. frame->freqIndex = 0;
  89. frame->frequency = 16000;
  90. frame->blocks = 4;
  91. frame->nrof_blocks = 15;
  92. frame->mode = 0;
  93. frame->nrof_channels = 1;
  94. frame->alloc = SBC_LOUDNESS;
  95. frame->subbands = 1;
  96. frame->nrof_subbands = 8;
  97. frame->cachedInfo = 0;
  98. frame->bitpool = 26;
  99. frame->crc = data[3];
  100. return;
  101. }
  102. /* Avoid filling out all these strucutures if we already remember the values
  103. * from last time. Just in case we get a stream corresponding to data[1] ==
  104. * 0, DecoderReset is responsible for ensuring the lookup table entries have
  105. * already been populated
  106. */
  107. d1 = data[1];
  108. if (d1 != frame->cachedInfo) {
  109. frame->freqIndex = (d1 & (BIT7 | BIT6)) >> 6;
  110. frame->frequency = freq_values[frame->freqIndex];
  111. frame->blocks = (d1 & (BIT5 | BIT4)) >> 4;
  112. frame->nrof_blocks = block_values[frame->blocks];
  113. frame->mode = (d1 & (BIT3 | BIT2)) >> 2;
  114. frame->nrof_channels = channel_values[frame->mode];
  115. frame->alloc = (d1 & BIT1) >> 1;
  116. frame->subbands = (d1 & BIT0);
  117. frame->nrof_subbands = band_values[frame->subbands];
  118. frame->cachedInfo = d1;
  119. }
  120. /*
  121. * For decode, the bit allocator needs to know the bitpool value
  122. */
  123. frame->bitpool = data[2];
  124. frame->crc = data[3];
  125. }
  126. #define LOW(x) ((x)& 0xf)
  127. #define HIGH(x) ((x) >> 4)
  128. /*
  129. * Read scalefactor values and prepare the bitstream for OI_SBC_ReadSamples
  130. */
  131. PRIVATE void OI_SBC_ReadScalefactors(OI_CODEC_SBC_COMMON_CONTEXT *common,
  132. const OI_BYTE *b,
  133. OI_BITSTREAM *bs)
  134. {
  135. OI_UINT i = common->frameInfo.nrof_subbands * common->frameInfo.nrof_channels;
  136. OI_INT8 *scale_factor = common->scale_factor;
  137. OI_UINT f;
  138. if (common->frameInfo.nrof_subbands == 8 || common->frameInfo.mode != SBC_JOINT_STEREO) {
  139. if (common->frameInfo.mode == SBC_JOINT_STEREO) {
  140. common->frameInfo.join = *b++;
  141. } else {
  142. common->frameInfo.join = 0;
  143. }
  144. i /= 2;
  145. do {
  146. *scale_factor++ = HIGH(f = *b++);
  147. *scale_factor++ = LOW(f);
  148. } while (--i);
  149. /*
  150. * In this case we know that the scale factors end on a byte boundary so all we need to do
  151. * is initialize the bitstream.
  152. */
  153. OI_BITSTREAM_ReadInit(bs, b);
  154. } else {
  155. OI_ASSERT(common->frameInfo.nrof_subbands == 4 && common->frameInfo.mode == SBC_JOINT_STEREO);
  156. common->frameInfo.join = HIGH(f = *b++);
  157. i = (i - 1) / 2;
  158. do {
  159. *scale_factor++ = LOW(f);
  160. *scale_factor++ = HIGH(f = *b++);
  161. } while (--i);
  162. *scale_factor++ = LOW(f);
  163. /*
  164. * In 4-subband joint stereo mode, the joint stereo information ends on a half-byte
  165. * boundary, so it's necessary to use the bitstream abstraction to read it, since
  166. * OI_SBC_ReadSamples will need to pick up in mid-byte.
  167. */
  168. OI_BITSTREAM_ReadInit(bs, b);
  169. *scale_factor++ = OI_BITSTREAM_ReadUINT4Aligned(bs);
  170. }
  171. }
  172. /** Read quantized subband samples from the input bitstream and expand them. */
  173. PRIVATE void OI_SBC_ReadSamples(OI_CODEC_SBC_DECODER_CONTEXT *context, OI_BITSTREAM *global_bs)
  174. {
  175. OI_CODEC_SBC_COMMON_CONTEXT *common = &context->common;
  176. OI_UINT nrof_blocks = common->frameInfo.nrof_blocks;
  177. OI_INT32 *RESTRICT s = common->subdata;
  178. OI_UINT8 *ptr = global_bs->ptr.w;
  179. OI_UINT32 value = global_bs->value;
  180. OI_UINT bitPtr = global_bs->bitPtr;
  181. const OI_UINT iter_count = common->frameInfo.nrof_channels * common->frameInfo.nrof_subbands / 4;
  182. do {
  183. OI_UINT i;
  184. for (i = 0; i < iter_count; ++i) {
  185. OI_UINT32 sf_by4 = ((OI_UINT32 *)common->scale_factor)[i];
  186. OI_UINT32 bits_by4 = common->bits.uint32[i];
  187. OI_UINT n;
  188. for (n = 0; n < 4; ++n) {
  189. OI_INT32 dequant;
  190. OI_UINT bits;
  191. OI_INT sf;
  192. if (OI_CPU_BYTE_ORDER == OI_LITTLE_ENDIAN_BYTE_ORDER) {
  193. bits = bits_by4 & 0xFF;
  194. bits_by4 >>= 8;
  195. sf = sf_by4 & 0xFF;
  196. sf_by4 >>= 8;
  197. } else {
  198. bits = (bits_by4 >> 24) & 0xFF;
  199. bits_by4 <<= 8;
  200. sf = (sf_by4 >> 24) & 0xFF;
  201. sf_by4 <<= 8;
  202. }
  203. if (bits) {
  204. OI_UINT32 raw;
  205. OI_BITSTREAM_READUINT(raw, bits, ptr, value, bitPtr);
  206. dequant = OI_SBC_Dequant(raw, sf, bits);
  207. } else {
  208. dequant = 0;
  209. }
  210. *s++ = dequant;
  211. }
  212. }
  213. } while (--nrof_blocks);
  214. }
  215. /**
  216. @}
  217. */
  218. #endif /* #if (defined(SBC_DEC_INCLUDED) && SBC_DEC_INCLUDED == TRUE) */