source: 3DVCSoftware/branches/HTM-DEV-0.3-dev1/source/Lib/TLibCommon/TComSlice.cpp @ 459

Last change on this file since 459 was 459, checked in by hhi, 11 years ago

Integation of depth intra methods in macro H_3D_DIM, including:

  • DMM coding modes in H_3D_DIM_DMM.
  • RBC coding mode in H_3D_DIM_RBC.
  • Property svn:eol-style set to native
File size: 78.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-2013, 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#if !L0034_COMBINED_LIST_CLEANUP
63, m_bRefPicListModificationFlagLC ( false )
64, m_bRefPicListCombinationFlag    ( false )
65#endif
66, m_bCheckLDC                     ( false )
67, m_iSliceQpDelta                 ( 0 )
68, m_iSliceQpDeltaCb               ( 0 )
69, m_iSliceQpDeltaCr               ( 0 )
70, m_iDepth                        ( 0 )
71, m_bRefenced                     ( false )
72, m_pcSPS                         ( NULL )
73, m_pcPPS                         ( NULL )
74, m_pcPic                         ( NULL )
75#if H_3D
76, m_picLists                      ( NULL )
77#endif
78, m_colFromL0Flag                 ( 1 )
79, m_colRefIdx                     ( 0 )
80#if SAO_CHROMA_LAMBDA
81, m_dLambdaLuma( 0.0 )
82, m_dLambdaChroma( 0.0 )
83#else
84, m_dLambda                       ( 0.0 )
85#endif
86#if !L0034_COMBINED_LIST_CLEANUP
87, m_bNoBackPredFlag               ( false )
88#endif
89, m_uiTLayer                      ( 0 )
90, m_bTLayerSwitchingFlag          ( false )
91, m_sliceMode                   ( 0 )
92, m_sliceArgument               ( 0 )
93, m_sliceCurStartCUAddr         ( 0 )
94, m_sliceCurEndCUAddr           ( 0 )
95, m_sliceIdx                    ( 0 )
96, m_sliceSegmentMode            ( 0 )
97, m_sliceSegmentArgument        ( 0 )
98, m_sliceSegmentCurStartCUAddr  ( 0 )
99, m_sliceSegmentCurEndCUAddr    ( 0 )
100, m_nextSlice                    ( false )
101, m_nextSliceSegment             ( false )
102, m_sliceBits                   ( 0 )
103, m_sliceSegmentBits         ( 0 )
104, m_bFinalized                    ( false )
105, m_uiTileOffstForMultES          ( 0 )
106, m_puiSubstreamSizes             ( NULL )
107, m_cabacInitFlag                 ( false )
108, m_bLMvdL1Zero                   ( false )
109, m_numEntryPointOffsets          ( 0 )
110, m_temporalLayerNonReferenceFlag ( false )
111, m_enableTMVPFlag                ( true )
112#if H_MV
113, m_layerId                       (0)
114, m_viewId                        (0)
115#if H_3D
116, m_viewIndex                     (0)
117, m_isDepth                       (false)
118#endif
119#endif
120{
121#if L0034_COMBINED_LIST_CLEANUP
122  m_aiNumRefIdx[0] = m_aiNumRefIdx[1] = 0;
123#else
124  m_aiNumRefIdx[0] = m_aiNumRefIdx[1] = m_aiNumRefIdx[2] = 0;
125#endif
126 
127  initEqualRef();
128 
129#if L0034_COMBINED_LIST_CLEANUP
130  for ( Int idx = 0; idx < MAX_NUM_REF; idx++ )
131  {
132    m_list1IdxToList0Idx[idx] = -1;
133  }
134#else
135  for(Int iNumCount = 0; iNumCount < MAX_NUM_REF_LC; iNumCount++)
136  {
137    m_iRefIdxOfLC[REF_PIC_LIST_0][iNumCount]=-1;
138    m_iRefIdxOfLC[REF_PIC_LIST_1][iNumCount]=-1;
139    m_eListIdFromIdxOfLC[iNumCount]=0;
140    m_iRefIdxFromIdxOfLC[iNumCount]=0;
141    m_iRefIdxOfL0FromRefIdxOfL1[iNumCount] = -1;
142    m_iRefIdxOfL1FromRefIdxOfL0[iNumCount] = -1;
143  }   
144#endif
145  for(Int iNumCount = 0; iNumCount < MAX_NUM_REF; iNumCount++)
146  {
147    m_apcRefPicList [0][iNumCount] = NULL;
148    m_apcRefPicList [1][iNumCount] = NULL;
149    m_aiRefPOCList  [0][iNumCount] = 0;
150    m_aiRefPOCList  [1][iNumCount] = 0;
151#if H_MV
152    m_aiRefLayerIdList[0][iNumCount] = 0;
153    m_aiRefLayerIdList[1][iNumCount] = 0;
154#endif
155  }
156  resetWpScaling();
157  initWpAcDcParam();
158  m_saoEnabledFlag = false;
159}
160
161TComSlice::~TComSlice()
162{
163  delete[] m_puiSubstreamSizes;
164  m_puiSubstreamSizes = NULL;
165}
166
167
168Void TComSlice::initSlice()
169{
170  m_aiNumRefIdx[0]      = 0;
171  m_aiNumRefIdx[1]      = 0;
172 
173  m_colFromL0Flag = 1;
174 
175  m_colRefIdx = 0;
176  initEqualRef();
177#if !L0034_COMBINED_LIST_CLEANUP
178  m_bNoBackPredFlag = false;
179  m_bRefPicListCombinationFlag = false;
180  m_bRefPicListModificationFlagLC = false;
181#endif
182  m_bCheckLDC = false;
183  m_iSliceQpDeltaCb = 0;
184  m_iSliceQpDeltaCr = 0;
185
186#if !L0034_COMBINED_LIST_CLEANUP
187  m_aiNumRefIdx[REF_PIC_LIST_C]      = 0;
188#endif
189
190  m_maxNumMergeCand = MRG_MAX_NUM_CANDS;
191
192  m_bFinalized=false;
193
194  m_tileByteLocation.clear();
195  m_cabacInitFlag        = false;
196  m_numEntryPointOffsets = 0;
197  m_enableTMVPFlag = true;
198}
199
200Bool TComSlice::getRapPicFlag()
201{
202  return getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_W_RADL
203      || getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_N_LP
204      || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP
205      || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL
206      || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP
207      || getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA;
208}
209
210/**
211 - allocate table to contain substream sizes to be written to the slice header.
212 .
213 \param uiNumSubstreams Number of substreams -- the allocation will be this value - 1.
214 */
215Void  TComSlice::allocSubstreamSizes(UInt uiNumSubstreams)
216{
217  delete[] m_puiSubstreamSizes;
218  m_puiSubstreamSizes = new UInt[uiNumSubstreams > 0 ? uiNumSubstreams-1 : 0];
219}
220
221Void  TComSlice::sortPicList        (TComList<TComPic*>& rcListPic)
222{
223  TComPic*    pcPicExtract;
224  TComPic*    pcPicInsert;
225 
226  TComList<TComPic*>::iterator    iterPicExtract;
227  TComList<TComPic*>::iterator    iterPicExtract_1;
228  TComList<TComPic*>::iterator    iterPicInsert;
229 
230  for (Int i = 1; i < (Int)(rcListPic.size()); i++)
231  {
232    iterPicExtract = rcListPic.begin();
233    for (Int j = 0; j < i; j++) iterPicExtract++;
234    pcPicExtract = *(iterPicExtract);
235    pcPicExtract->setCurrSliceIdx(0);
236   
237    iterPicInsert = rcListPic.begin();
238    while (iterPicInsert != iterPicExtract)
239    {
240      pcPicInsert = *(iterPicInsert);
241      pcPicInsert->setCurrSliceIdx(0);
242      if (pcPicInsert->getPOC() >= pcPicExtract->getPOC())
243      {
244        break;
245      }
246     
247      iterPicInsert++;
248    }
249   
250    iterPicExtract_1 = iterPicExtract;    iterPicExtract_1++;
251   
252    //  swap iterPicExtract and iterPicInsert, iterPicExtract = curr. / iterPicInsert = insertion position
253    rcListPic.insert (iterPicInsert, iterPicExtract, iterPicExtract_1);
254    rcListPic.erase  (iterPicExtract);
255  }
256}
257
258TComPic* TComSlice::xGetRefPic (TComList<TComPic*>& rcListPic,
259                                Int                 poc)
260{
261  TComList<TComPic*>::iterator  iterPic = rcListPic.begin(); 
262  TComPic*                      pcPic = *(iterPic);
263  while ( iterPic != rcListPic.end() )
264  {
265    if(pcPic->getPOC() == poc)
266    {
267      break;
268    }
269    iterPic++;
270    pcPic = *(iterPic);
271  }
272  return  pcPic;
273}
274
275
276TComPic* TComSlice::xGetLongTermRefPic(TComList<TComPic*>& rcListPic, Int poc, Bool pocHasMsb)
277{
278  TComList<TComPic*>::iterator  iterPic = rcListPic.begin(); 
279  TComPic*                      pcPic = *(iterPic);
280  TComPic*                      pcStPic = pcPic;
281 
282  Int pocCycle = 1 << getSPS()->getBitsForPOC();
283  if (!pocHasMsb)
284  {
285    poc = poc % pocCycle;
286  }
287 
288  while ( iterPic != rcListPic.end() )
289  {
290    pcPic = *(iterPic);
291    if (pcPic && pcPic->getPOC()!=this->getPOC() && pcPic->getSlice( 0 )->isReferenced())
292    {
293      Int picPoc = pcPic->getPOC();
294      if (!pocHasMsb)
295      {
296        picPoc = picPoc % pocCycle;
297      }
298     
299      if (poc == picPoc)
300    {
301      if(pcPic->getIsLongTerm())
302      {
303        return pcPic;
304      }
305      else
306      {
307        pcStPic = pcPic;
308      }
309      break;
310    }
311    }
312
313    iterPic++;
314  }
315 
316  return  pcStPic;
317}
318
319Void TComSlice::setRefPOCList       ()
320{
321  for (Int iDir = 0; iDir < 2; iDir++)
322  {
323    for (Int iNumRefIdx = 0; iNumRefIdx < m_aiNumRefIdx[iDir]; iNumRefIdx++)
324    {
325      m_aiRefPOCList[iDir][iNumRefIdx] = m_apcRefPicList[iDir][iNumRefIdx]->getPOC();
326#if H_MV
327      m_aiRefLayerIdList[iDir][iNumRefIdx] = m_apcRefPicList[iDir][iNumRefIdx]->getLayerId();
328#endif
329    }
330  }
331
332}
333
334#if L0034_COMBINED_LIST_CLEANUP
335Void TComSlice::setList1IdxToList0Idx()
336{
337  Int idxL0, idxL1;
338  for ( idxL1 = 0; idxL1 < getNumRefIdx( REF_PIC_LIST_1 ); idxL1++ )
339  {
340    m_list1IdxToList0Idx[idxL1] = -1;
341    for ( idxL0 = 0; idxL0 < getNumRefIdx( REF_PIC_LIST_0 ); idxL0++ )
342    {
343      if ( m_apcRefPicList[REF_PIC_LIST_0][idxL0]->getPOC() == m_apcRefPicList[REF_PIC_LIST_1][idxL1]->getPOC() )
344      {
345        m_list1IdxToList0Idx[idxL1] = idxL0;
346        break;
347      }
348    }
349  }
350}
351#else
352Void TComSlice::generateCombinedList()
353{
354  if(m_aiNumRefIdx[REF_PIC_LIST_C] > 0)
355  {
356    m_aiNumRefIdx[REF_PIC_LIST_C]=0;
357    for(Int iNumCount = 0; iNumCount < MAX_NUM_REF_LC; iNumCount++)
358    {
359      m_iRefIdxOfLC[REF_PIC_LIST_0][iNumCount]=-1;
360      m_iRefIdxOfLC[REF_PIC_LIST_1][iNumCount]=-1;
361      m_eListIdFromIdxOfLC[iNumCount]=0;
362      m_iRefIdxFromIdxOfLC[iNumCount]=0;
363      m_iRefIdxOfL0FromRefIdxOfL1[iNumCount] = -1;
364      m_iRefIdxOfL1FromRefIdxOfL0[iNumCount] = -1;
365    }
366
367    for (Int iNumRefIdx = 0; iNumRefIdx < MAX_NUM_REF; iNumRefIdx++)
368    {
369      if(iNumRefIdx < m_aiNumRefIdx[REF_PIC_LIST_0])
370      {
371        Bool bTempRefIdxInL2 = true;
372        for ( Int iRefIdxLC = 0; iRefIdxLC < m_aiNumRefIdx[REF_PIC_LIST_C]; iRefIdxLC++ )
373        {
374#if H_MV
375          if ( m_apcRefPicList[REF_PIC_LIST_0][iNumRefIdx]->getPOC() == m_apcRefPicList[m_eListIdFromIdxOfLC[iRefIdxLC]][m_iRefIdxFromIdxOfLC[iRefIdxLC]]->getPOC() &&
376               m_apcRefPicList[REF_PIC_LIST_0][iNumRefIdx]->getLayerId() == m_apcRefPicList[m_eListIdFromIdxOfLC[iRefIdxLC]][m_iRefIdxFromIdxOfLC[iRefIdxLC]]->getLayerId() )
377#else
378          if ( m_apcRefPicList[REF_PIC_LIST_0][iNumRefIdx]->getPOC() == m_apcRefPicList[m_eListIdFromIdxOfLC[iRefIdxLC]][m_iRefIdxFromIdxOfLC[iRefIdxLC]]->getPOC() )
379#endif
380          {
381            m_iRefIdxOfL1FromRefIdxOfL0[iNumRefIdx] = m_iRefIdxFromIdxOfLC[iRefIdxLC];
382            m_iRefIdxOfL0FromRefIdxOfL1[m_iRefIdxFromIdxOfLC[iRefIdxLC]] = iNumRefIdx;
383            bTempRefIdxInL2 = false;
384            break;
385          }
386        }
387
388        if(bTempRefIdxInL2 == true)
389        { 
390          m_eListIdFromIdxOfLC[m_aiNumRefIdx[REF_PIC_LIST_C]] = REF_PIC_LIST_0;
391          m_iRefIdxFromIdxOfLC[m_aiNumRefIdx[REF_PIC_LIST_C]] = iNumRefIdx;
392          m_iRefIdxOfLC[REF_PIC_LIST_0][iNumRefIdx] = m_aiNumRefIdx[REF_PIC_LIST_C]++;
393        }
394      }
395
396      if(iNumRefIdx < m_aiNumRefIdx[REF_PIC_LIST_1])
397      {
398        Bool bTempRefIdxInL2 = true;
399        for ( Int iRefIdxLC = 0; iRefIdxLC < m_aiNumRefIdx[REF_PIC_LIST_C]; iRefIdxLC++ )
400        {
401#if H_MV
402          if ( m_apcRefPicList[REF_PIC_LIST_1][iNumRefIdx]->getPOC() == m_apcRefPicList[m_eListIdFromIdxOfLC[iRefIdxLC]][m_iRefIdxFromIdxOfLC[iRefIdxLC]]->getPOC() &&
403               m_apcRefPicList[REF_PIC_LIST_1][iNumRefIdx]->getLayerId() == m_apcRefPicList[m_eListIdFromIdxOfLC[iRefIdxLC]][m_iRefIdxFromIdxOfLC[iRefIdxLC]]->getLayerId() )
404#else
405          if ( m_apcRefPicList[REF_PIC_LIST_1][iNumRefIdx]->getPOC() == m_apcRefPicList[m_eListIdFromIdxOfLC[iRefIdxLC]][m_iRefIdxFromIdxOfLC[iRefIdxLC]]->getPOC() )
406#endif
407          {
408            m_iRefIdxOfL0FromRefIdxOfL1[iNumRefIdx] = m_iRefIdxFromIdxOfLC[iRefIdxLC];
409            m_iRefIdxOfL1FromRefIdxOfL0[m_iRefIdxFromIdxOfLC[iRefIdxLC]] = iNumRefIdx;
410            bTempRefIdxInL2 = false;
411            break;
412          }
413        }
414        if(bTempRefIdxInL2 == true)
415        {
416          m_eListIdFromIdxOfLC[m_aiNumRefIdx[REF_PIC_LIST_C]] = REF_PIC_LIST_1;
417          m_iRefIdxFromIdxOfLC[m_aiNumRefIdx[REF_PIC_LIST_C]] = iNumRefIdx;
418          m_iRefIdxOfLC[REF_PIC_LIST_1][iNumRefIdx] = m_aiNumRefIdx[REF_PIC_LIST_C]++;
419        }
420      }
421    }
422  }
423}
424#endif
425
426#if H_MV
427Void TComSlice::setRefPicList( TComList<TComPic*>& rcListPic, std::vector<TComPic*>& refPicSetInterLayer , Bool checkNumPocTotalCurr)
428#else
429#if FIX1071
430Void TComSlice::setRefPicList( TComList<TComPic*>& rcListPic, Bool checkNumPocTotalCurr )
431#else
432Void TComSlice::setRefPicList( TComList<TComPic*>& rcListPic )
433#endif
434#endif
435{
436#if FIX1071
437  if (!checkNumPocTotalCurr)
438#endif
439  {
440    if (m_eSliceType == I_SLICE)
441    {
442      ::memset( m_apcRefPicList, 0, sizeof (m_apcRefPicList));
443      ::memset( m_aiNumRefIdx,   0, sizeof ( m_aiNumRefIdx ));
444     
445      return;
446    }
447   
448#if !H_MV
449    m_aiNumRefIdx[0] = getNumRefIdx(REF_PIC_LIST_0);
450    m_aiNumRefIdx[1] = getNumRefIdx(REF_PIC_LIST_1);
451#endif
452  }
453
454  TComPic*  pcRefPic= NULL;
455  TComPic*  RefPicSetStCurr0[16];
456  TComPic*  RefPicSetStCurr1[16];
457  TComPic*  RefPicSetLtCurr[16];
458  UInt NumPocStCurr0 = 0;
459  UInt NumPocStCurr1 = 0;
460  UInt NumPocLtCurr = 0;
461#if H_MV
462  Int numDirectRefLayers  = getVPS()->getNumDirectRefLayers( getLayerIdInVps() );
463  assert( numDirectRefLayers == refPicSetInterLayer.size() ); 
464#endif
465  Int i;
466  for(i=0; i < m_pcRPS->getNumberOfNegativePictures(); i++)
467  {
468    if(m_pcRPS->getUsed(i))
469    {
470      pcRefPic = xGetRefPic(rcListPic, getPOC()+m_pcRPS->getDeltaPOC(i));
471      pcRefPic->setIsLongTerm(0);
472      pcRefPic->getPicYuvRec()->extendPicBorder();
473      RefPicSetStCurr0[NumPocStCurr0] = pcRefPic;
474      NumPocStCurr0++;
475      pcRefPic->setCheckLTMSBPresent(false); 
476    }
477  }
478 
479  for(; i < m_pcRPS->getNumberOfNegativePictures()+m_pcRPS->getNumberOfPositivePictures(); i++)
480  {
481    if(m_pcRPS->getUsed(i))
482    {
483      pcRefPic = xGetRefPic(rcListPic, getPOC()+m_pcRPS->getDeltaPOC(i));
484      pcRefPic->setIsLongTerm(0);
485      pcRefPic->getPicYuvRec()->extendPicBorder();
486      RefPicSetStCurr1[NumPocStCurr1] = pcRefPic;
487      NumPocStCurr1++;
488      pcRefPic->setCheckLTMSBPresent(false); 
489    }
490  }
491 
492  for(i = m_pcRPS->getNumberOfNegativePictures()+m_pcRPS->getNumberOfPositivePictures()+m_pcRPS->getNumberOfLongtermPictures()-1; i > m_pcRPS->getNumberOfNegativePictures()+m_pcRPS->getNumberOfPositivePictures()-1 ; i--)
493  {
494    if(m_pcRPS->getUsed(i))
495    {
496      pcRefPic = xGetLongTermRefPic(rcListPic, m_pcRPS->getPOC(i), m_pcRPS->getCheckLTMSBPresent(i));
497      pcRefPic->setIsLongTerm(1);
498      pcRefPic->getPicYuvRec()->extendPicBorder();
499      RefPicSetLtCurr[NumPocLtCurr] = pcRefPic;
500      NumPocLtCurr++;
501    }
502    if(pcRefPic==NULL) 
503    {
504      pcRefPic = xGetLongTermRefPic(rcListPic, m_pcRPS->getPOC(i), m_pcRPS->getCheckLTMSBPresent(i));
505    }
506    pcRefPic->setCheckLTMSBPresent(m_pcRPS->getCheckLTMSBPresent(i)); 
507  }
508
509  // ref_pic_list_init
510  TComPic*  rpsCurrList0[MAX_NUM_REF+1];
511  TComPic*  rpsCurrList1[MAX_NUM_REF+1];
512#if H_MV
513  Int numPocTotalCurr = NumPocStCurr0 + NumPocStCurr1 + NumPocLtCurr + numDirectRefLayers;
514  assert( numPocTotalCurr == getNumRpsCurrTempList() );
515#else
516  Int numPocTotalCurr = NumPocStCurr0 + NumPocStCurr1 + NumPocLtCurr;
517#endif
518#if FIX1071
519  if (checkNumPocTotalCurr)
520  {
521    // 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:
522#if H_MV
523    // – 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.
524    // – Otherwise, when the current picture contains a P or B slice, the value of NumPocTotalCurr shall not be equal to 0.
525    if ( getRapPicFlag() && m_layerId == 0 )
526#else
527    // – If the current picture is a BLA or CRA picture, the value of NumPocTotalCurr shall be equal to 0.
528    // – Otherwise, when the current picture contains a P or B slice, the value of NumPocTotalCurr shall not be equal to 0.
529    if (getRapPicFlag())
530#endif
531    {
532      assert(numPocTotalCurr == 0);
533    }
534
535    if (m_eSliceType == I_SLICE)
536    {
537      ::memset( m_apcRefPicList, 0, sizeof (m_apcRefPicList));
538      ::memset( m_aiNumRefIdx,   0, sizeof ( m_aiNumRefIdx ));
539     
540      return;
541    }
542   
543    assert(numPocTotalCurr != 0);
544   
545    m_aiNumRefIdx[0] = getNumRefIdx(REF_PIC_LIST_0);
546    m_aiNumRefIdx[1] = getNumRefIdx(REF_PIC_LIST_1);
547  }
548#endif
549
550  Int cIdx = 0;
551  for ( i=0; i<NumPocStCurr0; i++, cIdx++)
552  {
553    rpsCurrList0[cIdx] = RefPicSetStCurr0[i];
554  }
555  for ( i=0; i<NumPocStCurr1; i++, cIdx++)
556  {
557    rpsCurrList0[cIdx] = RefPicSetStCurr1[i];
558  }
559  for ( i=0; i<NumPocLtCurr;  i++, cIdx++)
560  {
561    rpsCurrList0[cIdx] = RefPicSetLtCurr[i];
562  }
563#if H_MV
564  for ( i=0; i<numDirectRefLayers;  i++, cIdx++)
565  {
566    if( cIdx <= MAX_NUM_REF )
567    {
568      rpsCurrList0[cIdx] = refPicSetInterLayer[i];
569    }
570  }
571#endif
572
573  if (m_eSliceType==B_SLICE)
574  {
575    cIdx = 0;
576    for ( i=0; i<NumPocStCurr1; i++, cIdx++)
577    {
578      rpsCurrList1[cIdx] = RefPicSetStCurr1[i];
579    }
580    for ( i=0; i<NumPocStCurr0; i++, cIdx++)
581    {
582      rpsCurrList1[cIdx] = RefPicSetStCurr0[i];
583    }
584    for ( i=0; i<NumPocLtCurr;  i++, cIdx++)
585    {
586      rpsCurrList1[cIdx] = RefPicSetLtCurr[i];
587    }
588#if H_MV
589    for ( i=0; i<numDirectRefLayers;  i++, cIdx++)
590    {
591      if( cIdx <= MAX_NUM_REF )
592      {
593        rpsCurrList1[cIdx] = refPicSetInterLayer[i];
594      }
595    }
596#endif
597  }
598
599  ::memset(m_bIsUsedAsLongTerm, 0, sizeof(m_bIsUsedAsLongTerm));
600
601  for (Int rIdx = 0; rIdx <= (m_aiNumRefIdx[0]-1); rIdx ++)
602  {
603    m_apcRefPicList[0][rIdx] = m_RefPicListModification.getRefPicListModificationFlagL0() ? rpsCurrList0[ m_RefPicListModification.getRefPicSetIdxL0(rIdx) ] : rpsCurrList0[rIdx % numPocTotalCurr];
604    m_bIsUsedAsLongTerm[0][rIdx] = m_RefPicListModification.getRefPicListModificationFlagL0() ? (m_RefPicListModification.getRefPicSetIdxL0(rIdx) >= (NumPocStCurr0 + NumPocStCurr1))
605                                  : ((rIdx % numPocTotalCurr) >= (NumPocStCurr0 + NumPocStCurr1));
606  }
607  if ( m_eSliceType == P_SLICE )
608  {
609    m_aiNumRefIdx[1] = 0;
610    ::memset( m_apcRefPicList[1], 0, sizeof(m_apcRefPicList[1]));
611  }
612  else
613  {
614    for (Int rIdx = 0; rIdx <= (m_aiNumRefIdx[1]-1); rIdx ++)
615    {
616      m_apcRefPicList[1][rIdx] = m_RefPicListModification.getRefPicListModificationFlagL1() ? rpsCurrList1[ m_RefPicListModification.getRefPicSetIdxL1(rIdx) ] : rpsCurrList1[rIdx % numPocTotalCurr];
617      m_bIsUsedAsLongTerm[1][rIdx] = m_RefPicListModification.getRefPicListModificationFlagL1() ?
618                                  (m_RefPicListModification.getRefPicSetIdxL1(rIdx) >= (NumPocStCurr0 + NumPocStCurr1)): ((rIdx % numPocTotalCurr) >= (NumPocStCurr0 + NumPocStCurr1));
619    }
620  }
621}
622
623#if H_MV
624// Temporary fix for FIX1071 should be removed later
625Int TComSlice::getNumRpsCurrTempList( TComReferencePictureSet* rps /* = 0 */)
626#else
627Int TComSlice::getNumRpsCurrTempList()
628#endif
629{
630
631  Int numRpsCurrTempList = 0;
632
633  if (m_eSliceType == I_SLICE) 
634  {
635    return 0;
636  }
637#if H_MV
638// Temporary fix for FIX1071 should be removed later
639  if (rps == NULL)
640  {
641    rps = m_pcRPS; 
642  }
643
644  for(UInt i=0; i < rps->getNumberOfNegativePictures()+ rps->getNumberOfPositivePictures() + rps->getNumberOfLongtermPictures(); i++)
645  {
646    if(rps->getUsed(i))
647#else
648  for(UInt i=0; i < m_pcRPS->getNumberOfNegativePictures()+ m_pcRPS->getNumberOfPositivePictures() + m_pcRPS->getNumberOfLongtermPictures(); i++)
649  {
650    if(m_pcRPS->getUsed(i))
651#endif
652    {
653      numRpsCurrTempList++;
654    }
655  }
656#if H_MV
657  numRpsCurrTempList = numRpsCurrTempList + getVPS()->getNumDirectRefLayers( getLayerIdInVps() );
658#endif
659  return numRpsCurrTempList;
660}
661
662Void TComSlice::initEqualRef()
663{
664  for (Int iDir = 0; iDir < 2; iDir++)
665  {
666    for (Int iRefIdx1 = 0; iRefIdx1 < MAX_NUM_REF; iRefIdx1++)
667    {
668      for (Int iRefIdx2 = iRefIdx1; iRefIdx2 < MAX_NUM_REF; iRefIdx2++)
669      {
670        m_abEqualRef[iDir][iRefIdx1][iRefIdx2] = m_abEqualRef[iDir][iRefIdx2][iRefIdx1] = (iRefIdx1 == iRefIdx2? true : false);
671      }
672    }
673  }
674}
675#if H_3D
676Void TComSlice::setCamparaSlice( Int** aaiScale, Int** aaiOffset )
677{ 
678  if( m_pcSPS->hasCamParInSliceHeader() )
679  {   
680    for( UInt uiBaseViewIndex = 0; uiBaseViewIndex < m_viewIndex; uiBaseViewIndex++ )
681    {
682      m_aaiCodedScale [ 0 ][ uiBaseViewIndex ] = aaiScale [ uiBaseViewIndex ][     m_viewIndex ];
683      m_aaiCodedScale [ 1 ][ uiBaseViewIndex ] = aaiScale [     m_viewIndex ][ uiBaseViewIndex ];
684      m_aaiCodedOffset[ 0 ][ uiBaseViewIndex ] = aaiOffset[ uiBaseViewIndex ][     m_viewIndex ];
685      m_aaiCodedOffset[ 1 ][ uiBaseViewIndex ] = aaiOffset[     m_viewIndex ][ uiBaseViewIndex ];
686    }
687  } 
688}
689#endif
690
691Void TComSlice::checkColRefIdx(UInt curSliceIdx, TComPic* pic)
692{
693  Int i;
694  TComSlice* curSlice = pic->getSlice(curSliceIdx);
695  Int currColRefPOC =  curSlice->getRefPOC( RefPicList(1-curSlice->getColFromL0Flag()), curSlice->getColRefIdx());
696  TComSlice* preSlice;
697  Int preColRefPOC;
698  for(i=curSliceIdx-1; i>=0; i--)
699  {
700    preSlice = pic->getSlice(i);
701    if(preSlice->getSliceType() != I_SLICE)
702    {
703      preColRefPOC  = preSlice->getRefPOC( RefPicList(1-preSlice->getColFromL0Flag()), preSlice->getColRefIdx());
704      if(currColRefPOC != preColRefPOC)
705      {
706        printf("Collocated_ref_idx shall always be the same for all slices of a coded picture!\n");
707        exit(EXIT_FAILURE);
708      }
709      else
710      {
711        break;
712      }
713    }
714  }
715}
716
717Void TComSlice::checkCRA(TComReferencePictureSet *pReferencePictureSet, Int& pocCRA, Bool& prevRAPisBLA, TComList<TComPic *>& rcListPic)
718{
719  for(Int i = 0; i < pReferencePictureSet->getNumberOfNegativePictures()+pReferencePictureSet->getNumberOfPositivePictures(); i++)
720  {
721    if(pocCRA < MAX_UINT && getPOC() > pocCRA)
722    {
723      assert(getPOC()+pReferencePictureSet->getDeltaPOC(i) >= pocCRA);
724    }
725  }
726  for(Int i = pReferencePictureSet->getNumberOfNegativePictures()+pReferencePictureSet->getNumberOfPositivePictures(); i < pReferencePictureSet->getNumberOfPictures(); i++)
727  {
728    if(pocCRA < MAX_UINT && getPOC() > pocCRA)
729    {
730      if (!pReferencePictureSet->getCheckLTMSBPresent(i))
731      {
732        assert(xGetLongTermRefPic(rcListPic, pReferencePictureSet->getPOC(i), false)->getPOC() >= pocCRA);
733      }
734      else
735      {
736      assert(pReferencePictureSet->getPOC(i) >= pocCRA);
737    }
738  }
739  }
740  if ( getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_W_RADL || getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_N_LP ) // IDR picture found
741  {
742    pocCRA = getPOC();
743    prevRAPisBLA = false;
744  }
745  else if ( getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA ) // CRA picture found
746  {
747    pocCRA = getPOC();
748    prevRAPisBLA = false;
749  }
750  else if ( getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP
751         || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL
752         || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP ) // BLA picture found
753  {
754    pocCRA = getPOC();
755    prevRAPisBLA = true;
756  }
757}
758
759/** Function for marking the reference pictures when an IDR/CRA/CRANT/BLA/BLANT is encountered.
760 * \param pocCRA POC of the CRA/CRANT/BLA/BLANT picture
761 * \param bRefreshPending flag indicating if a deferred decoding refresh is pending
762 * \param rcListPic reference to the reference picture list
763 * This function marks the reference pictures as "unused for reference" in the following conditions.
764 * If the nal_unit_type is IDR/BLA/BLANT, all pictures in the reference picture list 
765 * are marked as "unused for reference"
766 *    If the nal_unit_type is BLA/BLANT, set the pocCRA to the temporal reference of the current picture.
767 * Otherwise
768 *    If the bRefreshPending flag is true (a deferred decoding refresh is pending) and the current
769 *    temporal reference is greater than the temporal reference of the latest CRA/CRANT/BLA/BLANT picture (pocCRA),
770 *    mark all reference pictures except the latest CRA/CRANT/BLA/BLANT picture as "unused for reference" and set
771 *    the bRefreshPending flag to false.
772 *    If the nal_unit_type is CRA/CRANT, set the bRefreshPending flag to true and pocCRA to the temporal
773 *    reference of the current picture.
774 * Note that the current picture is already placed in the reference list and its marking is not changed.
775 * If the current picture has a nal_ref_idc that is not 0, it will remain marked as "used for reference".
776 */
777Void TComSlice::decodingRefreshMarking(Int& pocCRA, Bool& bRefreshPending, TComList<TComPic*>& rcListPic)
778{
779  TComPic*                 rpcPic;
780  Int pocCurr = getPOC(); 
781
782  if ( getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP
783    || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL
784    || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP
785    || getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_W_RADL
786    || getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_N_LP )  // IDR or BLA picture
787  {
788    // mark all pictures as not used for reference
789    TComList<TComPic*>::iterator        iterPic       = rcListPic.begin();
790    while (iterPic != rcListPic.end())
791    {
792      rpcPic = *(iterPic);
793      rpcPic->setCurrSliceIdx(0);
794      if (rpcPic->getPOC() != pocCurr) rpcPic->getSlice(0)->setReferenced(false);
795      iterPic++;
796    }
797    if ( getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP
798      || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL
799      || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP )
800    {
801      pocCRA = pocCurr;
802    }
803  }
804  else // CRA or No DR
805  {
806    if (bRefreshPending==true && pocCurr > pocCRA) // CRA reference marking pending
807    {
808      TComList<TComPic*>::iterator        iterPic       = rcListPic.begin();
809      while (iterPic != rcListPic.end())
810      {
811        rpcPic = *(iterPic);
812        if (rpcPic->getPOC() != pocCurr && rpcPic->getPOC() != pocCRA)
813        {
814          rpcPic->getSlice(0)->setReferenced(false);
815        }
816        iterPic++;
817      }
818      bRefreshPending = false; 
819    }
820    if ( getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA ) // CRA picture found
821    {
822      bRefreshPending = true; 
823      pocCRA = pocCurr;
824    }
825  }
826}
827
828Void TComSlice::copySliceInfo(TComSlice *pSrc)
829{
830  assert( pSrc != NULL );
831
832  Int i, j, k;
833
834  m_iPOC                 = pSrc->m_iPOC;
835  m_eNalUnitType         = pSrc->m_eNalUnitType;
836#if H_MV
837  m_layerId              = pSrc->m_layerId;
838#endif
839  m_eSliceType           = pSrc->m_eSliceType;
840  m_iSliceQp             = pSrc->m_iSliceQp;
841#if ADAPTIVE_QP_SELECTION
842  m_iSliceQpBase         = pSrc->m_iSliceQpBase;
843#endif
844  m_deblockingFilterDisable   = pSrc->m_deblockingFilterDisable;
845  m_deblockingFilterOverrideFlag = pSrc->m_deblockingFilterOverrideFlag;
846  m_deblockingFilterBetaOffsetDiv2 = pSrc->m_deblockingFilterBetaOffsetDiv2;
847  m_deblockingFilterTcOffsetDiv2 = pSrc->m_deblockingFilterTcOffsetDiv2;
848 
849#if L0034_COMBINED_LIST_CLEANUP
850  for (i = 0; i < 2; i++)
851#else
852  for (i = 0; i < 3; i++)
853#endif
854  {
855    m_aiNumRefIdx[i]     = pSrc->m_aiNumRefIdx[i];
856  }
857
858#if L0034_COMBINED_LIST_CLEANUP
859  for (i = 0; i < MAX_NUM_REF; i++)
860  {
861    m_list1IdxToList0Idx[i] = pSrc->m_list1IdxToList0Idx[i];
862  } 
863#else
864  for (i = 0; i < 2; i++)
865  {
866    for (j = 0; j < MAX_NUM_REF_LC; j++)
867    {
868       m_iRefIdxOfLC[i][j]  = pSrc->m_iRefIdxOfLC[i][j];
869    }
870  }
871  for (i = 0; i < MAX_NUM_REF_LC; i++)
872  {
873    m_eListIdFromIdxOfLC[i] = pSrc->m_eListIdFromIdxOfLC[i];
874    m_iRefIdxFromIdxOfLC[i] = pSrc->m_iRefIdxFromIdxOfLC[i];
875    m_iRefIdxOfL1FromRefIdxOfL0[i] = pSrc->m_iRefIdxOfL1FromRefIdxOfL0[i];
876    m_iRefIdxOfL0FromRefIdxOfL1[i] = pSrc->m_iRefIdxOfL0FromRefIdxOfL1[i];
877  }
878  m_bRefPicListModificationFlagLC = pSrc->m_bRefPicListModificationFlagLC;
879  m_bRefPicListCombinationFlag    = pSrc->m_bRefPicListCombinationFlag;
880#endif
881  m_bCheckLDC             = pSrc->m_bCheckLDC;
882  m_iSliceQpDelta        = pSrc->m_iSliceQpDelta;
883  m_iSliceQpDeltaCb      = pSrc->m_iSliceQpDeltaCb;
884  m_iSliceQpDeltaCr      = pSrc->m_iSliceQpDeltaCr;
885  for (i = 0; i < 2; i++)
886  {
887    for (j = 0; j < MAX_NUM_REF; j++)
888    {
889      m_apcRefPicList[i][j]  = pSrc->m_apcRefPicList[i][j];
890      m_aiRefPOCList[i][j]   = pSrc->m_aiRefPOCList[i][j];
891#if H_MV
892      m_aiRefLayerIdList[i][j] = pSrc->m_aiRefLayerIdList[i][j];
893#endif
894    }
895  }
896  for (i = 0; i < 2; i++)
897  {
898    for (j = 0; j < MAX_NUM_REF + 1; j++)
899    {
900      m_bIsUsedAsLongTerm[i][j] = pSrc->m_bIsUsedAsLongTerm[i][j];
901    }
902  }
903  m_iDepth               = pSrc->m_iDepth;
904
905  // referenced slice
906  m_bRefenced            = pSrc->m_bRefenced;
907
908  // access channel
909#if H_MV
910  m_pcVPS                = pSrc->m_pcVPS;
911#endif
912  m_pcSPS                = pSrc->m_pcSPS;
913  m_pcPPS                = pSrc->m_pcPPS;
914  m_pcRPS                = pSrc->m_pcRPS;
915  m_iLastIDR             = pSrc->m_iLastIDR;
916
917  m_pcPic                = pSrc->m_pcPic;
918#if H_3D
919  m_picLists             = pSrc->m_picLists;
920#endif
921  m_colFromL0Flag        = pSrc->m_colFromL0Flag;
922  m_colRefIdx            = pSrc->m_colRefIdx;
923#if SAO_CHROMA_LAMBDA
924  m_dLambdaLuma          = pSrc->m_dLambdaLuma;
925  m_dLambdaChroma        = pSrc->m_dLambdaChroma;
926#else
927  m_dLambda              = pSrc->m_dLambda;
928#endif
929  for (i = 0; i < 2; i++)
930  {
931    for (j = 0; j < MAX_NUM_REF; j++)
932    {
933      for (k =0; k < MAX_NUM_REF; k++)
934      {
935        m_abEqualRef[i][j][k] = pSrc->m_abEqualRef[i][j][k];
936      }
937    }
938  }
939
940#if !L0034_COMBINED_LIST_CLEANUP
941  m_bNoBackPredFlag      = pSrc->m_bNoBackPredFlag;
942#endif
943  m_uiTLayer                      = pSrc->m_uiTLayer;
944  m_bTLayerSwitchingFlag          = pSrc->m_bTLayerSwitchingFlag;
945
946  m_sliceMode                   = pSrc->m_sliceMode;
947  m_sliceArgument               = pSrc->m_sliceArgument;
948  m_sliceCurStartCUAddr         = pSrc->m_sliceCurStartCUAddr;
949  m_sliceCurEndCUAddr           = pSrc->m_sliceCurEndCUAddr;
950  m_sliceIdx                    = pSrc->m_sliceIdx;
951  m_sliceSegmentMode            = pSrc->m_sliceSegmentMode;
952  m_sliceSegmentArgument        = pSrc->m_sliceSegmentArgument; 
953  m_sliceSegmentCurStartCUAddr  = pSrc->m_sliceSegmentCurStartCUAddr;
954  m_sliceSegmentCurEndCUAddr    = pSrc->m_sliceSegmentCurEndCUAddr;
955  m_nextSlice                    = pSrc->m_nextSlice;
956  m_nextSliceSegment             = pSrc->m_nextSliceSegment;
957  for ( Int e=0 ; e<2 ; e++ )
958  {
959    for ( Int n=0 ; n<MAX_NUM_REF ; n++ )
960    {
961      memcpy(m_weightPredTable[e][n], pSrc->m_weightPredTable[e][n], sizeof(wpScalingParam)*3 );
962    }
963  }
964  m_saoEnabledFlag = pSrc->m_saoEnabledFlag; 
965  m_saoEnabledFlagChroma = pSrc->m_saoEnabledFlagChroma;
966  m_cabacInitFlag                = pSrc->m_cabacInitFlag;
967  m_numEntryPointOffsets  = pSrc->m_numEntryPointOffsets;
968
969  m_bLMvdL1Zero = pSrc->m_bLMvdL1Zero;
970  m_LFCrossSliceBoundaryFlag = pSrc->m_LFCrossSliceBoundaryFlag;
971  m_enableTMVPFlag                = pSrc->m_enableTMVPFlag;
972  m_maxNumMergeCand               = pSrc->m_maxNumMergeCand;
973}
974
975Int TComSlice::m_prevPOC = 0;
976
977/** Function for setting the slice's temporal layer ID and corresponding temporal_layer_switching_point_flag.
978 * \param uiTLayer Temporal layer ID of the current slice
979 * The decoder calls this function to set temporal_layer_switching_point_flag for each temporal layer based on
980 * the SPS's temporal_id_nesting_flag and the parsed PPS.  Then, current slice's temporal layer ID and
981 * temporal_layer_switching_point_flag is set accordingly.
982 */
983Void TComSlice::setTLayerInfo( UInt uiTLayer )
984{
985  m_uiTLayer = uiTLayer;
986}
987
988/** Function for checking if this is a switching-point
989*/
990Bool TComSlice::isTemporalLayerSwitchingPoint( TComList<TComPic*>& rcListPic )
991{
992  TComPic* rpcPic;
993  // loop through all pictures in the reference picture buffer
994  TComList<TComPic*>::iterator iterPic = rcListPic.begin();
995  while ( iterPic != rcListPic.end())
996  {
997    rpcPic = *(iterPic++);
998    if(rpcPic->getSlice(0)->isReferenced() && rpcPic->getPOC() != getPOC())
999    {
1000      if(rpcPic->getTLayer() >= getTLayer())
1001      {
1002        return false;
1003      }
1004    }
1005  }
1006  return true;
1007}
1008
1009/** Function for checking if this is a STSA candidate
1010 */
1011Bool TComSlice::isStepwiseTemporalLayerSwitchingPointCandidate( TComList<TComPic*>& rcListPic )
1012{
1013    TComPic* rpcPic;
1014   
1015    TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1016    while ( iterPic != rcListPic.end())
1017    {
1018        rpcPic = *(iterPic++);
1019        if(rpcPic->getSlice(0)->isReferenced() &&  (rpcPic->getUsedByCurr()==true) && rpcPic->getPOC() != getPOC())
1020        {
1021            if(rpcPic->getTLayer() >= getTLayer())
1022            {
1023                return false;
1024            }
1025        }
1026    }
1027    return true;
1028}
1029
1030/** Function for applying picture marking based on the Reference Picture Set in pReferencePictureSet.
1031*/
1032Void TComSlice::applyReferencePictureSet( TComList<TComPic*>& rcListPic, TComReferencePictureSet *pReferencePictureSet)
1033{
1034  TComPic* rpcPic;
1035  Int i, isReference;
1036
1037  // loop through all pictures in the reference picture buffer
1038  TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1039  while ( iterPic != rcListPic.end())
1040  {
1041    rpcPic = *(iterPic++);
1042
1043    if(!rpcPic->getSlice( 0 )->isReferenced())
1044    {
1045      continue;
1046    }
1047
1048    isReference = 0;
1049    // loop through all pictures in the Reference Picture Set
1050    // to see if the picture should be kept as reference picture
1051    for(i=0;i<pReferencePictureSet->getNumberOfPositivePictures()+pReferencePictureSet->getNumberOfNegativePictures();i++)
1052    {
1053      if(!rpcPic->getIsLongTerm() && rpcPic->getPicSym()->getSlice(0)->getPOC() == this->getPOC() + pReferencePictureSet->getDeltaPOC(i))
1054      {
1055        isReference = 1;
1056        rpcPic->setUsedByCurr(pReferencePictureSet->getUsed(i));
1057        rpcPic->setIsLongTerm(0);
1058      }
1059    }
1060    for(;i<pReferencePictureSet->getNumberOfPictures();i++)
1061    {
1062      if(pReferencePictureSet->getCheckLTMSBPresent(i)==true)
1063      {
1064        if(rpcPic->getIsLongTerm() && (rpcPic->getPicSym()->getSlice(0)->getPOC()) == pReferencePictureSet->getPOC(i))
1065        {
1066          isReference = 1;
1067          rpcPic->setUsedByCurr(pReferencePictureSet->getUsed(i));
1068        }
1069      }
1070      else 
1071      {
1072        if(rpcPic->getIsLongTerm() && (rpcPic->getPicSym()->getSlice(0)->getPOC()%(1<<rpcPic->getPicSym()->getSlice(0)->getSPS()->getBitsForPOC())) == pReferencePictureSet->getPOC(i)%(1<<rpcPic->getPicSym()->getSlice(0)->getSPS()->getBitsForPOC()))
1073        {
1074          isReference = 1;
1075          rpcPic->setUsedByCurr(pReferencePictureSet->getUsed(i));
1076        }
1077      }
1078
1079    }
1080    // mark the picture as "unused for reference" if it is not in
1081    // the Reference Picture Set
1082    if(rpcPic->getPicSym()->getSlice(0)->getPOC() != this->getPOC() && isReference == 0)   
1083    {           
1084      rpcPic->getSlice( 0 )->setReferenced( false );   
1085      rpcPic->setUsedByCurr(0);
1086      rpcPic->setIsLongTerm(0);
1087    }
1088    //check that pictures of higher temporal layers are not used
1089    assert(rpcPic->getSlice( 0 )->isReferenced()==0||rpcPic->getUsedByCurr()==0||rpcPic->getTLayer()<=this->getTLayer());
1090    //check that pictures of higher or equal temporal layer are not in the RPS if the current picture is a TSA picture
1091    if(this->getNalUnitType() == NAL_UNIT_CODED_SLICE_TLA_R || this->getNalUnitType() == NAL_UNIT_CODED_SLICE_TSA_N)
1092    {
1093      assert(rpcPic->getSlice( 0 )->isReferenced()==0||rpcPic->getTLayer()<this->getTLayer());
1094    }
1095    //check that pictures marked as temporal layer non-reference pictures are not used for reference
1096    if(rpcPic->getPicSym()->getSlice(0)->getPOC() != this->getPOC() && rpcPic->getTLayer()==this->getTLayer())
1097    {
1098      assert(rpcPic->getSlice( 0 )->isReferenced()==0||rpcPic->getUsedByCurr()==0||rpcPic->getSlice( 0 )->getTemporalLayerNonReferenceFlag()==false);
1099    }
1100  }
1101}
1102
1103/** Function for applying picture marking based on the Reference Picture Set in pReferencePictureSet.
1104*/
1105Int TComSlice::checkThatAllRefPicsAreAvailable( TComList<TComPic*>& rcListPic, TComReferencePictureSet *pReferencePictureSet, Bool printErrors, Int pocRandomAccess)
1106{
1107  TComPic* rpcPic;
1108  Int i, isAvailable;
1109  Int atLeastOneLost = 0;
1110  Int atLeastOneRemoved = 0;
1111  Int iPocLost = 0;
1112
1113  // loop through all long-term pictures in the Reference Picture Set
1114  // to see if the picture should be kept as reference picture
1115  for(i=pReferencePictureSet->getNumberOfNegativePictures()+pReferencePictureSet->getNumberOfPositivePictures();i<pReferencePictureSet->getNumberOfPictures();i++)
1116  {
1117    isAvailable = 0;
1118    // loop through all pictures in the reference picture buffer
1119    TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1120    while ( iterPic != rcListPic.end())
1121    {
1122      rpcPic = *(iterPic++);
1123      if(pReferencePictureSet->getCheckLTMSBPresent(i)==true)
1124      {
1125        if(rpcPic->getIsLongTerm() && (rpcPic->getPicSym()->getSlice(0)->getPOC()) == pReferencePictureSet->getPOC(i) && rpcPic->getSlice(0)->isReferenced())
1126        {
1127          isAvailable = 1;
1128        }
1129      }
1130      else 
1131      {
1132        if(rpcPic->getIsLongTerm() && (rpcPic->getPicSym()->getSlice(0)->getPOC()%(1<<rpcPic->getPicSym()->getSlice(0)->getSPS()->getBitsForPOC())) == pReferencePictureSet->getPOC(i)%(1<<rpcPic->getPicSym()->getSlice(0)->getSPS()->getBitsForPOC()) && rpcPic->getSlice(0)->isReferenced())
1133        {
1134          isAvailable = 1;
1135        }
1136      }
1137    }
1138    // if there was no such long-term check the short terms
1139    if(!isAvailable)
1140    {
1141      iterPic = rcListPic.begin();
1142      while ( iterPic != rcListPic.end())
1143      {
1144        rpcPic = *(iterPic++);
1145
1146        Int pocCycle = 1 << rpcPic->getPicSym()->getSlice(0)->getSPS()->getBitsForPOC();
1147        Int curPoc = rpcPic->getPicSym()->getSlice(0)->getPOC();
1148        Int refPoc = pReferencePictureSet->getPOC(i);
1149        if (!pReferencePictureSet->getCheckLTMSBPresent(i))
1150        {
1151          curPoc = curPoc % pocCycle;
1152          refPoc = refPoc % pocCycle;
1153        }
1154       
1155        if (rpcPic->getSlice(0)->isReferenced() && curPoc == refPoc)
1156        {
1157          isAvailable = 1;
1158          rpcPic->setIsLongTerm(1);
1159          break;
1160        }
1161      }
1162    }
1163    // report that a picture is lost if it is in the Reference Picture Set
1164    // but not available as reference picture
1165    if(isAvailable == 0)   
1166    {           
1167      if (this->getPOC() + pReferencePictureSet->getDeltaPOC(i) >= pocRandomAccess)
1168      {
1169        if(!pReferencePictureSet->getUsed(i) )
1170        {
1171          if(printErrors)
1172          {
1173            printf("\nLong-term reference picture with POC = %3d seems to have been removed or not correctly decoded.", this->getPOC() + pReferencePictureSet->getDeltaPOC(i));
1174          }
1175          atLeastOneRemoved = 1;
1176        }
1177        else
1178        {
1179          if(printErrors)
1180          {
1181            printf("\nLong-term reference picture with POC = %3d is lost or not correctly decoded!", this->getPOC() + pReferencePictureSet->getDeltaPOC(i));
1182          }
1183          atLeastOneLost = 1;
1184          iPocLost=this->getPOC() + pReferencePictureSet->getDeltaPOC(i);
1185        }
1186      }
1187    }
1188  } 
1189  // loop through all short-term pictures in the Reference Picture Set
1190  // to see if the picture should be kept as reference picture
1191  for(i=0;i<pReferencePictureSet->getNumberOfNegativePictures()+pReferencePictureSet->getNumberOfPositivePictures();i++)
1192  {
1193    isAvailable = 0;
1194    // loop through all pictures in the reference picture buffer
1195    TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1196    while ( iterPic != rcListPic.end())
1197    {
1198      rpcPic = *(iterPic++);
1199
1200      if(!rpcPic->getIsLongTerm() && rpcPic->getPicSym()->getSlice(0)->getPOC() == this->getPOC() + pReferencePictureSet->getDeltaPOC(i) && rpcPic->getSlice(0)->isReferenced())
1201      {
1202        isAvailable = 1;
1203      }
1204    }
1205    // report that a picture is lost if it is in the Reference Picture Set
1206    // but not available as reference picture
1207    if(isAvailable == 0)   
1208    {           
1209      if (this->getPOC() + pReferencePictureSet->getDeltaPOC(i) >= pocRandomAccess)
1210      {
1211        if(!pReferencePictureSet->getUsed(i) )
1212        {
1213          if(printErrors)
1214          {
1215            printf("\nShort-term reference picture with POC = %3d seems to have been removed or not correctly decoded.", this->getPOC() + pReferencePictureSet->getDeltaPOC(i));
1216          }
1217          atLeastOneRemoved = 1;
1218        }
1219        else
1220        {
1221          if(printErrors)
1222          {
1223            printf("\nShort-term reference picture with POC = %3d is lost or not correctly decoded!", this->getPOC() + pReferencePictureSet->getDeltaPOC(i));
1224          }
1225          atLeastOneLost = 1;
1226          iPocLost=this->getPOC() + pReferencePictureSet->getDeltaPOC(i);
1227        }
1228      }
1229    }
1230  }   
1231  if(atLeastOneLost)
1232  {
1233    return iPocLost+1;
1234  }
1235  if(atLeastOneRemoved)
1236  {
1237    return -2;
1238  }
1239  else
1240  {
1241    return 0;
1242  }
1243}
1244
1245/** Function for constructing an explicit Reference Picture Set out of the available pictures in a referenced Reference Picture Set
1246*/
1247Void TComSlice::createExplicitReferencePictureSetFromReference( TComList<TComPic*>& rcListPic, TComReferencePictureSet *pReferencePictureSet)
1248{
1249  TComPic* rpcPic;
1250  Int i, j;
1251  Int k = 0;
1252  Int nrOfNegativePictures = 0;
1253  Int nrOfPositivePictures = 0;
1254  TComReferencePictureSet* pcRPS = this->getLocalRPS();
1255
1256  // loop through all pictures in the Reference Picture Set
1257  for(i=0;i<pReferencePictureSet->getNumberOfPictures();i++)
1258  {
1259    j = 0;
1260    // loop through all pictures in the reference picture buffer
1261    TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1262    while ( iterPic != rcListPic.end())
1263    {
1264      j++;
1265      rpcPic = *(iterPic++);
1266
1267      if(rpcPic->getPicSym()->getSlice(0)->getPOC() == this->getPOC() + pReferencePictureSet->getDeltaPOC(i) && rpcPic->getSlice(0)->isReferenced())
1268      {
1269        // This picture exists as a reference picture
1270        // and should be added to the explicit Reference Picture Set
1271        pcRPS->setDeltaPOC(k, pReferencePictureSet->getDeltaPOC(i));
1272        pcRPS->setUsed(k, pReferencePictureSet->getUsed(i));
1273        if(pcRPS->getDeltaPOC(k) < 0)
1274        {
1275          nrOfNegativePictures++;
1276        }
1277        else
1278        {
1279          nrOfPositivePictures++;
1280        }
1281        k++;
1282      }
1283    }
1284  }
1285  pcRPS->setNumberOfNegativePictures(nrOfNegativePictures);
1286  pcRPS->setNumberOfPositivePictures(nrOfPositivePictures);
1287  pcRPS->setNumberOfPictures(nrOfNegativePictures+nrOfPositivePictures);
1288  // This is a simplistic inter rps example. A smarter encoder will look for a better reference RPS to do the
1289  // inter RPS prediction with.  Here we just use the reference used by pReferencePictureSet.
1290  // If pReferencePictureSet is not inter_RPS_predicted, then inter_RPS_prediction is for the current RPS also disabled.
1291  if (!pReferencePictureSet->getInterRPSPrediction())
1292  {
1293    pcRPS->setInterRPSPrediction(false);
1294    pcRPS->setNumRefIdc(0);
1295  }
1296  else
1297  {
1298    Int rIdx =  this->getRPSidx() - pReferencePictureSet->getDeltaRIdxMinus1() - 1;
1299    Int deltaRPS = pReferencePictureSet->getDeltaRPS();
1300    TComReferencePictureSet* pcRefRPS = this->getSPS()->getRPSList()->getReferencePictureSet(rIdx);
1301    Int iRefPics = pcRefRPS->getNumberOfPictures();
1302    Int iNewIdc=0;
1303    for(i=0; i<= iRefPics; i++) 
1304    {
1305      Int deltaPOC = ((i != iRefPics)? pcRefRPS->getDeltaPOC(i) : 0);  // check if the reference abs POC is >= 0
1306      Int iRefIdc = 0;
1307      for (j=0; j < pcRPS->getNumberOfPictures(); j++) // loop through the  pictures in the new RPS
1308      {
1309        if ( (deltaPOC + deltaRPS) == pcRPS->getDeltaPOC(j))
1310        {
1311          if (pcRPS->getUsed(j))
1312          {
1313            iRefIdc = 1;
1314          }
1315          else
1316          {
1317            iRefIdc = 2;
1318          }
1319        }
1320      }
1321      pcRPS->setRefIdc(i, iRefIdc);
1322      iNewIdc++;
1323    }
1324    pcRPS->setInterRPSPrediction(true);
1325    pcRPS->setNumRefIdc(iNewIdc);
1326    pcRPS->setDeltaRPS(deltaRPS); 
1327    pcRPS->setDeltaRIdxMinus1(pReferencePictureSet->getDeltaRIdxMinus1() + this->getSPS()->getRPSList()->getNumberOfReferencePictureSets() - this->getRPSidx());
1328  }
1329
1330  this->setRPS(pcRPS);
1331  this->setRPSidx(-1);
1332}
1333
1334/** get AC and DC values for weighted pred
1335 * \param *wp
1336 * \returns Void
1337 */
1338Void  TComSlice::getWpAcDcParam(wpACDCParam *&wp)
1339{
1340  wp = m_weightACDCParam;
1341}
1342
1343/** init AC and DC values for weighted pred
1344 * \returns Void
1345 */
1346Void  TComSlice::initWpAcDcParam()
1347{
1348  for(Int iComp = 0; iComp < 3; iComp++ )
1349  {
1350    m_weightACDCParam[iComp].iAC = 0;
1351    m_weightACDCParam[iComp].iDC = 0;
1352  }
1353}
1354
1355/** get WP tables for weighted pred
1356 * \param RefPicList
1357 * \param iRefIdx
1358 * \param *&wpScalingParam
1359 * \returns Void
1360 */
1361Void  TComSlice::getWpScaling( RefPicList e, Int iRefIdx, wpScalingParam *&wp )
1362{
1363  wp = m_weightPredTable[e][iRefIdx];
1364}
1365
1366/** reset Default WP tables settings : no weight.
1367 * \param wpScalingParam
1368 * \returns Void
1369 */
1370Void  TComSlice::resetWpScaling()
1371{
1372  for ( Int e=0 ; e<2 ; e++ )
1373  {
1374    for ( Int i=0 ; i<MAX_NUM_REF ; i++ )
1375    {
1376      for ( Int yuv=0 ; yuv<3 ; yuv++ )
1377      {
1378        wpScalingParam  *pwp = &(m_weightPredTable[e][i][yuv]);
1379        pwp->bPresentFlag      = false;
1380        pwp->uiLog2WeightDenom = 0;
1381        pwp->uiLog2WeightDenom = 0;
1382        pwp->iWeight           = 1;
1383        pwp->iOffset           = 0;
1384      }
1385    }
1386  }
1387}
1388
1389/** init WP table
1390 * \returns Void
1391 */
1392Void  TComSlice::initWpScaling()
1393{
1394  for ( Int e=0 ; e<2 ; e++ )
1395  {
1396    for ( Int i=0 ; i<MAX_NUM_REF ; i++ )
1397    {
1398      for ( Int yuv=0 ; yuv<3 ; yuv++ )
1399      {
1400        wpScalingParam  *pwp = &(m_weightPredTable[e][i][yuv]);
1401        if ( !pwp->bPresentFlag ) 
1402        {
1403          // Inferring values not present :
1404          pwp->iWeight = (1 << pwp->uiLog2WeightDenom);
1405          pwp->iOffset = 0;
1406        }
1407
1408        pwp->w      = pwp->iWeight;
1409        Int bitDepth = yuv ? g_bitDepthC : g_bitDepthY;
1410        pwp->o      = pwp->iOffset << (bitDepth-8);
1411        pwp->shift  = pwp->uiLog2WeightDenom;
1412        pwp->round  = (pwp->uiLog2WeightDenom>=1) ? (1 << (pwp->uiLog2WeightDenom-1)) : (0);
1413      }
1414    }
1415  }
1416}
1417
1418// ------------------------------------------------------------------------------------------------
1419// Video parameter set (VPS)
1420// ------------------------------------------------------------------------------------------------
1421TComVPS::TComVPS()
1422: m_VPSId                     (  0)
1423, m_uiMaxTLayers              (  1)
1424, m_uiMaxLayers               (  1)
1425, m_bTemporalIdNestingFlag    (false)
1426, m_numHrdParameters          (  0)
1427#if H_MV
1428, m_maxNuhLayerId             (  0)
1429#else
1430, m_maxNuhReservedZeroLayerId (  0)
1431#endif
1432, m_hrdParameters             (NULL)
1433, m_hrdOpSetIdx               (NULL)
1434, m_cprmsPresentFlag          (NULL)
1435{
1436  for( Int i = 0; i < MAX_TLAYER; i++)
1437  {
1438    m_numReorderPics[i] = 0;
1439#if L0323_DPB
1440    m_uiMaxDecPicBuffering[i] = 1; 
1441#else
1442    m_uiMaxDecPicBuffering[i] = 0; 
1443#endif
1444    m_uiMaxLatencyIncrease[i] = 0;
1445  }
1446#if H_MV
1447  m_avcBaseLayerFlag = false;
1448  m_splittingFlag    = false;
1449 
1450  for( Int i = 0; i < MAX_NUM_SCALABILITY_TYPES; i++ )
1451  {
1452    m_scalabilityMask[i] = false;
1453    m_dimensionIdLen [i]  = -1; 
1454  }
1455
1456  m_vpsNuhLayerIdPresentFlag = false;
1457  m_numOutputLayerSets       = 0;
1458
1459  for( Int i = 0; i < MAX_VPS_OP_SETS_PLUS1; i++ )
1460  {
1461    m_vpsProfilePresentFlag   [i] = false;
1462    m_profileLayerSetRefMinus1[i] = 0;
1463    m_outputLayerSetIdx       [i] = 0;
1464    for( Int j = 0; j < MAX_VPS_NUH_LAYER_ID_PLUS1; j++ )
1465    {
1466      m_outputLayerFlag[i][j] = false;
1467    }
1468  }
1469
1470  for( Int i = 0; i < MAX_NUM_LAYER_IDS; i++ )
1471  {
1472    m_layerIdInVps[i] =  (i == 0 ) ? 0 : -1;         
1473  }
1474
1475  for( Int i = 0; i < MAX_NUM_LAYERS; i++ )
1476  {
1477    m_layerIdInNuh      [i] = ( i == 0 ) ? 0 : -1; 
1478    m_numDirectRefLayers[i] = 0; 
1479#if H_3D
1480    m_viewIndex         [i] = -1; 
1481    m_vpsDepthModesFlag [i] = false;
1482#endif
1483
1484    for( Int j = 0; j < MAX_NUM_LAYERS; j++ )
1485    {
1486      m_directDependencyFlag[i][j] = false;
1487      m_refLayerId[i][j]           = -1; 
1488    }
1489
1490    for( Int j = 0; j < MAX_NUM_SCALABILITY_TYPES; j++ )
1491    {
1492      m_dimensionId[i][j] = 0;
1493    }
1494  }
1495#endif
1496}
1497
1498TComVPS::~TComVPS()
1499{
1500if( m_hrdParameters    != NULL )     delete[] m_hrdParameters;
1501  if( m_hrdOpSetIdx      != NULL )     delete[] m_hrdOpSetIdx;
1502  if( m_cprmsPresentFlag != NULL )     delete[] m_cprmsPresentFlag;
1503}
1504
1505#if H_MV
1506
1507Bool TComVPS::checkVPSExtensionSyntax()
1508{
1509  // check splitting flag constraint
1510  if ( getSplittingFlag() )
1511  {
1512    // Derive dimBitOffset[j]
1513    Int dimBitOffset[MAX_NUM_SCALABILITY_TYPES+1];
1514    Int numScalabilityTypes = getNumScalabilityTypes();
1515    dimBitOffset[0] = 0;
1516
1517    for (Int type = 1; type <= numScalabilityTypes; type++ )
1518    {
1519      dimBitOffset[ type ] = 0;
1520      for (Int dimIdx = 0; dimIdx <= type - 1; dimIdx++)
1521        dimBitOffset[ type ] += ( getDimensionIdLen( dimIdx ) );
1522    }
1523
1524    for (Int type = 0; type < getNumScalabilityTypes(); type++ )
1525    {
1526      for( Int layer = 1; layer < getMaxLayers(); layer++ )
1527      {
1528        assert( getDimensionId( layer, type ) == ( ( getLayerIdInNuh( layer ) & ( (1 << dimBitOffset[ type + 1 ] ) - 1) ) >> dimBitOffset[ type ] ) );
1529      }; 
1530  };
1531  }
1532
1533  for( Int layer = 1; layer < getMaxLayers(); layer++ )
1534  {
1535    // check layer_id_in_nuh constraint
1536    assert( getLayerIdInNuh( layer ) > getLayerIdInNuh( layer -1 ) );
1537  }
1538  return true; 
1539}
1540
1541Int TComVPS::getNumScalabilityTypes()
1542{
1543  return scalTypeToScalIdx( ScalabilityType(MAX_NUM_SCALABILITY_TYPES) );
1544}
1545
1546Int TComVPS::scalTypeToScalIdx( ScalabilityType scalType )
1547{
1548  assert( scalType >= 0 && scalType <= MAX_NUM_SCALABILITY_TYPES ); 
1549  assert( scalType == MAX_NUM_SCALABILITY_TYPES || getScalabilityMask( scalType ) );
1550
1551  Int scalIdx = 0; 
1552  for( Int curScalType = 0; curScalType < scalType; curScalType++ )
1553  {
1554    scalIdx += ( getScalabilityMask( curScalType ) ? 1 : 0 );
1555  }
1556
1557  return scalIdx; 
1558}
1559
1560
1561
1562Void TComVPS::setScalabilityMask( UInt val )
1563{
1564  for ( Int scalType = 0; scalType < MAX_NUM_SCALABILITY_TYPES; scalType++ ) 
1565    setScalabilityMask( scalType, ( val & (1 << scalType ) ) != 0 );
1566}
1567
1568Void TComVPS::calcIvRefLayers()
1569{
1570  for( Int i = 1; i <= getMaxLayers(); i++ )
1571  { 
1572    m_numDirectRefLayers[ i ] = 0; 
1573    for( Int j = 0 ; j < i; j++ )
1574      if( m_directDependencyFlag[ i ][ j ])
1575        m_refLayerId[ i ][ m_numDirectRefLayers[ i ]++ ] = m_layerIdInNuh[ j ];   
1576  }
1577}
1578
1579Int TComVPS::getRefLayerId( Int layerIdInVps, Int idx )
1580{
1581  assert( idx >= 0 && idx < m_numDirectRefLayers[layerIdInVps] );     
1582  Int layerIdInNuh = m_refLayerId[ layerIdInVps ][ idx ];   
1583  assert ( layerIdInNuh >= 0 ); 
1584  return layerIdInNuh;
1585}
1586
1587Int TComVPS::getScalabilityId( Int layerIdInVps, ScalabilityType scalType )
1588{
1589  return getScalabilityMask( scalType ) ? getDimensionId( layerIdInVps, scalTypeToScalIdx( scalType ) ) : 0;
1590}
1591
1592#if H_3D
1593Void TComVPS::initViewIndex()
1594{
1595  Int viewIdList   [ MAX_NUM_LAYERS ]; // ed. should be changed to MAX_VIEW_ID
1596  Int viewIndexList[ MAX_NUM_LAYERS ]; 
1597  Int numViewIds = 0; 
1598
1599  for ( Int i = 0 ; i  <  m_uiMaxLayers; i++ )
1600  {     
1601    Int currViewId = getViewId( i ); 
1602
1603    Bool viewIdInListFlag = false; 
1604    for ( Int j = 0; j < numViewIds; j ++ )
1605    {
1606      viewIdInListFlag  = viewIdInListFlag || ( currViewId  == viewIdList[ j ]  );
1607    }
1608
1609    if ( !viewIdInListFlag ) 
1610    {
1611      viewIdList   [ numViewIds ] = currViewId;
1612      viewIndexList[ currViewId ] = numViewIds;
1613
1614      numViewIds++;
1615    } 
1616
1617    m_viewIndex[ i ] = viewIndexList[ currViewId ]; 
1618  }
1619}
1620
1621Int TComVPS::getLayerIdInNuh( Int viewIndex, Bool depthFlag )
1622{
1623  Int foundlayerId = -1; 
1624
1625  for (Int layer = 0 ; layer < m_uiMaxLayers; layer++ )
1626  {
1627    if( ( getViewIndex( layer ) == viewIndex ) && ( getDepthId( layer ) == ( depthFlag ? 1 : 0 ) )  )
1628    {
1629      foundlayerId = layer; 
1630      break; 
1631    }
1632  }
1633  assert( foundlayerId != -1 ); 
1634
1635  return getLayerIdInNuh( foundlayerId );
1636}
1637#endif // H_3D
1638#endif // H_MV
1639
1640// ------------------------------------------------------------------------------------------------
1641// Sequence parameter set (SPS)
1642// ------------------------------------------------------------------------------------------------
1643
1644TComSPS::TComSPS()
1645: m_SPSId                     (  0)
1646, m_VPSId                     (  0)
1647, m_chromaFormatIdc           (CHROMA_420)
1648, m_uiMaxTLayers              (  1)
1649// Structure
1650, m_picWidthInLumaSamples     (352)
1651, m_picHeightInLumaSamples    (288)
1652, m_log2MinCodingBlockSize    (  0)
1653, m_log2DiffMaxMinCodingBlockSize (0)
1654, m_uiMaxCUWidth              ( 32)
1655, m_uiMaxCUHeight             ( 32)
1656, m_uiMaxCUDepth              (  3)
1657, m_bLongTermRefsPresent      (false)
1658, m_uiQuadtreeTULog2MaxSize   (  0)
1659, m_uiQuadtreeTULog2MinSize   (  0)
1660, m_uiQuadtreeTUMaxDepthInter (  0)
1661, m_uiQuadtreeTUMaxDepthIntra (  0)
1662// Tool list
1663, m_usePCM                   (false)
1664, m_pcmLog2MaxSize            (  5)
1665, m_uiPCMLog2MinSize          (  7)
1666#if !L0034_COMBINED_LIST_CLEANUP
1667, m_bUseLComb                 (false)
1668#endif
1669, m_bitDepthY                 (  8)
1670, m_bitDepthC                 (  8)
1671, m_qpBDOffsetY               (  0)
1672, m_qpBDOffsetC               (  0)
1673, m_useLossless               (false)
1674, m_uiPCMBitDepthLuma         (  8)
1675, m_uiPCMBitDepthChroma       (  8)
1676, m_bPCMFilterDisableFlag     (false)
1677, m_uiBitsForPOC              (  8)
1678, m_numLongTermRefPicSPS    (  0) 
1679, m_uiMaxTrSize               ( 32)
1680, m_bUseSAO                   (false) 
1681, m_bTemporalIdNestingFlag    (false)
1682, m_scalingListEnabledFlag    (false)
1683, m_useStrongIntraSmoothing   (false)
1684, m_vuiParametersPresentFlag  (false)
1685, m_vuiParameters             ()
1686#if H_MV
1687, m_interViewMvVertConstraintFlag (false)
1688#endif
1689#if H_3D
1690, m_bCamParInSliceHeader      (false)
1691#endif
1692{
1693  for ( Int i = 0; i < MAX_TLAYER; i++ )
1694  {
1695    m_uiMaxLatencyIncrease[i] = 0;
1696#if L0323_DPB
1697    m_uiMaxDecPicBuffering[i] = 1;
1698#else
1699    m_uiMaxDecPicBuffering[i] = 0;
1700#endif
1701    m_numReorderPics[i]       = 0;
1702  }
1703  m_scalingList = new TComScalingList;
1704  ::memset(m_ltRefPicPocLsbSps, 0, sizeof(m_ltRefPicPocLsbSps));
1705  ::memset(m_usedByCurrPicLtSPSFlag, 0, sizeof(m_usedByCurrPicLtSPSFlag));
1706}
1707
1708TComSPS::~TComSPS()
1709{
1710  delete m_scalingList;
1711  m_RPSList.destroy();
1712}
1713
1714Void  TComSPS::createRPSList( Int numRPS )
1715{ 
1716  m_RPSList.destroy();
1717  m_RPSList.create(numRPS);
1718}
1719
1720Void TComSPS::setHrdParameters( UInt frameRate, UInt numDU, UInt bitRate, Bool randomAccess )
1721{
1722  if( !getVuiParametersPresentFlag() )
1723  {
1724    return;
1725  }
1726
1727  TComVUI *vui = getVuiParameters();
1728  TComHRD *hrd = vui->getHrdParameters();
1729
1730#if L0043_TIMING_INFO
1731  TimingInfo *timingInfo = vui->getTimingInfo();
1732  timingInfo->setTimingInfoPresentFlag( true );
1733  switch( frameRate )
1734  {
1735  case 24:
1736    timingInfo->setNumUnitsInTick( 1125000 );    timingInfo->setTimeScale    ( 27000000 );
1737    break;
1738  case 25:
1739    timingInfo->setNumUnitsInTick( 1080000 );    timingInfo->setTimeScale    ( 27000000 );
1740    break;
1741  case 30:
1742    timingInfo->setNumUnitsInTick( 900900 );     timingInfo->setTimeScale    ( 27000000 );
1743    break;
1744  case 50:
1745    timingInfo->setNumUnitsInTick( 540000 );     timingInfo->setTimeScale    ( 27000000 );
1746    break;
1747  case 60:
1748    timingInfo->setNumUnitsInTick( 450450 );     timingInfo->setTimeScale    ( 27000000 );
1749    break;
1750  default:
1751    timingInfo->setNumUnitsInTick( 1001 );       timingInfo->setTimeScale    ( 60000 );
1752    break;
1753  }
1754#else
1755  hrd->setTimingInfoPresentFlag( true );
1756  switch( frameRate )
1757  {
1758  case 24:
1759    hrd->setNumUnitsInTick( 1125000 );    hrd->setTimeScale    ( 27000000 );
1760    break;
1761  case 25:
1762    hrd->setNumUnitsInTick( 1080000 );    hrd->setTimeScale    ( 27000000 );
1763    break;
1764  case 30:
1765    hrd->setNumUnitsInTick( 900900 );     hrd->setTimeScale    ( 27000000 );
1766    break;
1767  case 50:
1768    hrd->setNumUnitsInTick( 540000 );     hrd->setTimeScale    ( 27000000 );
1769    break;
1770  case 60:
1771    hrd->setNumUnitsInTick( 450450 );     hrd->setTimeScale    ( 27000000 );
1772    break;
1773  default:
1774    hrd->setNumUnitsInTick( 1001 );       hrd->setTimeScale    ( 60000 );
1775    break;
1776  }
1777#endif
1778
1779  Bool rateCnt = ( bitRate > 0 );
1780  hrd->setNalHrdParametersPresentFlag( rateCnt );
1781  hrd->setVclHrdParametersPresentFlag( rateCnt );
1782
1783  hrd->setSubPicCpbParamsPresentFlag( ( numDU > 1 ) );
1784
1785  if( hrd->getSubPicCpbParamsPresentFlag() )
1786  {
1787    hrd->setTickDivisorMinus2( 100 - 2 );                          //
1788    hrd->setDuCpbRemovalDelayLengthMinus1( 7 );                    // 8-bit precision ( plus 1 for last DU in AU )
1789    hrd->setSubPicCpbParamsInPicTimingSEIFlag( true );
1790#if L0044_DU_DPB_OUTPUT_DELAY_HRD
1791    hrd->setDpbOutputDelayDuLengthMinus1( 5 + 7 );                 // With sub-clock tick factor of 100, at least 7 bits to have the same value as AU dpb delay
1792#endif
1793  }
1794  else
1795  {
1796    hrd->setSubPicCpbParamsInPicTimingSEIFlag( false ); 
1797  }
1798
1799  hrd->setBitRateScale( 4 );                                       // in units of 2~( 6 + 4 ) = 1,024 bps
1800  hrd->setCpbSizeScale( 6 );                                       // in units of 2~( 4 + 4 ) = 1,024 bit
1801  hrd->setDuCpbSizeScale( 6 );                                       // in units of 2~( 4 + 4 ) = 1,024 bit
1802 
1803  hrd->setInitialCpbRemovalDelayLengthMinus1(15);                  // assuming 0.5 sec, log2( 90,000 * 0.5 ) = 16-bit
1804  if( randomAccess )
1805  {
1806    hrd->setCpbRemovalDelayLengthMinus1(5);                        // 32 = 2^5 (plus 1)
1807    hrd->setDpbOutputDelayLengthMinus1 (5);                        // 32 + 3 = 2^6
1808  }
1809  else
1810  {
1811    hrd->setCpbRemovalDelayLengthMinus1(9);                        // max. 2^10
1812    hrd->setDpbOutputDelayLengthMinus1 (9);                        // max. 2^10
1813  }
1814
1815/*
1816   Note: only the case of "vps_max_temporal_layers_minus1 = 0" is supported.
1817*/
1818  Int i, j;
1819  UInt birateValue, cpbSizeValue;
1820  UInt ducpbSizeValue;
1821#if L0363_DU_BIT_RATE
1822  UInt duBitRateValue = 0;
1823#endif
1824
1825  for( i = 0; i < MAX_TLAYER; i ++ )
1826  {
1827    hrd->setFixedPicRateFlag( i, 1 );
1828    hrd->setPicDurationInTcMinus1( i, 0 );
1829    hrd->setLowDelayHrdFlag( i, 0 );
1830    hrd->setCpbCntMinus1( i, 0 );
1831
1832    birateValue  = bitRate;
1833    cpbSizeValue = bitRate;                                     // 1 second
1834    ducpbSizeValue = bitRate/numDU;
1835#if L0363_DU_BIT_RATE
1836    duBitRateValue = bitRate;
1837#endif
1838    for( j = 0; j < ( hrd->getCpbCntMinus1( i ) + 1 ); j ++ )
1839    {
1840      hrd->setBitRateValueMinus1( i, j, 0, ( birateValue  - 1 ) );
1841      hrd->setCpbSizeValueMinus1( i, j, 0, ( cpbSizeValue - 1 ) );
1842      hrd->setDuCpbSizeValueMinus1( i, j, 0, ( ducpbSizeValue - 1 ) );
1843      hrd->setCbrFlag( i, j, 0, ( j == 0 ) );
1844
1845      hrd->setBitRateValueMinus1( i, j, 1, ( birateValue  - 1) );
1846      hrd->setCpbSizeValueMinus1( i, j, 1, ( cpbSizeValue - 1 ) );
1847      hrd->setDuCpbSizeValueMinus1( i, j, 1, ( ducpbSizeValue - 1 ) );
1848#if L0363_DU_BIT_RATE
1849      hrd->setDuBitRateValueMinus1( i, j, 1, ( duBitRateValue - 1 ) );
1850#endif
1851      hrd->setCbrFlag( i, j, 1, ( j == 0 ) );
1852    }
1853  }
1854}
1855const Int TComSPS::m_winUnitX[]={1,2,2,1};
1856const Int TComSPS::m_winUnitY[]={1,2,1,1};
1857
1858TComPPS::TComPPS()
1859: m_PPSId                       (0)
1860, m_SPSId                       (0)
1861, m_picInitQPMinus26            (0)
1862, m_useDQP                      (false)
1863, m_bConstrainedIntraPred       (false)
1864, m_bSliceChromaQpFlag          (false)
1865, m_pcSPS                       (NULL)
1866, m_uiMaxCuDQPDepth             (0)
1867, m_uiMinCuDQPSize              (0)
1868, m_chromaCbQpOffset            (0)
1869, m_chromaCrQpOffset            (0)
1870, m_numRefIdxL0DefaultActive    (1)
1871, m_numRefIdxL1DefaultActive    (1)
1872, m_TransquantBypassEnableFlag  (false)
1873, m_useTransformSkip             (false)
1874, m_dependentSliceSegmentsEnabledFlag    (false)
1875, m_tilesEnabledFlag               (false)
1876, m_entropyCodingSyncEnabledFlag   (false)
1877, m_loopFilterAcrossTilesEnabledFlag  (true)
1878, m_uniformSpacingFlag           (0)
1879, m_iNumColumnsMinus1            (0)
1880, m_puiColumnWidth               (NULL)
1881, m_iNumRowsMinus1               (0)
1882, m_puiRowHeight                 (NULL)
1883, m_iNumSubstreams             (1)
1884, m_signHideFlag(0)
1885, m_cabacInitPresentFlag        (false)
1886, m_encCABACTableIdx            (I_SLICE)
1887, m_sliceHeaderExtensionPresentFlag    (false)
1888, m_loopFilterAcrossSlicesEnabledFlag (false)
1889, m_listsModificationPresentFlag(  0)
1890, m_numExtraSliceHeaderBits(0)
1891{
1892  m_scalingList = new TComScalingList;
1893}
1894
1895TComPPS::~TComPPS()
1896{
1897  if( m_iNumColumnsMinus1 > 0 && m_uniformSpacingFlag == 0 )
1898  {
1899    if (m_puiColumnWidth) delete [] m_puiColumnWidth; 
1900    m_puiColumnWidth = NULL;
1901  }
1902  if( m_iNumRowsMinus1 > 0 && m_uniformSpacingFlag == 0 )
1903  {
1904    if (m_puiRowHeight) delete [] m_puiRowHeight;
1905    m_puiRowHeight = NULL;
1906  }
1907  delete m_scalingList;
1908}
1909
1910#if H_3D
1911Void
1912TComSPS::initCamParaSPS( UInt uiViewIndex, UInt uiCamParPrecision, Bool bCamParSlice, Int** aaiScale, Int** aaiOffset )
1913{
1914  AOT( uiViewIndex != 0 && !bCamParSlice && ( aaiScale == 0 || aaiOffset == 0 ) ); 
1915 
1916  m_uiCamParPrecision     = ( uiViewIndex ? uiCamParPrecision : 0 );
1917  m_bCamParInSliceHeader  = ( uiViewIndex ? bCamParSlice  : false );
1918  ::memset( m_aaiCodedScale,  0x00, sizeof( m_aaiCodedScale  ) );
1919  ::memset( m_aaiCodedOffset, 0x00, sizeof( m_aaiCodedOffset ) );
1920
1921  if( !m_bCamParInSliceHeader )
1922  {
1923    for( UInt uiBaseViewIndex = 0; uiBaseViewIndex < uiViewIndex; uiBaseViewIndex++ )
1924    {
1925      m_aaiCodedScale [ 0 ][ uiBaseViewIndex ] = aaiScale [ uiBaseViewIndex ][     uiViewIndex ];
1926      m_aaiCodedScale [ 1 ][ uiBaseViewIndex ] = aaiScale [     uiViewIndex ][ uiBaseViewIndex ];
1927      m_aaiCodedOffset[ 0 ][ uiBaseViewIndex ] = aaiOffset[ uiBaseViewIndex ][     uiViewIndex ];
1928      m_aaiCodedOffset[ 1 ][ uiBaseViewIndex ] = aaiOffset[     uiViewIndex ][ uiBaseViewIndex ];
1929    }
1930  }
1931}
1932#endif
1933
1934TComReferencePictureSet::TComReferencePictureSet()
1935: m_numberOfPictures (0)
1936, m_numberOfNegativePictures (0)
1937, m_numberOfPositivePictures (0)
1938, m_numberOfLongtermPictures (0)
1939, m_interRPSPrediction (0) 
1940, m_deltaRIdxMinus1 (0)   
1941, m_deltaRPS (0) 
1942, m_numRefIdc (0) 
1943{
1944  ::memset( m_deltaPOC, 0, sizeof(m_deltaPOC) );
1945  ::memset( m_POC, 0, sizeof(m_POC) );
1946  ::memset( m_used, 0, sizeof(m_used) );
1947  ::memset( m_refIdc, 0, sizeof(m_refIdc) );
1948}
1949
1950TComReferencePictureSet::~TComReferencePictureSet()
1951{
1952}
1953
1954Void TComReferencePictureSet::setUsed(Int bufferNum, Bool used)
1955{
1956  m_used[bufferNum] = used;
1957}
1958
1959Void TComReferencePictureSet::setDeltaPOC(Int bufferNum, Int deltaPOC)
1960{
1961  m_deltaPOC[bufferNum] = deltaPOC;
1962}
1963
1964Void TComReferencePictureSet::setNumberOfPictures(Int numberOfPictures)
1965{
1966  m_numberOfPictures = numberOfPictures;
1967}
1968
1969Int TComReferencePictureSet::getUsed(Int bufferNum)
1970{
1971  return m_used[bufferNum];
1972}
1973
1974Int TComReferencePictureSet::getDeltaPOC(Int bufferNum)
1975{
1976  return m_deltaPOC[bufferNum];
1977}
1978
1979Int TComReferencePictureSet::getNumberOfPictures()
1980{
1981  return m_numberOfPictures;
1982}
1983
1984Int TComReferencePictureSet::getPOC(Int bufferNum)
1985{
1986  return m_POC[bufferNum];
1987}
1988
1989Void TComReferencePictureSet::setPOC(Int bufferNum, Int POC)
1990{
1991  m_POC[bufferNum] = POC;
1992}
1993
1994Bool TComReferencePictureSet::getCheckLTMSBPresent(Int bufferNum)
1995{
1996  return m_bCheckLTMSB[bufferNum];
1997}
1998
1999Void TComReferencePictureSet::setCheckLTMSBPresent(Int bufferNum, Bool b)
2000{
2001  m_bCheckLTMSB[bufferNum] = b;
2002}
2003
2004/** set the reference idc value at uiBufferNum entry to the value of iRefIdc
2005 * \param uiBufferNum
2006 * \param iRefIdc
2007 * \returns Void
2008 */
2009Void TComReferencePictureSet::setRefIdc(Int bufferNum, Int refIdc)
2010{
2011  m_refIdc[bufferNum] = refIdc;
2012}
2013
2014/** get the reference idc value at uiBufferNum
2015 * \param uiBufferNum
2016 * \returns Int
2017 */
2018Int  TComReferencePictureSet::getRefIdc(Int bufferNum)
2019{
2020  return m_refIdc[bufferNum];
2021}
2022
2023/** Sorts the deltaPOC and Used by current values in the RPS based on the deltaPOC values.
2024 *  deltaPOC values are sorted with -ve values before the +ve values.  -ve values are in decreasing order.
2025 *  +ve values are in increasing order.
2026 * \returns Void
2027 */
2028Void TComReferencePictureSet::sortDeltaPOC()
2029{
2030  // sort in increasing order (smallest first)
2031  for(Int j=1; j < getNumberOfPictures(); j++)
2032  { 
2033    Int deltaPOC = getDeltaPOC(j);
2034    Bool used = getUsed(j);
2035    for (Int k=j-1; k >= 0; k--)
2036    {
2037      Int temp = getDeltaPOC(k);
2038      if (deltaPOC < temp)
2039      {
2040        setDeltaPOC(k+1, temp);
2041        setUsed(k+1, getUsed(k));
2042        setDeltaPOC(k, deltaPOC);
2043        setUsed(k, used);
2044      }
2045    }
2046  }
2047  // flip the negative values to largest first
2048  Int numNegPics = getNumberOfNegativePictures();
2049  for(Int j=0, k=numNegPics-1; j < numNegPics>>1; j++, k--)
2050  { 
2051    Int deltaPOC = getDeltaPOC(j);
2052    Bool used = getUsed(j);
2053    setDeltaPOC(j, getDeltaPOC(k));
2054    setUsed(j, getUsed(k));
2055    setDeltaPOC(k, deltaPOC);
2056    setUsed(k, used);
2057  }
2058}
2059
2060/** Prints the deltaPOC and RefIdc (if available) values in the RPS.
2061 *  A "*" is added to the deltaPOC value if it is Used bu current.
2062 * \returns Void
2063 */
2064Void TComReferencePictureSet::printDeltaPOC()
2065{
2066  printf("DeltaPOC = { ");
2067  for(Int j=0; j < getNumberOfPictures(); j++)
2068  {
2069    printf("%d%s ", getDeltaPOC(j), (getUsed(j)==1)?"*":"");
2070  } 
2071  if (getInterRPSPrediction()) 
2072  {
2073    printf("}, RefIdc = { ");
2074    for(Int j=0; j < getNumRefIdc(); j++)
2075    {
2076      printf("%d ", getRefIdc(j));
2077    } 
2078  }
2079  printf("}\n");
2080}
2081
2082TComRPSList::TComRPSList()
2083:m_referencePictureSets (NULL)
2084{
2085}
2086
2087TComRPSList::~TComRPSList()
2088{
2089}
2090
2091Void TComRPSList::create( Int numberOfReferencePictureSets)
2092{
2093  m_numberOfReferencePictureSets = numberOfReferencePictureSets;
2094  m_referencePictureSets = new TComReferencePictureSet[numberOfReferencePictureSets];
2095}
2096
2097Void TComRPSList::destroy()
2098{
2099  if (m_referencePictureSets)
2100  {
2101    delete [] m_referencePictureSets;
2102  }
2103  m_numberOfReferencePictureSets = 0;
2104  m_referencePictureSets = NULL;
2105}
2106
2107
2108
2109TComReferencePictureSet* TComRPSList::getReferencePictureSet(Int referencePictureSetNum)
2110{
2111  return &m_referencePictureSets[referencePictureSetNum];
2112}
2113
2114Int TComRPSList::getNumberOfReferencePictureSets()
2115{
2116  return m_numberOfReferencePictureSets;
2117}
2118
2119Void TComRPSList::setNumberOfReferencePictureSets(Int numberOfReferencePictureSets)
2120{
2121  m_numberOfReferencePictureSets = numberOfReferencePictureSets;
2122}
2123
2124TComRefPicListModification::TComRefPicListModification()
2125: m_bRefPicListModificationFlagL0 (false)
2126, m_bRefPicListModificationFlagL1 (false)
2127{
2128  ::memset( m_RefPicSetIdxL0, 0, sizeof(m_RefPicSetIdxL0) );
2129  ::memset( m_RefPicSetIdxL1, 0, sizeof(m_RefPicSetIdxL1) );
2130}
2131
2132TComRefPicListModification::~TComRefPicListModification()
2133{
2134}
2135
2136TComScalingList::TComScalingList()
2137{
2138  m_useTransformSkip = false;
2139  init();
2140}
2141TComScalingList::~TComScalingList()
2142{
2143  destroy();
2144}
2145
2146/** set default quantization matrix to array
2147*/
2148Void TComSlice::setDefaultScalingList()
2149{
2150  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
2151  {
2152    for(UInt listId=0;listId<g_scalingListNum[sizeId];listId++)
2153    {
2154      getScalingList()->processDefaultMarix(sizeId, listId);
2155    }
2156  }
2157}
2158/** check if use default quantization matrix
2159 * \returns true if use default quantization matrix in all size
2160*/
2161Bool TComSlice::checkDefaultScalingList()
2162{
2163  UInt defaultCounter=0;
2164
2165  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
2166  {
2167    for(UInt listId=0;listId<g_scalingListNum[sizeId];listId++)
2168    {
2169      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
2170     && ((sizeId < SCALING_LIST_16x16) || (getScalingList()->getScalingListDC(sizeId,listId) == 16))) // check DC value
2171      {
2172        defaultCounter++;
2173      }
2174    }
2175  }
2176  return (defaultCounter == (SCALING_LIST_NUM * SCALING_LIST_SIZE_NUM - 4)) ? false : true; // -4 for 32x32
2177}
2178
2179#if H_MV
2180Void TComSlice::createAndApplyIvReferencePictureSet( TComPicLists* ivPicLists, std::vector<TComPic*>& refPicSetInterLayer )
2181{
2182  refPicSetInterLayer.clear(); 
2183
2184  for( Int i = 0; i < getVPS()->getNumDirectRefLayers( getLayerIdInVps() ); i++ ) 
2185  {
2186    Int layerIdRef = getVPS()->getRefLayerId( getLayerIdInVps(), i ); 
2187    TComPic* picRef = ivPicLists->getPic( layerIdRef, getPOC() ) ; 
2188    assert ( picRef != 0 ); 
2189
2190    picRef->getPicYuvRec()->extendPicBorder(); 
2191    picRef->setIsLongTerm( true );       
2192    picRef->getSlice(0)->setReferenced( true );       
2193
2194    refPicSetInterLayer.push_back( picRef ); 
2195  }
2196}
2197
2198Void TComSlice::markIvRefPicsAsShortTerm( std::vector<TComPic*> refPicSetInterLayer )
2199{
2200  // Mark as shortterm
2201  for ( Int i = 0; i < refPicSetInterLayer.size(); i++ ) 
2202  {
2203    refPicSetInterLayer[i]->setIsLongTerm( false ); 
2204  }
2205}
2206
2207Void TComSlice::markIvRefPicsAsUnused( TComPicLists* ivPicLists, std::vector<Int> targetDecLayerIdSet, TComVPS* vps, Int curLayerId, Int curPoc )
2208{
2209  // Fill targetDecLayerIdSet with all layers if empty.
2210  if (targetDecLayerIdSet.size() == 0 )   
2211  {
2212    for ( Int layerIdInVps = 0; layerIdInVps < vps->getMaxLayers(); layerIdInVps++ )
2213    {
2214      targetDecLayerIdSet.push_back( vps->getLayerIdInNuh( layerIdInVps ) ); 
2215    }
2216  }     
2217
2218  Int numTargetDecLayers = (Int) targetDecLayerIdSet.size(); 
2219  Int latestDecIdx; 
2220  for ( latestDecIdx = 0; latestDecIdx < numTargetDecLayers; latestDecIdx++)
2221  {
2222    if ( targetDecLayerIdSet[ latestDecIdx ] == curLayerId )
2223      break; 
2224  }       
2225
2226  for( Int i = 0; i <= latestDecIdx; i++ ) 
2227  {
2228    if ( vps->nuhLayerIdIncluded( targetDecLayerIdSet[ i ] ) )
2229    {
2230      TComPic* pcPic = ivPicLists->getPic( targetDecLayerIdSet[ i ], curPoc ); 
2231      if( pcPic->getSlice(0)->isReferenced() && pcPic->getSlice(0)->getTemporalLayerNonReferenceFlag() ) 
2232      { 
2233        Bool remainingInterLayerReferencesFlag = false; 
2234        for( Int j = latestDecIdx + 1; j < numTargetDecLayers; j++ )
2235        { 
2236          TComVPS* vpsSlice = pcPic->getSlice(0)->getVPS(); 
2237          if ( vps->nuhLayerIdIncluded( targetDecLayerIdSet[ j ] ) )
2238          {
2239            Int targetDecLayerIdinVPS = vpsSlice->getLayerIdInVps( targetDecLayerIdSet[ j ] ); 
2240            for( Int k = 0; k < vpsSlice->getNumDirectRefLayers( targetDecLayerIdinVPS ); k++ )
2241              if ( targetDecLayerIdSet[ i ] == vpsSlice->getRefLayerId( targetDecLayerIdinVPS,  k  ) )
2242                remainingInterLayerReferencesFlag = true;
2243          }
2244        }
2245        if( !remainingInterLayerReferencesFlag )
2246          pcPic->getSlice(0)->setReferenced( false );                   
2247      }
2248    }
2249  }
2250}
2251
2252Void TComSlice::xPrintRefPicList()
2253{ 
2254  for ( Int li = 0; li < 2; li++)
2255  {   
2256    std::cout << std::endl << "RefPicListL" <<  li << ":" << std::endl; 
2257    for (Int rIdx = 0; rIdx <= (m_aiNumRefIdx[li]-1); rIdx ++)
2258    {     
2259      if (rIdx == 0 && li == 0) m_apcRefPicList[li][rIdx]->print( true );
2260       
2261      m_apcRefPicList[li][rIdx]->print( false );
2262    }
2263  }
2264}
2265#endif
2266/** get scaling matrix from RefMatrixID
2267 * \param sizeId size index
2268 * \param Index of input matrix
2269 * \param Index of reference matrix
2270 */
2271Void TComScalingList::processRefMatrix( UInt sizeId, UInt listId , UInt refListId )
2272{
2273  ::memcpy(getScalingListAddress(sizeId, listId),((listId == refListId)? getScalingListDefaultAddress(sizeId, refListId): getScalingListAddress(sizeId, refListId)),sizeof(Int)*min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId]));
2274}
2275
2276/** parse syntax infomation
2277 *  \param pchFile syntax infomation
2278 *  \returns false if successful
2279 */
2280Bool TComScalingList::xParseScalingList(Char* pchFile)
2281{
2282  FILE *fp;
2283  Char line[1024];
2284  UInt sizeIdc,listIdc;
2285  UInt i,size = 0;
2286  Int *src=0,data;
2287  Char *ret;
2288  UInt  retval;
2289
2290  if((fp = fopen(pchFile,"r")) == (FILE*)NULL)
2291  {
2292    printf("can't open file %s :: set Default Matrix\n",pchFile);
2293    return true;
2294  }
2295
2296  for(sizeIdc = 0; sizeIdc < SCALING_LIST_SIZE_NUM; sizeIdc++)
2297  {
2298    size = min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeIdc]);
2299    for(listIdc = 0; listIdc < g_scalingListNum[sizeIdc]; listIdc++)
2300    {
2301      src = getScalingListAddress(sizeIdc, listIdc);
2302
2303      fseek(fp,0,0);
2304      do 
2305      {
2306        ret = fgets(line, 1024, fp);
2307        if ((ret==NULL)||(strstr(line, MatrixType[sizeIdc][listIdc])==NULL && feof(fp)))
2308        {
2309          printf("Error: can't read Matrix :: set Default Matrix\n");
2310          return true;
2311        }
2312      }
2313      while (strstr(line, MatrixType[sizeIdc][listIdc]) == NULL);
2314      for (i=0; i<size; i++)
2315      {
2316        retval = fscanf(fp, "%d,", &data);
2317        if (retval!=1)
2318        {
2319          printf("Error: can't read Matrix :: set Default Matrix\n");
2320          return true;
2321        }
2322        src[i] = data;
2323      }
2324      //set DC value for default matrix check
2325      setScalingListDC(sizeIdc,listIdc,src[0]);
2326
2327      if(sizeIdc > SCALING_LIST_8x8)
2328      {
2329        fseek(fp,0,0);
2330        do 
2331        {
2332          ret = fgets(line, 1024, fp);
2333          if ((ret==NULL)||(strstr(line, MatrixType_DC[sizeIdc][listIdc])==NULL && feof(fp)))
2334          {
2335            printf("Error: can't read DC :: set Default Matrix\n");
2336            return true;
2337          }
2338        }
2339        while (strstr(line, MatrixType_DC[sizeIdc][listIdc]) == NULL);
2340        retval = fscanf(fp, "%d,", &data);
2341        if (retval!=1)
2342        {
2343          printf("Error: can't read Matrix :: set Default Matrix\n");
2344          return true;
2345        }
2346        //overwrite DC value when size of matrix is larger than 16x16
2347        setScalingListDC(sizeIdc,listIdc,data);
2348      }
2349    }
2350  }
2351  fclose(fp);
2352  return false;
2353}
2354
2355/** initialization process of quantization matrix array
2356 */
2357Void TComScalingList::init()
2358{
2359  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
2360  {
2361    for(UInt listId = 0; listId < g_scalingListNum[sizeId]; listId++)
2362    {
2363      m_scalingListCoef[sizeId][listId] = new Int [min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId])];
2364    }
2365  }
2366  m_scalingListCoef[SCALING_LIST_32x32][3] = m_scalingListCoef[SCALING_LIST_32x32][1]; // copy address for 32x32
2367}
2368
2369/** destroy quantization matrix array
2370 */
2371Void TComScalingList::destroy()
2372{
2373  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
2374  {
2375    for(UInt listId = 0; listId < g_scalingListNum[sizeId]; listId++)
2376    {
2377      if(m_scalingListCoef[sizeId][listId]) delete [] m_scalingListCoef[sizeId][listId];
2378    }
2379  }
2380}
2381
2382/** get default address of quantization matrix
2383 * \param sizeId size index
2384 * \param listId list index
2385 * \returns pointer of quantization matrix
2386 */
2387Int* TComScalingList::getScalingListDefaultAddress(UInt sizeId, UInt listId)
2388{
2389  Int *src = 0;
2390  switch(sizeId)
2391  {
2392    case SCALING_LIST_4x4:
2393      src = g_quantTSDefault4x4;
2394      break;
2395    case SCALING_LIST_8x8:
2396      src = (listId<3) ? g_quantIntraDefault8x8 : g_quantInterDefault8x8;
2397      break;
2398    case SCALING_LIST_16x16:
2399      src = (listId<3) ? g_quantIntraDefault8x8 : g_quantInterDefault8x8;
2400      break;
2401    case SCALING_LIST_32x32:
2402      src = (listId<1) ? g_quantIntraDefault8x8 : g_quantInterDefault8x8;
2403      break;
2404    default:
2405      assert(0);
2406      src = NULL;
2407      break;
2408  }
2409  return src;
2410}
2411
2412/** process of default matrix
2413 * \param sizeId size index
2414 * \param Index of input matrix
2415 */
2416Void TComScalingList::processDefaultMarix(UInt sizeId, UInt listId)
2417{
2418  ::memcpy(getScalingListAddress(sizeId, listId),getScalingListDefaultAddress(sizeId,listId),sizeof(Int)*min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId]));
2419  setScalingListDC(sizeId,listId,SCALING_LIST_DC);
2420}
2421
2422/** check DC value of matrix for default matrix signaling
2423 */
2424Void TComScalingList::checkDcOfMatrix()
2425{
2426  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
2427  {
2428    for(UInt listId = 0; listId < g_scalingListNum[sizeId]; listId++)
2429    {
2430      //check default matrix?
2431      if(getScalingListDC(sizeId,listId) == 0)
2432      {
2433        processDefaultMarix(sizeId, listId);
2434      }
2435    }
2436  }
2437}
2438
2439ParameterSetManager::ParameterSetManager()
2440: m_vpsMap(MAX_NUM_VPS)
2441, m_spsMap(MAX_NUM_SPS)
2442, m_ppsMap(MAX_NUM_PPS)
2443, m_activeVPSId(-1)
2444, m_activeSPSId(-1)
2445, m_activePPSId(-1)
2446{
2447}
2448
2449
2450ParameterSetManager::~ParameterSetManager()
2451{
2452}
2453
2454//! activate a SPS from a active parameter sets SEI message
2455//! \returns true, if activation is successful
2456Bool ParameterSetManager::activateSPSWithSEI(Int spsId)
2457{
2458  TComSPS *sps = m_spsMap.getPS(spsId);
2459  if (sps)
2460  {
2461    Int vpsId = sps->getVPSId();
2462    if (m_vpsMap.getPS(vpsId))
2463    {
2464      m_activeVPSId = vpsId;
2465      m_activeSPSId = spsId;
2466      return true;
2467    }
2468    else
2469    {
2470      printf("Warning: tried to activate SPS using an Active parameter sets SEI message. Referenced VPS does not exist.");
2471    }
2472  }
2473  else
2474  {
2475    printf("Warning: tried to activate non-existing SPS using an Active parameter sets SEI message.");
2476  }
2477  return false;
2478}
2479
2480//! activate a PPS and depending on isIDR parameter also SPS and VPS
2481//! \returns true, if activation is successful
2482Bool ParameterSetManager::activatePPS(Int ppsId, Bool isIRAP)
2483{
2484  TComPPS *pps = m_ppsMap.getPS(ppsId);
2485  if (pps)
2486  {
2487    Int spsId = pps->getSPSId();
2488#if H_MV
2489    // active parameter sets per layer should be used here
2490#else
2491    if (!isIRAP && (spsId != m_activeSPSId))
2492    {
2493      printf("Warning: tried to activate PPS referring to a inactive SPS at non-IRAP.");
2494      return false;
2495    }
2496#endif
2497    TComSPS *sps = m_spsMap.getPS(spsId);
2498    if (sps)
2499    {
2500      Int vpsId = sps->getVPSId();
2501      if (!isIRAP && (vpsId != m_activeVPSId))
2502      {
2503        printf("Warning: tried to activate PPS referring to a inactive VPS at non-IRAP.");
2504        return false;
2505      }
2506      if (m_vpsMap.getPS(vpsId))
2507      {
2508        m_activePPSId = ppsId;
2509        m_activeVPSId = vpsId;
2510        m_activeSPSId = spsId;
2511        return true;
2512      }
2513      else
2514      {
2515        printf("Warning: tried to activate PPS that refers to a non-existing VPS.");
2516      }
2517    }
2518    else
2519    {
2520      printf("Warning: tried to activate a PPS that refers to a non-existing SPS.");
2521    }
2522  }
2523  else
2524  {
2525    printf("Warning: tried to activate non-existing PPS.");
2526  }
2527  return false;
2528}
2529
2530ProfileTierLevel::ProfileTierLevel()
2531  : m_profileSpace    (0)
2532  , m_tierFlag        (false)
2533  , m_profileIdc      (0)
2534  , m_levelIdc        (0)
2535#if L0046_CONSTRAINT_FLAGS
2536, m_progressiveSourceFlag  (false)
2537, m_interlacedSourceFlag   (false)
2538, m_nonPackedConstraintFlag(false)
2539, m_frameOnlyConstraintFlag(false)
2540#endif
2541{
2542  ::memset(m_profileCompatibilityFlag, 0, sizeof(m_profileCompatibilityFlag));
2543}
2544
2545TComPTL::TComPTL()
2546{
2547  ::memset(m_subLayerProfilePresentFlag, 0, sizeof(m_subLayerProfilePresentFlag));
2548  ::memset(m_subLayerLevelPresentFlag,   0, sizeof(m_subLayerLevelPresentFlag  ));
2549}
2550
2551#if H_MV
2552Void TComPTL::copyLevelFrom( TComPTL* source )
2553{
2554  getGeneralPTL()->setLevelIdc( source->getGeneralPTL()->getLevelIdc() );
2555  for( Int subLayer = 0; subLayer < 6; subLayer++ )
2556  {
2557    setSubLayerLevelPresentFlag( subLayer, source->getSubLayerLevelPresentFlag( subLayer ) );
2558    getSubLayerPTL( subLayer )->setLevelIdc( source->getSubLayerPTL( subLayer )->getLevelIdc() );
2559  }
2560}
2561#endif
2562#if SIGNAL_BITRATE_PICRATE_IN_VPS
2563TComBitRatePicRateInfo::TComBitRatePicRateInfo()
2564{
2565  ::memset(m_bitRateInfoPresentFlag, 0, sizeof(m_bitRateInfoPresentFlag));
2566  ::memset(m_picRateInfoPresentFlag, 0, sizeof(m_picRateInfoPresentFlag));
2567  ::memset(m_avgBitRate,             0, sizeof(m_avgBitRate));
2568  ::memset(m_maxBitRate,             0, sizeof(m_maxBitRate));
2569  ::memset(m_constantPicRateIdc,     0, sizeof(m_constantPicRateIdc));
2570  ::memset(m_avgPicRate,             0, sizeof(m_avgPicRate));
2571}
2572#endif
2573//! \}
Note: See TracBrowser for help on using the repository browser.