source: 3DVCSoftware/branches/HTM-6.2-dev1-MediaTek/source/Lib/TLibEncoder/TEncBinCoderCABAC.cpp @ 384

Last change on this file since 384 was 296, checked in by tech, 12 years ago

Reintegrated branch 5.1-dev0 rev. 295.

  • Property svn:eol-style set to native
File size: 8.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-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     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#if OL_FLUSH_ALIGN
113  m_pcTComBitIf->writeAlignZero();
114#endif
115
116  start();
117}
118
119/** Reset BAC register and counter values.
120 * \returns Void
121 */
122Void TEncBinCABAC::resetBac()
123{
124  start();
125}
126
127/** Encode # of subsequent IPCM blocks.
128 * \param numSubseqIPCM
129 * \returns Void
130 */
131Void TEncBinCABAC::encodeNumSubseqIPCM( Int numSubseqIPCM )
132{
133  finish();
134  m_pcTComBitIf->write( 1, 1 ); // stop bit
135
136  m_pcTComBitIf->write( numSubseqIPCM ? 1 : 0, 1);
137
138  if ( numSubseqIPCM > 0)
139  {
140    Bool bCodeLast = ( 3 > numSubseqIPCM );
141
142    while( --numSubseqIPCM )
143    {
144      m_pcTComBitIf->write( 1, 1 );
145    }
146    if( bCodeLast )
147    {
148      m_pcTComBitIf->write( 0, 1 );
149    }
150  }
151}
152
153/** Encode PCM alignment zero bits.
154 * \returns Void
155 */
156Void TEncBinCABAC::encodePCMAlignBits()
157{
158  m_pcTComBitIf->writeAlignZero(); // pcm align zero
159}
160
161/** Write a PCM code.
162 * \param uiCode code value
163 * \param uiLength code bit-depth
164 * \returns Void
165 */
166Void TEncBinCABAC::xWritePCMCode(UInt uiCode, UInt uiLength)
167{
168  m_pcTComBitIf->write(uiCode, uiLength);
169}
170
171Void TEncBinCABAC::copyState( TEncBinIf* pcTEncBinIf )
172{
173  TEncBinCABAC* pcTEncBinCABAC = pcTEncBinIf->getTEncBinCABAC();
174  m_uiLow           = pcTEncBinCABAC->m_uiLow;
175  m_uiRange         = pcTEncBinCABAC->m_uiRange;
176  m_bitsLeft        = pcTEncBinCABAC->m_bitsLeft;
177  m_bufferedByte    = pcTEncBinCABAC->m_bufferedByte;
178  m_numBufferedBytes = pcTEncBinCABAC->m_numBufferedBytes;
179#if FAST_BIT_EST
180  m_fracBits = pcTEncBinCABAC->m_fracBits;
181#endif
182}
183
184Void TEncBinCABAC::resetBits()
185{
186  m_uiLow            = 0;
187  m_bitsLeft         = 23;
188  m_numBufferedBytes = 0;
189  m_bufferedByte     = 0xff;
190  if ( m_binCountIncrement )
191  {
192    m_uiBinsCoded = 0;
193  }
194#if FAST_BIT_EST
195  m_fracBits &= 32767;
196#endif
197}
198
199UInt TEncBinCABAC::getNumWrittenBits()
200{
201  return m_pcTComBitIf->getNumberOfWrittenBits() + 8 * m_numBufferedBytes + 23 - m_bitsLeft;
202}
203
204/**
205 * \brief Encode bin
206 *
207 * \param binValue   bin value
208 * \param rcCtxModel context model
209 */
210Void TEncBinCABAC::encodeBin( UInt binValue, ContextModel &rcCtxModel )
211{
212  {
213    DTRACE_CABAC_VL( g_nSymbolCounter++ )
214    DTRACE_CABAC_T( "\tstate=" )
215    DTRACE_CABAC_V( ( rcCtxModel.getState() << 1 ) + rcCtxModel.getMps() )
216    DTRACE_CABAC_T( "\tsymbol=" )
217    DTRACE_CABAC_V( binValue )
218    DTRACE_CABAC_T( "\n" )
219  }
220  m_uiBinsCoded += m_binCountIncrement;
221#if CABAC_INIT_FLAG
222  rcCtxModel.setBinsCoded( 1 );
223#endif
224 
225  UInt  uiLPS   = TComCABACTables::sm_aucLPSTable[ rcCtxModel.getState() ][ ( m_uiRange >> 6 ) & 3 ];
226  m_uiRange    -= uiLPS;
227 
228  if( binValue != rcCtxModel.getMps() )
229  {
230    Int numBits = TComCABACTables::sm_aucRenormTable[ uiLPS >> 3 ];
231    m_uiLow     = ( m_uiLow + m_uiRange ) << numBits;
232    m_uiRange   = uiLPS << numBits;
233    rcCtxModel.updateLPS();
234   
235    m_bitsLeft -= numBits;
236  }
237  else
238  {
239    rcCtxModel.updateMPS();
240    if ( m_uiRange >= 256 )
241    {
242      return;
243    }
244   
245    m_uiLow <<= 1;
246    m_uiRange <<= 1;
247    m_bitsLeft--;
248  }
249 
250  testAndWriteOut();
251}
252
253/**
254 * \brief Encode equiprobable bin
255 *
256 * \param binValue bin value
257 */
258Void TEncBinCABAC::encodeBinEP( UInt binValue )
259{
260  {
261    DTRACE_CABAC_VL( g_nSymbolCounter++ )
262    DTRACE_CABAC_T( "\tEPsymbol=" )
263    DTRACE_CABAC_V( binValue )
264    DTRACE_CABAC_T( "\n" )
265  }
266  m_uiBinsCoded += m_binCountIncrement;
267  m_uiLow <<= 1;
268  if( binValue )
269  {
270    m_uiLow += m_uiRange;
271  }
272  m_bitsLeft--;
273 
274  testAndWriteOut();
275}
276
277/**
278 * \brief Encode equiprobable bins
279 *
280 * \param binValues bin values
281 * \param numBins number of bins
282 */
283Void TEncBinCABAC::encodeBinsEP( UInt binValues, Int numBins )
284{
285  m_uiBinsCoded += numBins & -m_binCountIncrement;
286 
287  for ( Int i = 0; i < numBins; i++ )
288  {
289    DTRACE_CABAC_VL( g_nSymbolCounter++ )
290    DTRACE_CABAC_T( "\tEPsymbol=" )
291    DTRACE_CABAC_V( ( binValues >> ( numBins - 1 - i ) ) & 1 )
292    DTRACE_CABAC_T( "\n" )
293  }
294 
295  while ( numBins > 8 )
296  {
297    numBins -= 8;
298    UInt pattern = binValues >> numBins; 
299    m_uiLow <<= 8;
300    m_uiLow += m_uiRange * pattern;
301    binValues -= pattern << numBins;
302    m_bitsLeft -= 8;
303   
304    testAndWriteOut();
305  }
306 
307  m_uiLow <<= numBins;
308  m_uiLow += m_uiRange * binValues;
309  m_bitsLeft -= numBins;
310 
311  testAndWriteOut();
312}
313
314/**
315 * \brief Encode terminating bin
316 *
317 * \param binValue bin value
318 */
319Void TEncBinCABAC::encodeBinTrm( UInt binValue )
320{
321  m_uiBinsCoded += m_binCountIncrement;
322  m_uiRange -= 2;
323  if( binValue )
324  {
325    m_uiLow  += m_uiRange;
326    m_uiLow <<= 7;
327    m_uiRange = 2 << 7;
328    m_bitsLeft -= 7;
329  }
330  else if ( m_uiRange >= 256 )
331  {
332    return;
333  }
334  else
335  {
336    m_uiLow   <<= 1;
337    m_uiRange <<= 1;
338    m_bitsLeft--;   
339  }
340 
341  testAndWriteOut();
342}
343
344Void TEncBinCABAC::testAndWriteOut()
345{
346  if ( m_bitsLeft < 12 )
347  {
348    writeOut();
349  }
350}
351
352/**
353 * \brief Move bits from register into bitstream
354 */
355Void TEncBinCABAC::writeOut()
356{
357  UInt leadByte = m_uiLow >> (24 - m_bitsLeft);
358  m_bitsLeft += 8;
359  m_uiLow &= 0xffffffffu >> m_bitsLeft;
360 
361  if ( leadByte == 0xff )
362  {
363    m_numBufferedBytes++;
364  }
365  else
366  {
367    if ( m_numBufferedBytes > 0 )
368    {
369      UInt carry = leadByte >> 8;
370      UInt byte = m_bufferedByte + carry;
371      m_bufferedByte = leadByte & 0xff;
372      m_pcTComBitIf->write( byte, 8 );
373     
374      byte = ( 0xff + carry ) & 0xff;
375      while ( m_numBufferedBytes > 1 )
376      {
377        m_pcTComBitIf->write( byte, 8 );
378        m_numBufferedBytes--;
379      }
380    }
381    else
382    {
383      m_numBufferedBytes = 1;
384      m_bufferedByte = leadByte;
385    }     
386  }   
387}
388
389/** flush bits when CABAC termination
390  * \param [in] bEnd true means this flushing happens at the end of RBSP. No need to encode stop bit
391  */
392Void TEncBinCABAC::encodeFlush(Bool bEnd)
393{
394  m_uiRange = 2;
395
396  m_uiLow  += 2;
397  m_uiLow <<= 7;
398  m_uiRange = 2 << 7;
399  m_bitsLeft -= 7;
400  testAndWriteOut();
401  finish();
402
403  if(!bEnd)
404  {
405    m_pcTComBitIf->write( 1, 1 ); // stop bit
406  }
407}
408
409//! \}
Note: See TracBrowser for help on using the repository browser.