1 /******************************************************************************
3 * Copyright (C) 2002-2012 Broadcom Corporation
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 ******************************************************************************/
19 /******************************************************************************
21 * Utility functions to help build and parse SBC Codec Information Element
24 ******************************************************************************/
26 #include "bt_target.h"
33 #if (A2D_SBC_INCLUDED == TRUE)
35 /*************************************************************************************************
37 * Purpose: to tie the SBC code with BTE/mobile stack code,
38 * especially for the case when the SBC is ported into a third-party Multimedia chip
41 * init process: all counters reset to 0,
42 * calculate base_index: (6 + s16NumOfChannels*s16NumOfSubBands/2)
43 * scramble side: the init process happens every time SBC_Encoder_Init() is called.
44 * descramble side: it would be nice to know if he "init" process has happened.
45 * alter the SBC SYNC word 0x9C (1001 1100) to 0x8C (1000 1100).
49 * Every SBC frame has a frame header.
50 * The 1st byte is the sync word and the following 2 bytes are about the stream format.
51 * They are supposed to be "constant" within a "song"
52 * The 4th byte is the CRC byte. The CRC byte is bound to be random.
53 * Derive 2 items from the CRC byte; one is the "use" bit, the other is the "index".
55 * SBC keeps 2 sets of "use" & "index"; derived the current and the previous frame.
57 * The "use" bit is any bit in SBC_PRTC_USE_MASK is set.
58 * If set, SBC uses the "index" from the current frame.
59 * If not set, SBC uses the "index" from the previous frame or 0.
61 * index = (CRC & 0x3) + ((CRC & 0x30) >> 2) // 8 is the max index
65 * p = &u8frame[base_index];
66 * if((index&1)&&(u16PacketLength > (base_index+index*2)))
68 * // odd index: swap 2 bytes
70 * p[index] = p[index*2];
75 * // even index: shift by 3
76 * tmp = (p[index] >> 3) + (p[index] << 5);
80 * //else index is 0. The frame stays unaltered
83 #define A2D_SBC_SYNC_WORD 0x9C
84 #define A2D_SBC_CRC_IDX 3
85 #define A2D_SBC_USE_MASK 0x64
86 #define A2D_SBC_SYNC_MASK 0x10
87 #define A2D_SBC_CIDX 0
88 #define A2D_SBC_LIDX 1
89 #define A2D_SBC_CH_M_BITS 0xC /* channel mode bits: 0: mono; 1 ch */
90 #define A2D_SBC_SUBBAND_BIT 0x1 /* num of subband bit: 0:4; 1: 8 */
92 #define A2D_SBC_GET_IDX(sc) (((sc) & 0x3) + (((sc) & 0x30) >> 2))
102 tA2D_SBC_FR_CB fr[2];
107 static tA2D_SBC_DS_CB a2d_sbc_ds_cb;
108 /*int a2d_count = 0;*/
109 /******************************************************************************
111 ** Function A2D_SbcChkFrInit
113 ** Description check if need to init the descramble control block.
116 ******************************************************************************/
117 void A2D_SbcChkFrInit(UINT8 *p_pkt)
121 UINT8 num_subband = 4;
123 if((p_pkt[0] & A2D_SBC_SYNC_MASK) == 0)
125 a2d_cb.use_desc = TRUE;
127 p_pkt[0] |= A2D_SBC_SYNC_MASK;
128 memset(&a2d_sbc_ds_cb, 0, sizeof(tA2D_SBC_DS_CB));
129 if(fmt & A2D_SBC_CH_M_BITS)
131 if(fmt & A2D_SBC_SUBBAND_BIT)
133 a2d_sbc_ds_cb.base = 6 + num_chnl*num_subband/2;
134 /*printf("base: %d\n", a2d_sbc_ds_cb.base);
139 /******************************************************************************
141 ** Function A2D_SbcDescramble
143 ** Description descramble the packet.
146 ******************************************************************************/
147 void A2D_SbcDescramble(UINT8 *p_pkt, UINT16 len)
149 tA2D_SBC_FR_CB *p_cur, *p_last;
150 UINT32 idx, tmp, tmp2;
155 p_last = &a2d_sbc_ds_cb.fr[A2D_SBC_LIDX];
156 p_cur = &a2d_sbc_ds_cb.fr[A2D_SBC_CIDX];
157 p_last->idx = p_cur->idx;
158 p_last->use = p_cur->use;
160 p_cur->use = p_pkt[A2D_SBC_CRC_IDX] & A2D_SBC_USE_MASK;
161 p_cur->idx = A2D_SBC_GET_IDX(p_pkt[A2D_SBC_CRC_IDX]);
162 a2d_sbc_ds_cb.index = (p_cur->use)?A2D_SBC_CIDX:A2D_SBC_LIDX;
164 printf("%05d: ar[%02d]: x%02x, msk: x%02x, use: %s, idx: %02d, ",
166 A2D_SBC_CRC_IDX, p_pkt[A2D_SBC_CRC_IDX], A2D_SBC_USE_MASK,
167 (p_cur->use)?"cur":"lst", p_cur->idx);
170 idx = a2d_sbc_ds_cb.fr[a2d_sbc_ds_cb.index].idx;
173 p_pkt = &p_pkt[a2d_sbc_ds_cb.base];
174 if((idx&1) && (len > (a2d_sbc_ds_cb.base+(idx<<1))))
178 p_pkt[idx] = p_pkt[tmp2];
181 printf("tmp2: %02d, len: %d, idx: %d\n",
182 tmp2, len, a2d_sbc_ds_cb.fr[a2d_sbc_ds_cb.index].idx);
188 tmp = (tmp2>>3)+(tmp2<<5);
189 p_pkt[idx] = (UINT8)tmp;
191 printf("tmp: x%02x, len: %d, idx: %d(cmp:%d)\n",
192 (UINT8)tmp2, len, a2d_sbc_ds_cb.fr[a2d_sbc_ds_cb.index].idx,
193 (a2d_sbc_ds_cb.base+(idx<<1)));
206 /******************************************************************************
208 ** Function A2D_BldSbcInfo
210 ** Description This function is called by an application to build
211 ** the SBC Media Codec Capabilities byte sequence
212 ** beginning from the LOSC octet.
214 ** media_type: Indicates Audio, or Multimedia.
216 ** p_ie: The SBC Codec Information Element information.
218 ** Output Parameters:
219 ** p_result: the resulting codec info byte sequence.
221 ** Returns A2D_SUCCESS if function execution succeeded.
222 ** Error status code, otherwise.
223 ******************************************************************************/
224 tA2D_STATUS A2D_BldSbcInfo(UINT8 media_type, tA2D_SBC_CIE *p_ie, UINT8 *p_result)
228 if( p_ie == NULL || p_result == NULL ||
229 (p_ie->samp_freq & ~A2D_SBC_IE_SAMP_FREQ_MSK) ||
230 (p_ie->ch_mode & ~A2D_SBC_IE_CH_MD_MSK) ||
231 (p_ie->block_len & ~A2D_SBC_IE_BLOCKS_MSK) ||
232 (p_ie->num_subbands & ~A2D_SBC_IE_SUBBAND_MSK) ||
233 (p_ie->alloc_mthd & ~A2D_SBC_IE_ALLOC_MD_MSK) ||
234 (p_ie->max_bitpool < p_ie->min_bitpool) ||
235 (p_ie->max_bitpool < A2D_SBC_IE_MIN_BITPOOL) ||
236 (p_ie->max_bitpool > A2D_SBC_IE_MAX_BITPOOL) ||
237 (p_ie->min_bitpool < A2D_SBC_IE_MIN_BITPOOL) ||
238 (p_ie->min_bitpool > A2D_SBC_IE_MAX_BITPOOL) )
240 /* if any unused bit is set */
241 status = A2D_INVALID_PARAMS;
245 status = A2D_SUCCESS;
246 *p_result++ = A2D_SBC_INFO_LEN;
247 *p_result++ = media_type;
248 *p_result++ = A2D_MEDIA_CT_SBC;
250 /* Media Codec Specific Information Element */
251 *p_result++ = p_ie->samp_freq | p_ie->ch_mode;
253 *p_result++ = p_ie->block_len | p_ie->num_subbands | p_ie->alloc_mthd;
255 *p_result++ = p_ie->min_bitpool;
256 *p_result = p_ie->max_bitpool;
261 /******************************************************************************
263 ** Function A2D_ParsSbcInfo
265 ** Description This function is called by an application to parse
266 ** the SBC Media Codec Capabilities byte sequence
267 ** beginning from the LOSC octet.
269 ** p_info: the byte sequence to parse.
271 ** for_caps: TRUE, if the byte sequence is for get capabilities response.
273 ** Output Parameters:
274 ** p_ie: The SBC Codec Information Element information.
276 ** Returns A2D_SUCCESS if function execution succeeded.
277 ** Error status code, otherwise.
278 ******************************************************************************/
279 tA2D_STATUS A2D_ParsSbcInfo(tA2D_SBC_CIE *p_ie, UINT8 *p_info, BOOLEAN for_caps)
285 if( p_ie == NULL || p_info == NULL)
286 status = A2D_INVALID_PARAMS;
291 /* If the function is called for the wrong Media Type or Media Codec Type */
292 if(losc != A2D_SBC_INFO_LEN || *p_info != A2D_MEDIA_CT_SBC)
293 status = A2D_WRONG_CODEC;
297 p_ie->samp_freq = *p_info & A2D_SBC_IE_SAMP_FREQ_MSK;
298 p_ie->ch_mode = *p_info & A2D_SBC_IE_CH_MD_MSK;
300 p_ie->block_len = *p_info & A2D_SBC_IE_BLOCKS_MSK;
301 p_ie->num_subbands = *p_info & A2D_SBC_IE_SUBBAND_MSK;
302 p_ie->alloc_mthd = *p_info & A2D_SBC_IE_ALLOC_MD_MSK;
304 p_ie->min_bitpool = *p_info++;
305 p_ie->max_bitpool = *p_info;
306 status = A2D_SUCCESS;
307 if(p_ie->min_bitpool < A2D_SBC_IE_MIN_BITPOOL || p_ie->min_bitpool > A2D_SBC_IE_MAX_BITPOOL )
308 status = A2D_BAD_MIN_BITPOOL;
310 if(p_ie->max_bitpool < A2D_SBC_IE_MIN_BITPOOL || p_ie->max_bitpool > A2D_SBC_IE_MAX_BITPOOL ||
311 p_ie->max_bitpool < p_ie->min_bitpool)
312 status = A2D_BAD_MAX_BITPOOL;
314 if(for_caps == FALSE)
316 if(A2D_BitsSet(p_ie->samp_freq) != A2D_SET_ONE_BIT)
317 status = A2D_BAD_SAMP_FREQ;
318 if(A2D_BitsSet(p_ie->ch_mode) != A2D_SET_ONE_BIT)
319 status = A2D_BAD_CH_MODE;
320 if(A2D_BitsSet(p_ie->block_len) != A2D_SET_ONE_BIT)
321 status = A2D_BAD_BLOCK_LEN;
322 if(A2D_BitsSet(p_ie->num_subbands) != A2D_SET_ONE_BIT)
323 status = A2D_BAD_SUBBANDS;
324 if(A2D_BitsSet(p_ie->alloc_mthd) != A2D_SET_ONE_BIT)
325 status = A2D_BAD_ALLOC_MTHD;
332 /******************************************************************************
334 ** Function A2D_BldSbcMplHdr
336 ** Description This function is called by an application to parse
337 ** the SBC Media Payload header.
339 ** frag: 1, if fragmented. 0, otherwise.
341 ** start: 1, if the starting packet of a fragmented frame.
343 ** last: 1, if the last packet of a fragmented frame.
345 ** num: If frag is 1, this is the number of remaining fragments
346 ** (including this fragment) of this frame.
347 ** If frag is 0, this is the number of frames in this packet.
349 ** Output Parameters:
350 ** p_dst: the resulting media payload header byte sequence.
353 ******************************************************************************/
354 void A2D_BldSbcMplHdr(UINT8 *p_dst, BOOLEAN frag, BOOLEAN start, BOOLEAN last, UINT8 num)
360 *p_dst |= A2D_SBC_HDR_F_MSK;
362 *p_dst |= A2D_SBC_HDR_S_MSK;
364 *p_dst |= A2D_SBC_HDR_L_MSK;
365 *p_dst |= (A2D_SBC_HDR_NUM_MSK & num);
369 /******************************************************************************
371 ** Function A2D_ParsSbcMplHdr
373 ** Description This function is called by an application to parse
374 ** the SBC Media Payload header.
376 ** p_src: the byte sequence to parse..
378 ** Output Parameters:
379 ** frag: 1, if fragmented. 0, otherwise.
381 ** start: 1, if the starting packet of a fragmented frame.
383 ** last: 1, if the last packet of a fragmented frame.
385 ** num: If frag is 1, this is the number of remaining fragments
386 ** (including this fragment) of this frame.
387 ** If frag is 0, this is the number of frames in this packet.
390 ******************************************************************************/
391 void A2D_ParsSbcMplHdr(UINT8 *p_src, BOOLEAN *p_frag, BOOLEAN *p_start, BOOLEAN *p_last, UINT8 *p_num)
393 if(p_src && p_frag && p_start && p_last && p_num)
395 *p_frag = (*p_src & A2D_SBC_HDR_F_MSK) ? TRUE: FALSE;
396 *p_start= (*p_src & A2D_SBC_HDR_S_MSK) ? TRUE: FALSE;
397 *p_last = (*p_src & A2D_SBC_HDR_L_MSK) ? TRUE: FALSE;
398 *p_num = (*p_src & A2D_SBC_HDR_NUM_MSK);
402 #else /* A2D_SBC_INCLUDED == TRUE */
404 void A2D_SbcChkFrInit(UINT8 *p_pkt)
409 void A2D_SbcDescramble(UINT8 *p_pkt, UINT16 len)
415 tA2D_STATUS A2D_BldSbcInfo(UINT8 media_type, tA2D_SBC_CIE *p_ie,
424 tA2D_STATUS A2D_ParsSbcInfo(tA2D_SBC_CIE *p_ie, UINT8 *p_info,
433 void A2D_BldSbcMplHdr(UINT8 *p_dst, BOOLEAN frag, BOOLEAN start,
434 BOOLEAN last, UINT8 num)
443 void A2D_ParsSbcMplHdr(UINT8 *p_src, BOOLEAN *p_frag,
444 BOOLEAN *p_start, BOOLEAN *p_last,
455 #endif /* A2D_SBC_INCLUDED == TRUE */