source: 3DVCSoftware/trunk/source/Lib/TLibEncoder/TEncBinCoderCABAC.cpp @ 1313

Last change on this file since 1313 was 1313, checked in by tech, 9 years ago

Merged 14.1-update-dev1@1312.

  • Property svn:eol-style set to native
File size: 10.3 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-2015, 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#include "TLibCommon/Debug.h"
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#if FAST_BIT_EST
77  m_fracBits         = 0;
78#endif
79}
80
81Void TEncBinCABAC::finish()
82{
83  if ( m_uiLow >> ( 32 - m_bitsLeft ) )
84  {
85    //assert( m_numBufferedBytes > 0 );
86    //assert( m_bufferedByte != 0xff );
87    m_pcTComBitIf->write( m_bufferedByte + 1, 8 );
88    while ( m_numBufferedBytes > 1 )
89    {
90      m_pcTComBitIf->write( 0x00, 8 );
91      m_numBufferedBytes--;
92    }
93    m_uiLow -= 1 << ( 32 - m_bitsLeft );
94  }
95  else
96  {
97    if ( m_numBufferedBytes > 0 )
98    {
99      m_pcTComBitIf->write( m_bufferedByte, 8 );
100    }
101    while ( m_numBufferedBytes > 1 )
102    {
103      m_pcTComBitIf->write( 0xff, 8 );
104      m_numBufferedBytes--;
105    }
106  }
107  m_pcTComBitIf->write( m_uiLow >> 8, 24 - m_bitsLeft );
108}
109
110Void TEncBinCABAC::flush()
111{
112  encodeBinTrm(1);
113  finish();
114  m_pcTComBitIf->write(1, 1);
115  m_pcTComBitIf->writeAlignZero();
116
117  start();
118}
119
120/** Reset BAC register and counter values.
121 * \returns Void
122 */
123Void TEncBinCABAC::resetBac()
124{
125  start();
126}
127
128/** Encode PCM alignment zero bits.
129 * \returns Void
130 */
131Void TEncBinCABAC::encodePCMAlignBits()
132{
133  finish();
134  m_pcTComBitIf->write(1, 1);
135  m_pcTComBitIf->writeAlignZero(); // pcm align zero
136}
137
138/** Write a PCM code.
139 * \param uiCode code value
140 * \param uiLength code bit-depth
141 * \returns Void
142 */
143Void TEncBinCABAC::xWritePCMCode(UInt uiCode, UInt uiLength)
144{
145  m_pcTComBitIf->write(uiCode, uiLength);
146}
147
148Void TEncBinCABAC::copyState( const TEncBinIf* pcTEncBinIf )
149{
150  const TEncBinCABAC* pcTEncBinCABAC = pcTEncBinIf->getTEncBinCABAC();
151  m_uiLow           = pcTEncBinCABAC->m_uiLow;
152  m_uiRange         = pcTEncBinCABAC->m_uiRange;
153  m_bitsLeft        = pcTEncBinCABAC->m_bitsLeft;
154  m_bufferedByte    = pcTEncBinCABAC->m_bufferedByte;
155  m_numBufferedBytes = pcTEncBinCABAC->m_numBufferedBytes;
156#if FAST_BIT_EST
157  m_fracBits = pcTEncBinCABAC->m_fracBits;
158  D_PRINT_INDENT(g_traceEncFracBits,  "CopyState " + n2s(m_fracBits) );   
159#endif
160}
161
162Void TEncBinCABAC::resetBits()
163{
164  m_uiLow            = 0;
165  m_bitsLeft         = 23;
166  m_numBufferedBytes = 0;
167  m_bufferedByte     = 0xff;
168  if ( m_binCountIncrement )
169  {
170    m_uiBinsCoded = 0;
171  }
172#if FAST_BIT_EST
173  D_PRINT_INDENT( g_traceEncFracBits, "Reset Bits Before" + n2s(m_fracBits) );
174  m_fracBits &= 32767;
175  D_PRINT_INDENT( g_traceEncFracBits, "Reset Bits " + n2s(m_fracBits) ); 
176#endif
177}
178
179UInt TEncBinCABAC::getNumWrittenBits()
180{
181  return m_pcTComBitIf->getNumberOfWrittenBits() + 8 * m_numBufferedBytes + 23 - m_bitsLeft;
182}
183
184/**
185 * \brief Encode bin
186 *
187 * \param binValue   bin value
188 * \param rcCtxModel context model
189 */
190Void TEncBinCABAC::encodeBin( UInt binValue, ContextModel &rcCtxModel )
191{
192#if !NH_MV
193  //{
194  //  DTRACE_CABAC_VL( g_nSymbolCounter++ )
195  //  DTRACE_CABAC_T( "\tstate=" )
196  //  DTRACE_CABAC_V( ( rcCtxModel.getState() << 1 ) + rcCtxModel.getMps() )
197  //  DTRACE_CABAC_T( "\tsymbol=" )
198  //  DTRACE_CABAC_V( binValue )
199  //  DTRACE_CABAC_T( "\n" )
200  //}
201#endif
202#if DEBUG_CABAC_BINS
203  const UInt startingRange = m_uiRange;
204#endif
205
206  m_uiBinsCoded += m_binCountIncrement;
207  rcCtxModel.setBinsCoded( 1 );
208
209  UInt  uiLPS   = TComCABACTables::sm_aucLPSTable[ rcCtxModel.getState() ][ ( m_uiRange >> 6 ) & 3 ];
210  m_uiRange    -= uiLPS;
211
212  if( binValue != rcCtxModel.getMps() )
213  {
214    Int numBits = TComCABACTables::sm_aucRenormTable[ uiLPS >> 3 ];
215    m_uiLow     = ( m_uiLow + m_uiRange ) << numBits;
216    m_uiRange   = uiLPS << numBits;
217    rcCtxModel.updateLPS();
218    m_bitsLeft -= numBits;
219    testAndWriteOut();
220  }
221  else
222  {
223    rcCtxModel.updateMPS();
224
225    if ( m_uiRange < 256 )
226    {
227      m_uiLow <<= 1;
228      m_uiRange <<= 1;
229      m_bitsLeft--;
230      testAndWriteOut();
231    }
232  }
233
234#if DEBUG_CABAC_BINS
235  if ((g_debugCounter + debugCabacBinWindow) >= debugCabacBinTargetLine)
236  {
237    std::cout << g_debugCounter << ": coding bin value " << binValue << ", range = [" << startingRange << "->" << m_uiRange << "]\n";
238  }
239
240  if (g_debugCounter >= debugCabacBinTargetLine)
241  {
242    Char breakPointThis;
243    breakPointThis = 7;
244  }
245  if (g_debugCounter >= (debugCabacBinTargetLine + debugCabacBinWindow))
246  {
247    exit(0);
248  }
249
250  g_debugCounter++;
251#endif
252}
253
254/**
255 * \brief Encode equiprobable bin
256 *
257 * \param binValue bin value
258 */
259Void TEncBinCABAC::encodeBinEP( UInt binValue )
260{
261  if (false)
262  {
263#if !NH_MV
264    DTRACE_CABAC_VL( g_nSymbolCounter++ )
265    DTRACE_CABAC_T( "\tEPsymbol=" )
266    DTRACE_CABAC_V( binValue )
267    DTRACE_CABAC_T( "\n" )
268#endif
269  }
270
271  m_uiBinsCoded += m_binCountIncrement;
272
273  if (m_uiRange == 256)
274  {
275    encodeAlignedBinsEP(binValue, 1);
276    return;
277  }
278
279  m_uiLow <<= 1;
280  if( binValue )
281  {
282    m_uiLow += m_uiRange;
283  }
284  m_bitsLeft--;
285
286  testAndWriteOut();
287}
288
289/**
290 * \brief Encode equiprobable bins
291 *
292 * \param binValues bin values
293 * \param numBins number of bins
294 */
295Void TEncBinCABAC::encodeBinsEP( UInt binValues, Int numBins )
296{
297  m_uiBinsCoded += numBins & -m_binCountIncrement;
298
299  if (false)
300  {
301    for ( Int i = 0; i < numBins; i++ )
302    {
303#if !NH_MV
304      DTRACE_CABAC_VL( g_nSymbolCounter++ )
305      DTRACE_CABAC_T( "\tEPsymbol=" )
306      DTRACE_CABAC_V( ( binValues >> ( numBins - 1 - i ) ) & 1 )
307      DTRACE_CABAC_T( "\n" )
308#endif
309    }
310  }
311
312  if (m_uiRange == 256)
313  {
314    encodeAlignedBinsEP(binValues, numBins);
315    return;
316  }
317
318  while ( numBins > 8 )
319  {
320    numBins -= 8;
321    UInt pattern = binValues >> numBins;
322    m_uiLow <<= 8;
323    m_uiLow += m_uiRange * pattern;
324    binValues -= pattern << numBins;
325    m_bitsLeft -= 8;
326
327    testAndWriteOut();
328  }
329
330  m_uiLow <<= numBins;
331  m_uiLow += m_uiRange * binValues;
332  m_bitsLeft -= numBins;
333
334  testAndWriteOut();
335}
336
337Void TEncBinCABAC::align()
338{
339  m_uiRange = 256;
340}
341
342Void TEncBinCABAC::encodeAlignedBinsEP( UInt binValues, Int numBins )
343{
344  Int binsRemaining = numBins;
345
346  assert(m_uiRange == 256); //aligned encode only works when range = 256
347
348  while (binsRemaining > 0)
349  {
350    const UInt binsToCode = std::min<UInt>(binsRemaining, 8); //code bytes if able to take advantage of the system's byte-write function
351    const UInt binMask    = (1 << binsToCode) - 1;
352
353    const UInt newBins = (binValues >> (binsRemaining - binsToCode)) & binMask;
354
355    //The process of encoding an EP bin is the same as that of coding a normal
356    //bin where the symbol ranges for 1 and 0 are both half the range:
357    //
358    //  low = (low + range/2) << 1       (to encode a 1)
359    //  low =  low            << 1       (to encode a 0)
360    //
361    //  i.e.
362    //  low = (low + (bin * range/2)) << 1
363    //
364    //  which is equivalent to:
365    //
366    //  low = (low << 1) + (bin * range)
367    //
368    //  this can be generalised for multiple bins, producing the following expression:
369    //
370    m_uiLow = (m_uiLow << binsToCode) + (newBins << 8); //range is known to be 256
371
372    binsRemaining -= binsToCode;
373    m_bitsLeft    -= binsToCode;
374
375    testAndWriteOut();
376  }
377}
378
379/**
380 * \brief Encode terminating bin
381 *
382 * \param binValue bin value
383 */
384Void TEncBinCABAC::encodeBinTrm( UInt binValue )
385{
386  m_uiBinsCoded += m_binCountIncrement;
387  m_uiRange -= 2;
388  if( binValue )
389  {
390    m_uiLow  += m_uiRange;
391    m_uiLow <<= 7;
392    m_uiRange = 2 << 7;
393    m_bitsLeft -= 7;
394  }
395  else if ( m_uiRange >= 256 )
396  {
397    return;
398  }
399  else
400  {
401    m_uiLow   <<= 1;
402    m_uiRange <<= 1;
403    m_bitsLeft--;
404  }
405
406  testAndWriteOut();
407}
408
409Void TEncBinCABAC::testAndWriteOut()
410{
411  if ( m_bitsLeft < 12 )
412  {
413    writeOut();
414  }
415}
416
417/**
418 * \brief Move bits from register into bitstream
419 */
420Void TEncBinCABAC::writeOut()
421{
422  UInt leadByte = m_uiLow >> (24 - m_bitsLeft);
423  m_bitsLeft += 8;
424  m_uiLow &= 0xffffffffu >> m_bitsLeft;
425
426  if ( leadByte == 0xff )
427  {
428    m_numBufferedBytes++;
429  }
430  else
431  {
432    if ( m_numBufferedBytes > 0 )
433    {
434      UInt carry = leadByte >> 8;
435      UInt byte = m_bufferedByte + carry;
436      m_bufferedByte = leadByte & 0xff;
437      m_pcTComBitIf->write( byte, 8 );
438
439      byte = ( 0xff + carry ) & 0xff;
440      while ( m_numBufferedBytes > 1 )
441      {
442        m_pcTComBitIf->write( byte, 8 );
443        m_numBufferedBytes--;
444      }
445    }
446    else
447    {
448      m_numBufferedBytes = 1;
449      m_bufferedByte = leadByte;
450    }
451  }
452}
453
454//! \}
Note: See TracBrowser for help on using the repository browser.