source: 3DVCSoftware/trunk/source/Lib/TLibDecoder/AnnexBread.h @ 833

Last change on this file since 833 was 608, checked in by tech, 11 years ago

Merged DEV-2.0-dev0@604.

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