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

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