framing.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /******************************************************************************
  2. *
  3. * Copyright (C) 2014 The Android Open Source Project
  4. * Copyright 2003 - 2004 Open Interface North America, Inc. All rights reserved.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at:
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. ******************************************************************************/
  19. /**********************************************************************************
  20. $Revision: #1 $
  21. ***********************************************************************************/
  22. /**
  23. @file
  24. Checksum and header-related functions.
  25. @ingroup codec_internal
  26. */
  27. /**
  28. @addtogroup codec_internal
  29. @{
  30. */
  31. #include "common/bt_target.h"
  32. #include "oi_codec_sbc_private.h"
  33. #include "oi_assert.h"
  34. #if (defined(SBC_DEC_INCLUDED) && SBC_DEC_INCLUDED == TRUE)
  35. /* asdasd */
  36. #define USE_NIBBLEWISE_CRC
  37. /* #define PRINT_SAMPLES */
  38. /* #define PRINT_SCALEFACTORS */
  39. /* #define DEBUG_CRC */
  40. /*
  41. * CRC-8 table for X^8 + X^4 + X^3 + X^2 + 1; byte-wise lookup
  42. */
  43. #ifdef USE_WIDE_CRC
  44. /* Save space if a char is 16 bits, such as on the C54x */
  45. const OI_BYTE crc8_wide[128] = {
  46. 0x001d, 0x3a27, 0x7469, 0x4e53, 0xe8f5, 0xd2cf, 0x9c81, 0xa6bb, 0xcdd0, 0xf7ea, 0xb9a4, 0x839e, 0x2538, 0x1f02, 0x514c, 0x6b76, 0x879a, 0xbda0, 0xf3ee, 0xc9d4, 0x6f72, 0x5548, 0x1b06, 0x213c, 0x4a57, 0x706d, 0x3e23, 0x0419, 0xa2bf, 0x9885, 0xd6cb, 0xecf1, 0x130e, 0x2934, 0x677a, 0x5d40, 0xfbe6, 0xc1dc, 0x8f92, 0xb5a8, 0xdec3, 0xe4f9, 0xaab7, 0x908d, 0x362b, 0x0c11, 0x425f, 0x7865, 0x9489, 0xaeb3, 0xe0fd, 0xdac7, 0x7c61, 0x465b, 0x0815, 0x322f, 0x5944, 0x637e, 0x2d30, 0x170a, 0xb1ac, 0x8b96, 0xc5d8, 0xffe2, 0x263b, 0x1c01, 0x524f, 0x6875, 0xced3, 0xf4e9, 0xbaa7, 0x809d, 0xebf6, 0xd1cc, 0x9f82, 0xa5b8, 0x031e, 0x3924, 0x776a, 0x4d50, 0xa1bc, 0x9b86, 0xd5c8, 0xeff2, 0x4954, 0x736e, 0x3d20, 0x071a, 0x6c71, 0x564b, 0x1805, 0x223f, 0x8499, 0xbea3, 0xf0ed, 0xcad7, 0x3528, 0x0f12, 0x415c, 0x7b66, 0xddc0, 0xe7fa, 0xa9b4, 0x938e, 0xf8e5, 0xc2df, 0x8c91, 0xb6ab, 0x100d, 0x2a37, 0x6479, 0x5e43, 0xb2af, 0x8895, 0xc6db, 0xfce1, 0x5a47, 0x607d, 0x2e33, 0x1409, 0x7f62, 0x4558, 0x0b16, 0x312c, 0x978a, 0xadb0, 0xe3fe, 0xd9c4,
  47. };
  48. #elif defined(USE_NIBBLEWISE_CRC)
  49. const OI_BYTE crc8_narrow[16] = {
  50. 0x00, 0x1d, 0x3a, 0x27, 0x74, 0x69, 0x4e, 0x53, 0xe8, 0xf5, 0xd2, 0xcf, 0x9c, 0x81, 0xa6, 0xbb
  51. };
  52. #else
  53. const OI_BYTE crc8_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22, 0x3f, 0x84, 0x99, 0xbe, 0xa3, 0xf0, 0xed, 0xca, 0xd7, 0x35, 0x28, 0x0f, 0x12, 0x41, 0x5c, 0x7b, 0x66, 0xdd, 0xc0, 0xe7, 0xfa, 0xa9, 0xb4, 0x93, 0x8e, 0xf8, 0xe5, 0xc2, 0xdf, 0x8c, 0x91, 0xb6, 0xab, 0x10, 0x0d, 0x2a, 0x37, 0x64, 0x79, 0x5e, 0x43, 0xb2, 0xaf, 0x88, 0x95, 0xc6, 0xdb, 0xfc, 0xe1, 0x5a, 0x47, 0x60, 0x7d, 0x2e, 0x33, 0x14, 0x09, 0x7f, 0x62, 0x45, 0x58, 0x0b, 0x16, 0x31, 0x2c, 0x97, 0x8a, 0xad, 0xb0, 0xe3, 0xfe, 0xd9, 0xc4
  55. };
  56. #endif
  57. const OI_UINT32 dequant_long_scaled[17] = {
  58. 0,
  59. 0,
  60. 0x1ee9e116, /* bits=2 0.24151243 1/3 * (1/1.38019122262781) (0x00000008)*/
  61. 0x0d3fa99c, /* bits=3 0.10350533 1/7 * (1/1.38019122262781) (0x00000013)*/
  62. 0x062ec69e, /* bits=4 0.04830249 1/15 * (1/1.38019122262781) (0x00000029)*/
  63. 0x02fddbfa, /* bits=5 0.02337217 1/31 * (1/1.38019122262781) (0x00000055)*/
  64. 0x0178d9f5, /* bits=6 0.01150059 1/63 * (1/1.38019122262781) (0x000000ad)*/
  65. 0x00baf129, /* bits=7 0.00570502 1/127 * (1/1.38019122262781) (0x0000015e)*/
  66. 0x005d1abe, /* bits=8 0.00284132 1/255 * (1/1.38019122262781) (0x000002bf)*/
  67. 0x002e760d, /* bits=9 0.00141788 1/511 * (1/1.38019122262781) (0x00000582)*/
  68. 0x00173536, /* bits=10 0.00070825 1/1023 * (1/1.38019122262781) (0x00000b07)*/
  69. 0x000b9928, /* bits=11 0.00035395 1/2047 * (1/1.38019122262781) (0x00001612)*/
  70. 0x0005cc37, /* bits=12 0.00017693 1/4095 * (1/1.38019122262781) (0x00002c27)*/
  71. 0x0002e604, /* bits=13 0.00008846 1/8191 * (1/1.38019122262781) (0x00005852)*/
  72. 0x000172fc, /* bits=14 0.00004422 1/16383 * (1/1.38019122262781) (0x0000b0a7)*/
  73. 0x0000b97d, /* bits=15 0.00002211 1/32767 * (1/1.38019122262781) (0x00016150)*/
  74. 0x00005cbe, /* bits=16 0.00001106 1/65535 * (1/1.38019122262781) (0x0002c2a5)*/
  75. };
  76. const OI_UINT32 dequant_long_unscaled[17] = {
  77. 0,
  78. 0,
  79. 0x2aaaaaab, /* bits=2 0.33333333 1/3 (0x00000005)*/
  80. 0x12492492, /* bits=3 0.14285714 1/7 (0x0000000e)*/
  81. 0x08888889, /* bits=4 0.06666667 1/15 (0x0000001d)*/
  82. 0x04210842, /* bits=5 0.03225806 1/31 (0x0000003e)*/
  83. 0x02082082, /* bits=6 0.01587302 1/63 (0x0000007e)*/
  84. 0x01020408, /* bits=7 0.00787402 1/127 (0x000000fe)*/
  85. 0x00808081, /* bits=8 0.00392157 1/255 (0x000001fd)*/
  86. 0x00402010, /* bits=9 0.00195695 1/511 (0x000003fe)*/
  87. 0x00200802, /* bits=10 0.00097752 1/1023 (0x000007fe)*/
  88. 0x00100200, /* bits=11 0.00048852 1/2047 (0x00000ffe)*/
  89. 0x00080080, /* bits=12 0.00024420 1/4095 (0x00001ffe)*/
  90. 0x00040020, /* bits=13 0.00012209 1/8191 (0x00003ffe)*/
  91. 0x00020008, /* bits=14 0.00006104 1/16383 (0x00007ffe)*/
  92. 0x00010002, /* bits=15 0.00003052 1/32767 (0x0000fffe)*/
  93. 0x00008001, /* bits=16 0.00001526 1/65535 (0x0001fffc)*/
  94. };
  95. #if defined(OI_DEBUG) || defined(PRINT_SAMPLES) || defined(PRINT_SCALEFACTORS)
  96. #include <stdio.h>
  97. #endif
  98. #ifdef USE_WIDE_CRC
  99. static INLINE OI_CHAR crc_iterate(OI_UINT8 oldcrc, OI_UINT8 next)
  100. {
  101. OI_UINT crc;
  102. OI_UINT idx;
  103. idx = oldcrc ^ next;
  104. crc = crc8_wide[idx >> 1];
  105. if (idx % 2) {
  106. crc &= 0xff;
  107. } else {
  108. crc >>= 8;
  109. }
  110. return crc;
  111. }
  112. static INLINE OI_CHAR crc_iterate_top4(OI_UINT8 oldcrc, OI_UINT8 next)
  113. {
  114. OI_UINT crc;
  115. OI_UINT idx;
  116. idx = (oldcrc ^ next) >> 4;
  117. crc = crc8_wide[idx >> 1];
  118. if (idx % 2) {
  119. crc &= 0xff;
  120. } else {
  121. crc >>= 8;
  122. }
  123. return (oldcrc << 4) ^ crc;
  124. }
  125. #else // USE_WIDE_CRC
  126. static INLINE OI_UINT8 crc_iterate_top4(OI_UINT8 oldcrc, OI_UINT8 next)
  127. {
  128. return (oldcrc << 4) ^ crc8_narrow[(oldcrc ^ next) >> 4];
  129. }
  130. #ifdef USE_NIBBLEWISE_CRC
  131. static INLINE OI_UINT8 crc_iterate(OI_UINT8 crc, OI_UINT8 next)
  132. {
  133. crc = (crc << 4) ^ crc8_narrow[(crc ^ next) >> 4];
  134. crc = (crc << 4) ^ crc8_narrow[((crc >> 4)^next) & 0xf];
  135. return crc;
  136. }
  137. #else // USE_NIBBLEWISE_CRC
  138. static INLINE OI_UINT8 crc_iterate(OI_UINT8 crc, OI_UINT8 next)
  139. {
  140. return crc8_narrow[crc ^ next];
  141. }
  142. #endif // USE_NIBBLEWISE_CRC
  143. #endif // USE_WIDE_CRC
  144. PRIVATE OI_UINT8 OI_SBC_CalculateChecksum(OI_CODEC_SBC_FRAME_INFO *frame, OI_BYTE const *data)
  145. {
  146. OI_UINT i;
  147. OI_UINT8 crc = 0x0f;
  148. /* Count is the number of whole bytes subject to CRC. Actually, it's one
  149. * more than this number, because data[3] is the CRC field itself, which is
  150. * explicitly skipped. Since crc_iterate (should be) inlined, it's cheaper
  151. * spacewise to include the check in the loop. This shouldn't be much of a
  152. * bottleneck routine in the first place. */
  153. OI_UINT count = (frame->nrof_subbands * frame->nrof_channels / 2u) + 4;
  154. if (frame->mode == SBC_JOINT_STEREO && frame->nrof_subbands == 8) {
  155. count++;
  156. }
  157. for (i = 1; i < count; i++) {
  158. if (i != 3) {
  159. crc = crc_iterate(crc, data[i]);
  160. }
  161. }
  162. if (frame->mode == SBC_JOINT_STEREO && frame->nrof_subbands == 4) {
  163. crc = crc_iterate_top4(crc, data[i]);
  164. }
  165. return crc;
  166. }
  167. void OI_SBC_ExpandFrameFields(OI_CODEC_SBC_FRAME_INFO *frame)
  168. {
  169. frame->nrof_blocks = block_values[frame->blocks];
  170. frame->nrof_subbands = band_values[frame->subbands];
  171. frame->frequency = freq_values[frame->freqIndex];
  172. frame->nrof_channels = channel_values[frame->mode];
  173. }
  174. /**
  175. * Unrolled macro to copy 4 32-bit aligned 32-bit values backward in memory
  176. */
  177. #define COPY4WORDS_BACK(_dest, _src) \
  178. do { \
  179. OI_INT32 _a, _b, _c, _d; \
  180. _a = *--_src; \
  181. _b = *--_src; \
  182. _c = *--_src; \
  183. _d = *--_src; \
  184. *--_dest = _a; \
  185. *--_dest = _b; \
  186. *--_dest = _c; \
  187. *--_dest = _d; \
  188. } while (0)
  189. #if defined(USE_PLATFORM_MEMMOVE) || defined(USE_PLATFORM_MEMCPY)
  190. #include <string.h>
  191. #endif
  192. PRIVATE void shift_buffer(SBC_BUFFER_T *dest, SBC_BUFFER_T *src, OI_UINT wordCount)
  193. {
  194. #ifdef USE_PLATFORM_MEMMOVE
  195. memmove(dest, src, wordCount * sizeof(SBC_BUFFER_T));
  196. #elif defined(USE_PLATFORM_MEMCPY)
  197. OI_ASSERT(((OI_CHAR *)(dest) - (OI_CHAR *)(src)) >= wordCount * sizeof(*dest));
  198. memcpy(dest, src, wordCount * sizeof(SBC_BUFFER_T));
  199. #else
  200. OI_UINT n;
  201. OI_INT32 *d;
  202. OI_INT32 *s;
  203. n = wordCount / 4 / (sizeof(OI_INT32) / sizeof(*dest));
  204. OI_ASSERT((n * 4 * (sizeof(OI_INT32) / sizeof(*dest))) == wordCount);
  205. d = (void *)(dest + wordCount);
  206. s = (void *)(src + wordCount);
  207. do {
  208. COPY4WORDS_BACK(d, s);
  209. } while (--n);
  210. #endif
  211. }
  212. /**
  213. @}
  214. */
  215. #endif /* #if (defined(SBC_DEC_INCLUDED) && SBC_DEC_INCLUDED == TRUE) */