source: 3DVCSoftware/branches/HTM-5.1-dev3-Mediatek/source/Lib/TLibEncoder/TEncGOP.cpp

Last change on this file was 231, checked in by mitsubishi-htm, 12 years ago

-Integration of JCT3V-C0152 & JCT3V-C0131
-This check-in enable C0131 only

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