source: 3DVCSoftware/branches/0.3-nokia/source/Lib/TLibEncoder/TEncBinCoderCABAC.cpp @ 122

Last change on this file since 122 was 5, checked in by hhi, 13 years ago

Clean version with cfg-files

  • Property svn:eol-style set to native
File size: 5.7 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-2011, 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 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
35
36/** \file     TEncBinCoderCABAC.cpp
37    \brief    binary entropy encoder of CABAC
38*/
39
40#include "TEncBinCoderCABAC.h"
41
42
43TEncBinCABAC::TEncBinCABAC()
44: m_pcTComBitIf( 0 )
45, m_bBinCountingEnabled(0)
46{
47}
48
49TEncBinCABAC::~TEncBinCABAC()
50{
51}
52
53Void
54TEncBinCABAC::init( TComBitIf* pcTComBitIf )
55{
56  m_pcTComBitIf = pcTComBitIf;
57}
58
59Void
60TEncBinCABAC::uninit()
61{
62  m_pcTComBitIf = 0;
63}
64
65Void
66TEncBinCABAC::start()
67{
68  m_uiLow           = 0;
69  m_uiRange         = 510;
70  m_uiBitsToFollow  = 0;
71  m_uiByte          = 0;
72  m_uiBitsLeft      = 9;
73}
74
75Void
76TEncBinCABAC::finish()
77{
78  xWriteBitAndBitsToFollow( ( m_uiLow >> 9 ) & 1 );
79  xWriteBit               ( ( m_uiLow >> 8 ) & 1 );
80  // xWriteBit               ( 1 ); // stop bit, already written in TEncGOP::compressGOP
81  if( 8 - m_uiBitsLeft != 0 )
82  {
83    m_pcTComBitIf->write  ( m_uiByte, 8 - m_uiBitsLeft );
84  }
85}
86
87Void
88TEncBinCABAC::copyState( TEncBinIf* pcTEncBinIf )
89{
90  TEncBinCABAC* pcTEncBinCABAC = pcTEncBinIf->getTEncBinCABAC();
91  m_uiLow           = pcTEncBinCABAC->m_uiLow;
92  m_uiRange         = pcTEncBinCABAC->m_uiRange;
93  m_uiBitsToFollow  = pcTEncBinCABAC->m_uiBitsToFollow;
94  m_uiByte          = pcTEncBinCABAC->m_uiByte;
95  m_uiBitsLeft      = pcTEncBinCABAC->m_uiBitsLeft;
96}
97
98Void 
99TEncBinCABAC::resetBits()
100{
101  m_uiLow          &= 255;
102  m_uiBitsToFollow  = 0;
103  m_uiByte          = 0;
104  m_uiBitsLeft      = 9;
105}
106
107UInt
108TEncBinCABAC::getNumWrittenBits()
109{
110  return m_pcTComBitIf->getNumberOfWrittenBits() + 8 + m_uiBitsToFollow - m_uiBitsLeft + 1;
111}
112
113Void
114TEncBinCABAC::encodeBin( UInt uiBin, ContextModel &rcCtxModel )
115{
116  {
117    DTRACE_CABAC_V( g_nSymbolCounter++ )
118    DTRACE_CABAC_T( "\tstate=" )
119    DTRACE_CABAC_V( ( rcCtxModel.getState() << 1 ) + rcCtxModel.getMps() )
120    DTRACE_CABAC_T( "\tsymbol=" )
121    DTRACE_CABAC_V( uiBin )
122    DTRACE_CABAC_T( "\n" )
123  }
124  if (m_bBinCountingEnabled) 
125  {
126    m_uiBinsCoded++;
127  }
128  UInt  uiLPS   = TComCABACTables::sm_aucLPSTable[ rcCtxModel.getState() ][ ( m_uiRange >> 6 ) & 3 ];
129  m_uiRange    -= uiLPS;
130  if( uiBin != rcCtxModel.getMps() )
131  {
132    m_uiLow    += m_uiRange;
133    m_uiRange   = uiLPS;
134    rcCtxModel.updateLPS();
135  }
136  else
137  {
138    rcCtxModel.updateMPS();
139  }
140  while( m_uiRange < 256 )
141  {
142    if( m_uiLow >= 512 )
143    {
144      xWriteBitAndBitsToFollow( 1 );
145      m_uiLow -= 512;
146    }
147    else if( m_uiLow < 256 )
148    {
149      xWriteBitAndBitsToFollow( 0 );
150    }
151    else
152    {
153      m_uiBitsToFollow++;
154      m_uiLow         -= 256;
155    }
156    m_uiLow   <<= 1;
157    m_uiRange <<= 1;
158  }
159}
160
161Void
162TEncBinCABAC::encodeBinEP( UInt uiBin )
163{
164  {
165    DTRACE_CABAC_V( g_nSymbolCounter++ )
166    DTRACE_CABAC_T( "\tEPsymbol=" )
167    DTRACE_CABAC_V( uiBin )
168    DTRACE_CABAC_T( "\n" )
169  }
170  if (m_bBinCountingEnabled)
171  {
172    m_uiBinsCoded++;
173  }
174  m_uiLow <<= 1;
175  if( uiBin )
176  {
177    m_uiLow += m_uiRange;
178  }
179  if( m_uiLow >= 1024 )
180  {
181    xWriteBitAndBitsToFollow( 1 );
182    m_uiLow -= 1024;
183  }
184  else if( m_uiLow < 512 )
185  {
186    xWriteBitAndBitsToFollow( 0 );
187  }
188  else
189  {
190    m_uiBitsToFollow++;
191    m_uiLow         -= 512;
192  }
193}
194
195Void
196TEncBinCABAC::encodeBinTrm( UInt uiBin )
197{
198  if (m_bBinCountingEnabled)
199  {
200    m_uiBinsCoded++;
201  }
202  m_uiRange -= 2;
203  if( uiBin )
204  {
205    m_uiLow  += m_uiRange;
206    m_uiRange = 2;
207  }
208  while( m_uiRange < 256 )
209  {
210    if( m_uiLow >= 512 )
211    {
212      xWriteBitAndBitsToFollow( 1 );
213      m_uiLow -= 512;
214    }
215    else if( m_uiLow < 256 )
216    {
217      xWriteBitAndBitsToFollow( 0 );
218    }
219    else
220    {
221      m_uiBitsToFollow++;
222      m_uiLow         -= 256;
223    }
224    m_uiLow   <<= 1;
225    m_uiRange <<= 1;
226  }
227}
228
229Void 
230TEncBinCABAC::xWriteBit( UInt uiBit )
231{
232  m_uiByte += m_uiByte + uiBit;
233  if( ! --m_uiBitsLeft )
234  {
235    m_pcTComBitIf->write( m_uiByte, 8 );
236    m_uiBitsLeft  = 8;
237    m_uiByte      = 0;
238  }
239}
240
241Void 
242TEncBinCABAC::xWriteBitAndBitsToFollow( UInt uiBit )
243{
244  xWriteBit( uiBit );
245  uiBit = 1 - uiBit;
246  while( m_uiBitsToFollow > 0 )
247  {
248    m_uiBitsToFollow--;
249    xWriteBit( uiBit );
250  }
251}
Note: See TracBrowser for help on using the repository browser.