decoder-sbc.c 16 KB

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