source: 3DVCSoftware/branches/HTM-14.1-update-dev1-RWTH/source/Lib/TLibEncoder/TEncCavlc.cpp @ 1237

Last change on this file since 1237 was 1237, checked in by rwth, 9 years ago
  • minor fix
  • Property svn:eol-style set to native
File size: 110.8 KB
Line 
1/* The copyright in this software is being made available under the BSD
2 * License, included below. This software may be subject to other third party
3 * and contributor rights, including patent rights, and no such rights are
4 * granted under this license.
5 *
6 * Copyright (c) 2010-2015, ITU/ISO/IEC
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 *
12 *  * Redistributions of source code must retain the above copyright notice,
13 *    this list of conditions and the following disclaimer.
14 *  * Redistributions in binary form must reproduce the above copyright notice,
15 *    this list of conditions and the following disclaimer in the documentation
16 *    and/or other materials provided with the distribution.
17 *  * Neither the name of the ITU/ISO/IEC nor the names of its contributors may
18 *    be used to endorse or promote products derived from this software without
19 *    specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31 * THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34/** \file     TEncCavlc.cpp
35    \brief    CAVLC encoder class
36*/
37
38#include "../TLibCommon/CommonDef.h"
39#include "TEncCavlc.h"
40#include "SEIwrite.h"
41
42#if NH_3D
43#include "TEncTop.h"
44#endif
45
46//! \ingroup TLibEncoder
47//! \{
48
49#if ENC_DEC_TRACE
50
51#if !H_MV_ENC_DEC_TRAC
52Void  xTraceVPSHeader ()
53{
54  fprintf( g_hTrace, "=========== Video Parameter Set     ===========\n" );
55
56Void  xTraceSPSHeader ()
57{
58  fprintf( g_hTrace, "=========== Sequence Parameter Set  ===========\n" );
59}
60
61Void  xTracePPSHeader ()
62{
63  fprintf( g_hTrace, "=========== Picture Parameter Set  ===========\n");
64}
65
66Void  xTraceSliceHeader ( )
67{
68  fprintf( g_hTrace, "=========== Slice ===========\n");
69}
70#endif
71#endif
72// ====================================================================================================================
73// Constructor / destructor / create / destroy
74// ====================================================================================================================
75
76TEncCavlc::TEncCavlc()
77{
78  m_pcBitIf           = NULL;
79}
80
81TEncCavlc::~TEncCavlc()
82{
83}
84
85
86// ====================================================================================================================
87// Public member functions
88// ====================================================================================================================
89
90Void TEncCavlc::resetEntropy(const TComSlice* /*pSlice*/)
91{
92}
93
94
95Void TEncCavlc::codeShortTermRefPicSet( const TComReferencePictureSet* rps, Bool calledFromSliceHeader, Int idx)
96{
97#if PRINT_RPS_INFO
98  Int lastBits = getNumberOfWrittenBits();
99#endif
100  if (idx > 0)
101  {
102  WRITE_FLAG( rps->getInterRPSPrediction(), "inter_ref_pic_set_prediction_flag" ); // inter_RPS_prediction_flag
103  }
104  if (rps->getInterRPSPrediction())
105  {
106    Int deltaRPS = rps->getDeltaRPS();
107    if(calledFromSliceHeader)
108    {
109      WRITE_UVLC( rps->getDeltaRIdxMinus1(), "delta_idx_minus1" ); // delta index of the Reference Picture Set used for prediction minus 1
110    }
111
112    WRITE_CODE( (deltaRPS >=0 ? 0: 1), 1, "delta_rps_sign" ); //delta_rps_sign
113    WRITE_UVLC( abs(deltaRPS) - 1, "abs_delta_rps_minus1"); // absolute delta RPS minus 1
114
115    for(Int j=0; j < rps->getNumRefIdc(); j++)
116    {
117      Int refIdc = rps->getRefIdc(j);
118      WRITE_CODE( (refIdc==1? 1: 0), 1, "used_by_curr_pic_flag" ); //first bit is "1" if Idc is 1
119      if (refIdc != 1)
120      {
121        WRITE_CODE( refIdc>>1, 1, "use_delta_flag" ); //second bit is "1" if Idc is 2, "0" otherwise.
122      }
123    }
124  }
125  else
126  {
127    WRITE_UVLC( rps->getNumberOfNegativePictures(), "num_negative_pics" );
128    WRITE_UVLC( rps->getNumberOfPositivePictures(), "num_positive_pics" );
129    Int prev = 0;
130    for(Int j=0 ; j < rps->getNumberOfNegativePictures(); j++)
131    {
132      WRITE_UVLC( prev-rps->getDeltaPOC(j)-1, "delta_poc_s0_minus1" );
133      prev = rps->getDeltaPOC(j);
134      WRITE_FLAG( rps->getUsed(j), "used_by_curr_pic_s0_flag");
135    }
136    prev = 0;
137    for(Int j=rps->getNumberOfNegativePictures(); j < rps->getNumberOfNegativePictures()+rps->getNumberOfPositivePictures(); j++)
138    {
139      WRITE_UVLC( rps->getDeltaPOC(j)-prev-1, "delta_poc_s1_minus1" );
140      prev = rps->getDeltaPOC(j);
141      WRITE_FLAG( rps->getUsed(j), "used_by_curr_pic_s1_flag" );
142    }
143  }
144
145#if PRINT_RPS_INFO
146  printf("irps=%d (%2d bits) ", rps->getInterRPSPrediction(), getNumberOfWrittenBits() - lastBits);
147  rps->printDeltaPOC();
148#endif
149}
150
151
152Void TEncCavlc::codePPS( const TComPPS* pcPPS )
153{
154#if ENC_DEC_TRACE
155#if H_MV_ENC_DEC_TRAC
156  tracePSHeader( "PPS", pcPPS->getLayerId() ); 
157#else
158  xTracePPSHeader ();
159#endif
160#endif
161  WRITE_UVLC( pcPPS->getPPSId(),                             "pps_pic_parameter_set_id" );
162  WRITE_UVLC( pcPPS->getSPSId(),                             "pps_seq_parameter_set_id" );
163  WRITE_FLAG( pcPPS->getDependentSliceSegmentsEnabledFlag()    ? 1 : 0, "dependent_slice_segments_enabled_flag" );
164  WRITE_FLAG( pcPPS->getOutputFlagPresentFlag() ? 1 : 0,     "output_flag_present_flag" );
165  WRITE_CODE( pcPPS->getNumExtraSliceHeaderBits(), 3,        "num_extra_slice_header_bits");
166  WRITE_FLAG( pcPPS->getSignHideFlag(), "sign_data_hiding_flag" );
167  WRITE_FLAG( pcPPS->getCabacInitPresentFlag() ? 1 : 0,   "cabac_init_present_flag" );
168#if PPS_FIX_DEPTH
169  if( pcPPS->getSPS()->getVPS()->getDepthId(pcPPS->getSPS()->getLayerId()) )
170  {
171    WRITE_UVLC( pcPPS->getNumRefIdxL0DefaultActive(),     "num_ref_idx_l0_default_active_minus1");
172    WRITE_UVLC( pcPPS->getNumRefIdxL1DefaultActive(),     "num_ref_idx_l1_default_active_minus1");
173  }
174  else
175  {
176#endif
177  WRITE_UVLC( pcPPS->getNumRefIdxL0DefaultActive()-1,     "num_ref_idx_l0_default_active_minus1");
178  WRITE_UVLC( pcPPS->getNumRefIdxL1DefaultActive()-1,     "num_ref_idx_l1_default_active_minus1");
179#if PPS_FIX_DEPTH
180  }
181#endif
182  WRITE_SVLC( pcPPS->getPicInitQPMinus26(),                  "init_qp_minus26");
183  WRITE_FLAG( pcPPS->getConstrainedIntraPred() ? 1 : 0,      "constrained_intra_pred_flag" );
184  WRITE_FLAG( pcPPS->getUseTransformSkip() ? 1 : 0,  "transform_skip_enabled_flag" );
185  WRITE_FLAG( pcPPS->getUseDQP() ? 1 : 0, "cu_qp_delta_enabled_flag" );
186  if ( pcPPS->getUseDQP() )
187  {
188    WRITE_UVLC( pcPPS->getMaxCuDQPDepth(), "diff_cu_qp_delta_depth" );
189  }
190
191  WRITE_SVLC( pcPPS->getQpOffset(COMPONENT_Cb), "pps_cb_qp_offset" );
192  WRITE_SVLC( pcPPS->getQpOffset(COMPONENT_Cr), "pps_cr_qp_offset" );
193
194  WRITE_FLAG( pcPPS->getSliceChromaQpFlag() ? 1 : 0,          "pps_slice_chroma_qp_offsets_present_flag" );
195
196  WRITE_FLAG( pcPPS->getUseWP() ? 1 : 0,  "weighted_pred_flag" );   // Use of Weighting Prediction (P_SLICE)
197  WRITE_FLAG( pcPPS->getWPBiPred() ? 1 : 0, "weighted_bipred_flag" );  // Use of Weighting Bi-Prediction (B_SLICE)
198  WRITE_FLAG( pcPPS->getTransquantBypassEnableFlag() ? 1 : 0, "transquant_bypass_enable_flag" );
199  WRITE_FLAG( pcPPS->getTilesEnabledFlag()             ? 1 : 0, "tiles_enabled_flag" );
200  WRITE_FLAG( pcPPS->getEntropyCodingSyncEnabledFlag() ? 1 : 0, "entropy_coding_sync_enabled_flag" );
201  if( pcPPS->getTilesEnabledFlag() )
202  {
203    WRITE_UVLC( pcPPS->getNumTileColumnsMinus1(),                                    "num_tile_columns_minus1" );
204    WRITE_UVLC( pcPPS->getNumTileRowsMinus1(),                                       "num_tile_rows_minus1" );
205    WRITE_FLAG( pcPPS->getTileUniformSpacingFlag(),                                  "uniform_spacing_flag" );
206    if( !pcPPS->getTileUniformSpacingFlag() )
207    {
208      for(UInt i=0; i<pcPPS->getNumTileColumnsMinus1(); i++)
209      {
210        WRITE_UVLC( pcPPS->getTileColumnWidth(i)-1,                                  "column_width_minus1" );
211      }
212      for(UInt i=0; i<pcPPS->getNumTileRowsMinus1(); i++)
213      {
214        WRITE_UVLC( pcPPS->getTileRowHeight(i)-1,                                    "row_height_minus1" );
215      }
216    }
217    if(pcPPS->getNumTileColumnsMinus1() !=0 || pcPPS->getNumTileRowsMinus1() !=0)
218    {
219      WRITE_FLAG( pcPPS->getLoopFilterAcrossTilesEnabledFlag()?1 : 0,          "loop_filter_across_tiles_enabled_flag");
220    }
221  }
222  WRITE_FLAG( pcPPS->getLoopFilterAcrossSlicesEnabledFlag()?1 : 0,        "pps_loop_filter_across_slices_enabled_flag");
223  WRITE_FLAG( pcPPS->getDeblockingFilterControlPresentFlag()?1 : 0,       "deblocking_filter_control_present_flag");
224  if(pcPPS->getDeblockingFilterControlPresentFlag())
225  {
226    WRITE_FLAG( pcPPS->getDeblockingFilterOverrideEnabledFlag() ? 1 : 0,  "deblocking_filter_override_enabled_flag" );
227    WRITE_FLAG( pcPPS->getPicDisableDeblockingFilterFlag() ? 1 : 0,       "pps_disable_deblocking_filter_flag" );
228    if(!pcPPS->getPicDisableDeblockingFilterFlag())
229    {
230      WRITE_SVLC( pcPPS->getDeblockingFilterBetaOffsetDiv2(),             "pps_beta_offset_div2" );
231      WRITE_SVLC( pcPPS->getDeblockingFilterTcOffsetDiv2(),               "pps_tc_offset_div2" );
232    }
233  }
234  WRITE_FLAG( pcPPS->getScalingListPresentFlag() ? 1 : 0,                          "pps_scaling_list_data_present_flag" );
235  if( pcPPS->getScalingListPresentFlag() )
236  {
237    codeScalingList( pcPPS->getScalingList() );
238  }
239#if PPS_FIX_DEPTH
240  if( pcPPS->getSPS()->getVPS()->getDepthId(pcPPS->getSPS()->getLayerId()) )
241  {
242    WRITE_FLAG( 1, "lists_modification_present_flag" );
243  }
244  else
245#endif
246  WRITE_FLAG( pcPPS->getListsModificationPresentFlag(), "lists_modification_present_flag");
247  WRITE_UVLC( pcPPS->getLog2ParallelMergeLevelMinus2(), "log2_parallel_merge_level_minus2");
248  WRITE_FLAG( pcPPS->getSliceHeaderExtensionPresentFlag() ? 1 : 0, "slice_segment_header_extension_present_flag");
249#if !NH_MV
250  Bool pps_extension_present_flag=false;
251  Bool pps_extension_flags[NUM_PPS_EXTENSION_FLAGS]={false};
252
253  pps_extension_flags[PPS_EXT__REXT] = pcPPS->getPpsRangeExtension().settingsDifferFromDefaults(pcPPS->getUseTransformSkip());
254
255  // Other PPS extension flags checked here.
256
257  for(Int i=0; i<NUM_PPS_EXTENSION_FLAGS; i++)
258  {
259    pps_extension_present_flag|=pps_extension_flags[i];
260  }
261
262  WRITE_FLAG( (pps_extension_present_flag?1:0), "pps_extension_present_flag" );
263
264  if (pps_extension_present_flag)
265  {
266#if ENC_DEC_TRACE || RExt__DECODER_DEBUG_BIT_STATISTICS
267    static const char *syntaxStrings[]={ "pps_range_extension_flag",
268                                         "pps_multilayer_extension_flag",
269                                         "pps_extension_6bits[0]",
270                                         "pps_extension_6bits[1]",
271                                         "pps_extension_6bits[2]",
272                                         "pps_extension_6bits[3]",
273                                         "pps_extension_6bits[4]",
274                                         "pps_extension_6bits[5]" };
275#endif
276
277    for(Int i=0; i<NUM_PPS_EXTENSION_FLAGS; i++)
278    {
279      WRITE_FLAG( pps_extension_flags[i]?1:0, syntaxStrings[i] );
280    }
281
282    for(Int i=0; i<NUM_PPS_EXTENSION_FLAGS; i++) // loop used so that the order is determined by the enum.
283    {
284      if (pps_extension_flags[i])
285      {
286        switch (PPSExtensionFlagIndex(i))
287        {
288          case PPS_EXT__REXT:
289            {
290              const TComPPSRExt &ppsRangeExtension = pcPPS->getPpsRangeExtension();
291            if (pcPPS->getUseTransformSkip())
292            {
293                WRITE_UVLC( ppsRangeExtension.getLog2MaxTransformSkipBlockSize()-2,            "log2_max_transform_skip_block_size_minus2");
294            }
295
296              WRITE_FLAG((ppsRangeExtension.getCrossComponentPredictionEnabledFlag() ? 1 : 0), "cross_component_prediction_enabled_flag" );
297
298              WRITE_FLAG(UInt(ppsRangeExtension.getChromaQpOffsetListEnabledFlag()),           "chroma_qp_offset_list_enabled_flag" );
299              if (ppsRangeExtension.getChromaQpOffsetListEnabledFlag())
300            {
301                WRITE_UVLC(ppsRangeExtension.getDiffCuChromaQpOffsetDepth(),                   "diff_cu_chroma_qp_offset_depth");
302                WRITE_UVLC(ppsRangeExtension.getChromaQpOffsetListLen() - 1,                   "chroma_qp_offset_list_len_minus1");
303              /* skip zero index */
304                for (Int cuChromaQpOffsetIdx = 0; cuChromaQpOffsetIdx < ppsRangeExtension.getChromaQpOffsetListLen(); cuChromaQpOffsetIdx++)
305              {
306                  WRITE_SVLC(ppsRangeExtension.getChromaQpOffsetListEntry(cuChromaQpOffsetIdx+1).u.comp.CbOffset,     "cb_qp_offset_list[i]");
307                  WRITE_SVLC(ppsRangeExtension.getChromaQpOffsetListEntry(cuChromaQpOffsetIdx+1).u.comp.CrOffset,     "cr_qp_offset_list[i]");
308              }
309            }
310
311              WRITE_UVLC( ppsRangeExtension.getLog2SaoOffsetScale(CHANNEL_TYPE_LUMA),           "log2_sao_offset_scale_luma"   );
312              WRITE_UVLC( ppsRangeExtension.getLog2SaoOffsetScale(CHANNEL_TYPE_CHROMA),         "log2_sao_offset_scale_chroma" );
313            }
314            break;
315          default:
316            assert(pps_extension_flags[i]==false); // Should never get here with an active PPS extension flag.
317            break;
318        } // switch
319      } // if flag present
320    } // loop over PPS flags
321  } // pps_extension_present_flag is non-zero
322#else
323    WRITE_FLAG( 1, "pps_extension_present_flag" );
324
325    WRITE_FLAG( pcPPS->getPpsRangeExtensionsFlag( ) ? 1 : 0 , "pps_range_extensions_flag" );
326    WRITE_FLAG( pcPPS->getPpsMultilayerExtensionFlag( ) ? 1 : 0 , "pps_multilayer_extension_flag" );
327    WRITE_FLAG( pcPPS->getPps3dExtensionFlag( ) ? 1 : 0 , "pps_3d_extension_flag" );
328    WRITE_CODE( pcPPS->getPpsExtension5bits( ), 5, "pps_extension_5bits" );
329    if ( pcPPS->getPpsRangeExtensionsFlag() )
330    { 
331              const TComPPSRExt &ppsRangeExtension = pcPPS->getPpsRangeExtension();
332              if (pcPPS->getUseTransformSkip())
333              {
334                WRITE_UVLC( ppsRangeExtension.getLog2MaxTransformSkipBlockSize()-2,            "log2_max_transform_skip_block_size_minus2");
335              }
336
337              WRITE_FLAG((ppsRangeExtension.getCrossComponentPredictionEnabledFlag() ? 1 : 0), "cross_component_prediction_enabled_flag" );
338
339              WRITE_FLAG(UInt(ppsRangeExtension.getChromaQpOffsetListEnabledFlag()),           "chroma_qp_offset_list_enabled_flag" );
340              if (ppsRangeExtension.getChromaQpOffsetListEnabledFlag())
341              {
342                WRITE_UVLC(ppsRangeExtension.getDiffCuChromaQpOffsetDepth(),                   "diff_cu_chroma_qp_offset_depth");
343                WRITE_UVLC(ppsRangeExtension.getChromaQpOffsetListLen() - 1,                   "chroma_qp_offset_list_len_minus1");
344                /* skip zero index */
345                for (Int cuChromaQpOffsetIdx = 0; cuChromaQpOffsetIdx < ppsRangeExtension.getChromaQpOffsetListLen(); cuChromaQpOffsetIdx++)
346                {
347                  WRITE_SVLC(ppsRangeExtension.getChromaQpOffsetListEntry(cuChromaQpOffsetIdx+1).u.comp.CbOffset,     "cb_qp_offset_list[i]");
348                  WRITE_SVLC(ppsRangeExtension.getChromaQpOffsetListEntry(cuChromaQpOffsetIdx+1).u.comp.CrOffset,     "cr_qp_offset_list[i]");
349                }
350              }
351
352              WRITE_UVLC( ppsRangeExtension.getLog2SaoOffsetScale(CHANNEL_TYPE_LUMA),           "log2_sao_offset_scale_luma"   );
353              WRITE_UVLC( ppsRangeExtension.getLog2SaoOffsetScale(CHANNEL_TYPE_CHROMA),         "log2_sao_offset_scale_chroma" );
354    }
355
356    if ( pcPPS->getPpsMultilayerExtensionFlag() )
357    { 
358      codePpsMultilayerExtension( pcPPS );
359    }
360
361#if NH_3D
362    if( pcPPS->getPps3dExtensionFlag( )  ) // This probably needs to be aligned with Rext and SHVC
363    {
364      codePps3dExtension( pcPPS ); 
365    }
366#endif
367#endif
368  xWriteRbspTrailingBits();
369}
370
371#if NH_3D
372Void  TEncCavlc::codePps3dExtension        ( const TComPPS* pcPPS )
373{
374  // Assuming that all PPS indirectly refer to the same VPS via different SPS
375  // There is no parsing dependency in decoding DLT in PPS.
376  // The VPS information passed to decodePPS() is used to arrange the decoded DLT tables to their corresponding layers.
377  // This is equivalent to the process of
378  //   Step 1) decoding DLT tables based on the number of depth layers, and
379  //   Step 2) mapping DLT tables to the depth layers
380  // as described in the 3D-HEVC WD.
381
382  // GT: Please remove dependency from VPS in the encoding as well as in the parsing function, e.g. using the equivalent process.
383  // GT: Moreover this function should only carry out the coding of the DLT and NO rate based decisions.
384  // GT: Please add a function e.g. xSetDLT in e.g. TEncSlice to find the best DLT syntax representation and do ONLY the coding of the DLT here !!
385
386#if NH_3D_DLT
387  WRITE_FLAG( pcPPS->getDLT()->getDltPresentFlag() ? 1 : 0, "dlt_present_flag" );
388
389  if ( pcPPS->getDLT()->getDltPresentFlag() )
390  {
391    WRITE_CODE(pcPPS->getDLT()->getNumDepthViews() - 1, 6, "pps_depth_layers_minus1");
392    WRITE_CODE((pcPPS->getDLT()->getDepthViewBitDepth() - 8), 4, "pps_bit_depth_for_depth_views_minus8");
393   
394    for( Int i = 0; i <= pcPPS->getDLT()->getNumDepthViews(); i++ )
395    {
396      Int layerId = pcPPS->getDLT()->getDepthIdxToLayerId(i);
397     
398      WRITE_FLAG( pcPPS->getDLT()->getUseDLTFlag( layerId ) ? 1 : 0, "dlt_flag[layerId]" );
399     
400      if ( pcPPS->getDLT()->getUseDLTFlag( layerId ) )
401      {
402        std::vector<Int> aiIdx2DepthValue_coded(256, 0);
403        UInt uiNumDepthValues_coded = pcPPS->getDLT()->getNumDepthValues(layerId);
404       
405        // ----------------------------- Actual coding -----------------------------
406        WRITE_FLAG( pcPPS->getDLT()->getInterViewDltPredEnableFlag( layerId ) ? 1 : 0, "inter_view_dlt_pred_enable_flag[ layerId ]");
407        if ( pcPPS->getDLT()->getInterViewDltPredEnableFlag( layerId ) == false )
408        {
409          WRITE_FLAG( pcPPS->getDLT()->getUseBitmapRep( layerId ) ? 1 : 0, "dlt_bit_map_rep_flag[ layerId ]" );
410         
411          for( UInt ui = 0; ui<uiNumDepthValues_coded; ui++ )
412          {
413            aiIdx2DepthValue_coded[ui] = pcPPS->getDLT()->idx2DepthValue(layerId, ui);
414          }
415        }
416        else
417        {
418          AOF( layerId > 1 );
419          // assumes ref layer id to be 1
420          std::vector<Int> viRefDLT = pcPPS->getDLT()->idx2DepthValue( 1 );
421          UInt uiRefNum = pcPPS->getDLT()->getNumDepthValues( 1 );
422          pcPPS->getDLT()->getDeltaDLT(layerId, viRefDLT, uiRefNum, aiIdx2DepthValue_coded, uiNumDepthValues_coded);
423        }
424       
425        // bit map coding
426        if ( pcPPS->getDLT()->getUseBitmapRep( layerId ) )
427        {
428          UInt uiDltArrayIndex = 0;
429          for (UInt d=0; d < 256; d++)
430          {
431            if ( d == aiIdx2DepthValue_coded[uiDltArrayIndex] )
432            {
433              WRITE_FLAG(1, "dlt_bit_map_flag[ layerId ][ j ]");
434              uiDltArrayIndex++;
435            }
436            else
437            {
438              WRITE_FLAG(0, "dlt_bit_map_flag[ layerId ][ j ]");
439            }
440          }
441        }
442        // Diff Coding
443        else
444        {
445          UInt uiMaxDiff               = 0;
446          UInt uiMinDiff               = MAX_INT;
447          UInt uiLengthMinDiff         = 0;
448          UInt uiLengthDltDiffMinusMin = 0;
449         
450          std::vector<UInt> puiDltDiffValues(uiNumDepthValues_coded, 0);
451         
452          for (UInt d = 1; d < uiNumDepthValues_coded; d++)
453          {
454            puiDltDiffValues[d] = aiIdx2DepthValue_coded[d] - aiIdx2DepthValue_coded[d-1];
455           
456            if ( uiMaxDiff < puiDltDiffValues[d] )
457            {
458              uiMaxDiff = puiDltDiffValues[d];
459            }
460           
461            if ( uiMinDiff > puiDltDiffValues[d] )
462            {
463              uiMinDiff = puiDltDiffValues[d];
464            }
465          }
466         
467          if ( uiNumDepthValues_coded > 2 )
468          {
469            uiLengthMinDiff    = (UInt) gCeilLog2(uiMaxDiff + 1);
470          }
471          if (uiMaxDiff > uiMinDiff)
472          {
473            uiLengthDltDiffMinusMin = (UInt) gCeilLog2(uiMaxDiff - uiMinDiff + 1);
474          }
475         
476          WRITE_CODE(uiNumDepthValues_coded, 8, "num_depth_values_in_dlt[layerId]");    // num_entry
477          {
478            // The condition if( uiNumDepthValues_coded > 0 ) is always true since for Single-view Diff Coding, there is at least one depth value in depth component.
479            if ( uiNumDepthValues_coded > 1 )
480            {
481              WRITE_CODE(uiMaxDiff, 8, "max_diff[ layerId ]");        // max_diff
482            }
483           
484            if ( uiNumDepthValues_coded > 2 )
485            {
486              WRITE_CODE((uiMinDiff - 1), uiLengthMinDiff, "min_diff_minus1[ layerId ]");     // min_diff_minus1
487            }
488           
489            WRITE_CODE(aiIdx2DepthValue_coded[0], 8, "dlt_depth_value0[layerId]");          // entry0
490           
491            if (uiMaxDiff > uiMinDiff)
492            {
493              for (UInt d=1; d < uiNumDepthValues_coded; d++)
494              {
495                WRITE_CODE( (puiDltDiffValues[d] - uiMinDiff), uiLengthDltDiffMinusMin, "dlt_depth_value_diff_minus_min[ layerId ][ j ]");    // entry_value_diff_minus_min[ k ]
496              }
497            }
498          }
499         
500        }
501      }
502    }
503  }
504#endif
505}
506#endif
507
508Void TEncCavlc::codeVUI( const TComVUI *pcVUI, const TComSPS* pcSPS )
509{
510#if ENC_DEC_TRACE
511  fprintf( g_hTrace, "----------- vui_parameters -----------\n");
512#endif
513  WRITE_FLAG(pcVUI->getAspectRatioInfoPresentFlag(),            "aspect_ratio_info_present_flag");
514  if (pcVUI->getAspectRatioInfoPresentFlag())
515  {
516    WRITE_CODE(pcVUI->getAspectRatioIdc(), 8,                   "aspect_ratio_idc" );
517    if (pcVUI->getAspectRatioIdc() == 255)
518    {
519      WRITE_CODE(pcVUI->getSarWidth(), 16,                      "sar_width");
520      WRITE_CODE(pcVUI->getSarHeight(), 16,                     "sar_height");
521    }
522  }
523  WRITE_FLAG(pcVUI->getOverscanInfoPresentFlag(),               "overscan_info_present_flag");
524  if (pcVUI->getOverscanInfoPresentFlag())
525  {
526    WRITE_FLAG(pcVUI->getOverscanAppropriateFlag(),             "overscan_appropriate_flag");
527  }
528  WRITE_FLAG(pcVUI->getVideoSignalTypePresentFlag(),            "video_signal_type_present_flag");
529#if NH_MV
530  assert( pcSPS->getLayerId() == 0 || !pcVUI->getVideoSignalTypePresentFlag() ); 
531#endif
532  if (pcVUI->getVideoSignalTypePresentFlag())
533  {
534    WRITE_CODE(pcVUI->getVideoFormat(), 3,                      "video_format");
535    WRITE_FLAG(pcVUI->getVideoFullRangeFlag(),                  "video_full_range_flag");
536    WRITE_FLAG(pcVUI->getColourDescriptionPresentFlag(),        "colour_description_present_flag");
537    if (pcVUI->getColourDescriptionPresentFlag())
538    {
539      WRITE_CODE(pcVUI->getColourPrimaries(), 8,                "colour_primaries");
540      WRITE_CODE(pcVUI->getTransferCharacteristics(), 8,        "transfer_characteristics");
541      WRITE_CODE(pcVUI->getMatrixCoefficients(), 8,             "matrix_coeffs");
542    }
543  }
544
545  WRITE_FLAG(pcVUI->getChromaLocInfoPresentFlag(),              "chroma_loc_info_present_flag");
546  if (pcVUI->getChromaLocInfoPresentFlag())
547  {
548    WRITE_UVLC(pcVUI->getChromaSampleLocTypeTopField(),         "chroma_sample_loc_type_top_field");
549    WRITE_UVLC(pcVUI->getChromaSampleLocTypeBottomField(),      "chroma_sample_loc_type_bottom_field");
550  }
551
552  WRITE_FLAG(pcVUI->getNeutralChromaIndicationFlag(),           "neutral_chroma_indication_flag");
553  WRITE_FLAG(pcVUI->getFieldSeqFlag(),                          "field_seq_flag");
554  WRITE_FLAG(pcVUI->getFrameFieldInfoPresentFlag(),             "frame_field_info_present_flag");
555
556  Window defaultDisplayWindow = pcVUI->getDefaultDisplayWindow();
557  WRITE_FLAG(defaultDisplayWindow.getWindowEnabledFlag(),       "default_display_window_flag");
558  if( defaultDisplayWindow.getWindowEnabledFlag() )
559  {
560    WRITE_UVLC(defaultDisplayWindow.getWindowLeftOffset()  / TComSPS::getWinUnitX(pcSPS->getChromaFormatIdc()), "def_disp_win_left_offset");
561    WRITE_UVLC(defaultDisplayWindow.getWindowRightOffset() / TComSPS::getWinUnitX(pcSPS->getChromaFormatIdc()), "def_disp_win_right_offset");
562    WRITE_UVLC(defaultDisplayWindow.getWindowTopOffset()   / TComSPS::getWinUnitY(pcSPS->getChromaFormatIdc()), "def_disp_win_top_offset");
563    WRITE_UVLC(defaultDisplayWindow.getWindowBottomOffset()/ TComSPS::getWinUnitY(pcSPS->getChromaFormatIdc()), "def_disp_win_bottom_offset");
564  }
565  const TimingInfo *timingInfo = pcVUI->getTimingInfo();
566  WRITE_FLAG(timingInfo->getTimingInfoPresentFlag(),          "vui_timing_info_present_flag");
567  if(timingInfo->getTimingInfoPresentFlag())
568  {
569    WRITE_CODE(timingInfo->getNumUnitsInTick(), 32,           "vui_num_units_in_tick");
570    WRITE_CODE(timingInfo->getTimeScale(),      32,           "vui_time_scale");
571    WRITE_FLAG(timingInfo->getPocProportionalToTimingFlag(),  "vui_poc_proportional_to_timing_flag");
572    if(timingInfo->getPocProportionalToTimingFlag())
573    {
574      WRITE_UVLC(timingInfo->getNumTicksPocDiffOneMinus1(),   "vui_num_ticks_poc_diff_one_minus1");
575    }
576    WRITE_FLAG(pcVUI->getHrdParametersPresentFlag(),              "vui_hrd_parameters_present_flag");
577    if( pcVUI->getHrdParametersPresentFlag() )
578    {
579      codeHrdParameters(pcVUI->getHrdParameters(), 1, pcSPS->getMaxTLayers() - 1 );
580    }
581  }
582
583  WRITE_FLAG(pcVUI->getBitstreamRestrictionFlag(),              "bitstream_restriction_flag");
584  if (pcVUI->getBitstreamRestrictionFlag())
585  {
586    WRITE_FLAG(pcVUI->getTilesFixedStructureFlag(),             "tiles_fixed_structure_flag");
587    WRITE_FLAG(pcVUI->getMotionVectorsOverPicBoundariesFlag(),  "motion_vectors_over_pic_boundaries_flag");
588    WRITE_FLAG(pcVUI->getRestrictedRefPicListsFlag(),           "restricted_ref_pic_lists_flag");
589    WRITE_UVLC(pcVUI->getMinSpatialSegmentationIdc(),           "min_spatial_segmentation_idc");
590    WRITE_UVLC(pcVUI->getMaxBytesPerPicDenom(),                 "max_bytes_per_pic_denom");
591    WRITE_UVLC(pcVUI->getMaxBitsPerMinCuDenom(),                "max_bits_per_min_cu_denom");
592    WRITE_UVLC(pcVUI->getLog2MaxMvLengthHorizontal(),           "log2_max_mv_length_horizontal");
593    WRITE_UVLC(pcVUI->getLog2MaxMvLengthVertical(),             "log2_max_mv_length_vertical");
594  }
595}
596
597Void TEncCavlc::codeHrdParameters( const TComHRD *hrd, Bool commonInfPresentFlag, UInt maxNumSubLayersMinus1 )
598{
599  if( commonInfPresentFlag )
600  {
601    WRITE_FLAG( hrd->getNalHrdParametersPresentFlag() ? 1 : 0 ,  "nal_hrd_parameters_present_flag" );
602    WRITE_FLAG( hrd->getVclHrdParametersPresentFlag() ? 1 : 0 ,  "vcl_hrd_parameters_present_flag" );
603    if( hrd->getNalHrdParametersPresentFlag() || hrd->getVclHrdParametersPresentFlag() )
604    {
605      WRITE_FLAG( hrd->getSubPicCpbParamsPresentFlag() ? 1 : 0,  "sub_pic_hrd_params_present_flag" );
606      if( hrd->getSubPicCpbParamsPresentFlag() )
607      {
608        WRITE_CODE( hrd->getTickDivisorMinus2(), 8,              "tick_divisor_minus2" );
609        WRITE_CODE( hrd->getDuCpbRemovalDelayLengthMinus1(), 5,  "du_cpb_removal_delay_increment_length_minus1" );
610        WRITE_FLAG( hrd->getSubPicCpbParamsInPicTimingSEIFlag() ? 1 : 0, "sub_pic_cpb_params_in_pic_timing_sei_flag" );
611        WRITE_CODE( hrd->getDpbOutputDelayDuLengthMinus1(), 5,   "dpb_output_delay_du_length_minus1"  );
612      }
613      WRITE_CODE( hrd->getBitRateScale(), 4,                     "bit_rate_scale" );
614      WRITE_CODE( hrd->getCpbSizeScale(), 4,                     "cpb_size_scale" );
615      if( hrd->getSubPicCpbParamsPresentFlag() )
616      {
617        WRITE_CODE( hrd->getDuCpbSizeScale(), 4,                "du_cpb_size_scale" );
618      }
619      WRITE_CODE( hrd->getInitialCpbRemovalDelayLengthMinus1(), 5, "initial_cpb_removal_delay_length_minus1" );
620      WRITE_CODE( hrd->getCpbRemovalDelayLengthMinus1(),        5, "au_cpb_removal_delay_length_minus1" );
621      WRITE_CODE( hrd->getDpbOutputDelayLengthMinus1(),         5, "dpb_output_delay_length_minus1" );
622    }
623  }
624  Int i, j, nalOrVcl;
625  for( i = 0; i <= maxNumSubLayersMinus1; i ++ )
626  {
627    WRITE_FLAG( hrd->getFixedPicRateFlag( i ) ? 1 : 0,          "fixed_pic_rate_general_flag");
628    Bool fixedPixRateWithinCvsFlag = true;
629    if( !hrd->getFixedPicRateFlag( i ) )
630    {
631      fixedPixRateWithinCvsFlag = hrd->getFixedPicRateWithinCvsFlag( i );
632      WRITE_FLAG( hrd->getFixedPicRateWithinCvsFlag( i ) ? 1 : 0, "fixed_pic_rate_within_cvs_flag");
633    }
634    if( fixedPixRateWithinCvsFlag )
635    {
636      WRITE_UVLC( hrd->getPicDurationInTcMinus1( i ),           "elemental_duration_in_tc_minus1");
637    }
638    else
639    {
640      WRITE_FLAG( hrd->getLowDelayHrdFlag( i ) ? 1 : 0,           "low_delay_hrd_flag");
641    }
642    if (!hrd->getLowDelayHrdFlag( i ))
643    {
644      WRITE_UVLC( hrd->getCpbCntMinus1( i ),                      "cpb_cnt_minus1");
645    }
646
647    for( nalOrVcl = 0; nalOrVcl < 2; nalOrVcl ++ )
648    {
649      if( ( ( nalOrVcl == 0 ) && ( hrd->getNalHrdParametersPresentFlag() ) ) ||
650          ( ( nalOrVcl == 1 ) && ( hrd->getVclHrdParametersPresentFlag() ) ) )
651      {
652        for( j = 0; j <= ( hrd->getCpbCntMinus1( i ) ); j ++ )
653        {
654          WRITE_UVLC( hrd->getBitRateValueMinus1( i, j, nalOrVcl ), "bit_rate_value_minus1");
655          WRITE_UVLC( hrd->getCpbSizeValueMinus1( i, j, nalOrVcl ), "cpb_size_value_minus1");
656          if( hrd->getSubPicCpbParamsPresentFlag() )
657          {
658            WRITE_UVLC( hrd->getDuCpbSizeValueMinus1( i, j, nalOrVcl ), "cpb_size_du_value_minus1");
659            WRITE_UVLC( hrd->getDuBitRateValueMinus1( i, j, nalOrVcl ), "bit_rate_du_value_minus1");
660          }
661          WRITE_FLAG( hrd->getCbrFlag( i, j, nalOrVcl ) ? 1 : 0, "cbr_flag");
662        }
663      }
664    }
665  }
666}
667
668Void TEncCavlc::codeSPS( const TComSPS* pcSPS )
669{
670
671  const ChromaFormat format                = pcSPS->getChromaFormatIdc();
672  const Bool         chromaEnabled         = isChromaEnabled(format);
673
674#if ENC_DEC_TRACE
675#if H_MV_ENC_DEC_TRAC
676  tracePSHeader( "SPS", pcSPS->getLayerId() ); 
677#else
678  xTraceSPSHeader ();
679#endif
680#endif
681  WRITE_CODE( pcSPS->getVPSId (),          4,       "sps_video_parameter_set_id" );
682#if NH_MV
683  if ( pcSPS->getLayerId() == 0 )
684  {
685#endif
686  WRITE_CODE( pcSPS->getMaxTLayers() - 1,  3,       "sps_max_sub_layers_minus1" );
687#if NH_MV
688  }
689  else
690  {
691    WRITE_CODE( pcSPS->getSpsExtOrMaxSubLayersMinus1( ), 3, "sps_ext_or_max_sub_layers_minus1" );
692  }
693  if ( !pcSPS->getMultiLayerExtSpsFlag() )
694  {
695#endif
696  WRITE_FLAG( pcSPS->getTemporalIdNestingFlag() ? 1 : 0, "sps_temporal_id_nesting_flag" );
697  codePTL(pcSPS->getPTL(), 1, pcSPS->getMaxTLayers() - 1);
698#if NH_MV
699}
700#endif
701  WRITE_UVLC( pcSPS->getSPSId (),                   "sps_seq_parameter_set_id" );
702#if NH_MV
703    if ( pcSPS->getMultiLayerExtSpsFlag() )
704  {
705    WRITE_FLAG( pcSPS->getUpdateRepFormatFlag( ) ? 1 : 0 , "update_rep_format_flag" );
706    if ( pcSPS->getUpdateRepFormatFlag() )
707    { 
708      WRITE_CODE( pcSPS->getSpsRepFormatIdx( ), 8, "sps_rep_format_idx" );
709    }
710  }
711  else
712  {
713#endif
714  WRITE_UVLC( Int(pcSPS->getChromaFormatIdc ()),    "chroma_format_idc" );
715  if( format == CHROMA_444 )
716  {
717    WRITE_FLAG( 0,                                  "separate_colour_plane_flag");
718  }
719
720  WRITE_UVLC( pcSPS->getPicWidthInLumaSamples (),   "pic_width_in_luma_samples" );
721  WRITE_UVLC( pcSPS->getPicHeightInLumaSamples(),   "pic_height_in_luma_samples" );
722  Window conf = pcSPS->getConformanceWindow();
723
724  WRITE_FLAG( conf.getWindowEnabledFlag(),          "conformance_window_flag" );
725  if (conf.getWindowEnabledFlag())
726  {
727    WRITE_UVLC( conf.getWindowLeftOffset()   / TComSPS::getWinUnitX(pcSPS->getChromaFormatIdc() ), "conf_win_left_offset" );
728    WRITE_UVLC( conf.getWindowRightOffset()  / TComSPS::getWinUnitX(pcSPS->getChromaFormatIdc() ), "conf_win_right_offset" );
729    WRITE_UVLC( conf.getWindowTopOffset()    / TComSPS::getWinUnitY(pcSPS->getChromaFormatIdc() ), "conf_win_top_offset" );
730    WRITE_UVLC( conf.getWindowBottomOffset() / TComSPS::getWinUnitY(pcSPS->getChromaFormatIdc() ), "conf_win_bottom_offset" );
731  }
732#if NH_MV
733}
734#endif
735#if NH_MV
736  if ( !pcSPS->getMultiLayerExtSpsFlag() )
737  { 
738#endif
739
740  WRITE_UVLC( pcSPS->getBitDepth(CHANNEL_TYPE_LUMA) - 8,                      "bit_depth_luma_minus8" );
741
742  WRITE_UVLC( chromaEnabled ? (pcSPS->getBitDepth(CHANNEL_TYPE_CHROMA) - 8):0,  "bit_depth_chroma_minus8" );
743#if NH_MV
744  }
745#endif
746  WRITE_UVLC( pcSPS->getBitsForPOC()-4,                 "log2_max_pic_order_cnt_lsb_minus4" );
747#if NH_MV
748  if ( !pcSPS->getMultiLayerExtSpsFlag()) 
749  { 
750#endif
751
752  const Bool subLayerOrderingInfoPresentFlag = 1;
753  WRITE_FLAG(subLayerOrderingInfoPresentFlag,       "sps_sub_layer_ordering_info_present_flag");
754  for(UInt i=0; i <= pcSPS->getMaxTLayers()-1; i++)
755  {
756    WRITE_UVLC( pcSPS->getMaxDecPicBuffering(i) - 1,       "sps_max_dec_pic_buffering_minus1[i]" );
757    WRITE_UVLC( pcSPS->getNumReorderPics(i),               "sps_max_num_reorder_pics[i]" );
758    WRITE_UVLC( pcSPS->getMaxLatencyIncrease(i),           "sps_max_latency_increase_plus1[i]" );
759    if (!subLayerOrderingInfoPresentFlag)
760    {
761      break;
762    }
763  }
764#if NH_MV
765  }
766#endif
767
768  assert( pcSPS->getMaxCUWidth() == pcSPS->getMaxCUHeight() );
769  WRITE_UVLC( pcSPS->getLog2MinCodingBlockSize() - 3,                                "log2_min_luma_coding_block_size_minus3" );
770  WRITE_UVLC( pcSPS->getLog2DiffMaxMinCodingBlockSize(),                             "log2_diff_max_min_luma_coding_block_size" );
771  WRITE_UVLC( pcSPS->getQuadtreeTULog2MinSize() - 2,                                 "log2_min_luma_transform_block_size_minus2" );
772  WRITE_UVLC( pcSPS->getQuadtreeTULog2MaxSize() - pcSPS->getQuadtreeTULog2MinSize(), "log2_diff_max_min_luma_transform_block_size" );
773  WRITE_UVLC( pcSPS->getQuadtreeTUMaxDepthInter() - 1,                               "max_transform_hierarchy_depth_inter" );
774  WRITE_UVLC( pcSPS->getQuadtreeTUMaxDepthIntra() - 1,                               "max_transform_hierarchy_depth_intra" );
775  WRITE_FLAG( pcSPS->getScalingListFlag() ? 1 : 0,                                   "scaling_list_enabled_flag" );
776  if(pcSPS->getScalingListFlag())
777  {
778#if NH_MV
779    if ( pcSPS->getMultiLayerExtSpsFlag() )
780    {   
781      WRITE_FLAG( pcSPS->getSpsInferScalingListFlag( ) ? 1 : 0 , "sps_infer_scaling_list_flag" );
782    }
783
784    if ( pcSPS->getSpsInferScalingListFlag() )
785    {
786      WRITE_CODE( pcSPS->getSpsScalingListRefLayerId( ), 6, "sps_scaling_list_ref_layer_id" );
787    }
788    else
789    {   
790#endif
791
792    WRITE_FLAG( pcSPS->getScalingListPresentFlag() ? 1 : 0,                          "sps_scaling_list_data_present_flag" );
793    if(pcSPS->getScalingListPresentFlag())
794    {
795      codeScalingList( pcSPS->getScalingList() );
796    }
797#if NH_MV
798    }
799#endif
800  }
801  WRITE_FLAG( pcSPS->getUseAMP() ? 1 : 0,                                            "amp_enabled_flag" );
802  WRITE_FLAG( pcSPS->getUseSAO() ? 1 : 0,                                            "sample_adaptive_offset_enabled_flag");
803
804  WRITE_FLAG( pcSPS->getUsePCM() ? 1 : 0,                                            "pcm_enabled_flag");
805  if( pcSPS->getUsePCM() )
806  {
807    WRITE_CODE( pcSPS->getPCMBitDepth(CHANNEL_TYPE_LUMA) - 1, 4,                            "pcm_sample_bit_depth_luma_minus1" );
808    WRITE_CODE( chromaEnabled ? (pcSPS->getPCMBitDepth(CHANNEL_TYPE_CHROMA) - 1) : 0, 4,    "pcm_sample_bit_depth_chroma_minus1" );
809    WRITE_UVLC( pcSPS->getPCMLog2MinSize() - 3,                                      "log2_min_pcm_luma_coding_block_size_minus3" );
810    WRITE_UVLC( pcSPS->getPCMLog2MaxSize() - pcSPS->getPCMLog2MinSize(),             "log2_diff_max_min_pcm_luma_coding_block_size" );
811    WRITE_FLAG( pcSPS->getPCMFilterDisableFlag()?1 : 0,                              "pcm_loop_filter_disable_flag");
812  }
813
814  assert( pcSPS->getMaxTLayers() > 0 );
815
816  const TComRPSList* rpsList = pcSPS->getRPSList();
817
818  WRITE_UVLC(rpsList->getNumberOfReferencePictureSets(), "num_short_term_ref_pic_sets" );
819  for(Int i=0; i < rpsList->getNumberOfReferencePictureSets(); i++)
820  {
821    const TComReferencePictureSet*rps = rpsList->getReferencePictureSet(i);
822    codeShortTermRefPicSet( rps,false, i);
823  }
824  WRITE_FLAG( pcSPS->getLongTermRefsPresent() ? 1 : 0,         "long_term_ref_pics_present_flag" );
825  if (pcSPS->getLongTermRefsPresent())
826  {
827    WRITE_UVLC(pcSPS->getNumLongTermRefPicSPS(), "num_long_term_ref_pics_sps" );
828    for (UInt k = 0; k < pcSPS->getNumLongTermRefPicSPS(); k++)
829    {
830      WRITE_CODE( pcSPS->getLtRefPicPocLsbSps(k), pcSPS->getBitsForPOC(), "lt_ref_pic_poc_lsb_sps");
831      WRITE_FLAG( pcSPS->getUsedByCurrPicLtSPSFlag(k), "used_by_curr_pic_lt_sps_flag[i]");
832    }
833  }
834  WRITE_FLAG( pcSPS->getTMVPFlagsPresent()  ? 1 : 0,           "sps_temporal_mvp_enable_flag" );
835
836  WRITE_FLAG( pcSPS->getUseStrongIntraSmoothing(),             "sps_strong_intra_smoothing_enable_flag" );
837
838  WRITE_FLAG( pcSPS->getVuiParametersPresentFlag(),             "vui_parameters_present_flag" );
839  if (pcSPS->getVuiParametersPresentFlag())
840  {
841      codeVUI(pcSPS->getVuiParameters(), pcSPS);
842  }
843
844#if !NH_MV
845  Bool sps_extension_present_flag=false;
846  Bool sps_extension_flags[NUM_SPS_EXTENSION_FLAGS]={false};
847
848  sps_extension_flags[SPS_EXT__REXT] = pcSPS->getSpsRangeExtension().settingsDifferFromDefaults();
849
850  // Other SPS extension flags checked here.
851
852  for(Int i=0; i<NUM_SPS_EXTENSION_FLAGS; i++)
853  {
854    sps_extension_present_flag|=sps_extension_flags[i];
855  }
856
857  WRITE_FLAG( (sps_extension_present_flag?1:0), "sps_extension_present_flag" );
858
859  if (sps_extension_present_flag)
860  {
861#if ENC_DEC_TRACE || RExt__DECODER_DEBUG_BIT_STATISTICS
862    static const char *syntaxStrings[]={ "sps_range_extension_flag",
863      "sps_multilayer_extension_flag",
864      "sps_extension_6bits[0]",
865      "sps_extension_6bits[1]",
866      "sps_extension_6bits[2]",
867      "sps_extension_6bits[3]",
868      "sps_extension_6bits[4]",
869      "sps_extension_6bits[5]" };
870#endif
871
872    for(Int i=0; i<NUM_SPS_EXTENSION_FLAGS; i++)
873    {
874      WRITE_FLAG( sps_extension_flags[i]?1:0, syntaxStrings[i] );
875    }
876
877    for(Int i=0; i<NUM_SPS_EXTENSION_FLAGS; i++) // loop used so that the order is determined by the enum.
878    {
879      if (sps_extension_flags[i])
880      {
881        switch (SPSExtensionFlagIndex(i))
882        {
883        case SPS_EXT__REXT:
884          {
885            const TComSPSRExt &spsRangeExtension=pcSPS->getSpsRangeExtension();
886
887            WRITE_FLAG( (spsRangeExtension.getTransformSkipRotationEnabledFlag() ? 1 : 0),      "transform_skip_rotation_enabled_flag");
888            WRITE_FLAG( (spsRangeExtension.getTransformSkipContextEnabledFlag() ? 1 : 0),       "transform_skip_context_enabled_flag");
889            WRITE_FLAG( (spsRangeExtension.getRdpcmEnabledFlag(RDPCM_SIGNAL_IMPLICIT) ? 1 : 0), "implicit_rdpcm_enabled_flag" );
890            WRITE_FLAG( (spsRangeExtension.getRdpcmEnabledFlag(RDPCM_SIGNAL_EXPLICIT) ? 1 : 0), "explicit_rdpcm_enabled_flag" );
891            WRITE_FLAG( (spsRangeExtension.getExtendedPrecisionProcessingFlag() ? 1 : 0),       "extended_precision_processing_flag" );
892            WRITE_FLAG( (spsRangeExtension.getIntraSmoothingDisabledFlag() ? 1 : 0),            "intra_smoothing_disabled_flag" );
893            WRITE_FLAG( (spsRangeExtension.getHighPrecisionOffsetsEnabledFlag() ? 1 : 0),       "high_precision_offsets_enabled_flag" );
894            WRITE_FLAG( (spsRangeExtension.getPersistentRiceAdaptationEnabledFlag() ? 1 : 0),   "persistent_rice_adaptation_enabled_flag" );
895            WRITE_FLAG( (spsRangeExtension.getCabacBypassAlignmentEnabledFlag() ? 1 : 0),       "cabac_bypass_alignment_enabled_flag" );
896          break;
897          }
898        default:
899          assert(sps_extension_flags[i]==false); // Should never get here with an active SPS extension flag.
900          break;
901        }
902      }
903    }
904  }
905#else
906  WRITE_FLAG( pcSPS->getSpsExtensionPresentFlag(), "sps_extension_present_flag" );
907
908  if ( pcSPS->getSpsExtensionPresentFlag() )
909  {
910    WRITE_FLAG( pcSPS->getSpsRangeExtensionsFlag( )     ? 1 : 0 , "sps_range_extensions_flag" );
911    WRITE_FLAG( pcSPS->getSpsMultilayerExtensionFlag( ) ? 1 : 0 , "sps_multilayer_extension_flag" );
912    WRITE_FLAG( pcSPS->getSps3dExtensionFlag( )         ? 1 : 0 , "sps_3d_extension_flag" );
913    WRITE_CODE( pcSPS->getSpsExtension5bits( )              , 5 , "sps_extension_5bits" );
914  }
915
916  if ( pcSPS->getSpsRangeExtensionsFlag() )
917  {
918    const TComSPSRExt &spsRangeExtension=pcSPS->getSpsRangeExtension();
919
920    WRITE_FLAG( (spsRangeExtension.getTransformSkipRotationEnabledFlag() ? 1 : 0),      "transform_skip_rotation_enabled_flag");
921    WRITE_FLAG( (spsRangeExtension.getTransformSkipContextEnabledFlag() ? 1 : 0),       "transform_skip_context_enabled_flag");
922    WRITE_FLAG( (spsRangeExtension.getRdpcmEnabledFlag(RDPCM_SIGNAL_IMPLICIT) ? 1 : 0), "implicit_rdpcm_enabled_flag" );
923    WRITE_FLAG( (spsRangeExtension.getRdpcmEnabledFlag(RDPCM_SIGNAL_EXPLICIT) ? 1 : 0), "explicit_rdpcm_enabled_flag" );
924    WRITE_FLAG( (spsRangeExtension.getExtendedPrecisionProcessingFlag() ? 1 : 0),       "extended_precision_processing_flag" );
925    WRITE_FLAG( (spsRangeExtension.getIntraSmoothingDisabledFlag() ? 1 : 0),            "intra_smoothing_disabled_flag" );
926    WRITE_FLAG( (spsRangeExtension.getHighPrecisionOffsetsEnabledFlag() ? 1 : 0),       "high_precision_offsets_enabled_flag" );
927    WRITE_FLAG( (spsRangeExtension.getPersistentRiceAdaptationEnabledFlag() ? 1 : 0),   "persistent_rice_adaptation_enabled_flag" );
928    WRITE_FLAG( (spsRangeExtension.getCabacBypassAlignmentEnabledFlag() ? 1 : 0),       "cabac_bypass_alignment_enabled_flag" );
929  }
930
931  if ( pcSPS->getSpsMultilayerExtensionFlag() )
932  {
933    codeSPSExtension( pcSPS ); 
934  }
935
936#if NH_3D
937  if ( pcSPS->getSps3dExtensionFlag() )
938  {
939    codeSPS3dExtension( pcSPS ); 
940  }
941
942#endif
943#endif
944  xWriteRbspTrailingBits();
945}
946
947#if NH_MV
948Void TEncCavlc::codeSPSExtension( const TComSPS* pcSPS )
949{
950  WRITE_FLAG( pcSPS->getInterViewMvVertConstraintFlag() ? 1 : 0, "inter_view_mv_vert_constraint_flag" );
951}
952
953
954Void TEncCavlc::codePpsMultilayerExtension(const TComPPS* pcPPS)
955{
956  WRITE_FLAG( pcPPS->getPocResetInfoPresentFlag( ) ? 1 : 0 , "poc_reset_info_present_flag" );
957  WRITE_FLAG( pcPPS->getPpsInferScalingListFlag( ) ? 1 : 0 , "pps_infer_scaling_list_flag" );
958  if (pcPPS->getPpsInferScalingListFlag())
959  {
960    WRITE_CODE( pcPPS->getPpsScalingListRefLayerId( ), 6, "pps_scaling_list_ref_layer_id" );
961  }
962  WRITE_UVLC( 0, "num_ref_loc_offsets" );
963  WRITE_FLAG( 0 , "colour_mapping_enabled_flag" );
964
965}
966
967#endif
968
969#if NH_3D
970Void TEncCavlc::codeSPS3dExtension( const TComSPS* pcSPS )
971{
972  const TComSps3dExtension* sps3dExt = pcSPS->getSps3dExtension();
973  for( Int d = 0; d  <=  1; d++ )
974  {
975    WRITE_FLAG( sps3dExt->getIvMvPredFlag( d ) ? 1 : 0 , "iv_mv_pred_flag" );
976    WRITE_FLAG( sps3dExt->getIvMvScalingFlag( d ) ? 1 : 0 , "iv_mv_scaling_flag" );
977    if( d  ==  0 )
978    {
979      WRITE_UVLC( sps3dExt->getLog2SubPbSizeMinus3( d ), "log2_sub_pb_size_minus3" );
980      WRITE_FLAG( sps3dExt->getIvResPredFlag( d ) ? 1 : 0 , "iv_res_pred_flag" );
981      WRITE_FLAG( sps3dExt->getDepthRefinementFlag( d ) ? 1 : 0 , "depth_refinement_flag" );
982      WRITE_FLAG( sps3dExt->getViewSynthesisPredFlag( d ) ? 1 : 0 , "view_synthesis_pred_flag" );
983      WRITE_FLAG( sps3dExt->getDepthBasedBlkPartFlag( d ) ? 1 : 0 , "depth_based_blk_part_flag" );
984    }
985    else 
986    {
987      WRITE_FLAG( sps3dExt->getMpiFlag( d ) ? 1 : 0 , "mpi_flag" );
988      WRITE_UVLC( sps3dExt->getLog2MpiSubPbSizeMinus3( d ), "log2_mpi_sub_pb_size_minus3" );
989      WRITE_FLAG( sps3dExt->getIntraContourFlag( d ) ? 1 : 0 , "intra_contour_flag" );
990      WRITE_FLAG( sps3dExt->getIntraSdcWedgeFlag( d ) ? 1 : 0 , "intra_sdc_wedge_flag" );
991      WRITE_FLAG( sps3dExt->getQtPredFlag( d ) ? 1 : 0 , "qt_pred_flag" );
992      WRITE_FLAG( sps3dExt->getInterSdcFlag( d ) ? 1 : 0 , "inter_sdc_flag" );
993      WRITE_FLAG( sps3dExt->getDepthIntraSkipFlag( d ) ? 1 : 0 , "intra_skip_flag" );
994    }
995  }
996}
997#endif
998
999
1000Void TEncCavlc::codeVPS( const TComVPS* pcVPS )
1001{
1002#if ENC_DEC_TRACE
1003#if H_MV_ENC_DEC_TRAC
1004  tracePSHeader( "VPS", getEncTop()->getLayerId() ); 
1005#else
1006  xTraceVPSHeader();
1007#endif
1008#endif
1009  WRITE_CODE( pcVPS->getVPSId(),                    4,        "vps_video_parameter_set_id" );
1010#if NH_MV
1011  WRITE_FLAG( pcVPS->getVpsBaseLayerInternalFlag( ) ? 1 : 0 , "vps_base_layer_internal_flag" );
1012  WRITE_FLAG( pcVPS->getVpsBaseLayerAvailableFlag( ) ? 1 : 0 , "vps_base_layer_available_flag" );
1013#else
1014  WRITE_FLAG(                                       1,        "vps_base_layer_internal_flag" );
1015  WRITE_FLAG(                                       1,        "vps_base_layer_available_flag" );
1016#endif
1017#if NH_MV
1018  WRITE_CODE( pcVPS->getMaxLayersMinus1(),       6,        "vps_max_layers_minus1" );
1019#else
1020  WRITE_CODE( 0,                                    6,        "vps_max_layers_minus1" );
1021#endif
1022  WRITE_CODE( pcVPS->getMaxTLayers() - 1,           3,        "vps_max_sub_layers_minus1" );
1023  WRITE_FLAG( pcVPS->getTemporalNestingFlag(),                "vps_temporal_id_nesting_flag" );
1024  assert (pcVPS->getMaxTLayers()>1||pcVPS->getTemporalNestingFlag());
1025  WRITE_CODE( 0xffff,                              16,        "vps_reserved_0xffff_16bits" );
1026  codePTL( pcVPS->getPTL(), true, pcVPS->getMaxTLayers() - 1 );
1027  const Bool subLayerOrderingInfoPresentFlag = 1;
1028  WRITE_FLAG(subLayerOrderingInfoPresentFlag,              "vps_sub_layer_ordering_info_present_flag");
1029  for(UInt i=0; i <= pcVPS->getMaxTLayers()-1; i++)
1030  {
1031    WRITE_UVLC( pcVPS->getMaxDecPicBuffering(i) - 1,       "vps_max_dec_pic_buffering_minus1[i]" );
1032    WRITE_UVLC( pcVPS->getNumReorderPics(i),               "vps_max_num_reorder_pics[i]" );
1033    WRITE_UVLC( pcVPS->getMaxLatencyIncrease(i),           "vps_max_latency_increase_plus1[i]" );
1034    if (!subLayerOrderingInfoPresentFlag)
1035    {
1036      break;
1037    }
1038  }
1039
1040  assert( pcVPS->getNumHrdParameters() <= MAX_VPS_NUM_HRD_PARAMETERS );
1041#if NH_MV
1042  assert( pcVPS->getVpsMaxLayerId() < MAX_VPS_NUH_LAYER_ID_PLUS1 );
1043  WRITE_CODE( pcVPS->getVpsMaxLayerId(), 6,                 "vps_max_layer_id" ); 
1044  WRITE_UVLC( pcVPS->getVpsNumLayerSetsMinus1(),  "vps_num_layer_sets_minus1" );
1045  for( UInt opsIdx = 1; opsIdx <= pcVPS->getVpsNumLayerSetsMinus1(); opsIdx ++ )
1046  {
1047    // Operation point set
1048    for( UInt i = 0; i <= pcVPS->getVpsMaxLayerId(); i ++ )
1049    {
1050#else
1051  assert( pcVPS->getMaxNuhReservedZeroLayerId() < MAX_VPS_NUH_RESERVED_ZERO_LAYER_ID_PLUS1 );
1052  WRITE_CODE( pcVPS->getMaxNuhReservedZeroLayerId(), 6,     "vps_max_layer_id" );
1053  WRITE_UVLC( pcVPS->getMaxOpSets() - 1,                    "vps_num_layer_sets_minus1" );
1054  for( UInt opsIdx = 1; opsIdx <= ( pcVPS->getMaxOpSets() - 1 ); opsIdx ++ )
1055  {
1056    // Operation point set
1057    for( UInt i = 0; i <= pcVPS->getMaxNuhReservedZeroLayerId(); i ++ )
1058    {
1059      // Only applicable for version 1
1060      // pcVPS->setLayerIdIncludedFlag( true, opsIdx, i );
1061#endif
1062      WRITE_FLAG( pcVPS->getLayerIdIncludedFlag( opsIdx, i ) ? 1 : 0, "layer_id_included_flag[opsIdx][i]" );
1063    }
1064  }
1065  const TimingInfo *timingInfo = pcVPS->getTimingInfo();
1066  WRITE_FLAG(timingInfo->getTimingInfoPresentFlag(),          "vps_timing_info_present_flag");
1067  if(timingInfo->getTimingInfoPresentFlag())
1068  {
1069    WRITE_CODE(timingInfo->getNumUnitsInTick(), 32,           "vps_num_units_in_tick");
1070    WRITE_CODE(timingInfo->getTimeScale(),      32,           "vps_time_scale");
1071    WRITE_FLAG(timingInfo->getPocProportionalToTimingFlag(),  "vps_poc_proportional_to_timing_flag");
1072    if(timingInfo->getPocProportionalToTimingFlag())
1073    {
1074      WRITE_UVLC(timingInfo->getNumTicksPocDiffOneMinus1(),   "vps_num_ticks_poc_diff_one_minus1");
1075    }
1076    WRITE_UVLC( pcVPS->getNumHrdParameters(),                 "vps_num_hrd_parameters" );
1077
1078    if( pcVPS->getNumHrdParameters() > 0 )
1079    {
1080      for( UInt i = 0; i < pcVPS->getNumHrdParameters(); i ++ )
1081      {
1082        // Only applicable for version 1
1083        WRITE_UVLC( pcVPS->getHrdOpSetIdx( i ),                "hrd_layer_set_idx" );
1084        if( i > 0 )
1085        {
1086          WRITE_FLAG( pcVPS->getCprmsPresentFlag( i ) ? 1 : 0, "cprms_present_flag[i]" );
1087        }
1088        codeHrdParameters(pcVPS->getHrdParameters(i), pcVPS->getCprmsPresentFlag( i ), pcVPS->getMaxTLayers() - 1);
1089      }
1090    }
1091  }
1092#if NH_MV
1093  WRITE_FLAG( pcVPS->getVpsExtensionFlag(),                     "vps_extension_flag" );
1094  m_pcBitIf->writeAlignOne();
1095  codeVPSExtension( pcVPS );                           
1096#if NH_3D
1097  WRITE_FLAG( 1,                     "vps_extension2_flag" ); 
1098  WRITE_FLAG( 1,                     "vps_3d_extension_flag" );
1099  m_pcBitIf->writeAlignOne();     
1100  codeVPS3dExtension( pcVPS ); 
1101  WRITE_FLAG( 0,                     "vps_extension3_flag" );
1102#else
1103  WRITE_FLAG( 0,                     "vps_extension2_flag" );
1104#endif
1105#else
1106  WRITE_FLAG( 0,                     "vps_extension_flag" );
1107#endif
1108
1109  //future extensions here..
1110  xWriteRbspTrailingBits();
1111}
1112
1113#if NH_MV
1114Void TEncCavlc::codeVPSExtension( const TComVPS *pcVPS ) 
1115{ 
1116  if( pcVPS->getMaxLayersMinus1() > 0  &&  pcVPS->getVpsBaseLayerInternalFlag() )
1117  {
1118    codePTL( pcVPS->getPTL( 1 ),0, pcVPS->getMaxSubLayersMinus1()  );
1119  }
1120
1121  WRITE_FLAG( pcVPS->getSplittingFlag() ? 1 : 0,             "splitting_flag" );
1122
1123  for( Int type = 0; type < MAX_NUM_SCALABILITY_TYPES; type++ )
1124  {
1125    WRITE_FLAG( pcVPS->getScalabilityMaskFlag( type ) ? 1 : 0,   "scalability_mask_flag[i]" );
1126  }
1127
1128  for( Int sIdx = 0; sIdx < pcVPS->getNumScalabilityTypes( ) - ( pcVPS->getSplittingFlag() ? 1 : 0 ); sIdx++ )
1129  {
1130    WRITE_CODE( pcVPS->getDimensionIdLen( sIdx ) - 1 , 3,    "dimension_id_len_minus1[j]");   
1131  }
1132
1133  if ( pcVPS->getSplittingFlag() )
1134  {   
1135    assert( pcVPS->getDimensionIdLen( pcVPS->getNumScalabilityTypes( ) - 1 ) == pcVPS->inferLastDimsionIdLenMinus1() + 1 );
1136  }   
1137
1138  WRITE_FLAG( pcVPS->getVpsNuhLayerIdPresentFlag() ? 1 : 0,  "vps_nuh_layer_id_present_flag");
1139
1140  for( Int i = 1; i <= pcVPS->getMaxLayersMinus1(); i++ )
1141  {
1142    if ( pcVPS->getVpsNuhLayerIdPresentFlag() )
1143    {     
1144      WRITE_CODE( pcVPS->getLayerIdInNuh( i ), 6,          "layer_id_in_nuh[i]");
1145    }
1146    else
1147    {
1148      assert( pcVPS->getLayerIdInNuh( i ) == i ); 
1149    }
1150
1151    assert(  pcVPS->getLayerIdInVps( pcVPS->getLayerIdInNuh( i ) ) == i ); 
1152
1153    for( Int j = 0; j < pcVPS->getNumScalabilityTypes() ; j++ )
1154    {
1155      if ( !pcVPS->getSplittingFlag() )
1156      {
1157        WRITE_CODE( pcVPS->getDimensionId( i, j ), pcVPS->getDimensionIdLen( j ), "dimension_id[i][j]");     
1158      }
1159      else
1160      {
1161        assert( pcVPS->getDimensionId( i, j ) ==  pcVPS->inferDimensionId( i, j )  );
1162      }
1163    }
1164  }
1165
1166  WRITE_CODE( pcVPS->getViewIdLen( ), 4, "view_id_len" );
1167
1168  if ( pcVPS->getViewIdLen( ) > 0 )
1169  {   
1170    for( Int i = 0; i < pcVPS->getNumViews(); i++ )
1171    {
1172      WRITE_CODE( pcVPS->getViewIdVal( i ), pcVPS->getViewIdLen( ), "view_id_val[i]" );
1173    }
1174  }
1175  else
1176  {
1177    for( Int i = 0; i < pcVPS->getNumViews(); i++ )
1178    {
1179      assert( pcVPS->getViewIdVal( i ) == 0 ); 
1180    }
1181  }
1182
1183
1184  for( Int i = 1; i <= pcVPS->getMaxLayersMinus1(); i++ )
1185  {
1186    for( Int j = 0; j < i; j++ )
1187    {
1188      WRITE_FLAG( pcVPS->getDirectDependencyFlag( i, j ),    "direct_dependency_flag[i][j]" );
1189    }
1190  }
1191
1192  if ( pcVPS->getNumIndependentLayers() > 1 ) 
1193  {
1194    WRITE_UVLC( pcVPS->getNumAddLayerSets( ), "num_add_layer_sets" );
1195  }
1196  for (Int i = 0; i < pcVPS->getNumAddLayerSets(); i++)
1197  {
1198    for (Int j = 1; j < pcVPS->getNumIndependentLayers(); j++)
1199    {
1200      WRITE_CODE( pcVPS->getHighestLayerIdxPlus1( i, j ), pcVPS->getHighestLayerIdxPlus1Len( j )  , "highest_layer_idx_plus1" );
1201    }
1202 
1203  }
1204
1205
1206  WRITE_FLAG( pcVPS->getVpsSubLayersMaxMinus1PresentFlag( ) ? 1 : 0 , "vps_sub_layers_max_minus1_present_flag" );
1207  if ( pcVPS->getVpsSubLayersMaxMinus1PresentFlag() )
1208  {
1209    for (Int i = 0; i <= pcVPS->getMaxLayersMinus1(); i++ )
1210    {
1211      WRITE_CODE( pcVPS->getSubLayersVpsMaxMinus1( i ), 3, "sub_layers_vps_max_minus1" );
1212      pcVPS->checkSubLayersVpsMaxMinus1( i ); 
1213    }
1214  } 
1215  else
1216  {
1217    for (Int i = 0; i <= pcVPS->getMaxLayersMinus1(); i++ )
1218    {
1219      assert( pcVPS->getSubLayersVpsMaxMinus1( i ) + 1 == pcVPS->getMaxTLayers( ) );   
1220    }
1221  }
1222  WRITE_FLAG( pcVPS->getMaxTidRefPresentFlag( ) ? 1 : 0 , "max_tid_ref_present_flag" );
1223
1224  if ( pcVPS->getMaxTidRefPresentFlag() )
1225  {   
1226    for( Int i = 0; i < pcVPS->getMaxLayersMinus1(); i++ )
1227    {
1228      for( Int j = i + 1; j <= pcVPS->getMaxLayersMinus1(); j++ )
1229      {
1230        if ( pcVPS->getDirectDependencyFlag(j,i) )
1231        {
1232          WRITE_CODE( pcVPS->getMaxTidIlRefPicsPlus1( i, j ), 3, "max_tid_il_ref_pics_plus1" );
1233        }
1234      }
1235    }
1236  }
1237
1238  WRITE_FLAG( pcVPS->getAllRefLayersActiveFlag( ) ? 1 : 0 , "all_ref_layers_active_flag" );
1239  WRITE_UVLC( pcVPS->getVpsNumProfileTierLevelMinus1( ), "vps_num_profile_tier_level_minus1" );
1240
1241  Int offsetVal =  ( pcVPS->getMaxLayersMinus1() > 0  &&  pcVPS->getVpsBaseLayerInternalFlag() ) ? 2 : 1;   
1242  for( Int i = pcVPS->getVpsBaseLayerInternalFlag() ? 2 : 1; i <= pcVPS->getVpsNumProfileTierLevelMinus1(); i++ )
1243  {
1244    WRITE_FLAG( pcVPS->getVpsProfilePresentFlag( i ) ? 1 : 0, "vps_profile_present_flag[i]" );
1245    codePTL( pcVPS->getPTL( offsetVal ), pcVPS->getVpsProfilePresentFlag( i ), pcVPS->getMaxTLayers() - 1 );
1246    offsetVal++;
1247  }
1248
1249
1250  if (pcVPS->getNumLayerSets() > 1)
1251  {
1252    WRITE_UVLC( pcVPS->getNumAddOlss( ), "num_add_olss" );
1253    WRITE_CODE( pcVPS->getDefaultOutputLayerIdc( ), 2, "default_output_layer_idc" );
1254  }
1255
1256  assert( pcVPS->getOutputLayerFlag(0, 0) == pcVPS->inferOutputLayerFlag( 0, 0 )); 
1257  assert( pcVPS->getLayerSetIdxForOlsMinus1( 0 ) == -1 ); 
1258
1259
1260  if (pcVPS->getVpsBaseLayerInternalFlag() )
1261  { 
1262    assert( pcVPS->getProfileTierLevelIdx(0,0) == pcVPS->inferProfileTierLevelIdx(0,0) );
1263  }
1264
1265
1266  for( Int i = 1; i < pcVPS->getNumOutputLayerSets( ); i++ )
1267  {
1268    if( pcVPS->getNumLayerSets() > 2 && i >= pcVPS->getNumLayerSets( ) )   
1269    {     
1270      WRITE_CODE( pcVPS->getLayerSetIdxForOlsMinus1( i ), pcVPS->getLayerSetIdxForOlsMinus1Len( i ) ,      "layer_set_idx_for_ols_minus1[i]" );
1271    }
1272
1273    if ( i > pcVPS->getVpsNumLayerSetsMinus1() || pcVPS->getDefaultOutputLayerIdc() == 2 )
1274    {       
1275      for( Int j = 0; j < pcVPS->getNumLayersInIdList( pcVPS->olsIdxToLsIdx( i ) ); j++ )
1276      {
1277        WRITE_FLAG( pcVPS->getOutputLayerFlag( i, j) ? 1 : 0, "output_layer_flag" );
1278      }             
1279    }
1280    else
1281    {
1282      for( Int j = 0; j < pcVPS->getNumLayersInIdList( pcVPS->olsIdxToLsIdx( i ) ) - 1; j++ )
1283      {             
1284        assert( pcVPS->getOutputLayerFlag( i , j ) == pcVPS->inferOutputLayerFlag( i, j )); 
1285      }
1286    }       
1287       
1288    for ( Int j = 0; j < pcVPS->getNumLayersInIdList( pcVPS->olsIdxToLsIdx(i)); j++ )
1289    {   
1290      if (pcVPS->getNecessaryLayerFlag( i, j ) && pcVPS->getVpsNumProfileTierLevelMinus1() > 0 )
1291      {
1292        WRITE_CODE( pcVPS->getProfileTierLevelIdx( i, j ), pcVPS->getProfileTierLevelIdxLen() ,"profile_tier_level_idx[ i ][ j ]" );   
1293      }
1294      if (pcVPS->getNecessaryLayerFlag( i, j ) && pcVPS->getVpsNumProfileTierLevelMinus1() == 0 )
1295      {
1296        assert( pcVPS->getProfileTierLevelIdx( i , j ) == pcVPS->inferProfileTierLevelIdx( i, j ) );
1297      }
1298
1299    }
1300    if( pcVPS->getNumOutputLayersInOutputLayerSet( i ) == 1 && pcVPS->getNumDirectRefLayers( pcVPS->getOlsHighestOutputLayerId( i ) ) > 0 )
1301    {
1302      WRITE_FLAG( pcVPS->getAltOutputLayerFlag( i ) ? 1 : 0 , "alt_output_layer_flag[ i ]" );
1303    }
1304  }
1305
1306  WRITE_UVLC( pcVPS->getVpsNumRepFormatsMinus1( ), "vps_num_rep_formats_minus1" );
1307
1308  for (Int i = 0; i <= pcVPS->getVpsNumRepFormatsMinus1(); i++ )
1309  {   
1310    const TComRepFormat* curRepFormat = pcVPS->getRepFormat(i);     
1311    const TComRepFormat* prevRepFormat = i > 0 ? pcVPS->getRepFormat( i - 1) : NULL; 
1312    codeRepFormat( i, curRepFormat ,  prevRepFormat); 
1313  }
1314
1315  if ( pcVPS->getVpsNumRepFormatsMinus1() > 0 )
1316  {
1317    WRITE_FLAG( pcVPS->getRepFormatIdxPresentFlag( ) ? 1 : 0 , "rep_format_idx_present_flag" );
1318  }
1319
1320  if( pcVPS->getRepFormatIdxPresentFlag() ) 
1321  {
1322    for( Int i = pcVPS->getVpsBaseLayerInternalFlag() ? 1 : 0; i <=  pcVPS->getMaxLayersMinus1(); i++ )
1323    {
1324        WRITE_CODE( pcVPS->getVpsRepFormatIdx(i), pcVPS->getVpsRepFormatIdxLen(), "vps_rep_format_idx[i]" );
1325    }
1326  }
1327  else
1328  {
1329    for( Int i = pcVPS->getVpsBaseLayerInternalFlag() ? 1 : 0; i <=  pcVPS->getMaxLayersMinus1(); i++ )
1330    {
1331      assert( pcVPS->getVpsRepFormatIdx( i ) ==  pcVPS->inferVpsRepFormatIdx( i ) );
1332    }
1333  }
1334
1335  WRITE_FLAG( pcVPS->getMaxOneActiveRefLayerFlag( ) ? 1 : 0, "max_one_active_ref_layer_flag" );
1336  WRITE_FLAG( pcVPS->getVpsPocLsbAlignedFlag( ) ? 1 : 0 , "vps_poc_lsb_aligned_flag" );
1337  for( Int i = 1; i  <=  pcVPS->getMaxLayersMinus1(); i++ )
1338  {
1339    if( pcVPS->getNumDirectRefLayers( pcVPS->getLayerIdInNuh( i ) )  ==  0 )
1340    {     
1341      WRITE_FLAG( pcVPS->getPocLsbNotPresentFlag( i ) ? 1 : 0 , "poc_lsb_not_present_flag" );
1342    }
1343  }
1344
1345  codeDpbSize( pcVPS ); 
1346
1347  WRITE_UVLC( pcVPS->getDirectDepTypeLenMinus2 ( ),         "direct_dep_type_len_minus2"); 
1348
1349  WRITE_FLAG( pcVPS->getDefaultDirectDependencyFlag( ) ? 1 : 0 , "default_direct_dependency_flag" );
1350  if ( pcVPS->getDefaultDirectDependencyFlag( ) )
1351  { 
1352    WRITE_CODE( pcVPS->getDefaultDirectDependencyType( ), pcVPS->getDirectDepTypeLenMinus2( ) + 2 , "default_direct_dependency_type" );   
1353  }
1354
1355  for( Int i = pcVPS->getVpsBaseLayerInternalFlag() ?  1 : 2; i <= pcVPS->getMaxLayersMinus1(); i++ )
1356  {
1357    for( Int j = pcVPS->getVpsBaseLayerInternalFlag() ?  0 : 1; j < i; j++ )
1358    {
1359      if (pcVPS->getDirectDependencyFlag( i, j) )
1360      {       
1361        if ( pcVPS->getDefaultDirectDependencyFlag( ) )
1362        { 
1363          assert( pcVPS->getDirectDependencyType( i, j ) == pcVPS->getDefaultDirectDependencyType( ) );
1364        }
1365        else
1366        {
1367          assert ( pcVPS->getDirectDependencyType( i, j ) != -1 ); 
1368          WRITE_CODE( pcVPS->getDirectDependencyType( i, j ),pcVPS->getDirectDepTypeLenMinus2( ) + 2,  "direct_dependency_type[i][j]" );
1369        }
1370      }
1371    }
1372  } 
1373  WRITE_UVLC( 0, "vps_non_vui_extension_length" );
1374 
1375
1376
1377
1378  WRITE_FLAG( pcVPS->getVpsVuiPresentFlag() ? 1 : 0 , "vps_vui_present_flag" );
1379  if( pcVPS->getVpsVuiPresentFlag() )
1380  {
1381    m_pcBitIf->writeAlignOne();  // vps_vui_alignment_bit_equal_to_one
1382    codeVPSVUI( pcVPS ); 
1383  }     
1384  else
1385  {
1386    const TComVPSVUI* pcVPSVUI = pcVPS->getVPSVUI( ); 
1387    assert( pcVPSVUI ); 
1388    assert( !pcVPSVUI->getCrossLayerIrapAlignedFlag() );     
1389  }
1390
1391}
1392
1393Void TEncCavlc::codeVideoSignalInfo( const TComVideoSignalInfo* pcVideoSignalInfo )
1394{
1395  assert( pcVideoSignalInfo ); 
1396  WRITE_CODE( pcVideoSignalInfo->getVideoVpsFormat( ), 3, "video_vps_format" );
1397  WRITE_FLAG( pcVideoSignalInfo->getVideoFullRangeVpsFlag( ) ? 1 : 0 , "video_full_range_vps_flag" );
1398  WRITE_CODE( pcVideoSignalInfo->getColourPrimariesVps( ), 8, "colour_primaries_vps" );
1399  WRITE_CODE( pcVideoSignalInfo->getTransferCharacteristicsVps( ), 8, "transfer_characteristics_vps" );
1400  WRITE_CODE( pcVideoSignalInfo->getMatrixCoeffsVps( ), 8, "matrix_coeffs_vps" );
1401}
1402
1403Void TEncCavlc::codeDpbSize( const TComVPS* vps )
1404{ 
1405  const TComDpbSize * dpbSize = vps->getDpbSize(); 
1406
1407  for( Int i = 1; i < vps->getNumOutputLayerSets(); i++ )
1408  { 
1409    Int currLsIdx = vps->olsIdxToLsIdx( i ); 
1410    WRITE_FLAG( dpbSize->getSubLayerFlagInfoPresentFlag( i ) ? 1 : 0 , "sub_layer_flag_info_present_flag" );
1411    for( Int j = 0; j  <=  vps->getMaxSubLayersInLayerSetMinus1( currLsIdx ); j++ )
1412    { 
1413      if( j > 0  &&  dpbSize->getSubLayerDpbInfoPresentFlag( i, j )  ) 
1414      {
1415        WRITE_FLAG( dpbSize->getSubLayerDpbInfoPresentFlag( i, j ) ? 1 : 0 , "sub_layer_dpb_info_present_flag" );
1416      }
1417      if( dpbSize->getSubLayerDpbInfoPresentFlag( i, j ) )
1418      { 
1419        for( Int k = 0; k < vps->getNumLayersInIdList( currLsIdx ); k++ )   
1420        {
1421          if ( vps->getNecessaryLayerFlag( i, k ) && ( vps->getVpsBaseLayerInternalFlag() || ( vps->getLayerSetLayerIdList(vps->olsIdxToLsIdx(i),k) != 0 ) ))
1422          {
1423            WRITE_UVLC( dpbSize->getMaxVpsDecPicBufferingMinus1( i, k, j ), "max_vps_dec_pic_buffering_minus1" );
1424          }
1425          else
1426          {
1427            if ( vps->getNecessaryLayerFlag( i, k ) && ( j == 0 ) && ( k == 0 ) ) 
1428            {
1429              assert( dpbSize->getMaxVpsDecPicBufferingMinus1(i ,k, j ) ==  0 );
1430            }
1431          }
1432        }
1433        WRITE_UVLC( dpbSize->getMaxVpsNumReorderPics( i, j ), "max_vps_num_reorder_pics" );
1434        WRITE_UVLC( dpbSize->getMaxVpsLatencyIncreasePlus1( i, j ), "max_vps_latency_increase_plus1" );
1435      }
1436      else
1437      {
1438        if ( j > 0 )
1439        {
1440          for( Int k = 0; k < vps->getNumLayersInIdList( vps->olsIdxToLsIdx( i ) ); k++ )   
1441          {
1442            if ( vps->getNecessaryLayerFlag(i, k ) )
1443            {           
1444              assert( dpbSize->getMaxVpsDecPicBufferingMinus1( i, k, j ) == dpbSize->getMaxVpsDecPicBufferingMinus1( i,k, j - 1 ) );
1445            }
1446          }
1447          assert( dpbSize->getMaxVpsNumReorderPics      ( i, j ) ==  dpbSize->getMaxVpsNumReorderPics      ( i, j - 1 ) );
1448          assert( dpbSize->getMaxVpsLatencyIncreasePlus1( i, j ) ==  dpbSize->getMaxVpsLatencyIncreasePlus1( i, j - 1 ) );
1449        }
1450      }
1451    }
1452  }
1453}
1454
1455Void TEncCavlc::codeRepFormat( Int i, const TComRepFormat* pcRepFormat, const TComRepFormat* pcPrevRepFormat )
1456{
1457  assert( pcRepFormat ); 
1458
1459  WRITE_CODE( pcRepFormat->getPicWidthVpsInLumaSamples( ),  16, "pic_width_vps_in_luma_samples" );
1460  WRITE_CODE( pcRepFormat->getPicHeightVpsInLumaSamples( ), 16, "pic_height_vps_in_luma_samples" );
1461  WRITE_FLAG( pcRepFormat->getChromaAndBitDepthVpsPresentFlag( ) ? 1 : 0 , "chroma_and_bit_depth_vps_present_flag" );
1462 
1463  pcRepFormat->checkChromaAndBitDepthVpsPresentFlag( i ); 
1464
1465  if ( pcRepFormat->getChromaAndBitDepthVpsPresentFlag() )
1466  { 
1467    WRITE_CODE( pcRepFormat->getChromaFormatVpsIdc( ), 2, "chroma_format_vps_idc" ); 
1468
1469    if ( pcRepFormat->getChromaFormatVpsIdc() == 3 )
1470    {
1471      WRITE_FLAG( pcRepFormat->getSeparateColourPlaneVpsFlag( ) ? 1 : 0 , "separate_colour_plane_vps_flag" );
1472    }
1473    WRITE_CODE( pcRepFormat->getBitDepthVpsLumaMinus8( ),      4, "bit_depth_vps_luma_minus8" );
1474    WRITE_CODE( pcRepFormat->getBitDepthVpsChromaMinus8( ),    4, "bit_depth_vps_chroma_minus8" );
1475  }
1476  else
1477  {
1478    pcRepFormat->checkInferChromaAndBitDepth(pcPrevRepFormat ); 
1479  }
1480  WRITE_FLAG( pcRepFormat->getConformanceWindowVpsFlag( ) ? 1 : 0 , "conformance_window_vps_flag" );
1481  if ( pcRepFormat->getConformanceWindowVpsFlag() )
1482  {   
1483    WRITE_UVLC( pcRepFormat->getConfWinVpsLeftOffset( ), "conf_win_vps_left_offset" );
1484    WRITE_UVLC( pcRepFormat->getConfWinVpsRightOffset( ), "conf_win_vps_right_offset" );
1485    WRITE_UVLC( pcRepFormat->getConfWinVpsTopOffset( ), "conf_win_vps_top_offset" );
1486    WRITE_UVLC( pcRepFormat->getConfWinVpsBottomOffset( ), "conf_win_vps_bottom_offset" );
1487  }
1488}
1489
1490Void TEncCavlc::codeVPSVUI( const TComVPS* pcVPS )
1491{
1492  assert( pcVPS ); 
1493
1494  const TComVPSVUI* pcVPSVUI = pcVPS->getVPSVUI( ); 
1495
1496  assert( pcVPSVUI ); 
1497
1498
1499  WRITE_FLAG( pcVPSVUI->getCrossLayerPicTypeAlignedFlag( ) ? 1 : 0 , "cross_layer_pic_type_aligned_flag" );
1500  if ( !pcVPSVUI->getCrossLayerPicTypeAlignedFlag() )
1501  { 
1502    WRITE_FLAG( pcVPSVUI->getCrossLayerIrapAlignedFlag( ) ? 1 : 0 , "cross_layer_irap_aligned_flag" );
1503  }
1504  if( pcVPSVUI->getCrossLayerIrapAlignedFlag( ) )
1505  {
1506    WRITE_FLAG( pcVPSVUI->getAllLayersIdrAlignedFlag( ) ? 1 : 0 , "all_layers_idr_aligned_flag" );
1507  }
1508  WRITE_FLAG( pcVPSVUI->getBitRatePresentVpsFlag( ) ? 1 : 0 , "bit_rate_present_vps_flag" );
1509  WRITE_FLAG( pcVPSVUI->getPicRatePresentVpsFlag( ) ? 1 : 0 , "pic_rate_present_vps_flag" );
1510  if( pcVPSVUI->getBitRatePresentVpsFlag( )  ||  pcVPSVUI->getPicRatePresentVpsFlag( ) )
1511  {
1512    for( Int i = pcVPS->getVpsBaseLayerInternalFlag() ? 0 : 1; i < pcVPS->getNumLayerSets(); i++ )
1513    {
1514      for( Int j = 0; j  <=  pcVPS->getMaxSubLayersInLayerSetMinus1( i ); j++ ) 
1515      {
1516        if( pcVPSVUI->getBitRatePresentVpsFlag( ) )
1517        {
1518          WRITE_FLAG( pcVPSVUI->getBitRatePresentFlag( i, j ) ? 1 : 0 , "bit_rate_present_flag" );
1519        }
1520        if( pcVPSVUI->getBitRatePresentVpsFlag( )  )
1521        {
1522          WRITE_FLAG( pcVPSVUI->getPicRatePresentFlag( i, j ) ? 1 : 0 , "pic_rate_present_flag" );
1523        }
1524        if( pcVPSVUI->getBitRatePresentFlag( i, j ) )
1525        {
1526          WRITE_CODE( pcVPSVUI->getAvgBitRate( i, j ), 16, "avg_bit_rate" );
1527          WRITE_CODE( pcVPSVUI->getMaxBitRate( i, j ), 16, "max_bit_rate" );
1528        }
1529        if( pcVPSVUI->getPicRatePresentFlag( i, j ) )
1530        {
1531          WRITE_CODE( pcVPSVUI->getConstantPicRateIdc( i, j ), 2, "constant_pic_rate_idc" );
1532          WRITE_CODE( pcVPSVUI->getAvgPicRate( i, j ), 16, "avg_pic_rate" );
1533        }
1534      }
1535    }
1536  }
1537
1538  WRITE_FLAG( pcVPSVUI->getVideoSignalInfoIdxPresentFlag( ) ? 1 : 0 , "video_signal_info_idx_present_flag" );
1539  if( pcVPSVUI->getVideoSignalInfoIdxPresentFlag() )
1540  {
1541    WRITE_CODE( pcVPSVUI->getVpsNumVideoSignalInfoMinus1( ), 4, "vps_num_video_signal_info_minus1" );
1542  }
1543  else
1544  {
1545    assert( pcVPSVUI->getVpsNumVideoSignalInfoMinus1() == pcVPS->getMaxLayersMinus1() - ( pcVPS->getVpsBaseLayerInternalFlag() ? 0 : 1)  ); 
1546  }
1547
1548  for( Int i = 0; i <= pcVPSVUI->getVpsNumVideoSignalInfoMinus1(); i++ )
1549  {
1550    assert( pcVPSVUI->getVideoSignalInfo( i ) != NULL ); 
1551    const TComVideoSignalInfo* curVideoSignalInfo = pcVPSVUI->getVideoSignalInfo( i ); 
1552    codeVideoSignalInfo( curVideoSignalInfo );     
1553
1554  }
1555
1556  if( pcVPSVUI->getVideoSignalInfoIdxPresentFlag() && pcVPSVUI->getVpsNumVideoSignalInfoMinus1() > 0 )
1557  {
1558    for( Int i = pcVPS->getVpsBaseLayerInternalFlag() ? 0 : 1; i <=  pcVPS->getMaxLayersMinus1(); i++ )
1559    {
1560      WRITE_CODE( pcVPSVUI->getVpsVideoSignalInfoIdx( i ), 4, "vps_video_signal_info_idx" );
1561    }
1562  }
1563  WRITE_FLAG( pcVPSVUI->getTilesNotInUseFlag( ) ? 1 : 0 , "tiles_not_in_use_flag" );
1564  if( !pcVPSVUI->getTilesNotInUseFlag() ) 
1565  {     
1566    for( Int i = pcVPS->getVpsBaseLayerInternalFlag() ? 0 : 1; i  <=  pcVPS->getMaxLayersMinus1(); i++ )
1567    {
1568      WRITE_FLAG( pcVPSVUI->getTilesInUseFlag( i ) ? 1 : 0 , "tiles_in_use_flag[i]" );
1569      if( pcVPSVUI->getTilesInUseFlag( i ) ) 
1570      {
1571        WRITE_FLAG( pcVPSVUI->getLoopFilterNotAcrossTilesFlag( i ) ? 1 : 0, "loop_filter_not_across_tiles_flag[i]" );
1572      }
1573    } 
1574    for( Int i = pcVPS->getVpsBaseLayerInternalFlag() ? 1 : 2; i  <=  pcVPS->getMaxLayersMinus1(); i++ ) 
1575    {
1576      for( Int j = 0; j < pcVPS->getNumDirectRefLayers( pcVPS->getLayerIdInNuh( i ) ) ; j++ )
1577      { 
1578        Int layerIdx = pcVPS->getLayerIdInVps(pcVPS->getIdRefLayer(pcVPS->getLayerIdInNuh( i ) , j  )); 
1579        if( pcVPSVUI->getTilesInUseFlag( i )  &&  pcVPSVUI->getTilesInUseFlag( layerIdx ) ) 
1580        {
1581          WRITE_FLAG( pcVPSVUI->getTileBoundariesAlignedFlag( i, j ) ? 1 : 0 , "tile_boundaries_aligned_flag[i][j]" );
1582        }
1583      } 
1584    }
1585  } 
1586
1587  WRITE_FLAG( pcVPSVUI->getWppNotInUseFlag( ) ? 1 : 0 , "wpp_not_in_use_flag" );
1588
1589  if( !pcVPSVUI->getWppNotInUseFlag( ) )
1590  {
1591    for( Int i = 0; i  <=  pcVPS->getMaxLayersMinus1(); i++ ) 
1592    {
1593      WRITE_FLAG( pcVPSVUI->getWppInUseFlag( i ) ? 1 : 0 , "wpp_in_use_flag[i]" );
1594    }
1595  }
1596  WRITE_FLAG( pcVPSVUI->getSingleLayerForNonIrapFlag( ) ? 1 : 0 , "single_layer_for_non_irap_flag" );
1597  WRITE_FLAG( pcVPSVUI->getHigherLayerIrapSkipFlag( ) ? 1 : 0 , "higher_layer_irap_skip_flag" );
1598  WRITE_FLAG( pcVPSVUI->getIlpRestrictedRefLayersFlag( ) ? 1 : 0 , "ilp_restricted_ref_layers_flag" );
1599
1600  if( pcVPSVUI->getIlpRestrictedRefLayersFlag( ) )
1601  {
1602    for( Int i = 1; i  <=  pcVPS->getMaxLayersMinus1(); i++ )
1603    {
1604      for( Int j = 0; j < pcVPS->getNumDirectRefLayers( pcVPS->getLayerIdInNuh( i ) ); j++ )
1605      {
1606        if( pcVPS->getVpsBaseLayerInternalFlag() || pcVPS->getIdRefLayer( pcVPS->getLayerIdInNuh( i ), j ) > 0 )
1607        {       
1608          WRITE_UVLC( pcVPSVUI->getMinSpatialSegmentOffsetPlus1( i, j ), "min_spatial_segment_offset_plus1" );
1609          if( pcVPSVUI->getMinSpatialSegmentOffsetPlus1( i, j ) > 0 )
1610          {
1611            WRITE_FLAG( pcVPSVUI->getCtuBasedOffsetEnabledFlag( i, j ) ? 1 : 0 , "ctu_based_offset_enabled_flag" );
1612            if( pcVPSVUI->getCtuBasedOffsetEnabledFlag( i, j ) )
1613            {
1614              WRITE_UVLC( pcVPSVUI->getMinHorizontalCtuOffsetPlus1( i, j ), "min_horizontal_ctu_offset_plus1" );
1615            }
1616          }
1617        }
1618      }
1619    }
1620  }
1621
1622
1623  WRITE_FLAG( pcVPSVUI->getVpsVuiBspHrdPresentFlag( ) ? 1 : 0 , "vps_vui_bsp_hrd_present_flag" );
1624  if ( pcVPSVUI->getVpsVuiBspHrdPresentFlag( ) )
1625  {
1626    codeVpsVuiBspHrdParameters( pcVPS ); 
1627  }
1628  for( Int i = 1; i  <=  pcVPS->getMaxLayersMinus1(); i++ )
1629  {
1630    if( pcVPS->getNumDirectRefLayers( pcVPS->getLayerIdInNuh( i )) == 0 ) 
1631    {
1632      WRITE_FLAG( pcVPSVUI->getBaseLayerParameterSetCompatibilityFlag( i ) ? 1 : 0 , "base_layer_parameter_set_compatibility_flag" );
1633    }
1634  }
1635}
1636
1637Void TEncCavlc::codeVpsVuiBspHrdParameters( const TComVPS* pcVPS )
1638{
1639  assert( pcVPS ); 
1640
1641  const TComVPSVUI* pcVPSVUI = pcVPS->getVPSVUI( ); 
1642
1643  assert( pcVPSVUI ); 
1644
1645  const TComVpsVuiBspHrdParameters*  vpsVuiBspHrdP = pcVPSVUI->getVpsVuiBspHrdParameters(); 
1646
1647  assert ( vpsVuiBspHrdP );
1648 
1649  WRITE_UVLC( vpsVuiBspHrdP->getVpsNumAddHrdParams( ), "vps_num_add_hrd_params" );
1650
1651  for( Int i = pcVPS->getNumHrdParameters(); i < pcVPS->getNumHrdParameters() + vpsVuiBspHrdP->getVpsNumAddHrdParams(); i++ )
1652  { 
1653    if( i > 0 ) 
1654    {
1655      WRITE_FLAG( vpsVuiBspHrdP->getCprmsAddPresentFlag( i ) ? 1 : 0 , "cprms_add_present_flag" );
1656    }
1657    WRITE_UVLC( vpsVuiBspHrdP->getNumSubLayerHrdMinus1( i ), "num_sub_layer_hrd_minus1" );
1658    const TComHRD* hrdParameters = vpsVuiBspHrdP->getHrdParametermeters( i ); 
1659    codeHrdParameters( hrdParameters, vpsVuiBspHrdP->getCprmsAddPresentFlag( i ), vpsVuiBspHrdP->getNumSubLayerHrdMinus1( i ) );     
1660  }
1661
1662  for( Int h = 1; h < pcVPS->getNumOutputLayerSets(); h++ )
1663  { 
1664    WRITE_UVLC( vpsVuiBspHrdP->getNumSignalledPartitioningSchemes( h ), "num_signalled_partitioning_schemes" );
1665
1666    for( Int j = 1; j < vpsVuiBspHrdP->getNumSignalledPartitioningSchemes( h ) + 1; j++ )
1667    { 
1668      WRITE_UVLC( vpsVuiBspHrdP->getNumPartitionsInSchemeMinus1( h, j ), "num_partitions_in_scheme_minus1" );
1669      for( Int k = 0; k  <=  vpsVuiBspHrdP->getNumPartitionsInSchemeMinus1( h, j ); k++ ) 
1670      {
1671        for( Int r = 0; r < pcVPS->getNumLayersInIdList(pcVPS->olsIdxToLsIdx( h ) )   ; r++ ) 
1672        {
1673          WRITE_FLAG( vpsVuiBspHrdP->getLayerIncludedInPartitionFlag( h, j, k, r ) ? 1 : 0 , "layer_included_in_partition_flag" );
1674        }
1675      }
1676    } 
1677    for( Int i = 0; i < vpsVuiBspHrdP->getNumSignalledPartitioningSchemes( h ) + 1; i++ ) 
1678    {
1679      for( Int t = 0; t  <=  pcVPS->getMaxSubLayersInLayerSetMinus1( pcVPS->olsIdxToLsIdx( h ) ); t++ )
1680      { 
1681        WRITE_UVLC( vpsVuiBspHrdP->getNumBspSchedulesMinus1( h, i, t ), "num_bsp_schedules_minus1" );
1682        for( Int j = 0; j  <=  vpsVuiBspHrdP->getNumBspSchedulesMinus1( h, i, t ); j++ ) 
1683        {
1684          for( Int k = 0; k  <=  vpsVuiBspHrdP->getNumPartitionsInSchemeMinus1( h, j ); k++ )
1685          { 
1686            WRITE_CODE( vpsVuiBspHrdP->getBspHrdIdx( h, i, t, j, k ), vpsVuiBspHrdP->getBspHrdIdxLen( pcVPS ), "bsp_hrd_idx" );
1687            WRITE_UVLC( vpsVuiBspHrdP->getBspSchedIdx( h, i, t, j, k ), "bsp_sched_idx" );
1688          } 
1689        }
1690      } 
1691    }
1692  } 
1693} 
1694#endif
1695
1696#if NH_3D
1697Void TEncCavlc::codeVPS3dExtension( const TComVPS* pcVPS )
1698{ 
1699  WRITE_UVLC( pcVPS->getCpPrecision( ), "cp_precision" );
1700  for (Int n = 1; n < pcVPS->getNumViews(); n++)
1701  {
1702    Int i      = pcVPS->getViewOIdxList( n );
1703    Int iInVps = pcVPS->getVoiInVps( i ); 
1704    WRITE_CODE( pcVPS->getNumCp( iInVps ), 6, "num_cp" );
1705
1706    if( pcVPS->getNumCp( iInVps ) > 0 )
1707    {
1708      WRITE_FLAG( pcVPS->getCpInSliceSegmentHeaderFlag( iInVps ) ? 1 : 0 , "cp_in_slice_segment_header_flag" );
1709      for( Int m = 0; m < pcVPS->getNumCp( iInVps ); m++ )
1710      {
1711        WRITE_UVLC( pcVPS->getCpRefVoi( iInVps, m ), "cp_ref_voi" );
1712        if( !pcVPS->getCpInSliceSegmentHeaderFlag( iInVps ) )
1713        {
1714          Int j      = pcVPS->getCpRefVoi( iInVps, m );
1715          Int jInVps = pcVPS->getVoiInVps( j ); 
1716          WRITE_SVLC( pcVPS->getVpsCpScale   ( iInVps, jInVps ), "vps_cp_scale" );
1717          WRITE_SVLC( pcVPS->getVpsCpOff     ( iInVps, jInVps ), "vps_cp_off" );
1718          WRITE_SVLC( pcVPS->getVpsCpInvScale( iInVps, jInVps ) + pcVPS->getVpsCpScale( iInVps, jInVps ), "vps_cp_inv_scale_plus_scale" );
1719          WRITE_SVLC( pcVPS->getVpsCpInvOff  ( iInVps, jInVps ) + pcVPS->getVpsCpOff  ( iInVps, jInVps ), "vps_cp_inv_off_plus_off" );
1720        }
1721      }
1722    }
1723  } 
1724}
1725#endif
1726
1727
1728Void TEncCavlc::codeSliceHeader         ( TComSlice* pcSlice )
1729{
1730#if NH_MV
1731  const TComVPS* vps = pcSlice->getVPS(); 
1732#endif
1733
1734#if ENC_DEC_TRACE
1735#if NH_MV
1736  tracePSHeader( "Slice", pcSlice->getLayerId() ); 
1737#else
1738  xTraceSliceHeader ();
1739#endif
1740#endif
1741
1742  const ChromaFormat format                = pcSlice->getSPS()->getChromaFormatIdc();
1743  const UInt         numberValidComponents = getNumberValidComponents(format);
1744  const Bool         chromaEnabled         = isChromaEnabled(format);
1745
1746  //calculate number of bits required for slice address
1747  Int maxSliceSegmentAddress = pcSlice->getPic()->getNumberOfCtusInFrame();
1748  Int bitsSliceSegmentAddress = 0;
1749  while(maxSliceSegmentAddress>(1<<bitsSliceSegmentAddress))
1750  {
1751    bitsSliceSegmentAddress++;
1752  }
1753  const Int ctuTsAddress = pcSlice->getSliceSegmentCurStartCtuTsAddr();
1754
1755  //write slice address
1756  const Int sliceSegmentRsAddress = pcSlice->getPic()->getPicSym()->getCtuTsToRsAddrMap(ctuTsAddress);
1757
1758  WRITE_FLAG( sliceSegmentRsAddress==0, "first_slice_segment_in_pic_flag" );
1759  if ( pcSlice->getRapPicFlag() )
1760  {
1761    WRITE_FLAG( pcSlice->getNoOutputPriorPicsFlag() ? 1 : 0, "no_output_of_prior_pics_flag" );
1762  }
1763#if PPS_FIX_DEPTH
1764  if( pcSlice->getIsDepth() )
1765  {
1766    WRITE_UVLC( 1, "slice_pic_parameter_set_id" );
1767  }
1768  else
1769#endif
1770
1771  WRITE_UVLC( pcSlice->getPPS()->getPPSId(), "slice_pic_parameter_set_id" );
1772  if ( pcSlice->getPPS()->getDependentSliceSegmentsEnabledFlag() && (sliceSegmentRsAddress!=0) )
1773  {
1774    WRITE_FLAG( pcSlice->getDependentSliceSegmentFlag() ? 1 : 0, "dependent_slice_segment_flag" );
1775  }
1776  if(sliceSegmentRsAddress>0)
1777  {
1778    WRITE_CODE( sliceSegmentRsAddress, bitsSliceSegmentAddress, "slice_segment_address" );
1779  }
1780  if ( !pcSlice->getDependentSliceSegmentFlag() )
1781  {
1782
1783#if NH_MV   
1784    Int esb = 0;  //Don't use i, otherwise will shadow something below
1785
1786    if ( pcSlice->getPPS()->getNumExtraSliceHeaderBits() > esb )
1787    {
1788      esb++; 
1789      WRITE_FLAG( pcSlice->getDiscardableFlag( ) ? 1 : 0 , "discardable_flag" );
1790      if (pcSlice->getDiscardableFlag( ))
1791      {
1792        assert(pcSlice->getNalUnitType() != NAL_UNIT_CODED_SLICE_TRAIL_R &&
1793          pcSlice->getNalUnitType() != NAL_UNIT_CODED_SLICE_TSA_R &&
1794          pcSlice->getNalUnitType() != NAL_UNIT_CODED_SLICE_STSA_R &&
1795          pcSlice->getNalUnitType() != NAL_UNIT_CODED_SLICE_RADL_R &&
1796          pcSlice->getNalUnitType() != NAL_UNIT_CODED_SLICE_RASL_R);
1797      }
1798    }
1799
1800    if ( pcSlice->getPPS()->getNumExtraSliceHeaderBits() > esb )
1801    {
1802      esb++; 
1803      WRITE_FLAG( pcSlice->getCrossLayerBlaFlag( ) ? 1 : 0 , "cross_layer_bla_flag" );
1804    }
1805    pcSlice->checkCrossLayerBlaFlag( ); 
1806
1807    for (; esb < pcSlice->getPPS()->getNumExtraSliceHeaderBits(); esb++)   
1808#else
1809    for (Int i = 0; i < pcSlice->getPPS()->getNumExtraSliceHeaderBits(); i++)
1810#endif
1811    {
1812      WRITE_FLAG(0, "slice_reserved_flag[]");
1813    }
1814
1815
1816
1817    WRITE_UVLC( pcSlice->getSliceType(),       "slice_type" );
1818
1819    if( pcSlice->getPPS()->getOutputFlagPresentFlag() )
1820    {
1821      WRITE_FLAG( pcSlice->getPicOutputFlag() ? 1 : 0, "pic_output_flag" );
1822    }
1823
1824#if NH_MV
1825    if ( (pcSlice->getLayerId() > 0 && !vps->getPocLsbNotPresentFlag( pcSlice->getLayerIdInVps())) || !pcSlice->getIdrPicFlag() )
1826    {
1827      Int picOrderCntLSB = (pcSlice->getPOC()-pcSlice->getLastIDR()+(1<<pcSlice->getSPS()->getBitsForPOC())) & ((1<<pcSlice->getSPS()->getBitsForPOC())-1);
1828      WRITE_CODE( picOrderCntLSB, pcSlice->getSPS()->getBitsForPOC(), "slice_pic_order_cnt_lsb");
1829      pcSlice->setSlicePicOrderCntLsb( picOrderCntLSB ); 
1830    }
1831
1832#endif
1833
1834    if( !pcSlice->getIdrPicFlag() )
1835    {
1836#if !NH_MV
1837      Int picOrderCntLSB = (pcSlice->getPOC()-pcSlice->getLastIDR()+(1<<pcSlice->getSPS()->getBitsForPOC())) & ((1<<pcSlice->getSPS()->getBitsForPOC())-1);
1838      WRITE_CODE( picOrderCntLSB, pcSlice->getSPS()->getBitsForPOC(), "slice_pic_order_cnt_lsb");
1839#endif
1840      const TComReferencePictureSet* rps = pcSlice->getRPS();
1841
1842      // check for bitstream restriction stating that:
1843      // If the current picture is a BLA or CRA picture, the value of NumPocTotalCurr shall be equal to 0.
1844      // Ideally this process should not be repeated for each slice in a picture
1845      if (pcSlice->isIRAP())
1846      {
1847        for (Int picIdx = 0; picIdx < rps->getNumberOfPictures(); picIdx++)
1848        {
1849          assert (!rps->getUsed(picIdx));
1850        }
1851      }
1852
1853      if(pcSlice->getRPSidx() < 0)
1854      {
1855        WRITE_FLAG( 0, "short_term_ref_pic_set_sps_flag");
1856        codeShortTermRefPicSet( rps, true, pcSlice->getSPS()->getRPSList()->getNumberOfReferencePictureSets());
1857      }
1858      else
1859      {
1860        WRITE_FLAG( 1, "short_term_ref_pic_set_sps_flag");
1861        Int numBits = 0;
1862        while ((1 << numBits) < pcSlice->getSPS()->getRPSList()->getNumberOfReferencePictureSets())
1863        {
1864          numBits++;
1865        }
1866        if (numBits > 0)
1867        {
1868          WRITE_CODE( pcSlice->getRPSidx(), numBits, "short_term_ref_pic_set_idx" );
1869        }
1870      }
1871      if(pcSlice->getSPS()->getLongTermRefsPresent())
1872      {
1873        Int numLtrpInSH = rps->getNumberOfLongtermPictures();
1874        Int ltrpInSPS[MAX_NUM_REF_PICS];
1875        Int numLtrpInSPS = 0;
1876        UInt ltrpIndex;
1877        Int counter = 0;
1878        // WARNING: The following code only works only if a matching long-term RPS is
1879        //          found in the SPS for ALL long-term pictures
1880        //          The problem is that the SPS coded long-term pictures are moved to the
1881        //          beginning of the list which causes a mismatch when no reference picture
1882        //          list reordering is used
1883        //          NB: Long-term coding is currently not supported in general by the HM encoder
1884        for(Int k = rps->getNumberOfPictures()-1; k > rps->getNumberOfPictures()-rps->getNumberOfLongtermPictures()-1; k--)
1885        {
1886          if (findMatchingLTRP(pcSlice, &ltrpIndex, rps->getPOC(k), rps->getUsed(k)))
1887          {
1888            ltrpInSPS[numLtrpInSPS] = ltrpIndex;
1889            numLtrpInSPS++;
1890          }
1891          else
1892          {
1893            counter++;
1894          }
1895        }
1896        numLtrpInSH -= numLtrpInSPS;
1897        // check that either all long-term pictures are coded in SPS or in slice header (no mixing)
1898        assert (numLtrpInSH==0 || numLtrpInSPS==0); 
1899
1900        Int bitsForLtrpInSPS = 0;
1901        while (pcSlice->getSPS()->getNumLongTermRefPicSPS() > (1 << bitsForLtrpInSPS))
1902        {
1903          bitsForLtrpInSPS++;
1904        }
1905        if (pcSlice->getSPS()->getNumLongTermRefPicSPS() > 0)
1906        {
1907          WRITE_UVLC( numLtrpInSPS, "num_long_term_sps");
1908        }
1909        WRITE_UVLC( numLtrpInSH, "num_long_term_pics");
1910        // Note that the LSBs of the LT ref. pic. POCs must be sorted before.
1911        // Not sorted here because LT ref indices will be used in setRefPicList()
1912        Int prevDeltaMSB = 0, prevLSB = 0;
1913        Int offset = rps->getNumberOfNegativePictures() + rps->getNumberOfPositivePictures();
1914        counter = 0;
1915        // Warning: If some pictures are moved to ltrpInSPS, i is referring to a wrong index
1916        //          (mapping would be required)
1917        for(Int i=rps->getNumberOfPictures()-1 ; i > offset-1; i--, counter++)
1918        {
1919          if (counter < numLtrpInSPS)
1920          {
1921            if (bitsForLtrpInSPS > 0)
1922            {
1923              WRITE_CODE( ltrpInSPS[counter], bitsForLtrpInSPS, "lt_idx_sps[i]");
1924            }
1925          }
1926          else
1927          {
1928            WRITE_CODE( rps->getPocLSBLT(i), pcSlice->getSPS()->getBitsForPOC(), "poc_lsb_lt");
1929            WRITE_FLAG( rps->getUsed(i), "used_by_curr_pic_lt_flag");
1930          }
1931          WRITE_FLAG( rps->getDeltaPocMSBPresentFlag(i), "delta_poc_msb_present_flag");
1932
1933          if(rps->getDeltaPocMSBPresentFlag(i))
1934          {
1935            Bool deltaFlag = false;
1936            //  First LTRP from SPS                 ||  First LTRP from SH                              || curr LSB            != prev LSB
1937            if( (i == rps->getNumberOfPictures()-1) || (i == rps->getNumberOfPictures()-1-numLtrpInSPS) || (rps->getPocLSBLT(i) != prevLSB) )
1938            {
1939              deltaFlag = true;
1940            }
1941            if(deltaFlag)
1942            {
1943              WRITE_UVLC( rps->getDeltaPocMSBCycleLT(i), "delta_poc_msb_cycle_lt[i]" );
1944            }
1945            else
1946            {
1947              Int differenceInDeltaMSB = rps->getDeltaPocMSBCycleLT(i) - prevDeltaMSB;
1948              assert(differenceInDeltaMSB >= 0);
1949              WRITE_UVLC( differenceInDeltaMSB, "delta_poc_msb_cycle_lt[i]" );
1950            }
1951            prevLSB = rps->getPocLSBLT(i);
1952            prevDeltaMSB = rps->getDeltaPocMSBCycleLT(i);
1953          }
1954        }
1955      }
1956      if (pcSlice->getSPS()->getTMVPFlagsPresent())
1957      {
1958#if NH_MV
1959        WRITE_FLAG( pcSlice->getEnableTMVPFlag() ? 1 : 0, "slice_temporal_mvp_enabled_flag" );
1960#else
1961        WRITE_FLAG( pcSlice->getEnableTMVPFlag() ? 1 : 0, "slice_temporal_mvp_enabled_flag" );
1962#endif
1963      }
1964    }
1965#if NH_MV
1966    Bool interLayerPredLayerIdcPresentFlag = false; 
1967    Int layerId = pcSlice->getLayerId(); 
1968#if NH_3D
1969    if( pcSlice->getLayerId() > 0 && !vps->getAllRefLayersActiveFlag() && vps->getNumRefListLayers( layerId ) > 0 )
1970#else
1971    if( pcSlice->getLayerId() > 0 && !vps->getAllRefLayersActiveFlag() && vps->getNumDirectRefLayers( layerId ) > 0 )
1972#endif
1973    {   
1974      WRITE_FLAG( pcSlice->getInterLayerPredEnabledFlag( ) ? 1 : 0 , "inter_layer_pred_enabled_flag" );
1975#if NH_3D
1976      if( pcSlice->getInterLayerPredEnabledFlag() && vps->getNumRefListLayers( layerId ) > 1 )
1977#else
1978      if( pcSlice->getInterLayerPredEnabledFlag() && vps->getNumDirectRefLayers( layerId ) > 1 )
1979#endif
1980      {           
1981        if( !vps->getMaxOneActiveRefLayerFlag()) 
1982        {
1983          WRITE_CODE( pcSlice->getNumInterLayerRefPicsMinus1( ), pcSlice->getNumInterLayerRefPicsMinus1Len( ), "num_inter_layer_ref_pics_minus1" );
1984        }
1985#if NH_3D
1986        if ( pcSlice->getNumActiveRefLayerPics() != vps->getNumRefListLayers( layerId ) )
1987#else
1988        if ( pcSlice->getNumActiveRefLayerPics() != vps->getNumDirectRefLayers( layerId ) )
1989#endif
1990        {       
1991          interLayerPredLayerIdcPresentFlag = true; 
1992          for( Int idx = 0; idx < pcSlice->getNumActiveRefLayerPics(); idx++ )   
1993          {
1994            WRITE_CODE( pcSlice->getInterLayerPredLayerIdc( idx ), pcSlice->getInterLayerPredLayerIdcLen( ), "inter_layer_pred_layer_idc" );
1995          }
1996        }
1997      } 
1998    }
1999    if ( !interLayerPredLayerIdcPresentFlag )
2000    {
2001      for( Int i = 0; i < pcSlice->getNumActiveRefLayerPics(); i++ )   
2002      {
2003        assert( pcSlice->getInterLayerPredLayerIdc( i ) == pcSlice->getRefLayerPicIdc( i ) );
2004      }
2005    }
2006#endif
2007
2008#if NH_3D     
2009  if( getEncTop()->decProcAnnexI() )
2010  {
2011      if ( pcSlice->getInCmpPredAvailFlag() )
2012      {
2013        WRITE_FLAG( pcSlice->getInCompPredFlag(), "in_comp_pred_flag" );
2014      }
2015  }
2016#endif
2017
2018    if(pcSlice->getSPS()->getUseSAO())
2019    {
2020       WRITE_FLAG( pcSlice->getSaoEnabledFlag(CHANNEL_TYPE_LUMA), "slice_sao_luma_flag" );
2021       if (chromaEnabled)
2022       {
2023         WRITE_FLAG( pcSlice->getSaoEnabledFlag(CHANNEL_TYPE_CHROMA), "slice_sao_chroma_flag" );
2024       }
2025    }
2026
2027    //check if numrefidxes match the defaults. If not, override
2028
2029    if (!pcSlice->isIntra())
2030    {
2031      Bool overrideFlag = (pcSlice->getNumRefIdx( REF_PIC_LIST_0 )!=pcSlice->getPPS()->getNumRefIdxL0DefaultActive()||(pcSlice->isInterB()&&pcSlice->getNumRefIdx( REF_PIC_LIST_1 )!=pcSlice->getPPS()->getNumRefIdxL1DefaultActive()));
2032#if PPS_FIX_DEPTH
2033      overrideFlag |= (pcSlice->getIsDepth() && !pcSlice->getViewIndex());
2034#endif
2035      WRITE_FLAG( overrideFlag ? 1 : 0,                               "num_ref_idx_active_override_flag");
2036      if (overrideFlag)
2037      {
2038        WRITE_UVLC( pcSlice->getNumRefIdx( REF_PIC_LIST_0 ) - 1,      "num_ref_idx_l0_active_minus1" );
2039        if (pcSlice->isInterB())
2040        {
2041          WRITE_UVLC( pcSlice->getNumRefIdx( REF_PIC_LIST_1 ) - 1,    "num_ref_idx_l1_active_minus1" );
2042        }
2043        else
2044        {
2045          pcSlice->setNumRefIdx(REF_PIC_LIST_1, 0);
2046        }
2047      }
2048    }
2049    else
2050    {
2051      pcSlice->setNumRefIdx(REF_PIC_LIST_0, 0);
2052      pcSlice->setNumRefIdx(REF_PIC_LIST_1, 0);
2053    }
2054#if PPS_FIX_DEPTH
2055    if( (pcSlice->getPPS()->getListsModificationPresentFlag() || (pcSlice->getIsDepth() && !pcSlice->getViewIndex())) && pcSlice->getNumRpsCurrTempList() > 1)
2056#else
2057    if( pcSlice->getPPS()->getListsModificationPresentFlag() && pcSlice->getNumRpsCurrTempList() > 1)
2058#endif
2059    {
2060      TComRefPicListModification* refPicListModification = pcSlice->getRefPicListModification();
2061      if(!pcSlice->isIntra())
2062      {
2063        WRITE_FLAG(pcSlice->getRefPicListModification()->getRefPicListModificationFlagL0() ? 1 : 0,       "ref_pic_list_modification_flag_l0" );
2064        if (pcSlice->getRefPicListModification()->getRefPicListModificationFlagL0())
2065        {
2066          Int numRpsCurrTempList0 = pcSlice->getNumRpsCurrTempList();
2067          if (numRpsCurrTempList0 > 1)
2068          {
2069            Int length = 1;
2070            numRpsCurrTempList0 --;
2071            while ( numRpsCurrTempList0 >>= 1)
2072            {
2073              length ++;
2074            }
2075            for(Int i = 0; i < pcSlice->getNumRefIdx( REF_PIC_LIST_0 ); i++)
2076            {
2077              WRITE_CODE( refPicListModification->getRefPicSetIdxL0(i), length, "list_entry_l0");
2078            }
2079          }
2080        }
2081      }
2082      if(pcSlice->isInterB())
2083      {
2084        WRITE_FLAG(pcSlice->getRefPicListModification()->getRefPicListModificationFlagL1() ? 1 : 0,       "ref_pic_list_modification_flag_l1" );
2085        if (pcSlice->getRefPicListModification()->getRefPicListModificationFlagL1())
2086        {
2087          Int numRpsCurrTempList1 = pcSlice->getNumRpsCurrTempList();
2088          if ( numRpsCurrTempList1 > 1 )
2089          {
2090            Int length = 1;
2091            numRpsCurrTempList1 --;
2092            while ( numRpsCurrTempList1 >>= 1)
2093            {
2094              length ++;
2095            }
2096            for(Int i = 0; i < pcSlice->getNumRefIdx( REF_PIC_LIST_1 ); i++)
2097            {
2098              WRITE_CODE( refPicListModification->getRefPicSetIdxL1(i), length, "list_entry_l1");
2099            }
2100          }
2101        }
2102      }
2103    }
2104
2105    if (pcSlice->isInterB())
2106    {
2107      WRITE_FLAG( pcSlice->getMvdL1ZeroFlag() ? 1 : 0,   "mvd_l1_zero_flag");
2108    }
2109
2110    if(!pcSlice->isIntra())
2111    {
2112      if (!pcSlice->isIntra() && pcSlice->getPPS()->getCabacInitPresentFlag())
2113      {
2114        SliceType sliceType   = pcSlice->getSliceType();
2115        SliceType  encCABACTableIdx = pcSlice->getEncCABACTableIdx();
2116        Bool encCabacInitFlag = (sliceType!=encCABACTableIdx && encCABACTableIdx!=I_SLICE) ? true : false;
2117        pcSlice->setCabacInitFlag( encCabacInitFlag );
2118        WRITE_FLAG( encCabacInitFlag?1:0, "cabac_init_flag" );
2119      }
2120    }
2121
2122    if ( pcSlice->getEnableTMVPFlag() )
2123    {
2124      if ( pcSlice->getSliceType() == B_SLICE )
2125      {
2126        WRITE_FLAG( pcSlice->getColFromL0Flag(), "collocated_from_l0_flag" );
2127      }
2128
2129      if ( pcSlice->getSliceType() != I_SLICE &&
2130        ((pcSlice->getColFromL0Flag()==1 && pcSlice->getNumRefIdx(REF_PIC_LIST_0)>1)||
2131        (pcSlice->getColFromL0Flag()==0  && pcSlice->getNumRefIdx(REF_PIC_LIST_1)>1)))
2132      {
2133        WRITE_UVLC( pcSlice->getColRefIdx(), "collocated_ref_idx" );
2134      }
2135    }
2136    if ( (pcSlice->getPPS()->getUseWP() && pcSlice->getSliceType()==P_SLICE) || (pcSlice->getPPS()->getWPBiPred() && pcSlice->getSliceType()==B_SLICE) )
2137    {
2138      xCodePredWeightTable( pcSlice );
2139    }
2140#if H_3D_IC
2141    else if( pcSlice->getViewIndex() && ( pcSlice->getSliceType() == P_SLICE || pcSlice->getSliceType() == B_SLICE )
2142      && !pcSlice->getIsDepth() && vps->getNumRefListLayers( layerId ) > 0 
2143      && getEncTop()->decProcAnnexI()       
2144      )
2145    {
2146      WRITE_FLAG( pcSlice->getApplyIC() ? 1 : 0, "slice_ic_enable_flag" );
2147      if( pcSlice->getApplyIC() )
2148      {
2149        WRITE_FLAG( pcSlice->getIcSkipParseFlag() ? 1 : 0, "ic_skip_mergeidx0" );
2150      }
2151    }
2152#endif
2153#if H_3D_IV_MERGE
2154    assert(pcSlice->getMaxNumMergeCand()<=MRG_MAX_NUM_CANDS_MEM);
2155#else
2156    assert(pcSlice->getMaxNumMergeCand()<=MRG_MAX_NUM_CANDS);
2157#endif
2158    if (!pcSlice->isIntra())
2159    {
2160#if H_3D_IV_MERGE
2161      WRITE_UVLC( ( ( pcSlice->getMpiFlag( ) || pcSlice->getIvMvPredFlag( ) || pcSlice->getViewSynthesisPredFlag( ) ) ? MRG_MAX_NUM_CANDS_MEM : MRG_MAX_NUM_CANDS ) - pcSlice->getMaxNumMergeCand(), "five_minus_max_num_merge_cand");
2162#else
2163      WRITE_UVLC(MRG_MAX_NUM_CANDS - pcSlice->getMaxNumMergeCand(), "five_minus_max_num_merge_cand");
2164#endif
2165    }
2166    Int iCode = pcSlice->getSliceQp() - ( pcSlice->getPPS()->getPicInitQPMinus26() + 26 );
2167    WRITE_SVLC( iCode, "slice_qp_delta" );
2168    if (pcSlice->getPPS()->getSliceChromaQpFlag())
2169    {
2170      if (numberValidComponents > COMPONENT_Cb)
2171      {
2172        WRITE_SVLC( pcSlice->getSliceChromaQpDelta(COMPONENT_Cb), "slice_cb_qp_offset" );
2173      }
2174      if (numberValidComponents > COMPONENT_Cr)
2175      {
2176        WRITE_SVLC( pcSlice->getSliceChromaQpDelta(COMPONENT_Cr), "slice_cr_qp_offset" );
2177      }
2178      assert(numberValidComponents <= COMPONENT_Cr+1);
2179    }
2180
2181    if (pcSlice->getPPS()->getPpsRangeExtension().getChromaQpOffsetListEnabledFlag())
2182    {
2183      WRITE_FLAG(pcSlice->getUseChromaQpAdj(), "cu_chroma_qp_offset_enabled_flag");
2184    }
2185
2186    if (pcSlice->getPPS()->getDeblockingFilterControlPresentFlag())
2187    {
2188      if (pcSlice->getPPS()->getDeblockingFilterOverrideEnabledFlag() )
2189      {
2190        WRITE_FLAG(pcSlice->getDeblockingFilterOverrideFlag(), "deblocking_filter_override_flag");
2191      }
2192      if (pcSlice->getDeblockingFilterOverrideFlag())
2193      {
2194        WRITE_FLAG(pcSlice->getDeblockingFilterDisable(), "slice_disable_deblocking_filter_flag");
2195        if(!pcSlice->getDeblockingFilterDisable())
2196        {
2197          WRITE_SVLC (pcSlice->getDeblockingFilterBetaOffsetDiv2(), "slice_beta_offset_div2");
2198          WRITE_SVLC (pcSlice->getDeblockingFilterTcOffsetDiv2(),   "slice_tc_offset_div2");
2199        }
2200      }
2201    }
2202
2203    Bool isSAOEnabled = pcSlice->getSPS()->getUseSAO() && (pcSlice->getSaoEnabledFlag(CHANNEL_TYPE_LUMA) || (chromaEnabled && pcSlice->getSaoEnabledFlag(CHANNEL_TYPE_CHROMA)));
2204    Bool isDBFEnabled = (!pcSlice->getDeblockingFilterDisable());
2205
2206    if(pcSlice->getPPS()->getLoopFilterAcrossSlicesEnabledFlag() && ( isSAOEnabled || isDBFEnabled ))
2207    {
2208      WRITE_FLAG(pcSlice->getLFCrossSliceBoundaryFlag()?1:0, "slice_loop_filter_across_slices_enabled_flag");
2209    }
2210#if NH_3D
2211    if (getEncTop()->decProcAnnexI() )
2212    {
2213      Int voiInVps = vps->getVoiInVps( pcSlice->getViewIndex() ); 
2214      if( vps->getCpInSliceSegmentHeaderFlag( voiInVps ) && !pcSlice->getIsDepth() )
2215      {
2216        for( Int m = 0; m < vps->getNumCp( voiInVps ); m++ )
2217        {
2218          Int jInVps = vps->getVoiInVps( vps->getCpRefVoi( voiInVps, m ));
2219          WRITE_SVLC( pcSlice->getCpScale   ( jInVps )   , "cp_scale" );
2220          WRITE_SVLC( pcSlice->getCpOff     ( jInVps )   , "cp_off" );
2221          WRITE_SVLC( pcSlice->getCpInvScale( jInVps ) + pcSlice->getCpScale( jInVps ) , "cp_inv_scale_plus_scale" );
2222          WRITE_SVLC( pcSlice->getCpInvOff  ( jInVps ) + pcSlice->getCpOff  ( jInVps ) , "cp_inv_off_plus_off" );
2223        }
2224      }
2225    }
2226#endif
2227
2228  }
2229#if !NH_MV
2230  if(pcSlice->getPPS()->getSliceHeaderExtensionPresentFlag())
2231  {
2232    WRITE_UVLC(0,"slice_segment_header_extension_length");
2233  }
2234#else
2235  if(pcSlice->getPPS()->getSliceHeaderExtensionPresentFlag())
2236  {
2237    // Derive the value of PocMsbValRequiredFlag
2238
2239    // Determine value of SH extension length.
2240    Int shExtnLengthInBit = 0;
2241    if (pcSlice->getPPS()->getPocResetInfoPresentFlag())
2242    {
2243      shExtnLengthInBit += 2;
2244    }
2245    if (pcSlice->getPocResetIdc() > 0)
2246    {
2247      shExtnLengthInBit += 6;
2248    }
2249    if (pcSlice->getPocResetIdc() == 3)
2250    {
2251      shExtnLengthInBit += (pcSlice->getSPS()->getBitsForPOC() + 1);
2252    }
2253
2254
2255    if( !pcSlice->getPocMsbValRequiredFlag() &&  pcSlice->getVPS()->getVpsPocLsbAlignedFlag() )
2256    {
2257      shExtnLengthInBit++;    // For poc_msb_val_present_flag
2258    }
2259    else
2260    {
2261      if( pcSlice->getPocMsbValRequiredFlag() )
2262      {
2263        pcSlice->setPocMsbValPresentFlag( true );
2264      }
2265      else
2266      {
2267        pcSlice->setPocMsbValPresentFlag( false );
2268      }
2269    }
2270
2271    if( pcSlice->getPocMsbValPresentFlag() )
2272    {
2273//      Int iMaxPOClsb = 1<< pcSlice->getSPS()->getBitsForPOC(); currently unused
2274
2275      UInt lengthVal = 1;
2276      UInt tempVal = pcSlice->getPocMsbVal() + 1;
2277      assert ( tempVal );
2278      while( 1 != tempVal )
2279      {
2280        tempVal >>= 1;
2281        lengthVal += 2;
2282      }
2283      shExtnLengthInBit += lengthVal;
2284    }
2285    Int shExtnAdditionalBits = 0;
2286    if(shExtnLengthInBit % 8 != 0)
2287    {
2288      shExtnAdditionalBits = 8 - (shExtnLengthInBit % 8);
2289    }
2290    pcSlice->setSliceSegmentHeaderExtensionLength((shExtnLengthInBit + shExtnAdditionalBits) / 8);
2291     
2292
2293    WRITE_UVLC( pcSlice->getSliceSegmentHeaderExtensionLength( ), "slice_segment_header_extension_length" );
2294    UInt posFollSliceSegHeaderExtLen = m_pcBitIf->getNumberOfWrittenBits();
2295    if( pcSlice->getPPS()->getPocResetInfoPresentFlag() )
2296    {
2297      WRITE_CODE( pcSlice->getPocResetIdc( ), 2, "poc_reset_idc" );
2298    }
2299    else
2300    {
2301      assert( pcSlice->getPocResetIdc( ) == 0 );
2302    }
2303
2304    pcSlice->checkPocResetIdc(); 
2305
2306    if( pcSlice->getPocResetIdc() !=  0 )
2307    {
2308      WRITE_CODE( pcSlice->getPocResetPeriodId( ), 6, "poc_reset_period_id" );
2309    }
2310   
2311    if( pcSlice->getPocResetIdc() ==  3 ) 
2312    {
2313      WRITE_FLAG( pcSlice->getFullPocResetFlag( ) ? 1 : 0 , "full_poc_reset_flag" );
2314      WRITE_CODE( pcSlice->getPocLsbVal( ), pcSlice->getPocLsbValLen() , "poc_lsb_val" );
2315    }             
2316    pcSlice->checkPocLsbVal(); 
2317
2318    if( !pcSlice->getPocMsbValRequiredFlag() &&  pcSlice->getVPS()->getVpsPocLsbAlignedFlag()  )
2319    {
2320      WRITE_FLAG( pcSlice->getPocMsbValPresentFlag( ) ? 1 : 0 , "poc_msb_val_present_flag" );
2321    }
2322    else
2323    {
2324      assert( pcSlice->getPocMsbValPresentFlag() ==  pcSlice->inferPocMsbValPresentFlag( ) ); 
2325    }
2326   
2327    if( pcSlice->getPocMsbValPresentFlag() )
2328    {
2329      WRITE_UVLC( pcSlice->getPocMsbVal( ), "poc_msb_val" );
2330    }
2331   
2332    while( ( m_pcBitIf->getNumberOfWrittenBits() - posFollSliceSegHeaderExtLen ) < pcSlice->getSliceSegmentHeaderExtensionLength() * 8 )
2333    {
2334      WRITE_FLAG( 0, "slice_segment_header_extension_data_bit" );
2335    }
2336
2337    assert( ( m_pcBitIf->getNumberOfWrittenBits() - posFollSliceSegHeaderExtLen ) == pcSlice->getSliceSegmentHeaderExtensionLength() * 8  ); 
2338  }
2339#endif
2340
2341}
2342
2343Void TEncCavlc::codePTL( const TComPTL* pcPTL, Bool profilePresentFlag, Int maxNumSubLayersMinus1)
2344{
2345  if(profilePresentFlag)
2346  {
2347    codeProfileTier(pcPTL->getGeneralPTL(), false);    // general_...
2348  }
2349  WRITE_CODE( Int(pcPTL->getGeneralPTL()->getLevelIdc()), 8, "general_level_idc" );
2350
2351  for (Int i = 0; i < maxNumSubLayersMinus1; i++)
2352  {
2353#if !NH_MV
2354#endif
2355    WRITE_FLAG( pcPTL->getSubLayerProfilePresentFlag(i), "sub_layer_profile_present_flag[i]" );
2356#if !NH_MV
2357#endif
2358    WRITE_FLAG( pcPTL->getSubLayerLevelPresentFlag(i),   "sub_layer_level_present_flag[i]" );
2359  }
2360
2361  if (maxNumSubLayersMinus1 > 0)
2362  {
2363    for (Int i = maxNumSubLayersMinus1; i < 8; i++)
2364    {
2365      WRITE_CODE(0, 2, "reserved_zero_2bits");
2366    }
2367  }
2368
2369  for(Int i = 0; i < maxNumSubLayersMinus1; i++)
2370  {
2371    if( pcPTL->getSubLayerProfilePresentFlag(i) )
2372    {
2373      codeProfileTier(pcPTL->getSubLayerPTL(i), true);  // sub_layer_...
2374    }
2375    if( pcPTL->getSubLayerLevelPresentFlag(i) )
2376    {
2377      WRITE_CODE( Int(pcPTL->getSubLayerPTL(i)->getLevelIdc()), 8, "sub_layer_level_idc[i]" );
2378    }
2379  }
2380}
2381
2382#if ENC_DEC_TRACE || RExt__DECODER_DEBUG_BIT_STATISTICS
2383Void TEncCavlc::codeProfileTier( const ProfileTierLevel* ptl, const Bool bIsSubLayer )
2384#define PTL_TRACE_TEXT(txt) bIsSubLayer?("sub_layer_" txt) : ("general_" txt)
2385#else
2386Void TEncCavlc::codeProfileTier( const ProfileTierLevel* ptl, const Bool /*bIsSubLayer*/ )
2387#define PTL_TRACE_TEXT(txt) txt
2388#endif
2389{
2390  WRITE_CODE( ptl->getProfileSpace(), 2 ,      PTL_TRACE_TEXT("profile_space"                   ));
2391  WRITE_FLAG( ptl->getTierFlag()==Level::HIGH, PTL_TRACE_TEXT("tier_flag"                       ));
2392  WRITE_CODE( Int(ptl->getProfileIdc()), 5 ,   PTL_TRACE_TEXT("profile_idc"                     ));
2393  for(Int j = 0; j < 32; j++)
2394  {
2395    WRITE_FLAG( ptl->getProfileCompatibilityFlag(j), PTL_TRACE_TEXT("profile_compatibility_flag[][j]" ));
2396  }
2397
2398  WRITE_FLAG(ptl->getProgressiveSourceFlag(),   PTL_TRACE_TEXT("progressive_source_flag"         ));
2399  WRITE_FLAG(ptl->getInterlacedSourceFlag(),    PTL_TRACE_TEXT("interlaced_source_flag"          ));
2400  WRITE_FLAG(ptl->getNonPackedConstraintFlag(), PTL_TRACE_TEXT("non_packed_constraint_flag"      ));
2401  WRITE_FLAG(ptl->getFrameOnlyConstraintFlag(), PTL_TRACE_TEXT("frame_only_constraint_flag"      ));
2402
2403#if NH_MV
2404  if( ptl->getV2ConstraintsPresentFlag() ) 
2405  {
2406    WRITE_FLAG( ptl->getMax12bitConstraintFlag( ) ? 1 : 0 , "max_12bit_constraint_flag" );
2407    WRITE_FLAG( ptl->getMax10bitConstraintFlag( ) ? 1 : 0 , "max_10bit_constraint_flag" );
2408    WRITE_FLAG( ptl->getMax8bitConstraintFlag( ) ? 1 : 0 , "max_8bit_constraint_flag" );
2409    WRITE_FLAG( ptl->getMax422chromaConstraintFlag( ) ? 1 : 0 , "max_422chroma_constraint_flag" );
2410    WRITE_FLAG( ptl->getMax420chromaConstraintFlag( ) ? 1 : 0 , "max_420chroma_constraint_flag" );
2411    WRITE_FLAG( ptl->getMaxMonochromeConstraintFlag( ) ? 1 : 0 , "max_monochrome_constraint_flag" );
2412    WRITE_FLAG( ptl->getIntraConstraintFlag( ) ? 1 : 0 , "intra_constraint_flag" );
2413    WRITE_FLAG( ptl->getOnePictureOnlyConstraintFlag( ) ? 1 : 0 , "one_picture_only_constraint_flag" );
2414    WRITE_FLAG( ptl->getLowerBitRateConstraintFlag( ) ? 1 : 0 , "lower_bit_rate_constraint_flag" );
2415    WRITE_CODE( 0, 16, "XXX_reserved_zero_34bits[0..15]");
2416    WRITE_CODE( 0, 16, "XXX_reserved_zero_34bits[16..31]");
2417    WRITE_CODE( 0, 2 , "XXX_reserved_zero_34bits[32..33]");
2418  }
2419  else
2420  {
2421    WRITE_CODE( 0, 16, "XXX_reserved_zero_43bits[0..15]");
2422    WRITE_CODE( 0, 16, "XXX_reserved_zero_43bits[16..31]");
2423    WRITE_CODE( 0, 11, "XXX_reserved_zero_43bits[32..42]");
2424  }
2425    if( ptl->getInbldPresentFlag() )
2426  {
2427    WRITE_FLAG( ptl->getInbldFlag( ) ? 1 : 0 , "inbld_flag" );
2428  }
2429  else
2430  {
2431    WRITE_FLAG(0, "reserved_zero_bit");
2432  }
2433#else
2434  if (ptl->getProfileIdc() == Profile::MAINREXT || ptl->getProfileIdc() == Profile::HIGHTHROUGHPUTREXT )
2435  {
2436    const UInt         bitDepthConstraint=ptl->getBitDepthConstraint();
2437    WRITE_FLAG(bitDepthConstraint<=12,          PTL_TRACE_TEXT("max_12bit_constraint_flag"       ));
2438    WRITE_FLAG(bitDepthConstraint<=10,          PTL_TRACE_TEXT("max_10bit_constraint_flag"       ));
2439    WRITE_FLAG(bitDepthConstraint<= 8,          PTL_TRACE_TEXT("max_8bit_constraint_flag"        ));
2440    const ChromaFormat chromaFmtConstraint=ptl->getChromaFormatConstraint();
2441    WRITE_FLAG(chromaFmtConstraint==CHROMA_422||chromaFmtConstraint==CHROMA_420||chromaFmtConstraint==CHROMA_400, PTL_TRACE_TEXT("max_422chroma_constraint_flag" ));
2442    WRITE_FLAG(chromaFmtConstraint==CHROMA_420||chromaFmtConstraint==CHROMA_400,                                  PTL_TRACE_TEXT("max_420chroma_constraint_flag" ));
2443    WRITE_FLAG(chromaFmtConstraint==CHROMA_400,                                                                   PTL_TRACE_TEXT("max_monochrome_constraint_flag"));
2444    WRITE_FLAG(ptl->getIntraConstraintFlag(),        PTL_TRACE_TEXT("intra_constraint_flag"           ));
2445    WRITE_FLAG(ptl->getOnePictureOnlyConstraintFlag(), PTL_TRACE_TEXT("one_picture_only_constraint_flag"));
2446    WRITE_FLAG(ptl->getLowerBitRateConstraintFlag(), PTL_TRACE_TEXT("lower_bit_rate_constraint_flag"  ));
2447    WRITE_CODE(0 , 16, PTL_TRACE_TEXT("reserved_zero_34bits[0..15]"     ));
2448    WRITE_CODE(0 , 16, PTL_TRACE_TEXT("reserved_zero_34bits[16..31]"    ));
2449    WRITE_CODE(0 ,  2, PTL_TRACE_TEXT("reserved_zero_34bits[32..33]"    ));
2450  }
2451  else
2452  {
2453    WRITE_CODE(0x0000 , 16, PTL_TRACE_TEXT("reserved_zero_43bits[0..15]"     ));
2454    WRITE_CODE(0x0000 , 16, PTL_TRACE_TEXT("reserved_zero_43bits[16..31]"    ));
2455    WRITE_CODE(0x000  , 11, PTL_TRACE_TEXT("reserved_zero_43bits[32..42]"    ));
2456  }
2457  WRITE_FLAG(false,   PTL_TRACE_TEXT("inbld_flag" ));
2458#undef PTL_TRACE_TEXT
2459#endif
2460}
2461
2462/**
2463 * Write tiles and wavefront substreams sizes for the slice header (entry points).
2464 *
2465 * \param pSlice TComSlice structure that contains the substream size information.
2466 */
2467Void  TEncCavlc::codeTilesWPPEntryPoint( TComSlice* pSlice )
2468{
2469  if (!pSlice->getPPS()->getTilesEnabledFlag() && !pSlice->getPPS()->getEntropyCodingSyncEnabledFlag())
2470  {
2471    return;
2472  }
2473  UInt maxOffset = 0;
2474  for(Int idx=0; idx<pSlice->getNumberOfSubstreamSizes(); idx++)
2475  {
2476    UInt offset=pSlice->getSubstreamSize(idx);
2477    if ( offset > maxOffset )
2478    {
2479      maxOffset = offset;
2480    }
2481  }
2482
2483  // Determine number of bits "offsetLenMinus1+1" required for entry point information
2484  UInt offsetLenMinus1 = 0;
2485  while (maxOffset >= (1u << (offsetLenMinus1 + 1)))
2486  {
2487    offsetLenMinus1++;
2488    assert(offsetLenMinus1 + 1 < 32);
2489  }
2490
2491  WRITE_UVLC(pSlice->getNumberOfSubstreamSizes(), "num_entry_point_offsets");
2492  if (pSlice->getNumberOfSubstreamSizes()>0)
2493  {
2494    WRITE_UVLC(offsetLenMinus1, "offset_len_minus1");
2495
2496    for (UInt idx=0; idx<pSlice->getNumberOfSubstreamSizes(); idx++)
2497    {
2498      WRITE_CODE(pSlice->getSubstreamSize(idx)-1, offsetLenMinus1+1, "entry_point_offset_minus1");
2499    }
2500  }
2501}
2502
2503Void TEncCavlc::codeTerminatingBit      ( UInt /*uilsLast*/ )
2504{
2505}
2506
2507Void TEncCavlc::codeSliceFinish ()
2508{
2509}
2510
2511Void TEncCavlc::codeMVPIdx ( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, RefPicList /*eRefList*/ )
2512{
2513  assert(0);
2514}
2515
2516Void TEncCavlc::codePartSize( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
2517{
2518  assert(0);
2519}
2520
2521Void TEncCavlc::codePredMode( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/ )
2522{
2523  assert(0);
2524}
2525
2526Void TEncCavlc::codeMergeFlag    ( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/ )
2527{
2528  assert(0);
2529}
2530
2531Void TEncCavlc::codeMergeIndex    ( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/ )
2532{
2533  assert(0);
2534}
2535
2536#if H_3D_ARP
2537Void TEncCavlc::codeARPW( TComDataCU* pcCU, UInt uiAbsPartIdx )
2538{
2539  assert(0);
2540}
2541#endif
2542
2543#if H_3D_IC
2544Void TEncCavlc::codeICFlag( TComDataCU* pcCU, UInt uiAbsPartIdx )
2545{
2546  assert(0);
2547}
2548#endif
2549
2550Void TEncCavlc::codeInterModeFlag( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/, UInt /*uiEncMode*/ )
2551{
2552  assert(0);
2553}
2554
2555Void TEncCavlc::codeCUTransquantBypassFlag( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/ )
2556{
2557  assert(0);
2558}
2559
2560Void TEncCavlc::codeSkipFlag( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/ )
2561{
2562  assert(0);
2563}
2564#if H_3D
2565Void TEncCavlc::codeDIS( TComDataCU* pcCU, UInt uiAbsPartIdx )
2566{
2567  assert(0);
2568}
2569#endif
2570
2571Void TEncCavlc::codeSplitFlag   ( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
2572{
2573  assert(0);
2574}
2575
2576Void TEncCavlc::codeTransformSubdivFlag( UInt /*uiSymbol*/, UInt /*uiCtx*/ )
2577{
2578  assert(0);
2579}
2580
2581Void TEncCavlc::codeQtCbf( TComTU& /*rTu*/, const ComponentID /*compID*/, const Bool /*lowestLevel*/ )
2582{
2583  assert(0);
2584}
2585
2586Void TEncCavlc::codeQtRootCbf( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/ )
2587{
2588  assert(0);
2589}
2590
2591Void TEncCavlc::codeQtCbfZero( TComTU& /*rTu*/, const ChannelType /*chType*/ )
2592{
2593  assert(0);
2594}
2595Void TEncCavlc::codeQtRootCbfZero( )
2596{
2597  assert(0);
2598}
2599
2600Void TEncCavlc::codeTransformSkipFlags (TComTU& /*rTu*/, ComponentID /*component*/ )
2601{
2602  assert(0);
2603}
2604
2605/** Code I_PCM information.
2606 * \param pcCU pointer to CU
2607 * \param uiAbsPartIdx CU index
2608 * \returns Void
2609 */
2610Void TEncCavlc::codeIPCMInfo( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/ )
2611{
2612  assert(0);
2613}
2614
2615Void TEncCavlc::codeIntraDirLumaAng( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, Bool /*isMultiple*/)
2616{
2617  assert(0);
2618}
2619
2620Void TEncCavlc::codeIntraDirChroma( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/ )
2621{
2622  assert(0);
2623}
2624
2625Void TEncCavlc::codeInterDir( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/ )
2626{
2627  assert(0);
2628}
2629
2630Void TEncCavlc::codeRefFrmIdx( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, RefPicList /*eRefList*/ )
2631{
2632  assert(0);
2633}
2634
2635Void TEncCavlc::codeMvd( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, RefPicList /*eRefList*/ )
2636{
2637  assert(0);
2638}
2639
2640Void TEncCavlc::codeCrossComponentPrediction( TComTU& /*rTu*/, ComponentID /*compID*/ )
2641{
2642  assert(0);
2643}
2644
2645Void TEncCavlc::codeDeltaQP( TComDataCU* pcCU, UInt uiAbsPartIdx )
2646{
2647  Int iDQp  = pcCU->getQP( uiAbsPartIdx ) - pcCU->getRefQP( uiAbsPartIdx );
2648
2649  Int qpBdOffsetY =  pcCU->getSlice()->getSPS()->getQpBDOffset(CHANNEL_TYPE_LUMA);
2650  iDQp = (iDQp + 78 + qpBdOffsetY + (qpBdOffsetY/2)) % (52 + qpBdOffsetY) - 26 - (qpBdOffsetY/2);
2651
2652  xWriteSvlc( iDQp );
2653
2654  return;
2655}
2656
2657Void TEncCavlc::codeChromaQpAdjustment( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/ )
2658{
2659  assert(0);
2660}
2661
2662Void TEncCavlc::codeCoeffNxN    ( TComTU& /*rTu*/, TCoeff* /*pcCoef*/, const ComponentID /*compID*/ )
2663{
2664  assert(0);
2665}
2666
2667Void TEncCavlc::estBit( estBitsSbacStruct* /*pcEstBitsCabac*/, Int /*width*/, Int /*height*/, ChannelType /*chType*/ )
2668{
2669  // printf("error : no VLC mode support in this version\n");
2670  return;
2671}
2672
2673// ====================================================================================================================
2674// Protected member functions
2675// ====================================================================================================================
2676
2677//! Code weighted prediction tables
2678Void TEncCavlc::xCodePredWeightTable( TComSlice* pcSlice )
2679{
2680  WPScalingParam  *wp;
2681  const ChromaFormat    format                = pcSlice->getPic()->getChromaFormat();
2682  const UInt            numberValidComponents = getNumberValidComponents(format);
2683  const Bool            bChroma               = isChromaEnabled(format);
2684  const Int             iNbRef                = (pcSlice->getSliceType() == B_SLICE ) ? (2) : (1);
2685        Bool            bDenomCoded           = false;
2686        UInt            uiTotalSignalledWeightFlags = 0;
2687
2688  if ( (pcSlice->getSliceType()==P_SLICE && pcSlice->getPPS()->getUseWP()) || (pcSlice->getSliceType()==B_SLICE && pcSlice->getPPS()->getWPBiPred()) )
2689  {
2690    for ( Int iNumRef=0 ; iNumRef<iNbRef ; iNumRef++ ) // loop over l0 and l1 syntax elements
2691    {
2692      RefPicList  eRefPicList = ( iNumRef ? REF_PIC_LIST_1 : REF_PIC_LIST_0 );
2693
2694      // NOTE: wp[].uiLog2WeightDenom and wp[].bPresentFlag are actually per-channel-type settings.
2695
2696      for ( Int iRefIdx=0 ; iRefIdx<pcSlice->getNumRefIdx(eRefPicList) ; iRefIdx++ )
2697      {
2698        pcSlice->getWpScaling(eRefPicList, iRefIdx, wp);
2699        if ( !bDenomCoded )
2700        {
2701          Int iDeltaDenom;
2702          WRITE_UVLC( wp[COMPONENT_Y].uiLog2WeightDenom, "luma_log2_weight_denom" );
2703
2704          if( bChroma )
2705          {
2706            assert(wp[COMPONENT_Cb].uiLog2WeightDenom == wp[COMPONENT_Cr].uiLog2WeightDenom); // check the channel-type settings are consistent across components.
2707            iDeltaDenom = (wp[COMPONENT_Cb].uiLog2WeightDenom - wp[COMPONENT_Y].uiLog2WeightDenom);
2708            WRITE_SVLC( iDeltaDenom, "delta_chroma_log2_weight_denom" );
2709          }
2710          bDenomCoded = true;
2711        }
2712        WRITE_FLAG( wp[COMPONENT_Y].bPresentFlag, iNumRef==0?"luma_weight_l0_flag[i]":"luma_weight_l1_flag[i]" );
2713        uiTotalSignalledWeightFlags += wp[COMPONENT_Y].bPresentFlag;
2714      }
2715      if (bChroma)
2716      {
2717        for ( Int iRefIdx=0 ; iRefIdx<pcSlice->getNumRefIdx(eRefPicList) ; iRefIdx++ )
2718        {
2719          pcSlice->getWpScaling(eRefPicList, iRefIdx, wp);
2720          assert(wp[COMPONENT_Cb].bPresentFlag == wp[COMPONENT_Cr].bPresentFlag); // check the channel-type settings are consistent across components.
2721          WRITE_FLAG( wp[COMPONENT_Cb].bPresentFlag, iNumRef==0?"chroma_weight_l0_flag[i]":"chroma_weight_l1_flag[i]" );
2722          uiTotalSignalledWeightFlags += 2*wp[COMPONENT_Cb].bPresentFlag;
2723        }
2724      }
2725
2726      for ( Int iRefIdx=0 ; iRefIdx<pcSlice->getNumRefIdx(eRefPicList) ; iRefIdx++ )
2727      {
2728        pcSlice->getWpScaling(eRefPicList, iRefIdx, wp);
2729        if ( wp[COMPONENT_Y].bPresentFlag )
2730        {
2731          Int iDeltaWeight = (wp[COMPONENT_Y].iWeight - (1<<wp[COMPONENT_Y].uiLog2WeightDenom));
2732          WRITE_SVLC( iDeltaWeight, iNumRef==0?"delta_luma_weight_l0[i]":"delta_luma_weight_l1[i]" );
2733          WRITE_SVLC( wp[COMPONENT_Y].iOffset, iNumRef==0?"luma_offset_l0[i]":"luma_offset_l1[i]" );
2734        }
2735
2736        if ( bChroma )
2737        {
2738          if ( wp[COMPONENT_Cb].bPresentFlag )
2739          {
2740            for ( Int j = COMPONENT_Cb ; j < numberValidComponents ; j++ )
2741            {
2742              assert(wp[COMPONENT_Cb].uiLog2WeightDenom == wp[COMPONENT_Cr].uiLog2WeightDenom);
2743              Int iDeltaWeight = (wp[j].iWeight - (1<<wp[COMPONENT_Cb].uiLog2WeightDenom));
2744              WRITE_SVLC( iDeltaWeight, iNumRef==0?"delta_chroma_weight_l0[i]":"delta_chroma_weight_l1[i]" );
2745
2746              Int range=pcSlice->getSPS()->getSpsRangeExtension().getHighPrecisionOffsetsEnabledFlag() ? (1<<pcSlice->getSPS()->getBitDepth(CHANNEL_TYPE_CHROMA))/2 : 128;
2747              Int pred = ( range - ( ( range*wp[j].iWeight)>>(wp[j].uiLog2WeightDenom) ) );
2748              Int iDeltaChroma = (wp[j].iOffset - pred);
2749              WRITE_SVLC( iDeltaChroma, iNumRef==0?"delta_chroma_offset_l0[i]":"delta_chroma_offset_l1[i]" );
2750            }
2751          }
2752        }
2753      }
2754    }
2755    assert(uiTotalSignalledWeightFlags<=24);
2756  }
2757}
2758
2759/** code quantization matrix
2760 *  \param scalingList quantization matrix information
2761 */
2762Void TEncCavlc::codeScalingList( const TComScalingList &scalingList )
2763{
2764  //for each size
2765  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
2766  {
2767    const Int predListStep = (sizeId == SCALING_LIST_32x32? (SCALING_LIST_NUM/NUMBER_OF_PREDICTION_MODES) : 1); // if 32x32, skip over chroma entries.
2768
2769    for(UInt listId = 0; listId < SCALING_LIST_NUM; listId+=predListStep)
2770    {
2771      Bool scalingListPredModeFlag = scalingList.getScalingListPredModeFlag(sizeId, listId);
2772      WRITE_FLAG( scalingListPredModeFlag, "scaling_list_pred_mode_flag" );
2773      if(!scalingListPredModeFlag)// Copy Mode
2774      {
2775        if (sizeId == SCALING_LIST_32x32)
2776        {
2777          // adjust the code, to cope with the missing chroma entries
2778          WRITE_UVLC( ((Int)listId - (Int)scalingList.getRefMatrixId (sizeId,listId)) / (SCALING_LIST_NUM/NUMBER_OF_PREDICTION_MODES), "scaling_list_pred_matrix_id_delta");
2779        }
2780        else
2781        {
2782          WRITE_UVLC( (Int)listId - (Int)scalingList.getRefMatrixId (sizeId,listId), "scaling_list_pred_matrix_id_delta");
2783        }
2784      }
2785      else// DPCM Mode
2786      {
2787        xCodeScalingList(&scalingList, sizeId, listId);
2788      }
2789    }
2790  }
2791  return;
2792}
2793/** code DPCM
2794 * \param scalingList quantization matrix information
2795 * \param sizeId      size index
2796 * \param listId      list index
2797 */
2798Void TEncCavlc::xCodeScalingList(const TComScalingList* scalingList, UInt sizeId, UInt listId)
2799{
2800  Int coefNum = min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId]);
2801  UInt* scan  = g_scanOrder[SCAN_UNGROUPED][SCAN_DIAG][sizeId==0 ? 2 : 3][sizeId==0 ? 2 : 3];
2802  Int nextCoef = SCALING_LIST_START_VALUE;
2803  Int data;
2804  const Int *src = scalingList->getScalingListAddress(sizeId, listId);
2805    if( sizeId > SCALING_LIST_8x8 )
2806    {
2807      WRITE_SVLC( scalingList->getScalingListDC(sizeId,listId) - 8, "scaling_list_dc_coef_minus8");
2808      nextCoef = scalingList->getScalingListDC(sizeId,listId);
2809    }
2810    for(Int i=0;i<coefNum;i++)
2811    {
2812      data = src[scan[i]] - nextCoef;
2813      nextCoef = src[scan[i]];
2814      if(data > 127)
2815      {
2816        data = data - 256;
2817      }
2818      if(data < -128)
2819      {
2820        data = data + 256;
2821      }
2822
2823      WRITE_SVLC( data,  "scaling_list_delta_coef");
2824    }
2825}
2826Bool TEncCavlc::findMatchingLTRP ( TComSlice* pcSlice, UInt *ltrpsIndex, Int ltrpPOC, Bool usedFlag )
2827{
2828  // Bool state = true, state2 = false;
2829  Int lsb = ltrpPOC & ((1<<pcSlice->getSPS()->getBitsForPOC())-1);
2830  for (Int k = 0; k < pcSlice->getSPS()->getNumLongTermRefPicSPS(); k++)
2831  {
2832    if ( (lsb == pcSlice->getSPS()->getLtRefPicPocLsbSps(k)) && (usedFlag == pcSlice->getSPS()->getUsedByCurrPicLtSPSFlag(k)) )
2833    {
2834      *ltrpsIndex = k;
2835      return true;
2836    }
2837  }
2838  return false;
2839}
2840
2841Void TEncCavlc::codeExplicitRdpcmMode( TComTU& /*rTu*/, const ComponentID /*compID*/ )
2842 {
2843   assert(0);
2844 }
2845#if NH_3D_SDC_INTRA || H_3D_INTER_SDC
2846Void TEncCavlc::codeSDCFlag ( TComDataCU* pcCU, UInt uiAbsPartIdx )
2847{
2848  assert(0);
2849}
2850#endif
2851   
2852#if H_3D_DBBP
2853Void TEncCavlc::codeDBBPFlag    ( TComDataCU* pcCU, UInt uiAbsPartIdx )
2854{
2855  assert(0);
2856}
2857#endif
2858
2859
2860//! \}
Note: See TracBrowser for help on using the repository browser.