source: 3DVCSoftware/branches/HTM-DEV-0.3-dev2/source/Lib/TLibDecoder/TDecCAVLC.cpp @ 539

Last change on this file since 539 was 539, checked in by orange, 11 years ago

Integrated QTLPC and added new line mark at the end of TComWedgelet.cpp for MERL

  • Property svn:eol-style set to native
File size: 83.3 KB
Line 
1/* The copyright in this software is being made available under the BSD
2* License, included below. This software may be subject to other third party
3* and contributor rights, including patent rights, and no such rights are
4* granted under this license. 
5*
6* Copyright (c) 2010-2013, ITU/ISO/IEC
7* All rights reserved.
8*
9* Redistribution and use in source and binary forms, with or without
10* modification, are permitted provided that the following conditions are met:
11*
12*  * Redistributions of source code must retain the above copyright notice,
13*    this list of conditions and the following disclaimer.
14*  * Redistributions in binary form must reproduce the above copyright notice,
15*    this list of conditions and the following disclaimer in the documentation
16*    and/or other materials provided with the distribution.
17*  * Neither the name of the ITU/ISO/IEC nor the names of its contributors may
18*    be used to endorse or promote products derived from this software without
19*    specific prior written permission.
20*
21* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
25* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31* THE POSSIBILITY OF SUCH DAMAGE.
32*/
33
34/** \file     TDecCAVLC.cpp
35\brief    CAVLC decoder class
36*/
37
38#include "TDecCAVLC.h"
39#include "SEIread.h"
40#include "TDecSlice.h"
41
42//! \ingroup TLibDecoder
43//! \{
44
45#if ENC_DEC_TRACE
46
47Void  xTraceSPSHeader (TComSPS *pSPS)
48{
49#if H_MV_ENC_DEC_TRAC
50  if ( g_disableHLSTrace )
51  {
52    return; 
53  }
54  // To avoid mismatches
55  fprintf( g_hTrace, "=========== Sequence Parameter Set ===========\n" );
56#else
57  fprintf( g_hTrace, "=========== Sequence Parameter Set ID: %d ===========\n", pSPS->getSPSId() );
58#endif
59}
60
61Void  xTracePPSHeader (TComPPS *pPPS)
62{
63#if H_MV_ENC_DEC_TRAC
64  if ( g_disableHLSTrace )
65  {
66    return; 
67  }
68  fprintf( g_hTrace, "=========== Picture Parameter Set ===========\n" );
69#else
70  fprintf( g_hTrace, "=========== Picture Parameter Set ID: %d ===========\n", pPPS->getPPSId() );
71#endif
72}
73
74Void  xTraceSliceHeader (TComSlice *pSlice)
75{
76#if H_MV_ENC_DEC_TRAC
77  if ( g_disableHLSTrace )
78  {
79    return; 
80  }
81#endif
82  fprintf( g_hTrace, "=========== Slice ===========\n");
83}
84
85#endif
86
87// ====================================================================================================================
88// Constructor / destructor / create / destroy
89// ====================================================================================================================
90
91TDecCavlc::TDecCavlc()
92{
93#if H_3D
94  m_aaiTempScale            = new Int* [ MAX_NUM_LAYERS ];
95  m_aaiTempOffset           = new Int* [ MAX_NUM_LAYERS ];
96  for( UInt uiVId = 0; uiVId < MAX_NUM_LAYERS; uiVId++ )
97  {
98    m_aaiTempScale            [ uiVId ] = new Int [ MAX_NUM_LAYERS ];
99    m_aaiTempOffset           [ uiVId ] = new Int [ MAX_NUM_LAYERS ];
100  }
101#endif
102}
103
104TDecCavlc::~TDecCavlc()
105{
106#if H_3D
107  for( UInt uiVId = 0; uiVId < MAX_NUM_LAYERS; uiVId++ )
108  {
109    delete [] m_aaiTempScale            [ uiVId ];
110    delete [] m_aaiTempOffset           [ uiVId ];
111  }
112  delete [] m_aaiTempScale;
113  delete [] m_aaiTempOffset;
114#endif
115}
116
117// ====================================================================================================================
118// Public member functions
119// ====================================================================================================================
120
121void TDecCavlc::parseShortTermRefPicSet( TComSPS* sps, TComReferencePictureSet* rps, Int idx )
122{
123  UInt code;
124  UInt interRPSPred;
125  if (idx > 0)
126  {
127    READ_FLAG(interRPSPred, "inter_ref_pic_set_prediction_flag");  rps->setInterRPSPrediction(interRPSPred);
128  }
129  else
130  {
131    interRPSPred = false;
132    rps->setInterRPSPrediction(false);
133  }
134
135  if (interRPSPred) 
136  {
137    UInt bit;
138    if(idx == sps->getRPSList()->getNumberOfReferencePictureSets())
139    {
140      READ_UVLC(code, "delta_idx_minus1" ); // delta index of the Reference Picture Set used for prediction minus 1
141    }
142    else
143    {
144      code = 0;
145    }
146    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
147    Int rIdx =  idx - 1 - code;
148    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
149    TComReferencePictureSet*   rpsRef = sps->getRPSList()->getReferencePictureSet(rIdx);
150    Int k = 0, k0 = 0, k1 = 0;
151    READ_CODE(1, bit, "delta_rps_sign"); // delta_RPS_sign
152    READ_UVLC(code, "abs_delta_rps_minus1");  // absolute delta RPS minus 1
153    Int deltaRPS = (1 - 2 * bit) * (code + 1); // delta_RPS
154    for(Int j=0 ; j <= rpsRef->getNumberOfPictures(); j++)
155    {
156      READ_CODE(1, bit, "used_by_curr_pic_flag" ); //first bit is "1" if Idc is 1
157      Int refIdc = bit;
158      if (refIdc == 0) 
159      {
160        READ_CODE(1, bit, "use_delta_flag" ); //second bit is "1" if Idc is 2, "0" otherwise.
161        refIdc = bit<<1; //second bit is "1" if refIdc is 2, "0" if refIdc = 0.
162      }
163      if (refIdc == 1 || refIdc == 2)
164      {
165        Int deltaPOC = deltaRPS + ((j < rpsRef->getNumberOfPictures())? rpsRef->getDeltaPOC(j) : 0);
166        rps->setDeltaPOC(k, deltaPOC);
167        rps->setUsed(k, (refIdc == 1));
168
169        if (deltaPOC < 0)
170        {
171          k0++;
172        }
173        else 
174        {
175          k1++;
176        }
177        k++;
178      } 
179      rps->setRefIdc(j,refIdc); 
180    }
181    rps->setNumRefIdc(rpsRef->getNumberOfPictures()+1); 
182    rps->setNumberOfPictures(k);
183    rps->setNumberOfNegativePictures(k0);
184    rps->setNumberOfPositivePictures(k1);
185    rps->sortDeltaPOC();
186  }
187  else
188  {
189    READ_UVLC(code, "num_negative_pics");           rps->setNumberOfNegativePictures(code);
190    READ_UVLC(code, "num_positive_pics");           rps->setNumberOfPositivePictures(code);
191    Int prev = 0;
192    Int poc;
193    for(Int j=0 ; j < rps->getNumberOfNegativePictures(); j++)
194    {
195      READ_UVLC(code, "delta_poc_s0_minus1");
196      poc = prev-code-1;
197      prev = poc;
198      rps->setDeltaPOC(j,poc);
199      READ_FLAG(code, "used_by_curr_pic_s0_flag");  rps->setUsed(j,code);
200    }
201    prev = 0;
202    for(Int j=rps->getNumberOfNegativePictures(); j < rps->getNumberOfNegativePictures()+rps->getNumberOfPositivePictures(); j++)
203    {
204      READ_UVLC(code, "delta_poc_s1_minus1");
205      poc = prev+code+1;
206      prev = poc;
207      rps->setDeltaPOC(j,poc);
208      READ_FLAG(code, "used_by_curr_pic_s1_flag");  rps->setUsed(j,code);
209    }
210    rps->setNumberOfPictures(rps->getNumberOfNegativePictures()+rps->getNumberOfPositivePictures());
211  }
212#if PRINT_RPS_INFO
213  rps->printDeltaPOC();
214#endif
215}
216
217Void TDecCavlc::parsePPS(TComPPS* pcPPS)
218{
219#if ENC_DEC_TRACE 
220  xTracePPSHeader (pcPPS);
221#endif
222  UInt  uiCode;
223
224  Int   iCode;
225
226  READ_UVLC( uiCode, "pps_pic_parameter_set_id");
227  assert(uiCode <= 63);
228  pcPPS->setPPSId (uiCode);
229 
230  READ_UVLC( uiCode, "pps_seq_parameter_set_id");
231  assert(uiCode <= 15);
232  pcPPS->setSPSId (uiCode);
233 
234  READ_FLAG( uiCode, "dependent_slice_segments_enabled_flag"    );    pcPPS->setDependentSliceSegmentsEnabledFlag   ( uiCode == 1 );
235  READ_FLAG( uiCode, "output_flag_present_flag" );                    pcPPS->setOutputFlagPresentFlag( uiCode==1 );
236
237  READ_CODE(3, uiCode, "num_extra_slice_header_bits");                pcPPS->setNumExtraSliceHeaderBits(uiCode);
238  READ_FLAG ( uiCode, "sign_data_hiding_flag" ); pcPPS->setSignHideFlag( uiCode );
239
240  READ_FLAG( uiCode,   "cabac_init_present_flag" );            pcPPS->setCabacInitPresentFlag( uiCode ? true : false );
241
242  READ_UVLC(uiCode, "num_ref_idx_l0_default_active_minus1");
243  assert(uiCode <= 14);
244  pcPPS->setNumRefIdxL0DefaultActive(uiCode+1);
245 
246  READ_UVLC(uiCode, "num_ref_idx_l1_default_active_minus1");
247  assert(uiCode <= 14);
248  pcPPS->setNumRefIdxL1DefaultActive(uiCode+1);
249 
250  READ_SVLC(iCode, "init_qp_minus26" );                            pcPPS->setPicInitQPMinus26(iCode);
251  READ_FLAG( uiCode, "constrained_intra_pred_flag" );              pcPPS->setConstrainedIntraPred( uiCode ? true : false );
252  READ_FLAG( uiCode, "transform_skip_enabled_flag" );               
253  pcPPS->setUseTransformSkip ( uiCode ? true : false ); 
254
255  READ_FLAG( uiCode, "cu_qp_delta_enabled_flag" );            pcPPS->setUseDQP( uiCode ? true : false );
256  if( pcPPS->getUseDQP() )
257  {
258    READ_UVLC( uiCode, "diff_cu_qp_delta_depth" );
259    pcPPS->setMaxCuDQPDepth( uiCode );
260  }
261  else
262  {
263    pcPPS->setMaxCuDQPDepth( 0 );
264  }
265  READ_SVLC( iCode, "pps_cb_qp_offset");
266  pcPPS->setChromaCbQpOffset(iCode);
267  assert( pcPPS->getChromaCbQpOffset() >= -12 );
268  assert( pcPPS->getChromaCbQpOffset() <=  12 );
269
270  READ_SVLC( iCode, "pps_cr_qp_offset");
271  pcPPS->setChromaCrQpOffset(iCode);
272  assert( pcPPS->getChromaCrQpOffset() >= -12 );
273  assert( pcPPS->getChromaCrQpOffset() <=  12 );
274
275  READ_FLAG( uiCode, "pps_slice_chroma_qp_offsets_present_flag" );
276  pcPPS->setSliceChromaQpFlag( uiCode ? true : false );
277
278  READ_FLAG( uiCode, "weighted_pred_flag" );          // Use of Weighting Prediction (P_SLICE)
279  pcPPS->setUseWP( uiCode==1 );
280  READ_FLAG( uiCode, "weighted_bipred_flag" );         // Use of Bi-Directional Weighting Prediction (B_SLICE)
281  pcPPS->setWPBiPred( uiCode==1 );
282
283  READ_FLAG( uiCode, "transquant_bypass_enable_flag");
284  pcPPS->setTransquantBypassEnableFlag(uiCode ? true : false);
285  READ_FLAG( uiCode, "tiles_enabled_flag"               );    pcPPS->setTilesEnabledFlag            ( uiCode == 1 );
286  READ_FLAG( uiCode, "entropy_coding_sync_enabled_flag" );    pcPPS->setEntropyCodingSyncEnabledFlag( uiCode == 1 );
287 
288  if( pcPPS->getTilesEnabledFlag() )
289  {
290    READ_UVLC ( uiCode, "num_tile_columns_minus1" );                pcPPS->setNumColumnsMinus1( uiCode ); 
291    READ_UVLC ( uiCode, "num_tile_rows_minus1" );                   pcPPS->setNumRowsMinus1( uiCode ); 
292    READ_FLAG ( uiCode, "uniform_spacing_flag" );                   pcPPS->setUniformSpacingFlag( uiCode );
293
294    if( !pcPPS->getUniformSpacingFlag())
295    {
296      UInt* columnWidth = (UInt*)malloc(pcPPS->getNumColumnsMinus1()*sizeof(UInt));
297      for(UInt i=0; i<pcPPS->getNumColumnsMinus1(); i++)
298      { 
299        READ_UVLC( uiCode, "column_width_minus1" ); 
300        columnWidth[i] = uiCode+1;
301      }
302      pcPPS->setColumnWidth(columnWidth);
303      free(columnWidth);
304
305      UInt* rowHeight = (UInt*)malloc(pcPPS->getNumRowsMinus1()*sizeof(UInt));
306      for(UInt i=0; i<pcPPS->getNumRowsMinus1(); i++)
307      {
308        READ_UVLC( uiCode, "row_height_minus1" );
309        rowHeight[i] = uiCode + 1;
310      }
311      pcPPS->setRowHeight(rowHeight);
312      free(rowHeight); 
313    }
314
315    if(pcPPS->getNumColumnsMinus1() !=0 || pcPPS->getNumRowsMinus1() !=0)
316    {
317      READ_FLAG ( uiCode, "loop_filter_across_tiles_enabled_flag" );   pcPPS->setLoopFilterAcrossTilesEnabledFlag( uiCode ? true : false );
318    }
319  }
320  READ_FLAG( uiCode, "loop_filter_across_slices_enabled_flag" );       pcPPS->setLoopFilterAcrossSlicesEnabledFlag( uiCode ? true : false );
321  READ_FLAG( uiCode, "deblocking_filter_control_present_flag" );       pcPPS->setDeblockingFilterControlPresentFlag( uiCode ? true : false );
322  if(pcPPS->getDeblockingFilterControlPresentFlag())
323  {
324    READ_FLAG( uiCode, "deblocking_filter_override_enabled_flag" );    pcPPS->setDeblockingFilterOverrideEnabledFlag( uiCode ? true : false );
325    READ_FLAG( uiCode, "pps_disable_deblocking_filter_flag" );         pcPPS->setPicDisableDeblockingFilterFlag(uiCode ? true : false );
326    if(!pcPPS->getPicDisableDeblockingFilterFlag())
327    {
328      READ_SVLC ( iCode, "pps_beta_offset_div2" );                     pcPPS->setDeblockingFilterBetaOffsetDiv2( iCode );
329      READ_SVLC ( iCode, "pps_tc_offset_div2" );                       pcPPS->setDeblockingFilterTcOffsetDiv2( iCode );
330    }
331  }
332  READ_FLAG( uiCode, "pps_scaling_list_data_present_flag" );           pcPPS->setScalingListPresentFlag( uiCode ? true : false );
333  if(pcPPS->getScalingListPresentFlag ())
334  {
335    parseScalingList( pcPPS->getScalingList() );
336  }
337
338  READ_FLAG( uiCode, "lists_modification_present_flag");
339  pcPPS->setListsModificationPresentFlag(uiCode);
340
341  READ_UVLC( uiCode, "log2_parallel_merge_level_minus2");
342  pcPPS->setLog2ParallelMergeLevelMinus2 (uiCode);
343
344  READ_FLAG( uiCode, "slice_segment_header_extension_present_flag");
345  pcPPS->setSliceHeaderExtensionPresentFlag(uiCode);
346
347  READ_FLAG( uiCode, "pps_extension_flag");
348  if (uiCode)
349  {
350    while ( xMoreRbspData() )
351    {
352      READ_FLAG( uiCode, "pps_extension_data_flag");
353    }
354  }
355}
356
357Void  TDecCavlc::parseVUI(TComVUI* pcVUI, TComSPS *pcSPS)
358{
359#if ENC_DEC_TRACE
360  fprintf( g_hTrace, "----------- vui_parameters -----------\n");
361#endif
362  UInt  uiCode;
363
364  READ_FLAG(     uiCode, "aspect_ratio_info_present_flag");           pcVUI->setAspectRatioInfoPresentFlag(uiCode);
365  if (pcVUI->getAspectRatioInfoPresentFlag())
366  {
367    READ_CODE(8, uiCode, "aspect_ratio_idc");                         pcVUI->setAspectRatioIdc(uiCode);
368    if (pcVUI->getAspectRatioIdc() == 255)
369    {
370      READ_CODE(16, uiCode, "sar_width");                             pcVUI->setSarWidth(uiCode);
371      READ_CODE(16, uiCode, "sar_height");                            pcVUI->setSarHeight(uiCode);
372    }
373  }
374
375  READ_FLAG(     uiCode, "overscan_info_present_flag");               pcVUI->setOverscanInfoPresentFlag(uiCode);
376  if (pcVUI->getOverscanInfoPresentFlag())
377  {
378    READ_FLAG(   uiCode, "overscan_appropriate_flag");                pcVUI->setOverscanAppropriateFlag(uiCode);
379  }
380
381  READ_FLAG(     uiCode, "video_signal_type_present_flag");           pcVUI->setVideoSignalTypePresentFlag(uiCode);
382  if (pcVUI->getVideoSignalTypePresentFlag())
383  {
384    READ_CODE(3, uiCode, "video_format");                             pcVUI->setVideoFormat(uiCode);
385    READ_FLAG(   uiCode, "video_full_range_flag");                    pcVUI->setVideoFullRangeFlag(uiCode);
386    READ_FLAG(   uiCode, "colour_description_present_flag");          pcVUI->setColourDescriptionPresentFlag(uiCode);
387    if (pcVUI->getColourDescriptionPresentFlag())
388    {
389      READ_CODE(8, uiCode, "colour_primaries");                       pcVUI->setColourPrimaries(uiCode);
390      READ_CODE(8, uiCode, "transfer_characteristics");               pcVUI->setTransferCharacteristics(uiCode);
391      READ_CODE(8, uiCode, "matrix_coefficients");                    pcVUI->setMatrixCoefficients(uiCode);
392    }
393  }
394
395  READ_FLAG(     uiCode, "chroma_loc_info_present_flag");             pcVUI->setChromaLocInfoPresentFlag(uiCode);
396  if (pcVUI->getChromaLocInfoPresentFlag())
397  {
398    READ_UVLC(   uiCode, "chroma_sample_loc_type_top_field" );        pcVUI->setChromaSampleLocTypeTopField(uiCode);
399    READ_UVLC(   uiCode, "chroma_sample_loc_type_bottom_field" );     pcVUI->setChromaSampleLocTypeBottomField(uiCode);
400  }
401
402  READ_FLAG(     uiCode, "neutral_chroma_indication_flag");           pcVUI->setNeutralChromaIndicationFlag(uiCode);
403
404  READ_FLAG(     uiCode, "field_seq_flag");                           pcVUI->setFieldSeqFlag(uiCode);
405
406  READ_FLAG(uiCode, "frame_field_info_present_flag");                 pcVUI->setFrameFieldInfoPresentFlag(uiCode);
407
408  READ_FLAG(     uiCode, "default_display_window_flag");
409  if (uiCode != 0)
410  {
411    Window &defDisp = pcVUI->getDefaultDisplayWindow();
412    READ_UVLC(   uiCode, "def_disp_win_left_offset" );                defDisp.setWindowLeftOffset  ( uiCode * TComSPS::getWinUnitX( pcSPS->getChromaFormatIdc()) );
413    READ_UVLC(   uiCode, "def_disp_win_right_offset" );               defDisp.setWindowRightOffset ( uiCode * TComSPS::getWinUnitX( pcSPS->getChromaFormatIdc()) );
414    READ_UVLC(   uiCode, "def_disp_win_top_offset" );                 defDisp.setWindowTopOffset   ( uiCode * TComSPS::getWinUnitY( pcSPS->getChromaFormatIdc()) );
415    READ_UVLC(   uiCode, "def_disp_win_bottom_offset" );              defDisp.setWindowBottomOffset( uiCode * TComSPS::getWinUnitY( pcSPS->getChromaFormatIdc()) );
416  }
417  TimingInfo *timingInfo = pcVUI->getTimingInfo();
418  READ_FLAG(       uiCode, "vui_timing_info_present_flag");         timingInfo->setTimingInfoPresentFlag      (uiCode ? true : false);
419  if(timingInfo->getTimingInfoPresentFlag())
420  {
421    READ_CODE( 32, uiCode, "vui_num_units_in_tick");                timingInfo->setNumUnitsInTick             (uiCode);
422    READ_CODE( 32, uiCode, "vui_time_scale");                       timingInfo->setTimeScale                  (uiCode);
423    READ_FLAG(     uiCode, "vui_poc_proportional_to_timing_flag");  timingInfo->setPocProportionalToTimingFlag(uiCode ? true : false);
424    if(timingInfo->getPocProportionalToTimingFlag())
425    {
426      READ_UVLC(   uiCode, "vui_num_ticks_poc_diff_one_minus1");    timingInfo->setNumTicksPocDiffOneMinus1   (uiCode);
427    }
428  READ_FLAG(     uiCode, "hrd_parameters_present_flag");              pcVUI->setHrdParametersPresentFlag(uiCode);
429  if( pcVUI->getHrdParametersPresentFlag() )
430  {
431    parseHrdParameters( pcVUI->getHrdParameters(), 1, pcSPS->getMaxTLayers() - 1 );
432  }
433  }
434  READ_FLAG(     uiCode, "bitstream_restriction_flag");               pcVUI->setBitstreamRestrictionFlag(uiCode);
435  if (pcVUI->getBitstreamRestrictionFlag())
436  {
437    READ_FLAG(   uiCode, "tiles_fixed_structure_flag");               pcVUI->setTilesFixedStructureFlag(uiCode);
438#if H_MV
439    if ( pcSPS->getLayerId() > 0 )
440    {
441      READ_FLAG( uiCode, "tile_boundaries_aligned_flag" ); pcVUI->setTileBoundariesAlignedFlag( uiCode == 1 );
442    }
443#endif
444    READ_FLAG(   uiCode, "motion_vectors_over_pic_boundaries_flag");  pcVUI->setMotionVectorsOverPicBoundariesFlag(uiCode);
445    READ_FLAG(   uiCode, "restricted_ref_pic_lists_flag");            pcVUI->setRestrictedRefPicListsFlag(uiCode);
446    READ_UVLC( uiCode, "min_spatial_segmentation_idc");            pcVUI->setMinSpatialSegmentationIdc(uiCode);
447    assert(uiCode < 4096);
448    READ_UVLC(   uiCode, "max_bytes_per_pic_denom" );                 pcVUI->setMaxBytesPerPicDenom(uiCode);
449    READ_UVLC(   uiCode, "max_bits_per_mincu_denom" );                pcVUI->setMaxBitsPerMinCuDenom(uiCode);
450    READ_UVLC(   uiCode, "log2_max_mv_length_horizontal" );           pcVUI->setLog2MaxMvLengthHorizontal(uiCode);
451    READ_UVLC(   uiCode, "log2_max_mv_length_vertical" );             pcVUI->setLog2MaxMvLengthVertical(uiCode);
452  }
453}
454
455Void TDecCavlc::parseHrdParameters(TComHRD *hrd, Bool commonInfPresentFlag, UInt maxNumSubLayersMinus1)
456{
457  UInt  uiCode;
458  if( commonInfPresentFlag )
459  {
460    READ_FLAG( uiCode, "nal_hrd_parameters_present_flag" );           hrd->setNalHrdParametersPresentFlag( uiCode == 1 ? true : false );
461    READ_FLAG( uiCode, "vcl_hrd_parameters_present_flag" );           hrd->setVclHrdParametersPresentFlag( uiCode == 1 ? true : false );
462    if( hrd->getNalHrdParametersPresentFlag() || hrd->getVclHrdParametersPresentFlag() )
463    {
464      READ_FLAG( uiCode, "sub_pic_cpb_params_present_flag" );         hrd->setSubPicCpbParamsPresentFlag( uiCode == 1 ? true : false );
465      if( hrd->getSubPicCpbParamsPresentFlag() )
466      {
467        READ_CODE( 8, uiCode, "tick_divisor_minus2" );                hrd->setTickDivisorMinus2( uiCode );
468        READ_CODE( 5, uiCode, "du_cpb_removal_delay_length_minus1" ); hrd->setDuCpbRemovalDelayLengthMinus1( uiCode );
469        READ_FLAG( uiCode, "sub_pic_cpb_params_in_pic_timing_sei_flag" ); hrd->setSubPicCpbParamsInPicTimingSEIFlag( uiCode == 1 ? true : false );
470        READ_CODE( 5, uiCode, "dpb_output_delay_du_length_minus1"  ); hrd->setDpbOutputDelayDuLengthMinus1( uiCode );
471      }
472      READ_CODE( 4, uiCode, "bit_rate_scale" );                       hrd->setBitRateScale( uiCode );
473      READ_CODE( 4, uiCode, "cpb_size_scale" );                       hrd->setCpbSizeScale( uiCode );
474      if( hrd->getSubPicCpbParamsPresentFlag() )
475      {
476        READ_CODE( 4, uiCode, "cpb_size_du_scale" );                  hrd->setDuCpbSizeScale( uiCode );
477      }
478      READ_CODE( 5, uiCode, "initial_cpb_removal_delay_length_minus1" ); hrd->setInitialCpbRemovalDelayLengthMinus1( uiCode );
479      READ_CODE( 5, uiCode, "au_cpb_removal_delay_length_minus1" );      hrd->setCpbRemovalDelayLengthMinus1( uiCode );
480      READ_CODE( 5, uiCode, "dpb_output_delay_length_minus1" );       hrd->setDpbOutputDelayLengthMinus1( uiCode );
481    }
482  }
483  Int i, j, nalOrVcl;
484  for( i = 0; i <= maxNumSubLayersMinus1; i ++ )
485  {
486    READ_FLAG( uiCode, "fixed_pic_rate_general_flag" );                     hrd->setFixedPicRateFlag( i, uiCode == 1 ? true : false  );
487    if( !hrd->getFixedPicRateFlag( i ) )
488    {
489      READ_FLAG( uiCode, "fixed_pic_rate_within_cvs_flag" );                hrd->setFixedPicRateWithinCvsFlag( i, uiCode == 1 ? true : false  );
490    }
491    else
492    {
493      hrd->setFixedPicRateWithinCvsFlag( i, true );
494    }
495    hrd->setLowDelayHrdFlag( i, 0 ); // Infered to be 0 when not present
496    hrd->setCpbCntMinus1   ( i, 0 ); // Infered to be 0 when not present
497    if( hrd->getFixedPicRateWithinCvsFlag( i ) )
498    {
499      READ_UVLC( uiCode, "elemental_duration_in_tc_minus1" );             hrd->setPicDurationInTcMinus1( i, uiCode );
500    }
501    else
502    {     
503      READ_FLAG( uiCode, "low_delay_hrd_flag" );                      hrd->setLowDelayHrdFlag( i, uiCode == 1 ? true : false  );
504    }
505    if (!hrd->getLowDelayHrdFlag( i ))
506    {
507      READ_UVLC( uiCode, "cpb_cnt_minus1" );                          hrd->setCpbCntMinus1( i, uiCode );     
508    }
509    for( nalOrVcl = 0; nalOrVcl < 2; nalOrVcl ++ )
510    {
511      if( ( ( nalOrVcl == 0 ) && ( hrd->getNalHrdParametersPresentFlag() ) ) ||
512          ( ( nalOrVcl == 1 ) && ( hrd->getVclHrdParametersPresentFlag() ) ) )
513      {
514        for( j = 0; j <= ( hrd->getCpbCntMinus1( i ) ); j ++ )
515        {
516          READ_UVLC( uiCode, "bit_rate_value_minus1" );             hrd->setBitRateValueMinus1( i, j, nalOrVcl, uiCode );
517          READ_UVLC( uiCode, "cpb_size_value_minus1" );             hrd->setCpbSizeValueMinus1( i, j, nalOrVcl, uiCode );
518          if( hrd->getSubPicCpbParamsPresentFlag() )
519          {
520            READ_UVLC( uiCode, "cpb_size_du_value_minus1" );       hrd->setDuCpbSizeValueMinus1( i, j, nalOrVcl, uiCode );
521            READ_UVLC( uiCode, "bit_rate_du_value_minus1" );       hrd->setDuBitRateValueMinus1( i, j, nalOrVcl, uiCode );
522          }
523          READ_FLAG( uiCode, "cbr_flag" );                          hrd->setCbrFlag( i, j, nalOrVcl, uiCode == 1 ? true : false  );
524        }
525      }
526    }
527  }
528}
529
530#if H_3D
531Void TDecCavlc::parseSPS(TComSPS* pcSPS, Int viewIndex, Bool depthFlag )
532#else
533Void TDecCavlc::parseSPS(TComSPS* pcSPS)
534#endif
535{
536#if ENC_DEC_TRACE 
537  xTraceSPSHeader (pcSPS);
538#endif
539
540  UInt  uiCode;
541  READ_CODE( 4,  uiCode, "sps_video_parameter_set_id");          pcSPS->setVPSId        ( uiCode );
542#if H_MV
543  if ( pcSPS->getLayerId() == 0 )
544  {
545#endif
546  READ_CODE( 3,  uiCode, "sps_max_sub_layers_minus1" );          pcSPS->setMaxTLayers   ( uiCode+1 );
547  assert(uiCode <= 6);
548 
549  READ_FLAG( uiCode, "sps_temporal_id_nesting_flag" );               pcSPS->setTemporalIdNestingFlag ( uiCode > 0 ? true : false );
550  if ( pcSPS->getMaxTLayers() == 1 )
551  {
552    // sps_temporal_id_nesting_flag must be 1 when sps_max_sub_layers_minus1 is 0
553    assert( uiCode == 1 );
554  }
555 
556  parsePTL(pcSPS->getPTL(), 1, pcSPS->getMaxTLayers() - 1);
557#if H_MV
558  }
559#endif
560  READ_UVLC(     uiCode, "sps_seq_parameter_set_id" );           pcSPS->setSPSId( uiCode );
561  assert(uiCode <= 15);
562 
563  READ_UVLC(     uiCode, "chroma_format_idc" );                  pcSPS->setChromaFormatIdc( uiCode );
564  assert(uiCode <= 3);
565  // in the first version we only support chroma_format_idc equal to 1 (4:2:0), so separate_colour_plane_flag cannot appear in the bitstream
566  assert (uiCode == 1);
567  if( uiCode == 3 )
568  {
569    READ_FLAG(     uiCode, "separate_colour_plane_flag");        assert(uiCode == 0);
570  }
571
572  READ_UVLC (    uiCode, "pic_width_in_luma_samples" );          pcSPS->setPicWidthInLumaSamples ( uiCode    );
573  READ_UVLC (    uiCode, "pic_height_in_luma_samples" );         pcSPS->setPicHeightInLumaSamples( uiCode    );
574  READ_FLAG(     uiCode, "conformance_window_flag");
575  if (uiCode != 0)
576  {
577    Window &conf = pcSPS->getConformanceWindow();
578    READ_UVLC(   uiCode, "conf_win_left_offset" );               conf.setWindowLeftOffset  ( uiCode * TComSPS::getWinUnitX( pcSPS->getChromaFormatIdc() ) );
579    READ_UVLC(   uiCode, "conf_win_right_offset" );              conf.setWindowRightOffset ( uiCode * TComSPS::getWinUnitX( pcSPS->getChromaFormatIdc() ) );
580    READ_UVLC(   uiCode, "conf_win_top_offset" );                conf.setWindowTopOffset   ( uiCode * TComSPS::getWinUnitY( pcSPS->getChromaFormatIdc() ) );
581    READ_UVLC(   uiCode, "conf_win_bottom_offset" );             conf.setWindowBottomOffset( uiCode * TComSPS::getWinUnitY( pcSPS->getChromaFormatIdc() ) );
582  }
583
584  READ_UVLC(     uiCode, "bit_depth_luma_minus8" );
585  assert(uiCode <= 6);
586  pcSPS->setBitDepthY( uiCode + 8 );
587  pcSPS->setQpBDOffsetY( (Int) (6*uiCode) );
588
589  READ_UVLC( uiCode,    "bit_depth_chroma_minus8" );
590  assert(uiCode <= 6);
591  pcSPS->setBitDepthC( uiCode + 8 );
592  pcSPS->setQpBDOffsetC( (Int) (6*uiCode) );
593
594  READ_UVLC( uiCode,    "log2_max_pic_order_cnt_lsb_minus4" );   pcSPS->setBitsForPOC( 4 + uiCode );
595  assert(uiCode <= 12);
596
597  UInt subLayerOrderingInfoPresentFlag;
598  READ_FLAG(subLayerOrderingInfoPresentFlag, "sps_sub_layer_ordering_info_present_flag");
599 
600  for(UInt i=0; i <= pcSPS->getMaxTLayers()-1; i++)
601  {
602#if H_MV
603    READ_UVLC ( uiCode, "sps_max_dec_pic_buffering_minus1[i]");
604#else
605    READ_UVLC ( uiCode, "sps_max_dec_pic_buffering_minus1");
606#endif
607    pcSPS->setMaxDecPicBuffering( uiCode + 1, i);
608#if H_MV
609    READ_UVLC ( uiCode, "sps_num_reorder_pics[i]" );
610#else
611    READ_UVLC ( uiCode, "sps_num_reorder_pics" );
612#endif
613    pcSPS->setNumReorderPics(uiCode, i);
614#if H_MV
615    READ_UVLC ( uiCode, "sps_max_latency_increase[i]");
616#else
617    READ_UVLC ( uiCode, "sps_max_latency_increase");
618#endif
619    pcSPS->setMaxLatencyIncrease( uiCode, i );
620
621    if (!subLayerOrderingInfoPresentFlag)
622    {
623      for (i++; i <= pcSPS->getMaxTLayers()-1; i++)
624      {
625        pcSPS->setMaxDecPicBuffering(pcSPS->getMaxDecPicBuffering(0), i);
626        pcSPS->setNumReorderPics(pcSPS->getNumReorderPics(0), i);
627        pcSPS->setMaxLatencyIncrease(pcSPS->getMaxLatencyIncrease(0), i);
628      }
629      break;
630    }
631  }
632
633  READ_UVLC( uiCode, "log2_min_coding_block_size_minus3" );
634  Int log2MinCUSize = uiCode + 3;
635  pcSPS->setLog2MinCodingBlockSize(log2MinCUSize);
636  READ_UVLC( uiCode, "log2_diff_max_min_coding_block_size" );
637  pcSPS->setLog2DiffMaxMinCodingBlockSize(uiCode);
638  Int maxCUDepthDelta = uiCode;
639  pcSPS->setMaxCUWidth  ( 1<<(log2MinCUSize + maxCUDepthDelta) ); 
640  pcSPS->setMaxCUHeight ( 1<<(log2MinCUSize + maxCUDepthDelta) );
641  READ_UVLC( uiCode, "log2_min_transform_block_size_minus2" );   pcSPS->setQuadtreeTULog2MinSize( uiCode + 2 );
642
643  READ_UVLC( uiCode, "log2_diff_max_min_transform_block_size" ); pcSPS->setQuadtreeTULog2MaxSize( uiCode + pcSPS->getQuadtreeTULog2MinSize() );
644  pcSPS->setMaxTrSize( 1<<(uiCode + pcSPS->getQuadtreeTULog2MinSize()) );
645
646  READ_UVLC( uiCode, "max_transform_hierarchy_depth_inter" );    pcSPS->setQuadtreeTUMaxDepthInter( uiCode+1 );
647  READ_UVLC( uiCode, "max_transform_hierarchy_depth_intra" );    pcSPS->setQuadtreeTUMaxDepthIntra( uiCode+1 );
648
649  Int addCuDepth = max (0, log2MinCUSize - (Int)pcSPS->getQuadtreeTULog2MinSize() );
650  pcSPS->setMaxCUDepth( maxCUDepthDelta + addCuDepth ); 
651
652  READ_FLAG( uiCode, "scaling_list_enabled_flag" );                 pcSPS->setScalingListFlag ( uiCode );
653  if(pcSPS->getScalingListFlag())
654  {
655    READ_FLAG( uiCode, "sps_scaling_list_data_present_flag" );                 pcSPS->setScalingListPresentFlag ( uiCode );
656    if(pcSPS->getScalingListPresentFlag ())
657    {
658      parseScalingList( pcSPS->getScalingList() );
659    }
660  }
661  READ_FLAG( uiCode, "amp_enabled_flag" );                          pcSPS->setUseAMP( uiCode );
662  READ_FLAG( uiCode, "sample_adaptive_offset_enabled_flag" );       pcSPS->setUseSAO ( uiCode ? true : false );
663
664  READ_FLAG( uiCode, "pcm_enabled_flag" ); pcSPS->setUsePCM( uiCode ? true : false );
665  if( pcSPS->getUsePCM() )
666  {
667    READ_CODE( 4, uiCode, "pcm_sample_bit_depth_luma_minus1" );          pcSPS->setPCMBitDepthLuma   ( 1 + uiCode );
668    READ_CODE( 4, uiCode, "pcm_sample_bit_depth_chroma_minus1" );        pcSPS->setPCMBitDepthChroma ( 1 + uiCode );
669    READ_UVLC( uiCode, "log2_min_pcm_luma_coding_block_size_minus3" );   pcSPS->setPCMLog2MinSize (uiCode+3);
670    READ_UVLC( uiCode, "log2_diff_max_min_pcm_luma_coding_block_size" ); pcSPS->setPCMLog2MaxSize ( uiCode+pcSPS->getPCMLog2MinSize() );
671    READ_FLAG( uiCode, "pcm_loop_filter_disable_flag" );                 pcSPS->setPCMFilterDisableFlag ( uiCode ? true : false );
672  }
673
674  READ_UVLC( uiCode, "num_short_term_ref_pic_sets" );
675  assert(uiCode <= 64);
676  pcSPS->createRPSList(uiCode);
677
678  TComRPSList* rpsList = pcSPS->getRPSList();
679  TComReferencePictureSet* rps;
680
681  for(UInt i=0; i< rpsList->getNumberOfReferencePictureSets(); i++)
682  {
683    rps = rpsList->getReferencePictureSet(i);
684    parseShortTermRefPicSet(pcSPS,rps,i);
685  }
686  READ_FLAG( uiCode, "long_term_ref_pics_present_flag" );          pcSPS->setLongTermRefsPresent(uiCode);
687  if (pcSPS->getLongTermRefsPresent()) 
688  {
689    READ_UVLC( uiCode, "num_long_term_ref_pic_sps" );
690    pcSPS->setNumLongTermRefPicSPS(uiCode);
691    for (UInt k = 0; k < pcSPS->getNumLongTermRefPicSPS(); k++)
692    {
693      READ_CODE( pcSPS->getBitsForPOC(), uiCode, "lt_ref_pic_poc_lsb_sps" );
694      pcSPS->setLtRefPicPocLsbSps(k, uiCode);
695      READ_FLAG( uiCode,  "used_by_curr_pic_lt_sps_flag[i]");
696      pcSPS->setUsedByCurrPicLtSPSFlag(k, uiCode?1:0);
697    }
698  }
699  READ_FLAG( uiCode, "sps_temporal_mvp_enable_flag" );            pcSPS->setTMVPFlagsPresent(uiCode);
700
701  READ_FLAG( uiCode, "sps_strong_intra_smoothing_enable_flag" );  pcSPS->setUseStrongIntraSmoothing(uiCode);
702
703  READ_FLAG( uiCode, "vui_parameters_present_flag" );             pcSPS->setVuiParametersPresentFlag(uiCode);
704
705  if (pcSPS->getVuiParametersPresentFlag())
706  {
707    parseVUI(pcSPS->getVuiParameters(), pcSPS);
708  }
709
710  READ_FLAG( uiCode, "sps_extension_flag");
711  if (uiCode)
712  {
713#if !H_MV
714    while ( xMoreRbspData() )
715    {
716      READ_FLAG( uiCode, "sps_extension_data_flag");
717    }
718#else
719    READ_FLAG( uiCode, "inter_view_mv_vert_constraint_flag" );    pcSPS->setInterViewMvVertConstraintFlag(uiCode == 1 ? true : false);
720    ////   sps_extension_vui_parameters( )
721    if( pcSPS->getVuiParameters()->getBitstreamRestrictionFlag() )
722    { 
723      READ_UVLC( uiCode, "num_ilp_restricted_ref_layers" ); pcSPS->setNumIlpRestrictedRefLayers( uiCode ); 
724      for( Int i = 0; i < pcSPS->getNumIlpRestrictedRefLayers( ); i++ ) 
725      { 
726        READ_UVLC( uiCode, "min_spatial_segment_offset_plus1" ); pcSPS->setMinSpatialSegmentOffsetPlus1( i, uiCode ); 
727        if( pcSPS->getMinSpatialSegmentOffsetPlus1( i ) > 0 ) 
728        { 
729          READ_FLAG( uiCode, "ctu_based_offset_enabled_flag[ i ]"); pcSPS->setCtuBasedOffsetEnabledFlag(i, uiCode == 1 ); 
730          if( pcSPS->getCtuBasedOffsetEnabledFlag( i ) ) 
731          {
732            READ_UVLC( uiCode, "min_horizontal_ctu_offset_plus1[ i ]"); pcSPS->setMinHorizontalCtuOffsetPlus1( i, uiCode ); 
733          }
734        } 
735      } 
736    }
737
738#if H_3D_QTLPC
739    if( depthFlag )
740    {
741      READ_FLAG( uiCode, "use_qtl_flag" );
742      pcSPS->setUseQTL( uiCode );
743      READ_FLAG( uiCode, "use_pc_flag" );
744      pcSPS->setUsePC( uiCode );
745    }
746#endif
747
748    ////   sps_extension_vui_parameters( ) END
749    READ_UVLC( uiCode, "sps_shvc_reserved_zero_idc" ); 
750    READ_FLAG( uiCode, "sps_extension2_flag");
751    if ( uiCode )
752    {
753#if !H_3D
754      while ( xMoreRbspData() )
755      {
756        READ_FLAG( uiCode, "sps_extension_data_flag");
757      }
758#else
759     
760      UInt uiCamParPrecision = 0; 
761      Bool bCamParSlice      = false; 
762      if ( !depthFlag )
763      {     
764        READ_UVLC( uiCamParPrecision, "cp_precision" );
765        READ_FLAG( uiCode, "cp_in_slice_header_flag" );    bCamParSlice = ( uiCode == 1 );
766        if( !bCamParSlice )
767        {       
768          for( UInt uiBaseIndex = 0; uiBaseIndex < viewIndex; uiBaseIndex++ )
769          {
770            Int iCode; 
771            READ_SVLC( iCode, "cp_scale" );                m_aaiTempScale  [ uiBaseIndex ][ viewIndex ]   = iCode;
772            READ_SVLC( iCode, "cp_off" );                  m_aaiTempOffset [ uiBaseIndex ][ viewIndex ]   = iCode;
773            READ_SVLC( iCode, "cp_inv_scale_plus_scale" ); m_aaiTempScale  [ viewIndex   ][ uiBaseIndex ] = iCode - m_aaiTempScale [ uiBaseIndex ][ viewIndex ];
774            READ_SVLC( iCode, "cp_inv_off_plus_off" );     m_aaiTempOffset [ viewIndex   ][ uiBaseIndex ] = iCode - m_aaiTempOffset[ uiBaseIndex ][ viewIndex ];
775          }
776        }
777      }
778      pcSPS->initCamParaSPS( viewIndex, uiCamParPrecision, bCamParSlice, m_aaiTempScale, m_aaiTempOffset );
779      READ_FLAG( uiCode, "sps_extension3_flag");
780      if ( uiCode )
781      {
782        while ( xMoreRbspData() )
783        {
784          READ_FLAG( uiCode, "sps_extension_data_flag");
785        }
786      }
787#endif // !H_3D
788    }
789#endif // !H_MV
790  }
791}
792
793Void TDecCavlc::parseVPS(TComVPS* pcVPS)
794{
795  UInt  uiCode;
796 
797  READ_CODE( 4,  uiCode,  "vps_video_parameter_set_id" );         pcVPS->setVPSId( uiCode );
798  READ_CODE( 2,  uiCode,  "vps_reserved_three_2bits" );           assert(uiCode == 3);
799#if H_MV
800  READ_CODE( 6,  uiCode,  "vps_max_layers_minus1" );              pcVPS->setMaxLayers( uiCode + 1 );
801#else
802  READ_CODE( 6,  uiCode,  "vps_reserved_zero_6bits" );            assert(uiCode == 0);
803#endif
804  READ_CODE( 3,  uiCode,  "vps_max_sub_layers_minus1" );          pcVPS->setMaxTLayers( uiCode + 1 );
805  READ_FLAG(     uiCode,  "vps_temporal_id_nesting_flag" );       pcVPS->setTemporalNestingFlag( uiCode ? true:false );
806  assert (pcVPS->getMaxTLayers()>1||pcVPS->getTemporalNestingFlag());
807#if H_MV
808  READ_CODE( 16, uiCode,  "vps_extension_offset" );               
809#else
810  READ_CODE( 16, uiCode,  "vps_reserved_ffff_16bits" );           assert(uiCode == 0xffff);
811#endif
812  parsePTL ( pcVPS->getPTL(), true, pcVPS->getMaxTLayers()-1);
813  UInt subLayerOrderingInfoPresentFlag;
814  READ_FLAG(subLayerOrderingInfoPresentFlag, "vps_sub_layer_ordering_info_present_flag");
815  for(UInt i = 0; i <= pcVPS->getMaxTLayers()-1; i++)
816  {
817    READ_UVLC( uiCode,  "vps_max_dec_pic_buffering_minus1[i]" );     pcVPS->setMaxDecPicBuffering( uiCode + 1, i );
818    READ_UVLC( uiCode,  "vps_num_reorder_pics[i]" );          pcVPS->setNumReorderPics( uiCode, i );
819    READ_UVLC( uiCode,  "vps_max_latency_increase_plus1[i]" );      pcVPS->setMaxLatencyIncrease( uiCode, i );
820
821    if (!subLayerOrderingInfoPresentFlag)
822    {
823      for (i++; i <= pcVPS->getMaxTLayers()-1; i++)
824      {
825        pcVPS->setMaxDecPicBuffering(pcVPS->getMaxDecPicBuffering(0), i);
826        pcVPS->setNumReorderPics(pcVPS->getNumReorderPics(0), i);
827        pcVPS->setMaxLatencyIncrease(pcVPS->getMaxLatencyIncrease(0), i);
828      }
829      break;
830    }
831  }
832
833  assert( pcVPS->getNumHrdParameters() < MAX_VPS_OP_SETS_PLUS1 );
834#if H_MV
835  assert( pcVPS->getVpsMaxLayerId() < MAX_VPS_NUH_LAYER_ID_PLUS1 );
836  READ_CODE( 6, uiCode, "vps_max_layer_id" );   pcVPS->setVpsMaxLayerId( uiCode );
837
838  READ_UVLC(    uiCode, "vps_max_num_layer_sets_minus1" );               pcVPS->setVpsNumLayerSetsMinus1( uiCode );
839  for( UInt opsIdx = 1; opsIdx <= pcVPS->getVpsNumLayerSetsMinus1(); opsIdx ++ )
840  {
841    for( UInt i = 0; i <= pcVPS->getVpsMaxLayerId(); i ++ )
842#else
843  assert( pcVPS->getMaxNuhReservedZeroLayerId() < MAX_VPS_NUH_RESERVED_ZERO_LAYER_ID_PLUS1 );
844  READ_CODE( 6, uiCode, "vps_max_nuh_reserved_zero_layer_id" );   pcVPS->setMaxNuhReservedZeroLayerId( uiCode );
845  READ_UVLC(    uiCode, "vps_max_op_sets_minus1" );               pcVPS->setMaxOpSets( uiCode + 1 );
846  for( UInt opsIdx = 1; opsIdx <= ( pcVPS->getMaxOpSets() - 1 ); opsIdx ++ )
847  {
848    // Operation point set
849    for( UInt i = 0; i <= pcVPS->getMaxNuhReservedZeroLayerId(); i ++ )
850#endif
851    {
852      READ_FLAG( uiCode, "layer_id_included_flag[opsIdx][i]" );     pcVPS->setLayerIdIncludedFlag( uiCode == 1 ? true : false, opsIdx, i );
853    }
854  }
855  TimingInfo *timingInfo = pcVPS->getTimingInfo();
856  READ_FLAG(       uiCode, "vps_timing_info_present_flag");         timingInfo->setTimingInfoPresentFlag      (uiCode ? true : false);
857  if(timingInfo->getTimingInfoPresentFlag())
858  {
859    READ_CODE( 32, uiCode, "vps_num_units_in_tick");                timingInfo->setNumUnitsInTick             (uiCode);
860    READ_CODE( 32, uiCode, "vps_time_scale");                       timingInfo->setTimeScale                  (uiCode);
861    READ_FLAG(     uiCode, "vps_poc_proportional_to_timing_flag");  timingInfo->setPocProportionalToTimingFlag(uiCode ? true : false);
862    if(timingInfo->getPocProportionalToTimingFlag())
863    {
864      READ_UVLC(   uiCode, "vps_num_ticks_poc_diff_one_minus1");    timingInfo->setNumTicksPocDiffOneMinus1   (uiCode);
865    }
866    READ_UVLC( uiCode, "vps_num_hrd_parameters" );                  pcVPS->setNumHrdParameters( uiCode );
867
868    if( pcVPS->getNumHrdParameters() > 0 )
869    {
870      pcVPS->createHrdParamBuffer();
871    }
872    for( UInt i = 0; i < pcVPS->getNumHrdParameters(); i ++ )
873    {
874      READ_UVLC( uiCode, "hrd_op_set_idx" );                       pcVPS->setHrdOpSetIdx( uiCode, i );
875      if( i > 0 )
876      {
877        READ_FLAG( uiCode, "cprms_present_flag[i]" );              pcVPS->setCprmsPresentFlag( uiCode == 1 ? true : false, i );
878      }
879      parseHrdParameters(pcVPS->getHrdParameters(i), pcVPS->getCprmsPresentFlag( i ), pcVPS->getMaxTLayers() - 1);
880    }
881  }
882  READ_FLAG( uiCode,  "vps_extension_flag" );
883  if (uiCode)
884  {
885#if H_MV
886    m_pcBitstream->readOutTrailingBits();
887
888    READ_FLAG( uiCode, "avc_base_layer_flag" );                     pcVPS->setAvcBaseLayerFlag( uiCode == 1 ? true : false );
889    READ_FLAG( uiCode, "splitting_flag" );                          pcVPS->setSplittingFlag( uiCode == 1 ? true : false );
890
891    for( Int sIdx = 0; sIdx < MAX_NUM_SCALABILITY_TYPES; sIdx++ )
892    {
893      READ_FLAG( uiCode,  "scalability_mask[i]" );                  pcVPS->setScalabilityMask( sIdx, uiCode == 1 ? true : false );     
894    }
895
896    for( Int sIdx = 0; sIdx < pcVPS->getNumScalabilityTypes( ) - ( pcVPS->getSplittingFlag() ? 1 : 0 ); sIdx++ )
897    {
898        READ_CODE( 3, uiCode, "dimension_id_len_minus1[j]" );       pcVPS->setDimensionIdLen( sIdx, uiCode + 1 );
899    }
900
901    if ( pcVPS->getSplittingFlag() )
902      {
903      pcVPS->setDimensionIdLen( pcVPS->getNumScalabilityTypes( ) - 1, pcVPS->inferLastDimsionIdLenMinus1() );       
904      }
905
906    READ_FLAG( uiCode, "vps_nuh_layer_id_present_flag" );           pcVPS->setVpsNuhLayerIdPresentFlag( uiCode == 1 ? true : false );
907
908    for( Int i = 0; i <= pcVPS->getMaxLayers() - 1; i++ )
909    {
910      if ( pcVPS->getVpsNuhLayerIdPresentFlag() && ( i > 0 ) )
911      {
912        READ_CODE( 6, uiCode, "layer_id_in_nuh[i]" );                pcVPS->setLayerIdInNuh( i, uiCode );
913      }
914      else
915      {
916        pcVPS->setLayerIdInNuh( i, i );; 
917    }
918
919      pcVPS->setLayerIdInVps( pcVPS->getLayerIdInNuh( i ), i ); 
920   
921      for( Int j = 0; j < pcVPS->getNumScalabilityTypes() ; j++ ) 
922    {
923        if ( !pcVPS->getSplittingFlag() )
924      {
925          READ_CODE( pcVPS->getDimensionIdLen( j ), uiCode, "dimension_id[i][j]" );  pcVPS->setDimensionId( i, j, uiCode );
926        }
927        else
928        {
929          pcVPS->setDimensionId( i, j, pcVPS->inferDimensionId( i, j)  );
930        }
931      }
932    }
933
934
935    for( Int i = 1; i <= pcVPS->getMaxLayers() - 1; i++ )
936    {
937      for( Int j = 0; j < i; j++ )
938      {
939        READ_FLAG( uiCode, "direct_dependency_flag[i][j]" );             pcVPS->setDirectDependencyFlag( i, j, uiCode );
940      }
941    }
942
943    for( Int i = 0; i < pcVPS->getMaxLayers() - 1; i++ )
944    {
945      READ_CODE( 3, uiCode,       "max_tid_il_ref_pics_plus1[i]" );      pcVPS->setMaxTidIlRefPicPlus1( i , uiCode ); 
946    }
947
948    READ_CODE( 10, uiCode, "vps_number_layer_sets_minus1"      );  pcVPS->setVpsNumberLayerSetsMinus1    ( uiCode ); 
949    READ_CODE( 6,  uiCode, "vps_num_profile_tier_level_minus1" );  pcVPS->setVpsNumProfileTierLevelMinus1( uiCode );
950
951    for( Int i = 1; i <= pcVPS->getVpsNumProfileTierLevelMinus1(); i++ )
952    {
953      READ_FLAG(  uiCode, "vps_profile_present_flag[i]" );    pcVPS->setVpsProfilePresentFlag( i, uiCode == 1 );
954      if( !pcVPS->getVpsProfilePresentFlag( i ) )
955      {
956        READ_CODE( 6, uiCode, "profile_ref_minus1[i]" ); pcVPS->setProfileRefMinus1( i, uiCode );
957      }
958      parsePTL ( pcVPS->getPTL( i ), pcVPS->getVpsProfilePresentFlag( i ), pcVPS->getMaxTLayers()-1);
959      if( !pcVPS->getVpsProfilePresentFlag( i ) )
960      {
961        TComPTL temp = *pcVPS->getPTL( i );
962        *pcVPS->getPTL( i ) = *pcVPS->getPTL( pcVPS->getProfileRefMinus1( i ) + 1 );
963        pcVPS->getPTL( i )->copyLevelFrom( &temp );
964      }
965    }
966
967    Int numOutputLayerSets = pcVPS->getVpsNumberLayerSetsMinus1( ) + 1; 
968
969    READ_FLAG( uiCode, "more_output_layer_sets_than_default_flag" ); pcVPS->setMoreOutputLayerSetsThanDefaultFlag( uiCode == 1 );
970
971    if ( pcVPS->getMoreOutputLayerSetsThanDefaultFlag( ) )
972    {
973      READ_CODE( 10, uiCode, "num_add_output_layer_sets_minus1"      ); pcVPS->setNumAddOutputLayerSetsMinus1( uiCode );
974      numOutputLayerSets += ( pcVPS->getNumAddOutputLayerSetsMinus1( ) + 1); 
975    }
976
977    if( numOutputLayerSets > 1)
978    {
979      READ_FLAG( uiCode, "default_one_target_output_layer_flag" ); pcVPS->setDefaultOneTargetOutputLayerFlag(  uiCode == 1); 
980    } 
981
982    for( Int i = 1; i < numOutputLayerSets; i++ )
983    {
984      if( i > pcVPS->getVpsNumberLayerSetsMinus1( ) )
985      {       
986        READ_UVLC( uiCode,      "output_layer_set_idx_minus1[i]" ); pcVPS->setOutputLayerSetIdxMinus1( i, uiCode ); 
987        for( Int j = 0; j < pcVPS->getNumLayersInIdList( j ) - 1; j++ )
988        {
989          READ_FLAG( uiCode, "output_layer_flag" ); pcVPS->setOutputLayerFlag( i, j, uiCode == 1 ); 
990        }       
991      }
992      if ( pcVPS->getProfileLevelTierIdxLen()  > 0 )
993      {     
994        READ_CODE( pcVPS->getProfileLevelTierIdxLen(), uiCode,"profile_level_tier_idx[ i ]" );   pcVPS->setProfileLevelTierIdx( i , uiCode ); 
995      }
996    }
997
998    READ_FLAG( uiCode , "max_one_active_ref_layer_flag" ); pcVPS->setMaxOneActiveRefLayerFlag( uiCode == 1 ); 
999    READ_UVLC( uiCode,  "direct_dep_type_len_minus2"); pcVPS->setDirectDepTypeLenMinus2 ( uiCode ); 
1000
1001    for( Int i = 1; i <= pcVPS->getMaxLayers() - 1; i++ )
1002    {
1003      for( Int j = 0; j < i; j++ )
1004      {
1005        if (pcVPS->getDirectDependencyFlag( i, j) )
1006        {       
1007          READ_CODE( pcVPS->getDirectDepTypeLenMinus2( ) + 2,  uiCode, "direct_dependency_type[i][j]" ); pcVPS->setDirectDependencyType( i, j , uiCode);
1008        }
1009      }
1010    }
1011
1012    READ_FLAG ( uiCode,                    "vps_shvc_reserved_zero_flag" ); 
1013
1014#if H_3D   
1015    READ_FLAG( uiCode,  "vps_extension2_flag" );
1016    if (uiCode)
1017    {
1018      m_pcBitstream->readOutTrailingBits();
1019
1020      for( Int i = 0; i <= pcVPS->getMaxLayers() - 1; i++ )
1021      {
1022
1023#if H_3D_ARP
1024        pcVPS->setUseAdvRP  ( i, 0 );
1025        pcVPS->setARPStepNum( i, 1 );
1026#endif 
1027        if ( i != 0 )
1028        {
1029          if( !( pcVPS->getDepthId( i ) == 1 ) )
1030          {
1031#if H_3D_IV_MERGE
1032                READ_FLAG( uiCode, "iv_mv_pred_flag[i]");          pcVPS->setIvMvPredFlag         ( i, uiCode == 1 ? true : false );
1033#endif
1034#if H_3D_ARP
1035                READ_FLAG( uiCode, "iv_res_pred_flag[i]"  );  pcVPS->setUseAdvRP  ( i, uiCode ); pcVPS->setARPStepNum( i, uiCode ? H_3D_ARP_WFNR : 1 );
1036
1037#endif
1038#if H_3D_NBDV_REF
1039                READ_FLAG( uiCode, "depth_refinement_flag[i]");    pcVPS->setDepthRefinementFlag  ( i, uiCode == 1 ? true : false );
1040#endif
1041#if H_3D_VSP
1042                READ_FLAG( uiCode, "view_synthesis_pred_flag[i]"); pcVPS->setViewSynthesisPredFlag( i, uiCode == 1 ? true : false );
1043#endif
1044          }
1045          else
1046          {
1047
1048            READ_FLAG( uiCode, "vps_depth_modes_flag[i]" );             pcVPS->setVpsDepthModesFlag( i, uiCode == 1 ? true : false );
1049            //          READ_FLAG( uiCode, "lim_qt_pred_flag[i]");                  pcVPS->setLimQtPreFlag     ( i, uiCode == 1 ? true : false );
1050#if H_3D_DIM_DLT
1051            if( pcVPS->getVpsDepthModesFlag( i ) )
1052            {
1053              READ_FLAG( uiCode, "dlt_flag[i]" );                       pcVPS->setUseDLTFlag( i, uiCode == 1 ? true : false );
1054            }
1055            if( pcVPS->getUseDLTFlag( i ) )
1056            {
1057              // decode mapping
1058              UInt uiNumDepthValues;
1059              // parse number of values in DLT
1060              READ_UVLC(uiNumDepthValues, "num_depth_values_in_dlt[i]");
1061
1062              // parse actual DLT values
1063              Int* aiIdx2DepthValue = (Int*) calloc(uiNumDepthValues, sizeof(Int));
1064              for(Int d=0; d<uiNumDepthValues; d++)
1065              {
1066                READ_UVLC(uiCode, "dlt_depth_value[i][d]");
1067                aiIdx2DepthValue[d] = (Int)uiCode;
1068              }
1069
1070              pcVPS->setDepthLUTs(i, aiIdx2DepthValue, uiNumDepthValues);
1071
1072              // clean memory
1073              free(aiIdx2DepthValue);
1074            }
1075#endif
1076          }
1077        }
1078      }
1079      READ_FLAG( uiCode, "iv_mv_scaling_flag");                       pcVPS->setIvMvScalingFlag( uiCode == 1 ? true : false ); 
1080    }
1081#endif
1082    pcVPS->checkVPSExtensionSyntax(); 
1083
1084    pcVPS->setRefLayers(); 
1085
1086#else
1087    while ( xMoreRbspData() )
1088    {
1089      READ_FLAG( uiCode, "vps_extension_data_flag");
1090    }
1091#endif   
1092  }
1093
1094#if H_3D
1095  pcVPS->initViewIndex(); 
1096#endif
1097  return;
1098}
1099
1100Void TDecCavlc::parseSliceHeader (TComSlice*& rpcSlice, ParameterSetManagerDecoder *parameterSetManager)
1101{
1102  UInt  uiCode;
1103  Int   iCode;
1104
1105#if ENC_DEC_TRACE
1106  xTraceSliceHeader(rpcSlice);
1107#endif
1108  TComPPS* pps = NULL;
1109  TComSPS* sps = NULL;
1110#if H_MV
1111  TComVPS* vps = NULL;
1112#endif
1113
1114  UInt firstSliceSegmentInPic;
1115  READ_FLAG( firstSliceSegmentInPic, "first_slice_segment_in_pic_flag" );
1116  if( rpcSlice->getRapPicFlag())
1117  { 
1118    READ_FLAG( uiCode, "no_output_of_prior_pics_flag" );  //ignored
1119  }
1120  READ_UVLC (    uiCode, "slice_pic_parameter_set_id" );  rpcSlice->setPPSId(uiCode);
1121  pps = parameterSetManager->getPrefetchedPPS(uiCode);
1122  //!KS: need to add error handling code here, if PPS is not available
1123  assert(pps!=0);
1124  sps = parameterSetManager->getPrefetchedSPS(pps->getSPSId());
1125  //!KS: need to add error handling code here, if SPS is not available
1126  assert(sps!=0);
1127#if H_MV
1128  vps = parameterSetManager->getPrefetchedVPS(sps->getVPSId());
1129  assert(vps!=0);
1130  rpcSlice->setVPS(vps);     
1131  rpcSlice->setViewId   ( vps->getViewId   ( rpcSlice->getLayerIdInVps() )      );
1132#if H_3D 
1133  rpcSlice->setViewIndex( vps->getViewIndex( rpcSlice->getLayerIdInVps() )      ); 
1134  rpcSlice->setIsDepth  ( vps->getDepthId  ( rpcSlice->getLayerIdInVps() ) == 1 );
1135#endif
1136#endif
1137  rpcSlice->setSPS(sps);
1138  rpcSlice->setPPS(pps);
1139  if( pps->getDependentSliceSegmentsEnabledFlag() && ( !firstSliceSegmentInPic ))
1140  {
1141    READ_FLAG( uiCode, "dependent_slice_segment_flag" );       rpcSlice->setDependentSliceSegmentFlag(uiCode ? true : false);
1142  }
1143  else
1144  {
1145    rpcSlice->setDependentSliceSegmentFlag(false);
1146  }
1147  Int numCTUs = ((sps->getPicWidthInLumaSamples()+sps->getMaxCUWidth()-1)/sps->getMaxCUWidth())*((sps->getPicHeightInLumaSamples()+sps->getMaxCUHeight()-1)/sps->getMaxCUHeight());
1148  Int maxParts = (1<<(sps->getMaxCUDepth()<<1));
1149  UInt sliceSegmentAddress = 0;
1150  Int bitsSliceSegmentAddress = 0;
1151  while(numCTUs>(1<<bitsSliceSegmentAddress))
1152  {
1153    bitsSliceSegmentAddress++;
1154  }
1155
1156  if(!firstSliceSegmentInPic)
1157  {
1158    READ_CODE( bitsSliceSegmentAddress, sliceSegmentAddress, "slice_segment_address" );
1159  }
1160  //set uiCode to equal slice start address (or dependent slice start address)
1161  Int startCuAddress = maxParts*sliceSegmentAddress;
1162  rpcSlice->setSliceSegmentCurStartCUAddr( startCuAddress );
1163  rpcSlice->setSliceSegmentCurEndCUAddr(numCTUs*maxParts);
1164
1165  if (rpcSlice->getDependentSliceSegmentFlag())
1166  {
1167    rpcSlice->setNextSlice          ( false );
1168    rpcSlice->setNextSliceSegment ( true  );
1169  }
1170  else
1171  {
1172    rpcSlice->setNextSlice          ( true  );
1173    rpcSlice->setNextSliceSegment ( false );
1174
1175    rpcSlice->setSliceCurStartCUAddr(startCuAddress);
1176    rpcSlice->setSliceCurEndCUAddr(numCTUs*maxParts);
1177  }
1178 
1179  if(!rpcSlice->getDependentSliceSegmentFlag())
1180  {
1181#if H_MV   
1182    if ( rpcSlice->getPPS()->getNumExtraSliceHeaderBits() > 0 )
1183    {
1184      READ_FLAG( uiCode, "discardable_flag" ); rpcSlice->setDiscardableFlag( uiCode == 1 );
1185    }
1186
1187    for (Int i = 1; i < rpcSlice->getPPS()->getNumExtraSliceHeaderBits(); i++)   
1188#else
1189    for (Int i = 0; i < rpcSlice->getPPS()->getNumExtraSliceHeaderBits(); i++)
1190#endif     
1191    {
1192      READ_FLAG(uiCode, "slice_reserved_undetermined_flag[]"); // ignored
1193    }
1194
1195    READ_UVLC (    uiCode, "slice_type" );            rpcSlice->setSliceType((SliceType)uiCode);
1196    if( pps->getOutputFlagPresentFlag() )
1197    {
1198      READ_FLAG( uiCode, "pic_output_flag" );    rpcSlice->setPicOutputFlag( uiCode ? true : false );
1199    }
1200    else
1201    {
1202      rpcSlice->setPicOutputFlag( true );
1203    }
1204    // in the first version chroma_format_idc is equal to one, thus colour_plane_id will not be present
1205    assert (sps->getChromaFormatIdc() == 1 );
1206    // if( separate_colour_plane_flag  ==  1 )
1207    //   colour_plane_id                                      u(2)
1208
1209    if( rpcSlice->getIdrPicFlag() )
1210    {
1211      rpcSlice->setPOC(0);
1212      TComReferencePictureSet* rps = rpcSlice->getLocalRPS();
1213      rps->setNumberOfNegativePictures(0);
1214      rps->setNumberOfPositivePictures(0);
1215      rps->setNumberOfLongtermPictures(0);
1216      rps->setNumberOfPictures(0);
1217      rpcSlice->setRPS(rps);
1218#if H_MV
1219      rpcSlice->setEnableTMVPFlag(false);
1220#endif
1221    }
1222    else
1223    {
1224      READ_CODE(sps->getBitsForPOC(), uiCode, "pic_order_cnt_lsb"); 
1225      Int iPOClsb = uiCode;
1226      Int iPrevPOC = rpcSlice->getPrevPOC();
1227      Int iMaxPOClsb = 1<< sps->getBitsForPOC();
1228      Int iPrevPOClsb = iPrevPOC%iMaxPOClsb;
1229      Int iPrevPOCmsb = iPrevPOC-iPrevPOClsb;
1230      Int iPOCmsb;
1231      if( ( iPOClsb  <  iPrevPOClsb ) && ( ( iPrevPOClsb - iPOClsb )  >=  ( iMaxPOClsb / 2 ) ) )
1232      {
1233        iPOCmsb = iPrevPOCmsb + iMaxPOClsb;
1234      }
1235      else if( (iPOClsb  >  iPrevPOClsb )  && ( (iPOClsb - iPrevPOClsb )  >  ( iMaxPOClsb / 2 ) ) ) 
1236      {
1237        iPOCmsb = iPrevPOCmsb - iMaxPOClsb;
1238      }
1239      else
1240      {
1241        iPOCmsb = iPrevPOCmsb;
1242      }
1243      if ( rpcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP
1244        || rpcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL
1245        || rpcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP )
1246      {
1247        // For BLA picture types, POCmsb is set to 0.
1248        iPOCmsb = 0;
1249      }
1250      rpcSlice->setPOC              (iPOCmsb+iPOClsb);
1251
1252      TComReferencePictureSet* rps;
1253      rps = rpcSlice->getLocalRPS();
1254      rpcSlice->setRPS(rps);
1255      READ_FLAG( uiCode, "short_term_ref_pic_set_sps_flag" );
1256      if(uiCode == 0) // use short-term reference picture set explicitly signalled in slice header
1257      {
1258        parseShortTermRefPicSet(sps,rps, sps->getRPSList()->getNumberOfReferencePictureSets());
1259      }
1260      else // use reference to short-term reference picture set in PPS
1261      {
1262        Int numBits = 0;
1263        while ((1 << numBits) < rpcSlice->getSPS()->getRPSList()->getNumberOfReferencePictureSets())
1264        {
1265          numBits++;
1266        }
1267        if (numBits > 0)
1268        {
1269          READ_CODE( numBits, uiCode, "short_term_ref_pic_set_idx");
1270        }
1271        else
1272        {
1273          uiCode = 0;
1274        }
1275        *rps = *(sps->getRPSList()->getReferencePictureSet(uiCode));
1276      }
1277      if(sps->getLongTermRefsPresent())
1278      {
1279        Int offset = rps->getNumberOfNegativePictures()+rps->getNumberOfPositivePictures();
1280        UInt numOfLtrp = 0;
1281        UInt numLtrpInSPS = 0;
1282        if (rpcSlice->getSPS()->getNumLongTermRefPicSPS() > 0)
1283        {
1284          READ_UVLC( uiCode, "num_long_term_sps");
1285          numLtrpInSPS = uiCode;
1286          numOfLtrp += numLtrpInSPS;
1287          rps->setNumberOfLongtermPictures(numOfLtrp);
1288        }
1289        Int bitsForLtrpInSPS = 0;
1290        while (rpcSlice->getSPS()->getNumLongTermRefPicSPS() > (1 << bitsForLtrpInSPS))
1291        {
1292          bitsForLtrpInSPS++;
1293        }
1294        READ_UVLC( uiCode, "num_long_term_pics");             rps->setNumberOfLongtermPictures(uiCode);
1295        numOfLtrp += uiCode;
1296        rps->setNumberOfLongtermPictures(numOfLtrp);
1297        Int maxPicOrderCntLSB = 1 << rpcSlice->getSPS()->getBitsForPOC();
1298        Int prevDeltaMSB = 0, deltaPocMSBCycleLT = 0;;
1299        for(Int j=offset+rps->getNumberOfLongtermPictures()-1, k = 0; k < numOfLtrp; j--, k++)
1300        {
1301          Int pocLsbLt;
1302          if (k < numLtrpInSPS)
1303          {
1304            uiCode = 0;
1305            if (bitsForLtrpInSPS > 0)
1306            {
1307              READ_CODE(bitsForLtrpInSPS, uiCode, "lt_idx_sps[i]");
1308            }
1309            Int usedByCurrFromSPS=rpcSlice->getSPS()->getUsedByCurrPicLtSPSFlag(uiCode);
1310
1311            pocLsbLt = rpcSlice->getSPS()->getLtRefPicPocLsbSps(uiCode);
1312            rps->setUsed(j,usedByCurrFromSPS);
1313          }
1314          else
1315          {
1316            READ_CODE(rpcSlice->getSPS()->getBitsForPOC(), uiCode, "poc_lsb_lt"); pocLsbLt= uiCode;
1317            READ_FLAG( uiCode, "used_by_curr_pic_lt_flag");     rps->setUsed(j,uiCode);
1318          }
1319          READ_FLAG(uiCode,"delta_poc_msb_present_flag");
1320          Bool mSBPresentFlag = uiCode ? true : false;
1321          if(mSBPresentFlag)                 
1322          {
1323            READ_UVLC( uiCode, "delta_poc_msb_cycle_lt[i]" );
1324            Bool deltaFlag = false;
1325            //            First LTRP                               || First LTRP from SH
1326            if( (j == offset+rps->getNumberOfLongtermPictures()-1) || (j == offset+(numOfLtrp-numLtrpInSPS)-1) )
1327            {
1328              deltaFlag = true;
1329            }
1330            if(deltaFlag)
1331            {
1332              deltaPocMSBCycleLT = uiCode;
1333            }
1334            else
1335            {
1336              deltaPocMSBCycleLT = uiCode + prevDeltaMSB;             
1337            }
1338
1339            Int pocLTCurr = rpcSlice->getPOC() - deltaPocMSBCycleLT * maxPicOrderCntLSB
1340                                        - iPOClsb + pocLsbLt;
1341            rps->setPOC     (j, pocLTCurr); 
1342            rps->setDeltaPOC(j, - rpcSlice->getPOC() + pocLTCurr);
1343            rps->setCheckLTMSBPresent(j,true); 
1344          }
1345          else
1346          {
1347            rps->setPOC     (j, pocLsbLt);
1348            rps->setDeltaPOC(j, - rpcSlice->getPOC() + pocLsbLt);
1349            rps->setCheckLTMSBPresent(j,false); 
1350          }
1351          prevDeltaMSB = deltaPocMSBCycleLT;
1352        }
1353        offset += rps->getNumberOfLongtermPictures();
1354        rps->setNumberOfPictures(offset);       
1355      } 
1356      if ( rpcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP
1357        || rpcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL
1358        || rpcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP )
1359      {
1360        // In the case of BLA picture types, rps data is read from slice header but ignored
1361        rps = rpcSlice->getLocalRPS();
1362        rps->setNumberOfNegativePictures(0);
1363        rps->setNumberOfPositivePictures(0);
1364        rps->setNumberOfLongtermPictures(0);
1365        rps->setNumberOfPictures(0);
1366        rpcSlice->setRPS(rps);
1367      }
1368      if (rpcSlice->getSPS()->getTMVPFlagsPresent())
1369      {
1370        READ_FLAG( uiCode, "slice_temporal_mvp_enable_flag" );
1371        rpcSlice->setEnableTMVPFlag( uiCode == 1 ? true : false ); 
1372      }
1373      else
1374      {
1375        rpcSlice->setEnableTMVPFlag(false);
1376      }
1377    }
1378#if H_MV
1379    Int layerIdInVps       = rpcSlice->getLayerIdInVps(); 
1380    if( rpcSlice->getLayerId() > 0 && vps->getNumDirectRefLayers( layerIdInVps ) > 0 )
1381    {   
1382      READ_FLAG( uiCode, "inter_layer_pred_enabled_flag" ); rpcSlice->setInterLayerPredEnabledFlag( uiCode == 1 );
1383      if( rpcSlice->getInterLayerPredEnabledFlag() && vps->getNumDirectRefLayers( layerIdInVps ) > 1 )
1384      {           
1385        if( !vps->getMaxOneActiveRefLayerFlag()) 
1386        {
1387          READ_CODE( rpcSlice->getNumInterLayerRefPicsMinus1Len( ), uiCode, "num_inter_layer_ref_pics_minus1" ); rpcSlice->setNumInterLayerRefPicsMinus1( uiCode );
1388        }
1389        for( Int i = 0; i < rpcSlice->getNumActiveRefLayerPics(); i++ )   
1390        {
1391          READ_CODE( rpcSlice->getInterLayerPredLayerIdcLen( ), uiCode, "inter_layer_pred_layer_idc" ); rpcSlice->setInterLayerPredLayerIdc( i, uiCode );
1392        }
1393      } 
1394    }
1395
1396    rpcSlice->setActiveMotionPredRefLayers( );
1397
1398    if( vps->getNumSamplePredRefLayers( layerIdInVps ) > 0  &&  rpcSlice->getNumActiveRefLayerPics() > 0 )
1399    {
1400      READ_FLAG( uiCode, "inter_layer_sample_pred_only_flag" ); rpcSlice->setInterLayerSamplePredOnlyFlag( uiCode == 1 );
1401    }
1402
1403#endif
1404    if(sps->getUseSAO())
1405    {
1406      READ_FLAG(uiCode, "slice_sao_luma_flag");  rpcSlice->setSaoEnabledFlag((Bool)uiCode);
1407      READ_FLAG(uiCode, "slice_sao_chroma_flag");  rpcSlice->setSaoEnabledFlagChroma((Bool)uiCode);
1408    }
1409
1410    if (rpcSlice->getIdrPicFlag())
1411    {
1412      rpcSlice->setEnableTMVPFlag(false);
1413    }
1414    if (!rpcSlice->isIntra())
1415    {
1416
1417      READ_FLAG( uiCode, "num_ref_idx_active_override_flag");
1418      if (uiCode)
1419      {
1420        READ_UVLC (uiCode, "num_ref_idx_l0_active_minus1" );  rpcSlice->setNumRefIdx( REF_PIC_LIST_0, uiCode + 1 );
1421        if (rpcSlice->isInterB())
1422        {
1423          READ_UVLC (uiCode, "num_ref_idx_l1_active_minus1" );  rpcSlice->setNumRefIdx( REF_PIC_LIST_1, uiCode + 1 );
1424        }
1425        else
1426        {
1427          rpcSlice->setNumRefIdx(REF_PIC_LIST_1, 0);
1428        }
1429      }
1430      else
1431      {
1432        rpcSlice->setNumRefIdx(REF_PIC_LIST_0, rpcSlice->getPPS()->getNumRefIdxL0DefaultActive());
1433        if (rpcSlice->isInterB())
1434        {
1435          rpcSlice->setNumRefIdx(REF_PIC_LIST_1, rpcSlice->getPPS()->getNumRefIdxL1DefaultActive());
1436        }
1437        else
1438        {
1439          rpcSlice->setNumRefIdx(REF_PIC_LIST_1,0);
1440        }
1441      }
1442    }
1443    // }
1444    TComRefPicListModification* refPicListModification = rpcSlice->getRefPicListModification();
1445    if(!rpcSlice->isIntra())
1446    {
1447      if( !rpcSlice->getPPS()->getListsModificationPresentFlag() || rpcSlice->getNumRpsCurrTempList() <= 1 )
1448      {
1449        refPicListModification->setRefPicListModificationFlagL0( 0 );
1450      }
1451      else
1452      {
1453        READ_FLAG( uiCode, "ref_pic_list_modification_flag_l0" ); refPicListModification->setRefPicListModificationFlagL0( uiCode ? 1 : 0 );
1454      }
1455
1456      if(refPicListModification->getRefPicListModificationFlagL0())
1457      { 
1458        uiCode = 0;
1459        Int i = 0;
1460        Int numRpsCurrTempList0 = rpcSlice->getNumRpsCurrTempList();
1461        if ( numRpsCurrTempList0 > 1 )
1462        {
1463          Int length = 1;
1464          numRpsCurrTempList0 --;
1465          while ( numRpsCurrTempList0 >>= 1) 
1466          {
1467            length ++;
1468          }
1469          for (i = 0; i < rpcSlice->getNumRefIdx(REF_PIC_LIST_0); i ++)
1470          {
1471            READ_CODE( length, uiCode, "list_entry_l0" );
1472            refPicListModification->setRefPicSetIdxL0(i, uiCode );
1473          }
1474        }
1475        else
1476        {
1477          for (i = 0; i < rpcSlice->getNumRefIdx(REF_PIC_LIST_0); i ++)
1478          {
1479            refPicListModification->setRefPicSetIdxL0(i, 0 );
1480          }
1481        }
1482      }
1483    }
1484    else
1485    {
1486      refPicListModification->setRefPicListModificationFlagL0(0);
1487    }
1488    if(rpcSlice->isInterB())
1489    {
1490      if( !rpcSlice->getPPS()->getListsModificationPresentFlag() || rpcSlice->getNumRpsCurrTempList() <= 1 )
1491      {
1492        refPicListModification->setRefPicListModificationFlagL1( 0 );
1493      }
1494      else
1495      {
1496        READ_FLAG( uiCode, "ref_pic_list_modification_flag_l1" ); refPicListModification->setRefPicListModificationFlagL1( uiCode ? 1 : 0 );
1497      }
1498      if(refPicListModification->getRefPicListModificationFlagL1())
1499      {
1500        uiCode = 0;
1501        Int i = 0;
1502        Int numRpsCurrTempList1 = rpcSlice->getNumRpsCurrTempList();
1503        if ( numRpsCurrTempList1 > 1 )
1504        {
1505          Int length = 1;
1506          numRpsCurrTempList1 --;
1507          while ( numRpsCurrTempList1 >>= 1) 
1508          {
1509            length ++;
1510          }
1511          for (i = 0; i < rpcSlice->getNumRefIdx(REF_PIC_LIST_1); i ++)
1512          {
1513            READ_CODE( length, uiCode, "list_entry_l1" );
1514            refPicListModification->setRefPicSetIdxL1(i, uiCode );
1515          }
1516        }
1517        else
1518        {
1519          for (i = 0; i < rpcSlice->getNumRefIdx(REF_PIC_LIST_1); i ++)
1520          {
1521            refPicListModification->setRefPicSetIdxL1(i, 0 );
1522          }
1523        }
1524      }
1525    } 
1526    else
1527    {
1528      refPicListModification->setRefPicListModificationFlagL1(0);
1529    }
1530    if (rpcSlice->isInterB())
1531    {
1532      READ_FLAG( uiCode, "mvd_l1_zero_flag" );       rpcSlice->setMvdL1ZeroFlag( (uiCode ? true : false) );
1533    }
1534
1535    rpcSlice->setCabacInitFlag( false ); // default
1536    if(pps->getCabacInitPresentFlag() && !rpcSlice->isIntra())
1537    {
1538      READ_FLAG(uiCode, "cabac_init_flag");
1539      rpcSlice->setCabacInitFlag( uiCode ? true : false );
1540    }
1541
1542    if ( rpcSlice->getEnableTMVPFlag() )
1543    {
1544#if H_MV
1545      if( rpcSlice->getLayerId() > 0 && rpcSlice->getNumActiveMotionPredRefLayers() > 0 )
1546      {
1547        READ_FLAG( uiCode, "alt_collocated_indication_flag" ); rpcSlice->setAltCollocatedIndicationFlag( uiCode == 1 );
1548      }
1549
1550      if( rpcSlice->getAltCollocatedIndicationFlag() && rpcSlice->getNumActiveMotionPredRefLayers() > 1 ) 
1551      {         
1552        READ_UVLC( uiCode, "collocated_ref_layer_idx" ); rpcSlice->setCollocatedRefLayerIdx( uiCode );
1553      }     
1554      else 
1555      {
1556#endif
1557      if ( rpcSlice->getSliceType() == B_SLICE )
1558      {
1559        READ_FLAG( uiCode, "collocated_from_l0_flag" );
1560        rpcSlice->setColFromL0Flag(uiCode);
1561      }
1562      else
1563      {
1564        rpcSlice->setColFromL0Flag( 1 );
1565      }
1566
1567      if ( rpcSlice->getSliceType() != I_SLICE &&
1568          ((rpcSlice->getColFromL0Flag() == 1 && rpcSlice->getNumRefIdx(REF_PIC_LIST_0) > 1)||
1569           (rpcSlice->getColFromL0Flag() == 0 && rpcSlice->getNumRefIdx(REF_PIC_LIST_1) > 1)))
1570      {
1571        READ_UVLC( uiCode, "collocated_ref_idx" );
1572        rpcSlice->setColRefIdx(uiCode);
1573      }
1574      else
1575      {
1576        rpcSlice->setColRefIdx(0);
1577      }
1578#if H_MV
1579      }
1580#endif
1581    }
1582    if ( (pps->getUseWP() && rpcSlice->getSliceType()==P_SLICE) || (pps->getWPBiPred() && rpcSlice->getSliceType()==B_SLICE) )
1583    {
1584      xParsePredWeightTable(rpcSlice);
1585      rpcSlice->initWpScaling();
1586    }
1587#if H_3D_IC
1588    else if( rpcSlice->getViewIndex() && ( rpcSlice->getSliceType() == P_SLICE || rpcSlice->getSliceType() == B_SLICE ) )
1589    {
1590      UInt uiCodeTmp = 0;
1591
1592      READ_FLAG ( uiCodeTmp, "slice_ic_enable_flag" );
1593      rpcSlice->setApplyIC( uiCodeTmp );
1594
1595      if ( uiCodeTmp )
1596      {
1597        READ_FLAG ( uiCodeTmp, "ic_skip_mergeidx0" );
1598        rpcSlice->setIcSkipParseFlag( uiCodeTmp );
1599      }
1600    }
1601#endif
1602    if (!rpcSlice->isIntra())
1603    {
1604      READ_UVLC( uiCode, "five_minus_max_num_merge_cand");
1605#if H_3D_IV_MERGE
1606      Bool ivMvPredFlag = rpcSlice->getVPS()->getIvMvPredFlag( rpcSlice->getLayerIdInVps() ) ;
1607      rpcSlice->setMaxNumMergeCand(( ivMvPredFlag ? MRG_MAX_NUM_CANDS_MEM : MRG_MAX_NUM_CANDS) - uiCode);
1608#else
1609      rpcSlice->setMaxNumMergeCand(MRG_MAX_NUM_CANDS - uiCode);
1610#endif
1611    }
1612
1613    READ_SVLC( iCode, "slice_qp_delta" );
1614    rpcSlice->setSliceQp (26 + pps->getPicInitQPMinus26() + iCode);
1615
1616    assert( rpcSlice->getSliceQp() >= -sps->getQpBDOffsetY() );
1617    assert( rpcSlice->getSliceQp() <=  51 );
1618
1619    if (rpcSlice->getPPS()->getSliceChromaQpFlag())
1620    {
1621      READ_SVLC( iCode, "slice_qp_delta_cb" );
1622      rpcSlice->setSliceQpDeltaCb( iCode );
1623      assert( rpcSlice->getSliceQpDeltaCb() >= -12 );
1624      assert( rpcSlice->getSliceQpDeltaCb() <=  12 );
1625      assert( (rpcSlice->getPPS()->getChromaCbQpOffset() + rpcSlice->getSliceQpDeltaCb()) >= -12 );
1626      assert( (rpcSlice->getPPS()->getChromaCbQpOffset() + rpcSlice->getSliceQpDeltaCb()) <=  12 );
1627
1628      READ_SVLC( iCode, "slice_qp_delta_cr" );
1629      rpcSlice->setSliceQpDeltaCr( iCode );
1630      assert( rpcSlice->getSliceQpDeltaCr() >= -12 );
1631      assert( rpcSlice->getSliceQpDeltaCr() <=  12 );
1632      assert( (rpcSlice->getPPS()->getChromaCrQpOffset() + rpcSlice->getSliceQpDeltaCr()) >= -12 );
1633      assert( (rpcSlice->getPPS()->getChromaCrQpOffset() + rpcSlice->getSliceQpDeltaCr()) <=  12 );
1634    }
1635
1636    if (rpcSlice->getPPS()->getDeblockingFilterControlPresentFlag())
1637    {
1638      if(rpcSlice->getPPS()->getDeblockingFilterOverrideEnabledFlag())
1639      {
1640        READ_FLAG ( uiCode, "deblocking_filter_override_flag" );        rpcSlice->setDeblockingFilterOverrideFlag(uiCode ? true : false);
1641      }
1642      else
1643      { 
1644        rpcSlice->setDeblockingFilterOverrideFlag(0);
1645      }
1646      if(rpcSlice->getDeblockingFilterOverrideFlag())
1647      {
1648        READ_FLAG ( uiCode, "slice_disable_deblocking_filter_flag" );   rpcSlice->setDeblockingFilterDisable(uiCode ? 1 : 0);
1649        if(!rpcSlice->getDeblockingFilterDisable())
1650        {
1651          READ_SVLC( iCode, "slice_beta_offset_div2" );                       rpcSlice->setDeblockingFilterBetaOffsetDiv2(iCode);
1652          assert(rpcSlice->getDeblockingFilterBetaOffsetDiv2() >= -6 &&
1653                 rpcSlice->getDeblockingFilterBetaOffsetDiv2() <=  6);
1654          READ_SVLC( iCode, "slice_tc_offset_div2" );                         rpcSlice->setDeblockingFilterTcOffsetDiv2(iCode);
1655          assert(rpcSlice->getDeblockingFilterTcOffsetDiv2() >= -6 &&
1656                 rpcSlice->getDeblockingFilterTcOffsetDiv2() <=  6);
1657        }
1658      }
1659      else
1660      {
1661        rpcSlice->setDeblockingFilterDisable   ( rpcSlice->getPPS()->getPicDisableDeblockingFilterFlag() );
1662        rpcSlice->setDeblockingFilterBetaOffsetDiv2( rpcSlice->getPPS()->getDeblockingFilterBetaOffsetDiv2() );
1663        rpcSlice->setDeblockingFilterTcOffsetDiv2  ( rpcSlice->getPPS()->getDeblockingFilterTcOffsetDiv2() );
1664      }
1665    }
1666    else
1667    { 
1668      rpcSlice->setDeblockingFilterDisable       ( false );
1669      rpcSlice->setDeblockingFilterBetaOffsetDiv2( 0 );
1670      rpcSlice->setDeblockingFilterTcOffsetDiv2  ( 0 );
1671    }
1672
1673    Bool isSAOEnabled = (!rpcSlice->getSPS()->getUseSAO())?(false):(rpcSlice->getSaoEnabledFlag()||rpcSlice->getSaoEnabledFlagChroma());
1674    Bool isDBFEnabled = (!rpcSlice->getDeblockingFilterDisable());
1675
1676    if(rpcSlice->getPPS()->getLoopFilterAcrossSlicesEnabledFlag() && ( isSAOEnabled || isDBFEnabled ))
1677    {
1678      READ_FLAG( uiCode, "slice_loop_filter_across_slices_enabled_flag");
1679    }
1680    else
1681    {
1682      uiCode = rpcSlice->getPPS()->getLoopFilterAcrossSlicesEnabledFlag()?1:0;
1683    }
1684    rpcSlice->setLFCrossSliceBoundaryFlag( (uiCode==1)?true:false);
1685
1686  }
1687 
1688    UInt *entryPointOffset          = NULL;
1689    UInt numEntryPointOffsets, offsetLenMinus1;
1690  if( pps->getTilesEnabledFlag() || pps->getEntropyCodingSyncEnabledFlag() )
1691  {
1692    READ_UVLC(numEntryPointOffsets, "num_entry_point_offsets"); rpcSlice->setNumEntryPointOffsets ( numEntryPointOffsets );
1693    if (numEntryPointOffsets>0)
1694    {
1695      READ_UVLC(offsetLenMinus1, "offset_len_minus1");
1696    }
1697    entryPointOffset = new UInt[numEntryPointOffsets];
1698    for (UInt idx=0; idx<numEntryPointOffsets; idx++)
1699    {
1700      READ_CODE(offsetLenMinus1+1, uiCode, "entry_point_offset_minus1");
1701      entryPointOffset[ idx ] = uiCode + 1;
1702    }
1703  }
1704  else
1705  {
1706    rpcSlice->setNumEntryPointOffsets ( 0 );
1707  }
1708
1709  if(pps->getSliceHeaderExtensionPresentFlag())
1710  {
1711    READ_UVLC(uiCode,"slice_header_extension_length");
1712#if H_3D
1713    if( rpcSlice->getSPS()->hasCamParInSliceHeader() )
1714    {
1715      UInt uiViewIndex = rpcSlice->getViewIndex();
1716      for( UInt uiBaseIndex = 0; uiBaseIndex < uiViewIndex; uiBaseIndex++ )
1717      {
1718        READ_SVLC( iCode, "cp_scale" );                m_aaiTempScale [ uiBaseIndex ][ uiViewIndex ] = iCode;
1719        READ_SVLC( iCode, "cp_off" );                  m_aaiTempOffset[ uiBaseIndex ][ uiViewIndex ] = iCode;
1720        READ_SVLC( iCode, "cp_inv_scale_plus_scale" ); m_aaiTempScale [ uiViewIndex ][ uiBaseIndex ] = iCode - m_aaiTempScale [ uiBaseIndex ][ uiViewIndex ];
1721        READ_SVLC( iCode, "cp_inv_off_plus_off" );     m_aaiTempOffset[ uiViewIndex ][ uiBaseIndex ] = iCode - m_aaiTempOffset[ uiBaseIndex ][ uiViewIndex ];
1722      }
1723      rpcSlice->setCamparaSlice( m_aaiTempScale, m_aaiTempOffset );
1724    }
1725
1726    READ_FLAG(uiCode,"slice_segment_header_extension2_flag"); 
1727    if ( uiCode )
1728    {   
1729      READ_UVLC(uiCode,"slice_header_extension2_length");
1730      for(Int i=0; i<uiCode; i++)
1731      {
1732        UInt ignore;
1733        READ_CODE(8,ignore,"slice_header_extension2_data_byte");
1734      }
1735    }
1736  }
1737#else
1738    for(Int i=0; i<uiCode; i++)
1739    {
1740      UInt ignore;
1741      READ_CODE(8,ignore,"slice_header_extension_data_byte");
1742    }
1743  }
1744#endif
1745  m_pcBitstream->readByteAlignment();
1746
1747  if( pps->getTilesEnabledFlag() || pps->getEntropyCodingSyncEnabledFlag() )
1748  {
1749    Int endOfSliceHeaderLocation = m_pcBitstream->getByteLocation();
1750   
1751    // Adjust endOfSliceHeaderLocation to account for emulation prevention bytes in the slice segment header
1752    for ( UInt curByteIdx  = 0; curByteIdx<m_pcBitstream->numEmulationPreventionBytesRead(); curByteIdx++ )
1753    {
1754      if ( m_pcBitstream->getEmulationPreventionByteLocation( curByteIdx ) < endOfSliceHeaderLocation )
1755      {
1756        endOfSliceHeaderLocation++;
1757      }
1758    }
1759
1760    Int  curEntryPointOffset     = 0;
1761    Int  prevEntryPointOffset    = 0;
1762    for (UInt idx=0; idx<numEntryPointOffsets; idx++)
1763    {
1764      curEntryPointOffset += entryPointOffset[ idx ];
1765
1766      Int emulationPreventionByteCount = 0;
1767      for ( UInt curByteIdx  = 0; curByteIdx<m_pcBitstream->numEmulationPreventionBytesRead(); curByteIdx++ )
1768      {
1769        if ( m_pcBitstream->getEmulationPreventionByteLocation( curByteIdx ) >= ( prevEntryPointOffset + endOfSliceHeaderLocation ) && 
1770             m_pcBitstream->getEmulationPreventionByteLocation( curByteIdx ) <  ( curEntryPointOffset  + endOfSliceHeaderLocation ) )
1771        {
1772          emulationPreventionByteCount++;
1773        }
1774      }
1775
1776      entryPointOffset[ idx ] -= emulationPreventionByteCount;
1777      prevEntryPointOffset = curEntryPointOffset;
1778    }
1779
1780    if ( pps->getTilesEnabledFlag() )
1781    {
1782      rpcSlice->setTileLocationCount( numEntryPointOffsets );
1783
1784      UInt prevPos = 0;
1785      for (Int idx=0; idx<rpcSlice->getTileLocationCount(); idx++)
1786      {
1787        rpcSlice->setTileLocation( idx, prevPos + entryPointOffset [ idx ] );
1788        prevPos += entryPointOffset[ idx ];
1789      }
1790    }
1791    else if ( pps->getEntropyCodingSyncEnabledFlag() )
1792    {
1793    Int numSubstreams = rpcSlice->getNumEntryPointOffsets()+1;
1794      rpcSlice->allocSubstreamSizes(numSubstreams);
1795      UInt *pSubstreamSizes       = rpcSlice->getSubstreamSizes();
1796      for (Int idx=0; idx<numSubstreams-1; idx++)
1797      {
1798        if ( idx < numEntryPointOffsets )
1799        {
1800          pSubstreamSizes[ idx ] = ( entryPointOffset[ idx ] << 3 ) ;
1801        }
1802        else
1803        {
1804          pSubstreamSizes[ idx ] = 0;
1805        }
1806      }
1807    }
1808
1809    if (entryPointOffset)
1810    {
1811      delete [] entryPointOffset;
1812    }
1813  }
1814
1815  return;
1816}
1817 
1818Void TDecCavlc::parsePTL( TComPTL *rpcPTL, Bool profilePresentFlag, Int maxNumSubLayersMinus1 )
1819{
1820  UInt uiCode;
1821  if(profilePresentFlag)
1822  {
1823    parseProfileTier(rpcPTL->getGeneralPTL());
1824  }
1825  READ_CODE( 8, uiCode, "general_level_idc" );    rpcPTL->getGeneralPTL()->setLevelIdc(uiCode);
1826
1827  for (Int i = 0; i < maxNumSubLayersMinus1; i++)
1828  {
1829#if !H_MV
1830    if(profilePresentFlag)
1831    {
1832#endif
1833      READ_FLAG( uiCode, "sub_layer_profile_present_flag[i]" ); rpcPTL->setSubLayerProfilePresentFlag(i, uiCode);
1834#if H_MV
1835    rpcPTL->setSubLayerProfilePresentFlag( i, profilePresentFlag && rpcPTL->getSubLayerProfilePresentFlag(i) );
1836#else
1837    }
1838#endif
1839    READ_FLAG( uiCode, "sub_layer_level_present_flag[i]"   ); rpcPTL->setSubLayerLevelPresentFlag  (i, uiCode);
1840  }
1841 
1842  if (maxNumSubLayersMinus1 > 0)
1843  {
1844    for (Int i = maxNumSubLayersMinus1; i < 8; i++)
1845    {
1846      READ_CODE(2, uiCode, "reserved_zero_2bits");
1847      assert(uiCode == 0);
1848    }
1849  }
1850 
1851  for(Int i = 0; i < maxNumSubLayersMinus1; i++)
1852  {
1853    if( profilePresentFlag && rpcPTL->getSubLayerProfilePresentFlag(i) )
1854    {
1855      parseProfileTier(rpcPTL->getSubLayerPTL(i));
1856    }
1857    if(rpcPTL->getSubLayerLevelPresentFlag(i))
1858    {
1859      READ_CODE( 8, uiCode, "sub_layer_level_idc[i]" );   rpcPTL->getSubLayerPTL(i)->setLevelIdc(uiCode);
1860    }
1861  }
1862}
1863
1864Void TDecCavlc::parseProfileTier(ProfileTierLevel *ptl)
1865{
1866  UInt uiCode;
1867  READ_CODE(2 , uiCode, "XXX_profile_space[]");   ptl->setProfileSpace(uiCode);
1868  READ_FLAG(    uiCode, "XXX_tier_flag[]"    );   ptl->setTierFlag    (uiCode ? 1 : 0);
1869  READ_CODE(5 , uiCode, "XXX_profile_idc[]"  );   ptl->setProfileIdc  (uiCode);
1870  for(Int j = 0; j < 32; j++)
1871  {
1872    READ_FLAG(  uiCode, "XXX_profile_compatibility_flag[][j]");   ptl->setProfileCompatibilityFlag(j, uiCode ? 1 : 0);
1873  }
1874  READ_FLAG(uiCode, "general_progressive_source_flag");
1875  ptl->setProgressiveSourceFlag(uiCode ? true : false);
1876
1877  READ_FLAG(uiCode, "general_interlaced_source_flag");
1878  ptl->setInterlacedSourceFlag(uiCode ? true : false);
1879 
1880  READ_FLAG(uiCode, "general_non_packed_constraint_flag");
1881  ptl->setNonPackedConstraintFlag(uiCode ? true : false);
1882 
1883  READ_FLAG(uiCode, "general_frame_only_constraint_flag");
1884  ptl->setFrameOnlyConstraintFlag(uiCode ? true : false);
1885 
1886  READ_CODE(16, uiCode, "XXX_reserved_zero_44bits[0..15]");
1887  READ_CODE(16, uiCode, "XXX_reserved_zero_44bits[16..31]");
1888  READ_CODE(12, uiCode, "XXX_reserved_zero_44bits[32..43]");
1889}
1890
1891Void TDecCavlc::parseTerminatingBit( UInt& ruiBit )
1892{
1893  ruiBit = false;
1894  Int iBitsLeft = m_pcBitstream->getNumBitsLeft();
1895  if(iBitsLeft <= 8)
1896  {
1897    UInt uiPeekValue = m_pcBitstream->peekBits(iBitsLeft);
1898    if (uiPeekValue == (1<<(iBitsLeft-1)))
1899    {
1900      ruiBit = true;
1901    }
1902  }
1903}
1904
1905Void TDecCavlc::parseSkipFlag( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
1906{
1907  assert(0);
1908}
1909
1910Void TDecCavlc::parseCUTransquantBypassFlag( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
1911{
1912  assert(0);
1913}
1914
1915Void TDecCavlc::parseMVPIdx( Int& /*riMVPIdx*/ )
1916{
1917  assert(0);
1918}
1919
1920Void TDecCavlc::parseSplitFlag     ( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
1921{
1922  assert(0);
1923}
1924
1925Void TDecCavlc::parsePartSize( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
1926{
1927  assert(0);
1928}
1929
1930Void TDecCavlc::parsePredMode( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
1931{
1932  assert(0);
1933}
1934
1935/** Parse I_PCM information.
1936* \param pcCU pointer to CU
1937* \param uiAbsPartIdx CU index
1938* \param uiDepth CU depth
1939* \returns Void
1940*
1941* If I_PCM flag indicates that the CU is I_PCM, parse its PCM alignment bits and codes. 
1942*/
1943Void TDecCavlc::parseIPCMInfo( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
1944{
1945  assert(0);
1946}
1947
1948Void TDecCavlc::parseIntraDirLumaAng  ( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
1949{ 
1950  assert(0);
1951}
1952
1953Void TDecCavlc::parseIntraDirChroma( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
1954{
1955  assert(0);
1956}
1957
1958Void TDecCavlc::parseInterDir( TComDataCU* /*pcCU*/, UInt& /*ruiInterDir*/, UInt /*uiAbsPartIdx*/ )
1959{
1960  assert(0);
1961}
1962
1963Void TDecCavlc::parseRefFrmIdx( TComDataCU* /*pcCU*/, Int& /*riRefFrmIdx*/, RefPicList /*eRefList*/ )
1964{
1965  assert(0);
1966}
1967
1968Void TDecCavlc::parseMvd( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiPartIdx*/, UInt /*uiDepth*/, RefPicList /*eRefList*/ )
1969{
1970  assert(0);
1971}
1972
1973Void TDecCavlc::parseDeltaQP( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
1974{
1975  Int qp;
1976  Int  iDQp;
1977
1978  xReadSvlc( iDQp );
1979
1980  Int qpBdOffsetY = pcCU->getSlice()->getSPS()->getQpBDOffsetY();
1981  qp = (((Int) pcCU->getRefQP( uiAbsPartIdx ) + iDQp + 52 + 2*qpBdOffsetY )%(52+ qpBdOffsetY)) -  qpBdOffsetY;
1982
1983  UInt uiAbsQpCUPartIdx = (uiAbsPartIdx>>((g_uiMaxCUDepth - pcCU->getSlice()->getPPS()->getMaxCuDQPDepth())<<1))<<((g_uiMaxCUDepth - pcCU->getSlice()->getPPS()->getMaxCuDQPDepth())<<1) ;
1984  UInt uiQpCUDepth =   min(uiDepth,pcCU->getSlice()->getPPS()->getMaxCuDQPDepth()) ;
1985
1986  pcCU->setQPSubParts( qp, uiAbsQpCUPartIdx, uiQpCUDepth );
1987}
1988
1989Void TDecCavlc::parseCoeffNxN( TComDataCU* /*pcCU*/, TCoeff* /*pcCoef*/, UInt /*uiAbsPartIdx*/, UInt /*uiWidth*/, UInt /*uiHeight*/, UInt /*uiDepth*/, TextType /*eTType*/ )
1990{
1991  assert(0);
1992}
1993
1994Void TDecCavlc::parseTransformSubdivFlag( UInt& /*ruiSubdivFlag*/, UInt /*uiLog2TransformBlockSize*/ )
1995{
1996  assert(0);
1997}
1998
1999Void TDecCavlc::parseQtCbf( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, TextType /*eType*/, UInt /*uiTrDepth*/, UInt /*uiDepth*/ )
2000{
2001  assert(0);
2002}
2003
2004Void TDecCavlc::parseQtRootCbf( UInt /*uiAbsPartIdx*/, UInt& /*uiQtRootCbf*/ )
2005{
2006  assert(0);
2007}
2008
2009Void TDecCavlc::parseTransformSkipFlags (TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*width*/, UInt /*height*/, UInt /*uiDepth*/, TextType /*eTType*/)
2010{
2011  assert(0);
2012}
2013
2014Void TDecCavlc::parseMergeFlag ( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/, UInt /*uiPUIdx*/ )
2015{
2016  assert(0);
2017}
2018
2019Void TDecCavlc::parseMergeIndex ( TComDataCU* /*pcCU*/, UInt& /*ruiMergeIndex*/ )
2020{
2021  assert(0);
2022}
2023
2024#if H_3D_ARP
2025Void TDecCavlc::parseARPW( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
2026{
2027  assert(0);
2028}
2029#endif
2030#if H_3D_IC
2031Void TDecCavlc::parseICFlag( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
2032{
2033  assert(0);
2034}
2035#endif
2036// ====================================================================================================================
2037// Protected member functions
2038// ====================================================================================================================
2039
2040/** parse explicit wp tables
2041* \param TComSlice* pcSlice
2042* \returns Void
2043*/
2044Void TDecCavlc::xParsePredWeightTable( TComSlice* pcSlice )
2045{
2046  wpScalingParam  *wp;
2047  Bool            bChroma     = true; // color always present in HEVC ?
2048  SliceType       eSliceType  = pcSlice->getSliceType();
2049  Int             iNbRef       = (eSliceType == B_SLICE ) ? (2) : (1);
2050  UInt            uiLog2WeightDenomLuma, uiLog2WeightDenomChroma;
2051  UInt            uiTotalSignalledWeightFlags = 0;
2052 
2053  Int iDeltaDenom;
2054  // decode delta_luma_log2_weight_denom :
2055  READ_UVLC( uiLog2WeightDenomLuma, "luma_log2_weight_denom" );     // ue(v): luma_log2_weight_denom
2056  assert( uiLog2WeightDenomLuma <= 7 );
2057  if( bChroma ) 
2058  {
2059    READ_SVLC( iDeltaDenom, "delta_chroma_log2_weight_denom" );     // se(v): delta_chroma_log2_weight_denom
2060    assert((iDeltaDenom + (Int)uiLog2WeightDenomLuma)>=0);
2061    assert((iDeltaDenom + (Int)uiLog2WeightDenomLuma)<=7);
2062    uiLog2WeightDenomChroma = (UInt)(iDeltaDenom + uiLog2WeightDenomLuma);
2063  }
2064
2065  for ( Int iNumRef=0 ; iNumRef<iNbRef ; iNumRef++ ) 
2066  {
2067    RefPicList  eRefPicList = ( iNumRef ? REF_PIC_LIST_1 : REF_PIC_LIST_0 );
2068    for ( Int iRefIdx=0 ; iRefIdx<pcSlice->getNumRefIdx(eRefPicList) ; iRefIdx++ ) 
2069    {
2070      pcSlice->getWpScaling(eRefPicList, iRefIdx, wp);
2071
2072      wp[0].uiLog2WeightDenom = uiLog2WeightDenomLuma;
2073      wp[1].uiLog2WeightDenom = uiLog2WeightDenomChroma;
2074      wp[2].uiLog2WeightDenom = uiLog2WeightDenomChroma;
2075
2076      UInt  uiCode;
2077      READ_FLAG( uiCode, "luma_weight_lX_flag" );           // u(1): luma_weight_l0_flag
2078      wp[0].bPresentFlag = ( uiCode == 1 );
2079      uiTotalSignalledWeightFlags += wp[0].bPresentFlag;
2080    }
2081    if ( bChroma ) 
2082    {
2083      UInt  uiCode;
2084      for ( Int iRefIdx=0 ; iRefIdx<pcSlice->getNumRefIdx(eRefPicList) ; iRefIdx++ ) 
2085      {
2086        pcSlice->getWpScaling(eRefPicList, iRefIdx, wp);
2087        READ_FLAG( uiCode, "chroma_weight_lX_flag" );      // u(1): chroma_weight_l0_flag
2088        wp[1].bPresentFlag = ( uiCode == 1 );
2089        wp[2].bPresentFlag = ( uiCode == 1 );
2090        uiTotalSignalledWeightFlags += 2*wp[1].bPresentFlag;
2091      }
2092    }
2093    for ( Int iRefIdx=0 ; iRefIdx<pcSlice->getNumRefIdx(eRefPicList) ; iRefIdx++ ) 
2094    {
2095      pcSlice->getWpScaling(eRefPicList, iRefIdx, wp);
2096      if ( wp[0].bPresentFlag ) 
2097      {
2098        Int iDeltaWeight;
2099        READ_SVLC( iDeltaWeight, "delta_luma_weight_lX" );  // se(v): delta_luma_weight_l0[i]
2100        assert( iDeltaWeight >= -128 );
2101        assert( iDeltaWeight <=  127 );
2102        wp[0].iWeight = (iDeltaWeight + (1<<wp[0].uiLog2WeightDenom));
2103        READ_SVLC( wp[0].iOffset, "luma_offset_lX" );       // se(v): luma_offset_l0[i]
2104        assert( wp[0].iOffset >= -128 );
2105        assert( wp[0].iOffset <=  127 );
2106      }
2107      else 
2108      {
2109        wp[0].iWeight = (1 << wp[0].uiLog2WeightDenom);
2110        wp[0].iOffset = 0;
2111      }
2112      if ( bChroma ) 
2113      {
2114        if ( wp[1].bPresentFlag ) 
2115        {
2116          for ( Int j=1 ; j<3 ; j++ ) 
2117          {
2118            Int iDeltaWeight;
2119            READ_SVLC( iDeltaWeight, "delta_chroma_weight_lX" );  // se(v): chroma_weight_l0[i][j]
2120            assert( iDeltaWeight >= -128 );
2121            assert( iDeltaWeight <=  127 );
2122            wp[j].iWeight = (iDeltaWeight + (1<<wp[1].uiLog2WeightDenom));
2123
2124            Int iDeltaChroma;
2125            READ_SVLC( iDeltaChroma, "delta_chroma_offset_lX" );  // se(v): delta_chroma_offset_l0[i][j]
2126            assert( iDeltaChroma >= -512 );
2127            assert( iDeltaChroma <=  511 );
2128            Int pred = ( 128 - ( ( 128*wp[j].iWeight)>>(wp[j].uiLog2WeightDenom) ) );
2129            wp[j].iOffset = Clip3(-128, 127, (iDeltaChroma + pred) );
2130          }
2131        }
2132        else 
2133        {
2134          for ( Int j=1 ; j<3 ; j++ ) 
2135          {
2136            wp[j].iWeight = (1 << wp[j].uiLog2WeightDenom);
2137            wp[j].iOffset = 0;
2138          }
2139        }
2140      }
2141    }
2142
2143    for ( Int iRefIdx=pcSlice->getNumRefIdx(eRefPicList) ; iRefIdx<MAX_NUM_REF ; iRefIdx++ ) 
2144    {
2145      pcSlice->getWpScaling(eRefPicList, iRefIdx, wp);
2146
2147      wp[0].bPresentFlag = false;
2148      wp[1].bPresentFlag = false;
2149      wp[2].bPresentFlag = false;
2150    }
2151  }
2152  assert(uiTotalSignalledWeightFlags<=24);
2153}
2154
2155/** decode quantization matrix
2156* \param scalingList quantization matrix information
2157*/
2158Void TDecCavlc::parseScalingList(TComScalingList* scalingList)
2159{
2160  UInt  code, sizeId, listId;
2161  Bool scalingListPredModeFlag;
2162  //for each size
2163  for(sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
2164  {
2165    for(listId = 0; listId <  g_scalingListNum[sizeId]; listId++)
2166    {
2167      READ_FLAG( code, "scaling_list_pred_mode_flag");
2168      scalingListPredModeFlag = (code) ? true : false;
2169      if(!scalingListPredModeFlag) //Copy Mode
2170      {
2171        READ_UVLC( code, "scaling_list_pred_matrix_id_delta");
2172        scalingList->setRefMatrixId (sizeId,listId,(UInt)((Int)(listId)-(code)));
2173        if( sizeId > SCALING_LIST_8x8 )
2174        {
2175          scalingList->setScalingListDC(sizeId,listId,((listId == scalingList->getRefMatrixId (sizeId,listId))? 16 :scalingList->getScalingListDC(sizeId, scalingList->getRefMatrixId (sizeId,listId))));
2176        }
2177        scalingList->processRefMatrix( sizeId, listId, scalingList->getRefMatrixId (sizeId,listId));
2178
2179      }
2180      else //DPCM Mode
2181      {
2182        xDecodeScalingList(scalingList, sizeId, listId);
2183      }
2184    }
2185  }
2186
2187  return;
2188}
2189/** decode DPCM
2190* \param scalingList  quantization matrix information
2191* \param sizeId size index
2192* \param listId list index
2193*/
2194Void TDecCavlc::xDecodeScalingList(TComScalingList *scalingList, UInt sizeId, UInt listId)
2195{
2196  Int i,coefNum = min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId]);
2197  Int data;
2198  Int scalingListDcCoefMinus8 = 0;
2199  Int nextCoef = SCALING_LIST_START_VALUE;
2200  UInt* scan  = (sizeId == 0) ? g_auiSigLastScan [ SCAN_DIAG ] [ 1 ] :  g_sigLastScanCG32x32;
2201  Int *dst = scalingList->getScalingListAddress(sizeId, listId);
2202
2203  if( sizeId > SCALING_LIST_8x8 )
2204  {
2205    READ_SVLC( scalingListDcCoefMinus8, "scaling_list_dc_coef_minus8");
2206    scalingList->setScalingListDC(sizeId,listId,scalingListDcCoefMinus8 + 8);
2207    nextCoef = scalingList->getScalingListDC(sizeId,listId);
2208  }
2209
2210  for(i = 0; i < coefNum; i++)
2211  {
2212    READ_SVLC( data, "scaling_list_delta_coef");
2213    nextCoef = (nextCoef + data + 256 ) % 256;
2214    dst[scan[i]] = nextCoef;
2215  }
2216}
2217
2218Bool TDecCavlc::xMoreRbspData()
2219{ 
2220  Int bitsLeft = m_pcBitstream->getNumBitsLeft();
2221
2222  // if there are more than 8 bits, it cannot be rbsp_trailing_bits
2223  if (bitsLeft > 8)
2224  {
2225    return true;
2226  }
2227
2228  UChar lastByte = m_pcBitstream->peekBits(bitsLeft);
2229  Int cnt = bitsLeft;
2230
2231  // remove trailing bits equal to zero
2232  while ((cnt>0) && ((lastByte & 1) == 0))
2233  {
2234    lastByte >>= 1;
2235    cnt--;
2236  }
2237  // remove bit equal to one
2238  cnt--;
2239
2240  // we should not have a negative number of bits
2241  assert (cnt>=0);
2242
2243  // we have more data, if cnt is not zero
2244  return (cnt>0);
2245}
2246
2247//! \}
2248
Note: See TracBrowser for help on using the repository browser.