source: 3DVCSoftware/branches/HTM-9.0-dev0/source/Lib/TLibCommon/TComSlice.cpp @ 730

Last change on this file since 730 was 730, checked in by tech, 11 years ago

MV HEVC 6 HLS changes.

  • Property svn:eol-style set to native
File size: 112.3 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    m_bUseDLTFlag         [i] = false;
1819   
1820    // allocate some memory and initialize with default mapping
1821    m_iNumDepthmapValues[i] = ((1 << g_bitDepthY)-1)+1;
1822    m_iBitsPerDepthValue[i] = numBitsForValue(m_iNumDepthmapValues[i]);
1823   
1824    m_iDepthValue2Idx[i]    = (Int*) xMalloc(Int, m_iNumDepthmapValues[i]);
1825    m_iIdx2DepthValue[i]    = (Int*) xMalloc(Int, m_iNumDepthmapValues[i]);
1826   
1827    //default mapping
1828    for (Int d=0; d<m_iNumDepthmapValues[i]; d++)
1829    {
1830      m_iDepthValue2Idx[i][d] = d;
1831      m_iIdx2DepthValue[i][d] = d;
1832    }
1833#endif
1834#if H_3D
1835    m_ivMvScalingFlag = true; 
1836#endif
1837#endif
1838
1839    for( Int j = 0; j < MAX_NUM_LAYERS; j++ )
1840    {
1841      m_directDependencyFlag[i][j] = false;
1842      m_directDependencyType[i][j] = -1; 
1843      m_refLayerId[i][j]           = -1; 
1844#if H_MV_6_ILDDS_O0225_30     
1845      m_maxTidIlRefPicsPlus1[i][j]  = 7;
1846#endif
1847    }
1848
1849    for( Int j = 0; j < MAX_NUM_SCALABILITY_TYPES; j++ )
1850    {
1851      m_dimensionId[i][j] = 0;
1852    }
1853#if H_3D_ARP
1854    m_uiUseAdvResPred[i]  = 0;
1855    m_uiARPStepNum[i]     = 1;
1856#endif
1857  }
1858  m_vpsVUI = new TComVPSVUI; 
1859#if H_MV_6_HRD_O0217_13
1860  m_dpbSize = new TComDpbSize; 
1861#endif
1862#
1863#if H_3D
1864  for( Int i = 0; i < MAX_NUM_LAYERS; i++ )
1865  {
1866#if H_3D_IV_MERGE
1867    m_ivMvPredFlag         [ i ] = false;
1868#if MTK_SPIVMP_F0110
1869    m_iSubPULog2Size       [ i ] = 0;
1870#endif
1871#endif
1872#if H_3D_VSP
1873    m_viewSynthesisPredFlag[ i ] = false;
1874#endif
1875#if H_3D_NBDV_REF
1876    m_depthRefinementFlag  [ i ] = false;
1877#endif
1878#if H_3D_INTER_SDC
1879    m_bInterSDCFlag        [ i ] = false;
1880#endif
1881#if SEC_MPI_ENABLING_MERGE_F0150
1882    m_bMPIFlag             [ i ] = false;
1883#endif
1884  } 
1885#endif
1886#endif
1887}
1888
1889TComVPS::~TComVPS()
1890{
1891if( m_hrdParameters    != NULL )     delete[] m_hrdParameters;
1892  if( m_hrdOpSetIdx      != NULL )     delete[] m_hrdOpSetIdx;
1893  if( m_cprmsPresentFlag != NULL )     delete[] m_cprmsPresentFlag;
1894#if H_MV
1895  if ( m_vpsVUI          != NULL )     delete m_vpsVUI; 
1896#if H_MV_6_HRD_O0217_13
1897  if ( m_dpbSize         != NULL )     delete m_dpbSize; 
1898#endif
1899
1900  for( Int i = 0; i < MAX_NUM_LAYERS; i++ )
1901  {
1902    if (m_repFormat[ i ] != NULL )      delete m_repFormat[ i ];   
1903#if H_3D_DIM_DLT
1904    if ( m_iDepthValue2Idx[i] != 0 ) 
1905    {
1906       xFree( m_iDepthValue2Idx[i] );
1907       m_iDepthValue2Idx[i] = 0; 
1908    }
1909
1910    if ( m_iIdx2DepthValue[i] != 0 ) 
1911    {
1912      xFree( m_iIdx2DepthValue[i] );
1913      m_iIdx2DepthValue[i] = 0; 
1914    }
1915#endif
1916  }
1917#endif
1918}
1919
1920#if H_3D_DIM_DLT
1921  Void TComVPS::setDepthLUTs(Int layerIdInVps, Int* idxToDepthValueTable, Int iNumDepthValues)
1922  {
1923    if( idxToDepthValueTable == NULL || iNumDepthValues == 0 ) // default mapping only
1924      return;
1925   
1926    // copy idx2DepthValue to internal array
1927    memcpy(m_iIdx2DepthValue[layerIdInVps], idxToDepthValueTable, iNumDepthValues*sizeof(UInt));
1928   
1929    UInt uiMaxDepthValue = ((1 << g_bitDepthY)-1);
1930    for(Int p=0; p<=uiMaxDepthValue; p++)
1931    {
1932      Int iIdxDown    = 0;
1933      Int iIdxUp      = iNumDepthValues-1;
1934      Bool bFound     = false;
1935     
1936      // iterate over indices to find lower closest depth
1937      Int i = 1;
1938      while(!bFound && i<iNumDepthValues)
1939      {
1940        if( m_iIdx2DepthValue[layerIdInVps][i] > p )
1941        {
1942          iIdxDown  = i-1;
1943          bFound    = true;
1944        }
1945       
1946        i++;
1947      }
1948      // iterate over indices to find upper closest depth
1949      i = iNumDepthValues-2;
1950      bFound = false;
1951      while(!bFound && i>=0)
1952      {
1953        if( m_iIdx2DepthValue[layerIdInVps][i] < p )
1954        {
1955          iIdxUp  = i+1;
1956          bFound    = true;
1957        }
1958       
1959        i--;
1960      }
1961     
1962      // assert monotony
1963      assert(iIdxDown<=iIdxUp);
1964     
1965      // assign closer depth value/idx
1966      if( abs(p-m_iIdx2DepthValue[layerIdInVps][iIdxDown]) < abs(p-m_iIdx2DepthValue[layerIdInVps][iIdxUp]) )
1967      {
1968        m_iDepthValue2Idx[layerIdInVps][p] = iIdxDown;
1969      }
1970      else
1971      {
1972        m_iDepthValue2Idx[layerIdInVps][p] = iIdxUp;
1973      }
1974     
1975    }
1976   
1977    // update DLT variables
1978    m_iNumDepthmapValues[layerIdInVps] = iNumDepthValues;
1979    m_iBitsPerDepthValue[layerIdInVps] = numBitsForValue(m_iNumDepthmapValues[layerIdInVps]);
1980  }
1981#endif
1982
1983#if H_MV
1984
1985Bool TComVPS::checkVPSExtensionSyntax()
1986{
1987  for( Int layer = 1; layer <= getMaxLayersMinus1(); layer++ )
1988  {
1989    // check layer_id_in_nuh constraint
1990    assert( getLayerIdInNuh( layer ) > getLayerIdInNuh( layer -1 ) );
1991  }
1992  return true; 
1993}
1994
1995Int TComVPS::getNumScalabilityTypes()
1996{
1997  return scalTypeToScalIdx( ScalabilityType(MAX_NUM_SCALABILITY_TYPES) );
1998}
1999
2000Int TComVPS::scalTypeToScalIdx( ScalabilityType scalType )
2001{
2002  assert( scalType >= 0 && scalType <= MAX_NUM_SCALABILITY_TYPES ); 
2003  assert( scalType == MAX_NUM_SCALABILITY_TYPES || getScalabilityMaskFlag( scalType ) );
2004  Int scalIdx = 0; 
2005  for( Int curScalType = 0; curScalType < scalType; curScalType++ )
2006  {
2007    scalIdx += ( getScalabilityMaskFlag( curScalType ) ? 1 : 0 );
2008
2009  }
2010
2011  return scalIdx; 
2012}
2013Void TComVPS::setScalabilityMaskFlag( UInt val )
2014{
2015  for ( Int scalType = 0; scalType < MAX_NUM_SCALABILITY_TYPES; scalType++ ) 
2016  {
2017    setScalabilityMaskFlag( scalType, ( val & (1 << scalType ) ) != 0 );
2018  }
2019}
2020
2021Void TComVPS::setRefLayers()
2022{
2023  for( Int i = 0; i  <= getMaxLayersMinus1(); i++ )
2024  {
2025    Int iNuhLId = getLayerIdInNuh( i ); 
2026    m_numDirectRefLayers[ iNuhLId ] = 0; 
2027    for( Int j = 0; j < i; j++ )
2028    {
2029      if( getDirectDependencyFlag(i , j) )
2030      {
2031        m_refLayerId[ iNuhLId ][m_numDirectRefLayers[ iNuhLId ]++ ] = getLayerIdInNuh( j );
2032      }
2033    }
2034  }
2035}
2036
2037Int TComVPS::getRefLayerId( Int layerIdInNuh, Int idx )
2038{
2039  assert( idx >= 0 && idx < m_numDirectRefLayers[layerIdInNuh] );     
2040  Int refLayerIdInNuh = m_refLayerId[ layerIdInNuh ][ idx ];   
2041  assert ( refLayerIdInNuh >= 0 ); 
2042  return refLayerIdInNuh;
2043}
2044
2045Int TComVPS::getScalabilityId( Int layerIdInVps, ScalabilityType scalType )
2046{
2047  return getScalabilityMaskFlag( scalType ) ? getDimensionId( layerIdInVps, scalTypeToScalIdx( scalType ) ) : 0;
2048}
2049
2050#if H_3D
2051Int TComVPS::getLayerIdInNuh( Int viewIndex, Bool depthFlag )
2052{
2053  Int foundLayerIdinNuh = -1; 
2054
2055  for (Int layerIdInVps = 0 ; layerIdInVps <= getMaxLayersMinus1(); layerIdInVps++ )
2056  {
2057    Int layerIdInNuh = getLayerIdInNuh( layerIdInVps ); 
2058    if( ( getViewIndex( layerIdInNuh ) == viewIndex ) && ( getDepthId( layerIdInNuh ) == ( depthFlag ? 1 : 0 ) )  )
2059    {
2060      foundLayerIdinNuh = layerIdInNuh; 
2061      break; 
2062    }
2063  }
2064  assert( foundLayerIdinNuh != -1 ); 
2065
2066  return foundLayerIdinNuh;
2067}
2068#endif // H_3D
2069
2070
2071Int TComVPS::xGetDimBitOffset( Int j )
2072{
2073  Int dimBitOffset = 0; 
2074  if ( getSplittingFlag() && j == getNumScalabilityTypes() )
2075  {
2076     dimBitOffset = 6; 
2077  }
2078  else
2079  {
2080    for (Int dimIdx = 0; dimIdx <= j-1; dimIdx++)
2081    {
2082      dimBitOffset += getDimensionIdLen( dimIdx ); 
2083    }
2084  }
2085  return dimBitOffset; 
2086}
2087
2088Int TComVPS::inferDimensionId( Int i, Int j )
2089{
2090    return ( ( getLayerIdInNuh( i ) & ( (1 << xGetDimBitOffset( j + 1 ) ) - 1) ) >> xGetDimBitOffset( j ) ); 
2091}
2092
2093Int TComVPS::inferLastDimsionIdLenMinus1()
2094{
2095  return ( 5 - xGetDimBitOffset( getNumScalabilityTypes() - 1 ) ); 
2096}
2097
2098Int TComVPS::getNumLayersInIdList( Int lsIdx )
2099{
2100  assert( lsIdx >= 0 ); 
2101  assert( lsIdx <= getVpsNumLayerSetsMinus1() ); 
2102#if H_MV_6_HRD_O0217_13
2103  return (Int) m_layerSetLayerIdList[ lsIdx ].size(); 
2104#else
2105  Int numLayersInIdList = 0; 
2106  for (Int layerId = 0; layerId < getVpsMaxLayerId(); layerId++ )
2107  {
2108    numLayersInIdList += ( getLayerIdIncludedFlag( lsIdx, layerId ) ); 
2109  }
2110
2111  return numLayersInIdList;
2112#endif
2113}
2114
2115#if H_MV_6_HRD_O0217_13
2116Int    TComVPS::getNumOutputLayerSets() 
2117{
2118  Int numOutputLayerSets = getVpsNumberLayerSetsMinus1( ) + 1; 
2119  if ( getMoreOutputLayerSetsThanDefaultFlag( ) )
2120  {     
2121    numOutputLayerSets += (getNumAddOutputLayerSetsMinus1( ) + 1); 
2122  }
2123  return numOutputLayerSets; 
2124}
2125#endif
2126
2127Int TComVPS::getNumViews()
2128{
2129  Int numViews = 1; 
2130  for( Int i = 0; i <=  getMaxLayersMinus1(); i++ )
2131  {
2132    Int lId = getLayerIdInNuh( i ); 
2133    if ( i > 0 && ( getViewIndex( lId ) != getScalabilityId( i - 1, VIEW_ORDER_INDEX ) ) )
2134    {
2135      numViews++; 
2136    }   
2137  }
2138
2139  return numViews;
2140}
2141
2142Bool TComVPS::getInDirectDependencyFlag( Int depLayeridInVps, Int refLayeridInVps, Int depth /*= 0 */ )
2143{
2144  assert( depth < 65 ); 
2145  Bool dependentFlag = getDirectDependencyFlag( depLayeridInVps, refLayeridInVps ); 
2146
2147  for( Int i = 0; i < depLayeridInVps && !dependentFlag; i++ )
2148  {
2149    if ( getDirectDependencyFlag( depLayeridInVps, i ) )
2150    {
2151      dependentFlag = getInDirectDependencyFlag( i, refLayeridInVps, depth++ ); 
2152    }
2153  }
2154  return dependentFlag;
2155}
2156
2157#if H_MV_6_HRD_O0217_13
2158Void TComVPS::deriveLayerSetLayerIdList()
2159{
2160  m_layerSetLayerIdList.resize( getVpsNumLayerSetsMinus1() + 1 ); 
2161  for (Int i = 0; i <= getVpsNumLayerSetsMinus1(); i++ )
2162  {
2163    for( Int m = 0; m  <= getVpsMaxLayerId(); m++ )
2164    {
2165      if( getLayerIdIncludedFlag( i, m) ) 
2166      {
2167        m_layerSetLayerIdList[ i ].push_back( m );       
2168      }
2169    }
2170  }
2171}
2172
2173Void TComVPS::deriveTargetLayerIdLists()
2174{
2175  m_targetDecLayerIdLists.resize( getNumOutputLayerSets() ); 
2176  m_targetOptLayerIdLists.resize( getNumOutputLayerSets() ); 
2177
2178  for (Int targetOptLayerSetIdx = 0; targetOptLayerSetIdx < getNumOutputLayerSets(); targetOptLayerSetIdx++ )
2179  {
2180    Int targetDecLayerSetIdx = getOutputLayerSetIdxMinus1( targetOptLayerSetIdx ) + 1;     
2181    Int lsIdx                = targetDecLayerSetIdx;
2182
2183    for( Int j = 0; j < getNumLayersInIdList( lsIdx ); j++ )
2184    {
2185      m_targetDecLayerIdLists[targetOptLayerSetIdx].push_back( m_layerSetLayerIdList[ lsIdx ][ j ] ); 
2186      if( getOutputLayerFlag( targetOptLayerSetIdx, j  )) // This seems to be wrong in draft text
2187      {
2188        m_targetOptLayerIdLists[targetOptLayerSetIdx].push_back( m_layerSetLayerIdList[ lsIdx ][ j ] );
2189      }
2190    } 
2191  }
2192}
2193#endif
2194#endif // H_MV
2195
2196// ------------------------------------------------------------------------------------------------
2197// Sequence parameter set (SPS)
2198// ------------------------------------------------------------------------------------------------
2199
2200TComSPS::TComSPS()
2201: m_SPSId                     (  0)
2202, m_VPSId                     (  0)
2203, m_chromaFormatIdc           (CHROMA_420)
2204, m_uiMaxTLayers              (  1)
2205// Structure
2206, m_picWidthInLumaSamples     (352)
2207, m_picHeightInLumaSamples    (288)
2208, m_log2MinCodingBlockSize    (  0)
2209, m_log2DiffMaxMinCodingBlockSize (0)
2210, m_uiMaxCUWidth              ( 32)
2211, m_uiMaxCUHeight             ( 32)
2212, m_uiMaxCUDepth              (  3)
2213, m_bLongTermRefsPresent      (false)
2214, m_uiQuadtreeTULog2MaxSize   (  0)
2215, m_uiQuadtreeTULog2MinSize   (  0)
2216, m_uiQuadtreeTUMaxDepthInter (  0)
2217, m_uiQuadtreeTUMaxDepthIntra (  0)
2218// Tool list
2219, m_usePCM                   (false)
2220, m_pcmLog2MaxSize            (  5)
2221, m_uiPCMLog2MinSize          (  7)
2222#if H_3D_QTLPC
2223, m_bUseQTL                   (false)
2224, m_bUsePC                    (false)
2225#endif
2226, m_bitDepthY                 (  8)
2227, m_bitDepthC                 (  8)
2228, m_qpBDOffsetY               (  0)
2229, m_qpBDOffsetC               (  0)
2230, m_useLossless               (false)
2231, m_uiPCMBitDepthLuma         (  8)
2232, m_uiPCMBitDepthChroma       (  8)
2233, m_bPCMFilterDisableFlag     (false)
2234, m_uiBitsForPOC              (  8)
2235, m_numLongTermRefPicSPS    (  0) 
2236, m_uiMaxTrSize               ( 32)
2237, m_bUseSAO                   (false) 
2238, m_bTemporalIdNestingFlag    (false)
2239, m_scalingListEnabledFlag    (false)
2240, m_useStrongIntraSmoothing   (false)
2241, m_vuiParametersPresentFlag  (false)
2242, m_vuiParameters             ()
2243#if H_MV
2244, m_pcVPS                     ( NULL )
2245, m_spsInferScalingListFlag   ( false )
2246, m_spsScalingListRefLayerId  ( 0 )
2247
2248#if !H_MV_6_PS_REP_FORM_18_19_20
2249, m_updateRepFormatFlag       ( true ) 
2250#else
2251, m_updateRepFormatFlag       ( false ) 
2252, m_spsRepFormatIdx           ( 0 )
2253#endif
2254, m_interViewMvVertConstraintFlag (false)
2255#endif
2256#if H_3D
2257, m_bCamParInSliceHeader      (false)
2258#endif
2259{
2260  for ( Int i = 0; i < MAX_TLAYER; i++ )
2261  {
2262    m_uiMaxLatencyIncrease[i] = 0;
2263    m_uiMaxDecPicBuffering[i] = 1;
2264    m_numReorderPics[i]       = 0;
2265  }
2266  m_scalingList = new TComScalingList;
2267  ::memset(m_ltRefPicPocLsbSps, 0, sizeof(m_ltRefPicPocLsbSps));
2268  ::memset(m_usedByCurrPicLtSPSFlag, 0, sizeof(m_usedByCurrPicLtSPSFlag));
2269#if H_MV_6_PSEM_O0142_3
2270  m_spsExtensionFlag = false; 
2271  for( Int i = 0; i < PS_EX_T_MAX_NUM; i++ ) 
2272  {
2273    m_spsExtensionTypeFlag[ i ] = false;
2274  }
2275#endif
2276#if H_MV_6_SHVC_O0098_36
2277  m_numScaledRefLayerOffsets = 0; 
2278
2279  for (Int i = 0; i < MAX_NUM_SCALED_REF_LAYERS; i++ )
2280  {
2281    m_scaledRefLayerId             [i] = -1;
2282  }
2283
2284  for (Int i = 0; i < MAX_NUM_LAYERS; i++ )
2285  {
2286    m_scaledRefLayerLeftOffset     [i] = 0;
2287    m_scaledRefLayerTopOffset      [i] = 0;
2288    m_scaledRefLayerRightOffset    [i] = 0;
2289    m_scaledRefLayerBottomOffset   [i] = 0;
2290  }
2291#endif
2292}
2293
2294TComSPS::~TComSPS()
2295{
2296  delete m_scalingList;
2297  m_RPSList.destroy();
2298}
2299
2300Void  TComSPS::createRPSList( Int numRPS )
2301{ 
2302  m_RPSList.destroy();
2303  m_RPSList.create(numRPS);
2304}
2305
2306Void TComSPS::setHrdParameters( UInt frameRate, UInt numDU, UInt bitRate, Bool randomAccess )
2307{
2308  if( !getVuiParametersPresentFlag() )
2309  {
2310    return;
2311  }
2312
2313  TComVUI *vui = getVuiParameters();
2314  TComHRD *hrd = vui->getHrdParameters();
2315
2316  TimingInfo *timingInfo = vui->getTimingInfo();
2317  timingInfo->setTimingInfoPresentFlag( true );
2318  switch( frameRate )
2319  {
2320  case 24:
2321    timingInfo->setNumUnitsInTick( 1125000 );    timingInfo->setTimeScale    ( 27000000 );
2322    break;
2323  case 25:
2324    timingInfo->setNumUnitsInTick( 1080000 );    timingInfo->setTimeScale    ( 27000000 );
2325    break;
2326  case 30:
2327    timingInfo->setNumUnitsInTick( 900900 );     timingInfo->setTimeScale    ( 27000000 );
2328    break;
2329  case 50:
2330    timingInfo->setNumUnitsInTick( 540000 );     timingInfo->setTimeScale    ( 27000000 );
2331    break;
2332  case 60:
2333    timingInfo->setNumUnitsInTick( 450450 );     timingInfo->setTimeScale    ( 27000000 );
2334    break;
2335  default:
2336    timingInfo->setNumUnitsInTick( 1001 );       timingInfo->setTimeScale    ( 60000 );
2337    break;
2338  }
2339
2340  Bool rateCnt = ( bitRate > 0 );
2341  hrd->setNalHrdParametersPresentFlag( rateCnt );
2342  hrd->setVclHrdParametersPresentFlag( rateCnt );
2343
2344  hrd->setSubPicCpbParamsPresentFlag( ( numDU > 1 ) );
2345
2346  if( hrd->getSubPicCpbParamsPresentFlag() )
2347  {
2348    hrd->setTickDivisorMinus2( 100 - 2 );                          //
2349    hrd->setDuCpbRemovalDelayLengthMinus1( 7 );                    // 8-bit precision ( plus 1 for last DU in AU )
2350    hrd->setSubPicCpbParamsInPicTimingSEIFlag( true );
2351    hrd->setDpbOutputDelayDuLengthMinus1( 5 + 7 );                 // With sub-clock tick factor of 100, at least 7 bits to have the same value as AU dpb delay
2352  }
2353  else
2354  {
2355    hrd->setSubPicCpbParamsInPicTimingSEIFlag( false ); 
2356  }
2357
2358  hrd->setBitRateScale( 4 );                                       // in units of 2~( 6 + 4 ) = 1,024 bps
2359  hrd->setCpbSizeScale( 6 );                                       // in units of 2~( 4 + 4 ) = 1,024 bit
2360  hrd->setDuCpbSizeScale( 6 );                                       // in units of 2~( 4 + 4 ) = 1,024 bit
2361 
2362  hrd->setInitialCpbRemovalDelayLengthMinus1(15);                  // assuming 0.5 sec, log2( 90,000 * 0.5 ) = 16-bit
2363  if( randomAccess )
2364  {
2365    hrd->setCpbRemovalDelayLengthMinus1(5);                        // 32 = 2^5 (plus 1)
2366    hrd->setDpbOutputDelayLengthMinus1 (5);                        // 32 + 3 = 2^6
2367  }
2368  else
2369  {
2370    hrd->setCpbRemovalDelayLengthMinus1(9);                        // max. 2^10
2371    hrd->setDpbOutputDelayLengthMinus1 (9);                        // max. 2^10
2372  }
2373
2374/*
2375   Note: only the case of "vps_max_temporal_layers_minus1 = 0" is supported.
2376*/
2377  Int i, j;
2378  UInt birateValue, cpbSizeValue;
2379  UInt ducpbSizeValue;
2380  UInt duBitRateValue = 0;
2381
2382  for( i = 0; i < MAX_TLAYER; i ++ )
2383  {
2384    hrd->setFixedPicRateFlag( i, 1 );
2385    hrd->setPicDurationInTcMinus1( i, 0 );
2386    hrd->setLowDelayHrdFlag( i, 0 );
2387    hrd->setCpbCntMinus1( i, 0 );
2388
2389    birateValue  = bitRate;
2390    cpbSizeValue = bitRate;                                     // 1 second
2391    ducpbSizeValue = bitRate/numDU;
2392    duBitRateValue = bitRate;
2393    for( j = 0; j < ( hrd->getCpbCntMinus1( i ) + 1 ); j ++ )
2394    {
2395      hrd->setBitRateValueMinus1( i, j, 0, ( birateValue  - 1 ) );
2396      hrd->setCpbSizeValueMinus1( i, j, 0, ( cpbSizeValue - 1 ) );
2397      hrd->setDuCpbSizeValueMinus1( i, j, 0, ( ducpbSizeValue - 1 ) );
2398      hrd->setCbrFlag( i, j, 0, ( j == 0 ) );
2399
2400      hrd->setBitRateValueMinus1( i, j, 1, ( birateValue  - 1) );
2401      hrd->setCpbSizeValueMinus1( i, j, 1, ( cpbSizeValue - 1 ) );
2402      hrd->setDuCpbSizeValueMinus1( i, j, 1, ( ducpbSizeValue - 1 ) );
2403      hrd->setDuBitRateValueMinus1( i, j, 1, ( duBitRateValue - 1 ) );
2404      hrd->setCbrFlag( i, j, 1, ( j == 0 ) );
2405    }
2406  }
2407}
2408const Int TComSPS::m_winUnitX[]={1,2,2,1};
2409const Int TComSPS::m_winUnitY[]={1,2,1,1};
2410
2411TComPPS::TComPPS()
2412: m_PPSId                       (0)
2413, m_SPSId                       (0)
2414, m_picInitQPMinus26            (0)
2415, m_useDQP                      (false)
2416, m_bConstrainedIntraPred       (false)
2417, m_bSliceChromaQpFlag          (false)
2418, m_pcSPS                       (NULL)
2419, m_uiMaxCuDQPDepth             (0)
2420, m_uiMinCuDQPSize              (0)
2421, m_chromaCbQpOffset            (0)
2422, m_chromaCrQpOffset            (0)
2423, m_numRefIdxL0DefaultActive    (1)
2424, m_numRefIdxL1DefaultActive    (1)
2425, m_TransquantBypassEnableFlag  (false)
2426, m_useTransformSkip             (false)
2427, m_dependentSliceSegmentsEnabledFlag    (false)
2428, m_tilesEnabledFlag               (false)
2429, m_entropyCodingSyncEnabledFlag   (false)
2430, m_loopFilterAcrossTilesEnabledFlag  (true)
2431, m_uniformSpacingFlag           (0)
2432, m_iNumColumnsMinus1            (0)
2433, m_puiColumnWidth               (NULL)
2434, m_iNumRowsMinus1               (0)
2435, m_puiRowHeight                 (NULL)
2436, m_iNumSubstreams             (1)
2437, m_signHideFlag(0)
2438, m_cabacInitPresentFlag        (false)
2439, m_encCABACTableIdx            (I_SLICE)
2440, m_sliceHeaderExtensionPresentFlag    (false)
2441, m_loopFilterAcrossSlicesEnabledFlag (false)
2442, m_listsModificationPresentFlag(  0)
2443, m_numExtraSliceHeaderBits(0)
2444#if H_MV
2445, m_ppsInferScalingListFlag(false)
2446, m_ppsScalingListRefLayerId(0)
2447#endif
2448{
2449  m_scalingList = new TComScalingList;
2450}
2451
2452TComPPS::~TComPPS()
2453{
2454  if( m_iNumColumnsMinus1 > 0 && m_uniformSpacingFlag == 0 )
2455  {
2456    if (m_puiColumnWidth) delete [] m_puiColumnWidth; 
2457    m_puiColumnWidth = NULL;
2458  }
2459  if( m_iNumRowsMinus1 > 0 && m_uniformSpacingFlag == 0 )
2460  {
2461    if (m_puiRowHeight) delete [] m_puiRowHeight;
2462    m_puiRowHeight = NULL;
2463  }
2464  delete m_scalingList;
2465}
2466
2467#if H_MV
2468Void TComSPS::inferRepFormat( TComVPS* vps, Int layerIdCurr )
2469{
2470  if ( layerIdCurr > 0 )
2471  { 
2472#if H_MV_6_PS_REP_FORM_18_19_20
2473    Int            repFormatIdx = getUpdateRepFormatFlag() ?  getSpsRepFormatIdx() : vps->getVpsRepFormatIdx( vps->getLayerIdInVps( layerIdCurr ) ) ;
2474    TComRepFormat* repFormat    = vps->getRepFormat( repFormatIdx ); 
2475#else
2476    TComRepFormat* repFormat = vps->getRepFormat( vps->getVpsRepFormatIdx( vps->getLayerIdInVps( layerIdCurr ) ) ); 
2477
2478    if ( !getUpdateRepFormatFlag() )
2479    {       
2480#endif
2481      setChromaFormatIdc( repFormat->getChromaFormatVpsIdc() );         
2482      //// ToDo: add when supported:
2483      // setSeperateColourPlaneFlag( repFormat->getSeparateColourPlaneVpsFlag() ) ;
2484
2485      setPicWidthInLumaSamples ( repFormat->getPicWidthVpsInLumaSamples()  ); 
2486      setPicHeightInLumaSamples( repFormat->getPicHeightVpsInLumaSamples() ); 
2487
2488      setBitDepthY             ( repFormat->getBitDepthVpsLumaMinus8()   + 8 ); 
2489      setQpBDOffsetY           ( (Int) (6*( getBitDepthY() - 8 )) );
2490
2491      setBitDepthC             ( repFormat->getBitDepthVpsChromaMinus8() + 8 ); 
2492      setQpBDOffsetC           ( (Int) (6* ( getBitDepthC() -8 ) ) );
2493#if !H_MV_6_PS_REP_FORM_18_19_20
2494    }
2495    else
2496#else
2497    if ( getLayerId() > 0 && getUpdateRepFormatFlag() )
2498#endif
2499    {
2500      assert( getChromaFormatIdc()      <=  repFormat->getChromaFormatVpsIdc()         ); 
2501      //// ToDo: add when supported:
2502      // assert( getSeperateColourPlaneFlag() <=  repFormat->getSeparateColourPlaneVpsFlag() ) ;
2503
2504      assert( getPicWidthInLumaSamples()  <= repFormat->getPicWidthVpsInLumaSamples()    ); 
2505      assert( getPicHeightInLumaSamples() <= repFormat->getPicHeightVpsInLumaSamples()   ); 
2506
2507      assert( getBitDepthY()              <= repFormat->getBitDepthVpsLumaMinus8()   + 8 );         
2508      assert( getBitDepthC()              <= repFormat->getBitDepthVpsChromaMinus8() + 8 ); 
2509    }
2510  }
2511
2512  // Set conformance window
2513  Int scal = TComSPS::getWinUnitX( getChromaFormatIdc() ) ;
2514  getConformanceWindow().scaleOffsets( scal );
2515  getVuiParameters()->getDefaultDisplayWindow().scaleOffsets( scal );
2516}
2517
2518Void TComSPS::inferScalingList( TComSPS* spsSrc )
2519{
2520  if ( getSpsInferScalingListFlag() ) 
2521  {
2522    assert( spsSrc != NULL ); 
2523    assert( !spsSrc->getSpsInferScalingListFlag() );             
2524    getScalingList()->inferFrom( spsSrc->getScalingList() ); 
2525  }
2526}
2527
2528#endif
2529#if H_3D
2530Void
2531TComSPS::initCamParaSPS( UInt uiViewIndex, UInt uiCamParPrecision, Bool bCamParSlice, Int** aaiScale, Int** aaiOffset )
2532{
2533  AOT( uiViewIndex != 0 && !bCamParSlice && ( aaiScale == 0 || aaiOffset == 0 ) ); 
2534 
2535  m_uiCamParPrecision     = ( uiViewIndex ? uiCamParPrecision : 0 );
2536  m_bCamParInSliceHeader  = ( uiViewIndex ? bCamParSlice  : false );
2537  ::memset( m_aaiCodedScale,  0x00, sizeof( m_aaiCodedScale  ) );
2538  ::memset( m_aaiCodedOffset, 0x00, sizeof( m_aaiCodedOffset ) );
2539
2540  if( !m_bCamParInSliceHeader )
2541  {
2542    for( UInt uiBaseViewIndex = 0; uiBaseViewIndex < uiViewIndex; uiBaseViewIndex++ )
2543    {
2544      m_aaiCodedScale [ 0 ][ uiBaseViewIndex ] = aaiScale [ uiBaseViewIndex ][     uiViewIndex ];
2545      m_aaiCodedScale [ 1 ][ uiBaseViewIndex ] = aaiScale [     uiViewIndex ][ uiBaseViewIndex ];
2546      m_aaiCodedOffset[ 0 ][ uiBaseViewIndex ] = aaiOffset[ uiBaseViewIndex ][     uiViewIndex ];
2547      m_aaiCodedOffset[ 1 ][ uiBaseViewIndex ] = aaiOffset[     uiViewIndex ][ uiBaseViewIndex ];
2548    }
2549  }
2550}
2551#endif
2552TComReferencePictureSet::TComReferencePictureSet()
2553: m_numberOfPictures (0)
2554, m_numberOfNegativePictures (0)
2555, m_numberOfPositivePictures (0)
2556, m_numberOfLongtermPictures (0)
2557, m_interRPSPrediction (0) 
2558, m_deltaRIdxMinus1 (0)   
2559, m_deltaRPS (0) 
2560, m_numRefIdc (0) 
2561{
2562  ::memset( m_deltaPOC, 0, sizeof(m_deltaPOC) );
2563  ::memset( m_POC, 0, sizeof(m_POC) );
2564  ::memset( m_used, 0, sizeof(m_used) );
2565  ::memset( m_refIdc, 0, sizeof(m_refIdc) );
2566}
2567
2568TComReferencePictureSet::~TComReferencePictureSet()
2569{
2570}
2571
2572Void TComReferencePictureSet::setUsed(Int bufferNum, Bool used)
2573{
2574  m_used[bufferNum] = used;
2575}
2576
2577Void TComReferencePictureSet::setDeltaPOC(Int bufferNum, Int deltaPOC)
2578{
2579  m_deltaPOC[bufferNum] = deltaPOC;
2580}
2581
2582Void TComReferencePictureSet::setNumberOfPictures(Int numberOfPictures)
2583{
2584  m_numberOfPictures = numberOfPictures;
2585}
2586
2587Int TComReferencePictureSet::getUsed(Int bufferNum)
2588{
2589  return m_used[bufferNum];
2590}
2591
2592Int TComReferencePictureSet::getDeltaPOC(Int bufferNum)
2593{
2594  return m_deltaPOC[bufferNum];
2595}
2596
2597Int TComReferencePictureSet::getNumberOfPictures()
2598{
2599  return m_numberOfPictures;
2600}
2601
2602Int TComReferencePictureSet::getPOC(Int bufferNum)
2603{
2604  return m_POC[bufferNum];
2605}
2606
2607Void TComReferencePictureSet::setPOC(Int bufferNum, Int POC)
2608{
2609  m_POC[bufferNum] = POC;
2610}
2611
2612Bool TComReferencePictureSet::getCheckLTMSBPresent(Int bufferNum)
2613{
2614  return m_bCheckLTMSB[bufferNum];
2615}
2616
2617Void TComReferencePictureSet::setCheckLTMSBPresent(Int bufferNum, Bool b)
2618{
2619  m_bCheckLTMSB[bufferNum] = b;
2620}
2621
2622/** set the reference idc value at uiBufferNum entry to the value of iRefIdc
2623 * \param uiBufferNum
2624 * \param iRefIdc
2625 * \returns Void
2626 */
2627Void TComReferencePictureSet::setRefIdc(Int bufferNum, Int refIdc)
2628{
2629  m_refIdc[bufferNum] = refIdc;
2630}
2631
2632/** get the reference idc value at uiBufferNum
2633 * \param uiBufferNum
2634 * \returns Int
2635 */
2636Int  TComReferencePictureSet::getRefIdc(Int bufferNum)
2637{
2638  return m_refIdc[bufferNum];
2639}
2640
2641/** Sorts the deltaPOC and Used by current values in the RPS based on the deltaPOC values.
2642 *  deltaPOC values are sorted with -ve values before the +ve values.  -ve values are in decreasing order.
2643 *  +ve values are in increasing order.
2644 * \returns Void
2645 */
2646Void TComReferencePictureSet::sortDeltaPOC()
2647{
2648  // sort in increasing order (smallest first)
2649  for(Int j=1; j < getNumberOfPictures(); j++)
2650  { 
2651    Int deltaPOC = getDeltaPOC(j);
2652    Bool used = getUsed(j);
2653    for (Int k=j-1; k >= 0; k--)
2654    {
2655      Int temp = getDeltaPOC(k);
2656      if (deltaPOC < temp)
2657      {
2658        setDeltaPOC(k+1, temp);
2659        setUsed(k+1, getUsed(k));
2660        setDeltaPOC(k, deltaPOC);
2661        setUsed(k, used);
2662      }
2663    }
2664  }
2665  // flip the negative values to largest first
2666  Int numNegPics = getNumberOfNegativePictures();
2667  for(Int j=0, k=numNegPics-1; j < numNegPics>>1; j++, k--)
2668  { 
2669    Int deltaPOC = getDeltaPOC(j);
2670    Bool used = getUsed(j);
2671    setDeltaPOC(j, getDeltaPOC(k));
2672    setUsed(j, getUsed(k));
2673    setDeltaPOC(k, deltaPOC);
2674    setUsed(k, used);
2675  }
2676}
2677
2678/** Prints the deltaPOC and RefIdc (if available) values in the RPS.
2679 *  A "*" is added to the deltaPOC value if it is Used bu current.
2680 * \returns Void
2681 */
2682Void TComReferencePictureSet::printDeltaPOC()
2683{
2684  printf("DeltaPOC = { ");
2685  for(Int j=0; j < getNumberOfPictures(); j++)
2686  {
2687    printf("%d%s ", getDeltaPOC(j), (getUsed(j)==1)?"*":"");
2688  } 
2689  if (getInterRPSPrediction()) 
2690  {
2691    printf("}, RefIdc = { ");
2692    for(Int j=0; j < getNumRefIdc(); j++)
2693    {
2694      printf("%d ", getRefIdc(j));
2695    } 
2696  }
2697  printf("}\n");
2698}
2699
2700TComRPSList::TComRPSList()
2701:m_referencePictureSets (NULL)
2702{
2703}
2704
2705TComRPSList::~TComRPSList()
2706{
2707}
2708
2709Void TComRPSList::create( Int numberOfReferencePictureSets)
2710{
2711  m_numberOfReferencePictureSets = numberOfReferencePictureSets;
2712  m_referencePictureSets = new TComReferencePictureSet[numberOfReferencePictureSets];
2713}
2714
2715Void TComRPSList::destroy()
2716{
2717  if (m_referencePictureSets)
2718  {
2719    delete [] m_referencePictureSets;
2720  }
2721  m_numberOfReferencePictureSets = 0;
2722  m_referencePictureSets = NULL;
2723}
2724
2725
2726
2727TComReferencePictureSet* TComRPSList::getReferencePictureSet(Int referencePictureSetNum)
2728{
2729  return &m_referencePictureSets[referencePictureSetNum];
2730}
2731
2732Int TComRPSList::getNumberOfReferencePictureSets()
2733{
2734  return m_numberOfReferencePictureSets;
2735}
2736
2737Void TComRPSList::setNumberOfReferencePictureSets(Int numberOfReferencePictureSets)
2738{
2739  m_numberOfReferencePictureSets = numberOfReferencePictureSets;
2740}
2741
2742TComRefPicListModification::TComRefPicListModification()
2743: m_bRefPicListModificationFlagL0 (false)
2744, m_bRefPicListModificationFlagL1 (false)
2745{
2746  ::memset( m_RefPicSetIdxL0, 0, sizeof(m_RefPicSetIdxL0) );
2747  ::memset( m_RefPicSetIdxL1, 0, sizeof(m_RefPicSetIdxL1) );
2748}
2749
2750TComRefPicListModification::~TComRefPicListModification()
2751{
2752}
2753
2754TComScalingList::TComScalingList()
2755{
2756  init();
2757}
2758TComScalingList::~TComScalingList()
2759{
2760  destroy();
2761}
2762
2763/** set default quantization matrix to array
2764*/
2765Void TComSlice::setDefaultScalingList()
2766{
2767  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
2768  {
2769    for(UInt listId=0;listId<g_scalingListNum[sizeId];listId++)
2770    {
2771      getScalingList()->processDefaultMarix(sizeId, listId);
2772    }
2773  }
2774}
2775/** check if use default quantization matrix
2776 * \returns true if use default quantization matrix in all size
2777*/
2778Bool TComSlice::checkDefaultScalingList()
2779{
2780  UInt defaultCounter=0;
2781
2782  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
2783  {
2784    for(UInt listId=0;listId<g_scalingListNum[sizeId];listId++)
2785    {
2786      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
2787     && ((sizeId < SCALING_LIST_16x16) || (getScalingList()->getScalingListDC(sizeId,listId) == 16))) // check DC value
2788      {
2789        defaultCounter++;
2790      }
2791    }
2792  }
2793  return (defaultCounter == (SCALING_LIST_NUM * SCALING_LIST_SIZE_NUM - 4)) ? false : true; // -4 for 32x32
2794}
2795
2796#if H_MV
2797Void TComSlice::createInterLayerReferencePictureSet( TComPicLists* ivPicLists, std::vector<TComPic*>& refPicSetInterLayer0, std::vector<TComPic*>& refPicSetInterLayer1 )
2798{ 
2799  refPicSetInterLayer0.clear(); 
2800  refPicSetInterLayer1.clear(); 
2801
2802  for( Int i = 0; i < getNumActiveRefLayerPics(); i++ ) 
2803  {
2804    Int layerIdRef = getRefPicLayerId( i ); 
2805    TComPic* picRef = ivPicLists->getPic( layerIdRef, getPOC() ) ; 
2806    assert ( picRef != 0 ); 
2807
2808    picRef->getPicYuvRec()->extendPicBorder(); 
2809    picRef->setIsLongTerm( true );       
2810    picRef->getSlice(0)->setReferenced( true );       
2811
2812    Int viewIdCur  = getVPS()->getViewId( getLayerId() ); 
2813    Int viewIdZero = getVPS()->getViewId( 0 );
2814    Int viewIdRef  = getVPS()->getViewId( layerIdRef ); 
2815
2816    if (  ( viewIdCur <= viewIdZero && viewIdCur <= viewIdRef ) || ( viewIdCur >= viewIdZero && viewIdCur >= viewIdRef ) )
2817    {
2818      refPicSetInterLayer0.push_back( picRef ); 
2819    }
2820    else
2821    {
2822      refPicSetInterLayer1.push_back( picRef ); 
2823    }
2824    // Consider to check here:
2825    // "If the current picture is a RADL picture, there shall be no entry in the RefPicSetInterLayer0 and RefPicSetInterLayer1 that is a RASL picture. "   
2826  }
2827}
2828
2829Void TComSlice::markIvRefPicsAsShortTerm( std::vector<TComPic*> refPicSetInterLayer0, std::vector<TComPic*> refPicSetInterLayer1 )
2830{
2831  // Mark as shortterm
2832  for ( Int i = 0; i < refPicSetInterLayer0.size(); i++ ) 
2833  {
2834    refPicSetInterLayer0[i]->setIsLongTerm( false ); 
2835  }
2836
2837  for ( Int i = 0; i < refPicSetInterLayer1.size(); i++ ) 
2838  {
2839    refPicSetInterLayer1[i]->setIsLongTerm( false ); 
2840  }
2841
2842}
2843
2844
2845Void TComSlice::markIvRefPicsAsUnused( TComPicLists* ivPicLists, std::vector<Int> targetDecLayerIdSet, TComVPS* vps, Int curLayerId, Int curPoc )
2846{
2847  // Fill targetDecLayerIdSet with all layers if empty (at encoder side)
2848  if (targetDecLayerIdSet.size() == 0 )   
2849  {
2850    for ( Int layerIdInVps = 0; layerIdInVps <= vps->getMaxLayersMinus1(); layerIdInVps++ )
2851    {
2852      targetDecLayerIdSet.push_back( vps->getLayerIdInNuh( layerIdInVps ) ); 
2853    }
2854  } 
2855
2856  Int numTargetDecLayers = (Int) targetDecLayerIdSet.size(); 
2857  Int latestDecIdx; 
2858  for ( latestDecIdx = 0; latestDecIdx < numTargetDecLayers; latestDecIdx++)
2859  {
2860    if ( targetDecLayerIdSet[ latestDecIdx ] == curLayerId )
2861    {
2862      break; 
2863    }       
2864  }       
2865
2866  for( Int i = 0; i <= latestDecIdx; i++ ) 
2867  {
2868    if ( vps->nuhLayerIdIncluded( targetDecLayerIdSet[ i ] ) )
2869    {
2870      TComPic* pcPic = ivPicLists->getPic( targetDecLayerIdSet[ i ], curPoc ); 
2871#if H_MV_LAYER_WISE_STARTUP
2872      if ( pcPic )
2873      {
2874#endif
2875        if( pcPic->getSlice(0)->isReferenced() && pcPic->getSlice(0)->getTemporalLayerNonReferenceFlag() ) 
2876        { 
2877          Bool remainingInterLayerReferencesFlag = false; 
2878          for( Int j = latestDecIdx + 1; j < numTargetDecLayers; j++ )
2879          { 
2880            TComVPS* vpsSlice = pcPic->getSlice(0)->getVPS(); 
2881            if ( vps->nuhLayerIdIncluded( targetDecLayerIdSet[ j ] ) )
2882            {
2883              for( Int k = 0; k < vpsSlice->getNumDirectRefLayers( targetDecLayerIdSet[ j ] ); k++ )
2884              {
2885                if ( targetDecLayerIdSet[ i ] == vpsSlice->getRefLayerId( targetDecLayerIdSet[ j ],  k  ) )
2886                {
2887                  remainingInterLayerReferencesFlag = true;
2888                }
2889              }
2890            }
2891          }
2892          if( !remainingInterLayerReferencesFlag )
2893          {
2894            pcPic->getSlice(0)->setReferenced( false );                   
2895          }
2896        }
2897#if H_MV_LAYER_WISE_STARTUP
2898      }
2899#endif
2900    }
2901  }
2902}
2903
2904Void TComSlice::printRefPicList()
2905{ 
2906  for ( Int li = 0; li < 2; li++)
2907  {   
2908    std::cout << std::endl << "RefPicListL" <<  li << ":" << std::endl; 
2909    for (Int rIdx = 0; rIdx <= (m_aiNumRefIdx[li]-1); rIdx ++)
2910    {     
2911      if (rIdx == 0 && li == 0) m_apcRefPicList[li][rIdx]->print( true );
2912
2913      m_apcRefPicList[li][rIdx]->print( false );
2914    }
2915  }
2916}
2917
2918Void TComSlice::markCurrPic( TComPic* currPic )
2919{
2920  if ( !currPic->getSlice(0)->getDiscardableFlag() )
2921  {
2922    currPic->getSlice(0)->setReferenced( true ) ; 
2923    currPic->setIsLongTerm( false ); 
2924  }
2925  else
2926  {
2927    currPic->getSlice(0)->setReferenced( false ) ; 
2928  }
2929}
2930
2931Void TComSlice::setRefPicSetInterLayer( std::vector<TComPic*>* refPicSetInterLayer0, std::vector<TComPic*>* refPicSetInterLayer1 )
2932{
2933  m_refPicSetInterLayer0 = refPicSetInterLayer0; 
2934  m_refPicSetInterLayer1 = refPicSetInterLayer1; 
2935}
2936
2937TComPic* TComSlice::getPicFromRefPicSetInterLayer(Int setIdc, Int layerId )
2938{
2939  assert ( setIdc == 0 || setIdc == 1);   
2940  std::vector<TComPic*>* refPicSetInterLayer = ( setIdc == 0 ? m_refPicSetInterLayer0 : m_refPicSetInterLayer1);   
2941  assert( refPicSetInterLayer != 0 ); 
2942 
2943  TComPic* pcPic = NULL; 
2944  for ( Int i = 0; i < (*refPicSetInterLayer).size(); i++ )
2945  {
2946    if ((*refPicSetInterLayer)[ i ]->getLayerId() == layerId )
2947    {
2948      pcPic = (*refPicSetInterLayer)[ i ]; 
2949    }
2950  }
2951
2952  assert(pcPic != NULL); 
2953  return pcPic;
2954}
2955
2956
2957#if H_MV_6_ILDDS_ILREFPICS_27_34
2958Int  TComSlice::getRefLayerPicFlag( Int i ) 
2959{
2960  TComVPS* vps = getVPS(); 
2961  Int refLayerIdx = vps->getLayerIdInVps( vps->getRefLayerId( getLayerId(), i ) ); 
2962
2963  Bool refLayerPicFlag = ( vps->getSubLayersVpsMaxMinus1( refLayerIdx ) >=  getTLayer() )  && 
2964    ( vps->getMaxTidIlRefPicsPlus1( refLayerIdx, vps->getLayerIdInVps( getLayerId() )) > getTLayer() ); 
2965
2966  return refLayerPicFlag;       
2967}   
2968
2969Int TComSlice::getRefLayerPicIdc( Int j ) 
2970{ 
2971  Int refLayerPicIdc = -1; 
2972  Int curj = 0; 
2973  for( Int i = 0;  i < getVPS()->getNumDirectRefLayers( getLayerId()) ; i++ )
2974  {
2975    if( getRefLayerPicFlag( i ) )
2976    {
2977      if ( curj == j ) 
2978      {
2979        refLayerPicIdc = i;         
2980        break;
2981      }
2982      curj++; 
2983    }
2984  }
2985
2986  assert( curj == j ); 
2987  assert( refLayerPicIdc != -1 ); 
2988  return refLayerPicIdc; 
2989}
2990
2991Int  TComSlice::getNumRefLayerPics( )
2992{ 
2993  Int numRefLayerPics = 0; 
2994  for( Int i = 0;  i < getVPS()->getNumDirectRefLayers( getLayerId()) ; i++ )
2995  {
2996    numRefLayerPics += getRefLayerPicFlag( i ); 
2997  }
2998  return numRefLayerPics; 
2999}
3000#endif
3001
3002
3003
3004Int TComSlice::getNumActiveRefLayerPics()
3005{
3006  Int numActiveRefLayerPics; 
3007
3008  if( getLayerId() == 0 || getVPS()->getNumDirectRefLayers( getLayerId() ) ==  0 )
3009  {
3010    numActiveRefLayerPics = 0; 
3011  }
3012  else if (getVPS()->getAllRefLayersActiveFlag() )
3013  {
3014#if H_MV_6_ILDDS_ILREFPICS_27_34
3015    numActiveRefLayerPics = getNumRefLayerPics(); 
3016#else
3017    numActiveRefLayerPics = getVPS()->getNumDirectRefLayers( getLayerId() );
3018#endif
3019  }
3020  else if ( !getInterLayerPredEnabledFlag() )
3021  {
3022    numActiveRefLayerPics = 0; 
3023  }
3024  else if( getVPS()->getMaxOneActiveRefLayerFlag() || getVPS()->getNumDirectRefLayers( getLayerId() ) == 1 )
3025  {
3026#if H_MV_6_ILDDS_ILREFPICS_27_34
3027    numActiveRefLayerPics = getRefLayerPicFlag( 0 ) ? 1 : 0; 
3028#else
3029    numActiveRefLayerPics = 1; 
3030#endif
3031  }
3032  else
3033  {
3034    numActiveRefLayerPics = getNumInterLayerRefPicsMinus1() + 1; 
3035  }
3036  return numActiveRefLayerPics;
3037}
3038
3039Int TComSlice::getRefPicLayerId( Int i )
3040{
3041  return getVPS()->getRefLayerId( getLayerId(), getInterLayerPredLayerIdc( i ) );
3042}
3043
3044#if H_3D_ARP
3045#if SHARP_ARP_REF_CHECK_F0105
3046Void TComSlice::setARPStepNum( TComPicLists*ivPicLists )
3047#else
3048Void TComSlice::setARPStepNum()                                 
3049#endif
3050{
3051#if SHARP_ARP_REF_CHECK_F0105
3052  Bool tempRefPicInListsFlag = false;
3053#else
3054  Bool bAllIvRef = true;
3055#endif
3056#if QC_MTK_INTERVIEW_ARP_F0123_F0108
3057  if(!getVPS()->getUseAdvRP(getLayerId()) || this->isIRAP())
3058#else
3059  if(!getVPS()->getUseAdvRP(getLayerId()))
3060#endif
3061  {
3062    m_nARPStepNum = 0;
3063  }
3064  else
3065  {
3066#if SHARP_ARP_REF_CHECK_F0105
3067    setFirstTRefIdx (REF_PIC_LIST_0, -1);
3068    setFirstTRefIdx (REF_PIC_LIST_1, -1);
3069    for ( Int refListIdx = 0; refListIdx < ((m_eSliceType==B_SLICE) ? 2 : 1); refListIdx++ )
3070    {
3071      for(Int i = 0; i < getNumRefIdx(RefPicList(refListIdx)); i++ )
3072      {
3073        if ( getRefPic(RefPicList(refListIdx), i)->getPOC() != getPOC() )
3074        {
3075          setFirstTRefIdx (RefPicList(refListIdx), i);
3076          break;
3077        }
3078      }
3079    }
3080    tempRefPicInListsFlag = getFirstTRefIdx(REF_PIC_LIST_0) >= 0 || getFirstTRefIdx(REF_PIC_LIST_1) >= 0;
3081    m_nARPStepNum = tempRefPicInListsFlag ? getVPS()->getARPStepNum(getLayerId()) : 0;
3082#else
3083    for( Int iRefListId = 0; iRefListId < 2; iRefListId++ )
3084    {
3085      RefPicList  eRefPicList = RefPicList( iRefListId );
3086      Int iNumRefIdx = getNumRefIdx(eRefPicList);
3087     
3088      if( iNumRefIdx <= 0 )
3089      {
3090        continue;
3091      }
3092
3093      for ( Int i = 0; i < iNumRefIdx; i++ )
3094      {
3095        if( getRefPic( eRefPicList, i)->getPOC() != getPOC() )
3096        {
3097          bAllIvRef = false;
3098          break;
3099        }
3100      }
3101
3102      if( bAllIvRef == false ) { break; }
3103    }
3104    m_nARPStepNum = !bAllIvRef ? getVPS()->getARPStepNum(getLayerId()) : 0;
3105#endif
3106  }
3107#if SHARP_ARP_REF_CHECK_F0105
3108  if (tempRefPicInListsFlag)
3109  {
3110    for ( Int refListIdx = 0; refListIdx < ((m_eSliceType==B_SLICE) ? 2 : 1); refListIdx++ )
3111    {
3112      RefPicList eRefPicList = RefPicList( refListIdx );
3113      Int prevPOC = getRefPic(eRefPicList, getFirstTRefIdx(eRefPicList) )->getPOC();
3114      for( Int i = 0; i < getNumActiveRefLayerPics(); i++ )
3115      {
3116        Int layerIdInNuh = getRefPicLayerId( i );
3117        Int viewIdx = getVPS()->getViewId( layerIdInNuh );
3118        TComPic*pcPicPrev = ivPicLists->getPic(viewIdx, 0, prevPOC);
3119        if (getFirstTRefIdx(eRefPicList) >= 0 && pcPicPrev && pcPicPrev->getSlice( 0 )->isReferenced())
3120        {
3121          m_arpRefPicAvailable[eRefPicList][layerIdInNuh] = true;
3122        }
3123        else
3124        {
3125          m_arpRefPicAvailable[eRefPicList][layerIdInNuh] = false;
3126        }
3127      }
3128  }
3129}
3130#endif
3131}
3132#endif
3133#if H_3D_IC
3134Void TComSlice::xSetApplyIC()
3135{
3136  Int iMaxPelValue = ( 1 << g_bitDepthY ); 
3137  Int *aiRefOrgHist;
3138  Int *aiCurrHist;
3139  aiRefOrgHist = (Int *) xMalloc( Int,iMaxPelValue );
3140  aiCurrHist   = (Int *) xMalloc( Int,iMaxPelValue );
3141  memset( aiRefOrgHist, 0, iMaxPelValue*sizeof(Int) );
3142  memset( aiCurrHist, 0, iMaxPelValue*sizeof(Int) );
3143  // Reference Idx Number
3144  Int iNumRefIdx = getNumRefIdx( REF_PIC_LIST_0 );
3145  TComPic* pcCurrPic = NULL;
3146  TComPic* pcRefPic = NULL;
3147  TComPicYuv* pcCurrPicYuv = NULL;
3148  TComPicYuv* pcRefPicYuvOrg = NULL;
3149  pcCurrPic = getPic();
3150  pcCurrPicYuv = pcCurrPic->getPicYuvOrg();
3151  Int iWidth = pcCurrPicYuv->getWidth();
3152  Int iHeight = pcCurrPicYuv->getHeight();
3153
3154
3155  // Get InterView Reference picture
3156  // !!!!! Assume only one Interview Reference Picture in L0
3157  for ( Int i = 0; i < iNumRefIdx; i++ )
3158  {
3159    pcRefPic = getRefPic( REF_PIC_LIST_0, i );
3160    if ( pcRefPic != NULL )
3161    {
3162      if ( pcCurrPic->getViewIndex() != pcRefPic->getViewIndex() )
3163      {
3164        pcRefPicYuvOrg = pcRefPic->getPicYuvOrg();
3165      }
3166    }
3167  }
3168
3169  if ( pcRefPicYuvOrg != NULL )
3170  {
3171    Pel* pCurrY = pcCurrPicYuv ->getLumaAddr();
3172    Pel* pRefOrgY = pcRefPicYuvOrg  ->getLumaAddr();
3173    Int iCurrStride = pcCurrPicYuv->getStride();
3174    Int iRefStride = pcRefPicYuvOrg->getStride();
3175    Int iSumOrgSAD = 0;
3176    Double dThresholdOrgSAD = getIsDepth() ? 0.1 : 0.05;
3177
3178    // Histogram building - luminance
3179    for ( Int y = 0; y < iHeight; y++ )
3180    {
3181      for ( Int x = 0; x < iWidth; x++ )
3182      {
3183        aiCurrHist[pCurrY[x]]++;
3184        aiRefOrgHist[pRefOrgY[x]]++;
3185      }
3186      pCurrY += iCurrStride;
3187      pRefOrgY += iRefStride;
3188    }
3189    // Histogram SAD
3190    for ( Int i = 0; i < iMaxPelValue; i++ )
3191    {
3192      iSumOrgSAD += abs( aiCurrHist[i] - aiRefOrgHist[i] );
3193    }
3194    // Setting
3195    if ( iSumOrgSAD > Int( dThresholdOrgSAD * iWidth * iHeight ) )
3196    {
3197      m_bApplyIC = true;
3198    }
3199    else
3200    {
3201      m_bApplyIC = false;
3202    }
3203  }
3204
3205  xFree( aiCurrHist   );
3206  xFree( aiRefOrgHist );
3207  aiCurrHist = NULL;
3208  aiRefOrgHist = NULL;
3209}
3210#endif
3211#if H_3D
3212Void TComSlice::setIvPicLists( TComPicLists* m_ivPicLists )
3213{
3214  for (Int i = 0; i < MAX_NUM_LAYERS; i++ )
3215  {     
3216    for ( Int depthId = 0; depthId < 2; depthId++ )
3217    {
3218      m_ivPicsCurrPoc[ depthId ][ i ] = ( i <= m_viewIndex ) ? m_ivPicLists->getPic( i, ( depthId == 1) , getPOC() ) : NULL;
3219    }       
3220  } 
3221}
3222Void TComSlice::setDepthToDisparityLUTs()
3223{ 
3224  Bool setupLUT = false; 
3225  Int layerIdInVPS = getVPS()->getLayerIdInNuh( m_layerId ); 
3226
3227#if H_3D_VSP
3228  setupLUT = setupLUT || getVPS()->getViewSynthesisPredFlag( layerIdInVPS); 
3229#endif
3230
3231#if H_3D_NBDV_REF
3232  setupLUT = setupLUT || getVPS()->getDepthRefinementFlag( layerIdInVPS );
3233#endif
3234
3235#if QC_DEPTH_IV_MRG_F0125
3236  setupLUT = setupLUT || ( getVPS()->getIvMvPredFlag(layerIdInVPS ) && getIsDepth() );
3237#endif
3238
3239  if( !setupLUT )
3240    return; 
3241
3242  /// GT: Allocation should be moved to a better place later;
3243  if ( m_depthToDisparityB == NULL )
3244  {
3245    m_depthToDisparityB = new Int*[ getViewIndex() ];
3246    for ( Int i = 0; i < getViewIndex(); i++ )
3247    {
3248      m_depthToDisparityB[ i ] = new Int[ Int(1 << g_bitDepthY) ]; 
3249    }
3250  }
3251
3252  if ( m_depthToDisparityF == NULL )
3253  {
3254    m_depthToDisparityF= new Int*[ getViewIndex() ];
3255    for ( Int i = 0; i < getViewIndex(); i++ )
3256    {
3257      m_depthToDisparityF[ i ] = new Int[ Int(1 << g_bitDepthY) ]; 
3258    }
3259  }
3260
3261  assert( m_depthToDisparityB != NULL ); 
3262  assert( m_depthToDisparityF != NULL ); 
3263
3264  TComSPS* sps = getSPS(); 
3265
3266  Int log2Div = g_bitDepthY - 1 + sps->getCamParPrecision();
3267
3268  Bool camParaSH = m_pcSPS->hasCamParInSliceHeader();
3269
3270  Int* codScale     = camParaSH ? m_aaiCodedScale [ 0 ] : sps->getCodedScale    (); 
3271  Int* codOffset    = camParaSH ? m_aaiCodedOffset[ 0 ] : sps->getCodedOffset   (); 
3272  Int* invCodScale  = camParaSH ? m_aaiCodedScale [ 1 ] : sps->getInvCodedScale (); 
3273  Int* invCodOffset = camParaSH ? m_aaiCodedOffset[ 1 ] : sps->getInvCodedOffset(); 
3274
3275  for (Int i = 0; i <= ( getViewIndex() - 1); i++)
3276  {
3277    for ( Int d = 0; d <= ( ( 1 << g_bitDepthY ) - 1 ); d++ )
3278    {
3279      Int offset =    ( codOffset  [ i ] << g_bitDepthY ) + ( ( 1 << log2Div ) >> 1 );         
3280      m_depthToDisparityB[ i ][ d ] = ( codScale [ i ] * d + offset ) >> log2Div; 
3281
3282      Int invOffset = ( invCodOffset[ i ] << g_bitDepthY ) + ( ( 1 << log2Div ) >> 1 );         
3283      m_depthToDisparityF[ i ][ d ] = ( invCodScale[ i ] * d + invOffset ) >> log2Div; 
3284    }
3285  }
3286}
3287#endif
3288#endif
3289/** get scaling matrix from RefMatrixID
3290 * \param sizeId size index
3291 * \param Index of input matrix
3292 * \param Index of reference matrix
3293 */
3294Void TComScalingList::processRefMatrix( UInt sizeId, UInt listId , UInt refListId )
3295{
3296  ::memcpy(getScalingListAddress(sizeId, listId),((listId == refListId)? getScalingListDefaultAddress(sizeId, refListId): getScalingListAddress(sizeId, refListId)),sizeof(Int)*min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId]));
3297}
3298
3299/** parse syntax infomation
3300 *  \param pchFile syntax infomation
3301 *  \returns false if successful
3302 */
3303Bool TComScalingList::xParseScalingList(Char* pchFile)
3304{
3305  FILE *fp;
3306  Char line[1024];
3307  UInt sizeIdc,listIdc;
3308  UInt i,size = 0;
3309  Int *src=0,data;
3310  Char *ret;
3311  UInt  retval;
3312
3313  if((fp = fopen(pchFile,"r")) == (FILE*)NULL)
3314  {
3315    printf("can't open file %s :: set Default Matrix\n",pchFile);
3316    return true;
3317  }
3318
3319  for(sizeIdc = 0; sizeIdc < SCALING_LIST_SIZE_NUM; sizeIdc++)
3320  {
3321    size = min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeIdc]);
3322    for(listIdc = 0; listIdc < g_scalingListNum[sizeIdc]; listIdc++)
3323    {
3324      src = getScalingListAddress(sizeIdc, listIdc);
3325
3326      fseek(fp,0,0);
3327      do 
3328      {
3329        ret = fgets(line, 1024, fp);
3330        if ((ret==NULL)||(strstr(line, MatrixType[sizeIdc][listIdc])==NULL && feof(fp)))
3331        {
3332          printf("Error: can't read Matrix :: set Default Matrix\n");
3333          return true;
3334        }
3335      }
3336      while (strstr(line, MatrixType[sizeIdc][listIdc]) == NULL);
3337      for (i=0; i<size; i++)
3338      {
3339        retval = fscanf(fp, "%d,", &data);
3340        if (retval!=1)
3341        {
3342          printf("Error: can't read Matrix :: set Default Matrix\n");
3343          return true;
3344        }
3345        src[i] = data;
3346      }
3347      //set DC value for default matrix check
3348      setScalingListDC(sizeIdc,listIdc,src[0]);
3349
3350      if(sizeIdc > SCALING_LIST_8x8)
3351      {
3352        fseek(fp,0,0);
3353        do 
3354        {
3355          ret = fgets(line, 1024, fp);
3356          if ((ret==NULL)||(strstr(line, MatrixType_DC[sizeIdc][listIdc])==NULL && feof(fp)))
3357          {
3358            printf("Error: can't read DC :: set Default Matrix\n");
3359            return true;
3360          }
3361        }
3362        while (strstr(line, MatrixType_DC[sizeIdc][listIdc]) == NULL);
3363        retval = fscanf(fp, "%d,", &data);
3364        if (retval!=1)
3365        {
3366          printf("Error: can't read Matrix :: set Default Matrix\n");
3367          return true;
3368        }
3369        //overwrite DC value when size of matrix is larger than 16x16
3370        setScalingListDC(sizeIdc,listIdc,data);
3371      }
3372    }
3373  }
3374  fclose(fp);
3375  return false;
3376}
3377
3378#if H_MV
3379Void TComScalingList::inferFrom( TComScalingList* srcScLi )
3380{
3381  for(Int sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
3382  {
3383    for(Int listId = 0; listId <  g_scalingListNum[sizeId]; listId++)
3384    {
3385      setRefMatrixId  (sizeId,listId, srcScLi->getRefMatrixId  (sizeId,listId));
3386      setScalingListDC(sizeId,listId, srcScLi->getScalingListDC(sizeId,listId));         
3387      ::memcpy(getScalingListAddress(sizeId, listId),srcScLi->getScalingListAddress(sizeId, listId),sizeof(Int)*min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId]));
3388    }
3389  }
3390}
3391#endif
3392/** initialization process of quantization matrix array
3393 */
3394Void TComScalingList::init()
3395{
3396  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
3397  {
3398    for(UInt listId = 0; listId < g_scalingListNum[sizeId]; listId++)
3399    {
3400      m_scalingListCoef[sizeId][listId] = new Int [min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId])];
3401    }
3402  }
3403  m_scalingListCoef[SCALING_LIST_32x32][3] = m_scalingListCoef[SCALING_LIST_32x32][1]; // copy address for 32x32
3404}
3405
3406/** destroy quantization matrix array
3407 */
3408Void TComScalingList::destroy()
3409{
3410  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
3411  {
3412    for(UInt listId = 0; listId < g_scalingListNum[sizeId]; listId++)
3413    {
3414      if(m_scalingListCoef[sizeId][listId]) delete [] m_scalingListCoef[sizeId][listId];
3415    }
3416  }
3417}
3418
3419/** get default address of quantization matrix
3420 * \param sizeId size index
3421 * \param listId list index
3422 * \returns pointer of quantization matrix
3423 */
3424Int* TComScalingList::getScalingListDefaultAddress(UInt sizeId, UInt listId)
3425{
3426  Int *src = 0;
3427  switch(sizeId)
3428  {
3429    case SCALING_LIST_4x4:
3430      src = g_quantTSDefault4x4;
3431      break;
3432    case SCALING_LIST_8x8:
3433      src = (listId<3) ? g_quantIntraDefault8x8 : g_quantInterDefault8x8;
3434      break;
3435    case SCALING_LIST_16x16:
3436      src = (listId<3) ? g_quantIntraDefault8x8 : g_quantInterDefault8x8;
3437      break;
3438    case SCALING_LIST_32x32:
3439      src = (listId<1) ? g_quantIntraDefault8x8 : g_quantInterDefault8x8;
3440      break;
3441    default:
3442      assert(0);
3443      src = NULL;
3444      break;
3445  }
3446  return src;
3447}
3448
3449/** process of default matrix
3450 * \param sizeId size index
3451 * \param Index of input matrix
3452 */
3453Void TComScalingList::processDefaultMarix(UInt sizeId, UInt listId)
3454{
3455  ::memcpy(getScalingListAddress(sizeId, listId),getScalingListDefaultAddress(sizeId,listId),sizeof(Int)*min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId]));
3456  setScalingListDC(sizeId,listId,SCALING_LIST_DC);
3457}
3458
3459/** check DC value of matrix for default matrix signaling
3460 */
3461Void TComScalingList::checkDcOfMatrix()
3462{
3463  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
3464  {
3465    for(UInt listId = 0; listId < g_scalingListNum[sizeId]; listId++)
3466    {
3467      //check default matrix?
3468      if(getScalingListDC(sizeId,listId) == 0)
3469      {
3470        processDefaultMarix(sizeId, listId);
3471      }
3472    }
3473  }
3474}
3475
3476ParameterSetManager::ParameterSetManager()
3477: m_vpsMap(MAX_NUM_VPS)
3478, m_spsMap(MAX_NUM_SPS)
3479, m_ppsMap(MAX_NUM_PPS)
3480, m_activeVPSId(-1)
3481#if !H_MV
3482, m_activeSPSId(-1)
3483, m_activePPSId(-1)
3484{
3485#else
3486{
3487  for (Int i = 0; i < MAX_NUM_LAYERS; i++ )
3488  {
3489    m_activeSPSId[ i ] = -1; 
3490    m_activePPSId[ i ] = -1; 
3491  }
3492#endif
3493}
3494
3495
3496ParameterSetManager::~ParameterSetManager()
3497{
3498}
3499
3500//! activate a SPS from a active parameter sets SEI message
3501//! \returns true, if activation is successful
3502#if H_MV
3503Bool ParameterSetManager::activateSPSWithSEI(Int spsId, Int layerId )
3504#else
3505Bool ParameterSetManager::activateSPSWithSEI(Int spsId)
3506#endif
3507{
3508  TComSPS *sps = m_spsMap.getPS(spsId);
3509  if (sps)
3510  {
3511    Int vpsId = sps->getVPSId();
3512    if (m_vpsMap.getPS(vpsId))
3513    {
3514      m_activeVPSId = vpsId;
3515#if H_MV
3516      m_activeSPSId[ layerId ] = spsId;
3517#else
3518      m_activeSPSId = spsId;
3519#endif
3520      return true;
3521    }
3522    else
3523    {
3524      printf("Warning: tried to activate SPS using an Active parameter sets SEI message. Referenced VPS does not exist.");
3525    }
3526  }
3527  else
3528  {
3529    printf("Warning: tried to activate non-existing SPS using an Active parameter sets SEI message.");
3530  }
3531  return false;
3532}
3533
3534//! activate a PPS and depending on isIDR parameter also SPS and VPS
3535//! \returns true, if activation is successful
3536#if H_MV
3537Bool ParameterSetManager::activatePPS(Int ppsId, Bool isIRAP, Int layerId )
3538#else
3539Bool ParameterSetManager::activatePPS(Int ppsId, Bool isIRAP)
3540#endif
3541{
3542  TComPPS *pps = m_ppsMap.getPS(ppsId);
3543  if (pps)
3544  {
3545    Int spsId = pps->getSPSId();
3546#if H_MV
3547    if (!isIRAP && (spsId != m_activeSPSId[ layerId ]))
3548#else
3549    if (!isIRAP && (spsId != m_activeSPSId))
3550#endif
3551    {
3552      printf("Warning: tried to activate PPS referring to a inactive SPS at non-IRAP.");
3553      return false;
3554    }
3555
3556    TComSPS *sps = m_spsMap.getPS(spsId);
3557    if (sps)
3558    {
3559      Int vpsId = sps->getVPSId();
3560      if (!isIRAP && (vpsId != m_activeVPSId))
3561      {
3562        printf("Warning: tried to activate PPS referring to a inactive VPS at non-IRAP.");
3563        return false;
3564      }
3565      if (m_vpsMap.getPS(vpsId))
3566      {
3567#if H_MV
3568        m_activePPSId[ layerId ] = ppsId;
3569        m_activeVPSId = vpsId;
3570        m_activeSPSId[ layerId ] = spsId;
3571#else
3572        m_activePPSId = ppsId;
3573        m_activeVPSId = vpsId;
3574        m_activeSPSId = spsId;
3575#endif
3576        return true;
3577      }
3578      else
3579      {
3580        printf("Warning: tried to activate PPS that refers to a non-existing VPS.");
3581      }
3582    }
3583    else
3584    {
3585      printf("Warning: tried to activate a PPS that refers to a non-existing SPS.");
3586    }
3587  }
3588  else
3589  {
3590    printf("Warning: tried to activate non-existing PPS.");
3591  }
3592  return false;
3593}
3594
3595ProfileTierLevel::ProfileTierLevel()
3596  : m_profileSpace    (0)
3597  , m_tierFlag        (false)
3598  , m_profileIdc      (0)
3599  , m_levelIdc        (0)
3600, m_progressiveSourceFlag  (false)
3601, m_interlacedSourceFlag   (false)
3602, m_nonPackedConstraintFlag(false)
3603, m_frameOnlyConstraintFlag(false)
3604{
3605  ::memset(m_profileCompatibilityFlag, 0, sizeof(m_profileCompatibilityFlag));
3606}
3607
3608TComPTL::TComPTL()
3609{
3610  ::memset(m_subLayerProfilePresentFlag, 0, sizeof(m_subLayerProfilePresentFlag));
3611  ::memset(m_subLayerLevelPresentFlag,   0, sizeof(m_subLayerLevelPresentFlag  ));
3612}
3613
3614#if H_MV
3615Void TComPTL::copyLevelFrom( TComPTL* source )
3616{
3617  getGeneralPTL()->setLevelIdc( source->getGeneralPTL()->getLevelIdc() );
3618  for( Int subLayer = 0; subLayer < 6; subLayer++ )
3619  {
3620    setSubLayerLevelPresentFlag( subLayer, source->getSubLayerLevelPresentFlag( subLayer ) );
3621    getSubLayerPTL( subLayer )->setLevelIdc( source->getSubLayerPTL( subLayer )->getLevelIdc() );
3622  }
3623}
3624#endif
3625//! \}
3626
3627#if H_MV
3628TComVPSVUI::TComVPSVUI()
3629{
3630#if H_MV_6_PS_O0223_29
3631  m_crossLayerIrapAlignedFlag = true; 
3632#endif
3633  m_bitRatePresentVpsFlag = false;
3634  m_picRatePresentVpsFlag = false;
3635  for ( Int i = 0; i < MAX_VPS_OP_SETS_PLUS1; i++)
3636  {   
3637    for ( Int j = 0; j < MAX_TLAYER; j++)
3638    {   
3639      m_bitRatePresentFlag          [i][j] = false;
3640      m_picRatePresentFlag          [i][j] = false;
3641      m_avgBitRate                  [i][j] = -1;
3642      m_maxBitRate                  [i][j] = -1;
3643      m_constantPicRateIdc          [i][j] = -1;
3644      m_avgPicRate                  [i][j] = -1;
3645    }
3646  }
3647
3648  m_ilpRestrictedRefLayersFlag = false;
3649
3650  for ( Int i = 0; i < MAX_NUM_LAYERS; i++)
3651  {         
3652    for ( Int j = 0; j < MAX_NUM_LAYERS; j++)
3653    {   
3654      m_tileBoundariesAlignedFlag   [i][j] = false;
3655      m_minSpatialSegmentOffsetPlus1[i][j] = 0;
3656      m_ctuBasedOffsetEnabledFlag   [i][j] = false;
3657      m_minHorizontalCtuOffsetPlus1 [i][j] = -1;
3658    }
3659  }
3660#if H_MV_6_PS_O0118_33 
3661  for ( Int i = 0; i < MAX_NUM_VIDEO_SIGNAL_INFO; i++ )
3662  {
3663    m_videoSignalInfo          [i] = NULL;     
3664  }
3665#endif
3666
3667#if H_MV_6_HRD_O0164_15
3668  m_vpsVuiBspHrdPresentFlag = false; 
3669  m_vpsVuiBspHrdParameters  = new TComVpsVuiBspHrdParameters();
3670#endif
3671}
3672
3673#if H_MV_6_PS_O0118_33
3674TComVPSVUI::~TComVPSVUI()
3675{
3676  for ( Int i = 0; i < MAX_NUM_VIDEO_SIGNAL_INFO; i++ )
3677  {
3678    if (m_videoSignalInfo[ i ] != NULL )      delete m_videoSignalInfo[ i ];   
3679    m_videoSignalInfo    [ i ] = NULL; 
3680  }
3681
3682#if H_MV_6_HRD_O0164_15
3683  if ( m_vpsVuiBspHrdParameters ) delete m_vpsVuiBspHrdParameters;
3684  m_vpsVuiBspHrdParameters = NULL; 
3685#endif
3686}
3687#endif
3688
3689#if H_MV_6_PS_REP_FORM_18_19_20
3690Void TComRepFormat::inferChromaAndBitDepth( TComRepFormat* prevRepFormat, Bool encoderFlag )
3691{
3692  if ( !encoderFlag )
3693  {
3694    setChromaAndBitDepthVpsPresentFlag( prevRepFormat->getChromaAndBitDepthVpsPresentFlag() );
3695    setSeparateColourPlaneVpsFlag     ( prevRepFormat->getSeparateColourPlaneVpsFlag     () );
3696    setBitDepthVpsLumaMinus8          ( prevRepFormat->getBitDepthVpsLumaMinus8          () );
3697    setBitDepthVpsChromaMinus8        ( prevRepFormat->getBitDepthVpsChromaMinus8        () );
3698  }
3699  else
3700  {
3701    assert( getChromaAndBitDepthVpsPresentFlag() == prevRepFormat->getChromaAndBitDepthVpsPresentFlag() );
3702    assert( getSeparateColourPlaneVpsFlag     () == prevRepFormat->getSeparateColourPlaneVpsFlag     () );
3703    assert( getBitDepthVpsLumaMinus8          () == prevRepFormat->getBitDepthVpsLumaMinus8          () );
3704    assert( getBitDepthVpsChromaMinus8        () == prevRepFormat->getBitDepthVpsChromaMinus8        () );
3705  }
3706}
3707#endif
3708
3709#if H_MV_6_HRD_O0164_15
3710Void TComVpsVuiBspHrdParameters::checkLayerInBspFlag( TComVPS* vps, Int h )
3711{
3712  // It is a requirement of bitstream conformance that bitstream partition with index j shall not include
3713  // direct or indirect reference layers of any layers in bitstream partition i for any values of i and j
3714  // in the range of 0 to num_bitstream_partitions[ h ] – 1, inclusive, such that i is less than j.
3715
3716  for ( Int partJ = 0; partJ < getNumBitstreamPartitions( h ); partJ++ )
3717  {       
3718    for ( Int partI = 0; partI < partJ; partI++ )
3719    {
3720      for ( Int layerJ = 0; layerJ < vps->getMaxLayersMinus1(); layerJ++ )
3721      {
3722        if ( m_layerInBspFlag[ h ][partJ][layerJ ] )
3723        {
3724          for ( Int layerI = 0; layerI < vps->getMaxLayersMinus1(); layerI++ )
3725          {
3726            if ( m_layerInBspFlag[ h ][partI][layerI] )
3727            {
3728              assert( !vps->getInDirectDependencyFlag( layerI, layerJ ) ); 
3729            }
3730          }
3731        }
3732      }
3733    }
3734  }
3735}
3736#endif
3737#endif
Note: See TracBrowser for help on using the repository browser.