source: 3DVCSoftware/trunk/source/Lib/TLibEncoder/TEncCavlc.cpp @ 872

Last change on this file since 872 was 872, checked in by tech, 10 years ago

Merged HTM-10.0-dev0@871. (MV-HEVC 7 HLS)

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