1 #ifndef _BITSTREAM_ 2 #define _BITSTREAM_ 3 4 5 // The structure holds the current position in the bitstream. 6 // pos points to the current byte position and bpos counts the 7 // bits left unread at the current byte pos. No bit read means 8 // bpos = 8. 9 // end is used to check that nothing is read at "end" or after it. 10 struct bitstream 11 { 12 unsigned char *pos; 13 int bpos; 14 unsigned char *end; 15 // Indicate how many bits are left in the stream after the previous 16 // read call. A negative number indicates that a read after the 17 // end of the stream was attempted. 18 int64_t bitsleft; 19 // Indicate an error occured while parsing the bitstream. 20 // This is meant to store high level syntax errors, i.e a function 21 // using the bitstream functions found a syntax error. 22 int error; 23 // Internal (private) variable - used to store the the bitstream 24 // position until it is decided if the bitstream pointer will be 25 // increased by the calling function, or not. 26 unsigned char *_i_pos; 27 int _i_bpos; 28 }; 29 30 #define read_u8(bstream) (uint8_t)bitstream_get_num(bstream,1,1) 31 #define read_u16(bstream) (uint16_t)bitstream_get_num(bstream,2,1) 32 #define read_u32(bstream) (uint32_t)bitstream_get_num(bstream,4,1) 33 #define read_u64(bstream) (uint64_t)bitstream_get_num(bstream,8,1) 34 #define read_i8(bstream) (int8_t)bitstream_get_num(bstream,1,1) 35 #define read_i16(bstream) (int16_t)bitstream_get_num(bstream,2,1) 36 #define read_i32(bstream) (int32_t)bitstream_get_num(bstream,4,1) 37 #define read_i64(bstream) (int64_t)bitstream_get_num(bstream,8,1) 38 39 #define skip_u32(bstream) (void)bitstream_get_num(bstream,4,1) 40 41 #define next_u8(bstream) (uint8_t)bitstream_get_num(bstream,1,0) 42 #define next_u16(bstream) (uint16_t)bitstream_get_num(bstream,2,0) 43 #define next_u32(bstream) (uint32_t)bitstream_get_num(bstream,4,0) 44 #define next_u64(bstream) (uint64_t)bitstream_get_num(bstream,8,0) 45 #define next_i8(bstream) (int8_t)bitstream_get_num(bstream,1,0) 46 #define next_i16(bstream) (int16_t)bitstream_get_num(bstream,2,0) 47 #define next_i32(bstream) (int32_t)bitstream_get_num(bstream,4,0) 48 #define next_i64(bstream) (int64_t)bitstream_get_num(bstream,8,0) 49 50 51 int init_bitstream(struct bitstream *bstr, unsigned char *start, unsigned char *end); 52 uint64_t next_bits(struct bitstream *bstr, unsigned bnum); 53 uint64_t read_bits(struct bitstream *bstr, unsigned bnum); 54 int skip_bits(struct bitstream *bstr, unsigned bnum); 55 int is_byte_aligned(struct bitstream *bstr); 56 void make_byte_aligned(struct bitstream *bstr); 57 unsigned char *next_bytes(struct bitstream *bstr, unsigned bynum); 58 unsigned char *read_bytes(struct bitstream *bstr, unsigned bynum); 59 uint64_t bitstream_get_num(struct bitstream *bstr, unsigned bytes, int advance); 60 uint64_t read_exp_golomb_unsigned(struct bitstream *bstr); 61 int64_t read_exp_golomb(struct bitstream *bstr); 62 uint64_t read_int_unsigned(struct bitstream *bstr, unsigned bnum); 63 int64_t read_int(struct bitstream *bstr, unsigned bnum); 64 uint8_t reverse8(uint8_t data); 65 66 #endif 67