HEVC Test Model (HM)  HM-16.18
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TEncBinCoderCABAC.cpp
Go to the documentation of this file.
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-2017, 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 
38 #include "TEncBinCoderCABAC.h"
39 #include "TLibCommon/TComRom.h"
40 #include "TLibCommon/Debug.h"
41 
44 
45 
47 : m_pcTComBitIf( 0 )
48 , m_binCountIncrement( 0 )
49 #if FAST_BIT_EST
50 , m_fracBits( 0 )
51 #endif
52 {
53 }
54 
56 {
57 }
58 
60 {
61  m_pcTComBitIf = pcTComBitIf;
62 }
63 
65 {
66  m_pcTComBitIf = 0;
67 }
68 
70 {
71  m_uiLow = 0;
72  m_uiRange = 510;
73  m_bitsLeft = 23;
75  m_bufferedByte = 0xff;
76 #if FAST_BIT_EST
77  m_fracBits = 0;
78 #endif
79 }
80 
82 {
83  if ( m_uiLow >> ( 32 - m_bitsLeft ) )
84  {
85  //assert( m_numBufferedBytes > 0 );
86  //assert( m_bufferedByte != 0xff );
88  while ( m_numBufferedBytes > 1 )
89  {
90  m_pcTComBitIf->write( 0x00, 8 );
92  }
93  m_uiLow -= 1 << ( 32 - m_bitsLeft );
94  }
95  else
96  {
97  if ( m_numBufferedBytes > 0 )
98  {
100  }
101  while ( m_numBufferedBytes > 1 )
102  {
103  m_pcTComBitIf->write( 0xff, 8 );
105  }
106  }
107  m_pcTComBitIf->write( m_uiLow >> 8, 24 - m_bitsLeft );
108 }
109 
111 {
112  encodeBinTrm(1);
113  finish();
114  m_pcTComBitIf->write(1, 1);
116 
117  start();
118 }
119 
124 {
125  start();
126 }
127 
132 {
133  finish();
134  m_pcTComBitIf->write(1, 1);
135  m_pcTComBitIf->writeAlignZero(); // pcm align zero
136 }
137 
144 {
145  m_pcTComBitIf->write(uiCode, uiLength);
146 }
147 
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 #endif
159 }
160 
162 {
163  m_uiLow = 0;
164  m_bitsLeft = 23;
165  m_numBufferedBytes = 0;
166  m_bufferedByte = 0xff;
167  if ( m_binCountIncrement )
168  {
169  m_uiBinsCoded = 0;
170  }
171 #if FAST_BIT_EST
172  m_fracBits &= 32767;
173 #endif
174 }
175 
177 {
179 }
180 
188 {
189  //{
190  // DTRACE_CABAC_VL( g_nSymbolCounter++ )
191  // DTRACE_CABAC_T( "\tstate=" )
192  // DTRACE_CABAC_V( ( rcCtxModel.getState() << 1 ) + rcCtxModel.getMps() )
193  // DTRACE_CABAC_T( "\tsymbol=" )
194  // DTRACE_CABAC_V( binValue )
195  // DTRACE_CABAC_T( "\n" )
196  //}
197 
198 #if DEBUG_CABAC_BINS
199  const UInt startingRange = m_uiRange;
200 #endif
201 
203  rcCtxModel.setBinsCoded( 1 );
204 
205  UInt uiLPS = TComCABACTables::sm_aucLPSTable[ rcCtxModel.getState() ][ ( m_uiRange >> 6 ) & 3 ];
206  m_uiRange -= uiLPS;
207 
208  if( binValue != rcCtxModel.getMps() )
209  {
210  Int numBits = TComCABACTables::sm_aucRenormTable[ uiLPS >> 3 ];
211  m_uiLow = ( m_uiLow + m_uiRange ) << numBits;
212  m_uiRange = uiLPS << numBits;
213  rcCtxModel.updateLPS();
214  m_bitsLeft -= numBits;
215  testAndWriteOut();
216  }
217  else
218  {
219  rcCtxModel.updateMPS();
220 
221  if ( m_uiRange < 256 )
222  {
223  m_uiLow <<= 1;
224  m_uiRange <<= 1;
225  m_bitsLeft--;
226  testAndWriteOut();
227  }
228  }
229 
230 #if DEBUG_CABAC_BINS
231  if ((g_debugCounter + debugCabacBinWindow) >= debugCabacBinTargetLine)
232  {
233  std::cout << g_debugCounter << ": coding bin value " << binValue << ", range = [" << startingRange << "->" << m_uiRange << "]\n";
234  }
235 
236  if (g_debugCounter >= debugCabacBinTargetLine)
237  {
238  UChar breakPointThis;
239  breakPointThis = 7;
240  }
241  if (g_debugCounter >= (debugCabacBinTargetLine + debugCabacBinWindow))
242  {
243  exit(0);
244  }
245 
246  g_debugCounter++;
247 #endif
248 }
249 
256 {
257  if (false)
258  {
259  DTRACE_CABAC_VL( g_nSymbolCounter++ )
260  DTRACE_CABAC_T( "\tEPsymbol=" )
261  DTRACE_CABAC_V( binValue )
262  DTRACE_CABAC_T( "\n" )
263  }
264 
266 
267  if (m_uiRange == 256)
268  {
269  encodeAlignedBinsEP(binValue, 1);
270  return;
271  }
272 
273  m_uiLow <<= 1;
274  if( binValue )
275  {
276  m_uiLow += m_uiRange;
277  }
278  m_bitsLeft--;
279 
280  testAndWriteOut();
281 }
282 
290 {
291  m_uiBinsCoded += numBins & -m_binCountIncrement;
292 
293  if (false)
294  {
295  for ( Int i = 0; i < numBins; i++ )
296  {
297  DTRACE_CABAC_VL( g_nSymbolCounter++ )
298  DTRACE_CABAC_T( "\tEPsymbol=" )
299  DTRACE_CABAC_V( ( binValues >> ( numBins - 1 - i ) ) & 1 )
300  DTRACE_CABAC_T( "\n" )
301  }
302  }
303 
304  if (m_uiRange == 256)
305  {
306  encodeAlignedBinsEP(binValues, numBins);
307  return;
308  }
309 
310  while ( numBins > 8 )
311  {
312  numBins -= 8;
313  UInt pattern = binValues >> numBins;
314  m_uiLow <<= 8;
315  m_uiLow += m_uiRange * pattern;
316  binValues -= pattern << numBins;
317  m_bitsLeft -= 8;
318 
319  testAndWriteOut();
320  }
321 
322  m_uiLow <<= numBins;
323  m_uiLow += m_uiRange * binValues;
324  m_bitsLeft -= numBins;
325 
326  testAndWriteOut();
327 }
328 
330 {
331  m_uiRange = 256;
332 }
333 
335 {
336  Int binsRemaining = numBins;
337 
338  assert(m_uiRange == 256); //aligned encode only works when range = 256
339 
340  while (binsRemaining > 0)
341  {
342  const UInt binsToCode = std::min<UInt>(binsRemaining, 8); //code bytes if able to take advantage of the system's byte-write function
343  const UInt binMask = (1 << binsToCode) - 1;
344 
345  const UInt newBins = (binValues >> (binsRemaining - binsToCode)) & binMask;
346 
347  //The process of encoding an EP bin is the same as that of coding a normal
348  //bin where the symbol ranges for 1 and 0 are both half the range:
349  //
350  // low = (low + range/2) << 1 (to encode a 1)
351  // low = low << 1 (to encode a 0)
352  //
353  // i.e.
354  // low = (low + (bin * range/2)) << 1
355  //
356  // which is equivalent to:
357  //
358  // low = (low << 1) + (bin * range)
359  //
360  // this can be generalised for multiple bins, producing the following expression:
361  //
362  m_uiLow = (m_uiLow << binsToCode) + (newBins << 8); //range is known to be 256
363 
364  binsRemaining -= binsToCode;
365  m_bitsLeft -= binsToCode;
366 
367  testAndWriteOut();
368  }
369 }
370 
377 {
379  m_uiRange -= 2;
380  if( binValue )
381  {
382  m_uiLow += m_uiRange;
383  m_uiLow <<= 7;
384  m_uiRange = 2 << 7;
385  m_bitsLeft -= 7;
386  }
387  else if ( m_uiRange >= 256 )
388  {
389  return;
390  }
391  else
392  {
393  m_uiLow <<= 1;
394  m_uiRange <<= 1;
395  m_bitsLeft--;
396  }
397 
398  testAndWriteOut();
399 }
400 
402 {
403  if ( m_bitsLeft < 12 )
404  {
405  writeOut();
406  }
407 }
408 
413 {
414  UInt leadByte = m_uiLow >> (24 - m_bitsLeft);
415  m_bitsLeft += 8;
416  m_uiLow &= 0xffffffffu >> m_bitsLeft;
417 
418  if ( leadByte == 0xff )
419  {
421  }
422  else
423  {
424  if ( m_numBufferedBytes > 0 )
425  {
426  UInt carry = leadByte >> 8;
427  UInt byte = m_bufferedByte + carry;
428  m_bufferedByte = leadByte & 0xff;
429  m_pcTComBitIf->write( byte, 8 );
430 
431  byte = ( 0xff + carry ) & 0xff;
432  while ( m_numBufferedBytes > 1 )
433  {
434  m_pcTComBitIf->write( byte, 8 );
436  }
437  }
438  else
439  {
440  m_numBufferedBytes = 1;
441  m_bufferedByte = leadByte;
442  }
443  }
444 }
445 
Void encodeBinsEP(UInt binValues, Int numBins)
Encode equiprobable bins.
static const UChar sm_aucLPSTable[1<< CONTEXT_STATE_BITS][4]
virtual Void write(UInt uiBits, UInt uiNumberOfBits)=0
void Void
Definition: TypeDef.h:203
pure virtual class for basic bit handling
Definition: TComBitStream.h:58
global variables &amp; functions (header)
Void encodeBinTrm(UInt binValue)
Encode terminating bin.
unsigned int UInt
Definition: TypeDef.h:212
Void copyState(const TEncBinIf *pcTEncBinIf)
#define FAST_BIT_EST
G763: Table-based bit estimation for CABAC.
Definition: TypeDef.h:119
UChar getState()
get current state
Definition: ContextModel.h:63
context model class
Definition: ContextModel.h:57
Void updateLPS()
Definition: ContextModel.h:69
Void encodeBin(UInt binValue, ContextModel &rcCtxModel)
Encode bin.
virtual TEncBinCABAC * getTEncBinCABAC()
Definition: TEncBinCoder.h:74
binary entropy encoder of CABAC
#define DTRACE_CABAC_VL(x)
Definition: TComRom.h:153
unsigned char UChar
Definition: TypeDef.h:208
Void setBinsCoded(UInt val)
Definition: ContextModel.h:89
#define DTRACE_CABAC_T(x)
Definition: TComRom.h:154
Void encodeAlignedBinsEP(UInt binValues, Int numBins)
UChar getMps()
get curret MPS
Definition: ContextModel.h:64
virtual Void writeAlignZero()
Definition: TComBitStream.h:62
UInt g_debugCounter
Definition: Debug.cpp:193
#define DTRACE_CABAC_V(x)
Definition: TComRom.h:152
Void init(TComBitIf *pcTComBitIf)
int Int
Definition: TypeDef.h:211
Void writeOut()
Move bits from register into bitstream.
Void xWritePCMCode(UInt uiCode, UInt uiLength)
Void encodeBinEP(UInt binValue)
Encode equiprobable bin.
virtual ~TEncBinCABAC()
static const UChar sm_aucRenormTable[32]
TComBitIf * m_pcTComBitIf
Void updateMPS()
Definition: ContextModel.h:74
Defines types and objects for environment-variable-based debugging and feature control.
virtual UInt getNumberOfWrittenBits() const =0