source: 3DVCSoftware/branches/HTM-DEV-0.1-dev/source/Lib/TLibEncoder/TEncBinCoderCABAC.cpp @ 365

Last change on this file since 365 was 324, checked in by tech, 12 years ago

Initial development version for update to latest HM version.
Includes MV-HEVC and basic extensions for 3D-HEVC.

  • Property svn:eol-style set to native
File size: 8.0 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     TEncBinCoderCABAC.cpp
35    \brief    binary entropy encoder of CABAC
36*/
37
38#include "TEncBinCoderCABAC.h"
39#include "TLibCommon/TComRom.h"
40
41
42//! \ingroup TLibEncoder
43//! \{
44
45
46TEncBinCABAC::TEncBinCABAC()
47: m_pcTComBitIf( 0 )
48, m_binCountIncrement( 0 )
49#if FAST_BIT_EST
50, m_fracBits( 0 )
51#endif
52{
53}
54
55TEncBinCABAC::~TEncBinCABAC()
56{
57}
58
59Void TEncBinCABAC::init( TComBitIf* pcTComBitIf )
60{
61  m_pcTComBitIf = pcTComBitIf;
62}
63
64Void TEncBinCABAC::uninit()
65{
66  m_pcTComBitIf = 0;
67}
68
69Void TEncBinCABAC::start()
70{
71  m_uiLow            = 0;
72  m_uiRange          = 510;
73  m_bitsLeft         = 23;
74  m_numBufferedBytes = 0;
75  m_bufferedByte     = 0xff;
76}
77
78Void TEncBinCABAC::finish()
79{
80  if ( m_uiLow >> ( 32 - m_bitsLeft ) )
81  {
82    //assert( m_numBufferedBytes > 0 );
83    //assert( m_bufferedByte != 0xff );
84    m_pcTComBitIf->write( m_bufferedByte + 1, 8 );
85    while ( m_numBufferedBytes > 1 )
86    {
87      m_pcTComBitIf->write( 0x00, 8 );
88      m_numBufferedBytes--;
89    }
90    m_uiLow -= 1 << ( 32 - m_bitsLeft );
91  }
92  else
93  {
94    if ( m_numBufferedBytes > 0 )
95    {
96      m_pcTComBitIf->write( m_bufferedByte, 8 );
97    }
98    while ( m_numBufferedBytes > 1 )
99    {
100      m_pcTComBitIf->write( 0xff, 8 );
101      m_numBufferedBytes--;
102    }   
103  }
104  m_pcTComBitIf->write( m_uiLow >> 8, 24 - m_bitsLeft );
105}
106
107Void TEncBinCABAC::flush()
108{
109  encodeBinTrm(1);
110  finish();
111  m_pcTComBitIf->write(1, 1);
112  m_pcTComBitIf->writeAlignZero();
113
114  start();
115}
116
117/** Reset BAC register and counter values.
118 * \returns Void
119 */
120Void TEncBinCABAC::resetBac()
121{
122  start();
123}
124
125/** Encode PCM alignment zero bits.
126 * \returns Void
127 */
128Void TEncBinCABAC::encodePCMAlignBits()
129{
130  finish();
131  m_pcTComBitIf->write(1, 1);
132  m_pcTComBitIf->writeAlignZero(); // pcm align zero
133}
134
135/** Write a PCM code.
136 * \param uiCode code value
137 * \param uiLength code bit-depth
138 * \returns Void
139 */
140Void TEncBinCABAC::xWritePCMCode(UInt uiCode, UInt uiLength)
141{
142  m_pcTComBitIf->write(uiCode, uiLength);
143}
144
145Void TEncBinCABAC::copyState( TEncBinIf* pcTEncBinIf )
146{
147  TEncBinCABAC* pcTEncBinCABAC = pcTEncBinIf->getTEncBinCABAC();
148  m_uiLow           = pcTEncBinCABAC->m_uiLow;
149  m_uiRange         = pcTEncBinCABAC->m_uiRange;
150  m_bitsLeft        = pcTEncBinCABAC->m_bitsLeft;
151  m_bufferedByte    = pcTEncBinCABAC->m_bufferedByte;
152  m_numBufferedBytes = pcTEncBinCABAC->m_numBufferedBytes;
153#if FAST_BIT_EST
154  m_fracBits = pcTEncBinCABAC->m_fracBits;
155#endif
156}
157
158Void TEncBinCABAC::resetBits()
159{
160  m_uiLow            = 0;
161  m_bitsLeft         = 23;
162  m_numBufferedBytes = 0;
163  m_bufferedByte     = 0xff;
164  if ( m_binCountIncrement )
165  {
166    m_uiBinsCoded = 0;
167  }
168#if FAST_BIT_EST
169  m_fracBits &= 32767;
170#endif
171}
172
173UInt TEncBinCABAC::getNumWrittenBits()
174{
175  return m_pcTComBitIf->getNumberOfWrittenBits() + 8 * m_numBufferedBytes + 23 - m_bitsLeft;
176}
177
178/**
179 * \brief Encode bin
180 *
181 * \param binValue   bin value
182 * \param rcCtxModel context model
183 */
184Void TEncBinCABAC::encodeBin( UInt binValue, ContextModel &rcCtxModel )
185{
186  {
187    DTRACE_CABAC_VL( g_nSymbolCounter++ )
188    DTRACE_CABAC_T( "\tstate=" )
189    DTRACE_CABAC_V( ( rcCtxModel.getState() << 1 ) + rcCtxModel.getMps() )
190    DTRACE_CABAC_T( "\tsymbol=" )
191    DTRACE_CABAC_V( binValue )
192    DTRACE_CABAC_T( "\n" )
193  }
194  m_uiBinsCoded += m_binCountIncrement;
195  rcCtxModel.setBinsCoded( 1 );
196 
197  UInt  uiLPS   = TComCABACTables::sm_aucLPSTable[ rcCtxModel.getState() ][ ( m_uiRange >> 6 ) & 3 ];
198  m_uiRange    -= uiLPS;
199 
200  if( binValue != rcCtxModel.getMps() )
201  {
202    Int numBits = TComCABACTables::sm_aucRenormTable[ uiLPS >> 3 ];
203    m_uiLow     = ( m_uiLow + m_uiRange ) << numBits;
204    m_uiRange   = uiLPS << numBits;
205    rcCtxModel.updateLPS();
206   
207    m_bitsLeft -= numBits;
208  }
209  else
210  {
211    rcCtxModel.updateMPS();
212    if ( m_uiRange >= 256 )
213    {
214      return;
215    }
216   
217    m_uiLow <<= 1;
218    m_uiRange <<= 1;
219    m_bitsLeft--;
220  }
221 
222  testAndWriteOut();
223}
224
225/**
226 * \brief Encode equiprobable bin
227 *
228 * \param binValue bin value
229 */
230Void TEncBinCABAC::encodeBinEP( UInt binValue )
231{
232  {
233    DTRACE_CABAC_VL( g_nSymbolCounter++ )
234    DTRACE_CABAC_T( "\tEPsymbol=" )
235    DTRACE_CABAC_V( binValue )
236    DTRACE_CABAC_T( "\n" )
237  }
238  m_uiBinsCoded += m_binCountIncrement;
239  m_uiLow <<= 1;
240  if( binValue )
241  {
242    m_uiLow += m_uiRange;
243  }
244  m_bitsLeft--;
245 
246  testAndWriteOut();
247}
248
249/**
250 * \brief Encode equiprobable bins
251 *
252 * \param binValues bin values
253 * \param numBins number of bins
254 */
255Void TEncBinCABAC::encodeBinsEP( UInt binValues, Int numBins )
256{
257  m_uiBinsCoded += numBins & -m_binCountIncrement;
258 
259  for ( Int i = 0; i < numBins; i++ )
260  {
261    DTRACE_CABAC_VL( g_nSymbolCounter++ )
262    DTRACE_CABAC_T( "\tEPsymbol=" )
263    DTRACE_CABAC_V( ( binValues >> ( numBins - 1 - i ) ) & 1 )
264    DTRACE_CABAC_T( "\n" )
265  }
266 
267  while ( numBins > 8 )
268  {
269    numBins -= 8;
270    UInt pattern = binValues >> numBins; 
271    m_uiLow <<= 8;
272    m_uiLow += m_uiRange * pattern;
273    binValues -= pattern << numBins;
274    m_bitsLeft -= 8;
275   
276    testAndWriteOut();
277  }
278 
279  m_uiLow <<= numBins;
280  m_uiLow += m_uiRange * binValues;
281  m_bitsLeft -= numBins;
282 
283  testAndWriteOut();
284}
285
286/**
287 * \brief Encode terminating bin
288 *
289 * \param binValue bin value
290 */
291Void TEncBinCABAC::encodeBinTrm( UInt binValue )
292{
293  m_uiBinsCoded += m_binCountIncrement;
294  m_uiRange -= 2;
295  if( binValue )
296  {
297    m_uiLow  += m_uiRange;
298    m_uiLow <<= 7;
299    m_uiRange = 2 << 7;
300    m_bitsLeft -= 7;
301  }
302  else if ( m_uiRange >= 256 )
303  {
304    return;
305  }
306  else
307  {
308    m_uiLow   <<= 1;
309    m_uiRange <<= 1;
310    m_bitsLeft--;   
311  }
312 
313  testAndWriteOut();
314}
315
316Void TEncBinCABAC::testAndWriteOut()
317{
318  if ( m_bitsLeft < 12 )
319  {
320    writeOut();
321  }
322}
323
324/**
325 * \brief Move bits from register into bitstream
326 */
327Void TEncBinCABAC::writeOut()
328{
329  UInt leadByte = m_uiLow >> (24 - m_bitsLeft);
330  m_bitsLeft += 8;
331  m_uiLow &= 0xffffffffu >> m_bitsLeft;
332 
333  if ( leadByte == 0xff )
334  {
335    m_numBufferedBytes++;
336  }
337  else
338  {
339    if ( m_numBufferedBytes > 0 )
340    {
341      UInt carry = leadByte >> 8;
342      UInt byte = m_bufferedByte + carry;
343      m_bufferedByte = leadByte & 0xff;
344      m_pcTComBitIf->write( byte, 8 );
345     
346      byte = ( 0xff + carry ) & 0xff;
347      while ( m_numBufferedBytes > 1 )
348      {
349        m_pcTComBitIf->write( byte, 8 );
350        m_numBufferedBytes--;
351      }
352    }
353    else
354    {
355      m_numBufferedBytes = 1;
356      m_bufferedByte = leadByte;
357    }     
358  }   
359}
360
361//! \}
Note: See TracBrowser for help on using the repository browser.