Index: branches/SHM-dev/source/App/TAppDecoder/TAppDecTop.cpp
===================================================================
--- branches/SHM-dev/source/App/TAppDecoder/TAppDecTop.cpp	(revision 1467)
+++ branches/SHM-dev/source/App/TAppDecoder/TAppDecTop.cpp	(revision 1470)
@@ -64,5 +64,4 @@
 #if Q0074_COLOUR_REMAPPING_SEI
 : m_pcSeiColourRemappingInfoPrevious(NULL)
-, m_pcPicYuvColourRemapped(NULL)
 #endif
 {
@@ -80,5 +79,4 @@
 #if Q0074_COLOUR_REMAPPING_SEI
  ,m_pcSeiColourRemappingInfoPrevious(NULL)
- ,m_pcPicYuvColourRemapped(NULL)
 #endif
 {
@@ -137,5 +135,5 @@
 Void TAppDecTop::decode()
 {
-  Int                poc;
+  Int                 poc;
   TComList<TComPic*>* pcListPic = NULL;
 
@@ -161,5 +159,18 @@
   // create & initialize internal classes
   xCreateDecLib();
-  xInitDecLib  ();  
+  xInitDecLib  ();
+
+#if Q0074_COLOUR_REMAPPING_SEI
+  // clear contents of colour-remap-information-SEI output file
+  if (!m_colourRemapSEIFileName.empty())
+  {
+    std::ofstream ofile(m_colourRemapSEIFileName.c_str());
+    if (!ofile.good() || !ofile.is_open())
+    {
+      fprintf(stderr, "\nUnable to open file '%s' for writing colour-remap-information-SEI video\n", m_colourRemapSEIFileName.c_str());
+      exit(EXIT_FAILURE);
+    }
+  }
+#endif
 
   // main decoder loop
@@ -446,4 +457,17 @@
   m_iPOCLastDisplay += m_iSkipFrame;      // set the last displayed POC correctly for skip forward.
 
+#if Q0074_COLOUR_REMAPPING_SEI
+  // clear contents of colour-remap-information-SEI output file
+  if (!m_colourRemapSEIFileName.empty())
+  {
+    std::ofstream ofile(m_colourRemapSEIFileName.c_str());
+    if (!ofile.good() || !ofile.is_open())
+    {
+      fprintf(stderr, "\nUnable to open file '%s' for writing colour-remap-information-SEI video\n", m_colourRemapSEIFileName.c_str());
+      exit(EXIT_FAILURE);
+    }
+  }
+#endif
+
   // main decoder loop
   Bool openedReconFile = false; // reconstruction file not yet opened. (must be performed after SPS is seen)
@@ -645,4 +669,11 @@
   m_cTDecTop.destroy();
 #endif
+#if Q0074_COLOUR_REMAPPING_SEI
+  if (m_pcSeiColourRemappingInfoPrevious != NULL)
+  {
+    delete m_pcSeiColourRemappingInfoPrevious;
+    m_pcSeiColourRemappingInfoPrevious = NULL;
+  }
+#endif
 }
 
@@ -688,10 +719,4 @@
     delete m_pcSeiColourRemappingInfoPrevious;
     m_pcSeiColourRemappingInfoPrevious = NULL;
-  }
-  if (m_pcPicYuvColourRemapped != NULL)
-  {
-    m_pcPicYuvColourRemapped->destroy();
-    delete m_pcPicYuvColourRemapped;
-    m_pcPicYuvColourRemapped  = NULL;
   }
 #endif
@@ -929,5 +954,5 @@
         if (!m_colourRemapSEIFileName.empty())
         {
-          xOutputColourRemapPic(pcPic, activeSPS);
+          xOutputColourRemapPic(pcPic);
         }
 #endif
@@ -1101,5 +1126,5 @@
         if (!m_colourRemapSEIFileName.empty())
         {
-          xOutputColourRemapPic(pcPic, &(pcPic->getPicSym()->getSPS()));
+          xOutputColourRemapPic(pcPic);
         }
 #endif
@@ -1164,6 +1189,7 @@
 #if Q0074_COLOUR_REMAPPING_SEI
 
-Void TAppDecTop::xOutputColourRemapPic(TComPic* pcPic, const TComSPS* activeSPS)
-{
+Void TAppDecTop::xOutputColourRemapPic(TComPic* pcPic)
+{
+  const TComSPS &sps=pcPic->getPicSym()->getSPS();
   SEIMessages colourRemappingInfo = getSeisByType(pcPic->getSEIs(), SEI::COLOUR_REMAPPING_INFO );
   SEIColourRemappingInfo *seiColourRemappingInfo = ( colourRemappingInfo.size() > 0 ) ? (SEIColourRemappingInfo*) *(colourRemappingInfo.begin()) : NULL;
@@ -1175,8 +1201,16 @@
   if (seiColourRemappingInfo)
   {
-    applyColourRemapping(*pcPic->getPicYuvRec(), *seiColourRemappingInfo, *activeSPS);
+    applyColourRemapping(*pcPic->getPicYuvRec(), *seiColourRemappingInfo, sps);
+
+    // save the last CRI SEI received
+    if (m_pcSeiColourRemappingInfoPrevious == NULL)
+    {
+      m_pcSeiColourRemappingInfoPrevious = new SEIColourRemappingInfo();
+    }
+    m_pcSeiColourRemappingInfoPrevious->copyFrom(*seiColourRemappingInfo);
   }
   else  // using the last CRI SEI received
   {
+    // TODO: prevent persistence of CRI SEI across C(L)VS.
     if (m_pcSeiColourRemappingInfoPrevious != NULL)
     {
@@ -1185,20 +1219,6 @@
         printf("Warning No SEI-CRI message is present for the current picture, persistence of the CRI is not managed\n");
       }
-      SEIColourRemappingInfo *seiColourRemappingInfoCopy;
-      seiColourRemappingInfoCopy = m_pcSeiColourRemappingInfoPrevious;
-      applyColourRemapping(*pcPic->getPicYuvRec(), *seiColourRemappingInfoCopy, *activeSPS);
-    }
-  }
-
-  // save the last CRI SEI received
-  if( seiColourRemappingInfo != NULL)
-  {
-    if (m_pcSeiColourRemappingInfoPrevious != NULL)
-    {
-      delete m_pcSeiColourRemappingInfoPrevious;
-      m_pcSeiColourRemappingInfoPrevious = NULL;
-    }
-    m_pcSeiColourRemappingInfoPrevious = new SEIColourRemappingInfo();
-    m_pcSeiColourRemappingInfoPrevious->copyFrom(*seiColourRemappingInfo);
+      applyColourRemapping(*pcPic->getPicYuvRec(), *m_pcSeiColourRemappingInfoPrevious, sps);
+    }
   }
 }
@@ -1373,32 +1393,22 @@
 {  
   const Int maxBitDepth = 16;
-  Bool firstPicture = true;
 
   // create colour remapped picture
-  if ( m_pcPicYuvColourRemapped == NULL )
-  {
-    const ChromaFormat chromaFormatIDC = pic.getChromaFormat(); 
+  if( !criSEI.m_colourRemapCancelFlag && pic.getChromaFormat()!=CHROMA_400) // 4:0:0 not supported.
+  {
+    const Int          iHeight         = pic.getHeight(COMPONENT_Y);
     const Int          iWidth          = pic.getWidth(COMPONENT_Y);
-    const Int          iHeight         = pic.getHeight(COMPONENT_Y);
-    const UInt         uiMaxCuWidth    = activeSPS.getMaxCUWidth();
-    const UInt         uiMaxCuHeight   = activeSPS.getMaxCUHeight();
-    const UInt         uiMaxDepth      = activeSPS.getMaxTotalCUDepth();
-    m_pcPicYuvColourRemapped  = new TComPicYuv;
-    m_pcPicYuvColourRemapped->create( iWidth, iHeight, chromaFormatIDC, uiMaxCuWidth, uiMaxCuHeight, uiMaxDepth, true );
-  }
-  else
-  {
-    firstPicture = false;
-  }
-
-  if( !criSEI.m_colourRemapCancelFlag )
-  {
-    const Int  iHeight  = pic.getHeight(COMPONENT_Y);
-    const Int  iWidth   = pic.getWidth(COMPONENT_Y);
-    const Int  iStride  = pic.getStride(COMPONENT_Y);
-    const Int  iCStride = pic.getStride(COMPONENT_Cb);
-    const Bool b444     = ( pic.getChromaFormat() == CHROMA_444 );
-    const Bool b422     = ( pic.getChromaFormat() == CHROMA_422 );
-    const Bool b420     = ( pic.getChromaFormat() == CHROMA_420 );
+    const ChromaFormat chromaFormatIDC = pic.getChromaFormat();
+
+    TComPicYuv picYuvColourRemapped;
+    picYuvColourRemapped.createWithoutCUInfo( iWidth, iHeight, chromaFormatIDC );
+
+    const Int  iStrideIn   = pic.getStride(COMPONENT_Y);
+    const Int  iCStrideIn  = pic.getStride(COMPONENT_Cb);
+    const Int  iStrideOut  = picYuvColourRemapped.getStride(COMPONENT_Y);
+    const Int  iCStrideOut = picYuvColourRemapped.getStride(COMPONENT_Cb);
+    const Bool b444        = ( pic.getChromaFormat() == CHROMA_444 );
+    const Bool b422        = ( pic.getChromaFormat() == CHROMA_422 );
+    const Bool b420        = ( pic.getChromaFormat() == CHROMA_420 );
 
     std::vector<Int> preLut[3];
@@ -1411,11 +1421,11 @@
     YUVIn[COMPONENT_Cb] = pic.getAddr(COMPONENT_Cb);
     YUVIn[COMPONENT_Cr] = pic.getAddr(COMPONENT_Cr);
-    YUVOut[COMPONENT_Y]  = m_pcPicYuvColourRemapped->getAddr(COMPONENT_Y);
-    YUVOut[COMPONENT_Cb] = m_pcPicYuvColourRemapped->getAddr(COMPONENT_Cb);
-    YUVOut[COMPONENT_Cr] = m_pcPicYuvColourRemapped->getAddr(COMPONENT_Cr);
-
-    Int bitDepth = criSEI.m_colourRemapBitDepth;
-    BitDepths bitDepthsCriFile;
-    bitDepthsCriFile.recon[CHANNEL_TYPE_LUMA] = bitDepth;
+    YUVOut[COMPONENT_Y]  = picYuvColourRemapped.getAddr(COMPONENT_Y);
+    YUVOut[COMPONENT_Cb] = picYuvColourRemapped.getAddr(COMPONENT_Cb);
+    YUVOut[COMPONENT_Cr] = picYuvColourRemapped.getAddr(COMPONENT_Cr);
+
+    const Int bitDepth = criSEI.m_colourRemapBitDepth;
+    BitDepths        bitDepthsCriFile;
+    bitDepthsCriFile.recon[CHANNEL_TYPE_LUMA]   = bitDepth;
     bitDepthsCriFile.recon[CHANNEL_TYPE_CHROMA] = bitDepth; // Different bitdepth is not implemented
 
@@ -1483,16 +1493,17 @@
       }
 
-      YUVIn[COMPONENT_Y]  += iStride;
-      YUVOut[COMPONENT_Y] += iStride;
+      YUVIn[COMPONENT_Y]  += iStrideIn;
+      YUVOut[COMPONENT_Y] += iStrideOut;
       if( !(b420 && !(y&1)) )
       {
-         YUVIn[COMPONENT_Cb]  += iCStride;
-         YUVIn[COMPONENT_Cr]  += iCStride;
-         YUVOut[COMPONENT_Cb] += iCStride;
-         YUVOut[COMPONENT_Cr] += iCStride;
+         YUVIn[COMPONENT_Cb]  += iCStrideIn;
+         YUVIn[COMPONENT_Cr]  += iCStrideIn;
+         YUVOut[COMPONENT_Cb] += iCStrideOut;
+         YUVOut[COMPONENT_Cr] += iCStrideOut;
       }
     }
     //Write remapped picture in display order
-    m_pcPicYuvColourRemapped->dump( m_colourRemapSEIFileName, bitDepthsCriFile, !firstPicture );
+    picYuvColourRemapped.dump( m_colourRemapSEIFileName, bitDepthsCriFile, true );
+    picYuvColourRemapped.destroy();
   }
 }
@@ -1521,7 +1532,7 @@
 
 #if Q0074_COLOUR_REMAPPING_SEI
-    if (!m_colourRemapSEIFileName.empty())
-    {
-      xOutputColourRemapPic(pic, pic->getSlice(0)->getSPS());
+    if( !m_colourRemapSEIFileName.empty() )
+    {
+      xOutputColourRemapPic(pic);
     }
 #endif
Index: branches/SHM-dev/source/App/TAppDecoder/TAppDecTop.h
===================================================================
--- branches/SHM-dev/source/App/TAppDecoder/TAppDecTop.h	(revision 1467)
+++ branches/SHM-dev/source/App/TAppDecoder/TAppDecTop.h	(revision 1470)
@@ -83,5 +83,4 @@
 #if Q0074_COLOUR_REMAPPING_SEI
   SEIColourRemappingInfo*         m_pcSeiColourRemappingInfoPrevious;
-  TComPicYuv*                     m_pcPicYuvColourRemapped;       ///< Colour Remapped picture
 #endif
 
@@ -129,5 +128,5 @@
 private:
   Void applyColourRemapping(const TComPicYuv& pic, SEIColourRemappingInfo& pCriSEI, const TComSPS &activeSPS);
-  Void xOutputColourRemapPic(TComPic* pcPic, const TComSPS* activeSPS);
+  Void xOutputColourRemapPic(TComPic* pcPic);
 #endif
 #if ALIGNED_BUMPING
