source: 3DVCSoftware/branches/HTM-16.0-MV-draft-5/source/Lib/TLibDecoder/TDecCAVLC.cpp @ 1417

Last change on this file since 1417 was 1390, checked in by tech, 9 years ago

Removed 3D.

  • Property svn:eol-style set to native
File size: 129.8 KB
Line 
1/* The copyright in this software is being made available under the BSD
2* License, included below. This software may be subject to other third party
3* and contributor rights, including patent rights, and no such rights are
4* granted under this license.
5*
6* Copyright (c) 2010-2015, 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     TDecCAVLC.cpp
35\brief    CAVLC decoder class
36*/
37
38#include "TDecCAVLC.h"
39#include "SEIread.h"
40#include "TDecSlice.h"
41#include "TLibCommon/TComChromaFormat.h"
42#if RExt__DECODER_DEBUG_BIT_STATISTICS
43#include "TLibCommon/TComCodingStatistics.h"
44#endif
45#if NH_MV
46#include "TDecTop.h"
47#endif
48
49//! \ingroup TLibDecoder
50//! \{
51
52#if ENC_DEC_TRACE
53#if !NH_MV_ENC_DEC_TRAC
54Void  xTraceVPSHeader ()
55{
56  fprintf( g_hTrace, "=========== Video Parameter Set     ===========\n" );
57}
58
59Void  xTraceSPSHeader ()
60{
61  fprintf( g_hTrace, "=========== Sequence Parameter Set  ===========\n" );
62}
63
64Void  xTracePPSHeader ()
65{
66  fprintf( g_hTrace, "=========== Picture Parameter Set  ===========\n");
67}
68
69Void  xTraceSliceHeader ()
70{
71  fprintf( g_hTrace, "=========== Slice ===========\n");
72}
73
74#endif
75#endif
76// ====================================================================================================================
77// Constructor / destructor / create / destroy
78// ====================================================================================================================
79
80TDecCavlc::TDecCavlc()
81{
82}
83
84TDecCavlc::~TDecCavlc()
85{
86
87}
88
89// ====================================================================================================================
90// Public member functions
91// ====================================================================================================================
92
93#if NH_MV
94Void TDecCavlc::parseShortTermRefPicSet( TComSPS* sps, TComStRefPicSet* stRps, Int stRpsIdx )
95{
96  UInt uiCode; 
97  if( stRpsIdx  !=  0 )
98  {
99    READ_FLAG( uiCode, "inter_ref_pic_set_prediction_flag" ); stRps->setInterRefPicSetPredictionFlag( uiCode == 1 );
100  }
101  if( stRps->getInterRefPicSetPredictionFlag() )
102  {
103    if( stRpsIdx  ==  sps->getNumShortTermRefPicSets() )
104    {
105      READ_UVLC( uiCode, "delta_idx_minus1" ); stRps->setDeltaIdxMinus1( uiCode );
106    }
107    READ_FLAG( uiCode, "delta_rps_sign" ); stRps->setDeltaRpsSign( uiCode == 1 );
108    READ_UVLC( uiCode, "abs_delta_rps_minus1" ); stRps->setAbsDeltaRpsMinus1( uiCode );
109    for( Int j = 0; j  <=  sps->getStRefPicSet( stRps->getRefRpsIdx( stRpsIdx ) )->getNumDeltaPocs() ; j++ )
110    {
111      READ_FLAG( uiCode, "used_by_curr_pic_flag" ); stRps->setUsedByCurrPicFlag( j, uiCode == 1 );
112      if( !stRps->getUsedByCurrPicFlag( j ) )
113      {
114        READ_FLAG( uiCode, "use_delta_flag" ); stRps->setUseDeltaFlag( j, uiCode == 1 );
115      }
116    }
117  }
118  else
119  {
120    READ_UVLC( uiCode, "num_negative_pics" ); stRps->setNumNegativePics( uiCode );
121    READ_UVLC( uiCode, "num_positive_pics" ); stRps->setNumPositivePics( uiCode );
122    for( Int i = 0; i < stRps->getNumNegativePics( ); i++ )
123    {
124      READ_UVLC( uiCode, "delta_poc_s0_minus1" ); stRps->setDeltaPocS0Minus1( i, uiCode );
125      READ_FLAG( uiCode, "used_by_curr_pic_s0_flag" ); stRps->setUsedByCurrPicS0Flag( i, uiCode == 1 );
126    }
127    for( Int i = 0; i < stRps->getNumPositivePics( ); i++ )
128    {
129      READ_UVLC( uiCode, "delta_poc_s1_minus1" ); stRps->setDeltaPocS1Minus1( i, uiCode );
130      READ_FLAG( uiCode, "used_by_curr_pic_s1_flag" ); stRps->setUsedByCurrPicS1Flag( i, uiCode == 1 );
131    }
132  }
133  stRps->inferRps( stRpsIdx, sps, false );
134}
135#else
136Void TDecCavlc::parseShortTermRefPicSet( TComSPS* sps, TComReferencePictureSet* rps, Int idx )
137{
138  UInt code;
139  UInt interRPSPred;
140  if (idx > 0)
141  {
142    READ_FLAG(interRPSPred, "inter_ref_pic_set_prediction_flag");  rps->setInterRPSPrediction(interRPSPred);
143  }
144  else
145  {
146    interRPSPred = false;
147    rps->setInterRPSPrediction(false);
148  }
149
150  if (interRPSPred)
151  {
152    UInt bit;
153    if(idx == sps->getRPSList()->getNumberOfReferencePictureSets())
154    {
155      READ_UVLC(code, "delta_idx_minus1" ); // delta index of the Reference Picture Set used for prediction minus 1
156    }
157    else
158    {
159      code = 0;
160    }
161    assert(code <= idx-1); // delta_idx_minus1 shall not be larger than idx-1, otherwise we will predict from a negative row position that does not exist. When idx equals 0 there is no legal value and interRPSPred must be zero. See J0185-r2
162    Int rIdx =  idx - 1 - code;
163    assert (rIdx <= idx-1 && rIdx >= 0); // Made assert tighter; if rIdx = idx then prediction is done from itself. rIdx must belong to range 0, idx-1, inclusive, see J0185-r2
164    TComReferencePictureSet*   rpsRef = sps->getRPSList()->getReferencePictureSet(rIdx);
165    Int k = 0, k0 = 0, k1 = 0;
166    READ_CODE(1, bit, "delta_rps_sign"); // delta_RPS_sign
167    READ_UVLC(code, "abs_delta_rps_minus1");  // absolute delta RPS minus 1
168    Int deltaRPS = (1 - 2 * bit) * (code + 1); // delta_RPS
169    for(Int j=0 ; j <= rpsRef->getNumberOfPictures(); j++)
170    {
171      READ_CODE(1, bit, "used_by_curr_pic_flag" ); //first bit is "1" if Idc is 1
172      Int refIdc = bit;
173      if (refIdc == 0)
174      {
175        READ_CODE(1, bit, "use_delta_flag" ); //second bit is "1" if Idc is 2, "0" otherwise.
176        refIdc = bit<<1; //second bit is "1" if refIdc is 2, "0" if refIdc = 0.
177      }
178      if (refIdc == 1 || refIdc == 2)
179      {
180        Int deltaPOC = deltaRPS + ((j < rpsRef->getNumberOfPictures())? rpsRef->getDeltaPOC(j) : 0);
181        rps->setDeltaPOC(k, deltaPOC);
182        rps->setUsed(k, (refIdc == 1));
183
184        if (deltaPOC < 0)
185        {
186          k0++;
187        }
188        else
189        {
190          k1++;
191        }
192        k++;
193      }
194      rps->setRefIdc(j,refIdc);
195    }
196    rps->setNumRefIdc(rpsRef->getNumberOfPictures()+1);
197    rps->setNumberOfPictures(k);
198    rps->setNumberOfNegativePictures(k0);
199    rps->setNumberOfPositivePictures(k1);
200    rps->sortDeltaPOC();
201  }
202  else
203  {
204    READ_UVLC(code, "num_negative_pics");           rps->setNumberOfNegativePictures(code);
205    READ_UVLC(code, "num_positive_pics");           rps->setNumberOfPositivePictures(code);
206    Int prev = 0;
207    Int poc;
208    for(Int j=0 ; j < rps->getNumberOfNegativePictures(); j++)
209    {
210      READ_UVLC(code, "delta_poc_s0_minus1");
211      poc = prev-code-1;
212      prev = poc;
213      rps->setDeltaPOC(j,poc);
214      READ_FLAG(code, "used_by_curr_pic_s0_flag");  rps->setUsed(j,code);
215    }
216    prev = 0;
217    for(Int j=rps->getNumberOfNegativePictures(); j < rps->getNumberOfNegativePictures()+rps->getNumberOfPositivePictures(); j++)
218    {
219      READ_UVLC(code, "delta_poc_s1_minus1");
220      poc = prev+code+1;
221      prev = poc;
222      rps->setDeltaPOC(j,poc);
223      READ_FLAG(code, "used_by_curr_pic_s1_flag");  rps->setUsed(j,code);
224    }
225    rps->setNumberOfPictures(rps->getNumberOfNegativePictures()+rps->getNumberOfPositivePictures());
226  }
227#if PRINT_RPS_INFO
228  rps->printDeltaPOC();
229#endif
230}
231#endif
232
233Void TDecCavlc::parsePPS(TComPPS* pcPPS)
234{
235#if ENC_DEC_TRACE
236#if NH_MV_ENC_DEC_TRAC
237  tracePSHeader( "PPS", pcPPS->getLayerId() ); 
238#else
239  xTracePPSHeader ();
240#endif
241#endif
242  UInt  uiCode;
243
244  Int   iCode;
245
246  READ_UVLC( uiCode, "pps_pic_parameter_set_id");
247  assert(uiCode <= 63);
248  pcPPS->setPPSId (uiCode);
249
250  READ_UVLC( uiCode, "pps_seq_parameter_set_id");
251  assert(uiCode <= 15);
252  pcPPS->setSPSId (uiCode);
253
254  READ_FLAG( uiCode, "dependent_slice_segments_enabled_flag"    );    pcPPS->setDependentSliceSegmentsEnabledFlag   ( uiCode == 1 );
255
256  READ_FLAG( uiCode, "output_flag_present_flag" );                    pcPPS->setOutputFlagPresentFlag( uiCode==1 );
257
258  READ_CODE(3, uiCode, "num_extra_slice_header_bits");                pcPPS->setNumExtraSliceHeaderBits(uiCode);
259
260  READ_FLAG ( uiCode, "sign_data_hiding_flag" ); pcPPS->setSignHideFlag( uiCode );
261
262  READ_FLAG( uiCode,   "cabac_init_present_flag" );            pcPPS->setCabacInitPresentFlag( uiCode ? true : false );
263
264  READ_UVLC(uiCode, "num_ref_idx_l0_default_active_minus1");
265  assert(uiCode <= 14);
266  pcPPS->setNumRefIdxL0DefaultActive(uiCode+1);
267
268  READ_UVLC(uiCode, "num_ref_idx_l1_default_active_minus1");
269  assert(uiCode <= 14);
270  pcPPS->setNumRefIdxL1DefaultActive(uiCode+1);
271
272  READ_SVLC(iCode, "init_qp_minus26" );                            pcPPS->setPicInitQPMinus26(iCode);
273  READ_FLAG( uiCode, "constrained_intra_pred_flag" );              pcPPS->setConstrainedIntraPred( uiCode ? true : false );
274  READ_FLAG( uiCode, "transform_skip_enabled_flag" );
275  pcPPS->setUseTransformSkip ( uiCode ? true : false );
276
277  READ_FLAG( uiCode, "cu_qp_delta_enabled_flag" );            pcPPS->setUseDQP( uiCode ? true : false );
278  if( pcPPS->getUseDQP() )
279  {
280    READ_UVLC( uiCode, "diff_cu_qp_delta_depth" );
281    pcPPS->setMaxCuDQPDepth( uiCode );
282  }
283  else
284  {
285    pcPPS->setMaxCuDQPDepth( 0 );
286  }
287  READ_SVLC( iCode, "pps_cb_qp_offset");
288  pcPPS->setQpOffset(COMPONENT_Cb, iCode);
289  assert( pcPPS->getQpOffset(COMPONENT_Cb) >= -12 );
290  assert( pcPPS->getQpOffset(COMPONENT_Cb) <=  12 );
291
292  READ_SVLC( iCode, "pps_cr_qp_offset");
293  pcPPS->setQpOffset(COMPONENT_Cr, iCode);
294  assert( pcPPS->getQpOffset(COMPONENT_Cr) >= -12 );
295  assert( pcPPS->getQpOffset(COMPONENT_Cr) <=  12 );
296
297  assert(MAX_NUM_COMPONENT<=3);
298
299  READ_FLAG( uiCode, "pps_slice_chroma_qp_offsets_present_flag" );
300  pcPPS->setSliceChromaQpFlag( uiCode ? true : false );
301
302  READ_FLAG( uiCode, "weighted_pred_flag" );          // Use of Weighting Prediction (P_SLICE)
303  pcPPS->setUseWP( uiCode==1 );
304  READ_FLAG( uiCode, "weighted_bipred_flag" );         // Use of Bi-Directional Weighting Prediction (B_SLICE)
305  pcPPS->setWPBiPred( uiCode==1 );
306
307  READ_FLAG( uiCode, "transquant_bypass_enable_flag");
308  pcPPS->setTransquantBypassEnableFlag(uiCode ? true : false);
309  READ_FLAG( uiCode, "tiles_enabled_flag"               );    pcPPS->setTilesEnabledFlag            ( uiCode == 1 );
310  READ_FLAG( uiCode, "entropy_coding_sync_enabled_flag" );    pcPPS->setEntropyCodingSyncEnabledFlag( uiCode == 1 );
311
312  if( pcPPS->getTilesEnabledFlag() )
313  {
314    READ_UVLC ( uiCode, "num_tile_columns_minus1" );                pcPPS->setNumTileColumnsMinus1( uiCode ); 
315    READ_UVLC ( uiCode, "num_tile_rows_minus1" );                   pcPPS->setNumTileRowsMinus1( uiCode ); 
316    READ_FLAG ( uiCode, "uniform_spacing_flag" );                   pcPPS->setTileUniformSpacingFlag( uiCode == 1 );
317
318    const UInt tileColumnsMinus1 = pcPPS->getNumTileColumnsMinus1();
319    const UInt tileRowsMinus1    = pcPPS->getNumTileRowsMinus1();
320 
321    if ( !pcPPS->getTileUniformSpacingFlag())
322    {
323      if (tileColumnsMinus1 > 0)
324      {
325        std::vector<Int> columnWidth(tileColumnsMinus1);
326        for(UInt i = 0; i < tileColumnsMinus1; i++)
327        { 
328          READ_UVLC( uiCode, "column_width_minus1" ); 
329          columnWidth[i] = uiCode+1;
330        }
331        pcPPS->setTileColumnWidth(columnWidth);
332      }
333
334      if (tileRowsMinus1 > 0)
335      {
336        std::vector<Int> rowHeight (tileRowsMinus1);
337        for(UInt i = 0; i < tileRowsMinus1; i++)
338        {
339          READ_UVLC( uiCode, "row_height_minus1" );
340          rowHeight[i] = uiCode + 1;
341        }
342        pcPPS->setTileRowHeight(rowHeight);
343      }
344    }
345
346    if ((tileColumnsMinus1 + tileRowsMinus1) != 0)
347    {
348      READ_FLAG ( uiCode, "loop_filter_across_tiles_enabled_flag" );   pcPPS->setLoopFilterAcrossTilesEnabledFlag( uiCode ? true : false );
349    }
350  }
351  READ_FLAG( uiCode, "pps_loop_filter_across_slices_enabled_flag" );   pcPPS->setLoopFilterAcrossSlicesEnabledFlag( uiCode ? true : false );
352  READ_FLAG( uiCode, "deblocking_filter_control_present_flag" );       pcPPS->setDeblockingFilterControlPresentFlag( uiCode ? true : false );
353  if(pcPPS->getDeblockingFilterControlPresentFlag())
354  {
355    READ_FLAG( uiCode, "deblocking_filter_override_enabled_flag" );    pcPPS->setDeblockingFilterOverrideEnabledFlag( uiCode ? true : false );
356    READ_FLAG( uiCode, "pps_disable_deblocking_filter_flag" );         pcPPS->setPicDisableDeblockingFilterFlag(uiCode ? true : false );
357    if(!pcPPS->getPicDisableDeblockingFilterFlag())
358    {
359      READ_SVLC ( iCode, "pps_beta_offset_div2" );                     pcPPS->setDeblockingFilterBetaOffsetDiv2( iCode );
360      READ_SVLC ( iCode, "pps_tc_offset_div2" );                       pcPPS->setDeblockingFilterTcOffsetDiv2( iCode );
361    }
362  }
363  READ_FLAG( uiCode, "pps_scaling_list_data_present_flag" );           pcPPS->setScalingListPresentFlag( uiCode ? true : false );
364  if(pcPPS->getScalingListPresentFlag ())
365  {
366    parseScalingList( &(pcPPS->getScalingList()) );
367  }
368
369  READ_FLAG( uiCode, "lists_modification_present_flag");
370  pcPPS->setListsModificationPresentFlag(uiCode);
371
372  READ_UVLC( uiCode, "log2_parallel_merge_level_minus2");
373  pcPPS->setLog2ParallelMergeLevelMinus2 (uiCode);
374
375  READ_FLAG( uiCode, "slice_segment_header_extension_present_flag");
376  pcPPS->setSliceHeaderExtensionPresentFlag(uiCode);
377
378  READ_FLAG( uiCode, "pps_extension_present_flag");
379  if (uiCode)
380  {
381#if NH_MV
382    READ_FLAG( uiCode, "pps_range_extensions_flag" ); pcPPS->setPpsRangeExtensionsFlag( uiCode == 1 );
383    READ_FLAG( uiCode, "pps_multilayer_extension_flag" ); pcPPS->setPpsMultilayerExtensionFlag( uiCode == 1 );
384    READ_FLAG( uiCode, "pps_3d_extension_flag" ); pcPPS->setPps3dExtensionFlag( uiCode == 1 );
385    READ_CODE( 5, uiCode, "pps_extension_5bits" ); pcPPS->setPpsExtension5bits( uiCode );
386    if ( pcPPS->getPpsRangeExtensionsFlag() )
387    { 
388      TComPPSRExt &ppsRangeExtension = pcPPS->getPpsRangeExtension();     
389
390      if (pcPPS->getUseTransformSkip())
391      {
392        READ_UVLC( uiCode, "log2_max_transform_skip_block_size_minus2");
393        ppsRangeExtension.setLog2MaxTransformSkipBlockSize(uiCode+2);
394      }
395
396      READ_FLAG( uiCode, "cross_component_prediction_enabled_flag");
397      ppsRangeExtension.setCrossComponentPredictionEnabledFlag(uiCode != 0);
398
399      READ_FLAG( uiCode, "chroma_qp_offset_list_enabled_flag");
400      if (uiCode == 0)
401      {
402        ppsRangeExtension.clearChromaQpOffsetList();
403        ppsRangeExtension.setDiffCuChromaQpOffsetDepth(0);
404      }
405      else
406      {
407        READ_UVLC(uiCode, "diff_cu_chroma_qp_offset_depth"); ppsRangeExtension.setDiffCuChromaQpOffsetDepth(uiCode);
408        UInt tableSizeMinus1 = 0;
409        READ_UVLC(tableSizeMinus1, "chroma_qp_offset_list_len_minus1");
410        assert(tableSizeMinus1 < MAX_QP_OFFSET_LIST_SIZE);
411
412        for (Int cuChromaQpOffsetIdx = 0; cuChromaQpOffsetIdx <= (tableSizeMinus1); cuChromaQpOffsetIdx++)
413        {
414          Int cbOffset;
415          Int crOffset;
416          READ_SVLC(cbOffset, "cb_qp_offset_list[i]");
417          assert(cbOffset >= -12 && cbOffset <= 12);
418          READ_SVLC(crOffset, "cr_qp_offset_list[i]");
419          assert(crOffset >= -12 && crOffset <= 12);
420          // table uses +1 for index (see comment inside the function)
421          ppsRangeExtension.setChromaQpOffsetListEntry(cuChromaQpOffsetIdx+1, cbOffset, crOffset);
422        }
423        assert(ppsRangeExtension.getChromaQpOffsetListLen() == tableSizeMinus1 + 1);
424      }
425
426      READ_UVLC( uiCode, "log2_sao_offset_scale_luma");
427      ppsRangeExtension.setLog2SaoOffsetScale(CHANNEL_TYPE_LUMA, uiCode);
428      READ_UVLC( uiCode, "log2_sao_offset_scale_chroma");
429      ppsRangeExtension.setLog2SaoOffsetScale(CHANNEL_TYPE_CHROMA, uiCode);    }
430
431    if ( pcPPS->getPpsMultilayerExtensionFlag() )
432    { 
433      parsePpsMultilayerExtension( pcPPS ); 
434    }
435
436    if ( pcPPS->getPps3dExtensionFlag() )
437    { 
438    }
439    if ( pcPPS->getPpsExtension5bits() || pcPPS->getPps3dExtensionFlag() )
440    {
441      while ( xMoreRbspData() )
442      {
443        READ_FLAG( uiCode, "pps_extension_data_flag");
444      }
445    }
446
447#else
448#if ENC_DEC_TRACE || RExt__DECODER_DEBUG_BIT_STATISTICS
449    static const TChar *syntaxStrings[]={ "pps_range_extension_flag",
450                                         "pps_multilayer_extension_flag",
451                                         "pps_extension_6bits[0]",
452                                         "pps_extension_6bits[1]",
453                                         "pps_extension_6bits[2]",
454                                         "pps_extension_6bits[3]",
455                                         "pps_extension_6bits[4]",
456                                         "pps_extension_6bits[5]" };
457#endif
458
459    Bool pps_extension_flags[NUM_PPS_EXTENSION_FLAGS];
460    for(Int i=0; i<NUM_PPS_EXTENSION_FLAGS; i++)
461    {
462      READ_FLAG( uiCode, syntaxStrings[i] );
463      pps_extension_flags[i] = uiCode!=0;
464    }
465
466    Bool bSkipTrailingExtensionBits=false;
467    for(Int i=0; i<NUM_PPS_EXTENSION_FLAGS; i++) // loop used so that the order is determined by the enum.
468    {
469      if (pps_extension_flags[i])
470      {
471        switch (PPSExtensionFlagIndex(i))
472        {
473          case PPS_EXT__REXT:
474            {
475              TComPPSRExt &ppsRangeExtension = pcPPS->getPpsRangeExtension();
476            assert(!bSkipTrailingExtensionBits);
477
478            if (pcPPS->getUseTransformSkip())
479            {
480              READ_UVLC( uiCode, "log2_max_transform_skip_block_size_minus2");
481                ppsRangeExtension.setLog2MaxTransformSkipBlockSize(uiCode+2);
482            }
483
484            READ_FLAG( uiCode, "cross_component_prediction_enabled_flag");
485              ppsRangeExtension.setCrossComponentPredictionEnabledFlag(uiCode != 0);
486
487            READ_FLAG( uiCode, "chroma_qp_offset_list_enabled_flag");
488            if (uiCode == 0)
489            {
490                ppsRangeExtension.clearChromaQpOffsetList();
491                ppsRangeExtension.setDiffCuChromaQpOffsetDepth(0);
492            }
493            else
494            {
495                READ_UVLC(uiCode, "diff_cu_chroma_qp_offset_depth"); ppsRangeExtension.setDiffCuChromaQpOffsetDepth(uiCode);
496              UInt tableSizeMinus1 = 0;
497              READ_UVLC(tableSizeMinus1, "chroma_qp_offset_list_len_minus1");
498              assert(tableSizeMinus1 < MAX_QP_OFFSET_LIST_SIZE);
499
500              for (Int cuChromaQpOffsetIdx = 0; cuChromaQpOffsetIdx <= (tableSizeMinus1); cuChromaQpOffsetIdx++)
501              {
502                Int cbOffset;
503                Int crOffset;
504                READ_SVLC(cbOffset, "cb_qp_offset_list[i]");
505                assert(cbOffset >= -12 && cbOffset <= 12); 
506                READ_SVLC(crOffset, "cr_qp_offset_list[i]");
507                assert(crOffset >= -12 && crOffset <= 12);
508                // table uses +1 for index (see comment inside the function)
509                  ppsRangeExtension.setChromaQpOffsetListEntry(cuChromaQpOffsetIdx+1, cbOffset, crOffset);
510              }
511                assert(ppsRangeExtension.getChromaQpOffsetListLen() == tableSizeMinus1 + 1);
512            }
513
514            READ_UVLC( uiCode, "log2_sao_offset_scale_luma");
515              ppsRangeExtension.setLog2SaoOffsetScale(CHANNEL_TYPE_LUMA, uiCode);
516            READ_UVLC( uiCode, "log2_sao_offset_scale_chroma");
517              ppsRangeExtension.setLog2SaoOffsetScale(CHANNEL_TYPE_CHROMA, uiCode);
518            }
519            break;
520          default:
521            bSkipTrailingExtensionBits=true;
522            break;
523        }
524      }
525    }
526    if (bSkipTrailingExtensionBits)
527    {
528      while ( xMoreRbspData() )
529      {
530        READ_FLAG( uiCode, "pps_extension_data_flag");
531      }
532    }
533#endif
534  }
535  xReadRbspTrailingBits();
536}
537
538
539
540Void  TDecCavlc::parseVUI(TComVUI* pcVUI, TComSPS *pcSPS)
541{
542#if ENC_DEC_TRACE
543  fprintf( g_hTrace, "----------- vui_parameters -----------\n");
544#endif
545  UInt  uiCode;
546
547  READ_FLAG(     uiCode, "aspect_ratio_info_present_flag");           pcVUI->setAspectRatioInfoPresentFlag(uiCode);
548  if (pcVUI->getAspectRatioInfoPresentFlag())
549  {
550    READ_CODE(8, uiCode, "aspect_ratio_idc");                         pcVUI->setAspectRatioIdc(uiCode);
551    if (pcVUI->getAspectRatioIdc() == 255)
552    {
553      READ_CODE(16, uiCode, "sar_width");                             pcVUI->setSarWidth(uiCode);
554      READ_CODE(16, uiCode, "sar_height");                              pcVUI->setSarHeight(uiCode);
555    }
556  }
557
558  READ_FLAG(     uiCode, "overscan_info_present_flag");               pcVUI->setOverscanInfoPresentFlag(uiCode);
559  if (pcVUI->getOverscanInfoPresentFlag())
560  {
561    READ_FLAG(   uiCode, "overscan_appropriate_flag");                pcVUI->setOverscanAppropriateFlag(uiCode);
562  }
563
564  READ_FLAG(     uiCode, "video_signal_type_present_flag");           pcVUI->setVideoSignalTypePresentFlag(uiCode);
565#if NH_MV
566  assert( pcSPS->getLayerId() == 0 || !pcVUI->getVideoSignalTypePresentFlag() ); 
567#endif
568  if (pcVUI->getVideoSignalTypePresentFlag())
569  {
570    READ_CODE(3, uiCode, "video_format");                             pcVUI->setVideoFormat(uiCode);
571    READ_FLAG(   uiCode, "video_full_range_flag");                    pcVUI->setVideoFullRangeFlag(uiCode);
572    READ_FLAG(   uiCode, "colour_description_present_flag");          pcVUI->setColourDescriptionPresentFlag(uiCode);
573    if (pcVUI->getColourDescriptionPresentFlag())
574    {
575      READ_CODE(8, uiCode, "colour_primaries");                       pcVUI->setColourPrimaries(uiCode);
576      READ_CODE(8, uiCode, "transfer_characteristics");               pcVUI->setTransferCharacteristics(uiCode);
577      READ_CODE(8, uiCode, "matrix_coeffs");                          pcVUI->setMatrixCoefficients(uiCode);
578    }
579  }
580
581  READ_FLAG(     uiCode, "chroma_loc_info_present_flag");             pcVUI->setChromaLocInfoPresentFlag(uiCode);
582  if (pcVUI->getChromaLocInfoPresentFlag())
583  {
584    READ_UVLC(   uiCode, "chroma_sample_loc_type_top_field" );        pcVUI->setChromaSampleLocTypeTopField(uiCode);
585    READ_UVLC(   uiCode, "chroma_sample_loc_type_bottom_field" );     pcVUI->setChromaSampleLocTypeBottomField(uiCode);
586  }
587
588  READ_FLAG(     uiCode, "neutral_chroma_indication_flag");           pcVUI->setNeutralChromaIndicationFlag(uiCode);
589
590  READ_FLAG(     uiCode, "field_seq_flag");                           pcVUI->setFieldSeqFlag(uiCode);
591
592  READ_FLAG(uiCode, "frame_field_info_present_flag");                 pcVUI->setFrameFieldInfoPresentFlag(uiCode);
593
594  READ_FLAG(     uiCode, "default_display_window_flag");
595  if (uiCode != 0)
596  {
597    Window &defDisp = pcVUI->getDefaultDisplayWindow();
598#if NH_MV
599    defDisp.setScaledFlag( false ); 
600    READ_UVLC(   uiCode, "def_disp_win_left_offset" );                defDisp.setWindowLeftOffset  ( uiCode );
601    READ_UVLC(   uiCode, "def_disp_win_right_offset" );               defDisp.setWindowRightOffset ( uiCode );
602    READ_UVLC(   uiCode, "def_disp_win_top_offset" );                 defDisp.setWindowTopOffset   ( uiCode );
603    READ_UVLC(   uiCode, "def_disp_win_bottom_offset" );              defDisp.setWindowBottomOffset( uiCode );
604#else
605    READ_UVLC(   uiCode, "def_disp_win_left_offset" );                defDisp.setWindowLeftOffset  ( uiCode * TComSPS::getWinUnitX( pcSPS->getChromaFormatIdc()) );
606    READ_UVLC(   uiCode, "def_disp_win_right_offset" );               defDisp.setWindowRightOffset ( uiCode * TComSPS::getWinUnitX( pcSPS->getChromaFormatIdc()) );
607    READ_UVLC(   uiCode, "def_disp_win_top_offset" );                 defDisp.setWindowTopOffset   ( uiCode * TComSPS::getWinUnitY( pcSPS->getChromaFormatIdc()) );
608    READ_UVLC(   uiCode, "def_disp_win_bottom_offset" );              defDisp.setWindowBottomOffset( uiCode * TComSPS::getWinUnitY( pcSPS->getChromaFormatIdc()) );
609#endif
610  }
611
612  TimingInfo *timingInfo = pcVUI->getTimingInfo();
613  READ_FLAG(       uiCode, "vui_timing_info_present_flag");         timingInfo->setTimingInfoPresentFlag      (uiCode ? true : false);
614  if(timingInfo->getTimingInfoPresentFlag())
615  {
616    READ_CODE( 32, uiCode, "vui_num_units_in_tick");                timingInfo->setNumUnitsInTick             (uiCode);
617    READ_CODE( 32, uiCode, "vui_time_scale");                       timingInfo->setTimeScale                  (uiCode);
618    READ_FLAG(     uiCode, "vui_poc_proportional_to_timing_flag");  timingInfo->setPocProportionalToTimingFlag(uiCode ? true : false);
619    if(timingInfo->getPocProportionalToTimingFlag())
620    {
621      READ_UVLC(   uiCode, "vui_num_ticks_poc_diff_one_minus1");    timingInfo->setNumTicksPocDiffOneMinus1   (uiCode);
622    }
623
624    READ_FLAG(     uiCode, "vui_hrd_parameters_present_flag");        pcVUI->setHrdParametersPresentFlag(uiCode);
625    if( pcVUI->getHrdParametersPresentFlag() )
626    {
627      parseHrdParameters( pcVUI->getHrdParameters(), 1, pcSPS->getMaxTLayers() - 1 );
628    }
629  }
630
631  READ_FLAG(     uiCode, "bitstream_restriction_flag");               pcVUI->setBitstreamRestrictionFlag(uiCode);
632  if (pcVUI->getBitstreamRestrictionFlag())
633  {
634    READ_FLAG(   uiCode, "tiles_fixed_structure_flag");               pcVUI->setTilesFixedStructureFlag(uiCode);
635    READ_FLAG(   uiCode, "motion_vectors_over_pic_boundaries_flag");  pcVUI->setMotionVectorsOverPicBoundariesFlag(uiCode);
636    READ_FLAG(   uiCode, "restricted_ref_pic_lists_flag");            pcVUI->setRestrictedRefPicListsFlag(uiCode);
637    READ_UVLC(   uiCode, "min_spatial_segmentation_idc");             pcVUI->setMinSpatialSegmentationIdc(uiCode);
638    assert(uiCode < 4096);
639    READ_UVLC(   uiCode, "max_bytes_per_pic_denom" );                 pcVUI->setMaxBytesPerPicDenom(uiCode);
640    READ_UVLC(   uiCode, "max_bits_per_min_cu_denom" );               pcVUI->setMaxBitsPerMinCuDenom(uiCode);
641    READ_UVLC(   uiCode, "log2_max_mv_length_horizontal" );           pcVUI->setLog2MaxMvLengthHorizontal(uiCode);
642    READ_UVLC(   uiCode, "log2_max_mv_length_vertical" );             pcVUI->setLog2MaxMvLengthVertical(uiCode);
643  }
644}
645
646Void TDecCavlc::parseHrdParameters(TComHRD *hrd, Bool commonInfPresentFlag, UInt maxNumSubLayersMinus1)
647{
648  UInt  uiCode;
649  if( commonInfPresentFlag )
650  {
651    READ_FLAG( uiCode, "nal_hrd_parameters_present_flag" );           hrd->setNalHrdParametersPresentFlag( uiCode == 1 ? true : false );
652    READ_FLAG( uiCode, "vcl_hrd_parameters_present_flag" );           hrd->setVclHrdParametersPresentFlag( uiCode == 1 ? true : false );
653    if( hrd->getNalHrdParametersPresentFlag() || hrd->getVclHrdParametersPresentFlag() )
654    {
655      READ_FLAG( uiCode, "sub_pic_hrd_params_present_flag" );         hrd->setSubPicCpbParamsPresentFlag( uiCode == 1 ? true : false );
656      if( hrd->getSubPicCpbParamsPresentFlag() )
657      {
658        READ_CODE( 8, uiCode, "tick_divisor_minus2" );                hrd->setTickDivisorMinus2( uiCode );
659        READ_CODE( 5, uiCode, "du_cpb_removal_delay_increment_length_minus1" ); hrd->setDuCpbRemovalDelayLengthMinus1( uiCode );
660        READ_FLAG( uiCode, "sub_pic_cpb_params_in_pic_timing_sei_flag" ); hrd->setSubPicCpbParamsInPicTimingSEIFlag( uiCode == 1 ? true : false );
661        READ_CODE( 5, uiCode, "dpb_output_delay_du_length_minus1"  ); hrd->setDpbOutputDelayDuLengthMinus1( uiCode );
662      }
663      READ_CODE( 4, uiCode, "bit_rate_scale" );                       hrd->setBitRateScale( uiCode );
664      READ_CODE( 4, uiCode, "cpb_size_scale" );                       hrd->setCpbSizeScale( uiCode );
665      if( hrd->getSubPicCpbParamsPresentFlag() )
666      {
667        READ_CODE( 4, uiCode, "cpb_size_du_scale" );                  hrd->setDuCpbSizeScale( uiCode );
668      }
669      READ_CODE( 5, uiCode, "initial_cpb_removal_delay_length_minus1" ); hrd->setInitialCpbRemovalDelayLengthMinus1( uiCode );
670      READ_CODE( 5, uiCode, "au_cpb_removal_delay_length_minus1" );      hrd->setCpbRemovalDelayLengthMinus1( uiCode );
671      READ_CODE( 5, uiCode, "dpb_output_delay_length_minus1" );       hrd->setDpbOutputDelayLengthMinus1( uiCode );
672    }
673  }
674  Int i, j, nalOrVcl;
675  for( i = 0; i <= maxNumSubLayersMinus1; i ++ )
676  {
677    READ_FLAG( uiCode, "fixed_pic_rate_general_flag" );                     hrd->setFixedPicRateFlag( i, uiCode == 1 ? true : false  );
678    if( !hrd->getFixedPicRateFlag( i ) )
679    {
680      READ_FLAG( uiCode, "fixed_pic_rate_within_cvs_flag" );                hrd->setFixedPicRateWithinCvsFlag( i, uiCode == 1 ? true : false  );
681    }
682    else
683    {
684      hrd->setFixedPicRateWithinCvsFlag( i, true );
685    }
686
687    hrd->setLowDelayHrdFlag( i, 0 ); // Infered to be 0 when not present
688    hrd->setCpbCntMinus1   ( i, 0 ); // Infered to be 0 when not present
689
690    if( hrd->getFixedPicRateWithinCvsFlag( i ) )
691    {
692      READ_UVLC( uiCode, "elemental_duration_in_tc_minus1" );             hrd->setPicDurationInTcMinus1( i, uiCode );
693    }
694    else
695    {
696      READ_FLAG( uiCode, "low_delay_hrd_flag" );                      hrd->setLowDelayHrdFlag( i, uiCode == 1 ? true : false  );
697    }
698    if (!hrd->getLowDelayHrdFlag( i ))
699    {
700      READ_UVLC( uiCode, "cpb_cnt_minus1" );                          hrd->setCpbCntMinus1( i, uiCode );
701    }
702
703    for( nalOrVcl = 0; nalOrVcl < 2; nalOrVcl ++ )
704    {
705      if( ( ( nalOrVcl == 0 ) && ( hrd->getNalHrdParametersPresentFlag() ) ) ||
706          ( ( nalOrVcl == 1 ) && ( hrd->getVclHrdParametersPresentFlag() ) ) )
707      {
708        for( j = 0; j <= ( hrd->getCpbCntMinus1( i ) ); j ++ )
709        {
710          READ_UVLC( uiCode, "bit_rate_value_minus1" );             hrd->setBitRateValueMinus1( i, j, nalOrVcl, uiCode );
711          READ_UVLC( uiCode, "cpb_size_value_minus1" );             hrd->setCpbSizeValueMinus1( i, j, nalOrVcl, uiCode );
712          if( hrd->getSubPicCpbParamsPresentFlag() )
713          {
714            READ_UVLC( uiCode, "cpb_size_du_value_minus1" );       hrd->setDuCpbSizeValueMinus1( i, j, nalOrVcl, uiCode );
715            READ_UVLC( uiCode, "bit_rate_du_value_minus1" );       hrd->setDuBitRateValueMinus1( i, j, nalOrVcl, uiCode );
716          }
717          READ_FLAG( uiCode, "cbr_flag" );                          hrd->setCbrFlag( i, j, nalOrVcl, uiCode == 1 ? true : false  );
718        }
719      }
720    }
721  }
722}
723
724Void TDecCavlc::parseSPS(TComSPS* pcSPS)
725{
726#if ENC_DEC_TRACE
727#if NH_MV_ENC_DEC_TRAC
728  tracePSHeader( "SPS", pcSPS->getLayerId() ); 
729#else
730  xTraceSPSHeader ();
731#endif
732#endif
733
734  UInt  uiCode;
735  READ_CODE( 4,  uiCode, "sps_video_parameter_set_id");          pcSPS->setVPSId        ( uiCode );
736
737#if NH_MV
738  if ( pcSPS->getLayerId() == 0 )
739  {
740#endif
741  READ_CODE( 3,  uiCode, "sps_max_sub_layers_minus1" );          pcSPS->setMaxTLayers   ( uiCode+1 );
742  assert(uiCode <= 6);
743#if NH_MV
744  }
745  else
746  {
747    READ_CODE( 3, uiCode, "sps_ext_or_max_sub_layers_minus1" ); pcSPS->setSpsExtOrMaxSubLayersMinus1( uiCode );   
748    pcSPS->inferSpsMaxSubLayersMinus1( false, NULL );
749  }
750  if ( !pcSPS->getMultiLayerExtSpsFlag() )
751  {
752#endif
753
754  READ_FLAG( uiCode, "sps_temporal_id_nesting_flag" );           pcSPS->setTemporalIdNestingFlag ( uiCode > 0 ? true : false );
755  if ( pcSPS->getMaxTLayers() == 1 )
756  {
757    // sps_temporal_id_nesting_flag must be 1 when sps_max_sub_layers_minus1 is 0
758    assert( uiCode == 1 );
759  }
760
761  parsePTL(pcSPS->getPTL(), 1, pcSPS->getMaxTLayers() - 1);
762#if NH_MV
763    pcSPS->getPTL()->inferGeneralValues ( true, 0, NULL ); 
764    pcSPS->getPTL()->inferSubLayerValues( pcSPS->getSpsMaxSubLayersMinus1(), 0, NULL );
765  }
766#endif
767  READ_UVLC(     uiCode, "sps_seq_parameter_set_id" );           pcSPS->setSPSId( uiCode );
768  assert(uiCode <= 15);
769#if NH_MV
770  if ( pcSPS->getMultiLayerExtSpsFlag() )
771  {
772    READ_FLAG( uiCode, "update_rep_format_flag" );               pcSPS->setUpdateRepFormatFlag( uiCode == 1 );
773    if ( pcSPS->getUpdateRepFormatFlag() )
774    { 
775      READ_CODE( 8, uiCode, "sps_rep_format_idx" );                pcSPS->setSpsRepFormatIdx( uiCode );
776    }
777  }
778  else
779  {
780#endif
781
782  READ_UVLC(     uiCode, "chroma_format_idc" );                  pcSPS->setChromaFormatIdc( ChromaFormat(uiCode) );
783  assert(uiCode <= 3);
784  if( pcSPS->getChromaFormatIdc() == CHROMA_444 )
785  {
786    READ_FLAG(     uiCode, "separate_colour_plane_flag");        assert(uiCode == 0);
787  }
788
789  READ_UVLC (    uiCode, "pic_width_in_luma_samples" );          pcSPS->setPicWidthInLumaSamples ( uiCode    );
790  READ_UVLC (    uiCode, "pic_height_in_luma_samples" );         pcSPS->setPicHeightInLumaSamples( uiCode    );
791  READ_FLAG(     uiCode, "conformance_window_flag");
792  if (uiCode != 0)
793  {
794    Window &conf = pcSPS->getConformanceWindow();
795#if NH_MV
796    // Needs to be scaled later, when ChromaFormatIdc is known.
797    conf.setScaledFlag( false ); 
798    READ_UVLC(   uiCode, "conf_win_left_offset" );               conf.setWindowLeftOffset  ( uiCode  );
799    READ_UVLC(   uiCode, "conf_win_right_offset" );              conf.setWindowRightOffset ( uiCode  );
800    READ_UVLC(   uiCode, "conf_win_top_offset" );                conf.setWindowTopOffset   ( uiCode  );
801    READ_UVLC(   uiCode, "conf_win_bottom_offset" );             conf.setWindowBottomOffset( uiCode  );   
802  }
803#else
804    READ_UVLC(   uiCode, "conf_win_left_offset" );               conf.setWindowLeftOffset  ( uiCode * TComSPS::getWinUnitX( pcSPS->getChromaFormatIdc() ) );
805    READ_UVLC(   uiCode, "conf_win_right_offset" );              conf.setWindowRightOffset ( uiCode * TComSPS::getWinUnitX( pcSPS->getChromaFormatIdc() ) );
806    READ_UVLC(   uiCode, "conf_win_top_offset" );                conf.setWindowTopOffset   ( uiCode * TComSPS::getWinUnitY( pcSPS->getChromaFormatIdc() ) );
807    READ_UVLC(   uiCode, "conf_win_bottom_offset" );             conf.setWindowBottomOffset( uiCode * TComSPS::getWinUnitY( pcSPS->getChromaFormatIdc() ) );
808#endif
809  }
810
811#if NH_MV
812  if ( !pcSPS->getMultiLayerExtSpsFlag() )
813  { 
814#endif
815
816  READ_UVLC(     uiCode, "bit_depth_luma_minus8" );
817#if O0043_BEST_EFFORT_DECODING
818  pcSPS->setStreamBitDepth(CHANNEL_TYPE_LUMA, 8 + uiCode);
819  const UInt forceDecodeBitDepth = pcSPS->getForceDecodeBitDepth();
820  if (forceDecodeBitDepth != 0)
821  {
822    uiCode = forceDecodeBitDepth - 8;
823  }
824#endif
825  assert(uiCode <= 8);
826  pcSPS->setBitDepth(CHANNEL_TYPE_LUMA, 8 + uiCode);
827
828#if O0043_BEST_EFFORT_DECODING
829  pcSPS->setQpBDOffset(CHANNEL_TYPE_LUMA, (Int) (6*(pcSPS->getStreamBitDepth(CHANNEL_TYPE_LUMA)-8)) );
830#else
831  pcSPS->setQpBDOffset(CHANNEL_TYPE_LUMA, (Int) (6*uiCode) );
832#endif
833
834  READ_UVLC( uiCode,    "bit_depth_chroma_minus8" );
835#if O0043_BEST_EFFORT_DECODING
836  pcSPS->setStreamBitDepth(CHANNEL_TYPE_CHROMA, 8 + uiCode);
837  if (forceDecodeBitDepth != 0)
838  {
839    uiCode = forceDecodeBitDepth - 8;
840  }
841#endif
842  assert(uiCode <= 8);
843  pcSPS->setBitDepth(CHANNEL_TYPE_CHROMA, 8 + uiCode);
844#if O0043_BEST_EFFORT_DECODING
845  pcSPS->setQpBDOffset(CHANNEL_TYPE_CHROMA,  (Int) (6*(pcSPS->getStreamBitDepth(CHANNEL_TYPE_CHROMA)-8)) );
846#else
847  pcSPS->setQpBDOffset(CHANNEL_TYPE_CHROMA,  (Int) (6*uiCode) );
848#endif
849#if NH_MV
850  }
851#endif
852
853  READ_UVLC( uiCode,    "log2_max_pic_order_cnt_lsb_minus4" );   pcSPS->setBitsForPOC( 4 + uiCode );
854  assert(uiCode <= 12);
855
856#if NH_MV
857  if ( !pcSPS->getMultiLayerExtSpsFlag()) 
858  { 
859#endif
860  UInt subLayerOrderingInfoPresentFlag;
861  READ_FLAG(subLayerOrderingInfoPresentFlag, "sps_sub_layer_ordering_info_present_flag");
862
863  for(UInt i=0; i <= pcSPS->getMaxTLayers()-1; i++)
864  {
865    READ_UVLC ( uiCode, "sps_max_dec_pic_buffering_minus1[i]");
866    pcSPS->setMaxDecPicBuffering( uiCode + 1, i);
867    READ_UVLC ( uiCode, "sps_max_num_reorder_pics[i]" );
868    pcSPS->setNumReorderPics(uiCode, i);
869    READ_UVLC ( uiCode, "sps_max_latency_increase_plus1[i]");
870#if NH_MV
871    pcSPS->setSpsMaxLatencyIncreasePlus1( uiCode, i );
872#else
873    pcSPS->setMaxLatencyIncreasePlus1( uiCode, i );
874#endif
875
876    if (!subLayerOrderingInfoPresentFlag)
877    {
878      for (i++; i <= pcSPS->getMaxTLayers()-1; i++)
879      {
880        pcSPS->setMaxDecPicBuffering(pcSPS->getMaxDecPicBuffering(0), i);
881        pcSPS->setNumReorderPics(pcSPS->getNumReorderPics(0), i);
882#if NH_MV
883        pcSPS->setSpsMaxLatencyIncreasePlus1(pcSPS->getSpsMaxLatencyIncreasePlus1(0), i);
884#else
885        pcSPS->setMaxLatencyIncreasePlus1(pcSPS->getMaxLatencyIncreasePlus1(0), i);
886#endif
887      }
888      break;
889    }
890  }
891#if NH_MV
892  }
893#endif
894
895  READ_UVLC( uiCode, "log2_min_luma_coding_block_size_minus3" );
896  Int log2MinCUSize = uiCode + 3;
897  pcSPS->setLog2MinCodingBlockSize(log2MinCUSize);
898  READ_UVLC( uiCode, "log2_diff_max_min_luma_coding_block_size" );
899  pcSPS->setLog2DiffMaxMinCodingBlockSize(uiCode);
900 
901  if (pcSPS->getPTL()->getGeneralPTL()->getLevelIdc() >= Level::LEVEL5)
902  {
903    assert(log2MinCUSize + pcSPS->getLog2DiffMaxMinCodingBlockSize() >= 5);
904  }
905 
906  Int maxCUDepthDelta = uiCode;
907  pcSPS->setMaxCUWidth  ( 1<<(log2MinCUSize + maxCUDepthDelta) );
908  pcSPS->setMaxCUHeight ( 1<<(log2MinCUSize + maxCUDepthDelta) );
909  READ_UVLC( uiCode, "log2_min_luma_transform_block_size_minus2" );   pcSPS->setQuadtreeTULog2MinSize( uiCode + 2 );
910
911  READ_UVLC( uiCode, "log2_diff_max_min_luma_transform_block_size" ); pcSPS->setQuadtreeTULog2MaxSize( uiCode + pcSPS->getQuadtreeTULog2MinSize() );
912  pcSPS->setMaxTrSize( 1<<(uiCode + pcSPS->getQuadtreeTULog2MinSize()) );
913
914  READ_UVLC( uiCode, "max_transform_hierarchy_depth_inter" );    pcSPS->setQuadtreeTUMaxDepthInter( uiCode+1 );
915  READ_UVLC( uiCode, "max_transform_hierarchy_depth_intra" );    pcSPS->setQuadtreeTUMaxDepthIntra( uiCode+1 );
916
917  Int addCuDepth = max (0, log2MinCUSize - (Int)pcSPS->getQuadtreeTULog2MinSize() );
918  pcSPS->setMaxTotalCUDepth( maxCUDepthDelta + addCuDepth  + getMaxCUDepthOffset(pcSPS->getChromaFormatIdc(), pcSPS->getQuadtreeTULog2MinSize()) );
919
920  READ_FLAG( uiCode, "scaling_list_enabled_flag" );                 pcSPS->setScalingListFlag ( uiCode );
921  if(pcSPS->getScalingListFlag())
922  {
923#if NH_MV
924    if ( pcSPS->getMultiLayerExtSpsFlag() )
925    {   
926      READ_FLAG( uiCode, "sps_infer_scaling_list_flag" ); pcSPS->setSpsInferScalingListFlag( uiCode == 1 );
927    }
928
929    if ( pcSPS->getSpsInferScalingListFlag() )
930    {
931      READ_CODE( 6, uiCode, "sps_scaling_list_ref_layer_id" ); pcSPS->setSpsScalingListRefLayerId( uiCode );
932    }
933    else
934    {   
935#endif
936    READ_FLAG( uiCode, "sps_scaling_list_data_present_flag" );                 pcSPS->setScalingListPresentFlag ( uiCode );
937    if(pcSPS->getScalingListPresentFlag ())
938    {
939      parseScalingList( &(pcSPS->getScalingList()) );
940    }
941#if NH_MV
942    }
943#endif
944  }
945  READ_FLAG( uiCode, "amp_enabled_flag" );                          pcSPS->setUseAMP( uiCode );
946  READ_FLAG( uiCode, "sample_adaptive_offset_enabled_flag" );       pcSPS->setUseSAO ( uiCode ? true : false );
947
948  READ_FLAG( uiCode, "pcm_enabled_flag" ); pcSPS->setUsePCM( uiCode ? true : false );
949  if( pcSPS->getUsePCM() )
950  {
951    READ_CODE( 4, uiCode, "pcm_sample_bit_depth_luma_minus1" );          pcSPS->setPCMBitDepth    ( CHANNEL_TYPE_LUMA, 1 + uiCode );
952    READ_CODE( 4, uiCode, "pcm_sample_bit_depth_chroma_minus1" );        pcSPS->setPCMBitDepth    ( CHANNEL_TYPE_CHROMA, 1 + uiCode );
953    READ_UVLC( uiCode, "log2_min_pcm_luma_coding_block_size_minus3" );   pcSPS->setPCMLog2MinSize (uiCode+3);
954    READ_UVLC( uiCode, "log2_diff_max_min_pcm_luma_coding_block_size" ); pcSPS->setPCMLog2MaxSize ( uiCode+pcSPS->getPCMLog2MinSize() );
955    READ_FLAG( uiCode, "pcm_loop_filter_disable_flag" );                 pcSPS->setPCMFilterDisableFlag ( uiCode ? true : false );
956  }
957
958  READ_UVLC( uiCode, "num_short_term_ref_pic_sets" );
959
960  assert(uiCode <= 64);
961#if NH_MV
962  pcSPS->setNumShortTermRefPicSets( uiCode ); 
963  pcSPS->initStRefPicSets();
964
965  for(Int i=0; i< pcSPS->getNumShortTermRefPicSets(); i++ )
966  {
967    parseShortTermRefPicSet(pcSPS,pcSPS->getStRefPicSet(i),i);
968  }
969
970  READ_FLAG( uiCode, "long_term_ref_pics_present_flag" );          pcSPS->setLongTermRefPicsPresentFlag(uiCode);
971  if (pcSPS->getLongTermRefPicsPresentFlag() )
972#else
973  pcSPS->createRPSList(uiCode);
974
975  TComRPSList* rpsList = pcSPS->getRPSList();
976  TComReferencePictureSet* rps;
977
978  for(UInt i=0; i< rpsList->getNumberOfReferencePictureSets(); i++)
979  {
980    rps = rpsList->getReferencePictureSet(i);
981    parseShortTermRefPicSet(pcSPS,rps,i);
982  }
983
984  READ_FLAG( uiCode, "long_term_ref_pics_present_flag" );          pcSPS->setLongTermRefsPresent(uiCode);
985  if (pcSPS->getLongTermRefsPresent())
986#endif
987  {
988    READ_UVLC( uiCode, "num_long_term_ref_pics_sps" );
989    pcSPS->setNumLongTermRefPicSPS(uiCode);
990    for (UInt k = 0; k < pcSPS->getNumLongTermRefPicSPS(); k++)
991    {
992      READ_CODE( pcSPS->getBitsForPOC(), uiCode, "lt_ref_pic_poc_lsb_sps" );
993      pcSPS->setLtRefPicPocLsbSps(k, uiCode);
994      READ_FLAG( uiCode,  "used_by_curr_pic_lt_sps_flag[i]");
995      pcSPS->setUsedByCurrPicLtSPSFlag(k, uiCode?1:0);
996    }
997  }
998  READ_FLAG( uiCode, "sps_temporal_mvp_enable_flag" );            pcSPS->setTMVPFlagsPresent(uiCode);
999
1000  READ_FLAG( uiCode, "sps_strong_intra_smoothing_enable_flag" );  pcSPS->setUseStrongIntraSmoothing(uiCode);
1001
1002  READ_FLAG( uiCode, "vui_parameters_present_flag" );             pcSPS->setVuiParametersPresentFlag(uiCode);
1003
1004  if (pcSPS->getVuiParametersPresentFlag())
1005  {
1006    parseVUI(pcSPS->getVuiParameters(), pcSPS);
1007  }
1008
1009  READ_FLAG( uiCode, "sps_extension_present_flag");
1010#if NH_MV
1011  pcSPS->setSpsExtensionPresentFlag( uiCode ); 
1012  if (pcSPS->getSpsExtensionPresentFlag( ) )
1013  { 
1014    READ_FLAG( uiCode, "sps_range_extensions_flag" );     pcSPS->setSpsRangeExtensionsFlag( uiCode == 1 );
1015    READ_FLAG( uiCode, "sps_multilayer_extension_flag" ); pcSPS->setSpsMultilayerExtensionFlag( uiCode == 1 );
1016    READ_FLAG( uiCode   , "sps_3d_extension_flag" );      pcSPS->setSps3dExtensionFlag( uiCode == 1 );
1017    READ_CODE( 5, uiCode, "sps_extension_5bits" )  ;      pcSPS->setSpsExtension5bits( uiCode );
1018
1019    if ( pcSPS->getSpsRangeExtensionsFlag() )
1020    {
1021      TComSPSRExt &spsRangeExtension = pcSPS->getSpsRangeExtension();
1022      READ_FLAG( uiCode, "transform_skip_rotation_enabled_flag");     spsRangeExtension.setTransformSkipRotationEnabledFlag(uiCode != 0);
1023      READ_FLAG( uiCode, "transform_skip_context_enabled_flag");      spsRangeExtension.setTransformSkipContextEnabledFlag (uiCode != 0);
1024      READ_FLAG( uiCode, "implicit_rdpcm_enabled_flag");              spsRangeExtension.setRdpcmEnabledFlag(RDPCM_SIGNAL_IMPLICIT, (uiCode != 0));
1025      READ_FLAG( uiCode, "explicit_rdpcm_enabled_flag");              spsRangeExtension.setRdpcmEnabledFlag(RDPCM_SIGNAL_EXPLICIT, (uiCode != 0));
1026      READ_FLAG( uiCode, "extended_precision_processing_flag");       spsRangeExtension.setExtendedPrecisionProcessingFlag (uiCode != 0);
1027      READ_FLAG( uiCode, "intra_smoothing_disabled_flag");            spsRangeExtension.setIntraSmoothingDisabledFlag      (uiCode != 0);
1028      READ_FLAG( uiCode, "high_precision_offsets_enabled_flag");      spsRangeExtension.setHighPrecisionOffsetsEnabledFlag (uiCode != 0);
1029      READ_FLAG( uiCode, "persistent_rice_adaptation_enabled_flag");  spsRangeExtension.setPersistentRiceAdaptationEnabledFlag (uiCode != 0);
1030      READ_FLAG( uiCode, "cabac_bypass_alignment_enabled_flag");      spsRangeExtension.setCabacBypassAlignmentEnabledFlag  (uiCode != 0);
1031    }
1032
1033    if ( pcSPS->getSpsMultilayerExtensionFlag() )
1034    {
1035      parseSpsMultilayerExtension( pcSPS ); 
1036    }
1037
1038    if ( pcSPS->getSps3dExtensionFlag() )
1039    {
1040    }
1041
1042    if ( pcSPS->getSpsExtension5bits() || pcSPS->getSps3dExtensionFlag() )
1043    { 
1044      while ( xMoreRbspData() )
1045      {
1046        READ_FLAG( uiCode, "sps_extension_data_flag");
1047      }
1048    }
1049  }
1050#else
1051  if (uiCode)
1052  {
1053
1054#if ENC_DEC_TRACE || RExt__DECODER_DEBUG_BIT_STATISTICS
1055    static const TChar *syntaxStrings[]={ "sps_range_extension_flag",
1056                                         "sps_multilayer_extension_flag",
1057                                         "sps_extension_6bits[0]",
1058                                         "sps_extension_6bits[1]",
1059                                         "sps_extension_6bits[2]",
1060                                         "sps_extension_6bits[3]",
1061                                         "sps_extension_6bits[4]",
1062                                         "sps_extension_6bits[5]" };
1063#endif
1064    Bool sps_extension_flags[NUM_SPS_EXTENSION_FLAGS];
1065
1066    for(Int i=0; i<NUM_SPS_EXTENSION_FLAGS; i++)
1067    {
1068      READ_FLAG( uiCode, syntaxStrings[i] );
1069      sps_extension_flags[i] = uiCode!=0;
1070    }
1071
1072    Bool bSkipTrailingExtensionBits=false;
1073    for(Int i=0; i<NUM_SPS_EXTENSION_FLAGS; i++) // loop used so that the order is determined by the enum.
1074    {
1075      if (sps_extension_flags[i])
1076      {
1077        switch (SPSExtensionFlagIndex(i))
1078        {
1079          case SPS_EXT__REXT:
1080            assert(!bSkipTrailingExtensionBits);
1081            {
1082              TComSPSRExt &spsRangeExtension = pcSPS->getSpsRangeExtension();
1083              READ_FLAG( uiCode, "transform_skip_rotation_enabled_flag");     spsRangeExtension.setTransformSkipRotationEnabledFlag(uiCode != 0);
1084              READ_FLAG( uiCode, "transform_skip_context_enabled_flag");      spsRangeExtension.setTransformSkipContextEnabledFlag (uiCode != 0);
1085              READ_FLAG( uiCode, "implicit_rdpcm_enabled_flag");              spsRangeExtension.setRdpcmEnabledFlag(RDPCM_SIGNAL_IMPLICIT, (uiCode != 0));
1086              READ_FLAG( uiCode, "explicit_rdpcm_enabled_flag");              spsRangeExtension.setRdpcmEnabledFlag(RDPCM_SIGNAL_EXPLICIT, (uiCode != 0));
1087              READ_FLAG( uiCode, "extended_precision_processing_flag");       spsRangeExtension.setExtendedPrecisionProcessingFlag (uiCode != 0);
1088              READ_FLAG( uiCode, "intra_smoothing_disabled_flag");            spsRangeExtension.setIntraSmoothingDisabledFlag      (uiCode != 0);
1089              READ_FLAG( uiCode, "high_precision_offsets_enabled_flag");      spsRangeExtension.setHighPrecisionOffsetsEnabledFlag (uiCode != 0);
1090              READ_FLAG( uiCode, "persistent_rice_adaptation_enabled_flag");  spsRangeExtension.setPersistentRiceAdaptationEnabledFlag (uiCode != 0);
1091              READ_FLAG( uiCode, "cabac_bypass_alignment_enabled_flag");      spsRangeExtension.setCabacBypassAlignmentEnabledFlag  (uiCode != 0);
1092            }
1093            break;
1094          default:
1095            bSkipTrailingExtensionBits=true;
1096            break;
1097        }
1098      }
1099    }
1100    if (bSkipTrailingExtensionBits)
1101    {
1102      while ( xMoreRbspData() )
1103      {
1104        READ_FLAG( uiCode, "sps_extension_data_flag");
1105      }
1106    }
1107  }
1108#endif
1109  xReadRbspTrailingBits();
1110}
1111
1112#if NH_MV
1113Void TDecCavlc::parseSpsMultilayerExtension( TComSPS* pcSPS )
1114{
1115  UInt uiCode; 
1116  READ_FLAG( uiCode, "inter_view_mv_vert_constraint_flag" );    pcSPS->setInterViewMvVertConstraintFlag(uiCode == 1 ? true : false); 
1117}
1118
1119
1120Void TDecCavlc::parsePpsMultilayerExtension(TComPPS* pcPPS)
1121{
1122  UInt uiCode = 0; 
1123  READ_FLAG( uiCode, "poc_reset_info_present_flag" ); pcPPS->setPocResetInfoPresentFlag( uiCode == 1 );
1124  READ_FLAG( uiCode, "pps_infer_scaling_list_flag" ); pcPPS->setPpsInferScalingListFlag( uiCode == 1 );
1125  if (pcPPS->getPpsInferScalingListFlag())
1126  {
1127  READ_CODE( 6, uiCode, "pps_scaling_list_ref_layer_id" ); pcPPS->setPpsScalingListRefLayerId( uiCode );
1128  }
1129
1130  UInt numRefLocOffsets;; 
1131  READ_UVLC( numRefLocOffsets, "num_ref_loc_offsets" );
1132
1133  // All of the following stuff is not needed, but allowed to be present.
1134  for (Int i = 0; i < numRefLocOffsets; i++ )
1135  { 
1136    Int   iCode = 0; 
1137    READ_CODE( 6, uiCode, "ref_loc_offset_layer_id" ); 
1138    READ_FLAG( uiCode, "scaled_ref_layer_offset_present_flag" );
1139
1140    if (uiCode)
1141    {   
1142      READ_SVLC( iCode, "scaled_ref_layer_left_offset" );   
1143      READ_SVLC( iCode, "scaled_ref_layer_top_offset" );   
1144      READ_SVLC( iCode, "scaled_ref_layer_right_offset" ); 
1145      READ_SVLC( iCode, "scaled_ref_layer_bottom_offset" ); 
1146    }
1147
1148    READ_FLAG( uiCode, "ref_region_offset_present_flag" ); 
1149    if (uiCode)
1150    {   
1151      READ_SVLC( iCode, "ref_region_left_offset" );     
1152      READ_SVLC( iCode, "ref_region_top_offset" );     
1153      READ_SVLC( iCode, "ref_region_right_offset" );   
1154      READ_SVLC( iCode, "ref_region_bottom_offset" );   
1155    }
1156
1157    READ_FLAG( uiCode, "resample_phase_set_present_flag" );
1158    if (uiCode)
1159    {     
1160      READ_UVLC( uiCode, "phase_hor_luma" );             
1161      READ_UVLC( uiCode, "phase_ver_luma" );             
1162      READ_UVLC( uiCode, "phase_hor_chroma_plus8" );     
1163      READ_UVLC( uiCode, "phase_ver_chroma_plus8" );     
1164    }
1165  }
1166  READ_FLAG( uiCode, "colour_mapping_enabled_flag" );   
1167  // This is required to equal to 0 for Multiview Main profile.
1168  assert( uiCode == 0 ); 
1169}
1170
1171#endif
1172
1173Void TDecCavlc::parseVPS(TComVPS* pcVPS)
1174{
1175#if ENC_DEC_TRACE
1176#if NH_MV_ENC_DEC_TRAC
1177  tracePSHeader( "VPS", getDecTop()->getLayerId() ); 
1178#else
1179  xTraceVPSHeader ();
1180#endif
1181#endif
1182
1183  UInt  uiCode;
1184
1185  READ_CODE( 4,  uiCode,  "vps_video_parameter_set_id" );         pcVPS->setVPSId( uiCode );
1186#if NH_MV
1187  READ_FLAG( uiCode, "vps_base_layer_internal_flag" );            pcVPS->setVpsBaseLayerInternalFlag( uiCode == 1 );
1188  READ_FLAG( uiCode, "vps_base_layer_available_flag" );           pcVPS->setVpsBaseLayerAvailableFlag( uiCode == 1 );
1189#else
1190  READ_FLAG( uiCode,      "vps_base_layer_internal_flag" );       assert(uiCode == 1);
1191  READ_FLAG( uiCode,      "vps_base_layer_available_flag" );      assert(uiCode == 1);
1192#endif
1193#if NH_MV
1194  READ_CODE( 6,  uiCode,  "vps_max_layers_minus1" );              pcVPS->setMaxLayersMinus1( std::min( uiCode, (UInt) ( MAX_NUM_LAYER_IDS-1) )  );
1195#else
1196  READ_CODE( 6,  uiCode,  "vps_max_layers_minus1" );
1197#endif
1198  READ_CODE( 3,  uiCode,  "vps_max_sub_layers_minus1" );          pcVPS->setMaxTLayers( uiCode + 1 );    assert(uiCode+1 <= MAX_TLAYER);
1199  READ_FLAG(     uiCode,  "vps_temporal_id_nesting_flag" );       pcVPS->setTemporalNestingFlag( uiCode ? true:false );
1200  assert (pcVPS->getMaxTLayers()>1||pcVPS->getTemporalNestingFlag());
1201  READ_CODE( 16, uiCode,  "vps_reserved_0xffff_16bits" );         assert(uiCode == 0xffff);
1202  parsePTL ( pcVPS->getPTL(), true, pcVPS->getMaxTLayers()-1);
1203#if NH_MV
1204  pcVPS->getPTL()->inferGeneralValues ( true, 0, NULL );
1205  pcVPS->getPTL()->inferSubLayerValues( pcVPS->getMaxTLayers() - 1, 0, NULL );
1206#endif
1207  UInt subLayerOrderingInfoPresentFlag;
1208  READ_FLAG(subLayerOrderingInfoPresentFlag, "vps_sub_layer_ordering_info_present_flag");
1209  for(UInt i = 0; i <= pcVPS->getMaxTLayers()-1; i++)
1210  {
1211    READ_UVLC( uiCode,  "vps_max_dec_pic_buffering_minus1[i]" );    pcVPS->setMaxDecPicBuffering( uiCode + 1, i );
1212    READ_UVLC( uiCode,  "vps_max_num_reorder_pics[i]" );            pcVPS->setNumReorderPics( uiCode, i );
1213    READ_UVLC( uiCode,  "vps_max_latency_increase_plus1[i]" );      pcVPS->setMaxLatencyIncrease( uiCode, i );
1214
1215    if (!subLayerOrderingInfoPresentFlag)
1216    {
1217      for (i++; i <= pcVPS->getMaxTLayers()-1; i++)
1218      {
1219        pcVPS->setMaxDecPicBuffering(pcVPS->getMaxDecPicBuffering(0), i);
1220        pcVPS->setNumReorderPics(pcVPS->getNumReorderPics(0), i);
1221        pcVPS->setMaxLatencyIncrease(pcVPS->getMaxLatencyIncrease(0), i);
1222      }
1223      break;
1224    }
1225  }
1226
1227  assert( pcVPS->getNumHrdParameters() < MAX_VPS_OP_SETS_PLUS1 );
1228#if NH_MV
1229  assert( pcVPS->getVpsMaxLayerId() < MAX_VPS_NUH_LAYER_ID_PLUS1 );
1230  READ_CODE( 6, uiCode, "vps_max_layer_id" );   pcVPS->setVpsMaxLayerId( uiCode );
1231
1232  READ_UVLC(    uiCode, "vps_num_layer_sets_minus1" );  pcVPS->setVpsNumLayerSetsMinus1( uiCode );
1233  for( UInt opsIdx = 1; opsIdx <= pcVPS->getVpsNumLayerSetsMinus1(); opsIdx ++ )
1234  {
1235    for( UInt i = 0; i <= pcVPS->getVpsMaxLayerId(); i ++ )
1236#else
1237  assert( pcVPS->getMaxNuhReservedZeroLayerId() < MAX_VPS_NUH_RESERVED_ZERO_LAYER_ID_PLUS1 );
1238  READ_CODE( 6, uiCode, "vps_max_layer_id" );                        pcVPS->setMaxNuhReservedZeroLayerId( uiCode );
1239  READ_UVLC(    uiCode, "vps_num_layer_sets_minus1" );               pcVPS->setMaxOpSets( uiCode + 1 );
1240  for( UInt opsIdx = 1; opsIdx <= ( pcVPS->getMaxOpSets() - 1 ); opsIdx ++ )
1241  {
1242    // Operation point set
1243    for( UInt i = 0; i <= pcVPS->getMaxNuhReservedZeroLayerId(); i ++ )
1244#endif
1245    {
1246      READ_FLAG( uiCode, "layer_id_included_flag[opsIdx][i]" );   pcVPS->setLayerIdIncludedFlag( uiCode == 1 ? true : false, opsIdx, i );
1247    }
1248  }
1249#if NH_MV
1250  pcVPS->deriveLayerSetLayerIdList(); 
1251#endif
1252
1253  TimingInfo *timingInfo = pcVPS->getTimingInfo();
1254  READ_FLAG(       uiCode, "vps_timing_info_present_flag");         timingInfo->setTimingInfoPresentFlag      (uiCode ? true : false);
1255  if(timingInfo->getTimingInfoPresentFlag())
1256  {
1257    READ_CODE( 32, uiCode, "vps_num_units_in_tick");                timingInfo->setNumUnitsInTick             (uiCode);
1258    READ_CODE( 32, uiCode, "vps_time_scale");                       timingInfo->setTimeScale                  (uiCode);
1259    READ_FLAG(     uiCode, "vps_poc_proportional_to_timing_flag");  timingInfo->setPocProportionalToTimingFlag(uiCode ? true : false);
1260    if(timingInfo->getPocProportionalToTimingFlag())
1261    {
1262      READ_UVLC(   uiCode, "vps_num_ticks_poc_diff_one_minus1");    timingInfo->setNumTicksPocDiffOneMinus1   (uiCode);
1263    }
1264
1265    READ_UVLC( uiCode, "vps_num_hrd_parameters" );                  pcVPS->setNumHrdParameters( uiCode );
1266
1267    if( pcVPS->getNumHrdParameters() > 0 )
1268    {
1269      pcVPS->createHrdParamBuffer();
1270    }
1271    for( UInt i = 0; i < pcVPS->getNumHrdParameters(); i ++ )
1272    {
1273      READ_UVLC( uiCode, "hrd_layer_set_idx[i]" );                  pcVPS->setHrdOpSetIdx( uiCode, i );
1274      if( i > 0 )
1275      {
1276        READ_FLAG( uiCode, "cprms_present_flag[i]" );               pcVPS->setCprmsPresentFlag( uiCode == 1 ? true : false, i );
1277      }
1278      else
1279      {
1280        pcVPS->setCprmsPresentFlag( true, i );
1281      }
1282
1283      parseHrdParameters(pcVPS->getHrdParameters(i), pcVPS->getCprmsPresentFlag( i ), pcVPS->getMaxTLayers() - 1);
1284    }
1285  }
1286#if NH_MV
1287  READ_FLAG( uiCode,  "vps_extension_flag" );                      pcVPS->setVpsExtensionFlag( uiCode == 1 ? true : false );
1288  if ( pcVPS->getVpsExtensionFlag() )
1289#else
1290  READ_FLAG( uiCode,  "vps_extension_flag" );
1291  if (uiCode)
1292#endif
1293  {
1294#if NH_MV
1295    m_pcBitstream->readOutTrailingBits();
1296    parseVPSExtension( pcVPS );   
1297    READ_FLAG( uiCode,  "vps_extension2_flag" );
1298    if (uiCode)
1299    {
1300#endif 
1301        while ( xMoreRbspData() )
1302        {
1303          READ_FLAG( uiCode, "vps_extension_data_flag");
1304        }
1305#if NH_MV
1306    }
1307#endif
1308  }
1309
1310  xReadRbspTrailingBits();
1311}
1312
1313#if NH_MV
1314Void TDecCavlc::parseVPSExtension( TComVPS* pcVPS )
1315{
1316  UInt uiCode; 
1317
1318  if( pcVPS->getMaxLayersMinus1() > 0  &&  pcVPS->getVpsBaseLayerInternalFlag() )
1319  {
1320    parsePTL( pcVPS->getPTL( 1 ),0, pcVPS->getMaxSubLayersMinus1()  ); 
1321   
1322    pcVPS->getPTL( 1 )->inferGeneralValues ( false, 1, pcVPS->getPTL( 0 ) );
1323    pcVPS->getPTL( 1 )->inferSubLayerValues( pcVPS->getMaxSubLayersMinus1(), 1, pcVPS->getPTL( 0 ) );   
1324  }
1325
1326  READ_FLAG( uiCode, "splitting_flag" );                          pcVPS->setSplittingFlag( uiCode == 1 ? true : false );
1327
1328  for( Int sIdx = 0; sIdx < MAX_NUM_SCALABILITY_TYPES; sIdx++ )
1329  {
1330    READ_FLAG( uiCode,  "scalability_mask_flag[i]" );             pcVPS->setScalabilityMaskFlag( sIdx, uiCode == 1 ? true : false );     
1331  }
1332
1333  for( Int sIdx = 0; sIdx < pcVPS->getNumScalabilityTypes( ) - ( pcVPS->getSplittingFlag() ? 1 : 0 ); sIdx++ )
1334  {
1335    READ_CODE( 3, uiCode, "dimension_id_len_minus1[j]" );       pcVPS->setDimensionIdLen( sIdx, uiCode + 1 );
1336  }
1337
1338  if ( pcVPS->getSplittingFlag() )
1339  {
1340    pcVPS->setDimensionIdLen( pcVPS->getNumScalabilityTypes( ) - 1, pcVPS->inferLastDimsionIdLenMinus1() );       
1341  }
1342
1343  READ_FLAG( uiCode, "vps_nuh_layer_id_present_flag" );           pcVPS->setVpsNuhLayerIdPresentFlag( uiCode == 1 ? true : false );
1344
1345  for( Int i = 1; i <= pcVPS->getMaxLayersMinus1(); i++ )
1346  {
1347    if ( pcVPS->getVpsNuhLayerIdPresentFlag() )
1348    {
1349      READ_CODE( 6, uiCode, "layer_id_in_nuh[i]" );                pcVPS->setLayerIdInNuh( i, uiCode );
1350    }
1351    else
1352    {
1353      pcVPS->setLayerIdInNuh( i, i );; 
1354    }
1355
1356    pcVPS->setLayerIdInVps( pcVPS->getLayerIdInNuh( i ), i ); 
1357
1358    for( Int j = 0; j < pcVPS->getNumScalabilityTypes() ; j++ ) 
1359    {
1360      if ( !pcVPS->getSplittingFlag() )
1361      {
1362        READ_CODE( pcVPS->getDimensionIdLen( j ), uiCode, "dimension_id[i][j]" );  pcVPS->setDimensionId( i, j, uiCode );
1363      }
1364      else
1365      {
1366        pcVPS->setDimensionId( i, j, pcVPS->inferDimensionId( i, j)  );
1367      }
1368    }
1369  }
1370
1371  pcVPS->initNumViews(); 
1372
1373  READ_CODE( 4, uiCode, "view_id_len" ); pcVPS->setViewIdLen( uiCode );
1374
1375  if ( pcVPS->getViewIdLen( ) > 0 )
1376  {   
1377    for( Int i = 0; i < pcVPS->getNumViews(); i++ )
1378    {
1379      READ_CODE( pcVPS->getViewIdLen( ), uiCode, "view_id_val[i]" ); pcVPS->setViewIdVal( i, uiCode );
1380    }
1381  }
1382  else
1383  {
1384    for( Int i = 0; i < pcVPS->getNumViews(); i++ )
1385    {
1386      pcVPS->setViewIdVal( i, 0 ); 
1387    }
1388  }
1389
1390
1391  for( Int i = 1; i <= pcVPS->getMaxLayersMinus1(); i++ )
1392  {
1393    for( Int j = 0; j < i; j++ )
1394    {
1395      READ_FLAG( uiCode, "direct_dependency_flag[i][j]" );             pcVPS->setDirectDependencyFlag( i, j, uiCode );
1396    }
1397  }
1398  pcVPS->setRefLayers(); 
1399
1400  if ( pcVPS->getNumIndependentLayers() > 1 ) 
1401  {
1402    READ_UVLC( uiCode, "num_add_layer_sets"      ); pcVPS->setNumAddLayerSets( uiCode );
1403  }
1404  for (Int i = 0; i < pcVPS->getNumAddLayerSets(); i++)
1405  {
1406    for (Int j = 1; j < pcVPS->getNumIndependentLayers(); j++)
1407    {
1408      READ_CODE( pcVPS->getHighestLayerIdxPlus1Len( j ) , uiCode, "highest_layer_idx_plus1" ); pcVPS->setHighestLayerIdxPlus1( i, j, uiCode );
1409    }
1410    pcVPS->deriveAddLayerSetLayerIdList( i );
1411  }
1412
1413  READ_FLAG( uiCode, "vps_sub_layers_max_minus1_present_flag" ); pcVPS->setVpsSubLayersMaxMinus1PresentFlag( uiCode == 1 );
1414  if ( pcVPS->getVpsSubLayersMaxMinus1PresentFlag() )
1415  {
1416    for (Int i = 0; i <= pcVPS->getMaxLayersMinus1(); i++ )
1417    {
1418      READ_CODE( 3, uiCode, "sub_layers_vps_max_minus1" ); pcVPS->setSubLayersVpsMaxMinus1( i, uiCode );   
1419      pcVPS->checkSubLayersVpsMaxMinus1( i ); 
1420    }
1421  } 
1422  else
1423  {
1424    for (Int i = 0; i <= pcVPS->getMaxLayersMinus1(); i++ )
1425    {
1426      pcVPS->setSubLayersVpsMaxMinus1( i, pcVPS->getMaxTLayers( ) - 1);   
1427    }
1428  }
1429  READ_FLAG( uiCode, "max_tid_ref_present_flag" ); pcVPS->setMaxTidRefPresentFlag( uiCode == 1 );
1430
1431  if ( pcVPS->getMaxTidRefPresentFlag() )
1432  {   
1433    for( Int i = 0; i < pcVPS->getMaxLayersMinus1(); i++ )
1434    {
1435      for( Int j = i + 1; j <= pcVPS->getMaxLayersMinus1(); j++ )
1436      {
1437        if ( pcVPS->getDirectDependencyFlag(j,i) )
1438        {
1439          READ_CODE( 3, uiCode, "max_tid_il_ref_pics_plus1" ); pcVPS->setMaxTidIlRefPicsPlus1( i, j, uiCode );
1440        }
1441      }
1442    }
1443  }
1444
1445  READ_FLAG( uiCode, "all_ref_layers_active_flag" );             pcVPS->setAllRefLayersActiveFlag( uiCode == 1 );
1446  READ_UVLC( uiCode, "vps_num_profile_tier_level_minus1" );  pcVPS->setVpsNumProfileTierLevelMinus1( uiCode ); 
1447
1448  Int offsetVal =  ( pcVPS->getMaxLayersMinus1() > 0  &&  pcVPS->getVpsBaseLayerInternalFlag() ) ? 2 : 1;   
1449  for( Int i = pcVPS->getVpsBaseLayerInternalFlag() ? 2 : 1; i <= pcVPS->getVpsNumProfileTierLevelMinus1(); i++ )
1450  {
1451    READ_FLAG(  uiCode, "vps_profile_present_flag[i]" );    pcVPS->setVpsProfilePresentFlag( i, uiCode == 1 );
1452    parsePTL ( pcVPS->getPTL( offsetVal ), pcVPS->getVpsProfilePresentFlag( i ), pcVPS->getMaxTLayers()-1);
1453    pcVPS->getPTL( offsetVal )->inferGeneralValues ( pcVPS->getVpsProfilePresentFlag( i ), offsetVal, pcVPS->getPTL( offsetVal - 1 ) );   
1454    pcVPS->getPTL( offsetVal )->inferSubLayerValues( pcVPS->getMaxSubLayersMinus1()      , offsetVal, pcVPS->getPTL( offsetVal - 1 ) );   
1455    offsetVal++;
1456  }
1457
1458
1459  if (pcVPS->getNumLayerSets() > 1)
1460  {
1461    READ_UVLC( uiCode, "num_add_olss" ); pcVPS->setNumAddOlss( uiCode );
1462    READ_CODE( 2, uiCode, "default_output_layer_idc" ); pcVPS->setDefaultOutputLayerIdc( std::min( uiCode, (UInt) 2 ) );   
1463  }
1464
1465  pcVPS->initTargetLayerIdLists( ); 
1466
1467
1468  pcVPS->setOutputLayerFlag(0, 0, pcVPS->inferOutputLayerFlag( 0, 0 )); 
1469  pcVPS->setLayerSetIdxForOlsMinus1(0, -1); 
1470
1471  pcVPS->deriveNecessaryLayerFlags( 0 ); 
1472  pcVPS->deriveTargetLayerIdList( 0 ); 
1473
1474  if (pcVPS->getVpsBaseLayerInternalFlag() )
1475  { 
1476    pcVPS->setProfileTierLevelIdx(0,0, pcVPS->inferProfileTierLevelIdx(0,0) );
1477  }
1478  for( Int i = 1; i < pcVPS->getNumOutputLayerSets( ); i++ )
1479  {
1480    if( pcVPS->getNumLayerSets() > 2 && i >= pcVPS->getNumLayerSets( ) )   
1481    {       
1482      READ_CODE( pcVPS->getLayerSetIdxForOlsMinus1Len( i ), uiCode, "layer_set_idx_for_ols_minus1[i]" ); pcVPS->setLayerSetIdxForOlsMinus1( i, uiCode ); 
1483    }
1484
1485    if ( i > pcVPS->getVpsNumLayerSetsMinus1() || pcVPS->getDefaultOutputLayerIdc() == 2 )
1486    {       
1487      for( Int j = 0; j < pcVPS->getNumLayersInIdList( pcVPS->olsIdxToLsIdx( i ) ); j++ )
1488      {
1489        READ_FLAG( uiCode, "output_layer_flag" ); pcVPS->setOutputLayerFlag( i, j, uiCode == 1 ); 
1490      }
1491    }
1492    else
1493    { 
1494      for( Int j = 0; j < pcVPS->getNumLayersInIdList( pcVPS->olsIdxToLsIdx( i ) ); j++ )
1495      {             
1496        pcVPS->setOutputLayerFlag(i,j, pcVPS->inferOutputLayerFlag( i, j )); 
1497      }
1498    }
1499    pcVPS->deriveNecessaryLayerFlags( i ); 
1500    pcVPS->deriveTargetLayerIdList( i ); 
1501
1502    for ( Int j = 0; j < pcVPS->getNumLayersInIdList( pcVPS->olsIdxToLsIdx(i)); j++ )
1503    {   
1504      if (pcVPS->getNecessaryLayerFlag( i, j ) && pcVPS->getVpsNumProfileTierLevelMinus1() > 0 )
1505      {
1506        READ_CODE( pcVPS->getProfileTierLevelIdxLen(), uiCode,"profile_tier_level_idx[ i ][ j ]" );   pcVPS->setProfileTierLevelIdx( i, j, uiCode ); 
1507      }
1508      if (pcVPS->getNecessaryLayerFlag( i, j ) && pcVPS->getVpsNumProfileTierLevelMinus1() == 0 )
1509      {
1510        pcVPS->setProfileTierLevelIdx( i , j, pcVPS->inferProfileTierLevelIdx( i, j) );
1511      }
1512    }
1513
1514    if( pcVPS->getNumOutputLayersInOutputLayerSet( i ) == 1 && pcVPS->getNumDirectRefLayers( pcVPS->getOlsHighestOutputLayerId( i ) ) > 0 )
1515    {
1516      READ_FLAG( uiCode, "alt_output_layer_flag[ i ]" ); pcVPS->setAltOutputLayerFlag( i, uiCode == 1 );
1517    }
1518  }
1519
1520  READ_UVLC( uiCode, "vps_num_rep_formats_minus1" ); pcVPS->setVpsNumRepFormatsMinus1( uiCode );
1521
1522  std::vector<TComRepFormat> repFormats;
1523  repFormats.resize( pcVPS->getVpsNumRepFormatsMinus1() + 1 ); 
1524  for (Int i = 0; i <= pcVPS->getVpsNumRepFormatsMinus1(); i++ )
1525  {     
1526    TComRepFormat* curRepFormat = &repFormats[i]; 
1527    TComRepFormat* prevRepFormat = i > 0 ? &repFormats[ i - 1] : NULL; 
1528    parseRepFormat( i, curRepFormat ,  prevRepFormat);     
1529  }
1530  pcVPS->setRepFormat( repFormats ); 
1531
1532  if ( pcVPS->getVpsNumRepFormatsMinus1() > 0 )
1533  {
1534    READ_FLAG( uiCode, "rep_format_idx_present_flag" ); pcVPS->setRepFormatIdxPresentFlag( uiCode == 1 );
1535  }
1536  if( pcVPS->getRepFormatIdxPresentFlag() ) 
1537  {
1538    for( Int i = pcVPS->getVpsBaseLayerInternalFlag() ? 1 : 0; i <=  pcVPS->getMaxLayersMinus1(); i++ )
1539    {
1540      READ_CODE( pcVPS->getVpsRepFormatIdxLen(), uiCode, "vps_rep_format_idx[i]" ); pcVPS->setVpsRepFormatIdx( i, uiCode );
1541    }
1542  }
1543  else
1544  {
1545    for( Int i = pcVPS->getVpsBaseLayerInternalFlag() ? 1 : 0; i <=  pcVPS->getMaxLayersMinus1(); i++ )
1546    {
1547      pcVPS->setVpsRepFormatIdx( i, pcVPS->inferVpsRepFormatIdx( i ) );
1548    }
1549  }
1550
1551  READ_FLAG( uiCode, "max_one_active_ref_layer_flag" ); pcVPS->setMaxOneActiveRefLayerFlag ( uiCode == 1 ); 
1552
1553  READ_FLAG( uiCode, "vps_poc_lsb_aligned_flag" ); pcVPS->setVpsPocLsbAlignedFlag( uiCode == 1 );
1554  for( Int i = 1; i  <=  pcVPS->getMaxLayersMinus1(); i++ )
1555  {
1556    if( pcVPS->getNumDirectRefLayers( pcVPS->getLayerIdInNuh( i ) )  ==  0 )
1557    {     
1558      READ_FLAG( uiCode, "poc_lsb_not_present_flag" ); pcVPS->setPocLsbNotPresentFlag( i, uiCode == 1 );
1559    }
1560  }
1561
1562  parseDpbSize( pcVPS ); 
1563
1564  READ_UVLC( uiCode, "direct_dep_type_len_minus2")    ; pcVPS->setDirectDepTypeLenMinus2   ( uiCode ); 
1565
1566  READ_FLAG( uiCode, "default_direct_dependency_flag" ); pcVPS->setDefaultDirectDependencyFlag( uiCode == 1 );
1567  if ( pcVPS->getDefaultDirectDependencyFlag( ) )
1568  { 
1569    READ_CODE( pcVPS->getDirectDepTypeLenMinus2( ) + 2, uiCode, "default_direct_dependency_type" ); pcVPS->setDefaultDirectDependencyType( uiCode );
1570  }
1571
1572  for( Int i = pcVPS->getVpsBaseLayerInternalFlag() ?  1 : 2; i <= pcVPS->getMaxLayersMinus1(); i++ )
1573  {
1574    for( Int j = pcVPS->getVpsBaseLayerInternalFlag() ?  0 : 1; j < i; j++ )
1575    {
1576      if (pcVPS->getDirectDependencyFlag( i, j) )
1577      {       
1578        if ( pcVPS->getDefaultDirectDependencyFlag( ) )
1579        { 
1580          pcVPS->setDirectDependencyType( i, j , pcVPS->getDefaultDirectDependencyType( ) );
1581        }
1582        else
1583        {
1584
1585          READ_CODE( pcVPS->getDirectDepTypeLenMinus2( ) + 2,  uiCode, "direct_dependency_type[i][j]" ); pcVPS->setDirectDependencyType( i, j , uiCode);
1586        }
1587      }
1588    }
1589  } 
1590  READ_UVLC( uiCode, "vps_non_vui_extension_length" ); pcVPS->setVpsNonVuiExtensionLength( uiCode ); 
1591  for ( Int i = 1; i <= pcVPS->getVpsNonVuiExtensionLength(); i++ )
1592  {
1593    READ_CODE( 8, uiCode, "vps_non_vui_extension_data_byte" );
1594  }
1595  READ_FLAG( uiCode, "vps_vui_present_flag" );  pcVPS->setVpsVuiPresentFlag( uiCode == 1 );
1596 
1597  TComVPSVUI vpsVui;
1598  if( pcVPS->getVpsVuiPresentFlag() )
1599  {
1600    m_pcBitstream->readOutTrailingBits(); // vps_vui_alignment_bit_equal_to_one
1601    parseVPSVUI( pcVPS, &vpsVui ); 
1602  }     
1603  else
1604  {   
1605    // inference of syntax elements that differ from default inference (as done in constructor), when VPS VUI is not present   
1606    vpsVui.setCrossLayerIrapAlignedFlag( false ); 
1607  }
1608  pcVPS->setVPSVUI( vpsVui ); 
1609  pcVPS->checkVPSExtensionSyntax(); 
1610}
1611
1612Void TDecCavlc::parseRepFormat( Int i, TComRepFormat* pcRepFormat, const TComRepFormat* pcPrevRepFormat )
1613{
1614  assert( pcRepFormat ); 
1615
1616  UInt uiCode; 
1617
1618  READ_CODE( 16, uiCode, "pic_width_vps_in_luma_samples" );  pcRepFormat->setPicWidthVpsInLumaSamples ( uiCode );
1619  READ_CODE( 16, uiCode, "pic_height_vps_in_luma_samples" ); pcRepFormat->setPicHeightVpsInLumaSamples( uiCode );
1620  READ_FLAG( uiCode, "chroma_and_bit_depth_vps_present_flag" ); pcRepFormat->setChromaAndBitDepthVpsPresentFlag( uiCode == 1 );
1621
1622  pcRepFormat->checkChromaAndBitDepthVpsPresentFlag( i ); 
1623
1624  if ( pcRepFormat->getChromaAndBitDepthVpsPresentFlag() )
1625  { 
1626    READ_CODE( 2,  uiCode, "chroma_format_vps_idc" );          pcRepFormat->setChromaFormatVpsIdc       ( uiCode );
1627    if ( pcRepFormat->getChromaFormatVpsIdc() == 3 )
1628    {
1629      READ_FLAG( uiCode, "separate_colour_plane_vps_flag" ); pcRepFormat->setSeparateColourPlaneVpsFlag( uiCode == 1 ); 
1630    }
1631    READ_CODE( 4,  uiCode, "bit_depth_vps_luma_minus8" );      pcRepFormat->setBitDepthVpsLumaMinus8    ( uiCode );
1632    READ_CODE( 4,  uiCode, "bit_depth_vps_chroma_minus8" );    pcRepFormat->setBitDepthVpsChromaMinus8  ( uiCode );
1633  }
1634  else
1635  {
1636    pcRepFormat->inferChromaAndBitDepth(pcPrevRepFormat ); 
1637  }
1638  READ_FLAG( uiCode, "conformance_window_vps_flag" ); pcRepFormat->setConformanceWindowVpsFlag( uiCode == 1 );
1639  if ( pcRepFormat->getConformanceWindowVpsFlag() )
1640  {
1641    READ_UVLC( uiCode, "conf_win_vps_left_offset" ); pcRepFormat->setConfWinVpsLeftOffset( uiCode );
1642    READ_UVLC( uiCode, "conf_win_vps_right_offset" ); pcRepFormat->setConfWinVpsRightOffset( uiCode );
1643    READ_UVLC( uiCode, "conf_win_vps_top_offset" ); pcRepFormat->setConfWinVpsTopOffset( uiCode );
1644    READ_UVLC( uiCode, "conf_win_vps_bottom_offset" ); pcRepFormat->setConfWinVpsBottomOffset( uiCode );
1645  }
1646}
1647
1648
1649Void TDecCavlc::parseVPSVUI( const TComVPS* pcVPS, TComVPSVUI* vpsVui )
1650{
1651  assert( pcVPS ); 
1652  assert( vpsVui ); 
1653 
1654  vpsVui->init(pcVPS->getNumAddLayerSets(),pcVPS->getMaxSubLayersMinus1() + 1, pcVPS->getMaxLayersMinus1() + 1 ); 
1655
1656  UInt uiCode; 
1657  READ_FLAG( uiCode, "cross_layer_pic_type_aligned_flag" ); vpsVui->setCrossLayerPicTypeAlignedFlag( uiCode == 1 );
1658  if ( !vpsVui->getCrossLayerPicTypeAlignedFlag() )
1659  { 
1660    READ_FLAG( uiCode, "cross_layer_irap_aligned_flag" ); vpsVui->setCrossLayerIrapAlignedFlag( uiCode == 1 );
1661  }
1662  if( vpsVui->getCrossLayerIrapAlignedFlag( ) )
1663  {
1664    READ_FLAG( uiCode, "all_layers_idr_aligned_flag" ); vpsVui->setAllLayersIdrAlignedFlag( uiCode == 1 );
1665  }
1666  READ_FLAG( uiCode, "bit_rate_present_vps_flag" ); vpsVui->setBitRatePresentVpsFlag( uiCode == 1 );
1667  READ_FLAG( uiCode, "pic_rate_present_vps_flag" ); vpsVui->setPicRatePresentVpsFlag( uiCode == 1 );
1668  if( vpsVui->getBitRatePresentVpsFlag( )  ||  vpsVui->getPicRatePresentVpsFlag( ) )
1669  {
1670    for( Int i = pcVPS->getVpsBaseLayerInternalFlag() ? 0 : 1; i  <  pcVPS->getNumLayerSets(); i++ )
1671    {
1672      for( Int j = 0; j  <=  pcVPS->getMaxSubLayersInLayerSetMinus1( i ); j++ ) 
1673      {
1674        if( vpsVui->getBitRatePresentVpsFlag( ) )
1675        {
1676          READ_FLAG( uiCode, "bit_rate_present_flag" ); vpsVui->setBitRatePresentFlag( i, j, uiCode == 1 );           
1677        }
1678        if( vpsVui->getPicRatePresentVpsFlag( )  )
1679        {
1680          READ_FLAG( uiCode, "pic_rate_present_flag" ); vpsVui->setPicRatePresentFlag( i, j, uiCode == 1 );
1681        }
1682        if( vpsVui->getBitRatePresentFlag( i, j ) )
1683        {
1684          READ_CODE( 16, uiCode, "avg_bit_rate" ); vpsVui->setAvgBitRate( i, j, uiCode );
1685          READ_CODE( 16, uiCode, "max_bit_rate" ); vpsVui->setMaxBitRate( i, j, uiCode );
1686        }
1687        if( vpsVui->getPicRatePresentFlag( i, j ) )
1688        {
1689          READ_CODE( 2,  uiCode, "constant_pic_rate_idc" ); vpsVui->setConstantPicRateIdc( i, j, uiCode );
1690          READ_CODE( 16, uiCode, "avg_pic_rate" );          vpsVui->setAvgPicRate( i, j, uiCode );
1691        }
1692      }
1693    }
1694  }
1695
1696  READ_FLAG( uiCode, "video_signal_info_idx_present_flag" ); vpsVui->setVideoSignalInfoIdxPresentFlag( uiCode == 1 );
1697  if( vpsVui->getVideoSignalInfoIdxPresentFlag() )
1698  {
1699    READ_CODE( 4, uiCode, "vps_num_video_signal_info_minus1" ); vpsVui->setVpsNumVideoSignalInfoMinus1( uiCode );
1700  }
1701  else
1702  {
1703    vpsVui->setVpsNumVideoSignalInfoMinus1( (pcVPS->getMaxLayersMinus1() - pcVPS->getVpsBaseLayerInternalFlag()) ? 0 : 1 );
1704  }
1705
1706  std::vector<TComVideoSignalInfo> videoSignalInfos; 
1707  videoSignalInfos.resize(vpsVui->getVpsNumVideoSignalInfoMinus1() + 1 );
1708
1709  for( Int i = 0; i <= vpsVui->getVpsNumVideoSignalInfoMinus1(); i++ )
1710  { 
1711    parseVideoSignalInfo( &videoSignalInfos[i] );     
1712  }
1713  vpsVui->setVideoSignalInfo( videoSignalInfos ); 
1714
1715  if( vpsVui->getVideoSignalInfoIdxPresentFlag() && vpsVui->getVpsNumVideoSignalInfoMinus1() > 0 )
1716  {
1717    for( Int i = pcVPS->getVpsBaseLayerInternalFlag() ? 0 : 1; i <=  pcVPS->getMaxLayersMinus1(); i++ )
1718    {
1719      READ_CODE( 4, uiCode, "vps_video_signal_info_idx" ); vpsVui->setVpsVideoSignalInfoIdx( i, uiCode );
1720    }
1721  }
1722  else if ( !vpsVui->getVideoSignalInfoIdxPresentFlag() )
1723  {
1724    for( Int i = pcVPS->getVpsBaseLayerInternalFlag() ? 0 : 1; i <=  pcVPS->getMaxLayersMinus1(); i++ )
1725    {
1726      vpsVui->setVpsVideoSignalInfoIdx( i, i );
1727    }
1728  }
1729  else
1730  {
1731    for( Int i = pcVPS->getVpsBaseLayerInternalFlag() ? 0 : 1; i <=  pcVPS->getMaxLayersMinus1(); i++ )
1732    {
1733      vpsVui->setVpsVideoSignalInfoIdx( i, 0 );
1734    }
1735  }
1736
1737  READ_FLAG( uiCode, "tiles_not_in_use_flag" ); vpsVui->setTilesNotInUseFlag( uiCode == 1 );
1738  if( !vpsVui->getTilesNotInUseFlag() ) 
1739  {     
1740    for( Int i = pcVPS->getVpsBaseLayerInternalFlag() ? 0 : 1; i  <=  pcVPS->getMaxLayersMinus1(); i++ )
1741    {
1742      READ_FLAG( uiCode, "tiles_in_use_flag[i]" ); vpsVui->setTilesInUseFlag( i, uiCode == 1 );
1743      if( vpsVui->getTilesInUseFlag( i ) ) 
1744      {
1745        READ_FLAG( uiCode, "loop_filter_not_across_tiles_flag[i]" ); vpsVui->setLoopFilterNotAcrossTilesFlag( i, uiCode == 1 );
1746      }
1747    } 
1748    for( Int i = pcVPS->getVpsBaseLayerInternalFlag() ? 1 : 2; i  <=  pcVPS->getMaxLayersMinus1(); i++ ) 
1749    {
1750      for( Int j = 0; j < pcVPS->getNumDirectRefLayers( pcVPS->getLayerIdInNuh( i ) ) ; j++ )
1751      {
1752        Int layerIdx = pcVPS->getLayerIdInVps(pcVPS->getIdDirectRefLayer(pcVPS->getLayerIdInNuh( i ) , j  )); 
1753        if( vpsVui->getTilesInUseFlag( i )  &&  vpsVui->getTilesInUseFlag( layerIdx ) ) 
1754        {
1755          READ_FLAG( uiCode, "tile_boundaries_aligned_flag[i][j]" ); vpsVui->setTileBoundariesAlignedFlag( i, j, uiCode == 1 );
1756        }
1757      } 
1758    }
1759  } 
1760 
1761  READ_FLAG( uiCode, "wpp_not_in_use_flag" ); vpsVui->setWppNotInUseFlag( uiCode == 1 );
1762 
1763  if( !vpsVui->getWppNotInUseFlag( ))
1764  {
1765    for( Int i = 0; i  <=  pcVPS->getMaxLayersMinus1(); i++ ) 
1766    {
1767      READ_FLAG( uiCode, "wpp_in_use_flag[i]" ); vpsVui->setWppInUseFlag( i, uiCode == 1 );
1768    }
1769  }
1770  READ_FLAG( uiCode, "single_layer_for_non_irap_flag" ); vpsVui->setSingleLayerForNonIrapFlag( uiCode == 1 );
1771  READ_FLAG( uiCode, "higher_layer_irap_skip_flag" ); vpsVui->setHigherLayerIrapSkipFlag( uiCode == 1 );
1772  READ_FLAG( uiCode, "ilp_restricted_ref_layers_flag" ); vpsVui->setIlpRestrictedRefLayersFlag( uiCode == 1 );
1773
1774  if( vpsVui->getIlpRestrictedRefLayersFlag( ) )
1775  {
1776    for( Int i = 1; i  <=  pcVPS->getMaxLayersMinus1(); i++ )
1777    {
1778      for( Int j = 0; j < pcVPS->getNumDirectRefLayers( pcVPS->getLayerIdInNuh( i ) ); j++ )
1779      {
1780        if( pcVPS->getVpsBaseLayerInternalFlag() || pcVPS->getIdDirectRefLayer( pcVPS->getLayerIdInNuh( i ), j ) > 0 )
1781        {       
1782          READ_UVLC( uiCode, "min_spatial_segment_offset_plus1" ); vpsVui->setMinSpatialSegmentOffsetPlus1( i, j, uiCode );
1783          if( vpsVui->getMinSpatialSegmentOffsetPlus1( i, j ) > 0 )
1784          {
1785            READ_FLAG( uiCode, "ctu_based_offset_enabled_flag" ); vpsVui->setCtuBasedOffsetEnabledFlag( i, j, uiCode == 1 );
1786            if( vpsVui->getCtuBasedOffsetEnabledFlag( i, j ) )
1787            {
1788              READ_UVLC( uiCode, "min_horizontal_ctu_offset_plus1" ); vpsVui->setMinHorizontalCtuOffsetPlus1( i, j, uiCode );
1789            }
1790          }
1791        }
1792      }
1793    }
1794  }
1795
1796  READ_FLAG( uiCode, "vps_vui_bsp_hrd_present_flag" ); vpsVui->setVpsVuiBspHrdPresentFlag( uiCode == 1 );
1797  if ( vpsVui->getVpsVuiBspHrdPresentFlag( ) )
1798  {
1799    assert(pcVPS->getTimingInfo()->getTimingInfoPresentFlag() == 1);   
1800    parseVpsVuiBspHrdParameters( pcVPS, vpsVui );     
1801  }
1802  for( Int i = 1; i  <=  pcVPS->getMaxLayersMinus1(); i++ )
1803  {
1804    if( pcVPS->getNumDirectRefLayers( pcVPS->getLayerIdInNuh( i )) == 0 ) 
1805    {
1806      READ_FLAG( uiCode, "base_layer_parameter_set_compatibility_flag" ); vpsVui->setBaseLayerParameterSetCompatibilityFlag( i, uiCode == 1 );
1807    }
1808  } 
1809}
1810
1811Void TDecCavlc::parseVpsVuiBspHrdParameters( const TComVPS* pcVPS, TComVPSVUI* vpsVui )
1812{
1813  assert( pcVPS  ); 
1814  assert( vpsVui ); 
1815
1816  TComVpsVuiBspHrdParameters  vpsVuiBspHrdP;   
1817 
1818  UInt uiCode; 
1819  READ_UVLC( uiCode, "vps_num_add_hrd_params" ); vpsVuiBspHrdP.setVpsNumAddHrdParams( uiCode );
1820  vpsVuiBspHrdP.createAfterVpsNumAddHrdParams( pcVPS ); 
1821  for( Int i = pcVPS->getNumHrdParameters(); i < pcVPS->getNumHrdParameters() + vpsVuiBspHrdP.getVpsNumAddHrdParams(); i++ )
1822  { 
1823    if( i > 0 ) 
1824    {
1825      READ_FLAG( uiCode, "cprms_add_present_flag" ); vpsVuiBspHrdP.setCprmsAddPresentFlag( i, uiCode == 1 );
1826    }
1827    else
1828    {
1829       vpsVuiBspHrdP.setCprmsAddPresentFlag( i, true );
1830    }
1831
1832    READ_UVLC( uiCode, "num_sub_layer_hrd_minus1" ); vpsVuiBspHrdP.setNumSubLayerHrdMinus1( i, uiCode );
1833   
1834    TComHRD hrdParameters;
1835    parseHrdParameters( &hrdParameters, vpsVuiBspHrdP.getCprmsAddPresentFlag( i ), vpsVuiBspHrdP.getNumSubLayerHrdMinus1( i ) );     
1836    vpsVuiBspHrdP.setHrdParametermeters( i, hrdParameters ); 
1837  }
1838
1839  vpsVuiBspHrdP.setNumPartitionsInSchemeMinus1( 0, 0, 0);
1840  vpsVuiBspHrdP.createAfterNumPartitionsInSchemeMinus1( pcVPS, 0, 0 );
1841
1842  for( Int h = 0; h < pcVPS->getNumOutputLayerSets(); h++ )
1843  { 
1844    if ( h == 0)
1845    {
1846      vpsVuiBspHrdP.setNumSignalledPartitioningSchemes( h, 0 );
1847    }
1848    else
1849    {
1850      READ_UVLC( uiCode, "num_signalled_partitioning_schemes" ); vpsVuiBspHrdP.setNumSignalledPartitioningSchemes( h, uiCode );
1851    }   
1852    vpsVuiBspHrdP.createAfterNumSignalledPartitioningSchemes( pcVPS, h );
1853
1854    for( Int j = 0; j < vpsVuiBspHrdP.getNumSignalledPartitioningSchemes( h ) + 1; j++ )
1855    { 
1856      if ( j == 0 && h == 0 )
1857      {
1858        vpsVuiBspHrdP.setNumPartitionsInSchemeMinus1( h, j, uiCode );
1859      }
1860      else if( j == 0 )
1861      {
1862        vpsVuiBspHrdP.setNumPartitionsInSchemeMinus1( h, j, pcVPS->getNumLayersInIdList( h ) - 1 );
1863      }
1864      else
1865      {
1866        READ_UVLC( uiCode, "num_partitions_in_scheme_minus1" ); vpsVuiBspHrdP.setNumPartitionsInSchemeMinus1( h, j, uiCode );
1867      }
1868      vpsVuiBspHrdP.createAfterNumPartitionsInSchemeMinus1(pcVPS, h, j );
1869
1870      for( Int k = 0; k  <=  vpsVuiBspHrdP.getNumPartitionsInSchemeMinus1( h, j ); k++ ) 
1871      {
1872        for( Int r = 0; r < pcVPS->getNumLayersInIdList(pcVPS->olsIdxToLsIdx( h ) )   ; r++ ) 
1873        {
1874          if( h == 0 && j == 0 && k == 0 && r == 0 )
1875          {
1876             vpsVuiBspHrdP.setLayerIncludedInPartitionFlag( h, j, k, r, true );
1877          }
1878          else if ( h > 0 && j == 0 )
1879          {
1880             vpsVuiBspHrdP.setLayerIncludedInPartitionFlag( h, j, k, r, (k == r) );
1881          }
1882          else
1883          {
1884            READ_FLAG( uiCode, "layer_included_in_partition_flag" ); vpsVuiBspHrdP.setLayerIncludedInPartitionFlag( h, j, k, r, uiCode == 1 );
1885          }         
1886        }
1887      }
1888    } 
1889    if ( h > 0 )
1890    {
1891      for( Int i = 0; i < vpsVuiBspHrdP.getNumSignalledPartitioningSchemes( h ) + 1; i++ ) 
1892      {
1893        for( Int t = 0; t  <=  pcVPS->getMaxSubLayersInLayerSetMinus1( pcVPS->olsIdxToLsIdx( i ) ); t++ )
1894        { 
1895          READ_UVLC( uiCode, "num_bsp_schedules_minus1" ); vpsVuiBspHrdP.setNumBspSchedulesMinus1( h, i, t, uiCode );
1896          vpsVuiBspHrdP.createAfterNumBspSchedulesMinus1(pcVPS, h, i, t );
1897          for( Int j = 0; j  <=  vpsVuiBspHrdP.getNumBspSchedulesMinus1( h, i, t ); j++ ) 
1898          {
1899            for( Int k = 0; k  <=  vpsVuiBspHrdP.getNumPartitionsInSchemeMinus1( h, j ); k++ )
1900            { 
1901              READ_CODE( vpsVuiBspHrdP.getBspHrdIdxLen( pcVPS ), uiCode, "bsp_hrd_idx" ); vpsVuiBspHrdP.setBspHrdIdx( h, i, t, j, k, uiCode );
1902              READ_UVLC( uiCode, "bsp_sched_idx" ); vpsVuiBspHrdP.setBspSchedIdx( h, i, t, j, k, uiCode );
1903            } 
1904          }
1905        } 
1906      }
1907    }
1908  } 
1909  vpsVui->setVpsVuiBspHrdParameters( vpsVuiBspHrdP ); 
1910}
1911
1912Void TDecCavlc::parseVideoSignalInfo( TComVideoSignalInfo* pcVideoSignalInfo ) 
1913{
1914  UInt uiCode; 
1915  READ_CODE( 3, uiCode, "video_vps_format" );             pcVideoSignalInfo->setVideoVpsFormat( uiCode );
1916  READ_FLAG( uiCode, "video_full_range_vps_flag" );       pcVideoSignalInfo->setVideoFullRangeVpsFlag( uiCode == 1 );
1917  READ_CODE( 8, uiCode, "colour_primaries_vps" );         pcVideoSignalInfo->setColourPrimariesVps( uiCode );
1918  READ_CODE( 8, uiCode, "transfer_characteristics_vps" ); pcVideoSignalInfo->setTransferCharacteristicsVps( uiCode );
1919  READ_CODE( 8, uiCode, "matrix_coeffs_vps" );            pcVideoSignalInfo->setMatrixCoeffsVps( uiCode );
1920}
1921
1922Void TDecCavlc::parseDpbSize( TComVPS* vps )
1923{
1924  UInt uiCode; 
1925  TComDpbSize dpbSize; 
1926 
1927  dpbSize.init( vps->getNumOutputLayerSets(), vps->getVpsMaxLayerId() + 1, vps->getMaxSubLayersMinus1() + 1 ) ;
1928
1929  for( Int i = 1; i < vps->getNumOutputLayerSets(); i++ )
1930  { 
1931    Int currLsIdx = vps->olsIdxToLsIdx( i ); 
1932    READ_FLAG( uiCode, "sub_layer_flag_info_present_flag" ); dpbSize.setSubLayerFlagInfoPresentFlag( i, uiCode == 1 );
1933    for( Int j = 0; j  <=  vps->getMaxSubLayersInLayerSetMinus1( currLsIdx ); j++ )
1934    { 
1935      if( j > 0  &&  dpbSize.getSubLayerDpbInfoPresentFlag( i, j )  ) 
1936      {
1937        READ_FLAG( uiCode, "sub_layer_dpb_info_present_flag" ); dpbSize.setSubLayerDpbInfoPresentFlag( i, j, uiCode == 1 );
1938      }
1939      if( dpbSize.getSubLayerDpbInfoPresentFlag( i, j ) )
1940      { 
1941        for( Int k = 0; k < vps->getNumLayersInIdList( currLsIdx ); k++ )   
1942        {
1943          if ( vps->getNecessaryLayerFlag( i, k ) && ( vps->getVpsBaseLayerInternalFlag() || ( vps->getLayerSetLayerIdList(vps->olsIdxToLsIdx(i),k) != 0 ) ))
1944          {
1945            READ_UVLC( uiCode, "max_vps_dec_pic_buffering_minus1" ); dpbSize.setMaxVpsDecPicBufferingMinus1( i, k, j, uiCode );
1946          }
1947          else
1948          {
1949            if ( vps->getNecessaryLayerFlag( i, k ) && ( j == 0 ) && ( k == 0 ))
1950            {
1951              dpbSize.setMaxVpsDecPicBufferingMinus1(i ,k, j, 0 );
1952            }
1953          }
1954        }
1955        READ_UVLC( uiCode, "max_vps_num_reorder_pics" ); dpbSize.setMaxVpsNumReorderPics( i, j, uiCode );
1956        READ_UVLC( uiCode, "max_vps_latency_increase_plus1" ); dpbSize.setMaxVpsLatencyIncreasePlus1( i, j, uiCode );
1957      }
1958      else
1959      {
1960        if ( j > 0 )
1961        {
1962          for( Int k = 0; k < vps->getNumLayersInIdList( vps->olsIdxToLsIdx( i ) ); k++ )   
1963          {
1964            if ( vps->getNecessaryLayerFlag(i, k ) )
1965            {           
1966              dpbSize.setMaxVpsDecPicBufferingMinus1( i, k, j, dpbSize.getMaxVpsDecPicBufferingMinus1( i,k, j - 1 ) );
1967            }
1968          }
1969          dpbSize.setMaxVpsNumReorderPics      ( i, j, dpbSize.getMaxVpsNumReorderPics      ( i, j - 1 ) );
1970          dpbSize.setMaxVpsLatencyIncreasePlus1( i, j, dpbSize.getMaxVpsLatencyIncreasePlus1( i, j - 1 ) );
1971        }
1972      }
1973    } 
1974  } 
1975  vps->setDpbSize( dpbSize ); 
1976}
1977
1978#endif
1979
1980#if NH_MV
1981Void TDecCavlc::parseFirstSliceSegmentInPicFlag(TComSlice* pcSlice )
1982{
1983#if ENC_DEC_TRACE
1984#if NH_MV
1985  tracePSHeader( "Slice", pcSlice->getLayerId() ); 
1986#else
1987  xTraceSliceHeader();
1988#endif
1989#endif
1990
1991  UInt uiCode;
1992  READ_FLAG( uiCode, "first_slice_segment_in_pic_flag" );
1993  pcSlice->setFirstSliceSegementInPicFlag( uiCode ); 
1994}
1995
1996Void TDecCavlc::parseSliceHeader (TComSlice* pcSlice, ParameterSetManager *parameterSetManager )
1997#else
1998Void TDecCavlc::parseSliceHeader (TComSlice* pcSlice, ParameterSetManager *parameterSetManager, const Int prevTid0POC)
1999#endif
2000{
2001  UInt  uiCode;
2002  Int   iCode;
2003
2004#if !NH_MV
2005#if ENC_DEC_TRACE
2006  xTraceSliceHeader();
2007#endif
2008#endif
2009  TComPPS* pps = NULL;
2010  TComSPS* sps = NULL;
2011#if NH_MV
2012  TComVPS* vps = NULL;
2013#endif
2014
2015#if NH_MV
2016  // This has been parsed before in parseFirstSliceSegmentFlag(TComSlice* pcSlice )
2017  UInt firstSliceSegmentInPic = pcSlice->getFirstSliceSegementInPicFlag();   
2018#else
2019  UInt firstSliceSegmentInPic;
2020  READ_FLAG( firstSliceSegmentInPic, "first_slice_segment_in_pic_flag" );
2021#endif
2022  if( pcSlice->getRapPicFlag())
2023  {
2024    READ_FLAG( uiCode, "no_output_of_prior_pics_flag" );  //ignored -- updated already
2025    pcSlice->setNoOutputPriorPicsFlag(uiCode ? true : false);
2026  }
2027  READ_UVLC (    uiCode, "slice_pic_parameter_set_id" );  pcSlice->setPPSId(uiCode);
2028  pps = parameterSetManager->getPPS(uiCode);
2029  //!KS: need to add error handling code here, if PPS is not available
2030  assert(pps!=0);
2031  sps = parameterSetManager->getSPS(pps->getSPSId());
2032  //!KS: need to add error handling code here, if SPS is not available
2033  assert(sps!=0);
2034#if NH_MV 
2035  vps = parameterSetManager->getVPS(sps->getVPSId());
2036  assert( vps != NULL ); 
2037 
2038  m_decTop->initFromActiveVps( vps );
2039 
2040  if ( !m_decTop->getIsInOwnTargetDecLayerIdList() )
2041  {
2042    return; 
2043  }
2044
2045  pcSlice->setDecodingProcess( m_decTop->getDecodingProcess() ); 
2046  Int targetOlsIdx = m_decTop->getTargetOlsIdx(); 
2047
2048  // Do inference
2049  sps->inferRepFormat  ( vps , pcSlice->getLayerId(), false ); 
2050  sps->inferScalingList( parameterSetManager->getActiveSPS( sps->getSpsScalingListRefLayerId() ) );   
2051  sps->inferSpsMaxDecPicBufferingMinus1( vps, targetOlsIdx, pcSlice->getLayerId(), false ); 
2052
2053  if( sps->getMultiLayerExtSpsFlag() )
2054  {
2055    sps->setTemporalIdNestingFlag( (sps->getMaxTLayers() > 1) ? vps->getTemporalNestingFlag() : true );
2056  }
2057
2058  if ( sps->getVuiParametersPresentFlag() )
2059  {
2060    sps->getVuiParameters()->inferVideoSignalInfo( vps, pcSlice->getLayerId() ); 
2061  }
2062
2063
2064  pcSlice->setVPS(vps);     
2065  pcSlice->setSPS(sps);
2066  pcSlice->setPPS(pps);
2067
2068  pcSlice->setViewId   ( vps->getViewId   ( pcSlice->getLayerId() )      );
2069  pcSlice->setViewIndex( vps->getViewIndex( pcSlice->getLayerId() )      ); 
2070#endif
2071
2072  const ChromaFormat chFmt = sps->getChromaFormatIdc();
2073  const UInt numValidComp=getNumberValidComponents(chFmt);
2074  const Bool bChroma=(chFmt!=CHROMA_400);
2075
2076  if( pps->getDependentSliceSegmentsEnabledFlag() && ( !firstSliceSegmentInPic ))
2077  {
2078    READ_FLAG( uiCode, "dependent_slice_segment_flag" );       pcSlice->setDependentSliceSegmentFlag(uiCode ? true : false);
2079  }
2080  else
2081  {
2082    pcSlice->setDependentSliceSegmentFlag(false);
2083  }
2084  Int numCTUs = ((sps->getPicWidthInLumaSamples()+sps->getMaxCUWidth()-1)/sps->getMaxCUWidth())*((sps->getPicHeightInLumaSamples()+sps->getMaxCUHeight()-1)/sps->getMaxCUHeight());
2085  UInt sliceSegmentAddress = 0;
2086  Int bitsSliceSegmentAddress = 0;
2087  while(numCTUs>(1<<bitsSliceSegmentAddress))
2088  {
2089    bitsSliceSegmentAddress++;
2090  }
2091
2092  if(!firstSliceSegmentInPic)
2093  {
2094    READ_CODE( bitsSliceSegmentAddress, sliceSegmentAddress, "slice_segment_address" );
2095  }
2096  //set uiCode to equal slice start address (or dependent slice start address)
2097  pcSlice->setSliceSegmentCurStartCtuTsAddr( sliceSegmentAddress );// this is actually a Raster-Scan (RS) address, but we do not have the RS->TS conversion table defined yet.
2098  pcSlice->setSliceSegmentCurEndCtuTsAddr(numCTUs);                // Set end as the last CTU of the picture.
2099
2100
2101  if (!pcSlice->getDependentSliceSegmentFlag())
2102  {
2103    pcSlice->setSliceCurStartCtuTsAddr(sliceSegmentAddress); // this is actually a Raster-Scan (RS) address, but we do not have the RS->TS conversion table defined yet.
2104    pcSlice->setSliceCurEndCtuTsAddr(numCTUs);
2105  }
2106
2107  if(!pcSlice->getDependentSliceSegmentFlag())
2108  {
2109#if NH_MV   
2110    Int esb = 0; //Don't use i, otherwise will shadow something below
2111
2112    if ( pps->getNumExtraSliceHeaderBits() > esb )
2113    {
2114      esb++; 
2115      READ_FLAG( uiCode, "discardable_flag" ); pcSlice->setDiscardableFlag( uiCode == 1 );
2116      if ( uiCode == 1 )
2117      {
2118        assert(pcSlice->getNalUnitType() != NAL_UNIT_CODED_SLICE_TRAIL_R &&
2119          pcSlice->getNalUnitType() != NAL_UNIT_CODED_SLICE_TSA_R &&
2120          pcSlice->getNalUnitType() != NAL_UNIT_CODED_SLICE_STSA_R &&
2121          pcSlice->getNalUnitType() != NAL_UNIT_CODED_SLICE_RADL_R &&
2122          pcSlice->getNalUnitType() != NAL_UNIT_CODED_SLICE_RASL_R);
2123      }
2124    }
2125
2126    if ( pps->getNumExtraSliceHeaderBits() > esb )
2127    {
2128      esb++; 
2129      READ_FLAG( uiCode, "cross_layer_bla_flag" ); pcSlice->setCrossLayerBlaFlag( uiCode == 1 );
2130    }
2131    pcSlice->checkCrossLayerBlaFlag( ); 
2132
2133
2134    for (; esb < pps->getNumExtraSliceHeaderBits(); esb++)   
2135#else
2136    for (Int i = 0; i < pps->getNumExtraSliceHeaderBits(); i++)
2137#endif
2138    {
2139      READ_FLAG(uiCode, "slice_reserved_flag[]"); // ignored
2140    }
2141
2142    READ_UVLC (    uiCode, "slice_type" );            pcSlice->setSliceType((SliceType)uiCode);
2143    if( pps->getOutputFlagPresentFlag() )
2144    {
2145      READ_FLAG( uiCode, "pic_output_flag" );    pcSlice->setPicOutputFlag( uiCode ? true : false );
2146    }
2147    else
2148    {
2149      pcSlice->setPicOutputFlag( true );
2150    }
2151
2152    // if (separate_colour_plane_flag == 1)
2153    //   read colour_plane_id
2154    //   (separate_colour_plane_flag == 1) is not supported in this version of the standard.
2155
2156#if NH_MV
2157    if ( (pcSlice->getLayerId() > 0 && !vps->getPocLsbNotPresentFlag( pcSlice->getLayerIdInVps())) || !pcSlice->getIdrPicFlag() )
2158    {
2159      READ_CODE(sps->getBitsForPOC(), uiCode, "slice_pic_order_cnt_lsb"); pcSlice->setSlicePicOrderCntLsb( uiCode );
2160    } 
2161    else
2162    {
2163      pcSlice->setSlicePicOrderCntLsb( 0 );
2164    }
2165
2166    if( pcSlice->getNalUnitType()  != NAL_UNIT_CODED_SLICE_IDR_W_RADL  &&  pcSlice->getNalUnitType() !=  NAL_UNIT_CODED_SLICE_IDR_N_LP )
2167    {     
2168      READ_FLAG( uiCode, "short_term_ref_pic_set_sps_flag" ); pcSlice->setShortTermRefPicSetSpsFlag( uiCode == 1 );
2169      if( !pcSlice->getShortTermRefPicSetSpsFlag() )
2170      {
2171        parseShortTermRefPicSet( sps, pcSlice->getLocalStRps( ), sps->getNumShortTermRefPicSets() );
2172      }
2173      else if( sps->getNumShortTermRefPicSets() > 1 )
2174      {
2175        READ_CODE( pcSlice->getShortTermRefPicSetIdxLen() , uiCode, "short_term_ref_pic_set_idx" ); pcSlice->setShortTermRefPicSetIdx( uiCode );
2176      }
2177      if( sps->getLongTermRefPicsPresentFlag() )
2178      {
2179        if( sps->getNumLongTermRefPicsSps() > 0 )
2180        {
2181          READ_UVLC( uiCode, "num_long_term_sps" ); pcSlice->setNumLongTermSps( uiCode );
2182        }
2183        READ_UVLC( uiCode, "num_long_term_pics" ); pcSlice->setNumLongTermPics( uiCode );
2184        for( Int i = 0; i < pcSlice->getNumLongTermSps() + pcSlice->getNumLongTermPics( ); i++ )
2185        {
2186          if( i < pcSlice->getNumLongTermSps() )
2187          {
2188            if( sps->getNumLongTermRefPicsSps() > 1 )
2189            {
2190              READ_CODE( pcSlice->getLtIdxSpsLen() , uiCode, "lt_idx_sps" ); pcSlice->setLtIdxSps( i, uiCode );
2191            }
2192          }
2193          else
2194          {
2195            READ_CODE( pcSlice->getPocLsbLtLen() , uiCode, "poc_lsb_lt" ); pcSlice->setPocLsbLt( i, uiCode );
2196            READ_FLAG( uiCode, "used_by_curr_pic_lt_flag" ); pcSlice->setUsedByCurrPicLtFlag( i, uiCode == 1 );
2197          }
2198          READ_FLAG( uiCode, "delta_poc_msb_present_flag" ); pcSlice->setDeltaPocMsbPresentFlag( i, uiCode == 1 );
2199          if( pcSlice->getDeltaPocMsbPresentFlag( i ) )
2200          {
2201            READ_UVLC( uiCode, "delta_poc_msb_cycle_lt" ); pcSlice->setDeltaPocMsbCycleLt( i, uiCode );
2202          }
2203        }
2204      }
2205      if( sps->getSpsTemporalMvpEnabledFlag() )
2206      {
2207        READ_FLAG( uiCode, "slice_temporal_mvp_enabled_flag" ); pcSlice->setSliceTemporalMvpEnabledFlag( uiCode == 1 );
2208      }
2209    }
2210#else
2211if( pcSlice->getIdrPicFlag() )
2212{
2213  pcSlice->setPOC(0);
2214  TComReferencePictureSet* rps = pcSlice->getLocalRPS();
2215  (*rps)=TComReferencePictureSet();
2216  pcSlice->setRPS(rps);
2217}
2218else
2219{
2220  READ_CODE(sps->getBitsForPOC(), uiCode, "slice_pic_order_cnt_lsb");
2221  Int iPOClsb = uiCode;
2222  Int iPrevPOC = prevTid0POC;
2223  Int iMaxPOClsb = 1<< sps->getBitsForPOC();
2224  Int iPrevPOClsb = iPrevPOC & (iMaxPOClsb - 1);
2225  Int iPrevPOCmsb = iPrevPOC-iPrevPOClsb;
2226  Int iPOCmsb;
2227  if( ( iPOClsb  <  iPrevPOClsb ) && ( ( iPrevPOClsb - iPOClsb )  >=  ( iMaxPOClsb / 2 ) ) )
2228  {
2229    iPOCmsb = iPrevPOCmsb + iMaxPOClsb;
2230  }
2231  else if( (iPOClsb  >  iPrevPOClsb )  && ( (iPOClsb - iPrevPOClsb )  >  ( iMaxPOClsb / 2 ) ) )
2232  {
2233    iPOCmsb = iPrevPOCmsb - iMaxPOClsb;
2234  }
2235  else
2236  {
2237    iPOCmsb = iPrevPOCmsb;
2238  }
2239  if ( pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP
2240    || pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL
2241    || pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP )
2242  {
2243    // For BLA picture types, POCmsb is set to 0.
2244    iPOCmsb = 0;
2245  }
2246  pcSlice->setPOC              (iPOCmsb+iPOClsb);
2247
2248  TComReferencePictureSet* rps;
2249  rps = pcSlice->getLocalRPS();
2250  (*rps)=TComReferencePictureSet();
2251
2252  pcSlice->setRPS(rps);
2253  READ_FLAG( uiCode, "short_term_ref_pic_set_sps_flag" );
2254  if(uiCode == 0) // use short-term reference picture set explicitly signalled in slice header
2255  {
2256    parseShortTermRefPicSet(sps,rps, sps->getRPSList()->getNumberOfReferencePictureSets());
2257  }
2258  else // use reference to short-term reference picture set in PPS
2259  {
2260    Int numBits = 0;
2261    while ((1 << numBits) < sps->getRPSList()->getNumberOfReferencePictureSets())
2262    {
2263      numBits++;
2264    }
2265    if (numBits > 0)
2266    {
2267      READ_CODE( numBits, uiCode, "short_term_ref_pic_set_idx");
2268    }
2269    else
2270    {
2271      uiCode = 0;       
2272    }
2273    *rps = *(sps->getRPSList()->getReferencePictureSet(uiCode));
2274  }
2275  if(sps->getLongTermRefsPresent())
2276  {
2277    Int offset = rps->getNumberOfNegativePictures()+rps->getNumberOfPositivePictures();
2278    UInt numOfLtrp = 0;
2279    UInt numLtrpInSPS = 0;
2280    if (sps->getNumLongTermRefPicSPS() > 0)
2281    {
2282      READ_UVLC( uiCode, "num_long_term_sps");
2283      numLtrpInSPS = uiCode;
2284      numOfLtrp += numLtrpInSPS;
2285      rps->setNumberOfLongtermPictures(numOfLtrp);
2286    }
2287    Int bitsForLtrpInSPS = 0;
2288    while (sps->getNumLongTermRefPicSPS() > (1 << bitsForLtrpInSPS))
2289    {
2290      bitsForLtrpInSPS++;
2291    }
2292    READ_UVLC( uiCode, "num_long_term_pics");             rps->setNumberOfLongtermPictures(uiCode);
2293    numOfLtrp += uiCode;
2294    rps->setNumberOfLongtermPictures(numOfLtrp);
2295    Int maxPicOrderCntLSB = 1 << sps->getBitsForPOC();
2296    Int prevDeltaMSB = 0, deltaPocMSBCycleLT = 0;
2297    for(Int j=offset+rps->getNumberOfLongtermPictures()-1, k = 0; k < numOfLtrp; j--, k++)
2298    {
2299      Int pocLsbLt;
2300      if (k < numLtrpInSPS)
2301      {
2302        uiCode = 0;
2303        if (bitsForLtrpInSPS > 0)
2304        {
2305          READ_CODE(bitsForLtrpInSPS, uiCode, "lt_idx_sps[i]");
2306        }
2307        Bool usedByCurrFromSPS=sps->getUsedByCurrPicLtSPSFlag(uiCode);
2308
2309        pocLsbLt = sps->getLtRefPicPocLsbSps(uiCode);
2310        rps->setUsed(j,usedByCurrFromSPS);
2311      }
2312      else
2313      {
2314        READ_CODE(sps->getBitsForPOC(), uiCode, "poc_lsb_lt"); pocLsbLt= uiCode;
2315        READ_FLAG( uiCode, "used_by_curr_pic_lt_flag");     rps->setUsed(j,uiCode);
2316      }
2317      READ_FLAG(uiCode,"delta_poc_msb_present_flag");
2318      Bool mSBPresentFlag = uiCode ? true : false;
2319      if(mSBPresentFlag)
2320      {
2321        READ_UVLC( uiCode, "delta_poc_msb_cycle_lt[i]" );
2322        Bool deltaFlag = false;
2323        //            First LTRP                               || First LTRP from SH
2324        if( (j == offset+rps->getNumberOfLongtermPictures()-1) || (j == offset+(numOfLtrp-numLtrpInSPS)-1) )
2325        {
2326          deltaFlag = true;
2327        }
2328        if(deltaFlag)
2329        {
2330          deltaPocMSBCycleLT = uiCode;
2331        }
2332        else
2333        {
2334          deltaPocMSBCycleLT = uiCode + prevDeltaMSB;
2335        }
2336
2337        Int pocLTCurr = pcSlice->getPOC() - deltaPocMSBCycleLT * maxPicOrderCntLSB
2338          - iPOClsb + pocLsbLt;
2339        rps->setPOC     (j, pocLTCurr);
2340        rps->setDeltaPOC(j, - pcSlice->getPOC() + pocLTCurr);
2341        rps->setCheckLTMSBPresent(j,true);
2342      }
2343      else
2344      {
2345            rps->setPOC     (j, pocLsbLt);
2346            rps->setDeltaPOC(j, - pcSlice->getPOC() + pocLsbLt);
2347        rps->setCheckLTMSBPresent(j,false);
2348
2349        // reset deltaPocMSBCycleLT for first LTRP from slice header if MSB not present
2350        if( j == offset+(numOfLtrp-numLtrpInSPS)-1 )
2351        {
2352          deltaPocMSBCycleLT = 0;
2353        }
2354      }
2355      prevDeltaMSB = deltaPocMSBCycleLT;
2356    }
2357    offset += rps->getNumberOfLongtermPictures();
2358    rps->setNumberOfPictures(offset);
2359  }
2360
2361  if ( pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP
2362    || pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL
2363    || pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP )
2364  {
2365    // In the case of BLA picture types, rps data is read from slice header but ignored
2366    rps = pcSlice->getLocalRPS();
2367    (*rps)=TComReferencePictureSet();
2368    pcSlice->setRPS(rps);
2369  }
2370  if (sps->getTMVPFlagsPresent())
2371  {
2372    READ_FLAG( uiCode, "slice_temporal_mvp_enabled_flag" );
2373    pcSlice->setEnableTMVPFlag( uiCode == 1 ? true : false );
2374  }
2375  else
2376  {
2377    pcSlice->setEnableTMVPFlag(false);
2378  }
2379}
2380#endif
2381
2382#if NH_MV
2383    Bool interLayerPredLayerIdcPresentFlag = false; 
2384    Int layerId       = pcSlice->getLayerId(); 
2385    if( pcSlice->getLayerId() > 0 && !vps->getAllRefLayersActiveFlag() && vps->getNumDirectRefLayers( layerId ) > 0 )
2386    {   
2387      READ_FLAG( uiCode, "inter_layer_pred_enabled_flag" ); pcSlice->setInterLayerPredEnabledFlag( uiCode == 1 );
2388      if( pcSlice->getInterLayerPredEnabledFlag() && vps->getNumDirectRefLayers( layerId ) > 1 )
2389      {           
2390        if( !vps->getMaxOneActiveRefLayerFlag()) 
2391        {
2392          READ_CODE( pcSlice->getNumInterLayerRefPicsMinus1Len( ), uiCode, "num_inter_layer_ref_pics_minus1" ); pcSlice->setNumInterLayerRefPicsMinus1( uiCode );
2393        }
2394        if ( pcSlice->getNumActiveRefLayerPics() != vps->getNumDirectRefLayers( layerId ) )
2395        {
2396          interLayerPredLayerIdcPresentFlag = true; 
2397          for( Int idx = 0; idx < pcSlice->getNumActiveRefLayerPics(); idx++ )   
2398          {
2399            READ_CODE( pcSlice->getInterLayerPredLayerIdcLen( ), uiCode, "inter_layer_pred_layer_idc" ); pcSlice->setInterLayerPredLayerIdc( idx, uiCode );
2400          }
2401        }
2402      } 
2403    }
2404    if ( !interLayerPredLayerIdcPresentFlag )
2405    {
2406      for( Int i = 0; i < pcSlice->getNumActiveRefLayerPics(); i++ )   
2407      {
2408        pcSlice->setInterLayerPredLayerIdc( i, pcSlice->getRefLayerPicIdc( i ) );
2409      }
2410    }
2411#endif
2412    if(sps->getUseSAO())
2413    {
2414      READ_FLAG(uiCode, "slice_sao_luma_flag");  pcSlice->setSaoEnabledFlag(CHANNEL_TYPE_LUMA, (Bool)uiCode);
2415
2416      if (bChroma)
2417      {
2418        READ_FLAG(uiCode, "slice_sao_chroma_flag");  pcSlice->setSaoEnabledFlag(CHANNEL_TYPE_CHROMA, (Bool)uiCode);
2419      }
2420    }
2421
2422    if (pcSlice->getIdrPicFlag())
2423    {
2424      pcSlice->setEnableTMVPFlag(false);
2425    }
2426    if (!pcSlice->isIntra())
2427    {
2428
2429      READ_FLAG( uiCode, "num_ref_idx_active_override_flag");
2430      if (uiCode)
2431      {
2432        READ_UVLC (uiCode, "num_ref_idx_l0_active_minus1" );  pcSlice->setNumRefIdx( REF_PIC_LIST_0, uiCode + 1 );
2433        if (pcSlice->isInterB())
2434        {
2435          READ_UVLC (uiCode, "num_ref_idx_l1_active_minus1" );  pcSlice->setNumRefIdx( REF_PIC_LIST_1, uiCode + 1 );
2436        }
2437        else
2438        {
2439          pcSlice->setNumRefIdx(REF_PIC_LIST_1, 0);
2440        }
2441      }
2442      else
2443      {
2444        pcSlice->setNumRefIdx(REF_PIC_LIST_0, pps->getNumRefIdxL0DefaultActive());
2445        if (pcSlice->isInterB())
2446        {
2447          pcSlice->setNumRefIdx(REF_PIC_LIST_1, pps->getNumRefIdxL1DefaultActive());
2448        }
2449        else
2450        {
2451          pcSlice->setNumRefIdx(REF_PIC_LIST_1,0);
2452        }
2453      }
2454    }
2455    // }
2456#if NH_MV
2457   
2458    const Int numPicTotalCurr = pcSlice->getNumPicTotalCurr(); 
2459    if( pcSlice->getSliceType() == P_SLICE || pcSlice->getSliceType() == B_SLICE )
2460    {
2461      if( pps->getListsModificationPresentFlag() && numPicTotalCurr > 1 )
2462      {
2463        TComRefPicListModification* rplMod = pcSlice->getRefPicListModification();
2464        READ_FLAG( uiCode, "ref_pic_list_modification_flag_l0" ); rplMod->setRefPicListModificationFlagL0( uiCode == 1 );
2465        if( rplMod->getRefPicListModificationFlagL0() )
2466        {
2467          for( Int i = 0; i  <=  pcSlice->getNumRefIdxL0ActiveMinus1( ); i++ )
2468          {
2469            READ_CODE( rplMod->getListEntryLXLen( numPicTotalCurr ) , uiCode, "list_entry_l0" ); rplMod->setListEntryL0( i, uiCode );
2470          }
2471        }
2472
2473        if( pcSlice->getSliceType() == B_SLICE )
2474        {
2475          READ_FLAG( uiCode, "ref_pic_list_modification_flag_l1" ); rplMod->setRefPicListModificationFlagL1( uiCode == 1 );
2476          if( rplMod->getRefPicListModificationFlagL1() )
2477          {
2478            for( Int i = 0; i  <=  pcSlice->getNumRefIdxL1ActiveMinus1( ); i++ )
2479            {
2480              READ_CODE( rplMod->getListEntryLXLen( numPicTotalCurr ), uiCode, "list_entry_l1" ); rplMod->setListEntryL1( i, uiCode );
2481            }
2482          }
2483        }
2484      }
2485    }
2486#else
2487    TComRefPicListModification* refPicListModification = pcSlice->getRefPicListModification();
2488    if(!pcSlice->isIntra())
2489    {
2490      if( !pps->getListsModificationPresentFlag() || pcSlice->getNumRpsCurrTempList() <= 1 )
2491      {
2492        refPicListModification->setRefPicListModificationFlagL0( 0 );
2493      }
2494      else
2495      {
2496        READ_FLAG( uiCode, "ref_pic_list_modification_flag_l0" ); refPicListModification->setRefPicListModificationFlagL0( uiCode ? 1 : 0 );
2497      }
2498
2499      if(refPicListModification->getRefPicListModificationFlagL0())
2500      {
2501        uiCode = 0;
2502        Int i = 0;
2503        Int numRpsCurrTempList0 = pcSlice->getNumRpsCurrTempList();
2504        if ( numRpsCurrTempList0 > 1 )
2505        {
2506          Int length = 1;
2507          numRpsCurrTempList0 --;
2508          while ( numRpsCurrTempList0 >>= 1)
2509          {
2510            length ++;
2511          }
2512          for (i = 0; i < pcSlice->getNumRefIdx(REF_PIC_LIST_0); i ++)
2513          {
2514            READ_CODE( length, uiCode, "list_entry_l0" );
2515            refPicListModification->setRefPicSetIdxL0(i, uiCode );
2516          }
2517        }
2518        else
2519        {
2520          for (i = 0; i < pcSlice->getNumRefIdx(REF_PIC_LIST_0); i ++)
2521          {
2522            refPicListModification->setRefPicSetIdxL0(i, 0 );
2523          }
2524        }
2525      }
2526    }
2527    else
2528    {
2529      refPicListModification->setRefPicListModificationFlagL0(0);
2530    }
2531    if(pcSlice->isInterB())
2532    {
2533      if( !pps->getListsModificationPresentFlag() || pcSlice->getNumRpsCurrTempList() <= 1 )
2534      {
2535        refPicListModification->setRefPicListModificationFlagL1( 0 );
2536      }
2537      else
2538      {
2539        READ_FLAG( uiCode, "ref_pic_list_modification_flag_l1" ); refPicListModification->setRefPicListModificationFlagL1( uiCode ? 1 : 0 );
2540      }
2541      if(refPicListModification->getRefPicListModificationFlagL1())
2542      {
2543        uiCode = 0;
2544        Int i = 0;
2545        Int numRpsCurrTempList1 = pcSlice->getNumRpsCurrTempList();
2546        if ( numRpsCurrTempList1 > 1 )
2547        {
2548          Int length = 1;
2549          numRpsCurrTempList1 --;
2550          while ( numRpsCurrTempList1 >>= 1)
2551          {
2552            length ++;
2553          }
2554          for (i = 0; i < pcSlice->getNumRefIdx(REF_PIC_LIST_1); i ++)
2555          {
2556            READ_CODE( length, uiCode, "list_entry_l1" );
2557            refPicListModification->setRefPicSetIdxL1(i, uiCode );
2558          }
2559        }
2560        else
2561        {
2562          for (i = 0; i < pcSlice->getNumRefIdx(REF_PIC_LIST_1); i ++)
2563          {
2564            refPicListModification->setRefPicSetIdxL1(i, 0 );
2565          }
2566        }
2567      }
2568    }
2569    else
2570    {
2571      refPicListModification->setRefPicListModificationFlagL1(0);
2572    }
2573#endif
2574
2575    if (pcSlice->isInterB())
2576    {
2577      READ_FLAG( uiCode, "mvd_l1_zero_flag" );       pcSlice->setMvdL1ZeroFlag( (uiCode ? true : false) );
2578    }
2579
2580    pcSlice->setCabacInitFlag( false ); // default
2581    if(pps->getCabacInitPresentFlag() && !pcSlice->isIntra())
2582    {
2583      READ_FLAG(uiCode, "cabac_init_flag");
2584      pcSlice->setCabacInitFlag( uiCode ? true : false );
2585    }
2586
2587    if ( pcSlice->getEnableTMVPFlag() )
2588    {
2589      if ( pcSlice->getSliceType() == B_SLICE )
2590      {
2591        READ_FLAG( uiCode, "collocated_from_l0_flag" );
2592        pcSlice->setColFromL0Flag(uiCode);
2593      }
2594      else
2595      {
2596        pcSlice->setColFromL0Flag( 1 );
2597      }
2598
2599      if ( pcSlice->getSliceType() != I_SLICE &&
2600          ((pcSlice->getColFromL0Flag() == 1 && pcSlice->getNumRefIdx(REF_PIC_LIST_0) > 1)||
2601           (pcSlice->getColFromL0Flag() == 0 && pcSlice->getNumRefIdx(REF_PIC_LIST_1) > 1)))
2602      {
2603        READ_UVLC( uiCode, "collocated_ref_idx" );
2604        pcSlice->setColRefIdx(uiCode);
2605      }
2606      else
2607      {
2608        pcSlice->setColRefIdx(0);
2609      }
2610    }
2611    if ( (pps->getUseWP() && pcSlice->getSliceType()==P_SLICE) || (pps->getWPBiPred() && pcSlice->getSliceType()==B_SLICE) )
2612    {
2613      xParsePredWeightTable(pcSlice, sps);
2614      pcSlice->initWpScaling(sps);
2615    }
2616
2617
2618    if (!pcSlice->isIntra())
2619    {
2620      READ_UVLC( uiCode, "five_minus_max_num_merge_cand");
2621      pcSlice->setMaxNumMergeCand(MRG_MAX_NUM_CANDS - uiCode);
2622    }
2623
2624    READ_SVLC( iCode, "slice_qp_delta" );
2625    pcSlice->setSliceQp (26 + pps->getPicInitQPMinus26() + iCode);
2626
2627    assert( pcSlice->getSliceQp() >= -sps->getQpBDOffset(CHANNEL_TYPE_LUMA) );
2628    assert( pcSlice->getSliceQp() <=  51 );
2629
2630    if (pps->getSliceChromaQpFlag())
2631    {
2632      if (numValidComp>COMPONENT_Cb)
2633      {
2634        READ_SVLC( iCode, "slice_cb_qp_offset" );
2635        pcSlice->setSliceChromaQpDelta(COMPONENT_Cb, iCode );
2636        assert( pcSlice->getSliceChromaQpDelta(COMPONENT_Cb) >= -12 );
2637        assert( pcSlice->getSliceChromaQpDelta(COMPONENT_Cb) <=  12 );
2638        assert( (pps->getQpOffset(COMPONENT_Cb) + pcSlice->getSliceChromaQpDelta(COMPONENT_Cb)) >= -12 );
2639        assert( (pps->getQpOffset(COMPONENT_Cb) + pcSlice->getSliceChromaQpDelta(COMPONENT_Cb)) <=  12 );
2640      }
2641
2642      if (numValidComp>COMPONENT_Cr)
2643      {
2644        READ_SVLC( iCode, "slice_cr_qp_offset" );
2645        pcSlice->setSliceChromaQpDelta(COMPONENT_Cr, iCode );
2646        assert( pcSlice->getSliceChromaQpDelta(COMPONENT_Cr) >= -12 );
2647        assert( pcSlice->getSliceChromaQpDelta(COMPONENT_Cr) <=  12 );
2648        assert( (pps->getQpOffset(COMPONENT_Cr) + pcSlice->getSliceChromaQpDelta(COMPONENT_Cr)) >= -12 );
2649        assert( (pps->getQpOffset(COMPONENT_Cr) + pcSlice->getSliceChromaQpDelta(COMPONENT_Cr)) <=  12 );
2650      }
2651    }
2652
2653    if (pps->getPpsRangeExtension().getChromaQpOffsetListEnabledFlag())
2654    {
2655      READ_FLAG(uiCode, "cu_chroma_qp_offset_enabled_flag"); pcSlice->setUseChromaQpAdj(uiCode != 0);
2656    }
2657    else
2658    {
2659      pcSlice->setUseChromaQpAdj(false);
2660    }
2661
2662    if (pps->getDeblockingFilterControlPresentFlag())
2663    {
2664      if(pps->getDeblockingFilterOverrideEnabledFlag())
2665      {
2666        READ_FLAG ( uiCode, "deblocking_filter_override_flag" );        pcSlice->setDeblockingFilterOverrideFlag(uiCode ? true : false);
2667      }
2668      else
2669      {
2670        pcSlice->setDeblockingFilterOverrideFlag(0);
2671      }
2672      if(pcSlice->getDeblockingFilterOverrideFlag())
2673      {
2674        READ_FLAG ( uiCode, "slice_disable_deblocking_filter_flag" );   pcSlice->setDeblockingFilterDisable(uiCode ? 1 : 0);
2675        if(!pcSlice->getDeblockingFilterDisable())
2676        {
2677          READ_SVLC( iCode, "slice_beta_offset_div2" );                       pcSlice->setDeblockingFilterBetaOffsetDiv2(iCode);
2678          assert(pcSlice->getDeblockingFilterBetaOffsetDiv2() >= -6 &&
2679                 pcSlice->getDeblockingFilterBetaOffsetDiv2() <=  6);
2680          READ_SVLC( iCode, "slice_tc_offset_div2" );                         pcSlice->setDeblockingFilterTcOffsetDiv2(iCode);
2681          assert(pcSlice->getDeblockingFilterTcOffsetDiv2() >= -6 &&
2682                 pcSlice->getDeblockingFilterTcOffsetDiv2() <=  6);
2683        }
2684      }
2685      else
2686      {
2687        pcSlice->setDeblockingFilterDisable   ( pps->getPicDisableDeblockingFilterFlag() );
2688        pcSlice->setDeblockingFilterBetaOffsetDiv2( pps->getDeblockingFilterBetaOffsetDiv2() );
2689        pcSlice->setDeblockingFilterTcOffsetDiv2  ( pps->getDeblockingFilterTcOffsetDiv2() );
2690      }
2691    }
2692    else
2693    {
2694      pcSlice->setDeblockingFilterDisable       ( false );
2695      pcSlice->setDeblockingFilterBetaOffsetDiv2( 0 );
2696      pcSlice->setDeblockingFilterTcOffsetDiv2  ( 0 );
2697    }
2698
2699    Bool isSAOEnabled = sps->getUseSAO() && (pcSlice->getSaoEnabledFlag(CHANNEL_TYPE_LUMA) || (bChroma && pcSlice->getSaoEnabledFlag(CHANNEL_TYPE_CHROMA)));
2700    Bool isDBFEnabled = (!pcSlice->getDeblockingFilterDisable());
2701
2702    if(pps->getLoopFilterAcrossSlicesEnabledFlag() && ( isSAOEnabled || isDBFEnabled ))
2703    {
2704      READ_FLAG( uiCode, "slice_loop_filter_across_slices_enabled_flag");
2705    }
2706    else
2707    {
2708      uiCode = pps->getLoopFilterAcrossSlicesEnabledFlag()?1:0;
2709    }
2710    pcSlice->setLFCrossSliceBoundaryFlag( (uiCode==1)?true:false);
2711
2712
2713  }
2714
2715  std::vector<UInt> entryPointOffset;
2716  if( pps->getTilesEnabledFlag() || pps->getEntropyCodingSyncEnabledFlag() )
2717  {
2718    UInt numEntryPointOffsets;
2719    UInt offsetLenMinus1;
2720    READ_UVLC(numEntryPointOffsets, "num_entry_point_offsets");
2721    if (numEntryPointOffsets>0)
2722    {
2723      READ_UVLC(offsetLenMinus1, "offset_len_minus1");
2724      entryPointOffset.resize(numEntryPointOffsets);
2725      for (UInt idx=0; idx<numEntryPointOffsets; idx++)
2726      {
2727        READ_CODE(offsetLenMinus1+1, uiCode, "entry_point_offset_minus1");
2728        entryPointOffset[ idx ] = uiCode + 1;
2729      }
2730    }
2731  }
2732
2733  if(pps->getSliceHeaderExtensionPresentFlag())
2734  {
2735#if NH_MV
2736    READ_UVLC( uiCode, "slice_segment_header_extension_length" ); pcSlice->setSliceSegmentHeaderExtensionLength( uiCode );
2737    UInt posFollSliceSegHeaderExtLen = m_pcBitstream->getNumBitsRead();
2738
2739    if( pps->getPocResetInfoPresentFlag() )
2740    {
2741      READ_CODE( 2, uiCode, "poc_reset_idc" ); pcSlice->setPocResetIdc( uiCode );
2742    }
2743    else
2744    {
2745      pcSlice->setPocResetIdc( 0 );
2746    }
2747    pcSlice->checkPocResetIdc(); 
2748
2749#if NH_MV
2750    if ( pcSlice->getVPS()->getPocLsbNotPresentFlag(pcSlice->getLayerId()) && pcSlice->getSlicePicOrderCntLsb() > 0 )
2751#else
2752    if ( pcSlice->getVPS()->getPocLsbNotPresentFlag(pcSlice->getLayerId()) && slicePicOrderCntLsb > 0 )
2753#endif
2754    {
2755      assert( pcSlice->getPocResetIdc() != 2 );
2756    }
2757
2758    if( pcSlice->getPocResetIdc() !=  0 )
2759    {
2760      READ_CODE( 6, uiCode, "poc_reset_period_id" ); pcSlice->setPocResetPeriodId( uiCode );     
2761      pcSlice->setHasPocResetPeriodIdPresent( true );
2762    }
2763    else
2764    {
2765      // TODO Copy poc_reset_period from earlier picture
2766#if NH_MV
2767      pcSlice->setHasPocResetPeriodIdPresent( false);
2768#else
2769      pcSlice->setPocResetPeriodId( 0 );
2770#endif
2771    }
2772   
2773    if( pcSlice->getPocResetIdc() ==  3 ) 
2774    {
2775      READ_FLAG( uiCode, "full_poc_reset_flag" ); pcSlice->setFullPocResetFlag( uiCode == 1 );
2776      READ_CODE( pcSlice->getPocLsbValLen() , uiCode, "poc_lsb_val" ); pcSlice->setPocLsbVal( uiCode );
2777    }         
2778    pcSlice->checkPocLsbVal();       
2779
2780    if( !pcSlice->getPocMsbValRequiredFlag() && pcSlice->getVPS()->getVpsPocLsbAlignedFlag() )
2781    {
2782#if NH_MV
2783      READ_FLAG( uiCode, "poc_msb_cycle_val_present_flag" ); pcSlice->setPocMsbCycleValPresentFlag( uiCode == 1 );
2784#else
2785      READ_FLAG( uiCode, "poc_msb_val_present_flag" ); pcSlice->setPocMsbValPresentFlag( uiCode == 1 );
2786#endif
2787    }
2788    else
2789    {
2790#if NH_MV
2791      pcSlice->setPocMsbCycleValPresentFlag( pcSlice->inferPocMsbCycleValPresentFlag( ) ); 
2792#else
2793      pcSlice->setPocMsbValPresentFlag( pcSlice->inferPocMsbValPresentFlag( ) ); 
2794#endif
2795    }
2796
2797   
2798#if NH_MV
2799    if( pcSlice->getPocMsbCycleValPresentFlag() )
2800#else
2801    if( pcSlice->getPocMsbValPresentFlag() )
2802#endif
2803    {
2804#if NH_MV
2805      READ_UVLC( uiCode, "poc_msb_val" ); pcSlice->setPocMsbCycleVal( uiCode );
2806#else
2807      READ_UVLC( uiCode, "poc_msb_val" ); pcSlice->setPocMsbVal( uiCode );
2808#endif
2809    }
2810
2811    while( ( m_pcBitstream->getNumBitsRead() - posFollSliceSegHeaderExtLen ) < pcSlice->getSliceSegmentHeaderExtensionLength() * 8 )
2812    {
2813     READ_FLAG( uiCode, "slice_segment_header_extension_data_bit" );
2814    }
2815    assert( ( m_pcBitstream->getNumBitsRead() - posFollSliceSegHeaderExtLen ) == pcSlice->getSliceSegmentHeaderExtensionLength() * 8  ); 
2816  }
2817#else
2818
2819    READ_UVLC(uiCode,"slice_segment_header_extension_length");
2820    for(Int i=0; i<uiCode; i++)
2821    {
2822      UInt ignore;
2823      READ_CODE(8,ignore,"slice_segment_header_extension_data_byte");
2824    }
2825  }
2826#endif
2827#if RExt__DECODER_DEBUG_BIT_STATISTICS
2828  TComCodingStatistics::IncrementStatisticEP(STATS__BYTE_ALIGNMENT_BITS,m_pcBitstream->readByteAlignment(),0);
2829#else
2830  m_pcBitstream->readByteAlignment();
2831#endif
2832
2833  pcSlice->clearSubstreamSizes();
2834
2835  if( pps->getTilesEnabledFlag() || pps->getEntropyCodingSyncEnabledFlag() )
2836  {
2837    Int endOfSliceHeaderLocation = m_pcBitstream->getByteLocation();
2838
2839    // Adjust endOfSliceHeaderLocation to account for emulation prevention bytes in the slice segment header
2840    for ( UInt curByteIdx  = 0; curByteIdx<m_pcBitstream->numEmulationPreventionBytesRead(); curByteIdx++ )
2841    {
2842      if ( m_pcBitstream->getEmulationPreventionByteLocation( curByteIdx ) < endOfSliceHeaderLocation )
2843      {
2844        endOfSliceHeaderLocation++;
2845      }
2846    }
2847
2848    Int  curEntryPointOffset     = 0;
2849    Int  prevEntryPointOffset    = 0;
2850    for (UInt idx=0; idx<entryPointOffset.size(); idx++)
2851    {
2852      curEntryPointOffset += entryPointOffset[ idx ];
2853
2854      Int emulationPreventionByteCount = 0;
2855      for ( UInt curByteIdx  = 0; curByteIdx<m_pcBitstream->numEmulationPreventionBytesRead(); curByteIdx++ )
2856      {
2857        if ( m_pcBitstream->getEmulationPreventionByteLocation( curByteIdx ) >= ( prevEntryPointOffset + endOfSliceHeaderLocation ) &&
2858             m_pcBitstream->getEmulationPreventionByteLocation( curByteIdx ) <  ( curEntryPointOffset  + endOfSliceHeaderLocation ) )
2859        {
2860          emulationPreventionByteCount++;
2861        }
2862      }
2863
2864      entryPointOffset[ idx ] -= emulationPreventionByteCount;
2865      prevEntryPointOffset = curEntryPointOffset;
2866      pcSlice->addSubstreamSize(entryPointOffset [ idx ] );
2867    }
2868  }
2869
2870  return;
2871}
2872
2873Void TDecCavlc::parsePTL( TComPTL *rpcPTL, Bool profilePresentFlag, Int maxNumSubLayersMinus1 )
2874{
2875  UInt uiCode;
2876  if(profilePresentFlag)
2877  {
2878    parseProfileTier(rpcPTL->getGeneralPTL(), false);
2879  }
2880  READ_CODE( 8, uiCode, "general_level_idc" );    rpcPTL->getGeneralPTL()->setLevelIdc(Level::Name(uiCode));
2881
2882  for (Int i = 0; i < maxNumSubLayersMinus1; i++)
2883  {
2884    READ_FLAG( uiCode, "sub_layer_profile_present_flag[i]" ); rpcPTL->setSubLayerProfilePresentFlag(i, uiCode);
2885    READ_FLAG( uiCode, "sub_layer_level_present_flag[i]"   ); rpcPTL->setSubLayerLevelPresentFlag  (i, uiCode);
2886#if NH_MV
2887    // When profilePresentFlag is equal to 0, sub_layer_profile_present_flag[ i ] shall be equal to 0.
2888    assert( profilePresentFlag || !rpcPTL->getSubLayerProfilePresentFlag(i) );
2889#endif
2890  }
2891
2892  if (maxNumSubLayersMinus1 > 0)
2893  {
2894    for (Int i = maxNumSubLayersMinus1; i < 8; i++)
2895    {
2896      READ_CODE(2, uiCode, "reserved_zero_2bits");
2897      assert(uiCode == 0);
2898    }
2899  }
2900
2901  for(Int i = 0; i < maxNumSubLayersMinus1; i++)
2902  {
2903    if( rpcPTL->getSubLayerProfilePresentFlag(i) )         
2904    {
2905      parseProfileTier(rpcPTL->getSubLayerPTL(i), true);
2906    }
2907    if(rpcPTL->getSubLayerLevelPresentFlag(i))
2908    {
2909      READ_CODE( 8, uiCode, "sub_layer_level_idc[i]" );   rpcPTL->getSubLayerPTL(i)->setLevelIdc(Level::Name(uiCode));
2910    }
2911  }
2912}
2913
2914#if ENC_DEC_TRACE || RExt__DECODER_DEBUG_BIT_STATISTICS
2915Void TDecCavlc::parseProfileTier(ProfileTierLevel *ptl, const Bool bIsSubLayer)
2916#define PTL_TRACE_TEXT(txt) bIsSubLayer?("sub_layer_" txt) : ("general_" txt)
2917#else
2918Void TDecCavlc::parseProfileTier(ProfileTierLevel *ptl, const Bool /*bIsSubLayer*/)
2919#define PTL_TRACE_TEXT(txt) txt
2920#endif
2921{
2922  UInt uiCode;
2923  READ_CODE(2 , uiCode,   PTL_TRACE_TEXT("profile_space"                   )); ptl->setProfileSpace(uiCode);
2924  READ_FLAG(    uiCode,   PTL_TRACE_TEXT("tier_flag"                       )); ptl->setTierFlag    (uiCode ? Level::HIGH : Level::MAIN);
2925  READ_CODE(5 , uiCode,   PTL_TRACE_TEXT("profile_idc"                     )); ptl->setProfileIdc  (Profile::Name(uiCode));
2926  for(Int j = 0; j < 32; j++)
2927  {
2928    READ_FLAG(  uiCode,   PTL_TRACE_TEXT("profile_compatibility_flag[][j]" )); ptl->setProfileCompatibilityFlag(j, uiCode ? 1 : 0);
2929  }
2930  READ_FLAG(uiCode,       PTL_TRACE_TEXT("progressive_source_flag"         )); ptl->setProgressiveSourceFlag(uiCode ? true : false);
2931
2932  READ_FLAG(uiCode,       PTL_TRACE_TEXT("interlaced_source_flag"          )); ptl->setInterlacedSourceFlag(uiCode ? true : false);
2933
2934  READ_FLAG(uiCode,       PTL_TRACE_TEXT("non_packed_constraint_flag"      )); ptl->setNonPackedConstraintFlag(uiCode ? true : false);
2935
2936  READ_FLAG(uiCode,       PTL_TRACE_TEXT("frame_only_constraint_flag"      )); ptl->setFrameOnlyConstraintFlag(uiCode ? true : false);
2937#if NH_MV
2938  if( ptl->getV2ConstraintsPresentFlag() )
2939  {
2940    READ_FLAG( uiCode, "max_12bit_constraint_flag" );        ptl->setMax12bitConstraintFlag      ( uiCode == 1 );
2941    READ_FLAG( uiCode, "max_10bit_constraint_flag" );        ptl->setMax10bitConstraintFlag      ( uiCode == 1 );
2942    READ_FLAG( uiCode, "max_8bit_constraint_flag" );         ptl->setMax8bitConstraintFlag       ( uiCode == 1 );
2943    READ_FLAG( uiCode, "max_422chroma_constraint_flag" );    ptl->setMax422chromaConstraintFlag  ( uiCode == 1 );
2944    READ_FLAG( uiCode, "max_420chroma_constraint_flag" );    ptl->setMax420chromaConstraintFlag  ( uiCode == 1 );
2945    READ_FLAG( uiCode, "max_monochrome_constraint_flag" );   ptl->setMaxMonochromeConstraintFlag ( uiCode == 1 );
2946    READ_FLAG( uiCode, "intra_constraint_flag" );            ptl->setIntraConstraintFlag         ( uiCode == 1 );
2947    READ_FLAG( uiCode, "one_picture_only_constraint_flag" ); ptl->setOnePictureOnlyConstraintFlag( uiCode == 1 );
2948    READ_FLAG( uiCode, "lower_bit_rate_constraint_flag" );   ptl->setLowerBitRateConstraintFlag  ( uiCode == 1 );   
2949    READ_CODE(16, uiCode, "XXX_reserved_zero_34bits[0..15]");
2950    READ_CODE(16, uiCode, "XXX_reserved_zero_34bits[16..31]");
2951    READ_CODE(2 , uiCode, "XXX_reserved_zero_34bits[32..33]");
2952  }
2953  else
2954  {
2955    READ_CODE(16, uiCode, "XXX_reserved_zero_43bits[0..15]");
2956    READ_CODE(16, uiCode, "XXX_reserved_zero_43bits[16..31]");
2957    READ_CODE(11, uiCode, "XXX_reserved_zero_43bits[32..42]");
2958  }
2959  if( ptl->getInbldPresentFlag() )
2960  {
2961    READ_FLAG( uiCode, "inbld_flag" ); ptl->setInbldFlag( uiCode == 1 );
2962  }
2963  else
2964  {
2965    READ_FLAG(uiCode, "reserved_zero_bit");
2966  }
2967#else
2968
2969  if (ptl->getProfileIdc() == Profile::MAINREXT           || ptl->getProfileCompatibilityFlag(Profile::MAINREXT) ||
2970      ptl->getProfileIdc() == Profile::HIGHTHROUGHPUTREXT || ptl->getProfileCompatibilityFlag(Profile::HIGHTHROUGHPUTREXT))
2971  {
2972    UInt maxBitDepth=16;
2973    READ_FLAG(    uiCode, PTL_TRACE_TEXT("max_12bit_constraint_flag"       )); if (uiCode) maxBitDepth=12;
2974    READ_FLAG(    uiCode, PTL_TRACE_TEXT("max_10bit_constraint_flag"       )); if (uiCode) maxBitDepth=10;
2975    READ_FLAG(    uiCode, PTL_TRACE_TEXT("max_8bit_constraint_flag"        )); if (uiCode) maxBitDepth=8;
2976    ptl->setBitDepthConstraint(maxBitDepth);
2977    ChromaFormat chromaFmtConstraint=CHROMA_444;
2978    READ_FLAG(    uiCode, PTL_TRACE_TEXT("max_422chroma_constraint_flag"   )); if (uiCode) chromaFmtConstraint=CHROMA_422;
2979    READ_FLAG(    uiCode, PTL_TRACE_TEXT("max_420chroma_constraint_flag"   )); if (uiCode) chromaFmtConstraint=CHROMA_420;
2980    READ_FLAG(    uiCode, PTL_TRACE_TEXT("max_monochrome_constraint_flag"  )); if (uiCode) chromaFmtConstraint=CHROMA_400;
2981    ptl->setChromaFormatConstraint(chromaFmtConstraint);
2982    READ_FLAG(    uiCode, PTL_TRACE_TEXT("intra_constraint_flag"           )); ptl->setIntraConstraintFlag(uiCode != 0);
2983    READ_FLAG(    uiCode, PTL_TRACE_TEXT("one_picture_only_constraint_flag")); ptl->setOnePictureOnlyConstraintFlag(uiCode != 0);
2984    READ_FLAG(    uiCode, PTL_TRACE_TEXT("lower_bit_rate_constraint_flag"  )); ptl->setLowerBitRateConstraintFlag(uiCode != 0);
2985    READ_CODE(16, uiCode, PTL_TRACE_TEXT("reserved_zero_34bits[0..15]"     ));
2986    READ_CODE(16, uiCode, PTL_TRACE_TEXT("reserved_zero_34bits[16..31]"    ));
2987    READ_CODE(2,  uiCode, PTL_TRACE_TEXT("reserved_zero_34bits[32..33]"    ));
2988  }
2989  else
2990  {
2991    ptl->setBitDepthConstraint((ptl->getProfileIdc() == Profile::MAIN10)?10:8);
2992    ptl->setChromaFormatConstraint(CHROMA_420);
2993    ptl->setIntraConstraintFlag(false);
2994    ptl->setLowerBitRateConstraintFlag(true);
2995    READ_CODE(16, uiCode, PTL_TRACE_TEXT("reserved_zero_43bits[0..15]"     ));
2996    READ_CODE(16, uiCode, PTL_TRACE_TEXT("reserved_zero_43bits[16..31]"    ));
2997    READ_CODE(11, uiCode, PTL_TRACE_TEXT("reserved_zero_43bits[32..42]"    ));
2998  }
2999
3000  if ((ptl->getProfileIdc() >= Profile::MAIN && ptl->getProfileIdc() <= Profile::HIGHTHROUGHPUTREXT) ||
3001       ptl->getProfileCompatibilityFlag(Profile::MAIN) ||
3002       ptl->getProfileCompatibilityFlag(Profile::MAIN10) ||
3003       ptl->getProfileCompatibilityFlag(Profile::MAINSTILLPICTURE) ||
3004       ptl->getProfileCompatibilityFlag(Profile::MAINREXT) ||
3005       ptl->getProfileCompatibilityFlag(Profile::HIGHTHROUGHPUTREXT) )
3006  {
3007    READ_FLAG(    uiCode, PTL_TRACE_TEXT("inbld_flag"                      )); assert(uiCode == 0);
3008  }
3009  else
3010  {
3011    READ_FLAG(    uiCode, PTL_TRACE_TEXT("reserved_zero_bit"               ));
3012  }
3013#undef PTL_TRACE_TEXT
3014#endif
3015}
3016
3017Void TDecCavlc::parseTerminatingBit( UInt& ruiBit )
3018{
3019  ruiBit = false;
3020  Int iBitsLeft = m_pcBitstream->getNumBitsLeft();
3021  if(iBitsLeft <= 8)
3022  {
3023    UInt uiPeekValue = m_pcBitstream->peekBits(iBitsLeft);
3024    if (uiPeekValue == (1<<(iBitsLeft-1)))
3025    {
3026      ruiBit = true;
3027    }
3028  }
3029}
3030
3031Void TDecCavlc::parseRemainingBytes( Bool noTrailingBytesExpected )
3032{
3033  if (noTrailingBytesExpected)
3034  {
3035    const UInt numberOfRemainingSubstreamBytes=m_pcBitstream->getNumBitsLeft();
3036    assert (numberOfRemainingSubstreamBytes == 0);
3037  }
3038  else
3039  {
3040    while (m_pcBitstream->getNumBitsLeft())
3041    {
3042      UInt trailingNullByte=m_pcBitstream->readByte();
3043      if (trailingNullByte!=0)
3044      {
3045        printf("Trailing byte should be 0, but has value %02x\n", trailingNullByte);
3046        assert(trailingNullByte==0);
3047      }
3048    }
3049  }
3050}
3051
3052
3053Void TDecCavlc::parseSkipFlag( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
3054{
3055  assert(0);
3056}
3057
3058Void TDecCavlc::parseCUTransquantBypassFlag( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
3059{
3060  assert(0);
3061}
3062
3063Void TDecCavlc::parseMVPIdx( Int& /*riMVPIdx*/ )
3064{
3065  assert(0);
3066}
3067
3068Void TDecCavlc::parseSplitFlag     ( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
3069{
3070  assert(0);
3071}
3072
3073Void TDecCavlc::parsePartSize( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
3074{
3075  assert(0);
3076}
3077
3078Void TDecCavlc::parsePredMode( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
3079{
3080  assert(0);
3081}
3082
3083/** Parse I_PCM information.
3084* \param pcCU pointer to CU
3085* \param uiAbsPartIdx CU index
3086* \param uiDepth CU depth
3087* \returns Void
3088*
3089* If I_PCM flag indicates that the CU is I_PCM, parse its PCM alignment bits and codes.
3090*/
3091Void TDecCavlc::parseIPCMInfo( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
3092{
3093  assert(0);
3094}
3095
3096Void TDecCavlc::parseIntraDirLumaAng  ( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
3097{
3098  assert(0);
3099}
3100
3101Void TDecCavlc::parseIntraDirChroma( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
3102{
3103  assert(0);
3104}
3105
3106Void TDecCavlc::parseInterDir( TComDataCU* /*pcCU*/, UInt& /*ruiInterDir*/, UInt /*uiAbsPartIdx*/ )
3107{
3108  assert(0);
3109}
3110
3111Void TDecCavlc::parseRefFrmIdx( TComDataCU* /*pcCU*/, Int& /*riRefFrmIdx*/, RefPicList /*eRefList*/ )
3112{
3113  assert(0);
3114}
3115
3116Void TDecCavlc::parseMvd( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiPartIdx*/, UInt /*uiDepth*/, RefPicList /*eRefList*/ )
3117{
3118  assert(0);
3119}
3120
3121Void TDecCavlc::parseCrossComponentPrediction( class TComTU& /*rTu*/, ComponentID /*compID*/ )
3122{
3123  assert(0);
3124}
3125
3126Void TDecCavlc::parseDeltaQP( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
3127{
3128  Int  iDQp;
3129
3130#if RExt__DECODER_DEBUG_BIT_STATISTICS
3131  READ_SVLC(iDQp, "delta_qp");
3132#else
3133  xReadSvlc( iDQp );
3134#endif
3135
3136  Int qpBdOffsetY = pcCU->getSlice()->getSPS()->getQpBDOffset(CHANNEL_TYPE_LUMA);
3137  const Int qp = (((Int) pcCU->getRefQP( uiAbsPartIdx ) + iDQp + 52 + 2*qpBdOffsetY )%(52+ qpBdOffsetY)) -  qpBdOffsetY;
3138
3139  const UInt maxCUDepth        = pcCU->getSlice()->getSPS()->getMaxTotalCUDepth();
3140  const UInt maxCuDQPDepth     = pcCU->getSlice()->getPPS()->getMaxCuDQPDepth();
3141  const UInt doubleDepthDifference = ((maxCUDepth - maxCuDQPDepth)<<1);
3142  const UInt uiAbsQpCUPartIdx = (uiAbsPartIdx>>doubleDepthDifference)<<doubleDepthDifference ;
3143  const UInt uiQpCUDepth =   min(uiDepth,pcCU->getSlice()->getPPS()->getMaxCuDQPDepth()) ;
3144
3145  pcCU->setQPSubParts( qp, uiAbsQpCUPartIdx, uiQpCUDepth );
3146}
3147
3148Void TDecCavlc::parseChromaQpAdjustment( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
3149{
3150  assert(0);
3151}
3152
3153Void TDecCavlc::parseCoeffNxN( TComTU &/*rTu*/, ComponentID /*compID*/ )
3154{
3155  assert(0);
3156}
3157
3158Void TDecCavlc::parseTransformSubdivFlag( UInt& /*ruiSubdivFlag*/, UInt /*uiLog2TransformBlockSize*/ )
3159{
3160  assert(0);
3161}
3162
3163Void TDecCavlc::parseQtCbf( TComTU &/*rTu*/, const ComponentID /*compID*/, const Bool /*lowestLevel*/ )
3164{
3165  assert(0);
3166}
3167
3168Void TDecCavlc::parseQtRootCbf( UInt /*uiAbsPartIdx*/, UInt& /*uiQtRootCbf*/ )
3169{
3170  assert(0);
3171}
3172
3173Void TDecCavlc::parseTransformSkipFlags (TComTU &/*rTu*/, ComponentID /*component*/)
3174{
3175  assert(0);
3176}
3177
3178Void TDecCavlc::parseMergeFlag ( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/, UInt /*uiPUIdx*/ )
3179{
3180  assert(0);
3181}
3182
3183Void TDecCavlc::parseMergeIndex ( TComDataCU* /*pcCU*/, UInt& /*ruiMergeIndex*/ )
3184{
3185  assert(0);
3186}
3187
3188// ====================================================================================================================
3189// Protected member functions
3190// ====================================================================================================================
3191
3192//! parse explicit wp tables
3193Void TDecCavlc::xParsePredWeightTable( TComSlice* pcSlice, const TComSPS *sps )
3194{
3195        WPScalingParam *wp;
3196  const ChromaFormat    chFmt        = sps->getChromaFormatIdc();
3197  const Int             numValidComp = Int(getNumberValidComponents(chFmt));
3198  const Bool            bChroma      = (chFmt!=CHROMA_400);
3199  const SliceType       eSliceType   = pcSlice->getSliceType();
3200  const Int             iNbRef       = (eSliceType == B_SLICE ) ? (2) : (1);
3201        UInt            uiLog2WeightDenomLuma=0, uiLog2WeightDenomChroma=0;
3202        UInt            uiTotalSignalledWeightFlags = 0;
3203
3204  Int iDeltaDenom;
3205  // decode delta_luma_log2_weight_denom :
3206  READ_UVLC( uiLog2WeightDenomLuma, "luma_log2_weight_denom" );
3207  assert( uiLog2WeightDenomLuma <= 7 );
3208  if( bChroma )
3209  {
3210    READ_SVLC( iDeltaDenom, "delta_chroma_log2_weight_denom" );
3211    assert((iDeltaDenom + (Int)uiLog2WeightDenomLuma)>=0);
3212    assert((iDeltaDenom + (Int)uiLog2WeightDenomLuma)<=7);
3213    uiLog2WeightDenomChroma = (UInt)(iDeltaDenom + uiLog2WeightDenomLuma);
3214  }
3215
3216
3217
3218  for ( Int iNumRef=0 ; iNumRef<iNbRef ; iNumRef++ ) // loop over l0 and l1 syntax elements
3219  {
3220    RefPicList  eRefPicList = ( iNumRef ? REF_PIC_LIST_1 : REF_PIC_LIST_0 );
3221    for ( Int iRefIdx=0 ; iRefIdx<pcSlice->getNumRefIdx(eRefPicList) ; iRefIdx++ )
3222    {
3223      pcSlice->getWpScaling(eRefPicList, iRefIdx, wp);
3224
3225      wp[COMPONENT_Y].uiLog2WeightDenom = uiLog2WeightDenomLuma;
3226      for(Int j=1; j<numValidComp; j++)
3227      {
3228        wp[j].uiLog2WeightDenom = uiLog2WeightDenomChroma;
3229      }
3230
3231      UInt  uiCode;
3232      READ_FLAG( uiCode, iNumRef==0?"luma_weight_l0_flag[i]":"luma_weight_l1_flag[i]" );
3233      wp[COMPONENT_Y].bPresentFlag = ( uiCode == 1 );
3234      uiTotalSignalledWeightFlags += wp[COMPONENT_Y].bPresentFlag;
3235    }
3236    if ( bChroma )
3237    {
3238      UInt  uiCode;
3239      for ( Int iRefIdx=0 ; iRefIdx<pcSlice->getNumRefIdx(eRefPicList) ; iRefIdx++ )
3240      {
3241        pcSlice->getWpScaling(eRefPicList, iRefIdx, wp);
3242        READ_FLAG( uiCode, iNumRef==0?"chroma_weight_l0_flag[i]":"chroma_weight_l1_flag[i]" );
3243        for(Int j=1; j<numValidComp; j++)
3244        {
3245          wp[j].bPresentFlag = ( uiCode == 1 );
3246        }
3247        uiTotalSignalledWeightFlags += 2*wp[COMPONENT_Cb].bPresentFlag;
3248      }
3249    }
3250    for ( Int iRefIdx=0 ; iRefIdx<pcSlice->getNumRefIdx(eRefPicList) ; iRefIdx++ )
3251    {
3252      pcSlice->getWpScaling(eRefPicList, iRefIdx, wp);
3253      if ( wp[COMPONENT_Y].bPresentFlag )
3254      {
3255        Int iDeltaWeight;
3256        READ_SVLC( iDeltaWeight, iNumRef==0?"delta_luma_weight_l0[i]":"delta_luma_weight_l1[i]" );
3257        assert( iDeltaWeight >= -128 );
3258        assert( iDeltaWeight <=  127 );
3259        wp[COMPONENT_Y].iWeight = (iDeltaWeight + (1<<wp[COMPONENT_Y].uiLog2WeightDenom));
3260        READ_SVLC( wp[COMPONENT_Y].iOffset, iNumRef==0?"luma_offset_l0[i]":"luma_offset_l1[i]" );
3261        Int range=sps->getSpsRangeExtension().getHighPrecisionOffsetsEnabledFlag() ? (1<<sps->getBitDepth(CHANNEL_TYPE_LUMA))/2 : 128;
3262        assert( wp[0].iOffset >= -range );
3263        assert( wp[0].iOffset <   range );
3264      }
3265      else
3266      {
3267        wp[COMPONENT_Y].iWeight = (1 << wp[COMPONENT_Y].uiLog2WeightDenom);
3268        wp[COMPONENT_Y].iOffset = 0;
3269      }
3270      if ( bChroma )
3271      {
3272        if ( wp[COMPONENT_Cb].bPresentFlag )
3273        {
3274          Int range=sps->getSpsRangeExtension().getHighPrecisionOffsetsEnabledFlag() ? (1<<sps->getBitDepth(CHANNEL_TYPE_CHROMA))/2 : 128;
3275          for ( Int j=1 ; j<numValidComp ; j++ )
3276          {
3277            Int iDeltaWeight;
3278            READ_SVLC( iDeltaWeight, iNumRef==0?"delta_chroma_weight_l0[i]":"delta_chroma_weight_l1[i]" );
3279            assert( iDeltaWeight >= -128 );
3280            assert( iDeltaWeight <=  127 );
3281            wp[j].iWeight = (iDeltaWeight + (1<<wp[j].uiLog2WeightDenom));
3282
3283            Int iDeltaChroma;
3284            READ_SVLC( iDeltaChroma, iNumRef==0?"delta_chroma_offset_l0[i]":"delta_chroma_offset_l1[i]" );
3285            assert( iDeltaChroma >= -4*range);
3286            assert( iDeltaChroma <   4*range);
3287            Int pred = ( range - ( ( range*wp[j].iWeight)>>(wp[j].uiLog2WeightDenom) ) );
3288            wp[j].iOffset = Clip3(-range, range-1, (iDeltaChroma + pred) );
3289          }
3290        }
3291        else
3292        {
3293          for ( Int j=1 ; j<numValidComp ; j++ )
3294          {
3295            wp[j].iWeight = (1 << wp[j].uiLog2WeightDenom);
3296            wp[j].iOffset = 0;
3297          }
3298        }
3299      }
3300    }
3301
3302    for ( Int iRefIdx=pcSlice->getNumRefIdx(eRefPicList) ; iRefIdx<MAX_NUM_REF ; iRefIdx++ )
3303    {
3304      pcSlice->getWpScaling(eRefPicList, iRefIdx, wp);
3305
3306      wp[0].bPresentFlag = false;
3307      wp[1].bPresentFlag = false;
3308      wp[2].bPresentFlag = false;
3309    }
3310  }
3311  assert(uiTotalSignalledWeightFlags<=24);
3312}
3313
3314/** decode quantization matrix
3315* \param scalingList quantization matrix information
3316*/
3317Void TDecCavlc::parseScalingList(TComScalingList* scalingList)
3318{
3319  UInt  code, sizeId, listId;
3320  Bool scalingListPredModeFlag;
3321  //for each size
3322  for(sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
3323  {
3324    for(listId = 0; listId <  SCALING_LIST_NUM; listId++)
3325    {
3326      if ((sizeId==SCALING_LIST_32x32) && (listId%(SCALING_LIST_NUM/NUMBER_OF_PREDICTION_MODES) != 0))
3327      {
3328        Int *src = scalingList->getScalingListAddress(sizeId, listId);
3329        const Int size = min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId]);
3330        const Int *srcNextSmallerSize = scalingList->getScalingListAddress(sizeId-1, listId);
3331        for(Int i=0; i<size; i++)
3332        {
3333          src[i] = srcNextSmallerSize[i];
3334        }
3335        scalingList->setScalingListDC(sizeId,listId,(sizeId > SCALING_LIST_8x8) ? scalingList->getScalingListDC(sizeId-1, listId) : src[0]);
3336      }
3337      else
3338      {
3339        READ_FLAG( code, "scaling_list_pred_mode_flag");
3340        scalingListPredModeFlag = (code) ? true : false;
3341        scalingList->setScalingListPredModeFlag(sizeId, listId, scalingListPredModeFlag);
3342        if(!scalingListPredModeFlag) //Copy Mode
3343        {
3344          READ_UVLC( code, "scaling_list_pred_matrix_id_delta");
3345
3346          if (sizeId==SCALING_LIST_32x32)
3347          {
3348            code*=(SCALING_LIST_NUM/NUMBER_OF_PREDICTION_MODES); // Adjust the decoded code for this size, to cope with the missing 32x32 chroma entries.
3349          }
3350
3351          scalingList->setRefMatrixId (sizeId,listId,(UInt)((Int)(listId)-(code)));
3352          if( sizeId > SCALING_LIST_8x8 )
3353          {
3354            scalingList->setScalingListDC(sizeId,listId,((listId == scalingList->getRefMatrixId (sizeId,listId))? 16 :scalingList->getScalingListDC(sizeId, scalingList->getRefMatrixId (sizeId,listId))));
3355          }
3356          scalingList->processRefMatrix( sizeId, listId, scalingList->getRefMatrixId (sizeId,listId));
3357
3358        }
3359        else //DPCM Mode
3360        {
3361          xDecodeScalingList(scalingList, sizeId, listId);
3362        }
3363      }
3364    }
3365  }
3366
3367  return;
3368}
3369/** decode DPCM
3370* \param scalingList  quantization matrix information
3371* \param sizeId size index
3372* \param listId list index
3373*/
3374Void TDecCavlc::xDecodeScalingList(TComScalingList *scalingList, UInt sizeId, UInt listId)
3375{
3376  Int i,coefNum = min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId]);
3377  Int data;
3378  Int scalingListDcCoefMinus8 = 0;
3379  Int nextCoef = SCALING_LIST_START_VALUE;
3380  UInt* scan  = g_scanOrder[SCAN_UNGROUPED][SCAN_DIAG][sizeId==0 ? 2 : 3][sizeId==0 ? 2 : 3];
3381  Int *dst = scalingList->getScalingListAddress(sizeId, listId);
3382
3383  if( sizeId > SCALING_LIST_8x8 )
3384  {
3385    READ_SVLC( scalingListDcCoefMinus8, "scaling_list_dc_coef_minus8");
3386    scalingList->setScalingListDC(sizeId,listId,scalingListDcCoefMinus8 + 8);
3387    nextCoef = scalingList->getScalingListDC(sizeId,listId);
3388  }
3389
3390  for(i = 0; i < coefNum; i++)
3391  {
3392    READ_SVLC( data, "scaling_list_delta_coef");
3393    nextCoef = (nextCoef + data + 256 ) % 256;
3394    dst[scan[i]] = nextCoef;
3395  }
3396}
3397
3398Bool TDecCavlc::xMoreRbspData()
3399{
3400  Int bitsLeft = m_pcBitstream->getNumBitsLeft();
3401
3402  // if there are more than 8 bits, it cannot be rbsp_trailing_bits
3403  if (bitsLeft > 8)
3404  {
3405    return true;
3406  }
3407
3408  UChar lastByte = m_pcBitstream->peekBits(bitsLeft);
3409  Int cnt = bitsLeft;
3410
3411  // remove trailing bits equal to zero
3412  while ((cnt>0) && ((lastByte & 1) == 0))
3413  {
3414    lastByte >>= 1;
3415    cnt--;
3416  }
3417  // remove bit equal to one
3418  cnt--;
3419
3420  // we should not have a negative number of bits
3421  assert (cnt>=0);
3422
3423  // we have more data, if cnt is not zero
3424  return (cnt>0);
3425}
3426
3427Void TDecCavlc::parseExplicitRdpcmMode( TComTU& /*rTu*/, ComponentID /*compID*/ )
3428{
3429  assert(0);
3430}
3431//! \}
3432
Note: See TracBrowser for help on using the repository browser.