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 *
9 /// @file AliHLTDataInflater.h
10 /// @author Matthias Richter, Timm Steinbeck
12 /// @brief Data inflater reading the bitstream from the AliHLTDataDeflater
13 /// @note Code original from AliHLTTPCCompModelInflater
15 #include "AliHLTLogging.h"
16 #include "AliHLTDataTypes.h"
17 #include "AliHLTStdIncludes.h"
20 * @class AliHLTDataInflater
21 * Data inflating interface to a bitstream encoded by AliHLTDataDeflater
23 * Setting up the data inflater:
25 AliHLTDataInflater inflater;
26 if (inflater.InitBitDataInput(pData, dataSize)<0) {
31 * If the layout in the bitstream is known the parts of known bit length
34 * - InputBits(value, length)
36 * For inflating huffman encoded streams where the symbol length is unknown
37 * one has to read a fixed value, retrieve the symbol and rewind the stream.
38 * Example how to read a fixed number of bits from the stream and rewind
39 * after the length of the symbol has been determined.
41 AliHLTUInt64_t inputWord=0;
42 int inputLength=sizeof(inputWord)*8;
43 while (outClusterCnt<nofClusters && inflater.InputBits(inputWord, inputLength)) {
44 // check how many of the bits belong to the symbol and rewind the
47 inputWord>>=(inputLength-symbolLength);
48 if (!inflater.RewindBitPosition(inputLength-symbolLength)) {
53 // do something with data in input word
55 // check if there is less remaining data than the full input word bit length
56 UInt_t bytes=inflater.GetRemainingBitDataSizeBytes();
57 if (bytes>0 && bytes<=sizeof(inputWord)) {
58 // reading the last bytes
59 // available bits are determined by cuurent position+1 (because
60 // position 0 means 1 bit still available) and the bits in the
62 inputLength=inflater.GetCurrentBitInputPosition()+1+(bytes-1)*8;
68 * @ingroup alihlt_base
70 class AliHLTDataInflater : public AliHLTLogging
73 /// standard constructor
76 ~AliHLTDataInflater();
78 /** init inflater for reading
79 * @param input AliHLTUInt8_t* pointer to input data
80 * @param inputSize UInt_t input data size
82 int InitBitDataInput(const AliHLTUInt8_t* input, UInt_t inputSize );
84 /** close inflater for reading */
85 void CloseBitDataInput();
87 /** function to get current byte input position
88 * @return unsigned long value of current byte input position
90 unsigned long GetCurrentByteInputPosition() const
92 return (unsigned long)( fBitDataCurrentInput - fBitDataCurrentInputStart );
95 /** function to get current bit input position
96 * @return unsigned value of current bit input position
98 unsigned GetCurrentBitInputPosition() const
100 return fBitDataCurrentPosInWord;
103 /** function to get current input byte
104 * @return AliHLTUInt8_t value of current input byte
106 AliHLTUInt8_t GetCurrentInputByte() const
108 return fBitDataCurrentWord;
111 /** function to determine end of bit input
112 * @return boolean if end is reached or not
114 bool EndOfBitInput() const
116 return (fBitDataCurrentInput>=fBitDataCurrentInputEnd);
119 /** function to get bit data input size bytes
120 * @return UInt_t value of bit data input size bytes
122 UInt_t GetBitDataInputSizeBytes() const
124 return fBitDataCurrentInput-fBitDataCurrentInputStart;
127 /** get number of remaining input bytes
128 * the last byte might be already partially read, use
129 * GetCurrentBitInputPosition()
131 UInt_t GetRemainingBitDataSizeBytes() const
133 return fBitDataCurrentInputEnd-fBitDataCurrentInput;
136 /** function to determine input bit
137 * @return boolean (if bit is 1 or 0)
139 bool InputBit( AliHLTUInt8_t & value );
141 /** function to read bits from bitstream
147 bool InputBits( T & value, UInt_t const & bitCount );
150 * Rewind the current bit position by the given number of bits.
152 bool RewindBitPosition(UInt_t const & bitCount);
154 /** function pad 8 bits */
157 /** function to determine input bytes
158 * @param data AliHLTUInt8_t* pointer to input data
159 * @param byteCount UInt_t const &
162 bool InputBytes( AliHLTUInt8_t* data, UInt_t const & byteCount );
165 * Read the next value.
166 * Data read function for inflaters for different formats
168 virtual bool NextValue(AliHLTUInt64_t& /*value*/, AliHLTUInt32_t& /*length*/) {return false;}
170 /// clear the object and reset pointer references
171 virtual void Clear(Option_t * /*option*/ ="");
174 virtual void Print(Option_t *option="") const;
177 virtual void Print(ostream& out, Option_t *option="") const;
181 /** copy constructor prohibited */
182 AliHLTDataInflater(const AliHLTDataInflater&);
183 /** assignment operator prohibited */
184 AliHLTDataInflater& operator=(const AliHLTDataInflater&);
186 /** member variable for bit data current word */
187 AliHLTUInt8_t fBitDataCurrentWord; // member variable for bit data current word
188 /** member variable for bit data current position in word */
189 UInt_t fBitDataCurrentPosInWord;// member variable for bit data current position in word
190 /** member variable for bit data current input */
191 const AliHLTUInt8_t *fBitDataCurrentInput; // member variable for bit data current input
192 /** member variable for bit data current input start */
193 const AliHLTUInt8_t *fBitDataCurrentInputStart; // member variable for bit data current input star
194 /** member variable for bit data current input end */
195 const AliHLTUInt8_t *fBitDataCurrentInputEnd; // member variable for bit data current input end
197 ClassDef(AliHLTDataInflater, 0)
201 bool AliHLTDataInflater::InputBits( T & value, UInt_t const & bitCount )
203 // read bits from the input stream into variable
204 if (bitCount>sizeof(T)*8) {
205 HLTFatal( "variable of type size %u too small to read %u bits", sizeof(T)*8, (unsigned)bitCount);
208 UInt_t bitsToRead=bitCount;
211 while ( bitsToRead>0 ) {
212 if ( fBitDataCurrentInput>=fBitDataCurrentInputEnd )
214 if ( bitsToRead >= fBitDataCurrentPosInWord+1 )
215 curBitCount = fBitDataCurrentPosInWord+1;
217 curBitCount = bitsToRead;
218 value = (value << curBitCount) | ( (fBitDataCurrentWord >> (fBitDataCurrentPosInWord-curBitCount+1)) & ((1 << curBitCount)-1) );
219 if ( fBitDataCurrentPosInWord < curBitCount ) {
220 fBitDataCurrentInput++;
221 fBitDataCurrentPosInWord = 7;
222 if ( fBitDataCurrentInput<fBitDataCurrentInputEnd ) {
223 fBitDataCurrentWord = *fBitDataCurrentInput;
227 fBitDataCurrentPosInWord -= curBitCount;
228 bitsToRead -= curBitCount;
233 ostream& operator<<(ostream &out, const AliHLTDataInflater& me);