source: 3DVCSoftware/branches/HTM-DEV-0.3-dev2/source/Lib/TLibDecoder/TDecBinCoderCABAC.cpp @ 521

Last change on this file since 521 was 521, checked in by tech, 11 years ago

Integrated following changes:

  • H_MV_FIX1071, fix of encoder side reference list construction on IRAP pictures, same as in HM11.
  • H_3D_VSO_FIX_BORDRE_EXTENSION, fixed uninitialized borders for org-yuvs in VSO
  • H_MV_ENC_DEC_TRAC, added cu/pu level trace ( only enabled when ENC_DEC_TRACE is enabled)
  • Property svn:eol-style set to native
File size: 6.5 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-2013, 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     TDecBinCoderCABAC.cpp
35    \brief    binary entropy decoder of CABAC
36*/
37
38#include "TDecBinCoderCABAC.h"
39#include "../TLibCommon/TComRom.h"
40
41//! \ingroup TLibDecoder
42//! \{
43
44TDecBinCABAC::TDecBinCABAC()
45: m_pcTComBitstream( 0 )
46{
47}
48
49TDecBinCABAC::~TDecBinCABAC()
50{
51}
52
53Void
54TDecBinCABAC::init( TComInputBitstream* pcTComBitstream )
55{
56  m_pcTComBitstream = pcTComBitstream;
57}
58
59Void
60TDecBinCABAC::uninit()
61{
62  m_pcTComBitstream = 0;
63}
64
65Void
66TDecBinCABAC::start()
67{
68  assert( m_pcTComBitstream->getNumBitsUntilByteAligned() == 0 );
69  m_uiRange    = 510;
70  m_bitsNeeded = -8;
71  m_uiValue    = (m_pcTComBitstream->readByte() << 8);
72  m_uiValue   |= m_pcTComBitstream->readByte();
73}
74
75Void
76TDecBinCABAC::finish()
77{
78}
79
80Void
81TDecBinCABAC::flush()
82{
83  while (m_pcTComBitstream->getNumBitsLeft() > 0 && m_pcTComBitstream->getNumBitsUntilByteAligned() != 0)
84  {
85    UInt uiBits;
86    m_pcTComBitstream->read ( 1, uiBits );
87  }
88  start();
89}
90
91/**
92 - Copy CABAC state.
93 .
94 \param pcTDecBinIf The source CABAC engine.
95 */
96Void
97TDecBinCABAC::copyState( TDecBinIf* pcTDecBinIf )
98{
99  TDecBinCABAC* pcTDecBinCABAC = pcTDecBinIf->getTDecBinCABAC();
100  m_uiRange   = pcTDecBinCABAC->m_uiRange;
101  m_uiValue   = pcTDecBinCABAC->m_uiValue;
102  m_bitsNeeded= pcTDecBinCABAC->m_bitsNeeded;
103}
104
105
106Void
107TDecBinCABAC::decodeBin( UInt& ruiBin, ContextModel &rcCtxModel )
108{
109  UInt uiLPS = TComCABACTables::sm_aucLPSTable[ rcCtxModel.getState() ][ ( m_uiRange >> 6 ) - 4 ];
110  m_uiRange -= uiLPS;
111  UInt scaledRange = m_uiRange << 7;
112 
113  if( m_uiValue < scaledRange )
114  {
115    // MPS path
116    ruiBin = rcCtxModel.getMps();
117    rcCtxModel.updateMPS();
118   
119    if ( scaledRange >= ( 256 << 7 ) )
120    {
121      return;
122    }
123   
124    m_uiRange = scaledRange >> 6;
125    m_uiValue += m_uiValue;
126   
127    if ( ++m_bitsNeeded == 0 )
128    {
129      m_bitsNeeded = -8;
130      m_uiValue += m_pcTComBitstream->readByte();     
131    }
132  }
133  else
134  {
135    // LPS path
136    Int numBits = TComCABACTables::sm_aucRenormTable[ uiLPS >> 3 ];
137    m_uiValue   = ( m_uiValue - scaledRange ) << numBits;
138    m_uiRange   = uiLPS << numBits;
139    ruiBin      = 1 - rcCtxModel.getMps();
140    rcCtxModel.updateLPS();
141   
142    m_bitsNeeded += numBits;
143   
144    if ( m_bitsNeeded >= 0 )
145    {
146      m_uiValue += m_pcTComBitstream->readByte() << m_bitsNeeded;
147      m_bitsNeeded -= 8;
148    }
149  }
150}
151
152Void
153TDecBinCABAC::decodeBinEP( UInt& ruiBin )
154{
155  m_uiValue += m_uiValue;
156 
157  if ( ++m_bitsNeeded >= 0 )
158  {
159    m_bitsNeeded = -8;
160    m_uiValue += m_pcTComBitstream->readByte();
161  }
162 
163  ruiBin = 0;
164  UInt scaledRange = m_uiRange << 7;
165  if ( m_uiValue >= scaledRange )
166  {
167    ruiBin = 1;
168    m_uiValue -= scaledRange;
169  }
170}
171
172Void TDecBinCABAC::decodeBinsEP( UInt& ruiBin, Int numBins )
173{
174  UInt bins = 0;
175 
176  while ( numBins > 8 )
177  {
178    m_uiValue = ( m_uiValue << 8 ) + ( m_pcTComBitstream->readByte() << ( 8 + m_bitsNeeded ) );
179   
180    UInt scaledRange = m_uiRange << 15;
181    for ( Int i = 0; i < 8; i++ )
182    {
183      bins += bins;
184      scaledRange >>= 1;
185      if ( m_uiValue >= scaledRange )
186      {
187        bins++;
188        m_uiValue -= scaledRange;
189      }
190    }
191    numBins -= 8;
192  }
193 
194  m_bitsNeeded += numBins;
195  m_uiValue <<= numBins;
196 
197  if ( m_bitsNeeded >= 0 )
198  {
199    m_uiValue += m_pcTComBitstream->readByte() << m_bitsNeeded;
200    m_bitsNeeded -= 8;
201  }
202 
203  UInt scaledRange = m_uiRange << ( numBins + 7 );
204  for ( Int i = 0; i < numBins; i++ )
205  {
206    bins += bins;
207    scaledRange >>= 1;
208    if ( m_uiValue >= scaledRange )
209    {
210      bins++;
211      m_uiValue -= scaledRange;
212    }
213  }
214 
215  ruiBin = bins;
216}
217
218Void
219TDecBinCABAC::decodeBinTrm( UInt& ruiBin )
220{
221  m_uiRange -= 2;
222  UInt scaledRange = m_uiRange << 7;
223  if( m_uiValue >= scaledRange )
224  {
225    ruiBin = 1;
226  }
227  else
228  {
229    ruiBin = 0;
230    if ( scaledRange < ( 256 << 7 ) )
231    {
232      m_uiRange = scaledRange >> 6;
233      m_uiValue += m_uiValue;
234     
235      if ( ++m_bitsNeeded == 0 )
236      {
237        m_bitsNeeded = -8;
238        m_uiValue += m_pcTComBitstream->readByte();     
239      }
240    }
241  }
242}
243
244/** Reset BAC register values.
245 * \returns Void
246 */
247Void TDecBinCABAC::resetBac()
248{
249  m_uiRange    = 510;
250  m_bitsNeeded = -8;
251  m_uiValue    = m_pcTComBitstream->read( 16 );
252}
253
254/** Decode PCM alignment zero bits.
255 * \returns Void
256 */
257Void TDecBinCABAC::decodePCMAlignBits()
258{
259  Int iNum = m_pcTComBitstream->getNumBitsUntilByteAligned();
260 
261  UInt uiBit = 0;
262  m_pcTComBitstream->read( iNum, uiBit );
263#if H_MV_ENC_DEC_TRAC         
264  DTRACE_CU("Number of pcm_alignment_zero_bit", iNum)
265#endif
266}
267
268/** Read a PCM code.
269 * \param uiLength code bit-depth
270 * \param ruiCode pointer to PCM code value
271 * \returns Void
272 */
273Void  TDecBinCABAC::xReadPCMCode(UInt uiLength, UInt& ruiCode)
274{
275  assert ( uiLength > 0 );
276  m_pcTComBitstream->read (uiLength, ruiCode);
277}
278//! \}
Note: See TracBrowser for help on using the repository browser.