[2] | 1 | |
---|
| 2 | |
---|
| 3 | /** \file TComBitStream.h |
---|
| 4 | \brief class for handling bitstream (header) |
---|
| 5 | */ |
---|
| 6 | |
---|
| 7 | #ifndef __COMBITSTREAM__ |
---|
| 8 | #define __COMBITSTREAM__ |
---|
| 9 | |
---|
| 10 | #if _MSC_VER > 1000 |
---|
| 11 | #pragma once |
---|
| 12 | #endif // _MSC_VER > 1000 |
---|
| 13 | |
---|
| 14 | #include <stdio.h> |
---|
| 15 | #include <assert.h> |
---|
| 16 | #include "CommonDef.h" |
---|
| 17 | |
---|
| 18 | // ==================================================================================================================== |
---|
| 19 | // Class definition |
---|
| 20 | // ==================================================================================================================== |
---|
| 21 | |
---|
| 22 | /// pure virtual class for basic bit handling |
---|
| 23 | class TComBitIf |
---|
| 24 | { |
---|
| 25 | public: |
---|
| 26 | virtual Void writeAlignOne () {}; |
---|
| 27 | virtual Void write ( UInt uiBits, UInt uiNumberOfBits ) = 0; |
---|
| 28 | virtual Void resetBits () = 0; |
---|
| 29 | virtual UInt getNumberOfWrittenBits() const = 0; |
---|
| 30 | virtual ~TComBitIf() {} |
---|
| 31 | }; |
---|
| 32 | |
---|
| 33 | /// class for handling bitstream |
---|
| 34 | class TComBitstream : public TComBitIf |
---|
| 35 | { |
---|
| 36 | protected: |
---|
| 37 | UInt* m_apulStreamPacketBegin; |
---|
| 38 | UInt* m_pulStreamPacket; |
---|
| 39 | UInt m_uiBufSize; |
---|
| 40 | |
---|
| 41 | UInt m_uiBitSize; |
---|
| 42 | Int m_iValidBits; |
---|
| 43 | |
---|
| 44 | UInt m_ulCurrentBits; |
---|
| 45 | UInt m_uiBitsWritten; |
---|
| 46 | |
---|
| 47 | UInt m_uiDWordsLeft; |
---|
| 48 | UInt m_uiBitsLeft; |
---|
| 49 | UInt m_uiNextBits; |
---|
| 50 | |
---|
| 51 | UInt *m_auiSliceByteLocation, m_uiSliceCount; // used to skip over slice start codes in initParsingConvertPayloadToRBSP() |
---|
| 52 | UInt m_uiSliceProcessed; |
---|
| 53 | |
---|
| 54 | UInt xSwap ( UInt ui ) |
---|
| 55 | { |
---|
| 56 | // heiko.schwarz@hhi.fhg.de: support for BSD systems as proposed by Steffen Kamp [kamp@ient.rwth-aachen.de] |
---|
| 57 | #ifdef MSYS_BIG_ENDIAN |
---|
| 58 | return ui; |
---|
| 59 | #else |
---|
| 60 | UInt ul2; |
---|
| 61 | |
---|
| 62 | ul2 = ui>>24; |
---|
| 63 | ul2 |= (ui>>8) & 0x0000ff00; |
---|
| 64 | ul2 |= (ui<<8) & 0x00ff0000; |
---|
| 65 | ul2 |= ui<<24; |
---|
| 66 | |
---|
| 67 | return ul2; |
---|
| 68 | #endif |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | // read one word |
---|
| 72 | __inline Void xReadNextWord (); |
---|
| 73 | |
---|
| 74 | public: |
---|
| 75 | TComBitstream() {} |
---|
| 76 | virtual ~TComBitstream() {} |
---|
| 77 | |
---|
| 78 | // create / destroy |
---|
| 79 | Void create ( UInt uiSizeInBytes ); |
---|
| 80 | Void destroy (); |
---|
| 81 | |
---|
| 82 | // interface for encoding |
---|
| 83 | Void write ( UInt uiBits, UInt uiNumberOfBits ); |
---|
| 84 | Void writeAlignOne (); |
---|
| 85 | Void writeAlignZero (); |
---|
| 86 | Void convertRBSPToPayload( UInt uiStartPos = 0); |
---|
| 87 | // interface for decoding |
---|
| 88 | Void initParsingConvertPayloadToRBSP( const UInt uiBytesRead ); |
---|
| 89 | Void initParsing ( UInt uiNumBytes ); |
---|
| 90 | #if LCEC_INTRA_MODE || QC_LCEC_INTER_MODE |
---|
| 91 | Void pseudoRead ( UInt uiNumberOfBits, UInt& ruiBits ); |
---|
| 92 | #endif |
---|
| 93 | Void read ( UInt uiNumberOfBits, UInt& ruiBits ); |
---|
| 94 | Void readAlignOne (); |
---|
| 95 | UInt getSliceProcessed () { return m_uiSliceProcessed; } |
---|
| 96 | Void setSliceProcessed (UInt u) { m_uiSliceProcessed = u; } |
---|
| 97 | |
---|
| 98 | // interface for slice start-code positioning at encoder |
---|
| 99 | UInt getSliceCount () { return m_uiSliceCount; } |
---|
| 100 | UInt getSliceByteLocation ( UInt uiIdx ) { return m_auiSliceByteLocation[ uiIdx ]; } |
---|
| 101 | Void setSliceCount ( UInt uiCount ) { m_uiSliceCount = uiCount; } |
---|
| 102 | Void setSliceByteLocation ( UInt uiIdx, UInt uiCount ) { m_auiSliceByteLocation[ uiIdx ] = uiCount; } |
---|
| 103 | |
---|
| 104 | // memory allocation / deallocation interface for "slice location" bookkeeping |
---|
| 105 | Void allocateMemoryForSliceLocations ( UInt uiMaxNumOfSlices ); |
---|
| 106 | Void freeMemoryAllocatedForSliceLocations (); |
---|
| 107 | |
---|
| 108 | // Peek at bits in word-storage. Used in determining if we have completed reading of current bitstream and therefore slice in LCEC. |
---|
| 109 | UInt peekBits (UInt uiBits) { return( m_ulCurrentBits >> (32 - uiBits)); } |
---|
| 110 | |
---|
| 111 | // reset internal status |
---|
| 112 | Void resetBits () |
---|
| 113 | { |
---|
| 114 | m_uiBitSize = 0; |
---|
| 115 | m_iValidBits = 32; |
---|
| 116 | m_ulCurrentBits = 0; |
---|
| 117 | m_uiBitsWritten = 0; |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | // utility functions |
---|
| 121 | unsigned read(unsigned numberOfBits) { UInt tmp; read(numberOfBits, tmp); return tmp; } |
---|
| 122 | UInt* getStartStream() const { return m_apulStreamPacketBegin; } |
---|
| 123 | UInt* getBuffer() { return m_pulStreamPacket; } |
---|
| 124 | Int getBitsUntilByteAligned() { return m_iValidBits & (0x7); } |
---|
| 125 | Void setModeSbac() { m_uiBitsLeft = 8*((m_uiBitsLeft+7)/8); } // stop bit + trailing stuffing bits |
---|
| 126 | Bool isWordAligned() { return (0 == (m_iValidBits & (0x1f))); } |
---|
| 127 | UInt getNumberOfWrittenBits() const { return m_uiBitsWritten; } |
---|
| 128 | Void flushBuffer(); |
---|
| 129 | Void rewindStreamPacket() { m_pulStreamPacket = m_apulStreamPacketBegin; } |
---|
| 130 | UInt getBitsLeft() { return m_uiBitsLeft; } |
---|
| 131 | |
---|
| 132 | void insertAt(const TComBitstream& src, unsigned pos); |
---|
| 133 | }; |
---|
| 134 | |
---|
| 135 | #endif |
---|
| 136 | |
---|