📄 bitreader.h
字号:
/************************************************************ Copyright (C) 2002 Sigma Designs, Inc. All Rights Reserved Proprietary and Confidential Author: David Weiguo Zheng <david_zheng@sdesigns.com> File Name: bitRead.h Purpose: All functions to handle bit reading**************************************************************/#ifndef _BITREAD_H_#define _BITREAD_H_typedef struct{ RMuint32 pos; RMuint32 curr; RMuint32 next; RMuint32 * buf; RMuint32 * ptr; RMuint32 length; RMuint32 dec_err;} BitReader;static __inline void br_init(BitReader * const br, void * const stream, const RMuint32 length, const RMuint32 start_bit);static __inline RMuint32 br_get(BitReader * const br, const RMuint32 n);static __inline RMuint32 br_get1(BitReader * const br);static __inline RMuint32 br_length(BitReader * const br);static __inline RMuint32 br_bit_used(BitReader * const br);static __inline RMuint32 br_show(BitReader * const br, const RMuint32 bits);static __inline void br_skip(BitReader * const br, const RMuint32 bits);static __inline RMuint32 br_skip_show(BitReader * const br, const RMuint32 skip_bits, const RMuint32 show_bits);static __inline void br_dword_align(BitReader * const br);static __inline void br_init(BitReader * const br, void * const stream, const RMuint32 length, const RMuint32 start_bit){ RMuint32 i1, i2; RMuint32 aligned = (RMuint32)stream & 3; if(aligned) {// pointer is not integer aligned RMuint32 i; RMuint8 * str = (RMuint8 *)stream; br->buf = br->ptr = (RMuint32*)(str+4-aligned); aligned = 4 - aligned; br->length = length;#ifdef _WIN32_PROTECT_ br->data_consumed = aligned;#endif i1 = 0; for(i=0; i< aligned; i++) { i1 = (i1 << 8) | *str++; } i2 = *br->ptr ++; RMuint32ToBeBuf(i2, (RMuint8 *) &i2); br->curr = i1; br->next = i2; br->pos = 32 - (aligned << 3); br->dec_err = 0; } else { br->buf = br->ptr = (RMuint32*)stream; br->length = length;#ifdef _WIN32_PROTECT_ br->data_consumed = 4;#endif i1 = *br->ptr ++; i2 = *br->ptr ++; RMuint32ToBeBuf(i1, (RMuint8 *) &i1); RMuint32ToBeBuf(i2, (RMuint8 *) &i2); br->curr = i1; br->next = i2; br->pos = 0; br->dec_err = 0; } if(start_bit) br_skip(br, start_bit); // flush the irrelative bits}// return the ammount of bytes used for decoding, ptr is for RMuint32 *static __inline RMuint32 br_length(BitReader * const br){ return ((br->ptr - br->buf)<<2) - 4 - ((32 - br->pos) >> 3);}// return the ammount of bits used for decoding, ptr is for RMuint32 *static __inline RMuint32 br_bit_used(BitReader * const br){ return ((((br->ptr - br->buf)<<2) - 4) <<3)- (32 - br->pos);}static __inline RMuint32 br_show(BitReader * const br, const RMuint32 bits){ RMuint32 pos = br->pos; RMuint32 nbit = bits + br->pos; if (nbit < 33) { return (br->curr & (0xffffffffU >> pos)) >> (32 - nbit); } else { return ((br->curr & (0xffffffffU >> pos)) << (nbit-32)) | (br->next >> (64 - nbit)); }}static __inline void br_skip(BitReader * const br, const RMuint32 bits){ br->pos += bits; if (br->pos > 31) { RMuint32 ui32 = *br->ptr++; br->curr = br->next; br->pos -= 32; RMuint32ToBeBuf(ui32, (RMuint8 *) &ui32); br->next = ui32;#ifdef _WIN32_PROTECT_ br->data_consumed += 4; // each time we read an integer = 4 bytes if(br->data_consumed > br->length) { br->ptr = br->buf; // reset the ptr br->dec_err = 1; }#endif }}static __inline RMuint32 br_skip_show(BitReader * const br, const RMuint32 skip_bits, const RMuint32 show_bits){ br->pos += skip_bits; if (br->pos > 31) { RMuint32 ui32 = *br->ptr++; br->curr = br->next; br->pos -= 32; RMuint32ToBeBuf(ui32, (RMuint8 *) &ui32); br->next = ui32;#ifdef _WIN32_PROTECT_ br->data_consumed += 4; // each time we read an integer = 4 bytes if(br->data_consumed > br->length) { br->ptr = br->buf; // reset the ptr br->dec_err = 1; }#endif } { RMuint32 pos = br->pos; RMuint32 new_pos = show_bits + br->pos; if (new_pos < 33) { return (br->curr & (0xffffffff >> pos)) >> (32 - new_pos); } else { return ((br->curr & (0xffffffff >> pos)) << (new_pos-32)) | (br->next >> (64 - new_pos)); } }}static __inline void br_byte_align(BitReader * const br){ RMuint32 remained = br->pos & 7; if (remained) br_skip(br, 8 - remained);}static __inline void br_dword_align(BitReader * const br){ RMuint32 remained = br->pos & 31; if (remained) br_skip(br, 32 - remained);}static __inline RMuint32 br_get(BitReader * const br, const RMuint32 n){ RMuint32 pos = br->pos; RMuint32 new_pos = n + br->pos; if (new_pos < 32) { RMuint32 val = (br->curr & (0xffffffff >> pos)) >> (32 - new_pos); br->pos = new_pos; return val; } else //new_pos > 32 { RMuint32 val; RMuint32 ui32 = *br->ptr++; if(new_pos > 32) val = ((br->curr & (0xffffffff >> pos)) << (new_pos-32)) | (br->next >> (64 - new_pos)); else val = br->curr & (0xffffffff >> pos); br->curr = br->next; RMuint32ToBeBuf(ui32, (RMuint8 *) &ui32); br->next = ui32; br->pos = new_pos - 32;#ifdef _WIN32_PROTECT_ br->data_consumed += 4; // each time we read an integer = 4 bytes if(br->data_consumed > br->length) { br->ptr = br->buf; // reset the ptr br->dec_err = 1; }#endif return val; }}static __inline RMuint32 br_get1(BitReader * const br){ RMuint32 val; RMuint32 pos = br->pos+1; val = (br->curr >> (32 - pos)) & 0x1; if (pos > 31) { RMuint32 ui32 = *br->ptr++; br->curr = br->next; pos -= 32; RMuint32ToBeBuf(ui32, (RMuint8 *) &ui32); br->next = ui32;#ifdef _WIN32_PROTECT_ br->data_consumed += 4; // each time we read an integer = 4 bytes if(br->data_consumed > br->length) { br->ptr = br->buf; // reset the ptr br->dec_err = 1; }#endif } br->pos = pos; return val;}#endif /* _BITREAD_H_ */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -