ZenLib
|
00001 /* Copyright (c) MediaArea.net SARL. All Rights Reserved. 00002 * 00003 * Use of this source code is governed by a zlib-style license that can 00004 * be found in the License.txt file in the root of the source tree. 00005 */ 00006 00007 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 00008 // 00009 // Read a stream bit per bit, Little Endian version (rarely used!!!) 00010 // Can read up to 32 bits at once 00011 // 00012 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 00013 00014 //--------------------------------------------------------------------------- 00015 #ifndef ZenBitStream_LEH 00016 #define ZenBitStream_LEH 00017 //--------------------------------------------------------------------------- 00018 00019 //--------------------------------------------------------------------------- 00020 #include "ZenLib/BitStream.h" 00021 //--------------------------------------------------------------------------- 00022 00023 namespace ZenLib 00024 { 00025 00026 class BitStream_LE : public BitStream 00027 { 00028 public: 00029 BitStream_LE () :BitStream() {}; 00030 BitStream_LE (const int8u* Buffer_, size_t Size_) :BitStream(Buffer_, Size_) {}; 00031 00032 void Attach(const int8u* Buffer_, size_t Size_) 00033 { 00034 endbyte=0; 00035 endbit=0; 00036 buffer=Buffer_; 00037 ptr=Buffer_; 00038 storage=(long)Size_; 00039 }; 00040 00041 int32u Get (size_t HowMany) 00042 { 00043 ptr_BeforeLastCall=ptr; 00044 00045 long ret; 00046 static const int32u Mask[33]={ 00047 0x00000000, 00048 0x00000001, 0x00000003, 0x00000007, 0x0000000f, 00049 0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff, 00050 0x000001ff, 0x000003ff, 0x000007ff, 0x00000fff, 00051 0x00001fff, 0x00003fff, 0x00007fff, 0x0000ffff, 00052 0x0001ffff, 0x0003ffff, 0x0007ffff, 0x000fffff, 00053 0x001fffff, 0x003fffff, 0x007fffff, 0x00ffffff, 00054 0x01ffffff, 0x03ffffff, 0x07ffffff, 0x0fffffff, 00055 0x1fffffff, 0x3fffffff, 0x7fffffff, 0xffffffff, 00056 }; 00057 unsigned long m=Mask[HowMany]; 00058 00059 HowMany+=endbit; 00060 00061 if(endbyte+4>=storage){ 00062 ret=-1L; 00063 if(endbyte*8+(long)HowMany>storage*8){ 00064 Attach(NULL, 0); 00065 goto overflow; 00066 } 00067 } 00068 00069 ret=ptr[0]>>endbit; 00070 if(HowMany>8){ 00071 ret|=ptr[1]<<(8-endbit); 00072 if(HowMany>16){ 00073 ret|=ptr[2]<<(16-endbit); 00074 if(HowMany>24){ 00075 ret|=ptr[3]<<(24-endbit); 00076 if(HowMany>32 && endbit){ 00077 ret|=ptr[4]<<(32-endbit); 00078 } 00079 } 00080 } 00081 } 00082 ret&=m; 00083 00084 ptr+=HowMany/8; 00085 endbyte+=(long)HowMany/8; 00086 endbit=(long)HowMany&7; 00087 00088 overflow: 00089 00090 return(ret); 00091 }; 00092 00093 void Skip(size_t bits) 00094 { 00095 Get(bits); 00096 } 00097 00098 int32u Remain () //How many bits remain? 00099 { 00100 return storage*8-(endbyte*8+endbit); 00101 }; 00102 00103 void Byte_Align() 00104 { 00105 }; 00106 00107 size_t Offset_Get() 00108 { 00109 return ptr-buffer; 00110 }; 00111 00112 size_t BitOffset_Get() 00113 { 00114 return endbit; 00115 }; 00116 00117 size_t OffsetBeforeLastCall_Get() 00118 { 00119 return ptr_BeforeLastCall-buffer; 00120 }; 00121 00122 private : 00123 long endbyte; 00124 int endbit; 00125 00126 const unsigned char *buffer; 00127 const unsigned char *ptr; 00128 const unsigned char *ptr_BeforeLastCall; 00129 long storage; 00130 }; 00131 00132 } //namespace ZenLib 00133 #endif