source: 3DVCSoftware/trunk/source/Lib/TLibCommon/TComSlice.cpp @ 976

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