source: SHVCSoftware/branches/SHM-3.1-dev/source/Lib/TLibDecoder/TDecBinCoderCABAC.cpp @ 577

Last change on this file since 577 was 313, checked in by suehring, 11 years ago

set svn:eol-style=native property on all source files to do proper
automatic line break conversion on check-out and check-in

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