source: 3DVCSoftware/branches/HTM-12.0-dev1/source/Lib/TLibCommon/TComSlice.cpp @ 1060

Last change on this file since 1060 was 1060, checked in by tech, 10 years ago

Added

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