source: 3DVCSoftware/branches/HTM-12.2-dev2-HHI/source/Lib/TLibCommon/TComSlice.cpp @ 1110

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

H_MV_HLS_FIX. Various fixes for MV-HEVC HLS syntax.

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