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

Last change on this file since 464 was 464, checked in by zhang, 11 years ago

Implementation of ARP from QC

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