source: 3DVCSoftware/branches/HTM-12.2-dev0/source/Lib/TLibCommon/TComSlice.cpp @ 1118

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

Merged 12.2-dev1-Hisilicon@1116.

  • Property svn:eol-style set to native
File size: 155.5 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#if  H_MV_FIX_NUM_VIEWS
1882  m_numViews = 0; 
1883#endif
1884
1885#endif
1886
1887  for( Int i = 0; i < MAX_TLAYER; i++)
1888  {
1889    m_numReorderPics[i] = 0;
1890    m_uiMaxDecPicBuffering[i] = 1; 
1891    m_uiMaxLatencyIncrease[i] = 0;
1892  }
1893#if H_MV
1894  for (Int lsIdx = 0; lsIdx < MAX_VPS_OP_SETS_PLUS1; lsIdx++ )
1895  { 
1896    for( Int layerId = 0; layerId < MAX_VPS_NUH_LAYER_ID_PLUS1; layerId++ )
1897    {
1898      m_layerIdIncludedFlag[lsIdx][layerId] = (( lsIdx == 0 ) && ( layerId == 0 )) ; 
1899    }
1900  } 
1901  m_vpsNumProfileTierLevelMinus1 = -1; 
1902   
1903  m_numAddLayerSets              = 0;   
1904  m_numAddOlss                   = 0; 
1905  m_defaultOutputLayerIdc     = 0; 
1906 
1907  for ( Int i = 0; i < MAX_VPS_OUTPUTLAYER_SETS; i++)
1908  {
1909    m_layerSetIdxForOlsMinus1[i]  = -1; 
1910    for ( Int j = 0; j < MAX_VPS_NUH_LAYER_ID_PLUS1; j++)
1911    {
1912      m_profileTierLevelIdx[i][j] = -1; 
1913      m_outputLayerFlag[i][j] = false; 
1914    }
1915    m_altOutputLayerFlag[ i ]       = false; 
1916  }
1917
1918  m_repFormatIdxPresentFlag = false; 
1919  m_maxOneActiveRefLayerFlag = false; 
1920#if H_MV_HLS_FIX
1921  m_vpsPocLsbAlignedFlag  = false; 
1922#endif
1923  m_directDepTypeLenMinus2   = 0;         
1924 
1925
1926  m_vpsExtensionFlag = true; 
1927  m_vpsNonVuiExtensionLength = 0;
1928  m_splittingFlag    = false;
1929
1930 
1931  for( Int i = 0; i < MAX_NUM_SCALABILITY_TYPES; i++ )
1932  {
1933    m_scalabilityMaskFlag[i] = false;
1934    m_dimensionIdLen [i]  = -1; 
1935  }
1936
1937  m_vpsNuhLayerIdPresentFlag = false;
1938
1939  for( Int i = 0; i < MAX_VPS_OP_SETS_PLUS1; i++ )
1940  {
1941    m_vpsProfilePresentFlag   [i] = false;
1942    m_layerSetIdxForOlsMinus1       [i] = 0;
1943    for( Int j = 0; j < MAX_VPS_NUH_LAYER_ID_PLUS1; j++ )
1944    {
1945      m_outputLayerFlag[i][j] = false;
1946    }
1947  }
1948
1949  for( Int i = 0; i < MAX_NUM_LAYER_IDS; i++ )
1950  {
1951    m_layerIdInVps[i] =  (i == 0 ) ? 0 : -1;         
1952  }
1953
1954  for( Int i = 0; i < MAX_NUM_LAYERS; i++ )
1955  {
1956    m_layerIdInNuh      [i] = ( i == 0 ) ? 0 : -1; 
1957    m_numDirectRefLayers[i] = 0; 
1958#if HHI_DEPENDENCY_SIGNALLING_I1_J0107
1959#if H_3D
1960    m_numRefListLayers[i] = 0; 
1961#endif
1962#endif
1963    m_vpsRepFormatIdx    [i] = 0; 
1964    m_pocLsbNotPresentFlag[i] = 0;
1965    m_repFormat          [i] = NULL; 
1966    m_viewIdVal          [i] = 0; 
1967
1968#if H_3D
1969    m_viewIndex         [i] = -1; 
1970#if !HHI_TOOL_PARAMETERS_I2_J0107
1971    m_vpsDepthModesFlag [i] = false;
1972    m_ivMvScalingFlag[i] = true; 
1973    m_bIVPFlag [i]      = false;
1974#if MTK_SINGLE_DEPTH_VPS_FLAG_J0060
1975    m_singleDepthModeFlag[ i ] = false;
1976#endif
1977#endif
1978#endif
1979
1980    for( Int j = 0; j < MAX_NUM_LAYERS; j++ )
1981    {
1982      m_directDependencyFlag[i][j] = false;
1983      m_directDependencyType[i][j] = -1; 
1984      m_dependencyFlag  [i][j]    = false; 
1985      m_idDirectRefLayer[i][j]    = -1; 
1986#if HHI_DEPENDENCY_SIGNALLING_I1_J0107
1987#if H_3D
1988      m_idRefListLayer[i][j]    = -1; 
1989#endif
1990#endif
1991      m_idPredictedLayer[i][j]    = -1; 
1992      m_idRefLayer      [i][j]    = -1; 
1993      m_maxTidIlRefPicsPlus1[i][j]  = 7;
1994    }
1995
1996    for( Int j = 0; j < MAX_NUM_SCALABILITY_TYPES; j++ )
1997    {
1998      m_dimensionId[i][j] = 0;
1999    }
2000#if H_3D_ARP
2001#if !HHI_TOOL_PARAMETERS_I2_J0107
2002    m_uiUseAdvResPred[i]  = 0;
2003    m_uiARPStepNum[i]     = 1;
2004#endif
2005#endif
2006  }
2007  m_vpsVUI = new TComVPSVUI; 
2008  m_dpbSize = new TComDpbSize; 
2009
2010#if !HHI_TOOL_PARAMETERS_I2_J0107
2011#if H_3D
2012  for( Int i = 0; i < MAX_NUM_LAYERS; i++ )
2013  {
2014#if H_3D_IV_MERGE
2015    m_ivMvPredFlag         [ i ] = false;
2016#if H_3D_SPIVMP
2017    m_iSubPULog2Size       [ i ] = 0;
2018#endif
2019#endif
2020#if H_3D_QTLPC
2021    m_bLimQtPredFlag       [ i ] = false;
2022#endif
2023#if H_3D_VSP
2024    m_viewSynthesisPredFlag[ i ] = false;
2025#endif
2026#if H_3D_NBDV_REF
2027    m_depthRefinementFlag  [ i ] = false;
2028#endif
2029#if H_3D_INTER_SDC
2030    m_bInterSDCFlag        [ i ] = false;
2031#endif
2032#if H_3D
2033    m_bIVPFlag             [ i ] = false;
2034#endif
2035#if H_3D_DBBP
2036    m_dbbpFlag             [ i ] = false;
2037#endif
2038#if H_3D_IV_MERGE
2039    m_bMPIFlag             [ i ] = false;
2040#endif
2041  } 
2042#endif
2043#endif
2044
2045#endif
2046}
2047
2048TComVPS::~TComVPS()
2049{
2050  if( m_hrdParameters    != NULL )     delete[] m_hrdParameters;
2051  if( m_hrdOpSetIdx      != NULL )     delete[] m_hrdOpSetIdx;
2052  if( m_cprmsPresentFlag != NULL )     delete[] m_cprmsPresentFlag;
2053#if H_MV
2054  if ( m_vpsVUI          != NULL )     delete m_vpsVUI; 
2055  if ( m_dpbSize         != NULL )     delete m_dpbSize; 
2056
2057  for( Int i = 0; i < MAX_NUM_LAYERS; i++ )
2058  {
2059    if (m_repFormat[ i ] != NULL )      delete m_repFormat[ i ];   
2060  }
2061#endif
2062#if H_3D
2063  deleteCamPars();
2064#endif
2065}
2066
2067#if H_MV
2068
2069Bool TComVPS::checkVPSExtensionSyntax()
2070{
2071  for( Int layer = 1; layer <= getMaxLayersMinus1(); layer++ )
2072  {
2073    // check layer_id_in_nuh constraint
2074    assert( getLayerIdInNuh( layer ) > getLayerIdInNuh( layer -1 ) );
2075  }
2076
2077  //The value of vps_num_rep_formats_minus1 shall be in the range of 0 to 255, inclusive.
2078  assert( getVpsNumRepFormatsMinus1() >= 0 ); 
2079  assert( getVpsNumRepFormatsMinus1() <= 255 ); 
2080
2081  // The value of num_add_layer_sets shall be in the range of 0 to 1023, inclusive.
2082  assert( getNumAddLayerSets() >= 0 && getNumAddLayerSets() <= 1023 ); 
2083  return true; 
2084}
2085
2086Int TComVPS::getNumScalabilityTypes()
2087{
2088  return scalTypeToScalIdx( ScalabilityType(MAX_NUM_SCALABILITY_TYPES) );
2089}
2090
2091Int TComVPS::scalTypeToScalIdx( ScalabilityType scalType )
2092{
2093  assert( scalType >= 0 && scalType <= MAX_NUM_SCALABILITY_TYPES ); 
2094  assert( scalType == MAX_NUM_SCALABILITY_TYPES || getScalabilityMaskFlag( scalType ) );
2095  Int scalIdx = 0; 
2096  for( Int curScalType = 0; curScalType < scalType; curScalType++ )
2097  {
2098    scalIdx += ( getScalabilityMaskFlag( curScalType ) ? 1 : 0 );
2099
2100  }
2101
2102  return scalIdx; 
2103}
2104Void TComVPS::setScalabilityMaskFlag( UInt val )
2105{
2106  for ( Int scalType = 0; scalType < MAX_NUM_SCALABILITY_TYPES; scalType++ ) 
2107  {
2108    setScalabilityMaskFlag( scalType, ( val & (1 << scalType ) ) != 0 );
2109  }
2110}
2111
2112Void TComVPS::setRefLayers()
2113{
2114
2115  for( Int i = 0; i  <=  getMaxLayersMinus1(); i++ )
2116  {
2117    for( Int j = 0; j  <=  getMaxLayersMinus1(); j++ )
2118    {
2119      m_dependencyFlag[ i ][ j ] = getDirectDependencyFlag( i , j );
2120      for( Int k = 0; k < i; k++ )
2121      {
2122        if( getDirectDependencyFlag(i , k )  &&  m_dependencyFlag[k][j] )
2123        {
2124          m_dependencyFlag[ i ][ j ] = true;
2125        }
2126      }
2127    }
2128  }
2129
2130  for( Int i = 0; i  <=  getMaxLayersMinus1(); i++ )
2131  {
2132    Int iNuhLId = getLayerIdInNuh( i );
2133    Int d = 0;
2134#if HHI_DEPENDENCY_SIGNALLING_I1_J0107
2135#if H_3D
2136    Int l = 0; 
2137#endif
2138#endif
2139    Int r = 0;
2140    Int p = 0;
2141
2142    for( Int j = 0; j  <=  getMaxLayersMinus1(); j++ )
2143    {
2144      Int jNuhLid = getLayerIdInNuh( j );
2145      if( getDirectDependencyFlag( i , j ) )
2146      {
2147        m_idDirectRefLayer[iNuhLId][d++] = jNuhLid;
2148      }
2149#if HHI_DEPENDENCY_SIGNALLING_I1_J0107
2150#if H_3D
2151      if( getDirectDependencyFlag( i , j ) && ( getDepthId( iNuhLId ) == getDepthId( jNuhLid ) ))
2152      {
2153        m_idRefListLayer [iNuhLId][l++] = jNuhLid;
2154      }
2155#endif
2156#endif
2157
2158      if( getDependencyFlag( i , j ) )
2159      {
2160        m_idRefLayer      [iNuhLId][r++] = jNuhLid;
2161      }
2162      if( getDependencyFlag( j , i ) )
2163      {
2164        m_idPredictedLayer[iNuhLId][p++] = jNuhLid;
2165      }
2166    }
2167    m_numDirectRefLayers[ iNuhLId ] = d;
2168#if HHI_DEPENDENCY_SIGNALLING_I1_J0107
2169#if H_3D
2170    m_numRefListLayers[ iNuhLId ] = l; 
2171#endif
2172#endif
2173
2174    m_numRefLayers      [ iNuhLId ] = r;
2175    m_numPredictedLayers[ iNuhLId ] = p;
2176  }
2177 
2178  Bool layerIdInListFlag[ 64 ]; 
2179  for( Int i = 0; i  <=  63; i++ )
2180  {
2181    layerIdInListFlag[ i ] = 0;
2182  }
2183
2184  Int k = 0; 
2185  for( Int i = 0; i  <=  getMaxLayersMinus1(); i++ )
2186  {
2187    Int iNuhLId = getLayerIdInNuh( i );
2188    if( getNumDirectRefLayers( iNuhLId )  ==  0 )
2189    {
2190      m_treePartitionLayerIdList[ k ][ 0 ] = iNuhLId;
2191      Int h = 1; 
2192      for( Int j = 0; j < getNumPredictedLayers( iNuhLId ); j++ ) 
2193      {
2194        Int predLId = getIdPredictedLayer( iNuhLId, j );
2195        if ( !layerIdInListFlag[ predLId ] )
2196        {
2197          m_treePartitionLayerIdList[ k ][ h++ ] = predLId;
2198          layerIdInListFlag[ predLId ] = 1; 
2199        }         
2200      }
2201      m_numLayersInTreePartition[ k++ ] = h;
2202    }
2203  }
2204  m_numIndependentLayers = k;
2205}
2206
2207
2208#if H_MV_FIX_NUM_VIEWS
2209Void     TComVPS::initNumViews( )
2210{
2211  m_numViews = 1; 
2212#if HHI_VIEW_ID_LIST_I5_J0107
2213#if H_3D
2214  AOF( m_viewOIdxList.size() == 0 ); 
2215  m_viewOIdxList.push_back( 0 );       
2216#endif
2217#endif
2218
2219  for( Int i = 0; i <=  getMaxLayersMinus1(); i++ )
2220  {
2221    Int lId = getLayerIdInNuh( i ); 
2222    if( i > 0 )
2223    {
2224      Bool newViewFlag = true; 
2225      for( Int j = 0; j < i; j++ )
2226      {
2227        if( getViewOrderIdx( lId )  ==  getViewOrderIdx( getLayerIdInNuh( j ) )  )
2228        {
2229          newViewFlag = false;
2230        }
2231      }
2232      if( newViewFlag )
2233      {
2234        m_numViews++;
2235#if HHI_VIEW_ID_LIST_I5_J0107
2236#if H_3D
2237        m_viewOIdxList.push_back( getViewOrderIdx( lId ) );       
2238#endif
2239#endif
2240      }
2241    }
2242  }
2243}
2244#endif
2245
2246
2247Int TComVPS::getScalabilityId( Int layerIdInVps, ScalabilityType scalType )
2248{
2249  return getScalabilityMaskFlag( scalType ) ? getDimensionId( layerIdInVps, scalTypeToScalIdx( scalType ) ) : 0;
2250}
2251
2252#if H_3D
2253Int TComVPS::getLayerIdInNuh( Int viewIndex, Bool depthFlag )
2254{
2255  Int foundLayerIdinNuh = -1; 
2256
2257  for (Int layerIdInVps = 0 ; layerIdInVps <= getMaxLayersMinus1(); layerIdInVps++ )
2258  {
2259    Int layerIdInNuh = getLayerIdInNuh( layerIdInVps ); 
2260    if( ( getViewIndex( layerIdInNuh ) == viewIndex ) && ( getDepthId( layerIdInNuh ) == ( depthFlag ? 1 : 0 ) )  )
2261    {
2262      foundLayerIdinNuh = layerIdInNuh; 
2263      break; 
2264    }
2265  }
2266#if !BUG_FIX_TK65
2267  assert( foundLayerIdinNuh != -1 ); 
2268#endif
2269  return foundLayerIdinNuh;
2270}
2271
2272Void TComVPS::createCamPars(Int iNumViews)
2273{
2274  Int i = 0, j = 0;
2275
2276  m_bCamParPresent = new Bool[ iNumViews ];
2277  m_bCamParInSliceHeader = new Bool[ iNumViews ];
2278
2279  m_aaaiCodedScale = new Int**[ iNumViews ];
2280  m_aaaiCodedOffset = new Int**[ iNumViews ];
2281  for ( i = 0; i < iNumViews ; i++ )
2282  {
2283    m_bCamParPresent[i] = false; 
2284    m_bCamParInSliceHeader[i] = false; 
2285    m_aaaiCodedScale[i] = new Int*[ 2 ];
2286    m_aaaiCodedOffset[i] = new Int*[ 2 ];
2287    for ( j = 0; j < 2; j++ )
2288    {
2289      m_aaaiCodedScale[i][j] = new Int[ MAX_NUM_LAYERS ];
2290      m_aaaiCodedOffset[i][j] = new Int[ MAX_NUM_LAYERS ];
2291      for ( Int k = 0; k < MAX_NUM_LAYERS; k++ )
2292      {
2293        m_aaaiCodedScale[i][j][k] = 0;
2294        m_aaaiCodedOffset[i][j][k] = 0;
2295      }
2296    }
2297  }
2298}
2299
2300Void TComVPS::deleteCamPars()
2301{
2302  Int iNumViews = getNumViews();
2303  Int i = 0, j = 0;
2304
2305  if ( m_bCamParPresent != NULL )
2306  {
2307    delete [] m_bCamParPresent;
2308  }
2309  if ( m_bCamParInSliceHeader != NULL )
2310  {
2311    delete [] m_bCamParInSliceHeader;
2312  }
2313
2314  if ( m_aaaiCodedScale != NULL )
2315  {
2316    for ( i = 0; i < iNumViews ; i++ )
2317    {
2318      for ( j = 0; j < 2; j++ )
2319      {
2320        delete [] m_aaaiCodedScale[i][j];
2321      }
2322      delete [] m_aaaiCodedScale[i];
2323    }
2324    delete [] m_aaaiCodedScale;
2325  }
2326
2327  if ( m_aaaiCodedOffset != NULL )
2328  {
2329    for ( i = 0; i < iNumViews ; i++ )
2330    {
2331      for ( j = 0; j < 2; j++ )
2332      {
2333        delete [] m_aaaiCodedOffset[i][j];
2334      }
2335      delete [] m_aaaiCodedOffset[i];
2336    }
2337    delete [] m_aaaiCodedOffset;
2338  }
2339}
2340
2341
2342Void
2343  TComVPS::initCamParaVPS( UInt uiViewIndex, Bool bCamParPresent, UInt uiCamParPrecision, Bool bCamParSlice, Int** aaiScale, Int** aaiOffset )
2344{
2345  AOT( uiViewIndex != 0 && !bCamParSlice && ( aaiScale == 0 || aaiOffset == 0 ) ); 
2346
2347  m_uiCamParPrecision = ( ( uiViewIndex != 0 )? uiCamParPrecision : 0 );
2348  m_bCamParPresent[ uiViewIndex ] = (( uiViewIndex != 0 )? bCamParPresent  : false );
2349  m_bCamParInSliceHeader[ uiViewIndex ]  = ( (uiViewIndex != 0)? bCamParSlice  : false );
2350
2351  if( !m_bCamParInSliceHeader[ uiViewIndex ] )
2352  {
2353    for( UInt uiBaseViewIndex = 0; uiBaseViewIndex < uiViewIndex; uiBaseViewIndex++ )
2354    {
2355      m_aaaiCodedScale [ uiViewIndex ][ 0 ][ uiBaseViewIndex ] = aaiScale [ uiBaseViewIndex ][     uiViewIndex ];
2356      m_aaaiCodedScale [ uiViewIndex ][ 1 ][ uiBaseViewIndex ] = aaiScale [     uiViewIndex ][ uiBaseViewIndex ];
2357      m_aaaiCodedOffset[ uiViewIndex ][ 0 ][ uiBaseViewIndex ] = aaiOffset[ uiBaseViewIndex ][     uiViewIndex ];
2358      m_aaaiCodedOffset[ uiViewIndex ][ 1 ][ uiBaseViewIndex ] = aaiOffset[     uiViewIndex ][ uiBaseViewIndex ];
2359    }
2360  }
2361}
2362
2363#endif // H_3D
2364
2365
2366Int TComVPS::xGetDimBitOffset( Int j )
2367{
2368  Int dimBitOffset = 0; 
2369  if ( getSplittingFlag() && j == getNumScalabilityTypes() )
2370  {
2371     dimBitOffset = 6; 
2372  }
2373  else
2374  {
2375    for (Int dimIdx = 0; dimIdx <= j-1; dimIdx++)
2376    {
2377      dimBitOffset += getDimensionIdLen( dimIdx ); 
2378    }
2379  }
2380  return dimBitOffset; 
2381}
2382
2383Int TComVPS::inferDimensionId( Int i, Int j )
2384{
2385    return ( ( getLayerIdInNuh( i ) & ( (1 << xGetDimBitOffset( j + 1 ) ) - 1) ) >> xGetDimBitOffset( j ) ); 
2386}
2387
2388Int TComVPS::inferLastDimsionIdLenMinus1()
2389{
2390  return ( 5 - xGetDimBitOffset( getNumScalabilityTypes() - 1 ) ); 
2391}
2392
2393Int TComVPS::getNumLayersInIdList( Int lsIdx )
2394{
2395  assert( lsIdx >= 0 ); 
2396  assert( lsIdx <= getNumLayerSets() ); 
2397  return (Int) m_layerSetLayerIdList[ lsIdx ].size(); 
2398}
2399
2400Int    TComVPS::getNumOutputLayerSets() 
2401{
2402  return getNumAddOlss() + getNumLayerSets(); 
2403}
2404
2405#if !H_MV_FIX_NUM_VIEWS
2406Int TComVPS::getNumViews()
2407{
2408  Int numViews = 1; 
2409  for( Int i = 0; i <=  getMaxLayersMinus1(); i++ )
2410  {
2411    Int lId = getLayerIdInNuh( i ); 
2412    if ( i > 0 && ( getViewIndex( lId ) != getScalabilityId( i - 1, VIEW_ORDER_INDEX ) ) )
2413    {
2414      numViews++; 
2415    }   
2416  }
2417  return numViews;
2418}
2419#endif
2420
2421
2422Void TComVPS::deriveLayerSetLayerIdList()
2423{
2424  m_layerSetLayerIdList.resize( getVpsNumLayerSetsMinus1() + 1 ); 
2425  for (Int i = 0; i <= getVpsNumLayerSetsMinus1(); i++ )
2426  {
2427    for( Int m = 0; m  <= getVpsMaxLayerId(); m++ )
2428    {
2429      if( getLayerIdIncludedFlag( i, m) ) 
2430      {
2431        m_layerSetLayerIdList[ i ].push_back( m );       
2432      }
2433    }
2434  }
2435}
2436
2437Void TComVPS::initTargetLayerIdLists()
2438{
2439  m_targetDecLayerIdLists.resize( getNumOutputLayerSets() ); 
2440  m_targetOptLayerIdLists.resize( getNumOutputLayerSets() ); 
2441}
2442
2443Void TComVPS::deriveTargetLayerIdList( Int i )
2444{ 
2445  Int lsIdx = olsIdxToLsIdx( i );     
2446
2447  for( Int j = 0; j < getNumLayersInIdList( lsIdx ); j++ )
2448  {
2449    if ( getNecessaryLayerFlag( i , j ))
2450    {
2451      m_targetDecLayerIdLists[i].push_back( m_layerSetLayerIdList[ lsIdx ][ j ] ); 
2452    }
2453
2454    if( getOutputLayerFlag( i, j  ))
2455    {
2456      m_targetOptLayerIdLists[i].push_back( m_layerSetLayerIdList[ lsIdx ][ j ] );
2457    }
2458  } 
2459  assert( getNumOutputLayersInOutputLayerSet( i ) > 0 ); 
2460}
2461
2462Bool TComVPS::inferOutputLayerFlag( Int i, Int j )
2463{
2464  Bool outputLayerFlag; 
2465  switch ( getDefaultOutputLayerIdc( ) )
2466  {
2467  case 0:
2468    outputLayerFlag = true; 
2469    break; 
2470  case 1:
2471    outputLayerFlag = ( j == m_layerSetLayerIdList[ olsIdxToLsIdx( i ) ].size() - 1 ); 
2472    break;
2473  case 2:
2474    if ( i == 0 && j == 0)
2475    {     
2476      outputLayerFlag = true;  // This is a software only fix for a bug in the spec. In spec outputLayerFlag is neither present nor inferred.
2477    }
2478    else
2479    {
2480      assert( 0 ); 
2481    }
2482    break; 
2483  default:     
2484    assert( 0 );
2485    break; 
2486  }
2487  return outputLayerFlag;
2488}
2489
2490Int TComVPS::getMaxSubLayersInLayerSetMinus1( Int i )
2491{
2492  Int maxSLMinus1 = 0; 
2493  for( Int k = 0; k < getNumLayersInIdList( i ); k++ )
2494  {
2495    Int lId = m_layerSetLayerIdList[i][k];
2496    maxSLMinus1 = std::max( maxSLMinus1, getSubLayersVpsMaxMinus1( getLayerIdInVps( lId ) ));
2497  }
2498  return maxSLMinus1;
2499}
2500
2501Bool TComVPS::getAltOutputLayerFlagVar( Int i )
2502{
2503  // Semantics variable not syntax element !
2504
2505  Bool altOptLayerFlag = false;     
2506  if ( i > 0 && getNumOutputLayersInOutputLayerSet( i ) == 1 && 
2507    getNumDirectRefLayers( getOlsHighestOutputLayerId( i ) ) > 0 )
2508  {
2509    altOptLayerFlag = getAltOutputLayerFlag( i ); 
2510  }
2511  return altOptLayerFlag;
2512}
2513
2514
2515
2516Int TComVPS::inferProfileTierLevelIdx(Int i, Int j)
2517{
2518  Bool inferZero        = ( i == 0 && j == 0 &&  getVpsBaseLayerInternalFlag() );
2519  Bool inferGreaterZero = getNecessaryLayerFlag(i,j) && ( getVpsNumProfileTierLevelMinus1() == 0 ); 
2520  assert( inferZero || inferGreaterZero );
2521
2522  Bool ptlIdx = 0; // inference for greaterZero
2523  if ( inferZero )
2524  {
2525    ptlIdx = getMaxLayersMinus1() > 0 ? 1 : 0; 
2526    if ( inferGreaterZero )
2527    {
2528      assert( ptlIdx == 0 ); 
2529      // This should never happen since :
2530      // 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.
2531    }
2532  }
2533  return ptlIdx;
2534}
2535
2536Void TComVPS::deriveAddLayerSetLayerIdList(Int i)
2537{
2538  assert( m_layerSetLayerIdList.size() ==  ( getVpsNumLayerSetsMinus1() + 1 + i ) ); 
2539  std::vector<Int> layerSetLayerIdList;
2540
2541  for( Int treeIdx = 1; treeIdx < getNumIndependentLayers(); treeIdx++ )
2542  { 
2543    // The value of highest_layer_idx_plus1[ i ][ j ] shall be in the range of 0 to NumLayersInTreePartition[ j ], inclusive.
2544    assert( getHighestLayerIdxPlus1( i, treeIdx ) >= 0 && getHighestLayerIdxPlus1( i, treeIdx ) <= getNumLayersInTreePartition( treeIdx ) );
2545
2546    for( Int layerCnt = 0; layerCnt < getHighestLayerIdxPlus1( i, treeIdx ); layerCnt++ )
2547    {
2548      layerSetLayerIdList.push_back( getTreePartitionLayerIdList( treeIdx, layerCnt ) );
2549    }
2550  }
2551  m_layerSetLayerIdList.push_back( layerSetLayerIdList ); 
2552
2553  //It is a requirement of bitstream conformance that
2554  //NumLayersInIdList[ vps_num_layer_sets_minus1 + 1 + i ] shall be greater than 0.
2555  assert( getNumLayersInIdList( getVpsNumLayerSetsMinus1() + 1 + i ) > 0 );
2556}
2557
2558
2559Void TComVPS::deriveNecessaryLayerFlags(Int olsIdx)
2560{
2561  AOF( olsIdx >= 0 && olsIdx < getNumOutputLayerSets() ); 
2562  Int lsIdx = olsIdxToLsIdx( olsIdx );
2563  for( Int lsLayerIdx = 0; lsLayerIdx < getNumLayersInIdList( lsIdx) ; lsLayerIdx++ )
2564  {
2565    m_necessaryLayerFlag[ olsIdx ][ lsLayerIdx ] = 0;
2566  }
2567  for( Int lsLayerIdx = 0; lsLayerIdx < getNumLayersInIdList( lsIdx ); lsLayerIdx++ )
2568  {
2569    if( getOutputLayerFlag( olsIdx, lsLayerIdx  ))
2570    {
2571      m_necessaryLayerFlag[ olsIdx ][ lsLayerIdx ] = 1;
2572      Int currLayerId = getLayerSetLayerIdList( lsIdx, lsLayerIdx );
2573      for( Int rLsLayerIdx = 0; rLsLayerIdx < lsLayerIdx; rLsLayerIdx++ )
2574      {
2575        Int refLayerId = getLayerSetLayerIdList( lsIdx, rLsLayerIdx );
2576        if( getDependencyFlag( getLayerIdInVps( currLayerId ), getLayerIdInVps( refLayerId ) ) )
2577        {
2578          m_necessaryLayerFlag[ olsIdx ][ rLsLayerIdx ] = 1;
2579        }
2580      }
2581    }
2582  }
2583  m_numNecessaryLayers[ olsIdx ] = 0;
2584  for( Int lsLayerIdx = 0; lsLayerIdx < getNumLayersInIdList( lsIdx ); lsLayerIdx++ ) 
2585  {
2586    m_numNecessaryLayers[ olsIdx ]  +=  m_necessaryLayerFlag[ olsIdx ][ lsLayerIdx ];
2587  }
2588}
2589
2590Void TComVPS::printPTL()
2591{
2592  std::vector<Int> idx; 
2593  std::vector<Int> num; 
2594  std::vector< std::vector<Int> > ptlInfo; 
2595
2596  std::cout << std::right << std::setw(60) << std::setfill('-') << " " << std::setfill(' ') << std::endl << "PTLI" << std::endl; 
2597
2598  for ( Int i = 0; i <= getVpsNumProfileTierLevelMinus1(); i++ )
2599  {
2600    std::vector<Int> curPtlInfo;
2601    ProfileTierLevel* ptl = getPTL( i )->getGeneralPTL(); 
2602    curPtlInfo.push_back( (Int) ptl->getProfileIdc()  );
2603    curPtlInfo.push_back( (Int) ptl->getTierFlag()    );
2604    curPtlInfo.push_back( (Int) ptl->getLevelIdc()    );
2605    curPtlInfo.push_back( (Int) ptl->getInbldFlag()   );
2606
2607    idx.push_back ( i );
2608    num.push_back ( 4 ); 
2609    ptlInfo.push_back( curPtlInfo );
2610  } 
2611
2612  xPrintArray( "VpsProfileTierLevel", getVpsNumProfileTierLevelMinus1() + 1, idx, num, ptlInfo, false  ); 
2613
2614  num.clear(); 
2615  idx.clear(); 
2616  for (Int i = 0; i < getNumOutputLayerSets(); i++)
2617  {
2618    num.push_back ( getNumLayersInIdList( olsIdxToLsIdx( i ))  ); 
2619    idx.push_back( i ); 
2620  }
2621
2622  xPrintArray( "profile_tier_level_idx", getNumOutputLayerSets(), idx, num, m_profileTierLevelIdx, true );
2623  std::cout << std::endl;
2624}
2625
2626Void TComVPS::printLayerDependencies()
2627{
2628  vector<Int> fullArray;
2629  vector<Int> range; 
2630
2631#if H_3D
2632  vector<Int> depthId; 
2633#endif
2634
2635  vector<Int> viewOrderIndex;
2636  vector<Int> auxId;
2637  vector<Int> dependencyId; 
2638  vector<Int> viewId; 
2639  for (Int i = 0; i <= getMaxLayersMinus1(); i++ )
2640  {
2641    fullArray.push_back( getMaxLayersMinus1() + 1 ); 
2642    range.push_back( i ); 
2643    viewOrderIndex.push_back( getViewIndex   ( i ) );
2644    dependencyId  .push_back( getDependencyId( i ) );
2645    auxId         .push_back( getAuxId       ( i ) );     
2646    viewId        .push_back( getViewId      ( getLayerIdInNuh( i ) ) );
2647#if H_3D 
2648    depthId.push_back( getDepthId( i ) );
2649#endif
2650  }
2651  std::cout << std::right << std::setw(60) << std::setfill('-') << " " << std::setfill(' ') << std::endl << "Layer Dependencies" << std::endl; 
2652  xPrintArray( "direct_dependency_flag", getMaxLayersMinus1()+1, range, fullArray, m_directDependencyFlag, false ); 
2653  xPrintArray( "DependencyFlag", getMaxLayersMinus1()+1, range, fullArray, m_dependencyFlag, false ); 
2654  xPrintArray( "layer_id_in_nuh", getMaxLayersMinus1()+1, m_layerIdInNuh, true  );     
2655  xPrintArray( "IdPredictedLayer", getMaxLayersMinus1() + 1, m_layerIdInNuh, m_numPredictedLayers, m_idPredictedLayer, true );
2656  xPrintArray( "IdRefLayer"      , getMaxLayersMinus1() + 1, m_layerIdInNuh, m_numRefLayers, m_idRefLayer, true );
2657  xPrintArray( "IdDirectRefLayer", getMaxLayersMinus1() + 1, m_layerIdInNuh, m_numDirectRefLayers, m_idDirectRefLayer, true );
2658#if HHI_DEPENDENCY_SIGNALLING_I1_J0107
2659#if H_3D
2660  xPrintArray( "IdRefListLayer", getMaxLayersMinus1() + 1, m_layerIdInNuh, m_numRefListLayers, m_idRefListLayer, true );
2661#endif
2662#endif
2663
2664  std::cout << std::endl;
2665}
2666
2667Void TComVPS::printScalabilityId()
2668{
2669  vector<Int> layerIdxInVps; 
2670
2671#if H_3D
2672  vector<Int> depthId; 
2673#endif
2674
2675  vector<Int> viewOrderIndex;
2676  vector<Int> auxId;
2677  vector<Int> dependencyId; 
2678  vector<Int> viewId; 
2679
2680  for (Int i = 0; i <= getMaxLayersMinus1(); i++ )
2681  {
2682    Int layerIdInNuh = getLayerIdInNuh( i );
2683    layerIdxInVps  .push_back( i ); 
2684    viewOrderIndex.push_back( getViewIndex   ( layerIdInNuh ) );
2685    dependencyId  .push_back( getDependencyId( layerIdInNuh ) );
2686    auxId         .push_back( getAuxId       ( layerIdInNuh ) );     
2687    viewId        .push_back( getViewId      ( layerIdInNuh ) );
2688#if H_3D 
2689    depthId       .push_back( getDepthId     ( layerIdInNuh ) );
2690#endif
2691  }
2692
2693  std::cout << std::right << std::setw(60) << std::setfill('-') << " " << std::setfill(' ') << std::endl << "Scalability Ids" << std::endl; 
2694  xPrintArray( "layerIdxInVps"  , getMaxLayersMinus1()+1, layerIdxInVps,          false );
2695  xPrintArray( "layer_id_in_nuh", getMaxLayersMinus1()+1, m_layerIdInNuh, false );     
2696
2697  xPrintArray( "ViewOrderIndex", getMaxLayersMinus1()+1, viewOrderIndex, false );     
2698  xPrintArray( "DependencyId"  , getMaxLayersMinus1()+1, dependencyId  , false );     
2699  xPrintArray( "AuxId"         , getMaxLayersMinus1()+1, auxId         , false );     
2700  xPrintArray( "ViewId"        , getMaxLayersMinus1()+1, viewId        , false );     
2701
2702  std::cout << std::endl;
2703}
2704
2705Void TComVPS::printLayerSets()
2706{
2707  vector<Int> fullArray;
2708  vector<Int> numLayersInIdList; 
2709  vector<Int> rangeLayerSets; 
2710
2711
2712  for (Int i = 0; i < getNumLayerSets(); i++ )
2713  {
2714    numLayersInIdList.push_back( getNumLayersInIdList( i ) );       
2715    rangeLayerSets.push_back( i ); 
2716  }
2717
2718  vector<Int> rangeOutputLayerSets; 
2719  vector<Int> numOutputLayersInOutputLayerSet; 
2720  vector<Int> numDecLayer; 
2721  vector<Int> numLayersInLayerSetForOutputLayerSet; 
2722  vector<Int> vOlsIdxToLsIdx;
2723  for (Int i = 0; i < getNumOutputLayerSets(); i++ )
2724  {
2725    vOlsIdxToLsIdx.push_back( olsIdxToLsIdx(i));
2726    numOutputLayersInOutputLayerSet.push_back( getNumOutputLayersInOutputLayerSet( i ) );       
2727    numDecLayer.push_back( (Int) m_targetDecLayerIdLists[ i ].size() );
2728    rangeOutputLayerSets.push_back( i ); 
2729    numLayersInLayerSetForOutputLayerSet.push_back( getNumLayersInIdList( olsIdxToLsIdx( i ) ) );
2730  }
2731
2732  vector<Int> rangeIndependentLayers;
2733  for(Int i = 0; i < getNumIndependentLayers(); i++ )
2734  {
2735    rangeIndependentLayers.push_back( i );   
2736  }
2737
2738  vector<Int> rangeAddLayerSets;
2739  vector<Int> numHighestLayerIdxPlus1; 
2740  for(Int i = 0; i < getNumAddLayerSets(); i++ )
2741  {
2742    rangeAddLayerSets.push_back( i );   
2743    numHighestLayerIdxPlus1.push_back( getNumIndependentLayers() );
2744  }
2745
2746  std::cout << std::right << std::setw(60) << std::setfill('-') << " " << std::setfill(' ') << std::endl << "Layer Sets" << std::endl;     
2747  xPrintArray( "TreePartitionLayerIdList", getNumIndependentLayers(), rangeIndependentLayers, m_numLayersInTreePartition, m_treePartitionLayerIdList, true );
2748  xPrintArray( "highest_layer_idx_plus1", getNumAddLayerSets(), rangeAddLayerSets, numHighestLayerIdxPlus1, m_highestLayerIdxPlus1, true ); 
2749  xPrintArray( "LayerSetLayerIdList" , (Int) getNumLayerSets()      , rangeLayerSets      , numLayersInIdList, m_layerSetLayerIdList, true );
2750  xPrintArray( "OlsIdxToLsIdx", (Int) vOlsIdxToLsIdx.size(), vOlsIdxToLsIdx, true ); 
2751  xPrintArray( "OutputLayerFlag"     , getNumOutputLayerSets(), rangeOutputLayerSets, numLayersInLayerSetForOutputLayerSet, m_outputLayerFlag, true );
2752  xPrintArray( "TargetOptLayerIdList", getNumOutputLayerSets(), rangeOutputLayerSets, numOutputLayersInOutputLayerSet, m_targetOptLayerIdLists, true );
2753  xPrintArray( "NecessaryLayerFlag"  , getNumOutputLayerSets(), rangeOutputLayerSets, numLayersInLayerSetForOutputLayerSet, m_necessaryLayerFlag   , true );
2754  xPrintArray( "TargetDecLayerIdList", getNumOutputLayerSets(), rangeOutputLayerSets, numDecLayer,                     m_targetDecLayerIdLists, true );
2755  std::cout << endl;
2756}
2757
2758
2759#endif // H_MV
2760
2761// ------------------------------------------------------------------------------------------------
2762// Sequence parameter set (SPS)
2763// ------------------------------------------------------------------------------------------------
2764
2765TComSPS::TComSPS()
2766: m_SPSId                     (  0)
2767, m_VPSId                     (  0)
2768, m_chromaFormatIdc           (CHROMA_420)
2769, m_uiMaxTLayers              (  1)
2770// Structure
2771, m_picWidthInLumaSamples     (352)
2772, m_picHeightInLumaSamples    (288)
2773, m_log2MinCodingBlockSize    (  0)
2774, m_log2DiffMaxMinCodingBlockSize (0)
2775, m_uiMaxCUWidth              ( 32)
2776, m_uiMaxCUHeight             ( 32)
2777, m_uiMaxCUDepth              (  3)
2778, m_bLongTermRefsPresent      (false)
2779, m_uiQuadtreeTULog2MaxSize   (  0)
2780, m_uiQuadtreeTULog2MinSize   (  0)
2781, m_uiQuadtreeTUMaxDepthInter (  0)
2782, m_uiQuadtreeTUMaxDepthIntra (  0)
2783// Tool list
2784, m_usePCM                   (false)
2785, m_pcmLog2MaxSize            (  5)
2786, m_uiPCMLog2MinSize          (  7)
2787, m_bitDepthY                 (  8)
2788, m_bitDepthC                 (  8)
2789, m_qpBDOffsetY               (  0)
2790, m_qpBDOffsetC               (  0)
2791, m_uiPCMBitDepthLuma         (  8)
2792, m_uiPCMBitDepthChroma       (  8)
2793, m_bPCMFilterDisableFlag     (false)
2794, m_uiBitsForPOC              (  8)
2795, m_numLongTermRefPicSPS    (  0) 
2796, m_uiMaxTrSize               ( 32)
2797, m_bUseSAO                   (false) 
2798, m_bTemporalIdNestingFlag    (false)
2799, m_scalingListEnabledFlag    (false)
2800, m_useStrongIntraSmoothing   (false)
2801, m_vuiParametersPresentFlag  (false)
2802, m_vuiParameters             ()
2803#if H_MV
2804, m_pcVPS                     ( NULL )
2805, m_spsInferScalingListFlag   ( false )
2806, m_spsScalingListRefLayerId  ( 0 )
2807
2808, m_updateRepFormatFlag       ( false ) 
2809, m_spsRepFormatIdx           ( 0 )
2810, m_interViewMvVertConstraintFlag (false)
2811#endif
2812#if H_3D
2813, m_bCamParInSliceHeader      (false)
2814#endif
2815{
2816  for ( Int i = 0; i < MAX_TLAYER; i++ )
2817  {
2818    m_uiMaxLatencyIncrease[i] = 0;
2819    m_uiMaxDecPicBuffering[i] = 1;
2820    m_numReorderPics[i]       = 0;
2821  }
2822  m_scalingList = new TComScalingList;
2823  ::memset(m_ltRefPicPocLsbSps, 0, sizeof(m_ltRefPicPocLsbSps));
2824  ::memset(m_usedByCurrPicLtSPSFlag, 0, sizeof(m_usedByCurrPicLtSPSFlag));
2825#if H_MV
2826  m_spsRangeExtensionsFlag     = false;
2827  m_spsMultilayerExtensionFlag = false;
2828#if H_3D
2829  m_spsExtension5bits          = 0;
2830  m_sps3dExtensionFlag         = false; 
2831#else
2832  m_spsExtension6bits          = 0;
2833#endif
2834
2835#endif
2836}
2837
2838TComSPS::~TComSPS()
2839{
2840  delete m_scalingList;
2841  m_RPSList.destroy();
2842}
2843
2844Void  TComSPS::createRPSList( Int numRPS )
2845{ 
2846  m_RPSList.destroy();
2847  m_RPSList.create(numRPS);
2848}
2849
2850Void TComSPS::setHrdParameters( UInt frameRate, UInt numDU, UInt bitRate, Bool randomAccess )
2851{
2852  if( !getVuiParametersPresentFlag() )
2853  {
2854    return;
2855  }
2856
2857  TComVUI *vui = getVuiParameters();
2858  TComHRD *hrd = vui->getHrdParameters();
2859
2860  TimingInfo *timingInfo = vui->getTimingInfo();
2861  timingInfo->setTimingInfoPresentFlag( true );
2862  switch( frameRate )
2863  {
2864  case 24:
2865    timingInfo->setNumUnitsInTick( 1125000 );    timingInfo->setTimeScale    ( 27000000 );
2866    break;
2867  case 25:
2868    timingInfo->setNumUnitsInTick( 1080000 );    timingInfo->setTimeScale    ( 27000000 );
2869    break;
2870  case 30:
2871    timingInfo->setNumUnitsInTick( 900900 );     timingInfo->setTimeScale    ( 27000000 );
2872    break;
2873  case 50:
2874    timingInfo->setNumUnitsInTick( 540000 );     timingInfo->setTimeScale    ( 27000000 );
2875    break;
2876  case 60:
2877    timingInfo->setNumUnitsInTick( 450450 );     timingInfo->setTimeScale    ( 27000000 );
2878    break;
2879  default:
2880    timingInfo->setNumUnitsInTick( 1001 );       timingInfo->setTimeScale    ( 60000 );
2881    break;
2882  }
2883
2884  Bool rateCnt = ( bitRate > 0 );
2885  hrd->setNalHrdParametersPresentFlag( rateCnt );
2886  hrd->setVclHrdParametersPresentFlag( rateCnt );
2887
2888  hrd->setSubPicCpbParamsPresentFlag( ( numDU > 1 ) );
2889
2890  if( hrd->getSubPicCpbParamsPresentFlag() )
2891  {
2892    hrd->setTickDivisorMinus2( 100 - 2 );                          //
2893    hrd->setDuCpbRemovalDelayLengthMinus1( 7 );                    // 8-bit precision ( plus 1 for last DU in AU )
2894    hrd->setSubPicCpbParamsInPicTimingSEIFlag( true );
2895    hrd->setDpbOutputDelayDuLengthMinus1( 5 + 7 );                 // With sub-clock tick factor of 100, at least 7 bits to have the same value as AU dpb delay
2896  }
2897  else
2898  {
2899    hrd->setSubPicCpbParamsInPicTimingSEIFlag( false ); 
2900  }
2901
2902  hrd->setBitRateScale( 4 );                                       // in units of 2~( 6 + 4 ) = 1,024 bps
2903  hrd->setCpbSizeScale( 6 );                                       // in units of 2~( 4 + 4 ) = 1,024 bit
2904  hrd->setDuCpbSizeScale( 6 );                                       // in units of 2~( 4 + 4 ) = 1,024 bit
2905 
2906  hrd->setInitialCpbRemovalDelayLengthMinus1(15);                  // assuming 0.5 sec, log2( 90,000 * 0.5 ) = 16-bit
2907  if( randomAccess )
2908  {
2909    hrd->setCpbRemovalDelayLengthMinus1(5);                        // 32 = 2^5 (plus 1)
2910    hrd->setDpbOutputDelayLengthMinus1 (5);                        // 32 + 3 = 2^6
2911  }
2912  else
2913  {
2914    hrd->setCpbRemovalDelayLengthMinus1(9);                        // max. 2^10
2915    hrd->setDpbOutputDelayLengthMinus1 (9);                        // max. 2^10
2916  }
2917
2918/*
2919   Note: only the case of "vps_max_temporal_layers_minus1 = 0" is supported.
2920*/
2921  Int i, j;
2922  UInt birateValue, cpbSizeValue;
2923  UInt ducpbSizeValue;
2924  UInt duBitRateValue = 0;
2925
2926  for( i = 0; i < MAX_TLAYER; i ++ )
2927  {
2928    hrd->setFixedPicRateFlag( i, 1 );
2929    hrd->setPicDurationInTcMinus1( i, 0 );
2930    hrd->setLowDelayHrdFlag( i, 0 );
2931    hrd->setCpbCntMinus1( i, 0 );
2932
2933    birateValue  = bitRate;
2934    cpbSizeValue = bitRate;                                     // 1 second
2935    ducpbSizeValue = bitRate/numDU;
2936    duBitRateValue = bitRate;
2937    for( j = 0; j < ( hrd->getCpbCntMinus1( i ) + 1 ); j ++ )
2938    {
2939      hrd->setBitRateValueMinus1( i, j, 0, ( birateValue  - 1 ) );
2940      hrd->setCpbSizeValueMinus1( i, j, 0, ( cpbSizeValue - 1 ) );
2941      hrd->setDuCpbSizeValueMinus1( i, j, 0, ( ducpbSizeValue - 1 ) );
2942      hrd->setCbrFlag( i, j, 0, ( j == 0 ) );
2943
2944      hrd->setBitRateValueMinus1( i, j, 1, ( birateValue  - 1) );
2945      hrd->setCpbSizeValueMinus1( i, j, 1, ( cpbSizeValue - 1 ) );
2946      hrd->setDuCpbSizeValueMinus1( i, j, 1, ( ducpbSizeValue - 1 ) );
2947      hrd->setDuBitRateValueMinus1( i, j, 1, ( duBitRateValue - 1 ) );
2948      hrd->setCbrFlag( i, j, 1, ( j == 0 ) );
2949    }
2950  }
2951}
2952const Int TComSPS::m_winUnitX[]={1,2,2,1};
2953const Int TComSPS::m_winUnitY[]={1,2,1,1};
2954
2955TComPPS::TComPPS()
2956: m_PPSId                       (0)
2957, m_SPSId                       (0)
2958, m_picInitQPMinus26            (0)
2959, m_useDQP                      (false)
2960, m_bConstrainedIntraPred       (false)
2961, m_bSliceChromaQpFlag          (false)
2962, m_pcSPS                       (NULL)
2963, m_uiMaxCuDQPDepth             (0)
2964, m_uiMinCuDQPSize              (0)
2965, m_chromaCbQpOffset            (0)
2966, m_chromaCrQpOffset            (0)
2967, m_numRefIdxL0DefaultActive    (1)
2968, m_numRefIdxL1DefaultActive    (1)
2969, m_TransquantBypassEnableFlag  (false)
2970, m_useTransformSkip             (false)
2971, m_dependentSliceSegmentsEnabledFlag    (false)
2972, m_tilesEnabledFlag               (false)
2973, m_entropyCodingSyncEnabledFlag   (false)
2974, m_loopFilterAcrossTilesEnabledFlag  (true)
2975, m_uniformSpacingFlag           (false)
2976, m_numTileColumnsMinus1         (0)
2977, m_numTileRowsMinus1            (0)
2978, m_numSubstreams               (1)
2979, m_signHideFlag(0)
2980, m_cabacInitPresentFlag        (false)
2981, m_encCABACTableIdx            (I_SLICE)
2982, m_sliceHeaderExtensionPresentFlag    (false)
2983, m_loopFilterAcrossSlicesEnabledFlag (false)
2984, m_listsModificationPresentFlag(  0)
2985, m_numExtraSliceHeaderBits(0)
2986#if H_MV
2987, m_ppsInferScalingListFlag(false)
2988, m_ppsScalingListRefLayerId(0)
2989, m_pocResetInfoPresentFlag(false)
2990#if H_3D
2991, m_pcDLT(NULL)
2992#endif
2993#endif
2994{
2995  m_scalingList = new TComScalingList;
2996
2997#if H_MV
2998  m_ppsRangeExtensionsFlag     = false;
2999  m_ppsMultilayerExtensionFlag = false;
3000#if !H_3D
3001  m_ppsExtension6bits          = 0;
3002#else
3003  m_pps3dExtensionFlag         = false;
3004  m_ppsExtension5bits          = 0;
3005#endif
3006#endif
3007}
3008
3009TComPPS::~TComPPS()
3010{
3011  delete m_scalingList;
3012}
3013
3014#if H_3D
3015TComDLT::TComDLT()
3016: m_bDltPresentFlag(false)
3017, m_iNumDepthViews(0)
3018, m_uiDepthViewBitDepth(8)
3019{
3020  m_uiDepthViewBitDepth = g_bitDepthY; 
3021
3022  for( Int i = 0; i < MAX_NUM_LAYERS; i++ )
3023  {
3024    m_bUseDLTFlag                 [i] = false;
3025    m_bInterViewDltPredEnableFlag [i] = false;
3026
3027    // allocate some memory and initialize with default mapping
3028    m_iNumDepthmapValues[i] = ((1 << m_uiDepthViewBitDepth)-1)+1;
3029    m_iDepthValue2Idx[i]    = (Int*) xMalloc(Int, m_iNumDepthmapValues[i]);
3030    m_iIdx2DepthValue[i]    = (Int*) xMalloc(Int, m_iNumDepthmapValues[i]);
3031
3032    //default mapping
3033    for (Int d=0; d<m_iNumDepthmapValues[i]; d++)
3034    {
3035      m_iDepthValue2Idx[i][d] = d;
3036      m_iIdx2DepthValue[i][d] = d;
3037    }
3038  }
3039}
3040
3041TComDLT::~TComDLT()
3042{
3043  for( Int i = 0; i < MAX_NUM_LAYERS; i++ )
3044  {
3045    if ( m_iDepthValue2Idx[i] != NULL ) 
3046    {
3047      xFree( m_iDepthValue2Idx[i] );
3048      m_iDepthValue2Idx[i] = NULL; 
3049    }
3050
3051    if ( m_iIdx2DepthValue[i] != NULL ) 
3052    {
3053      xFree( m_iIdx2DepthValue[i] );
3054      m_iIdx2DepthValue[i] = NULL; 
3055    }
3056  }
3057}
3058
3059Void TComDLT::setDepthLUTs(Int layerIdInVps, Int* idxToDepthValueTable, Int iNumDepthValues)
3060{
3061  if( idxToDepthValueTable == NULL || iNumDepthValues == 0 ) // default mapping only
3062    return;
3063
3064  // copy idx2DepthValue to internal array
3065  memcpy(m_iIdx2DepthValue[layerIdInVps], idxToDepthValueTable, iNumDepthValues*sizeof(UInt));
3066
3067  UInt uiMaxDepthValue = ((1 << g_bitDepthY)-1);
3068  for(Int p=0; p<=uiMaxDepthValue; p++)
3069  {
3070    Int iIdxDown    = 0;
3071    Int iIdxUp      = iNumDepthValues-1;
3072    Bool bFound     = false;
3073
3074    // iterate over indices to find lower closest depth
3075    Int i = 1;
3076    while(!bFound && i<iNumDepthValues)
3077    {
3078      if( m_iIdx2DepthValue[layerIdInVps][i] > p )
3079      {
3080        iIdxDown  = i-1;
3081        bFound    = true;
3082      }
3083
3084      i++;
3085    }
3086#if SHARP_DLT_SIMP_J0029
3087    iIdxUp = bFound ?  iIdxDown + 1 : iNumDepthValues-1;
3088#else
3089    // iterate over indices to find upper closest depth
3090    i = iNumDepthValues-2;
3091    bFound = false;
3092    while(!bFound && i>=0)
3093    {
3094      if( m_iIdx2DepthValue[layerIdInVps][i] < p )
3095      {
3096        iIdxUp  = i+1;
3097        bFound    = true;
3098      }
3099
3100      i--;
3101    }
3102
3103    // assert monotony
3104    assert(iIdxDown<=iIdxUp);
3105#endif
3106
3107    // assign closer depth value/idx
3108    if( abs(p-m_iIdx2DepthValue[layerIdInVps][iIdxDown]) < abs(p-m_iIdx2DepthValue[layerIdInVps][iIdxUp]) )
3109    {
3110      m_iDepthValue2Idx[layerIdInVps][p] = iIdxDown;
3111    }
3112    else
3113    {
3114      m_iDepthValue2Idx[layerIdInVps][p] = iIdxUp;
3115    }
3116
3117  }
3118
3119  // update DLT variables
3120  m_iNumDepthmapValues[layerIdInVps] = iNumDepthValues;
3121}
3122
3123Void TComDLT::getDeltaDLT( Int layerIdInVps, Int* piDLTInRef, UInt uiDLTInRefNum, Int* piDeltaDLTOut, UInt *puiDeltaDLTOutNum )
3124{
3125  Bool abBM0[ 256 ];
3126  Bool abBM1[ 256 ];
3127 
3128  memset( abBM0, 0, sizeof( abBM0 ));
3129  memset( abBM1, 0, sizeof( abBM1 ));
3130 
3131  // convert reference DLT to bit string
3132  for( Int i = 0; i < uiDLTInRefNum; i++ )
3133  {
3134    abBM0[ piDLTInRef[ i ] ] = true;
3135  }
3136  // convert internal DLT to bit string
3137  for( Int i = 0; i < m_iNumDepthmapValues[ layerIdInVps ]; i++ )
3138  {
3139    abBM1[ m_iIdx2DepthValue[ layerIdInVps ][ i ] ] = true;
3140  }
3141 
3142  *puiDeltaDLTOutNum = 0;
3143  for( Int i = 0; i < 256; i++ )
3144  {
3145    if( abBM0[ i ] ^ abBM1[ i ] )
3146    {
3147      piDeltaDLTOut[ *puiDeltaDLTOutNum ] = i;
3148      *puiDeltaDLTOutNum = *puiDeltaDLTOutNum + 1;
3149    }
3150  }
3151}
3152
3153Void TComDLT::setDeltaDLT( Int layerIdInVps, Int* piDLTInRef, UInt uiDLTInRefNum, Int* piDeltaDLTIn, UInt uiDeltaDLTInNum )
3154{
3155  Bool abBM0[ 256 ];
3156  Bool abBM1[ 256 ];
3157 
3158  memset( abBM0, 0, sizeof( abBM0 ));
3159  memset( abBM1, 0, sizeof( abBM1 ));
3160 
3161  // convert reference DLT to bit string
3162  for( Int i = 0; i < uiDLTInRefNum; i++ )
3163  {
3164    abBM0[ piDLTInRef[ i ] ] = true;
3165  }
3166  // convert delta DLT to bit string
3167  for( Int i = 0; i < uiDeltaDLTInNum; i++ )
3168  {
3169    abBM1[ piDeltaDLTIn[ i ] ] = true;
3170  }
3171 
3172  Int aiIdx2DepthValue[256];
3173  UInt uiNumDepthValues = 0;
3174  memset( aiIdx2DepthValue, 0, sizeof( aiIdx2DepthValue ));
3175 
3176  for( Int i = 0; i < 256; i++ )
3177  {
3178    if( abBM0[ i ] ^ abBM1[ i ] )
3179    {
3180      aiIdx2DepthValue[ uiNumDepthValues++ ] = i;
3181    }
3182  }
3183 
3184  // update internal tables
3185  setDepthLUTs(layerIdInVps, aiIdx2DepthValue, uiNumDepthValues);
3186}
3187
3188#endif
3189
3190#if H_MV
3191Void TComSPS::inferRepFormat( TComVPS* vps, Int layerIdCurr )
3192{
3193  if ( getMultiLayerExtSpsFlag() )
3194  { 
3195    Int            repFormatIdx = getUpdateRepFormatFlag() ?  getSpsRepFormatIdx() : vps->getVpsRepFormatIdx( vps->getLayerIdInVps( layerIdCurr ) ) ;
3196    TComRepFormat* repFormat    = vps->getRepFormat( repFormatIdx ); 
3197    setChromaFormatIdc( repFormat->getChromaFormatVpsIdc() );         
3198    //// ToDo: add when supported:
3199    // setSeperateColourPlaneFlag( repFormat->getSeparateColourPlaneVpsFlag() ) ;
3200
3201    setPicWidthInLumaSamples ( repFormat->getPicWidthVpsInLumaSamples()  ); 
3202    setPicHeightInLumaSamples( repFormat->getPicHeightVpsInLumaSamples() ); 
3203
3204    setBitDepthY             ( repFormat->getBitDepthVpsLumaMinus8()   + 8 ); 
3205    setQpBDOffsetY           ( (Int) (6*( getBitDepthY() - 8 )) );
3206
3207    setBitDepthC             ( repFormat->getBitDepthVpsChromaMinus8() + 8 ); 
3208    setQpBDOffsetC           ( (Int) (6* ( getBitDepthC() -8 ) ) );
3209    Window &spsConf    = getConformanceWindow();   
3210
3211    // Scaled later
3212    spsConf.setScaledFlag( false ); 
3213    spsConf.setWindowLeftOffset  ( repFormat->getConfWinVpsLeftOffset()    );
3214    spsConf.setWindowRightOffset ( repFormat->getConfWinVpsRightOffset()   );
3215    spsConf.setWindowTopOffset   ( repFormat->getConfWinVpsTopOffset()     );
3216    spsConf.setWindowBottomOffset( repFormat->getConfWinVpsBottomOffset()  );   
3217
3218   if ( getMultiLayerExtSpsFlag() && getUpdateRepFormatFlag() )
3219    {
3220      assert( getChromaFormatIdc()      <=  repFormat->getChromaFormatVpsIdc()         ); 
3221      //// ToDo: add when supported:
3222      // assert( getSeperateColourPlaneFlag() <=  repFormat->getSeparateColourPlaneVpsFlag() ) ;
3223
3224      assert( getPicWidthInLumaSamples()  <= repFormat->getPicWidthVpsInLumaSamples()    ); 
3225      assert( getPicHeightInLumaSamples() <= repFormat->getPicHeightVpsInLumaSamples()   ); 
3226
3227      assert( getBitDepthY()              <= repFormat->getBitDepthVpsLumaMinus8()   + 8 );         
3228      assert( getBitDepthC()              <= repFormat->getBitDepthVpsChromaMinus8() + 8 ); 
3229    }
3230  }
3231
3232  // Set conformance window
3233  Int scal = TComSPS::getWinUnitX( getChromaFormatIdc() ) ;
3234  getConformanceWindow().scaleOffsets( scal );
3235  getVuiParameters()->getDefaultDisplayWindow().scaleOffsets( scal );
3236}
3237
3238Void TComSPS::inferScalingList( TComSPS* spsSrc )
3239{
3240  if ( getSpsInferScalingListFlag() ) 
3241  {
3242    assert( spsSrc != NULL ); 
3243    assert( !spsSrc->getSpsInferScalingListFlag() );             
3244    getScalingList()->inferFrom( spsSrc->getScalingList() ); 
3245  }
3246}
3247
3248Void TComSPS::inferSpsMaxDecPicBufferingMinus1( TComVPS* vps, Int targetOptLayerSetIdx, Int currLayerId, Bool encoder )
3249{
3250  const std::vector<Int>& targetDecLayerIdList = vps->getTargetDecLayerIdList( vps->olsIdxToLsIdx( targetOptLayerSetIdx )); 
3251
3252  if ( getMultiLayerExtSpsFlag() )
3253  {
3254    Int layerIdx = 0;         
3255    while (layerIdx < (Int) targetDecLayerIdList.size() )
3256    {
3257      if ( targetDecLayerIdList[layerIdx] == currLayerId )
3258      { 
3259        break; 
3260      }
3261      layerIdx++; 
3262    }
3263
3264    assert( layerIdx < (Int) targetDecLayerIdList.size() ); 
3265
3266    for (Int i = 0; i <= getSpsMaxSubLayersMinus1(); i++ ) 
3267    {
3268      Int maxDecPicBufferingMinus1 = vps->getDpbSize()->getMaxVpsDecPicBufferingMinus1( targetOptLayerSetIdx, layerIdx, i ) ; 
3269
3270      // This preliminary fix needs to be checked.
3271      Int maxNumReorderPics       = vps->getDpbSize()->getMaxVpsNumReorderPics( targetOptLayerSetIdx, i ); 
3272      Int maxLatencyIncreasePlus1 = vps->getDpbSize()->getMaxVpsLatencyIncreasePlus1( targetOptLayerSetIdx, i ); 
3273      if ( encoder )     
3274      {
3275        assert( getMaxDecPicBuffering( i ) - 1 == maxDecPicBufferingMinus1 ); 
3276        // This preliminary fix needs to be checked.
3277        assert( getNumReorderPics( i )     == maxNumReorderPics       ); 
3278        assert( getMaxLatencyIncrease( i ) == maxLatencyIncreasePlus1 ); 
3279
3280      }
3281      else
3282      {
3283        // This preliminary fix needs to be checked.
3284        setMaxDecPicBuffering( maxDecPicBufferingMinus1 + 1 , i); 
3285        setNumReorderPics    ( maxNumReorderPics, i );
3286        setMaxLatencyIncrease( maxLatencyIncreasePlus1 - 1 , i); 
3287      }
3288    }   
3289  }
3290}
3291
3292Void TComSPS::checkRpsMaxNumPics( TComVPS* vps, Int currLayerId )
3293{
3294  for (Int i = 0; i < getRPSList()->getNumberOfReferencePictureSets(); i++ )
3295  {
3296    TComReferencePictureSet* rps = getRPSList()->getReferencePictureSet( i ); 
3297    if ( !rps->getInterRPSPrediction() )
3298    {
3299      rps->checkMaxNumPics( vps->getVpsExtensionFlag(), MAX_INT, getLayerId(), getMaxDecPicBuffering( getSpsMaxSubLayersMinus1() ) - 1 );   // INT_MAX to be replaced by DpbSize
3300    }
3301  }
3302}
3303
3304Void TComSPS::inferSpsMaxSubLayersMinus1(Bool atPsActivation, TComVPS* vps)
3305{
3306  assert( getLayerId() != 0 ); 
3307  if ( !atPsActivation   )
3308  {
3309    assert( vps == NULL );
3310    if (getSpsExtOrMaxSubLayersMinus1() != 7)
3311    {
3312      setSpsMaxSubLayersMinus1( getSpsExtOrMaxSubLayersMinus1() );
3313    }
3314  }
3315  else
3316  {
3317    assert( vps != NULL );
3318    if (getSpsExtOrMaxSubLayersMinus1() == 7)
3319    {
3320      setSpsMaxSubLayersMinus1( vps->getMaxSubLayersMinus1() );
3321    }
3322  }
3323}
3324#endif
3325
3326TComReferencePictureSet::TComReferencePictureSet()
3327: m_numberOfPictures (0)
3328, m_numberOfNegativePictures (0)
3329, m_numberOfPositivePictures (0)
3330, m_numberOfLongtermPictures (0)
3331, m_interRPSPrediction (0) 
3332, m_deltaRIdxMinus1 (0)   
3333, m_deltaRPS (0) 
3334, m_numRefIdc (0) 
3335{
3336  ::memset( m_deltaPOC, 0, sizeof(m_deltaPOC) );
3337  ::memset( m_POC, 0, sizeof(m_POC) );
3338  ::memset( m_used, 0, sizeof(m_used) );
3339  ::memset( m_refIdc, 0, sizeof(m_refIdc) );
3340}
3341
3342TComReferencePictureSet::~TComReferencePictureSet()
3343{
3344}
3345
3346Void TComReferencePictureSet::setUsed(Int bufferNum, Bool used)
3347{
3348  m_used[bufferNum] = used;
3349}
3350
3351Void TComReferencePictureSet::setDeltaPOC(Int bufferNum, Int deltaPOC)
3352{
3353  m_deltaPOC[bufferNum] = deltaPOC;
3354}
3355
3356Void TComReferencePictureSet::setNumberOfPictures(Int numberOfPictures)
3357{
3358  m_numberOfPictures = numberOfPictures;
3359}
3360
3361Int TComReferencePictureSet::getUsed(Int bufferNum)
3362{
3363  return m_used[bufferNum];
3364}
3365
3366Int TComReferencePictureSet::getDeltaPOC(Int bufferNum)
3367{
3368  return m_deltaPOC[bufferNum];
3369}
3370
3371Int TComReferencePictureSet::getNumberOfPictures()
3372{
3373  return m_numberOfPictures;
3374}
3375
3376Int TComReferencePictureSet::getPOC(Int bufferNum)
3377{
3378  return m_POC[bufferNum];
3379}
3380
3381Void TComReferencePictureSet::setPOC(Int bufferNum, Int POC)
3382{
3383  m_POC[bufferNum] = POC;
3384}
3385
3386Bool TComReferencePictureSet::getCheckLTMSBPresent(Int bufferNum)
3387{
3388  return m_bCheckLTMSB[bufferNum];
3389}
3390
3391Void TComReferencePictureSet::setCheckLTMSBPresent(Int bufferNum, Bool b)
3392{
3393  m_bCheckLTMSB[bufferNum] = b;
3394}
3395
3396/** set the reference idc value at uiBufferNum entry to the value of iRefIdc
3397 * \param uiBufferNum
3398 * \param iRefIdc
3399 * \returns Void
3400 */
3401Void TComReferencePictureSet::setRefIdc(Int bufferNum, Int refIdc)
3402{
3403  m_refIdc[bufferNum] = refIdc;
3404}
3405
3406/** get the reference idc value at uiBufferNum
3407 * \param uiBufferNum
3408 * \returns Int
3409 */
3410Int  TComReferencePictureSet::getRefIdc(Int bufferNum)
3411{
3412  return m_refIdc[bufferNum];
3413}
3414
3415/** Sorts the deltaPOC and Used by current values in the RPS based on the deltaPOC values.
3416 *  deltaPOC values are sorted with -ve values before the +ve values.  -ve values are in decreasing order.
3417 *  +ve values are in increasing order.
3418 * \returns Void
3419 */
3420Void TComReferencePictureSet::sortDeltaPOC()
3421{
3422  // sort in increasing order (smallest first)
3423  for(Int j=1; j < getNumberOfPictures(); j++)
3424  { 
3425    Int deltaPOC = getDeltaPOC(j);
3426    Bool used = getUsed(j);
3427    for (Int k=j-1; k >= 0; k--)
3428    {
3429      Int temp = getDeltaPOC(k);
3430      if (deltaPOC < temp)
3431      {
3432        setDeltaPOC(k+1, temp);
3433        setUsed(k+1, getUsed(k));
3434        setDeltaPOC(k, deltaPOC);
3435        setUsed(k, used);
3436      }
3437    }
3438  }
3439  // flip the negative values to largest first
3440  Int numNegPics = getNumberOfNegativePictures();
3441  for(Int j=0, k=numNegPics-1; j < numNegPics>>1; j++, k--)
3442  { 
3443    Int deltaPOC = getDeltaPOC(j);
3444    Bool used = getUsed(j);
3445    setDeltaPOC(j, getDeltaPOC(k));
3446    setUsed(j, getUsed(k));
3447    setDeltaPOC(k, deltaPOC);
3448    setUsed(k, used);
3449  }
3450}
3451
3452/** Prints the deltaPOC and RefIdc (if available) values in the RPS.
3453 *  A "*" is added to the deltaPOC value if it is Used bu current.
3454 * \returns Void
3455 */
3456Void TComReferencePictureSet::printDeltaPOC()
3457{
3458  printf("DeltaPOC = { ");
3459  for(Int j=0; j < getNumberOfPictures(); j++)
3460  {
3461    printf("%d%s ", getDeltaPOC(j), (getUsed(j)==1)?"*":"");
3462  } 
3463  if (getInterRPSPrediction()) 
3464  {
3465    printf("}, RefIdc = { ");
3466    for(Int j=0; j < getNumRefIdc(); j++)
3467    {
3468      printf("%d ", getRefIdc(j));
3469    } 
3470  }
3471  printf("}\n");
3472}
3473#if H_MV
3474Void TComReferencePictureSet::checkMaxNumPics( Bool vpsExtensionFlag, Int maxNumPics, Int nuhLayerId, Int spsMaxDecPicBufferingMinus1 )
3475{
3476  assert( getNumberOfPictures() >= 0 ); 
3477  if ( nuhLayerId == 0 )
3478  {
3479    assert( getNumberOfPictures() <= spsMaxDecPicBufferingMinus1 ); 
3480  }
3481
3482  if ( vpsExtensionFlag )
3483  {
3484    assert( getNumberOfPictures() <= maxNumPics );
3485  }
3486}
3487#endif
3488
3489TComRPSList::TComRPSList()
3490:m_referencePictureSets (NULL)
3491{
3492}
3493
3494TComRPSList::~TComRPSList()
3495{
3496}
3497
3498Void TComRPSList::create( Int numberOfReferencePictureSets)
3499{
3500  m_numberOfReferencePictureSets = numberOfReferencePictureSets;
3501  m_referencePictureSets = new TComReferencePictureSet[numberOfReferencePictureSets];
3502}
3503
3504Void TComRPSList::destroy()
3505{
3506  if (m_referencePictureSets)
3507  {
3508    delete [] m_referencePictureSets;
3509  }
3510  m_numberOfReferencePictureSets = 0;
3511  m_referencePictureSets = NULL;
3512}
3513
3514
3515
3516TComReferencePictureSet* TComRPSList::getReferencePictureSet(Int referencePictureSetNum)
3517{
3518  return &m_referencePictureSets[referencePictureSetNum];
3519}
3520
3521Int TComRPSList::getNumberOfReferencePictureSets()
3522{
3523  return m_numberOfReferencePictureSets;
3524}
3525
3526Void TComRPSList::setNumberOfReferencePictureSets(Int numberOfReferencePictureSets)
3527{
3528  m_numberOfReferencePictureSets = numberOfReferencePictureSets;
3529}
3530
3531TComRefPicListModification::TComRefPicListModification()
3532: m_bRefPicListModificationFlagL0 (false)
3533, m_bRefPicListModificationFlagL1 (false)
3534{
3535  ::memset( m_RefPicSetIdxL0, 0, sizeof(m_RefPicSetIdxL0) );
3536  ::memset( m_RefPicSetIdxL1, 0, sizeof(m_RefPicSetIdxL1) );
3537}
3538
3539TComRefPicListModification::~TComRefPicListModification()
3540{
3541}
3542
3543TComScalingList::TComScalingList()
3544{
3545  init();
3546}
3547TComScalingList::~TComScalingList()
3548{
3549  destroy();
3550}
3551
3552/** set default quantization matrix to array
3553*/
3554Void TComSlice::setDefaultScalingList()
3555{
3556  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
3557  {
3558    for(UInt listId=0;listId<g_scalingListNum[sizeId];listId++)
3559    {
3560      getScalingList()->processDefaultMatrix(sizeId, listId);
3561    }
3562  }
3563}
3564/** check if use default quantization matrix
3565 * \returns true if use default quantization matrix in all size
3566*/
3567Bool TComSlice::checkDefaultScalingList()
3568{
3569  UInt defaultCounter=0;
3570
3571  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
3572  {
3573    for(UInt listId=0;listId<g_scalingListNum[sizeId];listId++)
3574    {
3575      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
3576     && ((sizeId < SCALING_LIST_16x16) || (getScalingList()->getScalingListDC(sizeId,listId) == 16))) // check DC value
3577      {
3578        defaultCounter++;
3579      }
3580    }
3581  }
3582  return (defaultCounter == (SCALING_LIST_NUM * SCALING_LIST_SIZE_NUM - 4)) ? false : true; // -4 for 32x32
3583}
3584
3585#if H_MV
3586Void TComSlice::createInterLayerReferencePictureSet( TComPicLists* ivPicLists, std::vector<TComPic*>& refPicSetInterLayer0, std::vector<TComPic*>& refPicSetInterLayer1 )
3587{
3588  refPicSetInterLayer0.clear(); 
3589  refPicSetInterLayer1.clear(); 
3590
3591  for( Int i = 0; i < getNumActiveRefLayerPics(); i++ ) 
3592  {
3593    Int layerIdRef = getRefPicLayerId( i ); 
3594    TComPic* picRef = ivPicLists->getPic( layerIdRef, getPOC() ) ; 
3595    assert ( picRef != 0 ); // There shall be no entry equal to "no reference picture" in RefPicSetInterLayer0 or RefPicSetInterLayer1.
3596
3597    picRef->getPicYuvRec()->extendPicBorder(); 
3598    picRef->setIsLongTerm( true );       
3599    picRef->getSlice(0)->setReferenced( true );       
3600
3601    Int viewIdCur  = getVPS()->getViewId( getLayerId() ); 
3602    Int viewIdZero = getVPS()->getViewId( 0 );
3603    Int viewIdRef  = getVPS()->getViewId( layerIdRef ); 
3604
3605    if (  ( viewIdCur <= viewIdZero && viewIdCur <= viewIdRef ) || ( viewIdCur >= viewIdZero && viewIdCur >= viewIdRef ) )
3606    {
3607      refPicSetInterLayer0.push_back( picRef ); 
3608    }
3609    else
3610    {
3611      refPicSetInterLayer1.push_back( picRef ); 
3612    }
3613    // Consider to check here:
3614    // "If the current picture is a RADL picture, there shall be no entry in the RefPicSetInterLayer0 and RefPicSetInterLayer1 that is a RASL picture. "   
3615    assert( picRef->getSlice(0)->getDiscardableFlag() == false ); // "There shall be no picture that has discardable_flag equal to 1 in RefPicSetInterLayer0 or RefPicSetInterLayer1".       
3616  }
3617}
3618
3619Void TComSlice::markIvRefPicsAsShortTerm( std::vector<TComPic*> refPicSetInterLayer0, std::vector<TComPic*> refPicSetInterLayer1 )
3620{
3621  // Mark as short-term
3622  for ( Int i = 0; i < refPicSetInterLayer0.size(); i++ ) 
3623  {
3624    refPicSetInterLayer0[i]->setIsLongTerm( false ); 
3625  }
3626
3627  for ( Int i = 0; i < refPicSetInterLayer1.size(); i++ ) 
3628  {
3629    refPicSetInterLayer1[i]->setIsLongTerm( false ); 
3630  }
3631
3632}
3633
3634Void TComSlice::printRefPicList()
3635{ 
3636  for ( Int li = 0; li < 2; li++)
3637  {   
3638    std::cout << std::endl << "RefPicListL" <<  li << ":" << std::endl; 
3639    for (Int rIdx = 0; rIdx <= (m_aiNumRefIdx[li]-1); rIdx ++)
3640    {     
3641      if (rIdx == 0 && li == 0) m_apcRefPicList[li][rIdx]->print( true );
3642       
3643      m_apcRefPicList[li][rIdx]->print( false );
3644    }
3645  }
3646}
3647
3648Void TComSlice::markCurrPic( TComPic* currPic )
3649{
3650  currPic->getSlice(0)->setReferenced( true ) ; 
3651  currPic->setIsLongTerm( false ); 
3652
3653  currPic->setReconMark( true );
3654  currPic->setPicOutputFlag( currPic->getSlice(0)->getPicOutputFlag() );
3655}
3656
3657Void TComSlice::setRefPicSetInterLayer( std::vector<TComPic*>* refPicSetInterLayer0, std::vector<TComPic*>* refPicSetInterLayer1 )
3658{
3659  m_refPicSetInterLayer0 = refPicSetInterLayer0; 
3660  m_refPicSetInterLayer1 = refPicSetInterLayer1; 
3661}
3662
3663TComPic* TComSlice::getPicFromRefPicSetInterLayer(Int setIdc, Int layerId )
3664{
3665  assert ( setIdc == 0 || setIdc == 1);   
3666  std::vector<TComPic*>* refPicSetInterLayer = ( setIdc == 0 ? m_refPicSetInterLayer0 : m_refPicSetInterLayer1);   
3667  assert( refPicSetInterLayer != 0 ); 
3668 
3669  TComPic* pcPic = NULL; 
3670  for ( Int i = 0; i < (*refPicSetInterLayer).size(); i++ )
3671  {
3672    if ((*refPicSetInterLayer)[ i ]->getLayerId() == layerId )
3673    {
3674      pcPic = (*refPicSetInterLayer)[ i ]; 
3675    }
3676  }
3677
3678  assert(pcPic != NULL); 
3679  return pcPic;
3680}
3681
3682
3683Int  TComSlice::getRefLayerPicFlag( Int i ) 
3684{
3685  TComVPS* vps = getVPS(); 
3686#if HHI_DEPENDENCY_SIGNALLING_I1_J0107
3687#if H_3D
3688  Int refLayerIdx = vps->getLayerIdInVps( vps->getIdRefListLayer( getLayerId(), i ) ); 
3689#else
3690  Int refLayerIdx = vps->getLayerIdInVps( vps->getIdDirectRefLayer( getLayerId(), i ) ); 
3691#endif
3692#else
3693  Int refLayerIdx = vps->getLayerIdInVps( vps->getIdDirectRefLayer( getLayerId(), i ) ); 
3694#endif
3695
3696#if H_MV_FIX_REF_LAYER_PIC_FLAG
3697  Bool refLayerPicFlag = ( vps->getSubLayersVpsMaxMinus1( refLayerIdx ) >=  getTLayer()  && ( getTLayer() == 0   ||
3698    vps->getMaxTidIlRefPicsPlus1( refLayerIdx, vps->getLayerIdInVps( getLayerId() )) > getTLayer() )); 
3699#else
3700  Bool refLayerPicFlag = ( vps->getSubLayersVpsMaxMinus1( refLayerIdx ) >=  getTLayer() )  && ( getTLayer() == 0  ) &&
3701    ( vps->getMaxTidIlRefPicsPlus1( refLayerIdx, vps->getLayerIdInVps( getLayerId() )) > getTLayer() ); 
3702#endif
3703  return refLayerPicFlag;       
3704}   
3705
3706Int TComSlice::getRefLayerPicIdc( Int j ) 
3707{ 
3708  Int refLayerPicIdc = -1; 
3709  Int curj = 0; 
3710#if HHI_DEPENDENCY_SIGNALLING_I1_J0107
3711#if H_3D
3712  for( Int i = 0;  i < getVPS()->getNumRefListLayers( getLayerId()) ; i++ )
3713#else
3714  for( Int i = 0;  i < getVPS()->getNumDirectRefLayers( getLayerId()) ; i++ )
3715#endif
3716#else
3717  for( Int i = 0;  i < getVPS()->getNumDirectRefLayers( getLayerId()) ; i++ )
3718#endif
3719  {
3720    if( getRefLayerPicFlag( i ) )
3721    {
3722      if ( curj == j ) 
3723      {
3724        refLayerPicIdc = i;         
3725        break;
3726      }
3727      curj++; 
3728    }
3729  }
3730
3731  assert( curj == j ); 
3732  assert( refLayerPicIdc != -1 ); 
3733  return refLayerPicIdc; 
3734}
3735
3736Int  TComSlice::getNumRefLayerPics( )
3737{ 
3738  Int numRefLayerPics = 0; 
3739#if HHI_DEPENDENCY_SIGNALLING_I1_J0107
3740#if H_3D
3741  for( Int i = 0;  i < getVPS()->getNumRefListLayers( getLayerId()) ; i++ )
3742#else
3743  for( Int i = 0;  i < getVPS()->getNumDirectRefLayers( getLayerId()) ; i++ )
3744#endif
3745#else
3746  for( Int i = 0;  i < getVPS()->getNumDirectRefLayers( getLayerId()) ; i++ )
3747#endif
3748  {
3749    numRefLayerPics += getRefLayerPicFlag( i ); 
3750  }
3751  return numRefLayerPics; 
3752}
3753
3754
3755
3756Int TComSlice::getNumActiveRefLayerPics()
3757{
3758  Int numActiveRefLayerPics; 
3759
3760  if( getLayerId() == 0 || getNumRefLayerPics() ==  0 )
3761  {
3762    numActiveRefLayerPics = 0; 
3763  }
3764  else if (getVPS()->getAllRefLayersActiveFlag() )
3765  {
3766    numActiveRefLayerPics = getNumRefLayerPics(); 
3767  }
3768  else if ( !getInterLayerPredEnabledFlag() )
3769  {
3770    numActiveRefLayerPics = 0; 
3771  }
3772#if HHI_DEPENDENCY_SIGNALLING_I1_J0107
3773#if H_3D
3774  else if( getVPS()->getMaxOneActiveRefLayerFlag() || getVPS()->getNumRefListLayers( getLayerId() ) == 1 )
3775#else
3776  else if( getVPS()->getMaxOneActiveRefLayerFlag() || getVPS()->getNumDirectRefLayers( getLayerId() ) == 1 )
3777#endif
3778#else
3779  else if( getVPS()->getMaxOneActiveRefLayerFlag() || getVPS()->getNumDirectRefLayers( getLayerId() ) == 1 )
3780#endif 
3781  {
3782    numActiveRefLayerPics = 1; 
3783  }
3784  else
3785  {
3786    numActiveRefLayerPics = getNumInterLayerRefPicsMinus1() + 1; 
3787  }
3788  return numActiveRefLayerPics;
3789}
3790
3791Int TComSlice::getRefPicLayerId( Int i )
3792{
3793#if HHI_DEPENDENCY_SIGNALLING_I1_J0107
3794#if H_3D
3795  return getVPS()->getIdRefListLayer( getLayerId(), getInterLayerPredLayerIdc( i ) );
3796#else
3797  return getVPS()->getIdDirectRefLayer( getLayerId(), getInterLayerPredLayerIdc( i ) );
3798#endif
3799#else
3800  return getVPS()->getIdDirectRefLayer( getLayerId(), getInterLayerPredLayerIdc( i ) );
3801#endif
3802}
3803
3804#if SEC_ARP_VIEW_REF_CHECK_J0037 || SEC_DBBP_VIEW_REF_CHECK_J0037
3805Void TComSlice::setDefaultRefView( )
3806{
3807  setDefaultRefViewIdx( -1 );
3808  setDefaultRefViewIdxAvailableFlag( false ); 
3809
3810  Int valid = 0;
3811  Int DefaultRefViewIdx = -1;
3812  for( UInt curViewIdx = 0; curViewIdx < getViewIndex() && valid == 0; curViewIdx++ )
3813  {
3814    for( Int iRefListId = 0; ( iRefListId < (isInterB() ? 2 : 1) ) && !isIntra() && valid == 0; iRefListId++ )
3815    {
3816      RefPicList eRefPicList = RefPicList( iRefListId );
3817      Int        iNumRefPics = getNumRefIdx( eRefPicList );
3818      for( Int i = 0; i < iNumRefPics; i++ )
3819      { 
3820        if(getPOC() == getRefPic( eRefPicList, i )->getPOC() && curViewIdx == getRefPic( eRefPicList, i )->getViewIndex())
3821        {
3822          valid = 1;
3823          DefaultRefViewIdx = curViewIdx;
3824          break;
3825        }
3826      }
3827    }
3828  }
3829  if( valid )
3830  {
3831    setDefaultRefViewIdx( DefaultRefViewIdx );
3832    setDefaultRefViewIdxAvailableFlag( true );   
3833  }
3834}
3835#endif
3836
3837#if H_3D_ARP
3838Void TComSlice::setARPStepNum( TComPicLists*ivPicLists )
3839{
3840  Bool tempRefPicInListsFlag = false;
3841#if HHI_TOOL_PARAMETERS_I2_J0107
3842  if( !getIvResPredFlag() || this->isIRAP())
3843#else
3844  if(!getVPS()->getUseAdvRP(getLayerId()) || this->isIRAP())
3845#endif
3846  {
3847    m_nARPStepNum = 0;
3848  }
3849  else
3850  {
3851    setFirstTRefIdx (REF_PIC_LIST_0, -1);
3852    setFirstTRefIdx (REF_PIC_LIST_1, -1);
3853    for ( Int refListIdx = 0; refListIdx < ((m_eSliceType==B_SLICE) ? 2 : 1); refListIdx++ )
3854    {
3855      Int diffPOC=MAX_INT;
3856      Int idx=-1;
3857      for(Int i = 0; i < getNumRefIdx(RefPicList(refListIdx)); i++ )
3858      {
3859        if ( getRefPic(RefPicList(refListIdx), i)->getPOC() != getPOC() )
3860        {
3861          if( abs(getRefPic(RefPicList(refListIdx), i)->getPOC() - getPOC()) < diffPOC)
3862          {
3863            diffPOC=abs(getRefPic(RefPicList(refListIdx), i)->getPOC() - getPOC());
3864            idx=i;
3865          }
3866        }
3867        if(idx>=0)
3868        {
3869          setFirstTRefIdx (RefPicList(refListIdx), idx);
3870        }
3871      }
3872    }
3873#if SEC_ARP_VIEW_REF_CHECK_J0037
3874    tempRefPicInListsFlag = (getFirstTRefIdx(REF_PIC_LIST_0) >= 0 || getFirstTRefIdx(REF_PIC_LIST_1) >= 0) && getDefaultRefViewIdxAvailableFlag();
3875#else
3876    tempRefPicInListsFlag = getFirstTRefIdx(REF_PIC_LIST_0) >= 0 || getFirstTRefIdx(REF_PIC_LIST_1) >= 0;
3877#endif
3878#if HHI_TOOL_PARAMETERS_I2_J0107
3879    m_nARPStepNum = tempRefPicInListsFlag ?  H_3D_ARP_WFNR : 0;
3880#else
3881    m_nARPStepNum = tempRefPicInListsFlag ? getVPS()->getARPStepNum(getLayerId()) : 0;
3882#endif
3883  }
3884
3885  if (tempRefPicInListsFlag)
3886  {
3887    for ( Int refListIdx = 0; refListIdx < ((m_eSliceType==B_SLICE) ? 2 : 1); refListIdx++ )
3888    {
3889      RefPicList eRefPicList = RefPicList( refListIdx );
3890      Int prevPOC = getRefPic(eRefPicList, getFirstTRefIdx(eRefPicList) )->getPOC();
3891      for( Int i = 0; i < getNumActiveRefLayerPics(); i++ )
3892      {
3893        Int layerIdInNuh = getRefPicLayerId( i );
3894        Int viewIdx = getVPS()->getViewId( layerIdInNuh );
3895        TComPic*pcPicPrev = ivPicLists->getPic(viewIdx, 0, prevPOC);
3896        if (getFirstTRefIdx(eRefPicList) >= 0 && pcPicPrev && pcPicPrev->getSlice( 0 )->isReferenced())
3897        {
3898          m_arpRefPicAvailable[eRefPicList][layerIdInNuh] = true;
3899        }
3900        else
3901        {
3902          m_arpRefPicAvailable[eRefPicList][layerIdInNuh] = false;
3903        }
3904      }
3905    }
3906  }
3907}
3908#endif
3909#if H_3D_IC
3910Void TComSlice::xSetApplyIC(Bool bUseLowLatencyICEnc)
3911{
3912  if(bUseLowLatencyICEnc)
3913  {
3914    Bool existInterViewRef=false;
3915    TComPic* pcCurrPic = getPic();
3916    TComPic* pcRefPic = NULL;
3917    for ( Int i = 0; (i < getNumRefIdx( REF_PIC_LIST_0 )) && !existInterViewRef; i++ )
3918    {
3919      pcRefPic = getRefPic( REF_PIC_LIST_0, i );
3920      if ( pcRefPic != NULL )
3921      {
3922        if ( pcCurrPic->getViewIndex() != pcRefPic->getViewIndex() )
3923        {
3924          existInterViewRef = true;       
3925        }
3926      }
3927    }
3928
3929    for ( Int i = 0; (i < getNumRefIdx( REF_PIC_LIST_1 )) && !existInterViewRef; i++ )
3930    {
3931      pcRefPic = getRefPic( REF_PIC_LIST_1, i );
3932      if ( pcRefPic != NULL )
3933      {
3934        if ( pcCurrPic->getViewIndex() != pcRefPic->getViewIndex() )
3935        {
3936          existInterViewRef = true;       
3937        }
3938      }
3939    }
3940
3941    if(!existInterViewRef)
3942    {
3943      m_bApplyIC = false;
3944    }
3945    else
3946    {
3947      Int curLayer=getDepth();
3948      if( curLayer>9) curLayer=9; // Max layer is 10
3949
3950      m_bApplyIC = true;
3951      Int refLayer = curLayer-1;
3952
3953      Int ICEnableCandidate = getICEnableCandidate(refLayer);
3954      Int ICEnableNum = getICEnableNum(refLayer);
3955      if( (refLayer>=0) && (ICEnableCandidate>0) )
3956      {   
3957        Double ratio=Double(ICEnableNum/Double(ICEnableCandidate));
3958
3959        if( ratio > IC_LOW_LATENCY_ENCODING_THRESHOLD)
3960        {
3961          m_bApplyIC=true;
3962        }
3963        else
3964        {
3965          m_bApplyIC=false;
3966        }
3967      }
3968      setICEnableCandidate(curLayer, 0);
3969      setICEnableNum(curLayer, 0);
3970    }
3971  }
3972  else
3973  {
3974  Int iMaxPelValue = ( 1 << g_bitDepthY ); 
3975  Int *aiRefOrgHist;
3976  Int *aiCurrHist;
3977  aiRefOrgHist = (Int *) xMalloc( Int,iMaxPelValue );
3978  aiCurrHist   = (Int *) xMalloc( Int,iMaxPelValue );
3979  memset( aiRefOrgHist, 0, iMaxPelValue*sizeof(Int) );
3980  memset( aiCurrHist, 0, iMaxPelValue*sizeof(Int) );
3981  // Reference Idx Number
3982  Int iNumRefIdx = getNumRefIdx( REF_PIC_LIST_0 );
3983  TComPic* pcCurrPic = NULL;
3984  TComPic* pcRefPic = NULL;
3985  TComPicYuv* pcCurrPicYuv = NULL;
3986  TComPicYuv* pcRefPicYuvOrg = NULL;
3987  pcCurrPic = getPic();
3988  pcCurrPicYuv = pcCurrPic->getPicYuvOrg();
3989  Int iWidth = pcCurrPicYuv->getWidth();
3990  Int iHeight = pcCurrPicYuv->getHeight();
3991
3992
3993  // Get InterView Reference picture
3994  // !!!!! Assume only one Interview Reference Picture in L0
3995  for ( Int i = 0; i < iNumRefIdx; i++ )
3996  {
3997    pcRefPic = getRefPic( REF_PIC_LIST_0, i );
3998    if ( pcRefPic != NULL )
3999    {
4000      if ( pcCurrPic->getViewIndex() != pcRefPic->getViewIndex() )
4001      {
4002        pcRefPicYuvOrg = pcRefPic->getPicYuvOrg();
4003      }
4004    }
4005  }
4006
4007  if ( pcRefPicYuvOrg != NULL )
4008  {
4009    Pel* pCurrY = pcCurrPicYuv ->getLumaAddr();
4010    Pel* pRefOrgY = pcRefPicYuvOrg  ->getLumaAddr();
4011    Int iCurrStride = pcCurrPicYuv->getStride();
4012    Int iRefStride = pcRefPicYuvOrg->getStride();
4013    Int iSumOrgSAD = 0;
4014    Double dThresholdOrgSAD = getIsDepth() ? 0.1 : 0.05;
4015
4016    // Histogram building - luminance
4017    for ( Int y = 0; y < iHeight; y++ )
4018    {
4019      for ( Int x = 0; x < iWidth; x++ )
4020      {
4021        aiCurrHist[pCurrY[x]]++;
4022        aiRefOrgHist[pRefOrgY[x]]++;
4023      }
4024      pCurrY += iCurrStride;
4025      pRefOrgY += iRefStride;
4026    }
4027    // Histogram SAD
4028    for ( Int i = 0; i < iMaxPelValue; i++ )
4029    {
4030      iSumOrgSAD += abs( aiCurrHist[i] - aiRefOrgHist[i] );
4031    }
4032    // Setting
4033    if ( iSumOrgSAD > Int( dThresholdOrgSAD * iWidth * iHeight ) )
4034    {
4035      m_bApplyIC = true;
4036    }
4037    else
4038    {
4039      m_bApplyIC = false;
4040    }
4041  }
4042
4043  xFree( aiCurrHist   );
4044  xFree( aiRefOrgHist );
4045  aiCurrHist = NULL;
4046  aiRefOrgHist = NULL;
4047  }//if(bUseLowLatencyICEnc)
4048}
4049#endif
4050#if H_3D
4051Void TComSlice::setIvPicLists( TComPicLists* m_ivPicLists )
4052{
4053  for (Int i = 0; i < MAX_NUM_LAYERS; i++ )
4054  {     
4055    for ( Int depthId = 0; depthId < 2; depthId++ )
4056    {
4057      m_ivPicsCurrPoc[ depthId ][ i ] = ( i <= m_viewIndex ) ? m_ivPicLists->getPic( i, ( depthId == 1) , getPOC() ) : NULL;
4058    }       
4059  } 
4060}
4061Void TComSlice::setDepthToDisparityLUTs()
4062{ 
4063  Bool setupLUT = false; 
4064 
4065#if H_3D_VSP
4066#if HHI_TOOL_PARAMETERS_I2_J0107
4067  setupLUT = setupLUT || getViewSynthesisPredFlag( ); 
4068#else
4069  Int layerIdInVPS = getVPS()->getLayerIdInNuh( m_layerId ); 
4070  setupLUT = setupLUT || getVPS()->getViewSynthesisPredFlag( layerIdInVPS); 
4071#endif
4072#endif
4073
4074#if H_3D_NBDV_REF
4075#if HHI_TOOL_PARAMETERS_I2_J0107
4076  setupLUT = setupLUT || getDepthRefinementFlag( );
4077#else
4078  setupLUT = setupLUT || getVPS()->getDepthRefinementFlag( layerIdInVPS );
4079#endif
4080#endif
4081
4082#if H_3D_IV_MERGE
4083#if HHI_TOOL_PARAMETERS_I2_J0107
4084  setupLUT = setupLUT || ( getIvMvPredFlag() && getIsDepth() );
4085#else
4086  setupLUT = setupLUT || ( getVPS()->getIvMvPredFlag(layerIdInVPS ) && getIsDepth() );
4087#endif
4088#endif
4089
4090#if !LGE_DDD_REMOVAL_J0042_J0030
4091#if H_3D_DDD
4092#if H_3D_FCO
4093  if( getIsDepth() && getViewIndex() > 0 && getVPS()->getMPIFlag(layerIdInVPS))
4094#else
4095  if( getIsDepth() && getViewIndex() > 0 )
4096#endif
4097  {
4098      TComSlice *pcTextSlice = getTexturePic()->getSlice( 0 );
4099      memcpy( m_aiDDDInvScale, pcTextSlice->m_aiDDDInvScale, sizeof( Int ) * getViewIndex() );
4100      memcpy( m_aiDDDInvOffset, pcTextSlice->m_aiDDDInvOffset, sizeof( Int ) * getViewIndex() );
4101      memcpy( m_aiDDDShift, pcTextSlice->m_aiDDDShift, sizeof( Int ) * getViewIndex() );             
4102  } 
4103#endif
4104#endif
4105
4106  if( !setupLUT )
4107    return; 
4108
4109  /// GT: Allocation should be moved to a better place later;
4110  if ( m_depthToDisparityB == NULL )
4111  {
4112    m_depthToDisparityB = new Int*[ getViewIndex() ];
4113    for ( Int i = 0; i < getViewIndex(); i++ )
4114    {
4115      m_depthToDisparityB[ i ] = new Int[ Int(1 << g_bitDepthY) ]; 
4116    }
4117  }
4118
4119  if ( m_depthToDisparityF == NULL )
4120  {
4121    m_depthToDisparityF= new Int*[ getViewIndex() ];
4122    for ( Int i = 0; i < getViewIndex(); i++ )
4123    {
4124      m_depthToDisparityF[ i ] = new Int[ Int(1 << g_bitDepthY) ]; 
4125    }
4126  }
4127
4128  assert( m_depthToDisparityB != NULL ); 
4129  assert( m_depthToDisparityF != NULL ); 
4130
4131  TComVPS* vps = getVPS(); 
4132
4133  Int log2Div = g_bitDepthY - 1 + vps->getCamParPrecision();
4134  Int viewIndex = getViewIndex();
4135
4136  Bool camParaSH = vps->hasCamParInSliceHeader( viewIndex );
4137
4138  Int* codScale     = camParaSH ? m_aaiCodedScale [ 0 ] : vps->getCodedScale    ( viewIndex ); 
4139  Int* codOffset    = camParaSH ? m_aaiCodedOffset[ 0 ] : vps->getCodedOffset   ( viewIndex ); 
4140  Int* invCodScale  = camParaSH ? m_aaiCodedScale [ 1 ] : vps->getInvCodedScale ( viewIndex ); 
4141  Int* invCodOffset = camParaSH ? m_aaiCodedOffset[ 1 ] : vps->getInvCodedOffset( viewIndex ); 
4142
4143  for (Int i = 0; i <= ( getViewIndex() - 1); i++)
4144  {
4145    for ( Int d = 0; d <= ( ( 1 << g_bitDepthY ) - 1 ); d++ )
4146    {
4147      Int offset =    ( codOffset  [ i ] << g_bitDepthY ) + ( ( 1 << log2Div ) >> 1 );         
4148      m_depthToDisparityB[ i ][ d ] = ( codScale [ i ] * d + offset ) >> log2Div; 
4149
4150      Int invOffset = ( invCodOffset[ i ] << g_bitDepthY ) + ( ( 1 << log2Div ) >> 1 );         
4151      m_depthToDisparityF[ i ][ d ] = ( invCodScale[ i ] * d + invOffset ) >> log2Div; 
4152    }
4153#if !LGE_DDD_REMOVAL_J0042_J0030
4154#if H_3D_DDD
4155    initializeDDDPara( vps->getCamParPrecision(), codScale[ i ], codOffset[ i ], i );
4156#endif
4157#endif
4158  }
4159}
4160#endif
4161#endif
4162
4163#if !LGE_DDD_REMOVAL_J0042_J0030
4164#if H_3D_DDD
4165Void TComSlice::initializeDDDPara( UInt uiCamParsCodedPrecision, Int  iCodedScale,Int  iCodedOffset, Int iBaseViewIdx )
4166{
4167    UInt uiViewId     = getViewIndex();
4168
4169    if( uiViewId == 0 )
4170    {
4171        m_aiDDDInvScale[ iBaseViewIdx ] = m_aiDDDInvOffset[ iBaseViewIdx ] = m_aiDDDShift[ iBaseViewIdx ] = 0;
4172        return;
4173    }
4174
4175
4176    Int iSign = iCodedScale >= 0 ? 1 : -1;
4177    iCodedScale = abs( iCodedScale );
4178
4179    Int iBitWidth = 0;
4180
4181    const Int iInvPres = 9;
4182
4183    while( ((( 1 << iBitWidth ) << 1 ) <= iCodedScale ) )
4184    {
4185        iBitWidth ++;
4186    }
4187    iBitWidth += iInvPres;
4188    Int iTargetValue =  1 << iBitWidth;
4189
4190    Int iMinError = MAX_INT;
4191    Int iBestD = 1 << ( iInvPres - 1 );
4192    for( Int d = 1 << ( iInvPres - 1 ); d < ( 1 << iInvPres ); d++ )
4193    {
4194        Int iError = abs( iCodedScale * d - iTargetValue );
4195        if( iError < iMinError )
4196        {
4197            iMinError = iError;
4198            iBestD = d;
4199        }
4200        if( iMinError == 0 )
4201        {
4202            break;
4203        }
4204    }
4205    Int iRoundingDir = 0;
4206    if( iCodedScale * iBestD > iTargetValue )
4207    {
4208        iRoundingDir = -1;
4209    }
4210    else if( iCodedScale * iBestD < iTargetValue )
4211    {
4212        iRoundingDir = 1;
4213    }
4214    Int iCamPres = uiCamParsCodedPrecision - 1;
4215    m_aiDDDInvScale [ iBaseViewIdx ] = ( iBestD << ( iCamPres + g_bitDepthY )) * iSign;
4216    m_aiDDDInvOffset[ iBaseViewIdx ] = -iSign * iBestD * ( iCodedOffset << g_bitDepthY );
4217    m_aiDDDShift    [ iBaseViewIdx ] = iBitWidth;
4218    m_aiDDDInvOffset[ iBaseViewIdx ] += 1 << ( m_aiDDDShift[ iBaseViewIdx ] - 1 );
4219    m_aiDDDInvOffset[ iBaseViewIdx ] += ( 1 << ( m_aiDDDShift[ iBaseViewIdx ] - 4 ) ) * iRoundingDir;
4220
4221    return;
4222}
4223
4224
4225#endif
4226#endif
4227
4228#if H_MV
4229Void TComSlice::checkCrossLayerBlaFlag()
4230{
4231  // 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.
4232  if ( getLayerId() != 0 || getNalUnitType() != NAL_UNIT_CODED_SLICE_IDR_W_RADL || getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_N_LP )
4233  {
4234    assert( m_crossLayerBlaFlag == 0 ); 
4235  }
4236}
4237
4238Bool TComSlice::inferPocMsbValPresentFlag()
4239{
4240  Bool pocMsbValPresentFlag; 
4241  if( getSliceSegmentHeaderExtensionLength() == 0 ) 
4242  {
4243    pocMsbValPresentFlag = false; 
4244  }
4245  else if ( getPocMsbValRequiredFlag() )
4246  {
4247    pocMsbValPresentFlag = true; 
4248  }
4249  else
4250  {
4251    pocMsbValPresentFlag = false; 
4252  }
4253
4254  return pocMsbValPresentFlag;
4255}
4256
4257
4258#endif
4259
4260#if !LGE_DDD_REMOVAL_J0042_J0030
4261#if H_3D_DBBP
4262Int TComSlice::getDepthFromDV( Int iDV, Int iBaseViewIdx )
4263{
4264  return ClipY(( iDV * m_aiDDDInvScale[ iBaseViewIdx ] + m_aiDDDInvOffset[ iBaseViewIdx ] ) >> m_aiDDDShift[ iBaseViewIdx ]);
4265}
4266#endif
4267#endif
4268
4269#if HHI_TOOL_PARAMETERS_I2_J0107
4270#if H_3D
4271
4272Void TComSlice::init3dToolParameters()
4273{
4274  Bool depthFlag = getIsDepth();
4275
4276  Bool depthOfRefViewsAvailFlag = false; 
4277  Bool textOfCurViewAvailFlag = false; 
4278
4279  TComVPS* vps = getVPS(); 
4280
4281  if( !depthFlag )
4282  {
4283    depthOfRefViewsAvailFlag = true;
4284    for( Int i = 0; i <= vps->getNumRefListLayers( getLayerId() ) - 1; i++)
4285    {
4286      Bool curDepthAvailableFlag = false;   
4287      for (Int j = 0; j <= vps->getMaxLayersMinus1(); j++ )
4288      {
4289        if ( vps->getDirectDependencyFlag( vps->getLayerIdInVps( getLayerId() ), j )
4290          && vps->getVpsDepthFlag        ( vps->getLayerIdInNuh( j ) ) == 1
4291          && vps->getViewOrderIdx( vps->getLayerIdInNuh( j ) ) == vps->getViewOrderIdx( vps->getIdRefListLayer( getLayerId(), i ) )
4292          && vps->getDependencyId( vps->getLayerIdInNuh( j ) ) == 0
4293          && vps->getAuxId       ( vps->getLayerIdInNuh( j ) ) == 0
4294          )
4295        {
4296          curDepthAvailableFlag = true; 
4297        }
4298      }
4299      if ( !curDepthAvailableFlag )
4300      {
4301        depthOfRefViewsAvailFlag = false; 
4302      }   
4303    }
4304  }
4305  else
4306  {
4307    for (Int j = 0; j <= vps->getMaxLayersMinus1(); j++ )
4308    {
4309      if ( vps->getDirectDependencyFlag( vps->getLayerIdInVps( getLayerId() ), j ) 
4310        && vps->getVpsDepthFlag( vps->getLayerIdInNuh( j ) ) == 0
4311        && vps->getViewOrderIdx( vps->getLayerIdInNuh( j ) ) == getViewIndex() 
4312        && vps->getDependencyId( vps->getLayerIdInNuh( j ) ) == 0
4313        && vps->getAuxId       ( vps->getLayerIdInNuh( j ) ) == 0
4314        )
4315      {       
4316        textOfCurViewAvailFlag = true; 
4317      }
4318    }
4319  }
4320
4321  Bool lidG0  = ( getLayerId() > 0 );
4322  Bool nRLLG0 =  ( getVPS()->getNumRefListLayers( getLayerId() ) > 0 );     
4323
4324  TComSps3dExtension* sps3dExt = getSPS()->getSps3dExtension();
4325
4326  m_ivMvPredFlag           = sps3dExt->getIvMvPredFlag         ( depthFlag ) && lidG0 && nRLLG0                           ;                             
4327  m_ivMvScalingFlag        = sps3dExt->getIvMvScalingFlag      ( depthFlag ) && lidG0                                    ;                             
4328  m_ivResPredFlag          = sps3dExt->getIvResPredFlag        ( depthFlag ) && lidG0 && nRLLG0                           ;                               
4329  m_depthRefinementFlag    = sps3dExt->getDepthRefinementFlag  ( depthFlag ) && lidG0           && depthOfRefViewsAvailFlag;                           
4330  m_viewSynthesisPredFlag  = sps3dExt->getViewSynthesisPredFlag( depthFlag ) && lidG0 && nRLLG0 && depthOfRefViewsAvailFlag;                         
4331  m_depthBasedBlkPartFlag  = sps3dExt->getDepthBasedBlkPartFlag( depthFlag ) && lidG0           && depthOfRefViewsAvailFlag;                         
4332  m_mpiFlag                = sps3dExt->getMpiFlag              ( depthFlag ) && lidG0           &&   textOfCurViewAvailFlag;
4333  m_intraContourFlag       = sps3dExt->getIntraContourFlag     ( depthFlag ) && lidG0           &&   textOfCurViewAvailFlag;
4334  m_intraSdcWedgeFlag      = sps3dExt->getIntraSdcWedgeFlag    ( depthFlag ) && lidG0                                     ;                         
4335  m_qtPredFlag             = sps3dExt->getQtPredFlag           ( depthFlag ) && lidG0           &&   textOfCurViewAvailFlag;
4336  m_interSdcFlag           = sps3dExt->getInterSdcFlag         ( depthFlag ) && lidG0                                    ;                               
4337  m_intraSingleFlag        = sps3dExt->getIntraSingleFlag      ( depthFlag ) && lidG0                                    ;                         
4338
4339  m_subPbSize              = lidG0 ? ( 1 << ( sps3dExt->getLog2SubPbSizeMinus3   ( depthFlag ) + 3 ) ) : getSPS()->getMaxCUWidth(); 
4340  m_mpiSubPbSize           = 1 << ( sps3dExt->getLog2MpiSubPbSizeMinus3( depthFlag ) + 3 );
4341
4342#if H_3D_OUTPUT_ACTIVE_TOOLS
4343  std::cout << "Layer:                  :" << getLayerId()             << std::endl;
4344  std::cout << "DepthFlag:              :" << getIsDepth()             << std::endl;
4345  std::cout << "ViewOrderIdx:           :" << getViewIndex()           << std::endl;
4346  std::cout << "DepthOfRefViewsAvailFlag:" << depthOfRefViewsAvailFlag << std::endl;
4347  std::cout << "TextOfCurViewAvailFlag  :" << textOfCurViewAvailFlag   << std::endl;
4348 
4349  std::cout << "ivMvPredFlag            :" << m_ivMvPredFlag           << std::endl;
4350  std::cout << "ivMvScalingFlag         :" << m_ivMvScalingFlag        << std::endl;
4351  std::cout << "ivResPredFlag           :" << m_ivResPredFlag          << std::endl;
4352  std::cout << "depthRefinementFlag     :" << m_depthRefinementFlag    << std::endl;
4353  std::cout << "viewSynthesisPredFlag   :" << m_viewSynthesisPredFlag  << std::endl;
4354  std::cout << "depthBasedBlkPartFlag   :" << m_depthBasedBlkPartFlag  << std::endl;
4355  std::cout << "mpiFlag                 :" << m_mpiFlag                << std::endl;
4356  std::cout << "intraContourFlag        :" << m_intraContourFlag       << std::endl;
4357  std::cout << "intraSdcWedgeFlag       :" << m_intraSdcWedgeFlag      << std::endl;
4358  std::cout << "qtPredFlag              :" << m_qtPredFlag             << std::endl;
4359  std::cout << "interSdcFlag            :" << m_interSdcFlag           << std::endl;
4360  std::cout << "intraSingleFlag         :" << m_intraSingleFlag        << std::endl;   
4361  std::cout << "subPbSize               :" << m_subPbSize              << std::endl;
4362  std::cout << "mpiSubPbSize            :" << m_mpiSubPbSize           << std::endl;
4363#endif
4364}
4365#endif
4366#endif
4367
4368
4369/** get scaling matrix from RefMatrixID
4370 * \param sizeId size index
4371 * \param Index of input matrix
4372 * \param Index of reference matrix
4373 */
4374Void TComScalingList::processRefMatrix( UInt sizeId, UInt listId , UInt refListId )
4375{
4376  ::memcpy(getScalingListAddress(sizeId, listId),((listId == refListId)? getScalingListDefaultAddress(sizeId, refListId): getScalingListAddress(sizeId, refListId)),sizeof(Int)*min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId]));
4377}
4378
4379/** parse syntax infomation
4380 *  \param pchFile syntax infomation
4381 *  \returns false if successful
4382 */
4383Bool TComScalingList::xParseScalingList(Char* pchFile)
4384{
4385  FILE *fp;
4386  Char line[1024];
4387  UInt sizeIdc,listIdc;
4388  UInt i,size = 0;
4389  Int *src=0,data;
4390  Char *ret;
4391  UInt  retval;
4392
4393  if((fp = fopen(pchFile,"r")) == (FILE*)NULL)
4394  {
4395    printf("can't open file %s :: set Default Matrix\n",pchFile);
4396    return true;
4397  }
4398
4399  for(sizeIdc = 0; sizeIdc < SCALING_LIST_SIZE_NUM; sizeIdc++)
4400  {
4401    size = min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeIdc]);
4402    for(listIdc = 0; listIdc < g_scalingListNum[sizeIdc]; listIdc++)
4403    {
4404      src = getScalingListAddress(sizeIdc, listIdc);
4405
4406      fseek(fp,0,0);
4407      do 
4408      {
4409        ret = fgets(line, 1024, fp);
4410        if ((ret==NULL)||(strstr(line, MatrixType[sizeIdc][listIdc])==NULL && feof(fp)))
4411        {
4412          printf("Error: can't read Matrix :: set Default Matrix\n");
4413          return true;
4414        }
4415      }
4416      while (strstr(line, MatrixType[sizeIdc][listIdc]) == NULL);
4417      for (i=0; i<size; i++)
4418      {
4419        retval = fscanf(fp, "%d,", &data);
4420        if (retval!=1)
4421        {
4422          printf("Error: can't read Matrix :: set Default Matrix\n");
4423          return true;
4424        }
4425        src[i] = data;
4426      }
4427      //set DC value for default matrix check
4428      setScalingListDC(sizeIdc,listIdc,src[0]);
4429
4430      if(sizeIdc > SCALING_LIST_8x8)
4431      {
4432        fseek(fp,0,0);
4433        do 
4434        {
4435          ret = fgets(line, 1024, fp);
4436          if ((ret==NULL)||(strstr(line, MatrixType_DC[sizeIdc][listIdc])==NULL && feof(fp)))
4437          {
4438            printf("Error: can't read DC :: set Default Matrix\n");
4439            return true;
4440          }
4441        }
4442        while (strstr(line, MatrixType_DC[sizeIdc][listIdc]) == NULL);
4443        retval = fscanf(fp, "%d,", &data);
4444        if (retval!=1)
4445        {
4446          printf("Error: can't read Matrix :: set Default Matrix\n");
4447          return true;
4448        }
4449        //overwrite DC value when size of matrix is larger than 16x16
4450        setScalingListDC(sizeIdc,listIdc,data);
4451      }
4452    }
4453  }
4454  fclose(fp);
4455  return false;
4456}
4457
4458#if H_MV
4459Void TComScalingList::inferFrom( TComScalingList* srcScLi )
4460{
4461  for(Int sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
4462  {
4463    for(Int listId = 0; listId <  g_scalingListNum[sizeId]; listId++)
4464    {
4465      setRefMatrixId  (sizeId,listId, srcScLi->getRefMatrixId  (sizeId,listId));
4466      setScalingListDC(sizeId,listId, srcScLi->getScalingListDC(sizeId,listId));         
4467      ::memcpy(getScalingListAddress(sizeId, listId),srcScLi->getScalingListAddress(sizeId, listId),sizeof(Int)*min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId]));
4468    }
4469  }
4470}
4471#endif
4472/** initialization process of quantization matrix array
4473 */
4474Void TComScalingList::init()
4475{
4476  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
4477  {
4478    for(UInt listId = 0; listId < g_scalingListNum[sizeId]; listId++)
4479    {
4480      m_scalingListCoef[sizeId][listId] = new Int [min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId])];
4481    }
4482  }
4483  m_scalingListCoef[SCALING_LIST_32x32][3] = m_scalingListCoef[SCALING_LIST_32x32][1]; // copy address for 32x32
4484}
4485
4486/** destroy quantization matrix array
4487 */
4488Void TComScalingList::destroy()
4489{
4490  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
4491  {
4492    for(UInt listId = 0; listId < g_scalingListNum[sizeId]; listId++)
4493    {
4494      if(m_scalingListCoef[sizeId][listId]) delete [] m_scalingListCoef[sizeId][listId];
4495    }
4496  }
4497}
4498
4499/** get default address of quantization matrix
4500 * \param sizeId size index
4501 * \param listId list index
4502 * \returns pointer of quantization matrix
4503 */
4504Int* TComScalingList::getScalingListDefaultAddress(UInt sizeId, UInt listId)
4505{
4506  Int *src = 0;
4507  switch(sizeId)
4508  {
4509    case SCALING_LIST_4x4:
4510      src = g_quantTSDefault4x4;
4511      break;
4512    case SCALING_LIST_8x8:
4513      src = (listId<3) ? g_quantIntraDefault8x8 : g_quantInterDefault8x8;
4514      break;
4515    case SCALING_LIST_16x16:
4516      src = (listId<3) ? g_quantIntraDefault8x8 : g_quantInterDefault8x8;
4517      break;
4518    case SCALING_LIST_32x32:
4519      src = (listId<1) ? g_quantIntraDefault8x8 : g_quantInterDefault8x8;
4520      break;
4521    default:
4522      assert(0);
4523      src = NULL;
4524      break;
4525  }
4526  return src;
4527}
4528
4529/** process of default matrix
4530 * \param sizeId size index
4531 * \param Index of input matrix
4532 */
4533Void TComScalingList::processDefaultMatrix(UInt sizeId, UInt listId)
4534{
4535  ::memcpy(getScalingListAddress(sizeId, listId),getScalingListDefaultAddress(sizeId,listId),sizeof(Int)*min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId]));
4536  setScalingListDC(sizeId,listId,SCALING_LIST_DC);
4537}
4538
4539/** check DC value of matrix for default matrix signaling
4540 */
4541Void TComScalingList::checkDcOfMatrix()
4542{
4543  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
4544  {
4545    for(UInt listId = 0; listId < g_scalingListNum[sizeId]; listId++)
4546    {
4547      //check default matrix?
4548      if(getScalingListDC(sizeId,listId) == 0)
4549      {
4550        processDefaultMatrix(sizeId, listId);
4551      }
4552    }
4553  }
4554}
4555
4556ParameterSetManager::ParameterSetManager()
4557: m_vpsMap(MAX_NUM_VPS)
4558, m_spsMap(MAX_NUM_SPS)
4559, m_ppsMap(MAX_NUM_PPS)
4560, m_activeVPSId(-1)
4561#if !H_MV
4562, m_activeSPSId(-1)
4563, m_activePPSId(-1)
4564{
4565#else
4566{
4567  for (Int i = 0; i < MAX_NUM_LAYERS; i++ )
4568  {
4569    m_activeSPSId[ i ] = -1; 
4570    m_activePPSId[ i ] = -1; 
4571  }
4572#endif
4573}
4574
4575
4576ParameterSetManager::~ParameterSetManager()
4577{
4578}
4579
4580//! activate a SPS from a active parameter sets SEI message
4581//! \returns true, if activation is successful
4582#if H_MV
4583Bool ParameterSetManager::activateSPSWithSEI(Int spsId, Int layerId )
4584#else
4585Bool ParameterSetManager::activateSPSWithSEI(Int spsId)
4586#endif
4587{
4588  TComSPS *sps = m_spsMap.getPS(spsId);
4589  if (sps)
4590  {
4591    Int vpsId = sps->getVPSId();
4592    if (m_vpsMap.getPS(vpsId))
4593    {
4594      m_activeVPSId = vpsId;
4595#if H_MV
4596      m_activeSPSId[ layerId ] = spsId;
4597#else
4598      m_activeSPSId = spsId;
4599#endif
4600      return true;
4601    }
4602    else
4603    {
4604      printf("Warning: tried to activate SPS using an Active parameter sets SEI message. Referenced VPS does not exist.");
4605    }
4606  }
4607  else
4608  {
4609    printf("Warning: tried to activate non-existing SPS using an Active parameter sets SEI message.");
4610  }
4611  return false;
4612}
4613
4614//! activate a PPS and depending on isIDR parameter also SPS and VPS
4615//! \returns true, if activation is successful
4616#if H_MV
4617Bool ParameterSetManager::activatePPS(Int ppsId, Bool isIRAP, Int layerId )
4618#else
4619Bool ParameterSetManager::activatePPS(Int ppsId, Bool isIRAP)
4620#endif
4621{
4622  TComPPS *pps = m_ppsMap.getPS(ppsId);
4623  if (pps)
4624  {
4625    Int spsId = pps->getSPSId();
4626#if H_MV
4627    if (!isIRAP && (spsId != m_activeSPSId[ layerId ]))
4628#else
4629    if (!isIRAP && (spsId != m_activeSPSId))
4630#endif
4631    {
4632      printf("Warning: tried to activate PPS referring to a inactive SPS at non-IRAP.");
4633      return false;
4634    }
4635
4636    TComSPS *sps = m_spsMap.getPS(spsId);
4637    if (sps)
4638    {
4639      Int vpsId = sps->getVPSId();
4640      if (!isIRAP && (vpsId != m_activeVPSId))
4641      {
4642        printf("Warning: tried to activate PPS referring to a inactive VPS at non-IRAP.");
4643        return false;
4644      }
4645      if (m_vpsMap.getPS(vpsId))
4646      {
4647#if H_MV
4648        m_activePPSId[ layerId ] = ppsId;
4649        m_activeVPSId = vpsId;
4650        m_activeSPSId[ layerId ] = spsId;
4651#else
4652        m_activePPSId = ppsId;
4653        m_activeVPSId = vpsId;
4654        m_activeSPSId = spsId;
4655#endif
4656        return true;
4657      }
4658      else
4659      {
4660        printf("Warning: tried to activate PPS that refers to a non-existing VPS.");
4661      }
4662    }
4663    else
4664    {
4665      printf("Warning: tried to activate a PPS that refers to a non-existing SPS.");
4666    }
4667  }
4668  else
4669  {
4670    printf("Warning: tried to activate non-existing PPS.");
4671  }
4672  return false;
4673}
4674
4675ProfileTierLevel::ProfileTierLevel()
4676  : m_profileSpace    (0)
4677  , m_tierFlag        (false)
4678  , m_profileIdc      (0)
4679  , m_levelIdc        (0)
4680, m_progressiveSourceFlag  (false)
4681, m_interlacedSourceFlag   (false)
4682, m_nonPackedConstraintFlag(false)
4683, m_frameOnlyConstraintFlag(false)
4684#if H_MV
4685  , m_max12bitConstraintFlag      ( false )
4686  , m_max10bitConstraintFlag      ( false )
4687  , m_max8bitConstraintFlag       ( false )
4688  , m_max422chromaConstraintFlag  ( false )
4689  , m_max420chromaConstraintFlag  ( false )
4690  , m_maxMonochromeConstraintFlag ( false )
4691  , m_intraConstraintFlag         ( false )
4692  , m_onePictureOnlyConstraintFlag( false )
4693  , m_lowerBitRateConstraintFlag  ( false )
4694  , m_inbldFlag                   ( false )
4695#endif
4696{
4697  ::memset(m_profileCompatibilityFlag, 0, sizeof(m_profileCompatibilityFlag));
4698}
4699
4700#if H_MV
4701Bool ProfileTierLevel::getV2ConstraintsPresentFlag()
4702{
4703  return ( 
4704    getProfileIdc( ) ==  4 || getProfileCompatibilityFlag( 4 ) || getProfileIdc( ) ==  5 || getProfileCompatibilityFlag( 5 )  ||
4705    getProfileIdc( ) ==  6 || getProfileCompatibilityFlag( 6 ) || getProfileIdc( ) ==  7 || getProfileCompatibilityFlag( 7 ) 
4706    );
4707}
4708
4709Bool ProfileTierLevel::getInbldPresentFlag()
4710{
4711  return ( 
4712    ( getProfileIdc() >= 1 && getProfileIdc() <= 5 )  || getProfileCompatibilityFlag( 1 ) || getProfileCompatibilityFlag( 2 ) || 
4713    getProfileCompatibilityFlag( 3 ) || getProfileCompatibilityFlag( 4 )  ||   getProfileCompatibilityFlag( 5 ) 
4714    );
4715}
4716
4717Void ProfileTierLevel::copyV2ConstraintFlags(ProfileTierLevel* ptlRef)
4718{
4719  setMax12bitConstraintFlag         ( ptlRef->getMax12bitConstraintFlag       ( ) );
4720  setMax10bitConstraintFlag         ( ptlRef->getMax10bitConstraintFlag       ( ) );
4721  setMax8bitConstraintFlag          ( ptlRef->getMax8bitConstraintFlag        ( ) );
4722  setMax422chromaConstraintFlag     ( ptlRef->getMax422chromaConstraintFlag   ( ) );
4723  setMax420chromaConstraintFlag     ( ptlRef->getMax420chromaConstraintFlag   ( ) );
4724  setMaxMonochromeConstraintFlag    ( ptlRef->getMaxMonochromeConstraintFlag  ( ) );
4725  setIntraConstraintFlag            ( ptlRef->getIntraConstraintFlag          ( ) );
4726  setOnePictureOnlyConstraintFlag   ( ptlRef->getOnePictureOnlyConstraintFlag ( ) );
4727  setLowerBitRateConstraintFlag     ( ptlRef->getLowerBitRateConstraintFlag   ( ) );
4728}
4729
4730Void ProfileTierLevel::copyProfile(ProfileTierLevel* ptlRef)
4731{
4732  setProfileSpace            ( ptlRef->getProfileSpace              ( ) );
4733  setTierFlag                ( ptlRef->getTierFlag                  ( ) );
4734  setProfileIdc              ( ptlRef->getProfileIdc                ( ) );
4735  for (Int j = 0; j < 32; j++)
4736  {     
4737    setProfileCompatibilityFlag(j, ptlRef->getProfileCompatibilityFlag  ( j ) );           
4738  }
4739  setProgressiveSourceFlag   ( ptlRef->getProgressiveSourceFlag     ( ) );
4740  setInterlacedSourceFlag    ( ptlRef->getInterlacedSourceFlag      ( ) );
4741  setNonPackedConstraintFlag ( ptlRef->getNonPackedConstraintFlag   ( ) );
4742  setFrameOnlyConstraintFlag ( ptlRef->getFrameOnlyConstraintFlag   ( ) );
4743  copyV2ConstraintFlags      ( ptlRef );
4744}
4745#endif
4746
4747TComPTL::TComPTL()
4748{
4749  ::memset(m_subLayerProfilePresentFlag, 0, sizeof(m_subLayerProfilePresentFlag));
4750  ::memset(m_subLayerLevelPresentFlag,   0, sizeof(m_subLayerLevelPresentFlag  ));
4751}
4752
4753#if H_MV
4754Void TComPTL::inferGeneralValues(Bool profilePresentFlag, Int k, TComPTL* refPTL)
4755{
4756  ProfileTierLevel* refProfileTierLevel = NULL; 
4757  if ( k > 0 )
4758  {   
4759    assert( refPTL != NULL);
4760    refProfileTierLevel = refPTL->getGeneralPTL(); 
4761  }
4762
4763  ProfileTierLevel* curProfileTierLevel = getGeneralPTL( ); 
4764
4765  if( !profilePresentFlag )
4766  {
4767    assert( k > 0 ); 
4768    assert( refProfileTierLevel != NULL ); 
4769    curProfileTierLevel->copyProfile( refProfileTierLevel);
4770  }
4771  else
4772  {
4773    if ( !curProfileTierLevel->getV2ConstraintsPresentFlag() )
4774    {
4775      curProfileTierLevel->setMax12bitConstraintFlag         ( false );
4776      curProfileTierLevel->setMax10bitConstraintFlag         ( false );
4777      curProfileTierLevel->setMax8bitConstraintFlag          ( false );
4778      curProfileTierLevel->setMax422chromaConstraintFlag     ( false );
4779      curProfileTierLevel->setMax420chromaConstraintFlag     ( false );
4780      curProfileTierLevel->setMaxMonochromeConstraintFlag    ( false );
4781      curProfileTierLevel->setIntraConstraintFlag            ( false );
4782      curProfileTierLevel->setOnePictureOnlyConstraintFlag   ( false );
4783      curProfileTierLevel->setLowerBitRateConstraintFlag     ( false );   
4784    }
4785
4786    if ( !curProfileTierLevel->getInbldPresentFlag() )
4787    {
4788      curProfileTierLevel->setInbldFlag( false ); 
4789    }     
4790  }
4791}
4792
4793Void TComPTL::inferSubLayerValues(Int maxNumSubLayersMinus1, Int k, TComPTL* refPTL)
4794{
4795  assert( k == 0 || refPTL != NULL ); 
4796
4797  for (Int i = maxNumSubLayersMinus1; i >= 0; i--)
4798  {
4799    ProfileTierLevel* refProfileTierLevel;
4800    if ( k != 0 )
4801    {
4802      refProfileTierLevel = refPTL->getSubLayerPTL( i );
4803    }
4804    else
4805    {
4806      if ( i == maxNumSubLayersMinus1)     
4807      {
4808        refProfileTierLevel = getGeneralPTL();
4809      }
4810      else
4811      {
4812        refProfileTierLevel = getSubLayerPTL( i + 1 );
4813      }
4814    }   
4815
4816    ProfileTierLevel* curProfileTierLevel = getSubLayerPTL( i ); 
4817    if( !getSubLayerLevelPresentFlag( i ) )
4818    {
4819      curProfileTierLevel->setLevelIdc( refProfileTierLevel->getLevelIdc() ); 
4820    }
4821
4822    if( !getSubLayerProfilePresentFlag( i ) )
4823    {
4824      curProfileTierLevel->copyProfile( refProfileTierLevel);
4825    }
4826    else
4827    {
4828      if ( !curProfileTierLevel->getV2ConstraintsPresentFlag() )
4829      {
4830        curProfileTierLevel->copyV2ConstraintFlags( refProfileTierLevel ); 
4831      }
4832
4833      if ( !curProfileTierLevel->getInbldPresentFlag() )
4834      {
4835        curProfileTierLevel->setInbldFlag( refProfileTierLevel->getInbldFlag() ); 
4836      }     
4837    }     
4838  }
4839}
4840
4841#endif
4842//! \}
4843
4844#if H_MV
4845TComVPSVUI::TComVPSVUI()
4846{
4847  m_crossLayerIrapAlignedFlag = true; 
4848  m_allLayersIdrAlignedFlag   = false; 
4849  m_bitRatePresentVpsFlag = false;
4850  m_picRatePresentVpsFlag = false;
4851  for ( Int i = 0; i < MAX_VPS_OP_SETS_PLUS1; i++)
4852  {   
4853    for ( Int j = 0; j < MAX_TLAYER; j++)
4854    {   
4855      m_bitRatePresentFlag          [i][j] = false;
4856      m_picRatePresentFlag          [i][j] = false;
4857      m_avgBitRate                  [i][j] = -1;
4858      m_maxBitRate                  [i][j] = -1;
4859      m_constantPicRateIdc          [i][j] = -1;
4860      m_avgPicRate                  [i][j] = -1;
4861    }
4862  }
4863
4864  m_ilpRestrictedRefLayersFlag = false;
4865
4866  for ( Int i = 0; i < MAX_NUM_LAYERS; i++)
4867  {         
4868    for ( Int j = 0; j < MAX_NUM_LAYERS; j++)
4869    {   
4870      m_tileBoundariesAlignedFlag   [i][j] = false;
4871      m_minSpatialSegmentOffsetPlus1[i][j] = 0;
4872      m_ctuBasedOffsetEnabledFlag   [i][j] = false;
4873      m_minHorizontalCtuOffsetPlus1 [i][j] = -1;
4874    }
4875    m_baseLayerParameterSetCompatibilityFlag[i] = false;
4876  }
4877  for ( Int i = 0; i < MAX_NUM_VIDEO_SIGNAL_INFO; i++ )
4878  {
4879    m_videoSignalInfo          [i] = NULL;     
4880  }
4881
4882  m_vpsVuiBspHrdPresentFlag = false; 
4883  m_vpsVuiBspHrdParameters  = NULL;
4884}
4885
4886TComVPSVUI::~TComVPSVUI()
4887{
4888  for ( Int i = 0; i < MAX_NUM_VIDEO_SIGNAL_INFO; i++ )
4889  {
4890    if (m_videoSignalInfo[ i ] != NULL )      delete m_videoSignalInfo[ i ];   
4891    m_videoSignalInfo    [ i ] = NULL; 
4892  }
4893
4894  if ( m_vpsVuiBspHrdParameters ) delete m_vpsVuiBspHrdParameters;
4895  m_vpsVuiBspHrdParameters = NULL; 
4896}
4897
4898Void TComVPSVUI::inferVpsVui( Bool encoderFlag )
4899{
4900  // inference of syntax elements that differ from default inference (as done in constructor), when VPS VUI is not present
4901  if (!encoderFlag )
4902  {
4903    setCrossLayerIrapAlignedFlag( false ); 
4904  }
4905  else
4906  {
4907    assert( !getCrossLayerIrapAlignedFlag() ); 
4908  }
4909}
4910
4911Void TComRepFormat::inferChromaAndBitDepth( TComRepFormat* prevRepFormat, Bool encoderFlag )
4912{
4913  if ( !encoderFlag )
4914  {
4915    setChromaAndBitDepthVpsPresentFlag( prevRepFormat->getChromaAndBitDepthVpsPresentFlag() );
4916    setSeparateColourPlaneVpsFlag     ( prevRepFormat->getSeparateColourPlaneVpsFlag     () );
4917    setBitDepthVpsLumaMinus8          ( prevRepFormat->getBitDepthVpsLumaMinus8          () );
4918    setBitDepthVpsChromaMinus8        ( prevRepFormat->getBitDepthVpsChromaMinus8        () );
4919  }
4920  else
4921  {
4922    assert( getChromaAndBitDepthVpsPresentFlag() == prevRepFormat->getChromaAndBitDepthVpsPresentFlag() );
4923    assert( getSeparateColourPlaneVpsFlag     () == prevRepFormat->getSeparateColourPlaneVpsFlag     () );
4924    assert( getBitDepthVpsLumaMinus8          () == prevRepFormat->getBitDepthVpsLumaMinus8          () );
4925    assert( getBitDepthVpsChromaMinus8        () == prevRepFormat->getBitDepthVpsChromaMinus8        () );
4926  }
4927}
4928
4929
4930
4931TComVpsVuiBspHrdParameters::~TComVpsVuiBspHrdParameters()
4932{
4933  delete[] m_cprmsAddPresentFlag; 
4934  delete[] m_numSubLayerHrdMinus1; 
4935  delete[] m_hrdParameters; 
4936
4937  for (Int h = 0; h < m_numOls; h++)
4938  {
4939    if ( h > 0 )
4940    {   
4941      for (Int i = 0; i < getNumSignalledPartitioningSchemes(h)+1; i++)
4942      {
4943        for (Int t = 0; t < m_vps->getMaxSubLayersInLayerSetMinus1( m_vps->olsIdxToLsIdx(h) ) + 1; t++)
4944        {       
4945          for ( Int j = 0; j <= getNumBspSchedulesMinus1(h,i,j);j++ )
4946          {
4947            delete[] m_bspHrdIdx  [h][i][t][j]; 
4948            delete[] m_bspSchedIdx[h][i][t][j];
4949          }
4950        }
4951        delete[] m_numBspSchedulesMinus1[h][i];
4952      }
4953    }
4954
4955    for (Int j = 0; j <= getNumSignalledPartitioningSchemes(h ) + 1; j++ )
4956    {
4957      for (Int k = 0; k <= getNumPartitionsInSchemeMinus1(h,j); k++ )
4958      {
4959        delete[] m_layerIncludedInPartitionFlag[h][j][k];
4960      }       
4961    }
4962    delete[] m_numPartitionsInSchemeMinus1[h];       
4963  }
4964  delete[] m_numSignalledPartitioningSchemes;
4965}
4966
4967
4968Int TComVpsVuiBspHrdParameters::getBspHrdIdxLen(TComVPS* vps)
4969{
4970  return gCeilLog2( vps->getNumHrdParameters() + getVpsNumAddHrdParams() );
4971}
4972
4973Void TComVpsVuiBspHrdParameters::createAfterVpsNumAddHrdParams( TComVPS* vps )
4974{
4975  assert( vps == NULL ); 
4976  m_vps = vps; 
4977  m_offsetHrdParamIdx = vps->getNumHrdParameters(); 
4978  m_numHrdParam       = vps->getNumHrdParameters() + getVpsNumAddHrdParams() - m_offsetHrdParamIdx;
4979  m_numOls            = vps->getNumOutputLayerSets(); 
4980
4981  m_cprmsAddPresentFlag   = new Bool    [ m_numHrdParam ];
4982  m_numSubLayerHrdMinus1  = new Int     [ m_numHrdParam ];
4983  m_hrdParameters         = new TComHRD [ m_numHrdParam ];
4984
4985  m_numSignalledPartitioningSchemes = new Int    [ m_numOls ]; 
4986  m_numPartitionsInSchemeMinus1     = new Int*   [ m_numOls ];
4987  m_numBspSchedulesMinus1           = new Int**  [ m_numOls ]; 
4988  m_bspHrdIdx                       = new Int****[ m_numOls ];
4989  m_bspSchedIdx                     = new Int****[ m_numOls ];
4990}
4991
4992Void TComVpsVuiBspHrdParameters::createAfterNumSignalledPartitioningSchemes( Int h )
4993{
4994  m_numPartitionsInSchemeMinus1 [h]    = new Int    [ getNumSignalledPartitioningSchemes(h) ];
4995  m_layerIncludedInPartitionFlag[h]    = new Bool** [ getNumSignalledPartitioningSchemes(h) ];   
4996
4997  m_numBspSchedulesMinus1[h]           = new Int*   [ getNumSignalledPartitioningSchemes(h) + 1 ];
4998  for (Int i = 0; i < getNumSignalledPartitioningSchemes(h) + 1; i++)
4999  {
5000    Int tMax = m_vps->getMaxSubLayersInLayerSetMinus1( m_vps->olsIdxToLsIdx(h) ) + 1;
5001    m_numBspSchedulesMinus1[h][i] = new Int  [ tMax ];
5002    m_bspHrdIdx            [h][i] = new Int**[ tMax ];
5003    m_bspSchedIdx          [h][i] = new Int**[ tMax ];
5004  }
5005}
5006
5007Void TComVpsVuiBspHrdParameters::createAfterNumPartitionsInSchemeMinus1( Int h, Int j )
5008{
5009  m_layerIncludedInPartitionFlag[h][j] = new Bool*[ getNumPartitionsInSchemeMinus1(h,j)];
5010  for( Int k = 0; k < getNumPartitionsInSchemeMinus1(h,j); k++ )
5011  {
5012    m_layerIncludedInPartitionFlag[h][j][k] = new Bool[ m_vps->getNumLayersInIdList( m_vps->olsIdxToLsIdx(h))];
5013  }
5014}
5015
5016Void TComVpsVuiBspHrdParameters::createAfterNumBspSchedulesMinus1( Int h, Int i, Int t )
5017{
5018  m_bspSchedIdx[h][i][t] = new Int* [ getNumBspSchedulesMinus1( h, i, t ) + 1 ];
5019  m_bspHrdIdx  [h][i][t] = new Int* [ getNumBspSchedulesMinus1( h, i, t ) + 1 ];
5020  for( Int j = 0; j < getNumBspSchedulesMinus1( h, i, t ) + 1; j++ )
5021  {
5022    m_bspSchedIdx[h][i][t][j] = new Int[ getNumPartitionsInSchemeMinus1( h, i ) ];
5023    m_bspHrdIdx  [h][i][t][j] = new Int[ getNumPartitionsInSchemeMinus1( h, i ) ];
5024  }
5025}
5026
5027
5028Void TComVUI::inferVideoSignalInfo( TComVPS* vps, Int layerIdCurr )
5029{
5030  if ( layerIdCurr == 0 || !vps->getVpsVuiPresentFlag() ) 
5031  {
5032    return; 
5033  }
5034
5035  TComVPSVUI* vpsVUI = vps->getVPSVUI(); 
5036  assert( vpsVUI != NULL ); 
5037
5038  TComVideoSignalInfo* videoSignalInfo = vpsVUI->getVideoSignalInfo( vpsVUI->getVpsVideoSignalInfoIdx( vps->getLayerIdInVps( layerIdCurr ) ) ); 
5039  assert( videoSignalInfo != NULL );
5040
5041  setVideoFormat            ( videoSignalInfo->getVideoVpsFormat            () ); 
5042  setVideoFullRangeFlag     ( videoSignalInfo->getVideoFullRangeVpsFlag     () );
5043  setColourPrimaries        ( videoSignalInfo->getColourPrimariesVps        () );
5044  setTransferCharacteristics( videoSignalInfo->getTransferCharacteristicsVps() );
5045  setMatrixCoefficients     ( videoSignalInfo->getMatrixCoeffsVps           () );
5046}
5047
5048TComDpbSize::TComDpbSize()
5049{
5050  for (Int i = 0; i < MAX_VPS_OUTPUTLAYER_SETS; i++ )
5051  {     
5052    m_subLayerFlagInfoPresentFlag[i]  = false;
5053
5054    for (Int j = 0; j < MAX_TLAYER; j++  )
5055    {       
5056      m_subLayerDpbInfoPresentFlag [i][j] = ( j == 0) ;
5057      m_maxVpsNumReorderPics       [i][j] = 0;
5058      m_maxVpsLatencyIncreasePlus1 [i][j] = 0;
5059
5060      for (Int k = 0; k < MAX_NUM_LAYER_IDS; k++ )
5061      {
5062        m_maxVpsDecPicBufferingMinus1[i][k][j] = MIN_INT; 
5063      }
5064    }
5065  }
5066}
5067
5068Void Window::scaleOffsets( Int scal )
5069{
5070  if (! m_scaledFlag )
5071  {
5072    m_scaledFlag         = true; 
5073    m_winLeftOffset     *= scal; 
5074    m_winRightOffset    *= scal; 
5075    m_winTopOffset      *= scal; 
5076    m_winBottomOffset   *= scal; 
5077  }
5078}
5079#endif
5080
Note: See TracBrowser for help on using the repository browser.