source: 3DVCSoftware/branches/HTM-5.0-Nokia/source/Lib/TLibEncoder/TEncGOP.cpp @ 200

Last change on this file since 200 was 200, checked in by nokia, 12 years ago

Reintegration of Flexible Coding Order to 3DV-HTM v5.1
To enable following params are required:
FCO : 1
FCOCodingOrder : T0D0D1D2T1T2
VSOConfig: [ox0 B(cc1) I(s0.25 s0.5 s0.75)][cx1 B(oo0) B(oo2) I(s0.25 s0.5 s0.75 s1.25 s1.5 s1.75)][ox2 B(cc1) I(s1.25 s1.5 s1.75)]

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