source: 3DVCSoftware/branches/HTM-5.1-dev0-MERL-Mediatek-Fix/source/Lib/TLibEncoder/TEncGOP.cpp

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

Integration of branch dev 2.

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