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 
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 
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  {
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 
363 
367  unsigned char m_current_byte;
368 
373 
378 
383 
388  protected:
389 
390 
391  };
392 
393 
394 
395 } // namespace dirac
396 
397 #endif
#define BIT_IN_CHAR(bit)
Definition: byteio.h:66
Definition of class SequenceHeaderByteIO.
Definition: accessunit_byteio.h:52
Class ByteIO - top-level class for reading/writing bytes to a stream.
Definition: byteio.h:73
void WriteBit(const bool &bit)
Outputs a bit.
bool CanRead() const
Definition: byteio.h:146
int m_bits_left
num bits left to read
Definition: byteio.h:387
void RemoveRedundantBytes(const int count)
Removes portion of byte-stream no longer required.
int m_current_pos
Used to set individual bit within the current header byte.
Definition: byteio.h:372
std::string InputUnString(const int count)
Reads a series of bytes from a stream.
Definition: byteio.h:257
void OutputCurrentByte()
Outputs current byte contents.
Definition: byteio.h:301
void FlushInputB()
Flushes the bounde input.
unsigned int ReadUintB()
Reads an unsigned integer in interleaved exp Golomb format from bounded input.
bool m_new_stream
stream alloc flag
Definition: byteio.h:382
unsigned char m_current_byte
Char used for temporary storage of op data bits.
Definition: byteio.h:367
void ByteAlignInput()
Sync input for byte-alignment.
void WriteSint(int val)
Outputs an integer in Golomb signed integer format.
int ReadBit()
Reads next bit.
void WriteUint(unsigned int value)
Ouputs an unsigned integer in interleaved exp Golomb format.
unsigned int ReadNBits(int count)
Reads next 'count' bits.
void WriteUintLit(const unsigned int &value, const int &length)
Output unsigned int value in big endian format.
Definition: byteio.h:325
bool GetBit(unsigned char &c, int pos) const
Definition: byteio.h:148
void ByteAlignOutput()
Sync input for byte-alignment.
int ReadBitB()
Reads next bit - bounded i/o.
unsigned int ReadUintLit(const int byte_size)
Reads a fixed length unsigned integer from the stream in big endian.
Definition: byteio.h:238
ByteIO(const ByteIO &stream_data)
Constructor.
void SeekGet(const int offset, std::ios_base::seekdir dir)
Definition: byteio.h:341
virtual void CollateByteStats(DiracByteStats &dirac_byte_stats)
Gathers byte-stream statistics.
Definition: byteio.h:97
void SetByteParams(const ByteIO &byte_io)
Copies stream source/destination info.
int m_num_bytes
Number of bytes processed.
Definition: byteio.h:377
int ReadSint()
Reads a signed integer in interleaved exp-Golomb format return Signed integer read.
unsigned char InputUnByte()
Reads a byte from the stream.
Definition: byteio.h:252
int ReadSintB()
Reads a signed integer in interleaved exp-Golomb format from bounded input return Signed integer read...
void SetBitsLeft(int left_bits)
Sets input size in bits.
Definition: byteio.h:137
std::stringstream * mp_stream
Input/output steam.
Definition: byteio.h:349
void SetBits(unsigned char &c, unsigned char bits) const
Definition: byteio.h:152
virtual ~ByteIO()
Destructor.
virtual const std::string GetBytes()
Get bytes in Dirac-bytestream format.
bool ReadBool()
Reads boolean value.
int BitsLeft(void)
Sets input size in bits.
Definition: byteio.h:142
int WriteNBits(unsigned int val)
Outputs an unsigned integer.
void WriteNBits(unsigned int val, int count)
Outputs an n bit integer.
int GetReadBytePosition() const
Get position of read stream pointer.
Definition: byteio.h:108
void InputBytes(char *data, int count)
Reads from stream.
Definition: byteio.h:192
void SetBit(unsigned char &c, int pos) const
Definition: byteio.h:150
bool ReadBoolB()
Reads boolean value - bounded i/o.
unsigned int ReadUint()
Reads an unsigned integer in interleaved exp Golomb format.
ByteIO(bool new_stream=true)
Default constructor.
void OutputBytes(const std::string &bytes)
Outputs a series of bytes.
Definition: byteio.h:290
virtual int GetSize() const
Gets size (in bytes)
Class DiracByteStats - for collecting statistics on aspects of the Dirac byte-stream.
Definition: dirac_byte_stats.h:71
void Clear()
Clears data.
Definition: arith_codec.h:95
Definition: band_vlc.h:56

© 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.