source: SHVCSoftware/branches/SHM-dev/source/Lib/TLibDecoder/TDecCAVLC.cpp @ 1490

Last change on this file since 1490 was 1487, checked in by fujitsu, 10 years ago

Added support of scalable range extension profiles. Also corrected few issues such as the confWindow wrong initialization. Code is controlled by the macro SCALABLE_REXT.

  • Property svn:eol-style set to native
File size: 142.7 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 CGS_3D_ASYMLUT
46#include "../TLibCommon/TCom3DAsymLUT.h"
47#endif
48
49//! \ingroup TLibDecoder
50//! \{
51
52#if ENC_DEC_TRACE
53
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
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
93Void TDecCavlc::parseShortTermRefPicSet( TComSPS* sps, TComReferencePictureSet* rps, Int idx )
94{
95  UInt code;
96  UInt interRPSPred;
97  if (idx > 0)
98  {
99    READ_FLAG(interRPSPred, "inter_ref_pic_set_prediction_flag");  rps->setInterRPSPrediction(interRPSPred);
100  }
101  else
102  {
103    interRPSPred = false;
104    rps->setInterRPSPrediction(false);
105  }
106
107  if (interRPSPred)
108  {
109    UInt bit;
110    if(idx == sps->getRPSList()->getNumberOfReferencePictureSets())
111    {
112      READ_UVLC(code, "delta_idx_minus1" ); // delta index of the Reference Picture Set used for prediction minus 1
113    }
114    else
115    {
116      code = 0;
117    }
118    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
119    Int rIdx =  idx - 1 - code;
120    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
121    TComReferencePictureSet*   rpsRef = sps->getRPSList()->getReferencePictureSet(rIdx);
122    Int k = 0, k0 = 0, k1 = 0;
123    READ_CODE(1, bit, "delta_rps_sign"); // delta_RPS_sign
124    READ_UVLC(code, "abs_delta_rps_minus1");  // absolute delta RPS minus 1
125    Int deltaRPS = (1 - 2 * bit) * (code + 1); // delta_RPS
126    for(Int j=0 ; j <= rpsRef->getNumberOfPictures(); j++)
127    {
128      READ_CODE(1, bit, "used_by_curr_pic_flag" ); //first bit is "1" if Idc is 1
129      Int refIdc = bit;
130      if (refIdc == 0)
131      {
132        READ_CODE(1, bit, "use_delta_flag" ); //second bit is "1" if Idc is 2, "0" otherwise.
133        refIdc = bit<<1; //second bit is "1" if refIdc is 2, "0" if refIdc = 0.
134      }
135      if (refIdc == 1 || refIdc == 2)
136      {
137        Int deltaPOC = deltaRPS + ((j < rpsRef->getNumberOfPictures())? rpsRef->getDeltaPOC(j) : 0);
138        rps->setDeltaPOC(k, deltaPOC);
139        rps->setUsed(k, (refIdc == 1));
140
141        if (deltaPOC < 0)
142        {
143          k0++;
144        }
145        else
146        {
147          k1++;
148        }
149        k++;
150      }
151      rps->setRefIdc(j,refIdc);
152    }
153    rps->setNumRefIdc(rpsRef->getNumberOfPictures()+1);
154    rps->setNumberOfPictures(k);
155    rps->setNumberOfNegativePictures(k0);
156    rps->setNumberOfPositivePictures(k1);
157    rps->sortDeltaPOC();
158  }
159  else
160  {
161    READ_UVLC(code, "num_negative_pics");           rps->setNumberOfNegativePictures(code);
162    READ_UVLC(code, "num_positive_pics");           rps->setNumberOfPositivePictures(code);
163    Int prev = 0;
164    Int poc;
165    for(Int j=0 ; j < rps->getNumberOfNegativePictures(); j++)
166    {
167      READ_UVLC(code, "delta_poc_s0_minus1");
168      poc = prev-code-1;
169      prev = poc;
170      rps->setDeltaPOC(j,poc);
171      READ_FLAG(code, "used_by_curr_pic_s0_flag");  rps->setUsed(j,code);
172    }
173    prev = 0;
174    for(Int j=rps->getNumberOfNegativePictures(); j < rps->getNumberOfNegativePictures()+rps->getNumberOfPositivePictures(); j++)
175    {
176      READ_UVLC(code, "delta_poc_s1_minus1");
177      poc = prev+code+1;
178      prev = poc;
179      rps->setDeltaPOC(j,poc);
180      READ_FLAG(code, "used_by_curr_pic_s1_flag");  rps->setUsed(j,code);
181    }
182    rps->setNumberOfPictures(rps->getNumberOfNegativePictures()+rps->getNumberOfPositivePictures());
183  }
184#if PRINT_RPS_INFO
185  rps->printDeltaPOC();
186#endif
187}
188
189#if CGS_3D_ASYMLUT
190Void TDecCavlc::parsePPS(TComPPS* pcPPS, TCom3DAsymLUT * pc3DAsymLUT, Int nLayerID)
191#else
192Void TDecCavlc::parsePPS(TComPPS* pcPPS)
193#endif
194
195{
196#if ENC_DEC_TRACE
197  xTracePPSHeader ();
198#endif
199  UInt  uiCode;
200
201  Int   iCode;
202
203  READ_UVLC( uiCode, "pps_pic_parameter_set_id");
204  assert(uiCode <= 63);
205  pcPPS->setPPSId (uiCode);
206
207  READ_UVLC( uiCode, "pps_seq_parameter_set_id");
208  assert(uiCode <= 15);
209  pcPPS->setSPSId (uiCode);
210
211  READ_FLAG( uiCode, "dependent_slice_segments_enabled_flag"    );    pcPPS->setDependentSliceSegmentsEnabledFlag   ( uiCode == 1 );
212
213  READ_FLAG( uiCode, "output_flag_present_flag" );                    pcPPS->setOutputFlagPresentFlag( uiCode==1 );
214
215  READ_CODE(3, uiCode, "num_extra_slice_header_bits");                pcPPS->setNumExtraSliceHeaderBits(uiCode);
216
217  READ_FLAG ( uiCode, "sign_data_hiding_flag" ); pcPPS->setSignHideFlag( uiCode );
218
219  READ_FLAG( uiCode,   "cabac_init_present_flag" );            pcPPS->setCabacInitPresentFlag( uiCode ? true : false );
220
221  READ_UVLC(uiCode, "num_ref_idx_l0_default_active_minus1");
222  assert(uiCode <= 14);
223  pcPPS->setNumRefIdxL0DefaultActive(uiCode+1);
224
225  READ_UVLC(uiCode, "num_ref_idx_l1_default_active_minus1");
226  assert(uiCode <= 14);
227  pcPPS->setNumRefIdxL1DefaultActive(uiCode+1);
228
229  READ_SVLC(iCode, "init_qp_minus26" );                            pcPPS->setPicInitQPMinus26(iCode);
230  READ_FLAG( uiCode, "constrained_intra_pred_flag" );              pcPPS->setConstrainedIntraPred( uiCode ? true : false );
231  READ_FLAG( uiCode, "transform_skip_enabled_flag" );
232  pcPPS->setUseTransformSkip ( uiCode ? true : false );
233
234  READ_FLAG( uiCode, "cu_qp_delta_enabled_flag" );            pcPPS->setUseDQP( uiCode ? true : false );
235  if( pcPPS->getUseDQP() )
236  {
237    READ_UVLC( uiCode, "diff_cu_qp_delta_depth" );
238    pcPPS->setMaxCuDQPDepth( uiCode );
239  }
240  else
241  {
242    pcPPS->setMaxCuDQPDepth( 0 );
243  }
244  READ_SVLC( iCode, "pps_cb_qp_offset");
245  pcPPS->setQpOffset(COMPONENT_Cb, iCode);
246  assert( pcPPS->getQpOffset(COMPONENT_Cb) >= -12 );
247  assert( pcPPS->getQpOffset(COMPONENT_Cb) <=  12 );
248
249  READ_SVLC( iCode, "pps_cr_qp_offset");
250  pcPPS->setQpOffset(COMPONENT_Cr, iCode);
251  assert( pcPPS->getQpOffset(COMPONENT_Cr) >= -12 );
252  assert( pcPPS->getQpOffset(COMPONENT_Cr) <=  12 );
253
254  assert(MAX_NUM_COMPONENT<=3);
255
256  READ_FLAG( uiCode, "pps_slice_chroma_qp_offsets_present_flag" );
257  pcPPS->setSliceChromaQpFlag( uiCode ? true : false );
258
259  READ_FLAG( uiCode, "weighted_pred_flag" );          // Use of Weighting Prediction (P_SLICE)
260  pcPPS->setUseWP( uiCode==1 );
261  READ_FLAG( uiCode, "weighted_bipred_flag" );         // Use of Bi-Directional Weighting Prediction (B_SLICE)
262  pcPPS->setWPBiPred( uiCode==1 );
263
264  READ_FLAG( uiCode, "transquant_bypass_enable_flag");
265  pcPPS->setTransquantBypassEnableFlag(uiCode ? true : false);
266  READ_FLAG( uiCode, "tiles_enabled_flag"               );    pcPPS->setTilesEnabledFlag            ( uiCode == 1 );
267  READ_FLAG( uiCode, "entropy_coding_sync_enabled_flag" );    pcPPS->setEntropyCodingSyncEnabledFlag( uiCode == 1 );
268
269  if( pcPPS->getTilesEnabledFlag() )
270  {
271    READ_UVLC ( uiCode, "num_tile_columns_minus1" );                pcPPS->setNumTileColumnsMinus1( uiCode ); 
272    READ_UVLC ( uiCode, "num_tile_rows_minus1" );                   pcPPS->setNumTileRowsMinus1( uiCode ); 
273    READ_FLAG ( uiCode, "uniform_spacing_flag" );                   pcPPS->setTileUniformSpacingFlag( uiCode == 1 );
274
275    const UInt tileColumnsMinus1 = pcPPS->getNumTileColumnsMinus1();
276    const UInt tileRowsMinus1    = pcPPS->getNumTileRowsMinus1();
277 
278    if ( !pcPPS->getTileUniformSpacingFlag())
279    {
280      if (tileColumnsMinus1 > 0)
281      {
282        std::vector<Int> columnWidth(tileColumnsMinus1);
283        for(UInt i = 0; i < tileColumnsMinus1; i++)
284        { 
285          READ_UVLC( uiCode, "column_width_minus1" ); 
286          columnWidth[i] = uiCode+1;
287        }
288        pcPPS->setTileColumnWidth(columnWidth);
289      }
290
291      if (tileRowsMinus1 > 0)
292      {
293        std::vector<Int> rowHeight (tileRowsMinus1);
294        for(UInt i = 0; i < tileRowsMinus1; i++)
295        {
296          READ_UVLC( uiCode, "row_height_minus1" );
297          rowHeight[i] = uiCode + 1;
298        }
299        pcPPS->setTileRowHeight(rowHeight);
300      }
301    }
302
303    if ((tileColumnsMinus1 + tileRowsMinus1) != 0)
304    {
305      READ_FLAG ( uiCode, "loop_filter_across_tiles_enabled_flag" );   pcPPS->setLoopFilterAcrossTilesEnabledFlag( uiCode ? true : false );
306    }
307  }
308  READ_FLAG( uiCode, "pps_loop_filter_across_slices_enabled_flag" );   pcPPS->setLoopFilterAcrossSlicesEnabledFlag( uiCode ? true : false );
309  READ_FLAG( uiCode, "deblocking_filter_control_present_flag" );       pcPPS->setDeblockingFilterControlPresentFlag( uiCode ? true : false );
310  if(pcPPS->getDeblockingFilterControlPresentFlag())
311  {
312    READ_FLAG( uiCode, "deblocking_filter_override_enabled_flag" );    pcPPS->setDeblockingFilterOverrideEnabledFlag( uiCode ? true : false );
313    READ_FLAG( uiCode, "pps_disable_deblocking_filter_flag" );         pcPPS->setPicDisableDeblockingFilterFlag(uiCode ? true : false );
314    if(!pcPPS->getPicDisableDeblockingFilterFlag())
315    {
316      READ_SVLC ( iCode, "pps_beta_offset_div2" );                     pcPPS->setDeblockingFilterBetaOffsetDiv2( iCode );
317      READ_SVLC ( iCode, "pps_tc_offset_div2" );                       pcPPS->setDeblockingFilterTcOffsetDiv2( iCode );
318    }
319  }
320  READ_FLAG( uiCode, "pps_scaling_list_data_present_flag" );           pcPPS->setScalingListPresentFlag( uiCode ? true : false );
321  if(pcPPS->getScalingListPresentFlag ())
322  {
323    parseScalingList( &(pcPPS->getScalingList()) );
324  }
325
326  READ_FLAG( uiCode, "lists_modification_present_flag");
327  pcPPS->setListsModificationPresentFlag(uiCode);
328
329  READ_UVLC( uiCode, "log2_parallel_merge_level_minus2");
330  pcPPS->setLog2ParallelMergeLevelMinus2 (uiCode);
331
332  READ_FLAG( uiCode, "slice_segment_header_extension_present_flag");
333  pcPPS->setSliceHeaderExtensionPresentFlag(uiCode);
334
335  READ_FLAG( uiCode, "pps_extension_present_flag");
336
337#if SVC_EXTENSION
338  pcPPS->setExtensionFlag( uiCode ? true : false );
339  if( pcPPS->getExtensionFlag() )
340#else
341  if (uiCode)
342#endif
343  {
344#if ENC_DEC_TRACE || RExt__DECODER_DEBUG_BIT_STATISTICS
345    static const TChar *syntaxStrings[]={ "pps_range_extension_flag",
346                                          "pps_multilayer_extension_flag",
347                                          "pps_extension_6bits[0]",
348                                          "pps_extension_6bits[1]",
349                                          "pps_extension_6bits[2]",
350                                          "pps_extension_6bits[3]",
351                                          "pps_extension_6bits[4]",
352                                          "pps_extension_6bits[5]" };
353#endif
354
355    Bool pps_extension_flags[NUM_PPS_EXTENSION_FLAGS];
356    for(Int i=0; i<NUM_PPS_EXTENSION_FLAGS; i++)
357    {
358      READ_FLAG( uiCode, syntaxStrings[i] );
359      pps_extension_flags[i] = uiCode!=0;
360    }
361
362    Bool bSkipTrailingExtensionBits=false;
363    for(Int i=0; i<NUM_PPS_EXTENSION_FLAGS; i++) // loop used so that the order is determined by the enum.
364    {
365      if (pps_extension_flags[i])
366      {
367        switch (PPSExtensionFlagIndex(i))
368        {
369          case PPS_EXT__REXT:
370            {
371              TComPPSRExt &ppsRangeExtension = pcPPS->getPpsRangeExtension();
372              assert(!bSkipTrailingExtensionBits);
373
374              if (pcPPS->getUseTransformSkip())
375              {
376                READ_UVLC( uiCode, "log2_max_transform_skip_block_size_minus2");
377                ppsRangeExtension.setLog2MaxTransformSkipBlockSize(uiCode+2);
378              }
379
380              READ_FLAG( uiCode, "cross_component_prediction_enabled_flag");
381              ppsRangeExtension.setCrossComponentPredictionEnabledFlag(uiCode != 0);
382
383              READ_FLAG( uiCode, "chroma_qp_offset_list_enabled_flag");
384              if (uiCode == 0)
385              {
386                ppsRangeExtension.clearChromaQpOffsetList();
387                ppsRangeExtension.setDiffCuChromaQpOffsetDepth(0);
388              }
389              else
390              {
391                READ_UVLC(uiCode, "diff_cu_chroma_qp_offset_depth"); ppsRangeExtension.setDiffCuChromaQpOffsetDepth(uiCode);
392                UInt tableSizeMinus1 = 0;
393                READ_UVLC(tableSizeMinus1, "chroma_qp_offset_list_len_minus1");
394                assert(tableSizeMinus1 < MAX_QP_OFFSET_LIST_SIZE);
395
396                for (Int cuChromaQpOffsetIdx = 0; cuChromaQpOffsetIdx <= (tableSizeMinus1); cuChromaQpOffsetIdx++)
397                {
398                  Int cbOffset;
399                  Int crOffset;
400                  READ_SVLC(cbOffset, "cb_qp_offset_list[i]");
401                  assert(cbOffset >= -12 && cbOffset <= 12);
402                  READ_SVLC(crOffset, "cr_qp_offset_list[i]");
403                  assert(crOffset >= -12 && crOffset <= 12);
404                  // table uses +1 for index (see comment inside the function)
405                  ppsRangeExtension.setChromaQpOffsetListEntry(cuChromaQpOffsetIdx+1, cbOffset, crOffset);
406                }
407                assert(ppsRangeExtension.getChromaQpOffsetListLen() == tableSizeMinus1 + 1);
408              }
409
410              READ_UVLC( uiCode, "log2_sao_offset_scale_luma");
411              ppsRangeExtension.setLog2SaoOffsetScale(CHANNEL_TYPE_LUMA, uiCode);
412              READ_UVLC( uiCode, "log2_sao_offset_scale_chroma");
413              ppsRangeExtension.setLog2SaoOffsetScale(CHANNEL_TYPE_CHROMA, uiCode);
414            }
415            break;
416
417#if SVC_EXTENSION
418          case PPS_EXT__MLAYER:
419            READ_FLAG( uiCode, "poc_reset_info_present_flag" );
420            pcPPS->setPocResetInfoPresentFlag(uiCode ? true : false);
421
422            READ_FLAG( uiCode, "pps_infer_scaling_list_flag" );
423            pcPPS->setInferScalingListFlag( uiCode );
424
425            if( pcPPS->getInferScalingListFlag() )
426            {
427              READ_CODE( 6, uiCode, "pps_scaling_list_ref_layer_id" ); 
428              pcPPS->setScalingListRefLayerId( uiCode );
429              // The value of pps_scaling_list_ref_layer_id shall be in the range of 0 to 62, inclusive
430              assert( pcPPS->getScalingListRefLayerId() <= 62 );
431              pcPPS->setScalingListPresentFlag( false );
432            }
433
434            READ_UVLC( uiCode,      "num_ref_loc_offsets" ); pcPPS->setNumRefLayerLocationOffsets(uiCode);
435            for(Int k = 0; k < pcPPS->getNumRefLayerLocationOffsets(); k++)
436            {
437              READ_CODE( 6, uiCode,  "ref_loc_offset_layer_id" );  pcPPS->setRefLocationOffsetLayerId( k, uiCode );
438              READ_FLAG( uiCode, "scaled_ref_layer_offset_present_flag" );   pcPPS->setScaledRefLayerOffsetPresentFlag( k, uiCode );
439              if (uiCode)
440              {
441                Window& scaledWindow = pcPPS->getScaledRefLayerWindow(k);
442                READ_SVLC( iCode, "scaled_ref_layer_left_offset" );    scaledWindow.setWindowLeftOffset  (iCode << 1);
443                READ_SVLC( iCode, "scaled_ref_layer_top_offset" );     scaledWindow.setWindowTopOffset   (iCode << 1);
444                READ_SVLC( iCode, "scaled_ref_layer_right_offset" );   scaledWindow.setWindowRightOffset (iCode << 1);
445                READ_SVLC( iCode, "scaled_ref_layer_bottom_offset" );  scaledWindow.setWindowBottomOffset(iCode << 1);
446              }
447              READ_FLAG( uiCode, "ref_region_offset_present_flag" );   pcPPS->setRefRegionOffsetPresentFlag( k, uiCode );
448              if (uiCode)
449              {
450                Window& refWindow = pcPPS->getRefLayerWindow(k);
451                READ_SVLC( iCode, "ref_region_left_offset" );    refWindow.setWindowLeftOffset  (iCode << 1);
452                READ_SVLC( iCode, "ref_region_top_offset" );     refWindow.setWindowTopOffset   (iCode << 1);
453                READ_SVLC( iCode, "ref_region_right_offset" );   refWindow.setWindowRightOffset (iCode << 1);
454                READ_SVLC( iCode, "ref_region_bottom_offset" );  refWindow.setWindowBottomOffset(iCode << 1);
455              }
456              READ_FLAG( uiCode, "resample_phase_set_present_flag" );   pcPPS->setResamplePhaseSetPresentFlag( k, uiCode );
457              if (uiCode)
458              {
459                READ_UVLC( uiCode, "phase_hor_luma" );    pcPPS->setPhaseHorLuma ( k, uiCode );
460                READ_UVLC( uiCode, "phase_ver_luma" );    pcPPS->setPhaseVerLuma ( k, uiCode );
461                READ_UVLC( uiCode, "phase_hor_chroma_plus8" );  pcPPS->setPhaseHorChroma (k, uiCode - 8);
462                READ_UVLC( uiCode, "phase_ver_chroma_plus8" );  pcPPS->setPhaseVerChroma (k, uiCode - 8);
463              }
464            }
465#if CGS_3D_ASYMLUT
466            READ_FLAG( uiCode , "colour_mapping_enabled_flag" ); 
467            pcPPS->setCGSFlag( uiCode );
468            if( pcPPS->getCGSFlag() )
469            {
470              // when pps_pic_parameter_set_id greater than or equal to 8, colour_mapping_enabled_flag shall be equal to 0
471              assert( pcPPS->getPPSId() < 8 );
472
473              xParse3DAsymLUT( pc3DAsymLUT );
474              pcPPS->setCGSOutputBitDepthY( pc3DAsymLUT->getOutputBitDepthY() );
475              pcPPS->setCGSOutputBitDepthC( pc3DAsymLUT->getOutputBitDepthC() );
476            }
477#endif
478            break;
479#endif
480          default:
481            bSkipTrailingExtensionBits=true;
482            break;
483        }
484      }
485    }
486    if (bSkipTrailingExtensionBits)
487    {
488      while ( xMoreRbspData() )
489      {
490        READ_FLAG( uiCode, "pps_extension_data_flag");
491      }
492    }
493  }
494  xReadRbspTrailingBits();
495}
496
497Void  TDecCavlc::parseVUI(TComVUI* pcVUI, TComSPS *pcSPS)
498{
499#if ENC_DEC_TRACE
500  fprintf( g_hTrace, "----------- vui_parameters -----------\n");
501#endif
502  UInt  uiCode;
503
504  READ_FLAG(     uiCode, "aspect_ratio_info_present_flag");           pcVUI->setAspectRatioInfoPresentFlag(uiCode);
505  if (pcVUI->getAspectRatioInfoPresentFlag())
506  {
507    READ_CODE(8, uiCode, "aspect_ratio_idc");                         pcVUI->setAspectRatioIdc(uiCode);
508    if (pcVUI->getAspectRatioIdc() == 255)
509    {
510      READ_CODE(16, uiCode, "sar_width");                             pcVUI->setSarWidth(uiCode);
511      READ_CODE(16, uiCode, "sar_height");                            pcVUI->setSarHeight(uiCode);
512    }
513  }
514
515  READ_FLAG(     uiCode, "overscan_info_present_flag");               pcVUI->setOverscanInfoPresentFlag(uiCode);
516  if (pcVUI->getOverscanInfoPresentFlag())
517  {
518    READ_FLAG(   uiCode, "overscan_appropriate_flag");                pcVUI->setOverscanAppropriateFlag(uiCode);
519  }
520
521  READ_FLAG(     uiCode, "video_signal_type_present_flag");           pcVUI->setVideoSignalTypePresentFlag(uiCode);
522  if (pcVUI->getVideoSignalTypePresentFlag())
523  {
524    READ_CODE(3, uiCode, "video_format");                             pcVUI->setVideoFormat(uiCode);
525    READ_FLAG(   uiCode, "video_full_range_flag");                    pcVUI->setVideoFullRangeFlag(uiCode);
526    READ_FLAG(   uiCode, "colour_description_present_flag");          pcVUI->setColourDescriptionPresentFlag(uiCode);
527    if (pcVUI->getColourDescriptionPresentFlag())
528    {
529      READ_CODE(8, uiCode, "colour_primaries");                       pcVUI->setColourPrimaries(uiCode);
530      READ_CODE(8, uiCode, "transfer_characteristics");               pcVUI->setTransferCharacteristics(uiCode);
531      READ_CODE(8, uiCode, "matrix_coeffs");                          pcVUI->setMatrixCoefficients(uiCode);
532    }
533  }
534
535  READ_FLAG(     uiCode, "chroma_loc_info_present_flag");             pcVUI->setChromaLocInfoPresentFlag(uiCode);
536  if (pcVUI->getChromaLocInfoPresentFlag())
537  {
538    READ_UVLC(   uiCode, "chroma_sample_loc_type_top_field" );        pcVUI->setChromaSampleLocTypeTopField(uiCode);
539    READ_UVLC(   uiCode, "chroma_sample_loc_type_bottom_field" );     pcVUI->setChromaSampleLocTypeBottomField(uiCode);
540  }
541
542  READ_FLAG(     uiCode, "neutral_chroma_indication_flag");           pcVUI->setNeutralChromaIndicationFlag(uiCode);
543
544  READ_FLAG(     uiCode, "field_seq_flag");                           pcVUI->setFieldSeqFlag(uiCode);
545
546  READ_FLAG(uiCode, "frame_field_info_present_flag");                 pcVUI->setFrameFieldInfoPresentFlag(uiCode);
547
548  READ_FLAG(     uiCode, "default_display_window_flag");
549  if (uiCode != 0)
550  {
551    Window &defDisp = pcVUI->getDefaultDisplayWindow();
552    READ_UVLC(   uiCode, "def_disp_win_left_offset" );                defDisp.setWindowLeftOffset  ( uiCode * TComSPS::getWinUnitX( pcSPS->getChromaFormatIdc()) );
553    READ_UVLC(   uiCode, "def_disp_win_right_offset" );               defDisp.setWindowRightOffset ( uiCode * TComSPS::getWinUnitX( pcSPS->getChromaFormatIdc()) );
554    READ_UVLC(   uiCode, "def_disp_win_top_offset" );                 defDisp.setWindowTopOffset   ( uiCode * TComSPS::getWinUnitY( pcSPS->getChromaFormatIdc()) );
555    READ_UVLC(   uiCode, "def_disp_win_bottom_offset" );              defDisp.setWindowBottomOffset( uiCode * TComSPS::getWinUnitY( pcSPS->getChromaFormatIdc()) );
556  }
557
558  TimingInfo *timingInfo = pcVUI->getTimingInfo();
559  READ_FLAG(       uiCode, "vui_timing_info_present_flag");         timingInfo->setTimingInfoPresentFlag      (uiCode ? true : false);
560#if SVC_EXTENSION
561  if( pcSPS->getLayerId() > 0 )
562  {
563    assert( timingInfo->getTimingInfoPresentFlag() == false );
564  }
565#endif
566  if(timingInfo->getTimingInfoPresentFlag())
567  {
568    READ_CODE( 32, uiCode, "vui_num_units_in_tick");                timingInfo->setNumUnitsInTick             (uiCode);
569    READ_CODE( 32, uiCode, "vui_time_scale");                       timingInfo->setTimeScale                  (uiCode);
570    READ_FLAG(     uiCode, "vui_poc_proportional_to_timing_flag");  timingInfo->setPocProportionalToTimingFlag(uiCode ? true : false);
571    if(timingInfo->getPocProportionalToTimingFlag())
572    {
573      READ_UVLC(   uiCode, "vui_num_ticks_poc_diff_one_minus1");    timingInfo->setNumTicksPocDiffOneMinus1   (uiCode);
574    }
575
576    READ_FLAG(     uiCode, "vui_hrd_parameters_present_flag");        pcVUI->setHrdParametersPresentFlag(uiCode);
577    if( pcVUI->getHrdParametersPresentFlag() )
578    {
579      parseHrdParameters( pcVUI->getHrdParameters(), 1, pcSPS->getMaxTLayers() - 1 );
580    }
581  }
582
583  READ_FLAG(     uiCode, "bitstream_restriction_flag");               pcVUI->setBitstreamRestrictionFlag(uiCode);
584  if (pcVUI->getBitstreamRestrictionFlag())
585  {
586    READ_FLAG(   uiCode, "tiles_fixed_structure_flag");               pcVUI->setTilesFixedStructureFlag(uiCode);
587    READ_FLAG(   uiCode, "motion_vectors_over_pic_boundaries_flag");  pcVUI->setMotionVectorsOverPicBoundariesFlag(uiCode);
588    READ_FLAG(   uiCode, "restricted_ref_pic_lists_flag");            pcVUI->setRestrictedRefPicListsFlag(uiCode);
589    READ_UVLC(   uiCode, "min_spatial_segmentation_idc");             pcVUI->setMinSpatialSegmentationIdc(uiCode);
590    assert(uiCode < 4096);
591    READ_UVLC(   uiCode, "max_bytes_per_pic_denom" );                 pcVUI->setMaxBytesPerPicDenom(uiCode);
592    READ_UVLC(   uiCode, "max_bits_per_min_cu_denom" );               pcVUI->setMaxBitsPerMinCuDenom(uiCode);
593    READ_UVLC(   uiCode, "log2_max_mv_length_horizontal" );           pcVUI->setLog2MaxMvLengthHorizontal(uiCode);
594    READ_UVLC(   uiCode, "log2_max_mv_length_vertical" );             pcVUI->setLog2MaxMvLengthVertical(uiCode);
595  }
596}
597
598Void TDecCavlc::parseHrdParameters(TComHRD *hrd, Bool commonInfPresentFlag, UInt maxNumSubLayersMinus1)
599{
600  UInt  uiCode;
601  if( commonInfPresentFlag )
602  {
603    READ_FLAG( uiCode, "nal_hrd_parameters_present_flag" );           hrd->setNalHrdParametersPresentFlag( uiCode == 1 ? true : false );
604    READ_FLAG( uiCode, "vcl_hrd_parameters_present_flag" );           hrd->setVclHrdParametersPresentFlag( uiCode == 1 ? true : false );
605    if( hrd->getNalHrdParametersPresentFlag() || hrd->getVclHrdParametersPresentFlag() )
606    {
607      READ_FLAG( uiCode, "sub_pic_hrd_params_present_flag" );         hrd->setSubPicCpbParamsPresentFlag( uiCode == 1 ? true : false );
608      if( hrd->getSubPicCpbParamsPresentFlag() )
609      {
610        READ_CODE( 8, uiCode, "tick_divisor_minus2" );                hrd->setTickDivisorMinus2( uiCode );
611        READ_CODE( 5, uiCode, "du_cpb_removal_delay_increment_length_minus1" ); hrd->setDuCpbRemovalDelayLengthMinus1( uiCode );
612        READ_FLAG( uiCode, "sub_pic_cpb_params_in_pic_timing_sei_flag" ); hrd->setSubPicCpbParamsInPicTimingSEIFlag( uiCode == 1 ? true : false );
613        READ_CODE( 5, uiCode, "dpb_output_delay_du_length_minus1"  ); hrd->setDpbOutputDelayDuLengthMinus1( uiCode );
614      }
615      READ_CODE( 4, uiCode, "bit_rate_scale" );                       hrd->setBitRateScale( uiCode );
616      READ_CODE( 4, uiCode, "cpb_size_scale" );                       hrd->setCpbSizeScale( uiCode );
617      if( hrd->getSubPicCpbParamsPresentFlag() )
618      {
619        READ_CODE( 4, uiCode, "cpb_size_du_scale" );                  hrd->setDuCpbSizeScale( uiCode );
620      }
621      READ_CODE( 5, uiCode, "initial_cpb_removal_delay_length_minus1" ); hrd->setInitialCpbRemovalDelayLengthMinus1( uiCode );
622      READ_CODE( 5, uiCode, "au_cpb_removal_delay_length_minus1" );      hrd->setCpbRemovalDelayLengthMinus1( uiCode );
623      READ_CODE( 5, uiCode, "dpb_output_delay_length_minus1" );       hrd->setDpbOutputDelayLengthMinus1( uiCode );
624    }
625#if SVC_EXTENSION
626    else
627    {
628      hrd->setInitialCpbRemovalDelayLengthMinus1( 23 );
629      // Add inferred values for other syntax elements here.
630    }
631#endif
632  }
633  Int i, j, nalOrVcl;
634  for( i = 0; i <= maxNumSubLayersMinus1; i ++ )
635  {
636    READ_FLAG( uiCode, "fixed_pic_rate_general_flag" );                     hrd->setFixedPicRateFlag( i, uiCode == 1 ? true : false  );
637    if( !hrd->getFixedPicRateFlag( i ) )
638    {
639      READ_FLAG( uiCode, "fixed_pic_rate_within_cvs_flag" );                hrd->setFixedPicRateWithinCvsFlag( i, uiCode == 1 ? true : false  );
640    }
641    else
642    {
643      hrd->setFixedPicRateWithinCvsFlag( i, true );
644    }
645
646    hrd->setLowDelayHrdFlag( i, 0 ); // Infered to be 0 when not present
647    hrd->setCpbCntMinus1   ( i, 0 ); // Infered to be 0 when not present
648
649    if( hrd->getFixedPicRateWithinCvsFlag( i ) )
650    {
651      READ_UVLC( uiCode, "elemental_duration_in_tc_minus1" );             hrd->setPicDurationInTcMinus1( i, uiCode );
652    }
653    else
654    {
655      READ_FLAG( uiCode, "low_delay_hrd_flag" );                      hrd->setLowDelayHrdFlag( i, uiCode == 1 ? true : false  );
656    }
657    if (!hrd->getLowDelayHrdFlag( i ))
658    {
659      READ_UVLC( uiCode, "cpb_cnt_minus1" );                          hrd->setCpbCntMinus1( i, uiCode );
660    }
661
662    for( nalOrVcl = 0; nalOrVcl < 2; nalOrVcl ++ )
663    {
664      if( ( ( nalOrVcl == 0 ) && ( hrd->getNalHrdParametersPresentFlag() ) ) ||
665          ( ( nalOrVcl == 1 ) && ( hrd->getVclHrdParametersPresentFlag() ) ) )
666      {
667        for( j = 0; j <= ( hrd->getCpbCntMinus1( i ) ); j ++ )
668        {
669          READ_UVLC( uiCode, "bit_rate_value_minus1" );             hrd->setBitRateValueMinus1( i, j, nalOrVcl, uiCode );
670          READ_UVLC( uiCode, "cpb_size_value_minus1" );             hrd->setCpbSizeValueMinus1( i, j, nalOrVcl, uiCode );
671          if( hrd->getSubPicCpbParamsPresentFlag() )
672          {
673            READ_UVLC( uiCode, "cpb_size_du_value_minus1" );       hrd->setDuCpbSizeValueMinus1( i, j, nalOrVcl, uiCode );
674            READ_UVLC( uiCode, "bit_rate_du_value_minus1" );       hrd->setDuBitRateValueMinus1( i, j, nalOrVcl, uiCode );
675          }
676          READ_FLAG( uiCode, "cbr_flag" );                          hrd->setCbrFlag( i, j, nalOrVcl, uiCode == 1 ? true : false  );
677        }
678      }
679    }
680  }
681}
682
683#if SCALABLE_REXT
684Void TDecCavlc::parseSPS(TComSPS* pcSPS, ParameterSetManager* pcParamSetManager)
685#else
686Void TDecCavlc::parseSPS(TComSPS* pcSPS)
687#endif
688{
689#if ENC_DEC_TRACE
690  xTraceSPSHeader ();
691#endif
692
693  UInt  uiCode;
694  READ_CODE( 4,  uiCode, "sps_video_parameter_set_id");          pcSPS->setVPSId        ( uiCode );
695#if SCALABLE_REXT
696  const TComVPS* pTmpVPS = pcParamSetManager->getVPS(pcSPS->getVPSId()); 
697#endif
698#if SVC_EXTENSION
699  UInt uiTmp = 0;
700 
701  if(pcSPS->getLayerId() == 0)
702  {
703#endif
704  READ_CODE( 3,  uiCode, "sps_max_sub_layers_minus1" );          pcSPS->setMaxTLayers   ( uiCode+1 );
705  assert(uiCode <= 6);
706#if SVC_EXTENSION
707  }
708  else
709  {
710    READ_CODE( 3,  uiCode, "sps_ext_or_max_sub_layers_minus1" );     uiTmp = uiCode;
711
712    if( uiTmp != 7 )
713    {
714      pcSPS->setMaxTLayers(uiTmp + 1);
715    }
716  }
717
718  pcSPS->setMultiLayerExtSpsFlag( pcSPS->getLayerId() != 0 && uiTmp == 7 );
719
720  if( !pcSPS->getMultiLayerExtSpsFlag() )
721  {
722#endif
723  READ_FLAG( uiCode, "sps_temporal_id_nesting_flag" );           pcSPS->setTemporalIdNestingFlag ( uiCode > 0 ? true : false );
724#if SVC_EXTENSION
725    parsePTL(pcSPS->getPTL(), 1, pcSPS->getMaxTLayers() - 1);
726  }
727#else
728  if ( pcSPS->getMaxTLayers() == 1 )
729  {
730    // sps_temporal_id_nesting_flag must be 1 when sps_max_sub_layers_minus1 is 0
731    assert( uiCode == 1 );
732  }
733
734  parsePTL(pcSPS->getPTL(), 1, pcSPS->getMaxTLayers() - 1);
735#endif
736  READ_UVLC(     uiCode, "sps_seq_parameter_set_id" );           pcSPS->setSPSId( uiCode );
737  assert(uiCode <= 15);
738
739#if SVC_EXTENSION
740  if( pcSPS->getMultiLayerExtSpsFlag() )
741  {
742    READ_FLAG( uiCode, "update_rep_format_flag" );
743    pcSPS->setUpdateRepFormatFlag( uiCode ? true : false );
744   
745    if( pcSPS->getUpdateRepFormatFlag() )
746    {
747      READ_CODE(8, uiCode, "sps_rep_format_idx");
748      pcSPS->setUpdateRepFormatIndex(uiCode);
749    }
750#if SCALABLE_REXT
751    // If update_rep_format_flag is equal to 0, the variable repFormatIdx is set equal to vps_rep_format_idx[ LayerIdxInVps[ layerIdCurr ] ].
752    else
753    {
754      Int iVPSRepFormatIdx = pTmpVPS->getVpsRepFormatIdx( pTmpVPS->getLayerIdxInVps( pcSPS->getLayerId() ) );
755      pcSPS->setUpdateRepFormatIndex( iVPSRepFormatIdx );
756
757      pcSPS->setChromaFormatIdc(              pTmpVPS->getVpsRepFormat(iVPSRepFormatIdx)->getChromaFormatVpsIdc() );
758      pcSPS->setPicWidthInLumaSamples(        pTmpVPS->getVpsRepFormat(iVPSRepFormatIdx)->getPicWidthVpsInLumaSamples() );
759      pcSPS->setPicHeightInLumaSamples(       pTmpVPS->getVpsRepFormat(iVPSRepFormatIdx)->getPicHeightVpsInLumaSamples() );
760      pcSPS->setBitDepth(CHANNEL_TYPE_LUMA,   pTmpVPS->getVpsRepFormat(iVPSRepFormatIdx)->getBitDepthVpsLuma() );
761      pcSPS->setBitDepth(CHANNEL_TYPE_CHROMA, pTmpVPS->getVpsRepFormat(iVPSRepFormatIdx)->getBitDepthVpsChroma() );
762      Window &conf = pcSPS->getConformanceWindow();
763      conf.setWindowLeftOffset(               pTmpVPS->getVpsRepFormat(iVPSRepFormatIdx)->getConformanceWindowVps().getWindowLeftOffset() );
764      conf.setWindowRightOffset(              pTmpVPS->getVpsRepFormat(iVPSRepFormatIdx)->getConformanceWindowVps().getWindowRightOffset() );
765      conf.setWindowTopOffset(                pTmpVPS->getVpsRepFormat(iVPSRepFormatIdx)->getConformanceWindowVps().getWindowTopOffset() );
766      conf.setWindowBottomOffset(             pTmpVPS->getVpsRepFormat(iVPSRepFormatIdx)->getConformanceWindowVps().getWindowBottomOffset() );
767    }
768#endif
769  }
770  else
771  {
772    pcSPS->setUpdateRepFormatFlag( false );
773#endif
774  READ_UVLC(     uiCode, "chroma_format_idc" );                  pcSPS->setChromaFormatIdc( ChromaFormat(uiCode) );
775  assert(uiCode <= 3);
776
777  if( pcSPS->getChromaFormatIdc() == CHROMA_444 )
778  {
779    READ_FLAG(     uiCode, "separate_colour_plane_flag");        assert(uiCode == 0);
780  }
781
782  READ_UVLC (    uiCode, "pic_width_in_luma_samples" );          pcSPS->setPicWidthInLumaSamples ( uiCode    );
783  READ_UVLC (    uiCode, "pic_height_in_luma_samples" );         pcSPS->setPicHeightInLumaSamples( uiCode    );
784  READ_FLAG(     uiCode, "conformance_window_flag");
785  if (uiCode != 0)
786  {
787    Window &conf = pcSPS->getConformanceWindow();
788#if SVC_EXTENSION
789    READ_UVLC(   uiCode, "conf_win_left_offset" );               conf.setWindowLeftOffset  ( uiCode );
790    READ_UVLC(   uiCode, "conf_win_right_offset" );              conf.setWindowRightOffset ( uiCode );
791    READ_UVLC(   uiCode, "conf_win_top_offset" );                conf.setWindowTopOffset   ( uiCode );
792    READ_UVLC(   uiCode, "conf_win_bottom_offset" );             conf.setWindowBottomOffset( uiCode );
793#else
794    READ_UVLC(   uiCode, "conf_win_left_offset" );               conf.setWindowLeftOffset  ( uiCode * TComSPS::getWinUnitX( pcSPS->getChromaFormatIdc() ) );
795    READ_UVLC(   uiCode, "conf_win_right_offset" );              conf.setWindowRightOffset ( uiCode * TComSPS::getWinUnitX( pcSPS->getChromaFormatIdc() ) );
796    READ_UVLC(   uiCode, "conf_win_top_offset" );                conf.setWindowTopOffset   ( uiCode * TComSPS::getWinUnitY( pcSPS->getChromaFormatIdc() ) );
797    READ_UVLC(   uiCode, "conf_win_bottom_offset" );             conf.setWindowBottomOffset( uiCode * TComSPS::getWinUnitY( pcSPS->getChromaFormatIdc() ) );
798#endif
799  }
800
801  READ_UVLC(     uiCode, "bit_depth_luma_minus8" );
802#if O0043_BEST_EFFORT_DECODING
803  pcSPS->setStreamBitDepth(CHANNEL_TYPE_LUMA, 8 + uiCode);
804  const UInt forceDecodeBitDepth = pcSPS->getForceDecodeBitDepth();
805  if (forceDecodeBitDepth != 0)
806  {
807    uiCode = forceDecodeBitDepth - 8;
808  }
809#endif
810  assert(uiCode <= 8);
811  pcSPS->setBitDepth(CHANNEL_TYPE_LUMA, 8 + uiCode);
812
813#if O0043_BEST_EFFORT_DECODING
814  pcSPS->setQpBDOffset(CHANNEL_TYPE_LUMA, (Int) (6*(pcSPS->getStreamBitDepth(CHANNEL_TYPE_LUMA)-8)) );
815#else
816  pcSPS->setQpBDOffset(CHANNEL_TYPE_LUMA, (Int) (6*uiCode) );
817#endif
818
819  READ_UVLC( uiCode,    "bit_depth_chroma_minus8" );
820#if O0043_BEST_EFFORT_DECODING
821  pcSPS->setStreamBitDepth(CHANNEL_TYPE_CHROMA, 8 + uiCode);
822  if (forceDecodeBitDepth != 0)
823  {
824    uiCode = forceDecodeBitDepth - 8;
825  }
826#endif
827  assert(uiCode <= 8);
828  pcSPS->setBitDepth(CHANNEL_TYPE_CHROMA, 8 + uiCode);
829#if O0043_BEST_EFFORT_DECODING
830  pcSPS->setQpBDOffset(CHANNEL_TYPE_CHROMA,  (Int) (6*(pcSPS->getStreamBitDepth(CHANNEL_TYPE_CHROMA)-8)) );
831#else
832  pcSPS->setQpBDOffset(CHANNEL_TYPE_CHROMA,  (Int) (6*uiCode) );
833#endif
834
835#if SVC_EXTENSION
836  }
837#endif
838
839
840  READ_UVLC( uiCode,    "log2_max_pic_order_cnt_lsb_minus4" );   pcSPS->setBitsForPOC( 4 + uiCode );
841  assert(uiCode <= 12);
842
843#if SVC_EXTENSION
844  if( !pcSPS->getMultiLayerExtSpsFlag() ) 
845  {
846#endif
847  UInt subLayerOrderingInfoPresentFlag;
848  READ_FLAG(subLayerOrderingInfoPresentFlag, "sps_sub_layer_ordering_info_present_flag");
849
850  for(UInt i=0; i <= pcSPS->getMaxTLayers()-1; i++)
851  {
852    READ_UVLC ( uiCode, "sps_max_dec_pic_buffering_minus1[i]");
853    pcSPS->setMaxDecPicBuffering( uiCode + 1, i);
854    READ_UVLC ( uiCode, "sps_max_num_reorder_pics[i]" );
855    pcSPS->setNumReorderPics(uiCode, i);
856    READ_UVLC ( uiCode, "sps_max_latency_increase_plus1[i]");
857    pcSPS->setMaxLatencyIncreasePlus1( uiCode, i );
858
859    if (!subLayerOrderingInfoPresentFlag)
860    {
861      for (i++; i <= pcSPS->getMaxTLayers()-1; i++)
862      {
863        pcSPS->setMaxDecPicBuffering(pcSPS->getMaxDecPicBuffering(0), i);
864        pcSPS->setNumReorderPics(pcSPS->getNumReorderPics(0), i);
865        pcSPS->setMaxLatencyIncreasePlus1(pcSPS->getMaxLatencyIncreasePlus1(0), i);
866      }
867      break;
868    }
869
870#if SVC_EXTENSION
871    if( i > 0 )
872    {
873      // When i is greater than 0, sps_max_dec_pic_buffering_minus1[ i ] shall be greater than or equal to sps_max_dec_pic_buffering_minus1[ i - 1 ].
874      assert( pcSPS->getMaxDecPicBuffering(i) >= pcSPS->getMaxDecPicBuffering(i-1) );
875    }
876#endif
877
878  }
879#if SVC_EXTENSION
880  }
881#endif
882  READ_UVLC( uiCode, "log2_min_luma_coding_block_size_minus3" );
883  Int log2MinCUSize = uiCode + 3;
884  pcSPS->setLog2MinCodingBlockSize(log2MinCUSize);
885  READ_UVLC( uiCode, "log2_diff_max_min_luma_coding_block_size" );
886  pcSPS->setLog2DiffMaxMinCodingBlockSize(uiCode);
887 
888  if (pcSPS->getPTL()->getGeneralPTL()->getLevelIdc() >= Level::LEVEL5)
889  {
890    assert(log2MinCUSize + pcSPS->getLog2DiffMaxMinCodingBlockSize() >= 5);
891  }
892 
893  Int maxCUDepthDelta = uiCode;
894  pcSPS->setMaxCUWidth  ( 1<<(log2MinCUSize + maxCUDepthDelta) );
895  pcSPS->setMaxCUHeight ( 1<<(log2MinCUSize + maxCUDepthDelta) );
896  READ_UVLC( uiCode, "log2_min_luma_transform_block_size_minus2" );   pcSPS->setQuadtreeTULog2MinSize( uiCode + 2 );
897
898  READ_UVLC( uiCode, "log2_diff_max_min_luma_transform_block_size" ); pcSPS->setQuadtreeTULog2MaxSize( uiCode + pcSPS->getQuadtreeTULog2MinSize() );
899  pcSPS->setMaxTrSize( 1<<(uiCode + pcSPS->getQuadtreeTULog2MinSize()) );
900
901  READ_UVLC( uiCode, "max_transform_hierarchy_depth_inter" );    pcSPS->setQuadtreeTUMaxDepthInter( uiCode+1 );
902  READ_UVLC( uiCode, "max_transform_hierarchy_depth_intra" );    pcSPS->setQuadtreeTUMaxDepthIntra( uiCode+1 );
903
904  Int addCuDepth = max (0, log2MinCUSize - (Int)pcSPS->getQuadtreeTULog2MinSize() );
905  pcSPS->setMaxTotalCUDepth( maxCUDepthDelta + addCuDepth  + getMaxCUDepthOffset(pcSPS->getChromaFormatIdc(), pcSPS->getQuadtreeTULog2MinSize()) );
906
907  READ_FLAG( uiCode, "scaling_list_enabled_flag" );                 pcSPS->setScalingListFlag ( uiCode );
908  if(pcSPS->getScalingListFlag())
909  {
910#if SVC_EXTENSION
911    if( pcSPS->getMultiLayerExtSpsFlag() )
912    {
913      READ_FLAG( uiCode, "sps_infer_scaling_list_flag" ); pcSPS->setInferScalingListFlag( uiCode );
914    }
915
916    if( pcSPS->getInferScalingListFlag() )
917    {
918      READ_CODE( 6, uiCode, "sps_scaling_list_ref_layer_id" ); pcSPS->setScalingListRefLayerId( uiCode );
919
920      // The value of sps_scaling_list_ref_layer_id shall be in the range of 0 to 62, inclusive
921      assert( pcSPS->getScalingListRefLayerId() <= 62 );
922
923      pcSPS->setScalingListPresentFlag( false );
924    }
925    else
926    {
927#endif
928    READ_FLAG( uiCode, "sps_scaling_list_data_present_flag" );                 pcSPS->setScalingListPresentFlag ( uiCode );
929    if(pcSPS->getScalingListPresentFlag ())
930    {
931      parseScalingList( &(pcSPS->getScalingList()) );
932    }
933#if SVC_EXTENSION
934    }
935#endif
936  }
937  READ_FLAG( uiCode, "amp_enabled_flag" );                          pcSPS->setUseAMP( uiCode );
938  READ_FLAG( uiCode, "sample_adaptive_offset_enabled_flag" );       pcSPS->setUseSAO ( uiCode ? true : false );
939
940  READ_FLAG( uiCode, "pcm_enabled_flag" ); pcSPS->setUsePCM( uiCode ? true : false );
941  if( pcSPS->getUsePCM() )
942  {
943    READ_CODE( 4, uiCode, "pcm_sample_bit_depth_luma_minus1" );          pcSPS->setPCMBitDepth    ( CHANNEL_TYPE_LUMA, 1 + uiCode );
944    READ_CODE( 4, uiCode, "pcm_sample_bit_depth_chroma_minus1" );        pcSPS->setPCMBitDepth    ( CHANNEL_TYPE_CHROMA, 1 + uiCode );
945    READ_UVLC( uiCode, "log2_min_pcm_luma_coding_block_size_minus3" );   pcSPS->setPCMLog2MinSize (uiCode+3);
946    READ_UVLC( uiCode, "log2_diff_max_min_pcm_luma_coding_block_size" ); pcSPS->setPCMLog2MaxSize ( uiCode+pcSPS->getPCMLog2MinSize() );
947    READ_FLAG( uiCode, "pcm_loop_filter_disable_flag" );                 pcSPS->setPCMFilterDisableFlag ( uiCode ? true : false );
948  }
949
950  READ_UVLC( uiCode, "num_short_term_ref_pic_sets" );
951  assert(uiCode <= 64);
952  pcSPS->createRPSList(uiCode);
953
954  TComRPSList* rpsList = pcSPS->getRPSList();
955  TComReferencePictureSet* rps;
956
957  for(UInt i=0; i< rpsList->getNumberOfReferencePictureSets(); i++)
958  {
959    rps = rpsList->getReferencePictureSet(i);
960    parseShortTermRefPicSet(pcSPS,rps,i);
961  }
962  READ_FLAG( uiCode, "long_term_ref_pics_present_flag" );          pcSPS->setLongTermRefsPresent(uiCode);
963  if (pcSPS->getLongTermRefsPresent())
964  {
965    READ_UVLC( uiCode, "num_long_term_ref_pics_sps" );
966    pcSPS->setNumLongTermRefPicSPS(uiCode);
967    for (UInt k = 0; k < pcSPS->getNumLongTermRefPicSPS(); k++)
968    {
969      READ_CODE( pcSPS->getBitsForPOC(), uiCode, "lt_ref_pic_poc_lsb_sps" );
970      pcSPS->setLtRefPicPocLsbSps(k, uiCode);
971      READ_FLAG( uiCode,  "used_by_curr_pic_lt_sps_flag[i]");
972      pcSPS->setUsedByCurrPicLtSPSFlag(k, uiCode?1:0);
973    }
974  }
975  READ_FLAG( uiCode, "sps_temporal_mvp_enable_flag" );            pcSPS->setTMVPFlagsPresent(uiCode);
976
977  READ_FLAG( uiCode, "sps_strong_intra_smoothing_enable_flag" );  pcSPS->setUseStrongIntraSmoothing(uiCode);
978
979  READ_FLAG( uiCode, "vui_parameters_present_flag" );             pcSPS->setVuiParametersPresentFlag(uiCode);
980
981  if (pcSPS->getVuiParametersPresentFlag())
982  {
983    parseVUI(pcSPS->getVuiParameters(), pcSPS);
984  }
985
986  READ_FLAG( uiCode, "sps_extension_present_flag");
987
988#if SVC_EXTENSION
989  pcSPS->setExtensionFlag( uiCode ? true : false );
990
991  if( pcSPS->getExtensionFlag() )
992#else
993  if (uiCode)
994#endif
995  {
996#if ENC_DEC_TRACE || RExt__DECODER_DEBUG_BIT_STATISTICS
997    static const TChar *syntaxStrings[]={ "sps_range_extension_flag",
998                                          "sps_multilayer_extension_flag",
999                                          "sps_extension_6bits[0]",
1000                                          "sps_extension_6bits[1]",
1001                                          "sps_extension_6bits[2]",
1002                                          "sps_extension_6bits[3]",
1003                                          "sps_extension_6bits[4]",
1004                                          "sps_extension_6bits[5]" };
1005#endif
1006    Bool sps_extension_flags[NUM_SPS_EXTENSION_FLAGS];
1007
1008    for(Int i=0; i<NUM_SPS_EXTENSION_FLAGS; i++)
1009    {
1010      READ_FLAG( uiCode, syntaxStrings[i] );
1011      sps_extension_flags[i] = uiCode!=0;
1012    }
1013
1014    Bool bSkipTrailingExtensionBits=false;
1015    for(Int i=0; i<NUM_SPS_EXTENSION_FLAGS; i++) // loop used so that the order is determined by the enum.
1016    {
1017      if (sps_extension_flags[i])
1018      {
1019        switch (SPSExtensionFlagIndex(i))
1020        {
1021          case SPS_EXT__REXT:
1022            assert(!bSkipTrailingExtensionBits);
1023            {
1024              TComSPSRExt &spsRangeExtension = pcSPS->getSpsRangeExtension();
1025              READ_FLAG( uiCode, "transform_skip_rotation_enabled_flag");     spsRangeExtension.setTransformSkipRotationEnabledFlag(uiCode != 0);
1026              READ_FLAG( uiCode, "transform_skip_context_enabled_flag");      spsRangeExtension.setTransformSkipContextEnabledFlag (uiCode != 0);
1027              READ_FLAG( uiCode, "implicit_rdpcm_enabled_flag");              spsRangeExtension.setRdpcmEnabledFlag(RDPCM_SIGNAL_IMPLICIT, (uiCode != 0));
1028              READ_FLAG( uiCode, "explicit_rdpcm_enabled_flag");              spsRangeExtension.setRdpcmEnabledFlag(RDPCM_SIGNAL_EXPLICIT, (uiCode != 0));
1029              READ_FLAG( uiCode, "extended_precision_processing_flag");       spsRangeExtension.setExtendedPrecisionProcessingFlag (uiCode != 0);
1030              READ_FLAG( uiCode, "intra_smoothing_disabled_flag");            spsRangeExtension.setIntraSmoothingDisabledFlag      (uiCode != 0);
1031              READ_FLAG( uiCode, "high_precision_offsets_enabled_flag");      spsRangeExtension.setHighPrecisionOffsetsEnabledFlag (uiCode != 0);
1032              READ_FLAG( uiCode, "persistent_rice_adaptation_enabled_flag");  spsRangeExtension.setPersistentRiceAdaptationEnabledFlag (uiCode != 0);
1033              READ_FLAG( uiCode, "cabac_bypass_alignment_enabled_flag");      spsRangeExtension.setCabacBypassAlignmentEnabledFlag  (uiCode != 0);
1034            }
1035            break;
1036#if SVC_EXTENSION
1037          case SPS_EXT__MLAYER:
1038            parseSPSExtension( pcSPS );
1039            break;
1040#endif
1041          default:
1042            bSkipTrailingExtensionBits=true;
1043            break;
1044        }
1045      }
1046    }
1047    if (bSkipTrailingExtensionBits)
1048    {
1049      while ( xMoreRbspData() )
1050      {
1051        READ_FLAG( uiCode, "sps_extension_data_flag");
1052      }
1053    }
1054  }
1055
1056  xReadRbspTrailingBits();
1057}
1058
1059Void TDecCavlc::parseVPS(TComVPS* pcVPS)
1060{
1061#if ENC_DEC_TRACE
1062  xTraceVPSHeader ();
1063#endif
1064  UInt  uiCode;
1065
1066  READ_CODE( 4,  uiCode,  "vps_video_parameter_set_id" );         pcVPS->setVPSId( uiCode );
1067#if SVC_EXTENSION
1068  READ_FLAG( uiCode, "vps_base_layer_internal_flag");             pcVPS->setBaseLayerInternalFlag( uiCode ? true : false );
1069  READ_FLAG( uiCode, "vps_base_layer_available_flag");            pcVPS->setBaseLayerAvailableFlag( uiCode ? true : false );
1070  pcVPS->setNonHEVCBaseLayerFlag( (pcVPS->getBaseLayerAvailableFlag() && !pcVPS->getBaseLayerInternalFlag()) ? true : false);
1071
1072  READ_CODE( 6,  uiCode,  "vps_max_layers_minus1" );              pcVPS->setMaxLayers( min( 62u, uiCode) + 1 );
1073  assert( pcVPS->getBaseLayerInternalFlag() || pcVPS->getMaxLayers() > 1 );
1074#else
1075  READ_FLAG( uiCode,      "vps_base_layer_internal_flag" );       assert(uiCode == 1);
1076  READ_FLAG( uiCode,      "vps_base_layer_available_flag" );      assert(uiCode == 1);
1077  READ_CODE( 6,  uiCode,  "vps_max_layers_minus1" );
1078#endif
1079  READ_CODE( 3,  uiCode,  "vps_max_sub_layers_minus1" );          pcVPS->setMaxTLayers( uiCode + 1 );    assert(uiCode+1 <= MAX_TLAYER);
1080  READ_FLAG(     uiCode,  "vps_temporal_id_nesting_flag" );       pcVPS->setTemporalNestingFlag( uiCode ? true:false );
1081  assert (pcVPS->getMaxTLayers()>1||pcVPS->getTemporalNestingFlag());
1082  READ_CODE( 16, uiCode,  "vps_reserved_0xffff_16bits" );         assert(uiCode == 0xffff);
1083  parsePTL ( pcVPS->getPTL(), true, pcVPS->getMaxTLayers()-1);
1084  UInt subLayerOrderingInfoPresentFlag;
1085  READ_FLAG(subLayerOrderingInfoPresentFlag, "vps_sub_layer_ordering_info_present_flag");
1086  for(UInt i = 0; i <= pcVPS->getMaxTLayers()-1; i++)
1087  {
1088    READ_UVLC( uiCode,  "vps_max_dec_pic_buffering_minus1[i]" );    pcVPS->setMaxDecPicBuffering( uiCode + 1, i );
1089    READ_UVLC( uiCode,  "vps_max_num_reorder_pics[i]" );            pcVPS->setNumReorderPics( uiCode, i );
1090    READ_UVLC( uiCode,  "vps_max_latency_increase_plus1[i]" );      pcVPS->setMaxLatencyIncrease( uiCode, i );
1091
1092    if (!subLayerOrderingInfoPresentFlag)
1093    {
1094      for (i++; i <= pcVPS->getMaxTLayers()-1; i++)
1095      {
1096        pcVPS->setMaxDecPicBuffering(pcVPS->getMaxDecPicBuffering(0), i);
1097        pcVPS->setNumReorderPics(pcVPS->getNumReorderPics(0), i);
1098        pcVPS->setMaxLatencyIncrease(pcVPS->getMaxLatencyIncrease(0), i);
1099      }
1100      break;
1101    }
1102  }
1103
1104#if SVC_EXTENSION
1105  assert( pcVPS->getNumHrdParameters() < MAX_VPS_LAYER_SETS_PLUS1 );
1106  assert( pcVPS->getMaxLayerId()       < MAX_NUM_LAYER_IDS );
1107  READ_CODE( 6, uiCode, "vps_max_layer_id" );           pcVPS->setMaxLayerId( uiCode );
1108  READ_UVLC(uiCode, "vps_num_layer_sets_minus1");  pcVPS->setVpsNumLayerSetsMinus1(uiCode);
1109  pcVPS->setNumLayerSets(pcVPS->getVpsNumLayerSetsMinus1() + 1);
1110
1111  for (UInt opsIdx = 1; opsIdx <= pcVPS->getVpsNumLayerSetsMinus1(); opsIdx++)
1112  {
1113    // Operation point set
1114    for( UInt i = 0; i <= pcVPS->getMaxLayerId(); i ++ )
1115#else
1116  assert( pcVPS->getNumHrdParameters() < MAX_VPS_OP_SETS_PLUS1 );
1117  assert( pcVPS->getMaxNuhReservedZeroLayerId() < MAX_VPS_NUH_RESERVED_ZERO_LAYER_ID_PLUS1 );
1118  READ_CODE( 6, uiCode, "vps_max_layer_id" );                        pcVPS->setMaxNuhReservedZeroLayerId( uiCode );
1119  READ_UVLC(    uiCode, "vps_num_layer_sets_minus1" );               pcVPS->setMaxOpSets( uiCode + 1 );
1120  for( UInt opsIdx = 1; opsIdx <= ( pcVPS->getMaxOpSets() - 1 ); opsIdx ++ )
1121  {
1122    // Operation point set
1123    for( UInt i = 0; i <= pcVPS->getMaxNuhReservedZeroLayerId(); i ++ )
1124#endif
1125    {
1126      READ_FLAG( uiCode, "layer_id_included_flag[opsIdx][i]" );   pcVPS->setLayerIdIncludedFlag( uiCode == 1 ? true : false, opsIdx, i );
1127    }
1128  }
1129
1130#if SVC_EXTENSION
1131  pcVPS->deriveLayerIdListVariables();
1132#endif
1133
1134  TimingInfo *timingInfo = pcVPS->getTimingInfo();
1135  READ_FLAG(       uiCode, "vps_timing_info_present_flag");         timingInfo->setTimingInfoPresentFlag      (uiCode ? true : false);
1136  if(timingInfo->getTimingInfoPresentFlag())
1137  {
1138    READ_CODE( 32, uiCode, "vps_num_units_in_tick");                timingInfo->setNumUnitsInTick             (uiCode);
1139    READ_CODE( 32, uiCode, "vps_time_scale");                       timingInfo->setTimeScale                  (uiCode);
1140    READ_FLAG(     uiCode, "vps_poc_proportional_to_timing_flag");  timingInfo->setPocProportionalToTimingFlag(uiCode ? true : false);
1141    if(timingInfo->getPocProportionalToTimingFlag())
1142    {
1143      READ_UVLC(   uiCode, "vps_num_ticks_poc_diff_one_minus1");    timingInfo->setNumTicksPocDiffOneMinus1   (uiCode);
1144    }
1145
1146    READ_UVLC( uiCode, "vps_num_hrd_parameters" );                  pcVPS->setNumHrdParameters( uiCode );
1147
1148    if( pcVPS->getNumHrdParameters() > 0 )
1149    {
1150      pcVPS->createHrdParamBuffer();
1151    }
1152    for( UInt i = 0; i < pcVPS->getNumHrdParameters(); i ++ )
1153    {
1154      READ_UVLC( uiCode, "hrd_layer_set_idx[i]" );                  pcVPS->setHrdOpSetIdx( uiCode, i );
1155      if( i > 0 )
1156      {
1157        READ_FLAG( uiCode, "cprms_present_flag[i]" );               pcVPS->setCprmsPresentFlag( uiCode == 1 ? true : false, i );
1158      }
1159      else
1160      {
1161        pcVPS->setCprmsPresentFlag( true, i );
1162      }
1163
1164      parseHrdParameters(pcVPS->getHrdParameters(i), pcVPS->getCprmsPresentFlag( i ), pcVPS->getMaxTLayers() - 1);
1165    }
1166  }
1167
1168#if SVC_EXTENSION
1169  READ_FLAG( uiCode,  "vps_extension_flag" );      pcVPS->setVpsExtensionFlag( uiCode ? true : false );
1170
1171  // When MaxLayersMinus1 is greater than 0, vps_extension_flag shall be equal to 1.
1172  if( pcVPS->getMaxLayers() > 1 )
1173  {
1174    assert( pcVPS->getVpsExtensionFlag() == true );
1175  }
1176
1177  if( pcVPS->getVpsExtensionFlag()  )
1178  {
1179    while ( m_pcBitstream->getNumBitsRead() % 8 != 0 )
1180    {
1181      READ_FLAG( uiCode, "vps_extension_alignment_bit_equal_to_one"); assert(uiCode == 1);
1182    }
1183    parseVPSExtension(pcVPS);
1184    READ_FLAG( uiCode, "vps_extension2_flag" );
1185    if(uiCode)
1186    {
1187      while ( xMoreRbspData() )
1188      {
1189        READ_FLAG( uiCode, "vps_extension_data_flag");
1190      }
1191    }
1192  }
1193  else
1194  {
1195    // set default parameters when syntax elements are not present
1196    defaultVPSExtension(pcVPS);   
1197  }
1198#else
1199  READ_FLAG( uiCode,  "vps_extension_flag" );
1200  if (uiCode)
1201  {
1202    while ( xMoreRbspData() )
1203    {
1204      READ_FLAG( uiCode, "vps_extension_data_flag");
1205    }
1206  }
1207#endif
1208
1209  xReadRbspTrailingBits();
1210}
1211
1212Void TDecCavlc::parseSliceHeader (TComSlice* pcSlice, ParameterSetManager *parameterSetManager, const Int prevTid0POC)
1213{
1214  UInt  uiCode;
1215  Int   iCode;
1216
1217#if ENC_DEC_TRACE
1218  xTraceSliceHeader();
1219#endif
1220  TComPPS* pps = NULL;
1221  TComSPS* sps = NULL;
1222
1223  UInt firstSliceSegmentInPic;
1224  READ_FLAG( firstSliceSegmentInPic, "first_slice_segment_in_pic_flag" );
1225
1226#if SVC_EXTENSION
1227  pcSlice->setFirstSliceInPic( firstSliceSegmentInPic );
1228#endif
1229
1230  if( pcSlice->getRapPicFlag())
1231  {
1232    READ_FLAG( uiCode, "no_output_of_prior_pics_flag" );  //ignored -- updated already
1233    pcSlice->setNoOutputPriorPicsFlag(uiCode ? true : false);
1234  }
1235  READ_UVLC (    uiCode, "slice_pic_parameter_set_id" );  pcSlice->setPPSId(uiCode);
1236  pps = parameterSetManager->getPPS(uiCode);
1237  //!KS: need to add error handling code here, if PPS is not available
1238  assert(pps!=0);
1239  sps = parameterSetManager->getSPS(pps->getSPSId());
1240  //!KS: need to add error handling code here, if SPS is not available
1241  assert(sps!=0);
1242
1243#if SVC_EXTENSION
1244  pcSlice->setSPS(sps);
1245  pcSlice->setPPS(pps);
1246
1247  TComVPS* vps = parameterSetManager->getVPS(sps->getVPSId());
1248  pcSlice->setVPS(vps);
1249
1250  Int iPOClsb = 0;
1251#endif
1252
1253  const ChromaFormat chFmt = sps->getChromaFormatIdc();
1254  const UInt numValidComp=getNumberValidComponents(chFmt);
1255  const Bool bChroma=(chFmt!=CHROMA_400);
1256
1257  if( pps->getDependentSliceSegmentsEnabledFlag() && ( !firstSliceSegmentInPic ))
1258  {
1259    READ_FLAG( uiCode, "dependent_slice_segment_flag" );       pcSlice->setDependentSliceSegmentFlag(uiCode ? true : false);
1260  }
1261  else
1262  {
1263    pcSlice->setDependentSliceSegmentFlag(false);
1264  }
1265#if SVC_EXTENSION
1266  Int numCTUs = ((pcSlice->getPicWidthInLumaSamples()+sps->getMaxCUWidth()-1)/sps->getMaxCUWidth())*((pcSlice->getPicHeightInLumaSamples()+sps->getMaxCUHeight()-1)/sps->getMaxCUHeight());
1267#else
1268  Int numCTUs = ((sps->getPicWidthInLumaSamples()+sps->getMaxCUWidth()-1)/sps->getMaxCUWidth())*((sps->getPicHeightInLumaSamples()+sps->getMaxCUHeight()-1)/sps->getMaxCUHeight());
1269#endif 
1270  UInt sliceSegmentAddress = 0;
1271  Int bitsSliceSegmentAddress = 0;
1272  while(numCTUs>(1<<bitsSliceSegmentAddress))
1273  {
1274    bitsSliceSegmentAddress++;
1275  }
1276
1277  if(!firstSliceSegmentInPic)
1278  {
1279    READ_CODE( bitsSliceSegmentAddress, sliceSegmentAddress, "slice_segment_address" );
1280  }
1281  //set uiCode to equal slice start address (or dependent slice start address)
1282  pcSlice->setSliceSegmentCurStartCtuTsAddr( sliceSegmentAddress );// this is actually a Raster-Scan (RS) address, but we do not have the RS->TS conversion table defined yet.
1283  pcSlice->setSliceSegmentCurEndCtuTsAddr(numCTUs);                // Set end as the last CTU of the picture.
1284
1285  if (!pcSlice->getDependentSliceSegmentFlag())
1286  {
1287    pcSlice->setSliceCurStartCtuTsAddr(sliceSegmentAddress); // this is actually a Raster-Scan (RS) address, but we do not have the RS->TS conversion table defined yet.
1288    pcSlice->setSliceCurEndCtuTsAddr(numCTUs);
1289  }
1290
1291  if(!pcSlice->getDependentSliceSegmentFlag())
1292  {
1293#if SVC_EXTENSION
1294    Int iBits = 0;
1295    if(pps->getNumExtraSliceHeaderBits() > iBits)
1296    {
1297      READ_FLAG(uiCode, "discardable_flag");
1298      pcSlice->setDiscardableFlag( uiCode ? true : false );
1299
1300      if( uiCode )
1301      {
1302        assert(pcSlice->getNalUnitType() != NAL_UNIT_CODED_SLICE_TRAIL_R &&
1303          pcSlice->getNalUnitType() != NAL_UNIT_CODED_SLICE_TSA_R &&
1304          pcSlice->getNalUnitType() != NAL_UNIT_CODED_SLICE_STSA_R &&
1305          pcSlice->getNalUnitType() != NAL_UNIT_CODED_SLICE_RADL_R &&
1306          pcSlice->getNalUnitType() != NAL_UNIT_CODED_SLICE_RASL_R);
1307      }
1308
1309      iBits++;
1310    }
1311
1312    if(pps->getNumExtraSliceHeaderBits() > iBits)
1313    {
1314      READ_FLAG(uiCode, "cross_layer_bla_flag");  pcSlice->setCrossLayerBLAFlag( uiCode ? true : false );
1315      iBits++;
1316    }
1317
1318    for ( ; iBits < pps->getNumExtraSliceHeaderBits(); iBits++)
1319    {
1320      READ_FLAG(uiCode, "slice_reserved_undetermined_flag[]"); // ignored
1321    }
1322#else //SVC_EXTENSION
1323    for (Int i = 0; i < pps->getNumExtraSliceHeaderBits(); i++)
1324    {
1325      READ_FLAG(uiCode, "slice_reserved_flag[]"); // ignored
1326    }
1327#endif //SVC_EXTENSION
1328
1329    READ_UVLC (    uiCode, "slice_type" );            pcSlice->setSliceType((SliceType)uiCode);
1330    if( pps->getOutputFlagPresentFlag() )
1331    {
1332      READ_FLAG( uiCode, "pic_output_flag" );    pcSlice->setPicOutputFlag( uiCode ? true : false );
1333    }
1334    else
1335    {
1336      pcSlice->setPicOutputFlag( true );
1337    }
1338
1339    // if (separate_colour_plane_flag == 1)
1340    //   read colour_plane_id
1341    //   (separate_colour_plane_flag == 1) is not supported in this version of the standard.
1342
1343    if( pcSlice->getIdrPicFlag() )
1344    {
1345      pcSlice->setPOC(0);
1346      TComReferencePictureSet* rps = pcSlice->getLocalRPS();
1347      (*rps)=TComReferencePictureSet();
1348      pcSlice->setRPS(rps);
1349    }
1350
1351#if SVC_EXTENSION
1352    if( ( pcSlice->getLayerId() > 0 && !vps->getPocLsbNotPresentFlag( vps->getLayerIdxInVps(pcSlice->getLayerId())) ) || !pcSlice->getIdrPicFlag() )
1353#else
1354    else
1355#endif
1356    {
1357      READ_CODE(sps->getBitsForPOC(), uiCode, "slice_pic_order_cnt_lsb");
1358#if SVC_EXTENSION
1359      pcSlice->setPicOrderCntLsb( uiCode );
1360
1361      iPOClsb = uiCode;
1362#else
1363      Int iPOClsb = uiCode;
1364#endif
1365      Int iPrevPOC = prevTid0POC;
1366      Int iMaxPOClsb = 1<< sps->getBitsForPOC();
1367      Int iPrevPOClsb = iPrevPOC & (iMaxPOClsb - 1);
1368      Int iPrevPOCmsb = iPrevPOC-iPrevPOClsb;
1369      Int iPOCmsb;
1370      if( ( iPOClsb  <  iPrevPOClsb ) && ( ( iPrevPOClsb - iPOClsb )  >=  ( iMaxPOClsb / 2 ) ) )
1371      {
1372        iPOCmsb = iPrevPOCmsb + iMaxPOClsb;
1373      }
1374      else if( (iPOClsb  >  iPrevPOClsb )  && ( (iPOClsb - iPrevPOClsb )  >  ( iMaxPOClsb / 2 ) ) )
1375      {
1376        iPOCmsb = iPrevPOCmsb - iMaxPOClsb;
1377      }
1378      else
1379      {
1380        iPOCmsb = iPrevPOCmsb;
1381      }
1382      if ( pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP
1383        || pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL
1384        || pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP )
1385      {
1386        // For BLA picture types, POCmsb is set to 0.
1387        iPOCmsb = 0;
1388      }
1389      pcSlice->setPOC              (iPOCmsb+iPOClsb);
1390
1391#if SVC_EXTENSION
1392    }
1393    else
1394    {
1395      pcSlice->setPicOrderCntLsb( 0 );
1396    }
1397
1398    if( !pcSlice->getIdrPicFlag() )
1399    {
1400#endif
1401      TComReferencePictureSet* rps;
1402      rps = pcSlice->getLocalRPS();
1403      (*rps)=TComReferencePictureSet();
1404
1405      pcSlice->setRPS(rps);
1406      READ_FLAG( uiCode, "short_term_ref_pic_set_sps_flag" );
1407      if(uiCode == 0) // use short-term reference picture set explicitly signalled in slice header
1408      {
1409        parseShortTermRefPicSet(sps,rps, sps->getRPSList()->getNumberOfReferencePictureSets());
1410      }
1411      else // use reference to short-term reference picture set in PPS
1412      {
1413        Int numBits = 0;
1414        while ((1 << numBits) < sps->getRPSList()->getNumberOfReferencePictureSets())
1415        {
1416          numBits++;
1417        }
1418        if (numBits > 0)
1419        {
1420          READ_CODE( numBits, uiCode, "short_term_ref_pic_set_idx");
1421        }
1422        else
1423        {
1424          uiCode = 0;
1425       
1426        }
1427        *rps = *(sps->getRPSList()->getReferencePictureSet(uiCode));
1428      }
1429      if(sps->getLongTermRefsPresent())
1430      {
1431        Int offset = rps->getNumberOfNegativePictures()+rps->getNumberOfPositivePictures();
1432        UInt numOfLtrp = 0;
1433        UInt numLtrpInSPS = 0;
1434        if (sps->getNumLongTermRefPicSPS() > 0)
1435        {
1436          READ_UVLC( uiCode, "num_long_term_sps");
1437          numLtrpInSPS = uiCode;
1438          numOfLtrp += numLtrpInSPS;
1439          rps->setNumberOfLongtermPictures(numOfLtrp);
1440        }
1441        Int bitsForLtrpInSPS = 0;
1442        while (sps->getNumLongTermRefPicSPS() > (1 << bitsForLtrpInSPS))
1443        {
1444          bitsForLtrpInSPS++;
1445        }
1446        READ_UVLC( uiCode, "num_long_term_pics");             rps->setNumberOfLongtermPictures(uiCode);
1447        numOfLtrp += uiCode;
1448        rps->setNumberOfLongtermPictures(numOfLtrp);
1449        Int maxPicOrderCntLSB = 1 << sps->getBitsForPOC();
1450        Int prevDeltaMSB = 0, deltaPocMSBCycleLT = 0;
1451        for(Int j=offset+rps->getNumberOfLongtermPictures()-1, k = 0; k < numOfLtrp; j--, k++)
1452        {
1453          Int pocLsbLt;
1454          if (k < numLtrpInSPS)
1455          {
1456            uiCode = 0;
1457            if (bitsForLtrpInSPS > 0)
1458            {
1459              READ_CODE(bitsForLtrpInSPS, uiCode, "lt_idx_sps[i]");
1460            }
1461            Bool usedByCurrFromSPS=sps->getUsedByCurrPicLtSPSFlag(uiCode);
1462
1463            pocLsbLt = sps->getLtRefPicPocLsbSps(uiCode);
1464            rps->setUsed(j,usedByCurrFromSPS);
1465          }
1466          else
1467          {
1468            READ_CODE(sps->getBitsForPOC(), uiCode, "poc_lsb_lt"); pocLsbLt= uiCode;
1469            READ_FLAG( uiCode, "used_by_curr_pic_lt_flag");     rps->setUsed(j,uiCode);
1470          }
1471          READ_FLAG(uiCode,"delta_poc_msb_present_flag");
1472          Bool mSBPresentFlag = uiCode ? true : false;
1473          if(mSBPresentFlag)
1474          {
1475            READ_UVLC( uiCode, "delta_poc_msb_cycle_lt[i]" );
1476            Bool deltaFlag = false;
1477            //            First LTRP                               || First LTRP from SH
1478            if( (j == offset+rps->getNumberOfLongtermPictures()-1) || (j == offset+(numOfLtrp-numLtrpInSPS)-1) )
1479            {
1480              deltaFlag = true;
1481            }
1482            if(deltaFlag)
1483            {
1484              deltaPocMSBCycleLT = uiCode;
1485            }
1486            else
1487            {
1488              deltaPocMSBCycleLT = uiCode + prevDeltaMSB;
1489            }
1490
1491            Int pocLTCurr = pcSlice->getPOC() - deltaPocMSBCycleLT * maxPicOrderCntLSB
1492                                        - iPOClsb + pocLsbLt;
1493            rps->setPOC     (j, pocLTCurr);
1494            rps->setDeltaPOC(j, - pcSlice->getPOC() + pocLTCurr);
1495            rps->setCheckLTMSBPresent(j,true);
1496          }
1497          else
1498          {
1499            rps->setPOC     (j, pocLsbLt);
1500            rps->setDeltaPOC(j, - pcSlice->getPOC() + pocLsbLt);
1501            rps->setCheckLTMSBPresent(j,false);
1502
1503            // reset deltaPocMSBCycleLT for first LTRP from slice header if MSB not present
1504            if( j == offset+(numOfLtrp-numLtrpInSPS)-1 )
1505            {
1506              deltaPocMSBCycleLT = 0;
1507            }
1508          }
1509          prevDeltaMSB = deltaPocMSBCycleLT;
1510        }
1511        offset += rps->getNumberOfLongtermPictures();
1512        rps->setNumberOfPictures(offset);
1513      }
1514
1515#if SVC_EXTENSION
1516      // DPB constraints
1517      if( pcSlice->getVPS()->getVpsExtensionFlag() == 1 )
1518      {
1519        for( Int ii = 1; ii < (pcSlice->getVPS()->getVpsNumLayerSetsMinus1() + 1); ii++ )  // prevent assert error when num_add_layer_sets > 0
1520        {
1521          Int layerSetIdxForOutputLayerSet = pcSlice->getVPS()->getOutputLayerSetIdx( ii );
1522          Int chkAssert=0;
1523          for(Int kk = 0; kk < pcSlice->getVPS()->getNumLayersInIdList(layerSetIdxForOutputLayerSet); kk++)
1524          {
1525            if( pcSlice->getVPS()->getNecessaryLayerFlag(ii, kk) && pcSlice->getLayerId() == pcSlice->getVPS()->getLayerSetLayerIdList(layerSetIdxForOutputLayerSet, kk) )
1526            {
1527              chkAssert=1;
1528            }
1529          }
1530
1531          if( chkAssert )
1532          {
1533            UInt layerIdc = pcSlice->getVPS()->getLayerIdcForOls( ii, pcSlice->getLayerId() );
1534            assert(rps->getNumberOfNegativePictures() <= pcSlice->getVPS()->getMaxVpsDecPicBufferingMinus1(ii, layerIdc, pcSlice->getVPS()->getMaxSLayersInLayerSetMinus1(ii)));
1535            assert(rps->getNumberOfPositivePictures() <= pcSlice->getVPS()->getMaxVpsDecPicBufferingMinus1(ii, layerIdc, pcSlice->getVPS()->getMaxSLayersInLayerSetMinus1(ii)) - rps->getNumberOfNegativePictures());
1536            assert((rps->getNumberOfPositivePictures() + rps->getNumberOfNegativePictures() + rps->getNumberOfLongtermPictures()) <= pcSlice->getVPS()->getMaxVpsDecPicBufferingMinus1(ii, layerIdc, pcSlice->getVPS()->getMaxSLayersInLayerSetMinus1(ii)));
1537          }
1538        }
1539      }
1540
1541      if(pcSlice->getLayerId() == 0)
1542      {
1543        assert(rps->getNumberOfNegativePictures() <= pcSlice->getSPS()->getMaxDecPicBuffering(pcSlice->getSPS()->getMaxTLayers()-1) );
1544        assert(rps->getNumberOfPositivePictures() <= pcSlice->getSPS()->getMaxDecPicBuffering(pcSlice->getSPS()->getMaxTLayers()-1) -rps->getNumberOfNegativePictures());
1545        assert((rps->getNumberOfPositivePictures() + rps->getNumberOfNegativePictures() + rps->getNumberOfLongtermPictures()) <= pcSlice->getSPS()->getMaxDecPicBuffering(pcSlice->getSPS()->getMaxTLayers()-1));
1546      }
1547#endif
1548
1549      if ( pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP
1550        || pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL
1551        || pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP )
1552      {
1553        // In the case of BLA picture types, rps data is read from slice header but ignored
1554        rps = pcSlice->getLocalRPS();
1555        (*rps)=TComReferencePictureSet();
1556        pcSlice->setRPS(rps);
1557      }
1558      if (sps->getTMVPFlagsPresent())
1559      {
1560        READ_FLAG( uiCode, "slice_temporal_mvp_enabled_flag" );
1561        pcSlice->setEnableTMVPFlag( uiCode == 1 ? true : false );
1562      }
1563      else
1564      {
1565        pcSlice->setEnableTMVPFlag(false);
1566      }
1567    }
1568
1569#if SVC_EXTENSION
1570    pcSlice->setActiveNumILRRefIdx(0);
1571    if((pcSlice->getLayerId() > 0) && !(vps->getIlpSshSignalingEnabledFlag()) && (pcSlice->getNumILRRefIdx() > 0) )
1572    {
1573      READ_FLAG(uiCode,"inter_layer_pred_enabled_flag");
1574      pcSlice->setInterLayerPredEnabledFlag(uiCode);
1575      if( pcSlice->getInterLayerPredEnabledFlag())
1576      {
1577        if(pcSlice->getNumILRRefIdx() > 1)
1578        {
1579          Int numBits = 1;
1580          while ((1 << numBits) < pcSlice->getNumILRRefIdx())
1581          {
1582            numBits++;
1583          }
1584          if( !vps->getMaxOneActiveRefLayerFlag())
1585          {
1586            READ_CODE( numBits, uiCode,"num_inter_layer_ref_pics_minus1" );
1587            pcSlice->setActiveNumILRRefIdx(uiCode + 1);
1588          }
1589          else
1590          {
1591            for( Int i = 0; i < pcSlice->getNumILRRefIdx(); i++ ) 
1592            {
1593              if( ( vps->getMaxTidIlRefPicsPlus1(vps->getLayerIdxInVps(i), pcSlice->getLayerIdx()) > pcSlice->getTLayer() || pcSlice->getTLayer()==0 ) &&
1594                    vps->getMaxTSLayersMinus1(vps->getLayerIdxInVps(i)) >=  pcSlice->getTLayer() )
1595              {         
1596                pcSlice->setActiveNumILRRefIdx(1);
1597                break;
1598              }
1599            }
1600          }
1601
1602          if( pcSlice->getActiveNumILRRefIdx() == pcSlice->getNumILRRefIdx() )
1603          {
1604            for( Int i = 0; i < pcSlice->getActiveNumILRRefIdx(); i++ )
1605            {
1606              pcSlice->setInterLayerPredLayerIdc(i,i);
1607            }
1608          }
1609          else
1610          {
1611            for(Int i = 0; i < pcSlice->getActiveNumILRRefIdx(); i++ )
1612            {
1613              READ_CODE( numBits,uiCode,"inter_layer_pred_layer_idc[i]" );
1614              pcSlice->setInterLayerPredLayerIdc(uiCode, i);
1615            }
1616          }
1617        }
1618        else
1619        {
1620          Int refLayerId = vps->getRefLayerId(pcSlice->getLayerId(), 0);
1621          Int refLayerIdx = vps->getLayerIdxInVps(refLayerId);
1622
1623          if( ( vps->getMaxTidIlRefPicsPlus1(refLayerIdx, pcSlice->getLayerIdx()) > pcSlice->getTLayer() || pcSlice->getTLayer()==0 ) &&
1624                vps->getMaxTSLayersMinus1(refLayerIdx) >=  pcSlice->getTLayer() )
1625          {
1626            pcSlice->setActiveNumILRRefIdx(1);
1627            pcSlice->setInterLayerPredLayerIdc(0, 0);
1628          }
1629        }
1630      }
1631    }
1632    else if( vps->getIlpSshSignalingEnabledFlag() == true &&  (pcSlice->getLayerId() > 0 ))
1633    {
1634      pcSlice->setInterLayerPredEnabledFlag(true);
1635
1636      Int   numRefLayerPics = 0;
1637      Int   i = 0;
1638      Int   refLayerPicIdc  [MAX_VPS_LAYER_IDX_PLUS1];
1639      for(i = 0, numRefLayerPics = 0;  i < pcSlice->getNumILRRefIdx(); i++ ) 
1640      {
1641        if( ( vps->getMaxTidIlRefPicsPlus1(vps->getLayerIdxInVps(i), pcSlice->getLayerIdx()) > pcSlice->getTLayer() || pcSlice->getTLayer()==0 ) &&
1642              vps->getMaxTSLayersMinus1(vps->getLayerIdxInVps(i)) >=  pcSlice->getTLayer() )
1643        {         
1644          refLayerPicIdc[ numRefLayerPics++ ] = i;
1645        }
1646      }
1647      pcSlice->setActiveNumILRRefIdx(numRefLayerPics);
1648      for( i = 0; i < pcSlice->getActiveNumILRRefIdx(); i++ )
1649      {
1650        pcSlice->setInterLayerPredLayerIdc(refLayerPicIdc[i], i);
1651      }
1652    }
1653#endif //SVC_EXTENSION
1654
1655    if(sps->getUseSAO())
1656    {
1657      READ_FLAG(uiCode, "slice_sao_luma_flag");  pcSlice->setSaoEnabledFlag(CHANNEL_TYPE_LUMA, (Bool)uiCode);
1658#if SVC_EXTENSION
1659      ChromaFormat format;
1660      if( sps->getLayerId() == 0 )
1661      {
1662        format = sps->getChromaFormatIdc();
1663      }
1664      else
1665      {
1666        format = vps->getVpsRepFormat( sps->getUpdateRepFormatFlag() ? sps->getUpdateRepFormatIndex() : vps->getVpsRepFormatIdx( vps->getLayerIdxInVps(sps->getLayerId()) ) )->getChromaFormatVpsIdc();
1667
1668        // conformance check
1669        assert( (sps->getUpdateRepFormatFlag()==false && vps->getVpsNumRepFormats()==1) || vps->getVpsNumRepFormats() > 1 ); 
1670      }
1671      if (format != CHROMA_400)
1672#else
1673      if (bChroma)
1674#endif
1675      {
1676        READ_FLAG(uiCode, "slice_sao_chroma_flag");  pcSlice->setSaoEnabledFlag(CHANNEL_TYPE_CHROMA, (Bool)uiCode);
1677      }
1678#if SVC_EXTENSION
1679      else
1680      {
1681        pcSlice->setSaoEnabledFlag(CHANNEL_TYPE_CHROMA, false);
1682      }
1683#endif
1684    }
1685
1686    if (pcSlice->getIdrPicFlag())
1687    {
1688      pcSlice->setEnableTMVPFlag(false);
1689    }
1690    if (!pcSlice->isIntra())
1691    {
1692
1693      READ_FLAG( uiCode, "num_ref_idx_active_override_flag");
1694      if (uiCode)
1695      {
1696        READ_UVLC (uiCode, "num_ref_idx_l0_active_minus1" );  pcSlice->setNumRefIdx( REF_PIC_LIST_0, uiCode + 1 );
1697        if (pcSlice->isInterB())
1698        {
1699          READ_UVLC (uiCode, "num_ref_idx_l1_active_minus1" );  pcSlice->setNumRefIdx( REF_PIC_LIST_1, uiCode + 1 );
1700        }
1701        else
1702        {
1703          pcSlice->setNumRefIdx(REF_PIC_LIST_1, 0);
1704        }
1705      }
1706      else
1707      {
1708        pcSlice->setNumRefIdx(REF_PIC_LIST_0, pps->getNumRefIdxL0DefaultActive());
1709        if (pcSlice->isInterB())
1710        {
1711          pcSlice->setNumRefIdx(REF_PIC_LIST_1, pps->getNumRefIdxL1DefaultActive());
1712        }
1713        else
1714        {
1715          pcSlice->setNumRefIdx(REF_PIC_LIST_1,0);
1716        }
1717      }
1718    }
1719    // }
1720    TComRefPicListModification* refPicListModification = pcSlice->getRefPicListModification();
1721    if(!pcSlice->isIntra())
1722    {
1723      if( !pps->getListsModificationPresentFlag() || pcSlice->getNumRpsCurrTempList() <= 1 )
1724      {
1725        refPicListModification->setRefPicListModificationFlagL0( 0 );
1726      }
1727      else
1728      {
1729        READ_FLAG( uiCode, "ref_pic_list_modification_flag_l0" ); refPicListModification->setRefPicListModificationFlagL0( uiCode ? 1 : 0 );
1730      }
1731
1732      if(refPicListModification->getRefPicListModificationFlagL0())
1733      {
1734        uiCode = 0;
1735        Int i = 0;
1736        Int numRpsCurrTempList0 = pcSlice->getNumRpsCurrTempList();
1737        if ( numRpsCurrTempList0 > 1 )
1738        {
1739          Int length = 1;
1740          numRpsCurrTempList0 --;
1741          while ( numRpsCurrTempList0 >>= 1)
1742          {
1743            length ++;
1744          }
1745          for (i = 0; i < pcSlice->getNumRefIdx(REF_PIC_LIST_0); i ++)
1746          {
1747            READ_CODE( length, uiCode, "list_entry_l0" );
1748            refPicListModification->setRefPicSetIdxL0(i, uiCode );
1749          }
1750        }
1751        else
1752        {
1753          for (i = 0; i < pcSlice->getNumRefIdx(REF_PIC_LIST_0); i ++)
1754          {
1755            refPicListModification->setRefPicSetIdxL0(i, 0 );
1756          }
1757        }
1758      }
1759    }
1760    else
1761    {
1762      refPicListModification->setRefPicListModificationFlagL0(0);
1763    }
1764    if(pcSlice->isInterB())
1765    {
1766      if( !pps->getListsModificationPresentFlag() || pcSlice->getNumRpsCurrTempList() <= 1 )
1767      {
1768        refPicListModification->setRefPicListModificationFlagL1( 0 );
1769      }
1770      else
1771      {
1772        READ_FLAG( uiCode, "ref_pic_list_modification_flag_l1" ); refPicListModification->setRefPicListModificationFlagL1( uiCode ? 1 : 0 );
1773      }
1774      if(refPicListModification->getRefPicListModificationFlagL1())
1775      {
1776        uiCode = 0;
1777        Int i = 0;
1778        Int numRpsCurrTempList1 = pcSlice->getNumRpsCurrTempList();
1779        if ( numRpsCurrTempList1 > 1 )
1780        {
1781          Int length = 1;
1782          numRpsCurrTempList1 --;
1783          while ( numRpsCurrTempList1 >>= 1)
1784          {
1785            length ++;
1786          }
1787          for (i = 0; i < pcSlice->getNumRefIdx(REF_PIC_LIST_1); i ++)
1788          {
1789            READ_CODE( length, uiCode, "list_entry_l1" );
1790            refPicListModification->setRefPicSetIdxL1(i, uiCode );
1791          }
1792        }
1793        else
1794        {
1795          for (i = 0; i < pcSlice->getNumRefIdx(REF_PIC_LIST_1); i ++)
1796          {
1797            refPicListModification->setRefPicSetIdxL1(i, 0 );
1798          }
1799        }
1800      }
1801    }
1802    else
1803    {
1804      refPicListModification->setRefPicListModificationFlagL1(0);
1805    }
1806    if (pcSlice->isInterB())
1807    {
1808      READ_FLAG( uiCode, "mvd_l1_zero_flag" );       pcSlice->setMvdL1ZeroFlag( (uiCode ? true : false) );
1809    }
1810
1811    pcSlice->setCabacInitFlag( false ); // default
1812    if(pps->getCabacInitPresentFlag() && !pcSlice->isIntra())
1813    {
1814      READ_FLAG(uiCode, "cabac_init_flag");
1815      pcSlice->setCabacInitFlag( uiCode ? true : false );
1816    }
1817
1818    if ( pcSlice->getEnableTMVPFlag() )
1819    {
1820      if ( pcSlice->getSliceType() == B_SLICE )
1821      {
1822        READ_FLAG( uiCode, "collocated_from_l0_flag" );
1823        pcSlice->setColFromL0Flag(uiCode);
1824      }
1825      else
1826      {
1827        pcSlice->setColFromL0Flag( 1 );
1828      }
1829
1830      if ( pcSlice->getSliceType() != I_SLICE &&
1831          ((pcSlice->getColFromL0Flag() == 1 && pcSlice->getNumRefIdx(REF_PIC_LIST_0) > 1)||
1832           (pcSlice->getColFromL0Flag() == 0 && pcSlice->getNumRefIdx(REF_PIC_LIST_1) > 1)))
1833      {
1834        READ_UVLC( uiCode, "collocated_ref_idx" );
1835        pcSlice->setColRefIdx(uiCode);
1836      }
1837      else
1838      {
1839        pcSlice->setColRefIdx(0);
1840      }
1841    }
1842    if ( (pps->getUseWP() && pcSlice->getSliceType()==P_SLICE) || (pps->getWPBiPred() && pcSlice->getSliceType()==B_SLICE) )
1843    {
1844      xParsePredWeightTable(pcSlice, sps);
1845      pcSlice->initWpScaling(sps);
1846    }
1847    if (!pcSlice->isIntra())
1848    {
1849      READ_UVLC( uiCode, "five_minus_max_num_merge_cand");
1850      pcSlice->setMaxNumMergeCand(MRG_MAX_NUM_CANDS - uiCode);
1851    }
1852
1853    READ_SVLC( iCode, "slice_qp_delta" );
1854    pcSlice->setSliceQp (26 + pps->getPicInitQPMinus26() + iCode);
1855
1856#if SVC_EXTENSION
1857    assert( pcSlice->getSliceQp() >= -pcSlice->getQpBDOffset(CHANNEL_TYPE_LUMA) );
1858#else   
1859    assert( pcSlice->getSliceQp() >= -sps->getQpBDOffset(CHANNEL_TYPE_LUMA) );
1860#endif
1861    assert( pcSlice->getSliceQp() <=  51 );
1862
1863    if (pps->getSliceChromaQpFlag())
1864    {
1865      if (numValidComp>COMPONENT_Cb)
1866      {
1867        READ_SVLC( iCode, "slice_cb_qp_offset" );
1868        pcSlice->setSliceChromaQpDelta(COMPONENT_Cb, iCode );
1869        assert( pcSlice->getSliceChromaQpDelta(COMPONENT_Cb) >= -12 );
1870        assert( pcSlice->getSliceChromaQpDelta(COMPONENT_Cb) <=  12 );
1871        assert( (pps->getQpOffset(COMPONENT_Cb) + pcSlice->getSliceChromaQpDelta(COMPONENT_Cb)) >= -12 );
1872        assert( (pps->getQpOffset(COMPONENT_Cb) + pcSlice->getSliceChromaQpDelta(COMPONENT_Cb)) <=  12 );
1873      }
1874
1875      if (numValidComp>COMPONENT_Cr)
1876      {
1877        READ_SVLC( iCode, "slice_cr_qp_offset" );
1878        pcSlice->setSliceChromaQpDelta(COMPONENT_Cr, iCode );
1879        assert( pcSlice->getSliceChromaQpDelta(COMPONENT_Cr) >= -12 );
1880        assert( pcSlice->getSliceChromaQpDelta(COMPONENT_Cr) <=  12 );
1881        assert( (pps->getQpOffset(COMPONENT_Cr) + pcSlice->getSliceChromaQpDelta(COMPONENT_Cr)) >= -12 );
1882        assert( (pps->getQpOffset(COMPONENT_Cr) + pcSlice->getSliceChromaQpDelta(COMPONENT_Cr)) <=  12 );
1883      }
1884    }
1885
1886    if (pps->getPpsRangeExtension().getChromaQpOffsetListEnabledFlag())
1887    {
1888      READ_FLAG(uiCode, "cu_chroma_qp_offset_enabled_flag"); pcSlice->setUseChromaQpAdj(uiCode != 0);
1889    }
1890    else
1891    {
1892      pcSlice->setUseChromaQpAdj(false);
1893    }
1894
1895    if (pps->getDeblockingFilterControlPresentFlag())
1896    {
1897      if(pps->getDeblockingFilterOverrideEnabledFlag())
1898      {
1899        READ_FLAG ( uiCode, "deblocking_filter_override_flag" );        pcSlice->setDeblockingFilterOverrideFlag(uiCode ? true : false);
1900      }
1901      else
1902      {
1903        pcSlice->setDeblockingFilterOverrideFlag(0);
1904      }
1905      if(pcSlice->getDeblockingFilterOverrideFlag())
1906      {
1907        READ_FLAG ( uiCode, "slice_disable_deblocking_filter_flag" );   pcSlice->setDeblockingFilterDisable(uiCode ? 1 : 0);
1908        if(!pcSlice->getDeblockingFilterDisable())
1909        {
1910          READ_SVLC( iCode, "slice_beta_offset_div2" );                       pcSlice->setDeblockingFilterBetaOffsetDiv2(iCode);
1911          assert(pcSlice->getDeblockingFilterBetaOffsetDiv2() >= -6 &&
1912                 pcSlice->getDeblockingFilterBetaOffsetDiv2() <=  6);
1913          READ_SVLC( iCode, "slice_tc_offset_div2" );                         pcSlice->setDeblockingFilterTcOffsetDiv2(iCode);
1914          assert(pcSlice->getDeblockingFilterTcOffsetDiv2() >= -6 &&
1915                 pcSlice->getDeblockingFilterTcOffsetDiv2() <=  6);
1916        }
1917      }
1918      else
1919      {
1920        pcSlice->setDeblockingFilterDisable   ( pps->getPicDisableDeblockingFilterFlag() );
1921        pcSlice->setDeblockingFilterBetaOffsetDiv2( pps->getDeblockingFilterBetaOffsetDiv2() );
1922        pcSlice->setDeblockingFilterTcOffsetDiv2  ( pps->getDeblockingFilterTcOffsetDiv2() );
1923      }
1924    }
1925    else
1926    {
1927      pcSlice->setDeblockingFilterDisable       ( false );
1928      pcSlice->setDeblockingFilterBetaOffsetDiv2( 0 );
1929      pcSlice->setDeblockingFilterTcOffsetDiv2  ( 0 );
1930    }
1931
1932    Bool isSAOEnabled = sps->getUseSAO() && (pcSlice->getSaoEnabledFlag(CHANNEL_TYPE_LUMA) || (bChroma && pcSlice->getSaoEnabledFlag(CHANNEL_TYPE_CHROMA)));
1933    Bool isDBFEnabled = (!pcSlice->getDeblockingFilterDisable());
1934
1935    if(pps->getLoopFilterAcrossSlicesEnabledFlag() && ( isSAOEnabled || isDBFEnabled ))
1936    {
1937      READ_FLAG( uiCode, "slice_loop_filter_across_slices_enabled_flag");
1938    }
1939    else
1940    {
1941      uiCode = pps->getLoopFilterAcrossSlicesEnabledFlag()?1:0;
1942    }
1943    pcSlice->setLFCrossSliceBoundaryFlag( (uiCode==1)?true:false);
1944
1945  }
1946
1947  std::vector<UInt> entryPointOffset;
1948  if( pps->getTilesEnabledFlag() || pps->getEntropyCodingSyncEnabledFlag() )
1949  {
1950    UInt numEntryPointOffsets;
1951    UInt offsetLenMinus1;
1952    READ_UVLC(numEntryPointOffsets, "num_entry_point_offsets");
1953    if (numEntryPointOffsets>0)
1954    {
1955      READ_UVLC(offsetLenMinus1, "offset_len_minus1");
1956      entryPointOffset.resize(numEntryPointOffsets);
1957      for (UInt idx=0; idx<numEntryPointOffsets; idx++)
1958      {
1959        READ_CODE(offsetLenMinus1+1, uiCode, "entry_point_offset_minus1");
1960        entryPointOffset[ idx ] = uiCode + 1;
1961      }
1962    }
1963  }
1964
1965#if SVC_EXTENSION
1966  Int sliceHeaderExtensionLength = 0;
1967  if(pps->getSliceHeaderExtensionPresentFlag())
1968  {
1969    READ_UVLC( uiCode, "slice_segment_header_extension_length"); sliceHeaderExtensionLength = uiCode;
1970  }
1971  else
1972  {
1973    sliceHeaderExtensionLength = 0;
1974    pcSlice->setPocMsbValPresentFlag( false );
1975  }
1976
1977  UInt startBits = m_pcBitstream->getNumBitsRead();     // Start counter of # SH Extn bits
1978  if( sliceHeaderExtensionLength > 0 )
1979  {
1980    if( pcSlice->getPPS()->getPocResetInfoPresentFlag() )
1981    {
1982      READ_CODE( 2, uiCode,       "poc_reset_idc"); pcSlice->setPocResetIdc(uiCode);
1983
1984      /* The value of poc_reset_idc shall not be equal to 1 or 2 for a RASL picture, a RADL picture,
1985      a sub-layer non-reference picture, or a picture that has TemporalId greater than 0,
1986      or a picture that has discardable_flag equal to 1. */
1987      if( pcSlice->getPocResetIdc() == 1 || pcSlice->getPocResetIdc() == 2 )
1988      {
1989        assert( !pcSlice->isRASL() );
1990        assert( !pcSlice->isRADL() );
1991        assert( !pcSlice->isSLNR() );
1992        assert( pcSlice->getTLayer() == 0 );
1993        assert( pcSlice->getDiscardableFlag() == 0 );
1994      }
1995
1996      // The value of poc_reset_idc of a CRA or BLA picture shall be less than 3.
1997      if( pcSlice->getPocResetIdc() == 3)
1998      {
1999        assert( ! ( pcSlice->isCRA() || pcSlice->isBLA() ) );
2000      }
2001    }
2002    else
2003    {
2004      pcSlice->setPocResetIdc( 0 );
2005    }
2006
2007    if( pcSlice->getVPS()->getPocLsbNotPresentFlag( pcSlice->getVPS()->getLayerIdxInVps(pcSlice->getLayerId()) ) && iPOClsb > 0 )
2008    {
2009      assert( pcSlice->getPocResetIdc() != 2 );
2010    }
2011
2012    if( pcSlice->getPocResetIdc() > 0 )
2013    {
2014      READ_CODE(6, uiCode,      "poc_reset_period_id"); pcSlice->setPocResetPeriodId(uiCode);
2015    }
2016    else
2017    {
2018
2019      pcSlice->setPocResetPeriodId( 0 );
2020    }
2021
2022    if( pcSlice->getPocResetIdc() == 3 )
2023    {
2024      READ_FLAG( uiCode,        "full_poc_reset_flag"); pcSlice->setFullPocResetFlag((uiCode == 1) ? true : false);
2025      READ_CODE(pcSlice->getSPS()->getBitsForPOC(), uiCode,"poc_lsb_val"); pcSlice->setPocLsbVal(uiCode);
2026
2027      if( pcSlice->getVPS()->getPocLsbNotPresentFlag( pcSlice->getVPS()->getLayerIdxInVps(pcSlice->getLayerId()) ) && pcSlice->getFullPocResetFlag() )
2028      {
2029        assert( pcSlice->getPocLsbVal() == 0 );
2030      }
2031    }
2032
2033    // Derive the value of PocMsbValRequiredFlag
2034    pcSlice->setPocMsbValRequiredFlag( (pcSlice->getCraPicFlag() || pcSlice->getBlaPicFlag())
2035      && (!pcSlice->getVPS()->getVpsPocLsbAlignedFlag() ||
2036      (pcSlice->getVPS()->getVpsPocLsbAlignedFlag() && pcSlice->getVPS()->getNumDirectRefLayers(pcSlice->getLayerId()) == 0))
2037      );
2038
2039    if( !pcSlice->getPocMsbValRequiredFlag() && pcSlice->getVPS()->getVpsPocLsbAlignedFlag() )
2040    {
2041      READ_FLAG(uiCode, "poc_msb_cycle_val_present_flag"); pcSlice->setPocMsbValPresentFlag(uiCode ? true : false);
2042    }
2043    else
2044    {
2045      if( pcSlice->getPocMsbValRequiredFlag() )
2046      {
2047        pcSlice->setPocMsbValPresentFlag( true );
2048      }
2049      else
2050      {
2051        pcSlice->setPocMsbValPresentFlag( false );
2052      }
2053    }
2054
2055    if( pcSlice->getPocMsbValPresentFlag() )
2056    {
2057      READ_UVLC( uiCode,    "poc_msb_cycle_val");             pcSlice->setPocMsbVal( uiCode );
2058    }
2059
2060    // Read remaining bits in the slice header extension.
2061    UInt endBits = m_pcBitstream->getNumBitsRead();
2062    Int counter = (endBits - startBits) % 8;
2063    if( counter )
2064    {
2065      counter = 8 - counter;
2066    }
2067
2068    while( counter )
2069    {
2070      READ_FLAG( uiCode, "slice_segment_header_extension_data_bit" );
2071      counter--;
2072    }
2073  }
2074#else
2075  if(pps->getSliceHeaderExtensionPresentFlag())
2076  {
2077    READ_UVLC(uiCode,"slice_segment_header_extension_length");
2078    for(Int i=0; i<uiCode; i++)
2079    {
2080      UInt ignore;
2081      READ_CODE(8,ignore,"slice_segment_header_extension_data_byte");
2082    }
2083  }
2084#endif
2085#if RExt__DECODER_DEBUG_BIT_STATISTICS
2086  TComCodingStatistics::IncrementStatisticEP(STATS__BYTE_ALIGNMENT_BITS,m_pcBitstream->readByteAlignment(),0);
2087#else
2088  m_pcBitstream->readByteAlignment();
2089#endif
2090
2091  pcSlice->clearSubstreamSizes();
2092
2093  if( pps->getTilesEnabledFlag() || pps->getEntropyCodingSyncEnabledFlag() )
2094  {
2095    Int endOfSliceHeaderLocation = m_pcBitstream->getByteLocation();
2096
2097    // Adjust endOfSliceHeaderLocation to account for emulation prevention bytes in the slice segment header
2098    for ( UInt curByteIdx  = 0; curByteIdx<m_pcBitstream->numEmulationPreventionBytesRead(); curByteIdx++ )
2099    {
2100      if ( m_pcBitstream->getEmulationPreventionByteLocation( curByteIdx ) < endOfSliceHeaderLocation )
2101      {
2102        endOfSliceHeaderLocation++;
2103      }
2104    }
2105
2106    Int  curEntryPointOffset     = 0;
2107    Int  prevEntryPointOffset    = 0;
2108    for (UInt idx=0; idx<entryPointOffset.size(); idx++)
2109    {
2110      curEntryPointOffset += entryPointOffset[ idx ];
2111
2112      Int emulationPreventionByteCount = 0;
2113      for ( UInt curByteIdx  = 0; curByteIdx<m_pcBitstream->numEmulationPreventionBytesRead(); curByteIdx++ )
2114      {
2115        if ( m_pcBitstream->getEmulationPreventionByteLocation( curByteIdx ) >= ( prevEntryPointOffset + endOfSliceHeaderLocation ) &&
2116             m_pcBitstream->getEmulationPreventionByteLocation( curByteIdx ) <  ( curEntryPointOffset  + endOfSliceHeaderLocation ) )
2117        {
2118          emulationPreventionByteCount++;
2119        }
2120      }
2121
2122      entryPointOffset[ idx ] -= emulationPreventionByteCount;
2123      prevEntryPointOffset = curEntryPointOffset;
2124      pcSlice->addSubstreamSize(entryPointOffset [ idx ] );
2125    }
2126  }
2127
2128  return;
2129}
2130
2131Void TDecCavlc::parsePTL( TComPTL *rpcPTL, Bool profilePresentFlag, Int maxNumSubLayersMinus1 )
2132{
2133  UInt uiCode;
2134  if(profilePresentFlag)
2135  {
2136    parseProfileTier(rpcPTL->getGeneralPTL(), false);
2137  }
2138  READ_CODE( 8, uiCode, "general_level_idc" );    rpcPTL->getGeneralPTL()->setLevelIdc(Level::Name(uiCode));
2139
2140  for (Int i = 0; i < maxNumSubLayersMinus1; i++)
2141  {
2142    READ_FLAG( uiCode, "sub_layer_profile_present_flag[i]" ); rpcPTL->setSubLayerProfilePresentFlag(i, uiCode);
2143    READ_FLAG( uiCode, "sub_layer_level_present_flag[i]"   ); rpcPTL->setSubLayerLevelPresentFlag  (i, uiCode);
2144  }
2145
2146  if (maxNumSubLayersMinus1 > 0)
2147  {
2148    for (Int i = maxNumSubLayersMinus1; i < 8; i++)
2149    {
2150      READ_CODE(2, uiCode, "reserved_zero_2bits");
2151      assert(uiCode == 0);
2152    }
2153  }
2154
2155  for(Int i = 0; i < maxNumSubLayersMinus1; i++)
2156  {
2157    if( rpcPTL->getSubLayerProfilePresentFlag(i) )
2158    {
2159      parseProfileTier(rpcPTL->getSubLayerPTL(i), true);
2160    }
2161    if(rpcPTL->getSubLayerLevelPresentFlag(i))
2162    {
2163      READ_CODE( 8, uiCode, "sub_layer_level_idc[i]" );   rpcPTL->getSubLayerPTL(i)->setLevelIdc(Level::Name(uiCode));
2164    }
2165  }
2166}
2167
2168#if ENC_DEC_TRACE || RExt__DECODER_DEBUG_BIT_STATISTICS
2169Void TDecCavlc::parseProfileTier(ProfileTierLevel *ptl, const Bool bIsSubLayer)
2170#define PTL_TRACE_TEXT(txt) bIsSubLayer?("sub_layer_" txt) : ("general_" txt)
2171#else
2172Void TDecCavlc::parseProfileTier(ProfileTierLevel *ptl, const Bool /*bIsSubLayer*/)
2173#define PTL_TRACE_TEXT(txt) txt
2174#endif
2175{
2176  UInt uiCode;
2177  READ_CODE(2 , uiCode,   PTL_TRACE_TEXT("profile_space"                   )); ptl->setProfileSpace(uiCode);
2178  READ_FLAG(    uiCode,   PTL_TRACE_TEXT("tier_flag"                       )); ptl->setTierFlag    (uiCode ? Level::HIGH : Level::MAIN);
2179  READ_CODE(5 , uiCode,   PTL_TRACE_TEXT("profile_idc"                     )); ptl->setProfileIdc  (Profile::Name(uiCode));
2180  for(Int j = 0; j < 32; j++)
2181  {
2182    READ_FLAG(  uiCode,   PTL_TRACE_TEXT("profile_compatibility_flag[][j]" )); ptl->setProfileCompatibilityFlag(j, uiCode ? 1 : 0);
2183  }
2184  READ_FLAG(uiCode,       PTL_TRACE_TEXT("progressive_source_flag"         )); ptl->setProgressiveSourceFlag(uiCode ? true : false);
2185
2186  READ_FLAG(uiCode,       PTL_TRACE_TEXT("interlaced_source_flag"          )); ptl->setInterlacedSourceFlag(uiCode ? true : false);
2187
2188  READ_FLAG(uiCode,       PTL_TRACE_TEXT("non_packed_constraint_flag"      )); ptl->setNonPackedConstraintFlag(uiCode ? true : false);
2189
2190  READ_FLAG(uiCode,       PTL_TRACE_TEXT("frame_only_constraint_flag"      )); ptl->setFrameOnlyConstraintFlag(uiCode ? true : false);
2191
2192  if (ptl->getProfileIdc() == Profile::MAINREXT           || ptl->getProfileCompatibilityFlag(Profile::MAINREXT) ||
2193      ptl->getProfileIdc() == Profile::HIGHTHROUGHPUTREXT || ptl->getProfileCompatibilityFlag(Profile::HIGHTHROUGHPUTREXT
2194#if SCALABLE_REXT
2195      || ptl->getProfileIdc() == Profile::SCALABLEREXT
2196#endif
2197      ))
2198  {
2199    UInt maxBitDepth=16;
2200    READ_FLAG(    uiCode, PTL_TRACE_TEXT("max_12bit_constraint_flag"       )); if (uiCode) maxBitDepth=12;
2201    READ_FLAG(    uiCode, PTL_TRACE_TEXT("max_10bit_constraint_flag"       )); if (uiCode) maxBitDepth=10;
2202    READ_FLAG(    uiCode, PTL_TRACE_TEXT("max_8bit_constraint_flag"        )); if (uiCode) maxBitDepth=8;
2203    ptl->setBitDepthConstraint(maxBitDepth);
2204    ChromaFormat chromaFmtConstraint=CHROMA_444;
2205    READ_FLAG(    uiCode, PTL_TRACE_TEXT("max_422chroma_constraint_flag"   )); if (uiCode) chromaFmtConstraint=CHROMA_422;
2206    READ_FLAG(    uiCode, PTL_TRACE_TEXT("max_420chroma_constraint_flag"   )); if (uiCode) chromaFmtConstraint=CHROMA_420;
2207    READ_FLAG(    uiCode, PTL_TRACE_TEXT("max_monochrome_constraint_flag"  )); if (uiCode) chromaFmtConstraint=CHROMA_400;
2208    ptl->setChromaFormatConstraint(chromaFmtConstraint);
2209    READ_FLAG(    uiCode, PTL_TRACE_TEXT("intra_constraint_flag"           )); ptl->setIntraConstraintFlag(uiCode != 0);
2210    READ_FLAG(    uiCode, PTL_TRACE_TEXT("one_picture_only_constraint_flag")); ptl->setOnePictureOnlyConstraintFlag(uiCode != 0);
2211    READ_FLAG(    uiCode, PTL_TRACE_TEXT("lower_bit_rate_constraint_flag"  )); ptl->setLowerBitRateConstraintFlag(uiCode != 0);
2212#if SVC_EXTENSION
2213    READ_CODE(32, uiCode, "reserved_zero_34bits");  READ_CODE(2, uiCode, "reserved_zero_34bits");
2214  }
2215  else if( ptl->getProfileIdc() == Profile::SCALABLEMAIN || ptl->getProfileCompatibilityFlag(Profile::SCALABLEMAIN) )
2216  {
2217    READ_FLAG(    uiCode, "max_12bit_constraint_flag" ); assert (uiCode == 1);
2218    READ_FLAG(    uiCode, "max_10bit_constraint_flag" ); assert (uiCode == 1);
2219    READ_FLAG(    uiCode, "max_8bit_constraint_flag"  ); ptl->setProfileIdc  ((uiCode) ? Profile::SCALABLEMAIN : Profile::SCALABLEMAIN10);
2220    READ_FLAG(    uiCode, "max_422chroma_constraint_flag"  ); assert (uiCode == 1);
2221    READ_FLAG(    uiCode, "max_420chroma_constraint_flag"  ); assert (uiCode == 1);
2222    READ_FLAG(    uiCode, "max_monochrome_constraint_flag" ); assert (uiCode == 0);
2223    READ_FLAG(    uiCode, "intra_constraint_flag"); assert (uiCode == 0);
2224    READ_FLAG(    uiCode, "one_picture_only_constraint_flag"); assert (uiCode == 0);
2225    READ_FLAG(    uiCode, "lower_bit_rate_constraint_flag"); assert (uiCode == 1);
2226    READ_CODE(32, uiCode, "reserved_zero_34bits");  READ_CODE(2, uiCode, "reserved_zero_34bits");
2227  }
2228  else
2229  {
2230    ptl->setBitDepthConstraint((ptl->getProfileIdc() == Profile::MAIN10)?10:8);
2231    ptl->setChromaFormatConstraint(CHROMA_420);
2232    ptl->setIntraConstraintFlag(false);
2233    ptl->setLowerBitRateConstraintFlag(true);
2234    READ_CODE(32,  uiCode, "reserved_zero_43bits");  READ_CODE(11,  uiCode, "reserved_zero_43bits");
2235  }
2236#else
2237    READ_CODE(16, uiCode, PTL_TRACE_TEXT("reserved_zero_34bits[0..15]"     ));
2238    READ_CODE(16, uiCode, PTL_TRACE_TEXT("reserved_zero_34bits[16..31]"    ));
2239    READ_CODE(2,  uiCode, PTL_TRACE_TEXT("reserved_zero_34bits[32..33]"    ));
2240  }
2241  else
2242  {
2243    ptl->setBitDepthConstraint((ptl->getProfileIdc() == Profile::MAIN10)?10:8);
2244    ptl->setChromaFormatConstraint(CHROMA_420);
2245    ptl->setIntraConstraintFlag(false);
2246    ptl->setLowerBitRateConstraintFlag(true);
2247    READ_CODE(16, uiCode, PTL_TRACE_TEXT("reserved_zero_43bits[0..15]"     ));
2248    READ_CODE(16, uiCode, PTL_TRACE_TEXT("reserved_zero_43bits[16..31]"    ));
2249    READ_CODE(11, uiCode, PTL_TRACE_TEXT("reserved_zero_43bits[32..42]"    ));
2250  }
2251#endif
2252
2253  if ((ptl->getProfileIdc() >= Profile::MAIN && ptl->getProfileIdc() <= Profile::HIGHTHROUGHPUTREXT) ||
2254       ptl->getProfileCompatibilityFlag(Profile::MAIN) ||
2255       ptl->getProfileCompatibilityFlag(Profile::MAIN10) ||
2256       ptl->getProfileCompatibilityFlag(Profile::MAINSTILLPICTURE) ||
2257       ptl->getProfileCompatibilityFlag(Profile::MAINREXT) ||
2258       ptl->getProfileCompatibilityFlag(Profile::HIGHTHROUGHPUTREXT) )
2259  {
2260#if SVC_EXTENSION
2261    READ_FLAG(    uiCode, PTL_TRACE_TEXT("inbld_flag"                      ));
2262#else
2263    READ_FLAG(    uiCode, PTL_TRACE_TEXT("inbld_flag"                      )); assert(uiCode == 0);
2264#endif
2265  }
2266  else
2267  {
2268#if SVC_EXTENSION
2269    READ_FLAG(    uiCode, PTL_TRACE_TEXT("inbld_flag"                      ));
2270#else
2271    READ_FLAG(    uiCode, PTL_TRACE_TEXT("reserved_zero_bit"               ));
2272#endif
2273  }
2274#undef PTL_TRACE_TEXT
2275}
2276
2277Void TDecCavlc::parseTerminatingBit( UInt& ruiBit )
2278{
2279  ruiBit = false;
2280  Int iBitsLeft = m_pcBitstream->getNumBitsLeft();
2281  if(iBitsLeft <= 8)
2282  {
2283    UInt uiPeekValue = m_pcBitstream->peekBits(iBitsLeft);
2284    if (uiPeekValue == (1<<(iBitsLeft-1)))
2285    {
2286      ruiBit = true;
2287    }
2288  }
2289}
2290
2291Void TDecCavlc::parseRemainingBytes( Bool noTrailingBytesExpected )
2292{
2293  if (noTrailingBytesExpected)
2294  {
2295    const UInt numberOfRemainingSubstreamBytes=m_pcBitstream->getNumBitsLeft();
2296    assert (numberOfRemainingSubstreamBytes == 0);
2297  }
2298  else
2299  {
2300    while (m_pcBitstream->getNumBitsLeft())
2301    {
2302      UInt trailingNullByte=m_pcBitstream->readByte();
2303      if (trailingNullByte!=0)
2304      {
2305        printf("Trailing byte should be 0, but has value %02x\n", trailingNullByte);
2306        assert(trailingNullByte==0);
2307      }
2308    }
2309  }
2310}
2311
2312Void TDecCavlc::parseSkipFlag( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
2313{
2314  assert(0);
2315}
2316
2317Void TDecCavlc::parseCUTransquantBypassFlag( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
2318{
2319  assert(0);
2320}
2321
2322Void TDecCavlc::parseMVPIdx( Int& /*riMVPIdx*/ )
2323{
2324  assert(0);
2325}
2326
2327Void TDecCavlc::parseSplitFlag     ( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
2328{
2329  assert(0);
2330}
2331
2332Void TDecCavlc::parsePartSize( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
2333{
2334  assert(0);
2335}
2336
2337Void TDecCavlc::parsePredMode( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
2338{
2339  assert(0);
2340}
2341
2342/** Parse I_PCM information.
2343* \param pcCU pointer to CU
2344* \param uiAbsPartIdx CU index
2345* \param uiDepth CU depth
2346* \returns Void
2347*
2348* If I_PCM flag indicates that the CU is I_PCM, parse its PCM alignment bits and codes.
2349*/
2350Void TDecCavlc::parseIPCMInfo( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
2351{
2352  assert(0);
2353}
2354
2355Void TDecCavlc::parseIntraDirLumaAng  ( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
2356{
2357  assert(0);
2358}
2359
2360Void TDecCavlc::parseIntraDirChroma( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
2361{
2362  assert(0);
2363}
2364
2365Void TDecCavlc::parseInterDir( TComDataCU* /*pcCU*/, UInt& /*ruiInterDir*/, UInt /*uiAbsPartIdx*/ )
2366{
2367  assert(0);
2368}
2369
2370Void TDecCavlc::parseRefFrmIdx( TComDataCU* /*pcCU*/, Int& /*riRefFrmIdx*/, RefPicList /*eRefList*/ )
2371{
2372  assert(0);
2373}
2374
2375Void TDecCavlc::parseMvd( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiPartIdx*/, UInt /*uiDepth*/, RefPicList /*eRefList*/ )
2376{
2377  assert(0);
2378}
2379
2380Void TDecCavlc::parseCrossComponentPrediction( class TComTU& /*rTu*/, ComponentID /*compID*/ )
2381{
2382  assert(0);
2383}
2384
2385Void TDecCavlc::parseDeltaQP( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
2386{
2387  Int  iDQp;
2388
2389#if RExt__DECODER_DEBUG_BIT_STATISTICS
2390  READ_SVLC(iDQp, "delta_qp");
2391#else
2392  xReadSvlc( iDQp );
2393#endif
2394
2395#if SVC_EXTENSION
2396  Int qpBdOffsetY = pcCU->getSlice()->getQpBDOffset(CHANNEL_TYPE_LUMA);
2397#else
2398  Int qpBdOffsetY = pcCU->getSlice()->getSPS()->getQpBDOffset(CHANNEL_TYPE_LUMA);
2399#endif
2400  const Int qp = (((Int) pcCU->getRefQP( uiAbsPartIdx ) + iDQp + 52 + 2*qpBdOffsetY )%(52+ qpBdOffsetY)) -  qpBdOffsetY;
2401
2402  const UInt maxCUDepth        = pcCU->getSlice()->getSPS()->getMaxTotalCUDepth();
2403  const UInt maxCuDQPDepth     = pcCU->getSlice()->getPPS()->getMaxCuDQPDepth();
2404  const UInt doubleDepthDifference = ((maxCUDepth - maxCuDQPDepth)<<1);
2405  const UInt uiAbsQpCUPartIdx = (uiAbsPartIdx>>doubleDepthDifference)<<doubleDepthDifference ;
2406  const UInt uiQpCUDepth =   min(uiDepth,pcCU->getSlice()->getPPS()->getMaxCuDQPDepth()) ;
2407
2408  pcCU->setQPSubParts( qp, uiAbsQpCUPartIdx, uiQpCUDepth );
2409}
2410
2411Void TDecCavlc::parseChromaQpAdjustment( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
2412{
2413  assert(0);
2414}
2415
2416Void TDecCavlc::parseCoeffNxN( TComTU &/*rTu*/, ComponentID /*compID*/ )
2417{
2418  assert(0);
2419}
2420
2421Void TDecCavlc::parseTransformSubdivFlag( UInt& /*ruiSubdivFlag*/, UInt /*uiLog2TransformBlockSize*/ )
2422{
2423  assert(0);
2424}
2425
2426Void TDecCavlc::parseQtCbf( TComTU &/*rTu*/, const ComponentID /*compID*/, const Bool /*lowestLevel*/ )
2427{
2428  assert(0);
2429}
2430
2431Void TDecCavlc::parseQtRootCbf( UInt /*uiAbsPartIdx*/, UInt& /*uiQtRootCbf*/ )
2432{
2433  assert(0);
2434}
2435
2436Void TDecCavlc::parseTransformSkipFlags (TComTU &/*rTu*/, ComponentID /*component*/)
2437{
2438  assert(0);
2439}
2440
2441Void TDecCavlc::parseMergeFlag ( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/, UInt /*uiPUIdx*/ )
2442{
2443  assert(0);
2444}
2445
2446Void TDecCavlc::parseMergeIndex ( TComDataCU* /*pcCU*/, UInt& /*ruiMergeIndex*/ )
2447{
2448  assert(0);
2449}
2450
2451// ====================================================================================================================
2452// Protected member functions
2453// ====================================================================================================================
2454
2455//! parse explicit wp tables
2456Void TDecCavlc::xParsePredWeightTable( TComSlice* pcSlice, const TComSPS *sps )
2457{
2458        WPScalingParam *wp;
2459  const ChromaFormat    chFmt        = sps->getChromaFormatIdc();
2460  const Int             numValidComp = Int(getNumberValidComponents(chFmt));
2461  const Bool            bChroma      = (chFmt!=CHROMA_400);
2462  const SliceType       eSliceType   = pcSlice->getSliceType();
2463  const Int             iNbRef       = (eSliceType == B_SLICE ) ? (2) : (1);
2464        UInt            uiLog2WeightDenomLuma=0, uiLog2WeightDenomChroma=0;
2465        UInt            uiTotalSignalledWeightFlags = 0;
2466
2467  Int iDeltaDenom;
2468  // decode delta_luma_log2_weight_denom :
2469  READ_UVLC( uiLog2WeightDenomLuma, "luma_log2_weight_denom" );
2470  assert( uiLog2WeightDenomLuma <= 7 );
2471  if( bChroma )
2472  {
2473    READ_SVLC( iDeltaDenom, "delta_chroma_log2_weight_denom" );
2474    assert((iDeltaDenom + (Int)uiLog2WeightDenomLuma)>=0);
2475    assert((iDeltaDenom + (Int)uiLog2WeightDenomLuma)<=7);
2476    uiLog2WeightDenomChroma = (UInt)(iDeltaDenom + uiLog2WeightDenomLuma);
2477  }
2478
2479  for ( Int iNumRef=0 ; iNumRef<iNbRef ; iNumRef++ ) // loop over l0 and l1 syntax elements
2480  {
2481    RefPicList  eRefPicList = ( iNumRef ? REF_PIC_LIST_1 : REF_PIC_LIST_0 );
2482    for ( Int iRefIdx=0 ; iRefIdx<pcSlice->getNumRefIdx(eRefPicList) ; iRefIdx++ )
2483    {
2484      pcSlice->getWpScaling(eRefPicList, iRefIdx, wp);
2485
2486      wp[COMPONENT_Y].uiLog2WeightDenom = uiLog2WeightDenomLuma;
2487      for(Int j=1; j<numValidComp; j++)
2488      {
2489        wp[j].uiLog2WeightDenom = uiLog2WeightDenomChroma;
2490      }
2491
2492      UInt  uiCode;
2493      READ_FLAG( uiCode, iNumRef==0?"luma_weight_l0_flag[i]":"luma_weight_l1_flag[i]" );
2494      wp[COMPONENT_Y].bPresentFlag = ( uiCode == 1 );
2495      uiTotalSignalledWeightFlags += wp[COMPONENT_Y].bPresentFlag;
2496    }
2497    if ( bChroma )
2498    {
2499      UInt  uiCode;
2500      for ( Int iRefIdx=0 ; iRefIdx<pcSlice->getNumRefIdx(eRefPicList) ; iRefIdx++ )
2501      {
2502        pcSlice->getWpScaling(eRefPicList, iRefIdx, wp);
2503        READ_FLAG( uiCode, iNumRef==0?"chroma_weight_l0_flag[i]":"chroma_weight_l1_flag[i]" );
2504        for(Int j=1; j<numValidComp; j++)
2505        {
2506          wp[j].bPresentFlag = ( uiCode == 1 );
2507        }
2508        uiTotalSignalledWeightFlags += 2*wp[COMPONENT_Cb].bPresentFlag;
2509      }
2510    }
2511    for ( Int iRefIdx=0 ; iRefIdx<pcSlice->getNumRefIdx(eRefPicList) ; iRefIdx++ )
2512    {
2513      pcSlice->getWpScaling(eRefPicList, iRefIdx, wp);
2514      if ( wp[COMPONENT_Y].bPresentFlag )
2515      {
2516        Int iDeltaWeight;
2517        READ_SVLC( iDeltaWeight, iNumRef==0?"delta_luma_weight_l0[i]":"delta_luma_weight_l1[i]" );
2518        assert( iDeltaWeight >= -128 );
2519        assert( iDeltaWeight <=  127 );
2520        wp[COMPONENT_Y].iWeight = (iDeltaWeight + (1<<wp[COMPONENT_Y].uiLog2WeightDenom));
2521        READ_SVLC( wp[COMPONENT_Y].iOffset, iNumRef==0?"luma_offset_l0[i]":"luma_offset_l1[i]" );
2522#if SVC_EXTENSION
2523        Int range=sps->getSpsRangeExtension().getHighPrecisionOffsetsEnabledFlag() ? (1<<pcSlice->getBitDepth(CHANNEL_TYPE_LUMA))/2 : 128;       
2524#else
2525        Int range=sps->getSpsRangeExtension().getHighPrecisionOffsetsEnabledFlag() ? (1<<sps->getBitDepth(CHANNEL_TYPE_LUMA))/2 : 128;
2526#endif
2527        assert( wp[0].iOffset >= -range );
2528        assert( wp[0].iOffset <   range );
2529      }
2530      else
2531      {
2532        wp[COMPONENT_Y].iWeight = (1 << wp[COMPONENT_Y].uiLog2WeightDenom);
2533        wp[COMPONENT_Y].iOffset = 0;
2534      }
2535      if ( bChroma )
2536      {
2537        if ( wp[COMPONENT_Cb].bPresentFlag )
2538        {
2539#if SVC_EXTENSION
2540          Int range=sps->getSpsRangeExtension().getHighPrecisionOffsetsEnabledFlag() ? (1<<pcSlice->getBitDepth(CHANNEL_TYPE_CHROMA))/2 : 128;
2541#else
2542          Int range=sps->getSpsRangeExtension().getHighPrecisionOffsetsEnabledFlag() ? (1<<sps->getBitDepth(CHANNEL_TYPE_CHROMA))/2 : 128;
2543#endif
2544          for ( Int j=1 ; j<numValidComp ; j++ )
2545          {
2546            Int iDeltaWeight;
2547            READ_SVLC( iDeltaWeight, iNumRef==0?"delta_chroma_weight_l0[i]":"delta_chroma_weight_l1[i]" );
2548            assert( iDeltaWeight >= -128 );
2549            assert( iDeltaWeight <=  127 );
2550            wp[j].iWeight = (iDeltaWeight + (1<<wp[j].uiLog2WeightDenom));
2551
2552            Int iDeltaChroma;
2553            READ_SVLC( iDeltaChroma, iNumRef==0?"delta_chroma_offset_l0[i]":"delta_chroma_offset_l1[i]" );
2554            assert( iDeltaChroma >= -4*range);
2555            assert( iDeltaChroma <   4*range);
2556            Int pred = ( range - ( ( range*wp[j].iWeight)>>(wp[j].uiLog2WeightDenom) ) );
2557            wp[j].iOffset = Clip3(-range, range-1, (iDeltaChroma + pred) );
2558          }
2559        }
2560        else
2561        {
2562          for ( Int j=1 ; j<numValidComp ; j++ )
2563          {
2564            wp[j].iWeight = (1 << wp[j].uiLog2WeightDenom);
2565            wp[j].iOffset = 0;
2566          }
2567        }
2568      }
2569    }
2570
2571    for ( Int iRefIdx=pcSlice->getNumRefIdx(eRefPicList) ; iRefIdx<MAX_NUM_REF ; iRefIdx++ )
2572    {
2573      pcSlice->getWpScaling(eRefPicList, iRefIdx, wp);
2574
2575      wp[0].bPresentFlag = false;
2576      wp[1].bPresentFlag = false;
2577      wp[2].bPresentFlag = false;
2578    }
2579  }
2580  assert(uiTotalSignalledWeightFlags<=24);
2581}
2582
2583/** decode quantization matrix
2584* \param scalingList quantization matrix information
2585*/
2586Void TDecCavlc::parseScalingList(TComScalingList* scalingList)
2587{
2588  UInt  code, sizeId, listId;
2589  Bool scalingListPredModeFlag;
2590  //for each size
2591  for(sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
2592  {
2593    for(listId = 0; listId <  SCALING_LIST_NUM; listId++)
2594    {
2595      if ((sizeId==SCALING_LIST_32x32) && (listId%(SCALING_LIST_NUM/NUMBER_OF_PREDICTION_MODES) != 0))
2596      {
2597        Int *src = scalingList->getScalingListAddress(sizeId, listId);
2598        const Int size = min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId]);
2599        const Int *srcNextSmallerSize = scalingList->getScalingListAddress(sizeId-1, listId);
2600        for(Int i=0; i<size; i++)
2601        {
2602          src[i] = srcNextSmallerSize[i];
2603        }
2604        scalingList->setScalingListDC(sizeId,listId,(sizeId > SCALING_LIST_8x8) ? scalingList->getScalingListDC(sizeId-1, listId) : src[0]);
2605      }
2606      else
2607      {
2608        READ_FLAG( code, "scaling_list_pred_mode_flag");
2609        scalingListPredModeFlag = (code) ? true : false;
2610        scalingList->setScalingListPredModeFlag(sizeId, listId, scalingListPredModeFlag);
2611        if(!scalingListPredModeFlag) //Copy Mode
2612        {
2613          READ_UVLC( code, "scaling_list_pred_matrix_id_delta");
2614
2615          if (sizeId==SCALING_LIST_32x32)
2616          {
2617            code*=(SCALING_LIST_NUM/NUMBER_OF_PREDICTION_MODES); // Adjust the decoded code for this size, to cope with the missing 32x32 chroma entries.
2618          }
2619
2620          scalingList->setRefMatrixId (sizeId,listId,(UInt)((Int)(listId)-(code)));
2621          if( sizeId > SCALING_LIST_8x8 )
2622          {
2623            scalingList->setScalingListDC(sizeId,listId,((listId == scalingList->getRefMatrixId (sizeId,listId))? 16 :scalingList->getScalingListDC(sizeId, scalingList->getRefMatrixId (sizeId,listId))));
2624          }
2625          scalingList->processRefMatrix( sizeId, listId, scalingList->getRefMatrixId (sizeId,listId));
2626
2627        }
2628        else //DPCM Mode
2629        {
2630          xDecodeScalingList(scalingList, sizeId, listId);
2631        }
2632      }
2633    }
2634  }
2635
2636  return;
2637}
2638/** decode DPCM
2639* \param scalingList  quantization matrix information
2640* \param sizeId size index
2641* \param listId list index
2642*/
2643Void TDecCavlc::xDecodeScalingList(TComScalingList *scalingList, UInt sizeId, UInt listId)
2644{
2645  Int i,coefNum = min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId]);
2646  Int data;
2647  Int scalingListDcCoefMinus8 = 0;
2648  Int nextCoef = SCALING_LIST_START_VALUE;
2649  UInt* scan  = g_scanOrder[SCAN_UNGROUPED][SCAN_DIAG][sizeId==0 ? 2 : 3][sizeId==0 ? 2 : 3];
2650  Int *dst = scalingList->getScalingListAddress(sizeId, listId);
2651
2652  if( sizeId > SCALING_LIST_8x8 )
2653  {
2654    READ_SVLC( scalingListDcCoefMinus8, "scaling_list_dc_coef_minus8");
2655    scalingList->setScalingListDC(sizeId,listId,scalingListDcCoefMinus8 + 8);
2656    nextCoef = scalingList->getScalingListDC(sizeId,listId);
2657  }
2658
2659  for(i = 0; i < coefNum; i++)
2660  {
2661    READ_SVLC( data, "scaling_list_delta_coef");
2662    nextCoef = (nextCoef + data + 256 ) % 256;
2663    dst[scan[i]] = nextCoef;
2664  }
2665}
2666
2667Bool TDecCavlc::xMoreRbspData()
2668{
2669  Int bitsLeft = m_pcBitstream->getNumBitsLeft();
2670
2671  // if there are more than 8 bits, it cannot be rbsp_trailing_bits
2672  if (bitsLeft > 8)
2673  {
2674    return true;
2675  }
2676
2677  UChar lastByte = m_pcBitstream->peekBits(bitsLeft);
2678  Int cnt = bitsLeft;
2679
2680  // remove trailing bits equal to zero
2681  while ((cnt>0) && ((lastByte & 1) == 0))
2682  {
2683    lastByte >>= 1;
2684    cnt--;
2685  }
2686  // remove bit equal to one
2687  cnt--;
2688
2689  // we should not have a negative number of bits
2690  assert (cnt>=0);
2691
2692  // we have more data, if cnt is not zero
2693  return (cnt>0);
2694}
2695
2696Void TDecCavlc::parseExplicitRdpcmMode( TComTU& /*rTu*/, ComponentID /*compID*/ )
2697{
2698  assert(0);
2699}
2700
2701#if SVC_EXTENSION
2702Void TDecCavlc::parseVPSExtension(TComVPS *vps)
2703{
2704  UInt uiCode;
2705  Int NumOutputLayersInOutputLayerSet[MAX_VPS_LAYER_SETS_PLUS1];
2706  Int OlsHighestOutputLayerId[MAX_VPS_LAYER_SETS_PLUS1];
2707
2708  if( vps->getMaxLayers() > 1 && vps->getBaseLayerInternalFlag() )
2709  {
2710    vps->setProfilePresentFlag(1, false);
2711    parsePTL( vps->getPTL(1), vps->getProfilePresentFlag(1), vps->getMaxTLayers() - 1 );
2712  }
2713
2714  UInt numScalabilityTypes = 0, i = 0, j = 0;
2715
2716  READ_FLAG( uiCode, "splitting_flag" ); vps->setSplittingFlag(uiCode ? true : false);
2717
2718  for(i = 0; i < MAX_VPS_NUM_SCALABILITY_TYPES; i++)
2719  {
2720    READ_FLAG( uiCode, "scalability_mask[i]" ); vps->setScalabilityMask(i, uiCode ? true : false);
2721    numScalabilityTypes += uiCode;
2722  }
2723  vps->setNumScalabilityTypes(numScalabilityTypes);
2724
2725  for(j = 0; j < numScalabilityTypes - vps->getSplittingFlag(); j++)
2726  {
2727    READ_CODE( 3, uiCode, "dimension_id_len_minus1[j]" ); vps->setDimensionIdLen(j, uiCode + 1);
2728  }
2729
2730  // The value of dimBitOffset[ NumScalabilityTypes ] is set equal to 6.
2731  if(vps->getSplittingFlag())
2732  {
2733    UInt numBits = 0;
2734    for(j = 0; j < numScalabilityTypes - 1; j++)
2735    {
2736      numBits += vps->getDimensionIdLen(j);
2737    }
2738    assert( numBits < 6 );
2739    vps->setDimensionIdLen(numScalabilityTypes-1, 6 - numBits);
2740    numBits = 6;
2741  }
2742
2743  READ_FLAG( uiCode, "vps_nuh_layer_id_present_flag" ); vps->setNuhLayerIdPresentFlag(uiCode ? true : false);
2744  vps->setLayerIdInNuh(0, 0);
2745  vps->setLayerIdxInVps(0, 0);
2746  for(i = 1; i < vps->getMaxLayers(); i++)
2747  {
2748    if( vps->getNuhLayerIdPresentFlag() )
2749    {
2750      READ_CODE( 6, uiCode, "layer_id_in_nuh[i]" ); vps->setLayerIdInNuh(i, uiCode);
2751      assert( uiCode > vps->getLayerIdInNuh(i-1) );
2752    }
2753    else
2754    {
2755      vps->setLayerIdInNuh(i, i);
2756    }
2757    vps->setLayerIdxInVps(vps->getLayerIdInNuh(i), i);
2758
2759    if( !vps->getSplittingFlag() )
2760    {
2761      for(j = 0; j < numScalabilityTypes; j++)
2762      {
2763        READ_CODE( vps->getDimensionIdLen(j), uiCode, "dimension_id[i][j]" ); vps->setDimensionId(i, j, uiCode);
2764#if !AUXILIARY_PICTURES
2765        assert( uiCode <= vps->getMaxLayerId() );
2766#endif
2767      }
2768    }
2769  }
2770
2771  READ_CODE( 4, uiCode, "view_id_len" ); vps->setViewIdLen( uiCode );
2772
2773  if ( vps->getViewIdLen() > 0 )
2774  {
2775    for( i = 0; i < vps->getNumViews(); i++ )
2776    {
2777      READ_CODE( vps->getViewIdLen( ), uiCode, "view_id_val[i]" ); vps->setViewIdVal( i, uiCode );
2778    }
2779  }
2780
2781  // For layer 0
2782  vps->setNumDirectRefLayers(0, 0);
2783  // For other layers
2784  for( Int layerCtr = 1; layerCtr < vps->getMaxLayers(); layerCtr++)
2785  {
2786    UInt layerId = vps->getLayerIdInNuh(layerCtr); 
2787    UInt numDirectRefLayers = 0;
2788    for( Int refLayerCtr = 0; refLayerCtr < layerCtr; refLayerCtr++)
2789    {
2790      READ_FLAG(uiCode, "direct_dependency_flag[i][j]" ); vps->setDirectDependencyFlag(layerCtr, refLayerCtr, uiCode? true : false);
2791      if(uiCode)
2792      {
2793        vps->setRefLayerId(layerId, numDirectRefLayers, vps->getLayerIdInNuh(refLayerCtr));
2794        numDirectRefLayers++;
2795      }
2796    }
2797    vps->setNumDirectRefLayers(layerId, numDirectRefLayers);
2798  }
2799
2800  // dependency constraint
2801  vps->setNumRefLayers();
2802
2803  if (vps->getMaxLayers() > MAX_REF_LAYERS)
2804  {
2805    for (i = 1; i < vps->getMaxLayers(); i++)
2806    {
2807      assert(vps->getNumRefLayers(vps->getLayerIdInNuh(i)) <= MAX_REF_LAYERS);
2808    }
2809  }
2810
2811  vps->setPredictedLayerIds();
2812  vps->setTreePartitionLayerIdList();
2813
2814  if( vps->getNumIndependentLayers() > 1 )
2815  {
2816    READ_UVLC(uiCode, "num_add_layer_sets"); vps->setNumAddLayerSets(uiCode);
2817
2818    for( i = 0; i < vps->getNumAddLayerSets(); i++ )
2819    {
2820      for( j = 1; j < vps->getNumIndependentLayers(); j++ )
2821      {
2822        Int len = 1;
2823        while( (1 << len) < (vps->getNumLayersInTreePartition(j) + 1) )
2824        {
2825          len++;
2826        }
2827
2828        READ_CODE(len, uiCode, "highest_layer_idx_plus1[i][j]"); vps->setHighestLayerIdxPlus1(i, j, uiCode);
2829      }
2830    }
2831    vps->setNumLayerSets(vps->getNumLayerSets() + vps->getNumAddLayerSets());
2832    vps->deriveLayerIdListVariablesForAddLayerSets();
2833  }
2834  else
2835  {
2836    vps->setNumAddLayerSets(0);
2837  }
2838
2839  READ_FLAG( uiCode, "vps_sub_layers_max_minus1_present_flag"); vps->setMaxTSLayersPresentFlag(uiCode ? true : false);
2840
2841  if (vps->getMaxTSLayersPresentFlag())
2842  {
2843    for(i = 0; i < vps->getMaxLayers(); i++)
2844    {
2845      READ_CODE( 3, uiCode, "sub_layers_vps_max_minus1[i]" ); vps->setMaxTSLayersMinus1(i, uiCode);
2846    }
2847  }
2848  else
2849  {
2850    for( i = 0; i < vps->getMaxLayers(); i++)
2851    {
2852      vps->setMaxTSLayersMinus1(i, vps->getMaxTLayers()-1);
2853    }
2854  }
2855
2856  READ_FLAG( uiCode, "max_tid_ref_present_flag"); vps->setMaxTidRefPresentFlag(uiCode ? true : false);
2857  if( vps->getMaxTidRefPresentFlag() )
2858  {
2859    for( i = 0; i < vps->getMaxLayers() - 1; i++ )
2860    {
2861      for( j = i+1; j < vps->getMaxLayers(); j++ )
2862      {
2863        if( vps->getDirectDependencyFlag(j, i) )
2864        {
2865          READ_CODE( 3, uiCode, "max_tid_il_ref_pics_plus1[i][j]" ); vps->setMaxTidIlRefPicsPlus1(i, j, uiCode);         
2866        }
2867      }
2868    }
2869  }
2870  else
2871  {
2872    for(i = 0; i < vps->getMaxLayers() - 1; i++)
2873    {
2874      for( j = i+1; j < vps->getMaxLayers(); j++)
2875      {
2876        vps->setMaxTidIlRefPicsPlus1(i, j, 7);
2877      }
2878    }
2879  }
2880  READ_FLAG( uiCode, "all_ref_layers_active_flag" ); vps->setIlpSshSignalingEnabledFlag(uiCode ? true : false);
2881
2882  // Profile-tier-level signalling
2883  READ_UVLC(  uiCode, "vps_num_profile_tier_level_minus1"); vps->setNumProfileTierLevel( uiCode + 1 );
2884
2885  Int const numBitsForPtlIdx = vps->calculateLenOfSyntaxElement( vps->getNumProfileTierLevel() );
2886
2887  for( Int idx = vps->getBaseLayerInternalFlag() ? 2 : 1; idx < vps->getNumProfileTierLevel(); idx++ )
2888  {
2889    READ_FLAG( uiCode, "vps_profile_present_flag[i]" ); vps->setProfilePresentFlag(idx, uiCode ? true : false);
2890
2891    if( !vps->getProfilePresentFlag(idx) )
2892    {
2893      // Copy profile information from previous one
2894      vps->getPTL(idx)->copyProfileInfo( vps->getPTL( idx - 1 ) );
2895    }
2896
2897    parsePTL( vps->getPTL(idx), vps->getProfilePresentFlag(idx), vps->getMaxTLayers() - 1 );
2898  }
2899
2900  if( vps->getNumLayerSets() > 1 )
2901  {
2902    READ_UVLC( uiCode, "num_add_olss" );                  vps->setNumAddOutputLayerSets( uiCode );
2903    READ_CODE( 2, uiCode, "default_output_layer_idc" );   vps->setDefaultTargetOutputLayerIdc( uiCode );
2904  }
2905  else
2906  {
2907    vps->setNumAddOutputLayerSets( 0 );
2908  }
2909
2910  // The value of num_add_olss shall be in the range of 0 to 1023, inclusive.
2911  assert( vps->getNumAddOutputLayerSets() >= 0 && vps->getNumAddOutputLayerSets() < 1024 );
2912
2913  Int numOutputLayerSets = vps->getNumLayerSets() + vps->getNumAddOutputLayerSets();
2914
2915  vps->setNumOutputLayerSets( numOutputLayerSets );
2916
2917  // Default output layer set
2918  vps->setOutputLayerSetIdx(0, 0);
2919  vps->setOutputLayerFlag(0, 0, true);
2920  vps->deriveNecessaryLayerFlag(0);
2921  vps->getProfileLevelTierIdx()->resize(numOutputLayerSets);
2922  vps->getProfileLevelTierIdx(0)->push_back( vps->getBaseLayerInternalFlag() && vps->getMaxLayers() > 1 ? 1 : 0);
2923
2924  std::vector<Profile::Name> profiles;
2925
2926  if( vps->getScalabilityMask( VIEW_ORDER_INDEX ) )
2927  {
2928    profiles.push_back( Profile::MULTIVIEWMAIN );
2929  }
2930
2931  if( vps->getScalabilityMask( SCALABILITY_ID ) )
2932  {
2933    profiles.push_back( Profile::SCALABLEMAIN );
2934  }
2935
2936  for(i = 1; i < numOutputLayerSets; i++)
2937  {
2938    if( vps->getNumLayerSets() > 2 && i >= vps->getNumLayerSets() )
2939    {
2940      Int numBits = 1;
2941      while ((1 << numBits) < (vps->getNumLayerSets() - 1))
2942      {
2943        numBits++;
2944      }
2945      READ_CODE( numBits, uiCode, "layer_set_idx_for_ols_minus1");
2946    }
2947    else
2948    {
2949      uiCode = 0;
2950    }
2951
2952    vps->setOutputLayerSetIdx( i, i < vps->getNumLayerSets() ? i : (uiCode + 1) );
2953
2954    Int layerSetIdxForOutputLayerSet = vps->getOutputLayerSetIdx(i);
2955
2956    if( i > vps->getVpsNumLayerSetsMinus1() || vps->getDefaultTargetOutputLayerIdc() == 2 )
2957    {
2958      for( j = 0; j < vps->getNumLayersInIdList(layerSetIdxForOutputLayerSet); j++ )
2959      {
2960        READ_FLAG( uiCode, "output_layer_flag[i][j]"); vps->setOutputLayerFlag(i, j, uiCode);
2961      }
2962    }
2963    else
2964    {
2965      // i <= (vps->getNumLayerSets() - 1)
2966      // Assign OutputLayerFlag depending on default_one_target_output_layer_flag
2967      if( vps->getDefaultTargetOutputLayerIdc() == 1 )
2968      {
2969        for( j = 0; j < vps->getNumLayersInIdList(layerSetIdxForOutputLayerSet); j++ )
2970        {
2971          vps->setOutputLayerFlag(i, j, (j == (vps->getNumLayersInIdList(layerSetIdxForOutputLayerSet)-1))  );
2972        }
2973      }
2974      else if( vps->getDefaultTargetOutputLayerIdc() == 0 )
2975      {
2976        for( j = 0; j < vps->getNumLayersInIdList(layerSetIdxForOutputLayerSet); j++ )
2977        {
2978          vps->setOutputLayerFlag(i, j, 1);
2979        }
2980      }
2981    }
2982
2983    vps->deriveNecessaryLayerFlag(i); 
2984
2985    vps->getProfileLevelTierIdx(i)->assign(vps->getNumLayersInIdList(layerSetIdxForOutputLayerSet), -1);
2986
2987    std::vector<UInt> passedCheck(profiles.size(), 0);
2988    UInt numIncludedLayers = 0;
2989
2990    for( j = 0; j < vps->getNumLayersInIdList(layerSetIdxForOutputLayerSet); j++ )
2991    {
2992      if( vps->getNecessaryLayerFlag(i, j) && (vps->getNumProfileTierLevel()-1) > 0 )
2993      {
2994        READ_CODE( numBitsForPtlIdx, uiCode, "profile_tier_level_idx[i]" ); 
2995        vps->setProfileLevelTierIdx(i, j, uiCode );
2996
2997        //For conformance checking
2998        //Conformance of a layer in an output operation point associated with an OLS in a bitstream to the Scalable Main profile is indicated as follows:
2999        //If OpTid of the output operation point is equal to vps_max_sub_layer_minus1, the conformance is indicated by general_profile_idc being equal to 7 or general_profile_compatibility_flag[ 7 ] being equal to 1
3000        //Conformance of a layer in an output operation point associated with an OLS in a bitstream to the Scalable Main 10 profile is indicated as follows:
3001        //If OpTid of the output operation point is equal to vps_max_sub_layer_minus1, the conformance is indicated by general_profile_idc being equal to 7 or general_profile_compatibility_flag[ 7 ] being equal to 1
3002        //The following assert may be updated / upgraded to take care of general_profile_compatibility_flag.
3003
3004        if( layerSetIdxForOutputLayerSet <= vps->getVpsNumLayerSetsMinus1() )
3005        {
3006          if( vps->getLayerSetLayerIdList(layerSetIdxForOutputLayerSet, j) != 0 && vps->getNecessaryLayerFlag(i, j) )
3007          {
3008            ProfileTierLevel* ptl = vps->getPTL(vps->getProfileLevelTierIdx(i, j))->getGeneralPTL();
3009            numIncludedLayers++;
3010#if SCALABLE_REXT
3011            // if scalable profile is Scalable Main 10 or Scalable Rext, dimension_id = 2
3012            const Profile::Name profileName = ( ptl->getProfileIdc() == Profile::SCALABLEMAIN10 || ptl->getProfileIdc() == Profile::SCALABLEREXT ) ? Profile::SCALABLEMAIN : ptl->getProfileIdc();
3013#else
3014            const Profile::Name profileName = ptl->getProfileIdc() == Profile::SCALABLEMAIN10 ? Profile::SCALABLEMAIN : ptl->getProfileIdc();
3015#endif
3016
3017            for( Int p = 0; p < profiles.size(); p++ )
3018            {
3019              passedCheck[p] += profileName == profiles[p] || ptl->getProfileCompatibilityFlag(Int(profiles[p]));
3020            }
3021          }
3022        }
3023      }
3024    }
3025
3026    // check whether all layers are compatible to at least one extension profile
3027    assert( std::find(passedCheck.begin(), passedCheck.end(), numIncludedLayers) != passedCheck.end() );
3028
3029    NumOutputLayersInOutputLayerSet[i] = 0;
3030
3031    for( j = 0; j < vps->getNumLayersInIdList(layerSetIdxForOutputLayerSet); j++ )
3032    {
3033      NumOutputLayersInOutputLayerSet[i] += vps->getOutputLayerFlag(i, j);
3034      if( vps->getOutputLayerFlag(i, j) )
3035      {
3036        OlsHighestOutputLayerId[i] = vps->getLayerSetLayerIdList(layerSetIdxForOutputLayerSet, j);
3037      }
3038    }
3039
3040    if( NumOutputLayersInOutputLayerSet[i] == 1 && vps->getNumDirectRefLayers(OlsHighestOutputLayerId[i]) > 0 )
3041    {
3042      READ_FLAG(uiCode, "alt_output_layer_flag[i]");
3043      vps->setAltOuputLayerFlag(i, uiCode ? true : false);
3044    }
3045    else
3046    {
3047      vps->setAltOuputLayerFlag(i, false);
3048    }
3049
3050    assert( NumOutputLayersInOutputLayerSet[i] > 0 );
3051  }
3052
3053  vps->checkNecessaryLayerFlagCondition(); 
3054
3055  READ_UVLC( uiCode, "vps_num_rep_formats_minus1" );
3056  vps->setVpsNumRepFormats( uiCode + 1 );
3057
3058  // The value of vps_num_rep_formats_minus1 shall be in the range of 0 to 255, inclusive.
3059  assert( vps->getVpsNumRepFormats() > 0 && vps->getVpsNumRepFormats() <= 256 );
3060
3061  for(i = 0; i < vps->getVpsNumRepFormats(); i++)
3062  {
3063    // Read rep_format_structures
3064    parseRepFormat( vps->getVpsRepFormat(i), i > 0 ? vps->getVpsRepFormat(i-1) : 0 );
3065  }
3066
3067  // Default assignment for layer 0
3068  vps->setVpsRepFormatIdx( 0, 0 );
3069
3070  if( vps->getVpsNumRepFormats() > 1 )
3071  {
3072    READ_FLAG( uiCode, "rep_format_idx_present_flag");
3073    vps->setRepFormatIdxPresentFlag( uiCode ? true : false );
3074  }
3075  else
3076  {
3077    // When not present, the value of rep_format_idx_present_flag is inferred to be equal to 0
3078    vps->setRepFormatIdxPresentFlag( false );
3079  }
3080
3081  if( vps->getRepFormatIdxPresentFlag() )
3082  {
3083    for( i = vps->getBaseLayerInternalFlag() ? 1 : 0; i < vps->getMaxLayers(); i++ )
3084    {
3085      Int numBits = 1;
3086      while ((1 << numBits) < (vps->getVpsNumRepFormats()))
3087      {
3088        numBits++;
3089      }
3090      READ_CODE( numBits, uiCode, "vps_rep_format_idx[i]" );
3091      vps->setVpsRepFormatIdx( i, uiCode );
3092    }
3093  }
3094  else
3095  {
3096    // When not present, the value of vps_rep_format_idx[ i ] is inferred to be equal to Min (i, vps_num_rep_formats_minus1)
3097    for(i = 1; i < vps->getMaxLayers(); i++)
3098    {
3099      vps->setVpsRepFormatIdx( i, min( (Int)i, vps->getVpsNumRepFormats()-1 ) );
3100    }
3101  }
3102
3103  READ_FLAG(uiCode, "max_one_active_ref_layer_flag" );
3104  vps->setMaxOneActiveRefLayerFlag(uiCode);
3105
3106  READ_FLAG(uiCode, "vps_poc_lsb_aligned_flag");
3107  vps->setVpsPocLsbAlignedFlag(uiCode);
3108
3109  for( i = 1; i< vps->getMaxLayers(); i++ )
3110  {
3111    if( vps->getNumDirectRefLayers( vps->getLayerIdInNuh(i) ) == 0  )
3112    {
3113      READ_FLAG(uiCode, "poc_lsb_not_present_flag[i]");
3114      vps->setPocLsbNotPresentFlag(i, uiCode);
3115    }
3116  }
3117
3118  parseVpsDpbSizeTable(vps);
3119
3120  READ_UVLC( uiCode,           "direct_dep_type_len_minus2"); vps->setDirectDepTypeLen(uiCode+2);
3121
3122  READ_FLAG(uiCode, "direct_dependency_all_layers_flag"); 
3123  vps->setDefaultDirectDependecyTypeFlag(uiCode == 1? true : false);
3124
3125  if( vps->getDefaultDirectDependencyTypeFlag() )
3126  {
3127    READ_CODE( vps->getDirectDepTypeLen(), uiCode, "direct_dependency_all_layers_type" ); 
3128    vps->setDefaultDirectDependecyType(uiCode);
3129  }
3130
3131  for( i = vps->getBaseLayerInternalFlag() ? 1 : 2; i < vps->getMaxLayers(); i++ )
3132  {
3133    for( j = vps->getBaseLayerInternalFlag() ? 0 : 1; j < i; j++ )
3134    {
3135      if( vps->getDirectDependencyFlag(i, j) )
3136      {
3137        if (vps->getDefaultDirectDependencyTypeFlag())
3138        {
3139          vps->setDirectDependencyType(i, j, vps->getDefaultDirectDependencyType());
3140        }
3141        else
3142        {
3143          READ_CODE( vps->getDirectDepTypeLen(), uiCode, "direct_dependency_type[i][j]" ); 
3144          vps->setDirectDependencyType(i, j, uiCode);
3145        }
3146      }
3147    }
3148  }
3149
3150  READ_UVLC( uiCode,           "vps_non_vui_extension_length"); vps->setVpsNonVuiExtLength((Int)uiCode);
3151
3152  // The value of vps_non_vui_extension_length shall be in the range of 0 to 4096, inclusive.
3153  assert( vps->getVpsNonVuiExtLength() >= 0 && vps->getVpsNonVuiExtLength() <= 4096 );
3154
3155  Int nonVuiExtByte = uiCode;
3156  for (i = 1; i <= nonVuiExtByte; i++)
3157  {
3158    READ_CODE( 8, uiCode, "vps_non_vui_extension_data_byte" ); //just parse and discard for now.
3159  }
3160
3161  READ_FLAG( uiCode, "vps_vui_present_flag"); vps->setVpsVuiPresentFlag(uiCode ? true : false);
3162
3163  if ( vps->getVpsVuiPresentFlag() )
3164  {
3165    while ( m_pcBitstream->getNumBitsRead() % 8 != 0 )
3166    {
3167      READ_FLAG( uiCode, "vps_vui_alignment_bit_equal_to_one"); assert(uiCode == 1);
3168    }
3169    parseVPSVUI(vps);
3170  }
3171  else
3172  {
3173    // set default values for VPS VUI
3174    defaultVPSVUI( vps );
3175  }
3176}
3177
3178Void TDecCavlc::defaultVPSExtension( TComVPS* vps )
3179{
3180  // set default parameters when they are not present
3181  Int i, j;
3182
3183  // When layer_id_in_nuh[ i ] is not present, the value is inferred to be equal to i.
3184  for(i = 0; i < vps->getMaxLayers(); i++)
3185  {
3186    vps->setLayerIdInNuh(i, i);
3187    vps->setLayerIdxInVps(vps->getLayerIdInNuh(i), i);
3188  }
3189
3190  // When not present, sub_layers_vps_max_minus1[ i ] is inferred to be equal to vps_max_sub_layers_minus1.
3191  for( i = 0; i < vps->getMaxLayers(); i++)
3192  {
3193    vps->setMaxTSLayersMinus1(i, vps->getMaxTLayers()-1);
3194  }
3195
3196  // When not present, max_tid_il_ref_pics_plus1[ i ][ j ] is inferred to be equal to 7.
3197  for( i = 0; i < vps->getMaxLayers() - 1; i++ )
3198  {
3199    for( j = i + 1; j < vps->getMaxLayers(); j++ )
3200    {
3201      vps->setMaxTidIlRefPicsPlus1(i, j, 7);
3202    }
3203  }
3204
3205  // When not present, the value of num_add_olss is inferred to be equal to 0.
3206  // NumOutputLayerSets = num_add_olss + NumLayerSets
3207  vps->setNumOutputLayerSets( vps->getNumLayerSets() );
3208
3209  // For i in the range of 0 to NumOutputLayerSets-1, inclusive, the variable LayerSetIdxForOutputLayerSet[ i ] is derived as specified in the following:
3210  // LayerSetIdxForOutputLayerSet[ i ] = ( i <= vps_number_layer_sets_minus1 ) ? i : layer_set_idx_for_ols_minus1[ i ] + 1
3211  for( i = 1; i < vps->getNumOutputLayerSets(); i++ )
3212  {
3213    vps->setOutputLayerSetIdx( i, i < vps->getNumLayerSets() ? i : 1 );
3214
3215    Int lsIdx = vps->getOutputLayerSetIdx(i);
3216
3217    for( j = 0; j < vps->getNumLayersInIdList(lsIdx); j++ )
3218    {
3219      vps->setOutputLayerFlag(i, j, 1);
3220    }
3221  }
3222
3223  // Default output layer set
3224  // The value of NumLayersInIdList[ 0 ] is set equal to 1 and the value of LayerSetLayerIdList[ 0 ][ 0 ] is set equal to 0.
3225  vps->setOutputLayerSetIdx(0, 0);
3226
3227  // The value of output_layer_flag[ 0 ][ 0 ] is inferred to be equal to 1.
3228  vps->setOutputLayerFlag(0, 0, true);
3229
3230  vps->deriveNecessaryLayerFlag(0);
3231
3232  // The value of sub_layer_dpb_info_present_flag[ i ][ 0 ] for any possible value of i is inferred to be equal to 1
3233  // When not present, the value of sub_layer_dpb_info_present_flag[ i ][ j ] for j greater than 0 and any possible value of i, is inferred to be equal to be equal to 0.
3234  for( i = 1; i < vps->getNumOutputLayerSets(); i++ )
3235  {
3236    vps->setSubLayerDpbInfoPresentFlag( i, 0, true );
3237  }
3238
3239  // When not present, the value of vps_num_rep_formats_minus1 is inferred to be equal to MaxLayersMinus1.
3240  vps->setVpsNumRepFormats( vps->getMaxLayers() );
3241
3242  // When not present, the value of rep_format_idx_present_flag is inferred to be equal to 0
3243  vps->setRepFormatIdxPresentFlag( false );
3244
3245  if( !vps->getRepFormatIdxPresentFlag() )
3246  {
3247    // When not present, the value of vps_rep_format_idx[ i ] is inferred to be equal to Min(i, vps_num_rep_formats_minus1).
3248    for(i = 1; i < vps->getMaxLayers(); i++)
3249    {
3250      vps->setVpsRepFormatIdx( i, min( (Int)i, vps->getVpsNumRepFormats() - 1 ) );
3251    }
3252  }
3253
3254  vps->setVpsPocLsbAlignedFlag(false);
3255
3256  // When not present, poc_lsb_not_present_flag[ i ] is inferred to be equal to 0.
3257  for( i = 1; i< vps->getMaxLayers(); i++ )
3258  {
3259    vps->setPocLsbNotPresentFlag(i, 0);
3260  }
3261
3262  // set default values for VPS VUI
3263  defaultVPSVUI( vps );
3264}
3265
3266Void TDecCavlc::defaultVPSVUI( TComVPS* vps )
3267{
3268  // When not present, the value of all_layers_idr_aligned_flag is inferred to be equal to 0.
3269  vps->setCrossLayerIrapAlignFlag( false );
3270
3271  // When single_layer_for_non_irap_flag is not present, it is inferred to be equal to 0.
3272  vps->setSingleLayerForNonIrapFlag( false );
3273
3274  // When higher_layer_irap_skip_flag is not present it is inferred to be equal to 0
3275  vps->setHigherLayerIrapSkipFlag( false );
3276}
3277
3278Void  TDecCavlc::parseRepFormat( RepFormat *repFormat, RepFormat *repFormatPrev )
3279{
3280  UInt uiCode;
3281  READ_CODE( 16, uiCode, "pic_width_vps_in_luma_samples" );        repFormat->setPicWidthVpsInLumaSamples ( uiCode );
3282  READ_CODE( 16, uiCode, "pic_height_vps_in_luma_samples" );       repFormat->setPicHeightVpsInLumaSamples( uiCode );
3283  READ_FLAG( uiCode, "chroma_and_bit_depth_vps_present_flag" );    repFormat->setChromaAndBitDepthVpsPresentFlag( uiCode ? true : false ); 
3284
3285  if( !repFormatPrev )
3286  {
3287    // The value of chroma_and_bit_depth_vps_present_flag of the first rep_format( ) syntax structure in the VPS shall be equal to 1
3288    assert( repFormat->getChromaAndBitDepthVpsPresentFlag() );
3289  }
3290
3291  if( repFormat->getChromaAndBitDepthVpsPresentFlag() )
3292  {
3293    READ_CODE( 2, uiCode, "chroma_format_vps_idc" );
3294#if AUXILIARY_PICTURES
3295    repFormat->setChromaFormatVpsIdc( ChromaFormat(uiCode) );
3296#else
3297    repFormat->setChromaFormatVpsIdc( uiCode );
3298#endif
3299
3300    if( repFormat->getChromaFormatVpsIdc() == 3 )
3301    {
3302      READ_FLAG( uiCode, "separate_colour_plane_vps_flag" );       repFormat->setSeparateColourPlaneVpsFlag( uiCode ? true : false );
3303    }
3304
3305    READ_CODE( 4, uiCode, "bit_depth_vps_luma_minus8" );           repFormat->setBitDepthVpsLuma  ( uiCode + 8 );
3306    READ_CODE( 4, uiCode, "bit_depth_vps_chroma_minus8" );         repFormat->setBitDepthVpsChroma( uiCode + 8 );
3307  }
3308  else if( repFormatPrev )
3309  {
3310    // chroma_and_bit_depth_vps_present_flag equal to 0 specifies that the syntax elements, chroma_format_vps_idc, separate_colour_plane_vps_flag, bit_depth_vps_luma_minus8, and
3311    // bit_depth_vps_chroma_minus8 are not present and inferred from the previous rep_format( ) syntax structure in the VPS.
3312
3313    repFormat->setChromaFormatVpsIdc        ( repFormatPrev->getChromaFormatVpsIdc() );
3314    repFormat->setSeparateColourPlaneVpsFlag( repFormatPrev->getSeparateColourPlaneVpsFlag() );
3315    repFormat->setBitDepthVpsLuma           ( repFormatPrev->getBitDepthVpsLuma() );
3316    repFormat->setBitDepthVpsChroma         ( repFormatPrev->getBitDepthVpsChroma() );
3317  }
3318
3319  READ_FLAG( uiCode, "conformance_window_vps_flag" );
3320  if( uiCode != 0) 
3321  {
3322    Window &conf = repFormat->getConformanceWindowVps();
3323    READ_UVLC( uiCode, "conf_win_vps_left_offset" );         conf.setWindowLeftOffset  ( uiCode );
3324    READ_UVLC( uiCode, "conf_win_vps_right_offset" );        conf.setWindowRightOffset ( uiCode );
3325    READ_UVLC( uiCode, "conf_win_vps_top_offset" );          conf.setWindowTopOffset   ( uiCode );
3326    READ_UVLC( uiCode, "conf_win_vps_bottom_offset" );       conf.setWindowBottomOffset( uiCode );
3327  }
3328}
3329
3330Void TDecCavlc::parseVpsDpbSizeTable( TComVPS *vps )
3331{
3332  UInt uiCode;
3333
3334  vps->calculateMaxSLInLayerSets();
3335  vps->deriveNumberOfSubDpbs();
3336
3337  for(Int i = 1; i < vps->getNumOutputLayerSets(); i++)
3338  {
3339    Int layerSetIdxForOutputLayerSet = vps->getOutputLayerSetIdx( i );
3340
3341    READ_FLAG( uiCode, "sub_layer_flag_info_present_flag[i]");  vps->setSubLayerFlagInfoPresentFlag( i, uiCode ? true : false );
3342
3343    for(Int j = 0; j <= vps->getMaxSLayersInLayerSetMinus1( layerSetIdxForOutputLayerSet ); j++)
3344    {
3345      if( j > 0 && vps->getSubLayerFlagInfoPresentFlag(i) )
3346      {
3347        READ_FLAG( uiCode, "sub_layer_dpb_info_present_flag[i]");  vps->setSubLayerDpbInfoPresentFlag( i, j, uiCode ? true : false);
3348      }
3349      else
3350      {
3351        if( j == 0 )  // Always signal for the first sub-layer
3352        {
3353          vps->setSubLayerDpbInfoPresentFlag( i, j, true );
3354        }
3355        else // if (j != 0) && !vps->getSubLayerFlagInfoPresentFlag(i)
3356        {
3357          vps->setSubLayerDpbInfoPresentFlag( i, j, false );
3358        }
3359      }
3360
3361      if( vps->getSubLayerDpbInfoPresentFlag(i, j) )  // If sub-layer DPB information is present
3362      {
3363        for(Int k = 0; k < vps->getNumSubDpbs(layerSetIdxForOutputLayerSet); k++)
3364        {
3365          uiCode=0;
3366
3367          if( vps->getNecessaryLayerFlag(i, k) && ( vps->getBaseLayerInternalFlag() || vps->getLayerSetLayerIdList(layerSetIdxForOutputLayerSet, k) ) )
3368          {
3369            READ_UVLC( uiCode, "max_vps_dec_pic_buffering_minus1[i][k][j]" ); vps->setMaxVpsDecPicBufferingMinus1( i, k, j, uiCode );
3370          }
3371        }
3372        READ_UVLC( uiCode, "max_vps_num_reorder_pics[i][j]" );              vps->setMaxVpsNumReorderPics( i, j, uiCode);
3373
3374        READ_UVLC( uiCode, "max_vps_latency_increase_plus1[i][j]" );        vps->setMaxVpsLatencyIncreasePlus1( i, j, uiCode);
3375      }
3376    }
3377    for(Int j = vps->getMaxTLayers(); j < MAX_TLAYER; j++)
3378    {
3379      vps->setSubLayerDpbInfoPresentFlag( i, j, false );
3380    }
3381  }
3382
3383  // Infer values when not signalled
3384  for(Int i = 1; i < vps->getNumOutputLayerSets(); i++)
3385  {
3386    Int layerSetIdxForOutputLayerSet = vps->getOutputLayerSetIdx( i );
3387    for(Int j = 0; j < MAX_TLAYER; j++)
3388    {
3389      if( !vps->getSubLayerDpbInfoPresentFlag(i, j) )  // If sub-layer DPB information is NOT present
3390      {
3391        for(Int k = 0; k < vps->getNumLayersInIdList( layerSetIdxForOutputLayerSet ); k++)
3392        {
3393          vps->setMaxVpsDecPicBufferingMinus1( i, k, j, vps->getMaxVpsDecPicBufferingMinus1( i, k, j - 1 ) );
3394        }
3395        vps->setMaxVpsNumReorderPics( i, j, vps->getMaxVpsNumReorderPics( i, j - 1) );
3396        vps->setMaxVpsLatencyIncreasePlus1( i, j, vps->getMaxVpsLatencyIncreasePlus1( i, j - 1 ) );
3397      }
3398    }
3399  }
3400}
3401
3402Void TDecCavlc::parseVPSVUI(TComVPS *vps)
3403{
3404  UInt i,j;
3405  UInt uiCode;
3406  READ_FLAG(uiCode, "cross_layer_pic_type_aligned_flag" );
3407  vps->setCrossLayerPictureTypeAlignFlag(uiCode);
3408
3409  if( !uiCode ) 
3410  {
3411    READ_FLAG(uiCode, "cross_layer_irap_aligned_flag" );
3412    vps->setCrossLayerIrapAlignFlag(uiCode);
3413  }
3414  else
3415  {
3416    vps->setCrossLayerIrapAlignFlag(true);
3417  }
3418
3419  if( vps->getCrossLayerIrapAlignFlag() )
3420  {
3421    READ_FLAG( uiCode, "all_layers_idr_aligned_flag" );
3422    vps->setCrossLayerAlignedIdrOnlyFlag(uiCode);
3423  }
3424
3425  READ_FLAG( uiCode,        "bit_rate_present_vps_flag" );  vps->setBitRatePresentVpsFlag( uiCode ? true : false );
3426  READ_FLAG( uiCode,        "pic_rate_present_vps_flag" );  vps->setPicRatePresentVpsFlag( uiCode ? true : false );
3427
3428  if ( vps->getBitRatePresentVpsFlag() || vps->getPicRatePresentVpsFlag() )
3429  {
3430    for( i = vps->getBaseLayerInternalFlag() ? 0 : 1; i < vps->getNumLayerSets(); i++ )
3431    {
3432      for( j = 0; j <= vps->getMaxSLayersInLayerSetMinus1( i ); j++ ) 
3433      {
3434        if( vps->getBitRatePresentVpsFlag() )
3435        {
3436          READ_FLAG( uiCode, "bit_rate_present_flag[i][j]" ); vps->setBitRatePresentFlag( i, j, uiCode ? true : false );           
3437        }
3438        if( vps->getPicRatePresentVpsFlag( )  )
3439        {
3440          READ_FLAG( uiCode, "pic_rate_present_flag[i][j]" ); vps->setPicRatePresentFlag( i, j, uiCode ? true : false );
3441        }
3442        if( vps->getBitRatePresentFlag( i, j ) )
3443        {
3444          READ_CODE( 16, uiCode, "avg_bit_rate" ); vps->setAvgBitRate( i, j, uiCode );
3445          READ_CODE( 16, uiCode, "max_bit_rate" ); vps->setMaxBitRate( i, j, uiCode );
3446        }
3447        else
3448        {
3449          vps->setAvgBitRate( i, j, 0 );
3450          vps->setMaxBitRate( i, j, 0 );
3451        }
3452        if( vps->getPicRatePresentFlag( i, j ) )
3453        {
3454          READ_CODE( 2,  uiCode, "constant_pic_rate_idc" ); vps->setConstPicRateIdc( i, j, uiCode );
3455          READ_CODE( 16, uiCode, "avg_pic_rate" );          vps->setAvgPicRate( i, j, uiCode );
3456        }
3457        else
3458        {
3459          vps->setConstPicRateIdc( i, j, 0 );
3460          vps->setAvgPicRate( i, j, 0 );
3461        }
3462      }
3463    }
3464  }
3465
3466  READ_FLAG( uiCode, "video_signal_info_idx_present_flag" ); vps->setVideoSigPresentVpsFlag( uiCode == 1 );
3467  if (vps->getVideoSigPresentVpsFlag())
3468  {
3469    READ_CODE(4, uiCode, "vps_num_video_signal_info_minus1" ); vps->setNumVideoSignalInfo(uiCode + 1);
3470  }
3471  else
3472  {
3473    vps->setNumVideoSignalInfo(vps->getMaxLayers() - (vps->getBaseLayerInternalFlag() ? 0 : 1));
3474  }
3475
3476  for(i = 0; i < vps->getNumVideoSignalInfo(); i++)
3477  {
3478    READ_CODE(3, uiCode, "video_vps_format" ); vps->setVideoVPSFormat(i,uiCode);
3479    READ_FLAG(uiCode, "video_full_range_vps_flag" ); vps->setVideoFullRangeVpsFlag(i,uiCode);
3480    READ_CODE(8, uiCode, "color_primaries_vps" ); vps->setColorPrimaries(i,uiCode);
3481    READ_CODE(8, uiCode, "transfer_characteristics_vps" ); vps->setTransCharacter(i,uiCode);
3482    READ_CODE(8, uiCode, "matrix_coeffs_vps" );vps->setMaxtrixCoeff(i,uiCode);
3483  }
3484
3485  if( vps->getVideoSigPresentVpsFlag() && vps->getNumVideoSignalInfo() > 1 )
3486  {
3487    for(i = vps->getBaseLayerInternalFlag() ? 0 : 1; i < vps->getMaxLayers(); i++)
3488    {
3489      READ_CODE(4, uiCode, "vps_video_signal_info_idx" ); vps->setVideoSignalInfoIdx(i, uiCode);
3490    }
3491  }
3492  else if ( !vps->getVideoSigPresentVpsFlag() )
3493  {
3494    for(i = vps->getBaseLayerInternalFlag() ? 0 : 1; i < vps->getMaxLayers(); i++)
3495    {
3496      vps->setVideoSignalInfoIdx( i, i );
3497    }
3498  }
3499  else // ( vps->getNumVideoSignalInfo() = 0 )
3500  {
3501    for(i = vps->getBaseLayerInternalFlag() ? 0 : 1; i < vps->getMaxLayers(); i++)
3502    {
3503      vps->setVideoSignalInfoIdx( i, 0 );
3504    }
3505  }
3506
3507  READ_FLAG( uiCode, "tiles_not_in_use_flag" ); vps->setTilesNotInUseFlag(uiCode == 1);
3508
3509  if( !uiCode )
3510  {
3511    for( i = vps->getBaseLayerInternalFlag() ? 0 : 1; i < vps->getMaxLayers(); i++ )
3512    {
3513      READ_FLAG( uiCode, "tiles_in_use_flag[ i ]" ); vps->setTilesInUseFlag(i, (uiCode == 1));
3514
3515      if( uiCode )
3516      {
3517        READ_FLAG( uiCode, "loop_filter_not_across_tiles_flag[ i ]" ); vps->setLoopFilterNotAcrossTilesFlag(i, (uiCode == 1));
3518      }
3519      else
3520      {
3521        vps->setLoopFilterNotAcrossTilesFlag(i, false);
3522      }
3523    }
3524
3525    for( i = vps->getBaseLayerInternalFlag() ? 1 : 2; i < vps->getMaxLayers(); i++ )
3526    {
3527      for( j = 0; j < vps->getNumDirectRefLayers(vps->getLayerIdInNuh(i)); j++ )
3528      {
3529        UInt layerIdx = vps->getLayerIdxInVps(vps->getRefLayerId(vps->getLayerIdInNuh(i), j));
3530
3531        if( vps->getTilesInUseFlag(i) && vps->getTilesInUseFlag(layerIdx) )
3532        {
3533          READ_FLAG( uiCode, "tile_boundaries_aligned_flag[i][j]" ); vps->setTileBoundariesAlignedFlag(i,j,(uiCode == 1));
3534        }
3535      }
3536    }
3537  }
3538
3539  READ_FLAG( uiCode, "wpp_not_in_use_flag" ); vps->setWppNotInUseFlag(uiCode == 1);
3540  if( !uiCode )
3541  {
3542    for( i = vps->getBaseLayerInternalFlag() ? 0 : 1; i < vps->getMaxLayers(); i++ )
3543    {
3544      READ_FLAG( uiCode, "wpp_in_use_flag[ i ]" ); vps->setWppInUseFlag(i, (uiCode == 1));
3545    }
3546  }
3547
3548  READ_FLAG(uiCode, "single_layer_for_non_irap_flag" ); vps->setSingleLayerForNonIrapFlag(uiCode == 1 ? true : false);
3549
3550  READ_FLAG(uiCode, "higher_layer_irap_skip_flag" ); vps->setHigherLayerIrapSkipFlag(uiCode == 1 ? true : false);
3551
3552  // When single_layer_for_non_irap_flag is equal to 0, higher_layer_irap_skip_flag shall be equal to 0
3553  if( !vps->getSingleLayerForNonIrapFlag() )
3554  {
3555    assert( !vps->getHigherLayerIrapSkipFlag() );
3556  }
3557
3558  READ_FLAG( uiCode, "ilp_restricted_ref_layers_flag" ); vps->setIlpRestrictedRefLayersFlag( uiCode == 1 );
3559  if( vps->getIlpRestrictedRefLayersFlag())
3560  {
3561    for(i = 1; i < vps->getMaxLayers(); i++)
3562    {
3563      for(j = 0; j < vps->getNumDirectRefLayers(vps->getLayerIdInNuh(i)); j++)
3564      {
3565        if( vps->getBaseLayerInternalFlag() || vps->getRefLayerId(vps->getLayerIdInNuh(i), j) )
3566        {
3567          READ_UVLC( uiCode, "min_spatial_segment_offset_plus1[i][j]" ); vps->setMinSpatialSegmentOffsetPlus1( i, j, uiCode );
3568          if( vps->getMinSpatialSegmentOffsetPlus1(i,j ) > 0 )
3569          {
3570            READ_FLAG( uiCode, "ctu_based_offset_enabled_flag[i][j]"); vps->setCtuBasedOffsetEnabledFlag(i, j, uiCode == 1 );
3571            if(vps->getCtuBasedOffsetEnabledFlag(i,j))
3572            {
3573              READ_UVLC( uiCode, "min_horizontal_ctu_offset_plus1[i][j]"); vps->setMinHorizontalCtuOffsetPlus1( i,j, uiCode );
3574            }
3575          }
3576        }
3577      }
3578    }
3579  }
3580
3581#if O0164_MULTI_LAYER_HRD
3582  READ_FLAG(uiCode, "vps_vui_bsp_hrd_present_flag" ); vps->setVpsVuiBspHrdPresentFlag(uiCode);
3583
3584  if( vps->getVpsVuiBspHrdPresentFlag() )
3585  {
3586    parseVpsVuiBspHrdParams(vps);
3587  }
3588#endif
3589
3590  for( i = 1; i < vps->getMaxLayers(); i++ )
3591  {
3592    if (vps->getNumRefLayers(vps->getLayerIdInNuh(i)) == 0)
3593    {
3594      READ_FLAG( uiCode, "base_layer_parameter_set_compatibility_flag" ); 
3595      vps->setBaseLayerPSCompatibilityFlag( i, uiCode );
3596    }
3597    else
3598    {
3599      vps->setBaseLayerPSCompatibilityFlag( i, 0 );
3600    }
3601  }
3602}
3603
3604Void TDecCavlc::parseSPSExtension( TComSPS* pcSPS )
3605{
3606  UInt uiCode;
3607  // more syntax elements to be parsed here
3608
3609  READ_FLAG( uiCode, "inter_view_mv_vert_constraint_flag" );
3610}
3611
3612#if CGS_3D_ASYMLUT
3613Void TDecCavlc::xParse3DAsymLUT( TCom3DAsymLUT * pc3DAsymLUT )
3614{
3615  UInt uiNumRefLayersM1;
3616  READ_UVLC( uiNumRefLayersM1, "num_cm_ref_layers_minus1" );
3617  assert( uiNumRefLayersM1 <= 61 );
3618  for( UInt i = 0 ; i <= uiNumRefLayersM1 ; i++ )
3619  {
3620    UInt uiRefLayerId;
3621    READ_CODE( 6, uiRefLayerId, "cm_ref_layer_id" );
3622    pc3DAsymLUT->addRefLayerId( uiRefLayerId );
3623  }
3624
3625  UInt uiCurOctantDepth, uiCurPartNumLog2, uiInputBitDepthM8, uiOutputBitDepthM8, uiResQaunBit, uiDeltaBits;;
3626 
3627  READ_CODE( 2, uiCurOctantDepth, "cm_octant_depth" ); 
3628  READ_CODE( 2, uiCurPartNumLog2, "cm_y_part_num_log2" );     
3629
3630  UInt uiChromaInputBitDepthM8, uiChromaOutputBitDepthM8;
3631
3632  READ_UVLC( uiInputBitDepthM8, "cm_input_luma_bit_depth_minus8" );
3633  READ_UVLC( uiChromaInputBitDepthM8 , "cm_input_chroma_bit_depth_minus8" );
3634  READ_UVLC( uiOutputBitDepthM8, "cm_output_luma_bit_depth_minus8" );
3635  READ_UVLC( uiChromaOutputBitDepthM8,  "cm_output_chroma_bit_depth_minus8" );
3636  READ_CODE( 2, uiResQaunBit, "cm_res_quant_bit" );
3637
3638  READ_CODE( 2, uiDeltaBits, "cm_flc_bits" );
3639  pc3DAsymLUT->setDeltaBits(uiDeltaBits + 1);
3640
3641  Int nAdaptCThresholdU = 1 << ( uiChromaInputBitDepthM8 + 8 - 1 );
3642  Int nAdaptCThresholdV = 1 << ( uiChromaInputBitDepthM8 + 8 - 1 );
3643
3644  if( uiCurOctantDepth == 1 )
3645  {
3646    Int delta = 0;
3647    READ_SVLC( delta, "cm_adapt_threshold_u_delta" );
3648    nAdaptCThresholdU += delta;
3649    READ_SVLC( delta, "cm_adapt_threshold_v_delta" );
3650    nAdaptCThresholdV += delta;
3651  }
3652
3653  pc3DAsymLUT->destroy();
3654  pc3DAsymLUT->create( uiCurOctantDepth, uiInputBitDepthM8 + 8, uiChromaInputBitDepthM8 + 8, uiOutputBitDepthM8 + 8, uiChromaOutputBitDepthM8 + 8, uiCurPartNumLog2, nAdaptCThresholdU, nAdaptCThresholdV );
3655  pc3DAsymLUT->setResQuantBit( uiResQaunBit );
3656
3657#if R0164_CGS_LUT_BUGFIX_CHECK
3658  pc3DAsymLUT->xInitCuboids();
3659#endif
3660  xParse3DAsymLUTOctant( pc3DAsymLUT, 0, 0, 0, 0, 1 << pc3DAsymLUT->getCurOctantDepth() );
3661#if R0164_CGS_LUT_BUGFIX_CHECK
3662  printf("============= Before 'xCuboidsFilledCheck()': ================\n");
3663  pc3DAsymLUT->display();
3664  pc3DAsymLUT->xCuboidsFilledCheck( false );
3665  printf("============= After 'xCuboidsFilledCheck()': =================\n");
3666  pc3DAsymLUT->display();
3667#endif
3668}
3669
3670Void TDecCavlc::xParse3DAsymLUTOctant( TCom3DAsymLUT * pc3DAsymLUT, Int nDepth, Int yIdx, Int uIdx, Int vIdx, Int nLength )
3671{
3672  UInt uiOctantSplit = nDepth < pc3DAsymLUT->getCurOctantDepth();
3673  if( nDepth < pc3DAsymLUT->getCurOctantDepth() )
3674  {
3675    READ_FLAG( uiOctantSplit, "split_octant_flag" );
3676  }
3677  Int nYPartNum = 1 << pc3DAsymLUT->getCurYPartNumLog2();
3678
3679  if( uiOctantSplit )
3680  {
3681    Int nHalfLength = nLength >> 1;
3682    for( Int l = 0 ; l < 2 ; l++ )
3683    {
3684      for( Int m = 0 ; m < 2 ; m++ )
3685      {
3686        for( Int n = 0 ; n < 2 ; n++ )
3687        {
3688          xParse3DAsymLUTOctant( pc3DAsymLUT, nDepth + 1, yIdx + l * nHalfLength * nYPartNum, uIdx + m * nHalfLength, vIdx + n * nHalfLength, nHalfLength );
3689        }
3690      }
3691    }
3692  }
3693  else
3694  {
3695    Int nFLCbits = pc3DAsymLUT->getMappingShift()-pc3DAsymLUT->getResQuantBit()-pc3DAsymLUT->getDeltaBits() ; 
3696    nFLCbits = nFLCbits >= 0 ? nFLCbits:0;
3697
3698    for( Int l = 0; l < nYPartNum; l++ )
3699    {
3700      Int shift = pc3DAsymLUT->getCurOctantDepth() - nDepth;
3701
3702      for( Int nVertexIdx = 0; nVertexIdx < 4; nVertexIdx++ )
3703      {
3704        UInt uiCodeVertex = 0;
3705        Int deltaY = 0, deltaU = 0, deltaV = 0;
3706
3707        READ_FLAG( uiCodeVertex, "coded_vertex_flag" );
3708
3709        if( uiCodeVertex )
3710        {
3711          xReadParam( deltaY, nFLCbits );
3712          xReadParam( deltaU, nFLCbits );
3713          xReadParam( deltaV, nFLCbits );
3714        }
3715
3716        pc3DAsymLUT->setCuboidVertexResTree( yIdx + (l<<shift), uIdx, vIdx, nVertexIdx, deltaY, deltaU, deltaV );
3717
3718        for( Int m = 1; m < (1<<shift); m++ )
3719        {
3720          pc3DAsymLUT->setCuboidVertexResTree( yIdx + (l<<shift) + m, uIdx, vIdx, nVertexIdx, 0, 0, 0 );
3721#if R0164_CGS_LUT_BUGFIX_CHECK
3722          pc3DAsymLUT->xSetFilled( yIdx + (l<<shift) + m , uIdx , vIdx );
3723#endif
3724        }
3725      }
3726#if R0164_CGS_LUT_BUGFIX_CHECK
3727      pc3DAsymLUT->xSetExplicit( yIdx + (l<<shift) , uIdx , vIdx );
3728#endif
3729    }
3730
3731    for( Int u=0; u<nLength; u++ )
3732    {
3733      for( Int v=0; v<nLength; v++ )
3734      {
3735        if( u!=0 || v!=0 )
3736        {
3737          for( Int y=0; y<nLength*nYPartNum; y++ )
3738          {
3739            for( Int nVertexIdx = 0; nVertexIdx < 4; nVertexIdx++ )
3740            {
3741              pc3DAsymLUT->setCuboidVertexResTree( yIdx + y, uIdx + u, vIdx + v, nVertexIdx, 0, 0, 0 );
3742#if R0164_CGS_LUT_BUGFIX_CHECK
3743              pc3DAsymLUT->xSetFilled( yIdx + y, uIdx + u, vIdx + v );
3744#endif
3745            }
3746          }
3747        }
3748      }
3749    }
3750  }
3751}
3752
3753Void TDecCavlc::xReadParam( Int& param, Int rParam )
3754{
3755  UInt prefix;
3756  UInt codeWord ;
3757  UInt rSymbol;
3758  UInt sign;
3759
3760  READ_UVLC( prefix, "quotient")  ;
3761  READ_CODE (rParam, codeWord, "remainder");
3762  rSymbol = (prefix<<rParam) + codeWord;
3763
3764  if(rSymbol)
3765  {
3766    READ_FLAG(sign, "sign");
3767    param = sign ? -(Int)(rSymbol) : (Int)(rSymbol);
3768  }
3769  else param = 0;
3770}
3771#endif
3772
3773Void TDecCavlc::parseVpsVuiBspHrdParams( TComVPS *vps )
3774{
3775  UInt uiCode;
3776  assert (vps->getTimingInfo()->getTimingInfoPresentFlag() == 1);
3777  READ_UVLC( uiCode, "vps_num_add_hrd_params" ); vps->setVpsNumAddHrdParams(uiCode);
3778  vps->createBspHrdParamBuffer(vps->getVpsNumAddHrdParams()); // Also allocates m_cprmsAddPresentFlag and m_numSubLayerHrdMinus
3779
3780  for( Int i = vps->getNumHrdParameters(), j = 0; i < vps->getNumHrdParameters() + vps->getVpsNumAddHrdParams(); i++, j++ ) // j = i - vps->getNumHrdParameters()
3781  {
3782    if( i > 0 )
3783    {
3784      READ_FLAG( uiCode, "cprms_add_present_flag[i]" );   vps->setCprmsAddPresentFlag(j, uiCode ? true : false);
3785    }
3786    else
3787    {
3788      // i == 0
3789      if( vps->getNumHrdParameters() == 0 )
3790      {
3791        vps->setCprmsAddPresentFlag(0, true);
3792      }
3793    }
3794
3795    READ_UVLC( uiCode, "num_sub_layer_hrd_minus1[i]" ); vps->setNumSubLayerHrdMinus1(j, uiCode );
3796    assert( uiCode <= vps->getMaxTLayers() - 1 );
3797   
3798    parseHrdParameters( vps->getBspHrd(j), vps->getCprmsAddPresentFlag(j), vps->getNumSubLayerHrdMinus1(j) );
3799
3800    if( i > 0 && !vps->getCprmsAddPresentFlag(i) )
3801    {
3802      // Copy common information parameters
3803      if( i == vps->getNumHrdParameters() )
3804      {
3805        vps->getBspHrd(j)->copyCommonInformation( vps->getHrdParameters( vps->getNumHrdParameters() - 1 ) );
3806      }
3807      else
3808      {
3809        vps->getBspHrd(j)->copyCommonInformation( vps->getBspHrd( j - 1 ) );
3810      }
3811    }
3812  }
3813
3814  if( vps->getNumHrdParameters() + vps->getVpsNumAddHrdParams() > 0 )
3815  {
3816    for (Int h = 1; h < vps->getNumOutputLayerSets(); h++)
3817    {
3818      Int lsIdx = vps->getOutputLayerSetIdx(h);
3819      READ_UVLC(uiCode, "num_signalled_partitioning_schemes[h]"); vps->setNumSignalledPartitioningSchemes(h, uiCode);
3820
3821      for (Int j = 1; j < vps->getNumSignalledPartitioningSchemes(h) + 1; j++)
3822      {
3823        READ_UVLC(uiCode, "num_partitions_in_scheme_minus1[h][j]"); vps->setNumPartitionsInSchemeMinus1(h, j, uiCode);
3824
3825        for( Int k = 0; k <= vps->getNumPartitionsInSchemeMinus1(h, j); k++ )
3826        {
3827          for( Int r = 0; r < vps->getNumLayersInIdList(lsIdx); r++ )
3828          {
3829            READ_FLAG(uiCode, "layer_included_in_partition_flag[h][j][k][r]"); vps->setLayerIncludedInPartitionFlag(h, j, k, r, uiCode ? true : false);
3830          }
3831        }
3832      }
3833
3834      for( Int i = 0; i < vps->getNumSignalledPartitioningSchemes(h) + 1; i++ )
3835      {
3836        for( Int t = 0; t <= vps->getMaxSLayersInLayerSetMinus1(lsIdx); t++ )
3837        {
3838          READ_UVLC(uiCode, "num_bsp_schedules_minus1[h][i][t]");              vps->setNumBspSchedulesMinus1(h, i, t, uiCode);
3839
3840          for( Int j = 0; j <= vps->getNumBspSchedulesMinus1(h, i, t); j++)
3841          {
3842            for( Int k = 0; k <= vps->getNumPartitionsInSchemeMinus1(h, i); k++ )
3843            {
3844              if( vps->getNumHrdParameters() + vps->getVpsNumAddHrdParams() > 1 )
3845              {
3846                Int numBits = 1;
3847
3848                while( (1 << numBits) < (vps->getNumHrdParameters() + vps->getVpsNumAddHrdParams()) )
3849                {
3850                  numBits++;
3851                }
3852
3853                READ_CODE(numBits, uiCode, "bsp_hrd_idx[h][i][t][j][k]");      vps->setBspHrdIdx(h, i, t, j, k, uiCode);
3854              }
3855
3856              READ_UVLC(uiCode, "bsp_sched_idx[h][i][t][j][k]");    vps->setBspSchedIdx(h, i, t, j, k, uiCode);
3857            }
3858          }
3859        }
3860      }
3861
3862      // To be done: Check each layer included in not more than one BSP in every partitioning scheme,
3863      // and other related checks associated with layers in bitstream partitions.
3864
3865    }
3866  }
3867}
3868#endif //SVC_EXTENSION
3869//! \}
3870
Note: See TracBrowser for help on using the repository browser.