source: 3DVCSoftware/branches/HTM-DEV-0.3-dev2a/source/Lib/TLibEncoder/TEncSbac.cpp @ 468

Last change on this file since 468 was 468, checked in by lg, 11 years ago

1.IC and full pel depth coding are integrated and is guarded by Macro H_3D_IC.

  • Property svn:eol-style set to native
File size: 56.1 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     TEncSbac.cpp
35    \brief    SBAC encoder class
36*/
37
38#include "TEncTop.h"
39#include "TEncSbac.h"
40
41#include <map>
42#include <algorithm>
43
44//! \ingroup TLibEncoder
45//! \{
46
47// ====================================================================================================================
48// Constructor / destructor / create / destroy
49// ====================================================================================================================
50
51TEncSbac::TEncSbac()
52// new structure here
53: m_pcBitIf                   ( NULL )
54, m_pcSlice                   ( NULL )
55, m_pcBinIf                   ( NULL )
56, m_uiCoeffCost               ( 0 )
57, m_numContextModels          ( 0 )
58, m_cCUSplitFlagSCModel       ( 1,             1,               NUM_SPLIT_FLAG_CTX            , m_contextModels + m_numContextModels, m_numContextModels )
59, m_cCUSkipFlagSCModel        ( 1,             1,               NUM_SKIP_FLAG_CTX             , m_contextModels + m_numContextModels, m_numContextModels)
60, m_cCUMergeFlagExtSCModel    ( 1,             1,               NUM_MERGE_FLAG_EXT_CTX        , m_contextModels + m_numContextModels, m_numContextModels)
61, m_cCUMergeIdxExtSCModel     ( 1,             1,               NUM_MERGE_IDX_EXT_CTX         , m_contextModels + m_numContextModels, m_numContextModels)
62#if H_3D_ARP
63, m_cCUPUARPW                 ( 1,             1,               NUM_ARPW_CTX                  , m_contextModels + m_numContextModels, m_numContextModels)
64#endif
65#if H_3D_IC
66, m_cCUICFlagSCModel          ( 1,             1,               NUM_IC_FLAG_CTX               , m_contextModels + m_numContextModels, m_numContextModels)
67#endif
68, m_cCUPartSizeSCModel        ( 1,             1,               NUM_PART_SIZE_CTX             , m_contextModels + m_numContextModels, m_numContextModels)
69, m_cCUPredModeSCModel        ( 1,             1,               NUM_PRED_MODE_CTX             , m_contextModels + m_numContextModels, m_numContextModels)
70, m_cCUIntraPredSCModel       ( 1,             1,               NUM_ADI_CTX                   , m_contextModels + m_numContextModels, m_numContextModels)
71, m_cCUChromaPredSCModel      ( 1,             1,               NUM_CHROMA_PRED_CTX           , m_contextModels + m_numContextModels, m_numContextModels)
72, m_cCUDeltaQpSCModel         ( 1,             1,               NUM_DELTA_QP_CTX              , m_contextModels + m_numContextModels, m_numContextModels)
73, m_cCUInterDirSCModel        ( 1,             1,               NUM_INTER_DIR_CTX             , m_contextModels + m_numContextModels, m_numContextModels)
74, m_cCURefPicSCModel          ( 1,             1,               NUM_REF_NO_CTX                , m_contextModels + m_numContextModels, m_numContextModels)
75, m_cCUMvdSCModel             ( 1,             1,               NUM_MV_RES_CTX                , m_contextModels + m_numContextModels, m_numContextModels)
76, m_cCUQtCbfSCModel           ( 1,             2,               NUM_QT_CBF_CTX                , m_contextModels + m_numContextModels, m_numContextModels)
77, m_cCUTransSubdivFlagSCModel ( 1,             1,               NUM_TRANS_SUBDIV_FLAG_CTX     , m_contextModels + m_numContextModels, m_numContextModels)
78, m_cCUQtRootCbfSCModel       ( 1,             1,               NUM_QT_ROOT_CBF_CTX           , m_contextModels + m_numContextModels, m_numContextModels)
79, m_cCUSigCoeffGroupSCModel   ( 1,             2,               NUM_SIG_CG_FLAG_CTX           , m_contextModels + m_numContextModels, m_numContextModels)
80, m_cCUSigSCModel             ( 1,             1,               NUM_SIG_FLAG_CTX              , m_contextModels + m_numContextModels, m_numContextModels)
81, m_cCuCtxLastX               ( 1,             2,               NUM_CTX_LAST_FLAG_XY          , m_contextModels + m_numContextModels, m_numContextModels)
82, m_cCuCtxLastY               ( 1,             2,               NUM_CTX_LAST_FLAG_XY          , m_contextModels + m_numContextModels, m_numContextModels)
83, m_cCUOneSCModel             ( 1,             1,               NUM_ONE_FLAG_CTX              , m_contextModels + m_numContextModels, m_numContextModels)
84, m_cCUAbsSCModel             ( 1,             1,               NUM_ABS_FLAG_CTX              , m_contextModels + m_numContextModels, m_numContextModels)
85, m_cMVPIdxSCModel            ( 1,             1,               NUM_MVP_IDX_CTX               , m_contextModels + m_numContextModels, m_numContextModels)
86, m_cCUAMPSCModel             ( 1,             1,               NUM_CU_AMP_CTX                , m_contextModels + m_numContextModels, m_numContextModels)
87, m_cSaoMergeSCModel          ( 1,             1,               NUM_SAO_MERGE_FLAG_CTX   , m_contextModels + m_numContextModels, m_numContextModels)
88, m_cSaoTypeIdxSCModel        ( 1,             1,               NUM_SAO_TYPE_IDX_CTX          , m_contextModels + m_numContextModels, m_numContextModels)
89, m_cTransformSkipSCModel     ( 1,             2,               NUM_TRANSFORMSKIP_FLAG_CTX    , m_contextModels + m_numContextModels, m_numContextModels)
90, m_CUTransquantBypassFlagSCModel( 1,          1,               NUM_CU_TRANSQUANT_BYPASS_FLAG_CTX, m_contextModels + m_numContextModels, m_numContextModels)
91{
92  assert( m_numContextModels <= MAX_NUM_CTX_MOD );
93}
94
95TEncSbac::~TEncSbac()
96{
97}
98
99// ====================================================================================================================
100// Public member functions
101// ====================================================================================================================
102
103Void TEncSbac::resetEntropy           ()
104{
105  Int  iQp              = m_pcSlice->getSliceQp();
106  SliceType eSliceType  = m_pcSlice->getSliceType();
107 
108  Int  encCABACTableIdx = m_pcSlice->getPPS()->getEncCABACTableIdx();
109  if (!m_pcSlice->isIntra() && (encCABACTableIdx==B_SLICE || encCABACTableIdx==P_SLICE) && m_pcSlice->getPPS()->getCabacInitPresentFlag())
110  {
111    eSliceType = (SliceType) encCABACTableIdx;
112  }
113
114  m_cCUSplitFlagSCModel.initBuffer       ( eSliceType, iQp, (UChar*)INIT_SPLIT_FLAG );
115 
116  m_cCUSkipFlagSCModel.initBuffer        ( eSliceType, iQp, (UChar*)INIT_SKIP_FLAG );
117  m_cCUMergeFlagExtSCModel.initBuffer    ( eSliceType, iQp, (UChar*)INIT_MERGE_FLAG_EXT);
118  m_cCUMergeIdxExtSCModel.initBuffer     ( eSliceType, iQp, (UChar*)INIT_MERGE_IDX_EXT);
119#if H_3D_ARP
120  m_cCUPUARPW.initBuffer                 ( eSliceType, iQp, (UChar*)INIT_ARPW );
121#endif
122#if H_3D_IC
123  m_cCUICFlagSCModel.initBuffer          ( eSliceType, iQp, (UChar*)INIT_IC_FLAG );
124#endif
125  m_cCUPartSizeSCModel.initBuffer        ( eSliceType, iQp, (UChar*)INIT_PART_SIZE );
126  m_cCUAMPSCModel.initBuffer             ( eSliceType, iQp, (UChar*)INIT_CU_AMP_POS );
127  m_cCUPredModeSCModel.initBuffer        ( eSliceType, iQp, (UChar*)INIT_PRED_MODE );
128  m_cCUIntraPredSCModel.initBuffer       ( eSliceType, iQp, (UChar*)INIT_INTRA_PRED_MODE );
129  m_cCUChromaPredSCModel.initBuffer      ( eSliceType, iQp, (UChar*)INIT_CHROMA_PRED_MODE );
130  m_cCUInterDirSCModel.initBuffer        ( eSliceType, iQp, (UChar*)INIT_INTER_DIR );
131  m_cCUMvdSCModel.initBuffer             ( eSliceType, iQp, (UChar*)INIT_MVD );
132  m_cCURefPicSCModel.initBuffer          ( eSliceType, iQp, (UChar*)INIT_REF_PIC );
133  m_cCUDeltaQpSCModel.initBuffer         ( eSliceType, iQp, (UChar*)INIT_DQP );
134  m_cCUQtCbfSCModel.initBuffer           ( eSliceType, iQp, (UChar*)INIT_QT_CBF );
135  m_cCUQtRootCbfSCModel.initBuffer       ( eSliceType, iQp, (UChar*)INIT_QT_ROOT_CBF );
136  m_cCUSigCoeffGroupSCModel.initBuffer   ( eSliceType, iQp, (UChar*)INIT_SIG_CG_FLAG );
137  m_cCUSigSCModel.initBuffer             ( eSliceType, iQp, (UChar*)INIT_SIG_FLAG );
138  m_cCuCtxLastX.initBuffer               ( eSliceType, iQp, (UChar*)INIT_LAST );
139  m_cCuCtxLastY.initBuffer               ( eSliceType, iQp, (UChar*)INIT_LAST );
140  m_cCUOneSCModel.initBuffer             ( eSliceType, iQp, (UChar*)INIT_ONE_FLAG );
141  m_cCUAbsSCModel.initBuffer             ( eSliceType, iQp, (UChar*)INIT_ABS_FLAG );
142  m_cMVPIdxSCModel.initBuffer            ( eSliceType, iQp, (UChar*)INIT_MVP_IDX );
143  m_cCUTransSubdivFlagSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_TRANS_SUBDIV_FLAG );
144  m_cSaoMergeSCModel.initBuffer      ( eSliceType, iQp, (UChar*)INIT_SAO_MERGE_FLAG );
145  m_cSaoTypeIdxSCModel.initBuffer        ( eSliceType, iQp, (UChar*)INIT_SAO_TYPE_IDX );
146  m_cTransformSkipSCModel.initBuffer     ( eSliceType, iQp, (UChar*)INIT_TRANSFORMSKIP_FLAG );
147  m_CUTransquantBypassFlagSCModel.initBuffer( eSliceType, iQp, (UChar*)INIT_CU_TRANSQUANT_BYPASS_FLAG );
148  // new structure
149  m_uiLastQp = iQp;
150 
151  m_pcBinIf->start();
152 
153  return;
154}
155
156/** The function does the following:
157 * If current slice type is P/B then it determines the distance of initialisation type 1 and 2 from the current CABAC states and
158 * stores the index of the closest table.  This index is used for the next P/B slice when cabac_init_present_flag is true.
159 */
160Void TEncSbac::determineCabacInitIdx()
161{
162  Int  qp              = m_pcSlice->getSliceQp();
163
164  if (!m_pcSlice->isIntra())
165  {
166    SliceType aSliceTypeChoices[] = {B_SLICE, P_SLICE};
167
168    UInt bestCost             = MAX_UINT;
169    SliceType bestSliceType   = aSliceTypeChoices[0];
170    for (UInt idx=0; idx<2; idx++)
171    {
172      UInt curCost          = 0;
173      SliceType curSliceType  = aSliceTypeChoices[idx];
174
175      curCost  = m_cCUSplitFlagSCModel.calcCost       ( curSliceType, qp, (UChar*)INIT_SPLIT_FLAG );
176      curCost += m_cCUSkipFlagSCModel.calcCost        ( curSliceType, qp, (UChar*)INIT_SKIP_FLAG );
177      curCost += m_cCUMergeFlagExtSCModel.calcCost    ( curSliceType, qp, (UChar*)INIT_MERGE_FLAG_EXT);
178      curCost += m_cCUMergeIdxExtSCModel.calcCost     ( curSliceType, qp, (UChar*)INIT_MERGE_IDX_EXT);
179#if H_3D_IC
180      curCost += m_cCUICFlagSCModel.calcCost          ( curSliceType, qp, (UChar*)INIT_IC_FLAG );
181#endif
182      curCost += m_cCUPartSizeSCModel.calcCost        ( curSliceType, qp, (UChar*)INIT_PART_SIZE );
183      curCost += m_cCUAMPSCModel.calcCost             ( curSliceType, qp, (UChar*)INIT_CU_AMP_POS );
184      curCost += m_cCUPredModeSCModel.calcCost        ( curSliceType, qp, (UChar*)INIT_PRED_MODE );
185      curCost += m_cCUIntraPredSCModel.calcCost       ( curSliceType, qp, (UChar*)INIT_INTRA_PRED_MODE );
186      curCost += m_cCUChromaPredSCModel.calcCost      ( curSliceType, qp, (UChar*)INIT_CHROMA_PRED_MODE );
187      curCost += m_cCUInterDirSCModel.calcCost        ( curSliceType, qp, (UChar*)INIT_INTER_DIR );
188      curCost += m_cCUMvdSCModel.calcCost             ( curSliceType, qp, (UChar*)INIT_MVD );
189      curCost += m_cCURefPicSCModel.calcCost          ( curSliceType, qp, (UChar*)INIT_REF_PIC );
190      curCost += m_cCUDeltaQpSCModel.calcCost         ( curSliceType, qp, (UChar*)INIT_DQP );
191      curCost += m_cCUQtCbfSCModel.calcCost           ( curSliceType, qp, (UChar*)INIT_QT_CBF );
192      curCost += m_cCUQtRootCbfSCModel.calcCost       ( curSliceType, qp, (UChar*)INIT_QT_ROOT_CBF );
193      curCost += m_cCUSigCoeffGroupSCModel.calcCost   ( curSliceType, qp, (UChar*)INIT_SIG_CG_FLAG );
194      curCost += m_cCUSigSCModel.calcCost             ( curSliceType, qp, (UChar*)INIT_SIG_FLAG );
195      curCost += m_cCuCtxLastX.calcCost               ( curSliceType, qp, (UChar*)INIT_LAST );
196      curCost += m_cCuCtxLastY.calcCost               ( curSliceType, qp, (UChar*)INIT_LAST );
197      curCost += m_cCUOneSCModel.calcCost             ( curSliceType, qp, (UChar*)INIT_ONE_FLAG );
198      curCost += m_cCUAbsSCModel.calcCost             ( curSliceType, qp, (UChar*)INIT_ABS_FLAG );
199      curCost += m_cMVPIdxSCModel.calcCost            ( curSliceType, qp, (UChar*)INIT_MVP_IDX );
200      curCost += m_cCUTransSubdivFlagSCModel.calcCost ( curSliceType, qp, (UChar*)INIT_TRANS_SUBDIV_FLAG );
201      curCost += m_cSaoMergeSCModel.calcCost      ( curSliceType, qp, (UChar*)INIT_SAO_MERGE_FLAG );
202      curCost += m_cSaoTypeIdxSCModel.calcCost        ( curSliceType, qp, (UChar*)INIT_SAO_TYPE_IDX );
203      curCost += m_cTransformSkipSCModel.calcCost     ( curSliceType, qp, (UChar*)INIT_TRANSFORMSKIP_FLAG );
204      curCost += m_CUTransquantBypassFlagSCModel.calcCost( curSliceType, qp, (UChar*)INIT_CU_TRANSQUANT_BYPASS_FLAG );
205      if (curCost < bestCost)
206      {
207        bestSliceType = curSliceType;
208        bestCost      = curCost;
209      }
210    }
211    m_pcSlice->getPPS()->setEncCABACTableIdx( bestSliceType );
212  }
213  else
214  {
215    m_pcSlice->getPPS()->setEncCABACTableIdx( I_SLICE );
216  } 
217}
218
219
220/** The function does the followng: Write out terminate bit. Flush CABAC. Intialize CABAC states. Start CABAC.
221 */
222Void TEncSbac::updateContextTables( SliceType eSliceType, Int iQp, Bool bExecuteFinish )
223{
224  m_pcBinIf->encodeBinTrm(1);
225  if (bExecuteFinish) m_pcBinIf->finish();
226  m_cCUSplitFlagSCModel.initBuffer       ( eSliceType, iQp, (UChar*)INIT_SPLIT_FLAG );
227 
228  m_cCUSkipFlagSCModel.initBuffer        ( eSliceType, iQp, (UChar*)INIT_SKIP_FLAG );
229  m_cCUMergeFlagExtSCModel.initBuffer    ( eSliceType, iQp, (UChar*)INIT_MERGE_FLAG_EXT);
230  m_cCUMergeIdxExtSCModel.initBuffer     ( eSliceType, iQp, (UChar*)INIT_MERGE_IDX_EXT);
231#if H_3D_ARP
232  m_cCUPUARPW.initBuffer                 ( eSliceType, iQp, (UChar*)INIT_ARPW );
233#endif
234#if H_3D_IC
235  m_cCUICFlagSCModel.initBuffer          ( eSliceType, iQp, (UChar*)INIT_IC_FLAG );
236#endif
237  m_cCUPartSizeSCModel.initBuffer        ( eSliceType, iQp, (UChar*)INIT_PART_SIZE );
238  m_cCUAMPSCModel.initBuffer             ( eSliceType, iQp, (UChar*)INIT_CU_AMP_POS );
239  m_cCUPredModeSCModel.initBuffer        ( eSliceType, iQp, (UChar*)INIT_PRED_MODE );
240  m_cCUIntraPredSCModel.initBuffer       ( eSliceType, iQp, (UChar*)INIT_INTRA_PRED_MODE );
241  m_cCUChromaPredSCModel.initBuffer      ( eSliceType, iQp, (UChar*)INIT_CHROMA_PRED_MODE );
242  m_cCUInterDirSCModel.initBuffer        ( eSliceType, iQp, (UChar*)INIT_INTER_DIR );
243  m_cCUMvdSCModel.initBuffer             ( eSliceType, iQp, (UChar*)INIT_MVD );
244  m_cCURefPicSCModel.initBuffer          ( eSliceType, iQp, (UChar*)INIT_REF_PIC );
245  m_cCUDeltaQpSCModel.initBuffer         ( eSliceType, iQp, (UChar*)INIT_DQP );
246  m_cCUQtCbfSCModel.initBuffer           ( eSliceType, iQp, (UChar*)INIT_QT_CBF );
247  m_cCUQtRootCbfSCModel.initBuffer       ( eSliceType, iQp, (UChar*)INIT_QT_ROOT_CBF );
248  m_cCUSigCoeffGroupSCModel.initBuffer   ( eSliceType, iQp, (UChar*)INIT_SIG_CG_FLAG );
249  m_cCUSigSCModel.initBuffer             ( eSliceType, iQp, (UChar*)INIT_SIG_FLAG );
250  m_cCuCtxLastX.initBuffer               ( eSliceType, iQp, (UChar*)INIT_LAST );
251  m_cCuCtxLastY.initBuffer               ( eSliceType, iQp, (UChar*)INIT_LAST );
252  m_cCUOneSCModel.initBuffer             ( eSliceType, iQp, (UChar*)INIT_ONE_FLAG );
253  m_cCUAbsSCModel.initBuffer             ( eSliceType, iQp, (UChar*)INIT_ABS_FLAG );
254  m_cMVPIdxSCModel.initBuffer            ( eSliceType, iQp, (UChar*)INIT_MVP_IDX );
255  m_cCUTransSubdivFlagSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_TRANS_SUBDIV_FLAG );
256  m_cSaoMergeSCModel.initBuffer      ( eSliceType, iQp, (UChar*)INIT_SAO_MERGE_FLAG );
257  m_cSaoTypeIdxSCModel.initBuffer        ( eSliceType, iQp, (UChar*)INIT_SAO_TYPE_IDX );
258  m_cTransformSkipSCModel.initBuffer     ( eSliceType, iQp, (UChar*)INIT_TRANSFORMSKIP_FLAG );
259  m_CUTransquantBypassFlagSCModel.initBuffer( eSliceType, iQp, (UChar*)INIT_CU_TRANSQUANT_BYPASS_FLAG );
260  m_pcBinIf->start();
261}
262
263Void TEncSbac::codeVPS( TComVPS* pcVPS )
264{
265  assert (0);
266  return;
267}
268
269#if H_3D
270Void TEncSbac::codeSPS( TComSPS* pcSPS, Int viewIndex, Bool depthFlag )
271#else
272Void TEncSbac::codeSPS( TComSPS* pcSPS )
273#endif
274{
275  assert (0);
276  return;
277}
278
279Void TEncSbac::codePPS( TComPPS* pcPPS )
280{
281  assert (0);
282  return;
283}
284
285Void TEncSbac::codeSliceHeader( TComSlice* pcSlice )
286{
287  assert (0);
288  return;
289}
290
291Void TEncSbac::codeTilesWPPEntryPoint( TComSlice* pSlice )
292{
293  assert (0);
294  return;
295}
296
297Void TEncSbac::codeTerminatingBit( UInt uilsLast )
298{
299  m_pcBinIf->encodeBinTrm( uilsLast );
300}
301
302Void TEncSbac::codeSliceFinish()
303{
304  m_pcBinIf->finish();
305}
306
307Void TEncSbac::xWriteUnarySymbol( UInt uiSymbol, ContextModel* pcSCModel, Int iOffset )
308{
309  m_pcBinIf->encodeBin( uiSymbol ? 1 : 0, pcSCModel[0] );
310 
311  if( 0 == uiSymbol)
312  {
313    return;
314  }
315 
316  while( uiSymbol-- )
317  {
318    m_pcBinIf->encodeBin( uiSymbol ? 1 : 0, pcSCModel[ iOffset ] );
319  }
320 
321  return;
322}
323
324Void TEncSbac::xWriteUnaryMaxSymbol( UInt uiSymbol, ContextModel* pcSCModel, Int iOffset, UInt uiMaxSymbol )
325{
326  if (uiMaxSymbol == 0)
327  {
328    return;
329  }
330 
331  m_pcBinIf->encodeBin( uiSymbol ? 1 : 0, pcSCModel[ 0 ] );
332 
333  if ( uiSymbol == 0 )
334  {
335    return;
336  }
337 
338  Bool bCodeLast = ( uiMaxSymbol > uiSymbol );
339 
340  while( --uiSymbol )
341  {
342    m_pcBinIf->encodeBin( 1, pcSCModel[ iOffset ] );
343  }
344  if( bCodeLast )
345  {
346    m_pcBinIf->encodeBin( 0, pcSCModel[ iOffset ] );
347  }
348 
349  return;
350}
351
352Void TEncSbac::xWriteEpExGolomb( UInt uiSymbol, UInt uiCount )
353{
354  UInt bins = 0;
355  Int numBins = 0;
356 
357  while( uiSymbol >= (UInt)(1<<uiCount) )
358  {
359    bins = 2 * bins + 1;
360    numBins++;
361    uiSymbol -= 1 << uiCount;
362    uiCount  ++;
363  }
364  bins = 2 * bins + 0;
365  numBins++;
366 
367  bins = (bins << uiCount) | uiSymbol;
368  numBins += uiCount;
369 
370  assert( numBins <= 32 );
371  m_pcBinIf->encodeBinsEP( bins, numBins );
372}
373
374/** Coding of coeff_abs_level_minus3
375 * \param uiSymbol value of coeff_abs_level_minus3
376 * \param ruiGoRiceParam reference to Rice parameter
377 * \returns Void
378 */
379Void TEncSbac::xWriteCoefRemainExGolomb ( UInt symbol, UInt &rParam )
380{
381  Int codeNumber  = (Int)symbol;
382  UInt length;
383  if (codeNumber < (COEF_REMAIN_BIN_REDUCTION << rParam))
384  {
385    length = codeNumber>>rParam;
386    m_pcBinIf->encodeBinsEP( (1<<(length+1))-2 , length+1);
387    m_pcBinIf->encodeBinsEP((codeNumber%(1<<rParam)),rParam);
388  }
389  else
390  {
391    length = rParam;
392    codeNumber  = codeNumber - ( COEF_REMAIN_BIN_REDUCTION << rParam);
393    while (codeNumber >= (1<<length))
394    {
395      codeNumber -=  (1<<(length++));   
396    }
397    m_pcBinIf->encodeBinsEP((1<<(COEF_REMAIN_BIN_REDUCTION+length+1-rParam))-2,COEF_REMAIN_BIN_REDUCTION+length+1-rParam);
398    m_pcBinIf->encodeBinsEP(codeNumber,length);
399  }
400}
401
402// SBAC RD
403Void  TEncSbac::load ( TEncSbac* pSrc)
404{
405  this->xCopyFrom(pSrc);
406}
407
408Void  TEncSbac::loadIntraDirModeLuma( TEncSbac* pSrc)
409{
410  m_pcBinIf->copyState( pSrc->m_pcBinIf );
411 
412  this->m_cCUIntraPredSCModel      .copyFrom( &pSrc->m_cCUIntraPredSCModel       );
413}
414
415
416Void  TEncSbac::store( TEncSbac* pDest)
417{
418  pDest->xCopyFrom( this );
419}
420
421
422Void TEncSbac::xCopyFrom( TEncSbac* pSrc )
423{
424  m_pcBinIf->copyState( pSrc->m_pcBinIf );
425 
426  this->m_uiCoeffCost = pSrc->m_uiCoeffCost;
427  this->m_uiLastQp    = pSrc->m_uiLastQp;
428 
429  memcpy( m_contextModels, pSrc->m_contextModels, m_numContextModels * sizeof( ContextModel ) );
430}
431
432Void TEncSbac::codeMVPIdx ( TComDataCU* pcCU, UInt uiAbsPartIdx, RefPicList eRefList )
433{
434  Int iSymbol = pcCU->getMVPIdx(eRefList, uiAbsPartIdx);
435  Int iNum = AMVP_MAX_NUM_CANDS;
436
437  xWriteUnaryMaxSymbol(iSymbol, m_cMVPIdxSCModel.get(0), 1, iNum-1);
438}
439
440Void TEncSbac::codePartSize( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
441{
442  PartSize eSize         = pcCU->getPartitionSize( uiAbsPartIdx );
443  if ( pcCU->isIntra( uiAbsPartIdx ) )
444  {
445    if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth )
446    {
447      m_pcBinIf->encodeBin( eSize == SIZE_2Nx2N? 1 : 0, m_cCUPartSizeSCModel.get( 0, 0, 0 ) );
448    }
449    return;
450  }
451 
452  switch(eSize)
453  {
454    case SIZE_2Nx2N:
455    {
456      m_pcBinIf->encodeBin( 1, m_cCUPartSizeSCModel.get( 0, 0, 0) );
457      break;
458    }
459    case SIZE_2NxN:
460    case SIZE_2NxnU:
461    case SIZE_2NxnD:
462    {
463      m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 0) );
464      m_pcBinIf->encodeBin( 1, m_cCUPartSizeSCModel.get( 0, 0, 1) );
465      if ( pcCU->getSlice()->getSPS()->getAMPAcc( uiDepth ) )
466      {
467        if (eSize == SIZE_2NxN)
468        {
469          m_pcBinIf->encodeBin(1, m_cCUAMPSCModel.get( 0, 0, 0 ));
470        }
471        else
472        {
473          m_pcBinIf->encodeBin(0, m_cCUAMPSCModel.get( 0, 0, 0 ));         
474          m_pcBinIf->encodeBinEP((eSize == SIZE_2NxnU? 0: 1));
475        }
476      }
477      break;
478    }
479    case SIZE_Nx2N:
480    case SIZE_nLx2N:
481    case SIZE_nRx2N:
482    {
483      m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 0) );
484      m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 1) );
485      if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth && !( pcCU->getWidth(uiAbsPartIdx) == 8 && pcCU->getHeight(uiAbsPartIdx) == 8 ) )
486      {
487        m_pcBinIf->encodeBin( 1, m_cCUPartSizeSCModel.get( 0, 0, 2) );
488      }
489      if ( pcCU->getSlice()->getSPS()->getAMPAcc( uiDepth ) )
490      {
491        if (eSize == SIZE_Nx2N)
492        {
493          m_pcBinIf->encodeBin(1, m_cCUAMPSCModel.get( 0, 0, 0 ));
494        }
495        else
496        {
497          m_pcBinIf->encodeBin(0, m_cCUAMPSCModel.get( 0, 0, 0 ));
498          m_pcBinIf->encodeBinEP((eSize == SIZE_nLx2N? 0: 1));
499        }
500      }
501      break;
502    }
503    case SIZE_NxN:
504    {
505      if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth && !( pcCU->getWidth(uiAbsPartIdx) == 8 && pcCU->getHeight(uiAbsPartIdx) == 8 ) )
506      {
507        m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 0) );
508        m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 1) );
509        m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 2) );
510      }
511      break;
512    }
513    default:
514    {
515      assert(0);
516    }
517  }
518}
519
520/** code prediction mode
521 * \param pcCU
522 * \param uiAbsPartIdx 
523 * \returns Void
524 */
525Void TEncSbac::codePredMode( TComDataCU* pcCU, UInt uiAbsPartIdx )
526{
527  // get context function is here
528  Int iPredMode = pcCU->getPredictionMode( uiAbsPartIdx );
529  m_pcBinIf->encodeBin( iPredMode == MODE_INTER ? 0 : 1, m_cCUPredModeSCModel.get( 0, 0, 0 ) );
530}
531
532Void TEncSbac::codeCUTransquantBypassFlag( TComDataCU* pcCU, UInt uiAbsPartIdx )
533{
534  UInt uiSymbol = pcCU->getCUTransquantBypass(uiAbsPartIdx);
535  m_pcBinIf->encodeBin( uiSymbol, m_CUTransquantBypassFlagSCModel.get( 0, 0, 0 ) );
536}
537
538/** code skip flag
539 * \param pcCU
540 * \param uiAbsPartIdx
541 * \returns Void
542 */
543Void TEncSbac::codeSkipFlag( TComDataCU* pcCU, UInt uiAbsPartIdx )
544{
545  // get context function is here
546  UInt uiSymbol = pcCU->isSkipped( uiAbsPartIdx ) ? 1 : 0;
547  UInt uiCtxSkip = pcCU->getCtxSkipFlag( uiAbsPartIdx ) ;
548  m_pcBinIf->encodeBin( uiSymbol, m_cCUSkipFlagSCModel.get( 0, 0, uiCtxSkip ) );
549  DTRACE_CABAC_VL( g_nSymbolCounter++ );
550  DTRACE_CABAC_T( "\tSkipFlag" );
551  DTRACE_CABAC_T( "\tuiCtxSkip: ");
552  DTRACE_CABAC_V( uiCtxSkip );
553  DTRACE_CABAC_T( "\tuiSymbol: ");
554  DTRACE_CABAC_V( uiSymbol );
555  DTRACE_CABAC_T( "\n");
556}
557
558/** code merge flag
559 * \param pcCU
560 * \param uiAbsPartIdx
561 * \returns Void
562 */
563Void TEncSbac::codeMergeFlag( TComDataCU* pcCU, UInt uiAbsPartIdx )
564{
565  const UInt uiSymbol = pcCU->getMergeFlag( uiAbsPartIdx ) ? 1 : 0;
566  m_pcBinIf->encodeBin( uiSymbol, *m_cCUMergeFlagExtSCModel.get( 0 ) );
567
568  DTRACE_CABAC_VL( g_nSymbolCounter++ );
569  DTRACE_CABAC_T( "\tMergeFlag: " );
570  DTRACE_CABAC_V( uiSymbol );
571  DTRACE_CABAC_T( "\tAddress: " );
572  DTRACE_CABAC_V( pcCU->getAddr() );
573  DTRACE_CABAC_T( "\tuiAbsPartIdx: " );
574  DTRACE_CABAC_V( uiAbsPartIdx );
575  DTRACE_CABAC_T( "\n" );
576}
577
578/** code merge index
579 * \param pcCU
580 * \param uiAbsPartIdx
581 * \returns Void
582 */
583Void TEncSbac::codeMergeIndex( TComDataCU* pcCU, UInt uiAbsPartIdx )
584{
585  UInt uiUnaryIdx = pcCU->getMergeIndex( uiAbsPartIdx );
586  UInt uiNumCand = pcCU->getSlice()->getMaxNumMergeCand();
587  if ( uiNumCand > 1 )
588  {
589    for( UInt ui = 0; ui < uiNumCand - 1; ++ui )
590    {
591      const UInt uiSymbol = ui == uiUnaryIdx ? 0 : 1;
592      if ( ui==0 )
593      {
594        m_pcBinIf->encodeBin( uiSymbol, m_cCUMergeIdxExtSCModel.get( 0, 0, 0 ) );
595      }
596      else
597      {
598        m_pcBinIf->encodeBinEP( uiSymbol );
599      }
600      if( uiSymbol == 0 )
601      {
602        break;
603      }
604    }
605  }
606  DTRACE_CABAC_VL( g_nSymbolCounter++ );
607  DTRACE_CABAC_T( "\tparseMergeIndex()" );
608  DTRACE_CABAC_T( "\tuiMRGIdx= " );
609  DTRACE_CABAC_V( pcCU->getMergeIndex( uiAbsPartIdx ) );
610  DTRACE_CABAC_T( "\n" );
611}
612
613#if H_3D_ARP
614Void TEncSbac::codeARPW( TComDataCU* pcCU, UInt uiAbsPartIdx )
615{
616  Int  iW = (Int)pcCU->getARPW( uiAbsPartIdx );
617  Int  iMaxW = pcCU->getSlice()->getARPStepNum() - 1;
618  assert( iMaxW > 0);
619
620  Int nOffset = pcCU->getCTXARPWFlag(uiAbsPartIdx);
621  Int nBinNum = iW + ( iW != iMaxW );
622  m_pcBinIf->encodeBin( iW ? 1 : 0 , m_cCUPUARPW.get( 0, 0, 0 + nOffset ) );
623  if( nBinNum > 1 )
624  {
625     m_pcBinIf->encodeBin( ( iW == iMaxW ) ? 1 : 0, m_cCUPUARPW.get( 0, 0, 3 ) );
626  }
627}
628#endif
629
630#if H_3D_IC
631/** code Illumination Compensation flag
632 * \param pcCU
633 * \param uiAbsPartIdx
634 * \returns Void
635 */
636Void TEncSbac::codeICFlag( TComDataCU* pcCU, UInt uiAbsPartIdx )
637{
638  // get context function is here
639  UInt uiSymbol = pcCU->getICFlag( uiAbsPartIdx ) ? 1 : 0;
640  UInt uiCtxIC  = pcCU->getCtxICFlag( uiAbsPartIdx ) ;
641  m_pcBinIf->encodeBin( uiSymbol, m_cCUICFlagSCModel.get( 0, 0, uiCtxIC ) );
642  DTRACE_CABAC_VL( g_nSymbolCounter++ );
643  DTRACE_CABAC_T( "\tICFlag" );
644  DTRACE_CABAC_T( "\tuiCtxIC: ");
645  DTRACE_CABAC_V( uiCtxIC );
646  DTRACE_CABAC_T( "\tuiSymbol: ");
647  DTRACE_CABAC_V( uiSymbol );
648  DTRACE_CABAC_T( "\n");
649}
650#endif
651
652
653Void TEncSbac::codeSplitFlag   ( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
654{
655  if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth )
656    return;
657 
658  UInt uiCtx           = pcCU->getCtxSplitFlag( uiAbsPartIdx, uiDepth );
659  UInt uiCurrSplitFlag = ( pcCU->getDepth( uiAbsPartIdx ) > uiDepth ) ? 1 : 0;
660 
661  assert( uiCtx < 3 );
662  m_pcBinIf->encodeBin( uiCurrSplitFlag, m_cCUSplitFlagSCModel.get( 0, 0, uiCtx ) );
663  DTRACE_CABAC_VL( g_nSymbolCounter++ )
664  DTRACE_CABAC_T( "\tSplitFlag\n" )
665  return;
666}
667
668Void TEncSbac::codeTransformSubdivFlag( UInt uiSymbol, UInt uiCtx )
669{
670  m_pcBinIf->encodeBin( uiSymbol, m_cCUTransSubdivFlagSCModel.get( 0, 0, uiCtx ) );
671  DTRACE_CABAC_VL( g_nSymbolCounter++ )
672  DTRACE_CABAC_T( "\tparseTransformSubdivFlag()" )
673  DTRACE_CABAC_T( "\tsymbol=" )
674  DTRACE_CABAC_V( uiSymbol )
675  DTRACE_CABAC_T( "\tctx=" )
676  DTRACE_CABAC_V( uiCtx )
677  DTRACE_CABAC_T( "\n" )
678}
679
680Void TEncSbac::codeIntraDirLumaAng( TComDataCU* pcCU, UInt absPartIdx, Bool isMultiple)
681{
682  UInt dir[4],j;
683  Int preds[4][3] = {{-1, -1, -1},{-1, -1, -1},{-1, -1, -1},{-1, -1, -1}};
684  Int predNum[4], predIdx[4] ={ -1,-1,-1,-1};
685  PartSize mode = pcCU->getPartitionSize( absPartIdx );
686  UInt partNum = isMultiple?(mode==SIZE_NxN?4:1):1;
687  UInt partOffset = ( pcCU->getPic()->getNumPartInCU() >> ( pcCU->getDepth(absPartIdx) << 1 ) ) >> 2;
688  for (j=0;j<partNum;j++)
689  {
690    dir[j] = pcCU->getLumaIntraDir( absPartIdx+partOffset*j );
691    predNum[j] = pcCU->getIntraDirLumaPredictor(absPartIdx+partOffset*j, preds[j]); 
692    for(UInt i = 0; i < predNum[j]; i++)
693    {
694      if(dir[j] == preds[j][i])
695      {
696        predIdx[j] = i;
697      }
698    }
699    m_pcBinIf->encodeBin((predIdx[j] != -1)? 1 : 0, m_cCUIntraPredSCModel.get( 0, 0, 0 ) );
700  } 
701  for (j=0;j<partNum;j++)
702  {
703    if(predIdx[j] != -1)
704    {
705      m_pcBinIf->encodeBinEP( predIdx[j] ? 1 : 0 );
706      if (predIdx[j])
707      {
708        m_pcBinIf->encodeBinEP( predIdx[j]-1 );
709      }
710    }
711    else
712    {
713      if (preds[j][0] > preds[j][1])
714      { 
715        std::swap(preds[j][0], preds[j][1]); 
716      }
717      if (preds[j][0] > preds[j][2])
718      {
719        std::swap(preds[j][0], preds[j][2]);
720      }
721      if (preds[j][1] > preds[j][2])
722      {
723        std::swap(preds[j][1], preds[j][2]);
724      }
725      for(Int i = (predNum[j] - 1); i >= 0; i--)
726      {
727        dir[j] = dir[j] > preds[j][i] ? dir[j] - 1 : dir[j];
728      }
729      m_pcBinIf->encodeBinsEP( dir[j], 5 );
730    }
731  }
732  return;
733}
734
735Void TEncSbac::codeIntraDirChroma( TComDataCU* pcCU, UInt uiAbsPartIdx )
736{
737  UInt uiIntraDirChroma = pcCU->getChromaIntraDir( uiAbsPartIdx );
738
739  if( uiIntraDirChroma == DM_CHROMA_IDX ) 
740  {
741    m_pcBinIf->encodeBin( 0, m_cCUChromaPredSCModel.get( 0, 0, 0 ) );
742  }
743  else
744  { 
745    UInt uiAllowedChromaDir[ NUM_CHROMA_MODE ];
746    pcCU->getAllowedChromaDir( uiAbsPartIdx, uiAllowedChromaDir );
747
748    for( Int i = 0; i < NUM_CHROMA_MODE - 1; i++ )
749    {
750      if( uiIntraDirChroma == uiAllowedChromaDir[i] )
751      {
752        uiIntraDirChroma = i;
753        break;
754      }
755    }
756    m_pcBinIf->encodeBin( 1, m_cCUChromaPredSCModel.get( 0, 0, 0 ) );
757
758    m_pcBinIf->encodeBinsEP( uiIntraDirChroma, 2 );
759  }
760  return;
761}
762
763Void TEncSbac::codeInterDir( TComDataCU* pcCU, UInt uiAbsPartIdx )
764{
765  const UInt uiInterDir = pcCU->getInterDir( uiAbsPartIdx ) - 1;
766  const UInt uiCtx      = pcCU->getCtxInterDir( uiAbsPartIdx );
767  ContextModel *pCtx    = m_cCUInterDirSCModel.get( 0 );
768  if (pcCU->getPartitionSize(uiAbsPartIdx) == SIZE_2Nx2N || pcCU->getHeight(uiAbsPartIdx) != 8 )
769  {
770    m_pcBinIf->encodeBin( uiInterDir == 2 ? 1 : 0, *( pCtx + uiCtx ) );
771  }
772  if (uiInterDir < 2)
773  {
774    m_pcBinIf->encodeBin( uiInterDir, *( pCtx + 4 ) );
775  }
776  return;
777}
778
779Void TEncSbac::codeRefFrmIdx( TComDataCU* pcCU, UInt uiAbsPartIdx, RefPicList eRefList )
780{
781  {
782    Int iRefFrame = pcCU->getCUMvField( eRefList )->getRefIdx( uiAbsPartIdx );
783    ContextModel *pCtx = m_cCURefPicSCModel.get( 0 );
784    m_pcBinIf->encodeBin( ( iRefFrame == 0 ? 0 : 1 ), *pCtx );
785   
786    if( iRefFrame > 0 )
787    {
788      UInt uiRefNum = pcCU->getSlice()->getNumRefIdx( eRefList ) - 2;
789      pCtx++;
790      iRefFrame--;
791      for( UInt ui = 0; ui < uiRefNum; ++ui )
792      {
793        const UInt uiSymbol = ui == iRefFrame ? 0 : 1;
794        if( ui == 0 )
795        {
796          m_pcBinIf->encodeBin( uiSymbol, *pCtx );       
797        }
798        else
799        {
800          m_pcBinIf->encodeBinEP( uiSymbol );
801        }
802        if( uiSymbol == 0 )
803        {
804          break;
805        }
806      }
807    }
808  }
809  return;
810}
811
812Void TEncSbac::codeMvd( TComDataCU* pcCU, UInt uiAbsPartIdx, RefPicList eRefList )
813{
814  if(pcCU->getSlice()->getMvdL1ZeroFlag() && eRefList == REF_PIC_LIST_1 && pcCU->getInterDir(uiAbsPartIdx)==3)
815  {
816    return;
817  }
818
819  const TComCUMvField* pcCUMvField = pcCU->getCUMvField( eRefList );
820  const Int iHor = pcCUMvField->getMvd( uiAbsPartIdx ).getHor();
821  const Int iVer = pcCUMvField->getMvd( uiAbsPartIdx ).getVer();
822  ContextModel* pCtx = m_cCUMvdSCModel.get( 0 );
823
824  m_pcBinIf->encodeBin( iHor != 0 ? 1 : 0, *pCtx );
825  m_pcBinIf->encodeBin( iVer != 0 ? 1 : 0, *pCtx );
826
827  const Bool bHorAbsGr0 = iHor != 0;
828  const Bool bVerAbsGr0 = iVer != 0;
829  const UInt uiHorAbs   = 0 > iHor ? -iHor : iHor;
830  const UInt uiVerAbs   = 0 > iVer ? -iVer : iVer;
831  pCtx++;
832
833  if( bHorAbsGr0 )
834  {
835    m_pcBinIf->encodeBin( uiHorAbs > 1 ? 1 : 0, *pCtx );
836  }
837
838  if( bVerAbsGr0 )
839  {
840    m_pcBinIf->encodeBin( uiVerAbs > 1 ? 1 : 0, *pCtx );
841  }
842
843  if( bHorAbsGr0 )
844  {
845    if( uiHorAbs > 1 )
846    {
847      xWriteEpExGolomb( uiHorAbs-2, 1 );
848    }
849
850    m_pcBinIf->encodeBinEP( 0 > iHor ? 1 : 0 );
851  }
852
853  if( bVerAbsGr0 )
854  {
855    if( uiVerAbs > 1 )
856    {
857      xWriteEpExGolomb( uiVerAbs-2, 1 );
858    }
859
860    m_pcBinIf->encodeBinEP( 0 > iVer ? 1 : 0 );
861  }
862 
863  return;
864}
865
866Void TEncSbac::codeDeltaQP( TComDataCU* pcCU, UInt uiAbsPartIdx )
867{
868  Int iDQp  = pcCU->getQP( uiAbsPartIdx ) - pcCU->getRefQP( uiAbsPartIdx );
869 
870  Int qpBdOffsetY =  pcCU->getSlice()->getSPS()->getQpBDOffsetY();
871  iDQp = (iDQp + 78 + qpBdOffsetY + (qpBdOffsetY/2)) % (52 + qpBdOffsetY) - 26 - (qpBdOffsetY/2);
872
873  UInt uiAbsDQp = (UInt)((iDQp > 0)? iDQp  : (-iDQp));
874  UInt TUValue = min((Int)uiAbsDQp, CU_DQP_TU_CMAX);
875  xWriteUnaryMaxSymbol( TUValue, &m_cCUDeltaQpSCModel.get( 0, 0, 0 ), 1, CU_DQP_TU_CMAX);
876  if( uiAbsDQp >= CU_DQP_TU_CMAX )
877  {
878    xWriteEpExGolomb( uiAbsDQp - CU_DQP_TU_CMAX, CU_DQP_EG_k );
879  }
880
881  if ( uiAbsDQp > 0)
882  {
883    UInt uiSign = (iDQp > 0 ? 0 : 1);
884    m_pcBinIf->encodeBinEP(uiSign);
885  }
886
887  return;
888}
889
890Void TEncSbac::codeQtCbf( TComDataCU* pcCU, UInt uiAbsPartIdx, TextType eType, UInt uiTrDepth )
891{
892  UInt uiCbf = pcCU->getCbf     ( uiAbsPartIdx, eType, uiTrDepth );
893  UInt uiCtx = pcCU->getCtxQtCbf( eType, uiTrDepth );
894  m_pcBinIf->encodeBin( uiCbf , m_cCUQtCbfSCModel.get( 0, eType ? TEXT_CHROMA : eType, uiCtx ) );
895  DTRACE_CABAC_VL( g_nSymbolCounter++ )
896  DTRACE_CABAC_T( "\tparseQtCbf()" )
897  DTRACE_CABAC_T( "\tsymbol=" )
898  DTRACE_CABAC_V( uiCbf )
899  DTRACE_CABAC_T( "\tctx=" )
900  DTRACE_CABAC_V( uiCtx )
901  DTRACE_CABAC_T( "\tetype=" )
902  DTRACE_CABAC_V( eType )
903  DTRACE_CABAC_T( "\tuiAbsPartIdx=" )
904  DTRACE_CABAC_V( uiAbsPartIdx )
905  DTRACE_CABAC_T( "\n" )
906}
907
908void TEncSbac::codeTransformSkipFlags (TComDataCU* pcCU, UInt uiAbsPartIdx, UInt width, UInt height, TextType eTType )
909{
910  if (pcCU->getCUTransquantBypass(uiAbsPartIdx))
911  {
912    return;
913  }
914  if(width != 4 || height != 4)
915  {
916    return;
917  }
918
919  UInt useTransformSkip = pcCU->getTransformSkip( uiAbsPartIdx,eTType);
920  m_pcBinIf->encodeBin( useTransformSkip, m_cTransformSkipSCModel.get( 0, eTType? TEXT_CHROMA: TEXT_LUMA, 0 ) );
921  DTRACE_CABAC_VL( g_nSymbolCounter++ )
922  DTRACE_CABAC_T("\tparseTransformSkip()");
923  DTRACE_CABAC_T( "\tsymbol=" )
924  DTRACE_CABAC_V( useTransformSkip )
925  DTRACE_CABAC_T( "\tAddr=" )
926  DTRACE_CABAC_V( pcCU->getAddr() )
927  DTRACE_CABAC_T( "\tetype=" )
928  DTRACE_CABAC_V( eTType )
929  DTRACE_CABAC_T( "\tuiAbsPartIdx=" )
930  DTRACE_CABAC_V( uiAbsPartIdx )
931  DTRACE_CABAC_T( "\n" )
932}
933
934/** Code I_PCM information.
935 * \param pcCU pointer to CU
936 * \param uiAbsPartIdx CU index
937 * \returns Void
938 */
939Void TEncSbac::codeIPCMInfo( TComDataCU* pcCU, UInt uiAbsPartIdx )
940{
941  UInt uiIPCM = (pcCU->getIPCMFlag(uiAbsPartIdx) == true)? 1 : 0;
942
943  Bool writePCMSampleFlag = pcCU->getIPCMFlag(uiAbsPartIdx);
944
945  m_pcBinIf->encodeBinTrm (uiIPCM);
946
947  if (writePCMSampleFlag)
948  {
949    m_pcBinIf->encodePCMAlignBits();
950
951    UInt uiMinCoeffSize = pcCU->getPic()->getMinCUWidth()*pcCU->getPic()->getMinCUHeight();
952    UInt uiLumaOffset   = uiMinCoeffSize*uiAbsPartIdx;
953    UInt uiChromaOffset = uiLumaOffset>>2;
954    Pel* piPCMSample;
955    UInt uiWidth;
956    UInt uiHeight;
957    UInt uiSampleBits;
958    UInt uiX, uiY;
959
960    piPCMSample = pcCU->getPCMSampleY() + uiLumaOffset;
961    uiWidth = pcCU->getWidth(uiAbsPartIdx);
962    uiHeight = pcCU->getHeight(uiAbsPartIdx);
963    uiSampleBits = pcCU->getSlice()->getSPS()->getPCMBitDepthLuma();
964
965    for(uiY = 0; uiY < uiHeight; uiY++)
966    {
967      for(uiX = 0; uiX < uiWidth; uiX++)
968      {
969        UInt uiSample = piPCMSample[uiX];
970
971        m_pcBinIf->xWritePCMCode(uiSample, uiSampleBits);
972      }
973      piPCMSample += uiWidth;
974    }
975
976    piPCMSample = pcCU->getPCMSampleCb() + uiChromaOffset;
977    uiWidth = pcCU->getWidth(uiAbsPartIdx)/2;
978    uiHeight = pcCU->getHeight(uiAbsPartIdx)/2;
979    uiSampleBits = pcCU->getSlice()->getSPS()->getPCMBitDepthChroma();
980
981    for(uiY = 0; uiY < uiHeight; uiY++)
982    {
983      for(uiX = 0; uiX < uiWidth; uiX++)
984      {
985        UInt uiSample = piPCMSample[uiX];
986
987        m_pcBinIf->xWritePCMCode(uiSample, uiSampleBits);
988      }
989      piPCMSample += uiWidth;
990    }
991
992    piPCMSample = pcCU->getPCMSampleCr() + uiChromaOffset;
993    uiWidth = pcCU->getWidth(uiAbsPartIdx)/2;
994    uiHeight = pcCU->getHeight(uiAbsPartIdx)/2;
995    uiSampleBits = pcCU->getSlice()->getSPS()->getPCMBitDepthChroma();
996
997    for(uiY = 0; uiY < uiHeight; uiY++)
998    {
999      for(uiX = 0; uiX < uiWidth; uiX++)
1000      {
1001        UInt uiSample = piPCMSample[uiX];
1002
1003        m_pcBinIf->xWritePCMCode(uiSample, uiSampleBits);
1004      }
1005      piPCMSample += uiWidth;
1006    }
1007    m_pcBinIf->resetBac();
1008  }
1009}
1010
1011Void TEncSbac::codeQtRootCbf( TComDataCU* pcCU, UInt uiAbsPartIdx )
1012{
1013  UInt uiCbf = pcCU->getQtRootCbf( uiAbsPartIdx );
1014  UInt uiCtx = 0;
1015  m_pcBinIf->encodeBin( uiCbf , m_cCUQtRootCbfSCModel.get( 0, 0, uiCtx ) );
1016  DTRACE_CABAC_VL( g_nSymbolCounter++ )
1017  DTRACE_CABAC_T( "\tparseQtRootCbf()" )
1018  DTRACE_CABAC_T( "\tsymbol=" )
1019  DTRACE_CABAC_V( uiCbf )
1020  DTRACE_CABAC_T( "\tctx=" )
1021  DTRACE_CABAC_V( uiCtx )
1022  DTRACE_CABAC_T( "\tuiAbsPartIdx=" )
1023  DTRACE_CABAC_V( uiAbsPartIdx )
1024  DTRACE_CABAC_T( "\n" )
1025}
1026
1027Void TEncSbac::codeQtCbfZero( TComDataCU* pcCU, TextType eType, UInt uiTrDepth )
1028{
1029  // this function is only used to estimate the bits when cbf is 0
1030  // and will never be called when writing the bistream. do not need to write log
1031  UInt uiCbf = 0;
1032  UInt uiCtx = pcCU->getCtxQtCbf( eType, uiTrDepth );
1033  m_pcBinIf->encodeBin( uiCbf , m_cCUQtCbfSCModel.get( 0, eType ? TEXT_CHROMA : eType, uiCtx ) );
1034}
1035
1036Void TEncSbac::codeQtRootCbfZero( TComDataCU* pcCU )
1037{
1038  // this function is only used to estimate the bits when cbf is 0
1039  // and will never be called when writing the bistream. do not need to write log
1040  UInt uiCbf = 0;
1041  UInt uiCtx = 0;
1042  m_pcBinIf->encodeBin( uiCbf , m_cCUQtRootCbfSCModel.get( 0, 0, uiCtx ) );
1043}
1044
1045/** Encode (X,Y) position of the last significant coefficient
1046 * \param uiPosX X component of last coefficient
1047 * \param uiPosY Y component of last coefficient
1048 * \param width  Block width
1049 * \param height Block height
1050 * \param eTType plane type / luminance or chrominance
1051 * \param uiScanIdx scan type (zig-zag, hor, ver)
1052 * This method encodes the X and Y component within a block of the last significant coefficient.
1053 */
1054Void TEncSbac::codeLastSignificantXY( UInt uiPosX, UInt uiPosY, Int width, Int height, TextType eTType, UInt uiScanIdx )
1055{ 
1056  // swap
1057  if( uiScanIdx == SCAN_VER )
1058  {
1059    swap( uiPosX, uiPosY );
1060  }
1061
1062  UInt uiCtxLast;
1063  ContextModel *pCtxX = m_cCuCtxLastX.get( 0, eTType );
1064  ContextModel *pCtxY = m_cCuCtxLastY.get( 0, eTType );
1065  UInt uiGroupIdxX    = g_uiGroupIdx[ uiPosX ];
1066  UInt uiGroupIdxY    = g_uiGroupIdx[ uiPosY ];
1067
1068
1069  Int blkSizeOffsetX, blkSizeOffsetY, shiftX, shiftY;
1070  blkSizeOffsetX = eTType ? 0: (g_aucConvertToBit[ width ] *3 + ((g_aucConvertToBit[ width ] +1)>>2));
1071  blkSizeOffsetY = eTType ? 0: (g_aucConvertToBit[ height ]*3 + ((g_aucConvertToBit[ height ]+1)>>2));
1072  shiftX= eTType ? g_aucConvertToBit[ width  ] :((g_aucConvertToBit[ width  ]+3)>>2);
1073  shiftY= eTType ? g_aucConvertToBit[ height ] :((g_aucConvertToBit[ height ]+3)>>2);
1074  // posX
1075  for( uiCtxLast = 0; uiCtxLast < uiGroupIdxX; uiCtxLast++ )
1076  {
1077      m_pcBinIf->encodeBin( 1, *( pCtxX + blkSizeOffsetX + (uiCtxLast >>shiftX) ) );
1078  }
1079  if( uiGroupIdxX < g_uiGroupIdx[ width - 1 ])
1080  {
1081      m_pcBinIf->encodeBin( 0, *( pCtxX + blkSizeOffsetX + (uiCtxLast >>shiftX) ) );
1082  }
1083
1084  // posY
1085  for( uiCtxLast = 0; uiCtxLast < uiGroupIdxY; uiCtxLast++ )
1086  {
1087    m_pcBinIf->encodeBin( 1, *( pCtxY + blkSizeOffsetY + (uiCtxLast >>shiftY) ) );
1088  }
1089  if( uiGroupIdxY < g_uiGroupIdx[ height - 1 ])
1090  {
1091    m_pcBinIf->encodeBin( 0, *( pCtxY + blkSizeOffsetY + (uiCtxLast >>shiftY) ) );
1092  }
1093  if ( uiGroupIdxX > 3 )
1094  {     
1095    UInt uiCount = ( uiGroupIdxX - 2 ) >> 1;
1096    uiPosX       = uiPosX - g_uiMinInGroup[ uiGroupIdxX ];
1097    for (Int i = uiCount - 1 ; i >= 0; i-- )
1098    {
1099      m_pcBinIf->encodeBinEP( ( uiPosX >> i ) & 1 );
1100    }
1101  }
1102  if ( uiGroupIdxY > 3 )
1103  {     
1104    UInt uiCount = ( uiGroupIdxY - 2 ) >> 1;
1105    uiPosY       = uiPosY - g_uiMinInGroup[ uiGroupIdxY ];
1106    for ( Int i = uiCount - 1 ; i >= 0; i-- )
1107    {
1108      m_pcBinIf->encodeBinEP( ( uiPosY >> i ) & 1 );
1109    }
1110  }
1111}
1112
1113Void TEncSbac::codeCoeffNxN( TComDataCU* pcCU, TCoeff* pcCoef, UInt uiAbsPartIdx, UInt uiWidth, UInt uiHeight, UInt uiDepth, TextType eTType )
1114{
1115  DTRACE_CABAC_VL( g_nSymbolCounter++ )
1116  DTRACE_CABAC_T( "\tparseCoeffNxN()\teType=" )
1117  DTRACE_CABAC_V( eTType )
1118  DTRACE_CABAC_T( "\twidth=" )
1119  DTRACE_CABAC_V( uiWidth )
1120  DTRACE_CABAC_T( "\theight=" )
1121  DTRACE_CABAC_V( uiHeight )
1122  DTRACE_CABAC_T( "\tdepth=" )
1123  DTRACE_CABAC_V( uiDepth )
1124  DTRACE_CABAC_T( "\tabspartidx=" )
1125  DTRACE_CABAC_V( uiAbsPartIdx )
1126  DTRACE_CABAC_T( "\ttoCU-X=" )
1127  DTRACE_CABAC_V( pcCU->getCUPelX() )
1128  DTRACE_CABAC_T( "\ttoCU-Y=" )
1129  DTRACE_CABAC_V( pcCU->getCUPelY() )
1130  DTRACE_CABAC_T( "\tCU-addr=" )
1131  DTRACE_CABAC_V(  pcCU->getAddr() )
1132  DTRACE_CABAC_T( "\tinCU-X=" )
1133  DTRACE_CABAC_V( g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsPartIdx] ] )
1134  DTRACE_CABAC_T( "\tinCU-Y=" )
1135  DTRACE_CABAC_V( g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsPartIdx] ] )
1136  DTRACE_CABAC_T( "\tpredmode=" )
1137  DTRACE_CABAC_V(  pcCU->getPredictionMode( uiAbsPartIdx ) )
1138  DTRACE_CABAC_T( "\n" )
1139
1140  if( uiWidth > m_pcSlice->getSPS()->getMaxTrSize() )
1141  {
1142    uiWidth  = m_pcSlice->getSPS()->getMaxTrSize();
1143    uiHeight = m_pcSlice->getSPS()->getMaxTrSize();
1144  }
1145 
1146  UInt uiNumSig = 0;
1147 
1148  // compute number of significant coefficients
1149  uiNumSig = TEncEntropy::countNonZeroCoeffs(pcCoef, uiWidth * uiHeight);
1150 
1151  if ( uiNumSig == 0 )
1152    return;
1153  if(pcCU->getSlice()->getPPS()->getUseTransformSkip())
1154  {
1155    codeTransformSkipFlags( pcCU,uiAbsPartIdx, uiWidth, uiHeight, eTType );
1156  }
1157  eTType = eTType == TEXT_LUMA ? TEXT_LUMA : ( eTType == TEXT_NONE ? TEXT_NONE : TEXT_CHROMA );
1158 
1159  //----- encode significance map -----
1160  const UInt   uiLog2BlockSize = g_aucConvertToBit[ uiWidth ] + 2;
1161  UInt uiScanIdx = pcCU->getCoefScanIdx(uiAbsPartIdx, uiWidth, eTType==TEXT_LUMA, pcCU->isIntra(uiAbsPartIdx));
1162  const UInt *scan = g_auiSigLastScan[ uiScanIdx ][ uiLog2BlockSize - 1 ];
1163 
1164  Bool beValid;
1165  if (pcCU->getCUTransquantBypass(uiAbsPartIdx))
1166  {
1167    beValid = false;
1168  }
1169  else 
1170  {
1171    beValid = pcCU->getSlice()->getPPS()->getSignHideFlag() > 0;
1172  }
1173
1174  // Find position of last coefficient
1175  Int scanPosLast = -1;
1176  Int posLast;
1177
1178  const UInt * scanCG;
1179  {
1180    scanCG = g_auiSigLastScan[ uiScanIdx ][ uiLog2BlockSize > 3 ? uiLog2BlockSize-2-1 : 0 ];
1181    if( uiLog2BlockSize == 3 )
1182    {
1183      scanCG = g_sigLastScan8x8[ uiScanIdx ];
1184    }
1185    else if( uiLog2BlockSize == 5 )
1186    {
1187      scanCG = g_sigLastScanCG32x32;
1188    }
1189  }
1190  UInt uiSigCoeffGroupFlag[ MLS_GRP_NUM ];
1191  static const UInt uiShift = MLS_CG_SIZE >> 1;
1192  const UInt uiNumBlkSide = uiWidth >> uiShift;
1193
1194    ::memset( uiSigCoeffGroupFlag, 0, sizeof(UInt) * MLS_GRP_NUM );
1195
1196    do
1197    {
1198      posLast = scan[ ++scanPosLast ];
1199
1200      // get L1 sig map
1201      UInt uiPosY    = posLast >> uiLog2BlockSize;
1202      UInt uiPosX    = posLast - ( uiPosY << uiLog2BlockSize );
1203      UInt uiBlkIdx  = uiNumBlkSide * (uiPosY >> uiShift) + (uiPosX >> uiShift);
1204      if( pcCoef[ posLast ] )
1205      {
1206        uiSigCoeffGroupFlag[ uiBlkIdx ] = 1;
1207      }
1208
1209      uiNumSig -= ( pcCoef[ posLast ] != 0 );
1210    }
1211    while ( uiNumSig > 0 );
1212
1213  // Code position of last coefficient
1214  Int posLastY = posLast >> uiLog2BlockSize;
1215  Int posLastX = posLast - ( posLastY << uiLog2BlockSize );
1216  codeLastSignificantXY(posLastX, posLastY, uiWidth, uiHeight, eTType, uiScanIdx);
1217 
1218  //===== code significance flag =====
1219  ContextModel * const baseCoeffGroupCtx = m_cCUSigCoeffGroupSCModel.get( 0, eTType );
1220  ContextModel * const baseCtx = (eTType==TEXT_LUMA) ? m_cCUSigSCModel.get( 0, 0 ) : m_cCUSigSCModel.get( 0, 0 ) + NUM_SIG_FLAG_CTX_LUMA;
1221
1222
1223  const Int  iLastScanSet      = scanPosLast >> LOG2_SCAN_SET_SIZE;
1224  UInt c1 = 1;
1225  UInt uiGoRiceParam           = 0;
1226  Int  iScanPosSig             = scanPosLast;
1227
1228  for( Int iSubSet = iLastScanSet; iSubSet >= 0; iSubSet-- )
1229  {
1230    Int numNonZero = 0;
1231    Int  iSubPos     = iSubSet << LOG2_SCAN_SET_SIZE;
1232    uiGoRiceParam    = 0;
1233    Int absCoeff[16];
1234    UInt coeffSigns = 0;
1235
1236    Int lastNZPosInCG = -1, firstNZPosInCG = SCAN_SET_SIZE;
1237
1238    if( iScanPosSig == scanPosLast )
1239    {
1240      absCoeff[ 0 ] = abs( pcCoef[ posLast ] );
1241      coeffSigns    = ( pcCoef[ posLast ] < 0 );
1242      numNonZero    = 1;
1243      lastNZPosInCG  = iScanPosSig;
1244      firstNZPosInCG = iScanPosSig;
1245      iScanPosSig--;
1246    }
1247
1248      // encode significant_coeffgroup_flag
1249      Int iCGBlkPos = scanCG[ iSubSet ];
1250      Int iCGPosY   = iCGBlkPos / uiNumBlkSide;
1251      Int iCGPosX   = iCGBlkPos - (iCGPosY * uiNumBlkSide);
1252      if( iSubSet == iLastScanSet || iSubSet == 0)
1253      {
1254        uiSigCoeffGroupFlag[ iCGBlkPos ] = 1;
1255      }
1256      else
1257      {
1258          UInt uiSigCoeffGroup   = (uiSigCoeffGroupFlag[ iCGBlkPos ] != 0);
1259          UInt uiCtxSig  = TComTrQuant::getSigCoeffGroupCtxInc( uiSigCoeffGroupFlag, iCGPosX, iCGPosY, uiWidth, uiHeight );
1260          m_pcBinIf->encodeBin( uiSigCoeffGroup, baseCoeffGroupCtx[ uiCtxSig ] );
1261      }
1262     
1263      // encode significant_coeff_flag
1264      if( uiSigCoeffGroupFlag[ iCGBlkPos ] )
1265      {
1266        Int patternSigCtx = TComTrQuant::calcPatternSigCtx( uiSigCoeffGroupFlag, iCGPosX, iCGPosY, uiWidth, uiHeight );
1267        UInt uiBlkPos, uiPosY, uiPosX, uiSig, uiCtxSig;
1268        for( ; iScanPosSig >= iSubPos; iScanPosSig-- )
1269        {
1270          uiBlkPos  = scan[ iScanPosSig ]; 
1271          uiPosY    = uiBlkPos >> uiLog2BlockSize;
1272          uiPosX    = uiBlkPos - ( uiPosY << uiLog2BlockSize );
1273          uiSig     = (pcCoef[ uiBlkPos ] != 0);
1274          if( iScanPosSig > iSubPos || iSubSet == 0 || numNonZero )
1275          {
1276            uiCtxSig  = TComTrQuant::getSigCtxInc( patternSigCtx, uiScanIdx, uiPosX, uiPosY, uiLog2BlockSize, eTType );
1277            m_pcBinIf->encodeBin( uiSig, baseCtx[ uiCtxSig ] );
1278          }
1279          if( uiSig )
1280          {
1281            absCoeff[ numNonZero ] = abs( pcCoef[ uiBlkPos ] );
1282            coeffSigns = 2 * coeffSigns + ( pcCoef[ uiBlkPos ] < 0 );
1283            numNonZero++;
1284            if( lastNZPosInCG == -1 )
1285            {
1286              lastNZPosInCG = iScanPosSig;
1287            }
1288            firstNZPosInCG = iScanPosSig;
1289          }
1290        }
1291      }
1292      else
1293      {
1294        iScanPosSig = iSubPos - 1;
1295      }
1296
1297    if( numNonZero > 0 )
1298    {
1299      Bool signHidden = ( lastNZPosInCG - firstNZPosInCG >= SBH_THRESHOLD );
1300      UInt uiCtxSet = (iSubSet > 0 && eTType==TEXT_LUMA) ? 2 : 0;
1301     
1302      if( c1 == 0 )
1303      {
1304        uiCtxSet++;
1305      }
1306      c1 = 1;
1307      ContextModel *baseCtxMod = ( eTType==TEXT_LUMA ) ? m_cCUOneSCModel.get( 0, 0 ) + 4 * uiCtxSet : m_cCUOneSCModel.get( 0, 0 ) + NUM_ONE_FLAG_CTX_LUMA + 4 * uiCtxSet;
1308     
1309      Int numC1Flag = min(numNonZero, C1FLAG_NUMBER);
1310      Int firstC2FlagIdx = -1;
1311      for( Int idx = 0; idx < numC1Flag; idx++ )
1312      {
1313        UInt uiSymbol = absCoeff[ idx ] > 1;
1314        m_pcBinIf->encodeBin( uiSymbol, baseCtxMod[c1] );
1315        if( uiSymbol )
1316        {
1317          c1 = 0;
1318
1319          if (firstC2FlagIdx == -1)
1320          {
1321            firstC2FlagIdx = idx;
1322          }
1323        }
1324        else if( (c1 < 3) && (c1 > 0) )
1325        {
1326          c1++;
1327        }
1328      }
1329     
1330      if (c1 == 0)
1331      {
1332
1333        baseCtxMod = ( eTType==TEXT_LUMA ) ? m_cCUAbsSCModel.get( 0, 0 ) + uiCtxSet : m_cCUAbsSCModel.get( 0, 0 ) + NUM_ABS_FLAG_CTX_LUMA + uiCtxSet;
1334        if ( firstC2FlagIdx != -1)
1335        {
1336          UInt symbol = absCoeff[ firstC2FlagIdx ] > 2;
1337          m_pcBinIf->encodeBin( symbol, baseCtxMod[0] );
1338        }
1339      }
1340     
1341      if( beValid && signHidden )
1342      {
1343        m_pcBinIf->encodeBinsEP( (coeffSigns >> 1), numNonZero-1 );
1344      }
1345      else
1346      {
1347        m_pcBinIf->encodeBinsEP( coeffSigns, numNonZero );
1348      }
1349     
1350      Int iFirstCoeff2 = 1;   
1351      if (c1 == 0 || numNonZero > C1FLAG_NUMBER)
1352      {
1353        for ( Int idx = 0; idx < numNonZero; idx++ )
1354        {
1355          UInt baseLevel  = (idx < C1FLAG_NUMBER)? (2 + iFirstCoeff2 ) : 1;
1356
1357          if( absCoeff[ idx ] >= baseLevel)
1358          {
1359            xWriteCoefRemainExGolomb( absCoeff[ idx ] - baseLevel, uiGoRiceParam );
1360            if(absCoeff[idx] > 3*(1<<uiGoRiceParam))
1361            {
1362               uiGoRiceParam = min<UInt>(uiGoRiceParam+ 1, 4);
1363            }
1364          }
1365          if(absCoeff[ idx ] >= 2) 
1366          {
1367            iFirstCoeff2 = 0;
1368          }
1369        }       
1370      }
1371    }
1372  }
1373
1374  return;
1375}
1376
1377/** code SAO offset sign
1378 * \param code sign value
1379 */
1380Void TEncSbac::codeSAOSign( UInt code )
1381{
1382  m_pcBinIf->encodeBinEP( code );
1383}
1384
1385Void TEncSbac::codeSaoMaxUvlc    ( UInt code, UInt maxSymbol )
1386{
1387  if (maxSymbol == 0)
1388  {
1389    return;
1390  }
1391
1392  Int i;
1393  Bool bCodeLast = ( maxSymbol > code );
1394
1395  if ( code == 0 )
1396  {
1397    m_pcBinIf->encodeBinEP( 0 );
1398  }
1399  else
1400  {
1401    m_pcBinIf->encodeBinEP( 1 );
1402    for ( i=0; i<code-1; i++ )
1403    {
1404      m_pcBinIf->encodeBinEP( 1 );
1405    }
1406    if( bCodeLast )
1407    {
1408      m_pcBinIf->encodeBinEP( 0 );
1409    }
1410  }
1411}
1412
1413
1414/** Code SAO EO class or BO band position
1415 * \param uiLength
1416 * \param uiCode
1417 */
1418Void TEncSbac::codeSaoUflc       ( UInt uiLength, UInt uiCode )
1419{
1420   m_pcBinIf->encodeBinsEP ( uiCode, uiLength );
1421}
1422/** Code SAO merge flags
1423 * \param uiCode
1424 * \param uiCompIdx
1425 */
1426Void TEncSbac::codeSaoMerge       ( UInt uiCode )
1427{
1428  if (uiCode == 0)
1429  {
1430    m_pcBinIf->encodeBin(0,  m_cSaoMergeSCModel.get( 0, 0, 0 ));
1431  }
1432  else
1433  {
1434    m_pcBinIf->encodeBin(1,  m_cSaoMergeSCModel.get( 0, 0, 0 ));
1435  }
1436}
1437/** Code SAO type index
1438 * \param uiCode
1439 */
1440Void TEncSbac::codeSaoTypeIdx       ( UInt uiCode)
1441{
1442  if (uiCode == 0)
1443  {
1444    m_pcBinIf->encodeBin( 0, m_cSaoTypeIdxSCModel.get( 0, 0, 0 ) );
1445  }
1446  else
1447  {
1448    m_pcBinIf->encodeBin( 1, m_cSaoTypeIdxSCModel.get( 0, 0, 0 ) );
1449    m_pcBinIf->encodeBinEP( uiCode <= 4 ? 1 : 0 );
1450  }
1451}
1452/*!
1453 ****************************************************************************
1454 * \brief
1455 *   estimate bit cost for CBP, significant map and significant coefficients
1456 ****************************************************************************
1457 */
1458Void TEncSbac::estBit( estBitsSbacStruct* pcEstBitsSbac, Int width, Int height, TextType eTType )
1459{
1460  estCBFBit( pcEstBitsSbac );
1461
1462  estSignificantCoeffGroupMapBit( pcEstBitsSbac, eTType );
1463 
1464  // encode significance map
1465  estSignificantMapBit( pcEstBitsSbac, width, height, eTType );
1466 
1467  // encode significant coefficients
1468  estSignificantCoefficientsBit( pcEstBitsSbac, eTType );
1469}
1470
1471/*!
1472 ****************************************************************************
1473 * \brief
1474 *    estimate bit cost for each CBP bit
1475 ****************************************************************************
1476 */
1477Void TEncSbac::estCBFBit( estBitsSbacStruct* pcEstBitsSbac )
1478{
1479  ContextModel *pCtx = m_cCUQtCbfSCModel.get( 0 );
1480
1481  for( UInt uiCtxInc = 0; uiCtxInc < 3*NUM_QT_CBF_CTX; uiCtxInc++ )
1482  {
1483    pcEstBitsSbac->blockCbpBits[ uiCtxInc ][ 0 ] = pCtx[ uiCtxInc ].getEntropyBits( 0 );
1484    pcEstBitsSbac->blockCbpBits[ uiCtxInc ][ 1 ] = pCtx[ uiCtxInc ].getEntropyBits( 1 );
1485  }
1486
1487  pCtx = m_cCUQtRootCbfSCModel.get( 0 );
1488 
1489  for( UInt uiCtxInc = 0; uiCtxInc < 4; uiCtxInc++ )
1490  {
1491    pcEstBitsSbac->blockRootCbpBits[ uiCtxInc ][ 0 ] = pCtx[ uiCtxInc ].getEntropyBits( 0 );
1492    pcEstBitsSbac->blockRootCbpBits[ uiCtxInc ][ 1 ] = pCtx[ uiCtxInc ].getEntropyBits( 1 );
1493  }
1494}
1495
1496
1497/*!
1498 ****************************************************************************
1499 * \brief
1500 *    estimate SAMBAC bit cost for significant coefficient group map
1501 ****************************************************************************
1502 */
1503Void TEncSbac::estSignificantCoeffGroupMapBit( estBitsSbacStruct* pcEstBitsSbac, TextType eTType )
1504{
1505  Int firstCtx = 0, numCtx = NUM_SIG_CG_FLAG_CTX;
1506
1507  for ( Int ctxIdx = firstCtx; ctxIdx < firstCtx + numCtx; ctxIdx++ )
1508  {
1509    for( UInt uiBin = 0; uiBin < 2; uiBin++ )
1510    {
1511      pcEstBitsSbac->significantCoeffGroupBits[ ctxIdx ][ uiBin ] = m_cCUSigCoeffGroupSCModel.get(  0, eTType, ctxIdx ).getEntropyBits( uiBin );
1512    }
1513  }
1514}
1515
1516
1517/*!
1518 ****************************************************************************
1519 * \brief
1520 *    estimate SAMBAC bit cost for significant coefficient map
1521 ****************************************************************************
1522 */
1523Void TEncSbac::estSignificantMapBit( estBitsSbacStruct* pcEstBitsSbac, Int width, Int height, TextType eTType )
1524{
1525  Int firstCtx = 1, numCtx = 8;
1526  if (max(width, height) >= 16)
1527  {
1528    firstCtx = (eTType == TEXT_LUMA) ? 21 : 12;
1529    numCtx = (eTType == TEXT_LUMA) ? 6 : 3;
1530  }
1531  else if (width == 8)
1532  {
1533    firstCtx = 9;
1534    numCtx = (eTType == TEXT_LUMA) ? 12 : 3;
1535  }
1536 
1537  if (eTType == TEXT_LUMA )
1538  {
1539    for( UInt bin = 0; bin < 2; bin++ )
1540    {
1541      pcEstBitsSbac->significantBits[ 0 ][ bin ] = m_cCUSigSCModel.get(  0, 0, 0 ).getEntropyBits( bin );
1542    }
1543
1544    for ( Int ctxIdx = firstCtx; ctxIdx < firstCtx + numCtx; ctxIdx++ )
1545    {
1546      for( UInt uiBin = 0; uiBin < 2; uiBin++ )
1547      {
1548        pcEstBitsSbac->significantBits[ ctxIdx ][ uiBin ] = m_cCUSigSCModel.get(  0, 0, ctxIdx ).getEntropyBits( uiBin );
1549      }
1550    }
1551  }
1552  else
1553  {
1554    for( UInt bin = 0; bin < 2; bin++ )
1555    {
1556      pcEstBitsSbac->significantBits[ 0 ][ bin ] = m_cCUSigSCModel.get(  0, 0, NUM_SIG_FLAG_CTX_LUMA + 0 ).getEntropyBits( bin );
1557    }
1558    for ( Int ctxIdx = firstCtx; ctxIdx < firstCtx + numCtx; ctxIdx++ )
1559    {
1560      for( UInt uiBin = 0; uiBin < 2; uiBin++ )
1561      {
1562        pcEstBitsSbac->significantBits[ ctxIdx ][ uiBin ] = m_cCUSigSCModel.get(  0, 0, NUM_SIG_FLAG_CTX_LUMA + ctxIdx ).getEntropyBits( uiBin );
1563      }
1564    }
1565  }
1566  Int iBitsX = 0, iBitsY = 0;
1567  Int blkSizeOffsetX, blkSizeOffsetY, shiftX, shiftY;
1568
1569  blkSizeOffsetX = eTType ? 0: (g_aucConvertToBit[ width ] *3 + ((g_aucConvertToBit[ width ] +1)>>2));
1570  blkSizeOffsetY = eTType ? 0: (g_aucConvertToBit[ height ]*3 + ((g_aucConvertToBit[ height ]+1)>>2));
1571  shiftX = eTType ? g_aucConvertToBit[ width  ] :((g_aucConvertToBit[ width  ]+3)>>2);
1572  shiftY = eTType ? g_aucConvertToBit[ height ] :((g_aucConvertToBit[ height ]+3)>>2);
1573
1574  Int ctx;
1575  ContextModel *pCtxX      = m_cCuCtxLastX.get( 0, eTType );
1576  for (ctx = 0; ctx < g_uiGroupIdx[ width - 1 ]; ctx++)
1577  {
1578    Int ctxOffset = blkSizeOffsetX + (ctx >>shiftX);
1579    pcEstBitsSbac->lastXBits[ ctx ] = iBitsX + pCtxX[ ctxOffset ].getEntropyBits( 0 );
1580    iBitsX += pCtxX[ ctxOffset ].getEntropyBits( 1 );
1581  }
1582  pcEstBitsSbac->lastXBits[ctx] = iBitsX;
1583  ContextModel *pCtxY      = m_cCuCtxLastY.get( 0, eTType );
1584  for (ctx = 0; ctx < g_uiGroupIdx[ height - 1 ]; ctx++)
1585  {
1586    Int ctxOffset = blkSizeOffsetY + (ctx >>shiftY);
1587    pcEstBitsSbac->lastYBits[ ctx ] = iBitsY + pCtxY[ ctxOffset ].getEntropyBits( 0 );
1588    iBitsY += pCtxY[ ctxOffset ].getEntropyBits( 1 );
1589  }
1590  pcEstBitsSbac->lastYBits[ctx] = iBitsY;
1591}
1592
1593/*!
1594 ****************************************************************************
1595 * \brief
1596 *    estimate bit cost of significant coefficient
1597 ****************************************************************************
1598 */
1599Void TEncSbac::estSignificantCoefficientsBit( estBitsSbacStruct* pcEstBitsSbac, TextType eTType )
1600{
1601  if (eTType==TEXT_LUMA)
1602  {
1603    ContextModel *ctxOne = m_cCUOneSCModel.get(0, 0);
1604    ContextModel *ctxAbs = m_cCUAbsSCModel.get(0, 0);
1605
1606    for (Int ctxIdx = 0; ctxIdx < NUM_ONE_FLAG_CTX_LUMA; ctxIdx++)
1607    {
1608      pcEstBitsSbac->m_greaterOneBits[ ctxIdx ][ 0 ] = ctxOne[ ctxIdx ].getEntropyBits( 0 );
1609      pcEstBitsSbac->m_greaterOneBits[ ctxIdx ][ 1 ] = ctxOne[ ctxIdx ].getEntropyBits( 1 );   
1610    }
1611
1612    for (Int ctxIdx = 0; ctxIdx < NUM_ABS_FLAG_CTX_LUMA; ctxIdx++)
1613    {
1614      pcEstBitsSbac->m_levelAbsBits[ ctxIdx ][ 0 ] = ctxAbs[ ctxIdx ].getEntropyBits( 0 );
1615      pcEstBitsSbac->m_levelAbsBits[ ctxIdx ][ 1 ] = ctxAbs[ ctxIdx ].getEntropyBits( 1 );   
1616    }
1617  }
1618  else
1619  {
1620    ContextModel *ctxOne = m_cCUOneSCModel.get(0, 0) + NUM_ONE_FLAG_CTX_LUMA;
1621    ContextModel *ctxAbs = m_cCUAbsSCModel.get(0, 0) + NUM_ABS_FLAG_CTX_LUMA;
1622
1623    for (Int ctxIdx = 0; ctxIdx < NUM_ONE_FLAG_CTX_CHROMA; ctxIdx++)
1624    {
1625      pcEstBitsSbac->m_greaterOneBits[ ctxIdx ][ 0 ] = ctxOne[ ctxIdx ].getEntropyBits( 0 );
1626      pcEstBitsSbac->m_greaterOneBits[ ctxIdx ][ 1 ] = ctxOne[ ctxIdx ].getEntropyBits( 1 );   
1627    }
1628
1629    for (Int ctxIdx = 0; ctxIdx < NUM_ABS_FLAG_CTX_CHROMA; ctxIdx++)
1630    {
1631      pcEstBitsSbac->m_levelAbsBits[ ctxIdx ][ 0 ] = ctxAbs[ ctxIdx ].getEntropyBits( 0 );
1632      pcEstBitsSbac->m_levelAbsBits[ ctxIdx ][ 1 ] = ctxAbs[ ctxIdx ].getEntropyBits( 1 );   
1633    }
1634  }
1635}
1636
1637/**
1638 - Initialize our context information from the nominated source.
1639 .
1640 \param pSrc From where to copy context information.
1641 */
1642Void TEncSbac::xCopyContextsFrom( TEncSbac* pSrc )
1643{ 
1644  memcpy(m_contextModels, pSrc->m_contextModels, m_numContextModels*sizeof(m_contextModels[0]));
1645}
1646
1647Void  TEncSbac::loadContexts ( TEncSbac* pScr)
1648{
1649  this->xCopyContextsFrom(pScr);
1650}
1651//! \}
Note: See TracBrowser for help on using the repository browser.