You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

380 lines
13KB

  1. /*
  2. * Bluetooth low-complexity, subband codec (SBC)
  3. *
  4. * Copyright (C) 2017 Aurelien Jacobs <aurel@gnuage.org>
  5. * Copyright (C) 2012-2013 Intel Corporation
  6. * Copyright (C) 2008-2010 Nokia Corporation
  7. * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
  8. * Copyright (C) 2004-2005 Henryk Ploetz <henryk@ploetzli.ch>
  9. * Copyright (C) 2005-2008 Brad Midgley <bmidgley@xmission.com>
  10. *
  11. * This file is part of FFmpeg.
  12. *
  13. * FFmpeg is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU Lesser General Public
  15. * License as published by the Free Software Foundation; either
  16. * version 2.1 of the License, or (at your option) any later version.
  17. *
  18. * FFmpeg is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  21. * Lesser General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Lesser General Public
  24. * License along with FFmpeg; if not, write to the Free Software
  25. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  26. */
  27. /**
  28. * @file
  29. * SBC decoder implementation
  30. */
  31. #include <stdbool.h>
  32. #include "avcodec.h"
  33. #include "internal.h"
  34. #include "libavutil/intreadwrite.h"
  35. #include "sbc.h"
  36. #include "sbcdec_data.h"
  37. struct sbc_decoder_state {
  38. int32_t V[2][170];
  39. int offset[2][16];
  40. };
  41. typedef struct SBCDecContext {
  42. AVClass *class;
  43. DECLARE_ALIGNED(SBC_ALIGN, struct sbc_frame, frame);
  44. DECLARE_ALIGNED(SBC_ALIGN, struct sbc_decoder_state, dsp);
  45. } SBCDecContext;
  46. /*
  47. * Unpacks a SBC frame at the beginning of the stream in data,
  48. * which has at most len bytes into frame.
  49. * Returns the length in bytes of the packed frame, or a negative
  50. * value on error. The error codes are:
  51. *
  52. * -1 Data stream too short
  53. * -2 Sync byte incorrect
  54. * -3 CRC8 incorrect
  55. * -4 Bitpool value out of bounds
  56. */
  57. static int sbc_unpack_frame(const uint8_t *data, struct sbc_frame *frame,
  58. size_t len)
  59. {
  60. unsigned int consumed;
  61. /* Will copy the parts of the header that are relevant to crc
  62. * calculation here */
  63. uint8_t crc_header[11] = { 0 };
  64. int crc_pos;
  65. int32_t temp;
  66. uint32_t audio_sample;
  67. int ch, sb, blk, bit; /* channel, subband, block and bit standard
  68. counters */
  69. int bits[2][8]; /* bits distribution */
  70. uint32_t levels[2][8]; /* levels derived from that */
  71. if (len < 4)
  72. return -1;
  73. if (data[0] == MSBC_SYNCWORD) {
  74. if (data[1] != 0)
  75. return -2;
  76. if (data[2] != 0)
  77. return -2;
  78. frame->frequency = SBC_FREQ_16000;
  79. frame->blocks = MSBC_BLOCKS;
  80. frame->allocation = LOUDNESS;
  81. frame->mode = MONO;
  82. frame->channels = 1;
  83. frame->subbands = 8;
  84. frame->bitpool = 26;
  85. } else if (data[0] == SBC_SYNCWORD) {
  86. frame->frequency = (data[1] >> 6) & 0x03;
  87. frame->blocks = 4 * ((data[1] >> 4) & 0x03) + 4;
  88. frame->mode = (data[1] >> 2) & 0x03;
  89. frame->channels = frame->mode == MONO ? 1 : 2;
  90. frame->allocation = (data[1] >> 1) & 0x01;
  91. frame->subbands = data[1] & 0x01 ? 8 : 4;
  92. frame->bitpool = data[2];
  93. if ((frame->mode == MONO || frame->mode == DUAL_CHANNEL) &&
  94. frame->bitpool > 16 * frame->subbands)
  95. return -4;
  96. if ((frame->mode == STEREO || frame->mode == JOINT_STEREO) &&
  97. frame->bitpool > 32 * frame->subbands)
  98. return -4;
  99. } else
  100. return -2;
  101. consumed = 32;
  102. crc_header[0] = data[1];
  103. crc_header[1] = data[2];
  104. crc_pos = 16;
  105. if (frame->mode == JOINT_STEREO) {
  106. if (len * 8 < consumed + frame->subbands)
  107. return -1;
  108. frame->joint = 0x00;
  109. for (sb = 0; sb < frame->subbands - 1; sb++)
  110. frame->joint |= ((data[4] >> (7 - sb)) & 0x01) << sb;
  111. if (frame->subbands == 4)
  112. crc_header[crc_pos / 8] = data[4] & 0xf0;
  113. else
  114. crc_header[crc_pos / 8] = data[4];
  115. consumed += frame->subbands;
  116. crc_pos += frame->subbands;
  117. }
  118. if (len * 8 < consumed + (4 * frame->subbands * frame->channels))
  119. return -1;
  120. for (ch = 0; ch < frame->channels; ch++) {
  121. for (sb = 0; sb < frame->subbands; sb++) {
  122. /* FIXME assert(consumed % 4 == 0); */
  123. frame->scale_factor[ch][sb] =
  124. (data[consumed >> 3] >> (4 - (consumed & 0x7))) & 0x0F;
  125. crc_header[crc_pos >> 3] |=
  126. frame->scale_factor[ch][sb] << (4 - (crc_pos & 0x7));
  127. consumed += 4;
  128. crc_pos += 4;
  129. }
  130. }
  131. if (data[3] != ff_sbc_crc8(frame->crc_ctx, crc_header, crc_pos))
  132. return -3;
  133. ff_sbc_calculate_bits(frame, bits);
  134. for (ch = 0; ch < frame->channels; ch++) {
  135. for (sb = 0; sb < frame->subbands; sb++)
  136. levels[ch][sb] = (1 << bits[ch][sb]) - 1;
  137. }
  138. for (blk = 0; blk < frame->blocks; blk++) {
  139. for (ch = 0; ch < frame->channels; ch++) {
  140. for (sb = 0; sb < frame->subbands; sb++) {
  141. uint32_t shift;
  142. if (levels[ch][sb] == 0) {
  143. frame->sb_sample[blk][ch][sb] = 0;
  144. continue;
  145. }
  146. shift = frame->scale_factor[ch][sb] +
  147. 1 + SBCDEC_FIXED_EXTRA_BITS;
  148. audio_sample = 0;
  149. for (bit = 0; bit < bits[ch][sb]; bit++) {
  150. if (consumed > len * 8)
  151. return -1;
  152. if ((data[consumed >> 3] >> (7 - (consumed & 0x7))) & 0x01)
  153. audio_sample |= 1 << (bits[ch][sb] - bit - 1);
  154. consumed++;
  155. }
  156. frame->sb_sample[blk][ch][sb] = (int32_t)
  157. (((((uint64_t) audio_sample << 1) | 1) << shift) /
  158. levels[ch][sb]) - (1 << shift);
  159. }
  160. }
  161. }
  162. if (frame->mode == JOINT_STEREO) {
  163. for (blk = 0; blk < frame->blocks; blk++) {
  164. for (sb = 0; sb < frame->subbands; sb++) {
  165. if (frame->joint & (0x01 << sb)) {
  166. temp = frame->sb_sample[blk][0][sb] +
  167. frame->sb_sample[blk][1][sb];
  168. frame->sb_sample[blk][1][sb] =
  169. frame->sb_sample[blk][0][sb] -
  170. frame->sb_sample[blk][1][sb];
  171. frame->sb_sample[blk][0][sb] = temp;
  172. }
  173. }
  174. }
  175. }
  176. if ((consumed & 0x7) != 0)
  177. consumed += 8 - (consumed & 0x7);
  178. return consumed >> 3;
  179. }
  180. static inline void sbc_synthesize_four(struct sbc_decoder_state *state,
  181. struct sbc_frame *frame,
  182. int ch, int blk, AVFrame *output_frame)
  183. {
  184. int i, k, idx;
  185. int32_t *v = state->V[ch];
  186. int *offset = state->offset[ch];
  187. for (i = 0; i < 8; i++) {
  188. /* Shifting */
  189. offset[i]--;
  190. if (offset[i] < 0) {
  191. offset[i] = 79;
  192. memcpy(v + 80, v, 9 * sizeof(*v));
  193. }
  194. /* Distribute the new matrix value to the shifted position */
  195. v[offset[i]] =
  196. ( ff_synmatrix4[i][0] * frame->sb_sample[blk][ch][0] +
  197. ff_synmatrix4[i][1] * frame->sb_sample[blk][ch][1] +
  198. ff_synmatrix4[i][2] * frame->sb_sample[blk][ch][2] +
  199. ff_synmatrix4[i][3] * frame->sb_sample[blk][ch][3] ) >> 15;
  200. }
  201. /* Compute the samples */
  202. for (idx = 0, i = 0; i < 4; i++, idx += 5) {
  203. k = (i + 4) & 0xf;
  204. /* Store in output, Q0 */
  205. AV_WN16A(&output_frame->data[ch][blk * 8 + i * 2], av_clip_int16(
  206. ( v[offset[i] + 0] * ff_sbc_proto_4_40m0[idx + 0] +
  207. v[offset[k] + 1] * ff_sbc_proto_4_40m1[idx + 0] +
  208. v[offset[i] + 2] * ff_sbc_proto_4_40m0[idx + 1] +
  209. v[offset[k] + 3] * ff_sbc_proto_4_40m1[idx + 1] +
  210. v[offset[i] + 4] * ff_sbc_proto_4_40m0[idx + 2] +
  211. v[offset[k] + 5] * ff_sbc_proto_4_40m1[idx + 2] +
  212. v[offset[i] + 6] * ff_sbc_proto_4_40m0[idx + 3] +
  213. v[offset[k] + 7] * ff_sbc_proto_4_40m1[idx + 3] +
  214. v[offset[i] + 8] * ff_sbc_proto_4_40m0[idx + 4] +
  215. v[offset[k] + 9] * ff_sbc_proto_4_40m1[idx + 4] ) >> 15));
  216. }
  217. }
  218. static inline void sbc_synthesize_eight(struct sbc_decoder_state *state,
  219. struct sbc_frame *frame,
  220. int ch, int blk, AVFrame *output_frame)
  221. {
  222. int i, k, idx;
  223. int32_t *v = state->V[ch];
  224. int *offset = state->offset[ch];
  225. for (i = 0; i < 16; i++) {
  226. /* Shifting */
  227. offset[i]--;
  228. if (offset[i] < 0) {
  229. offset[i] = 159;
  230. memcpy(v + 160, v, 9 * sizeof(*v));
  231. }
  232. /* Distribute the new matrix value to the shifted position */
  233. v[offset[i]] =
  234. ( ff_synmatrix8[i][0] * frame->sb_sample[blk][ch][0] +
  235. ff_synmatrix8[i][1] * frame->sb_sample[blk][ch][1] +
  236. ff_synmatrix8[i][2] * frame->sb_sample[blk][ch][2] +
  237. ff_synmatrix8[i][3] * frame->sb_sample[blk][ch][3] +
  238. ff_synmatrix8[i][4] * frame->sb_sample[blk][ch][4] +
  239. ff_synmatrix8[i][5] * frame->sb_sample[blk][ch][5] +
  240. ff_synmatrix8[i][6] * frame->sb_sample[blk][ch][6] +
  241. ff_synmatrix8[i][7] * frame->sb_sample[blk][ch][7] ) >> 15;
  242. }
  243. /* Compute the samples */
  244. for (idx = 0, i = 0; i < 8; i++, idx += 5) {
  245. k = (i + 8) & 0xf;
  246. /* Store in output, Q0 */
  247. AV_WN16A(&output_frame->data[ch][blk * 16 + i * 2], av_clip_int16(
  248. ( v[offset[i] + 0] * ff_sbc_proto_8_80m0[idx + 0] +
  249. v[offset[k] + 1] * ff_sbc_proto_8_80m1[idx + 0] +
  250. v[offset[i] + 2] * ff_sbc_proto_8_80m0[idx + 1] +
  251. v[offset[k] + 3] * ff_sbc_proto_8_80m1[idx + 1] +
  252. v[offset[i] + 4] * ff_sbc_proto_8_80m0[idx + 2] +
  253. v[offset[k] + 5] * ff_sbc_proto_8_80m1[idx + 2] +
  254. v[offset[i] + 6] * ff_sbc_proto_8_80m0[idx + 3] +
  255. v[offset[k] + 7] * ff_sbc_proto_8_80m1[idx + 3] +
  256. v[offset[i] + 8] * ff_sbc_proto_8_80m0[idx + 4] +
  257. v[offset[k] + 9] * ff_sbc_proto_8_80m1[idx + 4] ) >> 15));
  258. }
  259. }
  260. static void sbc_synthesize_audio(struct sbc_decoder_state *state,
  261. struct sbc_frame *frame, AVFrame *output_frame)
  262. {
  263. int ch, blk;
  264. switch (frame->subbands) {
  265. case 4:
  266. for (ch = 0; ch < frame->channels; ch++)
  267. for (blk = 0; blk < frame->blocks; blk++)
  268. sbc_synthesize_four(state, frame, ch, blk, output_frame);
  269. break;
  270. case 8:
  271. for (ch = 0; ch < frame->channels; ch++)
  272. for (blk = 0; blk < frame->blocks; blk++)
  273. sbc_synthesize_eight(state, frame, ch, blk, output_frame);
  274. break;
  275. }
  276. }
  277. static int sbc_decode_init(AVCodecContext *avctx)
  278. {
  279. SBCDecContext *sbc = avctx->priv_data;
  280. int i, ch;
  281. sbc->frame.crc_ctx = av_crc_get_table(AV_CRC_8_EBU);
  282. memset(sbc->dsp.V, 0, sizeof(sbc->dsp.V));
  283. for (ch = 0; ch < 2; ch++)
  284. for (i = 0; i < FF_ARRAY_ELEMS(sbc->dsp.offset[0]); i++)
  285. sbc->dsp.offset[ch][i] = (10 * i + 10);
  286. return 0;
  287. }
  288. static int sbc_decode_frame(AVCodecContext *avctx,
  289. void *data, int *got_frame_ptr,
  290. AVPacket *avpkt)
  291. {
  292. SBCDecContext *sbc = avctx->priv_data;
  293. AVFrame *frame = data;
  294. int ret, frame_length;
  295. if (!sbc)
  296. return AVERROR(EIO);
  297. frame_length = sbc_unpack_frame(avpkt->data, &sbc->frame, avpkt->size);
  298. if (frame_length <= 0)
  299. return frame_length;
  300. frame->channels = sbc->frame.channels;
  301. frame->format = AV_SAMPLE_FMT_S16P;
  302. frame->nb_samples = sbc->frame.blocks * sbc->frame.subbands;
  303. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  304. return ret;
  305. sbc_synthesize_audio(&sbc->dsp, &sbc->frame, frame);
  306. *got_frame_ptr = 1;
  307. return frame_length;
  308. }
  309. AVCodec ff_sbc_decoder = {
  310. .name = "sbc",
  311. .long_name = NULL_IF_CONFIG_SMALL("SBC (low-complexity subband codec)"),
  312. .type = AVMEDIA_TYPE_AUDIO,
  313. .id = AV_CODEC_ID_SBC,
  314. .priv_data_size = sizeof(SBCDecContext),
  315. .init = sbc_decode_init,
  316. .decode = sbc_decode_frame,
  317. .capabilities = AV_CODEC_CAP_DR1,
  318. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
  319. .channel_layouts = (const uint64_t[]) { AV_CH_LAYOUT_MONO,
  320. AV_CH_LAYOUT_STEREO, 0},
  321. .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16P,
  322. AV_SAMPLE_FMT_NONE },
  323. .supported_samplerates = (const int[]) { 16000, 32000, 44100, 48000, 0 },
  324. };