Index: /branches/SHM-dev/source/App/TAppDecoder/TAppDecCfg.cpp
===================================================================
--- /branches/SHM-dev/source/App/TAppDecoder/TAppDecCfg.cpp	(revision 1322)
+++ /branches/SHM-dev/source/App/TAppDecoder/TAppDecCfg.cpp	(revision 1323)
@@ -124,4 +124,5 @@
 #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")
 #if Q0074_COLOUR_REMAPPING_SEI
   ("SEIColourRemappingInfo,-cri", m_colourRemapSEIEnabled, false, "Control handling of Colour Remapping Information SEI messages\n"
Index: /branches/SHM-dev/source/App/TAppDecoder/TAppDecCfg.h
===================================================================
--- /branches/SHM-dev/source/App/TAppDecoder/TAppDecCfg.h	(revision 1322)
+++ /branches/SHM-dev/source/App/TAppDecoder/TAppDecCfg.h	(revision 1323)
@@ -80,4 +80,5 @@
 #endif
   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 SVC_EXTENSION
Index: /branches/SHM-dev/source/App/TAppDecoder/TAppDecTop.cpp
===================================================================
--- /branches/SHM-dev/source/App/TAppDecoder/TAppDecTop.cpp	(revision 1322)
+++ /branches/SHM-dev/source/App/TAppDecoder/TAppDecTop.cpp	(revision 1323)
@@ -915,5 +915,6 @@
             conf.getWindowRightOffset() * xScal + defDisp.getWindowRightOffset(),
             conf.getWindowTopOffset()   * yScal + defDisp.getWindowTopOffset(),
-            conf.getWindowBottomOffset()* yScal + defDisp.getWindowBottomOffset() );
+            conf.getWindowBottomOffset()* yScal + defDisp.getWindowBottomOffset(),
+            NUM_CHROMA_FORMAT, m_bClipOutputVideoToRec709Range  );
         }
 
@@ -931,5 +932,6 @@
                                          conf.getWindowRightOffset() + defDisp.getWindowRightOffset(),
                                          conf.getWindowTopOffset() + defDisp.getWindowTopOffset(),
-                                         conf.getWindowBottomOffset() + defDisp.getWindowBottomOffset() );
+                                         conf.getWindowBottomOffset() + defDisp.getWindowBottomOffset(),
+                                         NUM_CHROMA_FORMAT, m_bClipOutputVideoToRec709Range  );
         }
 
@@ -1138,9 +1140,11 @@
           Int xScal =  TComSPS::getWinUnitX( chromaFormatIdc ), yScal = TComSPS::getWinUnitY( chromaFormatIdc );
 
-          m_acTVideoIOYuvReconFile[layerId].write( pcPic->getPicYuvRec(), m_outputColourSpaceConvert,
-            conf.getWindowLeftOffset()  *xScal + defDisp.getWindowLeftOffset(),
-            conf.getWindowRightOffset() *xScal + defDisp.getWindowRightOffset(),
-            conf.getWindowTopOffset()   *yScal + defDisp.getWindowTopOffset(),
-            conf.getWindowBottomOffset()*yScal + defDisp.getWindowBottomOffset() );
+          m_acTVideoIOYuvReconFile[layerId].write( pcPic->getPicYuvRec(),
+                                                   m_outputColourSpaceConvert,
+                                                   conf.getWindowLeftOffset()  *xScal + defDisp.getWindowLeftOffset(),
+                                                   conf.getWindowRightOffset() *xScal + defDisp.getWindowRightOffset(),
+                                                   conf.getWindowTopOffset()   *yScal + defDisp.getWindowTopOffset(),
+                                                   conf.getWindowBottomOffset()*yScal + defDisp.getWindowBottomOffset(),            
+                                                   NUM_CHROMA_FORMAT, m_bClipOutputVideoToRec709Range );
         }
 
@@ -1158,5 +1162,6 @@
                                          conf.getWindowRightOffset() + defDisp.getWindowRightOffset(),
                                          conf.getWindowTopOffset() + defDisp.getWindowTopOffset(),
-                                         conf.getWindowBottomOffset() + defDisp.getWindowBottomOffset() );
+                                         conf.getWindowBottomOffset() + defDisp.getWindowBottomOffset(),
+                                         NUM_CHROMA_FORMAT, m_bClipOutputVideoToRec709Range );
         }
 
Index: /branches/SHM-dev/source/App/TAppEncoder/TAppEncCfg.cpp
===================================================================
--- /branches/SHM-dev/source/App/TAppEncoder/TAppEncCfg.cpp	(revision 1322)
+++ /branches/SHM-dev/source/App/TAppEncoder/TAppEncCfg.cpp	(revision 1323)
@@ -1197,4 +1197,6 @@
   ("FrameSkip,-fs",                                   m_FrameSkip,                                         0u, "Number of frames to skip at start of input YUV")
   ("FramesToBeEncoded,f",                             m_framesToBeEncoded,                                  0, "Number of frames to be encoded (default=all)")
+  ("ClipInputVideoToRec709Range",                     m_bClipInputVideoToRec709Range,                   false, "If true then clip input video to the Rec. 709 Range on loading when InternalBitDepth is less than MSBExtendedBitDepth")
+  ("ClipOutputVideoToRec709Range",                    m_bClipOutputVideoToRec709Range,                  false, "If true then clip output video to the Rec. 709 Range on saving when OutputBitDepth is less than InternalBitDepth")
 
   //Field coding parameters
Index: /branches/SHM-dev/source/App/TAppEncoder/TAppEncCfg.h
===================================================================
--- /branches/SHM-dev/source/App/TAppEncoder/TAppEncCfg.h	(revision 1322)
+++ /branches/SHM-dev/source/App/TAppEncoder/TAppEncCfg.h	(revision 1323)
@@ -123,4 +123,6 @@
   Bool      m_printSequenceMSE;
   Bool      m_cabacZeroWordPaddingEnabled;
+  Bool      m_bClipInputVideoToRec709Range;
+  Bool      m_bClipOutputVideoToRec709Range;
 
 #if SVC_EXTENSION
Index: /branches/SHM-dev/source/App/TAppEncoder/TAppEncTop.cpp
===================================================================
--- /branches/SHM-dev/source/App/TAppEncoder/TAppEncTop.cpp	(revision 1322)
+++ /branches/SHM-dev/source/App/TAppEncoder/TAppEncTop.cpp	(revision 1323)
@@ -1714,5 +1714,5 @@
 
         // read input YUV file
-        m_acTVideoIOYuvInputFile[layer].read( pcPicYuvOrg[layer], &acPicYuvTrueOrg[layer], ipCSC, m_acLayerCfg[layer].getPad(), m_acLayerCfg[layer].getInputChromaFormat() );
+        m_acTVideoIOYuvInputFile[layer].read( pcPicYuvOrg[layer], &acPicYuvTrueOrg[layer], ipCSC, m_acLayerCfg[layer].getPad(), m_acLayerCfg[layer].getInputChromaFormat(), m_bClipInputVideoToRec709Range );
 
 #if AUXILIARY_PICTURES
@@ -2051,5 +2051,5 @@
 
     // read input YUV file
-    m_cTVideoIOYuvInputFile.read( pcPicYuvOrg, &cPicYuvTrueOrg, ipCSC, m_aiPad, m_InputChromaFormatIDC );
+    m_cTVideoIOYuvInputFile.read( pcPicYuvOrg, &cPicYuvTrueOrg, ipCSC, m_aiPad, m_InputChromaFormatIDC, m_bClipInputVideoToRec709Range );
 
     // increase number of received frames
@@ -2200,11 +2200,7 @@
       if( !m_acLayerCfg[layer].getReconFile().empty() && pcPicYuvRec->isReconstructed() )
       {
-#if SVC_EXTENSION
         m_acTVideoIOYuvReconFile[layer].write( pcPicYuvRec, ipCSC, m_acLayerCfg[layer].getConfWinLeft() * xScal, m_acLayerCfg[layer].getConfWinRight() * xScal,
-          m_acLayerCfg[layer].getConfWinTop() * yScal, m_acLayerCfg[layer].getConfWinBottom() * yScal );
-#else
-        m_acTVideoIOYuvReconFile[layer].write( pcPicYuvRec, ipCSC, m_acLayerCfg[layer].getConfWinLeft(), m_acLayerCfg[layer].getConfWinRight(),
-          m_acLayerCfg[layer].getConfWinTop(), m_acLayerCfg[layer].getConfWinBottom() );
-#endif
+          m_acLayerCfg[layer].getConfWinTop() * yScal, m_acLayerCfg[layer].getConfWinBottom() * yScal,
+          NUM_CHROMA_FORMAT, m_bClipOutputVideoToRec709Range  );
       }
     }
@@ -2340,5 +2336,6 @@
       if (m_pchReconFile)
       {
-        m_cTVideoIOYuvReconFile.write( pcPicYuvRec, ipCSC, m_confWinLeft, m_confWinRight, m_confWinTop, m_confWinBottom );
+        m_cTVideoIOYuvReconFile.write( pcPicYuvRec, ipCSC, m_confWinLeft, m_confWinRight, m_confWinTop, m_confWinBottom,
+            NUM_CHROMA_FORMAT, m_bClipOutputVideoToRec709Range  );
       }
 
Index: /branches/SHM-dev/source/Lib/TLibCommon/CommonDef.h
===================================================================
--- /branches/SHM-dev/source/Lib/TLibCommon/CommonDef.h	(revision 1322)
+++ /branches/SHM-dev/source/Lib/TLibCommon/CommonDef.h	(revision 1323)
@@ -213,6 +213,4 @@
 #define ADAPT_SR_SCALE              1           ///< division factor for adaptive search range
 
-#define CLIP_TO_709_RANGE           0
-
 // Early-skip threshold (encoder)
 #define EARLY_SKIP_THRES            1.50        ///< if RD < thres*avg[BestSkipRD]
Index: /branches/SHM-dev/source/Lib/TLibVideoIO/TVideoIOYuv.cpp
===================================================================
--- /branches/SHM-dev/source/Lib/TLibVideoIO/TVideoIOYuv.cpp	(revision 1322)
+++ /branches/SHM-dev/source/Lib/TLibVideoIO/TVideoIOYuv.cpp	(revision 1323)
@@ -678,5 +678,5 @@
  * @return true for success, false in case of error
  */
-Bool TVideoIOYuv::read ( TComPicYuv*  pPicYuvUser, TComPicYuv* pPicYuvTrueOrg, const InputColourSpaceConversion ipcsc, Int aiPad[2], ChromaFormat format )
+Bool TVideoIOYuv::read ( TComPicYuv*  pPicYuvUser, TComPicYuv* pPicYuvTrueOrg, const InputColourSpaceConversion ipcsc, Int aiPad[2], ChromaFormat format, const Bool bClipToRec709 )
 {
   // check end-of-file
@@ -720,12 +720,7 @@
     const Int desired_bitdepth = m_MSBExtendedBitDepth[chType] + m_bitdepthShift[chType];
 
-#if !CLIP_TO_709_RANGE
-    const Pel minval = 0;
-    const Pel maxval = (1 << desired_bitdepth) - 1;
-#else
-    const Bool b709Compliance=(m_bitdepthShift[chType] < 0 && desired_bitdepth >= 8);     /* ITU-R BT.709 compliant clipping for converting say 10b to 8b */
+    const Bool b709Compliance=(bClipToRec709) && (m_bitdepthShift[chType] < 0 && desired_bitdepth >= 8);     /* ITU-R BT.709 compliant clipping for converting say 10b to 8b */
     const Pel minval = b709Compliance? ((   1 << (desired_bitdepth - 8))   ) : 0;
     const Pel maxval = b709Compliance? ((0xff << (desired_bitdepth - 8)) -1) : (1 << desired_bitdepth) - 1;
-#endif
 
     if (! readPlane(pPicYuv->getAddr(compID), m_cHandle, is16bit, stride444, width444, height444, pad_h444, pad_v444, compID, pPicYuv->getChromaFormat(), format, m_fileBitdepth[chType]))
@@ -760,5 +755,5 @@
  * @return true for success, false in case of error
  */
-Bool TVideoIOYuv::write( TComPicYuv* pPicYuvUser, const InputColourSpaceConversion ipCSC, Int confLeft, Int confRight, Int confTop, Int confBottom, ChromaFormat format )
+Bool TVideoIOYuv::write( TComPicYuv* pPicYuvUser, const InputColourSpaceConversion ipCSC, Int confLeft, Int confRight, Int confTop, Int confBottom, ChromaFormat format, const Bool bClipToRec709 )
 {
   TComPicYuv cPicYuvCSCd;
@@ -811,12 +806,7 @@
       const ComponentID compID=ComponentID(comp);
       const ChannelType ch=toChannelType(compID);
-#if !CLIP_TO_709_RANGE
-      const Pel minval = 0;
-      const Pel maxval = (1 << m_MSBExtendedBitDepth[ch]) - 1;
-#else
-      const Bool b709Compliance=(-m_bitdepthShift[ch] < 0 && m_MSBExtendedBitDepth[ch] >= 8);     /* ITU-R BT.709 compliant clipping for converting say 10b to 8b */
+      const Bool b709Compliance = bClipToRec709 && (-m_bitdepthShift[ch] < 0 && m_MSBExtendedBitDepth[ch] >= 8);     /* ITU-R BT.709 compliant clipping for converting say 10b to 8b */
       const Pel minval = b709Compliance? ((   1 << (m_MSBExtendedBitDepth[ch] - 8))   ) : 0;
       const Pel maxval = b709Compliance? ((0xff << (m_MSBExtendedBitDepth[ch] - 8)) -1) : (1 << m_MSBExtendedBitDepth[ch]) - 1;
-#endif
 
       scalePlane(dstPicYuv->getAddr(compID), dstPicYuv->getStride(compID), dstPicYuv->getWidth(compID), dstPicYuv->getHeight(compID), -m_bitdepthShift[ch], minval, maxval);
@@ -852,5 +842,5 @@
 }
 
-Bool TVideoIOYuv::write( TComPicYuv* pPicYuvUserTop, TComPicYuv* pPicYuvUserBottom, const InputColourSpaceConversion ipCSC, Int confLeft, Int confRight, Int confTop, Int confBottom, ChromaFormat format, const Bool isTff )
+Bool TVideoIOYuv::write( TComPicYuv* pPicYuvUserTop, TComPicYuv* pPicYuvUserBottom, const InputColourSpaceConversion ipCSC, Int confLeft, Int confRight, Int confTop, Int confBottom, ChromaFormat format, const Bool isTff, const Bool bClipToRec709 )
 {
 
@@ -906,12 +896,7 @@
         const ComponentID compID=ComponentID(comp);
         const ChannelType ch=toChannelType(compID);
-#if !CLIP_TO_709_RANGE
-        const Pel minval = 0;
-        const Pel maxval = (1 << m_MSBExtendedBitDepth[ch]) - 1;
-#else
-        const Bool b709Compliance=(-m_bitdepthShift[ch] < 0 && m_MSBExtendedBitDepth[ch] >= 8);     /* ITU-R BT.709 compliant clipping for converting say 10b to 8b */
+        const Bool b709Compliance=bClipToRec709 && (-m_bitdepthShift[ch] < 0 && m_MSBExtendedBitDepth[ch] >= 8);     /* ITU-R BT.709 compliant clipping for converting say 10b to 8b */
         const Pel minval = b709Compliance? ((   1 << (m_MSBExtendedBitDepth[ch] - 8))   ) : 0;
         const Pel maxval = b709Compliance? ((0xff << (m_MSBExtendedBitDepth[ch] - 8)) -1) : (1 << m_MSBExtendedBitDepth[ch]) - 1;
-#endif
 
         scalePlane(dstPicYuv->getAddr(compID), dstPicYuv->getStride(compID), dstPicYuv->getWidth(compID), dstPicYuv->getHeight(compID), -m_bitdepthShift[ch], minval, maxval);
Index: /branches/SHM-dev/source/Lib/TLibVideoIO/TVideoIOYuv.h
===================================================================
--- /branches/SHM-dev/source/Lib/TLibVideoIO/TVideoIOYuv.h	(revision 1322)
+++ /branches/SHM-dev/source/Lib/TLibVideoIO/TVideoIOYuv.h	(revision 1323)
@@ -72,7 +72,12 @@
 
 
-  Bool  read  ( TComPicYuv* pPicYuv, TComPicYuv* pPicYuvTrueOrg, const InputColourSpaceConversion ipcsc, Int aiPad[2], ChromaFormat fileFormat=NUM_CHROMA_FORMAT );     ///< read one frame with padding parameter
-  Bool  write ( TComPicYuv* pPicYuv, const InputColourSpaceConversion ipCSC, Int confLeft=0, Int confRight=0, Int confTop=0, Int confBottom=0, ChromaFormat fileFormat=NUM_CHROMA_FORMAT );     ///< write one YUV frame with padding parameter
-  Bool  write ( TComPicYuv* pPicYuvTop, TComPicYuv* pPicYuvBottom, const InputColourSpaceConversion ipCSC, Int confLeft=0, Int confRight=0, Int confTop=0, Int confBottom=0, ChromaFormat fileFormat=NUM_CHROMA_FORMAT, Bool isTff=false);
+  // If fileFormat=NUM_CHROMA_FORMAT, use the format defined by pPicYuvTrueOrg
+  Bool  read  ( TComPicYuv* pPicYuv, TComPicYuv* pPicYuvTrueOrg, const InputColourSpaceConversion ipcsc, Int aiPad[2], ChromaFormat fileFormat=NUM_CHROMA_FORMAT, const Bool bClipToRec709=false );     ///< read one frame with padding parameter
+
+  // If fileFormat=NUM_CHROMA_FORMAT, use the format defined by pPicYuv
+  Bool  write ( TComPicYuv* pPicYuv, const InputColourSpaceConversion ipCSC, Int confLeft=0, Int confRight=0, Int confTop=0, Int confBottom=0, ChromaFormat fileFormat=NUM_CHROMA_FORMAT, const Bool bClipToRec709=false );     ///< write one YUV frame with padding parameter
+
+  // If fileFormat=NUM_CHROMA_FORMAT, use the format defined by pPicYuvTop and pPicYuvBottom
+  Bool  write ( TComPicYuv* pPicYuvTop, TComPicYuv* pPicYuvBottom, const InputColourSpaceConversion ipCSC, Int confLeft=0, Int confRight=0, Int confTop=0, Int confBottom=0, ChromaFormat fileFormat=NUM_CHROMA_FORMAT, const Bool isTff=false, const Bool bClipToRec709=false);
   static Void ColourSpaceConvert(const TComPicYuv &src, TComPicYuv &dest, const InputColourSpaceConversion conversion, Bool bIsForwards);
 
