source: 3DVCSoftware/branches/HTM-11.2-dev3-Sharp/source/Lib/TLibCommon/TComSlice.cpp @ 1231

Last change on this file since 1231 was 1000, checked in by sharpjp-htm, 11 years ago

Fix narrowing conversion of NumPocStCurr0,NumPocStCurr1 at TComSlice.cpp

  • Property svn:eol-style set to native
File size: 132.2 KB
Line 
1/* The copyright in this software is being made available under the BSD
2 * License, included below. This software may be subject to other third party
3 * and contributor rights, including patent rights, and no such rights are
4 * granted under this license. 
5 *
6* Copyright (c) 2010-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    m_ivMvScalingFlag = true; 
1955#endif
1956
1957    for( Int j = 0; j < MAX_NUM_LAYERS; j++ )
1958    {
1959      m_directDependencyFlag[i][j] = false;
1960      m_directDependencyType[i][j] = -1; 
1961      m_refLayerId[i][j]           = -1; 
1962      m_maxTidIlRefPicsPlus1[i][j]  = 7;
1963    }
1964
1965    for( Int j = 0; j < MAX_NUM_SCALABILITY_TYPES; j++ )
1966    {
1967      m_dimensionId[i][j] = 0;
1968    }
1969#if H_3D_ARP
1970    m_uiUseAdvResPred[i]  = 0;
1971    m_uiARPStepNum[i]     = 1;
1972#endif
1973  }
1974  m_vpsVUI = new TComVPSVUI; 
1975  m_dpbSize = new TComDpbSize; 
1976
1977#if H_3D
1978  for( Int i = 0; i < MAX_NUM_LAYERS; i++ )
1979  {
1980#if H_3D_IV_MERGE
1981    m_ivMvPredFlag         [ i ] = false;
1982#if H_3D_SPIVMP
1983    m_iSubPULog2Size       [ i ] = 0;
1984#endif
1985#endif
1986#if H_3D_VSP
1987    m_viewSynthesisPredFlag[ i ] = false;
1988#endif
1989#if H_3D_NBDV_REF
1990    m_depthRefinementFlag  [ i ] = false;
1991#endif
1992#if H_3D_INTER_SDC
1993    m_bInterSDCFlag        [ i ] = false;
1994#endif
1995#if H_3D_DBBP
1996    m_dbbpFlag             [ i ] = false;
1997#endif
1998#if H_3D_IV_MERGE
1999    m_bMPIFlag             [ i ] = false;
2000#endif
2001  } 
2002#endif
2003#endif
2004}
2005
2006TComVPS::~TComVPS()
2007{
2008  if( m_hrdParameters    != NULL )     delete[] m_hrdParameters;
2009  if( m_hrdOpSetIdx      != NULL )     delete[] m_hrdOpSetIdx;
2010  if( m_cprmsPresentFlag != NULL )     delete[] m_cprmsPresentFlag;
2011#if H_MV
2012  if ( m_vpsVUI          != NULL )     delete m_vpsVUI; 
2013  if ( m_dpbSize         != NULL )     delete m_dpbSize; 
2014
2015  for( Int i = 0; i < MAX_NUM_LAYERS; i++ )
2016  {
2017    if (m_repFormat[ i ] != NULL )      delete m_repFormat[ i ];   
2018  }
2019#endif
2020#if H_3D
2021  deleteCamPars();
2022#endif
2023}
2024
2025#if H_MV
2026
2027Bool TComVPS::checkVPSExtensionSyntax()
2028{
2029  for( Int layer = 1; layer <= getMaxLayersMinus1(); layer++ )
2030  {
2031    // check layer_id_in_nuh constraint
2032    assert( getLayerIdInNuh( layer ) > getLayerIdInNuh( layer -1 ) );
2033  }
2034
2035  //The value of vps_num_rep_formats_minus1 shall be in the range of 0 to 255, inclusive.
2036  assert( getVpsNumRepFormatsMinus1() >= 0 ); 
2037  assert( getVpsNumRepFormatsMinus1() <= 255 ); 
2038
2039  return true; 
2040}
2041
2042Int TComVPS::getNumScalabilityTypes()
2043{
2044  return scalTypeToScalIdx( ScalabilityType(MAX_NUM_SCALABILITY_TYPES) );
2045}
2046
2047Int TComVPS::scalTypeToScalIdx( ScalabilityType scalType )
2048{
2049  assert( scalType >= 0 && scalType <= MAX_NUM_SCALABILITY_TYPES ); 
2050  assert( scalType == MAX_NUM_SCALABILITY_TYPES || getScalabilityMaskFlag( scalType ) );
2051  Int scalIdx = 0; 
2052  for( Int curScalType = 0; curScalType < scalType; curScalType++ )
2053  {
2054    scalIdx += ( getScalabilityMaskFlag( curScalType ) ? 1 : 0 );
2055
2056  }
2057
2058  return scalIdx; 
2059}
2060Void TComVPS::setScalabilityMaskFlag( UInt val )
2061{
2062  for ( Int scalType = 0; scalType < MAX_NUM_SCALABILITY_TYPES; scalType++ ) 
2063  {
2064    setScalabilityMaskFlag( scalType, ( val & (1 << scalType ) ) != 0 );
2065  }
2066}
2067
2068Void TComVPS::setRefLayers()
2069{
2070  for( Int i = 0; i  <= getMaxLayersMinus1(); i++ )
2071  {
2072    Int iNuhLId = getLayerIdInNuh( i ); 
2073    m_numDirectRefLayers[ iNuhLId ] = 0; 
2074    for( Int j = 0; j < i; j++ )
2075    {
2076      if( getDirectDependencyFlag(i , j) )
2077      {
2078        m_refLayerId[ iNuhLId ][m_numDirectRefLayers[ iNuhLId ]++ ] = getLayerIdInNuh( j );
2079      }
2080    }
2081  }
2082
2083  for (Int i = 0 ; i < MAX_NUM_LAYER_IDS; i++ )
2084  {
2085    m_numRefLayers[i] = 0; 
2086  }
2087
2088  for (Int currLayerId = 0; currLayerId <= 62; currLayerId++ ) 
2089  {
2090    for (Int i = 0 ; i < MAX_NUM_LAYER_IDS; i++ )
2091    {
2092      m_recursiveRefLayerFlag[currLayerId][i] = 0; 
2093    }
2094  }
2095
2096  for( Int i = 0; i  <=  getMaxLayersMinus1(); i++ )
2097  {
2098    Int iNuhLId = getLayerIdInNuh( i );
2099    xSetRefLayerFlags( iNuhLId );
2100    for( Int j = 0; j < 63; j++ )
2101    {
2102      m_numRefLayers[ iNuhLId ]  +=  m_recursiveRefLayerFlag[ iNuhLId ][ j ];
2103    }
2104  }
2105
2106  for( Int i = 0; i <= getMaxLayersMinus1(); i++ )  // Bug in spec "<" instead of "<="
2107  {
2108    Int iNuhLId = getLayerIdInNuh( i );
2109    Int predIdx = 0;
2110    for( Int j = iNuhLId + 1; j < 63; j++ )
2111    {
2112      if( m_recursiveRefLayerFlag[ j ][ iNuhLId ] )
2113      {
2114        m_predictedLayerId[ iNuhLId ][ predIdx++ ] = j;
2115      }
2116    }
2117    m_numPredictedLayers[ iNuhLId ] = predIdx;
2118  }
2119 
2120  Bool countedLayerIdxFlag[ MAX_NUM_LAYERS ]; 
2121  for( Int i = 0; i  <=  getMaxLayersMinus1(); i++ )
2122  {
2123    countedLayerIdxFlag[ i ] = 0;
2124  }
2125 
2126  for( Int i = 0, k = 0; i  <=  getMaxLayersMinus1(); i++ )
2127  {
2128    Int iNuhLId = getLayerIdInNuh( i );
2129    if( m_numDirectRefLayers[ iNuhLId ]  ==  0 )
2130    {
2131      m_treePartitionLayerIdList[ k ][ 0 ] = iNuhLId;
2132      m_numLayersInTreePartition[ k ]      = 1;
2133      for( Int j = 0; j < m_numPredictedLayers[ iNuhLId ]; j++ ) 
2134      {
2135        if( !countedLayerIdxFlag[ getLayerIdInVps( m_predictedLayerId[ iNuhLId ][ j ] ) ] )   
2136        {
2137          m_treePartitionLayerIdList[ k ][ m_numLayersInTreePartition[ k ] ] = m_predictedLayerId[ iNuhLId ][ j ];
2138          m_numLayersInTreePartition[ k ]++;
2139          countedLayerIdxFlag[ getLayerIdInVps( m_predictedLayerId[ iNuhLId ][ j ] ) ] = 1;
2140        }
2141      }
2142      k++;
2143    }
2144    m_numIndependentLayers = k;
2145  }
2146}
2147
2148Int TComVPS::getRefLayerId( Int layerIdInNuh, Int idx )
2149{
2150  assert( idx >= 0 && idx < m_numDirectRefLayers[layerIdInNuh] );     
2151  Int refLayerIdInNuh = m_refLayerId[ layerIdInNuh ][ idx ];   
2152  assert ( refLayerIdInNuh >= 0 ); 
2153  return refLayerIdInNuh;
2154}
2155
2156Int TComVPS::getScalabilityId( Int layerIdInVps, ScalabilityType scalType )
2157{
2158  return getScalabilityMaskFlag( scalType ) ? getDimensionId( layerIdInVps, scalTypeToScalIdx( scalType ) ) : 0;
2159}
2160
2161#if H_3D
2162Int TComVPS::getLayerIdInNuh( Int viewIndex, Bool depthFlag )
2163{
2164  Int foundLayerIdinNuh = -1; 
2165
2166  for (Int layerIdInVps = 0 ; layerIdInVps <= getMaxLayersMinus1(); layerIdInVps++ )
2167  {
2168    Int layerIdInNuh = getLayerIdInNuh( layerIdInVps ); 
2169    if( ( getViewIndex( layerIdInNuh ) == viewIndex ) && ( getDepthId( layerIdInNuh ) == ( depthFlag ? 1 : 0 ) )  )
2170    {
2171      foundLayerIdinNuh = layerIdInNuh; 
2172      break; 
2173    }
2174  }
2175#if !BUG_FIX_TK65
2176  assert( foundLayerIdinNuh != -1 ); 
2177#endif
2178  return foundLayerIdinNuh;
2179}
2180
2181Void TComVPS::createCamPars(Int iNumViews)
2182{
2183  Int i = 0, j = 0;
2184
2185  m_bCamParPresent = new Bool[ iNumViews ];
2186  m_bCamParInSliceHeader = new Bool[ iNumViews ];
2187
2188  m_aaaiCodedScale = new Int**[ iNumViews ];
2189  m_aaaiCodedOffset = new Int**[ iNumViews ];
2190  for ( i = 0; i < iNumViews ; i++ )
2191  {
2192    m_bCamParInSliceHeader[i] = false; 
2193    m_aaaiCodedScale[i] = new Int*[ 2 ];
2194    m_aaaiCodedOffset[i] = new Int*[ 2 ];
2195    for ( j = 0; j < 2; j++ )
2196    {
2197      m_aaaiCodedScale[i][j] = new Int[ MAX_NUM_LAYERS ];
2198      m_aaaiCodedOffset[i][j] = new Int[ MAX_NUM_LAYERS ];
2199      for ( Int k = 0; k < MAX_NUM_LAYERS; k++ )
2200      {
2201        m_aaaiCodedScale[i][j][k] = 0;
2202        m_aaaiCodedOffset[i][j][k] = 0;
2203      }
2204    }
2205  }
2206}
2207
2208Void TComVPS::deleteCamPars()
2209{
2210  Int iNumViews = getNumViews();
2211  Int i = 0, j = 0;
2212
2213  if ( m_bCamParPresent != NULL )
2214  {
2215    delete [] m_bCamParPresent;
2216  }
2217  if ( m_bCamParInSliceHeader != NULL )
2218  {
2219    delete [] m_bCamParInSliceHeader;
2220  }
2221
2222  if ( m_aaaiCodedScale != NULL )
2223  {
2224    for ( i = 0; i < iNumViews ; i++ )
2225    {
2226      for ( j = 0; j < 2; j++ )
2227      {
2228        delete [] m_aaaiCodedScale[i][j];
2229      }
2230      delete [] m_aaaiCodedScale[i];
2231    }
2232    delete [] m_aaaiCodedScale;
2233  }
2234
2235  if ( m_aaaiCodedOffset != NULL )
2236  {
2237    for ( i = 0; i < iNumViews ; i++ )
2238    {
2239      for ( j = 0; j < 2; j++ )
2240      {
2241        delete [] m_aaaiCodedOffset[i][j];
2242      }
2243      delete [] m_aaaiCodedOffset[i];
2244    }
2245    delete [] m_aaaiCodedOffset;
2246  }
2247}
2248
2249
2250Void
2251  TComVPS::initCamParaVPS( UInt uiViewIndex, Bool bCamParPresent, UInt uiCamParPrecision, Bool bCamParSlice, Int** aaiScale, Int** aaiOffset )
2252{
2253  AOT( uiViewIndex != 0 && !bCamParSlice && ( aaiScale == 0 || aaiOffset == 0 ) ); 
2254
2255  m_uiCamParPrecision = ( ( uiViewIndex != 0 )? uiCamParPrecision : 0 );
2256  m_bCamParPresent[ uiViewIndex ] = (( uiViewIndex != 0 )? bCamParPresent  : false );
2257  m_bCamParInSliceHeader[ uiViewIndex ]  = ( (uiViewIndex != 0)? bCamParSlice  : false );
2258
2259  if( !m_bCamParInSliceHeader[ uiViewIndex ] )
2260  {
2261    for( UInt uiBaseViewIndex = 0; uiBaseViewIndex < uiViewIndex; uiBaseViewIndex++ )
2262    {
2263      m_aaaiCodedScale [ uiViewIndex ][ 0 ][ uiBaseViewIndex ] = aaiScale [ uiBaseViewIndex ][     uiViewIndex ];
2264      m_aaaiCodedScale [ uiViewIndex ][ 1 ][ uiBaseViewIndex ] = aaiScale [     uiViewIndex ][ uiBaseViewIndex ];
2265      m_aaaiCodedOffset[ uiViewIndex ][ 0 ][ uiBaseViewIndex ] = aaiOffset[ uiBaseViewIndex ][     uiViewIndex ];
2266      m_aaaiCodedOffset[ uiViewIndex ][ 1 ][ uiBaseViewIndex ] = aaiOffset[     uiViewIndex ][ uiBaseViewIndex ];
2267    }
2268  }
2269}
2270
2271#endif // H_3D
2272
2273
2274Int TComVPS::xGetDimBitOffset( Int j )
2275{
2276  Int dimBitOffset = 0; 
2277  if ( getSplittingFlag() && j == getNumScalabilityTypes() )
2278  {
2279     dimBitOffset = 6; 
2280  }
2281  else
2282  {
2283    for (Int dimIdx = 0; dimIdx <= j-1; dimIdx++)
2284    {
2285      dimBitOffset += getDimensionIdLen( dimIdx ); 
2286    }
2287  }
2288  return dimBitOffset; 
2289}
2290
2291Int TComVPS::inferDimensionId( Int i, Int j )
2292{
2293    return ( ( getLayerIdInNuh( i ) & ( (1 << xGetDimBitOffset( j + 1 ) ) - 1) ) >> xGetDimBitOffset( j ) ); 
2294}
2295
2296Int TComVPS::inferLastDimsionIdLenMinus1()
2297{
2298  return ( 5 - xGetDimBitOffset( getNumScalabilityTypes() - 1 ) ); 
2299}
2300
2301Int TComVPS::getNumLayersInIdList( Int lsIdx )
2302{
2303  assert( lsIdx >= 0 ); 
2304  assert( lsIdx <= getVpsNumLayerSetsMinus1() ); 
2305  return (Int) m_layerSetLayerIdList[ lsIdx ].size(); 
2306}
2307
2308Int    TComVPS::getNumOutputLayerSets() 
2309{
2310  return getNumAddOlss() + getNumLayerSets(); 
2311}
2312
2313Int TComVPS::getNumViews()
2314{
2315  Int numViews = 1; 
2316  for( Int i = 0; i <=  getMaxLayersMinus1(); i++ )
2317  {
2318    Int lId = getLayerIdInNuh( i ); 
2319    if ( i > 0 && ( getViewIndex( lId ) != getScalabilityId( i - 1, VIEW_ORDER_INDEX ) ) )
2320    {
2321      numViews++; 
2322    }   
2323  }
2324
2325  return numViews;
2326}
2327
2328Bool TComVPS::getInDirectDependencyFlag( Int depLayeridInVps, Int refLayeridInVps, Int depth /*= 0 */ )
2329{
2330  assert( depth < 65 ); 
2331  Bool dependentFlag = getDirectDependencyFlag( depLayeridInVps, refLayeridInVps ); 
2332
2333  for( Int i = 0; i < depLayeridInVps && !dependentFlag; i++ )
2334  {
2335    if ( getDirectDependencyFlag( depLayeridInVps, i ) )
2336    {
2337      dependentFlag = getInDirectDependencyFlag( i, refLayeridInVps, depth++ ); 
2338    }
2339  }
2340  return dependentFlag;
2341}
2342
2343Void TComVPS::deriveLayerSetLayerIdList()
2344{
2345  m_layerSetLayerIdList.resize( getVpsNumLayerSetsMinus1() + 1 ); 
2346  for (Int i = 0; i <= getVpsNumLayerSetsMinus1(); i++ )
2347  {
2348    for( Int m = 0; m  <= getVpsMaxLayerId(); m++ )
2349    {
2350      if( getLayerIdIncludedFlag( i, m) ) 
2351      {
2352        m_layerSetLayerIdList[ i ].push_back( m );       
2353      }
2354    }
2355  }
2356}
2357
2358Void TComVPS::initTargetLayerIdLists()
2359{
2360  m_targetDecLayerIdLists.resize( getNumOutputLayerSets() ); 
2361  m_targetOptLayerIdLists.resize( getNumOutputLayerSets() ); 
2362}
2363
2364Void TComVPS::deriveTargetLayerIdList( Int i )
2365{ 
2366  Int lsIdx = olsIdxToLsIdx( i );     
2367 
2368  for( Int j = 0; j < getNumLayersInIdList( lsIdx ); j++ )
2369  {
2370    m_targetDecLayerIdLists[i].push_back( m_layerSetLayerIdList[ lsIdx ][ j ] ); 
2371    if( getOutputLayerFlag( i, j  ))
2372    {
2373      m_targetOptLayerIdLists[i].push_back( m_layerSetLayerIdList[ lsIdx ][ j ] );
2374    }
2375  } 
2376  assert( getNumOutputLayersInOutputLayerSet( i ) > 0 ); 
2377}
2378
2379Bool TComVPS::inferOutputLayerFlag( Int i, Int j )
2380{
2381  Bool outputLayerFlag; 
2382  switch ( getDefaultOutputLayerIdc( ) )
2383  {
2384  case 0:
2385    outputLayerFlag = true; 
2386    break; 
2387  case 1:
2388    outputLayerFlag = ( j == m_layerSetLayerIdList[ olsIdxToLsIdx( i ) ].size() - 1 ); 
2389    break;
2390  case 2:
2391    if ( i == 0 && j == 0)
2392    {     
2393      outputLayerFlag = true;  // This is a software only fix for a bug in the spec. In spec outputLayerFlag is neither present nor inferred.
2394    }
2395    else
2396    {
2397      assert( 0 ); 
2398    }
2399    break; 
2400  default:     
2401    assert( 0 );
2402    break; 
2403  }
2404  return outputLayerFlag;
2405}
2406
2407Int TComVPS::getMaxSubLayersInLayerSetMinus1( Int i )
2408{
2409  Int maxSLMinus1 = 0; 
2410  for( Int k = 0; k < getNumLayersInIdList( i ); k++ )
2411  {
2412    Int lId = m_layerSetLayerIdList[i][k];
2413    maxSLMinus1 = std::max( maxSLMinus1, getSubLayersVpsMaxMinus1( getLayerIdInVps( lId ) ));
2414  }
2415  return maxSLMinus1;
2416}
2417
2418Void TComVPS::inferDbpSizeLayerSetZero( TComSPS* sps, Bool encoder )
2419{
2420  for( Int j = 0; j <= getMaxSubLayersInLayerSetMinus1( 0 ); j++ )
2421  {
2422    Int maxDecPicBufferingMinus1 = sps->getMaxDecPicBuffering( j ) - 1; 
2423    Int numReorderPics           = sps->getNumReorderPics    ( j ); 
2424    Int maxLatencyIncreasePlus1  = sps->getMaxLatencyIncrease( j ); 
2425
2426    if ( encoder )
2427    {
2428      assert( getDpbSize()->getMaxVpsDecPicBufferingMinus1(0, 0, j ) == maxDecPicBufferingMinus1 );
2429      assert( getDpbSize()->getMaxVpsNumReorderPics       (0,    j ) == numReorderPics           );
2430      assert( getDpbSize()->getMaxVpsLatencyIncreasePlus1 (0,    j ) == maxLatencyIncreasePlus1  );
2431    }
2432    else
2433    {
2434      getDpbSize()->setMaxVpsDecPicBufferingMinus1(0, 0, j, maxDecPicBufferingMinus1 );
2435      getDpbSize()->setMaxVpsNumReorderPics       (0,    j, numReorderPics           );
2436      getDpbSize()->setMaxVpsLatencyIncreasePlus1 (0,    j, maxLatencyIncreasePlus1  );       
2437    }     
2438  }
2439}
2440
2441Bool TComVPS::getAltOutputLayerFlagVar( Int i )
2442{
2443  // Semantics variable not syntax element !
2444
2445  Bool altOptLayerFlag = false;     
2446  if ( i > 0 && getNumOutputLayersInOutputLayerSet( i ) == 1 && 
2447    getNumDirectRefLayers( getOlsHighestOutputLayerId( i ) ) > 0 )
2448  {
2449    altOptLayerFlag = getAltOutputLayerFlag( i ); 
2450  }
2451  return altOptLayerFlag;
2452}
2453
2454Int TComVPS::getMaxNumPics( Int layerId )
2455{
2456  Int maxNumPics = MAX_INT; 
2457  for( Int olsIdx = 0; olsIdx < getNumOutputLayerSets(); olsIdx++)
2458  {
2459    Int lsIdx = olsIdxToLsIdx( olsIdx ); 
2460    for( Int j = 0; j < getNumLayersInIdList( lsIdx ); j++ )
2461    {
2462      if( getLayerSetLayerIdList(lsIdx, j ) ==  layerId )
2463      {
2464        Int maxSL = getMaxSubLayersInLayerSetMinus1( lsIdx ); 
2465        maxNumPics = std::min( maxNumPics, getDpbSize()->getMaxVpsDecPicBufferingMinus1( olsIdx , j,  maxSL ) );
2466      }
2467    }
2468  }
2469  assert( maxNumPics != MAX_INT ); 
2470  return maxNumPics;
2471}
2472
2473Void TComVPS::xSetRefLayerFlags( Int currLayerId )
2474{
2475  for( Int j = 0; j < getNumDirectRefLayers( currLayerId ); j++ )
2476  {
2477    Int refLayerId = m_refLayerId[ currLayerId ][ j ];
2478    m_recursiveRefLayerFlag[ currLayerId ][ refLayerId ] = 1;
2479    for( Int k = 0; k < MAX_NUM_LAYER_IDS; k++ )
2480    {
2481      m_recursiveRefLayerFlag[ currLayerId ][ k ] = m_recursiveRefLayerFlag[ currLayerId ][ k ]  ||  m_recursiveRefLayerFlag[ refLayerId ][ k ];
2482    }
2483  }
2484}
2485
2486#endif // H_MV
2487
2488// ------------------------------------------------------------------------------------------------
2489// Sequence parameter set (SPS)
2490// ------------------------------------------------------------------------------------------------
2491
2492TComSPS::TComSPS()
2493: m_SPSId                     (  0)
2494, m_VPSId                     (  0)
2495, m_chromaFormatIdc           (CHROMA_420)
2496, m_uiMaxTLayers              (  1)
2497// Structure
2498, m_picWidthInLumaSamples     (352)
2499, m_picHeightInLumaSamples    (288)
2500, m_log2MinCodingBlockSize    (  0)
2501, m_log2DiffMaxMinCodingBlockSize (0)
2502, m_uiMaxCUWidth              ( 32)
2503, m_uiMaxCUHeight             ( 32)
2504, m_uiMaxCUDepth              (  3)
2505, m_bLongTermRefsPresent      (false)
2506, m_uiQuadtreeTULog2MaxSize   (  0)
2507, m_uiQuadtreeTULog2MinSize   (  0)
2508, m_uiQuadtreeTUMaxDepthInter (  0)
2509, m_uiQuadtreeTUMaxDepthIntra (  0)
2510// Tool list
2511, m_usePCM                   (false)
2512, m_pcmLog2MaxSize            (  5)
2513, m_uiPCMLog2MinSize          (  7)
2514#if H_3D_QTLPC
2515, m_bUseQTL                   (false)
2516, m_bUsePC                    (false)
2517#endif
2518, m_bitDepthY                 (  8)
2519, m_bitDepthC                 (  8)
2520, m_qpBDOffsetY               (  0)
2521, m_qpBDOffsetC               (  0)
2522, m_uiPCMBitDepthLuma         (  8)
2523, m_uiPCMBitDepthChroma       (  8)
2524, m_bPCMFilterDisableFlag     (false)
2525, m_uiBitsForPOC              (  8)
2526, m_numLongTermRefPicSPS    (  0) 
2527, m_uiMaxTrSize               ( 32)
2528, m_bUseSAO                   (false) 
2529, m_bTemporalIdNestingFlag    (false)
2530, m_scalingListEnabledFlag    (false)
2531, m_useStrongIntraSmoothing   (false)
2532, m_vuiParametersPresentFlag  (false)
2533, m_vuiParameters             ()
2534#if H_MV
2535, m_pcVPS                     ( NULL )
2536, m_spsInferScalingListFlag   ( false )
2537, m_spsScalingListRefLayerId  ( 0 )
2538
2539, m_updateRepFormatFlag       ( false ) 
2540, m_spsRepFormatIdx           ( 0 )
2541, m_interViewMvVertConstraintFlag (false)
2542#endif
2543#if H_3D
2544, m_bCamParInSliceHeader      (false)
2545#endif
2546{
2547  for ( Int i = 0; i < MAX_TLAYER; i++ )
2548  {
2549    m_uiMaxLatencyIncrease[i] = 0;
2550    m_uiMaxDecPicBuffering[i] = 1;
2551    m_numReorderPics[i]       = 0;
2552  }
2553  m_scalingList = new TComScalingList;
2554  ::memset(m_ltRefPicPocLsbSps, 0, sizeof(m_ltRefPicPocLsbSps));
2555  ::memset(m_usedByCurrPicLtSPSFlag, 0, sizeof(m_usedByCurrPicLtSPSFlag));
2556#if H_MV
2557  m_spsRangeExtensionsFlag     = false;
2558  m_spsMultilayerExtensionFlag = false;
2559#if H_3D
2560  m_spsExtension5bits          = 0;
2561  m_sps3dExtensionFlag         = false; 
2562#else
2563  m_spsExtension6bits          = 0;
2564#endif
2565
2566  m_numScaledRefLayerOffsets = 0; 
2567
2568  for (Int i = 0; i < MAX_NUM_SCALED_REF_LAYERS; i++ )
2569  {
2570    m_scaledRefLayerId             [i] = -1;
2571  }
2572
2573  for (Int i = 0; i < MAX_NUM_LAYERS; i++ )
2574  {
2575    m_scaledRefLayerLeftOffset     [i] = 0;
2576    m_scaledRefLayerTopOffset      [i] = 0;
2577    m_scaledRefLayerRightOffset    [i] = 0;
2578    m_scaledRefLayerBottomOffset   [i] = 0;
2579  }
2580#endif
2581}
2582
2583TComSPS::~TComSPS()
2584{
2585  delete m_scalingList;
2586  m_RPSList.destroy();
2587}
2588
2589Void  TComSPS::createRPSList( Int numRPS )
2590{ 
2591  m_RPSList.destroy();
2592  m_RPSList.create(numRPS);
2593}
2594
2595Void TComSPS::setHrdParameters( UInt frameRate, UInt numDU, UInt bitRate, Bool randomAccess )
2596{
2597  if( !getVuiParametersPresentFlag() )
2598  {
2599    return;
2600  }
2601
2602  TComVUI *vui = getVuiParameters();
2603  TComHRD *hrd = vui->getHrdParameters();
2604
2605  TimingInfo *timingInfo = vui->getTimingInfo();
2606  timingInfo->setTimingInfoPresentFlag( true );
2607  switch( frameRate )
2608  {
2609  case 24:
2610    timingInfo->setNumUnitsInTick( 1125000 );    timingInfo->setTimeScale    ( 27000000 );
2611    break;
2612  case 25:
2613    timingInfo->setNumUnitsInTick( 1080000 );    timingInfo->setTimeScale    ( 27000000 );
2614    break;
2615  case 30:
2616    timingInfo->setNumUnitsInTick( 900900 );     timingInfo->setTimeScale    ( 27000000 );
2617    break;
2618  case 50:
2619    timingInfo->setNumUnitsInTick( 540000 );     timingInfo->setTimeScale    ( 27000000 );
2620    break;
2621  case 60:
2622    timingInfo->setNumUnitsInTick( 450450 );     timingInfo->setTimeScale    ( 27000000 );
2623    break;
2624  default:
2625    timingInfo->setNumUnitsInTick( 1001 );       timingInfo->setTimeScale    ( 60000 );
2626    break;
2627  }
2628
2629  Bool rateCnt = ( bitRate > 0 );
2630  hrd->setNalHrdParametersPresentFlag( rateCnt );
2631  hrd->setVclHrdParametersPresentFlag( rateCnt );
2632
2633  hrd->setSubPicCpbParamsPresentFlag( ( numDU > 1 ) );
2634
2635  if( hrd->getSubPicCpbParamsPresentFlag() )
2636  {
2637    hrd->setTickDivisorMinus2( 100 - 2 );                          //
2638    hrd->setDuCpbRemovalDelayLengthMinus1( 7 );                    // 8-bit precision ( plus 1 for last DU in AU )
2639    hrd->setSubPicCpbParamsInPicTimingSEIFlag( true );
2640    hrd->setDpbOutputDelayDuLengthMinus1( 5 + 7 );                 // With sub-clock tick factor of 100, at least 7 bits to have the same value as AU dpb delay
2641  }
2642  else
2643  {
2644    hrd->setSubPicCpbParamsInPicTimingSEIFlag( false ); 
2645  }
2646
2647  hrd->setBitRateScale( 4 );                                       // in units of 2~( 6 + 4 ) = 1,024 bps
2648  hrd->setCpbSizeScale( 6 );                                       // in units of 2~( 4 + 4 ) = 1,024 bit
2649  hrd->setDuCpbSizeScale( 6 );                                       // in units of 2~( 4 + 4 ) = 1,024 bit
2650 
2651  hrd->setInitialCpbRemovalDelayLengthMinus1(15);                  // assuming 0.5 sec, log2( 90,000 * 0.5 ) = 16-bit
2652  if( randomAccess )
2653  {
2654    hrd->setCpbRemovalDelayLengthMinus1(5);                        // 32 = 2^5 (plus 1)
2655    hrd->setDpbOutputDelayLengthMinus1 (5);                        // 32 + 3 = 2^6
2656  }
2657  else
2658  {
2659    hrd->setCpbRemovalDelayLengthMinus1(9);                        // max. 2^10
2660    hrd->setDpbOutputDelayLengthMinus1 (9);                        // max. 2^10
2661  }
2662
2663/*
2664   Note: only the case of "vps_max_temporal_layers_minus1 = 0" is supported.
2665*/
2666  Int i, j;
2667  UInt birateValue, cpbSizeValue;
2668  UInt ducpbSizeValue;
2669  UInt duBitRateValue = 0;
2670
2671  for( i = 0; i < MAX_TLAYER; i ++ )
2672  {
2673    hrd->setFixedPicRateFlag( i, 1 );
2674    hrd->setPicDurationInTcMinus1( i, 0 );
2675    hrd->setLowDelayHrdFlag( i, 0 );
2676    hrd->setCpbCntMinus1( i, 0 );
2677
2678    birateValue  = bitRate;
2679    cpbSizeValue = bitRate;                                     // 1 second
2680    ducpbSizeValue = bitRate/numDU;
2681    duBitRateValue = bitRate;
2682    for( j = 0; j < ( hrd->getCpbCntMinus1( i ) + 1 ); j ++ )
2683    {
2684      hrd->setBitRateValueMinus1( i, j, 0, ( birateValue  - 1 ) );
2685      hrd->setCpbSizeValueMinus1( i, j, 0, ( cpbSizeValue - 1 ) );
2686      hrd->setDuCpbSizeValueMinus1( i, j, 0, ( ducpbSizeValue - 1 ) );
2687      hrd->setCbrFlag( i, j, 0, ( j == 0 ) );
2688
2689      hrd->setBitRateValueMinus1( i, j, 1, ( birateValue  - 1) );
2690      hrd->setCpbSizeValueMinus1( i, j, 1, ( cpbSizeValue - 1 ) );
2691      hrd->setDuCpbSizeValueMinus1( i, j, 1, ( ducpbSizeValue - 1 ) );
2692      hrd->setDuBitRateValueMinus1( i, j, 1, ( duBitRateValue - 1 ) );
2693      hrd->setCbrFlag( i, j, 1, ( j == 0 ) );
2694    }
2695  }
2696}
2697const Int TComSPS::m_winUnitX[]={1,2,2,1};
2698const Int TComSPS::m_winUnitY[]={1,2,1,1};
2699
2700TComPPS::TComPPS()
2701: m_PPSId                       (0)
2702, m_SPSId                       (0)
2703, m_picInitQPMinus26            (0)
2704, m_useDQP                      (false)
2705, m_bConstrainedIntraPred       (false)
2706, m_bSliceChromaQpFlag          (false)
2707, m_pcSPS                       (NULL)
2708, m_uiMaxCuDQPDepth             (0)
2709, m_uiMinCuDQPSize              (0)
2710, m_chromaCbQpOffset            (0)
2711, m_chromaCrQpOffset            (0)
2712, m_numRefIdxL0DefaultActive    (1)
2713, m_numRefIdxL1DefaultActive    (1)
2714, m_TransquantBypassEnableFlag  (false)
2715, m_useTransformSkip             (false)
2716, m_dependentSliceSegmentsEnabledFlag    (false)
2717, m_tilesEnabledFlag               (false)
2718, m_entropyCodingSyncEnabledFlag   (false)
2719, m_loopFilterAcrossTilesEnabledFlag  (true)
2720, m_uniformSpacingFlag           (0)
2721, m_iNumColumnsMinus1            (0)
2722, m_puiColumnWidth               (NULL)
2723, m_iNumRowsMinus1               (0)
2724, m_puiRowHeight                 (NULL)
2725, m_iNumSubstreams             (1)
2726, m_signHideFlag(0)
2727, m_cabacInitPresentFlag        (false)
2728, m_encCABACTableIdx            (I_SLICE)
2729, m_sliceHeaderExtensionPresentFlag    (false)
2730, m_loopFilterAcrossSlicesEnabledFlag (false)
2731, m_listsModificationPresentFlag(  0)
2732, m_numExtraSliceHeaderBits(0)
2733#if H_MV
2734, m_ppsInferScalingListFlag(false)
2735, m_ppsScalingListRefLayerId(0)
2736, m_pocResetInfoPresentFlag(false)
2737#if H_3D
2738, m_pcDLT(NULL)
2739#endif
2740#endif
2741{
2742  m_scalingList = new TComScalingList;
2743
2744#if H_MV
2745  m_ppsRangeExtensionsFlag     = false;
2746  m_ppsMultilayerExtensionFlag = false;
2747#if !H_3D
2748  m_ppsExtension6bits          = 0;
2749#else
2750  m_pps3dExtensionFlag         = false;
2751  m_ppsExtension5bits          = 0;
2752#endif
2753#endif
2754}
2755
2756TComPPS::~TComPPS()
2757{
2758  if( m_iNumColumnsMinus1 > 0 && m_uniformSpacingFlag == 0 )
2759  {
2760    if (m_puiColumnWidth) delete [] m_puiColumnWidth; 
2761    m_puiColumnWidth = NULL;
2762  }
2763  if( m_iNumRowsMinus1 > 0 && m_uniformSpacingFlag == 0 )
2764  {
2765    if (m_puiRowHeight) delete [] m_puiRowHeight;
2766    m_puiRowHeight = NULL;
2767  }
2768  delete m_scalingList;
2769}
2770
2771#if H_3D
2772TComDLT::TComDLT()
2773: m_bDltPresentFlag(false)
2774, m_iNumDepthViews(0)
2775, m_uiDepthViewBitDepth(8)
2776{
2777  m_uiDepthViewBitDepth = g_bitDepthY; 
2778
2779  for( Int i = 0; i < MAX_NUM_LAYERS; i++ )
2780  {
2781    m_bUseDLTFlag                 [i] = false;
2782    m_bInterViewDltPredEnableFlag [i] = false;
2783
2784    // allocate some memory and initialize with default mapping
2785    m_iNumDepthmapValues[i] = ((1 << m_uiDepthViewBitDepth)-1)+1;
2786    m_iBitsPerDepthValue[i] = numBitsForValue(m_iNumDepthmapValues[i]);
2787
2788    m_iDepthValue2Idx[i]    = (Int*) xMalloc(Int, m_iNumDepthmapValues[i]);
2789    m_iIdx2DepthValue[i]    = (Int*) xMalloc(Int, m_iNumDepthmapValues[i]);
2790
2791    //default mapping
2792    for (Int d=0; d<m_iNumDepthmapValues[i]; d++)
2793    {
2794      m_iDepthValue2Idx[i][d] = d;
2795      m_iIdx2DepthValue[i][d] = d;
2796    }
2797  }
2798}
2799
2800TComDLT::~TComDLT()
2801{
2802  for( Int i = 0; i < MAX_NUM_LAYERS; i++ )
2803  {
2804    if ( m_iDepthValue2Idx[i] != NULL ) 
2805    {
2806      xFree( m_iDepthValue2Idx[i] );
2807      m_iDepthValue2Idx[i] = NULL; 
2808    }
2809
2810    if ( m_iIdx2DepthValue[i] != NULL ) 
2811    {
2812      xFree( m_iIdx2DepthValue[i] );
2813      m_iIdx2DepthValue[i] = NULL; 
2814    }
2815  }
2816}
2817
2818Void TComDLT::setDepthLUTs(Int layerIdInVps, Int* idxToDepthValueTable, Int iNumDepthValues)
2819{
2820  if( idxToDepthValueTable == NULL || iNumDepthValues == 0 ) // default mapping only
2821    return;
2822
2823  // copy idx2DepthValue to internal array
2824  memcpy(m_iIdx2DepthValue[layerIdInVps], idxToDepthValueTable, iNumDepthValues*sizeof(UInt));
2825
2826  UInt uiMaxDepthValue = ((1 << g_bitDepthY)-1);
2827  for(Int p=0; p<=uiMaxDepthValue; p++)
2828  {
2829    Int iIdxDown    = 0;
2830    Int iIdxUp      = iNumDepthValues-1;
2831    Bool bFound     = false;
2832
2833    // iterate over indices to find lower closest depth
2834    Int i = 1;
2835    while(!bFound && i<iNumDepthValues)
2836    {
2837      if( m_iIdx2DepthValue[layerIdInVps][i] > p )
2838      {
2839        iIdxDown  = i-1;
2840        bFound    = true;
2841      }
2842
2843      i++;
2844    }
2845    // iterate over indices to find upper closest depth
2846    i = iNumDepthValues-2;
2847    bFound = false;
2848    while(!bFound && i>=0)
2849    {
2850      if( m_iIdx2DepthValue[layerIdInVps][i] < p )
2851      {
2852        iIdxUp  = i+1;
2853        bFound    = true;
2854      }
2855
2856      i--;
2857    }
2858
2859    // assert monotony
2860    assert(iIdxDown<=iIdxUp);
2861
2862    // assign closer depth value/idx
2863    if( abs(p-m_iIdx2DepthValue[layerIdInVps][iIdxDown]) < abs(p-m_iIdx2DepthValue[layerIdInVps][iIdxUp]) )
2864    {
2865      m_iDepthValue2Idx[layerIdInVps][p] = iIdxDown;
2866    }
2867    else
2868    {
2869      m_iDepthValue2Idx[layerIdInVps][p] = iIdxUp;
2870    }
2871
2872  }
2873
2874  // update DLT variables
2875  m_iNumDepthmapValues[layerIdInVps] = iNumDepthValues;
2876  m_iBitsPerDepthValue[layerIdInVps] = numBitsForValue(m_iNumDepthmapValues[layerIdInVps]);
2877}
2878
2879#if H_3D_DELTA_DLT
2880Void TComDLT::getDeltaDLT( Int layerIdInVps, Int* piDLTInRef, UInt uiDLTInRefNum, Int* piDeltaDLTOut, UInt *puiDeltaDLTOutNum )
2881{
2882  Bool abBM0[ 256 ];
2883  Bool abBM1[ 256 ];
2884 
2885  memset( abBM0, 0, sizeof( abBM0 ));
2886  memset( abBM1, 0, sizeof( abBM1 ));
2887 
2888  // convert reference DLT to bit string
2889  for( Int i = 0; i < uiDLTInRefNum; i++ )
2890  {
2891    abBM0[ piDLTInRef[ i ] ] = true;
2892  }
2893  // convert internal DLT to bit string
2894  for( Int i = 0; i < m_iNumDepthmapValues[ layerIdInVps ]; i++ )
2895  {
2896    abBM1[ m_iIdx2DepthValue[ layerIdInVps ][ i ] ] = true;
2897  }
2898 
2899  *puiDeltaDLTOutNum = 0;
2900  for( Int i = 0; i < 256; i++ )
2901  {
2902    if( abBM0[ i ] ^ abBM1[ i ] )
2903    {
2904      piDeltaDLTOut[ *puiDeltaDLTOutNum ] = i;
2905      *puiDeltaDLTOutNum = *puiDeltaDLTOutNum + 1;
2906    }
2907  }
2908}
2909
2910Void TComDLT::setDeltaDLT( Int layerIdInVps, Int* piDLTInRef, UInt uiDLTInRefNum, Int* piDeltaDLTIn, UInt uiDeltaDLTInNum )
2911{
2912  Bool abBM0[ 256 ];
2913  Bool abBM1[ 256 ];
2914 
2915  memset( abBM0, 0, sizeof( abBM0 ));
2916  memset( abBM1, 0, sizeof( abBM1 ));
2917 
2918  // convert reference DLT to bit string
2919  for( Int i = 0; i < uiDLTInRefNum; i++ )
2920  {
2921    abBM0[ piDLTInRef[ i ] ] = true;
2922  }
2923  // convert delta DLT to bit string
2924  for( Int i = 0; i < uiDeltaDLTInNum; i++ )
2925  {
2926    abBM1[ piDeltaDLTIn[ i ] ] = true;
2927  }
2928 
2929  Int aiIdx2DepthValue[256];
2930  UInt uiNumDepthValues = 0;
2931  memset( aiIdx2DepthValue, 0, sizeof( aiIdx2DepthValue ));
2932 
2933  for( Int i = 0; i < 256; i++ )
2934  {
2935    if( abBM0[ i ] ^ abBM1[ i ] )
2936    {
2937      aiIdx2DepthValue[ uiNumDepthValues++ ] = i;
2938    }
2939  }
2940 
2941  // update internal tables
2942  setDepthLUTs(layerIdInVps, aiIdx2DepthValue, uiNumDepthValues);
2943}
2944#endif
2945
2946#endif
2947
2948#if H_MV
2949Void TComSPS::inferRepFormat( TComVPS* vps, Int layerIdCurr )
2950{
2951  if ( layerIdCurr > 0 )
2952  { 
2953    Int            repFormatIdx = getUpdateRepFormatFlag() ?  getSpsRepFormatIdx() : vps->getVpsRepFormatIdx( vps->getLayerIdInVps( layerIdCurr ) ) ;
2954    TComRepFormat* repFormat    = vps->getRepFormat( repFormatIdx ); 
2955      setChromaFormatIdc( repFormat->getChromaFormatVpsIdc() );         
2956      //// ToDo: add when supported:
2957      // setSeperateColourPlaneFlag( repFormat->getSeparateColourPlaneVpsFlag() ) ;
2958
2959      setPicWidthInLumaSamples ( repFormat->getPicWidthVpsInLumaSamples()  ); 
2960      setPicHeightInLumaSamples( repFormat->getPicHeightVpsInLumaSamples() ); 
2961
2962      setBitDepthY             ( repFormat->getBitDepthVpsLumaMinus8()   + 8 ); 
2963      setQpBDOffsetY           ( (Int) (6*( getBitDepthY() - 8 )) );
2964
2965      setBitDepthC             ( repFormat->getBitDepthVpsChromaMinus8() + 8 ); 
2966      setQpBDOffsetC           ( (Int) (6* ( getBitDepthC() -8 ) ) );
2967    if ( getLayerId() > 0 && getUpdateRepFormatFlag() )
2968    {
2969      assert( getChromaFormatIdc()      <=  repFormat->getChromaFormatVpsIdc()         ); 
2970      //// ToDo: add when supported:
2971      // assert( getSeperateColourPlaneFlag() <=  repFormat->getSeparateColourPlaneVpsFlag() ) ;
2972
2973      assert( getPicWidthInLumaSamples()  <= repFormat->getPicWidthVpsInLumaSamples()    ); 
2974      assert( getPicHeightInLumaSamples() <= repFormat->getPicHeightVpsInLumaSamples()   ); 
2975
2976      assert( getBitDepthY()              <= repFormat->getBitDepthVpsLumaMinus8()   + 8 );         
2977      assert( getBitDepthC()              <= repFormat->getBitDepthVpsChromaMinus8() + 8 ); 
2978    }
2979  }
2980
2981  // Set conformance window
2982  Int scal = TComSPS::getWinUnitX( getChromaFormatIdc() ) ;
2983  getConformanceWindow().scaleOffsets( scal );
2984  getVuiParameters()->getDefaultDisplayWindow().scaleOffsets( scal );
2985}
2986
2987Void TComSPS::inferScalingList( TComSPS* spsSrc )
2988{
2989  if ( getSpsInferScalingListFlag() ) 
2990  {
2991    assert( spsSrc != NULL ); 
2992    assert( !spsSrc->getSpsInferScalingListFlag() );             
2993    getScalingList()->inferFrom( spsSrc->getScalingList() ); 
2994  }
2995}
2996
2997Void TComSPS::inferSpsMaxDecPicBufferingMinus1( TComVPS* vps, Int targetOptLayerSetIdx, Int currLayerId, Bool encoder )
2998{
2999  const std::vector<Int>& targetDecLayerIdList = vps->getTargetDecLayerIdList( vps->olsIdxToLsIdx( targetOptLayerSetIdx )); 
3000
3001  if (getLayerId() > 0 )
3002  {
3003    Int layerIdx = 0;         
3004    while (layerIdx < (Int) targetDecLayerIdList.size() )
3005    {
3006      if ( targetDecLayerIdList[layerIdx] == currLayerId )
3007      { 
3008        break; 
3009      }
3010      layerIdx++; 
3011    }
3012
3013    assert( layerIdx < (Int) targetDecLayerIdList.size() ); 
3014
3015    for (Int i = 0; i <= getSpsMaxSubLayersMinus1(); i++ ) 
3016    {
3017      Int maxDecPicBufferingMinus1 = vps->getDpbSize()->getMaxVpsDecPicBufferingMinus1( targetOptLayerSetIdx, layerIdx, i ) ; 
3018
3019      if ( encoder )
3020      {
3021        assert( getMaxDecPicBuffering( i ) - 1 == maxDecPicBufferingMinus1 ); 
3022      }
3023      else
3024      {
3025        setMaxDecPicBuffering(i, maxDecPicBufferingMinus1 + 1 ); 
3026      }
3027    }
3028  }
3029}
3030
3031Void TComSPS::checkRpsMaxNumPics( TComVPS* vps, Int currLayerId )
3032{
3033  // In spec, when rps is in SPS, nuh_layer_id of SPS is used instead
3034  // of nuh_layer_id of slice (currLayerId), this seems to be a bug.
3035
3036  for (Int i = 0; i < getRPSList()->getNumberOfReferencePictureSets(); i++ )
3037  {
3038    TComReferencePictureSet* rps = getRPSList()->getReferencePictureSet( i ); 
3039    if ( !rps->getInterRPSPrediction() )
3040    {
3041      rps->checkMaxNumPics( vps->getVpsExtensionFlag(), vps->getMaxNumPics( currLayerId ), 
3042        getLayerId(), getMaxDecPicBuffering( getSpsMaxSubLayersMinus1() ) - 1 ); 
3043    }
3044  }
3045}
3046
3047#endif
3048
3049TComReferencePictureSet::TComReferencePictureSet()
3050: m_numberOfPictures (0)
3051, m_numberOfNegativePictures (0)
3052, m_numberOfPositivePictures (0)
3053, m_numberOfLongtermPictures (0)
3054, m_interRPSPrediction (0) 
3055, m_deltaRIdxMinus1 (0)   
3056, m_deltaRPS (0) 
3057, m_numRefIdc (0) 
3058{
3059  ::memset( m_deltaPOC, 0, sizeof(m_deltaPOC) );
3060  ::memset( m_POC, 0, sizeof(m_POC) );
3061  ::memset( m_used, 0, sizeof(m_used) );
3062  ::memset( m_refIdc, 0, sizeof(m_refIdc) );
3063}
3064
3065TComReferencePictureSet::~TComReferencePictureSet()
3066{
3067}
3068
3069Void TComReferencePictureSet::setUsed(Int bufferNum, Bool used)
3070{
3071  m_used[bufferNum] = used;
3072}
3073
3074Void TComReferencePictureSet::setDeltaPOC(Int bufferNum, Int deltaPOC)
3075{
3076  m_deltaPOC[bufferNum] = deltaPOC;
3077}
3078
3079Void TComReferencePictureSet::setNumberOfPictures(Int numberOfPictures)
3080{
3081  m_numberOfPictures = numberOfPictures;
3082}
3083
3084Int TComReferencePictureSet::getUsed(Int bufferNum)
3085{
3086  return m_used[bufferNum];
3087}
3088
3089Int TComReferencePictureSet::getDeltaPOC(Int bufferNum)
3090{
3091  return m_deltaPOC[bufferNum];
3092}
3093
3094Int TComReferencePictureSet::getNumberOfPictures()
3095{
3096  return m_numberOfPictures;
3097}
3098
3099Int TComReferencePictureSet::getPOC(Int bufferNum)
3100{
3101  return m_POC[bufferNum];
3102}
3103
3104Void TComReferencePictureSet::setPOC(Int bufferNum, Int POC)
3105{
3106  m_POC[bufferNum] = POC;
3107}
3108
3109Bool TComReferencePictureSet::getCheckLTMSBPresent(Int bufferNum)
3110{
3111  return m_bCheckLTMSB[bufferNum];
3112}
3113
3114Void TComReferencePictureSet::setCheckLTMSBPresent(Int bufferNum, Bool b)
3115{
3116  m_bCheckLTMSB[bufferNum] = b;
3117}
3118
3119/** set the reference idc value at uiBufferNum entry to the value of iRefIdc
3120 * \param uiBufferNum
3121 * \param iRefIdc
3122 * \returns Void
3123 */
3124Void TComReferencePictureSet::setRefIdc(Int bufferNum, Int refIdc)
3125{
3126  m_refIdc[bufferNum] = refIdc;
3127}
3128
3129/** get the reference idc value at uiBufferNum
3130 * \param uiBufferNum
3131 * \returns Int
3132 */
3133Int  TComReferencePictureSet::getRefIdc(Int bufferNum)
3134{
3135  return m_refIdc[bufferNum];
3136}
3137
3138/** Sorts the deltaPOC and Used by current values in the RPS based on the deltaPOC values.
3139 *  deltaPOC values are sorted with -ve values before the +ve values.  -ve values are in decreasing order.
3140 *  +ve values are in increasing order.
3141 * \returns Void
3142 */
3143Void TComReferencePictureSet::sortDeltaPOC()
3144{
3145  // sort in increasing order (smallest first)
3146  for(Int j=1; j < getNumberOfPictures(); j++)
3147  { 
3148    Int deltaPOC = getDeltaPOC(j);
3149    Bool used = getUsed(j);
3150    for (Int k=j-1; k >= 0; k--)
3151    {
3152      Int temp = getDeltaPOC(k);
3153      if (deltaPOC < temp)
3154      {
3155        setDeltaPOC(k+1, temp);
3156        setUsed(k+1, getUsed(k));
3157        setDeltaPOC(k, deltaPOC);
3158        setUsed(k, used);
3159      }
3160    }
3161  }
3162  // flip the negative values to largest first
3163  Int numNegPics = getNumberOfNegativePictures();
3164  for(Int j=0, k=numNegPics-1; j < numNegPics>>1; j++, k--)
3165  { 
3166    Int deltaPOC = getDeltaPOC(j);
3167    Bool used = getUsed(j);
3168    setDeltaPOC(j, getDeltaPOC(k));
3169    setUsed(j, getUsed(k));
3170    setDeltaPOC(k, deltaPOC);
3171    setUsed(k, used);
3172  }
3173}
3174
3175/** Prints the deltaPOC and RefIdc (if available) values in the RPS.
3176 *  A "*" is added to the deltaPOC value if it is Used bu current.
3177 * \returns Void
3178 */
3179Void TComReferencePictureSet::printDeltaPOC()
3180{
3181  printf("DeltaPOC = { ");
3182  for(Int j=0; j < getNumberOfPictures(); j++)
3183  {
3184    printf("%d%s ", getDeltaPOC(j), (getUsed(j)==1)?"*":"");
3185  } 
3186  if (getInterRPSPrediction()) 
3187  {
3188    printf("}, RefIdc = { ");
3189    for(Int j=0; j < getNumRefIdc(); j++)
3190    {
3191      printf("%d ", getRefIdc(j));
3192    } 
3193  }
3194  printf("}\n");
3195}
3196#if H_MV
3197Void TComReferencePictureSet::checkMaxNumPics( Bool vpsExtensionFlag, Int maxNumPics, Int nuhLayerId, Int spsMaxDecPicBufferingMinus1 )
3198{
3199  assert( getNumberOfPictures() >= 0 ); 
3200  if ( nuhLayerId == 0 )
3201  {
3202    assert( getNumberOfPictures() <= spsMaxDecPicBufferingMinus1 ); 
3203  }
3204
3205  if ( vpsExtensionFlag )
3206  {
3207    assert( getNumberOfPictures() <= maxNumPics );
3208  }
3209}
3210#endif
3211
3212TComRPSList::TComRPSList()
3213:m_referencePictureSets (NULL)
3214{
3215}
3216
3217TComRPSList::~TComRPSList()
3218{
3219}
3220
3221Void TComRPSList::create( Int numberOfReferencePictureSets)
3222{
3223  m_numberOfReferencePictureSets = numberOfReferencePictureSets;
3224  m_referencePictureSets = new TComReferencePictureSet[numberOfReferencePictureSets];
3225}
3226
3227Void TComRPSList::destroy()
3228{
3229  if (m_referencePictureSets)
3230  {
3231    delete [] m_referencePictureSets;
3232  }
3233  m_numberOfReferencePictureSets = 0;
3234  m_referencePictureSets = NULL;
3235}
3236
3237
3238
3239TComReferencePictureSet* TComRPSList::getReferencePictureSet(Int referencePictureSetNum)
3240{
3241  return &m_referencePictureSets[referencePictureSetNum];
3242}
3243
3244Int TComRPSList::getNumberOfReferencePictureSets()
3245{
3246  return m_numberOfReferencePictureSets;
3247}
3248
3249Void TComRPSList::setNumberOfReferencePictureSets(Int numberOfReferencePictureSets)
3250{
3251  m_numberOfReferencePictureSets = numberOfReferencePictureSets;
3252}
3253
3254TComRefPicListModification::TComRefPicListModification()
3255: m_bRefPicListModificationFlagL0 (false)
3256, m_bRefPicListModificationFlagL1 (false)
3257{
3258  ::memset( m_RefPicSetIdxL0, 0, sizeof(m_RefPicSetIdxL0) );
3259  ::memset( m_RefPicSetIdxL1, 0, sizeof(m_RefPicSetIdxL1) );
3260}
3261
3262TComRefPicListModification::~TComRefPicListModification()
3263{
3264}
3265
3266TComScalingList::TComScalingList()
3267{
3268  init();
3269}
3270TComScalingList::~TComScalingList()
3271{
3272  destroy();
3273}
3274
3275/** set default quantization matrix to array
3276*/
3277Void TComSlice::setDefaultScalingList()
3278{
3279  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
3280  {
3281    for(UInt listId=0;listId<g_scalingListNum[sizeId];listId++)
3282    {
3283      getScalingList()->processDefaultMatrix(sizeId, listId);
3284    }
3285  }
3286}
3287/** check if use default quantization matrix
3288 * \returns true if use default quantization matrix in all size
3289*/
3290Bool TComSlice::checkDefaultScalingList()
3291{
3292  UInt defaultCounter=0;
3293
3294  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
3295  {
3296    for(UInt listId=0;listId<g_scalingListNum[sizeId];listId++)
3297    {
3298      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
3299     && ((sizeId < SCALING_LIST_16x16) || (getScalingList()->getScalingListDC(sizeId,listId) == 16))) // check DC value
3300      {
3301        defaultCounter++;
3302      }
3303    }
3304  }
3305  return (defaultCounter == (SCALING_LIST_NUM * SCALING_LIST_SIZE_NUM - 4)) ? false : true; // -4 for 32x32
3306}
3307
3308#if H_MV
3309Void TComSlice::createInterLayerReferencePictureSet( TComPicLists* ivPicLists, std::vector<TComPic*>& refPicSetInterLayer0, std::vector<TComPic*>& refPicSetInterLayer1 )
3310{
3311  refPicSetInterLayer0.clear(); 
3312  refPicSetInterLayer1.clear(); 
3313
3314  for( Int i = 0; i < getNumActiveRefLayerPics(); i++ ) 
3315  {
3316    Int layerIdRef = getRefPicLayerId( i ); 
3317    TComPic* picRef = ivPicLists->getPic( layerIdRef, getPOC() ) ; 
3318    assert ( picRef != 0 ); // There shall be no entry equal to "no reference picture" in RefPicSetInterLayer0 or RefPicSetInterLayer1.
3319
3320    picRef->getPicYuvRec()->extendPicBorder(); 
3321    picRef->setIsLongTerm( true );       
3322    picRef->getSlice(0)->setReferenced( true );       
3323
3324    Int viewIdCur  = getVPS()->getViewId( getLayerId() ); 
3325    Int viewIdZero = getVPS()->getViewId( 0 );
3326    Int viewIdRef  = getVPS()->getViewId( layerIdRef ); 
3327
3328    if (  ( viewIdCur <= viewIdZero && viewIdCur <= viewIdRef ) || ( viewIdCur >= viewIdZero && viewIdCur >= viewIdRef ) )
3329    {
3330      refPicSetInterLayer0.push_back( picRef ); 
3331    }
3332    else
3333    {
3334      refPicSetInterLayer1.push_back( picRef ); 
3335    }
3336    // Consider to check here:
3337    // "If the current picture is a RADL picture, there shall be no entry in the RefPicSetInterLayer0 and RefPicSetInterLayer1 that is a RASL picture. "   
3338    assert( picRef->getSlice(0)->getDiscardableFlag() == false ); // "There shall be no picture that has discardable_flag equal to 1 in RefPicSetInterLayer0 or RefPicSetInterLayer1".       
3339  }
3340}
3341
3342Void TComSlice::markIvRefPicsAsShortTerm( std::vector<TComPic*> refPicSetInterLayer0, std::vector<TComPic*> refPicSetInterLayer1 )
3343{
3344  // Mark as short-term
3345  for ( Int i = 0; i < refPicSetInterLayer0.size(); i++ ) 
3346  {
3347    refPicSetInterLayer0[i]->setIsLongTerm( false ); 
3348  }
3349
3350  for ( Int i = 0; i < refPicSetInterLayer1.size(); i++ ) 
3351  {
3352    refPicSetInterLayer1[i]->setIsLongTerm( false ); 
3353  }
3354
3355}
3356
3357Void TComSlice::printRefPicList()
3358{ 
3359  for ( Int li = 0; li < 2; li++)
3360  {   
3361    std::cout << std::endl << "RefPicListL" <<  li << ":" << std::endl; 
3362    for (Int rIdx = 0; rIdx <= (m_aiNumRefIdx[li]-1); rIdx ++)
3363    {     
3364      if (rIdx == 0 && li == 0) m_apcRefPicList[li][rIdx]->print( true );
3365       
3366      m_apcRefPicList[li][rIdx]->print( false );
3367    }
3368  }
3369}
3370
3371Void TComSlice::markCurrPic( TComPic* currPic )
3372{
3373  currPic->getSlice(0)->setReferenced( true ) ; 
3374  currPic->setIsLongTerm( false ); 
3375
3376  currPic->setReconMark( true );
3377  currPic->setPicOutputFlag( currPic->getSlice(0)->getPicOutputFlag() );
3378}
3379
3380Void TComSlice::setRefPicSetInterLayer( std::vector<TComPic*>* refPicSetInterLayer0, std::vector<TComPic*>* refPicSetInterLayer1 )
3381{
3382  m_refPicSetInterLayer0 = refPicSetInterLayer0; 
3383  m_refPicSetInterLayer1 = refPicSetInterLayer1; 
3384}
3385
3386TComPic* TComSlice::getPicFromRefPicSetInterLayer(Int setIdc, Int layerId )
3387{
3388  assert ( setIdc == 0 || setIdc == 1);   
3389  std::vector<TComPic*>* refPicSetInterLayer = ( setIdc == 0 ? m_refPicSetInterLayer0 : m_refPicSetInterLayer1);   
3390  assert( refPicSetInterLayer != 0 ); 
3391 
3392  TComPic* pcPic = NULL; 
3393  for ( Int i = 0; i < (*refPicSetInterLayer).size(); i++ )
3394  {
3395    if ((*refPicSetInterLayer)[ i ]->getLayerId() == layerId )
3396    {
3397      pcPic = (*refPicSetInterLayer)[ i ]; 
3398    }
3399  }
3400
3401  assert(pcPic != NULL); 
3402  return pcPic;
3403}
3404
3405
3406Int  TComSlice::getRefLayerPicFlag( Int i ) 
3407{
3408  TComVPS* vps = getVPS(); 
3409  Int refLayerIdx = vps->getLayerIdInVps( vps->getRefLayerId( getLayerId(), i ) ); 
3410
3411  Bool refLayerPicFlag = ( vps->getSubLayersVpsMaxMinus1( refLayerIdx ) >=  getTLayer() )  && ( getTLayer() == 0  ) &&
3412    ( vps->getMaxTidIlRefPicsPlus1( refLayerIdx, vps->getLayerIdInVps( getLayerId() )) > getTLayer() ); 
3413
3414  return refLayerPicFlag;       
3415}   
3416
3417Int TComSlice::getRefLayerPicIdc( Int j ) 
3418{ 
3419  Int refLayerPicIdc = -1; 
3420  Int curj = 0; 
3421  for( Int i = 0;  i < getVPS()->getNumDirectRefLayers( getLayerId()) ; i++ )
3422  {
3423    if( getRefLayerPicFlag( i ) )
3424    {
3425      if ( curj == j ) 
3426      {
3427        refLayerPicIdc = i;         
3428        break;
3429      }
3430      curj++; 
3431    }
3432  }
3433
3434  assert( curj == j ); 
3435  assert( refLayerPicIdc != -1 ); 
3436  return refLayerPicIdc; 
3437}
3438
3439Int  TComSlice::getNumRefLayerPics( )
3440{ 
3441  Int numRefLayerPics = 0; 
3442  for( Int i = 0;  i < getVPS()->getNumDirectRefLayers( getLayerId()) ; i++ )
3443  {
3444    numRefLayerPics += getRefLayerPicFlag( i ); 
3445  }
3446  return numRefLayerPics; 
3447}
3448
3449
3450
3451Int TComSlice::getNumActiveRefLayerPics()
3452{
3453  Int numActiveRefLayerPics; 
3454
3455  if( getLayerId() == 0 || getNumRefLayerPics() ==  0 )
3456  {
3457    numActiveRefLayerPics = 0; 
3458  }
3459  else if (getVPS()->getAllRefLayersActiveFlag() )
3460  {
3461    numActiveRefLayerPics = getNumRefLayerPics(); 
3462  }
3463  else if ( !getInterLayerPredEnabledFlag() )
3464  {
3465    numActiveRefLayerPics = 0; 
3466  }
3467  else if( getVPS()->getMaxOneActiveRefLayerFlag() || getVPS()->getNumDirectRefLayers( getLayerId() ) == 1 )
3468  {
3469    numActiveRefLayerPics = 1; 
3470  }
3471  else
3472  {
3473    numActiveRefLayerPics = getNumInterLayerRefPicsMinus1() + 1; 
3474  }
3475  return numActiveRefLayerPics;
3476}
3477
3478Int TComSlice::getRefPicLayerId( Int i )
3479{
3480  return getVPS()->getRefLayerId( getLayerId(), getInterLayerPredLayerIdc( i ) );
3481}
3482
3483#if H_3D_ARP
3484Void TComSlice::setARPStepNum( TComPicLists*ivPicLists )
3485{
3486  Bool tempRefPicInListsFlag = false;
3487  if(!getVPS()->getUseAdvRP(getLayerId()) || this->isIRAP())
3488  {
3489    m_nARPStepNum = 0;
3490  }
3491  else
3492  {
3493    setFirstTRefIdx (REF_PIC_LIST_0, -1);
3494    setFirstTRefIdx (REF_PIC_LIST_1, -1);
3495    for ( Int refListIdx = 0; refListIdx < ((m_eSliceType==B_SLICE) ? 2 : 1); refListIdx++ )
3496    {
3497      Int diffPOC=MAX_INT;
3498      Int idx=-1;
3499      for(Int i = 0; i < getNumRefIdx(RefPicList(refListIdx)); i++ )
3500      {
3501        if ( getRefPic(RefPicList(refListIdx), i)->getPOC() != getPOC() )
3502        {
3503          if( abs(getRefPic(RefPicList(refListIdx), i)->getPOC() - getPOC()) < diffPOC)
3504          {
3505            diffPOC=abs(getRefPic(RefPicList(refListIdx), i)->getPOC() - getPOC());
3506            idx=i;
3507          }
3508        }
3509        if(idx>=0)
3510        {
3511          setFirstTRefIdx (RefPicList(refListIdx), idx);
3512        }
3513      }
3514    }
3515    tempRefPicInListsFlag = getFirstTRefIdx(REF_PIC_LIST_0) >= 0 || getFirstTRefIdx(REF_PIC_LIST_1) >= 0;
3516    m_nARPStepNum = tempRefPicInListsFlag ? getVPS()->getARPStepNum(getLayerId()) : 0;
3517  }
3518
3519  if (tempRefPicInListsFlag)
3520  {
3521    for ( Int refListIdx = 0; refListIdx < ((m_eSliceType==B_SLICE) ? 2 : 1); refListIdx++ )
3522    {
3523      RefPicList eRefPicList = RefPicList( refListIdx );
3524      Int prevPOC = getRefPic(eRefPicList, getFirstTRefIdx(eRefPicList) )->getPOC();
3525      for( Int i = 0; i < getNumActiveRefLayerPics(); i++ )
3526      {
3527        Int layerIdInNuh = getRefPicLayerId( i );
3528        Int viewIdx = getVPS()->getViewId( layerIdInNuh );
3529        TComPic*pcPicPrev = ivPicLists->getPic(viewIdx, 0, prevPOC);
3530        if (getFirstTRefIdx(eRefPicList) >= 0 && pcPicPrev && pcPicPrev->getSlice( 0 )->isReferenced())
3531        {
3532          m_arpRefPicAvailable[eRefPicList][layerIdInNuh] = true;
3533        }
3534        else
3535        {
3536          m_arpRefPicAvailable[eRefPicList][layerIdInNuh] = false;
3537        }
3538      }
3539    }
3540  }
3541}
3542#endif
3543#if H_3D_IC
3544Void TComSlice::xSetApplyIC(Bool bUseLowLatencyICEnc)
3545{
3546  if(bUseLowLatencyICEnc)
3547  {
3548    Bool existInterViewRef=false;
3549    TComPic* pcCurrPic = getPic();
3550    TComPic* pcRefPic = NULL;
3551    for ( Int i = 0; (i < getNumRefIdx( REF_PIC_LIST_0 )) && !existInterViewRef; i++ )
3552    {
3553      pcRefPic = getRefPic( REF_PIC_LIST_0, i );
3554      if ( pcRefPic != NULL )
3555      {
3556        if ( pcCurrPic->getViewIndex() != pcRefPic->getViewIndex() )
3557        {
3558          existInterViewRef = true;       
3559        }
3560      }
3561    }
3562
3563    for ( Int i = 0; (i < getNumRefIdx( REF_PIC_LIST_1 )) && !existInterViewRef; i++ )
3564    {
3565      pcRefPic = getRefPic( REF_PIC_LIST_1, i );
3566      if ( pcRefPic != NULL )
3567      {
3568        if ( pcCurrPic->getViewIndex() != pcRefPic->getViewIndex() )
3569        {
3570          existInterViewRef = true;       
3571        }
3572      }
3573    }
3574
3575    if(!existInterViewRef)
3576    {
3577      m_bApplyIC = false;
3578    }
3579    else
3580    {
3581      Int curLayer=getDepth();
3582      if( curLayer>9) curLayer=9; // Max layer is 10
3583
3584      m_bApplyIC = true;
3585      Int refLayer = curLayer-1;
3586      if( (refLayer>=0) && (g_aICEnableCANDIDATE[refLayer]>0) )
3587      {   
3588        Double ratio=Double(g_aICEnableNUM[refLayer])/Double(g_aICEnableCANDIDATE[refLayer]);
3589
3590        if( ratio > IC_LOW_LATENCY_ENCODING_THRESHOLD)
3591{
3592          m_bApplyIC=true;
3593        }
3594        else
3595        {
3596          m_bApplyIC=false;
3597        }
3598      }
3599      g_aICEnableNUM[curLayer]=0;
3600      g_aICEnableCANDIDATE[curLayer]=0;
3601      g_lastlayer=getDepth();
3602    }
3603  }
3604  else
3605  {
3606  Int iMaxPelValue = ( 1 << g_bitDepthY ); 
3607  Int *aiRefOrgHist;
3608  Int *aiCurrHist;
3609  aiRefOrgHist = (Int *) xMalloc( Int,iMaxPelValue );
3610  aiCurrHist   = (Int *) xMalloc( Int,iMaxPelValue );
3611  memset( aiRefOrgHist, 0, iMaxPelValue*sizeof(Int) );
3612  memset( aiCurrHist, 0, iMaxPelValue*sizeof(Int) );
3613  // Reference Idx Number
3614  Int iNumRefIdx = getNumRefIdx( REF_PIC_LIST_0 );
3615  TComPic* pcCurrPic = NULL;
3616  TComPic* pcRefPic = NULL;
3617  TComPicYuv* pcCurrPicYuv = NULL;
3618  TComPicYuv* pcRefPicYuvOrg = NULL;
3619  pcCurrPic = getPic();
3620  pcCurrPicYuv = pcCurrPic->getPicYuvOrg();
3621  Int iWidth = pcCurrPicYuv->getWidth();
3622  Int iHeight = pcCurrPicYuv->getHeight();
3623
3624
3625  // Get InterView Reference picture
3626  // !!!!! Assume only one Interview Reference Picture in L0
3627  for ( Int i = 0; i < iNumRefIdx; i++ )
3628  {
3629    pcRefPic = getRefPic( REF_PIC_LIST_0, i );
3630    if ( pcRefPic != NULL )
3631    {
3632      if ( pcCurrPic->getViewIndex() != pcRefPic->getViewIndex() )
3633      {
3634        pcRefPicYuvOrg = pcRefPic->getPicYuvOrg();
3635      }
3636    }
3637  }
3638
3639  if ( pcRefPicYuvOrg != NULL )
3640  {
3641    Pel* pCurrY = pcCurrPicYuv ->getLumaAddr();
3642    Pel* pRefOrgY = pcRefPicYuvOrg  ->getLumaAddr();
3643    Int iCurrStride = pcCurrPicYuv->getStride();
3644    Int iRefStride = pcRefPicYuvOrg->getStride();
3645    Int iSumOrgSAD = 0;
3646    Double dThresholdOrgSAD = getIsDepth() ? 0.1 : 0.05;
3647
3648    // Histogram building - luminance
3649    for ( Int y = 0; y < iHeight; y++ )
3650    {
3651      for ( Int x = 0; x < iWidth; x++ )
3652      {
3653        aiCurrHist[pCurrY[x]]++;
3654        aiRefOrgHist[pRefOrgY[x]]++;
3655      }
3656      pCurrY += iCurrStride;
3657      pRefOrgY += iRefStride;
3658    }
3659    // Histogram SAD
3660    for ( Int i = 0; i < iMaxPelValue; i++ )
3661    {
3662      iSumOrgSAD += abs( aiCurrHist[i] - aiRefOrgHist[i] );
3663    }
3664    // Setting
3665    if ( iSumOrgSAD > Int( dThresholdOrgSAD * iWidth * iHeight ) )
3666    {
3667      m_bApplyIC = true;
3668    }
3669    else
3670    {
3671      m_bApplyIC = false;
3672    }
3673  }
3674
3675  xFree( aiCurrHist   );
3676  xFree( aiRefOrgHist );
3677  aiCurrHist = NULL;
3678  aiRefOrgHist = NULL;
3679  }//if(bUseLowLatencyICEnc)
3680}
3681#endif
3682#if H_3D
3683Void TComSlice::setIvPicLists( TComPicLists* m_ivPicLists )
3684{
3685  for (Int i = 0; i < MAX_NUM_LAYERS; i++ )
3686  {     
3687    for ( Int depthId = 0; depthId < 2; depthId++ )
3688    {
3689      m_ivPicsCurrPoc[ depthId ][ i ] = ( i <= m_viewIndex ) ? m_ivPicLists->getPic( i, ( depthId == 1) , getPOC() ) : NULL;
3690    }       
3691  } 
3692}
3693Void TComSlice::setDepthToDisparityLUTs()
3694{ 
3695  Bool setupLUT = false; 
3696  Int layerIdInVPS = getVPS()->getLayerIdInNuh( m_layerId ); 
3697
3698#if H_3D_VSP
3699  setupLUT = setupLUT || getVPS()->getViewSynthesisPredFlag( layerIdInVPS); 
3700#endif
3701
3702#if H_3D_NBDV_REF
3703  setupLUT = setupLUT || getVPS()->getDepthRefinementFlag( layerIdInVPS );
3704#endif
3705
3706#if H_3D_IV_MERGE
3707  setupLUT = setupLUT || ( getVPS()->getIvMvPredFlag(layerIdInVPS ) && getIsDepth() );
3708#endif
3709
3710#if H_3D_DDD
3711  if( getIsDepth() && getViewIndex() > 0 )
3712  {
3713      TComSlice *pcTextSlice = getTexturePic()->getSlice( 0 );
3714      memcpy( m_aiDDDInvScale, pcTextSlice->m_aiDDDInvScale, sizeof( Int ) * getViewIndex() );
3715      memcpy( m_aiDDDInvOffset, pcTextSlice->m_aiDDDInvOffset, sizeof( Int ) * getViewIndex() );
3716      memcpy( m_aiDDDShift, pcTextSlice->m_aiDDDShift, sizeof( Int ) * getViewIndex() );             
3717  } 
3718#endif
3719
3720  if( !setupLUT )
3721    return; 
3722
3723  /// GT: Allocation should be moved to a better place later;
3724  if ( m_depthToDisparityB == NULL )
3725  {
3726    m_depthToDisparityB = new Int*[ getViewIndex() ];
3727    for ( Int i = 0; i < getViewIndex(); i++ )
3728    {
3729      m_depthToDisparityB[ i ] = new Int[ Int(1 << g_bitDepthY) ]; 
3730    }
3731  }
3732
3733  if ( m_depthToDisparityF == NULL )
3734  {
3735    m_depthToDisparityF= new Int*[ getViewIndex() ];
3736    for ( Int i = 0; i < getViewIndex(); i++ )
3737    {
3738      m_depthToDisparityF[ i ] = new Int[ Int(1 << g_bitDepthY) ]; 
3739    }
3740  }
3741
3742  assert( m_depthToDisparityB != NULL ); 
3743  assert( m_depthToDisparityF != NULL ); 
3744
3745  TComVPS* vps = getVPS(); 
3746
3747  Int log2Div = g_bitDepthY - 1 + vps->getCamParPrecision();
3748  Int viewIndex = getViewIndex();
3749
3750  Bool camParaSH = vps->hasCamParInSliceHeader( viewIndex );
3751
3752  Int* codScale     = camParaSH ? m_aaiCodedScale [ 0 ] : vps->getCodedScale    ( viewIndex ); 
3753  Int* codOffset    = camParaSH ? m_aaiCodedOffset[ 0 ] : vps->getCodedOffset   ( viewIndex ); 
3754  Int* invCodScale  = camParaSH ? m_aaiCodedScale [ 1 ] : vps->getInvCodedScale ( viewIndex ); 
3755  Int* invCodOffset = camParaSH ? m_aaiCodedOffset[ 1 ] : vps->getInvCodedOffset( viewIndex ); 
3756
3757  for (Int i = 0; i <= ( getViewIndex() - 1); i++)
3758  {
3759    for ( Int d = 0; d <= ( ( 1 << g_bitDepthY ) - 1 ); d++ )
3760    {
3761      Int offset =    ( codOffset  [ i ] << g_bitDepthY ) + ( ( 1 << log2Div ) >> 1 );         
3762      m_depthToDisparityB[ i ][ d ] = ( codScale [ i ] * d + offset ) >> log2Div; 
3763
3764      Int invOffset = ( invCodOffset[ i ] << g_bitDepthY ) + ( ( 1 << log2Div ) >> 1 );         
3765      m_depthToDisparityF[ i ][ d ] = ( invCodScale[ i ] * d + invOffset ) >> log2Div; 
3766    }
3767
3768#if H_3D_DDD
3769    initializeDDDPara( vps->getCamParPrecision(), codScale[ i ], codOffset[ i ], i );
3770#endif
3771  }
3772}
3773#endif
3774#endif
3775
3776#if H_3D_DDD
3777Void TComSlice::initializeDDDPara( UInt uiCamParsCodedPrecision, Int  iCodedScale,Int  iCodedOffset, Int iBaseViewIdx )
3778{
3779    UInt uiViewId     = getViewIndex();
3780
3781    if( uiViewId == 0 )
3782    {
3783        m_aiDDDInvScale[ iBaseViewIdx ] = m_aiDDDInvOffset[ iBaseViewIdx ] = m_aiDDDShift[ iBaseViewIdx ] = 0;
3784        return;
3785    }
3786
3787
3788    Int iSign = iCodedScale >= 0 ? 1 : -1;
3789    iCodedScale = abs( iCodedScale );
3790
3791    Int iBitWidth = 0;
3792
3793    const Int iInvPres = 9;
3794
3795    while( ((( 1 << iBitWidth ) << 1 ) <= iCodedScale ) )
3796    {
3797        iBitWidth ++;
3798    }
3799    iBitWidth += iInvPres;
3800    Int iTargetValue =  1 << iBitWidth;
3801
3802    Int iMinError = MAX_INT;
3803    Int iBestD = 1 << ( iInvPres - 1 );
3804    for( Int d = 1 << ( iInvPres - 1 ); d < ( 1 << iInvPres ); d++ )
3805    {
3806        Int iError = abs( iCodedScale * d - iTargetValue );
3807        if( iError < iMinError )
3808        {
3809            iMinError = iError;
3810            iBestD = d;
3811        }
3812        if( iMinError == 0 )
3813        {
3814            break;
3815        }
3816    }
3817    Int iRoundingDir = 0;
3818    if( iCodedScale * iBestD > iTargetValue )
3819    {
3820        iRoundingDir = -1;
3821    }
3822    else if( iCodedScale * iBestD < iTargetValue )
3823    {
3824        iRoundingDir = 1;
3825    }
3826    Int iCamPres = uiCamParsCodedPrecision - 1;
3827    m_aiDDDInvScale [ iBaseViewIdx ] = ( iBestD << ( iCamPres + g_bitDepthY )) * iSign;
3828    m_aiDDDInvOffset[ iBaseViewIdx ] = -iSign * iBestD * ( iCodedOffset << g_bitDepthY );
3829    m_aiDDDShift    [ iBaseViewIdx ] = iBitWidth;
3830    m_aiDDDInvOffset[ iBaseViewIdx ] += 1 << ( m_aiDDDShift[ iBaseViewIdx ] - 1 );
3831    m_aiDDDInvOffset[ iBaseViewIdx ] += ( 1 << ( m_aiDDDShift[ iBaseViewIdx ] - 4 ) ) * iRoundingDir;
3832
3833    return;
3834}
3835
3836
3837#endif
3838
3839#if H_MV
3840Void TComSlice::checkCrossLayerBlaFlag()
3841{
3842  // 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.
3843  if ( getLayerId() != 0 || getNalUnitType() != NAL_UNIT_CODED_SLICE_IDR_W_RADL || getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_N_LP )
3844  {
3845    assert( m_crossLayerBlaFlag == 0 ); 
3846  }
3847}
3848
3849Bool TComSlice::inferPocMsbValPresentFlag()
3850{
3851  Bool pocMsbValPresentFlag; 
3852  if( getSliceSegmentHeaderExtensionLength() == 0 ) 
3853  {
3854    pocMsbValPresentFlag = false; 
3855  }
3856  else if ( getPocMsbValRequiredFlag() )
3857  {
3858    pocMsbValPresentFlag = true; 
3859  }
3860  else
3861  {
3862    pocMsbValPresentFlag = false; 
3863  }
3864
3865  return pocMsbValPresentFlag;
3866}
3867
3868
3869#endif
3870
3871#if H_3D_DBBP
3872Int TComSlice::getDepthFromDV( Int iDV, Int iBaseViewIdx )
3873{
3874  return ClipY(( iDV * m_aiDDDInvScale[ iBaseViewIdx ] + m_aiDDDInvOffset[ iBaseViewIdx ] ) >> m_aiDDDShift[ iBaseViewIdx ]);
3875}
3876#endif
3877
3878/** get scaling matrix from RefMatrixID
3879 * \param sizeId size index
3880 * \param Index of input matrix
3881 * \param Index of reference matrix
3882 */
3883Void TComScalingList::processRefMatrix( UInt sizeId, UInt listId , UInt refListId )
3884{
3885  ::memcpy(getScalingListAddress(sizeId, listId),((listId == refListId)? getScalingListDefaultAddress(sizeId, refListId): getScalingListAddress(sizeId, refListId)),sizeof(Int)*min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId]));
3886}
3887
3888/** parse syntax infomation
3889 *  \param pchFile syntax infomation
3890 *  \returns false if successful
3891 */
3892Bool TComScalingList::xParseScalingList(Char* pchFile)
3893{
3894  FILE *fp;
3895  Char line[1024];
3896  UInt sizeIdc,listIdc;
3897  UInt i,size = 0;
3898  Int *src=0,data;
3899  Char *ret;
3900  UInt  retval;
3901
3902  if((fp = fopen(pchFile,"r")) == (FILE*)NULL)
3903  {
3904    printf("can't open file %s :: set Default Matrix\n",pchFile);
3905    return true;
3906  }
3907
3908  for(sizeIdc = 0; sizeIdc < SCALING_LIST_SIZE_NUM; sizeIdc++)
3909  {
3910    size = min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeIdc]);
3911    for(listIdc = 0; listIdc < g_scalingListNum[sizeIdc]; listIdc++)
3912    {
3913      src = getScalingListAddress(sizeIdc, listIdc);
3914
3915      fseek(fp,0,0);
3916      do 
3917      {
3918        ret = fgets(line, 1024, fp);
3919        if ((ret==NULL)||(strstr(line, MatrixType[sizeIdc][listIdc])==NULL && feof(fp)))
3920        {
3921          printf("Error: can't read Matrix :: set Default Matrix\n");
3922          return true;
3923        }
3924      }
3925      while (strstr(line, MatrixType[sizeIdc][listIdc]) == NULL);
3926      for (i=0; i<size; i++)
3927      {
3928        retval = fscanf(fp, "%d,", &data);
3929        if (retval!=1)
3930        {
3931          printf("Error: can't read Matrix :: set Default Matrix\n");
3932          return true;
3933        }
3934        src[i] = data;
3935      }
3936      //set DC value for default matrix check
3937      setScalingListDC(sizeIdc,listIdc,src[0]);
3938
3939      if(sizeIdc > SCALING_LIST_8x8)
3940      {
3941        fseek(fp,0,0);
3942        do 
3943        {
3944          ret = fgets(line, 1024, fp);
3945          if ((ret==NULL)||(strstr(line, MatrixType_DC[sizeIdc][listIdc])==NULL && feof(fp)))
3946          {
3947            printf("Error: can't read DC :: set Default Matrix\n");
3948            return true;
3949          }
3950        }
3951        while (strstr(line, MatrixType_DC[sizeIdc][listIdc]) == NULL);
3952        retval = fscanf(fp, "%d,", &data);
3953        if (retval!=1)
3954        {
3955          printf("Error: can't read Matrix :: set Default Matrix\n");
3956          return true;
3957        }
3958        //overwrite DC value when size of matrix is larger than 16x16
3959        setScalingListDC(sizeIdc,listIdc,data);
3960      }
3961    }
3962  }
3963  fclose(fp);
3964  return false;
3965}
3966
3967#if H_MV
3968Void TComScalingList::inferFrom( TComScalingList* srcScLi )
3969{
3970  for(Int sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
3971  {
3972    for(Int listId = 0; listId <  g_scalingListNum[sizeId]; listId++)
3973    {
3974      setRefMatrixId  (sizeId,listId, srcScLi->getRefMatrixId  (sizeId,listId));
3975      setScalingListDC(sizeId,listId, srcScLi->getScalingListDC(sizeId,listId));         
3976      ::memcpy(getScalingListAddress(sizeId, listId),srcScLi->getScalingListAddress(sizeId, listId),sizeof(Int)*min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId]));
3977    }
3978  }
3979}
3980#endif
3981/** initialization process of quantization matrix array
3982 */
3983Void TComScalingList::init()
3984{
3985  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
3986  {
3987    for(UInt listId = 0; listId < g_scalingListNum[sizeId]; listId++)
3988    {
3989      m_scalingListCoef[sizeId][listId] = new Int [min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId])];
3990    }
3991  }
3992  m_scalingListCoef[SCALING_LIST_32x32][3] = m_scalingListCoef[SCALING_LIST_32x32][1]; // copy address for 32x32
3993}
3994
3995/** destroy quantization matrix array
3996 */
3997Void TComScalingList::destroy()
3998{
3999  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
4000  {
4001    for(UInt listId = 0; listId < g_scalingListNum[sizeId]; listId++)
4002    {
4003      if(m_scalingListCoef[sizeId][listId]) delete [] m_scalingListCoef[sizeId][listId];
4004    }
4005  }
4006}
4007
4008/** get default address of quantization matrix
4009 * \param sizeId size index
4010 * \param listId list index
4011 * \returns pointer of quantization matrix
4012 */
4013Int* TComScalingList::getScalingListDefaultAddress(UInt sizeId, UInt listId)
4014{
4015  Int *src = 0;
4016  switch(sizeId)
4017  {
4018    case SCALING_LIST_4x4:
4019      src = g_quantTSDefault4x4;
4020      break;
4021    case SCALING_LIST_8x8:
4022      src = (listId<3) ? g_quantIntraDefault8x8 : g_quantInterDefault8x8;
4023      break;
4024    case SCALING_LIST_16x16:
4025      src = (listId<3) ? g_quantIntraDefault8x8 : g_quantInterDefault8x8;
4026      break;
4027    case SCALING_LIST_32x32:
4028      src = (listId<1) ? g_quantIntraDefault8x8 : g_quantInterDefault8x8;
4029      break;
4030    default:
4031      assert(0);
4032      src = NULL;
4033      break;
4034  }
4035  return src;
4036}
4037
4038/** process of default matrix
4039 * \param sizeId size index
4040 * \param Index of input matrix
4041 */
4042Void TComScalingList::processDefaultMatrix(UInt sizeId, UInt listId)
4043{
4044  ::memcpy(getScalingListAddress(sizeId, listId),getScalingListDefaultAddress(sizeId,listId),sizeof(Int)*min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId]));
4045  setScalingListDC(sizeId,listId,SCALING_LIST_DC);
4046}
4047
4048/** check DC value of matrix for default matrix signaling
4049 */
4050Void TComScalingList::checkDcOfMatrix()
4051{
4052  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
4053  {
4054    for(UInt listId = 0; listId < g_scalingListNum[sizeId]; listId++)
4055    {
4056      //check default matrix?
4057      if(getScalingListDC(sizeId,listId) == 0)
4058      {
4059        processDefaultMatrix(sizeId, listId);
4060      }
4061    }
4062  }
4063}
4064
4065ParameterSetManager::ParameterSetManager()
4066: m_vpsMap(MAX_NUM_VPS)
4067, m_spsMap(MAX_NUM_SPS)
4068, m_ppsMap(MAX_NUM_PPS)
4069, m_activeVPSId(-1)
4070#if !H_MV
4071, m_activeSPSId(-1)
4072, m_activePPSId(-1)
4073{
4074#else
4075{
4076  for (Int i = 0; i < MAX_NUM_LAYERS; i++ )
4077  {
4078    m_activeSPSId[ i ] = -1; 
4079    m_activePPSId[ i ] = -1; 
4080  }
4081#endif
4082}
4083
4084
4085ParameterSetManager::~ParameterSetManager()
4086{
4087}
4088
4089//! activate a SPS from a active parameter sets SEI message
4090//! \returns true, if activation is successful
4091#if H_MV
4092Bool ParameterSetManager::activateSPSWithSEI(Int spsId, Int layerId )
4093#else
4094Bool ParameterSetManager::activateSPSWithSEI(Int spsId)
4095#endif
4096{
4097  TComSPS *sps = m_spsMap.getPS(spsId);
4098  if (sps)
4099  {
4100    Int vpsId = sps->getVPSId();
4101    if (m_vpsMap.getPS(vpsId))
4102    {
4103      m_activeVPSId = vpsId;
4104#if H_MV
4105      m_activeSPSId[ layerId ] = spsId;
4106#else
4107      m_activeSPSId = spsId;
4108#endif
4109      return true;
4110    }
4111    else
4112    {
4113      printf("Warning: tried to activate SPS using an Active parameter sets SEI message. Referenced VPS does not exist.");
4114    }
4115  }
4116  else
4117  {
4118    printf("Warning: tried to activate non-existing SPS using an Active parameter sets SEI message.");
4119  }
4120  return false;
4121}
4122
4123//! activate a PPS and depending on isIDR parameter also SPS and VPS
4124//! \returns true, if activation is successful
4125#if H_MV
4126Bool ParameterSetManager::activatePPS(Int ppsId, Bool isIRAP, Int layerId )
4127#else
4128Bool ParameterSetManager::activatePPS(Int ppsId, Bool isIRAP)
4129#endif
4130{
4131  TComPPS *pps = m_ppsMap.getPS(ppsId);
4132  if (pps)
4133  {
4134    Int spsId = pps->getSPSId();
4135#if H_MV
4136    if (!isIRAP && (spsId != m_activeSPSId[ layerId ]))
4137#else
4138    if (!isIRAP && (spsId != m_activeSPSId))
4139#endif
4140    {
4141      printf("Warning: tried to activate PPS referring to a inactive SPS at non-IRAP.");
4142      return false;
4143    }
4144
4145    TComSPS *sps = m_spsMap.getPS(spsId);
4146    if (sps)
4147    {
4148      Int vpsId = sps->getVPSId();
4149      if (!isIRAP && (vpsId != m_activeVPSId))
4150      {
4151        printf("Warning: tried to activate PPS referring to a inactive VPS at non-IRAP.");
4152        return false;
4153      }
4154      if (m_vpsMap.getPS(vpsId))
4155      {
4156#if H_MV
4157        m_activePPSId[ layerId ] = ppsId;
4158        m_activeVPSId = vpsId;
4159        m_activeSPSId[ layerId ] = spsId;
4160#else
4161        m_activePPSId = ppsId;
4162        m_activeVPSId = vpsId;
4163        m_activeSPSId = spsId;
4164#endif
4165        return true;
4166      }
4167      else
4168      {
4169        printf("Warning: tried to activate PPS that refers to a non-existing VPS.");
4170      }
4171    }
4172    else
4173    {
4174      printf("Warning: tried to activate a PPS that refers to a non-existing SPS.");
4175    }
4176  }
4177  else
4178  {
4179    printf("Warning: tried to activate non-existing PPS.");
4180  }
4181  return false;
4182}
4183
4184ProfileTierLevel::ProfileTierLevel()
4185  : m_profileSpace    (0)
4186  , m_tierFlag        (false)
4187  , m_profileIdc      (0)
4188  , m_levelIdc        (0)
4189, m_progressiveSourceFlag  (false)
4190, m_interlacedSourceFlag   (false)
4191, m_nonPackedConstraintFlag(false)
4192, m_frameOnlyConstraintFlag(false)
4193{
4194  ::memset(m_profileCompatibilityFlag, 0, sizeof(m_profileCompatibilityFlag));
4195}
4196
4197TComPTL::TComPTL()
4198{
4199  ::memset(m_subLayerProfilePresentFlag, 0, sizeof(m_subLayerProfilePresentFlag));
4200  ::memset(m_subLayerLevelPresentFlag,   0, sizeof(m_subLayerLevelPresentFlag  ));
4201}
4202
4203#if H_MV
4204Void TComPTL::copyLevelFrom( TComPTL* source )
4205{
4206  getGeneralPTL()->setLevelIdc( source->getGeneralPTL()->getLevelIdc() );
4207  for( Int subLayer = 0; subLayer < 6; subLayer++ )
4208  {
4209    setSubLayerLevelPresentFlag( subLayer, source->getSubLayerLevelPresentFlag( subLayer ) );
4210    getSubLayerPTL( subLayer )->setLevelIdc( source->getSubLayerPTL( subLayer )->getLevelIdc() );
4211  }
4212}
4213#endif
4214//! \}
4215
4216#if H_MV
4217TComVPSVUI::TComVPSVUI()
4218{
4219  m_crossLayerIrapAlignedFlag = true; 
4220  m_allLayersIdrAlignedFlag   = false; 
4221  m_bitRatePresentVpsFlag = false;
4222  m_picRatePresentVpsFlag = false;
4223  for ( Int i = 0; i < MAX_VPS_OP_SETS_PLUS1; i++)
4224  {   
4225    for ( Int j = 0; j < MAX_TLAYER; j++)
4226    {   
4227      m_bitRatePresentFlag          [i][j] = false;
4228      m_picRatePresentFlag          [i][j] = false;
4229      m_avgBitRate                  [i][j] = -1;
4230      m_maxBitRate                  [i][j] = -1;
4231      m_constantPicRateIdc          [i][j] = -1;
4232      m_avgPicRate                  [i][j] = -1;
4233    }
4234  }
4235
4236  m_ilpRestrictedRefLayersFlag = false;
4237
4238  for ( Int i = 0; i < MAX_NUM_LAYERS; i++)
4239  {         
4240    for ( Int j = 0; j < MAX_NUM_LAYERS; j++)
4241    {   
4242      m_tileBoundariesAlignedFlag   [i][j] = false;
4243      m_minSpatialSegmentOffsetPlus1[i][j] = 0;
4244      m_ctuBasedOffsetEnabledFlag   [i][j] = false;
4245      m_minHorizontalCtuOffsetPlus1 [i][j] = -1;
4246    }
4247    m_baseLayerParameterSetCompatibilityFlag[i] = false;
4248  }
4249  for ( Int i = 0; i < MAX_NUM_VIDEO_SIGNAL_INFO; i++ )
4250  {
4251    m_videoSignalInfo          [i] = NULL;     
4252  }
4253
4254  m_vpsVuiBspHrdPresentFlag = false; 
4255  m_vpsVuiBspHrdParameters  = new TComVpsVuiBspHrdParameters();
4256}
4257
4258TComVPSVUI::~TComVPSVUI()
4259{
4260  for ( Int i = 0; i < MAX_NUM_VIDEO_SIGNAL_INFO; i++ )
4261  {
4262    if (m_videoSignalInfo[ i ] != NULL )      delete m_videoSignalInfo[ i ];   
4263    m_videoSignalInfo    [ i ] = NULL; 
4264  }
4265
4266  if ( m_vpsVuiBspHrdParameters ) delete m_vpsVuiBspHrdParameters;
4267  m_vpsVuiBspHrdParameters = NULL; 
4268}
4269
4270Void TComVPSVUI::inferVpsVui( Bool encoderFlag )
4271{
4272  // inference of syntax elements that differ from default inference (as done in constructor), when VPS VUI is not present
4273  if (!encoderFlag )
4274  {
4275    setCrossLayerIrapAlignedFlag( false ); 
4276  }
4277  else
4278  {
4279    assert( !getCrossLayerIrapAlignedFlag() ); 
4280  }
4281}
4282
4283Void TComRepFormat::inferChromaAndBitDepth( TComRepFormat* prevRepFormat, Bool encoderFlag )
4284{
4285  if ( !encoderFlag )
4286  {
4287    setChromaAndBitDepthVpsPresentFlag( prevRepFormat->getChromaAndBitDepthVpsPresentFlag() );
4288    setSeparateColourPlaneVpsFlag     ( prevRepFormat->getSeparateColourPlaneVpsFlag     () );
4289    setBitDepthVpsLumaMinus8          ( prevRepFormat->getBitDepthVpsLumaMinus8          () );
4290    setBitDepthVpsChromaMinus8        ( prevRepFormat->getBitDepthVpsChromaMinus8        () );
4291  }
4292  else
4293  {
4294    assert( getChromaAndBitDepthVpsPresentFlag() == prevRepFormat->getChromaAndBitDepthVpsPresentFlag() );
4295    assert( getSeparateColourPlaneVpsFlag     () == prevRepFormat->getSeparateColourPlaneVpsFlag     () );
4296    assert( getBitDepthVpsLumaMinus8          () == prevRepFormat->getBitDepthVpsLumaMinus8          () );
4297    assert( getBitDepthVpsChromaMinus8        () == prevRepFormat->getBitDepthVpsChromaMinus8        () );
4298}
4299}
4300
4301Void TComVpsVuiBspHrdParameters::checkLayerInBspFlag( TComVPS* vps, Int h )
4302{
4303  // It is a requirement of bitstream conformance that bitstream partition with index j shall not include
4304  // direct or indirect reference layers of any layers in bitstream partition i for any values of i and j
4305  // in the range of 0 to num_bitstream_partitions[ h ] ?1, inclusive, such that i is less than j.
4306
4307  for ( Int partJ = 0; partJ < getNumBitstreamPartitions( h ); partJ++ )
4308  {       
4309    for ( Int partI = 0; partI < partJ; partI++ )
4310    {
4311      for ( Int layerJ = 0; layerJ < vps->getMaxLayersMinus1(); layerJ++ )
4312      {
4313        if ( m_layerInBspFlag[ h ][partJ][layerJ ] )
4314        {
4315          for ( Int layerI = 0; layerI < vps->getMaxLayersMinus1(); layerI++ )
4316          {
4317            if ( m_layerInBspFlag[ h ][partI][layerI] )
4318            {
4319              assert( !vps->getInDirectDependencyFlag( layerI, layerJ ) ); 
4320            }
4321          }
4322        }
4323      }
4324    }
4325  }
4326
4327  // ---------------
4328  // To be added:
4329  // 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
4330  // 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,
4331  // inclusive, the value of layer_in_bsp_flag[ h ][ i ][ j ] for at least one value of j in the range of 1 to
4332  // NumLayersInIdList[ h ] - 1, inclusive, shall be equal to 1.
4333  // ---------------
4334
4335
4336  // 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,
4337  // 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
4338  // NumLayersInIdList[ h ] − 1, inclusive.
4339
4340
4341  if ( getNumBitstreamPartitions( h ) == 1 )
4342  {
4343    Bool atLeastOneZero = false; 
4344    for ( Int j = 0; j <= vps->getNumLayersInIdList( h ) - 1; j++ )
4345    {
4346      atLeastOneZero = atLeastOneZero || !getLayerInBspFlag( h, 0, j ); 
4347    }   
4348    assert( atLeastOneZero ); 
4349  }
4350
4351 
4352  // 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 ]
4353  // shall be equal to 1 for at most one value of i in the range of 0 to num_bitstream_partitions[ h ] − 1, inclusive.
4354
4355  for ( Int j = 0; j <= vps->getNumLayersInIdList( h ) - 1; j++ )
4356  {
4357    Int numLayerInBsp = 0; 
4358    for ( Int i = 0; i <= getNumBitstreamPartitions( h ) - 1; i++ )
4359    {
4360      numLayerInBsp += ( getLayerInBspFlag( h, i, j ) ? 1 : 0 ); 
4361    }   
4362    assert( numLayerInBsp <= 1 ); 
4363  }
4364
4365}
4366
4367Void TComVpsVuiBspHrdParameters::checkBspCombHrdAndShedIdx( TComVPS* vps, Int h, Int i, Int j )
4368{
4369  // bsp_comb_hrd_idx
4370  assert( getBspCombSchedIdx(h, i, j ) >= 0 );
4371  assert( getBspCombSchedIdx(h, i, j ) <= getVpsNumBspHrdParametersMinus1() );
4372
4373  // bsp_comb_sched_idx
4374  assert( getBspCombSchedIdx(h, i, j ) >= 0 );
4375
4376  //* This check needs to activated,  when HighestTid is available here   
4377  //  assert(  getBspCombSchedIdx(h, i, j ) <= vps->getHrdParameters( getBspCombHrdIdx( h, i, j ) )->getCpbCntMinus1( highestTid ) );
4378}
4379
4380Void TComVUI::inferVideoSignalInfo( TComVPS* vps, Int layerIdCurr )
4381{
4382  if ( layerIdCurr == 0 || !vps->getVpsVuiPresentFlag() ) 
4383  {
4384    return; 
4385  }
4386
4387  TComVPSVUI* vpsVUI = vps->getVPSVUI(); 
4388  assert( vpsVUI != NULL ); 
4389
4390  TComVideoSignalInfo* videoSignalInfo = vpsVUI->getVideoSignalInfo( vpsVUI->getVpsVideoSignalInfoIdx( vps->getLayerIdInVps( layerIdCurr ) ) ); 
4391  assert( videoSignalInfo != NULL );
4392
4393  setVideoFormat            ( videoSignalInfo->getVideoVpsFormat            () ); 
4394  setVideoFullRangeFlag     ( videoSignalInfo->getVideoFullRangeVpsFlag     () );
4395  setColourPrimaries        ( videoSignalInfo->getColourPrimariesVps        () );
4396  setTransferCharacteristics( videoSignalInfo->getTransferCharacteristicsVps() );
4397  setMatrixCoefficients     ( videoSignalInfo->getMatrixCoeffsVps           () );
4398}
4399
4400TComDpbSize::TComDpbSize()
4401{
4402  for (Int i = 0; i < MAX_VPS_OUTPUTLAYER_SETS; i++ )
4403  {     
4404    m_subLayerFlagInfoPresentFlag[i]  = false;
4405
4406    for (Int j = 0; j < MAX_TLAYER; j++  )
4407    {       
4408      m_subLayerDpbInfoPresentFlag [i][j] = ( j == 0) ;
4409      m_maxVpsNumReorderPics       [i][j] = 0;
4410      m_maxVpsLatencyIncreasePlus1 [i][j] = 0;
4411
4412      for (Int k = 0; k < MAX_NUM_LAYER_IDS; k++ )
4413      {
4414        m_maxVpsDecPicBufferingMinus1[i][k][j] = 0; 
4415      }
4416    }
4417  }
4418}
4419
4420Void Window::scaleOffsets( Int scal )
4421{
4422  if (! m_scaledFlag )
4423  {
4424    m_scaledFlag         = true; 
4425    m_winLeftOffset     *= scal; 
4426    m_winRightOffset    *= scal; 
4427    m_winTopOffset      *= scal; 
4428    m_winBottomOffset   *= scal; 
4429  }
4430}
4431#endif
4432
Note: See TracBrowser for help on using the repository browser.