source: SHVCSoftware/branches/SHM-1.0-dev/source/Lib/TLibCommon/TComSlice.cpp @ 26

Last change on this file since 26 was 25, checked in by qualcomm, 12 years ago

L0336: motion field mapping of inter-layer reference picture from Qualcomm, cjianle@…

File size: 69.3 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-2012, 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 )
52, m_eSliceType                    ( I_SLICE )
53, m_iSliceQp                      ( 0 )
54#if SLICEHEADER_SYNTAX_FIX
55, m_dependentSliceFlag            ( false )
56#endif
57#if ADAPTIVE_QP_SELECTION
58, m_iSliceQpBase                  ( 0 )
59#endif
60, m_deblockingFilterDisable        ( false )
61, m_deblockingFilterOverrideFlag   ( false )
62, m_deblockingFilterBetaOffsetDiv2 ( 0 )
63, m_deblockingFilterTcOffsetDiv2   ( 0 )
64, m_bRefPicListModificationFlagLC ( false )
65, m_bRefPicListCombinationFlag    ( false )
66, m_bCheckLDC                     ( false )
67, m_iSliceQpDelta                 ( 0 )
68#if CHROMA_QP_EXTENSION
69, m_iSliceQpDeltaCb               ( 0 )
70, m_iSliceQpDeltaCr               ( 0 )
71#endif
72, m_iDepth                        ( 0 )
73, m_bRefenced                     ( false )
74, m_pcSPS                         ( NULL )
75, m_pcPPS                         ( NULL )
76, m_pcPic                         ( NULL )
77, m_colFromL0Flag                 ( 1 )
78, m_colRefIdx                     ( 0 )
79#if ALF_CHROMA_LAMBDA || SAO_CHROMA_LAMBDA
80, m_dLambdaLuma( 0.0 )
81, m_dLambdaChroma( 0.0 )
82#else
83, m_dLambda                       ( 0.0 )
84#endif
85, m_bNoBackPredFlag               ( false )
86, m_uiTLayer                      ( 0 )
87#if SVC_EXTENSION
88, m_layerId                     ( 0 )
89, m_pcBaseColPic                  ( NULL )
90#endif
91, m_bTLayerSwitchingFlag          ( false )
92, m_uiSliceMode                   ( 0 )
93, m_uiSliceArgument               ( 0 )
94, m_uiSliceCurStartCUAddr         ( 0 )
95, m_uiSliceCurEndCUAddr           ( 0 )
96, m_uiSliceIdx                    ( 0 )
97, m_uiDependentSliceMode            ( 0 )
98, m_uiDependentSliceArgument        ( 0 )
99, m_uiDependentSliceCurStartCUAddr  ( 0 )
100, m_uiDependentSliceCurEndCUAddr    ( 0 )
101, m_bNextSlice                    ( false )
102, m_bNextDependentSlice             ( false )
103, m_uiSliceBits                   ( 0 )
104, m_uiDependentSliceCounter         ( 0 )
105, m_bFinalized                    ( false )
106, m_uiTileOffstForMultES          ( 0 )
107, m_puiSubstreamSizes             ( NULL )
108, m_cabacInitFlag                 ( false )
109, m_bLMvdL1Zero                   ( false )
110, m_numEntryPointOffsets          ( 0 )
111#if TEMPORAL_LAYER_NON_REFERENCE
112, m_temporalLayerNonReferenceFlag ( false )
113#endif
114#if !REMOVE_NAL_REF_FLAG
115, m_nalRefFlag                    ( 0 )
116#endif
117, m_enableTMVPFlag                ( true )
118{
119  m_aiNumRefIdx[0] = m_aiNumRefIdx[1] = m_aiNumRefIdx[2] = 0;
120 
121  initEqualRef();
122 
123  for(Int iNumCount = 0; iNumCount < MAX_NUM_REF_LC; iNumCount++)
124  {
125    m_iRefIdxOfLC[REF_PIC_LIST_0][iNumCount]=-1;
126    m_iRefIdxOfLC[REF_PIC_LIST_1][iNumCount]=-1;
127    m_eListIdFromIdxOfLC[iNumCount]=0;
128    m_iRefIdxFromIdxOfLC[iNumCount]=0;
129    m_iRefIdxOfL0FromRefIdxOfL1[iNumCount] = -1;
130    m_iRefIdxOfL1FromRefIdxOfL0[iNumCount] = -1;
131  }   
132  for(Int iNumCount = 0; iNumCount < MAX_NUM_REF; iNumCount++)
133  {
134    m_apcRefPicList [0][iNumCount] = NULL;
135    m_apcRefPicList [1][iNumCount] = NULL;
136    m_aiRefPOCList  [0][iNumCount] = 0;
137    m_aiRefPOCList  [1][iNumCount] = 0;
138  }
139  m_bCombineWithReferenceFlag = 0;
140  resetWpScaling(m_weightPredTable);
141  initWpAcDcParam();
142  m_saoEnabledFlag = false;
143#if !REMOVE_ALF
144  m_alfEnabledFlag[0] = m_alfEnabledFlag[1] = m_alfEnabledFlag[2] = false;
145#endif
146}
147
148TComSlice::~TComSlice()
149{
150  delete[] m_puiSubstreamSizes;
151  m_puiSubstreamSizes = NULL;
152}
153
154
155#if SET_SLICE_LAYER_ID
156Void TComSlice::initSlice( UInt layerId )
157#else
158Void TComSlice::initSlice()
159#endif
160{
161#if SET_SLICE_LAYER_ID
162  m_layerId = layerId;
163#endif
164  m_aiNumRefIdx[0]      = 0;
165  m_aiNumRefIdx[1]      = 0;
166 
167  m_colFromL0Flag = 1;
168 
169  m_colRefIdx = 0;
170  initEqualRef();
171  m_bNoBackPredFlag = false;
172  m_bRefPicListCombinationFlag = false;
173  m_bRefPicListModificationFlagLC = false;
174  m_bCheckLDC = false;
175#if CHROMA_QP_EXTENSION
176  m_iSliceQpDeltaCb = 0;
177  m_iSliceQpDeltaCr = 0;
178#endif
179
180  m_aiNumRefIdx[REF_PIC_LIST_C]      = 0;
181
182  m_maxNumMergeCand = MRG_MAX_NUM_CANDS;
183
184  m_bFinalized=false;
185
186  m_tileByteLocation.clear();
187  m_cabacInitFlag        = false;
188  m_numEntryPointOffsets = 0;
189  m_enableTMVPFlag = true;
190}
191
192Bool TComSlice::getRapPicFlag()
193{
194  return getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR
195#if SUPPORT_FOR_RAP_N_LP
196      || getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_N_LP
197      || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP
198#endif
199      || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLANT
200      || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA
201#if !NAL_UNIT_TYPES_J1003_D7
202      || getNalUnitType() == NAL_UNIT_CODED_SLICE_CRANT
203#endif
204      || getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA;
205}
206
207/**
208 - allocate table to contain substream sizes to be written to the slice header.
209 .
210 \param uiNumSubstreams Number of substreams -- the allocation will be this value - 1.
211 */
212Void  TComSlice::allocSubstreamSizes(UInt uiNumSubstreams)
213{
214  delete[] m_puiSubstreamSizes;
215  m_puiSubstreamSizes = new UInt[uiNumSubstreams > 0 ? uiNumSubstreams-1 : 0];
216}
217
218Void  TComSlice::sortPicList        (TComList<TComPic*>& rcListPic)
219{
220  TComPic*    pcPicExtract;
221  TComPic*    pcPicInsert;
222 
223  TComList<TComPic*>::iterator    iterPicExtract;
224  TComList<TComPic*>::iterator    iterPicExtract_1;
225  TComList<TComPic*>::iterator    iterPicInsert;
226 
227  for (Int i = 1; i < (Int)(rcListPic.size()); i++)
228  {
229    iterPicExtract = rcListPic.begin();
230    for (Int j = 0; j < i; j++) iterPicExtract++;
231    pcPicExtract = *(iterPicExtract);
232    pcPicExtract->setCurrSliceIdx(0);
233   
234    iterPicInsert = rcListPic.begin();
235    while (iterPicInsert != iterPicExtract)
236    {
237      pcPicInsert = *(iterPicInsert);
238      pcPicInsert->setCurrSliceIdx(0);
239      if (pcPicInsert->getPOC() >= pcPicExtract->getPOC())
240      {
241        break;
242      }
243     
244      iterPicInsert++;
245    }
246   
247    iterPicExtract_1 = iterPicExtract;    iterPicExtract_1++;
248   
249    //  swap iterPicExtract and iterPicInsert, iterPicExtract = curr. / iterPicInsert = insertion position
250    rcListPic.insert (iterPicInsert, iterPicExtract, iterPicExtract_1);
251    rcListPic.erase  (iterPicExtract);
252  }
253}
254
255TComPic* TComSlice::xGetRefPic (TComList<TComPic*>& rcListPic,
256                                UInt                uiPOC)
257{
258  TComList<TComPic*>::iterator  iterPic = rcListPic.begin(); 
259  TComPic*                      pcPic = *(iterPic);
260  while ( iterPic != rcListPic.end() )
261  {
262    if(pcPic->getPOC() == uiPOC)
263    {
264      break;
265    }
266    iterPic++;
267    pcPic = *(iterPic);
268  }
269  return  pcPic;
270}
271
272
273TComPic* TComSlice::xGetLongTermRefPic (TComList<TComPic*>& rcListPic,
274                                UInt                uiPOC)
275{
276  TComList<TComPic*>::iterator  iterPic = rcListPic.begin(); 
277  TComPic*                      pcPic = *(iterPic);
278  TComPic*                      pcStPic = pcPic;
279  while ( iterPic != rcListPic.end() )
280  {
281    pcPic = *(iterPic);
282    if(pcPic && (pcPic->getPOC()%(1<<getSPS()->getBitsForPOC())) == (uiPOC%(1<<getSPS()->getBitsForPOC())))
283    {
284      if(pcPic->getIsLongTerm())
285      {
286        return pcPic;
287      }
288      else
289      {
290        pcStPic = pcPic;
291      }
292      break;
293    }
294
295    iterPic++;
296  }
297  return  pcStPic;
298}
299
300Void TComSlice::setRefPOCList       ()
301{
302  for (Int iDir = 0; iDir < 2; iDir++)
303  {
304    for (Int iNumRefIdx = 0; iNumRefIdx < m_aiNumRefIdx[iDir]; iNumRefIdx++)
305    {
306      m_aiRefPOCList[iDir][iNumRefIdx] = m_apcRefPicList[iDir][iNumRefIdx]->getPOC();
307    }
308  }
309
310}
311
312Void TComSlice::generateCombinedList()
313{
314  if(m_aiNumRefIdx[REF_PIC_LIST_C] > 0)
315  {
316    m_aiNumRefIdx[REF_PIC_LIST_C]=0;
317    for(Int iNumCount = 0; iNumCount < MAX_NUM_REF_LC; iNumCount++)
318    {
319      m_iRefIdxOfLC[REF_PIC_LIST_0][iNumCount]=-1;
320      m_iRefIdxOfLC[REF_PIC_LIST_1][iNumCount]=-1;
321      m_eListIdFromIdxOfLC[iNumCount]=0;
322      m_iRefIdxFromIdxOfLC[iNumCount]=0;
323      m_iRefIdxOfL0FromRefIdxOfL1[iNumCount] = -1;
324      m_iRefIdxOfL1FromRefIdxOfL0[iNumCount] = -1;
325    }
326
327    for (Int iNumRefIdx = 0; iNumRefIdx < MAX_NUM_REF; iNumRefIdx++)
328    {
329      if(iNumRefIdx < m_aiNumRefIdx[REF_PIC_LIST_0])
330      {
331        Bool bTempRefIdxInL2 = true;
332        for ( Int iRefIdxLC = 0; iRefIdxLC < m_aiNumRefIdx[REF_PIC_LIST_C]; iRefIdxLC++ )
333        {
334          if ( m_apcRefPicList[REF_PIC_LIST_0][iNumRefIdx]->getPOC() == m_apcRefPicList[m_eListIdFromIdxOfLC[iRefIdxLC]][m_iRefIdxFromIdxOfLC[iRefIdxLC]]->getPOC() )
335          {
336            m_iRefIdxOfL1FromRefIdxOfL0[iNumRefIdx] = m_iRefIdxFromIdxOfLC[iRefIdxLC];
337            m_iRefIdxOfL0FromRefIdxOfL1[m_iRefIdxFromIdxOfLC[iRefIdxLC]] = iNumRefIdx;
338            bTempRefIdxInL2 = false;
339            break;
340          }
341        }
342
343        if(bTempRefIdxInL2 == true)
344        { 
345          m_eListIdFromIdxOfLC[m_aiNumRefIdx[REF_PIC_LIST_C]] = REF_PIC_LIST_0;
346          m_iRefIdxFromIdxOfLC[m_aiNumRefIdx[REF_PIC_LIST_C]] = iNumRefIdx;
347          m_iRefIdxOfLC[REF_PIC_LIST_0][iNumRefIdx] = m_aiNumRefIdx[REF_PIC_LIST_C]++;
348        }
349      }
350
351      if(iNumRefIdx < m_aiNumRefIdx[REF_PIC_LIST_1])
352      {
353        Bool bTempRefIdxInL2 = true;
354        for ( Int iRefIdxLC = 0; iRefIdxLC < m_aiNumRefIdx[REF_PIC_LIST_C]; iRefIdxLC++ )
355        {
356          if ( m_apcRefPicList[REF_PIC_LIST_1][iNumRefIdx]->getPOC() == m_apcRefPicList[m_eListIdFromIdxOfLC[iRefIdxLC]][m_iRefIdxFromIdxOfLC[iRefIdxLC]]->getPOC() )
357          {
358            m_iRefIdxOfL0FromRefIdxOfL1[iNumRefIdx] = m_iRefIdxFromIdxOfLC[iRefIdxLC];
359            m_iRefIdxOfL1FromRefIdxOfL0[m_iRefIdxFromIdxOfLC[iRefIdxLC]] = iNumRefIdx;
360            bTempRefIdxInL2 = false;
361            break;
362          }
363        }
364        if(bTempRefIdxInL2 == true)
365        {
366          m_eListIdFromIdxOfLC[m_aiNumRefIdx[REF_PIC_LIST_C]] = REF_PIC_LIST_1;
367          m_iRefIdxFromIdxOfLC[m_aiNumRefIdx[REF_PIC_LIST_C]] = iNumRefIdx;
368          m_iRefIdxOfLC[REF_PIC_LIST_1][iNumRefIdx] = m_aiNumRefIdx[REF_PIC_LIST_C]++;
369        }
370      }
371    }
372  }
373}
374
375Void TComSlice::setRefPicList( TComList<TComPic*>& rcListPic )
376{
377  if (m_eSliceType == I_SLICE
378#if REF_IDX_FRAMEWORK
379      || ( getSPS()->getLayerId() && 
380         (getNalUnitType() >= NAL_UNIT_CODED_SLICE_BLA) &&
381         (getNalUnitType() <= NAL_UNIT_CODED_SLICE_CRA) )
382#endif
383    )
384  {
385    ::memset( m_apcRefPicList, 0, sizeof (m_apcRefPicList));
386    ::memset( m_aiNumRefIdx,   0, sizeof ( m_aiNumRefIdx ));
387   
388    return;
389  }
390 
391  m_aiNumRefIdx[0] = getNumRefIdx(REF_PIC_LIST_0);
392  m_aiNumRefIdx[1] = getNumRefIdx(REF_PIC_LIST_1);
393
394  TComPic*  pcRefPic= NULL;
395  TComPic*  RefPicSetStCurr0[16];
396  TComPic*  RefPicSetStCurr1[16];
397  TComPic*  RefPicSetLtCurr[16];
398  UInt NumPocStCurr0 = 0;
399  UInt NumPocStCurr1 = 0;
400  UInt NumPocLtCurr = 0;
401  Int i;
402
403  for(i=0; i < m_pcRPS->getNumberOfNegativePictures(); i++)
404  {
405    if(m_pcRPS->getUsed(i))
406    {
407      pcRefPic = xGetRefPic(rcListPic, getPOC()+m_pcRPS->getDeltaPOC(i));
408      pcRefPic->setIsLongTerm(0);
409      pcRefPic->setIsUsedAsLongTerm(0);
410      pcRefPic->getPicYuvRec()->extendPicBorder();
411      RefPicSetStCurr0[NumPocStCurr0] = pcRefPic;
412      NumPocStCurr0++;
413      pcRefPic->setCheckLTMSBPresent(false); 
414    }
415  }
416  for(; i < m_pcRPS->getNumberOfNegativePictures()+m_pcRPS->getNumberOfPositivePictures(); i++)
417  {
418    if(m_pcRPS->getUsed(i))
419    {
420      pcRefPic = xGetRefPic(rcListPic, getPOC()+m_pcRPS->getDeltaPOC(i));
421      pcRefPic->setIsLongTerm(0);
422      pcRefPic->setIsUsedAsLongTerm(0);
423      pcRefPic->getPicYuvRec()->extendPicBorder();
424      RefPicSetStCurr1[NumPocStCurr1] = pcRefPic;
425      NumPocStCurr1++;
426      pcRefPic->setCheckLTMSBPresent(false); 
427    }
428  }
429  for(i = m_pcRPS->getNumberOfNegativePictures()+m_pcRPS->getNumberOfPositivePictures()+m_pcRPS->getNumberOfLongtermPictures()-1; i > m_pcRPS->getNumberOfNegativePictures()+m_pcRPS->getNumberOfPositivePictures()-1 ; i--)
430  {
431    if(m_pcRPS->getUsed(i))
432    {
433      pcRefPic = xGetLongTermRefPic(rcListPic, m_pcRPS->getPOC(i));
434      pcRefPic->setIsLongTerm(1);
435      pcRefPic->setIsUsedAsLongTerm(1);
436      pcRefPic->getPicYuvRec()->extendPicBorder();
437      RefPicSetLtCurr[NumPocLtCurr] = pcRefPic;
438      NumPocLtCurr++;
439    }
440    if(pcRefPic==NULL) 
441    {
442      pcRefPic = xGetLongTermRefPic(rcListPic, m_pcRPS->getPOC(i));
443    }
444    pcRefPic->setCheckLTMSBPresent(m_pcRPS->getCheckLTMSBPresent(i)); 
445  }
446
447  // ref_pic_list_init
448  UInt cIdx = 0;
449  UInt num_ref_idx_l0_active_minus1 = m_aiNumRefIdx[0] - 1;
450  UInt num_ref_idx_l1_active_minus1 = m_aiNumRefIdx[1] - 1;
451  TComPic*  refPicListTemp0[MAX_NUM_REF+1];
452  TComPic*  refPicListTemp1[MAX_NUM_REF+1];
453  Int  numRpsCurrTempList0, numRpsCurrTempList1;
454 
455  numRpsCurrTempList0 = numRpsCurrTempList1 = NumPocStCurr0 + NumPocStCurr1 + NumPocLtCurr;
456  if (numRpsCurrTempList0 <= num_ref_idx_l0_active_minus1)
457  {
458    numRpsCurrTempList0 = num_ref_idx_l0_active_minus1 + 1;
459  }
460  if (numRpsCurrTempList1 <= num_ref_idx_l1_active_minus1)
461  {
462    numRpsCurrTempList1 = num_ref_idx_l1_active_minus1 + 1;
463  }
464
465  cIdx = 0;
466  while (cIdx < numRpsCurrTempList0)
467  {
468    for ( i=0; i<NumPocStCurr0 && cIdx<numRpsCurrTempList0; cIdx++,i++)
469    {
470      refPicListTemp0[cIdx] = RefPicSetStCurr0[ i ];
471    }
472    for ( i=0; i<NumPocStCurr1 && cIdx<numRpsCurrTempList0; cIdx++,i++)
473    {
474      refPicListTemp0[cIdx] = RefPicSetStCurr1[ i ];
475    }
476    for ( i=0; i<NumPocLtCurr && cIdx<numRpsCurrTempList0; cIdx++,i++)
477    {
478      refPicListTemp0[cIdx] = RefPicSetLtCurr[ i ];
479    }
480  }
481  cIdx = 0;
482  while (cIdx<numRpsCurrTempList1 && m_eSliceType==B_SLICE)
483  {
484    for ( i=0; i<NumPocStCurr1 && cIdx<numRpsCurrTempList1; cIdx++,i++)
485    {
486      refPicListTemp1[cIdx] = RefPicSetStCurr1[ i ];
487    }
488    for ( i=0; i<NumPocStCurr0 && cIdx<numRpsCurrTempList1; cIdx++,i++)
489    {
490      refPicListTemp1[cIdx] = RefPicSetStCurr0[ i ];
491    }
492    for ( i=0; i<NumPocLtCurr && cIdx<numRpsCurrTempList1; cIdx++,i++)
493    {
494      refPicListTemp1[cIdx] = RefPicSetLtCurr[ i ];
495    }
496  }
497
498  for (cIdx = 0; cIdx <= num_ref_idx_l0_active_minus1; cIdx ++)
499  {
500    m_apcRefPicList[0][cIdx] = m_RefPicListModification.getRefPicListModificationFlagL0() ? refPicListTemp0[ m_RefPicListModification.getRefPicSetIdxL0(cIdx) ] : refPicListTemp0[cIdx];
501  }
502  if ( m_eSliceType == P_SLICE )
503  {
504    m_aiNumRefIdx[1] = 0;
505    ::memset( m_apcRefPicList[1], 0, sizeof(m_apcRefPicList[1]));
506  }
507  else
508  {
509    for (cIdx = 0; cIdx <= num_ref_idx_l1_active_minus1; cIdx ++)
510    {
511      m_apcRefPicList[1][cIdx] = m_RefPicListModification.getRefPicListModificationFlagL1() ? refPicListTemp1[ m_RefPicListModification.getRefPicSetIdxL1(cIdx) ] : refPicListTemp1[cIdx];
512    }
513  }
514}
515
516#if REF_IDX_FRAMEWORK
517Void TComSlice::addRefPicList( TComPic **pIlpPicList, Int iRefPicNum, Int iInsertOffset )
518{
519  if(getSPS()->getLayerId() && m_eSliceType != I_SLICE)
520  {
521#if REF_IDX_MFM
522    assert(iRefPicNum == 1);
523    if(getPOC() != 0)
524    { 
525      assert(getBaseColPic()->IsUpsampledMvField() == true); 
526      pIlpPicList[0]->copyUpsampledMvField(getBaseColPic());
527    }
528#endif
529    //add to list 0;
530    Int iOffset;
531    m_aiNumRefIdx[REF_PIC_LIST_0] += iInsertOffset;
532    iOffset = m_aiNumRefIdx[REF_PIC_LIST_0];
533    for (Int i=0; i<iRefPicNum; i++)
534    {
535      pIlpPicList[i]->setIsLongTerm(1);  //mark ilp as long-term reference
536      pIlpPicList[i]->setIsUsedAsLongTerm(1);  //mark ilp as long-term reference
537      m_apcRefPicList[REF_PIC_LIST_0][iOffset + i] = pIlpPicList[i]; 
538      m_aiNumRefIdx[REF_PIC_LIST_0]++;
539      //m_aiNumRefIdx[REF_PIC_LIST_C]++;
540    }
541    if(m_eSliceType == B_SLICE)
542    {
543      m_aiNumRefIdx[REF_PIC_LIST_1] += iInsertOffset;
544      iOffset = m_aiNumRefIdx[REF_PIC_LIST_1];
545      for (Int i=0; i<iRefPicNum; i++)
546      {
547        pIlpPicList[i]->setIsLongTerm(1);  //mark ilp as long-term reference
548        pIlpPicList[i]->setIsUsedAsLongTerm(1);  //mark ilp as long-term reference
549        m_apcRefPicList[REF_PIC_LIST_1][iOffset + i] = pIlpPicList[i]; 
550        m_aiNumRefIdx[REF_PIC_LIST_1]++;
551        //m_aiNumRefIdx[REF_PIC_LIST_C]++;
552      }
553    }
554  }
555}
556
557#if REF_IDX_MFM
558Void TComSlice::setRefPOCListILP(TComPic** ilpPic, TComPic *pcRefPicBL)
559{
560  //set reference picture POC of each ILP reference
561  Int thePoc = ilpPic[0]->getPOC(); 
562  assert(thePoc >= 0); 
563  assert(thePoc == pcRefPicBL->getPOC());
564
565  //initialize reference POC of ILP
566  for(Int refIdx = 0; refIdx < MAX_NUM_REF; refIdx++) 
567  { 
568    ilpPic[0]->getSlice(0)->setRefPOC(0, REF_PIC_LIST_0, refIdx); 
569    ilpPic[0]->getSlice(0)->setRefPOC(0, REF_PIC_LIST_1, refIdx); 
570
571    ilpPic[0]->getSlice(0)->setRefPic(NULL, REF_PIC_LIST_0, refIdx); 
572    ilpPic[0]->getSlice(0)->setRefPic(NULL, REF_PIC_LIST_1, refIdx); 
573  }
574
575  //set reference POC of ILP
576  ilpPic[0]->getSlice(0)->setNumRefIdx(REF_PIC_LIST_0, pcRefPicBL->getSlice(0)->getNumRefIdx(REF_PIC_LIST_0));
577  assert(ilpPic[0]->getSlice(0)->getNumRefIdx(REF_PIC_LIST_0) <= MAX_NUM_REF); 
578  ilpPic[0]->getSlice(0)->setNumRefIdx(REF_PIC_LIST_1, pcRefPicBL->getSlice(0)->getNumRefIdx(REF_PIC_LIST_1));
579  assert(ilpPic[0]->getSlice(0)->getNumRefIdx(REF_PIC_LIST_1) <= MAX_NUM_REF);
580 
581  for(Int refIdx = 0; refIdx < pcRefPicBL->getSlice(0)->getNumRefIdx(REF_PIC_LIST_0); refIdx++) 
582    ilpPic[0]->getSlice(0)->setRefPOC(pcRefPicBL->getSlice(0)->getRefPOC(REF_PIC_LIST_0, refIdx), REF_PIC_LIST_0, refIdx); 
583
584  for(Int refIdx = 0; refIdx < pcRefPicBL->getSlice(0)->getNumRefIdx(REF_PIC_LIST_1); refIdx++) 
585    ilpPic[0]->getSlice(0)->setRefPOC(pcRefPicBL->getSlice(0)->getRefPOC(REF_PIC_LIST_1, refIdx), REF_PIC_LIST_1, refIdx);
586
587  for(Int refIdx = 0; refIdx < pcRefPicBL->getSlice(0)->getNumRefIdx(REF_PIC_LIST_0); refIdx++) 
588    ilpPic[0]->getSlice(0)->setRefPic(pcRefPicBL->getSlice(0)->getRefPic(REF_PIC_LIST_0, refIdx), REF_PIC_LIST_0, refIdx); 
589
590  for(Int refIdx = 0; refIdx < pcRefPicBL->getSlice(0)->getNumRefIdx(REF_PIC_LIST_1); refIdx++) 
591    ilpPic[0]->getSlice(0)->setRefPic(pcRefPicBL->getSlice(0)->getRefPic(REF_PIC_LIST_1, refIdx), REF_PIC_LIST_1, refIdx); 
592return;
593}
594#endif
595
596#endif
597
598Int TComSlice::getNumRpsCurrTempList()
599{
600  Int numRpsCurrTempList = 0;
601
602  if (m_eSliceType == I_SLICE) 
603  {
604    return 0;
605  }
606  for(UInt i=0; i < m_pcRPS->getNumberOfNegativePictures()+ m_pcRPS->getNumberOfPositivePictures() + m_pcRPS->getNumberOfLongtermPictures(); i++)
607  {
608    if(m_pcRPS->getUsed(i))
609    {
610      numRpsCurrTempList++;
611    }
612  }
613  return numRpsCurrTempList;
614}
615#if SVC_EXTENSION
616Void TComSlice::setBaseColPic(  TComList<TComPic*>& rcListPic, UInt layerID )
617{ 
618  if (layerID == 0)
619  {
620    m_pcBaseColPic = NULL;
621    return;
622  }       
623  setBaseColPic(xGetRefPic(rcListPic, getPOC())); 
624}
625#endif
626
627Void TComSlice::initEqualRef()
628{
629  for (Int iDir = 0; iDir < 2; iDir++)
630  {
631    for (Int iRefIdx1 = 0; iRefIdx1 < MAX_NUM_REF; iRefIdx1++)
632    {
633      for (Int iRefIdx2 = iRefIdx1; iRefIdx2 < MAX_NUM_REF; iRefIdx2++)
634      {
635        m_abEqualRef[iDir][iRefIdx1][iRefIdx2] = m_abEqualRef[iDir][iRefIdx2][iRefIdx1] = (iRefIdx1 == iRefIdx2? true : false);
636      }
637    }
638  }
639}
640
641Void TComSlice::checkColRefIdx(UInt curSliceIdx, TComPic* pic)
642{
643  Int i;
644  TComSlice* curSlice = pic->getSlice(curSliceIdx);
645  Int currColRefPOC =  curSlice->getRefPOC( RefPicList(1-curSlice->getColFromL0Flag()), curSlice->getColRefIdx());
646  TComSlice* preSlice;
647  Int preColRefPOC;
648  for(i=curSliceIdx-1; i>=0; i--)
649  {
650    preSlice = pic->getSlice(i);
651    if(preSlice->getSliceType() != I_SLICE)
652    {
653      preColRefPOC  = preSlice->getRefPOC( RefPicList(1-preSlice->getColFromL0Flag()), preSlice->getColRefIdx());
654      if(currColRefPOC != preColRefPOC)
655      {
656        printf("Collocated_ref_idx shall always be the same for all slices of a coded picture!\n");
657        exit(EXIT_FAILURE);
658      }
659      else
660      {
661        break;
662      }
663    }
664  }
665}
666
667Void TComSlice::checkCRA(TComReferencePictureSet *pReferencePictureSet, Int& pocCRA, Bool& prevRAPisBLA, TComList<TComPic*>& rcListPic)
668{
669  for(Int i = 0; i < pReferencePictureSet->getNumberOfNegativePictures()+pReferencePictureSet->getNumberOfPositivePictures(); i++)
670  {
671    if(pocCRA < MAX_UINT && getPOC() > pocCRA)
672    {
673      assert(getPOC()+pReferencePictureSet->getDeltaPOC(i) >= pocCRA);
674    }
675  }
676  for(Int i = pReferencePictureSet->getNumberOfNegativePictures()+pReferencePictureSet->getNumberOfPositivePictures(); i < pReferencePictureSet->getNumberOfPictures(); i++)
677  {
678    if(pocCRA < MAX_UINT && getPOC() > pocCRA)
679    {
680      assert(pReferencePictureSet->getPOC(i) >= pocCRA);
681    }
682  }
683#if SUPPORT_FOR_RAP_N_LP
684  if ( getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR || getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_N_LP ) // IDR picture found
685#else
686  if ( getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR ) // IDR picture found
687#endif
688  {
689    prevRAPisBLA = false;
690  }
691#if NAL_UNIT_TYPES_J1003_D7
692  else if ( getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA ) // CRA picture found
693#else
694  else if ( getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA || getNalUnitType() == NAL_UNIT_CODED_SLICE_CRANT ) // CRA/CRANT picture found
695#endif
696  {
697    pocCRA = getPOC();
698    prevRAPisBLA = false;
699  }
700#if SUPPORT_FOR_RAP_N_LP
701  else if ( getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA
702         || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLANT
703         || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP ) // BLA picture found
704#else
705  else if ( getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLANT ) // BLA/BLANT picture found
706#endif
707  {
708    pocCRA = getPOC();
709    prevRAPisBLA = true;
710  }
711}
712
713/** Function for marking the reference pictures when an IDR/CRA/CRANT/BLA/BLANT is encountered.
714 * \param pocCRA POC of the CRA/CRANT/BLA/BLANT picture
715 * \param bRefreshPending flag indicating if a deferred decoding refresh is pending
716 * \param rcListPic reference to the reference picture list
717 * This function marks the reference pictures as "unused for reference" in the following conditions.
718 * If the nal_unit_type is IDR/BLA/BLANT, all pictures in the reference picture list 
719 * are marked as "unused for reference"
720 *    If the nal_unit_type is BLA/BLANT, set the pocCRA to the temporal reference of the current picture.
721 * Otherwise
722 *    If the bRefreshPending flag is true (a deferred decoding refresh is pending) and the current
723 *    temporal reference is greater than the temporal reference of the latest CRA/CRANT/BLA/BLANT picture (pocCRA),
724 *    mark all reference pictures except the latest CRA/CRANT/BLA/BLANT picture as "unused for reference" and set
725 *    the bRefreshPending flag to false.
726 *    If the nal_unit_type is CRA/CRANT, set the bRefreshPending flag to true and pocCRA to the temporal
727 *    reference of the current picture.
728 * Note that the current picture is already placed in the reference list and its marking is not changed.
729 * If the current picture has a nal_ref_idc that is not 0, it will remain marked as "used for reference".
730 */
731Void TComSlice::decodingRefreshMarking(Int& pocCRA, Bool& bRefreshPending, TComList<TComPic*>& rcListPic)
732{
733  TComPic*                 rpcPic;
734  UInt uiPOCCurr = getPOC(); 
735
736#if SUPPORT_FOR_RAP_N_LP
737  if ( getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA
738    || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLANT
739    || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP
740    || getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR
741    || getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_N_LP )  // IDR or BLA picture
742#else
743  if (getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLANT)  // IDR/BLA/BLANT
744#endif
745  {
746    // mark all pictures as not used for reference
747    TComList<TComPic*>::iterator        iterPic       = rcListPic.begin();
748    while (iterPic != rcListPic.end())
749    {
750      rpcPic = *(iterPic);
751      rpcPic->setCurrSliceIdx(0);
752      if (rpcPic->getPOC() != uiPOCCurr) rpcPic->getSlice(0)->setReferenced(false);
753      iterPic++;
754    }
755#if SUPPORT_FOR_RAP_N_LP
756    if ( getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA
757      || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLANT
758      || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP )
759#else
760    if (getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLANT)
761#endif
762    {
763      pocCRA = uiPOCCurr;
764    }
765  }
766  else // CRA or No DR
767  {
768    if (bRefreshPending==true && uiPOCCurr > pocCRA) // CRA reference marking pending
769    {
770      TComList<TComPic*>::iterator        iterPic       = rcListPic.begin();
771      while (iterPic != rcListPic.end())
772      {
773        rpcPic = *(iterPic);
774        if (rpcPic->getPOC() != uiPOCCurr && rpcPic->getPOC() != pocCRA) rpcPic->getSlice(0)->setReferenced(false);
775        iterPic++;
776      }
777      bRefreshPending = false; 
778    }
779#if NAL_UNIT_TYPES_J1003_D7
780    if ( getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA ) // CRA picture found
781#else
782    if (getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA || getNalUnitType() == NAL_UNIT_CODED_SLICE_CRANT) // CRA/CRANT picture found
783#endif
784    {
785      bRefreshPending = true; 
786      pocCRA = uiPOCCurr;
787    }
788  }
789}
790
791Void TComSlice::copySliceInfo(TComSlice *pSrc)
792{
793  assert( pSrc != NULL );
794
795  Int i, j, k;
796
797  m_iPOC                 = pSrc->m_iPOC;
798  m_eNalUnitType         = pSrc->m_eNalUnitType;
799  m_eSliceType           = pSrc->m_eSliceType;
800  m_iSliceQp             = pSrc->m_iSliceQp;
801#if ADAPTIVE_QP_SELECTION
802  m_iSliceQpBase         = pSrc->m_iSliceQpBase;
803#endif
804  m_deblockingFilterDisable   = pSrc->m_deblockingFilterDisable;
805  m_deblockingFilterOverrideFlag = pSrc->m_deblockingFilterOverrideFlag;
806  m_deblockingFilterBetaOffsetDiv2 = pSrc->m_deblockingFilterBetaOffsetDiv2;
807  m_deblockingFilterTcOffsetDiv2 = pSrc->m_deblockingFilterTcOffsetDiv2;
808 
809  for (i = 0; i < 3; i++)
810  {
811    m_aiNumRefIdx[i]     = pSrc->m_aiNumRefIdx[i];
812  }
813
814  for (i = 0; i < 2; i++)
815  {
816    for (j = 0; j < MAX_NUM_REF_LC; j++)
817    {
818       m_iRefIdxOfLC[i][j]  = pSrc->m_iRefIdxOfLC[i][j];
819    }
820  }
821  for (i = 0; i < MAX_NUM_REF_LC; i++)
822  {
823    m_eListIdFromIdxOfLC[i] = pSrc->m_eListIdFromIdxOfLC[i];
824    m_iRefIdxFromIdxOfLC[i] = pSrc->m_iRefIdxFromIdxOfLC[i];
825    m_iRefIdxOfL1FromRefIdxOfL0[i] = pSrc->m_iRefIdxOfL1FromRefIdxOfL0[i];
826    m_iRefIdxOfL0FromRefIdxOfL1[i] = pSrc->m_iRefIdxOfL0FromRefIdxOfL1[i];
827  }
828  m_bRefPicListModificationFlagLC = pSrc->m_bRefPicListModificationFlagLC;
829  m_bRefPicListCombinationFlag    = pSrc->m_bRefPicListCombinationFlag;
830  m_bCheckLDC             = pSrc->m_bCheckLDC;
831  m_iSliceQpDelta        = pSrc->m_iSliceQpDelta;
832#if CHROMA_QP_EXTENSION
833  m_iSliceQpDeltaCb      = pSrc->m_iSliceQpDeltaCb;
834  m_iSliceQpDeltaCr      = pSrc->m_iSliceQpDeltaCr;
835#endif
836  for (i = 0; i < 2; i++)
837  {
838    for (j = 0; j < MAX_NUM_REF; j++)
839    {
840      m_apcRefPicList[i][j]  = pSrc->m_apcRefPicList[i][j];
841      m_aiRefPOCList[i][j]   = pSrc->m_aiRefPOCList[i][j];
842    }
843  } 
844  m_iDepth               = pSrc->m_iDepth;
845
846  // referenced slice
847  m_bRefenced            = pSrc->m_bRefenced;
848
849  // access channel
850  m_pcSPS                = pSrc->m_pcSPS;
851  m_pcPPS                = pSrc->m_pcPPS;
852  m_pcRPS                = pSrc->m_pcRPS;
853  m_iLastIDR             = pSrc->m_iLastIDR;
854
855  m_pcPic                = pSrc->m_pcPic;
856#if !REMOVE_APS
857  m_pcAPS                = pSrc->m_pcAPS;
858  m_iAPSId               = pSrc->m_iAPSId;
859#endif
860
861  m_colFromL0Flag        = pSrc->m_colFromL0Flag;
862  m_colRefIdx            = pSrc->m_colRefIdx;
863#if ALF_CHROMA_LAMBDA || SAO_CHROMA_LAMBDA
864  m_dLambdaLuma          = pSrc->m_dLambdaLuma;
865  m_dLambdaChroma        = pSrc->m_dLambdaChroma;
866#else
867  m_dLambda              = pSrc->m_dLambda;
868#endif
869  for (i = 0; i < 2; i++)
870  {
871    for (j = 0; j < MAX_NUM_REF; j++)
872    {
873      for (k =0; k < MAX_NUM_REF; k++)
874      {
875        m_abEqualRef[i][j][k] = pSrc->m_abEqualRef[i][j][k];
876      }
877    }
878  }
879
880  m_bNoBackPredFlag      = pSrc->m_bNoBackPredFlag;
881  m_uiTLayer                      = pSrc->m_uiTLayer;
882  m_bTLayerSwitchingFlag          = pSrc->m_bTLayerSwitchingFlag;
883
884  m_uiSliceMode                   = pSrc->m_uiSliceMode;
885  m_uiSliceArgument               = pSrc->m_uiSliceArgument;
886  m_uiSliceCurStartCUAddr         = pSrc->m_uiSliceCurStartCUAddr;
887  m_uiSliceCurEndCUAddr           = pSrc->m_uiSliceCurEndCUAddr;
888  m_uiSliceIdx                    = pSrc->m_uiSliceIdx;
889  m_uiDependentSliceMode            = pSrc->m_uiDependentSliceMode;
890  m_uiDependentSliceArgument        = pSrc->m_uiDependentSliceArgument; 
891  m_uiDependentSliceCurStartCUAddr  = pSrc->m_uiDependentSliceCurStartCUAddr;
892  m_uiDependentSliceCurEndCUAddr    = pSrc->m_uiDependentSliceCurEndCUAddr;
893  m_bNextSlice                    = pSrc->m_bNextSlice;
894  m_bNextDependentSlice             = pSrc->m_bNextDependentSlice;
895  for ( int e=0 ; e<2 ; e++ )
896  {
897    for ( int n=0 ; n<MAX_NUM_REF ; n++ )
898    {
899      memcpy(m_weightPredTable[e][n], pSrc->m_weightPredTable[e][n], sizeof(wpScalingParam)*3 );
900    }
901  }
902  m_saoEnabledFlag = pSrc->m_saoEnabledFlag; 
903#if SAO_TYPE_SHARING
904  m_saoEnabledFlagChroma = pSrc->m_saoEnabledFlagChroma;
905#else
906  m_saoEnabledFlagCb = pSrc->m_saoEnabledFlagCb;
907  m_saoEnabledFlagCr = pSrc->m_saoEnabledFlagCr; 
908#endif
909  m_cabacInitFlag                = pSrc->m_cabacInitFlag;
910  m_numEntryPointOffsets  = pSrc->m_numEntryPointOffsets;
911
912  m_bLMvdL1Zero = pSrc->m_bLMvdL1Zero;
913#if !REMOVE_ALF
914  for(Int compIdx=0; compIdx < 3; compIdx++)
915  {
916    m_alfEnabledFlag[compIdx] = pSrc->m_alfEnabledFlag[compIdx];
917  }
918#endif
919  m_LFCrossSliceBoundaryFlag = pSrc->m_LFCrossSliceBoundaryFlag;
920  m_enableTMVPFlag                = pSrc->m_enableTMVPFlag;
921}
922
923#if PREVREFPIC_DEFN
924int TComSlice::m_prevPOC[MAX_TLAYER] = {0};
925#else
926int TComSlice::m_prevPOC = 0;
927#endif
928/** Function for setting the slice's temporal layer ID and corresponding temporal_layer_switching_point_flag.
929 * \param uiTLayer Temporal layer ID of the current slice
930 * The decoder calls this function to set temporal_layer_switching_point_flag for each temporal layer based on
931 * the SPS's temporal_id_nesting_flag and the parsed PPS.  Then, current slice's temporal layer ID and
932 * temporal_layer_switching_point_flag is set accordingly.
933 */
934Void TComSlice::setTLayerInfo( UInt uiTLayer )
935{
936  m_uiTLayer = uiTLayer;
937}
938
939/** Function for checking if this is a switching-point
940*/
941Bool TComSlice::isTemporalLayerSwitchingPoint( TComList<TComPic*>& rcListPic, TComReferencePictureSet *pReferencePictureSet)
942{
943  TComPic* rpcPic;
944  // loop through all pictures in the reference picture buffer
945  TComList<TComPic*>::iterator iterPic = rcListPic.begin();
946  while ( iterPic != rcListPic.end())
947  {
948    rpcPic = *(iterPic++);
949    if(rpcPic->getSlice(0)->isReferenced() && rpcPic->getPOC() != getPOC())
950    {
951      if(rpcPic->getTLayer() >= getTLayer())
952      {
953        return false;
954      }
955    }
956  }
957  return true;
958}
959
960#if STSA
961/** Function for checking if this is a STSA candidate
962 */
963Bool TComSlice::isStepwiseTemporalLayerSwitchingPointCandidate( TComList<TComPic*>& rcListPic, TComReferencePictureSet *pReferencePictureSet)
964{
965    TComPic* rpcPic;
966   
967    TComList<TComPic*>::iterator iterPic = rcListPic.begin();
968    while ( iterPic != rcListPic.end())
969    {
970        rpcPic = *(iterPic++);
971        if(rpcPic->getSlice(0)->isReferenced() &&  (rpcPic->getUsedByCurr()==true) && rpcPic->getPOC() != getPOC())
972        {
973            if(rpcPic->getTLayer() >= getTLayer())
974            {
975                return false;
976            }
977        }
978    }
979    return true;
980}
981#endif
982
983/** Function for applying picture marking based on the Reference Picture Set in pReferencePictureSet.
984*/
985Void TComSlice::applyReferencePictureSet( TComList<TComPic*>& rcListPic, TComReferencePictureSet *pReferencePictureSet)
986{
987  TComPic* rpcPic;
988  Int i, isReference;
989
990  Int j = 0;
991  // loop through all pictures in the reference picture buffer
992  TComList<TComPic*>::iterator iterPic = rcListPic.begin();
993  while ( iterPic != rcListPic.end())
994  {
995    j++;
996    rpcPic = *(iterPic++);
997
998    isReference = 0;
999    // loop through all pictures in the Reference Picture Set
1000    // to see if the picture should be kept as reference picture
1001    for(i=0;i<pReferencePictureSet->getNumberOfPositivePictures()+pReferencePictureSet->getNumberOfNegativePictures();i++)
1002    {
1003      if(!rpcPic->getIsLongTerm() && rpcPic->getPicSym()->getSlice(0)->getPOC() == this->getPOC() + pReferencePictureSet->getDeltaPOC(i))
1004      {
1005        isReference = 1;
1006        rpcPic->setUsedByCurr(pReferencePictureSet->getUsed(i));
1007        rpcPic->setIsLongTerm(0);
1008        rpcPic->setIsUsedAsLongTerm(0);
1009      }
1010    }
1011    for(;i<pReferencePictureSet->getNumberOfPictures();i++)
1012    {
1013      if(pReferencePictureSet->getCheckLTMSBPresent(i)==true)
1014      {
1015        if(rpcPic->getIsLongTerm() && (rpcPic->getPicSym()->getSlice(0)->getPOC()) == pReferencePictureSet->getPOC(i))
1016        {
1017          isReference = 1;
1018          rpcPic->setUsedByCurr(pReferencePictureSet->getUsed(i));
1019        }
1020      }
1021      else 
1022      {
1023        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()))
1024        {
1025          isReference = 1;
1026          rpcPic->setUsedByCurr(pReferencePictureSet->getUsed(i));
1027        }
1028      }
1029
1030    }
1031    // mark the picture as "unused for reference" if it is not in
1032    // the Reference Picture Set
1033    if(rpcPic->getPicSym()->getSlice(0)->getPOC() != this->getPOC() && isReference == 0)   
1034    {           
1035      rpcPic->getSlice( 0 )->setReferenced( false );   
1036      rpcPic->setIsLongTerm(0);
1037    }
1038    //check that pictures of higher temporal layers are not used
1039    assert(rpcPic->getSlice( 0 )->isReferenced()==0||rpcPic->getUsedByCurr()==0||rpcPic->getTLayer()<=this->getTLayer());
1040    //check that pictures of higher or equal temporal layer are not in the RPS if the current picture is a TSA picture
1041#if NAL_UNIT_TYPES_J1003_D7
1042    if(this->getNalUnitType() == NAL_UNIT_CODED_SLICE_TLA || this->getNalUnitType() == NAL_UNIT_CODED_SLICE_TSA_N)
1043#else
1044    if(this->getNalUnitType() == NAL_UNIT_CODED_SLICE_TLA)
1045#endif
1046    {
1047      assert(rpcPic->getSlice( 0 )->isReferenced()==0||rpcPic->getTLayer()<this->getTLayer());
1048    }
1049#if TEMPORAL_LAYER_NON_REFERENCE
1050    //check that pictures marked as temporal layer non-reference pictures are not used for reference
1051    if(rpcPic->getPicSym()->getSlice(0)->getPOC() != this->getPOC() && rpcPic->getTLayer()==this->getTLayer())
1052    {
1053      assert(rpcPic->getSlice( 0 )->isReferenced()==0||rpcPic->getUsedByCurr()==0||rpcPic->getSlice( 0 )->getTemporalLayerNonReferenceFlag()==false);
1054    }
1055#endif
1056  }
1057}
1058
1059/** Function for applying picture marking based on the Reference Picture Set in pReferencePictureSet.
1060*/
1061Int TComSlice::checkThatAllRefPicsAreAvailable( TComList<TComPic*>& rcListPic, TComReferencePictureSet *pReferencePictureSet, Bool printErrors, Int pocRandomAccess)
1062{
1063  TComPic* rpcPic;
1064  Int i, isAvailable, j;
1065  Int atLeastOneLost = 0;
1066  Int atLeastOneRemoved = 0;
1067  Int iPocLost = 0;
1068
1069  // loop through all long-term pictures in the Reference Picture Set
1070  // to see if the picture should be kept as reference picture
1071  for(i=pReferencePictureSet->getNumberOfNegativePictures()+pReferencePictureSet->getNumberOfPositivePictures();i<pReferencePictureSet->getNumberOfPictures();i++)
1072  {
1073    j = 0;
1074    isAvailable = 0;
1075    // loop through all pictures in the reference picture buffer
1076    TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1077    while ( iterPic != rcListPic.end())
1078    {
1079      j++;
1080      rpcPic = *(iterPic++);
1081      if(pReferencePictureSet->getCheckLTMSBPresent(i)==true)
1082      {
1083        if(rpcPic->getIsLongTerm() && (rpcPic->getPicSym()->getSlice(0)->getPOC()) == pReferencePictureSet->getPOC(i) && rpcPic->getSlice(0)->isReferenced())
1084        {
1085          isAvailable = 1;
1086        }
1087      }
1088      else 
1089      {
1090        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())
1091        {
1092          isAvailable = 1;
1093        }
1094      }
1095    }
1096    // if there was no such long-term check the short terms
1097    if(!isAvailable)
1098    {
1099      iterPic = rcListPic.begin();
1100      while ( iterPic != rcListPic.end())
1101      {
1102        j++;
1103        rpcPic = *(iterPic++);
1104
1105        if((rpcPic->getPicSym()->getSlice(0)->getPOC()%(1<<rpcPic->getPicSym()->getSlice(0)->getSPS()->getBitsForPOC())) == (this->getPOC() + pReferencePictureSet->getDeltaPOC(i))%(1<<rpcPic->getPicSym()->getSlice(0)->getSPS()->getBitsForPOC()) && rpcPic->getSlice(0)->isReferenced())
1106        {
1107          isAvailable = 1;
1108          rpcPic->setIsLongTerm(1);
1109          rpcPic->setIsUsedAsLongTerm(1);
1110          break;
1111        }
1112      }
1113    }
1114    // report that a picture is lost if it is in the Reference Picture Set
1115    // but not available as reference picture
1116    if(isAvailable == 0)   
1117    {           
1118      if (this->getPOC() + pReferencePictureSet->getDeltaPOC(i) >= pocRandomAccess)
1119      {
1120        if(!pReferencePictureSet->getUsed(i) )
1121        {
1122          if(printErrors)
1123          {
1124            printf("\nLong-term reference picture with POC = %3d seems to have been removed or not correctly decoded.", this->getPOC() + pReferencePictureSet->getDeltaPOC(i));
1125          }
1126          atLeastOneRemoved = 1;
1127        }
1128        else
1129        {
1130          if(printErrors)
1131          {
1132            printf("\nLong-term reference picture with POC = %3d is lost or not correctly decoded!", this->getPOC() + pReferencePictureSet->getDeltaPOC(i));
1133          }
1134          atLeastOneLost = 1;
1135          iPocLost=this->getPOC() + pReferencePictureSet->getDeltaPOC(i);
1136        }
1137      }
1138    }
1139  } 
1140  // loop through all short-term pictures in the Reference Picture Set
1141  // to see if the picture should be kept as reference picture
1142  for(i=0;i<pReferencePictureSet->getNumberOfNegativePictures()+pReferencePictureSet->getNumberOfPositivePictures();i++)
1143  {
1144    j = 0;
1145    isAvailable = 0;
1146    // loop through all pictures in the reference picture buffer
1147    TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1148    while ( iterPic != rcListPic.end())
1149    {
1150      j++;
1151      rpcPic = *(iterPic++);
1152
1153      if(!rpcPic->getIsLongTerm() && rpcPic->getPicSym()->getSlice(0)->getPOC() == this->getPOC() + pReferencePictureSet->getDeltaPOC(i) && rpcPic->getSlice(0)->isReferenced())
1154      {
1155        isAvailable = 1;
1156      }
1157    }
1158    // report that a picture is lost if it is in the Reference Picture Set
1159    // but not available as reference picture
1160    if(isAvailable == 0)   
1161    {           
1162      if (this->getPOC() + pReferencePictureSet->getDeltaPOC(i) >= pocRandomAccess)
1163      {
1164        if(!pReferencePictureSet->getUsed(i) )
1165        {
1166          if(printErrors)
1167            printf("\nShort-term reference picture with POC = %3d seems to have been removed or not correctly decoded.", this->getPOC() + pReferencePictureSet->getDeltaPOC(i));
1168          atLeastOneRemoved = 1;
1169        }
1170        else
1171        {
1172          if(printErrors)
1173            printf("\nShort-term reference picture with POC = %3d is lost or not correctly decoded!", this->getPOC() + pReferencePictureSet->getDeltaPOC(i));
1174          atLeastOneLost = 1;
1175          iPocLost=this->getPOC() + pReferencePictureSet->getDeltaPOC(i);
1176        }
1177      }
1178    }
1179  }   
1180  if(atLeastOneLost)
1181  {
1182    return iPocLost+1;
1183  }
1184  if(atLeastOneRemoved)
1185  {
1186    return -2;
1187  }
1188  else
1189  {
1190    return 0;
1191  }
1192}
1193
1194/** Function for constructing an explicit Reference Picture Set out of the available pictures in a referenced Reference Picture Set
1195*/
1196Void TComSlice::createExplicitReferencePictureSetFromReference( TComList<TComPic*>& rcListPic, TComReferencePictureSet *pReferencePictureSet)
1197{
1198  TComPic* rpcPic;
1199  Int i, j;
1200  Int k = 0;
1201  Int nrOfNegativePictures = 0;
1202  Int nrOfPositivePictures = 0;
1203  TComReferencePictureSet* pcRPS = this->getLocalRPS();
1204
1205  // loop through all pictures in the Reference Picture Set
1206  for(i=0;i<pReferencePictureSet->getNumberOfPictures();i++)
1207  {
1208    j = 0;
1209    // loop through all pictures in the reference picture buffer
1210    TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1211    while ( iterPic != rcListPic.end())
1212    {
1213      j++;
1214      rpcPic = *(iterPic++);
1215
1216      if(rpcPic->getPicSym()->getSlice(0)->getPOC() == this->getPOC() + pReferencePictureSet->getDeltaPOC(i) && rpcPic->getSlice(0)->isReferenced())
1217      {
1218        // This picture exists as a reference picture
1219        // and should be added to the explicit Reference Picture Set
1220        pcRPS->setDeltaPOC(k, pReferencePictureSet->getDeltaPOC(i));
1221        pcRPS->setUsed(k, pReferencePictureSet->getUsed(i));
1222        if(pcRPS->getDeltaPOC(k) < 0)
1223        {
1224          nrOfNegativePictures++;
1225        }
1226        else
1227        {
1228          nrOfPositivePictures++;
1229        }
1230        k++;
1231      }
1232    }
1233  }
1234  pcRPS->setNumberOfNegativePictures(nrOfNegativePictures);
1235  pcRPS->setNumberOfPositivePictures(nrOfPositivePictures);
1236  pcRPS->setNumberOfPictures(nrOfNegativePictures+nrOfPositivePictures);
1237  // This is a simplistic inter rps example. A smarter encoder will look for a better reference RPS to do the
1238  // inter RPS prediction with.  Here we just use the reference used by pReferencePictureSet.
1239  // If pReferencePictureSet is not inter_RPS_predicted, then inter_RPS_prediction is for the current RPS also disabled.
1240  if (!pReferencePictureSet->getInterRPSPrediction())
1241  {
1242    pcRPS->setInterRPSPrediction(false);
1243    pcRPS->setNumRefIdc(0);
1244  }
1245  else
1246  {
1247    Int rIdx =  this->getRPSidx() - pReferencePictureSet->getDeltaRIdxMinus1() - 1;
1248    Int deltaRPS = pReferencePictureSet->getDeltaRPS();
1249    TComReferencePictureSet* pcRefRPS = this->getSPS()->getRPSList()->getReferencePictureSet(rIdx);
1250    Int iRefPics = pcRefRPS->getNumberOfPictures();
1251    Int iNewIdc=0;
1252    for(i=0; i<= iRefPics; i++) 
1253    {
1254      Int deltaPOC = ((i != iRefPics)? pcRefRPS->getDeltaPOC(i) : 0);  // check if the reference abs POC is >= 0
1255      Int iRefIdc = 0;
1256      for (j=0; j < pcRPS->getNumberOfPictures(); j++) // loop through the  pictures in the new RPS
1257      {
1258        if ( (deltaPOC + deltaRPS) == pcRPS->getDeltaPOC(j))
1259        {
1260          if (pcRPS->getUsed(j))
1261          {
1262            iRefIdc = 1;
1263          }
1264          else
1265          {
1266            iRefIdc = 2;
1267          }
1268        }
1269      }
1270      pcRPS->setRefIdc(i, iRefIdc);
1271      iNewIdc++;
1272    }
1273    pcRPS->setInterRPSPrediction(true);
1274    pcRPS->setNumRefIdc(iNewIdc);
1275    pcRPS->setDeltaRPS(deltaRPS); 
1276    pcRPS->setDeltaRIdxMinus1(pReferencePictureSet->getDeltaRIdxMinus1() + this->getSPS()->getRPSList()->getNumberOfReferencePictureSets() - this->getRPSidx());
1277  }
1278
1279  this->setRPS(pcRPS);
1280  this->setRPSidx(-1);
1281}
1282
1283/** get AC and DC values for weighted pred
1284 * \param *wp
1285 * \returns Void
1286 */
1287Void  TComSlice::getWpAcDcParam(wpACDCParam *&wp)
1288{
1289  wp = m_weightACDCParam;
1290}
1291
1292/** init AC and DC values for weighted pred
1293 * \returns Void
1294 */
1295Void  TComSlice::initWpAcDcParam()
1296{
1297  for(Int iComp = 0; iComp < 3; iComp++ )
1298  {
1299    m_weightACDCParam[iComp].iAC = 0;
1300    m_weightACDCParam[iComp].iDC = 0;
1301  }
1302}
1303
1304/** get WP tables for weighted pred
1305 * \param RefPicList
1306 * \param iRefIdx
1307 * \param *&wpScalingParam
1308 * \returns Void
1309 */
1310Void  TComSlice::getWpScaling( RefPicList e, Int iRefIdx, wpScalingParam *&wp )
1311{
1312  wp = m_weightPredTable[e][iRefIdx];
1313}
1314
1315/** reset Default WP tables settings : no weight.
1316 * \param wpScalingParam
1317 * \returns Void
1318 */
1319Void  TComSlice::resetWpScaling(wpScalingParam  wp[2][MAX_NUM_REF][3])
1320{
1321  for ( int e=0 ; e<2 ; e++ ) 
1322  {
1323    for ( int i=0 ; i<MAX_NUM_REF ; i++ )
1324    {
1325      for ( int yuv=0 ; yuv<3 ; yuv++ ) 
1326      {
1327        wpScalingParam  *pwp = &(wp[e][i][yuv]);
1328        pwp->bPresentFlag      = false;
1329        pwp->uiLog2WeightDenom = 0;
1330        pwp->uiLog2WeightDenom = 0;
1331        pwp->iWeight           = 1;
1332        pwp->iOffset           = 0;
1333      }
1334    }
1335  }
1336}
1337
1338/** init WP table
1339 * \returns Void
1340 */
1341Void  TComSlice::initWpScaling()
1342{
1343  initWpScaling(m_weightPredTable);
1344}
1345
1346/** set WP tables
1347 * \param wpScalingParam
1348 * \returns Void
1349 */
1350Void  TComSlice::initWpScaling(wpScalingParam  wp[2][MAX_NUM_REF][3])
1351{
1352  for ( int e=0 ; e<2 ; e++ ) 
1353  {
1354    for ( int i=0 ; i<MAX_NUM_REF ; i++ )
1355    {
1356      for ( int yuv=0 ; yuv<3 ; yuv++ ) 
1357      {
1358        wpScalingParam  *pwp = &(wp[e][i][yuv]);
1359        if ( !pwp->bPresentFlag ) {
1360          // Inferring values not present :
1361          pwp->iWeight = (1 << pwp->uiLog2WeightDenom);
1362          pwp->iOffset = 0;
1363        }
1364
1365        pwp->w      = pwp->iWeight;
1366        pwp->o      = pwp->iOffset * (1 << (g_uiBitDepth-8));
1367        pwp->shift  = pwp->uiLog2WeightDenom;
1368        pwp->round  = (pwp->uiLog2WeightDenom>=1) ? (1 << (pwp->uiLog2WeightDenom-1)) : (0);
1369      }
1370    }
1371  }
1372}
1373
1374// ------------------------------------------------------------------------------------------------
1375// Video parameter set (VPS)
1376// ------------------------------------------------------------------------------------------------
1377TComVPS::TComVPS()
1378: m_VPSId                     (  0)
1379, m_uiMaxTLayers              (  1)
1380, m_uiMaxLayers               (  1)
1381, m_bTemporalIdNestingFlag    (false)
1382{
1383
1384  for( Int i = 0; i < MAX_TLAYER; i++)
1385  {
1386    m_numReorderPics[i] = 0;
1387    m_uiMaxDecPicBuffering[i] = 0; 
1388    m_uiMaxLatencyIncrease[i] = 0;
1389  }
1390}
1391
1392TComVPS::~TComVPS()
1393{
1394
1395
1396}
1397
1398// ------------------------------------------------------------------------------------------------
1399// Sequence parameter set (SPS)
1400// ------------------------------------------------------------------------------------------------
1401
1402TComSPS::TComSPS()
1403: m_SPSId                     (  0)
1404#if !SPS_SYNTAX_CHANGES
1405, m_ProfileSpace              (  0)
1406, m_ProfileIdc                (  0)
1407, m_ReservedIndicatorFlags    (  0)
1408, m_LevelIdc                  (  0)
1409, m_ProfileCompatibility      (  0)
1410#endif
1411, m_VPSId                     (  0)
1412, m_chromaFormatIdc           (CHROMA_420)
1413, m_uiMaxTLayers              (  1)
1414// Structure
1415, m_picWidthInLumaSamples     (352)
1416, m_picHeightInLumaSamples    (288)
1417, m_picCroppingFlag           (false)
1418, m_picCropLeftOffset         (  0)
1419, m_picCropRightOffset        (  0)
1420, m_picCropTopOffset          (  0)
1421, m_picCropBottomOffset       (  0) 
1422, m_uiMaxCUWidth              ( 32)
1423, m_uiMaxCUHeight             ( 32)
1424, m_uiMaxCUDepth              (  3)
1425, m_uiMinTrDepth              (  0)
1426, m_uiMaxTrDepth              (  1)
1427, m_bLongTermRefsPresent      (false)
1428, m_uiQuadtreeTULog2MaxSize   (  0)
1429, m_uiQuadtreeTULog2MinSize   (  0)
1430, m_uiQuadtreeTUMaxDepthInter (  0)
1431, m_uiQuadtreeTUMaxDepthIntra (  0)
1432// Tool list
1433, m_usePCM                   (false)
1434, m_pcmLog2MaxSize            (  5)
1435, m_uiPCMLog2MinSize          (  7)
1436#if !REMOVE_ALF
1437, m_bUseALF                   (false)
1438#endif
1439#if !REMOVE_LMCHROMA
1440, m_bUseLMChroma              (false)
1441#endif
1442#if !PPS_TS_FLAG
1443, m_useTransformSkip           (false)
1444, m_useTransformSkipFast       (false)
1445#endif
1446, m_bUseLComb                 (false)
1447, m_restrictedRefPicListsFlag   (  1)
1448, m_listsModificationPresentFlag(  0)
1449, m_uiBitDepth                (  8)
1450, m_uiBitIncrement            (  0)
1451, m_qpBDOffsetY               (  0)
1452, m_qpBDOffsetC               (  0)
1453, m_useLossless               (false)
1454, m_uiPCMBitDepthLuma         (  8)
1455, m_uiPCMBitDepthChroma       (  8)
1456, m_bPCMFilterDisableFlag     (false)
1457, m_uiBitsForPOC              (  8)
1458#if LTRP_IN_SPS
1459, m_numLongTermRefPicSPS    (  0) 
1460#endif
1461, m_uiMaxTrSize               ( 32)
1462#if !MOVE_LOOP_FILTER_SLICES_FLAG
1463, m_bLFCrossSliceBoundaryFlag (false)
1464#endif
1465, m_bUseSAO                   (false) 
1466, m_bTemporalIdNestingFlag    (false)
1467, m_scalingListEnabledFlag    (false)
1468#if SUPPORT_FOR_VUI
1469, m_vuiParametersPresentFlag  (false)
1470, m_vuiParameters             ()
1471#endif
1472#if SVC_EXTENSION
1473, m_layerId(0)
1474#endif
1475{
1476#if !SPS_AMVP_CLEANUP
1477  // AMVP parameter
1478  ::memset( m_aeAMVPMode, 0, sizeof( m_aeAMVPMode ) );
1479#endif
1480  for ( Int i = 0; i < MAX_TLAYER; i++ )
1481  {
1482    m_uiMaxLatencyIncrease[i] = 0;
1483    m_uiMaxDecPicBuffering[i] = 0;
1484    m_numReorderPics[i]       = 0;
1485  }
1486  m_scalingList = new TComScalingList;
1487#if LTRP_IN_SPS
1488  ::memset(m_ltRefPicPocLsbSps, 0, sizeof(m_ltRefPicPocLsbSps));
1489  ::memset(m_usedByCurrPicLtSPSFlag, 0, sizeof(m_usedByCurrPicLtSPSFlag));
1490#endif
1491}
1492
1493TComSPS::~TComSPS()
1494{
1495  delete m_scalingList;
1496  m_RPSList.destroy();
1497}
1498
1499Void  TComSPS::createRPSList( Int numRPS )
1500{ 
1501  m_RPSList.destroy();
1502  m_RPSList.create(numRPS);
1503}
1504#if BUFFERING_PERIOD_AND_TIMING_SEI
1505Void TComSPS::setHrdParameters( UInt frameRate, UInt numDU, UInt bitRate, Bool randomAccess )
1506{
1507  if( !getVuiParametersPresentFlag() )
1508  {
1509    return;
1510  }
1511
1512  TComVUI *vui = getVuiParameters();
1513
1514  vui->setTimingInfoPresentFlag( true );
1515  switch( frameRate )
1516  {
1517  case 24:
1518    vui->setNumUnitsInTick( 1125000 );    vui->setTimeScale    ( 27000000 );
1519    break;
1520  case 25:
1521    vui->setNumUnitsInTick( 1080000 );    vui->setTimeScale    ( 27000000 );
1522    break;
1523  case 30:
1524    vui->setNumUnitsInTick( 900900 );     vui->setTimeScale    ( 27000000 );
1525    break;
1526  case 50:
1527    vui->setNumUnitsInTick( 540000 );     vui->setTimeScale    ( 27000000 );
1528    break;
1529  case 60:
1530    vui->setNumUnitsInTick( 450450 );     vui->setTimeScale    ( 27000000 );
1531    break;
1532  default:
1533    vui->setNumUnitsInTick( 1001 );       vui->setTimeScale    ( 60000 );
1534    break;
1535  }
1536
1537  Bool rateCnt = ( bitRate > 0 );
1538  vui->setNalHrdParametersPresentFlag( rateCnt );
1539  vui->setVclHrdParametersPresentFlag( rateCnt );
1540
1541  vui->setSubPicCpbParamsPresentFlag( ( numDU > 1 ) );
1542
1543  if( vui->getSubPicCpbParamsPresentFlag() )
1544  {
1545    vui->setTickDivisorMinus2( 100 - 2 );                          //
1546    vui->setDuCpbRemovalDelayLengthMinus1( 7 );                    // 8-bit precision ( plus 1 for last DU in AU )
1547  }
1548
1549  vui->setBitRateScale( 4 );                                       // in units of 2~( 6 + 4 ) = 1,024 bps
1550  vui->setCpbSizeScale( 6 );                                       // in units of 2~( 4 + 4 ) = 1,024 bit
1551
1552  vui->setInitialCpbRemovalDelayLengthMinus1(15);                  // assuming 0.5 sec, log2( 90,000 * 0.5 ) = 16-bit
1553  if( randomAccess )
1554  {
1555    vui->setCpbRemovalDelayLengthMinus1(5);                        // 32 = 2^5 (plus 1)
1556    vui->setDpbOutputDelayLengthMinus1 (5);                        // 32 + 3 = 2^6
1557  }
1558  else
1559  {
1560    vui->setCpbRemovalDelayLengthMinus1(9);                        // max. 2^10
1561    vui->setDpbOutputDelayLengthMinus1 (9);                        // max. 2^10
1562  }
1563
1564/*
1565   Note: only the case of "vps_max_temporal_layers_minus1 = 0" is supported.
1566*/
1567  Int i, j;
1568  UInt birateValue, cpbSizeValue;
1569
1570  for( i = 0; i < MAX_TLAYER; i ++ )
1571  {
1572    vui->setFixedPicRateFlag( i, 1 );
1573    vui->setPicDurationInTcMinus1( i, 0 );
1574    vui->setLowDelayHrdFlag( i, 0 );
1575    vui->setCpbCntMinus1( i, 0 );
1576
1577    birateValue  = bitRate;
1578    cpbSizeValue = bitRate;                                     // 1 second
1579    for( j = 0; j < ( vui->getCpbCntMinus1( i ) + 1 ); j ++ )
1580    {
1581      vui->setBitRateValueMinus1( i, j, 0, ( birateValue  - 1 ) );
1582      vui->setCpbSizeValueMinus1( i, j, 0, ( cpbSizeValue - 1 ) );
1583      vui->setCbrFlag( i, j, 0, ( j == 0 ) );
1584
1585      vui->setBitRateValueMinus1( i, j, 1, ( birateValue  - 1) );
1586      vui->setCpbSizeValueMinus1( i, j, 1, ( cpbSizeValue - 1 ) );
1587      vui->setCbrFlag( i, j, 1, ( j == 0 ) );
1588    }
1589  }
1590}
1591#endif
1592const Int TComSPS::m_cropUnitX[]={1,2,2,1};
1593const Int TComSPS::m_cropUnitY[]={1,2,1,1};
1594
1595TComPPS::TComPPS()
1596: m_PPSId                       (0)
1597, m_SPSId                       (0)
1598, m_picInitQPMinus26            (0)
1599, m_useDQP                      (false)
1600, m_bConstrainedIntraPred       (false)
1601#if CHROMA_QP_EXTENSION
1602, m_bSliceChromaQpFlag          (false)
1603#endif
1604, m_pcSPS                       (NULL)
1605, m_uiMaxCuDQPDepth             (0)
1606, m_uiMinCuDQPSize              (0)
1607, m_chromaCbQpOffset            (0)
1608, m_chromaCrQpOffset            (0)
1609, m_numRefIdxL0DefaultActive    (1)
1610, m_numRefIdxL1DefaultActive    (1)
1611#if !REMOVE_FGS
1612, m_iSliceGranularity           (0)
1613#endif
1614, m_TransquantBypassEnableFlag  (false)
1615#if PPS_TS_FLAG
1616, m_useTransformSkip             (false)
1617#endif
1618#if TILES_WPP_ENTROPYSLICES_FLAGS
1619, m_dependentSliceEnabledFlag    (false)
1620, m_tilesEnabledFlag               (false)
1621, m_entropyCodingSyncEnabledFlag   (false)
1622, m_entropySliceEnabledFlag        (false)
1623#endif
1624, m_loopFilterAcrossTilesEnabledFlag  (true)
1625, m_uniformSpacingFlag           (0)
1626, m_iNumColumnsMinus1            (0)
1627, m_puiColumnWidth               (NULL)
1628, m_iNumRowsMinus1               (0)
1629, m_puiRowHeight                 (NULL)
1630, m_iNumSubstreams             (1)
1631, m_signHideFlag(0)
1632, m_cabacInitPresentFlag        (false)
1633, m_encCABACTableIdx            (I_SLICE)
1634#if SLICE_HEADER_EXTENSION
1635, m_sliceHeaderExtensionPresentFlag    (false)
1636#endif
1637#if MOVE_LOOP_FILTER_SLICES_FLAG
1638, m_loopFilterAcrossSlicesEnabledFlag (false)
1639#endif
1640{
1641  m_scalingList = new TComScalingList;
1642#if !TILES_WPP_ENTROPYSLICES_FLAGS
1643#if DEPENDENT_SLICES
1644  m_bDependentSliceEnabledFlag = false;
1645  m_bCabacIndependentFlag = false;
1646#endif
1647#endif
1648}
1649
1650TComPPS::~TComPPS()
1651{
1652  if( m_iNumColumnsMinus1 > 0 && m_uniformSpacingFlag == 0 )
1653  {
1654    if (m_puiColumnWidth) delete [] m_puiColumnWidth; 
1655    m_puiColumnWidth = NULL;
1656  }
1657  if( m_iNumRowsMinus1 > 0 && m_uniformSpacingFlag == 0 )
1658  {
1659    if (m_puiRowHeight) delete [] m_puiRowHeight;
1660    m_puiRowHeight = NULL;
1661  }
1662  delete m_scalingList;
1663}
1664
1665TComReferencePictureSet::TComReferencePictureSet()
1666: m_numberOfPictures (0)
1667, m_numberOfNegativePictures (0)
1668, m_numberOfPositivePictures (0)
1669, m_numberOfLongtermPictures (0)
1670, m_interRPSPrediction (0) 
1671, m_deltaRIdxMinus1 (0)   
1672, m_deltaRPS (0) 
1673, m_numRefIdc (0) 
1674{
1675  ::memset( m_deltaPOC, 0, sizeof(m_deltaPOC) );
1676  ::memset( m_POC, 0, sizeof(m_POC) );
1677  ::memset( m_used, 0, sizeof(m_used) );
1678  ::memset( m_refIdc, 0, sizeof(m_refIdc) );
1679}
1680
1681TComReferencePictureSet::~TComReferencePictureSet()
1682{
1683}
1684
1685Void TComReferencePictureSet::setUsed(Int bufferNum, Bool used)
1686{
1687  m_used[bufferNum] = used;
1688}
1689
1690Void TComReferencePictureSet::setDeltaPOC(Int bufferNum, Int deltaPOC)
1691{
1692  m_deltaPOC[bufferNum] = deltaPOC;
1693}
1694
1695Void TComReferencePictureSet::setNumberOfPictures(Int numberOfPictures)
1696{
1697  m_numberOfPictures = numberOfPictures;
1698}
1699
1700Int TComReferencePictureSet::getUsed(Int bufferNum)
1701{
1702  return m_used[bufferNum];
1703}
1704
1705Int TComReferencePictureSet::getDeltaPOC(Int bufferNum)
1706{
1707  return m_deltaPOC[bufferNum];
1708}
1709
1710Int TComReferencePictureSet::getNumberOfPictures()
1711{
1712  return m_numberOfPictures;
1713}
1714
1715Int TComReferencePictureSet::getPOC(Int bufferNum)
1716{
1717  return m_POC[bufferNum];
1718}
1719Void TComReferencePictureSet::setPOC(Int bufferNum, Int POC)
1720{
1721  m_POC[bufferNum] = POC;
1722}
1723Bool TComReferencePictureSet::getCheckLTMSBPresent(Int bufferNum)
1724{
1725  return m_bCheckLTMSB[bufferNum];
1726}
1727Void TComReferencePictureSet::setCheckLTMSBPresent(Int bufferNum, Bool b)
1728{
1729  m_bCheckLTMSB[bufferNum] = b;
1730}
1731
1732/** set the reference idc value at uiBufferNum entry to the value of iRefIdc
1733 * \param uiBufferNum
1734 * \param iRefIdc
1735 * \returns Void
1736 */
1737Void TComReferencePictureSet::setRefIdc(Int bufferNum, Int refIdc)
1738{
1739  m_refIdc[bufferNum] = refIdc;
1740}
1741
1742/** get the reference idc value at uiBufferNum
1743 * \param uiBufferNum
1744 * \returns Int
1745 */
1746Int  TComReferencePictureSet::getRefIdc(Int bufferNum)
1747{
1748  return m_refIdc[bufferNum];
1749}
1750
1751/** Sorts the deltaPOC and Used by current values in the RPS based on the deltaPOC values.
1752 *  deltaPOC values are sorted with -ve values before the +ve values.  -ve values are in decreasing order.
1753 *  +ve values are in increasing order.
1754 * \returns Void
1755 */
1756Void TComReferencePictureSet::sortDeltaPOC()
1757{
1758  // sort in increasing order (smallest first)
1759  for(Int j=1; j < getNumberOfPictures(); j++)
1760  { 
1761    Int deltaPOC = getDeltaPOC(j);
1762    Bool used = getUsed(j);
1763    for (Int k=j-1; k >= 0; k--)
1764    {
1765      Int temp = getDeltaPOC(k);
1766      if (deltaPOC < temp)
1767      {
1768        setDeltaPOC(k+1, temp);
1769        setUsed(k+1, getUsed(k));
1770        setDeltaPOC(k, deltaPOC);
1771        setUsed(k, used);
1772      }
1773    }
1774  }
1775  // flip the negative values to largest first
1776  Int numNegPics = getNumberOfNegativePictures();
1777  for(Int j=0, k=numNegPics-1; j < numNegPics>>1; j++, k--)
1778  { 
1779    Int deltaPOC = getDeltaPOC(j);
1780    Bool used = getUsed(j);
1781    setDeltaPOC(j, getDeltaPOC(k));
1782    setUsed(j, getUsed(k));
1783    setDeltaPOC(k, deltaPOC);
1784    setUsed(k, used);
1785  }
1786}
1787
1788/** Prints the deltaPOC and RefIdc (if available) values in the RPS.
1789 *  A "*" is added to the deltaPOC value if it is Used bu current.
1790 * \returns Void
1791 */
1792Void TComReferencePictureSet::printDeltaPOC()
1793{
1794  printf("DeltaPOC = { ");
1795  for(Int j=0; j < getNumberOfPictures(); j++)
1796  {
1797    printf("%d%s ", getDeltaPOC(j), (getUsed(j)==1)?"*":"");
1798  } 
1799  if (getInterRPSPrediction()) 
1800  {
1801    printf("}, RefIdc = { ");
1802    for(Int j=0; j < getNumRefIdc(); j++)
1803    {
1804      printf("%d ", getRefIdc(j));
1805    } 
1806  }
1807  printf("}\n");
1808}
1809
1810TComRPSList::TComRPSList()
1811:m_referencePictureSets (NULL)
1812{
1813}
1814
1815TComRPSList::~TComRPSList()
1816{
1817}
1818
1819Void TComRPSList::create( Int numberOfReferencePictureSets)
1820{
1821  m_numberOfReferencePictureSets = numberOfReferencePictureSets;
1822  m_referencePictureSets = new TComReferencePictureSet[numberOfReferencePictureSets];
1823}
1824
1825Void TComRPSList::destroy()
1826{
1827  if (m_referencePictureSets)
1828  {
1829    delete [] m_referencePictureSets;
1830  }
1831  m_numberOfReferencePictureSets = 0;
1832  m_referencePictureSets = NULL;
1833}
1834
1835
1836
1837TComReferencePictureSet* TComRPSList::getReferencePictureSet(Int referencePictureSetNum)
1838{
1839  return &m_referencePictureSets[referencePictureSetNum];
1840}
1841
1842Int TComRPSList::getNumberOfReferencePictureSets()
1843{
1844  return m_numberOfReferencePictureSets;
1845}
1846
1847Void TComRPSList::setNumberOfReferencePictureSets(Int numberOfReferencePictureSets)
1848{
1849  m_numberOfReferencePictureSets = numberOfReferencePictureSets;
1850}
1851
1852TComRefPicListModification::TComRefPicListModification()
1853: m_bRefPicListModificationFlagL0 (false)
1854, m_bRefPicListModificationFlagL1 (false)
1855{
1856  ::memset( m_RefPicSetIdxL0, 0, sizeof(m_RefPicSetIdxL0) );
1857  ::memset( m_RefPicSetIdxL1, 0, sizeof(m_RefPicSetIdxL1) );
1858}
1859
1860TComRefPicListModification::~TComRefPicListModification()
1861{
1862}
1863
1864#if !REMOVE_APS
1865TComAPS::TComAPS()
1866{
1867  m_apsID = 0;
1868  m_pSaoParam = NULL;
1869#if !REMOVE_ALF
1870  m_alfParam[0] = m_alfParam[1] = m_alfParam[2] = NULL;
1871#endif
1872}
1873
1874TComAPS::~TComAPS()
1875{
1876  delete m_pSaoParam;
1877#if !REMOVE_ALF
1878  for(Int compIdx =0; compIdx < 3; compIdx++)
1879  {
1880    delete m_alfParam[compIdx];
1881    m_alfParam[compIdx] = NULL;
1882  }
1883#endif
1884}
1885
1886TComAPS& TComAPS::operator= (const TComAPS& src)
1887{
1888  m_apsID       = src.m_apsID;
1889  m_pSaoParam   = src.m_pSaoParam;
1890#if !REMOVE_ALF
1891  for(Int compIdx =0; compIdx < 3; compIdx++)
1892  {
1893    m_alfParam[compIdx] = src.m_alfParam[compIdx];
1894  }
1895#endif
1896  return *this;
1897}
1898
1899Void TComAPS::createSaoParam()
1900{
1901  m_pSaoParam = new SAOParam;
1902}
1903
1904Void TComAPS::destroySaoParam()
1905{
1906  if(m_pSaoParam != NULL)
1907  {
1908    delete m_pSaoParam;
1909    m_pSaoParam = NULL;
1910  }
1911}
1912
1913#if !REMOVE_ALF
1914Void TComAPS::createAlfParam()
1915{
1916  for(Int compIdx =0; compIdx < 3; compIdx++)
1917  {
1918    m_alfParam[compIdx] = new ALFParam(compIdx);
1919    m_alfParam[compIdx]->alf_flag = 0;
1920  }
1921}
1922Void TComAPS::destroyAlfParam()
1923{
1924  for(Int compIdx=0; compIdx < 3; compIdx++)
1925  {
1926    if(m_alfParam[compIdx] != NULL)
1927    {
1928      delete m_alfParam[compIdx];
1929      m_alfParam[compIdx] = NULL;
1930    }
1931  }
1932}
1933#endif
1934#endif
1935
1936TComScalingList::TComScalingList()
1937{
1938#if TS_FLAT_QUANTIZATION_MATRIX
1939  m_useTransformSkip = false;
1940#endif
1941  init();
1942}
1943TComScalingList::~TComScalingList()
1944{
1945  destroy();
1946}
1947
1948/** set default quantization matrix to array
1949*/
1950Void TComSlice::setDefaultScalingList()
1951{
1952  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
1953  {
1954    for(UInt listId=0;listId<g_scalingListNum[sizeId];listId++)
1955    {
1956      getScalingList()->processDefaultMarix(sizeId, listId);
1957    }
1958  }
1959}
1960/** check if use default quantization matrix
1961 * \returns true if use default quantization matrix in all size
1962*/
1963Bool TComSlice::checkDefaultScalingList()
1964{
1965  UInt defaultCounter=0;
1966
1967  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
1968  {
1969    for(UInt listId=0;listId<g_scalingListNum[sizeId];listId++)
1970    {
1971      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
1972     && ((sizeId < SCALING_LIST_16x16) || (getScalingList()->getScalingListDC(sizeId,listId) == 16))) // check DC value
1973      {
1974        defaultCounter++;
1975      }
1976    }
1977  }
1978  return (defaultCounter == (SCALING_LIST_NUM * SCALING_LIST_SIZE_NUM - 4)) ? false : true; // -4 for 32x32
1979}
1980/** get scaling matrix from RefMatrixID
1981 * \param sizeId size index
1982 * \param Index of input matrix
1983 * \param Index of reference matrix
1984 */
1985Void TComScalingList::processRefMatrix( UInt sizeId, UInt listId , UInt refListId )
1986{
1987  ::memcpy(getScalingListAddress(sizeId, listId),((listId == refListId)? getScalingListDefaultAddress(sizeId, refListId): getScalingListAddress(sizeId, refListId)),sizeof(Int)*min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId]));
1988}
1989/** parse syntax infomation
1990 *  \param pchFile syntax infomation
1991 *  \returns false if successful
1992 */
1993Bool TComScalingList::xParseScalingList(char* pchFile)
1994{
1995  FILE *fp;
1996  Char line[1024];
1997  UInt sizeIdc,listIdc;
1998  UInt i,size = 0;
1999  Int *src=0,data;
2000  Char *ret;
2001  UInt  retval;
2002
2003  if((fp = fopen(pchFile,"r")) == (FILE*)NULL)
2004  {
2005    printf("can't open file %s :: set Default Matrix\n",pchFile);
2006    return true;
2007  }
2008
2009  for(sizeIdc = 0; sizeIdc < SCALING_LIST_SIZE_NUM; sizeIdc++)
2010  {
2011    size = min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeIdc]);
2012    for(listIdc = 0; listIdc < g_scalingListNum[sizeIdc]; listIdc++)
2013    {
2014      src = getScalingListAddress(sizeIdc, listIdc);
2015
2016      fseek(fp,0,0);
2017      do 
2018      {
2019        ret = fgets(line, 1024, fp);
2020        if ((ret==NULL)||(strstr(line, MatrixType[sizeIdc][listIdc])==NULL && feof(fp)))
2021        {
2022          printf("Error: can't read Matrix :: set Default Matrix\n");
2023          return true;
2024        }
2025      }
2026      while (strstr(line, MatrixType[sizeIdc][listIdc]) == NULL);
2027      for (i=0; i<size; i++)
2028      {
2029        retval = fscanf(fp, "%d,", &data);
2030        if (retval!=1)
2031        {
2032          printf("Error: can't read Matrix :: set Default Matrix\n");
2033          return true;
2034        }
2035        src[i] = data;
2036      }
2037      //set DC value for default matrix check
2038      setScalingListDC(sizeIdc,listIdc,src[0]);
2039
2040      if(sizeIdc > SCALING_LIST_8x8)
2041      {
2042        fseek(fp,0,0);
2043        do 
2044        {
2045          ret = fgets(line, 1024, fp);
2046          if ((ret==NULL)||(strstr(line, MatrixType_DC[sizeIdc][listIdc])==NULL && feof(fp)))
2047          {
2048            printf("Error: can't read DC :: set Default Matrix\n");
2049            return true;
2050          }
2051        }
2052        while (strstr(line, MatrixType_DC[sizeIdc][listIdc]) == NULL);
2053        retval = fscanf(fp, "%d,", &data);
2054        if (retval!=1)
2055        {
2056          printf("Error: can't read Matrix :: set Default Matrix\n");
2057          return true;
2058        }
2059        //overwrite DC value when size of matrix is larger than 16x16
2060        setScalingListDC(sizeIdc,listIdc,data);
2061      }
2062    }
2063  }
2064  fclose(fp);
2065  return false;
2066}
2067
2068/** initialization process of quantization matrix array
2069 */
2070Void TComScalingList::init()
2071{
2072  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
2073  {
2074    for(UInt listId = 0; listId < g_scalingListNum[sizeId]; listId++)
2075    {
2076      m_scalingListCoef[sizeId][listId] = new Int [min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId])];
2077    }
2078  }
2079  m_scalingListCoef[SCALING_LIST_32x32][3] = m_scalingListCoef[SCALING_LIST_32x32][1]; // copy address for 32x32
2080}
2081/** destroy quantization matrix array
2082 */
2083Void TComScalingList::destroy()
2084{
2085  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
2086  {
2087    for(UInt listId = 0; listId < g_scalingListNum[sizeId]; listId++)
2088    {
2089      if(m_scalingListCoef[sizeId][listId]) delete [] m_scalingListCoef[sizeId][listId];
2090    }
2091  }
2092}
2093/** get default address of quantization matrix
2094 * \param sizeId size index
2095 * \param listId list index
2096 * \returns pointer of quantization matrix
2097 */
2098Int* TComScalingList::getScalingListDefaultAddress(UInt sizeId, UInt listId)
2099{
2100  Int *src = 0;
2101  switch(sizeId)
2102  {
2103    case SCALING_LIST_4x4:
2104#if TS_FLAT_QUANTIZATION_MATRIX
2105      if( m_useTransformSkip )
2106      {
2107        src = g_quantTSDefault4x4;
2108      }
2109      else
2110      {
2111        src = (listId<3) ? g_quantIntraDefault4x4 : g_quantInterDefault4x4;
2112      }
2113#else
2114      src = (listId<3) ? g_quantIntraDefault4x4 : g_quantInterDefault4x4;
2115#endif
2116      break;
2117    case SCALING_LIST_8x8:
2118      src = (listId<3) ? g_quantIntraDefault8x8 : g_quantInterDefault8x8;
2119      break;
2120    case SCALING_LIST_16x16:
2121      src = (listId<3) ? g_quantIntraDefault8x8 : g_quantInterDefault8x8;
2122      break;
2123    case SCALING_LIST_32x32:
2124      src = (listId<1) ? g_quantIntraDefault8x8 : g_quantInterDefault8x8;
2125      break;
2126    default:
2127      assert(0);
2128      src = NULL;
2129      break;
2130  }
2131  return src;
2132}
2133/** process of default matrix
2134 * \param sizeId size index
2135 * \param Index of input matrix
2136 */
2137Void TComScalingList::processDefaultMarix(UInt sizeId, UInt listId)
2138{
2139  ::memcpy(getScalingListAddress(sizeId, listId),getScalingListDefaultAddress(sizeId,listId),sizeof(Int)*min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId]));
2140  setScalingListDC(sizeId,listId,SCALING_LIST_DC);
2141}
2142/** check DC value of matrix for default matrix signaling
2143 */
2144Void TComScalingList::checkDcOfMatrix()
2145{
2146  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
2147  {
2148    for(UInt listId = 0; listId < g_scalingListNum[sizeId]; listId++)
2149    {
2150      //check default matrix?
2151      if(getScalingListDC(sizeId,listId) == 0)
2152      {
2153        processDefaultMarix(sizeId, listId);
2154      }
2155    }
2156  }
2157}
2158
2159ParameterSetManager::ParameterSetManager()
2160: m_vpsMap(MAX_NUM_VPS)
2161, m_spsMap(MAX_NUM_SPS)
2162, m_ppsMap(MAX_NUM_PPS)
2163#if !REMOVE_APS
2164, m_apsMap(MAX_NUM_APS)
2165#endif
2166{
2167}
2168
2169
2170ParameterSetManager::~ParameterSetManager()
2171{
2172}
2173
2174#if PROFILE_TIER_LEVEL_SYNTAX
2175ProfileTierLevel::ProfileTierLevel()
2176  : m_profileSpace    (0)
2177  , m_tierFlag        (false)
2178  , m_profileIdc      (0)
2179  , m_levelIdc        (0)
2180{
2181  ::memset(m_profileCompatibilityFlag, 0, sizeof(m_profileCompatibilityFlag));
2182}
2183TComPTL::TComPTL()
2184{
2185  ::memset(m_subLayerProfilePresentFlag, 0, sizeof(m_subLayerProfilePresentFlag));
2186  ::memset(m_subLayerLevelPresentFlag,   0, sizeof(m_subLayerLevelPresentFlag  ));
2187}
2188#endif
2189//! \}
Note: See TracBrowser for help on using the repository browser.