ZenLib
BitStream_LE.h
Go to the documentation of this file.
1 // ZenLib::BitStream_LE - Read bit per bit, Little Endian version
2 // Copyright (C) 2007-2011 MediaArea.net SARL, Info@MediaArea.net
3 //
4 // This software is provided 'as-is', without any express or implied
5 // warranty. In no event will the authors be held liable for any damages
6 // arising from the use of this software.
7 //
8 // Permission is granted to anyone to use this software for any purpose,
9 // including commercial applications, and to alter it and redistribute it
10 // freely, subject to the following restrictions:
11 //
12 // 1. The origin of this software must not be misrepresented; you must not
13 // claim that you wrote the original software. If you use this software
14 // in a product, an acknowledgment in the product documentation would be
15 // appreciated but is not required.
16 // 2. Altered source versions must be plainly marked as such, and must not be
17 // misrepresented as being the original software.
18 // 3. This notice may not be removed or altered from any source distribution.
19 //
20 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
21 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
22 //
23 // Read a stream bit per bit, Little Endian version (rarely used!!!)
24 // Can read up to 32 bits at once
25 //
26 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
27 
28 //---------------------------------------------------------------------------
29 #ifndef ZenBitStream_LEH
30 #define ZenBitStream_LEH
31 //---------------------------------------------------------------------------
32 
33 //---------------------------------------------------------------------------
34 #include "ZenLib/BitStream.h"
35 //---------------------------------------------------------------------------
36 
37 namespace ZenLib
38 {
39 
40 class BitStream_LE : public BitStream
41 {
42 public:
44  BitStream_LE (const int8u* Buffer_, size_t Size_) :BitStream(Buffer_, Size_) {};
45 
46  void Attach(const int8u* Buffer_, size_t Size_)
47  {
48  endbyte=0;
49  endbit=0;
50  buffer=Buffer_;
51  ptr=Buffer_;
52  storage=(long)Size_;
53  };
54 
55  int32u Get (size_t HowMany)
56  {
57  ptr_BeforeLastCall=ptr;
58 
59  long ret;
60  static const int32u Mask[33]={
61  0x00000000,
62  0x00000001, 0x00000003, 0x00000007, 0x0000000f,
63  0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff,
64  0x000001ff, 0x000003ff, 0x000007ff, 0x00000fff,
65  0x00001fff, 0x00003fff, 0x00007fff, 0x0000ffff,
66  0x0001ffff, 0x0003ffff, 0x0007ffff, 0x000fffff,
67  0x001fffff, 0x003fffff, 0x007fffff, 0x00ffffff,
68  0x01ffffff, 0x03ffffff, 0x07ffffff, 0x0fffffff,
69  0x1fffffff, 0x3fffffff, 0x7fffffff, 0xffffffff,
70  };
71  unsigned long m=Mask[HowMany];
72 
73  HowMany+=endbit;
74 
75  if(endbyte+4>=storage){
76  ret=-1L;
77  if(endbyte*8+(long)HowMany>storage*8){
78  Attach(NULL, 0);
79  goto overflow;
80  }
81  }
82 
83  ret=ptr[0]>>endbit;
84  if(HowMany>8){
85  ret|=ptr[1]<<(8-endbit);
86  if(HowMany>16){
87  ret|=ptr[2]<<(16-endbit);
88  if(HowMany>24){
89  ret|=ptr[3]<<(24-endbit);
90  if(HowMany>32 && endbit){
91  ret|=ptr[4]<<(32-endbit);
92  }
93  }
94  }
95  }
96  ret&=m;
97 
98  ptr+=HowMany/8;
99  endbyte+=(long)HowMany/8;
100  endbit=(long)HowMany&7;
101 
102  overflow:
103 
104  return(ret);
105  };
106 
107  void Skip(size_t bits)
108  {
109  Get(bits);
110  }
111 
112  int32u Remain () //How many bits remain?
113  {
114  return storage*8-(endbyte*8+endbit);
115  };
116 
117  void Byte_Align()
118  {
119  };
120 
121  size_t Offset_Get()
122  {
123  return ptr-buffer;
124  };
125 
126  size_t BitOffset_Get()
127  {
128  return endbit;
129  };
130 
132  {
133  return ptr_BeforeLastCall-buffer;
134  };
135 
136 private :
137  long endbyte;
138  int endbit;
139 
140  const unsigned char *buffer;
141  const unsigned char *ptr;
142  const unsigned char *ptr_BeforeLastCall;
143  long storage;
144 };
145 
146 } //namespace ZenLib
147 #endif