Index: branches/SHM-dev/source/Lib/TLibDecoder/NALread.cpp
===================================================================
--- branches/SHM-dev/source/Lib/TLibDecoder/NALread.cpp	(revision 1316)
+++ branches/SHM-dev/source/Lib/TLibDecoder/NALread.cpp	(revision 1319)
@@ -34,5 +34,5 @@
 /**
  \file     NALread.cpp
- \brief    reading funtionality for NAL units
+ \brief    reading functionality for NAL units
  */
 
@@ -105,5 +105,5 @@
 Void readNalUnitHeader(InputNALUnit& nalu)
 {
-  TComInputBitstream& bs = *nalu.m_Bitstream;
+  TComInputBitstream& bs = nalu.getBitstream();
 
   Bool forbidden_zero_bit = bs.read(1);           // forbidden_zero_bit
@@ -165,13 +165,11 @@
  * a bitstream
  */
-Void read(InputNALUnit& nalu, vector<uint8_t>& nalUnitBuf)
+Void read(InputNALUnit& nalu)
 {
-  /* perform anti-emulation prevention */
-  TComInputBitstream *pcBitstream = new TComInputBitstream(NULL);
-  convertPayloadToRBSP(nalUnitBuf, pcBitstream, (nalUnitBuf[0] & 64) == 0);
-
-  nalu.m_Bitstream = new TComInputBitstream(&nalUnitBuf);
-  nalu.m_Bitstream->setEmulationPreventionByteLocation(pcBitstream->getEmulationPreventionByteLocation());
-  delete pcBitstream;
+  TComInputBitstream &bitstream = nalu.getBitstream();
+  vector<uint8_t>& nalUnitBuf=bitstream.getFifo();
+  // perform anti-emulation prevention
+  convertPayloadToRBSP(nalUnitBuf, &bitstream, (nalUnitBuf[0] & 64) == 0);
+  bitstream.resetToStart();
   readNalUnitHeader(nalu);
 }
Index: branches/SHM-dev/source/Lib/TLibDecoder/NALread.h
===================================================================
--- branches/SHM-dev/source/Lib/TLibDecoder/NALread.h	(revision 1316)
+++ branches/SHM-dev/source/Lib/TLibDecoder/NALread.h	(revision 1319)
@@ -34,5 +34,5 @@
 /**
  \file     NALread.h
- \brief    reading funtionality for NAL units
+ \brief    reading functionality for NAL units
  */
 
@@ -53,13 +53,19 @@
  * bitstream object.
  */
-struct InputNALUnit : public NALUnit
+class InputNALUnit : public NALUnit
 {
-  InputNALUnit() : m_Bitstream(0) {};
-  ~InputNALUnit() { delete m_Bitstream; }
+  private:
+    TComInputBitstream m_Bitstream;
 
-  TComInputBitstream* m_Bitstream;
+  public:
+    InputNALUnit(const InputNALUnit &src) : NALUnit(src), m_Bitstream(src.m_Bitstream) {};
+    InputNALUnit() : m_Bitstream() {};
+    virtual ~InputNALUnit() { }
+    const TComInputBitstream &getBitstream() const { return m_Bitstream; }
+          TComInputBitstream &getBitstream()       { return m_Bitstream; }
 };
 
-Void read(InputNALUnit& nalu, std::vector<uint8_t>& nalUnitBuf);
+Void read(InputNALUnit& nalu);
+Void readNalUnitHeader(InputNALUnit& nalu);
 
 //! \}
Index: branches/SHM-dev/source/Lib/TLibDecoder/SEIread.cpp
===================================================================
--- branches/SHM-dev/source/Lib/TLibDecoder/SEIread.cpp	(revision 1316)
+++ branches/SHM-dev/source/Lib/TLibDecoder/SEIread.cpp	(revision 1319)
@@ -471,5 +471,4 @@
 
   /* restore primary bitstream for sei_message */
-  getBitstream()->deleteFifo();
   delete getBitstream();
   setBitstream(bs);
Index: branches/SHM-dev/source/Lib/TLibDecoder/TDecGop.cpp
===================================================================
--- branches/SHM-dev/source/Lib/TLibDecoder/TDecGop.cpp	(revision 1316)
+++ branches/SHM-dev/source/Lib/TLibDecoder/TDecGop.cpp	(revision 1319)
@@ -154,5 +154,4 @@
   for (UInt ui = 0; ui < uiNumSubstreams; ui++)
   {
-    ppcSubstreams[ui]->deleteFifo();
     delete ppcSubstreams[ui];
   }
Index: branches/SHM-dev/source/Lib/TLibDecoder/TDecTop.cpp
===================================================================
--- branches/SHM-dev/source/Lib/TLibDecoder/TDecTop.cpp	(revision 1316)
+++ branches/SHM-dev/source/Lib/TLibDecoder/TDecTop.cpp	(revision 1319)
@@ -63,9 +63,46 @@
 
 TDecTop::TDecTop()
-  : m_pDecodedSEIOutputStream(NULL),
-    m_warningMessageSkipPicture(false)
-{
-  m_pcPic = 0;
-  m_iMaxRefPicNum = 0;
+  : m_iMaxRefPicNum(0)
+  , m_associatedIRAPType(NAL_UNIT_INVALID)
+  , m_pocCRA(0)
+  , m_pocRandomAccess(MAX_INT)
+  , m_cListPic()
+  , m_parameterSetManager()
+  , m_apcSlicePilot(NULL)
+  , m_SEIs()
+  , m_cPrediction()
+  , m_cTrQuant()
+  , m_cGopDecoder()
+  , m_cSliceDecoder()
+  , m_cCuDecoder()
+  , m_cEntropyDecoder()
+  , m_cCavlcDecoder()
+  , m_cSbacDecoder()
+  , m_cBinCABAC()
+  , m_seiReader()
+  , m_cLoopFilter()
+  , m_cSAO()
+  , m_pcPic(NULL)
+#if !SVC_EXTENSION
+  , m_prevPOC(MAX_INT)
+#endif
+  , m_prevTid0POC(0)
+  , m_bFirstSliceInPicture(true)
+#if !SVC_EXTENSION
+  , m_bFirstSliceInSequence(true)
+#endif
+  , m_prevSliceSkipped(false)
+  , m_skippedPOC(0)
+  , m_bFirstSliceInBitstream(true)
+  , m_lastPOCNoOutputPriorPics(-1)
+  , m_isNoOutputPriorPics(false)
+  , m_craNoRaslOutputFlag(false)
+#if O0043_BEST_EFFORT_DECODING
+  , m_forceDecodeBitDepth(8)
+#endif
+  , m_pDecodedSEIOutputStream(NULL)
+  , m_warningMessageSkipPicture(false)
+  , m_prefixSEINALUs()
+{
 #if ENC_DEC_TRACE
   if (g_hTrace == NULL)
@@ -76,21 +113,4 @@
   g_nSymbolCounter = 0;
 #endif
-  m_associatedIRAPType = NAL_UNIT_INVALID;
-  m_pocCRA = 0;
-  m_pocRandomAccess = MAX_INT;
-#if !SVC_EXTENSION
-  m_prevPOC                = MAX_INT;
-#endif
-  m_prevTid0POC            = 0;
-  m_bFirstSliceInPicture    = true;
-#if !SVC_EXTENSION
-  m_bFirstSliceInSequence   = true;
-#endif
-  m_prevSliceSkipped = false;
-  m_skippedPOC = 0;
-  m_bFirstSliceInBitstream  = true;
-  m_lastPOCNoOutputPriorPics = -1;
-  m_craNoRaslOutputFlag = false;
-  m_isNoOutputPriorPics = false;
 
 #if SVC_EXTENSION 
@@ -133,4 +153,9 @@
   }
 #endif
+  while (!m_prefixSEINALUs.empty())
+  {
+    delete m_prefixSEINALUs.front();
+    m_prefixSEINALUs.pop_front();
+  }
 #if CGS_3D_ASYMLUT
   if(m_pColorMappedPic)
@@ -431,4 +456,6 @@
       assert (0);
     }
+
+    xParsePrefixSEImessages();
 
 #if RExt__HIGH_BIT_DEPTH_SUPPORT==0
@@ -699,4 +726,6 @@
     }
 
+    xParsePrefixSEImessages();
+
     // Check if any new SEI has arrived
      if(!m_SEIs.empty())
@@ -709,5 +738,39 @@
      }
   }
-
+}
+
+
+Void TDecTop::xParsePrefixSEIsForUnknownVCLNal()
+{
+  while (!m_prefixSEINALUs.empty())
+  {
+    // do nothing?
+    printf("Discarding Prefix SEI associated with unknown VCL NAL unit.\n");
+    delete m_prefixSEINALUs.front();
+  }
+  // TODO: discard following suffix SEIs as well?
+}
+
+
+Void TDecTop::xParsePrefixSEImessages()
+{
+#if SVC_EXTENSION
+    if (m_prevSliceSkipped) // No need to decode SEI messages of a skipped access unit
+    {
+      return;
+    }
+#endif
+
+  while (!m_prefixSEINALUs.empty())
+  {
+    InputNALUnit &nalu=*m_prefixSEINALUs.front();
+#if LAYERS_NOT_PRESENT_SEI
+    m_seiReader.parseSEImessage( &(nalu.getBitstream()), m_SEIs, nalu.m_nalUnitType, m_parameterSetManager.getActiveVPS(), m_parameterSetManager.getActiveSPS(), m_pDecodedSEIOutputStream );
+#else
+    m_seiReader.parseSEImessage( &(nalu.getBitstream()), m_SEIs, nalu.m_nalUnitType, m_parameterSetManager.getActiveSPS(), m_pDecodedSEIOutputStream );
+#endif
+    delete m_prefixSEINALUs.front();
+    m_prefixSEINALUs.pop_front();
+  }
 }
 
@@ -783,4 +846,6 @@
     m_apcSlicePilot->setPOC(m_skippedPOC);
   }
+
+  xUpdatePreviousTid0POC(m_apcSlicePilot);
 
   m_apcSlicePilot->setAssociatedIRAPPOC(m_pocCRA);
@@ -1795,5 +1860,5 @@
 
   //  Decode a picture
-  m_cGopDecoder.decompressSlice(nalu.m_Bitstream, m_pcPic);
+  m_cGopDecoder.decompressSlice(&(nalu.getBitstream()), m_pcPic);
 
 #if SVC_EXTENSION
@@ -1808,10 +1873,10 @@
 }
 
-Void TDecTop::xDecodeVPS(const std::vector<UChar> *pNaluData)
+Void TDecTop::xDecodeVPS(const std::vector<UChar> &naluData)
 {
   TComVPS* vps = new TComVPS();
 
   m_cEntropyDecoder.decodeVPS( vps );
-  m_parameterSetManager.storeVPS(vps, pNaluData);
+  m_parameterSetManager.storeVPS(vps, naluData);
 #if SVC_EXTENSION
   checkValueOfTargetOutputLayerSetIdx(vps);
@@ -1837,5 +1902,5 @@
 }
 
-Void TDecTop::xDecodeSPS(const std::vector<UChar> *pNaluData)
+Void TDecTop::xDecodeSPS(const std::vector<UChar> &naluData)
 {
   TComSPS* sps = new TComSPS();
@@ -1847,11 +1912,11 @@
 #endif
   m_cEntropyDecoder.decodeSPS( sps );
-  m_parameterSetManager.storeSPS(sps, pNaluData);
+  m_parameterSetManager.storeSPS(sps, naluData);
 }
 
 #if CGS_3D_ASYMLUT
-Void TDecTop::xDecodePPS( const std::vector<UChar> *pNaluData, TCom3DAsymLUT * pc3DAsymLUT )
+Void TDecTop::xDecodePPS( const std::vector<UChar> &naluData, TCom3DAsymLUT * pc3DAsymLUT )
 #else
-Void TDecTop::xDecodePPS(const std::vector<UChar> *pNaluData)
+Void TDecTop::xDecodePPS(const std::vector<UChar> &naluData)
 #endif
 {
@@ -1866,57 +1931,5 @@
   m_cEntropyDecoder.decodePPS( pps );
 #endif
-  m_parameterSetManager.storePPS( pps, pNaluData);
-}
-
-Void TDecTop::xDecodeSEI( TComInputBitstream* bs, const NalUnitType nalUnitType )
-{
-  if(nalUnitType == NAL_UNIT_SUFFIX_SEI)
-  {
-#if SVC_EXTENSION
-    if (m_prevSliceSkipped) // No need to decode SEI messages of a skipped access unit
-    {
-      return;
-    }
-#endif
-#if LAYERS_NOT_PRESENT_SEI
-    m_seiReader.parseSEImessage( bs, m_pcPic->getSEIs(), nalUnitType, m_parameterSetManager.getActiveVPS(), m_parameterSetManager.getActiveSPS(), m_pDecodedSEIOutputStream  );
-#else
-    m_seiReader.parseSEImessage( bs, m_pcPic->getSEIs(), nalUnitType, m_parameterSetManager.getActiveSPS(), m_pDecodedSEIOutputStream );
-#endif
-  }
-  else
-  {
-#if LAYERS_NOT_PRESENT_SEI
-    m_seiReader.parseSEImessage( bs, m_SEIs, nalUnitType, m_parameterSetManager.getActiveVPS(), m_parameterSetManager.getActiveSPS(), m_pDecodedSEIOutputStream  );
-#else
-    m_seiReader.parseSEImessage( bs, m_SEIs, nalUnitType, m_parameterSetManager.getActiveSPS(), m_pDecodedSEIOutputStream );
-#endif
-    SEIMessages activeParamSets = getSeisByType(m_SEIs, SEI::ACTIVE_PARAMETER_SETS);
-    if (activeParamSets.size()>0)
-    {
-      SEIActiveParameterSets *seiAps = (SEIActiveParameterSets*)(*activeParamSets.begin());
-#if R0247_SEI_ACTIVE
-      assert(seiAps->activeSeqParameterSetId.size()>0);
-      if( !getLayerDec(0)->m_parameterSetManager.activateSPSWithSEI( seiAps->activeSeqParameterSetId[0] ) )
-      {
-        printf ("Warning SPS activation with Active parameter set SEI failed");
-      }
-      for (Int c=1 ; c <= seiAps->numSpsIdsMinus1; c++)
-      {
-        Int layerIdx = seiAps->layerSpsIdx[c];
-        if( !getLayerDec(layerIdx)->m_parameterSetManager.activateSPSWithSEI( seiAps->activeSeqParameterSetId[layerIdx] ) )
-        {
-          printf ("Warning SPS activation with Active parameter set SEI failed");
-        }
-      }
-#else
-      assert(seiAps->activeSeqParameterSetId.size()>0);
-      if (! m_parameterSetManager.activateSPSWithSEI(seiAps->activeSeqParameterSetId[0] ))
-      {
-        printf ("Warning SPS activation with Active parameter set SEI failed");
-      }
-#endif
-    }
-  }
+  m_parameterSetManager.storePPS( pps, naluData);
 }
 
@@ -1937,5 +1950,5 @@
   // Initialize entropy decoder
   m_cEntropyDecoder.setEntropyDecoder (&m_cCavlcDecoder);
-  m_cEntropyDecoder.setBitstream      (nalu.m_Bitstream);
+  m_cEntropyDecoder.setBitstream      (&(nalu.getBitstream()));
 
 #if SVC_EXTENSION
@@ -1952,7 +1965,7 @@
       assert( nalu.m_nuhLayerId == 0 ); // Non-conforming bitstream. The value of nuh_layer_id of VPS NAL unit shall be equal to 0.
 #endif
-      xDecodeVPS(nalu.m_Bitstream->getFifo());
+      xDecodeVPS(nalu.getBitstream().getFifo());
 #if RExt__DECODER_DEBUG_BIT_STATISTICS
-      TComCodingStatistics::IncrementStatisticEP(STATS__BYTE_ALIGNMENT_BITS,nalu.m_Bitstream->readByteAlignment(),0);
+      TComCodingStatistics::IncrementStatisticEP(STATS__BYTE_ALIGNMENT_BITS, nalu.getBitstream().readByteAlignment(), 0);
 #endif
 #if SVC_EXTENSION
@@ -1962,7 +1975,7 @@
 
     case NAL_UNIT_SPS:
-      xDecodeSPS(nalu.m_Bitstream->getFifo());
+      xDecodeSPS(nalu.getBitstream().getFifo());
 #if RExt__DECODER_DEBUG_BIT_STATISTICS
-      TComCodingStatistics::IncrementStatisticEP(STATS__BYTE_ALIGNMENT_BITS,nalu.m_Bitstream->readByteAlignment(),0);
+      TComCodingStatistics::IncrementStatisticEP(STATS__BYTE_ALIGNMENT_BITS, nalu.getBitstream().readByteAlignment(), 0);
 #endif
       return false;
@@ -1970,14 +1983,18 @@
     case NAL_UNIT_PPS:
 #if CGS_3D_ASYMLUT
-      xDecodePPS( nalu.m_Bitstream->getFifo(), &m_c3DAsymLUTPPS );
+      xDecodePPS( nalu.getBitstream().getFifo(), &m_c3DAsymLUTPPS );
 #else
-      xDecodePPS(nalu.m_Bitstream->getFifo());
+      xDecodePPS(nalu.getBitstream().getFifo());
 #endif
 #if RExt__DECODER_DEBUG_BIT_STATISTICS
-      TComCodingStatistics::IncrementStatisticEP(STATS__BYTE_ALIGNMENT_BITS,nalu.m_Bitstream->readByteAlignment(),0);
+      TComCodingStatistics::IncrementStatisticEP(STATS__BYTE_ALIGNMENT_BITS, nalu.getBitstream().readByteAlignment(),0);
 #endif
       return false;
 
     case NAL_UNIT_PREFIX_SEI:
+      // Buffer up prefix SEI messages until SPS of associated VCL is known.
+      m_prefixSEINALUs.push_back(new InputNALUnit(nalu));
+      return false;
+
     case NAL_UNIT_SUFFIX_SEI:
 #if SVC_EXTENSION
@@ -1987,5 +2004,16 @@
       }
 #endif
-      xDecodeSEI( nalu.m_Bitstream, nalu.m_nalUnitType );
+      if (m_pcPic)
+      {
+#if SVC_EXTENSION
+        m_seiReader.parseSEImessage( &(nalu.getBitstream()), m_pcPic->getSEIs(), nalu.m_nalUnitType, m_parameterSetManager.getActiveVPS(), m_parameterSetManager.getActiveSPS(), m_pDecodedSEIOutputStream );
+#else
+        m_seiReader.parseSEImessage( &(nalu.getBitstream()), m_pcPic->getSEIs(), nalu.m_nalUnitType, m_parameterSetManager.getActiveSPS(), m_pDecodedSEIOutputStream );
+#endif
+      }
+      else
+      {
+        printf ("Note: received suffix SEI but no picture currently active.\n");
+      }
       return false;
 
@@ -2079,4 +2107,7 @@
     case NAL_UNIT_RESERVED_VCL30:
     case NAL_UNIT_RESERVED_VCL31:
+      printf ("Note: found reserved VCL NAL unit.\n");
+      xParsePrefixSEIsForUnknownVCLNal();
+      return false;
 
     case NAL_UNIT_RESERVED_NVCL41:
Index: branches/SHM-dev/source/Lib/TLibDecoder/TDecTop.h
===================================================================
--- branches/SHM-dev/source/Lib/TLibDecoder/TDecTop.h	(revision 1316)
+++ branches/SHM-dev/source/Lib/TLibDecoder/TDecTop.h	(revision 1319)
@@ -56,5 +56,5 @@
 #include "SEIread.h"
 
-struct InputNALUnit;
+class InputNALUnit;
 
 //! \ingroup TLibDecoder
@@ -79,5 +79,5 @@
   TComSlice*              m_apcSlicePilot;
 
-  SEIMessages             m_SEIs; ///< List of SEI messages that have been received before the first slice and between slices
+  SEIMessages             m_SEIs; ///< List of SEI messages that have been received before the first slice and between slices, excluding prefix SEIs...
 
   // functional classes
@@ -123,4 +123,6 @@
 
   Bool                    m_warningMessageSkipPicture;
+
+  std::list<InputNALUnit*> m_prefixSEINALUs; /// Buffered up prefix SEI NAL Units.
 
 #if SVC_EXTENSION
@@ -277,13 +279,15 @@
   Bool      xDecodeSlice(InputNALUnit &nalu, Int &iSkipFrame, Int iPOCLastDisplay);
 #endif
-  Void      xDecodeVPS(const std::vector<UChar> *pNaluData);
-  Void      xDecodeSPS(const std::vector<UChar> *pNaluData);
+  Void      xDecodeVPS(const std::vector<UChar> &naluData);
+  Void      xDecodeSPS(const std::vector<UChar> &naluData);  
 #if CGS_3D_ASYMLUT
-  Void      xDecodePPS(const std::vector<UChar> *pNaluData, TCom3DAsymLUT *pc3DAsymLUT);
+  Void      xDecodePPS(const std::vector<UChar> &naluData, TCom3DAsymLUT *pc3DAsymLUT);
 #else
-  Void      xDecodePPS(const std::vector<UChar> *pNaluData);
+  Void      xDecodePPS(const std::vector<UChar> &naluData);
 #endif
   Void      xDecodeSEI( TComInputBitstream* bs, const NalUnitType nalUnitType );
   Void      xUpdatePreviousTid0POC( TComSlice *pSlice ) { if ((pSlice->getTLayer()==0) && (pSlice->isReferenceNalu() && (pSlice->getNalUnitType()!=NAL_UNIT_CODED_SLICE_RASL_R)&& (pSlice->getNalUnitType()!=NAL_UNIT_CODED_SLICE_RADL_R))) { m_prevTid0POC=pSlice->getPOC(); } }
+  Void      xParsePrefixSEImessages();
+  Void      xParsePrefixSEIsForUnknownVCLNal();
 
 
