source: 3DVCSoftware/branches/HTM-4.1-dev2-HHI/source/Lib/TLibEncoder/TEncGOP.cpp @ 193

Last change on this file since 193 was 120, checked in by tech, 12 years ago

Cleanup of WVSO and fixed cfg-file switch

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