sbc_packing.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. * This file contains code for packing the Encoded data into bit streams.
  21. *
  22. ******************************************************************************/
  23. #include "common/bt_target.h"
  24. #include "sbc_encoder.h"
  25. #include "sbc_enc_func_declare.h"
  26. #if (defined(SBC_ENC_INCLUDED) && SBC_ENC_INCLUDED == TRUE)
  27. #if (SBC_ARM_ASM_OPT==TRUE)
  28. #define Mult32(s32In1,s32In2,s32OutLow) \
  29. { \
  30. __asm \
  31. { \
  32. MUL s32OutLow,s32In1,s32In2; \
  33. } \
  34. }
  35. #define Mult64(s32In1, s32In2, s32OutLow, s32OutHi) \
  36. { \
  37. __asm \
  38. { \
  39. SMULL s32OutLow,s32OutHi,s32In1,s32In2 \
  40. } \
  41. }
  42. #else
  43. #define Mult32(s32In1,s32In2,s32OutLow) s32OutLow=(SINT32)s32In1*(SINT32)s32In2;
  44. #define Mult64(s32In1, s32In2, s32OutLow, s32OutHi) \
  45. { \
  46. s32OutLow = ((SINT32)(UINT16)s32In1 * (UINT16)s32In2); \
  47. s32TempVal2 = (SINT32)((s32In1 >> 16) * (UINT16)s32In2); \
  48. s32Carry = ( (((UINT32)(s32OutLow)>>16)&0xFFFF) + \
  49. + (s32TempVal2 & 0xFFFF) ) >> 16; \
  50. s32OutLow += (s32TempVal2 << 16); \
  51. s32OutHi = (s32TempVal2 >> 16) + s32Carry; \
  52. }
  53. #endif
  54. void EncPacking(SBC_ENC_PARAMS *pstrEncParams)
  55. {
  56. UINT8 *pu8PacketPtr; /* packet ptr*/
  57. UINT8 Temp;
  58. SINT32 s32Blk; /* counter for block*/
  59. SINT32 s32Ch; /* counter for channel*/
  60. SINT32 s32Sb; /* counter for sub-band*/
  61. SINT32 s32PresentBit; /* represents bit to be stored*/
  62. /*SINT32 s32LoopCountI; loop counter*/
  63. SINT32 s32LoopCountJ; /* loop counter*/
  64. UINT32 u32QuantizedSbValue, u32QuantizedSbValue0; /* temp variable to store quantized sb val*/
  65. SINT32 s32LoopCount; /* loop counter*/
  66. UINT8 u8XoredVal; /* to store XORed value in CRC calculation*/
  67. UINT8 u8CRC; /* to store CRC value*/
  68. SINT16 *ps16GenPtr;
  69. SINT32 s32NumOfBlocks;
  70. SINT32 s32NumOfSubBands = pstrEncParams->s16NumOfSubBands;
  71. SINT32 s32NumOfChannels = pstrEncParams->s16NumOfChannels;
  72. UINT32 u32SfRaisedToPow2; /*scale factor raised to power 2*/
  73. SINT16 *ps16ScfPtr;
  74. SINT32 *ps32SbPtr;
  75. UINT16 u16Levels; /*to store levels*/
  76. SINT32 s32Temp1; /*used in 64-bit multiplication*/
  77. SINT32 s32Low; /*used in 64-bit multiplication*/
  78. #if (SBC_IS_64_MULT_IN_QUANTIZER==TRUE)
  79. SINT32 s32Hi1, s32Low1, s32Carry, s32TempVal2, s32Hi, s32Temp2;
  80. #endif
  81. pu8PacketPtr = pstrEncParams->pu8NextPacket; /*Initialize the ptr*/
  82. if (pstrEncParams->sbc_mode != SBC_MODE_MSBC) {
  83. *pu8PacketPtr++ = (UINT8)SBC_SYNC_WORD_STD; /*Sync word*/
  84. *pu8PacketPtr++ = (UINT8)(pstrEncParams->FrameHeader);
  85. *pu8PacketPtr = (UINT8)(pstrEncParams->s16BitPool & 0x00FF);
  86. } else {
  87. *pu8PacketPtr++ = (UINT8)SBC_SYNC_WORD_MSBC; /*Sync word*/
  88. // two reserved bytes
  89. *pu8PacketPtr++ = 0;
  90. *pu8PacketPtr = 0;
  91. }
  92. pu8PacketPtr += 2; /*skip for CRC*/
  93. /*here it indicate if it is byte boundary or nibble boundary*/
  94. s32PresentBit = 8;
  95. Temp = 0;
  96. #if (SBC_JOINT_STE_INCLUDED == TRUE)
  97. if (pstrEncParams->s16ChannelMode == SBC_JOINT_STEREO) {
  98. /* pack join stero parameters */
  99. for (s32Sb = 0; s32Sb < s32NumOfSubBands; s32Sb++) {
  100. Temp <<= 1;
  101. Temp |= pstrEncParams->as16Join[s32Sb];
  102. }
  103. /* pack RFA */
  104. if (s32NumOfSubBands == SUB_BANDS_4) {
  105. s32PresentBit = 4;
  106. } else {
  107. *(pu8PacketPtr++) = Temp;
  108. Temp = 0;
  109. }
  110. }
  111. #endif
  112. /* Pack Scale factor */
  113. ps16GenPtr = pstrEncParams->as16ScaleFactor;
  114. s32Sb = s32NumOfChannels * s32NumOfSubBands;
  115. /*Temp=*pu8PacketPtr;*/
  116. for (s32Ch = s32Sb; s32Ch > 0; s32Ch--) {
  117. Temp <<= 4;
  118. Temp |= *ps16GenPtr++;
  119. if (s32PresentBit == 4) {
  120. s32PresentBit = 8;
  121. *(pu8PacketPtr++) = Temp;
  122. Temp = 0;
  123. } else {
  124. s32PresentBit = 4;
  125. }
  126. }
  127. /* Pack samples */
  128. ps32SbPtr = pstrEncParams->s32SbBuffer;
  129. /*Temp=*pu8PacketPtr;*/
  130. s32NumOfBlocks = pstrEncParams->s16NumOfBlocks;
  131. for (s32Blk = s32NumOfBlocks - 1; s32Blk >= 0; s32Blk--) {
  132. ps16GenPtr = pstrEncParams->as16Bits;
  133. ps16ScfPtr = pstrEncParams->as16ScaleFactor;
  134. for (s32Ch = s32Sb - 1; s32Ch >= 0; s32Ch--) {
  135. s32LoopCount = *ps16GenPtr++;
  136. if (s32LoopCount != 0) {
  137. #if (SBC_IS_64_MULT_IN_QUANTIZER==TRUE)
  138. /* finding level from reconstruction part of decoder */
  139. u32SfRaisedToPow2 = ((UINT32)1 << ((*ps16ScfPtr) + 1));
  140. u16Levels = (UINT16)(((UINT32)1 << s32LoopCount) - 1);
  141. /* quantizer */
  142. s32Temp1 = (*ps32SbPtr >> 2) + (u32SfRaisedToPow2 << 12);
  143. s32Temp2 = u16Levels;
  144. Mult64 (s32Temp1, s32Temp2, s32Low, s32Hi);
  145. s32Low1 = s32Low >> ((*ps16ScfPtr) + 2);
  146. s32Low1 &= ((UINT32)1 << (32 - ((*ps16ScfPtr) + 2))) - 1;
  147. s32Hi1 = s32Hi << (32 - ((*ps16ScfPtr) + 2));
  148. u32QuantizedSbValue0 = (UINT16)((s32Low1 | s32Hi1) >> 12);
  149. #else
  150. /* finding level from reconstruction part of decoder */
  151. u32SfRaisedToPow2 = ((UINT32)1 << *ps16ScfPtr);
  152. u16Levels = (UINT16)(((UINT32)1 << s32LoopCount) - 1);
  153. /* quantizer */
  154. s32Temp1 = (*ps32SbPtr >> 15) + u32SfRaisedToPow2;
  155. Mult32(s32Temp1, u16Levels, s32Low);
  156. s32Low >>= (*ps16ScfPtr + 1);
  157. u32QuantizedSbValue0 = (UINT16)s32Low;
  158. #endif
  159. /*store the number of bits required and the quantized s32Sb
  160. sample to ease the coding*/
  161. u32QuantizedSbValue = u32QuantizedSbValue0;
  162. if (s32PresentBit >= s32LoopCount) {
  163. Temp <<= s32LoopCount;
  164. Temp |= u32QuantizedSbValue;
  165. s32PresentBit -= s32LoopCount;
  166. } else {
  167. while (s32PresentBit < s32LoopCount) {
  168. s32LoopCount -= s32PresentBit;
  169. u32QuantizedSbValue >>= s32LoopCount;
  170. /*remove the unwanted msbs*/
  171. /*u32QuantizedSbValue <<= 16 - s32PresentBit;
  172. u32QuantizedSbValue >>= 16 - s32PresentBit;*/
  173. Temp <<= s32PresentBit;
  174. Temp |= u32QuantizedSbValue ;
  175. /*restore the original*/
  176. u32QuantizedSbValue = u32QuantizedSbValue0;
  177. *(pu8PacketPtr++) = Temp;
  178. Temp = 0;
  179. s32PresentBit = 8;
  180. }
  181. Temp <<= s32LoopCount;
  182. /* remove the unwanted msbs */
  183. /*u32QuantizedSbValue <<= 16 - s32LoopCount;
  184. u32QuantizedSbValue >>= 16 - s32LoopCount;*/
  185. Temp |= u32QuantizedSbValue;
  186. s32PresentBit -= s32LoopCount;
  187. }
  188. }
  189. ps16ScfPtr++;
  190. ps32SbPtr++;
  191. }
  192. }
  193. Temp <<= s32PresentBit;
  194. *pu8PacketPtr = Temp;
  195. pstrEncParams->u16PacketLength = pu8PacketPtr - pstrEncParams->pu8NextPacket + 1;
  196. /*find CRC*/
  197. pu8PacketPtr = pstrEncParams->pu8NextPacket + 1; /*Initialize the ptr*/
  198. u8CRC = 0x0F;
  199. s32LoopCount = s32Sb >> 1;
  200. /*
  201. The loops is run from the start of the packet till the scale factor
  202. parameters. In case of JS, 'join' parameter is included in the packet
  203. so that many more bytes are included in CRC calculation.
  204. */
  205. Temp = *pu8PacketPtr;
  206. for (s32Ch = 1; s32Ch < (s32LoopCount + 4); s32Ch++) {
  207. /* skip sync word and CRC bytes */
  208. if (s32Ch != 3) {
  209. for (s32LoopCountJ = 7; s32LoopCountJ >= 0; s32LoopCountJ--) {
  210. u8XoredVal = ((u8CRC >> 7) & 0x01) ^ ((Temp >> s32LoopCountJ) & 0x01);
  211. u8CRC <<= 1;
  212. u8CRC ^= (u8XoredVal * 0x1D);
  213. u8CRC &= 0xFF;
  214. }
  215. }
  216. Temp = *(++pu8PacketPtr);
  217. }
  218. if (pstrEncParams->s16ChannelMode == SBC_JOINT_STEREO) {
  219. for (s32LoopCountJ = 7; s32LoopCountJ >= (8 - s32NumOfSubBands); s32LoopCountJ--) {
  220. u8XoredVal = ((u8CRC >> 7) & 0x01) ^ ((Temp >> s32LoopCountJ) & 0x01);
  221. u8CRC <<= 1;
  222. u8CRC ^= (u8XoredVal * 0x1D);
  223. u8CRC &= 0xFF;
  224. }
  225. }
  226. /* CRC calculation ends here */
  227. /* store CRC in packet */
  228. pu8PacketPtr = pstrEncParams->pu8NextPacket; /*Initialize the ptr*/
  229. pu8PacketPtr += 3;
  230. *pu8PacketPtr = u8CRC;
  231. pstrEncParams->pu8NextPacket += pstrEncParams->u16PacketLength; /* move the pointer to the end in case there is more than one frame to encode */
  232. }
  233. #endif /* #if (defined(SBC_ENC_INCLUDED) && SBC_ENC_INCLUDED == TRUE) */