source: 3DVCSoftware/branches/HTM-10.2-dev3-MediaTek/source/Lib/TLibCommon/TComSlice.cpp @ 902

Last change on this file since 902 was 902, checked in by mediatek-htm, 10 years ago

Integration of low-latency IC encoding as proposed in JCT3V-H0086.
The MACRO is "MTK_LOW_LATENCY_IC_ENCODING_H0086"
The configuration files are modified by adding one additional control parameter "IlluCompLowLatencyEnc" which is set to 0 for CTC.

By Yi-Wen Chen (yiwen.chen@…)

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