在音频流解析过程中,常常会涉及到顺序读取某些bit的操作。
#include<stdio.h>
typedef struct _BIT_BUF {
unsigned char buffer[1024];
unsigned int byteSize;
unsigned int bytePosition;
unsigned int bitPosition;
unsigned int wBytePosition;
unsigned int wBitPosition;
}tBitBuffer;
//mask:baoliu保留低bit位,可用while 循环0x1右移bit-1 位。
unsigned int mask(unsigned int bit){
switch (bit) {
case 1:
return 0x1;
break;
case 2:
return 0x3;
break;
case 3:
return 0x7;
break;
case 4:
return 0xf;
break;
case 5:
return 0x1f;
break;
case 6:
return 0x3f;
break;
case 7:
return 0x7f;
break;
case 8:
return 0xff;
break;
case 9:
return 0x1ff;
break;
case 10:
return 0x3ff;
break;
case 11:
return 0x7ff;
break;
case 12:
return 0xfff;
break;
case 13:
return 0x1fff;
break;
case 14:
return 0x3fff;
break;
case 15:
return 0x7fff;
break;
case 16:
return 0xffff;
break;
}
}
//顺序读取bitbuffer 中的bits位,返回结果
unsigned long bitBuffer_readBits(tBitBuffer *pBitBuffer, unsigned int bits) {
unsigned long result = 0;
if (bits > 64) {
return 0;
}
if (pBitBuffer->bytePosition * 8 + (pBitBuffer->bitPosition + bits ) >pBitBuffer->wBytePosition * 8 + pBitBuffer->wBitPosition) {
printf("L%d, error,no data,bytePos:%d, bitPos:%d, wBytePos: %d, wBitPos:%d
",__LINE__, pBitBuffer->bytePosition, pBitBuffer->bitPosition, pBitBuffer->wBytePosition, pBitBuffer->wBitPosition);
return 0;
}
unsigned int bitsRemainning = 8 - pBitBuffer->bitPosition;
while (bits != 0) {
printf("bits:%d,bitPositon:%d, bitsRemainning:%d
", bits, pBitBuffer->bitPosition, bitsRemainning);
if (bitsRemainning > bits) {
result <<= bits;
pBitBuffer->bitPosition += bits;
bitsRemainning -= bits;
result |= (pBitBuffer->buffer[pBitBuffer->bytePosition] >> bitsRemainning) & mask(bits);
printf("result:0x%x
",result);
bits = 0;
}
else {
result <<= bitsRemainning;
result |= pBitBuffer->buffer[pBitBuffer->bytePosition] & mask(bitsRemainning);
printf("result:0x%x
",result);
bits -= bitsRemainning;
pBitBuffer->bitPosition += bitsRemainning;
}
if (pBitBuffer->bitPosition == 8) {
pBitBuffer->bitPosition = 0;
bitsRemainning = 8;
pBitBuffer->bytePosition++;
}
}
return result;
}
//顺序向bitBuffer写bits位,其值为value
int bitBuffer_writeBits(tBitBuffer *pBitBuffer,unsigned int bits,unsigned int value) {
unsigned int bitsRemainning = 8 - pBitBuffer->wBitPosition;
while (bits != 0) {
printf("bits:%d,bitPositon:%d, bitsRemainning:%d
", bits, pBitBuffer->wBitPosition, bitsRemainning);
if (bitsRemainning >= bits) {
value <<= bitsRemainning - bits;
pBitBuffer->buffer[pBitBuffer->wBytePosition] |= value;
pBitBuffer->wBitPosition += bits;
bitsRemainning -= bits;
bits = 0;
printf("L%d,value:0x%x, bits:%d, buffer[%d, 0x%x]
",__LINE__, value, bits, pBitBuffer->wBytePosition, pBitBuffer->buffer[pBitBuffer->wBytePosition]);
}
else {
pBitBuffer->buffer[pBitBuffer->wBytePosition] <<= bitsRemainning;
pBitBuffer->buffer[pBitBuffer->wBytePosition] |= value >>(bits - bitsRemainning);
value = value & mask(bits-bitsRemainning);
bits -= bitsRemainning;
pBitBuffer->wBitPosition += bitsRemainning;
printf("L%d,value:%d, bits:%d, buffer[%d, 0x%x]
",__LINE__, value, bits, pBitBuffer->wBytePosition, pBitBuffer->buffer[pBitBuffer->wBytePosition]);
}
if (pBitBuffer->wBitPosition == 8) {
pBitBuffer->wBitPosition = 0;
bitsRemainning = 8;
pBitBuffer->wBytePosition++;
}
}
printf("L%d,wBitPos:%d, wBytePos:%d
",__LINE__, pBitBuffer->wBitPosition, pBitBuffer->wBytePosition);
return 0;
}
//左移或右移bits位
int bitBuffer_shiftBits(tBitBuffer *pBitBuffer, int bits) {
int absBits = bits > 0 ? bits : -bits;
int shiftBytes = absBits / 8;
int shiftBits = absBits % 8;
int flag = bits > 0 ? 1 : 0;
if ( flag ) {
if (pBitBuffer->bitPosition + shiftBits > 8) {
pBitBuffer->bytePosition += shiftBytes + 1;
pBitBuffer->bitPosition += shiftBits -8;
}
else {
pBitBuffer->bytePosition += shiftBytes ;
pBitBuffer->bitPosition += shiftBits;
}
}
else {
if (pBitBuffer->bitPosition - shiftBits < 0) {
pBitBuffer->bytePosition -= shiftBytes -1;
pBitBuffer->bitPosition -= shiftBits -8;
}
else {
pBitBuffer->bytePosition = shiftBytes ;
pBitBuffer->bitPosition -= shiftBits ;
}
}
if (pBitBuffer->bytePosition < 0 || pBitBuffer->bytePosition > 1024) {
pBitBuffer->bytePosition = 0;
pBitBuffer->bitPosition = 0;
return -1;
}
else{
return 0;
}
}
//读取了多少位
int bitBuffer_getReadBits(tBitBuffer *pBitBuffer) {
return 8 * pBitBuffer->bytePosition + pBitBuffer->bitPosition;
}
int bitBuffer_writeByte(tBitBuffer *pBitBuffer, unsigned char byte){
pBitBuffer->buffer[pBitBuffer->wBytePosition ++] = byte;
pBitBuffer->byteSize ++;
}
void main(void) {
tBitBuffer *bitBuffer = (tBitBuffer*)malloc(sizeof(tBitBuffer));
memset(bitBuffer, 0, sizeof(tBitBuffer));
// bitBuffer_writeByte(bitBuffer, 0x56);
// bitBuffer_writeByte(bitBuffer, 0xe2);
bitBuffer_writeBits(bitBuffer, 11, 0x2b7);
bitBuffer_writeBits(bitBuffer, 5, 0x2);
unsigned long result = 0;
bitBuffer_shiftBits(bitBuffer, 3);
result = bitBuffer_readBits(bitBuffer, 6);
int readBits = bitBuffer_getReadBits(bitBuffer);
printf("result:0x%x, %d, bitPostion:%d, bytePosition:%d,byteSize:%d, wBytePosition:%d, readBits:%d
", result,result, bitBuffer->bitPosition, bitBuffer->bytePosition, bitBuffer->byteSize, bitBuffer->wBytePosition,readBits );
#if 0
result = bitBuffer_readBits(&bitBuffer, 3);
printf("result:0x%x, %d, bitPostion:%d, bytePosition:%d,byteSize:%d, wBytePosition:%d
", result,result, bitBuffer.bitPosition, bitBuffer.bytePosition, bitBuffer.byteSize, bitBuffer.wBytePosition);
result = bitBuffer_readBits(&bitBuffer, 6);
printf("result:0x%x, %d, bitPostion:%d, bytePosition:%d,byteSize:%d, wBytePosition:%d
", result,result, bitBuffer.bitPosition, bitBuffer.bytePosition, bitBuffer.byteSize, bitBuffer.wBytePosition);
#endif
}