source: 3DVCSoftware/trunk/source/Lib/TLibEncoder/TEncSlice.cpp @ 1404

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

Merged HTM-16.0-dev1@1395.

  • Property svn:eol-style set to native
File size: 53.3 KB
RevLine 
[5]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
[1313]4 * granted under this license.
[5]5 *
[1313]6 * Copyright (c) 2010-2015, ITU/ISO/IEC
[5]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.
[56]17 *  * Neither the name of the ITU/ISO/IEC nor the names of its contributors may
[5]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 */
[2]33
34/** \file     TEncSlice.cpp
35    \brief    slice encoder class
36*/
37
38#include "TEncTop.h"
39#include "TEncSlice.h"
[56]40#include <math.h>
[2]41
[56]42//! \ingroup TLibEncoder
43//! \{
44
[2]45// ====================================================================================================================
46// Constructor / destructor / create / destroy
47// ====================================================================================================================
48TEncSlice::TEncSlice()
[1313]49 : m_encCABACTableIdx(I_SLICE)
[2]50{
51  m_apcPicYuvPred = NULL;
52  m_apcPicYuvResi = NULL;
[1313]53
[2]54  m_pdRdPicLambda = NULL;
55  m_pdRdPicQp     = NULL;
56  m_piRdPicQp     = NULL;
57}
58
59TEncSlice::~TEncSlice()
60{
61}
62
[1313]63Void TEncSlice::create( Int iWidth, Int iHeight, ChromaFormat chromaFormat, UInt iMaxCUWidth, UInt iMaxCUHeight, UChar uhTotalDepth )
[2]64{
65  // create prediction picture
66  if ( m_apcPicYuvPred == NULL )
67  {
68    m_apcPicYuvPred  = new TComPicYuv;
[1313]69    m_apcPicYuvPred->create( iWidth, iHeight, chromaFormat, iMaxCUWidth, iMaxCUHeight, uhTotalDepth, true );
[2]70  }
[1313]71
[2]72  // create residual picture
73  if( m_apcPicYuvResi == NULL )
74  {
75    m_apcPicYuvResi  = new TComPicYuv;
[1313]76    m_apcPicYuvResi->create( iWidth, iHeight, chromaFormat, iMaxCUWidth, iMaxCUHeight, uhTotalDepth, true );
[2]77  }
78}
79
80Void TEncSlice::destroy()
81{
82  // destroy prediction picture
83  if ( m_apcPicYuvPred )
84  {
85    m_apcPicYuvPred->destroy();
86    delete m_apcPicYuvPred;
87    m_apcPicYuvPred  = NULL;
88  }
[1313]89
[2]90  // destroy residual picture
91  if ( m_apcPicYuvResi )
92  {
93    m_apcPicYuvResi->destroy();
94    delete m_apcPicYuvResi;
95    m_apcPicYuvResi  = NULL;
96  }
[1313]97
[2]98  // free lambda and QP arrays
[1313]99  if ( m_pdRdPicLambda )
[56]100  {
[1313]101    xFree( m_pdRdPicLambda );
102    m_pdRdPicLambda = NULL;
[56]103  }
[1313]104  if ( m_pdRdPicQp )
[56]105  {
[1313]106    xFree( m_pdRdPicQp );
107    m_pdRdPicQp = NULL;
[56]108  }
[1313]109  if ( m_piRdPicQp )
110  {
111    xFree( m_piRdPicQp );
112    m_piRdPicQp = NULL;
113  }
[2]114}
115
116Void TEncSlice::init( TEncTop* pcEncTop )
117{
118  m_pcCfg             = pcEncTop;
119  m_pcListPic         = pcEncTop->getListPic();
[1313]120
[56]121  m_pcGOPEncoder      = pcEncTop->getGOPEncoder();
[2]122  m_pcCuEncoder       = pcEncTop->getCuEncoder();
123  m_pcPredSearch      = pcEncTop->getPredSearch();
[1313]124
[2]125  m_pcEntropyCoder    = pcEncTop->getEntropyCoder();
126  m_pcSbacCoder       = pcEncTop->getSbacCoder();
127  m_pcBinCABAC        = pcEncTop->getBinCABAC();
128  m_pcTrQuant         = pcEncTop->getTrQuant();
[1313]129
[2]130  m_pcRdCost          = pcEncTop->getRdCost();
131  m_pppcRDSbacCoder   = pcEncTop->getRDSbacCoder();
132  m_pcRDGoOnSbacCoder = pcEncTop->getRDGoOnSbacCoder();
[1313]133
[2]134  // create lambda and QP arrays
135  m_pdRdPicLambda     = (Double*)xMalloc( Double, m_pcCfg->getDeltaQpRD() * 2 + 1 );
136  m_pdRdPicQp         = (Double*)xMalloc( Double, m_pcCfg->getDeltaQpRD() * 2 + 1 );
137  m_piRdPicQp         = (Int*   )xMalloc( Int,    m_pcCfg->getDeltaQpRD() * 2 + 1 );
[655]138#if KWU_RC_MADPRED_E0227
139  if(m_pcCfg->getUseRateCtrl())
140  {
141    m_pcRateCtrl        = pcEncTop->getRateCtrl();
142  }
143  else
144  {
145    m_pcRateCtrl        = NULL;
146  }
147#else
[608]148  m_pcRateCtrl        = pcEncTop->getRateCtrl();
[655]149#endif
[1313]150
[2]151}
152
[1313]153
154
155Void
156TEncSlice::setUpLambda(TComSlice* slice, const Double dLambda, Int iQP)
157{
158  // store lambda
159  m_pcRdCost ->setLambda( dLambda, slice->getSPS()->getBitDepths() );
160
161  // for RDO
162  // in RdCost there is only one lambda because the luma and chroma bits are not separated, instead we weight the distortion of chroma.
163  Double dLambdas[MAX_NUM_COMPONENT] = { dLambda };
164  for(UInt compIdx=1; compIdx<MAX_NUM_COMPONENT; compIdx++)
165  {
166    const ComponentID compID=ComponentID(compIdx);
167    Int chromaQPOffset = slice->getPPS()->getQpOffset(compID) + slice->getSliceChromaQpDelta(compID);
168    Int qpc=(iQP + chromaQPOffset < 0) ? iQP : getScaledChromaQP(iQP + chromaQPOffset, m_pcCfg->getChromaFormatIdc());
169    Double tmpWeight = pow( 2.0, (iQP-qpc)/3.0 );  // takes into account of the chroma qp mapping and chroma qp Offset
170    m_pcRdCost->setDistortionWeight(compID, tmpWeight);
171    dLambdas[compIdx]=dLambda/tmpWeight;
172  }
173
174#if RDOQ_CHROMA_LAMBDA
175// for RDOQ
176  m_pcTrQuant->setLambdas( dLambdas );
177#else
178  m_pcTrQuant->setLambda( dLambda );
179#endif
180
181// For SAO
182  slice   ->setLambdas( dLambdas );
183}
184
185
186
[2]187/**
188 - non-referenced frame marking
189 - QP computation based on temporal structure
190 - lambda computation based on QP
[56]191 - set temporal layer ID and the parameter sets
[2]192 .
193 \param pcPic         picture class
[608]194 \param pocLast       POC of last picture
195 \param pocCurr       current POC
[2]196 \param iNumPicRcvd   number of received pictures
[1313]197 \param iGOPid        POC offset for hierarchical structure
[2]198 \param rpcSlice      slice header class
[1313]199 \param isField       true for field coding
[2]200 */
[1313]201
202#if NH_MV
203Void TEncSlice::initEncSlice( TComPic* pcPic, Int pocLast, Int pocCurr, Int iGOPid, TComSlice*& rpcSlice, TComVPS* pVPS, Int layerId, bool isField )
[622]204#else
[1386]205Void TEncSlice::initEncSlice( TComPic* pcPic, const Int pocLast, const Int pocCurr, const Int iGOPid, TComSlice*& rpcSlice, const Bool isField )
[622]206#endif
[2]207{
208  Double dQP;
209  Double dLambda;
[1313]210
[2]211  rpcSlice = pcPic->getSlice(0);
[622]212
[1313]213#if NH_MV
[622]214  rpcSlice->setVPS( pVPS ); 
215
216  rpcSlice->setLayerId     ( layerId );
217  rpcSlice->setViewId      ( pVPS->getViewId      ( layerId ) );   
218  rpcSlice->setViewIndex   ( pVPS->getViewIndex   ( layerId ) );
[1313]219#if NH_3D
[622]220  rpcSlice->setIsDepth     ( pVPS->getDepthId     ( layerId ) != 0 );   
221#endif
[77]222#endif
[2]223  rpcSlice->setSliceBits(0);
224  rpcSlice->setPic( pcPic );
225  rpcSlice->initSlice();
[56]226  rpcSlice->setPicOutputFlag( true );
[608]227  rpcSlice->setPOC( pocCurr );
[1313]228#if NH_3D_IC
[608]229  rpcSlice->setApplyIC( false );
[189]230#endif
[56]231  // depth computation based on GOP size
[608]232  Int depth;
[56]233  {
[1313]234
[964]235    Int poc = rpcSlice->getPOC();
236    if(isField)
237    {
[1313]238      poc = (poc/2) % (m_pcCfg->getGOPSize()/2);
[964]239    }
240    else
241    {
[1313]242      poc = poc % m_pcCfg->getGOPSize();   
[964]243    }
[608]244    if ( poc == 0 )
[56]245    {
[608]246      depth = 0;
[56]247    }
248    else
249    {
[608]250      Int step = m_pcCfg->getGOPSize();
251      depth    = 0;
252      for( Int i=step>>1; i>=1; i>>=1 )
[56]253      {
[608]254        for ( Int j=i; j<m_pcCfg->getGOPSize(); j+=step )
[56]255        {
[608]256          if ( j == poc )
[56]257          {
258            i=0;
259            break;
260          }
261        }
[608]262        step >>= 1;
263        depth++;
[56]264      }
265    }
[1313]266
267    if(m_pcCfg->getHarmonizeGopFirstFieldCoupleEnabled() && poc != 0)
[964]268    {
[1313]269      if (isField && ((rpcSlice->getPOC() % 2) == 1))
270      {
271        depth ++;
272      }
[964]273    }
[56]274  }
[1313]275
[2]276  // slice type
[1313]277#if NH_MV
[56]278  SliceType eSliceTypeBaseView;
[608]279  if( pocLast == 0 || pocCurr % m_pcCfg->getIntraPeriod() == 0 || m_pcGOPEncoder->getGOPSize() == 0 )
[56]280  {
281    eSliceTypeBaseView = I_SLICE;
282  }
283  else
284  {
285    eSliceTypeBaseView = B_SLICE;
286  }
287  SliceType eSliceType = eSliceTypeBaseView;
288  if( eSliceTypeBaseView == I_SLICE && m_pcCfg->getGOPEntry(MAX_GOP).m_POC == 0 && m_pcCfg->getGOPEntry(MAX_GOP).m_sliceType != 'I' )
289  {
290    eSliceType = B_SLICE; 
291  }
[608]292#else
293  SliceType eSliceType;
[1313]294
[608]295  eSliceType=B_SLICE;
[1313]296  if(!(isField && pocLast == 1) || !m_pcCfg->getEfficientFieldIRAPEnabled())
[964]297  {
[1313]298    if(m_pcCfg->getDecodingRefreshType() == 3)
299    {
300      eSliceType = (pocLast == 0 || pocCurr % m_pcCfg->getIntraPeriod() == 0             || m_pcGOPEncoder->getGOPSize() == 0) ? I_SLICE : eSliceType;
301    }
302    else
303    {
304      eSliceType = (pocLast == 0 || (pocCurr - (isField ? 1 : 0)) % m_pcCfg->getIntraPeriod() == 0 || m_pcGOPEncoder->getGOPSize() == 0) ? I_SLICE : eSliceType;
305    }
[964]306  }
307#endif
[608]308  rpcSlice->setSliceType    ( eSliceType );
309
[2]310  // ------------------------------------------------------------------------------------------------------------------
311  // Non-referenced frame marking
312  // ------------------------------------------------------------------------------------------------------------------
[1313]313
[608]314  if(pocLast == 0)
[56]315  {
[608]316    rpcSlice->setTemporalLayerNonReferenceFlag(false);
[56]317  }
[608]318  else
319  {
[1386]320#if NH_MV
[1313]321#if 0 // Check this! NH_MV
[608]322    rpcSlice->setTemporalLayerNonReferenceFlag(!m_pcCfg->getGOPEntry( (eSliceTypeBaseView == I_SLICE) ? MAX_GOP : iGOPid ).m_refPic);
323#else
324    rpcSlice->setTemporalLayerNonReferenceFlag(!m_pcCfg->getGOPEntry(iGOPid).m_refPic);
325#endif
[1386]326#else
327    rpcSlice->setTemporalLayerNonReferenceFlag(!m_pcCfg->getGOPEntry(iGOPid).m_refPic);
328#endif
[608]329  }
330  rpcSlice->setReferenced(true);
[1313]331
[2]332  // ------------------------------------------------------------------------------------------------------------------
333  // QP setting
334  // ------------------------------------------------------------------------------------------------------------------
[1313]335
[56]336  dQP = m_pcCfg->getQP();
[608]337  if(eSliceType!=I_SLICE)
[56]338  {
[1313]339    if (!(( m_pcCfg->getMaxDeltaQP() == 0 ) && (dQP == -rpcSlice->getSPS()->getQpBDOffset(CHANNEL_TYPE_LUMA) ) && (rpcSlice->getPPS()->getTransquantBypassEnableFlag())))
[608]340    {
[1313]341#if NH_MV
[608]342      dQP += m_pcCfg->getGOPEntry( (eSliceTypeBaseView == I_SLICE) ? MAX_GOP : iGOPid ).m_QPOffset;
343#else
344      dQP += m_pcCfg->getGOPEntry(iGOPid).m_QPOffset;
[56]345#endif
346    }
347  }
[1313]348
[56]349  // modify QP
350  Int* pdQPs = m_pcCfg->getdQPs();
351  if ( pdQPs )
352  {
353    dQP += pdQPs[ rpcSlice->getPOC() ];
354  }
[1313]355
356  if (m_pcCfg->getCostMode()==COST_LOSSLESS_CODING)
357  {
358    dQP=LOSSLESS_AND_MIXED_LOSSLESS_RD_COST_TEST_QP;
359    m_pcCfg->setDeltaQpRD(0);
360  }
361
[2]362  // ------------------------------------------------------------------------------------------------------------------
363  // Lambda computation
364  // ------------------------------------------------------------------------------------------------------------------
[1313]365
[2]366  Int iQP;
[56]367  Double dOrigQP = dQP;
[2]368
369  // pre-compute lambda and QP values for all possible QP candidates
370  for ( Int iDQpIdx = 0; iDQpIdx < 2 * m_pcCfg->getDeltaQpRD() + 1; iDQpIdx++ )
371  {
372    // compute QP value
373    dQP = dOrigQP + ((iDQpIdx+1)>>1)*(iDQpIdx%2 ? -1 : 1);
[1313]374
[2]375    // compute lambda value
[56]376    Int    NumberBFrames = ( m_pcCfg->getGOPSize() - 1 );
[2]377    Int    SHIFT_QP = 12;
[1313]378
[2]379#if FULL_NBIT
[1313]380    Int    bitdepth_luma_qp_scale = 6 * (rpcSlice->getSPS()->getBitDepth(CHANNEL_TYPE_LUMA) - 8);
[2]381#else
382    Int    bitdepth_luma_qp_scale = 0;
383#endif
[56]384    Double qp_temp = (Double) dQP + bitdepth_luma_qp_scale - SHIFT_QP;
[2]385#if FULL_NBIT
[56]386    Double qp_temp_orig = (Double) dQP - SHIFT_QP;
[2]387#endif
388    // Case #1: I or P-slices (key-frame)
[1313]389#if NH_MV
[56]390    Double dQPFactor;
391    if( eSliceType != I_SLICE ) 
[2]392    {
[56]393      dQPFactor = m_pcCfg->getGOPEntry( (eSliceTypeBaseView == I_SLICE) ? MAX_GOP : iGOPid ).m_QPFactor;
[2]394    }
[56]395    else
[608]396#else
397    Double dQPFactor = m_pcCfg->getGOPEntry(iGOPid).m_QPFactor;
398    if ( eSliceType==I_SLICE )
399#endif
[2]400    {
[1386]401      if (m_pcCfg->getIntraQpFactor()>=0.0 && m_pcCfg->getGOPEntry(iGOPid).m_sliceType != I_SLICE)
402      {
403        dQPFactor=m_pcCfg->getIntraQpFactor();
404      }
405      else
406      {
407        Double dLambda_scale = 1.0 - Clip3( 0.0, 0.5, 0.05*(Double)(isField ? NumberBFrames/2 : NumberBFrames) );
408       
409        dQPFactor=0.57*dLambda_scale;
410      }
[56]411    }
412    dLambda = dQPFactor*pow( 2.0, qp_temp/3.0 );
413
[608]414    if ( depth>0 )
[56]415    {
[2]416#if FULL_NBIT
417        dLambda *= Clip3( 2.00, 4.00, (qp_temp_orig / 6.0) ); // (j == B_SLICE && p_cur_frm->layer != 0 )
418#else
419        dLambda *= Clip3( 2.00, 4.00, (qp_temp / 6.0) ); // (j == B_SLICE && p_cur_frm->layer != 0 )
420#endif
421    }
[1313]422
[2]423    // if hadamard is used in ME process
[608]424    if ( !m_pcCfg->getUseHADME() && rpcSlice->getSliceType( ) != I_SLICE )
[56]425    {
426      dLambda *= 0.95;
427    }
[5]428
[1313]429    iQP = max( -rpcSlice->getSPS()->getQpBDOffset(CHANNEL_TYPE_LUMA), min( MAX_QP, (Int) floor( dQP + 0.5 ) ) );
430
[2]431    m_pdRdPicLambda[iDQpIdx] = dLambda;
432    m_pdRdPicQp    [iDQpIdx] = dQP;
433    m_piRdPicQp    [iDQpIdx] = iQP;
434  }
[1313]435
[2]436  // obtain dQP = 0 case
437  dLambda = m_pdRdPicLambda[0];
438  dQP     = m_pdRdPicQp    [0];
439  iQP     = m_piRdPicQp    [0];
[1313]440
[1386]441
[1313]442#if NH_MV
[1386]443  const Int temporalId=m_pcCfg->getGOPEntry((eSliceTypeBaseView == I_SLICE) ? MAX_GOP : iGOPid).m_temporalId;
[608]444#else
[1386]445  const Int temporalId=m_pcCfg->getGOPEntry(iGOPid).m_temporalId;
[608]446#endif
[1386]447  const std::vector<Double> &intraLambdaModifiers=m_pcCfg->getIntraLambdaModifier();
448
449  Double lambdaModifier;
450  if( rpcSlice->getSliceType( ) != I_SLICE || intraLambdaModifiers.empty())
451  {
452    lambdaModifier = m_pcCfg->getLambdaModifier( temporalId );
[56]453  }
[1386]454  else
455  {
456    lambdaModifier = intraLambdaModifiers[ (temporalId < intraLambdaModifiers.size()) ? temporalId : (intraLambdaModifiers.size()-1) ];
457  }
[5]458
[1386]459  dLambda *= lambdaModifier;
[1313]460  setUpLambda(rpcSlice, dLambda, iQP);
[608]461
[1313]462#if NH_3D_VSO
[1396]463  m_pcRdCost->setUseLambdaScaleVSO  ( (m_pcCfg->getUseVSO() ||  m_pcCfg->getForceLambdaScaleVSO()) && ( m_pcCfg->getIsDepth() || m_pcCfg->getIsAuxDepth() ) );
[608]464  m_pcRdCost->setLambdaVSO          ( dLambda * m_pcCfg->getLambdaScaleVSO() );
465
466  // Should be moved to TEncTop
467 
468  // SAIT_VSO_EST_A0033
469  m_pcRdCost->setDisparityCoeff( m_pcCfg->getDispCoeff() );
470
471  // LGE_WVSO_A0119
[1386]472  if( m_pcCfg->getUseWVSO() && ( m_pcCfg->getIsDepth() || m_pcCfg->getIsAuxDepth() ) )
[608]473  {
474    m_pcRdCost->setDWeight  ( m_pcCfg->getDWeight()   );
475    m_pcRdCost->setVSOWeight( m_pcCfg->getVSOWeight() );
476    m_pcRdCost->setVSDWeight( m_pcCfg->getVSDWeight() );
477  }
478
479#endif
480
[1313]481  if (m_pcCfg->getFastMEForGenBLowDelayEnabled())
482  {
[56]483  // restore original slice type
[1313]484#if NH_MV
[56]485  eSliceType = eSliceTypeBaseView;
486  if( eSliceTypeBaseView == I_SLICE && m_pcCfg->getGOPEntry(MAX_GOP).m_POC == 0 && m_pcCfg->getGOPEntry(MAX_GOP).m_sliceType != 'I' )
487  {
488    eSliceType = B_SLICE;
489  }
[608]490#else
[1313]491    if(!(isField && pocLast == 1) || !m_pcCfg->getEfficientFieldIRAPEnabled())
[964]492  {
[1313]493    if(m_pcCfg->getDecodingRefreshType() == 3)
494    {
495      eSliceType = (pocLast == 0 || (pocCurr)                     % m_pcCfg->getIntraPeriod() == 0 || m_pcGOPEncoder->getGOPSize() == 0) ? I_SLICE : eSliceType;
496    }
497    else
498    {
499      eSliceType = (pocLast == 0 || (pocCurr - (isField ? 1 : 0)) % m_pcCfg->getIntraPeriod() == 0 || m_pcGOPEncoder->getGOPSize() == 0) ? I_SLICE : eSliceType;
[964]500
[1313]501    }
[964]502  }
[56]503#endif
[608]504
505  rpcSlice->setSliceType        ( eSliceType );
[1313]506}
507
[608]508  if (m_pcCfg->getUseRecalculateQPAccordingToLambda())
509  {
510    dQP = xGetQPValueAccordingToLambda( dLambda );
[1313]511    iQP = max( -rpcSlice->getSPS()->getQpBDOffset(CHANNEL_TYPE_LUMA), min( MAX_QP, (Int) floor( dQP + 0.5 ) ) );
[608]512  }
513
[1313]514  rpcSlice->setSliceQp           ( iQP );
[56]515#if ADAPTIVE_QP_SELECTION
[1313]516  rpcSlice->setSliceQpBase       ( iQP );
[56]517#endif
[1313]518  rpcSlice->setSliceQpDelta      ( 0 );
519  rpcSlice->setSliceChromaQpDelta( COMPONENT_Cb, 0 );
520  rpcSlice->setSliceChromaQpDelta( COMPONENT_Cr, 0 );
521  rpcSlice->setUseChromaQpAdj( rpcSlice->getPPS()->getPpsRangeExtension().getChromaQpOffsetListEnabledFlag() );
522#if NH_MV
[56]523  rpcSlice->setNumRefIdx(REF_PIC_LIST_0,m_pcCfg->getGOPEntry( (eSliceTypeBaseView == I_SLICE) ? MAX_GOP : iGOPid ).m_numRefPicsActive);
524  rpcSlice->setNumRefIdx(REF_PIC_LIST_1,m_pcCfg->getGOPEntry( (eSliceTypeBaseView == I_SLICE) ? MAX_GOP : iGOPid ).m_numRefPicsActive);
[608]525#else
526  rpcSlice->setNumRefIdx(REF_PIC_LIST_0,m_pcCfg->getGOPEntry(iGOPid).m_numRefPicsActive);
527  rpcSlice->setNumRefIdx(REF_PIC_LIST_1,m_pcCfg->getGOPEntry(iGOPid).m_numRefPicsActive);
528#endif
529
530  if ( m_pcCfg->getDeblockingFilterMetric() )
[56]531  {
[608]532    rpcSlice->setDeblockingFilterOverrideFlag(true);
533    rpcSlice->setDeblockingFilterDisable(false);
534    rpcSlice->setDeblockingFilterBetaOffsetDiv2( 0 );
535    rpcSlice->setDeblockingFilterTcOffsetDiv2( 0 );
[1313]536  }
537  else if (rpcSlice->getPPS()->getDeblockingFilterControlPresentFlag())
[608]538  {
[1313]539    rpcSlice->setDeblockingFilterOverrideFlag( rpcSlice->getPPS()->getDeblockingFilterOverrideEnabledFlag() );
540    rpcSlice->setDeblockingFilterDisable( rpcSlice->getPPS()->getPicDisableDeblockingFilterFlag() );
[608]541    if ( !rpcSlice->getDeblockingFilterDisable())
[56]542    {
[1313]543      if ( rpcSlice->getDeblockingFilterOverrideFlag() && eSliceType!=I_SLICE)
[608]544      {
[1313]545#if NH_MV
[608]546        rpcSlice->setDeblockingFilterBetaOffsetDiv2( m_pcCfg->getGOPEntry((eSliceTypeBaseView == I_SLICE) ? MAX_GOP : iGOPid).m_betaOffsetDiv2 + m_pcCfg->getLoopFilterBetaOffset()  );
547        rpcSlice->setDeblockingFilterTcOffsetDiv2( m_pcCfg->getGOPEntry((eSliceTypeBaseView == I_SLICE) ? MAX_GOP : iGOPid).m_tcOffsetDiv2 + m_pcCfg->getLoopFilterTcOffset() );
548#else
549        rpcSlice->setDeblockingFilterBetaOffsetDiv2( m_pcCfg->getGOPEntry(iGOPid).m_betaOffsetDiv2 + m_pcCfg->getLoopFilterBetaOffset()  );
550        rpcSlice->setDeblockingFilterTcOffsetDiv2( m_pcCfg->getGOPEntry(iGOPid).m_tcOffsetDiv2 + m_pcCfg->getLoopFilterTcOffset() );
551#endif
552      }
553      else
554      {
[1313]555        rpcSlice->setDeblockingFilterBetaOffsetDiv2( m_pcCfg->getLoopFilterBetaOffset() );
556        rpcSlice->setDeblockingFilterTcOffsetDiv2( m_pcCfg->getLoopFilterTcOffset() );
[608]557      }
[56]558    }
559  }
[608]560  else
561  {
562    rpcSlice->setDeblockingFilterOverrideFlag( false );
563    rpcSlice->setDeblockingFilterDisable( false );
564    rpcSlice->setDeblockingFilterBetaOffsetDiv2( 0 );
565    rpcSlice->setDeblockingFilterTcOffsetDiv2( 0 );
566  }
[5]567
[608]568  rpcSlice->setDepth            ( depth );
[1313]569
[1386]570  pcPic->setTLayer( temporalId );
[608]571  if(eSliceType==I_SLICE)
[56]572  {
573    pcPic->setTLayer(0);
574  }
575  rpcSlice->setTLayer( pcPic->getTLayer() );
[5]576
[2]577  assert( m_apcPicYuvPred );
578  assert( m_apcPicYuvResi );
[1313]579
[2]580  pcPic->setPicYuvPred( m_apcPicYuvPred );
581  pcPic->setPicYuvResi( m_apcPicYuvResi );
582  rpcSlice->setSliceMode            ( m_pcCfg->getSliceMode()            );
583  rpcSlice->setSliceArgument        ( m_pcCfg->getSliceArgument()        );
[608]584  rpcSlice->setSliceSegmentMode     ( m_pcCfg->getSliceSegmentMode()     );
585  rpcSlice->setSliceSegmentArgument ( m_pcCfg->getSliceSegmentArgument() );
[1313]586#if NH_3D_IV_MERGE
587#else
[1179]588  rpcSlice->setMaxNumMergeCand        ( m_pcCfg->getMaxNumMergeCand()        );
589#endif
[608]590}
[56]591
[608]592Void TEncSlice::resetQP( TComPic* pic, Int sliceQP, Double lambda )
593{
594  TComSlice* slice = pic->getSlice(0);
595
596  // store lambda
597  slice->setSliceQp( sliceQP );
[964]598#if ADAPTIVE_QP_SELECTION
[608]599  slice->setSliceQpBase ( sliceQP );
[964]600#endif
[1313]601  setUpLambda(slice, lambda, sliceQP);
602}
[608]603
[2]604// ====================================================================================================================
605// Public member functions
606// ====================================================================================================================
607
[1386]608//! set adaptive search range based on poc difference
[2]609Void TEncSlice::setSearchRange( TComSlice* pcSlice )
610{
611  Int iCurrPOC = pcSlice->getPOC();
612  Int iRefPOC;
[56]613  Int iGOPSize = m_pcCfg->getGOPSize();
614  Int iOffset = (iGOPSize >> 1);
[2]615  Int iMaxSR = m_pcCfg->getSearchRange();
616  Int iNumPredDir = pcSlice->isInterP() ? 1 : 2;
[1313]617
[1386]618  for (Int iDir = 0; iDir < iNumPredDir; iDir++)
[2]619  {
[608]620    RefPicList  e = ( iDir ? REF_PIC_LIST_1 : REF_PIC_LIST_0 );
[2]621    for (Int iRefIdx = 0; iRefIdx < pcSlice->getNumRefIdx(e); iRefIdx++)
622    {
623      iRefPOC = pcSlice->getRefPic(e, iRefIdx)->getPOC();
[1386]624      Int newSearchRange = Clip3(m_pcCfg->getMinSearchWindow(), iMaxSR, (iMaxSR*ADAPT_SR_SCALE*abs(iCurrPOC - iRefPOC)+iOffset)/iGOPSize);
625      m_pcPredSearch->setAdaptiveSearchRange(iDir, iRefIdx, newSearchRange);
[2]626    }
627  }
628}
629
630/**
[1313]631 Multi-loop slice encoding for different slice QP
632
633 \param pcPic    picture class
[2]634 */
[1313]635Void TEncSlice::precompressSlice( TComPic* pcPic )
[2]636{
637  // if deltaQP RD is not used, simply return
[56]638  if ( m_pcCfg->getDeltaQpRD() == 0 )
639  {
640    return;
641  }
[608]642
643  if ( m_pcCfg->getUseRateCtrl() )
644  {
645    printf( "\nMultiple QP optimization is not allowed when rate control is enabled." );
646    assert(0);
[1313]647    return;
[608]648  }
[1313]649
650  TComSlice* pcSlice        = pcPic->getSlice(getSliceIdx());
651
652  if (pcSlice->getDependentSliceSegmentFlag())
653  {
654    // if this is a dependent slice segment, then it was optimised
655    // when analysing the entire slice.
656    return;
657  }
658
659  if (pcSlice->getSliceMode()==FIXED_NUMBER_OF_BYTES)
660  {
661    // TODO: investigate use of average cost per CTU so that this Slice Mode can be used.
662    printf( "\nUnable to optimise Slice-level QP if Slice Mode is set to FIXED_NUMBER_OF_BYTES\n" );
663    assert(0);
664    return;
665  }
666
667
[2]668  Double     dPicRdCostBest = MAX_DOUBLE;
669  UInt       uiQpIdxBest = 0;
[1313]670
[2]671  Double dFrameLambda;
672#if FULL_NBIT
[1313]673  Int    SHIFT_QP = 12 + 6 * (pcSlice->getSPS()->getBitDepth(CHANNEL_TYPE_LUMA) - 8);
[2]674#else
675  Int    SHIFT_QP = 12;
676#endif
[1313]677
[2]678  // set frame lambda
679  if (m_pcCfg->getGOPSize() > 1)
680  {
681    dFrameLambda = 0.68 * pow (2, (m_piRdPicQp[0]  - SHIFT_QP) / 3.0) * (pcSlice->isInterB()? 2 : 1);
682  }
683  else
684  {
685    dFrameLambda = 0.68 * pow (2, (m_piRdPicQp[0] - SHIFT_QP) / 3.0);
686  }
687  m_pcRdCost      ->setFrameLambda(dFrameLambda);
[1313]688
[2]689  // for each QP candidate
690  for ( UInt uiQpIdx = 0; uiQpIdx < 2 * m_pcCfg->getDeltaQpRD() + 1; uiQpIdx++ )
691  {
692    pcSlice       ->setSliceQp             ( m_piRdPicQp    [uiQpIdx] );
[56]693#if ADAPTIVE_QP_SELECTION
694    pcSlice       ->setSliceQpBase         ( m_piRdPicQp    [uiQpIdx] );
695#endif
[1313]696    setUpLambda(pcSlice, m_pdRdPicLambda[uiQpIdx], m_piRdPicQp    [uiQpIdx]);
[608]697
[1313]698    // try compress
699    compressSlice   ( pcPic, true, m_pcCfg->getFastDeltaQp());
[608]700
[1313]701#if NH_3D_VSO
[608]702    Dist64 uiPicDist        = m_uiPicDist;
703#else
[1313]704    UInt64 uiPicDist        = m_uiPicDist; // Distortion, as calculated by compressSlice.
705    // NOTE: This distortion is the chroma-weighted SSE distortion for the slice.
706    //       Previously a standard SSE distortion was calculated (for the entire frame).
707    //       Which is correct?
708
709    // TODO: Update loop filter, SAO and distortion calculation to work on one slice only.
710    // m_pcGOPEncoder->preLoopFilterPicAll( pcPic, uiPicDist );
711
[608]712#endif
[1313]713
[2]714    // compute RD cost and choose the best
[1386]715#if NH_3D_VSO
716    Double dPicRdCost = m_pcRdCost->calcRdCost( (Double)m_uiPicTotalBits, uiPicDist, DF_SSE_FRAME);
717#else
718    Double dPicRdCost = m_pcRdCost->calcRdCost( (Double)m_uiPicTotalBits, (Double)uiPicDist, DF_SSE_FRAME);
719#endif
[608]720#if H_3D
721    // Above calculation need to be fixed for VSO, including frameLambda value.
722#endif
[1313]723
[2]724    if ( dPicRdCost < dPicRdCostBest )
725    {
726      uiQpIdxBest    = uiQpIdx;
727      dPicRdCostBest = dPicRdCost;
728    }
729  }
[1313]730
[2]731  // set best values
732  pcSlice       ->setSliceQp             ( m_piRdPicQp    [uiQpIdxBest] );
[56]733#if ADAPTIVE_QP_SELECTION
734  pcSlice       ->setSliceQpBase         ( m_piRdPicQp    [uiQpIdxBest] );
735#endif
[1313]736  setUpLambda(pcSlice, m_pdRdPicLambda[uiQpIdxBest], m_piRdPicQp    [uiQpIdxBest]);
[2]737}
738
[1313]739Void TEncSlice::calCostSliceI(TComPic* pcPic) // TODO: this only analyses the first slice segment. What about the others?
[608]740{
[1313]741  Double            iSumHadSlice      = 0;
742  TComSlice * const pcSlice           = pcPic->getSlice(getSliceIdx());
743  const TComSPS    &sps               = *(pcSlice->getSPS());
744  const Int         shift             = sps.getBitDepth(CHANNEL_TYPE_LUMA)-8;
745  const Int         offset            = (shift>0)?(1<<(shift-1)):0;
[608]746
[1313]747  pcSlice->setSliceSegmentBits(0);
[608]748
[1313]749  UInt startCtuTsAddr, boundingCtuTsAddr;
750  xDetermineStartAndBoundingCtuTsAddr ( startCtuTsAddr, boundingCtuTsAddr, pcPic );
751
752  for( UInt ctuTsAddr = startCtuTsAddr, ctuRsAddr = pcPic->getPicSym()->getCtuTsToRsAddrMap( startCtuTsAddr);
753       ctuTsAddr < boundingCtuTsAddr;
754       ctuRsAddr = pcPic->getPicSym()->getCtuTsToRsAddrMap(++ctuTsAddr) )
[608]755  {
756    // initialize CU encoder
[1313]757    TComDataCU* pCtu = pcPic->getCtu( ctuRsAddr );
758    pCtu->initCtu( pcPic, ctuRsAddr );
[608]759
[1313]760    Int height  = min( sps.getMaxCUHeight(),sps.getPicHeightInLumaSamples() - ctuRsAddr / pcPic->getFrameWidthInCtus() * sps.getMaxCUHeight() );
761    Int width   = min( sps.getMaxCUWidth(), sps.getPicWidthInLumaSamples()  - ctuRsAddr % pcPic->getFrameWidthInCtus() * sps.getMaxCUWidth() );
[608]762
[1313]763    Int iSumHad = m_pcCuEncoder->updateCtuDataISlice(pCtu, width, height);
[608]764
[1313]765    (m_pcRateCtrl->getRCPic()->getLCU(ctuRsAddr)).m_costIntra=(iSumHad+offset)>>shift;
766    iSumHadSlice += (m_pcRateCtrl->getRCPic()->getLCU(ctuRsAddr)).m_costIntra;
[608]767
768  }
769  m_pcRateCtrl->getRCPic()->setTotalIntraCost(iSumHadSlice);
770}
771
[1313]772/** \param pcPic   picture class
773 */
774Void TEncSlice::compressSlice( TComPic* pcPic, const Bool bCompressEntireSlice, const Bool bFastDeltaQP )
[2]775{
[1313]776  // if bCompressEntireSlice is true, then the entire slice (not slice segment) is compressed,
777  //   effectively disabling the slice-segment-mode.
778
779  UInt   startCtuTsAddr;
780  UInt   boundingCtuTsAddr;
781  TComSlice* const pcSlice            = pcPic->getSlice(getSliceIdx());
782  pcSlice->setSliceSegmentBits(0);
783  xDetermineStartAndBoundingCtuTsAddr ( startCtuTsAddr, boundingCtuTsAddr, pcPic );
784  if (bCompressEntireSlice)
785  {
786    boundingCtuTsAddr = pcSlice->getSliceCurEndCtuTsAddr();
787    pcSlice->setSliceSegmentCurEndCtuTsAddr(boundingCtuTsAddr);
788  }
789
790  // initialize cost values - these are used by precompressSlice (they should be parameters).
[2]791  m_uiPicTotalBits  = 0;
[1313]792  m_dPicRdCost      = 0; // NOTE: This is a write-only variable!
[2]793  m_uiPicDist       = 0;
[1313]794
795  m_pcEntropyCoder->setEntropyCoder   ( m_pppcRDSbacCoder[0][CI_CURR_BEST] );
796  m_pcEntropyCoder->resetEntropy      ( pcSlice );
797
798  TEncBinCABAC* pRDSbacCoder = (TEncBinCABAC *) m_pppcRDSbacCoder[0][CI_CURR_BEST]->getEncBinIf();
799  pRDSbacCoder->setBinCountingEnableFlag( false );
800  pRDSbacCoder->setBinsCoded( 0 );
801
802  TComBitCounter  tempBitCounter;
803  const UInt      frameWidthInCtus = pcPic->getPicSym()->getFrameWidthInCtus();
[56]804 
[1313]805  m_pcCuEncoder->setFastDeltaQp(bFastDeltaQP);
806
[56]807  //------------------------------------------------------------------------------
808  //  Weighted Prediction parameters estimation.
809  //------------------------------------------------------------------------------
810  // calculate AC/DC values for current picture
[608]811  if( pcSlice->getPPS()->getUseWP() || pcSlice->getPPS()->getWPBiPred() )
[56]812  {
813    xCalcACDCParamSlice(pcSlice);
814  }
[5]815
[1313]816  const Bool bWp_explicit = (pcSlice->getSliceType()==P_SLICE && pcSlice->getPPS()->getUseWP()) || (pcSlice->getSliceType()==B_SLICE && pcSlice->getPPS()->getWPBiPred());
[608]817
[313]818  if ( bWp_explicit )
819  {
820    //------------------------------------------------------------------------------
821    //  Weighted Prediction implemented at Slice level. SliceMode=2 is not supported yet.
822    //------------------------------------------------------------------------------
[1313]823    if ( pcSlice->getSliceMode()==FIXED_NUMBER_OF_BYTES || pcSlice->getSliceSegmentMode()==FIXED_NUMBER_OF_BYTES )
[313]824    {
825      printf("Weighted Prediction is not supported with slice mode determined by max number of bins.\n"); exit(0);
826    }
[608]827
[1386]828    xEstimateWPParamSlice( pcSlice, m_pcCfg->getWeightedPredictionMethod() );
[1313]829    pcSlice->initWpScaling(pcSlice->getSPS());
[608]830
[313]831    // check WP on/off
832    xCheckWPEnable( pcSlice );
833  }
834
[56]835#if ADAPTIVE_QP_SELECTION
[1313]836  if( m_pcCfg->getUseAdaptQpSelect() && !(pcSlice->getDependentSliceSegmentFlag()))
[56]837  {
[1313]838    // TODO: this won't work with dependent slices: they do not have their own QP. Check fix to mask clause execution with && !(pcSlice->getDependentSliceSegmentFlag())
839    m_pcTrQuant->clearSliceARLCnt(); // TODO: this looks wrong for multiple slices - the results of all but the last slice will be cleared before they are used (all slices compressed, and then all slices encoded)
[56]840    if(pcSlice->getSliceType()!=I_SLICE)
841    {
842      Int qpBase = pcSlice->getSliceQpBase();
843      pcSlice->setSliceQp(qpBase + m_pcTrQuant->getQpDelta(qpBase));
844    }
845  }
846#endif
[1313]847
848#if NH_3D_IC
849  if ( m_pcCfg->getViewIndex() && m_pcCfg->getUseIC() &&
[608]850       !( ( pcSlice->getSliceType() == P_SLICE && pcSlice->getPPS()->getUseWP() ) || ( pcSlice->getSliceType() == B_SLICE && pcSlice->getPPS()->getWPBiPred() ) )
851     )
[189]852  {
[1313]853    pcSlice ->xSetApplyIC(m_pcCfg->getUseICLowLatencyEnc());
[608]854    if ( pcSlice->getApplyIC() )
[443]855    {
[608]856      pcSlice->setIcSkipParseFlag( pcSlice->getPOC() % m_pcCfg->getIntraPeriod() != 0 );
[443]857    }
[189]858  }
859#endif
[56]860
[872]861
[1313]862
863  // Adjust initial state if this is the start of a dependent slice.
[608]864  {
[1313]865    const UInt      ctuRsAddr               = pcPic->getPicSym()->getCtuTsToRsAddrMap( startCtuTsAddr);
866    const UInt      currentTileIdx          = pcPic->getPicSym()->getTileIdxMap(ctuRsAddr);
867    const TComTile *pCurrentTile            = pcPic->getPicSym()->getTComTile(currentTileIdx);
868    const UInt      firstCtuRsAddrOfTile    = pCurrentTile->getFirstCtuRsAddr();
869    if( pcSlice->getDependentSliceSegmentFlag() && ctuRsAddr != firstCtuRsAddrOfTile )
[608]870    {
[1313]871      // This will only occur if dependent slice-segments (m_entropyCodingSyncContextState=true) are being used.
[1386]872      if( pCurrentTile->getTileWidthInCtus() >= 2 || !m_pcCfg->getEntropyCodingSyncEnabledFlag() )
[608]873      {
[1313]874        m_pppcRDSbacCoder[0][CI_CURR_BEST]->loadContexts( &m_lastSliceSegmentEndContextState );
[608]875      }
876    }
877  }
[1124]878
[1313]879  // for every CTU in the slice segment (may terminate sooner if there is a byte limit on the slice-segment)
880#if NH_3D_VSO
[100]881  Int iLastPosY = -1;
[210]882#endif
[1313]883
884  for( UInt ctuTsAddr = startCtuTsAddr; ctuTsAddr < boundingCtuTsAddr; ++ctuTsAddr )
[2]885  {
[1313]886    const UInt ctuRsAddr = pcPic->getPicSym()->getCtuTsToRsAddrMap(ctuTsAddr);
887    // initialize CTU encoder
888    TComDataCU* pCtu = pcPic->getCtu( ctuRsAddr );
889    pCtu->initCtu( pcPic, ctuRsAddr );
890#if NH_3D_VSO
[100]891    if ( m_pcRdCost->getUseRenModel() )
892    {
893      // updated renderer model if necessary
894      Int iCurPosX;
895      Int iCurPosY; 
[1313]896      pCtu->getPosInPic(0, iCurPosX, iCurPosY );
[100]897      if ( iCurPosY != iLastPosY )
898      {
[608]899        iLastPosY = iCurPosY;         
[1313]900        TEncTop* pcEncTop = (TEncTop*) m_pcCfg; // Fix this later.
[1386]901        pcEncTop->setupRenModel( pcSlice->getPOC() , pcSlice->getViewIndex(), pcSlice->getIsDepth() || pcSlice->getVPS()->getAuxId( pcSlice->getLayerId()  ) ? 1 : 0, iCurPosY, pcSlice->getSPS()->getMaxCUHeight() );
[100]902      }
[608]903    }
[210]904#endif
[1313]905
906    // update CABAC state
907    const UInt firstCtuRsAddrOfTile = pcPic->getPicSym()->getTComTile(pcPic->getPicSym()->getTileIdxMap(ctuRsAddr))->getFirstCtuRsAddr();
908    const UInt tileXPosInCtus = firstCtuRsAddrOfTile % frameWidthInCtus;
909    const UInt ctuXPosInCtus  = ctuRsAddr % frameWidthInCtus;
910   
911    if (ctuRsAddr == firstCtuRsAddrOfTile)
912    {
913      m_pppcRDSbacCoder[0][CI_CURR_BEST]->resetEntropy(pcSlice);
914    }
[1386]915    else if ( ctuXPosInCtus == tileXPosInCtus && m_pcCfg->getEntropyCodingSyncEnabledFlag())
[1313]916    {
917      // reset and then update contexts to the state at the end of the top-right CTU (if within current slice and tile).
918      m_pppcRDSbacCoder[0][CI_CURR_BEST]->resetEntropy(pcSlice);
919      // Sync if the Top-Right is available.
920      TComDataCU *pCtuUp = pCtu->getCtuAbove();
921      if ( pCtuUp && ((ctuRsAddr%frameWidthInCtus+1) < frameWidthInCtus)  )
[56]922      {
[1313]923        TComDataCU *pCtuTR = pcPic->getCtu( ctuRsAddr - frameWidthInCtus + 1 );
924        if ( pCtu->CUIsFromSameSliceAndTile(pCtuTR) )
[56]925        {
[1313]926          // Top-Right is available, we use it.
927          m_pppcRDSbacCoder[0][CI_CURR_BEST]->loadContexts( &m_entropyCodingSyncContextState );
[56]928        }
929      }
[1313]930    }
[56]931
[1313]932    // set go-on entropy coder (used for all trial encodings - the cu encoder and encoder search also have a copy of the same pointer)
933    m_pcEntropyCoder->setEntropyCoder ( m_pcRDGoOnSbacCoder );
934    m_pcEntropyCoder->setBitstream( &tempBitCounter );
935    tempBitCounter.resetBits();
936    m_pcRDGoOnSbacCoder->load( m_pppcRDSbacCoder[0][CI_CURR_BEST] ); // this copy is not strictly necessary here, but indicates that the GoOnSbacCoder
937                                                                     // is reset to a known state before every decision process.
938
939    ((TEncBinCABAC*)m_pcRDGoOnSbacCoder->getEncBinIf())->setBinCountingEnableFlag(true);
940
941    Double oldLambda = m_pcRdCost->getLambda();
942    if ( m_pcCfg->getUseRateCtrl() )
[56]943    {
[1313]944      Int estQP        = pcSlice->getSliceQp();
945      Double estLambda = -1.0;
946      Double bpp       = -1.0;
947
948      if ( ( pcPic->getSlice( 0 )->getSliceType() == I_SLICE && m_pcCfg->getForceIntraQP() ) || !m_pcCfg->getLCULevelRC() )
[56]949      {
[1313]950        estQP = pcSlice->getSliceQp();
[56]951      }
[1313]952      else
[608]953      {
[655]954#if KWU_RC_MADPRED_E0227
955          if(pcSlice->getLayerId() != 0 && m_pcCfg->getUseDepthMADPred() && !pcSlice->getIsDepth())
956          {
957            Double zn, zf, focallength, position, camShift;
958            Double basePos;
959            Bool bInterpolated;
960            Int direction = pcSlice->getViewId() - pcCU->getSlice()->getIvPic(false, 0)->getViewId();
961            Int disparity;
962
963            pcEncTop->getCamParam()->xGetZNearZFar(pcEncTop->getCamParam()->getBaseViewNumbers()[pcSlice->getViewIndex()], pcSlice->getPOC(), zn, zf);
964            pcEncTop->getCamParam()->xGetGeometryData(pcEncTop->getCamParam()->getBaseViewNumbers()[0], pcSlice->getPOC(), focallength, basePos, camShift, bInterpolated);
965            pcEncTop->getCamParam()->xGetGeometryData(pcEncTop->getCamParam()->getBaseViewNumbers()[pcSlice->getViewIndex()], pcSlice->getPOC(), focallength, position, camShift, bInterpolated);
966            bpp       = m_pcRateCtrl->getRCPic()->getLCUTargetBppforInterView( m_pcRateCtrl->getPicList(), pcCU,
967              basePos, position, focallength, zn, zf, (direction > 0 ? 1 : -1), &disparity );
968          }
969          else
970          {
971#endif
[1313]972        bpp = m_pcRateCtrl->getRCPic()->getLCUTargetBpp(pcSlice->getSliceType());
973        if ( pcPic->getSlice( 0 )->getSliceType() == I_SLICE)
974        {
975          estLambda = m_pcRateCtrl->getRCPic()->getLCUEstLambdaAndQP(bpp, pcSlice->getSliceQp(), &estQP);
976        }
977        else
978        {
979          estLambda = m_pcRateCtrl->getRCPic()->getLCUEstLambda( bpp );
980          estQP     = m_pcRateCtrl->getRCPic()->getLCUEstQP    ( estLambda, pcSlice->getSliceQp() );
981        }
[655]982#if KWU_RC_MADPRED_E0227
983          estLambda = m_pcRateCtrl->getRCPic()->getLCUEstLambda( bpp );
984          estQP     = m_pcRateCtrl->getRCPic()->getLCUEstQP    ( estLambda, pcSlice->getSliceQp() );
985#endif
[608]986
[1313]987        estQP     = Clip3( -pcSlice->getSPS()->getQpBDOffset(CHANNEL_TYPE_LUMA), MAX_QP, estQP );
988
989        m_pcRdCost->setLambda(estLambda, pcSlice->getSPS()->getBitDepths());
990
[608]991#if RDOQ_CHROMA_LAMBDA
[1313]992        // set lambda for RDOQ
993        const Double chromaLambda = estLambda / m_pcRdCost->getChromaWeight();
994        const Double lambdaArray[MAX_NUM_COMPONENT] = { estLambda, chromaLambda, chromaLambda };
[872]995        m_pcTrQuant->setLambdas( lambdaArray );
[608]996#else
[1313]997        m_pcTrQuant->setLambda( estLambda );
[608]998#endif
[1313]999      }
[608]1000
[1313]1001      m_pcRateCtrl->setRCQP( estQP );
[964]1002#if ADAPTIVE_QP_SELECTION
[1313]1003      pCtu->getSlice()->setSliceQpBase( estQP );
[964]1004#endif
[1313]1005    }
[608]1006
[1313]1007    // run CTU trial encoder
1008    m_pcCuEncoder->compressCtu( pCtu );
[2]1009
[608]1010
[1313]1011    // All CTU decisions have now been made. Restore entropy coder to an initial stage, ready to make a true encode,
1012    // which will result in the state of the contexts being correct. It will also count up the number of bits coded,
1013    // which is used if there is a limit of the number of bytes per slice-segment.
1014
1015    m_pcEntropyCoder->setEntropyCoder ( m_pppcRDSbacCoder[0][CI_CURR_BEST] );
1016    m_pcEntropyCoder->setBitstream( &tempBitCounter );
1017    pRDSbacCoder->setBinCountingEnableFlag( true );
1018    m_pppcRDSbacCoder[0][CI_CURR_BEST]->resetBits();
1019    pRDSbacCoder->setBinsCoded( 0 );
1020
1021    // encode CTU and calculate the true bit counters.
1022    m_pcCuEncoder->encodeCtu( pCtu );
1023
1024
1025    pRDSbacCoder->setBinCountingEnableFlag( false );
1026
1027    const Int numberOfWrittenBits = m_pcEntropyCoder->getNumberOfWrittenBits();
1028
1029    // Calculate if this CTU puts us over slice bit size.
1030    // cannot terminate if current slice/slice-segment would be 0 Ctu in size,
1031    const UInt validEndOfSliceCtuTsAddr = ctuTsAddr + (ctuTsAddr == startCtuTsAddr ? 1 : 0);
1032    // Set slice end parameter
1033    if(pcSlice->getSliceMode()==FIXED_NUMBER_OF_BYTES && pcSlice->getSliceBits()+numberOfWrittenBits > (pcSlice->getSliceArgument()<<3))
1034    {
1035      pcSlice->setSliceSegmentCurEndCtuTsAddr(validEndOfSliceCtuTsAddr);
1036      pcSlice->setSliceCurEndCtuTsAddr(validEndOfSliceCtuTsAddr);
1037      boundingCtuTsAddr=validEndOfSliceCtuTsAddr;
1038    }
1039    else if((!bCompressEntireSlice) && pcSlice->getSliceSegmentMode()==FIXED_NUMBER_OF_BYTES && pcSlice->getSliceSegmentBits()+numberOfWrittenBits > (pcSlice->getSliceSegmentArgument()<<3))
1040    {
1041      pcSlice->setSliceSegmentCurEndCtuTsAddr(validEndOfSliceCtuTsAddr);
1042      boundingCtuTsAddr=validEndOfSliceCtuTsAddr;
1043    }
1044
1045    if (boundingCtuTsAddr <= ctuTsAddr)
1046    {
1047      break;
1048    }
1049
1050    pcSlice->setSliceBits( (UInt)(pcSlice->getSliceBits() + numberOfWrittenBits) );
1051    pcSlice->setSliceSegmentBits(pcSlice->getSliceSegmentBits()+numberOfWrittenBits);
1052
1053    // Store probabilities of second CTU in line into buffer - used only if wavefront-parallel-processing is enabled.
[1386]1054    if ( ctuXPosInCtus == tileXPosInCtus+1 && m_pcCfg->getEntropyCodingSyncEnabledFlag())
[1313]1055    {
1056      m_entropyCodingSyncContextState.loadContexts(m_pppcRDSbacCoder[0][CI_CURR_BEST]);
1057    }
1058
1059
1060    if ( m_pcCfg->getUseRateCtrl() )
1061    {
[872]1062#if KWU_RC_MADPRED_E0227
[608]1063        UInt SAD    = m_pcCuEncoder->getLCUPredictionSAD();
1064        Int height  = min( pcSlice->getSPS()->getMaxCUHeight(),pcSlice->getSPS()->getPicHeightInLumaSamples() - uiCUAddr / rpcPic->getFrameWidthInCU() * pcSlice->getSPS()->getMaxCUHeight() );
1065        Int width   = min( pcSlice->getSPS()->getMaxCUWidth(),pcSlice->getSPS()->getPicWidthInLumaSamples() - uiCUAddr % rpcPic->getFrameWidthInCU() * pcSlice->getSPS()->getMaxCUWidth() );
1066        Double MAD = (Double)SAD / (Double)(height * width);
1067        MAD = MAD * MAD;
1068        ( m_pcRateCtrl->getRCPic()->getLCU(uiCUAddr) ).m_MAD = MAD;
1069#endif
1070
[1313]1071      Int actualQP        = g_RCInvalidQPValue;
1072      Double actualLambda = m_pcRdCost->getLambda();
1073      Int actualBits      = pCtu->getTotalBits();
1074      Int numberOfEffectivePixels    = 0;
1075      for ( Int idx = 0; idx < pcPic->getNumPartitionsInCtu(); idx++ )
1076      {
1077        if ( pCtu->getPredictionMode( idx ) != NUMBER_OF_PREDICTION_MODES && ( !pCtu->isSkipped( idx ) ) )
[608]1078        {
[1313]1079          numberOfEffectivePixels = numberOfEffectivePixels + 16;
1080          break;
[608]1081        }
[1313]1082      }
[608]1083
[1313]1084      if ( numberOfEffectivePixels == 0 )
1085      {
1086        actualQP = g_RCInvalidQPValue;
1087      }
1088      else
1089      {
1090        actualQP = pCtu->getQP( 0 );
1091      }
1092      m_pcRdCost->setLambda(oldLambda, pcSlice->getSPS()->getBitDepths());
1093      m_pcRateCtrl->getRCPic()->updateAfterCTU( m_pcRateCtrl->getRCPic()->getLCUCoded(), actualBits, actualQP, actualLambda,
1094                                                pCtu->getSlice()->getSliceType() == I_SLICE ? 0 : m_pcCfg->getLCULevelRC() );
1095    }
[608]1096
[1313]1097    m_uiPicTotalBits += pCtu->getTotalBits();
1098    m_dPicRdCost     += pCtu->getTotalCost();
1099    m_uiPicDist      += pCtu->getTotalDistortion();
[2]1100  }
[1313]1101
1102  // store context state at the end of this slice-segment, in case the next slice is a dependent slice and continues using the CABAC contexts.
1103  if( pcSlice->getPPS()->getDependentSliceSegmentsEnabledFlag() )
[608]1104  {
[1313]1105    m_lastSliceSegmentEndContextState.loadContexts( m_pppcRDSbacCoder[0][CI_CURR_BEST] );//ctx end of dep.slice
[608]1106  }
[1313]1107
1108  // stop use of temporary bit counter object.
1109  m_pppcRDSbacCoder[0][CI_CURR_BEST]->setBitstream(NULL);
1110  m_pcRDGoOnSbacCoder->setBitstream(NULL); // stop use of tempBitCounter.
1111
1112  // TODO: optimise cabac_init during compress slice to improve multi-slice operation
1113  //if (pcSlice->getPPS()->getCabacInitPresentFlag() && !pcSlice->getPPS()->getDependentSliceSegmentsEnabledFlag())
1114  //{
1115  //  m_encCABACTableIdx = m_pcEntropyCoder->determineCabacInitIdx();
1116  //}
1117  //else
1118  //{
1119  //  m_encCABACTableIdx = pcSlice->getSliceType();
1120  //}
[2]1121}
1122
[1313]1123Void TEncSlice::encodeSlice   ( TComPic* pcPic, TComOutputBitstream* pcSubstreams, UInt &numBinsCoded )
[2]1124{
[1313]1125  TComSlice *const pcSlice           = pcPic->getSlice(getSliceIdx());
[2]1126
[1313]1127  const UInt startCtuTsAddr          = pcSlice->getSliceSegmentCurStartCtuTsAddr();
1128  const UInt boundingCtuTsAddr       = pcSlice->getSliceSegmentCurEndCtuTsAddr();
1129
1130  const UInt frameWidthInCtus        = pcPic->getPicSym()->getFrameWidthInCtus();
1131  const Bool depSliceSegmentsEnabled = pcSlice->getPPS()->getDependentSliceSegmentsEnabledFlag();
1132  const Bool wavefrontsEnabled       = pcSlice->getPPS()->getEntropyCodingSyncEnabledFlag();
1133
1134  // initialise entropy coder for the slice
1135  m_pcSbacCoder->init( (TEncBinIf*)m_pcBinCABAC );
1136  m_pcEntropyCoder->setEntropyCoder ( m_pcSbacCoder );
1137  m_pcEntropyCoder->resetEntropy    ( pcSlice );
1138
1139  numBinsCoded = 0;
1140  m_pcBinCABAC->setBinCountingEnableFlag( true );
1141  m_pcBinCABAC->setBinsCoded(0);
1142
[2]1143#if ENC_DEC_TRACE
1144  g_bJustDoIt = g_bEncDecTraceEnable;
1145#endif
[1386]1146#if NH_MV_ENC_DEC_TRAC
[1313]1147#if ENC_DEC_TRACE
1148  incSymbolCounter();
1149#endif
1150  DTRACE_CABAC_VL( g_nSymbolCounter );
1151#else
[56]1152  DTRACE_CABAC_VL( g_nSymbolCounter++ );
[1313]1153#endif
[2]1154  DTRACE_CABAC_T( "\tPOC: " );
[1313]1155  DTRACE_CABAC_V( pcPic->getPOC() );
[1386]1156#if NH_MV_ENC_DEC_TRAC
[608]1157  DTRACE_CABAC_T( " Layer: " );
[1313]1158  DTRACE_CABAC_V( pcPic->getLayerId() );
[608]1159#endif
[2]1160  DTRACE_CABAC_T( "\n" );
1161#if ENC_DEC_TRACE
1162  g_bJustDoIt = g_bEncDecTraceDisable;
1163#endif
1164
[1313]1165
1166  if (depSliceSegmentsEnabled)
[2]1167  {
[1313]1168    // modify initial contexts with previous slice segment if this is a dependent slice.
1169    const UInt ctuRsAddr        = pcPic->getPicSym()->getCtuTsToRsAddrMap( startCtuTsAddr );
1170    const UInt currentTileIdx=pcPic->getPicSym()->getTileIdxMap(ctuRsAddr);
1171    const TComTile *pCurrentTile=pcPic->getPicSym()->getTComTile(currentTileIdx);
1172    const UInt firstCtuRsAddrOfTile = pCurrentTile->getFirstCtuRsAddr();
[56]1173
[1313]1174    if( pcSlice->getDependentSliceSegmentFlag() && ctuRsAddr != firstCtuRsAddrOfTile )
[56]1175    {
[1313]1176      if( pCurrentTile->getTileWidthInCtus() >= 2 || !wavefrontsEnabled )
1177      {
1178        m_pcSbacCoder->loadContexts(&m_lastSliceSegmentEndContextState);
1179      }
[56]1180    }
1181  }
1182
[1313]1183  // for every CTU in the slice segment...
1184
1185  for( UInt ctuTsAddr = startCtuTsAddr; ctuTsAddr < boundingCtuTsAddr; ++ctuTsAddr )
[608]1186  {
[1313]1187    const UInt ctuRsAddr = pcPic->getPicSym()->getCtuTsToRsAddrMap(ctuTsAddr);
1188    const TComTile &currentTile = *(pcPic->getPicSym()->getTComTile(pcPic->getPicSym()->getTileIdxMap(ctuRsAddr)));
1189    const UInt firstCtuRsAddrOfTile = currentTile.getFirstCtuRsAddr();
1190    const UInt tileXPosInCtus       = firstCtuRsAddrOfTile % frameWidthInCtus;
1191    const UInt tileYPosInCtus       = firstCtuRsAddrOfTile / frameWidthInCtus;
1192    const UInt ctuXPosInCtus        = ctuRsAddr % frameWidthInCtus;
1193    const UInt ctuYPosInCtus        = ctuRsAddr / frameWidthInCtus;
1194    const UInt uiSubStrm=pcPic->getSubstreamForCtuAddr(ctuRsAddr, true, pcSlice);
1195    TComDataCU* pCtu = pcPic->getCtu( ctuRsAddr );
1196
1197    m_pcEntropyCoder->setBitstream( &pcSubstreams[uiSubStrm] );
1198
1199    // set up CABAC contexts' state for this CTU
1200    if (ctuRsAddr == firstCtuRsAddrOfTile)
[608]1201    {
[1313]1202      if (ctuTsAddr != startCtuTsAddr) // if it is the first CTU, then the entropy coder has already been reset
[608]1203      {
[1313]1204        m_pcEntropyCoder->resetEntropy(pcSlice);
[608]1205      }
1206    }
[1313]1207    else if (ctuXPosInCtus == tileXPosInCtus && wavefrontsEnabled)
[608]1208    {
[1313]1209      // Synchronize cabac probabilities with upper-right CTU if it's available and at the start of a line.
1210      if (ctuTsAddr != startCtuTsAddr) // if it is the first CTU, then the entropy coder has already been reset
[608]1211      {
[1313]1212        m_pcEntropyCoder->resetEntropy(pcSlice);
[608]1213      }
[1313]1214      TComDataCU *pCtuUp = pCtu->getCtuAbove();
1215      if ( pCtuUp && ((ctuRsAddr%frameWidthInCtus+1) < frameWidthInCtus)  )
[56]1216      {
[1313]1217        TComDataCU *pCtuTR = pcPic->getCtu( ctuRsAddr - frameWidthInCtus + 1 );
1218        if ( pCtu->CUIsFromSameSliceAndTile(pCtuTR) )
[56]1219        {
[1313]1220          // Top-right is available, so use it.
1221          m_pcSbacCoder->loadContexts( &m_entropyCodingSyncContextState );
[56]1222        }
1223      }
[1313]1224    }
[872]1225
[1313]1226#if NH_3D_QTLPC
1227    pcPic->setReduceBitsFlag(true);
1228#endif
1229    if ( pcSlice->getSPS()->getUseSAO() )
[56]1230    {
[1313]1231      Bool bIsSAOSliceEnabled = false;
1232      Bool sliceEnabled[MAX_NUM_COMPONENT];
1233      for(Int comp=0; comp < MAX_NUM_COMPONENT; comp++)
[56]1234      {
[1313]1235        ComponentID compId=ComponentID(comp);
1236        sliceEnabled[compId] = pcSlice->getSaoEnabledFlag(toChannelType(compId)) && (comp < pcPic->getNumberValidComponents());
1237        if (sliceEnabled[compId])
[56]1238        {
[1313]1239          bIsSAOSliceEnabled=true;
[56]1240        }
1241      }
[1313]1242      if (bIsSAOSliceEnabled)
[56]1243      {
[1313]1244        SAOBlkParam& saoblkParam = (pcPic->getPicSym()->getSAOBlkParam())[ctuRsAddr];
[56]1245
[872]1246        Bool leftMergeAvail = false;
1247        Bool aboveMergeAvail= false;
1248        //merge left condition
[1313]1249        Int rx = (ctuRsAddr % frameWidthInCtus);
[872]1250        if(rx > 0)
[443]1251        {
[1313]1252          leftMergeAvail = pcPic->getSAOMergeAvailability(ctuRsAddr, ctuRsAddr-1);
[443]1253        }
[872]1254
1255        //merge up condition
[1313]1256        Int ry = (ctuRsAddr / frameWidthInCtus);
[872]1257        if(ry > 0)
[1313]1258        {
1259          aboveMergeAvail = pcPic->getSAOMergeAvailability(ctuRsAddr, ctuRsAddr-frameWidthInCtus);
[443]1260        }
[872]1261
[1313]1262        m_pcEntropyCoder->encodeSAOBlkParam(saoblkParam, pcPic->getPicSym()->getSPS().getBitDepths(), sliceEnabled, leftMergeAvail, aboveMergeAvail);
[608]1263      }
[443]1264    }
[1313]1265
[2]1266#if ENC_DEC_TRACE
1267    g_bJustDoIt = g_bEncDecTraceEnable;
1268#endif
[1313]1269      m_pcCuEncoder->encodeCtu( pCtu );
1270#if ENC_DEC_TRACE
1271    g_bJustDoIt = g_bEncDecTraceDisable;
1272#endif
1273
1274    //Store probabilities of second CTU in line into buffer
1275    if ( ctuXPosInCtus == tileXPosInCtus+1 && wavefrontsEnabled)
[2]1276    {
[1313]1277      m_entropyCodingSyncContextState.loadContexts( m_pcSbacCoder );
[2]1278    }
[1313]1279
1280    // terminate the sub-stream, if required (end of slice-segment, end of tile, end of wavefront-CTU-row):
1281    if (ctuTsAddr+1 == boundingCtuTsAddr ||
1282         (  ctuXPosInCtus + 1 == tileXPosInCtus + currentTile.getTileWidthInCtus() &&
1283          ( ctuYPosInCtus + 1 == tileYPosInCtus + currentTile.getTileHeightInCtus() || wavefrontsEnabled)
1284         )
1285       )
[2]1286    {
[1313]1287      m_pcEntropyCoder->encodeTerminatingBit(1);
1288      m_pcEntropyCoder->encodeSliceFinish();
1289      // Byte-alignment in slice_data() when new tile
1290      pcSubstreams[uiSubStrm].writeByteAlignment();
[56]1291
[1313]1292      // write sub-stream size
1293      if (ctuTsAddr+1 != boundingCtuTsAddr)
[56]1294      {
[1313]1295        pcSlice->addSubstreamSize( (pcSubstreams[uiSubStrm].getNumberOfWrittenBits() >> 3) + pcSubstreams[uiSubStrm].countStartCodeEmulations() );
[56]1296      }
[1313]1297    }
1298#if NH_3D_QTLPC
1299    pcPic->setReduceBitsFlag(false);
[189]1300#endif
[1313]1301  } // CTU-loop
1302
[608]1303  if( depSliceSegmentsEnabled )
1304  {
[1313]1305    m_lastSliceSegmentEndContextState.loadContexts( m_pcSbacCoder );//ctx end of dep.slice
[608]1306  }
[1313]1307
[56]1308#if ADAPTIVE_QP_SELECTION
1309  if( m_pcCfg->getUseAdaptQpSelect() )
1310  {
[1313]1311    m_pcTrQuant->storeSliceQpNext(pcSlice); // TODO: this will only be storing the adaptive QP state of the very last slice-segment that is not dependent in the frame... Perhaps this should be moved to the compress slice loop.
[56]1312  }
1313#endif
[1313]1314
1315  if (pcSlice->getPPS()->getCabacInitPresentFlag() && !pcSlice->getPPS()->getDependentSliceSegmentsEnabledFlag())
[56]1316  {
[1313]1317    m_encCABACTableIdx = m_pcEntropyCoder->determineCabacInitIdx(pcSlice);
[56]1318  }
[1313]1319  else
1320  {
1321    m_encCABACTableIdx = pcSlice->getSliceType();
1322  }
1323 
1324  numBinsCoded = m_pcBinCABAC->getBinsCoded();
[2]1325}
1326
[1313]1327Void TEncSlice::calculateBoundingCtuTsAddrForSlice(UInt &startCtuTSAddrSlice, UInt &boundingCtuTSAddrSlice, Bool &haveReachedTileBoundary,
1328                                                   TComPic* pcPic, const Int sliceMode, const Int sliceArgument)
[2]1329{
[1313]1330  TComSlice* pcSlice = pcPic->getSlice(getSliceIdx());
1331  const UInt numberOfCtusInFrame = pcPic->getNumberOfCtusInFrame();
1332  const TComPPS &pps=*(pcSlice->getPPS());
1333  boundingCtuTSAddrSlice=0;
1334  haveReachedTileBoundary=false;
[56]1335
[1313]1336  switch (sliceMode)
[2]1337  {
[1313]1338    case FIXED_NUMBER_OF_CTU:
1339      {
1340        UInt ctuAddrIncrement    = sliceArgument;
1341        boundingCtuTSAddrSlice  = ((startCtuTSAddrSlice + ctuAddrIncrement) < numberOfCtusInFrame) ? (startCtuTSAddrSlice + ctuAddrIncrement) : numberOfCtusInFrame;
1342      }
[2]1343      break;
[608]1344    case FIXED_NUMBER_OF_BYTES:
[1313]1345      boundingCtuTSAddrSlice  = numberOfCtusInFrame; // This will be adjusted later if required.
[2]1346      break;
[608]1347    case FIXED_NUMBER_OF_TILES:
[1313]1348      {
1349        const UInt tileIdx        = pcPic->getPicSym()->getTileIdxMap( pcPic->getPicSym()->getCtuTsToRsAddrMap(startCtuTSAddrSlice) );
1350        const UInt tileTotalCount = (pcPic->getPicSym()->getNumTileColumnsMinus1()+1) * (pcPic->getPicSym()->getNumTileRowsMinus1()+1);
1351        UInt ctuAddrIncrement   = 0;
[56]1352
[1313]1353        for(UInt tileIdxIncrement = 0; tileIdxIncrement < sliceArgument; tileIdxIncrement++)
[56]1354        {
[1313]1355          if((tileIdx + tileIdxIncrement) < tileTotalCount)
1356          {
1357            UInt tileWidthInCtus   = pcPic->getPicSym()->getTComTile(tileIdx + tileIdxIncrement)->getTileWidthInCtus();
1358            UInt tileHeightInCtus  = pcPic->getPicSym()->getTComTile(tileIdx + tileIdxIncrement)->getTileHeightInCtus();
1359            ctuAddrIncrement    += (tileWidthInCtus * tileHeightInCtus);
1360          }
[56]1361        }
1362
[1313]1363        boundingCtuTSAddrSlice  = ((startCtuTSAddrSlice + ctuAddrIncrement) < numberOfCtusInFrame) ? (startCtuTSAddrSlice + ctuAddrIncrement) : numberOfCtusInFrame;
[56]1364      }
1365      break;
[2]1366    default:
[1313]1367      boundingCtuTSAddrSlice    = numberOfCtusInFrame;
[2]1368      break;
1369  }
1370
[1313]1371  // Adjust for tiles and wavefronts.
1372  const Bool wavefrontsAreEnabled = pps.getEntropyCodingSyncEnabledFlag();
[56]1373
[1313]1374  if ((sliceMode == FIXED_NUMBER_OF_CTU || sliceMode == FIXED_NUMBER_OF_BYTES) &&
1375      (pps.getNumTileRowsMinus1() > 0 || pps.getNumTileColumnsMinus1() > 0))
[2]1376  {
[1313]1377    const UInt ctuRSAddr                  = pcPic->getPicSym()->getCtuTsToRsAddrMap(startCtuTSAddrSlice);
1378    const UInt startTileIdx               = pcPic->getPicSym()->getTileIdxMap(ctuRSAddr);
[608]1379
[1313]1380    const TComTile *pStartingTile         = pcPic->getPicSym()->getTComTile(startTileIdx);
1381    const UInt tileStartTsAddr            = pcPic->getPicSym()->getCtuRsToTsAddrMap(pStartingTile->getFirstCtuRsAddr());
1382    const UInt tileStartWidth             = pStartingTile->getTileWidthInCtus();
1383    const UInt tileStartHeight            = pStartingTile->getTileHeightInCtus();
1384    const UInt tileLastTsAddr_excl        = tileStartTsAddr + tileStartWidth*tileStartHeight;
1385    const UInt tileBoundingCtuTsAddrSlice = tileLastTsAddr_excl;
1386
1387    const UInt ctuColumnOfStartingTile    = ((startCtuTSAddrSlice-tileStartTsAddr)%tileStartWidth);
1388    if (wavefrontsAreEnabled && ctuColumnOfStartingTile!=0)
[608]1389    {
[1313]1390      // WPP: if a slice does not start at the beginning of a CTB row, it must end within the same CTB row
1391      const UInt numberOfCTUsToEndOfRow            = tileStartWidth - ctuColumnOfStartingTile;
1392      const UInt wavefrontTileBoundingCtuAddrSlice = startCtuTSAddrSlice + numberOfCTUsToEndOfRow;
1393      if (wavefrontTileBoundingCtuAddrSlice < boundingCtuTSAddrSlice)
[608]1394      {
[1313]1395        boundingCtuTSAddrSlice = wavefrontTileBoundingCtuAddrSlice;
[608]1396      }
1397    }
1398
[1313]1399    if (tileBoundingCtuTsAddrSlice < boundingCtuTSAddrSlice)
[608]1400    {
[1313]1401      boundingCtuTSAddrSlice = tileBoundingCtuTsAddrSlice;
1402      haveReachedTileBoundary = true;
[608]1403    }
[56]1404  }
[1313]1405  else if ((sliceMode == FIXED_NUMBER_OF_CTU || sliceMode == FIXED_NUMBER_OF_BYTES) && wavefrontsAreEnabled && ((startCtuTSAddrSlice % pcPic->getFrameWidthInCtus()) != 0))
[608]1406  {
[1313]1407    // Adjust for wavefronts (no tiles).
1408    // WPP: if a slice does not start at the beginning of a CTB row, it must end within the same CTB row
1409    boundingCtuTSAddrSlice = min(boundingCtuTSAddrSlice, startCtuTSAddrSlice - (startCtuTSAddrSlice % pcPic->getFrameWidthInCtus()) + (pcPic->getFrameWidthInCtus()));
[608]1410  }
[1313]1411}
[608]1412
[1313]1413/** Determines the starting and bounding CTU address of current slice / dependent slice
1414 * \param [out] startCtuTsAddr
1415 * \param [out] boundingCtuTsAddr
1416 * \param [in]  pcPic
[2]1417
[1313]1418 * Updates startCtuTsAddr, boundingCtuTsAddr with appropriate CTU address
1419 */
1420Void TEncSlice::xDetermineStartAndBoundingCtuTsAddr  ( UInt& startCtuTsAddr, UInt& boundingCtuTsAddr, TComPic* pcPic )
1421{
1422  TComSlice* pcSlice                 = pcPic->getSlice(getSliceIdx());
[2]1423
[1313]1424  // Non-dependent slice
1425  UInt startCtuTsAddrSlice           = pcSlice->getSliceCurStartCtuTsAddr();
1426  Bool haveReachedTileBoundarySlice  = false;
1427  UInt boundingCtuTsAddrSlice;
1428  calculateBoundingCtuTsAddrForSlice(startCtuTsAddrSlice, boundingCtuTsAddrSlice, haveReachedTileBoundarySlice, pcPic,
1429                                     m_pcCfg->getSliceMode(), m_pcCfg->getSliceArgument());
1430  pcSlice->setSliceCurEndCtuTsAddr(   boundingCtuTsAddrSlice );
1431  pcSlice->setSliceCurStartCtuTsAddr( startCtuTsAddrSlice    );
1432
1433  // Dependent slice
1434  UInt startCtuTsAddrSliceSegment          = pcSlice->getSliceSegmentCurStartCtuTsAddr();
1435  Bool haveReachedTileBoundarySliceSegment = false;
1436  UInt boundingCtuTsAddrSliceSegment;
1437  calculateBoundingCtuTsAddrForSlice(startCtuTsAddrSliceSegment, boundingCtuTsAddrSliceSegment, haveReachedTileBoundarySliceSegment, pcPic,
1438                                     m_pcCfg->getSliceSegmentMode(), m_pcCfg->getSliceSegmentArgument());
1439  if (boundingCtuTsAddrSliceSegment>boundingCtuTsAddrSlice)
[2]1440  {
[1313]1441    boundingCtuTsAddrSliceSegment = boundingCtuTsAddrSlice;
[2]1442  }
[1313]1443  pcSlice->setSliceSegmentCurEndCtuTsAddr( boundingCtuTsAddrSliceSegment );
1444  pcSlice->setSliceSegmentCurStartCtuTsAddr(startCtuTsAddrSliceSegment);
1445
1446  // Make a joint decision based on reconstruction and dependent slice bounds
1447  startCtuTsAddr    = max(startCtuTsAddrSlice   , startCtuTsAddrSliceSegment   );
1448  boundingCtuTsAddr = boundingCtuTsAddrSliceSegment;
[2]1449}
[608]1450
1451Double TEncSlice::xGetQPValueAccordingToLambda ( Double lambda )
1452{
1453  return 4.2005*log(lambda) + 13.7122;
1454}
1455
[56]1456//! \}
Note: See TracBrowser for help on using the repository browser.