source: 3DVCSoftware/branches/HTM-15.1-dev0-Vidyo/source/Lib/TLibCommon/TComSlice.cpp

Last change on this file was 1321, checked in by tech, 10 years ago

Merged 15.0-dev0@1320.

  • Property svn:eol-style set to native
File size: 173.2 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-2015, ITU/ISO/IEC
7 * All rights reserved.
8
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are met:
12 *
13 *  * Redistributions of source code must retain the above copyright notice,
14 *    this list of conditions and the following disclaimer.
15 *  * Redistributions in binary form must reproduce the above copyright notice,
16 *    this list of conditions and the following disclaimer in the documentation
17 *    and/or other materials provided with the distribution.
18 *  * Neither the name of the ITU/ISO/IEC nor the names of its contributors may
19 *    be used to endorse or promote products derived from this software without
20 *    specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 * THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35/** \file     TComSlice.cpp
36    \brief    slice header and SPS class
37*/
38
39#include "CommonDef.h"
40#include "TComSlice.h"
41#include "TComPic.h"
42#include "TLibEncoder/TEncSbac.h"
43#include "TLibDecoder/TDecSbac.h"
44
45
46//! \ingroup TLibCommon
47//! \{
48
49TComSlice::TComSlice()
50: m_iPPSId                        ( -1 )
51, m_PicOutputFlag                 ( true )
52#if NH_MV
53, m_slicePicOrderCntLsb           ( 0 )
54#endif
55, m_iPOC                          ( 0 )
56, m_iLastIDR                      ( 0 )
57, m_iAssociatedIRAP               ( 0 )
58, m_iAssociatedIRAPType           ( NAL_UNIT_INVALID )
59, m_pRPS                          ( 0 )
60, m_localRPS                      ( )
61, m_rpsIdx                        ( 0 )
62, m_RefPicListModification        ( )
63, m_eNalUnitType                  ( NAL_UNIT_CODED_SLICE_IDR_W_RADL )
64, m_eSliceType                    ( I_SLICE )
65, m_iSliceQp                      ( 0 )
66, m_dependentSliceSegmentFlag     ( false )
67#if ADAPTIVE_QP_SELECTION
68, m_iSliceQpBase                  ( 0 )
69#endif
70, m_ChromaQpAdjEnabled            ( false )
71, m_deblockingFilterDisable       ( false )
72, m_deblockingFilterOverrideFlag  ( false )
73, m_deblockingFilterBetaOffsetDiv2( 0 )
74, m_deblockingFilterTcOffsetDiv2  ( 0 )
75, m_bCheckLDC                     ( false )
76, m_iSliceQpDelta                 ( 0 )
77, m_iDepth                        ( 0 )
78, m_bRefenced                     ( false )
79, m_pcVPS                         ( NULL )
80, m_pcSPS                         ( NULL )
81, m_pcPPS                         ( NULL )
82, m_pcPic                         ( NULL )
83, m_colFromL0Flag                 ( true )
84, m_noOutputPriorPicsFlag         ( false )
85, m_noRaslOutputFlag              ( false )
86, m_handleCraAsBlaFlag            ( false )
87, m_colRefIdx                     ( 0 )
88, m_maxNumMergeCand               ( 0 )
89, m_uiTLayer                      ( 0 )
90, m_bTLayerSwitchingFlag          ( false )
91, m_sliceMode                     ( NO_SLICES )
92, m_sliceArgument                 ( 0 )
93, m_sliceCurStartCtuTsAddr        ( 0 )
94, m_sliceCurEndCtuTsAddr          ( 0 )
95, m_sliceIdx                      ( 0 )
96, m_sliceSegmentMode              ( NO_SLICES )
97, m_sliceSegmentArgument          ( 0 )
98, m_sliceSegmentCurStartCtuTsAddr ( 0 )
99, m_sliceSegmentCurEndCtuTsAddr   ( 0 )
100, m_nextSlice                     ( false )
101, m_nextSliceSegment              ( false )
102, m_sliceBits                     ( 0 )
103, m_sliceSegmentBits              ( 0 )
104, m_bFinalized                    ( false )
105, m_bTestWeightPred               ( false )
106, m_bTestWeightBiPred             ( false )
107, m_substreamSizes                ( )
108, m_cabacInitFlag                 ( false )
109, m_bLMvdL1Zero                   ( false )
110, m_temporalLayerNonReferenceFlag ( false )
111, m_LFCrossSliceBoundaryFlag      ( false )
112, m_enableTMVPFlag                ( true )
113, m_encCABACTableIdx              (I_SLICE)
114#if NH_MV
115, m_refPicSetInterLayer0          ( NULL )
116, m_refPicSetInterLayer1          ( NULL )
117, m_layerId                       (0)
118, m_viewId                        (0)
119, m_viewIndex                     (0)
120#if NH_3D
121, m_isDepth                       (false)
122#endif
123#if NH_MV
124, m_pocResetFlag                  (false)
125, m_crossLayerBlaFlag             (false)
126#endif
127, m_discardableFlag               (false)
128, m_interLayerPredEnabledFlag     (false)
129, m_numInterLayerRefPicsMinus1    (0)
130#if NH_MV
131, m_sliceSegmentHeaderExtensionLength (0)
132, m_pocResetIdc                   (0)
133, m_pocResetPeriodId              (0)
134, m_hasPocResetPeriodIdPresent    (false)
135, m_fullPocResetFlag              (false)
136, m_pocLsbVal                     (0)
137, m_pocMsbCycleValPresentFlag     (false)
138, m_pocMsbCycleVal                (0)
139, m_pocMsbValRequiredFlag         (false)
140#endif
141#if NH_3D_IC
142, m_bApplyIC                      (false)
143, m_icSkipParseFlag               (false)
144#endif
145#if NH_3D
146, m_inCmpPredFlag                 (false)
147, m_numViews                      (0)
148, m_depthToDisparityB             (NULL)
149, m_depthToDisparityF             (NULL)
150#endif
151#if NH_3D_DIS
152, m_bApplyDIS                     (false)
153#endif
154#endif
155{
156
157#if NH_MV
158  m_shortTermRefPicSetIdx = 0; 
159  m_numLongTermSps        = 0; 
160  m_numLongTermPics       = 0; 
161  for (Int i = 0; i < MAX_NUM_PICS_RPS; i++)
162  {
163    m_ltIdxSps          [i] = 0; 
164    m_deltaPocMsbCycleLt[i] = 0;
165  }
166  setSliceTemporalMvpEnabledFlag( false ); 
167#endif
168  for(UInt i=0; i<NUM_REF_PIC_LIST_01; i++)
169  {
170    m_aiNumRefIdx[i] = 0;
171  }
172
173  for (UInt component = 0; component < MAX_NUM_COMPONENT; component++)
174  {
175    m_lambdas            [component] = 0.0;
176    m_iSliceChromaQpDelta[component] = 0;
177  }
178
179  initEqualRef();
180
181  for ( Int idx = 0; idx < MAX_NUM_REF; idx++ )
182  {
183    m_list1IdxToList0Idx[idx] = -1;
184  }
185
186  for(Int iNumCount = 0; iNumCount < MAX_NUM_REF; iNumCount++)
187  {
188    for(UInt i=0; i<NUM_REF_PIC_LIST_01; i++)
189    {
190      m_apcRefPicList [i][iNumCount] = NULL;
191      m_aiRefPOCList  [i][iNumCount] = 0;
192    }
193#if NH_MV
194    m_aiRefLayerIdList[0][iNumCount] = 0;
195    m_aiRefLayerIdList[1][iNumCount] = 0;
196#endif
197  }
198
199  resetWpScaling();
200  initWpAcDcParam();
201
202  for(Int ch=0; ch < MAX_NUM_CHANNEL_TYPE; ch++)
203  {
204    m_saoEnabledFlag[ch] = false;
205  }
206#if NH_MV
207  for (Int i = 0; i < MAX_NUM_LAYERS; i++ )
208  {
209    m_interLayerPredLayerIdc[ i ] = -1;
210  }
211#endif
212#if NH_3D
213  m_iDefaultRefViewIdx = -1;
214  m_bDefaultRefViewIdxAvailableFlag = false;
215  m_ivMvPredFlag           = false;
216  m_ivMvScalingFlag        = false;
217  m_ivResPredFlag          = false; 
218  m_depthRefinementFlag    = false;
219  m_viewSynthesisPredFlag  = false;
220  m_depthBasedBlkPartFlag  = false;
221  m_mpiFlag                = false;
222  m_intraContourFlag       = false;
223  m_intraSdcWedgeFlag      = false;
224  m_qtPredFlag             = false;
225  m_interSdcFlag           = false;
226  m_depthIntraSkipFlag     = false;
227  m_subPbSize              =  1 << 6;
228  m_mpiSubPbSize           =  1 << 6;
229
230  m_aaiCodedOffset.resize(2);
231  m_aaiCodedScale .resize(2);
232  for (Int i = 0; i < 2; i++)
233  {
234    m_aaiCodedOffset[i].resize(MAX_NUM_LAYERS);
235    m_aaiCodedScale [i].resize(MAX_NUM_LAYERS);
236  }
237 
238#endif
239
240}
241
242TComSlice::~TComSlice()
243{
244#if NH_3D
245  for( UInt i = 0; i < m_numViews; i++ )
246  {
247    if ( m_depthToDisparityB && m_depthToDisparityB[ i ] )
248    {
249      delete[] m_depthToDisparityB [ i ];
250    }
251
252    if ( m_depthToDisparityF && m_depthToDisparityF[ i ] ) 
253    {
254      delete[] m_depthToDisparityF [ i ];
255    }
256  }
257
258  if ( m_depthToDisparityF )
259  {
260    delete[] m_depthToDisparityF; 
261  }
262
263  m_depthToDisparityF = NULL;
264
265  if ( m_depthToDisparityB )
266    delete[] m_depthToDisparityB; 
267
268  m_depthToDisparityB = NULL;
269#endif
270
271}
272
273
274Void TComSlice::initSlice()
275{
276  for(UInt i=0; i<NUM_REF_PIC_LIST_01; i++)
277  {
278    m_aiNumRefIdx[i]      = 0;
279  }
280  m_colFromL0Flag = true;
281
282  m_colRefIdx = 0;
283  initEqualRef();
284
285  m_bCheckLDC = false;
286
287  for (UInt component = 0; component < MAX_NUM_COMPONENT; component++)
288  {
289    m_iSliceChromaQpDelta[component] = 0;
290  }
291#if NH_3D_IV_MERGE
292  m_maxNumMergeCand = MRG_MAX_NUM_CANDS_MEM;
293#else
294  m_maxNumMergeCand = MRG_MAX_NUM_CANDS;
295#endif
296
297  m_bFinalized=false;
298
299  m_substreamSizes.clear();
300  m_cabacInitFlag        = false;
301  m_enableTMVPFlag = true;
302#if NH_3D_TMVP
303  m_aiAlterRefIdx[0]                  = -1;
304  m_aiAlterRefIdx[1]                  = -1;
305#endif
306}
307
308Bool TComSlice::getRapPicFlag() const
309{
310  return getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_W_RADL
311      || getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_N_LP
312      || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP
313      || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL
314      || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP
315      || getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA;
316}
317
318
319Void  TComSlice::sortPicList        (TComList<TComPic*>& rcListPic)
320{
321  TComPic*    pcPicExtract;
322  TComPic*    pcPicInsert;
323
324  TComList<TComPic*>::iterator    iterPicExtract;
325  TComList<TComPic*>::iterator    iterPicExtract_1;
326  TComList<TComPic*>::iterator    iterPicInsert;
327
328  for (Int i = 1; i < (Int)(rcListPic.size()); i++)
329  {
330    iterPicExtract = rcListPic.begin();
331    for (Int j = 0; j < i; j++)
332    {
333      iterPicExtract++;
334    }
335    pcPicExtract = *(iterPicExtract);
336    pcPicExtract->setCurrSliceIdx(0);
337
338    iterPicInsert = rcListPic.begin();
339    while (iterPicInsert != iterPicExtract)
340    {
341      pcPicInsert = *(iterPicInsert);
342      pcPicInsert->setCurrSliceIdx(0);
343      if (pcPicInsert->getPOC() >= pcPicExtract->getPOC())
344      {
345        break;
346      }
347
348      iterPicInsert++;
349    }
350
351    iterPicExtract_1 = iterPicExtract;    iterPicExtract_1++;
352
353    //  swap iterPicExtract and iterPicInsert, iterPicExtract = curr. / iterPicInsert = insertion position
354    rcListPic.insert (iterPicInsert, iterPicExtract, iterPicExtract_1);
355    rcListPic.erase  (iterPicExtract);
356  }
357}
358
359TComPic* TComSlice::xGetRefPic (TComList<TComPic*>& rcListPic, Int poc)
360{
361  TComList<TComPic*>::iterator  iterPic = rcListPic.begin();
362  TComPic*                      pcPic = *(iterPic);
363  while ( iterPic != rcListPic.end() )
364  {
365    if(pcPic->getPOC() == poc)
366    {
367      break;
368    }
369    iterPic++;
370    pcPic = *(iterPic);
371  }
372  return  pcPic;
373}
374
375
376TComPic* TComSlice::xGetLongTermRefPic(TComList<TComPic*>& rcListPic, Int poc, Bool pocHasMsb)
377{
378  TComList<TComPic*>::iterator  iterPic = rcListPic.begin();
379  TComPic*                      pcPic = *(iterPic);
380  TComPic*                      pcStPic = pcPic;
381
382  Int pocCycle = 1 << getSPS()->getBitsForPOC();
383  if (!pocHasMsb)
384  {
385    poc = poc & (pocCycle - 1);
386  }
387
388  while ( iterPic != rcListPic.end() )
389  {
390    pcPic = *(iterPic);
391    if (pcPic && pcPic->getPOC()!=this->getPOC() && pcPic->getSlice( 0 )->isReferenced())
392    {
393      Int picPoc = pcPic->getPOC();
394      if (!pocHasMsb)
395      {
396        picPoc = picPoc & (pocCycle - 1);
397      }
398
399      if (poc == picPoc)
400      {
401        if(pcPic->getIsLongTerm())
402        {
403          return pcPic;
404        }
405        else
406        {
407          pcStPic = pcPic;
408        }
409        break;
410      }
411    }
412
413    iterPic++;
414  }
415
416  return  pcStPic;
417}
418
419Void TComSlice::setRefPOCList       ()
420{
421  for (Int iDir = 0; iDir < NUM_REF_PIC_LIST_01; iDir++)
422  {
423    for (Int iNumRefIdx = 0; iNumRefIdx < m_aiNumRefIdx[iDir]; iNumRefIdx++)
424    {
425      m_aiRefPOCList[iDir][iNumRefIdx] = m_apcRefPicList[iDir][iNumRefIdx]->getPOC();
426#if NH_MV
427      m_aiRefLayerIdList[iDir][iNumRefIdx] = m_apcRefPicList[iDir][iNumRefIdx]->getLayerId();
428#endif
429    }
430  }
431
432}
433
434Void TComSlice::setList1IdxToList0Idx()
435{
436  Int idxL0, idxL1;
437  for ( idxL1 = 0; idxL1 < getNumRefIdx( REF_PIC_LIST_1 ); idxL1++ )
438  {
439    m_list1IdxToList0Idx[idxL1] = -1;
440    for ( idxL0 = 0; idxL0 < getNumRefIdx( REF_PIC_LIST_0 ); idxL0++ )
441    {
442      if ( m_apcRefPicList[REF_PIC_LIST_0][idxL0]->getPOC() == m_apcRefPicList[REF_PIC_LIST_1][idxL1]->getPOC() )
443      {
444        m_list1IdxToList0Idx[idxL1] = idxL0;
445        break;
446      }
447    }
448  }
449}
450
451#if !NH_MV
452Void TComSlice::setRefPicList( TComList<TComPic*>& rcListPic, Bool checkNumPocTotalCurr )
453{
454  if (!checkNumPocTotalCurr)
455  {
456    if (m_eSliceType == I_SLICE)
457    {
458      ::memset( m_apcRefPicList, 0, sizeof (m_apcRefPicList));
459      ::memset( m_aiNumRefIdx,   0, sizeof ( m_aiNumRefIdx ));
460
461      return;
462    }
463
464    m_aiNumRefIdx[REF_PIC_LIST_0] = getNumRefIdx(REF_PIC_LIST_0);
465    m_aiNumRefIdx[REF_PIC_LIST_1] = getNumRefIdx(REF_PIC_LIST_1);
466  }
467
468  TComPic*  pcRefPic= NULL;
469  static const UInt MAX_NUM_NEGATIVE_PICTURES=16;
470  TComPic*  RefPicSetStCurr0[MAX_NUM_NEGATIVE_PICTURES];
471  TComPic*  RefPicSetStCurr1[MAX_NUM_NEGATIVE_PICTURES];
472  TComPic*  RefPicSetLtCurr[MAX_NUM_NEGATIVE_PICTURES];
473  UInt NumPicStCurr0 = 0;
474  UInt NumPicStCurr1 = 0;
475  UInt NumPicLtCurr = 0;
476  Int i;
477
478  for(i=0; i < m_pRPS->getNumberOfNegativePictures(); i++)
479  {
480    if(m_pRPS->getUsed(i))
481    {
482      pcRefPic = xGetRefPic(rcListPic, getPOC()+m_pRPS->getDeltaPOC(i));
483      pcRefPic->setIsLongTerm(0);
484      pcRefPic->getPicYuvRec()->extendPicBorder();
485      RefPicSetStCurr0[NumPicStCurr0] = pcRefPic;
486      NumPicStCurr0++;
487      pcRefPic->setCheckLTMSBPresent(false);
488    }
489  }
490
491  for(; i < m_pRPS->getNumberOfNegativePictures()+m_pRPS->getNumberOfPositivePictures(); i++)
492  {
493    if(m_pRPS->getUsed(i))
494    {
495      pcRefPic = xGetRefPic(rcListPic, getPOC()+m_pRPS->getDeltaPOC(i));
496      pcRefPic->setIsLongTerm(0);
497      pcRefPic->getPicYuvRec()->extendPicBorder();
498      RefPicSetStCurr1[NumPicStCurr1] = pcRefPic;
499      NumPicStCurr1++;
500      pcRefPic->setCheckLTMSBPresent(false);
501    }
502  }
503
504  for(i = m_pRPS->getNumberOfNegativePictures()+m_pRPS->getNumberOfPositivePictures()+m_pRPS->getNumberOfLongtermPictures()-1; i > m_pRPS->getNumberOfNegativePictures()+m_pRPS->getNumberOfPositivePictures()-1 ; i--)
505  {
506    if(m_pRPS->getUsed(i))
507    {
508      pcRefPic = xGetLongTermRefPic(rcListPic, m_pRPS->getPOC(i), m_pRPS->getCheckLTMSBPresent(i));
509      pcRefPic->setIsLongTerm(1);
510      pcRefPic->getPicYuvRec()->extendPicBorder();
511      RefPicSetLtCurr[NumPicLtCurr] = pcRefPic;
512      NumPicLtCurr++;
513    }
514    if(pcRefPic==NULL)
515    {
516      pcRefPic = xGetLongTermRefPic(rcListPic, m_pRPS->getPOC(i), m_pRPS->getCheckLTMSBPresent(i));
517    }
518    pcRefPic->setCheckLTMSBPresent(m_pRPS->getCheckLTMSBPresent(i));
519  }
520
521  // ref_pic_list_init
522  TComPic*  rpsCurrList0[MAX_NUM_REF+1];
523  TComPic*  rpsCurrList1[MAX_NUM_REF+1];
524  Int numPicTotalCurr = NumPicStCurr0 + NumPicStCurr1 + NumPicLtCurr;
525
526  if (checkNumPocTotalCurr)
527  {
528    // The variable NumPocTotalCurr is derived as specified in subclause 7.4.7.2. It is a requirement of bitstream conformance that the following applies to the value of NumPocTotalCurr:
529    // - If the current picture is a BLA or CRA picture, the value of NumPocTotalCurr shall be equal to 0.
530    // - Otherwise, when the current picture contains a P or B slice, the value of NumPocTotalCurr shall not be equal to 0.
531    if (getRapPicFlag())
532    {
533      assert(numPicTotalCurr == 0);
534    }
535
536    if (m_eSliceType == I_SLICE)
537    {
538      ::memset( m_apcRefPicList, 0, sizeof (m_apcRefPicList));
539      ::memset( m_aiNumRefIdx,   0, sizeof ( m_aiNumRefIdx ));
540
541      return;
542    }
543
544    assert(numPicTotalCurr > 0);
545    // general tier and level limit:
546    assert(numPicTotalCurr <= 8);
547
548    m_aiNumRefIdx[0] = getNumRefIdx(REF_PIC_LIST_0);
549    m_aiNumRefIdx[1] = getNumRefIdx(REF_PIC_LIST_1);
550  }
551
552  Int cIdx = 0;
553  for ( i=0; i<NumPicStCurr0; i++, cIdx++)
554  {
555    rpsCurrList0[cIdx] = RefPicSetStCurr0[i];
556  }
557  for ( i=0; i<NumPicStCurr1; i++, cIdx++)
558  {
559    rpsCurrList0[cIdx] = RefPicSetStCurr1[i];
560  }
561  for ( i=0; i<NumPicLtCurr;  i++, cIdx++)
562  {
563    rpsCurrList0[cIdx] = RefPicSetLtCurr[i];
564  }
565  assert(cIdx == numPicTotalCurr);
566
567  if (m_eSliceType==B_SLICE)
568  {
569    cIdx = 0;
570    for ( i=0; i<NumPicStCurr1; i++, cIdx++)
571    {
572      rpsCurrList1[cIdx] = RefPicSetStCurr1[i];
573    }
574    for ( i=0; i<NumPicStCurr0; i++, cIdx++)
575    {
576      rpsCurrList1[cIdx] = RefPicSetStCurr0[i];
577    }
578    for ( i=0; i<NumPicLtCurr;  i++, cIdx++)
579    {
580      rpsCurrList1[cIdx] = RefPicSetLtCurr[i];
581    }
582    assert(cIdx == numPicTotalCurr);
583  }
584
585  ::memset(m_bIsUsedAsLongTerm, 0, sizeof(m_bIsUsedAsLongTerm));
586
587  for (Int rIdx = 0; rIdx < m_aiNumRefIdx[REF_PIC_LIST_0]; rIdx ++)
588  {
589    cIdx = m_RefPicListModification.getRefPicListModificationFlagL0() ? m_RefPicListModification.getRefPicSetIdxL0(rIdx) : rIdx % numPicTotalCurr;
590    assert(cIdx >= 0 && cIdx < numPicTotalCurr);
591    m_apcRefPicList[REF_PIC_LIST_0][rIdx] = rpsCurrList0[ cIdx ];
592    m_bIsUsedAsLongTerm[REF_PIC_LIST_0][rIdx] = ( cIdx >= NumPicStCurr0 + NumPicStCurr1 );
593  }
594  if ( m_eSliceType != B_SLICE )
595  {
596    m_aiNumRefIdx[REF_PIC_LIST_1] = 0;
597    ::memset( m_apcRefPicList[REF_PIC_LIST_1], 0, sizeof(m_apcRefPicList[REF_PIC_LIST_1]));
598  }
599  else
600  {
601    for (Int rIdx = 0; rIdx < m_aiNumRefIdx[REF_PIC_LIST_1]; rIdx ++)
602    {
603      cIdx = m_RefPicListModification.getRefPicListModificationFlagL1() ? m_RefPicListModification.getRefPicSetIdxL1(rIdx) : rIdx % numPicTotalCurr;
604      assert(cIdx >= 0 && cIdx < numPicTotalCurr);
605      m_apcRefPicList[REF_PIC_LIST_1][rIdx] = rpsCurrList1[ cIdx ];
606      m_bIsUsedAsLongTerm[REF_PIC_LIST_1][rIdx] = ( cIdx >= NumPicStCurr0 + NumPicStCurr1 );
607    }
608  }
609}
610#else
611Void TComSlice::getTempRefPicLists( TComList<TComPic*>& rcListPic, std::vector<TComPic*>& refPicSetInterLayer0, std::vector<TComPic*>& refPicSetInterLayer1,                                     
612                                   std::vector<TComPic*> rpsCurrList[2], std::vector<Bool> usedAsLongTerm[2], Int& numPocTotalCurr, Bool checkNumPocTotalCurr )
613{
614  if (!checkNumPocTotalCurr)
615  {
616    if (m_eSliceType == I_SLICE)
617    {     
618      return;
619    }   
620  }
621
622  TComPic*  pcRefPic= NULL;
623  TComPic*  RefPicSetStCurr0[16];
624  TComPic*  RefPicSetStCurr1[16];
625  TComPic*  RefPicSetLtCurr[16];
626  UInt NumPocStCurr0 = 0;
627  UInt NumPocStCurr1 = 0;
628  UInt NumPocLtCurr = 0;
629  Int i;
630#if NH_3D
631  m_pocsInCurrRPSs.clear();
632#endif
633  for(i=0; i < m_pRPS->getNumberOfNegativePictures(); i++)
634  {
635    if(m_pRPS->getUsed(i))
636    {
637      pcRefPic = xGetRefPic(rcListPic, getPOC()+m_pRPS->getDeltaPOC(i));
638      pcRefPic->setIsLongTerm(0);
639      pcRefPic->getPicYuvRec()->extendPicBorder();
640      RefPicSetStCurr0[NumPocStCurr0] = pcRefPic;
641      NumPocStCurr0++;
642      pcRefPic->setCheckLTMSBPresent(false); 
643#if NH_3D
644      m_pocsInCurrRPSs.push_back( pcRefPic->getPOC() ); 
645#endif
646    }
647  }
648 
649  for(; i < m_pRPS->getNumberOfNegativePictures()+m_pRPS->getNumberOfPositivePictures(); i++)
650  {
651    if(m_pRPS->getUsed(i))
652    {
653      pcRefPic = xGetRefPic(rcListPic, getPOC()+m_pRPS->getDeltaPOC(i));
654      pcRefPic->setIsLongTerm(0);
655      pcRefPic->getPicYuvRec()->extendPicBorder();
656      RefPicSetStCurr1[NumPocStCurr1] = pcRefPic;
657      NumPocStCurr1++;
658      pcRefPic->setCheckLTMSBPresent(false); 
659#if NH_3D
660      m_pocsInCurrRPSs.push_back( pcRefPic->getPOC() ); 
661#endif
662    }
663  }
664 
665  for(i = m_pRPS->getNumberOfNegativePictures()+m_pRPS->getNumberOfPositivePictures()+m_pRPS->getNumberOfLongtermPictures()-1; i > m_pRPS->getNumberOfNegativePictures()+m_pRPS->getNumberOfPositivePictures()-1 ; i--)
666  {
667    if(m_pRPS->getUsed(i))
668    {
669      pcRefPic = xGetLongTermRefPic(rcListPic, m_pRPS->getPOC(i), m_pRPS->getCheckLTMSBPresent(i));
670      pcRefPic->setIsLongTerm(1);
671      pcRefPic->getPicYuvRec()->extendPicBorder();
672      RefPicSetLtCurr[NumPocLtCurr] = pcRefPic;
673      NumPocLtCurr++;
674#if NH_3D
675      m_pocsInCurrRPSs.push_back( pcRefPic->getPOC() ); 
676#endif
677    }
678    if(pcRefPic==NULL) 
679    {
680      pcRefPic = xGetLongTermRefPic(rcListPic, m_pRPS->getPOC(i), m_pRPS->getCheckLTMSBPresent(i));
681    }
682    pcRefPic->setCheckLTMSBPresent(m_pRPS->getCheckLTMSBPresent(i)); 
683  }
684
685  Int numPocInterCurr = NumPocStCurr0 + NumPocStCurr1 + NumPocLtCurr; 
686#if NH_3D
687  assert( numPocInterCurr == (Int) m_pocsInCurrRPSs.size() ); 
688#endif
689  numPocTotalCurr = numPocInterCurr + getNumActiveRefLayerPics( );
690  assert( numPocTotalCurr == getNumRpsCurrTempList() );
691
692  if (checkNumPocTotalCurr)
693  {
694    // The variable NumPocTotalCurr is derived as specified in subclause 7.4.7.2. It is a requirement of bitstream conformance that the following applies to the value of NumPocTotalCurr:
695    // - If nuh_layer_id is equal to 0 and the current picture is a BLA picture or a CRA picture, the value of NumPocTotalCurr shall be equal to 0.
696    // - Otherwise, when the current picture contains a P or B slice, the value of NumPocTotalCurr shall not be equal to 0.
697    if ( getRapPicFlag() && m_layerId == 0 )
698    {
699      assert(numPocTotalCurr == 0);
700    }
701
702    if (m_eSliceType == I_SLICE)
703    {
704      ::memset( m_apcRefPicList, 0, sizeof (m_apcRefPicList));
705      ::memset( m_aiNumRefIdx,   0, sizeof ( m_aiNumRefIdx ));
706     
707      return;
708    }
709   
710    assert(numPocTotalCurr > 0);
711   
712    m_aiNumRefIdx[0] = getNumRefIdx(REF_PIC_LIST_0);
713    m_aiNumRefIdx[1] = getNumRefIdx(REF_PIC_LIST_1);
714  }
715
716  std::vector<TComPic*>* refPicSetInterLayer[2] = { &refPicSetInterLayer0, &refPicSetInterLayer1}; 
717  Int numPocInterLayer[2] = { getNumActiveRefLayerPics0( ), getNumActiveRefLayerPics1( ) }; 
718 
719  TComPic**             refPicSetStCurr    [2] = { RefPicSetStCurr0, RefPicSetStCurr1 };
720  Int numPocStCurr[2] = { (Int)NumPocStCurr0, (Int)NumPocStCurr1 }; 
721
722  for (Int li = 0; li < ((m_eSliceType==B_SLICE) ? 2 : 1); li++)
723  { 
724    rpsCurrList   [li].resize(MAX_NUM_REF+1,NULL ); 
725    usedAsLongTerm[li].resize(MAX_NUM_REF+1,false); 
726
727    Int cIdx = 0;
728    for ( i=0; i < numPocStCurr[li]; i++, cIdx++)
729    {
730      rpsCurrList[li][cIdx] = refPicSetStCurr[li][i];
731      usedAsLongTerm [li][cIdx] = false; 
732    }
733
734    for ( i=0; i < numPocInterLayer[li];  i++, cIdx++)
735    {   
736      rpsCurrList[li][cIdx] = (*refPicSetInterLayer[li])[i];
737      usedAsLongTerm [li][cIdx] = true; 
738    }
739
740    for ( i=0; i < numPocStCurr[1-li]; i++, cIdx++)
741    {
742      rpsCurrList[li][cIdx] = refPicSetStCurr[1-li][i];
743      usedAsLongTerm [li][cIdx] = false; 
744    }
745
746    for ( i=0; i<NumPocLtCurr;  i++, cIdx++)
747    {
748      rpsCurrList[li][cIdx] = RefPicSetLtCurr[i];
749      usedAsLongTerm [li][cIdx] = true; 
750    }
751
752    for ( i=0; i < numPocInterLayer[1-li];  i++, cIdx++)
753    {   
754      assert( cIdx < MAX_NUM_REF );   
755      rpsCurrList[li][cIdx] = (*refPicSetInterLayer[1-li])[i];
756      usedAsLongTerm [li][cIdx] = true; 
757    }
758
759    assert(cIdx == numPocTotalCurr);
760  }
761}
762
763Void TComSlice::setRefPicList( std::vector<TComPic*> rpsCurrList[2], std::vector<Bool> usedAsLongTerm[2], Int numPocTotalCurr, Bool checkNumPocTotalCurr )
764
765{
766  if (!checkNumPocTotalCurr)
767  {
768    if (m_eSliceType == I_SLICE)
769    {
770      ::memset( m_apcRefPicList, 0, sizeof (m_apcRefPicList));
771      ::memset( m_aiNumRefIdx,   0, sizeof ( m_aiNumRefIdx ));
772
773      return;
774    }   
775  }
776
777  ::memset(m_bIsUsedAsLongTerm, 0, sizeof(m_bIsUsedAsLongTerm));
778
779  for (Int li = 0; li < 2; li++)
780  {
781    if ( m_eSliceType == P_SLICE && li == 1 )
782    {
783      m_aiNumRefIdx[1] = 0;
784      ::memset( m_apcRefPicList[1], 0, sizeof(m_apcRefPicList[1]));
785    } 
786    else
787    {
788      for (Int rIdx = 0; rIdx <= (m_aiNumRefIdx[ li ] - 1 ); rIdx ++)
789      { 
790        Bool listModified             =                m_RefPicListModification.getRefPicListModificationFlagL( li ); 
791        Int orgIdx                    = listModified ? m_RefPicListModification.getRefPicSetIdxL(li, rIdx) : (rIdx % numPocTotalCurr); 
792
793        assert( rpsCurrList[li][ orgIdx ] != NULL ); 
794        assert( rpsCurrList[li][ orgIdx ]->getSlice(0)->getDiscardableFlag() == 0 );    // Inter-layer RPS shall not contain picture with discardable_flag = 1.
795        m_apcRefPicList    [li][rIdx] = rpsCurrList    [li][ orgIdx ];
796        m_bIsUsedAsLongTerm[li][rIdx] = usedAsLongTerm [li][ orgIdx ] ; 
797      }
798    }
799  }
800}
801#endif
802
803
804Int TComSlice::getNumRpsCurrTempList() const
805{
806  Int numRpsCurrTempList = 0;
807
808  if (m_eSliceType == I_SLICE)
809  {
810    return 0;
811  }
812  for(UInt i=0; i < m_pRPS->getNumberOfNegativePictures()+ m_pRPS->getNumberOfPositivePictures() + m_pRPS->getNumberOfLongtermPictures(); i++)
813  {
814    if(m_pRPS->getUsed(i))
815    {
816      numRpsCurrTempList++;
817    }
818  }
819#if NH_MV
820    numRpsCurrTempList = numRpsCurrTempList + getNumActiveRefLayerPics();
821#endif
822  return numRpsCurrTempList;
823}
824
825Void TComSlice::initEqualRef()
826{
827  for (Int iDir = 0; iDir < NUM_REF_PIC_LIST_01; iDir++)
828  {
829    for (Int iRefIdx1 = 0; iRefIdx1 < MAX_NUM_REF; iRefIdx1++)
830    {
831      for (Int iRefIdx2 = iRefIdx1; iRefIdx2 < MAX_NUM_REF; iRefIdx2++)
832      {
833        m_abEqualRef[iDir][iRefIdx1][iRefIdx2] = m_abEqualRef[iDir][iRefIdx2][iRefIdx1] = (iRefIdx1 == iRefIdx2? true : false);
834      }
835    }
836  }
837}
838#if NH_3D
839#if NH_3D_TMVP
840Void TComSlice::generateAlterRefforTMVP()
841{
842  for ( UInt uiRefListIdx = 0; uiRefListIdx < 2; uiRefListIdx++ )
843  {       
844    if ( this->getNumRefIdx( RefPicList( uiRefListIdx ) ) == 0)
845    {
846        continue;
847    }
848    Bool bZeroIdxLtFlag = this->getRefPic(RefPicList(uiRefListIdx), 0)->getIsLongTerm();
849    for(Int i = 1; i < this->getNumRefIdx(RefPicList(uiRefListIdx)); i++ )
850    {
851      if ( ( bZeroIdxLtFlag && !this->getRefPic(RefPicList(uiRefListIdx), i)->getIsLongTerm() ) ||
852           (!bZeroIdxLtFlag &&  this->getRefPic(RefPicList(uiRefListIdx), i)->getIsLongTerm() ) )
853      {
854        this->setAlterRefIdx(RefPicList(uiRefListIdx),i);
855        break;
856      }
857    }
858  }
859}
860#endif
861#endif
862
863#if NH_3D
864Void TComSlice::setCamparaSlice( Int** aaiScale, Int** aaiOffset )
865{ 
866  Int voiInVps      = m_pcVPS->getVoiInVps(getViewIndex() ); 
867  if( m_pcVPS->getNumCp( voiInVps ) > 0 )
868  {   
869    if( m_pcVPS->getCpInSliceSegmentHeaderFlag( voiInVps ) )
870    {
871      for( Int m = 0; m < m_pcVPS->getNumCp( voiInVps ); m++ )
872      {     
873        Int j      = m_pcVPS->getCpRefVoi( voiInVps, m );
874        Int jInVps = m_pcVPS->getVoiInVps( j ); 
875
876        setCpScale   ( jInVps , aaiScale [ jInVps   ][ voiInVps ]);
877        setCpInvScale( jInVps , aaiScale [ voiInVps ][ jInVps   ]);
878        setCpOff     ( jInVps , aaiOffset[ jInVps   ][ voiInVps ]);
879        setCpInvOff  ( jInVps , aaiOffset[ voiInVps ][ jInVps   ]);
880      }
881    }
882  } 
883}
884#endif
885
886Void TComSlice::checkColRefIdx(UInt curSliceIdx, TComPic* pic)
887{
888  Int i;
889  TComSlice* curSlice = pic->getSlice(curSliceIdx);
890  Int currColRefPOC =  curSlice->getRefPOC( RefPicList(1 - curSlice->getColFromL0Flag()), curSlice->getColRefIdx());
891  TComSlice* preSlice;
892  Int preColRefPOC;
893  for(i=curSliceIdx-1; i>=0; i--)
894  {
895    preSlice = pic->getSlice(i);
896    if(preSlice->getSliceType() != I_SLICE)
897    {
898      preColRefPOC  = preSlice->getRefPOC( RefPicList(1 - preSlice->getColFromL0Flag()), preSlice->getColRefIdx());
899      if(currColRefPOC != preColRefPOC)
900      {
901        printf("Collocated_ref_idx shall always be the same for all slices of a coded picture!\n");
902        exit(EXIT_FAILURE);
903      }
904      else
905      {
906        break;
907      }
908    }
909  }
910}
911
912Void TComSlice::checkCRA(const TComReferencePictureSet *pReferencePictureSet, Int& pocCRA, NalUnitType& associatedIRAPType, TComList<TComPic *>& rcListPic)
913{
914  for(Int i = 0; i < pReferencePictureSet->getNumberOfNegativePictures()+pReferencePictureSet->getNumberOfPositivePictures(); i++)
915  {
916    if(pocCRA < MAX_UINT && getPOC() > pocCRA)
917    {
918      assert(getPOC()+pReferencePictureSet->getDeltaPOC(i) >= pocCRA);
919    }
920  }
921  for(Int i = pReferencePictureSet->getNumberOfNegativePictures()+pReferencePictureSet->getNumberOfPositivePictures(); i < pReferencePictureSet->getNumberOfPictures(); i++)
922  {
923    if(pocCRA < MAX_UINT && getPOC() > pocCRA)
924    {
925      if (!pReferencePictureSet->getCheckLTMSBPresent(i))
926      {
927        assert(xGetLongTermRefPic(rcListPic, pReferencePictureSet->getPOC(i), false)->getPOC() >= pocCRA);
928      }
929      else
930      {
931        assert(pReferencePictureSet->getPOC(i) >= pocCRA);
932      }
933    }
934  }
935  if ( getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_W_RADL || getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_N_LP ) // IDR picture found
936  {
937    pocCRA = getPOC();
938    associatedIRAPType = getNalUnitType();
939  }
940  else if ( getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA ) // CRA picture found
941  {
942    pocCRA = getPOC();
943    associatedIRAPType = getNalUnitType();
944  }
945  else if ( getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP
946         || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL
947         || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP ) // BLA picture found
948  {
949    pocCRA = getPOC();
950    associatedIRAPType = getNalUnitType();
951  }
952}
953
954/** Function for marking the reference pictures when an IDR/CRA/CRANT/BLA/BLANT is encountered.
955 * \param pocCRA POC of the CRA/CRANT/BLA/BLANT picture
956 * \param bRefreshPending flag indicating if a deferred decoding refresh is pending
957 * \param rcListPic reference to the reference picture list
958 * This function marks the reference pictures as "unused for reference" in the following conditions.
959 * If the nal_unit_type is IDR/BLA/BLANT, all pictures in the reference picture list
960 * are marked as "unused for reference"
961 *    If the nal_unit_type is BLA/BLANT, set the pocCRA to the temporal reference of the current picture.
962 * Otherwise
963 *    If the bRefreshPending flag is true (a deferred decoding refresh is pending) and the current
964 *    temporal reference is greater than the temporal reference of the latest CRA/CRANT/BLA/BLANT picture (pocCRA),
965 *    mark all reference pictures except the latest CRA/CRANT/BLA/BLANT picture as "unused for reference" and set
966 *    the bRefreshPending flag to false.
967 *    If the nal_unit_type is CRA/CRANT, set the bRefreshPending flag to true and pocCRA to the temporal
968 *    reference of the current picture.
969 * Note that the current picture is already placed in the reference list and its marking is not changed.
970 * If the current picture has a nal_ref_idc that is not 0, it will remain marked as "used for reference".
971 */
972Void TComSlice::decodingRefreshMarking(Int& pocCRA, Bool& bRefreshPending, TComList<TComPic*>& rcListPic, const bool bEfficientFieldIRAPEnabled)
973{
974  TComPic* rpcPic;
975  Int      pocCurr = getPOC();
976
977  if ( getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP
978    || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL
979    || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP
980    || getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_W_RADL
981    || getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_N_LP )  // IDR or BLA picture
982  {
983    // mark all pictures as not used for reference
984    TComList<TComPic*>::iterator        iterPic       = rcListPic.begin();
985    while (iterPic != rcListPic.end())
986    {
987      rpcPic = *(iterPic);
988      rpcPic->setCurrSliceIdx(0);
989      if (rpcPic->getPOC() != pocCurr)
990      {
991        rpcPic->getSlice(0)->setReferenced(false);
992      }
993      iterPic++;
994    }
995    if ( getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP
996      || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL
997      || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP )
998    {
999      pocCRA = pocCurr;
1000    }
1001    if (bEfficientFieldIRAPEnabled)
1002    {
1003    bRefreshPending = true;
1004    }
1005  }
1006  else // CRA or No DR
1007  {
1008    if(bEfficientFieldIRAPEnabled && (getAssociatedIRAPType() == NAL_UNIT_CODED_SLICE_IDR_N_LP || getAssociatedIRAPType() == NAL_UNIT_CODED_SLICE_IDR_W_RADL))
1009    {
1010      if (bRefreshPending==true && pocCurr > m_iLastIDR) // IDR reference marking pending
1011      {
1012        TComList<TComPic*>::iterator        iterPic       = rcListPic.begin();
1013        while (iterPic != rcListPic.end())
1014        {
1015          rpcPic = *(iterPic);
1016          if (rpcPic->getPOC() != pocCurr && rpcPic->getPOC() != m_iLastIDR)
1017          {
1018            rpcPic->getSlice(0)->setReferenced(false);
1019          }
1020          iterPic++;
1021        }
1022        bRefreshPending = false; 
1023      }
1024    }
1025    else
1026    {
1027      if (bRefreshPending==true && pocCurr > pocCRA) // CRA reference marking pending
1028      {
1029        TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1030        while (iterPic != rcListPic.end())
1031        {
1032          rpcPic = *(iterPic);
1033          if (rpcPic->getPOC() != pocCurr && rpcPic->getPOC() != pocCRA)
1034          {
1035            rpcPic->getSlice(0)->setReferenced(false);
1036          }
1037          iterPic++;
1038        }
1039        bRefreshPending = false;
1040      }
1041    }
1042    if ( getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA ) // CRA picture found
1043    {
1044      bRefreshPending = true;
1045      pocCRA = pocCurr;
1046    }
1047  }
1048}
1049
1050Void TComSlice::copySliceInfo(TComSlice *pSrc)
1051{
1052  assert( pSrc != NULL );
1053
1054  Int i, j, k;
1055
1056  m_iPOC                 = pSrc->m_iPOC;
1057  m_eNalUnitType         = pSrc->m_eNalUnitType;
1058#if NH_MV
1059  m_layerId              = pSrc->m_layerId;
1060  // GT: Copying of several other values might be be missing here, or is above not necessary?
1061#endif
1062  m_eSliceType           = pSrc->m_eSliceType;
1063  m_iSliceQp             = pSrc->m_iSliceQp;
1064#if ADAPTIVE_QP_SELECTION
1065  m_iSliceQpBase         = pSrc->m_iSliceQpBase;
1066#endif
1067  m_ChromaQpAdjEnabled = pSrc->m_ChromaQpAdjEnabled;
1068  m_deblockingFilterDisable   = pSrc->m_deblockingFilterDisable;
1069  m_deblockingFilterOverrideFlag = pSrc->m_deblockingFilterOverrideFlag;
1070  m_deblockingFilterBetaOffsetDiv2 = pSrc->m_deblockingFilterBetaOffsetDiv2;
1071  m_deblockingFilterTcOffsetDiv2 = pSrc->m_deblockingFilterTcOffsetDiv2;
1072
1073  for (i = 0; i < NUM_REF_PIC_LIST_01; i++)
1074  {
1075    m_aiNumRefIdx[i]     = pSrc->m_aiNumRefIdx[i];
1076  }
1077
1078  for (i = 0; i < MAX_NUM_REF; i++)
1079  {
1080    m_list1IdxToList0Idx[i] = pSrc->m_list1IdxToList0Idx[i];
1081  }
1082
1083  m_bCheckLDC             = pSrc->m_bCheckLDC;
1084  m_iSliceQpDelta        = pSrc->m_iSliceQpDelta;
1085  for (UInt component = 0; component < MAX_NUM_COMPONENT; component++)
1086  {
1087    m_iSliceChromaQpDelta[component] = pSrc->m_iSliceChromaQpDelta[component];
1088  }
1089  for (i = 0; i < NUM_REF_PIC_LIST_01; i++)
1090  {
1091    for (j = 0; j < MAX_NUM_REF; j++)
1092    {
1093      m_apcRefPicList[i][j]  = pSrc->m_apcRefPicList[i][j];
1094      m_aiRefPOCList[i][j]   = pSrc->m_aiRefPOCList[i][j];
1095      m_bIsUsedAsLongTerm[i][j] = pSrc->m_bIsUsedAsLongTerm[i][j];
1096#if NH_MV
1097      m_aiRefLayerIdList[i][j] = pSrc->m_aiRefLayerIdList[i][j];
1098#endif
1099    }
1100    m_bIsUsedAsLongTerm[i][MAX_NUM_REF] = pSrc->m_bIsUsedAsLongTerm[i][MAX_NUM_REF];
1101  }
1102  m_iDepth               = pSrc->m_iDepth;
1103
1104  // referenced slice
1105  m_bRefenced            = pSrc->m_bRefenced;
1106
1107  // access channel
1108#if NH_MV
1109  m_pcVPS                = pSrc->m_pcVPS;
1110#endif
1111  m_pRPS                = pSrc->m_pRPS;
1112  m_iLastIDR             = pSrc->m_iLastIDR;
1113
1114  m_pcPic                = pSrc->m_pcPic;
1115
1116  m_colFromL0Flag        = pSrc->m_colFromL0Flag;
1117  m_colRefIdx            = pSrc->m_colRefIdx;
1118
1119  setLambdas(pSrc->getLambdas());
1120
1121  for (i = 0; i < NUM_REF_PIC_LIST_01; i++)
1122  {
1123    for (j = 0; j < MAX_NUM_REF; j++)
1124    {
1125      for (k =0; k < MAX_NUM_REF; k++)
1126      {
1127        m_abEqualRef[i][j][k] = pSrc->m_abEqualRef[i][j][k];
1128      }
1129    }
1130  }
1131
1132  m_uiTLayer                      = pSrc->m_uiTLayer;
1133  m_bTLayerSwitchingFlag          = pSrc->m_bTLayerSwitchingFlag;
1134
1135  m_sliceMode                     = pSrc->m_sliceMode;
1136  m_sliceArgument                 = pSrc->m_sliceArgument;
1137  m_sliceCurStartCtuTsAddr        = pSrc->m_sliceCurStartCtuTsAddr;
1138  m_sliceCurEndCtuTsAddr          = pSrc->m_sliceCurEndCtuTsAddr;
1139  m_sliceIdx                      = pSrc->m_sliceIdx;
1140  m_sliceSegmentMode              = pSrc->m_sliceSegmentMode;
1141  m_sliceSegmentArgument          = pSrc->m_sliceSegmentArgument;
1142  m_sliceSegmentCurStartCtuTsAddr = pSrc->m_sliceSegmentCurStartCtuTsAddr;
1143  m_sliceSegmentCurEndCtuTsAddr   = pSrc->m_sliceSegmentCurEndCtuTsAddr;
1144  m_nextSlice                     = pSrc->m_nextSlice;
1145  m_nextSliceSegment              = pSrc->m_nextSliceSegment;
1146
1147  for ( UInt e=0 ; e<NUM_REF_PIC_LIST_01 ; e++ )
1148  {
1149    for ( UInt n=0 ; n<MAX_NUM_REF ; n++ )
1150    {
1151      memcpy(m_weightPredTable[e][n], pSrc->m_weightPredTable[e][n], sizeof(WPScalingParam)*MAX_NUM_COMPONENT );
1152    }
1153  }
1154
1155  for( UInt ch = 0 ; ch < MAX_NUM_CHANNEL_TYPE; ch++)
1156  {
1157    m_saoEnabledFlag[ch] = pSrc->m_saoEnabledFlag[ch];
1158  }
1159
1160  m_cabacInitFlag                 = pSrc->m_cabacInitFlag;
1161
1162  m_bLMvdL1Zero                   = pSrc->m_bLMvdL1Zero;
1163  m_LFCrossSliceBoundaryFlag      = pSrc->m_LFCrossSliceBoundaryFlag;
1164  m_enableTMVPFlag                = pSrc->m_enableTMVPFlag;
1165  m_maxNumMergeCand               = pSrc->m_maxNumMergeCand;
1166  m_encCABACTableIdx              = pSrc->m_encCABACTableIdx;
1167
1168#if NH_MV
1169  // Additional slice header syntax elements
1170  m_pocResetFlag               = pSrc->m_pocResetFlag; 
1171  m_discardableFlag            = pSrc->m_discardableFlag; 
1172  m_interLayerPredEnabledFlag  = pSrc->m_interLayerPredEnabledFlag; 
1173  m_numInterLayerRefPicsMinus1 = pSrc->m_numInterLayerRefPicsMinus1;
1174
1175  for (Int layer = 0; layer < MAX_NUM_LAYERS; layer++ )
1176  {
1177    m_interLayerPredLayerIdc[ layer ] = pSrc->m_interLayerPredLayerIdc[ layer ]; 
1178  }
1179#endif
1180#if NH_3D_DIS
1181  m_bApplyDIS = pSrc->m_bApplyDIS;
1182#endif
1183#if NH_3D_IC
1184  m_bApplyIC = pSrc->m_bApplyIC;
1185  m_icSkipParseFlag = pSrc->m_icSkipParseFlag;
1186#endif
1187
1188}
1189
1190
1191/** Function for setting the slice's temporal layer ID and corresponding temporal_layer_switching_point_flag.
1192 * \param uiTLayer Temporal layer ID of the current slice
1193 * The decoder calls this function to set temporal_layer_switching_point_flag for each temporal layer based on
1194 * the SPS's temporal_id_nesting_flag and the parsed PPS.  Then, current slice's temporal layer ID and
1195 * temporal_layer_switching_point_flag is set accordingly.
1196 */
1197Void TComSlice::setTLayerInfo( UInt uiTLayer )
1198{
1199  m_uiTLayer = uiTLayer;
1200}
1201
1202/** Function for checking if this is a switching-point
1203*/
1204Bool TComSlice::isTemporalLayerSwitchingPoint(TComList<TComPic*>& rcListPic)
1205{
1206  TComPic* rpcPic;
1207  // loop through all pictures in the reference picture buffer
1208  TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1209  while ( iterPic != rcListPic.end())
1210  {
1211    rpcPic = *(iterPic++);
1212    if(rpcPic->getSlice(0)->isReferenced() && rpcPic->getPOC() != getPOC())
1213    {
1214      if(rpcPic->getTLayer() >= getTLayer())
1215      {
1216        return false;
1217      }
1218    }
1219  }
1220  return true;
1221}
1222
1223/** Function for checking if this is a STSA candidate
1224 */
1225Bool TComSlice::isStepwiseTemporalLayerSwitchingPointCandidate(TComList<TComPic*>& rcListPic)
1226{
1227  TComPic* rpcPic;
1228
1229  TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1230  while ( iterPic != rcListPic.end())
1231  {
1232    rpcPic = *(iterPic++);
1233    if(rpcPic->getSlice(0)->isReferenced() &&  (rpcPic->getUsedByCurr()==true) && rpcPic->getPOC() != getPOC())
1234    {
1235      if(rpcPic->getTLayer() >= getTLayer())
1236      {
1237        return false;
1238      }
1239    }
1240  }
1241  return true;
1242}
1243
1244
1245Void TComSlice::checkLeadingPictureRestrictions(TComList<TComPic*>& rcListPic)
1246{
1247  TComPic* rpcPic;
1248
1249  Int nalUnitType = this->getNalUnitType();
1250
1251  // When a picture is a leading picture, it shall be a RADL or RASL picture.
1252  if(this->getAssociatedIRAPPOC() > this->getPOC())
1253  {
1254    // Do not check IRAP pictures since they may get a POC lower than their associated IRAP
1255    if(nalUnitType < NAL_UNIT_CODED_SLICE_BLA_W_LP ||
1256       nalUnitType > NAL_UNIT_RESERVED_IRAP_VCL23)
1257    {
1258      assert(nalUnitType == NAL_UNIT_CODED_SLICE_RASL_N ||
1259             nalUnitType == NAL_UNIT_CODED_SLICE_RASL_R ||
1260             nalUnitType == NAL_UNIT_CODED_SLICE_RADL_N ||
1261             nalUnitType == NAL_UNIT_CODED_SLICE_RADL_R);
1262    }
1263  }
1264
1265  // When a picture is a trailing picture, it shall not be a RADL or RASL picture.
1266  if(this->getAssociatedIRAPPOC() < this->getPOC())
1267  {
1268    assert(nalUnitType != NAL_UNIT_CODED_SLICE_RASL_N &&
1269           nalUnitType != NAL_UNIT_CODED_SLICE_RASL_R &&
1270           nalUnitType != NAL_UNIT_CODED_SLICE_RADL_N &&
1271           nalUnitType != NAL_UNIT_CODED_SLICE_RADL_R);
1272  }
1273
1274  // No RASL pictures shall be present in the bitstream that are associated
1275  // with a BLA picture having nal_unit_type equal to BLA_W_RADL or BLA_N_LP.
1276  if(nalUnitType == NAL_UNIT_CODED_SLICE_RASL_N ||
1277     nalUnitType == NAL_UNIT_CODED_SLICE_RASL_R)
1278  {
1279    assert(this->getAssociatedIRAPType() != NAL_UNIT_CODED_SLICE_BLA_W_RADL &&
1280           this->getAssociatedIRAPType() != NAL_UNIT_CODED_SLICE_BLA_N_LP);
1281  }
1282
1283  // No RASL pictures shall be present in the bitstream that are associated with
1284  // an IDR picture.
1285  if(nalUnitType == NAL_UNIT_CODED_SLICE_RASL_N ||
1286     nalUnitType == NAL_UNIT_CODED_SLICE_RASL_R)
1287  {
1288    assert(this->getAssociatedIRAPType() != NAL_UNIT_CODED_SLICE_IDR_N_LP   &&
1289           this->getAssociatedIRAPType() != NAL_UNIT_CODED_SLICE_IDR_W_RADL);
1290  }
1291
1292  // No RADL pictures shall be present in the bitstream that are associated with
1293  // a BLA picture having nal_unit_type equal to BLA_N_LP or that are associated
1294  // with an IDR picture having nal_unit_type equal to IDR_N_LP.
1295  if(nalUnitType == NAL_UNIT_CODED_SLICE_RADL_N ||
1296     nalUnitType == NAL_UNIT_CODED_SLICE_RADL_R)
1297  {
1298    assert(this->getAssociatedIRAPType() != NAL_UNIT_CODED_SLICE_BLA_N_LP   &&
1299           this->getAssociatedIRAPType() != NAL_UNIT_CODED_SLICE_IDR_N_LP);
1300  }
1301
1302  // loop through all pictures in the reference picture buffer
1303  TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1304  while ( iterPic != rcListPic.end())
1305  {
1306    rpcPic = *(iterPic++);
1307    if(!rpcPic->getReconMark())
1308    {
1309      continue;
1310    }
1311    if (rpcPic->getPOC() == this->getPOC())
1312    {
1313      continue;
1314    }
1315
1316    // Any picture that has PicOutputFlag equal to 1 that precedes an IRAP picture
1317    // in decoding order shall precede the IRAP picture in output order.
1318    // (Note that any picture following in output order would be present in the DPB)
1319    if(rpcPic->getSlice(0)->getPicOutputFlag() == 1 && !this->getNoOutputPriorPicsFlag())
1320    {
1321      if(nalUnitType == NAL_UNIT_CODED_SLICE_BLA_N_LP    ||
1322         nalUnitType == NAL_UNIT_CODED_SLICE_BLA_W_LP    ||
1323         nalUnitType == NAL_UNIT_CODED_SLICE_BLA_W_RADL  ||
1324         nalUnitType == NAL_UNIT_CODED_SLICE_CRA         ||
1325         nalUnitType == NAL_UNIT_CODED_SLICE_IDR_N_LP    ||
1326         nalUnitType == NAL_UNIT_CODED_SLICE_IDR_W_RADL)
1327      {
1328        assert(rpcPic->getPOC() < this->getPOC());
1329      }
1330    }
1331
1332    // Any picture that has PicOutputFlag equal to 1 that precedes an IRAP picture
1333    // in decoding order shall precede any RADL picture associated with the IRAP
1334    // picture in output order.
1335    if(rpcPic->getSlice(0)->getPicOutputFlag() == 1)
1336    {
1337      if((nalUnitType == NAL_UNIT_CODED_SLICE_RADL_N ||
1338          nalUnitType == NAL_UNIT_CODED_SLICE_RADL_R))
1339      {
1340        // rpcPic precedes the IRAP in decoding order
1341        if(this->getAssociatedIRAPPOC() > rpcPic->getSlice(0)->getAssociatedIRAPPOC())
1342        {
1343          // rpcPic must not be the IRAP picture
1344          if(this->getAssociatedIRAPPOC() != rpcPic->getPOC())
1345          {
1346            assert(rpcPic->getPOC() < this->getPOC());
1347          }
1348        }
1349      }
1350    }
1351
1352    // When a picture is a leading picture, it shall precede, in decoding order,
1353    // all trailing pictures that are associated with the same IRAP picture.
1354      if(nalUnitType == NAL_UNIT_CODED_SLICE_RASL_N ||
1355         nalUnitType == NAL_UNIT_CODED_SLICE_RASL_R ||
1356         nalUnitType == NAL_UNIT_CODED_SLICE_RADL_N ||
1357         nalUnitType == NAL_UNIT_CODED_SLICE_RADL_R)
1358      {
1359        if(rpcPic->getSlice(0)->getAssociatedIRAPPOC() == this->getAssociatedIRAPPOC())
1360        {
1361          // rpcPic is a picture that preceded the leading in decoding order since it exist in the DPB
1362          // rpcPic would violate the constraint if it was a trailing picture
1363          assert(rpcPic->getPOC() <= this->getAssociatedIRAPPOC());
1364        }
1365      }
1366
1367    // Any RASL picture associated with a CRA or BLA picture shall precede any
1368    // RADL picture associated with the CRA or BLA picture in output order
1369    if(nalUnitType == NAL_UNIT_CODED_SLICE_RASL_N ||
1370       nalUnitType == NAL_UNIT_CODED_SLICE_RASL_R)
1371    {
1372      if((this->getAssociatedIRAPType() == NAL_UNIT_CODED_SLICE_BLA_N_LP   ||
1373          this->getAssociatedIRAPType() == NAL_UNIT_CODED_SLICE_BLA_W_LP   ||
1374          this->getAssociatedIRAPType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL ||
1375          this->getAssociatedIRAPType() == NAL_UNIT_CODED_SLICE_CRA)       &&
1376          this->getAssociatedIRAPPOC() == rpcPic->getSlice(0)->getAssociatedIRAPPOC())
1377      {
1378        if(rpcPic->getSlice(0)->getNalUnitType() == NAL_UNIT_CODED_SLICE_RADL_N ||
1379           rpcPic->getSlice(0)->getNalUnitType() == NAL_UNIT_CODED_SLICE_RADL_R)
1380        {
1381          assert(rpcPic->getPOC() > this->getPOC());
1382        }
1383      }
1384    }
1385
1386    // Any RASL picture associated with a CRA picture shall follow, in output
1387    // order, any IRAP picture that precedes the CRA picture in decoding order.
1388    if(nalUnitType == NAL_UNIT_CODED_SLICE_RASL_N ||
1389       nalUnitType == NAL_UNIT_CODED_SLICE_RASL_R)
1390    {
1391      if(this->getAssociatedIRAPType() == NAL_UNIT_CODED_SLICE_CRA)
1392      {
1393        if(rpcPic->getSlice(0)->getPOC() < this->getAssociatedIRAPPOC() &&
1394           (rpcPic->getSlice(0)->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP   ||
1395            rpcPic->getSlice(0)->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP   ||
1396            rpcPic->getSlice(0)->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL ||
1397            rpcPic->getSlice(0)->getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_N_LP   ||
1398            rpcPic->getSlice(0)->getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_W_RADL ||
1399            rpcPic->getSlice(0)->getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA))
1400        {
1401          assert(this->getPOC() > rpcPic->getSlice(0)->getPOC());
1402        }
1403      }
1404    }
1405  }
1406}
1407
1408
1409
1410/** Function for applying picture marking based on the Reference Picture Set in pReferencePictureSet.
1411*/
1412Void TComSlice::applyReferencePictureSet( TComList<TComPic*>& rcListPic, const TComReferencePictureSet *pReferencePictureSet)
1413{
1414  TComPic* rpcPic;
1415  Int i, isReference;
1416
1417  checkLeadingPictureRestrictions(rcListPic);
1418
1419  // loop through all pictures in the reference picture buffer
1420  TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1421  while ( iterPic != rcListPic.end())
1422  {
1423    rpcPic = *(iterPic++);
1424
1425    if(!rpcPic->getSlice( 0 )->isReferenced())
1426    {
1427      continue;
1428    }
1429
1430    isReference = 0;
1431    // loop through all pictures in the Reference Picture Set
1432    // to see if the picture should be kept as reference picture
1433    for(i=0;i<pReferencePictureSet->getNumberOfPositivePictures()+pReferencePictureSet->getNumberOfNegativePictures();i++)
1434    {
1435      if(!rpcPic->getIsLongTerm() && rpcPic->getPicSym()->getSlice(0)->getPOC() == this->getPOC() + pReferencePictureSet->getDeltaPOC(i))
1436      {
1437        isReference = 1;
1438        rpcPic->setUsedByCurr(pReferencePictureSet->getUsed(i));
1439        rpcPic->setIsLongTerm(0);
1440      }
1441    }
1442    for(;i<pReferencePictureSet->getNumberOfPictures();i++)
1443    {
1444      if(pReferencePictureSet->getCheckLTMSBPresent(i)==true)
1445      {
1446        if(rpcPic->getIsLongTerm() && (rpcPic->getPicSym()->getSlice(0)->getPOC()) == pReferencePictureSet->getPOC(i))
1447        {
1448          isReference = 1;
1449          rpcPic->setUsedByCurr(pReferencePictureSet->getUsed(i));
1450        }
1451      }
1452      else
1453      {
1454        Int pocCycle = 1<<rpcPic->getPicSym()->getSlice(0)->getSPS()->getBitsForPOC();
1455        Int curPoc = rpcPic->getPicSym()->getSlice(0)->getPOC() & (pocCycle-1);
1456        Int refPoc = pReferencePictureSet->getPOC(i) & (pocCycle-1);
1457        if(rpcPic->getIsLongTerm() && curPoc == refPoc)
1458        {
1459          isReference = 1;
1460          rpcPic->setUsedByCurr(pReferencePictureSet->getUsed(i));
1461        }
1462      }
1463
1464    }
1465#if NH_MV
1466    if( isReference ) // Current picture is in the temporal RPS
1467    {
1468      assert( rpcPic->getSlice(0)->getDiscardableFlag() == 0 ); // Temporal RPS shall not contain picture with discardable_flag equal to 1
1469    }
1470#endif
1471    // mark the picture as "unused for reference" if it is not in
1472    // the Reference Picture Set
1473    if(rpcPic->getPicSym()->getSlice(0)->getPOC() != this->getPOC() && isReference == 0)
1474    {
1475      rpcPic->getSlice( 0 )->setReferenced( false );
1476      rpcPic->setUsedByCurr(0);
1477      rpcPic->setIsLongTerm(0);
1478    }
1479    //check that pictures of higher temporal layers are not used
1480    assert(rpcPic->getSlice( 0 )->isReferenced()==0||rpcPic->getUsedByCurr()==0||rpcPic->getTLayer()<=this->getTLayer());
1481    //check that pictures of higher or equal temporal layer are not in the RPS if the current picture is a TSA picture
1482    if(this->getNalUnitType() == NAL_UNIT_CODED_SLICE_TSA_R || this->getNalUnitType() == NAL_UNIT_CODED_SLICE_TSA_N)
1483    {
1484      assert(rpcPic->getSlice( 0 )->isReferenced()==0||rpcPic->getTLayer()<this->getTLayer());
1485    }
1486    //check that pictures marked as temporal layer non-reference pictures are not used for reference
1487    if(rpcPic->getPicSym()->getSlice(0)->getPOC() != this->getPOC() && rpcPic->getTLayer()==this->getTLayer())
1488    {
1489      assert(rpcPic->getSlice( 0 )->isReferenced()==0||rpcPic->getUsedByCurr()==0||rpcPic->getSlice( 0 )->getTemporalLayerNonReferenceFlag()==false);
1490    }
1491  }
1492}
1493
1494/** Function for applying picture marking based on the Reference Picture Set in pReferencePictureSet.
1495*/
1496Int TComSlice::checkThatAllRefPicsAreAvailable( TComList<TComPic*>& rcListPic, const TComReferencePictureSet *pReferencePictureSet, Bool printErrors, Int pocRandomAccess, Bool bUseRecoveryPoint)
1497{
1498  Int atLeastOneUnabledByRecoveryPoint = 0;
1499  Int atLeastOneFlushedByPreviousIDR = 0;
1500  TComPic* rpcPic;
1501  Int i, isAvailable;
1502  Int atLeastOneLost = 0;
1503  Int atLeastOneRemoved = 0;
1504  Int iPocLost = 0;
1505
1506  // loop through all long-term pictures in the Reference Picture Set
1507  // to see if the picture should be kept as reference picture
1508  for(i=pReferencePictureSet->getNumberOfNegativePictures()+pReferencePictureSet->getNumberOfPositivePictures();i<pReferencePictureSet->getNumberOfPictures();i++)
1509  {
1510    isAvailable = 0;
1511    // loop through all pictures in the reference picture buffer
1512    TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1513    while ( iterPic != rcListPic.end())
1514    {
1515      rpcPic = *(iterPic++);
1516      if(pReferencePictureSet->getCheckLTMSBPresent(i)==true)
1517      {
1518        if(rpcPic->getIsLongTerm() && (rpcPic->getPicSym()->getSlice(0)->getPOC()) == pReferencePictureSet->getPOC(i) && rpcPic->getSlice(0)->isReferenced())
1519        {
1520          if(bUseRecoveryPoint && this->getPOC() > pocRandomAccess && this->getPOC() + pReferencePictureSet->getDeltaPOC(i) < pocRandomAccess)
1521          {
1522            isAvailable = 0;
1523          }
1524          else
1525          {
1526            isAvailable = 1;
1527          }
1528        }
1529      }
1530      else
1531      {
1532        Int pocCycle = 1<<rpcPic->getPicSym()->getSlice(0)->getSPS()->getBitsForPOC();
1533        Int curPoc = rpcPic->getPicSym()->getSlice(0)->getPOC() & (pocCycle-1);
1534        Int refPoc = pReferencePictureSet->getPOC(i) & (pocCycle-1);
1535        if(rpcPic->getIsLongTerm() && curPoc == refPoc && rpcPic->getSlice(0)->isReferenced())
1536        {
1537          if(bUseRecoveryPoint && this->getPOC() > pocRandomAccess && this->getPOC() + pReferencePictureSet->getDeltaPOC(i) < pocRandomAccess)
1538          {
1539            isAvailable = 0;
1540          }
1541          else
1542          {
1543            isAvailable = 1;
1544          }
1545        }
1546      }
1547    }
1548    // if there was no such long-term check the short terms
1549    if(!isAvailable)
1550    {
1551      iterPic = rcListPic.begin();
1552      while ( iterPic != rcListPic.end())
1553      {
1554        rpcPic = *(iterPic++);
1555
1556        Int pocCycle = 1 << rpcPic->getPicSym()->getSlice(0)->getSPS()->getBitsForPOC();
1557        Int curPoc = rpcPic->getPicSym()->getSlice(0)->getPOC();
1558        Int refPoc = pReferencePictureSet->getPOC(i);
1559        if (!pReferencePictureSet->getCheckLTMSBPresent(i))
1560        {
1561          curPoc = curPoc & (pocCycle - 1);
1562          refPoc = refPoc & (pocCycle - 1);
1563        }
1564
1565        if (rpcPic->getSlice(0)->isReferenced() && curPoc == refPoc)
1566        {
1567          if(bUseRecoveryPoint && this->getPOC() > pocRandomAccess && this->getPOC() + pReferencePictureSet->getDeltaPOC(i) < pocRandomAccess)
1568          {
1569            isAvailable = 0;
1570          }
1571          else
1572          {
1573            isAvailable = 1;
1574            rpcPic->setIsLongTerm(1);
1575            break;
1576          }
1577        }
1578      }
1579    }
1580    // report that a picture is lost if it is in the Reference Picture Set
1581    // but not available as reference picture
1582    if(isAvailable == 0)
1583    {
1584      if (this->getPOC() + pReferencePictureSet->getDeltaPOC(i) >= pocRandomAccess)
1585      {
1586        if(!pReferencePictureSet->getUsed(i) )
1587        {
1588          if(printErrors)
1589          {
1590            printf("\nLong-term reference picture with POC = %3d seems to have been removed or not correctly decoded.", this->getPOC() + pReferencePictureSet->getDeltaPOC(i));
1591          }
1592          atLeastOneRemoved = 1;
1593        }
1594        else
1595        {
1596          if(printErrors)
1597          {
1598            printf("\nLong-term reference picture with POC = %3d is lost or not correctly decoded!", this->getPOC() + pReferencePictureSet->getDeltaPOC(i));
1599          }
1600          atLeastOneLost = 1;
1601          iPocLost=this->getPOC() + pReferencePictureSet->getDeltaPOC(i);
1602        }
1603      }
1604      else if(bUseRecoveryPoint && this->getPOC() > pocRandomAccess)
1605      {
1606        atLeastOneUnabledByRecoveryPoint = 1;
1607      }
1608      else if(bUseRecoveryPoint && (this->getAssociatedIRAPType()==NAL_UNIT_CODED_SLICE_IDR_N_LP || this->getAssociatedIRAPType()==NAL_UNIT_CODED_SLICE_IDR_W_RADL))
1609      {
1610        atLeastOneFlushedByPreviousIDR = 1;
1611      }
1612    }
1613  }
1614  // loop through all short-term pictures in the Reference Picture Set
1615  // to see if the picture should be kept as reference picture
1616  for(i=0;i<pReferencePictureSet->getNumberOfNegativePictures()+pReferencePictureSet->getNumberOfPositivePictures();i++)
1617  {
1618    isAvailable = 0;
1619    // loop through all pictures in the reference picture buffer
1620    TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1621    while ( iterPic != rcListPic.end())
1622    {
1623      rpcPic = *(iterPic++);
1624
1625      if(!rpcPic->getIsLongTerm() && rpcPic->getPicSym()->getSlice(0)->getPOC() == this->getPOC() + pReferencePictureSet->getDeltaPOC(i) && rpcPic->getSlice(0)->isReferenced())
1626      {
1627        if(bUseRecoveryPoint && this->getPOC() > pocRandomAccess && this->getPOC() + pReferencePictureSet->getDeltaPOC(i) < pocRandomAccess)
1628        {
1629          isAvailable = 0;
1630        }
1631        else
1632        {
1633          isAvailable = 1;
1634        }
1635      }
1636    }
1637    // report that a picture is lost if it is in the Reference Picture Set
1638    // but not available as reference picture
1639    if(isAvailable == 0)
1640    {
1641      if (this->getPOC() + pReferencePictureSet->getDeltaPOC(i) >= pocRandomAccess)
1642      {
1643        if(!pReferencePictureSet->getUsed(i) )
1644        {
1645          if(printErrors)
1646          {
1647            printf("\nShort-term reference picture with POC = %3d seems to have been removed or not correctly decoded.", this->getPOC() + pReferencePictureSet->getDeltaPOC(i));
1648          }
1649          atLeastOneRemoved = 1;
1650        }
1651        else
1652        {
1653          if(printErrors)
1654          {
1655            printf("\nShort-term reference picture with POC = %3d is lost or not correctly decoded!", this->getPOC() + pReferencePictureSet->getDeltaPOC(i));
1656          }
1657          atLeastOneLost = 1;
1658          iPocLost=this->getPOC() + pReferencePictureSet->getDeltaPOC(i);
1659        }
1660      }
1661      else if(bUseRecoveryPoint && this->getPOC() > pocRandomAccess)
1662      {
1663        atLeastOneUnabledByRecoveryPoint = 1;
1664      }
1665      else if(bUseRecoveryPoint && (this->getAssociatedIRAPType()==NAL_UNIT_CODED_SLICE_IDR_N_LP || this->getAssociatedIRAPType()==NAL_UNIT_CODED_SLICE_IDR_W_RADL))
1666      {
1667        atLeastOneFlushedByPreviousIDR = 1;
1668      }
1669    }
1670  }
1671
1672  if(atLeastOneUnabledByRecoveryPoint || atLeastOneFlushedByPreviousIDR)
1673  {
1674    return -1;
1675  }   
1676  if(atLeastOneLost)
1677  {
1678    return iPocLost+1;
1679  }
1680  if(atLeastOneRemoved)
1681  {
1682    return -2;
1683  }
1684  else
1685  {
1686    return 0;
1687  }
1688}
1689
1690/** Function for constructing an explicit Reference Picture Set out of the available pictures in a referenced Reference Picture Set
1691*/
1692Void TComSlice::createExplicitReferencePictureSetFromReference( TComList<TComPic*>& rcListPic, const TComReferencePictureSet *pReferencePictureSet, Bool isRAP, Int pocRandomAccess, Bool bUseRecoveryPoint, const Bool bEfficientFieldIRAPEnabled)
1693{
1694  TComPic* rpcPic;
1695  Int i, j;
1696  Int k = 0;
1697  Int nrOfNegativePictures = 0;
1698  Int nrOfPositivePictures = 0;
1699  TComReferencePictureSet* pLocalRPS = this->getLocalRPS();
1700  (*pLocalRPS)=TComReferencePictureSet();
1701
1702  Bool irapIsInRPS = false; // Used when bEfficientFieldIRAPEnabled==true
1703
1704  // loop through all pictures in the Reference Picture Set
1705  for(i=0;i<pReferencePictureSet->getNumberOfPictures();i++)
1706  {
1707    j = 0;
1708    // loop through all pictures in the reference picture buffer
1709    TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1710    while ( iterPic != rcListPic.end())
1711    {
1712      j++;
1713      rpcPic = *(iterPic++);
1714
1715      if(rpcPic->getPicSym()->getSlice(0)->getPOC() == this->getPOC() + pReferencePictureSet->getDeltaPOC(i) && rpcPic->getSlice(0)->isReferenced())
1716      {
1717        // This picture exists as a reference picture
1718        // and should be added to the explicit Reference Picture Set
1719        pLocalRPS->setDeltaPOC(k, pReferencePictureSet->getDeltaPOC(i));
1720        pLocalRPS->setUsed(k, pReferencePictureSet->getUsed(i) && (!isRAP));
1721        if (bEfficientFieldIRAPEnabled)
1722        {
1723          pLocalRPS->setUsed(k, pLocalRPS->getUsed(k) && !(bUseRecoveryPoint && this->getPOC() > pocRandomAccess && this->getPOC() + pReferencePictureSet->getDeltaPOC(i) < pocRandomAccess) );
1724        }
1725
1726        if(pLocalRPS->getDeltaPOC(k) < 0)
1727        {
1728          nrOfNegativePictures++;
1729        }
1730        else
1731        {
1732          if(bEfficientFieldIRAPEnabled && rpcPic->getPicSym()->getSlice(0)->getPOC() == this->getAssociatedIRAPPOC() && this->getAssociatedIRAPPOC() == this->getPOC()+1)
1733          {
1734            irapIsInRPS = true;
1735          }
1736          nrOfPositivePictures++;
1737        }
1738        k++;
1739      }
1740    }
1741  }
1742
1743  Bool useNewRPS = false;
1744  // if current picture is complimentary field associated to IRAP, add the IRAP to its RPS.
1745  if(bEfficientFieldIRAPEnabled && m_pcPic->isField() && !irapIsInRPS)
1746  {
1747    TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1748    while ( iterPic != rcListPic.end())
1749    {
1750      rpcPic = *(iterPic++);
1751      if(rpcPic->getPicSym()->getSlice(0)->getPOC() == this->getAssociatedIRAPPOC() && this->getAssociatedIRAPPOC() == this->getPOC()+1)
1752      {
1753        pLocalRPS->setDeltaPOC(k, 1);
1754        pLocalRPS->setUsed(k, true);
1755        nrOfPositivePictures++;
1756        k ++;
1757        useNewRPS = true;
1758      }
1759    }
1760  }
1761  pLocalRPS->setNumberOfNegativePictures(nrOfNegativePictures);
1762  pLocalRPS->setNumberOfPositivePictures(nrOfPositivePictures);
1763  pLocalRPS->setNumberOfPictures(nrOfNegativePictures+nrOfPositivePictures);
1764  // This is a simplistic inter rps example. A smarter encoder will look for a better reference RPS to do the
1765  // inter RPS prediction with.  Here we just use the reference used by pReferencePictureSet.
1766  // If pReferencePictureSet is not inter_RPS_predicted, then inter_RPS_prediction is for the current RPS also disabled.
1767  if (!pReferencePictureSet->getInterRPSPrediction() || useNewRPS )
1768  {
1769    pLocalRPS->setInterRPSPrediction(false);
1770    pLocalRPS->setNumRefIdc(0);
1771  }
1772  else
1773  {
1774    Int rIdx =  this->getRPSidx() - pReferencePictureSet->getDeltaRIdxMinus1() - 1;
1775    Int deltaRPS = pReferencePictureSet->getDeltaRPS();
1776    const TComReferencePictureSet* pcRefRPS = this->getSPS()->getRPSList()->getReferencePictureSet(rIdx);
1777    Int iRefPics = pcRefRPS->getNumberOfPictures();
1778    Int iNewIdc=0;
1779    for(i=0; i<= iRefPics; i++)
1780    {
1781      Int deltaPOC = ((i != iRefPics)? pcRefRPS->getDeltaPOC(i) : 0);  // check if the reference abs POC is >= 0
1782      Int iRefIdc = 0;
1783      for (j=0; j < pLocalRPS->getNumberOfPictures(); j++) // loop through the  pictures in the new RPS
1784      {
1785        if ( (deltaPOC + deltaRPS) == pLocalRPS->getDeltaPOC(j))
1786        {
1787          if (pLocalRPS->getUsed(j))
1788          {
1789            iRefIdc = 1;
1790          }
1791          else
1792          {
1793            iRefIdc = 2;
1794          }
1795        }
1796      }
1797      pLocalRPS->setRefIdc(i, iRefIdc);
1798      iNewIdc++;
1799    }
1800    pLocalRPS->setInterRPSPrediction(true);
1801    pLocalRPS->setNumRefIdc(iNewIdc);
1802    pLocalRPS->setDeltaRPS(deltaRPS);
1803    pLocalRPS->setDeltaRIdxMinus1(pReferencePictureSet->getDeltaRIdxMinus1() + this->getSPS()->getRPSList()->getNumberOfReferencePictureSets() - this->getRPSidx());
1804  }
1805
1806  this->setRPS(pLocalRPS);
1807  this->setRPSidx(-1);
1808}
1809
1810//! get AC and DC values for weighted pred
1811Void  TComSlice::getWpAcDcParam(WPACDCParam *&wp)
1812{
1813  wp = m_weightACDCParam;
1814}
1815
1816//! init AC and DC values for weighted pred
1817Void  TComSlice::initWpAcDcParam()
1818{
1819  for(Int iComp = 0; iComp < MAX_NUM_COMPONENT; iComp++ )
1820  {
1821    m_weightACDCParam[iComp].iAC = 0;
1822    m_weightACDCParam[iComp].iDC = 0;
1823  }
1824}
1825
1826//! get tables for weighted prediction
1827Void  TComSlice::getWpScaling( RefPicList e, Int iRefIdx, WPScalingParam *&wp )
1828{
1829  assert (e<NUM_REF_PIC_LIST_01);
1830  wp = m_weightPredTable[e][iRefIdx];
1831}
1832
1833//! reset Default WP tables settings : no weight.
1834Void  TComSlice::resetWpScaling()
1835{
1836  for ( Int e=0 ; e<NUM_REF_PIC_LIST_01 ; e++ )
1837  {
1838    for ( Int i=0 ; i<MAX_NUM_REF ; i++ )
1839    {
1840      for ( Int yuv=0 ; yuv<MAX_NUM_COMPONENT ; yuv++ )
1841      {
1842        WPScalingParam  *pwp = &(m_weightPredTable[e][i][yuv]);
1843        pwp->bPresentFlag      = false;
1844        pwp->uiLog2WeightDenom = 0;
1845        pwp->uiLog2WeightDenom = 0;
1846        pwp->iWeight           = 1;
1847        pwp->iOffset           = 0;
1848      }
1849    }
1850  }
1851}
1852
1853//! init WP table
1854Void  TComSlice::initWpScaling(const TComSPS *sps)
1855{
1856  const Bool bUseHighPrecisionPredictionWeighting = sps->getSpsRangeExtension().getHighPrecisionOffsetsEnabledFlag();
1857  for ( Int e=0 ; e<NUM_REF_PIC_LIST_01 ; e++ )
1858  {
1859    for ( Int i=0 ; i<MAX_NUM_REF ; i++ )
1860    {
1861      for ( Int yuv=0 ; yuv<MAX_NUM_COMPONENT ; yuv++ )
1862      {
1863        WPScalingParam  *pwp = &(m_weightPredTable[e][i][yuv]);
1864        if ( !pwp->bPresentFlag )
1865        {
1866          // Inferring values not present :
1867          pwp->iWeight = (1 << pwp->uiLog2WeightDenom);
1868          pwp->iOffset = 0;
1869        }
1870
1871        const Int offsetScalingFactor = bUseHighPrecisionPredictionWeighting ? 1 : (1 << (sps->getBitDepth(toChannelType(ComponentID(yuv)))-8));
1872
1873        pwp->w      = pwp->iWeight;
1874        pwp->o      = pwp->iOffset * offsetScalingFactor; //NOTE: This value of the ".o" variable is never used - .o is set immediately before it gets used
1875        pwp->shift  = pwp->uiLog2WeightDenom;
1876        pwp->round  = (pwp->uiLog2WeightDenom>=1) ? (1 << (pwp->uiLog2WeightDenom-1)) : (0);
1877      }
1878    }
1879  }
1880}
1881
1882// ------------------------------------------------------------------------------------------------
1883// Video parameter set (VPS)
1884// ------------------------------------------------------------------------------------------------
1885TComVPS::TComVPS()
1886: m_VPSId                     (  0)
1887, m_uiMaxTLayers              (  1)
1888#if NH_MV
1889, m_uiMaxLayersMinus1         (  0)
1890#else
1891, m_uiMaxLayers               (  1)
1892#endif
1893, m_bTemporalIdNestingFlag    (false)
1894, m_numHrdParameters          (  0)
1895#if NH_MV
1896, m_maxLayerId             (  0)
1897#else
1898, m_maxNuhReservedZeroLayerId (  0)
1899#endif
1900, m_hrdParameters             ()
1901, m_hrdOpSetIdx               ()
1902, m_cprmsPresentFlag          ()
1903{
1904#if NH_MV
1905  m_vpsBaseLayerInternalFlag = true; 
1906  m_vpsBaseLayerAvailableFlag = true; 
1907
1908  m_numViews = 0; 
1909
1910#endif
1911
1912  for( Int i = 0; i < MAX_TLAYER; i++)
1913  {
1914    m_numReorderPics[i] = 0;
1915    m_uiMaxDecPicBuffering[i] = 1;
1916    m_uiMaxLatencyIncrease[i] = 0;
1917  }
1918#if NH_MV
1919  for (Int lsIdx = 0; lsIdx < MAX_VPS_OP_SETS_PLUS1; lsIdx++ )
1920  { 
1921    for( Int layerId = 0; layerId < MAX_VPS_NUH_LAYER_ID_PLUS1; layerId++ )
1922    {
1923      m_layerIdIncludedFlag[lsIdx][layerId] = (( lsIdx == 0 ) && ( layerId == 0 )) ; 
1924    }
1925  } 
1926  m_vpsNumProfileTierLevelMinus1 = -1; 
1927   
1928  m_numAddLayerSets              = 0;   
1929  m_numAddOlss                   = 0; 
1930  m_defaultOutputLayerIdc     = 0; 
1931 
1932  for ( Int i = 0; i < MAX_VPS_OUTPUTLAYER_SETS; i++)
1933  {
1934    m_layerSetIdxForOlsMinus1[i]  = -1; 
1935    for ( Int j = 0; j < MAX_VPS_NUH_LAYER_ID_PLUS1; j++)
1936    {
1937      m_profileTierLevelIdx[i][j] = -1; 
1938      m_outputLayerFlag[i][j] = false; 
1939    }
1940    m_altOutputLayerFlag[ i ]       = false; 
1941  }
1942
1943  m_repFormatIdxPresentFlag = false; 
1944  m_maxOneActiveRefLayerFlag = false; 
1945  m_vpsPocLsbAlignedFlag  = false; 
1946  m_directDepTypeLenMinus2   = 0;         
1947 
1948
1949  m_vpsExtensionFlag = true; 
1950  m_vpsNonVuiExtensionLength = 0;
1951  m_splittingFlag    = false;
1952
1953 
1954  for( Int i = 0; i < MAX_NUM_SCALABILITY_TYPES; i++ )
1955  {
1956    m_scalabilityMaskFlag[i] = false;
1957    m_dimensionIdLen [i]  = -1; 
1958  }
1959
1960  m_vpsNuhLayerIdPresentFlag = false;
1961
1962  for( Int i = 0; i < MAX_VPS_OP_SETS_PLUS1; i++ )
1963  {
1964    m_vpsProfilePresentFlag   [i] = false;
1965    m_layerSetIdxForOlsMinus1       [i] = 0;
1966    for( Int j = 0; j < MAX_VPS_NUH_LAYER_ID_PLUS1; j++ )
1967    {
1968      m_outputLayerFlag[i][j] = false;
1969    }
1970  }
1971
1972  for( Int i = 0; i < MAX_NUM_LAYER_IDS; i++ )
1973  {
1974    m_layerIdInVps[i] =  (i == 0 ) ? 0 : -1;         
1975  }
1976
1977  for( Int i = 0; i < MAX_NUM_LAYERS; i++ )
1978  {
1979    m_layerIdInNuh      [i] = ( i == 0 ) ? 0 : -1; 
1980    m_numDirectRefLayers[i] = 0; 
1981#if NH_3D
1982    m_numRefListLayers[i] = 0; 
1983#endif
1984    m_vpsRepFormatIdx    [i] = 0; 
1985    m_pocLsbNotPresentFlag[i] = 0;   
1986    m_viewIdVal          [i] = 0; 
1987
1988#if NH_3D
1989    m_viewIndex         [i] = -1; 
1990#endif
1991
1992    for( Int j = 0; j < MAX_NUM_LAYERS; j++ )
1993    {
1994      m_directDependencyFlag[i][j] = false;
1995      m_directDependencyType[i][j] = -1; 
1996      m_dependencyFlag  [i][j]    = false; 
1997      m_idDirectRefLayer[i][j]    = -1; 
1998#if NH_3D
1999      m_idRefListLayer[i][j]    = -1; 
2000#endif
2001      m_idPredictedLayer[i][j]    = -1; 
2002      m_idRefLayer      [i][j]    = -1; 
2003      m_maxTidIlRefPicsPlus1[i][j]  = 7;
2004    }
2005
2006    for( Int j = 0; j < MAX_NUM_SCALABILITY_TYPES; j++ )
2007    {
2008      m_dimensionId[i][j] = 0;
2009    }
2010#if NH_3D_ARP
2011#endif
2012  } 
2013#endif
2014}
2015
2016TComVPS::~TComVPS()
2017{
2018}
2019
2020#if NH_MV
2021
2022Bool TComVPS::checkVPSExtensionSyntax()
2023{
2024  for( Int layer = 1; layer <= getMaxLayersMinus1(); layer++ )
2025  {
2026    // check layer_id_in_nuh constraint
2027    assert( getLayerIdInNuh( layer ) > getLayerIdInNuh( layer -1 ) );
2028  }
2029
2030  //The value of vps_num_rep_formats_minus1 shall be in the range of 0 to 255, inclusive.
2031  assert( getVpsNumRepFormatsMinus1() >= 0 ); 
2032  assert( getVpsNumRepFormatsMinus1() <= 255 ); 
2033
2034  // The value of num_add_layer_sets shall be in the range of 0 to 1023, inclusive.
2035  assert( getNumAddLayerSets() >= 0 && getNumAddLayerSets() <= 1023 ); 
2036  return true; 
2037}
2038
2039Int TComVPS::getNumScalabilityTypes() const
2040{
2041  return scalTypeToScalIdx( ScalabilityType(MAX_NUM_SCALABILITY_TYPES) );
2042}
2043
2044Int TComVPS::scalTypeToScalIdx( ScalabilityType scalType ) const
2045{
2046  assert( (Int)scalType >= 0 && (Int)scalType <= MAX_NUM_SCALABILITY_TYPES );
2047  assert( (Int)scalType == MAX_NUM_SCALABILITY_TYPES || getScalabilityMaskFlag( scalType ) );
2048  Int scalIdx = 0; 
2049  for( Int curScalType = 0; curScalType < scalType; curScalType++ )
2050  {
2051    scalIdx += ( getScalabilityMaskFlag( curScalType ) ? 1 : 0 );
2052
2053  }
2054
2055  return scalIdx; 
2056}
2057Void TComVPS::setScalabilityMaskFlag( UInt val )
2058{
2059  for ( Int scalType = 0; scalType < MAX_NUM_SCALABILITY_TYPES; scalType++ ) 
2060  {
2061    setScalabilityMaskFlag( scalType, ( val & (1 << scalType ) ) != 0 );
2062  }
2063}
2064
2065Void TComVPS::setRefLayers()
2066{
2067
2068  for( Int i = 0; i  <=  getMaxLayersMinus1(); i++ )
2069  {
2070    for( Int j = 0; j  <=  getMaxLayersMinus1(); j++ )
2071    {
2072      m_dependencyFlag[ i ][ j ] = getDirectDependencyFlag( i , j );
2073      for( Int k = 0; k < i; k++ )
2074      {
2075        if( getDirectDependencyFlag(i , k )  &&  m_dependencyFlag[k][j] )
2076        {
2077          m_dependencyFlag[ i ][ j ] = true;
2078        }
2079      }
2080    }
2081  }
2082
2083  for( Int i = 0; i  <=  getMaxLayersMinus1(); i++ )
2084  {
2085    Int iNuhLId = getLayerIdInNuh( i );
2086    Int d = 0;
2087#if NH_3D
2088    Int l = 0; 
2089#endif
2090    Int r = 0;
2091    Int p = 0;
2092
2093    for( Int j = 0; j  <=  getMaxLayersMinus1(); j++ )
2094    {
2095      Int jNuhLid = getLayerIdInNuh( j );
2096      if( getDirectDependencyFlag( i , j ) )
2097      {
2098        m_idDirectRefLayer[iNuhLId][d++] = jNuhLid;
2099      }
2100#if NH_3D
2101      if( getDirectDependencyFlag( i , j ) && ( getDepthId( iNuhLId ) == getDepthId( jNuhLid ) ))
2102      {
2103        m_idRefListLayer [iNuhLId][l++] = jNuhLid;
2104      }
2105#endif
2106
2107      if( getDependencyFlag( i , j ) )
2108      {
2109        m_idRefLayer      [iNuhLId][r++] = jNuhLid;
2110      }
2111      if( getDependencyFlag( j , i ) )
2112      {
2113        m_idPredictedLayer[iNuhLId][p++] = jNuhLid;
2114      }
2115    }
2116    m_numDirectRefLayers[ iNuhLId ] = d;
2117#if NH_3D
2118    m_numRefListLayers[ iNuhLId ] = l; 
2119#endif
2120
2121    m_numRefLayers      [ iNuhLId ] = r;
2122    m_numPredictedLayers[ iNuhLId ] = p;
2123  }
2124 
2125  Bool layerIdInListFlag[ 64 ]; 
2126  for( Int i = 0; i  <=  63; i++ )
2127  {
2128    layerIdInListFlag[ i ] = 0;
2129  }
2130
2131  Int k = 0; 
2132  for( Int i = 0; i  <=  getMaxLayersMinus1(); i++ )
2133  {
2134    Int iNuhLId = getLayerIdInNuh( i );
2135    if( getNumDirectRefLayers( iNuhLId )  ==  0 )
2136    {
2137      m_treePartitionLayerIdList[ k ][ 0 ] = iNuhLId;
2138      Int h = 1; 
2139      for( Int j = 0; j < getNumPredictedLayers( iNuhLId ); j++ ) 
2140      {
2141        Int predLId = getIdPredictedLayer( iNuhLId, j );
2142        if ( !layerIdInListFlag[ predLId ] )
2143        {
2144          m_treePartitionLayerIdList[ k ][ h++ ] = predLId;
2145          layerIdInListFlag[ predLId ] = 1; 
2146        }         
2147      }
2148      m_numLayersInTreePartition[ k++ ] = h;
2149    }
2150  }
2151  m_numIndependentLayers = k;
2152}
2153
2154
2155Void     TComVPS::initNumViews( )
2156{
2157  m_numViews = 1; 
2158#if NH_3D
2159  AOF( m_viewOIdxList.size() == 0 );   
2160  m_viewOIdxList.push_back( 0 );       
2161#endif
2162
2163  for( Int i = 0; i <=  getMaxLayersMinus1(); i++ )
2164  {
2165    Int lId = getLayerIdInNuh( i ); 
2166    if( i > 0 )
2167    {
2168      Bool newViewFlag = true; 
2169      for( Int j = 0; j < i; j++ )
2170      {
2171        if( getViewOrderIdx( lId )  ==  getViewOrderIdx( getLayerIdInNuh( j ) )  )
2172        {
2173          newViewFlag = false;
2174        }
2175      }
2176      if( newViewFlag )
2177      {
2178        m_numViews++;
2179#if NH_3D
2180        m_viewOIdxList.push_back( getViewOrderIdx( lId ) );       
2181#endif
2182      }
2183    }
2184  }
2185}
2186
2187
2188Int TComVPS::getScalabilityId( Int layerIdInVps, ScalabilityType scalType ) const
2189{
2190  return getScalabilityMaskFlag( scalType ) ? getDimensionId( layerIdInVps, scalTypeToScalIdx( scalType ) ) : 0;
2191}
2192
2193#if NH_3D
2194Int TComVPS::getLayerIdInNuh( Int viewIndex, Bool depthFlag ) const
2195{
2196  Int foundLayerIdinNuh = -1; 
2197
2198  for (Int layerIdInVps = 0 ; layerIdInVps <= getMaxLayersMinus1(); layerIdInVps++ )
2199  {
2200    Int layerIdInNuh = getLayerIdInNuh( layerIdInVps ); 
2201    if( ( getViewIndex( layerIdInNuh ) == viewIndex ) && ( getDepthId( layerIdInNuh ) == ( depthFlag ? 1 : 0 ) )  )
2202    {
2203      foundLayerIdinNuh = layerIdInNuh; 
2204      break; 
2205    }
2206  }
2207  return foundLayerIdinNuh;
2208}
2209
2210Void TComVPS::createCamPars(Int iNumViews)
2211{
2212  m_numCp                     .resize( iNumViews );
2213  m_cpRefVoi                  .resize( iNumViews );
2214  m_cpInSliceSegmentHeaderFlag.resize( iNumViews );
2215  m_cpPresentFlag             .resize( iNumViews ); 
2216  m_aaaiCodedScale            .resize( iNumViews ); 
2217  m_aaaiCodedOffset           .resize( iNumViews ); 
2218
2219  for ( Int i = 0; i < iNumViews ; i++ )
2220  {
2221    m_numCp                     [i] = 0; 
2222    m_cpRefVoi                  [i].resize( iNumViews );
2223    m_cpInSliceSegmentHeaderFlag[i] = false;         
2224    m_aaaiCodedScale            [i].resize( 2 );
2225    m_aaaiCodedOffset           [i].resize( 2 );       
2226    m_cpPresentFlag             [i].resize( iNumViews ); 
2227
2228    for ( Int j = 0; j < iNumViews; j++)
2229    {
2230      m_cpRefVoi             [i][j] = 0; 
2231      m_cpPresentFlag        [i][j] = false; 
2232    }
2233
2234    for ( Int j = 0; j < 2; j++ )
2235    {
2236      m_aaaiCodedScale       [i][j].resize( MAX_NUM_LAYERS );
2237      m_aaaiCodedOffset      [i][j].resize( MAX_NUM_LAYERS );
2238
2239      for ( Int k = 0; k < MAX_NUM_LAYERS; k++ )
2240      {
2241        m_aaaiCodedScale [i][j][k] = 0;
2242        m_aaaiCodedOffset[i][j][k] = 0;
2243      }
2244    }
2245  }
2246}
2247#endif // NH_3D
2248
2249
2250Int TComVPS::xGetDimBitOffset( Int j ) const
2251{
2252  Int dimBitOffset = 0; 
2253  if ( getSplittingFlag() && j == getNumScalabilityTypes() )
2254  {
2255     dimBitOffset = 6; 
2256  }
2257  else
2258  {
2259    for (Int dimIdx = 0; dimIdx <= j-1; dimIdx++)
2260    {
2261      dimBitOffset += getDimensionIdLen( dimIdx ); 
2262    }
2263  }
2264  return dimBitOffset; 
2265}
2266
2267Int TComVPS::inferDimensionId( Int i, Int j ) const
2268{
2269    return ( ( getLayerIdInNuh( i ) & ( (1 << xGetDimBitOffset( j + 1 ) ) - 1) ) >> xGetDimBitOffset( j ) ); 
2270}
2271
2272Int TComVPS::inferLastDimsionIdLenMinus1() const
2273{
2274  return ( 5 - xGetDimBitOffset( getNumScalabilityTypes() - 1 ) ); 
2275}
2276
2277Int TComVPS::getNumLayersInIdList( Int lsIdx ) const
2278{
2279  assert( lsIdx >= 0 ); 
2280  assert( lsIdx <= getNumLayerSets() ); 
2281  return (Int) m_layerSetLayerIdList[ lsIdx ].size(); 
2282}
2283
2284Int    TComVPS::getNumOutputLayerSets() const
2285{
2286  return getNumAddOlss() + getNumLayerSets(); 
2287}
2288
2289
2290
2291Void TComVPS::deriveLayerSetLayerIdList()
2292{
2293  m_layerSetLayerIdList.resize( getVpsNumLayerSetsMinus1() + 1 ); 
2294  for (Int i = 0; i <= getVpsNumLayerSetsMinus1(); i++ )
2295  {
2296    for( Int m = 0; m  <= getVpsMaxLayerId(); m++ )
2297    {
2298      if( getLayerIdIncludedFlag( i, m) ) 
2299      {
2300        m_layerSetLayerIdList[ i ].push_back( m );       
2301      }
2302    }
2303  }
2304}
2305
2306Void TComVPS::initTargetLayerIdLists()
2307{
2308  m_targetDecLayerIdLists.resize( getNumOutputLayerSets() ); 
2309  m_targetOptLayerIdLists.resize( getNumOutputLayerSets() ); 
2310}
2311
2312Void TComVPS::deriveTargetLayerIdList( Int i )
2313{ 
2314  Int lsIdx = olsIdxToLsIdx( i );     
2315
2316  for( Int j = 0; j < getNumLayersInIdList( lsIdx ); j++ )
2317  {
2318    if ( getNecessaryLayerFlag( i , j ))
2319    {
2320      m_targetDecLayerIdLists[i].push_back( m_layerSetLayerIdList[ lsIdx ][ j ] ); 
2321    }
2322
2323    if( getOutputLayerFlag( i, j  ))
2324    {
2325      m_targetOptLayerIdLists[i].push_back( m_layerSetLayerIdList[ lsIdx ][ j ] );
2326    }
2327  } 
2328  assert( getNumOutputLayersInOutputLayerSet( i ) > 0 ); 
2329}
2330
2331Bool TComVPS::inferOutputLayerFlag( Int i, Int j ) const
2332{
2333  Bool outputLayerFlag; 
2334  switch ( getDefaultOutputLayerIdc( ) )
2335  {
2336  case 0:
2337    outputLayerFlag = true; 
2338    break; 
2339  case 1:
2340    outputLayerFlag = ( j == m_layerSetLayerIdList[ olsIdxToLsIdx( i ) ].size() - 1 ); 
2341    break;
2342  case 2:
2343    if ( i == 0 && j == 0)
2344    {     
2345      outputLayerFlag = true;  // This is a software only fix for a bug in the spec. In spec outputLayerFlag is neither present nor inferred.
2346    }
2347    else
2348    {
2349      assert( 0 ); 
2350    }
2351    break; 
2352  default:     
2353    assert( 0 );
2354    break; 
2355  }
2356  return outputLayerFlag;
2357}
2358
2359Int TComVPS::getMaxSubLayersInLayerSetMinus1( Int i ) const
2360{
2361  Int maxSLMinus1 = 0; 
2362  for( Int k = 0; k < getNumLayersInIdList( i ); k++ )
2363  {
2364    Int lId = m_layerSetLayerIdList[i][k];
2365    maxSLMinus1 = std::max( maxSLMinus1, getSubLayersVpsMaxMinus1( getLayerIdInVps( lId ) ));
2366  }
2367  return maxSLMinus1;
2368}
2369
2370Bool TComVPS::getAltOutputLayerFlagVar( Int i ) const
2371{
2372  // Semantics variable not syntax element !
2373
2374  Bool altOptLayerFlag = false;     
2375  if ( i > 0 && getNumOutputLayersInOutputLayerSet( i ) == 1 && 
2376    getNumDirectRefLayers( getOlsHighestOutputLayerId( i ) ) > 0 )
2377  {
2378    altOptLayerFlag = getAltOutputLayerFlag( i ); 
2379  }
2380  return altOptLayerFlag;
2381}
2382
2383
2384
2385Int TComVPS::inferProfileTierLevelIdx(Int i, Int j) const
2386{
2387  Bool inferZero        = ( i == 0 && j == 0 &&  getVpsBaseLayerInternalFlag() );
2388  Bool inferGreaterZero = getNecessaryLayerFlag(i,j) && ( getVpsNumProfileTierLevelMinus1() == 0 ); 
2389  assert( inferZero || inferGreaterZero );
2390
2391  Bool ptlIdx = 0; // inference for greaterZero
2392  if ( inferZero )
2393  {
2394    ptlIdx = getMaxLayersMinus1() > 0 ? 1 : 0; 
2395    if ( inferGreaterZero )
2396    {
2397      assert( ptlIdx == 0 ); 
2398      // This should never happen since :
2399      // When vps_max_layers_minus1 is greater than 0, the value of vps_num_profile_tier_level_minus1 shall be greater than or equal to 1.
2400    }
2401  }
2402  return ptlIdx;
2403}
2404
2405Void TComVPS::deriveAddLayerSetLayerIdList(Int i)
2406{
2407  assert( m_layerSetLayerIdList.size() ==  ( getVpsNumLayerSetsMinus1() + 1 + i ) ); 
2408  std::vector<Int> layerSetLayerIdList;
2409
2410  for( Int treeIdx = 1; treeIdx < getNumIndependentLayers(); treeIdx++ )
2411  { 
2412    // The value of highest_layer_idx_plus1[ i ][ j ] shall be in the range of 0 to NumLayersInTreePartition[ j ], inclusive.
2413    assert( getHighestLayerIdxPlus1( i, treeIdx ) >= 0 && getHighestLayerIdxPlus1( i, treeIdx ) <= getNumLayersInTreePartition( treeIdx ) );
2414
2415    for( Int layerCnt = 0; layerCnt < getHighestLayerIdxPlus1( i, treeIdx ); layerCnt++ )
2416    {
2417      layerSetLayerIdList.push_back( getTreePartitionLayerIdList( treeIdx, layerCnt ) );
2418    }
2419  }
2420  m_layerSetLayerIdList.push_back( layerSetLayerIdList ); 
2421
2422  //It is a requirement of bitstream conformance that
2423  //NumLayersInIdList[ vps_num_layer_sets_minus1 + 1 + i ] shall be greater than 0.
2424  assert( getNumLayersInIdList( getVpsNumLayerSetsMinus1() + 1 + i ) > 0 );
2425}
2426
2427
2428Void TComVPS::deriveNecessaryLayerFlags(Int olsIdx)
2429{
2430  AOF( olsIdx >= 0 && olsIdx < getNumOutputLayerSets() ); 
2431  Int lsIdx = olsIdxToLsIdx( olsIdx );
2432  for( Int lsLayerIdx = 0; lsLayerIdx < getNumLayersInIdList( lsIdx) ; lsLayerIdx++ )
2433  {
2434    m_necessaryLayerFlag[ olsIdx ][ lsLayerIdx ] = 0;
2435  }
2436  for( Int lsLayerIdx = 0; lsLayerIdx < getNumLayersInIdList( lsIdx ); lsLayerIdx++ )
2437  {
2438    if( getOutputLayerFlag( olsIdx, lsLayerIdx  ))
2439    {
2440      m_necessaryLayerFlag[ olsIdx ][ lsLayerIdx ] = 1;
2441      Int currLayerId = getLayerSetLayerIdList( lsIdx, lsLayerIdx );
2442      for( Int rLsLayerIdx = 0; rLsLayerIdx < lsLayerIdx; rLsLayerIdx++ )
2443      {
2444        Int refLayerId = getLayerSetLayerIdList( lsIdx, rLsLayerIdx );
2445        if( getDependencyFlag( getLayerIdInVps( currLayerId ), getLayerIdInVps( refLayerId ) ) )
2446        {
2447          m_necessaryLayerFlag[ olsIdx ][ rLsLayerIdx ] = 1;
2448        }
2449      }
2450    }
2451  }
2452  m_numNecessaryLayers[ olsIdx ] = 0;
2453  for( Int lsLayerIdx = 0; lsLayerIdx < getNumLayersInIdList( lsIdx ); lsLayerIdx++ ) 
2454  {
2455    m_numNecessaryLayers[ olsIdx ]  +=  m_necessaryLayerFlag[ olsIdx ][ lsLayerIdx ];
2456  }
2457}
2458
2459Void TComVPS::printPTL() const
2460{
2461  std::vector<Int> idx; 
2462  std::vector<Int> num; 
2463  IntAry2d ptlInfo; 
2464
2465  std::cout << std::right << std::setw(60) << std::setfill('-') << " " << std::setfill(' ') << std::endl << "PTLI" << std::endl; 
2466
2467  for ( Int i = 0; i <= getVpsNumProfileTierLevelMinus1(); i++ )
2468  {
2469    std::vector<Int> curPtlInfo;
2470    const ProfileTierLevel* ptl = getPTL( i )->getGeneralPTL(); 
2471    curPtlInfo.push_back( (Int) ptl->getProfileIdc()  );
2472    curPtlInfo.push_back( (Int) ptl->getTierFlag()    );
2473    curPtlInfo.push_back( (Int) ptl->getLevelIdc()    );
2474    curPtlInfo.push_back( (Int) ptl->getInbldFlag()   );
2475
2476    idx.push_back ( i );
2477    num.push_back ( 4 ); 
2478    ptlInfo.push_back( curPtlInfo );
2479  } 
2480
2481  xPrintArray( "VpsProfileTierLevel", getVpsNumProfileTierLevelMinus1() + 1, idx, num, ptlInfo, false  ); 
2482
2483  num.clear(); 
2484  idx.clear(); 
2485  for (Int i = 0; i < getNumOutputLayerSets(); i++)
2486  {
2487    num.push_back ( getNumLayersInIdList( olsIdxToLsIdx( i ))  ); 
2488    idx.push_back( i ); 
2489  }
2490
2491  xPrintArray( "profile_tier_level_idx", getNumOutputLayerSets(), idx, num, m_profileTierLevelIdx, true );
2492  std::cout << std::endl;
2493}
2494
2495Void TComVPS::printLayerDependencies() const
2496{
2497  vector<Int> fullArray;
2498  vector<Int> range; 
2499
2500#if NH_3D
2501  vector<Int> depthId; 
2502#endif
2503
2504  vector<Int> viewOrderIndex;
2505  vector<Int> auxId;
2506  vector<Int> dependencyId; 
2507  vector<Int> viewId; 
2508  for (Int i = 0; i <= getMaxLayersMinus1(); i++ )
2509  {
2510    fullArray.push_back( getMaxLayersMinus1() + 1 ); 
2511    range.push_back( i ); 
2512    viewOrderIndex.push_back( getViewIndex   ( i ) );
2513    dependencyId  .push_back( getDependencyId( i ) );
2514    auxId         .push_back( getAuxId       ( i ) );     
2515    viewId        .push_back( getViewId      ( getLayerIdInNuh( i ) ) );
2516#if NH_3D 
2517    depthId.push_back( getDepthId( i ) );
2518#endif
2519  }
2520  std::cout << std::right << std::setw(60) << std::setfill('-') << " " << std::setfill(' ') << std::endl << "Layer Dependencies" << std::endl; 
2521  xPrintArray( "direct_dependency_flag", getMaxLayersMinus1()+1, range, fullArray, m_directDependencyFlag, false ); 
2522  xPrintArray( "DependencyFlag", getMaxLayersMinus1()+1, range, fullArray, m_dependencyFlag, false ); 
2523  xPrintArray( "layer_id_in_nuh", getMaxLayersMinus1()+1, m_layerIdInNuh, true  );     
2524  xPrintArray( "IdPredictedLayer", getMaxLayersMinus1() + 1, m_layerIdInNuh, m_numPredictedLayers, m_idPredictedLayer, true );
2525  xPrintArray( "IdRefLayer"      , getMaxLayersMinus1() + 1, m_layerIdInNuh, m_numRefLayers, m_idRefLayer, true );
2526  xPrintArray( "IdDirectRefLayer", getMaxLayersMinus1() + 1, m_layerIdInNuh, m_numDirectRefLayers, m_idDirectRefLayer, true );
2527#if NH_3D
2528  xPrintArray( "IdRefListLayer", getMaxLayersMinus1() + 1, m_layerIdInNuh, m_numRefListLayers, m_idRefListLayer, true );
2529#endif
2530
2531  std::cout << std::endl;
2532}
2533
2534Void TComVPS::printScalabilityId() const
2535{
2536  vector<Int> layerIdxInVps; 
2537
2538
2539  vector<Int> depthId; 
2540  vector<Int> viewOrderIndex;
2541  vector<Int> auxId;
2542  vector<Int> dependencyId; 
2543  vector<Int> viewId; 
2544
2545  for (Int i = 0; i <= getMaxLayersMinus1(); i++ )
2546  {
2547    Int layerIdInNuh = getLayerIdInNuh( i );
2548    layerIdxInVps  .push_back( i ); 
2549    depthId       .push_back( getDepthId     ( layerIdInNuh ) );
2550    viewOrderIndex.push_back( getViewIndex   ( layerIdInNuh ) );
2551    dependencyId  .push_back( getDependencyId( layerIdInNuh ) );
2552    auxId         .push_back( getAuxId       ( layerIdInNuh ) );     
2553    viewId        .push_back( getViewId      ( layerIdInNuh ) );
2554
2555  }
2556
2557  std::cout << std::right << std::setw(60) << std::setfill('-') << " " << std::setfill(' ') << std::endl << "Scalability Ids" << std::endl; 
2558  xPrintArray( "layerIdxInVps"  , getMaxLayersMinus1()+1, layerIdxInVps,          false );
2559  xPrintArray( "layer_id_in_nuh", getMaxLayersMinus1()+1, m_layerIdInNuh, false );     
2560
2561  xPrintArray( "DepthLayerFlag", getMaxLayersMinus1()+1, depthId       , false );     
2562  xPrintArray( "ViewOrderIdx"  , getMaxLayersMinus1()+1, viewOrderIndex, false );     
2563  xPrintArray( "DependencyId"  , getMaxLayersMinus1()+1, dependencyId  , false );     
2564  xPrintArray( "AuxId"         , getMaxLayersMinus1()+1, auxId         , false );     
2565  xPrintArray( "ViewIdVal"     , getMaxLayersMinus1()+1, viewId        , false );     
2566
2567  std::cout << std::endl;
2568}
2569
2570Void TComVPS::printLayerSets() const
2571{
2572  vector<Int> fullArray;
2573  vector<Int> numLayersInIdList; 
2574  vector<Int> rangeLayerSets; 
2575
2576
2577  for (Int i = 0; i < getNumLayerSets(); i++ )
2578  {
2579    numLayersInIdList.push_back( getNumLayersInIdList( i ) );       
2580    rangeLayerSets.push_back( i ); 
2581  }
2582
2583  vector<Int> rangeOutputLayerSets; 
2584  vector<Int> numOutputLayersInOutputLayerSet; 
2585  vector<Int> numDecLayer; 
2586  vector<Int> numLayersInLayerSetForOutputLayerSet; 
2587  vector<Int> vOlsIdxToLsIdx;
2588  for (Int i = 0; i < getNumOutputLayerSets(); i++ )
2589  {
2590    vOlsIdxToLsIdx.push_back( olsIdxToLsIdx(i));
2591    numOutputLayersInOutputLayerSet.push_back( getNumOutputLayersInOutputLayerSet( i ) );       
2592    numDecLayer.push_back( (Int) m_targetDecLayerIdLists[ i ].size() );
2593    rangeOutputLayerSets.push_back( i ); 
2594    numLayersInLayerSetForOutputLayerSet.push_back( getNumLayersInIdList( olsIdxToLsIdx( i ) ) );
2595  }
2596
2597  vector<Int> rangeIndependentLayers;
2598  for(Int i = 0; i < getNumIndependentLayers(); i++ )
2599  {
2600    rangeIndependentLayers.push_back( i );   
2601  }
2602
2603  vector<Int> rangeAddLayerSets;
2604  vector<Int> numHighestLayerIdxPlus1; 
2605  for(Int i = 0; i < getNumAddLayerSets(); i++ )
2606  {
2607    rangeAddLayerSets.push_back( i );   
2608    numHighestLayerIdxPlus1.push_back( getNumIndependentLayers() );
2609  }
2610
2611  std::cout << std::right << std::setw(60) << std::setfill('-') << " " << std::setfill(' ') << std::endl << "Layer Sets" << std::endl;     
2612  xPrintArray( "TreePartitionLayerIdList", getNumIndependentLayers(), rangeIndependentLayers, m_numLayersInTreePartition, m_treePartitionLayerIdList, true );
2613  xPrintArray( "highest_layer_idx_plus1", getNumAddLayerSets(), rangeAddLayerSets, numHighestLayerIdxPlus1, m_highestLayerIdxPlus1, true ); 
2614  xPrintArray( "LayerSetLayerIdList" , (Int) getNumLayerSets()      , rangeLayerSets      , numLayersInIdList, m_layerSetLayerIdList, true );
2615  xPrintArray( "OlsIdxToLsIdx", (Int) vOlsIdxToLsIdx.size(), vOlsIdxToLsIdx, true ); 
2616  xPrintArray( "OutputLayerFlag"     , getNumOutputLayerSets(), rangeOutputLayerSets, numLayersInLayerSetForOutputLayerSet, m_outputLayerFlag, true );
2617  xPrintArray( "TargetOptLayerIdList", getNumOutputLayerSets(), rangeOutputLayerSets, numOutputLayersInOutputLayerSet, m_targetOptLayerIdLists, true );
2618  xPrintArray( "NecessaryLayerFlag"  , getNumOutputLayerSets(), rangeOutputLayerSets, numLayersInLayerSetForOutputLayerSet, m_necessaryLayerFlag   , true );
2619  xPrintArray( "TargetDecLayerIdList", getNumOutputLayerSets(), rangeOutputLayerSets, numDecLayer,                     m_targetDecLayerIdLists, true );
2620  std::cout << endl;
2621}
2622
2623#if NH_3D
2624Void TComVPS::initViewCompLayer()
2625{
2626  assert( m_viewCompLayerId.size() == 0 && m_viewCompLayerPresentFlag.size() == 0  );
2627  for( Int i = 0; i < getNumViews(); i++ )
2628  {
2629    m_viewCompLayerId         .push_back( std::vector<Int>(0)  );
2630    m_viewCompLayerPresentFlag.push_back( std::vector<Bool>(0) );     
2631
2632    for( Int depFlag = 0; depFlag  <=  1; depFlag++ )
2633    {
2634      Int iViewOIdx = getViewOIdxList( i );
2635      Int layerId = -1;
2636      for( Int j = 0; j  <=  getMaxLayersMinus1(); j++ ) 
2637      {
2638        Int jNuhLId = getLayerIdInNuh( j );
2639        if( getVpsDepthFlag( jNuhLId ) == ( (Bool) depFlag )  &&  getViewOrderIdx( jNuhLId )  ==  iViewOIdx 
2640          &&  getDependencyId( jNuhLId )  ==  0  &&  getAuxId( jNuhLId )  ==  0 )
2641        {
2642          layerId = jNuhLId;
2643        }
2644      }
2645      m_viewCompLayerPresentFlag[ i ].push_back( layerId  !=  -1 );
2646      m_viewCompLayerId         [ i ].push_back( layerId );       
2647    }
2648  }
2649}
2650
2651Int TComVPS::getVoiInVps(Int viewOIdx) const
2652{
2653  for ( Int i = 0; i < m_viewOIdxList.size(); i++ )
2654  {
2655    if  ( m_viewOIdxList[ i ] == viewOIdx )
2656    {
2657      return i; 
2658    }
2659  }
2660  assert( 0 );   
2661  return -1;
2662}
2663
2664Void TComVPS::deriveCpPresentFlag()
2665{
2666  for( Int nInVps = 0; nInVps < getNumViews(); nInVps++  )
2667  {
2668    for( Int mInVps = 0; mInVps < getNumViews(); mInVps++ )
2669    {
2670      m_cpPresentFlag[nInVps][mInVps] = 0; 
2671    }
2672  }
2673
2674  for( Int n = 1; n < getNumViews(); n++ )
2675  {
2676    Int iInVps = getVoiInVps(  getViewOIdxList( n ) );     
2677    for( Int m = 0; m < getNumCp( iInVps ); m++ )
2678    {
2679      m_cpPresentFlag[ iInVps ][ getVoiInVps( getCpRefVoi( iInVps, m ) ) ] = 1;
2680    }
2681  }
2682}
2683
2684#endif
2685#endif // NH_MV
2686
2687// ------------------------------------------------------------------------------------------------
2688// Sequence parameter set (SPS)
2689// ------------------------------------------------------------------------------------------------
2690
2691TComSPSRExt::TComSPSRExt()
2692 : m_transformSkipRotationEnabledFlag   (false)
2693 , m_transformSkipContextEnabledFlag    (false)
2694// m_rdpcmEnabledFlag initialized below
2695 , m_extendedPrecisionProcessingFlag    (false)
2696 , m_intraSmoothingDisabledFlag         (false)
2697 , m_highPrecisionOffsetsEnabledFlag    (false)
2698 , m_persistentRiceAdaptationEnabledFlag(false)
2699 , m_cabacBypassAlignmentEnabledFlag    (false)
2700{
2701  for (UInt signallingModeIndex = 0; signallingModeIndex < NUMBER_OF_RDPCM_SIGNALLING_MODES; signallingModeIndex++)
2702  {
2703    m_rdpcmEnabledFlag[signallingModeIndex] = false;
2704  }
2705}
2706
2707TComSPS::TComSPS()
2708: m_SPSId                     (  0)
2709, m_VPSId                     (  0)
2710, m_chromaFormatIdc           (CHROMA_420)
2711, m_uiMaxTLayers              (  1)
2712// Structure
2713, m_picWidthInLumaSamples     (352)
2714, m_picHeightInLumaSamples    (288)
2715, m_log2MinCodingBlockSize    (  0)
2716, m_log2DiffMaxMinCodingBlockSize(0)
2717, m_uiMaxCUWidth              ( 32)
2718, m_uiMaxCUHeight             ( 32)
2719, m_uiMaxTotalCUDepth         (  3)
2720, m_bLongTermRefsPresent      (false)
2721, m_uiQuadtreeTULog2MaxSize   (  0)
2722, m_uiQuadtreeTULog2MinSize   (  0)
2723, m_uiQuadtreeTUMaxDepthInter (  0)
2724, m_uiQuadtreeTUMaxDepthIntra (  0)
2725// Tool list
2726, m_usePCM                    (false)
2727, m_pcmLog2MaxSize            (  5)
2728, m_uiPCMLog2MinSize          (  7)
2729, m_bPCMFilterDisableFlag     (false)
2730, m_uiBitsForPOC              (  8)
2731, m_numLongTermRefPicSPS      (  0)
2732#if NH_MV
2733, m_numShortTermRefPicSets    (   0)
2734#endif
2735, m_uiMaxTrSize               ( 32)
2736, m_bUseSAO                   (false)
2737, m_bTemporalIdNestingFlag    (false)
2738, m_scalingListEnabledFlag    (false)
2739, m_useStrongIntraSmoothing   (false)
2740, m_vuiParametersPresentFlag  (false)
2741, m_vuiParameters             ()
2742#if NH_MV
2743, m_pcVPS                     ( NULL )
2744, m_spsInferScalingListFlag   ( false )
2745, m_spsScalingListRefLayerId  ( 0 )
2746
2747, m_updateRepFormatFlag       ( false ) 
2748, m_spsRepFormatIdx           ( 0 )
2749, m_interViewMvVertConstraintFlag (false)
2750#endif
2751
2752{
2753  for(Int ch=0; ch<MAX_NUM_CHANNEL_TYPE; ch++)
2754  {
2755    m_bitDepths.recon[ch] = 8;
2756#if O0043_BEST_EFFORT_DECODING
2757    m_bitDepths.stream[ch] = 8;
2758#endif
2759    m_pcmBitDepths[ch] = 8;
2760    m_qpBDOffset   [ch] = 0;
2761  }
2762
2763
2764  for ( Int i = 0; i < MAX_TLAYER; i++ )
2765  {
2766#if NH_MV
2767    m_uiSpsMaxLatencyIncreasePlus1[i] = 0;
2768#else
2769    m_uiMaxLatencyIncrease[i] = 0;
2770#endif
2771    m_uiMaxDecPicBuffering[i] = 1;
2772    m_numReorderPics[i]       = 0;
2773  }
2774
2775  ::memset(m_ltRefPicPocLsbSps, 0, sizeof(m_ltRefPicPocLsbSps));
2776  ::memset(m_usedByCurrPicLtSPSFlag, 0, sizeof(m_usedByCurrPicLtSPSFlag));
2777#if NH_MV
2778  m_spsRangeExtensionsFlag     = false;
2779  m_spsMultilayerExtensionFlag = false;
2780  m_spsExtension5bits          = 0;
2781  m_sps3dExtensionFlag         = false; 
2782
2783#endif
2784
2785}
2786
2787TComSPS::~TComSPS()
2788{
2789  m_RPSList.destroy();
2790}
2791
2792Void  TComSPS::createRPSList( Int numRPS )
2793{
2794  m_RPSList.destroy();
2795  m_RPSList.create(numRPS);
2796}
2797
2798const Int TComSPS::m_winUnitX[]={1,2,2,1};
2799const Int TComSPS::m_winUnitY[]={1,2,1,1};
2800
2801TComPPSRExt::TComPPSRExt()
2802: m_log2MaxTransformSkipBlockSize      (2)
2803, m_crossComponentPredictionEnabledFlag(false)
2804, m_diffCuChromaQpOffsetDepth          (0)
2805, m_chromaQpOffsetListLen              (0)
2806// m_ChromaQpAdjTableIncludingNullEntry initialized below
2807// m_log2SaoOffsetScale initialized below
2808{
2809  m_ChromaQpAdjTableIncludingNullEntry[0].u.comp.CbOffset = 0; // Array includes entry [0] for the null offset used when cu_chroma_qp_offset_flag=0. This is initialised here and never subsequently changed.
2810  m_ChromaQpAdjTableIncludingNullEntry[0].u.comp.CrOffset = 0;
2811  for(Int ch=0; ch<MAX_NUM_CHANNEL_TYPE; ch++)
2812  {
2813    m_log2SaoOffsetScale[ch] = 0;
2814  }
2815}
2816
2817TComPPS::TComPPS()
2818: m_PPSId                            (0)
2819, m_SPSId                            (0)
2820, m_picInitQPMinus26                 (0)
2821, m_useDQP                           (false)
2822, m_bConstrainedIntraPred            (false)
2823, m_bSliceChromaQpFlag               (false)
2824, m_uiMaxCuDQPDepth                  (0)
2825, m_chromaCbQpOffset                 (0)
2826, m_chromaCrQpOffset                 (0)
2827, m_numRefIdxL0DefaultActive         (1)
2828, m_numRefIdxL1DefaultActive         (1)
2829, m_TransquantBypassEnableFlag       (false)
2830, m_useTransformSkip                 (false)
2831, m_dependentSliceSegmentsEnabledFlag(false)
2832, m_tilesEnabledFlag                 (false)
2833, m_entropyCodingSyncEnabledFlag     (false)
2834, m_loopFilterAcrossTilesEnabledFlag (true)
2835, m_uniformSpacingFlag               (false)
2836, m_numTileColumnsMinus1             (0)
2837, m_numTileRowsMinus1                (0)
2838, m_signHideFlag                     (false)
2839, m_cabacInitPresentFlag             (false)
2840, m_sliceHeaderExtensionPresentFlag  (false)
2841, m_loopFilterAcrossSlicesEnabledFlag(false)
2842, m_listsModificationPresentFlag     (0)
2843, m_numExtraSliceHeaderBits          (0)
2844#if NH_MV
2845, m_ppsInferScalingListFlag          (false)
2846, m_ppsScalingListRefLayerId         (0)
2847, m_pocResetInfoPresentFlag          (false)
2848#endif
2849#if NH_3D_DLT
2850, m_cDLT                             ()
2851#endif
2852{
2853#if NH_MV
2854  m_ppsRangeExtensionsFlag     = false;
2855  m_ppsMultilayerExtensionFlag = false;
2856  m_pps3dExtensionFlag         = false;
2857  m_ppsExtension5bits          = 0;
2858#endif
2859
2860}
2861
2862TComPPS::~TComPPS()
2863{
2864}
2865
2866#if NH_3D_DLT
2867TComDLT::TComDLT()
2868: m_bDltPresentFlag(false)
2869, m_iNumDepthViews(0)
2870, m_uiDepthViewBitDepth(8)
2871{
2872  for( Int i = 0; i < MAX_NUM_LAYERS; i++ )
2873  {
2874    m_bUseDLTFlag                 [i] = false;
2875    m_bInterViewDltPredEnableFlag [i] = false;
2876    m_bDltBitMapRepFlag           [i] = false;
2877
2878    // allocate some memory and initialize with default mapping
2879    m_iNumDepthmapValues[i] = ((1 << m_uiDepthViewBitDepth)-1)+1;
2880    m_iDepthValue2Idx[i]    = std::vector<Int>(m_iNumDepthmapValues[i]);
2881    m_iIdx2DepthValue[i]    = std::vector<Int>(m_iNumDepthmapValues[i]);
2882   
2883    m_iDepthIdxToLayerId[i] = i;
2884
2885    //default mapping
2886    for (Int d=0; d<m_iNumDepthmapValues[i]; d++)
2887    {
2888      m_iDepthValue2Idx[i][d] = d;
2889      m_iIdx2DepthValue[i][d] = d;
2890    }
2891  }
2892}
2893
2894TComDLT::~TComDLT()
2895{
2896 
2897}
2898
2899Void TComDLT::setDepthLUTs(Int layerIdInVps, std::vector<Int> idxToDepthValueTable, Int iNumDepthValues)
2900{
2901  if( iNumDepthValues == 0 ) // default mapping only
2902    return;
2903
2904  // copy idx2DepthValue to internal array
2905  m_iIdx2DepthValue[layerIdInVps] = idxToDepthValueTable;
2906
2907  UInt uiMaxDepthValue = ((1 << m_uiDepthViewBitDepth)-1);
2908  for(Int p=0; p<=uiMaxDepthValue; p++)
2909  {
2910    Int iIdxDown    = 0;
2911    Int iIdxUp      = iNumDepthValues-1;
2912    Bool bFound     = false;
2913
2914    // iterate over indices to find lower closest depth
2915    Int i = 1;
2916    while(!bFound && i<iNumDepthValues)
2917    {
2918      if( m_iIdx2DepthValue[layerIdInVps][i] > p )
2919      {
2920        iIdxDown  = i-1;
2921        bFound    = true;
2922      }
2923
2924      i++;
2925    }
2926    iIdxUp = bFound ?  iIdxDown + 1 : iNumDepthValues-1;
2927
2928    // assign closer depth value/idx
2929    if( abs(p-m_iIdx2DepthValue[layerIdInVps][iIdxDown]) < abs(p-m_iIdx2DepthValue[layerIdInVps][iIdxUp]) )
2930    {
2931      m_iDepthValue2Idx[layerIdInVps][p] = iIdxDown;
2932    }
2933    else
2934    {
2935      m_iDepthValue2Idx[layerIdInVps][p] = iIdxUp;
2936    }
2937
2938  }
2939
2940  // update DLT variables
2941  m_iNumDepthmapValues[layerIdInVps] = iNumDepthValues;
2942}
2943
2944Void TComDLT::getDeltaDLT( Int layerIdInVps, std::vector<Int> piDLTInRef, UInt uiDLTInRefNum, std::vector<Int>& riDeltaDLTOut, UInt&ruiDeltaDLTOutNum ) const
2945{
2946  Bool abBM0[ 256 ];
2947  Bool abBM1[ 256 ];
2948 
2949  memset( abBM0, 0, sizeof( abBM0 ));
2950  memset( abBM1, 0, sizeof( abBM1 ));
2951 
2952  // convert reference DLT to bit string
2953  for( Int i = 0; i < uiDLTInRefNum; i++ )
2954  {
2955    abBM0[ piDLTInRef[ i ] ] = true;
2956  }
2957  // convert internal DLT to bit string
2958  for( Int i = 0; i < m_iNumDepthmapValues[ layerIdInVps ]; i++ )
2959  {
2960    abBM1[ m_iIdx2DepthValue[ layerIdInVps ][ i ] ] = true;
2961  }
2962 
2963  ruiDeltaDLTOutNum = 0;
2964  for( Int i = 0; i < 256; i++ )
2965  {
2966    if( abBM0[ i ] ^ abBM1[ i ] )
2967    {
2968      riDeltaDLTOut[ ruiDeltaDLTOutNum++ ] = i;
2969    }
2970  }
2971}
2972
2973Void TComDLT::setDeltaDLT( Int layerIdInVps, std::vector<Int> piDLTInRef, UInt uiDLTInRefNum, std::vector<Int> piDeltaDLTIn, UInt uiDeltaDLTInNum )
2974{
2975  Bool abBM0[ 256 ];
2976  Bool abBM1[ 256 ];
2977 
2978  memset( abBM0, 0, sizeof( abBM0 ));
2979  memset( abBM1, 0, sizeof( abBM1 ));
2980 
2981  // convert reference DLT to bit string
2982  for( Int i = 0; i < uiDLTInRefNum; i++ )
2983  {
2984    abBM0[ piDLTInRef[ i ] ] = true;
2985  }
2986  // convert delta DLT to bit string
2987  for( Int i = 0; i < uiDeltaDLTInNum; i++ )
2988  {
2989    abBM1[ piDeltaDLTIn[ i ] ] = true;
2990  }
2991 
2992  std::vector<Int> aiIdx2DepthValue(256, 0);
2993  UInt uiNumDepthValues = 0;
2994  std::fill(aiIdx2DepthValue.begin(), aiIdx2DepthValue.end(), 0);
2995 
2996  for( Int i = 0; i < 256; i++ )
2997  {
2998    if( abBM0[ i ] ^ abBM1[ i ] )
2999    {
3000      aiIdx2DepthValue[ uiNumDepthValues++ ] = i;
3001    }
3002  }
3003 
3004  // update internal tables
3005  setDepthLUTs(layerIdInVps, aiIdx2DepthValue, uiNumDepthValues);
3006}
3007
3008#endif
3009
3010#if NH_MV
3011Void TComSPS::inferRepFormat( TComVPS* vps, Int layerIdCurr, Bool encoder )
3012{
3013  if ( getMultiLayerExtSpsFlag() )
3014  {   
3015    Int            repFormatIdx = getUpdateRepFormatFlag() ?  getSpsRepFormatIdx() : vps->getVpsRepFormatIdx( vps->getLayerIdInVps( layerIdCurr ) ) ;
3016    const TComRepFormat* repFormat    = vps->getRepFormat( repFormatIdx ); 
3017
3018    if ( encoder )
3019    {
3020      assert( getChromaFormatIdc() ==  (ChromaFormat) repFormat->getChromaFormatVpsIdc() );         
3021      //// ToDo: add when supported:
3022      // assert( getSeperateColourPlaneFlag( repFormat->getSeparateColourPlaneVpsFlag() ) ;
3023
3024      assert( getPicWidthInLumaSamples()  ==  repFormat->getPicWidthVpsInLumaSamples()  ); 
3025      assert( getPicHeightInLumaSamples() == repFormat->getPicHeightVpsInLumaSamples() ); 
3026
3027      assert( getBitDepth              ( CHANNEL_TYPE_LUMA ) == repFormat->getBitDepthVpsLumaMinus8()   + 8 ); 
3028      assert( getQpBDOffset            ( CHANNEL_TYPE_LUMA ) == (Int) (6*( getBitDepth( CHANNEL_TYPE_LUMA ) - 8 )) );
3029
3030      assert( getBitDepth              ( CHANNEL_TYPE_CHROMA ) == repFormat->getBitDepthVpsChromaMinus8() + 8 ); 
3031      assert( getQpBDOffset            ( CHANNEL_TYPE_CHROMA ) == (Int) (6* ( getBitDepth( CHANNEL_TYPE_CHROMA ) -8 ) ) );
3032    }
3033    else
3034    {
3035      setChromaFormatIdc( (ChromaFormat) repFormat->getChromaFormatVpsIdc() );         
3036      //// ToDo: add when supported:
3037      // setSeperateColourPlaneFlag( repFormat->getSeparateColourPlaneVpsFlag() ) ;
3038
3039      setPicWidthInLumaSamples ( repFormat->getPicWidthVpsInLumaSamples()  ); 
3040      setPicHeightInLumaSamples( repFormat->getPicHeightVpsInLumaSamples() ); 
3041
3042      setBitDepth              ( CHANNEL_TYPE_LUMA, repFormat->getBitDepthVpsLumaMinus8()   + 8 ); 
3043      setQpBDOffset            ( CHANNEL_TYPE_LUMA, (Int) (6*( getBitDepth( CHANNEL_TYPE_LUMA ) - 8 )) );
3044
3045      setBitDepth              ( CHANNEL_TYPE_CHROMA, repFormat->getBitDepthVpsChromaMinus8() + 8 ); 
3046      setQpBDOffset            ( CHANNEL_TYPE_CHROMA, (Int) (6* ( getBitDepth( CHANNEL_TYPE_CHROMA ) -8 ) ) );
3047      Window &spsConf    = getConformanceWindow();   
3048
3049      // Scaled later
3050      spsConf.setScaledFlag( false ); 
3051      spsConf.setWindowLeftOffset  ( repFormat->getConfWinVpsLeftOffset()    );
3052      spsConf.setWindowRightOffset ( repFormat->getConfWinVpsRightOffset()   );
3053      spsConf.setWindowTopOffset   ( repFormat->getConfWinVpsTopOffset()     );
3054      spsConf.setWindowBottomOffset( repFormat->getConfWinVpsBottomOffset()  );           
3055    }
3056
3057   if ( getMultiLayerExtSpsFlag() && getUpdateRepFormatFlag() )
3058    {
3059      assert( getChromaFormatIdc()      <=  repFormat->getChromaFormatVpsIdc()         ); 
3060      //// ToDo: add when supported:
3061      // assert( getSeperateColourPlaneFlag() <=  repFormat->getSeparateColourPlaneVpsFlag() ) ;
3062
3063      assert( getPicWidthInLumaSamples()  <= repFormat->getPicWidthVpsInLumaSamples()    ); 
3064      assert( getPicHeightInLumaSamples() <= repFormat->getPicHeightVpsInLumaSamples()   ); 
3065
3066      assert( getBitDepth( CHANNEL_TYPE_LUMA   )  <= repFormat->getBitDepthVpsLumaMinus8()   + 8 );         
3067      assert( getBitDepth( CHANNEL_TYPE_CHROMA )  <= repFormat->getBitDepthVpsChromaMinus8() + 8 ); 
3068    }
3069  }
3070
3071  // Set conformance window
3072  Int scal = TComSPS::getWinUnitX( getChromaFormatIdc() ) ;
3073  getConformanceWindow().scaleOffsets( scal );
3074  getVuiParameters()->getDefaultDisplayWindow().scaleOffsets( scal );
3075
3076  if (encoder && getMultiLayerExtSpsFlag() )
3077  { 
3078    Int            repFormatIdx = getUpdateRepFormatFlag() ?  getSpsRepFormatIdx() : vps->getVpsRepFormatIdx( vps->getLayerIdInVps( layerIdCurr ) ) ;
3079    const TComRepFormat* repFormat    = vps->getRepFormat( repFormatIdx ); 
3080
3081    Window &spsConf    = getConformanceWindow();   
3082    assert( spsConf.getWindowLeftOffset  () == repFormat->getConfWinVpsLeftOffset()    );
3083    assert( spsConf.getWindowRightOffset () == repFormat->getConfWinVpsRightOffset()   );
3084    assert( spsConf.getWindowTopOffset   () == repFormat->getConfWinVpsTopOffset()     );
3085    assert( spsConf.getWindowBottomOffset() == repFormat->getConfWinVpsBottomOffset()  );           
3086  }
3087}
3088
3089Void TComSPS::inferScalingList( const TComSPS* spsSrc )
3090{
3091  if ( getSpsInferScalingListFlag() ) 
3092  {
3093    assert( spsSrc != NULL ); 
3094    assert( !spsSrc->getSpsInferScalingListFlag() );             
3095    getScalingList().inferFrom( (spsSrc->getScalingList()) ); 
3096  }
3097}
3098
3099Void TComSPS::inferSpsMaxDecPicBufferingMinus1( TComVPS* vps, Int targetOptLayerSetIdx, Int currLayerId, Bool encoder )
3100{
3101  if ( getMultiLayerExtSpsFlag() )
3102  {
3103    const std::vector<Int>& targetDecLayerIdList = vps->getTargetDecLayerIdList( vps->olsIdxToLsIdx( targetOptLayerSetIdx )); 
3104    Int layerIdx = 0;         
3105    while (layerIdx < (Int) targetDecLayerIdList.size() )
3106    {
3107      if ( targetDecLayerIdList[layerIdx] == currLayerId )
3108      { 
3109        break; 
3110      }
3111      layerIdx++; 
3112    }
3113
3114    assert( layerIdx < (Int) targetDecLayerIdList.size() ); 
3115
3116    for (Int i = 0; i <= getSpsMaxSubLayersMinus1(); i++ ) 
3117    {
3118      Int maxDecPicBufferingMinus1 = vps->getDpbSize()->getMaxVpsDecPicBufferingMinus1( targetOptLayerSetIdx, layerIdx, i ) ; 
3119
3120      // This preliminary fix needs to be checked.
3121      Int maxNumReorderPics       = vps->getDpbSize()->getMaxVpsNumReorderPics( targetOptLayerSetIdx, i ); 
3122      Int maxLatencyIncreasePlus1 = vps->getDpbSize()->getMaxVpsLatencyIncreasePlus1( targetOptLayerSetIdx, i ); 
3123      if ( encoder )     
3124      {
3125        assert( getMaxDecPicBuffering( i ) - 1 == maxDecPicBufferingMinus1 ); 
3126        // This preliminary fix needs to be checked.
3127        assert( getNumReorderPics( i )     == maxNumReorderPics       ); 
3128        assert( getSpsMaxLatencyIncreasePlus1( i ) == maxLatencyIncreasePlus1 ); 
3129
3130      }
3131      else
3132      {
3133        // This preliminary fix needs to be checked.
3134        setMaxDecPicBuffering( maxDecPicBufferingMinus1 + 1 , i); 
3135        setNumReorderPics    ( maxNumReorderPics, i );
3136        setSpsMaxLatencyIncreasePlus1( maxLatencyIncreasePlus1 , i); 
3137      }
3138    }   
3139  }
3140}
3141
3142Void TComSPS::checkRpsMaxNumPics( const TComVPS* vps, Int currLayerId ) const
3143{
3144  for (Int i = 0; i < getRPSList()->getNumberOfReferencePictureSets(); i++ )
3145  {
3146    const TComReferencePictureSet* rps = getRPSList()->getReferencePictureSet( i ); 
3147    if ( !rps->getInterRPSPrediction() )
3148    {
3149      rps->checkMaxNumPics( vps->getVpsExtensionFlag(), MAX_INT, getLayerId(), getMaxDecPicBuffering( getSpsMaxSubLayersMinus1() ) - 1 );   // INT_MAX to be replaced by DpbSize
3150    }
3151  }
3152}
3153
3154Void TComSPS::inferSpsMaxSubLayersMinus1(Bool atPsActivation, TComVPS* vps)
3155{
3156  assert( getLayerId() != 0 ); 
3157  if ( !atPsActivation   )
3158  {
3159    assert( vps == NULL );
3160    if (getSpsExtOrMaxSubLayersMinus1() != 7)
3161    {
3162      setSpsMaxSubLayersMinus1( getSpsExtOrMaxSubLayersMinus1() );
3163    }
3164  }
3165  else
3166  {
3167    assert( vps != NULL );
3168    if (getSpsExtOrMaxSubLayersMinus1() == 7)
3169    {
3170      setSpsMaxSubLayersMinus1( vps->getMaxSubLayersMinus1() );
3171    }
3172  }
3173}
3174#endif
3175
3176
3177TComReferencePictureSet::TComReferencePictureSet()
3178: m_numberOfPictures (0)
3179, m_numberOfNegativePictures (0)
3180, m_numberOfPositivePictures (0)
3181, m_numberOfLongtermPictures (0)
3182, m_interRPSPrediction (0)
3183, m_deltaRIdxMinus1 (0)
3184, m_deltaRPS (0)
3185, m_numRefIdc (0)
3186{
3187  ::memset( m_deltaPOC, 0, sizeof(m_deltaPOC) );
3188  ::memset( m_POC, 0, sizeof(m_POC) );
3189  ::memset( m_used, 0, sizeof(m_used) );
3190  ::memset( m_refIdc, 0, sizeof(m_refIdc) );
3191  ::memset( m_bCheckLTMSB, 0, sizeof(m_bCheckLTMSB) );
3192  ::memset( m_pocLSBLT, 0, sizeof(m_pocLSBLT) );
3193  ::memset( m_deltaPOCMSBCycleLT, 0, sizeof(m_deltaPOCMSBCycleLT) );
3194  ::memset( m_deltaPocMSBPresentFlag, 0, sizeof(m_deltaPocMSBPresentFlag) );
3195}
3196
3197TComReferencePictureSet::~TComReferencePictureSet()
3198{
3199}
3200
3201Void TComReferencePictureSet::setUsed(Int bufferNum, Bool used)
3202{
3203  m_used[bufferNum] = used;
3204}
3205
3206Void TComReferencePictureSet::setDeltaPOC(Int bufferNum, Int deltaPOC)
3207{
3208  m_deltaPOC[bufferNum] = deltaPOC;
3209}
3210
3211Void TComReferencePictureSet::setNumberOfPictures(Int numberOfPictures)
3212{
3213  m_numberOfPictures = numberOfPictures;
3214}
3215
3216Int TComReferencePictureSet::getUsed(Int bufferNum) const
3217{
3218  return m_used[bufferNum];
3219}
3220
3221Int TComReferencePictureSet::getDeltaPOC(Int bufferNum) const
3222{
3223  return m_deltaPOC[bufferNum];
3224}
3225
3226Int TComReferencePictureSet::getNumberOfPictures() const
3227{
3228  return m_numberOfPictures;
3229}
3230
3231Int TComReferencePictureSet::getPOC(Int bufferNum) const
3232{
3233  return m_POC[bufferNum];
3234}
3235
3236Void TComReferencePictureSet::setPOC(Int bufferNum, Int POC)
3237{
3238  m_POC[bufferNum] = POC;
3239}
3240
3241Bool TComReferencePictureSet::getCheckLTMSBPresent(Int bufferNum) const
3242{
3243  return m_bCheckLTMSB[bufferNum];
3244}
3245
3246Void TComReferencePictureSet::setCheckLTMSBPresent(Int bufferNum, Bool b)
3247{
3248  m_bCheckLTMSB[bufferNum] = b;
3249}
3250
3251//! set the reference idc value at uiBufferNum entry to the value of iRefIdc
3252Void TComReferencePictureSet::setRefIdc(Int bufferNum, Int refIdc)
3253{
3254  m_refIdc[bufferNum] = refIdc;
3255}
3256
3257//! get the reference idc value at uiBufferNum
3258Int  TComReferencePictureSet::getRefIdc(Int bufferNum) const
3259{
3260  return m_refIdc[bufferNum];
3261}
3262
3263/** Sorts the deltaPOC and Used by current values in the RPS based on the deltaPOC values.
3264 *  deltaPOC values are sorted with -ve values before the +ve values.  -ve values are in decreasing order.
3265 *  +ve values are in increasing order.
3266 * \returns Void
3267 */
3268Void TComReferencePictureSet::sortDeltaPOC()
3269{
3270  // sort in increasing order (smallest first)
3271  for(Int j=1; j < getNumberOfPictures(); j++)
3272  {
3273    Int deltaPOC = getDeltaPOC(j);
3274    Bool used = getUsed(j);
3275    for (Int k=j-1; k >= 0; k--)
3276    {
3277      Int temp = getDeltaPOC(k);
3278      if (deltaPOC < temp)
3279      {
3280        setDeltaPOC(k+1, temp);
3281        setUsed(k+1, getUsed(k));
3282        setDeltaPOC(k, deltaPOC);
3283        setUsed(k, used);
3284      }
3285    }
3286  }
3287  // flip the negative values to largest first
3288  Int numNegPics = getNumberOfNegativePictures();
3289  for(Int j=0, k=numNegPics-1; j < numNegPics>>1; j++, k--)
3290  {
3291    Int deltaPOC = getDeltaPOC(j);
3292    Bool used = getUsed(j);
3293    setDeltaPOC(j, getDeltaPOC(k));
3294    setUsed(j, getUsed(k));
3295    setDeltaPOC(k, deltaPOC);
3296    setUsed(k, used);
3297  }
3298}
3299
3300/** Prints the deltaPOC and RefIdc (if available) values in the RPS.
3301 *  A "*" is added to the deltaPOC value if it is Used bu current.
3302 * \returns Void
3303 */
3304Void TComReferencePictureSet::printDeltaPOC() const
3305{
3306  printf("DeltaPOC = { ");
3307  for(Int j=0; j < getNumberOfPictures(); j++)
3308  {
3309    printf("%d%s ", getDeltaPOC(j), (getUsed(j)==1)?"*":"");
3310  }
3311  if (getInterRPSPrediction())
3312  {
3313    printf("}, RefIdc = { ");
3314    for(Int j=0; j < getNumRefIdc(); j++)
3315    {
3316      printf("%d ", getRefIdc(j));
3317    }
3318  }
3319  printf("}\n");
3320}
3321
3322#if NH_MV
3323Void TComReferencePictureSet::checkMaxNumPics( Bool vpsExtensionFlag, Int maxNumPics, Int nuhLayerId, Int spsMaxDecPicBufferingMinus1 ) const
3324{
3325  assert( getNumberOfPictures() >= 0 ); 
3326  if ( nuhLayerId == 0 )
3327  {
3328    assert( getNumberOfPictures() <= spsMaxDecPicBufferingMinus1 ); 
3329  }
3330
3331  if ( vpsExtensionFlag )
3332  {
3333    assert( getNumberOfPictures() <= maxNumPics );
3334  }
3335}
3336#endif
3337
3338
3339
3340
3341TComRefPicListModification::TComRefPicListModification()
3342: m_refPicListModificationFlagL0 (false)
3343, m_refPicListModificationFlagL1 (false)
3344{
3345  ::memset( m_RefPicSetIdxL0, 0, sizeof(m_RefPicSetIdxL0) );
3346  ::memset( m_RefPicSetIdxL1, 0, sizeof(m_RefPicSetIdxL1) );
3347}
3348
3349TComRefPicListModification::~TComRefPicListModification()
3350{
3351}
3352
3353TComScalingList::TComScalingList()
3354{
3355  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
3356  {
3357    for(UInt listId = 0; listId < SCALING_LIST_NUM; listId++)
3358    {
3359      m_scalingListCoef[sizeId][listId].resize(min<Int>(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId]));
3360    }
3361  }
3362}
3363
3364/** set default quantization matrix to array
3365*/
3366Void TComScalingList::setDefaultScalingList()
3367{
3368  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
3369  {
3370    for(UInt listId=0;listId<SCALING_LIST_NUM;listId++)
3371    {
3372      processDefaultMatrix(sizeId, listId);
3373    }
3374  }
3375}
3376/** check if use default quantization matrix
3377 * \returns true if use default quantization matrix in all size
3378*/
3379Bool TComScalingList::checkDefaultScalingList()
3380{
3381  UInt defaultCounter=0;
3382
3383  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
3384  {
3385    for(UInt listId=0;listId<SCALING_LIST_NUM;listId++)
3386    {
3387      if( !memcmp(getScalingListAddress(sizeId,listId), getScalingListDefaultAddress(sizeId, listId),sizeof(Int)*min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId])) // check value of matrix
3388     && ((sizeId < SCALING_LIST_16x16) || (getScalingListDC(sizeId,listId) == 16))) // check DC value
3389      {
3390        defaultCounter++;
3391      }
3392    }
3393  }
3394
3395  return (defaultCounter == (SCALING_LIST_NUM * SCALING_LIST_SIZE_NUM )) ? false : true;
3396}
3397
3398#if NH_MV
3399Void TComSlice::createInterLayerReferencePictureSet( TComPicLists* ivPicLists, std::vector<TComPic*>& refPicSetInterLayer0, std::vector<TComPic*>& refPicSetInterLayer1 )
3400{
3401  refPicSetInterLayer0.clear(); 
3402  refPicSetInterLayer1.clear(); 
3403
3404  for( Int i = 0; i < getNumActiveRefLayerPics(); i++ ) 
3405  {
3406    Int layerIdRef = getRefPicLayerId( i ); 
3407    TComPic* picRef = ivPicLists->getPic( layerIdRef, getPOC() ) ; 
3408    assert ( picRef != 0 ); // There shall be no entry equal to "no reference picture" in RefPicSetInterLayer0 or RefPicSetInterLayer1.
3409
3410    picRef->getPicYuvRec()->extendPicBorder(); 
3411    picRef->setIsLongTerm( true );       
3412    picRef->getSlice(0)->setReferenced( true );       
3413
3414    Int viewIdCur  = getVPS()->getViewId( getLayerId() ); 
3415    Int viewIdZero = getVPS()->getViewId( 0 );
3416    Int viewIdRef  = getVPS()->getViewId( layerIdRef ); 
3417
3418    if (  ( viewIdCur <= viewIdZero && viewIdCur <= viewIdRef ) || ( viewIdCur >= viewIdZero && viewIdCur >= viewIdRef ) )
3419    {
3420      refPicSetInterLayer0.push_back( picRef ); 
3421    }
3422    else
3423    {
3424      refPicSetInterLayer1.push_back( picRef ); 
3425    }
3426    // Consider to check here:
3427    // "If the current picture is a RADL picture, there shall be no entry in the RefPicSetInterLayer0 and RefPicSetInterLayer1 that is a RASL picture. "   
3428    assert( picRef->getSlice(0)->getDiscardableFlag() == false ); // "There shall be no picture that has discardable_flag equal to 1 in RefPicSetInterLayer0 or RefPicSetInterLayer1".       
3429  }
3430}
3431Void TComSlice::markIvRefPicsAsShortTerm( std::vector<TComPic*> refPicSetInterLayer0, std::vector<TComPic*> refPicSetInterLayer1 )
3432{
3433  // Mark as short-term
3434  for ( Int i = 0; i < refPicSetInterLayer0.size(); i++ ) 
3435  {
3436    refPicSetInterLayer0[i]->setIsLongTerm( false ); 
3437  }
3438
3439  for ( Int i = 0; i < refPicSetInterLayer1.size(); i++ ) 
3440  {
3441    refPicSetInterLayer1[i]->setIsLongTerm( false ); 
3442  }
3443
3444}
3445
3446Void TComSlice::printRefPicList()
3447{ 
3448  for ( Int li = 0; li < 2; li++)
3449  {   
3450    std::cout << std::endl << "RefPicListL" <<  li << ":" << std::endl; 
3451    for (Int rIdx = 0; rIdx <= (m_aiNumRefIdx[li]-1); rIdx ++)
3452    {     
3453      if (rIdx == 0 && li == 0) m_apcRefPicList[li][rIdx]->print( 1 );
3454      m_apcRefPicList[li][rIdx]->print( 0 );     
3455       
3456    }
3457  }
3458}
3459
3460Void TComSlice::markCurrPic( TComPic* currPic )
3461{
3462  currPic->getSlice(0)->setReferenced( true ) ; 
3463  currPic->setIsLongTerm( false ); 
3464
3465  currPic->setReconMark( true );
3466  currPic->setPicOutputFlag( currPic->getSlice(0)->getPicOutputFlag() );
3467}
3468
3469Void TComSlice::setRefPicSetInterLayer( std::vector<TComPic*>* refPicSetInterLayer0, std::vector<TComPic*>* refPicSetInterLayer1 )
3470{
3471  m_refPicSetInterLayer0 = refPicSetInterLayer0; 
3472  m_refPicSetInterLayer1 = refPicSetInterLayer1; 
3473}
3474
3475TComPic* TComSlice::getRefPicSetInterLayer( Int setIdc, Int i ) const
3476{
3477  TComPic* pic = NULL; 
3478  if (setIdc == 0 )
3479  {
3480    pic = (*m_refPicSetInterLayer0)[ i ]; 
3481  }
3482  else if (setIdc == 1 )
3483  {
3484    pic = (*m_refPicSetInterLayer1)[ i ]; 
3485  }
3486
3487  assert( pic != NULL );   
3488
3489  return pic; 
3490}
3491
3492
3493TComPic* TComSlice::getPicFromRefPicSetInterLayer(Int setIdc, Int layerId ) const
3494{
3495  assert ( setIdc == 0 || setIdc == 1);   
3496  std::vector<TComPic*>* refPicSetInterLayer = ( setIdc == 0 ? m_refPicSetInterLayer0 : m_refPicSetInterLayer1);   
3497  assert( refPicSetInterLayer != 0 ); 
3498 
3499  TComPic* pcPic = NULL; 
3500  for ( Int i = 0; i < (*refPicSetInterLayer).size(); i++ )
3501  {
3502    if ((*refPicSetInterLayer)[ i ]->getLayerId() == layerId )
3503    {
3504      pcPic = (*refPicSetInterLayer)[ i ]; 
3505    }
3506  }
3507
3508  assert(pcPic != NULL); 
3509  return pcPic;
3510}
3511
3512
3513Int  TComSlice::getRefLayerPicFlag( Int i ) const
3514{
3515  const TComVPS* vps = getVPS(); 
3516#if NH_3D
3517  Int refLayerIdx = vps->getLayerIdInVps( vps->getIdRefListLayer( getLayerId(), i ) ); 
3518#else
3519  Int refLayerIdx = vps->getLayerIdInVps( vps->getIdDirectRefLayer( getLayerId(), i ) ); 
3520#endif
3521
3522  Bool refLayerPicFlag = ( vps->getSubLayersVpsMaxMinus1( refLayerIdx ) >=  getTLayer()  && ( getTLayer() == 0   ||
3523    vps->getMaxTidIlRefPicsPlus1( refLayerIdx, vps->getLayerIdInVps( getLayerId() )) > getTLayer() )); 
3524  return refLayerPicFlag;       
3525}   
3526
3527Int TComSlice::getRefLayerPicIdc( Int j ) const
3528{ 
3529  Int refLayerPicIdc = -1; 
3530  Int curj = 0; 
3531#if NH_3D
3532  for( Int i = 0;  i < getVPS()->getNumRefListLayers( getLayerId()) ; i++ )
3533#else
3534  for( Int i = 0;  i < getVPS()->getNumDirectRefLayers( getLayerId()) ; i++ )
3535#endif
3536  {
3537    if( getRefLayerPicFlag( i ) )
3538    {
3539      if ( curj == j ) 
3540      {
3541        refLayerPicIdc = i;         
3542        break;
3543      }
3544      curj++; 
3545    }
3546  }
3547
3548  assert( curj == j ); 
3549  assert( refLayerPicIdc != -1 ); 
3550  return refLayerPicIdc; 
3551}
3552
3553Int  TComSlice::getNumRefLayerPics( ) const
3554{ 
3555  Int numRefLayerPics = 0; 
3556#if NH_3D
3557  for( Int i = 0;  i < getVPS()->getNumRefListLayers( getLayerId()) ; i++ )
3558#else
3559  for( Int i = 0;  i < getVPS()->getNumDirectRefLayers( getLayerId()) ; i++ )
3560#endif
3561  {
3562    numRefLayerPics += getRefLayerPicFlag( i ); 
3563  }
3564  return numRefLayerPics; 
3565}
3566
3567
3568
3569Int TComSlice::getNumActiveRefLayerPics() const
3570{
3571  Int numActiveRefLayerPics; 
3572
3573  if( getLayerId() == 0 || getNumRefLayerPics() ==  0 )
3574  {
3575    numActiveRefLayerPics = 0; 
3576  }
3577  else if (getVPS()->getAllRefLayersActiveFlag() )
3578  {
3579    numActiveRefLayerPics = getNumRefLayerPics(); 
3580  }
3581  else if ( !getInterLayerPredEnabledFlag() )
3582  {
3583    numActiveRefLayerPics = 0; 
3584  }
3585#if NH_3D
3586  else if( getVPS()->getMaxOneActiveRefLayerFlag() || getVPS()->getNumRefListLayers( getLayerId() ) == 1 )
3587#else
3588  else if( getVPS()->getMaxOneActiveRefLayerFlag() || getVPS()->getNumDirectRefLayers( getLayerId() ) == 1 )
3589#endif
3590  {
3591    numActiveRefLayerPics = 1; 
3592  }
3593  else
3594  {
3595    numActiveRefLayerPics = getNumInterLayerRefPicsMinus1() + 1; 
3596  }
3597  return numActiveRefLayerPics;
3598}
3599
3600Int TComSlice::getRefPicLayerId( Int i ) const
3601{
3602#if NH_3D
3603  return getVPS()->getIdRefListLayer( getLayerId(), getInterLayerPredLayerIdc( i ) );
3604#else
3605  return getVPS()->getIdDirectRefLayer( getLayerId(), getInterLayerPredLayerIdc( i ) );
3606#endif
3607}
3608#endif
3609#if NH_3D_NBDV
3610Void TComSlice::setDefaultRefView()
3611{
3612  setDefaultRefViewIdx(-1);
3613  setDefaultRefViewIdxAvailableFlag(false); 
3614
3615  Int valid = 0;
3616  Int DefaultRefViewIdx = -1;
3617
3618  for(UInt curViewIdx = 0; curViewIdx < getViewIndex() && valid == 0; curViewIdx++)
3619  {
3620    for(Int iRefListId = 0; (iRefListId < (isInterB() ? 2 : 1)) && !isIntra() && valid == 0; iRefListId++)
3621    {
3622      RefPicList eRefPicList = RefPicList(iRefListId);
3623      Int        iNumRefPics = getNumRefIdx(eRefPicList);
3624
3625      for(Int i = 0; i < iNumRefPics; i++)
3626      { 
3627        if(getPOC() == getRefPic(eRefPicList, i)->getPOC() && curViewIdx == getRefPic(eRefPicList, i)->getViewIndex())
3628        {
3629          valid = 1;
3630          DefaultRefViewIdx = curViewIdx;
3631          break;
3632        }
3633      }
3634    }
3635  }
3636
3637  if(valid)
3638  {
3639    setDefaultRefViewIdx(DefaultRefViewIdx);
3640    setDefaultRefViewIdxAvailableFlag(true);
3641  }
3642}
3643#endif
3644
3645#if NH_3D_ARP
3646Void TComSlice::setARPStepNum( TComPicLists*ivPicLists )
3647{
3648  Bool tempRefPicInListsFlag = false;
3649  if( !getIvResPredFlag() || this->isIRAP())
3650  {
3651    m_nARPStepNum = 0;
3652  }
3653  else
3654  {
3655    setFirstTRefIdx (REF_PIC_LIST_0, -1);
3656    setFirstTRefIdx (REF_PIC_LIST_1, -1);
3657    for ( Int refListIdx = 0; refListIdx < ((m_eSliceType==B_SLICE) ? 2 : 1); refListIdx++ )
3658    {
3659      Int diffPOC=MAX_INT;
3660      Int idx=-1;
3661      for(Int i = 0; i < getNumRefIdx(RefPicList(refListIdx)); i++ )
3662      {
3663        if ( getRefPic(RefPicList(refListIdx), i)->getPOC() != getPOC() )
3664        {
3665          if( abs(getRefPic(RefPicList(refListIdx), i)->getPOC() - getPOC()) < diffPOC)
3666          {
3667            diffPOC=abs(getRefPic(RefPicList(refListIdx), i)->getPOC() - getPOC());
3668            idx=i;
3669          }
3670        }
3671        if(idx>=0)
3672        {
3673          setFirstTRefIdx (RefPicList(refListIdx), idx);
3674        }
3675      }
3676    }
3677    tempRefPicInListsFlag = (getFirstTRefIdx(REF_PIC_LIST_0) >= 0 || getFirstTRefIdx(REF_PIC_LIST_1) >= 0) && getDefaultRefViewIdxAvailableFlag();
3678    m_nARPStepNum = tempRefPicInListsFlag ?  H_3D_ARP_WFNR : 0;
3679  }
3680
3681  if (tempRefPicInListsFlag)
3682  {
3683    for ( Int refListIdx = 0; refListIdx < ((m_eSliceType==B_SLICE) ? 2 : 1); refListIdx++ )
3684    {
3685      RefPicList eRefPicList = RefPicList( refListIdx );
3686      Int prevPOC = getRefPic(eRefPicList, getFirstTRefIdx(eRefPicList) )->getPOC();
3687      for( Int i = 0; i < getNumActiveRefLayerPics(); i++ )
3688      {
3689        Int layerIdInNuh = getRefPicLayerId( i );
3690
3691        TComPic* picV = getIvPic( getIsDepth(), getVPS()->getViewIndex( layerIdInNuh ) );
3692        assert( picV != NULL ); 
3693        IntAry1d pocsInCurrRPSsPicV = picV->getSlice(0)->getPocsInCurrRPSs(); 
3694        Bool refRpRefAvailFlag = false; 
3695        for (Int idx = 0; idx < pocsInCurrRPSsPicV.size(); idx++)
3696        {
3697          if ( pocsInCurrRPSsPicV[idx] == prevPOC )
3698          {
3699            refRpRefAvailFlag = true; 
3700            break; 
3701          }
3702        }
3703
3704        if (getFirstTRefIdx(eRefPicList) >= 0 && refRpRefAvailFlag )
3705        {
3706          m_arpRefPicAvailable[eRefPicList][layerIdInNuh] = true;
3707        }
3708        else
3709        {
3710          m_arpRefPicAvailable[eRefPicList][layerIdInNuh] = false;
3711        }
3712      }
3713    }
3714  }
3715  if( m_nARPStepNum > 1)
3716  {
3717    for(Int i = 0; i < getNumActiveRefLayerPics(); i ++ )
3718    {
3719      Int  iLayerId = getRefPicLayerId( i );
3720      Int  iViewIdx =   getVPS()->getViewIndex(iLayerId);
3721      Bool bIsDepth = ( getVPS()->getDepthId  ( iLayerId ) == 1 );
3722      if( iViewIdx<getViewIndex() && !bIsDepth )
3723      {
3724        setBaseViewRefPicList( ivPicLists->getSubDpb( iLayerId, false ), iViewIdx );
3725      }
3726    }
3727  }
3728}
3729#endif
3730
3731#if NH_3D_IC
3732// This is an encoder only function and should be moved to TEncSlice or TEncSearch!!
3733Void TComSlice::xSetApplyIC(Bool bUseLowLatencyICEnc)
3734{
3735  if(bUseLowLatencyICEnc)
3736  {
3737    Bool existInterViewRef=false;
3738    TComPic* pcCurrPic = getPic();
3739    TComPic* pcRefPic = NULL;
3740    for ( Int i = 0; (i < getNumRefIdx( REF_PIC_LIST_0 )) && !existInterViewRef; i++ )
3741    {
3742      pcRefPic = getRefPic( REF_PIC_LIST_0, i );
3743      if ( pcRefPic != NULL )
3744      {
3745        if ( pcCurrPic->getViewIndex() != pcRefPic->getViewIndex() )
3746        {
3747          existInterViewRef = true;
3748        }
3749      }
3750    }
3751
3752    for ( Int i = 0; (i < getNumRefIdx( REF_PIC_LIST_1 )) && !existInterViewRef; i++ )
3753    {
3754      pcRefPic = getRefPic( REF_PIC_LIST_1, i );
3755      if ( pcRefPic != NULL )
3756      {
3757        if ( pcCurrPic->getViewIndex() != pcRefPic->getViewIndex() )
3758        {
3759          existInterViewRef = true;       
3760        }
3761      }
3762    }
3763
3764    if(!existInterViewRef)
3765    {
3766      m_bApplyIC = false;
3767    }
3768    else
3769    {
3770      Int curLayer=getDepth();
3771      if( curLayer>9) curLayer=9; // Max layer is 10
3772
3773      m_bApplyIC = true;
3774      Int refLayer = curLayer-1;
3775
3776      Int ICEnableCandidate = getICEnableCandidate(refLayer);
3777      Int ICEnableNum = getICEnableNum(refLayer);
3778      if( (refLayer>=0) && (ICEnableCandidate>0) )
3779      {   
3780        Double ratio=Double(ICEnableNum/Double(ICEnableCandidate));
3781
3782        if( ratio > IC_LOW_LATENCY_ENCODING_THRESHOLD)
3783        {
3784          m_bApplyIC=true;
3785        }
3786        else
3787        {
3788          m_bApplyIC=false;
3789        }
3790      }
3791      setICEnableCandidate(curLayer, 0);
3792      setICEnableNum(curLayer, 0);
3793    }
3794  }
3795  else
3796  {       
3797    TComPic*    pcCurrPic = getPic();
3798    TComPicYuv* pcCurrPicYuv = pcCurrPic->getPicYuvOrg();
3799   
3800    // Get InterView Reference picture
3801    // !!!!! Assume only one Interview Reference Picture in L0
3802    // GT: Is this assumption correct?
3803
3804    TComPicYuv* pcRefPicYuvOrg = NULL;
3805    for ( Int i = 0; i < getNumRefIdx( REF_PIC_LIST_0 ); i++ )
3806    {
3807      TComPic* pcRefPic = getRefPic( REF_PIC_LIST_0, i );
3808      if ( pcRefPic != NULL )
3809      {
3810        if ( pcCurrPic->getViewIndex() != pcRefPic->getViewIndex() )
3811        {
3812          pcRefPicYuvOrg = pcRefPic->getPicYuvOrg();
3813        }
3814      }
3815    }
3816
3817    if ( pcRefPicYuvOrg != NULL )
3818    {
3819      // Histogram building - luminance
3820      Int iMaxPelValue = ( 1 << getSPS()->getBitDepth(CHANNEL_TYPE_LUMA) ); 
3821      Int *aiRefOrgHist = (Int *) xMalloc( Int,iMaxPelValue );
3822      Int *aiCurrHist   = (Int *) xMalloc( Int,iMaxPelValue );
3823      memset( aiRefOrgHist, 0, iMaxPelValue*sizeof(Int) );
3824      memset( aiCurrHist, 0, iMaxPelValue*sizeof(Int) );
3825
3826      Int iWidth   = pcCurrPicYuv->getWidth(COMPONENT_Y);
3827      Int iHeight  = pcCurrPicYuv->getHeight(COMPONENT_Y);
3828      Pel* pCurrY   = pcCurrPicYuv->getAddr(COMPONENT_Y);
3829      Pel* pRefOrgY = pcRefPicYuvOrg->getAddr(COMPONENT_Y);
3830      Int iCurrStride = pcCurrPicYuv->getStride(COMPONENT_Y);
3831      Int iRefStride = pcRefPicYuvOrg->getStride(COMPONENT_Y);
3832      for ( Int y = 0; y < iHeight; y++ )
3833      {
3834        for ( Int x = 0; x < iWidth; x++ )
3835        {
3836          aiCurrHist[pCurrY[x]]++;
3837          aiRefOrgHist[pRefOrgY[x]]++;
3838        }
3839        pCurrY += iCurrStride;
3840        pRefOrgY += iRefStride;
3841      }
3842
3843      // Histogram SAD
3844      Int iSumOrgSAD = 0;
3845      for ( Int i = 0; i < iMaxPelValue; i++ )
3846      {
3847        iSumOrgSAD += abs( aiCurrHist[i] - aiRefOrgHist[i] );
3848      }
3849
3850      // Setting
3851      Double dThresholdOrgSAD = getIsDepth() ? 0.1 : 0.05;
3852
3853      if ( iSumOrgSAD > Int( dThresholdOrgSAD * iWidth * iHeight ) )
3854      {
3855        m_bApplyIC = true;
3856      }
3857      else
3858      {
3859        m_bApplyIC = false;
3860      }
3861
3862      xFree( aiCurrHist   );
3863      xFree( aiRefOrgHist );
3864    }
3865  }//if(bUseLowLatencyICEnc)
3866}
3867#endif
3868#if NH_3D
3869Void TComSlice::setIvPicLists( TComPicLists* m_ivPicLists )
3870{ 
3871  for (Int i = 0; i < MAX_NUM_LAYERS; i++ )
3872  { 
3873    for ( Int depthId = 0; depthId < 2; depthId++ )
3874    {
3875      m_ivPicsCurrPoc[ depthId ][ i ] = ( i <= m_viewIndex ) ? m_ivPicLists->getPic( i, ( depthId == 1) , getPOC() ) : NULL;
3876    }       
3877  } 
3878}
3879Void TComSlice::setDepthToDisparityLUTs()
3880{ 
3881  Bool setupLUT = false; 
3882 
3883  setupLUT = setupLUT || getViewSynthesisPredFlag( ); 
3884
3885#if NH_3D_NBDV_REF
3886  setupLUT = setupLUT || getDepthRefinementFlag( );
3887#endif 
3888
3889#if NH_3D_IV_MERGE
3890  setupLUT = setupLUT || ( getIvMvPredFlag() && getIsDepth() );
3891#endif
3892
3893  Int bitDepthY = getSPS()->getBitDepth(CHANNEL_TYPE_LUMA);
3894
3895  if( !setupLUT )
3896  {
3897    return; 
3898  }
3899
3900  m_numViews = getVPS()->getNumViews(); 
3901  /// GT: Allocation should be moved to a better place later;
3902  if ( m_depthToDisparityB == NULL )
3903  {
3904    m_depthToDisparityB = new Int*[ m_numViews ];
3905    for ( Int i = 0; i < getVPS()->getNumViews(); i++ )
3906    {
3907      m_depthToDisparityB[ i ] = new Int[ Int(1 << bitDepthY) ]; 
3908    }
3909  }
3910
3911 
3912  if ( m_depthToDisparityF == NULL )
3913  {
3914    m_depthToDisparityF = new Int*[ m_numViews ];
3915    for ( Int i = 0; i < m_numViews; i++ )
3916    {
3917      m_depthToDisparityF[ i ] = new Int[ Int(1 << bitDepthY) ]; 
3918    }
3919  }
3920
3921  assert( m_depthToDisparityB != NULL ); 
3922  assert( m_depthToDisparityF != NULL ); 
3923
3924  const TComVPS* vps = getVPS(); 
3925
3926  Int log2Div = bitDepthY - 1 + vps->getCpPrecision();
3927  Int voiInVps = vps->getVoiInVps( getViewIndex() ); 
3928  Bool camParaSH = vps->getCpInSliceSegmentHeaderFlag( voiInVps ); 
3929
3930  const IntAry1d codScale     = camParaSH ? m_aaiCodedScale [ 0 ] : vps->getCodedScale    ( voiInVps ); 
3931  const IntAry1d codOffset    = camParaSH ? m_aaiCodedOffset[ 0 ] : vps->getCodedOffset   ( voiInVps ); 
3932  const IntAry1d invCodScale  = camParaSH ? m_aaiCodedScale [ 1 ] : vps->getInvCodedScale ( voiInVps ); 
3933  const IntAry1d invCodOffset = camParaSH ? m_aaiCodedOffset[ 1 ] : vps->getInvCodedOffset( voiInVps ); 
3934
3935
3936  for (Int i = 0; i < voiInVps; i++)
3937  {
3938    Int iInVoi = vps->getVoiInVps( i ); 
3939#if ENC_DEC_TRACE && H_MV_ENC_DEC_TRAC
3940    if ( g_traceCameraParameters )
3941    {
3942      std::cout << "Cp: " << codScale   [ iInVoi ] << " " <<    codOffset[ iInVoi ] << " "
3943                << invCodScale[ iInVoi ] << " " << invCodOffset[ iInVoi ] << " " << log2Div << std::endl ; 
3944    }
3945#endif
3946    for ( Int d = 0; d <= ( ( 1 << bitDepthY ) - 1 ); d++ )
3947    {     
3948      Int offset =    ( codOffset  [ iInVoi ] << bitDepthY ) + ( ( 1 << log2Div ) >> 1 );         
3949      m_depthToDisparityB[ iInVoi ][ d ] = ( codScale [ iInVoi ] * d + offset ) >> log2Div; 
3950
3951      Int invOffset = ( invCodOffset[ iInVoi ] << bitDepthY ) + ( ( 1 << log2Div ) >> 1 );         
3952      m_depthToDisparityF[ iInVoi ][ d ] = ( invCodScale[ iInVoi ] * d + invOffset ) >> log2Div; 
3953    }
3954  }
3955}
3956#endif
3957
3958
3959
3960#if NH_MV
3961Void TComSlice::checkCrossLayerBlaFlag() const
3962{
3963  // cross_layer_bla_flag shall be equal to 0 for pictures with nal_unit_type not equal to IDR_W_RADL or IDR_N_LP or with nuh_layer_id not equal to 0.
3964  if ( getLayerId() != 0 || getNalUnitType() != NAL_UNIT_CODED_SLICE_IDR_W_RADL || getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_N_LP )
3965  {
3966    assert( m_crossLayerBlaFlag == 0 ); 
3967  }
3968}
3969
3970Bool TComSlice::inferPocMsbCycleValPresentFlag()
3971{
3972  Bool pocMsbValPresentFlag; 
3973  if( getSliceSegmentHeaderExtensionLength() == 0 ) 
3974  {
3975    pocMsbValPresentFlag = false; 
3976  }
3977  else if ( getPocMsbValRequiredFlag() )
3978  {
3979    pocMsbValPresentFlag = true; 
3980  }
3981  else
3982  {
3983    pocMsbValPresentFlag = false; 
3984  }
3985
3986  return pocMsbValPresentFlag;
3987}
3988
3989
3990Void TComSlice::f834decProcForRefPicListConst()
3991{
3992  // This process is invoked at the beginning of the decoding process for each P or B slice.
3993  assert( getSliceType() == B_SLICE || getSliceType() == P_SLICE );
3994
3995  // Reference pictures are addressed through reference indices as specified in clause 8.5.3.3.2. A reference index is an index into
3996  // a reference picture list. When decoding a P slice, there is a single reference picture list RefPicList0. When decoding a B
3997  // slice, there is a second independent reference picture list RefPicList1 in addition to RefPicList0.
3998
3999  // At the beginning of the decoding process for each slice, the reference picture lists RefPicList0 and, for B slices, RefPicList1
4000  // are derived as follows:
4001
4002  // The variable NumRpsCurrTempList0 is set equal to Max( num_ref_idx_l0_active_minus1 + 1, NumPicTotalCurr )
4003  Int numRpsCurrTempList0 = std::max( getNumRefIdxL0ActiveMinus1() + 1, getNumPicTotalCurr() );
4004
4005  // and the list RefPicListTemp0 is constructed as follows:
4006  std::vector<TComPic*> refPicListTemp0;
4007  refPicListTemp0.resize((MAX_NUM_REF+1),NULL);
4008
4009  const TComDecodedRps* decRps = getPic()->getDecodedRps(); 
4010
4011  const std::vector<TComPic*>& refPicSetStCurrBefore  = decRps->m_refPicSetStCurrBefore; 
4012  const std::vector<TComPic*>& refPicSetStCurrAfter   = decRps->m_refPicSetStCurrAfter;
4013  const std::vector<TComPic*>& refPicSetLtCurr        = decRps->m_refPicSetLtCurr; 
4014
4015  const Int                    numPocStCurrBefore     = decRps->m_numPocStCurrBefore; 
4016  const Int                    numPocStCurrAfter      = decRps->m_numPocStCurrAfter;
4017  const Int                    numPocLtCurr           = decRps->m_numPocLtCurr;
4018
4019  const Int                    numActiveRefLayerPics0 = decRps->m_numActiveRefLayerPics0;
4020  const Int                    numActiveRefLayerPics1 = decRps->m_numActiveRefLayerPics1;
4021
4022  const std::vector<TComPic*>& refPicSetInterLayer0   = decRps->m_refPicSetInterLayer0;
4023  const std::vector<TComPic*>& refPicSetInterLayer1   = decRps->m_refPicSetInterLayer1;
4024
4025  Int rIdx = 0;
4026  while( rIdx < numRpsCurrTempList0 )
4027  {
4028    for(Int  i = 0; i < numPocStCurrBefore  &&  rIdx < numRpsCurrTempList0; rIdx++, i++ )
4029    {
4030      refPicListTemp0[ rIdx ] = refPicSetStCurrBefore[ i ];
4031    }   
4032
4033    for(Int  i = 0; i < numActiveRefLayerPics0; rIdx++, i++ )
4034    {
4035      refPicListTemp0[ rIdx ] = refPicSetInterLayer0[ i ];
4036    }
4037
4038    for(Int  i = 0;  i < numPocStCurrAfter  &&  rIdx < numRpsCurrTempList0; rIdx++, i++ )  // (F 65)
4039    {
4040      refPicListTemp0[ rIdx ] = refPicSetStCurrAfter[ i ];
4041    }
4042
4043    for(Int  i = 0; i < numPocLtCurr  &&  rIdx < numRpsCurrTempList0; rIdx++, i++ )
4044    {
4045      refPicListTemp0[ rIdx ] = refPicSetLtCurr[ i ]; 
4046    }
4047
4048    for(Int  i = 0; i < numActiveRefLayerPics1; rIdx++, i++ )
4049    {
4050      refPicListTemp0[ rIdx ] = refPicSetInterLayer1[ i ];
4051    }
4052  }
4053
4054  // The list RefPicList0 is constructed as follows:
4055  TComRefPicListModification* rplm  = getRefPicListModification(); 
4056  for( rIdx = 0; rIdx  <=  getNumRefIdxL0ActiveMinus1(); rIdx++ )      //  (F 66)
4057  {
4058    m_apcRefPicList[ 0 ][ rIdx ] = rplm->getRefPicListModificationFlagL0( ) ? refPicListTemp0[ rplm->getListEntryL0( rIdx )] : refPicListTemp0[ rIdx ];
4059    // The decoding process below slice level requires the status
4060    // of the reference pictures, when decoding the RPS. So store it here.
4061    m_bIsUsedAsLongTerm[ 0 ][ rIdx ] = m_apcRefPicList[ 0 ][ rIdx ]->getIsLongTerm();
4062    m_aiRefPOCList     [ 0 ][ rIdx ] = m_apcRefPicList[ 0 ][ rIdx ]->getPOC();
4063    m_aiRefLayerIdList [ 0 ][ rIdx ] = m_apcRefPicList[ 0 ][ rIdx ]->getLayerId();
4064  } 
4065
4066  std::vector<TComPic*> refPicListTemp1;
4067  refPicListTemp1.resize((MAX_NUM_REF+1),NULL);
4068
4069  if (getSliceType() == B_SLICE )
4070  {
4071    // When the slice is a B slice, the variable NumRpsCurrTempList1 is set equal to
4072    // Max( num_ref_idx_l1_active_minus1 + 1, NumPicTotalCurr ) and the list RefPicListTemp1 is constructed as follows:       
4073    Int numRpsCurrTempList1 = std::max( getNumRefIdxL1ActiveMinus1() + 1, getNumPicTotalCurr() );
4074
4075    rIdx = 0; 
4076    while( rIdx < numRpsCurrTempList1 )
4077    {
4078      for( Int i = 0; i < numPocStCurrAfter  &&  rIdx < numRpsCurrTempList1; rIdx++, i++ )
4079      {
4080        refPicListTemp1[ rIdx ] = refPicSetStCurrAfter[ i ]; 
4081      }
4082      for( Int i = 0; i< numActiveRefLayerPics1; rIdx++, i++ )
4083      {
4084        refPicListTemp1[ rIdx ] = refPicSetInterLayer1[ i ]; 
4085      }
4086      for( Int i = 0;  i < numPocStCurrBefore  &&  rIdx < numRpsCurrTempList1; rIdx++, i++ )  // (F 67)
4087      {
4088        refPicListTemp1[ rIdx ] = refPicSetStCurrBefore[ i ];
4089      }         
4090      for( Int i = 0; i < numPocLtCurr  &&  rIdx < numRpsCurrTempList1; rIdx++, i++ )
4091      {
4092        refPicListTemp1[ rIdx ] = refPicSetLtCurr[ i ];
4093      }
4094      for( Int i = 0; i< numActiveRefLayerPics0; rIdx++, i++ )
4095      {
4096        refPicListTemp1[ rIdx ] = refPicSetInterLayer0[ i ]; 
4097      }
4098    }
4099  }
4100
4101  if (getSliceType() == B_SLICE )
4102  {
4103    //   When the slice is a B slice, the list RefPicList1 is constructed as follows:
4104    for( rIdx = 0; rIdx  <=  getNumRefIdxL1ActiveMinus1(); rIdx++ )      // (F 68)
4105    {
4106      m_apcRefPicList[ 1 ][ rIdx ] = rplm->getRefPicListModificationFlagL1() ? refPicListTemp1[ rplm->getListEntryL1( rIdx ) ] : refPicListTemp1[ rIdx ]; 
4107
4108      // The decoding process below slice level requires the marking status
4109      // of the reference pictures, when decoding the RPS. So store it here.
4110      m_bIsUsedAsLongTerm[ 1 ][ rIdx ] = m_apcRefPicList[ 1 ][ rIdx ]->getIsLongTerm();
4111      m_aiRefPOCList     [ 1 ][ rIdx ] = m_apcRefPicList[ 1 ][ rIdx ]->getPOC();
4112      m_aiRefLayerIdList [ 1 ][ rIdx ] = m_apcRefPicList[ 1 ][ rIdx ]->getLayerId();
4113    }   
4114  }
4115}
4116
4117
4118Void TComSlice::cl834DecProcForRefPicListConst()
4119{
4120  // This process is invoked at the beginning of the decoding process for each P or B slice.
4121  assert( getSliceType() == B_SLICE || getSliceType() == P_SLICE );
4122
4123  // Reference pictures are addressed through reference indices as specified in clause 8.5.3.3.2. A reference index is an index into
4124  // a reference picture list. When decoding a P slice, there is a single reference picture list RefPicList0. When decoding a B
4125  // slice, there is a second independent reference picture list RefPicList1 in addition to RefPicList0.
4126
4127  // At the beginning of the decoding process for each slice, the reference picture lists RefPicList0 and, for B slices, RefPicList1
4128  // are derived as follows:
4129
4130  // The variable NumRpsCurrTempList0 is set equal to Max( num_ref_idx_l0_active_minus1 + 1, NumPicTotalCurr )
4131  Int numRpsCurrTempList0 = std::max( getNumRefIdxL0ActiveMinus1() + 1, getNumPicTotalCurr() );
4132
4133  // and the list RefPicListTemp0 is constructed as follows:
4134  std::vector<TComPic*> refPicListTemp0; 
4135  refPicListTemp0.resize((MAX_NUM_REF+1),NULL);
4136
4137  const TComDecodedRps* decRps = getPic()->getDecodedRps(); 
4138
4139  const std::vector<TComPic*>& refPicSetStCurrBefore = decRps->m_refPicSetStCurrBefore; 
4140  const std::vector<TComPic*>& refPicSetStCurrAfter  = decRps->m_refPicSetStCurrAfter;
4141  const std::vector<TComPic*>& refPicSetLtCurr       = decRps->m_refPicSetLtCurr; 
4142   
4143  const Int                    numPocStCurrBefore    = decRps->m_numPocStCurrBefore; 
4144  const Int                    numPocStCurrAfter     = decRps->m_numPocStCurrAfter;
4145  const Int                    numPocLtCurr          = decRps->m_numPocLtCurr;
4146
4147  Int rIdx = 0;
4148  while( rIdx < numRpsCurrTempList0 )
4149  {
4150    for(Int  i = 0; i < numPocStCurrBefore  &&  rIdx < numRpsCurrTempList0; rIdx++, i++ )
4151    {
4152      refPicListTemp0[ rIdx ] = refPicSetStCurrBefore[ i ];
4153    }   
4154
4155    for(Int  i = 0;  i < numPocStCurrAfter  &&  rIdx < numRpsCurrTempList0; rIdx++, i++ )  // (8 8)
4156    {
4157      refPicListTemp0[ rIdx ] = refPicSetStCurrAfter[ i ];
4158    }
4159
4160    for(Int  i = 0; i < numPocLtCurr  &&  rIdx < numRpsCurrTempList0; rIdx++, i++ )
4161    {
4162      refPicListTemp0[ rIdx ] = refPicSetLtCurr[ i ]; 
4163    }
4164  }
4165
4166  // The list RefPicList0 is constructed as follows:
4167
4168  TComRefPicListModification* rplm = getRefPicListModification(); 
4169  for( rIdx = 0; rIdx  <=  getNumRefIdxL0ActiveMinus1(); rIdx++ )      //   (8-9)
4170  {
4171    m_apcRefPicList[0][ rIdx ] = rplm->getRefPicListModificationFlagL0( ) ? refPicListTemp0[ rplm->getListEntryL0( rIdx )] : refPicListTemp0[ rIdx ];
4172
4173    // The decoding process below slice level requires the marking status
4174    // of the reference pictures, when decoding the RPS. So store it here.
4175    m_bIsUsedAsLongTerm[ 0 ][ rIdx ] = m_apcRefPicList[ 0 ][ rIdx ]->getIsLongTerm();
4176    m_aiRefPOCList     [ 0 ][ rIdx ] = m_apcRefPicList[ 0 ][ rIdx ]->getPOC();
4177    m_aiRefLayerIdList [ 0 ][ rIdx ] = m_apcRefPicList[ 0 ][ rIdx ]->getLayerId();
4178  }
4179
4180  std::vector<TComPic*> refPicListTemp1;
4181  refPicListTemp1.resize((MAX_NUM_REF+1),NULL);
4182
4183  if (getSliceType() == B_SLICE )
4184  {
4185    // When the slice is a B slice, the variable NumRpsCurrTempList1 is set equal to
4186    // Max( num_ref_idx_l1_active_minus1 + 1, NumPicTotalCurr ) and the list RefPicListTemp1 is constructed as follows:
4187    Int numRpsCurrTempList1 = std::max( getNumRefIdxL1ActiveMinus1() + 1, getNumPicTotalCurr() );
4188
4189    rIdx = 0; 
4190    while( rIdx < numRpsCurrTempList1 )
4191    {
4192      for( Int i = 0; i < numPocStCurrAfter  &&  rIdx < numRpsCurrTempList1; rIdx++, i++ )
4193      {
4194        refPicListTemp1[ rIdx ] = refPicSetStCurrAfter[ i ]; 
4195      }
4196      for( Int i = 0;  i < numPocStCurrBefore  &&  rIdx < numRpsCurrTempList1; rIdx++, i++ )  // (8-10)
4197      {
4198        refPicListTemp1[ rIdx ] = refPicSetStCurrBefore[ i ];
4199      }         
4200      for( Int i = 0; i < numPocLtCurr  &&  rIdx < numRpsCurrTempList1; rIdx++, i++ )
4201      {
4202        refPicListTemp1[ rIdx ] = refPicSetLtCurr[ i ];
4203      }
4204    }
4205  }
4206
4207  if (getSliceType() == B_SLICE )
4208  {
4209    //   When the slice is a B slice, the list RefPicList1 is constructed as follows:
4210    for( rIdx = 0; rIdx  <=  getNumRefIdxL1ActiveMinus1(); rIdx++ )      // (F 68)
4211    {
4212      m_apcRefPicList[ 1 ][ rIdx ] = rplm->getRefPicListModificationFlagL1() ? refPicListTemp1[ rplm->getListEntryL1( rIdx ) ] : refPicListTemp1[ rIdx ]; 
4213
4214      // The decoding process below slice level requires the marking status
4215      // of the reference pictures, when decoding the RPS. So store it here.
4216      m_bIsUsedAsLongTerm[ 1 ][ rIdx ] = m_apcRefPicList[ 1 ][ rIdx ]->getIsLongTerm();
4217      m_aiRefPOCList     [ 1 ][ rIdx ] = m_apcRefPicList[ 1 ][ rIdx ]->getPOC();
4218      m_aiRefLayerIdList [ 1 ][ rIdx ] = m_apcRefPicList[ 1 ][ rIdx ]->getLayerId();
4219    }
4220  }
4221}
4222
4223Int TComSlice::getNumPicTotalCurr() const
4224{
4225  Int numPicTotalCurr = 0;
4226#if NH_MV_FIX_NUM_POC_TOTAL_CUR
4227  if ( !isIdr()  )
4228  {
4229    const TComStRefPicSet* stRps = getStRps( getCurrRpsIdx() ); 
4230#endif
4231    for( Int i = 0; i < stRps->getNumNegativePicsVar(); i++ )
4232    {
4233      if( stRps->getUsedByCurrPicS0Var( i ) )
4234      {
4235        numPicTotalCurr++;
4236      }
4237    }
4238    for( Int i = 0; i < stRps->getNumPositivePicsVar(); i++)  //(7 55)
4239    {
4240      if( stRps->getUsedByCurrPicS1Var(i) )
4241      {
4242        numPicTotalCurr++;
4243      }
4244    }
4245    for( Int i = 0; i < getNumLongTermSps() + getNumLongTermPics(); i++ )
4246    {
4247      if( getUsedByCurrPicLtVar( i ) )
4248      {
4249        numPicTotalCurr++;
4250      }
4251    }
4252#if NH_MV_FIX_NUM_POC_TOTAL_CUR
4253  }
4254#endif
4255
4256  if ( decProcAnnexF() )
4257  {
4258    numPicTotalCurr += getNumActiveRefLayerPics(); 
4259  }
4260  return numPicTotalCurr;
4261}
4262
4263
4264
4265Int TComSlice::getPocLsbLtVar( Int i )
4266{
4267  Int pocLsbLtVar; 
4268  if (i < getNumLongTermSps() )
4269  {
4270
4271    pocLsbLtVar = getSPS()->getLtRefPicPocLsbSps( getLtIdxSps( i ) ); 
4272  }
4273  else
4274  {
4275    pocLsbLtVar = getPocLsbLt( i ); 
4276  }
4277  return pocLsbLtVar;
4278}
4279
4280
4281Bool TComSlice::getUsedByCurrPicLtVar( Int i ) const
4282{
4283  Bool usedByCurrPicLtVar; 
4284  if (i < getNumLongTermSps() )
4285  {
4286    usedByCurrPicLtVar = getSPS()->getUsedByCurrPicLtSPSFlag( getLtIdxSps( i ) ); 
4287  }
4288  else
4289  {
4290    usedByCurrPicLtVar = getUsedByCurrPicLtFlag( i ); 
4291  }
4292  return usedByCurrPicLtVar;
4293}
4294
4295
4296Int TComSlice::getDeltaPocMsbCycleLtVar( Int i ) const
4297{
4298  Int deltaPocMsbCycleVar; 
4299  if (i == 0 || i == getNumLongTermSps() )
4300  {
4301    deltaPocMsbCycleVar = getDeltaPocMsbCycleLt( i ); 
4302  }
4303  else
4304  {
4305    deltaPocMsbCycleVar = getDeltaPocMsbCycleLt( i ) + getDeltaPocMsbCycleLtVar( i - 1 ); 
4306  }
4307  return deltaPocMsbCycleVar;
4308}
4309
4310#endif
4311
4312
4313#if NH_3D
4314Void TComSlice::init3dToolParameters()
4315{
4316  Bool depthFlag = getIsDepth();
4317
4318  Bool nRLLG0 =  ( getVPS()->getNumRefListLayers( getLayerId() ) > 0 );     
4319
4320  const TComSps3dExtension* sps3dExt = getSPS()->getSps3dExtension();
4321
4322  m_ivMvPredFlag           = sps3dExt->getIvMvPredFlag         ( depthFlag ) && nRLLG0                       ;                             
4323  m_ivMvScalingFlag        = sps3dExt->getIvMvScalingFlag      ( depthFlag )                                 ;                             
4324  m_ivResPredFlag          = sps3dExt->getIvResPredFlag        ( depthFlag ) && nRLLG0                       ;                               
4325  m_depthRefinementFlag    = sps3dExt->getDepthRefinementFlag  ( depthFlag )           && getInCompPredFlag() && m_cpAvailableFlag;
4326  m_viewSynthesisPredFlag  = sps3dExt->getViewSynthesisPredFlag( depthFlag ) && nRLLG0 && getInCompPredFlag() && m_cpAvailableFlag;
4327  m_depthBasedBlkPartFlag  = sps3dExt->getDepthBasedBlkPartFlag( depthFlag )           && getInCompPredFlag();                         
4328  m_mpiFlag                = sps3dExt->getMpiFlag              ( depthFlag )           && getInCompPredFlag();
4329  m_intraContourFlag       = sps3dExt->getIntraContourFlag     ( depthFlag )           && getInCompPredFlag();
4330  m_intraSdcWedgeFlag      = sps3dExt->getIntraSdcWedgeFlag    ( depthFlag )                                 ;                         
4331  m_qtPredFlag             = sps3dExt->getQtPredFlag           ( depthFlag )           && getInCompPredFlag();
4332  m_interSdcFlag           = sps3dExt->getInterSdcFlag         ( depthFlag )                                 ; 
4333  m_depthIntraSkipFlag     = sps3dExt->getDepthIntraSkipFlag   ( depthFlag )                                 ;                         
4334
4335  m_subPbSize              =  1 << ( sps3dExt->getLog2SubPbSizeMinus3   ( depthFlag ) + 3 ); 
4336  m_mpiSubPbSize           =  1 << ( sps3dExt->getLog2MpiSubPbSizeMinus3( depthFlag ) + 3 );
4337
4338
4339#if H_3D_OUTPUT_ACTIVE_TOOLS
4340  std::cout << "Layer:                  :" << getLayerId()             << std::endl;
4341  std::cout << "DepthFlag:              :" << getIsDepth()             << std::endl;
4342  std::cout << "ViewOrderIdx:           :" << getViewIndex()           << std::endl;
4343  std::cout << "InterCmpPredAvailableFlag:" << getInCmpPredAvailFlag() << std::endl;
4344  std::cout << "InterCompPredFlag       :"  << getInCompPredFlag()     << std::endl;
4345 
4346  std::cout << "ivMvPredFlag            :" << m_ivMvPredFlag           << std::endl;
4347  std::cout << "ivMvScalingFlag         :" << m_ivMvScalingFlag        << std::endl;
4348  std::cout << "ivResPredFlag           :" << m_ivResPredFlag          << std::endl;
4349  std::cout << "depthRefinementFlag     :" << m_depthRefinementFlag    << std::endl;
4350  std::cout << "viewSynthesisPredFlag   :" << m_viewSynthesisPredFlag  << std::endl;
4351  std::cout << "depthBasedBlkPartFlag   :" << m_depthBasedBlkPartFlag  << std::endl;
4352  std::cout << "mpiFlag                 :" << m_mpiFlag                << std::endl;
4353  std::cout << "intraContourFlag        :" << m_intraContourFlag       << std::endl;
4354  std::cout << "intraSdcWedgeFlag       :" << m_intraSdcWedgeFlag      << std::endl;
4355  std::cout << "qtPredFlag              :" << m_qtPredFlag             << std::endl;
4356  std::cout << "interSdcFlag            :" << m_interSdcFlag           << std::endl;
4357  std::cout << "depthIntraSkipFlag      :" << m_depthIntraSkipFlag     << std::endl;   
4358  std::cout << "subPbSize               :" << m_subPbSize              << std::endl;
4359  std::cout << "mpiSubPbSize            :" << m_mpiSubPbSize           << std::endl;
4360#endif
4361}
4362
4363Void TComSlice::deriveInCmpPredAndCpAvailFlag( )
4364{
4365  Int numCurCmpLIds = getIsDepth() ? 1 : getNumActiveRefLayerPics(); 
4366  std::vector<Int> curCmpLIds;
4367  if ( getIsDepth() )
4368  {
4369    curCmpLIds.push_back( getLayerId() );
4370  }
4371  else
4372  {
4373    for (Int i = 0; i < numCurCmpLIds; i++)
4374    {
4375      curCmpLIds.push_back( getRefPicLayerId( i ) );
4376    }
4377  }
4378
4379  m_cpAvailableFlag = true;
4380  m_inCmpRefViewIdcs.clear();
4381  Bool allRefCmpLayersAvailFlag = true;
4382
4383  for( Int i = 0; i <= numCurCmpLIds - 1; i++ )
4384  {     
4385    m_inCmpRefViewIdcs.push_back( getVPS()->getViewOrderIdx( curCmpLIds[ i ] ));
4386    if( !getVPS()->getCpPresentFlag( getVPS()->getVoiInVps( getViewIndex() ),  getVPS()->getVoiInVps( m_inCmpRefViewIdcs[ i ] ) ) )
4387    {
4388      m_cpAvailableFlag = false;
4389    }
4390    Bool refCmpCurLIdAvailFlag = false;
4391    if( getVPS()->getViewCompLayerPresentFlag( m_inCmpRefViewIdcs[ i ], !getIsDepth() ) )
4392    {
4393      Int j = getVPS()->getLayerIdInVps( getVPS()->getViewCompLayerId( m_inCmpRefViewIdcs[ i ],  !getIsDepth() ) );
4394      if  ( getVPS()->getDirectDependencyFlag( getVPS()->getLayerIdInVps( getLayerId() ) ,  j ) &&
4395        getVPS()->getSubLayersVpsMaxMinus1( j ) >= getTemporalId()   &&
4396        ( getTemporalId() == 0 || getVPS()->getMaxTidIlRefPicsPlus1( j , getVPS()->getLayerIdInVps( getLayerId() ) ) > getTemporalId() )       
4397        )
4398      {
4399        refCmpCurLIdAvailFlag = true;
4400      }
4401    }
4402    if( !refCmpCurLIdAvailFlag )
4403    {
4404      allRefCmpLayersAvailFlag = false;
4405    }
4406  }
4407
4408  if( !allRefCmpLayersAvailFlag )
4409  {
4410    m_inCmpPredAvailFlag = false;
4411  } 
4412  else 
4413  {
4414    const TComSps3dExtension* sps3dExt = getSPS()->getSps3dExtension();
4415    if( !getIsDepth() )
4416    {
4417      m_inCmpPredAvailFlag = sps3dExt->getViewSynthesisPredFlag( getIsDepth() ) || 
4418        sps3dExt->getDepthBasedBlkPartFlag( getIsDepth() ) || 
4419        sps3dExt->getDepthRefinementFlag  ( getIsDepth() );                           
4420    }
4421    else
4422    {
4423      m_inCmpPredAvailFlag = sps3dExt->getIntraContourFlag( getIsDepth() ) || 
4424        sps3dExt->getQtPredFlag( getIsDepth() ) || 
4425        sps3dExt->getMpiFlag( getIsDepth() );                                 
4426    }
4427  }
4428}
4429
4430Void TComSlice::checkInCompPredRefLayers()
4431{ 
4432  if ( getInCompPredFlag() )
4433  {
4434    for (Int i = 0; i < getNumCurCmpLIds(); i++ )
4435    {     
4436      assert( getIvPic(!getIsDepth(), getInCmpRefViewIdcs( i ) ) != NULL );       
4437      //  It is a requirement of bitstream conformance that there
4438      //  is a picture in the DPB with PicOrderCntVal equal to the PicOrderCntVal of the current picture,
4439      //  and a nuh_layer_id value equal to ViewCompLayerId[ inCmpRefViewIdcs[ i ] ][ !DepthFlag ].
4440    }
4441  } 
4442}
4443
4444Void TComSlice::setPocsInCurrRPSs()
4445{
4446  // Currently only needed at decoder side;
4447  m_pocsInCurrRPSs.clear();   
4448  std::vector<TComPic*>** rpsCurr = getPic()->getDecodedRps()->m_refPicSetsCurr;
4449  for (Int i = 0 ; i < 3; i++ )
4450  {
4451    for( Int j = 0; j < rpsCurr[i]->size(); j++ )
4452    {
4453      m_pocsInCurrRPSs.push_back( (*rpsCurr[i])[j]->getPOC() ); 
4454    }
4455  }
4456}
4457
4458#endif
4459
4460/** get scaling matrix from RefMatrixID
4461 * \param sizeId    size index
4462 * \param listId    index of input matrix
4463 * \param refListId index of reference matrix
4464 */
4465Void TComScalingList::processRefMatrix( UInt sizeId, UInt listId , UInt refListId )
4466{
4467  ::memcpy(getScalingListAddress(sizeId, listId),((listId == refListId)? getScalingListDefaultAddress(sizeId, refListId): getScalingListAddress(sizeId, refListId)),sizeof(Int)*min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId]));
4468}
4469
4470Void TComScalingList::checkPredMode(UInt sizeId, UInt listId)
4471{
4472  Int predListStep = (sizeId == SCALING_LIST_32x32? (SCALING_LIST_NUM/NUMBER_OF_PREDICTION_MODES) : 1); // if 32x32, skip over chroma entries.
4473
4474  for(Int predListIdx = (Int)listId ; predListIdx >= 0; predListIdx-=predListStep)
4475  {
4476    if( !memcmp(getScalingListAddress(sizeId,listId),((listId == predListIdx) ?
4477      getScalingListDefaultAddress(sizeId, predListIdx): getScalingListAddress(sizeId, predListIdx)),sizeof(Int)*min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId])) // check value of matrix
4478     && ((sizeId < SCALING_LIST_16x16) || (getScalingListDC(sizeId,listId) == getScalingListDC(sizeId,predListIdx)))) // check DC value
4479    {
4480      setRefMatrixId(sizeId, listId, predListIdx);
4481      setScalingListPredModeFlag(sizeId, listId, false);
4482      return;
4483    }
4484  }
4485  setScalingListPredModeFlag(sizeId, listId, true);
4486}
4487
4488static Void outputScalingListHelp(std::ostream &os)
4489{
4490  os << "The scaling list file specifies all matrices and their DC values; none can be missing,\n"
4491         "but their order is arbitrary.\n\n"
4492         "The matrices are specified by:\n"
4493         "<matrix name><unchecked data>\n"
4494         "  <value>,<value>,<value>,....\n\n"
4495         "  Line-feeds can be added arbitrarily between values, and the number of values needs to be\n"
4496         "  at least the number of entries for the matrix (superfluous entries are ignored).\n"
4497         "  The <unchecked data> is text on the same line as the matrix that is not checked\n"
4498         "  except to ensure that the matrix name token is unique. It is recommended that it is ' ='\n"
4499         "  The values in the matrices are the absolute values (0-255), not the delta values as\n"
4500         "  exchanged between the encoder and decoder\n\n"
4501         "The DC values (for matrix sizes larger than 8x8) are specified by:\n"
4502         "<matrix name>_DC<unchecked data>\n"
4503         "  <value>\n";
4504
4505  os << "The permitted matrix names are:\n";
4506  for(UInt sizeIdc = 0; sizeIdc < SCALING_LIST_SIZE_NUM; sizeIdc++)
4507  {
4508    for(UInt listIdc = 0; listIdc < SCALING_LIST_NUM; listIdc++)
4509    {
4510      if ((sizeIdc!=SCALING_LIST_32x32) || (listIdc%(SCALING_LIST_NUM/NUMBER_OF_PREDICTION_MODES) == 0))
4511      {
4512        os << "  " << MatrixType[sizeIdc][listIdc] << '\n';
4513      }
4514    }
4515  }
4516}
4517
4518Void TComScalingList::outputScalingLists(std::ostream &os) const
4519{
4520  for(UInt sizeIdc = 0; sizeIdc < SCALING_LIST_SIZE_NUM; sizeIdc++)
4521  {
4522    const UInt size = min(8,4<<(sizeIdc));
4523    for(UInt listIdc = 0; listIdc < SCALING_LIST_NUM; listIdc++)
4524    {
4525      if ((sizeIdc!=SCALING_LIST_32x32) || (listIdc%(SCALING_LIST_NUM/NUMBER_OF_PREDICTION_MODES) == 0))
4526      {
4527        const Int *src = getScalingListAddress(sizeIdc, listIdc);
4528        os << (MatrixType[sizeIdc][listIdc]) << " =\n  ";
4529        for(UInt y=0; y<size; y++)
4530        {
4531          for(UInt x=0; x<size; x++, src++)
4532          {
4533            os << std::setw(3) << (*src) << ", ";
4534          }
4535          os << (y+1<size?"\n  ":"\n");
4536        }
4537        if(sizeIdc > SCALING_LIST_8x8)
4538        {
4539          os << MatrixType_DC[sizeIdc][listIdc] << " = \n  " << std::setw(3) << getScalingListDC(sizeIdc, listIdc) << "\n";
4540        }
4541        os << "\n";
4542      }
4543    }
4544  }
4545}
4546
4547Bool TComScalingList::xParseScalingList(Char* pchFile)
4548{
4549  static const Int LINE_SIZE=1024;
4550  FILE *fp = NULL;
4551  Char line[LINE_SIZE];
4552
4553  if (pchFile == NULL)
4554  {
4555    fprintf(stderr, "Error: no scaling list file specified. Help on scaling lists being output\n");
4556    outputScalingListHelp(std::cout);
4557    std::cout << "\n\nExample scaling list file using default values:\n\n";
4558    outputScalingLists(std::cout);
4559    exit (1);
4560    return true;
4561  }
4562  else if ((fp = fopen(pchFile,"r")) == (FILE*)NULL)
4563  {
4564    fprintf(stderr, "Error: cannot open scaling list file %s for reading\n",pchFile);
4565    return true;
4566  }
4567
4568  for(UInt sizeIdc = 0; sizeIdc < SCALING_LIST_SIZE_NUM; sizeIdc++)
4569  {
4570    const UInt size = min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeIdc]);
4571
4572    for(UInt listIdc = 0; listIdc < SCALING_LIST_NUM; listIdc++)
4573    {
4574      Int * const src = getScalingListAddress(sizeIdc, listIdc);
4575
4576      if ((sizeIdc==SCALING_LIST_32x32) && (listIdc%(SCALING_LIST_NUM/NUMBER_OF_PREDICTION_MODES) != 0)) // derive chroma32x32 from chroma16x16
4577      {
4578        const Int *srcNextSmallerSize = getScalingListAddress(sizeIdc-1, listIdc);
4579        for(UInt i=0; i<size; i++)
4580        {
4581          src[i] = srcNextSmallerSize[i];
4582        }
4583        setScalingListDC(sizeIdc,listIdc,(sizeIdc > SCALING_LIST_8x8) ? getScalingListDC(sizeIdc-1, listIdc) : src[0]);
4584      }
4585      else
4586      {
4587        {
4588          fseek(fp, 0, SEEK_SET);
4589          Bool bFound=false;
4590          while ((!feof(fp)) && (!bFound))
4591          {
4592            Char *ret = fgets(line, LINE_SIZE, fp);
4593            Char *findNamePosition= ret==NULL ? NULL : strstr(line, MatrixType[sizeIdc][listIdc]);
4594            // This could be a match against the DC string as well, so verify it isn't
4595            if (findNamePosition!= NULL && (MatrixType_DC[sizeIdc][listIdc]==NULL || strstr(line, MatrixType_DC[sizeIdc][listIdc])==NULL))
4596            {
4597              bFound=true;
4598            }
4599          }
4600          if (!bFound)
4601          {
4602            fprintf(stderr, "Error: cannot find Matrix %s from scaling list file %s\n", MatrixType[sizeIdc][listIdc], pchFile);
4603            return true;
4604          }
4605        }
4606        for (UInt i=0; i<size; i++)
4607        {
4608          Int data;
4609          if (fscanf(fp, "%d,", &data)!=1)
4610          {
4611            fprintf(stderr, "Error: cannot read value #%d for Matrix %s from scaling list file %s at file position %ld\n", i, MatrixType[sizeIdc][listIdc], pchFile, ftell(fp));
4612            return true;
4613          }
4614          if (data<0 || data>255)
4615          {
4616            fprintf(stderr, "Error: QMatrix entry #%d of value %d for Matrix %s from scaling list file %s at file position %ld is out of range (0 to 255)\n", i, data, MatrixType[sizeIdc][listIdc], pchFile, ftell(fp));
4617            return true;
4618          }
4619          src[i] = data;
4620        }
4621
4622        //set DC value for default matrix check
4623        setScalingListDC(sizeIdc,listIdc,src[0]);
4624
4625        if(sizeIdc > SCALING_LIST_8x8)
4626        {
4627          {
4628            fseek(fp, 0, SEEK_SET);
4629            Bool bFound=false;
4630            while ((!feof(fp)) && (!bFound))
4631            {
4632              Char *ret = fgets(line, LINE_SIZE, fp);
4633              Char *findNamePosition= ret==NULL ? NULL : strstr(line, MatrixType_DC[sizeIdc][listIdc]);
4634              if (findNamePosition!= NULL)
4635              {
4636                // This won't be a match against the non-DC string.
4637                bFound=true;
4638              }
4639            }
4640            if (!bFound)
4641            {
4642              fprintf(stderr, "Error: cannot find DC Matrix %s from scaling list file %s\n", MatrixType_DC[sizeIdc][listIdc], pchFile);
4643              return true;
4644            }
4645          }
4646          Int data;
4647          if (fscanf(fp, "%d,", &data)!=1)
4648          {
4649            fprintf(stderr, "Error: cannot read DC %s from scaling list file %s at file position %ld\n", MatrixType_DC[sizeIdc][listIdc], pchFile, ftell(fp));
4650            return true;
4651          }
4652          if (data<0 || data>255)
4653          {
4654            fprintf(stderr, "Error: DC value %d for Matrix %s from scaling list file %s at file position %ld is out of range (0 to 255)\n", data, MatrixType[sizeIdc][listIdc], pchFile, ftell(fp));
4655            return true;
4656          }
4657          //overwrite DC value when size of matrix is larger than 16x16
4658          setScalingListDC(sizeIdc,listIdc,data);
4659        }
4660      }
4661    }
4662  }
4663//  std::cout << "\n\nRead scaling lists of:\n\n";
4664//  outputScalingLists(std::cout);
4665
4666  fclose(fp);
4667  return false;
4668}
4669
4670#if NH_MV
4671Void TComScalingList::inferFrom( const TComScalingList& srcScLi )
4672{
4673  for(Int sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
4674  {
4675    for(Int listId = 0; listId <  SCALING_LIST_NUM; listId++)
4676    {
4677      setRefMatrixId  (sizeId,listId, srcScLi.getRefMatrixId  (sizeId,listId));
4678      setScalingListDC(sizeId,listId, srcScLi.getScalingListDC(sizeId,listId));         
4679      ::memcpy(getScalingListAddress(sizeId, listId),srcScLi.getScalingListAddress(sizeId, listId),sizeof(Int)*min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId]));
4680    }
4681  }
4682}
4683#endif
4684
4685
4686/** get default address of quantization matrix
4687 * \param sizeId size index
4688 * \param listId list index
4689 * \returns pointer of quantization matrix
4690 */
4691const Int* TComScalingList::getScalingListDefaultAddress(UInt sizeId, UInt listId)
4692{
4693  const Int *src = 0;
4694  switch(sizeId)
4695  {
4696    case SCALING_LIST_4x4:
4697      src = g_quantTSDefault4x4;
4698      break;
4699    case SCALING_LIST_8x8:
4700    case SCALING_LIST_16x16:
4701    case SCALING_LIST_32x32:
4702      src = (listId < (SCALING_LIST_NUM/NUMBER_OF_PREDICTION_MODES) ) ? g_quantIntraDefault8x8 : g_quantInterDefault8x8;
4703      break;
4704    default:
4705      assert(0);
4706      src = NULL;
4707      break;
4708  }
4709  return src;
4710}
4711
4712/** process of default matrix
4713 * \param sizeId size index
4714 * \param listId index of input matrix
4715 */
4716Void TComScalingList::processDefaultMatrix(UInt sizeId, UInt listId)
4717{
4718  ::memcpy(getScalingListAddress(sizeId, listId),getScalingListDefaultAddress(sizeId,listId),sizeof(Int)*min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId]));
4719  setScalingListDC(sizeId,listId,SCALING_LIST_DC);
4720}
4721
4722/** check DC value of matrix for default matrix signaling
4723 */
4724Void TComScalingList::checkDcOfMatrix()
4725{
4726  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
4727  {
4728    for(UInt listId = 0; listId < SCALING_LIST_NUM; listId++)
4729    {
4730      //check default matrix?
4731      if(getScalingListDC(sizeId,listId) == 0)
4732      {
4733        processDefaultMatrix(sizeId, listId);
4734      }
4735    }
4736  }
4737}
4738
4739ParameterSetManager::ParameterSetManager()
4740: m_vpsMap(MAX_NUM_VPS)
4741, m_spsMap(MAX_NUM_SPS)
4742, m_ppsMap(MAX_NUM_PPS)
4743, m_activeVPSId(-1)
4744#if !NH_MV
4745, m_activeSPSId(-1)
4746#endif
4747{
4748#if NH_MV 
4749  for (Int i = 0; i < MAX_NUM_LAYERS; i++ )
4750  {   
4751    m_activeSPSId[i] = -1; 
4752  }
4753#endif
4754}
4755
4756
4757ParameterSetManager::~ParameterSetManager()
4758{
4759}
4760
4761//! activate a SPS from a active parameter sets SEI message
4762//! \returns true, if activation is successful
4763#if NH_MV
4764//Bool ParameterSetManager::activateSPSWithSEI(Int spsId, Int layerId )
4765#else
4766//Bool ParameterSetManager::activateSPSWithSEI(Int spsId)
4767#endif
4768//{
4769//TComSPS *sps = m_spsMap.getPS(spsId);
4770//  if (sps)
4771//  {
4772//    Int vpsId = sps->getVPSId();
4773//    TComVPS *vps = m_vpsMap.getPS(vpsId);
4774//    if (vps)
4775//    {
4776#if NH_MV
4777//      m_activeVPS = *(vps);
4778//      m_activeSPS[ layerId ] = *(sps);
4779#else
4780//      m_activeVPS = *(vps);
4781//      m_activeSPS = *(sps);
4782#endif
4783//      return true;
4784//    }
4785//    else
4786//    {
4787//      printf("Warning: tried to activate SPS using an Active parameter sets SEI message. Referenced VPS does not exist.");
4788//    }
4789//  }
4790//  else
4791//  {
4792//    printf("Warning: tried to activate non-existing SPS using an Active parameter sets SEI message.");
4793//  }
4794//  return false;
4795//}
4796
4797//! activate a PPS and depending on isIDR parameter also SPS and VPS
4798//! \returns true, if activation is successful
4799#if NH_MV
4800Bool ParameterSetManager::activatePPS(Int ppsId, Bool isIRAP, Int layerId )
4801#else
4802Bool ParameterSetManager::activatePPS(Int ppsId, Bool isIRAP)
4803#endif
4804{
4805  TComPPS *pps = m_ppsMap.getPS(ppsId);
4806  if (pps)
4807  {
4808    Int spsId = pps->getSPSId();
4809#if NH_MV
4810    if (!isIRAP && (spsId != m_activeSPSId[ layerId ] ))
4811#else
4812    if (!isIRAP && (spsId != m_activeSPSId ))
4813#endif
4814    {
4815      printf("Warning: tried to activate PPS referring to a inactive SPS at non-IDR.");
4816    }
4817    else
4818    {
4819      TComSPS *sps = m_spsMap.getPS(spsId);
4820      if (sps)
4821      {
4822        Int vpsId = sps->getVPSId();
4823        if (!isIRAP && (vpsId != m_activeVPSId ))
4824        {
4825          printf("Warning: tried to activate PPS referring to a inactive VPS at non-IDR.");
4826        }
4827        else
4828        {
4829          TComVPS *vps =m_vpsMap.getPS(vpsId);
4830          if (vps)
4831          {
4832            m_activeVPSId = vpsId;
4833#if NH_MV
4834            m_activeSPSId[layerId] = spsId;
4835#else
4836            m_activeSPSId = spsId;
4837#endif
4838            return true;
4839          }
4840          else
4841          {
4842            printf("Warning: tried to activate PPS that refers to a non-existing VPS.");
4843          }
4844        }
4845      }
4846      else
4847      {
4848       printf("Warning: tried to activate a PPS that refers to a non-existing SPS.");
4849      }
4850    }
4851  }
4852  else
4853  {
4854    printf("Warning: tried to activate non-existing PPS.");
4855  }
4856
4857  // Failed to activate if reach here.
4858#if NH_MV
4859  m_activeSPSId[layerId]=-1;
4860#else
4861  m_activeSPSId=-1;
4862#endif
4863  m_activeVPSId=-1;
4864
4865  return false;
4866}
4867
4868ProfileTierLevel::ProfileTierLevel()
4869  : m_profileSpace    (0)
4870  , m_tierFlag        (Level::MAIN)
4871  , m_profileIdc      (Profile::NONE)
4872  , m_levelIdc        (Level::NONE)
4873  , m_progressiveSourceFlag  (false)
4874  , m_interlacedSourceFlag   (false)
4875  , m_nonPackedConstraintFlag(false)
4876  , m_frameOnlyConstraintFlag(false)
4877#if NH_MV
4878  , m_intraConstraintFlag         ( false )
4879  , m_onePictureOnlyConstraintFlag( false )
4880  , m_lowerBitRateConstraintFlag  ( false )
4881  , m_max12bitConstraintFlag      ( false )
4882  , m_max10bitConstraintFlag      ( false )
4883  , m_max8bitConstraintFlag       ( false )
4884  , m_max422chromaConstraintFlag  ( false )
4885  , m_max420chromaConstraintFlag  ( false )
4886  , m_maxMonochromeConstraintFlag ( false ) 
4887  , m_inbldFlag                   ( false )
4888#endif
4889{
4890  ::memset(m_profileCompatibilityFlag, 0, sizeof(m_profileCompatibilityFlag));
4891}
4892
4893#if NH_MV
4894Bool ProfileTierLevel::getV2ConstraintsPresentFlag() const
4895{
4896  return ( 
4897    getProfileIdc( ) ==  4 || getProfileCompatibilityFlag( 4 ) || getProfileIdc( ) ==  5 || getProfileCompatibilityFlag( 5 )  ||
4898    getProfileIdc( ) ==  6 || getProfileCompatibilityFlag( 6 ) || getProfileIdc( ) ==  7 || getProfileCompatibilityFlag( 7 ) 
4899    );
4900}
4901
4902Bool ProfileTierLevel::getInbldPresentFlag() const
4903{
4904  return ( 
4905    ( getProfileIdc() >= 1 && getProfileIdc() <= 5 )  || getProfileCompatibilityFlag( 1 ) || getProfileCompatibilityFlag( 2 ) || 
4906    getProfileCompatibilityFlag( 3 ) || getProfileCompatibilityFlag( 4 )  ||   getProfileCompatibilityFlag( 5 ) 
4907    );
4908}
4909
4910Void ProfileTierLevel::copyV2ConstraintFlags(ProfileTierLevel* ptlRef)
4911{
4912  setMax12bitConstraintFlag         ( ptlRef->getMax12bitConstraintFlag       ( ) );
4913  setMax10bitConstraintFlag         ( ptlRef->getMax10bitConstraintFlag       ( ) );
4914  setMax8bitConstraintFlag          ( ptlRef->getMax8bitConstraintFlag        ( ) );
4915  setMax422chromaConstraintFlag     ( ptlRef->getMax422chromaConstraintFlag   ( ) );
4916  setMax420chromaConstraintFlag     ( ptlRef->getMax420chromaConstraintFlag   ( ) );
4917  setMaxMonochromeConstraintFlag    ( ptlRef->getMaxMonochromeConstraintFlag  ( ) );
4918  setIntraConstraintFlag            ( ptlRef->getIntraConstraintFlag          ( ) );
4919  setOnePictureOnlyConstraintFlag   ( ptlRef->getOnePictureOnlyConstraintFlag ( ) );
4920  setLowerBitRateConstraintFlag     ( ptlRef->getLowerBitRateConstraintFlag   ( ) );
4921}
4922
4923Void ProfileTierLevel::copyProfile(ProfileTierLevel* ptlRef)
4924{
4925  setProfileSpace            ( ptlRef->getProfileSpace              ( ) );
4926  setTierFlag                ( ptlRef->getTierFlag                  ( ) );
4927  setProfileIdc              ( ptlRef->getProfileIdc                ( ) );
4928  for (Int j = 0; j < 32; j++)
4929  {     
4930    setProfileCompatibilityFlag(j, ptlRef->getProfileCompatibilityFlag  ( j ) );           
4931  }
4932  setProgressiveSourceFlag   ( ptlRef->getProgressiveSourceFlag     ( ) );
4933  setInterlacedSourceFlag    ( ptlRef->getInterlacedSourceFlag      ( ) );
4934  setNonPackedConstraintFlag ( ptlRef->getNonPackedConstraintFlag   ( ) );
4935  setFrameOnlyConstraintFlag ( ptlRef->getFrameOnlyConstraintFlag   ( ) );
4936  copyV2ConstraintFlags      ( ptlRef );
4937}
4938#endif
4939
4940TComPTL::TComPTL()
4941{
4942  ::memset(m_subLayerProfilePresentFlag, 0, sizeof(m_subLayerProfilePresentFlag));
4943  ::memset(m_subLayerLevelPresentFlag,   0, sizeof(m_subLayerLevelPresentFlag  ));
4944}
4945
4946Void calculateParameterSetChangedFlag(Bool &bChanged, const std::vector<UChar> *pOldData, const std::vector<UChar> &newData)
4947{
4948  if (!bChanged)
4949  {
4950    if ((pOldData==0 && pOldData!=0) || (pOldData!=0 && pOldData==0))
4951    {
4952      bChanged=true;
4953    }
4954    else if (pOldData!=0 && pOldData!=0)
4955    {
4956      // compare the two
4957      if (pOldData->size() != pOldData->size())
4958      {
4959        bChanged=true;
4960      }
4961      else
4962      {
4963        const UChar *pNewDataArray=&(newData)[0];
4964        const UChar *pOldDataArray=&(*pOldData)[0];
4965        if (memcmp(pOldDataArray, pNewDataArray, pOldData->size()))
4966        {
4967          bChanged=true;
4968        }
4969      }
4970    }
4971  }
4972}
4973#if NH_MV
4974Void TComPTL::inferGeneralValues(Bool profilePresentFlag, Int k, TComPTL* refPTL)
4975{
4976  ProfileTierLevel* refProfileTierLevel = NULL; 
4977  if ( k > 0 )
4978  {   
4979    assert( refPTL != NULL);
4980    refProfileTierLevel = refPTL->getGeneralPTL(); 
4981  }
4982
4983  ProfileTierLevel* curProfileTierLevel = getGeneralPTL( ); 
4984  assert( curProfileTierLevel != NULL ); 
4985
4986  if( !profilePresentFlag )
4987  {
4988    assert( k > 0 ); 
4989    assert( refProfileTierLevel != NULL ); 
4990    curProfileTierLevel->copyProfile( refProfileTierLevel);
4991  }
4992  else
4993  {
4994    if ( !curProfileTierLevel->getV2ConstraintsPresentFlag() )
4995    {
4996      curProfileTierLevel->setMax12bitConstraintFlag         ( false );
4997      curProfileTierLevel->setMax10bitConstraintFlag         ( false );
4998      curProfileTierLevel->setMax8bitConstraintFlag          ( false );
4999      curProfileTierLevel->setMax422chromaConstraintFlag     ( false );
5000      curProfileTierLevel->setMax420chromaConstraintFlag     ( false );
5001      curProfileTierLevel->setMaxMonochromeConstraintFlag    ( false );
5002      curProfileTierLevel->setIntraConstraintFlag            ( false );
5003      curProfileTierLevel->setOnePictureOnlyConstraintFlag   ( false );
5004      curProfileTierLevel->setLowerBitRateConstraintFlag     ( false );   
5005    }
5006
5007    if ( !curProfileTierLevel->getInbldPresentFlag() )
5008    {
5009      curProfileTierLevel->setInbldFlag( false ); 
5010    }     
5011  }
5012}
5013
5014Void TComPTL::inferSubLayerValues(Int maxNumSubLayersMinus1, Int k, TComPTL* refPTL)
5015{
5016  assert( k == 0 || refPTL != NULL );   
5017  for (Int i = maxNumSubLayersMinus1; i >= 0; i--)
5018  {
5019    ProfileTierLevel* refProfileTierLevel;
5020    if ( k != 0 )
5021    {
5022      refProfileTierLevel = refPTL->getSubLayerPTL( i );
5023    }
5024    else
5025    {
5026      if ( i == maxNumSubLayersMinus1)     
5027      {
5028        refProfileTierLevel = getGeneralPTL();
5029      }
5030      else
5031      {
5032        refProfileTierLevel = getSubLayerPTL( i + 1 );
5033      }
5034    }   
5035
5036    assert( refProfileTierLevel != NULL ); 
5037    ProfileTierLevel* curProfileTierLevel = getSubLayerPTL( i ); 
5038    assert( curProfileTierLevel != NULL ); 
5039    if( !getSubLayerLevelPresentFlag( i ) )
5040    {
5041      curProfileTierLevel->setLevelIdc( refProfileTierLevel->getLevelIdc() ); 
5042    }
5043
5044    if( !getSubLayerProfilePresentFlag( i ) )
5045    {
5046      curProfileTierLevel->copyProfile( refProfileTierLevel);
5047    }
5048    else
5049    {
5050      if ( !curProfileTierLevel->getV2ConstraintsPresentFlag() )
5051      {
5052        curProfileTierLevel->copyV2ConstraintFlags( refProfileTierLevel ); 
5053      }
5054
5055      if ( !curProfileTierLevel->getInbldPresentFlag() )
5056      {
5057        curProfileTierLevel->setInbldFlag( refProfileTierLevel->getInbldFlag() ); 
5058      }     
5059    }     
5060  }
5061}
5062
5063#endif
5064
5065//! \}
5066
5067#if NH_MV
5068Void TComVPSVUI::init( Int numLayerSets, Int maxNumSubLayers, Int maxNumLayers )
5069{
5070  m_crossLayerIrapAlignedFlag = true; 
5071  m_allLayersIdrAlignedFlag   = false; 
5072  m_bitRatePresentVpsFlag     = false;
5073  m_picRatePresentVpsFlag     = false;
5074
5075  m_bitRatePresentFlag          .resize(numLayerSets); 
5076  m_picRatePresentFlag          .resize(numLayerSets); 
5077  m_avgBitRate                  .resize(numLayerSets); 
5078  m_maxBitRate                  .resize(numLayerSets); 
5079  m_constantPicRateIdc          .resize(numLayerSets); 
5080  m_avgPicRate                  .resize(numLayerSets); 
5081
5082  for ( Int i = 0; i < numLayerSets; i++)
5083  { 
5084    m_bitRatePresentFlag          [i].resize( maxNumSubLayers); 
5085    m_picRatePresentFlag          [i].resize( maxNumSubLayers); 
5086    m_avgBitRate                  [i].resize( maxNumSubLayers); 
5087    m_maxBitRate                  [i].resize( maxNumSubLayers); 
5088    m_constantPicRateIdc          [i].resize( maxNumSubLayers); 
5089    m_avgPicRate                  [i].resize( maxNumSubLayers); 
5090    for ( Int j = 0; j < maxNumSubLayers; j++)
5091    {   
5092      m_bitRatePresentFlag          [i][j] = false;
5093      m_picRatePresentFlag          [i][j] = false;
5094      m_avgBitRate                  [i][j] = -1;
5095      m_maxBitRate                  [i][j] = -1;
5096      m_constantPicRateIdc          [i][j] = -1;
5097      m_avgPicRate                  [i][j] = -1;
5098    }
5099  }
5100
5101  m_ilpRestrictedRefLayersFlag = false;
5102
5103  m_tileBoundariesAlignedFlag             .resize( maxNumLayers );
5104  m_minSpatialSegmentOffsetPlus1          .resize( maxNumLayers );
5105  m_ctuBasedOffsetEnabledFlag             .resize( maxNumLayers );
5106  m_minHorizontalCtuOffsetPlus1           .resize( maxNumLayers );
5107  m_baseLayerParameterSetCompatibilityFlag.resize( maxNumLayers );
5108
5109  for ( Int i = 0; i < maxNumLayers; i++)
5110  {         
5111    m_tileBoundariesAlignedFlag   [i].resize( maxNumLayers );
5112    m_minSpatialSegmentOffsetPlus1[i].resize( maxNumLayers );
5113    m_ctuBasedOffsetEnabledFlag   [i].resize( maxNumLayers );
5114    m_minHorizontalCtuOffsetPlus1 [i].resize( maxNumLayers );
5115    for ( Int j = 0; j < maxNumLayers; j++)
5116    {   
5117      m_tileBoundariesAlignedFlag   [i][j] = false;
5118      m_minSpatialSegmentOffsetPlus1[i][j] = 0;
5119      m_ctuBasedOffsetEnabledFlag   [i][j] = false;
5120      m_minHorizontalCtuOffsetPlus1 [i][j] = -1;
5121    }
5122    m_baseLayerParameterSetCompatibilityFlag[i] = false;
5123  }
5124  m_vpsVuiBspHrdPresentFlag = false; 
5125}
5126
5127Void TComRepFormat::inferChromaAndBitDepth( const TComRepFormat* prevRepFormat )
5128{
5129    setChromaAndBitDepthVpsPresentFlag( prevRepFormat->getChromaAndBitDepthVpsPresentFlag() );
5130    setSeparateColourPlaneVpsFlag     ( prevRepFormat->getSeparateColourPlaneVpsFlag     () );
5131    setBitDepthVpsLumaMinus8          ( prevRepFormat->getBitDepthVpsLumaMinus8          () );
5132    setBitDepthVpsChromaMinus8        ( prevRepFormat->getBitDepthVpsChromaMinus8        () );
5133}
5134
5135Void TComRepFormat::checkInferChromaAndBitDepth( const TComRepFormat* prevRepFormat ) const 
5136{
5137    assert( getChromaAndBitDepthVpsPresentFlag() == prevRepFormat->getChromaAndBitDepthVpsPresentFlag() );
5138    assert( getSeparateColourPlaneVpsFlag     () == prevRepFormat->getSeparateColourPlaneVpsFlag     () );
5139    assert( getBitDepthVpsLumaMinus8          () == prevRepFormat->getBitDepthVpsLumaMinus8          () );
5140    assert( getBitDepthVpsChromaMinus8        () == prevRepFormat->getBitDepthVpsChromaMinus8        () );
5141}
5142
5143Int TComVpsVuiBspHrdParameters::getBspHrdIdxLen( const TComVPS* vps) const
5144{
5145  return gCeilLog2( vps->getNumHrdParameters() + getVpsNumAddHrdParams() );
5146}
5147
5148Void TComVpsVuiBspHrdParameters::createAfterVpsNumAddHrdParams( const TComVPS* vps )
5149{
5150  m_offsetHrdParamIdx = vps->getNumHrdParameters(); 
5151  m_numHrdParam       = vps->getNumHrdParameters() + getVpsNumAddHrdParams() - m_offsetHrdParamIdx;
5152  m_numOls            = vps->getNumOutputLayerSets(); 
5153
5154  m_cprmsAddPresentFlag .resize( m_numHrdParam );
5155  m_numSubLayerHrdMinus1.resize( m_numHrdParam );
5156  m_hrdParameters       .resize( m_numHrdParam );
5157
5158  m_numSignalledPartitioningSchemes .resize( m_numOls ); 
5159  m_numPartitionsInSchemeMinus1     .resize( m_numOls );
5160  m_numBspSchedulesMinus1           .resize( m_numOls ); 
5161  m_bspHrdIdx                       .resize( m_numOls );
5162  m_bspSchedIdx                     .resize( m_numOls );
5163}
5164
5165Void TComVpsVuiBspHrdParameters::createAfterNumSignalledPartitioningSchemes( const TComVPS* vps, Int h )
5166{
5167  m_numPartitionsInSchemeMinus1 [h].resize( getNumSignalledPartitioningSchemes(h) );
5168  m_layerIncludedInPartitionFlag[h].resize( getNumSignalledPartitioningSchemes(h) );   
5169
5170  m_numBspSchedulesMinus1       [h].resize( getNumSignalledPartitioningSchemes(h) + 1 );
5171  for (Int i = 0; i < getNumSignalledPartitioningSchemes(h) + 1; i++)
5172  {
5173    Int tMax = vps->getMaxSubLayersInLayerSetMinus1( m_vps->olsIdxToLsIdx(h) ) + 1;
5174    m_numBspSchedulesMinus1[h][i].resize( tMax );
5175    m_bspHrdIdx            [h][i].resize( tMax );
5176    m_bspSchedIdx          [h][i].resize( tMax );
5177  }
5178}
5179
5180Void TComVpsVuiBspHrdParameters::createAfterNumPartitionsInSchemeMinus1( const TComVPS* vps, Int h, Int j )
5181{
5182  m_layerIncludedInPartitionFlag[h][j].resize( getNumPartitionsInSchemeMinus1(h,j));
5183  for( Int k = 0; k < getNumPartitionsInSchemeMinus1(h,j); k++ )
5184  {
5185    m_layerIncludedInPartitionFlag[h][j][k].resize( m_vps->getNumLayersInIdList( vps->olsIdxToLsIdx(h)));
5186  }
5187}
5188
5189Void TComVpsVuiBspHrdParameters::createAfterNumBspSchedulesMinus1( const TComVPS* vps, Int h, Int i, Int t )
5190{
5191  m_bspSchedIdx[h][i][t].resize( getNumBspSchedulesMinus1( h, i, t ) + 1 );
5192  m_bspHrdIdx  [h][i][t].resize( getNumBspSchedulesMinus1( h, i, t ) + 1 );
5193  for( Int j = 0; j < getNumBspSchedulesMinus1( h, i, t ) + 1; j++ )
5194  {
5195    m_bspSchedIdx[h][i][t][j].resize( getNumPartitionsInSchemeMinus1( h, i ) );
5196    m_bspHrdIdx  [h][i][t][j].resize( getNumPartitionsInSchemeMinus1( h, i ) );
5197  }
5198}
5199
5200
5201Void TComVUI::inferVideoSignalInfo( const TComVPS* vps, Int layerIdCurr )
5202{
5203  if ( layerIdCurr == 0 || !vps->getVpsVuiPresentFlag() ) 
5204  {
5205    return; 
5206  }
5207
5208  const TComVPSVUI* vpsVui = vps->getVPSVUI(); 
5209  assert( vpsVui != NULL ); 
5210
5211  const TComVideoSignalInfo* videoSignalInfo = vpsVui->getVideoSignalInfo( vpsVui->getVpsVideoSignalInfoIdx( vps->getLayerIdInVps( layerIdCurr ) ) ); 
5212  assert( videoSignalInfo != NULL );
5213
5214  setVideoFormat            ( videoSignalInfo->getVideoVpsFormat            () ); 
5215  setVideoFullRangeFlag     ( videoSignalInfo->getVideoFullRangeVpsFlag     () );
5216  setColourPrimaries        ( videoSignalInfo->getColourPrimariesVps        () );
5217  setTransferCharacteristics( videoSignalInfo->getTransferCharacteristicsVps() );
5218  setMatrixCoefficients     ( videoSignalInfo->getMatrixCoeffsVps           () );
5219}
5220
5221Void TComDpbSize::init( Int numOutputLayerSets, Int maxNumLayerIds, Int maxNumSubLayers ) 
5222{
5223  m_subLayerFlagInfoPresentFlag.resize( numOutputLayerSets );
5224  m_subLayerDpbInfoPresentFlag .resize( numOutputLayerSets );
5225  m_maxVpsDecPicBufferingMinus1.resize( numOutputLayerSets ); 
5226  m_maxVpsNumReorderPics       .resize( numOutputLayerSets );
5227  m_maxVpsLatencyIncreasePlus1 .resize( numOutputLayerSets );
5228
5229  for (Int i = 0; i < numOutputLayerSets; i++ )
5230  {     
5231    m_subLayerFlagInfoPresentFlag[i]  = false;
5232
5233    m_subLayerDpbInfoPresentFlag [i].resize( maxNumSubLayers );
5234    m_maxVpsDecPicBufferingMinus1[i].resize( maxNumLayerIds ); 
5235    m_maxVpsNumReorderPics       [i].resize( maxNumSubLayers );
5236    m_maxVpsLatencyIncreasePlus1 [i].resize( maxNumSubLayers );   
5237
5238    for (Int j = 0; j < maxNumSubLayers; j++  )
5239    {       
5240      m_subLayerDpbInfoPresentFlag [i][j] = ( j == 0) ;
5241      m_maxVpsNumReorderPics       [i][j] = 0;
5242      m_maxVpsLatencyIncreasePlus1 [i][j] = 0;
5243    }
5244
5245    for (Int k = 0; k < maxNumLayerIds; k++ )
5246    {
5247      m_maxVpsDecPicBufferingMinus1[i][k].resize( maxNumSubLayers );
5248      for (Int j = 0; j < maxNumSubLayers; j++  )
5249      {       
5250        m_maxVpsDecPicBufferingMinus1[i][k][j] = MIN_INT; 
5251      }
5252    }
5253  }
5254}
5255
5256
5257Int TComDpbSize::getVpsMaxLatencyPictures( Int i, Int j ) const
5258{
5259  return getMaxVpsNumReorderPics( i, j ) + getMaxVpsLatencyIncreasePlus1(i, j) - 1; 
5260}
5261
5262Void Window::scaleOffsets( Int scal )
5263{
5264  if (! m_scaledFlag )
5265  {
5266    m_scaledFlag         = true; 
5267    m_winLeftOffset     *= scal; 
5268    m_winRightOffset    *= scal; 
5269    m_winTopOffset      *= scal; 
5270    m_winBottomOffset   *= scal; 
5271  }
5272}
5273
5274Void TComStRefPicSet::inferRps( Int stRpsIdx, TComSPS* sps, Bool encoder )
5275{
5276  if ( getInterRefPicSetPredictionFlag() )
5277  {
5278    // When inter_ref_pic_set_prediction_flag is equal to 1, the variables DeltaPocS0[ stRpsIdx ][ i ], UsedByCurrPicS0[ stRpsIdx ][ i ],
5279    // NumNegativePics[ stRpsIdx ], DeltaPocS1[ stRpsIdx ][ i ], UsedByCurrPicS1[ stRpsIdx ][ i ] and NumPositivePics[ stRpsIdx ] are
5280    // derived as follows:
5281
5282    Int i = 0;
5283    Int refRpsIdx = getRefRpsIdx( stRpsIdx );
5284    TComStRefPicSet* refRps = sps->getStRefPicSet( refRpsIdx ); 
5285
5286    for( Int j = refRps->getNumPositivePicsVar( ) - 1; j >= 0; j-- ) 
5287    {
5288      Int dPoc = refRps->getDeltaPocS1Var( j ) + getDeltaRps();
5289      if( dPoc < 0  &&  getUseDeltaFlag( refRps->getNumNegativePicsVar( ) + j ) ) 
5290      {
5291        setDeltaPocS0Var     ( i, dPoc );
5292        setUsedByCurrPicS0Var( i++ , getUsedByCurrPicFlag( refRps->getNumNegativePicsVar( ) + j ) );
5293      }
5294    }
5295    if( getDeltaRps() < 0  && getUseDeltaFlag( refRps->getNumDeltaPocs() ) )   //   (7 59)
5296    {
5297      setDeltaPocS0Var( i,  getDeltaRps() ); 
5298      setUsedByCurrPicS0Var( i++ , getUsedByCurrPicFlag( refRps->getNumDeltaPocs() ) );
5299    }
5300    for( Int j = 0; j < refRps->getNumNegativePicsVar(); j++ )
5301    {
5302      Int dPoc = refRps->getDeltaPocS0Var( j ) + getDeltaRps(); 
5303      if( dPoc < 0  &&  getUseDeltaFlag( j ) ) 
5304      {
5305        setDeltaPocS0Var( i , dPoc);
5306        setUsedByCurrPicS0Var( i++ , getUsedByCurrPicFlag( j )) ;
5307      }
5308    }
5309
5310    setNumNegativePicsVar( i );
5311
5312    i = 0;
5313    for( Int j = refRps->getNumNegativePicsVar() - 1; j  >=  0; j-- )
5314    {
5315      Int dPoc = refRps->getDeltaPocS0Var( j ) + getDeltaRps(); 
5316      if( dPoc > 0  &&  getUseDeltaFlag( j ) )
5317      {
5318        setDeltaPocS1Var( i, dPoc );
5319        setUsedByCurrPicS1Var(  i++, getUsedByCurrPicFlag( j ) ) ;
5320      }
5321    }
5322
5323    if( getDeltaRps() > 0  &&  getUseDeltaFlag( refRps->getNumDeltaPocs() ) ) //  (7 60)
5324    { 
5325      setDeltaPocS1Var( i , getDeltaRps() );
5326      setUsedByCurrPicS1Var( i++ , getUsedByCurrPicFlag( refRps->getNumDeltaPocs() ));
5327    }
5328
5329    for( Int j = 0; j < refRps->getNumPositivePicsVar( ); j++) 
5330    {
5331      Int dPoc = refRps->getDeltaPocS1Var( j ) + getDeltaRps(); 
5332      if( dPoc > 0  &&  getUseDeltaFlag( refRps->getNumNegativePicsVar() + j ) ) 
5333      {
5334        setDeltaPocS1Var( i, dPoc);
5335        setUsedByCurrPicS1Var( i++, getUsedByCurrPicFlag( refRps->getNumNegativePicsVar() + j ));
5336      }
5337    }
5338    setNumPositivePicsVar(  i );
5339  }
5340  else
5341  {
5342    // When inter_ref_pic_set_prediction_flag is equal to 0, the variables NumNegativePics[ stRpsIdx ], NumPositivePics[ stRpsIdx ],
5343    // UsedByCurrPicS0[ stRpsIdx ][ i ], UsedByCurrPicS1[ stRpsIdx ][ i ], DeltaPocS0[ stRpsIdx ][ i ] and DeltaPocS1[ stRpsIdx ][ i ]
5344    // are derived as follows:
5345
5346    setNumNegativePicsVar( getNumNegativePics( ) );        //  (7 61)
5347    setNumPositivePicsVar( getNumPositivePics( ) );         //  (7 62)
5348
5349    for (Int i = 0 ; i < getNumNegativePics(); i++ )
5350    {     
5351      setUsedByCurrPicS0Var( i,  getUsedByCurrPicS0Flag( i ) ); //  (7 63)
5352      if (i == 0 )
5353      {
5354        setDeltaPocS0Var( i , -( getDeltaPocS0Minus1( i ) + 1 )); // (7 65)
5355      }
5356      else
5357      {
5358        setDeltaPocS0Var( i , getDeltaPocS0Var( i - 1 ) - ( getDeltaPocS0Minus1( i ) + 1 )); //  (7 67)
5359      }
5360    }
5361
5362    for (Int i = 0 ; i < getNumPositivePics(); i++ )
5363    {     
5364      setUsedByCurrPicS1Var( i,  getUsedByCurrPicS1Flag( i ) ); //  (7 64)
5365
5366      if (i == 0 )
5367      {
5368        setDeltaPocS1Var( i , getDeltaPocS1Minus1( i ) + 1    );      // (7 66)
5369      }
5370      else
5371      {
5372        setDeltaPocS1Var( i , getDeltaPocS1Var( i - 1 ) + ( getDeltaPocS1Minus1( i ) + 1 )); //  (7 68)
5373      }
5374    }
5375  }
5376}
5377
5378#endif
Note: See TracBrowser for help on using the repository browser.