source: 3DVCSoftware/branches/HTM-11.2-dev0/source/Lib/TLibCommon/TComSlice.cpp @ 1035

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

Fix ticket #77 Unused functions.

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