source: SHVCSoftware/branches/0.1.1-bugfix/source/Lib/TLibEncoder/TEncBinCoderCABAC.cpp @ 1263

Last change on this file since 1263 was 2, checked in by seregin, 12 years ago

Initial import by Vadim Seregin <vseregin@…>

File size: 9.2 KB
Line 
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-2012, ITU/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 ITU/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/** \file     TEncBinCoderCABAC.cpp
35    \brief    binary entropy encoder of CABAC
36*/
37
38#include "TEncBinCoderCABAC.h"
39#include "TLibCommon/TComRom.h"
40
41
42//! \ingroup TLibEncoder
43//! \{
44
45
46TEncBinCABAC::TEncBinCABAC()
47: m_pcTComBitIf( 0 )
48, m_binCountIncrement( 0 )
49#if FAST_BIT_EST
50, m_fracBits( 0 )
51#endif
52{
53}
54
55TEncBinCABAC::~TEncBinCABAC()
56{
57}
58
59Void TEncBinCABAC::init( TComBitIf* pcTComBitIf )
60{
61  m_pcTComBitIf = pcTComBitIf;
62}
63
64Void TEncBinCABAC::uninit()
65{
66  m_pcTComBitIf = 0;
67}
68
69Void TEncBinCABAC::start()
70{
71  m_uiLow            = 0;
72  m_uiRange          = 510;
73  m_bitsLeft         = 23;
74  m_numBufferedBytes = 0;
75  m_bufferedByte     = 0xff;
76}
77
78Void TEncBinCABAC::finish()
79{
80  if ( m_uiLow >> ( 32 - m_bitsLeft ) )
81  {
82    //assert( m_numBufferedBytes > 0 );
83    //assert( m_bufferedByte != 0xff );
84    m_pcTComBitIf->write( m_bufferedByte + 1, 8 );
85    while ( m_numBufferedBytes > 1 )
86    {
87      m_pcTComBitIf->write( 0x00, 8 );
88      m_numBufferedBytes--;
89    }
90    m_uiLow -= 1 << ( 32 - m_bitsLeft );
91  }
92  else
93  {
94    if ( m_numBufferedBytes > 0 )
95    {
96      m_pcTComBitIf->write( m_bufferedByte, 8 );
97    }
98    while ( m_numBufferedBytes > 1 )
99    {
100      m_pcTComBitIf->write( 0xff, 8 );
101      m_numBufferedBytes--;
102    }   
103  }
104  m_pcTComBitIf->write( m_uiLow >> 8, 24 - m_bitsLeft );
105}
106
107Void TEncBinCABAC::flush()
108{
109  encodeBinTrm(1);
110  finish();
111  m_pcTComBitIf->write(1, 1);
112  m_pcTComBitIf->writeAlignZero();
113
114  start();
115}
116
117/** Reset BAC register and counter values.
118 * \returns Void
119 */
120Void TEncBinCABAC::resetBac()
121{
122  start();
123}
124
125/** Encode # of subsequent IPCM blocks.
126 * \param numSubseqIPCM
127 * \returns Void
128 */
129Void TEncBinCABAC::encodeNumSubseqIPCM( Int numSubseqIPCM )
130{
131  finish();
132  m_pcTComBitIf->write( 1, 1 ); // stop bit
133
134  m_pcTComBitIf->write( numSubseqIPCM ? 1 : 0, 1);
135
136  if ( numSubseqIPCM > 0)
137  {
138    Bool bCodeLast = ( 3 > numSubseqIPCM );
139
140    while( --numSubseqIPCM )
141    {
142      m_pcTComBitIf->write( 1, 1 );
143    }
144    if( bCodeLast )
145    {
146      m_pcTComBitIf->write( 0, 1 );
147    }
148  }
149}
150
151/** Encode PCM alignment zero bits.
152 * \returns Void
153 */
154Void TEncBinCABAC::encodePCMAlignBits()
155{
156  m_pcTComBitIf->writeAlignZero(); // pcm align zero
157}
158
159/** Write a PCM code.
160 * \param uiCode code value
161 * \param uiLength code bit-depth
162 * \returns Void
163 */
164Void TEncBinCABAC::xWritePCMCode(UInt uiCode, UInt uiLength)
165{
166  m_pcTComBitIf->write(uiCode, uiLength);
167}
168
169Void TEncBinCABAC::copyState( TEncBinIf* pcTEncBinIf )
170{
171  TEncBinCABAC* pcTEncBinCABAC = pcTEncBinIf->getTEncBinCABAC();
172  m_uiLow           = pcTEncBinCABAC->m_uiLow;
173  m_uiRange         = pcTEncBinCABAC->m_uiRange;
174  m_bitsLeft        = pcTEncBinCABAC->m_bitsLeft;
175  m_bufferedByte    = pcTEncBinCABAC->m_bufferedByte;
176  m_numBufferedBytes = pcTEncBinCABAC->m_numBufferedBytes;
177#if FAST_BIT_EST
178  m_fracBits = pcTEncBinCABAC->m_fracBits;
179#endif
180}
181
182Void TEncBinCABAC::resetBits()
183{
184  m_uiLow            = 0;
185  m_bitsLeft         = 23;
186  m_numBufferedBytes = 0;
187  m_bufferedByte     = 0xff;
188  if ( m_binCountIncrement )
189  {
190    m_uiBinsCoded = 0;
191  }
192#if FAST_BIT_EST
193  m_fracBits &= 32767;
194#endif
195}
196
197UInt TEncBinCABAC::getNumWrittenBits()
198{
199  return m_pcTComBitIf->getNumberOfWrittenBits() + 8 * m_numBufferedBytes + 23 - m_bitsLeft;
200}
201
202/**
203 * \brief Encode bin
204 *
205 * \param binValue   bin value
206 * \param rcCtxModel context model
207 */
208Void TEncBinCABAC::encodeBin( UInt binValue, ContextModel &rcCtxModel )
209{
210  {
211    DTRACE_CABAC_VL( g_nSymbolCounter++ )
212    DTRACE_CABAC_T( "\tstate=" )
213    DTRACE_CABAC_V( ( rcCtxModel.getState() << 1 ) + rcCtxModel.getMps() )
214    DTRACE_CABAC_T( "\tsymbol=" )
215    DTRACE_CABAC_V( binValue )
216    DTRACE_CABAC_T( "\n" )
217  }
218  m_uiBinsCoded += m_binCountIncrement;
219  rcCtxModel.setBinsCoded( 1 );
220 
221  UInt  uiLPS   = TComCABACTables::sm_aucLPSTable[ rcCtxModel.getState() ][ ( m_uiRange >> 6 ) & 3 ];
222  m_uiRange    -= uiLPS;
223 
224  if( binValue != rcCtxModel.getMps() )
225  {
226    Int numBits = TComCABACTables::sm_aucRenormTable[ uiLPS >> 3 ];
227    m_uiLow     = ( m_uiLow + m_uiRange ) << numBits;
228    m_uiRange   = uiLPS << numBits;
229    rcCtxModel.updateLPS();
230   
231    m_bitsLeft -= numBits;
232  }
233  else
234  {
235    rcCtxModel.updateMPS();
236    if ( m_uiRange >= 256 )
237    {
238      return;
239    }
240   
241    m_uiLow <<= 1;
242    m_uiRange <<= 1;
243    m_bitsLeft--;
244  }
245 
246  testAndWriteOut();
247}
248
249/**
250 * \brief Encode equiprobable bin
251 *
252 * \param binValue bin value
253 */
254Void TEncBinCABAC::encodeBinEP( UInt binValue )
255{
256  {
257    DTRACE_CABAC_VL( g_nSymbolCounter++ )
258    DTRACE_CABAC_T( "\tEPsymbol=" )
259    DTRACE_CABAC_V( binValue )
260    DTRACE_CABAC_T( "\n" )
261  }
262  m_uiBinsCoded += m_binCountIncrement;
263  m_uiLow <<= 1;
264  if( binValue )
265  {
266    m_uiLow += m_uiRange;
267  }
268  m_bitsLeft--;
269 
270  testAndWriteOut();
271}
272
273/**
274 * \brief Encode equiprobable bins
275 *
276 * \param binValues bin values
277 * \param numBins number of bins
278 */
279Void TEncBinCABAC::encodeBinsEP( UInt binValues, Int numBins )
280{
281  m_uiBinsCoded += numBins & -m_binCountIncrement;
282 
283  for ( Int i = 0; i < numBins; i++ )
284  {
285    DTRACE_CABAC_VL( g_nSymbolCounter++ )
286    DTRACE_CABAC_T( "\tEPsymbol=" )
287    DTRACE_CABAC_V( ( binValues >> ( numBins - 1 - i ) ) & 1 )
288    DTRACE_CABAC_T( "\n" )
289  }
290 
291  while ( numBins > 8 )
292  {
293    numBins -= 8;
294    UInt pattern = binValues >> numBins; 
295    m_uiLow <<= 8;
296    m_uiLow += m_uiRange * pattern;
297    binValues -= pattern << numBins;
298    m_bitsLeft -= 8;
299   
300    testAndWriteOut();
301  }
302 
303  m_uiLow <<= numBins;
304  m_uiLow += m_uiRange * binValues;
305  m_bitsLeft -= numBins;
306 
307  testAndWriteOut();
308}
309
310/**
311 * \brief Encode terminating bin
312 *
313 * \param binValue bin value
314 */
315Void TEncBinCABAC::encodeBinTrm( UInt binValue )
316{
317  m_uiBinsCoded += m_binCountIncrement;
318  m_uiRange -= 2;
319  if( binValue )
320  {
321    m_uiLow  += m_uiRange;
322    m_uiLow <<= 7;
323    m_uiRange = 2 << 7;
324    m_bitsLeft -= 7;
325  }
326  else if ( m_uiRange >= 256 )
327  {
328    return;
329  }
330  else
331  {
332    m_uiLow   <<= 1;
333    m_uiRange <<= 1;
334    m_bitsLeft--;   
335  }
336 
337  testAndWriteOut();
338}
339
340Void TEncBinCABAC::testAndWriteOut()
341{
342  if ( m_bitsLeft < 12 )
343  {
344    writeOut();
345  }
346}
347
348/**
349 * \brief Move bits from register into bitstream
350 */
351Void TEncBinCABAC::writeOut()
352{
353  UInt leadByte = m_uiLow >> (24 - m_bitsLeft);
354  m_bitsLeft += 8;
355  m_uiLow &= 0xffffffffu >> m_bitsLeft;
356 
357  if ( leadByte == 0xff )
358  {
359    m_numBufferedBytes++;
360  }
361  else
362  {
363    if ( m_numBufferedBytes > 0 )
364    {
365      UInt carry = leadByte >> 8;
366      UInt byte = m_bufferedByte + carry;
367      m_bufferedByte = leadByte & 0xff;
368      m_pcTComBitIf->write( byte, 8 );
369     
370      byte = ( 0xff + carry ) & 0xff;
371      while ( m_numBufferedBytes > 1 )
372      {
373        m_pcTComBitIf->write( byte, 8 );
374        m_numBufferedBytes--;
375      }
376    }
377    else
378    {
379      m_numBufferedBytes = 1;
380      m_bufferedByte = leadByte;
381    }     
382  }   
383}
384
385/** flush bits when CABAC termination
386  * \param [in] bEnd true means this flushing happens at the end of RBSP. No need to encode stop bit
387  */
388Void TEncBinCABAC::encodeFlush(Bool bEnd)
389{
390  m_uiRange = 2;
391
392  m_uiLow  += 2;
393  m_uiLow <<= 7;
394  m_uiRange = 2 << 7;
395  m_bitsLeft -= 7;
396  testAndWriteOut();
397  finish();
398
399  if(!bEnd)
400  {
401    m_pcTComBitIf->write( 1, 1 ); // stop bit
402  }
403}
404
405//! \}
Note: See TracBrowser for help on using the repository browser.