[5] | 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 |
---|
[56] | 4 | * granted under this license. |
---|
[5] | 5 | * |
---|
[56] | 6 | * Copyright (c) 2010-2012, ITU/ISO/IEC |
---|
[5] | 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. |
---|
[56] | 17 | * * Neither the name of the ITU/ISO/IEC nor the names of its contributors may |
---|
[5] | 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 | */ |
---|
[2] | 33 | |
---|
| 34 | /** \file TComBitStream.h |
---|
| 35 | \brief class for handling bitstream (header) |
---|
| 36 | */ |
---|
| 37 | |
---|
| 38 | #ifndef __COMBITSTREAM__ |
---|
| 39 | #define __COMBITSTREAM__ |
---|
| 40 | |
---|
| 41 | #if _MSC_VER > 1000 |
---|
| 42 | #pragma once |
---|
| 43 | #endif // _MSC_VER > 1000 |
---|
| 44 | |
---|
[56] | 45 | #include <stdint.h> |
---|
| 46 | #include <vector> |
---|
[2] | 47 | #include <stdio.h> |
---|
| 48 | #include <assert.h> |
---|
| 49 | #include "CommonDef.h" |
---|
| 50 | |
---|
[56] | 51 | //! \ingroup TLibCommon |
---|
| 52 | //! \{ |
---|
| 53 | |
---|
[2] | 54 | // ==================================================================================================================== |
---|
| 55 | // Class definition |
---|
| 56 | // ==================================================================================================================== |
---|
| 57 | |
---|
| 58 | /// pure virtual class for basic bit handling |
---|
| 59 | class TComBitIf |
---|
| 60 | { |
---|
| 61 | public: |
---|
| 62 | virtual Void writeAlignOne () {}; |
---|
[56] | 63 | virtual Void writeAlignZero () {}; |
---|
[2] | 64 | virtual Void write ( UInt uiBits, UInt uiNumberOfBits ) = 0; |
---|
| 65 | virtual Void resetBits () = 0; |
---|
| 66 | virtual UInt getNumberOfWrittenBits() const = 0; |
---|
| 67 | virtual ~TComBitIf() {} |
---|
| 68 | }; |
---|
| 69 | |
---|
[56] | 70 | /** |
---|
| 71 | * Model of a writable bitstream that accumulates bits to produce a |
---|
| 72 | * bytestream. |
---|
| 73 | */ |
---|
| 74 | class TComOutputBitstream : public TComBitIf |
---|
[2] | 75 | { |
---|
[56] | 76 | /** |
---|
| 77 | * FIFO for storage of bytes. Use: |
---|
| 78 | * - fifo.push_back(x) to append words |
---|
| 79 | * - fifo.clear() to empty the FIFO |
---|
| 80 | * - &fifo.front() to get a pointer to the data array. |
---|
| 81 | * NB, this pointer is only valid until the next push_back()/clear() |
---|
| 82 | */ |
---|
| 83 | std::vector<uint8_t> *m_fifo; |
---|
[2] | 84 | |
---|
[56] | 85 | unsigned int m_num_held_bits; /// number of bits not flushed to bytestream. |
---|
| 86 | unsigned char m_held_bits; /// the bits held and not flushed to bytestream. |
---|
| 87 | /// this value is always msb-aligned, bigendian. |
---|
| 88 | UInt m_uiTileMarkerLocationCount; |
---|
| 89 | UInt *m_puiTileMarkerLocation; |
---|
[42] | 90 | |
---|
[2] | 91 | public: |
---|
| 92 | // create / destroy |
---|
[56] | 93 | TComOutputBitstream(); |
---|
| 94 | ~TComOutputBitstream(); |
---|
| 95 | |
---|
[2] | 96 | // interface for encoding |
---|
[56] | 97 | /** |
---|
| 98 | * append uiNumberOfBits least significant bits of uiBits to |
---|
| 99 | * the current bitstream |
---|
| 100 | */ |
---|
[2] | 101 | Void write ( UInt uiBits, UInt uiNumberOfBits ); |
---|
[56] | 102 | |
---|
| 103 | /** insert one bits until the bitstream is byte-aligned */ |
---|
[2] | 104 | Void writeAlignOne (); |
---|
[56] | 105 | |
---|
| 106 | /** insert zero bits until the bitstream is byte-aligned */ |
---|
[2] | 107 | Void writeAlignZero (); |
---|
[56] | 108 | |
---|
| 109 | /** this function should never be called */ |
---|
| 110 | void resetBits() { assert(0); } |
---|
| 111 | |
---|
| 112 | // utility functions |
---|
| 113 | |
---|
| 114 | /** |
---|
| 115 | * Return a pointer to the start of the byte-stream buffer. |
---|
| 116 | * Pointer is valid until the next write/flush/reset call. |
---|
| 117 | * NB, data is arranged such that subsequent bytes in the |
---|
| 118 | * bytestream are stored in ascending addresses. |
---|
| 119 | */ |
---|
| 120 | char* getByteStream() const; |
---|
| 121 | |
---|
| 122 | /** |
---|
| 123 | * Return the number of valid bytes available from getByteStream() |
---|
| 124 | */ |
---|
| 125 | unsigned int getByteStreamLength(); |
---|
| 126 | |
---|
| 127 | /** |
---|
| 128 | * Reset all internal state. |
---|
| 129 | */ |
---|
| 130 | Void clear(); |
---|
| 131 | |
---|
| 132 | /** |
---|
| 133 | * returns the number of bits that need to be written to |
---|
| 134 | * achieve byte alignment. |
---|
| 135 | */ |
---|
| 136 | Int getNumBitsUntilByteAligned() { return (8 - m_num_held_bits) & 0x7; } |
---|
| 137 | |
---|
| 138 | /** |
---|
| 139 | * Return the number of bits that have been written since the last clear() |
---|
| 140 | */ |
---|
| 141 | unsigned getNumberOfWrittenBits() const { return unsigned(m_fifo->size()) * 8 + m_num_held_bits; } |
---|
| 142 | |
---|
| 143 | void insertAt(const TComOutputBitstream& src, unsigned pos); |
---|
| 144 | |
---|
| 145 | /** |
---|
| 146 | * Return a reference to the internal fifo |
---|
| 147 | */ |
---|
| 148 | std::vector<uint8_t>& getFIFO() { return *m_fifo; } |
---|
| 149 | |
---|
| 150 | UChar getHeldBits () { return m_held_bits; } |
---|
| 151 | |
---|
| 152 | TComOutputBitstream& operator= (const TComOutputBitstream& src); |
---|
| 153 | UInt getTileMarkerLocationCount ( ) { return m_uiTileMarkerLocationCount ; } |
---|
| 154 | Void setTileMarkerLocationCount ( UInt i ) { m_uiTileMarkerLocationCount = i ; } |
---|
| 155 | UInt getTileMarkerLocation ( UInt i) { return m_puiTileMarkerLocation[i] ; } |
---|
| 156 | Void setTileMarkerLocation ( UInt i, UInt uiLOC ) { m_puiTileMarkerLocation[i] = uiLOC ; } |
---|
| 157 | /** Return a reference to the internal fifo */ |
---|
| 158 | std::vector<uint8_t>& getFIFO() const { return *m_fifo; } |
---|
| 159 | |
---|
| 160 | Void addSubstream ( TComOutputBitstream* pcSubstream ); |
---|
| 161 | }; |
---|
| 162 | |
---|
| 163 | /** |
---|
| 164 | * Model of an input bitstream that extracts bits from a predefined |
---|
| 165 | * bytestream. |
---|
| 166 | */ |
---|
| 167 | class TComInputBitstream |
---|
| 168 | { |
---|
| 169 | std::vector<uint8_t> *m_fifo; /// FIFO for storage of complete bytes |
---|
| 170 | |
---|
| 171 | protected: |
---|
| 172 | unsigned int m_fifo_idx; /// Read index into m_fifo |
---|
| 173 | |
---|
| 174 | unsigned int m_num_held_bits; |
---|
| 175 | unsigned char m_held_bits; |
---|
| 176 | UInt m_uiTileMarkerLocationCount; |
---|
| 177 | UInt *m_puiTileMarkerLocation; |
---|
| 178 | UInt m_numBitsRead; |
---|
| 179 | |
---|
| 180 | public: |
---|
| 181 | /** |
---|
| 182 | * Create a new bitstream reader object that reads from #buf#. Ownership |
---|
| 183 | * of #buf# remains with the callee, although the constructed object |
---|
| 184 | * will hold a reference to #buf# |
---|
| 185 | */ |
---|
| 186 | TComInputBitstream(std::vector<uint8_t>* buf); |
---|
| 187 | ~TComInputBitstream(); |
---|
| 188 | |
---|
[2] | 189 | // interface for decoding |
---|
| 190 | Void pseudoRead ( UInt uiNumberOfBits, UInt& ruiBits ); |
---|
| 191 | Void read ( UInt uiNumberOfBits, UInt& ruiBits ); |
---|
[296] | 192 | #if !OL_FLUSH_ALIGN |
---|
[56] | 193 | Void readByte ( UInt &ruiBits ) |
---|
| 194 | { |
---|
| 195 | // More expensive, but reads "bytes" that are not aligned. |
---|
| 196 | read(8, ruiBits); |
---|
| 197 | } |
---|
| 198 | #else |
---|
| 199 | Void readByte ( UInt &ruiBits ) |
---|
| 200 | { |
---|
| 201 | assert(m_fifo_idx < m_fifo->size()); |
---|
| 202 | ruiBits = (*m_fifo)[m_fifo_idx++]; |
---|
| 203 | } |
---|
| 204 | #endif // OL_FLUSH && !OL_FLUSH_ALIGN |
---|
[2] | 205 | |
---|
[56] | 206 | Void readOutTrailingBits (); |
---|
| 207 | UChar getHeldBits () { return m_held_bits; } |
---|
| 208 | TComOutputBitstream& operator= (const TComOutputBitstream& src); |
---|
| 209 | UInt getTileMarkerLocationCount ( ) { return m_uiTileMarkerLocationCount ; } |
---|
| 210 | Void setTileMarkerLocationCount ( UInt i ) { m_uiTileMarkerLocationCount = i ; } |
---|
| 211 | UInt getTileMarkerLocation ( UInt i) { return m_puiTileMarkerLocation[i] ; } |
---|
| 212 | Void setTileMarkerLocation ( UInt i, UInt uiLOC ) { m_puiTileMarkerLocation[i] = uiLOC ; } |
---|
| 213 | UInt getByteLocation ( ) { return m_fifo_idx ; } |
---|
[2] | 214 | |
---|
| 215 | // Peek at bits in word-storage. Used in determining if we have completed reading of current bitstream and therefore slice in LCEC. |
---|
[56] | 216 | UInt peekBits (UInt uiBits) { unsigned tmp; pseudoRead(uiBits, tmp); return tmp; } |
---|
[2] | 217 | |
---|
| 218 | // utility functions |
---|
| 219 | unsigned read(unsigned numberOfBits) { UInt tmp; read(numberOfBits, tmp); return tmp; } |
---|
[56] | 220 | UInt readByte() { UInt tmp; readByte( tmp ); return tmp; } |
---|
| 221 | unsigned getNumBitsUntilByteAligned() { return m_num_held_bits & (0x7); } |
---|
| 222 | unsigned getNumBitsLeft() { return 8*((unsigned)m_fifo->size() - m_fifo_idx) + m_num_held_bits; } |
---|
| 223 | TComInputBitstream *extractSubstream( UInt uiNumBits ); // Read the nominated number of bits, and return as a bitstream. |
---|
| 224 | Void deleteFifo(); // Delete internal fifo of bitstream. |
---|
| 225 | #if !OL_FLUSH_ALIGN |
---|
| 226 | Void backupByte() { m_fifo_idx--; } |
---|
| 227 | #endif |
---|
| 228 | UInt getNumBitsRead() { return m_numBitsRead; } |
---|
| 229 | }; |
---|
[2] | 230 | |
---|
[56] | 231 | //! \} |
---|
[42] | 232 | |
---|
[2] | 233 | #endif |
---|