source: SHVCSoftware/trunk/source/Lib/TLibDecoder/TDecBinCoderCABAC.cpp @ 28

Last change on this file since 28 was 2, checked in by seregin, 12 years ago

Initial import by Vadim Seregin <vseregin@…>

File size: 7.1 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-2012, 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}
78
79Void
80TDecBinCABAC::flush()
81{
82  while (m_pcTComBitstream->getNumBitsLeft() > 0 && m_pcTComBitstream->getNumBitsUntilByteAligned() != 0)
83  {
84    UInt uiBits;
85    m_pcTComBitstream->read ( 1, uiBits );
86  }
87  start();
88}
89
90/**
91 - Copy CABAC state.
92 .
93 \param pcTDecBinIf The source CABAC engine.
94 */
95Void
96TDecBinCABAC::copyState( TDecBinIf* pcTDecBinIf )
97{
98  TDecBinCABAC* pcTDecBinCABAC = pcTDecBinIf->getTDecBinCABAC();
99  m_uiRange   = pcTDecBinCABAC->m_uiRange;
100  m_uiValue   = pcTDecBinCABAC->m_uiValue;
101  m_bitsNeeded= pcTDecBinCABAC->m_bitsNeeded;
102}
103
104
105Void
106TDecBinCABAC::decodeBin( UInt& ruiBin, ContextModel &rcCtxModel )
107{
108  UInt uiLPS = TComCABACTables::sm_aucLPSTable[ rcCtxModel.getState() ][ ( m_uiRange >> 6 ) - 4 ];
109  m_uiRange -= uiLPS;
110  UInt scaledRange = m_uiRange << 7;
111 
112  if( m_uiValue < scaledRange )
113  {
114    // MPS path
115    ruiBin = rcCtxModel.getMps();
116    rcCtxModel.updateMPS();
117   
118    if ( scaledRange >= ( 256 << 7 ) )
119    {
120      return;
121    }
122   
123    m_uiRange = scaledRange >> 6;
124    m_uiValue += m_uiValue;
125   
126    if ( ++m_bitsNeeded == 0 )
127    {
128      m_bitsNeeded = -8;
129      m_uiValue += m_pcTComBitstream->readByte();     
130    }
131  }
132  else
133  {
134    // LPS path
135    Int numBits = TComCABACTables::sm_aucRenormTable[ uiLPS >> 3 ];
136    m_uiValue   = ( m_uiValue - scaledRange ) << numBits;
137    m_uiRange   = uiLPS << numBits;
138    ruiBin      = 1 - rcCtxModel.getMps();
139    rcCtxModel.updateLPS();
140   
141    m_bitsNeeded += numBits;
142   
143    if ( m_bitsNeeded >= 0 )
144    {
145      m_uiValue += m_pcTComBitstream->readByte() << m_bitsNeeded;
146      m_bitsNeeded -= 8;
147    }
148  }
149}
150
151Void
152TDecBinCABAC::decodeBinEP( UInt& ruiBin )
153{
154  m_uiValue += m_uiValue;
155 
156  if ( ++m_bitsNeeded >= 0 )
157  {
158    m_bitsNeeded = -8;
159    m_uiValue += m_pcTComBitstream->readByte();
160  }
161 
162  ruiBin = 0;
163  UInt scaledRange = m_uiRange << 7;
164  if ( m_uiValue >= scaledRange )
165  {
166    ruiBin = 1;
167    m_uiValue -= scaledRange;
168  }
169}
170
171Void TDecBinCABAC::decodeBinsEP( UInt& ruiBin, Int numBins )
172{
173  UInt bins = 0;
174 
175  while ( numBins > 8 )
176  {
177    m_uiValue = ( m_uiValue << 8 ) + ( m_pcTComBitstream->readByte() << ( 8 + m_bitsNeeded ) );
178   
179    UInt scaledRange = m_uiRange << 15;
180    for ( Int i = 0; i < 8; i++ )
181    {
182      bins += bins;
183      scaledRange >>= 1;
184      if ( m_uiValue >= scaledRange )
185      {
186        bins++;
187        m_uiValue -= scaledRange;
188      }
189    }
190    numBins -= 8;
191  }
192 
193  m_bitsNeeded += numBins;
194  m_uiValue <<= numBins;
195 
196  if ( m_bitsNeeded >= 0 )
197  {
198    m_uiValue += m_pcTComBitstream->readByte() << m_bitsNeeded;
199    m_bitsNeeded -= 8;
200  }
201 
202  UInt scaledRange = m_uiRange << ( numBins + 7 );
203  for ( Int i = 0; i < numBins; i++ )
204  {
205    bins += bins;
206    scaledRange >>= 1;
207    if ( m_uiValue >= scaledRange )
208    {
209      bins++;
210      m_uiValue -= scaledRange;
211    }
212  }
213 
214  ruiBin = bins;
215}
216
217Void
218TDecBinCABAC::decodeBinTrm( UInt& ruiBin )
219{
220  m_uiRange -= 2;
221  UInt scaledRange = m_uiRange << 7;
222  if( m_uiValue >= scaledRange )
223  {
224    ruiBin = 1;
225  }
226  else
227  {
228    ruiBin = 0;
229    if ( scaledRange < ( 256 << 7 ) )
230    {
231      m_uiRange = scaledRange >> 6;
232      m_uiValue += m_uiValue;
233     
234      if ( ++m_bitsNeeded == 0 )
235      {
236        m_bitsNeeded = -8;
237        m_uiValue += m_pcTComBitstream->readByte();     
238      }
239    }
240  }
241}
242
243/** Reset BAC register values.
244 * \returns Void
245 */
246Void TDecBinCABAC::resetBac()
247{
248  m_uiRange    = 510;
249  m_bitsNeeded = -8;
250  m_uiValue    = m_pcTComBitstream->read( 16 );
251}
252
253/** Decode subsequent_pcm_num.
254 * \param numSubseqIPCM
255 * \returns Void
256 */
257Void TDecBinCABAC::decodeNumSubseqIPCM( Int& numSubseqIPCM )
258{
259  UInt bit = 0;
260
261  numSubseqIPCM = 0;
262
263  do
264  {
265    m_uiValue += m_uiValue;
266    if ( ++m_bitsNeeded >= 0 )
267    {
268      m_bitsNeeded = -8;
269      m_uiValue += m_pcTComBitstream->readByte();
270    }
271    bit = ((m_uiValue&128)>>7);
272    numSubseqIPCM++;
273  }
274  while( bit && (numSubseqIPCM < 3 ));
275
276  if( bit && (numSubseqIPCM == 3 ))
277  {
278    numSubseqIPCM++;
279  }
280
281  numSubseqIPCM --;
282}
283
284/** Decode PCM alignment zero bits.
285 * \returns Void
286 */
287Void TDecBinCABAC::decodePCMAlignBits()
288{
289  Int iNum = m_pcTComBitstream->getNumBitsUntilByteAligned();
290 
291  UInt uiBit = 0;
292  m_pcTComBitstream->read( iNum, uiBit );
293}
294
295/** Read a PCM code.
296 * \param uiLength code bit-depth
297 * \param ruiCode pointer to PCM code value
298 * \returns Void
299 */
300Void  TDecBinCABAC::xReadPCMCode(UInt uiLength, UInt& ruiCode)
301{
302  assert ( uiLength > 0 );
303  m_pcTComBitstream->read (uiLength, ruiCode);
304}
305//! \}
Note: See TracBrowser for help on using the repository browser.