source: 3DVCSoftware/branches/HTM-16.0-dev1/source/Lib/TLibDecoder/TDecCAVLC.cpp @ 1394

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