source: 3DVCSoftware/branches/HTM-16.2-dev/source/Lib/TLibEncoder/TEncBinCoderCABAC.cpp @ 1412

Last change on this file since 1412 was 1412, checked in by tech, 7 years ago
  • Update HM-16.18
  • Cleanups
  • Encoder Extension

-- Representation formats
-- Parameter set sharing
-- GOP configuration

  • Property svn:eol-style set to native
File size: 10.4 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
[1313]4 * granted under this license.
[5]5 *
[1412]6 * Copyright (c) 2010-2017, 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"
[1313]40#include "TLibCommon/Debug.h"
[2]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;
[1313]76#if FAST_BIT_EST
77  m_fracBits         = 0;
78#endif
[2]79}
80
[56]81Void TEncBinCABAC::finish()
[2]82{
[56]83  if ( m_uiLow >> ( 32 - m_bitsLeft ) )
[2]84  {
[56]85    //assert( m_numBufferedBytes > 0 );
86    //assert( m_bufferedByte != 0xff );
87    m_pcTComBitIf->write( m_bufferedByte + 1, 8 );
88    while ( m_numBufferedBytes > 1 )
89    {
90      m_pcTComBitIf->write( 0x00, 8 );
91      m_numBufferedBytes--;
92    }
93    m_uiLow -= 1 << ( 32 - m_bitsLeft );
[2]94  }
[56]95  else
96  {
97    if ( m_numBufferedBytes > 0 )
98    {
99      m_pcTComBitIf->write( m_bufferedByte, 8 );
100    }
101    while ( m_numBufferedBytes > 1 )
102    {
103      m_pcTComBitIf->write( 0xff, 8 );
104      m_numBufferedBytes--;
[1313]105    }
[56]106  }
107  m_pcTComBitIf->write( m_uiLow >> 8, 24 - m_bitsLeft );
[2]108}
109
[56]110Void TEncBinCABAC::flush()
[2]111{
[56]112  encodeBinTrm(1);
113  finish();
114  m_pcTComBitIf->write(1, 1);
115  m_pcTComBitIf->writeAlignZero();
116
117  start();
118}
119
120/** Reset BAC register and counter values.
121 * \returns Void
122 */
123Void TEncBinCABAC::resetBac()
124{
125  start();
126}
127
128/** Encode PCM alignment zero bits.
129 * \returns Void
130 */
131Void TEncBinCABAC::encodePCMAlignBits()
132{
[608]133  finish();
134  m_pcTComBitIf->write(1, 1);
[56]135  m_pcTComBitIf->writeAlignZero(); // pcm align zero
136}
137
138/** Write a PCM code.
139 * \param uiCode code value
140 * \param uiLength code bit-depth
141 * \returns Void
142 */
143Void TEncBinCABAC::xWritePCMCode(UInt uiCode, UInt uiLength)
144{
145  m_pcTComBitIf->write(uiCode, uiLength);
146}
147
[1313]148Void TEncBinCABAC::copyState( const TEncBinIf* pcTEncBinIf )
[56]149{
[1313]150  const TEncBinCABAC* pcTEncBinCABAC = pcTEncBinIf->getTEncBinCABAC();
[2]151  m_uiLow           = pcTEncBinCABAC->m_uiLow;
152  m_uiRange         = pcTEncBinCABAC->m_uiRange;
[56]153  m_bitsLeft        = pcTEncBinCABAC->m_bitsLeft;
154  m_bufferedByte    = pcTEncBinCABAC->m_bufferedByte;
155  m_numBufferedBytes = pcTEncBinCABAC->m_numBufferedBytes;
156#if FAST_BIT_EST
157  m_fracBits = pcTEncBinCABAC->m_fracBits;
[1321]158#if NH_MV
[1313]159  D_PRINT_INDENT(g_traceEncFracBits,  "CopyState " + n2s(m_fracBits) );   
[56]160#endif
[1321]161#endif
[2]162}
163
[56]164Void TEncBinCABAC::resetBits()
[2]165{
[56]166  m_uiLow            = 0;
167  m_bitsLeft         = 23;
168  m_numBufferedBytes = 0;
169  m_bufferedByte     = 0xff;
170  if ( m_binCountIncrement )
171  {
172    m_uiBinsCoded = 0;
173  }
174#if FAST_BIT_EST
[1321]175#if NH_MV
[1313]176  D_PRINT_INDENT( g_traceEncFracBits, "Reset Bits Before" + n2s(m_fracBits) );
[1321]177#endif
[56]178  m_fracBits &= 32767;
[1321]179#if NH_MV
[1313]180  D_PRINT_INDENT( g_traceEncFracBits, "Reset Bits " + n2s(m_fracBits) ); 
[56]181#endif
[1321]182#endif
[2]183}
184
[56]185UInt TEncBinCABAC::getNumWrittenBits()
[2]186{
[56]187  return m_pcTComBitIf->getNumberOfWrittenBits() + 8 * m_numBufferedBytes + 23 - m_bitsLeft;
[2]188}
189
[56]190/**
191 * \brief Encode bin
192 *
193 * \param binValue   bin value
194 * \param rcCtxModel context model
195 */
196Void TEncBinCABAC::encodeBin( UInt binValue, ContextModel &rcCtxModel )
[2]197{
[1313]198#if !NH_MV
199  //{
200  //  DTRACE_CABAC_VL( g_nSymbolCounter++ )
201  //  DTRACE_CABAC_T( "\tstate=" )
202  //  DTRACE_CABAC_V( ( rcCtxModel.getState() << 1 ) + rcCtxModel.getMps() )
203  //  DTRACE_CABAC_T( "\tsymbol=" )
204  //  DTRACE_CABAC_V( binValue )
205  //  DTRACE_CABAC_T( "\n" )
206  //}
[608]207#endif
[1313]208#if DEBUG_CABAC_BINS
209  const UInt startingRange = m_uiRange;
210#endif
211
[56]212  m_uiBinsCoded += m_binCountIncrement;
213  rcCtxModel.setBinsCoded( 1 );
[1313]214
[2]215  UInt  uiLPS   = TComCABACTables::sm_aucLPSTable[ rcCtxModel.getState() ][ ( m_uiRange >> 6 ) & 3 ];
216  m_uiRange    -= uiLPS;
[1313]217
[56]218  if( binValue != rcCtxModel.getMps() )
[2]219  {
[56]220    Int numBits = TComCABACTables::sm_aucRenormTable[ uiLPS >> 3 ];
221    m_uiLow     = ( m_uiLow + m_uiRange ) << numBits;
222    m_uiRange   = uiLPS << numBits;
[2]223    rcCtxModel.updateLPS();
[56]224    m_bitsLeft -= numBits;
[1313]225    testAndWriteOut();
[2]226  }
227  else
228  {
229    rcCtxModel.updateMPS();
[1313]230
231    if ( m_uiRange < 256 )
[2]232    {
[1313]233      m_uiLow <<= 1;
234      m_uiRange <<= 1;
235      m_bitsLeft--;
236      testAndWriteOut();
[2]237    }
238  }
[1313]239
240#if DEBUG_CABAC_BINS
241  if ((g_debugCounter + debugCabacBinWindow) >= debugCabacBinTargetLine)
242  {
243    std::cout << g_debugCounter << ": coding bin value " << binValue << ", range = [" << startingRange << "->" << m_uiRange << "]\n";
244  }
245
246  if (g_debugCounter >= debugCabacBinTargetLine)
247  {
[1386]248    UChar breakPointThis;
[1313]249    breakPointThis = 7;
250  }
251  if (g_debugCounter >= (debugCabacBinTargetLine + debugCabacBinWindow))
252  {
253    exit(0);
254  }
255
256  g_debugCounter++;
257#endif
[2]258}
259
[56]260/**
261 * \brief Encode equiprobable bin
262 *
263 * \param binValue bin value
264 */
265Void TEncBinCABAC::encodeBinEP( UInt binValue )
[2]266{
[1313]267  if (false)
[2]268  {
[1313]269#if !NH_MV
[56]270    DTRACE_CABAC_VL( g_nSymbolCounter++ )
[2]271    DTRACE_CABAC_T( "\tEPsymbol=" )
[56]272    DTRACE_CABAC_V( binValue )
[2]273    DTRACE_CABAC_T( "\n" )
[608]274#endif
[2]275  }
[1313]276
[56]277  m_uiBinsCoded += m_binCountIncrement;
[1313]278
279  if (m_uiRange == 256)
280  {
281    encodeAlignedBinsEP(binValue, 1);
282    return;
283  }
284
[2]285  m_uiLow <<= 1;
[56]286  if( binValue )
[2]287  {
288    m_uiLow += m_uiRange;
289  }
[56]290  m_bitsLeft--;
[1313]291
[56]292  testAndWriteOut();
293}
294
295/**
296 * \brief Encode equiprobable bins
297 *
298 * \param binValues bin values
299 * \param numBins number of bins
300 */
301Void TEncBinCABAC::encodeBinsEP( UInt binValues, Int numBins )
302{
303  m_uiBinsCoded += numBins & -m_binCountIncrement;
[1313]304
305  if (false)
[2]306  {
[1313]307    for ( Int i = 0; i < numBins; i++ )
308    {
309#if !NH_MV
310      DTRACE_CABAC_VL( g_nSymbolCounter++ )
311      DTRACE_CABAC_T( "\tEPsymbol=" )
312      DTRACE_CABAC_V( ( binValues >> ( numBins - 1 - i ) ) & 1 )
313      DTRACE_CABAC_T( "\n" )
[608]314#endif
[1313]315    }
[2]316  }
[1313]317
318  if (m_uiRange == 256)
319  {
320    encodeAlignedBinsEP(binValues, numBins);
321    return;
322  }
323
[56]324  while ( numBins > 8 )
[2]325  {
[56]326    numBins -= 8;
[1313]327    UInt pattern = binValues >> numBins;
[56]328    m_uiLow <<= 8;
329    m_uiLow += m_uiRange * pattern;
330    binValues -= pattern << numBins;
331    m_bitsLeft -= 8;
[1313]332
[56]333    testAndWriteOut();
[2]334  }
[1313]335
[56]336  m_uiLow <<= numBins;
337  m_uiLow += m_uiRange * binValues;
338  m_bitsLeft -= numBins;
[1313]339
[56]340  testAndWriteOut();
341}
342
[1313]343Void TEncBinCABAC::align()
344{
345  m_uiRange = 256;
346}
347
348Void TEncBinCABAC::encodeAlignedBinsEP( UInt binValues, Int numBins )
349{
350  Int binsRemaining = numBins;
351
352  assert(m_uiRange == 256); //aligned encode only works when range = 256
353
354  while (binsRemaining > 0)
355  {
356    const UInt binsToCode = std::min<UInt>(binsRemaining, 8); //code bytes if able to take advantage of the system's byte-write function
357    const UInt binMask    = (1 << binsToCode) - 1;
358
359    const UInt newBins = (binValues >> (binsRemaining - binsToCode)) & binMask;
360
361    //The process of encoding an EP bin is the same as that of coding a normal
362    //bin where the symbol ranges for 1 and 0 are both half the range:
363    //
364    //  low = (low + range/2) << 1       (to encode a 1)
365    //  low =  low            << 1       (to encode a 0)
366    //
367    //  i.e.
368    //  low = (low + (bin * range/2)) << 1
369    //
370    //  which is equivalent to:
371    //
372    //  low = (low << 1) + (bin * range)
373    //
374    //  this can be generalised for multiple bins, producing the following expression:
375    //
376    m_uiLow = (m_uiLow << binsToCode) + (newBins << 8); //range is known to be 256
377
378    binsRemaining -= binsToCode;
379    m_bitsLeft    -= binsToCode;
380
381    testAndWriteOut();
382  }
383}
384
[56]385/**
386 * \brief Encode terminating bin
387 *
388 * \param binValue bin value
389 */
390Void TEncBinCABAC::encodeBinTrm( UInt binValue )
391{
392  m_uiBinsCoded += m_binCountIncrement;
393  m_uiRange -= 2;
394  if( binValue )
395  {
396    m_uiLow  += m_uiRange;
397    m_uiLow <<= 7;
398    m_uiRange = 2 << 7;
399    m_bitsLeft -= 7;
400  }
401  else if ( m_uiRange >= 256 )
402  {
403    return;
404  }
[2]405  else
406  {
[56]407    m_uiLow   <<= 1;
408    m_uiRange <<= 1;
[1313]409    m_bitsLeft--;
[2]410  }
[1313]411
[56]412  testAndWriteOut();
[2]413}
414
[56]415Void TEncBinCABAC::testAndWriteOut()
[2]416{
[56]417  if ( m_bitsLeft < 12 )
[2]418  {
[56]419    writeOut();
[2]420  }
[56]421}
422
423/**
424 * \brief Move bits from register into bitstream
425 */
426Void TEncBinCABAC::writeOut()
427{
428  UInt leadByte = m_uiLow >> (24 - m_bitsLeft);
429  m_bitsLeft += 8;
430  m_uiLow &= 0xffffffffu >> m_bitsLeft;
[1313]431
[56]432  if ( leadByte == 0xff )
[2]433  {
[56]434    m_numBufferedBytes++;
[2]435  }
[56]436  else
[2]437  {
[56]438    if ( m_numBufferedBytes > 0 )
[2]439    {
[56]440      UInt carry = leadByte >> 8;
441      UInt byte = m_bufferedByte + carry;
442      m_bufferedByte = leadByte & 0xff;
443      m_pcTComBitIf->write( byte, 8 );
[1313]444
[56]445      byte = ( 0xff + carry ) & 0xff;
446      while ( m_numBufferedBytes > 1 )
447      {
448        m_pcTComBitIf->write( byte, 8 );
449        m_numBufferedBytes--;
450      }
[2]451    }
452    else
453    {
[56]454      m_numBufferedBytes = 1;
455      m_bufferedByte = leadByte;
[1313]456    }
457  }
[2]458}
459
[56]460//! \}
Note: See TracBrowser for help on using the repository browser.