Index: branches/SHM-5.1-dev/source/Lib/TLibCommon/TComSlice.cpp
===================================================================
--- branches/SHM-5.1-dev/source/Lib/TLibCommon/TComSlice.cpp	(revision 625)
+++ branches/SHM-5.1-dev/source/Lib/TLibCommon/TComSlice.cpp	(revision 627)
@@ -125,4 +125,7 @@
   m_interLayerPredEnabledFlag = 0;
   ::memset( m_interLayerPredLayerIdc, 0, sizeof(m_interLayerPredLayerIdc) );
+#if P0312_VERT_PHASE_ADJ
+  ::memset( m_vertPhasePositionFlag, 0, sizeof(m_vertPhasePositionFlag) );
+#endif
 #endif //SVC_EXTENSION
 
@@ -1122,4 +1125,7 @@
   m_interLayerPredEnabledFlag  = pSrc->m_interLayerPredEnabledFlag;
   memcpy( m_interLayerPredLayerIdc, pSrc->m_interLayerPredLayerIdc, sizeof( m_interLayerPredLayerIdc ) );
+#if P0312_VERT_PHASE_ADJ
+  memcpy( m_vertPhasePositionFlag, pSrc->m_vertPhasePositionFlag, sizeof( m_vertPhasePositionFlag ) );
+#endif
 #endif
   m_pcSPS                = pSrc->m_pcSPS;
Index: branches/SHM-5.1-dev/source/Lib/TLibCommon/TComSlice.h
===================================================================
--- branches/SHM-5.1-dev/source/Lib/TLibCommon/TComSlice.h	(revision 625)
+++ branches/SHM-5.1-dev/source/Lib/TLibCommon/TComSlice.h	(revision 627)
@@ -627,4 +627,9 @@
   Int         m_avgPicRate          [MAX_VPS_LAYER_SETS_PLUS1][MAX_TLAYER];
 #endif
+  
+#if P0312_VERT_PHASE_ADJ
+  Bool        m_vpsVuiVertPhaseInUseFlag;
+#endif
+
 #if P0300_ALT_OUTPUT_LAYER_FLAG
   Bool       m_altOutputLayerFlag[MAX_VPS_LAYER_SETS_PLUS1];
@@ -1008,4 +1013,10 @@
   Int     getBaseLayerPSCompatibilityFlag (Int layer)   { return m_baseLayerPSCompatibilityFlag[layer];}
 #endif
+
+#if P0312_VERT_PHASE_ADJ
+  Bool getVpsVuiVertPhaseInUseFlag()       { return m_vpsVuiVertPhaseInUseFlag; }
+  Void setVpsVuiVertPhaseInUseFlag(Bool x) { m_vpsVuiVertPhaseInUseFlag = x;    }
+#endif
+
 #if P0300_ALT_OUTPUT_LAYER_FLAG
   Bool   getAltOuputLayerFlag(Int idx)         { return m_altOutputLayerFlag[idx]; }
@@ -1115,4 +1126,8 @@
   Int           m_winTopOffset;
   Int           m_winBottomOffset;
+#if P0312_VERT_PHASE_ADJ
+  Bool          m_vertPhasePositionEnableFlag;
+  Bool          m_vertPhasePositionFlag;
+#endif
 public:
   Window()
@@ -1122,8 +1137,16 @@
   , m_winTopOffset      (0)
   , m_winBottomOffset   (0)
+#if P0312_VERT_PHASE_ADJ
+  , m_vertPhasePositionEnableFlag(false)
+  , m_vertPhasePositionFlag(false)
+#endif
   { }
 
   Bool          getWindowEnabledFlag() const      { return m_enabledFlag; }
-  Void          resetWindow()                     { m_enabledFlag = false; m_winLeftOffset = m_winRightOffset = m_winTopOffset = m_winBottomOffset = 0; }
+#if P0312_VERT_PHASE_ADJ 
+  Void          resetWindow()                     { m_enabledFlag = false; m_winLeftOffset = m_winRightOffset = m_winTopOffset = m_winBottomOffset = 0; m_vertPhasePositionEnableFlag = m_vertPhasePositionFlag = false;} 
+#else
+  Void          resetWindow()                     { m_enabledFlag = false; m_winLeftOffset = m_winRightOffset = m_winTopOffset = m_winBottomOffset = 0;} 
+#endif
   Int           getWindowLeftOffset() const       { return m_enabledFlag ? m_winLeftOffset : 0; }
   Void          setWindowLeftOffset(Int val)      { m_winLeftOffset = val; m_enabledFlag = true; }
@@ -1135,5 +1158,15 @@
   Void          setWindowBottomOffset(Int val)    { m_winBottomOffset = val; m_enabledFlag = true; }
 
+#if P0312_VERT_PHASE_ADJ
+  Bool          getVertPhasePositionEnableFlag() const     { return m_vertPhasePositionEnableFlag ; }
+  Void          setVertPhasePositionEnableFlag(Bool val)    { m_vertPhasePositionEnableFlag = val;  }
+
+  Bool          getVertPhasePositionFlag() const     { return m_vertPhasePositionEnableFlag ? m_vertPhasePositionFlag: 0; }
+  Void          setVertPhasePositionFlag(Bool val)    { m_vertPhasePositionFlag = val;  }
+
+  Void          setWindow(Int offsetLeft, Int offsetLRight, Int offsetLTop, Int offsetLBottom  , Bool vertPhasePositionEnableFlag = 0, Bool phasePosFlag = 0)
+#else
   Void          setWindow(Int offsetLeft, Int offsetLRight, Int offsetLTop, Int offsetLBottom)
+#endif
   {
     m_enabledFlag       = true;
@@ -1142,4 +1175,8 @@
     m_winTopOffset      = offsetLTop;
     m_winBottomOffset   = offsetLBottom;
+#if P0312_VERT_PHASE_ADJ
+    m_vertPhasePositionEnableFlag = vertPhasePositionEnableFlag;
+    m_vertPhasePositionFlag = phasePosFlag;
+#endif
   }
 };
@@ -1393,4 +1430,7 @@
   UInt m_layerId;
   UInt        m_numScaledRefLayerOffsets;
+#if P0312_VERT_PHASE_ADJ
+ Bool         m_vertPhasePositionEnableFlag[MAX_LAYERS];
+#endif
 #if O0098_SCALED_REF_LAYER_ID
   UInt        m_scaledRefLayerId[MAX_LAYERS];
@@ -1549,4 +1589,8 @@
   UInt     getNumScaledRefLayerOffsets()  { return m_numScaledRefLayerOffsets; }
   Void     setNumScaledRefLayerOffsets(Int x)  { m_numScaledRefLayerOffsets = x; }
+#if P0312_VERT_PHASE_ADJ
+  Bool     getVertPhasePositionEnableFlag(Int x)  { return m_vertPhasePositionEnableFlag[x]; }
+  Void     setVertPhasePositionEnableFlag(Int x, Bool b)  { m_vertPhasePositionEnableFlag[x] = b; } 
+#endif
 #if O0098_SCALED_REF_LAYER_ID
   UInt     getScaledRefLayerId(Int x)          { return m_scaledRefLayerId[x]; }
@@ -1944,4 +1988,7 @@
   Int         m_activeNumILRRefIdx;        //< Active inter-layer reference pictures
   Int         m_interLayerPredLayerIdc  [MAX_VPS_LAYER_ID_PLUS1];
+#if P0312_VERT_PHASE_ADJ
+  Bool        m_vertPhasePositionFlag[MAX_VPS_LAYER_ID_PLUS1];
+#endif
 #if POC_RESET_FLAG
   Bool        m_bPocResetFlag;
@@ -2224,4 +2271,9 @@
   Bool      getInterLayerPredEnabledFlag     ()                { return m_interLayerPredEnabledFlag;}
 
+#if P0312_VERT_PHASE_ADJ
+  Int       getVertPhasePositionFlag (UInt layerIdx)              { return   m_vertPhasePositionFlag[layerIdx];}
+  Void      setVertPhasePositionFlag (Bool b, UInt layerIdx)      {  m_vertPhasePositionFlag[layerIdx] = b;  }
+#endif
+
   Void      setNumMotionPredRefLayers(int i)            { m_numMotionPredRefLayers = i; }
   Int       getNumMotionPredRefLayers()                 { return m_numMotionPredRefLayers; }
Index: branches/SHM-5.1-dev/source/Lib/TLibCommon/TComUpsampleFilter.cpp
===================================================================
--- branches/SHM-5.1-dev/source/Lib/TLibCommon/TComUpsampleFilter.cpp	(revision 625)
+++ branches/SHM-5.1-dev/source/Lib/TLibCommon/TComUpsampleFilter.cpp	(revision 627)
@@ -126,4 +126,13 @@
   Int heightEL  = pcUsPic->getHeight() - scalEL.getWindowTopOffset()  - scalEL.getWindowBottomOffset();
   Int strideEL  = pcUsPic->getStride();
+
+#if P0312_VERT_PHASE_ADJ
+  Bool vertPhasePositionEnableFlag = scalEL.getVertPhasePositionEnableFlag();
+  Bool vertPhasePositionFlag = scalEL.getVertPhasePositionFlag();
+  if (vertPhasePositionFlag)
+  {
+    assert (vertPhasePositionEnableFlag);
+  }
+#endif
 
   Pel* piTempBufY = pcTempPic->getLumaAddr();
@@ -219,8 +228,16 @@
 #if O0215_PHASE_ALIGNMENT //for Luma, if Phase 0, then both PhaseX  and PhaseY should be 0. If symmetric: both PhaseX and PhaseY should be 2
     Int   phaseX = 2*phaseAlignFlag;
+#if P0312_VERT_PHASE_ADJ
+    Int   phaseY = (vertPhasePositionEnableFlag?(vertPhasePositionFlag *4):(2*phaseAlignFlag));
+#else
     Int   phaseY = 2*phaseAlignFlag;
+#endif
 #else
     Int   phaseX = 0;
+#if P0312_VERT_PHASE_ADJ
+    Int   phaseY = (vertPhasePositionEnableFlag?(vertPhasePositionFlag *4):(0));
+#else
     Int   phaseY = 0;
+#endif
 #endif
 
@@ -363,32 +380,29 @@
 
 #if O0215_PHASE_ALIGNMENT
+    Int phaseXC = phaseAlignFlag;
+#if P0312_VERT_PHASE_ADJ
+    Int phaseYC = vertPhasePositionEnableFlag ? (vertPhasePositionFlag * 4):(phaseAlignFlag + 1);
+#else
+    Int phaseYC = phaseAlignFlag + 1;
+#endif
+#else
     Int phaseXC = 0;
+#if P0312_VERT_PHASE_ADJ
+    Int phaseYC = vertPhasePositionEnableFlag ? (vertPhasePositionFlag * 4): 1;;
+#else
     Int phaseYC = 1;
-
+#endif
+#endif
+    
 #if ROUNDING_OFFSET
-    addX       = ( ( (phaseXC+phaseAlignFlag) * scaleX + 2 ) >> 2 ) + ( 1 << ( shiftX - 5 ) );
-    addY       = ( ( (phaseYC+phaseAlignFlag) * scaleY + 2 ) >> 2 ) + ( 1 << ( shiftY - 5 ) );
-#else
-    addX       = ( ( ( widthBL * (phaseXC+phaseAlignFlag) ) << ( shiftX - 2 ) ) + ( widthEL >> 1 ) ) / widthEL + ( 1 << ( shiftX - 5 ) );
-    addY       = ( ( ( heightBL * (phaseYC+phaseAlignFlag) ) << ( shiftY - 2 ) ) + ( heightEL >> 1 ) ) / heightEL+ ( 1 << ( shiftY - 5 ) );
-#endif
-
-    deltaX     = 4 * (phaseXC+phaseAlignFlag);
-    deltaY     = 4 * (phaseYC+phaseAlignFlag);
-#else
-    phaseX = 0;
-    phaseY = 1;
-
-#if ROUNDING_OFFSET
-    addX       = ( ( phaseX * scaleX + 2 ) >> 2 ) + ( 1 << ( shiftX - 5 ) );
-    addY       = ( ( phaseY * scaleY + 2 ) >> 2 ) + ( 1 << ( shiftY - 5 ) );
-#else
-    addX       = ( ( ( widthBL * phaseX ) << ( shiftX - 2 ) ) + ( widthEL >> 1 ) ) / widthEL + ( 1 << ( shiftX - 5 ) );
-    addY       = ( ( ( heightBL * phaseY ) << ( shiftY - 2 ) ) + ( heightEL >> 1 ) ) / heightEL+ ( 1 << ( shiftY - 5 ) );
-#endif
-
-    deltaX     = 4 * phaseX;
-    deltaY     = 4 * phaseY;
-#endif
+    addX       = ( ( phaseXC * scaleX + 2 ) >> 2 ) + ( 1 << ( shiftX - 5 ) );
+    addY       = ( ( phaseYC * scaleY + 2 ) >> 2 ) + ( 1 << ( shiftY - 5 ) );
+#else
+    addX       = ( ( ( widthBL * (phaseXC) ) << ( shiftX - 2 ) ) + ( widthEL >> 1 ) ) / widthEL + ( 1 << ( shiftX - 5 ) );
+    addY       = ( ( ( heightBL * (phaseYC) ) << ( shiftY - 2 ) ) + ( heightEL >> 1 ) ) / heightEL+ ( 1 << ( shiftY - 5 ) );
+#endif
+
+    deltaX     = 4 * phaseXC;
+    deltaY     = 4 * phaseYC;
 
     shiftXM4 = shiftX - 4;
Index: branches/SHM-5.1-dev/source/Lib/TLibCommon/TypeDef.h
===================================================================
--- branches/SHM-5.1-dev/source/Lib/TLibCommon/TypeDef.h	(revision 625)
+++ branches/SHM-5.1-dev/source/Lib/TLibCommon/TypeDef.h	(revision 627)
@@ -50,4 +50,5 @@
 #define O0137_MAX_LAYERID                1      ///< JCTVC-O0137, JCTVC-O0200, JCTVC-O0223: restrict nuh_layer_id and vps_max_layers_minus1
 
+#define P0312_VERT_PHASE_ADJ             1      ///< JCTVC-P0312: vertical phase adjustment in re-sampling process (BoG report)
 #define P0130_EOB                        1      ///< JCTVC-P0130, set layer Id of EOB NALU to be fixed to 0
 #define P0307_REMOVE_VPS_VUI_OFFSET      1      ///< JCTVC-P0307, remove implementation related to VPS VUI offset signalling
Index: branches/SHM-5.1-dev/source/Lib/TLibDecoder/TDecCAVLC.cpp
===================================================================
--- branches/SHM-5.1-dev/source/Lib/TLibDecoder/TDecCAVLC.cpp	(revision 625)
+++ branches/SHM-5.1-dev/source/Lib/TLibDecoder/TDecCAVLC.cpp	(revision 627)
@@ -859,4 +859,7 @@
       READ_SVLC( iCode, "scaled_ref_layer_right_offset" );   scaledWindow.setWindowRightOffset (iCode << 1);
       READ_SVLC( iCode, "scaled_ref_layer_bottom_offset" );  scaledWindow.setWindowBottomOffset(iCode << 1);
+#if P0312_VERT_PHASE_ADJ
+      READ_FLAG( uiCode, "vert_phase_pos_enable_flag" );scaledWindow.setVertPhasePositionEnableFlag  (uiCode);  pcSPS->setVertPhasePositionEnableFlag(i,uiCode);
+#endif
     }
   }
@@ -1938,4 +1941,8 @@
     }
   }
+#endif
+
+#if P0312_VERT_PHASE_ADJ
+  READ_FLAG( uiCode, "vps_vui_vert_phase_in_use_flag" ); vps->setVpsVuiVertPhaseInUseFlag(uiCode);
 #endif
 
@@ -2486,4 +2493,13 @@
 #endif
 #endif
+#if P0312_VERT_PHASE_ADJ
+    for(Int i = 0; i < rpcSlice->getSPS()->getNumScaledRefLayerOffsets(); i++)
+    {
+      if (rpcSlice->getSPS()->getVertPhasePositionEnableFlag(rpcSlice->getInterLayerPredLayerIdc(i)))
+      {
+        READ_FLAG( uiCode, "phase_pos_flag" ); rpcSlice->setVertPhasePositionFlag( uiCode? true: false, i);
+      }
+    }
+#endif
 
     if(sps->getUseSAO())
Index: branches/SHM-5.1-dev/source/Lib/TLibDecoder/TDecTop.cpp
===================================================================
--- branches/SHM-5.1-dev/source/Lib/TLibDecoder/TDecTop.cpp	(revision 625)
+++ branches/SHM-5.1-dev/source/Lib/TLibDecoder/TDecTop.cpp	(revision 627)
@@ -1467,8 +1467,16 @@
 #endif
 
+#if P0312_VERT_PHASE_ADJ
+#if O0098_SCALED_REF_LAYER_ID
+        Window &scalEL = pcSlice->getSPS()->getScaledRefLayerWindowForLayer(pcSlice->getVPS()->getRefLayerId(m_layerId, refLayerIdc));
+#else
+        Window &scalEL = pcSlice->getSPS()->getScaledRefLayerWindow(refLayerIdc);
+#endif
+#else
 #if O0098_SCALED_REF_LAYER_ID
         const Window &scalEL = pcSlice->getSPS()->getScaledRefLayerWindowForLayer(pcSlice->getVPS()->getRefLayerId(m_layerId, refLayerIdc));
 #else
         const Window &scalEL = pcSlice->getSPS()->getScaledRefLayerWindow(refLayerIdc);
+#endif
 #endif
 
@@ -1493,4 +1501,7 @@
           Window &scalEL = pcSlice->getSPS()->getScaledRefLayerWindow(refLayerIdc);
 #endif*/
+#if P0312_VERT_PHASE_ADJ
+          scalEL.setVertPhasePositionFlag (pcSlice->getVertPhasePositionFlag(i));
+#endif
 #if O0215_PHASE_ALIGNMENT
 #if O0194_JOINT_US_BITSHIFT
Index: branches/SHM-5.1-dev/source/Lib/TLibEncoder/TEncCavlc.cpp
===================================================================
--- branches/SHM-5.1-dev/source/Lib/TLibEncoder/TEncCavlc.cpp	(revision 625)
+++ branches/SHM-5.1-dev/source/Lib/TLibEncoder/TEncCavlc.cpp	(revision 627)
@@ -663,4 +663,7 @@
       WRITE_SVLC( scaledWindow.getWindowRightOffset()  >> 1, "scaled_ref_layer_right_offset" );
       WRITE_SVLC( scaledWindow.getWindowBottomOffset() >> 1, "scaled_ref_layer_bottom_offset" );
+#if P0312_VERT_PHASE_ADJ
+      WRITE_FLAG( scaledWindow.getVertPhasePositionEnableFlag(), "vert_phase_pos_enable_flag" ); 
+#endif
     }
   }
@@ -1458,4 +1461,8 @@
     }
   }
+#endif
+
+#if P0312_VERT_PHASE_ADJ
+    WRITE_FLAG( vps->getVpsVuiVertPhaseInUseFlag(), "vps_vui_vert_phase_in_use_flag" );
 #endif
 
@@ -1839,4 +1846,13 @@
       }
     }     
+#if P0312_VERT_PHASE_ADJ
+    for(Int i = 0; i < pcSlice->getActiveNumILRRefIdx(); i++ )
+    {
+      if (pcSlice->getSPS()->getVertPhasePositionEnableFlag(pcSlice->getInterLayerPredLayerIdc(i)))
+      {
+        WRITE_FLAG( pcSlice->getVertPhasePositionFlag(i), "phase_pos_flag" );
+      }
+    }
+#endif
 #endif //SVC_EXTENSION
 
Index: branches/SHM-5.1-dev/source/Lib/TLibEncoder/TEncCfg.h
===================================================================
--- branches/SHM-5.1-dev/source/Lib/TLibEncoder/TEncCfg.h	(revision 625)
+++ branches/SHM-5.1-dev/source/Lib/TLibEncoder/TEncCfg.h	(revision 627)
@@ -386,5 +386,9 @@
 
   Window   &getConformanceWindow()                           { return m_conformanceWindow; }
+#if P0312_VERT_PHASE_ADJ 
+  Void      setConformanceWindow (Int confLeft, Int confRight, Int confTop, Int confBottom ) { m_conformanceWindow.setWindow (confLeft, confRight, confTop, confBottom, false, false); }
+#else
   Void      setConformanceWindow (Int confLeft, Int confRight, Int confTop, Int confBottom ) { m_conformanceWindow.setWindow (confLeft, confRight, confTop, confBottom); }
+#endif
 
   Void      setFramesToBeEncoded            ( Int   i )      { m_framesToBeEncoded = i; }
Index: branches/SHM-5.1-dev/source/Lib/TLibEncoder/TEncGOP.cpp
===================================================================
--- branches/SHM-5.1-dev/source/Lib/TLibEncoder/TEncGOP.cpp	(revision 625)
+++ branches/SHM-5.1-dev/source/Lib/TLibEncoder/TEncGOP.cpp	(revision 627)
@@ -771,4 +771,11 @@
         }
 
+#if P0312_VERT_PHASE_ADJ
+#if O0098_SCALED_REF_LAYER_ID
+        Window &scalEL = pcSlice->getSPS()->getScaledRefLayerWindowForLayer(pcSlice->getVPS()->getRefLayerId(m_layerId, refLayerIdc));
+#else
+        Window &scalEL = pcSlice->getSPS()->getScaledRefLayerWindow(refLayerIdc);
+#endif
+#else
 #if O0098_SCALED_REF_LAYER_ID
         const Window &scalEL = m_pcEncTop->getScaledRefLayerWindowForLayer(pcSlice->getVPS()->getRefLayerId(m_layerId, refLayerIdc));
@@ -776,4 +783,5 @@
         const Window &scalEL = m_pcEncTop->getScaledRefLayerWindow(refLayerIdc);
 #endif
+#endif
 
         Int widthBL   = pcSlice->getBaseColPic(refLayerIdc)->getPicYuvRec()->getWidth();
@@ -797,4 +805,12 @@
           Window scalEL = pcSlice->getSPS()->getScaledRefLayerWindow(refLayerIdc);
 #endif*/
+#if P0312_VERT_PHASE_ADJ
+          //when PhasePositionEnableFlag is equal to 1, set vertPhasePositionFlag to 0 if BL is top field and 1 if bottom
+          if (scalEL.getVertPhasePositionEnableFlag())
+          {
+            pcSlice->setVertPhasePositionFlag(pcSlice->getPOC()%2, i);
+            scalEL.setVertPhasePositionFlag (pcSlice->getVertPhasePositionFlag(i));
+          }
+#endif
 #if O0215_PHASE_ALIGNMENT
 #if O0194_JOINT_US_BITSHIFT
Index: branches/SHM-5.1-dev/source/Lib/TLibEncoder/TEncTop.cpp
===================================================================
--- branches/SHM-5.1-dev/source/Lib/TLibEncoder/TEncTop.cpp	(revision 625)
+++ branches/SHM-5.1-dev/source/Lib/TLibEncoder/TEncTop.cpp	(revision 627)
@@ -902,4 +902,7 @@
 #endif
     m_cSPS.getScaledRefLayerWindow(i) = m_scaledRefLayerWindow[i];
+#if P0312_VERT_PHASE_ADJ
+    m_cSPS.setVertPhasePositionEnableFlag(i,m_scaledRefLayerWindow[i].getVertPhasePositionEnableFlag());
+#endif
   }
 #endif //SVC_EXTENSION
Index: branches/SHM-5.1-dev/source/Lib/TLibEncoder/TEncTop.h
===================================================================
--- branches/SHM-5.1-dev/source/Lib/TLibEncoder/TEncTop.h	(revision 625)
+++ branches/SHM-5.1-dev/source/Lib/TLibEncoder/TEncTop.h	(revision 627)
@@ -139,4 +139,7 @@
 #endif
   Window                  m_scaledRefLayerWindow[MAX_LAYERS];
+#if P0312_VERT_PHASE_ADJ
+  Bool                    m_vertPhasePositionEnableFlag[MAX_LAYERS];
+#endif
 #if POC_RESET_FLAG
   Int                     m_pocAdjustmentValue;
@@ -226,4 +229,9 @@
 #endif
   Window&  getScaledRefLayerWindow(Int x)            { return m_scaledRefLayerWindow[x]; }
+#if P0312_VERT_PHASE_ADJ
+  Void                    setVertPhasePositionEnableFlag(Int x, Bool b) { m_vertPhasePositionEnableFlag[x] = b;   }
+  UInt                    getVertPhasePositionEnableFlag(Int x)          { return m_vertPhasePositionEnableFlag[x]; }
+#endif
+
   TComPic** getIlpList() { return m_cIlpPic; }
 #if REF_IDX_MFM
