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-2011, 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 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 | |
---|
36 | /** \file TComBitStream.h |
---|
37 | \brief class for handling bitstream (header) |
---|
38 | */ |
---|
39 | |
---|
40 | #ifndef __COMBITSTREAM__ |
---|
41 | #define __COMBITSTREAM__ |
---|
42 | |
---|
43 | #if _MSC_VER > 1000 |
---|
44 | #pragma once |
---|
45 | #endif // _MSC_VER > 1000 |
---|
46 | |
---|
47 | #include <stdio.h> |
---|
48 | #include <assert.h> |
---|
49 | #include "CommonDef.h" |
---|
50 | |
---|
51 | // ==================================================================================================================== |
---|
52 | // Class definition |
---|
53 | // ==================================================================================================================== |
---|
54 | |
---|
55 | /// pure virtual class for basic bit handling |
---|
56 | class TComBitIf |
---|
57 | { |
---|
58 | public: |
---|
59 | virtual Void writeAlignOne () {}; |
---|
60 | virtual Void write ( UInt uiBits, UInt uiNumberOfBits ) = 0; |
---|
61 | virtual Void resetBits () = 0; |
---|
62 | virtual UInt getNumberOfWrittenBits() const = 0; |
---|
63 | virtual ~TComBitIf() {} |
---|
64 | }; |
---|
65 | |
---|
66 | /// class for handling bitstream |
---|
67 | class TComBitstream : public TComBitIf |
---|
68 | { |
---|
69 | protected: |
---|
70 | UInt* m_apulStreamPacketBegin; |
---|
71 | UInt* m_pulStreamPacket; |
---|
72 | UInt m_uiBufSize; |
---|
73 | |
---|
74 | UInt m_uiBitSize; |
---|
75 | Int m_iValidBits; |
---|
76 | |
---|
77 | UInt m_ulCurrentBits; |
---|
78 | UInt m_uiBitsWritten; |
---|
79 | |
---|
80 | UInt m_uiDWordsLeft; |
---|
81 | UInt m_uiBitsLeft; |
---|
82 | UInt m_uiNextBits; |
---|
83 | |
---|
84 | UInt *m_auiSliceByteLocation, m_uiSliceCount; // used to skip over slice start codes in initParsingConvertPayloadToRBSP() |
---|
85 | UInt m_uiSliceProcessed; |
---|
86 | |
---|
87 | #if BITSTREAM_EXTRACTION |
---|
88 | UInt* m_apulPacketPayloadBuffer; |
---|
89 | UInt m_uiPacketPayloadSize; |
---|
90 | #endif |
---|
91 | |
---|
92 | UInt xSwap ( UInt ui ) |
---|
93 | { |
---|
94 | // heiko.schwarz@hhi.fhg.de: support for BSD systems as proposed by Steffen Kamp [kamp@ient.rwth-aachen.de] |
---|
95 | #ifdef MSYS_BIG_ENDIAN |
---|
96 | return ui; |
---|
97 | #else |
---|
98 | UInt ul2; |
---|
99 | |
---|
100 | ul2 = ui>>24; |
---|
101 | ul2 |= (ui>>8) & 0x0000ff00; |
---|
102 | ul2 |= (ui<<8) & 0x00ff0000; |
---|
103 | ul2 |= ui<<24; |
---|
104 | |
---|
105 | return ul2; |
---|
106 | #endif |
---|
107 | } |
---|
108 | |
---|
109 | // read one word |
---|
110 | __inline Void xReadNextWord (); |
---|
111 | |
---|
112 | public: |
---|
113 | TComBitstream() {} |
---|
114 | virtual ~TComBitstream() {} |
---|
115 | |
---|
116 | // create / destroy |
---|
117 | Void create ( UInt uiSizeInBytes ); |
---|
118 | Void destroy (); |
---|
119 | |
---|
120 | // interface for encoding |
---|
121 | Void write ( UInt uiBits, UInt uiNumberOfBits ); |
---|
122 | Void writeAlignOne (); |
---|
123 | Void writeAlignZero (); |
---|
124 | Void convertRBSPToPayload( UInt uiStartPos = 0); |
---|
125 | // interface for decoding |
---|
126 | Void initParsingConvertPayloadToRBSP( const UInt uiBytesRead ); |
---|
127 | Void initParsing ( UInt uiNumBytes ); |
---|
128 | #if LCEC_INTRA_MODE || QC_LCEC_INTER_MODE |
---|
129 | Void pseudoRead ( UInt uiNumberOfBits, UInt& ruiBits ); |
---|
130 | #endif |
---|
131 | Void read ( UInt uiNumberOfBits, UInt& ruiBits ); |
---|
132 | Void readAlignOne (); |
---|
133 | UInt getSliceProcessed () { return m_uiSliceProcessed; } |
---|
134 | Void setSliceProcessed (UInt u) { m_uiSliceProcessed = u; } |
---|
135 | |
---|
136 | // interface for slice start-code positioning at encoder |
---|
137 | UInt getSliceCount () { return m_uiSliceCount; } |
---|
138 | UInt getSliceByteLocation ( UInt uiIdx ) { return m_auiSliceByteLocation[ uiIdx ]; } |
---|
139 | Void setSliceCount ( UInt uiCount ) { m_uiSliceCount = uiCount; } |
---|
140 | Void setSliceByteLocation ( UInt uiIdx, UInt uiCount ) { m_auiSliceByteLocation[ uiIdx ] = uiCount; } |
---|
141 | |
---|
142 | // memory allocation / deallocation interface for "slice location" bookkeeping |
---|
143 | Void allocateMemoryForSliceLocations ( UInt uiMaxNumOfSlices ); |
---|
144 | Void freeMemoryAllocatedForSliceLocations (); |
---|
145 | |
---|
146 | // Peek at bits in word-storage. Used in determining if we have completed reading of current bitstream and therefore slice in LCEC. |
---|
147 | UInt peekBits (UInt uiBits) { return( m_ulCurrentBits >> (32 - uiBits)); } |
---|
148 | |
---|
149 | // reset internal status |
---|
150 | Void resetBits () |
---|
151 | { |
---|
152 | m_uiBitSize = 0; |
---|
153 | m_iValidBits = 32; |
---|
154 | m_ulCurrentBits = 0; |
---|
155 | m_uiBitsWritten = 0; |
---|
156 | } |
---|
157 | |
---|
158 | // utility functions |
---|
159 | unsigned read(unsigned numberOfBits) { UInt tmp; read(numberOfBits, tmp); return tmp; } |
---|
160 | UInt* getStartStream() const { return m_apulStreamPacketBegin; } |
---|
161 | UInt* getBuffer() { return m_pulStreamPacket; } |
---|
162 | Int getBitsUntilByteAligned() { return m_iValidBits & (0x7); } |
---|
163 | Void setModeSbac() { m_uiBitsLeft = 8*((m_uiBitsLeft+7)/8); } // stop bit + trailing stuffing bits |
---|
164 | Bool isWordAligned() { return (0 == (m_iValidBits & (0x1f))); } |
---|
165 | UInt getNumberOfWrittenBits() const { return m_uiBitsWritten; } |
---|
166 | Void flushBuffer(); |
---|
167 | Void rewindStreamPacket() { m_pulStreamPacket = m_apulStreamPacketBegin; } |
---|
168 | UInt getBitsLeft() { return m_uiBitsLeft; } |
---|
169 | |
---|
170 | void insertAt(const TComBitstream& src, unsigned pos); |
---|
171 | |
---|
172 | #if BITSTREAM_EXTRACTION |
---|
173 | UInt reinitParsing(); |
---|
174 | #endif |
---|
175 | }; |
---|
176 | |
---|
177 | #endif |
---|
178 | |
---|