📄 bitstream.c
字号:
bs->buf_byte_idx += N>>3;
if ( (tmp + bs->buf_bit_idx) <= 8) {
bs->buf_bit_idx += tmp;
}
else {
bs->buf_byte_idx ++;
bs->buf_bit_idx += (tmp - 8);
}
bs->buf[bs->buf_byte_idx] &= clearmask[bs->buf_bit_idx];
}
int mask[8]={0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80};
/*read 1 bit from the bit stream */
unsigned int get1bit(bs)
bitstream_t *bs; /* bit stream structure */
{
unsigned int bit;
register int i;
bs->totbit++;
if (!bs->buf_bit_idx) {
bs->buf_bit_idx = 8;
bs->buf_byte_idx--;
if ((bs->buf_byte_idx < MINIMUM) || (bs->buf_byte_idx < bs->eob)) {
if (bs->eob)
bs->eobs = TRUE;
else {
for (i=bs->buf_byte_idx; i>=0;i--)
bs->buf[bs->buf_size-1-bs->buf_byte_idx+i] = bs->buf[i];
refill_buffer(bs);
bs->buf_byte_idx = bs->buf_size-1;
}
}
}
bit = bs->buf[bs->buf_byte_idx]&mask[bs->buf_bit_idx-1];
bit = bit >> (bs->buf_bit_idx-1);
bs->buf_bit_idx--;
return(bit);
}
/*write 1 bit from the bit stream */
void put1bit(bs, bit)
bitstream_t *bs; /* bit stream structure */
int bit; /* bit to write into the buffer */
{
bs->totbit++;
bs->buf[bs->buf_byte_idx] |= (bit&0x1) << (bs->buf_bit_idx-1);
bs->buf_bit_idx--;
if (!bs->buf_bit_idx) {
bs->buf_bit_idx = 8;
bs->buf_byte_idx--;
if (bs->buf_byte_idx < 0)
empty_buffer(bs, MINIMUM);
bs->buf[bs->buf_byte_idx] = 0;
}
}
/*look ahead for the next N bits from the bit stream */
unsigned long look_ahead(bs, N)
bitstream_t *bs; /* bit stream structure */
int N; /* number of bits to read from the bit stream */
{
unsigned long val=0;
register int j = N;
register int k, tmp;
register int bit_idx = bs->buf_bit_idx;
register int byte_idx = bs->buf_byte_idx;
if (N > MAX_LENGTH)
printf("Cannot read or write more than %d bits at a time.\n", MAX_LENGTH);
while (j > 0) {
if (!bit_idx) {
bit_idx = 8;
byte_idx--;
}
k = MIN (j, bit_idx);
tmp = bs->buf[byte_idx]&putmask[bit_idx];
tmp = tmp >> (bit_idx-k);
val |= tmp << (j-k);
bit_idx -= k;
j -= k;
}
return(val);
}
/*read N bit from the bit stream */
unsigned long getbits(bs, N)
bitstream_t *bs; /* bit stream structure */
int N; /* number of bits to read from the bit stream */
{
unsigned long val=0;
register int i;
register int j = N;
register int k, tmp;
if (N > MAX_LENGTH)
printf("Cannot read or write more than %d bits at a time.\n", MAX_LENGTH);
bs->totbit += N;
while (j > 0) {
if (!bs->buf_bit_idx) {
bs->buf_bit_idx = 8;
bs->buf_byte_idx--;
if ((bs->buf_byte_idx < MINIMUM) || (bs->buf_byte_idx < bs->eob)) {
if (bs->eob)
bs->eobs = TRUE;
else {
for (i=bs->buf_byte_idx; i>=0;i--)
bs->buf[bs->buf_size-1-bs->buf_byte_idx+i] = bs->buf[i];
refill_buffer(bs);
bs->buf_byte_idx = bs->buf_size-1;
}
}
}
k = MIN (j, bs->buf_bit_idx);
tmp = bs->buf[bs->buf_byte_idx]&putmask[bs->buf_bit_idx];
tmp = tmp >> (bs->buf_bit_idx-k);
val |= tmp << (j-k);
bs->buf_bit_idx -= k;
j -= k;
}
return(val);
}
/*write N bits into the bit stream */
void putbits(bs, val, N)
bitstream_t *bs; /* bit stream structure */
unsigned int val; /* val to write into the buffer */
int N; /* number of bits of val */
{
register int j = N;
register int k, tmp;
if (N > MAX_LENGTH)
printf("Cannot read or write more than %d bits at a time.\n", MAX_LENGTH);
bs->totbit += N;
while (j > 0) {
k = MIN(j, bs->buf_bit_idx);
tmp = val >> (j-k);
bs->buf[bs->buf_byte_idx] |= (tmp&putmask[k]) << (bs->buf_bit_idx-k);
bs->buf_bit_idx -= k;
if (!bs->buf_bit_idx) {
bs->buf_bit_idx = 8;
bs->buf_byte_idx--;
if (bs->buf_byte_idx < 0)
empty_buffer(bs, MINIMUM);
bs->buf[bs->buf_byte_idx] = 0;
}
j -= k;
}
}
/*write N bits byte aligned into the bit stream */
void byte_ali_putbits(bs, val, N)
bitstream_t *bs; /* bit stream structure */
unsigned int val; /* val to write into the buffer */
int N; /* number of bits of val */
{
unsigned long aligning, sstell();
if (N > MAX_LENGTH)
printf("Cannot read or write more than %d bits at a time.\n", MAX_LENGTH);
aligning = sstell(bs)%8;
if (aligning)
putbits(bs, (unsigned int)0, (int)(8-aligning));
putbits(bs, val, N);
}
/*read the next bute aligned N bits from the bit stream */
unsigned long byte_ali_getbits(bs, N)
bitstream_t *bs; /* bit stream structure */
int N; /* number of bits of val */
{
unsigned long aligning, sstell();
if (N > MAX_LENGTH)
printf("Cannot read or write more than %d bits at a time.\n", MAX_LENGTH);
aligning = sstell(bs)%8;
if (aligning)
getbits(bs, (int)(8-aligning));
return(getbits(bs, N));
}
/*return the current bit stream length (in bits)*/
unsigned long sstell(bs)
bitstream_t *bs; /* bit stream structure */
{
return(bs->totbit);
}
/*return the status of the bit stream*/
/* returns 1 if end of bit stream was reached */
/* returns 0 if end of bit stream was not reached */
int end_bs(bs)
bitstream_t *bs; /* bit stream structure */
{
return(bs->eobs);
}
/*this function seeks for a byte aligned sync word in the bit stream and
places the bit stream pointer right after the sync.
This function returns 1 if the sync was found otherwise it returns 0 */
int seek_sync(bs, sync, N)
bitstream_t *bs; /* bit stream structure */
long sync; /* sync word maximum 32 bits */
int N; /* sync word length */
{
#if defined(MACINTOSH) && !defined(__powerc)
double pow();
#endif
unsigned long aligning, stell();
unsigned long val;
long maxi = (int)pow(2.0, (float)N) - 1;
aligning = sstell(bs)%ALIGNING;
if (aligning)
getbits(bs, (int)(ALIGNING-aligning));
val = getbits(bs, N);
while (((val&maxi) != sync) && (!end_bs(bs))) {
val <<= ALIGNING;
val |= getbits(bs, ALIGNING);
}
if (end_bs(bs)) return(0);
else return(1);
}
#define BUFSIZE 4096
static unsigned long offset,totbit=0, buf_byte_idx=0;
static unsigned int buf[BUFSIZE];
static unsigned int buf_bit_idx=8;
unsigned long hgetbits(int N)
{
unsigned long val=0;
register int j = N;
register int k, tmp;
totbit += N;
while (j > 0) {
if (!buf_bit_idx) {
buf_bit_idx = 8;
buf_byte_idx++;
}
k = MIN (j, buf_bit_idx);
tmp = buf[buf_byte_idx%BUFSIZE]&putmask[buf_bit_idx];
tmp = tmp >> (buf_bit_idx-k);
val |= tmp << (j-k);
buf_bit_idx -= k;
j -= k;
}
return(val);
}
/*****************************************************************************
*
* End of bit_stream.c package
*
*****************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -