有没有同学了解SBC编解码原理,比如参数设置中 block length中的具体含义和作用,以及APCM算法原理等,万分感谢!
PCM格式的左右声道进入多相解析器,输出尺度因子和子带采样数据
每一个尺度因子分别对应一个子带
量化后的子带采样数据须要进行打包,打包方式能够是分段或不分段
这里写图片描述
多相解析器的代码实现较为复杂,流程图以下[具体请看参考文献2的Appendix B]:
这里写图片描述
2.2 SBC解码算法实现
这里写图片描述
解码过程是编码过程的逆
多相综合器的代码实现较为复杂,流程图以下[具体请看参考文献2的Appendix B]:
这里写图片描述
3 SBC解码算法在某蓝牙主设备上的应用
3.1 帧格式
这里写图片描述
3.2 参数选择
sampling frequency:16
channel mode:单声道
number of subbands:8
number of channels:1
number of blocks:15
allocation method:SNR
bitpool:26
3.3 代码示例
void SBC_Decode(uint8_t * DataIn, FILE * fOutput)
{
#define SBC_SAMPLING_FREQ 16
#define SBC_CHANNEL_MODE 0
#define SBC_NUM_OF_SUBBANDS 8
#define SBC_NUM_OF_CHANNELS 1
#define SBC_NUM_OF_BLOCKS 15
#define SBC_ALLOC_METHOD 0
#define SBC_BITPOOL 26
#define SBC_DECODED_BUFFER_SIZE (16*8)
uint8_t blocks_per_packet = SBC_NUM_OF_BLOCKS;
uint8_t num_bits = SBC_BITPOOL;
const uint8_t * buf = (DataIn+1);//ignore CRC byte
uint16_t len = SBC_GROUP_SIZE;
uint16_t usDecodedBuffer[SBC_DECODED_BUFFER_SIZE];
/* convenience */
const uint8_t * end = buf + len;
#define left (end - buf)
uint16_t * outBufPtr = usDecodedBuffer;
/* workspace */
static INSAMPLE samples[16][8]; /* We blow the stack if this is not static. */
ITER i, j, k;
uint32_t scaleFactors[8]; //= {0x0f, 0x0c, 0x0b, 0x0b, 0x0a, 0x0a, 0x09, 0x09};
int32_t bitneed[8];
uint32_t bits[8];
int32_t bitcount, slicecount, bitslice;
uint8_t samplingRate, blocks, snr, numSubbands, bitpoolSz, bitpos = 0x80;
int8_t max_bitneed = 0;
#ifndef SPEED_OVER_ACCURACY
int32_t levels[8];
#endif
#if (DEBUG_DECODING == 1)
const uint8_t *start_buf = buf;
pr_info("%s: blocks_per_packet = %d, num_bits = %d, buf = %p, len = %d\n",
func, blocks_per_packet, num_bits, buf, len);
for (i = 0; i < len; i++) {
pr_info("buf[%d] = 0x%02x\n", i, buf[i]);
}
#endif
/* look into the frame header */
if (left < SBC_GROUP_SIZE) goto out;/* too short a frame header */
/* use Bemote specific constants */
samplingRate = 0; /* always 16000 Hz */
blocks = blocks_per_packet;
snr = 0;
numSubbands = SBC_NUM_OF_SUBBANDS;
bitpoolSz = num_bits;
/* read scale factors */
/* pr_info("sbc_decode: read scale factors, numSubbands = %d\n", numSubbands); */
/**/
for(i = 0; i < numSubbands; i++){
if(bitpos == 0x80){
scaleFactors[i] = (*buf) >> 4;
bitpos = 0x08;
}
else{
scaleFactors[i] = (*buf++) & 0x0F;
bitpos = 0x80;
}
}
/* calculate bitneed table and max_bitneed value (A2DP 12.6.3.1) */
if(snr){
for(i = 0; i < numSubbands; i++){
bitneed[i] = scaleFactors[i];
if(bitneed[i] > max_bitneed) max_bitneed = bitneed[i];
}
}
else{
const signed char* tbl;
if(numSubbands == 4) tbl = (const signed char*)loudness_4[samplingRate];
else tbl = (const signed char*)loudness_8[samplingRate];
for(i = 0; i < numSubbands; i++){
if(scaleFactors[i]){
int loudness = scaleFactors[i] - tbl[i];
if(loudness > 0) loudness /= 2;
bitneed[i] = loudness;
}
else bitneed[i] = -5;
if(bitneed[i] > max_bitneed) max_bitneed = bitneed[i];
}
}
/* fit bitslices into the bitpool */
bitcount = 0;
slicecount = 0;
bitslice = max_bitneed + 1;
/* pr_info("sbc_decode: fit bitslices into the bitpool, bitslice = %d\n", bitslice ); */
do{
bitslice--;
bitcount += slicecount;
slicecount = 0;
for(i = 0; i < numSubbands; i++){
if(bitneed[i] > bitslice + 1 && bitneed[i] < bitslice + 16) slicecount++;
else if(bitneed[i] == bitslice + 1) slicecount += 2;
}
}while(bitcount + slicecount < bitpoolSz);
/* distribute bits */
for(i = 0; i < numSubbands; i++){
if(bitneed[i] < bitslice + 2) bits[i] = 0;
else{
int8_t v = bitneed[i] - bitslice;
if(v > 16) v = 16;
bits[i] = v;
}
}
/* allocate remaining bits */
for(i = 0; i < numSubbands && bitcount < bitpoolSz; i++){
if(bits[i] >= 2 && bits[i] < 16){
bits[i]++;
bitcount++;
}
else if(bitneed[i] == bitslice + 1 && bitpoolSz > bitcount + 1){
bits[i] = 2;
bitcount += 2;
}
}
for(i = 0; i < numSubbands && bitcount < bitpoolSz; i++){
if(bits[i] < 16){
bits[i]++;
bitcount++;
}
}
/* reconstruct subband samples (A2DP 12.6.4) */
#ifndef SPEED_OVER_ACCURACY
for(i = 0; i < numSubbands; i++) levels[i] = (1 << bits[i]) - 1;
#endif
/* pr_info("sbc_decode: reconstruct subband samples, blocks = %d\n", blocks ); */
for(j = 0; j < blocks; j++){
for(i = 0; i < numSubbands; i++){
if(bits[i]){
uint32_t val = 0;
k = bits[i];
do{
val <<= 1;
#if (DEBUG_DECODING == 1)
pr_info("%s: buf = %p, offset %d\n",
func, buf, buf-start_buf);
#endif
if(*buf & bitpos) val++;
if(!(bitpos >>= 1)){
bitpos = 0x80;
buf++;
}
}while(--k);
val = (val << 1) | 1;
val <<= scaleFactors[i];
#ifdef SPEED_OVER_ACCURACY
val = mulshift(val, bits[i]);
#else
val /= levels[i];
#endif
val -= (1 << scaleFactors[i]);
samples[j][i] = SAMPLE_CVT(val);
}
else samples[j][i] = SAMPLE_CVT(0);
}
}
//sbc_decoder_reset();
for(j = 0; j < blocks; j++){
synth(outBufPtr, samples[j], numSubbands, gV);
outBufPtr += numSubbands;
}
/* if we used a byte partially, skip the rest of it, it is "padding" */
if(bitpos != 0x80) buf++;
out:
#if (DEBUG_DECODING == 1)
if(left < 0)
pr_err("SBC: buffer over-read by %d bytes.\n", -left);
if(left > 0)
pr_err("SBC: buffer under-read by %d bytes.\n", left);
#endif
fwrite(usDecodedBuffer, sizeof(uint16_t), 120, fOutput);
fflush(fOutput);
memset(usDecodedBuffer, 0, sizeof(usDecodedBuffer));
}
4 总结
在章节3中给出的例子中,压缩前的数据有1202=240Byte,压缩后的数据有54Byte,压缩比接近4.4:1,压缩比是可调的,其与编码参数有关
压缩后的帧数据长度能够由编码参数计算出来。此例中,
帧长度=4+(4 * 子带数量 * 通道数量)/8+(块数量 * 通道数量 * bitpool)/8
=4+(481)/8+(151*26)/8=8+48.75=57(加上CRC正好58字节)
若是在传输过程当中丢失了个别帧,解压后回放时会出现一小段音频的总体丢失,对总体的解压没有影响