decoder-sbc.c 16 KB

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