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

Last change on this file since 468 was 468, checked in by lg, 11 years ago

1.IC and full pel depth coding are integrated and is guarded by Macro H_3D_IC.

  • Property svn:eol-style set to native
File size: 81.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, 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#if H_3D_IC
117, m_bApplyIC                      ( false )
118, m_icSkipParseFlag               ( false )
119#endif
120#endif
121{
122#if L0034_COMBINED_LIST_CLEANUP
123  m_aiNumRefIdx[0] = m_aiNumRefIdx[1] = 0;
124#else
125  m_aiNumRefIdx[0] = m_aiNumRefIdx[1] = m_aiNumRefIdx[2] = 0;
126#endif
127 
128  initEqualRef();
129 
130#if L0034_COMBINED_LIST_CLEANUP
131  for ( Int idx = 0; idx < MAX_NUM_REF; idx++ )
132  {
133    m_list1IdxToList0Idx[idx] = -1;
134  }
135#else
136  for(Int iNumCount = 0; iNumCount < MAX_NUM_REF_LC; iNumCount++)
137  {
138    m_iRefIdxOfLC[REF_PIC_LIST_0][iNumCount]=-1;
139    m_iRefIdxOfLC[REF_PIC_LIST_1][iNumCount]=-1;
140    m_eListIdFromIdxOfLC[iNumCount]=0;
141    m_iRefIdxFromIdxOfLC[iNumCount]=0;
142    m_iRefIdxOfL0FromRefIdxOfL1[iNumCount] = -1;
143    m_iRefIdxOfL1FromRefIdxOfL0[iNumCount] = -1;
144  }   
145#endif
146  for(Int iNumCount = 0; iNumCount < MAX_NUM_REF; iNumCount++)
147  {
148    m_apcRefPicList [0][iNumCount] = NULL;
149    m_apcRefPicList [1][iNumCount] = NULL;
150    m_aiRefPOCList  [0][iNumCount] = 0;
151    m_aiRefPOCList  [1][iNumCount] = 0;
152#if H_MV
153    m_aiRefLayerIdList[0][iNumCount] = 0;
154    m_aiRefLayerIdList[1][iNumCount] = 0;
155#endif
156  }
157  resetWpScaling();
158  initWpAcDcParam();
159  m_saoEnabledFlag = false;
160}
161
162TComSlice::~TComSlice()
163{
164  delete[] m_puiSubstreamSizes;
165  m_puiSubstreamSizes = NULL;
166}
167
168
169Void TComSlice::initSlice()
170{
171  m_aiNumRefIdx[0]      = 0;
172  m_aiNumRefIdx[1]      = 0;
173 
174  m_colFromL0Flag = 1;
175 
176  m_colRefIdx = 0;
177  initEqualRef();
178#if !L0034_COMBINED_LIST_CLEANUP
179  m_bNoBackPredFlag = false;
180  m_bRefPicListCombinationFlag = false;
181  m_bRefPicListModificationFlagLC = false;
182#endif
183  m_bCheckLDC = false;
184  m_iSliceQpDeltaCb = 0;
185  m_iSliceQpDeltaCr = 0;
186
187#if !L0034_COMBINED_LIST_CLEANUP
188  m_aiNumRefIdx[REF_PIC_LIST_C]      = 0;
189#endif
190
191  m_maxNumMergeCand = MRG_MAX_NUM_CANDS;
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#if H_3D_IC
973  m_bApplyIC = pSrc->m_bApplyIC;
974  m_icSkipParseFlag = pSrc->m_icSkipParseFlag;
975#endif
976}
977
978Int TComSlice::m_prevPOC = 0;
979
980/** Function for setting the slice's temporal layer ID and corresponding temporal_layer_switching_point_flag.
981 * \param uiTLayer Temporal layer ID of the current slice
982 * The decoder calls this function to set temporal_layer_switching_point_flag for each temporal layer based on
983 * the SPS's temporal_id_nesting_flag and the parsed PPS.  Then, current slice's temporal layer ID and
984 * temporal_layer_switching_point_flag is set accordingly.
985 */
986Void TComSlice::setTLayerInfo( UInt uiTLayer )
987{
988  m_uiTLayer = uiTLayer;
989}
990
991/** Function for checking if this is a switching-point
992*/
993Bool TComSlice::isTemporalLayerSwitchingPoint( TComList<TComPic*>& rcListPic )
994{
995  TComPic* rpcPic;
996  // loop through all pictures in the reference picture buffer
997  TComList<TComPic*>::iterator iterPic = rcListPic.begin();
998  while ( iterPic != rcListPic.end())
999  {
1000    rpcPic = *(iterPic++);
1001    if(rpcPic->getSlice(0)->isReferenced() && rpcPic->getPOC() != getPOC())
1002    {
1003      if(rpcPic->getTLayer() >= getTLayer())
1004      {
1005        return false;
1006      }
1007    }
1008  }
1009  return true;
1010}
1011
1012/** Function for checking if this is a STSA candidate
1013 */
1014Bool TComSlice::isStepwiseTemporalLayerSwitchingPointCandidate( TComList<TComPic*>& rcListPic )
1015{
1016    TComPic* rpcPic;
1017   
1018    TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1019    while ( iterPic != rcListPic.end())
1020    {
1021        rpcPic = *(iterPic++);
1022        if(rpcPic->getSlice(0)->isReferenced() &&  (rpcPic->getUsedByCurr()==true) && rpcPic->getPOC() != getPOC())
1023        {
1024            if(rpcPic->getTLayer() >= getTLayer())
1025            {
1026                return false;
1027            }
1028        }
1029    }
1030    return true;
1031}
1032
1033/** Function for applying picture marking based on the Reference Picture Set in pReferencePictureSet.
1034*/
1035Void TComSlice::applyReferencePictureSet( TComList<TComPic*>& rcListPic, TComReferencePictureSet *pReferencePictureSet)
1036{
1037  TComPic* rpcPic;
1038  Int i, isReference;
1039
1040  // loop through all pictures in the reference picture buffer
1041  TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1042  while ( iterPic != rcListPic.end())
1043  {
1044    rpcPic = *(iterPic++);
1045
1046    if(!rpcPic->getSlice( 0 )->isReferenced())
1047    {
1048      continue;
1049    }
1050
1051    isReference = 0;
1052    // loop through all pictures in the Reference Picture Set
1053    // to see if the picture should be kept as reference picture
1054    for(i=0;i<pReferencePictureSet->getNumberOfPositivePictures()+pReferencePictureSet->getNumberOfNegativePictures();i++)
1055    {
1056      if(!rpcPic->getIsLongTerm() && rpcPic->getPicSym()->getSlice(0)->getPOC() == this->getPOC() + pReferencePictureSet->getDeltaPOC(i))
1057      {
1058        isReference = 1;
1059        rpcPic->setUsedByCurr(pReferencePictureSet->getUsed(i));
1060        rpcPic->setIsLongTerm(0);
1061      }
1062    }
1063    for(;i<pReferencePictureSet->getNumberOfPictures();i++)
1064    {
1065      if(pReferencePictureSet->getCheckLTMSBPresent(i)==true)
1066      {
1067        if(rpcPic->getIsLongTerm() && (rpcPic->getPicSym()->getSlice(0)->getPOC()) == pReferencePictureSet->getPOC(i))
1068        {
1069          isReference = 1;
1070          rpcPic->setUsedByCurr(pReferencePictureSet->getUsed(i));
1071        }
1072      }
1073      else 
1074      {
1075        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()))
1076        {
1077          isReference = 1;
1078          rpcPic->setUsedByCurr(pReferencePictureSet->getUsed(i));
1079        }
1080      }
1081
1082    }
1083    // mark the picture as "unused for reference" if it is not in
1084    // the Reference Picture Set
1085    if(rpcPic->getPicSym()->getSlice(0)->getPOC() != this->getPOC() && isReference == 0)   
1086    {           
1087      rpcPic->getSlice( 0 )->setReferenced( false );   
1088      rpcPic->setUsedByCurr(0);
1089      rpcPic->setIsLongTerm(0);
1090    }
1091    //check that pictures of higher temporal layers are not used
1092    assert(rpcPic->getSlice( 0 )->isReferenced()==0||rpcPic->getUsedByCurr()==0||rpcPic->getTLayer()<=this->getTLayer());
1093    //check that pictures of higher or equal temporal layer are not in the RPS if the current picture is a TSA picture
1094    if(this->getNalUnitType() == NAL_UNIT_CODED_SLICE_TLA_R || this->getNalUnitType() == NAL_UNIT_CODED_SLICE_TSA_N)
1095    {
1096      assert(rpcPic->getSlice( 0 )->isReferenced()==0||rpcPic->getTLayer()<this->getTLayer());
1097    }
1098    //check that pictures marked as temporal layer non-reference pictures are not used for reference
1099    if(rpcPic->getPicSym()->getSlice(0)->getPOC() != this->getPOC() && rpcPic->getTLayer()==this->getTLayer())
1100    {
1101      assert(rpcPic->getSlice( 0 )->isReferenced()==0||rpcPic->getUsedByCurr()==0||rpcPic->getSlice( 0 )->getTemporalLayerNonReferenceFlag()==false);
1102    }
1103  }
1104}
1105
1106/** Function for applying picture marking based on the Reference Picture Set in pReferencePictureSet.
1107*/
1108Int TComSlice::checkThatAllRefPicsAreAvailable( TComList<TComPic*>& rcListPic, TComReferencePictureSet *pReferencePictureSet, Bool printErrors, Int pocRandomAccess)
1109{
1110  TComPic* rpcPic;
1111  Int i, isAvailable;
1112  Int atLeastOneLost = 0;
1113  Int atLeastOneRemoved = 0;
1114  Int iPocLost = 0;
1115
1116  // loop through all long-term pictures in the Reference Picture Set
1117  // to see if the picture should be kept as reference picture
1118  for(i=pReferencePictureSet->getNumberOfNegativePictures()+pReferencePictureSet->getNumberOfPositivePictures();i<pReferencePictureSet->getNumberOfPictures();i++)
1119  {
1120    isAvailable = 0;
1121    // loop through all pictures in the reference picture buffer
1122    TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1123    while ( iterPic != rcListPic.end())
1124    {
1125      rpcPic = *(iterPic++);
1126      if(pReferencePictureSet->getCheckLTMSBPresent(i)==true)
1127      {
1128        if(rpcPic->getIsLongTerm() && (rpcPic->getPicSym()->getSlice(0)->getPOC()) == pReferencePictureSet->getPOC(i) && rpcPic->getSlice(0)->isReferenced())
1129        {
1130          isAvailable = 1;
1131        }
1132      }
1133      else 
1134      {
1135        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())
1136        {
1137          isAvailable = 1;
1138        }
1139      }
1140    }
1141    // if there was no such long-term check the short terms
1142    if(!isAvailable)
1143    {
1144      iterPic = rcListPic.begin();
1145      while ( iterPic != rcListPic.end())
1146      {
1147        rpcPic = *(iterPic++);
1148
1149        Int pocCycle = 1 << rpcPic->getPicSym()->getSlice(0)->getSPS()->getBitsForPOC();
1150        Int curPoc = rpcPic->getPicSym()->getSlice(0)->getPOC();
1151        Int refPoc = pReferencePictureSet->getPOC(i);
1152        if (!pReferencePictureSet->getCheckLTMSBPresent(i))
1153        {
1154          curPoc = curPoc % pocCycle;
1155          refPoc = refPoc % pocCycle;
1156        }
1157       
1158        if (rpcPic->getSlice(0)->isReferenced() && curPoc == refPoc)
1159        {
1160          isAvailable = 1;
1161          rpcPic->setIsLongTerm(1);
1162          break;
1163        }
1164      }
1165    }
1166    // report that a picture is lost if it is in the Reference Picture Set
1167    // but not available as reference picture
1168    if(isAvailable == 0)   
1169    {           
1170      if (this->getPOC() + pReferencePictureSet->getDeltaPOC(i) >= pocRandomAccess)
1171      {
1172        if(!pReferencePictureSet->getUsed(i) )
1173        {
1174          if(printErrors)
1175          {
1176            printf("\nLong-term reference picture with POC = %3d seems to have been removed or not correctly decoded.", this->getPOC() + pReferencePictureSet->getDeltaPOC(i));
1177          }
1178          atLeastOneRemoved = 1;
1179        }
1180        else
1181        {
1182          if(printErrors)
1183          {
1184            printf("\nLong-term reference picture with POC = %3d is lost or not correctly decoded!", this->getPOC() + pReferencePictureSet->getDeltaPOC(i));
1185          }
1186          atLeastOneLost = 1;
1187          iPocLost=this->getPOC() + pReferencePictureSet->getDeltaPOC(i);
1188        }
1189      }
1190    }
1191  } 
1192  // loop through all short-term pictures in the Reference Picture Set
1193  // to see if the picture should be kept as reference picture
1194  for(i=0;i<pReferencePictureSet->getNumberOfNegativePictures()+pReferencePictureSet->getNumberOfPositivePictures();i++)
1195  {
1196    isAvailable = 0;
1197    // loop through all pictures in the reference picture buffer
1198    TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1199    while ( iterPic != rcListPic.end())
1200    {
1201      rpcPic = *(iterPic++);
1202
1203      if(!rpcPic->getIsLongTerm() && rpcPic->getPicSym()->getSlice(0)->getPOC() == this->getPOC() + pReferencePictureSet->getDeltaPOC(i) && rpcPic->getSlice(0)->isReferenced())
1204      {
1205        isAvailable = 1;
1206      }
1207    }
1208    // report that a picture is lost if it is in the Reference Picture Set
1209    // but not available as reference picture
1210    if(isAvailable == 0)   
1211    {           
1212      if (this->getPOC() + pReferencePictureSet->getDeltaPOC(i) >= pocRandomAccess)
1213      {
1214        if(!pReferencePictureSet->getUsed(i) )
1215        {
1216          if(printErrors)
1217          {
1218            printf("\nShort-term reference picture with POC = %3d seems to have been removed or not correctly decoded.", this->getPOC() + pReferencePictureSet->getDeltaPOC(i));
1219          }
1220          atLeastOneRemoved = 1;
1221        }
1222        else
1223        {
1224          if(printErrors)
1225          {
1226            printf("\nShort-term reference picture with POC = %3d is lost or not correctly decoded!", this->getPOC() + pReferencePictureSet->getDeltaPOC(i));
1227          }
1228          atLeastOneLost = 1;
1229          iPocLost=this->getPOC() + pReferencePictureSet->getDeltaPOC(i);
1230        }
1231      }
1232    }
1233  }   
1234  if(atLeastOneLost)
1235  {
1236    return iPocLost+1;
1237  }
1238  if(atLeastOneRemoved)
1239  {
1240    return -2;
1241  }
1242  else
1243  {
1244    return 0;
1245  }
1246}
1247
1248/** Function for constructing an explicit Reference Picture Set out of the available pictures in a referenced Reference Picture Set
1249*/
1250Void TComSlice::createExplicitReferencePictureSetFromReference( TComList<TComPic*>& rcListPic, TComReferencePictureSet *pReferencePictureSet)
1251{
1252  TComPic* rpcPic;
1253  Int i, j;
1254  Int k = 0;
1255  Int nrOfNegativePictures = 0;
1256  Int nrOfPositivePictures = 0;
1257  TComReferencePictureSet* pcRPS = this->getLocalRPS();
1258
1259  // loop through all pictures in the Reference Picture Set
1260  for(i=0;i<pReferencePictureSet->getNumberOfPictures();i++)
1261  {
1262    j = 0;
1263    // loop through all pictures in the reference picture buffer
1264    TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1265    while ( iterPic != rcListPic.end())
1266    {
1267      j++;
1268      rpcPic = *(iterPic++);
1269
1270      if(rpcPic->getPicSym()->getSlice(0)->getPOC() == this->getPOC() + pReferencePictureSet->getDeltaPOC(i) && rpcPic->getSlice(0)->isReferenced())
1271      {
1272        // This picture exists as a reference picture
1273        // and should be added to the explicit Reference Picture Set
1274        pcRPS->setDeltaPOC(k, pReferencePictureSet->getDeltaPOC(i));
1275        pcRPS->setUsed(k, pReferencePictureSet->getUsed(i));
1276        if(pcRPS->getDeltaPOC(k) < 0)
1277        {
1278          nrOfNegativePictures++;
1279        }
1280        else
1281        {
1282          nrOfPositivePictures++;
1283        }
1284        k++;
1285      }
1286    }
1287  }
1288  pcRPS->setNumberOfNegativePictures(nrOfNegativePictures);
1289  pcRPS->setNumberOfPositivePictures(nrOfPositivePictures);
1290  pcRPS->setNumberOfPictures(nrOfNegativePictures+nrOfPositivePictures);
1291  // This is a simplistic inter rps example. A smarter encoder will look for a better reference RPS to do the
1292  // inter RPS prediction with.  Here we just use the reference used by pReferencePictureSet.
1293  // If pReferencePictureSet is not inter_RPS_predicted, then inter_RPS_prediction is for the current RPS also disabled.
1294  if (!pReferencePictureSet->getInterRPSPrediction())
1295  {
1296    pcRPS->setInterRPSPrediction(false);
1297    pcRPS->setNumRefIdc(0);
1298  }
1299  else
1300  {
1301    Int rIdx =  this->getRPSidx() - pReferencePictureSet->getDeltaRIdxMinus1() - 1;
1302    Int deltaRPS = pReferencePictureSet->getDeltaRPS();
1303    TComReferencePictureSet* pcRefRPS = this->getSPS()->getRPSList()->getReferencePictureSet(rIdx);
1304    Int iRefPics = pcRefRPS->getNumberOfPictures();
1305    Int iNewIdc=0;
1306    for(i=0; i<= iRefPics; i++) 
1307    {
1308      Int deltaPOC = ((i != iRefPics)? pcRefRPS->getDeltaPOC(i) : 0);  // check if the reference abs POC is >= 0
1309      Int iRefIdc = 0;
1310      for (j=0; j < pcRPS->getNumberOfPictures(); j++) // loop through the  pictures in the new RPS
1311      {
1312        if ( (deltaPOC + deltaRPS) == pcRPS->getDeltaPOC(j))
1313        {
1314          if (pcRPS->getUsed(j))
1315          {
1316            iRefIdc = 1;
1317          }
1318          else
1319          {
1320            iRefIdc = 2;
1321          }
1322        }
1323      }
1324      pcRPS->setRefIdc(i, iRefIdc);
1325      iNewIdc++;
1326    }
1327    pcRPS->setInterRPSPrediction(true);
1328    pcRPS->setNumRefIdc(iNewIdc);
1329    pcRPS->setDeltaRPS(deltaRPS); 
1330    pcRPS->setDeltaRIdxMinus1(pReferencePictureSet->getDeltaRIdxMinus1() + this->getSPS()->getRPSList()->getNumberOfReferencePictureSets() - this->getRPSidx());
1331  }
1332
1333  this->setRPS(pcRPS);
1334  this->setRPSidx(-1);
1335}
1336
1337/** get AC and DC values for weighted pred
1338 * \param *wp
1339 * \returns Void
1340 */
1341Void  TComSlice::getWpAcDcParam(wpACDCParam *&wp)
1342{
1343  wp = m_weightACDCParam;
1344}
1345
1346/** init AC and DC values for weighted pred
1347 * \returns Void
1348 */
1349Void  TComSlice::initWpAcDcParam()
1350{
1351  for(Int iComp = 0; iComp < 3; iComp++ )
1352  {
1353    m_weightACDCParam[iComp].iAC = 0;
1354    m_weightACDCParam[iComp].iDC = 0;
1355  }
1356}
1357
1358/** get WP tables for weighted pred
1359 * \param RefPicList
1360 * \param iRefIdx
1361 * \param *&wpScalingParam
1362 * \returns Void
1363 */
1364Void  TComSlice::getWpScaling( RefPicList e, Int iRefIdx, wpScalingParam *&wp )
1365{
1366  wp = m_weightPredTable[e][iRefIdx];
1367}
1368
1369/** reset Default WP tables settings : no weight.
1370 * \param wpScalingParam
1371 * \returns Void
1372 */
1373Void  TComSlice::resetWpScaling()
1374{
1375  for ( Int e=0 ; e<2 ; e++ )
1376  {
1377    for ( Int i=0 ; i<MAX_NUM_REF ; i++ )
1378    {
1379      for ( Int yuv=0 ; yuv<3 ; yuv++ )
1380      {
1381        wpScalingParam  *pwp = &(m_weightPredTable[e][i][yuv]);
1382        pwp->bPresentFlag      = false;
1383        pwp->uiLog2WeightDenom = 0;
1384        pwp->uiLog2WeightDenom = 0;
1385        pwp->iWeight           = 1;
1386        pwp->iOffset           = 0;
1387      }
1388    }
1389  }
1390}
1391
1392/** init WP table
1393 * \returns Void
1394 */
1395Void  TComSlice::initWpScaling()
1396{
1397  for ( Int e=0 ; e<2 ; e++ )
1398  {
1399    for ( Int i=0 ; i<MAX_NUM_REF ; i++ )
1400    {
1401      for ( Int yuv=0 ; yuv<3 ; yuv++ )
1402      {
1403        wpScalingParam  *pwp = &(m_weightPredTable[e][i][yuv]);
1404        if ( !pwp->bPresentFlag ) 
1405        {
1406          // Inferring values not present :
1407          pwp->iWeight = (1 << pwp->uiLog2WeightDenom);
1408          pwp->iOffset = 0;
1409        }
1410
1411        pwp->w      = pwp->iWeight;
1412        Int bitDepth = yuv ? g_bitDepthC : g_bitDepthY;
1413        pwp->o      = pwp->iOffset << (bitDepth-8);
1414        pwp->shift  = pwp->uiLog2WeightDenom;
1415        pwp->round  = (pwp->uiLog2WeightDenom>=1) ? (1 << (pwp->uiLog2WeightDenom-1)) : (0);
1416      }
1417    }
1418  }
1419}
1420
1421// ------------------------------------------------------------------------------------------------
1422// Video parameter set (VPS)
1423// ------------------------------------------------------------------------------------------------
1424TComVPS::TComVPS()
1425: m_VPSId                     (  0)
1426, m_uiMaxTLayers              (  1)
1427, m_uiMaxLayers               (  1)
1428, m_bTemporalIdNestingFlag    (false)
1429, m_numHrdParameters          (  0)
1430#if H_MV
1431, m_maxNuhLayerId             (  0)
1432#else
1433, m_maxNuhReservedZeroLayerId (  0)
1434#endif
1435, m_hrdParameters             (NULL)
1436, m_hrdOpSetIdx               (NULL)
1437, m_cprmsPresentFlag          (NULL)
1438{
1439  for( Int i = 0; i < MAX_TLAYER; i++)
1440  {
1441    m_numReorderPics[i] = 0;
1442#if L0323_DPB
1443    m_uiMaxDecPicBuffering[i] = 1; 
1444#else
1445    m_uiMaxDecPicBuffering[i] = 0; 
1446#endif
1447    m_uiMaxLatencyIncrease[i] = 0;
1448  }
1449#if H_MV
1450  m_avcBaseLayerFlag = false;
1451  m_splittingFlag    = false;
1452 
1453  for( Int i = 0; i < MAX_NUM_SCALABILITY_TYPES; i++ )
1454  {
1455    m_scalabilityMask[i] = false;
1456    m_dimensionIdLen [i]  = -1; 
1457  }
1458
1459  m_vpsNuhLayerIdPresentFlag = false;
1460  m_numOutputLayerSets       = 0;
1461
1462  for( Int i = 0; i < MAX_VPS_OP_SETS_PLUS1; i++ )
1463  {
1464    m_vpsProfilePresentFlag   [i] = false;
1465    m_profileLayerSetRefMinus1[i] = 0;
1466    m_outputLayerSetIdx       [i] = 0;
1467    for( Int j = 0; j < MAX_VPS_NUH_LAYER_ID_PLUS1; j++ )
1468    {
1469      m_outputLayerFlag[i][j] = false;
1470    }
1471  }
1472
1473  for( Int i = 0; i < MAX_NUM_LAYER_IDS; i++ )
1474  {
1475    m_layerIdInVps[i] =  (i == 0 ) ? 0 : -1;         
1476  }
1477
1478  for( Int i = 0; i < MAX_NUM_LAYERS; i++ )
1479  {
1480    m_layerIdInNuh      [i] = ( i == 0 ) ? 0 : -1; 
1481    m_numDirectRefLayers[i] = 0; 
1482#if H_3D
1483    m_viewIndex         [i] = -1; 
1484#endif
1485
1486    for( Int j = 0; j < MAX_NUM_LAYERS; j++ )
1487    {
1488      m_directDependencyFlag[i][j] = false;
1489      m_refLayerId[i][j]           = -1; 
1490    }
1491
1492    for( Int j = 0; j < MAX_NUM_SCALABILITY_TYPES; j++ )
1493    {
1494      m_dimensionId[i][j] = 0;
1495    }
1496
1497#if H_3D_ARP
1498    m_uiUseAdvResPred[i]  = 0;
1499    m_uiARPStepNum[i]     = 1;
1500#endif
1501  }
1502#endif
1503}
1504
1505TComVPS::~TComVPS()
1506{
1507if( m_hrdParameters    != NULL )     delete[] m_hrdParameters;
1508  if( m_hrdOpSetIdx      != NULL )     delete[] m_hrdOpSetIdx;
1509  if( m_cprmsPresentFlag != NULL )     delete[] m_cprmsPresentFlag;
1510}
1511
1512#if H_MV
1513
1514Bool TComVPS::checkVPSExtensionSyntax()
1515{
1516  // check splitting flag constraint
1517  if ( getSplittingFlag() )
1518  {
1519    // Derive dimBitOffset[j]
1520    Int dimBitOffset[MAX_NUM_SCALABILITY_TYPES+1];
1521    Int numScalabilityTypes = getNumScalabilityTypes();
1522    dimBitOffset[0] = 0;
1523
1524    for (Int type = 1; type <= numScalabilityTypes; type++ )
1525    {
1526      dimBitOffset[ type ] = 0;
1527      for (Int dimIdx = 0; dimIdx <= type - 1; dimIdx++)
1528        dimBitOffset[ type ] += ( getDimensionIdLen( dimIdx ) );
1529    }
1530
1531    for (Int type = 0; type < getNumScalabilityTypes(); type++ )
1532    {
1533      for( Int layer = 1; layer < getMaxLayers(); layer++ )
1534      {
1535        assert( getDimensionId( layer, type ) == ( ( getLayerIdInNuh( layer ) & ( (1 << dimBitOffset[ type + 1 ] ) - 1) ) >> dimBitOffset[ type ] ) );
1536      }; 
1537  };
1538  }
1539
1540  for( Int layer = 1; layer < getMaxLayers(); layer++ )
1541  {
1542    // check layer_id_in_nuh constraint
1543    assert( getLayerIdInNuh( layer ) > getLayerIdInNuh( layer -1 ) );
1544  }
1545  return true; 
1546}
1547
1548Int TComVPS::getNumScalabilityTypes()
1549{
1550  return scalTypeToScalIdx( ScalabilityType(MAX_NUM_SCALABILITY_TYPES) );
1551}
1552
1553Int TComVPS::scalTypeToScalIdx( ScalabilityType scalType )
1554{
1555  assert( scalType >= 0 && scalType <= MAX_NUM_SCALABILITY_TYPES ); 
1556  assert( scalType == MAX_NUM_SCALABILITY_TYPES || getScalabilityMask( scalType ) );
1557
1558  Int scalIdx = 0; 
1559  for( Int curScalType = 0; curScalType < scalType; curScalType++ )
1560  {
1561    scalIdx += ( getScalabilityMask( curScalType ) ? 1 : 0 );
1562  }
1563
1564  return scalIdx; 
1565}
1566
1567
1568
1569Void TComVPS::setScalabilityMask( UInt val )
1570{
1571  for ( Int scalType = 0; scalType < MAX_NUM_SCALABILITY_TYPES; scalType++ ) 
1572    setScalabilityMask( scalType, ( val & (1 << scalType ) ) != 0 );
1573}
1574
1575Void TComVPS::calcIvRefLayers()
1576{
1577  for( Int i = 1; i <= getMaxLayers(); i++ )
1578  { 
1579    m_numDirectRefLayers[ i ] = 0; 
1580    for( Int j = 0 ; j < i; j++ )
1581      if( m_directDependencyFlag[ i ][ j ])
1582        m_refLayerId[ i ][ m_numDirectRefLayers[ i ]++ ] = m_layerIdInNuh[ j ];   
1583  }
1584}
1585
1586Int TComVPS::getRefLayerId( Int layerIdInVps, Int idx )
1587{
1588  assert( idx >= 0 && idx < m_numDirectRefLayers[layerIdInVps] );     
1589  Int layerIdInNuh = m_refLayerId[ layerIdInVps ][ idx ];   
1590  assert ( layerIdInNuh >= 0 ); 
1591  return layerIdInNuh;
1592}
1593
1594Int TComVPS::getScalabilityId( Int layerIdInVps, ScalabilityType scalType )
1595{
1596  return getScalabilityMask( scalType ) ? getDimensionId( layerIdInVps, scalTypeToScalIdx( scalType ) ) : 0;
1597}
1598
1599#if H_3D
1600Void TComVPS::initViewIndex()
1601{
1602  Int viewIdList   [ MAX_NUM_LAYERS ]; // ed. should be changed to MAX_VIEW_ID
1603  Int viewIndexList[ MAX_NUM_LAYERS ]; 
1604  Int numViewIds = 0; 
1605
1606  for ( Int i = 0 ; i  <  m_uiMaxLayers; i++ )
1607  {     
1608    Int currViewId = getViewId( i ); 
1609
1610    Bool viewIdInListFlag = false; 
1611    for ( Int j = 0; j < numViewIds; j ++ )
1612    {
1613      viewIdInListFlag  = viewIdInListFlag || ( currViewId  == viewIdList[ j ]  );
1614    }
1615
1616    if ( !viewIdInListFlag ) 
1617    {
1618      viewIdList   [ numViewIds ] = currViewId;
1619      viewIndexList[ currViewId ] = numViewIds;
1620
1621      numViewIds++;
1622    } 
1623
1624    m_viewIndex[ i ] = viewIndexList[ currViewId ]; 
1625  }
1626}
1627
1628Int TComVPS::getLayerIdInNuh( Int viewIndex, Bool depthFlag )
1629{
1630  Int foundlayerId = -1; 
1631
1632  for (Int layer = 0 ; layer < m_uiMaxLayers; layer++ )
1633  {
1634    if( ( getViewIndex( layer ) == viewIndex ) && ( getDepthId( layer ) == ( depthFlag ? 1 : 0 ) )  )
1635    {
1636      foundlayerId = layer; 
1637      break; 
1638    }
1639  }
1640  assert( foundlayerId != -1 ); 
1641
1642  return getLayerIdInNuh( foundlayerId );
1643}
1644#endif // H_3D
1645#endif // H_MV
1646
1647// ------------------------------------------------------------------------------------------------
1648// Sequence parameter set (SPS)
1649// ------------------------------------------------------------------------------------------------
1650
1651TComSPS::TComSPS()
1652: m_SPSId                     (  0)
1653, m_VPSId                     (  0)
1654, m_chromaFormatIdc           (CHROMA_420)
1655, m_uiMaxTLayers              (  1)
1656// Structure
1657, m_picWidthInLumaSamples     (352)
1658, m_picHeightInLumaSamples    (288)
1659, m_log2MinCodingBlockSize    (  0)
1660, m_log2DiffMaxMinCodingBlockSize (0)
1661, m_uiMaxCUWidth              ( 32)
1662, m_uiMaxCUHeight             ( 32)
1663, m_uiMaxCUDepth              (  3)
1664, m_bLongTermRefsPresent      (false)
1665, m_uiQuadtreeTULog2MaxSize   (  0)
1666, m_uiQuadtreeTULog2MinSize   (  0)
1667, m_uiQuadtreeTUMaxDepthInter (  0)
1668, m_uiQuadtreeTUMaxDepthIntra (  0)
1669// Tool list
1670, m_usePCM                   (false)
1671, m_pcmLog2MaxSize            (  5)
1672, m_uiPCMLog2MinSize          (  7)
1673#if !L0034_COMBINED_LIST_CLEANUP
1674, m_bUseLComb                 (false)
1675#endif
1676, m_bitDepthY                 (  8)
1677, m_bitDepthC                 (  8)
1678, m_qpBDOffsetY               (  0)
1679, m_qpBDOffsetC               (  0)
1680, m_useLossless               (false)
1681, m_uiPCMBitDepthLuma         (  8)
1682, m_uiPCMBitDepthChroma       (  8)
1683, m_bPCMFilterDisableFlag     (false)
1684, m_uiBitsForPOC              (  8)
1685, m_numLongTermRefPicSPS    (  0) 
1686, m_uiMaxTrSize               ( 32)
1687, m_bUseSAO                   (false) 
1688, m_bTemporalIdNestingFlag    (false)
1689, m_scalingListEnabledFlag    (false)
1690, m_useStrongIntraSmoothing   (false)
1691, m_vuiParametersPresentFlag  (false)
1692, m_vuiParameters             ()
1693#if H_MV
1694, m_interViewMvVertConstraintFlag (false)
1695#endif
1696#if H_3D
1697, m_bCamParInSliceHeader      (false)
1698#endif
1699{
1700  for ( Int i = 0; i < MAX_TLAYER; i++ )
1701  {
1702    m_uiMaxLatencyIncrease[i] = 0;
1703#if L0323_DPB
1704    m_uiMaxDecPicBuffering[i] = 1;
1705#else
1706    m_uiMaxDecPicBuffering[i] = 0;
1707#endif
1708    m_numReorderPics[i]       = 0;
1709  }
1710  m_scalingList = new TComScalingList;
1711  ::memset(m_ltRefPicPocLsbSps, 0, sizeof(m_ltRefPicPocLsbSps));
1712  ::memset(m_usedByCurrPicLtSPSFlag, 0, sizeof(m_usedByCurrPicLtSPSFlag));
1713}
1714
1715TComSPS::~TComSPS()
1716{
1717  delete m_scalingList;
1718  m_RPSList.destroy();
1719}
1720
1721Void  TComSPS::createRPSList( Int numRPS )
1722{ 
1723  m_RPSList.destroy();
1724  m_RPSList.create(numRPS);
1725}
1726
1727Void TComSPS::setHrdParameters( UInt frameRate, UInt numDU, UInt bitRate, Bool randomAccess )
1728{
1729  if( !getVuiParametersPresentFlag() )
1730  {
1731    return;
1732  }
1733
1734  TComVUI *vui = getVuiParameters();
1735  TComHRD *hrd = vui->getHrdParameters();
1736
1737#if L0043_TIMING_INFO
1738  TimingInfo *timingInfo = vui->getTimingInfo();
1739  timingInfo->setTimingInfoPresentFlag( true );
1740  switch( frameRate )
1741  {
1742  case 24:
1743    timingInfo->setNumUnitsInTick( 1125000 );    timingInfo->setTimeScale    ( 27000000 );
1744    break;
1745  case 25:
1746    timingInfo->setNumUnitsInTick( 1080000 );    timingInfo->setTimeScale    ( 27000000 );
1747    break;
1748  case 30:
1749    timingInfo->setNumUnitsInTick( 900900 );     timingInfo->setTimeScale    ( 27000000 );
1750    break;
1751  case 50:
1752    timingInfo->setNumUnitsInTick( 540000 );     timingInfo->setTimeScale    ( 27000000 );
1753    break;
1754  case 60:
1755    timingInfo->setNumUnitsInTick( 450450 );     timingInfo->setTimeScale    ( 27000000 );
1756    break;
1757  default:
1758    timingInfo->setNumUnitsInTick( 1001 );       timingInfo->setTimeScale    ( 60000 );
1759    break;
1760  }
1761#else
1762  hrd->setTimingInfoPresentFlag( true );
1763  switch( frameRate )
1764  {
1765  case 24:
1766    hrd->setNumUnitsInTick( 1125000 );    hrd->setTimeScale    ( 27000000 );
1767    break;
1768  case 25:
1769    hrd->setNumUnitsInTick( 1080000 );    hrd->setTimeScale    ( 27000000 );
1770    break;
1771  case 30:
1772    hrd->setNumUnitsInTick( 900900 );     hrd->setTimeScale    ( 27000000 );
1773    break;
1774  case 50:
1775    hrd->setNumUnitsInTick( 540000 );     hrd->setTimeScale    ( 27000000 );
1776    break;
1777  case 60:
1778    hrd->setNumUnitsInTick( 450450 );     hrd->setTimeScale    ( 27000000 );
1779    break;
1780  default:
1781    hrd->setNumUnitsInTick( 1001 );       hrd->setTimeScale    ( 60000 );
1782    break;
1783  }
1784#endif
1785
1786  Bool rateCnt = ( bitRate > 0 );
1787  hrd->setNalHrdParametersPresentFlag( rateCnt );
1788  hrd->setVclHrdParametersPresentFlag( rateCnt );
1789
1790  hrd->setSubPicCpbParamsPresentFlag( ( numDU > 1 ) );
1791
1792  if( hrd->getSubPicCpbParamsPresentFlag() )
1793  {
1794    hrd->setTickDivisorMinus2( 100 - 2 );                          //
1795    hrd->setDuCpbRemovalDelayLengthMinus1( 7 );                    // 8-bit precision ( plus 1 for last DU in AU )
1796    hrd->setSubPicCpbParamsInPicTimingSEIFlag( true );
1797#if L0044_DU_DPB_OUTPUT_DELAY_HRD
1798    hrd->setDpbOutputDelayDuLengthMinus1( 5 + 7 );                 // With sub-clock tick factor of 100, at least 7 bits to have the same value as AU dpb delay
1799#endif
1800  }
1801  else
1802  {
1803    hrd->setSubPicCpbParamsInPicTimingSEIFlag( false ); 
1804  }
1805
1806  hrd->setBitRateScale( 4 );                                       // in units of 2~( 6 + 4 ) = 1,024 bps
1807  hrd->setCpbSizeScale( 6 );                                       // in units of 2~( 4 + 4 ) = 1,024 bit
1808  hrd->setDuCpbSizeScale( 6 );                                       // in units of 2~( 4 + 4 ) = 1,024 bit
1809 
1810  hrd->setInitialCpbRemovalDelayLengthMinus1(15);                  // assuming 0.5 sec, log2( 90,000 * 0.5 ) = 16-bit
1811  if( randomAccess )
1812  {
1813    hrd->setCpbRemovalDelayLengthMinus1(5);                        // 32 = 2^5 (plus 1)
1814    hrd->setDpbOutputDelayLengthMinus1 (5);                        // 32 + 3 = 2^6
1815  }
1816  else
1817  {
1818    hrd->setCpbRemovalDelayLengthMinus1(9);                        // max. 2^10
1819    hrd->setDpbOutputDelayLengthMinus1 (9);                        // max. 2^10
1820  }
1821
1822/*
1823   Note: only the case of "vps_max_temporal_layers_minus1 = 0" is supported.
1824*/
1825  Int i, j;
1826  UInt birateValue, cpbSizeValue;
1827  UInt ducpbSizeValue;
1828#if L0363_DU_BIT_RATE
1829  UInt duBitRateValue = 0;
1830#endif
1831
1832  for( i = 0; i < MAX_TLAYER; i ++ )
1833  {
1834    hrd->setFixedPicRateFlag( i, 1 );
1835    hrd->setPicDurationInTcMinus1( i, 0 );
1836    hrd->setLowDelayHrdFlag( i, 0 );
1837    hrd->setCpbCntMinus1( i, 0 );
1838
1839    birateValue  = bitRate;
1840    cpbSizeValue = bitRate;                                     // 1 second
1841    ducpbSizeValue = bitRate/numDU;
1842#if L0363_DU_BIT_RATE
1843    duBitRateValue = bitRate;
1844#endif
1845    for( j = 0; j < ( hrd->getCpbCntMinus1( i ) + 1 ); j ++ )
1846    {
1847      hrd->setBitRateValueMinus1( i, j, 0, ( birateValue  - 1 ) );
1848      hrd->setCpbSizeValueMinus1( i, j, 0, ( cpbSizeValue - 1 ) );
1849      hrd->setDuCpbSizeValueMinus1( i, j, 0, ( ducpbSizeValue - 1 ) );
1850      hrd->setCbrFlag( i, j, 0, ( j == 0 ) );
1851
1852      hrd->setBitRateValueMinus1( i, j, 1, ( birateValue  - 1) );
1853      hrd->setCpbSizeValueMinus1( i, j, 1, ( cpbSizeValue - 1 ) );
1854      hrd->setDuCpbSizeValueMinus1( i, j, 1, ( ducpbSizeValue - 1 ) );
1855#if L0363_DU_BIT_RATE
1856      hrd->setDuBitRateValueMinus1( i, j, 1, ( duBitRateValue - 1 ) );
1857#endif
1858      hrd->setCbrFlag( i, j, 1, ( j == 0 ) );
1859    }
1860  }
1861}
1862const Int TComSPS::m_winUnitX[]={1,2,2,1};
1863const Int TComSPS::m_winUnitY[]={1,2,1,1};
1864
1865TComPPS::TComPPS()
1866: m_PPSId                       (0)
1867, m_SPSId                       (0)
1868, m_picInitQPMinus26            (0)
1869, m_useDQP                      (false)
1870, m_bConstrainedIntraPred       (false)
1871, m_bSliceChromaQpFlag          (false)
1872, m_pcSPS                       (NULL)
1873, m_uiMaxCuDQPDepth             (0)
1874, m_uiMinCuDQPSize              (0)
1875, m_chromaCbQpOffset            (0)
1876, m_chromaCrQpOffset            (0)
1877, m_numRefIdxL0DefaultActive    (1)
1878, m_numRefIdxL1DefaultActive    (1)
1879, m_TransquantBypassEnableFlag  (false)
1880, m_useTransformSkip             (false)
1881, m_dependentSliceSegmentsEnabledFlag    (false)
1882, m_tilesEnabledFlag               (false)
1883, m_entropyCodingSyncEnabledFlag   (false)
1884, m_loopFilterAcrossTilesEnabledFlag  (true)
1885, m_uniformSpacingFlag           (0)
1886, m_iNumColumnsMinus1            (0)
1887, m_puiColumnWidth               (NULL)
1888, m_iNumRowsMinus1               (0)
1889, m_puiRowHeight                 (NULL)
1890, m_iNumSubstreams             (1)
1891, m_signHideFlag(0)
1892, m_cabacInitPresentFlag        (false)
1893, m_encCABACTableIdx            (I_SLICE)
1894, m_sliceHeaderExtensionPresentFlag    (false)
1895, m_loopFilterAcrossSlicesEnabledFlag (false)
1896, m_listsModificationPresentFlag(  0)
1897, m_numExtraSliceHeaderBits(0)
1898{
1899  m_scalingList = new TComScalingList;
1900}
1901
1902TComPPS::~TComPPS()
1903{
1904  if( m_iNumColumnsMinus1 > 0 && m_uniformSpacingFlag == 0 )
1905  {
1906    if (m_puiColumnWidth) delete [] m_puiColumnWidth; 
1907    m_puiColumnWidth = NULL;
1908  }
1909  if( m_iNumRowsMinus1 > 0 && m_uniformSpacingFlag == 0 )
1910  {
1911    if (m_puiRowHeight) delete [] m_puiRowHeight;
1912    m_puiRowHeight = NULL;
1913  }
1914  delete m_scalingList;
1915}
1916
1917#if H_3D
1918Void
1919TComSPS::initCamParaSPS( UInt uiViewIndex, UInt uiCamParPrecision, Bool bCamParSlice, Int** aaiScale, Int** aaiOffset )
1920{
1921  AOT( uiViewIndex != 0 && !bCamParSlice && ( aaiScale == 0 || aaiOffset == 0 ) ); 
1922 
1923  m_uiCamParPrecision     = ( uiViewIndex ? uiCamParPrecision : 0 );
1924  m_bCamParInSliceHeader  = ( uiViewIndex ? bCamParSlice  : false );
1925  ::memset( m_aaiCodedScale,  0x00, sizeof( m_aaiCodedScale  ) );
1926  ::memset( m_aaiCodedOffset, 0x00, sizeof( m_aaiCodedOffset ) );
1927
1928  if( !m_bCamParInSliceHeader )
1929  {
1930    for( UInt uiBaseViewIndex = 0; uiBaseViewIndex < uiViewIndex; uiBaseViewIndex++ )
1931    {
1932      m_aaiCodedScale [ 0 ][ uiBaseViewIndex ] = aaiScale [ uiBaseViewIndex ][     uiViewIndex ];
1933      m_aaiCodedScale [ 1 ][ uiBaseViewIndex ] = aaiScale [     uiViewIndex ][ uiBaseViewIndex ];
1934      m_aaiCodedOffset[ 0 ][ uiBaseViewIndex ] = aaiOffset[ uiBaseViewIndex ][     uiViewIndex ];
1935      m_aaiCodedOffset[ 1 ][ uiBaseViewIndex ] = aaiOffset[     uiViewIndex ][ uiBaseViewIndex ];
1936    }
1937  }
1938}
1939#endif
1940
1941TComReferencePictureSet::TComReferencePictureSet()
1942: m_numberOfPictures (0)
1943, m_numberOfNegativePictures (0)
1944, m_numberOfPositivePictures (0)
1945, m_numberOfLongtermPictures (0)
1946, m_interRPSPrediction (0) 
1947, m_deltaRIdxMinus1 (0)   
1948, m_deltaRPS (0) 
1949, m_numRefIdc (0) 
1950{
1951  ::memset( m_deltaPOC, 0, sizeof(m_deltaPOC) );
1952  ::memset( m_POC, 0, sizeof(m_POC) );
1953  ::memset( m_used, 0, sizeof(m_used) );
1954  ::memset( m_refIdc, 0, sizeof(m_refIdc) );
1955}
1956
1957TComReferencePictureSet::~TComReferencePictureSet()
1958{
1959}
1960
1961Void TComReferencePictureSet::setUsed(Int bufferNum, Bool used)
1962{
1963  m_used[bufferNum] = used;
1964}
1965
1966Void TComReferencePictureSet::setDeltaPOC(Int bufferNum, Int deltaPOC)
1967{
1968  m_deltaPOC[bufferNum] = deltaPOC;
1969}
1970
1971Void TComReferencePictureSet::setNumberOfPictures(Int numberOfPictures)
1972{
1973  m_numberOfPictures = numberOfPictures;
1974}
1975
1976Int TComReferencePictureSet::getUsed(Int bufferNum)
1977{
1978  return m_used[bufferNum];
1979}
1980
1981Int TComReferencePictureSet::getDeltaPOC(Int bufferNum)
1982{
1983  return m_deltaPOC[bufferNum];
1984}
1985
1986Int TComReferencePictureSet::getNumberOfPictures()
1987{
1988  return m_numberOfPictures;
1989}
1990
1991Int TComReferencePictureSet::getPOC(Int bufferNum)
1992{
1993  return m_POC[bufferNum];
1994}
1995
1996Void TComReferencePictureSet::setPOC(Int bufferNum, Int POC)
1997{
1998  m_POC[bufferNum] = POC;
1999}
2000
2001Bool TComReferencePictureSet::getCheckLTMSBPresent(Int bufferNum)
2002{
2003  return m_bCheckLTMSB[bufferNum];
2004}
2005
2006Void TComReferencePictureSet::setCheckLTMSBPresent(Int bufferNum, Bool b)
2007{
2008  m_bCheckLTMSB[bufferNum] = b;
2009}
2010
2011/** set the reference idc value at uiBufferNum entry to the value of iRefIdc
2012 * \param uiBufferNum
2013 * \param iRefIdc
2014 * \returns Void
2015 */
2016Void TComReferencePictureSet::setRefIdc(Int bufferNum, Int refIdc)
2017{
2018  m_refIdc[bufferNum] = refIdc;
2019}
2020
2021/** get the reference idc value at uiBufferNum
2022 * \param uiBufferNum
2023 * \returns Int
2024 */
2025Int  TComReferencePictureSet::getRefIdc(Int bufferNum)
2026{
2027  return m_refIdc[bufferNum];
2028}
2029
2030/** Sorts the deltaPOC and Used by current values in the RPS based on the deltaPOC values.
2031 *  deltaPOC values are sorted with -ve values before the +ve values.  -ve values are in decreasing order.
2032 *  +ve values are in increasing order.
2033 * \returns Void
2034 */
2035Void TComReferencePictureSet::sortDeltaPOC()
2036{
2037  // sort in increasing order (smallest first)
2038  for(Int j=1; j < getNumberOfPictures(); j++)
2039  { 
2040    Int deltaPOC = getDeltaPOC(j);
2041    Bool used = getUsed(j);
2042    for (Int k=j-1; k >= 0; k--)
2043    {
2044      Int temp = getDeltaPOC(k);
2045      if (deltaPOC < temp)
2046      {
2047        setDeltaPOC(k+1, temp);
2048        setUsed(k+1, getUsed(k));
2049        setDeltaPOC(k, deltaPOC);
2050        setUsed(k, used);
2051      }
2052    }
2053  }
2054  // flip the negative values to largest first
2055  Int numNegPics = getNumberOfNegativePictures();
2056  for(Int j=0, k=numNegPics-1; j < numNegPics>>1; j++, k--)
2057  { 
2058    Int deltaPOC = getDeltaPOC(j);
2059    Bool used = getUsed(j);
2060    setDeltaPOC(j, getDeltaPOC(k));
2061    setUsed(j, getUsed(k));
2062    setDeltaPOC(k, deltaPOC);
2063    setUsed(k, used);
2064  }
2065}
2066
2067/** Prints the deltaPOC and RefIdc (if available) values in the RPS.
2068 *  A "*" is added to the deltaPOC value if it is Used bu current.
2069 * \returns Void
2070 */
2071Void TComReferencePictureSet::printDeltaPOC()
2072{
2073  printf("DeltaPOC = { ");
2074  for(Int j=0; j < getNumberOfPictures(); j++)
2075  {
2076    printf("%d%s ", getDeltaPOC(j), (getUsed(j)==1)?"*":"");
2077  } 
2078  if (getInterRPSPrediction()) 
2079  {
2080    printf("}, RefIdc = { ");
2081    for(Int j=0; j < getNumRefIdc(); j++)
2082    {
2083      printf("%d ", getRefIdc(j));
2084    } 
2085  }
2086  printf("}\n");
2087}
2088
2089TComRPSList::TComRPSList()
2090:m_referencePictureSets (NULL)
2091{
2092}
2093
2094TComRPSList::~TComRPSList()
2095{
2096}
2097
2098Void TComRPSList::create( Int numberOfReferencePictureSets)
2099{
2100  m_numberOfReferencePictureSets = numberOfReferencePictureSets;
2101  m_referencePictureSets = new TComReferencePictureSet[numberOfReferencePictureSets];
2102}
2103
2104Void TComRPSList::destroy()
2105{
2106  if (m_referencePictureSets)
2107  {
2108    delete [] m_referencePictureSets;
2109  }
2110  m_numberOfReferencePictureSets = 0;
2111  m_referencePictureSets = NULL;
2112}
2113
2114
2115
2116TComReferencePictureSet* TComRPSList::getReferencePictureSet(Int referencePictureSetNum)
2117{
2118  return &m_referencePictureSets[referencePictureSetNum];
2119}
2120
2121Int TComRPSList::getNumberOfReferencePictureSets()
2122{
2123  return m_numberOfReferencePictureSets;
2124}
2125
2126Void TComRPSList::setNumberOfReferencePictureSets(Int numberOfReferencePictureSets)
2127{
2128  m_numberOfReferencePictureSets = numberOfReferencePictureSets;
2129}
2130
2131TComRefPicListModification::TComRefPicListModification()
2132: m_bRefPicListModificationFlagL0 (false)
2133, m_bRefPicListModificationFlagL1 (false)
2134{
2135  ::memset( m_RefPicSetIdxL0, 0, sizeof(m_RefPicSetIdxL0) );
2136  ::memset( m_RefPicSetIdxL1, 0, sizeof(m_RefPicSetIdxL1) );
2137}
2138
2139TComRefPicListModification::~TComRefPicListModification()
2140{
2141}
2142
2143TComScalingList::TComScalingList()
2144{
2145  m_useTransformSkip = false;
2146  init();
2147}
2148TComScalingList::~TComScalingList()
2149{
2150  destroy();
2151}
2152
2153/** set default quantization matrix to array
2154*/
2155Void TComSlice::setDefaultScalingList()
2156{
2157  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
2158  {
2159    for(UInt listId=0;listId<g_scalingListNum[sizeId];listId++)
2160    {
2161      getScalingList()->processDefaultMarix(sizeId, listId);
2162    }
2163  }
2164}
2165/** check if use default quantization matrix
2166 * \returns true if use default quantization matrix in all size
2167*/
2168Bool TComSlice::checkDefaultScalingList()
2169{
2170  UInt defaultCounter=0;
2171
2172  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
2173  {
2174    for(UInt listId=0;listId<g_scalingListNum[sizeId];listId++)
2175    {
2176      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
2177     && ((sizeId < SCALING_LIST_16x16) || (getScalingList()->getScalingListDC(sizeId,listId) == 16))) // check DC value
2178      {
2179        defaultCounter++;
2180      }
2181    }
2182  }
2183  return (defaultCounter == (SCALING_LIST_NUM * SCALING_LIST_SIZE_NUM - 4)) ? false : true; // -4 for 32x32
2184}
2185
2186#if H_MV
2187Void TComSlice::createAndApplyIvReferencePictureSet( TComPicLists* ivPicLists, std::vector<TComPic*>& refPicSetInterLayer )
2188{
2189  refPicSetInterLayer.clear(); 
2190
2191  for( Int i = 0; i < getVPS()->getNumDirectRefLayers( getLayerIdInVps() ); i++ ) 
2192  {
2193    Int layerIdRef = getVPS()->getRefLayerId( getLayerIdInVps(), i ); 
2194    TComPic* picRef = ivPicLists->getPic( layerIdRef, getPOC() ) ; 
2195    assert ( picRef != 0 ); 
2196
2197    picRef->getPicYuvRec()->extendPicBorder(); 
2198    picRef->setIsLongTerm( true );       
2199    picRef->getSlice(0)->setReferenced( true );       
2200
2201    refPicSetInterLayer.push_back( picRef ); 
2202  }
2203}
2204
2205Void TComSlice::markIvRefPicsAsShortTerm( std::vector<TComPic*> refPicSetInterLayer )
2206{
2207  // Mark as shortterm
2208  for ( Int i = 0; i < refPicSetInterLayer.size(); i++ ) 
2209  {
2210    refPicSetInterLayer[i]->setIsLongTerm( false ); 
2211  }
2212}
2213
2214Void TComSlice::markIvRefPicsAsUnused( TComPicLists* ivPicLists, std::vector<Int> targetDecLayerIdSet, TComVPS* vps, Int curLayerId, Int curPoc )
2215{
2216  // Fill targetDecLayerIdSet with all layers if empty.
2217  if (targetDecLayerIdSet.size() == 0 )   
2218  {
2219    for ( Int layerIdInVps = 0; layerIdInVps < vps->getMaxLayers(); layerIdInVps++ )
2220    {
2221      targetDecLayerIdSet.push_back( vps->getLayerIdInNuh( layerIdInVps ) ); 
2222    }
2223  }     
2224
2225  Int numTargetDecLayers = (Int) targetDecLayerIdSet.size(); 
2226  Int latestDecIdx; 
2227  for ( latestDecIdx = 0; latestDecIdx < numTargetDecLayers; latestDecIdx++)
2228  {
2229    if ( targetDecLayerIdSet[ latestDecIdx ] == curLayerId )
2230      break; 
2231  }       
2232
2233  for( Int i = 0; i <= latestDecIdx; i++ ) 
2234  {
2235    if ( vps->nuhLayerIdIncluded( targetDecLayerIdSet[ i ] ) )
2236    {
2237      TComPic* pcPic = ivPicLists->getPic( targetDecLayerIdSet[ i ], curPoc ); 
2238      if( pcPic->getSlice(0)->isReferenced() && pcPic->getSlice(0)->getTemporalLayerNonReferenceFlag() ) 
2239      { 
2240        Bool remainingInterLayerReferencesFlag = false; 
2241        for( Int j = latestDecIdx + 1; j < numTargetDecLayers; j++ )
2242        { 
2243          TComVPS* vpsSlice = pcPic->getSlice(0)->getVPS(); 
2244          if ( vps->nuhLayerIdIncluded( targetDecLayerIdSet[ j ] ) )
2245          {
2246            Int targetDecLayerIdinVPS = vpsSlice->getLayerIdInVps( targetDecLayerIdSet[ j ] ); 
2247            for( Int k = 0; k < vpsSlice->getNumDirectRefLayers( targetDecLayerIdinVPS ); k++ )
2248              if ( targetDecLayerIdSet[ i ] == vpsSlice->getRefLayerId( targetDecLayerIdinVPS,  k  ) )
2249                remainingInterLayerReferencesFlag = true;
2250          }
2251        }
2252        if( !remainingInterLayerReferencesFlag )
2253          pcPic->getSlice(0)->setReferenced( false );                   
2254      }
2255    }
2256  }
2257}
2258
2259Void TComSlice::xPrintRefPicList()
2260{ 
2261  for ( Int li = 0; li < 2; li++)
2262  {   
2263    std::cout << std::endl << "RefPicListL" <<  li << ":" << std::endl; 
2264    for (Int rIdx = 0; rIdx <= (m_aiNumRefIdx[li]-1); rIdx ++)
2265    {     
2266      if (rIdx == 0 && li == 0) m_apcRefPicList[li][rIdx]->print( true );
2267       
2268      m_apcRefPicList[li][rIdx]->print( false );
2269    }
2270  }
2271}
2272
2273#if H_3D_ARP
2274Void TComSlice::setARPStepNum()                                 
2275{
2276  Bool bAllIvRef = true;
2277
2278  if(!getVPS()->getUseAdvRP(getLayerId()))
2279  {
2280    m_nARPStepNum = 0;
2281  }
2282  else
2283  {
2284    for( Int iRefListId = 0; iRefListId < 2; iRefListId++ )
2285    {
2286      RefPicList  eRefPicList = RefPicList( iRefListId );
2287      Int iNumRefIdx = getNumRefIdx(eRefPicList);
2288     
2289      if( iNumRefIdx <= 0 )
2290      {
2291        continue;
2292      }
2293
2294      for ( Int i = 0; i < iNumRefIdx; i++ )
2295      {
2296        if( getRefPic( eRefPicList, i)->getPOC() != getPOC() )
2297        {
2298          bAllIvRef = false;
2299          break;
2300        }
2301      }
2302
2303      if( bAllIvRef == false ) { break; }
2304    }
2305    m_nARPStepNum = !bAllIvRef ? getVPS()->getARPStepNum(getLayerId()) : 0;
2306  }
2307}
2308#endif
2309#endif
2310
2311#if H_3D_IC
2312Void TComSlice::xSetApplyIC()
2313{
2314  Int iMaxPelValue = ( 1 << g_bitDepthY ); 
2315  Int *aiRefOrgHist;
2316  Int *aiCurrHist;
2317  aiRefOrgHist = new Int;
2318  aiCurrHist   = new Int;
2319  aiRefOrgHist = (Int *) xMalloc( Int,iMaxPelValue );
2320  aiCurrHist   = (Int *) xMalloc( Int,iMaxPelValue );
2321  memset( aiRefOrgHist, 0, iMaxPelValue*sizeof(Int) );
2322  memset( aiCurrHist, 0, iMaxPelValue*sizeof(Int) );
2323  // Reference Idx Number
2324  Int iNumRefIdx = getNumRefIdx( REF_PIC_LIST_0 );
2325  TComPic* pcCurrPic = NULL;
2326  TComPic* pcRefPic = NULL;
2327  TComPicYuv* pcCurrPicYuv = NULL;
2328  TComPicYuv* pcRefPicYuvOrg = NULL;
2329  pcCurrPic = getPic();
2330  pcCurrPicYuv = pcCurrPic->getPicYuvOrg();
2331  Int iWidth = pcCurrPicYuv->getWidth();
2332  Int iHeight = pcCurrPicYuv->getHeight();
2333
2334
2335  // Get InterView Reference picture
2336  // !!!!! Assume only one Interview Reference Picture in L0
2337  for ( Int i = 0; i < iNumRefIdx; i++ )
2338  {
2339    pcRefPic = getRefPic( REF_PIC_LIST_0, i );
2340    if ( pcRefPic != NULL )
2341    {
2342      if ( pcCurrPic->getViewIndex() != pcRefPic->getViewIndex() )
2343      {
2344        pcRefPicYuvOrg = pcRefPic->getPicYuvOrg();
2345      }
2346    }
2347  }
2348
2349  if ( pcRefPicYuvOrg != NULL )
2350  {
2351    Pel* pCurrY = pcCurrPicYuv ->getLumaAddr();
2352    Pel* pRefOrgY = pcRefPicYuvOrg  ->getLumaAddr();
2353    Int iCurrStride = pcCurrPicYuv->getStride();
2354    Int iRefStride = pcRefPicYuvOrg->getStride();
2355    Int iSumOrgSAD = 0;
2356    Double dThresholdOrgSAD = getIsDepth() ? 0.1 : 0.05;
2357
2358    // Histogram building - luminance
2359    for ( Int y = 0; y < iHeight; y++ )
2360    {
2361      for ( Int x = 0; x < iWidth; x++ )
2362      {
2363        aiCurrHist[pCurrY[x]]++;
2364        aiRefOrgHist[pRefOrgY[x]]++;
2365      }
2366      pCurrY += iCurrStride;
2367      pRefOrgY += iRefStride;
2368    }
2369    // Histogram SAD
2370    for ( Int i = 0; i < iMaxPelValue; i++ )
2371    {
2372      iSumOrgSAD += abs( aiCurrHist[i] - aiRefOrgHist[i] );
2373    }
2374    // Setting
2375    if ( iSumOrgSAD > Int( dThresholdOrgSAD * iWidth * iHeight ) )
2376    {
2377      m_bApplyIC = true;
2378    }
2379    else
2380    {
2381      m_bApplyIC = false;
2382    }
2383  }
2384
2385  xFree( aiCurrHist   );
2386  xFree( aiRefOrgHist );
2387  aiCurrHist = NULL;
2388  aiRefOrgHist = NULL;
2389}
2390#endif
2391
2392/** get scaling matrix from RefMatrixID
2393 * \param sizeId size index
2394 * \param Index of input matrix
2395 * \param Index of reference matrix
2396 */
2397Void TComScalingList::processRefMatrix( UInt sizeId, UInt listId , UInt refListId )
2398{
2399  ::memcpy(getScalingListAddress(sizeId, listId),((listId == refListId)? getScalingListDefaultAddress(sizeId, refListId): getScalingListAddress(sizeId, refListId)),sizeof(Int)*min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId]));
2400}
2401
2402/** parse syntax infomation
2403 *  \param pchFile syntax infomation
2404 *  \returns false if successful
2405 */
2406Bool TComScalingList::xParseScalingList(Char* pchFile)
2407{
2408  FILE *fp;
2409  Char line[1024];
2410  UInt sizeIdc,listIdc;
2411  UInt i,size = 0;
2412  Int *src=0,data;
2413  Char *ret;
2414  UInt  retval;
2415
2416  if((fp = fopen(pchFile,"r")) == (FILE*)NULL)
2417  {
2418    printf("can't open file %s :: set Default Matrix\n",pchFile);
2419    return true;
2420  }
2421
2422  for(sizeIdc = 0; sizeIdc < SCALING_LIST_SIZE_NUM; sizeIdc++)
2423  {
2424    size = min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeIdc]);
2425    for(listIdc = 0; listIdc < g_scalingListNum[sizeIdc]; listIdc++)
2426    {
2427      src = getScalingListAddress(sizeIdc, listIdc);
2428
2429      fseek(fp,0,0);
2430      do 
2431      {
2432        ret = fgets(line, 1024, fp);
2433        if ((ret==NULL)||(strstr(line, MatrixType[sizeIdc][listIdc])==NULL && feof(fp)))
2434        {
2435          printf("Error: can't read Matrix :: set Default Matrix\n");
2436          return true;
2437        }
2438      }
2439      while (strstr(line, MatrixType[sizeIdc][listIdc]) == NULL);
2440      for (i=0; i<size; i++)
2441      {
2442        retval = fscanf(fp, "%d,", &data);
2443        if (retval!=1)
2444        {
2445          printf("Error: can't read Matrix :: set Default Matrix\n");
2446          return true;
2447        }
2448        src[i] = data;
2449      }
2450      //set DC value for default matrix check
2451      setScalingListDC(sizeIdc,listIdc,src[0]);
2452
2453      if(sizeIdc > SCALING_LIST_8x8)
2454      {
2455        fseek(fp,0,0);
2456        do 
2457        {
2458          ret = fgets(line, 1024, fp);
2459          if ((ret==NULL)||(strstr(line, MatrixType_DC[sizeIdc][listIdc])==NULL && feof(fp)))
2460          {
2461            printf("Error: can't read DC :: set Default Matrix\n");
2462            return true;
2463          }
2464        }
2465        while (strstr(line, MatrixType_DC[sizeIdc][listIdc]) == NULL);
2466        retval = fscanf(fp, "%d,", &data);
2467        if (retval!=1)
2468        {
2469          printf("Error: can't read Matrix :: set Default Matrix\n");
2470          return true;
2471        }
2472        //overwrite DC value when size of matrix is larger than 16x16
2473        setScalingListDC(sizeIdc,listIdc,data);
2474      }
2475    }
2476  }
2477  fclose(fp);
2478  return false;
2479}
2480
2481/** initialization process of quantization matrix array
2482 */
2483Void TComScalingList::init()
2484{
2485  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
2486  {
2487    for(UInt listId = 0; listId < g_scalingListNum[sizeId]; listId++)
2488    {
2489      m_scalingListCoef[sizeId][listId] = new Int [min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId])];
2490    }
2491  }
2492  m_scalingListCoef[SCALING_LIST_32x32][3] = m_scalingListCoef[SCALING_LIST_32x32][1]; // copy address for 32x32
2493}
2494
2495/** destroy quantization matrix array
2496 */
2497Void TComScalingList::destroy()
2498{
2499  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
2500  {
2501    for(UInt listId = 0; listId < g_scalingListNum[sizeId]; listId++)
2502    {
2503      if(m_scalingListCoef[sizeId][listId]) delete [] m_scalingListCoef[sizeId][listId];
2504    }
2505  }
2506}
2507
2508/** get default address of quantization matrix
2509 * \param sizeId size index
2510 * \param listId list index
2511 * \returns pointer of quantization matrix
2512 */
2513Int* TComScalingList::getScalingListDefaultAddress(UInt sizeId, UInt listId)
2514{
2515  Int *src = 0;
2516  switch(sizeId)
2517  {
2518    case SCALING_LIST_4x4:
2519      src = g_quantTSDefault4x4;
2520      break;
2521    case SCALING_LIST_8x8:
2522      src = (listId<3) ? g_quantIntraDefault8x8 : g_quantInterDefault8x8;
2523      break;
2524    case SCALING_LIST_16x16:
2525      src = (listId<3) ? g_quantIntraDefault8x8 : g_quantInterDefault8x8;
2526      break;
2527    case SCALING_LIST_32x32:
2528      src = (listId<1) ? g_quantIntraDefault8x8 : g_quantInterDefault8x8;
2529      break;
2530    default:
2531      assert(0);
2532      src = NULL;
2533      break;
2534  }
2535  return src;
2536}
2537
2538/** process of default matrix
2539 * \param sizeId size index
2540 * \param Index of input matrix
2541 */
2542Void TComScalingList::processDefaultMarix(UInt sizeId, UInt listId)
2543{
2544  ::memcpy(getScalingListAddress(sizeId, listId),getScalingListDefaultAddress(sizeId,listId),sizeof(Int)*min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId]));
2545  setScalingListDC(sizeId,listId,SCALING_LIST_DC);
2546}
2547
2548/** check DC value of matrix for default matrix signaling
2549 */
2550Void TComScalingList::checkDcOfMatrix()
2551{
2552  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
2553  {
2554    for(UInt listId = 0; listId < g_scalingListNum[sizeId]; listId++)
2555    {
2556      //check default matrix?
2557      if(getScalingListDC(sizeId,listId) == 0)
2558      {
2559        processDefaultMarix(sizeId, listId);
2560      }
2561    }
2562  }
2563}
2564
2565ParameterSetManager::ParameterSetManager()
2566: m_vpsMap(MAX_NUM_VPS)
2567, m_spsMap(MAX_NUM_SPS)
2568, m_ppsMap(MAX_NUM_PPS)
2569, m_activeVPSId(-1)
2570, m_activeSPSId(-1)
2571, m_activePPSId(-1)
2572{
2573}
2574
2575
2576ParameterSetManager::~ParameterSetManager()
2577{
2578}
2579
2580//! activate a SPS from a active parameter sets SEI message
2581//! \returns true, if activation is successful
2582Bool ParameterSetManager::activateSPSWithSEI(Int spsId)
2583{
2584  TComSPS *sps = m_spsMap.getPS(spsId);
2585  if (sps)
2586  {
2587    Int vpsId = sps->getVPSId();
2588    if (m_vpsMap.getPS(vpsId))
2589    {
2590      m_activeVPSId = vpsId;
2591      m_activeSPSId = spsId;
2592      return true;
2593    }
2594    else
2595    {
2596      printf("Warning: tried to activate SPS using an Active parameter sets SEI message. Referenced VPS does not exist.");
2597    }
2598  }
2599  else
2600  {
2601    printf("Warning: tried to activate non-existing SPS using an Active parameter sets SEI message.");
2602  }
2603  return false;
2604}
2605
2606//! activate a PPS and depending on isIDR parameter also SPS and VPS
2607//! \returns true, if activation is successful
2608Bool ParameterSetManager::activatePPS(Int ppsId, Bool isIRAP)
2609{
2610  TComPPS *pps = m_ppsMap.getPS(ppsId);
2611  if (pps)
2612  {
2613    Int spsId = pps->getSPSId();
2614#if H_MV
2615    // active parameter sets per layer should be used here
2616#else
2617    if (!isIRAP && (spsId != m_activeSPSId))
2618    {
2619      printf("Warning: tried to activate PPS referring to a inactive SPS at non-IRAP.");
2620      return false;
2621    }
2622#endif
2623    TComSPS *sps = m_spsMap.getPS(spsId);
2624    if (sps)
2625    {
2626      Int vpsId = sps->getVPSId();
2627      if (!isIRAP && (vpsId != m_activeVPSId))
2628      {
2629        printf("Warning: tried to activate PPS referring to a inactive VPS at non-IRAP.");
2630        return false;
2631      }
2632      if (m_vpsMap.getPS(vpsId))
2633      {
2634        m_activePPSId = ppsId;
2635        m_activeVPSId = vpsId;
2636        m_activeSPSId = spsId;
2637        return true;
2638      }
2639      else
2640      {
2641        printf("Warning: tried to activate PPS that refers to a non-existing VPS.");
2642      }
2643    }
2644    else
2645    {
2646      printf("Warning: tried to activate a PPS that refers to a non-existing SPS.");
2647    }
2648  }
2649  else
2650  {
2651    printf("Warning: tried to activate non-existing PPS.");
2652  }
2653  return false;
2654}
2655
2656ProfileTierLevel::ProfileTierLevel()
2657  : m_profileSpace    (0)
2658  , m_tierFlag        (false)
2659  , m_profileIdc      (0)
2660  , m_levelIdc        (0)
2661#if L0046_CONSTRAINT_FLAGS
2662, m_progressiveSourceFlag  (false)
2663, m_interlacedSourceFlag   (false)
2664, m_nonPackedConstraintFlag(false)
2665, m_frameOnlyConstraintFlag(false)
2666#endif
2667{
2668  ::memset(m_profileCompatibilityFlag, 0, sizeof(m_profileCompatibilityFlag));
2669}
2670
2671TComPTL::TComPTL()
2672{
2673  ::memset(m_subLayerProfilePresentFlag, 0, sizeof(m_subLayerProfilePresentFlag));
2674  ::memset(m_subLayerLevelPresentFlag,   0, sizeof(m_subLayerLevelPresentFlag  ));
2675}
2676
2677#if H_MV
2678Void TComPTL::copyLevelFrom( TComPTL* source )
2679{
2680  getGeneralPTL()->setLevelIdc( source->getGeneralPTL()->getLevelIdc() );
2681  for( Int subLayer = 0; subLayer < 6; subLayer++ )
2682  {
2683    setSubLayerLevelPresentFlag( subLayer, source->getSubLayerLevelPresentFlag( subLayer ) );
2684    getSubLayerPTL( subLayer )->setLevelIdc( source->getSubLayerPTL( subLayer )->getLevelIdc() );
2685  }
2686}
2687#endif
2688#if SIGNAL_BITRATE_PICRATE_IN_VPS
2689TComBitRatePicRateInfo::TComBitRatePicRateInfo()
2690{
2691  ::memset(m_bitRateInfoPresentFlag, 0, sizeof(m_bitRateInfoPresentFlag));
2692  ::memset(m_picRateInfoPresentFlag, 0, sizeof(m_picRateInfoPresentFlag));
2693  ::memset(m_avgBitRate,             0, sizeof(m_avgBitRate));
2694  ::memset(m_maxBitRate,             0, sizeof(m_maxBitRate));
2695  ::memset(m_constantPicRateIdc,     0, sizeof(m_constantPicRateIdc));
2696  ::memset(m_avgPicRate,             0, sizeof(m_avgPicRate));
2697}
2698#endif
2699//! \}
Note: See TracBrowser for help on using the repository browser.