source: 3DVCSoftware/trunk/source/Lib/TLibCommon/TComSlice.cpp @ 738

Last change on this file since 738 was 738, checked in by tech, 10 years ago

Merged HTM-9.0-dev0@731. (MV-HEVC 6 HLS)

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