sbc_encoder.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /******************************************************************************
  2. *
  3. * Copyright (C) 1999-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. * contains code for encoder flow and initalization of encoder
  21. *
  22. ******************************************************************************/
  23. #include <string.h>
  24. #include "bt_target.h"
  25. #include "sbc_encoder.h"
  26. #include "sbc_enc_func_declare.h"
  27. #if (defined(SBC_ENC_INCLUDED) && SBC_ENC_INCLUDED == TRUE)
  28. SINT16 EncMaxShiftCounter;
  29. /*************************************************************************************************
  30. * SBC encoder scramble code
  31. * Purpose: to tie the SBC code with BTE/mobile stack code,
  32. * especially for the case when the SBC is ported into a third-party Multimedia chip
  33. *
  34. * Algorithm:
  35. * init process: all counters reset to 0,
  36. * calculate base_index: (6 + s16NumOfChannels*s16NumOfSubBands/2)
  37. * scramble side: the init process happens every time SBC_Encoder_Init() is called.
  38. * descramble side: it would be nice to know if he "init" process has happened.
  39. * alter the SBC SYNC word 0x9C (1001 1100) to 0x8C (1000 1100).
  40. *
  41. * scramble process:
  42. * The CRC byte:
  43. * Every SBC frame has a frame header.
  44. * The 1st byte is the sync word and the following 2 bytes are about the stream format.
  45. * They are supposed to be "constant" within a "song"
  46. * The 4th byte is the CRC byte. The CRC byte is bound to be random.
  47. * Derive 2 items from the CRC byte; one is the "use" bit, the other is the "index".
  48. *
  49. * SBC keeps 2 sets of "use" & "index"; derived the current and the previous frame.
  50. *
  51. * The "use" bit is any bit in SBC_PRTC_USE_MASK is set.
  52. * If set, SBC uses the "index" from the current frame.
  53. * If not set, SBC uses the "index" from the previous frame or 0.
  54. *
  55. * index = (CRC & 0x3) + ((CRC & 0x30) >> 2) // 8 is the max index
  56. *
  57. * if(index > 0)
  58. * {
  59. * p = &u8frame[base_index];
  60. * if((index&1)&&(u16PacketLength > (base_index+index*2)))
  61. * {
  62. * // odd index: swap 2 bytes
  63. * tmp = p[index];
  64. * p[index] = p[index*2];
  65. * p[index*2] = tmp;
  66. * }
  67. * else
  68. * {
  69. * // even index: shift by 3
  70. * tmp = (p[index] >> 5) + (p[index] << 3);
  71. * p[index] = tmp;
  72. * }
  73. * }
  74. * //else index is 0. The frame stays unaltered
  75. *
  76. */
  77. #define SBC_PRTC_CRC_IDX 3
  78. #define SBC_PRTC_USE_MASK 0x64
  79. #define SBC_PRTC_SYNC_MASK 0x10
  80. #define SBC_PRTC_CIDX 0
  81. #define SBC_PRTC_LIDX 1
  82. typedef struct {
  83. UINT8 use;
  84. UINT8 idx;
  85. } tSBC_FR_CB;
  86. typedef struct {
  87. tSBC_FR_CB fr[2];
  88. UINT8 init;
  89. UINT8 index;
  90. UINT8 base;
  91. } tSBC_PRTC_CB;
  92. tSBC_PRTC_CB sbc_prtc_cb;
  93. #define SBC_PRTC_IDX(sc) (((sc) & 0x3) + (((sc) & 0x30) >> 2))
  94. #define SBC_PRTC_CHK_INIT(ar) {if(sbc_prtc_cb.init == 0){sbc_prtc_cb.init=1; ar[0] &= ~SBC_PRTC_SYNC_MASK;}}
  95. #define SBC_PRTC_C2L() {p_last=&sbc_prtc_cb.fr[SBC_PRTC_LIDX]; p_cur=&sbc_prtc_cb.fr[SBC_PRTC_CIDX]; \
  96. p_last->idx = p_cur->idx; p_last->use = p_cur->use;}
  97. #define SBC_PRTC_GETC(ar) {p_cur->use = ar[SBC_PRTC_CRC_IDX] & SBC_PRTC_USE_MASK; \
  98. p_cur->idx = SBC_PRTC_IDX(ar[SBC_PRTC_CRC_IDX]);}
  99. #define SBC_PRTC_CHK_CRC(ar) {SBC_PRTC_C2L();SBC_PRTC_GETC(ar);sbc_prtc_cb.index = (p_cur->use)?SBC_PRTC_CIDX:SBC_PRTC_LIDX;}
  100. #define SBC_PRTC_SCRMB(ar) {idx = sbc_prtc_cb.fr[sbc_prtc_cb.index].idx; \
  101. if(idx > 0){if((idx&1)&&(pstrEncParams->u16PacketLength > (sbc_prtc_cb.base+(idx<<1)))) {tmp2=idx<<1; tmp=ar[idx];ar[idx]=ar[tmp2];ar[tmp2]=tmp;} \
  102. else{tmp2=ar[idx]; tmp=(tmp2>>5)+(tmp2<<3);ar[idx]=(UINT8)tmp;}}}
  103. #if (SBC_JOINT_STE_INCLUDED == TRUE)
  104. SINT32 s32LRDiff[SBC_MAX_NUM_OF_BLOCKS] = {0};
  105. SINT32 s32LRSum[SBC_MAX_NUM_OF_BLOCKS] = {0};
  106. #endif
  107. void SBC_Encoder(SBC_ENC_PARAMS *pstrEncParams)
  108. {
  109. SINT32 s32Ch; /* counter for ch*/
  110. SINT32 s32Sb; /* counter for sub-band*/
  111. UINT32 u32Count, maxBit = 0; /* loop count*/
  112. SINT32 s32MaxValue; /* temp variable to store max value */
  113. SINT16 *ps16ScfL;
  114. SINT32 *SbBuffer;
  115. SINT32 s32Blk; /* counter for block*/
  116. SINT32 s32NumOfBlocks = pstrEncParams->s16NumOfBlocks;
  117. #if (SBC_JOINT_STE_INCLUDED == TRUE)
  118. SINT32 s32MaxValue2;
  119. UINT32 u32CountSum, u32CountDiff;
  120. SINT32 *pSum, *pDiff;
  121. #endif
  122. UINT8 *pu8;
  123. tSBC_FR_CB *p_cur, *p_last;
  124. UINT32 idx, tmp, tmp2;
  125. register SINT32 s32NumOfSubBands = pstrEncParams->s16NumOfSubBands;
  126. pstrEncParams->pu8NextPacket = pstrEncParams->pu8Packet;
  127. #if (SBC_NO_PCM_CPY_OPTION == TRUE)
  128. pstrEncParams->ps16NextPcmBuffer = pstrEncParams->ps16PcmBuffer;
  129. #else
  130. pstrEncParams->ps16NextPcmBuffer = pstrEncParams->as16PcmBuffer;
  131. #endif
  132. do {
  133. /* SBC ananlysis filter*/
  134. if (s32NumOfSubBands == 4) {
  135. SbcAnalysisFilter4(pstrEncParams);
  136. } else {
  137. SbcAnalysisFilter8(pstrEncParams);
  138. }
  139. /* compute the scale factor, and save the max */
  140. ps16ScfL = pstrEncParams->as16ScaleFactor;
  141. s32Ch = pstrEncParams->s16NumOfChannels * s32NumOfSubBands;
  142. pstrEncParams->ps16NextPcmBuffer += s32Ch * s32NumOfBlocks; /* in case of multible sbc frame to encode update the pcm pointer */
  143. for (s32Sb = 0; s32Sb < s32Ch; s32Sb++) {
  144. SbBuffer = pstrEncParams->s32SbBuffer + s32Sb;
  145. s32MaxValue = 0;
  146. for (s32Blk = s32NumOfBlocks; s32Blk > 0; s32Blk--) {
  147. if (s32MaxValue < abs32(*SbBuffer)) {
  148. s32MaxValue = abs32(*SbBuffer);
  149. }
  150. SbBuffer += s32Ch;
  151. }
  152. u32Count = (s32MaxValue > 0x800000) ? 9 : 0;
  153. for ( ; u32Count < 15; u32Count++) {
  154. if (s32MaxValue <= (SINT32)(0x8000 << u32Count)) {
  155. break;
  156. }
  157. }
  158. *ps16ScfL++ = (SINT16)u32Count;
  159. if (u32Count > maxBit) {
  160. maxBit = u32Count;
  161. }
  162. }
  163. /* In case of JS processing,check whether to use JS */
  164. #if (SBC_JOINT_STE_INCLUDED == TRUE)
  165. if (pstrEncParams->s16ChannelMode == SBC_JOINT_STEREO) {
  166. /* Calculate sum and differance scale factors for making JS decision */
  167. ps16ScfL = pstrEncParams->as16ScaleFactor ;
  168. /* calculate the scale factor of Joint stereo max sum and diff */
  169. for (s32Sb = 0; s32Sb < s32NumOfSubBands - 1; s32Sb++) {
  170. SbBuffer = pstrEncParams->s32SbBuffer + s32Sb;
  171. s32MaxValue2 = 0;
  172. s32MaxValue = 0;
  173. pSum = s32LRSum;
  174. pDiff = s32LRDiff;
  175. for (s32Blk = 0; s32Blk < s32NumOfBlocks; s32Blk++) {
  176. *pSum = (*SbBuffer + * (SbBuffer + s32NumOfSubBands)) >> 1;
  177. if (abs32(*pSum) > s32MaxValue) {
  178. s32MaxValue = abs32(*pSum);
  179. }
  180. pSum++;
  181. *pDiff = (*SbBuffer - * (SbBuffer + s32NumOfSubBands)) >> 1;
  182. if (abs32(*pDiff) > s32MaxValue2) {
  183. s32MaxValue2 = abs32(*pDiff);
  184. }
  185. pDiff++;
  186. SbBuffer += s32Ch;
  187. }
  188. u32Count = (s32MaxValue > 0x800000) ? 9 : 0;
  189. for ( ; u32Count < 15; u32Count++) {
  190. if (s32MaxValue <= (SINT32)(0x8000 << u32Count)) {
  191. break;
  192. }
  193. }
  194. u32CountSum = u32Count;
  195. u32Count = (s32MaxValue2 > 0x800000) ? 9 : 0;
  196. for ( ; u32Count < 15; u32Count++) {
  197. if (s32MaxValue2 <= (SINT32)(0x8000 << u32Count)) {
  198. break;
  199. }
  200. }
  201. u32CountDiff = u32Count;
  202. if ( (*ps16ScfL + * (ps16ScfL + s32NumOfSubBands)) > (SINT16)(u32CountSum + u32CountDiff) ) {
  203. if (u32CountSum > maxBit) {
  204. maxBit = u32CountSum;
  205. }
  206. if (u32CountDiff > maxBit) {
  207. maxBit = u32CountDiff;
  208. }
  209. *ps16ScfL = (SINT16)u32CountSum;
  210. *(ps16ScfL + s32NumOfSubBands) = (SINT16)u32CountDiff;
  211. SbBuffer = pstrEncParams->s32SbBuffer + s32Sb;
  212. pSum = s32LRSum;
  213. pDiff = s32LRDiff;
  214. for (s32Blk = 0; s32Blk < s32NumOfBlocks; s32Blk++) {
  215. *SbBuffer = *pSum;
  216. *(SbBuffer + s32NumOfSubBands) = *pDiff;
  217. SbBuffer += s32NumOfSubBands << 1;
  218. pSum++;
  219. pDiff++;
  220. }
  221. pstrEncParams->as16Join[s32Sb] = 1;
  222. } else {
  223. pstrEncParams->as16Join[s32Sb] = 0;
  224. }
  225. ps16ScfL++;
  226. }
  227. pstrEncParams->as16Join[s32Sb] = 0;
  228. }
  229. #endif
  230. pstrEncParams->s16MaxBitNeed = (SINT16)maxBit;
  231. /* bit allocation */
  232. if ((pstrEncParams->s16ChannelMode == SBC_STEREO) || (pstrEncParams->s16ChannelMode == SBC_JOINT_STEREO)) {
  233. sbc_enc_bit_alloc_ste(pstrEncParams);
  234. } else {
  235. sbc_enc_bit_alloc_mono(pstrEncParams);
  236. }
  237. /* save the beginning of the frame. pu8NextPacket is modified in EncPacking() */
  238. pu8 = pstrEncParams->pu8NextPacket;
  239. /* Quantize the encoded audio */
  240. EncPacking(pstrEncParams);
  241. /* scramble the code */
  242. SBC_PRTC_CHK_INIT(pu8);
  243. SBC_PRTC_CHK_CRC(pu8);
  244. #if 0
  245. if (pstrEncParams->u16PacketLength > ((sbc_prtc_cb.fr[sbc_prtc_cb.index].idx * 2) + sbc_prtc_cb.base)) {
  246. printf("len: %d, idx: %d\n", pstrEncParams->u16PacketLength, sbc_prtc_cb.fr[sbc_prtc_cb.index].idx);
  247. } else {
  248. printf("len: %d, idx: %d!!!!\n", pstrEncParams->u16PacketLength, sbc_prtc_cb.fr[sbc_prtc_cb.index].idx);
  249. }
  250. #endif
  251. SBC_PRTC_SCRMB((&pu8[sbc_prtc_cb.base]));
  252. } while (--(pstrEncParams->u8NumPacketToEncode));
  253. pstrEncParams->u8NumPacketToEncode = 1; /* default is one for retrocompatibility purpose */
  254. }
  255. /****************************************************************************
  256. * InitSbcAnalysisFilt - Initalizes the input data to 0
  257. *
  258. * RETURNS : N/A
  259. */
  260. void SBC_Encoder_Init(SBC_ENC_PARAMS *pstrEncParams)
  261. {
  262. UINT16 s16SamplingFreq; /*temp variable to store smpling freq*/
  263. SINT16 s16Bitpool; /*to store bit pool value*/
  264. SINT16 s16BitRate; /*to store bitrate*/
  265. SINT16 s16FrameLen; /*to store frame length*/
  266. UINT16 HeaderParams;
  267. pstrEncParams->u8NumPacketToEncode = 1; /* default is one for retrocompatibility purpose */
  268. /* Required number of channels */
  269. if (pstrEncParams->s16ChannelMode == SBC_MONO) {
  270. pstrEncParams->s16NumOfChannels = 1;
  271. } else {
  272. pstrEncParams->s16NumOfChannels = 2;
  273. }
  274. /* Bit pool calculation */
  275. if (pstrEncParams->s16SamplingFreq == SBC_sf16000) {
  276. s16SamplingFreq = 16000;
  277. } else if (pstrEncParams->s16SamplingFreq == SBC_sf32000) {
  278. s16SamplingFreq = 32000;
  279. } else if (pstrEncParams->s16SamplingFreq == SBC_sf44100) {
  280. s16SamplingFreq = 44100;
  281. } else {
  282. s16SamplingFreq = 48000;
  283. }
  284. if ( (pstrEncParams->s16ChannelMode == SBC_JOINT_STEREO)
  285. || (pstrEncParams->s16ChannelMode == SBC_STEREO) ) {
  286. s16Bitpool = (SINT16)( (pstrEncParams->u16BitRate *
  287. pstrEncParams->s16NumOfSubBands * 1000 / s16SamplingFreq)
  288. - ( (32 + (4 * pstrEncParams->s16NumOfSubBands *
  289. pstrEncParams->s16NumOfChannels)
  290. + ( (pstrEncParams->s16ChannelMode - 2) *
  291. pstrEncParams->s16NumOfSubBands ) )
  292. / pstrEncParams->s16NumOfBlocks) );
  293. s16FrameLen = 4 + (4 * pstrEncParams->s16NumOfSubBands *
  294. pstrEncParams->s16NumOfChannels) / 8
  295. + ( ((pstrEncParams->s16ChannelMode - 2) *
  296. pstrEncParams->s16NumOfSubBands)
  297. + (pstrEncParams->s16NumOfBlocks * s16Bitpool) ) / 8;
  298. s16BitRate = (8 * s16FrameLen * s16SamplingFreq)
  299. / (pstrEncParams->s16NumOfSubBands *
  300. pstrEncParams->s16NumOfBlocks * 1000);
  301. if (s16BitRate > pstrEncParams->u16BitRate) {
  302. s16Bitpool--;
  303. }
  304. if (pstrEncParams->s16NumOfSubBands == 8) {
  305. pstrEncParams->s16BitPool = (s16Bitpool > 255) ? 255 : s16Bitpool;
  306. } else {
  307. pstrEncParams->s16BitPool = (s16Bitpool > 128) ? 128 : s16Bitpool;
  308. }
  309. } else {
  310. s16Bitpool = (SINT16)( ((pstrEncParams->s16NumOfSubBands *
  311. pstrEncParams->u16BitRate * 1000)
  312. / (s16SamplingFreq * pstrEncParams->s16NumOfChannels))
  313. - ( ( (32 / pstrEncParams->s16NumOfChannels) +
  314. (4 * pstrEncParams->s16NumOfSubBands) )
  315. / pstrEncParams->s16NumOfBlocks ) );
  316. pstrEncParams->s16BitPool = (s16Bitpool >
  317. (16 * pstrEncParams->s16NumOfSubBands))
  318. ? (16 * pstrEncParams->s16NumOfSubBands) : s16Bitpool;
  319. }
  320. if (pstrEncParams->s16BitPool < 0) {
  321. pstrEncParams->s16BitPool = 0;
  322. }
  323. /* sampling freq */
  324. HeaderParams = ((pstrEncParams->s16SamplingFreq & 3) << 6);
  325. /* number of blocks*/
  326. HeaderParams |= (((pstrEncParams->s16NumOfBlocks - 4) & 12) << 2);
  327. /* channel mode: mono, dual...*/
  328. HeaderParams |= ((pstrEncParams->s16ChannelMode & 3) << 2);
  329. /* Loudness or SNR */
  330. HeaderParams |= ((pstrEncParams->s16AllocationMethod & 1) << 1);
  331. HeaderParams |= ((pstrEncParams->s16NumOfSubBands >> 3) & 1); /*4 or 8*/
  332. pstrEncParams->FrameHeader = HeaderParams;
  333. if (pstrEncParams->s16NumOfSubBands == 4) {
  334. if (pstrEncParams->s16NumOfChannels == 1) {
  335. EncMaxShiftCounter = ((ENC_VX_BUFFER_SIZE - 4 * 10) >> 2) << 2;
  336. } else {
  337. EncMaxShiftCounter = ((ENC_VX_BUFFER_SIZE - 4 * 10 * 2) >> 3) << 2;
  338. }
  339. } else {
  340. if (pstrEncParams->s16NumOfChannels == 1) {
  341. EncMaxShiftCounter = ((ENC_VX_BUFFER_SIZE - 8 * 10) >> 3) << 3;
  342. } else {
  343. EncMaxShiftCounter = ((ENC_VX_BUFFER_SIZE - 8 * 10 * 2) >> 4) << 3;
  344. }
  345. }
  346. APPL_TRACE_EVENT("SBC_Encoder_Init : bitrate %d, bitpool %d",
  347. pstrEncParams->u16BitRate, pstrEncParams->s16BitPool);
  348. SbcAnalysisInit();
  349. memset(&sbc_prtc_cb, 0, sizeof(tSBC_PRTC_CB));
  350. sbc_prtc_cb.base = 6 + pstrEncParams->s16NumOfChannels * pstrEncParams->s16NumOfSubBands / 2;
  351. }
  352. #endif /* #if (defined(SBC_ENC_INCLUDED) && SBC_ENC_INCLUDED == TRUE) */