sbc_encoder.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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 "common/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. #if (SBC_JOINT_STE_INCLUDED == TRUE)
  30. SINT32 s32LRDiff[SBC_MAX_NUM_OF_BLOCKS] = {0};
  31. SINT32 s32LRSum[SBC_MAX_NUM_OF_BLOCKS] = {0};
  32. #endif
  33. void SBC_Encoder(SBC_ENC_PARAMS *pstrEncParams)
  34. {
  35. SINT32 s32Ch; /* counter for ch*/
  36. SINT32 s32Sb; /* counter for sub-band*/
  37. UINT32 u32Count, maxBit = 0; /* loop count*/
  38. SINT32 s32MaxValue; /* temp variable to store max value */
  39. SINT16 *ps16ScfL;
  40. SINT32 *SbBuffer;
  41. SINT32 s32Blk; /* counter for block*/
  42. SINT32 s32NumOfBlocks = pstrEncParams->s16NumOfBlocks;
  43. #if (SBC_JOINT_STE_INCLUDED == TRUE)
  44. SINT32 s32MaxValue2;
  45. UINT32 u32CountSum, u32CountDiff;
  46. SINT32 *pSum, *pDiff;
  47. #endif
  48. register SINT32 s32NumOfSubBands = pstrEncParams->s16NumOfSubBands;
  49. pstrEncParams->pu8NextPacket = pstrEncParams->pu8Packet;
  50. #if (SBC_NO_PCM_CPY_OPTION == TRUE)
  51. pstrEncParams->ps16NextPcmBuffer = pstrEncParams->ps16PcmBuffer;
  52. #else
  53. pstrEncParams->ps16NextPcmBuffer = pstrEncParams->as16PcmBuffer;
  54. #endif
  55. do {
  56. /* SBC ananlysis filter*/
  57. if (s32NumOfSubBands == 4) {
  58. SbcAnalysisFilter4(pstrEncParams);
  59. } else {
  60. SbcAnalysisFilter8(pstrEncParams);
  61. }
  62. /* compute the scale factor, and save the max */
  63. ps16ScfL = pstrEncParams->as16ScaleFactor;
  64. s32Ch = pstrEncParams->s16NumOfChannels * s32NumOfSubBands;
  65. pstrEncParams->ps16NextPcmBuffer += s32Ch * s32NumOfBlocks; /* in case of multible sbc frame to encode update the pcm pointer */
  66. for (s32Sb = 0; s32Sb < s32Ch; s32Sb++) {
  67. SbBuffer = pstrEncParams->s32SbBuffer + s32Sb;
  68. s32MaxValue = 0;
  69. for (s32Blk = s32NumOfBlocks; s32Blk > 0; s32Blk--) {
  70. if (s32MaxValue < abs32(*SbBuffer)) {
  71. s32MaxValue = abs32(*SbBuffer);
  72. }
  73. SbBuffer += s32Ch;
  74. }
  75. u32Count = (s32MaxValue > 0x800000) ? 9 : 0;
  76. for ( ; u32Count < 15; u32Count++) {
  77. if (s32MaxValue <= (SINT32)(0x8000 << u32Count)) {
  78. break;
  79. }
  80. }
  81. *ps16ScfL++ = (SINT16)u32Count;
  82. if (u32Count > maxBit) {
  83. maxBit = u32Count;
  84. }
  85. }
  86. /* In case of JS processing,check whether to use JS */
  87. #if (SBC_JOINT_STE_INCLUDED == TRUE)
  88. if (pstrEncParams->s16ChannelMode == SBC_JOINT_STEREO) {
  89. /* Calculate sum and differance scale factors for making JS decision */
  90. ps16ScfL = pstrEncParams->as16ScaleFactor ;
  91. /* calculate the scale factor of Joint stereo max sum and diff */
  92. for (s32Sb = 0; s32Sb < s32NumOfSubBands - 1; s32Sb++) {
  93. SbBuffer = pstrEncParams->s32SbBuffer + s32Sb;
  94. s32MaxValue2 = 0;
  95. s32MaxValue = 0;
  96. pSum = s32LRSum;
  97. pDiff = s32LRDiff;
  98. for (s32Blk = 0; s32Blk < s32NumOfBlocks; s32Blk++) {
  99. *pSum = (*SbBuffer + * (SbBuffer + s32NumOfSubBands)) >> 1;
  100. if (abs32(*pSum) > s32MaxValue) {
  101. s32MaxValue = abs32(*pSum);
  102. }
  103. pSum++;
  104. *pDiff = (*SbBuffer - * (SbBuffer + s32NumOfSubBands)) >> 1;
  105. if (abs32(*pDiff) > s32MaxValue2) {
  106. s32MaxValue2 = abs32(*pDiff);
  107. }
  108. pDiff++;
  109. SbBuffer += s32Ch;
  110. }
  111. u32Count = (s32MaxValue > 0x800000) ? 9 : 0;
  112. for ( ; u32Count < 15; u32Count++) {
  113. if (s32MaxValue <= (SINT32)(0x8000 << u32Count)) {
  114. break;
  115. }
  116. }
  117. u32CountSum = u32Count;
  118. u32Count = (s32MaxValue2 > 0x800000) ? 9 : 0;
  119. for ( ; u32Count < 15; u32Count++) {
  120. if (s32MaxValue2 <= (SINT32)(0x8000 << u32Count)) {
  121. break;
  122. }
  123. }
  124. u32CountDiff = u32Count;
  125. if ( (*ps16ScfL + * (ps16ScfL + s32NumOfSubBands)) > (SINT16)(u32CountSum + u32CountDiff) ) {
  126. if (u32CountSum > maxBit) {
  127. maxBit = u32CountSum;
  128. }
  129. if (u32CountDiff > maxBit) {
  130. maxBit = u32CountDiff;
  131. }
  132. *ps16ScfL = (SINT16)u32CountSum;
  133. *(ps16ScfL + s32NumOfSubBands) = (SINT16)u32CountDiff;
  134. SbBuffer = pstrEncParams->s32SbBuffer + s32Sb;
  135. pSum = s32LRSum;
  136. pDiff = s32LRDiff;
  137. for (s32Blk = 0; s32Blk < s32NumOfBlocks; s32Blk++) {
  138. *SbBuffer = *pSum;
  139. *(SbBuffer + s32NumOfSubBands) = *pDiff;
  140. SbBuffer += s32NumOfSubBands << 1;
  141. pSum++;
  142. pDiff++;
  143. }
  144. pstrEncParams->as16Join[s32Sb] = 1;
  145. } else {
  146. pstrEncParams->as16Join[s32Sb] = 0;
  147. }
  148. ps16ScfL++;
  149. }
  150. pstrEncParams->as16Join[s32Sb] = 0;
  151. }
  152. #endif
  153. pstrEncParams->s16MaxBitNeed = (SINT16)maxBit;
  154. /* bit allocation */
  155. if ((pstrEncParams->s16ChannelMode == SBC_STEREO) || (pstrEncParams->s16ChannelMode == SBC_JOINT_STEREO)) {
  156. sbc_enc_bit_alloc_ste(pstrEncParams);
  157. } else {
  158. sbc_enc_bit_alloc_mono(pstrEncParams);
  159. }
  160. /* Quantize the encoded audio */
  161. EncPacking(pstrEncParams);
  162. } while (--(pstrEncParams->u8NumPacketToEncode));
  163. pstrEncParams->u8NumPacketToEncode = 1; /* default is one for retrocompatibility purpose */
  164. }
  165. /****************************************************************************
  166. * InitSbcAnalysisFilt - Initalizes the input data to 0
  167. *
  168. * RETURNS : N/A
  169. */
  170. void SBC_Encoder_Init(SBC_ENC_PARAMS *pstrEncParams)
  171. {
  172. UINT16 s16SamplingFreq; /*temp variable to store smpling freq*/
  173. SINT16 s16Bitpool; /*to store bit pool value*/
  174. SINT16 s16BitRate; /*to store bitrate*/
  175. SINT16 s16FrameLen; /*to store frame length*/
  176. UINT16 HeaderParams;
  177. pstrEncParams->u8NumPacketToEncode = 1; /* default is one for retrocompatibility purpose */
  178. if (pstrEncParams->sbc_mode != SBC_MODE_MSBC) {
  179. /* Required number of channels */
  180. if (pstrEncParams->s16ChannelMode == SBC_MONO) {
  181. pstrEncParams->s16NumOfChannels = 1;
  182. } else {
  183. pstrEncParams->s16NumOfChannels = 2;
  184. }
  185. /* Bit pool calculation */
  186. if (pstrEncParams->s16SamplingFreq == SBC_sf16000) {
  187. s16SamplingFreq = 16000;
  188. } else if (pstrEncParams->s16SamplingFreq == SBC_sf32000) {
  189. s16SamplingFreq = 32000;
  190. } else if (pstrEncParams->s16SamplingFreq == SBC_sf44100) {
  191. s16SamplingFreq = 44100;
  192. } else {
  193. s16SamplingFreq = 48000;
  194. }
  195. if ( (pstrEncParams->s16ChannelMode == SBC_JOINT_STEREO)
  196. || (pstrEncParams->s16ChannelMode == SBC_STEREO) ) {
  197. s16Bitpool = (SINT16)( (pstrEncParams->u16BitRate *
  198. pstrEncParams->s16NumOfSubBands * 1000 / s16SamplingFreq)
  199. - ( (32 + (4 * pstrEncParams->s16NumOfSubBands *
  200. pstrEncParams->s16NumOfChannels)
  201. + ( (pstrEncParams->s16ChannelMode - 2) *
  202. pstrEncParams->s16NumOfSubBands ) )
  203. / pstrEncParams->s16NumOfBlocks) );
  204. s16FrameLen = 4 + (4 * pstrEncParams->s16NumOfSubBands *
  205. pstrEncParams->s16NumOfChannels) / 8
  206. + ( ((pstrEncParams->s16ChannelMode - 2) *
  207. pstrEncParams->s16NumOfSubBands)
  208. + (pstrEncParams->s16NumOfBlocks * s16Bitpool) ) / 8;
  209. s16BitRate = (8 * s16FrameLen * s16SamplingFreq)
  210. / (pstrEncParams->s16NumOfSubBands *
  211. pstrEncParams->s16NumOfBlocks * 1000);
  212. if (s16BitRate > pstrEncParams->u16BitRate) {
  213. s16Bitpool--;
  214. }
  215. if (pstrEncParams->s16NumOfSubBands == 8) {
  216. pstrEncParams->s16BitPool = (s16Bitpool > 255) ? 255 : s16Bitpool;
  217. } else {
  218. pstrEncParams->s16BitPool = (s16Bitpool > 128) ? 128 : s16Bitpool;
  219. }
  220. } else {
  221. s16Bitpool = (SINT16)( ((pstrEncParams->s16NumOfSubBands *
  222. pstrEncParams->u16BitRate * 1000)
  223. / (s16SamplingFreq * pstrEncParams->s16NumOfChannels))
  224. - ( ( (32 / pstrEncParams->s16NumOfChannels) +
  225. (4 * pstrEncParams->s16NumOfSubBands) )
  226. / pstrEncParams->s16NumOfBlocks ) );
  227. pstrEncParams->s16BitPool = (s16Bitpool >
  228. (16 * pstrEncParams->s16NumOfSubBands))
  229. ? (16 * pstrEncParams->s16NumOfSubBands) : s16Bitpool;
  230. }
  231. if (pstrEncParams->s16BitPool < 0) {
  232. pstrEncParams->s16BitPool = 0;
  233. }
  234. /* sampling freq */
  235. HeaderParams = ((pstrEncParams->s16SamplingFreq & 3) << 6);
  236. /* number of blocks*/
  237. HeaderParams |= (((pstrEncParams->s16NumOfBlocks - 4) & 12) << 2);
  238. /* channel mode: mono, dual...*/
  239. HeaderParams |= ((pstrEncParams->s16ChannelMode & 3) << 2);
  240. /* Loudness or SNR */
  241. HeaderParams |= ((pstrEncParams->s16AllocationMethod & 1) << 1);
  242. HeaderParams |= ((pstrEncParams->s16NumOfSubBands >> 3) & 1); /*4 or 8*/
  243. pstrEncParams->FrameHeader = HeaderParams;
  244. } else {
  245. // mSBC
  246. // Use mSBC encoding parameters to reset the control field
  247. /* Required number of channels: 1 */
  248. pstrEncParams->s16ChannelMode = SBC_MONO;
  249. pstrEncParams->s16NumOfChannels = 1;
  250. /* Required Sampling frequency : 16KHz */
  251. pstrEncParams->s16SamplingFreq = SBC_sf16000;
  252. /* Bit pool value: 26 */
  253. pstrEncParams->s16BitPool = 26;
  254. /* number of subbands: 8 */
  255. pstrEncParams->s16NumOfSubBands = 8;
  256. /* number of blocks: 15 */
  257. pstrEncParams->s16NumOfBlocks = 15;
  258. /* allocation method: loudness */
  259. pstrEncParams->s16AllocationMethod = SBC_LOUDNESS;
  260. /* set the header paramers, unused for mSBC */
  261. pstrEncParams->FrameHeader = 0;
  262. }
  263. if (pstrEncParams->s16NumOfSubBands == 4) {
  264. if (pstrEncParams->s16NumOfChannels == 1) {
  265. EncMaxShiftCounter = ((ENC_VX_BUFFER_SIZE - 4 * 10) >> 2) << 2;
  266. } else {
  267. EncMaxShiftCounter = ((ENC_VX_BUFFER_SIZE - 4 * 10 * 2) >> 3) << 2;
  268. }
  269. } else {
  270. if (pstrEncParams->s16NumOfChannels == 1) {
  271. EncMaxShiftCounter = ((ENC_VX_BUFFER_SIZE - 8 * 10) >> 3) << 3;
  272. } else {
  273. EncMaxShiftCounter = ((ENC_VX_BUFFER_SIZE - 8 * 10 * 2) >> 4) << 3;
  274. }
  275. }
  276. APPL_TRACE_EVENT("SBC_Encoder_Init : bitrate %d, bitpool %d",
  277. pstrEncParams->u16BitRate, pstrEncParams->s16BitPool);
  278. SbcAnalysisInit();
  279. }
  280. #endif /* #if (defined(SBC_ENC_INCLUDED) && SBC_ENC_INCLUDED == TRUE) */