source: 3DVCSoftware/branches/HTM-4.0-Nokia/source/Lib/TLibEncoder/TEncGOP.cpp @ 449

Last change on this file since 449 was 139, checked in by nokia, 12 years ago

3DV-HTM v4.0: FCO

  • Property svn:eol-style set to native
File size: 94.4 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  int is_Depth;
170
171  {
172      UInt uiColDir = 1;
173      //-- For time output for each slice
174      long iBeforeTime = clock();
175     
176      //select uiColDir
177      Int iCloseLeft=1, iCloseRight=-1;
178      for(Int i = 0; i<m_pcCfg->getGOPEntry(iGOPid).m_numRefPics; i++) 
179      {
180        Int iRef = m_pcCfg->getGOPEntry(iGOPid).m_referencePics[i];
181        if(iRef>0&&(iRef<iCloseRight||iCloseRight==-1))
182        {
183          iCloseRight=iRef;
184        }
185        else if(iRef<0&&(iRef>iCloseLeft||iCloseLeft==1))
186        {
187          iCloseLeft=iRef;
188        }
189      }
190      if(iCloseRight>-1)
191      {
192        iCloseRight=iCloseRight+m_pcCfg->getGOPEntry(iGOPid).m_POC-1;
193      }
194      if(iCloseLeft<1) 
195      {
196        iCloseLeft=iCloseLeft+m_pcCfg->getGOPEntry(iGOPid).m_POC-1;
197        while(iCloseLeft<0)
198        {
199          iCloseLeft+=m_iGopSize;
200        }
201      }
202      Int iLeftQP=0, iRightQP=0;
203      for(Int i=0; i<m_iGopSize; i++)
204      {
205        if(m_pcCfg->getGOPEntry(i).m_POC==(iCloseLeft%m_iGopSize)+1)
206        {
207          iLeftQP= m_pcCfg->getGOPEntry(i).m_QPOffset;
208        }
209        if (m_pcCfg->getGOPEntry(i).m_POC==(iCloseRight%m_iGopSize)+1)
210        {
211          iRightQP=m_pcCfg->getGOPEntry(i).m_QPOffset;
212        }
213      }
214      if(iCloseRight>-1&&iRightQP<iLeftQP)
215      {
216        uiColDir=0;
217      }
218
219      /////////////////////////////////////////////////////////////////////////////////////////////////// Initial to start encoding
220      UInt uiPOCCurr = iPOCLast -iNumPicRcvd+ m_pcCfg->getGOPEntry(iGOPid).m_POC;
221      Int iTimeOffset = m_pcCfg->getGOPEntry(iGOPid).m_POC;
222      if(iPOCLast == 0)
223      {
224        uiPOCCurr=0;
225        iTimeOffset = 1;
226      }
227      if(uiPOCCurr>=m_pcCfg->getFrameToBeEncoded())
228      {
229        return;
230      }       
231      if( getNalUnitTypeBaseViewMvc( uiPOCCurr ) == NAL_UNIT_CODED_SLICE_IDR )
232      {
233        m_iLastIDR = uiPOCCurr;
234      }       
235
236      /* start a new access unit: create an entry in the list of output
237       * access units */
238      accessUnitsInGOP.push_back(AccessUnit());
239      AccessUnit& accessUnit = accessUnitsInGOP.back();
240      xGetBuffer( rcListPic, rcListPicYuvRecOut, iNumPicRcvd, iTimeOffset, pcPic, pcPicYuvRecOut, uiPOCCurr );
241     
242      //  Slice data initialization
243      pcPic->clearSliceBuffer();
244      assert(pcPic->getNumAllocatedSlice() == 1);
245      m_pcSliceEncoder->setSliceIdx(0);
246      pcPic->setCurrSliceIdx(0);
247
248      std::vector<TComAPS>& vAPS = m_pcEncTop->getAPS();
249#if VIDYO_VPS_INTEGRATION
250    m_pcSliceEncoder->initEncSlice ( pcPic, iPOCLast, uiPOCCurr, iNumPicRcvd, iGOPid, pcSlice, m_pcEncTop->getEncTop()->getVPS(), m_pcEncTop->getSPS(), m_pcEncTop->getPPS() );
251#else
252      m_pcSliceEncoder->initEncSlice ( pcPic, iPOCLast, uiPOCCurr, iNumPicRcvd, iGOPid, pcSlice, m_pcEncTop->getSPS(), m_pcEncTop->getPPS() );
253#endif
254      pcSlice->setLastIDR(m_iLastIDR);
255      pcSlice->setSliceIdx(0);
256      pcSlice->setViewId( m_pcEncTop->getViewId() );
257      pcSlice->setIsDepth( m_pcEncTop->getIsDepth() ); 
258      is_Depth = m_pcEncTop->getIsDepth();
259      m_pcEncTop->getSPS()->setDisInter4x4(m_pcEncTop->getDisInter4x4());
260      pcSlice->setScalingList ( m_pcEncTop->getScalingList()  );
261      if(m_pcEncTop->getUseScalingListId() == SCALING_LIST_OFF)
262      {
263        m_pcEncTop->getTrQuant()->setFlatScalingList();
264        m_pcEncTop->getTrQuant()->setUseScalingList(false);
265      }
266      else if(m_pcEncTop->getUseScalingListId() == SCALING_LIST_DEFAULT)
267      {
268        pcSlice->setDefaultScalingList ();
269        pcSlice->getScalingList()->setScalingListPresentFlag(true);
270        m_pcEncTop->getTrQuant()->setScalingList(pcSlice->getScalingList());
271        m_pcEncTop->getTrQuant()->setUseScalingList(true);
272      }
273      else if(m_pcEncTop->getUseScalingListId() == SCALING_LIST_FILE_READ)
274      {
275        if(pcSlice->getScalingList()->xParseScalingList(m_pcCfg->getScalingListFile()))
276        {
277          pcSlice->setDefaultScalingList ();
278        }
279#if SCALING_LIST
280        pcSlice->getScalingList()->checkDcOfMatrix();
281#endif
282        pcSlice->getScalingList()->setScalingListPresentFlag(pcSlice->checkDefaultScalingList());
283        m_pcEncTop->getTrQuant()->setScalingList(pcSlice->getScalingList());
284        m_pcEncTop->getTrQuant()->setUseScalingList(true);
285      }
286      else
287      {
288        printf("error : ScalingList == %d no support\n",m_pcEncTop->getUseScalingListId());
289        assert(0);
290      }
291
292#if HHI_INTERVIEW_SKIP
293      if ( m_pcEncTop->getInterViewSkip() )
294      {
295        m_pcEncTop->getEncTop()->getUsedPelsMap( pcPic->getViewId(), pcPic->getPOC(), pcPic->getUsedPelsMap() );
296      }
297#endif
298      //  Slice info. refinement
299      if( pcSlice->getSliceType() == B_SLICE )
300      {
301        if( m_pcCfg->getGOPEntry( (getNalUnitType(uiPOCCurr) == NAL_UNIT_CODED_SLICE_IDV) ? MAX_GOP : iGOPid ).m_sliceType == 'P' ) { pcSlice->setSliceType( P_SLICE ); }
302      }
303
304      // Set the nal unit type
305      pcSlice->setNalUnitType( getNalUnitType(uiPOCCurr) );
306      pcSlice->setNalUnitTypeBaseViewMvc( getNalUnitTypeBaseViewMvc(uiPOCCurr) );
307
308      // Do decoding refresh marking if any
309      pcSlice->decodingRefreshMarking(m_pocCRA, m_bRefreshPending, rcListPic);
310
311      if ( !pcSlice->getPPS()->getEnableTMVPFlag() && pcPic->getTLayer() == 0 )
312      {
313        pcSlice->decodingMarkingForNoTMVP( rcListPic, pcSlice->getPOC() );
314      }
315
316      m_pcEncTop->selectReferencePictureSet(pcSlice, uiPOCCurr, iGOPid,rcListPic);
317      pcSlice->getRPS()->setNumberOfLongtermPictures(0);
318
319      if(pcSlice->checkThatAllRefPicsAreAvailable(rcListPic, pcSlice->getRPS(), false) != 0)
320      {
321         pcSlice->createExplicitReferencePictureSetFromReference(rcListPic, pcSlice->getRPS());
322      }
323      pcSlice->applyReferencePictureSet(rcListPic, pcSlice->getRPS());
324
325#if H0566_TLA && H0566_TLA_SET_FOR_SWITCHING_POINTS
326      if(pcSlice->getTLayer() > 0)
327      {
328        if(pcSlice->isTemporalLayerSwitchingPoint(rcListPic, pcSlice->getRPS()))
329        {
330          pcSlice->setNalUnitType(NAL_UNIT_CODED_SLICE_TLA);
331        }
332      }
333#endif
334
335      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()) ) );
336      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()) ) );
337
338      TComRefPicListModification* refPicListModification = pcSlice->getRefPicListModification();
339      refPicListModification->setRefPicListModificationFlagL0( false );
340#if !H0137_0138_LIST_MODIFICATION
341      refPicListModification->setNumberOfRefPicListModificationsL0(0);
342#endif
343      refPicListModification->setRefPicListModificationFlagL1( false );
344#if !H0137_0138_LIST_MODIFICATION
345      refPicListModification->setNumberOfRefPicListModificationsL1(0);
346#endif
347      xSetRefPicListModificationsMvc( pcSlice, uiPOCCurr, iGOPid );
348
349#if ADAPTIVE_QP_SELECTION
350      pcSlice->setTrQuant( m_pcEncTop->getTrQuant() );
351#endif     
352      //  Set reference list
353      TAppEncTop* tAppEncTop = m_pcEncTop->getEncTop();
354      assert( tAppEncTop != NULL );
355
356      TComPic * const pcTexturePic = m_pcEncTop->getIsDepth() ? tAppEncTop->getPicFromView( m_pcEncTop->getViewId(), pcSlice->getPOC(), false ) : NULL;
357      assert( !m_pcEncTop->getIsDepth() || pcTexturePic != NULL );
358      pcSlice->setTexturePic( pcTexturePic );
359
360      std::vector<TComPic*> apcInterViewRefPics = tAppEncTop->getInterViewRefPics( m_pcEncTop->getViewId(), pcSlice->getPOC(), m_pcEncTop->getIsDepth(), pcSlice->getSPS() );
361      pcSlice->setRefPicListMvc( rcListPic, apcInterViewRefPics );
362
363      //  Slice info. refinement
364      if( pcSlice->getSliceType() == B_SLICE )
365      {
366        if( m_pcCfg->getGOPEntry( (getNalUnitType(uiPOCCurr) == NAL_UNIT_CODED_SLICE_IDV) ? MAX_GOP : iGOPid ).m_sliceType == 'P' ) { pcSlice->setSliceType( P_SLICE ); }
367      }
368     
369      if (pcSlice->getSliceType() != B_SLICE || !pcSlice->getSPS()->getUseLComb())
370      {
371        pcSlice->setNumRefIdx(REF_PIC_LIST_C, 0);
372        pcSlice->setRefPicListCombinationFlag(false);
373        pcSlice->setRefPicListModificationFlagLC(false);
374      }
375      else
376      {
377        pcSlice->setRefPicListCombinationFlag(pcSlice->getSPS()->getUseLComb());
378        pcSlice->setRefPicListModificationFlagLC(pcSlice->getSPS()->getLCMod());
379        pcSlice->setNumRefIdx(REF_PIC_LIST_C, pcSlice->getNumRefIdx(REF_PIC_LIST_0));
380      }
381     
382      if (pcSlice->getSliceType() == B_SLICE)
383      {
384        pcSlice->setColDir(uiColDir);
385        Bool bLowDelay = true;
386        Int  iCurrPOC  = pcSlice->getPOC();
387        Int iRefIdx = 0;
388
389        for (iRefIdx = 0; iRefIdx < pcSlice->getNumRefIdx(REF_PIC_LIST_0) && bLowDelay; iRefIdx++)
390        {
391          if ( pcSlice->getRefPic(REF_PIC_LIST_0, iRefIdx)->getPOC() > iCurrPOC )
392          {
393            bLowDelay = false;
394          }
395        }
396        for (iRefIdx = 0; iRefIdx < pcSlice->getNumRefIdx(REF_PIC_LIST_1) && bLowDelay; iRefIdx++)
397        {
398          if ( pcSlice->getRefPic(REF_PIC_LIST_1, iRefIdx)->getPOC() > iCurrPOC )
399          {
400            bLowDelay = false;
401          }
402        }
403
404        pcSlice->setCheckLDC(bLowDelay); 
405      }
406     
407      uiColDir = 1-uiColDir;
408     
409      //-------------------------------------------------------------
410      pcSlice->setRefPOCnViewListsMvc();
411     
412      pcSlice->setNoBackPredFlag( false );
413      if ( pcSlice->getSliceType() == B_SLICE && !pcSlice->getRefPicListCombinationFlag())
414      {
415        if ( pcSlice->getNumRefIdx(RefPicList( 0 ) ) == pcSlice->getNumRefIdx(RefPicList( 1 ) ) )
416        {
417          pcSlice->setNoBackPredFlag( true );
418          int i;
419          for ( i=0; i < pcSlice->getNumRefIdx(RefPicList( 1 ) ); i++ )
420          {
421            if ( pcSlice->getRefPOC(RefPicList(1), i) != pcSlice->getRefPOC(RefPicList(0), i) ) 
422            {
423              pcSlice->setNoBackPredFlag( false );
424              break;
425            }
426          }
427        }
428      }
429
430      if(pcSlice->getNoBackPredFlag())
431      {
432        pcSlice->setNumRefIdx(REF_PIC_LIST_C, 0);
433      }
434      pcSlice->generateCombinedList();
435     
436#if HHI_VSO
437  Bool bUseVSO = m_pcEncTop->getUseVSO();
438  m_pcRdCost->setUseVSO( bUseVSO );
439#if SAIT_VSO_EST_A0033
440  m_pcRdCost->setUseEstimatedVSD( m_pcEncTop->getUseEstimatedVSD() );
441#endif
442
443  if ( bUseVSO )
444  {
445    Int iVSOMode = m_pcEncTop->getVSOMode();
446    m_pcRdCost->setVSOMode( iVSOMode  );
447#if HHI_VSO_DIST_INT
448    m_pcRdCost->setAllowNegDist( m_pcEncTop->getAllowNegDist() );
449#endif
450
451#if HHI_VSO_SPEEDUP_A0033
452#else
453    if ( iVSOMode == 4 )
454    {
455      m_pcEncTop->getEncTop()->setupRenModel( pcSlice->getPOC(), pcSlice->getViewId(), m_pcEncTop->isDepthCoder() ? 1 : 0 );
456    }
457    else
458    {
459      AOT(true); 
460    }
461#endif
462
463#if SAIT_VSO_EST_A0033
464   
465{
466Bool flag_rec;
467
468    flag_rec =  ((m_pcEncTop->getEncTop()->getPicYuvFromView( pcSlice->getViewId(), pcSlice->getPOC(), false, true) == NULL) ? false: true);
469    m_pcRdCost->setVideoRecPicYuv( m_pcEncTop->getEncTop()->getPicYuvFromView( pcSlice->getViewId(), pcSlice->getPOC(), false, flag_rec ) );
470    m_pcRdCost->setDepthPicYuv   ( m_pcEncTop->getEncTop()->getPicYuvFromView( pcSlice->getViewId(), pcSlice->getPOC(), true, false ) );
471}
472#endif
473  }
474#endif
475      /////////////////////////////////////////////////////////////////////////////////////////////////// Compress a slice
476      //  Slice compression
477      if (m_pcCfg->getUseASR())
478      {
479        m_pcSliceEncoder->setSearchRange(pcSlice);
480      }
481
482#if H0111_MVD_L1_ZERO
483      Bool bGPBcheck=false;
484      if ( pcSlice->getSliceType() == B_SLICE)
485      {
486        if ( pcSlice->getNumRefIdx(RefPicList( 0 ) ) == pcSlice->getNumRefIdx(RefPicList( 1 ) ) )
487        {
488          bGPBcheck=true;
489          int i;
490          for ( i=0; i < pcSlice->getNumRefIdx(RefPicList( 1 ) ); i++ )
491          {
492            if ( pcSlice->getRefPOC(RefPicList(1), i) != pcSlice->getRefPOC(RefPicList(0), i) ) 
493            {
494              bGPBcheck=false;
495              break;
496            }
497          }
498        }
499      }
500      if(bGPBcheck)
501      {
502        pcSlice->setMvdL1ZeroFlag(true);
503      }
504      else
505      {
506        pcSlice->setMvdL1ZeroFlag(false);
507      }
508      pcPic->getSlice(pcSlice->getSliceIdx())->setMvdL1ZeroFlag(pcSlice->getMvdL1ZeroFlag());
509#endif
510
511      UInt uiNumSlices = 1;
512
513      UInt uiInternalAddress = pcPic->getNumPartInCU()-4;
514      UInt uiExternalAddress = pcPic->getPicSym()->getNumberOfCUsInFrame()-1;
515      UInt uiPosX = ( uiExternalAddress % pcPic->getFrameWidthInCU() ) * g_uiMaxCUWidth+ g_auiRasterToPelX[ g_auiZscanToRaster[uiInternalAddress] ];
516      UInt uiPosY = ( uiExternalAddress / pcPic->getFrameWidthInCU() ) * g_uiMaxCUHeight+ g_auiRasterToPelY[ g_auiZscanToRaster[uiInternalAddress] ];
517      UInt uiWidth = pcSlice->getSPS()->getPicWidthInLumaSamples();
518      UInt uiHeight = pcSlice->getSPS()->getPicHeightInLumaSamples();
519      while(uiPosX>=uiWidth||uiPosY>=uiHeight) 
520      {
521        uiInternalAddress--;
522        uiPosX = ( uiExternalAddress % pcPic->getFrameWidthInCU() ) * g_uiMaxCUWidth+ g_auiRasterToPelX[ g_auiZscanToRaster[uiInternalAddress] ];
523        uiPosY = ( uiExternalAddress / pcPic->getFrameWidthInCU() ) * g_uiMaxCUHeight+ g_auiRasterToPelY[ g_auiZscanToRaster[uiInternalAddress] ];
524      }
525      uiInternalAddress++;
526      if(uiInternalAddress==pcPic->getNumPartInCU()) 
527      {
528        uiInternalAddress = 0;
529        uiExternalAddress++;
530      }
531      UInt uiRealEndAddress = uiExternalAddress*pcPic->getNumPartInCU()+uiInternalAddress;
532
533    UInt uiCummulativeTileWidth;
534    UInt uiCummulativeTileHeight;
535    Int  p, j;
536    UInt uiEncCUAddr;
537   
538#if !REMOVE_TILE_DEPENDENCE
539    if(pcSlice->getPPS()->getTileBehaviorControlPresentFlag() == 1)
540    {
541      pcPic->getPicSym()->setTileBoundaryIndependenceIdr( pcSlice->getPPS()->getTileBoundaryIndependenceIdr() );
542    }
543    else
544    {
545      pcPic->getPicSym()->setTileBoundaryIndependenceIdr( pcSlice->getPPS()->getSPS()->getTileBoundaryIndependenceIdr() );
546
547    }
548#endif
549
550    if( pcSlice->getPPS()->getColumnRowInfoPresent() == 1 )    //derive the tile parameters from PPS
551    {
552      //set NumColumnsMinus1 and NumRowsMinus1
553      pcPic->getPicSym()->setNumColumnsMinus1( pcSlice->getPPS()->getNumColumnsMinus1() );
554      pcPic->getPicSym()->setNumRowsMinus1( pcSlice->getPPS()->getNumRowsMinus1() );
555
556      //create the TComTileArray
557      pcPic->getPicSym()->xCreateTComTileArray();
558
559      if( pcSlice->getPPS()->getUniformSpacingIdr() == 1 )
560      {
561        //set the width for each tile
562        for(j=0; j < pcPic->getPicSym()->getNumRowsMinus1()+1; j++)
563        {
564          for(p=0; p < pcPic->getPicSym()->getNumColumnsMinus1()+1; p++)
565          {
566            pcPic->getPicSym()->getTComTile( j * (pcPic->getPicSym()->getNumColumnsMinus1()+1) + p )->
567              setTileWidth( (p+1)*pcPic->getPicSym()->getFrameWidthInCU()/(pcPic->getPicSym()->getNumColumnsMinus1()+1) 
568              - (p*pcPic->getPicSym()->getFrameWidthInCU())/(pcPic->getPicSym()->getNumColumnsMinus1()+1) );
569          }
570        }
571
572        //set the height for each tile
573        for(j=0; j < pcPic->getPicSym()->getNumColumnsMinus1()+1; j++)
574        {
575          for(p=0; p < pcPic->getPicSym()->getNumRowsMinus1()+1; p++)
576          {
577            pcPic->getPicSym()->getTComTile( p * (pcPic->getPicSym()->getNumColumnsMinus1()+1) + j )->
578              setTileHeight( (p+1)*pcPic->getPicSym()->getFrameHeightInCU()/(pcPic->getPicSym()->getNumRowsMinus1()+1) 
579              - (p*pcPic->getPicSym()->getFrameHeightInCU())/(pcPic->getPicSym()->getNumRowsMinus1()+1) );   
580          }
581        }
582      }
583      else
584      {
585        //set the width for each tile
586        for(j=0; j < pcPic->getPicSym()->getNumRowsMinus1()+1; j++)
587        {
588          uiCummulativeTileWidth = 0;
589          for(p=0; p < pcPic->getPicSym()->getNumColumnsMinus1(); p++)
590          {
591            pcPic->getPicSym()->getTComTile( j * (pcPic->getPicSym()->getNumColumnsMinus1()+1) + p )->setTileWidth( pcSlice->getPPS()->getColumnWidth(p) );
592            uiCummulativeTileWidth += pcSlice->getPPS()->getColumnWidth(p);
593          }
594          pcPic->getPicSym()->getTComTile(j * (pcPic->getPicSym()->getNumColumnsMinus1()+1) + p)->setTileWidth( pcPic->getPicSym()->getFrameWidthInCU()-uiCummulativeTileWidth );
595        }
596
597        //set the height for each tile
598        for(j=0; j < pcPic->getPicSym()->getNumColumnsMinus1()+1; j++)
599        {
600          uiCummulativeTileHeight = 0;
601          for(p=0; p < pcPic->getPicSym()->getNumRowsMinus1(); p++)
602          {
603            pcPic->getPicSym()->getTComTile( p * (pcPic->getPicSym()->getNumColumnsMinus1()+1) + j )->setTileHeight( pcSlice->getPPS()->getRowHeight(p) );
604            uiCummulativeTileHeight += pcSlice->getPPS()->getRowHeight(p);
605          }
606          pcPic->getPicSym()->getTComTile(p * (pcPic->getPicSym()->getNumColumnsMinus1()+1) + j)->setTileHeight( pcPic->getPicSym()->getFrameHeightInCU()-uiCummulativeTileHeight );
607        }
608      }
609    }
610    else //derive the tile parameters from SPS
611    {
612      //set NumColumnsMins1 and NumRowsMinus1
613      pcPic->getPicSym()->setNumColumnsMinus1( pcSlice->getSPS()->getNumColumnsMinus1() );
614      pcPic->getPicSym()->setNumRowsMinus1( pcSlice->getSPS()->getNumRowsMinus1() );
615
616      //create the TComTileArray
617      pcPic->getPicSym()->xCreateTComTileArray();
618
619      if( pcSlice->getSPS()->getUniformSpacingIdr() == 1 )
620      {
621        //set the width for each tile
622        for(j=0; j < pcPic->getPicSym()->getNumRowsMinus1()+1; j++)
623        {
624          for(p=0; p < pcPic->getPicSym()->getNumColumnsMinus1()+1; p++)
625          {
626            pcPic->getPicSym()->getTComTile( j * (pcPic->getPicSym()->getNumColumnsMinus1()+1) + p )->
627              setTileWidth( (p+1)*pcPic->getPicSym()->getFrameWidthInCU()/(pcPic->getPicSym()->getNumColumnsMinus1()+1) 
628              - (p*pcPic->getPicSym()->getFrameWidthInCU())/(pcPic->getPicSym()->getNumColumnsMinus1()+1) );
629          }
630        }
631
632        //set the height for each tile
633        for(j=0; j < pcPic->getPicSym()->getNumColumnsMinus1()+1; j++)
634        {
635          for(p=0; p < pcPic->getPicSym()->getNumRowsMinus1()+1; p++)
636          {
637            pcPic->getPicSym()->getTComTile( p * (pcPic->getPicSym()->getNumColumnsMinus1()+1) + j )->
638              setTileHeight( (p+1)*pcPic->getPicSym()->getFrameHeightInCU()/(pcPic->getPicSym()->getNumRowsMinus1()+1) 
639              - (p*pcPic->getPicSym()->getFrameHeightInCU())/(pcPic->getPicSym()->getNumRowsMinus1()+1) );   
640          }
641        }
642      }
643
644      else
645      {
646        //set the width for each tile
647        for(j=0; j < pcPic->getPicSym()->getNumRowsMinus1()+1; j++)
648        {
649          uiCummulativeTileWidth = 0;
650          for(p=0; p < pcPic->getPicSym()->getNumColumnsMinus1(); p++)
651          {
652            pcPic->getPicSym()->getTComTile( j * (pcPic->getPicSym()->getNumColumnsMinus1()+1) + p )->setTileWidth( pcSlice->getSPS()->getColumnWidth(p) );
653            uiCummulativeTileWidth += pcSlice->getSPS()->getColumnWidth(p);
654          }
655          pcPic->getPicSym()->getTComTile(j * (pcPic->getPicSym()->getNumColumnsMinus1()+1) + p)->setTileWidth( pcPic->getPicSym()->getFrameWidthInCU()-uiCummulativeTileWidth );
656        }
657
658        //set the height for each tile
659        for(j=0; j < pcPic->getPicSym()->getNumColumnsMinus1()+1; j++)
660        {
661          uiCummulativeTileHeight = 0;
662          for(p=0; p < pcPic->getPicSym()->getNumRowsMinus1(); p++)
663          {
664            pcPic->getPicSym()->getTComTile( p * (pcPic->getPicSym()->getNumColumnsMinus1()+1) + j )->setTileHeight( pcSlice->getSPS()->getRowHeight(p) );
665            uiCummulativeTileHeight += pcSlice->getSPS()->getRowHeight(p);
666          }
667          pcPic->getPicSym()->getTComTile(p * (pcPic->getPicSym()->getNumColumnsMinus1()+1) + j)->setTileHeight( pcPic->getPicSym()->getFrameHeightInCU()-uiCummulativeTileHeight );
668        }
669      }
670    }
671
672    //initialize each tile of the current picture
673    pcPic->getPicSym()->xInitTiles();
674
675    // Allocate some coders, now we know how many tiles there are.
676    Int iNumSubstreams = pcSlice->getPPS()->getNumSubstreams();
677   
678    //generate the Coding Order Map and Inverse Coding Order Map
679    for(p=0, uiEncCUAddr=0; p<pcPic->getPicSym()->getNumberOfCUsInFrame(); p++, uiEncCUAddr = pcPic->getPicSym()->xCalculateNxtCUAddr(uiEncCUAddr))
680    {
681      pcPic->getPicSym()->setCUOrderMap(p, uiEncCUAddr);
682      pcPic->getPicSym()->setInverseCUOrderMap(uiEncCUAddr, p);
683    }
684    pcPic->getPicSym()->setCUOrderMap(pcPic->getPicSym()->getNumberOfCUsInFrame(), pcPic->getPicSym()->getNumberOfCUsInFrame());   
685    pcPic->getPicSym()->setInverseCUOrderMap(pcPic->getPicSym()->getNumberOfCUsInFrame(), pcPic->getPicSym()->getNumberOfCUsInFrame());
686    if (pcSlice->getPPS()->getEntropyCodingMode())
687    {
688      // Allocate some coders, now we know how many tiles there are.
689      m_pcEncTop->createWPPCoders(iNumSubstreams);
690      pcSbacCoders = m_pcEncTop->getSbacCoders();
691      pcSubstreamsOut = new TComOutputBitstream[iNumSubstreams];
692    }
693
694      UInt uiStartCUAddrSliceIdx = 0; // used to index "m_uiStoredStartCUAddrForEncodingSlice" containing locations of slice boundaries
695      UInt uiStartCUAddrSlice    = 0; // used to keep track of current slice's starting CU addr.
696      pcSlice->setSliceCurStartCUAddr( uiStartCUAddrSlice ); // Setting "start CU addr" for current slice
697      memset(m_uiStoredStartCUAddrForEncodingSlice, 0, sizeof(UInt) * (pcPic->getPicSym()->getNumberOfCUsInFrame()*pcPic->getNumPartInCU()+1));
698
699      UInt uiStartCUAddrEntropySliceIdx = 0; // used to index "m_uiStoredStartCUAddrForEntropyEncodingSlice" containing locations of slice boundaries
700      UInt uiStartCUAddrEntropySlice    = 0; // used to keep track of current Entropy slice's starting CU addr.
701      pcSlice->setEntropySliceCurStartCUAddr( uiStartCUAddrEntropySlice ); // Setting "start CU addr" for current Entropy slice
702     
703      memset(m_uiStoredStartCUAddrForEncodingEntropySlice, 0, sizeof(UInt) * (pcPic->getPicSym()->getNumberOfCUsInFrame()*pcPic->getNumPartInCU()+1));
704      UInt uiNextCUAddr = 0;
705      m_uiStoredStartCUAddrForEncodingSlice[uiStartCUAddrSliceIdx++]                = uiNextCUAddr;
706      m_uiStoredStartCUAddrForEncodingEntropySlice[uiStartCUAddrEntropySliceIdx++]  = uiNextCUAddr;
707
708#if DEPTH_MAP_GENERATION
709      // init view component and predict virtual depth map
710      m_pcDepthMapGenerator->initViewComponent( pcPic );
711#if !QC_MULTI_DIS_CAN
712      m_pcDepthMapGenerator->predictDepthMap  ( pcPic );
713#endif
714#endif
715#if HHI_INTER_VIEW_MOTION_PRED
716      m_pcDepthMapGenerator->covertOrgDepthMap( pcPic );
717#endif
718#if HHI_INTER_VIEW_RESIDUAL_PRED
719      m_pcResidualGenerator->initViewComponent( pcPic );
720#endif
721
722      while(uiNextCUAddr<uiRealEndAddress) // determine slice boundaries
723      {
724        pcSlice->setNextSlice       ( false );
725        pcSlice->setNextEntropySlice( false );
726        assert(pcPic->getNumAllocatedSlice() == uiStartCUAddrSliceIdx);
727        m_pcSliceEncoder->precompressSlice( pcPic );
728        m_pcSliceEncoder->compressSlice   ( pcPic );
729
730        Bool bNoBinBitConstraintViolated = (!pcSlice->isNextSlice() && !pcSlice->isNextEntropySlice());
731        if (pcSlice->isNextSlice() || (bNoBinBitConstraintViolated && m_pcCfg->getSliceMode()==AD_HOC_SLICES_FIXED_NUMBER_OF_LCU_IN_SLICE))
732        {
733          uiStartCUAddrSlice                                              = pcSlice->getSliceCurEndCUAddr();
734          // Reconstruction slice
735          m_uiStoredStartCUAddrForEncodingSlice[uiStartCUAddrSliceIdx++]  = uiStartCUAddrSlice;
736          // Entropy slice
737          if (uiStartCUAddrEntropySliceIdx>0 && m_uiStoredStartCUAddrForEncodingEntropySlice[uiStartCUAddrEntropySliceIdx-1] != uiStartCUAddrSlice)
738          {
739            m_uiStoredStartCUAddrForEncodingEntropySlice[uiStartCUAddrEntropySliceIdx++]  = uiStartCUAddrSlice;
740          }
741         
742          if (uiStartCUAddrSlice < uiRealEndAddress)
743          {
744            pcPic->allocateNewSlice();         
745            pcPic->setCurrSliceIdx                  ( uiStartCUAddrSliceIdx-1 );
746            m_pcSliceEncoder->setSliceIdx           ( uiStartCUAddrSliceIdx-1 );
747            pcSlice = pcPic->getSlice               ( uiStartCUAddrSliceIdx-1 );
748            pcSlice->copySliceInfo                  ( pcPic->getSlice(0)      );
749            pcSlice->setSliceIdx                    ( uiStartCUAddrSliceIdx-1 );
750            pcSlice->setSliceCurStartCUAddr         ( uiStartCUAddrSlice      );
751            pcSlice->setEntropySliceCurStartCUAddr  ( uiStartCUAddrSlice      );
752            pcSlice->setSliceBits(0);
753            uiNumSlices ++;
754          }
755        }
756        else if (pcSlice->isNextEntropySlice() || (bNoBinBitConstraintViolated && m_pcCfg->getEntropySliceMode()==SHARP_FIXED_NUMBER_OF_LCU_IN_ENTROPY_SLICE))
757        {
758          uiStartCUAddrEntropySlice                                                     = pcSlice->getEntropySliceCurEndCUAddr();
759          m_uiStoredStartCUAddrForEncodingEntropySlice[uiStartCUAddrEntropySliceIdx++]  = uiStartCUAddrEntropySlice;
760          pcSlice->setEntropySliceCurStartCUAddr( uiStartCUAddrEntropySlice );
761        }
762        else
763        {
764          uiStartCUAddrSlice                                                            = pcSlice->getSliceCurEndCUAddr();
765          uiStartCUAddrEntropySlice                                                     = pcSlice->getEntropySliceCurEndCUAddr();
766        }       
767
768        uiNextCUAddr = (uiStartCUAddrSlice > uiStartCUAddrEntropySlice) ? uiStartCUAddrSlice : uiStartCUAddrEntropySlice;
769      }
770      m_uiStoredStartCUAddrForEncodingSlice[uiStartCUAddrSliceIdx++]                = pcSlice->getSliceCurEndCUAddr();
771      m_uiStoredStartCUAddrForEncodingEntropySlice[uiStartCUAddrEntropySliceIdx++]  = pcSlice->getSliceCurEndCUAddr();
772     
773      pcSlice = pcPic->getSlice(0);
774
775#if HHI_INTER_VIEW_RESIDUAL_PRED
776      // set residual picture
777      m_pcResidualGenerator->setRecResidualPic( pcPic );
778#endif
779#if DEPTH_MAP_GENERATION
780#if !QC_MULTI_DIS_CAN
781      // update virtual depth map
782      m_pcDepthMapGenerator->updateDepthMap( pcPic );
783#endif
784#endif
785
786      //-- Loop filter
787      Bool bLFCrossTileBoundary = (pcSlice->getPPS()->getTileBehaviorControlPresentFlag() == 1)?
788                                  (pcSlice->getPPS()->getLFCrossTileBoundaryFlag()):(pcSlice->getPPS()->getSPS()->getLFCrossTileBoundaryFlag());
789#if DBL_CONTROL
790      m_pcLoopFilter->setCfg(pcSlice->getPPS()->getDeblockingFilterControlPresent(), pcSlice->getLoopFilterDisable(), pcSlice->getLoopFilterBetaOffset(), pcSlice->getLoopFilterTcOffset(), bLFCrossTileBoundary);
791#else
792      m_pcLoopFilter->setCfg(pcSlice->getLoopFilterDisable(), pcSlice->getLoopFilterBetaOffset(), pcSlice->getLoopFilterTcOffset(), bLFCrossTileBoundary);
793#endif
794      m_pcLoopFilter->loopFilterPic( pcPic );
795
796      pcSlice = pcPic->getSlice(0);
797      if(pcSlice->getSPS()->getUseSAO() || pcSlice->getSPS()->getUseALF())
798      {
799        Int sliceGranularity = pcSlice->getPPS()->getSliceGranularity();
800        pcPic->createNonDBFilterInfo(m_uiStoredStartCUAddrForEncodingSlice, uiNumSlices, sliceGranularity, pcSlice->getSPS()->getLFCrossSliceBoundaryFlag(),pcPic->getPicSym()->getNumTiles() ,bLFCrossTileBoundary);
801      }
802
803
804      pcSlice = pcPic->getSlice(0);
805
806      if(pcSlice->getSPS()->getUseSAO())
807      {
808        m_pcSAO->createPicSaoInfo(pcPic, uiNumSlices);
809      }
810
811#if LCU_SYNTAX_ALF
812      AlfParamSet* alfSliceParams = NULL;
813      std::vector<AlfCUCtrlInfo>* alfCUCtrlParam = NULL;
814#else
815      std::vector<AlfCUCtrlInfo> vAlfCUCtrlParam;
816#endif
817      pcSlice = pcPic->getSlice(0);
818
819      if(pcSlice->getSPS()->getUseALF())
820      {
821#if LCU_SYNTAX_ALF
822        m_pcAdaptiveLoopFilter->createPicAlfInfo(pcPic, uiNumSlices, pcSlice->getSliceQp());
823        m_pcAdaptiveLoopFilter->initALFEnc(m_pcCfg->getALFParamInSlice(), m_pcCfg->getALFPicBasedEncode(), uiNumSlices, alfSliceParams, alfCUCtrlParam);
824#else
825        vAlfCUCtrlParam.resize(uiNumSlices);
826        m_pcAdaptiveLoopFilter->createPicAlfInfo(pcPic, uiNumSlices);
827#endif
828      }
829
830      /////////////////////////////////////////////////////////////////////////////////////////////////// File writing
831      // Set entropy coder
832      m_pcEntropyCoder->setEntropyCoder   ( m_pcCavlcCoder, pcSlice );
833
834      /* write various header sets. */
835      if ( m_bSeqFirst )
836      {
837#if VIDYO_VPS_INTEGRATION
838        {
839          OutputNALUnit nalu(NAL_UNIT_VPS, true, m_pcEncTop->getLayerId());
840          m_pcEntropyCoder->setBitstream(&nalu.m_Bitstream);
841          m_pcEntropyCoder->encodeVPS(m_pcEncTop->getEncTop()->getVPS());
842          writeRBSPTrailingBits(nalu.m_Bitstream);
843          accessUnit.push_back(new NALUnitEBSP(nalu));
844        }
845#endif
846#if NAL_REF_FLAG
847#if VIDYO_VPS_INTEGRATION
848        OutputNALUnit nalu(NAL_UNIT_SPS, true, m_pcEncTop->getLayerId());
849#else
850        OutputNALUnit nalu(NAL_UNIT_SPS, true, m_pcEncTop->getViewId(), m_pcEncTop->getIsDepth());
851#endif
852#else
853        OutputNALUnit nalu(NAL_UNIT_SPS, NAL_REF_IDC_PRIORITY_HIGHEST, m_pcEncTop->getViewId(), m_pcEncTop->getIsDepth());
854#endif
855        m_pcEntropyCoder->setBitstream(&nalu.m_Bitstream);
856#if TILES_WPP_ENTRY_POINT_SIGNALLING
857        pcSlice->getSPS()->setNumSubstreams( pcSlice->getPPS()->getNumSubstreams() );
858#endif
859#if HHI_MPI
860        m_pcEntropyCoder->encodeSPS(pcSlice->getSPS(), m_pcEncTop->getIsDepth());
861#else
862        m_pcEntropyCoder->encodeSPS(pcSlice->getSPS());
863#endif
864        writeRBSPTrailingBits(nalu.m_Bitstream);
865        accessUnit.push_back(new NALUnitEBSP(nalu));
866
867#if NAL_REF_FLAG
868#if VIDYO_VPS_INTEGRATION
869        nalu = NALUnit(NAL_UNIT_PPS, true, m_pcEncTop->getLayerId());
870#else
871        nalu = NALUnit(NAL_UNIT_PPS, true, m_pcEncTop->getViewId(), m_pcEncTop->getIsDepth());
872#endif
873#else
874        nalu = NALUnit(NAL_UNIT_PPS, NAL_REF_IDC_PRIORITY_HIGHEST, m_pcEncTop->getViewId(), m_pcEncTop->getIsDepth());
875#endif
876        m_pcEntropyCoder->setBitstream(&nalu.m_Bitstream);
877        m_pcEntropyCoder->encodePPS(pcSlice->getPPS());
878        writeRBSPTrailingBits(nalu.m_Bitstream);
879        accessUnit.push_back(new NALUnitEBSP(nalu));
880
881        m_bSeqFirst = false;
882      }
883
884      /* use the main bitstream buffer for storing the marshalled picture */
885      m_pcEntropyCoder->setBitstream(NULL);
886
887      uiStartCUAddrSliceIdx = 0;
888      uiStartCUAddrSlice    = 0; 
889
890      uiStartCUAddrEntropySliceIdx = 0;
891      uiStartCUAddrEntropySlice    = 0; 
892      uiNextCUAddr                 = 0;
893      pcSlice = pcPic->getSlice(uiStartCUAddrSliceIdx);
894
895      Int processingState = (pcSlice->getSPS()->getUseALF() || pcSlice->getSPS()->getUseSAO() || pcSlice->getSPS()->getScalingListFlag() || pcSlice->getSPS()->getUseDF())?(EXECUTE_INLOOPFILTER):(ENCODE_SLICE);
896
897      static Int iCurrAPSIdx = 0;
898      Int iCodedAPSIdx = 0;
899      TComSlice* pcSliceForAPS = NULL;
900
901      bool skippedSlice=false;
902      while (uiNextCUAddr < uiRealEndAddress) // Iterate over all slices
903      {
904        switch(processingState)
905        {
906        case ENCODE_SLICE:
907          {
908        pcSlice->setNextSlice       ( false );
909        pcSlice->setNextEntropySlice( false );
910        if (uiNextCUAddr == m_uiStoredStartCUAddrForEncodingSlice[uiStartCUAddrSliceIdx])
911        {
912          pcSlice = pcPic->getSlice(uiStartCUAddrSliceIdx);
913#if COLLOCATED_REF_IDX
914          if(uiStartCUAddrSliceIdx > 0 && pcSlice->getSliceType()!= I_SLICE)
915          {
916            pcSlice->checkColRefIdx(uiStartCUAddrSliceIdx, pcPic);
917          }
918#endif
919          pcPic->setCurrSliceIdx(uiStartCUAddrSliceIdx);
920          m_pcSliceEncoder->setSliceIdx(uiStartCUAddrSliceIdx);
921          assert(uiStartCUAddrSliceIdx == pcSlice->getSliceIdx());
922          // Reconstruction slice
923          pcSlice->setSliceCurStartCUAddr( uiNextCUAddr );  // to be used in encodeSlice() + context restriction
924          pcSlice->setSliceCurEndCUAddr  ( m_uiStoredStartCUAddrForEncodingSlice[uiStartCUAddrSliceIdx+1 ] );
925          // Entropy slice
926          pcSlice->setEntropySliceCurStartCUAddr( uiNextCUAddr );  // to be used in encodeSlice() + context restriction
927          pcSlice->setEntropySliceCurEndCUAddr  ( m_uiStoredStartCUAddrForEncodingEntropySlice[uiStartCUAddrEntropySliceIdx+1 ] );
928
929          pcSlice->setNextSlice       ( true );
930
931          uiStartCUAddrSliceIdx++;
932          uiStartCUAddrEntropySliceIdx++;
933        } 
934        else if (uiNextCUAddr == m_uiStoredStartCUAddrForEncodingEntropySlice[uiStartCUAddrEntropySliceIdx])
935        {
936          // Entropy slice
937          pcSlice->setEntropySliceCurStartCUAddr( uiNextCUAddr );  // to be used in encodeSlice() + context restriction
938          pcSlice->setEntropySliceCurEndCUAddr  ( m_uiStoredStartCUAddrForEncodingEntropySlice[uiStartCUAddrEntropySliceIdx+1 ] );
939
940          pcSlice->setNextEntropySlice( true );
941
942          uiStartCUAddrEntropySliceIdx++;
943        }
944
945      pcSlice->setRPS(pcPic->getSlice(0)->getRPS());
946      pcSlice->setRPSidx(pcPic->getSlice(0)->getRPSidx());
947        UInt uiDummyStartCUAddr;
948        UInt uiDummyBoundingCUAddr;
949        m_pcSliceEncoder->xDetermineStartAndBoundingCUAddr(uiDummyStartCUAddr,uiDummyBoundingCUAddr,pcPic,true);
950
951        uiInternalAddress = pcPic->getPicSym()->getPicSCUAddr(pcSlice->getEntropySliceCurEndCUAddr()-1) % pcPic->getNumPartInCU();
952        uiExternalAddress = pcPic->getPicSym()->getPicSCUAddr(pcSlice->getEntropySliceCurEndCUAddr()-1) / pcPic->getNumPartInCU();
953        uiPosX = ( uiExternalAddress % pcPic->getFrameWidthInCU() ) * g_uiMaxCUWidth+ g_auiRasterToPelX[ g_auiZscanToRaster[uiInternalAddress] ];
954        uiPosY = ( uiExternalAddress / pcPic->getFrameWidthInCU() ) * g_uiMaxCUHeight+ g_auiRasterToPelY[ g_auiZscanToRaster[uiInternalAddress] ];
955        uiWidth = pcSlice->getSPS()->getPicWidthInLumaSamples();
956        uiHeight = pcSlice->getSPS()->getPicHeightInLumaSamples();
957        while(uiPosX>=uiWidth||uiPosY>=uiHeight)
958        {
959          uiInternalAddress--;
960          uiPosX = ( uiExternalAddress % pcPic->getFrameWidthInCU() ) * g_uiMaxCUWidth+ g_auiRasterToPelX[ g_auiZscanToRaster[uiInternalAddress] ];
961          uiPosY = ( uiExternalAddress / pcPic->getFrameWidthInCU() ) * g_uiMaxCUHeight+ g_auiRasterToPelY[ g_auiZscanToRaster[uiInternalAddress] ];
962        }
963        uiInternalAddress++;
964        if(uiInternalAddress==pcPic->getNumPartInCU())
965        {
966          uiInternalAddress = 0;
967          uiExternalAddress = pcPic->getPicSym()->getCUOrderMap(pcPic->getPicSym()->getInverseCUOrderMap(uiExternalAddress)+1);
968        }
969        UInt uiEndAddress = pcPic->getPicSym()->getPicSCUEncOrder(uiExternalAddress*pcPic->getNumPartInCU()+uiInternalAddress);
970        if(uiEndAddress<=pcSlice->getEntropySliceCurStartCUAddr()) {
971          UInt uiBoundingAddrSlice, uiBoundingAddrEntropySlice;
972          uiBoundingAddrSlice        = m_uiStoredStartCUAddrForEncodingSlice[uiStartCUAddrSliceIdx];         
973          uiBoundingAddrEntropySlice = m_uiStoredStartCUAddrForEncodingEntropySlice[uiStartCUAddrEntropySliceIdx];         
974          uiNextCUAddr               = min(uiBoundingAddrSlice, uiBoundingAddrEntropySlice);
975          if(pcSlice->isNextSlice())
976          {
977            skippedSlice=true;
978          }
979          continue;
980        }
981        if(skippedSlice) 
982        {
983          pcSlice->setNextSlice       ( true );
984          pcSlice->setNextEntropySlice( false );
985        }
986        skippedSlice=false;
987        if (pcSlice->getPPS()->getEntropyCodingMode())
988        {
989          pcSlice->allocSubstreamSizes( iNumSubstreams );
990          for ( UInt ui = 0 ; ui < iNumSubstreams; ui++ )
991          pcSubstreamsOut[ui].clear();
992        }
993
994        m_pcEntropyCoder->setEntropyCoder   ( m_pcCavlcCoder, pcSlice );
995        m_pcEntropyCoder->resetEntropy      ();
996        /* start slice NALunit */
997#if H0388
998#if NAL_REF_FLAG
999        OutputNALUnit nalu( pcSlice->getNalUnitType(), pcSlice->isReferenced(),
1000#if !VIDYO_VPS_INTEGRATION
1001                           m_pcEncTop->getViewId(), m_pcEncTop->getIsDepth(), pcSlice->getTLayer() );
1002#else
1003                           m_pcEncTop->getLayerId(), pcSlice->getTLayer() );
1004#endif
1005#else
1006        OutputNALUnit nalu( pcSlice->getNalUnitType(), pcSlice->isReferenced() ? NAL_REF_IDC_PRIORITY_HIGHEST: NAL_REF_IDC_PRIORITY_LOWEST, 
1007#if !VIDYO_VPS_INTEGRATION
1008                           m_pcEncTop->getViewId(), m_pcEncTop->getIsDepth(), pcSlice->getTLayer() );
1009#else
1010                           m_pcEncTop->getLayerId(), pcSlice->getTLayer() );
1011#endif
1012#endif
1013#else
1014        OutputNALUnit nalu( pcSlice->getNalUnitType(), pcSlice->isReferenced() ? NAL_REF_IDC_PRIORITY_HIGHEST: NAL_REF_IDC_PRIORITY_LOWEST, 
1015#if !VIDYO_VPS_INTEGRATION
1016                           m_pcEncTop->getViewId(), m_pcEncTop->getIsDepth(), pcSlice->getTLayer(), true );
1017#else
1018                           m_pcEncTop->getLayerId(), pcSlice->getTLayer(), true );
1019#endif
1020
1021#endif
1022           
1023        Bool bEntropySlice = (!pcSlice->isNextSlice());
1024        if (!bEntropySlice)
1025        {
1026          uiOneBitstreamPerSliceLength = 0; // start of a new slice
1027        }
1028
1029        // used while writing slice header
1030        Int iTransmitLWHeader = (m_pcCfg->getTileMarkerFlag()==0) ? 0 : 1;
1031        pcSlice->setTileMarkerFlag ( iTransmitLWHeader );
1032        m_pcEntropyCoder->setBitstream(&nalu.m_Bitstream);
1033#if !CABAC_INIT_FLAG
1034        pcSlice->setCABACinitIDC(pcSlice->getSliceType());
1035#endif
1036
1037        m_pcEntropyCoder->encodeSliceHeader(pcSlice);
1038
1039        if(pcSlice->isNextSlice())
1040        {
1041          if (pcSlice->getSPS()->getUseALF())
1042          {
1043#if LCU_SYNTAX_ALF
1044            if(pcSlice->getAlfEnabledFlag())
1045#else
1046            if(pcSlice->getAPS()->getAlfEnabled())
1047#endif
1048            {
1049
1050#if LCU_SYNTAX_ALF
1051              if( pcSlice->getSPS()->getUseALFCoefInSlice())
1052              {
1053                Int iNumSUinLCU    = 1<< (g_uiMaxCUDepth << 1); 
1054                Int firstLCUAddr   = pcSlice->getSliceCurStartCUAddr() / iNumSUinLCU; 
1055                Bool isAcrossSlice = pcSlice->getSPS()->getLFCrossSliceBoundaryFlag();
1056                m_pcEntropyCoder->encodeAlfParam( &(alfSliceParams[pcSlice->getSliceIdx()]), false, firstLCUAddr, isAcrossSlice);
1057              }
1058
1059              if( !pcSlice->getSPS()->getUseALFCoefInSlice())
1060              {
1061                AlfCUCtrlInfo& cAlfCUCtrlParam = (*alfCUCtrlParam)[pcSlice->getSliceIdx()];
1062#else
1063              AlfCUCtrlInfo& cAlfCUCtrlParam = vAlfCUCtrlParam[pcSlice->getSliceIdx()];
1064#endif
1065              if(cAlfCUCtrlParam.cu_control_flag)
1066              {
1067                m_pcEntropyCoder->setAlfCtrl( true );
1068                m_pcEntropyCoder->setMaxAlfCtrlDepth(cAlfCUCtrlParam.alf_max_depth);
1069                m_pcCavlcCoder->setAlfCtrl(true);
1070                m_pcCavlcCoder->setMaxAlfCtrlDepth(cAlfCUCtrlParam.alf_max_depth); 
1071              }
1072              else
1073              {
1074                m_pcEntropyCoder->setAlfCtrl(false);
1075              }
1076              m_pcEntropyCoder->encodeAlfCtrlParam(cAlfCUCtrlParam, m_pcAdaptiveLoopFilter->getNumCUsInPic());
1077           
1078#if LCU_SYNTAX_ALF
1079              }
1080#endif           
1081            }
1082          }
1083        }
1084        m_pcEntropyCoder->encodeTileMarkerFlag(pcSlice);
1085
1086        // is it needed?
1087        {
1088          if (!bEntropySlice)
1089          {
1090            pcBitstreamRedirect->writeAlignOne();
1091          }
1092          else
1093          {
1094          // We've not completed our slice header info yet, do the alignment later.
1095          }
1096          m_pcSbacCoder->init( (TEncBinIf*)m_pcBinCABAC );
1097          m_pcEntropyCoder->setEntropyCoder ( m_pcSbacCoder, pcSlice );
1098          m_pcEntropyCoder->resetEntropy    ();
1099          for ( UInt ui = 0 ; ui < pcSlice->getPPS()->getNumSubstreams() ; ui++ )
1100          {
1101            m_pcEntropyCoder->setEntropyCoder ( &pcSbacCoders[ui], pcSlice );
1102            m_pcEntropyCoder->resetEntropy    ();
1103          }
1104        }
1105
1106        if(pcSlice->isNextSlice())
1107        {
1108          // set entropy coder for writing
1109          m_pcSbacCoder->init( (TEncBinIf*)m_pcBinCABAC );
1110          {
1111            for ( UInt ui = 0 ; ui < pcSlice->getPPS()->getNumSubstreams() ; ui++ )
1112            {
1113              m_pcEntropyCoder->setEntropyCoder ( &pcSbacCoders[ui], pcSlice );
1114              m_pcEntropyCoder->resetEntropy    ();
1115            }
1116            pcSbacCoders[0].load(m_pcSbacCoder);
1117            m_pcEntropyCoder->setEntropyCoder ( &pcSbacCoders[0], pcSlice );  //ALF is written in substream #0 with CABAC coder #0 (see ALF param encoding below)
1118          }
1119          m_pcEntropyCoder->resetEntropy    ();
1120          // File writing
1121          if (!bEntropySlice)
1122          {
1123            m_pcEntropyCoder->setBitstream(pcBitstreamRedirect);
1124          }
1125          else
1126          {
1127            m_pcEntropyCoder->setBitstream(&nalu.m_Bitstream);
1128          }
1129          // for now, override the TILES_DECODER setting in order to write substreams.
1130            m_pcEntropyCoder->setBitstream    ( &pcSubstreamsOut[0] );
1131
1132        }
1133        pcSlice->setFinalized(true);
1134
1135          m_pcSbacCoder->load( &pcSbacCoders[0] );
1136
1137        pcSlice->setTileOffstForMultES( uiOneBitstreamPerSliceLength );
1138        if (!bEntropySlice)
1139        {
1140          pcSlice->setTileLocationCount ( 0 );
1141          m_pcSliceEncoder->encodeSlice(pcPic, pcBitstreamRedirect, pcSubstreamsOut); // redirect is only used for CAVLC tile position info.
1142        }
1143        else
1144        {
1145          m_pcSliceEncoder->encodeSlice(pcPic, &nalu.m_Bitstream, pcSubstreamsOut); // nalu.m_Bitstream is only used for CAVLC tile position info.
1146        }
1147
1148        {
1149          // Construct the final bitstream by flushing and concatenating substreams.
1150          // The final bitstream is either nalu.m_Bitstream or pcBitstreamRedirect;
1151          UInt* puiSubstreamSizes = pcSlice->getSubstreamSizes();
1152          UInt uiTotalCodedSize = 0; // for padding calcs.
1153          UInt uiNumSubstreamsPerTile = iNumSubstreams;
1154#if !REMOVE_TILE_DEPENDENCE
1155#if WPP_SIMPLIFICATION
1156         if (pcPic->getPicSym()->getTileBoundaryIndependenceIdr() && iNumSubstreams > 1)
1157#else
1158          if (pcPic->getPicSym()->getTileBoundaryIndependenceIdr() && pcSlice->getPPS()->getEntropyCodingSynchro())
1159#endif
1160            uiNumSubstreamsPerTile /= pcPic->getPicSym()->getNumTiles();
1161#else
1162#if WPP_SIMPLIFICATION
1163          if (iNumSubstreams > 1)
1164#else
1165          if (pcSlice->getPPS()->getEntropyCodingSynchro())
1166#endif
1167          {
1168            uiNumSubstreamsPerTile /= pcPic->getPicSym()->getNumTiles();
1169          }
1170#endif
1171          for ( UInt ui = 0 ; ui < iNumSubstreams; ui++ )
1172          {
1173            // Flush all substreams -- this includes empty ones.
1174            // Terminating bit and flush.
1175            m_pcEntropyCoder->setEntropyCoder   ( &pcSbacCoders[ui], pcSlice );
1176            m_pcEntropyCoder->setBitstream      (  &pcSubstreamsOut[ui] );
1177            m_pcEntropyCoder->encodeTerminatingBit( 1 );
1178            m_pcEntropyCoder->encodeSliceFinish();
1179            pcSubstreamsOut[ui].write( 1, 1 ); // stop bit.
1180#if TILES_WPP_ENTRY_POINT_SIGNALLING
1181            pcSubstreamsOut[ui].writeAlignZero();
1182#endif
1183            // Byte alignment is necessary between tiles when tiles are independent.
1184            uiTotalCodedSize += pcSubstreamsOut[ui].getNumberOfWrittenBits();
1185
1186            {
1187              Bool bNextSubstreamInNewTile = ((ui+1) < iNumSubstreams)
1188                                             && ((ui+1)%uiNumSubstreamsPerTile == 0);
1189              if (bNextSubstreamInNewTile)
1190              {
1191                // byte align.
1192                while (uiTotalCodedSize&0x7)
1193                {
1194                  pcSubstreamsOut[ui].write(0, 1);
1195                  uiTotalCodedSize++;
1196                }
1197              }
1198              Bool bRecordOffsetNext = m_pcCfg->getTileLocationInSliceHeaderFlag()
1199                                            && bNextSubstreamInNewTile;
1200              if (bRecordOffsetNext)
1201                pcSlice->setTileLocation(ui/uiNumSubstreamsPerTile, pcSlice->getTileOffstForMultES()+(uiTotalCodedSize>>3));
1202            }
1203            if (ui+1 < pcSlice->getPPS()->getNumSubstreams())
1204              puiSubstreamSizes[ui] = pcSubstreamsOut[ui].getNumberOfWrittenBits();
1205          }
1206          // Complete the slice header info.
1207          m_pcEntropyCoder->setEntropyCoder   ( m_pcCavlcCoder, pcSlice );
1208          m_pcEntropyCoder->setBitstream(&nalu.m_Bitstream);
1209#if TILES_WPP_ENTRY_POINT_SIGNALLING
1210          if (m_pcCfg->getTileLocationInSliceHeaderFlag()==0) 
1211          {
1212            pcSlice->setTileLocationCount( 0 );
1213          }
1214          m_pcEntropyCoder->encodeTilesWPPEntryPoint( pcSlice );
1215#else
1216          m_pcEntropyCoder->encodeSliceHeaderSubstreamTable(pcSlice);
1217#endif
1218          // Substreams...
1219          TComOutputBitstream *pcOut = pcBitstreamRedirect;
1220          // xWriteTileLocation will perform byte-alignment...
1221          {
1222            if (bEntropySlice)
1223            {
1224              // In these cases, padding is necessary here.
1225              pcOut = &nalu.m_Bitstream;
1226              pcOut->writeAlignOne();
1227            }
1228          }
1229          UInt uiAccumulatedLength = 0;
1230          for ( UInt ui = 0 ; ui < pcSlice->getPPS()->getNumSubstreams(); ui++ )
1231          {
1232            pcOut->addSubstream(&pcSubstreamsOut[ui]);
1233
1234            // Update tile marker location information
1235            for (Int uiMrkIdx = 0; uiMrkIdx < pcSubstreamsOut[ui].getTileMarkerLocationCount(); uiMrkIdx++)
1236            {
1237              UInt uiBottom = pcOut->getTileMarkerLocationCount();
1238              pcOut->setTileMarkerLocation      ( uiBottom, uiAccumulatedLength + pcSubstreamsOut[ui].getTileMarkerLocation( uiMrkIdx ) );
1239              pcOut->setTileMarkerLocationCount ( uiBottom + 1 );
1240            }
1241            uiAccumulatedLength = (pcOut->getNumberOfWrittenBits() >> 3);
1242          }
1243        }
1244
1245        UInt uiBoundingAddrSlice, uiBoundingAddrEntropySlice;
1246        uiBoundingAddrSlice        = m_uiStoredStartCUAddrForEncodingSlice[uiStartCUAddrSliceIdx];         
1247        uiBoundingAddrEntropySlice = m_uiStoredStartCUAddrForEncodingEntropySlice[uiStartCUAddrEntropySliceIdx];         
1248        uiNextCUAddr               = min(uiBoundingAddrSlice, uiBoundingAddrEntropySlice);
1249#if !REMOVE_TILE_DEPENDENCE
1250        Bool bNextCUInNewSlice     = (uiNextCUAddr >= uiRealEndAddress) || (uiNextCUAddr == m_uiStoredStartCUAddrForEncodingSlice[uiStartCUAddrSliceIdx]);
1251#endif
1252        // If current NALU is the first NALU of slice (containing slice header) and more NALUs exist (due to multiple entropy slices) then buffer it.
1253        // 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.
1254        Bool bNALUAlignedWrittenToList    = false; // used to ensure current NALU is not written more than once to the NALU list.
1255#if !REMOVE_TILE_DEPENDENCE
1256        if (pcSlice->getSPS()->getTileBoundaryIndependenceIdr() && !pcSlice->getSPS()->getTileBoundaryIndependenceIdr())
1257        {
1258          if (bNextCUInNewSlice)
1259          {
1260            if (!bEntropySlice) // there were no entropy slices
1261            {
1262              xWriteTileLocationToSliceHeader(nalu, pcBitstreamRedirect, pcSlice);
1263            }
1264            // (a) writing current NALU
1265            writeRBSPTrailingBits(nalu.m_Bitstream);
1266            accessUnit.push_back(new NALUnitEBSP(nalu));
1267            bNALUAlignedWrittenToList = true;
1268
1269            // (b) update and write buffered NALU
1270            if (bEntropySlice) // if entropy slices existed in the slice then perform concatenation for the buffered nalu-bitstream and buffered payload bitstream
1271            {
1272              // Perform bitstream concatenation of slice header and partial slice payload
1273              xWriteTileLocationToSliceHeader((*naluBuffered), pcBitstreamRedirect, pcSlice);
1274              if (bIteratorAtListStart)
1275              {
1276                itLocationToPushSliceHeaderNALU = accessUnit.begin();
1277              }
1278              else
1279              {
1280                itLocationToPushSliceHeaderNALU++;
1281              }
1282              accessUnit.insert(itLocationToPushSliceHeaderNALU, (new NALUnitEBSP((*naluBuffered))) );
1283
1284              // free buffered nalu
1285              delete naluBuffered;
1286              naluBuffered     = NULL;
1287            }
1288          }
1289          else // another entropy slice exists
1290          {
1291            // Is this start-of-slice NALU? i.e. the one containing slice header. If Yes, then buffer it.
1292            if (!bEntropySlice)
1293            {
1294              // store a pointer to where NALU for slice header is to be written in NALU list
1295              itLocationToPushSliceHeaderNALU = accessUnit.end();
1296              if (accessUnit.begin() == accessUnit.end())
1297              {
1298                bIteratorAtListStart = true;
1299              }
1300              else
1301              {
1302                bIteratorAtListStart = false;
1303                itLocationToPushSliceHeaderNALU--;
1304              }
1305
1306              // buffer nalu for later writing
1307#if H0388
1308              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() );
1309#else
1310              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);
1311#endif
1312              copyNaluData( (*naluBuffered), nalu );
1313
1314              // perform byte-alignment to get appropriate bitstream length (used for explicit tile location signaling in slice header)
1315              writeRBSPTrailingBits((*pcBitstreamRedirect));
1316              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.
1317              uiOneBitstreamPerSliceLength += pcBitstreamRedirect->getNumberOfWrittenBits(); // length of bitstream after byte-alignment
1318            }
1319            else // write out entropy slice
1320            {
1321              writeRBSPTrailingBits(nalu.m_Bitstream);
1322              accessUnit.push_back(new NALUnitEBSP(nalu));
1323              bNALUAlignedWrittenToList = true; 
1324              uiOneBitstreamPerSliceLength += nalu.m_Bitstream.getNumberOfWrittenBits(); // length of bitstream after byte-alignment
1325            }
1326          }
1327        }
1328        else
1329        {
1330#endif
1331        xWriteTileLocationToSliceHeader(nalu, pcBitstreamRedirect, pcSlice);
1332        writeRBSPTrailingBits(nalu.m_Bitstream);
1333        accessUnit.push_back(new NALUnitEBSP(nalu));
1334        bNALUAlignedWrittenToList = true; 
1335        uiOneBitstreamPerSliceLength += nalu.m_Bitstream.getNumberOfWrittenBits(); // length of bitstream after byte-alignment
1336#if !REMOVE_TILE_DEPENDENCE
1337        }
1338#endif
1339
1340        if (!bNALUAlignedWrittenToList)
1341        {
1342        {
1343          nalu.m_Bitstream.writeAlignZero();
1344        }
1345        accessUnit.push_back(new NALUnitEBSP(nalu));
1346        uiOneBitstreamPerSliceLength += nalu.m_Bitstream.getNumberOfWrittenBits() + 24; // length of bitstream after byte-alignment + 3 byte startcode 0x000001
1347        }
1348
1349
1350        processingState = ENCODE_SLICE;
1351          }
1352          break;
1353        case EXECUTE_INLOOPFILTER:
1354          {
1355            TComAPS cAPS;
1356            allocAPS(&cAPS, pcSlice->getSPS());
1357#if SAO_UNIT_INTERLEAVING
1358            cAPS.setSaoInterleavingFlag(m_pcCfg->getSaoInterleavingFlag());
1359#endif
1360            // set entropy coder for RD
1361            m_pcEntropyCoder->setEntropyCoder ( m_pcCavlcCoder, pcSlice );
1362
1363            if ( pcSlice->getSPS()->getUseSAO() )
1364            {
1365              m_pcEntropyCoder->resetEntropy();
1366              m_pcEntropyCoder->setBitstream( m_pcBitCounter );
1367              m_pcSAO->startSaoEnc(pcPic, m_pcEntropyCoder, m_pcEncTop->getRDSbacCoder(), NULL);
1368              SAOParam& cSaoParam = *(cAPS.getSaoParam());
1369
1370#if SAO_CHROMA_LAMBDA
1371              m_pcSAO->SAOProcess(&cSaoParam, pcPic->getSlice(0)->getLambdaLuma(), pcPic->getSlice(0)->getLambdaChroma());
1372#else
1373#if ALF_CHROMA_LAMBDA
1374              m_pcSAO->SAOProcess(&cSaoParam, pcPic->getSlice(0)->getLambdaLuma());
1375#else
1376              m_pcSAO->SAOProcess(&cSaoParam, pcPic->getSlice(0)->getLambda());
1377#endif
1378#endif
1379              m_pcSAO->endSaoEnc();
1380
1381              m_pcAdaptiveLoopFilter->PCMLFDisableProcess(pcPic);
1382            }
1383
1384            // adaptive loop filter
1385#if !LCU_SYNTAX_ALF
1386            UInt64 uiDist, uiBits;
1387#endif
1388            if ( pcSlice->getSPS()->getUseALF())
1389            {
1390              m_pcEntropyCoder->resetEntropy    ();
1391              m_pcEntropyCoder->setBitstream    ( m_pcBitCounter );
1392              m_pcAdaptiveLoopFilter->startALFEnc(pcPic, m_pcEntropyCoder );
1393#if LCU_SYNTAX_ALF
1394              AlfParamSet* pAlfEncParam = (pcSlice->getSPS()->getUseALFCoefInSlice())?( alfSliceParams ):( cAPS.getAlfParam());
1395#if ALF_CHROMA_LAMBDA
1396#if HHI_INTERVIEW_SKIP
1397              m_pcAdaptiveLoopFilter->ALFProcess(pAlfEncParam, alfCUCtrlParam, pcPic->getSlice(0)->getLambdaLuma(), pcPic->getSlice(0)->getLambdaChroma(), m_pcEncTop->getInterViewSkip()  );
1398#else
1399              m_pcAdaptiveLoopFilter->ALFProcess(pAlfEncParam, alfCUCtrlParam, pcPic->getSlice(0)->getLambdaLuma(), pcPic->getSlice(0)->getLambdaChroma() );
1400#endif
1401#else
1402#if SAO_CHROMA_LAMBDA
1403#if HHI_INTERVIEW_SKIP
1404              m_pcAdaptiveLoopFilter->ALFProcess(pAlfEncParam, alfCUCtrlParam, pcPic->getSlice(0)->getLambdaLuma(), m_pcEncTop->getInterViewSkip());
1405#else
1406              m_pcAdaptiveLoopFilter->ALFProcess(pAlfEncParam, alfCUCtrlParam, pcPic->getSlice(0)->getLambdaLuma());
1407#endif
1408#else
1409#if HHI_INTERVIEW_SKIP
1410              m_pcAdaptiveLoopFilter->ALFProcess(pAlfEncParam, alfCUCtrlParam, pcPic->getSlice(0)->getLambda(), m_pcEncTop->getInterViewSkip() );
1411#else
1412              m_pcAdaptiveLoopFilter->ALFProcess(pAlfEncParam, alfCUCtrlParam, pcPic->getSlice(0)->getLambda());
1413#endif
1414#endif
1415#endif
1416
1417#else
1418              ALFParam& cAlfParam = *( cAPS.getAlfParam());
1419
1420#if ALF_CHROMA_LAMBDA
1421#if HHI_INTERVIEW_SKIP
1422              m_pcAdaptiveLoopFilter->ALFProcess( &cAlfParam, &vAlfCUCtrlParam, pcPic->getSlice(0)->getLambdaLuma(), pcPic->getSlice(0)->getLambdaChroma(), uiDist, uiBits, m_pcEncTop->getInterViewSkip());
1423#else
1424              m_pcAdaptiveLoopFilter->ALFProcess( &cAlfParam, &vAlfCUCtrlParam, pcPic->getSlice(0)->getLambdaLuma(), pcPic->getSlice(0)->getLambdaChroma(), uiDist, uiBits);
1425#endif
1426#else
1427#if SAO_CHROMA_LAMBDA
1428#if HHI_INTERVIEW_SKIP
1429              m_pcAdaptiveLoopFilter->ALFProcess( &cAlfParam, &vAlfCUCtrlParam, pcPic->getSlice(0)->getLambdaLuma(), uiDist, uiBits, m_pcEncTop->getInterViewSkip());
1430#else
1431              m_pcAdaptiveLoopFilter->ALFProcess( &cAlfParam, &vAlfCUCtrlParam, pcPic->getSlice(0)->getLambdaLuma(), uiDist, uiBits);
1432#endif
1433#else
1434#if HHI_INTERVIEW_SKIP
1435              m_pcAdaptiveLoopFilter->ALFProcess( &cAlfParam, &vAlfCUCtrlParam, pcPic->getSlice(0)->getLambdaLuma(), uiDist, uiBits, m_pcEncTop->getInterViewSkip());
1436#else
1437              m_pcAdaptiveLoopFilter->ALFProcess( &cAlfParam, &vAlfCUCtrlParam, pcPic->getSlice(0)->getLambda(), uiDist, uiBits);
1438#endif
1439#endif
1440#endif
1441#endif
1442              m_pcAdaptiveLoopFilter->endALFEnc();
1443
1444              m_pcAdaptiveLoopFilter->PCMLFDisableProcess(pcPic);
1445            }
1446            iCodedAPSIdx = iCurrAPSIdx; 
1447            pcSliceForAPS = pcSlice;
1448
1449            assignNewAPS(cAPS, iCodedAPSIdx, vAPS, pcSliceForAPS);
1450            iCurrAPSIdx = (iCurrAPSIdx +1)%MAX_NUM_SUPPORTED_APS;
1451            processingState = ENCODE_APS;
1452
1453            //set APS link to the slices
1454            for(Int s=0; s< uiNumSlices; s++)
1455            {
1456              if (pcSlice->getSPS()->getUseALF())
1457              {
1458#if LCU_SYNTAX_ALF
1459                pcPic->getSlice(s)->setAlfEnabledFlag(  (pcSlice->getSPS()->getUseALFCoefInSlice())?(alfSliceParams[s].isEnabled[ALF_Y]):(cAPS.getAlfEnabled())   );
1460#else
1461                pcPic->getSlice(s)->setAlfEnabledFlag((cAPS.getAlfParam()->alf_flag==1)?true:false);
1462#endif
1463              }
1464              if (pcSlice->getSPS()->getUseSAO())
1465              {
1466                pcPic->getSlice(s)->setSaoEnabledFlag((cAPS.getSaoParam()->bSaoFlag[0]==1)?true:false);
1467              }
1468              pcPic->getSlice(s)->setAPS(&(vAPS[iCodedAPSIdx]));
1469              pcPic->getSlice(s)->setAPSId(iCodedAPSIdx);
1470            }
1471          }
1472          break;
1473        case ENCODE_APS:
1474          {
1475#if NAL_REF_FLAG
1476#if VIDYO_VPS_INTEGRATION
1477            OutputNALUnit nalu(NAL_UNIT_APS, true, m_pcEncTop->getLayerId());
1478#else
1479            OutputNALUnit nalu(NAL_UNIT_APS, true, m_pcEncTop->getViewId(), m_pcEncTop->getIsDepth());
1480#endif
1481#else
1482            OutputNALUnit nalu(NAL_UNIT_APS, NAL_REF_IDC_PRIORITY_HIGHEST, m_pcEncTop->getViewId(), m_pcEncTop->getIsDepth());
1483#endif
1484            encodeAPS(&(vAPS[iCodedAPSIdx]), nalu.m_Bitstream, pcSliceForAPS);
1485            accessUnit.push_back(new NALUnitEBSP(nalu));
1486
1487            processingState = ENCODE_SLICE;
1488          }
1489          break;
1490        default:
1491          {
1492            printf("Not a supported encoding state\n");
1493            assert(0);
1494            exit(-1);
1495          }
1496        }
1497      } // end iteration over slices
1498
1499
1500      if(pcSlice->getSPS()->getUseSAO() || pcSlice->getSPS()->getUseALF())
1501      {
1502        if(pcSlice->getSPS()->getUseSAO())
1503        {
1504          m_pcSAO->destroyPicSaoInfo();
1505        }
1506
1507        if(pcSlice->getSPS()->getUseALF())
1508        {
1509#if LCU_SYNTAX_ALF
1510          m_pcAdaptiveLoopFilter->uninitALFEnc(alfSliceParams, alfCUCtrlParam);
1511#endif
1512          m_pcAdaptiveLoopFilter->destroyPicAlfInfo();
1513        }
1514
1515        pcPic->destroyNonDBFilterInfo();
1516      }
1517
1518#if HHI_INTERVIEW_SKIP
1519      if (pcPic->getUsedPelsMap())
1520        pcPic->removeUsedPelsMapBuffer() ;
1521#endif
1522#if HHI_INTER_VIEW_MOTION_PRED
1523      pcPic->removeOrgDepthMapBuffer();
1524#endif
1525   
1526   //   pcPic->compressMotion();
1527      m_pocLastCoded = pcPic->getPOC();
1528     
1529      //-- For time output for each slice
1530      Double dEncTime = (double)(clock()-iBeforeTime) / CLOCKS_PER_SEC;
1531
1532      const char* digestStr = NULL;
1533      if (m_pcCfg->getPictureDigestEnabled())
1534      {
1535        /* calculate MD5sum for entire reconstructed picture */
1536        SEIpictureDigest sei_recon_picture_digest;
1537        sei_recon_picture_digest.method = SEIpictureDigest::MD5;
1538        calcMD5(*pcPic->getPicYuvRec(), sei_recon_picture_digest.digest);
1539        digestStr = digestToString(sei_recon_picture_digest.digest);
1540
1541#if NAL_REF_FLAG
1542#if VIDYO_VPS_INTEGRATION
1543        OutputNALUnit nalu(NAL_UNIT_SEI, false, m_pcEncTop->getLayerId());
1544#else
1545        OutputNALUnit nalu(NAL_UNIT_SEI, false, m_pcEncTop->getViewId(), m_pcEncTop->getIsDepth());
1546#endif
1547#else
1548        OutputNALUnit nalu(NAL_UNIT_SEI, NAL_REF_IDC_PRIORITY_LOWEST, m_pcEncTop->getViewId(), m_pcEncTop->getIsDepth());
1549#endif
1550
1551        /* write the SEI messages */
1552        m_pcEntropyCoder->setEntropyCoder(m_pcCavlcCoder, pcSlice);
1553        m_pcEntropyCoder->setBitstream(&nalu.m_Bitstream);
1554        m_pcEntropyCoder->encodeSEI(sei_recon_picture_digest);
1555        writeRBSPTrailingBits(nalu.m_Bitstream);
1556
1557        /* insert the SEI message NALUnit before any Slice NALUnits */
1558        AccessUnit::iterator it = find_if(accessUnit.begin(), accessUnit.end(), mem_fun(&NALUnit::isSlice));
1559        accessUnit.insert(it, new NALUnitEBSP(nalu));
1560      }
1561
1562      xCalculateAddPSNR( pcPic, pcPic->getPicYuvRec(), accessUnit, dEncTime );
1563      if (digestStr)
1564        printf(" [MD5:%s]", digestStr);
1565
1566#if FIXED_ROUNDING_FRAME_MEMORY
1567      /* TODO: this should happen after copyToPic(pcPicYuvRecOut) */
1568      pcPic->getPicYuvRec()->xFixedRoundingPic();
1569#endif
1570      pcPic->getPicYuvRec()->copyToPic(pcPicYuvRecOut);
1571     
1572      pcPic->setReconMark   ( true );
1573
1574      pcPic->setUsedForTMVP ( true );
1575
1576      m_bFirst = false;
1577      m_iNumPicCoded++;
1578
1579      /* logging: insert a newline at end of picture period */
1580      printf("\n");
1581      fflush(stdout);
1582  }
1583 
1584  delete[] pcSubstreamsOut;
1585  delete pcBitstreamRedirect;
1586
1587}
1588
1589/** Memory allocation for APS
1590  * \param [out] pAPS APS pointer
1591  * \param [in] pSPS SPS pointer
1592  */
1593Void TEncGOP::allocAPS (TComAPS* pAPS, TComSPS* pSPS)
1594{
1595  if(pSPS->getUseSAO())
1596  {
1597    pAPS->createSaoParam();
1598    m_pcSAO->allocSaoParam(pAPS->getSaoParam());
1599  }
1600  if(pSPS->getUseALF())
1601  {
1602    pAPS->createAlfParam();
1603#if LCU_SYNTAX_ALF
1604    //alf Enabled flag in APS is false after pAPS->createAlfParam();
1605    if(!pSPS->getUseALFCoefInSlice())
1606    {
1607      pAPS->getAlfParam()->create(m_pcAdaptiveLoopFilter->getNumLCUInPicWidth(), m_pcAdaptiveLoopFilter->getNumLCUInPicHeight(), m_pcAdaptiveLoopFilter->getNumCUsInPic());
1608      pAPS->getAlfParam()->createALFParam();
1609    }
1610#else
1611    m_pcAdaptiveLoopFilter->allocALFParam(pAPS->getAlfParam());
1612#endif
1613  }
1614}
1615
1616/** Memory deallocation for APS
1617  * \param [out] pAPS APS pointer
1618  * \param [in] pSPS SPS pointer
1619  */
1620Void TEncGOP::freeAPS (TComAPS* pAPS, TComSPS* pSPS)
1621{
1622  if(pSPS->getUseSAO())
1623  {
1624    if(pAPS->getSaoParam() != NULL)
1625    {
1626      m_pcSAO->freeSaoParam(pAPS->getSaoParam());
1627      pAPS->destroySaoParam();
1628
1629    }
1630  }
1631  if(pSPS->getUseALF())
1632  {
1633    if(pAPS->getAlfParam() != NULL)
1634    {
1635#if LCU_SYNTAX_ALF
1636      if(!pSPS->getUseALFCoefInSlice())
1637      {
1638        pAPS->getAlfParam()->releaseALFParam();
1639      }
1640#else
1641      m_pcAdaptiveLoopFilter->freeALFParam(pAPS->getAlfParam());
1642#endif
1643      pAPS->destroyAlfParam();
1644    }
1645  }
1646}
1647
1648/** Assign APS object into APS container according to APS ID
1649  * \param [in] cAPS APS object
1650  * \param [in] apsID APS ID
1651  * \param [in,out] vAPS APS container
1652  * \param [in] pcSlice pointer to slice
1653  */
1654Void TEncGOP::assignNewAPS(TComAPS& cAPS, Int apsID, std::vector<TComAPS>& vAPS, TComSlice* pcSlice)
1655{
1656
1657  cAPS.setAPSID(apsID);
1658  if(pcSlice->getPOC() == 0)
1659  {
1660    cAPS.setScalingListEnabled(pcSlice->getSPS()->getScalingListFlag());
1661  }
1662  else
1663  {
1664    cAPS.setScalingListEnabled(false);
1665  }
1666
1667  cAPS.setSaoEnabled(pcSlice->getSPS()->getUseSAO() ? (cAPS.getSaoParam()->bSaoFlag[0] ):(false));
1668#if LCU_SYNTAX_ALF
1669  cAPS.setAlfEnabled(pcSlice->getSPS()->getUseALF() ? (cAPS.getAlfParam()->isEnabled[0]):(false));
1670#else
1671  cAPS.setAlfEnabled(pcSlice->getSPS()->getUseALF() ? (cAPS.getAlfParam()->alf_flag ==1):(false));
1672#endif
1673  cAPS.setLoopFilterOffsetInAPS(m_pcCfg->getLoopFilterOffsetInAPS());
1674  cAPS.setLoopFilterDisable(m_pcCfg->getLoopFilterDisable());
1675  cAPS.setLoopFilterBetaOffset(m_pcCfg->getLoopFilterBetaOffset());
1676  cAPS.setLoopFilterTcOffset(m_pcCfg->getLoopFilterTcOffset());
1677
1678  //assign new APS into APS container
1679  Int apsBufSize= (Int)vAPS.size();
1680
1681  if(apsID >= apsBufSize)
1682  {
1683    vAPS.resize(apsID +1);
1684  }
1685
1686  freeAPS(&(vAPS[apsID]), pcSlice->getSPS());
1687  vAPS[apsID] = cAPS;
1688}
1689
1690
1691/** encode APS syntax elements
1692  * \param [in] pcAPS APS pointer
1693  * \param [in, out] APSbs bitstream
1694  * \param [in] pointer to slice (just used for entropy coder initialization)
1695  */
1696Void TEncGOP::encodeAPS(TComAPS* pcAPS, TComOutputBitstream& APSbs, TComSlice* pcSlice)
1697{
1698  m_pcEntropyCoder->setEntropyCoder   ( m_pcCavlcCoder, pcSlice);
1699  m_pcEntropyCoder->resetEntropy      ();
1700  m_pcEntropyCoder->setBitstream(&APSbs);
1701
1702  m_pcEntropyCoder->encodeAPSInitInfo(pcAPS);
1703  if(pcAPS->getScalingListEnabled())
1704  {
1705    m_pcEntropyCoder->encodeScalingList( pcSlice->getScalingList() );
1706  }
1707  if(pcAPS->getLoopFilterOffsetInAPS())
1708  {
1709    m_pcEntropyCoder->encodeDFParams(pcAPS);
1710  }
1711#if SAO_UNIT_INTERLEAVING
1712  m_pcEntropyCoder->encodeSaoParam(pcAPS);
1713#else
1714  if(pcAPS->getSaoEnabled())
1715  {
1716    m_pcEntropyCoder->encodeSaoParam(pcAPS->getSaoParam());
1717  }
1718#endif
1719#if LCU_SYNTAX_ALF
1720  m_pcEntropyCoder->encodeAPSAlfFlag( pcAPS->getAlfEnabled()?1:0);
1721#endif
1722  if(pcAPS->getAlfEnabled())
1723  {
1724    m_pcEntropyCoder->encodeAlfParam(pcAPS->getAlfParam());
1725  }
1726
1727  m_pcEntropyCoder->encodeApsExtensionFlag();
1728  //neither SAO and ALF is enabled
1729  writeRBSPTrailingBits(APSbs);
1730}
1731
1732Void TEncGOP::preLoopFilterPicAll( TComPic* pcPic, UInt64& ruiDist, UInt64& ruiBits )
1733{
1734  TComSlice* pcSlice = pcPic->getSlice(pcPic->getCurrSliceIdx());
1735  Bool bCalcDist = false;
1736#if DBL_CONTROL
1737  m_pcLoopFilter->setCfg(pcSlice->getPPS()->getDeblockingFilterControlPresent(), pcSlice->getLoopFilterDisable(), m_pcCfg->getLoopFilterBetaOffset(), m_pcCfg->getLoopFilterTcOffset(), m_pcCfg->getLFCrossTileBoundaryFlag());
1738#else
1739  m_pcLoopFilter->setCfg(pcSlice->getLoopFilterDisable(), m_pcCfg->getLoopFilterBetaOffset(), m_pcCfg->getLoopFilterTcOffset(), m_pcCfg->getLFCrossTileBoundaryFlag());
1740#endif
1741  m_pcLoopFilter->loopFilterPic( pcPic );
1742 
1743  m_pcEntropyCoder->setEntropyCoder ( m_pcEncTop->getRDGoOnSbacCoder(), pcSlice );
1744  m_pcEntropyCoder->resetEntropy    ();
1745  m_pcEntropyCoder->setBitstream    ( m_pcBitCounter );
1746  pcSlice = pcPic->getSlice(0);
1747  if(pcSlice->getSPS()->getUseSAO() || pcSlice->getSPS()->getUseALF())
1748  {
1749    pcPic->createNonDBFilterInfo();
1750  }
1751 
1752  // Adaptive Loop filter
1753  if( pcSlice->getSPS()->getUseALF() )
1754  {
1755    m_pcAdaptiveLoopFilter->createPicAlfInfo(pcPic);
1756
1757#if LCU_SYNTAX_ALF
1758    AlfParamSet* alfParamSet;
1759    std::vector<AlfCUCtrlInfo>* alfCUCtrlParam = NULL;
1760    alfParamSet= new AlfParamSet;
1761    alfParamSet->create( m_pcAdaptiveLoopFilter->getNumLCUInPicWidth(), m_pcAdaptiveLoopFilter->getNumLCUInPicHeight(), m_pcAdaptiveLoopFilter->getNumCUsInPic());
1762    alfParamSet->createALFParam();
1763    m_pcAdaptiveLoopFilter->initALFEnc(false, true, 1, alfParamSet, alfCUCtrlParam);
1764#else
1765    ALFParam cAlfParam;
1766    m_pcAdaptiveLoopFilter->allocALFParam(&cAlfParam);
1767#endif   
1768    m_pcAdaptiveLoopFilter->startALFEnc(pcPic, m_pcEntropyCoder);
1769   
1770
1771#if LCU_SYNTAX_ALF
1772
1773#if ALF_CHROMA_LAMBDA
1774#if HHI_INTERVIEW_SKIP
1775    m_pcAdaptiveLoopFilter->ALFProcess(alfParamSet, NULL, pcPic->getSlice(0)->getLambdaLuma(), pcPic->getSlice(0)->getLambdaChroma(), m_pcEncTop->getInterViewSkip()  );
1776#else
1777    m_pcAdaptiveLoopFilter->ALFProcess(alfParamSet, NULL, pcPic->getSlice(0)->getLambdaLuma(), pcPic->getSlice(0)->getLambdaChroma() );
1778#endif
1779#else
1780#if SAO_CHROMA_LAMBDA
1781    m_pcAdaptiveLoopFilter->ALFProcess(alfParamSet, NULL, pcPic->getSlice(0)->getLambdaLuma(), m_pcEncTop->getInterViewSkip());
1782#if HHI_INTERVIEW_SKIP
1783#else
1784    m_pcAdaptiveLoopFilter->ALFProcess(alfParamSet, NULL, pcPic->getSlice(0)->getLambdaLuma());
1785#endif
1786#else
1787#if HHI_INTERVIEW_SKIP
1788    m_pcAdaptiveLoopFilter->ALFProcess(alfParamSet, NULL, pcPic->getSlice(0)->getLambda(), m_pcEncTop->getInterViewSkip());
1789#else
1790    m_pcAdaptiveLoopFilter->ALFProcess(alfParamSet, NULL, pcPic->getSlice(0)->getLambda());
1791#endif
1792#endif
1793#endif
1794
1795#else
1796#if ALF_CHROMA_LAMBDA 
1797    m_pcAdaptiveLoopFilter->ALFProcess(&cAlfParam, NULL, pcSlice->getLambdaLuma(), pcSlice->getLambdaChroma(), ruiDist, ruiBits, m_pcEncTop->getInterViewSkip());
1798#if HHI_INTERVIEW_SKIP
1799#else
1800    m_pcAdaptiveLoopFilter->ALFProcess(&cAlfParam, NULL, pcSlice->getLambdaLuma(), pcSlice->getLambdaChroma(), ruiDist, ruiBits);
1801#endif
1802#else
1803#if SAO_CHROMA_LAMBDA
1804#if HHI_INTERVIEW_SKIP
1805    m_pcAdaptiveLoopFilter->ALFProcess(&cAlfParam, NULL, pcSlice->getLambdaLuma(), ruiDist, ruiBits, m_pcEncTop->getInterViewSkip());
1806#else
1807    m_pcAdaptiveLoopFilter->ALFProcess(&cAlfParam, NULL, pcSlice->getLambdaLuma(), ruiDist, ruiBits);
1808#endif
1809#else
1810#if HHI_INTERVIEW_SKIP
1811    m_pcAdaptiveLoopFilter->ALFProcess(&cAlfParam, NULL, pcSlice->getLambda(), ruiDist, ruiBits, m_pcEncTop->getInterViewSkip());
1812#else
1813    m_pcAdaptiveLoopFilter->ALFProcess(&cAlfParam, NULL, pcSlice->getLambda(), ruiDist, ruiBits);
1814#endif
1815#endif
1816
1817#endif
1818#endif
1819    m_pcAdaptiveLoopFilter->endALFEnc();
1820
1821#if LCU_SYNTAX_ALF
1822    alfParamSet->releaseALFParam();
1823    delete alfParamSet;
1824    delete alfCUCtrlParam;
1825#else
1826    m_pcAdaptiveLoopFilter->freeALFParam(&cAlfParam);
1827#endif
1828    m_pcAdaptiveLoopFilter->PCMLFDisableProcess(pcPic);
1829    m_pcAdaptiveLoopFilter->destroyPicAlfInfo();
1830  }
1831  if( pcSlice->getSPS()->getUseSAO() || pcSlice->getSPS()->getUseALF())
1832  {
1833    pcPic->destroyNonDBFilterInfo();
1834  }
1835 
1836  m_pcEntropyCoder->resetEntropy    ();
1837  ruiBits += m_pcEntropyCoder->getNumberOfWrittenBits();
1838 
1839  if (!bCalcDist)
1840    ruiDist = xFindDistortionFrame(pcPic->getPicYuvOrg(), pcPic->getPicYuvRec());
1841}
1842
1843// ====================================================================================================================
1844// Protected member functions
1845// ====================================================================================================================
1846
1847Void TEncGOP::xInitGOP( Int iPOCLast, Int iNumPicRcvd, TComList<TComPic*>& rcListPic, TComList<TComPicYuv*>& rcListPicYuvRecOut )
1848{
1849  assert( iNumPicRcvd > 0 );
1850  //  Exception for the first frame
1851  if ( iPOCLast == 0 )
1852  {
1853    m_iGopSize    = 1;
1854  }
1855  else
1856    m_iGopSize    = m_pcCfg->getGOPSize();
1857 
1858  assert (m_iGopSize > 0); 
1859
1860  return;
1861}
1862
1863Void TEncGOP::xGetBuffer( TComList<TComPic*>&       rcListPic,
1864                         TComList<TComPicYuv*>&    rcListPicYuvRecOut,
1865                         Int                       iNumPicRcvd,
1866                         Int                       iTimeOffset,
1867                         TComPic*&                 rpcPic,
1868                         TComPicYuv*&              rpcPicYuvRecOut,
1869                         UInt                      uiPOCCurr )
1870{
1871  Int i;
1872  //  Rec. output
1873  TComList<TComPicYuv*>::iterator     iterPicYuvRec = rcListPicYuvRecOut.end();
1874  for ( i = 0; i < iNumPicRcvd - iTimeOffset + 1; i++ )
1875  {
1876    iterPicYuvRec--;
1877  }
1878 
1879  rpcPicYuvRecOut = *(iterPicYuvRec);
1880 
1881  //  Current pic.
1882  TComList<TComPic*>::iterator        iterPic       = rcListPic.begin();
1883  while (iterPic != rcListPic.end())
1884  {
1885    rpcPic = *(iterPic);
1886    rpcPic->setCurrSliceIdx(0);
1887    if (rpcPic->getPOC() == (Int)uiPOCCurr)
1888    {
1889      break;
1890    }
1891    iterPic++;
1892  }
1893 
1894  assert (rpcPic->getPOC() == (Int)uiPOCCurr);
1895 
1896  return;
1897}
1898
1899UInt64 TEncGOP::xFindDistortionFrame (TComPicYuv* pcPic0, TComPicYuv* pcPic1)
1900{
1901  Int     x, y;
1902  Pel*  pSrc0   = pcPic0 ->getLumaAddr();
1903  Pel*  pSrc1   = pcPic1 ->getLumaAddr();
1904#if IBDI_DISTORTION
1905  Int  iShift = g_uiBitIncrement;
1906  Int  iOffset = 1<<(g_uiBitIncrement-1);
1907#else
1908  UInt  uiShift = g_uiBitIncrement<<1;
1909#endif
1910  Int   iTemp;
1911 
1912  Int   iStride = pcPic0->getStride();
1913  Int   iWidth  = pcPic0->getWidth();
1914  Int   iHeight = pcPic0->getHeight();
1915 
1916  UInt64  uiTotalDiff = 0;
1917 
1918  for( y = 0; y < iHeight; y++ )
1919  {
1920    for( x = 0; x < iWidth; x++ )
1921    {
1922#if IBDI_DISTORTION
1923      iTemp = ((pSrc0[x]+iOffset)>>iShift) - ((pSrc1[x]+iOffset)>>iShift); uiTotalDiff += iTemp * iTemp;
1924#else
1925      iTemp = pSrc0[x] - pSrc1[x]; uiTotalDiff += (iTemp*iTemp) >> uiShift;
1926#endif
1927    }
1928    pSrc0 += iStride;
1929    pSrc1 += iStride;
1930  }
1931 
1932  iHeight >>= 1;
1933  iWidth  >>= 1;
1934  iStride >>= 1;
1935 
1936  pSrc0  = pcPic0->getCbAddr();
1937  pSrc1  = pcPic1->getCbAddr();
1938 
1939  for( y = 0; y < iHeight; y++ )
1940  {
1941    for( x = 0; x < iWidth; x++ )
1942    {
1943#if IBDI_DISTORTION
1944      iTemp = ((pSrc0[x]+iOffset)>>iShift) - ((pSrc1[x]+iOffset)>>iShift); uiTotalDiff += iTemp * iTemp;
1945#else
1946      iTemp = pSrc0[x] - pSrc1[x]; uiTotalDiff += (iTemp*iTemp) >> uiShift;
1947#endif
1948    }
1949    pSrc0 += iStride;
1950    pSrc1 += iStride;
1951  }
1952 
1953  pSrc0  = pcPic0->getCrAddr();
1954  pSrc1  = pcPic1->getCrAddr();
1955 
1956  for( y = 0; y < iHeight; y++ )
1957  {
1958    for( x = 0; x < iWidth; x++ )
1959    {
1960#if IBDI_DISTORTION
1961      iTemp = ((pSrc0[x]+iOffset)>>iShift) - ((pSrc1[x]+iOffset)>>iShift); uiTotalDiff += iTemp * iTemp;
1962#else
1963      iTemp = pSrc0[x] - pSrc1[x]; uiTotalDiff += (iTemp*iTemp) >> uiShift;
1964#endif
1965    }
1966    pSrc0 += iStride;
1967    pSrc1 += iStride;
1968  }
1969 
1970  return uiTotalDiff;
1971}
1972
1973#if VERBOSE_RATE
1974static const char* nalUnitTypeToString(NalUnitType type)
1975{
1976  switch (type)
1977  {
1978  case NAL_UNIT_CODED_SLICE: return "SLICE";
1979#if H0566_TLA
1980  case NAL_UNIT_CODED_SLICE_IDV: return "IDV";
1981  case NAL_UNIT_CODED_SLICE_CRA: return "CRA";
1982  case NAL_UNIT_CODED_SLICE_TLA: return "TLA";
1983#else
1984  case NAL_UNIT_CODED_SLICE_CDR: return "CDR";
1985#endif
1986  case NAL_UNIT_CODED_SLICE_IDR: return "IDR";
1987  case NAL_UNIT_SEI: return "SEI";
1988  case NAL_UNIT_SPS: return "SPS";
1989  case NAL_UNIT_PPS: return "PPS";
1990  case NAL_UNIT_FILLER_DATA: return "FILLER";
1991  default: return "UNK";
1992  }
1993}
1994#endif
1995
1996Void TEncGOP::xCalculateAddPSNR( TComPic* pcPic, TComPicYuv* pcPicD, const AccessUnit& accessUnit, Double dEncTime )
1997{
1998  Int     x, y;
1999  UInt64 uiSSDY  = 0;
2000  UInt64 uiSSDU  = 0;
2001  UInt64 uiSSDV  = 0;
2002 
2003  Double  dYPSNR  = 0.0;
2004  Double  dUPSNR  = 0.0;
2005  Double  dVPSNR  = 0.0;
2006 
2007  //===== calculate PSNR =====
2008  Pel*  pOrg    = pcPic ->getPicYuvOrg()->getLumaAddr();
2009  Pel*  pRec    = pcPicD->getLumaAddr();
2010  Int   iStride = pcPicD->getStride();
2011 
2012  Int   iWidth;
2013  Int   iHeight;
2014 
2015  iWidth  = pcPicD->getWidth () - m_pcEncTop->getPad(0);
2016  iHeight = pcPicD->getHeight() - m_pcEncTop->getPad(1);
2017 
2018  Int   iSize   = iWidth*iHeight;
2019 
2020  for( y = 0; y < iHeight; y++ )
2021  {
2022    for( x = 0; x < iWidth; x++ )
2023    {
2024      Int iDiff = (Int)( pOrg[x] - pRec[x] );
2025      uiSSDY   += iDiff * iDiff;
2026    }
2027    pOrg += iStride;
2028    pRec += iStride;
2029  }
2030 
2031#if HHI_VSO
2032#if HHI_VSO_SYNTH_DIST_OUT
2033  if ( m_pcRdCost->getUseRenModel() )
2034  {
2035    unsigned int maxval = 255 * (1<<(g_uiBitDepth + g_uiBitIncrement -8));
2036    Double fRefValueY = (double) maxval * maxval * iSize;
2037    Double fRefValueC = fRefValueY / 4.0;
2038    TRenModel*  pcRenModel = m_pcEncTop->getEncTop()->getRenModel();
2039    Int64 iDistVSOY, iDistVSOU, iDistVSOV;
2040    pcRenModel->getTotalSSE( iDistVSOY, iDistVSOU, iDistVSOV );
2041    dYPSNR = ( iDistVSOY ? 10.0 * log10( fRefValueY / (Double) iDistVSOY ) : 99.99 );
2042    dUPSNR = ( iDistVSOU ? 10.0 * log10( fRefValueC / (Double) iDistVSOU ) : 99.99 );
2043    dVPSNR = ( iDistVSOV ? 10.0 * log10( fRefValueC / (Double) iDistVSOV ) : 99.99 );
2044  }
2045  else
2046#endif
2047#endif
2048  {
2049  iHeight >>= 1;
2050  iWidth  >>= 1;
2051  iStride >>= 1;
2052  pOrg  = pcPic ->getPicYuvOrg()->getCbAddr();
2053  pRec  = pcPicD->getCbAddr();
2054 
2055  for( y = 0; y < iHeight; y++ )
2056  {
2057    for( x = 0; x < iWidth; x++ )
2058    {
2059      Int iDiff = (Int)( pOrg[x] - pRec[x] );
2060      uiSSDU   += iDiff * iDiff;
2061    }
2062    pOrg += iStride;
2063    pRec += iStride;
2064  }
2065 
2066  pOrg  = pcPic ->getPicYuvOrg()->getCrAddr();
2067  pRec  = pcPicD->getCrAddr();
2068 
2069  for( y = 0; y < iHeight; y++ )
2070  {
2071    for( x = 0; x < iWidth; x++ )
2072    {
2073      Int iDiff = (Int)( pOrg[x] - pRec[x] );
2074      uiSSDV   += iDiff * iDiff;
2075    }
2076    pOrg += iStride;
2077    pRec += iStride;
2078  }
2079 
2080  unsigned int maxval = 255 * (1<<(g_uiBitDepth + g_uiBitIncrement -8));
2081  Double fRefValueY = (double) maxval * maxval * iSize;
2082  Double fRefValueC = fRefValueY / 4.0;
2083  dYPSNR            = ( uiSSDY ? 10.0 * log10( fRefValueY / (Double)uiSSDY ) : 99.99 );
2084  dUPSNR            = ( uiSSDU ? 10.0 * log10( fRefValueC / (Double)uiSSDU ) : 99.99 );
2085  dVPSNR            = ( uiSSDV ? 10.0 * log10( fRefValueC / (Double)uiSSDV ) : 99.99 );
2086  }
2087  /* calculate the size of the access unit, excluding:
2088   *  - any AnnexB contributions (start_code_prefix, zero_byte, etc.,)
2089   *  - SEI NAL units
2090   */
2091  unsigned numRBSPBytes = 0;
2092  for (AccessUnit::const_iterator it = accessUnit.begin(); it != accessUnit.end(); it++)
2093  {
2094    unsigned numRBSPBytes_nal = unsigned((*it)->m_nalUnitData.str().size());
2095#if VERBOSE_RATE
2096    printf("*** %6s numBytesInNALunit: %u\n", nalUnitTypeToString((*it)->m_nalUnitType), numRBSPBytes_nal);
2097#endif
2098    if ((*it)->m_nalUnitType != NAL_UNIT_SEI)
2099      numRBSPBytes += numRBSPBytes_nal;
2100  }
2101
2102  unsigned uibits = numRBSPBytes * 8;
2103  m_vRVM_RP.push_back( uibits );
2104
2105  //===== add PSNR =====
2106  m_pcEncTop->getAnalyzeAll()->addResult (dYPSNR, dUPSNR, dVPSNR, (Double)uibits);
2107  TComSlice*  pcSlice = pcPic->getSlice(0);
2108  if (pcSlice->isIntra())
2109  {
2110    m_pcEncTop->getAnalyzeI()->addResult (dYPSNR, dUPSNR, dVPSNR, (Double)uibits);
2111  }
2112  if (pcSlice->isInterP())
2113  {
2114    m_pcEncTop->getAnalyzeP()->addResult (dYPSNR, dUPSNR, dVPSNR, (Double)uibits);
2115  }
2116  if (pcSlice->isInterB())
2117  {
2118    m_pcEncTop->getAnalyzeB()->addResult (dYPSNR, dUPSNR, dVPSNR, (Double)uibits);
2119  }
2120
2121  Char c = (pcSlice->isIntra() ? 'I' : pcSlice->isInterP() ? 'P' : 'B');
2122  if (!pcSlice->isReferenced()) c += 32;
2123
2124#if ADAPTIVE_QP_SELECTION
2125  printf("%s   View %3d POC %4d TId: %1d ( %c-SLICE, nQP %d QP %d ) %10d bits",
2126         pcSlice->getIsDepth() ? "Depth  " : "Texture",
2127         pcSlice->getViewId(),
2128         pcSlice->getPOC(),
2129         pcSlice->getTLayer(),
2130         c,
2131         pcSlice->getSliceQpBase(),
2132         pcSlice->getSliceQp(),
2133         uibits );
2134#else
2135  printf("%s   View %3d POC %4d TId: %1d ( %c-SLICE, QP %d ) %10d bits",
2136         pcSlice->getIsDepth() ? "Depth  " : "Texture",
2137         pcSlice->getViewId(),
2138         pcSlice->getPOC()-pcSlice->getLastIDR(),
2139         pcSlice->getTLayer(),
2140         c,
2141         pcSlice->getSliceQp(),
2142         uibits );
2143#endif
2144
2145  printf(" [Y %6.4lf dB    U %6.4lf dB    V %6.4lf dB]", dYPSNR, dUPSNR, dVPSNR );
2146  printf(" [ET %5.0f ]", dEncTime );
2147 
2148  for (Int iRefList = 0; iRefList < 2; iRefList++)
2149  {
2150    printf(" [L%d ", iRefList);
2151    for (Int iRefIndex = 0; iRefIndex < pcSlice->getNumRefIdx(RefPicList(iRefList)); iRefIndex++)
2152    {
2153      if( pcSlice->getViewId() != pcSlice->getRefViewId( RefPicList(iRefList), iRefIndex ) )
2154      {
2155        printf( "V%d ", pcSlice->getRefViewId( RefPicList(iRefList), iRefIndex ) );
2156      }
2157      else
2158      {
2159        printf ("%d ", pcSlice->getRefPOC(RefPicList(iRefList), iRefIndex)-pcSlice->getLastIDR());
2160      }
2161    }
2162    printf("]");
2163  }
2164  if(pcSlice->getNumRefIdx(REF_PIC_LIST_C)>0 && !pcSlice->getNoBackPredFlag())
2165  {
2166    printf(" [LC ");
2167    for (Int iRefIndex = 0; iRefIndex < pcSlice->getNumRefIdx(REF_PIC_LIST_C); iRefIndex++)
2168    {
2169      if( pcSlice->getViewId() != pcSlice->getRefViewId( (RefPicList)pcSlice->getListIdFromIdxOfLC(iRefIndex), pcSlice->getRefIdxFromIdxOfLC(iRefIndex) ) )
2170      {
2171        printf( "V%d ", pcSlice->getRefViewId( (RefPicList)pcSlice->getListIdFromIdxOfLC(iRefIndex), pcSlice->getRefIdxFromIdxOfLC(iRefIndex) ) );
2172      }
2173      else
2174      {
2175        printf ("%d ", pcSlice->getRefPOC((RefPicList)pcSlice->getListIdFromIdxOfLC(iRefIndex), pcSlice->getRefIdxFromIdxOfLC(iRefIndex))-pcSlice->getLastIDR());
2176      }
2177    }
2178    printf("]");
2179  }
2180}
2181
2182/** Function for deciding the nal_unit_type.
2183 * \param uiPOCCurr POC of the current picture
2184 * \returns the nal_unit type of the picture
2185 * This function checks the configuration and returns the appropriate nal_unit_type for the picture.
2186 */
2187NalUnitType TEncGOP::getNalUnitType(UInt uiPOCCurr)
2188{
2189  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') );
2190
2191  if (uiPOCCurr == 0)
2192  {
2193    if( bInterViewOnlySlice ) 
2194    { 
2195      return NAL_UNIT_CODED_SLICE_IDV; 
2196    }
2197    else
2198    { 
2199      return NAL_UNIT_CODED_SLICE_IDR;
2200    }
2201  }
2202  if (uiPOCCurr % m_pcCfg->getIntraPeriod() == 0)
2203  {
2204    if (m_pcCfg->getDecodingRefreshType() == 1)
2205    {
2206      if( bInterViewOnlySlice ) 
2207      { 
2208        return NAL_UNIT_CODED_SLICE_IDV; 
2209      }
2210      else
2211      { 
2212#if H0566_TLA
2213      return NAL_UNIT_CODED_SLICE_CRA;
2214#else
2215      return NAL_UNIT_CODED_SLICE_CDR;
2216#endif
2217      }
2218    }
2219    else if (m_pcCfg->getDecodingRefreshType() == 2)
2220    {
2221      if( bInterViewOnlySlice ) 
2222      { 
2223        return NAL_UNIT_CODED_SLICE_IDV; 
2224      }
2225      else
2226      { 
2227        return NAL_UNIT_CODED_SLICE_IDR;
2228      }
2229    }
2230  }
2231  return NAL_UNIT_CODED_SLICE;
2232}
2233
2234NalUnitType TEncGOP::getNalUnitTypeBaseViewMvc(UInt uiPOCCurr)
2235{
2236  if( uiPOCCurr == 0 )
2237  {
2238    return NAL_UNIT_CODED_SLICE_IDR;
2239  }
2240  if( uiPOCCurr % m_pcCfg->getIntraPeriod() == 0 )
2241  {
2242    if( m_pcCfg->getDecodingRefreshType() == 1 )
2243    {
2244#if H0566_TLA
2245      return NAL_UNIT_CODED_SLICE_CRA;
2246#else
2247      return NAL_UNIT_CODED_SLICE_CDR;
2248#endif
2249    }
2250    else if( m_pcCfg->getDecodingRefreshType() == 2 )
2251    {
2252      return NAL_UNIT_CODED_SLICE_IDR;
2253    }
2254  }
2255  return NAL_UNIT_CODED_SLICE;
2256}
2257
2258Double TEncGOP::xCalculateRVM()
2259{
2260  Double dRVM = 0;
2261 
2262  if( m_pcCfg->getGOPSize() == 1 && m_pcCfg->getIntraPeriod() != 1 && m_pcCfg->getFrameToBeEncoded() > RVM_VCEGAM10_M * 2 )
2263  {
2264    // calculate RVM only for lowdelay configurations
2265    std::vector<Double> vRL , vB;
2266    size_t N = m_vRVM_RP.size();
2267    vRL.resize( N );
2268    vB.resize( N );
2269   
2270    Int i;
2271    Double dRavg = 0 , dBavg = 0;
2272    vB[RVM_VCEGAM10_M] = 0;
2273    for( i = RVM_VCEGAM10_M + 1 ; i < N - RVM_VCEGAM10_M + 1 ; i++ )
2274    {
2275      vRL[i] = 0;
2276      for( Int j = i - RVM_VCEGAM10_M ; j <= i + RVM_VCEGAM10_M - 1 ; j++ )
2277        vRL[i] += m_vRVM_RP[j];
2278      vRL[i] /= ( 2 * RVM_VCEGAM10_M );
2279      vB[i] = vB[i-1] + m_vRVM_RP[i] - vRL[i];
2280      dRavg += m_vRVM_RP[i];
2281      dBavg += vB[i];
2282    }
2283   
2284    dRavg /= ( N - 2 * RVM_VCEGAM10_M );
2285    dBavg /= ( N - 2 * RVM_VCEGAM10_M );
2286   
2287    double dSigamB = 0;
2288    for( i = RVM_VCEGAM10_M + 1 ; i < N - RVM_VCEGAM10_M + 1 ; i++ )
2289    {
2290      Double tmp = vB[i] - dBavg;
2291      dSigamB += tmp * tmp;
2292    }
2293    dSigamB = sqrt( dSigamB / ( N - 2 * RVM_VCEGAM10_M ) );
2294   
2295    double f = sqrt( 12.0 * ( RVM_VCEGAM10_M - 1 ) / ( RVM_VCEGAM10_M + 1 ) );
2296   
2297    dRVM = dSigamB / dRavg * f;
2298  }
2299 
2300  return( dRVM );
2301}
2302
2303/** Determine the difference between consecutive tile sizes (in bytes) and writes it to  bistream rNalu [slice header]
2304 * \param rpcBitstreamRedirect contains the bitstream to be concatenated to rNalu. rpcBitstreamRedirect contains slice payload. rpcSlice contains tile location information.
2305 * \returns Updates rNalu to contain concatenated bitstream. rpcBitstreamRedirect is cleared at the end of this function call.
2306 */
2307Void TEncGOP::xWriteTileLocationToSliceHeader (OutputNALUnit& rNalu, TComOutputBitstream*& rpcBitstreamRedirect, TComSlice*& rpcSlice)
2308{
2309  {
2310#if !TILES_WPP_ENTRY_POINT_SIGNALLING
2311    Int iTransmitTileLocationInSliceHeader = (rpcSlice->getTileLocationCount()==0 || m_pcCfg->getTileLocationInSliceHeaderFlag()==0) ? 0 : 1;
2312    rNalu.m_Bitstream.write(iTransmitTileLocationInSliceHeader, 1);   // write flag indicating whether tile location information communicated in slice header
2313
2314    if (iTransmitTileLocationInSliceHeader)
2315    {
2316      rNalu.m_Bitstream.write(rpcSlice->getTileLocationCount()-1, 5);   // write number of tiles
2317
2318      Int *aiDiff;
2319      aiDiff = new Int [rpcSlice->getTileLocationCount()];
2320
2321      // Find largest number of bits required by Diff
2322      Int iLastSize = 0, iDiffMax = 0, iDiffMin = 0;
2323      for (UInt uiIdx=0; uiIdx<rpcSlice->getTileLocationCount(); uiIdx++)
2324      {
2325        Int iCurDiff, iCurSize;
2326        if (uiIdx==0)
2327        {
2328          iCurDiff  = rpcSlice->getTileLocation( uiIdx );
2329          iLastSize = rpcSlice->getTileLocation( uiIdx );
2330        }
2331        else
2332        {
2333          iCurSize  = rpcSlice->getTileLocation( uiIdx )  - rpcSlice->getTileLocation( uiIdx-1 );
2334          iCurDiff  = iCurSize - iLastSize;
2335          iLastSize = iCurSize;
2336        }
2337        // Store Diff so it may be written to slice header later without re-calculating.
2338        aiDiff[uiIdx] = iCurDiff;
2339
2340        if (iCurDiff>iDiffMax)
2341        {
2342          iDiffMax = iCurDiff;
2343        }
2344        if (iCurDiff<iDiffMin)
2345        {
2346          iDiffMin = iCurDiff;
2347        }
2348      }
2349
2350      Int iDiffMinAbs, iDiffMaxAbs;
2351      iDiffMinAbs = (iDiffMin<0) ? (-iDiffMin) : iDiffMin;
2352      iDiffMaxAbs = (iDiffMax<0) ? (-iDiffMax) : iDiffMax;
2353
2354      Int iBitsUsedByDiff = 0, iDiffAbsLargest;
2355      iDiffAbsLargest = (iDiffMinAbs < iDiffMaxAbs) ? iDiffMaxAbs : iDiffMinAbs;       
2356      while (1)
2357      {
2358        if (iDiffAbsLargest >= (1 << iBitsUsedByDiff) )
2359        {
2360          iBitsUsedByDiff++;
2361        }
2362        else
2363        {
2364          break;
2365        }
2366      }
2367      iBitsUsedByDiff++;
2368
2369      if (iBitsUsedByDiff > 32)
2370      {
2371        printf("\nDiff magnitude uses more than 32-bits");
2372        assert ( 0 );
2373        exit ( 0 ); // trying to catch any problems with using fixed bits for Diff information
2374      }
2375
2376      rNalu.m_Bitstream.write( iBitsUsedByDiff-1, 5 ); // write number of bits used by Diff
2377
2378      // Write diff to slice header (rNalu)
2379      for (UInt uiIdx=0; uiIdx<rpcSlice->getTileLocationCount(); uiIdx++)
2380      {
2381        Int iCurDiff = aiDiff[uiIdx];
2382
2383        // write sign of diff
2384        if (uiIdx!=0)
2385        {
2386          if (iCurDiff<0)         
2387          {
2388            rNalu.m_Bitstream.write(1, 1);
2389          }
2390          else
2391          {
2392            rNalu.m_Bitstream.write(0, 1);
2393          }
2394        }
2395
2396        // write abs value of diff
2397        Int iAbsDiff = (iCurDiff<0) ? (-iCurDiff) : iCurDiff;
2398        if (iAbsDiff > ((((UInt64)1)<<32)-1))
2399        {
2400          printf("\niAbsDiff exceeds 32-bit limit");
2401          exit(0);
2402        }
2403        rNalu.m_Bitstream.write( iAbsDiff, iBitsUsedByDiff-1 ); 
2404      }
2405
2406      delete [] aiDiff;
2407    }
2408#endif
2409  }
2410
2411  // Byte-align
2412  rNalu.m_Bitstream.writeAlignOne();
2413
2414  // Update tile marker locations
2415  TComOutputBitstream *pcOut = &rNalu.m_Bitstream;
2416  UInt uiAccumulatedLength   = pcOut->getNumberOfWrittenBits() >> 3;
2417  for (Int uiMrkIdx = 0; uiMrkIdx < rpcBitstreamRedirect->getTileMarkerLocationCount(); uiMrkIdx++)
2418  {
2419    UInt uiBottom = pcOut->getTileMarkerLocationCount();
2420    pcOut->setTileMarkerLocation      ( uiBottom, uiAccumulatedLength + rpcBitstreamRedirect->getTileMarkerLocation( uiMrkIdx ) );
2421    pcOut->setTileMarkerLocationCount ( uiBottom + 1 );
2422  }
2423
2424  // Perform bitstream concatenation
2425  if (rpcBitstreamRedirect->getNumberOfWrittenBits() > 0)
2426  {
2427    UInt uiBitCount  = rpcBitstreamRedirect->getNumberOfWrittenBits();
2428    if (rpcBitstreamRedirect->getByteStreamLength()>0)
2429    {
2430      UChar *pucStart  =  reinterpret_cast<UChar*>(rpcBitstreamRedirect->getByteStream());
2431      UInt uiWriteByteCount = 0;
2432      while (uiWriteByteCount < (uiBitCount >> 3) )
2433      {
2434        UInt uiBits = (*pucStart);
2435        rNalu.m_Bitstream.write(uiBits, 8);
2436        pucStart++;
2437        uiWriteByteCount++;
2438      }
2439    }
2440    UInt uiBitsHeld = (uiBitCount & 0x07);
2441    for (UInt uiIdx=0; uiIdx < uiBitsHeld; uiIdx++)
2442    {
2443      rNalu.m_Bitstream.write((rpcBitstreamRedirect->getHeldBits() & (1 << (7-uiIdx))) >> (7-uiIdx), 1);
2444    }         
2445  }
2446
2447  m_pcEntropyCoder->setBitstream(&rNalu.m_Bitstream);
2448
2449  delete rpcBitstreamRedirect;
2450  rpcBitstreamRedirect = new TComOutputBitstream;
2451}
2452
2453Void TEncGOP::xSetRefPicListModificationsMvc( TComSlice* pcSlice, UInt uiPOCCurr, UInt iGOPid )
2454{
2455  if( pcSlice->getSliceType() == I_SLICE || !(pcSlice->getSPS()->getListsModificationPresentFlag()) || pcSlice->getSPS()->getNumberOfUsableInterViewRefs() == 0 )
2456  {
2457    return;
2458  }
2459
2460  // analyze inter-view modifications
2461  GOPEntryMvc gem = m_pcCfg->getGOPEntry( (getNalUnitType(uiPOCCurr) == NAL_UNIT_CODED_SLICE_IDV) ? MAX_GOP : iGOPid );
2462  Int numL0Modifications = 0;
2463  Int numL1Modifications = 0;
2464  for( Int k = 0; k < gem.m_numInterViewRefPics; k++ )
2465  {
2466    if( gem.m_interViewRefPosL0[k] > 0 ) { numL0Modifications++; }
2467    if( gem.m_interViewRefPosL1[k] > 0 ) { numL1Modifications++; }
2468  }
2469
2470  TComRefPicListModification* refPicListModification = pcSlice->getRefPicListModification();
2471  Int maxRefListSize = pcSlice->getNumPocTotalCurrMvc();
2472  Int numTemporalRefs = pcSlice->getNumPocTotalCurr();
2473
2474  // set L0 inter-view modifications
2475  if( (maxRefListSize > 1) && (numL0Modifications > 0) )
2476  {
2477    refPicListModification->setRefPicListModificationFlagL0( true );
2478    Int tempListEntryL0[16];
2479    for( Int k = 0; k < 16; k++ ) { tempListEntryL0[k] = -1; }
2480   
2481    Bool hasModification = false;
2482    for( Int k = 0; k < gem.m_numInterViewRefPics; k++ )
2483    {
2484      if( gem.m_interViewRefPosL0[k] > 0 )
2485      {
2486        for( Int l = 0; l < pcSlice->getSPS()->getNumberOfUsableInterViewRefs(); l++ )
2487        {
2488          if( gem.m_interViewRefs[k] == pcSlice->getSPS()->getUsableInterViewRef( l ) && (gem.m_interViewRefPosL0[k] - 1) != (numTemporalRefs + l) )
2489          {
2490            tempListEntryL0[gem.m_interViewRefPosL0[k]-1] = numTemporalRefs + l;
2491            hasModification = true;
2492          }
2493        }
2494      }
2495    }
2496
2497    if( hasModification )
2498    {
2499      Int temporalRefIdx = 0;
2500      for( Int i = 0; i < pcSlice->getNumRefIdx( REF_PIC_LIST_0 ); i++ )
2501      {
2502        if( tempListEntryL0[i] >= 0 ) 
2503        {
2504          refPicListModification->setRefPicSetIdxL0( i, tempListEntryL0[i] );
2505        }
2506        else
2507        {
2508          refPicListModification->setRefPicSetIdxL0( i, temporalRefIdx );
2509          temporalRefIdx++;
2510        }
2511      }
2512    }
2513    else
2514    {
2515      refPicListModification->setRefPicListModificationFlagL0( false );
2516    }
2517  }
2518
2519  // set L1 inter-view modifications
2520  if( (maxRefListSize > 1) && (numL1Modifications > 0) )
2521  {
2522    refPicListModification->setRefPicListModificationFlagL1( true );
2523    Int tempListEntryL1[16];
2524    for( Int k = 0; k < 16; k++ ) { tempListEntryL1[k] = -1; }
2525
2526    Bool hasModification = false;
2527    for( Int k = 0; k < gem.m_numInterViewRefPics; k++ )
2528    {
2529      if( gem.m_interViewRefPosL1[k] > 0 )
2530      {
2531        for( Int l = 0; l < pcSlice->getSPS()->getNumberOfUsableInterViewRefs(); l++ )
2532        {
2533          if( gem.m_interViewRefs[k] == pcSlice->getSPS()->getUsableInterViewRef( l ) && (gem.m_interViewRefPosL1[k] - 1) != (numTemporalRefs + l) )
2534          {
2535            tempListEntryL1[gem.m_interViewRefPosL1[k]-1] = numTemporalRefs + l;
2536            hasModification = true;
2537          }
2538        }
2539      }
2540    }
2541
2542    if( hasModification )
2543    {
2544      Int temporalRefIdx = 0;
2545      for( Int i = 0; i < pcSlice->getNumRefIdx( REF_PIC_LIST_1 ); i++ )
2546      {
2547        if( tempListEntryL1[i] >= 0 ) 
2548        {
2549          refPicListModification->setRefPicSetIdxL1( i, tempListEntryL1[i] );
2550        }
2551        else
2552        {
2553          refPicListModification->setRefPicSetIdxL1( i, temporalRefIdx );
2554          temporalRefIdx++;
2555        }
2556      }
2557    } 
2558    else
2559    {
2560      refPicListModification->setRefPicListModificationFlagL1( false );
2561    }
2562  }
2563
2564  return;
2565}
2566//! \}
Note: See TracBrowser for help on using the repository browser.