source: 3DVCSoftware/trunk/source/Lib/TLibEncoder/TEncBinCoderCABAC.cpp @ 271

Last change on this file since 271 was 56, checked in by hschwarz, 13 years ago

updated trunk (move to HM6.1)

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