Index: branches/SHM-upgrade/source/App/TAppDecoder/TAppDecCfg.cpp
===================================================================
--- branches/SHM-upgrade/source/App/TAppDecoder/TAppDecCfg.cpp	(revision 927)
+++ branches/SHM-upgrade/source/App/TAppDecoder/TAppDecCfg.cpp	(revision 928)
@@ -73,4 +73,7 @@
   Int olsIdx;
 #endif
+#if CONFORMANCE_BITSTREAM_MODE
+  string cfg_confPrefix;
+#endif
 #if AVC_BASE
   string cfg_BLReconFile;
@@ -102,4 +105,8 @@
 #if OUTPUT_LAYER_SET_INDEX
   ("OutpuLayerSetIdx,-olsidx", olsIdx, -1, "Index of output layer set to be decoded.")
+#endif
+#if CONFORMANCE_BITSTREAM_MODE
+  ("ConformanceBitstremMode,-confMode", m_confModeFlag, false, "Enable generation of conformance bitstream metadata; True: Generate metadata, False: No metadata generated")
+  ("ConformanceMetadataPrefix,-confPrefix", cfg_confPrefix, string(""), "Prefix for the file name of the conformance data. Default name - 'decodedBitstream'")
 #endif
 #else
@@ -160,5 +167,33 @@
   assert( m_tgtLayerId < MAX_NUM_LAYER_IDS );
 #endif
-#if OUTPUT_LAYER_SET_INDEX  
+#if OUTPUT_LAYER_SET_INDEX
+#if CONFORMANCE_BITSTREAM_MODE
+  if( m_confModeFlag )
+  {
+    assert( olsIdx != -1 ); // In the conformance mode, target output layer set index is to be explicitly specified.
+
+    if( cfg_confPrefix.empty() )
+    {
+      m_confPrefix = string("decodedBitstream");
+    }
+    else
+    {
+      m_confPrefix = cfg_confPrefix;
+    }
+      // Open metadata file and write
+    char fileNameSuffix[255];
+    sprintf(fileNameSuffix, "%s-OLS%d.opl", m_confPrefix.c_str(), olsIdx);  // olsIdx is the target output layer set index.
+    m_metadataFileName = string(fileNameSuffix);
+    m_metadataFileRefresh = true;
+
+    // Decoded layer YUV files
+    for(UInt layer=0; layer<= m_tgtLayerId; layer++)
+    {
+      sprintf(fileNameSuffix, "%s-L%d.yuv", m_confPrefix.c_str(), layer);  // olsIdx is the target output layer set index.
+      m_decodedYuvLayerFileName[layer] = std::string( fileNameSuffix );
+      m_decodedYuvLayerRefresh[layer] = true;
+    }
+  }
+#endif
   this->getCommonDecoderParams()->setTargetOutputLayerSetIdx( olsIdx       );
   this->getCommonDecoderParams()->setTargetLayerId    ( m_tgtLayerId );
Index: branches/SHM-upgrade/source/App/TAppDecoder/TAppDecCfg.h
===================================================================
--- branches/SHM-upgrade/source/App/TAppDecoder/TAppDecCfg.h	(revision 927)
+++ branches/SHM-upgrade/source/App/TAppDecoder/TAppDecCfg.h	(revision 928)
@@ -94,4 +94,12 @@
   CommonDecoderParams             m_commonDecoderParams;
 #endif
+#if CONFORMANCE_BITSTREAM_MODE
+  Bool          m_confModeFlag;
+  std::string   m_confPrefix;
+  std::string   m_metadataFileName;
+  Bool          m_metadataFileRefresh;
+  std::string   m_decodedYuvLayerFileName[63];
+  Bool          m_decodedYuvLayerRefresh[63];
+#endif
 
 public:
@@ -131,4 +139,15 @@
   CommonDecoderParams* getCommonDecoderParams() {return &m_commonDecoderParams;}
 #endif
+#if CONFORMANCE_BITSTREAM_MODE
+  Bool const getConfModeFlag() { return m_confModeFlag;  }
+  std::string const getConfPrefix() { return m_confPrefix;}
+  std::string const getMetadataFileName() { return m_metadataFileName; }
+  Bool const getMetadataFileRefresh() {return m_metadataFileRefresh; }
+  Void setMetadataFileRefresh(Bool const x) {m_metadataFileRefresh = x; }
+  Void setDecodedYuvLayerFileName(Int layerId, std::string x) {  m_decodedYuvLayerFileName[layerId] = x; }
+  std::string const getDecodedYuvLayerFileName(Int layerId) {  return m_decodedYuvLayerFileName[layerId]; }
+  Bool const getDecodedYuvLayerRefresh(Int const layerId) {return m_decodedYuvLayerRefresh[layerId]; }
+  Void setDecodedYuvLayerRefresh(Int const layerId, Bool const x) {m_decodedYuvLayerRefresh[layerId] = x; }
+#endif
 };
 
Index: branches/SHM-upgrade/source/App/TAppDecoder/TAppDecTop.cpp
===================================================================
--- branches/SHM-upgrade/source/App/TAppDecoder/TAppDecTop.cpp	(revision 927)
+++ branches/SHM-upgrade/source/App/TAppDecoder/TAppDecTop.cpp	(revision 928)
@@ -48,4 +48,8 @@
 #include "TLibCommon/TComCodingStatistics.h"
 #endif
+#if CONFORMANCE_BITSTREAM_MODE
+#include "TLibCommon/TComPicYuv.h"
+#include "libmd5/MD5.h"
+#endif
 
 //! \ingroup TAppDecoder
@@ -630,7 +634,13 @@
     m_acTDecTop[layer].setNumLayer( m_tgtLayerId + 1 );
 #if OUTPUT_LAYER_SET_INDEX
-    m_acTDecTop[layer].setCommonDecoderParams( this->getCommonDecoderParams() );
-#endif
-  }
+    m_acTDecTop[layer].setCommonDecoderParams( &m_commonDecoderParams );
+#endif
+  }
+#if CONFORMANCE_BITSTREAM_MODE
+  for(UInt layer = 0; layer <= MAX_LAYERS; layer++)
+  {
+    m_acTDecTop[layer].setConfModeFlag ( m_confModeFlag );
+  }
+#endif
 #else
   m_cTDecTop.init();
@@ -1408,4 +1418,70 @@
 
       xOutputAndMarkPic( pic, m_pchReconFile[layerIdx], layerIdx, m_aiPOCLastDisplay[layerIdx], dpbStatus );
+
+#if CONFORMANCE_BITSTREAM_MODE
+      FILE *fptr;
+      if( m_confModeFlag )
+      {
+        if( m_metadataFileRefresh )
+        {
+          fptr = fopen( this->getMetadataFileName().c_str(), "w" );
+          fprintf(fptr, " LayerId      POC    MD5\n");
+          fprintf(fptr, "------------------------\n");
+        }
+        else
+        {
+          fptr = fopen( this->getMetadataFileName().c_str(), "a+" );
+        }
+        this->setMetadataFileRefresh(false);
+
+        TComDigest recon_digest;
+        calcMD5(*pic->getPicYuvRec(), recon_digest);
+        fprintf(fptr, "%8d%9d    MD5:%s\n", pic->getLayerId(), pic->getSlice(0)->getPOC(), digestToString(recon_digest, 16));
+        fclose(fptr);
+
+        // Output all picutres "decoded" in that layer that have POC less than the current picture
+        std::vector<TComPic> *layerBuffer = (m_acTDecTop->getLayerDec(pic->getLayerId()))->getConfListPic();
+        // Write all pictures to the file.
+        if( this->getDecodedYuvLayerRefresh(layerIdx) )
+        {
+          if (!m_outputBitDepth[CHANNEL_TYPE_LUMA]) { m_outputBitDepth[CHANNEL_TYPE_LUMA] = g_bitDepth[CHANNEL_TYPE_LUMA]; }
+          if (!m_outputBitDepth[CHANNEL_TYPE_CHROMA]) { m_outputBitDepth[CHANNEL_TYPE_CHROMA] = g_bitDepth[CHANNEL_TYPE_CHROMA]; }
+
+          char tempFileName[256];
+          strcpy(tempFileName, this->getDecodedYuvLayerFileName( layerIdx ).c_str());
+          m_confReconFile[layerIdx].open(tempFileName, true, m_outputBitDepth, m_outputBitDepth, g_bitDepth ); // write mode
+          this->setDecodedYuvLayerRefresh( layerIdx, false );
+        }
+        const Window &conf = pic->getConformanceWindow();
+        const Window &defDisp = m_respectDefDispWindow ? pic->getDefDisplayWindow() : Window();
+        Int xScal =  1, yScal = 1;
+#if REPN_FORMAT_IN_VPS
+        UInt chromaFormatIdc = pic->getSlice(0)->getChromaFormatIdc();
+        xScal = TComSPS::getWinUnitX( chromaFormatIdc );
+        yScal = TComSPS::getWinUnitY( chromaFormatIdc );
+#endif
+        std::vector<TComPic>::iterator iterPic;
+        for(iterPic = layerBuffer->begin(); iterPic != layerBuffer->end(); iterPic++)
+        {
+          TComPic checkPic = *iterPic;
+          if( checkPic.getPOC() <= pic->getPOC() )
+          {
+            TComPicYuv* pPicCYuvRec = checkPic.getPicYuvRec();
+            m_confReconFile[layerIdx].write( pPicCYuvRec, m_outputColourSpaceConvert,
+              conf.getWindowLeftOffset()  * xScal + defDisp.getWindowLeftOffset(),
+              conf.getWindowRightOffset() * xScal + defDisp.getWindowRightOffset(),
+              conf.getWindowTopOffset()   * yScal + defDisp.getWindowTopOffset(),
+              conf.getWindowBottomOffset()* yScal + defDisp.getWindowBottomOffset() );
+            layerBuffer->erase(iterPic);
+            iterPic = layerBuffer->begin();  // Ensure doesn't go to infinite loop
+            if(layerBuffer->size() == 0)
+            {
+              break;
+            }
+          }
+        }
+      }
+      // Now to remove the pictures that have been output
+#endif
 
       listOfPocsInEachLayer[layerIdx].erase( it );
Index: branches/SHM-upgrade/source/App/TAppDecoder/TAppDecTop.h
===================================================================
--- branches/SHM-upgrade/source/App/TAppDecoder/TAppDecTop.h	(revision 927)
+++ branches/SHM-upgrade/source/App/TAppDecoder/TAppDecTop.h	(revision 928)
@@ -74,4 +74,7 @@
   // for output control  
 #if SVC_EXTENSION
+#if CONFORMANCE_BITSTREAM_MODE
+  TVideoIOYuv                      m_confReconFile[63];        ///< decode YUV files
+#endif 
   Int                             m_aiPOCLastDisplay [MAX_LAYERS]; ///< last POC in display order
 #else
Index: branches/SHM-upgrade/source/App/TAppEncoder/TAppEncTop.cpp
===================================================================
--- branches/SHM-upgrade/source/App/TAppEncoder/TAppEncTop.cpp	(revision 927)
+++ branches/SHM-upgrade/source/App/TAppEncoder/TAppEncTop.cpp	(revision 928)
@@ -1616,4 +1616,5 @@
   vps->deriveNumberOfSubDpbs();
 #endif
+  vps->setOutputLayerFlag( 0, 0, 1 );
 
   // derive OutputLayerFlag[i][j] 
Index: branches/SHM-upgrade/source/Lib/TLibCommon/TypeDef.h
===================================================================
--- branches/SHM-upgrade/source/Lib/TLibCommon/TypeDef.h	(revision 927)
+++ branches/SHM-upgrade/source/Lib/TLibCommon/TypeDef.h	(revision 928)
@@ -46,4 +46,5 @@
 #define MAX_LAYERS                       8      ///< max number of layers the codec is supposed to handle
 
+#define CONFORMANCE_BITSTREAM_MODE       1      ///< In order to generate the metadata related to conformance bitstreams
 #define SIGNALLING_BITRATE_PICRATE_FIX   1      ///< Fix for signalling of bitrate and picture rate info in VPS VUI to be more aligned to JCTVC-R1008
 #define INFERENCE_POC_MSB_VAL_PRESENT    1      ///< JCTVC-Q0146 -- poc_msb_val_present_flag shall be equal to 0 when slice_header_extension_length is (inferred to be ) equal to 0
Index: branches/SHM-upgrade/source/Lib/TLibDecoder/TDecGop.cpp
===================================================================
--- branches/SHM-upgrade/source/Lib/TLibDecoder/TDecGop.cpp	(revision 927)
+++ branches/SHM-upgrade/source/Lib/TLibDecoder/TDecGop.cpp	(revision 928)
@@ -45,4 +45,7 @@
 #if SVC_EXTENSION
 #include "TDecTop.h"
+#if CONFORMANCE_BITSTREAM_MODE
+#include <algorithm>
+#endif
 #endif
 
@@ -50,4 +53,11 @@
 
 extern Bool g_md5_mismatch; ///< top level flag to signal when there is a decode problem
+
+#if CONFORMANCE_BITSTREAM_MODE
+Bool pocCompareFunction( TComPic &pic1, TComPic &pic2 )
+{
+  return (pic1.getPOC() < pic2.getPOC());
+}
+#endif
 
 //! \ingroup TLibDecoder
@@ -241,4 +251,13 @@
     calcAndPrintHashStatus(*(pcPic->getPicYuvRec()), hash);
   }
+#if CONFORMANCE_BITSTREAM_MODE
+  if( this->getLayerDec(pcPic->getLayerId())->getConfModeFlag() ) 
+  {
+    // Add this reconstructed picture to the parallel buffer.
+    std::vector<TComPic> *thisLayerBuffer = (this->getLayerDec(pcPic->getLayerId()))->getConfListPic();
+    thisLayerBuffer->push_back(*pcPic);
+    std::sort( thisLayerBuffer->begin(), thisLayerBuffer->end(), pocCompareFunction );
+  }
+#endif
 #if Q0074_COLOUR_REMAPPING_SEI
   if (m_colourRemapSEIEnabled)
Index: branches/SHM-upgrade/source/Lib/TLibDecoder/TDecTop.cpp
===================================================================
--- branches/SHM-upgrade/source/Lib/TLibDecoder/TDecTop.cpp	(revision 927)
+++ branches/SHM-upgrade/source/Lib/TLibDecoder/TDecTop.cpp	(revision 928)
@@ -129,4 +129,7 @@
   m_pocDecrementedInDPBFlag = false;
 #endif
+#if CONFORMANCE_BITSTREAM_MODE
+  m_confModeFlag = false;
+#endif
 }
 
@@ -2960,7 +2963,11 @@
   {
     // Check if the target decoded layer is the highest layer in the list
+#if !CONFORMANCE_BITSTREAM_MODE
     assert( params->getTargetOutputLayerSetIdx() < vps->getNumLayerSets() );
+#endif
     Int layerSetIdx = vps->getOutputLayerSetIdx( params->getTargetOutputLayerSetIdx() );  // Index to the layer set
+#if !CONFORMANCE_BITSTREAM_MODE
     assert( params->getTargetLayerId() == vps->getNumLayersInIdList( layerSetIdx ) - 1);
+#endif
 
     Bool layerSetMatchFlag = true;
Index: branches/SHM-upgrade/source/Lib/TLibDecoder/TDecTop.h
===================================================================
--- branches/SHM-upgrade/source/Lib/TLibDecoder/TDecTop.h	(revision 927)
+++ branches/SHM-upgrade/source/Lib/TLibDecoder/TDecTop.h	(revision 928)
@@ -170,4 +170,8 @@
                                            // When new VPS is activated, this should be re-initialized to -1
 #endif
+#if CONFORMANCE_BITSTREAM_MODE
+  Bool m_confModeFlag;
+  std::vector<TComPic>   m_confListPic;         //  Dynamic buffer for storing pictures for conformance purposes
+#endif
 public:
 #if POC_RESET_RESTRICTIONS
@@ -304,4 +308,10 @@
   Void assignSubDpbs(TComVPS *vps);
 #endif
+#if CONFORMANCE_BITSTREAM_MODE
+  std::vector<TComPic>* getConfListPic() {return &m_confListPic; }
+  // std::string const getDecodedYuvLayerFileName(Int layerId) { return m_decodedYuvLayerFileName[layerId]; }
+  Bool const getConfModeFlag() { return m_confModeFlag; }
+  Void setConfModeFlag(Bool x) { m_confModeFlag = x; }
+#endif
 #endif //SVC_EXTENSION
 
