HEVC Test Model (HM)  HM-16.3
AnnexBread.h
Go to the documentation of this file.
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-2015, 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 
39 #pragma once
40 
41 #ifndef __ANNEXBREAD__
42 #define __ANNEXBREAD__
43 
44 #include <stdint.h>
45 #include <istream>
46 #include <vector>
47 
48 #include "TLibCommon/TypeDef.h"
49 
52 
54 {
55 public:
65  InputByteStream(std::istream& istream)
66  : m_NumFutureBytes(0)
67  , m_FutureBytes(0)
68  , m_Input(istream)
69  {
70  istream.exceptions(std::istream::eofbit | std::istream::badbit);
71  }
72 
78  {
79  m_NumFutureBytes = 0;
80  m_FutureBytes = 0;
81  }
82 
88  {
89  assert(n <= 4);
90  if (m_NumFutureBytes >= n)
91  {
92  return false;
93  }
94 
95  n -= m_NumFutureBytes;
96  try
97  {
98  for (UInt i = 0; i < n; i++)
99  {
100  m_FutureBytes = (m_FutureBytes << 8) | m_Input.get();
102  }
103  }
104  catch (...)
105  {
106  return true;
107  }
108  return false;
109  }
110 
123  uint32_t peekBytes(UInt n)
124  {
125  eofBeforeNBytes(n);
126  return m_FutureBytes >> 8*(m_NumFutureBytes - n);
127  }
128 
135  uint8_t readByte()
136  {
137  if (!m_NumFutureBytes)
138  {
139  uint8_t byte = m_Input.get();
140  return byte;
141  }
143  uint8_t wanted_byte = m_FutureBytes >> 8*m_NumFutureBytes;
144  m_FutureBytes &= ~(0xff << 8*m_NumFutureBytes);
145  return wanted_byte;
146  }
147 
153  uint32_t readBytes(UInt n)
154  {
155  uint32_t val = 0;
156  for (UInt i = 0; i < n; i++)
157  {
158  val = (val << 8) | readByte();
159  }
160  return val;
161  }
162 
163 #if RExt__DECODER_DEBUG_BIT_STATISTICS
164  UInt GetNumBufferedBytes() const { return m_NumFutureBytes; }
165 #endif
166 
167 private:
168  UInt m_NumFutureBytes; /* number of valid bytes in m_FutureBytes */
169  uint32_t m_FutureBytes; /* bytes that have been peeked */
170  std::istream& m_Input; /* Input stream to read from */
171 };
172 
177 {
183 
185  {
186  this->m_numLeadingZero8BitsBytes += rhs.m_numLeadingZero8BitsBytes;
187  this->m_numZeroByteBytes += rhs.m_numZeroByteBytes;
188  this->m_numStartCodePrefixBytes += rhs.m_numStartCodePrefixBytes;
189  this->m_numBytesInNALUnit += rhs.m_numBytesInNALUnit;
190  this->m_numTrailingZero8BitsBytes += rhs.m_numTrailingZero8BitsBytes;
191  return *this;
192  }
193 };
194 
195 Bool byteStreamNALUnit(InputByteStream& bs, std::vector<uint8_t>& nalUnit, AnnexBStats& stats);
196 
198 
199 #endif
Bool eofBeforeNBytes(UInt n)
Definition: AnnexBread.h:87
Bool byteStreamNALUnit(InputByteStream &bs, std::vector< uint8_t > &nalUnit, AnnexBStats &stats)
Definition: AnnexBread.cpp:182
void Void
Definition: TypeDef.h:285
UInt m_numZeroByteBytes
Definition: AnnexBread.h:179
unsigned int UInt
Definition: TypeDef.h:297
bool Bool
Definition: TypeDef.h:286
UInt m_numBytesInNALUnit
Definition: AnnexBread.h:181
UInt m_numLeadingZero8BitsBytes
Definition: AnnexBread.h:178
uint32_t m_FutureBytes
Definition: AnnexBread.h:169
UInt m_NumFutureBytes
Definition: AnnexBread.h:168
UInt m_numTrailingZero8BitsBytes
Definition: AnnexBread.h:182
UInt m_numStartCodePrefixBytes
Definition: AnnexBread.h:180
uint32_t readBytes(UInt n)
Definition: AnnexBread.h:153
InputByteStream(std::istream &istream)
Definition: AnnexBread.h:65
uint32_t peekBytes(UInt n)
Definition: AnnexBread.h:123
AnnexBStats & operator+=(const AnnexBStats &rhs)
Definition: AnnexBread.h:184
std::istream & m_Input
Definition: AnnexBread.h:170
uint8_t readByte()
Definition: AnnexBread.h:135
Define basic types, new types and enumerations.