source: 3DVCSoftware/branches/HTM-4.0-dev0/source/Lib/TLibEncoder/TEncGOP.cpp @ 134

Last change on this file since 134 was 102, checked in by tech, 12 years ago

SAIT_VSO_EST_A0033 code improvement

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