sbc_encoder.c 16 KB

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