3 * Bluetooth low-complexity, subband codec (SBC) library
5 * Copyright (C) 2004-2006 Marcel Holtmann <marcel@holtmann.org>
6 * Copyright (C) 2004-2005 Henryk Ploetz <henryk@ploetzli.ch>
7 * Copyright (C) 2005-2006 Brad Midgley <bmidgley@xmission.com>
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 use a log2 table for byte integer scale factors calculation (sum log2 results for high and low bytes)
29 fill bitpool by 16 bits instead of one at a time in bits allocation/bitpool generation
31 don't consume more bytes than passed into the encoder
43 #include <sys/types.h>
44 #include <sys/errno.h>
48 #include "sbc_tables.h"
49 #include "cutils/log.h"
52 #define SBC_SYNCWORD 0xAD
53 #define SBC_POOLTAG 'CBSR'
55 /* sampling frequency */
56 #define SBC_FS_16 0x00
57 #define SBC_FS_32 0x01
58 #define SBC_FS_44 0x02
59 #define SBC_FS_48 0x03
64 #define SBC_NB_12 0x02
65 #define SBC_NB_16 0x03
68 #define SBC_CM_MONO 0x00
69 #define SBC_CM_DUAL_CHANNEL 0x01
70 #define SBC_CM_STEREO 0x02
71 #define SBC_CM_JOINT_STEREO 0x03
74 #define SBC_AM_LOUDNESS 0x00
75 #define SBC_AM_SNR 0x01
81 #define SBC_ALIGN_BITS 4
82 #define SBC_ALIGN_MASK ((1 << (SBC_ALIGN_BITS)) - 1)
85 typedef unsigned short uint16_t;
86 typedef unsigned char uint8_t;
87 typedef short int16_t;
88 typedef unsigned int u_int32_t
;
91 typedef unsigned int UINT
;
95 #if defined(WIN32) && !defined(__cplusplus)
97 #define inline __inline
101 typedef long long sbc_extended_t
;
103 static __inline void sbc_synthesize_four(struct sbc_decoder_state *state,
104 struct sbc_frame *frame, int ch, int blk);
108 /* This structure contains an unpacked SBC frame.
109 Yes, there is probably quite some unused space herein */
112 uint16_t sampling_frequency
; /* in kHz */
113 unsigned char blocks
;
116 DUAL_CHANNEL
= SBC_CM_DUAL_CHANNEL
,
117 STEREO
= SBC_CM_STEREO
,
118 JOINT_STEREO
= SBC_CM_JOINT_STEREO
122 LOUDNESS
= SBC_AM_LOUDNESS
,
127 uint8_t join
; /* bit number x set means joint stereo has been used in subband x */
128 int scale_factor
[2][8]; /* only the lower 4 bits of every element are to be used */
129 uint16_t audio_sample
[16][2][8]; /* raw integer subband samples in the frame */
131 int sb_sample_f
[16][2][8];
132 int sb_sample
[16][2][8]; /* modified subband samples */
133 short pcm_sample
[2][16*8]; /* original pcm audio samples */
136 struct sbc_decoder_state
{
142 struct sbc_encoder_state
{
148 * Calculates the CRC-8 of the first len bits in data
150 static const uint8_t crc_table
[256] = {
151 0x00, 0x1D, 0x3A, 0x27, 0x74, 0x69, 0x4E, 0x53,
152 0xE8, 0xF5, 0xD2, 0xCF, 0x9C, 0x81, 0xA6, 0xBB,
153 0xCD, 0xD0, 0xF7, 0xEA, 0xB9, 0xA4, 0x83, 0x9E,
154 0x25, 0x38, 0x1F, 0x02, 0x51, 0x4C, 0x6B, 0x76,
155 0x87, 0x9A, 0xBD, 0xA0, 0xF3, 0xEE, 0xC9, 0xD4,
156 0x6F, 0x72, 0x55, 0x48, 0x1B, 0x06, 0x21, 0x3C,
157 0x4A, 0x57, 0x70, 0x6D, 0x3E, 0x23, 0x04, 0x19,
158 0xA2, 0xBF, 0x98, 0x85, 0xD6, 0xCB, 0xEC, 0xF1,
159 0x13, 0x0E, 0x29, 0x34, 0x67, 0x7A, 0x5D, 0x40,
160 0xFB, 0xE6, 0xC1, 0xDC, 0x8F, 0x92, 0xB5, 0xA8,
161 0xDE, 0xC3, 0xE4, 0xF9, 0xAA, 0xB7, 0x90, 0x8D,
162 0x36, 0x2B, 0x0C, 0x11, 0x42, 0x5F, 0x78, 0x65,
163 0x94, 0x89, 0xAE, 0xB3, 0xE0, 0xFD, 0xDA, 0xC7,
164 0x7C, 0x61, 0x46, 0x5B, 0x08, 0x15, 0x32, 0x2F,
165 0x59, 0x44, 0x63, 0x7E, 0x2D, 0x30, 0x17, 0x0A,
166 0xB1, 0xAC, 0x8B, 0x96, 0xC5, 0xD8, 0xFF, 0xE2,
167 0x26, 0x3B, 0x1C, 0x01, 0x52, 0x4F, 0x68, 0x75,
168 0xCE, 0xD3, 0xF4, 0xE9, 0xBA, 0xA7, 0x80, 0x9D,
169 0xEB, 0xF6, 0xD1, 0xCC, 0x9F, 0x82, 0xA5, 0xB8,
170 0x03, 0x1E, 0x39, 0x24, 0x77, 0x6A, 0x4D, 0x50,
171 0xA1, 0xBC, 0x9B, 0x86, 0xD5, 0xC8, 0xEF, 0xF2,
172 0x49, 0x54, 0x73, 0x6E, 0x3D, 0x20, 0x07, 0x1A,
173 0x6C, 0x71, 0x56, 0x4B, 0x18, 0x05, 0x22, 0x3F,
174 0x84, 0x99, 0xBE, 0xA3, 0xF0, 0xED, 0xCA, 0xD7,
175 0x35, 0x28, 0x0F, 0x12, 0x41, 0x5C, 0x7B, 0x66,
176 0xDD, 0xC0, 0xE7, 0xFA, 0xA9, 0xB4, 0x93, 0x8E,
177 0xF8, 0xE5, 0xC2, 0xDF, 0x8C, 0x91, 0xB6, 0xAB,
178 0x10, 0x0D, 0x2A, 0x37, 0x64, 0x79, 0x5E, 0x43,
179 0xB2, 0xAF, 0x88, 0x95, 0xC6, 0xDB, 0xFC, 0xE1,
180 0x5A, 0x47, 0x60, 0x7D, 0x2E, 0x33, 0x14, 0x09,
181 0x7F, 0x62, 0x45, 0x58, 0x0B, 0x16, 0x31, 0x2C,
182 0x97, 0x8A, 0xAD, 0xB0, 0xE3, 0xFE, 0xD9, 0xC4
185 static uint8_t sbc_crc8(const uint8_t * data
, size_t len
)
191 for (i
= 0; i
< len
/ 8; i
++)
192 crc
= crc_table
[crc
^ data
[i
]];
195 for (i
= 0; i
< len
% 8; i
++)
197 char bit
= ((octet
^ crc
) & 0x80) >> 7;
199 crc
= ((crc
& 0x7f) << 1) ^ (bit
? 0x1d : 0);
208 * Code straight from the spec to calculate the bits array
209 * Takes a pointer to the frame in question, a pointer to the bits array and the sampling frequency (as 2 bit integer)
211 static void sbc_calculate_bits(const struct sbc_frame
*frame
, int (*bits
)[8], uint8_t sf
)
213 if (frame
->channel_mode
== MONO
|| frame
->channel_mode
== DUAL_CHANNEL
)
215 int bitneed
[2][8], loudness
, max_bitneed
, bitcount
, slicecount
, bitslice
;
218 for (ch
= 0; ch
< frame
->channels
; ch
++)
220 if (frame
->allocation_method
== SNR
)
222 for (sb
= 0; sb
< frame
->subbands
; sb
++)
224 bitneed
[ch
][sb
] = frame
->scale_factor
[ch
][sb
];
230 for (sb
= 0; sb
< frame
->subbands
; sb
++)
232 if (frame
->scale_factor
[ch
][sb
] == 0)
234 bitneed
[ch
][sb
] = -5;
238 if (frame
->subbands
== 4)
240 loudness
= frame
->scale_factor
[ch
][sb
] - sbc_offset4
[sf
][sb
];
245 loudness
= frame
->scale_factor
[ch
][sb
] - sbc_offset8
[sf
][sb
];
250 bitneed
[ch
][sb
] = loudness
/ 2;
255 bitneed
[ch
][sb
] = loudness
;
262 for (sb
= 0; sb
< frame
->subbands
; sb
++)
264 if (bitneed
[ch
][sb
] > max_bitneed
)
265 max_bitneed
= bitneed
[ch
][sb
];
270 bitslice
= max_bitneed
+ 1;
274 bitcount
+= slicecount
;
276 for (sb
= 0; sb
< frame
->subbands
; sb
++)
278 if ((bitneed
[ch
][sb
] > bitslice
+ 1) && (bitneed
[ch
][sb
] < bitslice
+ 16))
283 else if (bitneed
[ch
][sb
] == bitslice
+ 1)
288 } while (bitcount
+ slicecount
< frame
->bitpool
);
290 if (bitcount
+ slicecount
== frame
->bitpool
)
292 bitcount
+= slicecount
;
296 for (sb
= 0; sb
< frame
->subbands
; sb
++)
298 if (bitneed
[ch
][sb
] < bitslice
+ 2)
305 bits
[ch
][sb
] = bitneed
[ch
][sb
] - bitslice
;
306 if (bits
[ch
][sb
] > 16)
312 while (bitcount
< frame
->bitpool
&& sb
< frame
->subbands
)
314 if ((bits
[ch
][sb
] >= 2) && (bits
[ch
][sb
] < 16))
320 else if ((bitneed
[ch
][sb
] == bitslice
+ 1) && (frame
->bitpool
> bitcount
+ 1))
329 while (bitcount
< frame
->bitpool
&& sb
< frame
->subbands
)
331 if (bits
[ch
][sb
] < 16)
343 else if (frame
->channel_mode
== STEREO
|| frame
->channel_mode
== JOINT_STEREO
)
345 int bitneed
[2][8], loudness
, max_bitneed
, bitcount
, slicecount
, bitslice
;
348 if (frame
->allocation_method
== SNR
)
350 for (ch
= 0; ch
< 2; ch
++) {
351 for (sb
= 0; sb
< frame
->subbands
; sb
++)
353 bitneed
[ch
][sb
] = frame
->scale_factor
[ch
][sb
];
360 for (ch
= 0; ch
< 2; ch
++)
362 for (sb
= 0; sb
< frame
->subbands
; sb
++)
364 if (frame
->scale_factor
[ch
][sb
] == 0)
366 bitneed
[ch
][sb
] = -5;
371 if (frame
->subbands
== 4)
373 loudness
= frame
->scale_factor
[ch
][sb
] - sbc_offset4
[sf
][sb
];
378 loudness
= frame
->scale_factor
[ch
][sb
] - sbc_offset8
[sf
][sb
];
383 bitneed
[ch
][sb
] = loudness
/ 2;
388 bitneed
[ch
][sb
] = loudness
;
396 for (ch
= 0; ch
< 2; ch
++)
398 for (sb
= 0; sb
< frame
->subbands
; sb
++)
400 if (bitneed
[ch
][sb
] > max_bitneed
)
401 max_bitneed
= bitneed
[ch
][sb
];
407 bitslice
= max_bitneed
+ 1;
410 bitcount
+= slicecount
;
412 for (ch
= 0; ch
< 2; ch
++)
414 for (sb
= 0; sb
< frame
->subbands
; sb
++)
416 if ((bitneed
[ch
][sb
] > bitslice
+ 1) && (bitneed
[ch
][sb
] < bitslice
+ 16))
421 else if (bitneed
[ch
][sb
] == bitslice
+ 1)
427 } while (bitcount
+ slicecount
< frame
->bitpool
);
429 if (bitcount
+ slicecount
== frame
->bitpool
)
431 bitcount
+= slicecount
;
435 for (ch
= 0; ch
< 2; ch
++)
437 for (sb
= 0; sb
< frame
->subbands
; sb
++)
439 if (bitneed
[ch
][sb
] < bitslice
+ 2)
446 bits
[ch
][sb
] = bitneed
[ch
][sb
] - bitslice
;
447 if (bits
[ch
][sb
] > 16)
455 while ((bitcount
< frame
->bitpool
) && (sb
< frame
->subbands
))
457 if ((bits
[ch
][sb
] >= 2) && (bits
[ch
][sb
] < 16))
463 else if ((bitneed
[ch
][sb
] == bitslice
+ 1) && (frame
->bitpool
> bitcount
+ 1))
481 while ((bitcount
< frame
->bitpool
) && (sb
< frame
->subbands
))
483 if (bits
[ch
][sb
] < 16)
504 * Unpacks a SBC frame at the beginning of the stream in data,
505 * which has at most len bytes into frame.
506 * Returns the length in bytes of the packed frame, or a negative
507 * value on error. The error codes are:
509 * -1 Data stream too short
510 * -2 Sync byte incorrect
512 * -4 Bitpool value out of bounds
514 static int sbc_unpack_frame(const uint8_t * data
, struct sbc_frame
*frame
, size_t len
)
517 /* Will copy the parts of the header that are relevant to crc calculation here */
518 uint8_t crc_header
[11] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
522 uint8_t sf
; /* sampling_frequency, temporarily needed as array index */
525 int ch
, sb
, blk
, bit
; /* channel, subband, block and bit standard counters */
526 int bits
[2][8]; /* bits distribution */
527 int levels
[2][8]; /* levels derived from that */
531 //DbgPrint("Exit when len < 4\n");
535 if (data
[0] != SBC_SYNCWORD
)
537 //DbgPrint("Exit when data[0] != SBC_SYNCWORD\n");
542 sf
= (data
[1] >> 6) & 0x03;
545 frame
->sampling_frequency
= 16000;
548 frame
->sampling_frequency
= 32000;
551 frame
->sampling_frequency
= 44100;
554 frame
->sampling_frequency
= 48000;
558 switch ((data
[1] >> 4) & 0x03) {
573 frame
->channel_mode
= (data
[1] >> 2) & 0x03;
574 switch (frame
->channel_mode
) {
578 case DUAL_CHANNEL
: /* fall-through */
585 frame
->allocation_method
= (data
[1] >> 1) & 0x01;
586 frame
->subbands
= (data
[1] & 0x01) ? 8 : 4;
587 frame
->bitpool
= data
[2];
590 frame
->sampling_frequency
= 16000;
592 frame
->channel_mode
= MONO
;
594 frame
->allocation_method
= SBC_AM_LOUDNESS
;
599 if (((frame
->channel_mode
== MONO
|| frame
->channel_mode
== DUAL_CHANNEL
)
600 && frame
->bitpool
> 16 * frame
->subbands
)
601 || ((frame
->channel_mode
== STEREO
|| frame
->channel_mode
== JOINT_STEREO
)
602 && frame
->bitpool
> 32 * frame
->subbands
))
604 //DbgPrint("Exit when frame->mode == MONO || frame->mode == DUAL_CHANNEL) &&"
605 // "frame->bitpool > 16 * frame->subbands\n");
609 /* data[3] is crc, we're checking it later */
612 crc_header
[0] = data
[1];
613 crc_header
[1] = data
[2];
617 crc_header
[0] = 0x31;
618 crc_header
[1] = 0x1a;
623 if (frame
->channel_mode
== JOINT_STEREO
)
625 if (len
* 8 < consumed
+ frame
->subbands
)
629 for (sb
= 0; sb
< frame
->subbands
- 1; sb
++) {
630 frame
->join
|= ((data
[4] >> (7 - sb
)) & 0x01) << sb
;
632 if (frame
->subbands
== 4) {
633 crc_header
[crc_pos
/ 8] = data
[4] & 0xf0;
635 crc_header
[crc_pos
/ 8] = data
[4];
638 consumed
+= frame
->subbands
;
639 crc_pos
+= frame
->subbands
;
642 if (len
* 8 < consumed
+ (4 * frame
->subbands
* frame
->channels
))
644 //DbgPrint("len * 8 < consumed + (4 * frame->subbands * frame->channels\n");
648 for (ch
= 0; ch
< frame
->channels
; ch
++)
650 for (sb
= 0; sb
< frame
->subbands
; sb
++)
652 /* FIXME assert(consumed % 4 == 0); */
653 frame
->scale_factor
[ch
][sb
] = (data
[consumed
>> 3] >> (4 - (consumed
& 0x7))) & 0x0F;
654 crc_header
[crc_pos
>> 3] |= frame
->scale_factor
[ch
][sb
] << (4 - (crc_pos
& 0x7));
661 if (data
[3] != sbc_crc8(crc_header
, crc_pos
))
663 ALOGD("sbc_crc %02x -> %02x\n",data
[3],sbc_crc8(crc_header
, crc_pos
));
668 sbc_calculate_bits(frame
, bits
, sf
);
670 for (ch
= 0; ch
< frame
->channels
; ch
++)
672 for (sb
= 0; sb
< frame
->subbands
; sb
++)
674 levels
[ch
][sb
] = (1 << bits
[ch
][sb
]) - 1;
678 for (blk
= 0; blk
< frame
->blocks
; blk
++)
680 for (ch
= 0; ch
< frame
->channels
; ch
++)
682 for (sb
= 0; sb
< frame
->subbands
; sb
++)
684 if (levels
[ch
][sb
] > 0)
687 for (bit
= 0; bit
< bits
[ch
][sb
]; bit
++)
689 if (consumed
> len
* 8)
691 LogEvent("Exit when consumed > len * 8\n");
695 if ((data
[consumed
>> 3] >> (7 - (consumed
& 0x7))) & 0x01)
696 audio_sample
|= 1 << (bits
[ch
][sb
] - bit
- 1);
701 frame
->sb_sample
[blk
][ch
][sb
] =
702 (((audio_sample
<< 1) | 1) << frame
->scale_factor
[ch
][sb
]) /
703 levels
[ch
][sb
] - (1 << frame
->scale_factor
[ch
][sb
]);
707 frame
->sb_sample
[blk
][ch
][sb
] = 0;
715 for (blk
= 0; blk
< frame
->blocks
; blk
++)
717 for (ch
= 0; ch
< frame
->channels
; ch
++)
719 for (sb
= 0; sb
< frame
->subbands
; sb
++)
721 frame
->audio_sample
[blk
][ch
][sb
] = 0;
722 if (bits
[ch
][sb
] == 0)
725 for (bit
= 0; bit
< bits
[ch
][sb
]; bit
++)
728 if (consumed
> len
* 8)
731 b
= (data
[consumed
>> 3] >> (7 - (consumed
& 0x7))) & 0x01;
732 frame
->audio_sample
[blk
][ch
][sb
] |= b
<< (bits
[ch
][sb
] - bit
- 1);
740 for (blk
= 0; blk
< frame
->blocks
; blk
++)
742 for (ch
= 0; ch
< frame
->channels
; ch
++)
744 for (sb
= 0; sb
< frame
->subbands
; sb
++)
746 if (levels
[ch
][sb
] > 0)
748 frame
->sb_sample
[blk
][ch
][sb
] =
749 (((frame
->audio_sample
[blk
][ch
][sb
] << 16) | 0x8000) / levels
[ch
][sb
]) - 0x8000;
751 frame
->sb_sample
[blk
][ch
][sb
] >>= 3;
752 frame
->sb_sample
[blk
][ch
][sb
] = (frame
->sb_sample
[blk
][ch
][sb
] << (frame
->scale_factor
[ch
][sb
] + 1)); // Q13
756 frame
->sb_sample
[blk
][ch
][sb
] = 0;
763 if (frame
->channel_mode
== JOINT_STEREO
)
765 for (blk
= 0; blk
< frame
->blocks
; blk
++)
767 for (sb
= 0; sb
< frame
->subbands
; sb
++)
769 if (frame
->join
& (0x01 << sb
)) {
770 temp
= frame
->sb_sample
[blk
][0][sb
] + frame
->sb_sample
[blk
][1][sb
];
771 frame
->sb_sample
[blk
][1][sb
] = frame
->sb_sample
[blk
][0][sb
] - frame
->sb_sample
[blk
][1][sb
];
772 frame
->sb_sample
[blk
][0][sb
] = temp
;
778 if ((consumed
& 0x7) != 0)
779 consumed
+= 8 - (consumed
& 0x7);
782 return consumed
>> 3;
785 static void sbc_decoder_init(struct sbc_decoder_state
*state
, const struct sbc_frame
*frame
)
789 memset(state
->V
, 0, sizeof(state
->V
));
790 state
->subbands
= frame
->subbands
;
792 for (ch
= 0; ch
< 2; ch
++)
793 for (i
= 0; i
< frame
->subbands
* 2; i
++)
794 state
->offset
[ch
][i
] = (10 * i
+ 10);
797 static __inline
void sbc_synthesize_four(struct sbc_decoder_state
*state
,
798 struct sbc_frame
*frame
, int ch
, int blk
)
803 for(i
= 0; i
< 8; i
++) {
805 state
->offset
[ch
][i
]--;
806 if(state
->offset
[ch
][i
] < 0) {
807 state
->offset
[ch
][i
] = 79;
808 for(j
= 0; j
< 9; j
++) {
809 state
->V
[ch
][j
+80] = state
->V
[ch
][j
];
815 for(i
= 0; i
< 8; i
++) {
816 /* Distribute the new matrix value to the shifted position */
818 for (j
= 0; j
< 4; j
++) {
819 MULA(res
, synmatrix4
[i
][j
], frame
->sb_sample
[blk
][ch
][j
]);
821 state
->V
[ch
][state
->offset
[ch
][i
]] = SCALE4_STAGED1(res
);
824 /* Compute the samples */
825 for(idx
= 0, i
= 0; i
< 4; i
++) {
828 for(j
= 0; j
< 10; idx
++) {
829 MULA(res
, state
->V
[ch
][state
->offset
[ch
][i
]+j
++], sbc_proto_4_40m0
[idx
]);
830 MULA(res
, state
->V
[ch
][state
->offset
[ch
][k
]+j
++], sbc_proto_4_40m1
[idx
]);
832 /* Store in output */
833 frame
->pcm_sample
[ch
][blk
* 4 + i
] = (short)SCALE4_STAGED2(res
); // Q0
837 static __inline
void sbc_synthesize_eight(struct sbc_decoder_state
*state
,
838 struct sbc_frame
*frame
, int ch
, int blk
)
843 for(i
= 0; i
< 16; i
++) {
845 state
->offset
[ch
][i
]--;
846 if(state
->offset
[ch
][i
] < 0) {
847 state
->offset
[ch
][i
] = 159;
848 for(j
= 0; j
< 9; j
++) {
849 state
->V
[ch
][j
+160] = state
->V
[ch
][j
];
854 for(i
= 0; i
< 16; i
++) {
855 /* Distribute the new matrix value to the shifted position */
857 for (j
= 0; j
< 8; j
++) {
858 MULA(res
, synmatrix8
[i
][j
], frame
->sb_sample
[blk
][ch
][j
]); // Q28 = Q15 * Q13
860 state
->V
[ch
][state
->offset
[ch
][i
]] = SCALE8_STAGED1(res
); // Q10
864 /* Compute the samples */
865 for(idx
= 0, i
= 0; i
< 8; i
++) {
868 for(j
= 0; j
< 10; idx
++) {
869 MULA(res
, state
->V
[ch
][state
->offset
[ch
][i
]+j
++], sbc_proto_8_80m0
[idx
]);
870 MULA(res
, state
->V
[ch
][state
->offset
[ch
][k
]+j
++], sbc_proto_8_80m1
[idx
]);
872 /* Store in output */
873 frame
->pcm_sample
[ch
][blk
* 8 + i
] = (short)SCALE8_STAGED2(res
); // Q0
878 static int sbc_synthesize_audio(struct sbc_decoder_state
*state
, struct sbc_frame
*frame
)
882 switch (frame
->subbands
) {
884 for (ch
= 0; ch
< frame
->channels
; ch
++) {
885 for (blk
= 0; blk
< frame
->blocks
; blk
++)
886 sbc_synthesize_four(state
, frame
, ch
, blk
);
888 return frame
->blocks
* 4;
891 for (ch
= 0; ch
< frame
->channels
; ch
++) {
892 for (blk
= 0; blk
< frame
->blocks
; blk
++)
893 sbc_synthesize_eight(state
, frame
, ch
, blk
);
895 return frame
->blocks
* 8;
902 static void sbc_encoder_init(struct sbc_encoder_state
*state
, const struct sbc_frame
*frame
)
904 memset(&state
->X
, 0, sizeof(state
->X
));
905 state
->subbands
= frame
->subbands
;
908 static __inline
void _sbc_analyze_four(const int32_t *in
, int32_t *out
)
914 out
[0] = out
[1] = out
[2] = out
[3] = 0;
916 MUL(res
, _sbc_proto_4
[0], (in
[8] - in
[32])); // Q18
917 MULA(res
, _sbc_proto_4
[1], (in
[16] - in
[24]));
918 t
[0] = SCALE4_STAGE1(res
); // Q8
920 MUL(res
, _sbc_proto_4
[2], in
[1]);
921 MULA(res
, _sbc_proto_4
[3], in
[9]);
922 MULA(res
, _sbc_proto_4
[4], in
[17]);
923 MULA(res
, _sbc_proto_4
[5], in
[25]);
924 MULA(res
, _sbc_proto_4
[6], in
[33]);
925 t
[1] = SCALE4_STAGE1(res
);
927 MUL(res
, _sbc_proto_4
[7], in
[2]);
928 MULA(res
, _sbc_proto_4
[8], in
[10]);
929 MULA(res
, _sbc_proto_4
[9], in
[18]);
930 MULA(res
, _sbc_proto_4
[10], in
[26]);
931 MULA(res
, _sbc_proto_4
[11], in
[34]);
932 t
[2] = SCALE4_STAGE1(res
);
934 MUL(res
, _sbc_proto_4
[12], in
[3]);
935 MULA(res
, _sbc_proto_4
[13], in
[11]);
936 MULA(res
, _sbc_proto_4
[14], in
[19]);
937 MULA(res
, _sbc_proto_4
[15], in
[27]);
938 MULA(res
, _sbc_proto_4
[16], in
[35]);
939 t
[3] = SCALE4_STAGE1(res
);
941 MUL(res
, _sbc_proto_4
[17], in
[4]);
942 MULA(res
, _sbc_proto_4
[18], (in
[12] + in
[28]));
943 MULA(res
, _sbc_proto_4
[19], in
[20]);
944 MULA(res
, _sbc_proto_4
[17], in
[36]);
945 t
[4] = SCALE4_STAGE1(res
);
947 MUL(res
, _sbc_proto_4
[16], in
[5]);
948 MULA(res
, _sbc_proto_4
[15], in
[13]);
949 MULA(res
, _sbc_proto_4
[14], in
[21]);
950 MULA(res
, _sbc_proto_4
[13], in
[29]);
951 MULA(res
, _sbc_proto_4
[12], in
[37]);
952 t
[5] = SCALE4_STAGE1(res
);
954 MUL(res
, _sbc_proto_4
[11], in
[6]);
955 MULA(res
, _sbc_proto_4
[10], in
[14]);
956 MULA(res
, _sbc_proto_4
[9], in
[22]);
957 MULA(res
, _sbc_proto_4
[8], in
[30]);
958 MULA(res
, _sbc_proto_4
[7], in
[38]);
959 t
[6] = SCALE4_STAGE1(res
);
961 MUL(res
, _sbc_proto_4
[6], in
[7]);
962 MULA(res
, _sbc_proto_4
[5], in
[15]);
963 MULA(res
, _sbc_proto_4
[4], in
[23]);
964 MULA(res
, _sbc_proto_4
[3], in
[31]);
965 MULA(res
, _sbc_proto_4
[2], in
[39]);
966 t
[7] = SCALE4_STAGE1(res
);
968 MUL(res
, _anamatrix4
[0], t
[0]);
969 MULA(res
, _anamatrix4
[1], t
[1]);
970 MULA(res
, _anamatrix4
[2], t
[2]);
971 MULA(res
, _anamatrix4
[1], t
[3]);
972 MULA(res
, _anamatrix4
[0], t
[4]);
973 MULA(res
, _anamatrix4
[3], t
[5]);
974 MULA(res
, -_anamatrix4
[3], t
[7]);
975 out
[0] = (int)SCALE4_STAGE2(res
); // Q0
977 MUL(res
, -_anamatrix4
[0], t
[0]);
978 MULA(res
, _anamatrix4
[3], t
[1]);
979 MULA(res
, _anamatrix4
[2], t
[2]);
980 MULA(res
, _anamatrix4
[3], t
[3]);
981 MULA(res
, -_anamatrix4
[0], t
[4]);
982 MULA(res
, -_anamatrix4
[1], t
[5]);
983 MULA(res
, _anamatrix4
[1], t
[7]);
984 out
[1] = (int)SCALE4_STAGE2(res
);
987 MUL(res
, -_anamatrix4
[0], t
[0]);
988 MULA(res
, -_anamatrix4
[3], t
[1]);
989 MULA(res
, _anamatrix4
[2], t
[2]);
990 MULA(res
, -_anamatrix4
[3], t
[3]);
991 MULA(res
, -_anamatrix4
[0], t
[4]);
992 MULA(res
, _anamatrix4
[1], t
[5]);
993 MULA(res
, -_anamatrix4
[1], t
[7]);
994 out
[2] = (int)SCALE4_STAGE2(res
);
996 MUL(res
, _anamatrix4
[0], t
[0]);
997 MULA(res
, -_anamatrix4
[1], t
[1]);
998 MULA(res
, _anamatrix4
[2], t
[2]);
999 MULA(res
, -_anamatrix4
[1], t
[3]);
1000 MULA(res
, _anamatrix4
[0], t
[4]);
1001 MULA(res
, -_anamatrix4
[3], t
[5]);
1002 MULA(res
, _anamatrix4
[3], t
[7]);
1003 out
[3] = (int)SCALE4_STAGE2(res
);
1005 static __inline
void sbc_analyze_four(struct sbc_encoder_state
*state
,
1006 struct sbc_frame
*frame
, int ch
, int blk
)
1009 /* Input 4 New Audio Samples */
1010 for (i
= 39; i
>= 4; i
--)
1011 state
->X
[ch
][i
] = state
->X
[ch
][i
- 4];
1012 for (i
= 3; i
>= 0; i
--)
1013 state
->X
[ch
][i
] = frame
->pcm_sample
[ch
][blk
* 4 + (3 - i
)];
1014 _sbc_analyze_four(state
->X
[ch
], frame
->sb_sample_f
[blk
][ch
]);
1017 static __inline
void _sbc_analyze_eight(const int32_t *in
, int32_t *out
)
1020 sbc_extended_t t
[8];
1022 out
[0] = out
[1] = out
[2] = out
[3] = out
[4] = out
[5] = out
[6] = out
[7] = 0;
1024 MUL(res
, _sbc_proto_8
[0], (in
[16] - in
[64])); // Q18 = Q18 * Q0
1025 MULA(res
, _sbc_proto_8
[1], (in
[32] - in
[48]));
1026 MULA(res
, _sbc_proto_8
[2], in
[4]);
1027 MULA(res
, _sbc_proto_8
[3], in
[20]);
1028 MULA(res
, _sbc_proto_8
[4], in
[36]);
1029 MULA(res
, _sbc_proto_8
[5], in
[52]);
1030 t
[0] = SCALE8_STAGE1(res
); // Q10
1032 MUL(res
, _sbc_proto_8
[6], in
[2]);
1033 MULA(res
, _sbc_proto_8
[7], in
[18]);
1034 MULA(res
, _sbc_proto_8
[8], in
[34]);
1035 MULA(res
, _sbc_proto_8
[9], in
[50]);
1036 MULA(res
, _sbc_proto_8
[10], in
[66]);
1037 t
[1] = SCALE8_STAGE1(res
);
1039 MUL(res
, _sbc_proto_8
[11], in
[1]);
1040 MULA(res
, _sbc_proto_8
[12], in
[17]);
1041 MULA(res
, _sbc_proto_8
[13], in
[33]);
1042 MULA(res
, _sbc_proto_8
[14], in
[49]);
1043 MULA(res
, _sbc_proto_8
[15], in
[65]);
1044 MULA(res
, _sbc_proto_8
[16], in
[3]);
1045 MULA(res
, _sbc_proto_8
[17], in
[19]);
1046 MULA(res
, _sbc_proto_8
[18], in
[35]);
1047 MULA(res
, _sbc_proto_8
[19], in
[51]);
1048 MULA(res
, _sbc_proto_8
[20], in
[67]);
1049 t
[2] = SCALE8_STAGE1(res
);
1051 MUL(res
, _sbc_proto_8
[21], in
[5]);
1052 MULA(res
, _sbc_proto_8
[22], in
[21]);
1053 MULA(res
, _sbc_proto_8
[23], in
[37]);
1054 MULA(res
, _sbc_proto_8
[24], in
[53]);
1055 MULA(res
, _sbc_proto_8
[25], in
[69]);
1056 MULA(res
, -_sbc_proto_8
[15], in
[15]);
1057 MULA(res
, -_sbc_proto_8
[14], in
[31]);
1058 MULA(res
, -_sbc_proto_8
[13], in
[47]);
1059 MULA(res
, -_sbc_proto_8
[12], in
[63]);
1060 MULA(res
, -_sbc_proto_8
[11], in
[79]);
1061 t
[3] = SCALE8_STAGE1(res
);
1063 MUL(res
, _sbc_proto_8
[26], in
[6]);
1064 MULA(res
, _sbc_proto_8
[27], in
[22]);
1065 MULA(res
, _sbc_proto_8
[28], in
[38]);
1066 MULA(res
, _sbc_proto_8
[29], in
[54]);
1067 MULA(res
, _sbc_proto_8
[30], in
[70]);
1068 MULA(res
, -_sbc_proto_8
[10], in
[14]);
1069 MULA(res
, -_sbc_proto_8
[9], in
[30]);
1070 MULA(res
, -_sbc_proto_8
[8], in
[46]);
1071 MULA(res
, -_sbc_proto_8
[7], in
[62]);
1072 MULA(res
, -_sbc_proto_8
[6], in
[78]);
1073 t
[4] = SCALE8_STAGE1(res
);
1075 MUL(res
, _sbc_proto_8
[31], in
[7]);
1076 MULA(res
, _sbc_proto_8
[32], in
[23]);
1077 MULA(res
, _sbc_proto_8
[33], in
[39]);
1078 MULA(res
, _sbc_proto_8
[34], in
[55]);
1079 MULA(res
, _sbc_proto_8
[35], in
[71]);
1080 MULA(res
, -_sbc_proto_8
[20], in
[13]);
1081 MULA(res
, -_sbc_proto_8
[19], in
[29]);
1082 MULA(res
, -_sbc_proto_8
[18], in
[45]);
1083 MULA(res
, -_sbc_proto_8
[17], in
[61]);
1084 MULA(res
, -_sbc_proto_8
[16], in
[77]);
1085 t
[5] = SCALE8_STAGE1(res
);
1087 MUL(res
, _sbc_proto_8
[36], (in
[8] + in
[72]));
1088 MULA(res
, _sbc_proto_8
[37], in
[24]);
1089 MULA(res
, _sbc_proto_8
[38], in
[40]);
1090 MULA(res
, _sbc_proto_8
[37], in
[56]);
1091 MULA(res
, -_sbc_proto_8
[39], in
[12]);
1092 MULA(res
, -_sbc_proto_8
[5], in
[28]);
1093 MULA(res
, -_sbc_proto_8
[4], in
[44]);
1094 MULA(res
, -_sbc_proto_8
[3], in
[60]);
1095 MULA(res
, -_sbc_proto_8
[2], in
[76]);
1096 t
[6] = SCALE8_STAGE1(res
);
1098 MUL(res
, _sbc_proto_8
[35], in
[9]);
1099 MULA(res
, _sbc_proto_8
[34], in
[25]);
1100 MULA(res
, _sbc_proto_8
[33], in
[41]);
1101 MULA(res
, _sbc_proto_8
[32], in
[57]);
1102 MULA(res
, _sbc_proto_8
[31], in
[73]);
1103 MULA(res
, -_sbc_proto_8
[25], in
[11]);
1104 MULA(res
, -_sbc_proto_8
[24], in
[27]);
1105 MULA(res
, -_sbc_proto_8
[23], in
[43]);
1106 MULA(res
, -_sbc_proto_8
[22], in
[59]);
1107 MULA(res
, -_sbc_proto_8
[21], in
[75]);
1108 t
[7] = SCALE8_STAGE1(res
);
1110 MUL(res
, _anamatrix8
[0], t
[0]); // = Q14 * Q10
1111 MULA(res
, _anamatrix8
[7], t
[1]);
1112 MULA(res
, _anamatrix8
[2], t
[2]);
1113 MULA(res
, _anamatrix8
[3], t
[3]);
1114 MULA(res
, _anamatrix8
[6], t
[4]);
1115 MULA(res
, _anamatrix8
[4], t
[5]);
1116 MULA(res
, _anamatrix8
[1], t
[6]);
1117 MULA(res
, _anamatrix8
[5], t
[7]);
1118 out
[0] = (int)SCALE8_STAGE2(res
); // Q0
1120 MUL(res
, _anamatrix8
[1], t
[0]);
1121 MULA(res
, _anamatrix8
[7], t
[1]);
1122 MULA(res
, _anamatrix8
[3], t
[2]);
1123 MULA(res
, -_anamatrix8
[5], t
[3]);
1124 MULA(res
, -_anamatrix8
[6], t
[4]);
1125 MULA(res
, -_anamatrix8
[2], t
[5]);
1126 MULA(res
, -_anamatrix8
[0], t
[6]);
1127 MULA(res
, -_anamatrix8
[4], t
[7]);
1128 out
[1] = (int)SCALE8_STAGE2(res
);
1130 MUL(res
, -_anamatrix8
[1], t
[0]);
1131 MULA(res
, _anamatrix8
[7], t
[1]);
1132 MULA(res
, _anamatrix8
[4], t
[2]);
1133 MULA(res
, -_anamatrix8
[2], t
[3]);
1134 MULA(res
, -_anamatrix8
[6], t
[4]);
1135 MULA(res
, _anamatrix8
[5], t
[5]);
1136 MULA(res
, _anamatrix8
[0], t
[6]);
1137 MULA(res
, _anamatrix8
[3], t
[7]);
1138 out
[2] = (int)SCALE8_STAGE2(res
);
1140 MUL(res
, -_anamatrix8
[0], t
[0]);
1141 MULA(res
, _anamatrix8
[7], t
[1]);
1142 MULA(res
, _anamatrix8
[5], t
[2]);
1143 MULA(res
, -_anamatrix8
[4], t
[3]);
1144 MULA(res
, _anamatrix8
[6], t
[4]);
1145 MULA(res
, _anamatrix8
[3], t
[5]);
1146 MULA(res
, -_anamatrix8
[1], t
[6]);
1147 MULA(res
, -_anamatrix8
[2], t
[7]);
1148 out
[3] = (int)SCALE8_STAGE2(res
);
1150 MUL(res
, -_anamatrix8
[0], t
[0]);
1151 MULA(res
, _anamatrix8
[7], t
[1]);
1152 MULA(res
, -_anamatrix8
[5], t
[2]);
1153 MULA(res
, _anamatrix8
[4], t
[3]);
1154 MULA(res
, _anamatrix8
[6], t
[4]);
1155 MULA(res
, -_anamatrix8
[3], t
[5]);
1156 MULA(res
, -_anamatrix8
[1], t
[6]);
1157 MULA(res
, _anamatrix8
[2], t
[7]);
1158 out
[4] = (int)SCALE8_STAGE2(res
);
1160 MUL(res
, -_anamatrix8
[1], t
[0]);
1161 MULA(res
, _anamatrix8
[7], t
[1]);
1162 MULA(res
, -_anamatrix8
[4], t
[2]);
1163 MULA(res
, _anamatrix8
[2], t
[3]);
1164 MULA(res
, -_anamatrix8
[6], t
[4]);
1165 MULA(res
, -_anamatrix8
[5], t
[5]);
1166 MULA(res
, _anamatrix8
[0], t
[6]);
1167 MULA(res
, -_anamatrix8
[3], t
[7]);
1168 out
[5] = (int)SCALE8_STAGE2(res
);
1170 MUL(res
, _anamatrix8
[1], t
[0]);
1171 MULA(res
, _anamatrix8
[7], t
[1]);
1172 MULA(res
, -_anamatrix8
[3], t
[2]);
1173 MULA(res
, _anamatrix8
[5], t
[3]);
1174 MULA(res
, -_anamatrix8
[6], t
[4]);
1175 MULA(res
, _anamatrix8
[2], t
[5]);
1176 MULA(res
, -_anamatrix8
[0], t
[6]);
1177 MULA(res
, _anamatrix8
[4], t
[7]);
1178 out
[6] = (int)SCALE8_STAGE2(res
);
1180 MUL(res
, _anamatrix8
[0], t
[0]);
1181 MULA(res
, _anamatrix8
[7], t
[1]);
1182 MULA(res
, -_anamatrix8
[2], t
[2]);
1183 MULA(res
, -_anamatrix8
[3], t
[3]);
1184 MULA(res
, _anamatrix8
[6], t
[4]);
1185 MULA(res
, -_anamatrix8
[4], t
[5]);
1186 MULA(res
, _anamatrix8
[1], t
[6]);
1187 MULA(res
, -_anamatrix8
[5], t
[7]);
1188 out
[7] = (int)SCALE8_STAGE2(res
);
1191 static __inline
void sbc_analyze_eight(struct sbc_encoder_state
*state
,
1192 struct sbc_frame
*frame
, int ch
, int blk
)
1196 /* Input 8 Audio Samples */
1197 for (i
= 79; i
>= 8; i
--)
1198 state
->X
[ch
][i
] = state
->X
[ch
][i
- 8];
1199 for (i
= 7; i
>= 0; i
--)
1200 state
->X
[ch
][i
] = frame
->pcm_sample
[ch
][blk
* 8 + (7 - i
)];
1201 _sbc_analyze_eight(state
->X
[ch
], frame
->sb_sample_f
[blk
][ch
]);
1204 static int sbc_analyze_audio(struct sbc_encoder_state
*state
, struct sbc_frame
*frame
)
1208 switch (frame
->subbands
)
1211 for (ch
= 0; ch
< frame
->channels
; ch
++)
1212 for (blk
= 0; blk
< frame
->blocks
; blk
++) {
1213 sbc_analyze_four(state
, frame
, ch
, blk
);
1215 return frame
->blocks
* 4;
1218 for (ch
= 0; ch
< frame
->channels
; ch
++)
1219 for (blk
= 0; blk
< frame
->blocks
; blk
++) {
1220 sbc_analyze_eight(state
, frame
, ch
, blk
);
1222 return frame
->blocks
* 8;
1230 * Packs the SBC frame from frame into the memory at data. At most len
1231 * bytes will be used, should more memory be needed an appropriate
1232 * error code will be returned. Returns the length of the packed frame
1233 * on success or a negative value on error.
1235 * The error codes are:
1236 * -1 Not enough memory reserved
1237 * -2 Unsupported sampling rate
1238 * -3 Unsupported number of blocks
1239 * -4 Unsupported number of subbands
1240 * -5 Bitpool value out of bounds
1241 * -99 not implemented
1244 static int sbc_pack_frame(uint8_t * data
, struct sbc_frame
*frame
, size_t len
)
1247 /* Will copy the header parts for CRC-8 calculation here */
1248 uint8_t crc_header
[11] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
1251 uint8_t sf
; /* Sampling frequency as temporary value for table lookup */
1253 int ch
, sb
, blk
, bit
; /* channel, subband, block and bit counters */
1254 int bits
[2][8]; /* bits distribution */
1255 int levels
[2][8]; /* levels are derived from that */
1257 u_int32_t scalefactor
[2][8]; /* derived from frame->scale_factor */
1264 /* Clear first 4 bytes of data (that's the constant length part of the SBC header) */
1267 data
[0] = SBC_SYNCWORD
;
1269 if (frame
->sampling_frequency
== 16000)
1271 data
[1] |= (SBC_FS_16
& 0x03) << 6;
1274 else if (frame
->sampling_frequency
== 32000)
1276 data
[1] |= (SBC_FS_32
& 0x03) << 6;
1279 else if (frame
->sampling_frequency
== 44100)
1281 data
[1] |= (SBC_FS_44
& 0x03) << 6;
1284 else if (frame
->sampling_frequency
== 48000)
1286 data
[1] |= (SBC_FS_48
& 0x03) << 6;
1294 switch (frame
->blocks
)
1297 data
[1] |= (SBC_NB_4
& 0x03) << 4;
1300 data
[1] |= (SBC_NB_8
& 0x03) << 4;
1303 data
[1] |= (SBC_NB_12
& 0x03) << 4;
1306 data
[1] |= (SBC_NB_16
& 0x03) << 4;
1313 data
[1] |= (frame
->channel_mode
& 0x03) << 2;
1315 data
[1] |= (frame
->allocation_method
& 0x01) << 1;
1317 switch (frame
->subbands
) {
1329 data
[2] = frame
->bitpool
;
1330 if (((frame
->channel_mode
== MONO
|| frame
->channel_mode
== DUAL_CHANNEL
)
1331 && frame
->bitpool
> 16 * frame
->subbands
)
1332 || ((frame
->channel_mode
== STEREO
|| frame
->channel_mode
== JOINT_STEREO
)
1333 && frame
->bitpool
> 32 * frame
->subbands
)) {
1337 /* Can't fill in crc yet */
1345 crc_header
[0] = data
[1];
1346 crc_header
[1] = data
[2];
1350 for (ch
= 0; ch
< frame
->channels
; ch
++) {
1351 for (sb
= 0; sb
< frame
->subbands
; sb
++) {
1352 frame
->scale_factor
[ch
][sb
] = 0;
1353 scalefactor
[ch
][sb
] = 2;
1354 for (blk
= 0; blk
< frame
->blocks
; blk
++) {
1355 while (scalefactor
[ch
][sb
] < (u_int32_t
)fabs(frame
->sb_sample_f
[blk
][ch
][sb
])) {
1356 frame
->scale_factor
[ch
][sb
]++;
1357 scalefactor
[ch
][sb
] *= 2;
1363 if (frame
->channel_mode
== JOINT_STEREO
) {
1364 int32_t sb_sample_j
[16][2][7]; /* like frame->sb_sample but joint stereo */
1365 u_int32_t scalefactor_j
[2][7], scale_factor_j
[2][7]; /* scalefactor and scale_factor in joint case */
1367 /* Calculate joint stereo signal */
1368 for (sb
= 0; sb
< frame
->subbands
- 1; sb
++) {
1369 for (blk
= 0; blk
< frame
->blocks
; blk
++) {
1370 sb_sample_j
[blk
][0][sb
] = (frame
->sb_sample_f
[blk
][0][sb
] + frame
->sb_sample_f
[blk
][1][sb
]) >> 1;
1371 sb_sample_j
[blk
][1][sb
] = (frame
->sb_sample_f
[blk
][0][sb
] - frame
->sb_sample_f
[blk
][1][sb
]) >> 1;
1375 /* calculate scale_factor_j and scalefactor_j for joint case */
1376 for (ch
= 0; ch
< 2; ch
++) {
1377 for (sb
= 0; sb
< frame
->subbands
- 1; sb
++) {
1378 scale_factor_j
[ch
][sb
] = 0;
1379 scalefactor_j
[ch
][sb
] = 2;
1380 for (blk
= 0; blk
< frame
->blocks
; blk
++) {
1381 while (scalefactor_j
[ch
][sb
] < (u_int32_t
)fabs(sb_sample_j
[blk
][ch
][sb
])) {
1382 scale_factor_j
[ch
][sb
]++;
1383 scalefactor_j
[ch
][sb
] *= 2;
1389 /* decide which subbands to join */
1391 for (sb
= 0; sb
< frame
->subbands
- 1; sb
++) {
1392 if ((scalefactor
[0][sb
] + scalefactor
[1][sb
]) >
1393 (scalefactor_j
[0][sb
] + scalefactor_j
[1][sb
]) ) {
1394 /* use joint stereo for this subband */
1395 frame
->join
|= 1 << sb
;
1396 frame
->scale_factor
[0][sb
] = scale_factor_j
[0][sb
];
1397 frame
->scale_factor
[1][sb
] = scale_factor_j
[1][sb
];
1398 scalefactor
[0][sb
] = scalefactor_j
[0][sb
];
1399 scalefactor
[1][sb
] = scalefactor_j
[1][sb
];
1400 for (blk
= 0; blk
< frame
->blocks
; blk
++) {
1401 frame
->sb_sample_f
[blk
][0][sb
] = sb_sample_j
[blk
][0][sb
];
1402 frame
->sb_sample_f
[blk
][1][sb
] = sb_sample_j
[blk
][1][sb
];
1407 if (len
* 8 < produced
+ frame
->subbands
)
1411 for (sb
= 0; sb
< frame
->subbands
- 1; sb
++) {
1412 data
[4] |= ((frame
->join
>> sb
) & 0x01) << (7 - sb
);
1414 if (frame
->subbands
== 4) {
1415 crc_header
[crc_pos
/ 8] = data
[4] & 0xf0;
1417 crc_header
[crc_pos
/ 8] = data
[4];
1420 produced
+= frame
->subbands
;
1421 crc_pos
+= frame
->subbands
;
1424 if (len
* 8 < produced
+ (4 * frame
->subbands
* frame
->channels
))
1427 for (ch
= 0; ch
< frame
->channels
; ch
++) {
1428 for (sb
= 0; sb
< frame
->subbands
; sb
++) {
1429 if (produced
% 8 == 0)
1430 data
[produced
/ 8] = 0;
1431 data
[produced
/ 8] |= ((frame
->scale_factor
[ch
][sb
] & 0x0F) << (4 - (produced
% 8)));
1432 crc_header
[crc_pos
/ 8] |= ((frame
->scale_factor
[ch
][sb
] & 0x0F) << (4 - (crc_pos
% 8)));
1439 data
[3] = sbc_crc8(crc_header
, crc_pos
);
1443 sbc_calculate_bits(frame
, bits
, sf
);
1445 for (ch
= 0; ch
< frame
->channels
; ch
++) {
1446 for (sb
= 0; sb
< frame
->subbands
; sb
++) {
1447 levels
[ch
][sb
] = (1 << bits
[ch
][sb
]) - 1;
1451 for (blk
= 0; blk
< frame
->blocks
; blk
++) {
1452 for (ch
= 0; ch
< frame
->channels
; ch
++) {
1453 for (sb
= 0; sb
< frame
->subbands
; sb
++) {
1454 if (levels
[ch
][sb
] > 0) {
1455 frame
->audio_sample
[blk
][ch
][sb
] =
1456 (uint16_t) ((((frame
->sb_sample_f
[blk
][ch
][sb
]*levels
[ch
][sb
]) >> (frame
->scale_factor
[ch
][sb
] + 1)) +
1457 levels
[ch
][sb
]) >> 1);
1459 frame
->audio_sample
[blk
][ch
][sb
] = 0;
1465 for (blk
= 0; blk
< frame
->blocks
; blk
++) {
1466 for (ch
= 0; ch
< frame
->channels
; ch
++) {
1467 for (sb
= 0; sb
< frame
->subbands
; sb
++) {
1468 if (bits
[ch
][sb
] != 0) {
1469 for (bit
= 0; bit
< bits
[ch
][sb
]; bit
++) {
1471 if (produced
> len
* 8) {
1474 if (produced
% 8 == 0) {
1475 data
[produced
/ 8] = 0;
1477 b
= ((frame
->audio_sample
[blk
][ch
][sb
]) >> (bits
[ch
][sb
] - bit
-
1479 data
[produced
/ 8] |= b
<< (7 - (produced
% 8));
1487 if (produced
% 8 != 0) {
1488 produced
+= 8 - (produced
% 8);
1491 return (int)(produced
/ 8);
1496 struct sbc_frame frame
;
1497 struct sbc_decoder_state dec_state
;
1498 struct sbc_encoder_state enc_state
;
1501 int sbc_init(sbc_t
*sbc
)//int sbc_init(sbc_t *sbc, unsigned long flags)
1508 memset(sbc
, 0, sizeof(sbc_t
));
1510 sbc
->priv
= malloc(sizeof(struct sbc_priv
) + SBC_ALIGN_MASK
);
1513 memset(sbc
->priv
, 0, sizeof(struct sbc_priv
));
1525 int sbc_reinit(sbc_t
*sbc
)//int sbc_reinit(sbc_t *sbc, unsigned long flags)
1527 struct sbc_priv
*priv
;
1529 if (!sbc
|| !sbc
->priv
)
1535 if (priv
->init
== 1)
1536 memset(sbc
->priv
, 0, sizeof(struct sbc_priv
));
1542 int sbc_decode(sbc_t
*sbc
, void *input
, int input_len
, void *output
,
1543 int output_len
, int *written
)
1545 struct sbc_priv
*priv
;
1547 int i
, ch
, framelen
, samples
;
1551 //DbgPrint("Exit.when sbc is NULL.\n");
1557 //DbgPrint("!sbc && !input\n");
1565 framelen
= sbc_unpack_frame(input
, &priv
->frame
, input_len
);
1569 sbc_decoder_init(&priv
->dec_state
, &priv
->frame
);
1572 sbc
->rate
= priv
->frame
.sampling_frequency
;
1573 sbc
->channels
= priv
->frame
.channels
;
1574 sbc
->subbands
= priv
->frame
.subbands
;
1575 sbc
->blocks
= priv
->frame
.blocks
;
1576 sbc
->bitpool
= priv
->frame
.bitpool
;
1581 //DbgPrint("!output\n");
1590 //DbgPrint("Exit when framelen <= 0\n");
1594 samples
= sbc_synthesize_audio(&priv
->dec_state
, &priv
->frame
);
1597 if (output_len
< samples
* priv
->frame
.channels
* 2)
1598 samples
= output_len
/ (priv
->frame
.channels
* 2);
1603 sbc->size = samples * priv->frame.channels * 2;
1604 sbc->data = malloc(sbc->size);
1608 if (sbc->size < samples * priv->frame.channels * 2)
1610 sbc->size = samples * priv->frame.channels * 2;
1611 sbc->data = realloc(sbc->data, sbc->size);
1621 for (i
= 0; i
< samples
; i
++)
1623 for (ch
= 0; ch
< priv
->frame
.channels
; ch
++)
1626 s
= priv
->frame
.pcm_sample
[ch
][i
];
1628 *ptr++ = (s & 0xff00) >> 8;
1629 *ptr++ = (s & 0x00ff);
1631 *ptr
++ = (s
& 0x00ff);
1632 *ptr
++ = (s
& 0xff00) >> 8;
1637 *written
= samples
* priv
->frame
.channels
* 2;
1642 int sbc_encode(sbc_t
*sbc
, void *input
, int input_len
, void *output
,
1643 int output_len
, int *written
)
1645 struct sbc_priv
*priv
;
1647 int i
, ch
, framelen
, samples
;
1651 //DbgPrint("Exit, when !sbc.\n");
1658 //DbgPrint("Exit, when !sbc && !input.\n");
1662 /// make sure sbc has been initialized
1665 //DbgPrint("priv == NULL\n");
1674 //DbgPrint("Initial priv->frame ,when priv->init is null\n");
1675 priv
->frame
.sampling_frequency
= sbc
->rate
;
1676 priv
->frame
.channels
= sbc
->channels
;
1677 priv
->frame
.channel_mode
= MONO
;
1678 priv
->frame
.allocation_method
= LOUDNESS
;
1679 priv
->frame
.subbands
= sbc
->subbands
;
1680 priv
->frame
.blocks
= sbc
->blocks
;
1681 priv
->frame
.bitpool
= sbc
->bitpool
;
1683 sbc_encoder_init(&priv
->enc_state
, &priv
->frame
);
1687 /* input must be large enough to encode a complete frame */
1688 if (input_len
< 240)
1690 //DbgPrint("Exit, when input_len < priv->frame.codesize..\n");
1694 /* output must be large enough to receive the encoded frame */
1695 if (!output
|| output_len
< 57)
1697 //DbgPrint("Exit, when !output || output_len < priv->frame.length\n");
1702 for (i
= 0; i
< priv
->frame
.subbands
* priv
->frame
.blocks
; i
++)
1704 for (ch
= 0; ch
< sbc
->channels
; ch
++)
1706 // int16_t s = (ptr[0] & 0xff) << 8 | (ptr[1] & 0xff);
1707 int16_t s
= (ptr
[1] & 0xff) << 8 | (ptr
[0] & 0xff);
1709 priv
->frame
.pcm_sample
[ch
][i
] = s
;
1714 samples
= sbc_analyze_audio(&priv
->enc_state
, &priv
->frame
);
1719 // sbc->data = malloc(sbc->size);
1720 sbc
->data
= malloc(sbc
->size
);
1723 //DbgPrint("sbc->data allocate failed!!!\n");
1726 memset(sbc
->data
, 0, sbc
->size
);
1732 //DbgPrint("sbc->data is null, so exit!!!\n");
1736 framelen
= sbc_pack_frame(output
, &priv
->frame
, output_len
);
1739 *written
= (int)framelen
;//in 64 bit os, it should be okay.
1742 sbc
->len
= framelen
;
1743 sbc
->duration
= (1000000 * priv
->frame
.subbands
* priv
->frame
.blocks
) / sbc
->rate
;
1745 return samples
* sbc
->channels
* 2;
1749 void sbc_finish(sbc_t
*sbc
)
1762 memset(sbc
, 0, sizeof(sbc_t
));