[313] | 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 | * |
---|
[595] | 6 | * Copyright (c) 2010-2014, ITU/ISO/IEC |
---|
[313] | 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 | |
---|
[1029] | 41 | #ifndef __ANNEXBREAD__ |
---|
| 42 | #define __ANNEXBREAD__ |
---|
| 43 | |
---|
[313] | 44 | #include <stdint.h> |
---|
| 45 | #include <istream> |
---|
| 46 | #include <vector> |
---|
| 47 | |
---|
| 48 | #include "TLibCommon/TypeDef.h" |
---|
| 49 | |
---|
| 50 | //! \ingroup TLibDecoder |
---|
| 51 | //! \{ |
---|
| 52 | |
---|
| 53 | class InputByteStream |
---|
| 54 | { |
---|
| 55 | public: |
---|
| 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 | { |
---|
[713] | 70 | istream.exceptions(std::istream::eofbit | std::istream::badbit); |
---|
[313] | 71 | } |
---|
| 72 | |
---|
| 73 | /** |
---|
| 74 | * Reset the internal state. Must be called if input stream is |
---|
| 75 | * modified externally to this class |
---|
| 76 | */ |
---|
[1029] | 77 | Void reset() |
---|
[313] | 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 | return false; |
---|
| 92 | |
---|
| 93 | n -= m_NumFutureBytes; |
---|
| 94 | try |
---|
| 95 | { |
---|
| 96 | for (UInt i = 0; i < n; i++) |
---|
| 97 | { |
---|
| 98 | m_FutureBytes = (m_FutureBytes << 8) | m_Input.get(); |
---|
| 99 | m_NumFutureBytes++; |
---|
| 100 | } |
---|
| 101 | } |
---|
| 102 | catch (...) |
---|
| 103 | { |
---|
| 104 | return true; |
---|
| 105 | } |
---|
| 106 | return false; |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | /** |
---|
| 110 | * return the next n bytes in the stream without advancing |
---|
| 111 | * the stream pointer. |
---|
| 112 | * |
---|
| 113 | * Returns: an unsigned integer representing an n byte bigendian |
---|
| 114 | * word. |
---|
| 115 | * |
---|
| 116 | * If an attempt is made to read past EOF, an n-byte word is |
---|
| 117 | * returned, but the portion that required input bytes beyond EOF |
---|
| 118 | * is undefined. |
---|
| 119 | * |
---|
| 120 | */ |
---|
| 121 | uint32_t peekBytes(UInt n) |
---|
| 122 | { |
---|
| 123 | eofBeforeNBytes(n); |
---|
| 124 | return m_FutureBytes >> 8*(m_NumFutureBytes - n); |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | /** |
---|
| 128 | * consume and return one byte from the input. |
---|
| 129 | * |
---|
| 130 | * If bytestream is already at EOF prior to a call to readByte(), |
---|
| 131 | * an exception std::ios_base::failure is thrown. |
---|
| 132 | */ |
---|
| 133 | uint8_t readByte() |
---|
| 134 | { |
---|
| 135 | if (!m_NumFutureBytes) |
---|
| 136 | { |
---|
| 137 | uint8_t byte = m_Input.get(); |
---|
| 138 | return byte; |
---|
| 139 | } |
---|
| 140 | m_NumFutureBytes--; |
---|
| 141 | uint8_t wanted_byte = m_FutureBytes >> 8*m_NumFutureBytes; |
---|
| 142 | m_FutureBytes &= ~(0xff << 8*m_NumFutureBytes); |
---|
| 143 | return wanted_byte; |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | /** |
---|
| 147 | * consume and return n bytes from the input. n bytes from |
---|
| 148 | * bytestream are interpreted as bigendian when assembling |
---|
| 149 | * the return value. |
---|
| 150 | */ |
---|
| 151 | uint32_t readBytes(UInt n) |
---|
| 152 | { |
---|
| 153 | uint32_t val = 0; |
---|
| 154 | for (UInt i = 0; i < n; i++) |
---|
| 155 | val = (val << 8) | readByte(); |
---|
| 156 | return val; |
---|
| 157 | } |
---|
| 158 | |
---|
[1029] | 159 | #if RExt__DECODER_DEBUG_BIT_STATISTICS |
---|
| 160 | UInt GetNumBufferedBytes() const { return m_NumFutureBytes; } |
---|
| 161 | #endif |
---|
| 162 | |
---|
[313] | 163 | private: |
---|
| 164 | UInt m_NumFutureBytes; /* number of valid bytes in m_FutureBytes */ |
---|
| 165 | uint32_t m_FutureBytes; /* bytes that have been peeked */ |
---|
| 166 | std::istream& m_Input; /* Input stream to read from */ |
---|
| 167 | }; |
---|
| 168 | |
---|
| 169 | /** |
---|
| 170 | * Statistics associated with AnnexB bytestreams |
---|
| 171 | */ |
---|
| 172 | struct AnnexBStats |
---|
| 173 | { |
---|
| 174 | UInt m_numLeadingZero8BitsBytes; |
---|
| 175 | UInt m_numZeroByteBytes; |
---|
| 176 | UInt m_numStartCodePrefixBytes; |
---|
| 177 | UInt m_numBytesInNALUnit; |
---|
| 178 | UInt m_numTrailingZero8BitsBytes; |
---|
| 179 | |
---|
| 180 | AnnexBStats& operator+=(const AnnexBStats& rhs) |
---|
| 181 | { |
---|
| 182 | this->m_numLeadingZero8BitsBytes += rhs.m_numLeadingZero8BitsBytes; |
---|
| 183 | this->m_numZeroByteBytes += rhs.m_numZeroByteBytes; |
---|
| 184 | this->m_numStartCodePrefixBytes += rhs.m_numStartCodePrefixBytes; |
---|
| 185 | this->m_numBytesInNALUnit += rhs.m_numBytesInNALUnit; |
---|
| 186 | this->m_numTrailingZero8BitsBytes += rhs.m_numTrailingZero8BitsBytes; |
---|
| 187 | return *this; |
---|
| 188 | } |
---|
| 189 | }; |
---|
| 190 | |
---|
| 191 | Bool byteStreamNALUnit(InputByteStream& bs, std::vector<uint8_t>& nalUnit, AnnexBStats& stats); |
---|
| 192 | |
---|
| 193 | //! \} |
---|
[1029] | 194 | |
---|
| 195 | #endif |
---|