source: 3DVCSoftware/trunk/source/Lib/TLibEncoder/TEncSbac.cpp @ 976

Last change on this file since 976 was 976, checked in by tech, 10 years ago
  • Merged 11.1-dev0@975. (Clean ups)
  • Added coding results.
  • Changed version number.
  • Property svn:eol-style set to native
File size: 76.2 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-2014, 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_cCUPUARPWSCModel          ( 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_cSaoMergeSCModel          ( 1,             1,               NUM_SAO_MERGE_FLAG_CTX   , m_contextModels + m_numContextModels, m_numContextModels)
87, m_cSaoTypeIdxSCModel        ( 1,             1,               NUM_SAO_TYPE_IDX_CTX          , m_contextModels + m_numContextModels, m_numContextModels)
88, m_cTransformSkipSCModel     ( 1,             2,               NUM_TRANSFORMSKIP_FLAG_CTX    , m_contextModels + m_numContextModels, m_numContextModels)
89, m_CUTransquantBypassFlagSCModel( 1,          1,               NUM_CU_TRANSQUANT_BYPASS_FLAG_CTX, m_contextModels + m_numContextModels, m_numContextModels)
90#if H_3D_DIM
91, m_cDepthIntraModeSCModel    ( 1,             1,               NUM_DEPTH_INTRA_MODE_CTX      , m_contextModels + m_numContextModels, m_numContextModels)
92, m_cDdcFlagSCModel           ( 1,             1,               NUM_DDC_FLAG_CTX              , m_contextModels + m_numContextModels, m_numContextModels)
93, m_cDdcDataSCModel           ( 1,             1,               NUM_DDC_DATA_CTX              , m_contextModels + m_numContextModels, m_numContextModels)
94, m_cAngleFlagSCModel         ( 1,             1,               NUM_ANGLE_FLAG_CTX            , m_contextModels + m_numContextModels, m_numContextModels)
95#if H_3D_DIM_SDC
96, m_cSDCResidualFlagSCModel   ( 1,             1,               SDC_NUM_RESIDUAL_FLAG_CTX     , m_contextModels + m_numContextModels, m_numContextModels)
97, m_cSDCResidualSCModel       ( 1,             1,               SDC_NUM_RESIDUAL_CTX          , m_contextModels + m_numContextModels, m_numContextModels)
98#endif
99#endif
100#if H_3D_DIM_SDC
101, m_cSDCFlagSCModel                  ( 1,             1,  NUM_SDC_FLAG_CTX                 , m_contextModels + m_numContextModels, m_numContextModels)
102#endif
103#if H_3D_DBBP
104, m_cDBBPFlagSCModel             ( 1,             1,                 DBBP_NUM_FLAG_CTX           , m_contextModels + m_numContextModels, m_numContextModels)
105#endif
106{
107  assert( m_numContextModels <= MAX_NUM_CTX_MOD );
108}
109
110TEncSbac::~TEncSbac()
111{
112}
113
114// ====================================================================================================================
115// Public member functions
116// ====================================================================================================================
117
118Void TEncSbac::resetEntropy           ()
119{
120  Int  iQp              = m_pcSlice->getSliceQp();
121  SliceType eSliceType  = m_pcSlice->getSliceType();
122 
123  Int  encCABACTableIdx = m_pcSlice->getPPS()->getEncCABACTableIdx();
124  if (!m_pcSlice->isIntra() && (encCABACTableIdx==B_SLICE || encCABACTableIdx==P_SLICE) && m_pcSlice->getPPS()->getCabacInitPresentFlag())
125  {
126    eSliceType = (SliceType) encCABACTableIdx;
127  }
128
129  m_cCUSplitFlagSCModel.initBuffer       ( eSliceType, iQp, (UChar*)INIT_SPLIT_FLAG );
130 
131  m_cCUSkipFlagSCModel.initBuffer        ( eSliceType, iQp, (UChar*)INIT_SKIP_FLAG );
132  m_cCUMergeFlagExtSCModel.initBuffer    ( eSliceType, iQp, (UChar*)INIT_MERGE_FLAG_EXT);
133  m_cCUMergeIdxExtSCModel.initBuffer     ( eSliceType, iQp, (UChar*)INIT_MERGE_IDX_EXT);
134#if H_3D_ARP
135  m_cCUPUARPWSCModel.initBuffer          ( eSliceType, iQp, (UChar*)INIT_ARPW );
136#endif
137#if H_3D_IC
138  m_cCUICFlagSCModel.initBuffer          ( eSliceType, iQp, (UChar*)INIT_IC_FLAG );
139#endif
140  m_cCUPartSizeSCModel.initBuffer        ( eSliceType, iQp, (UChar*)INIT_PART_SIZE );
141  m_cCUPredModeSCModel.initBuffer        ( eSliceType, iQp, (UChar*)INIT_PRED_MODE );
142  m_cCUIntraPredSCModel.initBuffer       ( eSliceType, iQp, (UChar*)INIT_INTRA_PRED_MODE );
143  m_cCUChromaPredSCModel.initBuffer      ( eSliceType, iQp, (UChar*)INIT_CHROMA_PRED_MODE );
144  m_cCUInterDirSCModel.initBuffer        ( eSliceType, iQp, (UChar*)INIT_INTER_DIR );
145  m_cCUMvdSCModel.initBuffer             ( eSliceType, iQp, (UChar*)INIT_MVD );
146  m_cCURefPicSCModel.initBuffer          ( eSliceType, iQp, (UChar*)INIT_REF_PIC );
147  m_cCUDeltaQpSCModel.initBuffer         ( eSliceType, iQp, (UChar*)INIT_DQP );
148  m_cCUQtCbfSCModel.initBuffer           ( eSliceType, iQp, (UChar*)INIT_QT_CBF );
149  m_cCUQtRootCbfSCModel.initBuffer       ( eSliceType, iQp, (UChar*)INIT_QT_ROOT_CBF );
150  m_cCUSigCoeffGroupSCModel.initBuffer   ( eSliceType, iQp, (UChar*)INIT_SIG_CG_FLAG );
151  m_cCUSigSCModel.initBuffer             ( eSliceType, iQp, (UChar*)INIT_SIG_FLAG );
152  m_cCuCtxLastX.initBuffer               ( eSliceType, iQp, (UChar*)INIT_LAST );
153  m_cCuCtxLastY.initBuffer               ( eSliceType, iQp, (UChar*)INIT_LAST );
154  m_cCUOneSCModel.initBuffer             ( eSliceType, iQp, (UChar*)INIT_ONE_FLAG );
155  m_cCUAbsSCModel.initBuffer             ( eSliceType, iQp, (UChar*)INIT_ABS_FLAG );
156  m_cMVPIdxSCModel.initBuffer            ( eSliceType, iQp, (UChar*)INIT_MVP_IDX );
157  m_cCUTransSubdivFlagSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_TRANS_SUBDIV_FLAG );
158  m_cSaoMergeSCModel.initBuffer      ( eSliceType, iQp, (UChar*)INIT_SAO_MERGE_FLAG );
159  m_cSaoTypeIdxSCModel.initBuffer        ( eSliceType, iQp, (UChar*)INIT_SAO_TYPE_IDX );
160  m_cTransformSkipSCModel.initBuffer     ( eSliceType, iQp, (UChar*)INIT_TRANSFORMSKIP_FLAG );
161  m_CUTransquantBypassFlagSCModel.initBuffer( eSliceType, iQp, (UChar*)INIT_CU_TRANSQUANT_BYPASS_FLAG );
162
163#if H_3D_DIM
164  m_cDepthIntraModeSCModel.initBuffer    ( eSliceType, iQp, (UChar*)INIT_DEPTH_INTRA_MODE );
165  m_cDdcFlagSCModel.initBuffer           ( eSliceType, iQp, (UChar*)INIT_DDC_FLAG );
166  m_cDdcDataSCModel.initBuffer           ( eSliceType, iQp, (UChar*)INIT_DDC_DATA );
167  m_cAngleFlagSCModel.initBuffer         ( eSliceType, iQp, (UChar*)INIT_ANGLE_FLAG );
168#if H_3D_DIM_SDC
169  m_cSDCResidualFlagSCModel.initBuffer   ( eSliceType, iQp, (UChar*)INIT_SDC_RESIDUAL_FLAG );
170  m_cSDCResidualSCModel.initBuffer       ( eSliceType, iQp, (UChar*)INIT_SDC_RESIDUAL );
171#endif
172#endif
173#if H_3D_DIM_SDC
174  m_cSDCFlagSCModel.initBuffer              ( eSliceType, iQp, (UChar*)INIT_SDC_FLAG );
175#endif
176#if H_3D_DBBP
177  m_cDBBPFlagSCModel.initBuffer              ( eSliceType, iQp, (UChar*)INIT_DBBP_FLAG );
178#endif
179  // new structure
180  m_uiLastQp = iQp;
181 
182  m_pcBinIf->start();
183 
184  return;
185}
186
187/** The function does the following:
188 * If current slice type is P/B then it determines the distance of initialisation type 1 and 2 from the current CABAC states and
189 * stores the index of the closest table.  This index is used for the next P/B slice when cabac_init_present_flag is true.
190 */
191Void TEncSbac::determineCabacInitIdx()
192{
193  Int  qp              = m_pcSlice->getSliceQp();
194
195  if (!m_pcSlice->isIntra())
196  {
197    SliceType aSliceTypeChoices[] = {B_SLICE, P_SLICE};
198
199    UInt bestCost             = MAX_UINT;
200    SliceType bestSliceType   = aSliceTypeChoices[0];
201    for (UInt idx=0; idx<2; idx++)
202    {
203      UInt curCost          = 0;
204      SliceType curSliceType  = aSliceTypeChoices[idx];
205
206      curCost  = m_cCUSplitFlagSCModel.calcCost       ( curSliceType, qp, (UChar*)INIT_SPLIT_FLAG );
207      curCost += m_cCUSkipFlagSCModel.calcCost        ( curSliceType, qp, (UChar*)INIT_SKIP_FLAG );
208      curCost += m_cCUMergeFlagExtSCModel.calcCost    ( curSliceType, qp, (UChar*)INIT_MERGE_FLAG_EXT);
209      curCost += m_cCUMergeIdxExtSCModel.calcCost     ( curSliceType, qp, (UChar*)INIT_MERGE_IDX_EXT);
210#if H_3D_ARP
211      curCost += m_cCUPUARPWSCModel.calcCost          ( curSliceType, qp, (UChar*)INIT_ARPW );
212#endif
213#if H_3D_IC
214      curCost += m_cCUICFlagSCModel.calcCost          ( curSliceType, qp, (UChar*)INIT_IC_FLAG );
215#endif
216#if H_3D_DIM_SDC
217      curCost += m_cSDCFlagSCModel.calcCost           ( curSliceType, qp, (UChar*)INIT_SDC_FLAG );
218#endif
219#if H_3D_DBBP
220      curCost += m_cDBBPFlagSCModel.calcCost          ( curSliceType, qp, (UChar*)INIT_DBBP_FLAG );
221#endif
222      curCost += m_cCUPartSizeSCModel.calcCost        ( curSliceType, qp, (UChar*)INIT_PART_SIZE );
223      curCost += m_cCUPredModeSCModel.calcCost        ( curSliceType, qp, (UChar*)INIT_PRED_MODE );
224      curCost += m_cCUIntraPredSCModel.calcCost       ( curSliceType, qp, (UChar*)INIT_INTRA_PRED_MODE );
225      curCost += m_cCUChromaPredSCModel.calcCost      ( curSliceType, qp, (UChar*)INIT_CHROMA_PRED_MODE );
226      curCost += m_cCUInterDirSCModel.calcCost        ( curSliceType, qp, (UChar*)INIT_INTER_DIR );
227      curCost += m_cCUMvdSCModel.calcCost             ( curSliceType, qp, (UChar*)INIT_MVD );
228      curCost += m_cCURefPicSCModel.calcCost          ( curSliceType, qp, (UChar*)INIT_REF_PIC );
229      curCost += m_cCUDeltaQpSCModel.calcCost         ( curSliceType, qp, (UChar*)INIT_DQP );
230      curCost += m_cCUQtCbfSCModel.calcCost           ( curSliceType, qp, (UChar*)INIT_QT_CBF );
231      curCost += m_cCUQtRootCbfSCModel.calcCost       ( curSliceType, qp, (UChar*)INIT_QT_ROOT_CBF );
232      curCost += m_cCUSigCoeffGroupSCModel.calcCost   ( curSliceType, qp, (UChar*)INIT_SIG_CG_FLAG );
233      curCost += m_cCUSigSCModel.calcCost             ( curSliceType, qp, (UChar*)INIT_SIG_FLAG );
234      curCost += m_cCuCtxLastX.calcCost               ( curSliceType, qp, (UChar*)INIT_LAST );
235      curCost += m_cCuCtxLastY.calcCost               ( curSliceType, qp, (UChar*)INIT_LAST );
236      curCost += m_cCUOneSCModel.calcCost             ( curSliceType, qp, (UChar*)INIT_ONE_FLAG );
237      curCost += m_cCUAbsSCModel.calcCost             ( curSliceType, qp, (UChar*)INIT_ABS_FLAG );
238      curCost += m_cMVPIdxSCModel.calcCost            ( curSliceType, qp, (UChar*)INIT_MVP_IDX );
239      curCost += m_cCUTransSubdivFlagSCModel.calcCost ( curSliceType, qp, (UChar*)INIT_TRANS_SUBDIV_FLAG );
240      curCost += m_cSaoMergeSCModel.calcCost      ( curSliceType, qp, (UChar*)INIT_SAO_MERGE_FLAG );
241      curCost += m_cSaoTypeIdxSCModel.calcCost        ( curSliceType, qp, (UChar*)INIT_SAO_TYPE_IDX );
242      curCost += m_cTransformSkipSCModel.calcCost     ( curSliceType, qp, (UChar*)INIT_TRANSFORMSKIP_FLAG );
243      curCost += m_CUTransquantBypassFlagSCModel.calcCost( curSliceType, qp, (UChar*)INIT_CU_TRANSQUANT_BYPASS_FLAG );
244#if H_3D_DIM
245    if( m_pcSlice->getVpsDepthModesFlag() )
246    {
247      curCost += m_cDepthIntraModeSCModel.calcCost    ( curSliceType, qp, (UChar*)INIT_DEPTH_INTRA_MODE );
248      curCost += m_cDdcFlagSCModel.calcCost           ( curSliceType, qp, (UChar*)INIT_DDC_FLAG );
249      curCost += m_cDdcDataSCModel.calcCost           ( curSliceType, qp, (UChar*)INIT_DDC_DATA );
250      curCost += m_cAngleFlagSCModel.calcCost         ( curSliceType, qp, (UChar*)INIT_ANGLE_FLAG ); 
251    }
252#endif
253      if (curCost < bestCost)
254      {
255        bestSliceType = curSliceType;
256        bestCost      = curCost;
257      }
258    }
259    m_pcSlice->getPPS()->setEncCABACTableIdx( bestSliceType );
260  }
261  else
262  {
263    m_pcSlice->getPPS()->setEncCABACTableIdx( I_SLICE );
264  } 
265}
266
267
268/** The function does the followng: Write out terminate bit. Flush CABAC. Intialize CABAC states. Start CABAC.
269 */
270Void TEncSbac::updateContextTables( SliceType eSliceType, Int iQp, Bool bExecuteFinish )
271{
272  m_pcBinIf->encodeBinTrm(1);
273  if (bExecuteFinish) m_pcBinIf->finish();
274  m_cCUSplitFlagSCModel.initBuffer       ( eSliceType, iQp, (UChar*)INIT_SPLIT_FLAG );
275 
276  m_cCUSkipFlagSCModel.initBuffer        ( eSliceType, iQp, (UChar*)INIT_SKIP_FLAG );
277  m_cCUMergeFlagExtSCModel.initBuffer    ( eSliceType, iQp, (UChar*)INIT_MERGE_FLAG_EXT);
278  m_cCUMergeIdxExtSCModel.initBuffer     ( eSliceType, iQp, (UChar*)INIT_MERGE_IDX_EXT);
279#if H_3D_ARP
280  m_cCUPUARPWSCModel.initBuffer          ( eSliceType, iQp, (UChar*)INIT_ARPW );
281#endif
282#if H_3D_IC
283  m_cCUICFlagSCModel.initBuffer          ( eSliceType, iQp, (UChar*)INIT_IC_FLAG );
284#endif
285  m_cCUPartSizeSCModel.initBuffer        ( eSliceType, iQp, (UChar*)INIT_PART_SIZE );
286  m_cCUPredModeSCModel.initBuffer        ( eSliceType, iQp, (UChar*)INIT_PRED_MODE );
287  m_cCUIntraPredSCModel.initBuffer       ( eSliceType, iQp, (UChar*)INIT_INTRA_PRED_MODE );
288  m_cCUChromaPredSCModel.initBuffer      ( eSliceType, iQp, (UChar*)INIT_CHROMA_PRED_MODE );
289  m_cCUInterDirSCModel.initBuffer        ( eSliceType, iQp, (UChar*)INIT_INTER_DIR );
290  m_cCUMvdSCModel.initBuffer             ( eSliceType, iQp, (UChar*)INIT_MVD );
291  m_cCURefPicSCModel.initBuffer          ( eSliceType, iQp, (UChar*)INIT_REF_PIC );
292  m_cCUDeltaQpSCModel.initBuffer         ( eSliceType, iQp, (UChar*)INIT_DQP );
293  m_cCUQtCbfSCModel.initBuffer           ( eSliceType, iQp, (UChar*)INIT_QT_CBF );
294  m_cCUQtRootCbfSCModel.initBuffer       ( eSliceType, iQp, (UChar*)INIT_QT_ROOT_CBF );
295  m_cCUSigCoeffGroupSCModel.initBuffer   ( eSliceType, iQp, (UChar*)INIT_SIG_CG_FLAG );
296  m_cCUSigSCModel.initBuffer             ( eSliceType, iQp, (UChar*)INIT_SIG_FLAG );
297  m_cCuCtxLastX.initBuffer               ( eSliceType, iQp, (UChar*)INIT_LAST );
298  m_cCuCtxLastY.initBuffer               ( eSliceType, iQp, (UChar*)INIT_LAST );
299  m_cCUOneSCModel.initBuffer             ( eSliceType, iQp, (UChar*)INIT_ONE_FLAG );
300  m_cCUAbsSCModel.initBuffer             ( eSliceType, iQp, (UChar*)INIT_ABS_FLAG );
301  m_cMVPIdxSCModel.initBuffer            ( eSliceType, iQp, (UChar*)INIT_MVP_IDX );
302  m_cCUTransSubdivFlagSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_TRANS_SUBDIV_FLAG );
303  m_cSaoMergeSCModel.initBuffer      ( eSliceType, iQp, (UChar*)INIT_SAO_MERGE_FLAG );
304  m_cSaoTypeIdxSCModel.initBuffer        ( eSliceType, iQp, (UChar*)INIT_SAO_TYPE_IDX );
305  m_cTransformSkipSCModel.initBuffer     ( eSliceType, iQp, (UChar*)INIT_TRANSFORMSKIP_FLAG );
306  m_CUTransquantBypassFlagSCModel.initBuffer( eSliceType, iQp, (UChar*)INIT_CU_TRANSQUANT_BYPASS_FLAG );
307#if H_3D_DIM
308  m_cDepthIntraModeSCModel.initBuffer    ( eSliceType, iQp, (UChar*)INIT_DEPTH_INTRA_MODE );
309  m_cDdcFlagSCModel.initBuffer           ( eSliceType, iQp, (UChar*)INIT_DDC_FLAG );
310  m_cDdcDataSCModel.initBuffer           ( eSliceType, iQp, (UChar*)INIT_DDC_DATA );
311  m_cAngleFlagSCModel.initBuffer         ( eSliceType, iQp, (UChar*)INIT_ANGLE_FLAG );
312#if H_3D_DIM_SDC
313  m_cSDCResidualFlagSCModel.initBuffer   ( eSliceType, iQp, (UChar*)INIT_SDC_RESIDUAL_FLAG );
314  m_cSDCResidualSCModel.initBuffer       ( eSliceType, iQp, (UChar*)INIT_SDC_RESIDUAL );
315#endif
316#endif
317#if H_3D_DIM_SDC
318  m_cSDCFlagSCModel.initBuffer              ( eSliceType, iQp, (UChar*)INIT_SDC_FLAG );
319#endif
320#if H_3D_DBBP
321  m_cDBBPFlagSCModel.initBuffer              ( eSliceType, iQp, (UChar*)INIT_DBBP_FLAG );
322#endif
323  m_pcBinIf->start();
324}
325
326Void TEncSbac::codeVPS( TComVPS* pcVPS )
327{
328  assert (0);
329  return;
330}
331
332#if H_3D
333Void TEncSbac::codeSPS( TComSPS* pcSPS, Int viewIndex, Bool depthFlag )
334#else
335Void TEncSbac::codeSPS( TComSPS* pcSPS )
336#endif
337{
338  assert (0);
339  return;
340}
341
342Void TEncSbac::codePPS( TComPPS* pcPPS )
343{
344  assert (0);
345  return;
346}
347
348Void TEncSbac::codeSliceHeader( TComSlice* pcSlice )
349{
350  assert (0);
351  return;
352}
353
354Void TEncSbac::codeTilesWPPEntryPoint( TComSlice* pSlice )
355{
356  assert (0);
357  return;
358}
359
360Void TEncSbac::codeTerminatingBit( UInt uilsLast )
361{
362  m_pcBinIf->encodeBinTrm( uilsLast );
363}
364
365Void TEncSbac::codeSliceFinish()
366{
367  m_pcBinIf->finish();
368}
369
370Void TEncSbac::xWriteUnarySymbol( UInt uiSymbol, ContextModel* pcSCModel, Int iOffset )
371{
372  m_pcBinIf->encodeBin( uiSymbol ? 1 : 0, pcSCModel[0] );
373 
374  if( 0 == uiSymbol)
375  {
376    return;
377  }
378 
379  while( uiSymbol-- )
380  {
381    m_pcBinIf->encodeBin( uiSymbol ? 1 : 0, pcSCModel[ iOffset ] );
382  }
383 
384  return;
385}
386
387Void TEncSbac::xWriteUnaryMaxSymbol( UInt uiSymbol, ContextModel* pcSCModel, Int iOffset, UInt uiMaxSymbol )
388{
389  if (uiMaxSymbol == 0)
390  {
391    return;
392  }
393 
394  m_pcBinIf->encodeBin( uiSymbol ? 1 : 0, pcSCModel[ 0 ] );
395 
396  if ( uiSymbol == 0 )
397  {
398    return;
399  }
400 
401  Bool bCodeLast = ( uiMaxSymbol > uiSymbol );
402 
403  while( --uiSymbol )
404  {
405    m_pcBinIf->encodeBin( 1, pcSCModel[ iOffset ] );
406  }
407  if( bCodeLast )
408  {
409    m_pcBinIf->encodeBin( 0, pcSCModel[ iOffset ] );
410  }
411 
412  return;
413}
414
415Void TEncSbac::xWriteEpExGolomb( UInt uiSymbol, UInt uiCount )
416{
417  UInt bins = 0;
418  Int numBins = 0;
419 
420  while( uiSymbol >= (UInt)(1<<uiCount) )
421  {
422    bins = 2 * bins + 1;
423    numBins++;
424    uiSymbol -= 1 << uiCount;
425    uiCount  ++;
426  }
427  bins = 2 * bins + 0;
428  numBins++;
429 
430  bins = (bins << uiCount) | uiSymbol;
431  numBins += uiCount;
432 
433  assert( numBins <= 32 );
434  m_pcBinIf->encodeBinsEP( bins, numBins );
435}
436
437/** Coding of coeff_abs_level_minus3
438 * \param uiSymbol value of coeff_abs_level_minus3
439 * \param ruiGoRiceParam reference to Rice parameter
440 * \returns Void
441 */
442Void TEncSbac::xWriteCoefRemainExGolomb ( UInt symbol, UInt &rParam )
443{
444  Int codeNumber  = (Int)symbol;
445  UInt length;
446  if (codeNumber < (COEF_REMAIN_BIN_REDUCTION << rParam))
447  {
448    length = codeNumber>>rParam;
449    m_pcBinIf->encodeBinsEP( (1<<(length+1))-2 , length+1);
450    m_pcBinIf->encodeBinsEP((codeNumber%(1<<rParam)),rParam);
451  }
452  else
453  {
454    length = rParam;
455    codeNumber  = codeNumber - ( COEF_REMAIN_BIN_REDUCTION << rParam);
456    while (codeNumber >= (1<<length))
457    {
458      codeNumber -=  (1<<(length++));   
459    }
460    m_pcBinIf->encodeBinsEP((1<<(COEF_REMAIN_BIN_REDUCTION+length+1-rParam))-2,COEF_REMAIN_BIN_REDUCTION+length+1-rParam);
461    m_pcBinIf->encodeBinsEP(codeNumber,length);
462  }
463}
464
465#if H_3D_DIM
466Void TEncSbac::xWriteExGolombLevel( UInt uiSymbol, ContextModel& rcSCModel  )
467{
468  if( uiSymbol )
469  {
470    m_pcBinIf->encodeBin( 1, rcSCModel );
471    UInt uiCount = 0;
472    Bool bNoExGo = ( uiSymbol < 3 );
473
474    while( --uiSymbol && ++uiCount < 3 )
475    {
476      m_pcBinIf->encodeBin( 1, rcSCModel );
477    }
478    if( bNoExGo )
479    {
480      m_pcBinIf->encodeBin( 0, rcSCModel );
481    }
482    else
483    {
484      xWriteEpExGolomb( uiSymbol, 0 );
485    }
486  }
487  else
488  {
489    m_pcBinIf->encodeBin( 0, rcSCModel );
490  }
491
492  return;
493}
494
495Void TEncSbac::xCodeDimDeltaDC( Pel valDeltaDC, UInt uiNumSeg )
496{
497  xWriteExGolombLevel( UInt( abs( valDeltaDC ) - ( uiNumSeg > 1 ? 0 : 1 ) ), m_cDdcDataSCModel.get(0, 0, 0) );
498  if( valDeltaDC != 0 )
499  {
500    UInt uiSign = valDeltaDC > 0 ? 0 : 1;
501    m_pcBinIf->encodeBinEP( uiSign );
502  }
503}
504
505#if H_3D_DIM_DMM
506Void TEncSbac::xCodeDmm1WedgeIdx( UInt uiTabIdx, Int iNumBit )
507{
508  for ( Int i = 0; i < iNumBit; i++ )
509  {
510    m_pcBinIf->encodeBinEP( ( uiTabIdx >> i ) & 1 );
511  }
512}
513
514#endif
515
516#if H_3D_DIM_SDC
517Void TEncSbac::xCodeSDCResidualData ( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiSegment )
518{
519  assert( pcCU->getSlice()->getIsDepth() );
520  assert( pcCU->getPartitionSize(uiAbsPartIdx) == SIZE_2Nx2N );
521  assert( pcCU->getSDCFlag(uiAbsPartIdx) );
522  assert( uiSegment < 2 );
523 
524  Pel segmentDCOffset = pcCU->getSDCSegmentDCOffset(uiSegment, uiAbsPartIdx);
525 
526  UInt uiResidual = segmentDCOffset == 0 ? 0 : 1;
527  UInt uiSign     = segmentDCOffset < 0 ? 1 : 0;
528  UInt uiAbsIdx   = abs(segmentDCOffset);
529
530#if H_3D_DIM_DLT
531  UInt uiMaxResidualBits = pcCU->getSlice()->getPPS()->getDLT()->getBitsPerDepthValue( pcCU->getSlice()->getLayerIdInVps() );
532#else
533  UInt uiMaxResidualBits = g_bitDepthY;
534#endif
535  assert( uiMaxResidualBits <= g_bitDepthY );
536 
537  // residual flag
538  m_pcBinIf->encodeBin( uiResidual, m_cSDCResidualFlagSCModel.get( 0, 0, 0 ) );
539#if H_MV_ENC_DEC_TRAC
540  DTRACE_CU("sdc_residual_flag[i]", uiResidual)
541#endif 
542 
543  if (uiResidual)
544  {
545    // encode sign bit of residual
546    m_pcBinIf->encodeBinEP( uiSign );
547#if H_MV_ENC_DEC_TRAC
548    DTRACE_CU("sdc_residual_sign_flag[i]", uiSign)
549#endif
550#if H_3D_DIM_DLT   
551    UInt uiNumDepthValues = pcCU->getSlice()->getPPS()->getDLT()->getNumDepthValues( pcCU->getSlice()->getLayerIdInVps() );
552#else
553    UInt uiNumDepthValues = ((1 << g_bitDepthY)-1);
554#endif
555
556    assert(uiAbsIdx <= uiNumDepthValues);
557   
558    // encode residual magnitude
559    uiAbsIdx -= 1;
560
561#if H_MV_ENC_DEC_TRAC
562    DTRACE_CU("sdc_residual_abs_minus1[i]", uiAbsIdx)
563#endif
564
565    // prefix part
566    if ( uiAbsIdx == 0 )
567    {
568      m_pcBinIf->encodeBin( 0, m_cSDCResidualSCModel.get(0, 0, 0) );
569    }
570    else
571    {
572      UInt l = uiAbsIdx;
573      UInt k = 0;
574      UInt uiPrefixThreshold = ((uiNumDepthValues * 3) >> 2);
575      while ( l > 0 && k < uiPrefixThreshold )
576      {
577        m_pcBinIf->encodeBin( 1, m_cSDCResidualSCModel.get(0, 0, 0) );
578        l--;
579        k++;
580      }
581      if ( uiAbsIdx < uiPrefixThreshold )
582      {
583        m_pcBinIf->encodeBin( 0, m_cSDCResidualSCModel.get(0, 0, 0) );
584      }     
585      else
586      { // suffix part
587        uiAbsIdx -= uiPrefixThreshold;
588        UInt uiSuffixLength = numBitsForValue(uiNumDepthValues - uiPrefixThreshold);
589        UInt uiBitInfo = 0;
590        for ( Int i = 0; i < uiSuffixLength; i++)
591        {
592          uiBitInfo = ( uiAbsIdx & ( 1 << i ) ) >> i;
593          m_pcBinIf->encodeBinEP( uiBitInfo);
594        }
595      }
596    }
597   
598  }
599}
600#endif
601#endif
602
603// SBAC RD
604Void  TEncSbac::load ( TEncSbac* pSrc)
605{
606  this->xCopyFrom(pSrc);
607}
608
609Void  TEncSbac::loadIntraDirModeLuma( TEncSbac* pSrc)
610{
611  m_pcBinIf->copyState( pSrc->m_pcBinIf );
612 
613  this->m_cCUIntraPredSCModel      .copyFrom( &pSrc->m_cCUIntraPredSCModel       );
614}
615#if H_3D_DIM
616Void TEncSbac::loadIntraDepthMode( TEncSbac* pSrc)
617{
618  m_pcBinIf->copyState( pSrc->m_pcBinIf );
619
620  this->m_cDepthIntraModeSCModel .copyFrom( &pSrc->m_cDepthIntraModeSCModel );
621  this->m_cDdcFlagSCModel        .copyFrom( &pSrc->m_cDdcFlagSCModel );
622  this->m_cAngleFlagSCModel      .copyFrom( &pSrc->m_cAngleFlagSCModel );
623}
624#endif
625
626Void  TEncSbac::store( TEncSbac* pDest)
627{
628  pDest->xCopyFrom( this );
629}
630
631
632Void TEncSbac::xCopyFrom( TEncSbac* pSrc )
633{
634  m_pcBinIf->copyState( pSrc->m_pcBinIf );
635 
636  this->m_uiCoeffCost = pSrc->m_uiCoeffCost;
637  this->m_uiLastQp    = pSrc->m_uiLastQp;
638 
639  memcpy( m_contextModels, pSrc->m_contextModels, m_numContextModels * sizeof( ContextModel ) );
640}
641
642Void TEncSbac::codeMVPIdx ( TComDataCU* pcCU, UInt uiAbsPartIdx, RefPicList eRefList )
643{
644  Int iSymbol = pcCU->getMVPIdx(eRefList, uiAbsPartIdx);
645  Int iNum = AMVP_MAX_NUM_CANDS;
646
647  xWriteUnaryMaxSymbol(iSymbol, m_cMVPIdxSCModel.get(0), 1, iNum-1);
648#if H_MV_ENC_DEC_TRAC
649#if ENC_DEC_TRACE
650  if ( eRefList == REF_PIC_LIST_0 )
651  {
652    DTRACE_PU("mvp_l0_flag", iSymbol)
653  }
654  else
655  {
656    DTRACE_PU("mvp_l1_flag", iSymbol)
657  }
658#endif
659#endif
660}
661
662Void TEncSbac::codePartSize( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
663{
664  PartSize eSize         = pcCU->getPartitionSize( uiAbsPartIdx );
665#if H_3D_QTLPC
666  TComSPS *sps           = pcCU->getPic()->getSlice(0)->getSPS();
667  TComPic *pcTexture     = pcCU->getSlice()->getTexturePic();
668  Bool bDepthMapDetect   = (pcTexture != NULL);
669  Bool bIntraSliceDetect = (pcCU->getSlice()->getSliceType() == I_SLICE);
670 
671  Bool rapPic     = (pcCU->getSlice()->getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_W_RADL || pcCU->getSlice()->getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_N_LP || pcCU->getSlice()->getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA);
672
673  Bool depthDependent = false;
674  UInt uiTexturePart = eSize;
675  if(bDepthMapDetect && !bIntraSliceDetect && !rapPic && pcCU->getPic()->getReduceBitsFlag() && sps->getUseQTL() && sps->getUsePC() )
676  {
677    TComDataCU *pcTextureCU = pcTexture->getCU(pcCU->getAddr());
678    UInt uiCUIdx            = (pcCU->getZorderIdxInCU() == 0) ? uiAbsPartIdx : pcCU->getZorderIdxInCU();
679    assert(pcTextureCU->getDepth(uiCUIdx) >= uiDepth);
680    if(pcTextureCU->getDepth(uiCUIdx) == uiDepth )
681    {
682      depthDependent = true;
683      uiTexturePart = pcTextureCU->getPartitionSize( uiCUIdx );
684    }
685    if (pcTextureCU->getDepth(uiCUIdx) == uiDepth && pcTextureCU->getPartitionSize( uiCUIdx ) == SIZE_2Nx2N)
686    {
687      assert( eSize == SIZE_2Nx2N );
688      return;
689    }
690  }
691#endif
692  if ( pcCU->isIntra( uiAbsPartIdx ) )
693  {
694    if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth )
695    {
696      m_pcBinIf->encodeBin( eSize == SIZE_2Nx2N? 1 : 0, m_cCUPartSizeSCModel.get( 0, 0, 0 ) );
697#if H_MV_ENC_DEC_TRAC
698      DTRACE_CU("part_mode", eSize == SIZE_2Nx2N? 1 : 0)
699#endif       
700    }
701    return;
702  }
703 
704#if H_MV_ENC_DEC_TRAC         
705  DTRACE_CU("part_mode", eSize )
706#endif       
707#if H_3D_QTLPC
708    if (depthDependent==false || uiTexturePart == SIZE_NxN|| uiTexturePart == SIZE_2Nx2N)
709    {
710#endif
711  switch(eSize)
712  {
713    case SIZE_2Nx2N:
714    {
715      m_pcBinIf->encodeBin( 1, m_cCUPartSizeSCModel.get( 0, 0, 0) );
716      break;
717    }
718    case SIZE_2NxN:
719    case SIZE_2NxnU:
720    case SIZE_2NxnD:
721    {
722      m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 0) );
723      m_pcBinIf->encodeBin( 1, m_cCUPartSizeSCModel.get( 0, 0, 1) );
724      if ( pcCU->getSlice()->getSPS()->getAMPAcc( uiDepth ) )
725      {
726        if (eSize == SIZE_2NxN)
727        {
728          m_pcBinIf->encodeBin(1, m_cCUPartSizeSCModel.get( 0, 0, 3 ));
729        }
730        else
731        {
732          m_pcBinIf->encodeBin(0, m_cCUPartSizeSCModel.get( 0, 0, 3 ));
733          m_pcBinIf->encodeBinEP((eSize == SIZE_2NxnU? 0: 1));
734        }
735      }
736      break;
737    }
738    case SIZE_Nx2N:
739    case SIZE_nLx2N:
740    case SIZE_nRx2N:
741    {
742      m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 0) );
743      m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 1) );
744      if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth && !( pcCU->getWidth(uiAbsPartIdx) == 8 && pcCU->getHeight(uiAbsPartIdx) == 8 ) )
745      {
746        m_pcBinIf->encodeBin( 1, m_cCUPartSizeSCModel.get( 0, 0, 2) );
747      }
748      if ( pcCU->getSlice()->getSPS()->getAMPAcc( uiDepth ) )
749      {
750        if (eSize == SIZE_Nx2N)
751        {
752          m_pcBinIf->encodeBin(1, m_cCUPartSizeSCModel.get( 0, 0, 3 ));
753        }
754        else
755        {
756          m_pcBinIf->encodeBin(0, m_cCUPartSizeSCModel.get( 0, 0, 3 ));
757          m_pcBinIf->encodeBinEP((eSize == SIZE_nLx2N? 0: 1));
758        }
759      }
760      break;
761    }
762    case SIZE_NxN:
763    {
764      if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth && !( pcCU->getWidth(uiAbsPartIdx) == 8 && pcCU->getHeight(uiAbsPartIdx) == 8 ) )
765      {
766        m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 0) );
767        m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 1) );
768        m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 2) );
769      }
770      break;
771    }
772    default:
773    {
774      assert(0);
775    }
776  }
777#if H_3D_QTLPC
778    }
779    else if(uiTexturePart == SIZE_2NxN || uiTexturePart == SIZE_2NxnU || uiTexturePart == SIZE_2NxnD)
780    {
781      //assert(eSize!=SIZE_NxN);
782      //assert(eSize!=SIZE_Nx2N);
783      //assert(eSize==SIZE_2Nx2N || eSize==SIZE_2NxN || eSize==SIZE_2NxnU || eSize==SIZE_2NxnD);
784      switch(eSize)
785      {
786      case SIZE_2Nx2N:
787        {
788          m_pcBinIf->encodeBin( 1, m_cCUPartSizeSCModel.get( 0, 0, 0) );
789          break;
790        }
791      case SIZE_2NxN:
792        {
793          m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 0) );
794          if ( pcCU->getSlice()->getSPS()->getAMPAcc( uiDepth ) )
795          {     
796            m_pcBinIf->encodeBin( 1, m_cCUPartSizeSCModel.get( 0, 0, 1) );
797          }
798          break;
799        }
800      case SIZE_2NxnU:
801      case SIZE_2NxnD:
802        {
803          m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 0) );
804          m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 1) );
805          m_pcBinIf->encodeBinEP((eSize == SIZE_2NxnU? 0: 1));
806          break;
807        }
808      default:
809        {
810          assert(0);
811        }
812      }
813    }
814    else if(uiTexturePart == SIZE_Nx2N|| uiTexturePart==SIZE_nLx2N || uiTexturePart==SIZE_nRx2N)
815    {
816      //assert(eSize!=SIZE_NxN);
817      //assert(eSize!=SIZE_2NxN);
818      //assert(eSize==SIZE_2Nx2N ||eSize==SIZE_Nx2N || eSize==SIZE_nLx2N || eSize==SIZE_nRx2N);
819      switch(eSize)
820      {
821      case SIZE_2Nx2N:
822        {
823          m_pcBinIf->encodeBin( 1, m_cCUPartSizeSCModel.get( 0, 0, 0) );
824          break;
825        }
826      case SIZE_Nx2N:
827        {
828          m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 0) );
829          if ( pcCU->getSlice()->getSPS()->getAMPAcc( uiDepth ) )
830          {     
831            m_pcBinIf->encodeBin( 1, m_cCUPartSizeSCModel.get( 0, 0, 1) );
832          }
833          break;
834        }
835      case SIZE_nLx2N:
836      case SIZE_nRx2N:
837        {
838          m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 0) );
839          m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 1) );
840          m_pcBinIf->encodeBinEP((eSize == SIZE_nLx2N? 0: 1));
841          break;
842        }
843      default:
844        {
845          assert(0);
846        }
847      }
848    }
849    else
850    {
851      printf("uiTexturePart=%d",uiTexturePart);
852      assert(0);
853    }
854#endif
855}
856
857/** code prediction mode
858 * \param pcCU
859 * \param uiAbsPartIdx 
860 * \returns Void
861 */
862Void TEncSbac::codePredMode( TComDataCU* pcCU, UInt uiAbsPartIdx )
863{
864#if H_3D_DIM_SDC
865  if ( pcCU->getSlice()->isIntra() )
866  {
867    assert( pcCU->isIntra(uiAbsPartIdx) );
868    return;
869  }
870#endif
871 
872  // get context function is here
873  Int iPredMode = pcCU->getPredictionMode( uiAbsPartIdx );
874  m_pcBinIf->encodeBin( iPredMode == MODE_INTER ? 0 : 1, m_cCUPredModeSCModel.get( 0, 0, 0 ) );
875#if H_MV_ENC_DEC_TRAC
876  DTRACE_CU("pred_mode_flag", iPredMode == MODE_INTER ? 0 : 1); 
877#endif
878}
879
880Void TEncSbac::codeCUTransquantBypassFlag( TComDataCU* pcCU, UInt uiAbsPartIdx )
881{
882  UInt uiSymbol = pcCU->getCUTransquantBypass(uiAbsPartIdx);
883  m_pcBinIf->encodeBin( uiSymbol, m_CUTransquantBypassFlagSCModel.get( 0, 0, 0 ) );
884#if H_MV_ENC_DEC_TRAC
885  DTRACE_CU("cu_transquant_bypass_flag", uiSymbol); 
886#endif
887}
888
889/** code skip flag
890 * \param pcCU
891 * \param uiAbsPartIdx
892 * \returns Void
893 */
894Void TEncSbac::codeSkipFlag( TComDataCU* pcCU, UInt uiAbsPartIdx )
895{
896  // get context function is here
897  UInt uiSymbol = pcCU->isSkipped( uiAbsPartIdx ) ? 1 : 0;
898  UInt uiCtxSkip = pcCU->getCtxSkipFlag( uiAbsPartIdx ) ;
899  m_pcBinIf->encodeBin( uiSymbol, m_cCUSkipFlagSCModel.get( 0, 0, uiCtxSkip ) );
900#if !H_MV_ENC_DEC_TRAC
901  DTRACE_CABAC_VL( g_nSymbolCounter++ );
902  DTRACE_CABAC_T( "\tSkipFlag" );
903  DTRACE_CABAC_T( "\tuiCtxSkip: ");
904  DTRACE_CABAC_V( uiCtxSkip );
905  DTRACE_CABAC_T( "\tuiSymbol: ");
906  DTRACE_CABAC_V( uiSymbol );
907  DTRACE_CABAC_T( "\n");
908#else
909  DTRACE_CU("cu_skip_flag", uiSymbol); 
910#endif
911}
912
913/** code merge flag
914 * \param pcCU
915 * \param uiAbsPartIdx
916 * \returns Void
917 */
918Void TEncSbac::codeMergeFlag( TComDataCU* pcCU, UInt uiAbsPartIdx )
919{
920  const UInt uiSymbol = pcCU->getMergeFlag( uiAbsPartIdx ) ? 1 : 0;
921  m_pcBinIf->encodeBin( uiSymbol, *m_cCUMergeFlagExtSCModel.get( 0 ) );
922
923#if H_MV_ENC_DEC_TRAC
924  DTRACE_PU("merge_flag", uiSymbol); 
925#else
926  DTRACE_CABAC_VL( g_nSymbolCounter++ );
927  DTRACE_CABAC_T( "\tMergeFlag: " );
928  DTRACE_CABAC_V( uiSymbol );
929  DTRACE_CABAC_T( "\tAddress: " );
930  DTRACE_CABAC_V( pcCU->getAddr() );
931  DTRACE_CABAC_T( "\tuiAbsPartIdx: " );
932  DTRACE_CABAC_V( uiAbsPartIdx );
933  DTRACE_CABAC_T( "\n" );
934#endif
935}
936
937/** code merge index
938 * \param pcCU
939 * \param uiAbsPartIdx
940 * \returns Void
941 */
942Void TEncSbac::codeMergeIndex( TComDataCU* pcCU, UInt uiAbsPartIdx )
943{
944  UInt uiUnaryIdx = pcCU->getMergeIndex( uiAbsPartIdx );
945  UInt uiNumCand = pcCU->getSlice()->getMaxNumMergeCand();
946  if ( uiNumCand > 1 )
947  {
948    for( UInt ui = 0; ui < uiNumCand - 1; ++ui )
949    {
950      const UInt uiSymbol = ui == uiUnaryIdx ? 0 : 1;
951      if ( ui==0 )
952      {
953        m_pcBinIf->encodeBin( uiSymbol, m_cCUMergeIdxExtSCModel.get( 0, 0, 0 ) );
954      }
955      else
956      {
957        m_pcBinIf->encodeBinEP( uiSymbol );
958      }
959      if( uiSymbol == 0 )
960      {
961        break;
962      }
963    }
964#if H_MV_ENC_DEC_TRAC
965    DTRACE_PU("merge_idx", uiUnaryIdx); 
966#endif
967  }
968#if !H_MV_ENC_DEC_TRAC
969  DTRACE_CABAC_VL( g_nSymbolCounter++ );
970  DTRACE_CABAC_T( "\tparseMergeIndex()" );
971  DTRACE_CABAC_T( "\tuiMRGIdx= " );
972  DTRACE_CABAC_V( pcCU->getMergeIndex( uiAbsPartIdx ) );
973  DTRACE_CABAC_T( "\n" );
974#endif
975}
976
977#if H_3D_ARP
978Void TEncSbac::codeARPW( TComDataCU* pcCU, UInt uiAbsPartIdx )
979{
980  Int  iW = (Int)pcCU->getARPW( uiAbsPartIdx );
981  Int  iMaxW = pcCU->getSlice()->getARPStepNum() - 1;
982  assert( iMaxW > 0);
983
984  Int nOffset = pcCU->getCTXARPWFlag(uiAbsPartIdx);
985  Int nBinNum = iW + ( iW != iMaxW );
986  m_pcBinIf->encodeBin( iW ? 1 : 0 , m_cCUPUARPWSCModel.get( 0, 0, 0 + nOffset ) );
987  if( nBinNum > 1 )
988  {
989     m_pcBinIf->encodeBin( ( iW == iMaxW ) ? 1 : 0, m_cCUPUARPWSCModel.get( 0, 0, 2 ) );
990  }
991#if H_MV_ENC_DEC_TRAC
992  DTRACE_CU("iv_res_pred_weight_idx", iW); 
993#endif
994}
995#endif
996
997#if H_3D_IC
998/** code Illumination Compensation flag
999 * \param pcCU
1000 * \param uiAbsPartIdx
1001 * \returns Void
1002 */
1003Void TEncSbac::codeICFlag( TComDataCU* pcCU, UInt uiAbsPartIdx )
1004{
1005  // get context function is here
1006  UInt uiSymbol = pcCU->getICFlag( uiAbsPartIdx ) ? 1 : 0;
1007  m_pcBinIf->encodeBin( uiSymbol, m_cCUICFlagSCModel.get( 0, 0, 0 ) );
1008#if !H_MV_ENC_DEC_TRAC
1009  DTRACE_CABAC_VL( g_nSymbolCounter++ );
1010  DTRACE_CABAC_T( "\tICFlag" );
1011  DTRACE_CABAC_T( "\tuiSymbol: ");
1012  DTRACE_CABAC_V( uiSymbol );
1013  DTRACE_CABAC_T( "\n");
1014#else
1015  DTRACE_CU("ic_flag", uiSymbol ); 
1016#endif
1017}
1018#endif
1019
1020
1021Void TEncSbac::codeSplitFlag   ( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
1022{
1023  if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth )
1024    return;
1025 
1026  UInt uiCtx           = pcCU->getCtxSplitFlag( uiAbsPartIdx, uiDepth );
1027  UInt uiCurrSplitFlag = ( pcCU->getDepth( uiAbsPartIdx ) > uiDepth ) ? 1 : 0;
1028 
1029  assert( uiCtx < 3 );
1030#if H_3D_QTLPC
1031  Bool bCodeSplitFlag    = true;
1032
1033  TComSPS *sps           = pcCU->getPic()->getSlice(0)->getSPS();
1034  TComPic *pcTexture     = pcCU->getSlice()->getTexturePic();
1035  Bool bDepthMapDetect   = (pcTexture != NULL);
1036  Bool bIntraSliceDetect = (pcCU->getSlice()->getSliceType() == I_SLICE);
1037
1038  Bool rapPic     = (pcCU->getSlice()->getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_W_RADL || pcCU->getSlice()->getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_N_LP || pcCU->getSlice()->getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA);
1039
1040  if(bDepthMapDetect && !bIntraSliceDetect && !rapPic && pcCU->getPic()->getReduceBitsFlag() && sps->getUseQTL() && sps->getUsePC())
1041  {
1042    TComDataCU *pcTextureCU = pcTexture->getCU(pcCU->getAddr());
1043    UInt uiCUIdx            = (pcCU->getZorderIdxInCU() == 0) ? uiAbsPartIdx : pcCU->getZorderIdxInCU();
1044    assert(pcTextureCU->getDepth(uiCUIdx) >= uiDepth);
1045    bCodeSplitFlag          = (pcTextureCU->getDepth(uiCUIdx) > uiDepth);
1046  }
1047
1048  if(!bCodeSplitFlag)
1049  {
1050    assert(uiCurrSplitFlag == 0);
1051    return;
1052  }
1053#endif
1054  m_pcBinIf->encodeBin( uiCurrSplitFlag, m_cCUSplitFlagSCModel.get( 0, 0, uiCtx ) );
1055#if !H_MV_ENC_DEC_TRAC
1056  DTRACE_CABAC_VL( g_nSymbolCounter++ )
1057  DTRACE_CABAC_T( "\tSplitFlag\n" )
1058#else
1059  DTRACE_CU("split_cu_flag", uiCurrSplitFlag ); 
1060#endif
1061  return;
1062}
1063
1064Void TEncSbac::codeTransformSubdivFlag( UInt uiSymbol, UInt uiCtx )
1065{
1066  m_pcBinIf->encodeBin( uiSymbol, m_cCUTransSubdivFlagSCModel.get( 0, 0, uiCtx ) );
1067#if !H_MV_ENC_DEC_TRAC
1068  DTRACE_CABAC_VL( g_nSymbolCounter++ )
1069  DTRACE_CABAC_T( "\tparseTransformSubdivFlag()" )
1070  DTRACE_CABAC_T( "\tsymbol=" )
1071  DTRACE_CABAC_V( uiSymbol )
1072  DTRACE_CABAC_T( "\tctx=" )
1073  DTRACE_CABAC_V( uiCtx )
1074  DTRACE_CABAC_T( "\n" )
1075#endif
1076}
1077
1078Void TEncSbac::codeIntraDirLumaAng( TComDataCU* pcCU, UInt absPartIdx, Bool isMultiple)
1079{
1080  UInt dir[4],j;
1081  Int preds[4][3] = {{-1, -1, -1},{-1, -1, -1},{-1, -1, -1},{-1, -1, -1}};
1082  Int predNum[4], predIdx[4] ={ -1,-1,-1,-1};
1083  PartSize mode = pcCU->getPartitionSize( absPartIdx );
1084  UInt partNum = isMultiple?(mode==SIZE_NxN?4:1):1;
1085  UInt partOffset = ( pcCU->getPic()->getNumPartInCU() >> ( pcCU->getDepth(absPartIdx) << 1 ) ) >> 2;
1086  for (j=0;j<partNum;j++)
1087  {
1088    dir[j] = pcCU->getLumaIntraDir( absPartIdx+partOffset*j );
1089#if H_3D_DIM
1090    if( pcCU->getSlice()->getVpsDepthModesFlag() )
1091    {
1092      codeIntraDepth( pcCU, absPartIdx+partOffset*j );
1093    }
1094    if( pcCU->getLumaIntraDir( absPartIdx+partOffset*j ) < NUM_INTRA_MODE )
1095    {
1096#endif
1097      predNum[j] = pcCU->getIntraDirLumaPredictor(absPartIdx+partOffset*j, preds[j]); 
1098      for(UInt i = 0; i < predNum[j]; i++)
1099      {
1100        if(dir[j] == preds[j][i])
1101        {
1102          predIdx[j] = i;
1103        }
1104      }
1105      m_pcBinIf->encodeBin((predIdx[j] != -1)? 1 : 0, m_cCUIntraPredSCModel.get( 0, 0, 0 ) );
1106#if H_MV_ENC_DEC_TRAC
1107      DTRACE_CU("prev_intra_luma_pred_flag", (predIdx[j] != -1)? 1 : 0); 
1108#endif
1109#if H_3D_DIM
1110    }
1111#endif
1112
1113  } 
1114  for (j=0;j<partNum;j++)
1115  {
1116#if H_3D_DIM
1117    if( pcCU->getLumaIntraDir( absPartIdx+partOffset*j ) < NUM_INTRA_MODE )
1118    {
1119#endif
1120    if(predIdx[j] != -1)
1121    {
1122      m_pcBinIf->encodeBinEP( predIdx[j] ? 1 : 0 );
1123      if (predIdx[j])
1124      {
1125        m_pcBinIf->encodeBinEP( predIdx[j]-1 );
1126      }
1127#if H_MV_ENC_DEC_TRAC
1128      DTRACE_CU("mpm_idx", predIdx[j] ); 
1129#endif
1130    }
1131    else
1132    {
1133      if (preds[j][0] > preds[j][1])
1134      { 
1135        std::swap(preds[j][0], preds[j][1]); 
1136      }
1137      if (preds[j][0] > preds[j][2])
1138      {
1139        std::swap(preds[j][0], preds[j][2]);
1140      }
1141      if (preds[j][1] > preds[j][2])
1142      {
1143        std::swap(preds[j][1], preds[j][2]);
1144      }
1145      for(Int i = (predNum[j] - 1); i >= 0; i--)
1146      {
1147        dir[j] = dir[j] > preds[j][i] ? dir[j] - 1 : dir[j];
1148      }
1149      m_pcBinIf->encodeBinsEP( dir[j], 5 );
1150#if H_MV_ENC_DEC_TRAC
1151      DTRACE_CU("rem_intra_luma_pred_mode", dir[j] ); 
1152#endif
1153    }
1154#if H_3D_DIM
1155    }
1156#endif
1157  }
1158  return;
1159}
1160
1161Void TEncSbac::codeIntraDirChroma( TComDataCU* pcCU, UInt uiAbsPartIdx )
1162{
1163  UInt uiIntraDirChroma = pcCU->getChromaIntraDir( uiAbsPartIdx );
1164
1165  if( uiIntraDirChroma == DM_CHROMA_IDX ) 
1166  {
1167    m_pcBinIf->encodeBin( 0, m_cCUChromaPredSCModel.get( 0, 0, 0 ) );
1168#if H_MV_ENC_DEC_TRAC
1169    DTRACE_CU("intra_chroma_pred_mode", 0 ); 
1170#endif
1171  }
1172  else
1173  { 
1174    UInt uiAllowedChromaDir[ NUM_CHROMA_MODE ];
1175    pcCU->getAllowedChromaDir( uiAbsPartIdx, uiAllowedChromaDir );
1176
1177    for( Int i = 0; i < NUM_CHROMA_MODE - 1; i++ )
1178    {
1179      if( uiIntraDirChroma == uiAllowedChromaDir[i] )
1180      {
1181        uiIntraDirChroma = i;
1182        break;
1183      }
1184    }
1185    m_pcBinIf->encodeBin( 1, m_cCUChromaPredSCModel.get( 0, 0, 0 ) );
1186
1187    m_pcBinIf->encodeBinsEP( uiIntraDirChroma, 2 );
1188#if H_MV_ENC_DEC_TRAC
1189    DTRACE_CU("intra_chroma_pred_mode", uiIntraDirChroma ); 
1190#endif
1191
1192  }
1193  return;
1194}
1195
1196#if H_3D_DIM
1197Void TEncSbac::codeIntraDepth( TComDataCU* pcCU, UInt absPartIdx )
1198{
1199  codeIntraDepthMode( pcCU, absPartIdx );
1200
1201  UInt dir     = pcCU->getLumaIntraDir( absPartIdx );
1202  UInt dimType = getDimType( dir );
1203
1204  switch( dimType )
1205  {
1206#if H_3D_DIM_DMM
1207  case( DMM1_IDX ):
1208    {
1209      xCodeDmm1WedgeIdx( pcCU->getDmmWedgeTabIdx( dimType, absPartIdx ), g_dmm1TabIdxBits[pcCU->getIntraSizeIdx(absPartIdx)] );
1210    } break;
1211  case( DMM4_IDX ): break;
1212#endif
1213  default: break;
1214  }
1215}
1216
1217Void TEncSbac::codeIntraDepthMode( TComDataCU* pcCU, UInt absPartIdx )
1218{
1219  UInt dir = pcCU->getLumaIntraDir( absPartIdx );
1220
1221  if( ( pcCU->getSlice()->getSPS()->getMaxCUWidth() >> pcCU->getDepth( absPartIdx ) ) < 64 ) //DMM and HEVC intra modes are both allowed
1222  {
1223    m_pcBinIf->encodeBin( isDimMode( dir ) ? 0 : 1, m_cAngleFlagSCModel.get( 0, 0, 0 ) );
1224  }
1225  if( isDimMode( dir ) )
1226  {
1227    UInt uiCodeIdx = 0;
1228
1229    switch( getDimType( dir ) )
1230    {
1231    case DMM1_IDX: uiCodeIdx = 0; break;
1232    case DMM4_IDX: uiCodeIdx = 1; break;
1233    default:                      break;
1234    }
1235    //mode coding
1236    m_pcBinIf->encodeBin( uiCodeIdx == 0 ? 0 : 1, m_cDepthIntraModeSCModel.get( 0, 0, 0 ) );
1237  }
1238}
1239#endif
1240
1241
1242Void TEncSbac::codeInterDir( TComDataCU* pcCU, UInt uiAbsPartIdx )
1243{
1244  const UInt uiInterDir = pcCU->getInterDir( uiAbsPartIdx ) - 1;
1245  const UInt uiCtx      = pcCU->getCtxInterDir( uiAbsPartIdx );
1246  ContextModel *pCtx    = m_cCUInterDirSCModel.get( 0 );
1247  if (pcCU->getPartitionSize(uiAbsPartIdx) == SIZE_2Nx2N || pcCU->getHeight(uiAbsPartIdx) != 8 )
1248  {
1249    m_pcBinIf->encodeBin( uiInterDir == 2 ? 1 : 0, *( pCtx + uiCtx ) );
1250  }
1251  if (uiInterDir < 2)
1252  {
1253    m_pcBinIf->encodeBin( uiInterDir, *( pCtx + 4 ) );
1254  }
1255#if H_MV_ENC_DEC_TRAC
1256  DTRACE_PU("inter_pred_idc", uiInterDir ); 
1257#endif
1258
1259  return;
1260}
1261
1262Void TEncSbac::codeRefFrmIdx( TComDataCU* pcCU, UInt uiAbsPartIdx, RefPicList eRefList )
1263{
1264  {
1265    Int iRefFrame = pcCU->getCUMvField( eRefList )->getRefIdx( uiAbsPartIdx );
1266    ContextModel *pCtx = m_cCURefPicSCModel.get( 0 );
1267    m_pcBinIf->encodeBin( ( iRefFrame == 0 ? 0 : 1 ), *pCtx );
1268   
1269    if( iRefFrame > 0 )
1270    {
1271      UInt uiRefNum = pcCU->getSlice()->getNumRefIdx( eRefList ) - 2;
1272      pCtx++;
1273      iRefFrame--;
1274      for( UInt ui = 0; ui < uiRefNum; ++ui )
1275      {
1276        const UInt uiSymbol = ui == iRefFrame ? 0 : 1;
1277        if( ui == 0 )
1278        {
1279          m_pcBinIf->encodeBin( uiSymbol, *pCtx );       
1280        }
1281        else
1282        {
1283          m_pcBinIf->encodeBinEP( uiSymbol );
1284        }
1285        if( uiSymbol == 0 )
1286        {
1287          break;
1288        }
1289      }
1290    }
1291#if H_MV_ENC_DEC_TRAC
1292#if ENC_DEC_TRACE
1293    iRefFrame = pcCU->getCUMvField( eRefList )->getRefIdx( uiAbsPartIdx ); 
1294    if ( eRefList == REF_PIC_LIST_0 )
1295    {
1296      DTRACE_PU("ref_idx_l0", iRefFrame)
1297    }
1298    else
1299    {
1300      DTRACE_PU("ref_idx_l1", iRefFrame)
1301    }
1302#endif
1303#endif
1304  }
1305  return;
1306}
1307
1308Void TEncSbac::codeMvd( TComDataCU* pcCU, UInt uiAbsPartIdx, RefPicList eRefList )
1309{
1310  if(pcCU->getSlice()->getMvdL1ZeroFlag() && eRefList == REF_PIC_LIST_1 && pcCU->getInterDir(uiAbsPartIdx)==3)
1311  {
1312    return;
1313  }
1314
1315  const TComCUMvField* pcCUMvField = pcCU->getCUMvField( eRefList );
1316  const Int iHor = pcCUMvField->getMvd( uiAbsPartIdx ).getHor();
1317  const Int iVer = pcCUMvField->getMvd( uiAbsPartIdx ).getVer();
1318  ContextModel* pCtx = m_cCUMvdSCModel.get( 0 );
1319
1320  m_pcBinIf->encodeBin( iHor != 0 ? 1 : 0, *pCtx );
1321  m_pcBinIf->encodeBin( iVer != 0 ? 1 : 0, *pCtx );
1322
1323  const Bool bHorAbsGr0 = iHor != 0;
1324  const Bool bVerAbsGr0 = iVer != 0;
1325  const UInt uiHorAbs   = 0 > iHor ? -iHor : iHor;
1326  const UInt uiVerAbs   = 0 > iVer ? -iVer : iVer;
1327  pCtx++;
1328
1329  if( bHorAbsGr0 )
1330  {
1331    m_pcBinIf->encodeBin( uiHorAbs > 1 ? 1 : 0, *pCtx );
1332  }
1333
1334  if( bVerAbsGr0 )
1335  {
1336    m_pcBinIf->encodeBin( uiVerAbs > 1 ? 1 : 0, *pCtx );
1337  }
1338
1339  if( bHorAbsGr0 )
1340  {
1341    if( uiHorAbs > 1 )
1342    {
1343      xWriteEpExGolomb( uiHorAbs-2, 1 );
1344    }
1345
1346    m_pcBinIf->encodeBinEP( 0 > iHor ? 1 : 0 );
1347  }
1348
1349  if( bVerAbsGr0 )
1350  {
1351    if( uiVerAbs > 1 )
1352    {
1353      xWriteEpExGolomb( uiVerAbs-2, 1 );
1354    }
1355
1356    m_pcBinIf->encodeBinEP( 0 > iVer ? 1 : 0 );
1357  }
1358 
1359  return;
1360}
1361
1362Void TEncSbac::codeDeltaQP( TComDataCU* pcCU, UInt uiAbsPartIdx )
1363{
1364  Int iDQp  = pcCU->getQP( uiAbsPartIdx ) - pcCU->getRefQP( uiAbsPartIdx );
1365 
1366  Int qpBdOffsetY =  pcCU->getSlice()->getSPS()->getQpBDOffsetY();
1367  iDQp = (iDQp + 78 + qpBdOffsetY + (qpBdOffsetY/2)) % (52 + qpBdOffsetY) - 26 - (qpBdOffsetY/2);
1368
1369  UInt uiAbsDQp = (UInt)((iDQp > 0)? iDQp  : (-iDQp));
1370  UInt TUValue = min((Int)uiAbsDQp, CU_DQP_TU_CMAX);
1371  xWriteUnaryMaxSymbol( TUValue, &m_cCUDeltaQpSCModel.get( 0, 0, 0 ), 1, CU_DQP_TU_CMAX);
1372  if( uiAbsDQp >= CU_DQP_TU_CMAX )
1373  {
1374    xWriteEpExGolomb( uiAbsDQp - CU_DQP_TU_CMAX, CU_DQP_EG_k );
1375  }
1376
1377  if ( uiAbsDQp > 0)
1378  {
1379    UInt uiSign = (iDQp > 0 ? 0 : 1);
1380    m_pcBinIf->encodeBinEP(uiSign);
1381  }
1382
1383  return;
1384}
1385
1386Void TEncSbac::codeQtCbf( TComDataCU* pcCU, UInt uiAbsPartIdx, TextType eType, UInt uiTrDepth )
1387{
1388  UInt uiCbf = pcCU->getCbf     ( uiAbsPartIdx, eType, uiTrDepth );
1389  UInt uiCtx = pcCU->getCtxQtCbf( eType, uiTrDepth );
1390  m_pcBinIf->encodeBin( uiCbf , m_cCUQtCbfSCModel.get( 0, eType ? TEXT_CHROMA : eType, uiCtx ) );
1391#if !H_MV_ENC_DEC_TRAC
1392  DTRACE_CABAC_VL( g_nSymbolCounter++ )
1393  DTRACE_CABAC_T( "\tparseQtCbf()" )
1394  DTRACE_CABAC_T( "\tsymbol=" )
1395  DTRACE_CABAC_V( uiCbf )
1396  DTRACE_CABAC_T( "\tctx=" )
1397  DTRACE_CABAC_V( uiCtx )
1398  DTRACE_CABAC_T( "\tetype=" )
1399  DTRACE_CABAC_V( eType )
1400  DTRACE_CABAC_T( "\tuiAbsPartIdx=" )
1401  DTRACE_CABAC_V( uiAbsPartIdx )
1402  DTRACE_CABAC_T( "\n" )
1403#endif
1404}
1405
1406void TEncSbac::codeTransformSkipFlags (TComDataCU* pcCU, UInt uiAbsPartIdx, UInt width, UInt height, TextType eTType )
1407{
1408  if (pcCU->getCUTransquantBypass(uiAbsPartIdx))
1409  {
1410    return;
1411  }
1412  if(width != 4 || height != 4)
1413  {
1414    return;
1415  }
1416
1417  UInt useTransformSkip = pcCU->getTransformSkip( uiAbsPartIdx,eTType);
1418  m_pcBinIf->encodeBin( useTransformSkip, m_cTransformSkipSCModel.get( 0, eTType? TEXT_CHROMA: TEXT_LUMA, 0 ) );
1419#if !H_MV_ENC_DEC_TRAC
1420  DTRACE_CABAC_VL( g_nSymbolCounter++ )
1421  DTRACE_CABAC_T("\tparseTransformSkip()");
1422  DTRACE_CABAC_T( "\tsymbol=" )
1423  DTRACE_CABAC_V( useTransformSkip )
1424  DTRACE_CABAC_T( "\tAddr=" )
1425  DTRACE_CABAC_V( pcCU->getAddr() )
1426  DTRACE_CABAC_T( "\tetype=" )
1427  DTRACE_CABAC_V( eTType )
1428  DTRACE_CABAC_T( "\tuiAbsPartIdx=" )
1429  DTRACE_CABAC_V( uiAbsPartIdx )
1430  DTRACE_CABAC_T( "\n" )
1431#endif
1432}
1433
1434/** Code I_PCM information.
1435 * \param pcCU pointer to CU
1436 * \param uiAbsPartIdx CU index
1437 * \returns Void
1438 */
1439Void TEncSbac::codeIPCMInfo( TComDataCU* pcCU, UInt uiAbsPartIdx )
1440{
1441  UInt uiIPCM = (pcCU->getIPCMFlag(uiAbsPartIdx) == true)? 1 : 0;
1442
1443  Bool writePCMSampleFlag = pcCU->getIPCMFlag(uiAbsPartIdx);
1444
1445  m_pcBinIf->encodeBinTrm (uiIPCM);
1446#if H_MV_ENC_DEC_TRAC         
1447  DTRACE_CU("pcm_flag", uiIPCM)
1448#endif
1449  if (writePCMSampleFlag)
1450  {
1451    m_pcBinIf->encodePCMAlignBits();
1452
1453    UInt uiMinCoeffSize = pcCU->getPic()->getMinCUWidth()*pcCU->getPic()->getMinCUHeight();
1454    UInt uiLumaOffset   = uiMinCoeffSize*uiAbsPartIdx;
1455    UInt uiChromaOffset = uiLumaOffset>>2;
1456    Pel* piPCMSample;
1457    UInt uiWidth;
1458    UInt uiHeight;
1459    UInt uiSampleBits;
1460    UInt uiX, uiY;
1461
1462    piPCMSample = pcCU->getPCMSampleY() + uiLumaOffset;
1463    uiWidth = pcCU->getWidth(uiAbsPartIdx);
1464    uiHeight = pcCU->getHeight(uiAbsPartIdx);
1465    uiSampleBits = pcCU->getSlice()->getSPS()->getPCMBitDepthLuma();
1466
1467    for(uiY = 0; uiY < uiHeight; uiY++)
1468    {
1469      for(uiX = 0; uiX < uiWidth; uiX++)
1470      {
1471        UInt uiSample = piPCMSample[uiX];
1472
1473        m_pcBinIf->xWritePCMCode(uiSample, uiSampleBits);
1474      }
1475      piPCMSample += uiWidth;
1476    }
1477
1478    piPCMSample = pcCU->getPCMSampleCb() + uiChromaOffset;
1479    uiWidth = pcCU->getWidth(uiAbsPartIdx)/2;
1480    uiHeight = pcCU->getHeight(uiAbsPartIdx)/2;
1481    uiSampleBits = pcCU->getSlice()->getSPS()->getPCMBitDepthChroma();
1482
1483    for(uiY = 0; uiY < uiHeight; uiY++)
1484    {
1485      for(uiX = 0; uiX < uiWidth; uiX++)
1486      {
1487        UInt uiSample = piPCMSample[uiX];
1488
1489        m_pcBinIf->xWritePCMCode(uiSample, uiSampleBits);
1490      }
1491      piPCMSample += uiWidth;
1492    }
1493
1494    piPCMSample = pcCU->getPCMSampleCr() + uiChromaOffset;
1495    uiWidth = pcCU->getWidth(uiAbsPartIdx)/2;
1496    uiHeight = pcCU->getHeight(uiAbsPartIdx)/2;
1497    uiSampleBits = pcCU->getSlice()->getSPS()->getPCMBitDepthChroma();
1498
1499    for(uiY = 0; uiY < uiHeight; uiY++)
1500    {
1501      for(uiX = 0; uiX < uiWidth; uiX++)
1502      {
1503        UInt uiSample = piPCMSample[uiX];
1504
1505        m_pcBinIf->xWritePCMCode(uiSample, uiSampleBits);
1506      }
1507      piPCMSample += uiWidth;
1508    }
1509    m_pcBinIf->resetBac();
1510  }
1511}
1512
1513Void TEncSbac::codeQtRootCbf( TComDataCU* pcCU, UInt uiAbsPartIdx )
1514{
1515  UInt uiCbf = pcCU->getQtRootCbf( uiAbsPartIdx );
1516  UInt uiCtx = 0;
1517  m_pcBinIf->encodeBin( uiCbf , m_cCUQtRootCbfSCModel.get( 0, 0, uiCtx ) );
1518#if !H_MV_ENC_DEC_TRAC
1519  DTRACE_CABAC_VL( g_nSymbolCounter++ )
1520  DTRACE_CABAC_T( "\tparseQtRootCbf()" )
1521  DTRACE_CABAC_T( "\tsymbol=" )
1522  DTRACE_CABAC_V( uiCbf )
1523  DTRACE_CABAC_T( "\tctx=" )
1524  DTRACE_CABAC_V( uiCtx )
1525  DTRACE_CABAC_T( "\tuiAbsPartIdx=" )
1526  DTRACE_CABAC_V( uiAbsPartIdx )
1527  DTRACE_CABAC_T( "\n" )
1528#else
1529  DTRACE_CU( "rqt_root_cbf", uiCbf )
1530#endif
1531}
1532
1533Void TEncSbac::codeQtCbfZero( TComDataCU* pcCU, TextType eType, UInt uiTrDepth )
1534{
1535  // this function is only used to estimate the bits when cbf is 0
1536  // and will never be called when writing the bistream. do not need to write log
1537  UInt uiCbf = 0;
1538  UInt uiCtx = pcCU->getCtxQtCbf( eType, uiTrDepth );
1539  m_pcBinIf->encodeBin( uiCbf , m_cCUQtCbfSCModel.get( 0, eType ? TEXT_CHROMA : eType, uiCtx ) );
1540}
1541
1542Void TEncSbac::codeQtRootCbfZero( TComDataCU* pcCU )
1543{
1544  // this function is only used to estimate the bits when cbf is 0
1545  // and will never be called when writing the bistream. do not need to write log
1546  UInt uiCbf = 0;
1547  UInt uiCtx = 0;
1548  m_pcBinIf->encodeBin( uiCbf , m_cCUQtRootCbfSCModel.get( 0, 0, uiCtx ) );
1549}
1550
1551/** Encode (X,Y) position of the last significant coefficient
1552 * \param uiPosX X component of last coefficient
1553 * \param uiPosY Y component of last coefficient
1554 * \param width  Block width
1555 * \param height Block height
1556 * \param eTType plane type / luminance or chrominance
1557 * \param uiScanIdx scan type (zig-zag, hor, ver)
1558 * This method encodes the X and Y component within a block of the last significant coefficient.
1559 */
1560Void TEncSbac::codeLastSignificantXY( UInt uiPosX, UInt uiPosY, Int width, Int height, TextType eTType, UInt uiScanIdx )
1561{ 
1562  // swap
1563  if( uiScanIdx == SCAN_VER )
1564  {
1565    swap( uiPosX, uiPosY );
1566  }
1567
1568  UInt uiCtxLast;
1569  ContextModel *pCtxX = m_cCuCtxLastX.get( 0, eTType );
1570  ContextModel *pCtxY = m_cCuCtxLastY.get( 0, eTType );
1571  UInt uiGroupIdxX    = g_uiGroupIdx[ uiPosX ];
1572  UInt uiGroupIdxY    = g_uiGroupIdx[ uiPosY ];
1573
1574
1575  Int blkSizeOffsetX, blkSizeOffsetY, shiftX, shiftY;
1576  blkSizeOffsetX = eTType ? 0: (g_aucConvertToBit[ width ] *3 + ((g_aucConvertToBit[ width ] +1)>>2));
1577  blkSizeOffsetY = eTType ? 0: (g_aucConvertToBit[ height ]*3 + ((g_aucConvertToBit[ height ]+1)>>2));
1578  shiftX= eTType ? g_aucConvertToBit[ width  ] :((g_aucConvertToBit[ width  ]+3)>>2);
1579  shiftY= eTType ? g_aucConvertToBit[ height ] :((g_aucConvertToBit[ height ]+3)>>2);
1580  // posX
1581  for( uiCtxLast = 0; uiCtxLast < uiGroupIdxX; uiCtxLast++ )
1582  {
1583      m_pcBinIf->encodeBin( 1, *( pCtxX + blkSizeOffsetX + (uiCtxLast >>shiftX) ) );
1584  }
1585  if( uiGroupIdxX < g_uiGroupIdx[ width - 1 ])
1586  {
1587      m_pcBinIf->encodeBin( 0, *( pCtxX + blkSizeOffsetX + (uiCtxLast >>shiftX) ) );
1588  }
1589
1590  // posY
1591  for( uiCtxLast = 0; uiCtxLast < uiGroupIdxY; uiCtxLast++ )
1592  {
1593    m_pcBinIf->encodeBin( 1, *( pCtxY + blkSizeOffsetY + (uiCtxLast >>shiftY) ) );
1594  }
1595  if( uiGroupIdxY < g_uiGroupIdx[ height - 1 ])
1596  {
1597    m_pcBinIf->encodeBin( 0, *( pCtxY + blkSizeOffsetY + (uiCtxLast >>shiftY) ) );
1598  }
1599  if ( uiGroupIdxX > 3 )
1600  {     
1601    UInt uiCount = ( uiGroupIdxX - 2 ) >> 1;
1602    uiPosX       = uiPosX - g_uiMinInGroup[ uiGroupIdxX ];
1603    for (Int i = uiCount - 1 ; i >= 0; i-- )
1604    {
1605      m_pcBinIf->encodeBinEP( ( uiPosX >> i ) & 1 );
1606    }
1607  }
1608  if ( uiGroupIdxY > 3 )
1609  {     
1610    UInt uiCount = ( uiGroupIdxY - 2 ) >> 1;
1611    uiPosY       = uiPosY - g_uiMinInGroup[ uiGroupIdxY ];
1612    for ( Int i = uiCount - 1 ; i >= 0; i-- )
1613    {
1614      m_pcBinIf->encodeBinEP( ( uiPosY >> i ) & 1 );
1615    }
1616  }
1617}
1618
1619Void TEncSbac::codeCoeffNxN( TComDataCU* pcCU, TCoeff* pcCoef, UInt uiAbsPartIdx, UInt uiWidth, UInt uiHeight, UInt uiDepth, TextType eTType )
1620{
1621#if !H_MV_ENC_DEC_TRAC
1622  DTRACE_CABAC_VL( g_nSymbolCounter++ )
1623  DTRACE_CABAC_T( "\tparseCoeffNxN()\teType=" )
1624  DTRACE_CABAC_V( eTType )
1625  DTRACE_CABAC_T( "\twidth=" )
1626  DTRACE_CABAC_V( uiWidth )
1627  DTRACE_CABAC_T( "\theight=" )
1628  DTRACE_CABAC_V( uiHeight )
1629  DTRACE_CABAC_T( "\tdepth=" )
1630  DTRACE_CABAC_V( uiDepth )
1631  DTRACE_CABAC_T( "\tabspartidx=" )
1632  DTRACE_CABAC_V( uiAbsPartIdx )
1633  DTRACE_CABAC_T( "\ttoCU-X=" )
1634  DTRACE_CABAC_V( pcCU->getCUPelX() )
1635  DTRACE_CABAC_T( "\ttoCU-Y=" )
1636  DTRACE_CABAC_V( pcCU->getCUPelY() )
1637  DTRACE_CABAC_T( "\tCU-addr=" )
1638  DTRACE_CABAC_V(  pcCU->getAddr() )
1639  DTRACE_CABAC_T( "\tinCU-X=" )
1640  DTRACE_CABAC_V( g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsPartIdx] ] )
1641  DTRACE_CABAC_T( "\tinCU-Y=" )
1642  DTRACE_CABAC_V( g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsPartIdx] ] )
1643  DTRACE_CABAC_T( "\tpredmode=" )
1644  DTRACE_CABAC_V(  pcCU->getPredictionMode( uiAbsPartIdx ) )
1645  DTRACE_CABAC_T( "\n" )
1646#endif
1647  if( uiWidth > m_pcSlice->getSPS()->getMaxTrSize() )
1648  {
1649    uiWidth  = m_pcSlice->getSPS()->getMaxTrSize();
1650    uiHeight = m_pcSlice->getSPS()->getMaxTrSize();
1651  }
1652 
1653  UInt uiNumSig = 0;
1654 
1655  // compute number of significant coefficients
1656  uiNumSig = TEncEntropy::countNonZeroCoeffs(pcCoef, uiWidth * uiHeight);
1657 
1658  if ( uiNumSig == 0 )
1659    return;
1660  if(pcCU->getSlice()->getPPS()->getUseTransformSkip())
1661  {
1662    codeTransformSkipFlags( pcCU,uiAbsPartIdx, uiWidth, uiHeight, eTType );
1663  }
1664  eTType = eTType == TEXT_LUMA ? TEXT_LUMA : ( eTType == TEXT_NONE ? TEXT_NONE : TEXT_CHROMA );
1665 
1666  //----- encode significance map -----
1667  const UInt   uiLog2BlockSize = g_aucConvertToBit[ uiWidth ] + 2;
1668  UInt uiScanIdx = pcCU->getCoefScanIdx(uiAbsPartIdx, uiWidth, eTType==TEXT_LUMA, pcCU->isIntra(uiAbsPartIdx));
1669  const UInt *scan = g_auiSigLastScan[ uiScanIdx ][ uiLog2BlockSize - 1 ];
1670 
1671  Bool beValid;
1672  if (pcCU->getCUTransquantBypass(uiAbsPartIdx))
1673  {
1674    beValid = false;
1675  }
1676  else 
1677  {
1678    beValid = pcCU->getSlice()->getPPS()->getSignHideFlag() > 0;
1679  }
1680
1681  // Find position of last coefficient
1682  Int scanPosLast = -1;
1683  Int posLast;
1684
1685  const UInt * scanCG;
1686  {
1687    scanCG = g_auiSigLastScan[ uiScanIdx ][ uiLog2BlockSize > 3 ? uiLog2BlockSize-2-1 : 0 ];
1688    if( uiLog2BlockSize == 3 )
1689    {
1690      scanCG = g_sigLastScan8x8[ uiScanIdx ];
1691    }
1692    else if( uiLog2BlockSize == 5 )
1693    {
1694      scanCG = g_sigLastScanCG32x32;
1695    }
1696  }
1697  UInt uiSigCoeffGroupFlag[ MLS_GRP_NUM ];
1698  static const UInt uiShift = MLS_CG_SIZE >> 1;
1699  const UInt uiNumBlkSide = uiWidth >> uiShift;
1700
1701    ::memset( uiSigCoeffGroupFlag, 0, sizeof(UInt) * MLS_GRP_NUM );
1702
1703    do
1704    {
1705      posLast = scan[ ++scanPosLast ];
1706
1707      // get L1 sig map
1708      UInt uiPosY    = posLast >> uiLog2BlockSize;
1709      UInt uiPosX    = posLast - ( uiPosY << uiLog2BlockSize );
1710      UInt uiBlkIdx  = uiNumBlkSide * (uiPosY >> uiShift) + (uiPosX >> uiShift);
1711      if( pcCoef[ posLast ] )
1712      {
1713        uiSigCoeffGroupFlag[ uiBlkIdx ] = 1;
1714      }
1715
1716      uiNumSig -= ( pcCoef[ posLast ] != 0 );
1717    }
1718    while ( uiNumSig > 0 );
1719
1720  // Code position of last coefficient
1721  Int posLastY = posLast >> uiLog2BlockSize;
1722  Int posLastX = posLast - ( posLastY << uiLog2BlockSize );
1723  codeLastSignificantXY(posLastX, posLastY, uiWidth, uiHeight, eTType, uiScanIdx);
1724 
1725  //===== code significance flag =====
1726  ContextModel * const baseCoeffGroupCtx = m_cCUSigCoeffGroupSCModel.get( 0, eTType );
1727  ContextModel * const baseCtx = (eTType==TEXT_LUMA) ? m_cCUSigSCModel.get( 0, 0 ) : m_cCUSigSCModel.get( 0, 0 ) + NUM_SIG_FLAG_CTX_LUMA;
1728
1729
1730  const Int  iLastScanSet      = scanPosLast >> LOG2_SCAN_SET_SIZE;
1731  UInt c1 = 1;
1732  UInt uiGoRiceParam           = 0;
1733  Int  iScanPosSig             = scanPosLast;
1734
1735  for( Int iSubSet = iLastScanSet; iSubSet >= 0; iSubSet-- )
1736  {
1737    Int numNonZero = 0;
1738    Int  iSubPos     = iSubSet << LOG2_SCAN_SET_SIZE;
1739    uiGoRiceParam    = 0;
1740    Int absCoeff[16];
1741    UInt coeffSigns = 0;
1742
1743    Int lastNZPosInCG = -1, firstNZPosInCG = SCAN_SET_SIZE;
1744
1745    if( iScanPosSig == scanPosLast )
1746    {
1747      absCoeff[ 0 ] = abs( pcCoef[ posLast ] );
1748      coeffSigns    = ( pcCoef[ posLast ] < 0 );
1749      numNonZero    = 1;
1750      lastNZPosInCG  = iScanPosSig;
1751      firstNZPosInCG = iScanPosSig;
1752      iScanPosSig--;
1753    }
1754
1755      // encode significant_coeffgroup_flag
1756      Int iCGBlkPos = scanCG[ iSubSet ];
1757      Int iCGPosY   = iCGBlkPos / uiNumBlkSide;
1758      Int iCGPosX   = iCGBlkPos - (iCGPosY * uiNumBlkSide);
1759      if( iSubSet == iLastScanSet || iSubSet == 0)
1760      {
1761        uiSigCoeffGroupFlag[ iCGBlkPos ] = 1;
1762      }
1763      else
1764      {
1765          UInt uiSigCoeffGroup   = (uiSigCoeffGroupFlag[ iCGBlkPos ] != 0);
1766          UInt uiCtxSig  = TComTrQuant::getSigCoeffGroupCtxInc( uiSigCoeffGroupFlag, iCGPosX, iCGPosY, uiWidth, uiHeight );
1767          m_pcBinIf->encodeBin( uiSigCoeffGroup, baseCoeffGroupCtx[ uiCtxSig ] );
1768      }
1769     
1770      // encode significant_coeff_flag
1771      if( uiSigCoeffGroupFlag[ iCGBlkPos ] )
1772      {
1773        Int patternSigCtx = TComTrQuant::calcPatternSigCtx( uiSigCoeffGroupFlag, iCGPosX, iCGPosY, uiWidth, uiHeight );
1774        UInt uiBlkPos, uiPosY, uiPosX, uiSig, uiCtxSig;
1775        for( ; iScanPosSig >= iSubPos; iScanPosSig-- )
1776        {
1777          uiBlkPos  = scan[ iScanPosSig ]; 
1778          uiPosY    = uiBlkPos >> uiLog2BlockSize;
1779          uiPosX    = uiBlkPos - ( uiPosY << uiLog2BlockSize );
1780          uiSig     = (pcCoef[ uiBlkPos ] != 0);
1781          if( iScanPosSig > iSubPos || iSubSet == 0 || numNonZero )
1782          {
1783            uiCtxSig  = TComTrQuant::getSigCtxInc( patternSigCtx, uiScanIdx, uiPosX, uiPosY, uiLog2BlockSize, eTType );
1784            m_pcBinIf->encodeBin( uiSig, baseCtx[ uiCtxSig ] );
1785          }
1786          if( uiSig )
1787          {
1788            absCoeff[ numNonZero ] = abs( pcCoef[ uiBlkPos ] );
1789            coeffSigns = 2 * coeffSigns + ( pcCoef[ uiBlkPos ] < 0 );
1790            numNonZero++;
1791            if( lastNZPosInCG == -1 )
1792            {
1793              lastNZPosInCG = iScanPosSig;
1794            }
1795            firstNZPosInCG = iScanPosSig;
1796          }
1797        }
1798      }
1799      else
1800      {
1801        iScanPosSig = iSubPos - 1;
1802      }
1803
1804    if( numNonZero > 0 )
1805    {
1806      Bool signHidden = ( lastNZPosInCG - firstNZPosInCG >= SBH_THRESHOLD );
1807      UInt uiCtxSet = (iSubSet > 0 && eTType==TEXT_LUMA) ? 2 : 0;
1808     
1809      if( c1 == 0 )
1810      {
1811        uiCtxSet++;
1812      }
1813      c1 = 1;
1814      ContextModel *baseCtxMod = ( eTType==TEXT_LUMA ) ? m_cCUOneSCModel.get( 0, 0 ) + 4 * uiCtxSet : m_cCUOneSCModel.get( 0, 0 ) + NUM_ONE_FLAG_CTX_LUMA + 4 * uiCtxSet;
1815     
1816      Int numC1Flag = min(numNonZero, C1FLAG_NUMBER);
1817      Int firstC2FlagIdx = -1;
1818      for( Int idx = 0; idx < numC1Flag; idx++ )
1819      {
1820        UInt uiSymbol = absCoeff[ idx ] > 1;
1821        m_pcBinIf->encodeBin( uiSymbol, baseCtxMod[c1] );
1822        if( uiSymbol )
1823        {
1824          c1 = 0;
1825
1826          if (firstC2FlagIdx == -1)
1827          {
1828            firstC2FlagIdx = idx;
1829          }
1830        }
1831        else if( (c1 < 3) && (c1 > 0) )
1832        {
1833          c1++;
1834        }
1835      }
1836     
1837      if (c1 == 0)
1838      {
1839
1840        baseCtxMod = ( eTType==TEXT_LUMA ) ? m_cCUAbsSCModel.get( 0, 0 ) + uiCtxSet : m_cCUAbsSCModel.get( 0, 0 ) + NUM_ABS_FLAG_CTX_LUMA + uiCtxSet;
1841        if ( firstC2FlagIdx != -1)
1842        {
1843          UInt symbol = absCoeff[ firstC2FlagIdx ] > 2;
1844          m_pcBinIf->encodeBin( symbol, baseCtxMod[0] );
1845        }
1846      }
1847     
1848      if( beValid && signHidden )
1849      {
1850        m_pcBinIf->encodeBinsEP( (coeffSigns >> 1), numNonZero-1 );
1851      }
1852      else
1853      {
1854        m_pcBinIf->encodeBinsEP( coeffSigns, numNonZero );
1855      }
1856     
1857      Int iFirstCoeff2 = 1;   
1858      if (c1 == 0 || numNonZero > C1FLAG_NUMBER)
1859      {
1860        for ( Int idx = 0; idx < numNonZero; idx++ )
1861        {
1862          UInt baseLevel  = (idx < C1FLAG_NUMBER)? (2 + iFirstCoeff2 ) : 1;
1863
1864          if( absCoeff[ idx ] >= baseLevel)
1865          {
1866            xWriteCoefRemainExGolomb( absCoeff[ idx ] - baseLevel, uiGoRiceParam );
1867            if(absCoeff[idx] > 3*(1<<uiGoRiceParam))
1868            {
1869               uiGoRiceParam = min<UInt>(uiGoRiceParam+ 1, 4);
1870            }
1871          }
1872          if(absCoeff[ idx ] >= 2) 
1873          {
1874            iFirstCoeff2 = 0;
1875          }
1876        }       
1877      }
1878    }
1879  }
1880
1881  return;
1882}
1883
1884/** code SAO offset sign
1885 * \param code sign value
1886 */
1887Void TEncSbac::codeSAOSign( UInt code )
1888{
1889  m_pcBinIf->encodeBinEP( code );
1890}
1891
1892Void TEncSbac::codeSaoMaxUvlc    ( UInt code, UInt maxSymbol )
1893{
1894  if (maxSymbol == 0)
1895  {
1896    return;
1897  }
1898
1899  Int i;
1900  Bool bCodeLast = ( maxSymbol > code );
1901
1902  if ( code == 0 )
1903  {
1904    m_pcBinIf->encodeBinEP( 0 );
1905  }
1906  else
1907  {
1908    m_pcBinIf->encodeBinEP( 1 );
1909    for ( i=0; i<code-1; i++ )
1910    {
1911      m_pcBinIf->encodeBinEP( 1 );
1912    }
1913    if( bCodeLast )
1914    {
1915      m_pcBinIf->encodeBinEP( 0 );
1916    }
1917  }
1918}
1919
1920
1921/** Code SAO EO class or BO band position
1922 * \param uiLength
1923 * \param uiCode
1924 */
1925Void TEncSbac::codeSaoUflc       ( UInt uiLength, UInt uiCode )
1926{
1927   m_pcBinIf->encodeBinsEP ( uiCode, uiLength );
1928}
1929/** Code SAO merge flags
1930 * \param uiCode
1931 * \param uiCompIdx
1932 */
1933Void TEncSbac::codeSaoMerge       ( UInt uiCode )
1934{
1935  if (uiCode == 0)
1936  {
1937    m_pcBinIf->encodeBin(0,  m_cSaoMergeSCModel.get( 0, 0, 0 ));
1938  }
1939  else
1940  {
1941    m_pcBinIf->encodeBin(1,  m_cSaoMergeSCModel.get( 0, 0, 0 ));
1942  }
1943}
1944/** Code SAO type index
1945 * \param uiCode
1946 */
1947Void TEncSbac::codeSaoTypeIdx       ( UInt uiCode)
1948{
1949  if (uiCode == 0)
1950  {
1951    m_pcBinIf->encodeBin( 0, m_cSaoTypeIdxSCModel.get( 0, 0, 0 ) );
1952  }
1953  else
1954  {
1955    m_pcBinIf->encodeBin( 1, m_cSaoTypeIdxSCModel.get( 0, 0, 0 ) );
1956    m_pcBinIf->encodeBinEP( uiCode == 1 ? 0 : 1 );
1957  }
1958}
1959/*!
1960 ****************************************************************************
1961 * \brief
1962 *   estimate bit cost for CBP, significant map and significant coefficients
1963 ****************************************************************************
1964 */
1965Void TEncSbac::estBit( estBitsSbacStruct* pcEstBitsSbac, Int width, Int height, TextType eTType )
1966{
1967  estCBFBit( pcEstBitsSbac );
1968
1969  estSignificantCoeffGroupMapBit( pcEstBitsSbac, eTType );
1970 
1971  // encode significance map
1972  estSignificantMapBit( pcEstBitsSbac, width, height, eTType );
1973 
1974  // encode significant coefficients
1975  estSignificantCoefficientsBit( pcEstBitsSbac, eTType );
1976}
1977
1978/*!
1979 ****************************************************************************
1980 * \brief
1981 *    estimate bit cost for each CBP bit
1982 ****************************************************************************
1983 */
1984Void TEncSbac::estCBFBit( estBitsSbacStruct* pcEstBitsSbac )
1985{
1986  ContextModel *pCtx = m_cCUQtCbfSCModel.get( 0 );
1987
1988  for( UInt uiCtxInc = 0; uiCtxInc < 3*NUM_QT_CBF_CTX; uiCtxInc++ )
1989  {
1990    pcEstBitsSbac->blockCbpBits[ uiCtxInc ][ 0 ] = pCtx[ uiCtxInc ].getEntropyBits( 0 );
1991    pcEstBitsSbac->blockCbpBits[ uiCtxInc ][ 1 ] = pCtx[ uiCtxInc ].getEntropyBits( 1 );
1992  }
1993
1994  pCtx = m_cCUQtRootCbfSCModel.get( 0 );
1995 
1996  for( UInt uiCtxInc = 0; uiCtxInc < 4; uiCtxInc++ )
1997  {
1998    pcEstBitsSbac->blockRootCbpBits[ uiCtxInc ][ 0 ] = pCtx[ uiCtxInc ].getEntropyBits( 0 );
1999    pcEstBitsSbac->blockRootCbpBits[ uiCtxInc ][ 1 ] = pCtx[ uiCtxInc ].getEntropyBits( 1 );
2000  }
2001}
2002
2003
2004/*!
2005 ****************************************************************************
2006 * \brief
2007 *    estimate SAMBAC bit cost for significant coefficient group map
2008 ****************************************************************************
2009 */
2010Void TEncSbac::estSignificantCoeffGroupMapBit( estBitsSbacStruct* pcEstBitsSbac, TextType eTType )
2011{
2012  Int firstCtx = 0, numCtx = NUM_SIG_CG_FLAG_CTX;
2013
2014  for ( Int ctxIdx = firstCtx; ctxIdx < firstCtx + numCtx; ctxIdx++ )
2015  {
2016    for( UInt uiBin = 0; uiBin < 2; uiBin++ )
2017    {
2018      pcEstBitsSbac->significantCoeffGroupBits[ ctxIdx ][ uiBin ] = m_cCUSigCoeffGroupSCModel.get(  0, eTType, ctxIdx ).getEntropyBits( uiBin );
2019    }
2020  }
2021}
2022
2023
2024/*!
2025 ****************************************************************************
2026 * \brief
2027 *    estimate SAMBAC bit cost for significant coefficient map
2028 ****************************************************************************
2029 */
2030Void TEncSbac::estSignificantMapBit( estBitsSbacStruct* pcEstBitsSbac, Int width, Int height, TextType eTType )
2031{
2032  Int firstCtx = 1, numCtx = 8;
2033  if (max(width, height) >= 16)
2034  {
2035    firstCtx = (eTType == TEXT_LUMA) ? 21 : 12;
2036    numCtx = (eTType == TEXT_LUMA) ? 6 : 3;
2037  }
2038  else if (width == 8)
2039  {
2040    firstCtx = 9;
2041    numCtx = (eTType == TEXT_LUMA) ? 12 : 3;
2042  }
2043 
2044  if (eTType == TEXT_LUMA )
2045  {
2046    for( UInt bin = 0; bin < 2; bin++ )
2047    {
2048      pcEstBitsSbac->significantBits[ 0 ][ bin ] = m_cCUSigSCModel.get(  0, 0, 0 ).getEntropyBits( bin );
2049    }
2050
2051    for ( Int ctxIdx = firstCtx; ctxIdx < firstCtx + numCtx; ctxIdx++ )
2052    {
2053      for( UInt uiBin = 0; uiBin < 2; uiBin++ )
2054      {
2055        pcEstBitsSbac->significantBits[ ctxIdx ][ uiBin ] = m_cCUSigSCModel.get(  0, 0, ctxIdx ).getEntropyBits( uiBin );
2056      }
2057    }
2058  }
2059  else
2060  {
2061    for( UInt bin = 0; bin < 2; bin++ )
2062    {
2063      pcEstBitsSbac->significantBits[ 0 ][ bin ] = m_cCUSigSCModel.get(  0, 0, NUM_SIG_FLAG_CTX_LUMA + 0 ).getEntropyBits( bin );
2064    }
2065    for ( Int ctxIdx = firstCtx; ctxIdx < firstCtx + numCtx; ctxIdx++ )
2066    {
2067      for( UInt uiBin = 0; uiBin < 2; uiBin++ )
2068      {
2069        pcEstBitsSbac->significantBits[ ctxIdx ][ uiBin ] = m_cCUSigSCModel.get(  0, 0, NUM_SIG_FLAG_CTX_LUMA + ctxIdx ).getEntropyBits( uiBin );
2070      }
2071    }
2072  }
2073  Int iBitsX = 0, iBitsY = 0;
2074  Int blkSizeOffsetX, blkSizeOffsetY, shiftX, shiftY;
2075
2076  blkSizeOffsetX = eTType ? 0: (g_aucConvertToBit[ width ] *3 + ((g_aucConvertToBit[ width ] +1)>>2));
2077  blkSizeOffsetY = eTType ? 0: (g_aucConvertToBit[ height ]*3 + ((g_aucConvertToBit[ height ]+1)>>2));
2078  shiftX = eTType ? g_aucConvertToBit[ width  ] :((g_aucConvertToBit[ width  ]+3)>>2);
2079  shiftY = eTType ? g_aucConvertToBit[ height ] :((g_aucConvertToBit[ height ]+3)>>2);
2080
2081  Int ctx;
2082  ContextModel *pCtxX      = m_cCuCtxLastX.get( 0, eTType );
2083  for (ctx = 0; ctx < g_uiGroupIdx[ width - 1 ]; ctx++)
2084  {
2085    Int ctxOffset = blkSizeOffsetX + (ctx >>shiftX);
2086    pcEstBitsSbac->lastXBits[ ctx ] = iBitsX + pCtxX[ ctxOffset ].getEntropyBits( 0 );
2087    iBitsX += pCtxX[ ctxOffset ].getEntropyBits( 1 );
2088  }
2089  pcEstBitsSbac->lastXBits[ctx] = iBitsX;
2090  ContextModel *pCtxY      = m_cCuCtxLastY.get( 0, eTType );
2091  for (ctx = 0; ctx < g_uiGroupIdx[ height - 1 ]; ctx++)
2092  {
2093    Int ctxOffset = blkSizeOffsetY + (ctx >>shiftY);
2094    pcEstBitsSbac->lastYBits[ ctx ] = iBitsY + pCtxY[ ctxOffset ].getEntropyBits( 0 );
2095    iBitsY += pCtxY[ ctxOffset ].getEntropyBits( 1 );
2096  }
2097  pcEstBitsSbac->lastYBits[ctx] = iBitsY;
2098}
2099
2100/*!
2101 ****************************************************************************
2102 * \brief
2103 *    estimate bit cost of significant coefficient
2104 ****************************************************************************
2105 */
2106Void TEncSbac::estSignificantCoefficientsBit( estBitsSbacStruct* pcEstBitsSbac, TextType eTType )
2107{
2108  if (eTType==TEXT_LUMA)
2109  {
2110    ContextModel *ctxOne = m_cCUOneSCModel.get(0, 0);
2111    ContextModel *ctxAbs = m_cCUAbsSCModel.get(0, 0);
2112
2113    for (Int ctxIdx = 0; ctxIdx < NUM_ONE_FLAG_CTX_LUMA; ctxIdx++)
2114    {
2115      pcEstBitsSbac->m_greaterOneBits[ ctxIdx ][ 0 ] = ctxOne[ ctxIdx ].getEntropyBits( 0 );
2116      pcEstBitsSbac->m_greaterOneBits[ ctxIdx ][ 1 ] = ctxOne[ ctxIdx ].getEntropyBits( 1 );   
2117    }
2118
2119    for (Int ctxIdx = 0; ctxIdx < NUM_ABS_FLAG_CTX_LUMA; ctxIdx++)
2120    {
2121      pcEstBitsSbac->m_levelAbsBits[ ctxIdx ][ 0 ] = ctxAbs[ ctxIdx ].getEntropyBits( 0 );
2122      pcEstBitsSbac->m_levelAbsBits[ ctxIdx ][ 1 ] = ctxAbs[ ctxIdx ].getEntropyBits( 1 );   
2123    }
2124  }
2125  else
2126  {
2127    ContextModel *ctxOne = m_cCUOneSCModel.get(0, 0) + NUM_ONE_FLAG_CTX_LUMA;
2128    ContextModel *ctxAbs = m_cCUAbsSCModel.get(0, 0) + NUM_ABS_FLAG_CTX_LUMA;
2129
2130    for (Int ctxIdx = 0; ctxIdx < NUM_ONE_FLAG_CTX_CHROMA; ctxIdx++)
2131    {
2132      pcEstBitsSbac->m_greaterOneBits[ ctxIdx ][ 0 ] = ctxOne[ ctxIdx ].getEntropyBits( 0 );
2133      pcEstBitsSbac->m_greaterOneBits[ ctxIdx ][ 1 ] = ctxOne[ ctxIdx ].getEntropyBits( 1 );   
2134    }
2135
2136    for (Int ctxIdx = 0; ctxIdx < NUM_ABS_FLAG_CTX_CHROMA; ctxIdx++)
2137    {
2138      pcEstBitsSbac->m_levelAbsBits[ ctxIdx ][ 0 ] = ctxAbs[ ctxIdx ].getEntropyBits( 0 );
2139      pcEstBitsSbac->m_levelAbsBits[ ctxIdx ][ 1 ] = ctxAbs[ ctxIdx ].getEntropyBits( 1 );   
2140    }
2141  }
2142}
2143
2144/**
2145 - Initialize our context information from the nominated source.
2146 .
2147 \param pSrc From where to copy context information.
2148 */
2149Void TEncSbac::xCopyContextsFrom( TEncSbac* pSrc )
2150{ 
2151  memcpy(m_contextModels, pSrc->m_contextModels, m_numContextModels*sizeof(m_contextModels[0]));
2152}
2153
2154Void  TEncSbac::loadContexts ( TEncSbac* pScr)
2155{
2156  this->xCopyContextsFrom(pScr);
2157}
2158
2159Void TEncSbac::codeSAOOffsetParam(Int compIdx, SAOOffset& ctbParam, Bool sliceEnabled)
2160{
2161  UInt uiSymbol;
2162  if(!sliceEnabled)
2163  {
2164    assert(ctbParam.modeIdc == SAO_MODE_OFF);
2165    return;
2166  }
2167
2168  //type
2169  if(compIdx == SAO_Y || compIdx == SAO_Cb)
2170  {
2171    //sao_type_idx_luma or sao_type_idx_chroma
2172    if(ctbParam.modeIdc == SAO_MODE_OFF)
2173    {
2174      uiSymbol =0;
2175    }
2176    else if(ctbParam.typeIdc == SAO_TYPE_BO) //BO
2177    {
2178      uiSymbol = 1;
2179    }
2180    else
2181    {
2182      assert(ctbParam.typeIdc < SAO_TYPE_START_BO); //EO
2183      uiSymbol = 2;
2184    }
2185    codeSaoTypeIdx(uiSymbol); 
2186  }
2187
2188  if(ctbParam.modeIdc == SAO_MODE_NEW)
2189  {
2190    Int numClasses = (ctbParam.typeIdc == SAO_TYPE_BO)?4:NUM_SAO_EO_CLASSES; 
2191    Int offset[4];
2192    Int k=0;
2193    for(Int i=0; i< numClasses; i++)
2194    {
2195      if(ctbParam.typeIdc != SAO_TYPE_BO && i == SAO_CLASS_EO_PLAIN)
2196      {
2197        continue;
2198      }
2199      Int classIdx = (ctbParam.typeIdc == SAO_TYPE_BO)?(  (ctbParam.typeAuxInfo+i)% NUM_SAO_BO_CLASSES   ):i;
2200      offset[k] = ctbParam.offset[classIdx];
2201      k++;
2202    }
2203
2204    for(Int i=0; i< 4; i++)
2205    {
2206      codeSaoMaxUvlc((offset[i]<0)?(-offset[i]):(offset[i]),  g_saoMaxOffsetQVal[compIdx] ); //sao_offset_abs
2207    }
2208
2209
2210    if(ctbParam.typeIdc == SAO_TYPE_BO)
2211    {
2212      for(Int i=0; i< 4; i++)
2213      {
2214        if(offset[i] != 0)
2215        {
2216          codeSAOSign((offset[i]< 0)?1:0);
2217        }
2218      }
2219
2220      codeSaoUflc(NUM_SAO_BO_CLASSES_LOG2, ctbParam.typeAuxInfo ); //sao_band_position
2221    }
2222    else //EO
2223    {
2224      if(compIdx == SAO_Y || compIdx == SAO_Cb)
2225      {
2226        assert(ctbParam.typeIdc - SAO_TYPE_START_EO >=0);
2227        codeSaoUflc(NUM_SAO_EO_TYPES_LOG2, ctbParam.typeIdc - SAO_TYPE_START_EO ); //sao_eo_class_luma or sao_eo_class_chroma
2228      }
2229    }
2230
2231  }
2232}
2233
2234
2235Void TEncSbac::codeSAOBlkParam(SAOBlkParam& saoBlkParam
2236                              , Bool* sliceEnabled
2237                              , Bool leftMergeAvail
2238                              , Bool aboveMergeAvail
2239                              , Bool onlyEstMergeInfo // = false
2240                              )
2241{
2242
2243  Bool isLeftMerge = false;
2244  Bool isAboveMerge= false;
2245
2246  if(leftMergeAvail)
2247  {
2248    isLeftMerge = ((saoBlkParam[SAO_Y].modeIdc == SAO_MODE_MERGE) && (saoBlkParam[SAO_Y].typeIdc == SAO_MERGE_LEFT));
2249    codeSaoMerge( isLeftMerge?1:0  ); //sao_merge_left_flag
2250  }
2251
2252  if( aboveMergeAvail && !isLeftMerge)
2253  {
2254    isAboveMerge = ((saoBlkParam[SAO_Y].modeIdc == SAO_MODE_MERGE) && (saoBlkParam[SAO_Y].typeIdc == SAO_MERGE_ABOVE)); 
2255    codeSaoMerge( isAboveMerge?1:0  ); //sao_merge_left_flag
2256  }
2257
2258  if(onlyEstMergeInfo)
2259  {
2260    return; //only for RDO
2261  }
2262
2263  if(!isLeftMerge && !isAboveMerge) //not merge mode
2264  {
2265    for(Int compIdx=0; compIdx < NUM_SAO_COMPONENTS; compIdx++)
2266    {
2267      codeSAOOffsetParam(compIdx, saoBlkParam[compIdx], sliceEnabled[compIdx]);
2268    }
2269  }
2270}
2271
2272#if H_3D_INTER_SDC
2273Void TEncSbac::codeDeltaDC( TComDataCU* pcCU, UInt absPartIdx )
2274{
2275  if( !( pcCU->getSDCFlag( absPartIdx ) || ( pcCU->isIntra( absPartIdx ) && getDimType( pcCU->getLumaIntraDir( absPartIdx ) ) < DIM_NUM_TYPE ) ) )
2276  {
2277    assert( 0 );
2278  }
2279
2280  UInt uiNumSegments = 0;
2281  UInt dimDeltaDC = 0;
2282
2283  if( pcCU->isIntra( absPartIdx ) )
2284  {
2285    UInt dir     = pcCU->getLumaIntraDir( absPartIdx );
2286    uiNumSegments = isDimMode( dir ) ? 2 : 1;
2287
2288    if( pcCU->getSDCFlag( absPartIdx ) )
2289    {
2290      if( uiNumSegments == 1 )
2291      {
2292        dimDeltaDC = pcCU->getSDCSegmentDCOffset( 0, absPartIdx ) ? 1 : 0;
2293      }
2294      else
2295      {
2296        dimDeltaDC = ( pcCU->getSDCSegmentDCOffset( 0, absPartIdx ) || pcCU->getSDCSegmentDCOffset( 1, absPartIdx ) ) ? 1 : 0;
2297      }
2298    }
2299    else
2300    {
2301      dimDeltaDC = isDimDeltaDC( dir );
2302    }
2303    m_pcBinIf->encodeBin( dimDeltaDC, m_cDdcFlagSCModel.get( 0, 0, 0 ) );
2304  }
2305  else //all-zero inter SDC is not allowed
2306  {
2307    uiNumSegments = 1;
2308    dimDeltaDC = 1;
2309  }
2310
2311  if( dimDeltaDC )
2312  {
2313    for( UInt segment = 0; segment < uiNumSegments; segment++ )
2314    {
2315      Pel deltaDC = 0;
2316
2317      if( pcCU->isIntra( absPartIdx ) )
2318      {
2319        UInt dir     = pcCU->getLumaIntraDir( absPartIdx );
2320        deltaDC = pcCU->getSDCFlag( absPartIdx ) ? pcCU->getSDCSegmentDCOffset( segment, absPartIdx ) : pcCU->getDimDeltaDC( getDimType( dir ), segment, absPartIdx );
2321      }
2322      else
2323      {
2324        deltaDC = pcCU->getSDCSegmentDCOffset( segment, absPartIdx );
2325      }
2326
2327      xCodeDimDeltaDC( deltaDC, uiNumSegments );
2328    }
2329  }
2330}
2331
2332Void TEncSbac::codeSDCFlag( TComDataCU* pcCU, UInt uiAbsPartIdx )
2333{
2334  UInt uiSymbol = pcCU->getSDCFlag( uiAbsPartIdx ) ? 1 : 0;
2335  UInt uiCtxSDCFlag = pcCU->getCtxSDCFlag( uiAbsPartIdx );
2336
2337  m_pcBinIf->encodeBin( uiSymbol, m_cSDCFlagSCModel.get( 0, 0, uiCtxSDCFlag ) );
2338}
2339
2340#endif
2341
2342#if H_3D_DBBP
2343Void TEncSbac::codeDBBPFlag( TComDataCU* pcCU, UInt uiAbsPartIdx )
2344{
2345  AOF( pcCU->getSlice()->getVPS()->getUseDBBP(pcCU->getSlice()->getLayerIdInVps()) );
2346  AOF( !pcCU->getSlice()->getIsDepth() );
2347 
2348  UInt uiSymbol = pcCU->getDBBPFlag( uiAbsPartIdx ) ? 1 : 0;
2349  m_pcBinIf->encodeBin( uiSymbol, m_cDBBPFlagSCModel.get( 0, 0, 0 ) );
2350}
2351#endif
2352
2353//! \}
Note: See TracBrowser for help on using the repository browser.