source: 3DVCSoftware/branches/HTM-12.2-dev1-Hisilicon/source/Lib/TLibCommon/TComSlice.cpp @ 1404

Last change on this file since 1404 was 1099, checked in by sharpjp-htm, 10 years ago

Integration of J0029

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