# Dirac - A Video Codec

Created by the British Broadcasting Corporation.

byteio.h
Go to the documentation of this file.
1 /* ***** BEGIN LICENSE BLOCK *****
2 *
3 * $Id: byteio.h,v 1.11 2009/01/21 05:18:09 asuraparaju Exp $ $Name: Dirac_1_0_2 $
4 *
5 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 *
7 * The contents of this file are subject to the Mozilla Public License
8 * Version 1.1 (the "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 * http://www.mozilla.org/MPL/
11 *
12 * Software distributed under the License is distributed on an "AS IS" basis,
13 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
14 * the specific language governing rights and limitations under the License.
15 *
16 * The Original Code is BBC Research and Development code.
17 *
18 * The Initial Developer of the Original Code is the British Broadcasting
19 * Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 2004.
21 * All Rights Reserved.
22 *
23 * Contributor(s): Andrew Kennedy (Original Author),
24 * Anuradha Suraparaju
25 *
26 * Alternatively, the contents of this file may be used under the terms of
27 * the GNU General Public License Version 2 (the "GPL"), or the GNU Lesser
28 * Public License Version 2.1 (the "LGPL"), in which case the provisions of
29 * the GPL or the LGPL are applicable instead of those above. If you wish to
30 * allow use of your version of this file only under the terms of the either
31 * the GPL or LGPL and not to allow others to use your version of this file
32 * under the MPL, indicate your decision by deleting the provisions above
33 * and replace them with the notice and other provisions required by the GPL
34 * or LGPL. If you do not delete the provisions above, a recipient may use
35 * your version of this file under the terms of any one of the MPL, the GPL
36 * or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
38
42 #ifndef byteio_h
43 #define byteio_h
44
45 // SYSTEM INCLUDES
46 #include <iostream> // IO classes
47 #include <sstream> // IO classes
48 #include <iomanip> // setw
49 #include <climits> // CHAR_BIT
50
51 //LOCAL INCLUDEs
52 #include <libdirac_byteio/dirac_byte_stats.h> // stores stats
53
54 namespace dirac
55 {
56
57 // BIT DEFS
58 #define BIT_ZERO 0
59 #define BIT_ONE 1
60
61 // most significant bit in a character
62 #define MS_BIT (1 << (CHAR_BIT - 1))
63
64 /* array index for character containing bit */
65 //#define BIT_IN_CHAR(bit) (1 << (CHAR_BIT-1-bit))
66 #define BIT_IN_CHAR(bit) (1 << bit)
67
68
72 class ByteIO
73 {
74 public:
75
80 ByteIO(bool new_stream=true);
81
86 ByteIO(const ByteIO& stream_data);
87
91 virtual ~ByteIO();
92
97 virtual void CollateByteStats(DiracByteStats& dirac_byte_stats)
98 { dirac_byte_stats.Clear(); }
99
103 virtual const std::string GetBytes();
104
108 int GetReadBytePosition() const { return mp_stream->tellg();};
109
110
114 virtual int GetSize() const;
115
120 void SetByteParams(const ByteIO& byte_io);
121
125 void ByteAlignOutput();
126
131 //void OutputVarLengthUint(const unsigned int& value);
132 void WriteUint(unsigned int value);
133
137 void SetBitsLeft(int left_bits) { m_bits_left = left_bits; }
138
142 int BitsLeft(void) { return m_bits_left; }
143
144 protected:
145
146 inline bool CanRead() const { return(!mp_stream->eof()); }
147
148 inline bool GetBit(unsigned char& c, int pos) const { return (c & BIT_IN_CHAR(pos)); }
149
150 inline void SetBit(unsigned char& c, int pos) const { c |= BIT_IN_CHAR(pos); }
151
152 inline void SetBits(unsigned char& c, unsigned char bits) const { c |= bits; }
153
157 void ByteAlignInput();
158
159
163 bool ReadBool();
164
168 bool ReadBoolB();
169
173 int ReadBit();
174
178 int ReadBitB();
179
185 unsigned int ReadNBits(int count);
186
192 void InputBytes(char* data, int count)
193 {
194 //int j=mp_stream->tellg();
195 mp_stream->read(data, count);
196
197 //int h=mp_stream->tellg();
198 }
199
203 void FlushInputB();
204
209 //int InputVarLengthInt();
210 int ReadSint();
211
216 int ReadSintB();
217
222 //unsigned int InputVarLengthUint();
223 unsigned int ReadUint();
224
229 //unsigned int InputVarLengthUint();
230 unsigned int ReadUintB();
231
237 //inline unsigned int InputFixedLengthUint(const int byte_size) {
238 inline unsigned int ReadUintLit(const int byte_size) {
239 unsigned int val=0;
240 for(int i=0; i < byte_size; ++i)
241 {
242 val <<= 8;
243 val += (unsigned char)mp_stream->get();
244 }
245 m_num_bytes+=byte_size;
246 return val;
247 }
248
252 inline unsigned char InputUnByte() {m_num_bytes++ ; return mp_stream->get(); }
253
257 inline std::string InputUnString(const int count)
258 {
259 std::string str;
260 for(int index=0; index < count; ++index)
261 str.push_back(InputUnByte());
262 return str;
263 }
264
269 void WriteBit(const bool& bit);
270
276 int WriteNBits(unsigned int val);
277
283 void WriteNBits(unsigned int val, int count);
284
285
286
290 void OutputBytes(const std::string& bytes) {
291 int cur_pos = mp_stream->tellg();
292 mp_stream->str(mp_stream->str()+bytes);
293 m_num_bytes+=bytes.size();
294 // *mp_stream << bytes;
295 mp_stream->seekg(std::max(cur_pos,0), std::ios_base::beg);
296 }
297
301 inline void OutputCurrentByte()
302 {
303 if (m_current_pos)
304 {
305 *mp_stream << (m_current_byte);
306 ++m_num_bytes;
307 m_current_pos = 0;
308 m_current_byte = 0;
309 }
310 };
311
316 //void OutputVarLengthInt(const int val);
317 void WriteSint(int val);
318
324 //inline void OutputFixedLengthUint(const unsigned int& value, const int& length)
325 inline void WriteUintLit(const unsigned int& value, const int& length)
326 {
327 for(int i=length-1; i >=0 ; --i)
328 {
329 unsigned char cp = (value>>(i*8))&0xff;
330 *mp_stream << cp;
331 }
332 m_num_bytes+=length;
333 }
334
339 void RemoveRedundantBytes(const int count);
340
341 inline void SeekGet(const int offset, std::ios_base::seekdir dir)
342 {
343 mp_stream->seekg(offset, dir);
344 }
345
349 std::stringstream* mp_stream;
350
351
352 private:
353
357 friend class ArithCodecBase;
358
362 friend class ArithCodecToVLCAdapter;
363
367 unsigned char m_current_byte;
368
372 int m_current_pos;
373
377 int m_num_bytes;
378
382 bool m_new_stream;
383
387 int m_bits_left;
388 protected:
389
390
391 };
392
393
394
395 } // namespace dirac
396
397 #endif
dirac::ByteIO::ReadSint
int ReadSint()
Reads a signed integer in interleaved exp-Golomb format return Signed integer read.
dirac::ByteIO::~ByteIO
virtual ~ByteIO()
Destructor.
dirac::ByteIO::ByteAlignOutput
void ByteAlignOutput()
Sync input for byte-alignment.
dirac::ByteIO::ReadNBits
unsigned int ReadNBits(int count)
Reads next 'count' bits.
dirac::DiracByteStats::Clear
void Clear()
Clears data.
dirac::ByteIO::OutputBytes
void OutputBytes(const std::string &bytes)
Outputs a series of bytes.
Definition: byteio.h:290
dirac::ArithCodecToVLCAdapter
Definition: band_vlc.h:55
dirac::ByteIO::ReadUintB
unsigned int ReadUintB()
Reads an unsigned integer in interleaved exp Golomb format from bounded input.
dirac::ByteIO::InputBytes
void InputBytes(char *data, int count)
Reads from stream.
Definition: byteio.h:192
dirac::ByteIO::WriteSint
void WriteSint(int val)
Outputs an integer in Golomb signed integer format.
dirac::ByteIO::ReadUintLit
unsigned int ReadUintLit(const int byte_size)
Reads a fixed length unsigned integer from the stream in big endian.
Definition: byteio.h:238
dirac::ByteIO::InputUnString
std::string InputUnString(const int count)
Reads a series of bytes from a stream.
Definition: byteio.h:257
dirac::ByteIO::ByteAlignInput
void ByteAlignInput()
Sync input for byte-alignment.
dirac::ByteIO::ReadUint
unsigned int ReadUint()
Reads an unsigned integer in interleaved exp Golomb format.
dirac::ByteIO::FlushInputB
void FlushInputB()
Flushes the bounde input.
dirac::ByteIO::ReadBool
bool ReadBool()
Reads boolean value.
dirac::ByteIO::ReadBitB
int ReadBitB()
Reads next bit - bounded i/o.
dirac::ByteIO::RemoveRedundantBytes
void RemoveRedundantBytes(const int count)
Removes portion of byte-stream no longer required.
dirac::ByteIO::InputUnByte
unsigned char InputUnByte()
Reads a byte from the stream.
Definition: byteio.h:252
dirac::ByteIO::m_num_bytes
int m_num_bytes
Number of bytes processed.
Definition: byteio.h:377
dirac::ByteIO::WriteUintLit
void WriteUintLit(const unsigned int &value, const int &length)
Output unsigned int value in big endian format.
Definition: byteio.h:325
dirac::ByteIO::SeekGet
void SeekGet(const int offset, std::ios_base::seekdir dir)
Definition: byteio.h:341
dirac::ByteIO::ReadSintB
int ReadSintB()
Reads a signed integer in interleaved exp-Golomb format from bounded input return Signed integer read...
dirac::ByteIO::SetBit
void SetBit(unsigned char &c, int pos) const
Definition: byteio.h:150
dirac::ByteIO::m_bits_left
int m_bits_left
num bits left to read
Definition: byteio.h:387
dirac::ByteIO::OutputCurrentByte
void OutputCurrentByte()
Outputs current byte contents.
Definition: byteio.h:301
dirac::ByteIO::GetBytes
virtual const std::string GetBytes()
Get bytes in Dirac-bytestream format.
dirac::ByteIO::CollateByteStats
virtual void CollateByteStats(DiracByteStats &dirac_byte_stats)
Gathers byte-stream statistics.
Definition: byteio.h:97
dirac::ByteIO::SetBitsLeft
void SetBitsLeft(int left_bits)
Sets input size in bits.
Definition: byteio.h:137
dirac::ByteIO::ReadBit
int ReadBit()
Reads next bit.
dirac::ByteIO::WriteNBits
int WriteNBits(unsigned int val)
Outputs an unsigned integer.
dirac::ByteIO::GetReadBytePosition
int GetReadBytePosition() const
Get position of read stream pointer.
Definition: byteio.h:108
dirac::DiracByteStats
Class DiracByteStats - for collecting statistics on aspects of the Dirac byte-stream.
Definition: dirac_byte_stats.h:70
dirac::ByteIO::mp_stream
std::stringstream * mp_stream
Input/output steam.
Definition: byteio.h:349
dirac_byte_stats.h
dirac::ByteIO::GetBit
bool GetBit(unsigned char &c, int pos) const
Definition: byteio.h:148
dirac::ByteIO::WriteUint
void WriteUint(unsigned int value)
Ouputs an unsigned integer in interleaved exp Golomb format.
dirac::ByteIO::m_current_byte
unsigned char m_current_byte
Char used for temporary storage of op data bits.
Definition: byteio.h:367
BIT_IN_CHAR
#define BIT_IN_CHAR(bit)
Definition: byteio.h:66
dirac
Definition of class SequenceHeaderByteIO.
Definition: accessunit_byteio.h:51
dirac::ByteIO::GetSize
virtual int GetSize() const
Gets size (in bytes)
dirac::ByteIO::m_current_pos
int m_current_pos
Used to set individual bit within the current header byte.
Definition: byteio.h:372
dirac::ByteIO::CanRead
bool CanRead() const
Definition: byteio.h:146
dirac::ByteIO::m_new_stream
bool m_new_stream
stream alloc flag
Definition: byteio.h:382
dirac::ByteIO::SetByteParams
void SetByteParams(const ByteIO &byte_io)
Copies stream source/destination info.
dirac::ByteIO
Class ByteIO - top-level class for reading/writing bytes to a stream.
Definition: byteio.h:72
dirac::ByteIO::ReadBoolB
bool ReadBoolB()
Reads boolean value - bounded i/o.
dirac::ByteIO::ByteIO
ByteIO(bool new_stream=true)
Default constructor.
dirac::ArithCodecBase
Definition: arith_codec.h:95
dirac::ByteIO::WriteBit
void WriteBit(const bool &bit)
Outputs a bit.
dirac::ByteIO::BitsLeft
int BitsLeft(void)
Sets input size in bits.
Definition: byteio.h:142
dirac::ByteIO::SetBits
void SetBits(unsigned char &c, unsigned char bits) const
Definition: byteio.h:152

© 2004 British Broadcasting Corporation. Dirac code licensed under the Mozilla Public License (MPL) Version 1.1.
HTML documentation generated by Dimitri van Heesch's excellent Doxygen tool.