Index: branches/HTM-14.1-update-dev0/source/App/TAppDecoder/TAppDecCfg.cpp
===================================================================
--- branches/HTM-14.1-update-dev0/source/App/TAppDecoder/TAppDecCfg.cpp	(revision 1199)
+++ branches/HTM-14.1-update-dev0/source/App/TAppDecoder/TAppDecCfg.cpp	(revision 1200)
@@ -2,7 +2,7 @@
  * License, included below. This software may be subject to other third party
  * and contributor rights, including patent rights, and no such rights are
- * granted under this license.  
- *
-* Copyright (c) 2010-2015, ITU/ISO/IEC
+ * granted under this license.
+ *
+ * Copyright (c) 2010-2015, ITU/ISO/IEC
  * All rights reserved.
  *
@@ -41,10 +41,10 @@
 #include "TAppDecCfg.h"
 #include "TAppCommon/program_options_lite.h"
-
-#if H_MV
-#include <cassert>
-#endif
+#include "TLibCommon/TComChromaFormat.h"
 #ifdef WIN32
 #define strdup _strdup
+#endif
+#if NH_MV
+#include <cassert>
 #endif
 
@@ -68,36 +68,50 @@
   string cfg_ReconFile;
   string cfg_TargetDecLayerIdSetFile;
-#if H_3D
+#if NH_3D
   string cfg_ScaleOffsetFile;
 #endif
+  string outputColourSpaceConvert;
+  Int warnUnknowParameter = 0;
 
   po::Options opts;
   opts.addOptions()
-  ("help", do_help, false, "this help text")
-  ("BitstreamFile,b", cfg_BitstreamFile, string(""), "bitstream input file name")
-  ("ReconFile,o",     cfg_ReconFile,     string(""), "reconstructed YUV output file name\n"
-                                                     "YUV writing is skipped if omitted")
-#if H_3D
-  ("ScaleOffsetFile,p", cfg_ScaleOffsetFile, string(""), "file with coded scales and offsets")
-#endif
-  ("SkipFrames,s", m_iSkipFrame, 0, "number of frames to skip before random access")
-  ("OutputBitDepth,d", m_outputBitDepthY, 0, "bit depth of YUV output luma component (default: use 0 for native depth)")
-  ("OutputBitDepthC,d", m_outputBitDepthC, 0, "bit depth of YUV output chroma component (default: use 0 for native depth)")
-#if H_MV
-  ("TargetOptLayerSetIdx,x", m_targetOptLayerSetIdx, -1, "Target output layer set index. (default: -1, determine automatically to be equal to highest layer set index") // Should actually equal to 0 as default. However, this would cause only the base layer to be decoded.  
-#endif
-  ("MaxTemporalLayer,t", m_iMaxTemporalLayer, -1, "Maximum Temporal Layer to be decoded. -1 to decode all layers")
-  ("SEIDecodedPictureHash", m_decodedPictureHashSEIEnabled, 1, "Control handling of decoded picture hash SEI messages\n"
-                                              "\t1: check hash in SEI messages if available in the bitstream\n"
-                                              "\t0: ignore SEI message")
-  ("SEIpictureDigest", m_decodedPictureHashSEIEnabled, 1, "deprecated alias for SEIDecodedPictureHash")
-  ("TarDecLayerIdSetFile,l", cfg_TargetDecLayerIdSetFile, string(""), "targetDecLayerIdSet file name. The file should include white space separated LayerId values to be decoded. Omitting the option or a value of -1 in the file decodes all layers.")
-  ("RespectDefDispWindow,w", m_respectDefDispWindow, 0, "Only output content inside the default display window\n")
-#if H_MV
-  ("OutputVpsInfo,v", m_outputVpsInfo, false, "Output information about the layer dependencies and layer sets")
-#endif
+
+
+  ("help",                      do_help,                               false,      "this help text")
+  ("BitstreamFile,b",           cfg_BitstreamFile,                     string(""), "bitstream input file name")
+  ("ReconFile,o",               cfg_ReconFile,                         string(""), "reconstructed YUV output file name\n"
+                                                                                   "YUV writing is skipped if omitted")
+#if NH_3D
+  ("ScaleOffsetFile,p",         cfg_ScaleOffsetFile,                   string(""), "file with coded scales and offsets")
+  ("Depth420OutputFlag",        m_depth420OutputFlag,                  true      , "Output depth layers in 4:2:0 ") 
+#endif
+  ("WarnUnknowParameter,w",     warnUnknowParameter,                                  0, "warn for unknown configuration parameters instead of failing")
+  ("SkipFrames,s",              m_iSkipFrame,                          0,          "number of frames to skip before random access")
+  ("OutputBitDepth,d",          m_outputBitDepth[CHANNEL_TYPE_LUMA],   0,          "bit depth of YUV output luma component (default: use 0 for native depth)")
+  ("OutputBitDepthC,d",         m_outputBitDepth[CHANNEL_TYPE_CHROMA], 0,          "bit depth of YUV output chroma component (default: use 0 for native depth)")
+  ("OutputColourSpaceConvert",  outputColourSpaceConvert,              string(""), "Colour space conversion to apply to input 444 video. Permitted values are (empty string=UNCHANGED) " + getListOfColourSpaceConverts(false))
+#if NH_MV
+  ("TargetOptLayerSetIdx,x",    m_targetOptLayerSetIdx,                -1,         "Target output layer set index. (default: -1, determine automatically to be equal to highest layer set index") // Should actually equal to 0 as default. However, this would cause only the base layer to be decoded.  
+#endif
+  ("MaxTemporalLayer,t",        m_iMaxTemporalLayer,                   -1,         "Maximum Temporal Layer to be decoded. -1 to decode all layers")
+  ("SEIDecodedPictureHash",     m_decodedPictureHashSEIEnabled,        1,          "Control handling of decoded picture hash SEI messages\n"
+                                                                                   "\t1: check hash in SEI messages if available in the bitstream\n"
+                                                                                   "\t0: ignore SEI message")
+  ("SEINoDisplay",              m_decodedNoDisplaySEIEnabled,          true,       "Control handling of decoded no display SEI messages")
+  ("TarDecLayerIdSetFile,l",    cfg_TargetDecLayerIdSetFile,           string(""), "targetDecLayerIdSet file name. The file should include white space separated LayerId values to be decoded. Omitting the option or a value of -1 in the file decodes all layers.")
+  ("RespectDefDispWindow,w",    m_respectDefDispWindow,                0,          "Only output content inside the default display window\n")
+#if NH_MV
+  ("OutputVpsInfo,v",           m_outputVpsInfo,                       false,       "Output information about the layer dependencies and layer sets")
+#endif
+#if O0043_BEST_EFFORT_DECODING
+  ("ForceDecodeBitDepth",       m_forceDecodeBitDepth,                 0U,         "Force the decoder to operate at a particular bit-depth (best effort decoding)")
+#endif
+  ("OutputDecodedSEIMessagesFilename",  m_outputDecodedSEIMessagesFilename,    string(""), "When non empty, output decoded SEI messages to the indicated file. If file is '-', then output to stdout\n")
+  ("ClipOutputVideoToRec709Range",      m_bClipOutputVideoToRec709Range,  false, "If true then clip output video to the Rec. 709 Range on saving")
   ;
+
   po::setDefaults(opts);
-  const list<const Char*>& argv_unhandled = po::scanArgv(opts, argc, (const Char**) argv);
+  po::ErrorReporter err;
+  const list<const Char*>& argv_unhandled = po::scanArgv(opts, argc, (const Char**) argv, err);
 
   for (list<const Char*>::const_iterator it = argv_unhandled.begin(); it != argv_unhandled.end(); it++)
@@ -109,4 +123,20 @@
   {
     po::doHelp(cout, opts);
+    return false;
+  }
+
+  if (err.is_errored)
+  {
+    if (!warnUnknowParameter)
+    {
+      /* errors have already been reported to stderr */
+      return false;
+    }
+  }
+
+  m_outputColourSpaceConvert = stringToInputColourSpaceConvert(outputColourSpaceConvert, false);
+  if (m_outputColourSpaceConvert>=NUMBER_INPUT_COLOUR_SPACE_CONVERSIONS)
+  {
+    fprintf(stderr, "Bad output colour space conversion string\n");
     return false;
   }
@@ -116,10 +146,11 @@
   m_pchReconFile = cfg_ReconFile.empty() ? NULL : strdup(cfg_ReconFile.c_str());
 
-#if H_3D
+#if NH_3D
   m_pchScaleOffsetFile = cfg_ScaleOffsetFile.empty() ? NULL : strdup(cfg_ScaleOffsetFile.c_str());
 #endif
+
   if (!m_pchBitstreamFile)
   {
-    fprintf(stderr, "No input file specifed, aborting\n");
+    fprintf(stderr, "No input file specified, aborting\n");
     return false;
   }
@@ -127,5 +158,5 @@
   if ( !cfg_TargetDecLayerIdSetFile.empty() )
   {
-#if H_MV
+#if NH_MV
     m_targetDecLayerIdSetFileEmpty = false;     
 #endif
@@ -152,5 +183,5 @@
         if ( layerIdParsed < 0 || layerIdParsed >= MAX_NUM_LAYER_IDS )
         {
-          fprintf(stderr, "Warning! Parsed LayerId %d is not withing allowed range [0,%d]. Ignoring this value.\n", layerIdParsed, MAX_NUM_LAYER_IDS-1 );
+          fprintf(stderr, "Warning! Parsed LayerId %d is not within allowed range [0,%d]. Ignoring this value.\n", layerIdParsed, MAX_NUM_LAYER_IDS-1 );
         }
         else
@@ -172,12 +203,12 @@
     }
   }
-#if H_MV
-  m_targetDecLayerIdSet.push_back( 0 );         // Only base layer at startup
-#endif
-
+
+#if NH_MV
+  //m_targetDecLayerIdSet.push_back( 0 );         // Only base layer at startup
+#endif
   return true;
 }
 
-#if H_MV
+#if NH_MV
 Void TAppDecCfg::xAppendToFileNameEnd( Char* pchInputFileName, const Char* pchStringToAppend, Char*& rpchOutputFileName)
 {
Index: branches/HTM-14.1-update-dev0/source/App/TAppDecoder/TAppDecCfg.h
===================================================================
--- branches/HTM-14.1-update-dev0/source/App/TAppDecoder/TAppDecCfg.h	(revision 1199)
+++ branches/HTM-14.1-update-dev0/source/App/TAppDecoder/TAppDecCfg.h	(revision 1200)
@@ -2,7 +2,7 @@
  * License, included below. This software may be subject to other third party
  * and contributor rights, including patent rights, and no such rights are
- * granted under this license.  
+ * granted under this license.
  *
-* Copyright (c) 2010-2015, ITU/ISO/IEC
+ * Copyright (c) 2010-2015, ITU/ISO/IEC
  * All rights reserved.
  *
@@ -45,4 +45,5 @@
 #include "TLibCommon/CommonDef.h"
 #include <vector>
+
 //! \ingroup TAppDecoder
 //! \{
@@ -56,31 +57,38 @@
 {
 protected:
-  Char*         m_pchBitstreamFile;                   ///< input bitstream file name
-#if H_MV
-  Int           m_targetOptLayerSetIdx;               ///< target output layer set index
-  Int           m_maxLayerId;                         ///< maximum nuh_layer_id decoded
-  std::vector<Char*> m_pchReconFiles;                 ///< array of output reconstruction file name create from output reconstruction file name
+  Char*         m_pchBitstreamFile;                     ///< input bitstream file name
+#if NH_MV
+  Int           m_targetOptLayerSetIdx;                 ///< target output layer set index
+  Int           m_maxLayerId;                           ///< maximum nuh_layer_id decoded
+  std::vector<Char*> m_pchReconFiles;                   ///< array of output reconstruction file name create from output reconstruction file name
 #endif
-  Char*         m_pchReconFile;                       ///< output reconstruction file name
-#if H_3D
-  Char*         m_pchScaleOffsetFile;                 ///< output coded scale and offset parameters
+  Char*         m_pchReconFile;                         ///< output reconstruction file name
+#if NH_3D
+  Char*         m_pchScaleOffsetFile;                   ///< output coded scale and offset parameters
+  Bool          m_depth420OutputFlag;                   ///< output depth layers in 4:2:0
 #endif
-  Int           m_iSkipFrame;                         ///< counter for frames prior to the random access point to skip
-  Int           m_outputBitDepthY;                    ///< bit depth used for writing output (luma)
-  Int           m_outputBitDepthC;                    ///< bit depth used for writing output (chroma)t
+  Int           m_iSkipFrame;                           ///< counter for frames prior to the random access point to skip
+  Int           m_outputBitDepth[MAX_NUM_CHANNEL_TYPE]; ///< bit depth used for writing output
+  InputColourSpaceConversion m_outputColourSpaceConvert;
 
   Int           m_iMaxTemporalLayer;                  ///< maximum temporal layer to be decoded
   Int           m_decodedPictureHashSEIEnabled;       ///< Checksum(3)/CRC(2)/MD5(1)/disable(0) acting on decoded picture hash SEI message
+  Bool          m_decodedNoDisplaySEIEnabled;         ///< Enable(true)/disable(false) writing only pictures that get displayed based on the no display SEI message
+  std::vector<Int> m_targetDecLayerIdSet;             ///< set of LayerIds to be included in the sub-bitstream extraction process.
+#if NH_MV
+  Bool          m_targetDecLayerIdSetFileEmpty;       ///< indication if target layers are given by file
+#endif
 
-  std::vector<Int> m_targetDecLayerIdSet;             ///< set of LayerIds to be included in the sub-bitstream extraction process.
-#if H_MV
-  Bool          m_targetDecLayerIdSetFileEmpty;      ///< indication if target layers are given by file
+  Int           m_respectDefDispWindow;               ///< Only output content inside the default display window
+#if O0043_BEST_EFFORT_DECODING
+  UInt          m_forceDecodeBitDepth;                ///< if non-zero, force the bit depth at the decoder (best effort decoding)
 #endif
-  Int           m_respectDefDispWindow;               ///< Only output content inside the default display window 
-
-#if H_MV
+  std::string   m_outputDecodedSEIMessagesFilename;   ///< filename to output decoded SEI messages to. If '-', then use stdout. If empty, do not output details.
+  Bool          m_bClipOutputVideoToRec709Range;      ///< If true, clip the output video to the Rec 709 range on saving.
+#if NH_MV
   Bool          m_outputVpsInfo;                     ///< Output VPS information 
   Void xAppendToFileNameEnd( Char* pchInputFileName, const Char* pchStringToAppend, Char*& rpchOutputFileName); ///< create filenames
 #endif
+
 public:
   TAppDecCfg()
@@ -88,15 +96,24 @@
   , m_pchReconFile(NULL)
   , m_iSkipFrame(0)
-  , m_outputBitDepthY(0)
-  , m_outputBitDepthC(0)
+  , m_outputColourSpaceConvert(IPCOLOURSPACE_UNCHANGED)
   , m_iMaxTemporalLayer(-1)
   , m_decodedPictureHashSEIEnabled(0)
-#if H_MV
+  , m_decodedNoDisplaySEIEnabled(false)
+#if NH_MV
   , m_targetDecLayerIdSetFileEmpty(true)
 #endif
   , m_respectDefDispWindow(0)
-  {}
+#if O0043_BEST_EFFORT_DECODING
+  , m_forceDecodeBitDepth(0)
+#endif
+  {
+    for (UInt channelTypeIndex = 0; channelTypeIndex < MAX_NUM_CHANNEL_TYPE; channelTypeIndex++)
+    {
+      m_outputBitDepth[channelTypeIndex] = 0;
+    }
+  }
+
   virtual ~TAppDecCfg() {}
-  
+
   Bool  parseCfg        ( Int argc, Char* argv[] );   ///< initialize option class from configuration
 };
Index: branches/HTM-14.1-update-dev0/source/App/TAppDecoder/TAppDecTop.cpp
===================================================================
--- branches/HTM-14.1-update-dev0/source/App/TAppDecoder/TAppDecTop.cpp	(revision 1199)
+++ branches/HTM-14.1-update-dev0/source/App/TAppDecoder/TAppDecTop.cpp	(revision 1200)
@@ -2,7 +2,7 @@
  * License, included below. This software may be subject to other third party
  * and contributor rights, including patent rights, and no such rights are
- * granted under this license.  
+ * granted under this license.
  *
-* Copyright (c) 2010-2015, ITU/ISO/IEC
+ * Copyright (c) 2010-2015, ITU/ISO/IEC
  * All rights reserved.
  *
@@ -45,4 +45,7 @@
 #include "TLibDecoder/AnnexBread.h"
 #include "TLibDecoder/NALread.h"
+#if RExt__DECODER_DEBUG_BIT_STATISTICS
+#include "TLibCommon/TComCodingStatistics.h"
+#endif
 
 //! \ingroup TAppDecoder
@@ -54,5 +57,5 @@
 
 TAppDecTop::TAppDecTop()
-#if !H_MV
+#if !NH_MV
 : m_iPOCLastDisplay(-MAX_INT)
 #else
@@ -60,5 +63,5 @@
 #endif
 {
-#if H_MV
+#if NH_MV
   for (Int i = 0; i < MAX_NUM_LAYER_IDS; i++) 
   {
@@ -67,9 +70,9 @@
   }
 #endif
-#if H_3D
+#if NH_3D
     m_pScaleOffsetFile  = 0;
 #endif
 
-#if H_MV
+#if NH_MV
     m_markedForOutput = false; 
 #endif
@@ -83,4 +86,9 @@
 Void TAppDecTop::destroy()
 {
+#if NH_MV
+  // destroy internal classes
+  xDestroyDecLib();
+#endif
+
   if (m_pchBitstreamFile)
   {
@@ -88,5 +96,5 @@
     m_pchBitstreamFile = NULL;
   }
-#if H_MV
+#if NH_MV
   for (Int decIdx = 0; decIdx < m_numDecoders; decIdx++)
   {
@@ -103,5 +111,5 @@
     m_pchReconFile = NULL;
   }
-#if H_3D
+#if NH_3D
   if (m_pchScaleOffsetFile)
   {
@@ -127,5 +135,5 @@
 {
   Int                 poc;
-#if H_MV
+#if NH_MV
   poc = -1; 
 #endif
@@ -138,6 +146,5 @@
     exit(EXIT_FAILURE);
   }
-
-#if H_3D
+#if NH_3D
   if( m_pchScaleOffsetFile ) 
   { 
@@ -146,10 +153,21 @@
   }
 #endif
+
   InputByteStream bytestream(bitstreamFile);
+
+  if (!m_outputDecodedSEIMessagesFilename.empty() && m_outputDecodedSEIMessagesFilename!="-")
+  {
+    m_seiMessageFileStream.open(m_outputDecodedSEIMessagesFilename.c_str(), std::ios::out);
+    if (!m_seiMessageFileStream.is_open() || !m_seiMessageFileStream.good())
+    {
+      fprintf(stderr, "\nUnable to open file `%s' for writing decoded SEI messages\n", m_outputDecodedSEIMessagesFilename.c_str());
+      exit(EXIT_FAILURE);
+    }
+  }
 
   // create & initialize internal classes
   xCreateDecLib();
   xInitDecLib  ();
-#if !H_MV
+#if !NH_MV
   m_iPOCLastDisplay += m_iSkipFrame;      // set the last displayed POC correctly for skip forward.
 
@@ -169,5 +187,5 @@
   Bool firstSlice        = true; 
 #endif
-  Bool loopFiltered      = false;
+  Bool loopFiltered = false;
 
   while (!!bitstreamFile)
@@ -177,6 +195,11 @@
      * requires the TDecTop::decode() method to be called again with the same
      * nal unit. */
+#if RExt__DECODER_DEBUG_BIT_STATISTICS
+    TComCodingStatistics::TComCodingStatisticsData backupStats(TComCodingStatistics::GetStatistics());
+    streampos location = bitstreamFile.tellg() - streampos(bytestream.GetNumBufferedBytes());
+#else
     streampos location = bitstreamFile.tellg();
-#if H_MV
+#endif
+#if NH_MV
 #if ENC_DEC_TRACE
     Int64 symCount = g_nSymbolCounter;
@@ -184,11 +207,11 @@
 #endif
     AnnexBStats stats = AnnexBStats();
-    vector<uint8_t> nalUnit;
+
     InputNALUnit nalu;
-    byteStreamNALUnit(bytestream, nalUnit, stats);
+    byteStreamNALUnit(bytestream, nalu.getBitstream().getFifo(), stats);
 
     // call actual decoding function
     Bool bNewPicture = false;
-#if H_MV
+#if NH_MV
     Bool newSliceDiffPoc   = false;
     Bool newSliceDiffLayer = false;
@@ -196,5 +219,5 @@
     Bool allLayersDecoded  = false;     
 #endif
-    if (nalUnit.empty())
+    if (nalu.getBitstream().getFifo().empty())
     {
       /* this can happen if the following occur:
@@ -207,12 +230,11 @@
     else
     {
-      read(nalu, nalUnit);
-#if H_MV      
+      read(nalu);
+#if NH_MV      
       if( (m_iMaxTemporalLayer >= 0 && nalu.m_temporalId > m_iMaxTemporalLayer) 
           || !isNaluWithinTargetDecLayerIdSet(&nalu)
-          || nalu.m_layerId > MAX_NUM_LAYER_IDS-1
-          || (nalu.m_nalUnitType == NAL_UNIT_VPS && nalu.m_layerId > 0)           
-          || (nalu.m_nalUnitType == NAL_UNIT_EOB && nalu.m_layerId > 0)    
-          || (nalu.m_nalUnitType == NAL_UNIT_EOS && nalu.m_layerId > 0)    
+          || nalu.m_nuhLayerId > MAX_NUM_LAYER_IDS-1
+          || (nalu.m_nalUnitType == NAL_UNIT_VPS && nalu.m_nuhLayerId > 0)           
+          || (nalu.m_nalUnitType == NAL_UNIT_EOB && nalu.m_nuhLayerId > 0)              
          ) 
       {
@@ -225,6 +247,6 @@
       else
       { 
-        Int decIdx     = xGetDecoderIdx( nalu.m_layerId , true );      
-        newSliceDiffLayer = nalu.isSlice() && ( nalu.m_layerId != layerIdCurrPic ) && !firstSlice;
+        Int decIdx     = xGetDecoderIdx( nalu.m_nuhLayerId , true );      
+        newSliceDiffLayer = nalu.isSlice() && ( nalu.m_nuhLayerId != layerIdCurrPic ) && !firstSlice;
         newSliceDiffPoc   = m_tDecTop[decIdx]->decode(nalu, m_iSkipFrame, m_pocLastDisplay[decIdx], newSliceDiffLayer, sliceSkippedFlag );
         // decode function only returns true when all of the following conditions are true
@@ -233,31 +255,33 @@
         // - nalu.isSlice() == true      
 
-        if ( nalu.m_nalUnitType == NAL_UNIT_VPS )
-        {
-          m_vps = m_tDecTop[decIdx]->getPrefetchedVPS(); 
+        bNewPicture       = ( newSliceDiffLayer || newSliceDiffPoc ) && !sliceSkippedFlag; 
+        if ( nalu.isSlice() && firstSlice && !sliceSkippedFlag )        
+        {
+          layerIdCurrPic = nalu.m_nuhLayerId; 
+          pocCurrPic     = m_tDecTop[decIdx]->getCurrPoc(); 
+          decIdxCurrPic  = decIdx; 
+          firstSlice     = false; 
+
+          /// Use VPS activated by the first slice to determine OLS
+          m_vps = m_tDecTop[decIdx]->getActiveVPS( );
           if ( m_targetDecLayerIdSetFileEmpty )
-          {
-            TComVPS* vps = m_vps; 
+          {            
             if ( m_targetOptLayerSetIdx == -1 )
             {
-              // Not normative! Corresponds to specification by "External Means". (Should be set equal to 0, when no external means available. ) 
-              m_targetOptLayerSetIdx = vps->getVpsNumLayerSetsMinus1(); 
+              m_targetOptLayerSetIdx = m_tDecTop[decIdx]->getTargetOlsIdx(); 
             }
-
-            for (Int dI = 0; dI < m_numDecoders; dI++ )
+            else
             {
-              m_tDecTop[decIdx]->setTargetOptLayerSetIdx( m_targetOptLayerSetIdx ); 
-#if H_3D
-              m_tDecTop[decIdx]->setProfileIdc( ); 
-#endif
+              assert( m_tDecTop[decIdx]->getTargetOlsIdx() == m_targetOptLayerSetIdx );
             }
 
-            if ( m_targetOptLayerSetIdx < 0 || m_targetOptLayerSetIdx >= vps->getNumOutputLayerSets() )
+            if ( m_targetOptLayerSetIdx < 0 || m_targetOptLayerSetIdx >= m_vps->getNumOutputLayerSets() )
             {
-              fprintf(stderr, "\ntarget output layer set index must be in the range of 0 to %d, inclusive \n", vps->getNumOutputLayerSets() - 1 );            
+              fprintf(stderr, "\ntarget output layer set index must be in the range of 0 to %d, inclusive \n", m_vps->getNumOutputLayerSets() - 1 );            
               exit(EXIT_FAILURE);
             }
-            m_targetDecLayerIdSet = vps->getTargetDecLayerIdList( m_targetOptLayerSetIdx ); 
+            m_targetDecLayerIdSet = m_vps->getTargetDecLayerIdList( m_targetOptLayerSetIdx ); 
           }
+
           if (m_outputVpsInfo )
           {
@@ -267,29 +291,15 @@
             m_vps->printPTL(); 
           }
-        }
-#if H_3D
-        if (nalu.m_nalUnitType == NAL_UNIT_VPS )
-        {                  
-          m_cCamParsCollector.init( m_pScaleOffsetFile, m_tDecTop[decIdx]->getPrefetchedVPS() );
-        }       
-#endif
-        bNewPicture       = ( newSliceDiffLayer || newSliceDiffPoc ) && !sliceSkippedFlag; 
-        if ( nalu.isSlice() && firstSlice && !sliceSkippedFlag )        
-        {
-          layerIdCurrPic = nalu.m_layerId; 
-          pocCurrPic     = m_tDecTop[decIdx]->getCurrPoc(); 
-          decIdxCurrPic  = decIdx; 
-          firstSlice     = false; 
-        }
-
-        if ( bNewPicture || !bitstreamFile )
+        }        
+
+        if ( bNewPicture || !bitstreamFile || nalu.m_nalUnitType == NAL_UNIT_EOS )
         { 
           layerIdLastPic    = layerIdCurrPic; 
-          layerIdCurrPic    = nalu.m_layerId; 
+          layerIdCurrPic    = nalu.m_nuhLayerId; 
           pocLastPic        = pocCurrPic; 
           pocCurrPic        = m_tDecTop[decIdx]->getCurrPoc(); 
           decIdxLastPic     = decIdxCurrPic; 
           decIdxCurrPic     = decIdx; 
-          allLayersDecoded = ( pocCurrPic != pocLastPic );
+          allLayersDecoded = ( pocCurrPic != pocLastPic ) && ( nalu.m_nalUnitType != NAL_UNIT_EOS );
         }
 #else
@@ -309,6 +319,12 @@
            * [1] except for the first NAL unit in the file
            *     (but bNewPicture doesn't happen then) */
+#if RExt__DECODER_DEBUG_BIT_STATISTICS
+          bitstreamFile.seekg(location);
+          bytestream.reset();
+          TComCodingStatistics::SetStatistics(backupStats);
+#else
           bitstreamFile.seekg(location-streamoff(3));
           bytestream.reset();
+#endif
 #if H_MV_ENC_DEC_TRAC
 #if ENC_DEC_TRACE
@@ -327,9 +343,16 @@
       }
     }
-    if (bNewPicture || !bitstreamFile || nalu.m_nalUnitType == NAL_UNIT_EOS )
+
+    if ( (bNewPicture || !bitstreamFile || nalu.m_nalUnitType == NAL_UNIT_EOS) &&
+#if NH_MV      
+      !m_tDecTop[decIdxLastPic]->getFirstSliceInSequence () )
+#else
+      !m_cTDecTop.getFirstSliceInSequence () )
+#endif
+
     {
       if (!loopFiltered || bitstreamFile)
       {
-#if H_MV
+#if NH_MV
         assert( decIdxLastPic != -1 ); 
         m_tDecTop[decIdxLastPic]->endPicDecoding(poc, pcListPic, m_targetDecLayerIdSet );
@@ -340,14 +363,28 @@
       }
       loopFiltered = (nalu.m_nalUnitType == NAL_UNIT_EOS);
-    }
-#if !FIX_WRITING_OUTPUT
-#if SETTING_NO_OUT_PIC_PRIOR
-    if (bNewPicture && m_cTDecTop.getIsNoOutputPriorPics())
-    {
-      m_cTDecTop.checkNoOutputPriorPics( pcListPic );
-    }
-#endif
-#endif
-#if H_3D
+      if (nalu.m_nalUnitType == NAL_UNIT_EOS)
+      {
+#if NH_MV      
+        m_tDecTop[decIdxLastPic]->setFirstSliceInSequence(true);
+#else
+        m_cTDecTop.setFirstSliceInSequence(true);
+#endif
+      }
+    }
+    else if ( (bNewPicture || !bitstreamFile || nalu.m_nalUnitType == NAL_UNIT_EOS ) &&
+#if NH_MV      
+              m_tDecTop[decIdxLastPic]->getFirstSliceInSequence () ) 
+#else
+              m_cTDecTop.getFirstSliceInSequence () ) 
+#endif
+    {
+#if NH_MV      
+      m_tDecTop[decIdxLastPic]->setFirstSliceInPicture (true);
+#else
+      m_cTDecTop.setFirstSliceInPicture (true);
+#endif
+    }
+
+#if NH_3D
     if ( allLayersDecoded || !bitstreamFile )
     {
@@ -363,18 +400,22 @@
     if( pcListPic )
     {
-#if H_MV
+#if NH_MV
       if ( m_pchReconFiles[decIdxLastPic] && !m_reconOpen[decIdxLastPic] )
 #else
-      if ( m_pchReconFile && !openedReconFile  )
-#endif
-      {
-        if (!m_outputBitDepthY) { m_outputBitDepthY = g_bitDepthY; }
-        if (!m_outputBitDepthC) { m_outputBitDepthC = g_bitDepthC; }
-
-#if H_MV
-        m_tVideoIOYuvReconFile[decIdxLastPic]->open( m_pchReconFiles[decIdxLastPic], true, m_outputBitDepthY, m_outputBitDepthC, g_bitDepthY, g_bitDepthC ); // write mode
+      if ( m_pchReconFile && !openedReconFile )
+#endif
+      {
+        const BitDepths &bitDepths=pcListPic->front()->getPicSym()->getSPS().getBitDepths(); // use bit depths of first reconstructed picture.
+        for (UInt channelType = 0; channelType < MAX_NUM_CHANNEL_TYPE; channelType++)
+        {
+          if (m_outputBitDepth[channelType] == 0)
+          {
+            m_outputBitDepth[channelType] = bitDepths.recon[channelType];
+          }
+        }
+#if NH_MV
+        m_tVideoIOYuvReconFile[decIdxLastPic]->open( m_pchReconFiles[decIdxLastPic], true, m_outputBitDepth, m_outputBitDepth, bitDepths.recon ); // write mode
         m_reconOpen[decIdxLastPic] = true;
       }
-#if FIX_WRITING_OUTPUT
       // write reconstruction to file
       if( bNewPicture )
@@ -388,5 +429,4 @@
         xWriteOutput( pcListPic, decIdxLastPic, nalu.m_temporalId );
       }
-#if SETTING_NO_OUT_PIC_PRIOR
       if ( (bNewPicture || nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_CRA) && m_tDecTop[decIdxLastPic]->getNoOutputPriorPicsFlag() )
       {
@@ -394,12 +434,10 @@
         m_tDecTop[decIdxLastPic]->setNoOutputPriorPicsFlag (false);
       }
-#endif
-#endif
+
       if ( bNewPicture && newSliceDiffPoc && 
 #else
-        m_cTVideoIOYuvReconFile.open( m_pchReconFile, true, m_outputBitDepthY, m_outputBitDepthC, g_bitDepthY, g_bitDepthC ); // write mode
-        openedReconFile  = true;
-      }
-#if FIX_WRITING_OUTPUT
+        m_cTVideoIOYuvReconFile.open( m_pchReconFile, true, m_outputBitDepth, m_outputBitDepth, bitDepths.recon ); // write mode
+        openedReconFile = true;
+      }
       // write reconstruction to file
       if( bNewPicture )
@@ -407,5 +445,4 @@
         xWriteOutput( pcListPic, nalu.m_temporalId );
       }
-#if SETTING_NO_OUT_PIC_PRIOR
       if ( (bNewPicture || nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_CRA) && m_cTDecTop.getNoOutputPriorPicsFlag() )
       {
@@ -413,7 +450,6 @@
         m_cTDecTop.setNoOutputPriorPicsFlag (false);
       }
-#endif
-#endif
-      if ( bNewPicture && 
+
+      if ( bNewPicture &&
 #endif
            (   nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_IDR_W_RADL
@@ -423,5 +459,5 @@
             || nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_BLA_W_LP ) )
       {
-#if H_MV
+#if NH_MV
         xFlushOutput( pcListPic, decIdxLastPic );
 #else
@@ -431,30 +467,22 @@
       if (nalu.m_nalUnitType == NAL_UNIT_EOS)
       {
-#if H_MV
-#if FIX_OUTPUT_EOS
-        xWriteOutput( pcListPic, decIdxLastPic, nalu.m_temporalId );
-#else
-        xFlushOutput( pcListPic, decIdxLastPic );
-#endif
-#else
-#if FIX_OUTPUT_EOS
+#if NH_MV
+        xWriteOutput( pcListPic, decIdxCurrPic, nalu.m_temporalId );
+#else
         xWriteOutput( pcListPic, nalu.m_temporalId );
-#else
-        xFlushOutput( pcListPic );
-#endif
-
-#endif
-      }
-      // write reconstruction to file -- for additional bumping as defined in C.5.2.3 
-#if H_MV
+#endif
+#if NH_MV
+        m_tDecTop[decIdxCurrPic]->setFirstSliceInPicture (false);
+#else
+        m_cTDecTop.setFirstSliceInPicture (false);
+#endif
+      }
+      // write reconstruction to file -- for additional bumping as defined in C.5.2.3
+#if NH_MV
       // Above comment seems to be wrong
 #endif
-#if FIX_WRITING_OUTPUT
       if(!bNewPicture && nalu.m_nalUnitType >= NAL_UNIT_CODED_SLICE_TRAIL_N && nalu.m_nalUnitType <= NAL_UNIT_RESERVED_VCL31)
-#else
-      if(bNewPicture)
-#endif
-      {
-#if H_MV        
+      {
+#if NH_MV        
         // Bumping after reference picture set has been applied (here after first vcl nalu. 
 #if ENC_DEC_TRACE
@@ -471,6 +499,6 @@
     }
   }
-#if H_MV
-#if H_3D
+#if NH_MV
+#if NH_3D
   if( m_cCamParsCollector.isInitialized() )
   {
@@ -486,8 +514,7 @@
   // delete buffers
   m_cTDecTop.deletePicBuffer();
-#endif
-     
   // destroy internal classes
   xDestroyDecLib();
+#endif
 }
 
@@ -498,5 +525,5 @@
 Void TAppDecTop::xCreateDecLib()
 {
-#if H_MV
+#if NH_MV
   // initialize global variables
   initROM();  
@@ -512,5 +539,5 @@
 Void TAppDecTop::xDestroyDecLib()
 {
-#if H_MV
+#if NH_MV
   // destroy ROM
   destroyROM();
@@ -538,9 +565,9 @@
     m_cTVideoIOYuvReconFile. close();
   }
-  
+
   // destroy decoder class
   m_cTDecTop.destroy();
 #endif
-#if H_3D
+#if NH_3D
   m_cCamParsCollector.uninit();
   if( m_pScaleOffsetFile ) 
@@ -553,15 +580,27 @@
 Void TAppDecTop::xInitDecLib()
 {
-#if !H_MV
+
+#if NH_3D
+  m_cCamParsCollector.setCodeScaleOffsetFile( m_pScaleOffsetFile );
+#endif
+#if !NH_MV
   // initialize decoder class
   m_cTDecTop.init();
   m_cTDecTop.setDecodedPictureHashSEIEnabled(m_decodedPictureHashSEIEnabled);
+#if O0043_BEST_EFFORT_DECODING
+  m_cTDecTop.setForceDecodeBitDepth(m_forceDecodeBitDepth);
+#endif
+  if (!m_outputDecodedSEIMessagesFilename.empty())
+  {
+    std::ostream &os=m_seiMessageFileStream.is_open() ? m_seiMessageFileStream : std::cout;
+    m_cTDecTop.setDecodedSEIMessageOutputStream(&os);
+  }
 #endif
 }
 
 /** \param pcListPic list of pictures to be written to file
-    \todo            DYN_REF_FREE should be revised
+    \param tId       temporal sub-layer ID
  */
-#if H_MV
+#if NH_MV
 Void TAppDecTop::xWriteOutput( TComList<TComPic*>* pcListPic, Int decIdx, Int tId )
 #else
@@ -569,5 +608,4 @@
 #endif
 {
-
   if (pcListPic->empty())
   {
@@ -578,9 +616,6 @@
   Int numPicsNotYetDisplayed = 0;
   Int dpbFullness = 0;
-#if H_MV
-  TComSPS* activeSPS = m_tDecTop[ decIdx ]->getActiveSPS();
-#else
-  TComSPS* activeSPS = m_cTDecTop.getActiveSPS();
-#endif
+  const TComSPS* activeSPS = &(pcListPic->front()->getPicSym()->getSPS());
+
   UInt numReorderPicsHighestTid;
   UInt maxDecPicBufferingHighestTid;
@@ -597,9 +632,9 @@
     maxDecPicBufferingHighestTid = activeSPS->getMaxDecPicBuffering(m_iMaxTemporalLayer); 
   }
-  
+
   while (iterPic != pcListPic->end())
   {
     TComPic* pcPic = *(iterPic);
-#if H_MV
+#if NH_MV
     if(pcPic->getOutputMark() && pcPic->getPOC() > m_pocLastDisplay[decIdx])
 #else
@@ -607,5 +642,5 @@
 #endif
     {
-      numPicsNotYetDisplayed++;
+       numPicsNotYetDisplayed++;
       dpbFullness++;
     }
@@ -616,10 +651,12 @@
     iterPic++;
   }
-  iterPic   = pcListPic->begin();
+
+  iterPic = pcListPic->begin();
+
   if (numPicsNotYetDisplayed>2)
   {
     iterPic++;
   }
-  
+
   TComPic* pcPic = *(iterPic);
   if (numPicsNotYetDisplayed>2 && pcPic->isField()) //Field Decoding
@@ -633,8 +670,10 @@
       iterPic++;
       TComPic* pcPicBottom = *(iterPic);
-      
-#if H_MV
-      if ( pcPicTop->getOutputMark() && (numPicsNotYetDisplayed >  pcPicTop->getNumReorderPics(tId) && !(pcPicTop->getPOC()%2) && pcPicBottom->getPOC() == pcPicTop->getPOC()+1)
-          && pcPicBottom->getOutputMark() && (numPicsNotYetDisplayed >  pcPicBottom->getNumReorderPics(tId) && (pcPicTop->getPOC() == m_pocLastDisplay[decIdx]+1 || m_pocLastDisplay[decIdx]<0)))
+
+#if NH_MV
+      if ( pcPicTop->getOutputMark() && pcPicBottom->getOutputMark() &&
+        (numPicsNotYetDisplayed >  numReorderPicsHighestTid || dpbFullness > maxDecPicBufferingHighestTid) &&
+        (!(pcPicTop->getPOC()%2) && pcPicBottom->getPOC() == pcPicTop->getPOC()+1) &&
+        (pcPicTop->getPOC() == m_pocLastDisplay[decIdx]+1 || m_pocLastDisplay[decIdx] < 0))
 #else
       if ( pcPicTop->getOutputMark() && pcPicBottom->getOutputMark() &&
@@ -646,5 +685,5 @@
         // write to file
         numPicsNotYetDisplayed = numPicsNotYetDisplayed-2;
-#if H_MV
+#if NH_MV
       if ( m_pchReconFiles[decIdx] )
 #else
@@ -653,8 +692,21 @@
         {
           const Window &conf = pcPicTop->getConformanceWindow();
-          const Window &defDisp = m_respectDefDispWindow ? pcPicTop->getDefDisplayWindow() : Window();
-
+          const Window  defDisp = m_respectDefDispWindow ? pcPicTop->getDefDisplayWindow() : Window();
           const Bool isTff = pcPicTop->isTopField();
-#if H_MV
+
+          Bool display = true;
+          if( m_decodedNoDisplaySEIEnabled )
+          {
+            SEIMessages noDisplay = getSeisByType(pcPic->getSEIs(), SEI::NO_DISPLAY );
+            const SEINoDisplay *nd = ( noDisplay.size() > 0 ) ? (SEINoDisplay*) *(noDisplay.begin()) : NULL;
+            if( (nd != NULL) && nd->m_noDisplay )
+            {
+              display = false;
+            }
+          }
+
+          if (display)
+          {
+#if NH_MV
         assert( conf   .getScaledFlag() );
         assert( defDisp.getScaledFlag() );
@@ -667,49 +719,39 @@
         m_tVideoIOYuvReconFile[decIdx]->write( pcPicTop->getPicYuvRec(), pcPicBottom->getPicYuvRec(),
 #else
-          m_cTVideoIOYuvReconFile.write( pcPicTop->getPicYuvRec(), pcPicBottom->getPicYuvRec(),
-#endif
-                                        conf.getWindowLeftOffset() + defDisp.getWindowLeftOffset(),
-                                        conf.getWindowRightOffset() + defDisp.getWindowRightOffset(),
-                                        conf.getWindowTopOffset() + defDisp.getWindowTopOffset(),
-                                        conf.getWindowBottomOffset() + defDisp.getWindowBottomOffset(), isTff );
-        }
-        
+        m_cTVideoIOYuvReconFile.write( pcPicTop->getPicYuvRec(), pcPicBottom->getPicYuvRec(),
+#endif
+                                           m_outputColourSpaceConvert,
+                                           conf.getWindowLeftOffset() + defDisp.getWindowLeftOffset(),
+                                           conf.getWindowRightOffset() + defDisp.getWindowRightOffset(),
+                                           conf.getWindowTopOffset() + defDisp.getWindowTopOffset(),
+#if NH_3D
+                                           conf.getWindowBottomOffset() + defDisp.getWindowBottomOffset(), m_depth420OutputFlag && pcPicTop->getIsDepth() ? CHROMA_420 : NUM_CHROMA_FORMAT, isTff );
+#else
+                                           conf.getWindowBottomOffset() + defDisp.getWindowBottomOffset(), NUM_CHROMA_FORMAT, isTff );
+#endif
+          }
+        }
+
         // update POC of display order
-#if H_MV
+#if NH_MV
         m_pocLastDisplay[decIdx] = pcPic->getPOC();
 #else
         m_iPOCLastDisplay = pcPicBottom->getPOC();
 #endif
-        
+
         // erase non-referenced picture in the reference picture list after display
         if ( !pcPicTop->getSlice(0)->isReferenced() && pcPicTop->getReconMark() == true )
         {
-#if !DYN_REF_FREE
           pcPicTop->setReconMark(false);
-          
+
           // mark it should be extended later
           pcPicTop->getPicYuvRec()->setBorderExtension( false );
-          
-#else
-          pcPicTop->destroy();
-          pcListPic->erase( iterPic );
-          iterPic = pcListPic->begin(); // to the beginning, non-efficient way, have to be revised!
-          continue;
-#endif
         }
         if ( !pcPicBottom->getSlice(0)->isReferenced() && pcPicBottom->getReconMark() == true )
         {
-#if !DYN_REF_FREE
           pcPicBottom->setReconMark(false);
-          
+
           // mark it should be extended later
           pcPicBottom->getPicYuvRec()->setBorderExtension( false );
-          
-#else
-          pcPicBottom->destroy();
-          pcListPic->erase( iterPic );
-          iterPic = pcListPic->begin(); // to the beginning, non-efficient way, have to be revised!
-          continue;
-#endif
         }
         pcPicTop->setOutputMark(false);
@@ -721,9 +763,10 @@
   {
     iterPic = pcListPic->begin();
+
     while (iterPic != pcListPic->end())
     {
       pcPic = *(iterPic);
 
-#if H_MV
+#if NH_MV
       if(pcPic->getOutputMark() && pcPic->getPOC() > m_pocLastDisplay[decIdx] &&
         (numPicsNotYetDisplayed >  numReorderPicsHighestTid || dpbFullness > maxDecPicBufferingHighestTid))
@@ -734,10 +777,10 @@
       {
         // write to file
-        numPicsNotYetDisplayed--;
+         numPicsNotYetDisplayed--;
         if(pcPic->getSlice(0)->isReferenced() == false)
         {
           dpbFullness--;
         }
-#if H_MV
+#if NH_MV
       if ( m_pchReconFiles[decIdx] )
 #else
@@ -745,7 +788,7 @@
 #endif
         {
-          const Window &conf = pcPic->getConformanceWindow();
-          const Window &defDisp = m_respectDefDispWindow ? pcPic->getDefDisplayWindow() : Window();
-#if H_MV
+          const Window &conf    = pcPic->getConformanceWindow();
+          const Window defDisp = m_respectDefDispWindow ? pcPic->getDefDisplayWindow() : Window();
+#if NH_MV
         assert( conf   .getScaledFlag() );
         assert( defDisp.getScaledFlag() );
@@ -760,47 +803,46 @@
           m_cTVideoIOYuvReconFile.write( pcPic->getPicYuvRec(),
 #endif
-                                        conf.getWindowLeftOffset() + defDisp.getWindowLeftOffset(),
-                                        conf.getWindowRightOffset() + defDisp.getWindowRightOffset(),
-                                        conf.getWindowTopOffset() + defDisp.getWindowTopOffset(),
-                                        conf.getWindowBottomOffset() + defDisp.getWindowBottomOffset() );
-        }
-        
+                                         m_outputColourSpaceConvert,
+                                         conf.getWindowLeftOffset() + defDisp.getWindowLeftOffset(),
+                                         conf.getWindowRightOffset() + defDisp.getWindowRightOffset(),
+                                         conf.getWindowTopOffset() + defDisp.getWindowTopOffset(),
+                                         conf.getWindowBottomOffset() + defDisp.getWindowBottomOffset(), 
+#if NH_3D
+                                          m_depth420OutputFlag && pcPic->getIsDepth() ? CHROMA_420 : NUM_CHROMA_FORMAT,
+#else
+                                          NUM_CHROMA_FORMAT,
+#endif
+           m_bClipOutputVideoToRec709Range   );
+        }
+
         // update POC of display order
-#if H_MV
+#if NH_MV
         m_pocLastDisplay[decIdx] = pcPic->getPOC();
 #else
         m_iPOCLastDisplay = pcPic->getPOC();
 #endif
-        
+
         // erase non-referenced picture in the reference picture list after display
         if ( !pcPic->getSlice(0)->isReferenced() && pcPic->getReconMark() == true )
         {
-#if !DYN_REF_FREE
           pcPic->setReconMark(false);
-          
+
           // mark it should be extended later
           pcPic->getPicYuvRec()->setBorderExtension( false );
-          
-#else
-          pcPic->destroy();
-          pcListPic->erase( iterPic );
-          iterPic = pcListPic->begin(); // to the beginning, non-efficient way, have to be revised!
-          continue;
-#endif
         }
         pcPic->setOutputMark(false);
-#if H_MV
+#if NH_MV
         pcPic->setPicOutputFlag(false);
 #endif
       }
-      
+
       iterPic++;
     }
   }
 }
+
 /** \param pcListPic list of pictures to be written to file
-    \todo            DYN_REF_FREE should be revised
  */
-#if H_MV
+#if NH_MV
 Void TAppDecTop::xFlushOutput( TComList<TComPic*>* pcListPic, Int decIdx )
 #else
@@ -813,8 +855,8 @@
   }
   TComList<TComPic*>::iterator iterPic   = pcListPic->begin();
-  
+
   iterPic   = pcListPic->begin();
   TComPic* pcPic = *(iterPic);
-  
+
   if (pcPic->isField()) //Field Decoding
   {
@@ -827,9 +869,9 @@
       iterPic++;
       pcPicBottom = *(iterPic);
-      
+
       if ( pcPicTop->getOutputMark() && pcPicBottom->getOutputMark() && !(pcPicTop->getPOC()%2) && (pcPicBottom->getPOC() == pcPicTop->getPOC()+1) )
       {
         // write to file
-#if H_MV
+#if NH_MV
       if ( m_pchReconFiles[decIdx] )
 #else
@@ -838,7 +880,7 @@
         {
           const Window &conf = pcPicTop->getConformanceWindow();
-          const Window &defDisp = m_respectDefDispWindow ? pcPicTop->getDefDisplayWindow() : Window();
+          const Window  defDisp = m_respectDefDispWindow ? pcPicTop->getDefDisplayWindow() : Window();
           const Bool isTff = pcPicTop->isTopField();
-#if H_MV
+#if NH_MV
         assert( conf   .getScaledFlag() );
         assert( defDisp.getScaledFlag() );
@@ -853,51 +895,39 @@
           m_cTVideoIOYuvReconFile.write( pcPicTop->getPicYuvRec(), pcPicBottom->getPicYuvRec(),
 #endif
-                                        conf.getWindowLeftOffset() + defDisp.getWindowLeftOffset(),
-                                        conf.getWindowRightOffset() + defDisp.getWindowRightOffset(),
-                                        conf.getWindowTopOffset() + defDisp.getWindowTopOffset(),
-                                        conf.getWindowBottomOffset() + defDisp.getWindowBottomOffset(), isTff );
-        }
-        
+                                         m_outputColourSpaceConvert,
+                                         conf.getWindowLeftOffset() + defDisp.getWindowLeftOffset(),
+                                         conf.getWindowRightOffset() + defDisp.getWindowRightOffset(),
+                                         conf.getWindowTopOffset() + defDisp.getWindowTopOffset(),
+#if NH_3D
+                                         conf.getWindowBottomOffset() + defDisp.getWindowBottomOffset(), m_depth420OutputFlag && pcPicTop->getIsDepth() ? CHROMA_420 : NUM_CHROMA_FORMAT, isTff );
+#else
+                                         conf.getWindowBottomOffset() + defDisp.getWindowBottomOffset(), NUM_CHROMA_FORMAT, isTff );
+#endif
+        }
+
         // update POC of display order
-#if H_MV
+#if NH_MV
       m_pocLastDisplay[decIdx] = pcPic->getPOC();
 #else
-      m_iPOCLastDisplay = pcPicBottom->getPOC();
+        m_iPOCLastDisplay = pcPicBottom->getPOC();
 #endif        
         // erase non-referenced picture in the reference picture list after display
         if ( !pcPicTop->getSlice(0)->isReferenced() && pcPicTop->getReconMark() == true )
         {
-#if !DYN_REF_FREE
           pcPicTop->setReconMark(false);
-          
+
           // mark it should be extended later
           pcPicTop->getPicYuvRec()->setBorderExtension( false );
-          
-#else
-          pcPicTop->destroy();
-          pcListPic->erase( iterPic );
-          iterPic = pcListPic->begin(); // to the beginning, non-efficient way, have to be revised!
-          continue;
-#endif
         }
         if ( !pcPicBottom->getSlice(0)->isReferenced() && pcPicBottom->getReconMark() == true )
         {
-#if !DYN_REF_FREE
           pcPicBottom->setReconMark(false);
-          
+
           // mark it should be extended later
           pcPicBottom->getPicYuvRec()->setBorderExtension( false );
-          
-#else
-          pcPicBottom->destroy();
-          pcListPic->erase( iterPic );
-          iterPic = pcListPic->begin(); // to the beginning, non-efficient way, have to be revised!
-          continue;
-#endif
         }
         pcPicTop->setOutputMark(false);
         pcPicBottom->setOutputMark(false);
-        
-#if !DYN_REF_FREE
+
         if(pcPicTop)
         {
@@ -906,5 +936,4 @@
           pcPicTop = NULL;
         }
-#endif
       }
     }
@@ -921,9 +950,9 @@
     {
       pcPic = *(iterPic);
-      
+
       if ( pcPic->getOutputMark() )
       {
         // write to file
-#if H_MV
+#if NH_MV
       if ( m_pchReconFiles[decIdx] )
 #else
@@ -931,7 +960,7 @@
 #endif
         {
-          const Window &conf = pcPic->getConformanceWindow();
-          const Window &defDisp = m_respectDefDispWindow ? pcPic->getDefDisplayWindow() : Window();
-#if H_MV
+          const Window &conf    = pcPic->getConformanceWindow();
+          const Window  defDisp = m_respectDefDispWindow ? pcPic->getDefDisplayWindow() : Window();
+#if NH_MV
         assert( conf   .getScaledFlag() );
         assert( defDisp.getScaledFlag() );
@@ -946,41 +975,39 @@
           m_cTVideoIOYuvReconFile.write( pcPic->getPicYuvRec(),
 #endif
-                                        conf.getWindowLeftOffset() + defDisp.getWindowLeftOffset(),
-                                        conf.getWindowRightOffset() + defDisp.getWindowRightOffset(),
-                                        conf.getWindowTopOffset() + defDisp.getWindowTopOffset(),
-                                        conf.getWindowBottomOffset() + defDisp.getWindowBottomOffset() );
-        }
-        
+                                         m_outputColourSpaceConvert,
+                                         conf.getWindowLeftOffset() + defDisp.getWindowLeftOffset(),
+                                         conf.getWindowRightOffset() + defDisp.getWindowRightOffset(),
+                                         conf.getWindowTopOffset() + defDisp.getWindowTopOffset(),
+                                         conf.getWindowBottomOffset() + defDisp.getWindowBottomOffset(), 
+#if NH_3D
+                                         m_depth420OutputFlag && pcPic->getIsDepth() ? CHROMA_420 : NUM_CHROMA_FORMAT
+#else
+                                         NUM_CHROMA_FORMAT
+#endif
+                                         , m_bClipOutputVideoToRec709Range);
+        }
+
         // update POC of display order
-#if H_MV
+#if NH_MV
       m_pocLastDisplay[decIdx] = pcPic->getPOC();
 #else
         m_iPOCLastDisplay = pcPic->getPOC();
 #endif
-        
+
         // erase non-referenced picture in the reference picture list after display
         if ( !pcPic->getSlice(0)->isReferenced() && pcPic->getReconMark() == true )
         {
-#if !DYN_REF_FREE
           pcPic->setReconMark(false);
-          
+
           // mark it should be extended later
           pcPic->getPicYuvRec()->setBorderExtension( false );
-          
-#else
-          pcPic->destroy();
-          pcListPic->erase( iterPic );
-          iterPic = pcListPic->begin(); // to the beginning, non-efficient way, have to be revised!
-          continue;
-#endif
         }
         pcPic->setOutputMark(false);
-#if H_MV
+#if NH_MV
         pcPic->setPicOutputFlag(false);
 #endif
       }
-#if !H_MV
-#if !DYN_REF_FREE
-      if(pcPic)
+#if !NH_MV
+      if(pcPic != NULL)
       {
         pcPic->destroy();
@@ -988,10 +1015,9 @@
         pcPic = NULL;
       }
-#endif    
 #endif
       iterPic++;
     }
   }
-#if H_MV
+#if NH_MV
   m_pocLastDisplay[decIdx] = -MAX_INT;
 #else
@@ -1011,8 +1037,8 @@
   for (std::vector<Int>::iterator it = m_targetDecLayerIdSet.begin(); it != m_targetDecLayerIdSet.end(); it++)
   {
-#if H_MV
-    if ( nalu->m_layerId == (*it) )
-#else
-    if ( nalu->m_reservedZero6Bits == (*it) )
+#if NH_MV
+    if ( nalu->m_nuhLayerId == (*it) )
+#else
+    if ( nalu->m_nuhLayerId == (*it) )
 #endif
     {
@@ -1023,5 +1049,5 @@
 }
 
-#if H_MV
+#if NH_MV
 Int TAppDecTop::xGetDecoderIdx( Int layerId, Bool createFlag /*= false */ )
 {
@@ -1052,10 +1078,14 @@
     m_tDecTop[ decIdx ]->setIvPicLists( &m_ivPicLists ); 
     m_tDecTop[ decIdx ]->setLayerInitilizedFlags( m_layerInitilizedFlags );
-    m_tDecTop[ decIdx ]->setTargetOptLayerSetIdx( m_targetOptLayerSetIdx );    
-#if H_3D
-    m_tDecTop[ decIdx ]->setProfileIdc           ( );    
-#endif
-
-#if H_3D
+    m_tDecTop[ decIdx ]->setTargetOlsIdx( m_targetOptLayerSetIdx );    
+#if O0043_BEST_EFFORT_DECODING
+    m_cTDecTop[ decIdx ]->setForceDecodeBitDepth(m_forceDecodeBitDepth);
+#endif
+    if (!m_outputDecodedSEIMessagesFilename.empty())
+    {
+      std::ostream &os=m_seiMessageFileStream.is_open() ? m_seiMessageFileStream : std::cout;
+      m_tDecTop[ decIdx ]->setDecodedSEIMessageOutputStream(&os);
+    }
+#if NH_3D
    m_tDecTop[ decIdx ]->setCamParsCollector( &m_cCamParsCollector );
 #endif
Index: branches/HTM-14.1-update-dev0/source/App/TAppDecoder/TAppDecTop.h
===================================================================
--- branches/HTM-14.1-update-dev0/source/App/TAppDecoder/TAppDecTop.h	(revision 1199)
+++ branches/HTM-14.1-update-dev0/source/App/TAppDecoder/TAppDecTop.h	(revision 1200)
@@ -2,7 +2,7 @@
  * License, included below. This software may be subject to other third party
  * and contributor rights, including patent rights, and no such rights are
- * granted under this license.  
+ * granted under this license.
  *
-* Copyright (c) 2010-2015, ITU/ISO/IEC
+ * Copyright (c) 2010-2015, ITU/ISO/IEC
  * All rights reserved.
  *
@@ -61,5 +61,5 @@
 private:
   // class interface
-#if H_MV
+#if NH_MV
   TDecTop*                        m_tDecTop             [ MAX_NUM_LAYERS ];    ///< decoder classes
   TVideoIOYuv*                    m_tVideoIOYuvReconFile[ MAX_NUM_LAYERS ];    ///< reconstruction YUV class
@@ -68,11 +68,11 @@
   TComPicLists                    m_ivPicLists;                                ///< picture buffers of decoder instances
   Bool                            m_layerInitilizedFlags[ MAX_NUM_LAYER_IDS ]; ///< for layerwise startup 
-  TComVPS*                        m_vps;                                ///< active VPS
+  const TComVPS*                  m_vps;                                ///< active VPS
 #else
   TDecTop                         m_cTDecTop;                     ///< decoder class
   TVideoIOYuv                     m_cTVideoIOYuvReconFile;        ///< reconstruction YUV class
 #endif
-    // for output control
-#if H_MV
+  // for output control
+#if NH_MV
   Int                             m_pocLastDisplay      [ MAX_NUM_LAYERS ]; ///< last POC in display order
   Bool                            m_reconOpen           [ MAX_NUM_LAYERS ]; ///< reconstruction file opened
@@ -81,6 +81,7 @@
   Int                             m_iPOCLastDisplay;              ///< last POC in display order
 #endif
+  std::ofstream                   m_seiMessageFileStream;         ///< Used for outputing SEI messages.
 
-#if H_3D
+#if NH_3D
   FILE*                           m_pScaleOffsetFile;
   CamParsCollector                m_cCamParsCollector;
@@ -89,8 +90,24 @@
   TAppDecTop();
   virtual ~TAppDecTop() {}
-  
+
   Void  create            (); ///< create internal members
   Void  destroy           (); ///< destroy internal members
   Void  decode            (); ///< main decoding function
+#if NH_MV
+  UInt  getNumberOfChecksumErrorsDetected( ) const 
+  {
+    UInt numOfChecksumErrors = 0; 
+    for (Int i = 0; i < m_numDecoders; i++ )
+    {
+      numOfChecksumErrors += getNumberOfChecksumErrorsDetected( i ); 
+    }
+    return numOfChecksumErrors; 
+  }
+
+  UInt  getNumberOfChecksumErrorsDetected( Int decIdx ) const { return m_tDecTop[decIdx]->getNumberOfChecksumErrorsDetected(); }
+
+#else
+  UInt  getNumberOfChecksumErrorsDetected() const { return m_cTDecTop.getNumberOfChecksumErrorsDetected(); }
+#endif
 
 protected:
@@ -99,5 +116,5 @@
   Void  xInitDecLib       (); ///< initialize decoder class
 
-#if H_MV
+#if NH_MV
   Void  xWriteOutput      ( TComList<TComPic*>* pcListPic, Int layerId, Int tId ); ///< write YUV to file
 
Index: branches/HTM-14.1-update-dev0/source/App/TAppDecoder/decmain.cpp
===================================================================
--- branches/HTM-14.1-update-dev0/source/App/TAppDecoder/decmain.cpp	(revision 1199)
+++ branches/HTM-14.1-update-dev0/source/App/TAppDecoder/decmain.cpp	(revision 1200)
@@ -2,7 +2,7 @@
  * License, included below. This software may be subject to other third party
  * and contributor rights, including patent rights, and no such rights are
- * granted under this license.  
+ * granted under this license.
  *
-* Copyright (c) 2010-2015, ITU/ISO/IEC
+ * Copyright (c) 2010-2015, ITU/ISO/IEC
  * All rights reserved.
  *
@@ -44,6 +44,4 @@
 //! \{
 
-bool g_md5_mismatch = false; ///< top level flag that indicates if there has been a decoding mismatch
-
 // ====================================================================================================================
 // Main function
@@ -52,12 +50,13 @@
 int main(int argc, char* argv[])
 {
+  Int returnCode = EXIT_SUCCESS;
   TAppDecTop  cTAppDecTop;
 
   // print information
   fprintf( stdout, "\n" );
-#if H_MV
+#if NH_MV
   fprintf( stdout, "3D-HTM Software: Decoder Version [%s] based on HM Version [%s]", NV_VERSION, HM_VERSION );  
 #else
-  fprintf( stdout, "HM software: Decoder Version [%s]", NV_VERSION );
+  fprintf( stdout, "HM software: Decoder Version [%s] (including RExt)", NV_VERSION );
 #endif
   fprintf( stdout, NVM_ONOS );
@@ -73,21 +72,23 @@
   {
     cTAppDecTop.destroy();
-    return 1;
+    returnCode = EXIT_FAILURE;
+    return returnCode;
   }
 
   // starting time
-  double dResult;
-  long lBefore = clock();
+  Double dResult;
+  clock_t lBefore = clock();
 
   // call decoding function
   cTAppDecTop.decode();
 
-  if (g_md5_mismatch)
+  if (cTAppDecTop.getNumberOfChecksumErrorsDetected() != 0)
   {
     printf("\n\n***ERROR*** A decoding mismatch occured: signalled md5sum does not match\n");
+    returnCode = EXIT_FAILURE;
   }
 
   // ending time
-  dResult = (double)(clock()-lBefore) / CLOCKS_PER_SEC;
+  dResult = (Double)(clock()-lBefore) / CLOCKS_PER_SEC;
   printf("\n Total Time: %12.3f sec.\n", dResult);
 
@@ -95,5 +96,5 @@
   cTAppDecTop.destroy();
 
-  return g_md5_mismatch ? EXIT_FAILURE : EXIT_SUCCESS;
+  return returnCode;
 }
 
