decoder-private.c 7.4 KB

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