foobar2000 SDK  2015-08-03
bitreader_helper.h
Go to the documentation of this file.
1 namespace bitreader_helper {
2 
3  inline static size_t extract_bit(const t_uint8 * p_stream,size_t p_offset) {
4  return (p_stream[p_offset>>3] >> (7-(p_offset&7)))&1;
5  }
6 
7  inline static size_t extract_int(const t_uint8 * p_stream,size_t p_base,size_t p_width) {
8  size_t ret = 0;
9  size_t offset = p_base;
10  for(size_t bit=0;bit<p_width;bit++) {
11  ret <<= 1;
12  ret |= extract_bit(p_stream,offset++);
13  }
14  return ret;
15  }
16 
17 class bitreader
18 {
19 public:
20  inline bitreader(const t_uint8 * p_ptr,t_size p_base)
21  : m_ptr(p_ptr), m_bitptr(p_base)
22  {
23  }
24 
25  inline void skip(t_size p_bits)
26  {
27  m_bitptr += p_bits;
28  }
29 
30  template<typename t_ret>
31  t_ret read_t(t_size p_bits) {
32  t_ret ret = 0;
33  for(t_size bit=0;bit<p_bits;bit++)
34  {
35  ret <<= 1;
36  ret |= (m_ptr[m_bitptr>>3] >> (7-(m_bitptr&7)))&1;
37  m_bitptr++;
38  }
39  return ret;
40  }
41 
42  t_size read(t_size p_bits) {return read_t<t_size>(p_bits);}
43 
44  inline t_size get_bitptr() const {return m_bitptr;}
45 
46  inline bool read_bit() {
47  bool state = ( (m_ptr[m_bitptr>>3] >> (7-(m_bitptr&7)))&1 ) != 0;
48  m_bitptr++;
49  return state;
50  }
51 
52 private:
53 
54  const t_uint8 * m_ptr;
56 };
57 
59 {
60 public:
61  inline bitreader_fromfile(service_ptr_t<file> const& p_file) : m_file(p_file), m_buffer_ptr(0) {}
62 
63  t_size read(t_size p_bits,abort_callback & p_abort) {
64  t_size ret = 0;
65  for(t_size bit=0;bit<p_bits;bit++) {
66  if (m_buffer_ptr == 0)
67  m_file->read_object(&m_buffer,1,p_abort);
68 
69  ret <<= 1;
70  ret |= (m_buffer >> (7-m_buffer_ptr))&1;
71  m_buffer_ptr = (m_buffer_ptr+1) & 7;
72  }
73  return ret;
74  }
75 
76  void skip(t_size p_bits,abort_callback & p_abort) {
77  for(t_size bit=0;bit<p_bits;bit++) {
78  if (m_buffer_ptr == 0) m_file->read_object(&m_buffer,1,p_abort);
79  m_buffer_ptr = (m_buffer_ptr+1) & 7;
80  }
81  }
82 
83  inline void byte_align() {m_buffer_ptr = 0;}
84 
85 private:
89 };
90 
92 {
93 public:
94  inline bitreader_limited(const t_uint8 * p_ptr,t_size p_base,t_size p_remaining) : m_reader(p_ptr,p_base), m_remaining(p_remaining) {}
95 
96  inline t_size get_bitptr() const {return m_reader.get_bitptr();}
97 
98  inline t_size get_remaining() const {return m_remaining;}
99 
100  inline void skip(t_size p_bits) {
101  if (p_bits > m_remaining) throw exception_io_data_truncation();
102  m_remaining -= p_bits;
103  m_reader.skip(p_bits);
104  }
105 
107  {
108  if (p_bits > m_remaining) throw exception_io_data_truncation();
109  m_remaining -= p_bits;
110  return m_reader.read(p_bits);
111  }
112 
113 private:
116 };
117 
118 inline static t_size extract_bits(const t_uint8 * p_buffer,t_size p_base,t_size p_count) {
119  return bitreader(p_buffer,p_base).read(p_count);
120 }
121 
122 }
uint8_t t_uint8
Definition: int_types.h:9
t_size read(t_size p_bits, abort_callback &p_abort)
void skip(t_size p_bits)
bitreader_limited(const t_uint8 *p_ptr, t_size p_base, t_size p_remaining)
t_size read(t_size p_bits)
static t_size extract_bits(const t_uint8 *p_buffer, t_size p_base, t_size p_count)
bitreader(const t_uint8 *p_ptr, t_size p_base)
bitreader_fromfile(service_ptr_t< file > const &p_file)
size_t t_size
Definition: int_types.h:48
void skip(t_size p_bits, abort_callback &p_abort)
t_ret read_t(t_size p_bits)
static size_t extract_int(const t_uint8 *p_stream, size_t p_base, size_t p_width)
static size_t extract_bit(const t_uint8 *p_stream, size_t p_offset)