oi_codec_sbc.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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. } OI_CODEC_SBC_FRAME_INFO;
  121. /** Used internally. */
  122. typedef struct {
  123. const OI_CHAR *codecInfo;
  124. OI_CODEC_SBC_FRAME_INFO frameInfo;
  125. OI_INT8 scale_factor[SBC_MAX_CHANNELS * SBC_MAX_BANDS];
  126. OI_UINT32 frameCount;
  127. OI_INT32 *subdata;
  128. SBC_BUFFER_T *filterBuffer[SBC_MAX_CHANNELS];
  129. OI_INT32 filterBufferLen;
  130. OI_UINT filterBufferOffset;
  131. union {
  132. OI_UINT8 uint8[SBC_MAX_CHANNELS * SBC_MAX_BANDS];
  133. OI_UINT32 uint32[SBC_MAX_CHANNELS * SBC_MAX_BANDS / 4];
  134. } bits;
  135. OI_UINT8 maxBitneed; /**< Running maximum bitneed */
  136. OI_BYTE formatByte;
  137. OI_UINT8 pcmStride;
  138. OI_UINT8 maxChannels;
  139. } OI_CODEC_SBC_COMMON_CONTEXT;
  140. /*
  141. * A smaller value reduces RAM usage at the expense of increased CPU usage. Values in the range
  142. * 27..50 are recommended, beyond 50 there is a diminishing return on reduced CPU usage.
  143. */
  144. #define SBC_CODEC_MIN_FILTER_BUFFERS 16
  145. #define SBC_CODEC_FAST_FILTER_BUFFERS 27
  146. /* Expands to the number of OI_UINT32s needed to ensure enough memory to encode
  147. * or decode streams of numChannels channels, using numBuffers buffers.
  148. * Example:
  149. * OI_UINT32 decoderData[CODEC_DATA_WORDS(SBC_MAX_CHANNELS, SBC_DECODER_FAST_SYNTHESIS_BUFFERS)];
  150. * */
  151. #define CODEC_DATA_WORDS(numChannels, numBuffers) \
  152. ((\
  153. (sizeof(OI_INT32) * SBC_MAX_BLOCKS * numChannels * SBC_MAX_BANDS) \
  154. + (sizeof(SBC_BUFFER_T) * SBC_MAX_CHANNELS * SBC_MAX_BANDS * numBuffers) \
  155. + (sizeof (OI_UINT32) - 1) \
  156. ) / sizeof(OI_UINT32))
  157. /** Opaque parameter to decoding functions; maintains decoder context. */
  158. typedef struct {
  159. OI_CODEC_SBC_COMMON_CONTEXT common;
  160. OI_UINT8 limitFrameFormat; /* Boolean, set by OI_CODEC_SBC_DecoderLimit() */
  161. OI_UINT8 restrictSubbands;
  162. OI_UINT8 enhancedEnabled;
  163. OI_UINT8 bufferedBlocks;
  164. } OI_CODEC_SBC_DECODER_CONTEXT;
  165. typedef struct {
  166. OI_UINT32 data[CODEC_DATA_WORDS(1, SBC_CODEC_FAST_FILTER_BUFFERS)];
  167. } OI_CODEC_SBC_CODEC_DATA_MONO;
  168. typedef struct {
  169. OI_UINT32 data[CODEC_DATA_WORDS(2, SBC_CODEC_FAST_FILTER_BUFFERS)];
  170. } OI_CODEC_SBC_CODEC_DATA_STEREO;
  171. /**
  172. @}
  173. @addtogroup codec_lib
  174. @{
  175. */
  176. /**
  177. * This function resets the decoder. The context must be reset when
  178. * changing streams, or if the following stream parameters change:
  179. * number of subbands, stereo mode, or frequency.
  180. *
  181. * @param context Pointer to the decoder context structure to be reset.
  182. *
  183. * @param enhanced If true, enhanced SBC operation is enabled. If enabled,
  184. * the codec will recognize the alternative syncword for
  185. * decoding an enhanced SBC stream. Enhancements should not
  186. * be enabled unless the stream is known to be generated
  187. * by an enhanced encoder, or there is a small possibility
  188. * for decoding glitches if synchronization were to be lost.
  189. */
  190. OI_STATUS OI_CODEC_SBC_DecoderReset(OI_CODEC_SBC_DECODER_CONTEXT *context,
  191. OI_UINT32 *decoderData,
  192. OI_UINT32 decoderDataBytes,
  193. OI_UINT8 maxChannels,
  194. OI_UINT8 pcmStride,
  195. OI_BOOL enhanced);
  196. /**
  197. * This function restricts the kind of SBC frames that the Decoder will
  198. * process. Its use is optional. If used, it must be called after
  199. * calling OI_CODEC_SBC_DecoderReset(). After it is called, any calls
  200. * to OI_CODEC_SBC_DecodeFrame() with SBC frames that do not conform
  201. * to the Subband and Enhanced SBC setting will be rejected with an
  202. * OI_STATUS_INVALID_PARAMETERS return.
  203. *
  204. * @param context Pointer to the decoder context structure to be limited.
  205. *
  206. * @param enhanced If true, all frames passed to the decoder must be
  207. * Enhanced SBC frames. If false, all frames must be
  208. * standard SBC frames.
  209. *
  210. * @param subbands May be set to SBC_SUBBANDS_4 or SBC_SUBBANDS_8. All
  211. * frames passed to the decoder must be encoded with
  212. * the requested number of subbands.
  213. *
  214. */
  215. OI_STATUS OI_CODEC_SBC_DecoderLimit(OI_CODEC_SBC_DECODER_CONTEXT *context,
  216. OI_BOOL enhanced,
  217. OI_UINT8 subbands);
  218. /**
  219. * This function sets the decoder parameters for a raw decode where the decoder parameters are not
  220. * available in the sbc data stream. OI_CODEC_SBC_DecoderReset must be called
  221. * prior to calling this function.
  222. *
  223. * @param context Decoder context structure. This must be the context must be
  224. * used each time a frame is decoded.
  225. *
  226. * @param enhanced Set to TRUE to enable Qualcomm proprietary
  227. * quality enhancements.
  228. *
  229. * @param frequency One of SBC_FREQ_16000, SBC_FREQ_32000, SBC_FREQ_44100,
  230. * SBC_FREQ_48000
  231. *
  232. * @param mode One of SBC_MONO, SBC_DUAL_CHANNEL, SBC_STEREO,
  233. * SBC_JOINT_STEREO
  234. *
  235. * @param subbands One of SBC_SUBBANDS_4, SBC_SUBBANDS_8
  236. *
  237. * @param blocks One of SBC_BLOCKS_4, SBC_BLOCKS_8, SBC_BLOCKS_12,
  238. * SBC_BLOCKS_16
  239. *
  240. * @param alloc One of SBC_LOUDNESS, SBC_SNR
  241. *
  242. * @param maxBitpool The maximum bitpool size for this context
  243. */
  244. OI_STATUS OI_CODEC_SBC_DecoderConfigureRaw(OI_CODEC_SBC_DECODER_CONTEXT *context,
  245. OI_BOOL enhanced,
  246. OI_UINT8 frequency,
  247. OI_UINT8 mode,
  248. OI_UINT8 subbands,
  249. OI_UINT8 blocks,
  250. OI_UINT8 alloc,
  251. OI_UINT8 maxBitpool);
  252. /**
  253. * Decode one SBC frame. The frame has no header bytes. The context must have been previously
  254. * initialized by calling OI_CODEC_SBC_DecoderConfigureRaw().
  255. *
  256. * @param context Pointer to a decoder context structure. The same context
  257. * must be used each time when decoding from the same stream.
  258. *
  259. * @param bitpool The actual bitpool size for this frame. Must be <= the maxbitpool specified
  260. * in the call to OI_CODEC_SBC_DecoderConfigureRaw(),
  261. *
  262. * @param frameData Address of a pointer to the SBC data to decode. This
  263. * value will be updated to point to the next frame after
  264. * successful decoding.
  265. *
  266. * @param frameBytes Pointer to a UINT32 containing the number of available
  267. * bytes of frame data. This value will be updated to reflect
  268. * the number of bytes remaining after a decoding operation.
  269. *
  270. * @param pcmData Address of an array of OI_INT16 pairs, which will be
  271. * populated with the decoded audio data. This address
  272. * is not updated.
  273. *
  274. * @param pcmBytes Pointer to a UINT32 in/out parameter. On input, it
  275. * should contain the number of bytes available for pcm
  276. * data. On output, it will contain the number of bytes
  277. * written. Note that this differs from the semantics of
  278. * frameBytes.
  279. */
  280. OI_STATUS OI_CODEC_SBC_DecodeRaw(OI_CODEC_SBC_DECODER_CONTEXT *context,
  281. OI_UINT8 bitpool,
  282. const OI_BYTE **frameData,
  283. OI_UINT32 *frameBytes,
  284. OI_INT16 *pcmData,
  285. OI_UINT32 *pcmBytes);
  286. /**
  287. * Decode one SBC frame.
  288. *
  289. * @param context Pointer to a decoder context structure. The same context
  290. * must be used each time when decoding from the same stream.
  291. *
  292. * @param frameData Address of a pointer to the SBC data to decode. This
  293. * value will be updated to point to the next frame after
  294. * successful decoding.
  295. *
  296. * @param frameBytes Pointer to a UINT32 containing the number of available
  297. * bytes of frame data. This value will be updated to reflect
  298. * the number of bytes remaining after a decoding operation.
  299. *
  300. * @param pcmData Address of an array of OI_INT16 pairs, which will be
  301. * populated with the decoded audio data. This address
  302. * is not updated.
  303. *
  304. * @param pcmBytes Pointer to a UINT32 in/out parameter. On input, it
  305. * should contain the number of bytes available for pcm
  306. * data. On output, it will contain the number of bytes
  307. * written. Note that this differs from the semantics of
  308. * frameBytes.
  309. */
  310. OI_STATUS OI_CODEC_SBC_DecodeFrame(OI_CODEC_SBC_DECODER_CONTEXT *context,
  311. const OI_BYTE **frameData,
  312. OI_UINT32 *frameBytes,
  313. OI_INT16 *pcmData,
  314. OI_UINT32 *pcmBytes);
  315. /**
  316. * Calculate the number of SBC frames but don't decode. CRC's are not checked,
  317. * but the Sync word is found prior to count calculation.
  318. *
  319. * @param frameData Pointer to the SBC data.
  320. *
  321. * @param frameBytes Number of bytes avaiable in the frameData buffer
  322. *
  323. */
  324. OI_UINT8 OI_CODEC_SBC_FrameCount(OI_BYTE *frameData,
  325. OI_UINT32 frameBytes);
  326. /**
  327. * Analyze an SBC frame but don't do the decode.
  328. *
  329. * @param context Pointer to a decoder context structure. The same context
  330. * must be used each time when decoding from the same stream.
  331. *
  332. * @param frameData Address of a pointer to the SBC data to decode. This
  333. * value will be updated to point to the next frame after
  334. * successful decoding.
  335. *
  336. * @param frameBytes Pointer to a UINT32 containing the number of available
  337. * bytes of frame data. This value will be updated to reflect
  338. * the number of bytes remaining after a decoding operation.
  339. *
  340. */
  341. OI_STATUS OI_CODEC_SBC_SkipFrame(OI_CODEC_SBC_DECODER_CONTEXT *context,
  342. const OI_BYTE **frameData,
  343. OI_UINT32 *frameBytes);
  344. /* Common functions */
  345. /**
  346. Calculate the frame length.
  347. @param frame The frame whose length to calculate
  348. @return the length of an individual encoded frame in
  349. bytes
  350. */
  351. OI_UINT16 OI_CODEC_SBC_CalculateFramelen(OI_CODEC_SBC_FRAME_INFO *frame);
  352. /**
  353. * Calculate the maximum bitpool size that fits within a given frame length.
  354. *
  355. * @param frame The frame to calculate the bitpool size for
  356. * @param frameLen The frame length to fit the bitpool to
  357. *
  358. * @return the maximum bitpool that will fit in the specified frame length
  359. */
  360. OI_UINT16 OI_CODEC_SBC_CalculateBitpool(OI_CODEC_SBC_FRAME_INFO *frame,
  361. OI_UINT16 frameLen);
  362. /**
  363. Calculate the bit rate.
  364. @param frame The frame whose bit rate to calculate
  365. @return the approximate bit rate in bits per second,
  366. assuming that stream parameters are constant
  367. */
  368. OI_UINT32 OI_CODEC_SBC_CalculateBitrate(OI_CODEC_SBC_FRAME_INFO *frame);
  369. /**
  370. Calculate decoded audio data length for one frame.
  371. @param frame The frame whose audio data length to calculate
  372. @return length of decoded audio data for a
  373. single frame, in bytes
  374. */
  375. OI_UINT16 OI_CODEC_SBC_CalculatePcmBytes(OI_CODEC_SBC_COMMON_CONTEXT *common);
  376. /**
  377. * Get the codec version text.
  378. *
  379. * @return pointer to text string containing codec version text
  380. *
  381. */
  382. OI_CHAR *OI_CODEC_Version(void);
  383. /**
  384. @}
  385. @addtogroup codec_internal
  386. @{
  387. */
  388. extern const OI_CHAR *const OI_CODEC_SBC_FreqText[];
  389. extern const OI_CHAR *const OI_CODEC_SBC_ModeText[];
  390. extern const OI_CHAR *const OI_CODEC_SBC_SubbandsText[];
  391. extern const OI_CHAR *const OI_CODEC_SBC_BlocksText[];
  392. extern const OI_CHAR *const OI_CODEC_SBC_AllocText[];
  393. /**
  394. @}
  395. @addtogroup codec_lib
  396. @{
  397. */
  398. #ifdef OI_DEBUG
  399. void OI_CODEC_SBC_DumpConfig(OI_CODEC_SBC_FRAME_INFO *frameInfo);
  400. #else
  401. #define OI_CODEC_SBC_DumpConfig(f)
  402. #endif
  403. /**
  404. @}
  405. */
  406. #ifdef __cplusplus
  407. }
  408. #endif
  409. #endif /* _OI_CODEC_SBC_CORE_H */