oi_codec_sbc_private.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. #ifndef _OI_CODEC_SBC_PRIVATE_H
  20. #define _OI_CODEC_SBC_PRIVATE_H
  21. /**********************************************************************************
  22. $Revision: #1 $
  23. ***********************************************************************************/
  24. /**
  25. @file
  26. Function prototypes and macro definitions used internally by the codec.
  27. @ingroup codec_internal
  28. */
  29. /**
  30. @addtogroup codec_internal
  31. @{
  32. */
  33. #ifdef USE_RESTRICT_KEYWORD
  34. #define RESTRICT restrict
  35. #else
  36. #define RESTRICT
  37. #endif
  38. #ifdef CODEC_DEBUG
  39. #include <stdio.h>
  40. #define ERROR(x) do { printf x; printf("\n"); } while (0)
  41. #else
  42. #define ERROR(x)
  43. #endif
  44. #ifdef TRACE_EXECUTION
  45. #include <stdio.h>
  46. #define TRACE(x) do { printf x; printf("\n"); } while (0)
  47. #else
  48. #define TRACE(x)
  49. #endif
  50. #ifndef PRIVATE
  51. #define PRIVATE
  52. #endif
  53. #ifndef INLINE
  54. #define INLINE
  55. #endif
  56. #include "oi_assert.h"
  57. #include "oi_codec_sbc.h"
  58. /* BK4BTSTACK_CHANGE START */
  59. #ifndef OI_mSBC_SYNCWORD
  60. #define OI_mSBC_SYNCWORD 0xad
  61. #endif
  62. /* BK4BTSTACK_CHANGE END */
  63. #ifndef OI_SBC_SYNCWORD
  64. #define OI_SBC_SYNCWORD 0x9c
  65. #endif
  66. #ifndef DIVIDE
  67. #define DIVIDE(a, b) ((a) / (b))
  68. #endif
  69. typedef union {
  70. OI_UINT8 uint8[SBC_MAX_BANDS];
  71. OI_UINT32 uint32[SBC_MAX_BANDS / 4];
  72. } BITNEED_UNION1;
  73. typedef union {
  74. OI_UINT8 uint8[2 * SBC_MAX_BANDS];
  75. OI_UINT32 uint32[2 * SBC_MAX_BANDS / 4];
  76. } BITNEED_UNION2;
  77. static const OI_UINT16 freq_values[] = { 16000, 32000, 44100, 48000 };
  78. static const OI_UINT8 block_values[] = { 4, 8, 12, 16 };
  79. static const OI_UINT8 channel_values[] = { 1, 2, 2, 2 };
  80. static const OI_UINT8 band_values[] = { 4, 8 };
  81. #define TEST_MODE_SENTINEL "OINA"
  82. #define TEST_MODE_SENTINEL_LENGTH 4
  83. /** Used internally. */
  84. typedef struct {
  85. union {
  86. const OI_UINT8 *r;
  87. OI_UINT8 *w;
  88. } ptr;
  89. OI_UINT32 value;
  90. OI_UINT bitPtr;
  91. } OI_BITSTREAM;
  92. #define VALID_INT16(x) (((x) >= OI_INT16_MIN) && ((x) <= OI_INT16_MAX))
  93. #define VALID_INT32(x) (((x) >= OI_INT32_MIN) && ((x) <= OI_INT32_MAX))
  94. #define DCTII_8_SHIFT_IN 0
  95. #define DCTII_8_SHIFT_OUT 16-DCTII_8_SHIFT_IN
  96. #define DCTII_8_SHIFT_0 (DCTII_8_SHIFT_OUT)
  97. #define DCTII_8_SHIFT_1 (DCTII_8_SHIFT_OUT)
  98. #define DCTII_8_SHIFT_2 (DCTII_8_SHIFT_OUT)
  99. #define DCTII_8_SHIFT_3 (DCTII_8_SHIFT_OUT)
  100. #define DCTII_8_SHIFT_4 (DCTII_8_SHIFT_OUT)
  101. #define DCTII_8_SHIFT_5 (DCTII_8_SHIFT_OUT)
  102. #define DCTII_8_SHIFT_6 (DCTII_8_SHIFT_OUT-1)
  103. #define DCTII_8_SHIFT_7 (DCTII_8_SHIFT_OUT-2)
  104. #define DCT_SHIFT 15
  105. #define DCTIII_4_SHIFT_IN 2
  106. #define DCTIII_4_SHIFT_OUT 15
  107. #define DCTIII_8_SHIFT_IN 3
  108. #define DCTIII_8_SHIFT_OUT 14
  109. OI_UINT computeBitneed(OI_CODEC_SBC_COMMON_CONTEXT *common,
  110. OI_UINT8 *bitneeds,
  111. OI_UINT ch,
  112. OI_UINT *preferredBitpool);
  113. void oneChannelBitAllocation(OI_CODEC_SBC_COMMON_CONTEXT *common,
  114. BITNEED_UNION1 *bitneeds,
  115. OI_UINT ch,
  116. OI_UINT bitcount);
  117. OI_INT adjustToFitBitpool(const OI_UINT bitpool,
  118. OI_UINT32 *bitneeds,
  119. const OI_UINT subbands,
  120. OI_UINT bitcount,
  121. OI_UINT *excess);
  122. INLINE OI_INT allocAdjustedBits(OI_UINT8 *dest,
  123. OI_INT bits,
  124. OI_INT excess);
  125. INLINE OI_INT allocExcessBits(OI_UINT8 *dest,
  126. OI_INT excess);
  127. PRIVATE OI_UINT32 internal_CalculateBitrate(OI_CODEC_SBC_FRAME_INFO *frame);
  128. PRIVATE OI_UINT16 internal_CalculateFramelen(OI_CODEC_SBC_FRAME_INFO *frame);
  129. void monoBitAllocation(OI_CODEC_SBC_COMMON_CONTEXT *common);
  130. typedef void (*BIT_ALLOC)(OI_CODEC_SBC_COMMON_CONTEXT *common);
  131. PRIVATE OI_STATUS internal_DecodeRaw(OI_CODEC_SBC_DECODER_CONTEXT *context,
  132. OI_UINT8 bitpool,
  133. const OI_BYTE **frameData,
  134. OI_UINT32 *frameBytes,
  135. OI_INT16 *pcmData,
  136. OI_UINT32 *pcmBytes);
  137. INLINE OI_STATUS internal_DecoderReset(OI_CODEC_SBC_DECODER_CONTEXT *context,
  138. OI_UINT32 *decoderData,
  139. OI_UINT32 decoderDataBytes,
  140. OI_BYTE maxChannels,
  141. OI_BYTE pcmStride,
  142. OI_BOOL enhanced);
  143. INLINE OI_UINT16 OI_SBC_CalculateFrameAndHeaderlen(OI_CODEC_SBC_FRAME_INFO *frame, OI_UINT *headerLen_);
  144. PRIVATE OI_UINT32 OI_SBC_MaxBitpool(OI_CODEC_SBC_FRAME_INFO *frame);
  145. PRIVATE void OI_SBC_ComputeBitAllocation(OI_CODEC_SBC_COMMON_CONTEXT *frame);
  146. PRIVATE OI_UINT8 OI_SBC_CalculateChecksum(OI_CODEC_SBC_FRAME_INFO *frame, OI_BYTE const *data);
  147. PRIVATE OI_UINT8 OI_SBC_CalculateChecksum_mSBC(OI_CODEC_SBC_FRAME_INFO *frame, OI_BYTE const *data);
  148. /* Transform functions */
  149. PRIVATE void shift_buffer(SBC_BUFFER_T *dest, SBC_BUFFER_T *src, OI_UINT wordCount);
  150. PRIVATE void cosineModulateSynth4(SBC_BUFFER_T * RESTRICT out, OI_INT32 const * RESTRICT in);
  151. PRIVATE void SynthWindow40_int32_int32_symmetry_with_sum(OI_INT16 *pcm, SBC_BUFFER_T buffer[80], OI_UINT strideShift);
  152. INLINE void dct3_4(OI_INT32 * RESTRICT out, OI_INT32 const * RESTRICT in);
  153. PRIVATE void analyze4_generated(SBC_BUFFER_T analysisBuffer[RESTRICT 40],
  154. OI_INT16 *pcm,
  155. OI_UINT strideShift,
  156. OI_INT32 subband[4]);
  157. INLINE void dct3_8(OI_INT32 * RESTRICT out, OI_INT32 const * RESTRICT in);
  158. PRIVATE void analyze8_generated(SBC_BUFFER_T analysisBuffer[RESTRICT 80],
  159. OI_INT16 *pcm,
  160. OI_UINT strideShift,
  161. OI_INT32 subband[8]);
  162. #ifdef SBC_ENHANCED
  163. PRIVATE void analyze8_enhanced_generated(SBC_BUFFER_T analysisBuffer[RESTRICT 112],
  164. OI_INT16 *pcm,
  165. OI_UINT strideShift,
  166. OI_INT32 subband[8]);
  167. #endif
  168. /* Decoder functions */
  169. INLINE void OI_SBC_ReadHeader_mSBC(OI_CODEC_SBC_COMMON_CONTEXT *common, const OI_BYTE *data);
  170. INLINE void OI_SBC_ReadHeader(OI_CODEC_SBC_COMMON_CONTEXT *common, const OI_BYTE *data);
  171. PRIVATE void OI_SBC_ReadScalefactors(OI_CODEC_SBC_COMMON_CONTEXT *common, const OI_BYTE *b, OI_BITSTREAM *bs);
  172. PRIVATE void OI_SBC_ReadSamples(OI_CODEC_SBC_DECODER_CONTEXT *common, OI_BITSTREAM *ob);
  173. PRIVATE void OI_SBC_ReadSamplesJoint(OI_CODEC_SBC_DECODER_CONTEXT *common, OI_BITSTREAM *global_bs);
  174. PRIVATE void OI_SBC_SynthFrame(OI_CODEC_SBC_DECODER_CONTEXT *context, OI_INT16 *pcm, OI_UINT start_block, OI_UINT nrof_blocks);
  175. INLINE OI_INT32 OI_SBC_Dequant(OI_UINT32 raw, OI_UINT scale_factor, OI_UINT bits);
  176. PRIVATE OI_BOOL OI_SBC_ExamineCommandPacket(OI_CODEC_SBC_DECODER_CONTEXT *context, const OI_BYTE *data, OI_UINT32 len);
  177. PRIVATE void OI_SBC_GenerateTestSignal(OI_INT16 pcmData[][2], OI_UINT32 sampleCount);
  178. PRIVATE void OI_SBC_ExpandFrameFields(OI_CODEC_SBC_FRAME_INFO *frame);
  179. PRIVATE OI_STATUS OI_CODEC_SBC_Alloc(OI_CODEC_SBC_COMMON_CONTEXT *common,
  180. OI_UINT32 *codecDataAligned,
  181. OI_UINT32 codecDataBytes,
  182. OI_UINT8 maxChannels,
  183. OI_UINT8 pcmStride);
  184. /**
  185. @}
  186. */
  187. #endif /* _OI_CODEC_SBC_PRIVATE_H */