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 | UInt xSwap ( UInt ui ) |
---|
88 | { |
---|
89 | // heiko.schwarz@hhi.fhg.de: support for BSD systems as proposed by Steffen Kamp [kamp@ient.rwth-aachen.de] |
---|
90 | #ifdef MSYS_BIG_ENDIAN |
---|
91 | return ui; |
---|
92 | #else |
---|
93 | UInt ul2; |
---|
94 | |
---|
95 | ul2 = ui>>24; |
---|
96 | ul2 |= (ui>>8) & 0x0000ff00; |
---|
97 | ul2 |= (ui<<8) & 0x00ff0000; |
---|
98 | ul2 |= ui<<24; |
---|
99 | |
---|
100 | return ul2; |
---|
101 | #endif |
---|
102 | } |
---|
103 | |
---|
104 | // read one word |
---|
105 | __inline Void xReadNextWord (); |
---|
106 | |
---|
107 | public: |
---|
108 | TComBitstream() {} |
---|
109 | virtual ~TComBitstream() {} |
---|
110 | |
---|
111 | // create / destroy |
---|
112 | Void create ( UInt uiSizeInBytes ); |
---|
113 | Void destroy (); |
---|
114 | |
---|
115 | // interface for encoding |
---|
116 | Void write ( UInt uiBits, UInt uiNumberOfBits ); |
---|
117 | Void writeAlignOne (); |
---|
118 | Void writeAlignZero (); |
---|
119 | Void convertRBSPToPayload( UInt uiStartPos = 0); |
---|
120 | // interface for decoding |
---|
121 | Void initParsingConvertPayloadToRBSP( const UInt uiBytesRead ); |
---|
122 | Void initParsing ( UInt uiNumBytes ); |
---|
123 | #if LCEC_INTRA_MODE || QC_LCEC_INTER_MODE |
---|
124 | Void pseudoRead ( UInt uiNumberOfBits, UInt& ruiBits ); |
---|
125 | #endif |
---|
126 | Void read ( UInt uiNumberOfBits, UInt& ruiBits ); |
---|
127 | Void readAlignOne (); |
---|
128 | UInt getSliceProcessed () { return m_uiSliceProcessed; } |
---|
129 | Void setSliceProcessed (UInt u) { m_uiSliceProcessed = u; } |
---|
130 | |
---|
131 | // interface for slice start-code positioning at encoder |
---|
132 | UInt getSliceCount () { return m_uiSliceCount; } |
---|
133 | UInt getSliceByteLocation ( UInt uiIdx ) { return m_auiSliceByteLocation[ uiIdx ]; } |
---|
134 | Void setSliceCount ( UInt uiCount ) { m_uiSliceCount = uiCount; } |
---|
135 | Void setSliceByteLocation ( UInt uiIdx, UInt uiCount ) { m_auiSliceByteLocation[ uiIdx ] = uiCount; } |
---|
136 | |
---|
137 | // memory allocation / deallocation interface for "slice location" bookkeeping |
---|
138 | Void allocateMemoryForSliceLocations ( UInt uiMaxNumOfSlices ); |
---|
139 | Void freeMemoryAllocatedForSliceLocations (); |
---|
140 | |
---|
141 | // Peek at bits in word-storage. Used in determining if we have completed reading of current bitstream and therefore slice in LCEC. |
---|
142 | UInt peekBits (UInt uiBits) { return( m_ulCurrentBits >> (32 - uiBits)); } |
---|
143 | |
---|
144 | // reset internal status |
---|
145 | Void resetBits () |
---|
146 | { |
---|
147 | m_uiBitSize = 0; |
---|
148 | m_iValidBits = 32; |
---|
149 | m_ulCurrentBits = 0; |
---|
150 | m_uiBitsWritten = 0; |
---|
151 | } |
---|
152 | |
---|
153 | // utility functions |
---|
154 | unsigned read(unsigned numberOfBits) { UInt tmp; read(numberOfBits, tmp); return tmp; } |
---|
155 | UInt* getStartStream() const { return m_apulStreamPacketBegin; } |
---|
156 | UInt* getBuffer() { return m_pulStreamPacket; } |
---|
157 | Int getBitsUntilByteAligned() { return m_iValidBits & (0x7); } |
---|
158 | Void setModeSbac() { m_uiBitsLeft = 8*((m_uiBitsLeft+7)/8); } // stop bit + trailing stuffing bits |
---|
159 | Bool isWordAligned() { return (0 == (m_iValidBits & (0x1f))); } |
---|
160 | UInt getNumberOfWrittenBits() const { return m_uiBitsWritten; } |
---|
161 | Void flushBuffer(); |
---|
162 | Void rewindStreamPacket() { m_pulStreamPacket = m_apulStreamPacketBegin; } |
---|
163 | UInt getBitsLeft() { return m_uiBitsLeft; } |
---|
164 | |
---|
165 | void insertAt(const TComBitstream& src, unsigned pos); |
---|
166 | }; |
---|
167 | |
---|
168 | #endif |
---|
169 | |
---|