source: 3DVCSoftware/trunk/source/Lib/TLibDecoder/TDecBinCoderCABAC.cpp @ 685

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

Merged DEV-2.0-dev0@604.

  • Property svn:eol-style set to native
File size: 5.9 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 *
[608]6 * Copyright (c) 2010-2013, 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     TDecBinCoderCABAC.cpp
35    \brief    binary entropy decoder of CABAC
36*/
37
38#include "TDecBinCoderCABAC.h"
[608]39#include "../TLibCommon/TComRom.h"
[2]40
[56]41//! \ingroup TLibDecoder
42//! \{
[2]43
44TDecBinCABAC::TDecBinCABAC()
45: m_pcTComBitstream( 0 )
46{
47}
48
49TDecBinCABAC::~TDecBinCABAC()
50{
51}
52
53Void
[56]54TDecBinCABAC::init( TComInputBitstream* pcTComBitstream )
[2]55{
56  m_pcTComBitstream = pcTComBitstream;
57}
58
59Void
60TDecBinCABAC::uninit()
61{
62  m_pcTComBitstream = 0;
63}
64
65Void
66TDecBinCABAC::start()
67{
[56]68  assert( m_pcTComBitstream->getNumBitsUntilByteAligned() == 0 );
[2]69  m_uiRange    = 510;
[56]70  m_bitsNeeded = -8;
[608]71  m_uiValue    = (m_pcTComBitstream->readByte() << 8);
[56]72  m_uiValue   |= m_pcTComBitstream->readByte();
73}
74
75Void
76TDecBinCABAC::finish()
77{
[608]78  UInt lastByte;
[56]79
[608]80  m_pcTComBitstream->peekPreviousByte( lastByte );
81  // Check for proper stop/alignment pattern
82  assert( ((lastByte << (8 + m_bitsNeeded)) & 0xff) == 0x80 );
[2]83}
84
[56]85/**
86 - Copy CABAC state.
87 .
88 \param pcTDecBinIf The source CABAC engine.
89 */
[2]90Void
[56]91TDecBinCABAC::copyState( TDecBinIf* pcTDecBinIf )
[2]92{
[56]93  TDecBinCABAC* pcTDecBinCABAC = pcTDecBinIf->getTDecBinCABAC();
94  m_uiRange   = pcTDecBinCABAC->m_uiRange;
95  m_uiValue   = pcTDecBinCABAC->m_uiValue;
96  m_bitsNeeded= pcTDecBinCABAC->m_bitsNeeded;
[2]97}
98
[56]99
[2]100Void
101TDecBinCABAC::decodeBin( UInt& ruiBin, ContextModel &rcCtxModel )
102{
[56]103  UInt uiLPS = TComCABACTables::sm_aucLPSTable[ rcCtxModel.getState() ][ ( m_uiRange >> 6 ) - 4 ];
104  m_uiRange -= uiLPS;
105  UInt scaledRange = m_uiRange << 7;
106 
107  if( m_uiValue < scaledRange )
[2]108  {
[56]109    // MPS path
110    ruiBin = rcCtxModel.getMps();
[2]111    rcCtxModel.updateMPS();
[56]112   
113    if ( scaledRange >= ( 256 << 7 ) )
114    {
115      return;
116    }
117   
118    m_uiRange = scaledRange >> 6;
119    m_uiValue += m_uiValue;
120   
121    if ( ++m_bitsNeeded == 0 )
122    {
123      m_bitsNeeded = -8;
124      m_uiValue += m_pcTComBitstream->readByte();     
125    }
[2]126  }
127  else
128  {
[56]129    // LPS path
130    Int numBits = TComCABACTables::sm_aucRenormTable[ uiLPS >> 3 ];
131    m_uiValue   = ( m_uiValue - scaledRange ) << numBits;
132    m_uiRange   = uiLPS << numBits;
[2]133    ruiBin      = 1 - rcCtxModel.getMps();
134    rcCtxModel.updateLPS();
[56]135   
136    m_bitsNeeded += numBits;
137   
138    if ( m_bitsNeeded >= 0 )
139    {
140      m_uiValue += m_pcTComBitstream->readByte() << m_bitsNeeded;
141      m_bitsNeeded -= 8;
142    }
[2]143  }
144}
145
146Void
147TDecBinCABAC::decodeBinEP( UInt& ruiBin )
148{
[56]149  m_uiValue += m_uiValue;
150 
151  if ( ++m_bitsNeeded >= 0 )
[2]152  {
[56]153    m_bitsNeeded = -8;
154    m_uiValue += m_pcTComBitstream->readByte();
[2]155  }
[56]156 
157  ruiBin = 0;
158  UInt scaledRange = m_uiRange << 7;
159  if ( m_uiValue >= scaledRange )
[2]160  {
[56]161    ruiBin = 1;
162    m_uiValue -= scaledRange;
[2]163  }
164}
165
[56]166Void TDecBinCABAC::decodeBinsEP( UInt& ruiBin, Int numBins )
167{
168  UInt bins = 0;
169 
170  while ( numBins > 8 )
171  {
172    m_uiValue = ( m_uiValue << 8 ) + ( m_pcTComBitstream->readByte() << ( 8 + m_bitsNeeded ) );
173   
174    UInt scaledRange = m_uiRange << 15;
175    for ( Int i = 0; i < 8; i++ )
176    {
177      bins += bins;
178      scaledRange >>= 1;
179      if ( m_uiValue >= scaledRange )
180      {
181        bins++;
182        m_uiValue -= scaledRange;
183      }
184    }
185    numBins -= 8;
186  }
187 
188  m_bitsNeeded += numBins;
189  m_uiValue <<= numBins;
190 
191  if ( m_bitsNeeded >= 0 )
192  {
193    m_uiValue += m_pcTComBitstream->readByte() << m_bitsNeeded;
194    m_bitsNeeded -= 8;
195  }
196 
197  UInt scaledRange = m_uiRange << ( numBins + 7 );
198  for ( Int i = 0; i < numBins; i++ )
199  {
200    bins += bins;
201    scaledRange >>= 1;
202    if ( m_uiValue >= scaledRange )
203    {
204      bins++;
205      m_uiValue -= scaledRange;
206    }
207  }
208 
209  ruiBin = bins;
210}
211
[2]212Void
213TDecBinCABAC::decodeBinTrm( UInt& ruiBin )
214{
215  m_uiRange -= 2;
[56]216  UInt scaledRange = m_uiRange << 7;
217  if( m_uiValue >= scaledRange )
[2]218  {
219    ruiBin = 1;
220  }
221  else
222  {
223    ruiBin = 0;
[56]224    if ( scaledRange < ( 256 << 7 ) )
[2]225    {
[56]226      m_uiRange = scaledRange >> 6;
227      m_uiValue += m_uiValue;
228     
229      if ( ++m_bitsNeeded == 0 )
230      {
231        m_bitsNeeded = -8;
232        m_uiValue += m_pcTComBitstream->readByte();     
233      }
[2]234    }
235  }
236}
237
[56]238/** Read a PCM code.
239 * \param uiLength code bit-depth
240 * \param ruiCode pointer to PCM code value
241 * \returns Void
242 */
243Void  TDecBinCABAC::xReadPCMCode(UInt uiLength, UInt& ruiCode)
244{
245  assert ( uiLength > 0 );
246  m_pcTComBitstream->read (uiLength, ruiCode);
247}
248//! \}
Note: See TracBrowser for help on using the repository browser.