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

Last change on this file since 521 was 521, checked in by tech, 11 years ago

Integrated following changes:

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