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  {
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

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