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

Last change on this file since 1100 was 1100, checked in by samsung-htm, 9 years ago

Integration of JCT3V-J0037 (Item1 and Item4)

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