source: 3DVCSoftware/trunk/source/Lib/TLibEncoder/TEncGOP.cpp @ 56

Last change on this file since 56 was 56, checked in by hschwarz, 12 years ago

updated trunk (move to HM6.1)

  • Property svn:eol-style set to native
File size: 92.1 KB
Line 
1/* The copyright in this software is being made available under the BSD
2 * License, included below. This software may be subject to other third party
3 * and contributor rights, including patent rights, and no such rights are
4 * granted under this license. 
5 *
6 * Copyright (c) 2010-2012, ITU/ISO/IEC
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 *
12 *  * Redistributions of source code must retain the above copyright notice,
13 *    this list of conditions and the following disclaimer.
14 *  * Redistributions in binary form must reproduce the above copyright notice,
15 *    this list of conditions and the following disclaimer in the documentation
16 *    and/or other materials provided with the distribution.
17 *  * Neither the name of the ITU/ISO/IEC nor the names of its contributors may
18 *    be used to endorse or promote products derived from this software without
19 *    specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31 * THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34/** \file     TEncGOP.cpp
35    \brief    GOP encoder class
36*/
37
38#include <list>
39#include <algorithm>
40
41#include "TEncTop.h"
42#include "TEncGOP.h"
43#include "TEncAnalyze.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"
49
50#include <time.h>
51#include <math.h>
52
53using namespace std;
54
55//! \ingroup TLibEncoder
56//! \{
57
58// ====================================================================================================================
59// Constructor / destructor / initialization / destroy
60// ====================================================================================================================
61
62TEncGOP::TEncGOP()
63{
64  m_iLastIDR            = 0;
65  m_iGopSize            = 0;
66  m_iNumPicCoded        = 0; //Niko
67  m_bFirst              = true;
68 
69  m_pcCfg               = NULL;
70  m_pcSliceEncoder      = NULL;
71  m_pcListPic           = NULL;
72 
73  m_pcEntropyCoder      = NULL;
74  m_pcCavlcCoder        = NULL;
75  m_pcSbacCoder         = NULL;
76  m_pcBinCABAC          = NULL;
77#if DEPTH_MAP_GENERATION
78  m_pcDepthMapGenerator = NULL;
79#endif
80#if HHI_INTER_VIEW_RESIDUAL_PRED
81  m_pcResidualGenerator = NULL;
82#endif
83 
84  m_bSeqFirst           = true;
85 
86  m_bRefreshPending     = 0;
87  m_pocCRA              = 0;
88
89  return;
90}
91
92TEncGOP::~TEncGOP()
93{
94}
95
96/** Create list to contain pointers to LCU start addresses of slice.
97 * \param iWidth, iHeight are picture width, height. iMaxCUWidth, iMaxCUHeight are LCU width, height.
98 */
99Void  TEncGOP::create( Int iWidth, Int iHeight, UInt iMaxCUWidth, UInt iMaxCUHeight )
100{
101  UInt uiWidthInCU       = ( iWidth %iMaxCUWidth  ) ? iWidth /iMaxCUWidth  + 1 : iWidth /iMaxCUWidth;
102  UInt uiHeightInCU      = ( iHeight%iMaxCUHeight ) ? iHeight/iMaxCUHeight + 1 : iHeight/iMaxCUHeight;
103  UInt uiNumCUsInFrame   = uiWidthInCU * uiHeightInCU;
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;
108}
109
110Void  TEncGOP::destroy()
111{
112  delete [] m_uiStoredStartCUAddrForEncodingSlice; m_uiStoredStartCUAddrForEncodingSlice = NULL;
113  delete [] m_uiStoredStartCUAddrForEncodingEntropySlice; m_uiStoredStartCUAddrForEncodingEntropySlice = NULL;
114}
115
116Void TEncGOP::init ( TEncTop* pcTEncTop )
117{
118  m_pcEncTop     = pcTEncTop;
119  m_pcCfg                = pcTEncTop;
120  m_pcSliceEncoder       = pcTEncTop->getSliceEncoder();
121  m_pcListPic            = pcTEncTop->getListPic();
122 
123  m_pcEntropyCoder       = pcTEncTop->getEntropyCoder();
124  m_pcCavlcCoder         = pcTEncTop->getCavlcCoder();
125  m_pcSbacCoder          = pcTEncTop->getSbacCoder();
126  m_pcBinCABAC           = pcTEncTop->getBinCABAC();
127  m_pcLoopFilter         = pcTEncTop->getLoopFilter();
128  m_pcBitCounter         = pcTEncTop->getBitCounter();
129 
130#if DEPTH_MAP_GENERATION
131  m_pcDepthMapGenerator  = pcTEncTop->getDepthMapGenerator();
132#endif
133#if HHI_INTER_VIEW_RESIDUAL_PRED
134  m_pcResidualGenerator  = pcTEncTop->getResidualGenerator();
135#endif
136 
137  // Adaptive Loop filter
138  m_pcAdaptiveLoopFilter = pcTEncTop->getAdaptiveLoopFilter();
139  //--Adaptive Loop filter
140  m_pcSAO                = pcTEncTop->getSAO();
141  m_pcRdCost             = pcTEncTop->getRdCost();
142}
143
144// ====================================================================================================================
145// Public member functions
146// ====================================================================================================================
147
148Void TEncGOP::initGOP( Int iPOCLast, Int iNumPicRcvd, TComList<TComPic*>& rcListPic, TComList<TComPicYuv*>& rcListPicYuvRecOut, std::list<AccessUnit>& accessUnitsInGOP)
149{
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;
158  TComSlice*      pcSlice;
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;
172      //-- For time output for each slice
173      long iBeforeTime = clock();
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     
241      //  Slice data initialization
242      pcPic->clearSliceBuffer();
243      assert(pcPic->getNumAllocatedSlice() == 1);
244      m_pcSliceEncoder->setSliceIdx(0);
245      pcPic->setCurrSliceIdx(0);
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);
250      pcSlice->setSliceIdx(0);
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
299      // Set the nal unit type
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      }
370      else
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     
431#if HHI_VSO
432  Bool bUseVSO = m_pcEncTop->getUseVSO();
433  m_pcRdCost->setUseVSO( bUseVSO );
434
435  if ( bUseVSO )
436  {
437    Int iVSOMode = m_pcEncTop->getVSOMode();
438    m_pcRdCost->setVSOMode( iVSOMode  );
439#if HHI_VSO_DIST_INT
440    m_pcRdCost->setAllowNegDist( m_pcEncTop->getAllowNegDist() );
441#endif
442
443    if ( iVSOMode == 4 )
444    {
445      m_pcEncTop->getEncTop()->setupRenModel( pcSlice->getPOC(), pcSlice->getViewId(), m_pcEncTop->isDepthCoder() ? 1 : 0 );
446    }
447    else
448    {
449      AOT(true); 
450    }
451  }
452#endif
453      /////////////////////////////////////////////////////////////////////////////////////////////////// Compress a slice
454      //  Slice compression
455      if (m_pcCfg->getUseASR())
456      {
457        m_pcSliceEncoder->setSearchRange(pcSlice);
458      }
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    }
671
672      UInt uiStartCUAddrSliceIdx = 0; // used to index "m_uiStoredStartCUAddrForEncodingSlice" containing locations of slice boundaries
673      UInt uiStartCUAddrSlice    = 0; // used to keep track of current slice's starting CU addr.
674      pcSlice->setSliceCurStartCUAddr( uiStartCUAddrSlice ); // Setting "start CU addr" for current slice
675      memset(m_uiStoredStartCUAddrForEncodingSlice, 0, sizeof(UInt) * (pcPic->getPicSym()->getNumberOfCUsInFrame()*pcPic->getNumPartInCU()+1));
676
677      UInt uiStartCUAddrEntropySliceIdx = 0; // used to index "m_uiStoredStartCUAddrForEntropyEncodingSlice" containing locations of slice boundaries
678      UInt uiStartCUAddrEntropySlice    = 0; // used to keep track of current Entropy slice's starting CU addr.
679      pcSlice->setEntropySliceCurStartCUAddr( uiStartCUAddrEntropySlice ); // Setting "start CU addr" for current Entropy slice
680     
681      memset(m_uiStoredStartCUAddrForEncodingEntropySlice, 0, sizeof(UInt) * (pcPic->getPicSym()->getNumberOfCUsInFrame()*pcPic->getNumPartInCU()+1));
682      UInt uiNextCUAddr = 0;
683      m_uiStoredStartCUAddrForEncodingSlice[uiStartCUAddrSliceIdx++]                = uiNextCUAddr;
684      m_uiStoredStartCUAddrForEncodingEntropySlice[uiStartCUAddrEntropySliceIdx++]  = uiNextCUAddr;
685
686#if DEPTH_MAP_GENERATION
687      // init view component and predict virtual depth map
688      m_pcDepthMapGenerator->initViewComponent( pcPic );
689      m_pcDepthMapGenerator->predictDepthMap  ( pcPic );
690#endif
691#if HHI_INTER_VIEW_MOTION_PRED
692      m_pcDepthMapGenerator->covertOrgDepthMap( pcPic );
693#endif
694#if HHI_INTER_VIEW_RESIDUAL_PRED
695      m_pcResidualGenerator->initViewComponent( pcPic );
696#endif
697
698      while(uiNextCUAddr<uiRealEndAddress) // determine slice boundaries
699      {
700        pcSlice->setNextSlice       ( false );
701        pcSlice->setNextEntropySlice( false );
702        assert(pcPic->getNumAllocatedSlice() == uiStartCUAddrSliceIdx);
703        m_pcSliceEncoder->precompressSlice( pcPic );
704        m_pcSliceEncoder->compressSlice   ( pcPic );
705
706        Bool bNoBinBitConstraintViolated = (!pcSlice->isNextSlice() && !pcSlice->isNextEntropySlice());
707        if (pcSlice->isNextSlice() || (bNoBinBitConstraintViolated && m_pcCfg->getSliceMode()==AD_HOC_SLICES_FIXED_NUMBER_OF_LCU_IN_SLICE))
708        {
709          uiStartCUAddrSlice                                              = pcSlice->getSliceCurEndCUAddr();
710          // Reconstruction slice
711          m_uiStoredStartCUAddrForEncodingSlice[uiStartCUAddrSliceIdx++]  = uiStartCUAddrSlice;
712          // Entropy slice
713          if (uiStartCUAddrEntropySliceIdx>0 && m_uiStoredStartCUAddrForEncodingEntropySlice[uiStartCUAddrEntropySliceIdx-1] != uiStartCUAddrSlice)
714          {
715            m_uiStoredStartCUAddrForEncodingEntropySlice[uiStartCUAddrEntropySliceIdx++]  = uiStartCUAddrSlice;
716          }
717         
718          if (uiStartCUAddrSlice < uiRealEndAddress)
719          {
720            pcPic->allocateNewSlice();         
721            pcPic->setCurrSliceIdx                  ( uiStartCUAddrSliceIdx-1 );
722            m_pcSliceEncoder->setSliceIdx           ( uiStartCUAddrSliceIdx-1 );
723            pcSlice = pcPic->getSlice               ( uiStartCUAddrSliceIdx-1 );
724            pcSlice->copySliceInfo                  ( pcPic->getSlice(0)      );
725            pcSlice->setSliceIdx                    ( uiStartCUAddrSliceIdx-1 );
726            pcSlice->setSliceCurStartCUAddr         ( uiStartCUAddrSlice      );
727            pcSlice->setEntropySliceCurStartCUAddr  ( uiStartCUAddrSlice      );
728            pcSlice->setSliceBits(0);
729            uiNumSlices ++;
730          }
731        }
732        else if (pcSlice->isNextEntropySlice() || (bNoBinBitConstraintViolated && m_pcCfg->getEntropySliceMode()==SHARP_FIXED_NUMBER_OF_LCU_IN_ENTROPY_SLICE))
733        {
734          uiStartCUAddrEntropySlice                                                     = pcSlice->getEntropySliceCurEndCUAddr();
735          m_uiStoredStartCUAddrForEncodingEntropySlice[uiStartCUAddrEntropySliceIdx++]  = uiStartCUAddrEntropySlice;
736          pcSlice->setEntropySliceCurStartCUAddr( uiStartCUAddrEntropySlice );
737        }
738        else
739        {
740          uiStartCUAddrSlice                                                            = pcSlice->getSliceCurEndCUAddr();
741          uiStartCUAddrEntropySlice                                                     = pcSlice->getEntropySliceCurEndCUAddr();
742        }       
743
744        uiNextCUAddr = (uiStartCUAddrSlice > uiStartCUAddrEntropySlice) ? uiStartCUAddrSlice : uiStartCUAddrEntropySlice;
745      }
746      m_uiStoredStartCUAddrForEncodingSlice[uiStartCUAddrSliceIdx++]                = pcSlice->getSliceCurEndCUAddr();
747      m_uiStoredStartCUAddrForEncodingEntropySlice[uiStartCUAddrEntropySliceIdx++]  = pcSlice->getSliceCurEndCUAddr();
748     
749      pcSlice = pcPic->getSlice(0);
750
751#if HHI_INTER_VIEW_RESIDUAL_PRED
752      // set residual picture
753      m_pcResidualGenerator->setRecResidualPic( pcPic );
754#endif
755#if DEPTH_MAP_GENERATION
756      // update virtual depth map
757      m_pcDepthMapGenerator->updateDepthMap( pcPic );
758#endif
759
760      //-- Loop filter
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
768      m_pcLoopFilter->loopFilterPic( pcPic );
769
770      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);
792
793      if(pcSlice->getSPS()->getUseALF())
794      {
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
804      /////////////////////////////////////////////////////////////////////////////////////////////////// File writing
805      // Set entropy coder
806      m_pcEntropyCoder->setEntropyCoder   ( m_pcCavlcCoder, pcSlice );
807
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;
839      }
840
841      /* use the main bitstream buffer for storing the marshalled picture */
842      m_pcEntropyCoder->setBitstream(NULL);
843
844      uiStartCUAddrSliceIdx = 0;
845      uiStartCUAddrSlice    = 0; 
846
847      uiStartCUAddrEntropySliceIdx = 0;
848      uiStartCUAddrEntropySlice    = 0; 
849      uiNextCUAddr                 = 0;
850      pcSlice = pcPic->getSlice(uiStartCUAddrSliceIdx);
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          {
865        pcSlice->setNextSlice       ( false );
866        pcSlice->setNextEntropySlice( false );
867        if (uiNextCUAddr == m_uiStoredStartCUAddrForEncodingSlice[uiStartCUAddrSliceIdx])
868        {
869          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
876          pcPic->setCurrSliceIdx(uiStartCUAddrSliceIdx);
877          m_pcSliceEncoder->setSliceIdx(uiStartCUAddrSliceIdx);
878          assert(uiStartCUAddrSliceIdx == pcSlice->getSliceIdx());
879          // Reconstruction slice
880          pcSlice->setSliceCurStartCUAddr( uiNextCUAddr );  // to be used in encodeSlice() + context restriction
881          pcSlice->setSliceCurEndCUAddr  ( m_uiStoredStartCUAddrForEncodingSlice[uiStartCUAddrSliceIdx+1 ] );
882          // Entropy slice
883          pcSlice->setEntropySliceCurStartCUAddr( uiNextCUAddr );  // to be used in encodeSlice() + context restriction
884          pcSlice->setEntropySliceCurEndCUAddr  ( m_uiStoredStartCUAddrForEncodingEntropySlice[uiStartCUAddrEntropySliceIdx+1 ] );
885
886          pcSlice->setNextSlice       ( true );
887
888          uiStartCUAddrSliceIdx++;
889          uiStartCUAddrEntropySliceIdx++;
890        } 
891        else if (uiNextCUAddr == m_uiStoredStartCUAddrForEncodingEntropySlice[uiStartCUAddrEntropySliceIdx])
892        {
893          // Entropy slice
894          pcSlice->setEntropySliceCurStartCUAddr( uiNextCUAddr );  // to be used in encodeSlice() + context restriction
895          pcSlice->setEntropySliceCurEndCUAddr  ( m_uiStoredStartCUAddrForEncodingEntropySlice[uiStartCUAddrEntropySliceIdx+1 ] );
896
897          pcSlice->setNextEntropySlice( true );
898
899          uiStartCUAddrEntropySliceIdx++;
900        }
901
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.
1147          m_pcEntropyCoder->setEntropyCoder   ( m_pcCavlcCoder, pcSlice );
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
1300            // set entropy coder for RD
1301            m_pcEntropyCoder->setEntropyCoder ( m_pcCavlcCoder, pcSlice );
1302
1303            if ( pcSlice->getSPS()->getUseSAO() )
1304            {
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);
1322            }
1323
1324            // adaptive loop filter
1325#if !LCU_SYNTAX_ALF
1326            UInt64 uiDist, uiBits;
1327#endif
1328            if ( pcSlice->getSPS()->getUseALF())
1329            {
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);
1385            }
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++)
1395            {
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              }
1404              if (pcSlice->getSPS()->getUseSAO())
1405              {
1406                pcPic->getSlice(s)->setSaoEnabledFlag((cAPS.getSaoParam()->bSaoFlag[0]==1)?true:false);
1407              }
1408              pcPic->getSlice(s)->setAPS(&(vAPS[iCodedAPSIdx]));
1409              pcPic->getSlice(s)->setAPSId(iCodedAPSIdx);
1410            }
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          }
1432        }
1433      } // end iteration over slices
1434
1435
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     
1465      //-- For time output for each slice
1466      Double dEncTime = (double)(clock()-iBeforeTime) / CLOCKS_PER_SEC;
1467
1468      const char* digestStr = NULL;
1469      if (m_pcCfg->getPictureDigestEnabled())
1470      {
1471        /* calculate MD5sum for entire reconstructed picture */
1472        SEIpictureDigest sei_recon_picture_digest;
1473        sei_recon_picture_digest.method = SEIpictureDigest::MD5;
1474        calcMD5(*pcPic->getPicYuvRec(), sei_recon_picture_digest.digest);
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
1483        /* write the SEI messages */
1484        m_pcEntropyCoder->setEntropyCoder(m_pcCavlcCoder, pcSlice);
1485        m_pcEntropyCoder->setBitstream(&nalu.m_Bitstream);
1486        m_pcEntropyCoder->encodeSEI(sei_recon_picture_digest);
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
1502      pcPic->getPicYuvRec()->copyToPic(pcPicYuvRecOut);
1503     
1504      pcPic->setReconMark   ( true );
1505
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
1519}
1520
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 )
1665{
1666  TComSlice* pcSlice = pcPic->getSlice(pcPic->getCurrSliceIdx());
1667  Bool bCalcDist = false;
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
1673  m_pcLoopFilter->loopFilterPic( pcPic );
1674 
1675  m_pcEntropyCoder->setEntropyCoder ( m_pcEncTop->getRDGoOnSbacCoder(), pcSlice );
1676  m_pcEntropyCoder->resetEntropy    ();
1677  m_pcEntropyCoder->setBitstream    ( m_pcBitCounter );
1678  pcSlice = pcPic->getSlice(0);
1679  if(pcSlice->getSPS()->getUseSAO() || pcSlice->getSPS()->getUseALF())
1680  {
1681    pcPic->createNonDBFilterInfo();
1682  }
1683 
1684  // Adaptive Loop filter
1685  if( pcSlice->getSPS()->getUseALF() )
1686  {
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
1697    ALFParam cAlfParam;
1698    m_pcAdaptiveLoopFilter->allocALFParam(&cAlfParam);
1699#endif   
1700    m_pcAdaptiveLoopFilter->startALFEnc(pcPic, m_pcEntropyCoder);
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
1751    m_pcAdaptiveLoopFilter->endALFEnc();
1752
1753#if LCU_SYNTAX_ALF
1754    alfParamSet->releaseALFParam();
1755    delete alfParamSet;
1756    delete alfCUCtrlParam;
1757#else
1758    m_pcAdaptiveLoopFilter->freeALFParam(&cAlfParam);
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 
1768  m_pcEntropyCoder->resetEntropy    ();
1769  ruiBits += m_pcEntropyCoder->getNumberOfWrittenBits();
1770 
1771  if (!bCalcDist)
1772    ruiDist = xFindDistortionFrame(pcPic->getPicYuvOrg(), pcPic->getPicYuvRec());
1773}
1774
1775// ====================================================================================================================
1776// Protected member functions
1777// ====================================================================================================================
1778
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)
1832{
1833  Int     x, y;
1834  Pel*  pSrc0   = pcPic0 ->getLumaAddr();
1835  Pel*  pSrc1   = pcPic1 ->getLumaAddr();
1836#if IBDI_DISTORTION
1837  Int  iShift = g_uiBitIncrement;
1838  Int  iOffset = 1<<(g_uiBitIncrement-1);
1839#else
1840  UInt  uiShift = g_uiBitIncrement<<1;
1841#endif
1842  Int   iTemp;
1843 
1844  Int   iStride = pcPic0->getStride();
1845  Int   iWidth  = pcPic0->getWidth();
1846  Int   iHeight = pcPic0->getHeight();
1847 
1848  UInt64  uiTotalDiff = 0;
1849 
1850  for( y = 0; y < iHeight; y++ )
1851  {
1852    for( x = 0; x < iWidth; x++ )
1853    {
1854#if IBDI_DISTORTION
1855      iTemp = ((pSrc0[x]+iOffset)>>iShift) - ((pSrc1[x]+iOffset)>>iShift); uiTotalDiff += iTemp * iTemp;
1856#else
1857      iTemp = pSrc0[x] - pSrc1[x]; uiTotalDiff += (iTemp*iTemp) >> uiShift;
1858#endif
1859    }
1860    pSrc0 += iStride;
1861    pSrc1 += iStride;
1862  }
1863 
1864  iHeight >>= 1;
1865  iWidth  >>= 1;
1866  iStride >>= 1;
1867 
1868  pSrc0  = pcPic0->getCbAddr();
1869  pSrc1  = pcPic1->getCbAddr();
1870 
1871  for( y = 0; y < iHeight; y++ )
1872  {
1873    for( x = 0; x < iWidth; x++ )
1874    {
1875#if IBDI_DISTORTION
1876      iTemp = ((pSrc0[x]+iOffset)>>iShift) - ((pSrc1[x]+iOffset)>>iShift); uiTotalDiff += iTemp * iTemp;
1877#else
1878      iTemp = pSrc0[x] - pSrc1[x]; uiTotalDiff += (iTemp*iTemp) >> uiShift;
1879#endif
1880    }
1881    pSrc0 += iStride;
1882    pSrc1 += iStride;
1883  }
1884 
1885  pSrc0  = pcPic0->getCrAddr();
1886  pSrc1  = pcPic1->getCrAddr();
1887 
1888  for( y = 0; y < iHeight; y++ )
1889  {
1890    for( x = 0; x < iWidth; x++ )
1891    {
1892#if IBDI_DISTORTION
1893      iTemp = ((pSrc0[x]+iOffset)>>iShift) - ((pSrc1[x]+iOffset)>>iShift); uiTotalDiff += iTemp * iTemp;
1894#else
1895      iTemp = pSrc0[x] - pSrc1[x]; uiTotalDiff += (iTemp*iTemp) >> uiShift;
1896#endif
1897    }
1898    pSrc0 += iStride;
1899    pSrc1 += iStride;
1900  }
1901 
1902  return uiTotalDiff;
1903}
1904
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 )
1929{
1930  Int     x, y;
1931  UInt64 uiSSDY  = 0;
1932  UInt64 uiSSDU  = 0;
1933  UInt64 uiSSDV  = 0;
1934 
1935  Double  dYPSNR  = 0.0;
1936  Double  dUPSNR  = 0.0;
1937  Double  dVPSNR  = 0.0;
1938 
1939  //===== calculate PSNR =====
1940  Pel*  pOrg    = pcPic ->getPicYuvOrg()->getLumaAddr();
1941  Pel*  pRec    = pcPicD->getLumaAddr();
1942  Int   iStride = pcPicD->getStride();
1943 
1944  Int   iWidth;
1945  Int   iHeight;
1946 
1947  iWidth  = pcPicD->getWidth () - m_pcEncTop->getPad(0);
1948  iHeight = pcPicD->getHeight() - m_pcEncTop->getPad(1);
1949 
1950  Int   iSize   = iWidth*iHeight;
1951 
1952  for( y = 0; y < iHeight; y++ )
1953  {
1954    for( x = 0; x < iWidth; x++ )
1955    {
1956      Int iDiff = (Int)( pOrg[x] - pRec[x] );
1957      uiSSDY   += iDiff * iDiff;
1958    }
1959    pOrg += iStride;
1960    pRec += iStride;
1961  }
1962 
1963#if HHI_VSO
1964  if ( m_pcRdCost->getUseRenModel() )
1965  {
1966    unsigned int maxval = 255 * (1<<(g_uiBitDepth + g_uiBitIncrement -8));
1967    Double fRefValueY = (double) maxval * maxval * iSize;
1968    Double fRefValueC = fRefValueY / 4.0;
1969    TRenModel*  pcRenModel = m_pcEncTop->getEncTop()->getRenModel();
1970    Int64 iDistVSOY, iDistVSOU, iDistVSOV;
1971    pcRenModel->getTotalSSE( iDistVSOY, iDistVSOU, iDistVSOV );
1972    dYPSNR = ( iDistVSOY ? 10.0 * log10( fRefValueY / (Double) iDistVSOY ) : 99.99 );
1973    dUPSNR = ( iDistVSOU ? 10.0 * log10( fRefValueC / (Double) iDistVSOU ) : 99.99 );
1974    dVPSNR = ( iDistVSOV ? 10.0 * log10( fRefValueC / (Double) iDistVSOV ) : 99.99 );
1975  }
1976  else
1977#endif
1978  {
1979  iHeight >>= 1;
1980  iWidth  >>= 1;
1981  iStride >>= 1;
1982  pOrg  = pcPic ->getPicYuvOrg()->getCbAddr();
1983  pRec  = pcPicD->getCbAddr();
1984 
1985  for( y = 0; y < iHeight; y++ )
1986  {
1987    for( x = 0; x < iWidth; x++ )
1988    {
1989      Int iDiff = (Int)( pOrg[x] - pRec[x] );
1990      uiSSDU   += iDiff * iDiff;
1991    }
1992    pOrg += iStride;
1993    pRec += iStride;
1994  }
1995 
1996  pOrg  = pcPic ->getPicYuvOrg()->getCrAddr();
1997  pRec  = pcPicD->getCrAddr();
1998 
1999  for( y = 0; y < iHeight; y++ )
2000  {
2001    for( x = 0; x < iWidth; x++ )
2002    {
2003      Int iDiff = (Int)( pOrg[x] - pRec[x] );
2004      uiSSDV   += iDiff * iDiff;
2005    }
2006    pOrg += iStride;
2007    pRec += iStride;
2008  }
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;
2013  dYPSNR            = ( uiSSDY ? 10.0 * log10( fRefValueY / (Double)uiSSDY ) : 99.99 );
2014  dUPSNR            = ( uiSSDU ? 10.0 * log10( fRefValueC / (Double)uiSSDU ) : 99.99 );
2015  dVPSNR            = ( uiSSDV ? 10.0 * log10( fRefValueC / (Double)uiSSDV ) : 99.99 );
2016  }
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;
2033  m_vRVM_RP.push_back( uibits );
2034
2035  //===== add PSNR =====
2036  m_pcEncTop->getAnalyzeAll()->addResult (dYPSNR, dUPSNR, dVPSNR, (Double)uibits);
2037  TComSlice*  pcSlice = pcPic->getSlice(0);
2038  if (pcSlice->isIntra())
2039  {
2040    m_pcEncTop->getAnalyzeI()->addResult (dYPSNR, dUPSNR, dVPSNR, (Double)uibits);
2041  }
2042  if (pcSlice->isInterP())
2043  {
2044    m_pcEncTop->getAnalyzeP()->addResult (dYPSNR, dUPSNR, dVPSNR, (Double)uibits);
2045  }
2046  if (pcSlice->isInterB())
2047  {
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 
2078  for (Int iRefList = 0; iRefList < 2; iRefList++)
2079  {
2080    printf(" [L%d ", iRefList);
2081    for (Int iRefIndex = 0; iRefIndex < pcSlice->getNumRefIdx(RefPicList(iRefList)); iRefIndex++)
2082    {
2083      if( pcSlice->getViewId() != pcSlice->getRefViewId( RefPicList(iRefList), iRefIndex ) )
2084      {
2085        printf( "V%d ", pcSlice->getRefViewId( RefPicList(iRefList), iRefIndex ) );
2086      }
2087      else
2088      {
2089        printf ("%d ", pcSlice->getRefPOC(RefPicList(iRefList), iRefIndex)-pcSlice->getLastIDR());
2090      }
2091    }
2092    printf("]");
2093  }
2094  if(pcSlice->getNumRefIdx(REF_PIC_LIST_C)>0 && !pcSlice->getNoBackPredFlag())
2095  {
2096    printf(" [LC ");
2097    for (Int iRefIndex = 0; iRefIndex < pcSlice->getNumRefIdx(REF_PIC_LIST_C); iRefIndex++)
2098    {
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  }
2110}
2111
2112/** Function for deciding the nal_unit_type.
2113 * \param uiPOCCurr POC of the current picture
2114 * \returns the nal_unit type of the picture
2115 * This function checks the configuration and returns the appropriate nal_unit_type for the picture.
2116 */
2117NalUnitType TEncGOP::getNalUnitType(UInt uiPOCCurr)
2118{
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
2121  if (uiPOCCurr == 0)
2122  {
2123    if( bInterViewOnlySlice ) 
2124    { 
2125      return NAL_UNIT_CODED_SLICE_IDV; 
2126    }
2127    else
2128    { 
2129      return NAL_UNIT_CODED_SLICE_IDR;
2130    }
2131  }
2132  if (uiPOCCurr % m_pcCfg->getIntraPeriod() == 0)
2133  {
2134    if (m_pcCfg->getDecodingRefreshType() == 1)
2135    {
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
2145      return NAL_UNIT_CODED_SLICE_CDR;
2146#endif
2147      }
2148    }
2149    else if (m_pcCfg->getDecodingRefreshType() == 2)
2150    {
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  }
2161  return NAL_UNIT_CODED_SLICE;
2162}
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()
2189{
2190  Double dRVM = 0;
2191 
2192  if( m_pcCfg->getGOPSize() == 1 && m_pcCfg->getIntraPeriod() != 1 && m_pcCfg->getFrameToBeEncoded() > RVM_VCEGAM10_M * 2 )
2193  {
2194    // calculate RVM only for lowdelay configurations
2195    std::vector<Double> vRL , vB;
2196    size_t N = m_vRVM_RP.size();
2197    vRL.resize( N );
2198    vB.resize( N );
2199   
2200    Int i;
2201    Double dRavg = 0 , dBavg = 0;
2202    vB[RVM_VCEGAM10_M] = 0;
2203    for( i = RVM_VCEGAM10_M + 1 ; i < N - RVM_VCEGAM10_M + 1 ; i++ )
2204    {
2205      vRL[i] = 0;
2206      for( Int j = i - RVM_VCEGAM10_M ; j <= i + RVM_VCEGAM10_M - 1 ; j++ )
2207        vRL[i] += m_vRVM_RP[j];
2208      vRL[i] /= ( 2 * RVM_VCEGAM10_M );
2209      vB[i] = vB[i-1] + m_vRVM_RP[i] - vRL[i];
2210      dRavg += m_vRVM_RP[i];
2211      dBavg += vB[i];
2212    }
2213   
2214    dRavg /= ( N - 2 * RVM_VCEGAM10_M );
2215    dBavg /= ( N - 2 * RVM_VCEGAM10_M );
2216   
2217    double dSigamB = 0;
2218    for( i = RVM_VCEGAM10_M + 1 ; i < N - RVM_VCEGAM10_M + 1 ; i++ )
2219    {
2220      Double tmp = vB[i] - dBavg;
2221      dSigamB += tmp * tmp;
2222    }
2223    dSigamB = sqrt( dSigamB / ( N - 2 * RVM_VCEGAM10_M ) );
2224   
2225    double f = sqrt( 12.0 * ( RVM_VCEGAM10_M - 1 ) / ( RVM_VCEGAM10_M + 1 ) );
2226   
2227    dRVM = dSigamB / dRavg * f;
2228  }
2229 
2230  return( dRVM );
2231}
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 TracBrowser for help on using the repository browser.