decoder-sbc.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. /******************************************************************************
  2. *
  3. * Copyright (C) 2014 The Android Open Source Project
  4. * Copyright 2006 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. /** @file
  23. @ingroup codec_internal
  24. */
  25. /**@addtogroup codec_internal */
  26. /**@{*/
  27. #include "oi_codec_sbc_private.h"
  28. #include "oi_bitstream.h"
  29. #define SPECIALIZE_READ_SAMPLES_JOINT
  30. /**
  31. * Scans through a buffer looking for a codec syncword. If the decoder has been
  32. * set for enhanced operation using OI_CODEC_SBC_DecoderReset(), it will search
  33. * for both a standard and an enhanced syncword.
  34. */
  35. PRIVATE OI_STATUS FindSyncword(OI_CODEC_SBC_DECODER_CONTEXT *context,
  36. const OI_BYTE **frameData,
  37. OI_UINT32 *frameBytes);
  38. PRIVATE OI_STATUS FindSyncword(OI_CODEC_SBC_DECODER_CONTEXT *context,
  39. const OI_BYTE **frameData,
  40. OI_UINT32 *frameBytes)
  41. {
  42. #ifdef SBC_ENHANCED
  43. OI_BYTE search1 = OI_SBC_SYNCWORD;
  44. OI_BYTE search2 = OI_SBC_ENHANCED_SYNCWORD;
  45. #endif // SBC_ENHANCED
  46. if (*frameBytes == 0) {
  47. return OI_CODEC_SBC_NOT_ENOUGH_HEADER_DATA;
  48. }
  49. #ifdef SBC_ENHANCED
  50. if (context->limitFrameFormat && context->enhancedEnabled){
  51. /* If the context is restricted, only search for specified SYNCWORD */
  52. search1 = search2;
  53. } else if (context->enhancedEnabled == FALSE) {
  54. /* If enhanced is not enabled, only search for classic SBC SYNCWORD*/
  55. search2 = search1;
  56. }
  57. while (*frameBytes && (**frameData != search1) && (**frameData != search2)) {
  58. (*frameBytes)--;
  59. (*frameData)++;
  60. }
  61. if (*frameBytes) {
  62. /* Syncword found, *frameData points to it, and *frameBytes correctly
  63. * reflects the number of bytes available to read, including the
  64. * syncword. */
  65. context->common.frameInfo.enhanced = (**frameData == OI_SBC_ENHANCED_SYNCWORD);
  66. return OI_OK;
  67. } else {
  68. /* No syncword was found anywhere in the provided input data.
  69. * *frameData points past the end of the original input, and
  70. * *frameBytes is 0. */
  71. return OI_CODEC_SBC_NO_SYNCWORD;
  72. }
  73. #else // SBC_ENHANCED
  74. /* BK4BTSTACK_CHANGE START */
  75. OI_UINT8 syncword = OI_SBC_SYNCWORD;
  76. if (context->common.frameInfo.mSBCEnabled){
  77. syncword = OI_mSBC_SYNCWORD;
  78. }
  79. //printf("search %x\n", syncword);
  80. /* BK4BTSTACK_CHANGE END */
  81. while (*frameBytes && (**frameData != syncword)) {
  82. // printf("%c ", **frameData);
  83. (*frameBytes)--;
  84. (*frameData)++;
  85. }
  86. //printf("\n\n");
  87. if (*frameBytes) {
  88. /* Syncword found, *frameData points to it, and *frameBytes correctly
  89. * reflects the number of bytes available to read, including the
  90. * syncword. */
  91. context->common.frameInfo.enhanced = FALSE;
  92. return OI_OK;
  93. } else {
  94. /* No syncword was found anywhere in the provided input data.
  95. * *frameData points past the end of the original input, and
  96. * *frameBytes is 0. */
  97. return OI_CODEC_SBC_NO_SYNCWORD;
  98. }
  99. #endif // SBC_ENHANCED
  100. }
  101. static OI_STATUS DecodeBody(OI_CODEC_SBC_DECODER_CONTEXT *context,
  102. const OI_BYTE *bodyData,
  103. OI_INT16 *pcmData,
  104. OI_UINT32 *pcmBytes,
  105. OI_BOOL allowPartial)
  106. {
  107. OI_BITSTREAM bs;
  108. OI_UINT frameSamples = context->common.frameInfo.nrof_blocks * context->common.frameInfo.nrof_subbands;
  109. OI_UINT decode_block_count;
  110. /*
  111. * Based on the header data, make sure that there is enough room to write the output samples.
  112. */
  113. if ((*pcmBytes < (sizeof(OI_INT16) * frameSamples * context->common.pcmStride)) && !allowPartial) {
  114. /* If we're not allowing partial decodes, we need room for the entire
  115. * codec frame */
  116. TRACE(("-OI_CODEC_SBC_Decode: OI_CODEC_SBC_NOT_ENOUGH_AUDIO_DATA"));
  117. return OI_CODEC_SBC_NOT_ENOUGH_AUDIO_DATA;
  118. } else if (*pcmBytes < (sizeof (OI_INT16) * context->common.frameInfo.nrof_subbands * context->common.pcmStride)) {
  119. /* Even if we're allowing partials, we can still only decode on a frame
  120. * boundary */
  121. return OI_CODEC_SBC_NOT_ENOUGH_AUDIO_DATA;
  122. }
  123. if (context->bufferedBlocks == 0) {
  124. TRACE(("Reading scalefactors"));
  125. OI_SBC_ReadScalefactors(&context->common, bodyData, &bs);
  126. TRACE(("Computing bit allocation"));
  127. OI_SBC_ComputeBitAllocation(&context->common);
  128. TRACE(("Reading samples"));
  129. if (context->common.frameInfo.mode == SBC_JOINT_STEREO) {
  130. OI_SBC_ReadSamplesJoint(context, &bs);
  131. } else {
  132. OI_SBC_ReadSamples(context, &bs);
  133. }
  134. context->bufferedBlocks = context->common.frameInfo.nrof_blocks;
  135. }
  136. if (allowPartial) {
  137. decode_block_count = *pcmBytes / sizeof(OI_INT16) / context->common.pcmStride / context->common.frameInfo.nrof_subbands;
  138. if (decode_block_count > context->bufferedBlocks) {
  139. decode_block_count = context->bufferedBlocks;
  140. }
  141. } else {
  142. decode_block_count = context->common.frameInfo.nrof_blocks;
  143. }
  144. TRACE(("Synthesizing frame"));
  145. {
  146. OI_UINT start_block = context->common.frameInfo.nrof_blocks - context->bufferedBlocks;
  147. OI_SBC_SynthFrame(context, pcmData, start_block, decode_block_count);
  148. }
  149. OI_ASSERT(context->bufferedBlocks >= decode_block_count);
  150. context->bufferedBlocks -= decode_block_count;
  151. frameSamples = decode_block_count * context->common.frameInfo.nrof_subbands;
  152. /*
  153. * When decoding mono into a stride-2 array, copy pcm data to second channel
  154. */
  155. if ((context->common.frameInfo.nrof_channels == 1) && (context->common.pcmStride == 2)) {
  156. OI_UINT i;
  157. for (i = 0; i < frameSamples; ++i) {
  158. pcmData[(2*i)+1] = pcmData[2*i];
  159. }
  160. }
  161. /*
  162. * Return number of pcm bytes generated by the decode operation.
  163. */
  164. *pcmBytes = frameSamples * sizeof(OI_INT16) * context->common.pcmStride;
  165. if (context->bufferedBlocks > 0) {
  166. return OI_CODEC_SBC_PARTIAL_DECODE;
  167. } else {
  168. return OI_OK;
  169. }
  170. }
  171. PRIVATE OI_STATUS internal_DecodeRaw(OI_CODEC_SBC_DECODER_CONTEXT *context,
  172. OI_UINT8 bitpool,
  173. const OI_BYTE **frameData,
  174. OI_UINT32 *frameBytes,
  175. OI_INT16 *pcmData,
  176. OI_UINT32 *pcmBytes)
  177. {
  178. OI_STATUS status;
  179. OI_UINT bodyLen;
  180. TRACE(("+OI_CODEC_SBC_DecodeRaw"));
  181. if (context->bufferedBlocks == 0) {
  182. /*
  183. * The bitallocator needs to know the bitpool value.
  184. */
  185. context->common.frameInfo.bitpool = bitpool;
  186. /*
  187. * Compute the frame length and check we have enough frame data to proceed
  188. */
  189. bodyLen = OI_CODEC_SBC_CalculateFramelen(&context->common.frameInfo) - SBC_HEADER_LEN;
  190. if (*frameBytes < bodyLen) {
  191. TRACE(("-OI_CODEC_SBC_Decode: OI_CODEC_SBC_NOT_ENOUGH_BODY_DATA"));
  192. return OI_CODEC_SBC_NOT_ENOUGH_BODY_DATA;
  193. }
  194. } else {
  195. bodyLen = 0;
  196. }
  197. /*
  198. * Decode the SBC data. Pass TRUE to DecodeBody to allow partial decoding of
  199. * tones.
  200. */
  201. status = DecodeBody(context, *frameData, pcmData, pcmBytes, TRUE);
  202. if (OI_SUCCESS(status) || (status == OI_CODEC_SBC_PARTIAL_DECODE)) {
  203. *frameData += bodyLen;
  204. *frameBytes -= bodyLen;
  205. }
  206. TRACE(("-OI_CODEC_SBC_DecodeRaw: %d", status));
  207. return status;
  208. }
  209. OI_STATUS OI_CODEC_SBC_DecoderReset(OI_CODEC_SBC_DECODER_CONTEXT *context,
  210. OI_UINT32 *decoderData,
  211. OI_UINT32 decoderDataBytes,
  212. OI_UINT8 maxChannels,
  213. OI_UINT8 pcmStride,
  214. OI_BOOL enhanced)
  215. {
  216. return internal_DecoderReset(context, decoderData, decoderDataBytes, maxChannels, pcmStride, enhanced);
  217. }
  218. /* BK4BTSTACK_CHANGE START */
  219. OI_STATUS OI_CODEC_mSBC_DecoderReset(OI_CODEC_SBC_DECODER_CONTEXT *context,
  220. OI_UINT32 *decoderData,
  221. OI_UINT32 decoderDataBytes)
  222. {
  223. OI_STATUS status = OI_CODEC_SBC_DecoderReset(context, decoderData, decoderDataBytes, 1, 1, FALSE);
  224. context->common.frameInfo.mSBCEnabled = TRUE;
  225. return status;
  226. }
  227. /* BK4BTSTACK_CHANGE END */
  228. OI_STATUS OI_CODEC_SBC_DecodeFrame(OI_CODEC_SBC_DECODER_CONTEXT *context,
  229. const OI_BYTE **frameData,
  230. OI_UINT32 *frameBytes,
  231. OI_INT16 *pcmData,
  232. OI_UINT32 *pcmBytes)
  233. {
  234. OI_STATUS status;
  235. OI_UINT framelen;
  236. OI_UINT8 crc;
  237. TRACE(("+OI_CODEC_SBC_DecodeFrame"));
  238. TRACE(("Finding syncword"));
  239. //printf("OI_CODEC_SBC_DecodeFrame frameBytes %d\n", frameBytes);
  240. status = FindSyncword(context, frameData, frameBytes);
  241. if (!OI_SUCCESS(status)) {
  242. return status;
  243. }
  244. /* Make sure enough data remains to read the header. */
  245. if (*frameBytes < SBC_HEADER_LEN) {
  246. TRACE(("-OI_CODEC_SBC_DecodeFrame: OI_CODEC_SBC_NOT_ENOUGH_HEADER_DATA"));
  247. return OI_CODEC_SBC_NOT_ENOUGH_HEADER_DATA;
  248. }
  249. TRACE(("Reading Header"));
  250. if (context->common.frameInfo.mSBCEnabled){
  251. OI_SBC_ReadHeader_mSBC(&context->common, *frameData);
  252. } else {
  253. OI_SBC_ReadHeader(&context->common, *frameData);
  254. }
  255. /*
  256. * Some implementations load the decoder into RAM and use overlays for 4 vs 8 subbands. We need
  257. * to ensure that the SBC parameters for this frame are compatible with the restrictions imposed
  258. * by the loaded overlays.
  259. */
  260. if (context->limitFrameFormat && (context->common.frameInfo.subbands != context->restrictSubbands)) {
  261. ERROR(("SBC parameters incompatible with loaded overlay"));
  262. return OI_STATUS_INVALID_PARAMETERS;
  263. }
  264. TRACE(("Frame: "));
  265. if (context->common.frameInfo.nrof_channels > context->common.maxChannels) {
  266. ERROR(("SBC parameters incompatible with number of channels specified during reset"));
  267. return OI_STATUS_INVALID_PARAMETERS;
  268. }
  269. if ((context->common.pcmStride < 1) || (context->common.pcmStride > 2)) {
  270. ERROR(("PCM stride not set correctly during reset"));
  271. return OI_STATUS_INVALID_PARAMETERS;
  272. }
  273. /*
  274. * At this point a header has been read. However, it's possible that we found a false syncword,
  275. * so the header data might be invalid. Make sure we have enough bytes to read in the
  276. * CRC-protected header, but don't require we have the whole frame. That way, if it turns out
  277. * that we're acting on bogus header data, we don't stall the decoding process by waiting for
  278. * data that we don't actually need.
  279. */
  280. framelen = OI_CODEC_SBC_CalculateFramelen(&context->common.frameInfo);
  281. if (*frameBytes < framelen) {
  282. TRACE(("-OI_CODEC_SBC_DecodeFrame: OI_CODEC_SBC_NOT_ENOUGH_BODY_DATA"));
  283. return OI_CODEC_SBC_NOT_ENOUGH_BODY_DATA;
  284. }
  285. TRACE(("frame len %d\n", framelen));
  286. TRACE(("Calculating checksum"));
  287. if (context->common.frameInfo.mSBCEnabled){
  288. crc = OI_SBC_CalculateChecksum_mSBC(&context->common.frameInfo, *frameData);
  289. } else {
  290. crc = OI_SBC_CalculateChecksum(&context->common.frameInfo, *frameData);
  291. }
  292. if (crc != context->common.frameInfo.crc) {
  293. TRACE(("CRC Mismatch: calc=%02x read=%02x\n", crc, context->common.frameInfo.crc));
  294. TRACE(("-OI_CODEC_SBC_DecodeFrame: OI_CODEC_SBC_CHECKSUM_MISMATCH"));
  295. return OI_CODEC_SBC_CHECKSUM_MISMATCH;
  296. }
  297. #ifdef OI_DEBUG
  298. /*
  299. * Make sure the bitpool values are sane.
  300. */
  301. if ((context->common.frameInfo.bitpool < SBC_MIN_BITPOOL) && !context->common.frameInfo.enhanced) {
  302. ERROR(("Bitpool too small: %d (must be >= 2)", context->common.frameInfo.bitpool));
  303. return OI_STATUS_INVALID_PARAMETERS;
  304. }
  305. if (context->common.frameInfo.bitpool > OI_SBC_MaxBitpool(&context->common.frameInfo)) {
  306. ERROR(("Bitpool too large: %d (must be <= %ld)", context->common.frameInfo.bitpool, OI_SBC_MaxBitpool(&context->common.frameInfo)));
  307. return OI_STATUS_INVALID_PARAMETERS;
  308. }
  309. #endif
  310. /*
  311. * Now decode the SBC data. Partial decode is not yet implemented for an SBC
  312. * stream, so pass FALSE to decode body to have it enforce the old rule that
  313. * you have to decode a whole packet at a time.
  314. */
  315. status = DecodeBody(context, *frameData + SBC_HEADER_LEN, pcmData, pcmBytes, FALSE);
  316. if (OI_SUCCESS(status)) {
  317. *frameData += framelen;
  318. *frameBytes -= framelen;
  319. }
  320. TRACE(("-OI_CODEC_SBC_DecodeFrame: %d", status));
  321. return status;
  322. }
  323. OI_STATUS OI_CODEC_SBC_SkipFrame(OI_CODEC_SBC_DECODER_CONTEXT *context,
  324. const OI_BYTE **frameData,
  325. OI_UINT32 *frameBytes)
  326. {
  327. OI_STATUS status;
  328. OI_UINT framelen;
  329. OI_UINT headerlen;
  330. OI_UINT8 crc;
  331. status = FindSyncword(context, frameData, frameBytes);
  332. if (!OI_SUCCESS(status)) {
  333. return status;
  334. }
  335. if (*frameBytes < SBC_HEADER_LEN) {
  336. return OI_CODEC_SBC_NOT_ENOUGH_HEADER_DATA;
  337. }
  338. OI_SBC_ReadHeader(&context->common, *frameData);
  339. framelen = OI_SBC_CalculateFrameAndHeaderlen(&context->common.frameInfo, &headerlen);
  340. if (*frameBytes < headerlen) {
  341. return OI_CODEC_SBC_NOT_ENOUGH_HEADER_DATA;
  342. }
  343. crc = OI_SBC_CalculateChecksum(&context->common.frameInfo, *frameData);
  344. if (crc != context->common.frameInfo.crc) {
  345. return OI_CODEC_SBC_CHECKSUM_MISMATCH;
  346. }
  347. if (*frameBytes < framelen) {
  348. return OI_CODEC_SBC_NOT_ENOUGH_BODY_DATA;
  349. }
  350. context->bufferedBlocks = 0;
  351. *frameData += framelen;
  352. *frameBytes -= framelen;
  353. return OI_OK;
  354. }
  355. OI_UINT8 OI_CODEC_SBC_FrameCount(OI_BYTE *frameData,
  356. OI_UINT32 frameBytes)
  357. {
  358. OI_UINT8 mode;
  359. OI_UINT8 blocks;
  360. OI_UINT8 subbands;
  361. OI_UINT8 frameCount = 0;
  362. OI_UINT frameLen;
  363. while (frameBytes){
  364. while (frameBytes && ((frameData[0] & 0xFE) != 0x9C)){
  365. frameData++;
  366. frameBytes--;
  367. }
  368. if (frameBytes < SBC_HEADER_LEN) {
  369. return frameCount;
  370. }
  371. /* Extract and translate required fields from Header */
  372. subbands = mode = blocks = frameData[1];;
  373. mode = (mode & (BIT3 | BIT2)) >> 2;
  374. blocks = block_values[(blocks & (BIT5 | BIT4)) >> 4];
  375. subbands = band_values[(subbands & BIT0)];
  376. /* Inline logic to avoid corrupting context */
  377. frameLen = blocks * frameData[2];
  378. switch (mode){
  379. case SBC_JOINT_STEREO:
  380. frameLen += subbands + (8 * subbands);
  381. break;
  382. case SBC_DUAL_CHANNEL:
  383. frameLen *= 2;
  384. /* fall through */
  385. default:
  386. if (mode == SBC_MONO){
  387. frameLen += 4*subbands;
  388. } else {
  389. frameLen += 8*subbands;
  390. }
  391. }
  392. frameCount++;
  393. frameLen = SBC_HEADER_LEN + ((frameLen + 7) / 8);
  394. if (frameBytes > frameLen){
  395. frameBytes -= frameLen;
  396. frameData += frameLen;
  397. } else {
  398. frameBytes = 0;
  399. }
  400. }
  401. return frameCount;
  402. }
  403. /** Read quantized subband samples from the input bitstream and expand them. */
  404. #ifdef SPECIALIZE_READ_SAMPLES_JOINT
  405. PRIVATE void OI_SBC_ReadSamplesJoint4(OI_CODEC_SBC_DECODER_CONTEXT *context, OI_BITSTREAM *global_bs);
  406. PRIVATE void OI_SBC_ReadSamplesJoint4(OI_CODEC_SBC_DECODER_CONTEXT *context, OI_BITSTREAM *global_bs)
  407. {
  408. #define NROF_SUBBANDS 4
  409. #include "readsamplesjoint.inc"
  410. #undef NROF_SUBBANDS
  411. }
  412. PRIVATE void OI_SBC_ReadSamplesJoint8(OI_CODEC_SBC_DECODER_CONTEXT *context, OI_BITSTREAM *global_bs);
  413. PRIVATE void OI_SBC_ReadSamplesJoint8(OI_CODEC_SBC_DECODER_CONTEXT *context, OI_BITSTREAM *global_bs)
  414. {
  415. #define NROF_SUBBANDS 8
  416. #include "readsamplesjoint.inc"
  417. #undef NROF_SUBBANDS
  418. }
  419. typedef void (*READ_SAMPLES)(OI_CODEC_SBC_DECODER_CONTEXT *context, OI_BITSTREAM *global_bs);
  420. static const READ_SAMPLES SpecializedReadSamples[] = {
  421. OI_SBC_ReadSamplesJoint4,
  422. OI_SBC_ReadSamplesJoint8
  423. };
  424. #endif /* SPECIALIZE_READ_SAMPLES_JOINT */
  425. PRIVATE void OI_SBC_ReadSamplesJoint(OI_CODEC_SBC_DECODER_CONTEXT *context, OI_BITSTREAM *global_bs)
  426. {
  427. OI_CODEC_SBC_COMMON_CONTEXT *common = &context->common;
  428. OI_UINT nrof_subbands = common->frameInfo.nrof_subbands;
  429. #ifdef SPECIALIZE_READ_SAMPLES_JOINT
  430. OI_ASSERT((nrof_subbands >> 3u) <= 1u);
  431. SpecializedReadSamples[nrof_subbands >> 3](context, global_bs);
  432. #else
  433. #define NROF_SUBBANDS nrof_subbands
  434. #include "readsamplesjoint.inc"
  435. #undef NROF_SUBBANDS
  436. #endif /* SPECIALIZE_READ_SAMPLES_JOINT */
  437. }
  438. /**@}*/