source: 3DVCSoftware/branches/HTM-16.3-fixes/source/Lib/TLibDecoder/AnnexBread.h @ 1417

Last change on this file since 1417 was 1413, checked in by tech, 6 years ago

Merged HTM-16.2-dev@1412

File size: 5.6 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-2017, 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/**
35 \file     AnnexBread.h
36 \brief    reading functions for Annex B byte streams
37 */
38
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/CommonDef.h"
49
50//! \ingroup TLibDecoder
51//! \{
52
53class InputByteStream
54{
55public:
56  /**
57   * Create a bytestream reader that will extract bytes from
58   * istream.
59   *
60   * NB, it isn't safe to access istream while in use by a
61   * InputByteStream.
62   *
63   * Side-effects: the exception mask of istream is set to eofbit
64   */
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
73  /**
74   * Reset the internal state.  Must be called if input stream is
75   * modified externally to this class
76   */
77  Void reset()
78  {
79    m_NumFutureBytes = 0;
80    m_FutureBytes = 0;
81  }
82
83  /**
84   * returns true if an EOF will be encountered within the next
85   * n bytes.
86   */
87  Bool eofBeforeNBytes(UInt n)
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();
101        m_NumFutureBytes++;
102      }
103    }
104    catch (...)
105    {
106      return true;
107    }
108    return false;
109  }
110
111  /**
112   * return the next n bytes in the stream without advancing
113   * the stream pointer.
114   *
115   * Returns: an unsigned integer representing an n byte bigendian
116   * word.
117   *
118   * If an attempt is made to read past EOF, an n-byte word is
119   * returned, but the portion that required input bytes beyond EOF
120   * is undefined.
121   *
122   */
123  uint32_t peekBytes(UInt n)
124  {
125    eofBeforeNBytes(n);
126    return m_FutureBytes >> 8*(m_NumFutureBytes - n);
127  }
128
129  /**
130   * consume and return one byte from the input.
131   *
132   * If bytestream is already at EOF prior to a call to readByte(),
133   * an exception std::ios_base::failure is thrown.
134   */
135  uint8_t readByte()
136  {
137    if (!m_NumFutureBytes)
138    {
139      uint8_t byte = m_Input.get();
140      return byte;
141    }
142    m_NumFutureBytes--;
143    uint8_t wanted_byte = m_FutureBytes >> 8*m_NumFutureBytes;
144    m_FutureBytes &= ~(0xff << 8*m_NumFutureBytes);
145    return wanted_byte;
146  }
147
148  /**
149   * consume and return n bytes from the input.  n bytes from
150   * bytestream are interpreted as bigendian when assembling
151   * the return value.
152   */
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
167private:
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
173/**
174 * Statistics associated with AnnexB bytestreams
175 */
176struct AnnexBStats
177{
178  UInt m_numLeadingZero8BitsBytes;
179  UInt m_numZeroByteBytes;
180  UInt m_numStartCodePrefixBytes;
181  UInt m_numBytesInNALUnit;
182  UInt m_numTrailingZero8BitsBytes;
183
184  AnnexBStats& operator+=(const AnnexBStats& rhs)
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
195Bool byteStreamNALUnit(InputByteStream& bs, std::vector<uint8_t>& nalUnit, AnnexBStats& stats);
196
197//! \}
198
199#endif
Note: See TracBrowser for help on using the repository browser.