source: 3DVCSoftware/branches/HTM-DEV-0.3-dev2/source/Lib/TLibCommon/TComSlice.cpp @ 499

Last change on this file since 499 was 499, checked in by zhang, 11 years ago

Changes include: DV-MVP(H_3D_IDV), TMVP merging candidate(H_3D_TMVP, target reference index changes, scaling based on view order index) and cleanups (H_3D_CLEANUPS)

  • Property svn:eol-style set to native
File size: 82.4 KB
Line 
1/* The copyright in this software is being made available under the BSD
2 * License, included below. This software may be subject to other third party
3 * and contributor rights, including patent rights, and no such rights are
4 * granted under this license. 
5 *
6 * Copyright (c) 2010-2013, ITU/ISO/IEC
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 *
12 *  * Redistributions of source code must retain the above copyright notice,
13 *    this list of conditions and the following disclaimer.
14 *  * Redistributions in binary form must reproduce the above copyright notice,
15 *    this list of conditions and the following disclaimer in the documentation
16 *    and/or other materials provided with the distribution.
17 *  * Neither the name of the ITU/ISO/IEC nor the names of its contributors may
18 *    be used to endorse or promote products derived from this software without
19 *    specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31 * THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34/** \file     TComSlice.cpp
35    \brief    slice header and SPS class
36*/
37
38#include "CommonDef.h"
39#include "TComSlice.h"
40#include "TComPic.h"
41#include "TLibEncoder/TEncSbac.h"
42#include "TLibDecoder/TDecSbac.h"
43
44//! \ingroup TLibCommon
45//! \{
46
47TComSlice::TComSlice()
48: m_iPPSId                        ( -1 )
49, m_iPOC                          ( 0 )
50, m_iLastIDR                      ( 0 )
51, m_eNalUnitType                  ( NAL_UNIT_CODED_SLICE_IDR_W_RADL )
52, m_eSliceType                    ( I_SLICE )
53, m_iSliceQp                      ( 0 )
54, m_dependentSliceSegmentFlag            ( false )
55#if ADAPTIVE_QP_SELECTION
56, m_iSliceQpBase                  ( 0 )
57#endif
58, m_deblockingFilterDisable        ( false )
59, m_deblockingFilterOverrideFlag   ( false )
60, m_deblockingFilterBetaOffsetDiv2 ( 0 )
61, m_deblockingFilterTcOffsetDiv2   ( 0 )
62#if !L0034_COMBINED_LIST_CLEANUP
63, m_bRefPicListModificationFlagLC ( false )
64, m_bRefPicListCombinationFlag    ( false )
65#endif
66, m_bCheckLDC                     ( false )
67, m_iSliceQpDelta                 ( 0 )
68, m_iSliceQpDeltaCb               ( 0 )
69, m_iSliceQpDeltaCr               ( 0 )
70, m_iDepth                        ( 0 )
71, m_bRefenced                     ( false )
72, m_pcSPS                         ( NULL )
73, m_pcPPS                         ( NULL )
74, m_pcPic                         ( NULL )
75, m_colFromL0Flag                 ( 1 )
76, m_colRefIdx                     ( 0 )
77#if SAO_CHROMA_LAMBDA
78, m_dLambdaLuma( 0.0 )
79, m_dLambdaChroma( 0.0 )
80#else
81, m_dLambda                       ( 0.0 )
82#endif
83#if !L0034_COMBINED_LIST_CLEANUP
84, m_bNoBackPredFlag               ( false )
85#endif
86, m_uiTLayer                      ( 0 )
87, m_bTLayerSwitchingFlag          ( false )
88, m_sliceMode                   ( 0 )
89, m_sliceArgument               ( 0 )
90, m_sliceCurStartCUAddr         ( 0 )
91, m_sliceCurEndCUAddr           ( 0 )
92, m_sliceIdx                    ( 0 )
93, m_sliceSegmentMode            ( 0 )
94, m_sliceSegmentArgument        ( 0 )
95, m_sliceSegmentCurStartCUAddr  ( 0 )
96, m_sliceSegmentCurEndCUAddr    ( 0 )
97, m_nextSlice                    ( false )
98, m_nextSliceSegment             ( false )
99, m_sliceBits                   ( 0 )
100, m_sliceSegmentBits         ( 0 )
101, m_bFinalized                    ( false )
102, m_uiTileOffstForMultES          ( 0 )
103, m_puiSubstreamSizes             ( NULL )
104, m_cabacInitFlag                 ( false )
105, m_bLMvdL1Zero                   ( false )
106, m_numEntryPointOffsets          ( 0 )
107, m_temporalLayerNonReferenceFlag ( false )
108, m_enableTMVPFlag                ( true )
109#if H_MV
110, m_layerId                       (0)
111, m_viewId                        (0)
112#if H_3D
113, m_viewIndex                     (0)
114, m_isDepth                       (false)
115#endif
116#if H_3D_GEN
117, m_depthToDisparityB             ( NULL )
118, m_depthToDisparityF             ( NULL )
119#endif
120#endif
121{
122#if L0034_COMBINED_LIST_CLEANUP
123  m_aiNumRefIdx[0] = m_aiNumRefIdx[1] = 0;
124#else
125  m_aiNumRefIdx[0] = m_aiNumRefIdx[1] = m_aiNumRefIdx[2] = 0;
126#endif
127 
128  initEqualRef();
129 
130#if L0034_COMBINED_LIST_CLEANUP
131  for ( Int idx = 0; idx < MAX_NUM_REF; idx++ )
132  {
133    m_list1IdxToList0Idx[idx] = -1;
134  }
135#else
136  for(Int iNumCount = 0; iNumCount < MAX_NUM_REF_LC; iNumCount++)
137  {
138    m_iRefIdxOfLC[REF_PIC_LIST_0][iNumCount]=-1;
139    m_iRefIdxOfLC[REF_PIC_LIST_1][iNumCount]=-1;
140    m_eListIdFromIdxOfLC[iNumCount]=0;
141    m_iRefIdxFromIdxOfLC[iNumCount]=0;
142    m_iRefIdxOfL0FromRefIdxOfL1[iNumCount] = -1;
143    m_iRefIdxOfL1FromRefIdxOfL0[iNumCount] = -1;
144  }   
145#endif
146  for(Int iNumCount = 0; iNumCount < MAX_NUM_REF; iNumCount++)
147  {
148    m_apcRefPicList [0][iNumCount] = NULL;
149    m_apcRefPicList [1][iNumCount] = NULL;
150    m_aiRefPOCList  [0][iNumCount] = 0;
151    m_aiRefPOCList  [1][iNumCount] = 0;
152#if H_MV
153    m_aiRefLayerIdList[0][iNumCount] = 0;
154    m_aiRefLayerIdList[1][iNumCount] = 0;
155#endif
156  }
157  resetWpScaling();
158  initWpAcDcParam();
159  m_saoEnabledFlag = false;
160}
161
162TComSlice::~TComSlice()
163{
164  delete[] m_puiSubstreamSizes;
165  m_puiSubstreamSizes = NULL;
166#if H_3D_GEN   
167  for( UInt i = 0; i < getViewIndex(); i++ )
168  {
169    if ( m_depthToDisparityB && m_depthToDisparityB[ i ] )
170      delete[] m_depthToDisparityB [ i ];
171
172    if ( m_depthToDisparityF && m_depthToDisparityF[ i ] ) 
173      delete[] m_depthToDisparityF [ i ];
174  }
175
176  if ( m_depthToDisparityF )
177    delete[] m_depthToDisparityF; 
178
179  m_depthToDisparityF = NULL;
180
181  if ( m_depthToDisparityB )
182    delete[] m_depthToDisparityB; 
183
184  m_depthToDisparityB = NULL;
185#endif
186}
187
188
189Void TComSlice::initSlice()
190{
191  m_aiNumRefIdx[0]      = 0;
192  m_aiNumRefIdx[1]      = 0;
193 
194  m_colFromL0Flag = 1;
195 
196  m_colRefIdx = 0;
197  initEqualRef();
198#if !L0034_COMBINED_LIST_CLEANUP
199  m_bNoBackPredFlag = false;
200  m_bRefPicListCombinationFlag = false;
201  m_bRefPicListModificationFlagLC = false;
202#endif
203  m_bCheckLDC = false;
204  m_iSliceQpDeltaCb = 0;
205  m_iSliceQpDeltaCr = 0;
206
207#if !L0034_COMBINED_LIST_CLEANUP
208  m_aiNumRefIdx[REF_PIC_LIST_C]      = 0;
209#endif
210
211#if H_3D_IV_MERGE
212  m_maxNumMergeCand = MRG_MAX_NUM_CANDS_MEM;
213#else
214  m_maxNumMergeCand = MRG_MAX_NUM_CANDS;
215#endif
216
217  m_bFinalized=false;
218
219  m_tileByteLocation.clear();
220  m_cabacInitFlag        = false;
221  m_numEntryPointOffsets = 0;
222  m_enableTMVPFlag = true;
223#if H_3D_TMVP
224  m_aiAlterRefIdx[0]                  = -1;
225  m_aiAlterRefIdx[1]                  = -1;
226#endif
227}
228
229Bool TComSlice::getRapPicFlag()
230{
231  return getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_W_RADL
232      || getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_N_LP
233      || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP
234      || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL
235      || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP
236      || getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA;
237}
238
239/**
240 - allocate table to contain substream sizes to be written to the slice header.
241 .
242 \param uiNumSubstreams Number of substreams -- the allocation will be this value - 1.
243 */
244Void  TComSlice::allocSubstreamSizes(UInt uiNumSubstreams)
245{
246  delete[] m_puiSubstreamSizes;
247  m_puiSubstreamSizes = new UInt[uiNumSubstreams > 0 ? uiNumSubstreams-1 : 0];
248}
249
250Void  TComSlice::sortPicList        (TComList<TComPic*>& rcListPic)
251{
252  TComPic*    pcPicExtract;
253  TComPic*    pcPicInsert;
254 
255  TComList<TComPic*>::iterator    iterPicExtract;
256  TComList<TComPic*>::iterator    iterPicExtract_1;
257  TComList<TComPic*>::iterator    iterPicInsert;
258 
259  for (Int i = 1; i < (Int)(rcListPic.size()); i++)
260  {
261    iterPicExtract = rcListPic.begin();
262    for (Int j = 0; j < i; j++) iterPicExtract++;
263    pcPicExtract = *(iterPicExtract);
264    pcPicExtract->setCurrSliceIdx(0);
265   
266    iterPicInsert = rcListPic.begin();
267    while (iterPicInsert != iterPicExtract)
268    {
269      pcPicInsert = *(iterPicInsert);
270      pcPicInsert->setCurrSliceIdx(0);
271      if (pcPicInsert->getPOC() >= pcPicExtract->getPOC())
272      {
273        break;
274      }
275     
276      iterPicInsert++;
277    }
278   
279    iterPicExtract_1 = iterPicExtract;    iterPicExtract_1++;
280   
281    //  swap iterPicExtract and iterPicInsert, iterPicExtract = curr. / iterPicInsert = insertion position
282    rcListPic.insert (iterPicInsert, iterPicExtract, iterPicExtract_1);
283    rcListPic.erase  (iterPicExtract);
284  }
285}
286
287TComPic* TComSlice::xGetRefPic (TComList<TComPic*>& rcListPic,
288                                Int                 poc)
289{
290  TComList<TComPic*>::iterator  iterPic = rcListPic.begin(); 
291  TComPic*                      pcPic = *(iterPic);
292  while ( iterPic != rcListPic.end() )
293  {
294    if(pcPic->getPOC() == poc)
295    {
296      break;
297    }
298    iterPic++;
299    pcPic = *(iterPic);
300  }
301  return  pcPic;
302}
303
304
305TComPic* TComSlice::xGetLongTermRefPic(TComList<TComPic*>& rcListPic, Int poc, Bool pocHasMsb)
306{
307  TComList<TComPic*>::iterator  iterPic = rcListPic.begin(); 
308  TComPic*                      pcPic = *(iterPic);
309  TComPic*                      pcStPic = pcPic;
310 
311  Int pocCycle = 1 << getSPS()->getBitsForPOC();
312  if (!pocHasMsb)
313  {
314    poc = poc % pocCycle;
315  }
316 
317  while ( iterPic != rcListPic.end() )
318  {
319    pcPic = *(iterPic);
320    if (pcPic && pcPic->getPOC()!=this->getPOC() && pcPic->getSlice( 0 )->isReferenced())
321    {
322      Int picPoc = pcPic->getPOC();
323      if (!pocHasMsb)
324      {
325        picPoc = picPoc % pocCycle;
326      }
327     
328      if (poc == picPoc)
329    {
330      if(pcPic->getIsLongTerm())
331      {
332        return pcPic;
333      }
334      else
335      {
336        pcStPic = pcPic;
337      }
338      break;
339    }
340    }
341
342    iterPic++;
343  }
344 
345  return  pcStPic;
346}
347
348Void TComSlice::setRefPOCList       ()
349{
350  for (Int iDir = 0; iDir < 2; iDir++)
351  {
352    for (Int iNumRefIdx = 0; iNumRefIdx < m_aiNumRefIdx[iDir]; iNumRefIdx++)
353    {
354      m_aiRefPOCList[iDir][iNumRefIdx] = m_apcRefPicList[iDir][iNumRefIdx]->getPOC();
355#if H_MV
356      m_aiRefLayerIdList[iDir][iNumRefIdx] = m_apcRefPicList[iDir][iNumRefIdx]->getLayerId();
357#endif
358    }
359  }
360
361}
362
363#if L0034_COMBINED_LIST_CLEANUP
364Void TComSlice::setList1IdxToList0Idx()
365{
366  Int idxL0, idxL1;
367  for ( idxL1 = 0; idxL1 < getNumRefIdx( REF_PIC_LIST_1 ); idxL1++ )
368  {
369    m_list1IdxToList0Idx[idxL1] = -1;
370    for ( idxL0 = 0; idxL0 < getNumRefIdx( REF_PIC_LIST_0 ); idxL0++ )
371    {
372      if ( m_apcRefPicList[REF_PIC_LIST_0][idxL0]->getPOC() == m_apcRefPicList[REF_PIC_LIST_1][idxL1]->getPOC() )
373      {
374        m_list1IdxToList0Idx[idxL1] = idxL0;
375        break;
376      }
377    }
378  }
379}
380#else
381Void TComSlice::generateCombinedList()
382{
383  if(m_aiNumRefIdx[REF_PIC_LIST_C] > 0)
384  {
385    m_aiNumRefIdx[REF_PIC_LIST_C]=0;
386    for(Int iNumCount = 0; iNumCount < MAX_NUM_REF_LC; iNumCount++)
387    {
388      m_iRefIdxOfLC[REF_PIC_LIST_0][iNumCount]=-1;
389      m_iRefIdxOfLC[REF_PIC_LIST_1][iNumCount]=-1;
390      m_eListIdFromIdxOfLC[iNumCount]=0;
391      m_iRefIdxFromIdxOfLC[iNumCount]=0;
392      m_iRefIdxOfL0FromRefIdxOfL1[iNumCount] = -1;
393      m_iRefIdxOfL1FromRefIdxOfL0[iNumCount] = -1;
394    }
395
396    for (Int iNumRefIdx = 0; iNumRefIdx < MAX_NUM_REF; iNumRefIdx++)
397    {
398      if(iNumRefIdx < m_aiNumRefIdx[REF_PIC_LIST_0])
399      {
400        Bool bTempRefIdxInL2 = true;
401        for ( Int iRefIdxLC = 0; iRefIdxLC < m_aiNumRefIdx[REF_PIC_LIST_C]; iRefIdxLC++ )
402        {
403#if H_MV
404          if ( m_apcRefPicList[REF_PIC_LIST_0][iNumRefIdx]->getPOC() == m_apcRefPicList[m_eListIdFromIdxOfLC[iRefIdxLC]][m_iRefIdxFromIdxOfLC[iRefIdxLC]]->getPOC() &&
405               m_apcRefPicList[REF_PIC_LIST_0][iNumRefIdx]->getLayerId() == m_apcRefPicList[m_eListIdFromIdxOfLC[iRefIdxLC]][m_iRefIdxFromIdxOfLC[iRefIdxLC]]->getLayerId() )
406#else
407          if ( m_apcRefPicList[REF_PIC_LIST_0][iNumRefIdx]->getPOC() == m_apcRefPicList[m_eListIdFromIdxOfLC[iRefIdxLC]][m_iRefIdxFromIdxOfLC[iRefIdxLC]]->getPOC() )
408#endif
409          {
410            m_iRefIdxOfL1FromRefIdxOfL0[iNumRefIdx] = m_iRefIdxFromIdxOfLC[iRefIdxLC];
411            m_iRefIdxOfL0FromRefIdxOfL1[m_iRefIdxFromIdxOfLC[iRefIdxLC]] = iNumRefIdx;
412            bTempRefIdxInL2 = false;
413            break;
414          }
415        }
416
417        if(bTempRefIdxInL2 == true)
418        { 
419          m_eListIdFromIdxOfLC[m_aiNumRefIdx[REF_PIC_LIST_C]] = REF_PIC_LIST_0;
420          m_iRefIdxFromIdxOfLC[m_aiNumRefIdx[REF_PIC_LIST_C]] = iNumRefIdx;
421          m_iRefIdxOfLC[REF_PIC_LIST_0][iNumRefIdx] = m_aiNumRefIdx[REF_PIC_LIST_C]++;
422        }
423      }
424
425      if(iNumRefIdx < m_aiNumRefIdx[REF_PIC_LIST_1])
426      {
427        Bool bTempRefIdxInL2 = true;
428        for ( Int iRefIdxLC = 0; iRefIdxLC < m_aiNumRefIdx[REF_PIC_LIST_C]; iRefIdxLC++ )
429        {
430#if H_MV
431          if ( m_apcRefPicList[REF_PIC_LIST_1][iNumRefIdx]->getPOC() == m_apcRefPicList[m_eListIdFromIdxOfLC[iRefIdxLC]][m_iRefIdxFromIdxOfLC[iRefIdxLC]]->getPOC() &&
432               m_apcRefPicList[REF_PIC_LIST_1][iNumRefIdx]->getLayerId() == m_apcRefPicList[m_eListIdFromIdxOfLC[iRefIdxLC]][m_iRefIdxFromIdxOfLC[iRefIdxLC]]->getLayerId() )
433#else
434          if ( m_apcRefPicList[REF_PIC_LIST_1][iNumRefIdx]->getPOC() == m_apcRefPicList[m_eListIdFromIdxOfLC[iRefIdxLC]][m_iRefIdxFromIdxOfLC[iRefIdxLC]]->getPOC() )
435#endif
436          {
437            m_iRefIdxOfL0FromRefIdxOfL1[iNumRefIdx] = m_iRefIdxFromIdxOfLC[iRefIdxLC];
438            m_iRefIdxOfL1FromRefIdxOfL0[m_iRefIdxFromIdxOfLC[iRefIdxLC]] = iNumRefIdx;
439            bTempRefIdxInL2 = false;
440            break;
441          }
442        }
443        if(bTempRefIdxInL2 == true)
444        {
445          m_eListIdFromIdxOfLC[m_aiNumRefIdx[REF_PIC_LIST_C]] = REF_PIC_LIST_1;
446          m_iRefIdxFromIdxOfLC[m_aiNumRefIdx[REF_PIC_LIST_C]] = iNumRefIdx;
447          m_iRefIdxOfLC[REF_PIC_LIST_1][iNumRefIdx] = m_aiNumRefIdx[REF_PIC_LIST_C]++;
448        }
449      }
450    }
451  }
452}
453#endif
454
455#if H_MV
456Void TComSlice::setRefPicList( TComList<TComPic*>& rcListPic, std::vector<TComPic*>& refPicSetInterLayer , Bool checkNumPocTotalCurr)
457#else
458#if FIX1071
459Void TComSlice::setRefPicList( TComList<TComPic*>& rcListPic, Bool checkNumPocTotalCurr )
460#else
461Void TComSlice::setRefPicList( TComList<TComPic*>& rcListPic )
462#endif
463#endif
464{
465#if FIX1071
466  if (!checkNumPocTotalCurr)
467#endif
468  {
469    if (m_eSliceType == I_SLICE)
470    {
471      ::memset( m_apcRefPicList, 0, sizeof (m_apcRefPicList));
472      ::memset( m_aiNumRefIdx,   0, sizeof ( m_aiNumRefIdx ));
473     
474      return;
475    }
476   
477#if !H_MV
478    m_aiNumRefIdx[0] = getNumRefIdx(REF_PIC_LIST_0);
479    m_aiNumRefIdx[1] = getNumRefIdx(REF_PIC_LIST_1);
480#endif
481  }
482
483  TComPic*  pcRefPic= NULL;
484  TComPic*  RefPicSetStCurr0[16];
485  TComPic*  RefPicSetStCurr1[16];
486  TComPic*  RefPicSetLtCurr[16];
487  UInt NumPocStCurr0 = 0;
488  UInt NumPocStCurr1 = 0;
489  UInt NumPocLtCurr = 0;
490#if H_MV
491  Int numDirectRefLayers  = getVPS()->getNumDirectRefLayers( getLayerIdInVps() );
492  assert( numDirectRefLayers == refPicSetInterLayer.size() ); 
493#endif
494  Int i;
495  for(i=0; i < m_pcRPS->getNumberOfNegativePictures(); i++)
496  {
497    if(m_pcRPS->getUsed(i))
498    {
499      pcRefPic = xGetRefPic(rcListPic, getPOC()+m_pcRPS->getDeltaPOC(i));
500      pcRefPic->setIsLongTerm(0);
501      pcRefPic->getPicYuvRec()->extendPicBorder();
502      RefPicSetStCurr0[NumPocStCurr0] = pcRefPic;
503      NumPocStCurr0++;
504      pcRefPic->setCheckLTMSBPresent(false); 
505    }
506  }
507 
508  for(; i < m_pcRPS->getNumberOfNegativePictures()+m_pcRPS->getNumberOfPositivePictures(); i++)
509  {
510    if(m_pcRPS->getUsed(i))
511    {
512      pcRefPic = xGetRefPic(rcListPic, getPOC()+m_pcRPS->getDeltaPOC(i));
513      pcRefPic->setIsLongTerm(0);
514      pcRefPic->getPicYuvRec()->extendPicBorder();
515      RefPicSetStCurr1[NumPocStCurr1] = pcRefPic;
516      NumPocStCurr1++;
517      pcRefPic->setCheckLTMSBPresent(false); 
518    }
519  }
520 
521  for(i = m_pcRPS->getNumberOfNegativePictures()+m_pcRPS->getNumberOfPositivePictures()+m_pcRPS->getNumberOfLongtermPictures()-1; i > m_pcRPS->getNumberOfNegativePictures()+m_pcRPS->getNumberOfPositivePictures()-1 ; i--)
522  {
523    if(m_pcRPS->getUsed(i))
524    {
525      pcRefPic = xGetLongTermRefPic(rcListPic, m_pcRPS->getPOC(i), m_pcRPS->getCheckLTMSBPresent(i));
526      pcRefPic->setIsLongTerm(1);
527      pcRefPic->getPicYuvRec()->extendPicBorder();
528      RefPicSetLtCurr[NumPocLtCurr] = pcRefPic;
529      NumPocLtCurr++;
530    }
531    if(pcRefPic==NULL) 
532    {
533      pcRefPic = xGetLongTermRefPic(rcListPic, m_pcRPS->getPOC(i), m_pcRPS->getCheckLTMSBPresent(i));
534    }
535    pcRefPic->setCheckLTMSBPresent(m_pcRPS->getCheckLTMSBPresent(i)); 
536  }
537
538  // ref_pic_list_init
539  TComPic*  rpsCurrList0[MAX_NUM_REF+1];
540  TComPic*  rpsCurrList1[MAX_NUM_REF+1];
541#if H_MV
542  Int numPocTotalCurr = NumPocStCurr0 + NumPocStCurr1 + NumPocLtCurr + numDirectRefLayers;
543  assert( numPocTotalCurr == getNumRpsCurrTempList() );
544#else
545  Int numPocTotalCurr = NumPocStCurr0 + NumPocStCurr1 + NumPocLtCurr;
546#endif
547#if FIX1071
548  if (checkNumPocTotalCurr)
549  {
550    // 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:
551#if H_MV
552    // – 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.
553    // – Otherwise, when the current picture contains a P or B slice, the value of NumPocTotalCurr shall not be equal to 0.
554    if ( getRapPicFlag() && m_layerId == 0 )
555#else
556    // – If the current picture is a BLA or CRA picture, the value of NumPocTotalCurr shall be equal to 0.
557    // – Otherwise, when the current picture contains a P or B slice, the value of NumPocTotalCurr shall not be equal to 0.
558    if (getRapPicFlag())
559#endif
560    {
561      assert(numPocTotalCurr == 0);
562    }
563
564    if (m_eSliceType == I_SLICE)
565    {
566      ::memset( m_apcRefPicList, 0, sizeof (m_apcRefPicList));
567      ::memset( m_aiNumRefIdx,   0, sizeof ( m_aiNumRefIdx ));
568     
569      return;
570    }
571   
572    assert(numPocTotalCurr != 0);
573   
574    m_aiNumRefIdx[0] = getNumRefIdx(REF_PIC_LIST_0);
575    m_aiNumRefIdx[1] = getNumRefIdx(REF_PIC_LIST_1);
576  }
577#endif
578
579  Int cIdx = 0;
580  for ( i=0; i<NumPocStCurr0; i++, cIdx++)
581  {
582    rpsCurrList0[cIdx] = RefPicSetStCurr0[i];
583  }
584  for ( i=0; i<NumPocStCurr1; i++, cIdx++)
585  {
586    rpsCurrList0[cIdx] = RefPicSetStCurr1[i];
587  }
588  for ( i=0; i<NumPocLtCurr;  i++, cIdx++)
589  {
590    rpsCurrList0[cIdx] = RefPicSetLtCurr[i];
591  }
592#if H_MV
593  for ( i=0; i<numDirectRefLayers;  i++, cIdx++)
594  {
595    if( cIdx <= MAX_NUM_REF )
596    {
597      rpsCurrList0[cIdx] = refPicSetInterLayer[i];
598    }
599  }
600#endif
601
602  if (m_eSliceType==B_SLICE)
603  {
604    cIdx = 0;
605    for ( i=0; i<NumPocStCurr1; i++, cIdx++)
606    {
607      rpsCurrList1[cIdx] = RefPicSetStCurr1[i];
608    }
609    for ( i=0; i<NumPocStCurr0; i++, cIdx++)
610    {
611      rpsCurrList1[cIdx] = RefPicSetStCurr0[i];
612    }
613    for ( i=0; i<NumPocLtCurr;  i++, cIdx++)
614    {
615      rpsCurrList1[cIdx] = RefPicSetLtCurr[i];
616    }
617#if H_MV
618    for ( i=0; i<numDirectRefLayers;  i++, cIdx++)
619    {
620      if( cIdx <= MAX_NUM_REF )
621      {
622        rpsCurrList1[cIdx] = refPicSetInterLayer[i];
623      }
624    }
625#endif
626  }
627
628  ::memset(m_bIsUsedAsLongTerm, 0, sizeof(m_bIsUsedAsLongTerm));
629
630  for (Int rIdx = 0; rIdx <= (m_aiNumRefIdx[0]-1); rIdx ++)
631  {
632    m_apcRefPicList[0][rIdx] = m_RefPicListModification.getRefPicListModificationFlagL0() ? rpsCurrList0[ m_RefPicListModification.getRefPicSetIdxL0(rIdx) ] : rpsCurrList0[rIdx % numPocTotalCurr];
633    m_bIsUsedAsLongTerm[0][rIdx] = m_RefPicListModification.getRefPicListModificationFlagL0() ? (m_RefPicListModification.getRefPicSetIdxL0(rIdx) >= (NumPocStCurr0 + NumPocStCurr1))
634                                  : ((rIdx % numPocTotalCurr) >= (NumPocStCurr0 + NumPocStCurr1));
635  }
636  if ( m_eSliceType == P_SLICE )
637  {
638    m_aiNumRefIdx[1] = 0;
639    ::memset( m_apcRefPicList[1], 0, sizeof(m_apcRefPicList[1]));
640  }
641  else
642  {
643    for (Int rIdx = 0; rIdx <= (m_aiNumRefIdx[1]-1); rIdx ++)
644    {
645      m_apcRefPicList[1][rIdx] = m_RefPicListModification.getRefPicListModificationFlagL1() ? rpsCurrList1[ m_RefPicListModification.getRefPicSetIdxL1(rIdx) ] : rpsCurrList1[rIdx % numPocTotalCurr];
646      m_bIsUsedAsLongTerm[1][rIdx] = m_RefPicListModification.getRefPicListModificationFlagL1() ?
647                                  (m_RefPicListModification.getRefPicSetIdxL1(rIdx) >= (NumPocStCurr0 + NumPocStCurr1)): ((rIdx % numPocTotalCurr) >= (NumPocStCurr0 + NumPocStCurr1));
648    }
649  }
650}
651
652#if H_MV
653// Temporary fix for FIX1071 should be removed later
654Int TComSlice::getNumRpsCurrTempList( TComReferencePictureSet* rps /* = 0 */)
655#else
656Int TComSlice::getNumRpsCurrTempList()
657#endif
658{
659
660  Int numRpsCurrTempList = 0;
661
662  if (m_eSliceType == I_SLICE) 
663  {
664    return 0;
665  }
666#if H_MV
667// Temporary fix for FIX1071 should be removed later
668  if (rps == NULL)
669  {
670    rps = m_pcRPS; 
671  }
672
673  for(UInt i=0; i < rps->getNumberOfNegativePictures()+ rps->getNumberOfPositivePictures() + rps->getNumberOfLongtermPictures(); i++)
674  {
675    if(rps->getUsed(i))
676#else
677  for(UInt i=0; i < m_pcRPS->getNumberOfNegativePictures()+ m_pcRPS->getNumberOfPositivePictures() + m_pcRPS->getNumberOfLongtermPictures(); i++)
678  {
679    if(m_pcRPS->getUsed(i))
680#endif
681    {
682      numRpsCurrTempList++;
683    }
684  }
685#if H_MV
686  numRpsCurrTempList = numRpsCurrTempList + getVPS()->getNumDirectRefLayers( getLayerIdInVps() );
687#endif
688  return numRpsCurrTempList;
689}
690
691Void TComSlice::initEqualRef()
692{
693  for (Int iDir = 0; iDir < 2; iDir++)
694  {
695    for (Int iRefIdx1 = 0; iRefIdx1 < MAX_NUM_REF; iRefIdx1++)
696    {
697      for (Int iRefIdx2 = iRefIdx1; iRefIdx2 < MAX_NUM_REF; iRefIdx2++)
698      {
699        m_abEqualRef[iDir][iRefIdx1][iRefIdx2] = m_abEqualRef[iDir][iRefIdx2][iRefIdx1] = (iRefIdx1 == iRefIdx2? true : false);
700      }
701    }
702  }
703}
704#if H_3D
705#if H_3D_TMVP
706Void TComSlice::generateAlterRefforTMVP()
707{
708  for ( UInt uiRefListIdx = 0; uiRefListIdx < 2; uiRefListIdx++ )
709  {       
710    if ( this->getNumRefIdx( RefPicList( uiRefListIdx ) ) == 0)
711        continue;
712
713    Bool bZeroIdxLtFlag = this->getRefPic(RefPicList(uiRefListIdx), 0)->getIsLongTerm();
714    for(Int i = 1; i < this->getNumRefIdx(RefPicList(uiRefListIdx)); i++ )
715    {
716      if ( ( bZeroIdxLtFlag && !this->getRefPic(RefPicList(uiRefListIdx), i)->getIsLongTerm() ) ||
717           (!bZeroIdxLtFlag &&  this->getRefPic(RefPicList(uiRefListIdx), i)->getIsLongTerm() ) )
718      {
719        this->setAlterRefIdx(RefPicList(uiRefListIdx),i);
720        break;
721      }
722    }
723  }
724}
725#endif
726Void TComSlice::setCamparaSlice( Int** aaiScale, Int** aaiOffset )
727{ 
728  if( m_pcSPS->hasCamParInSliceHeader() )
729  {   
730    for( UInt uiBaseViewIndex = 0; uiBaseViewIndex < m_viewIndex; uiBaseViewIndex++ )
731    {
732      m_aaiCodedScale [ 0 ][ uiBaseViewIndex ] = aaiScale [ uiBaseViewIndex ][     m_viewIndex ];
733      m_aaiCodedScale [ 1 ][ uiBaseViewIndex ] = aaiScale [     m_viewIndex ][ uiBaseViewIndex ];
734      m_aaiCodedOffset[ 0 ][ uiBaseViewIndex ] = aaiOffset[ uiBaseViewIndex ][     m_viewIndex ];
735      m_aaiCodedOffset[ 1 ][ uiBaseViewIndex ] = aaiOffset[     m_viewIndex ][ uiBaseViewIndex ];
736    }
737  } 
738}
739#endif
740
741Void TComSlice::checkColRefIdx(UInt curSliceIdx, TComPic* pic)
742{
743  Int i;
744  TComSlice* curSlice = pic->getSlice(curSliceIdx);
745  Int currColRefPOC =  curSlice->getRefPOC( RefPicList(1-curSlice->getColFromL0Flag()), curSlice->getColRefIdx());
746  TComSlice* preSlice;
747  Int preColRefPOC;
748  for(i=curSliceIdx-1; i>=0; i--)
749  {
750    preSlice = pic->getSlice(i);
751    if(preSlice->getSliceType() != I_SLICE)
752    {
753      preColRefPOC  = preSlice->getRefPOC( RefPicList(1-preSlice->getColFromL0Flag()), preSlice->getColRefIdx());
754      if(currColRefPOC != preColRefPOC)
755      {
756        printf("Collocated_ref_idx shall always be the same for all slices of a coded picture!\n");
757        exit(EXIT_FAILURE);
758      }
759      else
760      {
761        break;
762      }
763    }
764  }
765}
766
767Void TComSlice::checkCRA(TComReferencePictureSet *pReferencePictureSet, Int& pocCRA, Bool& prevRAPisBLA, TComList<TComPic *>& rcListPic)
768{
769  for(Int i = 0; i < pReferencePictureSet->getNumberOfNegativePictures()+pReferencePictureSet->getNumberOfPositivePictures(); i++)
770  {
771    if(pocCRA < MAX_UINT && getPOC() > pocCRA)
772    {
773      assert(getPOC()+pReferencePictureSet->getDeltaPOC(i) >= pocCRA);
774    }
775  }
776  for(Int i = pReferencePictureSet->getNumberOfNegativePictures()+pReferencePictureSet->getNumberOfPositivePictures(); i < pReferencePictureSet->getNumberOfPictures(); i++)
777  {
778    if(pocCRA < MAX_UINT && getPOC() > pocCRA)
779    {
780      if (!pReferencePictureSet->getCheckLTMSBPresent(i))
781      {
782        assert(xGetLongTermRefPic(rcListPic, pReferencePictureSet->getPOC(i), false)->getPOC() >= pocCRA);
783      }
784      else
785      {
786      assert(pReferencePictureSet->getPOC(i) >= pocCRA);
787    }
788  }
789  }
790  if ( getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_W_RADL || getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_N_LP ) // IDR picture found
791  {
792    pocCRA = getPOC();
793    prevRAPisBLA = false;
794  }
795  else if ( getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA ) // CRA picture found
796  {
797    pocCRA = getPOC();
798    prevRAPisBLA = false;
799  }
800  else if ( getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP
801         || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL
802         || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP ) // BLA picture found
803  {
804    pocCRA = getPOC();
805    prevRAPisBLA = true;
806  }
807}
808
809/** Function for marking the reference pictures when an IDR/CRA/CRANT/BLA/BLANT is encountered.
810 * \param pocCRA POC of the CRA/CRANT/BLA/BLANT picture
811 * \param bRefreshPending flag indicating if a deferred decoding refresh is pending
812 * \param rcListPic reference to the reference picture list
813 * This function marks the reference pictures as "unused for reference" in the following conditions.
814 * If the nal_unit_type is IDR/BLA/BLANT, all pictures in the reference picture list 
815 * are marked as "unused for reference"
816 *    If the nal_unit_type is BLA/BLANT, set the pocCRA to the temporal reference of the current picture.
817 * Otherwise
818 *    If the bRefreshPending flag is true (a deferred decoding refresh is pending) and the current
819 *    temporal reference is greater than the temporal reference of the latest CRA/CRANT/BLA/BLANT picture (pocCRA),
820 *    mark all reference pictures except the latest CRA/CRANT/BLA/BLANT picture as "unused for reference" and set
821 *    the bRefreshPending flag to false.
822 *    If the nal_unit_type is CRA/CRANT, set the bRefreshPending flag to true and pocCRA to the temporal
823 *    reference of the current picture.
824 * Note that the current picture is already placed in the reference list and its marking is not changed.
825 * If the current picture has a nal_ref_idc that is not 0, it will remain marked as "used for reference".
826 */
827Void TComSlice::decodingRefreshMarking(Int& pocCRA, Bool& bRefreshPending, TComList<TComPic*>& rcListPic)
828{
829  TComPic*                 rpcPic;
830  Int pocCurr = getPOC(); 
831
832  if ( getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP
833    || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL
834    || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP
835    || getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_W_RADL
836    || getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_N_LP )  // IDR or BLA picture
837  {
838    // mark all pictures as not used for reference
839    TComList<TComPic*>::iterator        iterPic       = rcListPic.begin();
840    while (iterPic != rcListPic.end())
841    {
842      rpcPic = *(iterPic);
843      rpcPic->setCurrSliceIdx(0);
844      if (rpcPic->getPOC() != pocCurr) rpcPic->getSlice(0)->setReferenced(false);
845      iterPic++;
846    }
847    if ( getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP
848      || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL
849      || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP )
850    {
851      pocCRA = pocCurr;
852    }
853  }
854  else // CRA or No DR
855  {
856    if (bRefreshPending==true && pocCurr > pocCRA) // CRA reference marking pending
857    {
858      TComList<TComPic*>::iterator        iterPic       = rcListPic.begin();
859      while (iterPic != rcListPic.end())
860      {
861        rpcPic = *(iterPic);
862        if (rpcPic->getPOC() != pocCurr && rpcPic->getPOC() != pocCRA)
863        {
864          rpcPic->getSlice(0)->setReferenced(false);
865        }
866        iterPic++;
867      }
868      bRefreshPending = false; 
869    }
870    if ( getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA ) // CRA picture found
871    {
872      bRefreshPending = true; 
873      pocCRA = pocCurr;
874    }
875  }
876}
877
878Void TComSlice::copySliceInfo(TComSlice *pSrc)
879{
880  assert( pSrc != NULL );
881
882  Int i, j, k;
883
884  m_iPOC                 = pSrc->m_iPOC;
885  m_eNalUnitType         = pSrc->m_eNalUnitType;
886#if H_MV
887  m_layerId              = pSrc->m_layerId;
888  // GT: Copying of several other values might be be missing here, or is above not necessary?
889#endif
890  m_eSliceType           = pSrc->m_eSliceType;
891  m_iSliceQp             = pSrc->m_iSliceQp;
892#if ADAPTIVE_QP_SELECTION
893  m_iSliceQpBase         = pSrc->m_iSliceQpBase;
894#endif
895  m_deblockingFilterDisable   = pSrc->m_deblockingFilterDisable;
896  m_deblockingFilterOverrideFlag = pSrc->m_deblockingFilterOverrideFlag;
897  m_deblockingFilterBetaOffsetDiv2 = pSrc->m_deblockingFilterBetaOffsetDiv2;
898  m_deblockingFilterTcOffsetDiv2 = pSrc->m_deblockingFilterTcOffsetDiv2;
899 
900#if L0034_COMBINED_LIST_CLEANUP
901  for (i = 0; i < 2; i++)
902#else
903  for (i = 0; i < 3; i++)
904#endif
905  {
906    m_aiNumRefIdx[i]     = pSrc->m_aiNumRefIdx[i];
907  }
908
909#if L0034_COMBINED_LIST_CLEANUP
910  for (i = 0; i < MAX_NUM_REF; i++)
911  {
912    m_list1IdxToList0Idx[i] = pSrc->m_list1IdxToList0Idx[i];
913  } 
914#else
915  for (i = 0; i < 2; i++)
916  {
917    for (j = 0; j < MAX_NUM_REF_LC; j++)
918    {
919       m_iRefIdxOfLC[i][j]  = pSrc->m_iRefIdxOfLC[i][j];
920    }
921  }
922  for (i = 0; i < MAX_NUM_REF_LC; i++)
923  {
924    m_eListIdFromIdxOfLC[i] = pSrc->m_eListIdFromIdxOfLC[i];
925    m_iRefIdxFromIdxOfLC[i] = pSrc->m_iRefIdxFromIdxOfLC[i];
926    m_iRefIdxOfL1FromRefIdxOfL0[i] = pSrc->m_iRefIdxOfL1FromRefIdxOfL0[i];
927    m_iRefIdxOfL0FromRefIdxOfL1[i] = pSrc->m_iRefIdxOfL0FromRefIdxOfL1[i];
928  }
929  m_bRefPicListModificationFlagLC = pSrc->m_bRefPicListModificationFlagLC;
930  m_bRefPicListCombinationFlag    = pSrc->m_bRefPicListCombinationFlag;
931#endif
932  m_bCheckLDC             = pSrc->m_bCheckLDC;
933  m_iSliceQpDelta        = pSrc->m_iSliceQpDelta;
934  m_iSliceQpDeltaCb      = pSrc->m_iSliceQpDeltaCb;
935  m_iSliceQpDeltaCr      = pSrc->m_iSliceQpDeltaCr;
936  for (i = 0; i < 2; i++)
937  {
938    for (j = 0; j < MAX_NUM_REF; j++)
939    {
940      m_apcRefPicList[i][j]  = pSrc->m_apcRefPicList[i][j];
941      m_aiRefPOCList[i][j]   = pSrc->m_aiRefPOCList[i][j];
942#if H_MV
943      m_aiRefLayerIdList[i][j] = pSrc->m_aiRefLayerIdList[i][j];
944#endif
945    }
946  }
947  for (i = 0; i < 2; i++)
948  {
949    for (j = 0; j < MAX_NUM_REF + 1; j++)
950    {
951      m_bIsUsedAsLongTerm[i][j] = pSrc->m_bIsUsedAsLongTerm[i][j];
952    }
953  }
954  m_iDepth               = pSrc->m_iDepth;
955
956  // referenced slice
957  m_bRefenced            = pSrc->m_bRefenced;
958
959  // access channel
960#if H_MV
961  m_pcVPS                = pSrc->m_pcVPS;
962#endif
963  m_pcSPS                = pSrc->m_pcSPS;
964  m_pcPPS                = pSrc->m_pcPPS;
965  m_pcRPS                = pSrc->m_pcRPS;
966  m_iLastIDR             = pSrc->m_iLastIDR;
967
968  m_pcPic                = pSrc->m_pcPic;
969
970  m_colFromL0Flag        = pSrc->m_colFromL0Flag;
971  m_colRefIdx            = pSrc->m_colRefIdx;
972#if SAO_CHROMA_LAMBDA
973  m_dLambdaLuma          = pSrc->m_dLambdaLuma;
974  m_dLambdaChroma        = pSrc->m_dLambdaChroma;
975#else
976  m_dLambda              = pSrc->m_dLambda;
977#endif
978  for (i = 0; i < 2; i++)
979  {
980    for (j = 0; j < MAX_NUM_REF; j++)
981    {
982      for (k =0; k < MAX_NUM_REF; k++)
983      {
984        m_abEqualRef[i][j][k] = pSrc->m_abEqualRef[i][j][k];
985      }
986    }
987  }
988
989#if !L0034_COMBINED_LIST_CLEANUP
990  m_bNoBackPredFlag      = pSrc->m_bNoBackPredFlag;
991#endif
992  m_uiTLayer                      = pSrc->m_uiTLayer;
993  m_bTLayerSwitchingFlag          = pSrc->m_bTLayerSwitchingFlag;
994
995  m_sliceMode                   = pSrc->m_sliceMode;
996  m_sliceArgument               = pSrc->m_sliceArgument;
997  m_sliceCurStartCUAddr         = pSrc->m_sliceCurStartCUAddr;
998  m_sliceCurEndCUAddr           = pSrc->m_sliceCurEndCUAddr;
999  m_sliceIdx                    = pSrc->m_sliceIdx;
1000  m_sliceSegmentMode            = pSrc->m_sliceSegmentMode;
1001  m_sliceSegmentArgument        = pSrc->m_sliceSegmentArgument; 
1002  m_sliceSegmentCurStartCUAddr  = pSrc->m_sliceSegmentCurStartCUAddr;
1003  m_sliceSegmentCurEndCUAddr    = pSrc->m_sliceSegmentCurEndCUAddr;
1004  m_nextSlice                    = pSrc->m_nextSlice;
1005  m_nextSliceSegment             = pSrc->m_nextSliceSegment;
1006  for ( Int e=0 ; e<2 ; e++ )
1007  {
1008    for ( Int n=0 ; n<MAX_NUM_REF ; n++ )
1009    {
1010      memcpy(m_weightPredTable[e][n], pSrc->m_weightPredTable[e][n], sizeof(wpScalingParam)*3 );
1011    }
1012  }
1013  m_saoEnabledFlag = pSrc->m_saoEnabledFlag; 
1014  m_saoEnabledFlagChroma = pSrc->m_saoEnabledFlagChroma;
1015  m_cabacInitFlag                = pSrc->m_cabacInitFlag;
1016  m_numEntryPointOffsets  = pSrc->m_numEntryPointOffsets;
1017
1018  m_bLMvdL1Zero = pSrc->m_bLMvdL1Zero;
1019  m_LFCrossSliceBoundaryFlag = pSrc->m_LFCrossSliceBoundaryFlag;
1020  m_enableTMVPFlag                = pSrc->m_enableTMVPFlag;
1021  m_maxNumMergeCand               = pSrc->m_maxNumMergeCand;
1022}
1023
1024Int TComSlice::m_prevPOC = 0;
1025
1026/** Function for setting the slice's temporal layer ID and corresponding temporal_layer_switching_point_flag.
1027 * \param uiTLayer Temporal layer ID of the current slice
1028 * The decoder calls this function to set temporal_layer_switching_point_flag for each temporal layer based on
1029 * the SPS's temporal_id_nesting_flag and the parsed PPS.  Then, current slice's temporal layer ID and
1030 * temporal_layer_switching_point_flag is set accordingly.
1031 */
1032Void TComSlice::setTLayerInfo( UInt uiTLayer )
1033{
1034  m_uiTLayer = uiTLayer;
1035}
1036
1037/** Function for checking if this is a switching-point
1038*/
1039Bool TComSlice::isTemporalLayerSwitchingPoint( TComList<TComPic*>& rcListPic )
1040{
1041  TComPic* rpcPic;
1042  // loop through all pictures in the reference picture buffer
1043  TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1044  while ( iterPic != rcListPic.end())
1045  {
1046    rpcPic = *(iterPic++);
1047    if(rpcPic->getSlice(0)->isReferenced() && rpcPic->getPOC() != getPOC())
1048    {
1049      if(rpcPic->getTLayer() >= getTLayer())
1050      {
1051        return false;
1052      }
1053    }
1054  }
1055  return true;
1056}
1057
1058/** Function for checking if this is a STSA candidate
1059 */
1060Bool TComSlice::isStepwiseTemporalLayerSwitchingPointCandidate( TComList<TComPic*>& rcListPic )
1061{
1062    TComPic* rpcPic;
1063   
1064    TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1065    while ( iterPic != rcListPic.end())
1066    {
1067        rpcPic = *(iterPic++);
1068        if(rpcPic->getSlice(0)->isReferenced() &&  (rpcPic->getUsedByCurr()==true) && rpcPic->getPOC() != getPOC())
1069        {
1070            if(rpcPic->getTLayer() >= getTLayer())
1071            {
1072                return false;
1073            }
1074        }
1075    }
1076    return true;
1077}
1078
1079/** Function for applying picture marking based on the Reference Picture Set in pReferencePictureSet.
1080*/
1081Void TComSlice::applyReferencePictureSet( TComList<TComPic*>& rcListPic, TComReferencePictureSet *pReferencePictureSet)
1082{
1083  TComPic* rpcPic;
1084  Int i, isReference;
1085
1086  // loop through all pictures in the reference picture buffer
1087  TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1088  while ( iterPic != rcListPic.end())
1089  {
1090    rpcPic = *(iterPic++);
1091
1092    if(!rpcPic->getSlice( 0 )->isReferenced())
1093    {
1094      continue;
1095    }
1096
1097    isReference = 0;
1098    // loop through all pictures in the Reference Picture Set
1099    // to see if the picture should be kept as reference picture
1100    for(i=0;i<pReferencePictureSet->getNumberOfPositivePictures()+pReferencePictureSet->getNumberOfNegativePictures();i++)
1101    {
1102      if(!rpcPic->getIsLongTerm() && rpcPic->getPicSym()->getSlice(0)->getPOC() == this->getPOC() + pReferencePictureSet->getDeltaPOC(i))
1103      {
1104        isReference = 1;
1105        rpcPic->setUsedByCurr(pReferencePictureSet->getUsed(i));
1106        rpcPic->setIsLongTerm(0);
1107      }
1108    }
1109    for(;i<pReferencePictureSet->getNumberOfPictures();i++)
1110    {
1111      if(pReferencePictureSet->getCheckLTMSBPresent(i)==true)
1112      {
1113        if(rpcPic->getIsLongTerm() && (rpcPic->getPicSym()->getSlice(0)->getPOC()) == pReferencePictureSet->getPOC(i))
1114        {
1115          isReference = 1;
1116          rpcPic->setUsedByCurr(pReferencePictureSet->getUsed(i));
1117        }
1118      }
1119      else 
1120      {
1121        if(rpcPic->getIsLongTerm() && (rpcPic->getPicSym()->getSlice(0)->getPOC()%(1<<rpcPic->getPicSym()->getSlice(0)->getSPS()->getBitsForPOC())) == pReferencePictureSet->getPOC(i)%(1<<rpcPic->getPicSym()->getSlice(0)->getSPS()->getBitsForPOC()))
1122        {
1123          isReference = 1;
1124          rpcPic->setUsedByCurr(pReferencePictureSet->getUsed(i));
1125        }
1126      }
1127
1128    }
1129    // mark the picture as "unused for reference" if it is not in
1130    // the Reference Picture Set
1131    if(rpcPic->getPicSym()->getSlice(0)->getPOC() != this->getPOC() && isReference == 0)   
1132    {           
1133      rpcPic->getSlice( 0 )->setReferenced( false );   
1134      rpcPic->setUsedByCurr(0);
1135      rpcPic->setIsLongTerm(0);
1136    }
1137    //check that pictures of higher temporal layers are not used
1138    assert(rpcPic->getSlice( 0 )->isReferenced()==0||rpcPic->getUsedByCurr()==0||rpcPic->getTLayer()<=this->getTLayer());
1139    //check that pictures of higher or equal temporal layer are not in the RPS if the current picture is a TSA picture
1140    if(this->getNalUnitType() == NAL_UNIT_CODED_SLICE_TLA_R || this->getNalUnitType() == NAL_UNIT_CODED_SLICE_TSA_N)
1141    {
1142      assert(rpcPic->getSlice( 0 )->isReferenced()==0||rpcPic->getTLayer()<this->getTLayer());
1143    }
1144    //check that pictures marked as temporal layer non-reference pictures are not used for reference
1145    if(rpcPic->getPicSym()->getSlice(0)->getPOC() != this->getPOC() && rpcPic->getTLayer()==this->getTLayer())
1146    {
1147      assert(rpcPic->getSlice( 0 )->isReferenced()==0||rpcPic->getUsedByCurr()==0||rpcPic->getSlice( 0 )->getTemporalLayerNonReferenceFlag()==false);
1148    }
1149  }
1150}
1151
1152/** Function for applying picture marking based on the Reference Picture Set in pReferencePictureSet.
1153*/
1154Int TComSlice::checkThatAllRefPicsAreAvailable( TComList<TComPic*>& rcListPic, TComReferencePictureSet *pReferencePictureSet, Bool printErrors, Int pocRandomAccess)
1155{
1156  TComPic* rpcPic;
1157  Int i, isAvailable;
1158  Int atLeastOneLost = 0;
1159  Int atLeastOneRemoved = 0;
1160  Int iPocLost = 0;
1161
1162  // loop through all long-term pictures in the Reference Picture Set
1163  // to see if the picture should be kept as reference picture
1164  for(i=pReferencePictureSet->getNumberOfNegativePictures()+pReferencePictureSet->getNumberOfPositivePictures();i<pReferencePictureSet->getNumberOfPictures();i++)
1165  {
1166    isAvailable = 0;
1167    // loop through all pictures in the reference picture buffer
1168    TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1169    while ( iterPic != rcListPic.end())
1170    {
1171      rpcPic = *(iterPic++);
1172      if(pReferencePictureSet->getCheckLTMSBPresent(i)==true)
1173      {
1174        if(rpcPic->getIsLongTerm() && (rpcPic->getPicSym()->getSlice(0)->getPOC()) == pReferencePictureSet->getPOC(i) && rpcPic->getSlice(0)->isReferenced())
1175        {
1176          isAvailable = 1;
1177        }
1178      }
1179      else 
1180      {
1181        if(rpcPic->getIsLongTerm() && (rpcPic->getPicSym()->getSlice(0)->getPOC()%(1<<rpcPic->getPicSym()->getSlice(0)->getSPS()->getBitsForPOC())) == pReferencePictureSet->getPOC(i)%(1<<rpcPic->getPicSym()->getSlice(0)->getSPS()->getBitsForPOC()) && rpcPic->getSlice(0)->isReferenced())
1182        {
1183          isAvailable = 1;
1184        }
1185      }
1186    }
1187    // if there was no such long-term check the short terms
1188    if(!isAvailable)
1189    {
1190      iterPic = rcListPic.begin();
1191      while ( iterPic != rcListPic.end())
1192      {
1193        rpcPic = *(iterPic++);
1194
1195        Int pocCycle = 1 << rpcPic->getPicSym()->getSlice(0)->getSPS()->getBitsForPOC();
1196        Int curPoc = rpcPic->getPicSym()->getSlice(0)->getPOC();
1197        Int refPoc = pReferencePictureSet->getPOC(i);
1198        if (!pReferencePictureSet->getCheckLTMSBPresent(i))
1199        {
1200          curPoc = curPoc % pocCycle;
1201          refPoc = refPoc % pocCycle;
1202        }
1203       
1204        if (rpcPic->getSlice(0)->isReferenced() && curPoc == refPoc)
1205        {
1206          isAvailable = 1;
1207          rpcPic->setIsLongTerm(1);
1208          break;
1209        }
1210      }
1211    }
1212    // report that a picture is lost if it is in the Reference Picture Set
1213    // but not available as reference picture
1214    if(isAvailable == 0)   
1215    {           
1216      if (this->getPOC() + pReferencePictureSet->getDeltaPOC(i) >= pocRandomAccess)
1217      {
1218        if(!pReferencePictureSet->getUsed(i) )
1219        {
1220          if(printErrors)
1221          {
1222            printf("\nLong-term reference picture with POC = %3d seems to have been removed or not correctly decoded.", this->getPOC() + pReferencePictureSet->getDeltaPOC(i));
1223          }
1224          atLeastOneRemoved = 1;
1225        }
1226        else
1227        {
1228          if(printErrors)
1229          {
1230            printf("\nLong-term reference picture with POC = %3d is lost or not correctly decoded!", this->getPOC() + pReferencePictureSet->getDeltaPOC(i));
1231          }
1232          atLeastOneLost = 1;
1233          iPocLost=this->getPOC() + pReferencePictureSet->getDeltaPOC(i);
1234        }
1235      }
1236    }
1237  } 
1238  // loop through all short-term pictures in the Reference Picture Set
1239  // to see if the picture should be kept as reference picture
1240  for(i=0;i<pReferencePictureSet->getNumberOfNegativePictures()+pReferencePictureSet->getNumberOfPositivePictures();i++)
1241  {
1242    isAvailable = 0;
1243    // loop through all pictures in the reference picture buffer
1244    TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1245    while ( iterPic != rcListPic.end())
1246    {
1247      rpcPic = *(iterPic++);
1248
1249      if(!rpcPic->getIsLongTerm() && rpcPic->getPicSym()->getSlice(0)->getPOC() == this->getPOC() + pReferencePictureSet->getDeltaPOC(i) && rpcPic->getSlice(0)->isReferenced())
1250      {
1251        isAvailable = 1;
1252      }
1253    }
1254    // report that a picture is lost if it is in the Reference Picture Set
1255    // but not available as reference picture
1256    if(isAvailable == 0)   
1257    {           
1258      if (this->getPOC() + pReferencePictureSet->getDeltaPOC(i) >= pocRandomAccess)
1259      {
1260        if(!pReferencePictureSet->getUsed(i) )
1261        {
1262          if(printErrors)
1263          {
1264            printf("\nShort-term reference picture with POC = %3d seems to have been removed or not correctly decoded.", this->getPOC() + pReferencePictureSet->getDeltaPOC(i));
1265          }
1266          atLeastOneRemoved = 1;
1267        }
1268        else
1269        {
1270          if(printErrors)
1271          {
1272            printf("\nShort-term reference picture with POC = %3d is lost or not correctly decoded!", this->getPOC() + pReferencePictureSet->getDeltaPOC(i));
1273          }
1274          atLeastOneLost = 1;
1275          iPocLost=this->getPOC() + pReferencePictureSet->getDeltaPOC(i);
1276        }
1277      }
1278    }
1279  }   
1280  if(atLeastOneLost)
1281  {
1282    return iPocLost+1;
1283  }
1284  if(atLeastOneRemoved)
1285  {
1286    return -2;
1287  }
1288  else
1289  {
1290    return 0;
1291  }
1292}
1293
1294/** Function for constructing an explicit Reference Picture Set out of the available pictures in a referenced Reference Picture Set
1295*/
1296Void TComSlice::createExplicitReferencePictureSetFromReference( TComList<TComPic*>& rcListPic, TComReferencePictureSet *pReferencePictureSet)
1297{
1298  TComPic* rpcPic;
1299  Int i, j;
1300  Int k = 0;
1301  Int nrOfNegativePictures = 0;
1302  Int nrOfPositivePictures = 0;
1303  TComReferencePictureSet* pcRPS = this->getLocalRPS();
1304
1305  // loop through all pictures in the Reference Picture Set
1306  for(i=0;i<pReferencePictureSet->getNumberOfPictures();i++)
1307  {
1308    j = 0;
1309    // loop through all pictures in the reference picture buffer
1310    TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1311    while ( iterPic != rcListPic.end())
1312    {
1313      j++;
1314      rpcPic = *(iterPic++);
1315
1316      if(rpcPic->getPicSym()->getSlice(0)->getPOC() == this->getPOC() + pReferencePictureSet->getDeltaPOC(i) && rpcPic->getSlice(0)->isReferenced())
1317      {
1318        // This picture exists as a reference picture
1319        // and should be added to the explicit Reference Picture Set
1320        pcRPS->setDeltaPOC(k, pReferencePictureSet->getDeltaPOC(i));
1321        pcRPS->setUsed(k, pReferencePictureSet->getUsed(i));
1322        if(pcRPS->getDeltaPOC(k) < 0)
1323        {
1324          nrOfNegativePictures++;
1325        }
1326        else
1327        {
1328          nrOfPositivePictures++;
1329        }
1330        k++;
1331      }
1332    }
1333  }
1334  pcRPS->setNumberOfNegativePictures(nrOfNegativePictures);
1335  pcRPS->setNumberOfPositivePictures(nrOfPositivePictures);
1336  pcRPS->setNumberOfPictures(nrOfNegativePictures+nrOfPositivePictures);
1337  // This is a simplistic inter rps example. A smarter encoder will look for a better reference RPS to do the
1338  // inter RPS prediction with.  Here we just use the reference used by pReferencePictureSet.
1339  // If pReferencePictureSet is not inter_RPS_predicted, then inter_RPS_prediction is for the current RPS also disabled.
1340  if (!pReferencePictureSet->getInterRPSPrediction())
1341  {
1342    pcRPS->setInterRPSPrediction(false);
1343    pcRPS->setNumRefIdc(0);
1344  }
1345  else
1346  {
1347    Int rIdx =  this->getRPSidx() - pReferencePictureSet->getDeltaRIdxMinus1() - 1;
1348    Int deltaRPS = pReferencePictureSet->getDeltaRPS();
1349    TComReferencePictureSet* pcRefRPS = this->getSPS()->getRPSList()->getReferencePictureSet(rIdx);
1350    Int iRefPics = pcRefRPS->getNumberOfPictures();
1351    Int iNewIdc=0;
1352    for(i=0; i<= iRefPics; i++) 
1353    {
1354      Int deltaPOC = ((i != iRefPics)? pcRefRPS->getDeltaPOC(i) : 0);  // check if the reference abs POC is >= 0
1355      Int iRefIdc = 0;
1356      for (j=0; j < pcRPS->getNumberOfPictures(); j++) // loop through the  pictures in the new RPS
1357      {
1358        if ( (deltaPOC + deltaRPS) == pcRPS->getDeltaPOC(j))
1359        {
1360          if (pcRPS->getUsed(j))
1361          {
1362            iRefIdc = 1;
1363          }
1364          else
1365          {
1366            iRefIdc = 2;
1367          }
1368        }
1369      }
1370      pcRPS->setRefIdc(i, iRefIdc);
1371      iNewIdc++;
1372    }
1373    pcRPS->setInterRPSPrediction(true);
1374    pcRPS->setNumRefIdc(iNewIdc);
1375    pcRPS->setDeltaRPS(deltaRPS); 
1376    pcRPS->setDeltaRIdxMinus1(pReferencePictureSet->getDeltaRIdxMinus1() + this->getSPS()->getRPSList()->getNumberOfReferencePictureSets() - this->getRPSidx());
1377  }
1378
1379  this->setRPS(pcRPS);
1380  this->setRPSidx(-1);
1381}
1382
1383/** get AC and DC values for weighted pred
1384 * \param *wp
1385 * \returns Void
1386 */
1387Void  TComSlice::getWpAcDcParam(wpACDCParam *&wp)
1388{
1389  wp = m_weightACDCParam;
1390}
1391
1392/** init AC and DC values for weighted pred
1393 * \returns Void
1394 */
1395Void  TComSlice::initWpAcDcParam()
1396{
1397  for(Int iComp = 0; iComp < 3; iComp++ )
1398  {
1399    m_weightACDCParam[iComp].iAC = 0;
1400    m_weightACDCParam[iComp].iDC = 0;
1401  }
1402}
1403
1404/** get WP tables for weighted pred
1405 * \param RefPicList
1406 * \param iRefIdx
1407 * \param *&wpScalingParam
1408 * \returns Void
1409 */
1410Void  TComSlice::getWpScaling( RefPicList e, Int iRefIdx, wpScalingParam *&wp )
1411{
1412  wp = m_weightPredTable[e][iRefIdx];
1413}
1414
1415/** reset Default WP tables settings : no weight.
1416 * \param wpScalingParam
1417 * \returns Void
1418 */
1419Void  TComSlice::resetWpScaling()
1420{
1421  for ( Int e=0 ; e<2 ; e++ )
1422  {
1423    for ( Int i=0 ; i<MAX_NUM_REF ; i++ )
1424    {
1425      for ( Int yuv=0 ; yuv<3 ; yuv++ )
1426      {
1427        wpScalingParam  *pwp = &(m_weightPredTable[e][i][yuv]);
1428        pwp->bPresentFlag      = false;
1429        pwp->uiLog2WeightDenom = 0;
1430        pwp->uiLog2WeightDenom = 0;
1431        pwp->iWeight           = 1;
1432        pwp->iOffset           = 0;
1433      }
1434    }
1435  }
1436}
1437
1438/** init WP table
1439 * \returns Void
1440 */
1441Void  TComSlice::initWpScaling()
1442{
1443  for ( Int e=0 ; e<2 ; e++ )
1444  {
1445    for ( Int i=0 ; i<MAX_NUM_REF ; i++ )
1446    {
1447      for ( Int yuv=0 ; yuv<3 ; yuv++ )
1448      {
1449        wpScalingParam  *pwp = &(m_weightPredTable[e][i][yuv]);
1450        if ( !pwp->bPresentFlag ) 
1451        {
1452          // Inferring values not present :
1453          pwp->iWeight = (1 << pwp->uiLog2WeightDenom);
1454          pwp->iOffset = 0;
1455        }
1456
1457        pwp->w      = pwp->iWeight;
1458        Int bitDepth = yuv ? g_bitDepthC : g_bitDepthY;
1459        pwp->o      = pwp->iOffset << (bitDepth-8);
1460        pwp->shift  = pwp->uiLog2WeightDenom;
1461        pwp->round  = (pwp->uiLog2WeightDenom>=1) ? (1 << (pwp->uiLog2WeightDenom-1)) : (0);
1462      }
1463    }
1464  }
1465}
1466
1467// ------------------------------------------------------------------------------------------------
1468// Video parameter set (VPS)
1469// ------------------------------------------------------------------------------------------------
1470TComVPS::TComVPS()
1471: m_VPSId                     (  0)
1472, m_uiMaxTLayers              (  1)
1473, m_uiMaxLayers               (  1)
1474, m_bTemporalIdNestingFlag    (false)
1475, m_numHrdParameters          (  0)
1476#if H_MV
1477, m_maxNuhLayerId             (  0)
1478#else
1479, m_maxNuhReservedZeroLayerId (  0)
1480#endif
1481, m_hrdParameters             (NULL)
1482, m_hrdOpSetIdx               (NULL)
1483, m_cprmsPresentFlag          (NULL)
1484{
1485  for( Int i = 0; i < MAX_TLAYER; i++)
1486  {
1487    m_numReorderPics[i] = 0;
1488#if L0323_DPB
1489    m_uiMaxDecPicBuffering[i] = 1; 
1490#else
1491    m_uiMaxDecPicBuffering[i] = 0; 
1492#endif
1493    m_uiMaxLatencyIncrease[i] = 0;
1494  }
1495#if H_MV
1496  m_avcBaseLayerFlag = false;
1497  m_splittingFlag    = false;
1498 
1499  for( Int i = 0; i < MAX_NUM_SCALABILITY_TYPES; i++ )
1500  {
1501    m_scalabilityMask[i] = false;
1502    m_dimensionIdLen [i]  = -1; 
1503  }
1504
1505  m_vpsNuhLayerIdPresentFlag = false;
1506  m_numOutputLayerSets       = 0;
1507
1508  for( Int i = 0; i < MAX_VPS_OP_SETS_PLUS1; i++ )
1509  {
1510    m_vpsProfilePresentFlag   [i] = false;
1511    m_profileLayerSetRefMinus1[i] = 0;
1512    m_outputLayerSetIdx       [i] = 0;
1513    for( Int j = 0; j < MAX_VPS_NUH_LAYER_ID_PLUS1; j++ )
1514    {
1515      m_outputLayerFlag[i][j] = false;
1516    }
1517  }
1518
1519  for( Int i = 0; i < MAX_NUM_LAYER_IDS; i++ )
1520  {
1521    m_layerIdInVps[i] =  (i == 0 ) ? 0 : -1;         
1522  }
1523
1524  for( Int i = 0; i < MAX_NUM_LAYERS; i++ )
1525  {
1526    m_layerIdInNuh      [i] = ( i == 0 ) ? 0 : -1; 
1527    m_numDirectRefLayers[i] = 0; 
1528#if H_3D
1529    m_viewIndex         [i] = -1; 
1530#endif
1531
1532    for( Int j = 0; j < MAX_NUM_LAYERS; j++ )
1533    {
1534      m_directDependencyFlag[i][j] = false;
1535      m_refLayerId[i][j]           = -1; 
1536    }
1537
1538    for( Int j = 0; j < MAX_NUM_SCALABILITY_TYPES; j++ )
1539    {
1540      m_dimensionId[i][j] = 0;
1541    }
1542  }
1543#if H_3D_GEN
1544  for( Int i = 0; i < MAX_NUM_LAYERS; i++ )  {
1545#if H_3D_IV_MERGE
1546    m_ivMvPredFlag         [ i ] = false;
1547#endif
1548#if H_3D_VSP
1549    m_viewSynthesisPredFlag[ i ] = false;
1550#endif
1551#if H_3D_NBDV_REF
1552    m_depthRefinementFlag  [ i ] = false;
1553#endif
1554  }
1555#endif
1556#endif
1557}
1558
1559TComVPS::~TComVPS()
1560{
1561if( m_hrdParameters    != NULL )     delete[] m_hrdParameters;
1562  if( m_hrdOpSetIdx      != NULL )     delete[] m_hrdOpSetIdx;
1563  if( m_cprmsPresentFlag != NULL )     delete[] m_cprmsPresentFlag;
1564}
1565
1566#if H_MV
1567
1568Bool TComVPS::checkVPSExtensionSyntax()
1569{
1570  // check splitting flag constraint
1571  if ( getSplittingFlag() )
1572  {
1573    // Derive dimBitOffset[j]
1574    Int dimBitOffset[MAX_NUM_SCALABILITY_TYPES+1];
1575    Int numScalabilityTypes = getNumScalabilityTypes();
1576    dimBitOffset[0] = 0;
1577
1578    for (Int type = 1; type <= numScalabilityTypes; type++ )
1579    {
1580      dimBitOffset[ type ] = 0;
1581      for (Int dimIdx = 0; dimIdx <= type - 1; dimIdx++)
1582        dimBitOffset[ type ] += ( getDimensionIdLen( dimIdx ) );
1583    }
1584
1585    for (Int type = 0; type < getNumScalabilityTypes(); type++ )
1586    {
1587      for( Int layer = 1; layer < getMaxLayers(); layer++ )
1588      {
1589        assert( getDimensionId( layer, type ) == ( ( getLayerIdInNuh( layer ) & ( (1 << dimBitOffset[ type + 1 ] ) - 1) ) >> dimBitOffset[ type ] ) );
1590      }; 
1591  };
1592  }
1593
1594  for( Int layer = 1; layer < getMaxLayers(); layer++ )
1595  {
1596    // check layer_id_in_nuh constraint
1597    assert( getLayerIdInNuh( layer ) > getLayerIdInNuh( layer -1 ) );
1598  }
1599  return true; 
1600}
1601
1602Int TComVPS::getNumScalabilityTypes()
1603{
1604  return scalTypeToScalIdx( ScalabilityType(MAX_NUM_SCALABILITY_TYPES) );
1605}
1606
1607Int TComVPS::scalTypeToScalIdx( ScalabilityType scalType )
1608{
1609  assert( scalType >= 0 && scalType <= MAX_NUM_SCALABILITY_TYPES ); 
1610  assert( scalType == MAX_NUM_SCALABILITY_TYPES || getScalabilityMask( scalType ) );
1611
1612  Int scalIdx = 0; 
1613  for( Int curScalType = 0; curScalType < scalType; curScalType++ )
1614  {
1615    scalIdx += ( getScalabilityMask( curScalType ) ? 1 : 0 );
1616  }
1617
1618  return scalIdx; 
1619}
1620
1621
1622
1623Void TComVPS::setScalabilityMask( UInt val )
1624{
1625  for ( Int scalType = 0; scalType < MAX_NUM_SCALABILITY_TYPES; scalType++ ) 
1626    setScalabilityMask( scalType, ( val & (1 << scalType ) ) != 0 );
1627}
1628
1629Void TComVPS::calcIvRefLayers()
1630{
1631  for( Int i = 1; i <= getMaxLayers(); i++ )
1632  { 
1633    m_numDirectRefLayers[ i ] = 0; 
1634    for( Int j = 0 ; j < i; j++ )
1635      if( m_directDependencyFlag[ i ][ j ])
1636        m_refLayerId[ i ][ m_numDirectRefLayers[ i ]++ ] = m_layerIdInNuh[ j ];   
1637  }
1638}
1639
1640Int TComVPS::getRefLayerId( Int layerIdInVps, Int idx )
1641{
1642  assert( idx >= 0 && idx < m_numDirectRefLayers[layerIdInVps] );     
1643  Int layerIdInNuh = m_refLayerId[ layerIdInVps ][ idx ];   
1644  assert ( layerIdInNuh >= 0 ); 
1645  return layerIdInNuh;
1646}
1647
1648Int TComVPS::getScalabilityId( Int layerIdInVps, ScalabilityType scalType )
1649{
1650  return getScalabilityMask( scalType ) ? getDimensionId( layerIdInVps, scalTypeToScalIdx( scalType ) ) : 0;
1651}
1652
1653#if H_3D
1654Void TComVPS::initViewIndex()
1655{
1656  Int viewIdList   [ MAX_NUM_LAYERS ]; // ed. should be changed to MAX_VIEW_ID
1657  Int viewIndexList[ MAX_NUM_LAYERS ]; 
1658  Int numViewIds = 0; 
1659
1660  for ( Int i = 0 ; i  <  m_uiMaxLayers; i++ )
1661  {     
1662    Int currViewId = getViewId( i ); 
1663
1664    Bool viewIdInListFlag = false; 
1665    for ( Int j = 0; j < numViewIds; j ++ )
1666    {
1667      viewIdInListFlag  = viewIdInListFlag || ( currViewId  == viewIdList[ j ]  );
1668    }
1669
1670    if ( !viewIdInListFlag ) 
1671    {
1672      viewIdList   [ numViewIds ] = currViewId;
1673      viewIndexList[ currViewId ] = numViewIds;
1674
1675      numViewIds++;
1676    } 
1677
1678    m_viewIndex[ i ] = viewIndexList[ currViewId ]; 
1679  }
1680}
1681
1682Int TComVPS::getLayerIdInNuh( Int viewIndex, Bool depthFlag )
1683{
1684  Int foundlayerId = -1; 
1685
1686  for (Int layer = 0 ; layer < m_uiMaxLayers; layer++ )
1687  {
1688    if( ( getViewIndex( layer ) == viewIndex ) && ( getDepthId( layer ) == ( depthFlag ? 1 : 0 ) )  )
1689    {
1690      foundlayerId = layer; 
1691      break; 
1692    }
1693  }
1694  assert( foundlayerId != -1 ); 
1695
1696  return getLayerIdInNuh( foundlayerId );
1697}
1698#endif // H_3D
1699#endif // H_MV
1700
1701// ------------------------------------------------------------------------------------------------
1702// Sequence parameter set (SPS)
1703// ------------------------------------------------------------------------------------------------
1704
1705TComSPS::TComSPS()
1706: m_SPSId                     (  0)
1707, m_VPSId                     (  0)
1708, m_chromaFormatIdc           (CHROMA_420)
1709, m_uiMaxTLayers              (  1)
1710// Structure
1711, m_picWidthInLumaSamples     (352)
1712, m_picHeightInLumaSamples    (288)
1713, m_log2MinCodingBlockSize    (  0)
1714, m_log2DiffMaxMinCodingBlockSize (0)
1715, m_uiMaxCUWidth              ( 32)
1716, m_uiMaxCUHeight             ( 32)
1717, m_uiMaxCUDepth              (  3)
1718, m_bLongTermRefsPresent      (false)
1719, m_uiQuadtreeTULog2MaxSize   (  0)
1720, m_uiQuadtreeTULog2MinSize   (  0)
1721, m_uiQuadtreeTUMaxDepthInter (  0)
1722, m_uiQuadtreeTUMaxDepthIntra (  0)
1723// Tool list
1724, m_usePCM                   (false)
1725, m_pcmLog2MaxSize            (  5)
1726, m_uiPCMLog2MinSize          (  7)
1727#if !L0034_COMBINED_LIST_CLEANUP
1728, m_bUseLComb                 (false)
1729#endif
1730, m_bitDepthY                 (  8)
1731, m_bitDepthC                 (  8)
1732, m_qpBDOffsetY               (  0)
1733, m_qpBDOffsetC               (  0)
1734, m_useLossless               (false)
1735, m_uiPCMBitDepthLuma         (  8)
1736, m_uiPCMBitDepthChroma       (  8)
1737, m_bPCMFilterDisableFlag     (false)
1738, m_uiBitsForPOC              (  8)
1739, m_numLongTermRefPicSPS    (  0) 
1740, m_uiMaxTrSize               ( 32)
1741, m_bUseSAO                   (false) 
1742, m_bTemporalIdNestingFlag    (false)
1743, m_scalingListEnabledFlag    (false)
1744, m_useStrongIntraSmoothing   (false)
1745, m_vuiParametersPresentFlag  (false)
1746, m_vuiParameters             ()
1747#if H_MV
1748, m_interViewMvVertConstraintFlag (false)
1749#endif
1750#if H_3D
1751, m_bCamParInSliceHeader      (false)
1752#endif
1753{
1754  for ( Int i = 0; i < MAX_TLAYER; i++ )
1755  {
1756    m_uiMaxLatencyIncrease[i] = 0;
1757#if L0323_DPB
1758    m_uiMaxDecPicBuffering[i] = 1;
1759#else
1760    m_uiMaxDecPicBuffering[i] = 0;
1761#endif
1762    m_numReorderPics[i]       = 0;
1763  }
1764  m_scalingList = new TComScalingList;
1765  ::memset(m_ltRefPicPocLsbSps, 0, sizeof(m_ltRefPicPocLsbSps));
1766  ::memset(m_usedByCurrPicLtSPSFlag, 0, sizeof(m_usedByCurrPicLtSPSFlag));
1767}
1768
1769TComSPS::~TComSPS()
1770{
1771  delete m_scalingList;
1772  m_RPSList.destroy();
1773}
1774
1775Void  TComSPS::createRPSList( Int numRPS )
1776{ 
1777  m_RPSList.destroy();
1778  m_RPSList.create(numRPS);
1779}
1780
1781Void TComSPS::setHrdParameters( UInt frameRate, UInt numDU, UInt bitRate, Bool randomAccess )
1782{
1783  if( !getVuiParametersPresentFlag() )
1784  {
1785    return;
1786  }
1787
1788  TComVUI *vui = getVuiParameters();
1789  TComHRD *hrd = vui->getHrdParameters();
1790
1791#if L0043_TIMING_INFO
1792  TimingInfo *timingInfo = vui->getTimingInfo();
1793  timingInfo->setTimingInfoPresentFlag( true );
1794  switch( frameRate )
1795  {
1796  case 24:
1797    timingInfo->setNumUnitsInTick( 1125000 );    timingInfo->setTimeScale    ( 27000000 );
1798    break;
1799  case 25:
1800    timingInfo->setNumUnitsInTick( 1080000 );    timingInfo->setTimeScale    ( 27000000 );
1801    break;
1802  case 30:
1803    timingInfo->setNumUnitsInTick( 900900 );     timingInfo->setTimeScale    ( 27000000 );
1804    break;
1805  case 50:
1806    timingInfo->setNumUnitsInTick( 540000 );     timingInfo->setTimeScale    ( 27000000 );
1807    break;
1808  case 60:
1809    timingInfo->setNumUnitsInTick( 450450 );     timingInfo->setTimeScale    ( 27000000 );
1810    break;
1811  default:
1812    timingInfo->setNumUnitsInTick( 1001 );       timingInfo->setTimeScale    ( 60000 );
1813    break;
1814  }
1815#else
1816  hrd->setTimingInfoPresentFlag( true );
1817  switch( frameRate )
1818  {
1819  case 24:
1820    hrd->setNumUnitsInTick( 1125000 );    hrd->setTimeScale    ( 27000000 );
1821    break;
1822  case 25:
1823    hrd->setNumUnitsInTick( 1080000 );    hrd->setTimeScale    ( 27000000 );
1824    break;
1825  case 30:
1826    hrd->setNumUnitsInTick( 900900 );     hrd->setTimeScale    ( 27000000 );
1827    break;
1828  case 50:
1829    hrd->setNumUnitsInTick( 540000 );     hrd->setTimeScale    ( 27000000 );
1830    break;
1831  case 60:
1832    hrd->setNumUnitsInTick( 450450 );     hrd->setTimeScale    ( 27000000 );
1833    break;
1834  default:
1835    hrd->setNumUnitsInTick( 1001 );       hrd->setTimeScale    ( 60000 );
1836    break;
1837  }
1838#endif
1839
1840  Bool rateCnt = ( bitRate > 0 );
1841  hrd->setNalHrdParametersPresentFlag( rateCnt );
1842  hrd->setVclHrdParametersPresentFlag( rateCnt );
1843
1844  hrd->setSubPicCpbParamsPresentFlag( ( numDU > 1 ) );
1845
1846  if( hrd->getSubPicCpbParamsPresentFlag() )
1847  {
1848    hrd->setTickDivisorMinus2( 100 - 2 );                          //
1849    hrd->setDuCpbRemovalDelayLengthMinus1( 7 );                    // 8-bit precision ( plus 1 for last DU in AU )
1850    hrd->setSubPicCpbParamsInPicTimingSEIFlag( true );
1851#if L0044_DU_DPB_OUTPUT_DELAY_HRD
1852    hrd->setDpbOutputDelayDuLengthMinus1( 5 + 7 );                 // With sub-clock tick factor of 100, at least 7 bits to have the same value as AU dpb delay
1853#endif
1854  }
1855  else
1856  {
1857    hrd->setSubPicCpbParamsInPicTimingSEIFlag( false ); 
1858  }
1859
1860  hrd->setBitRateScale( 4 );                                       // in units of 2~( 6 + 4 ) = 1,024 bps
1861  hrd->setCpbSizeScale( 6 );                                       // in units of 2~( 4 + 4 ) = 1,024 bit
1862  hrd->setDuCpbSizeScale( 6 );                                       // in units of 2~( 4 + 4 ) = 1,024 bit
1863 
1864  hrd->setInitialCpbRemovalDelayLengthMinus1(15);                  // assuming 0.5 sec, log2( 90,000 * 0.5 ) = 16-bit
1865  if( randomAccess )
1866  {
1867    hrd->setCpbRemovalDelayLengthMinus1(5);                        // 32 = 2^5 (plus 1)
1868    hrd->setDpbOutputDelayLengthMinus1 (5);                        // 32 + 3 = 2^6
1869  }
1870  else
1871  {
1872    hrd->setCpbRemovalDelayLengthMinus1(9);                        // max. 2^10
1873    hrd->setDpbOutputDelayLengthMinus1 (9);                        // max. 2^10
1874  }
1875
1876/*
1877   Note: only the case of "vps_max_temporal_layers_minus1 = 0" is supported.
1878*/
1879  Int i, j;
1880  UInt birateValue, cpbSizeValue;
1881  UInt ducpbSizeValue;
1882#if L0363_DU_BIT_RATE
1883  UInt duBitRateValue = 0;
1884#endif
1885
1886  for( i = 0; i < MAX_TLAYER; i ++ )
1887  {
1888    hrd->setFixedPicRateFlag( i, 1 );
1889    hrd->setPicDurationInTcMinus1( i, 0 );
1890    hrd->setLowDelayHrdFlag( i, 0 );
1891    hrd->setCpbCntMinus1( i, 0 );
1892
1893    birateValue  = bitRate;
1894    cpbSizeValue = bitRate;                                     // 1 second
1895    ducpbSizeValue = bitRate/numDU;
1896#if L0363_DU_BIT_RATE
1897    duBitRateValue = bitRate;
1898#endif
1899    for( j = 0; j < ( hrd->getCpbCntMinus1( i ) + 1 ); j ++ )
1900    {
1901      hrd->setBitRateValueMinus1( i, j, 0, ( birateValue  - 1 ) );
1902      hrd->setCpbSizeValueMinus1( i, j, 0, ( cpbSizeValue - 1 ) );
1903      hrd->setDuCpbSizeValueMinus1( i, j, 0, ( ducpbSizeValue - 1 ) );
1904      hrd->setCbrFlag( i, j, 0, ( j == 0 ) );
1905
1906      hrd->setBitRateValueMinus1( i, j, 1, ( birateValue  - 1) );
1907      hrd->setCpbSizeValueMinus1( i, j, 1, ( cpbSizeValue - 1 ) );
1908      hrd->setDuCpbSizeValueMinus1( i, j, 1, ( ducpbSizeValue - 1 ) );
1909#if L0363_DU_BIT_RATE
1910      hrd->setDuBitRateValueMinus1( i, j, 1, ( duBitRateValue - 1 ) );
1911#endif
1912      hrd->setCbrFlag( i, j, 1, ( j == 0 ) );
1913    }
1914  }
1915}
1916const Int TComSPS::m_winUnitX[]={1,2,2,1};
1917const Int TComSPS::m_winUnitY[]={1,2,1,1};
1918
1919TComPPS::TComPPS()
1920: m_PPSId                       (0)
1921, m_SPSId                       (0)
1922, m_picInitQPMinus26            (0)
1923, m_useDQP                      (false)
1924, m_bConstrainedIntraPred       (false)
1925, m_bSliceChromaQpFlag          (false)
1926, m_pcSPS                       (NULL)
1927, m_uiMaxCuDQPDepth             (0)
1928, m_uiMinCuDQPSize              (0)
1929, m_chromaCbQpOffset            (0)
1930, m_chromaCrQpOffset            (0)
1931, m_numRefIdxL0DefaultActive    (1)
1932, m_numRefIdxL1DefaultActive    (1)
1933, m_TransquantBypassEnableFlag  (false)
1934, m_useTransformSkip             (false)
1935, m_dependentSliceSegmentsEnabledFlag    (false)
1936, m_tilesEnabledFlag               (false)
1937, m_entropyCodingSyncEnabledFlag   (false)
1938, m_loopFilterAcrossTilesEnabledFlag  (true)
1939, m_uniformSpacingFlag           (0)
1940, m_iNumColumnsMinus1            (0)
1941, m_puiColumnWidth               (NULL)
1942, m_iNumRowsMinus1               (0)
1943, m_puiRowHeight                 (NULL)
1944, m_iNumSubstreams             (1)
1945, m_signHideFlag(0)
1946, m_cabacInitPresentFlag        (false)
1947, m_encCABACTableIdx            (I_SLICE)
1948, m_sliceHeaderExtensionPresentFlag    (false)
1949, m_loopFilterAcrossSlicesEnabledFlag (false)
1950, m_listsModificationPresentFlag(  0)
1951, m_numExtraSliceHeaderBits(0)
1952{
1953  m_scalingList = new TComScalingList;
1954}
1955
1956TComPPS::~TComPPS()
1957{
1958  if( m_iNumColumnsMinus1 > 0 && m_uniformSpacingFlag == 0 )
1959  {
1960    if (m_puiColumnWidth) delete [] m_puiColumnWidth; 
1961    m_puiColumnWidth = NULL;
1962  }
1963  if( m_iNumRowsMinus1 > 0 && m_uniformSpacingFlag == 0 )
1964  {
1965    if (m_puiRowHeight) delete [] m_puiRowHeight;
1966    m_puiRowHeight = NULL;
1967  }
1968  delete m_scalingList;
1969}
1970
1971#if H_3D
1972Void
1973TComSPS::initCamParaSPS( UInt uiViewIndex, UInt uiCamParPrecision, Bool bCamParSlice, Int** aaiScale, Int** aaiOffset )
1974{
1975  AOT( uiViewIndex != 0 && !bCamParSlice && ( aaiScale == 0 || aaiOffset == 0 ) ); 
1976 
1977  m_uiCamParPrecision     = ( uiViewIndex ? uiCamParPrecision : 0 );
1978  m_bCamParInSliceHeader  = ( uiViewIndex ? bCamParSlice  : false );
1979  ::memset( m_aaiCodedScale,  0x00, sizeof( m_aaiCodedScale  ) );
1980  ::memset( m_aaiCodedOffset, 0x00, sizeof( m_aaiCodedOffset ) );
1981
1982  if( !m_bCamParInSliceHeader )
1983  {
1984    for( UInt uiBaseViewIndex = 0; uiBaseViewIndex < uiViewIndex; uiBaseViewIndex++ )
1985    {
1986      m_aaiCodedScale [ 0 ][ uiBaseViewIndex ] = aaiScale [ uiBaseViewIndex ][     uiViewIndex ];
1987      m_aaiCodedScale [ 1 ][ uiBaseViewIndex ] = aaiScale [     uiViewIndex ][ uiBaseViewIndex ];
1988      m_aaiCodedOffset[ 0 ][ uiBaseViewIndex ] = aaiOffset[ uiBaseViewIndex ][     uiViewIndex ];
1989      m_aaiCodedOffset[ 1 ][ uiBaseViewIndex ] = aaiOffset[     uiViewIndex ][ uiBaseViewIndex ];
1990    }
1991  }
1992}
1993#endif
1994
1995TComReferencePictureSet::TComReferencePictureSet()
1996: m_numberOfPictures (0)
1997, m_numberOfNegativePictures (0)
1998, m_numberOfPositivePictures (0)
1999, m_numberOfLongtermPictures (0)
2000, m_interRPSPrediction (0) 
2001, m_deltaRIdxMinus1 (0)   
2002, m_deltaRPS (0) 
2003, m_numRefIdc (0) 
2004{
2005  ::memset( m_deltaPOC, 0, sizeof(m_deltaPOC) );
2006  ::memset( m_POC, 0, sizeof(m_POC) );
2007  ::memset( m_used, 0, sizeof(m_used) );
2008  ::memset( m_refIdc, 0, sizeof(m_refIdc) );
2009}
2010
2011TComReferencePictureSet::~TComReferencePictureSet()
2012{
2013}
2014
2015Void TComReferencePictureSet::setUsed(Int bufferNum, Bool used)
2016{
2017  m_used[bufferNum] = used;
2018}
2019
2020Void TComReferencePictureSet::setDeltaPOC(Int bufferNum, Int deltaPOC)
2021{
2022  m_deltaPOC[bufferNum] = deltaPOC;
2023}
2024
2025Void TComReferencePictureSet::setNumberOfPictures(Int numberOfPictures)
2026{
2027  m_numberOfPictures = numberOfPictures;
2028}
2029
2030Int TComReferencePictureSet::getUsed(Int bufferNum)
2031{
2032  return m_used[bufferNum];
2033}
2034
2035Int TComReferencePictureSet::getDeltaPOC(Int bufferNum)
2036{
2037  return m_deltaPOC[bufferNum];
2038}
2039
2040Int TComReferencePictureSet::getNumberOfPictures()
2041{
2042  return m_numberOfPictures;
2043}
2044
2045Int TComReferencePictureSet::getPOC(Int bufferNum)
2046{
2047  return m_POC[bufferNum];
2048}
2049
2050Void TComReferencePictureSet::setPOC(Int bufferNum, Int POC)
2051{
2052  m_POC[bufferNum] = POC;
2053}
2054
2055Bool TComReferencePictureSet::getCheckLTMSBPresent(Int bufferNum)
2056{
2057  return m_bCheckLTMSB[bufferNum];
2058}
2059
2060Void TComReferencePictureSet::setCheckLTMSBPresent(Int bufferNum, Bool b)
2061{
2062  m_bCheckLTMSB[bufferNum] = b;
2063}
2064
2065/** set the reference idc value at uiBufferNum entry to the value of iRefIdc
2066 * \param uiBufferNum
2067 * \param iRefIdc
2068 * \returns Void
2069 */
2070Void TComReferencePictureSet::setRefIdc(Int bufferNum, Int refIdc)
2071{
2072  m_refIdc[bufferNum] = refIdc;
2073}
2074
2075/** get the reference idc value at uiBufferNum
2076 * \param uiBufferNum
2077 * \returns Int
2078 */
2079Int  TComReferencePictureSet::getRefIdc(Int bufferNum)
2080{
2081  return m_refIdc[bufferNum];
2082}
2083
2084/** Sorts the deltaPOC and Used by current values in the RPS based on the deltaPOC values.
2085 *  deltaPOC values are sorted with -ve values before the +ve values.  -ve values are in decreasing order.
2086 *  +ve values are in increasing order.
2087 * \returns Void
2088 */
2089Void TComReferencePictureSet::sortDeltaPOC()
2090{
2091  // sort in increasing order (smallest first)
2092  for(Int j=1; j < getNumberOfPictures(); j++)
2093  { 
2094    Int deltaPOC = getDeltaPOC(j);
2095    Bool used = getUsed(j);
2096    for (Int k=j-1; k >= 0; k--)
2097    {
2098      Int temp = getDeltaPOC(k);
2099      if (deltaPOC < temp)
2100      {
2101        setDeltaPOC(k+1, temp);
2102        setUsed(k+1, getUsed(k));
2103        setDeltaPOC(k, deltaPOC);
2104        setUsed(k, used);
2105      }
2106    }
2107  }
2108  // flip the negative values to largest first
2109  Int numNegPics = getNumberOfNegativePictures();
2110  for(Int j=0, k=numNegPics-1; j < numNegPics>>1; j++, k--)
2111  { 
2112    Int deltaPOC = getDeltaPOC(j);
2113    Bool used = getUsed(j);
2114    setDeltaPOC(j, getDeltaPOC(k));
2115    setUsed(j, getUsed(k));
2116    setDeltaPOC(k, deltaPOC);
2117    setUsed(k, used);
2118  }
2119}
2120
2121/** Prints the deltaPOC and RefIdc (if available) values in the RPS.
2122 *  A "*" is added to the deltaPOC value if it is Used bu current.
2123 * \returns Void
2124 */
2125Void TComReferencePictureSet::printDeltaPOC()
2126{
2127  printf("DeltaPOC = { ");
2128  for(Int j=0; j < getNumberOfPictures(); j++)
2129  {
2130    printf("%d%s ", getDeltaPOC(j), (getUsed(j)==1)?"*":"");
2131  } 
2132  if (getInterRPSPrediction()) 
2133  {
2134    printf("}, RefIdc = { ");
2135    for(Int j=0; j < getNumRefIdc(); j++)
2136    {
2137      printf("%d ", getRefIdc(j));
2138    } 
2139  }
2140  printf("}\n");
2141}
2142
2143TComRPSList::TComRPSList()
2144:m_referencePictureSets (NULL)
2145{
2146}
2147
2148TComRPSList::~TComRPSList()
2149{
2150}
2151
2152Void TComRPSList::create( Int numberOfReferencePictureSets)
2153{
2154  m_numberOfReferencePictureSets = numberOfReferencePictureSets;
2155  m_referencePictureSets = new TComReferencePictureSet[numberOfReferencePictureSets];
2156}
2157
2158Void TComRPSList::destroy()
2159{
2160  if (m_referencePictureSets)
2161  {
2162    delete [] m_referencePictureSets;
2163  }
2164  m_numberOfReferencePictureSets = 0;
2165  m_referencePictureSets = NULL;
2166}
2167
2168
2169
2170TComReferencePictureSet* TComRPSList::getReferencePictureSet(Int referencePictureSetNum)
2171{
2172  return &m_referencePictureSets[referencePictureSetNum];
2173}
2174
2175Int TComRPSList::getNumberOfReferencePictureSets()
2176{
2177  return m_numberOfReferencePictureSets;
2178}
2179
2180Void TComRPSList::setNumberOfReferencePictureSets(Int numberOfReferencePictureSets)
2181{
2182  m_numberOfReferencePictureSets = numberOfReferencePictureSets;
2183}
2184
2185TComRefPicListModification::TComRefPicListModification()
2186: m_bRefPicListModificationFlagL0 (false)
2187, m_bRefPicListModificationFlagL1 (false)
2188{
2189  ::memset( m_RefPicSetIdxL0, 0, sizeof(m_RefPicSetIdxL0) );
2190  ::memset( m_RefPicSetIdxL1, 0, sizeof(m_RefPicSetIdxL1) );
2191}
2192
2193TComRefPicListModification::~TComRefPicListModification()
2194{
2195}
2196
2197TComScalingList::TComScalingList()
2198{
2199  m_useTransformSkip = false;
2200  init();
2201}
2202TComScalingList::~TComScalingList()
2203{
2204  destroy();
2205}
2206
2207/** set default quantization matrix to array
2208*/
2209Void TComSlice::setDefaultScalingList()
2210{
2211  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
2212  {
2213    for(UInt listId=0;listId<g_scalingListNum[sizeId];listId++)
2214    {
2215      getScalingList()->processDefaultMarix(sizeId, listId);
2216    }
2217  }
2218}
2219/** check if use default quantization matrix
2220 * \returns true if use default quantization matrix in all size
2221*/
2222Bool TComSlice::checkDefaultScalingList()
2223{
2224  UInt defaultCounter=0;
2225
2226  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
2227  {
2228    for(UInt listId=0;listId<g_scalingListNum[sizeId];listId++)
2229    {
2230      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
2231     && ((sizeId < SCALING_LIST_16x16) || (getScalingList()->getScalingListDC(sizeId,listId) == 16))) // check DC value
2232      {
2233        defaultCounter++;
2234      }
2235    }
2236  }
2237  return (defaultCounter == (SCALING_LIST_NUM * SCALING_LIST_SIZE_NUM - 4)) ? false : true; // -4 for 32x32
2238}
2239
2240#if H_MV
2241Void TComSlice::createAndApplyIvReferencePictureSet( TComPicLists* ivPicLists, std::vector<TComPic*>& refPicSetInterLayer )
2242{
2243  refPicSetInterLayer.clear(); 
2244
2245  for( Int i = 0; i < getVPS()->getNumDirectRefLayers( getLayerIdInVps() ); i++ ) 
2246  {
2247    Int layerIdRef = getVPS()->getRefLayerId( getLayerIdInVps(), i ); 
2248    TComPic* picRef = ivPicLists->getPic( layerIdRef, getPOC() ) ; 
2249    assert ( picRef != 0 ); 
2250
2251    picRef->getPicYuvRec()->extendPicBorder(); 
2252    picRef->setIsLongTerm( true );       
2253    picRef->getSlice(0)->setReferenced( true );       
2254
2255    refPicSetInterLayer.push_back( picRef ); 
2256  }
2257}
2258
2259Void TComSlice::markIvRefPicsAsShortTerm( std::vector<TComPic*> refPicSetInterLayer )
2260{
2261  // Mark as shortterm
2262  for ( Int i = 0; i < refPicSetInterLayer.size(); i++ ) 
2263  {
2264    refPicSetInterLayer[i]->setIsLongTerm( false ); 
2265  }
2266}
2267
2268Void TComSlice::markIvRefPicsAsUnused( TComPicLists* ivPicLists, std::vector<Int> targetDecLayerIdSet, TComVPS* vps, Int curLayerId, Int curPoc )
2269{
2270  // Fill targetDecLayerIdSet with all layers if empty.
2271  if (targetDecLayerIdSet.size() == 0 )   
2272  {
2273    for ( Int layerIdInVps = 0; layerIdInVps < vps->getMaxLayers(); layerIdInVps++ )
2274    {
2275      targetDecLayerIdSet.push_back( vps->getLayerIdInNuh( layerIdInVps ) ); 
2276    }
2277  }     
2278
2279  Int numTargetDecLayers = (Int) targetDecLayerIdSet.size(); 
2280  Int latestDecIdx; 
2281  for ( latestDecIdx = 0; latestDecIdx < numTargetDecLayers; latestDecIdx++)
2282  {
2283    if ( targetDecLayerIdSet[ latestDecIdx ] == curLayerId )
2284      break; 
2285  }       
2286
2287  for( Int i = 0; i <= latestDecIdx; i++ ) 
2288  {
2289    if ( vps->nuhLayerIdIncluded( targetDecLayerIdSet[ i ] ) )
2290    {
2291      TComPic* pcPic = ivPicLists->getPic( targetDecLayerIdSet[ i ], curPoc ); 
2292      if( pcPic->getSlice(0)->isReferenced() && pcPic->getSlice(0)->getTemporalLayerNonReferenceFlag() ) 
2293      { 
2294        Bool remainingInterLayerReferencesFlag = false; 
2295        for( Int j = latestDecIdx + 1; j < numTargetDecLayers; j++ )
2296        { 
2297          TComVPS* vpsSlice = pcPic->getSlice(0)->getVPS(); 
2298          if ( vps->nuhLayerIdIncluded( targetDecLayerIdSet[ j ] ) )
2299          {
2300            Int targetDecLayerIdinVPS = vpsSlice->getLayerIdInVps( targetDecLayerIdSet[ j ] ); 
2301            for( Int k = 0; k < vpsSlice->getNumDirectRefLayers( targetDecLayerIdinVPS ); k++ )
2302              if ( targetDecLayerIdSet[ i ] == vpsSlice->getRefLayerId( targetDecLayerIdinVPS,  k  ) )
2303                remainingInterLayerReferencesFlag = true;
2304          }
2305        }
2306        if( !remainingInterLayerReferencesFlag )
2307          pcPic->getSlice(0)->setReferenced( false );                   
2308      }
2309    }
2310  }
2311}
2312
2313Void TComSlice::xPrintRefPicList()
2314{ 
2315  for ( Int li = 0; li < 2; li++)
2316  {   
2317    std::cout << std::endl << "RefPicListL" <<  li << ":" << std::endl; 
2318    for (Int rIdx = 0; rIdx <= (m_aiNumRefIdx[li]-1); rIdx ++)
2319    {     
2320      if (rIdx == 0 && li == 0) m_apcRefPicList[li][rIdx]->print( true );
2321       
2322      m_apcRefPicList[li][rIdx]->print( false );
2323    }
2324  }
2325}
2326#if H_3D_GEN
2327Void TComSlice::setIvPicLists( TComPicLists* m_ivPicLists )
2328{
2329  for (Int i = 0; i < MAX_NUM_LAYERS; i++ )
2330  {     
2331    for ( Int depthId = 0; depthId < 2; depthId++ )
2332    {
2333      m_ivPicsCurrPoc[ depthId ][ i ] = ( i <= m_viewIndex ) ? m_ivPicLists->getPic( i, ( depthId == 1) , getPOC() ) : NULL;
2334    }       
2335  } 
2336}
2337Void TComSlice::setDepthToDisparityLUTs()
2338{ 
2339  Bool setupLUT = false; 
2340  Int layerIdInVPS = getVPS()->getLayerIdInNuh( m_layerId ); 
2341
2342#if H_3D_VSP
2343  setupLUT = setupLUT || getVPS()->getViewSynthesisPredFlag( layerIdInVPS); 
2344#endif
2345
2346#if H_3D_NBDV_REF
2347  setupLUT = setupLUT || getVPS()->getDepthRefinementFlag( layerIdInVPS );
2348#endif
2349
2350  if( !setupLUT )
2351    return; 
2352
2353  /// GT: Allocation should be moved to a better place later;
2354  if ( m_depthToDisparityB == NULL )
2355  {
2356    m_depthToDisparityB = new Int*[ getViewIndex() ];
2357    for ( Int i = 0; i < getViewIndex(); i++ )
2358    {
2359      m_depthToDisparityB[ i ] = new Int[ Int(1 << g_bitDepthY) ]; 
2360    }
2361  }
2362
2363  if ( m_depthToDisparityF == NULL )
2364  {
2365    m_depthToDisparityF= new Int*[ getViewIndex() ];
2366    for ( Int i = 0; i < getViewIndex(); i++ )
2367    {
2368      m_depthToDisparityF[ i ] = new Int[ Int(1 << g_bitDepthY) ]; 
2369    }
2370  }
2371
2372  assert( m_depthToDisparityB != NULL ); 
2373  assert( m_depthToDisparityF != NULL ); 
2374
2375  TComSPS* sps = getSPS(); 
2376
2377  Int log2Div = g_bitDepthY - 1 + sps->getCamParPrecision();
2378
2379  Bool camParaSH = m_pcSPS->hasCamParInSliceHeader();
2380
2381  Int* codScale     = camParaSH ? m_aaiCodedScale [ 0 ] : sps->getCodedScale    (); 
2382  Int* codOffset    = camParaSH ? m_aaiCodedOffset[ 0 ] : sps->getCodedOffset   (); 
2383  Int* invCodScale  = camParaSH ? m_aaiCodedScale [ 1 ] : sps->getInvCodedScale (); 
2384  Int* invCodOffset = camParaSH ? m_aaiCodedOffset[ 1 ] : sps->getInvCodedOffset(); 
2385
2386  for (Int i = 0; i <= ( getViewIndex() - 1); i++)
2387  {
2388    for ( Int d = 0; d <= ( ( 1 << g_bitDepthY ) - 1 ); d++ )
2389    {
2390      Int offset =    ( codOffset  [ i ] << g_bitDepthY ) + ( ( 1 << log2Div ) >> 1 );         
2391      m_depthToDisparityB[ i ][ d ] = ( codScale [ i ] * d + offset ) >> log2Div; 
2392
2393      Int invOffset = ( invCodOffset[ i ] << g_bitDepthY ) + ( ( 1 << log2Div ) >> 1 );         
2394      m_depthToDisparityF[ i ][ d ] = ( invCodScale[ i ] * d + invOffset ) >> log2Div; 
2395    }
2396  }
2397}
2398#endif
2399#endif
2400/** get scaling matrix from RefMatrixID
2401 * \param sizeId size index
2402 * \param Index of input matrix
2403 * \param Index of reference matrix
2404 */
2405Void TComScalingList::processRefMatrix( UInt sizeId, UInt listId , UInt refListId )
2406{
2407  ::memcpy(getScalingListAddress(sizeId, listId),((listId == refListId)? getScalingListDefaultAddress(sizeId, refListId): getScalingListAddress(sizeId, refListId)),sizeof(Int)*min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId]));
2408}
2409
2410/** parse syntax infomation
2411 *  \param pchFile syntax infomation
2412 *  \returns false if successful
2413 */
2414Bool TComScalingList::xParseScalingList(Char* pchFile)
2415{
2416  FILE *fp;
2417  Char line[1024];
2418  UInt sizeIdc,listIdc;
2419  UInt i,size = 0;
2420  Int *src=0,data;
2421  Char *ret;
2422  UInt  retval;
2423
2424  if((fp = fopen(pchFile,"r")) == (FILE*)NULL)
2425  {
2426    printf("can't open file %s :: set Default Matrix\n",pchFile);
2427    return true;
2428  }
2429
2430  for(sizeIdc = 0; sizeIdc < SCALING_LIST_SIZE_NUM; sizeIdc++)
2431  {
2432    size = min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeIdc]);
2433    for(listIdc = 0; listIdc < g_scalingListNum[sizeIdc]; listIdc++)
2434    {
2435      src = getScalingListAddress(sizeIdc, listIdc);
2436
2437      fseek(fp,0,0);
2438      do 
2439      {
2440        ret = fgets(line, 1024, fp);
2441        if ((ret==NULL)||(strstr(line, MatrixType[sizeIdc][listIdc])==NULL && feof(fp)))
2442        {
2443          printf("Error: can't read Matrix :: set Default Matrix\n");
2444          return true;
2445        }
2446      }
2447      while (strstr(line, MatrixType[sizeIdc][listIdc]) == NULL);
2448      for (i=0; i<size; i++)
2449      {
2450        retval = fscanf(fp, "%d,", &data);
2451        if (retval!=1)
2452        {
2453          printf("Error: can't read Matrix :: set Default Matrix\n");
2454          return true;
2455        }
2456        src[i] = data;
2457      }
2458      //set DC value for default matrix check
2459      setScalingListDC(sizeIdc,listIdc,src[0]);
2460
2461      if(sizeIdc > SCALING_LIST_8x8)
2462      {
2463        fseek(fp,0,0);
2464        do 
2465        {
2466          ret = fgets(line, 1024, fp);
2467          if ((ret==NULL)||(strstr(line, MatrixType_DC[sizeIdc][listIdc])==NULL && feof(fp)))
2468          {
2469            printf("Error: can't read DC :: set Default Matrix\n");
2470            return true;
2471          }
2472        }
2473        while (strstr(line, MatrixType_DC[sizeIdc][listIdc]) == NULL);
2474        retval = fscanf(fp, "%d,", &data);
2475        if (retval!=1)
2476        {
2477          printf("Error: can't read Matrix :: set Default Matrix\n");
2478          return true;
2479        }
2480        //overwrite DC value when size of matrix is larger than 16x16
2481        setScalingListDC(sizeIdc,listIdc,data);
2482      }
2483    }
2484  }
2485  fclose(fp);
2486  return false;
2487}
2488
2489/** initialization process of quantization matrix array
2490 */
2491Void TComScalingList::init()
2492{
2493  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
2494  {
2495    for(UInt listId = 0; listId < g_scalingListNum[sizeId]; listId++)
2496    {
2497      m_scalingListCoef[sizeId][listId] = new Int [min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId])];
2498    }
2499  }
2500  m_scalingListCoef[SCALING_LIST_32x32][3] = m_scalingListCoef[SCALING_LIST_32x32][1]; // copy address for 32x32
2501}
2502
2503/** destroy quantization matrix array
2504 */
2505Void TComScalingList::destroy()
2506{
2507  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
2508  {
2509    for(UInt listId = 0; listId < g_scalingListNum[sizeId]; listId++)
2510    {
2511      if(m_scalingListCoef[sizeId][listId]) delete [] m_scalingListCoef[sizeId][listId];
2512    }
2513  }
2514}
2515
2516/** get default address of quantization matrix
2517 * \param sizeId size index
2518 * \param listId list index
2519 * \returns pointer of quantization matrix
2520 */
2521Int* TComScalingList::getScalingListDefaultAddress(UInt sizeId, UInt listId)
2522{
2523  Int *src = 0;
2524  switch(sizeId)
2525  {
2526    case SCALING_LIST_4x4:
2527      src = g_quantTSDefault4x4;
2528      break;
2529    case SCALING_LIST_8x8:
2530      src = (listId<3) ? g_quantIntraDefault8x8 : g_quantInterDefault8x8;
2531      break;
2532    case SCALING_LIST_16x16:
2533      src = (listId<3) ? g_quantIntraDefault8x8 : g_quantInterDefault8x8;
2534      break;
2535    case SCALING_LIST_32x32:
2536      src = (listId<1) ? g_quantIntraDefault8x8 : g_quantInterDefault8x8;
2537      break;
2538    default:
2539      assert(0);
2540      src = NULL;
2541      break;
2542  }
2543  return src;
2544}
2545
2546/** process of default matrix
2547 * \param sizeId size index
2548 * \param Index of input matrix
2549 */
2550Void TComScalingList::processDefaultMarix(UInt sizeId, UInt listId)
2551{
2552  ::memcpy(getScalingListAddress(sizeId, listId),getScalingListDefaultAddress(sizeId,listId),sizeof(Int)*min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId]));
2553  setScalingListDC(sizeId,listId,SCALING_LIST_DC);
2554}
2555
2556/** check DC value of matrix for default matrix signaling
2557 */
2558Void TComScalingList::checkDcOfMatrix()
2559{
2560  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
2561  {
2562    for(UInt listId = 0; listId < g_scalingListNum[sizeId]; listId++)
2563    {
2564      //check default matrix?
2565      if(getScalingListDC(sizeId,listId) == 0)
2566      {
2567        processDefaultMarix(sizeId, listId);
2568      }
2569    }
2570  }
2571}
2572
2573ParameterSetManager::ParameterSetManager()
2574: m_vpsMap(MAX_NUM_VPS)
2575, m_spsMap(MAX_NUM_SPS)
2576, m_ppsMap(MAX_NUM_PPS)
2577, m_activeVPSId(-1)
2578, m_activeSPSId(-1)
2579, m_activePPSId(-1)
2580{
2581}
2582
2583
2584ParameterSetManager::~ParameterSetManager()
2585{
2586}
2587
2588//! activate a SPS from a active parameter sets SEI message
2589//! \returns true, if activation is successful
2590Bool ParameterSetManager::activateSPSWithSEI(Int spsId)
2591{
2592  TComSPS *sps = m_spsMap.getPS(spsId);
2593  if (sps)
2594  {
2595    Int vpsId = sps->getVPSId();
2596    if (m_vpsMap.getPS(vpsId))
2597    {
2598      m_activeVPSId = vpsId;
2599      m_activeSPSId = spsId;
2600      return true;
2601    }
2602    else
2603    {
2604      printf("Warning: tried to activate SPS using an Active parameter sets SEI message. Referenced VPS does not exist.");
2605    }
2606  }
2607  else
2608  {
2609    printf("Warning: tried to activate non-existing SPS using an Active parameter sets SEI message.");
2610  }
2611  return false;
2612}
2613
2614//! activate a PPS and depending on isIDR parameter also SPS and VPS
2615//! \returns true, if activation is successful
2616Bool ParameterSetManager::activatePPS(Int ppsId, Bool isIRAP)
2617{
2618  TComPPS *pps = m_ppsMap.getPS(ppsId);
2619  if (pps)
2620  {
2621    Int spsId = pps->getSPSId();
2622#if H_MV
2623    // active parameter sets per layer should be used here
2624#else
2625    if (!isIRAP && (spsId != m_activeSPSId))
2626    {
2627      printf("Warning: tried to activate PPS referring to a inactive SPS at non-IRAP.");
2628      return false;
2629    }
2630#endif
2631    TComSPS *sps = m_spsMap.getPS(spsId);
2632    if (sps)
2633    {
2634      Int vpsId = sps->getVPSId();
2635      if (!isIRAP && (vpsId != m_activeVPSId))
2636      {
2637        printf("Warning: tried to activate PPS referring to a inactive VPS at non-IRAP.");
2638        return false;
2639      }
2640      if (m_vpsMap.getPS(vpsId))
2641      {
2642        m_activePPSId = ppsId;
2643        m_activeVPSId = vpsId;
2644        m_activeSPSId = spsId;
2645        return true;
2646      }
2647      else
2648      {
2649        printf("Warning: tried to activate PPS that refers to a non-existing VPS.");
2650      }
2651    }
2652    else
2653    {
2654      printf("Warning: tried to activate a PPS that refers to a non-existing SPS.");
2655    }
2656  }
2657  else
2658  {
2659    printf("Warning: tried to activate non-existing PPS.");
2660  }
2661  return false;
2662}
2663
2664ProfileTierLevel::ProfileTierLevel()
2665  : m_profileSpace    (0)
2666  , m_tierFlag        (false)
2667  , m_profileIdc      (0)
2668  , m_levelIdc        (0)
2669#if L0046_CONSTRAINT_FLAGS
2670, m_progressiveSourceFlag  (false)
2671, m_interlacedSourceFlag   (false)
2672, m_nonPackedConstraintFlag(false)
2673, m_frameOnlyConstraintFlag(false)
2674#endif
2675{
2676  ::memset(m_profileCompatibilityFlag, 0, sizeof(m_profileCompatibilityFlag));
2677}
2678
2679TComPTL::TComPTL()
2680{
2681  ::memset(m_subLayerProfilePresentFlag, 0, sizeof(m_subLayerProfilePresentFlag));
2682  ::memset(m_subLayerLevelPresentFlag,   0, sizeof(m_subLayerLevelPresentFlag  ));
2683}
2684
2685#if H_MV
2686Void TComPTL::copyLevelFrom( TComPTL* source )
2687{
2688  getGeneralPTL()->setLevelIdc( source->getGeneralPTL()->getLevelIdc() );
2689  for( Int subLayer = 0; subLayer < 6; subLayer++ )
2690  {
2691    setSubLayerLevelPresentFlag( subLayer, source->getSubLayerLevelPresentFlag( subLayer ) );
2692    getSubLayerPTL( subLayer )->setLevelIdc( source->getSubLayerPTL( subLayer )->getLevelIdc() );
2693  }
2694}
2695#endif
2696#if SIGNAL_BITRATE_PICRATE_IN_VPS
2697TComBitRatePicRateInfo::TComBitRatePicRateInfo()
2698{
2699  ::memset(m_bitRateInfoPresentFlag, 0, sizeof(m_bitRateInfoPresentFlag));
2700  ::memset(m_picRateInfoPresentFlag, 0, sizeof(m_picRateInfoPresentFlag));
2701  ::memset(m_avgBitRate,             0, sizeof(m_avgBitRate));
2702  ::memset(m_maxBitRate,             0, sizeof(m_maxBitRate));
2703  ::memset(m_constantPicRateIdc,     0, sizeof(m_constantPicRateIdc));
2704  ::memset(m_avgPicRate,             0, sizeof(m_avgPicRate));
2705}
2706#endif
2707
2708//! \}
Note: See TracBrowser for help on using the repository browser.