source: 3DVCSoftware/branches/HTM-5.1-dev2-Mediatek/source/Lib/TLibEncoder/TEncGOP.cpp @ 269

Last change on this file since 269 was 254, checked in by mediatek-htm, 12 years ago

Integration of JCT3V-C0137

The added texture merging candidate is controlled by the MACRO "MTK_DEPTH_MERGE_TEXTURE_CANDIDATE_C0137"

From MediaTek
yiwen.chen@…

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