oi_codec_sbc.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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. #ifndef _OI_CODEC_SBC_CORE_H
  23. #define _OI_CODEC_SBC_CORE_H
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif
  27. /**
  28. @file
  29. Declarations of codec functions, data types, and macros.
  30. @ingroup codec_lib
  31. */
  32. /**
  33. @addtogroup codec_lib
  34. @{
  35. */
  36. /* Non-BM3 users of of the codec must include oi_codec_sbc_bm3defs.h prior to
  37. * including this file, or else these includes will fail because the BM3 SDK is
  38. * not in the include path */
  39. #ifndef _OI_CODEC_SBC_BM3DEFS_H
  40. #include "oi_stddefs.h"
  41. #include "oi_status.h"
  42. #endif
  43. #include <stdint.h>
  44. #define SBC_MAX_CHANNELS 2
  45. #define SBC_MAX_BANDS 8
  46. #define SBC_MAX_BLOCKS 16
  47. #define SBC_MIN_BITPOOL 2 /**< Minimum size of the bit allocation pool used to encode the stream */
  48. #define SBC_MAX_BITPOOL 250 /**< Maximum size of the bit allocation pool used to encode the stream */
  49. #define SBC_MAX_ONE_CHANNEL_BPS 320000
  50. #define SBC_MAX_TWO_CHANNEL_BPS 512000
  51. #define SBC_WBS_BITRATE 62000
  52. #define SBC_WBS_BITPOOL 27
  53. #define SBC_WBS_NROF_BLOCKS 16
  54. #define SBC_WBS_FRAME_LEN 62
  55. #define SBC_WBS_SAMPLES_PER_FRAME 128
  56. #define SBC_HEADER_LEN 4
  57. #define SBC_MAX_FRAME_LEN (SBC_HEADER_LEN + \
  58. ((SBC_MAX_BANDS * SBC_MAX_CHANNELS / 2) + \
  59. ((SBC_MAX_BANDS + (SBC_MAX_BLOCKS * SBC_MAX_BITPOOL) + 7)/8)))
  60. #define SBC_MAX_SAMPLES_PER_FRAME (SBC_MAX_BANDS * SBC_MAX_BLOCKS)
  61. #define SBC_MAX_SCALEFACTOR_BYTES ((4*(SBC_MAX_CHANNELS * SBC_MAX_BANDS) + 7)/8)
  62. #define OI_SBC_SYNCWORD 0x9c
  63. #define OI_SBC_ENHANCED_SYNCWORD 0x9d
  64. /**@name Sampling frequencies */
  65. /**@{*/
  66. #define SBC_FREQ_16000 0 /**< The sampling frequency is 16 kHz. One possible value for the @a frequency parameter of OI_CODEC_SBC_EncoderConfigure() */
  67. #define SBC_FREQ_32000 1 /**< The sampling frequency is 32 kHz. One possible value for the @a frequency parameter of OI_CODEC_SBC_EncoderConfigure() */
  68. #define SBC_FREQ_44100 2 /**< The sampling frequency is 44.1 kHz. One possible value for the @a frequency parameter of OI_CODEC_SBC_EncoderConfigure() */
  69. #define SBC_FREQ_48000 3 /**< The sampling frequency is 48 kHz. One possible value for the @a frequency parameter of OI_CODEC_SBC_EncoderConfigure() */
  70. /**@}*/
  71. /**@name Channel modes */
  72. /**@{*/
  73. #define SBC_MONO 0 /**< The mode of the encoded channel is mono. One possible value for the @a mode parameter of OI_CODEC_SBC_EncoderConfigure() */
  74. #define SBC_DUAL_CHANNEL 1 /**< The mode of the encoded channel is dual-channel. One possible value for the @a mode parameter of OI_CODEC_SBC_EncoderConfigure() */
  75. #define SBC_STEREO 2 /**< The mode of the encoded channel is stereo. One possible value for the @a mode parameter of OI_CODEC_SBC_EncoderConfigure() */
  76. #define SBC_JOINT_STEREO 3 /**< The mode of the encoded channel is joint stereo. One possible value for the @a mode parameter of OI_CODEC_SBC_EncoderConfigure() */
  77. /**@}*/
  78. /**@name Subbands */
  79. /**@{*/
  80. #define SBC_SUBBANDS_4 0 /**< The encoded stream has 4 subbands. One possible value for the @a subbands parameter of OI_CODEC_SBC_EncoderConfigure()*/
  81. #define SBC_SUBBANDS_8 1 /**< The encoded stream has 8 subbands. One possible value for the @a subbands parameter of OI_CODEC_SBC_EncoderConfigure() */
  82. /**@}*/
  83. /**@name Block lengths */
  84. /**@{*/
  85. #define SBC_BLOCKS_4 0 /**< A block size of 4 blocks was used to encode the stream. One possible value for the @a blocks parameter of OI_CODEC_SBC_EncoderConfigure() */
  86. #define SBC_BLOCKS_8 1 /**< A block size of 8 blocks was used to encode the stream is. One possible value for the @a blocks parameter of OI_CODEC_SBC_EncoderConfigure() */
  87. #define SBC_BLOCKS_12 2 /**< A block size of 12 blocks was used to encode the stream. One possible value for the @a blocks parameter of OI_CODEC_SBC_EncoderConfigure() */
  88. #define SBC_BLOCKS_16 3 /**< A block size of 16 blocks was used to encode the stream. One possible value for the @a blocks parameter of OI_CODEC_SBC_EncoderConfigure() */
  89. /**@}*/
  90. /**@name Bit allocation methods */
  91. /**@{*/
  92. #define SBC_LOUDNESS 0 /**< The bit allocation method. One possible value for the @a loudness parameter of OI_CODEC_SBC_EncoderConfigure() */
  93. #define SBC_SNR 1 /**< The bit allocation method. One possible value for the @a loudness parameter of OI_CODEC_SBC_EncoderConfigure() */
  94. /**@}*/
  95. /**
  96. @}
  97. @addtogroup codec_internal
  98. @{
  99. */
  100. typedef OI_INT16 SBC_BUFFER_T;
  101. /** Used internally. */
  102. typedef struct {
  103. OI_UINT16 frequency; /**< The sampling frequency. Input parameter. */
  104. OI_UINT8 freqIndex;
  105. OI_UINT8 nrof_blocks; /**< The block size used to encode the stream. Input parameter. */
  106. OI_UINT8 blocks;
  107. OI_UINT8 nrof_subbands; /**< The number of subbands of the encoded stream. Input parameter. */
  108. OI_UINT8 subbands;
  109. OI_UINT8 mode; /**< The mode of the encoded channel. Input parameter. */
  110. OI_UINT8 nrof_channels; /**< The number of channels of the encoded stream. */
  111. OI_UINT8 alloc; /**< The bit allocation method. Input parameter. */
  112. OI_UINT8 bitpool; /**< Size of the bit allocation pool used to encode the stream. Input parameter. */
  113. OI_UINT8 crc; /**< Parity check byte used for error detection. */
  114. OI_UINT8 join; /**< Whether joint stereo has been used. */
  115. OI_UINT8 enhanced;
  116. OI_UINT8 min_bitpool; /**< This value is only used when encoding. SBC_MAX_BITPOOL if variable
  117. bitpools are disallowed, otherwise the minimum bitpool size that will
  118. be used by the bit allocator. */
  119. OI_UINT8 cachedInfo; /**< Information about the previous frame */
  120. /* BK4BTSTACK_CHANGE START */
  121. OI_UINT8 reserved_for_future_use[2];
  122. OI_UINT8 mSBCEnabled; // default 0
  123. /* BK4BTSTACK_CHANGE END */
  124. } OI_CODEC_SBC_FRAME_INFO;
  125. /** Used internally. */
  126. typedef struct {
  127. const OI_CHAR *codecInfo;
  128. OI_CODEC_SBC_FRAME_INFO frameInfo;
  129. OI_INT8 scale_factor[SBC_MAX_CHANNELS*SBC_MAX_BANDS];
  130. OI_UINT32 frameCount;
  131. OI_INT32 *subdata;
  132. SBC_BUFFER_T *filterBuffer[SBC_MAX_CHANNELS];
  133. OI_INT32 filterBufferLen;
  134. OI_UINT filterBufferOffset;
  135. union {
  136. OI_UINT8 uint8[SBC_MAX_CHANNELS*SBC_MAX_BANDS];
  137. OI_UINT32 uint32[SBC_MAX_CHANNELS*SBC_MAX_BANDS/4];
  138. } bits;
  139. OI_UINT8 maxBitneed; /**< Running maximum bitneed */
  140. OI_BYTE formatByte;
  141. OI_UINT8 pcmStride;
  142. OI_UINT8 maxChannels;
  143. } OI_CODEC_SBC_COMMON_CONTEXT;
  144. /*
  145. * A smaller value reduces RAM usage at the expense of increased CPU usage. Values in the range
  146. * 27..50 are recommended, beyond 50 there is a diminishing return on reduced CPU usage.
  147. */
  148. #define SBC_CODEC_MIN_FILTER_BUFFERS 16
  149. #define SBC_CODEC_FAST_FILTER_BUFFERS 27
  150. /* Expands to the number of OI_UINT32s needed to ensure enough memory to encode
  151. * or decode streams of numChannels channels, using numBuffers buffers.
  152. * Example:
  153. * OI_UINT32 decoderData[CODEC_DATA_WORDS(SBC_MAX_CHANNELS, SBC_DECODER_FAST_SYNTHESIS_BUFFERS)];
  154. * */
  155. #define CODEC_DATA_WORDS(numChannels, numBuffers) \
  156. ((\
  157. (sizeof(OI_INT32) * SBC_MAX_BLOCKS * numChannels * SBC_MAX_BANDS) \
  158. + (sizeof(SBC_BUFFER_T) * SBC_MAX_CHANNELS * SBC_MAX_BANDS * numBuffers) \
  159. + (sizeof (OI_UINT32) - 1) \
  160. ) / sizeof(OI_UINT32))
  161. /** Opaque parameter to decoding functions; maintains decoder context. */
  162. typedef struct {
  163. OI_CODEC_SBC_COMMON_CONTEXT common;
  164. OI_UINT8 limitFrameFormat; /* Boolean, set by OI_CODEC_SBC_DecoderLimit() */
  165. OI_UINT8 restrictSubbands;
  166. OI_UINT8 enhancedEnabled;
  167. OI_UINT8 bufferedBlocks;
  168. } OI_CODEC_SBC_DECODER_CONTEXT;
  169. typedef struct {
  170. OI_UINT32 data[CODEC_DATA_WORDS(1, SBC_CODEC_FAST_FILTER_BUFFERS)];
  171. } OI_CODEC_SBC_CODEC_DATA_MONO;
  172. typedef struct {
  173. OI_UINT32 data[CODEC_DATA_WORDS(2, SBC_CODEC_FAST_FILTER_BUFFERS)];
  174. } OI_CODEC_SBC_CODEC_DATA_STEREO;
  175. /**
  176. @}
  177. @addtogroup codec_lib
  178. @{
  179. */
  180. /**
  181. * This function resets the decoder. The context must be reset when
  182. * changing streams, or if the following stream parameters change:
  183. * number of subbands, stereo mode, or frequency.
  184. *
  185. * @param context Pointer to the decoder context structure to be reset.
  186. *
  187. * @param enhanced If true, enhanced SBC operation is enabled. If enabled,
  188. * the codec will recognize the alternative syncword for
  189. * decoding an enhanced SBC stream. Enhancements should not
  190. * be enabled unless the stream is known to be generated
  191. * by an enhanced encoder, or there is a small possibility
  192. * for decoding glitches if synchronization were to be lost.
  193. */
  194. OI_STATUS OI_CODEC_SBC_DecoderReset(OI_CODEC_SBC_DECODER_CONTEXT *context,
  195. OI_UINT32 *decoderData,
  196. OI_UINT32 decoderDataBytes,
  197. OI_UINT8 maxChannels,
  198. OI_UINT8 pcmStride,
  199. OI_BOOL enhanced);
  200. /* BK4BTSTACK_CHANGE START */
  201. OI_STATUS OI_CODEC_mSBC_DecoderReset(OI_CODEC_SBC_DECODER_CONTEXT *context,
  202. OI_UINT32 *decoderData,
  203. OI_UINT32 decoderDataBytes);
  204. /* BK4BTSTACK_CHANGE END */
  205. /**
  206. * This function restricts the kind of SBC frames that the Decoder will
  207. * process. Its use is optional. If used, it must be called after
  208. * calling OI_CODEC_SBC_DecoderReset(). After it is called, any calls
  209. * to OI_CODEC_SBC_DecodeFrame() with SBC frames that do not conform
  210. * to the Subband and Enhanced SBC setting will be rejected with an
  211. * OI_STATUS_INVALID_PARAMETERS return.
  212. *
  213. * @param context Pointer to the decoder context structure to be limited.
  214. *
  215. * @param enhanced If true, all frames passed to the decoder must be
  216. * Enhanced SBC frames. If false, all frames must be
  217. * standard SBC frames.
  218. *
  219. * @param subbands May be set to SBC_SUBBANDS_4 or SBC_SUBBANDS_8. All
  220. * frames passed to the decoder must be encoded with
  221. * the requested number of subbands.
  222. *
  223. */
  224. OI_STATUS OI_CODEC_SBC_DecoderLimit(OI_CODEC_SBC_DECODER_CONTEXT *context,
  225. OI_BOOL enhanced,
  226. OI_UINT8 subbands);
  227. /**
  228. * This function sets the decoder parameters for a raw decode where the decoder parameters are not
  229. * available in the sbc data stream. OI_CODEC_SBC_DecoderReset must be called
  230. * prior to calling this function.
  231. *
  232. * @param context Decoder context structure. This must be the context must be
  233. * used each time a frame is decoded.
  234. *
  235. * @param enhanced Set to TRUE to enable Qualcomm proprietary
  236. * quality enhancements.
  237. *
  238. * @param frequency One of SBC_FREQ_16000, SBC_FREQ_32000, SBC_FREQ_44100,
  239. * SBC_FREQ_48000
  240. *
  241. * @param mode One of SBC_MONO, SBC_DUAL_CHANNEL, SBC_STEREO,
  242. * SBC_JOINT_STEREO
  243. *
  244. * @param subbands One of SBC_SUBBANDS_4, SBC_SUBBANDS_8
  245. *
  246. * @param blocks One of SBC_BLOCKS_4, SBC_BLOCKS_8, SBC_BLOCKS_12,
  247. * SBC_BLOCKS_16
  248. *
  249. * @param alloc One of SBC_LOUDNESS, SBC_SNR
  250. *
  251. * @param maxBitpool The maximum bitpool size for this context
  252. */
  253. OI_STATUS OI_CODEC_SBC_DecoderConfigureRaw(OI_CODEC_SBC_DECODER_CONTEXT *context,
  254. OI_BOOL enhanced,
  255. OI_UINT8 frequency,
  256. OI_UINT8 mode,
  257. OI_UINT8 subbands,
  258. OI_UINT8 blocks,
  259. OI_UINT8 alloc,
  260. OI_UINT8 maxBitpool);
  261. /**
  262. * Decode one SBC frame. The frame has no header bytes. The context must have been previously
  263. * initialized by calling OI_CODEC_SBC_DecoderConfigureRaw().
  264. *
  265. * @param context Pointer to a decoder context structure. The same context
  266. * must be used each time when decoding from the same stream.
  267. *
  268. * @param bitpool The actual bitpool size for this frame. Must be <= the maxbitpool specified
  269. * in the call to OI_CODEC_SBC_DecoderConfigureRaw(),
  270. *
  271. * @param frameData Address of a pointer to the SBC data to decode. This
  272. * value will be updated to point to the next frame after
  273. * successful decoding.
  274. *
  275. * @param frameBytes Pointer to a UINT32 containing the number of available
  276. * bytes of frame data. This value will be updated to reflect
  277. * the number of bytes remaining after a decoding operation.
  278. *
  279. * @param pcmData Address of an array of OI_INT16 pairs, which will be
  280. * populated with the decoded audio data. This address
  281. * is not updated.
  282. *
  283. * @param pcmBytes Pointer to a UINT32 in/out parameter. On input, it
  284. * should contain the number of bytes available for pcm
  285. * data. On output, it will contain the number of bytes
  286. * written. Note that this differs from the semantics of
  287. * frameBytes.
  288. */
  289. OI_STATUS OI_CODEC_SBC_DecodeRaw(OI_CODEC_SBC_DECODER_CONTEXT *context,
  290. OI_UINT8 bitpool,
  291. const OI_BYTE **frameData,
  292. OI_UINT32 *frameBytes,
  293. OI_INT16 *pcmData,
  294. OI_UINT32 *pcmBytes);
  295. /**
  296. * Decode one SBC frame.
  297. *
  298. * @param context Pointer to a decoder context structure. The same context
  299. * must be used each time when decoding from the same stream.
  300. *
  301. * @param frameData Address of a pointer to the SBC data to decode. This
  302. * value will be updated to point to the next frame after
  303. * successful decoding.
  304. *
  305. * @param frameBytes Pointer to a UINT32 containing the number of available
  306. * bytes of frame data. This value will be updated to reflect
  307. * the number of bytes remaining after a decoding operation.
  308. *
  309. * @param pcmData Address of an array of OI_INT16 pairs, which will be
  310. * populated with the decoded audio data. This address
  311. * is not updated.
  312. *
  313. * @param pcmBytes Pointer to a UINT32 in/out parameter. On input, it
  314. * should contain the number of bytes available for pcm
  315. * data. On output, it will contain the number of bytes
  316. * written. Note that this differs from the semantics of
  317. * frameBytes.
  318. */
  319. OI_STATUS OI_CODEC_SBC_DecodeFrame(OI_CODEC_SBC_DECODER_CONTEXT *context,
  320. const OI_BYTE **frameData,
  321. OI_UINT32 *frameBytes,
  322. OI_INT16 *pcmData,
  323. OI_UINT32 *pcmBytes);
  324. /**
  325. * Calculate the number of SBC frames but don't decode. CRC's are not checked,
  326. * but the Sync word is found prior to count calculation.
  327. *
  328. * @param frameData Pointer to the SBC data.
  329. *
  330. * @param frameBytes Number of bytes avaiable in the frameData buffer
  331. *
  332. */
  333. OI_UINT8 OI_CODEC_SBC_FrameCount(OI_BYTE *frameData,
  334. OI_UINT32 frameBytes);
  335. /**
  336. * Analyze an SBC frame but don't do the decode.
  337. *
  338. * @param context Pointer to a decoder context structure. The same context
  339. * must be used each time when decoding from the same stream.
  340. *
  341. * @param frameData Address of a pointer to the SBC data to decode. This
  342. * value will be updated to point to the next frame after
  343. * successful decoding.
  344. *
  345. * @param frameBytes Pointer to a UINT32 containing the number of available
  346. * bytes of frame data. This value will be updated to reflect
  347. * the number of bytes remaining after a decoding operation.
  348. *
  349. */
  350. OI_STATUS OI_CODEC_SBC_SkipFrame(OI_CODEC_SBC_DECODER_CONTEXT *context,
  351. const OI_BYTE **frameData,
  352. OI_UINT32 *frameBytes);
  353. /* Common functions */
  354. /**
  355. Calculate the frame length.
  356. @param frame The frame whose length to calculate
  357. @return the length of an individual encoded frame in
  358. bytes
  359. */
  360. OI_UINT16 OI_CODEC_SBC_CalculateFramelen(OI_CODEC_SBC_FRAME_INFO *frame);
  361. /**
  362. * Calculate the maximum bitpool size that fits within a given frame length.
  363. *
  364. * @param frame The frame to calculate the bitpool size for
  365. * @param frameLen The frame length to fit the bitpool to
  366. *
  367. * @return the maximum bitpool that will fit in the specified frame length
  368. */
  369. OI_UINT16 OI_CODEC_SBC_CalculateBitpool(OI_CODEC_SBC_FRAME_INFO *frame,
  370. OI_UINT16 frameLen);
  371. /**
  372. Calculate the bit rate.
  373. @param frame The frame whose bit rate to calculate
  374. @return the approximate bit rate in bits per second,
  375. assuming that stream parameters are constant
  376. */
  377. OI_UINT32 OI_CODEC_SBC_CalculateBitrate(OI_CODEC_SBC_FRAME_INFO *frame);
  378. /**
  379. Calculate decoded audio data length for one frame.
  380. @param frame The frame whose audio data length to calculate
  381. @return length of decoded audio data for a
  382. single frame, in bytes
  383. */
  384. OI_UINT16 OI_CODEC_SBC_CalculatePcmBytes(OI_CODEC_SBC_COMMON_CONTEXT *common);
  385. /**
  386. * Get the codec version text.
  387. *
  388. * @return pointer to text string containing codec version text
  389. *
  390. */
  391. const OI_CHAR *OI_CODEC_Version(void);
  392. /**
  393. @}
  394. @addtogroup codec_internal
  395. @{
  396. */
  397. extern const OI_CHAR* const OI_CODEC_SBC_FreqText[];
  398. extern const OI_CHAR* const OI_CODEC_SBC_ModeText[];
  399. extern const OI_CHAR* const OI_CODEC_SBC_SubbandsText[];
  400. extern const OI_CHAR* const OI_CODEC_SBC_BlocksText[];
  401. extern const OI_CHAR* const OI_CODEC_SBC_AllocText[];
  402. /**
  403. @}
  404. @addtogroup codec_lib
  405. @{
  406. */
  407. #ifdef OI_DEBUG
  408. void OI_CODEC_SBC_DumpConfig(OI_CODEC_SBC_FRAME_INFO *frameInfo);
  409. #else
  410. #define OI_CODEC_SBC_DumpConfig(f)
  411. #endif
  412. /**
  413. @}
  414. */
  415. #ifdef __cplusplus
  416. }
  417. #endif
  418. #endif /* _OI_CODEC_SBC_CORE_H */