1 | |
---|
2 | |
---|
3 | /** \file TEncBinCoderCABAC.cpp |
---|
4 | \brief binary entropy encoder of CABAC |
---|
5 | */ |
---|
6 | |
---|
7 | #include "TEncBinCoderCABAC.h" |
---|
8 | |
---|
9 | |
---|
10 | TEncBinCABAC::TEncBinCABAC() |
---|
11 | : m_pcTComBitIf( 0 ) |
---|
12 | , m_bBinCountingEnabled(0) |
---|
13 | { |
---|
14 | } |
---|
15 | |
---|
16 | TEncBinCABAC::~TEncBinCABAC() |
---|
17 | { |
---|
18 | } |
---|
19 | |
---|
20 | Void |
---|
21 | TEncBinCABAC::init( TComBitIf* pcTComBitIf ) |
---|
22 | { |
---|
23 | m_pcTComBitIf = pcTComBitIf; |
---|
24 | } |
---|
25 | |
---|
26 | Void |
---|
27 | TEncBinCABAC::uninit() |
---|
28 | { |
---|
29 | m_pcTComBitIf = 0; |
---|
30 | } |
---|
31 | |
---|
32 | Void |
---|
33 | TEncBinCABAC::start() |
---|
34 | { |
---|
35 | m_uiLow = 0; |
---|
36 | m_uiRange = 510; |
---|
37 | m_uiBitsToFollow = 0; |
---|
38 | m_uiByte = 0; |
---|
39 | m_uiBitsLeft = 9; |
---|
40 | } |
---|
41 | |
---|
42 | Void |
---|
43 | TEncBinCABAC::finish() |
---|
44 | { |
---|
45 | xWriteBitAndBitsToFollow( ( m_uiLow >> 9 ) & 1 ); |
---|
46 | xWriteBit ( ( m_uiLow >> 8 ) & 1 ); |
---|
47 | // xWriteBit ( 1 ); // stop bit, already written in TEncGOP::compressGOP |
---|
48 | if( 8 - m_uiBitsLeft != 0 ) |
---|
49 | { |
---|
50 | m_pcTComBitIf->write ( m_uiByte, 8 - m_uiBitsLeft ); |
---|
51 | } |
---|
52 | } |
---|
53 | |
---|
54 | Void |
---|
55 | TEncBinCABAC::copyState( TEncBinIf* pcTEncBinIf ) |
---|
56 | { |
---|
57 | TEncBinCABAC* pcTEncBinCABAC = pcTEncBinIf->getTEncBinCABAC(); |
---|
58 | m_uiLow = pcTEncBinCABAC->m_uiLow; |
---|
59 | m_uiRange = pcTEncBinCABAC->m_uiRange; |
---|
60 | m_uiBitsToFollow = pcTEncBinCABAC->m_uiBitsToFollow; |
---|
61 | m_uiByte = pcTEncBinCABAC->m_uiByte; |
---|
62 | m_uiBitsLeft = pcTEncBinCABAC->m_uiBitsLeft; |
---|
63 | } |
---|
64 | |
---|
65 | Void |
---|
66 | TEncBinCABAC::resetBits() |
---|
67 | { |
---|
68 | m_uiLow &= 255; |
---|
69 | m_uiBitsToFollow = 0; |
---|
70 | m_uiByte = 0; |
---|
71 | m_uiBitsLeft = 9; |
---|
72 | } |
---|
73 | |
---|
74 | UInt |
---|
75 | TEncBinCABAC::getNumWrittenBits() |
---|
76 | { |
---|
77 | return m_pcTComBitIf->getNumberOfWrittenBits() + 8 + m_uiBitsToFollow - m_uiBitsLeft + 1; |
---|
78 | } |
---|
79 | |
---|
80 | Void |
---|
81 | TEncBinCABAC::encodeBin( UInt uiBin, ContextModel &rcCtxModel ) |
---|
82 | { |
---|
83 | { |
---|
84 | DTRACE_CABAC_V( g_nSymbolCounter++ ) |
---|
85 | DTRACE_CABAC_T( "\tstate=" ) |
---|
86 | DTRACE_CABAC_V( ( rcCtxModel.getState() << 1 ) + rcCtxModel.getMps() ) |
---|
87 | DTRACE_CABAC_T( "\tsymbol=" ) |
---|
88 | DTRACE_CABAC_V( uiBin ) |
---|
89 | DTRACE_CABAC_T( "\n" ) |
---|
90 | } |
---|
91 | if (m_bBinCountingEnabled) |
---|
92 | { |
---|
93 | m_uiBinsCoded++; |
---|
94 | } |
---|
95 | UInt uiLPS = TComCABACTables::sm_aucLPSTable[ rcCtxModel.getState() ][ ( m_uiRange >> 6 ) & 3 ]; |
---|
96 | m_uiRange -= uiLPS; |
---|
97 | if( uiBin != rcCtxModel.getMps() ) |
---|
98 | { |
---|
99 | m_uiLow += m_uiRange; |
---|
100 | m_uiRange = uiLPS; |
---|
101 | rcCtxModel.updateLPS(); |
---|
102 | } |
---|
103 | else |
---|
104 | { |
---|
105 | rcCtxModel.updateMPS(); |
---|
106 | } |
---|
107 | while( m_uiRange < 256 ) |
---|
108 | { |
---|
109 | if( m_uiLow >= 512 ) |
---|
110 | { |
---|
111 | xWriteBitAndBitsToFollow( 1 ); |
---|
112 | m_uiLow -= 512; |
---|
113 | } |
---|
114 | else if( m_uiLow < 256 ) |
---|
115 | { |
---|
116 | xWriteBitAndBitsToFollow( 0 ); |
---|
117 | } |
---|
118 | else |
---|
119 | { |
---|
120 | m_uiBitsToFollow++; |
---|
121 | m_uiLow -= 256; |
---|
122 | } |
---|
123 | m_uiLow <<= 1; |
---|
124 | m_uiRange <<= 1; |
---|
125 | } |
---|
126 | } |
---|
127 | |
---|
128 | Void |
---|
129 | TEncBinCABAC::encodeBinEP( UInt uiBin ) |
---|
130 | { |
---|
131 | { |
---|
132 | DTRACE_CABAC_V( g_nSymbolCounter++ ) |
---|
133 | DTRACE_CABAC_T( "\tEPsymbol=" ) |
---|
134 | DTRACE_CABAC_V( uiBin ) |
---|
135 | DTRACE_CABAC_T( "\n" ) |
---|
136 | } |
---|
137 | if (m_bBinCountingEnabled) |
---|
138 | { |
---|
139 | m_uiBinsCoded++; |
---|
140 | } |
---|
141 | m_uiLow <<= 1; |
---|
142 | if( uiBin ) |
---|
143 | { |
---|
144 | m_uiLow += m_uiRange; |
---|
145 | } |
---|
146 | if( m_uiLow >= 1024 ) |
---|
147 | { |
---|
148 | xWriteBitAndBitsToFollow( 1 ); |
---|
149 | m_uiLow -= 1024; |
---|
150 | } |
---|
151 | else if( m_uiLow < 512 ) |
---|
152 | { |
---|
153 | xWriteBitAndBitsToFollow( 0 ); |
---|
154 | } |
---|
155 | else |
---|
156 | { |
---|
157 | m_uiBitsToFollow++; |
---|
158 | m_uiLow -= 512; |
---|
159 | } |
---|
160 | } |
---|
161 | |
---|
162 | Void |
---|
163 | TEncBinCABAC::encodeBinTrm( UInt uiBin ) |
---|
164 | { |
---|
165 | if (m_bBinCountingEnabled) |
---|
166 | { |
---|
167 | m_uiBinsCoded++; |
---|
168 | } |
---|
169 | m_uiRange -= 2; |
---|
170 | if( uiBin ) |
---|
171 | { |
---|
172 | m_uiLow += m_uiRange; |
---|
173 | m_uiRange = 2; |
---|
174 | } |
---|
175 | while( m_uiRange < 256 ) |
---|
176 | { |
---|
177 | if( m_uiLow >= 512 ) |
---|
178 | { |
---|
179 | xWriteBitAndBitsToFollow( 1 ); |
---|
180 | m_uiLow -= 512; |
---|
181 | } |
---|
182 | else if( m_uiLow < 256 ) |
---|
183 | { |
---|
184 | xWriteBitAndBitsToFollow( 0 ); |
---|
185 | } |
---|
186 | else |
---|
187 | { |
---|
188 | m_uiBitsToFollow++; |
---|
189 | m_uiLow -= 256; |
---|
190 | } |
---|
191 | m_uiLow <<= 1; |
---|
192 | m_uiRange <<= 1; |
---|
193 | } |
---|
194 | } |
---|
195 | |
---|
196 | Void |
---|
197 | TEncBinCABAC::xWriteBit( UInt uiBit ) |
---|
198 | { |
---|
199 | m_uiByte += m_uiByte + uiBit; |
---|
200 | if( ! --m_uiBitsLeft ) |
---|
201 | { |
---|
202 | m_pcTComBitIf->write( m_uiByte, 8 ); |
---|
203 | m_uiBitsLeft = 8; |
---|
204 | m_uiByte = 0; |
---|
205 | } |
---|
206 | } |
---|
207 | |
---|
208 | Void |
---|
209 | TEncBinCABAC::xWriteBitAndBitsToFollow( UInt uiBit ) |
---|
210 | { |
---|
211 | xWriteBit( uiBit ); |
---|
212 | uiBit = 1 - uiBit; |
---|
213 | while( m_uiBitsToFollow > 0 ) |
---|
214 | { |
---|
215 | m_uiBitsToFollow--; |
---|
216 | xWriteBit( uiBit ); |
---|
217 | } |
---|
218 | } |
---|