source: 3DVCSoftware/branches/HTM-5.0-Qualcomm/source/Lib/TLibDecoder/AnnexBread.h @ 192

Last change on this file since 192 was 56, checked in by hschwarz, 13 years ago

updated trunk (move to HM6.1)

File size: 5.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-2012, 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#pragma once
35
36#include <stdint.h>
37#include <istream>
38#include <vector>
39
40//! \ingroup TLibDecoder
41//! \{
42
43class InputByteStream
44{
45public:
46  /**
47   * Create a bytestream reader that will extract bytes from
48   * istream.
49   *
50   * NB, it isn't safe to access istream while in use by a
51   * InputByteStream.
52   *
53   * Side-effects: the exception mask of istream is set to eofbit
54   */
55  InputByteStream(std::istream& istream)
56  : m_NumFutureBytes(0)
57  , m_FutureBytes(0)
58  , m_Input(istream)
59  {
60    istream.exceptions(std::istream::eofbit);
61  }
62
63  /**
64   * Reset the internal state.  Must be called if input stream is
65   * modified externally to this class
66   */
67  void reset()
68  {
69    m_NumFutureBytes = 0;
70    m_FutureBytes = 0;
71  }
72
73  /**
74   * returns true if an EOF will be encountered within the next
75   * n bytes.
76   */
77  bool eofBeforeNBytes(unsigned n)
78  {
79    assert(n <= 4);
80    if (m_NumFutureBytes >= n)
81      return false;
82
83    n -= m_NumFutureBytes;
84    try
85    {
86      for (unsigned i = 0; i < n; i++)
87      {
88        m_FutureBytes = (m_FutureBytes << 8) | m_Input.get();
89        m_NumFutureBytes++;
90      }
91    }
92    catch (...)
93    {
94      return true;
95    }
96    return false;
97  }
98
99  /**
100   * return the next n bytes in the stream without advancing
101   * the stream pointer.
102   *
103   * Returns: an unsigned integer representing an n byte bigendian
104   * word.
105   *
106   * If an attempt is made to read past EOF, an n-byte word is
107   * returned, but the portion that required input bytes beyond EOF
108   * is undefined.
109   *
110   */
111  uint32_t peekBytes(unsigned n)
112  {
113    eofBeforeNBytes(n);
114    return m_FutureBytes >> 8*(m_NumFutureBytes - n);
115  }
116
117  /**
118   * consume and return one byte from the input.
119   *
120   * If bytestream is already at EOF prior to a call to readByte(),
121   * an exception std::ios_base::failure is thrown.
122   */
123  uint8_t readByte()
124  {
125    if (!m_NumFutureBytes)
126    {
127      uint8_t byte = m_Input.get();
128      return byte;
129    }
130    m_NumFutureBytes--;
131    uint8_t wanted_byte = m_FutureBytes >> 8*m_NumFutureBytes;
132    m_FutureBytes &= ~(0xff << 8*m_NumFutureBytes);
133    return wanted_byte;
134  }
135
136  /**
137   * consume and return n bytes from the input.  n bytes from
138   * bytestream are interpreted as bigendian when assembling
139   * the return value.
140   */
141  uint32_t readBytes(unsigned n)
142  {
143    uint32_t val = 0;
144    for (unsigned i = 0; i < n; i++)
145      val = (val << 8) | readByte();
146    return val;
147  }
148
149private:
150  unsigned m_NumFutureBytes; /* number of valid bytes in m_FutureBytes */
151  uint32_t m_FutureBytes; /* bytes that have been peeked */
152  std::istream& m_Input; /* Input stream to read from */
153};
154
155/**
156 * Statistics associated with AnnexB bytestreams
157 */
158struct AnnexBStats
159{
160  unsigned m_numLeadingZero8BitsBytes;
161  unsigned m_numZeroByteBytes;
162  unsigned m_numStartCodePrefixBytes;
163  unsigned m_numBytesInNALUnit;
164  unsigned m_numTrailingZero8BitsBytes;
165
166  AnnexBStats& operator+=(const AnnexBStats& rhs)
167  {
168    this->m_numLeadingZero8BitsBytes += rhs.m_numLeadingZero8BitsBytes;
169    this->m_numZeroByteBytes += rhs.m_numZeroByteBytes;
170    this->m_numStartCodePrefixBytes += rhs.m_numStartCodePrefixBytes;
171    this->m_numBytesInNALUnit += rhs.m_numBytesInNALUnit;
172    this->m_numTrailingZero8BitsBytes += rhs.m_numTrailingZero8BitsBytes;
173    return *this;
174  }
175};
176
177bool byteStreamNALUnit(InputByteStream& bs, std::vector<uint8_t>& nalUnit, AnnexBStats& stats);
178
179//! \}
Note: See TracBrowser for help on using the repository browser.