Index: trunk/source/App/TAppDecoder/TAppDecTop.cpp
===================================================================
--- trunk/source/App/TAppDecoder/TAppDecTop.cpp	(revision 622)
+++ trunk/source/App/TAppDecoder/TAppDecTop.cpp	(revision 655)
@@ -60,5 +60,4 @@
 #endif
 {
-  ::memset (m_abDecFlag, 0, sizeof (m_abDecFlag));
 #if H_MV
   for (Int i = 0; i < MAX_NUM_LAYER_IDS; i++) m_layerIdToDecIdx[i] = -1; 
@@ -148,5 +147,5 @@
 
   // main decoder loop
-  Bool recon_opened = false; // reconstruction file not yet opened. (must be performed after SPS is seen)
+  Bool openedReconFile = false; // reconstruction file not yet opened. (must be performed after SPS is seen)
 #else
 #if H_3D
@@ -176,8 +175,4 @@
 #endif
     AnnexBStats stats = AnnexBStats();
-#if !H_MV
-    Bool bPreviousPictureDecoded = false;
-#endif
-
     vector<uint8_t> nalUnit;
     InputNALUnit nalu;
@@ -246,18 +241,8 @@
 #endif
         }
-
-        
 #else
       if( (m_iMaxTemporalLayer >= 0 && nalu.m_temporalId > m_iMaxTemporalLayer) || !isNaluWithinTargetDecLayerIdSet(&nalu)  )
       {
-        if(bPreviousPictureDecoded)
-        {
-          bNewPicture = true;
-          bPreviousPictureDecoded = false;
-        }
-        else
-        {
-          bNewPicture = false;
-        }
+        bNewPicture = false;
       }
       else
@@ -290,7 +275,4 @@
 #endif
         }
-#if !H_MV
-        bPreviousPictureDecoded = true; 
-#endif
       }
     }
@@ -311,9 +293,5 @@
         TComPic* picLastCoded = m_ivPicLists.getPic( m_tDecTop[dI]->getLayerId(), pocLastPic );
         assert( picLastCoded != NULL );        
-#if MTK_SONY_PROGRESSIVE_MV_COMPRESSION_E0170
         picLastCoded->compressMotion(1);
-#else
-        picLastCoded->compressMotion();         
-#endif
       }
     }
@@ -325,5 +303,5 @@
       if ( m_pchReconFiles[decIdxLastPic] && !m_reconOpen[decIdxLastPic] )
 #else
-      if ( m_pchReconFile && !recon_opened )
+      if ( m_pchReconFile && !openedReconFile  )
 #endif
       {
@@ -338,5 +316,5 @@
 #else
         m_cTVideoIOYuvReconFile.open( m_pchReconFile, true, m_outputBitDepthY, m_outputBitDepthC, g_bitDepthY, g_bitDepthC ); // write mode
-        recon_opened = true;
+        openedReconFile  = true;
       }
       if ( bNewPicture && 
@@ -466,86 +444,179 @@
 {
   TComList<TComPic*>::iterator iterPic   = pcListPic->begin();
-  Int not_displayed = 0;
-
-  while (iterPic != pcListPic->end())
-  {
-    TComPic* pcPic = *(iterPic);
-#if H_MV
-    if(pcPic->getOutputMark() && pcPic->getPOC() > m_pocLastDisplay[decIdx])
-#else
-    if(pcPic->getOutputMark() && pcPic->getPOC() > m_iPOCLastDisplay)
-#endif
-    {
-       not_displayed++;
-    }
-    iterPic++;
-  }
-  iterPic   = pcListPic->begin();
+  Int numPicsNotYetDisplayed = 0;
   
   while (iterPic != pcListPic->end())
   {
     TComPic* pcPic = *(iterPic);
-    
-#if H_MV
-    if ( pcPic->getOutputMark() && (not_displayed >  pcPic->getNumReorderPics(tId) && pcPic->getPOC() > m_pocLastDisplay[decIdx]))
-#else
-    if ( pcPic->getOutputMark() && (not_displayed >  pcPic->getNumReorderPics(tId) && pcPic->getPOC() > m_iPOCLastDisplay))
-#endif
-    {
-      // write to file
-       not_displayed--;
+#if H_MV
+    if(pcPic->getOutputMark() && pcPic->getPOC() > m_pocLastDisplay[decIdx])
+#else
+    if(pcPic->getOutputMark() && pcPic->getPOC() > m_iPOCLastDisplay)
+#endif
+    {
+      numPicsNotYetDisplayed++;
+    }
+    iterPic++;
+  }
+  iterPic   = pcListPic->begin();
+  if (numPicsNotYetDisplayed>2)
+  {
+    iterPic++;
+  }
+  
+  TComPic* pcPic = *(iterPic);
+  if (numPicsNotYetDisplayed>2 && pcPic->isField()) //Field Decoding
+  {
+    TComList<TComPic*>::iterator endPic   = pcListPic->end();
+    endPic--;
+    iterPic   = pcListPic->begin();
+    while (iterPic != endPic)
+    {
+      TComPic* pcPicTop = *(iterPic);
+      iterPic++;
+      TComPic* pcPicBottom = *(iterPic);
+      
+#if H_MV
+      if ( pcPicTop->getOutputMark() && (numPicsNotYetDisplayed >  pcPicTop->getNumReorderPics(tId) && !(pcPicTop->getPOC()%2) && pcPicBottom->getPOC() == pcPicTop->getPOC()+1)
+          && pcPicBottom->getOutputMark() && (numPicsNotYetDisplayed >  pcPicBottom->getNumReorderPics(tId) && (pcPicTop->getPOC() == m_pocLastDisplay[decIdx]+1 || m_pocLastDisplay[decIdx]<0)))
+#else
+      if ( pcPicTop->getOutputMark() && (numPicsNotYetDisplayed >  pcPicTop->getNumReorderPics(tId) && !(pcPicTop->getPOC()%2) && pcPicBottom->getPOC() == pcPicTop->getPOC()+1)
+          && pcPicBottom->getOutputMark() && (numPicsNotYetDisplayed >  pcPicBottom->getNumReorderPics(tId) && (pcPicTop->getPOC() == m_iPOCLastDisplay+1 || m_iPOCLastDisplay<0)))
+#endif
+      {
+        // write to file
+        numPicsNotYetDisplayed = numPicsNotYetDisplayed-2;
 #if H_MV
       if ( m_pchReconFiles[decIdx] )
 #else
-      if ( m_pchReconFile )
-#endif
-      {
-        const Window &conf = pcPic->getConformanceWindow();
-        const Window &defDisp = m_respectDefDispWindow ? pcPic->getDefDisplayWindow() : Window();
-#if H_MV
-#if H_MV5
+        if ( m_pchReconFile )
+#endif
+        {
+          const Window &conf = pcPicTop->getConformanceWindow();
+          const Window &defDisp = m_respectDefDispWindow ? pcPicTop->getDefDisplayWindow() : Window();
+
+          const Bool isTff = pcPicTop->isTopField();
+#if H_MV
         assert( conf   .getScaledFlag() );
         assert( defDisp.getScaledFlag() );
-#endif
+        m_tVideoIOYuvReconFile[decIdx]->write( pcPicTop->getPicYuvRec(), pcPicBottom->getPicYuvRec(),
+#else
+          m_cTVideoIOYuvReconFile.write( pcPicTop->getPicYuvRec(), pcPicBottom->getPicYuvRec(),
+#endif
+                                        conf.getWindowLeftOffset() + defDisp.getWindowLeftOffset(),
+                                        conf.getWindowRightOffset() + defDisp.getWindowRightOffset(),
+                                        conf.getWindowTopOffset() + defDisp.getWindowTopOffset(),
+                                        conf.getWindowBottomOffset() + defDisp.getWindowBottomOffset(), isTff );
+        }
+        
+        // update POC of display order
+#if H_MV
+        m_pocLastDisplay[decIdx] = pcPic->getPOC();
+#else
+        m_iPOCLastDisplay = pcPicBottom->getPOC();
+#endif
+        
+        // erase non-referenced picture in the reference picture list after display
+        if ( !pcPicTop->getSlice(0)->isReferenced() && pcPicTop->getReconMark() == true )
+        {
+#if !DYN_REF_FREE
+          pcPicTop->setReconMark(false);
+          
+          // mark it should be extended later
+          pcPicTop->getPicYuvRec()->setBorderExtension( false );
+          
+#else
+          pcPicTop->destroy();
+          pcListPic->erase( iterPic );
+          iterPic = pcListPic->begin(); // to the beginning, non-efficient way, have to be revised!
+          continue;
+#endif
+        }
+        if ( !pcPicBottom->getSlice(0)->isReferenced() && pcPicBottom->getReconMark() == true )
+        {
+#if !DYN_REF_FREE
+          pcPicBottom->setReconMark(false);
+          
+          // mark it should be extended later
+          pcPicBottom->getPicYuvRec()->setBorderExtension( false );
+          
+#else
+          pcPicBottom->destroy();
+          pcListPic->erase( iterPic );
+          iterPic = pcListPic->begin(); // to the beginning, non-efficient way, have to be revised!
+          continue;
+#endif
+        }
+        pcPicTop->setOutputMark(false);
+        pcPicBottom->setOutputMark(false);
+      }
+    }
+  }
+  else if (!pcPic->isField()) //Frame Decoding
+  {
+    iterPic = pcListPic->begin();
+    while (iterPic != pcListPic->end())
+    {
+      pcPic = *(iterPic);
+
+#if H_MV
+      if ( pcPic->getOutputMark() && (numPicsNotYetDisplayed >  pcPic->getNumReorderPics(tId) && pcPic->getPOC() > m_pocLastDisplay[decIdx]))
+#else      
+      if ( pcPic->getOutputMark() && (numPicsNotYetDisplayed >  pcPic->getNumReorderPics(tId) && pcPic->getPOC() > m_iPOCLastDisplay))
+#endif
+      {
+        // write to file
+        numPicsNotYetDisplayed--;
+#if H_MV
+      if ( m_pchReconFiles[decIdx] )
+#else
+        if ( m_pchReconFile )
+#endif
+        {
+          const Window &conf = pcPic->getConformanceWindow();
+          const Window &defDisp = m_respectDefDispWindow ? pcPic->getDefDisplayWindow() : Window();
+#if H_MV
+        assert( conf   .getScaledFlag() );
+        assert( defDisp.getScaledFlag() );
         m_tVideoIOYuvReconFile[decIdx]->write( pcPic->getPicYuvRec(),
 #else
-        m_cTVideoIOYuvReconFile.write( pcPic->getPicYuvRec(),
-#endif
-                                       conf.getWindowLeftOffset() + defDisp.getWindowLeftOffset(),
-                                       conf.getWindowRightOffset() + defDisp.getWindowRightOffset(),
-                                       conf.getWindowTopOffset() + defDisp.getWindowTopOffset(),
-                                       conf.getWindowBottomOffset() + defDisp.getWindowBottomOffset() );
+          m_cTVideoIOYuvReconFile.write( pcPic->getPicYuvRec(),
+#endif
+                                        conf.getWindowLeftOffset() + defDisp.getWindowLeftOffset(),
+                                        conf.getWindowRightOffset() + defDisp.getWindowRightOffset(),
+                                        conf.getWindowTopOffset() + defDisp.getWindowTopOffset(),
+                                        conf.getWindowBottomOffset() + defDisp.getWindowBottomOffset() );
+        }
+        
+        // update POC of display order
+#if H_MV
+        m_pocLastDisplay[decIdx] = pcPic->getPOC();
+#else
+        m_iPOCLastDisplay = pcPic->getPOC();
+#endif
+        
+        // erase non-referenced picture in the reference picture list after display
+        if ( !pcPic->getSlice(0)->isReferenced() && pcPic->getReconMark() == true )
+        {
+#if !DYN_REF_FREE
+          pcPic->setReconMark(false);
+          
+          // mark it should be extended later
+          pcPic->getPicYuvRec()->setBorderExtension( false );
+          
+#else
+          pcPic->destroy();
+          pcListPic->erase( iterPic );
+          iterPic = pcListPic->begin(); // to the beginning, non-efficient way, have to be revised!
+          continue;
+#endif
+        }
+        pcPic->setOutputMark(false);
       }
       
-      // update POC of display order
-#if H_MV
-      m_pocLastDisplay[decIdx] = pcPic->getPOC();
-#else
-      m_iPOCLastDisplay = pcPic->getPOC();
-#endif
-      
-      // erase non-referenced picture in the reference picture list after display
-      if ( !pcPic->getSlice(0)->isReferenced() && pcPic->getReconMark() == true )
-      {
-#if !DYN_REF_FREE
-        pcPic->setReconMark(false);
-        
-        // mark it should be extended later
-        pcPic->getPicYuvRec()->setBorderExtension( false );
-        
-#else
-        pcPic->destroy();
-        pcListPic->erase( iterPic );
-        iterPic = pcListPic->begin(); // to the beginning, non-efficient way, have to be revised!
-        continue;
-#endif
-      }
-      pcPic->setOutputMark(false);
-    }
-    
-    iterPic++;
-  }
-}
-
+      iterPic++;
+    }
+  }
+}
 /** \param pcListPic list of pictures to be written to file
     \todo            DYN_REF_FREE should be revised
@@ -560,75 +631,170 @@
   {
     return;
-  } 
+  }
   TComList<TComPic*>::iterator iterPic   = pcListPic->begin();
-
+  
   iterPic   = pcListPic->begin();
+  TComPic* pcPic = *(iterPic);
   
-  while (iterPic != pcListPic->end())
-  {
-    TComPic* pcPic = *(iterPic);
-
-    if ( pcPic->getOutputMark() )
-    {
-      // write to file
+  if (pcPic->isField()) //Field Decoding
+  {
+    TComList<TComPic*>::iterator endPic   = pcListPic->end();
+    endPic--;
+    TComPic *pcPicTop, *pcPicBottom = NULL;
+    while (iterPic != endPic)
+    {
+      pcPicTop = *(iterPic);
+      iterPic++;
+      pcPicBottom = *(iterPic);
+      
+      if ( pcPicTop->getOutputMark() && pcPicBottom->getOutputMark() && !(pcPicTop->getPOC()%2) && (pcPicBottom->getPOC() == pcPicTop->getPOC()+1) )
+      {
+        // write to file
 #if H_MV
       if ( m_pchReconFiles[decIdx] )
 #else
-      if ( m_pchReconFile )
-#endif
-      {
-        const Window &conf = pcPic->getConformanceWindow();
-        const Window &defDisp = m_respectDefDispWindow ? pcPic->getDefDisplayWindow() : Window();
-#if H_MV
-#if H_MV5
+        if ( m_pchReconFile )
+#endif
+        {
+          const Window &conf = pcPicTop->getConformanceWindow();
+          const Window &defDisp = m_respectDefDispWindow ? pcPicTop->getDefDisplayWindow() : Window();
+          const Bool isTff = pcPicTop->isTopField();
+#if H_MV
         assert( conf   .getScaledFlag() );
         assert( defDisp.getScaledFlag() );
-#endif
+        m_tVideoIOYuvReconFile[decIdx]->write( pcPicTop->getPicYuvRec(), pcPicBottom->getPicYuvRec(),
+#else
+          m_cTVideoIOYuvReconFile.write( pcPicTop->getPicYuvRec(), pcPicBottom->getPicYuvRec(),
+#endif
+                                        conf.getWindowLeftOffset() + defDisp.getWindowLeftOffset(),
+                                        conf.getWindowRightOffset() + defDisp.getWindowRightOffset(),
+                                        conf.getWindowTopOffset() + defDisp.getWindowTopOffset(),
+                                        conf.getWindowBottomOffset() + defDisp.getWindowBottomOffset(), isTff );
+        }
+        
+        // update POC of display order
+#if H_MV
+      m_pocLastDisplay[decIdx] = pcPic->getPOC();
+#else
+      m_iPOCLastDisplay = pcPicBottom->getPOC();
+#endif        
+        // erase non-referenced picture in the reference picture list after display
+        if ( !pcPicTop->getSlice(0)->isReferenced() && pcPicTop->getReconMark() == true )
+        {
+#if !DYN_REF_FREE
+          pcPicTop->setReconMark(false);
+          
+          // mark it should be extended later
+          pcPicTop->getPicYuvRec()->setBorderExtension( false );
+          
+#else
+          pcPicTop->destroy();
+          pcListPic->erase( iterPic );
+          iterPic = pcListPic->begin(); // to the beginning, non-efficient way, have to be revised!
+          continue;
+#endif
+        }
+        if ( !pcPicBottom->getSlice(0)->isReferenced() && pcPicBottom->getReconMark() == true )
+        {
+#if !DYN_REF_FREE
+          pcPicBottom->setReconMark(false);
+          
+          // mark it should be extended later
+          pcPicBottom->getPicYuvRec()->setBorderExtension( false );
+          
+#else
+          pcPicBottom->destroy();
+          pcListPic->erase( iterPic );
+          iterPic = pcListPic->begin(); // to the beginning, non-efficient way, have to be revised!
+          continue;
+#endif
+        }
+        pcPicTop->setOutputMark(false);
+        pcPicBottom->setOutputMark(false);
+        
+#if !DYN_REF_FREE
+        if(pcPicTop)
+        {
+          pcPicTop->destroy();
+          delete pcPicTop;
+          pcPicTop = NULL;
+        }
+#endif
+      }
+    }
+    if(pcPicBottom)
+    {
+      pcPicBottom->destroy();
+      delete pcPicBottom;
+      pcPicBottom = NULL;
+    }
+  }
+  else //Frame decoding
+  {
+    while (iterPic != pcListPic->end())
+    {
+      pcPic = *(iterPic);
+      
+      if ( pcPic->getOutputMark() )
+      {
+        // write to file
+#if H_MV
+      if ( m_pchReconFiles[decIdx] )
+#else
+        if ( m_pchReconFile )
+#endif
+        {
+          const Window &conf = pcPic->getConformanceWindow();
+          const Window &defDisp = m_respectDefDispWindow ? pcPic->getDefDisplayWindow() : Window();
+#if H_MV
+        assert( conf   .getScaledFlag() );
+        assert( defDisp.getScaledFlag() );
         m_tVideoIOYuvReconFile[decIdx]->write( pcPic->getPicYuvRec(),
 #else
-        m_cTVideoIOYuvReconFile.write( pcPic->getPicYuvRec(),
-#endif
-                                       conf.getWindowLeftOffset() + defDisp.getWindowLeftOffset(),
-                                       conf.getWindowRightOffset() + defDisp.getWindowRightOffset(),
-                                       conf.getWindowTopOffset() + defDisp.getWindowTopOffset(),
-                                       conf.getWindowBottomOffset() + defDisp.getWindowBottomOffset() );
-      }
-      
-      // update POC of display order
+          m_cTVideoIOYuvReconFile.write( pcPic->getPicYuvRec(),
+#endif
+                                        conf.getWindowLeftOffset() + defDisp.getWindowLeftOffset(),
+                                        conf.getWindowRightOffset() + defDisp.getWindowRightOffset(),
+                                        conf.getWindowTopOffset() + defDisp.getWindowTopOffset(),
+                                        conf.getWindowBottomOffset() + defDisp.getWindowBottomOffset() );
+        }
+        
+        // update POC of display order
 #if H_MV
       m_pocLastDisplay[decIdx] = pcPic->getPOC();
 #else
-      m_iPOCLastDisplay = pcPic->getPOC();
-#endif
-      
-      // erase non-referenced picture in the reference picture list after display
-      if ( !pcPic->getSlice(0)->isReferenced() && pcPic->getReconMark() == true )
-      {
+        m_iPOCLastDisplay = pcPic->getPOC();
+#endif
+        
+        // erase non-referenced picture in the reference picture list after display
+        if ( !pcPic->getSlice(0)->isReferenced() && pcPic->getReconMark() == true )
+        {
 #if !DYN_REF_FREE
-        pcPic->setReconMark(false);
-        
-        // mark it should be extended later
-        pcPic->getPicYuvRec()->setBorderExtension( false );
-        
-#else
-        pcPic->destroy();
-        pcListPic->erase( iterPic );
-        iterPic = pcListPic->begin(); // to the beginning, non-efficient way, have to be revised!
-        continue;
-#endif
-      }
-      pcPic->setOutputMark(false);
-    }
+          pcPic->setReconMark(false);
+          
+          // mark it should be extended later
+          pcPic->getPicYuvRec()->setBorderExtension( false );
+          
+#else
+          pcPic->destroy();
+          pcListPic->erase( iterPic );
+          iterPic = pcListPic->begin(); // to the beginning, non-efficient way, have to be revised!
+          continue;
+#endif
+        }
+        pcPic->setOutputMark(false);
+      }
 #if !H_MV
 #if !DYN_REF_FREE
-    if(pcPic)
-    {
-      pcPic->destroy();
-      delete pcPic;
-      pcPic = NULL;
-    }
-#endif 
-#endif
-    iterPic++;
+      if(pcPic)
+      {
+        pcPic->destroy();
+        delete pcPic;
+        pcPic = NULL;
+      }
+#endif    
+#endif
+      iterPic++;
+    }
   }
 #if H_MV
Index: trunk/source/App/TAppDecoder/TAppDecTop.h
===================================================================
--- trunk/source/App/TAppDecoder/TAppDecTop.h	(revision 622)
+++ trunk/source/App/TAppDecoder/TAppDecTop.h	(revision 655)
@@ -72,5 +72,4 @@
 #endif
     // for output control
-  Bool                            m_abDecFlag[ MAX_GOP ];         ///< decoded flag in one GOP
 #if H_MV
   Int                             m_pocLastDisplay      [ MAX_NUM_LAYERS ]; ///< last POC in display order
Index: trunk/source/App/TAppEncoder/TAppEncCfg.cpp
===================================================================
--- trunk/source/App/TAppEncoder/TAppEncCfg.cpp	(revision 622)
+++ trunk/source/App/TAppEncoder/TAppEncCfg.cpp	(revision 655)
@@ -82,4 +82,8 @@
   m_codedPivotValue = NULL;
   m_targetPivotValue = NULL;
+
+#if KWU_RC_MADPRED_E0227
+  m_depthMADPred = 0;
+#endif
 }
 
@@ -224,10 +228,4 @@
     in>>entry.m_interViewRefPosL[1][i];
   }
-#if !H_MV5
-  if (entry.m_numActiveRefLayerPics > 0 )
-  {
-    in>>entry.m_collocatedRefLayerIdx; 
-  }
-#endif
 #endif
   return in;
@@ -369,19 +367,11 @@
   ("NumberOfLayers",        m_numberOfLayers     , 1,                     "Number of layers")
 #if !H_3D
-#if H_MV5
   ("ScalabilityMask",       m_scalabilityMask    , 2                    , "Scalability Mask")    
-#else
-  ("ScalabilityMask",       m_scalabilityMask    , 1                    , "Scalability Mask")    
-#endif
 #else
   ("ScalabilityMask",       m_scalabilityMask    , 3                    , "Scalability Mask, 1: Texture 3: Texture + Depth ")    
 #endif  
   ("DimensionIdLen",        m_dimensionIdLen     , cfg_dimensionLength  , "Number of bits used to store dimensions Id")
-#if H_MV5
   ("ViewOrderIndex",        m_viewOrderIndex     , std::vector<Int>(1,0), "View Order Index per layer")
   ("ViewId",                m_viewId             , std::vector<Int>(1,0), "View Id per View Order Index")
-#else
-  ("ViewId",                m_viewId             , std::vector<Int>(1,0), "View Id")
-#endif
 #if H_3D
   ("DepthFlag",             m_depthFlag          , std::vector<Int>(1,0), "Depth Flag")
@@ -428,4 +418,8 @@
   ("FramesToBeEncoded,f",   m_framesToBeEncoded,   0, "Number of frames to be encoded (default=all)")
 
+  //Field coding parameters
+  ("FieldCoding", m_isField, false, "Signals if it's a field based coding")
+  ("TopFieldFirst, Tff", m_isTopFieldFirst, false, "In case of field based coding, signals whether if it's a top field first or not")
+  
   // Profile and level
   ("Profile", m_profile,   Profile::NONE, "Profile to be used when encoding (Incomplete)")
@@ -517,5 +511,5 @@
   ("IlluCompEnable",           m_abUseIC, std::vector<Bool>(2, true), "Enable illumination compensation")
 #endif
-#if LGE_INTER_SDC_E0156
+#if H_3D_INTER_SDC
   ("InterSDC",                 m_bDepthInterSDCFlag,        true, "Enable depth inter SDC")
 #endif
@@ -594,11 +588,26 @@
   ( "InitialQP",           m_RCInitialQP,               0, "Rate control: initial QP" )
   ( "RCForceIntraQP",      m_RCForceIntraQP,        false, "Rate control: force intra QP to be equal to initial QP" )
+
+#if KWU_RC_VIEWRC_E0227
+  ("ViewWiseTargetBits, -vtbr" ,  m_viewTargetBits,  std::vector<Int>(1, 32), "View-wise target bit-rate setting")
+  ("TargetBitAssign, -ta", m_viewWiseRateCtrl, false, "View-wise rate control on/off")
+#endif
+#if KWU_RC_MADPRED_E0227
+  ("DepthMADPred, -dm", m_depthMADPred, (UInt)0, "Depth based MAD prediction on/off")
+#endif
 #else
   ("RateCtrl,-rc", m_enableRateCtrl, false, "Rate control on/off")
   ("TargetBitrate,-tbr", m_targetBitrate, 0, "Input target bitrate")
   ("NumLCUInUnit,-nu", m_numLCUInUnit, 0, "Number of LCUs in an Unit")
-#endif
-
-#if H_MV5
+
+#if KWU_RC_VIEWRC_E0227
+  ("ViewWiseTargetBits, -vtbr" ,  m_viewTargetBits,  std::vector<Int>(1, 32), "View-wise target bit-rate setting")
+  ("TargetBitAssign, -ta", m_viewWiseRateCtrl, false, "View-wise rate control on/off")
+#endif
+#if KWU_RC_MADPRED_E0227
+  ("DepthMADPred, -dm", m_depthMADPred, (UInt)0, "Depth based MAD prediction on/off")
+#endif
+#endif
+
 #if H_MV
   // VPS VUI
@@ -617,5 +626,4 @@
   ("CtuBasedOffsetEnabledFlag"   , m_ctuBasedOffsetEnabledFlag   , std::vector< Bool >(1,0)  ,MAX_NUM_LAYERS       , "CtuBasedOffsetEnabledFlag    per direct reference for the N-th layer")
   ("MinHorizontalCtuOffsetPlus1" , m_minHorizontalCtuOffsetPlus1 , std::vector< Int  >(1,0)  ,MAX_NUM_LAYERS       , "MinHorizontalCtuOffsetPlus1  per direct reference for the N-th layer")
-#endif
 #endif
 
@@ -762,7 +770,5 @@
     if( k == 0 )
     {
-#if H_MV5
       m_GOPListMvc[0][0].m_sliceType = 'I'; 
-#endif
       for( Int i = 1; i < MAX_GOP + 1; i++ ) 
       {
@@ -810,4 +816,14 @@
   const list<const Char*>& argv_unhandled = po::scanArgv(opts, argc, (const Char**) argv);
 
+  if(m_isField)
+  {
+    //Frame height
+    m_iSourceHeightOrg = m_iSourceHeight;
+    //Field height
+    m_iSourceHeight = m_iSourceHeight >> 1;
+    //number of fields to encode
+    m_framesToBeEncoded *= 2;
+  }
+  
   for (list<const Char*>::const_iterator it = argv_unhandled.begin(); it != argv_unhandled.end(); it++)
   {
@@ -928,4 +944,9 @@
         m_aiPad[1] = m_confBottom = ((m_iSourceHeight / minCuSize) + 1) * minCuSize - m_iSourceHeight;
         m_iSourceHeight += m_confBottom;
+        if ( m_isField )
+        {
+          m_iSourceHeightOrg += m_confBottom << 1;
+          m_aiPad[1] = m_confBottom << 1;
+        }
       }
       if (m_aiPad[0] % TComSPS::getWinUnitX(CHROMA_420) != 0)
@@ -968,5 +989,4 @@
   // allocate slice-based dQP values
 #if H_MV
-#if H_MV5
   xResizeVector( m_viewOrderIndex    ); 
 
@@ -988,26 +1008,4 @@
 #if H_3D
   xResizeVector( m_depthFlag ); 
-#endif
-#else
-  xResizeVector( m_viewId    ); 
-#if H_3D
-  xResizeVector( m_depthFlag ); 
-
-  std::vector<Int> uniqueViewIds; 
-  for( Int layer = 0; layer < m_numberOfLayers; layer++ )
-  {    
-    Bool isIn = false; 
-    for ( Int i = 0 ; i < uniqueViewIds.size(); i++ )
-    {
-      isIn = isIn || ( m_viewId[ layer ] == uniqueViewIds[ i ] ); 
-    }
-    if ( !isIn ) 
-    {
-      uniqueViewIds.push_back( m_viewId[ layer ] ); 
-    } 
-  }
-  m_iNumberOfViews = (Int) uniqueViewIds.size(); 
-#endif
-
 #endif
   xResizeVector( m_fQP ); 
@@ -1283,5 +1281,4 @@
   xConfirmPara( (m_layerIdInNuh.size()!=1) && (m_layerIdInNuh.size() < m_numberOfLayers) , "LayerIdInNuh must be given for all layers. ");
   
-#if H_MV5
 #if H_3D
   xConfirmPara( m_scalabilityMask != 2 && m_scalabilityMask != 3, "Scalability Mask must be equal to 2 or 3. ");
@@ -1298,19 +1295,4 @@
 
   m_dimIds.push_back( m_viewOrderIndex );   
-#else
-#if H_3D
-  xConfirmPara( m_scalabilityMask != 1 && m_scalabilityMask != 3, "Scalability Mask must be equal to 1 or 3. ");
-#else
-  xConfirmPara( m_scalabilityMask != 1 , "Scalability Mask must be equal to 1. ");
-#endif
- 
-  m_dimIds.push_back( m_viewId ); 
-  const Int viewDimPosition = 0; 
-#if H_3D
-  if ( m_scalabilityMask & ( 1 << DEPTH_ID ) )
-    m_dimIds.push_back( m_depthFlag ); 
-#endif
-
-#endif
   xConfirmPara(  m_dimensionIdLen.size() < m_dimIds.size(), "DimensionIdLen must be given for all dimensions. "   );   Int dimBitOffset[MAX_NUM_SCALABILITY_TYPES+1]; 
 
@@ -1329,9 +1311,5 @@
   {    
     xConfirmPara( m_dimIds[j].size() < m_numberOfLayers,  "DimensionId must be given for all layers and all dimensions. ");   
-#if H_MV5   
     xConfirmPara( (m_dimIds[j][0] != 0)                 , "DimensionId of layer 0 must be 0. " );
-#else
-    xConfirmPara( ( j != viewDimPosition ) &&  (m_dimIds[j][0] != 0), "DimensionId of layer 0 must be 0. " );
-#endif
     xConfirmPara( m_dimensionIdLen[j] < 1 || m_dimensionIdLen[j] > 8, "DimensionIdLen must be greater than 0 and less than 9 in all dimensions. " ); 
      
@@ -1372,26 +1350,19 @@
      xConfirmPara( allEqual , "Each layer shall have a different position in scalability space." );
 
+#if !H_3D_FCO
      if ( numDiff  == 1 ) 
      {
        Bool inc = m_dimIds[ lastDiff ][ i ] > m_dimIds[ lastDiff ][ j ]; 
-#if H_MV5
        Bool shallBeButIsNotIncreasing = ( !inc  ) ; 
-#else
-       Bool shallBeButIsNotIncreasing = ( !inc && ( lastDiff != viewDimPosition ) ) ; 
-#endif
        if ( shallBeButIsNotIncreasing )
        {       
          printf( "\nError: Positions of Layers %d and %d is not increasing in dimension %d \n", i, j, lastDiff);        
        }
-#if H_MV5
        xConfirmPara( shallBeButIsNotIncreasing,  "DimensionIds shall be increasing within one dimension. " );
-#else
-       xConfirmPara( shallBeButIsNotIncreasing && ( lastDiff != viewDimPosition ),  "DimensionIds shall be increasing within one dimension. " );
-#endif
      }
+#endif
    }
  }
 
-#if H_MV5
  /// ViewId 
  xConfirmPara( m_viewId.size() != m_iNumberOfViews, "The number of ViewIds must be equal to the number of views." ); 
@@ -1399,7 +1370,4 @@
   /// Layer sets
   xConfirmPara( m_vpsNumLayerSets < 0 || m_vpsNumLayerSets > 1024, "VpsNumLayerSets must be greater than 0 and less than 1025. ") ; 
-#else
-  xConfirmPara( m_vpsNumLayerSets < 0 || m_vpsNumLayerSets > 1024, "VpsNumLayerSets must be greater than 0 and less than 1025") ; 
-#endif
   for( Int lsIdx = 0; lsIdx < m_vpsNumLayerSets; lsIdx++ )
   {
@@ -1743,7 +1711,11 @@
   Bool errorGOP=false;
   Int checkGOP=1;
-  Int numRefs = 1;
+  Int numRefs = m_isField ? 2 : 1;
   Int refList[MAX_NUM_REF_PICS+1];
   refList[0]=0;
+  if(m_isField)
+  {
+    refList[1] = 1;
+  }
   Bool isOK[MAX_GOP];
   for(Int i=0; i<MAX_GOP; i++) 
@@ -2157,9 +2129,10 @@
     xConfirmPara( (numLCUInPic % m_numLCUInUnit) != 0, "total number of LCUs in a frame should be completely divided by NumLCUInUnit" );
 
+#if !KWU_FIX_URQ
     m_iMaxDeltaQP       = MAX_DELTA_QP;
+#endif
     m_iMaxCuDQPDepth    = MAX_CUDQP_DEPTH;
   }
 #endif
-#if H_MV5
 #if H_MV
   // VPS VUI
@@ -2184,5 +2157,4 @@
   }
 #endif
-#endif
 
   xConfirmPara(!m_TransquantBypassEnableFlag && m_CUTransquantBypassFlagValue, "CUTransquantBypassFlagValue cannot be 1 when TransquantBypassEnableFlag is 0");
@@ -2250,10 +2222,6 @@
 #endif
 #if H_MV
-#if H_MV5
   xPrintParaVector( "ViewIdVal"     , m_viewId ); 
   xPrintParaVector( "ViewOrderIndex", m_viewOrderIndex ); 
-#else
-  xPrintParaVector( "ViewId", m_viewId ); 
-#endif
 #endif
 #if H_3D
@@ -2268,5 +2236,22 @@
   printf("Real     Format              : %dx%d %dHz\n", m_iSourceWidth - m_confLeft - m_confRight, m_iSourceHeight - m_confTop - m_confBottom, m_iFrameRate );
   printf("Internal Format              : %dx%d %dHz\n", m_iSourceWidth, m_iSourceHeight, m_iFrameRate );
+  if (m_isField)
+  {
+    printf("Frame/Field          : Field based coding\n");
+    printf("Field index          : %u - %d (%d fields)\n", m_FrameSkip, m_FrameSkip+m_framesToBeEncoded-1, m_framesToBeEncoded );
+    if (m_isTopFieldFirst)
+    {
+      printf("Field Order            : Top field first\n");
+    }
+    else
+    {
+      printf("Field Order            : Bottom field first\n");
+    }
+  }
+  else
+  {
+    printf("Frame/Field                  : Frame based coding\n");
   printf("Frame index                  : %u - %d (%d frames)\n", m_FrameSkip, m_FrameSkip+m_framesToBeEncoded-1, m_framesToBeEncoded );
+  }
   printf("CU size / depth              : %d / %d\n", m_uiMaxCUWidth, m_uiMaxCUDepth );
   printf("RQT trans. size (min / max)  : %d / %d\n", 1 << m_uiQuadtreeTULog2MinSize, 1 << m_uiQuadtreeTULog2MaxSize );
@@ -2299,4 +2284,23 @@
     printf("InitialQP                    : %d\n", m_RCInitialQP );
     printf("ForceIntraQP                 : %d\n", m_RCForceIntraQP );
+
+#if KWU_RC_MADPRED_E0227
+    printf("Depth based MAD prediction   : %d\n", m_depthMADPred);
+#endif
+#if KWU_RC_VIEWRC_E0227
+    printf("View-wise Rate control       : %d\n", m_viewWiseRateCtrl);
+    if(m_viewWiseRateCtrl)
+    {
+
+      printf("ViewWiseTargetBits           : ");
+      for (Int i = 0 ; i < m_iNumberOfViews ; i++)
+        printf("%d ", m_viewTargetBits[i]);
+      printf("\n");
+    }
+    else
+    {
+      printf("TargetBitrate                : %d\n", m_RCTargetBitrate );
+    }
+#endif
   }
 #else
@@ -2306,4 +2310,23 @@
     printf("TargetBitrate                : %d\n", m_targetBitrate);
     printf("NumLCUInUnit                 : %d\n", m_numLCUInUnit);
+
+#if KWU_RC_MADPRED_E0227
+    printf("Depth based MAD prediction   : %d\n", m_depthMADPred);
+#endif
+#if KWU_RC_VIEWRC_E0227
+    printf("View-wise Rate control       : %d\n", m_viewWiseRateCtrl);
+    if(m_viewWiseRateCtrl)
+    {
+
+      printf("ViewWiseTargetBits           : ");
+      for (Int i = 0 ; i < m_iNumberOfViews ; i++)
+        printf("%d ", m_viewTargetBits[i]);
+      printf("\n");
+    }
+    else
+    {
+      printf("TargetBitrate                : %d\n", m_targetBitrate );
+    }
+#endif
   }
 #endif
@@ -2414,5 +2437,5 @@
   printf("DLT:%d ", m_useDLT );
 #endif
-#if LGE_INTER_SDC_E0156
+#if H_3D_INTER_SDC
   printf( "interSDC: %d ", m_bDepthInterSDCFlag ? 1 : 0 );
 #endif
Index: trunk/source/App/TAppEncoder/TAppEncCfg.h
===================================================================
--- trunk/source/App/TAppEncoder/TAppEncCfg.h	(revision 622)
+++ trunk/source/App/TAppEncoder/TAppEncCfg.h	(revision 655)
@@ -69,11 +69,5 @@
   std::vector<char*>     m_pchReconFileList;                  ///< output reconstruction file names
   Int                    m_numberOfLayers;                    ///< number of Layers to Encode
-#if H_MV5
   Int                    m_iNumberOfViews;                    ///< number of Layers that are views
-#else
-#if H_3D
-  Int                    m_iNumberOfViews;                    ///< number of Layers that are views
-#endif
-#endif
 #else
   Char*     m_pchReconFile;                                   ///< output reconstruction file
@@ -83,7 +77,5 @@
   std::vector< std::vector<Int> > m_dimIds;                   ///< dimension ids ( pointers to m_viewId and m_depthFlag 
   std::vector<Int>       m_viewId;                            ///< view id
-#if H_MV5
   std::vector<Int>       m_viewOrderIndex;                    ///< view order index  
-#endif
 #if H_3D
   std::vector<Int>       m_depthFlag;                         ///< depth flag
@@ -106,5 +98,4 @@
   std::vector< std::vector<Int> > m_dependencyTypes;          ///< Dependency types of direct reference layers
 
-#if H_MV5
   // VPS VUI
   Bool m_vpsVuiPresentFlag;
@@ -122,5 +113,4 @@
   std::vector< std::vector<Bool > > m_ctuBasedOffsetEnabledFlag;
   std::vector< std::vector<Int  > > m_minHorizontalCtuOffsetPlus1;
-#endif
 
 #if H_3D_IV_MERGE
@@ -149,5 +139,11 @@
   UInt      m_FrameSkip;                                      ///< number of skipped frames from the beginning
   Int       m_iSourceWidth;                                   ///< source width in pixel
-  Int       m_iSourceHeight;                                  ///< source height in pixel
+  Int       m_iSourceHeight;                                  ///< source height in pixel (when interlaced = field height)
+  
+  Int       m_iSourceHeightOrg;                               ///< original source height in pixel (when interlaced = frame height)
+  
+  bool      m_isField;                                        ///< enable field coding
+  bool      m_isTopFieldFirst;
+  
   Int       m_conformanceMode;
   Int       m_confLeft;
@@ -367,8 +363,24 @@
   Int       m_RCInitialQP;                        ///< inital QP for rate control
   Bool      m_RCForceIntraQP;                     ///< force all intra picture to use initial QP or not
+
+#if KWU_RC_VIEWRC_E0227
+  vector<Int>     m_viewTargetBits;
+  Bool      m_viewWiseRateCtrl;                              ///< Flag for using view-wise rate control
+#endif
+#if KWU_RC_MADPRED_E0227
+  UInt       m_depthMADPred;
+#endif
 #else
   Bool      m_enableRateCtrl;                                   ///< Flag for using rate control algorithm
   Int       m_targetBitrate;                                 ///< target bitrate
   Int       m_numLCUInUnit;                                  ///< Total number of LCUs in a frame should be completely divided by the NumLCUInUnit
+
+#if KWU_RC_VIEWRC_E0227
+  vector<Int>     m_viewTargetBits;
+  Bool      m_viewWiseRateCtrl;                              ///< Flag for using view-wise rate control
+#endif
+#if KWU_RC_MADPRED_E0227
+  UInt       m_depthMADPred;
+#endif
 #endif
   Int       m_useScalingListId;                               ///< using quantization matrix
@@ -457,5 +469,5 @@
   Bool      m_bUsePC;                                         ///< flag for using Predictive Coding with QTL
 #endif
-#if LGE_INTER_SDC_E0156
+#if H_3D_INTER_SDC
   Bool m_bDepthInterSDCFlag;                                ///< flag for inter SDC of depth map coding
 #endif
Index: trunk/source/App/TAppEncoder/TAppEncTop.cpp
===================================================================
--- trunk/source/App/TAppEncoder/TAppEncTop.cpp	(revision 622)
+++ trunk/source/App/TAppEncoder/TAppEncTop.cpp	(revision 655)
@@ -88,9 +88,5 @@
     vps.setTemporalNestingFlag(true);
   }
-#if H_MV5
   vps.setMaxLayersMinus1( m_numberOfLayers - 1);
-#else
-  vps.setMaxLayers( m_numberOfLayers );
-#endif
   for(Int i = 0; i < MAX_TLAYER; i++)
   {
@@ -124,15 +120,8 @@
   xSetDependencies( vps );
   xSetProfileTierLevel     ( vps ); 
-#if H_MV5
   xSetRepFormat            ( vps ); 
-#endif
   xSetLayerSets            ( vps ); 
-#if H_MV5
   xSetVPSVUI               ( vps ); 
-#endif
 #if H_3D
-#if !H_MV5
-  vps.initViewIndex(); 
-#endif
   xSetVPSExtension2        ( vps ); 
   m_ivPicLists.setVPS      ( &vps ); 
@@ -140,9 +129,5 @@
 
 
-#if H_MV5
   for(Int layerIdInVps = 0; layerIdInVps < m_numberOfLayers; layerIdInVps++)
-#else 
-  for(Int layer = 0; layer < m_numberOfLayers; layer++)
-#endif
   {
     m_frameRcvd                 .push_back(0);
@@ -151,5 +136,4 @@
     m_acTVideoIOYuvReconFileList.push_back(new TVideoIOYuv);
     m_picYuvRec                 .push_back(new TComList<TComPicYuv*>) ;
-#if H_MV5   
     m_ivPicLists.push_back( m_acTEncTopList[ layerIdInVps ]->getListPic()  ); 
     TEncTop& m_cTEncTop = *m_acTEncTopList[ layerIdInVps ];  // It is not a member, but this name helps avoiding code duplication !!!
@@ -160,20 +144,6 @@
     m_cTEncTop.setViewId                       ( vps.getViewId      (  layerId ) );
     m_cTEncTop.setViewIndex                    ( vps.getViewIndex   (  layerId ) );
-#else
-    m_ivPicLists.push_back( m_acTEncTopList[ layer ]->getListPic()  ); 
-    TEncTop& m_cTEncTop = *m_acTEncTopList[ layer ];  // It is not a member, but this name helps avoiding code duplication !!!
-    
-    m_cTEncTop.setLayerIdInVps                 ( layer ); 
-    m_cTEncTop.setLayerId                      ( vps.getLayerIdInNuh( layer ) );    
-    m_cTEncTop.setViewId                       ( vps.getViewId      ( layer ) );
-
-#endif
 #if H_3D
-#if H_MV5
     Bool isDepth = ( vps.getDepthId     ( layerId ) != 0 ) ;
-#else
-    Bool isDepth = ( vps.getDepthId     ( layer ) != 0 ) ;
-    m_cTEncTop.setViewIndex                    ( vps.getViewIndex   ( layer ) );
-#endif
     m_cTEncTop.setIsDepth                      ( isDepth );
     //====== Camera Parameters =========
@@ -201,5 +171,4 @@
     m_cTEncTop.setDWeight                      ( isDepth ? m_iDWeight             : 0     );
 #endif // H_3D_VSO
-#if H_MV5
 #if H_3D_ARP
     //====== Advanced Inter-view Residual Prediction =========
@@ -209,14 +178,4 @@
 #if H_3D_IC
     m_cTEncTop.setUseIC                        ( vps.getViewIndex( layerId ) == 0 ? false : m_abUseIC[isDepth ? 1 : 0] );
-#endif
-#else
-#if H_3D_ARP
-    //====== Advanced Inter-view Residual Prediction =========
-    m_cTEncTop.setUseAdvRP                     ( ( isDepth || 0==layer ) ? 0 : m_uiUseAdvResPred );
-    m_cTEncTop.setARPStepNum                   ( ( isDepth || 0==layer ) ? 1 : H_3D_ARP_WFNR     );
-#endif
-#if H_3D_IC
-    m_cTEncTop.setUseIC                        ( vps.getViewIndex( layer ) == 0 ? false : m_abUseIC[isDepth ? 1 : 0] );
-#endif
 #endif
   //========== Depth intra modes ==========
@@ -232,5 +191,5 @@
 #endif
     //====== Depth Inter SDC =========
-#if LGE_INTER_SDC_E0156
+#if H_3D_INTER_SDC
     m_cTEncTop.setInterSDCEnable               ( isDepth ? m_bDepthInterSDCFlag    : false );
 #endif
@@ -260,5 +219,4 @@
   m_cTEncTop.setGOPSize                      ( m_iGOPSize );
 #if H_MV
-#if H_MV5
 m_cTEncTop.setGopList                      ( m_GOPListMvc[layerIdInVps] );
   m_cTEncTop.setExtraRPSs                    ( m_extraRPSsMvc[layerIdInVps] );
@@ -269,13 +227,4 @@
   }
 #else
-  m_cTEncTop.setGopList                      ( m_GOPListMvc[layer] );
-  m_cTEncTop.setExtraRPSs                    ( m_extraRPSsMvc[layer] );
-  for(Int i = 0; i < MAX_TLAYER; i++)
-  {
-    m_cTEncTop.setNumReorderPics             ( m_numReorderPicsMvc[layer][i], i );
-    m_cTEncTop.setMaxDecPicBuffering         ( m_maxDecPicBufferingMvc[layer][i], i );
-  }
-#endif
-#else
   m_cTEncTop.setGopList                      ( m_GOPList );
   m_cTEncTop.setExtraRPSs                    ( m_extraRPSs );
@@ -291,10 +240,6 @@
   }
 #if H_MV
-#if H_MV5
   m_cTEncTop.setQP                           ( m_iQP[layerIdInVps] );
 #else
-  m_cTEncTop.setQP                           ( m_iQP[layer] );
-#endif
-#else
   m_cTEncTop.setQP                           ( m_iQP );
 #endif
@@ -303,10 +248,6 @@
 
 #if H_MV
-#if H_MV5
   m_cTEncTop.setMaxTempLayer                 ( m_maxTempLayerMvc[layerIdInVps] );
 #else
-  m_cTEncTop.setMaxTempLayer                 ( m_maxTempLayerMvc[layer] );
-#endif
-#else
   m_cTEncTop.setMaxTempLayer                 ( m_maxTempLayer );
 #endif
@@ -317,9 +258,5 @@
   //====== Loop/Deblock Filter ========
 #if H_MV
-#if H_MV5
   m_cTEncTop.setLoopFilterDisable            ( m_bLoopFilterDisable[layerIdInVps]);
-#else
-  m_cTEncTop.setLoopFilterDisable            ( m_bLoopFilterDisable[layer]);
-#endif
 #else
   m_cTEncTop.setLoopFilterDisable            ( m_bLoopFilterDisable       );
@@ -351,9 +288,5 @@
 
 #if H_MV
-#if H_MV5
   if ((m_iMaxDeltaQP == 0 ) && (m_iQP[layerIdInVps] == lowestQP) && (m_useLossless == true))
-#else
-  if ((m_iMaxDeltaQP == 0 ) && (m_iQP[layer] == lowestQP) && (m_useLossless == true))
-#endif
 #else
   if ((m_iMaxDeltaQP == 0 ) && (m_iQP == lowestQP) && (m_useLossless == true))
@@ -372,9 +305,5 @@
   m_cTEncTop.setUseLossless                  ( m_useLossless );
 #if H_MV
-#if H_MV5
   m_cTEncTop.setdQPs                         ( m_aidQP[layerIdInVps]   );
-#else
-  m_cTEncTop.setdQPs                         ( m_aidQP[layer]   );
-#endif
 #else
   m_cTEncTop.setdQPs                         ( m_aidQP        );
@@ -435,9 +364,5 @@
   m_cTEncTop.setLFCrossSliceBoundaryFlag( m_bLFCrossSliceBoundaryFlag );
 #if H_MV
-#if H_MV5
   m_cTEncTop.setUseSAO ( m_bUseSAO[layerIdInVps] );
-#else
-  m_cTEncTop.setUseSAO ( m_bUseSAO[layer] );
-#endif
 #else
   m_cTEncTop.setUseSAO ( m_bUseSAO );
@@ -512,6 +437,19 @@
   m_cTEncTop.setSignHideFlag(m_signHideFlag);
 #if RATE_CONTROL_LAMBDA_DOMAIN
-  m_cTEncTop.setUseRateCtrl         ( m_RCEnableRateControl );
+#if KWU_RC_VIEWRC_E0227 || KWU_RC_MADPRED_E0227
+  if(!m_cTEncTop.getIsDepth())    //only for texture
+  {
+    m_cTEncTop.setUseRateCtrl         ( m_RCEnableRateControl );
+  }
+  else
+  {
+    m_cTEncTop.setUseRateCtrl         ( 0 );
+  }
+#else
+    m_cTEncTop.setUseRateCtrl         ( m_RCEnableRateControl );
+#endif
+#if !KWU_RC_VIEWRC_E0227
   m_cTEncTop.setTargetBitrate       ( m_RCTargetBitrate );
+#endif
   m_cTEncTop.setKeepHierBit         ( m_RCKeepHierarchicalBit );
   m_cTEncTop.setLCULevelRC          ( m_RCLCULevelRC );
@@ -519,8 +457,162 @@
   m_cTEncTop.setInitialQP           ( m_RCInitialQP );
   m_cTEncTop.setForceIntraQP        ( m_RCForceIntraQP );
-#else
-  m_cTEncTop.setUseRateCtrl     ( m_enableRateCtrl);
-  m_cTEncTop.setTargetBitrate   ( m_targetBitrate);
-  m_cTEncTop.setNumLCUInUnit    ( m_numLCUInUnit);
+
+#if KWU_RC_MADPRED_E0227
+  if(m_cTEncTop.getUseRateCtrl() && !m_cTEncTop.getIsDepth())
+  {
+    m_cTEncTop.setUseDepthMADPred(layerIdInVps ? m_depthMADPred       : 0);
+
+    if(m_cTEncTop.getUseDepthMADPred())
+    {
+      m_cTEncTop.setCamParam(&m_cCameraData);
+    }
+  }
+#endif
+#if KWU_RC_VIEWRC_E0227
+  if(m_cTEncTop.getUseRateCtrl() && !m_cTEncTop.getIsDepth())
+  {
+    m_cTEncTop.setUseViewWiseRateCtrl(m_viewWiseRateCtrl);
+
+    if(m_iNumberOfViews == 1)
+    {
+      if(m_viewWiseRateCtrl)
+      {
+        m_cTEncTop.setTargetBitrate(m_viewTargetBits[layerIdInVps>>1]);
+      }
+      else
+      {
+        m_cTEncTop.setTargetBitrate       ( m_RCTargetBitrate );
+      }
+    }
+    else
+    {
+      if(m_viewWiseRateCtrl)
+      {
+        m_cTEncTop.setTargetBitrate(m_viewTargetBits[layerIdInVps>>1]);
+      }
+      else
+      {
+        if(m_iNumberOfViews == 2)
+        {
+          if(m_cTEncTop.getViewId() == 0)
+          {
+            m_cTEncTop.setTargetBitrate              ( (m_RCTargetBitrate*80)/100 );
+          }
+          else if(m_cTEncTop.getViewId() == 1)
+          {
+            m_cTEncTop.setTargetBitrate              ( (m_RCTargetBitrate*20)/100 );
+          }
+        }
+        else if(m_iNumberOfViews == 3)
+        {
+          if(m_cTEncTop.getViewId() == 0)
+          {
+            m_cTEncTop.setTargetBitrate              ( (m_RCTargetBitrate*66)/100 );
+          }
+          else if(m_cTEncTop.getViewId() == 1)
+          {
+            m_cTEncTop.setTargetBitrate              ( (m_RCTargetBitrate*17)/100 );
+          }
+          else if(m_cTEncTop.getViewId() == 2)
+          {
+            m_cTEncTop.setTargetBitrate              ( (m_RCTargetBitrate*17)/100 );
+          }
+        }
+        else
+        {
+          m_cTEncTop.setTargetBitrate              ( m_RCTargetBitrate );
+        }
+      }
+    }
+  }
+#endif
+#else
+#if KWU_RC_VIEWRC_E0227 || KWU_RC_MADPRED_E0227
+  if(!m_cTEncTop.getIsDepth())    //only for texture
+  {
+    m_cTEncTop.setUseRateCtrl         ( m_enableRateCtrl );
+    m_cTEncTop.setTargetBitrate       ( m_targetBitrate );
+    m_cTEncTop.setNumLCUInUnit        ( m_numLCUInUnit);
+  }
+  else
+  {
+    m_cTEncTop.setUseRateCtrl         ( 0 );
+  }
+#else
+  m_cTEncTop.setUseRateCtrl         ( m_enableRateCtrl );
+  m_cTEncTop.setTargetBitrate       ( m_targetBitrate );
+  m_cTEncTop.setNumLCUInUnit        ( m_numLCUInUnit);
+#endif
+
+  
+#if KWU_RC_MADPRED_E0227
+  if(m_cTEncTop.getUseRateCtrl() && !m_cTEncTop.getIsDepth())
+  {
+    m_cTEncTop.setUseDepthMADPred(layerIdInVps ? m_depthMADPred       : 0);
+
+    if(m_cTEncTop.getUseDepthMADPred())
+    {
+      m_cTEncTop.setCamParam(&m_cCameraData);
+    }
+  }
+#endif
+
+#if KWU_RC_VIEWRC_E0227
+  if(m_cTEncTop.getUseRateCtrl() && !m_cTEncTop.getIsDepth())
+  {
+    m_cTEncTop.setUseViewWiseRateCtrl(m_viewWiseRateCtrl);
+    if(m_iNumberOfViews == 1)
+    {
+      if(m_viewWiseRateCtrl)
+      {
+        m_cTEncTop.setTargetBitrate(m_viewTargetBits[layerIdInVps>>1]);
+      }
+      else
+      {
+        m_cTEncTop.setTargetBitrate       ( m_targetBitrate );
+      }
+    }
+    else
+    {
+      if(m_viewWiseRateCtrl)
+      {
+        m_cTEncTop.setTargetBitrate(m_viewTargetBits[layerIdInVps>>1]);
+      }
+      else
+      {
+        if(m_iNumberOfViews == 2)
+        {
+          if(m_cTEncTop.getViewId() == 0)
+          {
+            m_cTEncTop.setTargetBitrate              ( (m_targetBitrate*80)/100 );
+          }
+          else if(m_cTEncTop.getViewId() == 1)
+          {
+            m_cTEncTop.setTargetBitrate              ( (m_targetBitrate*20)/100 );
+          }
+        }
+        else if(m_iNumberOfViews == 3)
+        {
+          if(m_cTEncTop.getViewId() == 0)
+          {
+            m_cTEncTop.setTargetBitrate              ( (m_targetBitrate*66)/100 );
+          }
+          else if(m_cTEncTop.getViewId() == 1)
+          {
+            m_cTEncTop.setTargetBitrate              ( (m_targetBitrate*17)/100 );
+          }
+          else if(m_cTEncTop.getViewId() == 2)
+          {
+            m_cTEncTop.setTargetBitrate              ( (m_targetBitrate*17)/100 );
+          }
+        }
+        else
+        {
+          m_cTEncTop.setTargetBitrate              ( m_targetBitrate );
+        }
+      }
+    }
+  }
+#endif
 #endif
   m_cTEncTop.setTransquantBypassEnableFlag(m_TransquantBypassEnableFlag);
@@ -664,13 +756,17 @@
 }
 
-Void TAppEncTop::xInitLib()
+Void TAppEncTop::xInitLib(Bool isFieldCoding)
 {
 #if H_MV
   for(Int layer=0; layer<m_numberOfLayers; layer++)
   {
-    m_acTEncTopList[layer]->init( );
-  }
-#else
-  m_cTEncTop.init();
+#if KWU_RC_MADPRED_E0227
+    m_acTEncTopList[layer]->init( isFieldCoding, this );
+#else
+    m_acTEncTopList[layer]->init( isFieldCoding );
+#endif
+  }
+#else
+  m_cTEncTop.init( isFieldCoding );
 #endif
 }
@@ -703,5 +799,5 @@
   xInitLibCfg();
   xCreateLib();
-  xInitLib();
+  xInitLib(m_isField);
   
   // main encoder loop
@@ -728,5 +824,12 @@
 
   // allocate original YUV buffer
+  if( m_isField )
+  {
+    pcPicYuvOrg->create( m_iSourceWidth, m_iSourceHeightOrg, m_uiMaxCUWidth, m_uiMaxCUHeight, m_uiMaxCUDepth );
+  }
+  else
+  {
   pcPicYuvOrg->create( m_iSourceWidth, m_iSourceHeight, m_uiMaxCUWidth, m_uiMaxCUHeight, m_uiMaxCUDepth );
+  }
   
 #if H_MV
@@ -789,9 +892,20 @@
       }
     }
+
+#if !RATE_CONTROL_LAMBDA_DOMAIN && KWU_FIX_URQ
+    for(Int layer=0; layer < m_numberOfLayers; layer++ )
+    {
+      if(m_acTEncTopList[layer]->getUseRateCtrl()  && !m_acTEncTopList[layer]->getIsDepth())
+      {
+        m_acTEncTopList[layer]->getRateCtrl()->updateRCGOPStatus();
+      }
+    }
+#endif
+
     gopSize = maxGopSize;
   }
   for(Int layer=0; layer < m_numberOfLayers; layer++ )
   {
-    m_acTEncTopList[layer]->printSummary( m_acTEncTopList[layer]->getNumAllPicCoded() );
+    m_acTEncTopList[layer]->printSummary( m_acTEncTopList[layer]->getNumAllPicCoded(), m_isField );
   }
 #else
@@ -807,5 +921,5 @@
     m_iFrameRcvd++;
 
-    bEos = (m_iFrameRcvd == m_framesToBeEncoded);
+    bEos = (m_isField && (m_iFrameRcvd == (m_framesToBeEncoded >> 1) )) || ( !m_isField && (m_iFrameRcvd == m_framesToBeEncoded) );
 
     Bool flush = 0;
@@ -820,5 +934,12 @@
 
     // call encoding function for one frame
+    if ( m_isField )
+    {
+      m_cTEncTop.encode( bEos, flush ? 0 : pcPicYuvOrg, m_cListPicYuvRec, outputAccessUnits, iNumEncoded, m_isTopFieldFirst);
+    }
+    else
+    {
     m_cTEncTop.encode( bEos, flush ? 0 : pcPicYuvOrg, m_cListPicYuvRec, outputAccessUnits, iNumEncoded );
+    }
     
     // write bistream to file if necessary
@@ -830,5 +951,5 @@
   }
 
-  m_cTEncTop.printSummary();
+  m_cTEncTop.printSummary(m_isField);
 #endif
 
@@ -941,30 +1062,35 @@
 #endif
 {
-  Int i;
-  
-#if H_MV
-  if( iNumEncoded > 0 )
-  {
-    TComList<TComPicYuv*>::iterator iterPicYuvRec = m_picYuvRec[layerId]->end();
-#else
-  TComList<TComPicYuv*>::iterator iterPicYuvRec = m_cListPicYuvRec.end();
-  list<AccessUnit>::const_iterator iterBitstream = accessUnits.begin();
-#endif
-
-  for ( i = 0; i < iNumEncoded; i++ )
-  {
-    --iterPicYuvRec;
-  }
-  
-  for ( i = 0; i < iNumEncoded; i++ )
-  {
-    TComPicYuv*  pcPicYuvRec  = *(iterPicYuvRec++);
+  if (m_isField)
+  {
+    //Reinterlace fields
+    Int i;
+#if H_MV
+    if( iNumEncoded > 0 )
+    {
+      TComList<TComPicYuv*>::iterator iterPicYuvRec = m_picYuvRec[layerId]->end();
+#else
+    TComList<TComPicYuv*>::iterator iterPicYuvRec = m_cListPicYuvRec.end();
+    list<AccessUnit>::const_iterator iterBitstream = accessUnits.begin();
+#endif
+
+    for ( i = 0; i < iNumEncoded; i++ )
+    {
+      --iterPicYuvRec;
+    }
+
+    for ( i = 0; i < iNumEncoded/2; i++ )
+    {
+      TComPicYuv*  pcPicYuvRecTop  = *(iterPicYuvRec++);
+      TComPicYuv*  pcPicYuvRecBottom  = *(iterPicYuvRec++);
+
 #if H_MV
       if (m_pchReconFileList[layerId])
       {
-        m_acTVideoIOYuvReconFileList[layerId]->write( pcPicYuvRec, m_confLeft, m_confRight, m_confTop, m_confBottom );
-      }
-    }
-  }
+        m_acTVideoIOYuvReconFileList[layerId]->write( pcPicYuvRecTop, pcPicYuvRecBottom, m_confLeft, m_confRight, m_confTop, m_confBottom, m_isTopFieldFirst );
+      }
+    }
+  }
+
   if( ! accessUnits.empty() )
   {
@@ -972,4 +1098,56 @@
     for( aUIter = accessUnits.begin(); aUIter != accessUnits.end(); aUIter++ )
     {
+      const vector<UInt>& stats = writeAnnexB(bitstreamFile, *aUIter);
+      rateStatsAccum(*aUIter, stats);
+    }
+  }
+#else
+      if (m_pchReconFile)
+      {
+        m_cTVideoIOYuvReconFile.write( pcPicYuvRecTop, pcPicYuvRecBottom, m_confLeft, m_confRight, m_confTop, m_confBottom, m_isTopFieldFirst );
+      }
+
+      const AccessUnit& auTop = *(iterBitstream++);
+      const vector<UInt>& statsTop = writeAnnexB(bitstreamFile, auTop);
+      rateStatsAccum(auTop, statsTop);
+
+      const AccessUnit& auBottom = *(iterBitstream++);
+      const vector<UInt>& statsBottom = writeAnnexB(bitstreamFile, auBottom);
+      rateStatsAccum(auBottom, statsBottom);
+    }
+#endif
+  }
+  else
+  {
+    Int i;
+#if H_MV
+    if( iNumEncoded > 0 )
+    {
+      TComList<TComPicYuv*>::iterator iterPicYuvRec = m_picYuvRec[layerId]->end();
+#else
+    TComList<TComPicYuv*>::iterator iterPicYuvRec = m_cListPicYuvRec.end();
+    list<AccessUnit>::const_iterator iterBitstream = accessUnits.begin();
+#endif
+
+    for ( i = 0; i < iNumEncoded; i++ )
+    {
+      --iterPicYuvRec;
+    }
+
+    for ( i = 0; i < iNumEncoded; i++ )
+    {
+      TComPicYuv*  pcPicYuvRec  = *(iterPicYuvRec++);
+#if H_MV
+      if (m_pchReconFileList[layerId])
+      {
+        m_acTVideoIOYuvReconFileList[layerId]->write( pcPicYuvRec, m_confLeft, m_confRight, m_confTop, m_confBottom );
+      }    
+    }
+  }
+  if( ! accessUnits.empty() )
+  {
+    list<AccessUnit>::iterator aUIter;
+    for( aUIter = accessUnits.begin(); aUIter != accessUnits.end(); aUIter++ )
+    {
       const vector<unsigned>& stats = writeAnnexB(bitstreamFile, *aUIter);
       rateStatsAccum(*aUIter, stats);
@@ -977,16 +1155,17 @@
   }
 #else
-    if (m_pchReconFile)
-    {
-      m_cTVideoIOYuvReconFile.write( pcPicYuvRec, m_confLeft, m_confRight, m_confTop, m_confBottom );
-    }
-
-    const AccessUnit& au = *(iterBitstream++);
-    const vector<UInt>& stats = writeAnnexB(bitstreamFile, au);
-    rateStatsAccum(au, stats);
-  }
-#endif
-}
-
+      if (m_pchReconFile)
+      {
+        m_cTVideoIOYuvReconFile.write( pcPicYuvRec, m_confLeft, m_confRight, m_confTop, m_confBottom );
+      }
+
+      const AccessUnit& au = *(iterBitstream++);
+      const vector<UInt>& stats = writeAnnexB(bitstreamFile, au);
+      rateStatsAccum(au, stats);   
+    }
+#endif    
+  }
+}
+  
 /**
  *
@@ -1121,17 +1300,9 @@
 Void TAppEncTop::xSetDimensionIdAndLength( TComVPS& vps )
 {   
-#if H_MV5
   vps.setScalabilityMaskFlag( m_scalabilityMask ); 
-#else
-  vps.setScalabilityMask( m_scalabilityMask ); 
-#endif
   for( Int dim = 0; dim < m_dimIds.size(); dim++ )
   {
     vps.setDimensionIdLen( dim, m_dimensionIdLen[ dim ] );
-#if H_MV5
     for( Int layer = 0; layer <= vps.getMaxLayersMinus1(); layer++ )
-#else
-    for( Int layer = 0; layer < vps.getMaxLayers(); layer++ )
-#endif
 
     {        
@@ -1140,5 +1311,4 @@
   }
 
-#if H_MV5
   Int maxViewId = xGetMax( m_viewId ); 
 
@@ -1153,5 +1323,4 @@
 
   assert( m_iNumberOfViews == vps.getNumViews() ); 
-#endif
 }
 
@@ -1180,7 +1349,5 @@
 
   // Max temporal id for inter layer reference pictures + presence flag
-#if H_MV5
   Bool maxTidRefPresentFlag = false; 
-#endif
   for ( Int refLayerIdInVps = 0; refLayerIdInVps < m_numberOfLayers; refLayerIdInVps++)
     {
@@ -1203,27 +1370,15 @@
     }
     vps.setMaxTidIlRefPicPlus1( refLayerIdInVps, maxTid + 1 );
-#if H_MV5
     maxTidRefPresentFlag = maxTidRefPresentFlag || ( maxTid != 6 );    
-#endif
-  }
-
-#if H_MV5
+  }
+
   vps.setMaxTidRefPresentFlag( maxTidRefPresentFlag );
-#endif
   // Max one active ref layer flag
   Bool maxOneActiveRefLayerFlag = true;  
-#if H_MV5
   for ( Int layerIdInVps = 1; layerIdInVps < m_numberOfLayers && maxOneActiveRefLayerFlag; layerIdInVps++)
-#else
-  for ( Int currLayerIdInVps = 1; currLayerIdInVps < m_numberOfLayers && maxOneActiveRefLayerFlag; currLayerIdInVps++)
-#endif
   {
     for( Int i = 0; i < ( getGOPSize() + 1) && maxOneActiveRefLayerFlag; i++ ) 
     {        
-#if H_MV5
       GOPEntry ge =  m_GOPListMvc[layerIdInVps][ ( i < getGOPSize()  ? i : MAX_GOP ) ]; 
-#else
-      GOPEntry ge =  m_GOPListMvc[currLayerIdInVps][ ( i < getGOPSize()  ? i : MAX_GOP ) ]; 
-#endif
       maxOneActiveRefLayerFlag =  maxOneActiveRefLayerFlag && (ge.m_numActiveRefLayerPics <= 1); 
     }            
@@ -1233,5 +1388,4 @@
   
   
-#if H_MV5
   // All Ref layers active flag
   Bool allRefLayersActiveFlag = true; 
@@ -1249,9 +1403,7 @@
   // Currently cross layer irap aligned is always true.   
   vps.setCrossLayerIrapAlignedFlag( true ); 
-#endif
   vps.setRefLayers(); 
 }; 
 
-#if H_MV5
 GOPEntry* TAppEncTop::xGetGopEntry( Int layerIdInVps, Int poc )
 {
@@ -1268,5 +1420,4 @@
   return geFound; 
 }
-#endif
 
 Void TAppEncTop::xSetLayerIds( TComVPS& vps )
@@ -1308,5 +1459,4 @@
 }
 
-#if H_MV5
 
 Void TAppEncTop::xSetRepFormat( TComVPS& vps )
@@ -1333,5 +1483,4 @@
   }
 }
-#endif
 
 Void TAppEncTop::xSetLayerSets( TComVPS& vps )
@@ -1386,5 +1535,4 @@
   }
 }
-#if H_MV5
 
 Void TAppEncTop::xSetVPSVUI( TComVPS& vps )
@@ -1478,14 +1626,9 @@
 }
 #endif
-#endif
 #if H_3D
 Void TAppEncTop::xSetVPSExtension2( TComVPS& vps )
 {
-#if H_MV5
 
   for ( Int layer = 0; layer <= vps.getMaxLayersMinus1(); layer++ )
-#else
-  for ( Int layer = 0; layer < vps.getMaxLayers(); layer++ )
-#endif
   {
     Bool isDepth      = ( vps.getDepthId( layer ) == 1 ) ;
@@ -1517,5 +1660,5 @@
     vps.setViewSynthesisPredFlag( layer, !isLayerZero && !isDepth && m_viewSynthesisPredFlag );         
 #endif      
-#if LGE_INTER_SDC_E0156
+#if H_3D_INTER_SDC
     vps.setInterSDCFlag( layer, !isLayerZero && isDepth && m_bDepthInterSDCFlag );
 #endif
Index: trunk/source/App/TAppEncoder/TAppEncTop.h
===================================================================
--- trunk/source/App/TAppEncoder/TAppEncTop.h	(revision 622)
+++ trunk/source/App/TAppEncoder/TAppEncTop.h	(revision 655)
@@ -50,4 +50,7 @@
 #endif
 
+#if KWU_RC_MADPRED_E0227
+class TEncTop;
+#endif
 //! \ingroup TAppEncoder
 //! \{
@@ -93,5 +96,5 @@
   Void  xCreateLib        ();                               ///< create files & encoder class
   Void  xInitLibCfg       ();                               ///< initialize internal variables
-  Void  xInitLib          ();                               ///< initialize encoder class
+  Void  xInitLib          (Bool isFieldCoding);             ///< initialize encoder class
   Void  xDestroyLib       ();                               ///< destroy encoder class
   
@@ -121,9 +124,7 @@
   Void xSetLayerSets              ( TComVPS& vps );
   Void xSetProfileTierLevel       ( TComVPS& vps );
-#if H_MV5
   Void xSetRepFormat              ( TComVPS& vps );
   Void xSetVPSVUI                 ( TComVPS& vps );
   GOPEntry* xGetGopEntry( Int layerIdInVps, Int poc );
-#endif
   Int  xGetMax( std::vector<Int>& vec);
 #endif
