]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/BASE/AliHLTDataInflater.h
added data inflater based on AliHLTTPCCompModelInflater
[u/mrichter/AliRoot.git] / HLT / BASE / AliHLTDataInflater.h
1 //-*- Mode: C++ -*-
2 // $Id$
3 #ifndef ALIHLTDATAINFLATER_H
4 #define ALIHLTDATAINFLATER_H
5 //* This file is property of and copyright by the ALICE HLT Project        * 
6 //* ALICE Experiment at CERN, All rights reserved.                         *
7 //* See cxx source for full Copyright notice                               *
8
9 /// @file   AliHLTDataInflater.h
10 /// @author Matthias Richter, Timm Steinbeck
11 /// @date   2011-08-10
12 /// @brief  Data inflater reading the bitstream from the AliHLTDataDeflater
13 /// @note   Code original from AliHLTTPCCompModelInflater
14
15 #include "AliHLTLogging.h"
16 #include "AliHLTDataTypes.h"
17 #include "AliHLTStdIncludes.h"
18
19 /**
20  * @class AliHLTDataInflater
21  * Data inflating interface to a bitstream encoded by AliHLTDataInflater
22  * 
23  * 
24  * 
25  *
26  * @ingroup alihlt_base
27  */
28 class AliHLTDataInflater : public AliHLTLogging
29 {
30 public:
31   /// standard constructor
32   AliHLTDataInflater();
33   /// destructor
34   ~AliHLTDataInflater();
35
36   /** init inflater for reading
37    * @param input  AliHLTUInt8_t* pointer to input data
38    * @param inputSize UInt_t input data size
39    */
40   int InitBitDataInput(const AliHLTUInt8_t* input, UInt_t inputSize );
41
42   /** close inflater for reading */     
43   void CloseBitDataInput();
44       
45   /** function to get current byte input position 
46    * @return unsigned long value of current byte input position
47    */
48   unsigned long GetCurrentByteInputPosition() const
49   {
50     return (unsigned long)( fBitDataCurrentInput - fBitDataCurrentInputStart );
51   }
52
53   /** function to get current bit input position
54    * @return unsigned value of current bit input position
55    */
56   unsigned GetCurrentBitInputPosition() const
57   {
58     return fBitDataCurrentPosInWord;
59   }
60
61   /** function to get current input byte
62    * @return AliHLTUInt8_t value of current input byte
63    */
64   AliHLTUInt8_t GetCurrentInputByte() const
65   {
66     return fBitDataCurrentWord;
67   }
68
69   /** function to determine end of bit input
70    * @return boolean if end is reached or not
71    */
72   bool EndOfBitInput() const
73   {
74     return (fBitDataCurrentInput>=fBitDataCurrentInputEnd);
75   }
76       
77   /** function to get bit data input size bytes
78    * @return UInt_t value of bit data input size bytes
79    */
80   UInt_t GetBitDataInputSizeBytes() const
81   {
82     return fBitDataCurrentInput-fBitDataCurrentInputStart;
83   }
84
85   /** function to determine input bit
86    * @return boolean (if bit is 1 or 0)
87    */
88   bool InputBit( AliHLTUInt8_t & value );
89
90   /** function to read bits from bitstream
91    * @param value
92    * @param bitCount
93    * @return boolean 
94    */
95   template<typename T>
96   bool InputBits( T & value, UInt_t const & bitCount );
97  
98   /** function pad 8 bits */
99   void Pad8Bits();
100
101   /** function to determine input bytes
102    * @param data       AliHLTUInt8_t* pointer to input data
103    * @param byteCount  UInt_t const &
104    * @return boolean
105    */
106   bool InputBytes( AliHLTUInt8_t* data, UInt_t const & byteCount );
107
108   /// clear the object and reset pointer references
109   virtual void Clear(Option_t * /*option*/ ="");
110
111   /// print info
112   virtual void Print(Option_t *option="") const;
113
114   /// print info
115   virtual void Print(ostream& out, Option_t *option="") const;
116
117 protected:
118 private:
119   /** copy constructor prohibited */
120   AliHLTDataInflater(const AliHLTDataInflater&);
121   /** assignment operator prohibited */
122   AliHLTDataInflater& operator=(const AliHLTDataInflater&);
123
124   /** member variable for bit data current word */
125   AliHLTUInt8_t fBitDataCurrentWord; // member variable for bit data current word
126   /** member variable for bit data current position in word */
127   UInt_t fBitDataCurrentPosInWord;// member variable for bit data current position in word
128   /** member variable for bit data current input */
129   const AliHLTUInt8_t *fBitDataCurrentInput; // member variable for bit data current input
130   /** member variable for bit data current input start */
131   const AliHLTUInt8_t *fBitDataCurrentInputStart; // member variable for bit data current input star
132   /** member variable for bit data current input end */
133   const AliHLTUInt8_t *fBitDataCurrentInputEnd; // member variable for bit data current input end
134
135   ClassDef(AliHLTDataInflater, 0)
136 };
137
138 template<typename T>
139 bool AliHLTDataInflater::InputBits( T & value, UInt_t const & bitCount )
140 {
141   // read bits from the input stream into variable
142   if (bitCount>sizeof(T)*8) {
143     HLTFatal( "variable of type size %u too small to read %u bits", sizeof(T)*8, (unsigned)bitCount);
144     return false;
145   }
146   UInt_t bitsToRead=bitCount;
147   UInt_t curBitCount;
148   value = 0;
149   while ( bitsToRead>0 ) {
150     if ( fBitDataCurrentInput>=fBitDataCurrentInputEnd )
151       return false;
152     if ( bitsToRead >= fBitDataCurrentPosInWord+1 )
153       curBitCount = fBitDataCurrentPosInWord+1;
154     else
155       curBitCount = bitsToRead;
156     value = (value << curBitCount) | ( (fBitDataCurrentWord >> (fBitDataCurrentPosInWord-curBitCount+1)) & ((1 << curBitCount)-1) );
157     if ( fBitDataCurrentPosInWord < curBitCount ) {
158       fBitDataCurrentInput++;
159       if ( fBitDataCurrentInput<fBitDataCurrentInputEnd ) {
160         fBitDataCurrentWord = *fBitDataCurrentInput;
161         fBitDataCurrentPosInWord = 7;
162       }
163     }
164     else
165       fBitDataCurrentPosInWord -= curBitCount;
166     bitsToRead -= curBitCount;
167   }
168   return true;
169 }
170
171 ostream& operator<<(ostream &out, const AliHLTDataInflater& me);
172
173 #endif