source: 3DVCSoftware/branches/HTM-10.0-dev0/source/Lib/TLibCommon/TComSlice.cpp @ 852

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

Update HM-12.0 -> HM-13.0.

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