source: 3DVCSoftware/branches/HTM-16.0-MV-draft-5/source/Lib/TLibCommon/TComSlice.cpp @ 1417

Last change on this file since 1417 was 1390, checked in by tech, 9 years ago

Removed 3D.

  • Property svn:eol-style set to native
File size: 144.4 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_MV
121, m_pocResetFlag                  (false)
122, m_crossLayerBlaFlag             (false)
123#endif
124, m_discardableFlag               (false)
125, m_interLayerPredEnabledFlag     (false)
126, m_numInterLayerRefPicsMinus1    (0)
127#if NH_MV
128, m_sliceSegmentHeaderExtensionLength (0)
129, m_pocResetIdc                   (0)
130, m_pocResetPeriodId              (0)
131, m_hasPocResetPeriodIdPresent    (false)
132, m_fullPocResetFlag              (false)
133, m_pocLsbVal                     (0)
134, m_pocMsbCycleValPresentFlag     (false)
135, m_pocMsbCycleVal                (0)
136, m_pocMsbValRequiredFlag         (false)
137#endif
138#endif
139{
140
141#if NH_MV
142  m_shortTermRefPicSetIdx = 0; 
143  m_numLongTermSps        = 0; 
144  m_numLongTermPics       = 0; 
145  for (Int i = 0; i < MAX_NUM_PICS_RPS; i++)
146  {
147    m_ltIdxSps          [i] = 0; 
148    m_deltaPocMsbCycleLt[i] = 0;
149  }
150  setSliceTemporalMvpEnabledFlag( false ); 
151#endif
152  for(UInt i=0; i<NUM_REF_PIC_LIST_01; i++)
153  {
154    m_aiNumRefIdx[i] = 0;
155  }
156
157  for (UInt component = 0; component < MAX_NUM_COMPONENT; component++)
158  {
159    m_lambdas            [component] = 0.0;
160    m_iSliceChromaQpDelta[component] = 0;
161  }
162
163  initEqualRef();
164
165  for ( Int idx = 0; idx < MAX_NUM_REF; idx++ )
166  {
167    m_list1IdxToList0Idx[idx] = -1;
168  }
169
170  for(Int iNumCount = 0; iNumCount < MAX_NUM_REF; iNumCount++)
171  {
172    for(UInt i=0; i<NUM_REF_PIC_LIST_01; i++)
173    {
174      m_apcRefPicList [i][iNumCount] = NULL;
175      m_aiRefPOCList  [i][iNumCount] = 0;
176    }
177#if NH_MV
178    m_aiRefLayerIdList[0][iNumCount] = 0;
179    m_aiRefLayerIdList[1][iNumCount] = 0;
180#endif
181  }
182
183  resetWpScaling();
184  initWpAcDcParam();
185
186  for(Int ch=0; ch < MAX_NUM_CHANNEL_TYPE; ch++)
187  {
188    m_saoEnabledFlag[ch] = false;
189  }
190#if NH_MV
191  for (Int i = 0; i < MAX_NUM_LAYERS; i++ )
192  {
193    m_interLayerPredLayerIdc[ i ] = -1;
194  }
195#endif
196
197}
198
199TComSlice::~TComSlice()
200{
201
202}
203
204
205Void TComSlice::initSlice()
206{
207  for(UInt i=0; i<NUM_REF_PIC_LIST_01; i++)
208  {
209    m_aiNumRefIdx[i]      = 0;
210  }
211  m_colFromL0Flag = true;
212
213  m_colRefIdx = 0;
214  initEqualRef();
215
216  m_bCheckLDC = false;
217
218  for (UInt component = 0; component < MAX_NUM_COMPONENT; component++)
219  {
220    m_iSliceChromaQpDelta[component] = 0;
221  }
222  m_maxNumMergeCand = MRG_MAX_NUM_CANDS;
223
224  m_bFinalized=false;
225
226  m_substreamSizes.clear();
227  m_cabacInitFlag        = false;
228  m_enableTMVPFlag = true;
229}
230
231Bool TComSlice::getRapPicFlag() const
232{
233  return getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_W_RADL
234      || getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_N_LP
235      || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP
236      || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL
237      || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP
238      || getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA;
239}
240
241
242Void  TComSlice::sortPicList        (TComList<TComPic*>& rcListPic)
243{
244  TComPic*    pcPicExtract;
245  TComPic*    pcPicInsert;
246
247  TComList<TComPic*>::iterator    iterPicExtract;
248  TComList<TComPic*>::iterator    iterPicExtract_1;
249  TComList<TComPic*>::iterator    iterPicInsert;
250
251  for (Int i = 1; i < (Int)(rcListPic.size()); i++)
252  {
253    iterPicExtract = rcListPic.begin();
254    for (Int j = 0; j < i; j++)
255    {
256      iterPicExtract++;
257    }
258    pcPicExtract = *(iterPicExtract);
259    pcPicExtract->setCurrSliceIdx(0);
260
261    iterPicInsert = rcListPic.begin();
262    while (iterPicInsert != iterPicExtract)
263    {
264      pcPicInsert = *(iterPicInsert);
265      pcPicInsert->setCurrSliceIdx(0);
266      if (pcPicInsert->getPOC() >= pcPicExtract->getPOC())
267      {
268        break;
269      }
270
271      iterPicInsert++;
272    }
273
274    iterPicExtract_1 = iterPicExtract;    iterPicExtract_1++;
275
276    //  swap iterPicExtract and iterPicInsert, iterPicExtract = curr. / iterPicInsert = insertion position
277    rcListPic.insert (iterPicInsert, iterPicExtract, iterPicExtract_1);
278    rcListPic.erase  (iterPicExtract);
279  }
280}
281
282TComPic* TComSlice::xGetRefPic (TComList<TComPic*>& rcListPic, Int poc)
283{
284  TComList<TComPic*>::iterator  iterPic = rcListPic.begin();
285  TComPic*                      pcPic = *(iterPic);
286  while ( iterPic != rcListPic.end() )
287  {
288    if(pcPic->getPOC() == poc)
289    {
290      break;
291    }
292    iterPic++;
293    pcPic = *(iterPic);
294  }
295  return  pcPic;
296}
297
298
299TComPic* TComSlice::xGetLongTermRefPic(TComList<TComPic*>& rcListPic, Int poc, Bool pocHasMsb)
300{
301  TComList<TComPic*>::iterator  iterPic = rcListPic.begin();
302  TComPic*                      pcPic = *(iterPic);
303  TComPic*                      pcStPic = pcPic;
304
305  Int pocCycle = 1 << getSPS()->getBitsForPOC();
306  if (!pocHasMsb)
307  {
308    poc = poc & (pocCycle - 1);
309  }
310
311  while ( iterPic != rcListPic.end() )
312  {
313    pcPic = *(iterPic);
314    if (pcPic && pcPic->getPOC()!=this->getPOC() && pcPic->getSlice( 0 )->isReferenced())
315    {
316      Int picPoc = pcPic->getPOC();
317      if (!pocHasMsb)
318      {
319        picPoc = picPoc & (pocCycle - 1);
320      }
321
322      if (poc == picPoc)
323      {
324        if(pcPic->getIsLongTerm())
325        {
326          return pcPic;
327        }
328        else
329        {
330          pcStPic = pcPic;
331        }
332        break;
333      }
334    }
335
336    iterPic++;
337  }
338
339  return  pcStPic;
340}
341
342Void TComSlice::setRefPOCList       ()
343{
344  for (Int iDir = 0; iDir < NUM_REF_PIC_LIST_01; iDir++)
345  {
346    for (Int iNumRefIdx = 0; iNumRefIdx < m_aiNumRefIdx[iDir]; iNumRefIdx++)
347    {
348      m_aiRefPOCList[iDir][iNumRefIdx] = m_apcRefPicList[iDir][iNumRefIdx]->getPOC();
349#if NH_MV
350      m_aiRefLayerIdList[iDir][iNumRefIdx] = m_apcRefPicList[iDir][iNumRefIdx]->getLayerId();
351#endif
352    }
353  }
354
355}
356
357Void TComSlice::setList1IdxToList0Idx()
358{
359  Int idxL0, idxL1;
360  for ( idxL1 = 0; idxL1 < getNumRefIdx( REF_PIC_LIST_1 ); idxL1++ )
361  {
362    m_list1IdxToList0Idx[idxL1] = -1;
363    for ( idxL0 = 0; idxL0 < getNumRefIdx( REF_PIC_LIST_0 ); idxL0++ )
364    {
365      if ( m_apcRefPicList[REF_PIC_LIST_0][idxL0]->getPOC() == m_apcRefPicList[REF_PIC_LIST_1][idxL1]->getPOC() )
366      {
367        m_list1IdxToList0Idx[idxL1] = idxL0;
368        break;
369      }
370    }
371  }
372}
373
374#if !NH_MV
375Void TComSlice::setRefPicList( TComList<TComPic*>& rcListPic, Bool checkNumPocTotalCurr )
376{
377    if (m_eSliceType == I_SLICE)
378    {
379      ::memset( m_apcRefPicList, 0, sizeof (m_apcRefPicList));
380      ::memset( m_aiNumRefIdx,   0, sizeof ( m_aiNumRefIdx ));
381
382    if (!checkNumPocTotalCurr)
383    {
384      return;
385    }
386  }
387
388  TComPic*  pcRefPic= NULL;
389  static const UInt MAX_NUM_NEGATIVE_PICTURES=16;
390  TComPic*  RefPicSetStCurr0[MAX_NUM_NEGATIVE_PICTURES];
391  TComPic*  RefPicSetStCurr1[MAX_NUM_NEGATIVE_PICTURES];
392  TComPic*  RefPicSetLtCurr[MAX_NUM_NEGATIVE_PICTURES];
393  UInt NumPicStCurr0 = 0;
394  UInt NumPicStCurr1 = 0;
395  UInt NumPicLtCurr = 0;
396  Int i;
397
398  for(i=0; i < m_pRPS->getNumberOfNegativePictures(); i++)
399  {
400    if(m_pRPS->getUsed(i))
401    {
402      pcRefPic = xGetRefPic(rcListPic, getPOC()+m_pRPS->getDeltaPOC(i));
403      pcRefPic->setIsLongTerm(0);
404      pcRefPic->getPicYuvRec()->extendPicBorder();
405      RefPicSetStCurr0[NumPicStCurr0] = pcRefPic;
406      NumPicStCurr0++;
407      pcRefPic->setCheckLTMSBPresent(false);
408    }
409  }
410
411  for(; i < m_pRPS->getNumberOfNegativePictures()+m_pRPS->getNumberOfPositivePictures(); i++)
412  {
413    if(m_pRPS->getUsed(i))
414    {
415      pcRefPic = xGetRefPic(rcListPic, getPOC()+m_pRPS->getDeltaPOC(i));
416      pcRefPic->setIsLongTerm(0);
417      pcRefPic->getPicYuvRec()->extendPicBorder();
418      RefPicSetStCurr1[NumPicStCurr1] = pcRefPic;
419      NumPicStCurr1++;
420      pcRefPic->setCheckLTMSBPresent(false);
421    }
422  }
423
424  for(i = m_pRPS->getNumberOfNegativePictures()+m_pRPS->getNumberOfPositivePictures()+m_pRPS->getNumberOfLongtermPictures()-1; i > m_pRPS->getNumberOfNegativePictures()+m_pRPS->getNumberOfPositivePictures()-1 ; i--)
425  {
426    if(m_pRPS->getUsed(i))
427    {
428      pcRefPic = xGetLongTermRefPic(rcListPic, m_pRPS->getPOC(i), m_pRPS->getCheckLTMSBPresent(i));
429      pcRefPic->setIsLongTerm(1);
430      pcRefPic->getPicYuvRec()->extendPicBorder();
431      RefPicSetLtCurr[NumPicLtCurr] = pcRefPic;
432      NumPicLtCurr++;
433    }
434    if(pcRefPic==NULL)
435    {
436      pcRefPic = xGetLongTermRefPic(rcListPic, m_pRPS->getPOC(i), m_pRPS->getCheckLTMSBPresent(i));
437    }
438    pcRefPic->setCheckLTMSBPresent(m_pRPS->getCheckLTMSBPresent(i));
439  }
440
441  // ref_pic_list_init
442  TComPic*  rpsCurrList0[MAX_NUM_REF+1];
443  TComPic*  rpsCurrList1[MAX_NUM_REF+1];
444  Int numPicTotalCurr = NumPicStCurr0 + NumPicStCurr1 + NumPicLtCurr;
445
446  if (checkNumPocTotalCurr)
447  {
448    // 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:
449    // - If the current picture is a BLA or CRA picture, the value of NumPocTotalCurr shall be equal to 0.
450    // - Otherwise, when the current picture contains a P or B slice, the value of NumPocTotalCurr shall not be equal to 0.
451    if (getRapPicFlag())
452    {
453      assert(numPicTotalCurr == 0);
454    }
455
456    if (m_eSliceType == I_SLICE)
457    {
458      return;
459    }
460
461    assert(numPicTotalCurr > 0);
462    // general tier and level limit:
463    assert(numPicTotalCurr <= 8);
464  }
465
466  Int cIdx = 0;
467  for ( i=0; i<NumPicStCurr0; i++, cIdx++)
468  {
469    rpsCurrList0[cIdx] = RefPicSetStCurr0[i];
470  }
471  for ( i=0; i<NumPicStCurr1; i++, cIdx++)
472  {
473    rpsCurrList0[cIdx] = RefPicSetStCurr1[i];
474  }
475  for ( i=0; i<NumPicLtCurr;  i++, cIdx++)
476  {
477    rpsCurrList0[cIdx] = RefPicSetLtCurr[i];
478  }
479  assert(cIdx == numPicTotalCurr);
480
481  if (m_eSliceType==B_SLICE)
482  {
483    cIdx = 0;
484    for ( i=0; i<NumPicStCurr1; i++, cIdx++)
485    {
486      rpsCurrList1[cIdx] = RefPicSetStCurr1[i];
487    }
488    for ( i=0; i<NumPicStCurr0; i++, cIdx++)
489    {
490      rpsCurrList1[cIdx] = RefPicSetStCurr0[i];
491    }
492    for ( i=0; i<NumPicLtCurr;  i++, cIdx++)
493    {
494      rpsCurrList1[cIdx] = RefPicSetLtCurr[i];
495    }
496    assert(cIdx == numPicTotalCurr);
497  }
498
499  ::memset(m_bIsUsedAsLongTerm, 0, sizeof(m_bIsUsedAsLongTerm));
500
501  for (Int rIdx = 0; rIdx < m_aiNumRefIdx[REF_PIC_LIST_0]; rIdx ++)
502  {
503    cIdx = m_RefPicListModification.getRefPicListModificationFlagL0() ? m_RefPicListModification.getRefPicSetIdxL0(rIdx) : rIdx % numPicTotalCurr;
504    assert(cIdx >= 0 && cIdx < numPicTotalCurr);
505    m_apcRefPicList[REF_PIC_LIST_0][rIdx] = rpsCurrList0[ cIdx ];
506    m_bIsUsedAsLongTerm[REF_PIC_LIST_0][rIdx] = ( cIdx >= NumPicStCurr0 + NumPicStCurr1 );
507  }
508  if ( m_eSliceType != B_SLICE )
509  {
510    m_aiNumRefIdx[REF_PIC_LIST_1] = 0;
511    ::memset( m_apcRefPicList[REF_PIC_LIST_1], 0, sizeof(m_apcRefPicList[REF_PIC_LIST_1]));
512  }
513  else
514  {
515    for (Int rIdx = 0; rIdx < m_aiNumRefIdx[REF_PIC_LIST_1]; rIdx ++)
516    {
517      cIdx = m_RefPicListModification.getRefPicListModificationFlagL1() ? m_RefPicListModification.getRefPicSetIdxL1(rIdx) : rIdx % numPicTotalCurr;
518      assert(cIdx >= 0 && cIdx < numPicTotalCurr);
519      m_apcRefPicList[REF_PIC_LIST_1][rIdx] = rpsCurrList1[ cIdx ];
520      m_bIsUsedAsLongTerm[REF_PIC_LIST_1][rIdx] = ( cIdx >= NumPicStCurr0 + NumPicStCurr1 );
521    }
522  }
523}
524#else
525Void TComSlice::getTempRefPicLists( TComList<TComPic*>& rcListPic, std::vector<TComPic*>& refPicSetInterLayer0, std::vector<TComPic*>& refPicSetInterLayer1,                                     
526                                   std::vector<TComPic*> rpsCurrList[2], std::vector<Bool> usedAsLongTerm[2], Int& numPocTotalCurr, Bool checkNumPocTotalCurr )
527{
528  if (!checkNumPocTotalCurr)
529  {
530    if (m_eSliceType == I_SLICE)
531    {     
532      return;
533    }   
534  }
535
536  TComPic*  pcRefPic= NULL;
537  TComPic*  RefPicSetStCurr0[16];
538  TComPic*  RefPicSetStCurr1[16];
539  TComPic*  RefPicSetLtCurr[16];
540  UInt NumPocStCurr0 = 0;
541  UInt NumPocStCurr1 = 0;
542  UInt NumPocLtCurr = 0;
543  Int i;
544  for(i=0; i < m_pRPS->getNumberOfNegativePictures(); i++)
545  {
546    if(m_pRPS->getUsed(i))
547    {
548      pcRefPic = xGetRefPic(rcListPic, getPOC()+m_pRPS->getDeltaPOC(i));
549      pcRefPic->setIsLongTerm(0);
550      pcRefPic->getPicYuvRec()->extendPicBorder();
551      RefPicSetStCurr0[NumPocStCurr0] = pcRefPic;
552      NumPocStCurr0++;
553      pcRefPic->setCheckLTMSBPresent(false); 
554    }
555  }
556 
557  for(; i < m_pRPS->getNumberOfNegativePictures()+m_pRPS->getNumberOfPositivePictures(); i++)
558  {
559    if(m_pRPS->getUsed(i))
560    {
561      pcRefPic = xGetRefPic(rcListPic, getPOC()+m_pRPS->getDeltaPOC(i));
562      pcRefPic->setIsLongTerm(0);
563      pcRefPic->getPicYuvRec()->extendPicBorder();
564      RefPicSetStCurr1[NumPocStCurr1] = pcRefPic;
565      NumPocStCurr1++;
566      pcRefPic->setCheckLTMSBPresent(false); 
567    }
568  }
569 
570  for(i = m_pRPS->getNumberOfNegativePictures()+m_pRPS->getNumberOfPositivePictures()+m_pRPS->getNumberOfLongtermPictures()-1; i > m_pRPS->getNumberOfNegativePictures()+m_pRPS->getNumberOfPositivePictures()-1 ; i--)
571  {
572    if(m_pRPS->getUsed(i))
573    {
574      pcRefPic = xGetLongTermRefPic(rcListPic, m_pRPS->getPOC(i), m_pRPS->getCheckLTMSBPresent(i));
575      pcRefPic->setIsLongTerm(1);
576      pcRefPic->getPicYuvRec()->extendPicBorder();
577      RefPicSetLtCurr[NumPocLtCurr] = pcRefPic;
578      NumPocLtCurr++;
579    }
580    if(pcRefPic==NULL) 
581    {
582      pcRefPic = xGetLongTermRefPic(rcListPic, m_pRPS->getPOC(i), m_pRPS->getCheckLTMSBPresent(i));
583    }
584    pcRefPic->setCheckLTMSBPresent(m_pRPS->getCheckLTMSBPresent(i)); 
585  }
586
587  Int numPocInterCurr = NumPocStCurr0 + NumPocStCurr1 + NumPocLtCurr; 
588  numPocTotalCurr = numPocInterCurr + getNumActiveRefLayerPics( );
589  assert( numPocTotalCurr == getNumRpsCurrTempList() );
590
591  if (checkNumPocTotalCurr)
592  {
593    // 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:
594    // - 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.
595    // - Otherwise, when the current picture contains a P or B slice, the value of NumPocTotalCurr shall not be equal to 0.
596    if ( getRapPicFlag() && m_layerId == 0 )
597    {
598      assert(numPocTotalCurr == 0);
599    }
600
601    if (m_eSliceType == I_SLICE)
602    {
603      ::memset( m_apcRefPicList, 0, sizeof (m_apcRefPicList));
604      ::memset( m_aiNumRefIdx,   0, sizeof ( m_aiNumRefIdx ));
605     
606      return;
607    }
608   
609    assert(numPocTotalCurr > 0);
610   
611    m_aiNumRefIdx[0] = getNumRefIdx(REF_PIC_LIST_0);
612    m_aiNumRefIdx[1] = getNumRefIdx(REF_PIC_LIST_1);
613  }
614
615  std::vector<TComPic*>* refPicSetInterLayer[2] = { &refPicSetInterLayer0, &refPicSetInterLayer1}; 
616  Int numPocInterLayer[2] = { getNumActiveRefLayerPics0( ), getNumActiveRefLayerPics1( ) }; 
617 
618  TComPic**             refPicSetStCurr    [2] = { RefPicSetStCurr0, RefPicSetStCurr1 };
619  Int numPocStCurr[2] = { (Int)NumPocStCurr0, (Int)NumPocStCurr1 }; 
620
621  for (Int li = 0; li < ((m_eSliceType==B_SLICE) ? 2 : 1); li++)
622  { 
623    rpsCurrList   [li].resize(MAX_NUM_REF+1,NULL ); 
624    usedAsLongTerm[li].resize(MAX_NUM_REF+1,false); 
625
626    Int cIdx = 0;
627    for ( i=0; i < numPocStCurr[li]; i++, cIdx++)
628    {
629      rpsCurrList[li][cIdx] = refPicSetStCurr[li][i];
630      usedAsLongTerm [li][cIdx] = false; 
631    }
632
633    for ( i=0; i < numPocInterLayer[li];  i++, cIdx++)
634    {   
635      rpsCurrList[li][cIdx] = (*refPicSetInterLayer[li])[i];
636      usedAsLongTerm [li][cIdx] = true; 
637    }
638
639    for ( i=0; i < numPocStCurr[1-li]; i++, cIdx++)
640    {
641      rpsCurrList[li][cIdx] = refPicSetStCurr[1-li][i];
642      usedAsLongTerm [li][cIdx] = false; 
643    }
644
645    for ( i=0; i<NumPocLtCurr;  i++, cIdx++)
646    {
647      rpsCurrList[li][cIdx] = RefPicSetLtCurr[i];
648      usedAsLongTerm [li][cIdx] = true; 
649    }
650
651    for ( i=0; i < numPocInterLayer[1-li];  i++, cIdx++)
652    {   
653      assert( cIdx < MAX_NUM_REF );   
654      rpsCurrList[li][cIdx] = (*refPicSetInterLayer[1-li])[i];
655      usedAsLongTerm [li][cIdx] = true; 
656    }
657
658    assert(cIdx == numPocTotalCurr);
659  }
660}
661
662Void TComSlice::setRefPicList( std::vector<TComPic*> rpsCurrList[2], std::vector<Bool> usedAsLongTerm[2], Int numPocTotalCurr, Bool checkNumPocTotalCurr )
663
664{
665  if (!checkNumPocTotalCurr)
666  {
667    if (m_eSliceType == I_SLICE)
668    {
669      ::memset( m_apcRefPicList, 0, sizeof (m_apcRefPicList));
670      ::memset( m_aiNumRefIdx,   0, sizeof ( m_aiNumRefIdx ));
671
672      return;
673    }   
674  }
675
676  ::memset(m_bIsUsedAsLongTerm, 0, sizeof(m_bIsUsedAsLongTerm));
677
678  for (Int li = 0; li < 2; li++)
679  {
680    if ( m_eSliceType == P_SLICE && li == 1 )
681    {
682      m_aiNumRefIdx[1] = 0;
683      ::memset( m_apcRefPicList[1], 0, sizeof(m_apcRefPicList[1]));
684    } 
685    else
686    {
687      for (Int rIdx = 0; rIdx <= (m_aiNumRefIdx[ li ] - 1 ); rIdx ++)
688      { 
689        Bool listModified             =                m_RefPicListModification.getRefPicListModificationFlagL( li ); 
690        Int orgIdx                    = listModified ? m_RefPicListModification.getRefPicSetIdxL(li, rIdx) : (rIdx % numPocTotalCurr); 
691
692        assert( rpsCurrList[li][ orgIdx ] != NULL ); 
693        assert( rpsCurrList[li][ orgIdx ]->getSlice(0)->getDiscardableFlag() == 0 );    // Inter-layer RPS shall not contain picture with discardable_flag = 1.
694        m_apcRefPicList    [li][rIdx] = rpsCurrList    [li][ orgIdx ];
695        m_bIsUsedAsLongTerm[li][rIdx] = usedAsLongTerm [li][ orgIdx ] ; 
696      }
697    }
698  }
699}
700#endif
701
702
703Int TComSlice::getNumRpsCurrTempList() const
704{
705  Int numRpsCurrTempList = 0;
706
707  if (m_eSliceType == I_SLICE)
708  {
709    return 0;
710  }
711  for(UInt i=0; i < m_pRPS->getNumberOfNegativePictures()+ m_pRPS->getNumberOfPositivePictures() + m_pRPS->getNumberOfLongtermPictures(); i++)
712  {
713    if(m_pRPS->getUsed(i))
714    {
715      numRpsCurrTempList++;
716    }
717  }
718#if NH_MV
719    numRpsCurrTempList = numRpsCurrTempList + getNumActiveRefLayerPics();
720#endif
721  return numRpsCurrTempList;
722}
723
724Void TComSlice::initEqualRef()
725{
726  for (Int iDir = 0; iDir < NUM_REF_PIC_LIST_01; iDir++)
727  {
728    for (Int iRefIdx1 = 0; iRefIdx1 < MAX_NUM_REF; iRefIdx1++)
729    {
730      for (Int iRefIdx2 = iRefIdx1; iRefIdx2 < MAX_NUM_REF; iRefIdx2++)
731      {
732        m_abEqualRef[iDir][iRefIdx1][iRefIdx2] = m_abEqualRef[iDir][iRefIdx2][iRefIdx1] = (iRefIdx1 == iRefIdx2? true : false);
733      }
734    }
735  }
736}
737
738
739Void TComSlice::checkColRefIdx(UInt curSliceIdx, TComPic* pic)
740{
741  Int i;
742  TComSlice* curSlice = pic->getSlice(curSliceIdx);
743  Int currColRefPOC =  curSlice->getRefPOC( RefPicList(1 - curSlice->getColFromL0Flag()), curSlice->getColRefIdx());
744  TComSlice* preSlice;
745  Int preColRefPOC;
746  for(i=curSliceIdx-1; i>=0; i--)
747  {
748    preSlice = pic->getSlice(i);
749    if(preSlice->getSliceType() != I_SLICE)
750    {
751      preColRefPOC  = preSlice->getRefPOC( RefPicList(1 - preSlice->getColFromL0Flag()), preSlice->getColRefIdx());
752      if(currColRefPOC != preColRefPOC)
753      {
754        printf("Collocated_ref_idx shall always be the same for all slices of a coded picture!\n");
755        exit(EXIT_FAILURE);
756      }
757      else
758      {
759        break;
760      }
761    }
762  }
763}
764
765Void TComSlice::checkCRA(const TComReferencePictureSet *pReferencePictureSet, Int& pocCRA, NalUnitType& associatedIRAPType, TComList<TComPic *>& rcListPic)
766{
767  for(Int i = 0; i < pReferencePictureSet->getNumberOfNegativePictures()+pReferencePictureSet->getNumberOfPositivePictures(); i++)
768  {
769    if(pocCRA < MAX_UINT && getPOC() > pocCRA)
770    {
771      assert(getPOC()+pReferencePictureSet->getDeltaPOC(i) >= pocCRA);
772    }
773  }
774  for(Int i = pReferencePictureSet->getNumberOfNegativePictures()+pReferencePictureSet->getNumberOfPositivePictures(); i < pReferencePictureSet->getNumberOfPictures(); i++)
775  {
776    if(pocCRA < MAX_UINT && getPOC() > pocCRA)
777    {
778      if (!pReferencePictureSet->getCheckLTMSBPresent(i))
779      {
780        assert(xGetLongTermRefPic(rcListPic, pReferencePictureSet->getPOC(i), false)->getPOC() >= pocCRA);
781      }
782      else
783      {
784        assert(pReferencePictureSet->getPOC(i) >= pocCRA);
785      }
786    }
787  }
788  if ( getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_W_RADL || getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_N_LP ) // IDR picture found
789  {
790    pocCRA = getPOC();
791    associatedIRAPType = getNalUnitType();
792  }
793  else if ( getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA ) // CRA picture found
794  {
795    pocCRA = getPOC();
796    associatedIRAPType = getNalUnitType();
797  }
798  else if ( getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP
799         || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL
800         || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP ) // BLA picture found
801  {
802    pocCRA = getPOC();
803    associatedIRAPType = getNalUnitType();
804  }
805}
806
807/** Function for marking the reference pictures when an IDR/CRA/CRANT/BLA/BLANT is encountered.
808 * \param pocCRA POC of the CRA/CRANT/BLA/BLANT picture
809 * \param bRefreshPending flag indicating if a deferred decoding refresh is pending
810 * \param rcListPic reference to the reference picture list
811 * This function marks the reference pictures as "unused for reference" in the following conditions.
812 * If the nal_unit_type is IDR/BLA/BLANT, all pictures in the reference picture list
813 * are marked as "unused for reference"
814 *    If the nal_unit_type is BLA/BLANT, set the pocCRA to the temporal reference of the current picture.
815 * Otherwise
816 *    If the bRefreshPending flag is true (a deferred decoding refresh is pending) and the current
817 *    temporal reference is greater than the temporal reference of the latest CRA/CRANT/BLA/BLANT picture (pocCRA),
818 *    mark all reference pictures except the latest CRA/CRANT/BLA/BLANT picture as "unused for reference" and set
819 *    the bRefreshPending flag to false.
820 *    If the nal_unit_type is CRA/CRANT, set the bRefreshPending flag to true and pocCRA to the temporal
821 *    reference of the current picture.
822 * Note that the current picture is already placed in the reference list and its marking is not changed.
823 * If the current picture has a nal_ref_idc that is not 0, it will remain marked as "used for reference".
824 */
825Void TComSlice::decodingRefreshMarking(Int& pocCRA, Bool& bRefreshPending, TComList<TComPic*>& rcListPic, const bool bEfficientFieldIRAPEnabled)
826{
827  TComPic* rpcPic;
828  Int      pocCurr = getPOC();
829
830  if ( getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP
831    || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL
832    || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP
833    || getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_W_RADL
834    || getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_N_LP )  // IDR or BLA picture
835  {
836    // mark all pictures as not used for reference
837    TComList<TComPic*>::iterator        iterPic       = rcListPic.begin();
838    while (iterPic != rcListPic.end())
839    {
840      rpcPic = *(iterPic);
841      rpcPic->setCurrSliceIdx(0);
842      if (rpcPic->getPOC() != pocCurr)
843      {
844        rpcPic->getSlice(0)->setReferenced(false);
845      }
846      iterPic++;
847    }
848    if ( getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP
849      || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL
850      || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP )
851    {
852      pocCRA = pocCurr;
853    }
854    if (bEfficientFieldIRAPEnabled)
855    {
856    bRefreshPending = true;
857    }
858  }
859  else // CRA or No DR
860  {
861    if(bEfficientFieldIRAPEnabled && (getAssociatedIRAPType() == NAL_UNIT_CODED_SLICE_IDR_N_LP || getAssociatedIRAPType() == NAL_UNIT_CODED_SLICE_IDR_W_RADL))
862    {
863      if (bRefreshPending==true && pocCurr > m_iLastIDR) // IDR reference marking pending
864      {
865        TComList<TComPic*>::iterator        iterPic       = rcListPic.begin();
866        while (iterPic != rcListPic.end())
867        {
868          rpcPic = *(iterPic);
869          if (rpcPic->getPOC() != pocCurr && rpcPic->getPOC() != m_iLastIDR)
870          {
871            rpcPic->getSlice(0)->setReferenced(false);
872          }
873          iterPic++;
874        }
875        bRefreshPending = false; 
876      }
877    }
878    else
879    {
880      if (bRefreshPending==true && pocCurr > pocCRA) // CRA reference marking pending
881      {
882        TComList<TComPic*>::iterator iterPic = rcListPic.begin();
883        while (iterPic != rcListPic.end())
884        {
885          rpcPic = *(iterPic);
886          if (rpcPic->getPOC() != pocCurr && rpcPic->getPOC() != pocCRA)
887          {
888            rpcPic->getSlice(0)->setReferenced(false);
889          }
890          iterPic++;
891        }
892        bRefreshPending = false;
893      }
894    }
895    if ( getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA ) // CRA picture found
896    {
897      bRefreshPending = true;
898      pocCRA = pocCurr;
899    }
900  }
901}
902
903Void TComSlice::copySliceInfo(TComSlice *pSrc)
904{
905  assert( pSrc != NULL );
906
907  Int i, j, k;
908
909  m_iPOC                 = pSrc->m_iPOC;
910  m_eNalUnitType         = pSrc->m_eNalUnitType;
911#if NH_MV
912  m_layerId              = pSrc->m_layerId;
913  // GT: Copying of several other values might be be missing here, or is above not necessary?
914#endif
915  m_eSliceType           = pSrc->m_eSliceType;
916  m_iSliceQp             = pSrc->m_iSliceQp;
917#if ADAPTIVE_QP_SELECTION
918  m_iSliceQpBase         = pSrc->m_iSliceQpBase;
919#endif
920  m_ChromaQpAdjEnabled = pSrc->m_ChromaQpAdjEnabled;
921  m_deblockingFilterDisable   = pSrc->m_deblockingFilterDisable;
922  m_deblockingFilterOverrideFlag = pSrc->m_deblockingFilterOverrideFlag;
923  m_deblockingFilterBetaOffsetDiv2 = pSrc->m_deblockingFilterBetaOffsetDiv2;
924  m_deblockingFilterTcOffsetDiv2 = pSrc->m_deblockingFilterTcOffsetDiv2;
925
926  for (i = 0; i < NUM_REF_PIC_LIST_01; i++)
927  {
928    m_aiNumRefIdx[i]     = pSrc->m_aiNumRefIdx[i];
929  }
930
931  for (i = 0; i < MAX_NUM_REF; i++)
932  {
933    m_list1IdxToList0Idx[i] = pSrc->m_list1IdxToList0Idx[i];
934  }
935
936  m_bCheckLDC             = pSrc->m_bCheckLDC;
937  m_iSliceQpDelta        = pSrc->m_iSliceQpDelta;
938  for (UInt component = 0; component < MAX_NUM_COMPONENT; component++)
939  {
940    m_iSliceChromaQpDelta[component] = pSrc->m_iSliceChromaQpDelta[component];
941  }
942  for (i = 0; i < NUM_REF_PIC_LIST_01; i++)
943  {
944    for (j = 0; j < MAX_NUM_REF; j++)
945    {
946      m_apcRefPicList[i][j]  = pSrc->m_apcRefPicList[i][j];
947      m_aiRefPOCList[i][j]   = pSrc->m_aiRefPOCList[i][j];
948      m_bIsUsedAsLongTerm[i][j] = pSrc->m_bIsUsedAsLongTerm[i][j];
949#if NH_MV
950      m_aiRefLayerIdList[i][j] = pSrc->m_aiRefLayerIdList[i][j];
951#endif
952    }
953    m_bIsUsedAsLongTerm[i][MAX_NUM_REF] = pSrc->m_bIsUsedAsLongTerm[i][MAX_NUM_REF];
954  }
955  m_iDepth               = pSrc->m_iDepth;
956
957  // referenced slice
958  m_bRefenced            = pSrc->m_bRefenced;
959
960  // access channel
961#if NH_MV
962  m_pcVPS                = pSrc->m_pcVPS;
963#endif
964  m_pRPS                = pSrc->m_pRPS;
965  m_iLastIDR             = pSrc->m_iLastIDR;
966
967  m_pcPic                = pSrc->m_pcPic;
968
969  m_colFromL0Flag        = pSrc->m_colFromL0Flag;
970  m_colRefIdx            = pSrc->m_colRefIdx;
971
972  setLambdas(pSrc->getLambdas());
973
974  for (i = 0; i < NUM_REF_PIC_LIST_01; i++)
975  {
976    for (j = 0; j < MAX_NUM_REF; j++)
977    {
978      for (k =0; k < MAX_NUM_REF; k++)
979      {
980        m_abEqualRef[i][j][k] = pSrc->m_abEqualRef[i][j][k];
981      }
982    }
983  }
984
985  m_uiTLayer                      = pSrc->m_uiTLayer;
986  m_bTLayerSwitchingFlag          = pSrc->m_bTLayerSwitchingFlag;
987
988  m_sliceMode                     = pSrc->m_sliceMode;
989  m_sliceArgument                 = pSrc->m_sliceArgument;
990  m_sliceCurStartCtuTsAddr        = pSrc->m_sliceCurStartCtuTsAddr;
991  m_sliceCurEndCtuTsAddr          = pSrc->m_sliceCurEndCtuTsAddr;
992  m_sliceIdx                      = pSrc->m_sliceIdx;
993  m_sliceSegmentMode              = pSrc->m_sliceSegmentMode;
994  m_sliceSegmentArgument          = pSrc->m_sliceSegmentArgument;
995  m_sliceSegmentCurStartCtuTsAddr = pSrc->m_sliceSegmentCurStartCtuTsAddr;
996  m_sliceSegmentCurEndCtuTsAddr   = pSrc->m_sliceSegmentCurEndCtuTsAddr;
997  m_nextSlice                     = pSrc->m_nextSlice;
998  m_nextSliceSegment              = pSrc->m_nextSliceSegment;
999
1000  for ( UInt e=0 ; e<NUM_REF_PIC_LIST_01 ; e++ )
1001  {
1002    for ( UInt n=0 ; n<MAX_NUM_REF ; n++ )
1003    {
1004      memcpy(m_weightPredTable[e][n], pSrc->m_weightPredTable[e][n], sizeof(WPScalingParam)*MAX_NUM_COMPONENT );
1005    }
1006  }
1007
1008  for( UInt ch = 0 ; ch < MAX_NUM_CHANNEL_TYPE; ch++)
1009  {
1010    m_saoEnabledFlag[ch] = pSrc->m_saoEnabledFlag[ch];
1011  }
1012
1013  m_cabacInitFlag                 = pSrc->m_cabacInitFlag;
1014
1015  m_bLMvdL1Zero                   = pSrc->m_bLMvdL1Zero;
1016  m_LFCrossSliceBoundaryFlag      = pSrc->m_LFCrossSliceBoundaryFlag;
1017  m_enableTMVPFlag                = pSrc->m_enableTMVPFlag;
1018  m_maxNumMergeCand               = pSrc->m_maxNumMergeCand;
1019  m_encCABACTableIdx              = pSrc->m_encCABACTableIdx;
1020
1021#if NH_MV
1022  // Additional slice header syntax elements
1023  m_pocResetFlag               = pSrc->m_pocResetFlag; 
1024  m_discardableFlag            = pSrc->m_discardableFlag; 
1025  m_interLayerPredEnabledFlag  = pSrc->m_interLayerPredEnabledFlag; 
1026  m_numInterLayerRefPicsMinus1 = pSrc->m_numInterLayerRefPicsMinus1;
1027
1028  for (Int layer = 0; layer < MAX_NUM_LAYERS; layer++ )
1029  {
1030    m_interLayerPredLayerIdc[ layer ] = pSrc->m_interLayerPredLayerIdc[ layer ]; 
1031  }
1032#endif
1033
1034}
1035
1036
1037/** Function for setting the slice's temporal layer ID and corresponding temporal_layer_switching_point_flag.
1038 * \param uiTLayer Temporal layer ID of the current slice
1039 * The decoder calls this function to set temporal_layer_switching_point_flag for each temporal layer based on
1040 * the SPS's temporal_id_nesting_flag and the parsed PPS.  Then, current slice's temporal layer ID and
1041 * temporal_layer_switching_point_flag is set accordingly.
1042 */
1043Void TComSlice::setTLayerInfo( UInt uiTLayer )
1044{
1045  m_uiTLayer = uiTLayer;
1046}
1047
1048/** Function for checking if this is a switching-point
1049*/
1050Bool TComSlice::isTemporalLayerSwitchingPoint(TComList<TComPic*>& rcListPic)
1051{
1052  TComPic* rpcPic;
1053  // loop through all pictures in the reference picture buffer
1054  TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1055  while ( iterPic != rcListPic.end())
1056  {
1057    rpcPic = *(iterPic++);
1058    if(rpcPic->getSlice(0)->isReferenced() && rpcPic->getPOC() != getPOC())
1059    {
1060      if(rpcPic->getTLayer() >= getTLayer())
1061      {
1062        return false;
1063      }
1064    }
1065  }
1066  return true;
1067}
1068
1069/** Function for checking if this is a STSA candidate
1070 */
1071Bool TComSlice::isStepwiseTemporalLayerSwitchingPointCandidate(TComList<TComPic*>& rcListPic)
1072{
1073  TComPic* rpcPic;
1074
1075  TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1076  while ( iterPic != rcListPic.end())
1077  {
1078    rpcPic = *(iterPic++);
1079    if(rpcPic->getSlice(0)->isReferenced() &&  (rpcPic->getUsedByCurr()==true) && rpcPic->getPOC() != getPOC())
1080    {
1081      if(rpcPic->getTLayer() >= getTLayer())
1082      {
1083        return false;
1084      }
1085    }
1086  }
1087  return true;
1088}
1089
1090
1091Void TComSlice::checkLeadingPictureRestrictions(TComList<TComPic*>& rcListPic)
1092{
1093  TComPic* rpcPic;
1094
1095  Int nalUnitType = this->getNalUnitType();
1096
1097  // When a picture is a leading picture, it shall be a RADL or RASL picture.
1098  if(this->getAssociatedIRAPPOC() > this->getPOC())
1099  {
1100    // Do not check IRAP pictures since they may get a POC lower than their associated IRAP
1101    if(nalUnitType < NAL_UNIT_CODED_SLICE_BLA_W_LP ||
1102       nalUnitType > NAL_UNIT_RESERVED_IRAP_VCL23)
1103    {
1104      assert(nalUnitType == NAL_UNIT_CODED_SLICE_RASL_N ||
1105             nalUnitType == NAL_UNIT_CODED_SLICE_RASL_R ||
1106             nalUnitType == NAL_UNIT_CODED_SLICE_RADL_N ||
1107             nalUnitType == NAL_UNIT_CODED_SLICE_RADL_R);
1108    }
1109  }
1110
1111  // When a picture is a trailing picture, it shall not be a RADL or RASL picture.
1112  if(this->getAssociatedIRAPPOC() < this->getPOC())
1113  {
1114    assert(nalUnitType != NAL_UNIT_CODED_SLICE_RASL_N &&
1115           nalUnitType != NAL_UNIT_CODED_SLICE_RASL_R &&
1116           nalUnitType != NAL_UNIT_CODED_SLICE_RADL_N &&
1117           nalUnitType != NAL_UNIT_CODED_SLICE_RADL_R);
1118  }
1119
1120  // No RASL pictures shall be present in the bitstream that are associated
1121  // with a BLA picture having nal_unit_type equal to BLA_W_RADL or BLA_N_LP.
1122  if(nalUnitType == NAL_UNIT_CODED_SLICE_RASL_N ||
1123     nalUnitType == NAL_UNIT_CODED_SLICE_RASL_R)
1124  {
1125    assert(this->getAssociatedIRAPType() != NAL_UNIT_CODED_SLICE_BLA_W_RADL &&
1126           this->getAssociatedIRAPType() != NAL_UNIT_CODED_SLICE_BLA_N_LP);
1127  }
1128
1129  // No RASL pictures shall be present in the bitstream that are associated with
1130  // an IDR picture.
1131  if(nalUnitType == NAL_UNIT_CODED_SLICE_RASL_N ||
1132     nalUnitType == NAL_UNIT_CODED_SLICE_RASL_R)
1133  {
1134    assert(this->getAssociatedIRAPType() != NAL_UNIT_CODED_SLICE_IDR_N_LP   &&
1135           this->getAssociatedIRAPType() != NAL_UNIT_CODED_SLICE_IDR_W_RADL);
1136  }
1137
1138  // No RADL pictures shall be present in the bitstream that are associated with
1139  // a BLA picture having nal_unit_type equal to BLA_N_LP or that are associated
1140  // with an IDR picture having nal_unit_type equal to IDR_N_LP.
1141  if(nalUnitType == NAL_UNIT_CODED_SLICE_RADL_N ||
1142     nalUnitType == NAL_UNIT_CODED_SLICE_RADL_R)
1143  {
1144    assert(this->getAssociatedIRAPType() != NAL_UNIT_CODED_SLICE_BLA_N_LP   &&
1145           this->getAssociatedIRAPType() != NAL_UNIT_CODED_SLICE_IDR_N_LP);
1146  }
1147
1148  // loop through all pictures in the reference picture buffer
1149  TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1150  while ( iterPic != rcListPic.end())
1151  {
1152    rpcPic = *(iterPic++);
1153    if(!rpcPic->getReconMark())
1154    {
1155      continue;
1156    }
1157    if (rpcPic->getPOC() == this->getPOC())
1158    {
1159      continue;
1160    }
1161
1162    // Any picture that has PicOutputFlag equal to 1 that precedes an IRAP picture
1163    // in decoding order shall precede the IRAP picture in output order.
1164    // (Note that any picture following in output order would be present in the DPB)
1165    if(rpcPic->getSlice(0)->getPicOutputFlag() == 1 && !this->getNoOutputPriorPicsFlag())
1166    {
1167      if(nalUnitType == NAL_UNIT_CODED_SLICE_BLA_N_LP    ||
1168         nalUnitType == NAL_UNIT_CODED_SLICE_BLA_W_LP    ||
1169         nalUnitType == NAL_UNIT_CODED_SLICE_BLA_W_RADL  ||
1170         nalUnitType == NAL_UNIT_CODED_SLICE_CRA         ||
1171         nalUnitType == NAL_UNIT_CODED_SLICE_IDR_N_LP    ||
1172         nalUnitType == NAL_UNIT_CODED_SLICE_IDR_W_RADL)
1173      {
1174        assert(rpcPic->getPOC() < this->getPOC());
1175      }
1176    }
1177
1178    // Any picture that has PicOutputFlag equal to 1 that precedes an IRAP picture
1179    // in decoding order shall precede any RADL picture associated with the IRAP
1180    // picture in output order.
1181    if(rpcPic->getSlice(0)->getPicOutputFlag() == 1)
1182    {
1183      if((nalUnitType == NAL_UNIT_CODED_SLICE_RADL_N ||
1184          nalUnitType == NAL_UNIT_CODED_SLICE_RADL_R))
1185      {
1186        // rpcPic precedes the IRAP in decoding order
1187        if(this->getAssociatedIRAPPOC() > rpcPic->getSlice(0)->getAssociatedIRAPPOC())
1188        {
1189          // rpcPic must not be the IRAP picture
1190          if(this->getAssociatedIRAPPOC() != rpcPic->getPOC())
1191          {
1192            assert(rpcPic->getPOC() < this->getPOC());
1193          }
1194        }
1195      }
1196    }
1197
1198    // When a picture is a leading picture, it shall precede, in decoding order,
1199    // all trailing pictures that are associated with the same IRAP picture.
1200      if(nalUnitType == NAL_UNIT_CODED_SLICE_RASL_N ||
1201         nalUnitType == NAL_UNIT_CODED_SLICE_RASL_R ||
1202         nalUnitType == NAL_UNIT_CODED_SLICE_RADL_N ||
1203         nalUnitType == NAL_UNIT_CODED_SLICE_RADL_R)
1204      {
1205        if(rpcPic->getSlice(0)->getAssociatedIRAPPOC() == this->getAssociatedIRAPPOC())
1206        {
1207          // rpcPic is a picture that preceded the leading in decoding order since it exist in the DPB
1208          // rpcPic would violate the constraint if it was a trailing picture
1209          assert(rpcPic->getPOC() <= this->getAssociatedIRAPPOC());
1210        }
1211      }
1212
1213    // Any RASL picture associated with a CRA or BLA picture shall precede any
1214    // RADL picture associated with the CRA or BLA picture in output order
1215    if(nalUnitType == NAL_UNIT_CODED_SLICE_RASL_N ||
1216       nalUnitType == NAL_UNIT_CODED_SLICE_RASL_R)
1217    {
1218      if((this->getAssociatedIRAPType() == NAL_UNIT_CODED_SLICE_BLA_N_LP   ||
1219          this->getAssociatedIRAPType() == NAL_UNIT_CODED_SLICE_BLA_W_LP   ||
1220          this->getAssociatedIRAPType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL ||
1221          this->getAssociatedIRAPType() == NAL_UNIT_CODED_SLICE_CRA)       &&
1222          this->getAssociatedIRAPPOC() == rpcPic->getSlice(0)->getAssociatedIRAPPOC())
1223      {
1224        if(rpcPic->getSlice(0)->getNalUnitType() == NAL_UNIT_CODED_SLICE_RADL_N ||
1225           rpcPic->getSlice(0)->getNalUnitType() == NAL_UNIT_CODED_SLICE_RADL_R)
1226        {
1227          assert(rpcPic->getPOC() > this->getPOC());
1228        }
1229      }
1230    }
1231
1232    // Any RASL picture associated with a CRA picture shall follow, in output
1233    // order, any IRAP picture that precedes the CRA picture in decoding order.
1234    if(nalUnitType == NAL_UNIT_CODED_SLICE_RASL_N ||
1235       nalUnitType == NAL_UNIT_CODED_SLICE_RASL_R)
1236    {
1237      if(this->getAssociatedIRAPType() == NAL_UNIT_CODED_SLICE_CRA)
1238      {
1239        if(rpcPic->getSlice(0)->getPOC() < this->getAssociatedIRAPPOC() &&
1240           (rpcPic->getSlice(0)->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP   ||
1241            rpcPic->getSlice(0)->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP   ||
1242            rpcPic->getSlice(0)->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL ||
1243            rpcPic->getSlice(0)->getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_N_LP   ||
1244            rpcPic->getSlice(0)->getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_W_RADL ||
1245            rpcPic->getSlice(0)->getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA))
1246        {
1247          assert(this->getPOC() > rpcPic->getSlice(0)->getPOC());
1248        }
1249      }
1250    }
1251  }
1252}
1253
1254
1255
1256/** Function for applying picture marking based on the Reference Picture Set in pReferencePictureSet.
1257*/
1258Void TComSlice::applyReferencePictureSet( TComList<TComPic*>& rcListPic, const TComReferencePictureSet *pReferencePictureSet)
1259{
1260  TComPic* rpcPic;
1261  Int i, isReference;
1262
1263  checkLeadingPictureRestrictions(rcListPic);
1264
1265  // loop through all pictures in the reference picture buffer
1266  TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1267  while ( iterPic != rcListPic.end())
1268  {
1269    rpcPic = *(iterPic++);
1270
1271    if(!rpcPic->getSlice( 0 )->isReferenced())
1272    {
1273      continue;
1274    }
1275
1276    isReference = 0;
1277    // loop through all pictures in the Reference Picture Set
1278    // to see if the picture should be kept as reference picture
1279    for(i=0;i<pReferencePictureSet->getNumberOfPositivePictures()+pReferencePictureSet->getNumberOfNegativePictures();i++)
1280    {
1281      if(!rpcPic->getIsLongTerm() && rpcPic->getPicSym()->getSlice(0)->getPOC() == this->getPOC() + pReferencePictureSet->getDeltaPOC(i))
1282      {
1283        isReference = 1;
1284        rpcPic->setUsedByCurr(pReferencePictureSet->getUsed(i));
1285        rpcPic->setIsLongTerm(0);
1286      }
1287    }
1288    for(;i<pReferencePictureSet->getNumberOfPictures();i++)
1289    {
1290      if(pReferencePictureSet->getCheckLTMSBPresent(i)==true)
1291      {
1292        if(rpcPic->getIsLongTerm() && (rpcPic->getPicSym()->getSlice(0)->getPOC()) == pReferencePictureSet->getPOC(i))
1293        {
1294          isReference = 1;
1295          rpcPic->setUsedByCurr(pReferencePictureSet->getUsed(i));
1296        }
1297      }
1298      else
1299      {
1300        Int pocCycle = 1<<rpcPic->getPicSym()->getSlice(0)->getSPS()->getBitsForPOC();
1301        Int curPoc = rpcPic->getPicSym()->getSlice(0)->getPOC() & (pocCycle-1);
1302        Int refPoc = pReferencePictureSet->getPOC(i) & (pocCycle-1);
1303        if(rpcPic->getIsLongTerm() && curPoc == refPoc)
1304        {
1305          isReference = 1;
1306          rpcPic->setUsedByCurr(pReferencePictureSet->getUsed(i));
1307        }
1308      }
1309
1310    }
1311#if NH_MV
1312    if( isReference ) // Current picture is in the temporal RPS
1313    {
1314      assert( rpcPic->getSlice(0)->getDiscardableFlag() == 0 ); // Temporal RPS shall not contain picture with discardable_flag equal to 1
1315    }
1316#endif
1317    // mark the picture as "unused for reference" if it is not in
1318    // the Reference Picture Set
1319    if(rpcPic->getPicSym()->getSlice(0)->getPOC() != this->getPOC() && isReference == 0)
1320    {
1321      rpcPic->getSlice( 0 )->setReferenced( false );
1322      rpcPic->setUsedByCurr(0);
1323      rpcPic->setIsLongTerm(0);
1324    }
1325    //check that pictures of higher temporal layers are not used
1326    assert(rpcPic->getSlice( 0 )->isReferenced()==0||rpcPic->getUsedByCurr()==0||rpcPic->getTLayer()<=this->getTLayer());
1327    //check that pictures of higher or equal temporal layer are not in the RPS if the current picture is a TSA picture
1328    if(this->getNalUnitType() == NAL_UNIT_CODED_SLICE_TSA_R || this->getNalUnitType() == NAL_UNIT_CODED_SLICE_TSA_N)
1329    {
1330      assert(rpcPic->getSlice( 0 )->isReferenced()==0||rpcPic->getTLayer()<this->getTLayer());
1331    }
1332    //check that pictures marked as temporal layer non-reference pictures are not used for reference
1333    if(rpcPic->getPicSym()->getSlice(0)->getPOC() != this->getPOC() && rpcPic->getTLayer()==this->getTLayer())
1334    {
1335      assert(rpcPic->getSlice( 0 )->isReferenced()==0||rpcPic->getUsedByCurr()==0||rpcPic->getSlice( 0 )->getTemporalLayerNonReferenceFlag()==false);
1336    }
1337  }
1338}
1339
1340/** Function for applying picture marking based on the Reference Picture Set in pReferencePictureSet.
1341*/
1342Int TComSlice::checkThatAllRefPicsAreAvailable( TComList<TComPic*>& rcListPic, const TComReferencePictureSet *pReferencePictureSet, Bool printErrors, Int pocRandomAccess, Bool bUseRecoveryPoint)
1343{
1344  Int atLeastOneUnabledByRecoveryPoint = 0;
1345  Int atLeastOneFlushedByPreviousIDR = 0;
1346  TComPic* rpcPic;
1347  Int i, isAvailable;
1348  Int atLeastOneLost = 0;
1349  Int atLeastOneRemoved = 0;
1350  Int iPocLost = 0;
1351
1352  // loop through all long-term pictures in the Reference Picture Set
1353  // to see if the picture should be kept as reference picture
1354  for(i=pReferencePictureSet->getNumberOfNegativePictures()+pReferencePictureSet->getNumberOfPositivePictures();i<pReferencePictureSet->getNumberOfPictures();i++)
1355  {
1356    isAvailable = 0;
1357    // loop through all pictures in the reference picture buffer
1358    TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1359    while ( iterPic != rcListPic.end())
1360    {
1361      rpcPic = *(iterPic++);
1362      if(pReferencePictureSet->getCheckLTMSBPresent(i)==true)
1363      {
1364        if(rpcPic->getIsLongTerm() && (rpcPic->getPicSym()->getSlice(0)->getPOC()) == pReferencePictureSet->getPOC(i) && rpcPic->getSlice(0)->isReferenced())
1365        {
1366          if(bUseRecoveryPoint && this->getPOC() > pocRandomAccess && this->getPOC() + pReferencePictureSet->getDeltaPOC(i) < pocRandomAccess)
1367          {
1368            isAvailable = 0;
1369          }
1370          else
1371          {
1372            isAvailable = 1;
1373          }
1374        }
1375      }
1376      else
1377      {
1378        Int pocCycle = 1<<rpcPic->getPicSym()->getSlice(0)->getSPS()->getBitsForPOC();
1379        Int curPoc = rpcPic->getPicSym()->getSlice(0)->getPOC() & (pocCycle-1);
1380        Int refPoc = pReferencePictureSet->getPOC(i) & (pocCycle-1);
1381        if(rpcPic->getIsLongTerm() && curPoc == refPoc && rpcPic->getSlice(0)->isReferenced())
1382        {
1383          if(bUseRecoveryPoint && this->getPOC() > pocRandomAccess && this->getPOC() + pReferencePictureSet->getDeltaPOC(i) < pocRandomAccess)
1384          {
1385            isAvailable = 0;
1386          }
1387          else
1388          {
1389            isAvailable = 1;
1390          }
1391        }
1392      }
1393    }
1394    // if there was no such long-term check the short terms
1395    if(!isAvailable)
1396    {
1397      iterPic = rcListPic.begin();
1398      while ( iterPic != rcListPic.end())
1399      {
1400        rpcPic = *(iterPic++);
1401
1402        Int pocCycle = 1 << rpcPic->getPicSym()->getSlice(0)->getSPS()->getBitsForPOC();
1403        Int curPoc = rpcPic->getPicSym()->getSlice(0)->getPOC();
1404        Int refPoc = pReferencePictureSet->getPOC(i);
1405        if (!pReferencePictureSet->getCheckLTMSBPresent(i))
1406        {
1407          curPoc = curPoc & (pocCycle - 1);
1408          refPoc = refPoc & (pocCycle - 1);
1409        }
1410
1411        if (rpcPic->getSlice(0)->isReferenced() && curPoc == refPoc)
1412        {
1413          if(bUseRecoveryPoint && this->getPOC() > pocRandomAccess && this->getPOC() + pReferencePictureSet->getDeltaPOC(i) < pocRandomAccess)
1414          {
1415            isAvailable = 0;
1416          }
1417          else
1418          {
1419            isAvailable = 1;
1420            rpcPic->setIsLongTerm(1);
1421            break;
1422          }
1423        }
1424      }
1425    }
1426    // report that a picture is lost if it is in the Reference Picture Set
1427    // but not available as reference picture
1428    if(isAvailable == 0)
1429    {
1430      if (this->getPOC() + pReferencePictureSet->getDeltaPOC(i) >= pocRandomAccess)
1431      {
1432        if(!pReferencePictureSet->getUsed(i) )
1433        {
1434          if(printErrors)
1435          {
1436            printf("\nLong-term reference picture with POC = %3d seems to have been removed or not correctly decoded.", this->getPOC() + pReferencePictureSet->getDeltaPOC(i));
1437          }
1438          atLeastOneRemoved = 1;
1439        }
1440        else
1441        {
1442          if(printErrors)
1443          {
1444            printf("\nLong-term reference picture with POC = %3d is lost or not correctly decoded!", this->getPOC() + pReferencePictureSet->getDeltaPOC(i));
1445          }
1446          atLeastOneLost = 1;
1447          iPocLost=this->getPOC() + pReferencePictureSet->getDeltaPOC(i);
1448        }
1449      }
1450      else if(bUseRecoveryPoint && this->getPOC() > pocRandomAccess)
1451      {
1452        atLeastOneUnabledByRecoveryPoint = 1;
1453      }
1454      else if(bUseRecoveryPoint && (this->getAssociatedIRAPType()==NAL_UNIT_CODED_SLICE_IDR_N_LP || this->getAssociatedIRAPType()==NAL_UNIT_CODED_SLICE_IDR_W_RADL))
1455      {
1456        atLeastOneFlushedByPreviousIDR = 1;
1457      }
1458    }
1459  }
1460  // loop through all short-term pictures in the Reference Picture Set
1461  // to see if the picture should be kept as reference picture
1462  for(i=0;i<pReferencePictureSet->getNumberOfNegativePictures()+pReferencePictureSet->getNumberOfPositivePictures();i++)
1463  {
1464    isAvailable = 0;
1465    // loop through all pictures in the reference picture buffer
1466    TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1467    while ( iterPic != rcListPic.end())
1468    {
1469      rpcPic = *(iterPic++);
1470
1471      if(!rpcPic->getIsLongTerm() && rpcPic->getPicSym()->getSlice(0)->getPOC() == this->getPOC() + pReferencePictureSet->getDeltaPOC(i) && rpcPic->getSlice(0)->isReferenced())
1472      {
1473        if(bUseRecoveryPoint && this->getPOC() > pocRandomAccess && this->getPOC() + pReferencePictureSet->getDeltaPOC(i) < pocRandomAccess)
1474        {
1475          isAvailable = 0;
1476        }
1477        else
1478        {
1479          isAvailable = 1;
1480        }
1481      }
1482    }
1483    // report that a picture is lost if it is in the Reference Picture Set
1484    // but not available as reference picture
1485    if(isAvailable == 0)
1486    {
1487      if (this->getPOC() + pReferencePictureSet->getDeltaPOC(i) >= pocRandomAccess)
1488      {
1489        if(!pReferencePictureSet->getUsed(i) )
1490        {
1491          if(printErrors)
1492          {
1493            printf("\nShort-term reference picture with POC = %3d seems to have been removed or not correctly decoded.", this->getPOC() + pReferencePictureSet->getDeltaPOC(i));
1494          }
1495          atLeastOneRemoved = 1;
1496        }
1497        else
1498        {
1499          if(printErrors)
1500          {
1501            printf("\nShort-term reference picture with POC = %3d is lost or not correctly decoded!", this->getPOC() + pReferencePictureSet->getDeltaPOC(i));
1502          }
1503          atLeastOneLost = 1;
1504          iPocLost=this->getPOC() + pReferencePictureSet->getDeltaPOC(i);
1505        }
1506      }
1507      else if(bUseRecoveryPoint && this->getPOC() > pocRandomAccess)
1508      {
1509        atLeastOneUnabledByRecoveryPoint = 1;
1510      }
1511      else if(bUseRecoveryPoint && (this->getAssociatedIRAPType()==NAL_UNIT_CODED_SLICE_IDR_N_LP || this->getAssociatedIRAPType()==NAL_UNIT_CODED_SLICE_IDR_W_RADL))
1512      {
1513        atLeastOneFlushedByPreviousIDR = 1;
1514      }
1515    }
1516  }
1517
1518  if(atLeastOneUnabledByRecoveryPoint || atLeastOneFlushedByPreviousIDR)
1519  {
1520    return -1;
1521  }   
1522  if(atLeastOneLost)
1523  {
1524    return iPocLost+1;
1525  }
1526  if(atLeastOneRemoved)
1527  {
1528    return -2;
1529  }
1530  else
1531  {
1532    return 0;
1533  }
1534}
1535
1536/** Function for constructing an explicit Reference Picture Set out of the available pictures in a referenced Reference Picture Set
1537*/
1538Void TComSlice::createExplicitReferencePictureSetFromReference( TComList<TComPic*>& rcListPic, const TComReferencePictureSet *pReferencePictureSet, Bool isRAP, Int pocRandomAccess, Bool bUseRecoveryPoint, const Bool bEfficientFieldIRAPEnabled)
1539{
1540  TComPic* rpcPic;
1541  Int i, j;
1542  Int k = 0;
1543  Int nrOfNegativePictures = 0;
1544  Int nrOfPositivePictures = 0;
1545  TComReferencePictureSet* pLocalRPS = this->getLocalRPS();
1546  (*pLocalRPS)=TComReferencePictureSet();
1547
1548  Bool irapIsInRPS = false; // Used when bEfficientFieldIRAPEnabled==true
1549
1550  // loop through all pictures in the Reference Picture Set
1551  for(i=0;i<pReferencePictureSet->getNumberOfPictures();i++)
1552  {
1553    j = 0;
1554    // loop through all pictures in the reference picture buffer
1555    TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1556    while ( iterPic != rcListPic.end())
1557    {
1558      j++;
1559      rpcPic = *(iterPic++);
1560
1561      if(rpcPic->getPicSym()->getSlice(0)->getPOC() == this->getPOC() + pReferencePictureSet->getDeltaPOC(i) && rpcPic->getSlice(0)->isReferenced())
1562      {
1563        // This picture exists as a reference picture
1564        // and should be added to the explicit Reference Picture Set
1565        pLocalRPS->setDeltaPOC(k, pReferencePictureSet->getDeltaPOC(i));
1566        pLocalRPS->setUsed(k, pReferencePictureSet->getUsed(i) && (!isRAP));
1567        if (bEfficientFieldIRAPEnabled)
1568        {
1569          pLocalRPS->setUsed(k, pLocalRPS->getUsed(k) && !(bUseRecoveryPoint && this->getPOC() > pocRandomAccess && this->getPOC() + pReferencePictureSet->getDeltaPOC(i) < pocRandomAccess) );
1570        }
1571
1572        if(pLocalRPS->getDeltaPOC(k) < 0)
1573        {
1574          nrOfNegativePictures++;
1575        }
1576        else
1577        {
1578          if(bEfficientFieldIRAPEnabled && rpcPic->getPicSym()->getSlice(0)->getPOC() == this->getAssociatedIRAPPOC() && this->getAssociatedIRAPPOC() == this->getPOC()+1)
1579          {
1580            irapIsInRPS = true;
1581          }
1582          nrOfPositivePictures++;
1583        }
1584        k++;
1585      }
1586    }
1587  }
1588
1589  Bool useNewRPS = false;
1590  // if current picture is complimentary field associated to IRAP, add the IRAP to its RPS.
1591  if(bEfficientFieldIRAPEnabled && m_pcPic->isField() && !irapIsInRPS)
1592  {
1593    TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1594    while ( iterPic != rcListPic.end())
1595    {
1596      rpcPic = *(iterPic++);
1597      if(rpcPic->getPicSym()->getSlice(0)->getPOC() == this->getAssociatedIRAPPOC() && this->getAssociatedIRAPPOC() == this->getPOC()+1)
1598      {
1599        pLocalRPS->setDeltaPOC(k, 1);
1600        pLocalRPS->setUsed(k, true);
1601        nrOfPositivePictures++;
1602        k ++;
1603        useNewRPS = true;
1604      }
1605    }
1606  }
1607  pLocalRPS->setNumberOfNegativePictures(nrOfNegativePictures);
1608  pLocalRPS->setNumberOfPositivePictures(nrOfPositivePictures);
1609  pLocalRPS->setNumberOfPictures(nrOfNegativePictures+nrOfPositivePictures);
1610  // This is a simplistic inter rps example. A smarter encoder will look for a better reference RPS to do the
1611  // inter RPS prediction with.  Here we just use the reference used by pReferencePictureSet.
1612  // If pReferencePictureSet is not inter_RPS_predicted, then inter_RPS_prediction is for the current RPS also disabled.
1613  if (!pReferencePictureSet->getInterRPSPrediction() || useNewRPS )
1614  {
1615    pLocalRPS->setInterRPSPrediction(false);
1616    pLocalRPS->setNumRefIdc(0);
1617  }
1618  else
1619  {
1620    Int rIdx =  this->getRPSidx() - pReferencePictureSet->getDeltaRIdxMinus1() - 1;
1621    Int deltaRPS = pReferencePictureSet->getDeltaRPS();
1622    const TComReferencePictureSet* pcRefRPS = this->getSPS()->getRPSList()->getReferencePictureSet(rIdx);
1623    Int iRefPics = pcRefRPS->getNumberOfPictures();
1624    Int iNewIdc=0;
1625    for(i=0; i<= iRefPics; i++)
1626    {
1627      Int deltaPOC = ((i != iRefPics)? pcRefRPS->getDeltaPOC(i) : 0);  // check if the reference abs POC is >= 0
1628      Int iRefIdc = 0;
1629      for (j=0; j < pLocalRPS->getNumberOfPictures(); j++) // loop through the  pictures in the new RPS
1630      {
1631        if ( (deltaPOC + deltaRPS) == pLocalRPS->getDeltaPOC(j))
1632        {
1633          if (pLocalRPS->getUsed(j))
1634          {
1635            iRefIdc = 1;
1636          }
1637          else
1638          {
1639            iRefIdc = 2;
1640          }
1641        }
1642      }
1643      pLocalRPS->setRefIdc(i, iRefIdc);
1644      iNewIdc++;
1645    }
1646    pLocalRPS->setInterRPSPrediction(true);
1647    pLocalRPS->setNumRefIdc(iNewIdc);
1648    pLocalRPS->setDeltaRPS(deltaRPS);
1649    pLocalRPS->setDeltaRIdxMinus1(pReferencePictureSet->getDeltaRIdxMinus1() + this->getSPS()->getRPSList()->getNumberOfReferencePictureSets() - this->getRPSidx());
1650  }
1651
1652  this->setRPS(pLocalRPS);
1653  this->setRPSidx(-1);
1654}
1655
1656//! get AC and DC values for weighted pred
1657Void  TComSlice::getWpAcDcParam(WPACDCParam *&wp)
1658{
1659  wp = m_weightACDCParam;
1660}
1661
1662//! init AC and DC values for weighted pred
1663Void  TComSlice::initWpAcDcParam()
1664{
1665  for(Int iComp = 0; iComp < MAX_NUM_COMPONENT; iComp++ )
1666  {
1667    m_weightACDCParam[iComp].iAC = 0;
1668    m_weightACDCParam[iComp].iDC = 0;
1669  }
1670}
1671
1672//! get tables for weighted prediction
1673Void  TComSlice::getWpScaling( RefPicList e, Int iRefIdx, WPScalingParam *&wp )
1674{
1675  assert (e<NUM_REF_PIC_LIST_01);
1676  wp = m_weightPredTable[e][iRefIdx];
1677}
1678
1679//! reset Default WP tables settings : no weight.
1680Void  TComSlice::resetWpScaling()
1681{
1682  for ( Int e=0 ; e<NUM_REF_PIC_LIST_01 ; e++ )
1683  {
1684    for ( Int i=0 ; i<MAX_NUM_REF ; i++ )
1685    {
1686      for ( Int yuv=0 ; yuv<MAX_NUM_COMPONENT ; yuv++ )
1687      {
1688        WPScalingParam  *pwp = &(m_weightPredTable[e][i][yuv]);
1689        pwp->bPresentFlag      = false;
1690        pwp->uiLog2WeightDenom = 0;
1691        pwp->uiLog2WeightDenom = 0;
1692        pwp->iWeight           = 1;
1693        pwp->iOffset           = 0;
1694      }
1695    }
1696  }
1697}
1698
1699//! init WP table
1700Void  TComSlice::initWpScaling(const TComSPS *sps)
1701{
1702  const Bool bUseHighPrecisionPredictionWeighting = sps->getSpsRangeExtension().getHighPrecisionOffsetsEnabledFlag();
1703  for ( Int e=0 ; e<NUM_REF_PIC_LIST_01 ; e++ )
1704  {
1705    for ( Int i=0 ; i<MAX_NUM_REF ; i++ )
1706    {
1707      for ( Int yuv=0 ; yuv<MAX_NUM_COMPONENT ; yuv++ )
1708      {
1709        WPScalingParam  *pwp = &(m_weightPredTable[e][i][yuv]);
1710        if ( !pwp->bPresentFlag )
1711        {
1712          // Inferring values not present :
1713          pwp->iWeight = (1 << pwp->uiLog2WeightDenom);
1714          pwp->iOffset = 0;
1715        }
1716
1717        const Int offsetScalingFactor = bUseHighPrecisionPredictionWeighting ? 1 : (1 << (sps->getBitDepth(toChannelType(ComponentID(yuv)))-8));
1718
1719        pwp->w      = pwp->iWeight;
1720        pwp->o      = pwp->iOffset * offsetScalingFactor; //NOTE: This value of the ".o" variable is never used - .o is set immediately before it gets used
1721        pwp->shift  = pwp->uiLog2WeightDenom;
1722        pwp->round  = (pwp->uiLog2WeightDenom>=1) ? (1 << (pwp->uiLog2WeightDenom-1)) : (0);
1723      }
1724    }
1725  }
1726}
1727
1728// ------------------------------------------------------------------------------------------------
1729// Video parameter set (VPS)
1730// ------------------------------------------------------------------------------------------------
1731TComVPS::TComVPS()
1732: m_VPSId                     (  0)
1733, m_uiMaxTLayers              (  1)
1734#if NH_MV
1735, m_uiMaxLayersMinus1         (  0)
1736#else
1737, m_uiMaxLayers               (  1)
1738#endif
1739, m_bTemporalIdNestingFlag    (false)
1740, m_numHrdParameters          (  0)
1741#if NH_MV
1742, m_maxLayerId             (  0)
1743#else
1744, m_maxNuhReservedZeroLayerId (  0)
1745#endif
1746, m_hrdParameters             ()
1747, m_hrdOpSetIdx               ()
1748, m_cprmsPresentFlag          ()
1749{
1750#if NH_MV
1751  m_vpsBaseLayerInternalFlag = true; 
1752  m_vpsBaseLayerAvailableFlag = true; 
1753
1754  m_numViews = 0; 
1755
1756#endif
1757
1758  for( Int i = 0; i < MAX_TLAYER; i++)
1759  {
1760    m_numReorderPics[i] = 0;
1761    m_uiMaxDecPicBuffering[i] = 1;
1762    m_uiMaxLatencyIncrease[i] = 0;
1763  }
1764#if NH_MV
1765  for (Int lsIdx = 0; lsIdx < MAX_VPS_OP_SETS_PLUS1; lsIdx++ )
1766  { 
1767    for( Int layerId = 0; layerId < MAX_VPS_NUH_LAYER_ID_PLUS1; layerId++ )
1768    {
1769      m_layerIdIncludedFlag[lsIdx][layerId] = (( lsIdx == 0 ) && ( layerId == 0 )) ; 
1770    }
1771  } 
1772  m_vpsNumProfileTierLevelMinus1 = -1; 
1773   
1774  m_numAddLayerSets              = 0;   
1775  m_numAddOlss                   = 0; 
1776  m_defaultOutputLayerIdc     = 0; 
1777 
1778  for ( Int i = 0; i < MAX_VPS_OUTPUTLAYER_SETS; i++)
1779  {
1780    m_layerSetIdxForOlsMinus1[i]  = -1; 
1781    for ( Int j = 0; j < MAX_VPS_NUH_LAYER_ID_PLUS1; j++)
1782    {
1783      m_profileTierLevelIdx[i][j] = -1; 
1784      m_outputLayerFlag[i][j] = false; 
1785    }
1786    m_altOutputLayerFlag[ i ]       = false; 
1787  }
1788
1789  m_repFormatIdxPresentFlag = false; 
1790  m_maxOneActiveRefLayerFlag = false; 
1791  m_vpsPocLsbAlignedFlag  = false; 
1792  m_directDepTypeLenMinus2   = 0;         
1793 
1794
1795  m_vpsExtensionFlag = true; 
1796  m_vpsNonVuiExtensionLength = 0;
1797  m_splittingFlag    = false;
1798
1799 
1800  for( Int i = 0; i < MAX_NUM_SCALABILITY_TYPES; i++ )
1801  {
1802    m_scalabilityMaskFlag[i] = false;
1803    m_dimensionIdLen [i]  = -1; 
1804  }
1805
1806  m_vpsNuhLayerIdPresentFlag = false;
1807
1808  for( Int i = 0; i < MAX_VPS_OP_SETS_PLUS1; i++ )
1809  {
1810    m_vpsProfilePresentFlag   [i] = false;
1811    m_layerSetIdxForOlsMinus1       [i] = 0;
1812    for( Int j = 0; j < MAX_VPS_NUH_LAYER_ID_PLUS1; j++ )
1813    {
1814      m_outputLayerFlag[i][j] = false;
1815    }
1816  }
1817
1818  for( Int i = 0; i < MAX_NUM_LAYER_IDS; i++ )
1819  {
1820    m_layerIdInVps[i] =  (i == 0 ) ? 0 : -1;         
1821  }
1822
1823  for( Int i = 0; i < MAX_NUM_LAYERS; i++ )
1824  {
1825    m_layerIdInNuh      [i] = ( i == 0 ) ? 0 : -1; 
1826    m_numDirectRefLayers[i] = 0; 
1827    m_vpsRepFormatIdx    [i] = 0; 
1828    m_pocLsbNotPresentFlag[i] = 0;   
1829    m_viewIdVal          [i] = 0; 
1830
1831
1832    for( Int j = 0; j < MAX_NUM_LAYERS; j++ )
1833    {
1834      m_directDependencyFlag[i][j] = false;
1835      m_directDependencyType[i][j] = -1; 
1836      m_dependencyFlag  [i][j]    = false; 
1837      m_idDirectRefLayer[i][j]    = -1; 
1838      m_idPredictedLayer[i][j]    = -1; 
1839      m_idRefLayer      [i][j]    = -1; 
1840      m_maxTidIlRefPicsPlus1[i][j]  = 7;
1841    }
1842
1843    for( Int j = 0; j < MAX_NUM_SCALABILITY_TYPES; j++ )
1844    {
1845      m_dimensionId[i][j] = 0;
1846    }
1847  } 
1848#endif
1849}
1850
1851TComVPS::~TComVPS()
1852{
1853}
1854
1855#if NH_MV
1856
1857Bool TComVPS::checkVPSExtensionSyntax()
1858{
1859  for( Int layer = 1; layer <= getMaxLayersMinus1(); layer++ )
1860  {
1861    // check layer_id_in_nuh constraint
1862    assert( getLayerIdInNuh( layer ) > getLayerIdInNuh( layer -1 ) );
1863  }
1864
1865  //The value of vps_num_rep_formats_minus1 shall be in the range of 0 to 255, inclusive.
1866  assert( getVpsNumRepFormatsMinus1() >= 0 ); 
1867  assert( getVpsNumRepFormatsMinus1() <= 255 ); 
1868
1869  // The value of num_add_layer_sets shall be in the range of 0 to 1023, inclusive.
1870  assert( getNumAddLayerSets() >= 0 && getNumAddLayerSets() <= 1023 ); 
1871  return true; 
1872}
1873
1874Int TComVPS::getNumScalabilityTypes() const
1875{
1876  return scalTypeToScalIdx( ScalabilityType(MAX_NUM_SCALABILITY_TYPES) );
1877}
1878
1879Int TComVPS::scalTypeToScalIdx( ScalabilityType scalType ) const
1880{
1881  assert( (Int)scalType >= 0 && (Int)scalType <= MAX_NUM_SCALABILITY_TYPES );
1882  assert( (Int)scalType == MAX_NUM_SCALABILITY_TYPES || getScalabilityMaskFlag( scalType ) );
1883  Int scalIdx = 0; 
1884  for( Int curScalType = 0; curScalType < scalType; curScalType++ )
1885  {
1886    scalIdx += ( getScalabilityMaskFlag( curScalType ) ? 1 : 0 );
1887
1888  }
1889
1890  return scalIdx; 
1891}
1892Void TComVPS::setScalabilityMaskFlag( UInt val )
1893{
1894  for ( Int scalType = 0; scalType < MAX_NUM_SCALABILITY_TYPES; scalType++ ) 
1895  {
1896    setScalabilityMaskFlag( scalType, ( val & (1 << scalType ) ) != 0 );
1897  }
1898}
1899
1900Void TComVPS::setRefLayers()
1901{
1902
1903  for( Int i = 0; i  <=  getMaxLayersMinus1(); i++ )
1904  {
1905    for( Int j = 0; j  <=  getMaxLayersMinus1(); j++ )
1906    {
1907      m_dependencyFlag[ i ][ j ] = getDirectDependencyFlag( i , j );
1908      for( Int k = 0; k < i; k++ )
1909      {
1910        if( getDirectDependencyFlag(i , k )  &&  m_dependencyFlag[k][j] )
1911        {
1912          m_dependencyFlag[ i ][ j ] = true;
1913        }
1914      }
1915    }
1916  }
1917
1918  for( Int i = 0; i  <=  getMaxLayersMinus1(); i++ )
1919  {
1920    Int iNuhLId = getLayerIdInNuh( i );
1921    Int d = 0;
1922    Int r = 0;
1923    Int p = 0;
1924
1925    for( Int j = 0; j  <=  getMaxLayersMinus1(); j++ )
1926    {
1927      Int jNuhLid = getLayerIdInNuh( j );
1928      if( getDirectDependencyFlag( i , j ) )
1929      {
1930        m_idDirectRefLayer[iNuhLId][d++] = jNuhLid;
1931      }
1932
1933      if( getDependencyFlag( i , j ) )
1934      {
1935        m_idRefLayer      [iNuhLId][r++] = jNuhLid;
1936      }
1937      if( getDependencyFlag( j , i ) )
1938      {
1939        m_idPredictedLayer[iNuhLId][p++] = jNuhLid;
1940      }
1941    }
1942    m_numDirectRefLayers[ iNuhLId ] = d;
1943
1944    m_numRefLayers      [ iNuhLId ] = r;
1945    m_numPredictedLayers[ iNuhLId ] = p;
1946  }
1947 
1948  Bool layerIdInListFlag[ 64 ]; 
1949  for( Int i = 0; i  <=  63; i++ )
1950  {
1951    layerIdInListFlag[ i ] = 0;
1952  }
1953
1954  Int k = 0; 
1955  for( Int i = 0; i  <=  getMaxLayersMinus1(); i++ )
1956  {
1957    Int iNuhLId = getLayerIdInNuh( i );
1958    if( getNumDirectRefLayers( iNuhLId )  ==  0 )
1959    {
1960      m_treePartitionLayerIdList[ k ][ 0 ] = iNuhLId;
1961      Int h = 1; 
1962      for( Int j = 0; j < getNumPredictedLayers( iNuhLId ); j++ ) 
1963      {
1964        Int predLId = getIdPredictedLayer( iNuhLId, j );
1965        if ( !layerIdInListFlag[ predLId ] )
1966        {
1967          m_treePartitionLayerIdList[ k ][ h++ ] = predLId;
1968          layerIdInListFlag[ predLId ] = 1; 
1969        }         
1970      }
1971      m_numLayersInTreePartition[ k++ ] = h;
1972    }
1973  }
1974  m_numIndependentLayers = k;
1975}
1976
1977
1978Void     TComVPS::initNumViews( )
1979{
1980  m_numViews = 1; 
1981
1982  for( Int i = 0; i <=  getMaxLayersMinus1(); i++ )
1983  {
1984    Int lId = getLayerIdInNuh( i ); 
1985    if( i > 0 )
1986    {
1987      Bool newViewFlag = true; 
1988      for( Int j = 0; j < i; j++ )
1989      {
1990        if( getViewOrderIdx( lId )  ==  getViewOrderIdx( getLayerIdInNuh( j ) )  )
1991        {
1992          newViewFlag = false;
1993        }
1994      }
1995      if( newViewFlag )
1996      {
1997        m_numViews++;
1998      }
1999    }
2000  }
2001}
2002
2003
2004Int TComVPS::getScalabilityId( Int layerIdInVps, ScalabilityType scalType ) const
2005{
2006  return getScalabilityMaskFlag( scalType ) ? getDimensionId( layerIdInVps, scalTypeToScalIdx( scalType ) ) : 0;
2007}
2008
2009
2010
2011Int TComVPS::xGetDimBitOffset( Int j ) const
2012{
2013  Int dimBitOffset = 0; 
2014  if ( getSplittingFlag() && j == getNumScalabilityTypes() )
2015  {
2016     dimBitOffset = 6; 
2017  }
2018  else
2019  {
2020    for (Int dimIdx = 0; dimIdx <= j-1; dimIdx++)
2021    {
2022      dimBitOffset += getDimensionIdLen( dimIdx ); 
2023    }
2024  }
2025  return dimBitOffset; 
2026}
2027
2028Int TComVPS::inferDimensionId( Int i, Int j ) const
2029{
2030    return ( ( getLayerIdInNuh( i ) & ( (1 << xGetDimBitOffset( j + 1 ) ) - 1) ) >> xGetDimBitOffset( j ) ); 
2031}
2032
2033Int TComVPS::inferLastDimsionIdLenMinus1() const
2034{
2035  return ( 5 - xGetDimBitOffset( getNumScalabilityTypes() - 1 ) ); 
2036}
2037
2038Int TComVPS::getNumLayersInIdList( Int lsIdx ) const
2039{
2040  assert( lsIdx >= 0 ); 
2041  assert( lsIdx <= getNumLayerSets() ); 
2042  return (Int) m_layerSetLayerIdList[ lsIdx ].size(); 
2043}
2044
2045Int    TComVPS::getNumOutputLayerSets() const
2046{
2047  return getNumAddOlss() + getNumLayerSets(); 
2048}
2049
2050
2051
2052Void TComVPS::deriveLayerSetLayerIdList()
2053{
2054  m_layerSetLayerIdList.resize( getVpsNumLayerSetsMinus1() + 1 ); 
2055  for (Int i = 0; i <= getVpsNumLayerSetsMinus1(); i++ )
2056  {
2057    for( Int m = 0; m  <= getVpsMaxLayerId(); m++ )
2058    {
2059      if( getLayerIdIncludedFlag( i, m) ) 
2060      {
2061        m_layerSetLayerIdList[ i ].push_back( m );       
2062      }
2063    }
2064  }
2065}
2066
2067Void TComVPS::initTargetLayerIdLists()
2068{
2069  m_targetDecLayerIdLists.resize( getNumOutputLayerSets() ); 
2070  m_targetOptLayerIdLists.resize( getNumOutputLayerSets() ); 
2071}
2072
2073Void TComVPS::deriveTargetLayerIdList( Int i )
2074{ 
2075  Int lsIdx = olsIdxToLsIdx( i );     
2076
2077  for( Int j = 0; j < getNumLayersInIdList( lsIdx ); j++ )
2078  {
2079    if ( getNecessaryLayerFlag( i , j ))
2080    {
2081      m_targetDecLayerIdLists[i].push_back( m_layerSetLayerIdList[ lsIdx ][ j ] ); 
2082    }
2083
2084    if( getOutputLayerFlag( i, j  ))
2085    {
2086      m_targetOptLayerIdLists[i].push_back( m_layerSetLayerIdList[ lsIdx ][ j ] );
2087    }
2088  } 
2089  assert( getNumOutputLayersInOutputLayerSet( i ) > 0 ); 
2090}
2091
2092Bool TComVPS::inferOutputLayerFlag( Int i, Int j ) const
2093{
2094  Bool outputLayerFlag; 
2095  switch ( getDefaultOutputLayerIdc( ) )
2096  {
2097  case 0:
2098    outputLayerFlag = true; 
2099    break; 
2100  case 1:
2101    outputLayerFlag = ( j == m_layerSetLayerIdList[ olsIdxToLsIdx( i ) ].size() - 1 ); 
2102    break;
2103  case 2:
2104    if ( i == 0 && j == 0)
2105    {     
2106      outputLayerFlag = true;  // This is a software only fix for a bug in the spec. In spec outputLayerFlag is neither present nor inferred.
2107    }
2108    else
2109    {
2110      assert( 0 ); 
2111    }
2112    break; 
2113  default:     
2114    assert( 0 );
2115    break; 
2116  }
2117  return outputLayerFlag;
2118}
2119
2120Int TComVPS::getMaxSubLayersInLayerSetMinus1( Int i ) const
2121{
2122  Int maxSLMinus1 = 0; 
2123  for( Int k = 0; k < getNumLayersInIdList( i ); k++ )
2124  {
2125    Int lId = m_layerSetLayerIdList[i][k];
2126    maxSLMinus1 = std::max( maxSLMinus1, getSubLayersVpsMaxMinus1( getLayerIdInVps( lId ) ));
2127  }
2128  return maxSLMinus1;
2129}
2130
2131Bool TComVPS::getAltOutputLayerFlagVar( Int i ) const
2132{
2133  // Semantics variable not syntax element !
2134
2135  Bool altOptLayerFlag = false;     
2136  if ( i > 0 && getNumOutputLayersInOutputLayerSet( i ) == 1 && 
2137    getNumDirectRefLayers( getOlsHighestOutputLayerId( i ) ) > 0 )
2138  {
2139    altOptLayerFlag = getAltOutputLayerFlag( i ); 
2140  }
2141  return altOptLayerFlag;
2142}
2143
2144
2145
2146Int TComVPS::inferProfileTierLevelIdx(Int i, Int j) const
2147{
2148  Bool inferZero        = ( i == 0 && j == 0 &&  getVpsBaseLayerInternalFlag() );
2149  Bool inferGreaterZero = getNecessaryLayerFlag(i,j) && ( getVpsNumProfileTierLevelMinus1() == 0 ); 
2150  assert( inferZero || inferGreaterZero );
2151
2152  Bool ptlIdx = 0; // inference for greaterZero
2153  if ( inferZero )
2154  {
2155    ptlIdx = getMaxLayersMinus1() > 0 ? 1 : 0; 
2156    if ( inferGreaterZero )
2157    {
2158      assert( ptlIdx == 0 ); 
2159      // This should never happen since :
2160      // 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.
2161    }
2162  }
2163  return ptlIdx;
2164}
2165
2166Void TComVPS::deriveAddLayerSetLayerIdList(Int i)
2167{
2168  assert( m_layerSetLayerIdList.size() ==  ( getVpsNumLayerSetsMinus1() + 1 + i ) ); 
2169  std::vector<Int> layerSetLayerIdList;
2170
2171  for( Int treeIdx = 1; treeIdx < getNumIndependentLayers(); treeIdx++ )
2172  { 
2173    // The value of highest_layer_idx_plus1[ i ][ j ] shall be in the range of 0 to NumLayersInTreePartition[ j ], inclusive.
2174    assert( getHighestLayerIdxPlus1( i, treeIdx ) >= 0 && getHighestLayerIdxPlus1( i, treeIdx ) <= getNumLayersInTreePartition( treeIdx ) );
2175
2176    for( Int layerCnt = 0; layerCnt < getHighestLayerIdxPlus1( i, treeIdx ); layerCnt++ )
2177    {
2178      layerSetLayerIdList.push_back( getTreePartitionLayerIdList( treeIdx, layerCnt ) );
2179    }
2180  }
2181  m_layerSetLayerIdList.push_back( layerSetLayerIdList ); 
2182
2183  //It is a requirement of bitstream conformance that
2184  //NumLayersInIdList[ vps_num_layer_sets_minus1 + 1 + i ] shall be greater than 0.
2185  assert( getNumLayersInIdList( getVpsNumLayerSetsMinus1() + 1 + i ) > 0 );
2186}
2187
2188
2189Void TComVPS::deriveNecessaryLayerFlags(Int olsIdx)
2190{
2191  AOF( olsIdx >= 0 && olsIdx < getNumOutputLayerSets() ); 
2192  Int lsIdx = olsIdxToLsIdx( olsIdx );
2193  for( Int lsLayerIdx = 0; lsLayerIdx < getNumLayersInIdList( lsIdx) ; lsLayerIdx++ )
2194  {
2195    m_necessaryLayerFlag[ olsIdx ][ lsLayerIdx ] = 0;
2196  }
2197  for( Int lsLayerIdx = 0; lsLayerIdx < getNumLayersInIdList( lsIdx ); lsLayerIdx++ )
2198  {
2199    if( getOutputLayerFlag( olsIdx, lsLayerIdx  ))
2200    {
2201      m_necessaryLayerFlag[ olsIdx ][ lsLayerIdx ] = 1;
2202      Int currLayerId = getLayerSetLayerIdList( lsIdx, lsLayerIdx );
2203      for( Int rLsLayerIdx = 0; rLsLayerIdx < lsLayerIdx; rLsLayerIdx++ )
2204      {
2205        Int refLayerId = getLayerSetLayerIdList( lsIdx, rLsLayerIdx );
2206        if( getDependencyFlag( getLayerIdInVps( currLayerId ), getLayerIdInVps( refLayerId ) ) )
2207        {
2208          m_necessaryLayerFlag[ olsIdx ][ rLsLayerIdx ] = 1;
2209        }
2210      }
2211    }
2212  }
2213  m_numNecessaryLayers[ olsIdx ] = 0;
2214  for( Int lsLayerIdx = 0; lsLayerIdx < getNumLayersInIdList( lsIdx ); lsLayerIdx++ ) 
2215  {
2216    m_numNecessaryLayers[ olsIdx ]  +=  m_necessaryLayerFlag[ olsIdx ][ lsLayerIdx ];
2217  }
2218}
2219
2220Void TComVPS::printPTL() const
2221{
2222  std::vector<Int> idx; 
2223  std::vector<Int> num; 
2224  IntAry2d ptlInfo; 
2225
2226  std::cout << std::right << std::setw(60) << std::setfill('-') << " " << std::setfill(' ') << std::endl << "PTLI" << std::endl; 
2227
2228  for ( Int i = 0; i <= getVpsNumProfileTierLevelMinus1(); i++ )
2229  {
2230    std::vector<Int> curPtlInfo;
2231    const ProfileTierLevel* ptl = getPTL( i )->getGeneralPTL(); 
2232    curPtlInfo.push_back( (Int) ptl->getProfileIdc()  );
2233    curPtlInfo.push_back( (Int) ptl->getTierFlag()    );
2234    curPtlInfo.push_back( (Int) ptl->getLevelIdc()    );
2235    curPtlInfo.push_back( (Int) ptl->getInbldFlag()   );
2236
2237    idx.push_back ( i );
2238    num.push_back ( 4 ); 
2239    ptlInfo.push_back( curPtlInfo );
2240  } 
2241
2242  xPrintArray( "VpsProfileTierLevel", getVpsNumProfileTierLevelMinus1() + 1, idx, num, ptlInfo, false  ); 
2243
2244  num.clear(); 
2245  idx.clear(); 
2246  for (Int i = 0; i < getNumOutputLayerSets(); i++)
2247  {
2248    num.push_back ( getNumLayersInIdList( olsIdxToLsIdx( i ))  ); 
2249    idx.push_back( i ); 
2250  }
2251
2252  xPrintArray( "profile_tier_level_idx", getNumOutputLayerSets(), idx, num, m_profileTierLevelIdx, true );
2253  std::cout << std::endl;
2254}
2255
2256Void TComVPS::printLayerDependencies() const
2257{
2258  vector<Int> fullArray;
2259  vector<Int> range; 
2260
2261
2262  vector<Int> viewOrderIndex;
2263  vector<Int> auxId;
2264  vector<Int> dependencyId; 
2265  vector<Int> viewId; 
2266  for (Int i = 0; i <= getMaxLayersMinus1(); i++ )
2267  {
2268    fullArray.push_back( getMaxLayersMinus1() + 1 ); 
2269    range.push_back( i ); 
2270    viewOrderIndex.push_back( getViewIndex   ( i ) );
2271    dependencyId  .push_back( getDependencyId( i ) );
2272    auxId         .push_back( getAuxId       ( i ) );     
2273    viewId        .push_back( getViewId      ( getLayerIdInNuh( i ) ) );
2274  }
2275  std::cout << std::right << std::setw(60) << std::setfill('-') << " " << std::setfill(' ') << std::endl << "Layer Dependencies" << std::endl; 
2276  xPrintArray( "direct_dependency_flag", getMaxLayersMinus1()+1, range, fullArray, m_directDependencyFlag, false ); 
2277  xPrintArray( "DependencyFlag", getMaxLayersMinus1()+1, range, fullArray, m_dependencyFlag, false ); 
2278  xPrintArray( "layer_id_in_nuh", getMaxLayersMinus1()+1, m_layerIdInNuh, true  );     
2279  xPrintArray( "IdPredictedLayer", getMaxLayersMinus1() + 1, m_layerIdInNuh, m_numPredictedLayers, m_idPredictedLayer, true );
2280  xPrintArray( "IdRefLayer"      , getMaxLayersMinus1() + 1, m_layerIdInNuh, m_numRefLayers, m_idRefLayer, true );
2281  xPrintArray( "IdDirectRefLayer", getMaxLayersMinus1() + 1, m_layerIdInNuh, m_numDirectRefLayers, m_idDirectRefLayer, true );
2282
2283  std::cout << std::endl;
2284}
2285
2286Void TComVPS::printScalabilityId() const
2287{
2288  vector<Int> layerIdxInVps; 
2289
2290
2291  vector<Int> depthId; 
2292  vector<Int> viewOrderIndex;
2293  vector<Int> auxId;
2294  vector<Int> dependencyId; 
2295  vector<Int> viewId; 
2296
2297  for (Int i = 0; i <= getMaxLayersMinus1(); i++ )
2298  {
2299    Int layerIdInNuh = getLayerIdInNuh( i );
2300    layerIdxInVps  .push_back( i ); 
2301    depthId       .push_back( getDepthId     ( layerIdInNuh ) );
2302    viewOrderIndex.push_back( getViewIndex   ( layerIdInNuh ) );
2303    dependencyId  .push_back( getDependencyId( layerIdInNuh ) );
2304    auxId         .push_back( getAuxId       ( layerIdInNuh ) );     
2305    viewId        .push_back( getViewId      ( layerIdInNuh ) );
2306
2307  }
2308
2309  std::cout << std::right << std::setw(60) << std::setfill('-') << " " << std::setfill(' ') << std::endl << "Scalability Ids" << std::endl; 
2310  xPrintArray( "layerIdxInVps"  , getMaxLayersMinus1()+1, layerIdxInVps,          false );
2311  xPrintArray( "layer_id_in_nuh", getMaxLayersMinus1()+1, m_layerIdInNuh, false );     
2312
2313  xPrintArray( "DepthLayerFlag", getMaxLayersMinus1()+1, depthId       , false );     
2314  xPrintArray( "ViewOrderIdx"  , getMaxLayersMinus1()+1, viewOrderIndex, false );     
2315  xPrintArray( "DependencyId"  , getMaxLayersMinus1()+1, dependencyId  , false );     
2316  xPrintArray( "AuxId"         , getMaxLayersMinus1()+1, auxId         , false );     
2317  xPrintArray( "ViewIdVal"     , getMaxLayersMinus1()+1, viewId        , false );     
2318
2319  std::cout << std::endl;
2320}
2321
2322Void TComVPS::printLayerSets() const
2323{
2324  vector<Int> fullArray;
2325  vector<Int> numLayersInIdList; 
2326  vector<Int> rangeLayerSets; 
2327
2328
2329  for (Int i = 0; i < getNumLayerSets(); i++ )
2330  {
2331    numLayersInIdList.push_back( getNumLayersInIdList( i ) );       
2332    rangeLayerSets.push_back( i ); 
2333  }
2334
2335  vector<Int> rangeOutputLayerSets; 
2336  vector<Int> numOutputLayersInOutputLayerSet; 
2337  vector<Int> numDecLayer; 
2338  vector<Int> numLayersInLayerSetForOutputLayerSet; 
2339  vector<Int> vOlsIdxToLsIdx;
2340  for (Int i = 0; i < getNumOutputLayerSets(); i++ )
2341  {
2342    vOlsIdxToLsIdx.push_back( olsIdxToLsIdx(i));
2343    numOutputLayersInOutputLayerSet.push_back( getNumOutputLayersInOutputLayerSet( i ) );       
2344    numDecLayer.push_back( (Int) m_targetDecLayerIdLists[ i ].size() );
2345    rangeOutputLayerSets.push_back( i ); 
2346    numLayersInLayerSetForOutputLayerSet.push_back( getNumLayersInIdList( olsIdxToLsIdx( i ) ) );
2347  }
2348
2349  vector<Int> rangeIndependentLayers;
2350  for(Int i = 0; i < getNumIndependentLayers(); i++ )
2351  {
2352    rangeIndependentLayers.push_back( i );   
2353  }
2354
2355  vector<Int> rangeAddLayerSets;
2356  vector<Int> numHighestLayerIdxPlus1; 
2357  for(Int i = 0; i < getNumAddLayerSets(); i++ )
2358  {
2359    rangeAddLayerSets.push_back( i );   
2360    numHighestLayerIdxPlus1.push_back( getNumIndependentLayers() );
2361  }
2362
2363  std::cout << std::right << std::setw(60) << std::setfill('-') << " " << std::setfill(' ') << std::endl << "Layer Sets" << std::endl;     
2364  xPrintArray( "TreePartitionLayerIdList", getNumIndependentLayers(), rangeIndependentLayers, m_numLayersInTreePartition, m_treePartitionLayerIdList, true );
2365  xPrintArray( "highest_layer_idx_plus1", getNumAddLayerSets(), rangeAddLayerSets, numHighestLayerIdxPlus1, m_highestLayerIdxPlus1, true ); 
2366  xPrintArray( "LayerSetLayerIdList" , (Int) getNumLayerSets()      , rangeLayerSets      , numLayersInIdList, m_layerSetLayerIdList, true );
2367  xPrintArray( "OlsIdxToLsIdx", (Int) vOlsIdxToLsIdx.size(), vOlsIdxToLsIdx, true ); 
2368  xPrintArray( "OutputLayerFlag"     , getNumOutputLayerSets(), rangeOutputLayerSets, numLayersInLayerSetForOutputLayerSet, m_outputLayerFlag, true );
2369  xPrintArray( "TargetOptLayerIdList", getNumOutputLayerSets(), rangeOutputLayerSets, numOutputLayersInOutputLayerSet, m_targetOptLayerIdLists, true );
2370  xPrintArray( "NecessaryLayerFlag"  , getNumOutputLayerSets(), rangeOutputLayerSets, numLayersInLayerSetForOutputLayerSet, m_necessaryLayerFlag   , true );
2371  xPrintArray( "TargetDecLayerIdList", getNumOutputLayerSets(), rangeOutputLayerSets, numDecLayer,                     m_targetDecLayerIdLists, true );
2372  std::cout << endl;
2373}
2374
2375#endif // NH_MV
2376
2377// ------------------------------------------------------------------------------------------------
2378// Sequence parameter set (SPS)
2379// ------------------------------------------------------------------------------------------------
2380
2381TComSPSRExt::TComSPSRExt()
2382 : m_transformSkipRotationEnabledFlag   (false)
2383 , m_transformSkipContextEnabledFlag    (false)
2384// m_rdpcmEnabledFlag initialized below
2385 , m_extendedPrecisionProcessingFlag    (false)
2386 , m_intraSmoothingDisabledFlag         (false)
2387 , m_highPrecisionOffsetsEnabledFlag    (false)
2388 , m_persistentRiceAdaptationEnabledFlag(false)
2389 , m_cabacBypassAlignmentEnabledFlag    (false)
2390{
2391  for (UInt signallingModeIndex = 0; signallingModeIndex < NUMBER_OF_RDPCM_SIGNALLING_MODES; signallingModeIndex++)
2392  {
2393    m_rdpcmEnabledFlag[signallingModeIndex] = false;
2394  }
2395}
2396
2397TComSPS::TComSPS()
2398: m_SPSId                     (  0)
2399, m_VPSId                     (  0)
2400, m_chromaFormatIdc           (CHROMA_420)
2401, m_uiMaxTLayers              (  1)
2402// Structure
2403, m_picWidthInLumaSamples     (352)
2404, m_picHeightInLumaSamples    (288)
2405, m_log2MinCodingBlockSize    (  0)
2406, m_log2DiffMaxMinCodingBlockSize(0)
2407, m_uiMaxCUWidth              ( 32)
2408, m_uiMaxCUHeight             ( 32)
2409, m_uiMaxTotalCUDepth         (  3)
2410, m_bLongTermRefsPresent      (false)
2411, m_uiQuadtreeTULog2MaxSize   (  0)
2412, m_uiQuadtreeTULog2MinSize   (  0)
2413, m_uiQuadtreeTUMaxDepthInter (  0)
2414, m_uiQuadtreeTUMaxDepthIntra (  0)
2415// Tool list
2416, m_usePCM                    (false)
2417, m_pcmLog2MaxSize            (  5)
2418, m_uiPCMLog2MinSize          (  7)
2419, m_bPCMFilterDisableFlag     (false)
2420, m_uiBitsForPOC              (  8)
2421, m_numLongTermRefPicSPS      (  0)
2422#if NH_MV
2423, m_numShortTermRefPicSets    (   0)
2424#endif
2425, m_uiMaxTrSize               ( 32)
2426, m_bUseSAO                   (false)
2427, m_bTemporalIdNestingFlag    (false)
2428, m_scalingListEnabledFlag    (false)
2429, m_useStrongIntraSmoothing   (false)
2430, m_vuiParametersPresentFlag  (false)
2431, m_vuiParameters             ()
2432#if NH_MV
2433, m_pcVPS                     ( NULL )
2434, m_spsInferScalingListFlag   ( false )
2435, m_spsScalingListRefLayerId  ( 0 )
2436
2437, m_updateRepFormatFlag       ( false ) 
2438, m_spsRepFormatIdx           ( 0 )
2439, m_interViewMvVertConstraintFlag (false)
2440#endif
2441
2442{
2443  for(Int ch=0; ch<MAX_NUM_CHANNEL_TYPE; ch++)
2444  {
2445    m_bitDepths.recon[ch] = 8;
2446#if O0043_BEST_EFFORT_DECODING
2447    m_bitDepths.stream[ch] = 8;
2448#endif
2449    m_pcmBitDepths[ch] = 8;
2450    m_qpBDOffset   [ch] = 0;
2451  }
2452
2453
2454  for ( Int i = 0; i < MAX_TLAYER; i++ )
2455  {
2456#if NH_MV
2457    m_uiSpsMaxLatencyIncreasePlus1[i] = 0;
2458#else
2459    m_uiMaxLatencyIncreasePlus1[i] = 0;
2460#endif
2461    m_uiMaxDecPicBuffering[i] = 1;
2462    m_numReorderPics[i]       = 0;
2463  }
2464
2465  ::memset(m_ltRefPicPocLsbSps, 0, sizeof(m_ltRefPicPocLsbSps));
2466  ::memset(m_usedByCurrPicLtSPSFlag, 0, sizeof(m_usedByCurrPicLtSPSFlag));
2467#if NH_MV
2468  m_spsRangeExtensionsFlag     = false;
2469  m_spsMultilayerExtensionFlag = false;
2470  m_spsExtension5bits          = 0;
2471  m_sps3dExtensionFlag         = false; 
2472
2473#endif
2474
2475}
2476
2477TComSPS::~TComSPS()
2478{
2479  m_RPSList.destroy();
2480}
2481
2482Void  TComSPS::createRPSList( Int numRPS )
2483{
2484  m_RPSList.destroy();
2485  m_RPSList.create(numRPS);
2486}
2487
2488const Int TComSPS::m_winUnitX[]={1,2,2,1};
2489const Int TComSPS::m_winUnitY[]={1,2,1,1};
2490
2491TComPPSRExt::TComPPSRExt()
2492: m_log2MaxTransformSkipBlockSize      (2)
2493, m_crossComponentPredictionEnabledFlag(false)
2494, m_diffCuChromaQpOffsetDepth          (0)
2495, m_chromaQpOffsetListLen              (0)
2496// m_ChromaQpAdjTableIncludingNullEntry initialized below
2497// m_log2SaoOffsetScale initialized below
2498{
2499  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.
2500  m_ChromaQpAdjTableIncludingNullEntry[0].u.comp.CrOffset = 0;
2501  for(Int ch=0; ch<MAX_NUM_CHANNEL_TYPE; ch++)
2502  {
2503    m_log2SaoOffsetScale[ch] = 0;
2504  }
2505}
2506
2507TComPPS::TComPPS()
2508: m_PPSId                            (0)
2509, m_SPSId                            (0)
2510, m_picInitQPMinus26                 (0)
2511, m_useDQP                           (false)
2512, m_bConstrainedIntraPred            (false)
2513, m_bSliceChromaQpFlag               (false)
2514, m_uiMaxCuDQPDepth                  (0)
2515, m_chromaCbQpOffset                 (0)
2516, m_chromaCrQpOffset                 (0)
2517, m_numRefIdxL0DefaultActive         (1)
2518, m_numRefIdxL1DefaultActive         (1)
2519, m_TransquantBypassEnableFlag       (false)
2520, m_useTransformSkip                 (false)
2521, m_dependentSliceSegmentsEnabledFlag(false)
2522, m_tilesEnabledFlag                 (false)
2523, m_entropyCodingSyncEnabledFlag     (false)
2524, m_loopFilterAcrossTilesEnabledFlag (true)
2525, m_uniformSpacingFlag               (false)
2526, m_numTileColumnsMinus1             (0)
2527, m_numTileRowsMinus1                (0)
2528, m_signHideFlag                     (false)
2529, m_cabacInitPresentFlag             (false)
2530, m_sliceHeaderExtensionPresentFlag  (false)
2531, m_loopFilterAcrossSlicesEnabledFlag(false)
2532, m_listsModificationPresentFlag     (0)
2533, m_numExtraSliceHeaderBits          (0)
2534#if NH_MV
2535, m_ppsInferScalingListFlag          (false)
2536, m_ppsScalingListRefLayerId         (0)
2537, m_pocResetInfoPresentFlag          (false)
2538#endif
2539{
2540#if NH_MV
2541  m_ppsRangeExtensionsFlag     = false;
2542  m_ppsMultilayerExtensionFlag = false;
2543  m_pps3dExtensionFlag         = false;
2544  m_ppsExtension5bits          = 0;
2545#endif
2546
2547}
2548
2549TComPPS::~TComPPS()
2550{
2551}
2552
2553
2554#if NH_MV
2555Void TComSPS::inferRepFormat( TComVPS* vps, Int layerIdCurr, Bool encoder )
2556{
2557  if ( getMultiLayerExtSpsFlag() )
2558  {   
2559    Int            repFormatIdx = getUpdateRepFormatFlag() ?  getSpsRepFormatIdx() : vps->getVpsRepFormatIdx( vps->getLayerIdInVps( layerIdCurr ) ) ;
2560    const TComRepFormat* repFormat    = vps->getRepFormat( repFormatIdx ); 
2561
2562    if ( encoder )
2563    {
2564      assert( getChromaFormatIdc() ==  (ChromaFormat) repFormat->getChromaFormatVpsIdc() );         
2565      //// ToDo: add when supported:
2566      // assert( getSeperateColourPlaneFlag( repFormat->getSeparateColourPlaneVpsFlag() ) ;
2567
2568      assert( getPicWidthInLumaSamples()  ==  repFormat->getPicWidthVpsInLumaSamples()  ); 
2569      assert( getPicHeightInLumaSamples() == repFormat->getPicHeightVpsInLumaSamples() ); 
2570
2571      assert( getBitDepth              ( CHANNEL_TYPE_LUMA ) == repFormat->getBitDepthVpsLumaMinus8()   + 8 ); 
2572      assert( getQpBDOffset            ( CHANNEL_TYPE_LUMA ) == (Int) (6*( getBitDepth( CHANNEL_TYPE_LUMA ) - 8 )) );
2573
2574      assert( getBitDepth              ( CHANNEL_TYPE_CHROMA ) == repFormat->getBitDepthVpsChromaMinus8() + 8 ); 
2575      assert( getQpBDOffset            ( CHANNEL_TYPE_CHROMA ) == (Int) (6* ( getBitDepth( CHANNEL_TYPE_CHROMA ) -8 ) ) );
2576    }
2577    else
2578    {
2579      setChromaFormatIdc( (ChromaFormat) repFormat->getChromaFormatVpsIdc() );         
2580      //// ToDo: add when supported:
2581      // setSeperateColourPlaneFlag( repFormat->getSeparateColourPlaneVpsFlag() ) ;
2582
2583      setPicWidthInLumaSamples ( repFormat->getPicWidthVpsInLumaSamples()  ); 
2584      setPicHeightInLumaSamples( repFormat->getPicHeightVpsInLumaSamples() ); 
2585
2586      setBitDepth              ( CHANNEL_TYPE_LUMA, repFormat->getBitDepthVpsLumaMinus8()   + 8 ); 
2587      setQpBDOffset            ( CHANNEL_TYPE_LUMA, (Int) (6*( getBitDepth( CHANNEL_TYPE_LUMA ) - 8 )) );
2588
2589      setBitDepth              ( CHANNEL_TYPE_CHROMA, repFormat->getBitDepthVpsChromaMinus8() + 8 ); 
2590      setQpBDOffset            ( CHANNEL_TYPE_CHROMA, (Int) (6* ( getBitDepth( CHANNEL_TYPE_CHROMA ) -8 ) ) );
2591      Window &spsConf    = getConformanceWindow();   
2592
2593      // Scaled later
2594      spsConf.setScaledFlag( false ); 
2595      spsConf.setWindowLeftOffset  ( repFormat->getConfWinVpsLeftOffset()    );
2596      spsConf.setWindowRightOffset ( repFormat->getConfWinVpsRightOffset()   );
2597      spsConf.setWindowTopOffset   ( repFormat->getConfWinVpsTopOffset()     );
2598      spsConf.setWindowBottomOffset( repFormat->getConfWinVpsBottomOffset()  );           
2599    }
2600
2601   if ( getMultiLayerExtSpsFlag() && getUpdateRepFormatFlag() )
2602    {
2603      assert( getChromaFormatIdc()      <=  repFormat->getChromaFormatVpsIdc()         ); 
2604      //// ToDo: add when supported:
2605      // assert( getSeperateColourPlaneFlag() <=  repFormat->getSeparateColourPlaneVpsFlag() ) ;
2606
2607      assert( getPicWidthInLumaSamples()  <= repFormat->getPicWidthVpsInLumaSamples()    ); 
2608      assert( getPicHeightInLumaSamples() <= repFormat->getPicHeightVpsInLumaSamples()   ); 
2609
2610      assert( getBitDepth( CHANNEL_TYPE_LUMA   )  <= repFormat->getBitDepthVpsLumaMinus8()   + 8 );         
2611      assert( getBitDepth( CHANNEL_TYPE_CHROMA )  <= repFormat->getBitDepthVpsChromaMinus8() + 8 ); 
2612    }
2613  }
2614
2615  // Set conformance window
2616  Int scal = TComSPS::getWinUnitX( getChromaFormatIdc() ) ;
2617  getConformanceWindow().scaleOffsets( scal );
2618  getVuiParameters()->getDefaultDisplayWindow().scaleOffsets( scal );
2619
2620  if (encoder && getMultiLayerExtSpsFlag() )
2621  { 
2622    Int            repFormatIdx = getUpdateRepFormatFlag() ?  getSpsRepFormatIdx() : vps->getVpsRepFormatIdx( vps->getLayerIdInVps( layerIdCurr ) ) ;
2623    const TComRepFormat* repFormat    = vps->getRepFormat( repFormatIdx ); 
2624
2625    Window &spsConf    = getConformanceWindow();   
2626    assert( spsConf.getWindowLeftOffset  () == repFormat->getConfWinVpsLeftOffset()    );
2627    assert( spsConf.getWindowRightOffset () == repFormat->getConfWinVpsRightOffset()   );
2628    assert( spsConf.getWindowTopOffset   () == repFormat->getConfWinVpsTopOffset()     );
2629    assert( spsConf.getWindowBottomOffset() == repFormat->getConfWinVpsBottomOffset()  );           
2630  }
2631}
2632
2633Void TComSPS::inferScalingList( const TComSPS* spsSrc )
2634{
2635  if ( getSpsInferScalingListFlag() ) 
2636  {
2637    assert( spsSrc != NULL ); 
2638    assert( !spsSrc->getSpsInferScalingListFlag() );             
2639    getScalingList().inferFrom( (spsSrc->getScalingList()) ); 
2640  }
2641}
2642
2643Void TComSPS::inferSpsMaxDecPicBufferingMinus1( TComVPS* vps, Int targetOptLayerSetIdx, Int currLayerId, Bool encoder )
2644{
2645  if ( getMultiLayerExtSpsFlag() )
2646  {
2647    const std::vector<Int>& targetDecLayerIdList = vps->getTargetDecLayerIdList( vps->olsIdxToLsIdx( targetOptLayerSetIdx )); 
2648    Int layerIdx = 0;         
2649    while (layerIdx < (Int) targetDecLayerIdList.size() )
2650    {
2651      if ( targetDecLayerIdList[layerIdx] == currLayerId )
2652      { 
2653        break; 
2654      }
2655      layerIdx++; 
2656    }
2657
2658    assert( layerIdx < (Int) targetDecLayerIdList.size() ); 
2659
2660    for (Int i = 0; i <= getSpsMaxSubLayersMinus1(); i++ ) 
2661    {
2662      Int maxDecPicBufferingMinus1 = vps->getDpbSize()->getMaxVpsDecPicBufferingMinus1( targetOptLayerSetIdx, layerIdx, i ) ; 
2663
2664      // This preliminary fix needs to be checked.
2665      Int maxNumReorderPics       = vps->getDpbSize()->getMaxVpsNumReorderPics( targetOptLayerSetIdx, i ); 
2666      Int maxLatencyIncreasePlus1 = vps->getDpbSize()->getMaxVpsLatencyIncreasePlus1( targetOptLayerSetIdx, i ); 
2667      if ( encoder )     
2668      {
2669        assert( getMaxDecPicBuffering( i ) - 1 == maxDecPicBufferingMinus1 ); 
2670        // This preliminary fix needs to be checked.
2671        assert( getNumReorderPics( i )     == maxNumReorderPics       ); 
2672        assert( getSpsMaxLatencyIncreasePlus1( i ) == maxLatencyIncreasePlus1 ); 
2673
2674      }
2675      else
2676      {
2677        // This preliminary fix needs to be checked.
2678        setMaxDecPicBuffering( maxDecPicBufferingMinus1 + 1 , i); 
2679        setNumReorderPics    ( maxNumReorderPics, i );
2680        setSpsMaxLatencyIncreasePlus1( maxLatencyIncreasePlus1 , i); 
2681      }
2682    }   
2683  }
2684}
2685
2686Void TComSPS::checkRpsMaxNumPics( const TComVPS* vps, Int currLayerId ) const
2687{
2688  for (Int i = 0; i < getRPSList()->getNumberOfReferencePictureSets(); i++ )
2689  {
2690    const TComReferencePictureSet* rps = getRPSList()->getReferencePictureSet( i ); 
2691    if ( !rps->getInterRPSPrediction() )
2692    {
2693      rps->checkMaxNumPics( vps->getVpsExtensionFlag(), MAX_INT, getLayerId(), getMaxDecPicBuffering( getSpsMaxSubLayersMinus1() ) - 1 );   // INT_MAX to be replaced by DpbSize
2694    }
2695  }
2696}
2697
2698Void TComSPS::inferSpsMaxSubLayersMinus1(Bool atPsActivation, TComVPS* vps)
2699{
2700  assert( getLayerId() != 0 ); 
2701  if ( !atPsActivation   )
2702  {
2703    assert( vps == NULL );
2704    if (getSpsExtOrMaxSubLayersMinus1() != 7)
2705    {
2706      setSpsMaxSubLayersMinus1( getSpsExtOrMaxSubLayersMinus1() );
2707    }
2708  }
2709  else
2710  {
2711    assert( vps != NULL );
2712    if (getSpsExtOrMaxSubLayersMinus1() == 7)
2713    {
2714      setSpsMaxSubLayersMinus1( vps->getMaxSubLayersMinus1() );
2715    }
2716  }
2717}
2718#endif
2719
2720
2721TComReferencePictureSet::TComReferencePictureSet()
2722: m_numberOfPictures (0)
2723, m_numberOfNegativePictures (0)
2724, m_numberOfPositivePictures (0)
2725, m_numberOfLongtermPictures (0)
2726, m_interRPSPrediction (0)
2727, m_deltaRIdxMinus1 (0)
2728, m_deltaRPS (0)
2729, m_numRefIdc (0)
2730{
2731  ::memset( m_deltaPOC, 0, sizeof(m_deltaPOC) );
2732  ::memset( m_POC, 0, sizeof(m_POC) );
2733  ::memset( m_used, 0, sizeof(m_used) );
2734  ::memset( m_refIdc, 0, sizeof(m_refIdc) );
2735  ::memset( m_bCheckLTMSB, 0, sizeof(m_bCheckLTMSB) );
2736  ::memset( m_pocLSBLT, 0, sizeof(m_pocLSBLT) );
2737  ::memset( m_deltaPOCMSBCycleLT, 0, sizeof(m_deltaPOCMSBCycleLT) );
2738  ::memset( m_deltaPocMSBPresentFlag, 0, sizeof(m_deltaPocMSBPresentFlag) );
2739}
2740
2741TComReferencePictureSet::~TComReferencePictureSet()
2742{
2743}
2744
2745Void TComReferencePictureSet::setUsed(Int bufferNum, Bool used)
2746{
2747  m_used[bufferNum] = used;
2748}
2749
2750Void TComReferencePictureSet::setDeltaPOC(Int bufferNum, Int deltaPOC)
2751{
2752  m_deltaPOC[bufferNum] = deltaPOC;
2753}
2754
2755Void TComReferencePictureSet::setNumberOfPictures(Int numberOfPictures)
2756{
2757  m_numberOfPictures = numberOfPictures;
2758}
2759
2760Int TComReferencePictureSet::getUsed(Int bufferNum) const
2761{
2762  return m_used[bufferNum];
2763}
2764
2765Int TComReferencePictureSet::getDeltaPOC(Int bufferNum) const
2766{
2767  return m_deltaPOC[bufferNum];
2768}
2769
2770Int TComReferencePictureSet::getNumberOfPictures() const
2771{
2772  return m_numberOfPictures;
2773}
2774
2775Int TComReferencePictureSet::getPOC(Int bufferNum) const
2776{
2777  return m_POC[bufferNum];
2778}
2779
2780Void TComReferencePictureSet::setPOC(Int bufferNum, Int POC)
2781{
2782  m_POC[bufferNum] = POC;
2783}
2784
2785Bool TComReferencePictureSet::getCheckLTMSBPresent(Int bufferNum) const
2786{
2787  return m_bCheckLTMSB[bufferNum];
2788}
2789
2790Void TComReferencePictureSet::setCheckLTMSBPresent(Int bufferNum, Bool b)
2791{
2792  m_bCheckLTMSB[bufferNum] = b;
2793}
2794
2795//! set the reference idc value at uiBufferNum entry to the value of iRefIdc
2796Void TComReferencePictureSet::setRefIdc(Int bufferNum, Int refIdc)
2797{
2798  m_refIdc[bufferNum] = refIdc;
2799}
2800
2801//! get the reference idc value at uiBufferNum
2802Int  TComReferencePictureSet::getRefIdc(Int bufferNum) const
2803{
2804  return m_refIdc[bufferNum];
2805}
2806
2807/** Sorts the deltaPOC and Used by current values in the RPS based on the deltaPOC values.
2808 *  deltaPOC values are sorted with -ve values before the +ve values.  -ve values are in decreasing order.
2809 *  +ve values are in increasing order.
2810 * \returns Void
2811 */
2812Void TComReferencePictureSet::sortDeltaPOC()
2813{
2814  // sort in increasing order (smallest first)
2815  for(Int j=1; j < getNumberOfPictures(); j++)
2816  {
2817    Int deltaPOC = getDeltaPOC(j);
2818    Bool used = getUsed(j);
2819    for (Int k=j-1; k >= 0; k--)
2820    {
2821      Int temp = getDeltaPOC(k);
2822      if (deltaPOC < temp)
2823      {
2824        setDeltaPOC(k+1, temp);
2825        setUsed(k+1, getUsed(k));
2826        setDeltaPOC(k, deltaPOC);
2827        setUsed(k, used);
2828      }
2829    }
2830  }
2831  // flip the negative values to largest first
2832  Int numNegPics = getNumberOfNegativePictures();
2833  for(Int j=0, k=numNegPics-1; j < numNegPics>>1; j++, k--)
2834  {
2835    Int deltaPOC = getDeltaPOC(j);
2836    Bool used = getUsed(j);
2837    setDeltaPOC(j, getDeltaPOC(k));
2838    setUsed(j, getUsed(k));
2839    setDeltaPOC(k, deltaPOC);
2840    setUsed(k, used);
2841  }
2842}
2843
2844/** Prints the deltaPOC and RefIdc (if available) values in the RPS.
2845 *  A "*" is added to the deltaPOC value if it is Used bu current.
2846 * \returns Void
2847 */
2848Void TComReferencePictureSet::printDeltaPOC() const
2849{
2850  printf("DeltaPOC = { ");
2851  for(Int j=0; j < getNumberOfPictures(); j++)
2852  {
2853    printf("%d%s ", getDeltaPOC(j), (getUsed(j)==1)?"*":"");
2854  }
2855  if (getInterRPSPrediction())
2856  {
2857    printf("}, RefIdc = { ");
2858    for(Int j=0; j < getNumRefIdc(); j++)
2859    {
2860      printf("%d ", getRefIdc(j));
2861    }
2862  }
2863  printf("}\n");
2864}
2865
2866#if NH_MV
2867Void TComReferencePictureSet::checkMaxNumPics( Bool vpsExtensionFlag, Int maxNumPics, Int nuhLayerId, Int spsMaxDecPicBufferingMinus1 ) const
2868{
2869  assert( getNumberOfPictures() >= 0 ); 
2870  if ( nuhLayerId == 0 )
2871  {
2872    assert( getNumberOfPictures() <= spsMaxDecPicBufferingMinus1 ); 
2873  }
2874
2875  if ( vpsExtensionFlag )
2876  {
2877    assert( getNumberOfPictures() <= maxNumPics );
2878  }
2879}
2880#endif
2881
2882
2883
2884
2885TComRefPicListModification::TComRefPicListModification()
2886: m_refPicListModificationFlagL0 (false)
2887, m_refPicListModificationFlagL1 (false)
2888{
2889  ::memset( m_RefPicSetIdxL0, 0, sizeof(m_RefPicSetIdxL0) );
2890  ::memset( m_RefPicSetIdxL1, 0, sizeof(m_RefPicSetIdxL1) );
2891}
2892
2893TComRefPicListModification::~TComRefPicListModification()
2894{
2895}
2896
2897TComScalingList::TComScalingList()
2898{
2899  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
2900  {
2901    for(UInt listId = 0; listId < SCALING_LIST_NUM; listId++)
2902    {
2903      m_scalingListCoef[sizeId][listId].resize(min<Int>(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId]));
2904    }
2905  }
2906}
2907
2908/** set default quantization matrix to array
2909*/
2910Void TComScalingList::setDefaultScalingList()
2911{
2912  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
2913  {
2914    for(UInt listId=0;listId<SCALING_LIST_NUM;listId++)
2915    {
2916      processDefaultMatrix(sizeId, listId);
2917    }
2918  }
2919}
2920/** check if use default quantization matrix
2921 * \returns true if use default quantization matrix in all size
2922*/
2923Bool TComScalingList::checkDefaultScalingList()
2924{
2925  UInt defaultCounter=0;
2926
2927  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
2928  {
2929    for(UInt listId=0;listId<SCALING_LIST_NUM;listId++)
2930    {
2931      if( !memcmp(getScalingListAddress(sizeId,listId), getScalingListDefaultAddress(sizeId, listId),sizeof(Int)*min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId])) // check value of matrix
2932     && ((sizeId < SCALING_LIST_16x16) || (getScalingListDC(sizeId,listId) == 16))) // check DC value
2933      {
2934        defaultCounter++;
2935      }
2936    }
2937  }
2938
2939  return (defaultCounter == (SCALING_LIST_NUM * SCALING_LIST_SIZE_NUM )) ? false : true;
2940}
2941
2942#if NH_MV
2943Void TComSlice::createInterLayerReferencePictureSet( TComPicLists* ivPicLists, std::vector<TComPic*>& refPicSetInterLayer0, std::vector<TComPic*>& refPicSetInterLayer1 )
2944{
2945  refPicSetInterLayer0.clear(); 
2946  refPicSetInterLayer1.clear(); 
2947
2948  for( Int i = 0; i < getNumActiveRefLayerPics(); i++ ) 
2949  {
2950    Int layerIdRef = getRefPicLayerId( i ); 
2951    TComPic* picRef = ivPicLists->getPic( layerIdRef, getPOC() ) ; 
2952    assert ( picRef != 0 ); // There shall be no entry equal to "no reference picture" in RefPicSetInterLayer0 or RefPicSetInterLayer1.
2953
2954    picRef->getPicYuvRec()->extendPicBorder(); 
2955    picRef->setIsLongTerm( true );       
2956    picRef->getSlice(0)->setReferenced( true );       
2957
2958    Int viewIdCur  = getVPS()->getViewId( getLayerId() ); 
2959    Int viewIdZero = getVPS()->getViewId( 0 );
2960    Int viewIdRef  = getVPS()->getViewId( layerIdRef ); 
2961
2962    if (  ( viewIdCur <= viewIdZero && viewIdCur <= viewIdRef ) || ( viewIdCur >= viewIdZero && viewIdCur >= viewIdRef ) )
2963    {
2964      refPicSetInterLayer0.push_back( picRef ); 
2965    }
2966    else
2967    {
2968      refPicSetInterLayer1.push_back( picRef ); 
2969    }
2970    // Consider to check here:
2971    // "If the current picture is a RADL picture, there shall be no entry in the RefPicSetInterLayer0 and RefPicSetInterLayer1 that is a RASL picture. "   
2972    assert( picRef->getSlice(0)->getDiscardableFlag() == false ); // "There shall be no picture that has discardable_flag equal to 1 in RefPicSetInterLayer0 or RefPicSetInterLayer1".       
2973  }
2974}
2975Void TComSlice::markIvRefPicsAsShortTerm( std::vector<TComPic*> refPicSetInterLayer0, std::vector<TComPic*> refPicSetInterLayer1 )
2976{
2977  // Mark as short-term
2978  for ( Int i = 0; i < refPicSetInterLayer0.size(); i++ ) 
2979  {
2980    refPicSetInterLayer0[i]->setIsLongTerm( false ); 
2981  }
2982
2983  for ( Int i = 0; i < refPicSetInterLayer1.size(); i++ ) 
2984  {
2985    refPicSetInterLayer1[i]->setIsLongTerm( false ); 
2986  }
2987
2988}
2989
2990Void TComSlice::printRefPicList()
2991{ 
2992  for ( Int li = 0; li < 2; li++)
2993  {   
2994    std::cout << std::endl << "RefPicListL" <<  li << ":" << std::endl; 
2995    for (Int rIdx = 0; rIdx <= (m_aiNumRefIdx[li]-1); rIdx ++)
2996    {     
2997      if (rIdx == 0 && li == 0) m_apcRefPicList[li][rIdx]->print( 1 );
2998      m_apcRefPicList[li][rIdx]->print( 0 );     
2999       
3000    }
3001  }
3002}
3003
3004Void TComSlice::markCurrPic( TComPic* currPic )
3005{
3006  currPic->getSlice(0)->setReferenced( true ) ; 
3007  currPic->setIsLongTerm( false ); 
3008
3009  currPic->setReconMark( true );
3010  currPic->setPicOutputFlag( currPic->getSlice(0)->getPicOutputFlag() );
3011}
3012
3013Void TComSlice::setRefPicSetInterLayer( std::vector<TComPic*>* refPicSetInterLayer0, std::vector<TComPic*>* refPicSetInterLayer1 )
3014{
3015  m_refPicSetInterLayer0 = refPicSetInterLayer0; 
3016  m_refPicSetInterLayer1 = refPicSetInterLayer1; 
3017}
3018
3019TComPic* TComSlice::getRefPicSetInterLayer( Int setIdc, Int i ) const
3020{
3021  TComPic* pic = NULL; 
3022  if (setIdc == 0 )
3023  {
3024    pic = (*m_refPicSetInterLayer0)[ i ]; 
3025  }
3026  else if (setIdc == 1 )
3027  {
3028    pic = (*m_refPicSetInterLayer1)[ i ]; 
3029  }
3030
3031  assert( pic != NULL );   
3032
3033  return pic; 
3034}
3035
3036
3037TComPic* TComSlice::getPicFromRefPicSetInterLayer(Int setIdc, Int layerId ) const
3038{
3039  assert ( setIdc == 0 || setIdc == 1);   
3040  std::vector<TComPic*>* refPicSetInterLayer = ( setIdc == 0 ? m_refPicSetInterLayer0 : m_refPicSetInterLayer1);   
3041  assert( refPicSetInterLayer != 0 ); 
3042 
3043  TComPic* pcPic = NULL; 
3044  for ( Int i = 0; i < (*refPicSetInterLayer).size(); i++ )
3045  {
3046    if ((*refPicSetInterLayer)[ i ]->getLayerId() == layerId )
3047    {
3048      pcPic = (*refPicSetInterLayer)[ i ]; 
3049    }
3050  }
3051
3052  assert(pcPic != NULL); 
3053  return pcPic;
3054}
3055
3056
3057Int  TComSlice::getRefLayerPicFlag( Int i ) const
3058{
3059  const TComVPS* vps = getVPS(); 
3060  Int refLayerIdx = vps->getLayerIdInVps( vps->getIdDirectRefLayer( getLayerId(), i ) ); 
3061
3062  Bool refLayerPicFlag = ( vps->getSubLayersVpsMaxMinus1( refLayerIdx ) >=  getTLayer()  && ( getTLayer() == 0   ||
3063    vps->getMaxTidIlRefPicsPlus1( refLayerIdx, vps->getLayerIdInVps( getLayerId() )) > getTLayer() )); 
3064  return refLayerPicFlag;       
3065}   
3066
3067Int TComSlice::getRefLayerPicIdc( Int j ) const
3068{ 
3069  Int refLayerPicIdc = -1; 
3070  Int curj = 0; 
3071  for( Int i = 0;  i < getVPS()->getNumDirectRefLayers( getLayerId()) ; i++ )
3072  {
3073    if( getRefLayerPicFlag( i ) )
3074    {
3075      if ( curj == j ) 
3076      {
3077        refLayerPicIdc = i;         
3078        break;
3079      }
3080      curj++; 
3081    }
3082  }
3083
3084  assert( curj == j ); 
3085  assert( refLayerPicIdc != -1 ); 
3086  return refLayerPicIdc; 
3087}
3088
3089Int  TComSlice::getNumRefLayerPics( ) const
3090{ 
3091  Int numRefLayerPics = 0; 
3092  for( Int i = 0;  i < getVPS()->getNumDirectRefLayers( getLayerId()) ; i++ )
3093  {
3094    numRefLayerPics += getRefLayerPicFlag( i ); 
3095  }
3096  return numRefLayerPics; 
3097}
3098
3099
3100
3101Int TComSlice::getNumActiveRefLayerPics() const
3102{
3103  Int numActiveRefLayerPics; 
3104
3105  if( getLayerId() == 0 || getNumRefLayerPics() ==  0 )
3106  {
3107    numActiveRefLayerPics = 0; 
3108  }
3109  else if (getVPS()->getAllRefLayersActiveFlag() )
3110  {
3111    numActiveRefLayerPics = getNumRefLayerPics(); 
3112  }
3113  else if ( !getInterLayerPredEnabledFlag() )
3114  {
3115    numActiveRefLayerPics = 0; 
3116  }
3117  else if( getVPS()->getMaxOneActiveRefLayerFlag() || getVPS()->getNumDirectRefLayers( getLayerId() ) == 1 )
3118  {
3119    numActiveRefLayerPics = 1; 
3120  }
3121  else
3122  {
3123    numActiveRefLayerPics = getNumInterLayerRefPicsMinus1() + 1; 
3124  }
3125  return numActiveRefLayerPics;
3126}
3127
3128Int TComSlice::getRefPicLayerId( Int i ) const
3129{
3130  return getVPS()->getIdDirectRefLayer( getLayerId(), getInterLayerPredLayerIdc( i ) );
3131}
3132#endif
3133
3134
3135
3136
3137
3138#if NH_MV
3139Void TComSlice::checkCrossLayerBlaFlag() const
3140{
3141  // 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.
3142  if ( getLayerId() != 0 || getNalUnitType() != NAL_UNIT_CODED_SLICE_IDR_W_RADL || getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_N_LP )
3143  {
3144    assert( m_crossLayerBlaFlag == 0 ); 
3145  }
3146}
3147
3148Bool TComSlice::inferPocMsbCycleValPresentFlag()
3149{
3150  Bool pocMsbValPresentFlag; 
3151  if( getSliceSegmentHeaderExtensionLength() == 0 ) 
3152  {
3153    pocMsbValPresentFlag = false; 
3154  }
3155  else if ( getPocMsbValRequiredFlag() )
3156  {
3157    pocMsbValPresentFlag = true; 
3158  }
3159  else
3160  {
3161    pocMsbValPresentFlag = false; 
3162  }
3163
3164  return pocMsbValPresentFlag;
3165}
3166
3167
3168Void TComSlice::f834decProcForRefPicListConst()
3169{
3170  // This process is invoked at the beginning of the decoding process for each P or B slice.
3171  assert( getSliceType() == B_SLICE || getSliceType() == P_SLICE );
3172
3173  // Reference pictures are addressed through reference indices as specified in clause 8.5.3.3.2. A reference index is an index into
3174  // a reference picture list. When decoding a P slice, there is a single reference picture list RefPicList0. When decoding a B
3175  // slice, there is a second independent reference picture list RefPicList1 in addition to RefPicList0.
3176
3177  // At the beginning of the decoding process for each slice, the reference picture lists RefPicList0 and, for B slices, RefPicList1
3178  // are derived as follows:
3179
3180  // The variable NumRpsCurrTempList0 is set equal to Max( num_ref_idx_l0_active_minus1 + 1, NumPicTotalCurr )
3181  Int numRpsCurrTempList0 = std::max( getNumRefIdxL0ActiveMinus1() + 1, getNumPicTotalCurr() );
3182
3183  // and the list RefPicListTemp0 is constructed as follows:
3184  std::vector<TComPic*> refPicListTemp0;
3185  refPicListTemp0.resize((MAX_NUM_REF+1),NULL);
3186
3187  const TComDecodedRps* decRps = getPic()->getDecodedRps(); 
3188
3189  const std::vector<TComPic*>& refPicSetStCurrBefore  = decRps->m_refPicSetStCurrBefore; 
3190  const std::vector<TComPic*>& refPicSetStCurrAfter   = decRps->m_refPicSetStCurrAfter;
3191  const std::vector<TComPic*>& refPicSetLtCurr        = decRps->m_refPicSetLtCurr; 
3192
3193  const Int                    numPocStCurrBefore     = decRps->m_numPocStCurrBefore; 
3194  const Int                    numPocStCurrAfter      = decRps->m_numPocStCurrAfter;
3195  const Int                    numPocLtCurr           = decRps->m_numPocLtCurr;
3196
3197  const Int                    numActiveRefLayerPics0 = decRps->m_numActiveRefLayerPics0;
3198  const Int                    numActiveRefLayerPics1 = decRps->m_numActiveRefLayerPics1;
3199
3200  const std::vector<TComPic*>& refPicSetInterLayer0   = decRps->m_refPicSetInterLayer0;
3201  const std::vector<TComPic*>& refPicSetInterLayer1   = decRps->m_refPicSetInterLayer1;
3202
3203  Int rIdx = 0;
3204  while( rIdx < numRpsCurrTempList0 )
3205  {
3206    for(Int  i = 0; i < numPocStCurrBefore  &&  rIdx < numRpsCurrTempList0; rIdx++, i++ )
3207    {
3208      refPicListTemp0[ rIdx ] = refPicSetStCurrBefore[ i ];
3209    }   
3210
3211    for(Int  i = 0; i < numActiveRefLayerPics0; rIdx++, i++ )
3212    {
3213      refPicListTemp0[ rIdx ] = refPicSetInterLayer0[ i ];
3214    }
3215
3216    for(Int  i = 0;  i < numPocStCurrAfter  &&  rIdx < numRpsCurrTempList0; rIdx++, i++ )  // (F 65)
3217    {
3218      refPicListTemp0[ rIdx ] = refPicSetStCurrAfter[ i ];
3219    }
3220
3221    for(Int  i = 0; i < numPocLtCurr  &&  rIdx < numRpsCurrTempList0; rIdx++, i++ )
3222    {
3223      refPicListTemp0[ rIdx ] = refPicSetLtCurr[ i ]; 
3224    }
3225
3226    for(Int  i = 0; i < numActiveRefLayerPics1; rIdx++, i++ )
3227    {
3228      refPicListTemp0[ rIdx ] = refPicSetInterLayer1[ i ];
3229    }
3230  }
3231
3232  // The list RefPicList0 is constructed as follows:
3233  TComRefPicListModification* rplm  = getRefPicListModification(); 
3234  for( rIdx = 0; rIdx  <=  getNumRefIdxL0ActiveMinus1(); rIdx++ )      //  (F 66)
3235  {
3236    m_apcRefPicList[ 0 ][ rIdx ] = rplm->getRefPicListModificationFlagL0( ) ? refPicListTemp0[ rplm->getListEntryL0( rIdx )] : refPicListTemp0[ rIdx ];
3237    // The decoding process below slice level requires the status
3238    // of the reference pictures, when decoding the RPS. So store it here.
3239    m_bIsUsedAsLongTerm[ 0 ][ rIdx ] = m_apcRefPicList[ 0 ][ rIdx ]->getIsLongTerm();
3240    m_aiRefPOCList     [ 0 ][ rIdx ] = m_apcRefPicList[ 0 ][ rIdx ]->getPOC();
3241    m_aiRefLayerIdList [ 0 ][ rIdx ] = m_apcRefPicList[ 0 ][ rIdx ]->getLayerId();
3242  } 
3243
3244  std::vector<TComPic*> refPicListTemp1;
3245  refPicListTemp1.resize((MAX_NUM_REF+1),NULL);
3246
3247  if (getSliceType() == B_SLICE )
3248  {
3249    // When the slice is a B slice, the variable NumRpsCurrTempList1 is set equal to
3250    // Max( num_ref_idx_l1_active_minus1 + 1, NumPicTotalCurr ) and the list RefPicListTemp1 is constructed as follows:       
3251    Int numRpsCurrTempList1 = std::max( getNumRefIdxL1ActiveMinus1() + 1, getNumPicTotalCurr() );
3252
3253    rIdx = 0; 
3254    while( rIdx < numRpsCurrTempList1 )
3255    {
3256      for( Int i = 0; i < numPocStCurrAfter  &&  rIdx < numRpsCurrTempList1; rIdx++, i++ )
3257      {
3258        refPicListTemp1[ rIdx ] = refPicSetStCurrAfter[ i ]; 
3259      }
3260      for( Int i = 0; i< numActiveRefLayerPics1; rIdx++, i++ )
3261      {
3262        refPicListTemp1[ rIdx ] = refPicSetInterLayer1[ i ]; 
3263      }
3264      for( Int i = 0;  i < numPocStCurrBefore  &&  rIdx < numRpsCurrTempList1; rIdx++, i++ )  // (F 67)
3265      {
3266        refPicListTemp1[ rIdx ] = refPicSetStCurrBefore[ i ];
3267      }         
3268      for( Int i = 0; i < numPocLtCurr  &&  rIdx < numRpsCurrTempList1; rIdx++, i++ )
3269      {
3270        refPicListTemp1[ rIdx ] = refPicSetLtCurr[ i ];
3271      }
3272      for( Int i = 0; i< numActiveRefLayerPics0; rIdx++, i++ )
3273      {
3274        refPicListTemp1[ rIdx ] = refPicSetInterLayer0[ i ]; 
3275      }
3276    }
3277  }
3278
3279  if (getSliceType() == B_SLICE )
3280  {
3281    //   When the slice is a B slice, the list RefPicList1 is constructed as follows:
3282    for( rIdx = 0; rIdx  <=  getNumRefIdxL1ActiveMinus1(); rIdx++ )      // (F 68)
3283    {
3284      m_apcRefPicList[ 1 ][ rIdx ] = rplm->getRefPicListModificationFlagL1() ? refPicListTemp1[ rplm->getListEntryL1( rIdx ) ] : refPicListTemp1[ rIdx ]; 
3285
3286      // The decoding process below slice level requires the marking status
3287      // of the reference pictures, when decoding the RPS. So store it here.
3288      m_bIsUsedAsLongTerm[ 1 ][ rIdx ] = m_apcRefPicList[ 1 ][ rIdx ]->getIsLongTerm();
3289      m_aiRefPOCList     [ 1 ][ rIdx ] = m_apcRefPicList[ 1 ][ rIdx ]->getPOC();
3290      m_aiRefLayerIdList [ 1 ][ rIdx ] = m_apcRefPicList[ 1 ][ rIdx ]->getLayerId();
3291    }   
3292  }
3293}
3294
3295
3296Void TComSlice::cl834DecProcForRefPicListConst()
3297{
3298  // This process is invoked at the beginning of the decoding process for each P or B slice.
3299  assert( getSliceType() == B_SLICE || getSliceType() == P_SLICE );
3300
3301  // Reference pictures are addressed through reference indices as specified in clause 8.5.3.3.2. A reference index is an index into
3302  // a reference picture list. When decoding a P slice, there is a single reference picture list RefPicList0. When decoding a B
3303  // slice, there is a second independent reference picture list RefPicList1 in addition to RefPicList0.
3304
3305  // At the beginning of the decoding process for each slice, the reference picture lists RefPicList0 and, for B slices, RefPicList1
3306  // are derived as follows:
3307
3308  // The variable NumRpsCurrTempList0 is set equal to Max( num_ref_idx_l0_active_minus1 + 1, NumPicTotalCurr )
3309  Int numRpsCurrTempList0 = std::max( getNumRefIdxL0ActiveMinus1() + 1, getNumPicTotalCurr() );
3310
3311  // and the list RefPicListTemp0 is constructed as follows:
3312  std::vector<TComPic*> refPicListTemp0; 
3313  refPicListTemp0.resize((MAX_NUM_REF+1),NULL);
3314
3315  const TComDecodedRps* decRps = getPic()->getDecodedRps(); 
3316
3317  const std::vector<TComPic*>& refPicSetStCurrBefore = decRps->m_refPicSetStCurrBefore; 
3318  const std::vector<TComPic*>& refPicSetStCurrAfter  = decRps->m_refPicSetStCurrAfter;
3319  const std::vector<TComPic*>& refPicSetLtCurr       = decRps->m_refPicSetLtCurr; 
3320   
3321  const Int                    numPocStCurrBefore    = decRps->m_numPocStCurrBefore; 
3322  const Int                    numPocStCurrAfter     = decRps->m_numPocStCurrAfter;
3323  const Int                    numPocLtCurr          = decRps->m_numPocLtCurr;
3324
3325  Int rIdx = 0;
3326  while( rIdx < numRpsCurrTempList0 )
3327  {
3328    for(Int  i = 0; i < numPocStCurrBefore  &&  rIdx < numRpsCurrTempList0; rIdx++, i++ )
3329    {
3330      refPicListTemp0[ rIdx ] = refPicSetStCurrBefore[ i ];
3331    }   
3332
3333    for(Int  i = 0;  i < numPocStCurrAfter  &&  rIdx < numRpsCurrTempList0; rIdx++, i++ )  // (8 8)
3334    {
3335      refPicListTemp0[ rIdx ] = refPicSetStCurrAfter[ i ];
3336    }
3337
3338    for(Int  i = 0; i < numPocLtCurr  &&  rIdx < numRpsCurrTempList0; rIdx++, i++ )
3339    {
3340      refPicListTemp0[ rIdx ] = refPicSetLtCurr[ i ]; 
3341    }
3342  }
3343
3344  // The list RefPicList0 is constructed as follows:
3345
3346  TComRefPicListModification* rplm = getRefPicListModification(); 
3347  for( rIdx = 0; rIdx  <=  getNumRefIdxL0ActiveMinus1(); rIdx++ )      //   (8-9)
3348  {
3349    m_apcRefPicList[0][ rIdx ] = rplm->getRefPicListModificationFlagL0( ) ? refPicListTemp0[ rplm->getListEntryL0( rIdx )] : refPicListTemp0[ rIdx ];
3350
3351    // The decoding process below slice level requires the marking status
3352    // of the reference pictures, when decoding the RPS. So store it here.
3353    m_bIsUsedAsLongTerm[ 0 ][ rIdx ] = m_apcRefPicList[ 0 ][ rIdx ]->getIsLongTerm();
3354    m_aiRefPOCList     [ 0 ][ rIdx ] = m_apcRefPicList[ 0 ][ rIdx ]->getPOC();
3355    m_aiRefLayerIdList [ 0 ][ rIdx ] = m_apcRefPicList[ 0 ][ rIdx ]->getLayerId();
3356  }
3357
3358  std::vector<TComPic*> refPicListTemp1;
3359  refPicListTemp1.resize((MAX_NUM_REF+1),NULL);
3360
3361  if (getSliceType() == B_SLICE )
3362  {
3363    // When the slice is a B slice, the variable NumRpsCurrTempList1 is set equal to
3364    // Max( num_ref_idx_l1_active_minus1 + 1, NumPicTotalCurr ) and the list RefPicListTemp1 is constructed as follows:
3365    Int numRpsCurrTempList1 = std::max( getNumRefIdxL1ActiveMinus1() + 1, getNumPicTotalCurr() );
3366
3367    rIdx = 0; 
3368    while( rIdx < numRpsCurrTempList1 )
3369    {
3370      for( Int i = 0; i < numPocStCurrAfter  &&  rIdx < numRpsCurrTempList1; rIdx++, i++ )
3371      {
3372        refPicListTemp1[ rIdx ] = refPicSetStCurrAfter[ i ]; 
3373      }
3374      for( Int i = 0;  i < numPocStCurrBefore  &&  rIdx < numRpsCurrTempList1; rIdx++, i++ )  // (8-10)
3375      {
3376        refPicListTemp1[ rIdx ] = refPicSetStCurrBefore[ i ];
3377      }         
3378      for( Int i = 0; i < numPocLtCurr  &&  rIdx < numRpsCurrTempList1; rIdx++, i++ )
3379      {
3380        refPicListTemp1[ rIdx ] = refPicSetLtCurr[ i ];
3381      }
3382    }
3383  }
3384
3385  if (getSliceType() == B_SLICE )
3386  {
3387    //   When the slice is a B slice, the list RefPicList1 is constructed as follows:
3388    for( rIdx = 0; rIdx  <=  getNumRefIdxL1ActiveMinus1(); rIdx++ )      // (F 68)
3389    {
3390      m_apcRefPicList[ 1 ][ rIdx ] = rplm->getRefPicListModificationFlagL1() ? refPicListTemp1[ rplm->getListEntryL1( rIdx ) ] : refPicListTemp1[ rIdx ]; 
3391
3392      // The decoding process below slice level requires the marking status
3393      // of the reference pictures, when decoding the RPS. So store it here.
3394      m_bIsUsedAsLongTerm[ 1 ][ rIdx ] = m_apcRefPicList[ 1 ][ rIdx ]->getIsLongTerm();
3395      m_aiRefPOCList     [ 1 ][ rIdx ] = m_apcRefPicList[ 1 ][ rIdx ]->getPOC();
3396      m_aiRefLayerIdList [ 1 ][ rIdx ] = m_apcRefPicList[ 1 ][ rIdx ]->getLayerId();
3397    }
3398  }
3399}
3400
3401Int TComSlice::getNumPicTotalCurr() const
3402{
3403  Int numPicTotalCurr = 0;
3404#if NH_MV_FIX_NUM_POC_TOTAL_CUR
3405  if ( !isIdr()  )
3406  {
3407    const TComStRefPicSet* stRps = getStRps( getCurrRpsIdx() ); 
3408#endif
3409    for( Int i = 0; i < stRps->getNumNegativePicsVar(); i++ )
3410    {
3411      if( stRps->getUsedByCurrPicS0Var( i ) )
3412      {
3413        numPicTotalCurr++;
3414      }
3415    }
3416    for( Int i = 0; i < stRps->getNumPositivePicsVar(); i++)  //(7 55)
3417    {
3418      if( stRps->getUsedByCurrPicS1Var(i) )
3419      {
3420        numPicTotalCurr++;
3421      }
3422    }
3423    for( Int i = 0; i < getNumLongTermSps() + getNumLongTermPics(); i++ )
3424    {
3425      if( getUsedByCurrPicLtVar( i ) )
3426      {
3427        numPicTotalCurr++;
3428      }
3429    }
3430#if NH_MV_FIX_NUM_POC_TOTAL_CUR
3431  }
3432#endif
3433
3434  if ( decProcAnnexF() )
3435  {
3436    numPicTotalCurr += getNumActiveRefLayerPics(); 
3437  }
3438  return numPicTotalCurr;
3439}
3440
3441
3442
3443Int TComSlice::getPocLsbLtVar( Int i )
3444{
3445  Int pocLsbLtVar; 
3446  if (i < getNumLongTermSps() )
3447  {
3448
3449    pocLsbLtVar = getSPS()->getLtRefPicPocLsbSps( getLtIdxSps( i ) ); 
3450  }
3451  else
3452  {
3453    pocLsbLtVar = getPocLsbLt( i ); 
3454  }
3455  return pocLsbLtVar;
3456}
3457
3458
3459Bool TComSlice::getUsedByCurrPicLtVar( Int i ) const
3460{
3461  Bool usedByCurrPicLtVar; 
3462  if (i < getNumLongTermSps() )
3463  {
3464    usedByCurrPicLtVar = getSPS()->getUsedByCurrPicLtSPSFlag( getLtIdxSps( i ) ); 
3465  }
3466  else
3467  {
3468    usedByCurrPicLtVar = getUsedByCurrPicLtFlag( i ); 
3469  }
3470  return usedByCurrPicLtVar;
3471}
3472
3473
3474Int TComSlice::getDeltaPocMsbCycleLtVar( Int i ) const
3475{
3476  Int deltaPocMsbCycleVar; 
3477  if (i == 0 || i == getNumLongTermSps() )
3478  {
3479    deltaPocMsbCycleVar = getDeltaPocMsbCycleLt( i ); 
3480  }
3481  else
3482  {
3483    deltaPocMsbCycleVar = getDeltaPocMsbCycleLt( i ) + getDeltaPocMsbCycleLtVar( i - 1 ); 
3484  }
3485  return deltaPocMsbCycleVar;
3486}
3487
3488#endif
3489
3490
3491
3492/** get scaling matrix from RefMatrixID
3493 * \param sizeId    size index
3494 * \param listId    index of input matrix
3495 * \param refListId index of reference matrix
3496 */
3497Void TComScalingList::processRefMatrix( UInt sizeId, UInt listId , UInt refListId )
3498{
3499  ::memcpy(getScalingListAddress(sizeId, listId),((listId == refListId)? getScalingListDefaultAddress(sizeId, refListId): getScalingListAddress(sizeId, refListId)),sizeof(Int)*min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId]));
3500}
3501
3502Void TComScalingList::checkPredMode(UInt sizeId, UInt listId)
3503{
3504  Int predListStep = (sizeId == SCALING_LIST_32x32? (SCALING_LIST_NUM/NUMBER_OF_PREDICTION_MODES) : 1); // if 32x32, skip over chroma entries.
3505
3506  for(Int predListIdx = (Int)listId ; predListIdx >= 0; predListIdx-=predListStep)
3507  {
3508    if( !memcmp(getScalingListAddress(sizeId,listId),((listId == predListIdx) ?
3509      getScalingListDefaultAddress(sizeId, predListIdx): getScalingListAddress(sizeId, predListIdx)),sizeof(Int)*min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId])) // check value of matrix
3510     && ((sizeId < SCALING_LIST_16x16) || (getScalingListDC(sizeId,listId) == getScalingListDC(sizeId,predListIdx)))) // check DC value
3511    {
3512      setRefMatrixId(sizeId, listId, predListIdx);
3513      setScalingListPredModeFlag(sizeId, listId, false);
3514      return;
3515    }
3516  }
3517  setScalingListPredModeFlag(sizeId, listId, true);
3518}
3519
3520static Void outputScalingListHelp(std::ostream &os)
3521{
3522  os << "The scaling list file specifies all matrices and their DC values; none can be missing,\n"
3523         "but their order is arbitrary.\n\n"
3524         "The matrices are specified by:\n"
3525         "<matrix name><unchecked data>\n"
3526         "  <value>,<value>,<value>,....\n\n"
3527         "  Line-feeds can be added arbitrarily between values, and the number of values needs to be\n"
3528         "  at least the number of entries for the matrix (superfluous entries are ignored).\n"
3529         "  The <unchecked data> is text on the same line as the matrix that is not checked\n"
3530         "  except to ensure that the matrix name token is unique. It is recommended that it is ' ='\n"
3531         "  The values in the matrices are the absolute values (0-255), not the delta values as\n"
3532         "  exchanged between the encoder and decoder\n\n"
3533         "The DC values (for matrix sizes larger than 8x8) are specified by:\n"
3534         "<matrix name>_DC<unchecked data>\n"
3535         "  <value>\n";
3536
3537  os << "The permitted matrix names are:\n";
3538  for(UInt sizeIdc = 0; sizeIdc < SCALING_LIST_SIZE_NUM; sizeIdc++)
3539  {
3540    for(UInt listIdc = 0; listIdc < SCALING_LIST_NUM; listIdc++)
3541    {
3542      if ((sizeIdc!=SCALING_LIST_32x32) || (listIdc%(SCALING_LIST_NUM/NUMBER_OF_PREDICTION_MODES) == 0))
3543      {
3544        os << "  " << MatrixType[sizeIdc][listIdc] << '\n';
3545      }
3546    }
3547  }
3548}
3549
3550Void TComScalingList::outputScalingLists(std::ostream &os) const
3551{
3552  for(UInt sizeIdc = 0; sizeIdc < SCALING_LIST_SIZE_NUM; sizeIdc++)
3553  {
3554    const UInt size = min(8,4<<(sizeIdc));
3555    for(UInt listIdc = 0; listIdc < SCALING_LIST_NUM; listIdc++)
3556    {
3557      if ((sizeIdc!=SCALING_LIST_32x32) || (listIdc%(SCALING_LIST_NUM/NUMBER_OF_PREDICTION_MODES) == 0))
3558      {
3559        const Int *src = getScalingListAddress(sizeIdc, listIdc);
3560        os << (MatrixType[sizeIdc][listIdc]) << " =\n  ";
3561        for(UInt y=0; y<size; y++)
3562        {
3563          for(UInt x=0; x<size; x++, src++)
3564          {
3565            os << std::setw(3) << (*src) << ", ";
3566          }
3567          os << (y+1<size?"\n  ":"\n");
3568        }
3569        if(sizeIdc > SCALING_LIST_8x8)
3570        {
3571          os << MatrixType_DC[sizeIdc][listIdc] << " = \n  " << std::setw(3) << getScalingListDC(sizeIdc, listIdc) << "\n";
3572        }
3573        os << "\n";
3574      }
3575    }
3576  }
3577}
3578
3579Bool TComScalingList::xParseScalingList(const std::string &fileName)
3580{
3581  static const Int LINE_SIZE=1024;
3582  FILE *fp = NULL;
3583  TChar line[LINE_SIZE];
3584
3585  if (fileName.empty())
3586  {
3587    fprintf(stderr, "Error: no scaling list file specified. Help on scaling lists being output\n");
3588    outputScalingListHelp(std::cout);
3589    std::cout << "\n\nExample scaling list file using default values:\n\n";
3590    outputScalingLists(std::cout);
3591    exit (1);
3592    return true;
3593  }
3594  else if ((fp = fopen(fileName.c_str(),"r")) == (FILE*)NULL)
3595  {
3596    fprintf(stderr, "Error: cannot open scaling list file %s for reading\n", fileName.c_str());
3597    return true;
3598  }
3599
3600  for(UInt sizeIdc = 0; sizeIdc < SCALING_LIST_SIZE_NUM; sizeIdc++)
3601  {
3602    const UInt size = min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeIdc]);
3603
3604    for(UInt listIdc = 0; listIdc < SCALING_LIST_NUM; listIdc++)
3605    {
3606      Int * const src = getScalingListAddress(sizeIdc, listIdc);
3607
3608      if ((sizeIdc==SCALING_LIST_32x32) && (listIdc%(SCALING_LIST_NUM/NUMBER_OF_PREDICTION_MODES) != 0)) // derive chroma32x32 from chroma16x16
3609      {
3610        const Int *srcNextSmallerSize = getScalingListAddress(sizeIdc-1, listIdc);
3611        for(UInt i=0; i<size; i++)
3612        {
3613          src[i] = srcNextSmallerSize[i];
3614        }
3615        setScalingListDC(sizeIdc,listIdc,(sizeIdc > SCALING_LIST_8x8) ? getScalingListDC(sizeIdc-1, listIdc) : src[0]);
3616      }
3617      else
3618      {
3619        {
3620          fseek(fp, 0, SEEK_SET);
3621          Bool bFound=false;
3622          while ((!feof(fp)) && (!bFound))
3623          {
3624            TChar *ret = fgets(line, LINE_SIZE, fp);
3625            TChar *findNamePosition= ret==NULL ? NULL : strstr(line, MatrixType[sizeIdc][listIdc]);
3626            // This could be a match against the DC string as well, so verify it isn't
3627            if (findNamePosition!= NULL && (MatrixType_DC[sizeIdc][listIdc]==NULL || strstr(line, MatrixType_DC[sizeIdc][listIdc])==NULL))
3628            {
3629              bFound=true;
3630            }
3631          }
3632          if (!bFound)
3633          {
3634            fprintf(stderr, "Error: cannot find Matrix %s from scaling list file %s\n", MatrixType[sizeIdc][listIdc], fileName.c_str());
3635            return true;
3636          }
3637        }
3638        for (UInt i=0; i<size; i++)
3639        {
3640          Int data;
3641          if (fscanf(fp, "%d,", &data)!=1)
3642          {
3643            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));
3644            return true;
3645          }
3646          if (data<0 || data>255)
3647          {
3648            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));
3649            return true;
3650          }
3651          src[i] = data;
3652        }
3653
3654        //set DC value for default matrix check
3655        setScalingListDC(sizeIdc,listIdc,src[0]);
3656
3657        if(sizeIdc > SCALING_LIST_8x8)
3658        {
3659          {
3660            fseek(fp, 0, SEEK_SET);
3661            Bool bFound=false;
3662            while ((!feof(fp)) && (!bFound))
3663            {
3664              TChar *ret = fgets(line, LINE_SIZE, fp);
3665              TChar *findNamePosition= ret==NULL ? NULL : strstr(line, MatrixType_DC[sizeIdc][listIdc]);
3666              if (findNamePosition!= NULL)
3667              {
3668                // This won't be a match against the non-DC string.
3669                bFound=true;
3670              }
3671            }
3672            if (!bFound)
3673            {
3674              fprintf(stderr, "Error: cannot find DC Matrix %s from scaling list file %s\n", MatrixType_DC[sizeIdc][listIdc], fileName.c_str());
3675              return true;
3676            }
3677          }
3678          Int data;
3679          if (fscanf(fp, "%d,", &data)!=1)
3680          {
3681            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));
3682            return true;
3683          }
3684          if (data<0 || data>255)
3685          {
3686            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));
3687            return true;
3688          }
3689          //overwrite DC value when size of matrix is larger than 16x16
3690          setScalingListDC(sizeIdc,listIdc,data);
3691        }
3692      }
3693    }
3694  }
3695//  std::cout << "\n\nRead scaling lists of:\n\n";
3696//  outputScalingLists(std::cout);
3697
3698  fclose(fp);
3699  return false;
3700}
3701
3702#if NH_MV
3703Void TComScalingList::inferFrom( const TComScalingList& srcScLi )
3704{
3705  for(Int sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
3706  {
3707    for(Int listId = 0; listId <  SCALING_LIST_NUM; listId++)
3708    {
3709      setRefMatrixId  (sizeId,listId, srcScLi.getRefMatrixId  (sizeId,listId));
3710      setScalingListDC(sizeId,listId, srcScLi.getScalingListDC(sizeId,listId));         
3711      ::memcpy(getScalingListAddress(sizeId, listId),srcScLi.getScalingListAddress(sizeId, listId),sizeof(Int)*min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId]));
3712    }
3713  }
3714}
3715#endif
3716
3717
3718/** get default address of quantization matrix
3719 * \param sizeId size index
3720 * \param listId list index
3721 * \returns pointer of quantization matrix
3722 */
3723const Int* TComScalingList::getScalingListDefaultAddress(UInt sizeId, UInt listId)
3724{
3725  const Int *src = 0;
3726  switch(sizeId)
3727  {
3728    case SCALING_LIST_4x4:
3729      src = g_quantTSDefault4x4;
3730      break;
3731    case SCALING_LIST_8x8:
3732    case SCALING_LIST_16x16:
3733    case SCALING_LIST_32x32:
3734      src = (listId < (SCALING_LIST_NUM/NUMBER_OF_PREDICTION_MODES) ) ? g_quantIntraDefault8x8 : g_quantInterDefault8x8;
3735      break;
3736    default:
3737      assert(0);
3738      src = NULL;
3739      break;
3740  }
3741  return src;
3742}
3743
3744/** process of default matrix
3745 * \param sizeId size index
3746 * \param listId index of input matrix
3747 */
3748Void TComScalingList::processDefaultMatrix(UInt sizeId, UInt listId)
3749{
3750  ::memcpy(getScalingListAddress(sizeId, listId),getScalingListDefaultAddress(sizeId,listId),sizeof(Int)*min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId]));
3751  setScalingListDC(sizeId,listId,SCALING_LIST_DC);
3752}
3753
3754/** check DC value of matrix for default matrix signaling
3755 */
3756Void TComScalingList::checkDcOfMatrix()
3757{
3758  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
3759  {
3760    for(UInt listId = 0; listId < SCALING_LIST_NUM; listId++)
3761    {
3762      //check default matrix?
3763      if(getScalingListDC(sizeId,listId) == 0)
3764      {
3765        processDefaultMatrix(sizeId, listId);
3766      }
3767    }
3768  }
3769}
3770
3771ParameterSetManager::ParameterSetManager()
3772: m_vpsMap(MAX_NUM_VPS)
3773, m_spsMap(MAX_NUM_SPS)
3774, m_ppsMap(MAX_NUM_PPS)
3775, m_activeVPSId(-1)
3776#if !NH_MV
3777, m_activeSPSId(-1)
3778#endif
3779{
3780#if NH_MV 
3781  for (Int i = 0; i < MAX_NUM_LAYERS; i++ )
3782  {   
3783    m_activeSPSId[i] = -1; 
3784  }
3785#endif
3786}
3787
3788
3789ParameterSetManager::~ParameterSetManager()
3790{
3791}
3792
3793//! activate a SPS from a active parameter sets SEI message
3794//! \returns true, if activation is successful
3795#if NH_MV
3796//Bool ParameterSetManager::activateSPSWithSEI(Int spsId, Int layerId )
3797#else
3798//Bool ParameterSetManager::activateSPSWithSEI(Int spsId)
3799#endif
3800//{
3801//TComSPS *sps = m_spsMap.getPS(spsId);
3802//  if (sps)
3803//  {
3804//    Int vpsId = sps->getVPSId();
3805//    TComVPS *vps = m_vpsMap.getPS(vpsId);
3806//    if (vps)
3807//    {
3808#if NH_MV
3809//      m_activeVPS = *(vps);
3810//      m_activeSPS[ layerId ] = *(sps);
3811#else
3812//      m_activeVPS = *(vps);
3813//      m_activeSPS = *(sps);
3814#endif
3815//      return true;
3816//    }
3817//    else
3818//    {
3819//      printf("Warning: tried to activate SPS using an Active parameter sets SEI message. Referenced VPS does not exist.");
3820//    }
3821//  }
3822//  else
3823//  {
3824//    printf("Warning: tried to activate non-existing SPS using an Active parameter sets SEI message.");
3825//  }
3826//  return false;
3827//}
3828
3829//! activate a PPS and depending on isIDR parameter also SPS and VPS
3830//! \returns true, if activation is successful
3831#if NH_MV
3832Bool ParameterSetManager::activatePPS(Int ppsId, Bool isIRAP, Int layerId )
3833#else
3834Bool ParameterSetManager::activatePPS(Int ppsId, Bool isIRAP)
3835#endif
3836{
3837  TComPPS *pps = m_ppsMap.getPS(ppsId);
3838  if (pps)
3839  {
3840    Int spsId = pps->getSPSId();
3841#if NH_MV
3842    if (!isIRAP && (spsId != m_activeSPSId[ layerId ] ))
3843#else
3844    if (!isIRAP && (spsId != m_activeSPSId ))
3845#endif
3846    {
3847      printf("Warning: tried to activate PPS referring to a inactive SPS at non-IDR.");
3848    }
3849    else
3850    {
3851      TComSPS *sps = m_spsMap.getPS(spsId);
3852      if (sps)
3853      {
3854        Int vpsId = sps->getVPSId();
3855        if (!isIRAP && (vpsId != m_activeVPSId ))
3856        {
3857          printf("Warning: tried to activate PPS referring to a inactive VPS at non-IDR.");
3858        }
3859        else
3860        {
3861          TComVPS *vps =m_vpsMap.getPS(vpsId);
3862          if (vps)
3863          {
3864            m_activeVPSId = vpsId;
3865#if NH_MV
3866            m_activeSPSId[layerId] = spsId;
3867#else
3868            m_activeSPSId = spsId;
3869#endif
3870            return true;
3871          }
3872          else
3873          {
3874            printf("Warning: tried to activate PPS that refers to a non-existing VPS.");
3875          }
3876        }
3877      }
3878      else
3879      {
3880       printf("Warning: tried to activate a PPS that refers to a non-existing SPS.");
3881      }
3882    }
3883  }
3884  else
3885  {
3886    printf("Warning: tried to activate non-existing PPS.");
3887  }
3888
3889  // Failed to activate if reach here.
3890#if NH_MV
3891  m_activeSPSId[layerId]=-1;
3892#else
3893  m_activeSPSId=-1;
3894#endif
3895  m_activeVPSId=-1;
3896
3897  return false;
3898}
3899
3900ProfileTierLevel::ProfileTierLevel()
3901  : m_profileSpace    (0)
3902  , m_tierFlag        (Level::MAIN)
3903  , m_profileIdc      (Profile::NONE)
3904  , m_levelIdc        (Level::NONE)
3905  , m_progressiveSourceFlag  (false)
3906  , m_interlacedSourceFlag   (false)
3907  , m_nonPackedConstraintFlag(false)
3908  , m_frameOnlyConstraintFlag(false)
3909#if NH_MV
3910  , m_intraConstraintFlag         ( false )
3911  , m_onePictureOnlyConstraintFlag( false )
3912  , m_lowerBitRateConstraintFlag  ( false )
3913  , m_max12bitConstraintFlag      ( false )
3914  , m_max10bitConstraintFlag      ( false )
3915  , m_max8bitConstraintFlag       ( false )
3916  , m_max422chromaConstraintFlag  ( false )
3917  , m_max420chromaConstraintFlag  ( false )
3918  , m_maxMonochromeConstraintFlag ( false ) 
3919  , m_inbldFlag                   ( false )
3920#endif
3921{
3922  ::memset(m_profileCompatibilityFlag, 0, sizeof(m_profileCompatibilityFlag));
3923}
3924
3925#if NH_MV
3926Bool ProfileTierLevel::getV2ConstraintsPresentFlag() const
3927{
3928  return ( 
3929    getProfileIdc( ) ==  4 || getProfileCompatibilityFlag( 4 ) || getProfileIdc( ) ==  5 || getProfileCompatibilityFlag( 5 )  ||
3930    getProfileIdc( ) ==  6 || getProfileCompatibilityFlag( 6 ) || getProfileIdc( ) ==  7 || getProfileCompatibilityFlag( 7 ) 
3931    );
3932}
3933
3934Bool ProfileTierLevel::getInbldPresentFlag() const
3935{
3936  return ( 
3937    ( getProfileIdc() >= 1 && getProfileIdc() <= 5 )  || getProfileCompatibilityFlag( 1 ) || getProfileCompatibilityFlag( 2 ) || 
3938    getProfileCompatibilityFlag( 3 ) || getProfileCompatibilityFlag( 4 )  ||   getProfileCompatibilityFlag( 5 ) 
3939    );
3940}
3941
3942Void ProfileTierLevel::copyV2ConstraintFlags(ProfileTierLevel* ptlRef)
3943{
3944  setMax12bitConstraintFlag         ( ptlRef->getMax12bitConstraintFlag       ( ) );
3945  setMax10bitConstraintFlag         ( ptlRef->getMax10bitConstraintFlag       ( ) );
3946  setMax8bitConstraintFlag          ( ptlRef->getMax8bitConstraintFlag        ( ) );
3947  setMax422chromaConstraintFlag     ( ptlRef->getMax422chromaConstraintFlag   ( ) );
3948  setMax420chromaConstraintFlag     ( ptlRef->getMax420chromaConstraintFlag   ( ) );
3949  setMaxMonochromeConstraintFlag    ( ptlRef->getMaxMonochromeConstraintFlag  ( ) );
3950  setIntraConstraintFlag            ( ptlRef->getIntraConstraintFlag          ( ) );
3951  setOnePictureOnlyConstraintFlag   ( ptlRef->getOnePictureOnlyConstraintFlag ( ) );
3952  setLowerBitRateConstraintFlag     ( ptlRef->getLowerBitRateConstraintFlag   ( ) );
3953}
3954
3955Void ProfileTierLevel::copyProfile(ProfileTierLevel* ptlRef)
3956{
3957  setProfileSpace            ( ptlRef->getProfileSpace              ( ) );
3958  setTierFlag                ( ptlRef->getTierFlag                  ( ) );
3959  setProfileIdc              ( ptlRef->getProfileIdc                ( ) );
3960  for (Int j = 0; j < 32; j++)
3961  {     
3962    setProfileCompatibilityFlag(j, ptlRef->getProfileCompatibilityFlag  ( j ) );           
3963  }
3964  setProgressiveSourceFlag   ( ptlRef->getProgressiveSourceFlag     ( ) );
3965  setInterlacedSourceFlag    ( ptlRef->getInterlacedSourceFlag      ( ) );
3966  setNonPackedConstraintFlag ( ptlRef->getNonPackedConstraintFlag   ( ) );
3967  setFrameOnlyConstraintFlag ( ptlRef->getFrameOnlyConstraintFlag   ( ) );
3968  copyV2ConstraintFlags      ( ptlRef );
3969}
3970#endif
3971
3972TComPTL::TComPTL()
3973{
3974  ::memset(m_subLayerProfilePresentFlag, 0, sizeof(m_subLayerProfilePresentFlag));
3975  ::memset(m_subLayerLevelPresentFlag,   0, sizeof(m_subLayerLevelPresentFlag  ));
3976}
3977
3978Void calculateParameterSetChangedFlag(Bool &bChanged, const std::vector<UChar> *pOldData, const std::vector<UChar> &newData)
3979{
3980  if (!bChanged)
3981  {
3982    if ((pOldData==0 && pOldData!=0) || (pOldData!=0 && pOldData==0))
3983    {
3984      bChanged=true;
3985    }
3986    else if (pOldData!=0 && pOldData!=0)
3987    {
3988      // compare the two
3989      if (pOldData->size() != pOldData->size())
3990      {
3991        bChanged=true;
3992      }
3993      else
3994      {
3995        const UChar *pNewDataArray=&(newData)[0];
3996        const UChar *pOldDataArray=&(*pOldData)[0];
3997        if (memcmp(pOldDataArray, pNewDataArray, pOldData->size()))
3998        {
3999          bChanged=true;
4000        }
4001      }
4002    }
4003  }
4004}
4005#if NH_MV
4006Void TComPTL::inferGeneralValues(Bool profilePresentFlag, Int k, TComPTL* refPTL)
4007{
4008  ProfileTierLevel* refProfileTierLevel = NULL; 
4009  if ( k > 0 )
4010  {   
4011    assert( refPTL != NULL);
4012    refProfileTierLevel = refPTL->getGeneralPTL(); 
4013  }
4014
4015  ProfileTierLevel* curProfileTierLevel = getGeneralPTL( ); 
4016  assert( curProfileTierLevel != NULL ); 
4017
4018  if( !profilePresentFlag )
4019  {
4020    assert( k > 0 ); 
4021    assert( refProfileTierLevel != NULL ); 
4022    curProfileTierLevel->copyProfile( refProfileTierLevel);
4023  }
4024  else
4025  {
4026    if ( !curProfileTierLevel->getV2ConstraintsPresentFlag() )
4027    {
4028      curProfileTierLevel->setMax12bitConstraintFlag         ( false );
4029      curProfileTierLevel->setMax10bitConstraintFlag         ( false );
4030      curProfileTierLevel->setMax8bitConstraintFlag          ( false );
4031      curProfileTierLevel->setMax422chromaConstraintFlag     ( false );
4032      curProfileTierLevel->setMax420chromaConstraintFlag     ( false );
4033      curProfileTierLevel->setMaxMonochromeConstraintFlag    ( false );
4034      curProfileTierLevel->setIntraConstraintFlag            ( false );
4035      curProfileTierLevel->setOnePictureOnlyConstraintFlag   ( false );
4036      curProfileTierLevel->setLowerBitRateConstraintFlag     ( false );   
4037    }
4038
4039    if ( !curProfileTierLevel->getInbldPresentFlag() )
4040    {
4041      curProfileTierLevel->setInbldFlag( false ); 
4042    }     
4043  }
4044}
4045
4046Void TComPTL::inferSubLayerValues(Int maxNumSubLayersMinus1, Int k, TComPTL* refPTL)
4047{
4048  assert( k == 0 || refPTL != NULL );   
4049  for (Int i = maxNumSubLayersMinus1; i >= 0; i--)
4050  {
4051    ProfileTierLevel* refProfileTierLevel;
4052    if ( k != 0 )
4053    {
4054      refProfileTierLevel = refPTL->getSubLayerPTL( i );
4055    }
4056    else
4057    {
4058      if ( i == maxNumSubLayersMinus1)     
4059      {
4060        refProfileTierLevel = getGeneralPTL();
4061      }
4062      else
4063      {
4064        refProfileTierLevel = getSubLayerPTL( i + 1 );
4065      }
4066    }   
4067
4068    assert( refProfileTierLevel != NULL ); 
4069    ProfileTierLevel* curProfileTierLevel = getSubLayerPTL( i ); 
4070    assert( curProfileTierLevel != NULL ); 
4071    if( !getSubLayerLevelPresentFlag( i ) )
4072    {
4073      curProfileTierLevel->setLevelIdc( refProfileTierLevel->getLevelIdc() ); 
4074    }
4075
4076    if( !getSubLayerProfilePresentFlag( i ) )
4077    {
4078      curProfileTierLevel->copyProfile( refProfileTierLevel);
4079    }
4080    else
4081    {
4082      if ( !curProfileTierLevel->getV2ConstraintsPresentFlag() )
4083      {
4084        curProfileTierLevel->copyV2ConstraintFlags( refProfileTierLevel ); 
4085      }
4086
4087      if ( !curProfileTierLevel->getInbldPresentFlag() )
4088      {
4089        curProfileTierLevel->setInbldFlag( refProfileTierLevel->getInbldFlag() ); 
4090      }     
4091    }     
4092  }
4093}
4094
4095#endif
4096
4097//! \}
4098
4099#if NH_MV
4100Void TComVPSVUI::init( Int numLayerSets, Int maxNumSubLayers, Int maxNumLayers )
4101{
4102  m_crossLayerIrapAlignedFlag = true; 
4103  m_allLayersIdrAlignedFlag   = false; 
4104  m_bitRatePresentVpsFlag     = false;
4105  m_picRatePresentVpsFlag     = false;
4106
4107  m_bitRatePresentFlag          .resize(numLayerSets); 
4108  m_picRatePresentFlag          .resize(numLayerSets); 
4109  m_avgBitRate                  .resize(numLayerSets); 
4110  m_maxBitRate                  .resize(numLayerSets); 
4111  m_constantPicRateIdc          .resize(numLayerSets); 
4112  m_avgPicRate                  .resize(numLayerSets); 
4113
4114  for ( Int i = 0; i < numLayerSets; i++)
4115  { 
4116    m_bitRatePresentFlag          [i].resize( maxNumSubLayers); 
4117    m_picRatePresentFlag          [i].resize( maxNumSubLayers); 
4118    m_avgBitRate                  [i].resize( maxNumSubLayers); 
4119    m_maxBitRate                  [i].resize( maxNumSubLayers); 
4120    m_constantPicRateIdc          [i].resize( maxNumSubLayers); 
4121    m_avgPicRate                  [i].resize( maxNumSubLayers); 
4122    for ( Int j = 0; j < maxNumSubLayers; j++)
4123    {   
4124      m_bitRatePresentFlag          [i][j] = false;
4125      m_picRatePresentFlag          [i][j] = false;
4126      m_avgBitRate                  [i][j] = -1;
4127      m_maxBitRate                  [i][j] = -1;
4128      m_constantPicRateIdc          [i][j] = -1;
4129      m_avgPicRate                  [i][j] = -1;
4130    }
4131  }
4132
4133  m_ilpRestrictedRefLayersFlag = false;
4134
4135  m_tileBoundariesAlignedFlag             .resize( maxNumLayers );
4136  m_minSpatialSegmentOffsetPlus1          .resize( maxNumLayers );
4137  m_ctuBasedOffsetEnabledFlag             .resize( maxNumLayers );
4138  m_minHorizontalCtuOffsetPlus1           .resize( maxNumLayers );
4139  m_baseLayerParameterSetCompatibilityFlag.resize( maxNumLayers );
4140
4141  for ( Int i = 0; i < maxNumLayers; i++)
4142  {         
4143    m_tileBoundariesAlignedFlag   [i].resize( maxNumLayers );
4144    m_minSpatialSegmentOffsetPlus1[i].resize( maxNumLayers );
4145    m_ctuBasedOffsetEnabledFlag   [i].resize( maxNumLayers );
4146    m_minHorizontalCtuOffsetPlus1 [i].resize( maxNumLayers );
4147    for ( Int j = 0; j < maxNumLayers; j++)
4148    {   
4149      m_tileBoundariesAlignedFlag   [i][j] = false;
4150      m_minSpatialSegmentOffsetPlus1[i][j] = 0;
4151      m_ctuBasedOffsetEnabledFlag   [i][j] = false;
4152      m_minHorizontalCtuOffsetPlus1 [i][j] = -1;
4153    }
4154    m_baseLayerParameterSetCompatibilityFlag[i] = false;
4155  }
4156  m_vpsVuiBspHrdPresentFlag = false; 
4157}
4158
4159Void TComRepFormat::inferChromaAndBitDepth( const TComRepFormat* prevRepFormat )
4160{
4161    setChromaAndBitDepthVpsPresentFlag( prevRepFormat->getChromaAndBitDepthVpsPresentFlag() );
4162    setSeparateColourPlaneVpsFlag     ( prevRepFormat->getSeparateColourPlaneVpsFlag     () );
4163    setBitDepthVpsLumaMinus8          ( prevRepFormat->getBitDepthVpsLumaMinus8          () );
4164    setBitDepthVpsChromaMinus8        ( prevRepFormat->getBitDepthVpsChromaMinus8        () );
4165}
4166
4167Void TComRepFormat::checkInferChromaAndBitDepth( const TComRepFormat* prevRepFormat ) const 
4168{
4169    assert( getChromaAndBitDepthVpsPresentFlag() == prevRepFormat->getChromaAndBitDepthVpsPresentFlag() );
4170    assert( getSeparateColourPlaneVpsFlag     () == prevRepFormat->getSeparateColourPlaneVpsFlag     () );
4171    assert( getBitDepthVpsLumaMinus8          () == prevRepFormat->getBitDepthVpsLumaMinus8          () );
4172    assert( getBitDepthVpsChromaMinus8        () == prevRepFormat->getBitDepthVpsChromaMinus8        () );
4173}
4174
4175Int TComVpsVuiBspHrdParameters::getBspHrdIdxLen( const TComVPS* vps) const
4176{
4177  return gCeilLog2( vps->getNumHrdParameters() + getVpsNumAddHrdParams() );
4178}
4179
4180Void TComVpsVuiBspHrdParameters::createAfterVpsNumAddHrdParams( const TComVPS* vps )
4181{
4182  m_offsetHrdParamIdx = vps->getNumHrdParameters(); 
4183  m_numHrdParam       = vps->getNumHrdParameters() + getVpsNumAddHrdParams() - m_offsetHrdParamIdx;
4184  m_numOls            = vps->getNumOutputLayerSets(); 
4185
4186  m_cprmsAddPresentFlag .resize( m_numHrdParam );
4187  m_numSubLayerHrdMinus1.resize( m_numHrdParam );
4188  m_hrdParameters       .resize( m_numHrdParam );
4189
4190  m_numSignalledPartitioningSchemes .resize( m_numOls ); 
4191  m_numPartitionsInSchemeMinus1     .resize( m_numOls );
4192  m_numBspSchedulesMinus1           .resize( m_numOls ); 
4193  m_bspHrdIdx                       .resize( m_numOls );
4194  m_bspSchedIdx                     .resize( m_numOls );
4195}
4196
4197Void TComVpsVuiBspHrdParameters::createAfterNumSignalledPartitioningSchemes( const TComVPS* vps, Int h )
4198{
4199  m_numPartitionsInSchemeMinus1 [h].resize( getNumSignalledPartitioningSchemes(h) );
4200  m_layerIncludedInPartitionFlag[h].resize( getNumSignalledPartitioningSchemes(h) );   
4201
4202  m_numBspSchedulesMinus1       [h].resize( getNumSignalledPartitioningSchemes(h) + 1 );
4203  for (Int i = 0; i < getNumSignalledPartitioningSchemes(h) + 1; i++)
4204  {
4205    Int tMax = vps->getMaxSubLayersInLayerSetMinus1( m_vps->olsIdxToLsIdx(h) ) + 1;
4206    m_numBspSchedulesMinus1[h][i].resize( tMax );
4207    m_bspHrdIdx            [h][i].resize( tMax );
4208    m_bspSchedIdx          [h][i].resize( tMax );
4209  }
4210}
4211
4212Void TComVpsVuiBspHrdParameters::createAfterNumPartitionsInSchemeMinus1( const TComVPS* vps, Int h, Int j )
4213{
4214  m_layerIncludedInPartitionFlag[h][j].resize( getNumPartitionsInSchemeMinus1(h,j));
4215  for( Int k = 0; k < getNumPartitionsInSchemeMinus1(h,j); k++ )
4216  {
4217    m_layerIncludedInPartitionFlag[h][j][k].resize( m_vps->getNumLayersInIdList( vps->olsIdxToLsIdx(h)));
4218  }
4219}
4220
4221Void TComVpsVuiBspHrdParameters::createAfterNumBspSchedulesMinus1( const TComVPS* vps, Int h, Int i, Int t )
4222{
4223  m_bspSchedIdx[h][i][t].resize( getNumBspSchedulesMinus1( h, i, t ) + 1 );
4224  m_bspHrdIdx  [h][i][t].resize( getNumBspSchedulesMinus1( h, i, t ) + 1 );
4225  for( Int j = 0; j < getNumBspSchedulesMinus1( h, i, t ) + 1; j++ )
4226  {
4227    m_bspSchedIdx[h][i][t][j].resize( getNumPartitionsInSchemeMinus1( h, i ) );
4228    m_bspHrdIdx  [h][i][t][j].resize( getNumPartitionsInSchemeMinus1( h, i ) );
4229  }
4230}
4231
4232
4233Void TComVUI::inferVideoSignalInfo( const TComVPS* vps, Int layerIdCurr )
4234{
4235  if ( layerIdCurr == 0 || !vps->getVpsVuiPresentFlag() ) 
4236  {
4237    return; 
4238  }
4239
4240  const TComVPSVUI* vpsVui = vps->getVPSVUI(); 
4241  assert( vpsVui != NULL ); 
4242
4243  const TComVideoSignalInfo* videoSignalInfo = vpsVui->getVideoSignalInfo( vpsVui->getVpsVideoSignalInfoIdx( vps->getLayerIdInVps( layerIdCurr ) ) ); 
4244  assert( videoSignalInfo != NULL );
4245
4246  setVideoFormat            ( videoSignalInfo->getVideoVpsFormat            () ); 
4247  setVideoFullRangeFlag     ( videoSignalInfo->getVideoFullRangeVpsFlag     () );
4248  setColourPrimaries        ( videoSignalInfo->getColourPrimariesVps        () );
4249  setTransferCharacteristics( videoSignalInfo->getTransferCharacteristicsVps() );
4250  setMatrixCoefficients     ( videoSignalInfo->getMatrixCoeffsVps           () );
4251}
4252
4253Void TComDpbSize::init( Int numOutputLayerSets, Int maxNumLayerIds, Int maxNumSubLayers ) 
4254{
4255  m_subLayerFlagInfoPresentFlag.resize( numOutputLayerSets );
4256  m_subLayerDpbInfoPresentFlag .resize( numOutputLayerSets );
4257  m_maxVpsDecPicBufferingMinus1.resize( numOutputLayerSets ); 
4258  m_maxVpsNumReorderPics       .resize( numOutputLayerSets );
4259  m_maxVpsLatencyIncreasePlus1 .resize( numOutputLayerSets );
4260
4261  for (Int i = 0; i < numOutputLayerSets; i++ )
4262  {     
4263    m_subLayerFlagInfoPresentFlag[i]  = false;
4264
4265    m_subLayerDpbInfoPresentFlag [i].resize( maxNumSubLayers );
4266    m_maxVpsDecPicBufferingMinus1[i].resize( maxNumLayerIds ); 
4267    m_maxVpsNumReorderPics       [i].resize( maxNumSubLayers );
4268    m_maxVpsLatencyIncreasePlus1 [i].resize( maxNumSubLayers );   
4269
4270    for (Int j = 0; j < maxNumSubLayers; j++  )
4271    {       
4272      m_subLayerDpbInfoPresentFlag [i][j] = ( j == 0) ;
4273      m_maxVpsNumReorderPics       [i][j] = 0;
4274      m_maxVpsLatencyIncreasePlus1 [i][j] = 0;
4275    }
4276
4277    for (Int k = 0; k < maxNumLayerIds; k++ )
4278    {
4279      m_maxVpsDecPicBufferingMinus1[i][k].resize( maxNumSubLayers );
4280      for (Int j = 0; j < maxNumSubLayers; j++  )
4281      {       
4282        m_maxVpsDecPicBufferingMinus1[i][k][j] = MIN_INT; 
4283      }
4284    }
4285  }
4286}
4287
4288
4289Int TComDpbSize::getVpsMaxLatencyPictures( Int i, Int j ) const
4290{
4291  return getMaxVpsNumReorderPics( i, j ) + getMaxVpsLatencyIncreasePlus1(i, j) - 1; 
4292}
4293
4294Void Window::scaleOffsets( Int scal )
4295{
4296  if (! m_scaledFlag )
4297  {
4298    m_scaledFlag         = true; 
4299    m_winLeftOffset     *= scal; 
4300    m_winRightOffset    *= scal; 
4301    m_winTopOffset      *= scal; 
4302    m_winBottomOffset   *= scal; 
4303  }
4304}
4305
4306Void TComStRefPicSet::inferRps( Int stRpsIdx, TComSPS* sps, Bool encoder )
4307{
4308  if ( getInterRefPicSetPredictionFlag() )
4309  {
4310    // When inter_ref_pic_set_prediction_flag is equal to 1, the variables DeltaPocS0[ stRpsIdx ][ i ], UsedByCurrPicS0[ stRpsIdx ][ i ],
4311    // NumNegativePics[ stRpsIdx ], DeltaPocS1[ stRpsIdx ][ i ], UsedByCurrPicS1[ stRpsIdx ][ i ] and NumPositivePics[ stRpsIdx ] are
4312    // derived as follows:
4313
4314    Int i = 0;
4315    Int refRpsIdx = getRefRpsIdx( stRpsIdx );
4316    TComStRefPicSet* refRps = sps->getStRefPicSet( refRpsIdx ); 
4317
4318    for( Int j = refRps->getNumPositivePicsVar( ) - 1; j >= 0; j-- ) 
4319    {
4320      Int dPoc = refRps->getDeltaPocS1Var( j ) + getDeltaRps();
4321      if( dPoc < 0  &&  getUseDeltaFlag( refRps->getNumNegativePicsVar( ) + j ) ) 
4322      {
4323        setDeltaPocS0Var     ( i, dPoc );
4324        setUsedByCurrPicS0Var( i++ , getUsedByCurrPicFlag( refRps->getNumNegativePicsVar( ) + j ) );
4325      }
4326    }
4327    if( getDeltaRps() < 0  && getUseDeltaFlag( refRps->getNumDeltaPocs() ) )   //   (7 59)
4328    {
4329      setDeltaPocS0Var( i,  getDeltaRps() ); 
4330      setUsedByCurrPicS0Var( i++ , getUsedByCurrPicFlag( refRps->getNumDeltaPocs() ) );
4331    }
4332    for( Int j = 0; j < refRps->getNumNegativePicsVar(); j++ )
4333    {
4334      Int dPoc = refRps->getDeltaPocS0Var( j ) + getDeltaRps(); 
4335      if( dPoc < 0  &&  getUseDeltaFlag( j ) ) 
4336      {
4337        setDeltaPocS0Var( i , dPoc);
4338        setUsedByCurrPicS0Var( i++ , getUsedByCurrPicFlag( j )) ;
4339      }
4340    }
4341
4342    setNumNegativePicsVar( i );
4343
4344    i = 0;
4345    for( Int j = refRps->getNumNegativePicsVar() - 1; j  >=  0; j-- )
4346    {
4347      Int dPoc = refRps->getDeltaPocS0Var( j ) + getDeltaRps(); 
4348      if( dPoc > 0  &&  getUseDeltaFlag( j ) )
4349      {
4350        setDeltaPocS1Var( i, dPoc );
4351        setUsedByCurrPicS1Var(  i++, getUsedByCurrPicFlag( j ) ) ;
4352      }
4353    }
4354
4355    if( getDeltaRps() > 0  &&  getUseDeltaFlag( refRps->getNumDeltaPocs() ) ) //  (7 60)
4356    { 
4357      setDeltaPocS1Var( i , getDeltaRps() );
4358      setUsedByCurrPicS1Var( i++ , getUsedByCurrPicFlag( refRps->getNumDeltaPocs() ));
4359    }
4360
4361    for( Int j = 0; j < refRps->getNumPositivePicsVar( ); j++) 
4362    {
4363      Int dPoc = refRps->getDeltaPocS1Var( j ) + getDeltaRps(); 
4364      if( dPoc > 0  &&  getUseDeltaFlag( refRps->getNumNegativePicsVar() + j ) ) 
4365      {
4366        setDeltaPocS1Var( i, dPoc);
4367        setUsedByCurrPicS1Var( i++, getUsedByCurrPicFlag( refRps->getNumNegativePicsVar() + j ));
4368      }
4369    }
4370    setNumPositivePicsVar(  i );
4371  }
4372  else
4373  {
4374    // When inter_ref_pic_set_prediction_flag is equal to 0, the variables NumNegativePics[ stRpsIdx ], NumPositivePics[ stRpsIdx ],
4375    // UsedByCurrPicS0[ stRpsIdx ][ i ], UsedByCurrPicS1[ stRpsIdx ][ i ], DeltaPocS0[ stRpsIdx ][ i ] and DeltaPocS1[ stRpsIdx ][ i ]
4376    // are derived as follows:
4377
4378    setNumNegativePicsVar( getNumNegativePics( ) );        //  (7 61)
4379    setNumPositivePicsVar( getNumPositivePics( ) );         //  (7 62)
4380
4381    for (Int i = 0 ; i < getNumNegativePics(); i++ )
4382    {     
4383      setUsedByCurrPicS0Var( i,  getUsedByCurrPicS0Flag( i ) ); //  (7 63)
4384      if (i == 0 )
4385      {
4386        setDeltaPocS0Var( i , -( getDeltaPocS0Minus1( i ) + 1 )); // (7 65)
4387      }
4388      else
4389      {
4390        setDeltaPocS0Var( i , getDeltaPocS0Var( i - 1 ) - ( getDeltaPocS0Minus1( i ) + 1 )); //  (7 67)
4391      }
4392    }
4393
4394    for (Int i = 0 ; i < getNumPositivePics(); i++ )
4395    {     
4396      setUsedByCurrPicS1Var( i,  getUsedByCurrPicS1Flag( i ) ); //  (7 64)
4397
4398      if (i == 0 )
4399      {
4400        setDeltaPocS1Var( i , getDeltaPocS1Minus1( i ) + 1    );      // (7 66)
4401      }
4402      else
4403      {
4404        setDeltaPocS1Var( i , getDeltaPocS1Var( i - 1 ) + ( getDeltaPocS1Minus1( i ) + 1 )); //  (7 68)
4405      }
4406    }
4407  }
4408}
4409
4410#endif
Note: See TracBrowser for help on using the repository browser.