a2d_sbc.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 "common/bt_target.h"
  25. #include <string.h>
  26. #include "stack/a2d_api.h"
  27. #include "a2d_int.h"
  28. #include "stack/a2d_sbc.h"
  29. #include "common/bt_defs.h"
  30. #if (defined(A2D_INCLUDED) && A2D_INCLUDED == TRUE)
  31. /******************************************************************************
  32. **
  33. ** Function A2D_BldSbcInfo
  34. **
  35. ** Description This function is called by an application to build
  36. ** the SBC Media Codec Capabilities byte sequence
  37. ** beginning from the LOSC octet.
  38. ** Input Parameters:
  39. ** media_type: Indicates Audio, or Multimedia.
  40. **
  41. ** p_ie: The SBC Codec Information Element information.
  42. **
  43. ** Output Parameters:
  44. ** p_result: the resulting codec info byte sequence.
  45. **
  46. ** Returns A2D_SUCCESS if function execution succeeded.
  47. ** Error status code, otherwise.
  48. ******************************************************************************/
  49. tA2D_STATUS A2D_BldSbcInfo(UINT8 media_type, tA2D_SBC_CIE *p_ie, UINT8 *p_result)
  50. {
  51. tA2D_STATUS status;
  52. if ( p_ie == NULL || p_result == NULL ||
  53. (p_ie->samp_freq & ~A2D_SBC_IE_SAMP_FREQ_MSK) ||
  54. (p_ie->ch_mode & ~A2D_SBC_IE_CH_MD_MSK) ||
  55. (p_ie->block_len & ~A2D_SBC_IE_BLOCKS_MSK) ||
  56. (p_ie->num_subbands & ~A2D_SBC_IE_SUBBAND_MSK) ||
  57. (p_ie->alloc_mthd & ~A2D_SBC_IE_ALLOC_MD_MSK) ||
  58. (p_ie->max_bitpool < p_ie->min_bitpool) ||
  59. (p_ie->max_bitpool < A2D_SBC_IE_MIN_BITPOOL) ||
  60. (p_ie->max_bitpool > A2D_SBC_IE_MAX_BITPOOL) ||
  61. (p_ie->min_bitpool < A2D_SBC_IE_MIN_BITPOOL) ||
  62. (p_ie->min_bitpool > A2D_SBC_IE_MAX_BITPOOL) ) {
  63. /* if any unused bit is set */
  64. status = A2D_INVALID_PARAMS;
  65. } else {
  66. status = A2D_SUCCESS;
  67. *p_result++ = A2D_SBC_INFO_LEN;
  68. *p_result++ = media_type;
  69. *p_result++ = A2D_MEDIA_CT_SBC;
  70. /* Media Codec Specific Information Element */
  71. *p_result++ = p_ie->samp_freq | p_ie->ch_mode;
  72. *p_result++ = p_ie->block_len | p_ie->num_subbands | p_ie->alloc_mthd;
  73. *p_result++ = p_ie->min_bitpool;
  74. *p_result = p_ie->max_bitpool;
  75. }
  76. return status;
  77. }
  78. /******************************************************************************
  79. **
  80. ** Function A2D_ParsSbcInfo
  81. **
  82. ** Description This function is called by an application to parse
  83. ** the SBC Media Codec Capabilities byte sequence
  84. ** beginning from the LOSC octet.
  85. ** Input Parameters:
  86. ** p_info: the byte sequence to parse.
  87. **
  88. ** for_caps: TRUE, if the byte sequence is for get capabilities response.
  89. **
  90. ** Output Parameters:
  91. ** p_ie: The SBC Codec Information Element information.
  92. **
  93. ** Returns A2D_SUCCESS if function execution succeeded.
  94. ** Error status code, otherwise.
  95. ******************************************************************************/
  96. tA2D_STATUS A2D_ParsSbcInfo(tA2D_SBC_CIE *p_ie, UINT8 *p_info, BOOLEAN for_caps)
  97. {
  98. tA2D_STATUS status = A2D_SUCCESS;
  99. UINT8 losc;
  100. if ( p_ie == NULL || p_info == NULL) {
  101. status = A2D_INVALID_PARAMS;
  102. } else {
  103. losc = *p_info++;
  104. p_info++;
  105. /* If the function is called for the wrong Media Type or Media Codec Type */
  106. if (losc != A2D_SBC_INFO_LEN || *p_info != A2D_MEDIA_CT_SBC) {
  107. status = A2D_WRONG_CODEC;
  108. } else {
  109. p_info++;
  110. p_ie->samp_freq = *p_info & A2D_SBC_IE_SAMP_FREQ_MSK;
  111. p_ie->ch_mode = *p_info & A2D_SBC_IE_CH_MD_MSK;
  112. p_info++;
  113. p_ie->block_len = *p_info & A2D_SBC_IE_BLOCKS_MSK;
  114. p_ie->num_subbands = *p_info & A2D_SBC_IE_SUBBAND_MSK;
  115. p_ie->alloc_mthd = *p_info & A2D_SBC_IE_ALLOC_MD_MSK;
  116. p_info++;
  117. p_ie->min_bitpool = *p_info++;
  118. p_ie->max_bitpool = *p_info;
  119. if (p_ie->min_bitpool < A2D_SBC_IE_MIN_BITPOOL || p_ie->min_bitpool > A2D_SBC_IE_MAX_BITPOOL ) {
  120. status = A2D_BAD_MIN_BITPOOL;
  121. }
  122. if (p_ie->max_bitpool < A2D_SBC_IE_MIN_BITPOOL || p_ie->max_bitpool > A2D_SBC_IE_MAX_BITPOOL ||
  123. p_ie->max_bitpool < p_ie->min_bitpool) {
  124. status = A2D_BAD_MAX_BITPOOL;
  125. }
  126. if (for_caps == FALSE) {
  127. if (A2D_BitsSet(p_ie->samp_freq) != A2D_SET_ONE_BIT) {
  128. status = A2D_BAD_SAMP_FREQ;
  129. } else if (A2D_BitsSet(p_ie->ch_mode) != A2D_SET_ONE_BIT) {
  130. status = A2D_BAD_CH_MODE;
  131. } else if (A2D_BitsSet(p_ie->block_len) != A2D_SET_ONE_BIT) {
  132. status = A2D_BAD_BLOCK_LEN;
  133. } else if (A2D_BitsSet(p_ie->num_subbands) != A2D_SET_ONE_BIT) {
  134. status = A2D_BAD_SUBBANDS;
  135. } else if (A2D_BitsSet(p_ie->alloc_mthd) != A2D_SET_ONE_BIT) {
  136. status = A2D_BAD_ALLOC_MTHD;
  137. }
  138. }
  139. }
  140. }
  141. return status;
  142. }
  143. /******************************************************************************
  144. **
  145. ** Function A2D_BldSbcMplHdr
  146. **
  147. ** Description This function is called by an application to parse
  148. ** the SBC Media Payload header.
  149. ** Input Parameters:
  150. ** frag: 1, if fragmented. 0, otherwise.
  151. **
  152. ** start: 1, if the starting packet of a fragmented frame.
  153. **
  154. ** last: 1, if the last packet of a fragmented frame.
  155. **
  156. ** num: If frag is 1, this is the number of remaining fragments
  157. ** (including this fragment) of this frame.
  158. ** If frag is 0, this is the number of frames in this packet.
  159. **
  160. ** Output Parameters:
  161. ** p_dst: the resulting media payload header byte sequence.
  162. **
  163. ** Returns void.
  164. ******************************************************************************/
  165. void A2D_BldSbcMplHdr(UINT8 *p_dst, BOOLEAN frag, BOOLEAN start, BOOLEAN last, UINT8 num)
  166. {
  167. if (p_dst) {
  168. *p_dst = 0;
  169. if (frag) {
  170. *p_dst |= A2D_SBC_HDR_F_MSK;
  171. }
  172. if (start) {
  173. *p_dst |= A2D_SBC_HDR_S_MSK;
  174. }
  175. if (last) {
  176. *p_dst |= A2D_SBC_HDR_L_MSK;
  177. }
  178. *p_dst |= (A2D_SBC_HDR_NUM_MSK & num);
  179. }
  180. }
  181. /******************************************************************************
  182. **
  183. ** Function A2D_ParsSbcMplHdr
  184. **
  185. ** Description This function is called by an application to parse
  186. ** the SBC Media Payload header.
  187. ** Input Parameters:
  188. ** p_src: the byte sequence to parse..
  189. **
  190. ** Output Parameters:
  191. ** frag: 1, if fragmented. 0, otherwise.
  192. **
  193. ** start: 1, if the starting packet of a fragmented frame.
  194. **
  195. ** last: 1, if the last packet of a fragmented frame.
  196. **
  197. ** num: If frag is 1, this is the number of remaining fragments
  198. ** (including this fragment) of this frame.
  199. ** If frag is 0, this is the number of frames in this packet.
  200. **
  201. ** Returns void.
  202. ******************************************************************************/
  203. void A2D_ParsSbcMplHdr(UINT8 *p_src, BOOLEAN *p_frag, BOOLEAN *p_start, BOOLEAN *p_last, UINT8 *p_num)
  204. {
  205. if (p_src && p_frag && p_start && p_last && p_num) {
  206. *p_frag = (*p_src & A2D_SBC_HDR_F_MSK) ? TRUE : FALSE;
  207. *p_start = (*p_src & A2D_SBC_HDR_S_MSK) ? TRUE : FALSE;
  208. *p_last = (*p_src & A2D_SBC_HDR_L_MSK) ? TRUE : FALSE;
  209. *p_num = (*p_src & A2D_SBC_HDR_NUM_MSK);
  210. }
  211. }
  212. #endif /* #if (defined(A2D_INCLUDED) && A2D_INCLUDED == TRUE) */