Changeset 56 in 3DVCSoftware for trunk/source/Lib/TLibEncoder/TEncGOP.cpp


Ignore:
Timestamp:
11 May 2012, 21:20:17 (12 years ago)
Author:
hschwarz
Message:

updated trunk (move to HM6.1)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/source/Lib/TLibEncoder/TEncGOP.cpp

    r42 r56  
    22 * License, included below. This software may be subject to other third party
    33 * and contributor rights, including patent rights, and no such rights are
    4  * granted under this license.
     4 * granted under this license. 
    55 *
    6  * Copyright (c) 2010-2011, ISO/IEC
     6 * Copyright (c) 2010-2012, ITU/ISO/IEC
    77 * All rights reserved.
    88 *
     
    1515 *    this list of conditions and the following disclaimer in the documentation
    1616 *    and/or other materials provided with the distribution.
    17  *  * Neither the name of the ISO/IEC nor the names of its contributors may
     17 *  * Neither the name of the ITU/ISO/IEC nor the names of its contributors may
    1818 *    be used to endorse or promote products derived from this software without
    1919 *    specific prior written permission.
     
    3232 */
    3333
    34 
    35 
    36 /** \file     TEncPic.cpp
     34/** \file     TEncGOP.cpp
    3735    \brief    GOP encoder class
    3836*/
     37
     38#include <list>
     39#include <algorithm>
    3940
    4041#include "TEncTop.h"
    4142#include "TEncGOP.h"
    4243#include "TEncAnalyze.h"
    43 #include "../libmd5/MD5.h"
    44 #include "../TLibCommon/SEI.h"
     44#include "libmd5/MD5.h"
     45#include "TLibCommon/SEI.h"
     46#include "TLibCommon/NAL.h"
     47#include "NALwrite.h"
     48#include "../../App/TAppEncoder/TAppEncTop.h"
    4549
    4650#include <time.h>
    47 
    48 #include "../../App/TAppEncoder/TAppEncTop.h"
     51#include <math.h>
     52
     53using namespace std;
     54
     55//! \ingroup TLibEncoder
     56//! \{
    4957
    5058// ====================================================================================================================
     
    5260// ====================================================================================================================
    5361
    54 TEncPic::TEncPic()
     62TEncGOP::TEncGOP()
    5563{
     64  m_iLastIDR            = 0;
     65  m_iGopSize            = 0;
     66  m_iNumPicCoded        = 0; //Niko
     67  m_bFirst              = true;
     68 
    5669  m_pcCfg               = NULL;
    5770  m_pcSliceEncoder      = NULL;
    5871  m_pcListPic           = NULL;
    59 
     72 
    6073  m_pcEntropyCoder      = NULL;
    6174  m_pcCavlcCoder        = NULL;
     
    6881  m_pcResidualGenerator = NULL;
    6982#endif
    70 
    71 #if DCM_DECODING_REFRESH
     83 
     84  m_bSeqFirst           = true;
     85 
    7286  m_bRefreshPending     = 0;
    73   m_uiPOCCDR            = 0;
    74 #endif
     87  m_pocCRA              = 0;
    7588
    7689  return;
    7790}
    7891
    79 TEncPic::~TEncPic()
     92TEncGOP::~TEncGOP()
    8093{
    8194}
     
    8497 * \param iWidth, iHeight are picture width, height. iMaxCUWidth, iMaxCUHeight are LCU width, height.
    8598 */
    86 Void  TEncPic::create( Int iWidth, Int iHeight, UInt iMaxCUWidth, UInt iMaxCUHeight )
     99Void  TEncGOP::create( Int iWidth, Int iHeight, UInt iMaxCUWidth, UInt iMaxCUHeight )
    87100{
    88101  UInt uiWidthInCU       = ( iWidth %iMaxCUWidth  ) ? iWidth /iMaxCUWidth  + 1 : iWidth /iMaxCUWidth;
    89102  UInt uiHeightInCU      = ( iHeight%iMaxCUHeight ) ? iHeight/iMaxCUHeight + 1 : iHeight/iMaxCUHeight;
    90103  UInt uiNumCUsInFrame   = uiWidthInCU * uiHeightInCU;
    91   m_uiStoredStartCUAddrForEncodingSlice = new UInt [uiNumCUsInFrame+1];
    92   m_uiStoredStartCUAddrForEncodingEntropySlice = new UInt [uiNumCUsInFrame+1];
     104  m_uiStoredStartCUAddrForEncodingSlice = new UInt [uiNumCUsInFrame*(1<<(g_uiMaxCUDepth<<1))+1];
     105  m_uiStoredStartCUAddrForEncodingEntropySlice = new UInt [uiNumCUsInFrame*(1<<(g_uiMaxCUDepth<<1))+1];
     106  m_bLongtermTestPictureHasBeenCoded = 0;
     107  m_bLongtermTestPictureHasBeenCoded2 = 0;
    93108}
    94109
    95 Void  TEncPic::destroy()
     110Void  TEncGOP::destroy()
    96111{
    97112  delete [] m_uiStoredStartCUAddrForEncodingSlice; m_uiStoredStartCUAddrForEncodingSlice = NULL;
     
    99114}
    100115
    101 Void TEncPic::init ( TEncTop* pcTEncTop )
     116Void TEncGOP::init ( TEncTop* pcTEncTop )
    102117{
    103118  m_pcEncTop     = pcTEncTop;
     
    105120  m_pcSliceEncoder       = pcTEncTop->getSliceEncoder();
    106121  m_pcListPic            = pcTEncTop->getListPic();
    107 
     122 
    108123  m_pcEntropyCoder       = pcTEncTop->getEntropyCoder();
    109124  m_pcCavlcCoder         = pcTEncTop->getCavlcCoder();
     
    112127  m_pcLoopFilter         = pcTEncTop->getLoopFilter();
    113128  m_pcBitCounter         = pcTEncTop->getBitCounter();
     129 
    114130#if DEPTH_MAP_GENERATION
    115131  m_pcDepthMapGenerator  = pcTEncTop->getDepthMapGenerator();
     
    118134  m_pcResidualGenerator  = pcTEncTop->getResidualGenerator();
    119135#endif
    120 
     136 
    121137  // Adaptive Loop filter
    122138  m_pcAdaptiveLoopFilter = pcTEncTop->getAdaptiveLoopFilter();
    123139  //--Adaptive Loop filter
    124 #if MTK_SAO
    125140  m_pcSAO                = pcTEncTop->getSAO();
    126 #endif
    127141  m_pcRdCost             = pcTEncTop->getRdCost();
    128 
    129142}
    130143
     
    133146// ====================================================================================================================
    134147
    135 Void TEncPic::compressPic( TComBitstream* pcBitstreamOut, TComPicYuv cPicOrg, TComPic* pcPic, TComPicYuv* pcPicYuvRecOut,
    136                TComPic* pcOrgRefList[2][MAX_REF_PIC_NUM], Bool&  rbSeqFirst, TComList<TComPic*>& rcListPic  )
     148Void TEncGOP::initGOP( Int iPOCLast, Int iNumPicRcvd, TComList<TComPic*>& rcListPic, TComList<TComPicYuv*>& rcListPicYuvRecOut, std::list<AccessUnit>& accessUnitsInGOP)
    137149{
     150  xInitGOP( iPOCLast, iNumPicRcvd, rcListPic, rcListPicYuvRecOut );
     151  m_iNumPicCoded = 0;
     152}
     153
     154Void TEncGOP::compressPicInGOP( Int iPOCLast, Int iNumPicRcvd, TComList<TComPic*>& rcListPic, TComList<TComPicYuv*>& rcListPicYuvRecOut, std::list<AccessUnit>& accessUnitsInGOP, Int iGOPid)
     155{
     156  TComPic*        pcPic;
     157  TComPicYuv*     pcPicYuvRecOut;
    138158  TComSlice*      pcSlice;
    139 
     159  TComOutputBitstream  *pcBitstreamRedirect;
     160  pcBitstreamRedirect = new TComOutputBitstream;
     161#if !REMOVE_TILE_DEPENDENCE
     162  OutputNALUnit        *naluBuffered             = NULL;
     163  Bool                  bIteratorAtListStart     = false;
     164#endif
     165  AccessUnit::iterator  itLocationToPushSliceHeaderNALU; // used to store location where NALU containing slice header is to be inserted
     166  UInt                  uiOneBitstreamPerSliceLength = 0;
     167  TEncSbac* pcSbacCoders = NULL;
     168  TComOutputBitstream* pcSubstreamsOut = NULL;
     169
     170  {
     171      UInt uiColDir = 1;
    140172      //-- For time output for each slice
    141173      long iBeforeTime = clock();
    142 
    143       //  Bitstream reset
    144       pcBitstreamOut->resetBits();
    145       pcBitstreamOut->rewindStreamPacket();
    146 
     174     
     175      //select uiColDir
     176      Int iCloseLeft=1, iCloseRight=-1;
     177      for(Int i = 0; i<m_pcCfg->getGOPEntry(iGOPid).m_numRefPics; i++)
     178      {
     179        Int iRef = m_pcCfg->getGOPEntry(iGOPid).m_referencePics[i];
     180        if(iRef>0&&(iRef<iCloseRight||iCloseRight==-1))
     181        {
     182          iCloseRight=iRef;
     183        }
     184        else if(iRef<0&&(iRef>iCloseLeft||iCloseLeft==1))
     185        {
     186          iCloseLeft=iRef;
     187        }
     188      }
     189      if(iCloseRight>-1)
     190      {
     191        iCloseRight=iCloseRight+m_pcCfg->getGOPEntry(iGOPid).m_POC-1;
     192      }
     193      if(iCloseLeft<1)
     194      {
     195        iCloseLeft=iCloseLeft+m_pcCfg->getGOPEntry(iGOPid).m_POC-1;
     196        while(iCloseLeft<0)
     197        {
     198          iCloseLeft+=m_iGopSize;
     199        }
     200      }
     201      Int iLeftQP=0, iRightQP=0;
     202      for(Int i=0; i<m_iGopSize; i++)
     203      {
     204        if(m_pcCfg->getGOPEntry(i).m_POC==(iCloseLeft%m_iGopSize)+1)
     205        {
     206          iLeftQP= m_pcCfg->getGOPEntry(i).m_QPOffset;
     207        }
     208        if (m_pcCfg->getGOPEntry(i).m_POC==(iCloseRight%m_iGopSize)+1)
     209        {
     210          iRightQP=m_pcCfg->getGOPEntry(i).m_QPOffset;
     211        }
     212      }
     213      if(iCloseRight>-1&&iRightQP<iLeftQP)
     214      {
     215        uiColDir=0;
     216      }
     217
     218      /////////////////////////////////////////////////////////////////////////////////////////////////// Initial to start encoding
     219      UInt uiPOCCurr = iPOCLast -iNumPicRcvd+ m_pcCfg->getGOPEntry(iGOPid).m_POC;
     220      Int iTimeOffset = m_pcCfg->getGOPEntry(iGOPid).m_POC;
     221      if(iPOCLast == 0)
     222      {
     223        uiPOCCurr=0;
     224        iTimeOffset = 1;
     225      }
     226      if(uiPOCCurr>=m_pcCfg->getFrameToBeEncoded())
     227      {
     228        return;
     229      }       
     230      if( getNalUnitTypeBaseViewMvc( uiPOCCurr ) == NAL_UNIT_CODED_SLICE_IDR )
     231      {
     232        m_iLastIDR = uiPOCCurr;
     233      }       
     234
     235      /* start a new access unit: create an entry in the list of output
     236       * access units */
     237      accessUnitsInGOP.push_back(AccessUnit());
     238      AccessUnit& accessUnit = accessUnitsInGOP.back();
     239      xGetBuffer( rcListPic, rcListPicYuvRecOut, iNumPicRcvd, iTimeOffset, pcPic, pcPicYuvRecOut, uiPOCCurr );
     240     
    147241      //  Slice data initialization
    148242      pcPic->clearSliceBuffer();
     
    150244      m_pcSliceEncoder->setSliceIdx(0);
    151245      pcPic->setCurrSliceIdx(0);
    152       m_pcSliceEncoder->initEncSlice ( pcPic, pcSlice );
     246
     247      std::vector<TComAPS>& vAPS = m_pcEncTop->getAPS();
     248      m_pcSliceEncoder->initEncSlice ( pcPic, iPOCLast, uiPOCCurr, iNumPicRcvd, iGOPid, pcSlice, m_pcEncTop->getSPS(), m_pcEncTop->getPPS() );
     249      pcSlice->setLastIDR(m_iLastIDR);
    153250      pcSlice->setSliceIdx(0);
    154 
    155       //  Set SPS
    156       pcSlice->setSPS( m_pcEncTop->getSPS() );
    157       pcSlice->setPPS( m_pcEncTop->getPPS() );
    158       pcSlice->setPPSId( pcSlice->getPPS()->getPPSId() );
    159 
    160   // set mutliview parameters
    161       pcSlice->initMultiviewSlice( pcPic->getCodedScale(), pcPic->getCodedOffset() );
    162 
    163 #if DCM_DECODING_REFRESH
     251      pcSlice->setViewId( m_pcEncTop->getViewId() );
     252      pcSlice->setIsDepth( m_pcEncTop->getIsDepth() );
     253
     254      m_pcEncTop->getSPS()->setDisInter4x4(m_pcEncTop->getDisInter4x4());
     255      pcSlice->setScalingList ( m_pcEncTop->getScalingList()  );
     256      if(m_pcEncTop->getUseScalingListId() == SCALING_LIST_OFF)
     257      {
     258        m_pcEncTop->getTrQuant()->setFlatScalingList();
     259        m_pcEncTop->getTrQuant()->setUseScalingList(false);
     260      }
     261      else if(m_pcEncTop->getUseScalingListId() == SCALING_LIST_DEFAULT)
     262      {
     263        pcSlice->setDefaultScalingList ();
     264        pcSlice->getScalingList()->setScalingListPresentFlag(true);
     265        m_pcEncTop->getTrQuant()->setScalingList(pcSlice->getScalingList());
     266        m_pcEncTop->getTrQuant()->setUseScalingList(true);
     267      }
     268      else if(m_pcEncTop->getUseScalingListId() == SCALING_LIST_FILE_READ)
     269      {
     270        if(pcSlice->getScalingList()->xParseScalingList(m_pcCfg->getScalingListFile()))
     271        {
     272          pcSlice->setDefaultScalingList ();
     273        }
     274#if SCALING_LIST
     275        pcSlice->getScalingList()->checkDcOfMatrix();
     276#endif
     277        pcSlice->getScalingList()->setScalingListPresentFlag(pcSlice->checkDefaultScalingList());
     278        m_pcEncTop->getTrQuant()->setScalingList(pcSlice->getScalingList());
     279        m_pcEncTop->getTrQuant()->setUseScalingList(true);
     280      }
     281      else
     282      {
     283        printf("error : ScalingList == %d no support\n",m_pcEncTop->getUseScalingListId());
     284        assert(0);
     285      }
     286
     287#if HHI_INTERVIEW_SKIP
     288      if ( m_pcEncTop->getInterViewSkip() )
     289      {
     290        m_pcEncTop->getEncTop()->getUsedPelsMap( pcPic->getViewId(), pcPic->getPOC(), pcPic->getUsedPelsMap() );
     291      }
     292#endif
     293      //  Slice info. refinement
     294      if( pcSlice->getSliceType() == B_SLICE )
     295      {
     296        if( m_pcCfg->getGOPEntry( (getNalUnitType(uiPOCCurr) == NAL_UNIT_CODED_SLICE_IDV) ? MAX_GOP : iGOPid ).m_sliceType == 'P' ) { pcSlice->setSliceType( P_SLICE ); }
     297      }
     298
    164299      // Set the nal unit type
    165       if( pcSlice->getPOC() == 0 )
    166         pcSlice->setNalUnitType( NAL_UNIT_CODED_SLICE_IDR );
     300      pcSlice->setNalUnitType( getNalUnitType(uiPOCCurr) );
     301      pcSlice->setNalUnitTypeBaseViewMvc( getNalUnitTypeBaseViewMvc(uiPOCCurr) );
     302
     303      // Do decoding refresh marking if any
     304      pcSlice->decodingRefreshMarking(m_pocCRA, m_bRefreshPending, rcListPic);
     305
     306      if ( !pcSlice->getPPS()->getEnableTMVPFlag() && pcPic->getTLayer() == 0 )
     307      {
     308        pcSlice->decodingMarkingForNoTMVP( rcListPic, pcSlice->getPOC() );
     309      }
     310
     311      m_pcEncTop->selectReferencePictureSet(pcSlice, uiPOCCurr, iGOPid,rcListPic);
     312      pcSlice->getRPS()->setNumberOfLongtermPictures(0);
     313
     314      if(pcSlice->checkThatAllRefPicsAreAvailable(rcListPic, pcSlice->getRPS(), false) != 0)
     315      {
     316         pcSlice->createExplicitReferencePictureSetFromReference(rcListPic, pcSlice->getRPS());
     317      }
     318      pcSlice->applyReferencePictureSet(rcListPic, pcSlice->getRPS());
     319
     320#if H0566_TLA && H0566_TLA_SET_FOR_SWITCHING_POINTS
     321      if(pcSlice->getTLayer() > 0)
     322      {
     323        if(pcSlice->isTemporalLayerSwitchingPoint(rcListPic, pcSlice->getRPS()))
     324        {
     325          pcSlice->setNalUnitType(NAL_UNIT_CODED_SLICE_TLA);
     326        }
     327      }
     328#endif
     329
     330      pcSlice->setNumRefIdx( REF_PIC_LIST_0, min( m_pcCfg->getGOPEntry( (getNalUnitType(uiPOCCurr) == NAL_UNIT_CODED_SLICE_IDV) ? MAX_GOP : iGOPid ).m_numRefPicsActive, (pcSlice->getRPS()->getNumberOfPictures() + pcSlice->getSPS()->getNumberOfUsableInterViewRefs()) ) );
     331      pcSlice->setNumRefIdx( REF_PIC_LIST_1, min( m_pcCfg->getGOPEntry( (getNalUnitType(uiPOCCurr) == NAL_UNIT_CODED_SLICE_IDV) ? MAX_GOP : iGOPid ).m_numRefPicsActive, (pcSlice->getRPS()->getNumberOfPictures() + pcSlice->getSPS()->getNumberOfUsableInterViewRefs()) ) );
     332
     333      TComRefPicListModification* refPicListModification = pcSlice->getRefPicListModification();
     334      refPicListModification->setRefPicListModificationFlagL0( false );
     335#if !H0137_0138_LIST_MODIFICATION
     336      refPicListModification->setNumberOfRefPicListModificationsL0(0);
     337#endif
     338      refPicListModification->setRefPicListModificationFlagL1( false );
     339#if !H0137_0138_LIST_MODIFICATION
     340      refPicListModification->setNumberOfRefPicListModificationsL1(0);
     341#endif
     342      xSetRefPicListModificationsMvc( pcSlice, uiPOCCurr, iGOPid );
     343
     344#if ADAPTIVE_QP_SELECTION
     345      pcSlice->setTrQuant( m_pcEncTop->getTrQuant() );
     346#endif     
     347      //  Set reference list
     348      TAppEncTop* tAppEncTop = m_pcEncTop->getEncTop();
     349      assert( tAppEncTop != NULL );
     350
     351      TComPic * const pcTexturePic = m_pcEncTop->getIsDepth() ? tAppEncTop->getPicFromView( m_pcEncTop->getViewId(), pcSlice->getPOC(), false ) : NULL;
     352      assert( !m_pcEncTop->getIsDepth() || pcTexturePic != NULL );
     353      pcSlice->setTexturePic( pcTexturePic );
     354
     355      std::vector<TComPic*> apcInterViewRefPics = tAppEncTop->getInterViewRefPics( m_pcEncTop->getViewId(), pcSlice->getPOC(), m_pcEncTop->getIsDepth(), pcSlice->getSPS() );
     356      pcSlice->setRefPicListMvc( rcListPic, apcInterViewRefPics );
     357
     358      //  Slice info. refinement
     359      if( pcSlice->getSliceType() == B_SLICE )
     360      {
     361        if( m_pcCfg->getGOPEntry( (getNalUnitType(uiPOCCurr) == NAL_UNIT_CODED_SLICE_IDV) ? MAX_GOP : iGOPid ).m_sliceType == 'P' ) { pcSlice->setSliceType( P_SLICE ); }
     362      }
     363     
     364      if (pcSlice->getSliceType() != B_SLICE || !pcSlice->getSPS()->getUseLComb())
     365      {
     366        pcSlice->setNumRefIdx(REF_PIC_LIST_C, 0);
     367        pcSlice->setRefPicListCombinationFlag(false);
     368        pcSlice->setRefPicListModificationFlagLC(false);
     369      }
    167370      else
    168         pcSlice->setNalUnitType( NAL_UNIT_CODED_SLICE );
    169 
    170       //pcSlice->setNalUnitType(getNalUnitType(uiPOCCurr));
    171       // Do decoding refresh marking if any
    172       pcSlice->decodingRefreshMarking(m_uiPOCCDR, m_bRefreshPending, rcListPic);
    173 #endif
    174 
    175 // GT FIX
    176   std::vector<TComPic*> apcSpatRefPics = m_pcEncTop->getEncTop()->getSpatialRefPics( pcPic->getViewIdx(), pcSlice->getPOC(), m_pcEncTop->isDepthCoder() );
    177   TComPic * const pcTexturePic = m_pcEncTop->isDepthCoder() ? m_pcEncTop->getEncTop()->getPicFromView( pcPic->getViewIdx(), pcSlice->getPOC(), false ) : NULL;
    178   assert( ! m_pcEncTop->isDepthCoder() || pcTexturePic != NULL );
    179   pcSlice->setTexturePic( pcTexturePic );
    180 
    181   pcSlice->setRefPicListFromGOPSTring( rcListPic, apcSpatRefPics );
    182 
     371      {
     372        pcSlice->setRefPicListCombinationFlag(pcSlice->getSPS()->getUseLComb());
     373        pcSlice->setRefPicListModificationFlagLC(pcSlice->getSPS()->getLCMod());
     374        pcSlice->setNumRefIdx(REF_PIC_LIST_C, pcSlice->getNumRefIdx(REF_PIC_LIST_0));
     375      }
     376     
     377      if (pcSlice->getSliceType() == B_SLICE)
     378      {
     379        pcSlice->setColDir(uiColDir);
     380        Bool bLowDelay = true;
     381        Int  iCurrPOC  = pcSlice->getPOC();
     382        Int iRefIdx = 0;
     383
     384        for (iRefIdx = 0; iRefIdx < pcSlice->getNumRefIdx(REF_PIC_LIST_0) && bLowDelay; iRefIdx++)
     385        {
     386          if ( pcSlice->getRefPic(REF_PIC_LIST_0, iRefIdx)->getPOC() > iCurrPOC )
     387          {
     388            bLowDelay = false;
     389          }
     390        }
     391        for (iRefIdx = 0; iRefIdx < pcSlice->getNumRefIdx(REF_PIC_LIST_1) && bLowDelay; iRefIdx++)
     392        {
     393          if ( pcSlice->getRefPic(REF_PIC_LIST_1, iRefIdx)->getPOC() > iCurrPOC )
     394          {
     395            bLowDelay = false;
     396          }
     397        }
     398
     399        pcSlice->setCheckLDC(bLowDelay); 
     400      }
     401     
     402      uiColDir = 1-uiColDir;
     403     
     404      //-------------------------------------------------------------
     405      pcSlice->setRefPOCnViewListsMvc();
     406     
     407      pcSlice->setNoBackPredFlag( false );
     408      if ( pcSlice->getSliceType() == B_SLICE && !pcSlice->getRefPicListCombinationFlag())
     409      {
     410        if ( pcSlice->getNumRefIdx(RefPicList( 0 ) ) == pcSlice->getNumRefIdx(RefPicList( 1 ) ) )
     411        {
     412          pcSlice->setNoBackPredFlag( true );
     413          int i;
     414          for ( i=0; i < pcSlice->getNumRefIdx(RefPicList( 1 ) ); i++ )
     415          {
     416            if ( pcSlice->getRefPOC(RefPicList(1), i) != pcSlice->getRefPOC(RefPicList(0), i) )
     417            {
     418              pcSlice->setNoBackPredFlag( false );
     419              break;
     420            }
     421          }
     422        }
     423      }
     424
     425      if(pcSlice->getNoBackPredFlag())
     426      {
     427        pcSlice->setNumRefIdx(REF_PIC_LIST_C, 0);
     428      }
     429      pcSlice->generateCombinedList();
     430     
    183431#if HHI_VSO
    184   m_pcEncTop->getEncTop()->setMVDPic(pcPic->getViewIdx(), pcSlice->getPOC(), pcPic->getMVDReferenceInfo() );
    185 
    186 
    187432  Bool bUseVSO = m_pcEncTop->getUseVSO();
    188433  m_pcRdCost->setUseVSO( bUseVSO );
     
    198443    if ( iVSOMode == 4 )
    199444    {
    200       m_pcEncTop->getEncTop()->setupRenModel( pcSlice->getPOC(), pcPic->getViewIdx(), m_pcEncTop->isDepthCoder() ? 1 : 0 );
     445      m_pcEncTop->getEncTop()->setupRenModel( pcSlice->getPOC(), pcSlice->getViewId(), m_pcEncTop->isDepthCoder() ? 1 : 0 );
    201446    }
    202447    else
    203   {
    204     m_pcRdCost->setRefDataFromMVDInfo( pcPic->getMVDReferenceInfo() );
    205   }
    206   }
    207 #endif
    208 
    209 #if HHI_INTERVIEW_SKIP
    210   if ( m_pcEncTop->getInterViewSkip() )
    211   {
    212     m_pcEncTop->getEncTop()->getUsedPelsMap( pcPic->getViewIdx(), pcPic->getPOC(), pcPic->getUsedPelsMap() );
    213   }
    214 #endif
    215 
    216       pcSlice->setNoBackPredFlag( false );
    217 #if DCM_COMB_LIST
    218       if ( pcSlice->getSliceType() == B_SLICE && !pcSlice->getRefPicListCombinationFlag())
    219 #else
    220       if ( pcSlice->getSliceType() == B_SLICE )
    221 #endif
    222       {
    223         if ( pcSlice->getNumRefIdx(RefPicList( 0 ) ) == pcSlice->getNumRefIdx(RefPicList( 1 ) ) )
    224         {
    225           pcSlice->setNoBackPredFlag( true );
    226           int i;
    227           for ( i=0; i < pcSlice->getNumRefIdx(RefPicList( 1 ) ); i++ )
    228           {
    229             if ( pcSlice->getRefPOC(RefPicList(1), i) != pcSlice->getRefPOC(RefPicList(0), i) )
    230             {
    231               pcSlice->setNoBackPredFlag( false );
    232               break;
    233             }
    234           }
    235         }
    236       }
    237 
    238 #if DCM_COMB_LIST
    239       if(pcSlice->getNoBackPredFlag())
    240       {
    241         pcSlice->setNumRefIdx(REF_PIC_LIST_C, -1);
    242       }
    243       pcSlice->generateCombinedList();
    244 #endif
    245 
     448    {
     449      AOT(true);
     450    }
     451  }
     452#endif
    246453      /////////////////////////////////////////////////////////////////////////////////////////////////// Compress a slice
    247454      //  Slice compression
     
    250457        m_pcSliceEncoder->setSearchRange(pcSlice);
    251458      }
    252 #ifdef ROUNDING_CONTROL_BIPRED
    253       Bool b = true;
    254       if (m_pcCfg->getUseRoundingControlBipred())
    255       {
    256         if (m_pcCfg->getCodedPictureBufferSize()==1)
    257           b = ((pcSlice->getPOC()&1)==0);
    258         else
    259           b = (pcSlice->isReferenced() == 0);
    260       }
    261 
    262 #if HIGH_ACCURACY_BI
    263       pcSlice->setRounding(false);
    264 #else
    265       pcSlice->setRounding(b);
    266 #endif
    267 #endif
     459
     460#if H0111_MVD_L1_ZERO
     461      Bool bGPBcheck=false;
     462      if ( pcSlice->getSliceType() == B_SLICE)
     463      {
     464        if ( pcSlice->getNumRefIdx(RefPicList( 0 ) ) == pcSlice->getNumRefIdx(RefPicList( 1 ) ) )
     465        {
     466          bGPBcheck=true;
     467          int i;
     468          for ( i=0; i < pcSlice->getNumRefIdx(RefPicList( 1 ) ); i++ )
     469          {
     470            if ( pcSlice->getRefPOC(RefPicList(1), i) != pcSlice->getRefPOC(RefPicList(0), i) )
     471            {
     472              bGPBcheck=false;
     473              break;
     474            }
     475          }
     476        }
     477      }
     478      if(bGPBcheck)
     479      {
     480        pcSlice->setMvdL1ZeroFlag(true);
     481      }
     482      else
     483      {
     484        pcSlice->setMvdL1ZeroFlag(false);
     485      }
     486      pcPic->getSlice(pcSlice->getSliceIdx())->setMvdL1ZeroFlag(pcSlice->getMvdL1ZeroFlag());
     487#endif
     488
     489      UInt uiNumSlices = 1;
     490
     491      UInt uiInternalAddress = pcPic->getNumPartInCU()-4;
     492      UInt uiExternalAddress = pcPic->getPicSym()->getNumberOfCUsInFrame()-1;
     493      UInt uiPosX = ( uiExternalAddress % pcPic->getFrameWidthInCU() ) * g_uiMaxCUWidth+ g_auiRasterToPelX[ g_auiZscanToRaster[uiInternalAddress] ];
     494      UInt uiPosY = ( uiExternalAddress / pcPic->getFrameWidthInCU() ) * g_uiMaxCUHeight+ g_auiRasterToPelY[ g_auiZscanToRaster[uiInternalAddress] ];
     495      UInt uiWidth = pcSlice->getSPS()->getPicWidthInLumaSamples();
     496      UInt uiHeight = pcSlice->getSPS()->getPicHeightInLumaSamples();
     497      while(uiPosX>=uiWidth||uiPosY>=uiHeight)
     498      {
     499        uiInternalAddress--;
     500        uiPosX = ( uiExternalAddress % pcPic->getFrameWidthInCU() ) * g_uiMaxCUWidth+ g_auiRasterToPelX[ g_auiZscanToRaster[uiInternalAddress] ];
     501        uiPosY = ( uiExternalAddress / pcPic->getFrameWidthInCU() ) * g_uiMaxCUHeight+ g_auiRasterToPelY[ g_auiZscanToRaster[uiInternalAddress] ];
     502      }
     503      uiInternalAddress++;
     504      if(uiInternalAddress==pcPic->getNumPartInCU())
     505      {
     506        uiInternalAddress = 0;
     507        uiExternalAddress++;
     508      }
     509      UInt uiRealEndAddress = uiExternalAddress*pcPic->getNumPartInCU()+uiInternalAddress;
     510
     511    UInt uiCummulativeTileWidth;
     512    UInt uiCummulativeTileHeight;
     513    Int  p, j;
     514    UInt uiEncCUAddr;
     515   
     516#if !REMOVE_TILE_DEPENDENCE
     517    if(pcSlice->getPPS()->getTileBehaviorControlPresentFlag() == 1)
     518    {
     519      pcPic->getPicSym()->setTileBoundaryIndependenceIdr( pcSlice->getPPS()->getTileBoundaryIndependenceIdr() );
     520    }
     521    else
     522    {
     523      pcPic->getPicSym()->setTileBoundaryIndependenceIdr( pcSlice->getPPS()->getSPS()->getTileBoundaryIndependenceIdr() );
     524
     525    }
     526#endif
     527
     528    if( pcSlice->getPPS()->getColumnRowInfoPresent() == 1 )    //derive the tile parameters from PPS
     529    {
     530      //set NumColumnsMinus1 and NumRowsMinus1
     531      pcPic->getPicSym()->setNumColumnsMinus1( pcSlice->getPPS()->getNumColumnsMinus1() );
     532      pcPic->getPicSym()->setNumRowsMinus1( pcSlice->getPPS()->getNumRowsMinus1() );
     533
     534      //create the TComTileArray
     535      pcPic->getPicSym()->xCreateTComTileArray();
     536
     537      if( pcSlice->getPPS()->getUniformSpacingIdr() == 1 )
     538      {
     539        //set the width for each tile
     540        for(j=0; j < pcPic->getPicSym()->getNumRowsMinus1()+1; j++)
     541        {
     542          for(p=0; p < pcPic->getPicSym()->getNumColumnsMinus1()+1; p++)
     543          {
     544            pcPic->getPicSym()->getTComTile( j * (pcPic->getPicSym()->getNumColumnsMinus1()+1) + p )->
     545              setTileWidth( (p+1)*pcPic->getPicSym()->getFrameWidthInCU()/(pcPic->getPicSym()->getNumColumnsMinus1()+1)
     546              - (p*pcPic->getPicSym()->getFrameWidthInCU())/(pcPic->getPicSym()->getNumColumnsMinus1()+1) );
     547          }
     548        }
     549
     550        //set the height for each tile
     551        for(j=0; j < pcPic->getPicSym()->getNumColumnsMinus1()+1; j++)
     552        {
     553          for(p=0; p < pcPic->getPicSym()->getNumRowsMinus1()+1; p++)
     554          {
     555            pcPic->getPicSym()->getTComTile( p * (pcPic->getPicSym()->getNumColumnsMinus1()+1) + j )->
     556              setTileHeight( (p+1)*pcPic->getPicSym()->getFrameHeightInCU()/(pcPic->getPicSym()->getNumRowsMinus1()+1)
     557              - (p*pcPic->getPicSym()->getFrameHeightInCU())/(pcPic->getPicSym()->getNumRowsMinus1()+1) );   
     558          }
     559        }
     560      }
     561      else
     562      {
     563        //set the width for each tile
     564        for(j=0; j < pcPic->getPicSym()->getNumRowsMinus1()+1; j++)
     565        {
     566          uiCummulativeTileWidth = 0;
     567          for(p=0; p < pcPic->getPicSym()->getNumColumnsMinus1(); p++)
     568          {
     569            pcPic->getPicSym()->getTComTile( j * (pcPic->getPicSym()->getNumColumnsMinus1()+1) + p )->setTileWidth( pcSlice->getPPS()->getColumnWidth(p) );
     570            uiCummulativeTileWidth += pcSlice->getPPS()->getColumnWidth(p);
     571          }
     572          pcPic->getPicSym()->getTComTile(j * (pcPic->getPicSym()->getNumColumnsMinus1()+1) + p)->setTileWidth( pcPic->getPicSym()->getFrameWidthInCU()-uiCummulativeTileWidth );
     573        }
     574
     575        //set the height for each tile
     576        for(j=0; j < pcPic->getPicSym()->getNumColumnsMinus1()+1; j++)
     577        {
     578          uiCummulativeTileHeight = 0;
     579          for(p=0; p < pcPic->getPicSym()->getNumRowsMinus1(); p++)
     580          {
     581            pcPic->getPicSym()->getTComTile( p * (pcPic->getPicSym()->getNumColumnsMinus1()+1) + j )->setTileHeight( pcSlice->getPPS()->getRowHeight(p) );
     582            uiCummulativeTileHeight += pcSlice->getPPS()->getRowHeight(p);
     583          }
     584          pcPic->getPicSym()->getTComTile(p * (pcPic->getPicSym()->getNumColumnsMinus1()+1) + j)->setTileHeight( pcPic->getPicSym()->getFrameHeightInCU()-uiCummulativeTileHeight );
     585        }
     586      }
     587    }
     588    else //derive the tile parameters from SPS
     589    {
     590      //set NumColumnsMins1 and NumRowsMinus1
     591      pcPic->getPicSym()->setNumColumnsMinus1( pcSlice->getSPS()->getNumColumnsMinus1() );
     592      pcPic->getPicSym()->setNumRowsMinus1( pcSlice->getSPS()->getNumRowsMinus1() );
     593
     594      //create the TComTileArray
     595      pcPic->getPicSym()->xCreateTComTileArray();
     596
     597      if( pcSlice->getSPS()->getUniformSpacingIdr() == 1 )
     598      {
     599        //set the width for each tile
     600        for(j=0; j < pcPic->getPicSym()->getNumRowsMinus1()+1; j++)
     601        {
     602          for(p=0; p < pcPic->getPicSym()->getNumColumnsMinus1()+1; p++)
     603          {
     604            pcPic->getPicSym()->getTComTile( j * (pcPic->getPicSym()->getNumColumnsMinus1()+1) + p )->
     605              setTileWidth( (p+1)*pcPic->getPicSym()->getFrameWidthInCU()/(pcPic->getPicSym()->getNumColumnsMinus1()+1)
     606              - (p*pcPic->getPicSym()->getFrameWidthInCU())/(pcPic->getPicSym()->getNumColumnsMinus1()+1) );
     607          }
     608        }
     609
     610        //set the height for each tile
     611        for(j=0; j < pcPic->getPicSym()->getNumColumnsMinus1()+1; j++)
     612        {
     613          for(p=0; p < pcPic->getPicSym()->getNumRowsMinus1()+1; p++)
     614          {
     615            pcPic->getPicSym()->getTComTile( p * (pcPic->getPicSym()->getNumColumnsMinus1()+1) + j )->
     616              setTileHeight( (p+1)*pcPic->getPicSym()->getFrameHeightInCU()/(pcPic->getPicSym()->getNumRowsMinus1()+1)
     617              - (p*pcPic->getPicSym()->getFrameHeightInCU())/(pcPic->getPicSym()->getNumRowsMinus1()+1) );   
     618          }
     619        }
     620      }
     621
     622      else
     623      {
     624        //set the width for each tile
     625        for(j=0; j < pcPic->getPicSym()->getNumRowsMinus1()+1; j++)
     626        {
     627          uiCummulativeTileWidth = 0;
     628          for(p=0; p < pcPic->getPicSym()->getNumColumnsMinus1(); p++)
     629          {
     630            pcPic->getPicSym()->getTComTile( j * (pcPic->getPicSym()->getNumColumnsMinus1()+1) + p )->setTileWidth( pcSlice->getSPS()->getColumnWidth(p) );
     631            uiCummulativeTileWidth += pcSlice->getSPS()->getColumnWidth(p);
     632          }
     633          pcPic->getPicSym()->getTComTile(j * (pcPic->getPicSym()->getNumColumnsMinus1()+1) + p)->setTileWidth( pcPic->getPicSym()->getFrameWidthInCU()-uiCummulativeTileWidth );
     634        }
     635
     636        //set the height for each tile
     637        for(j=0; j < pcPic->getPicSym()->getNumColumnsMinus1()+1; j++)
     638        {
     639          uiCummulativeTileHeight = 0;
     640          for(p=0; p < pcPic->getPicSym()->getNumRowsMinus1(); p++)
     641          {
     642            pcPic->getPicSym()->getTComTile( p * (pcPic->getPicSym()->getNumColumnsMinus1()+1) + j )->setTileHeight( pcSlice->getSPS()->getRowHeight(p) );
     643            uiCummulativeTileHeight += pcSlice->getSPS()->getRowHeight(p);
     644          }
     645          pcPic->getPicSym()->getTComTile(p * (pcPic->getPicSym()->getNumColumnsMinus1()+1) + j)->setTileHeight( pcPic->getPicSym()->getFrameHeightInCU()-uiCummulativeTileHeight );
     646        }
     647      }
     648    }
     649
     650    //initialize each tile of the current picture
     651    pcPic->getPicSym()->xInitTiles();
     652
     653    // Allocate some coders, now we know how many tiles there are.
     654    Int iNumSubstreams = pcSlice->getPPS()->getNumSubstreams();
     655   
     656    //generate the Coding Order Map and Inverse Coding Order Map
     657    for(p=0, uiEncCUAddr=0; p<pcPic->getPicSym()->getNumberOfCUsInFrame(); p++, uiEncCUAddr = pcPic->getPicSym()->xCalculateNxtCUAddr(uiEncCUAddr))
     658    {
     659      pcPic->getPicSym()->setCUOrderMap(p, uiEncCUAddr);
     660      pcPic->getPicSym()->setInverseCUOrderMap(uiEncCUAddr, p);
     661    }
     662    pcPic->getPicSym()->setCUOrderMap(pcPic->getPicSym()->getNumberOfCUsInFrame(), pcPic->getPicSym()->getNumberOfCUsInFrame());   
     663    pcPic->getPicSym()->setInverseCUOrderMap(pcPic->getPicSym()->getNumberOfCUsInFrame(), pcPic->getPicSym()->getNumberOfCUsInFrame());
     664    if (pcSlice->getPPS()->getEntropyCodingMode())
     665    {
     666      // Allocate some coders, now we know how many tiles there are.
     667      m_pcEncTop->createWPPCoders(iNumSubstreams);
     668      pcSbacCoders = m_pcEncTop->getSbacCoders();
     669      pcSubstreamsOut = new TComOutputBitstream[iNumSubstreams];
     670    }
    268671
    269672      UInt uiStartCUAddrSliceIdx = 0; // used to index "m_uiStoredStartCUAddrForEncodingSlice" containing locations of slice boundaries
    270673      UInt uiStartCUAddrSlice    = 0; // used to keep track of current slice's starting CU addr.
    271674      pcSlice->setSliceCurStartCUAddr( uiStartCUAddrSlice ); // Setting "start CU addr" for current slice
    272       memset(m_uiStoredStartCUAddrForEncodingSlice, 0, sizeof(UInt) * (pcPic->getPicSym()->getNumberOfCUsInFrame()+1));
     675      memset(m_uiStoredStartCUAddrForEncodingSlice, 0, sizeof(UInt) * (pcPic->getPicSym()->getNumberOfCUsInFrame()*pcPic->getNumPartInCU()+1));
    273676
    274677      UInt uiStartCUAddrEntropySliceIdx = 0; // used to index "m_uiStoredStartCUAddrForEntropyEncodingSlice" containing locations of slice boundaries
    275678      UInt uiStartCUAddrEntropySlice    = 0; // used to keep track of current Entropy slice's starting CU addr.
    276679      pcSlice->setEntropySliceCurStartCUAddr( uiStartCUAddrEntropySlice ); // Setting "start CU addr" for current Entropy slice
    277       memset(m_uiStoredStartCUAddrForEncodingEntropySlice, 0, sizeof(UInt) * (pcPic->getPicSym()->getNumberOfCUsInFrame()+1));
    278 
     680     
     681      memset(m_uiStoredStartCUAddrForEncodingEntropySlice, 0, sizeof(UInt) * (pcPic->getPicSym()->getNumberOfCUsInFrame()*pcPic->getNumPartInCU()+1));
    279682      UInt uiNextCUAddr = 0;
    280683      m_uiStoredStartCUAddrForEncodingSlice[uiStartCUAddrSliceIdx++]                = uiNextCUAddr;
     
    285688      m_pcDepthMapGenerator->initViewComponent( pcPic );
    286689      m_pcDepthMapGenerator->predictDepthMap  ( pcPic );
     690#endif
    287691#if HHI_INTER_VIEW_MOTION_PRED
    288692      m_pcDepthMapGenerator->covertOrgDepthMap( pcPic );
     
    291695      m_pcResidualGenerator->initViewComponent( pcPic );
    292696#endif
    293 #endif
    294 
    295       while(uiNextCUAddr<pcPic->getPicSym()->getNumberOfCUsInFrame()) // determine slice boundaries
     697
     698      while(uiNextCUAddr<uiRealEndAddress) // determine slice boundaries
    296699      {
    297700        pcSlice->setNextSlice       ( false );
     
    312715            m_uiStoredStartCUAddrForEncodingEntropySlice[uiStartCUAddrEntropySliceIdx++]  = uiStartCUAddrSlice;
    313716          }
    314 
    315           if (uiStartCUAddrSlice < pcPic->getPicSym()->getNumberOfCUsInFrame())
    316           {
    317             pcPic->allocateNewSlice();
     717         
     718          if (uiStartCUAddrSlice < uiRealEndAddress)
     719          {
     720            pcPic->allocateNewSlice();         
    318721            pcPic->setCurrSliceIdx                  ( uiStartCUAddrSliceIdx-1 );
    319722            m_pcSliceEncoder->setSliceIdx           ( uiStartCUAddrSliceIdx-1 );
     
    324727            pcSlice->setEntropySliceCurStartCUAddr  ( uiStartCUAddrSlice      );
    325728            pcSlice->setSliceBits(0);
     729            uiNumSlices ++;
    326730          }
    327731        }
     
    336740          uiStartCUAddrSlice                                                            = pcSlice->getSliceCurEndCUAddr();
    337741          uiStartCUAddrEntropySlice                                                     = pcSlice->getEntropySliceCurEndCUAddr();
    338         }
     742        }       
    339743
    340744        uiNextCUAddr = (uiStartCUAddrSlice > uiStartCUAddrEntropySlice) ? uiStartCUAddrSlice : uiStartCUAddrEntropySlice;
     
    342746      m_uiStoredStartCUAddrForEncodingSlice[uiStartCUAddrSliceIdx++]                = pcSlice->getSliceCurEndCUAddr();
    343747      m_uiStoredStartCUAddrForEncodingEntropySlice[uiStartCUAddrEntropySliceIdx++]  = pcSlice->getSliceCurEndCUAddr();
    344 
     748     
    345749      pcSlice = pcPic->getSlice(0);
    346 #if MTK_SAO  // PRE_DF
    347       SAOParam cSaoParam;
    348 #endif
    349750
    350751#if HHI_INTER_VIEW_RESIDUAL_PRED
     
    358759
    359760      //-- Loop filter
    360       m_pcLoopFilter->setCfg(pcSlice->getLoopFilterDisable(), m_pcCfg->getLoopFilterAlphaC0Offget(), m_pcCfg->getLoopFilterBetaOffget());
     761      Bool bLFCrossTileBoundary = (pcSlice->getPPS()->getTileBehaviorControlPresentFlag() == 1)?
     762                                  (pcSlice->getPPS()->getLFCrossTileBoundaryFlag()):(pcSlice->getPPS()->getSPS()->getLFCrossTileBoundaryFlag());
     763#if DBL_CONTROL
     764      m_pcLoopFilter->setCfg(pcSlice->getPPS()->getDeblockingFilterControlPresent(), pcSlice->getLoopFilterDisable(), pcSlice->getLoopFilterBetaOffset(), pcSlice->getLoopFilterTcOffset(), bLFCrossTileBoundary);
     765#else
     766      m_pcLoopFilter->setCfg(pcSlice->getLoopFilterDisable(), pcSlice->getLoopFilterBetaOffset(), pcSlice->getLoopFilterTcOffset(), bLFCrossTileBoundary);
     767#endif
    361768      m_pcLoopFilter->loopFilterPic( pcPic );
    362769
    363 #if MTK_NONCROSS_INLOOP_FILTER
    364770      pcSlice = pcPic->getSlice(0);
     771      if(pcSlice->getSPS()->getUseSAO() || pcSlice->getSPS()->getUseALF())
     772      {
     773        Int sliceGranularity = pcSlice->getPPS()->getSliceGranularity();
     774        pcPic->createNonDBFilterInfo(m_uiStoredStartCUAddrForEncodingSlice, uiNumSlices, sliceGranularity, pcSlice->getSPS()->getLFCrossSliceBoundaryFlag(),pcPic->getPicSym()->getNumTiles() ,bLFCrossTileBoundary);
     775      }
     776
     777
     778      pcSlice = pcPic->getSlice(0);
     779
     780      if(pcSlice->getSPS()->getUseSAO())
     781      {
     782        m_pcSAO->createPicSaoInfo(pcPic, uiNumSlices);
     783      }
     784
     785#if LCU_SYNTAX_ALF
     786      AlfParamSet* alfSliceParams = NULL;
     787      std::vector<AlfCUCtrlInfo>* alfCUCtrlParam = NULL;
     788#else
     789      std::vector<AlfCUCtrlInfo> vAlfCUCtrlParam;
     790#endif
     791      pcSlice = pcPic->getSlice(0);
    365792
    366793      if(pcSlice->getSPS()->getUseALF())
    367794      {
    368         if(pcSlice->getSPS()->getLFCrossSliceBoundaryFlag())
    369         {
    370           m_pcAdaptiveLoopFilter->setUseNonCrossAlf(false);
    371         }
    372         else
    373         {
    374           UInt uiNumSlices = uiStartCUAddrSliceIdx-1;
    375           m_pcAdaptiveLoopFilter->setUseNonCrossAlf( (uiNumSlices > 1)  );
    376           if(m_pcAdaptiveLoopFilter->getUseNonCrossAlf())
    377           {
    378             m_pcAdaptiveLoopFilter->setNumSlicesInPic( uiNumSlices );
    379             m_pcAdaptiveLoopFilter->createSlice();
    380 
    381             //set the startLCU and endLCU addr. to ALF slices
    382             for(UInt i=0; i< uiNumSlices ; i++)
    383             {
    384               (*m_pcAdaptiveLoopFilter)[i].create(pcPic, i,
    385                                                   m_uiStoredStartCUAddrForEncodingSlice[i],
    386                                                   m_uiStoredStartCUAddrForEncodingSlice[i+1]-1
    387                                                   );
    388 
    389             }
    390           }
    391         }
    392       }
    393 #endif
     795#if LCU_SYNTAX_ALF
     796        m_pcAdaptiveLoopFilter->createPicAlfInfo(pcPic, uiNumSlices, pcSlice->getSliceQp());
     797        m_pcAdaptiveLoopFilter->initALFEnc(m_pcCfg->getALFParamInSlice(), m_pcCfg->getALFPicBasedEncode(), uiNumSlices, alfSliceParams, alfCUCtrlParam);
     798#else
     799        vAlfCUCtrlParam.resize(uiNumSlices);
     800        m_pcAdaptiveLoopFilter->createPicAlfInfo(pcPic, uiNumSlices);
     801#endif
     802      }
     803
    394804      /////////////////////////////////////////////////////////////////////////////////////////////////// File writing
    395805      // Set entropy coder
    396806      m_pcEntropyCoder->setEntropyCoder   ( m_pcCavlcCoder, pcSlice );
    397807
    398       /* write various header sets.
    399        * The header sets are written into a separate bitstream buffer to
    400        * allow SEI messages that are calculated after the picture has been
    401        * encoded to be sent before the picture.
    402        */
    403       TComBitstream bs_SPS_PPS_SEI;
    404       bs_SPS_PPS_SEI.create(512); /* TODO: this should dynamically resize */
    405       if ( rbSeqFirst )
    406       {
    407         m_pcEntropyCoder->setBitstream(&bs_SPS_PPS_SEI);
    408 
    409         m_pcEntropyCoder->encodeSPS( pcSlice->getSPS() );
    410         bs_SPS_PPS_SEI.write( 1, 1 );
    411         bs_SPS_PPS_SEI.writeAlignZero();
    412         // generate start code
    413         bs_SPS_PPS_SEI.write( 1, 32);
    414 
    415         m_pcEntropyCoder->encodePPS( pcSlice->getPPS() );
    416         bs_SPS_PPS_SEI.write( 1, 1 );
    417         bs_SPS_PPS_SEI.writeAlignZero();
    418         // generate start code
    419         bs_SPS_PPS_SEI.write( 1, 32);
    420         rbSeqFirst = false;
     808      /* write various header sets. */
     809      if ( m_bSeqFirst )
     810      {
     811#if NAL_REF_FLAG
     812        OutputNALUnit nalu(NAL_UNIT_SPS, true, m_pcEncTop->getViewId(), m_pcEncTop->getIsDepth());
     813#else
     814        OutputNALUnit nalu(NAL_UNIT_SPS, NAL_REF_IDC_PRIORITY_HIGHEST, m_pcEncTop->getViewId(), m_pcEncTop->getIsDepth());
     815#endif
     816        m_pcEntropyCoder->setBitstream(&nalu.m_Bitstream);
     817#if TILES_WPP_ENTRY_POINT_SIGNALLING
     818        pcSlice->getSPS()->setNumSubstreams( pcSlice->getPPS()->getNumSubstreams() );
     819#endif
     820#if HHI_MPI
     821        m_pcEntropyCoder->encodeSPS(pcSlice->getSPS(), m_pcEncTop->getIsDepth());
     822#else
     823        m_pcEntropyCoder->encodeSPS(pcSlice->getSPS());
     824#endif
     825        writeRBSPTrailingBits(nalu.m_Bitstream);
     826        accessUnit.push_back(new NALUnitEBSP(nalu));
     827
     828#if NAL_REF_FLAG
     829        nalu = NALUnit(NAL_UNIT_PPS, true, m_pcEncTop->getViewId(), m_pcEncTop->getIsDepth());
     830#else
     831        nalu = NALUnit(NAL_UNIT_PPS, NAL_REF_IDC_PRIORITY_HIGHEST, m_pcEncTop->getViewId(), m_pcEncTop->getIsDepth());
     832#endif
     833        m_pcEntropyCoder->setBitstream(&nalu.m_Bitstream);
     834        m_pcEntropyCoder->encodePPS(pcSlice->getPPS());
     835        writeRBSPTrailingBits(nalu.m_Bitstream);
     836        accessUnit.push_back(new NALUnitEBSP(nalu));
     837
     838        m_bSeqFirst = false;
    421839      }
    422840
    423841      /* use the main bitstream buffer for storing the marshalled picture */
    424       m_pcEntropyCoder->setBitstream(pcBitstreamOut);
     842      m_pcEntropyCoder->setBitstream(NULL);
    425843
    426844      uiStartCUAddrSliceIdx = 0;
    427       uiStartCUAddrSlice    = 0;
    428       pcBitstreamOut->allocateMemoryForSliceLocations( pcPic->getPicSym()->getNumberOfCUsInFrame() ); // Assuming number of slices <= number of LCU. Needs to be changed for sub-LCU slice coding.
    429       pcBitstreamOut->setSliceCount( 0 );                                      // intialize number of slices to zero, used while converting RBSP to NALU
     845      uiStartCUAddrSlice    = 0;
    430846
    431847      uiStartCUAddrEntropySliceIdx = 0;
    432       uiStartCUAddrEntropySlice    = 0;
     848      uiStartCUAddrEntropySlice    = 0; 
    433849      uiNextCUAddr                 = 0;
    434850      pcSlice = pcPic->getSlice(uiStartCUAddrSliceIdx);
    435       while (uiNextCUAddr < pcPic->getPicSym()->getNumberOfCUsInFrame()) // Iterate over all slices
    436       {
     851
     852      Int processingState = (pcSlice->getSPS()->getUseALF() || pcSlice->getSPS()->getUseSAO() || pcSlice->getSPS()->getScalingListFlag() || pcSlice->getSPS()->getUseDF())?(EXECUTE_INLOOPFILTER):(ENCODE_SLICE);
     853
     854      static Int iCurrAPSIdx = 0;
     855      Int iCodedAPSIdx = 0;
     856      TComSlice* pcSliceForAPS = NULL;
     857
     858      bool skippedSlice=false;
     859      while (uiNextCUAddr < uiRealEndAddress) // Iterate over all slices
     860      {
     861        switch(processingState)
     862        {
     863        case ENCODE_SLICE:
     864          {
    437865        pcSlice->setNextSlice       ( false );
    438866        pcSlice->setNextEntropySlice( false );
     
    440868        {
    441869          pcSlice = pcPic->getSlice(uiStartCUAddrSliceIdx);
     870#if COLLOCATED_REF_IDX
     871          if(uiStartCUAddrSliceIdx > 0 && pcSlice->getSliceType()!= I_SLICE)
     872          {
     873            pcSlice->checkColRefIdx(uiStartCUAddrSliceIdx, pcPic);
     874          }
     875#endif
    442876          pcPic->setCurrSliceIdx(uiStartCUAddrSliceIdx);
    443877          m_pcSliceEncoder->setSliceIdx(uiStartCUAddrSliceIdx);
     
    454888          uiStartCUAddrSliceIdx++;
    455889          uiStartCUAddrEntropySliceIdx++;
    456         }
     890        } 
    457891        else if (uiNextCUAddr == m_uiStoredStartCUAddrForEncodingEntropySlice[uiStartCUAddrEntropySliceIdx])
    458892        {
     
    466900        }
    467901
    468         // Get ready for writing slice header (other than the first one in the picture)
    469         if (uiNextCUAddr!=0)
    470         {
     902      pcSlice->setRPS(pcPic->getSlice(0)->getRPS());
     903      pcSlice->setRPSidx(pcPic->getSlice(0)->getRPSidx());
     904        UInt uiDummyStartCUAddr;
     905        UInt uiDummyBoundingCUAddr;
     906        m_pcSliceEncoder->xDetermineStartAndBoundingCUAddr(uiDummyStartCUAddr,uiDummyBoundingCUAddr,pcPic,true);
     907
     908        uiInternalAddress = pcPic->getPicSym()->getPicSCUAddr(pcSlice->getEntropySliceCurEndCUAddr()-1) % pcPic->getNumPartInCU();
     909        uiExternalAddress = pcPic->getPicSym()->getPicSCUAddr(pcSlice->getEntropySliceCurEndCUAddr()-1) / pcPic->getNumPartInCU();
     910        uiPosX = ( uiExternalAddress % pcPic->getFrameWidthInCU() ) * g_uiMaxCUWidth+ g_auiRasterToPelX[ g_auiZscanToRaster[uiInternalAddress] ];
     911        uiPosY = ( uiExternalAddress / pcPic->getFrameWidthInCU() ) * g_uiMaxCUHeight+ g_auiRasterToPelY[ g_auiZscanToRaster[uiInternalAddress] ];
     912        uiWidth = pcSlice->getSPS()->getPicWidthInLumaSamples();
     913        uiHeight = pcSlice->getSPS()->getPicHeightInLumaSamples();
     914        while(uiPosX>=uiWidth||uiPosY>=uiHeight)
     915        {
     916          uiInternalAddress--;
     917          uiPosX = ( uiExternalAddress % pcPic->getFrameWidthInCU() ) * g_uiMaxCUWidth+ g_auiRasterToPelX[ g_auiZscanToRaster[uiInternalAddress] ];
     918          uiPosY = ( uiExternalAddress / pcPic->getFrameWidthInCU() ) * g_uiMaxCUHeight+ g_auiRasterToPelY[ g_auiZscanToRaster[uiInternalAddress] ];
     919        }
     920        uiInternalAddress++;
     921        if(uiInternalAddress==pcPic->getNumPartInCU())
     922        {
     923          uiInternalAddress = 0;
     924          uiExternalAddress = pcPic->getPicSym()->getCUOrderMap(pcPic->getPicSym()->getInverseCUOrderMap(uiExternalAddress)+1);
     925        }
     926        UInt uiEndAddress = pcPic->getPicSym()->getPicSCUEncOrder(uiExternalAddress*pcPic->getNumPartInCU()+uiInternalAddress);
     927        if(uiEndAddress<=pcSlice->getEntropySliceCurStartCUAddr()) {
     928          UInt uiBoundingAddrSlice, uiBoundingAddrEntropySlice;
     929          uiBoundingAddrSlice        = m_uiStoredStartCUAddrForEncodingSlice[uiStartCUAddrSliceIdx];         
     930          uiBoundingAddrEntropySlice = m_uiStoredStartCUAddrForEncodingEntropySlice[uiStartCUAddrEntropySliceIdx];         
     931          uiNextCUAddr               = min(uiBoundingAddrSlice, uiBoundingAddrEntropySlice);
     932          if(pcSlice->isNextSlice())
     933          {
     934            skippedSlice=true;
     935          }
     936          continue;
     937        }
     938        if(skippedSlice)
     939        {
     940          pcSlice->setNextSlice       ( true );
     941          pcSlice->setNextEntropySlice( false );
     942        }
     943        skippedSlice=false;
     944        if (pcSlice->getPPS()->getEntropyCodingMode())
     945        {
     946          pcSlice->allocSubstreamSizes( iNumSubstreams );
     947          for ( UInt ui = 0 ; ui < iNumSubstreams; ui++ )
     948          pcSubstreamsOut[ui].clear();
     949        }
     950
     951        m_pcEntropyCoder->setEntropyCoder   ( m_pcCavlcCoder, pcSlice );
     952        m_pcEntropyCoder->resetEntropy      ();
     953        /* start slice NALunit */
     954#if H0388
     955#if NAL_REF_FLAG
     956        OutputNALUnit nalu( pcSlice->getNalUnitType(), pcSlice->isReferenced(), m_pcEncTop->getViewId(), m_pcEncTop->getIsDepth(), pcSlice->getTLayer() );
     957#else
     958        OutputNALUnit nalu( pcSlice->getNalUnitType(), pcSlice->isReferenced() ? NAL_REF_IDC_PRIORITY_HIGHEST: NAL_REF_IDC_PRIORITY_LOWEST, m_pcEncTop->getViewId(), m_pcEncTop->getIsDepth(), pcSlice->getTLayer() );
     959#endif
     960#else
     961        OutputNALUnit nalu( pcSlice->getNalUnitType(), pcSlice->isReferenced() ? NAL_REF_IDC_PRIORITY_HIGHEST: NAL_REF_IDC_PRIORITY_LOWEST, m_pcEncTop->getViewId(), m_pcEncTop->getIsDepth(), pcSlice->getTLayer(), true);
     962#endif
     963        Bool bEntropySlice = (!pcSlice->isNextSlice());
     964        if (!bEntropySlice)
     965        {
     966          uiOneBitstreamPerSliceLength = 0; // start of a new slice
     967        }
     968
     969        // used while writing slice header
     970        Int iTransmitLWHeader = (m_pcCfg->getTileMarkerFlag()==0) ? 0 : 1;
     971        pcSlice->setTileMarkerFlag ( iTransmitLWHeader );
     972        m_pcEntropyCoder->setBitstream(&nalu.m_Bitstream);
     973#if !CABAC_INIT_FLAG
     974        pcSlice->setCABACinitIDC(pcSlice->getSliceType());
     975#endif
     976
     977        m_pcEntropyCoder->encodeSliceHeader(pcSlice);
     978
     979        if(pcSlice->isNextSlice())
     980        {
     981          if (pcSlice->getSPS()->getUseALF())
     982          {
     983#if LCU_SYNTAX_ALF
     984            if(pcSlice->getAlfEnabledFlag())
     985#else
     986            if(pcSlice->getAPS()->getAlfEnabled())
     987#endif
     988            {
     989
     990#if LCU_SYNTAX_ALF
     991              if( pcSlice->getSPS()->getUseALFCoefInSlice())
     992              {
     993                Int iNumSUinLCU    = 1<< (g_uiMaxCUDepth << 1);
     994                Int firstLCUAddr   = pcSlice->getSliceCurStartCUAddr() / iNumSUinLCU; 
     995                Bool isAcrossSlice = pcSlice->getSPS()->getLFCrossSliceBoundaryFlag();
     996                m_pcEntropyCoder->encodeAlfParam( &(alfSliceParams[pcSlice->getSliceIdx()]), false, firstLCUAddr, isAcrossSlice);
     997              }
     998
     999              if( !pcSlice->getSPS()->getUseALFCoefInSlice())
     1000              {
     1001                AlfCUCtrlInfo& cAlfCUCtrlParam = (*alfCUCtrlParam)[pcSlice->getSliceIdx()];
     1002#else
     1003              AlfCUCtrlInfo& cAlfCUCtrlParam = vAlfCUCtrlParam[pcSlice->getSliceIdx()];
     1004#endif
     1005              if(cAlfCUCtrlParam.cu_control_flag)
     1006              {
     1007                m_pcEntropyCoder->setAlfCtrl( true );
     1008                m_pcEntropyCoder->setMaxAlfCtrlDepth(cAlfCUCtrlParam.alf_max_depth);
     1009                m_pcCavlcCoder->setAlfCtrl(true);
     1010                m_pcCavlcCoder->setMaxAlfCtrlDepth(cAlfCUCtrlParam.alf_max_depth);
     1011              }
     1012              else
     1013              {
     1014                m_pcEntropyCoder->setAlfCtrl(false);
     1015              }
     1016              m_pcEntropyCoder->encodeAlfCtrlParam(cAlfCUCtrlParam, m_pcAdaptiveLoopFilter->getNumCUsInPic());
     1017           
     1018#if LCU_SYNTAX_ALF
     1019              }
     1020#endif           
     1021            }
     1022          }
     1023        }
     1024        m_pcEntropyCoder->encodeTileMarkerFlag(pcSlice);
     1025
     1026        // is it needed?
     1027        {
     1028          if (!bEntropySlice)
     1029          {
     1030            pcBitstreamRedirect->writeAlignOne();
     1031          }
     1032          else
     1033          {
     1034          // We've not completed our slice header info yet, do the alignment later.
     1035          }
     1036          m_pcSbacCoder->init( (TEncBinIf*)m_pcBinCABAC );
     1037          m_pcEntropyCoder->setEntropyCoder ( m_pcSbacCoder, pcSlice );
     1038          m_pcEntropyCoder->resetEntropy    ();
     1039          for ( UInt ui = 0 ; ui < pcSlice->getPPS()->getNumSubstreams() ; ui++ )
     1040          {
     1041            m_pcEntropyCoder->setEntropyCoder ( &pcSbacCoders[ui], pcSlice );
     1042            m_pcEntropyCoder->resetEntropy    ();
     1043          }
     1044        }
     1045
     1046        if(pcSlice->isNextSlice())
     1047        {
     1048          // set entropy coder for writing
     1049          m_pcSbacCoder->init( (TEncBinIf*)m_pcBinCABAC );
     1050          {
     1051            for ( UInt ui = 0 ; ui < pcSlice->getPPS()->getNumSubstreams() ; ui++ )
     1052            {
     1053              m_pcEntropyCoder->setEntropyCoder ( &pcSbacCoders[ui], pcSlice );
     1054              m_pcEntropyCoder->resetEntropy    ();
     1055            }
     1056            pcSbacCoders[0].load(m_pcSbacCoder);
     1057            m_pcEntropyCoder->setEntropyCoder ( &pcSbacCoders[0], pcSlice );  //ALF is written in substream #0 with CABAC coder #0 (see ALF param encoding below)
     1058          }
     1059          m_pcEntropyCoder->resetEntropy    ();
     1060          // File writing
     1061          if (!bEntropySlice)
     1062          {
     1063            m_pcEntropyCoder->setBitstream(pcBitstreamRedirect);
     1064          }
     1065          else
     1066          {
     1067            m_pcEntropyCoder->setBitstream(&nalu.m_Bitstream);
     1068          }
     1069          // for now, override the TILES_DECODER setting in order to write substreams.
     1070            m_pcEntropyCoder->setBitstream    ( &pcSubstreamsOut[0] );
     1071
     1072        }
     1073        pcSlice->setFinalized(true);
     1074
     1075          m_pcSbacCoder->load( &pcSbacCoders[0] );
     1076
     1077        pcSlice->setTileOffstForMultES( uiOneBitstreamPerSliceLength );
     1078        if (!bEntropySlice)
     1079        {
     1080          pcSlice->setTileLocationCount ( 0 );
     1081          m_pcSliceEncoder->encodeSlice(pcPic, pcBitstreamRedirect, pcSubstreamsOut); // redirect is only used for CAVLC tile position info.
     1082        }
     1083        else
     1084        {
     1085          m_pcSliceEncoder->encodeSlice(pcPic, &nalu.m_Bitstream, pcSubstreamsOut); // nalu.m_Bitstream is only used for CAVLC tile position info.
     1086        }
     1087
     1088        {
     1089          // Construct the final bitstream by flushing and concatenating substreams.
     1090          // The final bitstream is either nalu.m_Bitstream or pcBitstreamRedirect;
     1091          UInt* puiSubstreamSizes = pcSlice->getSubstreamSizes();
     1092          UInt uiTotalCodedSize = 0; // for padding calcs.
     1093          UInt uiNumSubstreamsPerTile = iNumSubstreams;
     1094#if !REMOVE_TILE_DEPENDENCE
     1095#if WPP_SIMPLIFICATION
     1096         if (pcPic->getPicSym()->getTileBoundaryIndependenceIdr() && iNumSubstreams > 1)
     1097#else
     1098          if (pcPic->getPicSym()->getTileBoundaryIndependenceIdr() && pcSlice->getPPS()->getEntropyCodingSynchro())
     1099#endif
     1100            uiNumSubstreamsPerTile /= pcPic->getPicSym()->getNumTiles();
     1101#else
     1102#if WPP_SIMPLIFICATION
     1103          if (iNumSubstreams > 1)
     1104#else
     1105          if (pcSlice->getPPS()->getEntropyCodingSynchro())
     1106#endif
     1107          {
     1108            uiNumSubstreamsPerTile /= pcPic->getPicSym()->getNumTiles();
     1109          }
     1110#endif
     1111          for ( UInt ui = 0 ; ui < iNumSubstreams; ui++ )
     1112          {
     1113            // Flush all substreams -- this includes empty ones.
     1114            // Terminating bit and flush.
     1115            m_pcEntropyCoder->setEntropyCoder   ( &pcSbacCoders[ui], pcSlice );
     1116            m_pcEntropyCoder->setBitstream      (  &pcSubstreamsOut[ui] );
     1117            m_pcEntropyCoder->encodeTerminatingBit( 1 );
     1118            m_pcEntropyCoder->encodeSliceFinish();
     1119            pcSubstreamsOut[ui].write( 1, 1 ); // stop bit.
     1120#if TILES_WPP_ENTRY_POINT_SIGNALLING
     1121            pcSubstreamsOut[ui].writeAlignZero();
     1122#endif
     1123            // Byte alignment is necessary between tiles when tiles are independent.
     1124            uiTotalCodedSize += pcSubstreamsOut[ui].getNumberOfWrittenBits();
     1125
     1126            {
     1127              Bool bNextSubstreamInNewTile = ((ui+1) < iNumSubstreams)
     1128                                             && ((ui+1)%uiNumSubstreamsPerTile == 0);
     1129              if (bNextSubstreamInNewTile)
     1130              {
     1131                // byte align.
     1132                while (uiTotalCodedSize&0x7)
     1133                {
     1134                  pcSubstreamsOut[ui].write(0, 1);
     1135                  uiTotalCodedSize++;
     1136                }
     1137              }
     1138              Bool bRecordOffsetNext = m_pcCfg->getTileLocationInSliceHeaderFlag()
     1139                                            && bNextSubstreamInNewTile;
     1140              if (bRecordOffsetNext)
     1141                pcSlice->setTileLocation(ui/uiNumSubstreamsPerTile, pcSlice->getTileOffstForMultES()+(uiTotalCodedSize>>3));
     1142            }
     1143            if (ui+1 < pcSlice->getPPS()->getNumSubstreams())
     1144              puiSubstreamSizes[ui] = pcSubstreamsOut[ui].getNumberOfWrittenBits();
     1145          }
     1146          // Complete the slice header info.
    4711147          m_pcEntropyCoder->setEntropyCoder   ( m_pcCavlcCoder, pcSlice );
    472           m_pcEntropyCoder->setBitstream      ( pcBitstreamOut          );
    473           m_pcEntropyCoder->resetEntropy      ();
    474         }
    475 
    476       // write SliceHeader
    477       m_pcEntropyCoder->encodeSliceHeader ( pcSlice                 );
    478 
    479       // is it needed?
    480       if ( pcSlice->getSymbolMode() )
    481       {
    482         m_pcSbacCoder->init( (TEncBinIf*)m_pcBinCABAC );
    483         m_pcEntropyCoder->setEntropyCoder ( m_pcSbacCoder, pcSlice );
    484         m_pcEntropyCoder->resetEntropy    ();
    485       }
    486 
    487         if (uiNextCUAddr==0)  // Compute ALF params and write only for first slice header
    488         {
    489           // adaptive loop filter
    490 #if MTK_SAO
    491           if ( pcSlice->getSPS()->getUseALF() || (pcSlice->getSPS()->getUseSAO()) )
    492 #else
    493           if ( pcSlice->getSPS()->getUseALF())
    494 #endif
    495           {
    496             ALFParam cAlfParam;
    497 #if TSB_ALF_HEADER
    498             m_pcAdaptiveLoopFilter->setNumCUsInFrame(pcPic);
    499 #endif
    500             m_pcAdaptiveLoopFilter->allocALFParam(&cAlfParam);
    501 
     1148          m_pcEntropyCoder->setBitstream(&nalu.m_Bitstream);
     1149#if TILES_WPP_ENTRY_POINT_SIGNALLING
     1150          if (m_pcCfg->getTileLocationInSliceHeaderFlag()==0)
     1151          {
     1152            pcSlice->setTileLocationCount( 0 );
     1153          }
     1154          m_pcEntropyCoder->encodeTilesWPPEntryPoint( pcSlice );
     1155#else
     1156          m_pcEntropyCoder->encodeSliceHeaderSubstreamTable(pcSlice);
     1157#endif
     1158          // Substreams...
     1159          TComOutputBitstream *pcOut = pcBitstreamRedirect;
     1160          // xWriteTileLocation will perform byte-alignment...
     1161          {
     1162            if (bEntropySlice)
     1163            {
     1164              // In these cases, padding is necessary here.
     1165              pcOut = &nalu.m_Bitstream;
     1166              pcOut->writeAlignOne();
     1167            }
     1168          }
     1169          UInt uiAccumulatedLength = 0;
     1170          for ( UInt ui = 0 ; ui < pcSlice->getPPS()->getNumSubstreams(); ui++ )
     1171          {
     1172            pcOut->addSubstream(&pcSubstreamsOut[ui]);
     1173
     1174            // Update tile marker location information
     1175            for (Int uiMrkIdx = 0; uiMrkIdx < pcSubstreamsOut[ui].getTileMarkerLocationCount(); uiMrkIdx++)
     1176            {
     1177              UInt uiBottom = pcOut->getTileMarkerLocationCount();
     1178              pcOut->setTileMarkerLocation      ( uiBottom, uiAccumulatedLength + pcSubstreamsOut[ui].getTileMarkerLocation( uiMrkIdx ) );
     1179              pcOut->setTileMarkerLocationCount ( uiBottom + 1 );
     1180            }
     1181            uiAccumulatedLength = (pcOut->getNumberOfWrittenBits() >> 3);
     1182          }
     1183        }
     1184
     1185        UInt uiBoundingAddrSlice, uiBoundingAddrEntropySlice;
     1186        uiBoundingAddrSlice        = m_uiStoredStartCUAddrForEncodingSlice[uiStartCUAddrSliceIdx];         
     1187        uiBoundingAddrEntropySlice = m_uiStoredStartCUAddrForEncodingEntropySlice[uiStartCUAddrEntropySliceIdx];         
     1188        uiNextCUAddr               = min(uiBoundingAddrSlice, uiBoundingAddrEntropySlice);
     1189#if !REMOVE_TILE_DEPENDENCE
     1190        Bool bNextCUInNewSlice     = (uiNextCUAddr >= uiRealEndAddress) || (uiNextCUAddr == m_uiStoredStartCUAddrForEncodingSlice[uiStartCUAddrSliceIdx]);
     1191#endif
     1192        // If current NALU is the first NALU of slice (containing slice header) and more NALUs exist (due to multiple entropy slices) then buffer it.
     1193        // If current NALU is the last NALU of slice and a NALU was buffered, then (a) Write current NALU (b) Update an write buffered NALU at approproate location in NALU list.
     1194        Bool bNALUAlignedWrittenToList    = false; // used to ensure current NALU is not written more than once to the NALU list.
     1195#if !REMOVE_TILE_DEPENDENCE
     1196        if (pcSlice->getSPS()->getTileBoundaryIndependenceIdr() && !pcSlice->getSPS()->getTileBoundaryIndependenceIdr())
     1197        {
     1198          if (bNextCUInNewSlice)
     1199          {
     1200            if (!bEntropySlice) // there were no entropy slices
     1201            {
     1202              xWriteTileLocationToSliceHeader(nalu, pcBitstreamRedirect, pcSlice);
     1203            }
     1204            // (a) writing current NALU
     1205            writeRBSPTrailingBits(nalu.m_Bitstream);
     1206            accessUnit.push_back(new NALUnitEBSP(nalu));
     1207            bNALUAlignedWrittenToList = true;
     1208
     1209            // (b) update and write buffered NALU
     1210            if (bEntropySlice) // if entropy slices existed in the slice then perform concatenation for the buffered nalu-bitstream and buffered payload bitstream
     1211            {
     1212              // Perform bitstream concatenation of slice header and partial slice payload
     1213              xWriteTileLocationToSliceHeader((*naluBuffered), pcBitstreamRedirect, pcSlice);
     1214              if (bIteratorAtListStart)
     1215              {
     1216                itLocationToPushSliceHeaderNALU = accessUnit.begin();
     1217              }
     1218              else
     1219              {
     1220                itLocationToPushSliceHeaderNALU++;
     1221              }
     1222              accessUnit.insert(itLocationToPushSliceHeaderNALU, (new NALUnitEBSP((*naluBuffered))) );
     1223
     1224              // free buffered nalu
     1225              delete naluBuffered;
     1226              naluBuffered     = NULL;
     1227            }
     1228          }
     1229          else // another entropy slice exists
     1230          {
     1231            // Is this start-of-slice NALU? i.e. the one containing slice header. If Yes, then buffer it.
     1232            if (!bEntropySlice)
     1233            {
     1234              // store a pointer to where NALU for slice header is to be written in NALU list
     1235              itLocationToPushSliceHeaderNALU = accessUnit.end();
     1236              if (accessUnit.begin() == accessUnit.end())
     1237              {
     1238                bIteratorAtListStart = true;
     1239              }
     1240              else
     1241              {
     1242                bIteratorAtListStart = false;
     1243                itLocationToPushSliceHeaderNALU--;
     1244              }
     1245
     1246              // buffer nalu for later writing
     1247#if H0388
     1248              naluBuffered = new OutputNALUnit( pcSlice->getNalUnitType(), pcSlice->isReferenced() ? NAL_REF_IDC_PRIORITY_HIGHEST: NAL_REF_IDC_PRIORITY_LOWEST, m_pcEncTop->getViewId(), m_pcEncTop->getIsDepth(), pcSlice->getTLayer() );
     1249#else
     1250              naluBuffered = new OutputNALUnit(pcSlice->getNalUnitType(), pcSlice->isReferenced() ? NAL_REF_IDC_PRIORITY_HIGHEST: NAL_REF_IDC_PRIORITY_LOWEST, m_pcEncTop->getViewId(), m_pcEncTop->getIsDepth(), pcSlice->getTLayer(), true);
     1251#endif
     1252              copyNaluData( (*naluBuffered), nalu );
     1253
     1254              // perform byte-alignment to get appropriate bitstream length (used for explicit tile location signaling in slice header)
     1255              writeRBSPTrailingBits((*pcBitstreamRedirect));
     1256              bNALUAlignedWrittenToList = true; // This is not really a write to bitsream but buffered for later. The flag is set to prevent writing of current NALU to list.
     1257              uiOneBitstreamPerSliceLength += pcBitstreamRedirect->getNumberOfWrittenBits(); // length of bitstream after byte-alignment
     1258            }
     1259            else // write out entropy slice
     1260            {
     1261              writeRBSPTrailingBits(nalu.m_Bitstream);
     1262              accessUnit.push_back(new NALUnitEBSP(nalu));
     1263              bNALUAlignedWrittenToList = true;
     1264              uiOneBitstreamPerSliceLength += nalu.m_Bitstream.getNumberOfWrittenBits(); // length of bitstream after byte-alignment
     1265            }
     1266          }
     1267        }
     1268        else
     1269        {
     1270#endif
     1271        xWriteTileLocationToSliceHeader(nalu, pcBitstreamRedirect, pcSlice);
     1272        writeRBSPTrailingBits(nalu.m_Bitstream);
     1273        accessUnit.push_back(new NALUnitEBSP(nalu));
     1274        bNALUAlignedWrittenToList = true;
     1275        uiOneBitstreamPerSliceLength += nalu.m_Bitstream.getNumberOfWrittenBits(); // length of bitstream after byte-alignment
     1276#if !REMOVE_TILE_DEPENDENCE
     1277        }
     1278#endif
     1279
     1280        if (!bNALUAlignedWrittenToList)
     1281        {
     1282        {
     1283          nalu.m_Bitstream.writeAlignZero();
     1284        }
     1285        accessUnit.push_back(new NALUnitEBSP(nalu));
     1286        uiOneBitstreamPerSliceLength += nalu.m_Bitstream.getNumberOfWrittenBits() + 24; // length of bitstream after byte-alignment + 3 byte startcode 0x000001
     1287        }
     1288
     1289
     1290        processingState = ENCODE_SLICE;
     1291          }
     1292          break;
     1293        case EXECUTE_INLOOPFILTER:
     1294          {
     1295            TComAPS cAPS;
     1296            allocAPS(&cAPS, pcSlice->getSPS());
     1297#if SAO_UNIT_INTERLEAVING
     1298            cAPS.setSaoInterleavingFlag(m_pcCfg->getSaoInterleavingFlag());
     1299#endif
    5021300            // set entropy coder for RD
    503             if ( pcSlice->getSymbolMode() )
     1301            m_pcEntropyCoder->setEntropyCoder ( m_pcCavlcCoder, pcSlice );
     1302
     1303            if ( pcSlice->getSPS()->getUseSAO() )
    5041304            {
    505               m_pcEntropyCoder->setEntropyCoder ( m_pcEncTop->getRDGoOnSbacCoder(), pcSlice );
     1305              m_pcEntropyCoder->resetEntropy();
     1306              m_pcEntropyCoder->setBitstream( m_pcBitCounter );
     1307              m_pcSAO->startSaoEnc(pcPic, m_pcEntropyCoder, m_pcEncTop->getRDSbacCoder(), NULL);
     1308              SAOParam& cSaoParam = *(cAPS.getSaoParam());
     1309
     1310#if SAO_CHROMA_LAMBDA
     1311              m_pcSAO->SAOProcess(&cSaoParam, pcPic->getSlice(0)->getLambdaLuma(), pcPic->getSlice(0)->getLambdaChroma());
     1312#else
     1313#if ALF_CHROMA_LAMBDA
     1314              m_pcSAO->SAOProcess(&cSaoParam, pcPic->getSlice(0)->getLambdaLuma());
     1315#else
     1316              m_pcSAO->SAOProcess(&cSaoParam, pcPic->getSlice(0)->getLambda());
     1317#endif
     1318#endif
     1319              m_pcSAO->endSaoEnc();
     1320
     1321              m_pcAdaptiveLoopFilter->PCMLFDisableProcess(pcPic);
    5061322            }
    507             else
     1323
     1324            // adaptive loop filter
     1325#if !LCU_SYNTAX_ALF
     1326            UInt64 uiDist, uiBits;
     1327#endif
     1328            if ( pcSlice->getSPS()->getUseALF())
    5081329            {
    509               m_pcEntropyCoder->setEntropyCoder ( m_pcCavlcCoder, pcSlice );
     1330              m_pcEntropyCoder->resetEntropy    ();
     1331              m_pcEntropyCoder->setBitstream    ( m_pcBitCounter );
     1332              m_pcAdaptiveLoopFilter->startALFEnc(pcPic, m_pcEntropyCoder );
     1333#if LCU_SYNTAX_ALF
     1334              AlfParamSet* pAlfEncParam = (pcSlice->getSPS()->getUseALFCoefInSlice())?( alfSliceParams ):( cAPS.getAlfParam());
     1335#if ALF_CHROMA_LAMBDA
     1336#if HHI_INTERVIEW_SKIP
     1337              m_pcAdaptiveLoopFilter->ALFProcess(pAlfEncParam, alfCUCtrlParam, pcPic->getSlice(0)->getLambdaLuma(), pcPic->getSlice(0)->getLambdaChroma(), m_pcEncTop->getInterViewSkip()  );
     1338#else
     1339              m_pcAdaptiveLoopFilter->ALFProcess(pAlfEncParam, alfCUCtrlParam, pcPic->getSlice(0)->getLambdaLuma(), pcPic->getSlice(0)->getLambdaChroma() );
     1340#endif
     1341#else
     1342#if SAO_CHROMA_LAMBDA
     1343#if HHI_INTERVIEW_SKIP
     1344              m_pcAdaptiveLoopFilter->ALFProcess(pAlfEncParam, alfCUCtrlParam, pcPic->getSlice(0)->getLambdaLuma(), m_pcEncTop->getInterViewSkip());
     1345#else
     1346              m_pcAdaptiveLoopFilter->ALFProcess(pAlfEncParam, alfCUCtrlParam, pcPic->getSlice(0)->getLambdaLuma());
     1347#endif
     1348#else
     1349#if HHI_INTERVIEW_SKIP
     1350              m_pcAdaptiveLoopFilter->ALFProcess(pAlfEncParam, alfCUCtrlParam, pcPic->getSlice(0)->getLambda(), m_pcEncTop->getInterViewSkip() );
     1351#else
     1352              m_pcAdaptiveLoopFilter->ALFProcess(pAlfEncParam, alfCUCtrlParam, pcPic->getSlice(0)->getLambda());
     1353#endif
     1354#endif
     1355#endif
     1356
     1357#else
     1358              ALFParam& cAlfParam = *( cAPS.getAlfParam());
     1359
     1360#if ALF_CHROMA_LAMBDA
     1361#if HHI_INTERVIEW_SKIP
     1362              m_pcAdaptiveLoopFilter->ALFProcess( &cAlfParam, &vAlfCUCtrlParam, pcPic->getSlice(0)->getLambdaLuma(), pcPic->getSlice(0)->getLambdaChroma(), uiDist, uiBits, m_pcEncTop->getInterViewSkip());
     1363#else
     1364              m_pcAdaptiveLoopFilter->ALFProcess( &cAlfParam, &vAlfCUCtrlParam, pcPic->getSlice(0)->getLambdaLuma(), pcPic->getSlice(0)->getLambdaChroma(), uiDist, uiBits);
     1365#endif
     1366#else
     1367#if SAO_CHROMA_LAMBDA
     1368#if HHI_INTERVIEW_SKIP
     1369              m_pcAdaptiveLoopFilter->ALFProcess( &cAlfParam, &vAlfCUCtrlParam, pcPic->getSlice(0)->getLambdaLuma(), uiDist, uiBits, m_pcEncTop->getInterViewSkip());
     1370#else
     1371              m_pcAdaptiveLoopFilter->ALFProcess( &cAlfParam, &vAlfCUCtrlParam, pcPic->getSlice(0)->getLambdaLuma(), uiDist, uiBits);
     1372#endif
     1373#else
     1374#if HHI_INTERVIEW_SKIP
     1375              m_pcAdaptiveLoopFilter->ALFProcess( &cAlfParam, &vAlfCUCtrlParam, pcPic->getSlice(0)->getLambdaLuma(), uiDist, uiBits, m_pcEncTop->getInterViewSkip());
     1376#else
     1377              m_pcAdaptiveLoopFilter->ALFProcess( &cAlfParam, &vAlfCUCtrlParam, pcPic->getSlice(0)->getLambda(), uiDist, uiBits);
     1378#endif
     1379#endif
     1380#endif
     1381#endif
     1382              m_pcAdaptiveLoopFilter->endALFEnc();
     1383
     1384              m_pcAdaptiveLoopFilter->PCMLFDisableProcess(pcPic);
    5101385            }
    511             m_pcEntropyCoder->resetEntropy    ();
    512             m_pcEntropyCoder->setBitstream    ( m_pcBitCounter );
    513 
    514             m_pcAdaptiveLoopFilter->startALFEnc(pcPic, m_pcEntropyCoder );
    515 #if MTK_SAO  // PostDF
     1386            iCodedAPSIdx = iCurrAPSIdx; 
     1387            pcSliceForAPS = pcSlice;
     1388
     1389            assignNewAPS(cAPS, iCodedAPSIdx, vAPS, pcSliceForAPS);
     1390            iCurrAPSIdx = (iCurrAPSIdx +1)%MAX_NUM_SUPPORTED_APS;
     1391            processingState = ENCODE_APS;
     1392
     1393            //set APS link to the slices
     1394            for(Int s=0; s< uiNumSlices; s++)
    5161395            {
     1396              if (pcSlice->getSPS()->getUseALF())
     1397              {
     1398#if LCU_SYNTAX_ALF
     1399                pcPic->getSlice(s)->setAlfEnabledFlag(  (pcSlice->getSPS()->getUseALFCoefInSlice())?(alfSliceParams[s].isEnabled[ALF_Y]):(cAPS.getAlfEnabled())   );
     1400#else
     1401                pcPic->getSlice(s)->setAlfEnabledFlag((cAPS.getAlfParam()->alf_flag==1)?true:false);
     1402#endif
     1403              }
    5171404              if (pcSlice->getSPS()->getUseSAO())
    5181405              {
    519                 m_pcSAO->startSaoEnc(pcPic, m_pcEntropyCoder, m_pcEncTop->getRDSbacCoder(), m_pcCfg->getUseSBACRD() ?  m_pcEncTop->getRDGoOnSbacCoder() : NULL);
    520                 m_pcSAO->SAOProcess(pcPic->getSlice(0)->getLambda());
    521                 m_pcSAO->copyQaoData(&cSaoParam);
    522                 m_pcSAO->endSaoEnc();
     1406                pcPic->getSlice(s)->setSaoEnabledFlag((cAPS.getSaoParam()->bSaoFlag[0]==1)?true:false);
    5231407              }
     1408              pcPic->getSlice(s)->setAPS(&(vAPS[iCodedAPSIdx]));
     1409              pcPic->getSlice(s)->setAPSId(iCodedAPSIdx);
    5241410            }
    525 #endif
    526             UInt uiMaxAlfCtrlDepth;
    527 
    528             UInt64 uiDist, uiBits;
    529 #if MTK_SAO
    530             if ( pcSlice->getSPS()->getUseALF())
    531 #endif
    532               m_pcAdaptiveLoopFilter->ALFProcess( &cAlfParam, pcPic->getSlice(0)->getLambda(), uiDist, uiBits, uiMaxAlfCtrlDepth );
    533 #if MTK_SAO
    534             else
    535               cAlfParam.cu_control_flag = 0;
    536 #endif
    537             m_pcAdaptiveLoopFilter->endALFEnc();
    538 
    539             // set entropy coder for writing
    540             m_pcSbacCoder->init( (TEncBinIf*)m_pcBinCABAC );
    541             if ( pcSlice->getSymbolMode() )
    542             {
    543               m_pcEntropyCoder->setEntropyCoder ( m_pcSbacCoder, pcSlice );
    544             }
    545             else
    546             {
    547               m_pcEntropyCoder->setEntropyCoder ( m_pcCavlcCoder, pcSlice );
    548             }
    549             m_pcEntropyCoder->resetEntropy    ();
    550             m_pcEntropyCoder->setBitstream    ( pcBitstreamOut );
    551             if (cAlfParam.cu_control_flag)
    552             {
    553               m_pcEntropyCoder->setAlfCtrl( true );
    554               m_pcEntropyCoder->setMaxAlfCtrlDepth(uiMaxAlfCtrlDepth);
    555               if (pcSlice->getSymbolMode() == 0)
    556               {
    557                 m_pcCavlcCoder->setAlfCtrl(true);
    558                 m_pcCavlcCoder->setMaxAlfCtrlDepth(uiMaxAlfCtrlDepth); //D0201
    559               }
    560             }
    561             else
    562             {
    563               m_pcEntropyCoder->setAlfCtrl(false);
    564             }
    565 #if MTK_SAO
    566             if (pcSlice->getSPS()->getUseSAO())
    567             {
    568               m_pcEntropyCoder->encodeSaoParam(&cSaoParam);
    569             }
    570             if (pcSlice->getSPS()->getUseALF())
    571 #endif
    572             m_pcEntropyCoder->encodeAlfParam(&cAlfParam);
    573 
    574 #if TSB_ALF_HEADER
    575             if(cAlfParam.cu_control_flag)
    576             {
    577               m_pcEntropyCoder->encodeAlfCtrlParam(&cAlfParam);
    578             }
    579 #endif
    580             m_pcAdaptiveLoopFilter->freeALFParam(&cAlfParam);
    581           }
    582         }
    583 
    584         // File writing
    585         m_pcSliceEncoder->encodeSlice( pcPic, pcBitstreamOut );
    586 
    587         //  End of bitstream & byte align
    588         pcBitstreamOut->write( 1, 1 );
    589         pcBitstreamOut->writeAlignZero();
    590 
    591         UInt uiBoundingAddrSlice, uiBoundingAddrEntropySlice;
    592         uiBoundingAddrSlice        = m_uiStoredStartCUAddrForEncodingSlice[uiStartCUAddrSliceIdx];
    593         uiBoundingAddrEntropySlice = m_uiStoredStartCUAddrForEncodingEntropySlice[uiStartCUAddrEntropySliceIdx];
    594         uiNextCUAddr               = min(uiBoundingAddrSlice, uiBoundingAddrEntropySlice);
    595         if (uiNextCUAddr < pcPic->getPicSym()->getNumberOfCUsInFrame())   // if more slices to be encoded insert start code
    596         {
    597           UInt uiSliceCount = pcBitstreamOut->getSliceCount();
    598           pcBitstreamOut->setSliceByteLocation( uiSliceCount, (pcBitstreamOut->getNumberOfWrittenBits()>>3) );
    599           pcBitstreamOut->setSliceCount( uiSliceCount+1 );
    600           pcBitstreamOut->write( 1, 32);
     1411          }
     1412          break;
     1413        case ENCODE_APS:
     1414          {
     1415#if NAL_REF_FLAG
     1416            OutputNALUnit nalu(NAL_UNIT_APS, true, m_pcEncTop->getViewId(), m_pcEncTop->getIsDepth());
     1417#else
     1418            OutputNALUnit nalu(NAL_UNIT_APS, NAL_REF_IDC_PRIORITY_HIGHEST, m_pcEncTop->getViewId(), m_pcEncTop->getIsDepth());
     1419#endif
     1420            encodeAPS(&(vAPS[iCodedAPSIdx]), nalu.m_Bitstream, pcSliceForAPS);
     1421            accessUnit.push_back(new NALUnitEBSP(nalu));
     1422
     1423            processingState = ENCODE_SLICE;
     1424          }
     1425          break;
     1426        default:
     1427          {
     1428            printf("Not a supported encoding state\n");
     1429            assert(0);
     1430            exit(-1);
     1431          }
    6011432        }
    6021433      } // end iteration over slices
    6031434
    6041435
    605 #if MTK_NONCROSS_INLOOP_FILTER
    606       if(pcSlice->getSPS()->getUseALF())
    607       {
    608         if(m_pcAdaptiveLoopFilter->getUseNonCrossAlf())
    609           m_pcAdaptiveLoopFilter->destroySlice();
    610       }
    611 #endif
    612 
    613 
    614       pcBitstreamOut->flushBuffer();
    615       pcBitstreamOut->convertRBSPToPayload(0);
    616 
    617 /*#if AMVP_BUFFERCOMPRESS
    618       pcPic->compressMotion(); // moved to end of access unit
    619 #endif */
    620       pcBitstreamOut->freeMemoryAllocatedForSliceLocations();
    621 
     1436      if(pcSlice->getSPS()->getUseSAO() || pcSlice->getSPS()->getUseALF())
     1437      {
     1438        if(pcSlice->getSPS()->getUseSAO())
     1439        {
     1440          m_pcSAO->destroyPicSaoInfo();
     1441        }
     1442
     1443        if(pcSlice->getSPS()->getUseALF())
     1444        {
     1445#if LCU_SYNTAX_ALF
     1446          m_pcAdaptiveLoopFilter->uninitALFEnc(alfSliceParams, alfCUCtrlParam);
     1447#endif
     1448          m_pcAdaptiveLoopFilter->destroyPicAlfInfo();
     1449        }
     1450
     1451        pcPic->destroyNonDBFilterInfo();
     1452      }
     1453
     1454#if HHI_INTERVIEW_SKIP
     1455      if (pcPic->getUsedPelsMap())
     1456        pcPic->removeUsedPelsMapBuffer() ;
     1457#endif
     1458#if HHI_INTER_VIEW_MOTION_PRED
     1459      pcPic->removeOrgDepthMapBuffer();
     1460#endif
     1461   
     1462   //   pcPic->compressMotion();
     1463      m_pocLastCoded = pcPic->getPOC();
     1464     
    6221465      //-- For time output for each slice
    6231466      Double dEncTime = (double)(clock()-iBeforeTime) / CLOCKS_PER_SEC;
    6241467
    625       xCalculateAddPSNR( pcPic, pcPic->getPicYuvRec(), pcBitstreamOut->getNumberOfWrittenBits(), dEncTime );
    626 
    627 #if FIXED_ROUNDING_FRAME_MEMORY
    628       pcPic->getPicYuvRec()->xFixedRoundingPic();
    629 #endif
    630 
    631       if (m_pcCfg->getPictureDigestEnabled()) {
     1468      const char* digestStr = NULL;
     1469      if (m_pcCfg->getPictureDigestEnabled())
     1470      {
    6321471        /* calculate MD5sum for entire reconstructed picture */
    6331472        SEIpictureDigest sei_recon_picture_digest;
    6341473        sei_recon_picture_digest.method = SEIpictureDigest::MD5;
    6351474        calcMD5(*pcPic->getPicYuvRec(), sei_recon_picture_digest.digest);
    636         printf("[MD5:%s] ", digestToString(sei_recon_picture_digest.digest));
    637 
    638         TComBitstream seiBs;
    639         seiBs.create(1024);
     1475        digestStr = digestToString(sei_recon_picture_digest.digest);
     1476
     1477#if NAL_REF_FLAG
     1478        OutputNALUnit nalu(NAL_UNIT_SEI, false, m_pcEncTop->getViewId(), m_pcEncTop->getIsDepth());
     1479#else
     1480        OutputNALUnit nalu(NAL_UNIT_SEI, NAL_REF_IDC_PRIORITY_LOWEST, m_pcEncTop->getViewId(), m_pcEncTop->getIsDepth());
     1481#endif
     1482
    6401483        /* write the SEI messages */
    641 #if BITSTREAM_EXTRACTION
    642         sei_recon_picture_digest.setLayerId( pcSlice->getLayerId() );
    643 #endif
    6441484        m_pcEntropyCoder->setEntropyCoder(m_pcCavlcCoder, pcSlice);
    645         m_pcEntropyCoder->setBitstream(&seiBs);
     1485        m_pcEntropyCoder->setBitstream(&nalu.m_Bitstream);
    6461486        m_pcEntropyCoder->encodeSEI(sei_recon_picture_digest);
    647         /* and trailing bits */
    648         seiBs.write(1, 1);
    649         seiBs.writeAlignZero();
    650         seiBs.flushBuffer();
    651         seiBs.convertRBSPToPayload(0);
    652 
    653         /* append the SEI message after any SPS/PPS */
    654         /* the following loop is a work around current limitations in
    655          * TComBitstream that won't be fixed before HM-3.0 */
    656         UChar *seiData = reinterpret_cast<UChar *>(seiBs.getStartStream());
    657         for (Int i = 0; i < seiBs.getNumberOfWrittenBits()/8; i++)
    658         {
    659           bs_SPS_PPS_SEI.write(seiData[i], 8);
    660         }
    661         bs_SPS_PPS_SEI.write(1, 32);
    662         seiBs.destroy();
    663       }
    664 
    665       /* insert the bs_SPS_PPS_SEI before the pcBitstreamOut */
    666       bs_SPS_PPS_SEI.flushBuffer();
    667       pcBitstreamOut->insertAt(bs_SPS_PPS_SEI, 0);
    668 
    669       bs_SPS_PPS_SEI.destroy();
     1487        writeRBSPTrailingBits(nalu.m_Bitstream);
     1488
     1489        /* insert the SEI message NALUnit before any Slice NALUnits */
     1490        AccessUnit::iterator it = find_if(accessUnit.begin(), accessUnit.end(), mem_fun(&NALUnit::isSlice));
     1491        accessUnit.insert(it, new NALUnitEBSP(nalu));
     1492      }
     1493
     1494      xCalculateAddPSNR( pcPic, pcPic->getPicYuvRec(), accessUnit, dEncTime );
     1495      if (digestStr)
     1496        printf(" [MD5:%s]", digestStr);
     1497
     1498#if FIXED_ROUNDING_FRAME_MEMORY
     1499      /* TODO: this should happen after copyToPic(pcPicYuvRecOut) */
     1500      pcPic->getPicYuvRec()->xFixedRoundingPic();
     1501#endif
    6701502      pcPic->getPicYuvRec()->copyToPic(pcPicYuvRecOut);
    671 
     1503     
    6721504      pcPic->setReconMark   ( true );
    6731505
     1506      pcPic->setUsedForTMVP ( true );
     1507
     1508      m_bFirst = false;
     1509      m_iNumPicCoded++;
     1510
     1511      /* logging: insert a newline at end of picture period */
     1512      printf("\n");
     1513      fflush(stdout);
     1514  }
     1515 
     1516  delete[] pcSubstreamsOut;
     1517  delete pcBitstreamRedirect;
     1518
    6741519}
    6751520
    676 Void TEncPic::preLoopFilterPicAll( TComPic* pcPic, UInt64& ruiDist, UInt64& ruiBits )
     1521/** Memory allocation for APS
     1522  * \param [out] pAPS APS pointer
     1523  * \param [in] pSPS SPS pointer
     1524  */
     1525Void TEncGOP::allocAPS (TComAPS* pAPS, TComSPS* pSPS)
     1526{
     1527  if(pSPS->getUseSAO())
     1528  {
     1529    pAPS->createSaoParam();
     1530    m_pcSAO->allocSaoParam(pAPS->getSaoParam());
     1531  }
     1532  if(pSPS->getUseALF())
     1533  {
     1534    pAPS->createAlfParam();
     1535#if LCU_SYNTAX_ALF
     1536    //alf Enabled flag in APS is false after pAPS->createAlfParam();
     1537    if(!pSPS->getUseALFCoefInSlice())
     1538    {
     1539      pAPS->getAlfParam()->create(m_pcAdaptiveLoopFilter->getNumLCUInPicWidth(), m_pcAdaptiveLoopFilter->getNumLCUInPicHeight(), m_pcAdaptiveLoopFilter->getNumCUsInPic());
     1540      pAPS->getAlfParam()->createALFParam();
     1541    }
     1542#else
     1543    m_pcAdaptiveLoopFilter->allocALFParam(pAPS->getAlfParam());
     1544#endif
     1545  }
     1546}
     1547
     1548/** Memory deallocation for APS
     1549  * \param [out] pAPS APS pointer
     1550  * \param [in] pSPS SPS pointer
     1551  */
     1552Void TEncGOP::freeAPS (TComAPS* pAPS, TComSPS* pSPS)
     1553{
     1554  if(pSPS->getUseSAO())
     1555  {
     1556    if(pAPS->getSaoParam() != NULL)
     1557    {
     1558      m_pcSAO->freeSaoParam(pAPS->getSaoParam());
     1559      pAPS->destroySaoParam();
     1560
     1561    }
     1562  }
     1563  if(pSPS->getUseALF())
     1564  {
     1565    if(pAPS->getAlfParam() != NULL)
     1566    {
     1567#if LCU_SYNTAX_ALF
     1568      if(!pSPS->getUseALFCoefInSlice())
     1569      {
     1570        pAPS->getAlfParam()->releaseALFParam();
     1571      }
     1572#else
     1573      m_pcAdaptiveLoopFilter->freeALFParam(pAPS->getAlfParam());
     1574#endif
     1575      pAPS->destroyAlfParam();
     1576    }
     1577  }
     1578}
     1579
     1580/** Assign APS object into APS container according to APS ID
     1581  * \param [in] cAPS APS object
     1582  * \param [in] apsID APS ID
     1583  * \param [in,out] vAPS APS container
     1584  * \param [in] pcSlice pointer to slice
     1585  */
     1586Void TEncGOP::assignNewAPS(TComAPS& cAPS, Int apsID, std::vector<TComAPS>& vAPS, TComSlice* pcSlice)
     1587{
     1588
     1589  cAPS.setAPSID(apsID);
     1590  if(pcSlice->getPOC() == 0)
     1591  {
     1592    cAPS.setScalingListEnabled(pcSlice->getSPS()->getScalingListFlag());
     1593  }
     1594  else
     1595  {
     1596    cAPS.setScalingListEnabled(false);
     1597  }
     1598
     1599  cAPS.setSaoEnabled(pcSlice->getSPS()->getUseSAO() ? (cAPS.getSaoParam()->bSaoFlag[0] ):(false));
     1600#if LCU_SYNTAX_ALF
     1601  cAPS.setAlfEnabled(pcSlice->getSPS()->getUseALF() ? (cAPS.getAlfParam()->isEnabled[0]):(false));
     1602#else
     1603  cAPS.setAlfEnabled(pcSlice->getSPS()->getUseALF() ? (cAPS.getAlfParam()->alf_flag ==1):(false));
     1604#endif
     1605  cAPS.setLoopFilterOffsetInAPS(m_pcCfg->getLoopFilterOffsetInAPS());
     1606  cAPS.setLoopFilterDisable(m_pcCfg->getLoopFilterDisable());
     1607  cAPS.setLoopFilterBetaOffset(m_pcCfg->getLoopFilterBetaOffset());
     1608  cAPS.setLoopFilterTcOffset(m_pcCfg->getLoopFilterTcOffset());
     1609
     1610  //assign new APS into APS container
     1611  Int apsBufSize= (Int)vAPS.size();
     1612
     1613  if(apsID >= apsBufSize)
     1614  {
     1615    vAPS.resize(apsID +1);
     1616  }
     1617
     1618  freeAPS(&(vAPS[apsID]), pcSlice->getSPS());
     1619  vAPS[apsID] = cAPS;
     1620}
     1621
     1622
     1623/** encode APS syntax elements
     1624  * \param [in] pcAPS APS pointer
     1625  * \param [in, out] APSbs bitstream
     1626  * \param [in] pointer to slice (just used for entropy coder initialization)
     1627  */
     1628Void TEncGOP::encodeAPS(TComAPS* pcAPS, TComOutputBitstream& APSbs, TComSlice* pcSlice)
     1629{
     1630  m_pcEntropyCoder->setEntropyCoder   ( m_pcCavlcCoder, pcSlice);
     1631  m_pcEntropyCoder->resetEntropy      ();
     1632  m_pcEntropyCoder->setBitstream(&APSbs);
     1633
     1634  m_pcEntropyCoder->encodeAPSInitInfo(pcAPS);
     1635  if(pcAPS->getScalingListEnabled())
     1636  {
     1637    m_pcEntropyCoder->encodeScalingList( pcSlice->getScalingList() );
     1638  }
     1639  if(pcAPS->getLoopFilterOffsetInAPS())
     1640  {
     1641    m_pcEntropyCoder->encodeDFParams(pcAPS);
     1642  }
     1643#if SAO_UNIT_INTERLEAVING
     1644  m_pcEntropyCoder->encodeSaoParam(pcAPS);
     1645#else
     1646  if(pcAPS->getSaoEnabled())
     1647  {
     1648    m_pcEntropyCoder->encodeSaoParam(pcAPS->getSaoParam());
     1649  }
     1650#endif
     1651#if LCU_SYNTAX_ALF
     1652  m_pcEntropyCoder->encodeAPSAlfFlag( pcAPS->getAlfEnabled()?1:0);
     1653#endif
     1654  if(pcAPS->getAlfEnabled())
     1655  {
     1656    m_pcEntropyCoder->encodeAlfParam(pcAPS->getAlfParam());
     1657  }
     1658
     1659  m_pcEntropyCoder->encodeApsExtensionFlag();
     1660  //neither SAO and ALF is enabled
     1661  writeRBSPTrailingBits(APSbs);
     1662}
     1663
     1664Void TEncGOP::preLoopFilterPicAll( TComPic* pcPic, UInt64& ruiDist, UInt64& ruiBits )
    6771665{
    6781666  TComSlice* pcSlice = pcPic->getSlice(pcPic->getCurrSliceIdx());
    6791667  Bool bCalcDist = false;
    680 
    681   m_pcLoopFilter->setCfg(pcSlice->getLoopFilterDisable(), m_pcCfg->getLoopFilterAlphaC0Offget(), m_pcCfg->getLoopFilterBetaOffget());
     1668#if DBL_CONTROL
     1669  m_pcLoopFilter->setCfg(pcSlice->getPPS()->getDeblockingFilterControlPresent(), pcSlice->getLoopFilterDisable(), m_pcCfg->getLoopFilterBetaOffset(), m_pcCfg->getLoopFilterTcOffset(), m_pcCfg->getLFCrossTileBoundaryFlag());
     1670#else
     1671  m_pcLoopFilter->setCfg(pcSlice->getLoopFilterDisable(), m_pcCfg->getLoopFilterBetaOffset(), m_pcCfg->getLoopFilterTcOffset(), m_pcCfg->getLFCrossTileBoundaryFlag());
     1672#endif
    6821673  m_pcLoopFilter->loopFilterPic( pcPic );
    683 
     1674 
    6841675  m_pcEntropyCoder->setEntropyCoder ( m_pcEncTop->getRDGoOnSbacCoder(), pcSlice );
    6851676  m_pcEntropyCoder->resetEntropy    ();
    6861677  m_pcEntropyCoder->setBitstream    ( m_pcBitCounter );
    687 
     1678  pcSlice = pcPic->getSlice(0);
     1679  if(pcSlice->getSPS()->getUseSAO() || pcSlice->getSPS()->getUseALF())
     1680  {
     1681    pcPic->createNonDBFilterInfo();
     1682  }
     1683 
    6881684  // Adaptive Loop filter
    6891685  if( pcSlice->getSPS()->getUseALF() )
    6901686  {
     1687    m_pcAdaptiveLoopFilter->createPicAlfInfo(pcPic);
     1688
     1689#if LCU_SYNTAX_ALF
     1690    AlfParamSet* alfParamSet;
     1691    std::vector<AlfCUCtrlInfo>* alfCUCtrlParam = NULL;
     1692    alfParamSet= new AlfParamSet;
     1693    alfParamSet->create( m_pcAdaptiveLoopFilter->getNumLCUInPicWidth(), m_pcAdaptiveLoopFilter->getNumLCUInPicHeight(), m_pcAdaptiveLoopFilter->getNumCUsInPic());
     1694    alfParamSet->createALFParam();
     1695    m_pcAdaptiveLoopFilter->initALFEnc(false, true, 1, alfParamSet, alfCUCtrlParam);
     1696#else
    6911697    ALFParam cAlfParam;
    692 #if TSB_ALF_HEADER
    693     m_pcAdaptiveLoopFilter->setNumCUsInFrame(pcPic);
    694 #endif
    6951698    m_pcAdaptiveLoopFilter->allocALFParam(&cAlfParam);
    696 
     1699#endif   
    6971700    m_pcAdaptiveLoopFilter->startALFEnc(pcPic, m_pcEntropyCoder);
    698 
    699     UInt uiMaxAlfCtrlDepth;
    700     m_pcAdaptiveLoopFilter->ALFProcess(&cAlfParam, pcSlice->getLambda(), ruiDist, ruiBits, uiMaxAlfCtrlDepth );
     1701   
     1702
     1703#if LCU_SYNTAX_ALF
     1704
     1705#if ALF_CHROMA_LAMBDA
     1706#if HHI_INTERVIEW_SKIP
     1707    m_pcAdaptiveLoopFilter->ALFProcess(alfParamSet, NULL, pcPic->getSlice(0)->getLambdaLuma(), pcPic->getSlice(0)->getLambdaChroma(), m_pcEncTop->getInterViewSkip()  );
     1708#else
     1709    m_pcAdaptiveLoopFilter->ALFProcess(alfParamSet, NULL, pcPic->getSlice(0)->getLambdaLuma(), pcPic->getSlice(0)->getLambdaChroma() );
     1710#endif
     1711#else
     1712#if SAO_CHROMA_LAMBDA
     1713    m_pcAdaptiveLoopFilter->ALFProcess(alfParamSet, NULL, pcPic->getSlice(0)->getLambdaLuma(), m_pcEncTop->getInterViewSkip());
     1714#if HHI_INTERVIEW_SKIP
     1715#else
     1716    m_pcAdaptiveLoopFilter->ALFProcess(alfParamSet, NULL, pcPic->getSlice(0)->getLambdaLuma());
     1717#endif
     1718#else
     1719#if HHI_INTERVIEW_SKIP
     1720    m_pcAdaptiveLoopFilter->ALFProcess(alfParamSet, NULL, pcPic->getSlice(0)->getLambda(), m_pcEncTop->getInterViewSkip());
     1721#else
     1722    m_pcAdaptiveLoopFilter->ALFProcess(alfParamSet, NULL, pcPic->getSlice(0)->getLambda());
     1723#endif
     1724#endif
     1725#endif
     1726
     1727#else
     1728#if ALF_CHROMA_LAMBDA 
     1729    m_pcAdaptiveLoopFilter->ALFProcess(&cAlfParam, NULL, pcSlice->getLambdaLuma(), pcSlice->getLambdaChroma(), ruiDist, ruiBits, m_pcEncTop->getInterViewSkip());
     1730#if HHI_INTERVIEW_SKIP
     1731#else
     1732    m_pcAdaptiveLoopFilter->ALFProcess(&cAlfParam, NULL, pcSlice->getLambdaLuma(), pcSlice->getLambdaChroma(), ruiDist, ruiBits);
     1733#endif
     1734#else
     1735#if SAO_CHROMA_LAMBDA
     1736#if HHI_INTERVIEW_SKIP
     1737    m_pcAdaptiveLoopFilter->ALFProcess(&cAlfParam, NULL, pcSlice->getLambdaLuma(), ruiDist, ruiBits, m_pcEncTop->getInterViewSkip());
     1738#else
     1739    m_pcAdaptiveLoopFilter->ALFProcess(&cAlfParam, NULL, pcSlice->getLambdaLuma(), ruiDist, ruiBits);
     1740#endif
     1741#else
     1742#if HHI_INTERVIEW_SKIP
     1743    m_pcAdaptiveLoopFilter->ALFProcess(&cAlfParam, NULL, pcSlice->getLambda(), ruiDist, ruiBits, m_pcEncTop->getInterViewSkip());
     1744#else
     1745    m_pcAdaptiveLoopFilter->ALFProcess(&cAlfParam, NULL, pcSlice->getLambda(), ruiDist, ruiBits);
     1746#endif
     1747#endif
     1748
     1749#endif
     1750#endif
    7011751    m_pcAdaptiveLoopFilter->endALFEnc();
     1752
     1753#if LCU_SYNTAX_ALF
     1754    alfParamSet->releaseALFParam();
     1755    delete alfParamSet;
     1756    delete alfCUCtrlParam;
     1757#else
    7021758    m_pcAdaptiveLoopFilter->freeALFParam(&cAlfParam);
    703   }
    704 
     1759#endif
     1760    m_pcAdaptiveLoopFilter->PCMLFDisableProcess(pcPic);
     1761    m_pcAdaptiveLoopFilter->destroyPicAlfInfo();
     1762  }
     1763  if( pcSlice->getSPS()->getUseSAO() || pcSlice->getSPS()->getUseALF())
     1764  {
     1765    pcPic->destroyNonDBFilterInfo();
     1766  }
     1767 
    7051768  m_pcEntropyCoder->resetEntropy    ();
    7061769  ruiBits += m_pcEntropyCoder->getNumberOfWrittenBits();
    707 
     1770 
    7081771  if (!bCalcDist)
    7091772    ruiDist = xFindDistortionFrame(pcPic->getPicYuvOrg(), pcPic->getPicYuvRec());
     
    7141777// ====================================================================================================================
    7151778
    716 UInt64 TEncPic::xFindDistortionFrame (TComPicYuv* pcPic0, TComPicYuv* pcPic1)
     1779Void TEncGOP::xInitGOP( Int iPOCLast, Int iNumPicRcvd, TComList<TComPic*>& rcListPic, TComList<TComPicYuv*>& rcListPicYuvRecOut )
     1780{
     1781  assert( iNumPicRcvd > 0 );
     1782  //  Exception for the first frame
     1783  if ( iPOCLast == 0 )
     1784  {
     1785    m_iGopSize    = 1;
     1786  }
     1787  else
     1788    m_iGopSize    = m_pcCfg->getGOPSize();
     1789 
     1790  assert (m_iGopSize > 0);
     1791
     1792  return;
     1793}
     1794
     1795Void TEncGOP::xGetBuffer( TComList<TComPic*>&       rcListPic,
     1796                         TComList<TComPicYuv*>&    rcListPicYuvRecOut,
     1797                         Int                       iNumPicRcvd,
     1798                         Int                       iTimeOffset,
     1799                         TComPic*&                 rpcPic,
     1800                         TComPicYuv*&              rpcPicYuvRecOut,
     1801                         UInt                      uiPOCCurr )
     1802{
     1803  Int i;
     1804  //  Rec. output
     1805  TComList<TComPicYuv*>::iterator     iterPicYuvRec = rcListPicYuvRecOut.end();
     1806  for ( i = 0; i < iNumPicRcvd - iTimeOffset + 1; i++ )
     1807  {
     1808    iterPicYuvRec--;
     1809  }
     1810 
     1811  rpcPicYuvRecOut = *(iterPicYuvRec);
     1812 
     1813  //  Current pic.
     1814  TComList<TComPic*>::iterator        iterPic       = rcListPic.begin();
     1815  while (iterPic != rcListPic.end())
     1816  {
     1817    rpcPic = *(iterPic);
     1818    rpcPic->setCurrSliceIdx(0);
     1819    if (rpcPic->getPOC() == (Int)uiPOCCurr)
     1820    {
     1821      break;
     1822    }
     1823    iterPic++;
     1824  }
     1825 
     1826  assert (rpcPic->getPOC() == (Int)uiPOCCurr);
     1827 
     1828  return;
     1829}
     1830
     1831UInt64 TEncGOP::xFindDistortionFrame (TComPicYuv* pcPic0, TComPicYuv* pcPic1)
    7171832{
    7181833  Int     x, y;
     
    7261841#endif
    7271842  Int   iTemp;
    728 
     1843 
    7291844  Int   iStride = pcPic0->getStride();
    7301845  Int   iWidth  = pcPic0->getWidth();
    7311846  Int   iHeight = pcPic0->getHeight();
    732 
     1847 
    7331848  UInt64  uiTotalDiff = 0;
    734 
     1849 
    7351850  for( y = 0; y < iHeight; y++ )
    7361851  {
     
    7461861    pSrc1 += iStride;
    7471862  }
    748 
     1863 
    7491864  iHeight >>= 1;
    7501865  iWidth  >>= 1;
    7511866  iStride >>= 1;
    752 
     1867 
    7531868  pSrc0  = pcPic0->getCbAddr();
    7541869  pSrc1  = pcPic1->getCbAddr();
    755 
     1870 
    7561871  for( y = 0; y < iHeight; y++ )
    7571872  {
     
    7671882    pSrc1 += iStride;
    7681883  }
    769 
     1884 
    7701885  pSrc0  = pcPic0->getCrAddr();
    7711886  pSrc1  = pcPic1->getCrAddr();
    772 
     1887 
    7731888  for( y = 0; y < iHeight; y++ )
    7741889  {
     
    7841899    pSrc1 += iStride;
    7851900  }
    786 
     1901 
    7871902  return uiTotalDiff;
    7881903}
    7891904
    790 Void TEncPic::xCalculateAddPSNR( TComPic* pcPic, TComPicYuv* pcPicD, UInt uibits, Double dEncTime )
     1905#if VERBOSE_RATE
     1906static const char* nalUnitTypeToString(NalUnitType type)
     1907{
     1908  switch (type)
     1909  {
     1910  case NAL_UNIT_CODED_SLICE: return "SLICE";
     1911#if H0566_TLA
     1912  case NAL_UNIT_CODED_SLICE_IDV: return "IDV";
     1913  case NAL_UNIT_CODED_SLICE_CRA: return "CRA";
     1914  case NAL_UNIT_CODED_SLICE_TLA: return "TLA";
     1915#else
     1916  case NAL_UNIT_CODED_SLICE_CDR: return "CDR";
     1917#endif
     1918  case NAL_UNIT_CODED_SLICE_IDR: return "IDR";
     1919  case NAL_UNIT_SEI: return "SEI";
     1920  case NAL_UNIT_SPS: return "SPS";
     1921  case NAL_UNIT_PPS: return "PPS";
     1922  case NAL_UNIT_FILLER_DATA: return "FILLER";
     1923  default: return "UNK";
     1924  }
     1925}
     1926#endif
     1927
     1928Void TEncGOP::xCalculateAddPSNR( TComPic* pcPic, TComPicYuv* pcPicD, const AccessUnit& accessUnit, Double dEncTime )
    7911929{
    7921930  Int     x, y;
     
    7941932  UInt64 uiSSDU  = 0;
    7951933  UInt64 uiSSDV  = 0;
    796 
     1934 
    7971935  Double  dYPSNR  = 0.0;
    7981936  Double  dUPSNR  = 0.0;
    7991937  Double  dVPSNR  = 0.0;
    800 
     1938 
    8011939  //===== calculate PSNR =====
    8021940  Pel*  pOrg    = pcPic ->getPicYuvOrg()->getLumaAddr();
    8031941  Pel*  pRec    = pcPicD->getLumaAddr();
    8041942  Int   iStride = pcPicD->getStride();
    805 
     1943 
    8061944  Int   iWidth;
    8071945  Int   iHeight;
    808 
     1946 
    8091947  iWidth  = pcPicD->getWidth () - m_pcEncTop->getPad(0);
    8101948  iHeight = pcPicD->getHeight() - m_pcEncTop->getPad(1);
    811 
     1949 
    8121950  Int   iSize   = iWidth*iHeight;
    813 
    814   UInt   maxval = 255 * (1<<(g_uiBitDepth + g_uiBitIncrement -8));
    815   Double fRefValueY = (double) maxval * maxval * iSize;
    816   Double fRefValueC = fRefValueY / 4.0;
    817 
     1951 
    8181952  for( y = 0; y < iHeight; y++ )
    8191953  {
     
    8261960    pRec += iStride;
    8271961  }
    828 
    829 #if HHI_VSO_PRINT_DIST
     1962 
     1963#if HHI_VSO
    8301964  if ( m_pcRdCost->getUseRenModel() )
    8311965  {
     1966    unsigned int maxval = 255 * (1<<(g_uiBitDepth + g_uiBitIncrement -8));
     1967    Double fRefValueY = (double) maxval * maxval * iSize;
     1968    Double fRefValueC = fRefValueY / 4.0;
    8321969    TRenModel*  pcRenModel = m_pcEncTop->getEncTop()->getRenModel();
    8331970    Int64 iDistVSOY, iDistVSOU, iDistVSOV;
     
    8451982  pOrg  = pcPic ->getPicYuvOrg()->getCbAddr();
    8461983  pRec  = pcPicD->getCbAddr();
    847 
     1984 
    8481985  for( y = 0; y < iHeight; y++ )
    8491986  {
     
    8561993    pRec += iStride;
    8571994  }
    858 
     1995 
    8591996  pOrg  = pcPic ->getPicYuvOrg()->getCrAddr();
    8601997  pRec  = pcPicD->getCrAddr();
    861 
     1998 
    8621999  for( y = 0; y < iHeight; y++ )
    8632000  {
     
    8702007    pRec += iStride;
    8712008  }
     2009 
     2010  unsigned int maxval = 255 * (1<<(g_uiBitDepth + g_uiBitIncrement -8));
     2011  Double fRefValueY = (double) maxval * maxval * iSize;
     2012  Double fRefValueC = fRefValueY / 4.0;
    8722013  dYPSNR            = ( uiSSDY ? 10.0 * log10( fRefValueY / (Double)uiSSDY ) : 99.99 );
    8732014  dUPSNR            = ( uiSSDU ? 10.0 * log10( fRefValueC / (Double)uiSSDU ) : 99.99 );
    8742015  dVPSNR            = ( uiSSDV ? 10.0 * log10( fRefValueC / (Double)uiSSDV ) : 99.99 );
    8752016  }
    876   // fix: total bits should consider slice size bits (32bit)
    877   uibits += 32;
    878 
    879 #if RVM_VCEGAM10
     2017  /* calculate the size of the access unit, excluding:
     2018   *  - any AnnexB contributions (start_code_prefix, zero_byte, etc.,)
     2019   *  - SEI NAL units
     2020   */
     2021  unsigned numRBSPBytes = 0;
     2022  for (AccessUnit::const_iterator it = accessUnit.begin(); it != accessUnit.end(); it++)
     2023  {
     2024    unsigned numRBSPBytes_nal = unsigned((*it)->m_nalUnitData.str().size());
     2025#if VERBOSE_RATE
     2026    printf("*** %6s numBytesInNALunit: %u\n", nalUnitTypeToString((*it)->m_nalUnitType), numRBSPBytes_nal);
     2027#endif
     2028    if ((*it)->m_nalUnitType != NAL_UNIT_SEI)
     2029      numRBSPBytes += numRBSPBytes_nal;
     2030  }
     2031
     2032  unsigned uibits = numRBSPBytes * 8;
    8802033  m_vRVM_RP.push_back( uibits );
    881 #endif
    8822034
    8832035  //===== add PSNR =====
    884   m_pcEncTop->m_cAnalyzeAll.addResult (dYPSNR, dUPSNR, dVPSNR, (Double)uibits);
     2036  m_pcEncTop->getAnalyzeAll()->addResult (dYPSNR, dUPSNR, dVPSNR, (Double)uibits);
    8852037  TComSlice*  pcSlice = pcPic->getSlice(0);
    8862038  if (pcSlice->isIntra())
    8872039  {
    888     m_pcEncTop->m_cAnalyzeI.addResult (dYPSNR, dUPSNR, dVPSNR, (Double)uibits);
     2040    m_pcEncTop->getAnalyzeI()->addResult (dYPSNR, dUPSNR, dVPSNR, (Double)uibits);
    8892041  }
    8902042  if (pcSlice->isInterP())
    8912043  {
    892     m_pcEncTop->m_cAnalyzeP.addResult (dYPSNR, dUPSNR, dVPSNR, (Double)uibits);
     2044    m_pcEncTop->getAnalyzeP()->addResult (dYPSNR, dUPSNR, dVPSNR, (Double)uibits);
    8932045  }
    8942046  if (pcSlice->isInterB())
    8952047  {
    896     m_pcEncTop->m_cAnalyzeB.addResult (dYPSNR, dUPSNR, dVPSNR, (Double)uibits);
    897   }
    898   if(pcPic->getViewIdx()!=-1)
    899   {
    900     if(! m_pcEncTop->isDepthCoder())
    901     {
    902       printf("\nView \t\t%2d\t POC %4d ( %c-SLICE, QP %d ) %10d bits ",
    903       pcSlice->getViewIdx(),
    904       pcSlice->getPOC(),
    905       pcSlice->isIntra() ? 'I' : pcSlice->isInterP() ? 'P' : 'B',
    906       pcSlice->getSliceQp(),
    907       uibits );
    908     }
    909     else
    910     {
    911       printf("\nDepth View \t%2d\t POC %4d ( %c-SLICE, QP %d ) %10d bits ",
    912             pcSlice->getViewIdx(),
    913             pcSlice->getPOC(),
    914             pcSlice->isIntra() ? 'I' : pcSlice->isInterP() ? 'P' : 'B',
    915             pcSlice->getSliceQp(),
    916             uibits );
    917     }
    918   }
    919   else
    920   {
    921     printf("\nPOC %4d ( %c-SLICE, QP %d ) %10d bits ",
    922            pcSlice->getPOC(),
    923            pcSlice->isIntra() ? 'I' : pcSlice->isInterP() ? 'P' : 'B',
    924            pcSlice->getSliceQp(),
    925            uibits );
    926   }
    927 
    928   printf( "[Y %6.4lf dB    U %6.4lf dB    V %6.4lf dB]  ", dYPSNR, dUPSNR, dVPSNR );
    929   printf ("[ET %5.0f ] ", dEncTime );
    930 
     2048    m_pcEncTop->getAnalyzeB()->addResult (dYPSNR, dUPSNR, dVPSNR, (Double)uibits);
     2049  }
     2050
     2051  Char c = (pcSlice->isIntra() ? 'I' : pcSlice->isInterP() ? 'P' : 'B');
     2052  if (!pcSlice->isReferenced()) c += 32;
     2053
     2054#if ADAPTIVE_QP_SELECTION
     2055  printf("%s   View %3d POC %4d TId: %1d ( %c-SLICE, nQP %d QP %d ) %10d bits",
     2056         pcSlice->getIsDepth() ? "Depth  " : "Texture",
     2057         pcSlice->getViewId(),
     2058         pcSlice->getPOC(),
     2059         pcSlice->getTLayer(),
     2060         c,
     2061         pcSlice->getSliceQpBase(),
     2062         pcSlice->getSliceQp(),
     2063         uibits );
     2064#else
     2065  printf("%s   View %3d POC %4d TId: %1d ( %c-SLICE, QP %d ) %10d bits",
     2066         pcSlice->getIsDepth() ? "Depth  " : "Texture",
     2067         pcSlice->getViewId(),
     2068         pcSlice->getPOC()-pcSlice->getLastIDR(),
     2069         pcSlice->getTLayer(),
     2070         c,
     2071         pcSlice->getSliceQp(),
     2072         uibits );
     2073#endif
     2074
     2075  printf(" [Y %6.4lf dB    U %6.4lf dB    V %6.4lf dB]", dYPSNR, dUPSNR, dVPSNR );
     2076  printf(" [ET %5.0f ]", dEncTime );
     2077 
    9312078  for (Int iRefList = 0; iRefList < 2; iRefList++)
    9322079  {
    933     printf ("[L%d ", iRefList);
     2080    printf(" [L%d ", iRefList);
    9342081    for (Int iRefIndex = 0; iRefIndex < pcSlice->getNumRefIdx(RefPicList(iRefList)); iRefIndex++)
    9352082    {
    936       if( pcSlice->getViewIdx() != pcSlice->getRefViewIdx( RefPicList(iRefList), iRefIndex ) )
    937       {
    938         printf( "V%d", pcSlice->getRefViewIdx( RefPicList(iRefList), iRefIndex ) );
    939         if( pcSlice->getPOC() != pcSlice->getRefPOC( RefPicList(iRefList), iRefIndex ) )
    940           printf( "(%d)", pcSlice->getRefPOC( RefPicList(iRefList), iRefIndex ) );
    941         printf( " " );
     2083      if( pcSlice->getViewId() != pcSlice->getRefViewId( RefPicList(iRefList), iRefIndex ) )
     2084      {
     2085        printf( "V%d ", pcSlice->getRefViewId( RefPicList(iRefList), iRefIndex ) );
    9422086      }
    9432087      else
    944         printf ("%d ", pcSlice->getRefPOC(RefPicList(iRefList), iRefIndex));
    945     }
    946     printf ("] ");
    947   }
    948 #if DCM_COMB_LIST
     2088      {
     2089        printf ("%d ", pcSlice->getRefPOC(RefPicList(iRefList), iRefIndex)-pcSlice->getLastIDR());
     2090      }
     2091    }
     2092    printf("]");
     2093  }
    9492094  if(pcSlice->getNumRefIdx(REF_PIC_LIST_C)>0 && !pcSlice->getNoBackPredFlag())
    9502095  {
    951     printf ("[LC ");
     2096    printf(" [LC ");
    9522097    for (Int iRefIndex = 0; iRefIndex < pcSlice->getNumRefIdx(REF_PIC_LIST_C); iRefIndex++)
    9532098    {
    954       printf ("%d ", pcSlice->getRefPOC((RefPicList)pcSlice->getListIdFromIdxOfLC(iRefIndex), pcSlice->getRefIdxFromIdxOfLC(iRefIndex)));
    955     }
    956     printf ("] ");
    957   }
    958 #endif
    959 
    960   fflush(stdout);
     2099      if( pcSlice->getViewId() != pcSlice->getRefViewId( (RefPicList)pcSlice->getListIdFromIdxOfLC(iRefIndex), pcSlice->getRefIdxFromIdxOfLC(iRefIndex) ) )
     2100      {
     2101        printf( "V%d ", pcSlice->getRefViewId( (RefPicList)pcSlice->getListIdFromIdxOfLC(iRefIndex), pcSlice->getRefIdxFromIdxOfLC(iRefIndex) ) );
     2102      }
     2103      else
     2104      {
     2105        printf ("%d ", pcSlice->getRefPOC((RefPicList)pcSlice->getListIdFromIdxOfLC(iRefIndex), pcSlice->getRefIdxFromIdxOfLC(iRefIndex))-pcSlice->getLastIDR());
     2106      }
     2107    }
     2108    printf("]");
     2109  }
    9612110}
    9622111
    963 #if DCM_DECODING_REFRESH
    9642112/** Function for deciding the nal_unit_type.
    9652113 * \param uiPOCCurr POC of the current picture
     
    9672115 * This function checks the configuration and returns the appropriate nal_unit_type for the picture.
    9682116 */
    969 NalUnitType TEncPic::getNalUnitType(UInt uiPOCCurr)
     2117NalUnitType TEncGOP::getNalUnitType(UInt uiPOCCurr)
    9702118{
     2119  Bool bInterViewOnlySlice = ( m_pcCfg->getGOPEntry(MAX_GOP).m_POC == 0 && (m_pcCfg->getGOPEntry(MAX_GOP).m_sliceType == 'P' || m_pcCfg->getGOPEntry(MAX_GOP).m_sliceType == 'B') );
     2120
    9712121  if (uiPOCCurr == 0)
    9722122  {
    973     return NAL_UNIT_CODED_SLICE_IDR;
    974   }
    975 #if 0
     2123    if( bInterViewOnlySlice )
     2124    {
     2125      return NAL_UNIT_CODED_SLICE_IDV;
     2126    }
     2127    else
     2128    {
     2129      return NAL_UNIT_CODED_SLICE_IDR;
     2130    }
     2131  }
    9762132  if (uiPOCCurr % m_pcCfg->getIntraPeriod() == 0)
    9772133  {
    9782134    if (m_pcCfg->getDecodingRefreshType() == 1)
    9792135    {
     2136      if( bInterViewOnlySlice )
     2137      {
     2138        return NAL_UNIT_CODED_SLICE_IDV;
     2139      }
     2140      else
     2141      {
     2142#if H0566_TLA
     2143      return NAL_UNIT_CODED_SLICE_CRA;
     2144#else
    9802145      return NAL_UNIT_CODED_SLICE_CDR;
     2146#endif
     2147      }
    9812148    }
    9822149    else if (m_pcCfg->getDecodingRefreshType() == 2)
    9832150    {
    984       return NAL_UNIT_CODED_SLICE_IDR;
    985     }
    986   }
    987 #endif
     2151      if( bInterViewOnlySlice )
     2152      {
     2153        return NAL_UNIT_CODED_SLICE_IDV;
     2154      }
     2155      else
     2156      {
     2157        return NAL_UNIT_CODED_SLICE_IDR;
     2158      }
     2159    }
     2160  }
    9882161  return NAL_UNIT_CODED_SLICE;
    9892162}
    990 #endif
    991 
    992 #if RVM_VCEGAM10
    993 Double TEncPic::xCalculateRVM()
     2163
     2164NalUnitType TEncGOP::getNalUnitTypeBaseViewMvc(UInt uiPOCCurr)
     2165{
     2166  if( uiPOCCurr == 0 )
     2167  {
     2168    return NAL_UNIT_CODED_SLICE_IDR;
     2169  }
     2170  if( uiPOCCurr % m_pcCfg->getIntraPeriod() == 0 )
     2171  {
     2172    if( m_pcCfg->getDecodingRefreshType() == 1 )
     2173    {
     2174#if H0566_TLA
     2175      return NAL_UNIT_CODED_SLICE_CRA;
     2176#else
     2177      return NAL_UNIT_CODED_SLICE_CDR;
     2178#endif
     2179    }
     2180    else if( m_pcCfg->getDecodingRefreshType() == 2 )
     2181    {
     2182      return NAL_UNIT_CODED_SLICE_IDR;
     2183    }
     2184  }
     2185  return NAL_UNIT_CODED_SLICE;
     2186}
     2187
     2188Double TEncGOP::xCalculateRVM()
    9942189{
    9952190  Double dRVM = 0;
    996 
    997   //if( m_pcCfg->getGOPSize() == 1 && m_pcCfg->getIntraPeriod() != 1 && m_pcCfg->getFrameToBeEncoded() > RVM_VCEGAM10_M * 2 )
     2191 
     2192  if( m_pcCfg->getGOPSize() == 1 && m_pcCfg->getIntraPeriod() != 1 && m_pcCfg->getFrameToBeEncoded() > RVM_VCEGAM10_M * 2 )
    9982193  {
    9992194    // calculate RVM only for lowdelay configurations
     
    10022197    vRL.resize( N );
    10032198    vB.resize( N );
    1004 
     2199   
    10052200    Int i;
    10062201    Double dRavg = 0 , dBavg = 0;
     
    10162211      dBavg += vB[i];
    10172212    }
    1018 
     2213   
    10192214    dRavg /= ( N - 2 * RVM_VCEGAM10_M );
    10202215    dBavg /= ( N - 2 * RVM_VCEGAM10_M );
    1021 
     2216   
    10222217    double dSigamB = 0;
    10232218    for( i = RVM_VCEGAM10_M + 1 ; i < N - RVM_VCEGAM10_M + 1 ; i++ )
     
    10272222    }
    10282223    dSigamB = sqrt( dSigamB / ( N - 2 * RVM_VCEGAM10_M ) );
    1029 
     2224   
    10302225    double f = sqrt( 12.0 * ( RVM_VCEGAM10_M - 1 ) / ( RVM_VCEGAM10_M + 1 ) );
    1031 
     2226   
    10322227    dRVM = dSigamB / dRavg * f;
    10332228  }
    1034 
     2229 
    10352230  return( dRVM );
    10362231}
    1037 #endif
    1038 
     2232
     2233/** Determine the difference between consecutive tile sizes (in bytes) and writes it to  bistream rNalu [slice header]
     2234 * \param rpcBitstreamRedirect contains the bitstream to be concatenated to rNalu. rpcBitstreamRedirect contains slice payload. rpcSlice contains tile location information.
     2235 * \returns Updates rNalu to contain concatenated bitstream. rpcBitstreamRedirect is cleared at the end of this function call.
     2236 */
     2237Void TEncGOP::xWriteTileLocationToSliceHeader (OutputNALUnit& rNalu, TComOutputBitstream*& rpcBitstreamRedirect, TComSlice*& rpcSlice)
     2238{
     2239  {
     2240#if !TILES_WPP_ENTRY_POINT_SIGNALLING
     2241    Int iTransmitTileLocationInSliceHeader = (rpcSlice->getTileLocationCount()==0 || m_pcCfg->getTileLocationInSliceHeaderFlag()==0) ? 0 : 1;
     2242    rNalu.m_Bitstream.write(iTransmitTileLocationInSliceHeader, 1);   // write flag indicating whether tile location information communicated in slice header
     2243
     2244    if (iTransmitTileLocationInSliceHeader)
     2245    {
     2246      rNalu.m_Bitstream.write(rpcSlice->getTileLocationCount()-1, 5);   // write number of tiles
     2247
     2248      Int *aiDiff;
     2249      aiDiff = new Int [rpcSlice->getTileLocationCount()];
     2250
     2251      // Find largest number of bits required by Diff
     2252      Int iLastSize = 0, iDiffMax = 0, iDiffMin = 0;
     2253      for (UInt uiIdx=0; uiIdx<rpcSlice->getTileLocationCount(); uiIdx++)
     2254      {
     2255        Int iCurDiff, iCurSize;
     2256        if (uiIdx==0)
     2257        {
     2258          iCurDiff  = rpcSlice->getTileLocation( uiIdx );
     2259          iLastSize = rpcSlice->getTileLocation( uiIdx );
     2260        }
     2261        else
     2262        {
     2263          iCurSize  = rpcSlice->getTileLocation( uiIdx )  - rpcSlice->getTileLocation( uiIdx-1 );
     2264          iCurDiff  = iCurSize - iLastSize;
     2265          iLastSize = iCurSize;
     2266        }
     2267        // Store Diff so it may be written to slice header later without re-calculating.
     2268        aiDiff[uiIdx] = iCurDiff;
     2269
     2270        if (iCurDiff>iDiffMax)
     2271        {
     2272          iDiffMax = iCurDiff;
     2273        }
     2274        if (iCurDiff<iDiffMin)
     2275        {
     2276          iDiffMin = iCurDiff;
     2277        }
     2278      }
     2279
     2280      Int iDiffMinAbs, iDiffMaxAbs;
     2281      iDiffMinAbs = (iDiffMin<0) ? (-iDiffMin) : iDiffMin;
     2282      iDiffMaxAbs = (iDiffMax<0) ? (-iDiffMax) : iDiffMax;
     2283
     2284      Int iBitsUsedByDiff = 0, iDiffAbsLargest;
     2285      iDiffAbsLargest = (iDiffMinAbs < iDiffMaxAbs) ? iDiffMaxAbs : iDiffMinAbs;       
     2286      while (1)
     2287      {
     2288        if (iDiffAbsLargest >= (1 << iBitsUsedByDiff) )
     2289        {
     2290          iBitsUsedByDiff++;
     2291        }
     2292        else
     2293        {
     2294          break;
     2295        }
     2296      }
     2297      iBitsUsedByDiff++;
     2298
     2299      if (iBitsUsedByDiff > 32)
     2300      {
     2301        printf("\nDiff magnitude uses more than 32-bits");
     2302        assert ( 0 );
     2303        exit ( 0 ); // trying to catch any problems with using fixed bits for Diff information
     2304      }
     2305
     2306      rNalu.m_Bitstream.write( iBitsUsedByDiff-1, 5 ); // write number of bits used by Diff
     2307
     2308      // Write diff to slice header (rNalu)
     2309      for (UInt uiIdx=0; uiIdx<rpcSlice->getTileLocationCount(); uiIdx++)
     2310      {
     2311        Int iCurDiff = aiDiff[uiIdx];
     2312
     2313        // write sign of diff
     2314        if (uiIdx!=0)
     2315        {
     2316          if (iCurDiff<0)         
     2317          {
     2318            rNalu.m_Bitstream.write(1, 1);
     2319          }
     2320          else
     2321          {
     2322            rNalu.m_Bitstream.write(0, 1);
     2323          }
     2324        }
     2325
     2326        // write abs value of diff
     2327        Int iAbsDiff = (iCurDiff<0) ? (-iCurDiff) : iCurDiff;
     2328        if (iAbsDiff > ((((UInt64)1)<<32)-1))
     2329        {
     2330          printf("\niAbsDiff exceeds 32-bit limit");
     2331          exit(0);
     2332        }
     2333        rNalu.m_Bitstream.write( iAbsDiff, iBitsUsedByDiff-1 );
     2334      }
     2335
     2336      delete [] aiDiff;
     2337    }
     2338#endif
     2339  }
     2340
     2341  // Byte-align
     2342  rNalu.m_Bitstream.writeAlignOne();
     2343
     2344  // Update tile marker locations
     2345  TComOutputBitstream *pcOut = &rNalu.m_Bitstream;
     2346  UInt uiAccumulatedLength   = pcOut->getNumberOfWrittenBits() >> 3;
     2347  for (Int uiMrkIdx = 0; uiMrkIdx < rpcBitstreamRedirect->getTileMarkerLocationCount(); uiMrkIdx++)
     2348  {
     2349    UInt uiBottom = pcOut->getTileMarkerLocationCount();
     2350    pcOut->setTileMarkerLocation      ( uiBottom, uiAccumulatedLength + rpcBitstreamRedirect->getTileMarkerLocation( uiMrkIdx ) );
     2351    pcOut->setTileMarkerLocationCount ( uiBottom + 1 );
     2352  }
     2353
     2354  // Perform bitstream concatenation
     2355  if (rpcBitstreamRedirect->getNumberOfWrittenBits() > 0)
     2356  {
     2357    UInt uiBitCount  = rpcBitstreamRedirect->getNumberOfWrittenBits();
     2358    if (rpcBitstreamRedirect->getByteStreamLength()>0)
     2359    {
     2360      UChar *pucStart  =  reinterpret_cast<UChar*>(rpcBitstreamRedirect->getByteStream());
     2361      UInt uiWriteByteCount = 0;
     2362      while (uiWriteByteCount < (uiBitCount >> 3) )
     2363      {
     2364        UInt uiBits = (*pucStart);
     2365        rNalu.m_Bitstream.write(uiBits, 8);
     2366        pucStart++;
     2367        uiWriteByteCount++;
     2368      }
     2369    }
     2370    UInt uiBitsHeld = (uiBitCount & 0x07);
     2371    for (UInt uiIdx=0; uiIdx < uiBitsHeld; uiIdx++)
     2372    {
     2373      rNalu.m_Bitstream.write((rpcBitstreamRedirect->getHeldBits() & (1 << (7-uiIdx))) >> (7-uiIdx), 1);
     2374    }         
     2375  }
     2376
     2377  m_pcEntropyCoder->setBitstream(&rNalu.m_Bitstream);
     2378
     2379  delete rpcBitstreamRedirect;
     2380  rpcBitstreamRedirect = new TComOutputBitstream;
     2381}
     2382
     2383Void TEncGOP::xSetRefPicListModificationsMvc( TComSlice* pcSlice, UInt uiPOCCurr, UInt iGOPid )
     2384{
     2385  if( pcSlice->getSliceType() == I_SLICE || !(pcSlice->getSPS()->getListsModificationPresentFlag()) || pcSlice->getSPS()->getNumberOfUsableInterViewRefs() == 0 )
     2386  {
     2387    return;
     2388  }
     2389
     2390  // analyze inter-view modifications
     2391  GOPEntryMvc gem = m_pcCfg->getGOPEntry( (getNalUnitType(uiPOCCurr) == NAL_UNIT_CODED_SLICE_IDV) ? MAX_GOP : iGOPid );
     2392  Int numL0Modifications = 0;
     2393  Int numL1Modifications = 0;
     2394  for( Int k = 0; k < gem.m_numInterViewRefPics; k++ )
     2395  {
     2396    if( gem.m_interViewRefPosL0[k] > 0 ) { numL0Modifications++; }
     2397    if( gem.m_interViewRefPosL1[k] > 0 ) { numL1Modifications++; }
     2398  }
     2399
     2400  TComRefPicListModification* refPicListModification = pcSlice->getRefPicListModification();
     2401  Int maxRefListSize = pcSlice->getNumPocTotalCurrMvc();
     2402  Int numTemporalRefs = pcSlice->getNumPocTotalCurr();
     2403
     2404  // set L0 inter-view modifications
     2405  if( (maxRefListSize > 1) && (numL0Modifications > 0) )
     2406  {
     2407    refPicListModification->setRefPicListModificationFlagL0( true );
     2408    Int tempListEntryL0[16];
     2409    for( Int k = 0; k < 16; k++ ) { tempListEntryL0[k] = -1; }
     2410   
     2411    Bool hasModification = false;
     2412    for( Int k = 0; k < gem.m_numInterViewRefPics; k++ )
     2413    {
     2414      if( gem.m_interViewRefPosL0[k] > 0 )
     2415      {
     2416        for( Int l = 0; l < pcSlice->getSPS()->getNumberOfUsableInterViewRefs(); l++ )
     2417        {
     2418          if( gem.m_interViewRefs[k] == pcSlice->getSPS()->getUsableInterViewRef( l ) && (gem.m_interViewRefPosL0[k] - 1) != (numTemporalRefs + l) )
     2419          {
     2420            tempListEntryL0[gem.m_interViewRefPosL0[k]-1] = numTemporalRefs + l;
     2421            hasModification = true;
     2422          }
     2423        }
     2424      }
     2425    }
     2426
     2427    if( hasModification )
     2428    {
     2429            Int temporalRefIdx = 0;
     2430            for( Int i = 0; i < pcSlice->getNumRefIdx( REF_PIC_LIST_0 ); i++ )
     2431            {
     2432              if( tempListEntryL0[i] >= 0 )
     2433              {
     2434                refPicListModification->setRefPicSetIdxL0( i, tempListEntryL0[i] );
     2435              }
     2436              else
     2437              {
     2438                refPicListModification->setRefPicSetIdxL0( i, temporalRefIdx );
     2439                temporalRefIdx++;
     2440              }
     2441            }
     2442          }
     2443    else
     2444    {
     2445      refPicListModification->setRefPicListModificationFlagL0( false );
     2446    }
     2447  }
     2448
     2449  // set L1 inter-view modifications
     2450  if( (maxRefListSize > 1) && (numL1Modifications > 0) )
     2451  {
     2452    refPicListModification->setRefPicListModificationFlagL1( true );
     2453    Int tempListEntryL1[16];
     2454    for( Int k = 0; k < 16; k++ ) { tempListEntryL1[k] = -1; }
     2455
     2456    Bool hasModification = false;
     2457    for( Int k = 0; k < gem.m_numInterViewRefPics; k++ )
     2458    {
     2459      if( gem.m_interViewRefPosL1[k] > 0 )
     2460      {
     2461        for( Int l = 0; l < pcSlice->getSPS()->getNumberOfUsableInterViewRefs(); l++ )
     2462        {
     2463          if( gem.m_interViewRefs[k] == pcSlice->getSPS()->getUsableInterViewRef( l ) && (gem.m_interViewRefPosL1[k] - 1) != (numTemporalRefs + l) )
     2464          {
     2465            tempListEntryL1[gem.m_interViewRefPosL1[k]-1] = numTemporalRefs + l;
     2466            hasModification = true;
     2467          }
     2468        }
     2469      }
     2470    }
     2471
     2472    if( hasModification )
     2473    {
     2474            Int temporalRefIdx = 0;
     2475            for( Int i = 0; i < pcSlice->getNumRefIdx( REF_PIC_LIST_1 ); i++ )
     2476            {
     2477              if( tempListEntryL1[i] >= 0 )
     2478              {
     2479                refPicListModification->setRefPicSetIdxL1( i, tempListEntryL1[i] );
     2480              }
     2481              else
     2482              {
     2483                refPicListModification->setRefPicSetIdxL1( i, temporalRefIdx );
     2484                temporalRefIdx++;
     2485              }
     2486            }
     2487    }
     2488    else
     2489    {
     2490      refPicListModification->setRefPicListModificationFlagL1( false );
     2491    }
     2492  }
     2493
     2494  return;
     2495}
     2496//! \}
Note: See TracChangeset for help on using the changeset viewer.