source: SHVCSoftware/trunk/source/Lib/TLibCommon/TComBitStream.h @ 160

Last change on this file since 160 was 125, checked in by seregin, 12 years ago

copy from HM-10.0-dev-SHM

File size: 7.3 KB
Line 
1/* The copyright in this software is being made available under the BSD
2 * License, included below. This software may be subject to other third party
3 * and contributor rights, including patent rights, and no such rights are
4 * granted under this license. 
5 *
6 * Copyright (c) 2010-2013, ITU/ISO/IEC
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 *
12 *  * Redistributions of source code must retain the above copyright notice,
13 *    this list of conditions and the following disclaimer.
14 *  * Redistributions in binary form must reproduce the above copyright notice,
15 *    this list of conditions and the following disclaimer in the documentation
16 *    and/or other materials provided with the distribution.
17 *  * Neither the name of the ITU/ISO/IEC nor the names of its contributors may
18 *    be used to endorse or promote products derived from this software without
19 *    specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31 * THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34/** \file     TComBitStream.h
35    \brief    class for handling bitstream (header)
36*/
37
38#ifndef __COMBITSTREAM__
39#define __COMBITSTREAM__
40
41#if _MSC_VER > 1000
42#pragma once
43#endif // _MSC_VER > 1000
44
45#include <stdint.h>
46#include <vector>
47#include <stdio.h>
48#include <assert.h>
49#include "CommonDef.h"
50
51//! \ingroup TLibCommon
52//! \{
53
54// ====================================================================================================================
55// Class definition
56// ====================================================================================================================
57
58/// pure virtual class for basic bit handling
59class TComBitIf
60{
61public:
62  virtual Void        writeAlignOne         () {};
63  virtual Void        writeAlignZero        () {};
64  virtual Void        write                 ( UInt uiBits, UInt uiNumberOfBits )  = 0;
65  virtual Void        resetBits             ()                                    = 0;
66  virtual UInt getNumberOfWrittenBits() const = 0;
67  virtual ~TComBitIf() {}
68};
69
70/**
71 * Model of a writable bitstream that accumulates bits to produce a
72 * bytestream.
73 */
74class TComOutputBitstream : public TComBitIf
75{
76  /**
77   * FIFO for storage of bytes.  Use:
78   *  - fifo.push_back(x) to append words
79   *  - fifo.clear() to empty the FIFO
80   *  - &fifo.front() to get a pointer to the data array.
81   *    NB, this pointer is only valid until the next push_back()/clear()
82   */
83  std::vector<uint8_t> *m_fifo;
84
85  UInt m_num_held_bits; /// number of bits not flushed to bytestream.
86  UChar m_held_bits; /// the bits held and not flushed to bytestream.
87                             /// this value is always msb-aligned, bigendian.
88
89public:
90  // create / destroy
91  TComOutputBitstream();
92  ~TComOutputBitstream();
93
94  // interface for encoding
95  /**
96   * append uiNumberOfBits least significant bits of uiBits to
97   * the current bitstream
98   */
99  Void        write           ( UInt uiBits, UInt uiNumberOfBits );
100
101  /** insert one bits until the bitstream is byte-aligned */
102  Void        writeAlignOne   ();
103
104  /** insert zero bits until the bitstream is byte-aligned */
105  Void        writeAlignZero  ();
106
107  /** this function should never be called */
108  void resetBits() { assert(0); }
109
110  // utility functions
111
112  /**
113   * Return a pointer to the start of the byte-stream buffer.
114   * Pointer is valid until the next write/flush/reset call.
115   * NB, data is arranged such that subsequent bytes in the
116   * bytestream are stored in ascending addresses.
117   */
118  Char* getByteStream() const;
119
120  /**
121   * Return the number of valid bytes available from  getByteStream()
122   */
123  UInt getByteStreamLength();
124
125  /**
126   * Reset all internal state.
127   */
128  Void clear();
129
130  /**
131   * returns the number of bits that need to be written to
132   * achieve byte alignment.
133   */
134  Int getNumBitsUntilByteAligned() { return (8 - m_num_held_bits) & 0x7; }
135
136  /**
137   * Return the number of bits that have been written since the last clear()
138   */
139  UInt getNumberOfWrittenBits() const { return UInt(m_fifo->size()) * 8 + m_num_held_bits; }
140
141  void insertAt(const TComOutputBitstream& src, UInt pos);
142
143  /**
144   * Return a reference to the internal fifo
145   */
146  std::vector<uint8_t>& getFIFO() { return *m_fifo; }
147
148  UChar getHeldBits  ()          { return m_held_bits;          }
149
150  TComOutputBitstream& operator= (const TComOutputBitstream& src);
151  /** Return a reference to the internal fifo */
152  std::vector<uint8_t>& getFIFO() const { return *m_fifo; }
153
154  Void          addSubstream    ( TComOutputBitstream* pcSubstream );
155  Void writeByteAlignment();
156};
157
158/**
159 * Model of an input bitstream that extracts bits from a predefined
160 * bytestream.
161 */
162class TComInputBitstream
163{
164  std::vector<uint8_t> *m_fifo; /// FIFO for storage of complete bytes
165
166protected:
167  UInt m_fifo_idx; /// Read index into m_fifo
168
169  UInt m_num_held_bits;
170  UChar m_held_bits;
171  UInt  m_numBitsRead;
172
173public:
174  /**
175   * Create a new bitstream reader object that reads from #buf#.  Ownership
176   * of #buf# remains with the callee, although the constructed object
177   * will hold a reference to #buf#
178   */
179  TComInputBitstream(std::vector<uint8_t>* buf);
180  ~TComInputBitstream();
181
182  // interface for decoding
183  Void        pseudoRead      ( UInt uiNumberOfBits, UInt& ruiBits );
184  Void        read            ( UInt uiNumberOfBits, UInt& ruiBits );
185  Void        readByte        ( UInt &ruiBits )
186  {
187    assert(m_fifo_idx < m_fifo->size());
188    ruiBits = (*m_fifo)[m_fifo_idx++];
189  }
190
191  Void        readOutTrailingBits ();
192  UChar getHeldBits  ()          { return m_held_bits;          }
193  TComOutputBitstream& operator= (const TComOutputBitstream& src);
194  UInt  getByteLocation              ( )                     { return m_fifo_idx                    ; }
195
196  // Peek at bits in word-storage. Used in determining if we have completed reading of current bitstream and therefore slice in LCEC.
197  UInt        peekBits (UInt uiBits) { UInt tmp; pseudoRead(uiBits, tmp); return tmp; }
198
199  // utility functions
200  UInt read(UInt numberOfBits) { UInt tmp; read(numberOfBits, tmp); return tmp; }
201  UInt     readByte() { UInt tmp; readByte( tmp ); return tmp; }
202  UInt getNumBitsUntilByteAligned() { return m_num_held_bits & (0x7); }
203  UInt getNumBitsLeft() { return 8*((UInt)m_fifo->size() - m_fifo_idx) + m_num_held_bits; }
204  TComInputBitstream *extractSubstream( UInt uiNumBits ); // Read the nominated number of bits, and return as a bitstream.
205  Void                deleteFifo(); // Delete internal fifo of bitstream.
206  UInt  getNumBitsRead() { return m_numBitsRead; }
207  Void readByteAlignment();
208};
209
210//! \}
211
212#endif
Note: See TracBrowser for help on using the repository browser.