3 * Bluetooth low-complexity, subband codec (SBC) library
5 * Copyright (C) 2008-2010 Nokia Corporation
6 * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
7 * Copyright (C) 2004-2005 Henryk Ploetz <henryk@ploetzli.ch>
8 * Copyright (C) 2005-2008 Brad Midgley <bmidgley@xmission.com>
9 * Copyright (C) 2012-2013 Intel Corporation
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
37 #include <sys/types.h>
41 #include "sbc_tables.h"
44 #include "sbc_private.h"
45 #include "sbc_primitives.h"
47 #define SBC_SYNCWORD 0x9C
49 #define MSBC_SYNCWORD 0xAD
50 #define MSBC_BLOCKS 15
52 #define A2DP_SAMPLING_FREQ_16000 (1 << 3)
53 #define A2DP_SAMPLING_FREQ_32000 (1 << 2)
54 #define A2DP_SAMPLING_FREQ_44100 (1 << 1)
55 #define A2DP_SAMPLING_FREQ_48000 (1 << 0)
57 #define A2DP_CHANNEL_MODE_MONO (1 << 3)
58 #define A2DP_CHANNEL_MODE_DUAL_CHANNEL (1 << 2)
59 #define A2DP_CHANNEL_MODE_STEREO (1 << 1)
60 #define A2DP_CHANNEL_MODE_JOINT_STEREO (1 << 0)
62 #define A2DP_BLOCK_LENGTH_4 (1 << 3)
63 #define A2DP_BLOCK_LENGTH_8 (1 << 2)
64 #define A2DP_BLOCK_LENGTH_12 (1 << 1)
65 #define A2DP_BLOCK_LENGTH_16 (1 << 0)
67 #define A2DP_SUBBANDS_4 (1 << 1)
68 #define A2DP_SUBBANDS_8 (1 << 0)
70 #define A2DP_ALLOCATION_SNR (1 << 1)
71 #define A2DP_ALLOCATION_LOUDNESS (1 << 0)
73 #if __BYTE_ORDER == __LITTLE_ENDIAN
76 uint8_t channel_mode:4;
78 uint8_t allocation_method:2;
80 uint8_t block_length:4;
83 } __attribute__ ((packed));
85 #elif __BYTE_ORDER == __BIG_ENDIAN
89 uint8_t channel_mode:4;
90 uint8_t block_length:4;
92 uint8_t allocation_method:2;
95 } __attribute__ ((packed));
98 #error "Unknown byte order"
101 /* This structure contains an unpacked SBC frame.
102 Yes, there is probably quite some unused space herein */
108 MONO = SBC_MODE_MONO,
109 DUAL_CHANNEL = SBC_MODE_DUAL_CHANNEL,
110 STEREO = SBC_MODE_STEREO,
111 JOINT_STEREO = SBC_MODE_JOINT_STEREO
115 LOUDNESS = SBC_AM_LOUDNESS,
118 uint8_t subband_mode;
124 /* bit number x set means joint stereo has been used in subband x */
127 /* only the lower 4 bits of every element are to be used */
128 uint32_t SBC_ALIGNED scale_factor[2][8];
130 /* raw integer subband samples in the frame */
131 int32_t SBC_ALIGNED sb_sample_f[16][2][8];
133 /* modified subband samples */
134 int32_t SBC_ALIGNED sb_sample[16][2][8];
136 /* original pcm audio samples */
137 int16_t SBC_ALIGNED pcm_sample[2][16*8];
140 struct sbc_decoder_state {
147 * Calculates the CRC-8 of the first len bits in data
149 static const uint8_t crc_table[256] = {
150 0x00, 0x1D, 0x3A, 0x27, 0x74, 0x69, 0x4E, 0x53,
151 0xE8, 0xF5, 0xD2, 0xCF, 0x9C, 0x81, 0xA6, 0xBB,
152 0xCD, 0xD0, 0xF7, 0xEA, 0xB9, 0xA4, 0x83, 0x9E,
153 0x25, 0x38, 0x1F, 0x02, 0x51, 0x4C, 0x6B, 0x76,
154 0x87, 0x9A, 0xBD, 0xA0, 0xF3, 0xEE, 0xC9, 0xD4,
155 0x6F, 0x72, 0x55, 0x48, 0x1B, 0x06, 0x21, 0x3C,
156 0x4A, 0x57, 0x70, 0x6D, 0x3E, 0x23, 0x04, 0x19,
157 0xA2, 0xBF, 0x98, 0x85, 0xD6, 0xCB, 0xEC, 0xF1,
158 0x13, 0x0E, 0x29, 0x34, 0x67, 0x7A, 0x5D, 0x40,
159 0xFB, 0xE6, 0xC1, 0xDC, 0x8F, 0x92, 0xB5, 0xA8,
160 0xDE, 0xC3, 0xE4, 0xF9, 0xAA, 0xB7, 0x90, 0x8D,
161 0x36, 0x2B, 0x0C, 0x11, 0x42, 0x5F, 0x78, 0x65,
162 0x94, 0x89, 0xAE, 0xB3, 0xE0, 0xFD, 0xDA, 0xC7,
163 0x7C, 0x61, 0x46, 0x5B, 0x08, 0x15, 0x32, 0x2F,
164 0x59, 0x44, 0x63, 0x7E, 0x2D, 0x30, 0x17, 0x0A,
165 0xB1, 0xAC, 0x8B, 0x96, 0xC5, 0xD8, 0xFF, 0xE2,
166 0x26, 0x3B, 0x1C, 0x01, 0x52, 0x4F, 0x68, 0x75,
167 0xCE, 0xD3, 0xF4, 0xE9, 0xBA, 0xA7, 0x80, 0x9D,
168 0xEB, 0xF6, 0xD1, 0xCC, 0x9F, 0x82, 0xA5, 0xB8,
169 0x03, 0x1E, 0x39, 0x24, 0x77, 0x6A, 0x4D, 0x50,
170 0xA1, 0xBC, 0x9B, 0x86, 0xD5, 0xC8, 0xEF, 0xF2,
171 0x49, 0x54, 0x73, 0x6E, 0x3D, 0x20, 0x07, 0x1A,
172 0x6C, 0x71, 0x56, 0x4B, 0x18, 0x05, 0x22, 0x3F,
173 0x84, 0x99, 0xBE, 0xA3, 0xF0, 0xED, 0xCA, 0xD7,
174 0x35, 0x28, 0x0F, 0x12, 0x41, 0x5C, 0x7B, 0x66,
175 0xDD, 0xC0, 0xE7, 0xFA, 0xA9, 0xB4, 0x93, 0x8E,
176 0xF8, 0xE5, 0xC2, 0xDF, 0x8C, 0x91, 0xB6, 0xAB,
177 0x10, 0x0D, 0x2A, 0x37, 0x64, 0x79, 0x5E, 0x43,
178 0xB2, 0xAF, 0x88, 0x95, 0xC6, 0xDB, 0xFC, 0xE1,
179 0x5A, 0x47, 0x60, 0x7D, 0x2E, 0x33, 0x14, 0x09,
180 0x7F, 0x62, 0x45, 0x58, 0x0B, 0x16, 0x31, 0x2C,
181 0x97, 0x8A, 0xAD, 0xB0, 0xE3, 0xFE, 0xD9, 0xC4
184 static uint8_t sbc_crc8(const uint8_t *data, size_t len)
190 for (i = 0; i < len / 8; i++)
191 crc = crc_table[crc ^ data[i]];
194 for (i = 0; i < len % 8; i++) {
195 char bit = ((octet ^ crc) & 0x80) >> 7;
197 crc = ((crc & 0x7f) << 1) ^ (bit ? 0x1d : 0);
206 * Code straight from the spec to calculate the bits array
207 * Takes a pointer to the frame in question, a pointer to the bits array and
208 * the sampling frequency (as 2 bit integer)
210 static SBC_ALWAYS_INLINE void sbc_calculate_bits_internal(
211 const struct sbc_frame *frame, int (*bits)[8], int subbands)
213 uint8_t sf = frame->frequency;
215 if (frame->mode == MONO || frame->mode == DUAL_CHANNEL) {
216 int bitneed[2][8], loudness, max_bitneed, bitcount, slicecount, bitslice;
219 for (ch = 0; ch < frame->channels; ch++) {
221 if (frame->allocation == SNR) {
222 for (sb = 0; sb < subbands; sb++) {
223 bitneed[ch][sb] = frame->scale_factor[ch][sb];
224 if (bitneed[ch][sb] > max_bitneed)
225 max_bitneed = bitneed[ch][sb];
228 for (sb = 0; sb < subbands; sb++) {
229 if (frame->scale_factor[ch][sb] == 0)
230 bitneed[ch][sb] = -5;
233 loudness = frame->scale_factor[ch][sb] - sbc_offset4[sf][sb];
235 loudness = frame->scale_factor[ch][sb] - sbc_offset8[sf][sb];
237 bitneed[ch][sb] = loudness / 2;
239 bitneed[ch][sb] = loudness;
241 if (bitneed[ch][sb] > max_bitneed)
242 max_bitneed = bitneed[ch][sb];
248 bitslice = max_bitneed + 1;
251 bitcount += slicecount;
253 for (sb = 0; sb < subbands; sb++) {
254 if ((bitneed[ch][sb] > bitslice + 1) && (bitneed[ch][sb] < bitslice + 16))
256 else if (bitneed[ch][sb] == bitslice + 1)
259 } while (bitcount + slicecount < frame->bitpool);
261 if (bitcount + slicecount == frame->bitpool) {
262 bitcount += slicecount;
266 for (sb = 0; sb < subbands; sb++) {
267 if (bitneed[ch][sb] < bitslice + 2)
270 bits[ch][sb] = bitneed[ch][sb] - bitslice;
271 if (bits[ch][sb] > 16)
276 for (sb = 0; bitcount < frame->bitpool &&
277 sb < subbands; sb++) {
278 if ((bits[ch][sb] >= 2) && (bits[ch][sb] < 16)) {
281 } else if ((bitneed[ch][sb] == bitslice + 1) && (frame->bitpool > bitcount + 1)) {
287 for (sb = 0; bitcount < frame->bitpool &&
288 sb < subbands; sb++) {
289 if (bits[ch][sb] < 16) {
297 } else if (frame->mode == STEREO || frame->mode == JOINT_STEREO) {
298 int bitneed[2][8], loudness, max_bitneed, bitcount, slicecount, bitslice;
302 if (frame->allocation == SNR) {
303 for (ch = 0; ch < 2; ch++) {
304 for (sb = 0; sb < subbands; sb++) {
305 bitneed[ch][sb] = frame->scale_factor[ch][sb];
306 if (bitneed[ch][sb] > max_bitneed)
307 max_bitneed = bitneed[ch][sb];
311 for (ch = 0; ch < 2; ch++) {
312 for (sb = 0; sb < subbands; sb++) {
313 if (frame->scale_factor[ch][sb] == 0)
314 bitneed[ch][sb] = -5;
317 loudness = frame->scale_factor[ch][sb] - sbc_offset4[sf][sb];
319 loudness = frame->scale_factor[ch][sb] - sbc_offset8[sf][sb];
321 bitneed[ch][sb] = loudness / 2;
323 bitneed[ch][sb] = loudness;
325 if (bitneed[ch][sb] > max_bitneed)
326 max_bitneed = bitneed[ch][sb];
333 bitslice = max_bitneed + 1;
336 bitcount += slicecount;
338 for (ch = 0; ch < 2; ch++) {
339 for (sb = 0; sb < subbands; sb++) {
340 if ((bitneed[ch][sb] > bitslice + 1) && (bitneed[ch][sb] < bitslice + 16))
342 else if (bitneed[ch][sb] == bitslice + 1)
346 } while (bitcount + slicecount < frame->bitpool);
348 if (bitcount + slicecount == frame->bitpool) {
349 bitcount += slicecount;
353 for (ch = 0; ch < 2; ch++) {
354 for (sb = 0; sb < subbands; sb++) {
355 if (bitneed[ch][sb] < bitslice + 2) {
358 bits[ch][sb] = bitneed[ch][sb] - bitslice;
359 if (bits[ch][sb] > 16)
367 while (bitcount < frame->bitpool) {
368 if ((bits[ch][sb] >= 2) && (bits[ch][sb] < 16)) {
371 } else if ((bitneed[ch][sb] == bitslice + 1) && (frame->bitpool > bitcount + 1)) {
386 while (bitcount < frame->bitpool) {
387 if (bits[ch][sb] < 16) {
404 static void sbc_calculate_bits(const struct sbc_frame *frame, int (*bits)[8])
406 if (frame->subbands == 4)
407 sbc_calculate_bits_internal(frame, bits, 4);
409 sbc_calculate_bits_internal(frame, bits, 8);
413 * Unpacks a SBC frame at the beginning of the stream in data,
414 * which has at most len bytes into frame.
415 * Returns the length in bytes of the packed frame, or a negative
416 * value on error. The error codes are:
418 * -1 Data stream too short
419 * -2 Sync byte incorrect
421 * -4 Bitpool value out of bounds
423 static int sbc_unpack_frame_internal(const uint8_t *data,
424 struct sbc_frame *frame, size_t len)
426 unsigned int consumed;
427 /* Will copy the parts of the header that are relevant to crc
428 * calculation here */
429 uint8_t crc_header[11] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
433 uint32_t audio_sample;
434 int ch, sb, blk, bit; /* channel, subband, block and bit standard
436 int bits[2][8]; /* bits distribution */
437 uint32_t levels[2][8]; /* levels derived from that */
441 crc_header[0] = data[1];
442 crc_header[1] = data[2];
445 if (frame->mode == JOINT_STEREO) {
446 if (len * 8 < consumed + frame->subbands)
450 for (sb = 0; sb < frame->subbands - 1; sb++)
451 frame->joint |= ((data[4] >> (7 - sb)) & 0x01) << sb;
452 if (frame->subbands == 4)
453 crc_header[crc_pos / 8] = data[4] & 0xf0;
455 crc_header[crc_pos / 8] = data[4];
457 consumed += frame->subbands;
458 crc_pos += frame->subbands;
461 if (len * 8 < consumed + (4 * frame->subbands * frame->channels))
464 for (ch = 0; ch < frame->channels; ch++) {
465 for (sb = 0; sb < frame->subbands; sb++) {
466 /* FIXME assert(consumed % 4 == 0); */
467 frame->scale_factor[ch][sb] =
468 (data[consumed >> 3] >> (4 - (consumed & 0x7))) & 0x0F;
469 crc_header[crc_pos >> 3] |=
470 frame->scale_factor[ch][sb] << (4 - (crc_pos & 0x7));
477 if (data[3] != sbc_crc8(crc_header, crc_pos))
480 sbc_calculate_bits(frame, bits);
482 for (ch = 0; ch < frame->channels; ch++) {
483 for (sb = 0; sb < frame->subbands; sb++)
484 levels[ch][sb] = (1 << bits[ch][sb]) - 1;
487 for (blk = 0; blk < frame->blocks; blk++) {
488 for (ch = 0; ch < frame->channels; ch++) {
489 for (sb = 0; sb < frame->subbands; sb++) {
492 if (levels[ch][sb] == 0) {
493 frame->sb_sample[blk][ch][sb] = 0;
497 shift = frame->scale_factor[ch][sb] +
498 1 + SBCDEC_FIXED_EXTRA_BITS;
501 for (bit = 0; bit < bits[ch][sb]; bit++) {
502 if (consumed > len * 8)
505 if ((data[consumed >> 3] >> (7 - (consumed & 0x7))) & 0x01)
506 audio_sample |= 1 << (bits[ch][sb] - bit - 1);
511 frame->sb_sample[blk][ch][sb] = (int32_t)
512 (((((uint64_t) audio_sample << 1) | 1) << shift) /
513 levels[ch][sb]) - (1 << shift);
518 if (frame->mode == JOINT_STEREO) {
519 for (blk = 0; blk < frame->blocks; blk++) {
520 for (sb = 0; sb < frame->subbands; sb++) {
521 if (frame->joint & (0x01 << sb)) {
522 temp = frame->sb_sample[blk][0][sb] +
523 frame->sb_sample[blk][1][sb];
524 frame->sb_sample[blk][1][sb] =
525 frame->sb_sample[blk][0][sb] -
526 frame->sb_sample[blk][1][sb];
527 frame->sb_sample[blk][0][sb] = temp;
533 if ((consumed & 0x7) != 0)
534 consumed += 8 - (consumed & 0x7);
536 return consumed >> 3;
539 static int sbc_unpack_frame(const uint8_t *data,
540 struct sbc_frame *frame, size_t len)
545 if (data[0] != SBC_SYNCWORD)
548 frame->frequency = (data[1] >> 6) & 0x03;
549 frame->block_mode = (data[1] >> 4) & 0x03;
551 switch (frame->block_mode) {
566 frame->mode = (data[1] >> 2) & 0x03;
568 switch (frame->mode) {
572 case DUAL_CHANNEL: /* fall-through */
579 frame->allocation = (data[1] >> 1) & 0x01;
581 frame->subband_mode = (data[1] & 0x01);
582 frame->subbands = frame->subband_mode ? 8 : 4;
584 frame->bitpool = data[2];
586 if ((frame->mode == MONO || frame->mode == DUAL_CHANNEL) &&
587 frame->bitpool > 16 * frame->subbands)
590 if ((frame->mode == STEREO || frame->mode == JOINT_STEREO) &&
591 frame->bitpool > 32 * frame->subbands)
594 return sbc_unpack_frame_internal(data, frame, len);
597 static int msbc_unpack_frame(const uint8_t *data,
598 struct sbc_frame *frame, size_t len)
603 if (data[0] != MSBC_SYNCWORD)
610 frame->frequency = SBC_FREQ_16000;
611 frame->block_mode = SBC_BLK_4;
612 frame->blocks = MSBC_BLOCKS;
613 frame->allocation = LOUDNESS;
616 frame->subband_mode = 1;
620 return sbc_unpack_frame_internal(data, frame, len);
623 static void sbc_decoder_init(struct sbc_decoder_state *state,
624 const struct sbc_frame *frame)
628 memset(state->V, 0, sizeof(state->V));
629 state->subbands = frame->subbands;
631 for (ch = 0; ch < 2; ch++)
632 for (i = 0; i < frame->subbands * 2; i++)
633 state->offset[ch][i] = (10 * i + 10);
636 static SBC_ALWAYS_INLINE int16_t sbc_clip16(int32_t s)
640 else if (s < -0x8000)
646 static inline void sbc_synthesize_four(struct sbc_decoder_state *state,
647 struct sbc_frame *frame, int ch, int blk)
650 int32_t *v = state->V[ch];
651 int *offset = state->offset[ch];
653 for (i = 0; i < 8; i++) {
658 memcpy(v + 80, v, 9 * sizeof(*v));
661 /* Distribute the new matrix value to the shifted position */
662 v[offset[i]] = SCALE4_STAGED1(
663 MULA(synmatrix4[i][0], frame->sb_sample[blk][ch][0],
664 MULA(synmatrix4[i][1], frame->sb_sample[blk][ch][1],
665 MULA(synmatrix4[i][2], frame->sb_sample[blk][ch][2],
666 MUL (synmatrix4[i][3], frame->sb_sample[blk][ch][3])))));
669 /* Compute the samples */
670 for (idx = 0, i = 0; i < 4; i++, idx += 5) {
673 /* Store in output, Q0 */
674 frame->pcm_sample[ch][blk * 4 + i] = sbc_clip16(SCALE4_STAGED1(
675 MULA(v[offset[i] + 0], sbc_proto_4_40m0[idx + 0],
676 MULA(v[offset[k] + 1], sbc_proto_4_40m1[idx + 0],
677 MULA(v[offset[i] + 2], sbc_proto_4_40m0[idx + 1],
678 MULA(v[offset[k] + 3], sbc_proto_4_40m1[idx + 1],
679 MULA(v[offset[i] + 4], sbc_proto_4_40m0[idx + 2],
680 MULA(v[offset[k] + 5], sbc_proto_4_40m1[idx + 2],
681 MULA(v[offset[i] + 6], sbc_proto_4_40m0[idx + 3],
682 MULA(v[offset[k] + 7], sbc_proto_4_40m1[idx + 3],
683 MULA(v[offset[i] + 8], sbc_proto_4_40m0[idx + 4],
684 MUL( v[offset[k] + 9], sbc_proto_4_40m1[idx + 4]))))))))))));
688 static inline void sbc_synthesize_eight(struct sbc_decoder_state *state,
689 struct sbc_frame *frame, int ch, int blk)
692 int *offset = state->offset[ch];
694 for (i = 0; i < 16; i++) {
699 for (j = 0; j < 9; j++)
700 state->V[ch][j + 160] = state->V[ch][j];
703 /* Distribute the new matrix value to the shifted position */
704 state->V[ch][offset[i]] = SCALE8_STAGED1(
705 MULA(synmatrix8[i][0], frame->sb_sample[blk][ch][0],
706 MULA(synmatrix8[i][1], frame->sb_sample[blk][ch][1],
707 MULA(synmatrix8[i][2], frame->sb_sample[blk][ch][2],
708 MULA(synmatrix8[i][3], frame->sb_sample[blk][ch][3],
709 MULA(synmatrix8[i][4], frame->sb_sample[blk][ch][4],
710 MULA(synmatrix8[i][5], frame->sb_sample[blk][ch][5],
711 MULA(synmatrix8[i][6], frame->sb_sample[blk][ch][6],
712 MUL( synmatrix8[i][7], frame->sb_sample[blk][ch][7])))))))));
715 /* Compute the samples */
716 for (idx = 0, i = 0; i < 8; i++, idx += 5) {
719 /* Store in output, Q0 */
720 frame->pcm_sample[ch][blk * 8 + i] = sbc_clip16(SCALE8_STAGED1(
721 MULA(state->V[ch][offset[i] + 0], sbc_proto_8_80m0[idx + 0],
722 MULA(state->V[ch][offset[k] + 1], sbc_proto_8_80m1[idx + 0],
723 MULA(state->V[ch][offset[i] + 2], sbc_proto_8_80m0[idx + 1],
724 MULA(state->V[ch][offset[k] + 3], sbc_proto_8_80m1[idx + 1],
725 MULA(state->V[ch][offset[i] + 4], sbc_proto_8_80m0[idx + 2],
726 MULA(state->V[ch][offset[k] + 5], sbc_proto_8_80m1[idx + 2],
727 MULA(state->V[ch][offset[i] + 6], sbc_proto_8_80m0[idx + 3],
728 MULA(state->V[ch][offset[k] + 7], sbc_proto_8_80m1[idx + 3],
729 MULA(state->V[ch][offset[i] + 8], sbc_proto_8_80m0[idx + 4],
730 MUL( state->V[ch][offset[k] + 9], sbc_proto_8_80m1[idx + 4]))))))))))));
734 static int sbc_synthesize_audio(struct sbc_decoder_state *state,
735 struct sbc_frame *frame)
739 switch (frame->subbands) {
741 for (ch = 0; ch < frame->channels; ch++) {
742 for (blk = 0; blk < frame->blocks; blk++)
743 sbc_synthesize_four(state, frame, ch, blk);
745 return frame->blocks * 4;
748 for (ch = 0; ch < frame->channels; ch++) {
749 for (blk = 0; blk < frame->blocks; blk++)
750 sbc_synthesize_eight(state, frame, ch, blk);
752 return frame->blocks * 8;
759 static int sbc_analyze_audio(struct sbc_encoder_state *state,
760 struct sbc_frame *frame)
765 switch (frame->subbands) {
767 for (ch = 0; ch < frame->channels; ch++) {
768 x = &state->X[ch][state->position - 4 *
769 state->increment + frame->blocks * 4];
770 for (blk = 0; blk < frame->blocks;
771 blk += state->increment) {
772 state->sbc_analyze_4s(
774 frame->sb_sample_f[blk][ch],
775 frame->sb_sample_f[blk + 1][ch] -
776 frame->sb_sample_f[blk][ch]);
777 x -= 4 * state->increment;
780 return frame->blocks * 4;
783 for (ch = 0; ch < frame->channels; ch++) {
784 x = &state->X[ch][state->position - 8 *
785 state->increment + frame->blocks * 8];
786 for (blk = 0; blk < frame->blocks;
787 blk += state->increment) {
788 state->sbc_analyze_8s(
790 frame->sb_sample_f[blk][ch],
791 frame->sb_sample_f[blk + 1][ch] -
792 frame->sb_sample_f[blk][ch]);
793 x -= 8 * state->increment;
796 return frame->blocks * 8;
803 /* Supplementary bitstream writing macros for 'sbc_pack_frame' */
805 #define PUT_BITS(data_ptr, bits_cache, bits_count, v, n) \
807 bits_cache = (v) | (bits_cache << (n)); \
809 if (bits_count >= 16) { \
811 *data_ptr++ = (uint8_t) \
812 (bits_cache >> bits_count); \
814 *data_ptr++ = (uint8_t) \
815 (bits_cache >> bits_count); \
819 #define FLUSH_BITS(data_ptr, bits_cache, bits_count) \
821 while (bits_count >= 8) { \
823 *data_ptr++ = (uint8_t) \
824 (bits_cache >> bits_count); \
826 if (bits_count > 0) \
827 *data_ptr++ = (uint8_t) \
828 (bits_cache << (8 - bits_count)); \
832 * Packs the SBC frame from frame into the memory at data. At most len
833 * bytes will be used, should more memory be needed an appropriate
834 * error code will be returned. Returns the length of the packed frame
835 * on success or a negative value on error.
837 * The error codes are:
838 * -1 Not enough memory reserved
839 * -2 Unsupported sampling rate
840 * -3 Unsupported number of blocks
841 * -4 Unsupported number of subbands
842 * -5 Bitpool value out of bounds
843 * -99 not implemented
846 static SBC_ALWAYS_INLINE ssize_t sbc_pack_frame_internal(uint8_t *data,
847 struct sbc_frame *frame, size_t len,
848 int frame_subbands, int frame_channels,
851 /* Bitstream writer starts from the fourth byte */
852 uint8_t *data_ptr = data + 4;
853 uint32_t bits_cache = 0;
854 uint32_t bits_count = 0;
856 /* Will copy the header parts for CRC-8 calculation here */
857 uint8_t crc_header[11] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
860 uint32_t audio_sample;
862 int ch, sb, blk; /* channel, subband, block and bit counters */
863 int bits[2][8]; /* bits distribution */
864 uint32_t levels[2][8]; /* levels are derived from that */
865 uint32_t sb_sample_delta[2][8];
867 /* Can't fill in crc yet */
869 crc_header[0] = data[1];
870 crc_header[1] = data[2];
873 if (frame->mode == JOINT_STEREO) {
874 PUT_BITS(data_ptr, bits_cache, bits_count,
875 joint, frame_subbands);
876 crc_header[crc_pos >> 3] = joint;
877 crc_pos += frame_subbands;
880 for (ch = 0; ch < frame_channels; ch++) {
881 for (sb = 0; sb < frame_subbands; sb++) {
882 PUT_BITS(data_ptr, bits_cache, bits_count,
883 frame->scale_factor[ch][sb] & 0x0F, 4);
884 crc_header[crc_pos >> 3] <<= 4;
885 crc_header[crc_pos >> 3] |= frame->scale_factor[ch][sb] & 0x0F;
890 /* align the last crc byte */
892 crc_header[crc_pos >> 3] <<= 8 - (crc_pos % 8);
894 data[3] = sbc_crc8(crc_header, crc_pos);
896 sbc_calculate_bits(frame, bits);
898 for (ch = 0; ch < frame_channels; ch++) {
899 for (sb = 0; sb < frame_subbands; sb++) {
900 levels[ch][sb] = ((1 << bits[ch][sb]) - 1) <<
901 (32 - (frame->scale_factor[ch][sb] +
902 SCALE_OUT_BITS + 2));
903 sb_sample_delta[ch][sb] = (uint32_t) 1 <<
904 (frame->scale_factor[ch][sb] +
909 for (blk = 0; blk < frame->blocks; blk++) {
910 for (ch = 0; ch < frame_channels; ch++) {
911 for (sb = 0; sb < frame_subbands; sb++) {
913 if (bits[ch][sb] == 0)
916 audio_sample = ((uint64_t) levels[ch][sb] *
917 (sb_sample_delta[ch][sb] +
918 frame->sb_sample_f[blk][ch][sb])) >> 32;
920 PUT_BITS(data_ptr, bits_cache, bits_count,
921 audio_sample, bits[ch][sb]);
926 FLUSH_BITS(data_ptr, bits_cache, bits_count);
928 return data_ptr - data;
931 static ssize_t sbc_pack_frame(uint8_t *data, struct sbc_frame *frame, size_t len,
934 int frame_subbands = 4;
936 data[0] = SBC_SYNCWORD;
938 data[1] = (frame->frequency & 0x03) << 6;
939 data[1] |= (frame->block_mode & 0x03) << 4;
940 data[1] |= (frame->mode & 0x03) << 2;
941 data[1] |= (frame->allocation & 0x01) << 1;
943 data[2] = frame->bitpool;
945 if (frame->subbands != 4)
948 if ((frame->mode == MONO || frame->mode == DUAL_CHANNEL) &&
949 frame->bitpool > frame_subbands << 4)
952 if ((frame->mode == STEREO || frame->mode == JOINT_STEREO) &&
953 frame->bitpool > frame_subbands << 5)
956 if (frame->subbands == 4) {
957 if (frame->channels == 1)
958 return sbc_pack_frame_internal(
959 data, frame, len, 4, 1, joint);
961 return sbc_pack_frame_internal(
962 data, frame, len, 4, 2, joint);
965 if (frame->channels == 1)
966 return sbc_pack_frame_internal(
967 data, frame, len, 8, 1, joint);
969 return sbc_pack_frame_internal(
970 data, frame, len, 8, 2, joint);
974 static ssize_t msbc_pack_frame(uint8_t *data, struct sbc_frame *frame,
975 size_t len, int joint)
977 data[0] = MSBC_SYNCWORD;
981 return sbc_pack_frame_internal(data, frame, len, 8, 1, joint);
984 static void sbc_encoder_init(bool msbc, struct sbc_encoder_state *state,
985 const struct sbc_frame *frame)
987 memset(&state->X, 0, sizeof(state->X));
988 state->position = (SBC_X_BUFFER_SIZE - frame->subbands * 9) & ~7;
990 state->increment = 1;
992 state->increment = 4;
994 sbc_init_primitives(state);
1000 struct SBC_ALIGNED sbc_frame frame;
1001 struct SBC_ALIGNED sbc_decoder_state dec_state;
1002 struct SBC_ALIGNED sbc_encoder_state enc_state;
1003 int (*unpack_frame)(const uint8_t *data, struct sbc_frame *frame,
1005 ssize_t (*pack_frame)(uint8_t *data, struct sbc_frame *frame,
1006 size_t len, int joint);
1009 static void sbc_set_defaults(sbc_t *sbc, unsigned long flags)
1011 struct sbc_priv *priv = sbc->priv;
1014 priv->pack_frame = msbc_pack_frame;
1015 priv->unpack_frame = msbc_unpack_frame;
1017 priv->pack_frame = sbc_pack_frame;
1018 priv->unpack_frame = sbc_unpack_frame;
1022 sbc->frequency = SBC_FREQ_44100;
1023 sbc->mode = SBC_MODE_STEREO;
1024 sbc->subbands = SBC_SB_8;
1025 sbc->blocks = SBC_BLK_16;
1027 #if __BYTE_ORDER == __LITTLE_ENDIAN
1028 sbc->endian = SBC_LE;
1029 #elif __BYTE_ORDER == __BIG_ENDIAN
1030 sbc->endian = SBC_BE;
1032 #error "Unknown byte order"
1036 SBC_EXPORT int sbc_init(sbc_t *sbc, unsigned long flags)
1041 memset(sbc, 0, sizeof(sbc_t));
1043 sbc->priv_alloc_base = malloc(sizeof(struct sbc_priv) + SBC_ALIGN_MASK);
1044 if (!sbc->priv_alloc_base)
1047 sbc->priv = (void *) (((uintptr_t) sbc->priv_alloc_base +
1048 SBC_ALIGN_MASK) & ~((uintptr_t) SBC_ALIGN_MASK));
1050 memset(sbc->priv, 0, sizeof(struct sbc_priv));
1052 sbc_set_defaults(sbc, flags);
1057 SBC_EXPORT int sbc_init_msbc(sbc_t *sbc, unsigned long flags)
1059 struct sbc_priv *priv;
1064 memset(sbc, 0, sizeof(sbc_t));
1066 sbc->priv_alloc_base = malloc(sizeof(struct sbc_priv) + SBC_ALIGN_MASK);
1067 if (!sbc->priv_alloc_base)
1070 sbc->priv = (void *) (((uintptr_t) sbc->priv_alloc_base +
1071 SBC_ALIGN_MASK) & ~((uintptr_t) SBC_ALIGN_MASK));
1073 memset(sbc->priv, 0, sizeof(struct sbc_priv));
1078 sbc_set_defaults(sbc, flags);
1080 sbc->frequency = SBC_FREQ_16000;
1081 sbc->blocks = MSBC_BLOCKS;
1082 sbc->subbands = SBC_SB_8;
1083 sbc->mode = SBC_MODE_MONO;
1084 sbc->allocation = SBC_AM_LOUDNESS;
1090 static int sbc_set_a2dp(sbc_t *sbc, unsigned long flags,
1091 const void *conf, size_t conf_len)
1093 const struct a2dp_sbc *a2dp;
1095 if (conf_len != sizeof(*a2dp))
1100 switch (a2dp->frequency) {
1101 case A2DP_SAMPLING_FREQ_16000:
1102 sbc->frequency = SBC_FREQ_16000;
1104 case A2DP_SAMPLING_FREQ_32000:
1105 sbc->frequency = SBC_FREQ_32000;
1107 case A2DP_SAMPLING_FREQ_44100:
1108 sbc->frequency = SBC_FREQ_44100;
1110 case A2DP_SAMPLING_FREQ_48000:
1111 sbc->frequency = SBC_FREQ_48000;
1117 switch (a2dp->channel_mode) {
1118 case A2DP_CHANNEL_MODE_MONO:
1119 sbc->mode = SBC_MODE_MONO;
1121 case A2DP_CHANNEL_MODE_DUAL_CHANNEL:
1122 sbc->mode = SBC_MODE_DUAL_CHANNEL;
1124 case A2DP_CHANNEL_MODE_STEREO:
1125 sbc->mode = SBC_MODE_STEREO;
1127 case A2DP_CHANNEL_MODE_JOINT_STEREO:
1128 sbc->mode = SBC_MODE_JOINT_STEREO;
1134 switch (a2dp->allocation_method) {
1135 case A2DP_ALLOCATION_SNR:
1136 sbc->allocation = SBC_AM_SNR;
1138 case A2DP_ALLOCATION_LOUDNESS:
1139 sbc->allocation = SBC_AM_LOUDNESS;
1145 switch (a2dp->subbands) {
1146 case A2DP_SUBBANDS_4:
1147 sbc->subbands = SBC_SB_4;
1149 case A2DP_SUBBANDS_8:
1150 sbc->subbands = SBC_SB_8;
1156 switch (a2dp->block_length) {
1157 case A2DP_BLOCK_LENGTH_4:
1158 sbc->blocks = SBC_BLK_4;
1160 case A2DP_BLOCK_LENGTH_8:
1161 sbc->blocks = SBC_BLK_8;
1163 case A2DP_BLOCK_LENGTH_12:
1164 sbc->blocks = SBC_BLK_12;
1166 case A2DP_BLOCK_LENGTH_16:
1167 sbc->blocks = SBC_BLK_16;
1176 SBC_EXPORT int sbc_init_a2dp(sbc_t *sbc, unsigned long flags,
1177 const void *conf, size_t conf_len)
1181 err = sbc_init(sbc, flags);
1185 err = sbc_set_a2dp(sbc, flags, conf, conf_len);
1194 int sbc_reinit_a2dp(sbc_t *sbc, unsigned long flags,
1195 const void *conf, size_t conf_len)
1199 err = sbc_reinit(sbc, flags);
1203 return sbc_set_a2dp(sbc, flags, conf, conf_len);
1206 SBC_EXPORT ssize_t sbc_parse(sbc_t *sbc, const void *input, size_t input_len)
1208 return sbc_decode(sbc, input, input_len, NULL, 0, NULL);
1211 SBC_EXPORT ssize_t sbc_decode(sbc_t *sbc, const void *input, size_t input_len,
1212 void *output, size_t output_len, size_t *written)
1214 struct sbc_priv *priv;
1216 int i, ch, framelen, samples;
1223 framelen = priv->unpack_frame(input, &priv->frame, input_len);
1226 sbc_decoder_init(&priv->dec_state, &priv->frame);
1229 sbc->frequency = priv->frame.frequency;
1230 sbc->mode = priv->frame.mode;
1231 sbc->subbands = priv->frame.subband_mode;
1232 sbc->blocks = priv->frame.block_mode;
1233 sbc->allocation = priv->frame.allocation;
1234 sbc->bitpool = priv->frame.bitpool;
1236 priv->frame.codesize = sbc_get_codesize(sbc);
1237 priv->frame.length = framelen;
1238 } else if (priv->frame.bitpool != sbc->bitpool) {
1239 priv->frame.length = framelen;
1240 sbc->bitpool = priv->frame.bitpool;
1252 samples = sbc_synthesize_audio(&priv->dec_state, &priv->frame);
1256 if (output_len < (size_t) (samples * priv->frame.channels * 2))
1257 samples = output_len / (priv->frame.channels * 2);
1259 for (i = 0; i < samples; i++) {
1260 for (ch = 0; ch < priv->frame.channels; ch++) {
1262 s = priv->frame.pcm_sample[ch][i];
1264 if (sbc->endian == SBC_BE) {
1265 *ptr++ = (s & 0xff00) >> 8;
1266 *ptr++ = (s & 0x00ff);
1268 *ptr++ = (s & 0x00ff);
1269 *ptr++ = (s & 0xff00) >> 8;
1275 *written = samples * priv->frame.channels * 2;
1280 SBC_EXPORT ssize_t sbc_encode(sbc_t *sbc, const void *input, size_t input_len,
1281 void *output, size_t output_len, ssize_t *written)
1283 struct sbc_priv *priv;
1286 int (*sbc_enc_process_input)(int position,
1287 const uint8_t *pcm, int16_t X[2][SBC_X_BUFFER_SIZE],
1288 int nsamples, int nchannels);
1299 priv->frame.frequency = sbc->frequency;
1300 priv->frame.mode = sbc->mode;
1301 priv->frame.channels = sbc->mode == SBC_MODE_MONO ? 1 : 2;
1302 priv->frame.allocation = sbc->allocation;
1303 priv->frame.subband_mode = sbc->subbands;
1304 priv->frame.subbands = sbc->subbands ? 8 : 4;
1305 priv->frame.block_mode = sbc->blocks;
1307 priv->frame.blocks = MSBC_BLOCKS;
1309 priv->frame.blocks = 4 + (sbc->blocks * 4);
1310 priv->frame.bitpool = sbc->bitpool;
1311 priv->frame.codesize = sbc_get_codesize(sbc);
1312 priv->frame.length = sbc_get_frame_length(sbc);
1314 sbc_encoder_init(priv->msbc, &priv->enc_state, &priv->frame);
1316 } else if (priv->frame.bitpool != sbc->bitpool) {
1317 priv->frame.length = sbc_get_frame_length(sbc);
1318 priv->frame.bitpool = sbc->bitpool;
1321 /* input must be large enough to encode a complete frame */
1322 if (input_len < priv->frame.codesize)
1325 /* output must be large enough to receive the encoded frame */
1326 if (!output || output_len < priv->frame.length)
1329 /* Select the needed input data processing function and call it */
1330 if (priv->frame.subbands == 8) {
1331 if (sbc->endian == SBC_BE)
1332 sbc_enc_process_input =
1333 priv->enc_state.sbc_enc_process_input_8s_be;
1335 sbc_enc_process_input =
1336 priv->enc_state.sbc_enc_process_input_8s_le;
1338 if (sbc->endian == SBC_BE)
1339 sbc_enc_process_input =
1340 priv->enc_state.sbc_enc_process_input_4s_be;
1342 sbc_enc_process_input =
1343 priv->enc_state.sbc_enc_process_input_4s_le;
1346 priv->enc_state.position = sbc_enc_process_input(
1347 priv->enc_state.position, (const uint8_t *) input,
1348 priv->enc_state.X, priv->frame.subbands * priv->frame.blocks,
1349 priv->frame.channels);
1351 samples = sbc_analyze_audio(&priv->enc_state, &priv->frame);
1353 if (priv->frame.mode == JOINT_STEREO) {
1354 int j = priv->enc_state.sbc_calc_scalefactors_j(
1355 priv->frame.sb_sample_f, priv->frame.scale_factor,
1356 priv->frame.blocks, priv->frame.subbands);
1357 framelen = priv->pack_frame(output,
1358 &priv->frame, output_len, j);
1360 priv->enc_state.sbc_calc_scalefactors(
1361 priv->frame.sb_sample_f, priv->frame.scale_factor,
1362 priv->frame.blocks, priv->frame.channels,
1363 priv->frame.subbands);
1364 framelen = priv->pack_frame(output,
1365 &priv->frame, output_len, 0);
1369 *written = framelen;
1371 return samples * priv->frame.channels * 2;
1374 SBC_EXPORT void sbc_finish(sbc_t *sbc)
1379 free(sbc->priv_alloc_base);
1381 memset(sbc, 0, sizeof(sbc_t));
1384 SBC_EXPORT size_t sbc_get_frame_length(sbc_t *sbc)
1387 uint8_t subbands, channels, blocks, joint, bitpool;
1388 struct sbc_priv *priv;
1391 if (priv->init && priv->frame.bitpool == sbc->bitpool)
1392 return priv->frame.length;
1394 subbands = sbc->subbands ? 8 : 4;
1396 blocks = MSBC_BLOCKS;
1398 blocks = 4 + (sbc->blocks * 4);
1399 channels = sbc->mode == SBC_MODE_MONO ? 1 : 2;
1400 joint = sbc->mode == SBC_MODE_JOINT_STEREO ? 1 : 0;
1401 bitpool = sbc->bitpool;
1403 ret = 4 + (4 * subbands * channels) / 8;
1404 /* This term is not always evenly divide so we round it up */
1406 ret += ((blocks * channels * bitpool) + 7) / 8;
1408 ret += (((joint ? subbands : 0) + blocks * bitpool) + 7) / 8;
1413 SBC_EXPORT unsigned sbc_get_frame_duration(sbc_t *sbc)
1415 uint8_t subbands, blocks;
1417 struct sbc_priv *priv;
1421 subbands = sbc->subbands ? 8 : 4;
1423 blocks = MSBC_BLOCKS;
1425 blocks = 4 + (sbc->blocks * 4);
1427 subbands = priv->frame.subbands;
1428 blocks = priv->frame.blocks;
1431 switch (sbc->frequency) {
1432 case SBC_FREQ_16000:
1436 case SBC_FREQ_32000:
1440 case SBC_FREQ_44100:
1444 case SBC_FREQ_48000:
1451 return (1000000 * blocks * subbands) / frequency;
1454 SBC_EXPORT size_t sbc_get_codesize(sbc_t *sbc)
1456 uint16_t subbands, channels, blocks;
1457 struct sbc_priv *priv;
1461 subbands = sbc->subbands ? 8 : 4;
1463 blocks = MSBC_BLOCKS;
1465 blocks = 4 + (sbc->blocks * 4);
1466 channels = sbc->mode == SBC_MODE_MONO ? 1 : 2;
1468 subbands = priv->frame.subbands;
1469 blocks = priv->frame.blocks;
1470 channels = priv->frame.channels;
1473 return subbands * blocks * channels * 2;
1476 SBC_EXPORT const char *sbc_get_implementation_info(sbc_t *sbc)
1478 struct sbc_priv *priv;
1487 return priv->enc_state.implementation_info;
1490 SBC_EXPORT int sbc_reinit(sbc_t *sbc, unsigned long flags)
1492 struct sbc_priv *priv;
1494 if (!sbc || !sbc->priv)
1500 memset(sbc->priv, 0, sizeof(struct sbc_priv));
1502 sbc_set_defaults(sbc, flags);