a2d_sbc.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /******************************************************************************
  2. *
  3. * Copyright (C) 2002-2012 Broadcom Corporation
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at:
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. ******************************************************************************/
  18. /******************************************************************************
  19. *
  20. * Utility functions to help build and parse SBC Codec Information Element
  21. * and Media Payload.
  22. *
  23. ******************************************************************************/
  24. #include "bt_target.h"
  25. #include <string.h>
  26. #include "a2d_api.h"
  27. #include "a2d_int.h"
  28. #include "a2d_sbc.h"
  29. #include "bt_utils.h"
  30. #if (defined(A2D_INCLUDED) && A2D_INCLUDED == TRUE)
  31. /*************************************************************************************************
  32. * SBC descramble code
  33. * Purpose: to tie the SBC code with BTE/mobile stack code,
  34. * especially for the case when the SBC is ported into a third-party Multimedia chip
  35. *
  36. * Algorithm:
  37. * init process: all counters reset to 0,
  38. * calculate base_index: (6 + s16NumOfChannels*s16NumOfSubBands/2)
  39. * scramble side: the init process happens every time SBC_Encoder_Init() is called.
  40. * descramble side: it would be nice to know if he "init" process has happened.
  41. * alter the SBC SYNC word 0x9C (1001 1100) to 0x8C (1000 1100).
  42. *
  43. * scramble process:
  44. * The CRC byte:
  45. * Every SBC frame has a frame header.
  46. * The 1st byte is the sync word and the following 2 bytes are about the stream format.
  47. * They are supposed to be "constant" within a "song"
  48. * The 4th byte is the CRC byte. The CRC byte is bound to be random.
  49. * Derive 2 items from the CRC byte; one is the "use" bit, the other is the "index".
  50. *
  51. * SBC keeps 2 sets of "use" & "index"; derived the current and the previous frame.
  52. *
  53. * The "use" bit is any bit in SBC_PRTC_USE_MASK is set.
  54. * If set, SBC uses the "index" from the current frame.
  55. * If not set, SBC uses the "index" from the previous frame or 0.
  56. *
  57. * index = (CRC & 0x3) + ((CRC & 0x30) >> 2) // 8 is the max index
  58. *
  59. * if(index > 0)
  60. * {
  61. * p = &u8frame[base_index];
  62. * if((index&1)&&(u16PacketLength > (base_index+index*2)))
  63. * {
  64. * // odd index: swap 2 bytes
  65. * tmp = p[index];
  66. * p[index] = p[index*2];
  67. * p[index*2] = tmp;
  68. * }
  69. * else
  70. * {
  71. * // even index: shift by 3
  72. * tmp = (p[index] >> 3) + (p[index] << 5);
  73. * p[index] = tmp;
  74. * }
  75. * }
  76. * //else index is 0. The frame stays unaltered
  77. *
  78. */
  79. #define A2D_SBC_SYNC_WORD 0x9C
  80. #define A2D_SBC_CRC_IDX 3
  81. #define A2D_SBC_USE_MASK 0x64
  82. #define A2D_SBC_SYNC_MASK 0x10
  83. #define A2D_SBC_CIDX 0
  84. #define A2D_SBC_LIDX 1
  85. #define A2D_SBC_CH_M_BITS 0xC /* channel mode bits: 0: mono; 1 ch */
  86. #define A2D_SBC_SUBBAND_BIT 0x1 /* num of subband bit: 0:4; 1: 8 */
  87. #define A2D_SBC_GET_IDX(sc) (((sc) & 0x3) + (((sc) & 0x30) >> 2))
  88. typedef struct {
  89. UINT8 use;
  90. UINT8 idx;
  91. } tA2D_SBC_FR_CB;
  92. typedef struct {
  93. tA2D_SBC_FR_CB fr[2];
  94. UINT8 index;
  95. UINT8 base;
  96. } tA2D_SBC_DS_CB;
  97. static tA2D_SBC_DS_CB a2d_sbc_ds_cb;
  98. /*int a2d_count = 0;*/
  99. /******************************************************************************
  100. **
  101. ** Function A2D_SbcChkFrInit
  102. **
  103. ** Description check if need to init the descramble control block.
  104. **
  105. ** Returns nothing.
  106. ******************************************************************************/
  107. void A2D_SbcChkFrInit(UINT8 *p_pkt)
  108. {
  109. UINT8 fmt;
  110. UINT8 num_chnl = 1;
  111. UINT8 num_subband = 4;
  112. if ((p_pkt[0] & A2D_SBC_SYNC_MASK) == 0) {
  113. a2d_cb.use_desc = TRUE;
  114. fmt = p_pkt[1];
  115. p_pkt[0] |= A2D_SBC_SYNC_MASK;
  116. memset(&a2d_sbc_ds_cb, 0, sizeof(tA2D_SBC_DS_CB));
  117. if (fmt & A2D_SBC_CH_M_BITS) {
  118. num_chnl = 2;
  119. }
  120. if (fmt & A2D_SBC_SUBBAND_BIT) {
  121. num_subband = 8;
  122. }
  123. a2d_sbc_ds_cb.base = 6 + num_chnl * num_subband / 2;
  124. /*printf("base: %d\n", a2d_sbc_ds_cb.base);
  125. a2d_count = 0;*/
  126. }
  127. }
  128. /******************************************************************************
  129. **
  130. ** Function A2D_SbcDescramble
  131. **
  132. ** Description descramble the packet.
  133. **
  134. ** Returns nothing.
  135. ******************************************************************************/
  136. void A2D_SbcDescramble(UINT8 *p_pkt, UINT16 len)
  137. {
  138. tA2D_SBC_FR_CB *p_cur, *p_last;
  139. UINT32 idx, tmp, tmp2;
  140. if (a2d_cb.use_desc) {
  141. /* c2l */
  142. p_last = &a2d_sbc_ds_cb.fr[A2D_SBC_LIDX];
  143. p_cur = &a2d_sbc_ds_cb.fr[A2D_SBC_CIDX];
  144. p_last->idx = p_cur->idx;
  145. p_last->use = p_cur->use;
  146. /* getc */
  147. p_cur->use = p_pkt[A2D_SBC_CRC_IDX] & A2D_SBC_USE_MASK;
  148. p_cur->idx = A2D_SBC_GET_IDX(p_pkt[A2D_SBC_CRC_IDX]);
  149. a2d_sbc_ds_cb.index = (p_cur->use) ? A2D_SBC_CIDX : A2D_SBC_LIDX;
  150. /*
  151. printf("%05d: ar[%02d]: x%02x, msk: x%02x, use: %s, idx: %02d, ",
  152. a2d_count++,
  153. A2D_SBC_CRC_IDX, p_pkt[A2D_SBC_CRC_IDX], A2D_SBC_USE_MASK,
  154. (p_cur->use)?"cur":"lst", p_cur->idx);
  155. */
  156. /* descramble */
  157. idx = a2d_sbc_ds_cb.fr[a2d_sbc_ds_cb.index].idx;
  158. if (idx > 0) {
  159. p_pkt = &p_pkt[a2d_sbc_ds_cb.base];
  160. if ((idx & 1) && (len > (a2d_sbc_ds_cb.base + (idx << 1)))) {
  161. tmp2 = (idx << 1);
  162. tmp = p_pkt[idx];
  163. p_pkt[idx] = p_pkt[tmp2];
  164. p_pkt[tmp2] = tmp;
  165. /*
  166. printf("tmp2: %02d, len: %d, idx: %d\n",
  167. tmp2, len, a2d_sbc_ds_cb.fr[a2d_sbc_ds_cb.index].idx);
  168. */
  169. } else {
  170. tmp2 = p_pkt[idx];
  171. tmp = (tmp2 >> 3) + (tmp2 << 5);
  172. p_pkt[idx] = (UINT8)tmp;
  173. /*
  174. printf("tmp: x%02x, len: %d, idx: %d(cmp:%d)\n",
  175. (UINT8)tmp2, len, a2d_sbc_ds_cb.fr[a2d_sbc_ds_cb.index].idx,
  176. (a2d_sbc_ds_cb.base+(idx<<1)));
  177. */
  178. }
  179. }
  180. /*
  181. else
  182. {
  183. printf("!!!!\n");
  184. }
  185. */
  186. }
  187. }
  188. /******************************************************************************
  189. **
  190. ** Function A2D_BldSbcInfo
  191. **
  192. ** Description This function is called by an application to build
  193. ** the SBC Media Codec Capabilities byte sequence
  194. ** beginning from the LOSC octet.
  195. ** Input Parameters:
  196. ** media_type: Indicates Audio, or Multimedia.
  197. **
  198. ** p_ie: The SBC Codec Information Element information.
  199. **
  200. ** Output Parameters:
  201. ** p_result: the resulting codec info byte sequence.
  202. **
  203. ** Returns A2D_SUCCESS if function execution succeeded.
  204. ** Error status code, otherwise.
  205. ******************************************************************************/
  206. tA2D_STATUS A2D_BldSbcInfo(UINT8 media_type, tA2D_SBC_CIE *p_ie, UINT8 *p_result)
  207. {
  208. tA2D_STATUS status;
  209. if ( p_ie == NULL || p_result == NULL ||
  210. (p_ie->samp_freq & ~A2D_SBC_IE_SAMP_FREQ_MSK) ||
  211. (p_ie->ch_mode & ~A2D_SBC_IE_CH_MD_MSK) ||
  212. (p_ie->block_len & ~A2D_SBC_IE_BLOCKS_MSK) ||
  213. (p_ie->num_subbands & ~A2D_SBC_IE_SUBBAND_MSK) ||
  214. (p_ie->alloc_mthd & ~A2D_SBC_IE_ALLOC_MD_MSK) ||
  215. (p_ie->max_bitpool < p_ie->min_bitpool) ||
  216. (p_ie->max_bitpool < A2D_SBC_IE_MIN_BITPOOL) ||
  217. (p_ie->max_bitpool > A2D_SBC_IE_MAX_BITPOOL) ||
  218. (p_ie->min_bitpool < A2D_SBC_IE_MIN_BITPOOL) ||
  219. (p_ie->min_bitpool > A2D_SBC_IE_MAX_BITPOOL) ) {
  220. /* if any unused bit is set */
  221. status = A2D_INVALID_PARAMS;
  222. } else {
  223. status = A2D_SUCCESS;
  224. *p_result++ = A2D_SBC_INFO_LEN;
  225. *p_result++ = media_type;
  226. *p_result++ = A2D_MEDIA_CT_SBC;
  227. /* Media Codec Specific Information Element */
  228. *p_result++ = p_ie->samp_freq | p_ie->ch_mode;
  229. *p_result++ = p_ie->block_len | p_ie->num_subbands | p_ie->alloc_mthd;
  230. *p_result++ = p_ie->min_bitpool;
  231. *p_result = p_ie->max_bitpool;
  232. }
  233. return status;
  234. }
  235. /******************************************************************************
  236. **
  237. ** Function A2D_ParsSbcInfo
  238. **
  239. ** Description This function is called by an application to parse
  240. ** the SBC Media Codec Capabilities byte sequence
  241. ** beginning from the LOSC octet.
  242. ** Input Parameters:
  243. ** p_info: the byte sequence to parse.
  244. **
  245. ** for_caps: TRUE, if the byte sequence is for get capabilities response.
  246. **
  247. ** Output Parameters:
  248. ** p_ie: The SBC Codec Information Element information.
  249. **
  250. ** Returns A2D_SUCCESS if function execution succeeded.
  251. ** Error status code, otherwise.
  252. ******************************************************************************/
  253. tA2D_STATUS A2D_ParsSbcInfo(tA2D_SBC_CIE *p_ie, UINT8 *p_info, BOOLEAN for_caps)
  254. {
  255. tA2D_STATUS status;
  256. UINT8 losc;
  257. if ( p_ie == NULL || p_info == NULL) {
  258. status = A2D_INVALID_PARAMS;
  259. } else {
  260. losc = *p_info++;
  261. p_info++;
  262. /* If the function is called for the wrong Media Type or Media Codec Type */
  263. if (losc != A2D_SBC_INFO_LEN || *p_info != A2D_MEDIA_CT_SBC) {
  264. status = A2D_WRONG_CODEC;
  265. } else {
  266. p_info++;
  267. p_ie->samp_freq = *p_info & A2D_SBC_IE_SAMP_FREQ_MSK;
  268. p_ie->ch_mode = *p_info & A2D_SBC_IE_CH_MD_MSK;
  269. p_info++;
  270. p_ie->block_len = *p_info & A2D_SBC_IE_BLOCKS_MSK;
  271. p_ie->num_subbands = *p_info & A2D_SBC_IE_SUBBAND_MSK;
  272. p_ie->alloc_mthd = *p_info & A2D_SBC_IE_ALLOC_MD_MSK;
  273. p_info++;
  274. p_ie->min_bitpool = *p_info++;
  275. p_ie->max_bitpool = *p_info;
  276. status = A2D_SUCCESS;
  277. if (p_ie->min_bitpool < A2D_SBC_IE_MIN_BITPOOL || p_ie->min_bitpool > A2D_SBC_IE_MAX_BITPOOL ) {
  278. status = A2D_BAD_MIN_BITPOOL;
  279. }
  280. if (p_ie->max_bitpool < A2D_SBC_IE_MIN_BITPOOL || p_ie->max_bitpool > A2D_SBC_IE_MAX_BITPOOL ||
  281. p_ie->max_bitpool < p_ie->min_bitpool) {
  282. status = A2D_BAD_MAX_BITPOOL;
  283. }
  284. if (for_caps == FALSE) {
  285. if (A2D_BitsSet(p_ie->samp_freq) != A2D_SET_ONE_BIT) {
  286. status = A2D_BAD_SAMP_FREQ;
  287. }
  288. if (A2D_BitsSet(p_ie->ch_mode) != A2D_SET_ONE_BIT) {
  289. status = A2D_BAD_CH_MODE;
  290. }
  291. if (A2D_BitsSet(p_ie->block_len) != A2D_SET_ONE_BIT) {
  292. status = A2D_BAD_BLOCK_LEN;
  293. }
  294. if (A2D_BitsSet(p_ie->num_subbands) != A2D_SET_ONE_BIT) {
  295. status = A2D_BAD_SUBBANDS;
  296. }
  297. if (A2D_BitsSet(p_ie->alloc_mthd) != A2D_SET_ONE_BIT) {
  298. status = A2D_BAD_ALLOC_MTHD;
  299. }
  300. }
  301. }
  302. }
  303. return status;
  304. }
  305. /******************************************************************************
  306. **
  307. ** Function A2D_BldSbcMplHdr
  308. **
  309. ** Description This function is called by an application to parse
  310. ** the SBC Media Payload header.
  311. ** Input Parameters:
  312. ** frag: 1, if fragmented. 0, otherwise.
  313. **
  314. ** start: 1, if the starting packet of a fragmented frame.
  315. **
  316. ** last: 1, if the last packet of a fragmented frame.
  317. **
  318. ** num: If frag is 1, this is the number of remaining fragments
  319. ** (including this fragment) of this frame.
  320. ** If frag is 0, this is the number of frames in this packet.
  321. **
  322. ** Output Parameters:
  323. ** p_dst: the resulting media payload header byte sequence.
  324. **
  325. ** Returns void.
  326. ******************************************************************************/
  327. void A2D_BldSbcMplHdr(UINT8 *p_dst, BOOLEAN frag, BOOLEAN start, BOOLEAN last, UINT8 num)
  328. {
  329. if (p_dst) {
  330. *p_dst = 0;
  331. if (frag) {
  332. *p_dst |= A2D_SBC_HDR_F_MSK;
  333. }
  334. if (start) {
  335. *p_dst |= A2D_SBC_HDR_S_MSK;
  336. }
  337. if (last) {
  338. *p_dst |= A2D_SBC_HDR_L_MSK;
  339. }
  340. *p_dst |= (A2D_SBC_HDR_NUM_MSK & num);
  341. }
  342. }
  343. /******************************************************************************
  344. **
  345. ** Function A2D_ParsSbcMplHdr
  346. **
  347. ** Description This function is called by an application to parse
  348. ** the SBC Media Payload header.
  349. ** Input Parameters:
  350. ** p_src: the byte sequence to parse..
  351. **
  352. ** Output Parameters:
  353. ** frag: 1, if fragmented. 0, otherwise.
  354. **
  355. ** start: 1, if the starting packet of a fragmented frame.
  356. **
  357. ** last: 1, if the last packet of a fragmented frame.
  358. **
  359. ** num: If frag is 1, this is the number of remaining fragments
  360. ** (including this fragment) of this frame.
  361. ** If frag is 0, this is the number of frames in this packet.
  362. **
  363. ** Returns void.
  364. ******************************************************************************/
  365. void A2D_ParsSbcMplHdr(UINT8 *p_src, BOOLEAN *p_frag, BOOLEAN *p_start, BOOLEAN *p_last, UINT8 *p_num)
  366. {
  367. if (p_src && p_frag && p_start && p_last && p_num) {
  368. *p_frag = (*p_src & A2D_SBC_HDR_F_MSK) ? TRUE : FALSE;
  369. *p_start = (*p_src & A2D_SBC_HDR_S_MSK) ? TRUE : FALSE;
  370. *p_last = (*p_src & A2D_SBC_HDR_L_MSK) ? TRUE : FALSE;
  371. *p_num = (*p_src & A2D_SBC_HDR_NUM_MSK);
  372. }
  373. }
  374. #endif /* #if (defined(A2D_INCLUDED) && A2D_INCLUDED == TRUE) */