source: 3DVCSoftware/trunk/source/Lib/TLibEncoder/TEncGOP.cpp @ 115

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

Integrated revision 114. with:

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