source: 3DVCSoftware/branches/HTM-9.1-dev0-ZTE/source/Lib/TLibCommon/TComSlice.cpp @ 748

Last change on this file since 748 was 748, checked in by zte, 10 years ago

JCT3V-F0131, JCT3V-F0139

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