source: 3DVCSoftware/branches/HTM-15.2-dev/source/Lib/TLibCommon/TComSlice.cpp @ 1373

Last change on this file since 1373 was 1373, checked in by tech, 8 years ago

Macro fixes.

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