source: 3DVCSoftware/branches/HTM-3.1-MediaTek/source/Lib/TLibEncoder/TEncGOP.cpp @ 141

Last change on this file since 141 was 91, checked in by qualcomm, 12 years ago

M24937 with macro QC_MULTI_DIS_CAN

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