source: 3DVCSoftware/branches/HTM-4.0.1-VSP-dev0/source/Lib/TLibEncoder/TEncGOP.cpp @ 166

Last change on this file since 166 was 166, checked in by mitsubishi-htm, 12 years ago

Initial integration of VSP into HTM 4.0.1. The version used for JCT3V-B0102 at Shanghai meeting.

  • VC9 project/solution files updated. Other Visual C++ project/solution files are not updated.
  • Linux make file updated.

TODO

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