source: 3DVCSoftware/branches/HTM-4.1-dev1-HHI/source/Lib/TLibEncoder/TEncGOP.cpp

Last change on this file was 167, checked in by qualcomm, 12 years ago

JCT3V-B0047

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